]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/config/tc-i386.c
x86/Intel: restrict suffix derivation
[thirdparty/binutils-gdb.git] / gas / config / tc-i386.c
CommitLineData
b534c6d3 1/* tc-i386.c -- Assemble code for the Intel 80386
a2c58332 2 Copyright (C) 1989-2022 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"
d2b2c203 33#include "elf/x86-64.h"
40fb9820 34#include "opcodes/i386-init.h"
41fd2579 35#include <limits.h>
41fd2579 36
c3332e24 37#ifndef INFER_ADDR_PREFIX
eecb386c 38#define INFER_ADDR_PREFIX 1
c3332e24
AM
39#endif
40
29b0f896
AM
41#ifndef DEFAULT_ARCH
42#define DEFAULT_ARCH "i386"
246fcdee 43#endif
252b5132 44
edde18a5
AM
45#ifndef INLINE
46#if __GNUC__ >= 2
47#define INLINE __inline__
48#else
49#define INLINE
50#endif
51#endif
52
6305a203
L
53/* Prefixes will be emitted in the order defined below.
54 WAIT_PREFIX must be the first prefix since FWAIT is really is an
55 instruction, and so must come before any prefixes.
56 The preferred prefix order is SEG_PREFIX, ADDR_PREFIX, DATA_PREFIX,
42164a71 57 REP_PREFIX/HLE_PREFIX, LOCK_PREFIX. */
6305a203
L
58#define WAIT_PREFIX 0
59#define SEG_PREFIX 1
60#define ADDR_PREFIX 2
61#define DATA_PREFIX 3
c32fa91d 62#define REP_PREFIX 4
42164a71 63#define HLE_PREFIX REP_PREFIX
7e8b059b 64#define BND_PREFIX REP_PREFIX
c32fa91d 65#define LOCK_PREFIX 5
4e9ac44a
L
66#define REX_PREFIX 6 /* must come last. */
67#define MAX_PREFIXES 7 /* max prefixes per opcode */
6305a203
L
68
69/* we define the syntax here (modulo base,index,scale syntax) */
70#define REGISTER_PREFIX '%'
71#define IMMEDIATE_PREFIX '$'
72#define ABSOLUTE_PREFIX '*'
73
74/* these are the instruction mnemonic suffixes in AT&T syntax or
75 memory operand size in Intel syntax. */
76#define WORD_MNEM_SUFFIX 'w'
77#define BYTE_MNEM_SUFFIX 'b'
78#define SHORT_MNEM_SUFFIX 's'
79#define LONG_MNEM_SUFFIX 'l'
80#define QWORD_MNEM_SUFFIX 'q'
6305a203
L
81/* Intel Syntax. Use a non-ascii letter since since it never appears
82 in instructions. */
83#define LONG_DOUBLE_MNEM_SUFFIX '\1'
84
85#define END_OF_INSN '\0'
86
79dec6b7
JB
87/* This matches the C -> StaticRounding alias in the opcode table. */
88#define commutative staticrounding
89
6305a203
L
90/*
91 'templates' is for grouping together 'template' structures for opcodes
92 of the same name. This is only used for storing the insns in the grand
93 ole hash table of insns.
94 The templates themselves start at START and range up to (but not including)
95 END.
96 */
97typedef struct
98{
d3ce72d0
NC
99 const insn_template *start;
100 const insn_template *end;
6305a203
L
101}
102templates;
103
104/* 386 operand encoding bytes: see 386 book for details of this. */
105typedef struct
106{
107 unsigned int regmem; /* codes register or memory operand */
108 unsigned int reg; /* codes register operand (or extended opcode) */
109 unsigned int mode; /* how to interpret regmem & reg */
110}
111modrm_byte;
112
113/* x86-64 extension prefix. */
114typedef int rex_byte;
115
6305a203
L
116/* 386 opcode byte to code indirect addressing. */
117typedef struct
118{
119 unsigned base;
120 unsigned index;
121 unsigned scale;
122}
123sib_byte;
124
6305a203
L
125/* x86 arch names, types and features */
126typedef struct
127{
128 const char *name; /* arch name */
6ceeed25
JB
129 unsigned int len:8; /* arch string length */
130 bool skip:1; /* show_arch should skip this. */
6305a203 131 enum processor_type type; /* arch type */
ae89daec
JB
132 i386_cpu_flags enable; /* cpu feature enable flags */
133 i386_cpu_flags disable; /* cpu feature disable flags */
6305a203
L
134}
135arch_entry;
136
78f12dd3 137static void update_code_flag (int, int);
e3bb37b5
L
138static void set_code_flag (int);
139static void set_16bit_gcc_code_flag (int);
140static void set_intel_syntax (int);
1efbbeb4 141static void set_intel_mnemonic (int);
db51cc60 142static void set_allow_index_reg (int);
7bab8ab5 143static void set_check (int);
e3bb37b5 144static void set_cpu_arch (int);
6482c264 145#ifdef TE_PE
e3bb37b5 146static void pe_directive_secrel (int);
145667f8 147static void pe_directive_secidx (int);
6482c264 148#endif
e3bb37b5
L
149static void signed_cons (int);
150static char *output_invalid (int c);
ee86248c
JB
151static int i386_finalize_immediate (segT, expressionS *, i386_operand_type,
152 const char *);
153static int i386_finalize_displacement (segT, expressionS *, i386_operand_type,
154 const char *);
a7619375 155static int i386_att_operand (char *);
e3bb37b5 156static int i386_intel_operand (char *, int);
ee86248c
JB
157static int i386_intel_simplify (expressionS *);
158static int i386_intel_parse_name (const char *, expressionS *);
e3bb37b5
L
159static const reg_entry *parse_register (char *, char **);
160static char *parse_insn (char *, char *);
161static char *parse_operands (char *, const char *);
162static void swap_operands (void);
783c187b 163static void swap_2_operands (unsigned int, unsigned int);
48bcea9f 164static enum flag_code i386_addressing_mode (void);
e3bb37b5
L
165static void optimize_imm (void);
166static void optimize_disp (void);
83b16ac6 167static const insn_template *match_template (char);
e3bb37b5
L
168static int check_string (void);
169static int process_suffix (void);
170static int check_byte_reg (void);
171static int check_long_reg (void);
172static int check_qword_reg (void);
173static int check_word_reg (void);
174static int finalize_imm (void);
175static int process_operands (void);
5e042380 176static const reg_entry *build_modrm_byte (void);
e3bb37b5
L
177static void output_insn (void);
178static void output_imm (fragS *, offsetT);
179static void output_disp (fragS *, offsetT);
29b0f896 180#ifndef I386COFF
e3bb37b5 181static void s_bss (int);
252b5132 182#endif
17d4e2a2
L
183#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
184static void handle_large_common (int small ATTRIBUTE_UNUSED);
b4a3a7b4
L
185
186/* GNU_PROPERTY_X86_ISA_1_USED. */
187static unsigned int x86_isa_1_used;
188/* GNU_PROPERTY_X86_FEATURE_2_USED. */
189static unsigned int x86_feature_2_used;
190/* Generate x86 used ISA and feature properties. */
191static unsigned int x86_used_note = DEFAULT_X86_USED_NOTE;
17d4e2a2 192#endif
252b5132 193
a847613f 194static const char *default_arch = DEFAULT_ARCH;
3e73aa7c 195
8a6fb3f9
JB
196/* parse_register() returns this when a register alias cannot be used. */
197static const reg_entry bad_reg = { "<bad>", OPERAND_TYPE_NONE, 0, 0,
198 { Dw2Inval, Dw2Inval } };
199
34684862 200static const reg_entry *reg_eax;
5e042380
JB
201static const reg_entry *reg_ds;
202static const reg_entry *reg_es;
203static const reg_entry *reg_ss;
6288d05f 204static const reg_entry *reg_st0;
6225c532
JB
205static const reg_entry *reg_k0;
206
c0f3af97
L
207/* VEX prefix. */
208typedef struct
209{
43234a1e
L
210 /* VEX prefix is either 2 byte or 3 byte. EVEX is 4 byte. */
211 unsigned char bytes[4];
c0f3af97
L
212 unsigned int length;
213 /* Destination or source register specifier. */
214 const reg_entry *register_specifier;
215} vex_prefix;
216
252b5132 217/* 'md_assemble ()' gathers together information and puts it into a
47926f60 218 i386_insn. */
252b5132 219
520dc8e8
AM
220union i386_op
221 {
222 expressionS *disps;
223 expressionS *imms;
224 const reg_entry *regs;
225 };
226
a65babc9
L
227enum i386_error
228 {
b4d65f2d 229 no_error, /* Must be first. */
86e026a4 230 operand_size_mismatch,
a65babc9
L
231 operand_type_mismatch,
232 register_type_mismatch,
233 number_of_operands_mismatch,
234 invalid_instruction_suffix,
235 bad_imm4,
a65babc9
L
236 unsupported_with_intel_mnemonic,
237 unsupported_syntax,
6c30d220 238 unsupported,
260cd341 239 invalid_sib_address,
6c30d220 240 invalid_vsib_address,
7bab8ab5 241 invalid_vector_register_set,
260cd341 242 invalid_tmm_register_set,
0cc78721 243 invalid_dest_and_src_register_set,
43234a1e
L
244 unsupported_vector_index_register,
245 unsupported_broadcast,
43234a1e
L
246 broadcast_needed,
247 unsupported_masking,
248 mask_not_on_destination,
249 no_default_mask,
250 unsupported_rc_sae,
43234a1e 251 invalid_register_operand,
a65babc9
L
252 };
253
252b5132
RH
254struct _i386_insn
255 {
47926f60 256 /* TM holds the template for the insn were currently assembling. */
d3ce72d0 257 insn_template tm;
252b5132 258
7d5e4556
L
259 /* SUFFIX holds the instruction size suffix for byte, word, dword
260 or qword, if given. */
252b5132
RH
261 char suffix;
262
9a182d04
JB
263 /* OPCODE_LENGTH holds the number of base opcode bytes. */
264 unsigned char opcode_length;
265
47926f60 266 /* OPERANDS gives the number of given operands. */
252b5132
RH
267 unsigned int operands;
268
269 /* REG_OPERANDS, DISP_OPERANDS, MEM_OPERANDS, IMM_OPERANDS give the number
270 of given register, displacement, memory operands and immediate
47926f60 271 operands. */
252b5132
RH
272 unsigned int reg_operands, disp_operands, mem_operands, imm_operands;
273
274 /* TYPES [i] is the type (see above #defines) which tells us how to
520dc8e8 275 use OP[i] for the corresponding operand. */
40fb9820 276 i386_operand_type types[MAX_OPERANDS];
252b5132 277
520dc8e8
AM
278 /* Displacement expression, immediate expression, or register for each
279 operand. */
280 union i386_op op[MAX_OPERANDS];
252b5132 281
3e73aa7c
JH
282 /* Flags for operands. */
283 unsigned int flags[MAX_OPERANDS];
284#define Operand_PCrel 1
c48dadc9 285#define Operand_Mem 2
3e73aa7c 286
252b5132 287 /* Relocation type for operand */
f86103b7 288 enum bfd_reloc_code_real reloc[MAX_OPERANDS];
252b5132 289
252b5132
RH
290 /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
291 the base index byte below. */
292 const reg_entry *base_reg;
293 const reg_entry *index_reg;
294 unsigned int log2_scale_factor;
295
296 /* SEG gives the seg_entries of this insn. They are zero unless
47926f60 297 explicit segment overrides are given. */
5e042380 298 const reg_entry *seg[2];
252b5132 299
8325cc63
JB
300 /* Copied first memory operand string, for re-checking. */
301 char *memop1_string;
302
252b5132
RH
303 /* PREFIX holds all the given prefix opcodes (usually null).
304 PREFIXES is the number of prefix opcodes. */
305 unsigned int prefixes;
306 unsigned char prefix[MAX_PREFIXES];
307
50128d0c 308 /* Register is in low 3 bits of opcode. */
5b7c81bd 309 bool short_form;
50128d0c 310
6f2f06be 311 /* The operand to a branch insn indicates an absolute branch. */
5b7c81bd 312 bool jumpabsolute;
6f2f06be 313
921eafea
L
314 /* Extended states. */
315 enum
316 {
317 /* Use MMX state. */
318 xstate_mmx = 1 << 0,
319 /* Use XMM state. */
320 xstate_xmm = 1 << 1,
321 /* Use YMM state. */
322 xstate_ymm = 1 << 2 | xstate_xmm,
323 /* Use ZMM state. */
324 xstate_zmm = 1 << 3 | xstate_ymm,
325 /* Use TMM state. */
32930e4e
L
326 xstate_tmm = 1 << 4,
327 /* Use MASK state. */
328 xstate_mask = 1 << 5
921eafea 329 } xstate;
260cd341 330
e379e5f3 331 /* Has GOTPC or TLS relocation. */
5b7c81bd 332 bool has_gotpc_tls_reloc;
e379e5f3 333
252b5132 334 /* RM and SIB are the modrm byte and the sib byte where the
c1e679ec 335 addressing modes of this insn are encoded. */
252b5132 336 modrm_byte rm;
3e73aa7c 337 rex_byte rex;
43234a1e 338 rex_byte vrex;
252b5132 339 sib_byte sib;
c0f3af97 340 vex_prefix vex;
b6169b20 341
6225c532
JB
342 /* Masking attributes.
343
344 The struct describes masking, applied to OPERAND in the instruction.
345 REG is a pointer to the corresponding mask register. ZEROING tells
346 whether merging or zeroing mask is used. */
347 struct Mask_Operation
348 {
349 const reg_entry *reg;
350 unsigned int zeroing;
351 /* The operand where this operation is associated. */
352 unsigned int operand;
353 } mask;
43234a1e
L
354
355 /* Rounding control and SAE attributes. */
ca5312a2
JB
356 struct RC_Operation
357 {
358 enum rc_type
359 {
360 rc_none = -1,
361 rne,
362 rd,
363 ru,
364 rz,
365 saeonly
366 } type;
7063667e
JB
367 /* In Intel syntax the operand modifier form is supposed to be used, but
368 we continue to accept the immediate forms as well. */
369 bool modifier;
ca5312a2 370 } rounding;
43234a1e 371
5273a3cd
JB
372 /* Broadcasting attributes.
373
374 The struct describes broadcasting, applied to OPERAND. TYPE is
375 expresses the broadcast factor. */
376 struct Broadcast_Operation
377 {
0cc78721 378 /* Type of broadcast: {1to2}, {1to4}, {1to8}, {1to16} or {1to32}. */
5273a3cd
JB
379 unsigned int type;
380
381 /* Index of broadcasted operand. */
382 unsigned int operand;
383
384 /* Number of bytes to broadcast. */
385 unsigned int bytes;
386 } broadcast;
43234a1e
L
387
388 /* Compressed disp8*N attribute. */
389 unsigned int memshift;
390
86fa6981
L
391 /* Prefer load or store in encoding. */
392 enum
393 {
394 dir_encoding_default = 0,
395 dir_encoding_load,
64c49ab3
JB
396 dir_encoding_store,
397 dir_encoding_swap
86fa6981 398 } dir_encoding;
891edac4 399
41eb8e88 400 /* Prefer 8bit, 16bit, 32bit displacement in encoding. */
a501d77e
L
401 enum
402 {
403 disp_encoding_default = 0,
404 disp_encoding_8bit,
41eb8e88 405 disp_encoding_16bit,
a501d77e
L
406 disp_encoding_32bit
407 } disp_encoding;
f8a5c266 408
6b6b6807 409 /* Prefer the REX byte in encoding. */
5b7c81bd 410 bool rex_encoding;
6b6b6807 411
b6f8c7c4 412 /* Disable instruction size optimization. */
5b7c81bd 413 bool no_optimize;
b6f8c7c4 414
86fa6981
L
415 /* How to encode vector instructions. */
416 enum
417 {
418 vex_encoding_default = 0,
42e04b36 419 vex_encoding_vex,
86fa6981 420 vex_encoding_vex3,
da4977e0
JB
421 vex_encoding_evex,
422 vex_encoding_error
86fa6981
L
423 } vec_encoding;
424
d5de92cf
L
425 /* REP prefix. */
426 const char *rep_prefix;
427
165de32a
L
428 /* HLE prefix. */
429 const char *hle_prefix;
42164a71 430
7e8b059b
L
431 /* Have BND prefix. */
432 const char *bnd_prefix;
433
04ef582a
L
434 /* Have NOTRACK prefix. */
435 const char *notrack_prefix;
436
891edac4 437 /* Error message. */
a65babc9 438 enum i386_error error;
252b5132
RH
439 };
440
441typedef struct _i386_insn i386_insn;
442
43234a1e
L
443/* Link RC type with corresponding string, that'll be looked for in
444 asm. */
445struct RC_name
446{
447 enum rc_type type;
448 const char *name;
449 unsigned int len;
450};
451
452static const struct RC_name RC_NamesTable[] =
453{
454 { rne, STRING_COMMA_LEN ("rn-sae") },
455 { rd, STRING_COMMA_LEN ("rd-sae") },
456 { ru, STRING_COMMA_LEN ("ru-sae") },
457 { rz, STRING_COMMA_LEN ("rz-sae") },
458 { saeonly, STRING_COMMA_LEN ("sae") },
459};
460
252b5132
RH
461/* List of chars besides those in app.c:symbol_chars that can start an
462 operand. Used to prevent the scrubber eating vital white-space. */
86fa6981 463const char extra_symbol_chars[] = "*%-([{}"
252b5132 464#ifdef LEX_AT
32137342
NC
465 "@"
466#endif
467#ifdef LEX_QM
468 "?"
252b5132 469#endif
32137342 470 ;
252b5132 471
b3983e5f
JB
472#if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
473 && !defined (TE_GNU) \
474 && !defined (TE_LINUX) \
d85e70a3 475 && !defined (TE_Haiku) \
b3983e5f
JB
476 && !defined (TE_FreeBSD) \
477 && !defined (TE_DragonFly) \
478 && !defined (TE_NetBSD))
252b5132 479/* This array holds the chars that always start a comment. If the
b3b91714
AM
480 pre-processor is disabled, these aren't very useful. The option
481 --divide will remove '/' from this list. */
482const char *i386_comment_chars = "#/";
483#define SVR4_COMMENT_CHARS 1
252b5132 484#define PREFIX_SEPARATOR '\\'
252b5132 485
b3b91714
AM
486#else
487const char *i386_comment_chars = "#";
488#define PREFIX_SEPARATOR '/'
489#endif
490
252b5132
RH
491/* This array holds the chars that only start a comment at the beginning of
492 a line. If the line seems to have the form '# 123 filename'
ce8a8b2f
AM
493 .line and .file directives will appear in the pre-processed output.
494 Note that input_file.c hand checks for '#' at the beginning of the
252b5132 495 first line of the input file. This is because the compiler outputs
ce8a8b2f
AM
496 #NO_APP at the beginning of its output.
497 Also note that comments started like this one will always work if
252b5132 498 '/' isn't otherwise defined. */
b3b91714 499const char line_comment_chars[] = "#/";
252b5132 500
63a0b638 501const char line_separator_chars[] = ";";
252b5132 502
ce8a8b2f
AM
503/* Chars that can be used to separate mant from exp in floating point
504 nums. */
252b5132
RH
505const char EXP_CHARS[] = "eE";
506
ce8a8b2f
AM
507/* Chars that mean this number is a floating point constant
508 As in 0f12.456
509 or 0d1.2345e12. */
de133cf9 510const char FLT_CHARS[] = "fFdDxXhHbB";
252b5132 511
ce8a8b2f 512/* Tables for lexical analysis. */
252b5132
RH
513static char mnemonic_chars[256];
514static char register_chars[256];
515static char operand_chars[256];
516static char identifier_chars[256];
252b5132 517
ce8a8b2f 518/* Lexical macros. */
252b5132
RH
519#define is_mnemonic_char(x) (mnemonic_chars[(unsigned char) x])
520#define is_operand_char(x) (operand_chars[(unsigned char) x])
521#define is_register_char(x) (register_chars[(unsigned char) x])
522#define is_space_char(x) ((x) == ' ')
523#define is_identifier_char(x) (identifier_chars[(unsigned char) x])
252b5132 524
0234cb7c 525/* All non-digit non-letter characters that may occur in an operand. */
252b5132
RH
526static char operand_special_chars[] = "%$-+(,)*._~/<>|&^!:[@]";
527
528/* md_assemble() always leaves the strings it's passed unaltered. To
529 effect this we maintain a stack of saved characters that we've smashed
530 with '\0's (indicating end of strings for various sub-fields of the
47926f60 531 assembler instruction). */
252b5132 532static char save_stack[32];
ce8a8b2f 533static char *save_stack_p;
252b5132
RH
534#define END_STRING_AND_SAVE(s) \
535 do { *save_stack_p++ = *(s); *(s) = '\0'; } while (0)
536#define RESTORE_END_STRING(s) \
537 do { *(s) = *--save_stack_p; } while (0)
538
47926f60 539/* The instruction we're assembling. */
252b5132
RH
540static i386_insn i;
541
542/* Possible templates for current insn. */
543static const templates *current_templates;
544
31b2323c
L
545/* Per instruction expressionS buffers: max displacements & immediates. */
546static expressionS disp_expressions[MAX_MEMORY_OPERANDS];
547static expressionS im_expressions[MAX_IMMEDIATE_OPERANDS];
252b5132 548
47926f60 549/* Current operand we are working on. */
ee86248c 550static int this_operand = -1;
252b5132 551
3e73aa7c
JH
552/* We support four different modes. FLAG_CODE variable is used to distinguish
553 these. */
554
555enum flag_code {
556 CODE_32BIT,
557 CODE_16BIT,
558 CODE_64BIT };
559
560static enum flag_code flag_code;
4fa24527 561static unsigned int object_64bit;
862be3fb 562static unsigned int disallow_64bit_reloc;
3e73aa7c 563static int use_rela_relocations = 0;
e379e5f3
L
564/* __tls_get_addr/___tls_get_addr symbol for TLS. */
565static const char *tls_get_addr;
3e73aa7c 566
7af8ed2d
NC
567#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
568 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
569 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
570
351f65ca
L
571/* The ELF ABI to use. */
572enum x86_elf_abi
573{
574 I386_ABI,
7f56bc95
L
575 X86_64_ABI,
576 X86_64_X32_ABI
351f65ca
L
577};
578
579static enum x86_elf_abi x86_elf_abi = I386_ABI;
7af8ed2d 580#endif
351f65ca 581
167ad85b
TG
582#if defined (TE_PE) || defined (TE_PEP)
583/* Use big object file format. */
584static int use_big_obj = 0;
585#endif
586
8dcea932
L
587#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
588/* 1 if generating code for a shared library. */
589static int shared = 0;
590#endif
591
47926f60
KH
592/* 1 for intel syntax,
593 0 if att syntax. */
594static int intel_syntax = 0;
252b5132 595
4b5aaf5f
L
596static enum x86_64_isa
597{
598 amd64 = 1, /* AMD64 ISA. */
599 intel64 /* Intel64 ISA. */
600} isa64;
e89c5eaa 601
1efbbeb4
L
602/* 1 for intel mnemonic,
603 0 if att mnemonic. */
604static int intel_mnemonic = !SYSV386_COMPAT;
605
a60de03c
JB
606/* 1 if pseudo registers are permitted. */
607static int allow_pseudo_reg = 0;
608
47926f60
KH
609/* 1 if register prefix % not required. */
610static int allow_naked_reg = 0;
252b5132 611
33eaf5de 612/* 1 if the assembler should add BND prefix for all control-transferring
7e8b059b
L
613 instructions supporting it, even if this prefix wasn't specified
614 explicitly. */
615static int add_bnd_prefix = 0;
616
ba104c83 617/* 1 if pseudo index register, eiz/riz, is allowed . */
db51cc60
L
618static int allow_index_reg = 0;
619
d022bddd
IT
620/* 1 if the assembler should ignore LOCK prefix, even if it was
621 specified explicitly. */
622static int omit_lock_prefix = 0;
623
e4e00185
AS
624/* 1 if the assembler should encode lfence, mfence, and sfence as
625 "lock addl $0, (%{re}sp)". */
626static int avoid_fence = 0;
627
ae531041
L
628/* 1 if lfence should be inserted after every load. */
629static int lfence_after_load = 0;
630
631/* Non-zero if lfence should be inserted before indirect branch. */
632static enum lfence_before_indirect_branch_kind
633 {
634 lfence_branch_none = 0,
635 lfence_branch_register,
636 lfence_branch_memory,
637 lfence_branch_all
638 }
639lfence_before_indirect_branch;
640
641/* Non-zero if lfence should be inserted before ret. */
642static enum lfence_before_ret_kind
643 {
644 lfence_before_ret_none = 0,
645 lfence_before_ret_not,
a09f656b 646 lfence_before_ret_or,
647 lfence_before_ret_shl
ae531041
L
648 }
649lfence_before_ret;
650
651/* Types of previous instruction is .byte or prefix. */
e379e5f3
L
652static struct
653 {
654 segT seg;
655 const char *file;
656 const char *name;
657 unsigned int line;
658 enum last_insn_kind
659 {
660 last_insn_other = 0,
661 last_insn_directive,
662 last_insn_prefix
663 } kind;
664 } last_insn;
665
0cb4071e
L
666/* 1 if the assembler should generate relax relocations. */
667
668static int generate_relax_relocations
669 = DEFAULT_GENERATE_X86_RELAX_RELOCATIONS;
670
7bab8ab5 671static enum check_kind
daf50ae7 672 {
7bab8ab5
JB
673 check_none = 0,
674 check_warning,
675 check_error
daf50ae7 676 }
7bab8ab5 677sse_check, operand_check = check_warning;
daf50ae7 678
e379e5f3
L
679/* Non-zero if branches should be aligned within power of 2 boundary. */
680static int align_branch_power = 0;
681
682/* Types of branches to align. */
683enum align_branch_kind
684 {
685 align_branch_none = 0,
686 align_branch_jcc = 1,
687 align_branch_fused = 2,
688 align_branch_jmp = 3,
689 align_branch_call = 4,
690 align_branch_indirect = 5,
691 align_branch_ret = 6
692 };
693
694/* Type bits of branches to align. */
695enum align_branch_bit
696 {
697 align_branch_jcc_bit = 1 << align_branch_jcc,
698 align_branch_fused_bit = 1 << align_branch_fused,
699 align_branch_jmp_bit = 1 << align_branch_jmp,
700 align_branch_call_bit = 1 << align_branch_call,
701 align_branch_indirect_bit = 1 << align_branch_indirect,
702 align_branch_ret_bit = 1 << align_branch_ret
703 };
704
705static unsigned int align_branch = (align_branch_jcc_bit
706 | align_branch_fused_bit
707 | align_branch_jmp_bit);
708
79d72f45
HL
709/* Types of condition jump used by macro-fusion. */
710enum mf_jcc_kind
711 {
712 mf_jcc_jo = 0, /* base opcode 0x70 */
713 mf_jcc_jc, /* base opcode 0x72 */
714 mf_jcc_je, /* base opcode 0x74 */
715 mf_jcc_jna, /* base opcode 0x76 */
716 mf_jcc_js, /* base opcode 0x78 */
717 mf_jcc_jp, /* base opcode 0x7a */
718 mf_jcc_jl, /* base opcode 0x7c */
719 mf_jcc_jle, /* base opcode 0x7e */
720 };
721
722/* Types of compare flag-modifying insntructions used by macro-fusion. */
723enum mf_cmp_kind
724 {
725 mf_cmp_test_and, /* test/cmp */
726 mf_cmp_alu_cmp, /* add/sub/cmp */
727 mf_cmp_incdec /* inc/dec */
728 };
729
e379e5f3
L
730/* The maximum padding size for fused jcc. CMP like instruction can
731 be 9 bytes and jcc can be 6 bytes. Leave room just in case for
732 prefixes. */
733#define MAX_FUSED_JCC_PADDING_SIZE 20
734
735/* The maximum number of prefixes added for an instruction. */
736static unsigned int align_branch_prefix_size = 5;
737
b6f8c7c4
L
738/* Optimization:
739 1. Clear the REX_W bit with register operand if possible.
740 2. Above plus use 128bit vector instruction to clear the full vector
741 register.
742 */
743static int optimize = 0;
744
745/* Optimization:
746 1. Clear the REX_W bit with register operand if possible.
747 2. Above plus use 128bit vector instruction to clear the full vector
748 register.
749 3. Above plus optimize "test{q,l,w} $imm8,%r{64,32,16}" to
750 "testb $imm7,%r8".
751 */
752static int optimize_for_space = 0;
753
2ca3ace5
L
754/* Register prefix used for error message. */
755static const char *register_prefix = "%";
756
47926f60
KH
757/* Used in 16 bit gcc mode to add an l suffix to call, ret, enter,
758 leave, push, and pop instructions so that gcc has the same stack
759 frame as in 32 bit mode. */
760static char stackop_size = '\0';
eecb386c 761
12b55ccc
L
762/* Non-zero to optimize code alignment. */
763int optimize_align_code = 1;
764
47926f60
KH
765/* Non-zero to quieten some warnings. */
766static int quiet_warnings = 0;
a38cf1db 767
d59a54c2
JB
768/* Guard to avoid repeated warnings about non-16-bit code on 16-bit CPUs. */
769static bool pre_386_16bit_warned;
770
47926f60
KH
771/* CPU name. */
772static const char *cpu_arch_name = NULL;
6305a203 773static char *cpu_sub_arch_name = NULL;
a38cf1db 774
47926f60 775/* CPU feature flags. */
40fb9820
L
776static i386_cpu_flags cpu_arch_flags = CPU_UNKNOWN_FLAGS;
777
ccc9c027
L
778/* If we have selected a cpu we are generating instructions for. */
779static int cpu_arch_tune_set = 0;
780
9103f4f4 781/* Cpu we are generating instructions for. */
fbf3f584 782enum processor_type cpu_arch_tune = PROCESSOR_UNKNOWN;
9103f4f4
L
783
784/* CPU feature flags of cpu we are generating instructions for. */
40fb9820 785static i386_cpu_flags cpu_arch_tune_flags;
9103f4f4 786
ccc9c027 787/* CPU instruction set architecture used. */
fbf3f584 788enum processor_type cpu_arch_isa = PROCESSOR_UNKNOWN;
ccc9c027 789
9103f4f4 790/* CPU feature flags of instruction set architecture used. */
fbf3f584 791i386_cpu_flags cpu_arch_isa_flags;
9103f4f4 792
fddf5b5b
AM
793/* If set, conditional jumps are not automatically promoted to handle
794 larger than a byte offset. */
f68697e8 795static bool no_cond_jump_promotion = false;
fddf5b5b 796
c0f3af97
L
797/* Encode SSE instructions with VEX prefix. */
798static unsigned int sse2avx;
799
c8480b58
L
800/* Encode aligned vector move as unaligned vector move. */
801static unsigned int use_unaligned_vector_move;
802
539f890d
L
803/* Encode scalar AVX instructions with specific vector length. */
804static enum
805 {
806 vex128 = 0,
807 vex256
808 } avxscalar;
809
03751133
L
810/* Encode VEX WIG instructions with specific vex.w. */
811static enum
812 {
813 vexw0 = 0,
814 vexw1
815 } vexwig;
816
43234a1e
L
817/* Encode scalar EVEX LIG instructions with specific vector length. */
818static enum
819 {
820 evexl128 = 0,
821 evexl256,
822 evexl512
823 } evexlig;
824
825/* Encode EVEX WIG instructions with specific evex.w. */
826static enum
827 {
828 evexw0 = 0,
829 evexw1
830 } evexwig;
831
d3d3c6db
IT
832/* Value to encode in EVEX RC bits, for SAE-only instructions. */
833static enum rc_type evexrcig = rne;
834
29b0f896 835/* Pre-defined "_GLOBAL_OFFSET_TABLE_". */
87c245cc 836static symbolS *GOT_symbol;
29b0f896 837
a4447b93
RH
838/* The dwarf2 return column, adjusted for 32 or 64 bit. */
839unsigned int x86_dwarf2_return_column;
840
841/* The dwarf2 data alignment, adjusted for 32 or 64 bit. */
842int x86_cie_data_alignment;
843
252b5132 844/* Interface to relax_segment.
fddf5b5b
AM
845 There are 3 major relax states for 386 jump insns because the
846 different types of jumps add different sizes to frags when we're
e379e5f3
L
847 figuring out what sort of jump to choose to reach a given label.
848
849 BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING are used to align
850 branches which are handled by md_estimate_size_before_relax() and
851 i386_generic_table_relax_frag(). */
252b5132 852
47926f60 853/* Types. */
93c2a809
AM
854#define UNCOND_JUMP 0
855#define COND_JUMP 1
856#define COND_JUMP86 2
e379e5f3
L
857#define BRANCH_PADDING 3
858#define BRANCH_PREFIX 4
859#define FUSED_JCC_PADDING 5
fddf5b5b 860
47926f60 861/* Sizes. */
252b5132
RH
862#define CODE16 1
863#define SMALL 0
29b0f896 864#define SMALL16 (SMALL | CODE16)
252b5132 865#define BIG 2
29b0f896 866#define BIG16 (BIG | CODE16)
252b5132
RH
867
868#ifndef INLINE
869#ifdef __GNUC__
870#define INLINE __inline__
871#else
872#define INLINE
873#endif
874#endif
875
fddf5b5b
AM
876#define ENCODE_RELAX_STATE(type, size) \
877 ((relax_substateT) (((type) << 2) | (size)))
878#define TYPE_FROM_RELAX_STATE(s) \
879 ((s) >> 2)
880#define DISP_SIZE_FROM_RELAX_STATE(s) \
881 ((((s) & 3) == BIG ? 4 : (((s) & 3) == BIG16 ? 2 : 1)))
252b5132
RH
882
883/* This table is used by relax_frag to promote short jumps to long
884 ones where necessary. SMALL (short) jumps may be promoted to BIG
885 (32 bit long) ones, and SMALL16 jumps to BIG16 (16 bit long). We
886 don't allow a short jump in a 32 bit code segment to be promoted to
887 a 16 bit offset jump because it's slower (requires data size
888 prefix), and doesn't work, unless the destination is in the bottom
889 64k of the code segment (The top 16 bits of eip are zeroed). */
890
891const relax_typeS md_relax_table[] =
892{
24eab124
AM
893 /* The fields are:
894 1) most positive reach of this state,
895 2) most negative reach of this state,
93c2a809 896 3) how many bytes this mode will have in the variable part of the frag
ce8a8b2f 897 4) which index into the table to try if we can't fit into this one. */
252b5132 898
fddf5b5b 899 /* UNCOND_JUMP states. */
93c2a809
AM
900 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG)},
901 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16)},
902 /* dword jmp adds 4 bytes to frag:
903 0 extra opcode bytes, 4 displacement bytes. */
252b5132 904 {0, 0, 4, 0},
93c2a809
AM
905 /* word jmp adds 2 byte2 to frag:
906 0 extra opcode bytes, 2 displacement bytes. */
252b5132
RH
907 {0, 0, 2, 0},
908
93c2a809
AM
909 /* COND_JUMP states. */
910 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG)},
911 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG16)},
912 /* dword conditionals adds 5 bytes to frag:
913 1 extra opcode byte, 4 displacement bytes. */
914 {0, 0, 5, 0},
fddf5b5b 915 /* word conditionals add 3 bytes to frag:
93c2a809
AM
916 1 extra opcode byte, 2 displacement bytes. */
917 {0, 0, 3, 0},
918
919 /* COND_JUMP86 states. */
920 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG)},
921 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG16)},
922 /* dword conditionals adds 5 bytes to frag:
923 1 extra opcode byte, 4 displacement bytes. */
924 {0, 0, 5, 0},
925 /* word conditionals add 4 bytes to frag:
926 1 displacement byte and a 3 byte long branch insn. */
927 {0, 0, 4, 0}
252b5132
RH
928};
929
6ceeed25 930#define ARCH(n, t, f, s) \
ae89daec
JB
931 { STRING_COMMA_LEN (#n), s, PROCESSOR_ ## t, CPU_ ## f ## _FLAGS, \
932 CPU_NONE_FLAGS }
933#define SUBARCH(n, e, d, s) \
934 { STRING_COMMA_LEN (#n), s, PROCESSOR_NONE, CPU_ ## e ## _FLAGS, \
935 CPU_ ## d ## _FLAGS }
6ceeed25 936
9103f4f4
L
937static const arch_entry cpu_arch[] =
938{
3ce2ebcf
JB
939 /* Do not replace the first two entries - i386_target_format() and
940 set_cpu_arch() rely on them being there in this order. */
6ceeed25
JB
941 ARCH (generic32, GENERIC32, GENERIC32, false),
942 ARCH (generic64, GENERIC64, GENERIC64, false),
943 ARCH (i8086, UNKNOWN, NONE, false),
944 ARCH (i186, UNKNOWN, I186, false),
945 ARCH (i286, UNKNOWN, I286, false),
946 ARCH (i386, I386, I386, false),
947 ARCH (i486, I486, I486, false),
948 ARCH (i586, PENTIUM, I586, false),
949 ARCH (i686, PENTIUMPRO, I686, false),
950 ARCH (pentium, PENTIUM, I586, false),
951 ARCH (pentiumpro, PENTIUMPRO, PENTIUMPRO, false),
952 ARCH (pentiumii, PENTIUMPRO, P2, false),
953 ARCH (pentiumiii, PENTIUMPRO, P3, false),
954 ARCH (pentium4, PENTIUM4, P4, false),
955 ARCH (prescott, NOCONA, CORE, false),
956 ARCH (nocona, NOCONA, NOCONA, false),
957 ARCH (yonah, CORE, CORE, true),
958 ARCH (core, CORE, CORE, false),
959 ARCH (merom, CORE2, CORE2, true),
960 ARCH (core2, CORE2, CORE2, false),
961 ARCH (corei7, COREI7, COREI7, false),
962 ARCH (iamcu, IAMCU, IAMCU, false),
963 ARCH (k6, K6, K6, false),
964 ARCH (k6_2, K6, K6_2, false),
965 ARCH (athlon, ATHLON, ATHLON, false),
966 ARCH (sledgehammer, K8, K8, true),
967 ARCH (opteron, K8, K8, false),
968 ARCH (k8, K8, K8, false),
969 ARCH (amdfam10, AMDFAM10, AMDFAM10, false),
970 ARCH (bdver1, BD, BDVER1, false),
971 ARCH (bdver2, BD, BDVER2, false),
972 ARCH (bdver3, BD, BDVER3, false),
973 ARCH (bdver4, BD, BDVER4, false),
974 ARCH (znver1, ZNVER, ZNVER1, false),
975 ARCH (znver2, ZNVER, ZNVER2, false),
976 ARCH (znver3, ZNVER, ZNVER3, false),
977 ARCH (btver1, BT, BTVER1, false),
978 ARCH (btver2, BT, BTVER2, false),
979
ae89daec
JB
980 SUBARCH (8087, 8087, ANY_X87, false),
981 SUBARCH (87, NONE, ANY_X87, false), /* Disable only! */
982 SUBARCH (287, 287, ANY_287, false),
983 SUBARCH (387, 387, ANY_387, false),
984 SUBARCH (687, 687, ANY_687, false),
985 SUBARCH (cmov, CMOV, ANY_CMOV, false),
986 SUBARCH (fxsr, FXSR, ANY_FXSR, false),
987 SUBARCH (mmx, MMX, ANY_MMX, false),
988 SUBARCH (sse, SSE, ANY_SSE, false),
989 SUBARCH (sse2, SSE2, ANY_SSE2, false),
990 SUBARCH (sse3, SSE3, ANY_SSE3, false),
991 SUBARCH (sse4a, SSE4A, ANY_SSE4A, false),
992 SUBARCH (ssse3, SSSE3, ANY_SSSE3, false),
993 SUBARCH (sse4.1, SSE4_1, ANY_SSE4_1, false),
994 SUBARCH (sse4.2, SSE4_2, ANY_SSE4_2, false),
995 SUBARCH (sse4, SSE4_2, ANY_SSE4_1, false),
996 SUBARCH (avx, AVX, ANY_AVX, false),
997 SUBARCH (avx2, AVX2, ANY_AVX2, false),
998 SUBARCH (avx512f, AVX512F, ANY_AVX512F, false),
999 SUBARCH (avx512cd, AVX512CD, ANY_AVX512CD, false),
1000 SUBARCH (avx512er, AVX512ER, ANY_AVX512ER, false),
1001 SUBARCH (avx512pf, AVX512PF, ANY_AVX512PF, false),
1002 SUBARCH (avx512dq, AVX512DQ, ANY_AVX512DQ, false),
1003 SUBARCH (avx512bw, AVX512BW, ANY_AVX512BW, false),
1004 SUBARCH (avx512vl, AVX512VL, ANY_AVX512VL, false),
1005 SUBARCH (vmx, VMX, VMX, false),
1006 SUBARCH (vmfunc, VMFUNC, VMFUNC, false),
1007 SUBARCH (smx, SMX, SMX, false),
1008 SUBARCH (xsave, XSAVE, XSAVE, false),
1009 SUBARCH (xsaveopt, XSAVEOPT, XSAVEOPT, false),
1010 SUBARCH (xsavec, XSAVEC, XSAVEC, false),
1011 SUBARCH (xsaves, XSAVES, XSAVES, false),
1012 SUBARCH (aes, AES, AES, false),
1013 SUBARCH (pclmul, PCLMUL, PCLMUL, false),
1014 SUBARCH (clmul, PCLMUL, PCLMUL, true),
1015 SUBARCH (fsgsbase, FSGSBASE, FSGSBASE, false),
1016 SUBARCH (rdrnd, RDRND, RDRND, false),
1017 SUBARCH (f16c, F16C, F16C, false),
1018 SUBARCH (bmi2, BMI2, BMI2, false),
1019 SUBARCH (fma, FMA, FMA, false),
1020 SUBARCH (fma4, FMA4, FMA4, false),
1021 SUBARCH (xop, XOP, XOP, false),
1022 SUBARCH (lwp, LWP, LWP, false),
1023 SUBARCH (movbe, MOVBE, MOVBE, false),
1024 SUBARCH (cx16, CX16, CX16, false),
1025 SUBARCH (ept, EPT, EPT, false),
1026 SUBARCH (lzcnt, LZCNT, LZCNT, false),
1027 SUBARCH (popcnt, POPCNT, POPCNT, false),
1028 SUBARCH (hle, HLE, HLE, false),
1029 SUBARCH (rtm, RTM, RTM, false),
1030 SUBARCH (invpcid, INVPCID, INVPCID, false),
1031 SUBARCH (clflush, CLFLUSH, CLFLUSH, false),
1032 SUBARCH (nop, NOP, NOP, false),
1033 SUBARCH (syscall, SYSCALL, SYSCALL, false),
1034 SUBARCH (rdtscp, RDTSCP, RDTSCP, false),
1035 SUBARCH (3dnow, 3DNOW, 3DNOW, false),
1036 SUBARCH (3dnowa, 3DNOWA, 3DNOWA, false),
1037 SUBARCH (padlock, PADLOCK, PADLOCK, false),
1038 SUBARCH (pacifica, SVME, SVME, true),
1039 SUBARCH (svme, SVME, SVME, false),
1040 SUBARCH (sse4a, SSE4A, SSE4A, false),
1041 SUBARCH (abm, ABM, ABM, false),
1042 SUBARCH (bmi, BMI, BMI, false),
1043 SUBARCH (tbm, TBM, TBM, false),
1044 SUBARCH (adx, ADX, ADX, false),
1045 SUBARCH (rdseed, RDSEED, RDSEED, false),
1046 SUBARCH (prfchw, PRFCHW, PRFCHW, false),
1047 SUBARCH (smap, SMAP, SMAP, false),
1048 SUBARCH (mpx, MPX, MPX, false),
1049 SUBARCH (sha, SHA, SHA, false),
1050 SUBARCH (clflushopt, CLFLUSHOPT, CLFLUSHOPT, false),
1051 SUBARCH (prefetchwt1, PREFETCHWT1, PREFETCHWT1, false),
1052 SUBARCH (se1, SE1, SE1, false),
1053 SUBARCH (clwb, CLWB, CLWB, false),
1054 SUBARCH (avx512ifma, AVX512IFMA, ANY_AVX512IFMA, false),
1055 SUBARCH (avx512vbmi, AVX512VBMI, ANY_AVX512VBMI, false),
1056 SUBARCH (avx512_4fmaps, AVX512_4FMAPS, ANY_AVX512_4FMAPS, false),
1057 SUBARCH (avx512_4vnniw, AVX512_4VNNIW, ANY_AVX512_4VNNIW, false),
1058 SUBARCH (avx512_vpopcntdq, AVX512_VPOPCNTDQ, ANY_AVX512_VPOPCNTDQ, false),
1059 SUBARCH (avx512_vbmi2, AVX512_VBMI2, ANY_AVX512_VBMI2, false),
1060 SUBARCH (avx512_vnni, AVX512_VNNI, ANY_AVX512_VNNI, false),
1061 SUBARCH (avx512_bitalg, AVX512_BITALG, ANY_AVX512_BITALG, false),
1062 SUBARCH (avx_vnni, AVX_VNNI, ANY_AVX_VNNI, false),
1063 SUBARCH (clzero, CLZERO, CLZERO, false),
1064 SUBARCH (mwaitx, MWAITX, MWAITX, false),
1065 SUBARCH (ospke, OSPKE, OSPKE, false),
1066 SUBARCH (rdpid, RDPID, RDPID, false),
1067 SUBARCH (ptwrite, PTWRITE, PTWRITE, false),
1068 SUBARCH (ibt, IBT, ANY_IBT, false),
1069 SUBARCH (shstk, SHSTK, ANY_SHSTK, false),
1070 SUBARCH (gfni, GFNI, GFNI, false),
1071 SUBARCH (vaes, VAES, VAES, false),
1072 SUBARCH (vpclmulqdq, VPCLMULQDQ, VPCLMULQDQ, false),
1073 SUBARCH (wbnoinvd, WBNOINVD, WBNOINVD, false),
1074 SUBARCH (pconfig, PCONFIG, PCONFIG, false),
1075 SUBARCH (waitpkg, WAITPKG, WAITPKG, false),
1076 SUBARCH (cldemote, CLDEMOTE, CLDEMOTE, false),
1077 SUBARCH (amx_int8, AMX_INT8, ANY_AMX_INT8, false),
1078 SUBARCH (amx_bf16, AMX_BF16, ANY_AMX_BF16, false),
1079 SUBARCH (amx_tile, AMX_TILE, ANY_AMX_TILE, false),
1080 SUBARCH (movdiri, MOVDIRI, ANY_MOVDIRI, false),
1081 SUBARCH (movdir64b, MOVDIR64B, ANY_MOVDIR64B, false),
1082 SUBARCH (avx512_bf16, AVX512_BF16, ANY_AVX512_BF16, false),
1083 SUBARCH (avx512_vp2intersect, AVX512_VP2INTERSECT,
1084 ANY_AVX512_VP2INTERSECT, false),
1085 SUBARCH (tdx, TDX, ANY_TDX, false),
1086 SUBARCH (enqcmd, ENQCMD, ANY_ENQCMD, false),
1087 SUBARCH (serialize, SERIALIZE, ANY_SERIALIZE, false),
1088 SUBARCH (rdpru, RDPRU, RDPRU, false),
1089 SUBARCH (mcommit, MCOMMIT, MCOMMIT, false),
1090 SUBARCH (sev_es, SEV_ES, SEV_ES, false),
1091 SUBARCH (tsxldtrk, TSXLDTRK, ANY_TSXLDTRK, false),
1092 SUBARCH (kl, KL, ANY_KL, false),
1093 SUBARCH (widekl, WIDEKL, ANY_WIDEKL, false),
1094 SUBARCH (uintr, UINTR, ANY_UINTR, false),
1095 SUBARCH (hreset, HRESET, ANY_HRESET, false),
1096 SUBARCH (avx512_fp16, AVX512_FP16, ANY_AVX512_FP16, false),
293f5f65
L
1097};
1098
6ceeed25
JB
1099#undef SUBARCH
1100#undef ARCH
1101
704209c0 1102#ifdef I386COFF
a6c24e68
NC
1103/* Like s_lcomm_internal in gas/read.c but the alignment string
1104 is allowed to be optional. */
1105
1106static symbolS *
1107pe_lcomm_internal (int needs_align, symbolS *symbolP, addressT size)
1108{
1109 addressT align = 0;
1110
1111 SKIP_WHITESPACE ();
1112
7ab9ffdd 1113 if (needs_align
a6c24e68
NC
1114 && *input_line_pointer == ',')
1115 {
1116 align = parse_align (needs_align - 1);
7ab9ffdd 1117
a6c24e68
NC
1118 if (align == (addressT) -1)
1119 return NULL;
1120 }
1121 else
1122 {
1123 if (size >= 8)
1124 align = 3;
1125 else if (size >= 4)
1126 align = 2;
1127 else if (size >= 2)
1128 align = 1;
1129 else
1130 align = 0;
1131 }
1132
1133 bss_alloc (symbolP, size, align);
1134 return symbolP;
1135}
1136
704209c0 1137static void
a6c24e68
NC
1138pe_lcomm (int needs_align)
1139{
1140 s_comm_internal (needs_align * 2, pe_lcomm_internal);
1141}
704209c0 1142#endif
a6c24e68 1143
29b0f896
AM
1144const pseudo_typeS md_pseudo_table[] =
1145{
1146#if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
1147 {"align", s_align_bytes, 0},
1148#else
1149 {"align", s_align_ptwo, 0},
1150#endif
1151 {"arch", set_cpu_arch, 0},
1152#ifndef I386COFF
1153 {"bss", s_bss, 0},
a6c24e68
NC
1154#else
1155 {"lcomm", pe_lcomm, 1},
29b0f896
AM
1156#endif
1157 {"ffloat", float_cons, 'f'},
1158 {"dfloat", float_cons, 'd'},
1159 {"tfloat", float_cons, 'x'},
7d19d096 1160 {"hfloat", float_cons, 'h'},
de133cf9 1161 {"bfloat16", float_cons, 'b'},
29b0f896 1162 {"value", cons, 2},
d182319b 1163 {"slong", signed_cons, 4},
29b0f896
AM
1164 {"noopt", s_ignore, 0},
1165 {"optim", s_ignore, 0},
1166 {"code16gcc", set_16bit_gcc_code_flag, CODE_16BIT},
1167 {"code16", set_code_flag, CODE_16BIT},
1168 {"code32", set_code_flag, CODE_32BIT},
da5f19a2 1169#ifdef BFD64
29b0f896 1170 {"code64", set_code_flag, CODE_64BIT},
da5f19a2 1171#endif
29b0f896
AM
1172 {"intel_syntax", set_intel_syntax, 1},
1173 {"att_syntax", set_intel_syntax, 0},
1efbbeb4
L
1174 {"intel_mnemonic", set_intel_mnemonic, 1},
1175 {"att_mnemonic", set_intel_mnemonic, 0},
db51cc60
L
1176 {"allow_index_reg", set_allow_index_reg, 1},
1177 {"disallow_index_reg", set_allow_index_reg, 0},
7bab8ab5
JB
1178 {"sse_check", set_check, 0},
1179 {"operand_check", set_check, 1},
3b22753a
L
1180#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1181 {"largecomm", handle_large_common, 0},
07a53e5c 1182#else
68d20676 1183 {"file", dwarf2_directive_file, 0},
07a53e5c
RH
1184 {"loc", dwarf2_directive_loc, 0},
1185 {"loc_mark_labels", dwarf2_directive_loc_mark_labels, 0},
3b22753a 1186#endif
6482c264
NC
1187#ifdef TE_PE
1188 {"secrel32", pe_directive_secrel, 0},
145667f8 1189 {"secidx", pe_directive_secidx, 0},
6482c264 1190#endif
29b0f896
AM
1191 {0, 0, 0}
1192};
1193
1194/* For interface with expression (). */
1195extern char *input_line_pointer;
1196
1197/* Hash table for instruction mnemonic lookup. */
629310ab 1198static htab_t op_hash;
29b0f896
AM
1199
1200/* Hash table for register lookup. */
629310ab 1201static htab_t reg_hash;
29b0f896 1202\f
ce8a8b2f
AM
1203 /* Various efficient no-op patterns for aligning code labels.
1204 Note: Don't try to assemble the instructions in the comments.
1205 0L and 0w are not legal. */
62a02d25
L
1206static const unsigned char f32_1[] =
1207 {0x90}; /* nop */
1208static const unsigned char f32_2[] =
1209 {0x66,0x90}; /* xchg %ax,%ax */
1210static const unsigned char f32_3[] =
1211 {0x8d,0x76,0x00}; /* leal 0(%esi),%esi */
1212static const unsigned char f32_4[] =
1213 {0x8d,0x74,0x26,0x00}; /* leal 0(%esi,1),%esi */
62a02d25
L
1214static const unsigned char f32_6[] =
1215 {0x8d,0xb6,0x00,0x00,0x00,0x00}; /* leal 0L(%esi),%esi */
1216static const unsigned char f32_7[] =
1217 {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal 0L(%esi,1),%esi */
62a02d25 1218static const unsigned char f16_3[] =
3ae729d5 1219 {0x8d,0x74,0x00}; /* lea 0(%si),%si */
62a02d25 1220static const unsigned char f16_4[] =
3ae729d5
L
1221 {0x8d,0xb4,0x00,0x00}; /* lea 0W(%si),%si */
1222static const unsigned char jump_disp8[] =
1223 {0xeb}; /* jmp disp8 */
1224static const unsigned char jump32_disp32[] =
1225 {0xe9}; /* jmp disp32 */
1226static const unsigned char jump16_disp32[] =
1227 {0x66,0xe9}; /* jmp disp32 */
62a02d25
L
1228/* 32-bit NOPs patterns. */
1229static const unsigned char *const f32_patt[] = {
3ae729d5 1230 f32_1, f32_2, f32_3, f32_4, NULL, f32_6, f32_7
62a02d25
L
1231};
1232/* 16-bit NOPs patterns. */
1233static const unsigned char *const f16_patt[] = {
3ae729d5 1234 f32_1, f32_2, f16_3, f16_4
62a02d25
L
1235};
1236/* nopl (%[re]ax) */
1237static const unsigned char alt_3[] =
1238 {0x0f,0x1f,0x00};
1239/* nopl 0(%[re]ax) */
1240static const unsigned char alt_4[] =
1241 {0x0f,0x1f,0x40,0x00};
1242/* nopl 0(%[re]ax,%[re]ax,1) */
1243static const unsigned char alt_5[] =
1244 {0x0f,0x1f,0x44,0x00,0x00};
1245/* nopw 0(%[re]ax,%[re]ax,1) */
1246static const unsigned char alt_6[] =
1247 {0x66,0x0f,0x1f,0x44,0x00,0x00};
1248/* nopl 0L(%[re]ax) */
1249static const unsigned char alt_7[] =
1250 {0x0f,0x1f,0x80,0x00,0x00,0x00,0x00};
1251/* nopl 0L(%[re]ax,%[re]ax,1) */
1252static const unsigned char alt_8[] =
1253 {0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1254/* nopw 0L(%[re]ax,%[re]ax,1) */
1255static const unsigned char alt_9[] =
1256 {0x66,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1257/* nopw %cs:0L(%[re]ax,%[re]ax,1) */
1258static const unsigned char alt_10[] =
1259 {0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
3ae729d5
L
1260/* data16 nopw %cs:0L(%eax,%eax,1) */
1261static const unsigned char alt_11[] =
1262 {0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
62a02d25
L
1263/* 32-bit and 64-bit NOPs patterns. */
1264static const unsigned char *const alt_patt[] = {
1265 f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
3ae729d5 1266 alt_9, alt_10, alt_11
62a02d25
L
1267};
1268
1269/* Genenerate COUNT bytes of NOPs to WHERE from PATT with the maximum
1270 size of a single NOP instruction MAX_SINGLE_NOP_SIZE. */
1271
1272static void
1273i386_output_nops (char *where, const unsigned char *const *patt,
1274 int count, int max_single_nop_size)
1275
1276{
3ae729d5
L
1277 /* Place the longer NOP first. */
1278 int last;
1279 int offset;
3076e594
NC
1280 const unsigned char *nops;
1281
1282 if (max_single_nop_size < 1)
1283 {
1284 as_fatal (_("i386_output_nops called to generate nops of at most %d bytes!"),
1285 max_single_nop_size);
1286 return;
1287 }
1288
1289 nops = patt[max_single_nop_size - 1];
3ae729d5
L
1290
1291 /* Use the smaller one if the requsted one isn't available. */
1292 if (nops == NULL)
62a02d25 1293 {
3ae729d5
L
1294 max_single_nop_size--;
1295 nops = patt[max_single_nop_size - 1];
62a02d25
L
1296 }
1297
3ae729d5
L
1298 last = count % max_single_nop_size;
1299
1300 count -= last;
1301 for (offset = 0; offset < count; offset += max_single_nop_size)
1302 memcpy (where + offset, nops, max_single_nop_size);
1303
1304 if (last)
1305 {
1306 nops = patt[last - 1];
1307 if (nops == NULL)
1308 {
1309 /* Use the smaller one plus one-byte NOP if the needed one
1310 isn't available. */
1311 last--;
1312 nops = patt[last - 1];
1313 memcpy (where + offset, nops, last);
1314 where[offset + last] = *patt[0];
1315 }
1316 else
1317 memcpy (where + offset, nops, last);
1318 }
62a02d25
L
1319}
1320
3ae729d5
L
1321static INLINE int
1322fits_in_imm7 (offsetT num)
1323{
1324 return (num & 0x7f) == num;
1325}
1326
1327static INLINE int
1328fits_in_imm31 (offsetT num)
1329{
1330 return (num & 0x7fffffff) == num;
1331}
62a02d25
L
1332
1333/* Genenerate COUNT bytes of NOPs to WHERE with the maximum size of a
1334 single NOP instruction LIMIT. */
1335
1336void
3ae729d5 1337i386_generate_nops (fragS *fragP, char *where, offsetT count, int limit)
62a02d25 1338{
3ae729d5 1339 const unsigned char *const *patt = NULL;
62a02d25 1340 int max_single_nop_size;
3ae729d5
L
1341 /* Maximum number of NOPs before switching to jump over NOPs. */
1342 int max_number_of_nops;
62a02d25 1343
3ae729d5 1344 switch (fragP->fr_type)
62a02d25 1345 {
3ae729d5
L
1346 case rs_fill_nop:
1347 case rs_align_code:
1348 break;
e379e5f3
L
1349 case rs_machine_dependent:
1350 /* Allow NOP padding for jumps and calls. */
1351 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
1352 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
1353 break;
1354 /* Fall through. */
3ae729d5 1355 default:
62a02d25
L
1356 return;
1357 }
1358
ccc9c027
L
1359 /* We need to decide which NOP sequence to use for 32bit and
1360 64bit. When -mtune= is used:
4eed87de 1361
76bc74dc
L
1362 1. For PROCESSOR_I386, PROCESSOR_I486, PROCESSOR_PENTIUM and
1363 PROCESSOR_GENERIC32, f32_patt will be used.
80b8656c
L
1364 2. For the rest, alt_patt will be used.
1365
1366 When -mtune= isn't used, alt_patt will be used if
22109423 1367 cpu_arch_isa_flags has CpuNop. Otherwise, f32_patt will
76bc74dc 1368 be used.
ccc9c027
L
1369
1370 When -march= or .arch is used, we can't use anything beyond
1371 cpu_arch_isa_flags. */
1372
1373 if (flag_code == CODE_16BIT)
1374 {
3ae729d5
L
1375 patt = f16_patt;
1376 max_single_nop_size = sizeof (f16_patt) / sizeof (f16_patt[0]);
1377 /* Limit number of NOPs to 2 in 16-bit mode. */
1378 max_number_of_nops = 2;
252b5132 1379 }
33fef721 1380 else
ccc9c027 1381 {
fbf3f584 1382 if (fragP->tc_frag_data.isa == PROCESSOR_UNKNOWN)
ccc9c027
L
1383 {
1384 /* PROCESSOR_UNKNOWN means that all ISAs may be used. */
1385 switch (cpu_arch_tune)
1386 {
1387 case PROCESSOR_UNKNOWN:
1388 /* We use cpu_arch_isa_flags to check if we SHOULD
22109423
L
1389 optimize with nops. */
1390 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1391 patt = alt_patt;
ccc9c027
L
1392 else
1393 patt = f32_patt;
1394 break;
ccc9c027
L
1395 case PROCESSOR_PENTIUM4:
1396 case PROCESSOR_NOCONA:
ef05d495 1397 case PROCESSOR_CORE:
76bc74dc 1398 case PROCESSOR_CORE2:
bd5295b2 1399 case PROCESSOR_COREI7:
76bc74dc 1400 case PROCESSOR_GENERIC64:
ccc9c027
L
1401 case PROCESSOR_K6:
1402 case PROCESSOR_ATHLON:
1403 case PROCESSOR_K8:
4eed87de 1404 case PROCESSOR_AMDFAM10:
8aedb9fe 1405 case PROCESSOR_BD:
029f3522 1406 case PROCESSOR_ZNVER:
7b458c12 1407 case PROCESSOR_BT:
80b8656c 1408 patt = alt_patt;
ccc9c027 1409 break;
76bc74dc 1410 case PROCESSOR_I386:
ccc9c027
L
1411 case PROCESSOR_I486:
1412 case PROCESSOR_PENTIUM:
2dde1948 1413 case PROCESSOR_PENTIUMPRO:
81486035 1414 case PROCESSOR_IAMCU:
ccc9c027
L
1415 case PROCESSOR_GENERIC32:
1416 patt = f32_patt;
1417 break;
c368d2a8
JB
1418 case PROCESSOR_NONE:
1419 abort ();
4eed87de 1420 }
ccc9c027
L
1421 }
1422 else
1423 {
fbf3f584 1424 switch (fragP->tc_frag_data.tune)
ccc9c027
L
1425 {
1426 case PROCESSOR_UNKNOWN:
e6a14101 1427 /* When cpu_arch_isa is set, cpu_arch_tune shouldn't be
ccc9c027
L
1428 PROCESSOR_UNKNOWN. */
1429 abort ();
1430 break;
1431
76bc74dc 1432 case PROCESSOR_I386:
ccc9c027
L
1433 case PROCESSOR_I486:
1434 case PROCESSOR_PENTIUM:
81486035 1435 case PROCESSOR_IAMCU:
ccc9c027
L
1436 case PROCESSOR_K6:
1437 case PROCESSOR_ATHLON:
1438 case PROCESSOR_K8:
4eed87de 1439 case PROCESSOR_AMDFAM10:
8aedb9fe 1440 case PROCESSOR_BD:
029f3522 1441 case PROCESSOR_ZNVER:
7b458c12 1442 case PROCESSOR_BT:
ccc9c027
L
1443 case PROCESSOR_GENERIC32:
1444 /* We use cpu_arch_isa_flags to check if we CAN optimize
22109423
L
1445 with nops. */
1446 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1447 patt = alt_patt;
ccc9c027
L
1448 else
1449 patt = f32_patt;
1450 break;
76bc74dc
L
1451 case PROCESSOR_PENTIUMPRO:
1452 case PROCESSOR_PENTIUM4:
1453 case PROCESSOR_NOCONA:
1454 case PROCESSOR_CORE:
ef05d495 1455 case PROCESSOR_CORE2:
bd5295b2 1456 case PROCESSOR_COREI7:
22109423 1457 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1458 patt = alt_patt;
ccc9c027
L
1459 else
1460 patt = f32_patt;
1461 break;
1462 case PROCESSOR_GENERIC64:
80b8656c 1463 patt = alt_patt;
ccc9c027 1464 break;
c368d2a8
JB
1465 case PROCESSOR_NONE:
1466 abort ();
4eed87de 1467 }
ccc9c027
L
1468 }
1469
76bc74dc
L
1470 if (patt == f32_patt)
1471 {
3ae729d5
L
1472 max_single_nop_size = sizeof (f32_patt) / sizeof (f32_patt[0]);
1473 /* Limit number of NOPs to 2 for older processors. */
1474 max_number_of_nops = 2;
76bc74dc
L
1475 }
1476 else
1477 {
3ae729d5
L
1478 max_single_nop_size = sizeof (alt_patt) / sizeof (alt_patt[0]);
1479 /* Limit number of NOPs to 7 for newer processors. */
1480 max_number_of_nops = 7;
1481 }
1482 }
1483
1484 if (limit == 0)
1485 limit = max_single_nop_size;
1486
1487 if (fragP->fr_type == rs_fill_nop)
1488 {
1489 /* Output NOPs for .nop directive. */
1490 if (limit > max_single_nop_size)
1491 {
1492 as_bad_where (fragP->fr_file, fragP->fr_line,
1493 _("invalid single nop size: %d "
1494 "(expect within [0, %d])"),
1495 limit, max_single_nop_size);
1496 return;
1497 }
1498 }
e379e5f3 1499 else if (fragP->fr_type != rs_machine_dependent)
3ae729d5
L
1500 fragP->fr_var = count;
1501
1502 if ((count / max_single_nop_size) > max_number_of_nops)
1503 {
1504 /* Generate jump over NOPs. */
1505 offsetT disp = count - 2;
1506 if (fits_in_imm7 (disp))
1507 {
1508 /* Use "jmp disp8" if possible. */
1509 count = disp;
1510 where[0] = jump_disp8[0];
1511 where[1] = count;
1512 where += 2;
1513 }
1514 else
1515 {
1516 unsigned int size_of_jump;
1517
1518 if (flag_code == CODE_16BIT)
1519 {
1520 where[0] = jump16_disp32[0];
1521 where[1] = jump16_disp32[1];
1522 size_of_jump = 2;
1523 }
1524 else
1525 {
1526 where[0] = jump32_disp32[0];
1527 size_of_jump = 1;
1528 }
1529
1530 count -= size_of_jump + 4;
1531 if (!fits_in_imm31 (count))
1532 {
1533 as_bad_where (fragP->fr_file, fragP->fr_line,
1534 _("jump over nop padding out of range"));
1535 return;
1536 }
1537
1538 md_number_to_chars (where + size_of_jump, count, 4);
1539 where += size_of_jump + 4;
76bc74dc 1540 }
ccc9c027 1541 }
3ae729d5
L
1542
1543 /* Generate multiple NOPs. */
1544 i386_output_nops (where, patt, count, limit);
252b5132
RH
1545}
1546
c6fb90c8 1547static INLINE int
0dfbf9d7 1548operand_type_all_zero (const union i386_operand_type *x)
40fb9820 1549{
0dfbf9d7 1550 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1551 {
1552 case 3:
0dfbf9d7 1553 if (x->array[2])
c6fb90c8 1554 return 0;
1a0670f3 1555 /* Fall through. */
c6fb90c8 1556 case 2:
0dfbf9d7 1557 if (x->array[1])
c6fb90c8 1558 return 0;
1a0670f3 1559 /* Fall through. */
c6fb90c8 1560 case 1:
0dfbf9d7 1561 return !x->array[0];
c6fb90c8
L
1562 default:
1563 abort ();
1564 }
40fb9820
L
1565}
1566
c6fb90c8 1567static INLINE void
0dfbf9d7 1568operand_type_set (union i386_operand_type *x, unsigned int v)
40fb9820 1569{
0dfbf9d7 1570 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1571 {
1572 case 3:
0dfbf9d7 1573 x->array[2] = v;
1a0670f3 1574 /* Fall through. */
c6fb90c8 1575 case 2:
0dfbf9d7 1576 x->array[1] = v;
1a0670f3 1577 /* Fall through. */
c6fb90c8 1578 case 1:
0dfbf9d7 1579 x->array[0] = v;
1a0670f3 1580 /* Fall through. */
c6fb90c8
L
1581 break;
1582 default:
1583 abort ();
1584 }
bab6aec1
JB
1585
1586 x->bitfield.class = ClassNone;
75e5731b 1587 x->bitfield.instance = InstanceNone;
c6fb90c8 1588}
40fb9820 1589
c6fb90c8 1590static INLINE int
0dfbf9d7
L
1591operand_type_equal (const union i386_operand_type *x,
1592 const union i386_operand_type *y)
c6fb90c8 1593{
0dfbf9d7 1594 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1595 {
1596 case 3:
0dfbf9d7 1597 if (x->array[2] != y->array[2])
c6fb90c8 1598 return 0;
1a0670f3 1599 /* Fall through. */
c6fb90c8 1600 case 2:
0dfbf9d7 1601 if (x->array[1] != y->array[1])
c6fb90c8 1602 return 0;
1a0670f3 1603 /* Fall through. */
c6fb90c8 1604 case 1:
0dfbf9d7 1605 return x->array[0] == y->array[0];
c6fb90c8
L
1606 break;
1607 default:
1608 abort ();
1609 }
1610}
40fb9820 1611
0dfbf9d7
L
1612static INLINE int
1613cpu_flags_all_zero (const union i386_cpu_flags *x)
1614{
1615 switch (ARRAY_SIZE(x->array))
1616 {
53467f57
IT
1617 case 4:
1618 if (x->array[3])
1619 return 0;
1620 /* Fall through. */
0dfbf9d7
L
1621 case 3:
1622 if (x->array[2])
1623 return 0;
1a0670f3 1624 /* Fall through. */
0dfbf9d7
L
1625 case 2:
1626 if (x->array[1])
1627 return 0;
1a0670f3 1628 /* Fall through. */
0dfbf9d7
L
1629 case 1:
1630 return !x->array[0];
1631 default:
1632 abort ();
1633 }
1634}
1635
0dfbf9d7
L
1636static INLINE int
1637cpu_flags_equal (const union i386_cpu_flags *x,
1638 const union i386_cpu_flags *y)
1639{
1640 switch (ARRAY_SIZE(x->array))
1641 {
53467f57
IT
1642 case 4:
1643 if (x->array[3] != y->array[3])
1644 return 0;
1645 /* Fall through. */
0dfbf9d7
L
1646 case 3:
1647 if (x->array[2] != y->array[2])
1648 return 0;
1a0670f3 1649 /* Fall through. */
0dfbf9d7
L
1650 case 2:
1651 if (x->array[1] != y->array[1])
1652 return 0;
1a0670f3 1653 /* Fall through. */
0dfbf9d7
L
1654 case 1:
1655 return x->array[0] == y->array[0];
1656 break;
1657 default:
1658 abort ();
1659 }
1660}
c6fb90c8
L
1661
1662static INLINE int
1663cpu_flags_check_cpu64 (i386_cpu_flags f)
1664{
1665 return !((flag_code == CODE_64BIT && f.bitfield.cpuno64)
1666 || (flag_code != CODE_64BIT && f.bitfield.cpu64));
40fb9820
L
1667}
1668
c6fb90c8
L
1669static INLINE i386_cpu_flags
1670cpu_flags_and (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1671{
c6fb90c8
L
1672 switch (ARRAY_SIZE (x.array))
1673 {
53467f57
IT
1674 case 4:
1675 x.array [3] &= y.array [3];
1676 /* Fall through. */
c6fb90c8
L
1677 case 3:
1678 x.array [2] &= y.array [2];
1a0670f3 1679 /* Fall through. */
c6fb90c8
L
1680 case 2:
1681 x.array [1] &= y.array [1];
1a0670f3 1682 /* Fall through. */
c6fb90c8
L
1683 case 1:
1684 x.array [0] &= y.array [0];
1685 break;
1686 default:
1687 abort ();
1688 }
1689 return x;
1690}
40fb9820 1691
c6fb90c8
L
1692static INLINE i386_cpu_flags
1693cpu_flags_or (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1694{
c6fb90c8 1695 switch (ARRAY_SIZE (x.array))
40fb9820 1696 {
53467f57
IT
1697 case 4:
1698 x.array [3] |= y.array [3];
1699 /* Fall through. */
c6fb90c8
L
1700 case 3:
1701 x.array [2] |= y.array [2];
1a0670f3 1702 /* Fall through. */
c6fb90c8
L
1703 case 2:
1704 x.array [1] |= y.array [1];
1a0670f3 1705 /* Fall through. */
c6fb90c8
L
1706 case 1:
1707 x.array [0] |= y.array [0];
40fb9820
L
1708 break;
1709 default:
1710 abort ();
1711 }
40fb9820
L
1712 return x;
1713}
1714
309d3373
JB
1715static INLINE i386_cpu_flags
1716cpu_flags_and_not (i386_cpu_flags x, i386_cpu_flags y)
1717{
1718 switch (ARRAY_SIZE (x.array))
1719 {
53467f57
IT
1720 case 4:
1721 x.array [3] &= ~y.array [3];
1722 /* Fall through. */
309d3373
JB
1723 case 3:
1724 x.array [2] &= ~y.array [2];
1a0670f3 1725 /* Fall through. */
309d3373
JB
1726 case 2:
1727 x.array [1] &= ~y.array [1];
1a0670f3 1728 /* Fall through. */
309d3373
JB
1729 case 1:
1730 x.array [0] &= ~y.array [0];
1731 break;
1732 default:
1733 abort ();
1734 }
1735 return x;
1736}
1737
6c0946d0
JB
1738static const i386_cpu_flags avx512 = CPU_ANY_AVX512F_FLAGS;
1739
c0f3af97
L
1740#define CPU_FLAGS_ARCH_MATCH 0x1
1741#define CPU_FLAGS_64BIT_MATCH 0x2
1742
c0f3af97 1743#define CPU_FLAGS_PERFECT_MATCH \
db12e14e 1744 (CPU_FLAGS_ARCH_MATCH | CPU_FLAGS_64BIT_MATCH)
c0f3af97
L
1745
1746/* Return CPU flags match bits. */
3629bb00 1747
40fb9820 1748static int
d3ce72d0 1749cpu_flags_match (const insn_template *t)
40fb9820 1750{
c0f3af97
L
1751 i386_cpu_flags x = t->cpu_flags;
1752 int match = cpu_flags_check_cpu64 (x) ? CPU_FLAGS_64BIT_MATCH : 0;
40fb9820
L
1753
1754 x.bitfield.cpu64 = 0;
1755 x.bitfield.cpuno64 = 0;
1756
0dfbf9d7 1757 if (cpu_flags_all_zero (&x))
c0f3af97
L
1758 {
1759 /* This instruction is available on all archs. */
db12e14e 1760 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 1761 }
3629bb00
L
1762 else
1763 {
c0f3af97 1764 /* This instruction is available only on some archs. */
3629bb00
L
1765 i386_cpu_flags cpu = cpu_arch_flags;
1766
ab592e75
JB
1767 /* AVX512VL is no standalone feature - match it and then strip it. */
1768 if (x.bitfield.cpuavx512vl && !cpu.bitfield.cpuavx512vl)
1769 return match;
1770 x.bitfield.cpuavx512vl = 0;
1771
22c36940
JB
1772 /* AVX and AVX2 present at the same time express an operand size
1773 dependency - strip AVX2 for the purposes here. The operand size
1774 dependent check occurs in check_vecOperands(). */
1775 if (x.bitfield.cpuavx && x.bitfield.cpuavx2)
1776 x.bitfield.cpuavx2 = 0;
1777
3629bb00 1778 cpu = cpu_flags_and (x, cpu);
c0f3af97
L
1779 if (!cpu_flags_all_zero (&cpu))
1780 {
57392598 1781 if (x.bitfield.cpuavx)
a5ff0eb2 1782 {
929f69fa 1783 /* We need to check a few extra flags with AVX. */
b9d49817 1784 if (cpu.bitfield.cpuavx
40d231b4
JB
1785 && (!t->opcode_modifier.sse2avx
1786 || (sse2avx && !i.prefix[DATA_PREFIX]))
b9d49817 1787 && (!x.bitfield.cpuaes || cpu.bitfield.cpuaes)
929f69fa 1788 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
b9d49817
JB
1789 && (!x.bitfield.cpupclmul || cpu.bitfield.cpupclmul))
1790 match |= CPU_FLAGS_ARCH_MATCH;
a5ff0eb2 1791 }
929f69fa
JB
1792 else if (x.bitfield.cpuavx512f)
1793 {
1794 /* We need to check a few extra flags with AVX512F. */
1795 if (cpu.bitfield.cpuavx512f
1796 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
1797 && (!x.bitfield.cpuvaes || cpu.bitfield.cpuvaes)
1798 && (!x.bitfield.cpuvpclmulqdq || cpu.bitfield.cpuvpclmulqdq))
1799 match |= CPU_FLAGS_ARCH_MATCH;
1800 }
a5ff0eb2 1801 else
db12e14e 1802 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 1803 }
3629bb00 1804 }
c0f3af97 1805 return match;
40fb9820
L
1806}
1807
c6fb90c8
L
1808static INLINE i386_operand_type
1809operand_type_and (i386_operand_type x, i386_operand_type y)
40fb9820 1810{
bab6aec1
JB
1811 if (x.bitfield.class != y.bitfield.class)
1812 x.bitfield.class = ClassNone;
75e5731b
JB
1813 if (x.bitfield.instance != y.bitfield.instance)
1814 x.bitfield.instance = InstanceNone;
bab6aec1 1815
c6fb90c8
L
1816 switch (ARRAY_SIZE (x.array))
1817 {
1818 case 3:
1819 x.array [2] &= y.array [2];
1a0670f3 1820 /* Fall through. */
c6fb90c8
L
1821 case 2:
1822 x.array [1] &= y.array [1];
1a0670f3 1823 /* Fall through. */
c6fb90c8
L
1824 case 1:
1825 x.array [0] &= y.array [0];
1826 break;
1827 default:
1828 abort ();
1829 }
1830 return x;
40fb9820
L
1831}
1832
73053c1f
JB
1833static INLINE i386_operand_type
1834operand_type_and_not (i386_operand_type x, i386_operand_type y)
1835{
bab6aec1 1836 gas_assert (y.bitfield.class == ClassNone);
75e5731b 1837 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 1838
73053c1f
JB
1839 switch (ARRAY_SIZE (x.array))
1840 {
1841 case 3:
1842 x.array [2] &= ~y.array [2];
1843 /* Fall through. */
1844 case 2:
1845 x.array [1] &= ~y.array [1];
1846 /* Fall through. */
1847 case 1:
1848 x.array [0] &= ~y.array [0];
1849 break;
1850 default:
1851 abort ();
1852 }
1853 return x;
1854}
1855
c6fb90c8
L
1856static INLINE i386_operand_type
1857operand_type_or (i386_operand_type x, i386_operand_type y)
40fb9820 1858{
bab6aec1
JB
1859 gas_assert (x.bitfield.class == ClassNone ||
1860 y.bitfield.class == ClassNone ||
1861 x.bitfield.class == y.bitfield.class);
75e5731b
JB
1862 gas_assert (x.bitfield.instance == InstanceNone ||
1863 y.bitfield.instance == InstanceNone ||
1864 x.bitfield.instance == y.bitfield.instance);
bab6aec1 1865
c6fb90c8 1866 switch (ARRAY_SIZE (x.array))
40fb9820 1867 {
c6fb90c8
L
1868 case 3:
1869 x.array [2] |= y.array [2];
1a0670f3 1870 /* Fall through. */
c6fb90c8
L
1871 case 2:
1872 x.array [1] |= y.array [1];
1a0670f3 1873 /* Fall through. */
c6fb90c8
L
1874 case 1:
1875 x.array [0] |= y.array [0];
40fb9820
L
1876 break;
1877 default:
1878 abort ();
1879 }
c6fb90c8
L
1880 return x;
1881}
40fb9820 1882
c6fb90c8
L
1883static INLINE i386_operand_type
1884operand_type_xor (i386_operand_type x, i386_operand_type y)
1885{
bab6aec1 1886 gas_assert (y.bitfield.class == ClassNone);
75e5731b 1887 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 1888
c6fb90c8
L
1889 switch (ARRAY_SIZE (x.array))
1890 {
1891 case 3:
1892 x.array [2] ^= y.array [2];
1a0670f3 1893 /* Fall through. */
c6fb90c8
L
1894 case 2:
1895 x.array [1] ^= y.array [1];
1a0670f3 1896 /* Fall through. */
c6fb90c8
L
1897 case 1:
1898 x.array [0] ^= y.array [0];
1899 break;
1900 default:
1901 abort ();
1902 }
40fb9820
L
1903 return x;
1904}
1905
40fb9820 1906static const i386_operand_type disp16_32 = OPERAND_TYPE_DISP16_32;
bab6aec1
JB
1907static const i386_operand_type anydisp = OPERAND_TYPE_ANYDISP;
1908static const i386_operand_type anyimm = OPERAND_TYPE_ANYIMM;
40fb9820 1909static const i386_operand_type regxmm = OPERAND_TYPE_REGXMM;
43234a1e 1910static const i386_operand_type regmask = OPERAND_TYPE_REGMASK;
40fb9820
L
1911static const i386_operand_type imm8 = OPERAND_TYPE_IMM8;
1912static const i386_operand_type imm8s = OPERAND_TYPE_IMM8S;
1913static const i386_operand_type imm16 = OPERAND_TYPE_IMM16;
1914static const i386_operand_type imm32 = OPERAND_TYPE_IMM32;
1915static const i386_operand_type imm32s = OPERAND_TYPE_IMM32S;
1916static const i386_operand_type imm64 = OPERAND_TYPE_IMM64;
1917static const i386_operand_type imm16_32 = OPERAND_TYPE_IMM16_32;
1918static const i386_operand_type imm16_32s = OPERAND_TYPE_IMM16_32S;
1919static const i386_operand_type imm16_32_32s = OPERAND_TYPE_IMM16_32_32S;
1920
1921enum operand_type
1922{
1923 reg,
40fb9820
L
1924 imm,
1925 disp,
1926 anymem
1927};
1928
c6fb90c8 1929static INLINE int
40fb9820
L
1930operand_type_check (i386_operand_type t, enum operand_type c)
1931{
1932 switch (c)
1933 {
1934 case reg:
bab6aec1 1935 return t.bitfield.class == Reg;
40fb9820 1936
40fb9820
L
1937 case imm:
1938 return (t.bitfield.imm8
1939 || t.bitfield.imm8s
1940 || t.bitfield.imm16
1941 || t.bitfield.imm32
1942 || t.bitfield.imm32s
1943 || t.bitfield.imm64);
1944
1945 case disp:
1946 return (t.bitfield.disp8
1947 || t.bitfield.disp16
1948 || t.bitfield.disp32
40fb9820
L
1949 || t.bitfield.disp64);
1950
1951 case anymem:
1952 return (t.bitfield.disp8
1953 || t.bitfield.disp16
1954 || t.bitfield.disp32
40fb9820
L
1955 || t.bitfield.disp64
1956 || t.bitfield.baseindex);
1957
1958 default:
1959 abort ();
1960 }
2cfe26b6
AM
1961
1962 return 0;
40fb9820
L
1963}
1964
7a54636a
L
1965/* Return 1 if there is no conflict in 8bit/16bit/32bit/64bit/80bit size
1966 between operand GIVEN and opeand WANTED for instruction template T. */
5c07affc
L
1967
1968static INLINE int
7a54636a
L
1969match_operand_size (const insn_template *t, unsigned int wanted,
1970 unsigned int given)
5c07affc 1971{
3ac21baa
JB
1972 return !((i.types[given].bitfield.byte
1973 && !t->operand_types[wanted].bitfield.byte)
1974 || (i.types[given].bitfield.word
1975 && !t->operand_types[wanted].bitfield.word)
1976 || (i.types[given].bitfield.dword
1977 && !t->operand_types[wanted].bitfield.dword)
1978 || (i.types[given].bitfield.qword
1979 && !t->operand_types[wanted].bitfield.qword)
1980 || (i.types[given].bitfield.tbyte
1981 && !t->operand_types[wanted].bitfield.tbyte));
5c07affc
L
1982}
1983
dd40ce22
L
1984/* Return 1 if there is no conflict in SIMD register between operand
1985 GIVEN and opeand WANTED for instruction template T. */
1b54b8d7
JB
1986
1987static INLINE int
dd40ce22
L
1988match_simd_size (const insn_template *t, unsigned int wanted,
1989 unsigned int given)
1b54b8d7 1990{
3ac21baa
JB
1991 return !((i.types[given].bitfield.xmmword
1992 && !t->operand_types[wanted].bitfield.xmmword)
1993 || (i.types[given].bitfield.ymmword
1994 && !t->operand_types[wanted].bitfield.ymmword)
1995 || (i.types[given].bitfield.zmmword
260cd341
LC
1996 && !t->operand_types[wanted].bitfield.zmmword)
1997 || (i.types[given].bitfield.tmmword
1998 && !t->operand_types[wanted].bitfield.tmmword));
1b54b8d7
JB
1999}
2000
7a54636a
L
2001/* Return 1 if there is no conflict in any size between operand GIVEN
2002 and opeand WANTED for instruction template T. */
5c07affc
L
2003
2004static INLINE int
dd40ce22
L
2005match_mem_size (const insn_template *t, unsigned int wanted,
2006 unsigned int given)
5c07affc 2007{
7a54636a 2008 return (match_operand_size (t, wanted, given)
3ac21baa 2009 && !((i.types[given].bitfield.unspecified
5273a3cd 2010 && !i.broadcast.type
a5748e0d 2011 && !i.broadcast.bytes
3ac21baa
JB
2012 && !t->operand_types[wanted].bitfield.unspecified)
2013 || (i.types[given].bitfield.fword
2014 && !t->operand_types[wanted].bitfield.fword)
1b54b8d7
JB
2015 /* For scalar opcode templates to allow register and memory
2016 operands at the same time, some special casing is needed
d6793fa1
JB
2017 here. Also for v{,p}broadcast*, {,v}pmov{s,z}*, and
2018 down-conversion vpmov*. */
3528c362 2019 || ((t->operand_types[wanted].bitfield.class == RegSIMD
bc49bfd8
JB
2020 && t->operand_types[wanted].bitfield.byte
2021 + t->operand_types[wanted].bitfield.word
2022 + t->operand_types[wanted].bitfield.dword
2023 + t->operand_types[wanted].bitfield.qword
2024 > !!t->opcode_modifier.broadcast)
3ac21baa
JB
2025 ? (i.types[given].bitfield.xmmword
2026 || i.types[given].bitfield.ymmword
2027 || i.types[given].bitfield.zmmword)
2028 : !match_simd_size(t, wanted, given))));
5c07affc
L
2029}
2030
3ac21baa
JB
2031/* Return value has MATCH_STRAIGHT set if there is no size conflict on any
2032 operands for instruction template T, and it has MATCH_REVERSE set if there
2033 is no size conflict on any operands for the template with operands reversed
2034 (and the template allows for reversing in the first place). */
5c07affc 2035
3ac21baa
JB
2036#define MATCH_STRAIGHT 1
2037#define MATCH_REVERSE 2
2038
2039static INLINE unsigned int
d3ce72d0 2040operand_size_match (const insn_template *t)
5c07affc 2041{
3ac21baa 2042 unsigned int j, match = MATCH_STRAIGHT;
5c07affc 2043
0cfa3eb3 2044 /* Don't check non-absolute jump instructions. */
5c07affc 2045 if (t->opcode_modifier.jump
0cfa3eb3 2046 && t->opcode_modifier.jump != JUMP_ABSOLUTE)
5c07affc
L
2047 return match;
2048
2049 /* Check memory and accumulator operand size. */
2050 for (j = 0; j < i.operands; j++)
2051 {
3528c362
JB
2052 if (i.types[j].bitfield.class != Reg
2053 && i.types[j].bitfield.class != RegSIMD
601e8564 2054 && t->opcode_modifier.anysize)
5c07affc
L
2055 continue;
2056
bab6aec1 2057 if (t->operand_types[j].bitfield.class == Reg
7a54636a 2058 && !match_operand_size (t, j, j))
5c07affc
L
2059 {
2060 match = 0;
2061 break;
2062 }
2063
3528c362 2064 if (t->operand_types[j].bitfield.class == RegSIMD
3ac21baa 2065 && !match_simd_size (t, j, j))
1b54b8d7
JB
2066 {
2067 match = 0;
2068 break;
2069 }
2070
75e5731b 2071 if (t->operand_types[j].bitfield.instance == Accum
7a54636a 2072 && (!match_operand_size (t, j, j) || !match_simd_size (t, j, j)))
1b54b8d7
JB
2073 {
2074 match = 0;
2075 break;
2076 }
2077
c48dadc9 2078 if ((i.flags[j] & Operand_Mem) && !match_mem_size (t, j, j))
5c07affc
L
2079 {
2080 match = 0;
2081 break;
2082 }
2083 }
2084
3ac21baa 2085 if (!t->opcode_modifier.d)
891edac4 2086 {
dc1e8a47 2087 mismatch:
3ac21baa
JB
2088 if (!match)
2089 i.error = operand_size_mismatch;
2090 return match;
891edac4 2091 }
5c07affc
L
2092
2093 /* Check reverse. */
8bd915b7
JB
2094 gas_assert ((i.operands >= 2 && i.operands <= 3)
2095 || t->opcode_modifier.vexsources);
5c07affc 2096
f5eb1d70 2097 for (j = 0; j < i.operands; j++)
5c07affc 2098 {
f5eb1d70
JB
2099 unsigned int given = i.operands - j - 1;
2100
8bd915b7
JB
2101 /* For 4- and 5-operand insns VEX.W controls just the first two
2102 register operands. */
2103 if (t->opcode_modifier.vexsources)
2104 given = j < 2 ? 1 - j : j;
2105
bab6aec1 2106 if (t->operand_types[j].bitfield.class == Reg
f5eb1d70 2107 && !match_operand_size (t, j, given))
891edac4 2108 goto mismatch;
5c07affc 2109
3528c362 2110 if (t->operand_types[j].bitfield.class == RegSIMD
f5eb1d70 2111 && !match_simd_size (t, j, given))
dbbc8b7e
JB
2112 goto mismatch;
2113
75e5731b 2114 if (t->operand_types[j].bitfield.instance == Accum
f5eb1d70
JB
2115 && (!match_operand_size (t, j, given)
2116 || !match_simd_size (t, j, given)))
dbbc8b7e
JB
2117 goto mismatch;
2118
f5eb1d70 2119 if ((i.flags[given] & Operand_Mem) && !match_mem_size (t, j, given))
891edac4 2120 goto mismatch;
5c07affc
L
2121 }
2122
3ac21baa 2123 return match | MATCH_REVERSE;
5c07affc
L
2124}
2125
c6fb90c8 2126static INLINE int
40fb9820
L
2127operand_type_match (i386_operand_type overlap,
2128 i386_operand_type given)
2129{
2130 i386_operand_type temp = overlap;
2131
7d5e4556 2132 temp.bitfield.unspecified = 0;
5c07affc
L
2133 temp.bitfield.byte = 0;
2134 temp.bitfield.word = 0;
2135 temp.bitfield.dword = 0;
2136 temp.bitfield.fword = 0;
2137 temp.bitfield.qword = 0;
2138 temp.bitfield.tbyte = 0;
2139 temp.bitfield.xmmword = 0;
c0f3af97 2140 temp.bitfield.ymmword = 0;
43234a1e 2141 temp.bitfield.zmmword = 0;
260cd341 2142 temp.bitfield.tmmword = 0;
0dfbf9d7 2143 if (operand_type_all_zero (&temp))
891edac4 2144 goto mismatch;
40fb9820 2145
6f2f06be 2146 if (given.bitfield.baseindex == overlap.bitfield.baseindex)
891edac4
L
2147 return 1;
2148
dc1e8a47 2149 mismatch:
a65babc9 2150 i.error = operand_type_mismatch;
891edac4 2151 return 0;
40fb9820
L
2152}
2153
7d5e4556 2154/* If given types g0 and g1 are registers they must be of the same type
10c17abd 2155 unless the expected operand type register overlap is null.
5de4d9ef 2156 Some Intel syntax memory operand size checking also happens here. */
40fb9820 2157
c6fb90c8 2158static INLINE int
dc821c5f 2159operand_type_register_match (i386_operand_type g0,
40fb9820 2160 i386_operand_type t0,
40fb9820
L
2161 i386_operand_type g1,
2162 i386_operand_type t1)
2163{
bab6aec1 2164 if (g0.bitfield.class != Reg
3528c362 2165 && g0.bitfield.class != RegSIMD
10c17abd
JB
2166 && (!operand_type_check (g0, anymem)
2167 || g0.bitfield.unspecified
5de4d9ef
JB
2168 || (t0.bitfield.class != Reg
2169 && t0.bitfield.class != RegSIMD)))
40fb9820
L
2170 return 1;
2171
bab6aec1 2172 if (g1.bitfield.class != Reg
3528c362 2173 && g1.bitfield.class != RegSIMD
10c17abd
JB
2174 && (!operand_type_check (g1, anymem)
2175 || g1.bitfield.unspecified
5de4d9ef
JB
2176 || (t1.bitfield.class != Reg
2177 && t1.bitfield.class != RegSIMD)))
40fb9820
L
2178 return 1;
2179
dc821c5f
JB
2180 if (g0.bitfield.byte == g1.bitfield.byte
2181 && g0.bitfield.word == g1.bitfield.word
2182 && g0.bitfield.dword == g1.bitfield.dword
10c17abd
JB
2183 && g0.bitfield.qword == g1.bitfield.qword
2184 && g0.bitfield.xmmword == g1.bitfield.xmmword
2185 && g0.bitfield.ymmword == g1.bitfield.ymmword
2186 && g0.bitfield.zmmword == g1.bitfield.zmmword)
40fb9820
L
2187 return 1;
2188
c4d09633
JB
2189 /* If expectations overlap in no more than a single size, all is fine. */
2190 g0 = operand_type_and (t0, t1);
2191 if (g0.bitfield.byte
2192 + g0.bitfield.word
2193 + g0.bitfield.dword
2194 + g0.bitfield.qword
2195 + g0.bitfield.xmmword
2196 + g0.bitfield.ymmword
2197 + g0.bitfield.zmmword <= 1)
891edac4
L
2198 return 1;
2199
a65babc9 2200 i.error = register_type_mismatch;
891edac4
L
2201
2202 return 0;
40fb9820
L
2203}
2204
4c692bc7
JB
2205static INLINE unsigned int
2206register_number (const reg_entry *r)
2207{
2208 unsigned int nr = r->reg_num;
2209
2210 if (r->reg_flags & RegRex)
2211 nr += 8;
2212
200cbe0f
L
2213 if (r->reg_flags & RegVRex)
2214 nr += 16;
2215
4c692bc7
JB
2216 return nr;
2217}
2218
252b5132 2219static INLINE unsigned int
40fb9820 2220mode_from_disp_size (i386_operand_type t)
252b5132 2221{
b5014f7a 2222 if (t.bitfield.disp8)
40fb9820
L
2223 return 1;
2224 else if (t.bitfield.disp16
a775efc8 2225 || t.bitfield.disp32)
40fb9820
L
2226 return 2;
2227 else
2228 return 0;
252b5132
RH
2229}
2230
2231static INLINE int
65879393 2232fits_in_signed_byte (addressT num)
252b5132 2233{
65879393 2234 return num + 0x80 <= 0xff;
47926f60 2235}
252b5132
RH
2236
2237static INLINE int
65879393 2238fits_in_unsigned_byte (addressT num)
252b5132 2239{
65879393 2240 return num <= 0xff;
47926f60 2241}
252b5132
RH
2242
2243static INLINE int
65879393 2244fits_in_unsigned_word (addressT num)
252b5132 2245{
65879393 2246 return num <= 0xffff;
47926f60 2247}
252b5132
RH
2248
2249static INLINE int
65879393 2250fits_in_signed_word (addressT num)
252b5132 2251{
65879393 2252 return num + 0x8000 <= 0xffff;
47926f60 2253}
2a962e6d 2254
3e73aa7c 2255static INLINE int
65879393 2256fits_in_signed_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2257{
2258#ifndef BFD64
2259 return 1;
2260#else
65879393 2261 return num + 0x80000000 <= 0xffffffff;
3e73aa7c
JH
2262#endif
2263} /* fits_in_signed_long() */
2a962e6d 2264
3e73aa7c 2265static INLINE int
65879393 2266fits_in_unsigned_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2267{
2268#ifndef BFD64
2269 return 1;
2270#else
65879393 2271 return num <= 0xffffffff;
3e73aa7c
JH
2272#endif
2273} /* fits_in_unsigned_long() */
252b5132 2274
a442cac5
JB
2275static INLINE valueT extend_to_32bit_address (addressT num)
2276{
2277#ifdef BFD64
2278 if (fits_in_unsigned_long(num))
2279 return (num ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
2280
2281 if (!fits_in_signed_long (num))
2282 return num & 0xffffffff;
2283#endif
2284
2285 return num;
2286}
2287
43234a1e 2288static INLINE int
b5014f7a 2289fits_in_disp8 (offsetT num)
43234a1e
L
2290{
2291 int shift = i.memshift;
2292 unsigned int mask;
2293
2294 if (shift == -1)
2295 abort ();
2296
2297 mask = (1 << shift) - 1;
2298
2299 /* Return 0 if NUM isn't properly aligned. */
2300 if ((num & mask))
2301 return 0;
2302
2303 /* Check if NUM will fit in 8bit after shift. */
2304 return fits_in_signed_byte (num >> shift);
2305}
2306
a683cc34
SP
2307static INLINE int
2308fits_in_imm4 (offsetT num)
2309{
2310 return (num & 0xf) == num;
2311}
2312
40fb9820 2313static i386_operand_type
e3bb37b5 2314smallest_imm_type (offsetT num)
252b5132 2315{
40fb9820 2316 i386_operand_type t;
7ab9ffdd 2317
0dfbf9d7 2318 operand_type_set (&t, 0);
40fb9820
L
2319 t.bitfield.imm64 = 1;
2320
2321 if (cpu_arch_tune != PROCESSOR_I486 && num == 1)
e413e4e9
AM
2322 {
2323 /* This code is disabled on the 486 because all the Imm1 forms
2324 in the opcode table are slower on the i486. They're the
2325 versions with the implicitly specified single-position
2326 displacement, which has another syntax if you really want to
2327 use that form. */
40fb9820
L
2328 t.bitfield.imm1 = 1;
2329 t.bitfield.imm8 = 1;
2330 t.bitfield.imm8s = 1;
2331 t.bitfield.imm16 = 1;
2332 t.bitfield.imm32 = 1;
2333 t.bitfield.imm32s = 1;
2334 }
2335 else if (fits_in_signed_byte (num))
2336 {
2337 t.bitfield.imm8 = 1;
2338 t.bitfield.imm8s = 1;
2339 t.bitfield.imm16 = 1;
2340 t.bitfield.imm32 = 1;
2341 t.bitfield.imm32s = 1;
2342 }
2343 else if (fits_in_unsigned_byte (num))
2344 {
2345 t.bitfield.imm8 = 1;
2346 t.bitfield.imm16 = 1;
2347 t.bitfield.imm32 = 1;
2348 t.bitfield.imm32s = 1;
2349 }
2350 else if (fits_in_signed_word (num) || fits_in_unsigned_word (num))
2351 {
2352 t.bitfield.imm16 = 1;
2353 t.bitfield.imm32 = 1;
2354 t.bitfield.imm32s = 1;
2355 }
2356 else if (fits_in_signed_long (num))
2357 {
2358 t.bitfield.imm32 = 1;
2359 t.bitfield.imm32s = 1;
2360 }
2361 else if (fits_in_unsigned_long (num))
2362 t.bitfield.imm32 = 1;
2363
2364 return t;
47926f60 2365}
252b5132 2366
847f7ad4 2367static offsetT
e3bb37b5 2368offset_in_range (offsetT val, int size)
847f7ad4 2369{
508866be 2370 addressT mask;
ba2adb93 2371
847f7ad4
AM
2372 switch (size)
2373 {
508866be
L
2374 case 1: mask = ((addressT) 1 << 8) - 1; break;
2375 case 2: mask = ((addressT) 1 << 16) - 1; break;
3e73aa7c 2376#ifdef BFD64
64965897 2377 case 4: mask = ((addressT) 1 << 32) - 1; break;
3e73aa7c 2378#endif
64965897 2379 case sizeof (val): return val;
47926f60 2380 default: abort ();
847f7ad4
AM
2381 }
2382
4fe51f7d 2383 if ((val & ~mask) != 0 && (-val & ~mask) != 0)
f493c217
AM
2384 as_warn (_("0x%" PRIx64 " shortened to 0x%" PRIx64),
2385 (uint64_t) val, (uint64_t) (val & mask));
847f7ad4 2386
847f7ad4
AM
2387 return val & mask;
2388}
2389
c32fa91d
L
2390enum PREFIX_GROUP
2391{
2392 PREFIX_EXIST = 0,
2393 PREFIX_LOCK,
2394 PREFIX_REP,
04ef582a 2395 PREFIX_DS,
c32fa91d
L
2396 PREFIX_OTHER
2397};
2398
2399/* Returns
2400 a. PREFIX_EXIST if attempting to add a prefix where one from the
2401 same class already exists.
2402 b. PREFIX_LOCK if lock prefix is added.
2403 c. PREFIX_REP if rep/repne prefix is added.
04ef582a
L
2404 d. PREFIX_DS if ds prefix is added.
2405 e. PREFIX_OTHER if other prefix is added.
c32fa91d
L
2406 */
2407
2408static enum PREFIX_GROUP
e3bb37b5 2409add_prefix (unsigned int prefix)
252b5132 2410{
c32fa91d 2411 enum PREFIX_GROUP ret = PREFIX_OTHER;
b1905489 2412 unsigned int q;
252b5132 2413
29b0f896
AM
2414 if (prefix >= REX_OPCODE && prefix < REX_OPCODE + 16
2415 && flag_code == CODE_64BIT)
b1905489 2416 {
161a04f6 2417 if ((i.prefix[REX_PREFIX] & prefix & REX_W)
44846f29
JB
2418 || (i.prefix[REX_PREFIX] & prefix & REX_R)
2419 || (i.prefix[REX_PREFIX] & prefix & REX_X)
2420 || (i.prefix[REX_PREFIX] & prefix & REX_B))
c32fa91d 2421 ret = PREFIX_EXIST;
b1905489
JB
2422 q = REX_PREFIX;
2423 }
3e73aa7c 2424 else
b1905489
JB
2425 {
2426 switch (prefix)
2427 {
2428 default:
2429 abort ();
2430
b1905489 2431 case DS_PREFIX_OPCODE:
04ef582a
L
2432 ret = PREFIX_DS;
2433 /* Fall through. */
2434 case CS_PREFIX_OPCODE:
b1905489
JB
2435 case ES_PREFIX_OPCODE:
2436 case FS_PREFIX_OPCODE:
2437 case GS_PREFIX_OPCODE:
2438 case SS_PREFIX_OPCODE:
2439 q = SEG_PREFIX;
2440 break;
2441
2442 case REPNE_PREFIX_OPCODE:
2443 case REPE_PREFIX_OPCODE:
c32fa91d
L
2444 q = REP_PREFIX;
2445 ret = PREFIX_REP;
2446 break;
2447
b1905489 2448 case LOCK_PREFIX_OPCODE:
c32fa91d
L
2449 q = LOCK_PREFIX;
2450 ret = PREFIX_LOCK;
b1905489
JB
2451 break;
2452
2453 case FWAIT_OPCODE:
2454 q = WAIT_PREFIX;
2455 break;
2456
2457 case ADDR_PREFIX_OPCODE:
2458 q = ADDR_PREFIX;
2459 break;
2460
2461 case DATA_PREFIX_OPCODE:
2462 q = DATA_PREFIX;
2463 break;
2464 }
2465 if (i.prefix[q] != 0)
c32fa91d 2466 ret = PREFIX_EXIST;
b1905489 2467 }
252b5132 2468
b1905489 2469 if (ret)
252b5132 2470 {
b1905489
JB
2471 if (!i.prefix[q])
2472 ++i.prefixes;
2473 i.prefix[q] |= prefix;
252b5132 2474 }
b1905489
JB
2475 else
2476 as_bad (_("same type of prefix used twice"));
252b5132 2477
252b5132
RH
2478 return ret;
2479}
2480
2481static void
78f12dd3 2482update_code_flag (int value, int check)
eecb386c 2483{
78f12dd3
L
2484 PRINTF_LIKE ((*as_error));
2485
1e9cc1c2 2486 flag_code = (enum flag_code) value;
40fb9820
L
2487 if (flag_code == CODE_64BIT)
2488 {
2489 cpu_arch_flags.bitfield.cpu64 = 1;
2490 cpu_arch_flags.bitfield.cpuno64 = 0;
40fb9820
L
2491 }
2492 else
2493 {
2494 cpu_arch_flags.bitfield.cpu64 = 0;
2495 cpu_arch_flags.bitfield.cpuno64 = 1;
40fb9820
L
2496 }
2497 if (value == CODE_64BIT && !cpu_arch_flags.bitfield.cpulm )
3e73aa7c 2498 {
78f12dd3
L
2499 if (check)
2500 as_error = as_fatal;
2501 else
2502 as_error = as_bad;
2503 (*as_error) (_("64bit mode not supported on `%s'."),
2504 cpu_arch_name ? cpu_arch_name : default_arch);
3e73aa7c 2505 }
40fb9820 2506 if (value == CODE_32BIT && !cpu_arch_flags.bitfield.cpui386)
3e73aa7c 2507 {
78f12dd3
L
2508 if (check)
2509 as_error = as_fatal;
2510 else
2511 as_error = as_bad;
2512 (*as_error) (_("32bit mode not supported on `%s'."),
2513 cpu_arch_name ? cpu_arch_name : default_arch);
3e73aa7c 2514 }
eecb386c
AM
2515 stackop_size = '\0';
2516}
2517
78f12dd3
L
2518static void
2519set_code_flag (int value)
2520{
2521 update_code_flag (value, 0);
2522}
2523
eecb386c 2524static void
e3bb37b5 2525set_16bit_gcc_code_flag (int new_code_flag)
252b5132 2526{
1e9cc1c2 2527 flag_code = (enum flag_code) new_code_flag;
40fb9820
L
2528 if (flag_code != CODE_16BIT)
2529 abort ();
2530 cpu_arch_flags.bitfield.cpu64 = 0;
2531 cpu_arch_flags.bitfield.cpuno64 = 1;
9306ca4a 2532 stackop_size = LONG_MNEM_SUFFIX;
252b5132
RH
2533}
2534
2535static void
e3bb37b5 2536set_intel_syntax (int syntax_flag)
252b5132
RH
2537{
2538 /* Find out if register prefixing is specified. */
2539 int ask_naked_reg = 0;
2540
2541 SKIP_WHITESPACE ();
29b0f896 2542 if (!is_end_of_line[(unsigned char) *input_line_pointer])
252b5132 2543 {
d02603dc
NC
2544 char *string;
2545 int e = get_symbol_name (&string);
252b5132 2546
47926f60 2547 if (strcmp (string, "prefix") == 0)
252b5132 2548 ask_naked_reg = 1;
47926f60 2549 else if (strcmp (string, "noprefix") == 0)
252b5132
RH
2550 ask_naked_reg = -1;
2551 else
d0b47220 2552 as_bad (_("bad argument to syntax directive."));
d02603dc 2553 (void) restore_line_pointer (e);
252b5132
RH
2554 }
2555 demand_empty_rest_of_line ();
c3332e24 2556
252b5132
RH
2557 intel_syntax = syntax_flag;
2558
2559 if (ask_naked_reg == 0)
f86103b7
AM
2560 allow_naked_reg = (intel_syntax
2561 && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
252b5132
RH
2562 else
2563 allow_naked_reg = (ask_naked_reg < 0);
9306ca4a 2564
ee86248c 2565 expr_set_rank (O_full_ptr, syntax_flag ? 10 : 0);
7ab9ffdd 2566
e4a3b5a4 2567 identifier_chars['%'] = intel_syntax && allow_naked_reg ? '%' : 0;
9306ca4a 2568 identifier_chars['$'] = intel_syntax ? '$' : 0;
e4a3b5a4 2569 register_prefix = allow_naked_reg ? "" : "%";
252b5132
RH
2570}
2571
1efbbeb4
L
2572static void
2573set_intel_mnemonic (int mnemonic_flag)
2574{
e1d4d893 2575 intel_mnemonic = mnemonic_flag;
1efbbeb4
L
2576}
2577
db51cc60
L
2578static void
2579set_allow_index_reg (int flag)
2580{
2581 allow_index_reg = flag;
2582}
2583
cb19c032 2584static void
7bab8ab5 2585set_check (int what)
cb19c032 2586{
7bab8ab5
JB
2587 enum check_kind *kind;
2588 const char *str;
2589
2590 if (what)
2591 {
2592 kind = &operand_check;
2593 str = "operand";
2594 }
2595 else
2596 {
2597 kind = &sse_check;
2598 str = "sse";
2599 }
2600
cb19c032
L
2601 SKIP_WHITESPACE ();
2602
2603 if (!is_end_of_line[(unsigned char) *input_line_pointer])
2604 {
d02603dc
NC
2605 char *string;
2606 int e = get_symbol_name (&string);
cb19c032
L
2607
2608 if (strcmp (string, "none") == 0)
7bab8ab5 2609 *kind = check_none;
cb19c032 2610 else if (strcmp (string, "warning") == 0)
7bab8ab5 2611 *kind = check_warning;
cb19c032 2612 else if (strcmp (string, "error") == 0)
7bab8ab5 2613 *kind = check_error;
cb19c032 2614 else
7bab8ab5 2615 as_bad (_("bad argument to %s_check directive."), str);
d02603dc 2616 (void) restore_line_pointer (e);
cb19c032
L
2617 }
2618 else
7bab8ab5 2619 as_bad (_("missing argument for %s_check directive"), str);
cb19c032
L
2620
2621 demand_empty_rest_of_line ();
2622}
2623
8a9036a4
L
2624static void
2625check_cpu_arch_compatible (const char *name ATTRIBUTE_UNUSED,
1e9cc1c2 2626 i386_cpu_flags new_flag ATTRIBUTE_UNUSED)
8a9036a4
L
2627{
2628#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2629 static const char *arch;
2630
c085ab00 2631 /* Intel MCU is only supported on ELF. */
8a9036a4
L
2632 if (!IS_ELF)
2633 return;
2634
2635 if (!arch)
2636 {
2637 /* Use cpu_arch_name if it is set in md_parse_option. Otherwise
2638 use default_arch. */
2639 arch = cpu_arch_name;
2640 if (!arch)
2641 arch = default_arch;
2642 }
2643
81486035 2644 /* If we are targeting Intel MCU, we must enable it. */
648d04db
JB
2645 if ((get_elf_backend_data (stdoutput)->elf_machine_code == EM_IAMCU)
2646 == new_flag.bitfield.cpuiamcu)
81486035
L
2647 return;
2648
8a9036a4
L
2649 as_bad (_("`%s' is not supported on `%s'"), name, arch);
2650#endif
2651}
2652
8180707f
JB
2653static void
2654extend_cpu_sub_arch_name (const char *name)
2655{
2656 if (cpu_sub_arch_name)
2657 cpu_sub_arch_name = reconcat (cpu_sub_arch_name, cpu_sub_arch_name,
ae89daec 2658 ".", name, (const char *) NULL);
8180707f 2659 else
ae89daec 2660 cpu_sub_arch_name = concat (".", name, (const char *) NULL);
8180707f
JB
2661}
2662
e413e4e9 2663static void
e3bb37b5 2664set_cpu_arch (int dummy ATTRIBUTE_UNUSED)
e413e4e9 2665{
f68697e8
JB
2666 typedef struct arch_stack_entry
2667 {
2668 const struct arch_stack_entry *prev;
2669 const char *name;
2670 char *sub_name;
2671 i386_cpu_flags flags;
2672 i386_cpu_flags isa_flags;
2673 enum processor_type isa;
2674 enum flag_code flag_code;
2675 char stackop_size;
2676 bool no_cond_jump_promotion;
2677 } arch_stack_entry;
2678 static const arch_stack_entry *arch_stack_top;
2679
47926f60 2680 SKIP_WHITESPACE ();
e413e4e9 2681
29b0f896 2682 if (!is_end_of_line[(unsigned char) *input_line_pointer])
e413e4e9 2683 {
3ce2ebcf
JB
2684 char *s;
2685 int e = get_symbol_name (&s);
2686 const char *string = s;
2687 unsigned int j = 0;
40fb9820 2688 i386_cpu_flags flags;
e413e4e9 2689
3ce2ebcf
JB
2690 if (strcmp (string, "default") == 0)
2691 {
2692 if (strcmp (default_arch, "iamcu") == 0)
2693 string = default_arch;
2694 else
2695 {
2696 static const i386_cpu_flags cpu_unknown_flags = CPU_UNKNOWN_FLAGS;
2697
2698 cpu_arch_name = NULL;
2699 free (cpu_sub_arch_name);
2700 cpu_sub_arch_name = NULL;
2701 cpu_arch_flags = cpu_unknown_flags;
2702 if (flag_code == CODE_64BIT)
2703 {
2704 cpu_arch_flags.bitfield.cpu64 = 1;
2705 cpu_arch_flags.bitfield.cpuno64 = 0;
2706 }
2707 else
2708 {
2709 cpu_arch_flags.bitfield.cpu64 = 0;
2710 cpu_arch_flags.bitfield.cpuno64 = 1;
2711 }
2712 cpu_arch_isa = PROCESSOR_UNKNOWN;
ae89daec 2713 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
3ce2ebcf
JB
2714 if (!cpu_arch_tune_set)
2715 {
2716 cpu_arch_tune = cpu_arch_isa;
2717 cpu_arch_tune_flags = cpu_arch_isa_flags;
2718 }
2719
2720 j = ARRAY_SIZE (cpu_arch) + 1;
2721 }
2722 }
f68697e8
JB
2723 else if (strcmp (string, "push") == 0)
2724 {
2725 arch_stack_entry *top = XNEW (arch_stack_entry);
2726
2727 top->name = cpu_arch_name;
2728 if (cpu_sub_arch_name)
2729 top->sub_name = xstrdup (cpu_sub_arch_name);
2730 else
2731 top->sub_name = NULL;
2732 top->flags = cpu_arch_flags;
2733 top->isa = cpu_arch_isa;
2734 top->isa_flags = cpu_arch_isa_flags;
2735 top->flag_code = flag_code;
2736 top->stackop_size = stackop_size;
2737 top->no_cond_jump_promotion = no_cond_jump_promotion;
2738
2739 top->prev = arch_stack_top;
2740 arch_stack_top = top;
2741
2742 (void) restore_line_pointer (e);
2743 demand_empty_rest_of_line ();
2744 return;
2745 }
2746 else if (strcmp (string, "pop") == 0)
2747 {
2748 const arch_stack_entry *top = arch_stack_top;
2749
2750 if (!top)
2751 as_bad (_(".arch stack is empty"));
2752 else if (top->flag_code != flag_code
2753 || top->stackop_size != stackop_size)
2754 {
2755 static const unsigned int bits[] = {
2756 [CODE_16BIT] = 16,
2757 [CODE_32BIT] = 32,
2758 [CODE_64BIT] = 64,
2759 };
2760
2761 as_bad (_("this `.arch pop' requires `.code%u%s' to be in effect"),
2762 bits[top->flag_code],
2763 top->stackop_size == LONG_MNEM_SUFFIX ? "gcc" : "");
2764 }
2765 else
2766 {
2767 arch_stack_top = top->prev;
2768
2769 cpu_arch_name = top->name;
2770 free (cpu_sub_arch_name);
2771 cpu_sub_arch_name = top->sub_name;
2772 cpu_arch_flags = top->flags;
2773 cpu_arch_isa = top->isa;
2774 cpu_arch_isa_flags = top->isa_flags;
2775 no_cond_jump_promotion = top->no_cond_jump_promotion;
2776
2777 XDELETE (top);
2778 }
2779
2780 (void) restore_line_pointer (e);
2781 demand_empty_rest_of_line ();
2782 return;
2783 }
3ce2ebcf
JB
2784
2785 for (; j < ARRAY_SIZE (cpu_arch); j++)
e413e4e9 2786 {
ae89daec
JB
2787 if (strcmp (string + (*string == '.'), cpu_arch[j].name) == 0
2788 && (*string == '.') == (cpu_arch[j].type == PROCESSOR_NONE))
e413e4e9 2789 {
5c6af06e
JB
2790 if (*string != '.')
2791 {
ae89daec 2792 check_cpu_arch_compatible (string, cpu_arch[j].enable);
648d04db 2793
91d6fa6a 2794 cpu_arch_name = cpu_arch[j].name;
d92c7521 2795 free (cpu_sub_arch_name);
5c6af06e 2796 cpu_sub_arch_name = NULL;
ae89daec 2797 cpu_arch_flags = cpu_arch[j].enable;
40fb9820
L
2798 if (flag_code == CODE_64BIT)
2799 {
2800 cpu_arch_flags.bitfield.cpu64 = 1;
2801 cpu_arch_flags.bitfield.cpuno64 = 0;
2802 }
2803 else
2804 {
2805 cpu_arch_flags.bitfield.cpu64 = 0;
2806 cpu_arch_flags.bitfield.cpuno64 = 1;
2807 }
91d6fa6a 2808 cpu_arch_isa = cpu_arch[j].type;
ae89daec 2809 cpu_arch_isa_flags = cpu_arch[j].enable;
ccc9c027
L
2810 if (!cpu_arch_tune_set)
2811 {
2812 cpu_arch_tune = cpu_arch_isa;
2813 cpu_arch_tune_flags = cpu_arch_isa_flags;
2814 }
d59a54c2 2815 pre_386_16bit_warned = false;
5c6af06e
JB
2816 break;
2817 }
40fb9820 2818
ae89daec
JB
2819 if (cpu_flags_all_zero (&cpu_arch[j].enable))
2820 continue;
2821
293f5f65 2822 flags = cpu_flags_or (cpu_arch_flags,
ae89daec 2823 cpu_arch[j].enable);
81486035 2824
5b64d091 2825 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
5c6af06e 2826 {
ae89daec 2827 extend_cpu_sub_arch_name (string + 1);
40fb9820 2828 cpu_arch_flags = flags;
a586129e 2829 cpu_arch_isa_flags = flags;
5c6af06e 2830 }
0089dace
L
2831 else
2832 cpu_arch_isa_flags
2833 = cpu_flags_or (cpu_arch_isa_flags,
ae89daec 2834 cpu_arch[j].enable);
d02603dc 2835 (void) restore_line_pointer (e);
5c6af06e
JB
2836 demand_empty_rest_of_line ();
2837 return;
e413e4e9
AM
2838 }
2839 }
293f5f65 2840
ae89daec 2841 if (startswith (string, ".no") && j >= ARRAY_SIZE (cpu_arch))
293f5f65 2842 {
33eaf5de 2843 /* Disable an ISA extension. */
ae89daec
JB
2844 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
2845 if (cpu_arch[j].type == PROCESSOR_NONE
2846 && strcmp (string + 3, cpu_arch[j].name) == 0)
293f5f65
L
2847 {
2848 flags = cpu_flags_and_not (cpu_arch_flags,
ae89daec 2849 cpu_arch[j].disable);
293f5f65
L
2850 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
2851 {
ae89daec 2852 extend_cpu_sub_arch_name (string + 1);
293f5f65
L
2853 cpu_arch_flags = flags;
2854 cpu_arch_isa_flags = flags;
2855 }
2856 (void) restore_line_pointer (e);
2857 demand_empty_rest_of_line ();
2858 return;
2859 }
293f5f65
L
2860 }
2861
3ce2ebcf 2862 if (j == ARRAY_SIZE (cpu_arch))
e413e4e9
AM
2863 as_bad (_("no such architecture: `%s'"), string);
2864
2865 *input_line_pointer = e;
2866 }
2867 else
2868 as_bad (_("missing cpu architecture"));
2869
fddf5b5b
AM
2870 no_cond_jump_promotion = 0;
2871 if (*input_line_pointer == ','
29b0f896 2872 && !is_end_of_line[(unsigned char) input_line_pointer[1]])
fddf5b5b 2873 {
d02603dc
NC
2874 char *string;
2875 char e;
2876
2877 ++input_line_pointer;
2878 e = get_symbol_name (&string);
fddf5b5b
AM
2879
2880 if (strcmp (string, "nojumps") == 0)
2881 no_cond_jump_promotion = 1;
2882 else if (strcmp (string, "jumps") == 0)
2883 ;
2884 else
2885 as_bad (_("no such architecture modifier: `%s'"), string);
2886
d02603dc 2887 (void) restore_line_pointer (e);
fddf5b5b
AM
2888 }
2889
e413e4e9
AM
2890 demand_empty_rest_of_line ();
2891}
2892
8a9036a4
L
2893enum bfd_architecture
2894i386_arch (void)
2895{
c085ab00 2896 if (cpu_arch_isa == PROCESSOR_IAMCU)
81486035
L
2897 {
2898 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2899 || flag_code == CODE_64BIT)
2900 as_fatal (_("Intel MCU is 32bit ELF only"));
2901 return bfd_arch_iamcu;
2902 }
8a9036a4
L
2903 else
2904 return bfd_arch_i386;
2905}
2906
b9d79e03 2907unsigned long
7016a5d5 2908i386_mach (void)
b9d79e03 2909{
d34049e8 2910 if (startswith (default_arch, "x86_64"))
8a9036a4 2911 {
c085ab00 2912 if (default_arch[6] == '\0')
8a9036a4 2913 return bfd_mach_x86_64;
351f65ca
L
2914 else
2915 return bfd_mach_x64_32;
8a9036a4 2916 }
5197d474
L
2917 else if (!strcmp (default_arch, "i386")
2918 || !strcmp (default_arch, "iamcu"))
81486035
L
2919 {
2920 if (cpu_arch_isa == PROCESSOR_IAMCU)
2921 {
2922 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
2923 as_fatal (_("Intel MCU is 32bit ELF only"));
2924 return bfd_mach_i386_iamcu;
2925 }
2926 else
2927 return bfd_mach_i386_i386;
2928 }
b9d79e03 2929 else
2b5d6a91 2930 as_fatal (_("unknown architecture"));
b9d79e03 2931}
b9d79e03 2932\f
252b5132 2933void
7016a5d5 2934md_begin (void)
252b5132 2935{
86fa6981
L
2936 /* Support pseudo prefixes like {disp32}. */
2937 lex_type ['{'] = LEX_BEGIN_NAME;
2938
47926f60 2939 /* Initialize op_hash hash table. */
629310ab 2940 op_hash = str_htab_create ();
252b5132
RH
2941
2942 {
d3ce72d0 2943 const insn_template *optab;
29b0f896 2944 templates *core_optab;
252b5132 2945
47926f60
KH
2946 /* Setup for loop. */
2947 optab = i386_optab;
654d6f31 2948 core_optab = notes_alloc (sizeof (*core_optab));
252b5132
RH
2949 core_optab->start = optab;
2950
2951 while (1)
2952 {
2953 ++optab;
2954 if (optab->name == NULL
2955 || strcmp (optab->name, (optab - 1)->name) != 0)
2956 {
2957 /* different name --> ship out current template list;
47926f60 2958 add to hash table; & begin anew. */
252b5132 2959 core_optab->end = optab;
fe0e921f
AM
2960 if (str_hash_insert (op_hash, (optab - 1)->name, core_optab, 0))
2961 as_fatal (_("duplicate %s"), (optab - 1)->name);
2962
252b5132
RH
2963 if (optab->name == NULL)
2964 break;
654d6f31 2965 core_optab = notes_alloc (sizeof (*core_optab));
252b5132
RH
2966 core_optab->start = optab;
2967 }
2968 }
2969 }
2970
47926f60 2971 /* Initialize reg_hash hash table. */
629310ab 2972 reg_hash = str_htab_create ();
252b5132 2973 {
29b0f896 2974 const reg_entry *regtab;
c3fe08fa 2975 unsigned int regtab_size = i386_regtab_size;
252b5132 2976
c3fe08fa 2977 for (regtab = i386_regtab; regtab_size--; regtab++)
6225c532 2978 {
6288d05f
JB
2979 switch (regtab->reg_type.bitfield.class)
2980 {
2981 case Reg:
34684862
JB
2982 if (regtab->reg_type.bitfield.dword)
2983 {
2984 if (regtab->reg_type.bitfield.instance == Accum)
2985 reg_eax = regtab;
2986 }
2987 else if (regtab->reg_type.bitfield.tbyte)
6288d05f
JB
2988 {
2989 /* There's no point inserting st(<N>) in the hash table, as
2990 parentheses aren't included in register_chars[] anyway. */
2991 if (regtab->reg_type.bitfield.instance != Accum)
2992 continue;
2993 reg_st0 = regtab;
2994 }
2995 break;
2996
5e042380
JB
2997 case SReg:
2998 switch (regtab->reg_num)
2999 {
3000 case 0: reg_es = regtab; break;
3001 case 2: reg_ss = regtab; break;
3002 case 3: reg_ds = regtab; break;
3003 }
3004 break;
3005
6288d05f
JB
3006 case RegMask:
3007 if (!regtab->reg_num)
3008 reg_k0 = regtab;
3009 break;
3010 }
3011
6225c532
JB
3012 if (str_hash_insert (reg_hash, regtab->reg_name, regtab, 0) != NULL)
3013 as_fatal (_("duplicate %s"), regtab->reg_name);
6225c532 3014 }
252b5132
RH
3015 }
3016
47926f60 3017 /* Fill in lexical tables: mnemonic_chars, operand_chars. */
252b5132 3018 {
29b0f896
AM
3019 int c;
3020 char *p;
252b5132
RH
3021
3022 for (c = 0; c < 256; c++)
3023 {
014fbcda 3024 if (ISDIGIT (c) || ISLOWER (c))
252b5132
RH
3025 {
3026 mnemonic_chars[c] = c;
3027 register_chars[c] = c;
3028 operand_chars[c] = c;
3029 }
3882b010 3030 else if (ISUPPER (c))
252b5132 3031 {
3882b010 3032 mnemonic_chars[c] = TOLOWER (c);
252b5132
RH
3033 register_chars[c] = mnemonic_chars[c];
3034 operand_chars[c] = c;
3035 }
43234a1e 3036 else if (c == '{' || c == '}')
86fa6981
L
3037 {
3038 mnemonic_chars[c] = c;
3039 operand_chars[c] = c;
3040 }
b3983e5f
JB
3041#ifdef SVR4_COMMENT_CHARS
3042 else if (c == '\\' && strchr (i386_comment_chars, '/'))
3043 operand_chars[c] = c;
3044#endif
252b5132 3045
3882b010 3046 if (ISALPHA (c) || ISDIGIT (c))
252b5132
RH
3047 identifier_chars[c] = c;
3048 else if (c >= 128)
3049 {
3050 identifier_chars[c] = c;
3051 operand_chars[c] = c;
3052 }
3053 }
3054
3055#ifdef LEX_AT
3056 identifier_chars['@'] = '@';
32137342
NC
3057#endif
3058#ifdef LEX_QM
3059 identifier_chars['?'] = '?';
3060 operand_chars['?'] = '?';
252b5132 3061#endif
c0f3af97 3062 mnemonic_chars['_'] = '_';
791fe849 3063 mnemonic_chars['-'] = '-';
0003779b 3064 mnemonic_chars['.'] = '.';
252b5132
RH
3065 identifier_chars['_'] = '_';
3066 identifier_chars['.'] = '.';
3067
3068 for (p = operand_special_chars; *p != '\0'; p++)
3069 operand_chars[(unsigned char) *p] = *p;
3070 }
3071
a4447b93
RH
3072 if (flag_code == CODE_64BIT)
3073 {
ca19b261
KT
3074#if defined (OBJ_COFF) && defined (TE_PE)
3075 x86_dwarf2_return_column = (OUTPUT_FLAVOR == bfd_target_coff_flavour
3076 ? 32 : 16);
3077#else
a4447b93 3078 x86_dwarf2_return_column = 16;
ca19b261 3079#endif
61ff971f 3080 x86_cie_data_alignment = -8;
a4447b93
RH
3081 }
3082 else
3083 {
3084 x86_dwarf2_return_column = 8;
3085 x86_cie_data_alignment = -4;
3086 }
e379e5f3
L
3087
3088 /* NB: FUSED_JCC_PADDING frag must have sufficient room so that it
3089 can be turned into BRANCH_PREFIX frag. */
3090 if (align_branch_prefix_size > MAX_FUSED_JCC_PADDING_SIZE)
3091 abort ();
252b5132
RH
3092}
3093
3094void
e3bb37b5 3095i386_print_statistics (FILE *file)
252b5132 3096{
629310ab
ML
3097 htab_print_statistics (file, "i386 opcode", op_hash);
3098 htab_print_statistics (file, "i386 register", reg_hash);
252b5132 3099}
654d6f31
AM
3100
3101void
3102i386_md_end (void)
3103{
3104 htab_delete (op_hash);
3105 htab_delete (reg_hash);
3106}
252b5132 3107\f
252b5132
RH
3108#ifdef DEBUG386
3109
ce8a8b2f 3110/* Debugging routines for md_assemble. */
d3ce72d0 3111static void pte (insn_template *);
40fb9820 3112static void pt (i386_operand_type);
e3bb37b5
L
3113static void pe (expressionS *);
3114static void ps (symbolS *);
252b5132
RH
3115
3116static void
2c703856 3117pi (const char *line, i386_insn *x)
252b5132 3118{
09137c09 3119 unsigned int j;
252b5132
RH
3120
3121 fprintf (stdout, "%s: template ", line);
3122 pte (&x->tm);
09f131f2
JH
3123 fprintf (stdout, " address: base %s index %s scale %x\n",
3124 x->base_reg ? x->base_reg->reg_name : "none",
3125 x->index_reg ? x->index_reg->reg_name : "none",
3126 x->log2_scale_factor);
3127 fprintf (stdout, " modrm: mode %x reg %x reg/mem %x\n",
252b5132 3128 x->rm.mode, x->rm.reg, x->rm.regmem);
09f131f2
JH
3129 fprintf (stdout, " sib: base %x index %x scale %x\n",
3130 x->sib.base, x->sib.index, x->sib.scale);
3131 fprintf (stdout, " rex: 64bit %x extX %x extY %x extZ %x\n",
161a04f6
L
3132 (x->rex & REX_W) != 0,
3133 (x->rex & REX_R) != 0,
3134 (x->rex & REX_X) != 0,
3135 (x->rex & REX_B) != 0);
09137c09 3136 for (j = 0; j < x->operands; j++)
252b5132 3137 {
09137c09
SP
3138 fprintf (stdout, " #%d: ", j + 1);
3139 pt (x->types[j]);
252b5132 3140 fprintf (stdout, "\n");
bab6aec1 3141 if (x->types[j].bitfield.class == Reg
3528c362
JB
3142 || x->types[j].bitfield.class == RegMMX
3143 || x->types[j].bitfield.class == RegSIMD
dd6b8a0b 3144 || x->types[j].bitfield.class == RegMask
00cee14f 3145 || x->types[j].bitfield.class == SReg
4a5c67ed
JB
3146 || x->types[j].bitfield.class == RegCR
3147 || x->types[j].bitfield.class == RegDR
dd6b8a0b
JB
3148 || x->types[j].bitfield.class == RegTR
3149 || x->types[j].bitfield.class == RegBND)
09137c09
SP
3150 fprintf (stdout, "%s\n", x->op[j].regs->reg_name);
3151 if (operand_type_check (x->types[j], imm))
3152 pe (x->op[j].imms);
3153 if (operand_type_check (x->types[j], disp))
3154 pe (x->op[j].disps);
252b5132
RH
3155 }
3156}
3157
3158static void
d3ce72d0 3159pte (insn_template *t)
252b5132 3160{
b933fa4b 3161 static const unsigned char opc_pfx[] = { 0, 0x66, 0xf3, 0xf2 };
441f6aca 3162 static const char *const opc_spc[] = {
0cc78721 3163 NULL, "0f", "0f38", "0f3a", NULL, "evexmap5", "evexmap6", NULL,
441f6aca
JB
3164 "XOP08", "XOP09", "XOP0A",
3165 };
09137c09 3166 unsigned int j;
441f6aca 3167
252b5132 3168 fprintf (stdout, " %d operands ", t->operands);
441f6aca
JB
3169 if (opc_pfx[t->opcode_modifier.opcodeprefix])
3170 fprintf (stdout, "pfx %x ", opc_pfx[t->opcode_modifier.opcodeprefix]);
3171 if (opc_spc[t->opcode_modifier.opcodespace])
3172 fprintf (stdout, "space %s ", opc_spc[t->opcode_modifier.opcodespace]);
47926f60 3173 fprintf (stdout, "opcode %x ", t->base_opcode);
252b5132
RH
3174 if (t->extension_opcode != None)
3175 fprintf (stdout, "ext %x ", t->extension_opcode);
40fb9820 3176 if (t->opcode_modifier.d)
252b5132 3177 fprintf (stdout, "D");
40fb9820 3178 if (t->opcode_modifier.w)
252b5132
RH
3179 fprintf (stdout, "W");
3180 fprintf (stdout, "\n");
09137c09 3181 for (j = 0; j < t->operands; j++)
252b5132 3182 {
09137c09
SP
3183 fprintf (stdout, " #%d type ", j + 1);
3184 pt (t->operand_types[j]);
252b5132
RH
3185 fprintf (stdout, "\n");
3186 }
3187}
3188
3189static void
e3bb37b5 3190pe (expressionS *e)
252b5132 3191{
24eab124 3192 fprintf (stdout, " operation %d\n", e->X_op);
b8281767
AM
3193 fprintf (stdout, " add_number %" PRId64 " (%" PRIx64 ")\n",
3194 (int64_t) e->X_add_number, (uint64_t) (valueT) e->X_add_number);
252b5132
RH
3195 if (e->X_add_symbol)
3196 {
3197 fprintf (stdout, " add_symbol ");
3198 ps (e->X_add_symbol);
3199 fprintf (stdout, "\n");
3200 }
3201 if (e->X_op_symbol)
3202 {
3203 fprintf (stdout, " op_symbol ");
3204 ps (e->X_op_symbol);
3205 fprintf (stdout, "\n");
3206 }
3207}
3208
3209static void
e3bb37b5 3210ps (symbolS *s)
252b5132
RH
3211{
3212 fprintf (stdout, "%s type %s%s",
3213 S_GET_NAME (s),
3214 S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
3215 segment_name (S_GET_SEGMENT (s)));
3216}
3217
7b81dfbb 3218static struct type_name
252b5132 3219 {
40fb9820
L
3220 i386_operand_type mask;
3221 const char *name;
252b5132 3222 }
7b81dfbb 3223const type_names[] =
252b5132 3224{
40fb9820
L
3225 { OPERAND_TYPE_REG8, "r8" },
3226 { OPERAND_TYPE_REG16, "r16" },
3227 { OPERAND_TYPE_REG32, "r32" },
3228 { OPERAND_TYPE_REG64, "r64" },
2c703856
JB
3229 { OPERAND_TYPE_ACC8, "acc8" },
3230 { OPERAND_TYPE_ACC16, "acc16" },
3231 { OPERAND_TYPE_ACC32, "acc32" },
3232 { OPERAND_TYPE_ACC64, "acc64" },
40fb9820
L
3233 { OPERAND_TYPE_IMM8, "i8" },
3234 { OPERAND_TYPE_IMM8, "i8s" },
3235 { OPERAND_TYPE_IMM16, "i16" },
3236 { OPERAND_TYPE_IMM32, "i32" },
3237 { OPERAND_TYPE_IMM32S, "i32s" },
3238 { OPERAND_TYPE_IMM64, "i64" },
3239 { OPERAND_TYPE_IMM1, "i1" },
3240 { OPERAND_TYPE_BASEINDEX, "BaseIndex" },
3241 { OPERAND_TYPE_DISP8, "d8" },
3242 { OPERAND_TYPE_DISP16, "d16" },
3243 { OPERAND_TYPE_DISP32, "d32" },
40fb9820
L
3244 { OPERAND_TYPE_DISP64, "d64" },
3245 { OPERAND_TYPE_INOUTPORTREG, "InOutPortReg" },
3246 { OPERAND_TYPE_SHIFTCOUNT, "ShiftCount" },
3247 { OPERAND_TYPE_CONTROL, "control reg" },
3248 { OPERAND_TYPE_TEST, "test reg" },
3249 { OPERAND_TYPE_DEBUG, "debug reg" },
3250 { OPERAND_TYPE_FLOATREG, "FReg" },
3251 { OPERAND_TYPE_FLOATACC, "FAcc" },
21df382b 3252 { OPERAND_TYPE_SREG, "SReg" },
40fb9820
L
3253 { OPERAND_TYPE_REGMMX, "rMMX" },
3254 { OPERAND_TYPE_REGXMM, "rXMM" },
0349dc08 3255 { OPERAND_TYPE_REGYMM, "rYMM" },
43234a1e 3256 { OPERAND_TYPE_REGZMM, "rZMM" },
260cd341 3257 { OPERAND_TYPE_REGTMM, "rTMM" },
43234a1e 3258 { OPERAND_TYPE_REGMASK, "Mask reg" },
252b5132
RH
3259};
3260
3261static void
40fb9820 3262pt (i386_operand_type t)
252b5132 3263{
40fb9820 3264 unsigned int j;
c6fb90c8 3265 i386_operand_type a;
252b5132 3266
40fb9820 3267 for (j = 0; j < ARRAY_SIZE (type_names); j++)
c6fb90c8
L
3268 {
3269 a = operand_type_and (t, type_names[j].mask);
2c703856 3270 if (operand_type_equal (&a, &type_names[j].mask))
c6fb90c8
L
3271 fprintf (stdout, "%s, ", type_names[j].name);
3272 }
252b5132
RH
3273 fflush (stdout);
3274}
3275
3276#endif /* DEBUG386 */
3277\f
252b5132 3278static bfd_reloc_code_real_type
3956db08 3279reloc (unsigned int size,
64e74474
AM
3280 int pcrel,
3281 int sign,
3282 bfd_reloc_code_real_type other)
252b5132 3283{
47926f60 3284 if (other != NO_RELOC)
3956db08 3285 {
91d6fa6a 3286 reloc_howto_type *rel;
3956db08
JB
3287
3288 if (size == 8)
3289 switch (other)
3290 {
64e74474
AM
3291 case BFD_RELOC_X86_64_GOT32:
3292 return BFD_RELOC_X86_64_GOT64;
3293 break;
553d1284
L
3294 case BFD_RELOC_X86_64_GOTPLT64:
3295 return BFD_RELOC_X86_64_GOTPLT64;
3296 break;
64e74474
AM
3297 case BFD_RELOC_X86_64_PLTOFF64:
3298 return BFD_RELOC_X86_64_PLTOFF64;
3299 break;
3300 case BFD_RELOC_X86_64_GOTPC32:
3301 other = BFD_RELOC_X86_64_GOTPC64;
3302 break;
3303 case BFD_RELOC_X86_64_GOTPCREL:
3304 other = BFD_RELOC_X86_64_GOTPCREL64;
3305 break;
3306 case BFD_RELOC_X86_64_TPOFF32:
3307 other = BFD_RELOC_X86_64_TPOFF64;
3308 break;
3309 case BFD_RELOC_X86_64_DTPOFF32:
3310 other = BFD_RELOC_X86_64_DTPOFF64;
3311 break;
3312 default:
3313 break;
3956db08 3314 }
e05278af 3315
8ce3d284 3316#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
3317 if (other == BFD_RELOC_SIZE32)
3318 {
3319 if (size == 8)
1ab668bf 3320 other = BFD_RELOC_SIZE64;
8fd4256d 3321 if (pcrel)
1ab668bf
AM
3322 {
3323 as_bad (_("there are no pc-relative size relocations"));
3324 return NO_RELOC;
3325 }
8fd4256d 3326 }
8ce3d284 3327#endif
8fd4256d 3328
e05278af 3329 /* Sign-checking 4-byte relocations in 16-/32-bit code is pointless. */
f2d8a97c 3330 if (size == 4 && (flag_code != CODE_64BIT || disallow_64bit_reloc))
e05278af
JB
3331 sign = -1;
3332
91d6fa6a
NC
3333 rel = bfd_reloc_type_lookup (stdoutput, other);
3334 if (!rel)
3956db08 3335 as_bad (_("unknown relocation (%u)"), other);
91d6fa6a 3336 else if (size != bfd_get_reloc_size (rel))
3956db08 3337 as_bad (_("%u-byte relocation cannot be applied to %u-byte field"),
91d6fa6a 3338 bfd_get_reloc_size (rel),
3956db08 3339 size);
91d6fa6a 3340 else if (pcrel && !rel->pc_relative)
3956db08 3341 as_bad (_("non-pc-relative relocation for pc-relative field"));
91d6fa6a 3342 else if ((rel->complain_on_overflow == complain_overflow_signed
3956db08 3343 && !sign)
91d6fa6a 3344 || (rel->complain_on_overflow == complain_overflow_unsigned
64e74474 3345 && sign > 0))
3956db08
JB
3346 as_bad (_("relocated field and relocation type differ in signedness"));
3347 else
3348 return other;
3349 return NO_RELOC;
3350 }
252b5132
RH
3351
3352 if (pcrel)
3353 {
3e73aa7c 3354 if (!sign)
3956db08 3355 as_bad (_("there are no unsigned pc-relative relocations"));
252b5132
RH
3356 switch (size)
3357 {
3358 case 1: return BFD_RELOC_8_PCREL;
3359 case 2: return BFD_RELOC_16_PCREL;
d258b828 3360 case 4: return BFD_RELOC_32_PCREL;
d6ab8113 3361 case 8: return BFD_RELOC_64_PCREL;
252b5132 3362 }
3956db08 3363 as_bad (_("cannot do %u byte pc-relative relocation"), size);
252b5132
RH
3364 }
3365 else
3366 {
3956db08 3367 if (sign > 0)
e5cb08ac 3368 switch (size)
3e73aa7c
JH
3369 {
3370 case 4: return BFD_RELOC_X86_64_32S;
3371 }
3372 else
3373 switch (size)
3374 {
3375 case 1: return BFD_RELOC_8;
3376 case 2: return BFD_RELOC_16;
3377 case 4: return BFD_RELOC_32;
3378 case 8: return BFD_RELOC_64;
3379 }
3956db08
JB
3380 as_bad (_("cannot do %s %u byte relocation"),
3381 sign > 0 ? "signed" : "unsigned", size);
252b5132
RH
3382 }
3383
0cc9e1d3 3384 return NO_RELOC;
252b5132
RH
3385}
3386
47926f60
KH
3387/* Here we decide which fixups can be adjusted to make them relative to
3388 the beginning of the section instead of the symbol. Basically we need
3389 to make sure that the dynamic relocations are done correctly, so in
3390 some cases we force the original symbol to be used. */
3391
252b5132 3392int
e3bb37b5 3393tc_i386_fix_adjustable (fixS *fixP ATTRIBUTE_UNUSED)
252b5132 3394{
6d249963 3395#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 3396 if (!IS_ELF)
31312f95
AM
3397 return 1;
3398
a161fe53
AM
3399 /* Don't adjust pc-relative references to merge sections in 64-bit
3400 mode. */
3401 if (use_rela_relocations
3402 && (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0
3403 && fixP->fx_pcrel)
252b5132 3404 return 0;
31312f95 3405
8d01d9a9
AJ
3406 /* The x86_64 GOTPCREL are represented as 32bit PCrel relocations
3407 and changed later by validate_fix. */
3408 if (GOT_symbol && fixP->fx_subsy == GOT_symbol
3409 && fixP->fx_r_type == BFD_RELOC_32_PCREL)
3410 return 0;
3411
8fd4256d
L
3412 /* Adjust_reloc_syms doesn't know about the GOT. Need to keep symbol
3413 for size relocations. */
3414 if (fixP->fx_r_type == BFD_RELOC_SIZE32
3415 || fixP->fx_r_type == BFD_RELOC_SIZE64
3416 || fixP->fx_r_type == BFD_RELOC_386_GOTOFF
252b5132 3417 || fixP->fx_r_type == BFD_RELOC_386_GOT32
02a86693 3418 || fixP->fx_r_type == BFD_RELOC_386_GOT32X
13ae64f3
JJ
3419 || fixP->fx_r_type == BFD_RELOC_386_TLS_GD
3420 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDM
3421 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDO_32
3422 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE_32
37e55690
JJ
3423 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE
3424 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTIE
13ae64f3
JJ
3425 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE_32
3426 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE
67a4f2b7
AO
3427 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTDESC
3428 || fixP->fx_r_type == BFD_RELOC_386_TLS_DESC_CALL
3e73aa7c 3429 || fixP->fx_r_type == BFD_RELOC_X86_64_GOT32
80b3ee89 3430 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCREL
56ceb5b5
L
3431 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCRELX
3432 || fixP->fx_r_type == BFD_RELOC_X86_64_REX_GOTPCRELX
bffbf940
JJ
3433 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSGD
3434 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSLD
3435 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF32
d6ab8113 3436 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF64
bffbf940
JJ
3437 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF
3438 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF32
d6ab8113
JB
3439 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF64
3440 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTOFF64
67a4f2b7
AO
3441 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
3442 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSDESC_CALL
252b5132
RH
3443 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3444 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3445 return 0;
31312f95 3446#endif
252b5132
RH
3447 return 1;
3448}
252b5132 3449
a9aabc23
JB
3450static INLINE bool
3451want_disp32 (const insn_template *t)
3452{
3453 return flag_code != CODE_64BIT
3454 || i.prefix[ADDR_PREFIX]
3455 || (t->base_opcode == 0x8d
3456 && t->opcode_modifier.opcodespace == SPACE_BASE
fe134c65
JB
3457 && (!i.types[1].bitfield.qword
3458 || t->opcode_modifier.size == SIZE32));
a9aabc23
JB
3459}
3460
b4cac588 3461static int
e3bb37b5 3462intel_float_operand (const char *mnemonic)
252b5132 3463{
9306ca4a
JB
3464 /* Note that the value returned is meaningful only for opcodes with (memory)
3465 operands, hence the code here is free to improperly handle opcodes that
3466 have no operands (for better performance and smaller code). */
3467
3468 if (mnemonic[0] != 'f')
3469 return 0; /* non-math */
3470
3471 switch (mnemonic[1])
3472 {
3473 /* fclex, fdecstp, fdisi, femms, feni, fincstp, finit, fsetpm, and
3474 the fs segment override prefix not currently handled because no
3475 call path can make opcodes without operands get here */
3476 case 'i':
3477 return 2 /* integer op */;
3478 case 'l':
3479 if (mnemonic[2] == 'd' && (mnemonic[3] == 'c' || mnemonic[3] == 'e'))
3480 return 3; /* fldcw/fldenv */
3481 break;
3482 case 'n':
3483 if (mnemonic[2] != 'o' /* fnop */)
3484 return 3; /* non-waiting control op */
3485 break;
3486 case 'r':
3487 if (mnemonic[2] == 's')
3488 return 3; /* frstor/frstpm */
3489 break;
3490 case 's':
3491 if (mnemonic[2] == 'a')
3492 return 3; /* fsave */
3493 if (mnemonic[2] == 't')
3494 {
3495 switch (mnemonic[3])
3496 {
3497 case 'c': /* fstcw */
3498 case 'd': /* fstdw */
3499 case 'e': /* fstenv */
3500 case 's': /* fsts[gw] */
3501 return 3;
3502 }
3503 }
3504 break;
3505 case 'x':
3506 if (mnemonic[2] == 'r' || mnemonic[2] == 's')
3507 return 0; /* fxsave/fxrstor are not really math ops */
3508 break;
3509 }
252b5132 3510
9306ca4a 3511 return 1;
252b5132
RH
3512}
3513
9a182d04
JB
3514static INLINE void
3515install_template (const insn_template *t)
3516{
3517 unsigned int l;
3518
3519 i.tm = *t;
3520
3521 /* Note that for pseudo prefixes this produces a length of 1. But for them
3522 the length isn't interesting at all. */
3523 for (l = 1; l < 4; ++l)
3524 if (!(t->base_opcode >> (8 * l)))
3525 break;
3526
3527 i.opcode_length = l;
3528}
3529
c0f3af97
L
3530/* Build the VEX prefix. */
3531
3532static void
d3ce72d0 3533build_vex_prefix (const insn_template *t)
c0f3af97
L
3534{
3535 unsigned int register_specifier;
c0f3af97 3536 unsigned int vector_length;
03751133 3537 unsigned int w;
c0f3af97
L
3538
3539 /* Check register specifier. */
3540 if (i.vex.register_specifier)
43234a1e
L
3541 {
3542 register_specifier =
3543 ~register_number (i.vex.register_specifier) & 0xf;
3544 gas_assert ((i.vex.register_specifier->reg_flags & RegVRex) == 0);
3545 }
c0f3af97
L
3546 else
3547 register_specifier = 0xf;
3548
79f0fa25
L
3549 /* Use 2-byte VEX prefix by swapping destination and source operand
3550 if there are more than 1 register operand. */
3551 if (i.reg_operands > 1
3552 && i.vec_encoding != vex_encoding_vex3
86fa6981 3553 && i.dir_encoding == dir_encoding_default
fa99fab2 3554 && i.operands == i.reg_operands
dbbc8b7e 3555 && operand_type_equal (&i.types[0], &i.types[i.operands - 1])
441f6aca 3556 && i.tm.opcode_modifier.opcodespace == SPACE_0F
dbbc8b7e 3557 && (i.tm.opcode_modifier.load || i.tm.opcode_modifier.d)
fa99fab2
L
3558 && i.rex == REX_B)
3559 {
3560 unsigned int xchg = i.operands - 1;
3561 union i386_op temp_op;
3562 i386_operand_type temp_type;
3563
3564 temp_type = i.types[xchg];
3565 i.types[xchg] = i.types[0];
3566 i.types[0] = temp_type;
3567 temp_op = i.op[xchg];
3568 i.op[xchg] = i.op[0];
3569 i.op[0] = temp_op;
3570
9c2799c2 3571 gas_assert (i.rm.mode == 3);
fa99fab2
L
3572
3573 i.rex = REX_R;
3574 xchg = i.rm.regmem;
3575 i.rm.regmem = i.rm.reg;
3576 i.rm.reg = xchg;
3577
dbbc8b7e
JB
3578 if (i.tm.opcode_modifier.d)
3579 i.tm.base_opcode ^= (i.tm.base_opcode & 0xee) != 0x6e
2c735193 3580 ? Opcode_ExtD : Opcode_SIMD_IntD;
dbbc8b7e 3581 else /* Use the next insn. */
9a182d04 3582 install_template (&t[1]);
fa99fab2
L
3583 }
3584
79dec6b7
JB
3585 /* Use 2-byte VEX prefix by swapping commutative source operands if there
3586 are no memory operands and at least 3 register ones. */
3587 if (i.reg_operands >= 3
3588 && i.vec_encoding != vex_encoding_vex3
3589 && i.reg_operands == i.operands - i.imm_operands
3590 && i.tm.opcode_modifier.vex
3591 && i.tm.opcode_modifier.commutative
3592 && (i.tm.opcode_modifier.sse2avx || optimize > 1)
3593 && i.rex == REX_B
3594 && i.vex.register_specifier
3595 && !(i.vex.register_specifier->reg_flags & RegRex))
3596 {
3597 unsigned int xchg = i.operands - i.reg_operands;
3598 union i386_op temp_op;
3599 i386_operand_type temp_type;
3600
441f6aca 3601 gas_assert (i.tm.opcode_modifier.opcodespace == SPACE_0F);
79dec6b7
JB
3602 gas_assert (!i.tm.opcode_modifier.sae);
3603 gas_assert (operand_type_equal (&i.types[i.operands - 2],
3604 &i.types[i.operands - 3]));
3605 gas_assert (i.rm.mode == 3);
3606
3607 temp_type = i.types[xchg];
3608 i.types[xchg] = i.types[xchg + 1];
3609 i.types[xchg + 1] = temp_type;
3610 temp_op = i.op[xchg];
3611 i.op[xchg] = i.op[xchg + 1];
3612 i.op[xchg + 1] = temp_op;
3613
3614 i.rex = 0;
3615 xchg = i.rm.regmem | 8;
3616 i.rm.regmem = ~register_specifier & 0xf;
3617 gas_assert (!(i.rm.regmem & 8));
3618 i.vex.register_specifier += xchg - i.rm.regmem;
3619 register_specifier = ~xchg & 0xf;
3620 }
3621
539f890d
L
3622 if (i.tm.opcode_modifier.vex == VEXScalar)
3623 vector_length = avxscalar;
10c17abd
JB
3624 else if (i.tm.opcode_modifier.vex == VEX256)
3625 vector_length = 1;
539f890d 3626 else
10c17abd 3627 {
56522fc5 3628 unsigned int op;
10c17abd 3629
c7213af9
L
3630 /* Determine vector length from the last multi-length vector
3631 operand. */
10c17abd 3632 vector_length = 0;
56522fc5 3633 for (op = t->operands; op--;)
10c17abd
JB
3634 if (t->operand_types[op].bitfield.xmmword
3635 && t->operand_types[op].bitfield.ymmword
3636 && i.types[op].bitfield.ymmword)
3637 {
3638 vector_length = 1;
3639 break;
3640 }
3641 }
c0f3af97 3642
03751133
L
3643 /* Check the REX.W bit and VEXW. */
3644 if (i.tm.opcode_modifier.vexw == VEXWIG)
3645 w = (vexwig == vexw1 || (i.rex & REX_W)) ? 1 : 0;
3646 else if (i.tm.opcode_modifier.vexw)
3647 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3648 else
931d03b7 3649 w = (flag_code == CODE_64BIT ? i.rex & REX_W : vexwig == vexw1) ? 1 : 0;
03751133 3650
c0f3af97 3651 /* Use 2-byte VEX prefix if possible. */
03751133
L
3652 if (w == 0
3653 && i.vec_encoding != vex_encoding_vex3
441f6aca 3654 && i.tm.opcode_modifier.opcodespace == SPACE_0F
c0f3af97
L
3655 && (i.rex & (REX_W | REX_X | REX_B)) == 0)
3656 {
3657 /* 2-byte VEX prefix. */
3658 unsigned int r;
3659
3660 i.vex.length = 2;
3661 i.vex.bytes[0] = 0xc5;
3662
3663 /* Check the REX.R bit. */
3664 r = (i.rex & REX_R) ? 0 : 1;
3665 i.vex.bytes[1] = (r << 7
3666 | register_specifier << 3
3667 | vector_length << 2
35648716 3668 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3669 }
3670 else
3671 {
3672 /* 3-byte VEX prefix. */
f88c9eb0 3673 i.vex.length = 3;
f88c9eb0 3674
441f6aca 3675 switch (i.tm.opcode_modifier.opcodespace)
5dd85c99 3676 {
441f6aca
JB
3677 case SPACE_0F:
3678 case SPACE_0F38:
3679 case SPACE_0F3A:
80de6e00 3680 i.vex.bytes[0] = 0xc4;
7f399153 3681 break;
441f6aca
JB
3682 case SPACE_XOP08:
3683 case SPACE_XOP09:
3684 case SPACE_XOP0A:
f88c9eb0 3685 i.vex.bytes[0] = 0x8f;
7f399153
L
3686 break;
3687 default:
3688 abort ();
f88c9eb0 3689 }
c0f3af97 3690
c0f3af97
L
3691 /* The high 3 bits of the second VEX byte are 1's compliment
3692 of RXB bits from REX. */
441f6aca 3693 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | i.tm.opcode_modifier.opcodespace;
c0f3af97 3694
c0f3af97
L
3695 i.vex.bytes[2] = (w << 7
3696 | register_specifier << 3
3697 | vector_length << 2
35648716 3698 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3699 }
3700}
3701
5b7c81bd 3702static INLINE bool
e771e7c9
JB
3703is_evex_encoding (const insn_template *t)
3704{
7091c612 3705 return t->opcode_modifier.evex || t->opcode_modifier.disp8memshift
e771e7c9 3706 || t->opcode_modifier.broadcast || t->opcode_modifier.masking
a80195f1 3707 || t->opcode_modifier.sae;
e771e7c9
JB
3708}
3709
5b7c81bd 3710static INLINE bool
7a8655d2
JB
3711is_any_vex_encoding (const insn_template *t)
3712{
7b47a312 3713 return t->opcode_modifier.vex || is_evex_encoding (t);
7a8655d2
JB
3714}
3715
a5748e0d
JB
3716static unsigned int
3717get_broadcast_bytes (const insn_template *t, bool diag)
3718{
3719 unsigned int op, bytes;
3720 const i386_operand_type *types;
3721
3722 if (i.broadcast.type)
3723 return i.broadcast.bytes = ((1 << (t->opcode_modifier.broadcast - 1))
3724 * i.broadcast.type);
3725
3726 gas_assert (intel_syntax);
3727
3728 for (op = 0; op < t->operands; ++op)
3729 if (t->operand_types[op].bitfield.baseindex)
3730 break;
3731
3732 gas_assert (op < t->operands);
3733
3734 if (t->opcode_modifier.evex
3735 && t->opcode_modifier.evex != EVEXDYN)
3736 switch (i.broadcast.bytes)
3737 {
3738 case 1:
3739 if (t->operand_types[op].bitfield.word)
3740 return 2;
3741 /* Fall through. */
3742 case 2:
3743 if (t->operand_types[op].bitfield.dword)
3744 return 4;
3745 /* Fall through. */
3746 case 4:
3747 if (t->operand_types[op].bitfield.qword)
3748 return 8;
3749 /* Fall through. */
3750 case 8:
3751 if (t->operand_types[op].bitfield.xmmword)
3752 return 16;
3753 if (t->operand_types[op].bitfield.ymmword)
3754 return 32;
3755 if (t->operand_types[op].bitfield.zmmword)
3756 return 64;
3757 /* Fall through. */
3758 default:
3759 abort ();
3760 }
3761
3762 gas_assert (op + 1 < t->operands);
3763
3764 if (t->operand_types[op + 1].bitfield.xmmword
3765 + t->operand_types[op + 1].bitfield.ymmword
3766 + t->operand_types[op + 1].bitfield.zmmword > 1)
3767 {
3768 types = &i.types[op + 1];
3769 diag = false;
3770 }
3771 else /* Ambiguous - guess with a preference to non-AVX512VL forms. */
3772 types = &t->operand_types[op];
3773
3774 if (types->bitfield.zmmword)
3775 bytes = 64;
3776 else if (types->bitfield.ymmword)
3777 bytes = 32;
3778 else
3779 bytes = 16;
3780
3781 if (diag)
3782 as_warn (_("ambiguous broadcast for `%s', using %u-bit form"),
3783 t->name, bytes * 8);
3784
3785 return bytes;
3786}
3787
43234a1e
L
3788/* Build the EVEX prefix. */
3789
3790static void
3791build_evex_prefix (void)
3792{
35648716 3793 unsigned int register_specifier, w;
43234a1e
L
3794 rex_byte vrex_used = 0;
3795
3796 /* Check register specifier. */
3797 if (i.vex.register_specifier)
3798 {
3799 gas_assert ((i.vrex & REX_X) == 0);
3800
3801 register_specifier = i.vex.register_specifier->reg_num;
3802 if ((i.vex.register_specifier->reg_flags & RegRex))
3803 register_specifier += 8;
3804 /* The upper 16 registers are encoded in the fourth byte of the
3805 EVEX prefix. */
3806 if (!(i.vex.register_specifier->reg_flags & RegVRex))
3807 i.vex.bytes[3] = 0x8;
3808 register_specifier = ~register_specifier & 0xf;
3809 }
3810 else
3811 {
3812 register_specifier = 0xf;
3813
3814 /* Encode upper 16 vector index register in the fourth byte of
3815 the EVEX prefix. */
3816 if (!(i.vrex & REX_X))
3817 i.vex.bytes[3] = 0x8;
3818 else
3819 vrex_used |= REX_X;
3820 }
3821
43234a1e
L
3822 /* 4 byte EVEX prefix. */
3823 i.vex.length = 4;
3824 i.vex.bytes[0] = 0x62;
3825
43234a1e
L
3826 /* The high 3 bits of the second EVEX byte are 1's compliment of RXB
3827 bits from REX. */
441f6aca 3828 gas_assert (i.tm.opcode_modifier.opcodespace >= SPACE_0F);
0cc78721 3829 gas_assert (i.tm.opcode_modifier.opcodespace <= SPACE_EVEXMAP6);
441f6aca 3830 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | i.tm.opcode_modifier.opcodespace;
43234a1e
L
3831
3832 /* The fifth bit of the second EVEX byte is 1's compliment of the
3833 REX_R bit in VREX. */
3834 if (!(i.vrex & REX_R))
3835 i.vex.bytes[1] |= 0x10;
3836 else
3837 vrex_used |= REX_R;
3838
3839 if ((i.reg_operands + i.imm_operands) == i.operands)
3840 {
3841 /* When all operands are registers, the REX_X bit in REX is not
3842 used. We reuse it to encode the upper 16 registers, which is
3843 indicated by the REX_B bit in VREX. The REX_X bit is encoded
3844 as 1's compliment. */
3845 if ((i.vrex & REX_B))
3846 {
3847 vrex_used |= REX_B;
3848 i.vex.bytes[1] &= ~0x40;
3849 }
3850 }
3851
3852 /* EVEX instructions shouldn't need the REX prefix. */
3853 i.vrex &= ~vrex_used;
3854 gas_assert (i.vrex == 0);
3855
6865c043
L
3856 /* Check the REX.W bit and VEXW. */
3857 if (i.tm.opcode_modifier.vexw == VEXWIG)
3858 w = (evexwig == evexw1 || (i.rex & REX_W)) ? 1 : 0;
3859 else if (i.tm.opcode_modifier.vexw)
3860 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3861 else
931d03b7 3862 w = (flag_code == CODE_64BIT ? i.rex & REX_W : evexwig == evexw1) ? 1 : 0;
43234a1e 3863
43234a1e 3864 /* The third byte of the EVEX prefix. */
35648716
JB
3865 i.vex.bytes[2] = ((w << 7)
3866 | (register_specifier << 3)
3867 | 4 /* Encode the U bit. */
3868 | i.tm.opcode_modifier.opcodeprefix);
43234a1e
L
3869
3870 /* The fourth byte of the EVEX prefix. */
3871 /* The zeroing-masking bit. */
6225c532 3872 if (i.mask.reg && i.mask.zeroing)
43234a1e
L
3873 i.vex.bytes[3] |= 0x80;
3874
3875 /* Don't always set the broadcast bit if there is no RC. */
ca5312a2 3876 if (i.rounding.type == rc_none)
43234a1e
L
3877 {
3878 /* Encode the vector length. */
3879 unsigned int vec_length;
3880
e771e7c9
JB
3881 if (!i.tm.opcode_modifier.evex
3882 || i.tm.opcode_modifier.evex == EVEXDYN)
3883 {
56522fc5 3884 unsigned int op;
e771e7c9 3885
c7213af9
L
3886 /* Determine vector length from the last multi-length vector
3887 operand. */
56522fc5 3888 for (op = i.operands; op--;)
e771e7c9
JB
3889 if (i.tm.operand_types[op].bitfield.xmmword
3890 + i.tm.operand_types[op].bitfield.ymmword
3891 + i.tm.operand_types[op].bitfield.zmmword > 1)
3892 {
3893 if (i.types[op].bitfield.zmmword)
c7213af9
L
3894 {
3895 i.tm.opcode_modifier.evex = EVEX512;
3896 break;
3897 }
e771e7c9 3898 else if (i.types[op].bitfield.ymmword)
c7213af9
L
3899 {
3900 i.tm.opcode_modifier.evex = EVEX256;
3901 break;
3902 }
e771e7c9 3903 else if (i.types[op].bitfield.xmmword)
c7213af9
L
3904 {
3905 i.tm.opcode_modifier.evex = EVEX128;
3906 break;
3907 }
a5748e0d 3908 else if (i.broadcast.bytes && op == i.broadcast.operand)
625cbd7a 3909 {
a5748e0d 3910 switch (get_broadcast_bytes (&i.tm, true))
625cbd7a
JB
3911 {
3912 case 64:
3913 i.tm.opcode_modifier.evex = EVEX512;
3914 break;
3915 case 32:
3916 i.tm.opcode_modifier.evex = EVEX256;
3917 break;
3918 case 16:
3919 i.tm.opcode_modifier.evex = EVEX128;
3920 break;
3921 default:
c7213af9 3922 abort ();
625cbd7a 3923 }
c7213af9 3924 break;
625cbd7a 3925 }
e771e7c9 3926 }
c7213af9 3927
56522fc5 3928 if (op >= MAX_OPERANDS)
c7213af9 3929 abort ();
e771e7c9
JB
3930 }
3931
43234a1e
L
3932 switch (i.tm.opcode_modifier.evex)
3933 {
3934 case EVEXLIG: /* LL' is ignored */
3935 vec_length = evexlig << 5;
3936 break;
3937 case EVEX128:
3938 vec_length = 0 << 5;
3939 break;
3940 case EVEX256:
3941 vec_length = 1 << 5;
3942 break;
3943 case EVEX512:
3944 vec_length = 2 << 5;
3945 break;
3946 default:
3947 abort ();
3948 break;
3949 }
3950 i.vex.bytes[3] |= vec_length;
3951 /* Encode the broadcast bit. */
a5748e0d 3952 if (i.broadcast.bytes)
43234a1e
L
3953 i.vex.bytes[3] |= 0x10;
3954 }
ca5312a2
JB
3955 else if (i.rounding.type != saeonly)
3956 i.vex.bytes[3] |= 0x10 | (i.rounding.type << 5);
43234a1e 3957 else
ca5312a2 3958 i.vex.bytes[3] |= 0x10 | (evexrcig << 5);
43234a1e 3959
6225c532
JB
3960 if (i.mask.reg)
3961 i.vex.bytes[3] |= i.mask.reg->reg_num;
43234a1e
L
3962}
3963
65da13b5
L
3964static void
3965process_immext (void)
3966{
3967 expressionS *exp;
3968
c0f3af97 3969 /* These AMD 3DNow! and SSE2 instructions have an opcode suffix
65da13b5
L
3970 which is coded in the same place as an 8-bit immediate field
3971 would be. Here we fake an 8-bit immediate operand from the
3972 opcode suffix stored in tm.extension_opcode.
3973
c1e679ec 3974 AVX instructions also use this encoding, for some of
c0f3af97 3975 3 argument instructions. */
65da13b5 3976
43234a1e 3977 gas_assert (i.imm_operands <= 1
7ab9ffdd 3978 && (i.operands <= 2
7a8655d2 3979 || (is_any_vex_encoding (&i.tm)
7ab9ffdd 3980 && i.operands <= 4)));
65da13b5
L
3981
3982 exp = &im_expressions[i.imm_operands++];
3983 i.op[i.operands].imms = exp;
3984 i.types[i.operands] = imm8;
3985 i.operands++;
3986 exp->X_op = O_constant;
3987 exp->X_add_number = i.tm.extension_opcode;
3988 i.tm.extension_opcode = None;
3989}
3990
42164a71
L
3991
3992static int
3993check_hle (void)
3994{
742732c7 3995 switch (i.tm.opcode_modifier.prefixok)
42164a71
L
3996 {
3997 default:
3998 abort ();
742732c7
JB
3999 case PrefixLock:
4000 case PrefixNone:
4001 case PrefixNoTrack:
4002 case PrefixRep:
165de32a
L
4003 as_bad (_("invalid instruction `%s' after `%s'"),
4004 i.tm.name, i.hle_prefix);
42164a71 4005 return 0;
742732c7 4006 case PrefixHLELock:
42164a71
L
4007 if (i.prefix[LOCK_PREFIX])
4008 return 1;
165de32a 4009 as_bad (_("missing `lock' with `%s'"), i.hle_prefix);
42164a71 4010 return 0;
742732c7 4011 case PrefixHLEAny:
42164a71 4012 return 1;
742732c7 4013 case PrefixHLERelease:
42164a71
L
4014 if (i.prefix[HLE_PREFIX] != XRELEASE_PREFIX_OPCODE)
4015 {
4016 as_bad (_("instruction `%s' after `xacquire' not allowed"),
4017 i.tm.name);
4018 return 0;
4019 }
8dc0818e 4020 if (i.mem_operands == 0 || !(i.flags[i.operands - 1] & Operand_Mem))
42164a71
L
4021 {
4022 as_bad (_("memory destination needed for instruction `%s'"
4023 " after `xrelease'"), i.tm.name);
4024 return 0;
4025 }
4026 return 1;
4027 }
4028}
4029
c8480b58
L
4030/* Encode aligned vector move as unaligned vector move. */
4031
4032static void
4033encode_with_unaligned_vector_move (void)
4034{
4035 switch (i.tm.base_opcode)
4036 {
b3a9fe6f
L
4037 case 0x28: /* Load instructions. */
4038 case 0x29: /* Store instructions. */
c8480b58
L
4039 /* movaps/movapd/vmovaps/vmovapd. */
4040 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4041 && i.tm.opcode_modifier.opcodeprefix <= PREFIX_0X66)
b3a9fe6f 4042 i.tm.base_opcode = 0x10 | (i.tm.base_opcode & 1);
c8480b58 4043 break;
b3a9fe6f
L
4044 case 0x6f: /* Load instructions. */
4045 case 0x7f: /* Store instructions. */
c8480b58
L
4046 /* movdqa/vmovdqa/vmovdqa64/vmovdqa32. */
4047 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4048 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0X66)
4049 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
4050 break;
4051 default:
4052 break;
4053 }
4054}
4055
b6f8c7c4
L
4056/* Try the shortest encoding by shortening operand size. */
4057
4058static void
4059optimize_encoding (void)
4060{
a0a1771e 4061 unsigned int j;
b6f8c7c4 4062
fe134c65
JB
4063 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
4064 && i.tm.base_opcode == 0x8d)
4065 {
4066 /* Optimize: -O:
4067 lea symbol, %rN -> mov $symbol, %rN
4068 lea (%rM), %rN -> mov %rM, %rN
4069 lea (,%rM,1), %rN -> mov %rM, %rN
4070
4071 and in 32-bit mode for 16-bit addressing
4072
4073 lea (%rM), %rN -> movzx %rM, %rN
4074
4075 and in 64-bit mode zap 32-bit addressing in favor of using a
4076 32-bit (or less) destination.
4077 */
4078 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
4079 {
4080 if (!i.op[1].regs->reg_type.bitfield.word)
4081 i.tm.opcode_modifier.size = SIZE32;
4082 i.prefix[ADDR_PREFIX] = 0;
4083 }
4084
4085 if (!i.index_reg && !i.base_reg)
4086 {
4087 /* Handle:
4088 lea symbol, %rN -> mov $symbol, %rN
4089 */
4090 if (flag_code == CODE_64BIT)
4091 {
4092 /* Don't transform a relocation to a 16-bit one. */
4093 if (i.op[0].disps
4094 && i.op[0].disps->X_op != O_constant
4095 && i.op[1].regs->reg_type.bitfield.word)
4096 return;
4097
4098 if (!i.op[1].regs->reg_type.bitfield.qword
4099 || i.tm.opcode_modifier.size == SIZE32)
4100 {
4101 i.tm.base_opcode = 0xb8;
4102 i.tm.opcode_modifier.modrm = 0;
4103 if (!i.op[1].regs->reg_type.bitfield.word)
4104 i.types[0].bitfield.imm32 = 1;
4105 else
4106 {
4107 i.tm.opcode_modifier.size = SIZE16;
4108 i.types[0].bitfield.imm16 = 1;
4109 }
4110 }
4111 else
4112 {
4113 /* Subject to further optimization below. */
4114 i.tm.base_opcode = 0xc7;
4115 i.tm.extension_opcode = 0;
4116 i.types[0].bitfield.imm32s = 1;
4117 i.types[0].bitfield.baseindex = 0;
4118 }
4119 }
4120 /* Outside of 64-bit mode address and operand sizes have to match if
4121 a relocation is involved, as otherwise we wouldn't (currently) or
4122 even couldn't express the relocation correctly. */
4123 else if (i.op[0].disps
4124 && i.op[0].disps->X_op != O_constant
4125 && ((!i.prefix[ADDR_PREFIX])
4126 != (flag_code == CODE_32BIT
4127 ? i.op[1].regs->reg_type.bitfield.dword
4128 : i.op[1].regs->reg_type.bitfield.word)))
4129 return;
7772f168
JB
4130 /* In 16-bit mode converting LEA with 16-bit addressing and a 32-bit
4131 destination is going to grow encoding size. */
4132 else if (flag_code == CODE_16BIT
4133 && (optimize <= 1 || optimize_for_space)
4134 && !i.prefix[ADDR_PREFIX]
4135 && i.op[1].regs->reg_type.bitfield.dword)
4136 return;
fe134c65
JB
4137 else
4138 {
4139 i.tm.base_opcode = 0xb8;
4140 i.tm.opcode_modifier.modrm = 0;
4141 if (i.op[1].regs->reg_type.bitfield.dword)
4142 i.types[0].bitfield.imm32 = 1;
4143 else
4144 i.types[0].bitfield.imm16 = 1;
4145
4146 if (i.op[0].disps
4147 && i.op[0].disps->X_op == O_constant
4148 && i.op[1].regs->reg_type.bitfield.dword
60cfa10c
L
4149 /* NB: Add () to !i.prefix[ADDR_PREFIX] to silence
4150 GCC 5. */
4151 && (!i.prefix[ADDR_PREFIX]) != (flag_code == CODE_32BIT))
fe134c65
JB
4152 i.op[0].disps->X_add_number &= 0xffff;
4153 }
4154
4155 i.tm.operand_types[0] = i.types[0];
4156 i.imm_operands = 1;
4157 if (!i.op[0].imms)
4158 {
4159 i.op[0].imms = &im_expressions[0];
4160 i.op[0].imms->X_op = O_absent;
4161 }
4162 }
4163 else if (i.op[0].disps
4164 && (i.op[0].disps->X_op != O_constant
4165 || i.op[0].disps->X_add_number))
4166 return;
4167 else
4168 {
4169 /* Handle:
4170 lea (%rM), %rN -> mov %rM, %rN
4171 lea (,%rM,1), %rN -> mov %rM, %rN
4172 lea (%rM), %rN -> movzx %rM, %rN
4173 */
4174 const reg_entry *addr_reg;
4175
4176 if (!i.index_reg && i.base_reg->reg_num != RegIP)
4177 addr_reg = i.base_reg;
4178 else if (!i.base_reg
4179 && i.index_reg->reg_num != RegIZ
4180 && !i.log2_scale_factor)
4181 addr_reg = i.index_reg;
4182 else
4183 return;
4184
4185 if (addr_reg->reg_type.bitfield.word
4186 && i.op[1].regs->reg_type.bitfield.dword)
4187 {
4188 if (flag_code != CODE_32BIT)
4189 return;
4190 i.tm.opcode_modifier.opcodespace = SPACE_0F;
4191 i.tm.base_opcode = 0xb7;
4192 }
4193 else
4194 i.tm.base_opcode = 0x8b;
4195
4196 if (addr_reg->reg_type.bitfield.dword
4197 && i.op[1].regs->reg_type.bitfield.qword)
4198 i.tm.opcode_modifier.size = SIZE32;
4199
4200 i.op[0].regs = addr_reg;
4201 i.reg_operands = 2;
4202 }
4203
4204 i.mem_operands = 0;
4205 i.disp_operands = 0;
4206 i.prefix[ADDR_PREFIX] = 0;
4207 i.prefix[SEG_PREFIX] = 0;
4208 i.seg[0] = NULL;
4209 }
4210
b6f8c7c4 4211 if (optimize_for_space
389d00a5 4212 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
b6f8c7c4
L
4213 && i.reg_operands == 1
4214 && i.imm_operands == 1
4215 && !i.types[1].bitfield.byte
4216 && i.op[0].imms->X_op == O_constant
4217 && fits_in_imm7 (i.op[0].imms->X_add_number)
72aea328 4218 && (i.tm.base_opcode == 0xa8
b6f8c7c4
L
4219 || (i.tm.base_opcode == 0xf6
4220 && i.tm.extension_opcode == 0x0)))
4221 {
4222 /* Optimize: -Os:
4223 test $imm7, %r64/%r32/%r16 -> test $imm7, %r8
4224 */
4225 unsigned int base_regnum = i.op[1].regs->reg_num;
4226 if (flag_code == CODE_64BIT || base_regnum < 4)
4227 {
4228 i.types[1].bitfield.byte = 1;
4229 /* Ignore the suffix. */
4230 i.suffix = 0;
7697afb6
JB
4231 /* Convert to byte registers. */
4232 if (i.types[1].bitfield.word)
4233 j = 16;
4234 else if (i.types[1].bitfield.dword)
4235 j = 32;
4236 else
4237 j = 48;
4238 if (!(i.op[1].regs->reg_flags & RegRex) && base_regnum < 4)
4239 j += 8;
4240 i.op[1].regs -= j;
b6f8c7c4
L
4241 }
4242 }
4243 else if (flag_code == CODE_64BIT
389d00a5 4244 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
d3d50934
L
4245 && ((i.types[1].bitfield.qword
4246 && i.reg_operands == 1
b6f8c7c4
L
4247 && i.imm_operands == 1
4248 && i.op[0].imms->X_op == O_constant
507916b8 4249 && ((i.tm.base_opcode == 0xb8
b6f8c7c4
L
4250 && i.tm.extension_opcode == None
4251 && fits_in_unsigned_long (i.op[0].imms->X_add_number))
4252 || (fits_in_imm31 (i.op[0].imms->X_add_number)
72aea328
JB
4253 && ((i.tm.base_opcode == 0x24
4254 || i.tm.base_opcode == 0xa8)
b6f8c7c4
L
4255 || (i.tm.base_opcode == 0x80
4256 && i.tm.extension_opcode == 0x4)
4257 || ((i.tm.base_opcode == 0xf6
507916b8 4258 || (i.tm.base_opcode | 1) == 0xc7)
b8364fa7
JB
4259 && i.tm.extension_opcode == 0x0)))
4260 || (fits_in_imm7 (i.op[0].imms->X_add_number)
4261 && i.tm.base_opcode == 0x83
4262 && i.tm.extension_opcode == 0x4)))
d3d50934
L
4263 || (i.types[0].bitfield.qword
4264 && ((i.reg_operands == 2
4265 && i.op[0].regs == i.op[1].regs
72aea328
JB
4266 && (i.tm.base_opcode == 0x30
4267 || i.tm.base_opcode == 0x28))
d3d50934
L
4268 || (i.reg_operands == 1
4269 && i.operands == 1
72aea328 4270 && i.tm.base_opcode == 0x30)))))
b6f8c7c4
L
4271 {
4272 /* Optimize: -O:
4273 andq $imm31, %r64 -> andl $imm31, %r32
b8364fa7 4274 andq $imm7, %r64 -> andl $imm7, %r32
b6f8c7c4
L
4275 testq $imm31, %r64 -> testl $imm31, %r32
4276 xorq %r64, %r64 -> xorl %r32, %r32
4277 subq %r64, %r64 -> subl %r32, %r32
4278 movq $imm31, %r64 -> movl $imm31, %r32
4279 movq $imm32, %r64 -> movl $imm32, %r32
4280 */
4281 i.tm.opcode_modifier.norex64 = 1;
507916b8 4282 if (i.tm.base_opcode == 0xb8 || (i.tm.base_opcode | 1) == 0xc7)
b6f8c7c4
L
4283 {
4284 /* Handle
4285 movq $imm31, %r64 -> movl $imm31, %r32
4286 movq $imm32, %r64 -> movl $imm32, %r32
4287 */
4288 i.tm.operand_types[0].bitfield.imm32 = 1;
4289 i.tm.operand_types[0].bitfield.imm32s = 0;
4290 i.tm.operand_types[0].bitfield.imm64 = 0;
4291 i.types[0].bitfield.imm32 = 1;
4292 i.types[0].bitfield.imm32s = 0;
4293 i.types[0].bitfield.imm64 = 0;
4294 i.types[1].bitfield.dword = 1;
4295 i.types[1].bitfield.qword = 0;
507916b8 4296 if ((i.tm.base_opcode | 1) == 0xc7)
b6f8c7c4
L
4297 {
4298 /* Handle
4299 movq $imm31, %r64 -> movl $imm31, %r32
4300 */
507916b8 4301 i.tm.base_opcode = 0xb8;
b6f8c7c4 4302 i.tm.extension_opcode = None;
507916b8 4303 i.tm.opcode_modifier.w = 0;
b6f8c7c4
L
4304 i.tm.opcode_modifier.modrm = 0;
4305 }
4306 }
4307 }
5641ec01
JB
4308 else if (optimize > 1
4309 && !optimize_for_space
389d00a5 4310 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
5641ec01
JB
4311 && i.reg_operands == 2
4312 && i.op[0].regs == i.op[1].regs
4313 && ((i.tm.base_opcode & ~(Opcode_D | 1)) == 0x8
4314 || (i.tm.base_opcode & ~(Opcode_D | 1)) == 0x20)
4315 && (flag_code != CODE_64BIT || !i.types[0].bitfield.dword))
4316 {
4317 /* Optimize: -O2:
4318 andb %rN, %rN -> testb %rN, %rN
4319 andw %rN, %rN -> testw %rN, %rN
4320 andq %rN, %rN -> testq %rN, %rN
4321 orb %rN, %rN -> testb %rN, %rN
4322 orw %rN, %rN -> testw %rN, %rN
4323 orq %rN, %rN -> testq %rN, %rN
4324
4325 and outside of 64-bit mode
4326
4327 andl %rN, %rN -> testl %rN, %rN
4328 orl %rN, %rN -> testl %rN, %rN
4329 */
4330 i.tm.base_opcode = 0x84 | (i.tm.base_opcode & 1);
4331 }
99112332 4332 else if (i.reg_operands == 3
b6f8c7c4
L
4333 && i.op[0].regs == i.op[1].regs
4334 && !i.types[2].bitfield.xmmword
4335 && (i.tm.opcode_modifier.vex
6225c532 4336 || ((!i.mask.reg || i.mask.zeroing)
e771e7c9 4337 && is_evex_encoding (&i.tm)
80c34c38 4338 && (i.vec_encoding != vex_encoding_evex
dd22218c 4339 || cpu_arch_isa_flags.bitfield.cpuavx512vl
80c34c38 4340 || i.tm.cpu_flags.bitfield.cpuavx512vl
7091c612 4341 || (i.tm.operand_types[2].bitfield.zmmword
dd22218c 4342 && i.types[2].bitfield.ymmword))))
5844ccaa
JB
4343 && i.tm.opcode_modifier.opcodespace == SPACE_0F
4344 && ((i.tm.base_opcode | 2) == 0x57
4345 || i.tm.base_opcode == 0xdf
4346 || i.tm.base_opcode == 0xef
4347 || (i.tm.base_opcode | 3) == 0xfb
4348 || i.tm.base_opcode == 0x42
4349 || i.tm.base_opcode == 0x47))
b6f8c7c4 4350 {
99112332 4351 /* Optimize: -O1:
8305403a
L
4352 VOP, one of vandnps, vandnpd, vxorps, vxorpd, vpsubb, vpsubd,
4353 vpsubq and vpsubw:
b6f8c7c4
L
4354 EVEX VOP %zmmM, %zmmM, %zmmN
4355 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4356 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4357 EVEX VOP %ymmM, %ymmM, %ymmN
4358 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4359 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4360 VEX VOP %ymmM, %ymmM, %ymmN
4361 -> VEX VOP %xmmM, %xmmM, %xmmN
4362 VOP, one of vpandn and vpxor:
4363 VEX VOP %ymmM, %ymmM, %ymmN
4364 -> VEX VOP %xmmM, %xmmM, %xmmN
4365 VOP, one of vpandnd and vpandnq:
4366 EVEX VOP %zmmM, %zmmM, %zmmN
4367 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4368 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4369 EVEX VOP %ymmM, %ymmM, %ymmN
4370 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4371 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4372 VOP, one of vpxord and vpxorq:
4373 EVEX VOP %zmmM, %zmmM, %zmmN
4374 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4375 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4376 EVEX VOP %ymmM, %ymmM, %ymmN
4377 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4378 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
1424ad86
JB
4379 VOP, one of kxord and kxorq:
4380 VEX VOP %kM, %kM, %kN
4381 -> VEX kxorw %kM, %kM, %kN
4382 VOP, one of kandnd and kandnq:
4383 VEX VOP %kM, %kM, %kN
4384 -> VEX kandnw %kM, %kM, %kN
b6f8c7c4 4385 */
e771e7c9 4386 if (is_evex_encoding (&i.tm))
b6f8c7c4 4387 {
7b1d7ca1 4388 if (i.vec_encoding != vex_encoding_evex)
b6f8c7c4
L
4389 {
4390 i.tm.opcode_modifier.vex = VEX128;
4391 i.tm.opcode_modifier.vexw = VEXW0;
4392 i.tm.opcode_modifier.evex = 0;
4393 }
7b1d7ca1 4394 else if (optimize > 1)
dd22218c
L
4395 i.tm.opcode_modifier.evex = EVEX128;
4396 else
4397 return;
b6f8c7c4 4398 }
f74a6307 4399 else if (i.tm.operand_types[0].bitfield.class == RegMask)
1424ad86 4400 {
35648716 4401 i.tm.opcode_modifier.opcodeprefix = PREFIX_NONE;
1424ad86
JB
4402 i.tm.opcode_modifier.vexw = VEXW0;
4403 }
b6f8c7c4
L
4404 else
4405 i.tm.opcode_modifier.vex = VEX128;
4406
4407 if (i.tm.opcode_modifier.vex)
4408 for (j = 0; j < 3; j++)
4409 {
4410 i.types[j].bitfield.xmmword = 1;
4411 i.types[j].bitfield.ymmword = 0;
4412 }
4413 }
392a5972 4414 else if (i.vec_encoding != vex_encoding_evex
97ed31ae 4415 && !i.types[0].bitfield.zmmword
392a5972 4416 && !i.types[1].bitfield.zmmword
6225c532 4417 && !i.mask.reg
a5748e0d 4418 && !i.broadcast.bytes
97ed31ae 4419 && is_evex_encoding (&i.tm)
35648716
JB
4420 && ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4421 || (i.tm.base_opcode & ~4) == 0xdb
4422 || (i.tm.base_opcode & ~4) == 0xeb)
97ed31ae
L
4423 && i.tm.extension_opcode == None)
4424 {
4425 /* Optimize: -O1:
4426 VOP, one of vmovdqa32, vmovdqa64, vmovdqu8, vmovdqu16,
4427 vmovdqu32 and vmovdqu64:
4428 EVEX VOP %xmmM, %xmmN
4429 -> VEX vmovdqa|vmovdqu %xmmM, %xmmN (M and N < 16)
4430 EVEX VOP %ymmM, %ymmN
4431 -> VEX vmovdqa|vmovdqu %ymmM, %ymmN (M and N < 16)
4432 EVEX VOP %xmmM, mem
4433 -> VEX vmovdqa|vmovdqu %xmmM, mem (M < 16)
4434 EVEX VOP %ymmM, mem
4435 -> VEX vmovdqa|vmovdqu %ymmM, mem (M < 16)
4436 EVEX VOP mem, %xmmN
4437 -> VEX mvmovdqa|vmovdquem, %xmmN (N < 16)
4438 EVEX VOP mem, %ymmN
4439 -> VEX vmovdqa|vmovdqu mem, %ymmN (N < 16)
a0a1771e
JB
4440 VOP, one of vpand, vpandn, vpor, vpxor:
4441 EVEX VOP{d,q} %xmmL, %xmmM, %xmmN
4442 -> VEX VOP %xmmL, %xmmM, %xmmN (L, M, and N < 16)
4443 EVEX VOP{d,q} %ymmL, %ymmM, %ymmN
4444 -> VEX VOP %ymmL, %ymmM, %ymmN (L, M, and N < 16)
4445 EVEX VOP{d,q} mem, %xmmM, %xmmN
4446 -> VEX VOP mem, %xmmM, %xmmN (M and N < 16)
4447 EVEX VOP{d,q} mem, %ymmM, %ymmN
4448 -> VEX VOP mem, %ymmM, %ymmN (M and N < 16)
97ed31ae 4449 */
a0a1771e 4450 for (j = 0; j < i.operands; j++)
392a5972
L
4451 if (operand_type_check (i.types[j], disp)
4452 && i.op[j].disps->X_op == O_constant)
4453 {
4454 /* Since the VEX prefix has 2 or 3 bytes, the EVEX prefix
4455 has 4 bytes, EVEX Disp8 has 1 byte and VEX Disp32 has 4
4456 bytes, we choose EVEX Disp8 over VEX Disp32. */
4457 int evex_disp8, vex_disp8;
4458 unsigned int memshift = i.memshift;
4459 offsetT n = i.op[j].disps->X_add_number;
4460
4461 evex_disp8 = fits_in_disp8 (n);
4462 i.memshift = 0;
4463 vex_disp8 = fits_in_disp8 (n);
4464 if (evex_disp8 != vex_disp8)
4465 {
4466 i.memshift = memshift;
4467 return;
4468 }
4469
4470 i.types[j].bitfield.disp8 = vex_disp8;
4471 break;
4472 }
35648716
JB
4473 if ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4474 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2)
4475 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
97ed31ae
L
4476 i.tm.opcode_modifier.vex
4477 = i.types[0].bitfield.ymmword ? VEX256 : VEX128;
4478 i.tm.opcode_modifier.vexw = VEXW0;
79dec6b7 4479 /* VPAND, VPOR, and VPXOR are commutative. */
35648716 4480 if (i.reg_operands == 3 && i.tm.base_opcode != 0xdf)
79dec6b7 4481 i.tm.opcode_modifier.commutative = 1;
97ed31ae
L
4482 i.tm.opcode_modifier.evex = 0;
4483 i.tm.opcode_modifier.masking = 0;
a0a1771e 4484 i.tm.opcode_modifier.broadcast = 0;
97ed31ae
L
4485 i.tm.opcode_modifier.disp8memshift = 0;
4486 i.memshift = 0;
a0a1771e
JB
4487 if (j < i.operands)
4488 i.types[j].bitfield.disp8
4489 = fits_in_disp8 (i.op[j].disps->X_add_number);
97ed31ae 4490 }
b6f8c7c4
L
4491}
4492
ae531041
L
4493/* Return non-zero for load instruction. */
4494
4495static int
4496load_insn_p (void)
4497{
4498 unsigned int dest;
4499 int any_vex_p = is_any_vex_encoding (&i.tm);
4500 unsigned int base_opcode = i.tm.base_opcode | 1;
4501
4502 if (!any_vex_p)
4503 {
a09f656b 4504 /* Anysize insns: lea, invlpg, clflush, prefetchnta, prefetcht0,
4505 prefetcht1, prefetcht2, prefetchtw, bndmk, bndcl, bndcu, bndcn,
4506 bndstx, bndldx, prefetchwt1, clflushopt, clwb, cldemote. */
4507 if (i.tm.opcode_modifier.anysize)
ae531041
L
4508 return 0;
4509
389d00a5
JB
4510 /* pop. */
4511 if (strcmp (i.tm.name, "pop") == 0)
4512 return 1;
4513 }
4514
4515 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE)
4516 {
4517 /* popf, popa. */
4518 if (i.tm.base_opcode == 0x9d
a09f656b 4519 || i.tm.base_opcode == 0x61)
ae531041
L
4520 return 1;
4521
4522 /* movs, cmps, lods, scas. */
4523 if ((i.tm.base_opcode | 0xb) == 0xaf)
4524 return 1;
4525
a09f656b 4526 /* outs, xlatb. */
4527 if (base_opcode == 0x6f
4528 || i.tm.base_opcode == 0xd7)
ae531041 4529 return 1;
a09f656b 4530 /* NB: For AMD-specific insns with implicit memory operands,
4531 they're intentionally not covered. */
ae531041
L
4532 }
4533
4534 /* No memory operand. */
4535 if (!i.mem_operands)
4536 return 0;
4537
4538 if (any_vex_p)
4539 {
4540 /* vldmxcsr. */
4541 if (i.tm.base_opcode == 0xae
4542 && i.tm.opcode_modifier.vex
441f6aca 4543 && i.tm.opcode_modifier.opcodespace == SPACE_0F
35648716 4544 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
ae531041
L
4545 && i.tm.extension_opcode == 2)
4546 return 1;
4547 }
389d00a5 4548 else if (i.tm.opcode_modifier.opcodespace == SPACE_BASE)
ae531041
L
4549 {
4550 /* test, not, neg, mul, imul, div, idiv. */
4551 if ((i.tm.base_opcode == 0xf6 || i.tm.base_opcode == 0xf7)
4552 && i.tm.extension_opcode != 1)
4553 return 1;
4554
4555 /* inc, dec. */
4556 if (base_opcode == 0xff && i.tm.extension_opcode <= 1)
4557 return 1;
4558
4559 /* add, or, adc, sbb, and, sub, xor, cmp. */
4560 if (i.tm.base_opcode >= 0x80 && i.tm.base_opcode <= 0x83)
4561 return 1;
4562
ae531041
L
4563 /* rol, ror, rcl, rcr, shl/sal, shr, sar. */
4564 if ((base_opcode == 0xc1
4565 || (i.tm.base_opcode >= 0xd0 && i.tm.base_opcode <= 0xd3))
4566 && i.tm.extension_opcode != 6)
4567 return 1;
4568
ae531041 4569 /* Check for x87 instructions. */
389d00a5 4570 if (base_opcode >= 0xd8 && base_opcode <= 0xdf)
ae531041
L
4571 {
4572 /* Skip fst, fstp, fstenv, fstcw. */
4573 if (i.tm.base_opcode == 0xd9
4574 && (i.tm.extension_opcode == 2
4575 || i.tm.extension_opcode == 3
4576 || i.tm.extension_opcode == 6
4577 || i.tm.extension_opcode == 7))
4578 return 0;
4579
4580 /* Skip fisttp, fist, fistp, fstp. */
4581 if (i.tm.base_opcode == 0xdb
4582 && (i.tm.extension_opcode == 1
4583 || i.tm.extension_opcode == 2
4584 || i.tm.extension_opcode == 3
4585 || i.tm.extension_opcode == 7))
4586 return 0;
4587
4588 /* Skip fisttp, fst, fstp, fsave, fstsw. */
4589 if (i.tm.base_opcode == 0xdd
4590 && (i.tm.extension_opcode == 1
4591 || i.tm.extension_opcode == 2
4592 || i.tm.extension_opcode == 3
4593 || i.tm.extension_opcode == 6
4594 || i.tm.extension_opcode == 7))
4595 return 0;
4596
4597 /* Skip fisttp, fist, fistp, fbstp, fistp. */
4598 if (i.tm.base_opcode == 0xdf
4599 && (i.tm.extension_opcode == 1
4600 || i.tm.extension_opcode == 2
4601 || i.tm.extension_opcode == 3
4602 || i.tm.extension_opcode == 6
4603 || i.tm.extension_opcode == 7))
4604 return 0;
4605
4606 return 1;
4607 }
4608 }
389d00a5
JB
4609 else if (i.tm.opcode_modifier.opcodespace == SPACE_0F)
4610 {
4611 /* bt, bts, btr, btc. */
4612 if (i.tm.base_opcode == 0xba
4613 && (i.tm.extension_opcode >= 4 && i.tm.extension_opcode <= 7))
4614 return 1;
4615
4616 /* cmpxchg8b, cmpxchg16b, xrstors, vmptrld. */
4617 if (i.tm.base_opcode == 0xc7
4618 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
4619 && (i.tm.extension_opcode == 1 || i.tm.extension_opcode == 3
4620 || i.tm.extension_opcode == 6))
4621 return 1;
4622
4623 /* fxrstor, ldmxcsr, xrstor. */
4624 if (i.tm.base_opcode == 0xae
4625 && (i.tm.extension_opcode == 1
4626 || i.tm.extension_opcode == 2
4627 || i.tm.extension_opcode == 5))
4628 return 1;
4629
4630 /* lgdt, lidt, lmsw. */
4631 if (i.tm.base_opcode == 0x01
4632 && (i.tm.extension_opcode == 2
4633 || i.tm.extension_opcode == 3
4634 || i.tm.extension_opcode == 6))
4635 return 1;
4636 }
ae531041
L
4637
4638 dest = i.operands - 1;
4639
4640 /* Check fake imm8 operand and 3 source operands. */
4641 if ((i.tm.opcode_modifier.immext
4642 || i.tm.opcode_modifier.vexsources == VEX3SOURCES)
4643 && i.types[dest].bitfield.imm8)
4644 dest--;
4645
389d00a5
JB
4646 /* add, or, adc, sbb, and, sub, xor, cmp, test, xchg. */
4647 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
ae531041
L
4648 && (base_opcode == 0x1
4649 || base_opcode == 0x9
4650 || base_opcode == 0x11
4651 || base_opcode == 0x19
4652 || base_opcode == 0x21
4653 || base_opcode == 0x29
4654 || base_opcode == 0x31
4655 || base_opcode == 0x39
389d00a5
JB
4656 || (base_opcode | 2) == 0x87))
4657 return 1;
4658
4659 /* xadd. */
4660 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4661 && base_opcode == 0xc1)
ae531041
L
4662 return 1;
4663
4664 /* Check for load instruction. */
4665 return (i.types[dest].bitfield.class != ClassNone
4666 || i.types[dest].bitfield.instance == Accum);
4667}
4668
4669/* Output lfence, 0xfaee8, after instruction. */
4670
4671static void
4672insert_lfence_after (void)
4673{
4674 if (lfence_after_load && load_insn_p ())
4675 {
a09f656b 4676 /* There are also two REP string instructions that require
4677 special treatment. Specifically, the compare string (CMPS)
4678 and scan string (SCAS) instructions set EFLAGS in a manner
4679 that depends on the data being compared/scanned. When used
4680 with a REP prefix, the number of iterations may therefore
4681 vary depending on this data. If the data is a program secret
4682 chosen by the adversary using an LVI method,
4683 then this data-dependent behavior may leak some aspect
4684 of the secret. */
4685 if (((i.tm.base_opcode | 0x1) == 0xa7
4686 || (i.tm.base_opcode | 0x1) == 0xaf)
4687 && i.prefix[REP_PREFIX])
4688 {
4689 as_warn (_("`%s` changes flags which would affect control flow behavior"),
4690 i.tm.name);
4691 }
ae531041
L
4692 char *p = frag_more (3);
4693 *p++ = 0xf;
4694 *p++ = 0xae;
4695 *p = 0xe8;
4696 }
4697}
4698
4699/* Output lfence, 0xfaee8, before instruction. */
4700
4701static void
4702insert_lfence_before (void)
4703{
4704 char *p;
4705
389d00a5 4706 if (i.tm.opcode_modifier.opcodespace != SPACE_BASE)
ae531041
L
4707 return;
4708
4709 if (i.tm.base_opcode == 0xff
4710 && (i.tm.extension_opcode == 2 || i.tm.extension_opcode == 4))
4711 {
4712 /* Insert lfence before indirect branch if needed. */
4713
4714 if (lfence_before_indirect_branch == lfence_branch_none)
4715 return;
4716
4717 if (i.operands != 1)
4718 abort ();
4719
4720 if (i.reg_operands == 1)
4721 {
4722 /* Indirect branch via register. Don't insert lfence with
4723 -mlfence-after-load=yes. */
4724 if (lfence_after_load
4725 || lfence_before_indirect_branch == lfence_branch_memory)
4726 return;
4727 }
4728 else if (i.mem_operands == 1
4729 && lfence_before_indirect_branch != lfence_branch_register)
4730 {
4731 as_warn (_("indirect `%s` with memory operand should be avoided"),
4732 i.tm.name);
4733 return;
4734 }
4735 else
4736 return;
4737
4738 if (last_insn.kind != last_insn_other
4739 && last_insn.seg == now_seg)
4740 {
4741 as_warn_where (last_insn.file, last_insn.line,
4742 _("`%s` skips -mlfence-before-indirect-branch on `%s`"),
4743 last_insn.name, i.tm.name);
4744 return;
4745 }
4746
4747 p = frag_more (3);
4748 *p++ = 0xf;
4749 *p++ = 0xae;
4750 *p = 0xe8;
4751 return;
4752 }
4753
503648e4 4754 /* Output or/not/shl and lfence before near ret. */
ae531041
L
4755 if (lfence_before_ret != lfence_before_ret_none
4756 && (i.tm.base_opcode == 0xc2
503648e4 4757 || i.tm.base_opcode == 0xc3))
ae531041
L
4758 {
4759 if (last_insn.kind != last_insn_other
4760 && last_insn.seg == now_seg)
4761 {
4762 as_warn_where (last_insn.file, last_insn.line,
4763 _("`%s` skips -mlfence-before-ret on `%s`"),
4764 last_insn.name, i.tm.name);
4765 return;
4766 }
a09f656b 4767
a09f656b 4768 /* Near ret ingore operand size override under CPU64. */
503648e4 4769 char prefix = flag_code == CODE_64BIT
4770 ? 0x48
4771 : i.prefix[DATA_PREFIX] ? 0x66 : 0x0;
a09f656b 4772
4773 if (lfence_before_ret == lfence_before_ret_not)
4774 {
4775 /* not: 0xf71424, may add prefix
4776 for operand size override or 64-bit code. */
4777 p = frag_more ((prefix ? 2 : 0) + 6 + 3);
4778 if (prefix)
4779 *p++ = prefix;
ae531041
L
4780 *p++ = 0xf7;
4781 *p++ = 0x14;
4782 *p++ = 0x24;
a09f656b 4783 if (prefix)
4784 *p++ = prefix;
ae531041
L
4785 *p++ = 0xf7;
4786 *p++ = 0x14;
4787 *p++ = 0x24;
4788 }
a09f656b 4789 else
4790 {
4791 p = frag_more ((prefix ? 1 : 0) + 4 + 3);
4792 if (prefix)
4793 *p++ = prefix;
4794 if (lfence_before_ret == lfence_before_ret_or)
4795 {
4796 /* or: 0x830c2400, may add prefix
4797 for operand size override or 64-bit code. */
4798 *p++ = 0x83;
4799 *p++ = 0x0c;
4800 }
4801 else
4802 {
4803 /* shl: 0xc1242400, may add prefix
4804 for operand size override or 64-bit code. */
4805 *p++ = 0xc1;
4806 *p++ = 0x24;
4807 }
4808
4809 *p++ = 0x24;
4810 *p++ = 0x0;
4811 }
4812
ae531041
L
4813 *p++ = 0xf;
4814 *p++ = 0xae;
4815 *p = 0xe8;
4816 }
4817}
4818
252b5132
RH
4819/* This is the guts of the machine-dependent assembler. LINE points to a
4820 machine dependent instruction. This function is supposed to emit
4821 the frags/bytes it assembles to. */
4822
4823void
65da13b5 4824md_assemble (char *line)
252b5132 4825{
40fb9820 4826 unsigned int j;
83b16ac6 4827 char mnemonic[MAX_MNEM_SIZE], mnem_suffix;
d3ce72d0 4828 const insn_template *t;
252b5132 4829
47926f60 4830 /* Initialize globals. */
252b5132 4831 memset (&i, '\0', sizeof (i));
ca5312a2 4832 i.rounding.type = rc_none;
252b5132 4833 for (j = 0; j < MAX_OPERANDS; j++)
1ae12ab7 4834 i.reloc[j] = NO_RELOC;
252b5132
RH
4835 memset (disp_expressions, '\0', sizeof (disp_expressions));
4836 memset (im_expressions, '\0', sizeof (im_expressions));
ce8a8b2f 4837 save_stack_p = save_stack;
252b5132
RH
4838
4839 /* First parse an instruction mnemonic & call i386_operand for the operands.
4840 We assume that the scrubber has arranged it so that line[0] is the valid
47926f60 4841 start of a (possibly prefixed) mnemonic. */
252b5132 4842
29b0f896
AM
4843 line = parse_insn (line, mnemonic);
4844 if (line == NULL)
4845 return;
83b16ac6 4846 mnem_suffix = i.suffix;
252b5132 4847
29b0f896 4848 line = parse_operands (line, mnemonic);
ee86248c 4849 this_operand = -1;
8325cc63
JB
4850 xfree (i.memop1_string);
4851 i.memop1_string = NULL;
29b0f896
AM
4852 if (line == NULL)
4853 return;
252b5132 4854
29b0f896
AM
4855 /* Now we've parsed the mnemonic into a set of templates, and have the
4856 operands at hand. */
4857
b630c145 4858 /* All Intel opcodes have reversed operands except for "bound", "enter",
c0e54661
JB
4859 "invlpg*", "monitor*", "mwait*", "tpause", "umwait", "pvalidate",
4860 "rmpadjust", and "rmpupdate". We also don't reverse intersegment "jmp"
4861 and "call" instructions with 2 immediate operands so that the immediate
4862 segment precedes the offset consistently in Intel and AT&T modes. */
4d456e3d
L
4863 if (intel_syntax
4864 && i.operands > 1
29b0f896 4865 && (strcmp (mnemonic, "bound") != 0)
c0e54661 4866 && (strncmp (mnemonic, "invlpg", 6) != 0)
d34049e8
ML
4867 && !startswith (mnemonic, "monitor")
4868 && !startswith (mnemonic, "mwait")
c0e54661 4869 && (strcmp (mnemonic, "pvalidate") != 0)
d34049e8 4870 && !startswith (mnemonic, "rmp")
b630c145
JB
4871 && (strcmp (mnemonic, "tpause") != 0)
4872 && (strcmp (mnemonic, "umwait") != 0)
40fb9820
L
4873 && !(operand_type_check (i.types[0], imm)
4874 && operand_type_check (i.types[1], imm)))
29b0f896
AM
4875 swap_operands ();
4876
ec56d5c0
JB
4877 /* The order of the immediates should be reversed
4878 for 2 immediates extrq and insertq instructions */
4879 if (i.imm_operands == 2
4880 && (strcmp (mnemonic, "extrq") == 0
4881 || strcmp (mnemonic, "insertq") == 0))
4882 swap_2_operands (0, 1);
4883
29b0f896
AM
4884 if (i.imm_operands)
4885 optimize_imm ();
4886
9386188e
JB
4887 if (i.disp_operands && !want_disp32 (current_templates->start)
4888 && (!current_templates->start->opcode_modifier.jump
4889 || i.jumpabsolute || i.types[0].bitfield.baseindex))
cce08655
JB
4890 {
4891 for (j = 0; j < i.operands; ++j)
4892 {
4893 const expressionS *exp = i.op[j].disps;
4894
4895 if (!operand_type_check (i.types[j], disp))
4896 continue;
4897
4898 if (exp->X_op != O_constant)
4899 continue;
4900
4901 /* Since displacement is signed extended to 64bit, don't allow
a775efc8 4902 disp32 if it is out of range. */
cce08655
JB
4903 if (fits_in_signed_long (exp->X_add_number))
4904 continue;
4905
a775efc8 4906 i.types[j].bitfield.disp32 = 0;
cce08655
JB
4907 if (i.types[j].bitfield.baseindex)
4908 {
f493c217
AM
4909 as_bad (_("0x%" PRIx64 " out of range of signed 32bit displacement"),
4910 (uint64_t) exp->X_add_number);
cce08655
JB
4911 return;
4912 }
4913 }
4914 }
4915
b300c311
L
4916 /* Don't optimize displacement for movabs since it only takes 64bit
4917 displacement. */
4918 if (i.disp_operands
1a42a9fe 4919 && i.disp_encoding <= disp_encoding_8bit
862be3fb
L
4920 && (flag_code != CODE_64BIT
4921 || strcmp (mnemonic, "movabs") != 0))
4922 optimize_disp ();
29b0f896
AM
4923
4924 /* Next, we find a template that matches the given insn,
4925 making sure the overlap of the given operands types is consistent
4926 with the template operand types. */
252b5132 4927
83b16ac6 4928 if (!(t = match_template (mnem_suffix)))
29b0f896 4929 return;
252b5132 4930
7bab8ab5 4931 if (sse_check != check_none
ffb86450
JB
4932 /* The opcode space check isn't strictly needed; it's there only to
4933 bypass the logic below when easily possible. */
4934 && t->opcode_modifier.opcodespace >= SPACE_0F
4935 && t->opcode_modifier.opcodespace <= SPACE_0F3A
4936 && !i.tm.cpu_flags.bitfield.cpusse4a
4937 && !is_any_vex_encoding (t))
daf50ae7 4938 {
ffb86450
JB
4939 bool simd = false;
4940
4941 for (j = 0; j < t->operands; ++j)
4942 {
4943 if (t->operand_types[j].bitfield.class == RegMMX)
4944 break;
4945 if (t->operand_types[j].bitfield.class == RegSIMD)
4946 simd = true;
4947 }
4948
4949 if (j >= t->operands && simd)
4950 (sse_check == check_warning
4951 ? as_warn
4952 : as_bad) (_("SSE instruction `%s' is used"), i.tm.name);
daf50ae7
L
4953 }
4954
40fb9820 4955 if (i.tm.opcode_modifier.fwait)
29b0f896
AM
4956 if (!add_prefix (FWAIT_OPCODE))
4957 return;
252b5132 4958
d5de92cf 4959 /* Check if REP prefix is OK. */
742732c7 4960 if (i.rep_prefix && i.tm.opcode_modifier.prefixok != PrefixRep)
d5de92cf
L
4961 {
4962 as_bad (_("invalid instruction `%s' after `%s'"),
4963 i.tm.name, i.rep_prefix);
4964 return;
4965 }
4966
c1ba0266
L
4967 /* Check for lock without a lockable instruction. Destination operand
4968 must be memory unless it is xchg (0x86). */
c32fa91d 4969 if (i.prefix[LOCK_PREFIX]
742732c7 4970 && (i.tm.opcode_modifier.prefixok < PrefixLock
c1ba0266
L
4971 || i.mem_operands == 0
4972 || (i.tm.base_opcode != 0x86
8dc0818e 4973 && !(i.flags[i.operands - 1] & Operand_Mem))))
c32fa91d
L
4974 {
4975 as_bad (_("expecting lockable instruction after `lock'"));
4976 return;
4977 }
4978
40d231b4
JB
4979 /* Check for data size prefix on VEX/XOP/EVEX encoded and SIMD insns. */
4980 if (i.prefix[DATA_PREFIX]
4981 && (is_any_vex_encoding (&i.tm)
4982 || i.tm.operand_types[i.imm_operands].bitfield.class >= RegMMX
4983 || i.tm.operand_types[i.imm_operands + 1].bitfield.class >= RegMMX))
7a8655d2
JB
4984 {
4985 as_bad (_("data size prefix invalid with `%s'"), i.tm.name);
4986 return;
4987 }
4988
42164a71 4989 /* Check if HLE prefix is OK. */
165de32a 4990 if (i.hle_prefix && !check_hle ())
42164a71
L
4991 return;
4992
7e8b059b
L
4993 /* Check BND prefix. */
4994 if (i.bnd_prefix && !i.tm.opcode_modifier.bndprefixok)
4995 as_bad (_("expecting valid branch instruction after `bnd'"));
4996
04ef582a 4997 /* Check NOTRACK prefix. */
742732c7 4998 if (i.notrack_prefix && i.tm.opcode_modifier.prefixok != PrefixNoTrack)
9fef80d6 4999 as_bad (_("expecting indirect branch instruction after `notrack'"));
04ef582a 5000
327e8c42
JB
5001 if (i.tm.cpu_flags.bitfield.cpumpx)
5002 {
5003 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
5004 as_bad (_("32-bit address isn't allowed in 64-bit MPX instructions."));
5005 else if (flag_code != CODE_16BIT
5006 ? i.prefix[ADDR_PREFIX]
5007 : i.mem_operands && !i.prefix[ADDR_PREFIX])
5008 as_bad (_("16-bit address isn't allowed in MPX instructions"));
5009 }
7e8b059b
L
5010
5011 /* Insert BND prefix. */
76d3a78a
JB
5012 if (add_bnd_prefix && i.tm.opcode_modifier.bndprefixok)
5013 {
5014 if (!i.prefix[BND_PREFIX])
5015 add_prefix (BND_PREFIX_OPCODE);
5016 else if (i.prefix[BND_PREFIX] != BND_PREFIX_OPCODE)
5017 {
5018 as_warn (_("replacing `rep'/`repe' prefix by `bnd'"));
5019 i.prefix[BND_PREFIX] = BND_PREFIX_OPCODE;
5020 }
5021 }
7e8b059b 5022
29b0f896 5023 /* Check string instruction segment overrides. */
51c8edf6 5024 if (i.tm.opcode_modifier.isstring >= IS_STRING_ES_OP0)
29b0f896 5025 {
51c8edf6 5026 gas_assert (i.mem_operands);
29b0f896 5027 if (!check_string ())
5dd0794d 5028 return;
fc0763e6 5029 i.disp_operands = 0;
29b0f896 5030 }
5dd0794d 5031
b6f8c7c4
L
5032 if (optimize && !i.no_optimize && i.tm.opcode_modifier.optimize)
5033 optimize_encoding ();
5034
c8480b58
L
5035 if (use_unaligned_vector_move)
5036 encode_with_unaligned_vector_move ();
5037
29b0f896
AM
5038 if (!process_suffix ())
5039 return;
e413e4e9 5040
921eafea 5041 /* Update operand types and check extended states. */
bc0844ae 5042 for (j = 0; j < i.operands; j++)
921eafea
L
5043 {
5044 i.types[j] = operand_type_and (i.types[j], i.tm.operand_types[j]);
3d70986f 5045 switch (i.tm.operand_types[j].bitfield.class)
921eafea
L
5046 {
5047 default:
5048 break;
5049 case RegMMX:
5050 i.xstate |= xstate_mmx;
5051 break;
5052 case RegMask:
32930e4e 5053 i.xstate |= xstate_mask;
921eafea
L
5054 break;
5055 case RegSIMD:
3d70986f 5056 if (i.tm.operand_types[j].bitfield.tmmword)
921eafea 5057 i.xstate |= xstate_tmm;
3d70986f 5058 else if (i.tm.operand_types[j].bitfield.zmmword)
921eafea 5059 i.xstate |= xstate_zmm;
3d70986f 5060 else if (i.tm.operand_types[j].bitfield.ymmword)
921eafea 5061 i.xstate |= xstate_ymm;
3d70986f 5062 else if (i.tm.operand_types[j].bitfield.xmmword)
921eafea
L
5063 i.xstate |= xstate_xmm;
5064 break;
5065 }
5066 }
bc0844ae 5067
29b0f896
AM
5068 /* Make still unresolved immediate matches conform to size of immediate
5069 given in i.suffix. */
5070 if (!finalize_imm ())
5071 return;
252b5132 5072
40fb9820 5073 if (i.types[0].bitfield.imm1)
29b0f896 5074 i.imm_operands = 0; /* kludge for shift insns. */
252b5132 5075
9afe6eb8
L
5076 /* We only need to check those implicit registers for instructions
5077 with 3 operands or less. */
5078 if (i.operands <= 3)
5079 for (j = 0; j < i.operands; j++)
75e5731b
JB
5080 if (i.types[j].bitfield.instance != InstanceNone
5081 && !i.types[j].bitfield.xmmword)
9afe6eb8 5082 i.reg_operands--;
40fb9820 5083
29b0f896
AM
5084 /* For insns with operands there are more diddles to do to the opcode. */
5085 if (i.operands)
5086 {
5087 if (!process_operands ())
5088 return;
5089 }
8c190ce0 5090 else if (!quiet_warnings && i.tm.opcode_modifier.ugh)
29b0f896
AM
5091 {
5092 /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc. */
5093 as_warn (_("translating to `%sp'"), i.tm.name);
5094 }
252b5132 5095
7a8655d2 5096 if (is_any_vex_encoding (&i.tm))
9e5e5283 5097 {
c1dc7af5 5098 if (!cpu_arch_flags.bitfield.cpui286)
9e5e5283 5099 {
c1dc7af5 5100 as_bad (_("instruction `%s' isn't supported outside of protected mode."),
9e5e5283
L
5101 i.tm.name);
5102 return;
5103 }
c0f3af97 5104
0b9404fd
JB
5105 /* Check for explicit REX prefix. */
5106 if (i.prefix[REX_PREFIX] || i.rex_encoding)
5107 {
5108 as_bad (_("REX prefix invalid with `%s'"), i.tm.name);
5109 return;
5110 }
5111
9e5e5283
L
5112 if (i.tm.opcode_modifier.vex)
5113 build_vex_prefix (t);
5114 else
5115 build_evex_prefix ();
0b9404fd
JB
5116
5117 /* The individual REX.RXBW bits got consumed. */
5118 i.rex &= REX_OPCODE;
9e5e5283 5119 }
43234a1e 5120
5dd85c99
SP
5121 /* Handle conversion of 'int $3' --> special int3 insn. XOP or FMA4
5122 instructions may define INT_OPCODE as well, so avoid this corner
5123 case for those instructions that use MODRM. */
389d00a5
JB
5124 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
5125 && i.tm.base_opcode == INT_OPCODE
a6461c02
SP
5126 && !i.tm.opcode_modifier.modrm
5127 && i.op[0].imms->X_add_number == 3)
29b0f896
AM
5128 {
5129 i.tm.base_opcode = INT3_OPCODE;
5130 i.imm_operands = 0;
5131 }
252b5132 5132
0cfa3eb3
JB
5133 if ((i.tm.opcode_modifier.jump == JUMP
5134 || i.tm.opcode_modifier.jump == JUMP_BYTE
5135 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896
AM
5136 && i.op[0].disps->X_op == O_constant)
5137 {
5138 /* Convert "jmp constant" (and "call constant") to a jump (call) to
5139 the absolute address given by the constant. Since ix86 jumps and
5140 calls are pc relative, we need to generate a reloc. */
5141 i.op[0].disps->X_add_symbol = &abs_symbol;
5142 i.op[0].disps->X_op = O_symbol;
5143 }
252b5132 5144
29b0f896
AM
5145 /* For 8 bit registers we need an empty rex prefix. Also if the
5146 instruction already has a prefix, we need to convert old
5147 registers to new ones. */
773f551c 5148
bab6aec1 5149 if ((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte
29b0f896 5150 && (i.op[0].regs->reg_flags & RegRex64) != 0)
bab6aec1 5151 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte
29b0f896 5152 && (i.op[1].regs->reg_flags & RegRex64) != 0)
bab6aec1
JB
5153 || (((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte)
5154 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte))
29b0f896
AM
5155 && i.rex != 0))
5156 {
5157 int x;
726c5dcd 5158
29b0f896
AM
5159 i.rex |= REX_OPCODE;
5160 for (x = 0; x < 2; x++)
5161 {
5162 /* Look for 8 bit operand that uses old registers. */
bab6aec1 5163 if (i.types[x].bitfield.class == Reg && i.types[x].bitfield.byte
29b0f896 5164 && (i.op[x].regs->reg_flags & RegRex64) == 0)
773f551c 5165 {
3f93af61 5166 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
29b0f896
AM
5167 /* In case it is "hi" register, give up. */
5168 if (i.op[x].regs->reg_num > 3)
a540244d 5169 as_bad (_("can't encode register '%s%s' in an "
4eed87de 5170 "instruction requiring REX prefix."),
a540244d 5171 register_prefix, i.op[x].regs->reg_name);
773f551c 5172
29b0f896
AM
5173 /* Otherwise it is equivalent to the extended register.
5174 Since the encoding doesn't change this is merely
5175 cosmetic cleanup for debug output. */
5176
5177 i.op[x].regs = i.op[x].regs + 8;
773f551c 5178 }
29b0f896
AM
5179 }
5180 }
773f551c 5181
6b6b6807
L
5182 if (i.rex == 0 && i.rex_encoding)
5183 {
5184 /* Check if we can add a REX_OPCODE byte. Look for 8 bit operand
3f93af61 5185 that uses legacy register. If it is "hi" register, don't add
6b6b6807
L
5186 the REX_OPCODE byte. */
5187 int x;
5188 for (x = 0; x < 2; x++)
bab6aec1 5189 if (i.types[x].bitfield.class == Reg
6b6b6807
L
5190 && i.types[x].bitfield.byte
5191 && (i.op[x].regs->reg_flags & RegRex64) == 0
5192 && i.op[x].regs->reg_num > 3)
5193 {
3f93af61 5194 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
5b7c81bd 5195 i.rex_encoding = false;
6b6b6807
L
5196 break;
5197 }
5198
5199 if (i.rex_encoding)
5200 i.rex = REX_OPCODE;
5201 }
5202
7ab9ffdd 5203 if (i.rex != 0)
29b0f896
AM
5204 add_prefix (REX_OPCODE | i.rex);
5205
ae531041
L
5206 insert_lfence_before ();
5207
29b0f896
AM
5208 /* We are ready to output the insn. */
5209 output_insn ();
e379e5f3 5210
ae531041
L
5211 insert_lfence_after ();
5212
e379e5f3
L
5213 last_insn.seg = now_seg;
5214
5215 if (i.tm.opcode_modifier.isprefix)
5216 {
5217 last_insn.kind = last_insn_prefix;
5218 last_insn.name = i.tm.name;
5219 last_insn.file = as_where (&last_insn.line);
5220 }
5221 else
5222 last_insn.kind = last_insn_other;
29b0f896
AM
5223}
5224
5225static char *
e3bb37b5 5226parse_insn (char *line, char *mnemonic)
29b0f896
AM
5227{
5228 char *l = line;
5229 char *token_start = l;
5230 char *mnem_p;
5c6af06e 5231 int supported;
d3ce72d0 5232 const insn_template *t;
b6169b20 5233 char *dot_p = NULL;
29b0f896 5234
29b0f896
AM
5235 while (1)
5236 {
5237 mnem_p = mnemonic;
5238 while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
5239 {
b6169b20
L
5240 if (*mnem_p == '.')
5241 dot_p = mnem_p;
29b0f896
AM
5242 mnem_p++;
5243 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
45288df1 5244 {
29b0f896
AM
5245 as_bad (_("no such instruction: `%s'"), token_start);
5246 return NULL;
5247 }
5248 l++;
5249 }
5250 if (!is_space_char (*l)
5251 && *l != END_OF_INSN
e44823cf
JB
5252 && (intel_syntax
5253 || (*l != PREFIX_SEPARATOR
5254 && *l != ',')))
29b0f896
AM
5255 {
5256 as_bad (_("invalid character %s in mnemonic"),
5257 output_invalid (*l));
5258 return NULL;
5259 }
5260 if (token_start == l)
5261 {
e44823cf 5262 if (!intel_syntax && *l == PREFIX_SEPARATOR)
29b0f896
AM
5263 as_bad (_("expecting prefix; got nothing"));
5264 else
5265 as_bad (_("expecting mnemonic; got nothing"));
5266 return NULL;
5267 }
45288df1 5268
29b0f896 5269 /* Look up instruction (or prefix) via hash table. */
629310ab 5270 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
47926f60 5271
29b0f896
AM
5272 if (*l != END_OF_INSN
5273 && (!is_space_char (*l) || l[1] != END_OF_INSN)
5274 && current_templates
40fb9820 5275 && current_templates->start->opcode_modifier.isprefix)
29b0f896 5276 {
c6fb90c8 5277 if (!cpu_flags_check_cpu64 (current_templates->start->cpu_flags))
2dd88dca
JB
5278 {
5279 as_bad ((flag_code != CODE_64BIT
5280 ? _("`%s' is only supported in 64-bit mode")
5281 : _("`%s' is not supported in 64-bit mode")),
5282 current_templates->start->name);
5283 return NULL;
5284 }
29b0f896
AM
5285 /* If we are in 16-bit mode, do not allow addr16 or data16.
5286 Similarly, in 32-bit mode, do not allow addr32 or data32. */
673fe0f0
JB
5287 if ((current_templates->start->opcode_modifier.size == SIZE16
5288 || current_templates->start->opcode_modifier.size == SIZE32)
29b0f896 5289 && flag_code != CODE_64BIT
673fe0f0 5290 && ((current_templates->start->opcode_modifier.size == SIZE32)
29b0f896
AM
5291 ^ (flag_code == CODE_16BIT)))
5292 {
5293 as_bad (_("redundant %s prefix"),
5294 current_templates->start->name);
5295 return NULL;
45288df1 5296 }
31184569
JB
5297
5298 if (current_templates->start->base_opcode == PSEUDO_PREFIX)
29b0f896 5299 {
86fa6981 5300 /* Handle pseudo prefixes. */
31184569 5301 switch (current_templates->start->extension_opcode)
86fa6981 5302 {
41eb8e88 5303 case Prefix_Disp8:
86fa6981
L
5304 /* {disp8} */
5305 i.disp_encoding = disp_encoding_8bit;
5306 break;
41eb8e88
L
5307 case Prefix_Disp16:
5308 /* {disp16} */
5309 i.disp_encoding = disp_encoding_16bit;
5310 break;
5311 case Prefix_Disp32:
86fa6981
L
5312 /* {disp32} */
5313 i.disp_encoding = disp_encoding_32bit;
5314 break;
41eb8e88 5315 case Prefix_Load:
86fa6981
L
5316 /* {load} */
5317 i.dir_encoding = dir_encoding_load;
5318 break;
41eb8e88 5319 case Prefix_Store:
86fa6981
L
5320 /* {store} */
5321 i.dir_encoding = dir_encoding_store;
5322 break;
41eb8e88 5323 case Prefix_VEX:
42e04b36
L
5324 /* {vex} */
5325 i.vec_encoding = vex_encoding_vex;
86fa6981 5326 break;
41eb8e88 5327 case Prefix_VEX3:
86fa6981
L
5328 /* {vex3} */
5329 i.vec_encoding = vex_encoding_vex3;
5330 break;
41eb8e88 5331 case Prefix_EVEX:
86fa6981
L
5332 /* {evex} */
5333 i.vec_encoding = vex_encoding_evex;
5334 break;
41eb8e88 5335 case Prefix_REX:
6b6b6807 5336 /* {rex} */
5b7c81bd 5337 i.rex_encoding = true;
6b6b6807 5338 break;
41eb8e88 5339 case Prefix_NoOptimize:
b6f8c7c4 5340 /* {nooptimize} */
5b7c81bd 5341 i.no_optimize = true;
b6f8c7c4 5342 break;
86fa6981
L
5343 default:
5344 abort ();
5345 }
5346 }
5347 else
5348 {
5349 /* Add prefix, checking for repeated prefixes. */
4e9ac44a 5350 switch (add_prefix (current_templates->start->base_opcode))
86fa6981 5351 {
4e9ac44a
L
5352 case PREFIX_EXIST:
5353 return NULL;
5354 case PREFIX_DS:
d777820b 5355 if (current_templates->start->cpu_flags.bitfield.cpuibt)
4e9ac44a
L
5356 i.notrack_prefix = current_templates->start->name;
5357 break;
5358 case PREFIX_REP:
5359 if (current_templates->start->cpu_flags.bitfield.cpuhle)
5360 i.hle_prefix = current_templates->start->name;
5361 else if (current_templates->start->cpu_flags.bitfield.cpumpx)
5362 i.bnd_prefix = current_templates->start->name;
5363 else
5364 i.rep_prefix = current_templates->start->name;
5365 break;
5366 default:
5367 break;
86fa6981 5368 }
29b0f896
AM
5369 }
5370 /* Skip past PREFIX_SEPARATOR and reset token_start. */
5371 token_start = ++l;
5372 }
5373 else
5374 break;
5375 }
45288df1 5376
30a55f88 5377 if (!current_templates)
b6169b20 5378 {
07d5e953
JB
5379 /* Deprecated functionality (new code should use pseudo-prefixes instead):
5380 Check if we should swap operand or force 32bit displacement in
f8a5c266 5381 encoding. */
30a55f88 5382 if (mnem_p - 2 == dot_p && dot_p[1] == 's')
64c49ab3 5383 i.dir_encoding = dir_encoding_swap;
8d63c93e 5384 else if (mnem_p - 3 == dot_p
a501d77e
L
5385 && dot_p[1] == 'd'
5386 && dot_p[2] == '8')
5387 i.disp_encoding = disp_encoding_8bit;
8d63c93e 5388 else if (mnem_p - 4 == dot_p
f8a5c266
L
5389 && dot_p[1] == 'd'
5390 && dot_p[2] == '3'
5391 && dot_p[3] == '2')
a501d77e 5392 i.disp_encoding = disp_encoding_32bit;
30a55f88
L
5393 else
5394 goto check_suffix;
5395 mnem_p = dot_p;
5396 *dot_p = '\0';
629310ab 5397 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
b6169b20
L
5398 }
5399
29b0f896
AM
5400 if (!current_templates)
5401 {
dc1e8a47 5402 check_suffix:
1c529385 5403 if (mnem_p > mnemonic)
29b0f896 5404 {
1c529385
LH
5405 /* See if we can get a match by trimming off a suffix. */
5406 switch (mnem_p[-1])
29b0f896 5407 {
1c529385
LH
5408 case WORD_MNEM_SUFFIX:
5409 if (intel_syntax && (intel_float_operand (mnemonic) & 2))
29b0f896
AM
5410 i.suffix = SHORT_MNEM_SUFFIX;
5411 else
1c529385
LH
5412 /* Fall through. */
5413 case BYTE_MNEM_SUFFIX:
5414 case QWORD_MNEM_SUFFIX:
5415 i.suffix = mnem_p[-1];
29b0f896 5416 mnem_p[-1] = '\0';
fe0e921f
AM
5417 current_templates
5418 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5419 break;
5420 case SHORT_MNEM_SUFFIX:
5421 case LONG_MNEM_SUFFIX:
5422 if (!intel_syntax)
5423 {
5424 i.suffix = mnem_p[-1];
5425 mnem_p[-1] = '\0';
fe0e921f
AM
5426 current_templates
5427 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5428 }
5429 break;
5430
5431 /* Intel Syntax. */
5432 case 'd':
5433 if (intel_syntax)
5434 {
5435 if (intel_float_operand (mnemonic) == 1)
5436 i.suffix = SHORT_MNEM_SUFFIX;
5437 else
5438 i.suffix = LONG_MNEM_SUFFIX;
5439 mnem_p[-1] = '\0';
fe0e921f
AM
5440 current_templates
5441 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5442 }
5443 break;
29b0f896 5444 }
29b0f896 5445 }
1c529385 5446
29b0f896
AM
5447 if (!current_templates)
5448 {
5449 as_bad (_("no such instruction: `%s'"), token_start);
5450 return NULL;
5451 }
5452 }
252b5132 5453
0cfa3eb3
JB
5454 if (current_templates->start->opcode_modifier.jump == JUMP
5455 || current_templates->start->opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
5456 {
5457 /* Check for a branch hint. We allow ",pt" and ",pn" for
5458 predict taken and predict not taken respectively.
5459 I'm not sure that branch hints actually do anything on loop
5460 and jcxz insns (JumpByte) for current Pentium4 chips. They
5461 may work in the future and it doesn't hurt to accept them
5462 now. */
5463 if (l[0] == ',' && l[1] == 'p')
5464 {
5465 if (l[2] == 't')
5466 {
5467 if (!add_prefix (DS_PREFIX_OPCODE))
5468 return NULL;
5469 l += 3;
5470 }
5471 else if (l[2] == 'n')
5472 {
5473 if (!add_prefix (CS_PREFIX_OPCODE))
5474 return NULL;
5475 l += 3;
5476 }
5477 }
5478 }
5479 /* Any other comma loses. */
5480 if (*l == ',')
5481 {
5482 as_bad (_("invalid character %s in mnemonic"),
5483 output_invalid (*l));
5484 return NULL;
5485 }
252b5132 5486
29b0f896 5487 /* Check if instruction is supported on specified architecture. */
5c6af06e
JB
5488 supported = 0;
5489 for (t = current_templates->start; t < current_templates->end; ++t)
5490 {
c0f3af97
L
5491 supported |= cpu_flags_match (t);
5492 if (supported == CPU_FLAGS_PERFECT_MATCH)
d59a54c2 5493 return l;
29b0f896 5494 }
3629bb00 5495
548d0ee6
JB
5496 if (!(supported & CPU_FLAGS_64BIT_MATCH))
5497 as_bad (flag_code == CODE_64BIT
5498 ? _("`%s' is not supported in 64-bit mode")
5499 : _("`%s' is only supported in 64-bit mode"),
5500 current_templates->start->name);
5501 else
5502 as_bad (_("`%s' is not supported on `%s%s'"),
5503 current_templates->start->name,
5504 cpu_arch_name ? cpu_arch_name : default_arch,
5505 cpu_sub_arch_name ? cpu_sub_arch_name : "");
252b5132 5506
548d0ee6 5507 return NULL;
29b0f896 5508}
252b5132 5509
29b0f896 5510static char *
e3bb37b5 5511parse_operands (char *l, const char *mnemonic)
29b0f896
AM
5512{
5513 char *token_start;
3138f287 5514
29b0f896
AM
5515 /* 1 if operand is pending after ','. */
5516 unsigned int expecting_operand = 0;
252b5132 5517
29b0f896
AM
5518 while (*l != END_OF_INSN)
5519 {
e68c3d59
JB
5520 /* Non-zero if operand parens not balanced. */
5521 unsigned int paren_not_balanced = 0;
5522 /* True if inside double quotes. */
5523 bool in_quotes = false;
5524
29b0f896
AM
5525 /* Skip optional white space before operand. */
5526 if (is_space_char (*l))
5527 ++l;
d02603dc 5528 if (!is_operand_char (*l) && *l != END_OF_INSN && *l != '"')
29b0f896
AM
5529 {
5530 as_bad (_("invalid character %s before operand %d"),
5531 output_invalid (*l),
5532 i.operands + 1);
5533 return NULL;
5534 }
d02603dc 5535 token_start = l; /* After white space. */
e68c3d59 5536 while (in_quotes || paren_not_balanced || *l != ',')
29b0f896
AM
5537 {
5538 if (*l == END_OF_INSN)
5539 {
e68c3d59
JB
5540 if (in_quotes)
5541 {
5542 as_bad (_("unbalanced double quotes in operand %d."),
5543 i.operands + 1);
5544 return NULL;
5545 }
29b0f896
AM
5546 if (paren_not_balanced)
5547 {
98ff9f1c
JB
5548 know (!intel_syntax);
5549 as_bad (_("unbalanced parenthesis in operand %d."),
5550 i.operands + 1);
29b0f896
AM
5551 return NULL;
5552 }
5553 else
5554 break; /* we are done */
5555 }
e68c3d59
JB
5556 else if (*l == '\\' && l[1] == '"')
5557 ++l;
5558 else if (*l == '"')
5559 in_quotes = !in_quotes;
5560 else if (!in_quotes && !is_operand_char (*l) && !is_space_char (*l))
29b0f896
AM
5561 {
5562 as_bad (_("invalid character %s in operand %d"),
5563 output_invalid (*l),
5564 i.operands + 1);
5565 return NULL;
5566 }
e68c3d59 5567 if (!intel_syntax && !in_quotes)
29b0f896
AM
5568 {
5569 if (*l == '(')
5570 ++paren_not_balanced;
5571 if (*l == ')')
5572 --paren_not_balanced;
5573 }
29b0f896
AM
5574 l++;
5575 }
5576 if (l != token_start)
5577 { /* Yes, we've read in another operand. */
5578 unsigned int operand_ok;
5579 this_operand = i.operands++;
5580 if (i.operands > MAX_OPERANDS)
5581 {
5582 as_bad (_("spurious operands; (%d operands/instruction max)"),
5583 MAX_OPERANDS);
5584 return NULL;
5585 }
9d46ce34 5586 i.types[this_operand].bitfield.unspecified = 1;
29b0f896
AM
5587 /* Now parse operand adding info to 'i' as we go along. */
5588 END_STRING_AND_SAVE (l);
5589
1286ab78
L
5590 if (i.mem_operands > 1)
5591 {
5592 as_bad (_("too many memory references for `%s'"),
5593 mnemonic);
5594 return 0;
5595 }
5596
29b0f896
AM
5597 if (intel_syntax)
5598 operand_ok =
5599 i386_intel_operand (token_start,
5600 intel_float_operand (mnemonic));
5601 else
a7619375 5602 operand_ok = i386_att_operand (token_start);
29b0f896
AM
5603
5604 RESTORE_END_STRING (l);
5605 if (!operand_ok)
5606 return NULL;
5607 }
5608 else
5609 {
5610 if (expecting_operand)
5611 {
5612 expecting_operand_after_comma:
5613 as_bad (_("expecting operand after ','; got nothing"));
5614 return NULL;
5615 }
5616 if (*l == ',')
5617 {
5618 as_bad (_("expecting operand before ','; got nothing"));
5619 return NULL;
5620 }
5621 }
7f3f1ea2 5622
29b0f896
AM
5623 /* Now *l must be either ',' or END_OF_INSN. */
5624 if (*l == ',')
5625 {
5626 if (*++l == END_OF_INSN)
5627 {
5628 /* Just skip it, if it's \n complain. */
5629 goto expecting_operand_after_comma;
5630 }
5631 expecting_operand = 1;
5632 }
5633 }
5634 return l;
5635}
7f3f1ea2 5636
050dfa73 5637static void
783c187b 5638swap_2_operands (unsigned int xchg1, unsigned int xchg2)
050dfa73
MM
5639{
5640 union i386_op temp_op;
40fb9820 5641 i386_operand_type temp_type;
c48dadc9 5642 unsigned int temp_flags;
050dfa73 5643 enum bfd_reloc_code_real temp_reloc;
4eed87de 5644
050dfa73
MM
5645 temp_type = i.types[xchg2];
5646 i.types[xchg2] = i.types[xchg1];
5647 i.types[xchg1] = temp_type;
c48dadc9
JB
5648
5649 temp_flags = i.flags[xchg2];
5650 i.flags[xchg2] = i.flags[xchg1];
5651 i.flags[xchg1] = temp_flags;
5652
050dfa73
MM
5653 temp_op = i.op[xchg2];
5654 i.op[xchg2] = i.op[xchg1];
5655 i.op[xchg1] = temp_op;
c48dadc9 5656
050dfa73
MM
5657 temp_reloc = i.reloc[xchg2];
5658 i.reloc[xchg2] = i.reloc[xchg1];
5659 i.reloc[xchg1] = temp_reloc;
43234a1e 5660
6225c532 5661 if (i.mask.reg)
43234a1e 5662 {
6225c532
JB
5663 if (i.mask.operand == xchg1)
5664 i.mask.operand = xchg2;
5665 else if (i.mask.operand == xchg2)
5666 i.mask.operand = xchg1;
43234a1e 5667 }
a5748e0d 5668 if (i.broadcast.type || i.broadcast.bytes)
43234a1e 5669 {
5273a3cd
JB
5670 if (i.broadcast.operand == xchg1)
5671 i.broadcast.operand = xchg2;
5672 else if (i.broadcast.operand == xchg2)
5673 i.broadcast.operand = xchg1;
43234a1e 5674 }
050dfa73
MM
5675}
5676
29b0f896 5677static void
e3bb37b5 5678swap_operands (void)
29b0f896 5679{
b7c61d9a 5680 switch (i.operands)
050dfa73 5681 {
c0f3af97 5682 case 5:
b7c61d9a 5683 case 4:
4d456e3d 5684 swap_2_operands (1, i.operands - 2);
1a0670f3 5685 /* Fall through. */
b7c61d9a
L
5686 case 3:
5687 case 2:
4d456e3d 5688 swap_2_operands (0, i.operands - 1);
b7c61d9a
L
5689 break;
5690 default:
5691 abort ();
29b0f896 5692 }
29b0f896
AM
5693
5694 if (i.mem_operands == 2)
5695 {
5e042380 5696 const reg_entry *temp_seg;
29b0f896
AM
5697 temp_seg = i.seg[0];
5698 i.seg[0] = i.seg[1];
5699 i.seg[1] = temp_seg;
5700 }
5701}
252b5132 5702
29b0f896
AM
5703/* Try to ensure constant immediates are represented in the smallest
5704 opcode possible. */
5705static void
e3bb37b5 5706optimize_imm (void)
29b0f896
AM
5707{
5708 char guess_suffix = 0;
5709 int op;
252b5132 5710
29b0f896
AM
5711 if (i.suffix)
5712 guess_suffix = i.suffix;
5713 else if (i.reg_operands)
5714 {
5715 /* Figure out a suffix from the last register operand specified.
75e5731b
JB
5716 We can't do this properly yet, i.e. excluding special register
5717 instances, but the following works for instructions with
5718 immediates. In any case, we can't set i.suffix yet. */
29b0f896 5719 for (op = i.operands; --op >= 0;)
bab6aec1
JB
5720 if (i.types[op].bitfield.class != Reg)
5721 continue;
5722 else if (i.types[op].bitfield.byte)
7ab9ffdd 5723 {
40fb9820
L
5724 guess_suffix = BYTE_MNEM_SUFFIX;
5725 break;
5726 }
bab6aec1 5727 else if (i.types[op].bitfield.word)
252b5132 5728 {
40fb9820
L
5729 guess_suffix = WORD_MNEM_SUFFIX;
5730 break;
5731 }
bab6aec1 5732 else if (i.types[op].bitfield.dword)
40fb9820
L
5733 {
5734 guess_suffix = LONG_MNEM_SUFFIX;
5735 break;
5736 }
bab6aec1 5737 else if (i.types[op].bitfield.qword)
40fb9820
L
5738 {
5739 guess_suffix = QWORD_MNEM_SUFFIX;
29b0f896 5740 break;
252b5132 5741 }
29b0f896
AM
5742 }
5743 else if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
5744 guess_suffix = WORD_MNEM_SUFFIX;
5745
5746 for (op = i.operands; --op >= 0;)
40fb9820 5747 if (operand_type_check (i.types[op], imm))
29b0f896
AM
5748 {
5749 switch (i.op[op].imms->X_op)
252b5132 5750 {
29b0f896
AM
5751 case O_constant:
5752 /* If a suffix is given, this operand may be shortened. */
5753 switch (guess_suffix)
252b5132 5754 {
29b0f896 5755 case LONG_MNEM_SUFFIX:
40fb9820
L
5756 i.types[op].bitfield.imm32 = 1;
5757 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
5758 break;
5759 case WORD_MNEM_SUFFIX:
40fb9820
L
5760 i.types[op].bitfield.imm16 = 1;
5761 i.types[op].bitfield.imm32 = 1;
5762 i.types[op].bitfield.imm32s = 1;
5763 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
5764 break;
5765 case BYTE_MNEM_SUFFIX:
40fb9820
L
5766 i.types[op].bitfield.imm8 = 1;
5767 i.types[op].bitfield.imm8s = 1;
5768 i.types[op].bitfield.imm16 = 1;
5769 i.types[op].bitfield.imm32 = 1;
5770 i.types[op].bitfield.imm32s = 1;
5771 i.types[op].bitfield.imm64 = 1;
29b0f896 5772 break;
252b5132 5773 }
252b5132 5774
29b0f896
AM
5775 /* If this operand is at most 16 bits, convert it
5776 to a signed 16 bit number before trying to see
5777 whether it will fit in an even smaller size.
5778 This allows a 16-bit operand such as $0xffe0 to
5779 be recognised as within Imm8S range. */
40fb9820 5780 if ((i.types[op].bitfield.imm16)
7e96fb68 5781 && fits_in_unsigned_word (i.op[op].imms->X_add_number))
252b5132 5782 {
87ed972d
JB
5783 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
5784 ^ 0x8000) - 0x8000);
29b0f896 5785 }
a28def75
L
5786#ifdef BFD64
5787 /* Store 32-bit immediate in 64-bit for 64-bit BFD. */
40fb9820 5788 if ((i.types[op].bitfield.imm32)
7e96fb68 5789 && fits_in_unsigned_long (i.op[op].imms->X_add_number))
29b0f896
AM
5790 {
5791 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
5792 ^ ((offsetT) 1 << 31))
5793 - ((offsetT) 1 << 31));
5794 }
a28def75 5795#endif
40fb9820 5796 i.types[op]
c6fb90c8
L
5797 = operand_type_or (i.types[op],
5798 smallest_imm_type (i.op[op].imms->X_add_number));
252b5132 5799
29b0f896
AM
5800 /* We must avoid matching of Imm32 templates when 64bit
5801 only immediate is available. */
5802 if (guess_suffix == QWORD_MNEM_SUFFIX)
40fb9820 5803 i.types[op].bitfield.imm32 = 0;
29b0f896 5804 break;
252b5132 5805
29b0f896
AM
5806 case O_absent:
5807 case O_register:
5808 abort ();
5809
5810 /* Symbols and expressions. */
5811 default:
9cd96992
JB
5812 /* Convert symbolic operand to proper sizes for matching, but don't
5813 prevent matching a set of insns that only supports sizes other
5814 than those matching the insn suffix. */
5815 {
40fb9820 5816 i386_operand_type mask, allowed;
87ed972d 5817 const insn_template *t = current_templates->start;
9cd96992 5818
0dfbf9d7 5819 operand_type_set (&mask, 0);
87ed972d 5820 allowed = t->operand_types[op];
40fb9820 5821
87ed972d 5822 while (++t < current_templates->end)
bab6aec1 5823 {
bab6aec1 5824 allowed = operand_type_and (allowed, anyimm);
87ed972d 5825 allowed = operand_type_or (allowed, t->operand_types[op]);
bab6aec1 5826 }
9cd96992
JB
5827 switch (guess_suffix)
5828 {
5829 case QWORD_MNEM_SUFFIX:
40fb9820
L
5830 mask.bitfield.imm64 = 1;
5831 mask.bitfield.imm32s = 1;
9cd96992
JB
5832 break;
5833 case LONG_MNEM_SUFFIX:
40fb9820 5834 mask.bitfield.imm32 = 1;
9cd96992
JB
5835 break;
5836 case WORD_MNEM_SUFFIX:
40fb9820 5837 mask.bitfield.imm16 = 1;
9cd96992
JB
5838 break;
5839 case BYTE_MNEM_SUFFIX:
40fb9820 5840 mask.bitfield.imm8 = 1;
9cd96992
JB
5841 break;
5842 default:
9cd96992
JB
5843 break;
5844 }
c6fb90c8 5845 allowed = operand_type_and (mask, allowed);
0dfbf9d7 5846 if (!operand_type_all_zero (&allowed))
c6fb90c8 5847 i.types[op] = operand_type_and (i.types[op], mask);
9cd96992 5848 }
29b0f896 5849 break;
252b5132 5850 }
29b0f896
AM
5851 }
5852}
47926f60 5853
29b0f896
AM
5854/* Try to use the smallest displacement type too. */
5855static void
e3bb37b5 5856optimize_disp (void)
29b0f896
AM
5857{
5858 int op;
3e73aa7c 5859
29b0f896 5860 for (op = i.operands; --op >= 0;)
40fb9820 5861 if (operand_type_check (i.types[op], disp))
252b5132 5862 {
b300c311 5863 if (i.op[op].disps->X_op == O_constant)
252b5132 5864 {
91d6fa6a 5865 offsetT op_disp = i.op[op].disps->X_add_number;
29b0f896 5866
91d6fa6a 5867 if (!op_disp && i.types[op].bitfield.baseindex)
b300c311 5868 {
2f2be86b
JB
5869 i.types[op] = operand_type_and_not (i.types[op], anydisp);
5870 i.op[op].disps = NULL;
b300c311 5871 i.disp_operands--;
f185acdd
JB
5872 continue;
5873 }
5874
5875 if (i.types[op].bitfield.disp16
cd613c1f 5876 && fits_in_unsigned_word (op_disp))
f185acdd
JB
5877 {
5878 /* If this operand is at most 16 bits, convert
5879 to a signed 16 bit number and don't use 64bit
5880 displacement. */
5881 op_disp = ((op_disp ^ 0x8000) - 0x8000);
5882 i.types[op].bitfield.disp64 = 0;
b300c311 5883 }
f185acdd 5884
28a167a4 5885#ifdef BFD64
a50187b2 5886 /* Optimize 64-bit displacement to 32-bit for 64-bit BFD. */
a775efc8
JB
5887 if ((flag_code != CODE_64BIT
5888 ? i.types[op].bitfield.disp32
5889 : want_disp32 (current_templates->start)
5890 && (!current_templates->start->opcode_modifier.jump
5891 || i.jumpabsolute || i.types[op].bitfield.baseindex))
a50187b2 5892 && fits_in_unsigned_long (op_disp))
b300c311 5893 {
a50187b2
JB
5894 /* If this operand is at most 32 bits, convert
5895 to a signed 32 bit number and don't use 64bit
5896 displacement. */
5897 op_disp = (op_disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
5898 i.types[op].bitfield.disp64 = 0;
5899 i.types[op].bitfield.disp32 = 1;
5900 }
28a167a4 5901
a50187b2
JB
5902 if (flag_code == CODE_64BIT && fits_in_signed_long (op_disp))
5903 {
5904 i.types[op].bitfield.disp64 = 0;
a775efc8 5905 i.types[op].bitfield.disp32 = 1;
b300c311 5906 }
28a167a4 5907#endif
40fb9820 5908 if ((i.types[op].bitfield.disp32
40fb9820 5909 || i.types[op].bitfield.disp16)
b5014f7a 5910 && fits_in_disp8 (op_disp))
40fb9820 5911 i.types[op].bitfield.disp8 = 1;
77c59789
JB
5912
5913 i.op[op].disps->X_add_number = op_disp;
252b5132 5914 }
67a4f2b7
AO
5915 else if (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
5916 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL)
5917 {
5918 fix_new_exp (frag_now, frag_more (0) - frag_now->fr_literal, 0,
5919 i.op[op].disps, 0, i.reloc[op]);
2f2be86b 5920 i.types[op] = operand_type_and_not (i.types[op], anydisp);
67a4f2b7
AO
5921 }
5922 else
b300c311 5923 /* We only support 64bit displacement on constants. */
40fb9820 5924 i.types[op].bitfield.disp64 = 0;
252b5132 5925 }
29b0f896
AM
5926}
5927
4a1b91ea
L
5928/* Return 1 if there is a match in broadcast bytes between operand
5929 GIVEN and instruction template T. */
5930
5931static INLINE int
5932match_broadcast_size (const insn_template *t, unsigned int given)
5933{
5934 return ((t->opcode_modifier.broadcast == BYTE_BROADCAST
5935 && i.types[given].bitfield.byte)
5936 || (t->opcode_modifier.broadcast == WORD_BROADCAST
5937 && i.types[given].bitfield.word)
5938 || (t->opcode_modifier.broadcast == DWORD_BROADCAST
5939 && i.types[given].bitfield.dword)
5940 || (t->opcode_modifier.broadcast == QWORD_BROADCAST
5941 && i.types[given].bitfield.qword));
5942}
5943
6c30d220
L
5944/* Check if operands are valid for the instruction. */
5945
5946static int
5947check_VecOperands (const insn_template *t)
5948{
43234a1e 5949 unsigned int op;
e2195274 5950 i386_cpu_flags cpu;
e2195274
JB
5951
5952 /* Templates allowing for ZMMword as well as YMMword and/or XMMword for
5953 any one operand are implicity requiring AVX512VL support if the actual
5954 operand size is YMMword or XMMword. Since this function runs after
5955 template matching, there's no need to check for YMMword/XMMword in
5956 the template. */
5957 cpu = cpu_flags_and (t->cpu_flags, avx512);
5958 if (!cpu_flags_all_zero (&cpu)
5959 && !t->cpu_flags.bitfield.cpuavx512vl
5960 && !cpu_arch_flags.bitfield.cpuavx512vl)
5961 {
5962 for (op = 0; op < t->operands; ++op)
5963 {
5964 if (t->operand_types[op].bitfield.zmmword
5965 && (i.types[op].bitfield.ymmword
5966 || i.types[op].bitfield.xmmword))
5967 {
5968 i.error = unsupported;
5969 return 1;
5970 }
5971 }
5972 }
43234a1e 5973
22c36940
JB
5974 /* Somewhat similarly, templates specifying both AVX and AVX2 are
5975 requiring AVX2 support if the actual operand size is YMMword. */
5976 if (t->cpu_flags.bitfield.cpuavx
5977 && t->cpu_flags.bitfield.cpuavx2
5978 && !cpu_arch_flags.bitfield.cpuavx2)
5979 {
5980 for (op = 0; op < t->operands; ++op)
5981 {
5982 if (t->operand_types[op].bitfield.xmmword
5983 && i.types[op].bitfield.ymmword)
5984 {
5985 i.error = unsupported;
5986 return 1;
5987 }
5988 }
5989 }
5990
6c30d220 5991 /* Without VSIB byte, we can't have a vector register for index. */
63112cd6 5992 if (!t->opcode_modifier.sib
6c30d220 5993 && i.index_reg
1b54b8d7
JB
5994 && (i.index_reg->reg_type.bitfield.xmmword
5995 || i.index_reg->reg_type.bitfield.ymmword
5996 || i.index_reg->reg_type.bitfield.zmmword))
6c30d220
L
5997 {
5998 i.error = unsupported_vector_index_register;
5999 return 1;
6000 }
6001
ad8ecc81
MZ
6002 /* Check if default mask is allowed. */
6003 if (t->opcode_modifier.nodefmask
6225c532 6004 && (!i.mask.reg || i.mask.reg->reg_num == 0))
ad8ecc81
MZ
6005 {
6006 i.error = no_default_mask;
6007 return 1;
6008 }
6009
7bab8ab5
JB
6010 /* For VSIB byte, we need a vector register for index, and all vector
6011 registers must be distinct. */
260cd341 6012 if (t->opcode_modifier.sib && t->opcode_modifier.sib != SIBMEM)
7bab8ab5
JB
6013 {
6014 if (!i.index_reg
63112cd6 6015 || !((t->opcode_modifier.sib == VECSIB128
1b54b8d7 6016 && i.index_reg->reg_type.bitfield.xmmword)
63112cd6 6017 || (t->opcode_modifier.sib == VECSIB256
1b54b8d7 6018 && i.index_reg->reg_type.bitfield.ymmword)
63112cd6 6019 || (t->opcode_modifier.sib == VECSIB512
1b54b8d7 6020 && i.index_reg->reg_type.bitfield.zmmword)))
7bab8ab5
JB
6021 {
6022 i.error = invalid_vsib_address;
6023 return 1;
6024 }
6025
6225c532
JB
6026 gas_assert (i.reg_operands == 2 || i.mask.reg);
6027 if (i.reg_operands == 2 && !i.mask.reg)
43234a1e 6028 {
3528c362 6029 gas_assert (i.types[0].bitfield.class == RegSIMD);
1b54b8d7
JB
6030 gas_assert (i.types[0].bitfield.xmmword
6031 || i.types[0].bitfield.ymmword);
3528c362 6032 gas_assert (i.types[2].bitfield.class == RegSIMD);
1b54b8d7
JB
6033 gas_assert (i.types[2].bitfield.xmmword
6034 || i.types[2].bitfield.ymmword);
43234a1e
L
6035 if (operand_check == check_none)
6036 return 0;
6037 if (register_number (i.op[0].regs)
6038 != register_number (i.index_reg)
6039 && register_number (i.op[2].regs)
6040 != register_number (i.index_reg)
6041 && register_number (i.op[0].regs)
6042 != register_number (i.op[2].regs))
6043 return 0;
6044 if (operand_check == check_error)
6045 {
6046 i.error = invalid_vector_register_set;
6047 return 1;
6048 }
6049 as_warn (_("mask, index, and destination registers should be distinct"));
6050 }
6225c532 6051 else if (i.reg_operands == 1 && i.mask.reg)
8444f82a 6052 {
3528c362 6053 if (i.types[1].bitfield.class == RegSIMD
1b54b8d7
JB
6054 && (i.types[1].bitfield.xmmword
6055 || i.types[1].bitfield.ymmword
6056 || i.types[1].bitfield.zmmword)
8444f82a
MZ
6057 && (register_number (i.op[1].regs)
6058 == register_number (i.index_reg)))
6059 {
6060 if (operand_check == check_error)
6061 {
6062 i.error = invalid_vector_register_set;
6063 return 1;
6064 }
6065 if (operand_check != check_none)
6066 as_warn (_("index and destination registers should be distinct"));
6067 }
6068 }
43234a1e 6069 }
7bab8ab5 6070
fc141319
L
6071 /* For AMX instructions with 3 TMM register operands, all operands
6072 must be distinct. */
6073 if (i.reg_operands == 3
6074 && t->operand_types[0].bitfield.tmmword
6075 && (i.op[0].regs == i.op[1].regs
6076 || i.op[0].regs == i.op[2].regs
6077 || i.op[1].regs == i.op[2].regs))
6078 {
6079 i.error = invalid_tmm_register_set;
6080 return 1;
260cd341
LC
6081 }
6082
0cc78721
CL
6083 /* For some special instructions require that destination must be distinct
6084 from source registers. */
6085 if (t->opcode_modifier.distinctdest)
6086 {
6087 unsigned int dest_reg = i.operands - 1;
6088
6089 know (i.operands >= 3);
6090
6091 /* #UD if dest_reg == src1_reg or dest_reg == src2_reg. */
6092 if (i.op[dest_reg - 1].regs == i.op[dest_reg].regs
6093 || (i.reg_operands > 2
6094 && i.op[dest_reg - 2].regs == i.op[dest_reg].regs))
6095 {
6096 i.error = invalid_dest_and_src_register_set;
6097 return 1;
6098 }
6099 }
6100
43234a1e
L
6101 /* Check if broadcast is supported by the instruction and is applied
6102 to the memory operand. */
a5748e0d 6103 if (i.broadcast.type || i.broadcast.bytes)
43234a1e 6104 {
8e6e0792 6105 i386_operand_type type, overlap;
43234a1e
L
6106
6107 /* Check if specified broadcast is supported in this instruction,
4a1b91ea 6108 and its broadcast bytes match the memory operand. */
5273a3cd 6109 op = i.broadcast.operand;
8e6e0792 6110 if (!t->opcode_modifier.broadcast
c48dadc9 6111 || !(i.flags[op] & Operand_Mem)
c39e5b26 6112 || (!i.types[op].bitfield.unspecified
4a1b91ea 6113 && !match_broadcast_size (t, op)))
43234a1e
L
6114 {
6115 bad_broadcast:
6116 i.error = unsupported_broadcast;
6117 return 1;
6118 }
8e6e0792 6119
a5748e0d
JB
6120 if (i.broadcast.type)
6121 i.broadcast.bytes = ((1 << (t->opcode_modifier.broadcast - 1))
6122 * i.broadcast.type);
8e6e0792 6123 operand_type_set (&type, 0);
a5748e0d 6124 switch (get_broadcast_bytes (t, false))
8e6e0792 6125 {
4a1b91ea
L
6126 case 2:
6127 type.bitfield.word = 1;
6128 break;
6129 case 4:
6130 type.bitfield.dword = 1;
6131 break;
8e6e0792
JB
6132 case 8:
6133 type.bitfield.qword = 1;
6134 break;
6135 case 16:
6136 type.bitfield.xmmword = 1;
6137 break;
6138 case 32:
6139 type.bitfield.ymmword = 1;
6140 break;
6141 case 64:
6142 type.bitfield.zmmword = 1;
6143 break;
6144 default:
6145 goto bad_broadcast;
6146 }
6147
6148 overlap = operand_type_and (type, t->operand_types[op]);
bc49bfd8
JB
6149 if (t->operand_types[op].bitfield.class == RegSIMD
6150 && t->operand_types[op].bitfield.byte
6151 + t->operand_types[op].bitfield.word
6152 + t->operand_types[op].bitfield.dword
6153 + t->operand_types[op].bitfield.qword > 1)
6154 {
6155 overlap.bitfield.xmmword = 0;
6156 overlap.bitfield.ymmword = 0;
6157 overlap.bitfield.zmmword = 0;
6158 }
8e6e0792
JB
6159 if (operand_type_all_zero (&overlap))
6160 goto bad_broadcast;
6161
6162 if (t->opcode_modifier.checkregsize)
6163 {
6164 unsigned int j;
6165
e2195274 6166 type.bitfield.baseindex = 1;
8e6e0792
JB
6167 for (j = 0; j < i.operands; ++j)
6168 {
6169 if (j != op
6170 && !operand_type_register_match(i.types[j],
6171 t->operand_types[j],
6172 type,
6173 t->operand_types[op]))
6174 goto bad_broadcast;
6175 }
6176 }
43234a1e
L
6177 }
6178 /* If broadcast is supported in this instruction, we need to check if
6179 operand of one-element size isn't specified without broadcast. */
6180 else if (t->opcode_modifier.broadcast && i.mem_operands)
6181 {
6182 /* Find memory operand. */
6183 for (op = 0; op < i.operands; op++)
8dc0818e 6184 if (i.flags[op] & Operand_Mem)
43234a1e
L
6185 break;
6186 gas_assert (op < i.operands);
6187 /* Check size of the memory operand. */
4a1b91ea 6188 if (match_broadcast_size (t, op))
43234a1e
L
6189 {
6190 i.error = broadcast_needed;
6191 return 1;
6192 }
6193 }
c39e5b26
JB
6194 else
6195 op = MAX_OPERANDS - 1; /* Avoid uninitialized variable warning. */
43234a1e
L
6196
6197 /* Check if requested masking is supported. */
6225c532 6198 if (i.mask.reg)
43234a1e 6199 {
ae2387fe
JB
6200 switch (t->opcode_modifier.masking)
6201 {
6202 case BOTH_MASKING:
6203 break;
6204 case MERGING_MASKING:
6225c532 6205 if (i.mask.zeroing)
ae2387fe
JB
6206 {
6207 case 0:
6208 i.error = unsupported_masking;
6209 return 1;
6210 }
6211 break;
6212 case DYNAMIC_MASKING:
6213 /* Memory destinations allow only merging masking. */
6225c532 6214 if (i.mask.zeroing && i.mem_operands)
ae2387fe
JB
6215 {
6216 /* Find memory operand. */
6217 for (op = 0; op < i.operands; op++)
c48dadc9 6218 if (i.flags[op] & Operand_Mem)
ae2387fe
JB
6219 break;
6220 gas_assert (op < i.operands);
6221 if (op == i.operands - 1)
6222 {
6223 i.error = unsupported_masking;
6224 return 1;
6225 }
6226 }
6227 break;
6228 default:
6229 abort ();
6230 }
43234a1e
L
6231 }
6232
6233 /* Check if masking is applied to dest operand. */
6225c532 6234 if (i.mask.reg && (i.mask.operand != i.operands - 1))
43234a1e
L
6235 {
6236 i.error = mask_not_on_destination;
6237 return 1;
6238 }
6239
43234a1e 6240 /* Check RC/SAE. */
ca5312a2 6241 if (i.rounding.type != rc_none)
43234a1e 6242 {
a80195f1 6243 if (!t->opcode_modifier.sae
cf665fee
JB
6244 || ((i.rounding.type != saeonly) != t->opcode_modifier.staticrounding)
6245 || i.mem_operands)
43234a1e
L
6246 {
6247 i.error = unsupported_rc_sae;
6248 return 1;
6249 }
cf665fee
JB
6250
6251 /* Non-EVEX.LIG forms need to have a ZMM register as at least one
6252 operand. */
6253 if (t->opcode_modifier.evex != EVEXLIG)
7bab8ab5 6254 {
cf665fee
JB
6255 for (op = 0; op < t->operands; ++op)
6256 if (i.types[op].bitfield.zmmword)
6257 break;
6258 if (op >= t->operands)
6259 {
6260 i.error = operand_size_mismatch;
6261 return 1;
6262 }
7bab8ab5 6263 }
6c30d220
L
6264 }
6265
da4977e0
JB
6266 /* Check the special Imm4 cases; must be the first operand. */
6267 if (t->cpu_flags.bitfield.cpuxop && t->operands == 5)
6268 {
6269 if (i.op[0].imms->X_op != O_constant
6270 || !fits_in_imm4 (i.op[0].imms->X_add_number))
6271 {
6272 i.error = bad_imm4;
6273 return 1;
6274 }
6275
6276 /* Turn off Imm<N> so that update_imm won't complain. */
6277 operand_type_set (&i.types[0], 0);
6278 }
6279
43234a1e 6280 /* Check vector Disp8 operand. */
b5014f7a 6281 if (t->opcode_modifier.disp8memshift
1a42a9fe 6282 && i.disp_encoding <= disp_encoding_8bit)
43234a1e 6283 {
a5748e0d 6284 if (i.broadcast.bytes)
4a1b91ea 6285 i.memshift = t->opcode_modifier.broadcast - 1;
7091c612 6286 else if (t->opcode_modifier.disp8memshift != DISP8_SHIFT_VL)
43234a1e 6287 i.memshift = t->opcode_modifier.disp8memshift;
7091c612
JB
6288 else
6289 {
125ff819 6290 const i386_operand_type *type = NULL, *fallback = NULL;
7091c612
JB
6291
6292 i.memshift = 0;
6293 for (op = 0; op < i.operands; op++)
8dc0818e 6294 if (i.flags[op] & Operand_Mem)
7091c612 6295 {
4174bfff
JB
6296 if (t->opcode_modifier.evex == EVEXLIG)
6297 i.memshift = 2 + (i.suffix == QWORD_MNEM_SUFFIX);
6298 else if (t->operand_types[op].bitfield.xmmword
6299 + t->operand_types[op].bitfield.ymmword
6300 + t->operand_types[op].bitfield.zmmword <= 1)
7091c612
JB
6301 type = &t->operand_types[op];
6302 else if (!i.types[op].bitfield.unspecified)
6303 type = &i.types[op];
125ff819
JB
6304 else /* Ambiguities get resolved elsewhere. */
6305 fallback = &t->operand_types[op];
7091c612 6306 }
3528c362 6307 else if (i.types[op].bitfield.class == RegSIMD
4174bfff 6308 && t->opcode_modifier.evex != EVEXLIG)
7091c612
JB
6309 {
6310 if (i.types[op].bitfield.zmmword)
6311 i.memshift = 6;
6312 else if (i.types[op].bitfield.ymmword && i.memshift < 5)
6313 i.memshift = 5;
6314 else if (i.types[op].bitfield.xmmword && i.memshift < 4)
6315 i.memshift = 4;
6316 }
6317
125ff819
JB
6318 if (!type && !i.memshift)
6319 type = fallback;
7091c612
JB
6320 if (type)
6321 {
6322 if (type->bitfield.zmmword)
6323 i.memshift = 6;
6324 else if (type->bitfield.ymmword)
6325 i.memshift = 5;
6326 else if (type->bitfield.xmmword)
6327 i.memshift = 4;
6328 }
6329
6330 /* For the check in fits_in_disp8(). */
6331 if (i.memshift == 0)
6332 i.memshift = -1;
6333 }
43234a1e
L
6334
6335 for (op = 0; op < i.operands; op++)
6336 if (operand_type_check (i.types[op], disp)
6337 && i.op[op].disps->X_op == O_constant)
6338 {
b5014f7a 6339 if (fits_in_disp8 (i.op[op].disps->X_add_number))
43234a1e 6340 {
b5014f7a
JB
6341 i.types[op].bitfield.disp8 = 1;
6342 return 0;
43234a1e 6343 }
b5014f7a 6344 i.types[op].bitfield.disp8 = 0;
43234a1e
L
6345 }
6346 }
b5014f7a
JB
6347
6348 i.memshift = 0;
43234a1e 6349
6c30d220
L
6350 return 0;
6351}
6352
da4977e0 6353/* Check if encoding requirements are met by the instruction. */
a683cc34
SP
6354
6355static int
da4977e0 6356VEX_check_encoding (const insn_template *t)
a683cc34 6357{
da4977e0
JB
6358 if (i.vec_encoding == vex_encoding_error)
6359 {
6360 i.error = unsupported;
6361 return 1;
6362 }
6363
86fa6981 6364 if (i.vec_encoding == vex_encoding_evex)
43234a1e 6365 {
86fa6981 6366 /* This instruction must be encoded with EVEX prefix. */
e771e7c9 6367 if (!is_evex_encoding (t))
86fa6981
L
6368 {
6369 i.error = unsupported;
6370 return 1;
6371 }
6372 return 0;
43234a1e
L
6373 }
6374
a683cc34 6375 if (!t->opcode_modifier.vex)
86fa6981
L
6376 {
6377 /* This instruction template doesn't have VEX prefix. */
6378 if (i.vec_encoding != vex_encoding_default)
6379 {
6380 i.error = unsupported;
6381 return 1;
6382 }
6383 return 0;
6384 }
a683cc34 6385
a683cc34
SP
6386 return 0;
6387}
6388
d3ce72d0 6389static const insn_template *
83b16ac6 6390match_template (char mnem_suffix)
29b0f896
AM
6391{
6392 /* Points to template once we've found it. */
d3ce72d0 6393 const insn_template *t;
40fb9820 6394 i386_operand_type overlap0, overlap1, overlap2, overlap3;
c0f3af97 6395 i386_operand_type overlap4;
29b0f896 6396 unsigned int found_reverse_match;
dc2be329 6397 i386_opcode_modifier suffix_check;
40fb9820 6398 i386_operand_type operand_types [MAX_OPERANDS];
539e75ad 6399 int addr_prefix_disp;
45a4bb20 6400 unsigned int j, size_match, check_register;
5614d22c 6401 enum i386_error specific_error = 0;
29b0f896 6402
c0f3af97
L
6403#if MAX_OPERANDS != 5
6404# error "MAX_OPERANDS must be 5."
f48ff2ae
L
6405#endif
6406
29b0f896 6407 found_reverse_match = 0;
539e75ad 6408 addr_prefix_disp = -1;
40fb9820 6409
dc2be329 6410 /* Prepare for mnemonic suffix check. */
40fb9820 6411 memset (&suffix_check, 0, sizeof (suffix_check));
dc2be329
L
6412 switch (mnem_suffix)
6413 {
6414 case BYTE_MNEM_SUFFIX:
6415 suffix_check.no_bsuf = 1;
6416 break;
6417 case WORD_MNEM_SUFFIX:
6418 suffix_check.no_wsuf = 1;
6419 break;
6420 case SHORT_MNEM_SUFFIX:
6421 suffix_check.no_ssuf = 1;
6422 break;
6423 case LONG_MNEM_SUFFIX:
6424 suffix_check.no_lsuf = 1;
6425 break;
6426 case QWORD_MNEM_SUFFIX:
6427 suffix_check.no_qsuf = 1;
6428 break;
6429 default:
6430 /* NB: In Intel syntax, normally we can check for memory operand
6431 size when there is no mnemonic suffix. But jmp and call have
6432 2 different encodings with Dword memory operand size, one with
6433 No_ldSuf and the other without. i.suffix is set to
6434 LONG_DOUBLE_MNEM_SUFFIX to skip the one with No_ldSuf. */
6435 if (i.suffix == LONG_DOUBLE_MNEM_SUFFIX)
6436 suffix_check.no_ldsuf = 1;
83b16ac6
JB
6437 }
6438
01559ecc
L
6439 /* Must have right number of operands. */
6440 i.error = number_of_operands_mismatch;
6441
45aa61fe 6442 for (t = current_templates->start; t < current_templates->end; t++)
29b0f896 6443 {
539e75ad 6444 addr_prefix_disp = -1;
dbbc8b7e 6445 found_reverse_match = 0;
539e75ad 6446
29b0f896
AM
6447 if (i.operands != t->operands)
6448 continue;
6449
50aecf8c 6450 /* Check processor support. */
a65babc9 6451 i.error = unsupported;
45a4bb20 6452 if (cpu_flags_match (t) != CPU_FLAGS_PERFECT_MATCH)
50aecf8c
L
6453 continue;
6454
57392598
CL
6455 /* Check Pseudo Prefix. */
6456 i.error = unsupported;
6457 if (t->opcode_modifier.pseudovexprefix
6458 && !(i.vec_encoding == vex_encoding_vex
6459 || i.vec_encoding == vex_encoding_vex3))
6460 continue;
6461
e1d4d893 6462 /* Check AT&T mnemonic. */
a65babc9 6463 i.error = unsupported_with_intel_mnemonic;
e1d4d893 6464 if (intel_mnemonic && t->opcode_modifier.attmnemonic)
1efbbeb4
L
6465 continue;
6466
4b5aaf5f 6467 /* Check AT&T/Intel syntax. */
a65babc9 6468 i.error = unsupported_syntax;
5c07affc 6469 if ((intel_syntax && t->opcode_modifier.attsyntax)
4b5aaf5f 6470 || (!intel_syntax && t->opcode_modifier.intelsyntax))
1efbbeb4
L
6471 continue;
6472
4b5aaf5f
L
6473 /* Check Intel64/AMD64 ISA. */
6474 switch (isa64)
6475 {
6476 default:
6477 /* Default: Don't accept Intel64. */
6478 if (t->opcode_modifier.isa64 == INTEL64)
6479 continue;
6480 break;
6481 case amd64:
6482 /* -mamd64: Don't accept Intel64 and Intel64 only. */
6483 if (t->opcode_modifier.isa64 >= INTEL64)
6484 continue;
6485 break;
6486 case intel64:
6487 /* -mintel64: Don't accept AMD64. */
5990e377 6488 if (t->opcode_modifier.isa64 == AMD64 && flag_code == CODE_64BIT)
4b5aaf5f
L
6489 continue;
6490 break;
6491 }
6492
dc2be329 6493 /* Check the suffix. */
a65babc9 6494 i.error = invalid_instruction_suffix;
dc2be329
L
6495 if ((t->opcode_modifier.no_bsuf && suffix_check.no_bsuf)
6496 || (t->opcode_modifier.no_wsuf && suffix_check.no_wsuf)
6497 || (t->opcode_modifier.no_lsuf && suffix_check.no_lsuf)
6498 || (t->opcode_modifier.no_ssuf && suffix_check.no_ssuf)
6499 || (t->opcode_modifier.no_qsuf && suffix_check.no_qsuf)
6500 || (t->opcode_modifier.no_ldsuf && suffix_check.no_ldsuf))
83b16ac6 6501 continue;
29b0f896 6502
3ac21baa
JB
6503 size_match = operand_size_match (t);
6504 if (!size_match)
7d5e4556 6505 continue;
539e75ad 6506
6f2f06be
JB
6507 /* This is intentionally not
6508
0cfa3eb3 6509 if (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE))
6f2f06be
JB
6510
6511 as the case of a missing * on the operand is accepted (perhaps with
6512 a warning, issued further down). */
0cfa3eb3 6513 if (i.jumpabsolute && t->opcode_modifier.jump != JUMP_ABSOLUTE)
6f2f06be
JB
6514 {
6515 i.error = operand_type_mismatch;
6516 continue;
6517 }
6518
5c07affc
L
6519 for (j = 0; j < MAX_OPERANDS; j++)
6520 operand_types[j] = t->operand_types[j];
6521
e365e234
JB
6522 /* In general, don't allow
6523 - 64-bit operands outside of 64-bit mode,
6524 - 32-bit operands on pre-386. */
4873e243 6525 j = i.imm_operands + (t->operands > i.imm_operands + 1);
e365e234
JB
6526 if (((i.suffix == QWORD_MNEM_SUFFIX
6527 && flag_code != CODE_64BIT
389d00a5
JB
6528 && !(t->opcode_modifier.opcodespace == SPACE_0F
6529 && t->base_opcode == 0xc7
5e74b495 6530 && t->opcode_modifier.opcodeprefix == PREFIX_NONE
8b65b895 6531 && t->extension_opcode == 1) /* cmpxchg8b */)
e365e234
JB
6532 || (i.suffix == LONG_MNEM_SUFFIX
6533 && !cpu_arch_flags.bitfield.cpui386))
45aa61fe 6534 && (intel_syntax
3cd7f3e3 6535 ? (t->opcode_modifier.mnemonicsize != IGNORESIZE
45aa61fe
AM
6536 && !intel_float_operand (t->name))
6537 : intel_float_operand (t->name) != 2)
4873e243
JB
6538 && (t->operands == i.imm_operands
6539 || (operand_types[i.imm_operands].bitfield.class != RegMMX
6540 && operand_types[i.imm_operands].bitfield.class != RegSIMD
6541 && operand_types[i.imm_operands].bitfield.class != RegMask)
6542 || (operand_types[j].bitfield.class != RegMMX
6543 && operand_types[j].bitfield.class != RegSIMD
6544 && operand_types[j].bitfield.class != RegMask))
63112cd6 6545 && !t->opcode_modifier.sib)
192dc9c6
JB
6546 continue;
6547
29b0f896 6548 /* Do not verify operands when there are none. */
e365e234 6549 if (!t->operands)
da4977e0
JB
6550 {
6551 if (VEX_check_encoding (t))
6552 {
6553 specific_error = i.error;
6554 continue;
6555 }
6556
6557 /* We've found a match; break out of loop. */
6558 break;
6559 }
252b5132 6560
48bcea9f
JB
6561 if (!t->opcode_modifier.jump
6562 || t->opcode_modifier.jump == JUMP_ABSOLUTE)
6563 {
6564 /* There should be only one Disp operand. */
6565 for (j = 0; j < MAX_OPERANDS; j++)
6566 if (operand_type_check (operand_types[j], disp))
539e75ad 6567 break;
48bcea9f
JB
6568 if (j < MAX_OPERANDS)
6569 {
5b7c81bd 6570 bool override = (i.prefix[ADDR_PREFIX] != 0);
48bcea9f
JB
6571
6572 addr_prefix_disp = j;
6573
a775efc8
JB
6574 /* Address size prefix will turn Disp64 operand into Disp32 and
6575 Disp32/Disp16 one into Disp16/Disp32 respectively. */
48bcea9f 6576 switch (flag_code)
40fb9820 6577 {
48bcea9f
JB
6578 case CODE_16BIT:
6579 override = !override;
6580 /* Fall through. */
6581 case CODE_32BIT:
6582 if (operand_types[j].bitfield.disp32
6583 && operand_types[j].bitfield.disp16)
40fb9820 6584 {
48bcea9f
JB
6585 operand_types[j].bitfield.disp16 = override;
6586 operand_types[j].bitfield.disp32 = !override;
40fb9820 6587 }
a775efc8 6588 gas_assert (!operand_types[j].bitfield.disp64);
48bcea9f
JB
6589 break;
6590
6591 case CODE_64BIT:
a775efc8 6592 if (operand_types[j].bitfield.disp64)
40fb9820 6593 {
a775efc8 6594 gas_assert (!operand_types[j].bitfield.disp32);
48bcea9f 6595 operand_types[j].bitfield.disp32 = override;
a775efc8 6596 operand_types[j].bitfield.disp64 = !override;
40fb9820 6597 }
48bcea9f
JB
6598 operand_types[j].bitfield.disp16 = 0;
6599 break;
40fb9820 6600 }
539e75ad 6601 }
48bcea9f 6602 }
539e75ad 6603
d7e3e627
L
6604 switch (i.reloc[0])
6605 {
6606 case BFD_RELOC_386_GOT32:
6607 /* Force 0x8b encoding for "mov foo@GOT, %eax". */
6608 if (t->base_opcode == 0xa0
6609 && t->opcode_modifier.opcodespace == SPACE_BASE)
6610 continue;
6611 break;
6612 case BFD_RELOC_386_TLS_GOTIE:
6613 case BFD_RELOC_386_TLS_LE_32:
6614 case BFD_RELOC_X86_64_GOTTPOFF:
6615 case BFD_RELOC_X86_64_TLSLD:
6616 /* Don't allow KMOV in TLS code sequences. */
6617 if (t->opcode_modifier.vex)
6618 continue;
6619 break;
6620 default:
6621 break;
6622 }
02a86693 6623
56ffb741 6624 /* We check register size if needed. */
e2195274
JB
6625 if (t->opcode_modifier.checkregsize)
6626 {
6627 check_register = (1 << t->operands) - 1;
a5748e0d 6628 if (i.broadcast.type || i.broadcast.bytes)
5273a3cd 6629 check_register &= ~(1 << i.broadcast.operand);
e2195274
JB
6630 }
6631 else
6632 check_register = 0;
6633
c6fb90c8 6634 overlap0 = operand_type_and (i.types[0], operand_types[0]);
29b0f896
AM
6635 switch (t->operands)
6636 {
6637 case 1:
40fb9820 6638 if (!operand_type_match (overlap0, i.types[0]))
29b0f896
AM
6639 continue;
6640 break;
6641 case 2:
33eaf5de 6642 /* xchg %eax, %eax is a special case. It is an alias for nop
8b38ad71
L
6643 only in 32bit mode and we can use opcode 0x90. In 64bit
6644 mode, we can't use 0x90 for xchg %eax, %eax since it should
6645 zero-extend %eax to %rax. */
6646 if (flag_code == CODE_64BIT
6647 && t->base_opcode == 0x90
35648716 6648 && t->opcode_modifier.opcodespace == SPACE_BASE
75e5731b
JB
6649 && i.types[0].bitfield.instance == Accum
6650 && i.types[0].bitfield.dword
6651 && i.types[1].bitfield.instance == Accum
6652 && i.types[1].bitfield.dword)
8b38ad71 6653 continue;
1212781b
JB
6654 /* xrelease mov %eax, <disp> is another special case. It must not
6655 match the accumulator-only encoding of mov. */
6656 if (flag_code != CODE_64BIT
6657 && i.hle_prefix
6658 && t->base_opcode == 0xa0
35648716 6659 && t->opcode_modifier.opcodespace == SPACE_BASE
75e5731b 6660 && i.types[0].bitfield.instance == Accum
8dc0818e 6661 && (i.flags[1] & Operand_Mem))
1212781b 6662 continue;
f5eb1d70
JB
6663 /* Fall through. */
6664
6665 case 3:
3ac21baa
JB
6666 if (!(size_match & MATCH_STRAIGHT))
6667 goto check_reverse;
64c49ab3
JB
6668 /* Reverse direction of operands if swapping is possible in the first
6669 place (operands need to be symmetric) and
6670 - the load form is requested, and the template is a store form,
6671 - the store form is requested, and the template is a load form,
6672 - the non-default (swapped) form is requested. */
6673 overlap1 = operand_type_and (operand_types[0], operand_types[1]);
f5eb1d70 6674 if (t->opcode_modifier.d && i.reg_operands == i.operands
64c49ab3
JB
6675 && !operand_type_all_zero (&overlap1))
6676 switch (i.dir_encoding)
6677 {
6678 case dir_encoding_load:
6679 if (operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 6680 || t->opcode_modifier.regmem)
64c49ab3
JB
6681 goto check_reverse;
6682 break;
6683
6684 case dir_encoding_store:
6685 if (!operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 6686 && !t->opcode_modifier.regmem)
64c49ab3
JB
6687 goto check_reverse;
6688 break;
6689
6690 case dir_encoding_swap:
6691 goto check_reverse;
6692
6693 case dir_encoding_default:
6694 break;
6695 }
86fa6981 6696 /* If we want store form, we skip the current load. */
64c49ab3
JB
6697 if ((i.dir_encoding == dir_encoding_store
6698 || i.dir_encoding == dir_encoding_swap)
86fa6981
L
6699 && i.mem_operands == 0
6700 && t->opcode_modifier.load)
fa99fab2 6701 continue;
1a0670f3 6702 /* Fall through. */
f48ff2ae 6703 case 4:
c0f3af97 6704 case 5:
c6fb90c8 6705 overlap1 = operand_type_and (i.types[1], operand_types[1]);
40fb9820
L
6706 if (!operand_type_match (overlap0, i.types[0])
6707 || !operand_type_match (overlap1, i.types[1])
e2195274 6708 || ((check_register & 3) == 3
dc821c5f 6709 && !operand_type_register_match (i.types[0],
40fb9820 6710 operand_types[0],
dc821c5f 6711 i.types[1],
40fb9820 6712 operand_types[1])))
29b0f896
AM
6713 {
6714 /* Check if other direction is valid ... */
38e314eb 6715 if (!t->opcode_modifier.d)
29b0f896
AM
6716 continue;
6717
dc1e8a47 6718 check_reverse:
3ac21baa
JB
6719 if (!(size_match & MATCH_REVERSE))
6720 continue;
29b0f896 6721 /* Try reversing direction of operands. */
8bd915b7
JB
6722 j = t->opcode_modifier.vexsources ? 1 : i.operands - 1;
6723 overlap0 = operand_type_and (i.types[0], operand_types[j]);
6724 overlap1 = operand_type_and (i.types[j], operand_types[0]);
c975cec5
JB
6725 overlap2 = operand_type_and (i.types[1], operand_types[1]);
6726 gas_assert (t->operands != 3 || !check_register);
40fb9820 6727 if (!operand_type_match (overlap0, i.types[0])
8bd915b7 6728 || !operand_type_match (overlap1, i.types[j])
c975cec5
JB
6729 || (t->operands == 3
6730 && !operand_type_match (overlap2, i.types[1]))
45664ddb 6731 || (check_register
dc821c5f 6732 && !operand_type_register_match (i.types[0],
8bd915b7
JB
6733 operand_types[j],
6734 i.types[j],
45664ddb 6735 operand_types[0])))
29b0f896
AM
6736 {
6737 /* Does not match either direction. */
6738 continue;
6739 }
38e314eb 6740 /* found_reverse_match holds which of D or FloatR
29b0f896 6741 we've found. */
38e314eb
JB
6742 if (!t->opcode_modifier.d)
6743 found_reverse_match = 0;
6744 else if (operand_types[0].bitfield.tbyte)
8a2ed489 6745 found_reverse_match = Opcode_FloatD;
8bd915b7
JB
6746 else if (t->opcode_modifier.vexsources)
6747 {
6748 found_reverse_match = Opcode_VexW;
6749 goto check_operands_345;
6750 }
2c735193
JB
6751 else if (t->opcode_modifier.opcodespace != SPACE_BASE
6752 && (t->opcode_modifier.opcodespace != SPACE_0F
6753 /* MOV to/from CR/DR/TR, as an exception, follow
6754 the base opcode space encoding model. */
6755 || (t->base_opcode | 7) != 0x27))
dbbc8b7e 6756 found_reverse_match = (t->base_opcode & 0xee) != 0x6e
2c735193 6757 ? Opcode_ExtD : Opcode_SIMD_IntD;
8a2ed489 6758 else
38e314eb 6759 found_reverse_match = Opcode_D;
40fb9820 6760 if (t->opcode_modifier.floatr)
8a2ed489 6761 found_reverse_match |= Opcode_FloatR;
29b0f896 6762 }
f48ff2ae 6763 else
29b0f896 6764 {
f48ff2ae 6765 /* Found a forward 2 operand match here. */
8bd915b7 6766 check_operands_345:
d1cbb4db
L
6767 switch (t->operands)
6768 {
c0f3af97 6769 case 5:
3d0738af 6770 overlap4 = operand_type_and (i.types[4], operand_types[4]);
c0f3af97 6771 if (!operand_type_match (overlap4, i.types[4])
dc821c5f 6772 || !operand_type_register_match (i.types[3],
c0f3af97 6773 operand_types[3],
c0f3af97
L
6774 i.types[4],
6775 operand_types[4]))
6776 continue;
1a0670f3 6777 /* Fall through. */
f48ff2ae 6778 case 4:
3d0738af 6779 overlap3 = operand_type_and (i.types[3], operand_types[3]);
40fb9820 6780 if (!operand_type_match (overlap3, i.types[3])
e2195274
JB
6781 || ((check_register & 0xa) == 0xa
6782 && !operand_type_register_match (i.types[1],
f7768225
JB
6783 operand_types[1],
6784 i.types[3],
e2195274
JB
6785 operand_types[3]))
6786 || ((check_register & 0xc) == 0xc
6787 && !operand_type_register_match (i.types[2],
6788 operand_types[2],
6789 i.types[3],
6790 operand_types[3])))
f48ff2ae 6791 continue;
1a0670f3 6792 /* Fall through. */
f48ff2ae 6793 case 3:
3d0738af 6794 overlap2 = operand_type_and (i.types[2], operand_types[2]);
40fb9820 6795 if (!operand_type_match (overlap2, i.types[2])
e2195274
JB
6796 || ((check_register & 5) == 5
6797 && !operand_type_register_match (i.types[0],
23e42951
JB
6798 operand_types[0],
6799 i.types[2],
e2195274
JB
6800 operand_types[2]))
6801 || ((check_register & 6) == 6
6802 && !operand_type_register_match (i.types[1],
6803 operand_types[1],
6804 i.types[2],
6805 operand_types[2])))
f48ff2ae
L
6806 continue;
6807 break;
6808 }
29b0f896 6809 }
f48ff2ae 6810 /* Found either forward/reverse 2, 3 or 4 operand match here:
29b0f896
AM
6811 slip through to break. */
6812 }
c0f3af97 6813
da4977e0
JB
6814 /* Check if vector operands are valid. */
6815 if (check_VecOperands (t))
6816 {
6817 specific_error = i.error;
6818 continue;
6819 }
6820
6821 /* Check if VEX/EVEX encoding requirements can be satisfied. */
6822 if (VEX_check_encoding (t))
5614d22c
JB
6823 {
6824 specific_error = i.error;
6825 continue;
6826 }
a683cc34 6827
29b0f896
AM
6828 /* We've found a match; break out of loop. */
6829 break;
6830 }
6831
6832 if (t == current_templates->end)
6833 {
6834 /* We found no match. */
a65babc9 6835 const char *err_msg;
5614d22c 6836 switch (specific_error ? specific_error : i.error)
a65babc9
L
6837 {
6838 default:
6839 abort ();
86e026a4 6840 case operand_size_mismatch:
a65babc9
L
6841 err_msg = _("operand size mismatch");
6842 break;
6843 case operand_type_mismatch:
6844 err_msg = _("operand type mismatch");
6845 break;
6846 case register_type_mismatch:
6847 err_msg = _("register type mismatch");
6848 break;
6849 case number_of_operands_mismatch:
6850 err_msg = _("number of operands mismatch");
6851 break;
6852 case invalid_instruction_suffix:
6853 err_msg = _("invalid instruction suffix");
6854 break;
6855 case bad_imm4:
4a2608e3 6856 err_msg = _("constant doesn't fit in 4 bits");
a65babc9 6857 break;
a65babc9
L
6858 case unsupported_with_intel_mnemonic:
6859 err_msg = _("unsupported with Intel mnemonic");
6860 break;
6861 case unsupported_syntax:
6862 err_msg = _("unsupported syntax");
6863 break;
6864 case unsupported:
35262a23 6865 as_bad (_("unsupported instruction `%s'"),
10efe3f6
L
6866 current_templates->start->name);
6867 return NULL;
260cd341
LC
6868 case invalid_sib_address:
6869 err_msg = _("invalid SIB address");
6870 break;
6c30d220
L
6871 case invalid_vsib_address:
6872 err_msg = _("invalid VSIB address");
6873 break;
7bab8ab5
JB
6874 case invalid_vector_register_set:
6875 err_msg = _("mask, index, and destination registers must be distinct");
6876 break;
260cd341
LC
6877 case invalid_tmm_register_set:
6878 err_msg = _("all tmm registers must be distinct");
6879 break;
0cc78721
CL
6880 case invalid_dest_and_src_register_set:
6881 err_msg = _("destination and source registers must be distinct");
6882 break;
6c30d220
L
6883 case unsupported_vector_index_register:
6884 err_msg = _("unsupported vector index register");
6885 break;
43234a1e
L
6886 case unsupported_broadcast:
6887 err_msg = _("unsupported broadcast");
6888 break;
43234a1e
L
6889 case broadcast_needed:
6890 err_msg = _("broadcast is needed for operand of such type");
6891 break;
6892 case unsupported_masking:
6893 err_msg = _("unsupported masking");
6894 break;
6895 case mask_not_on_destination:
6896 err_msg = _("mask not on destination operand");
6897 break;
6898 case no_default_mask:
6899 err_msg = _("default mask isn't allowed");
6900 break;
6901 case unsupported_rc_sae:
6902 err_msg = _("unsupported static rounding/sae");
6903 break;
43234a1e
L
6904 case invalid_register_operand:
6905 err_msg = _("invalid register operand");
6906 break;
a65babc9
L
6907 }
6908 as_bad (_("%s for `%s'"), err_msg,
891edac4 6909 current_templates->start->name);
fa99fab2 6910 return NULL;
29b0f896 6911 }
252b5132 6912
29b0f896
AM
6913 if (!quiet_warnings)
6914 {
6915 if (!intel_syntax
0cfa3eb3 6916 && (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE)))
6f2f06be 6917 as_warn (_("indirect %s without `*'"), t->name);
29b0f896 6918
40fb9820 6919 if (t->opcode_modifier.isprefix
3cd7f3e3 6920 && t->opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
6921 {
6922 /* Warn them that a data or address size prefix doesn't
6923 affect assembly of the next line of code. */
6924 as_warn (_("stand-alone `%s' prefix"), t->name);
6925 }
6926 }
6927
6928 /* Copy the template we found. */
9a182d04 6929 install_template (t);
539e75ad
L
6930
6931 if (addr_prefix_disp != -1)
6932 i.tm.operand_types[addr_prefix_disp]
6933 = operand_types[addr_prefix_disp];
6934
8bd915b7 6935 switch (found_reverse_match)
29b0f896 6936 {
8bd915b7
JB
6937 case 0:
6938 break;
6939
6940 default:
dfd69174
JB
6941 /* If we found a reverse match we must alter the opcode direction
6942 bit and clear/flip the regmem modifier one. found_reverse_match
6943 holds bits to change (different for int & float insns). */
29b0f896
AM
6944
6945 i.tm.base_opcode ^= found_reverse_match;
6946
f5eb1d70
JB
6947 i.tm.operand_types[0] = operand_types[i.operands - 1];
6948 i.tm.operand_types[i.operands - 1] = operand_types[0];
dfd69174
JB
6949
6950 /* Certain SIMD insns have their load forms specified in the opcode
6951 table, and hence we need to _set_ RegMem instead of clearing it.
6952 We need to avoid setting the bit though on insns like KMOVW. */
6953 i.tm.opcode_modifier.regmem
6954 = i.tm.opcode_modifier.modrm && i.tm.opcode_modifier.d
6955 && i.tm.operands > 2U - i.tm.opcode_modifier.sse2avx
6956 && !i.tm.opcode_modifier.regmem;
8bd915b7
JB
6957 break;
6958
6959 case Opcode_VexW:
6960 /* Only the first two register operands need reversing, alongside
6961 flipping VEX.W. */
6962 i.tm.opcode_modifier.vexw ^= VEXW0 ^ VEXW1;
6963
6964 j = i.tm.operand_types[0].bitfield.imm8;
6965 i.tm.operand_types[j] = operand_types[j + 1];
6966 i.tm.operand_types[j + 1] = operand_types[j];
6967 break;
29b0f896
AM
6968 }
6969
fa99fab2 6970 return t;
29b0f896
AM
6971}
6972
6973static int
e3bb37b5 6974check_string (void)
29b0f896 6975{
51c8edf6
JB
6976 unsigned int es_op = i.tm.opcode_modifier.isstring - IS_STRING_ES_OP0;
6977 unsigned int op = i.tm.operand_types[0].bitfield.baseindex ? es_op : 0;
8dc0818e 6978
5e042380 6979 if (i.seg[op] != NULL && i.seg[op] != reg_es)
29b0f896 6980 {
51c8edf6
JB
6981 as_bad (_("`%s' operand %u must use `%ses' segment"),
6982 i.tm.name,
6983 intel_syntax ? i.tm.operands - es_op : es_op + 1,
6984 register_prefix);
6985 return 0;
29b0f896 6986 }
51c8edf6
JB
6987
6988 /* There's only ever one segment override allowed per instruction.
6989 This instruction possibly has a legal segment override on the
6990 second operand, so copy the segment to where non-string
6991 instructions store it, allowing common code. */
6992 i.seg[op] = i.seg[1];
6993
29b0f896
AM
6994 return 1;
6995}
6996
6997static int
543613e9 6998process_suffix (void)
29b0f896 6999{
5b7c81bd 7000 bool is_crc32 = false, is_movx = false;
8b65b895 7001
29b0f896
AM
7002 /* If matched instruction specifies an explicit instruction mnemonic
7003 suffix, use it. */
673fe0f0 7004 if (i.tm.opcode_modifier.size == SIZE16)
40fb9820 7005 i.suffix = WORD_MNEM_SUFFIX;
673fe0f0 7006 else if (i.tm.opcode_modifier.size == SIZE32)
40fb9820 7007 i.suffix = LONG_MNEM_SUFFIX;
673fe0f0 7008 else if (i.tm.opcode_modifier.size == SIZE64)
40fb9820 7009 i.suffix = QWORD_MNEM_SUFFIX;
13e600d0 7010 else if (i.reg_operands
c8f8eebc
JB
7011 && (i.operands > 1 || i.types[0].bitfield.class == Reg)
7012 && !i.tm.opcode_modifier.addrprefixopreg)
29b0f896 7013 {
65fca059 7014 unsigned int numop = i.operands;
389d00a5
JB
7015
7016 /* MOVSX/MOVZX */
7017 is_movx = (i.tm.opcode_modifier.opcodespace == SPACE_0F
7018 && (i.tm.base_opcode | 8) == 0xbe)
7019 || (i.tm.opcode_modifier.opcodespace == SPACE_BASE
7020 && i.tm.base_opcode == 0x63
7021 && i.tm.cpu_flags.bitfield.cpu64);
7022
8b65b895 7023 /* CRC32 */
389d00a5
JB
7024 is_crc32 = (i.tm.base_opcode == 0xf0
7025 && i.tm.opcode_modifier.opcodespace == SPACE_0F38
8b65b895 7026 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2);
65fca059
JB
7027
7028 /* movsx/movzx want only their source operand considered here, for the
7029 ambiguity checking below. The suffix will be replaced afterwards
7030 to represent the destination (register). */
389d00a5 7031 if (is_movx && (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63))
65fca059
JB
7032 --i.operands;
7033
643bb870 7034 /* crc32 needs REX.W set regardless of suffix / source operand size. */
8b65b895 7035 if (is_crc32 && i.tm.operand_types[1].bitfield.qword)
643bb870
JB
7036 i.rex |= REX_W;
7037
29b0f896 7038 /* If there's no instruction mnemonic suffix we try to invent one
13e600d0 7039 based on GPR operands. */
29b0f896
AM
7040 if (!i.suffix)
7041 {
7042 /* We take i.suffix from the last register operand specified,
7043 Destination register type is more significant than source
381d071f
L
7044 register type. crc32 in SSE4.2 prefers source register
7045 type. */
8b65b895 7046 unsigned int op = is_crc32 ? 1 : i.operands;
20592a94 7047
1a035124
JB
7048 while (op--)
7049 if (i.tm.operand_types[op].bitfield.instance == InstanceNone
7050 || i.tm.operand_types[op].bitfield.instance == Accum)
7051 {
7052 if (i.types[op].bitfield.class != Reg)
7053 continue;
7054 if (i.types[op].bitfield.byte)
7055 i.suffix = BYTE_MNEM_SUFFIX;
7056 else if (i.types[op].bitfield.word)
7057 i.suffix = WORD_MNEM_SUFFIX;
7058 else if (i.types[op].bitfield.dword)
7059 i.suffix = LONG_MNEM_SUFFIX;
7060 else if (i.types[op].bitfield.qword)
7061 i.suffix = QWORD_MNEM_SUFFIX;
7062 else
7063 continue;
7064 break;
7065 }
65fca059
JB
7066
7067 /* As an exception, movsx/movzx silently default to a byte source
7068 in AT&T mode. */
389d00a5 7069 if (is_movx && i.tm.opcode_modifier.w && !i.suffix && !intel_syntax)
65fca059 7070 i.suffix = BYTE_MNEM_SUFFIX;
29b0f896
AM
7071 }
7072 else if (i.suffix == BYTE_MNEM_SUFFIX)
7073 {
1cb0ab18 7074 if (!check_byte_reg ())
29b0f896
AM
7075 return 0;
7076 }
7077 else if (i.suffix == LONG_MNEM_SUFFIX)
7078 {
1cb0ab18 7079 if (!check_long_reg ())
29b0f896
AM
7080 return 0;
7081 }
7082 else if (i.suffix == QWORD_MNEM_SUFFIX)
7083 {
1cb0ab18 7084 if (!check_qword_reg ())
29b0f896
AM
7085 return 0;
7086 }
7087 else if (i.suffix == WORD_MNEM_SUFFIX)
7088 {
1cb0ab18 7089 if (!check_word_reg ())
29b0f896
AM
7090 return 0;
7091 }
3cd7f3e3
L
7092 else if (intel_syntax
7093 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
7094 /* Do nothing if the instruction is going to ignore the prefix. */
7095 ;
7096 else
7097 abort ();
65fca059
JB
7098
7099 /* Undo the movsx/movzx change done above. */
7100 i.operands = numop;
29b0f896 7101 }
3cd7f3e3
L
7102 else if (i.tm.opcode_modifier.mnemonicsize == DEFAULTSIZE
7103 && !i.suffix)
29b0f896 7104 {
13e600d0
JB
7105 i.suffix = stackop_size;
7106 if (stackop_size == LONG_MNEM_SUFFIX)
06f74c5c
L
7107 {
7108 /* stackop_size is set to LONG_MNEM_SUFFIX for the
7109 .code16gcc directive to support 16-bit mode with
7110 32-bit address. For IRET without a suffix, generate
7111 16-bit IRET (opcode 0xcf) to return from an interrupt
7112 handler. */
13e600d0
JB
7113 if (i.tm.base_opcode == 0xcf)
7114 {
7115 i.suffix = WORD_MNEM_SUFFIX;
7116 as_warn (_("generating 16-bit `iret' for .code16gcc directive"));
7117 }
7118 /* Warn about changed behavior for segment register push/pop. */
7119 else if ((i.tm.base_opcode | 1) == 0x07)
7120 as_warn (_("generating 32-bit `%s', unlike earlier gas versions"),
7121 i.tm.name);
06f74c5c 7122 }
29b0f896 7123 }
c006a730 7124 else if (!i.suffix
0cfa3eb3
JB
7125 && (i.tm.opcode_modifier.jump == JUMP_ABSOLUTE
7126 || i.tm.opcode_modifier.jump == JUMP_BYTE
7127 || i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT
389d00a5
JB
7128 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
7129 && i.tm.base_opcode == 0x01 /* [ls][gi]dt */
64e74474 7130 && i.tm.extension_opcode <= 3)))
9306ca4a
JB
7131 {
7132 switch (flag_code)
7133 {
7134 case CODE_64BIT:
40fb9820 7135 if (!i.tm.opcode_modifier.no_qsuf)
9306ca4a 7136 {
828c2a25
JB
7137 if (i.tm.opcode_modifier.jump == JUMP_BYTE
7138 || i.tm.opcode_modifier.no_lsuf)
7139 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a
JB
7140 break;
7141 }
1a0670f3 7142 /* Fall through. */
9306ca4a 7143 case CODE_32BIT:
40fb9820 7144 if (!i.tm.opcode_modifier.no_lsuf)
9306ca4a
JB
7145 i.suffix = LONG_MNEM_SUFFIX;
7146 break;
7147 case CODE_16BIT:
40fb9820 7148 if (!i.tm.opcode_modifier.no_wsuf)
9306ca4a
JB
7149 i.suffix = WORD_MNEM_SUFFIX;
7150 break;
7151 }
7152 }
252b5132 7153
c006a730 7154 if (!i.suffix
3cd7f3e3 7155 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8
JB
7156 /* Also cover lret/retf/iret in 64-bit mode. */
7157 || (flag_code == CODE_64BIT
7158 && !i.tm.opcode_modifier.no_lsuf
7159 && !i.tm.opcode_modifier.no_qsuf))
3cd7f3e3 7160 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
8bbb3ad8
JB
7161 /* Explicit sizing prefixes are assumed to disambiguate insns. */
7162 && !i.prefix[DATA_PREFIX] && !(i.prefix[REX_PREFIX] & REX_W)
62b3f548
JB
7163 /* Accept FLDENV et al without suffix. */
7164 && (i.tm.opcode_modifier.no_ssuf || i.tm.opcode_modifier.floatmf))
29b0f896 7165 {
6c0946d0 7166 unsigned int suffixes, evex = 0;
c006a730
JB
7167
7168 suffixes = !i.tm.opcode_modifier.no_bsuf;
7169 if (!i.tm.opcode_modifier.no_wsuf)
7170 suffixes |= 1 << 1;
7171 if (!i.tm.opcode_modifier.no_lsuf)
7172 suffixes |= 1 << 2;
7173 if (!i.tm.opcode_modifier.no_ldsuf)
7174 suffixes |= 1 << 3;
7175 if (!i.tm.opcode_modifier.no_ssuf)
7176 suffixes |= 1 << 4;
7177 if (flag_code == CODE_64BIT && !i.tm.opcode_modifier.no_qsuf)
7178 suffixes |= 1 << 5;
7179
6c0946d0
JB
7180 /* For [XYZ]MMWORD operands inspect operand sizes. While generally
7181 also suitable for AT&T syntax mode, it was requested that this be
7182 restricted to just Intel syntax. */
a5748e0d
JB
7183 if (intel_syntax && is_any_vex_encoding (&i.tm)
7184 && !i.broadcast.type && !i.broadcast.bytes)
6c0946d0 7185 {
b9915cbc 7186 unsigned int op;
6c0946d0 7187
b9915cbc 7188 for (op = 0; op < i.tm.operands; ++op)
6c0946d0 7189 {
b9915cbc
JB
7190 if (is_evex_encoding (&i.tm)
7191 && !cpu_arch_flags.bitfield.cpuavx512vl)
6c0946d0 7192 {
b9915cbc
JB
7193 if (i.tm.operand_types[op].bitfield.ymmword)
7194 i.tm.operand_types[op].bitfield.xmmword = 0;
7195 if (i.tm.operand_types[op].bitfield.zmmword)
7196 i.tm.operand_types[op].bitfield.ymmword = 0;
7197 if (!i.tm.opcode_modifier.evex
7198 || i.tm.opcode_modifier.evex == EVEXDYN)
7199 i.tm.opcode_modifier.evex = EVEX512;
7200 }
6c0946d0 7201
b9915cbc
JB
7202 if (i.tm.operand_types[op].bitfield.xmmword
7203 + i.tm.operand_types[op].bitfield.ymmword
7204 + i.tm.operand_types[op].bitfield.zmmword < 2)
7205 continue;
6c0946d0 7206
b9915cbc
JB
7207 /* Any properly sized operand disambiguates the insn. */
7208 if (i.types[op].bitfield.xmmword
7209 || i.types[op].bitfield.ymmword
7210 || i.types[op].bitfield.zmmword)
7211 {
7212 suffixes &= ~(7 << 6);
7213 evex = 0;
7214 break;
7215 }
6c0946d0 7216
b9915cbc
JB
7217 if ((i.flags[op] & Operand_Mem)
7218 && i.tm.operand_types[op].bitfield.unspecified)
7219 {
7220 if (i.tm.operand_types[op].bitfield.xmmword)
7221 suffixes |= 1 << 6;
7222 if (i.tm.operand_types[op].bitfield.ymmword)
7223 suffixes |= 1 << 7;
7224 if (i.tm.operand_types[op].bitfield.zmmword)
7225 suffixes |= 1 << 8;
7226 if (is_evex_encoding (&i.tm))
7227 evex = EVEX512;
6c0946d0
JB
7228 }
7229 }
7230 }
7231
7232 /* Are multiple suffixes / operand sizes allowed? */
c006a730 7233 if (suffixes & (suffixes - 1))
9306ca4a 7234 {
873494c8 7235 if (intel_syntax
3cd7f3e3 7236 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8 7237 || operand_check == check_error))
9306ca4a 7238 {
c006a730 7239 as_bad (_("ambiguous operand size for `%s'"), i.tm.name);
9306ca4a
JB
7240 return 0;
7241 }
c006a730 7242 if (operand_check == check_error)
9306ca4a 7243 {
c006a730
JB
7244 as_bad (_("no instruction mnemonic suffix given and "
7245 "no register operands; can't size `%s'"), i.tm.name);
9306ca4a
JB
7246 return 0;
7247 }
c006a730 7248 if (operand_check == check_warning)
873494c8
JB
7249 as_warn (_("%s; using default for `%s'"),
7250 intel_syntax
7251 ? _("ambiguous operand size")
7252 : _("no instruction mnemonic suffix given and "
7253 "no register operands"),
7254 i.tm.name);
c006a730
JB
7255
7256 if (i.tm.opcode_modifier.floatmf)
7257 i.suffix = SHORT_MNEM_SUFFIX;
389d00a5 7258 else if (is_movx)
65fca059 7259 /* handled below */;
6c0946d0
JB
7260 else if (evex)
7261 i.tm.opcode_modifier.evex = evex;
c006a730
JB
7262 else if (flag_code == CODE_16BIT)
7263 i.suffix = WORD_MNEM_SUFFIX;
1a035124 7264 else if (!i.tm.opcode_modifier.no_lsuf)
c006a730 7265 i.suffix = LONG_MNEM_SUFFIX;
1a035124
JB
7266 else
7267 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a 7268 }
29b0f896 7269 }
252b5132 7270
389d00a5 7271 if (is_movx)
65fca059
JB
7272 {
7273 /* In Intel syntax, movsx/movzx must have a "suffix" (checked above).
7274 In AT&T syntax, if there is no suffix (warned about above), the default
7275 will be byte extension. */
7276 if (i.tm.opcode_modifier.w && i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
7277 i.tm.base_opcode |= 1;
7278
7279 /* For further processing, the suffix should represent the destination
7280 (register). This is already the case when one was used with
7281 mov[sz][bw]*, but we need to replace it for mov[sz]x, or if there was
7282 no suffix to begin with. */
7283 if (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63 || !i.suffix)
7284 {
7285 if (i.types[1].bitfield.word)
7286 i.suffix = WORD_MNEM_SUFFIX;
7287 else if (i.types[1].bitfield.qword)
7288 i.suffix = QWORD_MNEM_SUFFIX;
7289 else
7290 i.suffix = LONG_MNEM_SUFFIX;
7291
7292 i.tm.opcode_modifier.w = 0;
7293 }
7294 }
7295
50128d0c
JB
7296 if (!i.tm.opcode_modifier.modrm && i.reg_operands && i.tm.operands < 3)
7297 i.short_form = (i.tm.operand_types[0].bitfield.class == Reg)
7298 != (i.tm.operand_types[1].bitfield.class == Reg);
7299
d2224064
JB
7300 /* Change the opcode based on the operand size given by i.suffix. */
7301 switch (i.suffix)
29b0f896 7302 {
d2224064
JB
7303 /* Size floating point instruction. */
7304 case LONG_MNEM_SUFFIX:
7305 if (i.tm.opcode_modifier.floatmf)
7306 {
7307 i.tm.base_opcode ^= 4;
7308 break;
7309 }
7310 /* fall through */
7311 case WORD_MNEM_SUFFIX:
7312 case QWORD_MNEM_SUFFIX:
29b0f896 7313 /* It's not a byte, select word/dword operation. */
40fb9820 7314 if (i.tm.opcode_modifier.w)
29b0f896 7315 {
50128d0c 7316 if (i.short_form)
29b0f896
AM
7317 i.tm.base_opcode |= 8;
7318 else
7319 i.tm.base_opcode |= 1;
7320 }
d2224064
JB
7321 /* fall through */
7322 case SHORT_MNEM_SUFFIX:
29b0f896
AM
7323 /* Now select between word & dword operations via the operand
7324 size prefix, except for instructions that will ignore this
7325 prefix anyway. */
c8f8eebc 7326 if (i.suffix != QWORD_MNEM_SUFFIX
3cd7f3e3 7327 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
c8f8eebc
JB
7328 && !i.tm.opcode_modifier.floatmf
7329 && !is_any_vex_encoding (&i.tm)
7330 && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
7331 || (flag_code == CODE_64BIT
7332 && i.tm.opcode_modifier.jump == JUMP_BYTE)))
24eab124
AM
7333 {
7334 unsigned int prefix = DATA_PREFIX_OPCODE;
543613e9 7335
0cfa3eb3 7336 if (i.tm.opcode_modifier.jump == JUMP_BYTE) /* jcxz, loop */
29b0f896 7337 prefix = ADDR_PREFIX_OPCODE;
252b5132 7338
29b0f896
AM
7339 if (!add_prefix (prefix))
7340 return 0;
24eab124 7341 }
252b5132 7342
29b0f896
AM
7343 /* Set mode64 for an operand. */
7344 if (i.suffix == QWORD_MNEM_SUFFIX
9146926a 7345 && flag_code == CODE_64BIT
d2224064 7346 && !i.tm.opcode_modifier.norex64
4ed21b58 7347 && !i.tm.opcode_modifier.vexw
46e883c5 7348 /* Special case for xchg %rax,%rax. It is NOP and doesn't
d2224064
JB
7349 need rex64. */
7350 && ! (i.operands == 2
7351 && i.tm.base_opcode == 0x90
7352 && i.tm.extension_opcode == None
75e5731b
JB
7353 && i.types[0].bitfield.instance == Accum
7354 && i.types[0].bitfield.qword
7355 && i.types[1].bitfield.instance == Accum
7356 && i.types[1].bitfield.qword))
d2224064 7357 i.rex |= REX_W;
3e73aa7c 7358
d2224064 7359 break;
8bbb3ad8
JB
7360
7361 case 0:
f9a6a8f0 7362 /* Select word/dword/qword operation with explicit data sizing prefix
8bbb3ad8
JB
7363 when there are no suitable register operands. */
7364 if (i.tm.opcode_modifier.w
7365 && (i.prefix[DATA_PREFIX] || (i.prefix[REX_PREFIX] & REX_W))
7366 && (!i.reg_operands
7367 || (i.reg_operands == 1
7368 /* ShiftCount */
7369 && (i.tm.operand_types[0].bitfield.instance == RegC
7370 /* InOutPortReg */
7371 || i.tm.operand_types[0].bitfield.instance == RegD
7372 || i.tm.operand_types[1].bitfield.instance == RegD
7373 /* CRC32 */
8b65b895 7374 || is_crc32))))
8bbb3ad8
JB
7375 i.tm.base_opcode |= 1;
7376 break;
29b0f896 7377 }
7ecd2f8b 7378
c8f8eebc 7379 if (i.tm.opcode_modifier.addrprefixopreg)
c0a30a9f 7380 {
c8f8eebc
JB
7381 gas_assert (!i.suffix);
7382 gas_assert (i.reg_operands);
c0a30a9f 7383
c8f8eebc
JB
7384 if (i.tm.operand_types[0].bitfield.instance == Accum
7385 || i.operands == 1)
7386 {
7387 /* The address size override prefix changes the size of the
7388 first operand. */
7389 if (flag_code == CODE_64BIT
7390 && i.op[0].regs->reg_type.bitfield.word)
7391 {
7392 as_bad (_("16-bit addressing unavailable for `%s'"),
7393 i.tm.name);
7394 return 0;
7395 }
7396
7397 if ((flag_code == CODE_32BIT
7398 ? i.op[0].regs->reg_type.bitfield.word
7399 : i.op[0].regs->reg_type.bitfield.dword)
7400 && !add_prefix (ADDR_PREFIX_OPCODE))
7401 return 0;
7402 }
c0a30a9f
L
7403 else
7404 {
c8f8eebc
JB
7405 /* Check invalid register operand when the address size override
7406 prefix changes the size of register operands. */
7407 unsigned int op;
7408 enum { need_word, need_dword, need_qword } need;
7409
27f13469 7410 /* Check the register operand for the address size prefix if
b3a3496f 7411 the memory operand has no real registers, like symbol, DISP
829f3fe1 7412 or bogus (x32-only) symbol(%rip) when symbol(%eip) is meant. */
27f13469
L
7413 if (i.mem_operands == 1
7414 && i.reg_operands == 1
7415 && i.operands == 2
27f13469 7416 && i.types[1].bitfield.class == Reg
b3a3496f
L
7417 && (flag_code == CODE_32BIT
7418 ? i.op[1].regs->reg_type.bitfield.word
7419 : i.op[1].regs->reg_type.bitfield.dword)
7420 && ((i.base_reg == NULL && i.index_reg == NULL)
829f3fe1
JB
7421#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
7422 || (x86_elf_abi == X86_64_X32_ABI
7423 && i.base_reg
b3a3496f
L
7424 && i.base_reg->reg_num == RegIP
7425 && i.base_reg->reg_type.bitfield.qword))
829f3fe1
JB
7426#else
7427 || 0)
7428#endif
27f13469
L
7429 && !add_prefix (ADDR_PREFIX_OPCODE))
7430 return 0;
7431
c8f8eebc
JB
7432 if (flag_code == CODE_32BIT)
7433 need = i.prefix[ADDR_PREFIX] ? need_word : need_dword;
7434 else if (i.prefix[ADDR_PREFIX])
c0a30a9f
L
7435 need = need_dword;
7436 else
7437 need = flag_code == CODE_64BIT ? need_qword : need_word;
c0a30a9f 7438
c8f8eebc
JB
7439 for (op = 0; op < i.operands; op++)
7440 {
7441 if (i.types[op].bitfield.class != Reg)
7442 continue;
7443
7444 switch (need)
7445 {
7446 case need_word:
7447 if (i.op[op].regs->reg_type.bitfield.word)
7448 continue;
7449 break;
7450 case need_dword:
7451 if (i.op[op].regs->reg_type.bitfield.dword)
7452 continue;
7453 break;
7454 case need_qword:
7455 if (i.op[op].regs->reg_type.bitfield.qword)
7456 continue;
7457 break;
7458 }
7459
7460 as_bad (_("invalid register operand size for `%s'"),
7461 i.tm.name);
7462 return 0;
7463 }
7464 }
c0a30a9f
L
7465 }
7466
29b0f896
AM
7467 return 1;
7468}
3e73aa7c 7469
29b0f896 7470static int
543613e9 7471check_byte_reg (void)
29b0f896
AM
7472{
7473 int op;
543613e9 7474
29b0f896
AM
7475 for (op = i.operands; --op >= 0;)
7476 {
dc821c5f 7477 /* Skip non-register operands. */
bab6aec1 7478 if (i.types[op].bitfield.class != Reg)
dc821c5f
JB
7479 continue;
7480
29b0f896
AM
7481 /* If this is an eight bit register, it's OK. If it's the 16 or
7482 32 bit version of an eight bit register, we will just use the
7483 low portion, and that's OK too. */
dc821c5f 7484 if (i.types[op].bitfield.byte)
29b0f896
AM
7485 continue;
7486
5a819eb9 7487 /* I/O port address operands are OK too. */
75e5731b
JB
7488 if (i.tm.operand_types[op].bitfield.instance == RegD
7489 && i.tm.operand_types[op].bitfield.word)
5a819eb9
JB
7490 continue;
7491
9706160a 7492 /* crc32 only wants its source operand checked here. */
389d00a5
JB
7493 if (i.tm.base_opcode == 0xf0
7494 && i.tm.opcode_modifier.opcodespace == SPACE_0F38
8b65b895
L
7495 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2
7496 && op != 0)
9344ff29
L
7497 continue;
7498
29b0f896 7499 /* Any other register is bad. */
73c76375
JB
7500 as_bad (_("`%s%s' not allowed with `%s%c'"),
7501 register_prefix, i.op[op].regs->reg_name,
7502 i.tm.name, i.suffix);
7503 return 0;
29b0f896
AM
7504 }
7505 return 1;
7506}
7507
7508static int
e3bb37b5 7509check_long_reg (void)
29b0f896
AM
7510{
7511 int op;
7512
7513 for (op = i.operands; --op >= 0;)
dc821c5f 7514 /* Skip non-register operands. */
bab6aec1 7515 if (i.types[op].bitfield.class != Reg)
dc821c5f 7516 continue;
29b0f896
AM
7517 /* Reject eight bit registers, except where the template requires
7518 them. (eg. movzb) */
dc821c5f 7519 else if (i.types[op].bitfield.byte
bab6aec1 7520 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7521 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7522 && (i.tm.operand_types[op].bitfield.word
7523 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7524 {
a540244d
L
7525 as_bad (_("`%s%s' not allowed with `%s%c'"),
7526 register_prefix,
29b0f896
AM
7527 i.op[op].regs->reg_name,
7528 i.tm.name,
7529 i.suffix);
7530 return 0;
7531 }
be4c5e58
L
7532 /* Error if the e prefix on a general reg is missing. */
7533 else if (i.types[op].bitfield.word
bab6aec1 7534 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7535 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7536 && i.tm.operand_types[op].bitfield.dword)
29b0f896 7537 {
be4c5e58
L
7538 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7539 register_prefix, i.op[op].regs->reg_name,
7540 i.suffix);
7541 return 0;
252b5132 7542 }
e4630f71 7543 /* Warn if the r prefix on a general reg is present. */
dc821c5f 7544 else if (i.types[op].bitfield.qword
bab6aec1 7545 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7546 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7547 && i.tm.operand_types[op].bitfield.dword)
252b5132 7548 {
1cb0ab18
JB
7549 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7550 register_prefix, i.op[op].regs->reg_name, i.suffix);
7551 return 0;
29b0f896
AM
7552 }
7553 return 1;
7554}
252b5132 7555
29b0f896 7556static int
e3bb37b5 7557check_qword_reg (void)
29b0f896
AM
7558{
7559 int op;
252b5132 7560
29b0f896 7561 for (op = i.operands; --op >= 0; )
dc821c5f 7562 /* Skip non-register operands. */
bab6aec1 7563 if (i.types[op].bitfield.class != Reg)
dc821c5f 7564 continue;
29b0f896
AM
7565 /* Reject eight bit registers, except where the template requires
7566 them. (eg. movzb) */
dc821c5f 7567 else if (i.types[op].bitfield.byte
bab6aec1 7568 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7569 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7570 && (i.tm.operand_types[op].bitfield.word
7571 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7572 {
a540244d
L
7573 as_bad (_("`%s%s' not allowed with `%s%c'"),
7574 register_prefix,
29b0f896
AM
7575 i.op[op].regs->reg_name,
7576 i.tm.name,
7577 i.suffix);
7578 return 0;
7579 }
e4630f71 7580 /* Warn if the r prefix on a general reg is missing. */
dc821c5f
JB
7581 else if ((i.types[op].bitfield.word
7582 || i.types[op].bitfield.dword)
bab6aec1 7583 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7584 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7585 && i.tm.operand_types[op].bitfield.qword)
29b0f896
AM
7586 {
7587 /* Prohibit these changes in the 64bit mode, since the
7588 lowering is more complicated. */
1cb0ab18
JB
7589 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7590 register_prefix, i.op[op].regs->reg_name, i.suffix);
7591 return 0;
252b5132 7592 }
29b0f896
AM
7593 return 1;
7594}
252b5132 7595
29b0f896 7596static int
e3bb37b5 7597check_word_reg (void)
29b0f896
AM
7598{
7599 int op;
7600 for (op = i.operands; --op >= 0;)
dc821c5f 7601 /* Skip non-register operands. */
bab6aec1 7602 if (i.types[op].bitfield.class != Reg)
dc821c5f 7603 continue;
29b0f896
AM
7604 /* Reject eight bit registers, except where the template requires
7605 them. (eg. movzb) */
dc821c5f 7606 else if (i.types[op].bitfield.byte
bab6aec1 7607 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7608 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7609 && (i.tm.operand_types[op].bitfield.word
7610 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7611 {
a540244d
L
7612 as_bad (_("`%s%s' not allowed with `%s%c'"),
7613 register_prefix,
29b0f896
AM
7614 i.op[op].regs->reg_name,
7615 i.tm.name,
7616 i.suffix);
7617 return 0;
7618 }
9706160a
JB
7619 /* Error if the e or r prefix on a general reg is present. */
7620 else if ((i.types[op].bitfield.dword
dc821c5f 7621 || i.types[op].bitfield.qword)
bab6aec1 7622 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7623 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7624 && i.tm.operand_types[op].bitfield.word)
252b5132 7625 {
9706160a
JB
7626 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7627 register_prefix, i.op[op].regs->reg_name,
7628 i.suffix);
7629 return 0;
29b0f896
AM
7630 }
7631 return 1;
7632}
252b5132 7633
29b0f896 7634static int
40fb9820 7635update_imm (unsigned int j)
29b0f896 7636{
bc0844ae 7637 i386_operand_type overlap = i.types[j];
40fb9820
L
7638 if ((overlap.bitfield.imm8
7639 || overlap.bitfield.imm8s
7640 || overlap.bitfield.imm16
7641 || overlap.bitfield.imm32
7642 || overlap.bitfield.imm32s
7643 || overlap.bitfield.imm64)
0dfbf9d7
L
7644 && !operand_type_equal (&overlap, &imm8)
7645 && !operand_type_equal (&overlap, &imm8s)
7646 && !operand_type_equal (&overlap, &imm16)
7647 && !operand_type_equal (&overlap, &imm32)
7648 && !operand_type_equal (&overlap, &imm32s)
7649 && !operand_type_equal (&overlap, &imm64))
29b0f896
AM
7650 {
7651 if (i.suffix)
7652 {
40fb9820
L
7653 i386_operand_type temp;
7654
0dfbf9d7 7655 operand_type_set (&temp, 0);
7ab9ffdd 7656 if (i.suffix == BYTE_MNEM_SUFFIX)
40fb9820
L
7657 {
7658 temp.bitfield.imm8 = overlap.bitfield.imm8;
7659 temp.bitfield.imm8s = overlap.bitfield.imm8s;
7660 }
7661 else if (i.suffix == WORD_MNEM_SUFFIX)
7662 temp.bitfield.imm16 = overlap.bitfield.imm16;
7663 else if (i.suffix == QWORD_MNEM_SUFFIX)
7664 {
7665 temp.bitfield.imm64 = overlap.bitfield.imm64;
7666 temp.bitfield.imm32s = overlap.bitfield.imm32s;
7667 }
7668 else
7669 temp.bitfield.imm32 = overlap.bitfield.imm32;
7670 overlap = temp;
29b0f896 7671 }
0dfbf9d7
L
7672 else if (operand_type_equal (&overlap, &imm16_32_32s)
7673 || operand_type_equal (&overlap, &imm16_32)
7674 || operand_type_equal (&overlap, &imm16_32s))
29b0f896 7675 {
40fb9820 7676 if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
65da13b5 7677 overlap = imm16;
40fb9820 7678 else
65da13b5 7679 overlap = imm32s;
29b0f896 7680 }
8bbb3ad8
JB
7681 else if (i.prefix[REX_PREFIX] & REX_W)
7682 overlap = operand_type_and (overlap, imm32s);
7683 else if (i.prefix[DATA_PREFIX])
7684 overlap = operand_type_and (overlap,
7685 flag_code != CODE_16BIT ? imm16 : imm32);
0dfbf9d7
L
7686 if (!operand_type_equal (&overlap, &imm8)
7687 && !operand_type_equal (&overlap, &imm8s)
7688 && !operand_type_equal (&overlap, &imm16)
7689 && !operand_type_equal (&overlap, &imm32)
7690 && !operand_type_equal (&overlap, &imm32s)
7691 && !operand_type_equal (&overlap, &imm64))
29b0f896 7692 {
4eed87de
AM
7693 as_bad (_("no instruction mnemonic suffix given; "
7694 "can't determine immediate size"));
29b0f896
AM
7695 return 0;
7696 }
7697 }
40fb9820 7698 i.types[j] = overlap;
29b0f896 7699
40fb9820
L
7700 return 1;
7701}
7702
7703static int
7704finalize_imm (void)
7705{
bc0844ae 7706 unsigned int j, n;
29b0f896 7707
bc0844ae
L
7708 /* Update the first 2 immediate operands. */
7709 n = i.operands > 2 ? 2 : i.operands;
7710 if (n)
7711 {
7712 for (j = 0; j < n; j++)
7713 if (update_imm (j) == 0)
7714 return 0;
40fb9820 7715
bc0844ae
L
7716 /* The 3rd operand can't be immediate operand. */
7717 gas_assert (operand_type_check (i.types[2], imm) == 0);
7718 }
29b0f896
AM
7719
7720 return 1;
7721}
7722
7723static int
e3bb37b5 7724process_operands (void)
29b0f896
AM
7725{
7726 /* Default segment register this instruction will use for memory
7727 accesses. 0 means unknown. This is only for optimizing out
7728 unnecessary segment overrides. */
5e042380 7729 const reg_entry *default_seg = NULL;
29b0f896 7730
a5aeccd9
JB
7731 if (i.tm.opcode_modifier.sse2avx)
7732 {
7733 /* Legacy encoded insns allow explicit REX prefixes, so these prefixes
7734 need converting. */
7735 i.rex |= i.prefix[REX_PREFIX] & (REX_W | REX_R | REX_X | REX_B);
7736 i.prefix[REX_PREFIX] = 0;
7737 i.rex_encoding = 0;
7738 }
c423d21a
JB
7739 /* ImmExt should be processed after SSE2AVX. */
7740 else if (i.tm.opcode_modifier.immext)
7741 process_immext ();
a5aeccd9 7742
2426c15f 7743 if (i.tm.opcode_modifier.sse2avx && i.tm.opcode_modifier.vexvvvv)
29b0f896 7744 {
91d6fa6a
NC
7745 unsigned int dupl = i.operands;
7746 unsigned int dest = dupl - 1;
9fcfb3d7
L
7747 unsigned int j;
7748
c0f3af97 7749 /* The destination must be an xmm register. */
9c2799c2 7750 gas_assert (i.reg_operands
91d6fa6a 7751 && MAX_OPERANDS > dupl
7ab9ffdd 7752 && operand_type_equal (&i.types[dest], &regxmm));
c0f3af97 7753
75e5731b 7754 if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 7755 && i.tm.operand_types[0].bitfield.xmmword)
e2ec9d29 7756 {
8cd7925b 7757 if (i.tm.opcode_modifier.vexsources == VEX3SOURCES)
c0f3af97
L
7758 {
7759 /* Keep xmm0 for instructions with VEX prefix and 3
7760 sources. */
75e5731b 7761 i.tm.operand_types[0].bitfield.instance = InstanceNone;
3528c362 7762 i.tm.operand_types[0].bitfield.class = RegSIMD;
c0f3af97
L
7763 goto duplicate;
7764 }
e2ec9d29 7765 else
c0f3af97
L
7766 {
7767 /* We remove the first xmm0 and keep the number of
7768 operands unchanged, which in fact duplicates the
7769 destination. */
7770 for (j = 1; j < i.operands; j++)
7771 {
7772 i.op[j - 1] = i.op[j];
7773 i.types[j - 1] = i.types[j];
7774 i.tm.operand_types[j - 1] = i.tm.operand_types[j];
8dc0818e 7775 i.flags[j - 1] = i.flags[j];
c0f3af97
L
7776 }
7777 }
7778 }
7779 else if (i.tm.opcode_modifier.implicit1stxmm0)
7ab9ffdd 7780 {
91d6fa6a 7781 gas_assert ((MAX_OPERANDS - 1) > dupl
8cd7925b
L
7782 && (i.tm.opcode_modifier.vexsources
7783 == VEX3SOURCES));
c0f3af97
L
7784
7785 /* Add the implicit xmm0 for instructions with VEX prefix
7786 and 3 sources. */
7787 for (j = i.operands; j > 0; j--)
7788 {
7789 i.op[j] = i.op[j - 1];
7790 i.types[j] = i.types[j - 1];
7791 i.tm.operand_types[j] = i.tm.operand_types[j - 1];
8dc0818e 7792 i.flags[j] = i.flags[j - 1];
c0f3af97
L
7793 }
7794 i.op[0].regs
629310ab 7795 = (const reg_entry *) str_hash_find (reg_hash, "xmm0");
7ab9ffdd 7796 i.types[0] = regxmm;
c0f3af97
L
7797 i.tm.operand_types[0] = regxmm;
7798
7799 i.operands += 2;
7800 i.reg_operands += 2;
7801 i.tm.operands += 2;
7802
91d6fa6a 7803 dupl++;
c0f3af97 7804 dest++;
91d6fa6a
NC
7805 i.op[dupl] = i.op[dest];
7806 i.types[dupl] = i.types[dest];
7807 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 7808 i.flags[dupl] = i.flags[dest];
e2ec9d29 7809 }
c0f3af97
L
7810 else
7811 {
dc1e8a47 7812 duplicate:
c0f3af97
L
7813 i.operands++;
7814 i.reg_operands++;
7815 i.tm.operands++;
7816
91d6fa6a
NC
7817 i.op[dupl] = i.op[dest];
7818 i.types[dupl] = i.types[dest];
7819 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 7820 i.flags[dupl] = i.flags[dest];
c0f3af97
L
7821 }
7822
7823 if (i.tm.opcode_modifier.immext)
7824 process_immext ();
7825 }
75e5731b 7826 else if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 7827 && i.tm.operand_types[0].bitfield.xmmword)
c0f3af97
L
7828 {
7829 unsigned int j;
7830
9fcfb3d7
L
7831 for (j = 1; j < i.operands; j++)
7832 {
7833 i.op[j - 1] = i.op[j];
7834 i.types[j - 1] = i.types[j];
7835
7836 /* We need to adjust fields in i.tm since they are used by
7837 build_modrm_byte. */
7838 i.tm.operand_types [j - 1] = i.tm.operand_types [j];
8dc0818e
JB
7839
7840 i.flags[j - 1] = i.flags[j];
9fcfb3d7
L
7841 }
7842
e2ec9d29
L
7843 i.operands--;
7844 i.reg_operands--;
e2ec9d29
L
7845 i.tm.operands--;
7846 }
920d2ddc
IT
7847 else if (i.tm.opcode_modifier.implicitquadgroup)
7848 {
a477a8c4
JB
7849 unsigned int regnum, first_reg_in_group, last_reg_in_group;
7850
920d2ddc 7851 /* The second operand must be {x,y,z}mmN, where N is a multiple of 4. */
3528c362 7852 gas_assert (i.operands >= 2 && i.types[1].bitfield.class == RegSIMD);
a477a8c4
JB
7853 regnum = register_number (i.op[1].regs);
7854 first_reg_in_group = regnum & ~3;
7855 last_reg_in_group = first_reg_in_group + 3;
7856 if (regnum != first_reg_in_group)
7857 as_warn (_("source register `%s%s' implicitly denotes"
7858 " `%s%.3s%u' to `%s%.3s%u' source group in `%s'"),
7859 register_prefix, i.op[1].regs->reg_name,
7860 register_prefix, i.op[1].regs->reg_name, first_reg_in_group,
7861 register_prefix, i.op[1].regs->reg_name, last_reg_in_group,
7862 i.tm.name);
7863 }
e2ec9d29
L
7864 else if (i.tm.opcode_modifier.regkludge)
7865 {
7866 /* The imul $imm, %reg instruction is converted into
7867 imul $imm, %reg, %reg, and the clr %reg instruction
7868 is converted into xor %reg, %reg. */
7869
7870 unsigned int first_reg_op;
7871
7872 if (operand_type_check (i.types[0], reg))
7873 first_reg_op = 0;
7874 else
7875 first_reg_op = 1;
7876 /* Pretend we saw the extra register operand. */
9c2799c2 7877 gas_assert (i.reg_operands == 1
7ab9ffdd 7878 && i.op[first_reg_op + 1].regs == 0);
e2ec9d29
L
7879 i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
7880 i.types[first_reg_op + 1] = i.types[first_reg_op];
7881 i.operands++;
7882 i.reg_operands++;
29b0f896
AM
7883 }
7884
85b80b0f 7885 if (i.tm.opcode_modifier.modrm)
29b0f896
AM
7886 {
7887 /* The opcode is completed (modulo i.tm.extension_opcode which
52271982
AM
7888 must be put into the modrm byte). Now, we make the modrm and
7889 index base bytes based on all the info we've collected. */
29b0f896
AM
7890
7891 default_seg = build_modrm_byte ();
7892 }
00cee14f 7893 else if (i.types[0].bitfield.class == SReg)
85b80b0f
JB
7894 {
7895 if (flag_code != CODE_64BIT
7896 ? i.tm.base_opcode == POP_SEG_SHORT
7897 && i.op[0].regs->reg_num == 1
389d00a5 7898 : (i.tm.base_opcode | 1) == (POP_SEG386_SHORT & 0xff)
85b80b0f
JB
7899 && i.op[0].regs->reg_num < 4)
7900 {
7901 as_bad (_("you can't `%s %s%s'"),
7902 i.tm.name, register_prefix, i.op[0].regs->reg_name);
7903 return 0;
7904 }
389d00a5
JB
7905 if (i.op[0].regs->reg_num > 3
7906 && i.tm.opcode_modifier.opcodespace == SPACE_BASE )
85b80b0f 7907 {
389d00a5
JB
7908 i.tm.base_opcode ^= (POP_SEG_SHORT ^ POP_SEG386_SHORT) & 0xff;
7909 i.tm.opcode_modifier.opcodespace = SPACE_0F;
85b80b0f
JB
7910 }
7911 i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
7912 }
389d00a5
JB
7913 else if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
7914 && (i.tm.base_opcode & ~3) == MOV_AX_DISP32)
29b0f896 7915 {
5e042380 7916 default_seg = reg_ds;
29b0f896 7917 }
40fb9820 7918 else if (i.tm.opcode_modifier.isstring)
29b0f896
AM
7919 {
7920 /* For the string instructions that allow a segment override
7921 on one of their operands, the default segment is ds. */
5e042380 7922 default_seg = reg_ds;
29b0f896 7923 }
50128d0c 7924 else if (i.short_form)
85b80b0f
JB
7925 {
7926 /* The register or float register operand is in operand
7927 0 or 1. */
bab6aec1 7928 unsigned int op = i.tm.operand_types[0].bitfield.class != Reg;
85b80b0f
JB
7929
7930 /* Register goes in low 3 bits of opcode. */
7931 i.tm.base_opcode |= i.op[op].regs->reg_num;
7932 if ((i.op[op].regs->reg_flags & RegRex) != 0)
7933 i.rex |= REX_B;
7934 if (!quiet_warnings && i.tm.opcode_modifier.ugh)
7935 {
7936 /* Warn about some common errors, but press on regardless.
7937 The first case can be generated by gcc (<= 2.8.1). */
7938 if (i.operands == 2)
7939 {
7940 /* Reversed arguments on faddp, fsubp, etc. */
7941 as_warn (_("translating to `%s %s%s,%s%s'"), i.tm.name,
7942 register_prefix, i.op[!intel_syntax].regs->reg_name,
7943 register_prefix, i.op[intel_syntax].regs->reg_name);
7944 }
7945 else
7946 {
7947 /* Extraneous `l' suffix on fp insn. */
7948 as_warn (_("translating to `%s %s%s'"), i.tm.name,
7949 register_prefix, i.op[0].regs->reg_name);
7950 }
7951 }
7952 }
29b0f896 7953
514a8bb0 7954 if ((i.seg[0] || i.prefix[SEG_PREFIX])
514a8bb0 7955 && i.tm.base_opcode == 0x8d /* lea */
35648716 7956 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
514a8bb0 7957 && !is_any_vex_encoding(&i.tm))
92334ad2
JB
7958 {
7959 if (!quiet_warnings)
7960 as_warn (_("segment override on `%s' is ineffectual"), i.tm.name);
7961 if (optimize)
7962 {
7963 i.seg[0] = NULL;
7964 i.prefix[SEG_PREFIX] = 0;
7965 }
7966 }
52271982
AM
7967
7968 /* If a segment was explicitly specified, and the specified segment
b6773884
JB
7969 is neither the default nor the one already recorded from a prefix,
7970 use an opcode prefix to select it. If we never figured out what
7971 the default segment is, then default_seg will be zero at this
7972 point, and the specified segment prefix will always be used. */
7973 if (i.seg[0]
7974 && i.seg[0] != default_seg
5e042380 7975 && i386_seg_prefixes[i.seg[0]->reg_num] != i.prefix[SEG_PREFIX])
29b0f896 7976 {
5e042380 7977 if (!add_prefix (i386_seg_prefixes[i.seg[0]->reg_num]))
29b0f896
AM
7978 return 0;
7979 }
7980 return 1;
7981}
7982
a5aeccd9 7983static INLINE void set_rex_vrex (const reg_entry *r, unsigned int rex_bit,
5b7c81bd 7984 bool do_sse2avx)
a5aeccd9
JB
7985{
7986 if (r->reg_flags & RegRex)
7987 {
7988 if (i.rex & rex_bit)
7989 as_bad (_("same type of prefix used twice"));
7990 i.rex |= rex_bit;
7991 }
7992 else if (do_sse2avx && (i.rex & rex_bit) && i.vex.register_specifier)
7993 {
7994 gas_assert (i.vex.register_specifier == r);
7995 i.vex.register_specifier += 8;
7996 }
7997
7998 if (r->reg_flags & RegVRex)
7999 i.vrex |= rex_bit;
8000}
8001
5e042380 8002static const reg_entry *
e3bb37b5 8003build_modrm_byte (void)
29b0f896 8004{
5e042380 8005 const reg_entry *default_seg = NULL;
c0f3af97 8006 unsigned int source, dest;
8cd7925b 8007 int vex_3_sources;
c0f3af97 8008
8cd7925b 8009 vex_3_sources = i.tm.opcode_modifier.vexsources == VEX3SOURCES;
c0f3af97
L
8010 if (vex_3_sources)
8011 {
91d6fa6a 8012 unsigned int nds, reg_slot;
4c2c6516 8013 expressionS *exp;
c0f3af97 8014
6b8d3588 8015 dest = i.operands - 1;
c0f3af97 8016 nds = dest - 1;
922d8de8 8017
a683cc34 8018 /* There are 2 kinds of instructions:
bed3d976 8019 1. 5 operands: 4 register operands or 3 register operands
9d3bf266 8020 plus 1 memory operand plus one Imm4 operand, VexXDS, and
bed3d976 8021 VexW0 or VexW1. The destination must be either XMM, YMM or
43234a1e 8022 ZMM register.
bed3d976 8023 2. 4 operands: 4 register operands or 3 register operands
2f1bada2 8024 plus 1 memory operand, with VexXDS. */
922d8de8 8025 gas_assert ((i.reg_operands == 4
bed3d976
JB
8026 || (i.reg_operands == 3 && i.mem_operands == 1))
8027 && i.tm.opcode_modifier.vexvvvv == VEXXDS
dcd7e323 8028 && i.tm.opcode_modifier.vexw
3528c362 8029 && i.tm.operand_types[dest].bitfield.class == RegSIMD);
a683cc34 8030
48db9223
JB
8031 /* If VexW1 is set, the first non-immediate operand is the source and
8032 the second non-immediate one is encoded in the immediate operand. */
8033 if (i.tm.opcode_modifier.vexw == VEXW1)
8034 {
8035 source = i.imm_operands;
8036 reg_slot = i.imm_operands + 1;
8037 }
8038 else
8039 {
8040 source = i.imm_operands + 1;
8041 reg_slot = i.imm_operands;
8042 }
8043
a683cc34 8044 if (i.imm_operands == 0)
bed3d976
JB
8045 {
8046 /* When there is no immediate operand, generate an 8bit
8047 immediate operand to encode the first operand. */
8048 exp = &im_expressions[i.imm_operands++];
8049 i.op[i.operands].imms = exp;
8050 i.types[i.operands] = imm8;
8051 i.operands++;
8052
3528c362 8053 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
bed3d976
JB
8054 exp->X_op = O_constant;
8055 exp->X_add_number = register_number (i.op[reg_slot].regs) << 4;
43234a1e
L
8056 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
8057 }
922d8de8 8058 else
bed3d976 8059 {
9d3bf266
JB
8060 gas_assert (i.imm_operands == 1);
8061 gas_assert (fits_in_imm4 (i.op[0].imms->X_add_number));
8062 gas_assert (!i.tm.opcode_modifier.immext);
a683cc34 8063
9d3bf266
JB
8064 /* Turn on Imm8 again so that output_imm will generate it. */
8065 i.types[0].bitfield.imm8 = 1;
bed3d976 8066
3528c362 8067 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
9d3bf266 8068 i.op[0].imms->X_add_number
bed3d976 8069 |= register_number (i.op[reg_slot].regs) << 4;
43234a1e 8070 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
bed3d976 8071 }
a683cc34 8072
3528c362 8073 gas_assert (i.tm.operand_types[nds].bitfield.class == RegSIMD);
dae39acc 8074 i.vex.register_specifier = i.op[nds].regs;
c0f3af97
L
8075 }
8076 else
8077 source = dest = 0;
29b0f896
AM
8078
8079 /* i.reg_operands MUST be the number of real register operands;
c0f3af97
L
8080 implicit registers do not count. If there are 3 register
8081 operands, it must be a instruction with VexNDS. For a
8082 instruction with VexNDD, the destination register is encoded
8083 in VEX prefix. If there are 4 register operands, it must be
8084 a instruction with VEX prefix and 3 sources. */
7ab9ffdd
L
8085 if (i.mem_operands == 0
8086 && ((i.reg_operands == 2
2426c15f 8087 && i.tm.opcode_modifier.vexvvvv <= VEXXDS)
7ab9ffdd 8088 || (i.reg_operands == 3
2426c15f 8089 && i.tm.opcode_modifier.vexvvvv == VEXXDS)
7ab9ffdd 8090 || (i.reg_operands == 4 && vex_3_sources)))
29b0f896 8091 {
cab737b9
L
8092 switch (i.operands)
8093 {
8094 case 2:
8095 source = 0;
8096 break;
8097 case 3:
c81128dc
L
8098 /* When there are 3 operands, one of them may be immediate,
8099 which may be the first or the last operand. Otherwise,
c0f3af97
L
8100 the first operand must be shift count register (cl) or it
8101 is an instruction with VexNDS. */
9c2799c2 8102 gas_assert (i.imm_operands == 1
7ab9ffdd 8103 || (i.imm_operands == 0
2426c15f 8104 && (i.tm.opcode_modifier.vexvvvv == VEXXDS
75e5731b
JB
8105 || (i.types[0].bitfield.instance == RegC
8106 && i.types[0].bitfield.byte))));
40fb9820 8107 if (operand_type_check (i.types[0], imm)
75e5731b
JB
8108 || (i.types[0].bitfield.instance == RegC
8109 && i.types[0].bitfield.byte))
40fb9820
L
8110 source = 1;
8111 else
8112 source = 0;
cab737b9
L
8113 break;
8114 case 4:
368d64cc
L
8115 /* When there are 4 operands, the first two must be 8bit
8116 immediate operands. The source operand will be the 3rd
c0f3af97
L
8117 one.
8118
8119 For instructions with VexNDS, if the first operand
8120 an imm8, the source operand is the 2nd one. If the last
8121 operand is imm8, the source operand is the first one. */
9c2799c2 8122 gas_assert ((i.imm_operands == 2
7ab9ffdd
L
8123 && i.types[0].bitfield.imm8
8124 && i.types[1].bitfield.imm8)
2426c15f 8125 || (i.tm.opcode_modifier.vexvvvv == VEXXDS
7ab9ffdd
L
8126 && i.imm_operands == 1
8127 && (i.types[0].bitfield.imm8
cf665fee 8128 || i.types[i.operands - 1].bitfield.imm8)));
9f2670f2
L
8129 if (i.imm_operands == 2)
8130 source = 2;
8131 else
c0f3af97
L
8132 {
8133 if (i.types[0].bitfield.imm8)
8134 source = 1;
8135 else
8136 source = 0;
8137 }
c0f3af97
L
8138 break;
8139 case 5:
cf665fee
JB
8140 gas_assert (!is_evex_encoding (&i.tm));
8141 gas_assert (i.imm_operands == 1 && vex_3_sources);
cab737b9
L
8142 break;
8143 default:
8144 abort ();
8145 }
8146
c0f3af97
L
8147 if (!vex_3_sources)
8148 {
8149 dest = source + 1;
8150
2426c15f 8151 if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
c0f3af97 8152 {
43234a1e 8153 /* For instructions with VexNDS, the register-only source
c5d0745b 8154 operand must be a 32/64bit integer, XMM, YMM, ZMM, or mask
dfd69174 8155 register. It is encoded in VEX prefix. */
f12dc422
L
8156
8157 i386_operand_type op;
8158 unsigned int vvvv;
8159
c2ecccb3
L
8160 /* Swap two source operands if needed. */
8161 if (i.tm.opcode_modifier.swapsources)
f12dc422
L
8162 {
8163 vvvv = source;
8164 source = dest;
8165 }
8166 else
8167 vvvv = dest;
8168
8169 op = i.tm.operand_types[vvvv];
c0f3af97 8170 if ((dest + 1) >= i.operands
bab6aec1 8171 || ((op.bitfield.class != Reg
dc821c5f 8172 || (!op.bitfield.dword && !op.bitfield.qword))
3528c362 8173 && op.bitfield.class != RegSIMD
43234a1e 8174 && !operand_type_equal (&op, &regmask)))
c0f3af97 8175 abort ();
f12dc422 8176 i.vex.register_specifier = i.op[vvvv].regs;
c0f3af97
L
8177 dest++;
8178 }
8179 }
29b0f896
AM
8180
8181 i.rm.mode = 3;
dfd69174
JB
8182 /* One of the register operands will be encoded in the i.rm.reg
8183 field, the other in the combined i.rm.mode and i.rm.regmem
29b0f896
AM
8184 fields. If no form of this instruction supports a memory
8185 destination operand, then we assume the source operand may
8186 sometimes be a memory operand and so we need to store the
8187 destination in the i.rm.reg field. */
dfd69174 8188 if (!i.tm.opcode_modifier.regmem
40fb9820 8189 && operand_type_check (i.tm.operand_types[dest], anymem) == 0)
29b0f896
AM
8190 {
8191 i.rm.reg = i.op[dest].regs->reg_num;
8192 i.rm.regmem = i.op[source].regs->reg_num;
a5aeccd9 8193 set_rex_vrex (i.op[dest].regs, REX_R, i.tm.opcode_modifier.sse2avx);
5b7c81bd 8194 set_rex_vrex (i.op[source].regs, REX_B, false);
29b0f896
AM
8195 }
8196 else
8197 {
8198 i.rm.reg = i.op[source].regs->reg_num;
8199 i.rm.regmem = i.op[dest].regs->reg_num;
a5aeccd9 8200 set_rex_vrex (i.op[dest].regs, REX_B, i.tm.opcode_modifier.sse2avx);
5b7c81bd 8201 set_rex_vrex (i.op[source].regs, REX_R, false);
29b0f896 8202 }
e0c7f900 8203 if (flag_code != CODE_64BIT && (i.rex & REX_R))
c4a530c5 8204 {
4a5c67ed 8205 if (i.types[!i.tm.opcode_modifier.regmem].bitfield.class != RegCR)
c4a530c5 8206 abort ();
e0c7f900 8207 i.rex &= ~REX_R;
c4a530c5
JB
8208 add_prefix (LOCK_PREFIX_OPCODE);
8209 }
29b0f896
AM
8210 }
8211 else
8212 { /* If it's not 2 reg operands... */
c0f3af97
L
8213 unsigned int mem;
8214
29b0f896
AM
8215 if (i.mem_operands)
8216 {
8217 unsigned int fake_zero_displacement = 0;
99018f42 8218 unsigned int op;
4eed87de 8219
7ab9ffdd 8220 for (op = 0; op < i.operands; op++)
8dc0818e 8221 if (i.flags[op] & Operand_Mem)
7ab9ffdd 8222 break;
7ab9ffdd 8223 gas_assert (op < i.operands);
29b0f896 8224
63112cd6 8225 if (i.tm.opcode_modifier.sib)
6c30d220 8226 {
260cd341
LC
8227 /* The index register of VSIB shouldn't be RegIZ. */
8228 if (i.tm.opcode_modifier.sib != SIBMEM
8229 && i.index_reg->reg_num == RegIZ)
6c30d220
L
8230 abort ();
8231
8232 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8233 if (!i.base_reg)
8234 {
8235 i.sib.base = NO_BASE_REGISTER;
8236 i.sib.scale = i.log2_scale_factor;
2f2be86b 8237 i.types[op] = operand_type_and_not (i.types[op], anydisp);
a775efc8 8238 i.types[op].bitfield.disp32 = 1;
6c30d220 8239 }
260cd341
LC
8240
8241 /* Since the mandatory SIB always has index register, so
8242 the code logic remains unchanged. The non-mandatory SIB
8243 without index register is allowed and will be handled
8244 later. */
8245 if (i.index_reg)
8246 {
8247 if (i.index_reg->reg_num == RegIZ)
8248 i.sib.index = NO_INDEX_REGISTER;
8249 else
8250 i.sib.index = i.index_reg->reg_num;
5b7c81bd 8251 set_rex_vrex (i.index_reg, REX_X, false);
260cd341 8252 }
6c30d220
L
8253 }
8254
5e042380 8255 default_seg = reg_ds;
29b0f896
AM
8256
8257 if (i.base_reg == 0)
8258 {
8259 i.rm.mode = 0;
8260 if (!i.disp_operands)
9bb129e8 8261 fake_zero_displacement = 1;
29b0f896
AM
8262 if (i.index_reg == 0)
8263 {
260cd341
LC
8264 /* Both check for VSIB and mandatory non-vector SIB. */
8265 gas_assert (!i.tm.opcode_modifier.sib
8266 || i.tm.opcode_modifier.sib == SIBMEM);
29b0f896 8267 /* Operand is just <disp> */
2f2be86b 8268 i.types[op] = operand_type_and_not (i.types[op], anydisp);
20f0a1fc 8269 if (flag_code == CODE_64BIT)
29b0f896
AM
8270 {
8271 /* 64bit mode overwrites the 32bit absolute
8272 addressing by RIP relative addressing and
8273 absolute addressing is encoded by one of the
8274 redundant SIB forms. */
8275 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8276 i.sib.base = NO_BASE_REGISTER;
8277 i.sib.index = NO_INDEX_REGISTER;
a775efc8 8278 i.types[op].bitfield.disp32 = 1;
20f0a1fc 8279 }
fc225355
L
8280 else if ((flag_code == CODE_16BIT)
8281 ^ (i.prefix[ADDR_PREFIX] != 0))
20f0a1fc
NC
8282 {
8283 i.rm.regmem = NO_BASE_REGISTER_16;
2f2be86b 8284 i.types[op].bitfield.disp16 = 1;
20f0a1fc
NC
8285 }
8286 else
8287 {
8288 i.rm.regmem = NO_BASE_REGISTER;
2f2be86b 8289 i.types[op].bitfield.disp32 = 1;
29b0f896
AM
8290 }
8291 }
63112cd6 8292 else if (!i.tm.opcode_modifier.sib)
29b0f896 8293 {
6c30d220 8294 /* !i.base_reg && i.index_reg */
e968fc9b 8295 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8296 i.sib.index = NO_INDEX_REGISTER;
8297 else
8298 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8299 i.sib.base = NO_BASE_REGISTER;
8300 i.sib.scale = i.log2_scale_factor;
8301 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
2f2be86b 8302 i.types[op] = operand_type_and_not (i.types[op], anydisp);
a775efc8 8303 i.types[op].bitfield.disp32 = 1;
29b0f896 8304 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8305 i.rex |= REX_X;
29b0f896
AM
8306 }
8307 }
8308 /* RIP addressing for 64bit mode. */
e968fc9b 8309 else if (i.base_reg->reg_num == RegIP)
29b0f896 8310 {
63112cd6 8311 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896 8312 i.rm.regmem = NO_BASE_REGISTER;
40fb9820
L
8313 i.types[op].bitfield.disp8 = 0;
8314 i.types[op].bitfield.disp16 = 0;
a775efc8 8315 i.types[op].bitfield.disp32 = 1;
40fb9820 8316 i.types[op].bitfield.disp64 = 0;
71903a11 8317 i.flags[op] |= Operand_PCrel;
20f0a1fc
NC
8318 if (! i.disp_operands)
8319 fake_zero_displacement = 1;
29b0f896 8320 }
dc821c5f 8321 else if (i.base_reg->reg_type.bitfield.word)
29b0f896 8322 {
63112cd6 8323 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896
AM
8324 switch (i.base_reg->reg_num)
8325 {
8326 case 3: /* (%bx) */
8327 if (i.index_reg == 0)
8328 i.rm.regmem = 7;
8329 else /* (%bx,%si) -> 0, or (%bx,%di) -> 1 */
8330 i.rm.regmem = i.index_reg->reg_num - 6;
8331 break;
8332 case 5: /* (%bp) */
5e042380 8333 default_seg = reg_ss;
29b0f896
AM
8334 if (i.index_reg == 0)
8335 {
8336 i.rm.regmem = 6;
40fb9820 8337 if (operand_type_check (i.types[op], disp) == 0)
29b0f896
AM
8338 {
8339 /* fake (%bp) into 0(%bp) */
41eb8e88 8340 if (i.disp_encoding == disp_encoding_16bit)
1a02d6b0
L
8341 i.types[op].bitfield.disp16 = 1;
8342 else
8343 i.types[op].bitfield.disp8 = 1;
252b5132 8344 fake_zero_displacement = 1;
29b0f896
AM
8345 }
8346 }
8347 else /* (%bp,%si) -> 2, or (%bp,%di) -> 3 */
8348 i.rm.regmem = i.index_reg->reg_num - 6 + 2;
8349 break;
8350 default: /* (%si) -> 4 or (%di) -> 5 */
8351 i.rm.regmem = i.base_reg->reg_num - 6 + 4;
8352 }
41eb8e88
L
8353 if (!fake_zero_displacement
8354 && !i.disp_operands
8355 && i.disp_encoding)
8356 {
8357 fake_zero_displacement = 1;
8358 if (i.disp_encoding == disp_encoding_8bit)
8359 i.types[op].bitfield.disp8 = 1;
8360 else
8361 i.types[op].bitfield.disp16 = 1;
8362 }
29b0f896
AM
8363 i.rm.mode = mode_from_disp_size (i.types[op]);
8364 }
8365 else /* i.base_reg and 32/64 bit mode */
8366 {
a9aabc23 8367 if (operand_type_check (i.types[op], disp))
40fb9820 8368 {
73053c1f
JB
8369 i.types[op].bitfield.disp16 = 0;
8370 i.types[op].bitfield.disp64 = 0;
a775efc8 8371 i.types[op].bitfield.disp32 = 1;
40fb9820 8372 }
20f0a1fc 8373
63112cd6 8374 if (!i.tm.opcode_modifier.sib)
6c30d220 8375 i.rm.regmem = i.base_reg->reg_num;
29b0f896 8376 if ((i.base_reg->reg_flags & RegRex) != 0)
161a04f6 8377 i.rex |= REX_B;
29b0f896
AM
8378 i.sib.base = i.base_reg->reg_num;
8379 /* x86-64 ignores REX prefix bit here to avoid decoder
8380 complications. */
848930b2
JB
8381 if (!(i.base_reg->reg_flags & RegRex)
8382 && (i.base_reg->reg_num == EBP_REG_NUM
8383 || i.base_reg->reg_num == ESP_REG_NUM))
5e042380 8384 default_seg = reg_ss;
848930b2 8385 if (i.base_reg->reg_num == 5 && i.disp_operands == 0)
29b0f896 8386 {
848930b2 8387 fake_zero_displacement = 1;
1a02d6b0
L
8388 if (i.disp_encoding == disp_encoding_32bit)
8389 i.types[op].bitfield.disp32 = 1;
8390 else
8391 i.types[op].bitfield.disp8 = 1;
29b0f896
AM
8392 }
8393 i.sib.scale = i.log2_scale_factor;
8394 if (i.index_reg == 0)
8395 {
260cd341
LC
8396 /* Only check for VSIB. */
8397 gas_assert (i.tm.opcode_modifier.sib != VECSIB128
8398 && i.tm.opcode_modifier.sib != VECSIB256
8399 && i.tm.opcode_modifier.sib != VECSIB512);
8400
29b0f896
AM
8401 /* <disp>(%esp) becomes two byte modrm with no index
8402 register. We've already stored the code for esp
8403 in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
8404 Any base register besides %esp will not use the
8405 extra modrm byte. */
8406 i.sib.index = NO_INDEX_REGISTER;
29b0f896 8407 }
63112cd6 8408 else if (!i.tm.opcode_modifier.sib)
29b0f896 8409 {
e968fc9b 8410 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8411 i.sib.index = NO_INDEX_REGISTER;
8412 else
8413 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8414 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8415 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8416 i.rex |= REX_X;
29b0f896 8417 }
67a4f2b7
AO
8418
8419 if (i.disp_operands
8420 && (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
8421 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL))
8422 i.rm.mode = 0;
8423 else
a501d77e
L
8424 {
8425 if (!fake_zero_displacement
8426 && !i.disp_operands
8427 && i.disp_encoding)
8428 {
8429 fake_zero_displacement = 1;
8430 if (i.disp_encoding == disp_encoding_8bit)
8431 i.types[op].bitfield.disp8 = 1;
8432 else
8433 i.types[op].bitfield.disp32 = 1;
8434 }
8435 i.rm.mode = mode_from_disp_size (i.types[op]);
8436 }
29b0f896 8437 }
252b5132 8438
29b0f896
AM
8439 if (fake_zero_displacement)
8440 {
8441 /* Fakes a zero displacement assuming that i.types[op]
8442 holds the correct displacement size. */
8443 expressionS *exp;
8444
9c2799c2 8445 gas_assert (i.op[op].disps == 0);
29b0f896
AM
8446 exp = &disp_expressions[i.disp_operands++];
8447 i.op[op].disps = exp;
8448 exp->X_op = O_constant;
8449 exp->X_add_number = 0;
8450 exp->X_add_symbol = (symbolS *) 0;
8451 exp->X_op_symbol = (symbolS *) 0;
8452 }
c0f3af97
L
8453
8454 mem = op;
29b0f896 8455 }
c0f3af97
L
8456 else
8457 mem = ~0;
252b5132 8458
8c43a48b 8459 if (i.tm.opcode_modifier.vexsources == XOP2SOURCES)
5dd85c99
SP
8460 {
8461 if (operand_type_check (i.types[0], imm))
8462 i.vex.register_specifier = NULL;
8463 else
8464 {
8465 /* VEX.vvvv encodes one of the sources when the first
8466 operand is not an immediate. */
1ef99a7b 8467 if (i.tm.opcode_modifier.vexw == VEXW0)
5dd85c99
SP
8468 i.vex.register_specifier = i.op[0].regs;
8469 else
8470 i.vex.register_specifier = i.op[1].regs;
8471 }
8472
8473 /* Destination is a XMM register encoded in the ModRM.reg
8474 and VEX.R bit. */
8475 i.rm.reg = i.op[2].regs->reg_num;
8476 if ((i.op[2].regs->reg_flags & RegRex) != 0)
8477 i.rex |= REX_R;
8478
8479 /* ModRM.rm and VEX.B encodes the other source. */
8480 if (!i.mem_operands)
8481 {
8482 i.rm.mode = 3;
8483
1ef99a7b 8484 if (i.tm.opcode_modifier.vexw == VEXW0)
5dd85c99
SP
8485 i.rm.regmem = i.op[1].regs->reg_num;
8486 else
8487 i.rm.regmem = i.op[0].regs->reg_num;
8488
8489 if ((i.op[1].regs->reg_flags & RegRex) != 0)
8490 i.rex |= REX_B;
8491 }
8492 }
2426c15f 8493 else if (i.tm.opcode_modifier.vexvvvv == VEXLWP)
f88c9eb0
SP
8494 {
8495 i.vex.register_specifier = i.op[2].regs;
8496 if (!i.mem_operands)
8497 {
8498 i.rm.mode = 3;
8499 i.rm.regmem = i.op[1].regs->reg_num;
8500 if ((i.op[1].regs->reg_flags & RegRex) != 0)
8501 i.rex |= REX_B;
8502 }
8503 }
29b0f896
AM
8504 /* Fill in i.rm.reg or i.rm.regmem field with register operand
8505 (if any) based on i.tm.extension_opcode. Again, we must be
8506 careful to make sure that segment/control/debug/test/MMX
8507 registers are coded into the i.rm.reg field. */
f88c9eb0 8508 else if (i.reg_operands)
29b0f896 8509 {
99018f42 8510 unsigned int op;
7ab9ffdd
L
8511 unsigned int vex_reg = ~0;
8512
8513 for (op = 0; op < i.operands; op++)
921eafea
L
8514 if (i.types[op].bitfield.class == Reg
8515 || i.types[op].bitfield.class == RegBND
8516 || i.types[op].bitfield.class == RegMask
8517 || i.types[op].bitfield.class == SReg
8518 || i.types[op].bitfield.class == RegCR
8519 || i.types[op].bitfield.class == RegDR
8520 || i.types[op].bitfield.class == RegTR
8521 || i.types[op].bitfield.class == RegSIMD
8522 || i.types[op].bitfield.class == RegMMX)
8523 break;
c0209578 8524
7ab9ffdd
L
8525 if (vex_3_sources)
8526 op = dest;
2426c15f 8527 else if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
7ab9ffdd
L
8528 {
8529 /* For instructions with VexNDS, the register-only
8530 source operand is encoded in VEX prefix. */
8531 gas_assert (mem != (unsigned int) ~0);
c0f3af97 8532
7ab9ffdd 8533 if (op > mem)
c0f3af97 8534 {
7ab9ffdd
L
8535 vex_reg = op++;
8536 gas_assert (op < i.operands);
c0f3af97
L
8537 }
8538 else
c0f3af97 8539 {
f12dc422
L
8540 /* Check register-only source operand when two source
8541 operands are swapped. */
8542 if (!i.tm.operand_types[op].bitfield.baseindex
8543 && i.tm.operand_types[op + 1].bitfield.baseindex)
8544 {
8545 vex_reg = op;
8546 op += 2;
8547 gas_assert (mem == (vex_reg + 1)
8548 && op < i.operands);
8549 }
8550 else
8551 {
8552 vex_reg = op + 1;
8553 gas_assert (vex_reg < i.operands);
8554 }
c0f3af97 8555 }
7ab9ffdd 8556 }
2426c15f 8557 else if (i.tm.opcode_modifier.vexvvvv == VEXNDD)
7ab9ffdd 8558 {
f12dc422 8559 /* For instructions with VexNDD, the register destination
7ab9ffdd 8560 is encoded in VEX prefix. */
f12dc422
L
8561 if (i.mem_operands == 0)
8562 {
8563 /* There is no memory operand. */
8564 gas_assert ((op + 2) == i.operands);
8565 vex_reg = op + 1;
8566 }
8567 else
8d63c93e 8568 {
ed438a93
JB
8569 /* There are only 2 non-immediate operands. */
8570 gas_assert (op < i.imm_operands + 2
8571 && i.operands == i.imm_operands + 2);
8572 vex_reg = i.imm_operands + 1;
f12dc422 8573 }
7ab9ffdd
L
8574 }
8575 else
8576 gas_assert (op < i.operands);
99018f42 8577
7ab9ffdd
L
8578 if (vex_reg != (unsigned int) ~0)
8579 {
f12dc422 8580 i386_operand_type *type = &i.tm.operand_types[vex_reg];
7ab9ffdd 8581
bab6aec1 8582 if ((type->bitfield.class != Reg
dc821c5f 8583 || (!type->bitfield.dword && !type->bitfield.qword))
3528c362 8584 && type->bitfield.class != RegSIMD
43234a1e 8585 && !operand_type_equal (type, &regmask))
7ab9ffdd 8586 abort ();
f88c9eb0 8587
7ab9ffdd
L
8588 i.vex.register_specifier = i.op[vex_reg].regs;
8589 }
8590
1b9f0c97
L
8591 /* Don't set OP operand twice. */
8592 if (vex_reg != op)
7ab9ffdd 8593 {
1b9f0c97
L
8594 /* If there is an extension opcode to put here, the
8595 register number must be put into the regmem field. */
8596 if (i.tm.extension_opcode != None)
8597 {
8598 i.rm.regmem = i.op[op].regs->reg_num;
a5aeccd9
JB
8599 set_rex_vrex (i.op[op].regs, REX_B,
8600 i.tm.opcode_modifier.sse2avx);
1b9f0c97
L
8601 }
8602 else
8603 {
8604 i.rm.reg = i.op[op].regs->reg_num;
a5aeccd9
JB
8605 set_rex_vrex (i.op[op].regs, REX_R,
8606 i.tm.opcode_modifier.sse2avx);
1b9f0c97 8607 }
7ab9ffdd 8608 }
252b5132 8609
29b0f896
AM
8610 /* Now, if no memory operand has set i.rm.mode = 0, 1, 2 we
8611 must set it to 3 to indicate this is a register operand
8612 in the regmem field. */
8613 if (!i.mem_operands)
8614 i.rm.mode = 3;
8615 }
252b5132 8616
29b0f896 8617 /* Fill in i.rm.reg field with extension opcode (if any). */
c1e679ec 8618 if (i.tm.extension_opcode != None)
29b0f896
AM
8619 i.rm.reg = i.tm.extension_opcode;
8620 }
8621 return default_seg;
8622}
252b5132 8623
48ef937e
JB
8624static INLINE void
8625frag_opcode_byte (unsigned char byte)
8626{
8627 if (now_seg != absolute_section)
8628 FRAG_APPEND_1_CHAR (byte);
8629 else
8630 ++abs_section_offset;
8631}
8632
376cd056
JB
8633static unsigned int
8634flip_code16 (unsigned int code16)
8635{
8636 gas_assert (i.tm.operands == 1);
8637
8638 return !(i.prefix[REX_PREFIX] & REX_W)
8639 && (code16 ? i.tm.operand_types[0].bitfield.disp32
376cd056
JB
8640 : i.tm.operand_types[0].bitfield.disp16)
8641 ? CODE16 : 0;
8642}
8643
29b0f896 8644static void
e3bb37b5 8645output_branch (void)
29b0f896
AM
8646{
8647 char *p;
f8a5c266 8648 int size;
29b0f896
AM
8649 int code16;
8650 int prefix;
8651 relax_substateT subtype;
8652 symbolS *sym;
8653 offsetT off;
8654
48ef937e
JB
8655 if (now_seg == absolute_section)
8656 {
8657 as_bad (_("relaxable branches not supported in absolute section"));
8658 return;
8659 }
8660
f8a5c266 8661 code16 = flag_code == CODE_16BIT ? CODE16 : 0;
1a42a9fe 8662 size = i.disp_encoding > disp_encoding_8bit ? BIG : SMALL;
29b0f896
AM
8663
8664 prefix = 0;
8665 if (i.prefix[DATA_PREFIX] != 0)
252b5132 8666 {
29b0f896
AM
8667 prefix = 1;
8668 i.prefixes -= 1;
376cd056 8669 code16 ^= flip_code16(code16);
252b5132 8670 }
29b0f896
AM
8671 /* Pentium4 branch hints. */
8672 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8673 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
2f66722d 8674 {
29b0f896
AM
8675 prefix++;
8676 i.prefixes--;
8677 }
8678 if (i.prefix[REX_PREFIX] != 0)
8679 {
8680 prefix++;
8681 i.prefixes--;
2f66722d
AM
8682 }
8683
7e8b059b
L
8684 /* BND prefixed jump. */
8685 if (i.prefix[BND_PREFIX] != 0)
8686 {
6cb0a70e
JB
8687 prefix++;
8688 i.prefixes--;
7e8b059b
L
8689 }
8690
f2810fe0
JB
8691 if (i.prefixes != 0)
8692 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
29b0f896
AM
8693
8694 /* It's always a symbol; End frag & setup for relax.
8695 Make sure there is enough room in this frag for the largest
8696 instruction we may generate in md_convert_frag. This is 2
8697 bytes for the opcode and room for the prefix and largest
8698 displacement. */
8699 frag_grow (prefix + 2 + 4);
8700 /* Prefix and 1 opcode byte go in fr_fix. */
8701 p = frag_more (prefix + 1);
8702 if (i.prefix[DATA_PREFIX] != 0)
8703 *p++ = DATA_PREFIX_OPCODE;
8704 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
8705 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
8706 *p++ = i.prefix[SEG_PREFIX];
6cb0a70e
JB
8707 if (i.prefix[BND_PREFIX] != 0)
8708 *p++ = BND_PREFIX_OPCODE;
29b0f896
AM
8709 if (i.prefix[REX_PREFIX] != 0)
8710 *p++ = i.prefix[REX_PREFIX];
8711 *p = i.tm.base_opcode;
8712
8713 if ((unsigned char) *p == JUMP_PC_RELATIVE)
f8a5c266 8714 subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, size);
40fb9820 8715 else if (cpu_arch_flags.bitfield.cpui386)
f8a5c266 8716 subtype = ENCODE_RELAX_STATE (COND_JUMP, size);
29b0f896 8717 else
f8a5c266 8718 subtype = ENCODE_RELAX_STATE (COND_JUMP86, size);
29b0f896 8719 subtype |= code16;
3e73aa7c 8720
29b0f896
AM
8721 sym = i.op[0].disps->X_add_symbol;
8722 off = i.op[0].disps->X_add_number;
3e73aa7c 8723
29b0f896
AM
8724 if (i.op[0].disps->X_op != O_constant
8725 && i.op[0].disps->X_op != O_symbol)
3e73aa7c 8726 {
29b0f896
AM
8727 /* Handle complex expressions. */
8728 sym = make_expr_symbol (i.op[0].disps);
8729 off = 0;
8730 }
3e73aa7c 8731
1ef3994a
JB
8732 frag_now->tc_frag_data.code64 = flag_code == CODE_64BIT;
8733
29b0f896
AM
8734 /* 1 possible extra opcode + 4 byte displacement go in var part.
8735 Pass reloc in fr_var. */
d258b828 8736 frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
29b0f896 8737}
3e73aa7c 8738
bd7ab16b
L
8739#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8740/* Return TRUE iff PLT32 relocation should be used for branching to
8741 symbol S. */
8742
5b7c81bd 8743static bool
bd7ab16b
L
8744need_plt32_p (symbolS *s)
8745{
8746 /* PLT32 relocation is ELF only. */
8747 if (!IS_ELF)
5b7c81bd 8748 return false;
bd7ab16b 8749
a5def729
RO
8750#ifdef TE_SOLARIS
8751 /* Don't emit PLT32 relocation on Solaris: neither native linker nor
8752 krtld support it. */
5b7c81bd 8753 return false;
a5def729
RO
8754#endif
8755
bd7ab16b
L
8756 /* Since there is no need to prepare for PLT branch on x86-64, we
8757 can generate R_X86_64_PLT32, instead of R_X86_64_PC32, which can
8758 be used as a marker for 32-bit PC-relative branches. */
8759 if (!object_64bit)
5b7c81bd 8760 return false;
bd7ab16b 8761
44365e88 8762 if (s == NULL)
5b7c81bd 8763 return false;
44365e88 8764
bd7ab16b
L
8765 /* Weak or undefined symbol need PLT32 relocation. */
8766 if (S_IS_WEAK (s) || !S_IS_DEFINED (s))
5b7c81bd 8767 return true;
bd7ab16b
L
8768
8769 /* Non-global symbol doesn't need PLT32 relocation. */
8770 if (! S_IS_EXTERNAL (s))
5b7c81bd 8771 return false;
bd7ab16b
L
8772
8773 /* Other global symbols need PLT32 relocation. NB: Symbol with
8774 non-default visibilities are treated as normal global symbol
8775 so that PLT32 relocation can be used as a marker for 32-bit
8776 PC-relative branches. It is useful for linker relaxation. */
5b7c81bd 8777 return true;
bd7ab16b
L
8778}
8779#endif
8780
29b0f896 8781static void
e3bb37b5 8782output_jump (void)
29b0f896
AM
8783{
8784 char *p;
8785 int size;
3e02c1cc 8786 fixS *fixP;
bd7ab16b 8787 bfd_reloc_code_real_type jump_reloc = i.reloc[0];
29b0f896 8788
0cfa3eb3 8789 if (i.tm.opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
8790 {
8791 /* This is a loop or jecxz type instruction. */
8792 size = 1;
8793 if (i.prefix[ADDR_PREFIX] != 0)
8794 {
48ef937e 8795 frag_opcode_byte (ADDR_PREFIX_OPCODE);
29b0f896
AM
8796 i.prefixes -= 1;
8797 }
8798 /* Pentium4 branch hints. */
8799 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8800 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
8801 {
48ef937e 8802 frag_opcode_byte (i.prefix[SEG_PREFIX]);
29b0f896 8803 i.prefixes--;
3e73aa7c
JH
8804 }
8805 }
29b0f896
AM
8806 else
8807 {
8808 int code16;
3e73aa7c 8809
29b0f896
AM
8810 code16 = 0;
8811 if (flag_code == CODE_16BIT)
8812 code16 = CODE16;
3e73aa7c 8813
29b0f896
AM
8814 if (i.prefix[DATA_PREFIX] != 0)
8815 {
48ef937e 8816 frag_opcode_byte (DATA_PREFIX_OPCODE);
29b0f896 8817 i.prefixes -= 1;
376cd056 8818 code16 ^= flip_code16(code16);
29b0f896 8819 }
252b5132 8820
29b0f896
AM
8821 size = 4;
8822 if (code16)
8823 size = 2;
8824 }
9fcc94b6 8825
6cb0a70e
JB
8826 /* BND prefixed jump. */
8827 if (i.prefix[BND_PREFIX] != 0)
29b0f896 8828 {
48ef937e 8829 frag_opcode_byte (i.prefix[BND_PREFIX]);
29b0f896
AM
8830 i.prefixes -= 1;
8831 }
252b5132 8832
6cb0a70e 8833 if (i.prefix[REX_PREFIX] != 0)
7e8b059b 8834 {
48ef937e 8835 frag_opcode_byte (i.prefix[REX_PREFIX]);
7e8b059b
L
8836 i.prefixes -= 1;
8837 }
8838
f2810fe0
JB
8839 if (i.prefixes != 0)
8840 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
e0890092 8841
48ef937e
JB
8842 if (now_seg == absolute_section)
8843 {
9a182d04 8844 abs_section_offset += i.opcode_length + size;
48ef937e
JB
8845 return;
8846 }
8847
9a182d04
JB
8848 p = frag_more (i.opcode_length + size);
8849 switch (i.opcode_length)
42164a71
L
8850 {
8851 case 2:
8852 *p++ = i.tm.base_opcode >> 8;
1a0670f3 8853 /* Fall through. */
42164a71
L
8854 case 1:
8855 *p++ = i.tm.base_opcode;
8856 break;
8857 default:
8858 abort ();
8859 }
e0890092 8860
bd7ab16b 8861#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1ef3994a
JB
8862 if (flag_code == CODE_64BIT && size == 4
8863 && jump_reloc == NO_RELOC && i.op[0].disps->X_add_number == 0
bd7ab16b
L
8864 && need_plt32_p (i.op[0].disps->X_add_symbol))
8865 jump_reloc = BFD_RELOC_X86_64_PLT32;
8866#endif
8867
8868 jump_reloc = reloc (size, 1, 1, jump_reloc);
8869
3e02c1cc 8870 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
bd7ab16b 8871 i.op[0].disps, 1, jump_reloc);
3e02c1cc 8872
eb19308f
JB
8873 /* All jumps handled here are signed, but don't unconditionally use a
8874 signed limit check for 32 and 16 bit jumps as we want to allow wrap
8875 around at 4G (outside of 64-bit mode) and 64k (except for XBEGIN)
8876 respectively. */
8877 switch (size)
8878 {
8879 case 1:
8880 fixP->fx_signed = 1;
8881 break;
8882
8883 case 2:
8884 if (i.tm.base_opcode == 0xc7f8)
8885 fixP->fx_signed = 1;
8886 break;
8887
8888 case 4:
8889 if (flag_code == CODE_64BIT)
8890 fixP->fx_signed = 1;
8891 break;
8892 }
29b0f896 8893}
e0890092 8894
29b0f896 8895static void
e3bb37b5 8896output_interseg_jump (void)
29b0f896
AM
8897{
8898 char *p;
8899 int size;
8900 int prefix;
8901 int code16;
252b5132 8902
29b0f896
AM
8903 code16 = 0;
8904 if (flag_code == CODE_16BIT)
8905 code16 = CODE16;
a217f122 8906
29b0f896
AM
8907 prefix = 0;
8908 if (i.prefix[DATA_PREFIX] != 0)
8909 {
8910 prefix = 1;
8911 i.prefixes -= 1;
8912 code16 ^= CODE16;
8913 }
6cb0a70e
JB
8914
8915 gas_assert (!i.prefix[REX_PREFIX]);
252b5132 8916
29b0f896
AM
8917 size = 4;
8918 if (code16)
8919 size = 2;
252b5132 8920
f2810fe0
JB
8921 if (i.prefixes != 0)
8922 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
252b5132 8923
48ef937e
JB
8924 if (now_seg == absolute_section)
8925 {
8926 abs_section_offset += prefix + 1 + 2 + size;
8927 return;
8928 }
8929
29b0f896
AM
8930 /* 1 opcode; 2 segment; offset */
8931 p = frag_more (prefix + 1 + 2 + size);
3e73aa7c 8932
29b0f896
AM
8933 if (i.prefix[DATA_PREFIX] != 0)
8934 *p++ = DATA_PREFIX_OPCODE;
252b5132 8935
29b0f896
AM
8936 if (i.prefix[REX_PREFIX] != 0)
8937 *p++ = i.prefix[REX_PREFIX];
252b5132 8938
29b0f896
AM
8939 *p++ = i.tm.base_opcode;
8940 if (i.op[1].imms->X_op == O_constant)
8941 {
8942 offsetT n = i.op[1].imms->X_add_number;
252b5132 8943
29b0f896
AM
8944 if (size == 2
8945 && !fits_in_unsigned_word (n)
8946 && !fits_in_signed_word (n))
8947 {
8948 as_bad (_("16-bit jump out of range"));
8949 return;
8950 }
8951 md_number_to_chars (p, n, size);
8952 }
8953 else
8954 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
d258b828 8955 i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
6d96a594
C
8956
8957 p += size;
8958 if (i.op[0].imms->X_op == O_constant)
8959 md_number_to_chars (p, (valueT) i.op[0].imms->X_add_number, 2);
8960 else
8961 fix_new_exp (frag_now, p - frag_now->fr_literal, 2,
8962 i.op[0].imms, 0, reloc (2, 0, 0, i.reloc[0]));
29b0f896 8963}
a217f122 8964
b4a3a7b4
L
8965#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8966void
8967x86_cleanup (void)
8968{
8969 char *p;
8970 asection *seg = now_seg;
8971 subsegT subseg = now_subseg;
8972 asection *sec;
8973 unsigned int alignment, align_size_1;
8974 unsigned int isa_1_descsz, feature_2_descsz, descsz;
8975 unsigned int isa_1_descsz_raw, feature_2_descsz_raw;
8976 unsigned int padding;
8977
1273b2f8 8978 if (!IS_ELF || !x86_used_note)
b4a3a7b4
L
8979 return;
8980
b4a3a7b4
L
8981 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X86;
8982
8983 /* The .note.gnu.property section layout:
8984
8985 Field Length Contents
8986 ---- ---- ----
8987 n_namsz 4 4
8988 n_descsz 4 The note descriptor size
8989 n_type 4 NT_GNU_PROPERTY_TYPE_0
8990 n_name 4 "GNU"
8991 n_desc n_descsz The program property array
8992 .... .... ....
8993 */
8994
8995 /* Create the .note.gnu.property section. */
8996 sec = subseg_new (NOTE_GNU_PROPERTY_SECTION_NAME, 0);
fd361982 8997 bfd_set_section_flags (sec,
b4a3a7b4
L
8998 (SEC_ALLOC
8999 | SEC_LOAD
9000 | SEC_DATA
9001 | SEC_HAS_CONTENTS
9002 | SEC_READONLY));
9003
9004 if (get_elf_backend_data (stdoutput)->s->elfclass == ELFCLASS64)
9005 {
9006 align_size_1 = 7;
9007 alignment = 3;
9008 }
9009 else
9010 {
9011 align_size_1 = 3;
9012 alignment = 2;
9013 }
9014
fd361982 9015 bfd_set_section_alignment (sec, alignment);
b4a3a7b4
L
9016 elf_section_type (sec) = SHT_NOTE;
9017
1273b2f8
L
9018 /* GNU_PROPERTY_X86_ISA_1_USED: 4-byte type + 4-byte data size
9019 + 4-byte data */
9020 isa_1_descsz_raw = 4 + 4 + 4;
9021 /* Align GNU_PROPERTY_X86_ISA_1_USED. */
9022 isa_1_descsz = (isa_1_descsz_raw + align_size_1) & ~align_size_1;
b4a3a7b4
L
9023
9024 feature_2_descsz_raw = isa_1_descsz;
9025 /* GNU_PROPERTY_X86_FEATURE_2_USED: 4-byte type + 4-byte data size
1273b2f8 9026 + 4-byte data */
b4a3a7b4
L
9027 feature_2_descsz_raw += 4 + 4 + 4;
9028 /* Align GNU_PROPERTY_X86_FEATURE_2_USED. */
9029 feature_2_descsz = ((feature_2_descsz_raw + align_size_1)
9030 & ~align_size_1);
9031
9032 descsz = feature_2_descsz;
9033 /* Section size: n_namsz + n_descsz + n_type + n_name + n_descsz. */
9034 p = frag_more (4 + 4 + 4 + 4 + descsz);
9035
9036 /* Write n_namsz. */
9037 md_number_to_chars (p, (valueT) 4, 4);
9038
9039 /* Write n_descsz. */
9040 md_number_to_chars (p + 4, (valueT) descsz, 4);
9041
9042 /* Write n_type. */
9043 md_number_to_chars (p + 4 * 2, (valueT) NT_GNU_PROPERTY_TYPE_0, 4);
9044
9045 /* Write n_name. */
9046 memcpy (p + 4 * 3, "GNU", 4);
9047
1273b2f8
L
9048 /* Write 4-byte type. */
9049 md_number_to_chars (p + 4 * 4,
9050 (valueT) GNU_PROPERTY_X86_ISA_1_USED, 4);
b4a3a7b4 9051
1273b2f8
L
9052 /* Write 4-byte data size. */
9053 md_number_to_chars (p + 4 * 5, (valueT) 4, 4);
b4a3a7b4 9054
1273b2f8
L
9055 /* Write 4-byte data. */
9056 md_number_to_chars (p + 4 * 6, (valueT) x86_isa_1_used, 4);
b4a3a7b4 9057
1273b2f8
L
9058 /* Zero out paddings. */
9059 padding = isa_1_descsz - isa_1_descsz_raw;
9060 if (padding)
9061 memset (p + 4 * 7, 0, padding);
b4a3a7b4
L
9062
9063 /* Write 4-byte type. */
9064 md_number_to_chars (p + isa_1_descsz + 4 * 4,
9065 (valueT) GNU_PROPERTY_X86_FEATURE_2_USED, 4);
9066
9067 /* Write 4-byte data size. */
9068 md_number_to_chars (p + isa_1_descsz + 4 * 5, (valueT) 4, 4);
9069
9070 /* Write 4-byte data. */
9071 md_number_to_chars (p + isa_1_descsz + 4 * 6,
9072 (valueT) x86_feature_2_used, 4);
9073
9074 /* Zero out paddings. */
9075 padding = feature_2_descsz - feature_2_descsz_raw;
9076 if (padding)
9077 memset (p + isa_1_descsz + 4 * 7, 0, padding);
9078
9079 /* We probably can't restore the current segment, for there likely
9080 isn't one yet... */
9081 if (seg && subseg)
9082 subseg_set (seg, subseg);
9083}
9084#endif
9085
9c33702b
JB
9086static unsigned int
9087encoding_length (const fragS *start_frag, offsetT start_off,
9088 const char *frag_now_ptr)
9089{
9090 unsigned int len = 0;
9091
9092 if (start_frag != frag_now)
9093 {
9094 const fragS *fr = start_frag;
9095
9096 do {
9097 len += fr->fr_fix;
9098 fr = fr->fr_next;
9099 } while (fr && fr != frag_now);
9100 }
9101
9102 return len - start_off + (frag_now_ptr - frag_now->fr_literal);
9103}
9104
e379e5f3 9105/* Return 1 for test, and, cmp, add, sub, inc and dec which may
79d72f45
HL
9106 be macro-fused with conditional jumps.
9107 NB: If TEST/AND/CMP/ADD/SUB/INC/DEC is of RIP relative address,
9108 or is one of the following format:
9109
9110 cmp m, imm
9111 add m, imm
9112 sub m, imm
9113 test m, imm
9114 and m, imm
9115 inc m
9116 dec m
9117
9118 it is unfusible. */
e379e5f3
L
9119
9120static int
79d72f45 9121maybe_fused_with_jcc_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
9122{
9123 /* No RIP address. */
9124 if (i.base_reg && i.base_reg->reg_num == RegIP)
9125 return 0;
9126
389d00a5
JB
9127 /* No opcodes outside of base encoding space. */
9128 if (i.tm.opcode_modifier.opcodespace != SPACE_BASE)
e379e5f3
L
9129 return 0;
9130
79d72f45
HL
9131 /* add, sub without add/sub m, imm. */
9132 if (i.tm.base_opcode <= 5
e379e5f3
L
9133 || (i.tm.base_opcode >= 0x28 && i.tm.base_opcode <= 0x2d)
9134 || ((i.tm.base_opcode | 3) == 0x83
79d72f45 9135 && (i.tm.extension_opcode == 0x5
e379e5f3 9136 || i.tm.extension_opcode == 0x0)))
79d72f45
HL
9137 {
9138 *mf_cmp_p = mf_cmp_alu_cmp;
9139 return !(i.mem_operands && i.imm_operands);
9140 }
e379e5f3 9141
79d72f45
HL
9142 /* and without and m, imm. */
9143 if ((i.tm.base_opcode >= 0x20 && i.tm.base_opcode <= 0x25)
9144 || ((i.tm.base_opcode | 3) == 0x83
9145 && i.tm.extension_opcode == 0x4))
9146 {
9147 *mf_cmp_p = mf_cmp_test_and;
9148 return !(i.mem_operands && i.imm_operands);
9149 }
9150
9151 /* test without test m imm. */
e379e5f3
L
9152 if ((i.tm.base_opcode | 1) == 0x85
9153 || (i.tm.base_opcode | 1) == 0xa9
9154 || ((i.tm.base_opcode | 1) == 0xf7
79d72f45
HL
9155 && i.tm.extension_opcode == 0))
9156 {
9157 *mf_cmp_p = mf_cmp_test_and;
9158 return !(i.mem_operands && i.imm_operands);
9159 }
9160
9161 /* cmp without cmp m, imm. */
9162 if ((i.tm.base_opcode >= 0x38 && i.tm.base_opcode <= 0x3d)
e379e5f3
L
9163 || ((i.tm.base_opcode | 3) == 0x83
9164 && (i.tm.extension_opcode == 0x7)))
79d72f45
HL
9165 {
9166 *mf_cmp_p = mf_cmp_alu_cmp;
9167 return !(i.mem_operands && i.imm_operands);
9168 }
e379e5f3 9169
79d72f45 9170 /* inc, dec without inc/dec m. */
e379e5f3
L
9171 if ((i.tm.cpu_flags.bitfield.cpuno64
9172 && (i.tm.base_opcode | 0xf) == 0x4f)
9173 || ((i.tm.base_opcode | 1) == 0xff
9174 && i.tm.extension_opcode <= 0x1))
79d72f45
HL
9175 {
9176 *mf_cmp_p = mf_cmp_incdec;
9177 return !i.mem_operands;
9178 }
e379e5f3
L
9179
9180 return 0;
9181}
9182
9183/* Return 1 if a FUSED_JCC_PADDING frag should be generated. */
9184
9185static int
79d72f45 9186add_fused_jcc_padding_frag_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
9187{
9188 /* NB: Don't work with COND_JUMP86 without i386. */
9189 if (!align_branch_power
9190 || now_seg == absolute_section
9191 || !cpu_arch_flags.bitfield.cpui386
9192 || !(align_branch & align_branch_fused_bit))
9193 return 0;
9194
79d72f45 9195 if (maybe_fused_with_jcc_p (mf_cmp_p))
e379e5f3
L
9196 {
9197 if (last_insn.kind == last_insn_other
9198 || last_insn.seg != now_seg)
9199 return 1;
9200 if (flag_debug)
9201 as_warn_where (last_insn.file, last_insn.line,
9202 _("`%s` skips -malign-branch-boundary on `%s`"),
9203 last_insn.name, i.tm.name);
9204 }
9205
9206 return 0;
9207}
9208
9209/* Return 1 if a BRANCH_PREFIX frag should be generated. */
9210
9211static int
9212add_branch_prefix_frag_p (void)
9213{
9214 /* NB: Don't work with COND_JUMP86 without i386. Don't add prefix
9215 to PadLock instructions since they include prefixes in opcode. */
9216 if (!align_branch_power
9217 || !align_branch_prefix_size
9218 || now_seg == absolute_section
9219 || i.tm.cpu_flags.bitfield.cpupadlock
9220 || !cpu_arch_flags.bitfield.cpui386)
9221 return 0;
9222
9223 /* Don't add prefix if it is a prefix or there is no operand in case
9224 that segment prefix is special. */
9225 if (!i.operands || i.tm.opcode_modifier.isprefix)
9226 return 0;
9227
9228 if (last_insn.kind == last_insn_other
9229 || last_insn.seg != now_seg)
9230 return 1;
9231
9232 if (flag_debug)
9233 as_warn_where (last_insn.file, last_insn.line,
9234 _("`%s` skips -malign-branch-boundary on `%s`"),
9235 last_insn.name, i.tm.name);
9236
9237 return 0;
9238}
9239
9240/* Return 1 if a BRANCH_PADDING frag should be generated. */
9241
9242static int
79d72f45
HL
9243add_branch_padding_frag_p (enum align_branch_kind *branch_p,
9244 enum mf_jcc_kind *mf_jcc_p)
e379e5f3
L
9245{
9246 int add_padding;
9247
9248 /* NB: Don't work with COND_JUMP86 without i386. */
9249 if (!align_branch_power
9250 || now_seg == absolute_section
389d00a5
JB
9251 || !cpu_arch_flags.bitfield.cpui386
9252 || i.tm.opcode_modifier.opcodespace != SPACE_BASE)
e379e5f3
L
9253 return 0;
9254
9255 add_padding = 0;
9256
9257 /* Check for jcc and direct jmp. */
9258 if (i.tm.opcode_modifier.jump == JUMP)
9259 {
9260 if (i.tm.base_opcode == JUMP_PC_RELATIVE)
9261 {
9262 *branch_p = align_branch_jmp;
9263 add_padding = align_branch & align_branch_jmp_bit;
9264 }
9265 else
9266 {
79d72f45
HL
9267 /* Because J<cc> and JN<cc> share same group in macro-fusible table,
9268 igore the lowest bit. */
9269 *mf_jcc_p = (i.tm.base_opcode & 0x0e) >> 1;
e379e5f3
L
9270 *branch_p = align_branch_jcc;
9271 if ((align_branch & align_branch_jcc_bit))
9272 add_padding = 1;
9273 }
9274 }
e379e5f3
L
9275 else if ((i.tm.base_opcode | 1) == 0xc3)
9276 {
9277 /* Near ret. */
9278 *branch_p = align_branch_ret;
9279 if ((align_branch & align_branch_ret_bit))
9280 add_padding = 1;
9281 }
9282 else
9283 {
9284 /* Check for indirect jmp, direct and indirect calls. */
9285 if (i.tm.base_opcode == 0xe8)
9286 {
9287 /* Direct call. */
9288 *branch_p = align_branch_call;
9289 if ((align_branch & align_branch_call_bit))
9290 add_padding = 1;
9291 }
9292 else if (i.tm.base_opcode == 0xff
9293 && (i.tm.extension_opcode == 2
9294 || i.tm.extension_opcode == 4))
9295 {
9296 /* Indirect call and jmp. */
9297 *branch_p = align_branch_indirect;
9298 if ((align_branch & align_branch_indirect_bit))
9299 add_padding = 1;
9300 }
9301
9302 if (add_padding
9303 && i.disp_operands
9304 && tls_get_addr
9305 && (i.op[0].disps->X_op == O_symbol
9306 || (i.op[0].disps->X_op == O_subtract
9307 && i.op[0].disps->X_op_symbol == GOT_symbol)))
9308 {
9309 symbolS *s = i.op[0].disps->X_add_symbol;
9310 /* No padding to call to global or undefined tls_get_addr. */
9311 if ((S_IS_EXTERNAL (s) || !S_IS_DEFINED (s))
9312 && strcmp (S_GET_NAME (s), tls_get_addr) == 0)
9313 return 0;
9314 }
9315 }
9316
9317 if (add_padding
9318 && last_insn.kind != last_insn_other
9319 && last_insn.seg == now_seg)
9320 {
9321 if (flag_debug)
9322 as_warn_where (last_insn.file, last_insn.line,
9323 _("`%s` skips -malign-branch-boundary on `%s`"),
9324 last_insn.name, i.tm.name);
9325 return 0;
9326 }
9327
9328 return add_padding;
9329}
9330
29b0f896 9331static void
e3bb37b5 9332output_insn (void)
29b0f896 9333{
2bbd9c25
JJ
9334 fragS *insn_start_frag;
9335 offsetT insn_start_off;
e379e5f3
L
9336 fragS *fragP = NULL;
9337 enum align_branch_kind branch = align_branch_none;
79d72f45
HL
9338 /* The initializer is arbitrary just to avoid uninitialized error.
9339 it's actually either assigned in add_branch_padding_frag_p
9340 or never be used. */
9341 enum mf_jcc_kind mf_jcc = mf_jcc_jo;
2bbd9c25 9342
b4a3a7b4 9343#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
48ef937e 9344 if (IS_ELF && x86_used_note && now_seg != absolute_section)
b4a3a7b4 9345 {
32930e4e
L
9346 if ((i.xstate & xstate_tmm) == xstate_tmm
9347 || i.tm.cpu_flags.bitfield.cpuamx_tile)
9348 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_TMM;
9349
b4a3a7b4
L
9350 if (i.tm.cpu_flags.bitfield.cpu8087
9351 || i.tm.cpu_flags.bitfield.cpu287
9352 || i.tm.cpu_flags.bitfield.cpu387
9353 || i.tm.cpu_flags.bitfield.cpu687
9354 || i.tm.cpu_flags.bitfield.cpufisttp)
9355 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X87;
014d61ea 9356
921eafea 9357 if ((i.xstate & xstate_mmx)
389d00a5
JB
9358 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
9359 && !is_any_vex_encoding (&i.tm)
9360 && (i.tm.base_opcode == 0x77 /* emms */
9361 || i.tm.base_opcode == 0x0e /* femms */)))
b4a3a7b4 9362 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MMX;
014d61ea 9363
32930e4e
L
9364 if (i.index_reg)
9365 {
9366 if (i.index_reg->reg_type.bitfield.zmmword)
9367 i.xstate |= xstate_zmm;
9368 else if (i.index_reg->reg_type.bitfield.ymmword)
9369 i.xstate |= xstate_ymm;
9370 else if (i.index_reg->reg_type.bitfield.xmmword)
9371 i.xstate |= xstate_xmm;
9372 }
014d61ea
JB
9373
9374 /* vzeroall / vzeroupper */
9375 if (i.tm.base_opcode == 0x77 && i.tm.cpu_flags.bitfield.cpuavx)
9376 i.xstate |= xstate_ymm;
9377
c4694f17 9378 if ((i.xstate & xstate_xmm)
389d00a5
JB
9379 /* ldmxcsr / stmxcsr / vldmxcsr / vstmxcsr */
9380 || (i.tm.base_opcode == 0xae
9381 && (i.tm.cpu_flags.bitfield.cpusse
9382 || i.tm.cpu_flags.bitfield.cpuavx))
c4694f17
TG
9383 || i.tm.cpu_flags.bitfield.cpuwidekl
9384 || i.tm.cpu_flags.bitfield.cpukl)
b4a3a7b4 9385 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XMM;
014d61ea 9386
921eafea 9387 if ((i.xstate & xstate_ymm) == xstate_ymm)
b4a3a7b4 9388 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_YMM;
921eafea 9389 if ((i.xstate & xstate_zmm) == xstate_zmm)
b4a3a7b4 9390 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_ZMM;
6225c532 9391 if (i.mask.reg || (i.xstate & xstate_mask) == xstate_mask)
32930e4e 9392 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MASK;
b4a3a7b4
L
9393 if (i.tm.cpu_flags.bitfield.cpufxsr)
9394 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_FXSR;
9395 if (i.tm.cpu_flags.bitfield.cpuxsave)
9396 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVE;
9397 if (i.tm.cpu_flags.bitfield.cpuxsaveopt)
9398 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT;
9399 if (i.tm.cpu_flags.bitfield.cpuxsavec)
9400 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEC;
b0ab0693
L
9401
9402 if (x86_feature_2_used
9403 || i.tm.cpu_flags.bitfield.cpucmov
9404 || i.tm.cpu_flags.bitfield.cpusyscall
389d00a5
JB
9405 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
9406 && i.tm.base_opcode == 0xc7
70e95837 9407 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
b0ab0693
L
9408 && i.tm.extension_opcode == 1) /* cmpxchg8b */)
9409 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_BASELINE;
9410 if (i.tm.cpu_flags.bitfield.cpusse3
9411 || i.tm.cpu_flags.bitfield.cpussse3
9412 || i.tm.cpu_flags.bitfield.cpusse4_1
9413 || i.tm.cpu_flags.bitfield.cpusse4_2
9414 || i.tm.cpu_flags.bitfield.cpucx16
9415 || i.tm.cpu_flags.bitfield.cpupopcnt
9416 /* LAHF-SAHF insns in 64-bit mode. */
9417 || (flag_code == CODE_64BIT
35648716
JB
9418 && (i.tm.base_opcode | 1) == 0x9f
9419 && i.tm.opcode_modifier.opcodespace == SPACE_BASE))
b0ab0693
L
9420 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V2;
9421 if (i.tm.cpu_flags.bitfield.cpuavx
9422 || i.tm.cpu_flags.bitfield.cpuavx2
a9860005
JB
9423 /* Any VEX encoded insns execpt for AVX512F, AVX512BW, AVX512DQ,
9424 XOP, FMA4, LPW, TBM, and AMX. */
b0ab0693
L
9425 || (i.tm.opcode_modifier.vex
9426 && !i.tm.cpu_flags.bitfield.cpuavx512f
9427 && !i.tm.cpu_flags.bitfield.cpuavx512bw
9428 && !i.tm.cpu_flags.bitfield.cpuavx512dq
a9860005
JB
9429 && !i.tm.cpu_flags.bitfield.cpuxop
9430 && !i.tm.cpu_flags.bitfield.cpufma4
b0ab0693
L
9431 && !i.tm.cpu_flags.bitfield.cpulwp
9432 && !i.tm.cpu_flags.bitfield.cputbm
9433 && !(x86_feature_2_used & GNU_PROPERTY_X86_FEATURE_2_TMM))
9434 || i.tm.cpu_flags.bitfield.cpuf16c
9435 || i.tm.cpu_flags.bitfield.cpufma
9436 || i.tm.cpu_flags.bitfield.cpulzcnt
9437 || i.tm.cpu_flags.bitfield.cpumovbe
9438 || i.tm.cpu_flags.bitfield.cpuxsaves
9439 || (x86_feature_2_used
9440 & (GNU_PROPERTY_X86_FEATURE_2_XSAVE
9441 | GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT
9442 | GNU_PROPERTY_X86_FEATURE_2_XSAVEC)) != 0)
9443 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V3;
9444 if (i.tm.cpu_flags.bitfield.cpuavx512f
9445 || i.tm.cpu_flags.bitfield.cpuavx512bw
9446 || i.tm.cpu_flags.bitfield.cpuavx512dq
9447 || i.tm.cpu_flags.bitfield.cpuavx512vl
a9860005
JB
9448 /* Any EVEX encoded insns except for AVX512ER, AVX512PF,
9449 AVX512-4FMAPS, and AVX512-4VNNIW. */
b0ab0693
L
9450 || (i.tm.opcode_modifier.evex
9451 && !i.tm.cpu_flags.bitfield.cpuavx512er
9452 && !i.tm.cpu_flags.bitfield.cpuavx512pf
a9860005 9453 && !i.tm.cpu_flags.bitfield.cpuavx512_4fmaps
b0ab0693
L
9454 && !i.tm.cpu_flags.bitfield.cpuavx512_4vnniw))
9455 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V4;
b4a3a7b4
L
9456 }
9457#endif
9458
29b0f896
AM
9459 /* Tie dwarf2 debug info to the address at the start of the insn.
9460 We can't do this after the insn has been output as the current
9461 frag may have been closed off. eg. by frag_var. */
9462 dwarf2_emit_insn (0);
9463
2bbd9c25
JJ
9464 insn_start_frag = frag_now;
9465 insn_start_off = frag_now_fix ();
9466
79d72f45 9467 if (add_branch_padding_frag_p (&branch, &mf_jcc))
e379e5f3
L
9468 {
9469 char *p;
9470 /* Branch can be 8 bytes. Leave some room for prefixes. */
9471 unsigned int max_branch_padding_size = 14;
9472
9473 /* Align section to boundary. */
9474 record_alignment (now_seg, align_branch_power);
9475
9476 /* Make room for padding. */
9477 frag_grow (max_branch_padding_size);
9478
9479 /* Start of the padding. */
9480 p = frag_more (0);
9481
9482 fragP = frag_now;
9483
9484 frag_var (rs_machine_dependent, max_branch_padding_size, 0,
9485 ENCODE_RELAX_STATE (BRANCH_PADDING, 0),
9486 NULL, 0, p);
9487
79d72f45 9488 fragP->tc_frag_data.mf_type = mf_jcc;
e379e5f3
L
9489 fragP->tc_frag_data.branch_type = branch;
9490 fragP->tc_frag_data.max_bytes = max_branch_padding_size;
9491 }
9492
d59a54c2
JB
9493 if (!cpu_arch_flags.bitfield.cpui386 && (flag_code != CODE_16BIT)
9494 && !pre_386_16bit_warned)
9495 {
9496 as_warn (_("use .code16 to ensure correct addressing mode"));
9497 pre_386_16bit_warned = true;
9498 }
9499
29b0f896 9500 /* Output jumps. */
0cfa3eb3 9501 if (i.tm.opcode_modifier.jump == JUMP)
29b0f896 9502 output_branch ();
0cfa3eb3
JB
9503 else if (i.tm.opcode_modifier.jump == JUMP_BYTE
9504 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896 9505 output_jump ();
0cfa3eb3 9506 else if (i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT)
29b0f896
AM
9507 output_interseg_jump ();
9508 else
9509 {
9510 /* Output normal instructions here. */
9511 char *p;
9512 unsigned char *q;
47465058 9513 unsigned int j;
79d72f45 9514 enum mf_cmp_kind mf_cmp;
4dffcebc 9515
e4e00185 9516 if (avoid_fence
389d00a5
JB
9517 && (i.tm.base_opcode == 0xaee8
9518 || i.tm.base_opcode == 0xaef0
9519 || i.tm.base_opcode == 0xaef8))
48ef937e
JB
9520 {
9521 /* Encode lfence, mfence, and sfence as
9522 f0 83 04 24 00 lock addl $0x0, (%{re}sp). */
47f4115a
JB
9523 if (flag_code == CODE_16BIT)
9524 as_bad (_("Cannot convert `%s' in 16-bit mode"), i.tm.name);
9525 else if (omit_lock_prefix)
9526 as_bad (_("Cannot convert `%s' with `-momit-lock-prefix=yes' in effect"),
9527 i.tm.name);
9528 else if (now_seg != absolute_section)
48ef937e
JB
9529 {
9530 offsetT val = 0x240483f0ULL;
9531
9532 p = frag_more (5);
9533 md_number_to_chars (p, val, 5);
9534 }
9535 else
9536 abs_section_offset += 5;
9537 return;
9538 }
e4e00185 9539
d022bddd
IT
9540 /* Some processors fail on LOCK prefix. This options makes
9541 assembler ignore LOCK prefix and serves as a workaround. */
9542 if (omit_lock_prefix)
9543 {
35648716
JB
9544 if (i.tm.base_opcode == LOCK_PREFIX_OPCODE
9545 && i.tm.opcode_modifier.isprefix)
d022bddd
IT
9546 return;
9547 i.prefix[LOCK_PREFIX] = 0;
9548 }
9549
e379e5f3
L
9550 if (branch)
9551 /* Skip if this is a branch. */
9552 ;
79d72f45 9553 else if (add_fused_jcc_padding_frag_p (&mf_cmp))
e379e5f3
L
9554 {
9555 /* Make room for padding. */
9556 frag_grow (MAX_FUSED_JCC_PADDING_SIZE);
9557 p = frag_more (0);
9558
9559 fragP = frag_now;
9560
9561 frag_var (rs_machine_dependent, MAX_FUSED_JCC_PADDING_SIZE, 0,
9562 ENCODE_RELAX_STATE (FUSED_JCC_PADDING, 0),
9563 NULL, 0, p);
9564
79d72f45 9565 fragP->tc_frag_data.mf_type = mf_cmp;
e379e5f3
L
9566 fragP->tc_frag_data.branch_type = align_branch_fused;
9567 fragP->tc_frag_data.max_bytes = MAX_FUSED_JCC_PADDING_SIZE;
9568 }
9569 else if (add_branch_prefix_frag_p ())
9570 {
9571 unsigned int max_prefix_size = align_branch_prefix_size;
9572
9573 /* Make room for padding. */
9574 frag_grow (max_prefix_size);
9575 p = frag_more (0);
9576
9577 fragP = frag_now;
9578
9579 frag_var (rs_machine_dependent, max_prefix_size, 0,
9580 ENCODE_RELAX_STATE (BRANCH_PREFIX, 0),
9581 NULL, 0, p);
9582
9583 fragP->tc_frag_data.max_bytes = max_prefix_size;
9584 }
9585
43234a1e
L
9586 /* Since the VEX/EVEX prefix contains the implicit prefix, we
9587 don't need the explicit prefix. */
cf665fee 9588 if (!is_any_vex_encoding (&i.tm))
bc4bd9ab 9589 {
7b47a312 9590 switch (i.tm.opcode_modifier.opcodeprefix)
bc4bd9ab 9591 {
7b47a312
L
9592 case PREFIX_0X66:
9593 add_prefix (0x66);
9594 break;
9595 case PREFIX_0XF2:
9596 add_prefix (0xf2);
9597 break;
9598 case PREFIX_0XF3:
8b65b895
L
9599 if (!i.tm.cpu_flags.bitfield.cpupadlock
9600 || (i.prefix[REP_PREFIX] != 0xf3))
9601 add_prefix (0xf3);
c0f3af97 9602 break;
7b47a312 9603 case PREFIX_NONE:
9a182d04 9604 switch (i.opcode_length)
c0f3af97 9605 {
7b47a312 9606 case 2:
7b47a312 9607 break;
9a182d04 9608 case 1:
7b47a312 9609 /* Check for pseudo prefixes. */
9a182d04
JB
9610 if (!i.tm.opcode_modifier.isprefix || i.tm.base_opcode)
9611 break;
7b47a312
L
9612 as_bad_where (insn_start_frag->fr_file,
9613 insn_start_frag->fr_line,
9614 _("pseudo prefix without instruction"));
9615 return;
9616 default:
9617 abort ();
4dffcebc 9618 }
c0f3af97 9619 break;
c0f3af97
L
9620 default:
9621 abort ();
bc4bd9ab 9622 }
c0f3af97 9623
6d19a37a 9624#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
cf61b747
L
9625 /* For x32, add a dummy REX_OPCODE prefix for mov/add with
9626 R_X86_64_GOTTPOFF relocation so that linker can safely
14470f07
L
9627 perform IE->LE optimization. A dummy REX_OPCODE prefix
9628 is also needed for lea with R_X86_64_GOTPC32_TLSDESC
9629 relocation for GDesc -> IE/LE optimization. */
cf61b747
L
9630 if (x86_elf_abi == X86_64_X32_ABI
9631 && i.operands == 2
14470f07
L
9632 && (i.reloc[0] == BFD_RELOC_X86_64_GOTTPOFF
9633 || i.reloc[0] == BFD_RELOC_X86_64_GOTPC32_TLSDESC)
cf61b747
L
9634 && i.prefix[REX_PREFIX] == 0)
9635 add_prefix (REX_OPCODE);
6d19a37a 9636#endif
cf61b747 9637
c0f3af97
L
9638 /* The prefix bytes. */
9639 for (j = ARRAY_SIZE (i.prefix), q = i.prefix; j > 0; j--, q++)
9640 if (*q)
48ef937e 9641 frag_opcode_byte (*q);
0f10071e 9642 }
ae5c1c7b 9643 else
c0f3af97
L
9644 {
9645 for (j = 0, q = i.prefix; j < ARRAY_SIZE (i.prefix); j++, q++)
9646 if (*q)
9647 switch (j)
9648 {
c0f3af97
L
9649 case SEG_PREFIX:
9650 case ADDR_PREFIX:
48ef937e 9651 frag_opcode_byte (*q);
c0f3af97
L
9652 break;
9653 default:
9654 /* There should be no other prefixes for instructions
9655 with VEX prefix. */
9656 abort ();
9657 }
9658
43234a1e
L
9659 /* For EVEX instructions i.vrex should become 0 after
9660 build_evex_prefix. For VEX instructions upper 16 registers
9661 aren't available, so VREX should be 0. */
9662 if (i.vrex)
9663 abort ();
c0f3af97 9664 /* Now the VEX prefix. */
48ef937e
JB
9665 if (now_seg != absolute_section)
9666 {
9667 p = frag_more (i.vex.length);
9668 for (j = 0; j < i.vex.length; j++)
9669 p[j] = i.vex.bytes[j];
9670 }
9671 else
9672 abs_section_offset += i.vex.length;
c0f3af97 9673 }
252b5132 9674
29b0f896 9675 /* Now the opcode; be careful about word order here! */
389d00a5
JB
9676 j = i.opcode_length;
9677 if (!i.vex.length)
9678 switch (i.tm.opcode_modifier.opcodespace)
9679 {
9680 case SPACE_BASE:
9681 break;
9682 case SPACE_0F:
9683 ++j;
9684 break;
9685 case SPACE_0F38:
9686 case SPACE_0F3A:
9687 j += 2;
9688 break;
9689 default:
9690 abort ();
9691 }
9692
48ef937e 9693 if (now_seg == absolute_section)
389d00a5
JB
9694 abs_section_offset += j;
9695 else if (j == 1)
29b0f896
AM
9696 {
9697 FRAG_APPEND_1_CHAR (i.tm.base_opcode);
9698 }
9699 else
9700 {
389d00a5
JB
9701 p = frag_more (j);
9702 if (!i.vex.length
9703 && i.tm.opcode_modifier.opcodespace != SPACE_BASE)
9704 {
9705 *p++ = 0x0f;
9706 if (i.tm.opcode_modifier.opcodespace != SPACE_0F)
9707 *p++ = i.tm.opcode_modifier.opcodespace == SPACE_0F38
9708 ? 0x38 : 0x3a;
9709 }
9710
9a182d04 9711 switch (i.opcode_length)
331d2d0d 9712 {
4dffcebc 9713 case 2:
389d00a5
JB
9714 /* Put out high byte first: can't use md_number_to_chars! */
9715 *p++ = (i.tm.base_opcode >> 8) & 0xff;
9716 /* Fall through. */
9717 case 1:
9718 *p = i.tm.base_opcode & 0xff;
4dffcebc
L
9719 break;
9720 default:
9721 abort ();
9722 break;
331d2d0d 9723 }
0f10071e 9724
29b0f896 9725 }
3e73aa7c 9726
29b0f896 9727 /* Now the modrm byte and sib byte (if present). */
40fb9820 9728 if (i.tm.opcode_modifier.modrm)
29b0f896 9729 {
48ef937e
JB
9730 frag_opcode_byte ((i.rm.regmem << 0)
9731 | (i.rm.reg << 3)
9732 | (i.rm.mode << 6));
29b0f896
AM
9733 /* If i.rm.regmem == ESP (4)
9734 && i.rm.mode != (Register mode)
9735 && not 16 bit
9736 ==> need second modrm byte. */
9737 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
9738 && i.rm.mode != 3
dc821c5f 9739 && !(i.base_reg && i.base_reg->reg_type.bitfield.word))
48ef937e
JB
9740 frag_opcode_byte ((i.sib.base << 0)
9741 | (i.sib.index << 3)
9742 | (i.sib.scale << 6));
29b0f896 9743 }
3e73aa7c 9744
29b0f896 9745 if (i.disp_operands)
2bbd9c25 9746 output_disp (insn_start_frag, insn_start_off);
3e73aa7c 9747
29b0f896 9748 if (i.imm_operands)
2bbd9c25 9749 output_imm (insn_start_frag, insn_start_off);
9c33702b
JB
9750
9751 /*
9752 * frag_now_fix () returning plain abs_section_offset when we're in the
9753 * absolute section, and abs_section_offset not getting updated as data
9754 * gets added to the frag breaks the logic below.
9755 */
9756 if (now_seg != absolute_section)
9757 {
9758 j = encoding_length (insn_start_frag, insn_start_off, frag_more (0));
9759 if (j > 15)
9760 as_warn (_("instruction length of %u bytes exceeds the limit of 15"),
9761 j);
e379e5f3
L
9762 else if (fragP)
9763 {
9764 /* NB: Don't add prefix with GOTPC relocation since
9765 output_disp() above depends on the fixed encoding
9766 length. Can't add prefix with TLS relocation since
9767 it breaks TLS linker optimization. */
9768 unsigned int max = i.has_gotpc_tls_reloc ? 0 : 15 - j;
9769 /* Prefix count on the current instruction. */
9770 unsigned int count = i.vex.length;
9771 unsigned int k;
9772 for (k = 0; k < ARRAY_SIZE (i.prefix); k++)
9773 /* REX byte is encoded in VEX/EVEX prefix. */
9774 if (i.prefix[k] && (k != REX_PREFIX || !i.vex.length))
9775 count++;
9776
9777 /* Count prefixes for extended opcode maps. */
9778 if (!i.vex.length)
389d00a5 9779 switch (i.tm.opcode_modifier.opcodespace)
e379e5f3 9780 {
389d00a5 9781 case SPACE_BASE:
e379e5f3 9782 break;
389d00a5
JB
9783 case SPACE_0F:
9784 count++;
e379e5f3 9785 break;
389d00a5
JB
9786 case SPACE_0F38:
9787 case SPACE_0F3A:
9788 count += 2;
e379e5f3
L
9789 break;
9790 default:
9791 abort ();
9792 }
9793
9794 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
9795 == BRANCH_PREFIX)
9796 {
9797 /* Set the maximum prefix size in BRANCH_PREFIX
9798 frag. */
9799 if (fragP->tc_frag_data.max_bytes > max)
9800 fragP->tc_frag_data.max_bytes = max;
9801 if (fragP->tc_frag_data.max_bytes > count)
9802 fragP->tc_frag_data.max_bytes -= count;
9803 else
9804 fragP->tc_frag_data.max_bytes = 0;
9805 }
9806 else
9807 {
9808 /* Remember the maximum prefix size in FUSED_JCC_PADDING
9809 frag. */
9810 unsigned int max_prefix_size;
9811 if (align_branch_prefix_size > max)
9812 max_prefix_size = max;
9813 else
9814 max_prefix_size = align_branch_prefix_size;
9815 if (max_prefix_size > count)
9816 fragP->tc_frag_data.max_prefix_length
9817 = max_prefix_size - count;
9818 }
9819
9820 /* Use existing segment prefix if possible. Use CS
9821 segment prefix in 64-bit mode. In 32-bit mode, use SS
9822 segment prefix with ESP/EBP base register and use DS
9823 segment prefix without ESP/EBP base register. */
9824 if (i.prefix[SEG_PREFIX])
9825 fragP->tc_frag_data.default_prefix = i.prefix[SEG_PREFIX];
9826 else if (flag_code == CODE_64BIT)
9827 fragP->tc_frag_data.default_prefix = CS_PREFIX_OPCODE;
9828 else if (i.base_reg
9829 && (i.base_reg->reg_num == 4
9830 || i.base_reg->reg_num == 5))
9831 fragP->tc_frag_data.default_prefix = SS_PREFIX_OPCODE;
9832 else
9833 fragP->tc_frag_data.default_prefix = DS_PREFIX_OPCODE;
9834 }
9c33702b 9835 }
29b0f896 9836 }
252b5132 9837
e379e5f3
L
9838 /* NB: Don't work with COND_JUMP86 without i386. */
9839 if (align_branch_power
9840 && now_seg != absolute_section
9841 && cpu_arch_flags.bitfield.cpui386)
9842 {
9843 /* Terminate each frag so that we can add prefix and check for
9844 fused jcc. */
9845 frag_wane (frag_now);
9846 frag_new (0);
9847 }
9848
29b0f896
AM
9849#ifdef DEBUG386
9850 if (flag_debug)
9851 {
7b81dfbb 9852 pi ("" /*line*/, &i);
29b0f896
AM
9853 }
9854#endif /* DEBUG386 */
9855}
252b5132 9856
e205caa7
L
9857/* Return the size of the displacement operand N. */
9858
9859static int
9860disp_size (unsigned int n)
9861{
9862 int size = 4;
43234a1e 9863
b5014f7a 9864 if (i.types[n].bitfield.disp64)
40fb9820
L
9865 size = 8;
9866 else if (i.types[n].bitfield.disp8)
9867 size = 1;
9868 else if (i.types[n].bitfield.disp16)
9869 size = 2;
e205caa7
L
9870 return size;
9871}
9872
9873/* Return the size of the immediate operand N. */
9874
9875static int
9876imm_size (unsigned int n)
9877{
9878 int size = 4;
40fb9820
L
9879 if (i.types[n].bitfield.imm64)
9880 size = 8;
9881 else if (i.types[n].bitfield.imm8 || i.types[n].bitfield.imm8s)
9882 size = 1;
9883 else if (i.types[n].bitfield.imm16)
9884 size = 2;
e205caa7
L
9885 return size;
9886}
9887
29b0f896 9888static void
64e74474 9889output_disp (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
9890{
9891 char *p;
9892 unsigned int n;
252b5132 9893
29b0f896
AM
9894 for (n = 0; n < i.operands; n++)
9895 {
b5014f7a 9896 if (operand_type_check (i.types[n], disp))
29b0f896 9897 {
48ef937e
JB
9898 int size = disp_size (n);
9899
9900 if (now_seg == absolute_section)
9901 abs_section_offset += size;
9902 else if (i.op[n].disps->X_op == O_constant)
29b0f896 9903 {
43234a1e 9904 offsetT val = i.op[n].disps->X_add_number;
252b5132 9905
629cfaf1
JB
9906 val = offset_in_range (val >> (size == 1 ? i.memshift : 0),
9907 size);
29b0f896
AM
9908 p = frag_more (size);
9909 md_number_to_chars (p, val, size);
9910 }
9911 else
9912 {
f86103b7 9913 enum bfd_reloc_code_real reloc_type;
a775efc8
JB
9914 bool pcrel = (i.flags[n] & Operand_PCrel) != 0;
9915 bool sign = (flag_code == CODE_64BIT && size == 4
9916 && (!want_disp32 (&i.tm)
9917 || (i.tm.opcode_modifier.jump && !i.jumpabsolute
9918 && !i.types[n].bitfield.baseindex)))
9919 || pcrel;
02a86693 9920 fixS *fixP;
29b0f896 9921
e205caa7 9922 /* We can't have 8 bit displacement here. */
9c2799c2 9923 gas_assert (!i.types[n].bitfield.disp8);
e205caa7 9924
29b0f896
AM
9925 /* The PC relative address is computed relative
9926 to the instruction boundary, so in case immediate
9927 fields follows, we need to adjust the value. */
9928 if (pcrel && i.imm_operands)
9929 {
29b0f896 9930 unsigned int n1;
e205caa7 9931 int sz = 0;
252b5132 9932
29b0f896 9933 for (n1 = 0; n1 < i.operands; n1++)
40fb9820 9934 if (operand_type_check (i.types[n1], imm))
252b5132 9935 {
e205caa7
L
9936 /* Only one immediate is allowed for PC
9937 relative address. */
9c2799c2 9938 gas_assert (sz == 0);
e205caa7
L
9939 sz = imm_size (n1);
9940 i.op[n].disps->X_add_number -= sz;
252b5132 9941 }
29b0f896 9942 /* We should find the immediate. */
9c2799c2 9943 gas_assert (sz != 0);
29b0f896 9944 }
520dc8e8 9945
29b0f896 9946 p = frag_more (size);
d258b828 9947 reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
d6ab8113 9948 if (GOT_symbol
2bbd9c25 9949 && GOT_symbol == i.op[n].disps->X_add_symbol
d6ab8113 9950 && (((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
9951 || reloc_type == BFD_RELOC_X86_64_32S
9952 || (reloc_type == BFD_RELOC_64
9953 && object_64bit))
d6ab8113
JB
9954 && (i.op[n].disps->X_op == O_symbol
9955 || (i.op[n].disps->X_op == O_add
9956 && ((symbol_get_value_expression
9957 (i.op[n].disps->X_op_symbol)->X_op)
9958 == O_subtract))))
9959 || reloc_type == BFD_RELOC_32_PCREL))
2bbd9c25 9960 {
4fa24527 9961 if (!object_64bit)
7b81dfbb
AJ
9962 {
9963 reloc_type = BFD_RELOC_386_GOTPC;
5b7c81bd 9964 i.has_gotpc_tls_reloc = true;
98da05bf 9965 i.op[n].disps->X_add_number +=
d583596c 9966 encoding_length (insn_start_frag, insn_start_off, p);
7b81dfbb
AJ
9967 }
9968 else if (reloc_type == BFD_RELOC_64)
9969 reloc_type = BFD_RELOC_X86_64_GOTPC64;
d6ab8113 9970 else
7b81dfbb
AJ
9971 /* Don't do the adjustment for x86-64, as there
9972 the pcrel addressing is relative to the _next_
9973 insn, and that is taken care of in other code. */
d6ab8113 9974 reloc_type = BFD_RELOC_X86_64_GOTPC32;
2bbd9c25 9975 }
e379e5f3
L
9976 else if (align_branch_power)
9977 {
9978 switch (reloc_type)
9979 {
9980 case BFD_RELOC_386_TLS_GD:
9981 case BFD_RELOC_386_TLS_LDM:
9982 case BFD_RELOC_386_TLS_IE:
9983 case BFD_RELOC_386_TLS_IE_32:
9984 case BFD_RELOC_386_TLS_GOTIE:
9985 case BFD_RELOC_386_TLS_GOTDESC:
9986 case BFD_RELOC_386_TLS_DESC_CALL:
9987 case BFD_RELOC_X86_64_TLSGD:
9988 case BFD_RELOC_X86_64_TLSLD:
9989 case BFD_RELOC_X86_64_GOTTPOFF:
9990 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
9991 case BFD_RELOC_X86_64_TLSDESC_CALL:
5b7c81bd 9992 i.has_gotpc_tls_reloc = true;
e379e5f3
L
9993 default:
9994 break;
9995 }
9996 }
02a86693
L
9997 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal,
9998 size, i.op[n].disps, pcrel,
9999 reloc_type);
eb19308f
JB
10000
10001 if (flag_code == CODE_64BIT && size == 4 && pcrel
10002 && !i.prefix[ADDR_PREFIX])
10003 fixP->fx_signed = 1;
10004
02a86693
L
10005 /* Check for "call/jmp *mem", "mov mem, %reg",
10006 "test %reg, mem" and "binop mem, %reg" where binop
10007 is one of adc, add, and, cmp, or, sbb, sub, xor
e60f4d3b
L
10008 instructions without data prefix. Always generate
10009 R_386_GOT32X for "sym*GOT" operand in 32-bit mode. */
10010 if (i.prefix[DATA_PREFIX] == 0
10011 && (generate_relax_relocations
10012 || (!object_64bit
10013 && i.rm.mode == 0
10014 && i.rm.regmem == 5))
0cb4071e
L
10015 && (i.rm.mode == 2
10016 || (i.rm.mode == 0 && i.rm.regmem == 5))
389d00a5 10017 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
02a86693
L
10018 && ((i.operands == 1
10019 && i.tm.base_opcode == 0xff
10020 && (i.rm.reg == 2 || i.rm.reg == 4))
10021 || (i.operands == 2
10022 && (i.tm.base_opcode == 0x8b
10023 || i.tm.base_opcode == 0x85
2ae4c703 10024 || (i.tm.base_opcode & ~0x38) == 0x03))))
02a86693
L
10025 {
10026 if (object_64bit)
10027 {
10028 fixP->fx_tcbit = i.rex != 0;
10029 if (i.base_reg
e968fc9b 10030 && (i.base_reg->reg_num == RegIP))
02a86693
L
10031 fixP->fx_tcbit2 = 1;
10032 }
10033 else
10034 fixP->fx_tcbit2 = 1;
10035 }
29b0f896
AM
10036 }
10037 }
10038 }
10039}
252b5132 10040
29b0f896 10041static void
64e74474 10042output_imm (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
10043{
10044 char *p;
10045 unsigned int n;
252b5132 10046
29b0f896
AM
10047 for (n = 0; n < i.operands; n++)
10048 {
40fb9820 10049 if (operand_type_check (i.types[n], imm))
29b0f896 10050 {
48ef937e
JB
10051 int size = imm_size (n);
10052
10053 if (now_seg == absolute_section)
10054 abs_section_offset += size;
10055 else if (i.op[n].imms->X_op == O_constant)
29b0f896 10056 {
29b0f896 10057 offsetT val;
b4cac588 10058
29b0f896
AM
10059 val = offset_in_range (i.op[n].imms->X_add_number,
10060 size);
10061 p = frag_more (size);
10062 md_number_to_chars (p, val, size);
10063 }
10064 else
10065 {
10066 /* Not absolute_section.
10067 Need a 32-bit fixup (don't support 8bit
10068 non-absolute imms). Try to support other
10069 sizes ... */
f86103b7 10070 enum bfd_reloc_code_real reloc_type;
e205caa7 10071 int sign;
29b0f896 10072
40fb9820 10073 if (i.types[n].bitfield.imm32s
a7d61044 10074 && (i.suffix == QWORD_MNEM_SUFFIX
40fb9820 10075 || (!i.suffix && i.tm.opcode_modifier.no_lsuf)))
29b0f896 10076 sign = 1;
e205caa7
L
10077 else
10078 sign = 0;
520dc8e8 10079
29b0f896 10080 p = frag_more (size);
d258b828 10081 reloc_type = reloc (size, 0, sign, i.reloc[n]);
f86103b7 10082
2bbd9c25
JJ
10083 /* This is tough to explain. We end up with this one if we
10084 * have operands that look like
10085 * "_GLOBAL_OFFSET_TABLE_+[.-.L284]". The goal here is to
10086 * obtain the absolute address of the GOT, and it is strongly
10087 * preferable from a performance point of view to avoid using
10088 * a runtime relocation for this. The actual sequence of
10089 * instructions often look something like:
10090 *
10091 * call .L66
10092 * .L66:
10093 * popl %ebx
10094 * addl $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
10095 *
10096 * The call and pop essentially return the absolute address
10097 * of the label .L66 and store it in %ebx. The linker itself
10098 * will ultimately change the first operand of the addl so
10099 * that %ebx points to the GOT, but to keep things simple, the
10100 * .o file must have this operand set so that it generates not
10101 * the absolute address of .L66, but the absolute address of
10102 * itself. This allows the linker itself simply treat a GOTPC
10103 * relocation as asking for a pcrel offset to the GOT to be
10104 * added in, and the addend of the relocation is stored in the
10105 * operand field for the instruction itself.
10106 *
10107 * Our job here is to fix the operand so that it would add
10108 * the correct offset so that %ebx would point to itself. The
10109 * thing that is tricky is that .-.L66 will point to the
10110 * beginning of the instruction, so we need to further modify
10111 * the operand so that it will point to itself. There are
10112 * other cases where you have something like:
10113 *
10114 * .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
10115 *
10116 * and here no correction would be required. Internally in
10117 * the assembler we treat operands of this form as not being
10118 * pcrel since the '.' is explicitly mentioned, and I wonder
10119 * whether it would simplify matters to do it this way. Who
10120 * knows. In earlier versions of the PIC patches, the
10121 * pcrel_adjust field was used to store the correction, but
10122 * since the expression is not pcrel, I felt it would be
10123 * confusing to do it this way. */
10124
d6ab8113 10125 if ((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
10126 || reloc_type == BFD_RELOC_X86_64_32S
10127 || reloc_type == BFD_RELOC_64)
29b0f896
AM
10128 && GOT_symbol
10129 && GOT_symbol == i.op[n].imms->X_add_symbol
10130 && (i.op[n].imms->X_op == O_symbol
10131 || (i.op[n].imms->X_op == O_add
10132 && ((symbol_get_value_expression
10133 (i.op[n].imms->X_op_symbol)->X_op)
10134 == O_subtract))))
10135 {
4fa24527 10136 if (!object_64bit)
d6ab8113 10137 reloc_type = BFD_RELOC_386_GOTPC;
7b81dfbb 10138 else if (size == 4)
d6ab8113 10139 reloc_type = BFD_RELOC_X86_64_GOTPC32;
7b81dfbb
AJ
10140 else if (size == 8)
10141 reloc_type = BFD_RELOC_X86_64_GOTPC64;
5b7c81bd 10142 i.has_gotpc_tls_reloc = true;
d583596c
JB
10143 i.op[n].imms->X_add_number +=
10144 encoding_length (insn_start_frag, insn_start_off, p);
29b0f896 10145 }
29b0f896
AM
10146 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
10147 i.op[n].imms, 0, reloc_type);
10148 }
10149 }
10150 }
252b5132
RH
10151}
10152\f
d182319b
JB
10153/* x86_cons_fix_new is called via the expression parsing code when a
10154 reloc is needed. We use this hook to get the correct .got reloc. */
d182319b
JB
10155static int cons_sign = -1;
10156
10157void
e3bb37b5 10158x86_cons_fix_new (fragS *frag, unsigned int off, unsigned int len,
62ebcb5c 10159 expressionS *exp, bfd_reloc_code_real_type r)
d182319b 10160{
d258b828 10161 r = reloc (len, 0, cons_sign, r);
d182319b
JB
10162
10163#ifdef TE_PE
10164 if (exp->X_op == O_secrel)
10165 {
10166 exp->X_op = O_symbol;
10167 r = BFD_RELOC_32_SECREL;
10168 }
145667f8
MH
10169 else if (exp->X_op == O_secidx)
10170 r = BFD_RELOC_16_SECIDX;
d182319b
JB
10171#endif
10172
10173 fix_new_exp (frag, off, len, exp, 0, r);
10174}
10175
357d1bd8
L
10176/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
10177 purpose of the `.dc.a' internal pseudo-op. */
10178
10179int
10180x86_address_bytes (void)
10181{
10182 if ((stdoutput->arch_info->mach & bfd_mach_x64_32))
10183 return 4;
10184 return stdoutput->arch_info->bits_per_address / 8;
10185}
10186
deea4973
JB
10187#if (!(defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined (OBJ_MACH_O)) \
10188 || defined (LEX_AT)) && !defined (TE_PE)
d258b828 10189# define lex_got(reloc, adjust, types) NULL
718ddfc0 10190#else
f3c180ae
AM
10191/* Parse operands of the form
10192 <symbol>@GOTOFF+<nnn>
10193 and similar .plt or .got references.
10194
10195 If we find one, set up the correct relocation in RELOC and copy the
10196 input string, minus the `@GOTOFF' into a malloc'd buffer for
10197 parsing by the calling routine. Return this buffer, and if ADJUST
10198 is non-null set it to the length of the string we removed from the
10199 input line. Otherwise return NULL. */
10200static char *
91d6fa6a 10201lex_got (enum bfd_reloc_code_real *rel,
64e74474 10202 int *adjust,
d258b828 10203 i386_operand_type *types)
f3c180ae 10204{
7b81dfbb
AJ
10205 /* Some of the relocations depend on the size of what field is to
10206 be relocated. But in our callers i386_immediate and i386_displacement
10207 we don't yet know the operand size (this will be set by insn
10208 matching). Hence we record the word32 relocation here,
10209 and adjust the reloc according to the real size in reloc(). */
145667f8
MH
10210 static const struct
10211 {
f3c180ae 10212 const char *str;
cff8d58a 10213 int len;
4fa24527 10214 const enum bfd_reloc_code_real rel[2];
40fb9820 10215 const i386_operand_type types64;
5b7c81bd 10216 bool need_GOT_symbol;
145667f8
MH
10217 }
10218 gotrel[] =
10219 {
deea4973 10220#ifndef TE_PE
8ce3d284 10221#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
10222 { STRING_COMMA_LEN ("SIZE"), { BFD_RELOC_SIZE32,
10223 BFD_RELOC_SIZE32 },
5b7c81bd 10224 OPERAND_TYPE_IMM32_64, false },
8ce3d284 10225#endif
cff8d58a
L
10226 { STRING_COMMA_LEN ("PLTOFF"), { _dummy_first_bfd_reloc_code_real,
10227 BFD_RELOC_X86_64_PLTOFF64 },
5b7c81bd 10228 OPERAND_TYPE_IMM64, true },
cff8d58a
L
10229 { STRING_COMMA_LEN ("PLT"), { BFD_RELOC_386_PLT32,
10230 BFD_RELOC_X86_64_PLT32 },
a775efc8 10231 OPERAND_TYPE_IMM32_32S_DISP32, false },
cff8d58a
L
10232 { STRING_COMMA_LEN ("GOTPLT"), { _dummy_first_bfd_reloc_code_real,
10233 BFD_RELOC_X86_64_GOTPLT64 },
5b7c81bd 10234 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
10235 { STRING_COMMA_LEN ("GOTOFF"), { BFD_RELOC_386_GOTOFF,
10236 BFD_RELOC_X86_64_GOTOFF64 },
5b7c81bd 10237 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
10238 { STRING_COMMA_LEN ("GOTPCREL"), { _dummy_first_bfd_reloc_code_real,
10239 BFD_RELOC_X86_64_GOTPCREL },
a775efc8 10240 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10241 { STRING_COMMA_LEN ("TLSGD"), { BFD_RELOC_386_TLS_GD,
10242 BFD_RELOC_X86_64_TLSGD },
a775efc8 10243 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10244 { STRING_COMMA_LEN ("TLSLDM"), { BFD_RELOC_386_TLS_LDM,
10245 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10246 OPERAND_TYPE_NONE, true },
cff8d58a
L
10247 { STRING_COMMA_LEN ("TLSLD"), { _dummy_first_bfd_reloc_code_real,
10248 BFD_RELOC_X86_64_TLSLD },
a775efc8 10249 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10250 { STRING_COMMA_LEN ("GOTTPOFF"), { BFD_RELOC_386_TLS_IE_32,
10251 BFD_RELOC_X86_64_GOTTPOFF },
a775efc8 10252 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10253 { STRING_COMMA_LEN ("TPOFF"), { BFD_RELOC_386_TLS_LE_32,
10254 BFD_RELOC_X86_64_TPOFF32 },
a775efc8 10255 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
10256 { STRING_COMMA_LEN ("NTPOFF"), { BFD_RELOC_386_TLS_LE,
10257 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10258 OPERAND_TYPE_NONE, true },
cff8d58a
L
10259 { STRING_COMMA_LEN ("DTPOFF"), { BFD_RELOC_386_TLS_LDO_32,
10260 BFD_RELOC_X86_64_DTPOFF32 },
a775efc8 10261 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
10262 { STRING_COMMA_LEN ("GOTNTPOFF"),{ BFD_RELOC_386_TLS_GOTIE,
10263 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10264 OPERAND_TYPE_NONE, true },
cff8d58a
L
10265 { STRING_COMMA_LEN ("INDNTPOFF"),{ BFD_RELOC_386_TLS_IE,
10266 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10267 OPERAND_TYPE_NONE, true },
cff8d58a
L
10268 { STRING_COMMA_LEN ("GOT"), { BFD_RELOC_386_GOT32,
10269 BFD_RELOC_X86_64_GOT32 },
a775efc8 10270 OPERAND_TYPE_IMM32_32S_64_DISP32, true },
cff8d58a
L
10271 { STRING_COMMA_LEN ("TLSDESC"), { BFD_RELOC_386_TLS_GOTDESC,
10272 BFD_RELOC_X86_64_GOTPC32_TLSDESC },
a775efc8 10273 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10274 { STRING_COMMA_LEN ("TLSCALL"), { BFD_RELOC_386_TLS_DESC_CALL,
10275 BFD_RELOC_X86_64_TLSDESC_CALL },
a775efc8 10276 OPERAND_TYPE_IMM32_32S_DISP32, true },
deea4973
JB
10277#else /* TE_PE */
10278 { STRING_COMMA_LEN ("SECREL32"), { BFD_RELOC_32_SECREL,
10279 BFD_RELOC_32_SECREL },
a775efc8 10280 OPERAND_TYPE_IMM32_32S_64_DISP32_64, false },
deea4973 10281#endif
f3c180ae
AM
10282 };
10283 char *cp;
10284 unsigned int j;
10285
deea4973 10286#if defined (OBJ_MAYBE_ELF) && !defined (TE_PE)
718ddfc0
JB
10287 if (!IS_ELF)
10288 return NULL;
d382c579 10289#endif
718ddfc0 10290
f3c180ae 10291 for (cp = input_line_pointer; *cp != '@'; cp++)
67c11a9b 10292 if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
f3c180ae
AM
10293 return NULL;
10294
47465058 10295 for (j = 0; j < ARRAY_SIZE (gotrel); j++)
f3c180ae 10296 {
cff8d58a 10297 int len = gotrel[j].len;
28f81592 10298 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
f3c180ae 10299 {
4fa24527 10300 if (gotrel[j].rel[object_64bit] != 0)
f3c180ae 10301 {
28f81592
AM
10302 int first, second;
10303 char *tmpbuf, *past_reloc;
f3c180ae 10304
91d6fa6a 10305 *rel = gotrel[j].rel[object_64bit];
f3c180ae 10306
3956db08
JB
10307 if (types)
10308 {
10309 if (flag_code != CODE_64BIT)
40fb9820
L
10310 {
10311 types->bitfield.imm32 = 1;
10312 types->bitfield.disp32 = 1;
10313 }
3956db08
JB
10314 else
10315 *types = gotrel[j].types64;
10316 }
10317
844bf810 10318 if (gotrel[j].need_GOT_symbol && GOT_symbol == NULL)
f3c180ae
AM
10319 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
10320
28f81592 10321 /* The length of the first part of our input line. */
f3c180ae 10322 first = cp - input_line_pointer;
28f81592
AM
10323
10324 /* The second part goes from after the reloc token until
67c11a9b 10325 (and including) an end_of_line char or comma. */
28f81592 10326 past_reloc = cp + 1 + len;
67c11a9b
AM
10327 cp = past_reloc;
10328 while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
10329 ++cp;
10330 second = cp + 1 - past_reloc;
28f81592
AM
10331
10332 /* Allocate and copy string. The trailing NUL shouldn't
10333 be necessary, but be safe. */
add39d23 10334 tmpbuf = XNEWVEC (char, first + second + 2);
f3c180ae 10335 memcpy (tmpbuf, input_line_pointer, first);
0787a12d
AM
10336 if (second != 0 && *past_reloc != ' ')
10337 /* Replace the relocation token with ' ', so that
10338 errors like foo@GOTOFF1 will be detected. */
10339 tmpbuf[first++] = ' ';
af89796a
L
10340 else
10341 /* Increment length by 1 if the relocation token is
10342 removed. */
10343 len++;
10344 if (adjust)
10345 *adjust = len;
0787a12d
AM
10346 memcpy (tmpbuf + first, past_reloc, second);
10347 tmpbuf[first + second] = '\0';
f3c180ae
AM
10348 return tmpbuf;
10349 }
10350
4fa24527
JB
10351 as_bad (_("@%s reloc is not supported with %d-bit output format"),
10352 gotrel[j].str, 1 << (5 + object_64bit));
f3c180ae
AM
10353 return NULL;
10354 }
10355 }
10356
10357 /* Might be a symbol version string. Don't as_bad here. */
10358 return NULL;
10359}
4e4f7c87 10360#endif
f3c180ae 10361
62ebcb5c 10362bfd_reloc_code_real_type
e3bb37b5 10363x86_cons (expressionS *exp, int size)
f3c180ae 10364{
62ebcb5c
AM
10365 bfd_reloc_code_real_type got_reloc = NO_RELOC;
10366
2748c1b1
L
10367#if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
10368 && !defined (LEX_AT)) \
10369 || defined (TE_PE)
ee86248c
JB
10370 intel_syntax = -intel_syntax;
10371
3c7b9c2c 10372 exp->X_md = 0;
4fa24527 10373 if (size == 4 || (object_64bit && size == 8))
f3c180ae
AM
10374 {
10375 /* Handle @GOTOFF and the like in an expression. */
10376 char *save;
10377 char *gotfree_input_line;
4a57f2cf 10378 int adjust = 0;
f3c180ae
AM
10379
10380 save = input_line_pointer;
d258b828 10381 gotfree_input_line = lex_got (&got_reloc, &adjust, NULL);
f3c180ae
AM
10382 if (gotfree_input_line)
10383 input_line_pointer = gotfree_input_line;
10384
10385 expression (exp);
10386
10387 if (gotfree_input_line)
10388 {
10389 /* expression () has merrily parsed up to the end of line,
10390 or a comma - in the wrong buffer. Transfer how far
10391 input_line_pointer has moved to the right buffer. */
10392 input_line_pointer = (save
10393 + (input_line_pointer - gotfree_input_line)
10394 + adjust);
10395 free (gotfree_input_line);
3992d3b7
AM
10396 if (exp->X_op == O_constant
10397 || exp->X_op == O_absent
10398 || exp->X_op == O_illegal
0398aac5 10399 || exp->X_op == O_register
3992d3b7
AM
10400 || exp->X_op == O_big)
10401 {
10402 char c = *input_line_pointer;
10403 *input_line_pointer = 0;
10404 as_bad (_("missing or invalid expression `%s'"), save);
10405 *input_line_pointer = c;
10406 }
b9519cfe
L
10407 else if ((got_reloc == BFD_RELOC_386_PLT32
10408 || got_reloc == BFD_RELOC_X86_64_PLT32)
10409 && exp->X_op != O_symbol)
10410 {
10411 char c = *input_line_pointer;
10412 *input_line_pointer = 0;
10413 as_bad (_("invalid PLT expression `%s'"), save);
10414 *input_line_pointer = c;
10415 }
f3c180ae
AM
10416 }
10417 }
10418 else
10419 expression (exp);
ee86248c
JB
10420
10421 intel_syntax = -intel_syntax;
10422
10423 if (intel_syntax)
10424 i386_intel_simplify (exp);
2748c1b1
L
10425#else
10426 expression (exp);
10427#endif
62ebcb5c 10428
a442cac5
JB
10429 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
10430 if (size == 4 && exp->X_op == O_constant && !object_64bit)
10431 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
10432
62ebcb5c 10433 return got_reloc;
f3c180ae 10434}
f3c180ae 10435
9f32dd5b
L
10436static void
10437signed_cons (int size)
6482c264 10438{
a442cac5 10439 if (object_64bit)
d182319b
JB
10440 cons_sign = 1;
10441 cons (size);
10442 cons_sign = -1;
6482c264
NC
10443}
10444
d182319b 10445#ifdef TE_PE
6482c264 10446static void
7016a5d5 10447pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
6482c264
NC
10448{
10449 expressionS exp;
10450
10451 do
10452 {
10453 expression (&exp);
10454 if (exp.X_op == O_symbol)
10455 exp.X_op = O_secrel;
10456
10457 emit_expr (&exp, 4);
10458 }
10459 while (*input_line_pointer++ == ',');
10460
10461 input_line_pointer--;
10462 demand_empty_rest_of_line ();
10463}
145667f8
MH
10464
10465static void
10466pe_directive_secidx (int dummy ATTRIBUTE_UNUSED)
10467{
10468 expressionS exp;
10469
10470 do
10471 {
10472 expression (&exp);
10473 if (exp.X_op == O_symbol)
10474 exp.X_op = O_secidx;
10475
10476 emit_expr (&exp, 2);
10477 }
10478 while (*input_line_pointer++ == ',');
10479
10480 input_line_pointer--;
10481 demand_empty_rest_of_line ();
10482}
6482c264
NC
10483#endif
10484
7063667e
JB
10485/* Handle Rounding Control / SAE specifiers. */
10486
10487static char *
10488RC_SAE_specifier (const char *pstr)
10489{
10490 unsigned int j;
10491
10492 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); j++)
10493 {
10494 if (!strncmp (pstr, RC_NamesTable[j].name, RC_NamesTable[j].len))
10495 {
10496 if (i.rounding.type != rc_none)
10497 {
10498 as_bad (_("duplicated `{%s}'"), RC_NamesTable[j].name);
10499 return NULL;
10500 }
10501
10502 i.rounding.type = RC_NamesTable[j].type;
10503
10504 return (char *)(pstr + RC_NamesTable[j].len);
10505 }
10506 }
10507
10508 return NULL;
10509}
10510
43234a1e
L
10511/* Handle Vector operations. */
10512
10513static char *
f70c6814 10514check_VecOperations (char *op_string)
43234a1e
L
10515{
10516 const reg_entry *mask;
10517 const char *saved;
10518 char *end_op;
10519
f70c6814 10520 while (*op_string)
43234a1e
L
10521 {
10522 saved = op_string;
10523 if (*op_string == '{')
10524 {
10525 op_string++;
10526
10527 /* Check broadcasts. */
d34049e8 10528 if (startswith (op_string, "1to"))
43234a1e 10529 {
5273a3cd 10530 unsigned int bcst_type;
43234a1e 10531
5273a3cd 10532 if (i.broadcast.type)
43234a1e
L
10533 goto duplicated_vec_op;
10534
10535 op_string += 3;
10536 if (*op_string == '8')
8e6e0792 10537 bcst_type = 8;
b28d1bda 10538 else if (*op_string == '4')
8e6e0792 10539 bcst_type = 4;
b28d1bda 10540 else if (*op_string == '2')
8e6e0792 10541 bcst_type = 2;
43234a1e
L
10542 else if (*op_string == '1'
10543 && *(op_string+1) == '6')
10544 {
8e6e0792 10545 bcst_type = 16;
43234a1e
L
10546 op_string++;
10547 }
0cc78721
CL
10548 else if (*op_string == '3'
10549 && *(op_string+1) == '2')
10550 {
10551 bcst_type = 32;
10552 op_string++;
10553 }
43234a1e
L
10554 else
10555 {
10556 as_bad (_("Unsupported broadcast: `%s'"), saved);
10557 return NULL;
10558 }
10559 op_string++;
10560
5273a3cd
JB
10561 i.broadcast.type = bcst_type;
10562 i.broadcast.operand = this_operand;
43234a1e
L
10563 }
10564 /* Check masking operation. */
10565 else if ((mask = parse_register (op_string, &end_op)) != NULL)
10566 {
8a6fb3f9
JB
10567 if (mask == &bad_reg)
10568 return NULL;
10569
43234a1e 10570 /* k0 can't be used for write mask. */
f74a6307 10571 if (mask->reg_type.bitfield.class != RegMask || !mask->reg_num)
43234a1e 10572 {
6d2cd6b2
JB
10573 as_bad (_("`%s%s' can't be used for write mask"),
10574 register_prefix, mask->reg_name);
43234a1e
L
10575 return NULL;
10576 }
10577
6225c532 10578 if (!i.mask.reg)
43234a1e 10579 {
6225c532
JB
10580 i.mask.reg = mask;
10581 i.mask.operand = this_operand;
43234a1e 10582 }
6225c532
JB
10583 else if (i.mask.reg->reg_num)
10584 goto duplicated_vec_op;
43234a1e
L
10585 else
10586 {
6225c532 10587 i.mask.reg = mask;
43234a1e
L
10588
10589 /* Only "{z}" is allowed here. No need to check
10590 zeroing mask explicitly. */
6225c532 10591 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
10592 {
10593 as_bad (_("invalid write mask `%s'"), saved);
10594 return NULL;
10595 }
10596 }
10597
10598 op_string = end_op;
10599 }
10600 /* Check zeroing-flag for masking operation. */
10601 else if (*op_string == 'z')
10602 {
6225c532 10603 if (!i.mask.reg)
43234a1e 10604 {
6225c532
JB
10605 i.mask.reg = reg_k0;
10606 i.mask.zeroing = 1;
10607 i.mask.operand = this_operand;
43234a1e
L
10608 }
10609 else
10610 {
6225c532 10611 if (i.mask.zeroing)
43234a1e
L
10612 {
10613 duplicated_vec_op:
10614 as_bad (_("duplicated `%s'"), saved);
10615 return NULL;
10616 }
10617
6225c532 10618 i.mask.zeroing = 1;
43234a1e
L
10619
10620 /* Only "{%k}" is allowed here. No need to check mask
10621 register explicitly. */
6225c532 10622 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
10623 {
10624 as_bad (_("invalid zeroing-masking `%s'"),
10625 saved);
10626 return NULL;
10627 }
10628 }
10629
10630 op_string++;
10631 }
7063667e
JB
10632 else if (intel_syntax
10633 && (op_string = RC_SAE_specifier (op_string)) != NULL)
10634 i.rounding.modifier = true;
43234a1e
L
10635 else
10636 goto unknown_vec_op;
10637
10638 if (*op_string != '}')
10639 {
10640 as_bad (_("missing `}' in `%s'"), saved);
10641 return NULL;
10642 }
10643 op_string++;
0ba3a731
L
10644
10645 /* Strip whitespace since the addition of pseudo prefixes
10646 changed how the scrubber treats '{'. */
10647 if (is_space_char (*op_string))
10648 ++op_string;
10649
43234a1e
L
10650 continue;
10651 }
10652 unknown_vec_op:
10653 /* We don't know this one. */
10654 as_bad (_("unknown vector operation: `%s'"), saved);
10655 return NULL;
10656 }
10657
6225c532 10658 if (i.mask.reg && i.mask.zeroing && !i.mask.reg->reg_num)
6d2cd6b2
JB
10659 {
10660 as_bad (_("zeroing-masking only allowed with write mask"));
10661 return NULL;
10662 }
10663
43234a1e
L
10664 return op_string;
10665}
10666
252b5132 10667static int
70e41ade 10668i386_immediate (char *imm_start)
252b5132
RH
10669{
10670 char *save_input_line_pointer;
f3c180ae 10671 char *gotfree_input_line;
252b5132 10672 segT exp_seg = 0;
47926f60 10673 expressionS *exp;
40fb9820
L
10674 i386_operand_type types;
10675
0dfbf9d7 10676 operand_type_set (&types, ~0);
252b5132
RH
10677
10678 if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
10679 {
31b2323c
L
10680 as_bad (_("at most %d immediate operands are allowed"),
10681 MAX_IMMEDIATE_OPERANDS);
252b5132
RH
10682 return 0;
10683 }
10684
10685 exp = &im_expressions[i.imm_operands++];
520dc8e8 10686 i.op[this_operand].imms = exp;
252b5132
RH
10687
10688 if (is_space_char (*imm_start))
10689 ++imm_start;
10690
10691 save_input_line_pointer = input_line_pointer;
10692 input_line_pointer = imm_start;
10693
d258b828 10694 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
10695 if (gotfree_input_line)
10696 input_line_pointer = gotfree_input_line;
252b5132
RH
10697
10698 exp_seg = expression (exp);
10699
83183c0c 10700 SKIP_WHITESPACE ();
252b5132 10701 if (*input_line_pointer)
f3c180ae 10702 as_bad (_("junk `%s' after expression"), input_line_pointer);
252b5132
RH
10703
10704 input_line_pointer = save_input_line_pointer;
f3c180ae 10705 if (gotfree_input_line)
ee86248c
JB
10706 {
10707 free (gotfree_input_line);
10708
9aac24b1 10709 if (exp->X_op == O_constant)
ee86248c
JB
10710 exp->X_op = O_illegal;
10711 }
10712
9aac24b1
JB
10713 if (exp_seg == reg_section)
10714 {
10715 as_bad (_("illegal immediate register operand %s"), imm_start);
10716 return 0;
10717 }
10718
ee86248c
JB
10719 return i386_finalize_immediate (exp_seg, exp, types, imm_start);
10720}
252b5132 10721
ee86248c
JB
10722static int
10723i386_finalize_immediate (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
10724 i386_operand_type types, const char *imm_start)
10725{
10726 if (exp->X_op == O_absent || exp->X_op == O_illegal || exp->X_op == O_big)
252b5132 10727 {
313c53d1
L
10728 if (imm_start)
10729 as_bad (_("missing or invalid immediate expression `%s'"),
10730 imm_start);
3992d3b7 10731 return 0;
252b5132 10732 }
3e73aa7c 10733 else if (exp->X_op == O_constant)
252b5132 10734 {
47926f60 10735 /* Size it properly later. */
40fb9820 10736 i.types[this_operand].bitfield.imm64 = 1;
a442cac5
JB
10737
10738 /* If not 64bit, sign/zero extend val, to account for wraparound
10739 when !BFD64. */
10740 if (flag_code != CODE_64BIT)
10741 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
252b5132 10742 }
4c63da97 10743#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
f86103b7 10744 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
31312f95 10745 && exp_seg != absolute_section
47926f60 10746 && exp_seg != text_section
24eab124
AM
10747 && exp_seg != data_section
10748 && exp_seg != bss_section
10749 && exp_seg != undefined_section
f86103b7 10750 && !bfd_is_com_section (exp_seg))
252b5132 10751 {
d0b47220 10752 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
252b5132
RH
10753 return 0;
10754 }
10755#endif
10756 else
10757 {
10758 /* This is an address. The size of the address will be
24eab124 10759 determined later, depending on destination register,
3e73aa7c 10760 suffix, or the default for the section. */
40fb9820
L
10761 i.types[this_operand].bitfield.imm8 = 1;
10762 i.types[this_operand].bitfield.imm16 = 1;
10763 i.types[this_operand].bitfield.imm32 = 1;
10764 i.types[this_operand].bitfield.imm32s = 1;
10765 i.types[this_operand].bitfield.imm64 = 1;
c6fb90c8
L
10766 i.types[this_operand] = operand_type_and (i.types[this_operand],
10767 types);
252b5132
RH
10768 }
10769
10770 return 1;
10771}
10772
551c1ca1 10773static char *
e3bb37b5 10774i386_scale (char *scale)
252b5132 10775{
551c1ca1
AM
10776 offsetT val;
10777 char *save = input_line_pointer;
252b5132 10778
551c1ca1
AM
10779 input_line_pointer = scale;
10780 val = get_absolute_expression ();
10781
10782 switch (val)
252b5132 10783 {
551c1ca1 10784 case 1:
252b5132
RH
10785 i.log2_scale_factor = 0;
10786 break;
551c1ca1 10787 case 2:
252b5132
RH
10788 i.log2_scale_factor = 1;
10789 break;
551c1ca1 10790 case 4:
252b5132
RH
10791 i.log2_scale_factor = 2;
10792 break;
551c1ca1 10793 case 8:
252b5132
RH
10794 i.log2_scale_factor = 3;
10795 break;
10796 default:
a724f0f4
JB
10797 {
10798 char sep = *input_line_pointer;
10799
10800 *input_line_pointer = '\0';
10801 as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
10802 scale);
10803 *input_line_pointer = sep;
10804 input_line_pointer = save;
10805 return NULL;
10806 }
252b5132 10807 }
29b0f896 10808 if (i.log2_scale_factor != 0 && i.index_reg == 0)
252b5132
RH
10809 {
10810 as_warn (_("scale factor of %d without an index register"),
24eab124 10811 1 << i.log2_scale_factor);
252b5132 10812 i.log2_scale_factor = 0;
252b5132 10813 }
551c1ca1
AM
10814 scale = input_line_pointer;
10815 input_line_pointer = save;
10816 return scale;
252b5132
RH
10817}
10818
252b5132 10819static int
e3bb37b5 10820i386_displacement (char *disp_start, char *disp_end)
252b5132 10821{
29b0f896 10822 expressionS *exp;
252b5132
RH
10823 segT exp_seg = 0;
10824 char *save_input_line_pointer;
f3c180ae 10825 char *gotfree_input_line;
40fb9820
L
10826 int override;
10827 i386_operand_type bigdisp, types = anydisp;
3992d3b7 10828 int ret;
252b5132 10829
31b2323c
L
10830 if (i.disp_operands == MAX_MEMORY_OPERANDS)
10831 {
10832 as_bad (_("at most %d displacement operands are allowed"),
10833 MAX_MEMORY_OPERANDS);
10834 return 0;
10835 }
10836
0dfbf9d7 10837 operand_type_set (&bigdisp, 0);
6f2f06be 10838 if (i.jumpabsolute
48bcea9f 10839 || i.types[this_operand].bitfield.baseindex
0cfa3eb3
JB
10840 || (current_templates->start->opcode_modifier.jump != JUMP
10841 && current_templates->start->opcode_modifier.jump != JUMP_DWORD))
e05278af 10842 {
48bcea9f 10843 i386_addressing_mode ();
e05278af 10844 override = (i.prefix[ADDR_PREFIX] != 0);
40fb9820
L
10845 if (flag_code == CODE_64BIT)
10846 {
a775efc8 10847 bigdisp.bitfield.disp32 = 1;
40fb9820 10848 if (!override)
a775efc8 10849 bigdisp.bitfield.disp64 = 1;
40fb9820
L
10850 }
10851 else if ((flag_code == CODE_16BIT) ^ override)
40fb9820 10852 bigdisp.bitfield.disp16 = 1;
48bcea9f
JB
10853 else
10854 bigdisp.bitfield.disp32 = 1;
e05278af
JB
10855 }
10856 else
10857 {
376cd056
JB
10858 /* For PC-relative branches, the width of the displacement may be
10859 dependent upon data size, but is never dependent upon address size.
10860 Also make sure to not unintentionally match against a non-PC-relative
10861 branch template. */
10862 static templates aux_templates;
10863 const insn_template *t = current_templates->start;
5b7c81bd 10864 bool has_intel64 = false;
376cd056
JB
10865
10866 aux_templates.start = t;
10867 while (++t < current_templates->end)
10868 {
10869 if (t->opcode_modifier.jump
10870 != current_templates->start->opcode_modifier.jump)
10871 break;
4b5aaf5f 10872 if ((t->opcode_modifier.isa64 >= INTEL64))
5b7c81bd 10873 has_intel64 = true;
376cd056
JB
10874 }
10875 if (t < current_templates->end)
10876 {
10877 aux_templates.end = t;
10878 current_templates = &aux_templates;
10879 }
10880
e05278af 10881 override = (i.prefix[DATA_PREFIX] != 0);
40fb9820
L
10882 if (flag_code == CODE_64BIT)
10883 {
376cd056
JB
10884 if ((override || i.suffix == WORD_MNEM_SUFFIX)
10885 && (!intel64 || !has_intel64))
40fb9820
L
10886 bigdisp.bitfield.disp16 = 1;
10887 else
a775efc8 10888 bigdisp.bitfield.disp32 = 1;
40fb9820
L
10889 }
10890 else
e05278af
JB
10891 {
10892 if (!override)
10893 override = (i.suffix == (flag_code != CODE_16BIT
10894 ? WORD_MNEM_SUFFIX
10895 : LONG_MNEM_SUFFIX));
40fb9820
L
10896 bigdisp.bitfield.disp32 = 1;
10897 if ((flag_code == CODE_16BIT) ^ override)
10898 {
10899 bigdisp.bitfield.disp32 = 0;
10900 bigdisp.bitfield.disp16 = 1;
10901 }
e05278af 10902 }
e05278af 10903 }
c6fb90c8
L
10904 i.types[this_operand] = operand_type_or (i.types[this_operand],
10905 bigdisp);
252b5132
RH
10906
10907 exp = &disp_expressions[i.disp_operands];
520dc8e8 10908 i.op[this_operand].disps = exp;
252b5132
RH
10909 i.disp_operands++;
10910 save_input_line_pointer = input_line_pointer;
10911 input_line_pointer = disp_start;
10912 END_STRING_AND_SAVE (disp_end);
10913
10914#ifndef GCC_ASM_O_HACK
10915#define GCC_ASM_O_HACK 0
10916#endif
10917#if GCC_ASM_O_HACK
10918 END_STRING_AND_SAVE (disp_end + 1);
40fb9820 10919 if (i.types[this_operand].bitfield.baseIndex
24eab124 10920 && displacement_string_end[-1] == '+')
252b5132
RH
10921 {
10922 /* This hack is to avoid a warning when using the "o"
24eab124
AM
10923 constraint within gcc asm statements.
10924 For instance:
10925
10926 #define _set_tssldt_desc(n,addr,limit,type) \
10927 __asm__ __volatile__ ( \
10928 "movw %w2,%0\n\t" \
10929 "movw %w1,2+%0\n\t" \
10930 "rorl $16,%1\n\t" \
10931 "movb %b1,4+%0\n\t" \
10932 "movb %4,5+%0\n\t" \
10933 "movb $0,6+%0\n\t" \
10934 "movb %h1,7+%0\n\t" \
10935 "rorl $16,%1" \
10936 : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
10937
10938 This works great except that the output assembler ends
10939 up looking a bit weird if it turns out that there is
10940 no offset. You end up producing code that looks like:
10941
10942 #APP
10943 movw $235,(%eax)
10944 movw %dx,2+(%eax)
10945 rorl $16,%edx
10946 movb %dl,4+(%eax)
10947 movb $137,5+(%eax)
10948 movb $0,6+(%eax)
10949 movb %dh,7+(%eax)
10950 rorl $16,%edx
10951 #NO_APP
10952
47926f60 10953 So here we provide the missing zero. */
24eab124
AM
10954
10955 *displacement_string_end = '0';
252b5132
RH
10956 }
10957#endif
d258b828 10958 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
10959 if (gotfree_input_line)
10960 input_line_pointer = gotfree_input_line;
252b5132 10961
24eab124 10962 exp_seg = expression (exp);
252b5132 10963
636c26b0
AM
10964 SKIP_WHITESPACE ();
10965 if (*input_line_pointer)
10966 as_bad (_("junk `%s' after expression"), input_line_pointer);
10967#if GCC_ASM_O_HACK
10968 RESTORE_END_STRING (disp_end + 1);
10969#endif
636c26b0 10970 input_line_pointer = save_input_line_pointer;
636c26b0 10971 if (gotfree_input_line)
ee86248c
JB
10972 {
10973 free (gotfree_input_line);
10974
10975 if (exp->X_op == O_constant || exp->X_op == O_register)
10976 exp->X_op = O_illegal;
10977 }
10978
10979 ret = i386_finalize_displacement (exp_seg, exp, types, disp_start);
10980
10981 RESTORE_END_STRING (disp_end);
10982
10983 return ret;
10984}
10985
10986static int
10987i386_finalize_displacement (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
10988 i386_operand_type types, const char *disp_start)
10989{
ee86248c 10990 int ret = 1;
636c26b0 10991
24eab124
AM
10992 /* We do this to make sure that the section symbol is in
10993 the symbol table. We will ultimately change the relocation
47926f60 10994 to be relative to the beginning of the section. */
1ae12ab7 10995 if (i.reloc[this_operand] == BFD_RELOC_386_GOTOFF
d6ab8113
JB
10996 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
10997 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
24eab124 10998 {
636c26b0 10999 if (exp->X_op != O_symbol)
3992d3b7 11000 goto inv_disp;
636c26b0 11001
e5cb08ac 11002 if (S_IS_LOCAL (exp->X_add_symbol)
c64efb4b
L
11003 && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section
11004 && S_GET_SEGMENT (exp->X_add_symbol) != expr_section)
24eab124 11005 section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
24eab124
AM
11006 exp->X_op = O_subtract;
11007 exp->X_op_symbol = GOT_symbol;
1ae12ab7 11008 if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
29b0f896 11009 i.reloc[this_operand] = BFD_RELOC_32_PCREL;
d6ab8113
JB
11010 else if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
11011 i.reloc[this_operand] = BFD_RELOC_64;
23df1078 11012 else
29b0f896 11013 i.reloc[this_operand] = BFD_RELOC_32;
24eab124 11014 }
252b5132 11015
3992d3b7
AM
11016 else if (exp->X_op == O_absent
11017 || exp->X_op == O_illegal
ee86248c 11018 || exp->X_op == O_big)
2daf4fd8 11019 {
3992d3b7
AM
11020 inv_disp:
11021 as_bad (_("missing or invalid displacement expression `%s'"),
2daf4fd8 11022 disp_start);
3992d3b7 11023 ret = 0;
2daf4fd8
AM
11024 }
11025
a50187b2
JB
11026 else if (exp->X_op == O_constant)
11027 {
11028 /* Sizing gets taken care of by optimize_disp().
11029
11030 If not 64bit, sign/zero extend val, to account for wraparound
11031 when !BFD64. */
11032 if (flag_code != CODE_64BIT)
11033 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
11034 }
11035
4c63da97 11036#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
a50187b2 11037 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
3992d3b7
AM
11038 && exp_seg != absolute_section
11039 && exp_seg != text_section
11040 && exp_seg != data_section
11041 && exp_seg != bss_section
11042 && exp_seg != undefined_section
11043 && !bfd_is_com_section (exp_seg))
24eab124 11044 {
d0b47220 11045 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
3992d3b7 11046 ret = 0;
24eab124 11047 }
252b5132 11048#endif
3956db08 11049
a50187b2 11050 else if (current_templates->start->opcode_modifier.jump == JUMP_BYTE)
48bcea9f
JB
11051 i.types[this_operand].bitfield.disp8 = 1;
11052
40fb9820 11053 /* Check if this is a displacement only operand. */
02b83698
JB
11054 if (!i.types[this_operand].bitfield.baseindex)
11055 i.types[this_operand] =
11056 operand_type_or (operand_type_and_not (i.types[this_operand], anydisp),
11057 operand_type_and (i.types[this_operand], types));
3956db08 11058
3992d3b7 11059 return ret;
252b5132
RH
11060}
11061
2abc2bec
JB
11062/* Return the active addressing mode, taking address override and
11063 registers forming the address into consideration. Update the
11064 address override prefix if necessary. */
47926f60 11065
2abc2bec
JB
11066static enum flag_code
11067i386_addressing_mode (void)
252b5132 11068{
be05d201
L
11069 enum flag_code addr_mode;
11070
11071 if (i.prefix[ADDR_PREFIX])
11072 addr_mode = flag_code == CODE_32BIT ? CODE_16BIT : CODE_32BIT;
a23b33b3
JB
11073 else if (flag_code == CODE_16BIT
11074 && current_templates->start->cpu_flags.bitfield.cpumpx
11075 /* Avoid replacing the "16-bit addressing not allowed" diagnostic
11076 from md_assemble() by "is not a valid base/index expression"
11077 when there is a base and/or index. */
11078 && !i.types[this_operand].bitfield.baseindex)
11079 {
11080 /* MPX insn memory operands with neither base nor index must be forced
11081 to use 32-bit addressing in 16-bit mode. */
11082 addr_mode = CODE_32BIT;
11083 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
11084 ++i.prefixes;
11085 gas_assert (!i.types[this_operand].bitfield.disp16);
11086 gas_assert (!i.types[this_operand].bitfield.disp32);
11087 }
be05d201
L
11088 else
11089 {
11090 addr_mode = flag_code;
11091
24eab124 11092#if INFER_ADDR_PREFIX
be05d201
L
11093 if (i.mem_operands == 0)
11094 {
11095 /* Infer address prefix from the first memory operand. */
11096 const reg_entry *addr_reg = i.base_reg;
11097
11098 if (addr_reg == NULL)
11099 addr_reg = i.index_reg;
eecb386c 11100
be05d201
L
11101 if (addr_reg)
11102 {
e968fc9b 11103 if (addr_reg->reg_type.bitfield.dword)
be05d201
L
11104 addr_mode = CODE_32BIT;
11105 else if (flag_code != CODE_64BIT
dc821c5f 11106 && addr_reg->reg_type.bitfield.word)
be05d201
L
11107 addr_mode = CODE_16BIT;
11108
11109 if (addr_mode != flag_code)
11110 {
11111 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
11112 i.prefixes += 1;
11113 /* Change the size of any displacement too. At most one
11114 of Disp16 or Disp32 is set.
11115 FIXME. There doesn't seem to be any real need for
11116 separate Disp16 and Disp32 flags. The same goes for
11117 Imm16 and Imm32. Removing them would probably clean
11118 up the code quite a lot. */
11119 if (flag_code != CODE_64BIT
11120 && (i.types[this_operand].bitfield.disp16
11121 || i.types[this_operand].bitfield.disp32))
11122 i.types[this_operand]
11123 = operand_type_xor (i.types[this_operand], disp16_32);
11124 }
11125 }
11126 }
24eab124 11127#endif
be05d201
L
11128 }
11129
2abc2bec
JB
11130 return addr_mode;
11131}
11132
11133/* Make sure the memory operand we've been dealt is valid.
11134 Return 1 on success, 0 on a failure. */
11135
11136static int
11137i386_index_check (const char *operand_string)
11138{
11139 const char *kind = "base/index";
11140 enum flag_code addr_mode = i386_addressing_mode ();
a152332d 11141 const insn_template *t = current_templates->start;
2abc2bec 11142
a152332d
JB
11143 if (t->opcode_modifier.isstring
11144 && !t->cpu_flags.bitfield.cpupadlock
fc0763e6
JB
11145 && (current_templates->end[-1].opcode_modifier.isstring
11146 || i.mem_operands))
11147 {
11148 /* Memory operands of string insns are special in that they only allow
11149 a single register (rDI, rSI, or rBX) as their memory address. */
be05d201
L
11150 const reg_entry *expected_reg;
11151 static const char *di_si[][2] =
11152 {
11153 { "esi", "edi" },
11154 { "si", "di" },
11155 { "rsi", "rdi" }
11156 };
11157 static const char *bx[] = { "ebx", "bx", "rbx" };
fc0763e6
JB
11158
11159 kind = "string address";
11160
a152332d 11161 if (t->opcode_modifier.prefixok == PrefixRep)
fc0763e6 11162 {
51c8edf6
JB
11163 int es_op = current_templates->end[-1].opcode_modifier.isstring
11164 - IS_STRING_ES_OP0;
11165 int op = 0;
fc0763e6 11166
51c8edf6 11167 if (!current_templates->end[-1].operand_types[0].bitfield.baseindex
fc0763e6
JB
11168 || ((!i.mem_operands != !intel_syntax)
11169 && current_templates->end[-1].operand_types[1]
11170 .bitfield.baseindex))
51c8edf6 11171 op = 1;
fe0e921f
AM
11172 expected_reg
11173 = (const reg_entry *) str_hash_find (reg_hash,
11174 di_si[addr_mode][op == es_op]);
fc0763e6
JB
11175 }
11176 else
fe0e921f
AM
11177 expected_reg
11178 = (const reg_entry *)str_hash_find (reg_hash, bx[addr_mode]);
fc0763e6 11179
be05d201
L
11180 if (i.base_reg != expected_reg
11181 || i.index_reg
fc0763e6 11182 || operand_type_check (i.types[this_operand], disp))
fc0763e6 11183 {
be05d201
L
11184 /* The second memory operand must have the same size as
11185 the first one. */
11186 if (i.mem_operands
11187 && i.base_reg
11188 && !((addr_mode == CODE_64BIT
dc821c5f 11189 && i.base_reg->reg_type.bitfield.qword)
be05d201 11190 || (addr_mode == CODE_32BIT
dc821c5f
JB
11191 ? i.base_reg->reg_type.bitfield.dword
11192 : i.base_reg->reg_type.bitfield.word)))
be05d201
L
11193 goto bad_address;
11194
fc0763e6
JB
11195 as_warn (_("`%s' is not valid here (expected `%c%s%s%c')"),
11196 operand_string,
11197 intel_syntax ? '[' : '(',
11198 register_prefix,
be05d201 11199 expected_reg->reg_name,
fc0763e6 11200 intel_syntax ? ']' : ')');
be05d201 11201 return 1;
fc0763e6 11202 }
be05d201
L
11203 else
11204 return 1;
11205
dc1e8a47 11206 bad_address:
be05d201
L
11207 as_bad (_("`%s' is not a valid %s expression"),
11208 operand_string, kind);
11209 return 0;
3e73aa7c
JH
11210 }
11211 else
11212 {
be05d201
L
11213 if (addr_mode != CODE_16BIT)
11214 {
11215 /* 32-bit/64-bit checks. */
41eb8e88
L
11216 if (i.disp_encoding == disp_encoding_16bit)
11217 {
11218 bad_disp:
11219 as_bad (_("invalid `%s' prefix"),
11220 addr_mode == CODE_16BIT ? "{disp32}" : "{disp16}");
11221 return 0;
11222 }
11223
be05d201 11224 if ((i.base_reg
e968fc9b
JB
11225 && ((addr_mode == CODE_64BIT
11226 ? !i.base_reg->reg_type.bitfield.qword
11227 : !i.base_reg->reg_type.bitfield.dword)
11228 || (i.index_reg && i.base_reg->reg_num == RegIP)
11229 || i.base_reg->reg_num == RegIZ))
be05d201 11230 || (i.index_reg
1b54b8d7
JB
11231 && !i.index_reg->reg_type.bitfield.xmmword
11232 && !i.index_reg->reg_type.bitfield.ymmword
11233 && !i.index_reg->reg_type.bitfield.zmmword
be05d201 11234 && ((addr_mode == CODE_64BIT
e968fc9b
JB
11235 ? !i.index_reg->reg_type.bitfield.qword
11236 : !i.index_reg->reg_type.bitfield.dword)
be05d201
L
11237 || !i.index_reg->reg_type.bitfield.baseindex)))
11238 goto bad_address;
8178be5b 11239
260cd341 11240 /* bndmk, bndldx, bndstx and mandatory non-vector SIB have special restrictions. */
a152332d 11241 if ((t->opcode_modifier.opcodeprefix == PREFIX_0XF3
389d00a5
JB
11242 && t->opcode_modifier.opcodespace == SPACE_0F
11243 && t->base_opcode == 0x1b)
a152332d 11244 || (t->opcode_modifier.opcodeprefix == PREFIX_NONE
389d00a5
JB
11245 && t->opcode_modifier.opcodespace == SPACE_0F
11246 && (t->base_opcode & ~1) == 0x1a)
a152332d 11247 || t->opcode_modifier.sib == SIBMEM)
8178be5b
JB
11248 {
11249 /* They cannot use RIP-relative addressing. */
e968fc9b 11250 if (i.base_reg && i.base_reg->reg_num == RegIP)
8178be5b
JB
11251 {
11252 as_bad (_("`%s' cannot be used here"), operand_string);
11253 return 0;
11254 }
11255
11256 /* bndldx and bndstx ignore their scale factor. */
a152332d 11257 if (t->opcode_modifier.opcodeprefix == PREFIX_NONE
389d00a5
JB
11258 && t->opcode_modifier.opcodespace == SPACE_0F
11259 && (t->base_opcode & ~1) == 0x1a
8178be5b
JB
11260 && i.log2_scale_factor)
11261 as_warn (_("register scaling is being ignored here"));
11262 }
be05d201
L
11263 }
11264 else
3e73aa7c 11265 {
be05d201 11266 /* 16-bit checks. */
41eb8e88
L
11267 if (i.disp_encoding == disp_encoding_32bit)
11268 goto bad_disp;
11269
3e73aa7c 11270 if ((i.base_reg
dc821c5f 11271 && (!i.base_reg->reg_type.bitfield.word
40fb9820 11272 || !i.base_reg->reg_type.bitfield.baseindex))
3e73aa7c 11273 || (i.index_reg
dc821c5f 11274 && (!i.index_reg->reg_type.bitfield.word
40fb9820 11275 || !i.index_reg->reg_type.bitfield.baseindex
29b0f896
AM
11276 || !(i.base_reg
11277 && i.base_reg->reg_num < 6
11278 && i.index_reg->reg_num >= 6
11279 && i.log2_scale_factor == 0))))
be05d201 11280 goto bad_address;
3e73aa7c
JH
11281 }
11282 }
be05d201 11283 return 1;
24eab124 11284}
252b5132 11285
43234a1e
L
11286/* Handle vector immediates. */
11287
11288static int
11289RC_SAE_immediate (const char *imm_start)
11290{
43234a1e 11291 const char *pstr = imm_start;
43234a1e
L
11292
11293 if (*pstr != '{')
11294 return 0;
11295
7063667e
JB
11296 pstr = RC_SAE_specifier (pstr + 1);
11297 if (pstr == NULL)
43234a1e
L
11298 return 0;
11299
11300 if (*pstr++ != '}')
11301 {
11302 as_bad (_("Missing '}': '%s'"), imm_start);
11303 return 0;
11304 }
11305 /* RC/SAE immediate string should contain nothing more. */;
11306 if (*pstr != 0)
11307 {
11308 as_bad (_("Junk after '}': '%s'"), imm_start);
11309 return 0;
11310 }
11311
cf665fee
JB
11312 /* Internally this doesn't count as an operand. */
11313 --i.operands;
43234a1e 11314
43234a1e
L
11315 return 1;
11316}
11317
8325cc63
JB
11318/* Only string instructions can have a second memory operand, so
11319 reduce current_templates to just those if it contains any. */
11320static int
11321maybe_adjust_templates (void)
11322{
11323 const insn_template *t;
11324
11325 gas_assert (i.mem_operands == 1);
11326
11327 for (t = current_templates->start; t < current_templates->end; ++t)
11328 if (t->opcode_modifier.isstring)
11329 break;
11330
11331 if (t < current_templates->end)
11332 {
11333 static templates aux_templates;
5b7c81bd 11334 bool recheck;
8325cc63
JB
11335
11336 aux_templates.start = t;
11337 for (; t < current_templates->end; ++t)
11338 if (!t->opcode_modifier.isstring)
11339 break;
11340 aux_templates.end = t;
11341
11342 /* Determine whether to re-check the first memory operand. */
11343 recheck = (aux_templates.start != current_templates->start
11344 || t != current_templates->end);
11345
11346 current_templates = &aux_templates;
11347
11348 if (recheck)
11349 {
11350 i.mem_operands = 0;
11351 if (i.memop1_string != NULL
11352 && i386_index_check (i.memop1_string) == 0)
11353 return 0;
11354 i.mem_operands = 1;
11355 }
11356 }
11357
11358 return 1;
11359}
11360
9d299bea
JB
11361static INLINE bool starts_memory_operand (char c)
11362{
014fbcda 11363 return ISDIGIT (c)
9d299bea 11364 || is_identifier_char (c)
014fbcda 11365 || strchr ("([\"+-!~", c);
9d299bea
JB
11366}
11367
fc0763e6 11368/* Parse OPERAND_STRING into the i386_insn structure I. Returns zero
47926f60 11369 on error. */
252b5132 11370
252b5132 11371static int
a7619375 11372i386_att_operand (char *operand_string)
252b5132 11373{
af6bdddf
AM
11374 const reg_entry *r;
11375 char *end_op;
24eab124 11376 char *op_string = operand_string;
252b5132 11377
24eab124 11378 if (is_space_char (*op_string))
252b5132
RH
11379 ++op_string;
11380
24eab124 11381 /* We check for an absolute prefix (differentiating,
47926f60 11382 for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
24eab124
AM
11383 if (*op_string == ABSOLUTE_PREFIX)
11384 {
11385 ++op_string;
11386 if (is_space_char (*op_string))
11387 ++op_string;
5b7c81bd 11388 i.jumpabsolute = true;
24eab124 11389 }
252b5132 11390
47926f60 11391 /* Check if operand is a register. */
4d1bb795 11392 if ((r = parse_register (op_string, &end_op)) != NULL)
24eab124 11393 {
40fb9820
L
11394 i386_operand_type temp;
11395
8a6fb3f9
JB
11396 if (r == &bad_reg)
11397 return 0;
11398
24eab124
AM
11399 /* Check for a segment override by searching for ':' after a
11400 segment register. */
11401 op_string = end_op;
11402 if (is_space_char (*op_string))
11403 ++op_string;
00cee14f 11404 if (*op_string == ':' && r->reg_type.bitfield.class == SReg)
24eab124 11405 {
5e042380 11406 i.seg[i.mem_operands] = r;
252b5132 11407
24eab124 11408 /* Skip the ':' and whitespace. */
252b5132
RH
11409 ++op_string;
11410 if (is_space_char (*op_string))
24eab124 11411 ++op_string;
252b5132 11412
47926f60 11413 /* Handle case of %es:*foo. */
c8d541e2 11414 if (!i.jumpabsolute && *op_string == ABSOLUTE_PREFIX)
24eab124
AM
11415 {
11416 ++op_string;
11417 if (is_space_char (*op_string))
11418 ++op_string;
5b7c81bd 11419 i.jumpabsolute = true;
24eab124 11420 }
c8d541e2 11421
9d299bea 11422 if (!starts_memory_operand (*op_string))
c8d541e2
JB
11423 {
11424 as_bad (_("bad memory operand `%s'"), op_string);
11425 return 0;
11426 }
24eab124
AM
11427 goto do_memory_reference;
11428 }
43234a1e
L
11429
11430 /* Handle vector operations. */
11431 if (*op_string == '{')
11432 {
f70c6814 11433 op_string = check_VecOperations (op_string);
43234a1e
L
11434 if (op_string == NULL)
11435 return 0;
11436 }
11437
24eab124
AM
11438 if (*op_string)
11439 {
d0b47220 11440 as_bad (_("junk `%s' after register"), op_string);
24eab124
AM
11441 return 0;
11442 }
40fb9820
L
11443 temp = r->reg_type;
11444 temp.bitfield.baseindex = 0;
c6fb90c8
L
11445 i.types[this_operand] = operand_type_or (i.types[this_operand],
11446 temp);
7d5e4556 11447 i.types[this_operand].bitfield.unspecified = 0;
520dc8e8 11448 i.op[this_operand].regs = r;
24eab124 11449 i.reg_operands++;
cf665fee
JB
11450
11451 /* A GPR may follow an RC or SAE immediate only if a (vector) register
11452 operand was also present earlier on. */
11453 if (i.rounding.type != rc_none && temp.bitfield.class == Reg
11454 && i.reg_operands == 1)
11455 {
11456 unsigned int j;
11457
11458 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); ++j)
11459 if (i.rounding.type == RC_NamesTable[j].type)
11460 break;
11461 as_bad (_("`%s': misplaced `{%s}'"),
11462 current_templates->start->name, RC_NamesTable[j].name);
11463 return 0;
11464 }
24eab124 11465 }
af6bdddf
AM
11466 else if (*op_string == REGISTER_PREFIX)
11467 {
11468 as_bad (_("bad register name `%s'"), op_string);
11469 return 0;
11470 }
24eab124 11471 else if (*op_string == IMMEDIATE_PREFIX)
ce8a8b2f 11472 {
24eab124 11473 ++op_string;
6f2f06be 11474 if (i.jumpabsolute)
24eab124 11475 {
d0b47220 11476 as_bad (_("immediate operand illegal with absolute jump"));
24eab124
AM
11477 return 0;
11478 }
11479 if (!i386_immediate (op_string))
11480 return 0;
cf665fee
JB
11481 if (i.rounding.type != rc_none)
11482 {
11483 as_bad (_("`%s': RC/SAE operand must follow immediate operands"),
11484 current_templates->start->name);
11485 return 0;
11486 }
24eab124 11487 }
43234a1e
L
11488 else if (RC_SAE_immediate (operand_string))
11489 {
cf665fee
JB
11490 /* If it is a RC or SAE immediate, do the necessary placement check:
11491 Only another immediate or a GPR may precede it. */
11492 if (i.mem_operands || i.reg_operands + i.imm_operands > 1
11493 || (i.reg_operands == 1
11494 && i.op[0].regs->reg_type.bitfield.class != Reg))
11495 {
11496 as_bad (_("`%s': misplaced `%s'"),
11497 current_templates->start->name, operand_string);
11498 return 0;
11499 }
43234a1e 11500 }
9d299bea 11501 else if (starts_memory_operand (*op_string))
24eab124 11502 {
47926f60 11503 /* This is a memory reference of some sort. */
af6bdddf 11504 char *base_string;
252b5132 11505
47926f60 11506 /* Start and end of displacement string expression (if found). */
eecb386c
AM
11507 char *displacement_string_start;
11508 char *displacement_string_end;
252b5132 11509
24eab124 11510 do_memory_reference:
8325cc63
JB
11511 if (i.mem_operands == 1 && !maybe_adjust_templates ())
11512 return 0;
24eab124 11513 if ((i.mem_operands == 1
40fb9820 11514 && !current_templates->start->opcode_modifier.isstring)
24eab124
AM
11515 || i.mem_operands == 2)
11516 {
11517 as_bad (_("too many memory references for `%s'"),
11518 current_templates->start->name);
11519 return 0;
11520 }
252b5132 11521
24eab124
AM
11522 /* Check for base index form. We detect the base index form by
11523 looking for an ')' at the end of the operand, searching
11524 for the '(' matching it, and finding a REGISTER_PREFIX or ','
11525 after the '('. */
af6bdddf 11526 base_string = op_string + strlen (op_string);
c3332e24 11527
43234a1e 11528 /* Handle vector operations. */
6b5ba0d4
JB
11529 --base_string;
11530 if (is_space_char (*base_string))
11531 --base_string;
11532
11533 if (*base_string == '}')
43234a1e 11534 {
6b5ba0d4
JB
11535 char *vop_start = NULL;
11536
11537 while (base_string-- > op_string)
11538 {
11539 if (*base_string == '"')
11540 break;
11541 if (*base_string != '{')
11542 continue;
11543
11544 vop_start = base_string;
11545
11546 --base_string;
11547 if (is_space_char (*base_string))
11548 --base_string;
11549
11550 if (*base_string != '}')
11551 break;
11552
11553 vop_start = NULL;
11554 }
11555
11556 if (!vop_start)
11557 {
11558 as_bad (_("unbalanced figure braces"));
11559 return 0;
11560 }
11561
f70c6814 11562 if (check_VecOperations (vop_start) == NULL)
43234a1e 11563 return 0;
43234a1e
L
11564 }
11565
47926f60 11566 /* If we only have a displacement, set-up for it to be parsed later. */
af6bdddf
AM
11567 displacement_string_start = op_string;
11568 displacement_string_end = base_string + 1;
252b5132 11569
24eab124
AM
11570 if (*base_string == ')')
11571 {
af6bdddf 11572 char *temp_string;
cc0f9635 11573 unsigned int parens_not_balanced = 1;
e68c3d59 11574
24eab124 11575 /* We've already checked that the number of left & right ()'s are
47926f60 11576 equal, so this loop will not be infinite. */
24eab124
AM
11577 do
11578 {
11579 base_string--;
cc0f9635
JB
11580 if (*base_string == ')')
11581 parens_not_balanced++;
11582 if (*base_string == '(')
11583 parens_not_balanced--;
24eab124 11584 }
cc0f9635 11585 while (parens_not_balanced && *base_string != '"');
c3332e24 11586
af6bdddf 11587 temp_string = base_string;
c3332e24 11588
24eab124 11589 /* Skip past '(' and whitespace. */
e68c3d59
JB
11590 if (*base_string == '(')
11591 ++base_string;
252b5132 11592 if (is_space_char (*base_string))
24eab124 11593 ++base_string;
252b5132 11594
af6bdddf 11595 if (*base_string == ','
4eed87de
AM
11596 || ((i.base_reg = parse_register (base_string, &end_op))
11597 != NULL))
252b5132 11598 {
af6bdddf 11599 displacement_string_end = temp_string;
252b5132 11600
40fb9820 11601 i.types[this_operand].bitfield.baseindex = 1;
252b5132 11602
af6bdddf 11603 if (i.base_reg)
24eab124 11604 {
8a6fb3f9
JB
11605 if (i.base_reg == &bad_reg)
11606 return 0;
24eab124
AM
11607 base_string = end_op;
11608 if (is_space_char (*base_string))
11609 ++base_string;
af6bdddf
AM
11610 }
11611
11612 /* There may be an index reg or scale factor here. */
11613 if (*base_string == ',')
11614 {
11615 ++base_string;
11616 if (is_space_char (*base_string))
11617 ++base_string;
11618
4eed87de
AM
11619 if ((i.index_reg = parse_register (base_string, &end_op))
11620 != NULL)
24eab124 11621 {
8a6fb3f9
JB
11622 if (i.index_reg == &bad_reg)
11623 return 0;
af6bdddf 11624 base_string = end_op;
24eab124
AM
11625 if (is_space_char (*base_string))
11626 ++base_string;
af6bdddf
AM
11627 if (*base_string == ',')
11628 {
11629 ++base_string;
11630 if (is_space_char (*base_string))
11631 ++base_string;
11632 }
e5cb08ac 11633 else if (*base_string != ')')
af6bdddf 11634 {
4eed87de
AM
11635 as_bad (_("expecting `,' or `)' "
11636 "after index register in `%s'"),
af6bdddf
AM
11637 operand_string);
11638 return 0;
11639 }
24eab124 11640 }
af6bdddf 11641 else if (*base_string == REGISTER_PREFIX)
24eab124 11642 {
f76bf5e0
L
11643 end_op = strchr (base_string, ',');
11644 if (end_op)
11645 *end_op = '\0';
af6bdddf 11646 as_bad (_("bad register name `%s'"), base_string);
24eab124
AM
11647 return 0;
11648 }
252b5132 11649
47926f60 11650 /* Check for scale factor. */
551c1ca1 11651 if (*base_string != ')')
af6bdddf 11652 {
551c1ca1
AM
11653 char *end_scale = i386_scale (base_string);
11654
11655 if (!end_scale)
af6bdddf 11656 return 0;
24eab124 11657
551c1ca1 11658 base_string = end_scale;
af6bdddf
AM
11659 if (is_space_char (*base_string))
11660 ++base_string;
11661 if (*base_string != ')')
11662 {
4eed87de
AM
11663 as_bad (_("expecting `)' "
11664 "after scale factor in `%s'"),
af6bdddf
AM
11665 operand_string);
11666 return 0;
11667 }
11668 }
11669 else if (!i.index_reg)
24eab124 11670 {
4eed87de
AM
11671 as_bad (_("expecting index register or scale factor "
11672 "after `,'; got '%c'"),
af6bdddf 11673 *base_string);
24eab124
AM
11674 return 0;
11675 }
11676 }
af6bdddf 11677 else if (*base_string != ')')
24eab124 11678 {
4eed87de
AM
11679 as_bad (_("expecting `,' or `)' "
11680 "after base register in `%s'"),
af6bdddf 11681 operand_string);
24eab124
AM
11682 return 0;
11683 }
c3332e24 11684 }
af6bdddf 11685 else if (*base_string == REGISTER_PREFIX)
c3332e24 11686 {
f76bf5e0
L
11687 end_op = strchr (base_string, ',');
11688 if (end_op)
11689 *end_op = '\0';
af6bdddf 11690 as_bad (_("bad register name `%s'"), base_string);
24eab124 11691 return 0;
c3332e24 11692 }
24eab124
AM
11693 }
11694
11695 /* If there's an expression beginning the operand, parse it,
11696 assuming displacement_string_start and
11697 displacement_string_end are meaningful. */
11698 if (displacement_string_start != displacement_string_end)
11699 {
11700 if (!i386_displacement (displacement_string_start,
11701 displacement_string_end))
11702 return 0;
11703 }
11704
11705 /* Special case for (%dx) while doing input/output op. */
11706 if (i.base_reg
75e5731b
JB
11707 && i.base_reg->reg_type.bitfield.instance == RegD
11708 && i.base_reg->reg_type.bitfield.word
24eab124
AM
11709 && i.index_reg == 0
11710 && i.log2_scale_factor == 0
11711 && i.seg[i.mem_operands] == 0
40fb9820 11712 && !operand_type_check (i.types[this_operand], disp))
24eab124 11713 {
2fb5be8d 11714 i.types[this_operand] = i.base_reg->reg_type;
24eab124
AM
11715 return 1;
11716 }
11717
eecb386c
AM
11718 if (i386_index_check (operand_string) == 0)
11719 return 0;
c48dadc9 11720 i.flags[this_operand] |= Operand_Mem;
8325cc63
JB
11721 if (i.mem_operands == 0)
11722 i.memop1_string = xstrdup (operand_string);
24eab124
AM
11723 i.mem_operands++;
11724 }
11725 else
ce8a8b2f
AM
11726 {
11727 /* It's not a memory operand; argh! */
24eab124
AM
11728 as_bad (_("invalid char %s beginning operand %d `%s'"),
11729 output_invalid (*op_string),
11730 this_operand + 1,
11731 op_string);
11732 return 0;
11733 }
47926f60 11734 return 1; /* Normal return. */
252b5132
RH
11735}
11736\f
fa94de6b
RM
11737/* Calculate the maximum variable size (i.e., excluding fr_fix)
11738 that an rs_machine_dependent frag may reach. */
11739
11740unsigned int
11741i386_frag_max_var (fragS *frag)
11742{
11743 /* The only relaxable frags are for jumps.
11744 Unconditional jumps can grow by 4 bytes and others by 5 bytes. */
11745 gas_assert (frag->fr_type == rs_machine_dependent);
11746 return TYPE_FROM_RELAX_STATE (frag->fr_subtype) == UNCOND_JUMP ? 4 : 5;
11747}
11748
b084df0b
L
11749#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
11750static int
8dcea932 11751elf_symbol_resolved_in_segment_p (symbolS *fr_symbol, offsetT fr_var)
b084df0b
L
11752{
11753 /* STT_GNU_IFUNC symbol must go through PLT. */
11754 if ((symbol_get_bfdsym (fr_symbol)->flags
11755 & BSF_GNU_INDIRECT_FUNCTION) != 0)
11756 return 0;
11757
11758 if (!S_IS_EXTERNAL (fr_symbol))
11759 /* Symbol may be weak or local. */
11760 return !S_IS_WEAK (fr_symbol);
11761
8dcea932
L
11762 /* Global symbols with non-default visibility can't be preempted. */
11763 if (ELF_ST_VISIBILITY (S_GET_OTHER (fr_symbol)) != STV_DEFAULT)
11764 return 1;
11765
11766 if (fr_var != NO_RELOC)
11767 switch ((enum bfd_reloc_code_real) fr_var)
11768 {
11769 case BFD_RELOC_386_PLT32:
11770 case BFD_RELOC_X86_64_PLT32:
33eaf5de 11771 /* Symbol with PLT relocation may be preempted. */
8dcea932
L
11772 return 0;
11773 default:
11774 abort ();
11775 }
11776
b084df0b
L
11777 /* Global symbols with default visibility in a shared library may be
11778 preempted by another definition. */
8dcea932 11779 return !shared;
b084df0b
L
11780}
11781#endif
11782
79d72f45
HL
11783/* Table 3-2. Macro-Fusible Instructions in Haswell Microarchitecture
11784 Note also work for Skylake and Cascadelake.
11785---------------------------------------------------------------------
11786| JCC | ADD/SUB/CMP | INC/DEC | TEST/AND |
11787| ------ | ----------- | ------- | -------- |
11788| Jo | N | N | Y |
11789| Jno | N | N | Y |
11790| Jc/Jb | Y | N | Y |
11791| Jae/Jnb | Y | N | Y |
11792| Je/Jz | Y | Y | Y |
11793| Jne/Jnz | Y | Y | Y |
11794| Jna/Jbe | Y | N | Y |
11795| Ja/Jnbe | Y | N | Y |
11796| Js | N | N | Y |
11797| Jns | N | N | Y |
11798| Jp/Jpe | N | N | Y |
11799| Jnp/Jpo | N | N | Y |
11800| Jl/Jnge | Y | Y | Y |
11801| Jge/Jnl | Y | Y | Y |
11802| Jle/Jng | Y | Y | Y |
11803| Jg/Jnle | Y | Y | Y |
11804--------------------------------------------------------------------- */
11805static int
11806i386_macro_fusible_p (enum mf_cmp_kind mf_cmp, enum mf_jcc_kind mf_jcc)
11807{
11808 if (mf_cmp == mf_cmp_alu_cmp)
11809 return ((mf_jcc >= mf_jcc_jc && mf_jcc <= mf_jcc_jna)
11810 || mf_jcc == mf_jcc_jl || mf_jcc == mf_jcc_jle);
11811 if (mf_cmp == mf_cmp_incdec)
11812 return (mf_jcc == mf_jcc_je || mf_jcc == mf_jcc_jl
11813 || mf_jcc == mf_jcc_jle);
11814 if (mf_cmp == mf_cmp_test_and)
11815 return 1;
11816 return 0;
11817}
11818
e379e5f3
L
11819/* Return the next non-empty frag. */
11820
11821static fragS *
11822i386_next_non_empty_frag (fragS *fragP)
11823{
11824 /* There may be a frag with a ".fill 0" when there is no room in
11825 the current frag for frag_grow in output_insn. */
11826 for (fragP = fragP->fr_next;
11827 (fragP != NULL
11828 && fragP->fr_type == rs_fill
11829 && fragP->fr_fix == 0);
11830 fragP = fragP->fr_next)
11831 ;
11832 return fragP;
11833}
11834
11835/* Return the next jcc frag after BRANCH_PADDING. */
11836
11837static fragS *
79d72f45 11838i386_next_fusible_jcc_frag (fragS *maybe_cmp_fragP, fragS *pad_fragP)
e379e5f3 11839{
79d72f45
HL
11840 fragS *branch_fragP;
11841 if (!pad_fragP)
e379e5f3
L
11842 return NULL;
11843
79d72f45
HL
11844 if (pad_fragP->fr_type == rs_machine_dependent
11845 && (TYPE_FROM_RELAX_STATE (pad_fragP->fr_subtype)
e379e5f3
L
11846 == BRANCH_PADDING))
11847 {
79d72f45
HL
11848 branch_fragP = i386_next_non_empty_frag (pad_fragP);
11849 if (branch_fragP->fr_type != rs_machine_dependent)
e379e5f3 11850 return NULL;
79d72f45
HL
11851 if (TYPE_FROM_RELAX_STATE (branch_fragP->fr_subtype) == COND_JUMP
11852 && i386_macro_fusible_p (maybe_cmp_fragP->tc_frag_data.mf_type,
11853 pad_fragP->tc_frag_data.mf_type))
11854 return branch_fragP;
e379e5f3
L
11855 }
11856
11857 return NULL;
11858}
11859
11860/* Classify BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags. */
11861
11862static void
11863i386_classify_machine_dependent_frag (fragS *fragP)
11864{
11865 fragS *cmp_fragP;
11866 fragS *pad_fragP;
11867 fragS *branch_fragP;
11868 fragS *next_fragP;
11869 unsigned int max_prefix_length;
11870
11871 if (fragP->tc_frag_data.classified)
11872 return;
11873
11874 /* First scan for BRANCH_PADDING and FUSED_JCC_PADDING. Convert
11875 FUSED_JCC_PADDING and merge BRANCH_PADDING. */
11876 for (next_fragP = fragP;
11877 next_fragP != NULL;
11878 next_fragP = next_fragP->fr_next)
11879 {
11880 next_fragP->tc_frag_data.classified = 1;
11881 if (next_fragP->fr_type == rs_machine_dependent)
11882 switch (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype))
11883 {
11884 case BRANCH_PADDING:
11885 /* The BRANCH_PADDING frag must be followed by a branch
11886 frag. */
11887 branch_fragP = i386_next_non_empty_frag (next_fragP);
11888 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
11889 break;
11890 case FUSED_JCC_PADDING:
11891 /* Check if this is a fused jcc:
11892 FUSED_JCC_PADDING
11893 CMP like instruction
11894 BRANCH_PADDING
11895 COND_JUMP
11896 */
11897 cmp_fragP = i386_next_non_empty_frag (next_fragP);
11898 pad_fragP = i386_next_non_empty_frag (cmp_fragP);
79d72f45 11899 branch_fragP = i386_next_fusible_jcc_frag (next_fragP, pad_fragP);
e379e5f3
L
11900 if (branch_fragP)
11901 {
11902 /* The BRANCH_PADDING frag is merged with the
11903 FUSED_JCC_PADDING frag. */
11904 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
11905 /* CMP like instruction size. */
11906 next_fragP->tc_frag_data.cmp_size = cmp_fragP->fr_fix;
11907 frag_wane (pad_fragP);
11908 /* Skip to branch_fragP. */
11909 next_fragP = branch_fragP;
11910 }
11911 else if (next_fragP->tc_frag_data.max_prefix_length)
11912 {
11913 /* Turn FUSED_JCC_PADDING into BRANCH_PREFIX if it isn't
11914 a fused jcc. */
11915 next_fragP->fr_subtype
11916 = ENCODE_RELAX_STATE (BRANCH_PREFIX, 0);
11917 next_fragP->tc_frag_data.max_bytes
11918 = next_fragP->tc_frag_data.max_prefix_length;
11919 /* This will be updated in the BRANCH_PREFIX scan. */
11920 next_fragP->tc_frag_data.max_prefix_length = 0;
11921 }
11922 else
11923 frag_wane (next_fragP);
11924 break;
11925 }
11926 }
11927
11928 /* Stop if there is no BRANCH_PREFIX. */
11929 if (!align_branch_prefix_size)
11930 return;
11931
11932 /* Scan for BRANCH_PREFIX. */
11933 for (; fragP != NULL; fragP = fragP->fr_next)
11934 {
11935 if (fragP->fr_type != rs_machine_dependent
11936 || (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
11937 != BRANCH_PREFIX))
11938 continue;
11939
11940 /* Count all BRANCH_PREFIX frags before BRANCH_PADDING and
11941 COND_JUMP_PREFIX. */
11942 max_prefix_length = 0;
11943 for (next_fragP = fragP;
11944 next_fragP != NULL;
11945 next_fragP = next_fragP->fr_next)
11946 {
11947 if (next_fragP->fr_type == rs_fill)
11948 /* Skip rs_fill frags. */
11949 continue;
11950 else if (next_fragP->fr_type != rs_machine_dependent)
11951 /* Stop for all other frags. */
11952 break;
11953
11954 /* rs_machine_dependent frags. */
11955 if (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
11956 == BRANCH_PREFIX)
11957 {
11958 /* Count BRANCH_PREFIX frags. */
11959 if (max_prefix_length >= MAX_FUSED_JCC_PADDING_SIZE)
11960 {
11961 max_prefix_length = MAX_FUSED_JCC_PADDING_SIZE;
11962 frag_wane (next_fragP);
11963 }
11964 else
11965 max_prefix_length
11966 += next_fragP->tc_frag_data.max_bytes;
11967 }
11968 else if ((TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
11969 == BRANCH_PADDING)
11970 || (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
11971 == FUSED_JCC_PADDING))
11972 {
11973 /* Stop at BRANCH_PADDING and FUSED_JCC_PADDING. */
11974 fragP->tc_frag_data.u.padding_fragP = next_fragP;
11975 break;
11976 }
11977 else
11978 /* Stop for other rs_machine_dependent frags. */
11979 break;
11980 }
11981
11982 fragP->tc_frag_data.max_prefix_length = max_prefix_length;
11983
11984 /* Skip to the next frag. */
11985 fragP = next_fragP;
11986 }
11987}
11988
11989/* Compute padding size for
11990
11991 FUSED_JCC_PADDING
11992 CMP like instruction
11993 BRANCH_PADDING
11994 COND_JUMP/UNCOND_JUMP
11995
11996 or
11997
11998 BRANCH_PADDING
11999 COND_JUMP/UNCOND_JUMP
12000 */
12001
12002static int
12003i386_branch_padding_size (fragS *fragP, offsetT address)
12004{
12005 unsigned int offset, size, padding_size;
12006 fragS *branch_fragP = fragP->tc_frag_data.u.branch_fragP;
12007
12008 /* The start address of the BRANCH_PADDING or FUSED_JCC_PADDING frag. */
12009 if (!address)
12010 address = fragP->fr_address;
12011 address += fragP->fr_fix;
12012
12013 /* CMP like instrunction size. */
12014 size = fragP->tc_frag_data.cmp_size;
12015
12016 /* The base size of the branch frag. */
12017 size += branch_fragP->fr_fix;
12018
12019 /* Add opcode and displacement bytes for the rs_machine_dependent
12020 branch frag. */
12021 if (branch_fragP->fr_type == rs_machine_dependent)
12022 size += md_relax_table[branch_fragP->fr_subtype].rlx_length;
12023
12024 /* Check if branch is within boundary and doesn't end at the last
12025 byte. */
12026 offset = address & ((1U << align_branch_power) - 1);
12027 if ((offset + size) >= (1U << align_branch_power))
12028 /* Padding needed to avoid crossing boundary. */
12029 padding_size = (1U << align_branch_power) - offset;
12030 else
12031 /* No padding needed. */
12032 padding_size = 0;
12033
12034 /* The return value may be saved in tc_frag_data.length which is
12035 unsigned byte. */
12036 if (!fits_in_unsigned_byte (padding_size))
12037 abort ();
12038
12039 return padding_size;
12040}
12041
12042/* i386_generic_table_relax_frag()
12043
12044 Handle BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags to
12045 grow/shrink padding to align branch frags. Hand others to
12046 relax_frag(). */
12047
12048long
12049i386_generic_table_relax_frag (segT segment, fragS *fragP, long stretch)
12050{
12051 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12052 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
12053 {
12054 long padding_size = i386_branch_padding_size (fragP, 0);
12055 long grow = padding_size - fragP->tc_frag_data.length;
12056
12057 /* When the BRANCH_PREFIX frag is used, the computed address
12058 must match the actual address and there should be no padding. */
12059 if (fragP->tc_frag_data.padding_address
12060 && (fragP->tc_frag_data.padding_address != fragP->fr_address
12061 || padding_size))
12062 abort ();
12063
12064 /* Update the padding size. */
12065 if (grow)
12066 fragP->tc_frag_data.length = padding_size;
12067
12068 return grow;
12069 }
12070 else if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12071 {
12072 fragS *padding_fragP, *next_fragP;
12073 long padding_size, left_size, last_size;
12074
12075 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
12076 if (!padding_fragP)
12077 /* Use the padding set by the leading BRANCH_PREFIX frag. */
12078 return (fragP->tc_frag_data.length
12079 - fragP->tc_frag_data.last_length);
12080
12081 /* Compute the relative address of the padding frag in the very
12082 first time where the BRANCH_PREFIX frag sizes are zero. */
12083 if (!fragP->tc_frag_data.padding_address)
12084 fragP->tc_frag_data.padding_address
12085 = padding_fragP->fr_address - (fragP->fr_address - stretch);
12086
12087 /* First update the last length from the previous interation. */
12088 left_size = fragP->tc_frag_data.prefix_length;
12089 for (next_fragP = fragP;
12090 next_fragP != padding_fragP;
12091 next_fragP = next_fragP->fr_next)
12092 if (next_fragP->fr_type == rs_machine_dependent
12093 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12094 == BRANCH_PREFIX))
12095 {
12096 if (left_size)
12097 {
12098 int max = next_fragP->tc_frag_data.max_bytes;
12099 if (max)
12100 {
12101 int size;
12102 if (max > left_size)
12103 size = left_size;
12104 else
12105 size = max;
12106 left_size -= size;
12107 next_fragP->tc_frag_data.last_length = size;
12108 }
12109 }
12110 else
12111 next_fragP->tc_frag_data.last_length = 0;
12112 }
12113
12114 /* Check the padding size for the padding frag. */
12115 padding_size = i386_branch_padding_size
12116 (padding_fragP, (fragP->fr_address
12117 + fragP->tc_frag_data.padding_address));
12118
12119 last_size = fragP->tc_frag_data.prefix_length;
12120 /* Check if there is change from the last interation. */
12121 if (padding_size == last_size)
12122 {
12123 /* Update the expected address of the padding frag. */
12124 padding_fragP->tc_frag_data.padding_address
12125 = (fragP->fr_address + padding_size
12126 + fragP->tc_frag_data.padding_address);
12127 return 0;
12128 }
12129
12130 if (padding_size > fragP->tc_frag_data.max_prefix_length)
12131 {
12132 /* No padding if there is no sufficient room. Clear the
12133 expected address of the padding frag. */
12134 padding_fragP->tc_frag_data.padding_address = 0;
12135 padding_size = 0;
12136 }
12137 else
12138 /* Store the expected address of the padding frag. */
12139 padding_fragP->tc_frag_data.padding_address
12140 = (fragP->fr_address + padding_size
12141 + fragP->tc_frag_data.padding_address);
12142
12143 fragP->tc_frag_data.prefix_length = padding_size;
12144
12145 /* Update the length for the current interation. */
12146 left_size = padding_size;
12147 for (next_fragP = fragP;
12148 next_fragP != padding_fragP;
12149 next_fragP = next_fragP->fr_next)
12150 if (next_fragP->fr_type == rs_machine_dependent
12151 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12152 == BRANCH_PREFIX))
12153 {
12154 if (left_size)
12155 {
12156 int max = next_fragP->tc_frag_data.max_bytes;
12157 if (max)
12158 {
12159 int size;
12160 if (max > left_size)
12161 size = left_size;
12162 else
12163 size = max;
12164 left_size -= size;
12165 next_fragP->tc_frag_data.length = size;
12166 }
12167 }
12168 else
12169 next_fragP->tc_frag_data.length = 0;
12170 }
12171
12172 return (fragP->tc_frag_data.length
12173 - fragP->tc_frag_data.last_length);
12174 }
12175 return relax_frag (segment, fragP, stretch);
12176}
12177
ee7fcc42
AM
12178/* md_estimate_size_before_relax()
12179
12180 Called just before relax() for rs_machine_dependent frags. The x86
12181 assembler uses these frags to handle variable size jump
12182 instructions.
12183
12184 Any symbol that is now undefined will not become defined.
12185 Return the correct fr_subtype in the frag.
12186 Return the initial "guess for variable size of frag" to caller.
12187 The guess is actually the growth beyond the fixed part. Whatever
12188 we do to grow the fixed or variable part contributes to our
12189 returned value. */
12190
252b5132 12191int
7016a5d5 12192md_estimate_size_before_relax (fragS *fragP, segT segment)
252b5132 12193{
e379e5f3
L
12194 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12195 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX
12196 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
12197 {
12198 i386_classify_machine_dependent_frag (fragP);
12199 return fragP->tc_frag_data.length;
12200 }
12201
252b5132 12202 /* We've already got fragP->fr_subtype right; all we have to do is
b98ef147
AM
12203 check for un-relaxable symbols. On an ELF system, we can't relax
12204 an externally visible symbol, because it may be overridden by a
12205 shared library. */
12206 if (S_GET_SEGMENT (fragP->fr_symbol) != segment
6d249963 12207#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12208 || (IS_ELF
8dcea932
L
12209 && !elf_symbol_resolved_in_segment_p (fragP->fr_symbol,
12210 fragP->fr_var))
fbeb56a4
DK
12211#endif
12212#if defined (OBJ_COFF) && defined (TE_PE)
7ab9ffdd 12213 || (OUTPUT_FLAVOR == bfd_target_coff_flavour
fbeb56a4 12214 && S_IS_WEAK (fragP->fr_symbol))
b98ef147
AM
12215#endif
12216 )
252b5132 12217 {
b98ef147
AM
12218 /* Symbol is undefined in this segment, or we need to keep a
12219 reloc so that weak symbols can be overridden. */
12220 int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
f86103b7 12221 enum bfd_reloc_code_real reloc_type;
ee7fcc42
AM
12222 unsigned char *opcode;
12223 int old_fr_fix;
eb19308f 12224 fixS *fixP = NULL;
f6af82bd 12225
ee7fcc42 12226 if (fragP->fr_var != NO_RELOC)
1e9cc1c2 12227 reloc_type = (enum bfd_reloc_code_real) fragP->fr_var;
b98ef147 12228 else if (size == 2)
f6af82bd 12229 reloc_type = BFD_RELOC_16_PCREL;
bd7ab16b 12230#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1ef3994a
JB
12231 else if (fragP->tc_frag_data.code64 && fragP->fr_offset == 0
12232 && need_plt32_p (fragP->fr_symbol))
bd7ab16b
L
12233 reloc_type = BFD_RELOC_X86_64_PLT32;
12234#endif
f6af82bd
AM
12235 else
12236 reloc_type = BFD_RELOC_32_PCREL;
252b5132 12237
ee7fcc42
AM
12238 old_fr_fix = fragP->fr_fix;
12239 opcode = (unsigned char *) fragP->fr_opcode;
12240
fddf5b5b 12241 switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
252b5132 12242 {
fddf5b5b
AM
12243 case UNCOND_JUMP:
12244 /* Make jmp (0xeb) a (d)word displacement jump. */
47926f60 12245 opcode[0] = 0xe9;
252b5132 12246 fragP->fr_fix += size;
eb19308f
JB
12247 fixP = fix_new (fragP, old_fr_fix, size,
12248 fragP->fr_symbol,
12249 fragP->fr_offset, 1,
12250 reloc_type);
252b5132
RH
12251 break;
12252
fddf5b5b 12253 case COND_JUMP86:
412167cb
AM
12254 if (size == 2
12255 && (!no_cond_jump_promotion || fragP->fr_var != NO_RELOC))
fddf5b5b
AM
12256 {
12257 /* Negate the condition, and branch past an
12258 unconditional jump. */
12259 opcode[0] ^= 1;
12260 opcode[1] = 3;
12261 /* Insert an unconditional jump. */
12262 opcode[2] = 0xe9;
12263 /* We added two extra opcode bytes, and have a two byte
12264 offset. */
12265 fragP->fr_fix += 2 + 2;
062cd5e7
AS
12266 fix_new (fragP, old_fr_fix + 2, 2,
12267 fragP->fr_symbol,
12268 fragP->fr_offset, 1,
12269 reloc_type);
fddf5b5b
AM
12270 break;
12271 }
12272 /* Fall through. */
12273
12274 case COND_JUMP:
412167cb
AM
12275 if (no_cond_jump_promotion && fragP->fr_var == NO_RELOC)
12276 {
12277 fragP->fr_fix += 1;
3e02c1cc
AM
12278 fixP = fix_new (fragP, old_fr_fix, 1,
12279 fragP->fr_symbol,
12280 fragP->fr_offset, 1,
12281 BFD_RELOC_8_PCREL);
12282 fixP->fx_signed = 1;
412167cb
AM
12283 break;
12284 }
93c2a809 12285
24eab124 12286 /* This changes the byte-displacement jump 0x7N
fddf5b5b 12287 to the (d)word-displacement jump 0x0f,0x8N. */
252b5132 12288 opcode[1] = opcode[0] + 0x10;
f6af82bd 12289 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
47926f60
KH
12290 /* We've added an opcode byte. */
12291 fragP->fr_fix += 1 + size;
eb19308f
JB
12292 fixP = fix_new (fragP, old_fr_fix + 1, size,
12293 fragP->fr_symbol,
12294 fragP->fr_offset, 1,
12295 reloc_type);
252b5132 12296 break;
fddf5b5b
AM
12297
12298 default:
12299 BAD_CASE (fragP->fr_subtype);
12300 break;
252b5132 12301 }
eb19308f
JB
12302
12303 /* All jumps handled here are signed, but don't unconditionally use a
12304 signed limit check for 32 and 16 bit jumps as we want to allow wrap
12305 around at 4G (outside of 64-bit mode) and 64k. */
12306 if (size == 4 && flag_code == CODE_64BIT)
12307 fixP->fx_signed = 1;
12308
252b5132 12309 frag_wane (fragP);
ee7fcc42 12310 return fragP->fr_fix - old_fr_fix;
252b5132 12311 }
93c2a809 12312
93c2a809
AM
12313 /* Guess size depending on current relax state. Initially the relax
12314 state will correspond to a short jump and we return 1, because
12315 the variable part of the frag (the branch offset) is one byte
12316 long. However, we can relax a section more than once and in that
12317 case we must either set fr_subtype back to the unrelaxed state,
12318 or return the value for the appropriate branch. */
12319 return md_relax_table[fragP->fr_subtype].rlx_length;
ee7fcc42
AM
12320}
12321
47926f60
KH
12322/* Called after relax() is finished.
12323
12324 In: Address of frag.
12325 fr_type == rs_machine_dependent.
12326 fr_subtype is what the address relaxed to.
12327
12328 Out: Any fixSs and constants are set up.
12329 Caller will turn frag into a ".space 0". */
12330
252b5132 12331void
7016a5d5
TG
12332md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec ATTRIBUTE_UNUSED,
12333 fragS *fragP)
252b5132 12334{
29b0f896 12335 unsigned char *opcode;
252b5132 12336 unsigned char *where_to_put_displacement = NULL;
847f7ad4
AM
12337 offsetT target_address;
12338 offsetT opcode_address;
252b5132 12339 unsigned int extension = 0;
847f7ad4 12340 offsetT displacement_from_opcode_start;
252b5132 12341
e379e5f3
L
12342 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12343 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING
12344 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12345 {
12346 /* Generate nop padding. */
12347 unsigned int size = fragP->tc_frag_data.length;
12348 if (size)
12349 {
12350 if (size > fragP->tc_frag_data.max_bytes)
12351 abort ();
12352
12353 if (flag_debug)
12354 {
12355 const char *msg;
12356 const char *branch = "branch";
12357 const char *prefix = "";
12358 fragS *padding_fragP;
12359 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
12360 == BRANCH_PREFIX)
12361 {
12362 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
12363 switch (fragP->tc_frag_data.default_prefix)
12364 {
12365 default:
12366 abort ();
12367 break;
12368 case CS_PREFIX_OPCODE:
12369 prefix = " cs";
12370 break;
12371 case DS_PREFIX_OPCODE:
12372 prefix = " ds";
12373 break;
12374 case ES_PREFIX_OPCODE:
12375 prefix = " es";
12376 break;
12377 case FS_PREFIX_OPCODE:
12378 prefix = " fs";
12379 break;
12380 case GS_PREFIX_OPCODE:
12381 prefix = " gs";
12382 break;
12383 case SS_PREFIX_OPCODE:
12384 prefix = " ss";
12385 break;
12386 }
12387 if (padding_fragP)
12388 msg = _("%s:%u: add %d%s at 0x%llx to align "
12389 "%s within %d-byte boundary\n");
12390 else
12391 msg = _("%s:%u: add additional %d%s at 0x%llx to "
12392 "align %s within %d-byte boundary\n");
12393 }
12394 else
12395 {
12396 padding_fragP = fragP;
12397 msg = _("%s:%u: add %d%s-byte nop at 0x%llx to align "
12398 "%s within %d-byte boundary\n");
12399 }
12400
12401 if (padding_fragP)
12402 switch (padding_fragP->tc_frag_data.branch_type)
12403 {
12404 case align_branch_jcc:
12405 branch = "jcc";
12406 break;
12407 case align_branch_fused:
12408 branch = "fused jcc";
12409 break;
12410 case align_branch_jmp:
12411 branch = "jmp";
12412 break;
12413 case align_branch_call:
12414 branch = "call";
12415 break;
12416 case align_branch_indirect:
12417 branch = "indiret branch";
12418 break;
12419 case align_branch_ret:
12420 branch = "ret";
12421 break;
12422 default:
12423 break;
12424 }
12425
12426 fprintf (stdout, msg,
12427 fragP->fr_file, fragP->fr_line, size, prefix,
12428 (long long) fragP->fr_address, branch,
12429 1 << align_branch_power);
12430 }
12431 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12432 memset (fragP->fr_opcode,
12433 fragP->tc_frag_data.default_prefix, size);
12434 else
12435 i386_generate_nops (fragP, (char *) fragP->fr_opcode,
12436 size, 0);
12437 fragP->fr_fix += size;
12438 }
12439 return;
12440 }
12441
252b5132
RH
12442 opcode = (unsigned char *) fragP->fr_opcode;
12443
47926f60 12444 /* Address we want to reach in file space. */
252b5132 12445 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
252b5132 12446
47926f60 12447 /* Address opcode resides at in file space. */
252b5132
RH
12448 opcode_address = fragP->fr_address + fragP->fr_fix;
12449
47926f60 12450 /* Displacement from opcode start to fill into instruction. */
252b5132
RH
12451 displacement_from_opcode_start = target_address - opcode_address;
12452
fddf5b5b 12453 if ((fragP->fr_subtype & BIG) == 0)
252b5132 12454 {
47926f60
KH
12455 /* Don't have to change opcode. */
12456 extension = 1; /* 1 opcode + 1 displacement */
252b5132 12457 where_to_put_displacement = &opcode[1];
fddf5b5b
AM
12458 }
12459 else
12460 {
12461 if (no_cond_jump_promotion
12462 && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
4eed87de
AM
12463 as_warn_where (fragP->fr_file, fragP->fr_line,
12464 _("long jump required"));
252b5132 12465
fddf5b5b
AM
12466 switch (fragP->fr_subtype)
12467 {
12468 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
12469 extension = 4; /* 1 opcode + 4 displacement */
12470 opcode[0] = 0xe9;
12471 where_to_put_displacement = &opcode[1];
12472 break;
252b5132 12473
fddf5b5b
AM
12474 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
12475 extension = 2; /* 1 opcode + 2 displacement */
12476 opcode[0] = 0xe9;
12477 where_to_put_displacement = &opcode[1];
12478 break;
252b5132 12479
fddf5b5b
AM
12480 case ENCODE_RELAX_STATE (COND_JUMP, BIG):
12481 case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
12482 extension = 5; /* 2 opcode + 4 displacement */
12483 opcode[1] = opcode[0] + 0x10;
12484 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12485 where_to_put_displacement = &opcode[2];
12486 break;
252b5132 12487
fddf5b5b
AM
12488 case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
12489 extension = 3; /* 2 opcode + 2 displacement */
12490 opcode[1] = opcode[0] + 0x10;
12491 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12492 where_to_put_displacement = &opcode[2];
12493 break;
252b5132 12494
fddf5b5b
AM
12495 case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
12496 extension = 4;
12497 opcode[0] ^= 1;
12498 opcode[1] = 3;
12499 opcode[2] = 0xe9;
12500 where_to_put_displacement = &opcode[3];
12501 break;
12502
12503 default:
12504 BAD_CASE (fragP->fr_subtype);
12505 break;
12506 }
252b5132 12507 }
fddf5b5b 12508
7b81dfbb
AJ
12509 /* If size if less then four we are sure that the operand fits,
12510 but if it's 4, then it could be that the displacement is larger
12511 then -/+ 2GB. */
12512 if (DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype) == 4
12513 && object_64bit
12514 && ((addressT) (displacement_from_opcode_start - extension
4eed87de
AM
12515 + ((addressT) 1 << 31))
12516 > (((addressT) 2 << 31) - 1)))
7b81dfbb
AJ
12517 {
12518 as_bad_where (fragP->fr_file, fragP->fr_line,
12519 _("jump target out of range"));
12520 /* Make us emit 0. */
12521 displacement_from_opcode_start = extension;
12522 }
47926f60 12523 /* Now put displacement after opcode. */
252b5132
RH
12524 md_number_to_chars ((char *) where_to_put_displacement,
12525 (valueT) (displacement_from_opcode_start - extension),
fddf5b5b 12526 DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
252b5132
RH
12527 fragP->fr_fix += extension;
12528}
12529\f
7016a5d5 12530/* Apply a fixup (fixP) to segment data, once it has been determined
252b5132
RH
12531 by our caller that we have all the info we need to fix it up.
12532
7016a5d5
TG
12533 Parameter valP is the pointer to the value of the bits.
12534
252b5132
RH
12535 On the 386, immediates, displacements, and data pointers are all in
12536 the same (little-endian) format, so we don't need to care about which
12537 we are handling. */
12538
94f592af 12539void
7016a5d5 12540md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 12541{
94f592af 12542 char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
c6682705 12543 valueT value = *valP;
252b5132 12544
f86103b7 12545#if !defined (TE_Mach)
93382f6d
AM
12546 if (fixP->fx_pcrel)
12547 {
12548 switch (fixP->fx_r_type)
12549 {
5865bb77
ILT
12550 default:
12551 break;
12552
d6ab8113
JB
12553 case BFD_RELOC_64:
12554 fixP->fx_r_type = BFD_RELOC_64_PCREL;
12555 break;
93382f6d 12556 case BFD_RELOC_32:
ae8887b5 12557 case BFD_RELOC_X86_64_32S:
93382f6d
AM
12558 fixP->fx_r_type = BFD_RELOC_32_PCREL;
12559 break;
12560 case BFD_RELOC_16:
12561 fixP->fx_r_type = BFD_RELOC_16_PCREL;
12562 break;
12563 case BFD_RELOC_8:
12564 fixP->fx_r_type = BFD_RELOC_8_PCREL;
12565 break;
12566 }
12567 }
252b5132 12568
a161fe53 12569 if (fixP->fx_addsy != NULL
31312f95 12570 && (fixP->fx_r_type == BFD_RELOC_32_PCREL
d6ab8113 12571 || fixP->fx_r_type == BFD_RELOC_64_PCREL
31312f95 12572 || fixP->fx_r_type == BFD_RELOC_16_PCREL
d258b828 12573 || fixP->fx_r_type == BFD_RELOC_8_PCREL)
31312f95 12574 && !use_rela_relocations)
252b5132 12575 {
31312f95
AM
12576 /* This is a hack. There should be a better way to handle this.
12577 This covers for the fact that bfd_install_relocation will
12578 subtract the current location (for partial_inplace, PC relative
12579 relocations); see more below. */
252b5132 12580#ifndef OBJ_AOUT
718ddfc0 12581 if (IS_ELF
252b5132
RH
12582#ifdef TE_PE
12583 || OUTPUT_FLAVOR == bfd_target_coff_flavour
12584#endif
12585 )
12586 value += fixP->fx_where + fixP->fx_frag->fr_address;
12587#endif
12588#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12589 if (IS_ELF)
252b5132 12590 {
6539b54b 12591 segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
2f66722d 12592
6539b54b 12593 if ((sym_seg == seg
2f66722d 12594 || (symbol_section_p (fixP->fx_addsy)
6539b54b 12595 && sym_seg != absolute_section))
af65af87 12596 && !generic_force_reloc (fixP))
2f66722d
AM
12597 {
12598 /* Yes, we add the values in twice. This is because
6539b54b
AM
12599 bfd_install_relocation subtracts them out again. I think
12600 bfd_install_relocation is broken, but I don't dare change
2f66722d
AM
12601 it. FIXME. */
12602 value += fixP->fx_where + fixP->fx_frag->fr_address;
12603 }
252b5132
RH
12604 }
12605#endif
12606#if defined (OBJ_COFF) && defined (TE_PE)
977cdf5a
NC
12607 /* For some reason, the PE format does not store a
12608 section address offset for a PC relative symbol. */
12609 if (S_GET_SEGMENT (fixP->fx_addsy) != seg
7be1c489 12610 || S_IS_WEAK (fixP->fx_addsy))
252b5132
RH
12611 value += md_pcrel_from (fixP);
12612#endif
12613 }
fbeb56a4 12614#if defined (OBJ_COFF) && defined (TE_PE)
f01c1a09
NC
12615 if (fixP->fx_addsy != NULL
12616 && S_IS_WEAK (fixP->fx_addsy)
12617 /* PR 16858: Do not modify weak function references. */
12618 && ! fixP->fx_pcrel)
fbeb56a4 12619 {
296a8689
NC
12620#if !defined (TE_PEP)
12621 /* For x86 PE weak function symbols are neither PC-relative
12622 nor do they set S_IS_FUNCTION. So the only reliable way
12623 to detect them is to check the flags of their containing
12624 section. */
12625 if (S_GET_SEGMENT (fixP->fx_addsy) != NULL
12626 && S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_CODE)
12627 ;
12628 else
12629#endif
fbeb56a4
DK
12630 value -= S_GET_VALUE (fixP->fx_addsy);
12631 }
12632#endif
252b5132
RH
12633
12634 /* Fix a few things - the dynamic linker expects certain values here,
0234cb7c 12635 and we must not disappoint it. */
252b5132 12636#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12637 if (IS_ELF && fixP->fx_addsy)
47926f60
KH
12638 switch (fixP->fx_r_type)
12639 {
12640 case BFD_RELOC_386_PLT32:
3e73aa7c 12641 case BFD_RELOC_X86_64_PLT32:
b9519cfe
L
12642 /* Make the jump instruction point to the address of the operand.
12643 At runtime we merely add the offset to the actual PLT entry.
12644 NB: Subtract the offset size only for jump instructions. */
12645 if (fixP->fx_pcrel)
12646 value = -4;
47926f60 12647 break;
31312f95 12648
13ae64f3
JJ
12649 case BFD_RELOC_386_TLS_GD:
12650 case BFD_RELOC_386_TLS_LDM:
13ae64f3 12651 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
12652 case BFD_RELOC_386_TLS_IE:
12653 case BFD_RELOC_386_TLS_GOTIE:
67a4f2b7 12654 case BFD_RELOC_386_TLS_GOTDESC:
bffbf940
JJ
12655 case BFD_RELOC_X86_64_TLSGD:
12656 case BFD_RELOC_X86_64_TLSLD:
12657 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7 12658 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
00f7efb6
JJ
12659 value = 0; /* Fully resolved at runtime. No addend. */
12660 /* Fallthrough */
12661 case BFD_RELOC_386_TLS_LE:
12662 case BFD_RELOC_386_TLS_LDO_32:
12663 case BFD_RELOC_386_TLS_LE_32:
12664 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 12665 case BFD_RELOC_X86_64_DTPOFF64:
00f7efb6 12666 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113 12667 case BFD_RELOC_X86_64_TPOFF64:
00f7efb6
JJ
12668 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12669 break;
12670
67a4f2b7
AO
12671 case BFD_RELOC_386_TLS_DESC_CALL:
12672 case BFD_RELOC_X86_64_TLSDESC_CALL:
12673 value = 0; /* Fully resolved at runtime. No addend. */
12674 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12675 fixP->fx_done = 0;
12676 return;
12677
47926f60
KH
12678 case BFD_RELOC_VTABLE_INHERIT:
12679 case BFD_RELOC_VTABLE_ENTRY:
12680 fixP->fx_done = 0;
94f592af 12681 return;
47926f60
KH
12682
12683 default:
12684 break;
12685 }
12686#endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) */
a442cac5
JB
12687
12688 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
12689 if (!object_64bit)
12690 value = extend_to_32bit_address (value);
12691
c6682705 12692 *valP = value;
f86103b7 12693#endif /* !defined (TE_Mach) */
3e73aa7c 12694
3e73aa7c 12695 /* Are we finished with this relocation now? */
c6682705 12696 if (fixP->fx_addsy == NULL)
b8188555
JB
12697 {
12698 fixP->fx_done = 1;
12699 switch (fixP->fx_r_type)
12700 {
12701 case BFD_RELOC_X86_64_32S:
12702 fixP->fx_signed = 1;
12703 break;
12704
12705 default:
12706 break;
12707 }
12708 }
fbeb56a4
DK
12709#if defined (OBJ_COFF) && defined (TE_PE)
12710 else if (fixP->fx_addsy != NULL && S_IS_WEAK (fixP->fx_addsy))
12711 {
12712 fixP->fx_done = 0;
12713 /* Remember value for tc_gen_reloc. */
12714 fixP->fx_addnumber = value;
12715 /* Clear out the frag for now. */
12716 value = 0;
12717 }
12718#endif
3e73aa7c
JH
12719 else if (use_rela_relocations)
12720 {
46fb6d5a
JB
12721 if (!disallow_64bit_reloc || fixP->fx_r_type == NO_RELOC)
12722 fixP->fx_no_overflow = 1;
062cd5e7
AS
12723 /* Remember value for tc_gen_reloc. */
12724 fixP->fx_addnumber = value;
3e73aa7c
JH
12725 value = 0;
12726 }
f86103b7 12727
94f592af 12728 md_number_to_chars (p, value, fixP->fx_size);
252b5132 12729}
252b5132 12730\f
6d4af3c2 12731const char *
499ac353 12732md_atof (int type, char *litP, int *sizeP)
252b5132 12733{
499ac353
NC
12734 /* This outputs the LITTLENUMs in REVERSE order;
12735 in accord with the bigendian 386. */
5b7c81bd 12736 return ieee_md_atof (type, litP, sizeP, false);
252b5132
RH
12737}
12738\f
2d545b82 12739static char output_invalid_buf[sizeof (unsigned char) * 2 + 6];
252b5132 12740
252b5132 12741static char *
e3bb37b5 12742output_invalid (int c)
252b5132 12743{
3882b010 12744 if (ISPRINT (c))
f9f21a03
L
12745 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
12746 "'%c'", c);
252b5132 12747 else
f9f21a03 12748 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
2d545b82 12749 "(0x%x)", (unsigned char) c);
252b5132
RH
12750 return output_invalid_buf;
12751}
12752
8a6fb3f9
JB
12753/* Verify that @r can be used in the current context. */
12754
5b7c81bd 12755static bool check_register (const reg_entry *r)
8a6fb3f9
JB
12756{
12757 if (allow_pseudo_reg)
5b7c81bd 12758 return true;
8a6fb3f9
JB
12759
12760 if (operand_type_all_zero (&r->reg_type))
5b7c81bd 12761 return false;
8a6fb3f9
JB
12762
12763 if ((r->reg_type.bitfield.dword
12764 || (r->reg_type.bitfield.class == SReg && r->reg_num > 3)
12765 || r->reg_type.bitfield.class == RegCR
22e00a3f 12766 || r->reg_type.bitfield.class == RegDR)
8a6fb3f9 12767 && !cpu_arch_flags.bitfield.cpui386)
5b7c81bd 12768 return false;
8a6fb3f9 12769
22e00a3f
JB
12770 if (r->reg_type.bitfield.class == RegTR
12771 && (flag_code == CODE_64BIT
12772 || !cpu_arch_flags.bitfield.cpui386
12773 || cpu_arch_isa_flags.bitfield.cpui586
12774 || cpu_arch_isa_flags.bitfield.cpui686))
5b7c81bd 12775 return false;
22e00a3f 12776
8a6fb3f9 12777 if (r->reg_type.bitfield.class == RegMMX && !cpu_arch_flags.bitfield.cpummx)
5b7c81bd 12778 return false;
8a6fb3f9
JB
12779
12780 if (!cpu_arch_flags.bitfield.cpuavx512f)
12781 {
12782 if (r->reg_type.bitfield.zmmword
12783 || r->reg_type.bitfield.class == RegMask)
5b7c81bd 12784 return false;
8a6fb3f9
JB
12785
12786 if (!cpu_arch_flags.bitfield.cpuavx)
12787 {
12788 if (r->reg_type.bitfield.ymmword)
5b7c81bd 12789 return false;
8a6fb3f9
JB
12790
12791 if (!cpu_arch_flags.bitfield.cpusse && r->reg_type.bitfield.xmmword)
5b7c81bd 12792 return false;
8a6fb3f9
JB
12793 }
12794 }
12795
260cd341
LC
12796 if (r->reg_type.bitfield.tmmword
12797 && (!cpu_arch_flags.bitfield.cpuamx_tile
12798 || flag_code != CODE_64BIT))
5b7c81bd 12799 return false;
260cd341 12800
8a6fb3f9 12801 if (r->reg_type.bitfield.class == RegBND && !cpu_arch_flags.bitfield.cpumpx)
5b7c81bd 12802 return false;
8a6fb3f9
JB
12803
12804 /* Don't allow fake index register unless allow_index_reg isn't 0. */
12805 if (!allow_index_reg && r->reg_num == RegIZ)
5b7c81bd 12806 return false;
8a6fb3f9
JB
12807
12808 /* Upper 16 vector registers are only available with VREX in 64bit
12809 mode, and require EVEX encoding. */
12810 if (r->reg_flags & RegVRex)
12811 {
12812 if (!cpu_arch_flags.bitfield.cpuavx512f
12813 || flag_code != CODE_64BIT)
5b7c81bd 12814 return false;
8a6fb3f9 12815
da4977e0
JB
12816 if (i.vec_encoding == vex_encoding_default)
12817 i.vec_encoding = vex_encoding_evex;
12818 else if (i.vec_encoding != vex_encoding_evex)
12819 i.vec_encoding = vex_encoding_error;
8a6fb3f9
JB
12820 }
12821
12822 if (((r->reg_flags & (RegRex64 | RegRex)) || r->reg_type.bitfield.qword)
12823 && (!cpu_arch_flags.bitfield.cpulm || r->reg_type.bitfield.class != RegCR)
12824 && flag_code != CODE_64BIT)
5b7c81bd 12825 return false;
8a6fb3f9
JB
12826
12827 if (r->reg_type.bitfield.class == SReg && r->reg_num == RegFlat
12828 && !intel_syntax)
5b7c81bd 12829 return false;
8a6fb3f9 12830
5b7c81bd 12831 return true;
8a6fb3f9
JB
12832}
12833
af6bdddf 12834/* REG_STRING starts *before* REGISTER_PREFIX. */
252b5132
RH
12835
12836static const reg_entry *
4d1bb795 12837parse_real_register (char *reg_string, char **end_op)
252b5132 12838{
af6bdddf
AM
12839 char *s = reg_string;
12840 char *p;
252b5132
RH
12841 char reg_name_given[MAX_REG_NAME_SIZE + 1];
12842 const reg_entry *r;
12843
12844 /* Skip possible REGISTER_PREFIX and possible whitespace. */
12845 if (*s == REGISTER_PREFIX)
12846 ++s;
12847
12848 if (is_space_char (*s))
12849 ++s;
12850
12851 p = reg_name_given;
af6bdddf 12852 while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
252b5132
RH
12853 {
12854 if (p >= reg_name_given + MAX_REG_NAME_SIZE)
af6bdddf
AM
12855 return (const reg_entry *) NULL;
12856 s++;
252b5132
RH
12857 }
12858
6588847e
DN
12859 /* For naked regs, make sure that we are not dealing with an identifier.
12860 This prevents confusing an identifier like `eax_var' with register
12861 `eax'. */
12862 if (allow_naked_reg && identifier_chars[(unsigned char) *s])
12863 return (const reg_entry *) NULL;
12864
af6bdddf 12865 *end_op = s;
252b5132 12866
629310ab 12867 r = (const reg_entry *) str_hash_find (reg_hash, reg_name_given);
252b5132 12868
5f47d35b 12869 /* Handle floating point regs, allowing spaces in the (i) part. */
6288d05f 12870 if (r == reg_st0)
5f47d35b 12871 {
0e0eea78
JB
12872 if (!cpu_arch_flags.bitfield.cpu8087
12873 && !cpu_arch_flags.bitfield.cpu287
af32b722
JB
12874 && !cpu_arch_flags.bitfield.cpu387
12875 && !allow_pseudo_reg)
0e0eea78
JB
12876 return (const reg_entry *) NULL;
12877
5f47d35b
AM
12878 if (is_space_char (*s))
12879 ++s;
12880 if (*s == '(')
12881 {
af6bdddf 12882 ++s;
5f47d35b
AM
12883 if (is_space_char (*s))
12884 ++s;
12885 if (*s >= '0' && *s <= '7')
12886 {
db557034 12887 int fpr = *s - '0';
af6bdddf 12888 ++s;
5f47d35b
AM
12889 if (is_space_char (*s))
12890 ++s;
12891 if (*s == ')')
12892 {
12893 *end_op = s + 1;
6288d05f 12894 know (r[fpr].reg_num == fpr);
db557034 12895 return r + fpr;
5f47d35b 12896 }
5f47d35b 12897 }
47926f60 12898 /* We have "%st(" then garbage. */
5f47d35b
AM
12899 return (const reg_entry *) NULL;
12900 }
12901 }
12902
8a6fb3f9 12903 return r && check_register (r) ? r : NULL;
252b5132 12904}
4d1bb795
JB
12905
12906/* REG_STRING starts *before* REGISTER_PREFIX. */
12907
12908static const reg_entry *
12909parse_register (char *reg_string, char **end_op)
12910{
12911 const reg_entry *r;
12912
12913 if (*reg_string == REGISTER_PREFIX || allow_naked_reg)
12914 r = parse_real_register (reg_string, end_op);
12915 else
12916 r = NULL;
12917 if (!r)
12918 {
12919 char *save = input_line_pointer;
12920 char c;
12921 symbolS *symbolP;
12922
12923 input_line_pointer = reg_string;
d02603dc 12924 c = get_symbol_name (&reg_string);
4d1bb795 12925 symbolP = symbol_find (reg_string);
64d23078
JB
12926 while (symbolP && S_GET_SEGMENT (symbolP) != reg_section)
12927 {
12928 const expressionS *e = symbol_get_value_expression(symbolP);
12929
12930 if (e->X_op != O_symbol || e->X_add_number)
12931 break;
12932 symbolP = e->X_add_symbol;
12933 }
4d1bb795
JB
12934 if (symbolP && S_GET_SEGMENT (symbolP) == reg_section)
12935 {
12936 const expressionS *e = symbol_get_value_expression (symbolP);
12937
3b55a1d0
JB
12938 know (e->X_op == O_register);
12939 know (e->X_add_number >= 0
12940 && (valueT) e->X_add_number < i386_regtab_size);
12941 r = i386_regtab + e->X_add_number;
12942 if (!check_register (r))
8a6fb3f9 12943 {
3b55a1d0
JB
12944 as_bad (_("register '%s%s' cannot be used here"),
12945 register_prefix, r->reg_name);
12946 r = &bad_reg;
8a6fb3f9 12947 }
3b55a1d0 12948 *end_op = input_line_pointer;
4d1bb795
JB
12949 }
12950 *input_line_pointer = c;
12951 input_line_pointer = save;
12952 }
12953 return r;
12954}
12955
12956int
12957i386_parse_name (char *name, expressionS *e, char *nextcharP)
12958{
4faaa10f 12959 const reg_entry *r = NULL;
4d1bb795
JB
12960 char *end = input_line_pointer;
12961
12962 *end = *nextcharP;
4faaa10f
JB
12963 if (*name == REGISTER_PREFIX || allow_naked_reg)
12964 r = parse_real_register (name, &input_line_pointer);
4d1bb795
JB
12965 if (r && end <= input_line_pointer)
12966 {
12967 *nextcharP = *input_line_pointer;
12968 *input_line_pointer = 0;
8a6fb3f9
JB
12969 if (r != &bad_reg)
12970 {
12971 e->X_op = O_register;
12972 e->X_add_number = r - i386_regtab;
12973 }
12974 else
12975 e->X_op = O_illegal;
4d1bb795
JB
12976 return 1;
12977 }
12978 input_line_pointer = end;
12979 *end = 0;
ee86248c 12980 return intel_syntax ? i386_intel_parse_name (name, e) : 0;
4d1bb795
JB
12981}
12982
12983void
12984md_operand (expressionS *e)
12985{
ee86248c
JB
12986 char *end;
12987 const reg_entry *r;
4d1bb795 12988
ee86248c
JB
12989 switch (*input_line_pointer)
12990 {
12991 case REGISTER_PREFIX:
12992 r = parse_real_register (input_line_pointer, &end);
4d1bb795
JB
12993 if (r)
12994 {
12995 e->X_op = O_register;
12996 e->X_add_number = r - i386_regtab;
12997 input_line_pointer = end;
12998 }
ee86248c
JB
12999 break;
13000
13001 case '[':
9c2799c2 13002 gas_assert (intel_syntax);
ee86248c
JB
13003 end = input_line_pointer++;
13004 expression (e);
13005 if (*input_line_pointer == ']')
13006 {
13007 ++input_line_pointer;
13008 e->X_op_symbol = make_expr_symbol (e);
13009 e->X_add_symbol = NULL;
13010 e->X_add_number = 0;
13011 e->X_op = O_index;
13012 }
13013 else
13014 {
13015 e->X_op = O_absent;
13016 input_line_pointer = end;
13017 }
13018 break;
4d1bb795
JB
13019 }
13020}
13021
252b5132 13022\f
4cc782b5 13023#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
b6f8c7c4 13024const char *md_shortopts = "kVQ:sqnO::";
252b5132 13025#else
b6f8c7c4 13026const char *md_shortopts = "qnO::";
252b5132 13027#endif
6e0b89ee 13028
3e73aa7c 13029#define OPTION_32 (OPTION_MD_BASE + 0)
b3b91714
AM
13030#define OPTION_64 (OPTION_MD_BASE + 1)
13031#define OPTION_DIVIDE (OPTION_MD_BASE + 2)
9103f4f4
L
13032#define OPTION_MARCH (OPTION_MD_BASE + 3)
13033#define OPTION_MTUNE (OPTION_MD_BASE + 4)
1efbbeb4
L
13034#define OPTION_MMNEMONIC (OPTION_MD_BASE + 5)
13035#define OPTION_MSYNTAX (OPTION_MD_BASE + 6)
13036#define OPTION_MINDEX_REG (OPTION_MD_BASE + 7)
13037#define OPTION_MNAKED_REG (OPTION_MD_BASE + 8)
bd5dea88 13038#define OPTION_MRELAX_RELOCATIONS (OPTION_MD_BASE + 9)
c0f3af97 13039#define OPTION_MSSE2AVX (OPTION_MD_BASE + 10)
daf50ae7 13040#define OPTION_MSSE_CHECK (OPTION_MD_BASE + 11)
7bab8ab5
JB
13041#define OPTION_MOPERAND_CHECK (OPTION_MD_BASE + 12)
13042#define OPTION_MAVXSCALAR (OPTION_MD_BASE + 13)
13043#define OPTION_X32 (OPTION_MD_BASE + 14)
7e8b059b 13044#define OPTION_MADD_BND_PREFIX (OPTION_MD_BASE + 15)
43234a1e
L
13045#define OPTION_MEVEXLIG (OPTION_MD_BASE + 16)
13046#define OPTION_MEVEXWIG (OPTION_MD_BASE + 17)
167ad85b 13047#define OPTION_MBIG_OBJ (OPTION_MD_BASE + 18)
d1982f93 13048#define OPTION_MOMIT_LOCK_PREFIX (OPTION_MD_BASE + 19)
d3d3c6db 13049#define OPTION_MEVEXRCIG (OPTION_MD_BASE + 20)
8dcea932 13050#define OPTION_MSHARED (OPTION_MD_BASE + 21)
5db04b09
L
13051#define OPTION_MAMD64 (OPTION_MD_BASE + 22)
13052#define OPTION_MINTEL64 (OPTION_MD_BASE + 23)
e4e00185 13053#define OPTION_MFENCE_AS_LOCK_ADD (OPTION_MD_BASE + 24)
b4a3a7b4 13054#define OPTION_X86_USED_NOTE (OPTION_MD_BASE + 25)
03751133 13055#define OPTION_MVEXWIG (OPTION_MD_BASE + 26)
e379e5f3
L
13056#define OPTION_MALIGN_BRANCH_BOUNDARY (OPTION_MD_BASE + 27)
13057#define OPTION_MALIGN_BRANCH_PREFIX_SIZE (OPTION_MD_BASE + 28)
13058#define OPTION_MALIGN_BRANCH (OPTION_MD_BASE + 29)
76cf450b 13059#define OPTION_MBRANCHES_WITH_32B_BOUNDARIES (OPTION_MD_BASE + 30)
ae531041
L
13060#define OPTION_MLFENCE_AFTER_LOAD (OPTION_MD_BASE + 31)
13061#define OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH (OPTION_MD_BASE + 32)
13062#define OPTION_MLFENCE_BEFORE_RET (OPTION_MD_BASE + 33)
c8480b58 13063#define OPTION_MUSE_UNALIGNED_VECTOR_MOVE (OPTION_MD_BASE + 34)
b3b91714 13064
99ad8390
NC
13065struct option md_longopts[] =
13066{
3e73aa7c 13067 {"32", no_argument, NULL, OPTION_32},
321098a5 13068#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 13069 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c 13070 {"64", no_argument, NULL, OPTION_64},
351f65ca
L
13071#endif
13072#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 13073 {"x32", no_argument, NULL, OPTION_X32},
8dcea932 13074 {"mshared", no_argument, NULL, OPTION_MSHARED},
b4a3a7b4 13075 {"mx86-used-note", required_argument, NULL, OPTION_X86_USED_NOTE},
6e0b89ee 13076#endif
b3b91714 13077 {"divide", no_argument, NULL, OPTION_DIVIDE},
9103f4f4
L
13078 {"march", required_argument, NULL, OPTION_MARCH},
13079 {"mtune", required_argument, NULL, OPTION_MTUNE},
1efbbeb4
L
13080 {"mmnemonic", required_argument, NULL, OPTION_MMNEMONIC},
13081 {"msyntax", required_argument, NULL, OPTION_MSYNTAX},
13082 {"mindex-reg", no_argument, NULL, OPTION_MINDEX_REG},
13083 {"mnaked-reg", no_argument, NULL, OPTION_MNAKED_REG},
c0f3af97 13084 {"msse2avx", no_argument, NULL, OPTION_MSSE2AVX},
c8480b58 13085 {"muse-unaligned-vector-move", no_argument, NULL, OPTION_MUSE_UNALIGNED_VECTOR_MOVE},
daf50ae7 13086 {"msse-check", required_argument, NULL, OPTION_MSSE_CHECK},
7bab8ab5 13087 {"moperand-check", required_argument, NULL, OPTION_MOPERAND_CHECK},
539f890d 13088 {"mavxscalar", required_argument, NULL, OPTION_MAVXSCALAR},
03751133 13089 {"mvexwig", required_argument, NULL, OPTION_MVEXWIG},
7e8b059b 13090 {"madd-bnd-prefix", no_argument, NULL, OPTION_MADD_BND_PREFIX},
43234a1e
L
13091 {"mevexlig", required_argument, NULL, OPTION_MEVEXLIG},
13092 {"mevexwig", required_argument, NULL, OPTION_MEVEXWIG},
167ad85b
TG
13093# if defined (TE_PE) || defined (TE_PEP)
13094 {"mbig-obj", no_argument, NULL, OPTION_MBIG_OBJ},
13095#endif
d1982f93 13096 {"momit-lock-prefix", required_argument, NULL, OPTION_MOMIT_LOCK_PREFIX},
e4e00185 13097 {"mfence-as-lock-add", required_argument, NULL, OPTION_MFENCE_AS_LOCK_ADD},
0cb4071e 13098 {"mrelax-relocations", required_argument, NULL, OPTION_MRELAX_RELOCATIONS},
d3d3c6db 13099 {"mevexrcig", required_argument, NULL, OPTION_MEVEXRCIG},
e379e5f3
L
13100 {"malign-branch-boundary", required_argument, NULL, OPTION_MALIGN_BRANCH_BOUNDARY},
13101 {"malign-branch-prefix-size", required_argument, NULL, OPTION_MALIGN_BRANCH_PREFIX_SIZE},
13102 {"malign-branch", required_argument, NULL, OPTION_MALIGN_BRANCH},
76cf450b 13103 {"mbranches-within-32B-boundaries", no_argument, NULL, OPTION_MBRANCHES_WITH_32B_BOUNDARIES},
ae531041
L
13104 {"mlfence-after-load", required_argument, NULL, OPTION_MLFENCE_AFTER_LOAD},
13105 {"mlfence-before-indirect-branch", required_argument, NULL,
13106 OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH},
13107 {"mlfence-before-ret", required_argument, NULL, OPTION_MLFENCE_BEFORE_RET},
5db04b09
L
13108 {"mamd64", no_argument, NULL, OPTION_MAMD64},
13109 {"mintel64", no_argument, NULL, OPTION_MINTEL64},
252b5132
RH
13110 {NULL, no_argument, NULL, 0}
13111};
13112size_t md_longopts_size = sizeof (md_longopts);
13113
13114int
17b9d67d 13115md_parse_option (int c, const char *arg)
252b5132 13116{
91d6fa6a 13117 unsigned int j;
e379e5f3 13118 char *arch, *next, *saved, *type;
9103f4f4 13119
252b5132
RH
13120 switch (c)
13121 {
12b55ccc
L
13122 case 'n':
13123 optimize_align_code = 0;
13124 break;
13125
a38cf1db
AM
13126 case 'q':
13127 quiet_warnings = 1;
252b5132
RH
13128 break;
13129
13130#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
a38cf1db
AM
13131 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
13132 should be emitted or not. FIXME: Not implemented. */
13133 case 'Q':
d4693039
JB
13134 if ((arg[0] != 'y' && arg[0] != 'n') || arg[1])
13135 return 0;
252b5132
RH
13136 break;
13137
13138 /* -V: SVR4 argument to print version ID. */
13139 case 'V':
13140 print_version_id ();
13141 break;
13142
a38cf1db
AM
13143 /* -k: Ignore for FreeBSD compatibility. */
13144 case 'k':
252b5132 13145 break;
4cc782b5
ILT
13146
13147 case 's':
13148 /* -s: On i386 Solaris, this tells the native assembler to use
29b0f896 13149 .stab instead of .stab.excl. We always use .stab anyhow. */
4cc782b5 13150 break;
8dcea932
L
13151
13152 case OPTION_MSHARED:
13153 shared = 1;
13154 break;
b4a3a7b4
L
13155
13156 case OPTION_X86_USED_NOTE:
13157 if (strcasecmp (arg, "yes") == 0)
13158 x86_used_note = 1;
13159 else if (strcasecmp (arg, "no") == 0)
13160 x86_used_note = 0;
13161 else
13162 as_fatal (_("invalid -mx86-used-note= option: `%s'"), arg);
13163 break;
13164
13165
99ad8390 13166#endif
321098a5 13167#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 13168 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c
JH
13169 case OPTION_64:
13170 {
13171 const char **list, **l;
13172
3e73aa7c
JH
13173 list = bfd_target_list ();
13174 for (l = list; *l != NULL; l++)
08dedd66 13175 if (startswith (*l, "elf64-x86-64")
99ad8390
NC
13176 || strcmp (*l, "coff-x86-64") == 0
13177 || strcmp (*l, "pe-x86-64") == 0
d382c579
TG
13178 || strcmp (*l, "pei-x86-64") == 0
13179 || strcmp (*l, "mach-o-x86-64") == 0)
6e0b89ee
AM
13180 {
13181 default_arch = "x86_64";
13182 break;
13183 }
3e73aa7c 13184 if (*l == NULL)
2b5d6a91 13185 as_fatal (_("no compiled in support for x86_64"));
3e73aa7c
JH
13186 free (list);
13187 }
13188 break;
13189#endif
252b5132 13190
351f65ca 13191#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 13192 case OPTION_X32:
351f65ca
L
13193 if (IS_ELF)
13194 {
13195 const char **list, **l;
13196
13197 list = bfd_target_list ();
13198 for (l = list; *l != NULL; l++)
08dedd66 13199 if (startswith (*l, "elf32-x86-64"))
351f65ca
L
13200 {
13201 default_arch = "x86_64:32";
13202 break;
13203 }
13204 if (*l == NULL)
2b5d6a91 13205 as_fatal (_("no compiled in support for 32bit x86_64"));
351f65ca
L
13206 free (list);
13207 }
13208 else
13209 as_fatal (_("32bit x86_64 is only supported for ELF"));
13210 break;
13211#endif
13212
6e0b89ee
AM
13213 case OPTION_32:
13214 default_arch = "i386";
13215 break;
13216
b3b91714
AM
13217 case OPTION_DIVIDE:
13218#ifdef SVR4_COMMENT_CHARS
13219 {
13220 char *n, *t;
13221 const char *s;
13222
add39d23 13223 n = XNEWVEC (char, strlen (i386_comment_chars) + 1);
b3b91714
AM
13224 t = n;
13225 for (s = i386_comment_chars; *s != '\0'; s++)
13226 if (*s != '/')
13227 *t++ = *s;
13228 *t = '\0';
13229 i386_comment_chars = n;
13230 }
13231#endif
13232 break;
13233
9103f4f4 13234 case OPTION_MARCH:
293f5f65
L
13235 saved = xstrdup (arg);
13236 arch = saved;
13237 /* Allow -march=+nosse. */
13238 if (*arch == '+')
13239 arch++;
6305a203 13240 do
9103f4f4 13241 {
6305a203 13242 if (*arch == '.')
2b5d6a91 13243 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
13244 next = strchr (arch, '+');
13245 if (next)
13246 *next++ = '\0';
91d6fa6a 13247 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 13248 {
ae89daec
JB
13249 if (arch == saved && cpu_arch[j].type != PROCESSOR_NONE
13250 && strcmp (arch, cpu_arch[j].name) == 0)
ccc9c027 13251 {
6305a203 13252 /* Processor. */
ae89daec 13253 if (! cpu_arch[j].enable.bitfield.cpui386)
1ded5609
JB
13254 continue;
13255
91d6fa6a 13256 cpu_arch_name = cpu_arch[j].name;
d92c7521 13257 free (cpu_sub_arch_name);
6305a203 13258 cpu_sub_arch_name = NULL;
ae89daec 13259 cpu_arch_flags = cpu_arch[j].enable;
91d6fa6a 13260 cpu_arch_isa = cpu_arch[j].type;
ae89daec 13261 cpu_arch_isa_flags = cpu_arch[j].enable;
6305a203
L
13262 if (!cpu_arch_tune_set)
13263 {
13264 cpu_arch_tune = cpu_arch_isa;
13265 cpu_arch_tune_flags = cpu_arch_isa_flags;
13266 }
13267 break;
13268 }
ae89daec
JB
13269 else if (cpu_arch[j].type == PROCESSOR_NONE
13270 && strcmp (arch, cpu_arch[j].name) == 0
13271 && !cpu_flags_all_zero (&cpu_arch[j].enable))
6305a203 13272 {
33eaf5de 13273 /* ISA extension. */
6305a203 13274 i386_cpu_flags flags;
309d3373 13275
293f5f65 13276 flags = cpu_flags_or (cpu_arch_flags,
ae89daec 13277 cpu_arch[j].enable);
81486035 13278
5b64d091 13279 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
6305a203 13280 {
ae89daec 13281 extend_cpu_sub_arch_name (arch);
6305a203 13282 cpu_arch_flags = flags;
a586129e 13283 cpu_arch_isa_flags = flags;
6305a203 13284 }
0089dace
L
13285 else
13286 cpu_arch_isa_flags
13287 = cpu_flags_or (cpu_arch_isa_flags,
ae89daec 13288 cpu_arch[j].enable);
6305a203 13289 break;
ccc9c027 13290 }
9103f4f4 13291 }
6305a203 13292
ae89daec 13293 if (j >= ARRAY_SIZE (cpu_arch) && startswith (arch, "no"))
293f5f65 13294 {
33eaf5de 13295 /* Disable an ISA extension. */
ae89daec
JB
13296 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13297 if (cpu_arch[j].type == PROCESSOR_NONE
13298 && strcmp (arch + 2, cpu_arch[j].name) == 0)
293f5f65
L
13299 {
13300 i386_cpu_flags flags;
13301
13302 flags = cpu_flags_and_not (cpu_arch_flags,
ae89daec 13303 cpu_arch[j].disable);
293f5f65
L
13304 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
13305 {
8180707f 13306 extend_cpu_sub_arch_name (arch);
293f5f65
L
13307 cpu_arch_flags = flags;
13308 cpu_arch_isa_flags = flags;
13309 }
13310 break;
13311 }
293f5f65
L
13312 }
13313
91d6fa6a 13314 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 13315 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
13316
13317 arch = next;
9103f4f4 13318 }
293f5f65
L
13319 while (next != NULL);
13320 free (saved);
9103f4f4
L
13321 break;
13322
13323 case OPTION_MTUNE:
13324 if (*arg == '.')
2b5d6a91 13325 as_fatal (_("invalid -mtune= option: `%s'"), arg);
91d6fa6a 13326 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 13327 {
ae89daec
JB
13328 if (cpu_arch[j].type != PROCESSOR_NONE
13329 && strcmp (arg, cpu_arch[j].name) == 0)
9103f4f4 13330 {
ccc9c027 13331 cpu_arch_tune_set = 1;
91d6fa6a 13332 cpu_arch_tune = cpu_arch [j].type;
ae89daec 13333 cpu_arch_tune_flags = cpu_arch[j].enable;
9103f4f4
L
13334 break;
13335 }
13336 }
91d6fa6a 13337 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 13338 as_fatal (_("invalid -mtune= option: `%s'"), arg);
9103f4f4
L
13339 break;
13340
1efbbeb4
L
13341 case OPTION_MMNEMONIC:
13342 if (strcasecmp (arg, "att") == 0)
13343 intel_mnemonic = 0;
13344 else if (strcasecmp (arg, "intel") == 0)
13345 intel_mnemonic = 1;
13346 else
2b5d6a91 13347 as_fatal (_("invalid -mmnemonic= option: `%s'"), arg);
1efbbeb4
L
13348 break;
13349
13350 case OPTION_MSYNTAX:
13351 if (strcasecmp (arg, "att") == 0)
13352 intel_syntax = 0;
13353 else if (strcasecmp (arg, "intel") == 0)
13354 intel_syntax = 1;
13355 else
2b5d6a91 13356 as_fatal (_("invalid -msyntax= option: `%s'"), arg);
1efbbeb4
L
13357 break;
13358
13359 case OPTION_MINDEX_REG:
13360 allow_index_reg = 1;
13361 break;
13362
13363 case OPTION_MNAKED_REG:
13364 allow_naked_reg = 1;
13365 break;
13366
c0f3af97
L
13367 case OPTION_MSSE2AVX:
13368 sse2avx = 1;
13369 break;
13370
c8480b58
L
13371 case OPTION_MUSE_UNALIGNED_VECTOR_MOVE:
13372 use_unaligned_vector_move = 1;
13373 break;
13374
daf50ae7
L
13375 case OPTION_MSSE_CHECK:
13376 if (strcasecmp (arg, "error") == 0)
7bab8ab5 13377 sse_check = check_error;
daf50ae7 13378 else if (strcasecmp (arg, "warning") == 0)
7bab8ab5 13379 sse_check = check_warning;
daf50ae7 13380 else if (strcasecmp (arg, "none") == 0)
7bab8ab5 13381 sse_check = check_none;
daf50ae7 13382 else
2b5d6a91 13383 as_fatal (_("invalid -msse-check= option: `%s'"), arg);
daf50ae7
L
13384 break;
13385
7bab8ab5
JB
13386 case OPTION_MOPERAND_CHECK:
13387 if (strcasecmp (arg, "error") == 0)
13388 operand_check = check_error;
13389 else if (strcasecmp (arg, "warning") == 0)
13390 operand_check = check_warning;
13391 else if (strcasecmp (arg, "none") == 0)
13392 operand_check = check_none;
13393 else
13394 as_fatal (_("invalid -moperand-check= option: `%s'"), arg);
13395 break;
13396
539f890d
L
13397 case OPTION_MAVXSCALAR:
13398 if (strcasecmp (arg, "128") == 0)
13399 avxscalar = vex128;
13400 else if (strcasecmp (arg, "256") == 0)
13401 avxscalar = vex256;
13402 else
2b5d6a91 13403 as_fatal (_("invalid -mavxscalar= option: `%s'"), arg);
539f890d
L
13404 break;
13405
03751133
L
13406 case OPTION_MVEXWIG:
13407 if (strcmp (arg, "0") == 0)
40c9c8de 13408 vexwig = vexw0;
03751133 13409 else if (strcmp (arg, "1") == 0)
40c9c8de 13410 vexwig = vexw1;
03751133
L
13411 else
13412 as_fatal (_("invalid -mvexwig= option: `%s'"), arg);
13413 break;
13414
7e8b059b
L
13415 case OPTION_MADD_BND_PREFIX:
13416 add_bnd_prefix = 1;
13417 break;
13418
43234a1e
L
13419 case OPTION_MEVEXLIG:
13420 if (strcmp (arg, "128") == 0)
13421 evexlig = evexl128;
13422 else if (strcmp (arg, "256") == 0)
13423 evexlig = evexl256;
13424 else if (strcmp (arg, "512") == 0)
13425 evexlig = evexl512;
13426 else
13427 as_fatal (_("invalid -mevexlig= option: `%s'"), arg);
13428 break;
13429
d3d3c6db
IT
13430 case OPTION_MEVEXRCIG:
13431 if (strcmp (arg, "rne") == 0)
13432 evexrcig = rne;
13433 else if (strcmp (arg, "rd") == 0)
13434 evexrcig = rd;
13435 else if (strcmp (arg, "ru") == 0)
13436 evexrcig = ru;
13437 else if (strcmp (arg, "rz") == 0)
13438 evexrcig = rz;
13439 else
13440 as_fatal (_("invalid -mevexrcig= option: `%s'"), arg);
13441 break;
13442
43234a1e
L
13443 case OPTION_MEVEXWIG:
13444 if (strcmp (arg, "0") == 0)
13445 evexwig = evexw0;
13446 else if (strcmp (arg, "1") == 0)
13447 evexwig = evexw1;
13448 else
13449 as_fatal (_("invalid -mevexwig= option: `%s'"), arg);
13450 break;
13451
167ad85b
TG
13452# if defined (TE_PE) || defined (TE_PEP)
13453 case OPTION_MBIG_OBJ:
13454 use_big_obj = 1;
13455 break;
13456#endif
13457
d1982f93 13458 case OPTION_MOMIT_LOCK_PREFIX:
d022bddd
IT
13459 if (strcasecmp (arg, "yes") == 0)
13460 omit_lock_prefix = 1;
13461 else if (strcasecmp (arg, "no") == 0)
13462 omit_lock_prefix = 0;
13463 else
13464 as_fatal (_("invalid -momit-lock-prefix= option: `%s'"), arg);
13465 break;
13466
e4e00185
AS
13467 case OPTION_MFENCE_AS_LOCK_ADD:
13468 if (strcasecmp (arg, "yes") == 0)
13469 avoid_fence = 1;
13470 else if (strcasecmp (arg, "no") == 0)
13471 avoid_fence = 0;
13472 else
13473 as_fatal (_("invalid -mfence-as-lock-add= option: `%s'"), arg);
13474 break;
13475
ae531041
L
13476 case OPTION_MLFENCE_AFTER_LOAD:
13477 if (strcasecmp (arg, "yes") == 0)
13478 lfence_after_load = 1;
13479 else if (strcasecmp (arg, "no") == 0)
13480 lfence_after_load = 0;
13481 else
13482 as_fatal (_("invalid -mlfence-after-load= option: `%s'"), arg);
13483 break;
13484
13485 case OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH:
13486 if (strcasecmp (arg, "all") == 0)
a09f656b 13487 {
13488 lfence_before_indirect_branch = lfence_branch_all;
13489 if (lfence_before_ret == lfence_before_ret_none)
13490 lfence_before_ret = lfence_before_ret_shl;
13491 }
ae531041
L
13492 else if (strcasecmp (arg, "memory") == 0)
13493 lfence_before_indirect_branch = lfence_branch_memory;
13494 else if (strcasecmp (arg, "register") == 0)
13495 lfence_before_indirect_branch = lfence_branch_register;
13496 else if (strcasecmp (arg, "none") == 0)
13497 lfence_before_indirect_branch = lfence_branch_none;
13498 else
13499 as_fatal (_("invalid -mlfence-before-indirect-branch= option: `%s'"),
13500 arg);
13501 break;
13502
13503 case OPTION_MLFENCE_BEFORE_RET:
13504 if (strcasecmp (arg, "or") == 0)
13505 lfence_before_ret = lfence_before_ret_or;
13506 else if (strcasecmp (arg, "not") == 0)
13507 lfence_before_ret = lfence_before_ret_not;
a09f656b 13508 else if (strcasecmp (arg, "shl") == 0 || strcasecmp (arg, "yes") == 0)
13509 lfence_before_ret = lfence_before_ret_shl;
ae531041
L
13510 else if (strcasecmp (arg, "none") == 0)
13511 lfence_before_ret = lfence_before_ret_none;
13512 else
13513 as_fatal (_("invalid -mlfence-before-ret= option: `%s'"),
13514 arg);
13515 break;
13516
0cb4071e
L
13517 case OPTION_MRELAX_RELOCATIONS:
13518 if (strcasecmp (arg, "yes") == 0)
13519 generate_relax_relocations = 1;
13520 else if (strcasecmp (arg, "no") == 0)
13521 generate_relax_relocations = 0;
13522 else
13523 as_fatal (_("invalid -mrelax-relocations= option: `%s'"), arg);
13524 break;
13525
e379e5f3
L
13526 case OPTION_MALIGN_BRANCH_BOUNDARY:
13527 {
13528 char *end;
13529 long int align = strtoul (arg, &end, 0);
13530 if (*end == '\0')
13531 {
13532 if (align == 0)
13533 {
13534 align_branch_power = 0;
13535 break;
13536 }
13537 else if (align >= 16)
13538 {
13539 int align_power;
13540 for (align_power = 0;
13541 (align & 1) == 0;
13542 align >>= 1, align_power++)
13543 continue;
13544 /* Limit alignment power to 31. */
13545 if (align == 1 && align_power < 32)
13546 {
13547 align_branch_power = align_power;
13548 break;
13549 }
13550 }
13551 }
13552 as_fatal (_("invalid -malign-branch-boundary= value: %s"), arg);
13553 }
13554 break;
13555
13556 case OPTION_MALIGN_BRANCH_PREFIX_SIZE:
13557 {
13558 char *end;
13559 int align = strtoul (arg, &end, 0);
13560 /* Some processors only support 5 prefixes. */
13561 if (*end == '\0' && align >= 0 && align < 6)
13562 {
13563 align_branch_prefix_size = align;
13564 break;
13565 }
13566 as_fatal (_("invalid -malign-branch-prefix-size= value: %s"),
13567 arg);
13568 }
13569 break;
13570
13571 case OPTION_MALIGN_BRANCH:
13572 align_branch = 0;
13573 saved = xstrdup (arg);
13574 type = saved;
13575 do
13576 {
13577 next = strchr (type, '+');
13578 if (next)
13579 *next++ = '\0';
13580 if (strcasecmp (type, "jcc") == 0)
13581 align_branch |= align_branch_jcc_bit;
13582 else if (strcasecmp (type, "fused") == 0)
13583 align_branch |= align_branch_fused_bit;
13584 else if (strcasecmp (type, "jmp") == 0)
13585 align_branch |= align_branch_jmp_bit;
13586 else if (strcasecmp (type, "call") == 0)
13587 align_branch |= align_branch_call_bit;
13588 else if (strcasecmp (type, "ret") == 0)
13589 align_branch |= align_branch_ret_bit;
13590 else if (strcasecmp (type, "indirect") == 0)
13591 align_branch |= align_branch_indirect_bit;
13592 else
13593 as_fatal (_("invalid -malign-branch= option: `%s'"), arg);
13594 type = next;
13595 }
13596 while (next != NULL);
13597 free (saved);
13598 break;
13599
76cf450b
L
13600 case OPTION_MBRANCHES_WITH_32B_BOUNDARIES:
13601 align_branch_power = 5;
13602 align_branch_prefix_size = 5;
13603 align_branch = (align_branch_jcc_bit
13604 | align_branch_fused_bit
13605 | align_branch_jmp_bit);
13606 break;
13607
5db04b09 13608 case OPTION_MAMD64:
4b5aaf5f 13609 isa64 = amd64;
5db04b09
L
13610 break;
13611
13612 case OPTION_MINTEL64:
4b5aaf5f 13613 isa64 = intel64;
5db04b09
L
13614 break;
13615
b6f8c7c4
L
13616 case 'O':
13617 if (arg == NULL)
13618 {
13619 optimize = 1;
13620 /* Turn off -Os. */
13621 optimize_for_space = 0;
13622 }
13623 else if (*arg == 's')
13624 {
13625 optimize_for_space = 1;
13626 /* Turn on all encoding optimizations. */
41fd2579 13627 optimize = INT_MAX;
b6f8c7c4
L
13628 }
13629 else
13630 {
13631 optimize = atoi (arg);
13632 /* Turn off -Os. */
13633 optimize_for_space = 0;
13634 }
13635 break;
13636
252b5132
RH
13637 default:
13638 return 0;
13639 }
13640 return 1;
13641}
13642
8a2c8fef
L
13643#define MESSAGE_TEMPLATE \
13644" "
13645
293f5f65
L
13646static char *
13647output_message (FILE *stream, char *p, char *message, char *start,
13648 int *left_p, const char *name, int len)
13649{
13650 int size = sizeof (MESSAGE_TEMPLATE);
13651 int left = *left_p;
13652
13653 /* Reserve 2 spaces for ", " or ",\0" */
13654 left -= len + 2;
13655
13656 /* Check if there is any room. */
13657 if (left >= 0)
13658 {
13659 if (p != start)
13660 {
13661 *p++ = ',';
13662 *p++ = ' ';
13663 }
13664 p = mempcpy (p, name, len);
13665 }
13666 else
13667 {
13668 /* Output the current message now and start a new one. */
13669 *p++ = ',';
13670 *p = '\0';
13671 fprintf (stream, "%s\n", message);
13672 p = start;
13673 left = size - (start - message) - len - 2;
13674
13675 gas_assert (left >= 0);
13676
13677 p = mempcpy (p, name, len);
13678 }
13679
13680 *left_p = left;
13681 return p;
13682}
13683
8a2c8fef 13684static void
1ded5609 13685show_arch (FILE *stream, int ext, int check)
8a2c8fef
L
13686{
13687 static char message[] = MESSAGE_TEMPLATE;
13688 char *start = message + 27;
13689 char *p;
13690 int size = sizeof (MESSAGE_TEMPLATE);
13691 int left;
13692 const char *name;
13693 int len;
13694 unsigned int j;
13695
13696 p = start;
13697 left = size - (start - message);
3ce2ebcf
JB
13698
13699 if (!ext && check)
13700 {
13701 p = output_message (stream, p, message, start, &left,
13702 STRING_COMMA_LEN ("default"));
f68697e8
JB
13703 p = output_message (stream, p, message, start, &left,
13704 STRING_COMMA_LEN ("push"));
13705 p = output_message (stream, p, message, start, &left,
13706 STRING_COMMA_LEN ("pop"));
3ce2ebcf
JB
13707 }
13708
8a2c8fef
L
13709 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13710 {
13711 /* Should it be skipped? */
13712 if (cpu_arch [j].skip)
13713 continue;
13714
13715 name = cpu_arch [j].name;
13716 len = cpu_arch [j].len;
ae89daec 13717 if (cpu_arch[j].type == PROCESSOR_NONE)
8a2c8fef
L
13718 {
13719 /* It is an extension. Skip if we aren't asked to show it. */
ae89daec 13720 if (!ext || cpu_flags_all_zero (&cpu_arch[j].enable))
8a2c8fef
L
13721 continue;
13722 }
13723 else if (ext)
13724 {
13725 /* It is an processor. Skip if we show only extension. */
13726 continue;
13727 }
ae89daec 13728 else if (check && ! cpu_arch[j].enable.bitfield.cpui386)
1ded5609
JB
13729 {
13730 /* It is an impossible processor - skip. */
13731 continue;
13732 }
8a2c8fef 13733
293f5f65 13734 p = output_message (stream, p, message, start, &left, name, len);
8a2c8fef
L
13735 }
13736
293f5f65
L
13737 /* Display disabled extensions. */
13738 if (ext)
ae89daec 13739 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
293f5f65 13740 {
ae89daec
JB
13741 char *str;
13742
13743 if (cpu_arch[j].type != PROCESSOR_NONE
13744 || !cpu_flags_all_zero (&cpu_arch[j].enable))
13745 continue;
13746 str = xasprintf ("no%s", cpu_arch[j].name);
13747 p = output_message (stream, p, message, start, &left, str,
13748 strlen (str));
13749 free (str);
293f5f65
L
13750 }
13751
8a2c8fef
L
13752 *p = '\0';
13753 fprintf (stream, "%s\n", message);
13754}
13755
252b5132 13756void
8a2c8fef 13757md_show_usage (FILE *stream)
252b5132 13758{
4cc782b5
ILT
13759#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13760 fprintf (stream, _("\
d4693039 13761 -Qy, -Qn ignored\n\
a38cf1db 13762 -V print assembler version number\n\
b3b91714
AM
13763 -k ignored\n"));
13764#endif
13765 fprintf (stream, _("\
7ebd68d1
NC
13766 -n do not optimize code alignment\n\
13767 -O{012s} attempt some code optimizations\n\
b3b91714
AM
13768 -q quieten some warnings\n"));
13769#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13770 fprintf (stream, _("\
a38cf1db 13771 -s ignored\n"));
b3b91714 13772#endif
b00af7c8
JB
13773#ifdef BFD64
13774# if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13775 fprintf (stream, _("\
13776 --32/--64/--x32 generate 32bit/64bit/x32 object\n"));
13777# elif defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O)
751d281c 13778 fprintf (stream, _("\
b00af7c8
JB
13779 --32/--64 generate 32bit/64bit object\n"));
13780# endif
751d281c 13781#endif
b3b91714
AM
13782#ifdef SVR4_COMMENT_CHARS
13783 fprintf (stream, _("\
13784 --divide do not treat `/' as a comment character\n"));
a38cf1db
AM
13785#else
13786 fprintf (stream, _("\
b3b91714 13787 --divide ignored\n"));
4cc782b5 13788#endif
9103f4f4 13789 fprintf (stream, _("\
6305a203 13790 -march=CPU[,+EXTENSION...]\n\
8a2c8fef 13791 generate code for CPU and EXTENSION, CPU is one of:\n"));
1ded5609 13792 show_arch (stream, 0, 1);
8a2c8fef 13793 fprintf (stream, _("\
ae89daec 13794 EXTENSION is combination of (possibly \"no\"-prefixed):\n"));
1ded5609 13795 show_arch (stream, 1, 0);
6305a203 13796 fprintf (stream, _("\
8a2c8fef 13797 -mtune=CPU optimize for CPU, CPU is one of:\n"));
1ded5609 13798 show_arch (stream, 0, 0);
ba104c83 13799 fprintf (stream, _("\
c0f3af97
L
13800 -msse2avx encode SSE instructions with VEX prefix\n"));
13801 fprintf (stream, _("\
c8480b58
L
13802 -muse-unaligned-vector-move\n\
13803 encode aligned vector move as unaligned vector move\n"));
13804 fprintf (stream, _("\
7c5c05ef 13805 -msse-check=[none|error|warning] (default: warning)\n\
daf50ae7
L
13806 check SSE instructions\n"));
13807 fprintf (stream, _("\
7c5c05ef 13808 -moperand-check=[none|error|warning] (default: warning)\n\
7bab8ab5
JB
13809 check operand combinations for validity\n"));
13810 fprintf (stream, _("\
7c5c05ef
L
13811 -mavxscalar=[128|256] (default: 128)\n\
13812 encode scalar AVX instructions with specific vector\n\
539f890d
L
13813 length\n"));
13814 fprintf (stream, _("\
03751133
L
13815 -mvexwig=[0|1] (default: 0)\n\
13816 encode VEX instructions with specific VEX.W value\n\
13817 for VEX.W bit ignored instructions\n"));
13818 fprintf (stream, _("\
7c5c05ef
L
13819 -mevexlig=[128|256|512] (default: 128)\n\
13820 encode scalar EVEX instructions with specific vector\n\
43234a1e
L
13821 length\n"));
13822 fprintf (stream, _("\
7c5c05ef
L
13823 -mevexwig=[0|1] (default: 0)\n\
13824 encode EVEX instructions with specific EVEX.W value\n\
43234a1e
L
13825 for EVEX.W bit ignored instructions\n"));
13826 fprintf (stream, _("\
7c5c05ef 13827 -mevexrcig=[rne|rd|ru|rz] (default: rne)\n\
d3d3c6db
IT
13828 encode EVEX instructions with specific EVEX.RC value\n\
13829 for SAE-only ignored instructions\n"));
13830 fprintf (stream, _("\
7c5c05ef
L
13831 -mmnemonic=[att|intel] "));
13832 if (SYSV386_COMPAT)
13833 fprintf (stream, _("(default: att)\n"));
13834 else
13835 fprintf (stream, _("(default: intel)\n"));
13836 fprintf (stream, _("\
13837 use AT&T/Intel mnemonic\n"));
ba104c83 13838 fprintf (stream, _("\
7c5c05ef
L
13839 -msyntax=[att|intel] (default: att)\n\
13840 use AT&T/Intel syntax\n"));
ba104c83
L
13841 fprintf (stream, _("\
13842 -mindex-reg support pseudo index registers\n"));
13843 fprintf (stream, _("\
13844 -mnaked-reg don't require `%%' prefix for registers\n"));
13845 fprintf (stream, _("\
7e8b059b 13846 -madd-bnd-prefix add BND prefix for all valid branches\n"));
b4a3a7b4 13847#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8dcea932
L
13848 fprintf (stream, _("\
13849 -mshared disable branch optimization for shared code\n"));
b4a3a7b4
L
13850 fprintf (stream, _("\
13851 -mx86-used-note=[no|yes] "));
13852 if (DEFAULT_X86_USED_NOTE)
13853 fprintf (stream, _("(default: yes)\n"));
13854 else
13855 fprintf (stream, _("(default: no)\n"));
13856 fprintf (stream, _("\
13857 generate x86 used ISA and feature properties\n"));
13858#endif
13859#if defined (TE_PE) || defined (TE_PEP)
167ad85b
TG
13860 fprintf (stream, _("\
13861 -mbig-obj generate big object files\n"));
13862#endif
d022bddd 13863 fprintf (stream, _("\
7c5c05ef 13864 -momit-lock-prefix=[no|yes] (default: no)\n\
d022bddd 13865 strip all lock prefixes\n"));
5db04b09 13866 fprintf (stream, _("\
7c5c05ef 13867 -mfence-as-lock-add=[no|yes] (default: no)\n\
e4e00185
AS
13868 encode lfence, mfence and sfence as\n\
13869 lock addl $0x0, (%%{re}sp)\n"));
13870 fprintf (stream, _("\
7c5c05ef
L
13871 -mrelax-relocations=[no|yes] "));
13872 if (DEFAULT_GENERATE_X86_RELAX_RELOCATIONS)
13873 fprintf (stream, _("(default: yes)\n"));
13874 else
13875 fprintf (stream, _("(default: no)\n"));
13876 fprintf (stream, _("\
0cb4071e
L
13877 generate relax relocations\n"));
13878 fprintf (stream, _("\
e379e5f3
L
13879 -malign-branch-boundary=NUM (default: 0)\n\
13880 align branches within NUM byte boundary\n"));
13881 fprintf (stream, _("\
13882 -malign-branch=TYPE[+TYPE...] (default: jcc+fused+jmp)\n\
13883 TYPE is combination of jcc, fused, jmp, call, ret,\n\
13884 indirect\n\
13885 specify types of branches to align\n"));
13886 fprintf (stream, _("\
13887 -malign-branch-prefix-size=NUM (default: 5)\n\
13888 align branches with NUM prefixes per instruction\n"));
13889 fprintf (stream, _("\
76cf450b
L
13890 -mbranches-within-32B-boundaries\n\
13891 align branches within 32 byte boundary\n"));
13892 fprintf (stream, _("\
ae531041
L
13893 -mlfence-after-load=[no|yes] (default: no)\n\
13894 generate lfence after load\n"));
13895 fprintf (stream, _("\
13896 -mlfence-before-indirect-branch=[none|all|register|memory] (default: none)\n\
13897 generate lfence before indirect near branch\n"));
13898 fprintf (stream, _("\
a09f656b 13899 -mlfence-before-ret=[none|or|not|shl|yes] (default: none)\n\
ae531041
L
13900 generate lfence before ret\n"));
13901 fprintf (stream, _("\
7c5c05ef 13902 -mamd64 accept only AMD64 ISA [default]\n"));
5db04b09
L
13903 fprintf (stream, _("\
13904 -mintel64 accept only Intel64 ISA\n"));
252b5132
RH
13905}
13906
3e73aa7c 13907#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
321098a5 13908 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
e57f8c65 13909 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
252b5132
RH
13910
13911/* Pick the target format to use. */
13912
47926f60 13913const char *
e3bb37b5 13914i386_target_format (void)
252b5132 13915{
d34049e8 13916 if (startswith (default_arch, "x86_64"))
351f65ca
L
13917 {
13918 update_code_flag (CODE_64BIT, 1);
13919 if (default_arch[6] == '\0')
7f56bc95 13920 x86_elf_abi = X86_64_ABI;
351f65ca 13921 else
7f56bc95 13922 x86_elf_abi = X86_64_X32_ABI;
351f65ca 13923 }
3e73aa7c 13924 else if (!strcmp (default_arch, "i386"))
78f12dd3 13925 update_code_flag (CODE_32BIT, 1);
5197d474
L
13926 else if (!strcmp (default_arch, "iamcu"))
13927 {
13928 update_code_flag (CODE_32BIT, 1);
13929 if (cpu_arch_isa == PROCESSOR_UNKNOWN)
13930 {
13931 static const i386_cpu_flags iamcu_flags = CPU_IAMCU_FLAGS;
13932 cpu_arch_name = "iamcu";
d92c7521 13933 free (cpu_sub_arch_name);
5197d474
L
13934 cpu_sub_arch_name = NULL;
13935 cpu_arch_flags = iamcu_flags;
13936 cpu_arch_isa = PROCESSOR_IAMCU;
13937 cpu_arch_isa_flags = iamcu_flags;
13938 if (!cpu_arch_tune_set)
13939 {
13940 cpu_arch_tune = cpu_arch_isa;
13941 cpu_arch_tune_flags = cpu_arch_isa_flags;
13942 }
13943 }
8d471ec1 13944 else if (cpu_arch_isa != PROCESSOR_IAMCU)
5197d474
L
13945 as_fatal (_("Intel MCU doesn't support `%s' architecture"),
13946 cpu_arch_name);
13947 }
3e73aa7c 13948 else
2b5d6a91 13949 as_fatal (_("unknown architecture"));
89507696
JB
13950
13951 if (cpu_flags_all_zero (&cpu_arch_isa_flags))
ae89daec 13952 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
89507696 13953 if (cpu_flags_all_zero (&cpu_arch_tune_flags))
ae89daec 13954 cpu_arch_tune_flags = cpu_arch[flag_code == CODE_64BIT].enable;
89507696 13955
252b5132
RH
13956 switch (OUTPUT_FLAVOR)
13957 {
9384f2ff 13958#if defined (OBJ_MAYBE_AOUT) || defined (OBJ_AOUT)
4c63da97 13959 case bfd_target_aout_flavour:
47926f60 13960 return AOUT_TARGET_FORMAT;
4c63da97 13961#endif
9384f2ff
AM
13962#if defined (OBJ_MAYBE_COFF) || defined (OBJ_COFF)
13963# if defined (TE_PE) || defined (TE_PEP)
13964 case bfd_target_coff_flavour:
167ad85b 13965 if (flag_code == CODE_64BIT)
eb19308f
JB
13966 {
13967 object_64bit = 1;
13968 return use_big_obj ? "pe-bigobj-x86-64" : "pe-x86-64";
13969 }
13970 return use_big_obj ? "pe-bigobj-i386" : "pe-i386";
9384f2ff 13971# elif defined (TE_GO32)
0561d57c
JK
13972 case bfd_target_coff_flavour:
13973 return "coff-go32";
9384f2ff 13974# else
252b5132
RH
13975 case bfd_target_coff_flavour:
13976 return "coff-i386";
9384f2ff 13977# endif
4c63da97 13978#endif
3e73aa7c 13979#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
252b5132 13980 case bfd_target_elf_flavour:
3e73aa7c 13981 {
351f65ca
L
13982 const char *format;
13983
13984 switch (x86_elf_abi)
4fa24527 13985 {
351f65ca
L
13986 default:
13987 format = ELF_TARGET_FORMAT;
e379e5f3
L
13988#ifndef TE_SOLARIS
13989 tls_get_addr = "___tls_get_addr";
13990#endif
351f65ca 13991 break;
7f56bc95 13992 case X86_64_ABI:
351f65ca 13993 use_rela_relocations = 1;
4fa24527 13994 object_64bit = 1;
e379e5f3
L
13995#ifndef TE_SOLARIS
13996 tls_get_addr = "__tls_get_addr";
13997#endif
351f65ca
L
13998 format = ELF_TARGET_FORMAT64;
13999 break;
7f56bc95 14000 case X86_64_X32_ABI:
4fa24527 14001 use_rela_relocations = 1;
351f65ca 14002 object_64bit = 1;
e379e5f3
L
14003#ifndef TE_SOLARIS
14004 tls_get_addr = "__tls_get_addr";
14005#endif
862be3fb 14006 disallow_64bit_reloc = 1;
351f65ca
L
14007 format = ELF_TARGET_FORMAT32;
14008 break;
4fa24527 14009 }
c085ab00 14010 if (cpu_arch_isa == PROCESSOR_IAMCU)
81486035
L
14011 {
14012 if (x86_elf_abi != I386_ABI)
14013 as_fatal (_("Intel MCU is 32bit only"));
14014 return ELF_TARGET_IAMCU_FORMAT;
14015 }
8a9036a4 14016 else
351f65ca 14017 return format;
3e73aa7c 14018 }
e57f8c65
TG
14019#endif
14020#if defined (OBJ_MACH_O)
14021 case bfd_target_mach_o_flavour:
d382c579
TG
14022 if (flag_code == CODE_64BIT)
14023 {
14024 use_rela_relocations = 1;
14025 object_64bit = 1;
14026 return "mach-o-x86-64";
14027 }
14028 else
14029 return "mach-o-i386";
4c63da97 14030#endif
252b5132
RH
14031 default:
14032 abort ();
14033 return NULL;
14034 }
14035}
14036
47926f60 14037#endif /* OBJ_MAYBE_ more than one */
252b5132 14038\f
252b5132 14039symbolS *
7016a5d5 14040md_undefined_symbol (char *name)
252b5132 14041{
18dc2407
ILT
14042 if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
14043 && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
14044 && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
14045 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
24eab124
AM
14046 {
14047 if (!GOT_symbol)
14048 {
14049 if (symbol_find (name))
14050 as_bad (_("GOT already in symbol table"));
14051 GOT_symbol = symbol_new (name, undefined_section,
e01e1cee 14052 &zero_address_frag, 0);
24eab124
AM
14053 };
14054 return GOT_symbol;
14055 }
252b5132
RH
14056 return 0;
14057}
14058
14059/* Round up a section size to the appropriate boundary. */
47926f60 14060
252b5132 14061valueT
7016a5d5 14062md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
252b5132 14063{
4c63da97
AM
14064#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
14065 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
14066 {
14067 /* For a.out, force the section size to be aligned. If we don't do
14068 this, BFD will align it for us, but it will not write out the
14069 final bytes of the section. This may be a bug in BFD, but it is
14070 easier to fix it here since that is how the other a.out targets
14071 work. */
14072 int align;
14073
fd361982 14074 align = bfd_section_alignment (segment);
8d3842cd 14075 size = ((size + (1 << align) - 1) & (-((valueT) 1 << align)));
4c63da97 14076 }
252b5132
RH
14077#endif
14078
14079 return size;
14080}
14081
14082/* On the i386, PC-relative offsets are relative to the start of the
14083 next instruction. That is, the address of the offset, plus its
14084 size, since the offset is always the last part of the insn. */
14085
14086long
e3bb37b5 14087md_pcrel_from (fixS *fixP)
252b5132
RH
14088{
14089 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
14090}
14091
14092#ifndef I386COFF
14093
14094static void
e3bb37b5 14095s_bss (int ignore ATTRIBUTE_UNUSED)
252b5132 14096{
29b0f896 14097 int temp;
252b5132 14098
8a75718c
JB
14099#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14100 if (IS_ELF)
14101 obj_elf_section_change_hook ();
14102#endif
252b5132
RH
14103 temp = get_absolute_expression ();
14104 subseg_set (bss_section, (subsegT) temp);
14105 demand_empty_rest_of_line ();
14106}
14107
14108#endif
14109
e379e5f3
L
14110/* Remember constant directive. */
14111
14112void
14113i386_cons_align (int ignore ATTRIBUTE_UNUSED)
14114{
14115 if (last_insn.kind != last_insn_directive
14116 && (bfd_section_flags (now_seg) & SEC_CODE))
14117 {
14118 last_insn.seg = now_seg;
14119 last_insn.kind = last_insn_directive;
14120 last_insn.name = "constant directive";
14121 last_insn.file = as_where (&last_insn.line);
ae531041
L
14122 if (lfence_before_ret != lfence_before_ret_none)
14123 {
14124 if (lfence_before_indirect_branch != lfence_branch_none)
14125 as_warn (_("constant directive skips -mlfence-before-ret "
14126 "and -mlfence-before-indirect-branch"));
14127 else
14128 as_warn (_("constant directive skips -mlfence-before-ret"));
14129 }
14130 else if (lfence_before_indirect_branch != lfence_branch_none)
14131 as_warn (_("constant directive skips -mlfence-before-indirect-branch"));
e379e5f3
L
14132 }
14133}
14134
3abbafc2 14135int
e3bb37b5 14136i386_validate_fix (fixS *fixp)
252b5132 14137{
e52a16f2
JB
14138 if (fixp->fx_addsy && S_GET_SEGMENT(fixp->fx_addsy) == reg_section)
14139 {
14140 reloc_howto_type *howto;
14141
14142 howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
14143 as_bad_where (fixp->fx_file, fixp->fx_line,
14144 _("invalid %s relocation against register"),
14145 howto ? howto->name : "<unknown>");
14146 return 0;
14147 }
14148
3abbafc2
JB
14149#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14150 if (fixp->fx_r_type == BFD_RELOC_SIZE32
14151 || fixp->fx_r_type == BFD_RELOC_SIZE64)
14152 return IS_ELF && fixp->fx_addsy
14153 && (!S_IS_DEFINED (fixp->fx_addsy)
14154 || S_IS_EXTERNAL (fixp->fx_addsy));
14155#endif
14156
02a86693 14157 if (fixp->fx_subsy)
252b5132 14158 {
02a86693 14159 if (fixp->fx_subsy == GOT_symbol)
23df1078 14160 {
02a86693
L
14161 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
14162 {
14163 if (!object_64bit)
14164 abort ();
14165#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14166 if (fixp->fx_tcbit2)
56ceb5b5
L
14167 fixp->fx_r_type = (fixp->fx_tcbit
14168 ? BFD_RELOC_X86_64_REX_GOTPCRELX
14169 : BFD_RELOC_X86_64_GOTPCRELX);
02a86693
L
14170 else
14171#endif
14172 fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
14173 }
d6ab8113 14174 else
02a86693
L
14175 {
14176 if (!object_64bit)
14177 fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
14178 else
14179 fixp->fx_r_type = BFD_RELOC_X86_64_GOTOFF64;
14180 }
14181 fixp->fx_subsy = 0;
23df1078 14182 }
252b5132 14183 }
02a86693 14184#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2585b7a5 14185 else
02a86693 14186 {
2585b7a5
L
14187 /* NB: Commit 292676c1 resolved PLT32 reloc aganst local symbol
14188 to section. Since PLT32 relocation must be against symbols,
14189 turn such PLT32 relocation into PC32 relocation. */
14190 if (fixp->fx_addsy
14191 && (fixp->fx_r_type == BFD_RELOC_386_PLT32
14192 || fixp->fx_r_type == BFD_RELOC_X86_64_PLT32)
14193 && symbol_section_p (fixp->fx_addsy))
14194 fixp->fx_r_type = BFD_RELOC_32_PCREL;
14195 if (!object_64bit)
14196 {
14197 if (fixp->fx_r_type == BFD_RELOC_386_GOT32
14198 && fixp->fx_tcbit2)
14199 fixp->fx_r_type = BFD_RELOC_386_GOT32X;
14200 }
02a86693
L
14201 }
14202#endif
3abbafc2
JB
14203
14204 return 1;
252b5132
RH
14205}
14206
252b5132 14207arelent *
7016a5d5 14208tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
14209{
14210 arelent *rel;
14211 bfd_reloc_code_real_type code;
14212
14213 switch (fixp->fx_r_type)
14214 {
8ce3d284 14215#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3abbafc2
JB
14216 symbolS *sym;
14217
8fd4256d
L
14218 case BFD_RELOC_SIZE32:
14219 case BFD_RELOC_SIZE64:
3abbafc2
JB
14220 if (fixp->fx_addsy
14221 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))
14222 && (!fixp->fx_subsy
14223 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))))
14224 sym = fixp->fx_addsy;
14225 else if (fixp->fx_subsy
14226 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))
14227 && (!fixp->fx_addsy
14228 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))))
14229 sym = fixp->fx_subsy;
14230 else
14231 sym = NULL;
14232 if (IS_ELF && sym && S_IS_DEFINED (sym) && !S_IS_EXTERNAL (sym))
8fd4256d
L
14233 {
14234 /* Resolve size relocation against local symbol to size of
14235 the symbol plus addend. */
3abbafc2 14236 valueT value = S_GET_SIZE (sym);
44f87162 14237
3abbafc2
JB
14238 if (symbol_get_bfdsym (sym)->flags & BSF_SECTION_SYM)
14239 value = bfd_section_size (S_GET_SEGMENT (sym));
14240 if (sym == fixp->fx_subsy)
14241 {
14242 value = -value;
14243 if (fixp->fx_addsy)
14244 value += S_GET_VALUE (fixp->fx_addsy);
14245 }
14246 else if (fixp->fx_subsy)
14247 value -= S_GET_VALUE (fixp->fx_subsy);
44f87162 14248 value += fixp->fx_offset;
8fd4256d 14249 if (fixp->fx_r_type == BFD_RELOC_SIZE32
d965814f 14250 && object_64bit
8fd4256d
L
14251 && !fits_in_unsigned_long (value))
14252 as_bad_where (fixp->fx_file, fixp->fx_line,
14253 _("symbol size computation overflow"));
14254 fixp->fx_addsy = NULL;
14255 fixp->fx_subsy = NULL;
14256 md_apply_fix (fixp, (valueT *) &value, NULL);
14257 return NULL;
14258 }
3abbafc2
JB
14259 if (!fixp->fx_addsy || fixp->fx_subsy)
14260 {
14261 as_bad_where (fixp->fx_file, fixp->fx_line,
14262 "unsupported expression involving @size");
14263 return NULL;
14264 }
8ce3d284 14265#endif
1a0670f3 14266 /* Fall through. */
8fd4256d 14267
3e73aa7c
JH
14268 case BFD_RELOC_X86_64_PLT32:
14269 case BFD_RELOC_X86_64_GOT32:
14270 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
14271 case BFD_RELOC_X86_64_GOTPCRELX:
14272 case BFD_RELOC_X86_64_REX_GOTPCRELX:
252b5132
RH
14273 case BFD_RELOC_386_PLT32:
14274 case BFD_RELOC_386_GOT32:
02a86693 14275 case BFD_RELOC_386_GOT32X:
252b5132
RH
14276 case BFD_RELOC_386_GOTOFF:
14277 case BFD_RELOC_386_GOTPC:
13ae64f3
JJ
14278 case BFD_RELOC_386_TLS_GD:
14279 case BFD_RELOC_386_TLS_LDM:
14280 case BFD_RELOC_386_TLS_LDO_32:
14281 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
14282 case BFD_RELOC_386_TLS_IE:
14283 case BFD_RELOC_386_TLS_GOTIE:
13ae64f3
JJ
14284 case BFD_RELOC_386_TLS_LE_32:
14285 case BFD_RELOC_386_TLS_LE:
67a4f2b7
AO
14286 case BFD_RELOC_386_TLS_GOTDESC:
14287 case BFD_RELOC_386_TLS_DESC_CALL:
bffbf940
JJ
14288 case BFD_RELOC_X86_64_TLSGD:
14289 case BFD_RELOC_X86_64_TLSLD:
14290 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 14291 case BFD_RELOC_X86_64_DTPOFF64:
bffbf940
JJ
14292 case BFD_RELOC_X86_64_GOTTPOFF:
14293 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113
JB
14294 case BFD_RELOC_X86_64_TPOFF64:
14295 case BFD_RELOC_X86_64_GOTOFF64:
14296 case BFD_RELOC_X86_64_GOTPC32:
7b81dfbb
AJ
14297 case BFD_RELOC_X86_64_GOT64:
14298 case BFD_RELOC_X86_64_GOTPCREL64:
14299 case BFD_RELOC_X86_64_GOTPC64:
14300 case BFD_RELOC_X86_64_GOTPLT64:
14301 case BFD_RELOC_X86_64_PLTOFF64:
67a4f2b7
AO
14302 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14303 case BFD_RELOC_X86_64_TLSDESC_CALL:
252b5132
RH
14304 case BFD_RELOC_RVA:
14305 case BFD_RELOC_VTABLE_ENTRY:
14306 case BFD_RELOC_VTABLE_INHERIT:
6482c264
NC
14307#ifdef TE_PE
14308 case BFD_RELOC_32_SECREL:
145667f8 14309 case BFD_RELOC_16_SECIDX:
6482c264 14310#endif
252b5132
RH
14311 code = fixp->fx_r_type;
14312 break;
dbbaec26
L
14313 case BFD_RELOC_X86_64_32S:
14314 if (!fixp->fx_pcrel)
14315 {
14316 /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32. */
14317 code = fixp->fx_r_type;
14318 break;
14319 }
1a0670f3 14320 /* Fall through. */
252b5132 14321 default:
93382f6d 14322 if (fixp->fx_pcrel)
252b5132 14323 {
93382f6d
AM
14324 switch (fixp->fx_size)
14325 {
14326 default:
b091f402
AM
14327 as_bad_where (fixp->fx_file, fixp->fx_line,
14328 _("can not do %d byte pc-relative relocation"),
14329 fixp->fx_size);
93382f6d
AM
14330 code = BFD_RELOC_32_PCREL;
14331 break;
14332 case 1: code = BFD_RELOC_8_PCREL; break;
14333 case 2: code = BFD_RELOC_16_PCREL; break;
d258b828 14334 case 4: code = BFD_RELOC_32_PCREL; break;
d6ab8113
JB
14335#ifdef BFD64
14336 case 8: code = BFD_RELOC_64_PCREL; break;
14337#endif
93382f6d
AM
14338 }
14339 }
14340 else
14341 {
14342 switch (fixp->fx_size)
14343 {
14344 default:
b091f402
AM
14345 as_bad_where (fixp->fx_file, fixp->fx_line,
14346 _("can not do %d byte relocation"),
14347 fixp->fx_size);
93382f6d
AM
14348 code = BFD_RELOC_32;
14349 break;
14350 case 1: code = BFD_RELOC_8; break;
14351 case 2: code = BFD_RELOC_16; break;
14352 case 4: code = BFD_RELOC_32; break;
937149dd 14353#ifdef BFD64
3e73aa7c 14354 case 8: code = BFD_RELOC_64; break;
937149dd 14355#endif
93382f6d 14356 }
252b5132
RH
14357 }
14358 break;
14359 }
252b5132 14360
d182319b
JB
14361 if ((code == BFD_RELOC_32
14362 || code == BFD_RELOC_32_PCREL
14363 || code == BFD_RELOC_X86_64_32S)
252b5132
RH
14364 && GOT_symbol
14365 && fixp->fx_addsy == GOT_symbol)
3e73aa7c 14366 {
4fa24527 14367 if (!object_64bit)
d6ab8113
JB
14368 code = BFD_RELOC_386_GOTPC;
14369 else
14370 code = BFD_RELOC_X86_64_GOTPC32;
3e73aa7c 14371 }
7b81dfbb
AJ
14372 if ((code == BFD_RELOC_64 || code == BFD_RELOC_64_PCREL)
14373 && GOT_symbol
14374 && fixp->fx_addsy == GOT_symbol)
14375 {
14376 code = BFD_RELOC_X86_64_GOTPC64;
14377 }
252b5132 14378
add39d23
TS
14379 rel = XNEW (arelent);
14380 rel->sym_ptr_ptr = XNEW (asymbol *);
49309057 14381 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
14382
14383 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
c87db184 14384
3e73aa7c
JH
14385 if (!use_rela_relocations)
14386 {
14387 /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
14388 vtable entry to be used in the relocation's section offset. */
14389 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
14390 rel->address = fixp->fx_offset;
fbeb56a4
DK
14391#if defined (OBJ_COFF) && defined (TE_PE)
14392 else if (fixp->fx_addsy && S_IS_WEAK (fixp->fx_addsy))
14393 rel->addend = fixp->fx_addnumber - (S_GET_VALUE (fixp->fx_addsy) * 2);
14394 else
14395#endif
c6682705 14396 rel->addend = 0;
3e73aa7c
JH
14397 }
14398 /* Use the rela in 64bit mode. */
252b5132 14399 else
3e73aa7c 14400 {
862be3fb
L
14401 if (disallow_64bit_reloc)
14402 switch (code)
14403 {
862be3fb
L
14404 case BFD_RELOC_X86_64_DTPOFF64:
14405 case BFD_RELOC_X86_64_TPOFF64:
14406 case BFD_RELOC_64_PCREL:
14407 case BFD_RELOC_X86_64_GOTOFF64:
14408 case BFD_RELOC_X86_64_GOT64:
14409 case BFD_RELOC_X86_64_GOTPCREL64:
14410 case BFD_RELOC_X86_64_GOTPC64:
14411 case BFD_RELOC_X86_64_GOTPLT64:
14412 case BFD_RELOC_X86_64_PLTOFF64:
14413 as_bad_where (fixp->fx_file, fixp->fx_line,
14414 _("cannot represent relocation type %s in x32 mode"),
14415 bfd_get_reloc_code_name (code));
14416 break;
14417 default:
14418 break;
14419 }
14420
062cd5e7
AS
14421 if (!fixp->fx_pcrel)
14422 rel->addend = fixp->fx_offset;
14423 else
14424 switch (code)
14425 {
14426 case BFD_RELOC_X86_64_PLT32:
14427 case BFD_RELOC_X86_64_GOT32:
14428 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
14429 case BFD_RELOC_X86_64_GOTPCRELX:
14430 case BFD_RELOC_X86_64_REX_GOTPCRELX:
bffbf940
JJ
14431 case BFD_RELOC_X86_64_TLSGD:
14432 case BFD_RELOC_X86_64_TLSLD:
14433 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7
AO
14434 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14435 case BFD_RELOC_X86_64_TLSDESC_CALL:
062cd5e7
AS
14436 rel->addend = fixp->fx_offset - fixp->fx_size;
14437 break;
14438 default:
14439 rel->addend = (section->vma
14440 - fixp->fx_size
14441 + fixp->fx_addnumber
14442 + md_pcrel_from (fixp));
14443 break;
14444 }
3e73aa7c
JH
14445 }
14446
252b5132
RH
14447 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
14448 if (rel->howto == NULL)
14449 {
14450 as_bad_where (fixp->fx_file, fixp->fx_line,
d0b47220 14451 _("cannot represent relocation type %s"),
252b5132
RH
14452 bfd_get_reloc_code_name (code));
14453 /* Set howto to a garbage value so that we can keep going. */
14454 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
9c2799c2 14455 gas_assert (rel->howto != NULL);
252b5132
RH
14456 }
14457
14458 return rel;
14459}
14460
ee86248c 14461#include "tc-i386-intel.c"
54cfded0 14462
a60de03c
JB
14463void
14464tc_x86_parse_to_dw2regnum (expressionS *exp)
54cfded0 14465{
a60de03c
JB
14466 int saved_naked_reg;
14467 char saved_register_dot;
54cfded0 14468
a60de03c
JB
14469 saved_naked_reg = allow_naked_reg;
14470 allow_naked_reg = 1;
14471 saved_register_dot = register_chars['.'];
14472 register_chars['.'] = '.';
14473 allow_pseudo_reg = 1;
14474 expression_and_evaluate (exp);
14475 allow_pseudo_reg = 0;
14476 register_chars['.'] = saved_register_dot;
14477 allow_naked_reg = saved_naked_reg;
14478
e96d56a1 14479 if (exp->X_op == O_register && exp->X_add_number >= 0)
54cfded0 14480 {
a60de03c
JB
14481 if ((addressT) exp->X_add_number < i386_regtab_size)
14482 {
14483 exp->X_op = O_constant;
14484 exp->X_add_number = i386_regtab[exp->X_add_number]
14485 .dw2_regnum[flag_code >> 1];
14486 }
14487 else
14488 exp->X_op = O_illegal;
54cfded0 14489 }
54cfded0
AM
14490}
14491
14492void
14493tc_x86_frame_initial_instructions (void)
14494{
a60de03c
JB
14495 static unsigned int sp_regno[2];
14496
14497 if (!sp_regno[flag_code >> 1])
14498 {
14499 char *saved_input = input_line_pointer;
14500 char sp[][4] = {"esp", "rsp"};
14501 expressionS exp;
a4447b93 14502
a60de03c
JB
14503 input_line_pointer = sp[flag_code >> 1];
14504 tc_x86_parse_to_dw2regnum (&exp);
9c2799c2 14505 gas_assert (exp.X_op == O_constant);
a60de03c
JB
14506 sp_regno[flag_code >> 1] = exp.X_add_number;
14507 input_line_pointer = saved_input;
14508 }
a4447b93 14509
61ff971f
L
14510 cfi_add_CFA_def_cfa (sp_regno[flag_code >> 1], -x86_cie_data_alignment);
14511 cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
54cfded0 14512}
d2b2c203 14513
d7921315
L
14514int
14515x86_dwarf2_addr_size (void)
14516{
14517#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
14518 if (x86_elf_abi == X86_64_X32_ABI)
14519 return 4;
14520#endif
14521 return bfd_arch_bits_per_address (stdoutput) / 8;
14522}
14523
d2b2c203
DJ
14524int
14525i386_elf_section_type (const char *str, size_t len)
14526{
14527 if (flag_code == CODE_64BIT
14528 && len == sizeof ("unwind") - 1
d34049e8 14529 && startswith (str, "unwind"))
d2b2c203
DJ
14530 return SHT_X86_64_UNWIND;
14531
14532 return -1;
14533}
bb41ade5 14534
ad5fec3b
EB
14535#ifdef TE_SOLARIS
14536void
14537i386_solaris_fix_up_eh_frame (segT sec)
14538{
14539 if (flag_code == CODE_64BIT)
14540 elf_section_type (sec) = SHT_X86_64_UNWIND;
14541}
14542#endif
14543
bb41ade5
AM
14544#ifdef TE_PE
14545void
14546tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
14547{
91d6fa6a 14548 expressionS exp;
bb41ade5 14549
91d6fa6a
NC
14550 exp.X_op = O_secrel;
14551 exp.X_add_symbol = symbol;
14552 exp.X_add_number = 0;
14553 emit_expr (&exp, size);
bb41ade5
AM
14554}
14555#endif
3b22753a
L
14556
14557#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14558/* For ELF on x86-64, add support for SHF_X86_64_LARGE. */
14559
01e1a5bc 14560bfd_vma
6d4af3c2 14561x86_64_section_letter (int letter, const char **ptr_msg)
3b22753a
L
14562{
14563 if (flag_code == CODE_64BIT)
14564 {
14565 if (letter == 'l')
14566 return SHF_X86_64_LARGE;
14567
8f3bae45 14568 *ptr_msg = _("bad .section directive: want a,l,w,x,M,S,G,T in string");
64e74474 14569 }
3b22753a 14570 else
8f3bae45 14571 *ptr_msg = _("bad .section directive: want a,w,x,M,S,G,T in string");
3b22753a
L
14572 return -1;
14573}
14574
01e1a5bc 14575bfd_vma
3b22753a
L
14576x86_64_section_word (char *str, size_t len)
14577{
08dedd66 14578 if (len == 5 && flag_code == CODE_64BIT && startswith (str, "large"))
3b22753a
L
14579 return SHF_X86_64_LARGE;
14580
14581 return -1;
14582}
14583
14584static void
14585handle_large_common (int small ATTRIBUTE_UNUSED)
14586{
14587 if (flag_code != CODE_64BIT)
14588 {
14589 s_comm_internal (0, elf_common_parse);
14590 as_warn (_(".largecomm supported only in 64bit mode, producing .comm"));
14591 }
14592 else
14593 {
14594 static segT lbss_section;
14595 asection *saved_com_section_ptr = elf_com_section_ptr;
14596 asection *saved_bss_section = bss_section;
14597
14598 if (lbss_section == NULL)
14599 {
14600 flagword applicable;
14601 segT seg = now_seg;
14602 subsegT subseg = now_subseg;
14603
14604 /* The .lbss section is for local .largecomm symbols. */
14605 lbss_section = subseg_new (".lbss", 0);
14606 applicable = bfd_applicable_section_flags (stdoutput);
fd361982 14607 bfd_set_section_flags (lbss_section, applicable & SEC_ALLOC);
3b22753a
L
14608 seg_info (lbss_section)->bss = 1;
14609
14610 subseg_set (seg, subseg);
14611 }
14612
14613 elf_com_section_ptr = &_bfd_elf_large_com_section;
14614 bss_section = lbss_section;
14615
14616 s_comm_internal (0, elf_common_parse);
14617
14618 elf_com_section_ptr = saved_com_section_ptr;
14619 bss_section = saved_bss_section;
14620 }
14621}
14622#endif /* OBJ_ELF || OBJ_MAYBE_ELF */