]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/config/tc-i386.c
Add handler for more i386_cpu_flags
[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),
68830fba 1079 SUBARCH (amx_fp16, AMX_FP16, AMX_FP16, false),
ae89daec
JB
1080 SUBARCH (amx_tile, AMX_TILE, ANY_AMX_TILE, false),
1081 SUBARCH (movdiri, MOVDIRI, ANY_MOVDIRI, false),
1082 SUBARCH (movdir64b, MOVDIR64B, ANY_MOVDIR64B, false),
1083 SUBARCH (avx512_bf16, AVX512_BF16, ANY_AVX512_BF16, false),
1084 SUBARCH (avx512_vp2intersect, AVX512_VP2INTERSECT,
1085 ANY_AVX512_VP2INTERSECT, false),
1086 SUBARCH (tdx, TDX, ANY_TDX, false),
1087 SUBARCH (enqcmd, ENQCMD, ANY_ENQCMD, false),
1088 SUBARCH (serialize, SERIALIZE, ANY_SERIALIZE, false),
1089 SUBARCH (rdpru, RDPRU, RDPRU, false),
1090 SUBARCH (mcommit, MCOMMIT, MCOMMIT, false),
1091 SUBARCH (sev_es, SEV_ES, SEV_ES, false),
1092 SUBARCH (tsxldtrk, TSXLDTRK, ANY_TSXLDTRK, false),
1093 SUBARCH (kl, KL, ANY_KL, false),
1094 SUBARCH (widekl, WIDEKL, ANY_WIDEKL, false),
1095 SUBARCH (uintr, UINTR, ANY_UINTR, false),
1096 SUBARCH (hreset, HRESET, ANY_HRESET, false),
1097 SUBARCH (avx512_fp16, AVX512_FP16, ANY_AVX512_FP16, false),
ef07be45 1098 SUBARCH (prefetchi, PREFETCHI, PREFETCHI, false),
4321af3e 1099 SUBARCH (avx_ifma, AVX_IFMA, ANY_AVX_IFMA, false),
23ae61ad 1100 SUBARCH (avx_vnni_int8, AVX_VNNI_INT8, ANY_AVX_VNNI_INT8, false),
a93e3234 1101 SUBARCH (cmpccxadd, CMPCCXADD, ANY_CMPCCXADD, false),
293f5f65
L
1102};
1103
6ceeed25
JB
1104#undef SUBARCH
1105#undef ARCH
1106
704209c0 1107#ifdef I386COFF
a6c24e68
NC
1108/* Like s_lcomm_internal in gas/read.c but the alignment string
1109 is allowed to be optional. */
1110
1111static symbolS *
1112pe_lcomm_internal (int needs_align, symbolS *symbolP, addressT size)
1113{
1114 addressT align = 0;
1115
1116 SKIP_WHITESPACE ();
1117
7ab9ffdd 1118 if (needs_align
a6c24e68
NC
1119 && *input_line_pointer == ',')
1120 {
1121 align = parse_align (needs_align - 1);
7ab9ffdd 1122
a6c24e68
NC
1123 if (align == (addressT) -1)
1124 return NULL;
1125 }
1126 else
1127 {
1128 if (size >= 8)
1129 align = 3;
1130 else if (size >= 4)
1131 align = 2;
1132 else if (size >= 2)
1133 align = 1;
1134 else
1135 align = 0;
1136 }
1137
1138 bss_alloc (symbolP, size, align);
1139 return symbolP;
1140}
1141
704209c0 1142static void
a6c24e68
NC
1143pe_lcomm (int needs_align)
1144{
1145 s_comm_internal (needs_align * 2, pe_lcomm_internal);
1146}
704209c0 1147#endif
a6c24e68 1148
29b0f896
AM
1149const pseudo_typeS md_pseudo_table[] =
1150{
1151#if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
1152 {"align", s_align_bytes, 0},
1153#else
1154 {"align", s_align_ptwo, 0},
1155#endif
1156 {"arch", set_cpu_arch, 0},
1157#ifndef I386COFF
1158 {"bss", s_bss, 0},
a6c24e68
NC
1159#else
1160 {"lcomm", pe_lcomm, 1},
29b0f896
AM
1161#endif
1162 {"ffloat", float_cons, 'f'},
1163 {"dfloat", float_cons, 'd'},
1164 {"tfloat", float_cons, 'x'},
7d19d096 1165 {"hfloat", float_cons, 'h'},
de133cf9 1166 {"bfloat16", float_cons, 'b'},
29b0f896 1167 {"value", cons, 2},
d182319b 1168 {"slong", signed_cons, 4},
29b0f896
AM
1169 {"noopt", s_ignore, 0},
1170 {"optim", s_ignore, 0},
1171 {"code16gcc", set_16bit_gcc_code_flag, CODE_16BIT},
1172 {"code16", set_code_flag, CODE_16BIT},
1173 {"code32", set_code_flag, CODE_32BIT},
da5f19a2 1174#ifdef BFD64
29b0f896 1175 {"code64", set_code_flag, CODE_64BIT},
da5f19a2 1176#endif
29b0f896
AM
1177 {"intel_syntax", set_intel_syntax, 1},
1178 {"att_syntax", set_intel_syntax, 0},
1efbbeb4
L
1179 {"intel_mnemonic", set_intel_mnemonic, 1},
1180 {"att_mnemonic", set_intel_mnemonic, 0},
db51cc60
L
1181 {"allow_index_reg", set_allow_index_reg, 1},
1182 {"disallow_index_reg", set_allow_index_reg, 0},
7bab8ab5
JB
1183 {"sse_check", set_check, 0},
1184 {"operand_check", set_check, 1},
3b22753a
L
1185#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1186 {"largecomm", handle_large_common, 0},
07a53e5c 1187#else
68d20676 1188 {"file", dwarf2_directive_file, 0},
07a53e5c
RH
1189 {"loc", dwarf2_directive_loc, 0},
1190 {"loc_mark_labels", dwarf2_directive_loc_mark_labels, 0},
3b22753a 1191#endif
6482c264
NC
1192#ifdef TE_PE
1193 {"secrel32", pe_directive_secrel, 0},
145667f8 1194 {"secidx", pe_directive_secidx, 0},
6482c264 1195#endif
29b0f896
AM
1196 {0, 0, 0}
1197};
1198
1199/* For interface with expression (). */
1200extern char *input_line_pointer;
1201
1202/* Hash table for instruction mnemonic lookup. */
629310ab 1203static htab_t op_hash;
29b0f896
AM
1204
1205/* Hash table for register lookup. */
629310ab 1206static htab_t reg_hash;
29b0f896 1207\f
ce8a8b2f
AM
1208 /* Various efficient no-op patterns for aligning code labels.
1209 Note: Don't try to assemble the instructions in the comments.
1210 0L and 0w are not legal. */
62a02d25
L
1211static const unsigned char f32_1[] =
1212 {0x90}; /* nop */
1213static const unsigned char f32_2[] =
1214 {0x66,0x90}; /* xchg %ax,%ax */
1215static const unsigned char f32_3[] =
1216 {0x8d,0x76,0x00}; /* leal 0(%esi),%esi */
1217static const unsigned char f32_4[] =
1218 {0x8d,0x74,0x26,0x00}; /* leal 0(%esi,1),%esi */
62a02d25
L
1219static const unsigned char f32_6[] =
1220 {0x8d,0xb6,0x00,0x00,0x00,0x00}; /* leal 0L(%esi),%esi */
1221static const unsigned char f32_7[] =
1222 {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal 0L(%esi,1),%esi */
62a02d25 1223static const unsigned char f16_3[] =
3ae729d5 1224 {0x8d,0x74,0x00}; /* lea 0(%si),%si */
62a02d25 1225static const unsigned char f16_4[] =
3ae729d5
L
1226 {0x8d,0xb4,0x00,0x00}; /* lea 0W(%si),%si */
1227static const unsigned char jump_disp8[] =
1228 {0xeb}; /* jmp disp8 */
1229static const unsigned char jump32_disp32[] =
1230 {0xe9}; /* jmp disp32 */
1231static const unsigned char jump16_disp32[] =
1232 {0x66,0xe9}; /* jmp disp32 */
62a02d25
L
1233/* 32-bit NOPs patterns. */
1234static const unsigned char *const f32_patt[] = {
3ae729d5 1235 f32_1, f32_2, f32_3, f32_4, NULL, f32_6, f32_7
62a02d25
L
1236};
1237/* 16-bit NOPs patterns. */
1238static const unsigned char *const f16_patt[] = {
3ae729d5 1239 f32_1, f32_2, f16_3, f16_4
62a02d25
L
1240};
1241/* nopl (%[re]ax) */
1242static const unsigned char alt_3[] =
1243 {0x0f,0x1f,0x00};
1244/* nopl 0(%[re]ax) */
1245static const unsigned char alt_4[] =
1246 {0x0f,0x1f,0x40,0x00};
1247/* nopl 0(%[re]ax,%[re]ax,1) */
1248static const unsigned char alt_5[] =
1249 {0x0f,0x1f,0x44,0x00,0x00};
1250/* nopw 0(%[re]ax,%[re]ax,1) */
1251static const unsigned char alt_6[] =
1252 {0x66,0x0f,0x1f,0x44,0x00,0x00};
1253/* nopl 0L(%[re]ax) */
1254static const unsigned char alt_7[] =
1255 {0x0f,0x1f,0x80,0x00,0x00,0x00,0x00};
1256/* nopl 0L(%[re]ax,%[re]ax,1) */
1257static const unsigned char alt_8[] =
1258 {0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1259/* nopw 0L(%[re]ax,%[re]ax,1) */
1260static const unsigned char alt_9[] =
1261 {0x66,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1262/* nopw %cs:0L(%[re]ax,%[re]ax,1) */
1263static const unsigned char alt_10[] =
1264 {0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
3ae729d5
L
1265/* data16 nopw %cs:0L(%eax,%eax,1) */
1266static const unsigned char alt_11[] =
1267 {0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
62a02d25
L
1268/* 32-bit and 64-bit NOPs patterns. */
1269static const unsigned char *const alt_patt[] = {
1270 f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
3ae729d5 1271 alt_9, alt_10, alt_11
62a02d25
L
1272};
1273
1274/* Genenerate COUNT bytes of NOPs to WHERE from PATT with the maximum
1275 size of a single NOP instruction MAX_SINGLE_NOP_SIZE. */
1276
1277static void
1278i386_output_nops (char *where, const unsigned char *const *patt,
1279 int count, int max_single_nop_size)
1280
1281{
3ae729d5
L
1282 /* Place the longer NOP first. */
1283 int last;
1284 int offset;
3076e594
NC
1285 const unsigned char *nops;
1286
1287 if (max_single_nop_size < 1)
1288 {
1289 as_fatal (_("i386_output_nops called to generate nops of at most %d bytes!"),
1290 max_single_nop_size);
1291 return;
1292 }
1293
1294 nops = patt[max_single_nop_size - 1];
3ae729d5
L
1295
1296 /* Use the smaller one if the requsted one isn't available. */
1297 if (nops == NULL)
62a02d25 1298 {
3ae729d5
L
1299 max_single_nop_size--;
1300 nops = patt[max_single_nop_size - 1];
62a02d25
L
1301 }
1302
3ae729d5
L
1303 last = count % max_single_nop_size;
1304
1305 count -= last;
1306 for (offset = 0; offset < count; offset += max_single_nop_size)
1307 memcpy (where + offset, nops, max_single_nop_size);
1308
1309 if (last)
1310 {
1311 nops = patt[last - 1];
1312 if (nops == NULL)
1313 {
1314 /* Use the smaller one plus one-byte NOP if the needed one
1315 isn't available. */
1316 last--;
1317 nops = patt[last - 1];
1318 memcpy (where + offset, nops, last);
1319 where[offset + last] = *patt[0];
1320 }
1321 else
1322 memcpy (where + offset, nops, last);
1323 }
62a02d25
L
1324}
1325
3ae729d5
L
1326static INLINE int
1327fits_in_imm7 (offsetT num)
1328{
1329 return (num & 0x7f) == num;
1330}
1331
1332static INLINE int
1333fits_in_imm31 (offsetT num)
1334{
1335 return (num & 0x7fffffff) == num;
1336}
62a02d25
L
1337
1338/* Genenerate COUNT bytes of NOPs to WHERE with the maximum size of a
1339 single NOP instruction LIMIT. */
1340
1341void
3ae729d5 1342i386_generate_nops (fragS *fragP, char *where, offsetT count, int limit)
62a02d25 1343{
3ae729d5 1344 const unsigned char *const *patt = NULL;
62a02d25 1345 int max_single_nop_size;
3ae729d5
L
1346 /* Maximum number of NOPs before switching to jump over NOPs. */
1347 int max_number_of_nops;
62a02d25 1348
3ae729d5 1349 switch (fragP->fr_type)
62a02d25 1350 {
3ae729d5
L
1351 case rs_fill_nop:
1352 case rs_align_code:
1353 break;
e379e5f3
L
1354 case rs_machine_dependent:
1355 /* Allow NOP padding for jumps and calls. */
1356 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
1357 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
1358 break;
1359 /* Fall through. */
3ae729d5 1360 default:
62a02d25
L
1361 return;
1362 }
1363
ccc9c027
L
1364 /* We need to decide which NOP sequence to use for 32bit and
1365 64bit. When -mtune= is used:
4eed87de 1366
76bc74dc
L
1367 1. For PROCESSOR_I386, PROCESSOR_I486, PROCESSOR_PENTIUM and
1368 PROCESSOR_GENERIC32, f32_patt will be used.
80b8656c
L
1369 2. For the rest, alt_patt will be used.
1370
1371 When -mtune= isn't used, alt_patt will be used if
22109423 1372 cpu_arch_isa_flags has CpuNop. Otherwise, f32_patt will
76bc74dc 1373 be used.
ccc9c027
L
1374
1375 When -march= or .arch is used, we can't use anything beyond
1376 cpu_arch_isa_flags. */
1377
1378 if (flag_code == CODE_16BIT)
1379 {
3ae729d5
L
1380 patt = f16_patt;
1381 max_single_nop_size = sizeof (f16_patt) / sizeof (f16_patt[0]);
1382 /* Limit number of NOPs to 2 in 16-bit mode. */
1383 max_number_of_nops = 2;
252b5132 1384 }
33fef721 1385 else
ccc9c027 1386 {
fbf3f584 1387 if (fragP->tc_frag_data.isa == PROCESSOR_UNKNOWN)
ccc9c027
L
1388 {
1389 /* PROCESSOR_UNKNOWN means that all ISAs may be used. */
1390 switch (cpu_arch_tune)
1391 {
1392 case PROCESSOR_UNKNOWN:
1393 /* We use cpu_arch_isa_flags to check if we SHOULD
22109423
L
1394 optimize with nops. */
1395 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1396 patt = alt_patt;
ccc9c027
L
1397 else
1398 patt = f32_patt;
1399 break;
ccc9c027
L
1400 case PROCESSOR_PENTIUM4:
1401 case PROCESSOR_NOCONA:
ef05d495 1402 case PROCESSOR_CORE:
76bc74dc 1403 case PROCESSOR_CORE2:
bd5295b2 1404 case PROCESSOR_COREI7:
76bc74dc 1405 case PROCESSOR_GENERIC64:
ccc9c027
L
1406 case PROCESSOR_K6:
1407 case PROCESSOR_ATHLON:
1408 case PROCESSOR_K8:
4eed87de 1409 case PROCESSOR_AMDFAM10:
8aedb9fe 1410 case PROCESSOR_BD:
029f3522 1411 case PROCESSOR_ZNVER:
7b458c12 1412 case PROCESSOR_BT:
80b8656c 1413 patt = alt_patt;
ccc9c027 1414 break;
76bc74dc 1415 case PROCESSOR_I386:
ccc9c027
L
1416 case PROCESSOR_I486:
1417 case PROCESSOR_PENTIUM:
2dde1948 1418 case PROCESSOR_PENTIUMPRO:
81486035 1419 case PROCESSOR_IAMCU:
ccc9c027
L
1420 case PROCESSOR_GENERIC32:
1421 patt = f32_patt;
1422 break;
c368d2a8
JB
1423 case PROCESSOR_NONE:
1424 abort ();
4eed87de 1425 }
ccc9c027
L
1426 }
1427 else
1428 {
fbf3f584 1429 switch (fragP->tc_frag_data.tune)
ccc9c027
L
1430 {
1431 case PROCESSOR_UNKNOWN:
e6a14101 1432 /* When cpu_arch_isa is set, cpu_arch_tune shouldn't be
ccc9c027
L
1433 PROCESSOR_UNKNOWN. */
1434 abort ();
1435 break;
1436
76bc74dc 1437 case PROCESSOR_I386:
ccc9c027
L
1438 case PROCESSOR_I486:
1439 case PROCESSOR_PENTIUM:
81486035 1440 case PROCESSOR_IAMCU:
ccc9c027
L
1441 case PROCESSOR_K6:
1442 case PROCESSOR_ATHLON:
1443 case PROCESSOR_K8:
4eed87de 1444 case PROCESSOR_AMDFAM10:
8aedb9fe 1445 case PROCESSOR_BD:
029f3522 1446 case PROCESSOR_ZNVER:
7b458c12 1447 case PROCESSOR_BT:
ccc9c027
L
1448 case PROCESSOR_GENERIC32:
1449 /* We use cpu_arch_isa_flags to check if we CAN optimize
22109423
L
1450 with nops. */
1451 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1452 patt = alt_patt;
ccc9c027
L
1453 else
1454 patt = f32_patt;
1455 break;
76bc74dc
L
1456 case PROCESSOR_PENTIUMPRO:
1457 case PROCESSOR_PENTIUM4:
1458 case PROCESSOR_NOCONA:
1459 case PROCESSOR_CORE:
ef05d495 1460 case PROCESSOR_CORE2:
bd5295b2 1461 case PROCESSOR_COREI7:
22109423 1462 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1463 patt = alt_patt;
ccc9c027
L
1464 else
1465 patt = f32_patt;
1466 break;
1467 case PROCESSOR_GENERIC64:
80b8656c 1468 patt = alt_patt;
ccc9c027 1469 break;
c368d2a8
JB
1470 case PROCESSOR_NONE:
1471 abort ();
4eed87de 1472 }
ccc9c027
L
1473 }
1474
76bc74dc
L
1475 if (patt == f32_patt)
1476 {
3ae729d5
L
1477 max_single_nop_size = sizeof (f32_patt) / sizeof (f32_patt[0]);
1478 /* Limit number of NOPs to 2 for older processors. */
1479 max_number_of_nops = 2;
76bc74dc
L
1480 }
1481 else
1482 {
3ae729d5
L
1483 max_single_nop_size = sizeof (alt_patt) / sizeof (alt_patt[0]);
1484 /* Limit number of NOPs to 7 for newer processors. */
1485 max_number_of_nops = 7;
1486 }
1487 }
1488
1489 if (limit == 0)
1490 limit = max_single_nop_size;
1491
1492 if (fragP->fr_type == rs_fill_nop)
1493 {
1494 /* Output NOPs for .nop directive. */
1495 if (limit > max_single_nop_size)
1496 {
1497 as_bad_where (fragP->fr_file, fragP->fr_line,
1498 _("invalid single nop size: %d "
1499 "(expect within [0, %d])"),
1500 limit, max_single_nop_size);
1501 return;
1502 }
1503 }
e379e5f3 1504 else if (fragP->fr_type != rs_machine_dependent)
3ae729d5
L
1505 fragP->fr_var = count;
1506
1507 if ((count / max_single_nop_size) > max_number_of_nops)
1508 {
1509 /* Generate jump over NOPs. */
1510 offsetT disp = count - 2;
1511 if (fits_in_imm7 (disp))
1512 {
1513 /* Use "jmp disp8" if possible. */
1514 count = disp;
1515 where[0] = jump_disp8[0];
1516 where[1] = count;
1517 where += 2;
1518 }
1519 else
1520 {
1521 unsigned int size_of_jump;
1522
1523 if (flag_code == CODE_16BIT)
1524 {
1525 where[0] = jump16_disp32[0];
1526 where[1] = jump16_disp32[1];
1527 size_of_jump = 2;
1528 }
1529 else
1530 {
1531 where[0] = jump32_disp32[0];
1532 size_of_jump = 1;
1533 }
1534
1535 count -= size_of_jump + 4;
1536 if (!fits_in_imm31 (count))
1537 {
1538 as_bad_where (fragP->fr_file, fragP->fr_line,
1539 _("jump over nop padding out of range"));
1540 return;
1541 }
1542
1543 md_number_to_chars (where + size_of_jump, count, 4);
1544 where += size_of_jump + 4;
76bc74dc 1545 }
ccc9c027 1546 }
3ae729d5
L
1547
1548 /* Generate multiple NOPs. */
1549 i386_output_nops (where, patt, count, limit);
252b5132
RH
1550}
1551
c6fb90c8 1552static INLINE int
0dfbf9d7 1553operand_type_all_zero (const union i386_operand_type *x)
40fb9820 1554{
0dfbf9d7 1555 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1556 {
1557 case 3:
0dfbf9d7 1558 if (x->array[2])
c6fb90c8 1559 return 0;
1a0670f3 1560 /* Fall through. */
c6fb90c8 1561 case 2:
0dfbf9d7 1562 if (x->array[1])
c6fb90c8 1563 return 0;
1a0670f3 1564 /* Fall through. */
c6fb90c8 1565 case 1:
0dfbf9d7 1566 return !x->array[0];
c6fb90c8
L
1567 default:
1568 abort ();
1569 }
40fb9820
L
1570}
1571
c6fb90c8 1572static INLINE void
0dfbf9d7 1573operand_type_set (union i386_operand_type *x, unsigned int v)
40fb9820 1574{
0dfbf9d7 1575 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1576 {
1577 case 3:
0dfbf9d7 1578 x->array[2] = v;
1a0670f3 1579 /* Fall through. */
c6fb90c8 1580 case 2:
0dfbf9d7 1581 x->array[1] = v;
1a0670f3 1582 /* Fall through. */
c6fb90c8 1583 case 1:
0dfbf9d7 1584 x->array[0] = v;
1a0670f3 1585 /* Fall through. */
c6fb90c8
L
1586 break;
1587 default:
1588 abort ();
1589 }
bab6aec1
JB
1590
1591 x->bitfield.class = ClassNone;
75e5731b 1592 x->bitfield.instance = InstanceNone;
c6fb90c8 1593}
40fb9820 1594
c6fb90c8 1595static INLINE int
0dfbf9d7
L
1596operand_type_equal (const union i386_operand_type *x,
1597 const union i386_operand_type *y)
c6fb90c8 1598{
0dfbf9d7 1599 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1600 {
1601 case 3:
0dfbf9d7 1602 if (x->array[2] != y->array[2])
c6fb90c8 1603 return 0;
1a0670f3 1604 /* Fall through. */
c6fb90c8 1605 case 2:
0dfbf9d7 1606 if (x->array[1] != y->array[1])
c6fb90c8 1607 return 0;
1a0670f3 1608 /* Fall through. */
c6fb90c8 1609 case 1:
0dfbf9d7 1610 return x->array[0] == y->array[0];
c6fb90c8
L
1611 break;
1612 default:
1613 abort ();
1614 }
1615}
40fb9820 1616
0dfbf9d7
L
1617static INLINE int
1618cpu_flags_all_zero (const union i386_cpu_flags *x)
1619{
1620 switch (ARRAY_SIZE(x->array))
1621 {
75f8266a
KL
1622 case 5:
1623 if (x->array[4])
1624 return 0;
1625 /* Fall through. */
53467f57
IT
1626 case 4:
1627 if (x->array[3])
1628 return 0;
1629 /* Fall through. */
0dfbf9d7
L
1630 case 3:
1631 if (x->array[2])
1632 return 0;
1a0670f3 1633 /* Fall through. */
0dfbf9d7
L
1634 case 2:
1635 if (x->array[1])
1636 return 0;
1a0670f3 1637 /* Fall through. */
0dfbf9d7
L
1638 case 1:
1639 return !x->array[0];
1640 default:
1641 abort ();
1642 }
1643}
1644
0dfbf9d7
L
1645static INLINE int
1646cpu_flags_equal (const union i386_cpu_flags *x,
1647 const union i386_cpu_flags *y)
1648{
1649 switch (ARRAY_SIZE(x->array))
1650 {
75f8266a
KL
1651 case 5:
1652 if (x->array[4] != y->array[4])
1653 return 0;
1654 /* Fall through. */
53467f57
IT
1655 case 4:
1656 if (x->array[3] != y->array[3])
1657 return 0;
1658 /* Fall through. */
0dfbf9d7
L
1659 case 3:
1660 if (x->array[2] != y->array[2])
1661 return 0;
1a0670f3 1662 /* Fall through. */
0dfbf9d7
L
1663 case 2:
1664 if (x->array[1] != y->array[1])
1665 return 0;
1a0670f3 1666 /* Fall through. */
0dfbf9d7
L
1667 case 1:
1668 return x->array[0] == y->array[0];
1669 break;
1670 default:
1671 abort ();
1672 }
1673}
c6fb90c8
L
1674
1675static INLINE int
1676cpu_flags_check_cpu64 (i386_cpu_flags f)
1677{
1678 return !((flag_code == CODE_64BIT && f.bitfield.cpuno64)
1679 || (flag_code != CODE_64BIT && f.bitfield.cpu64));
40fb9820
L
1680}
1681
c6fb90c8
L
1682static INLINE i386_cpu_flags
1683cpu_flags_and (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1684{
c6fb90c8
L
1685 switch (ARRAY_SIZE (x.array))
1686 {
75f8266a
KL
1687 case 5:
1688 x.array [4] &= y.array [4];
1689 /* Fall through. */
53467f57
IT
1690 case 4:
1691 x.array [3] &= y.array [3];
1692 /* Fall through. */
c6fb90c8
L
1693 case 3:
1694 x.array [2] &= y.array [2];
1a0670f3 1695 /* Fall through. */
c6fb90c8
L
1696 case 2:
1697 x.array [1] &= y.array [1];
1a0670f3 1698 /* Fall through. */
c6fb90c8
L
1699 case 1:
1700 x.array [0] &= y.array [0];
1701 break;
1702 default:
1703 abort ();
1704 }
1705 return x;
1706}
40fb9820 1707
c6fb90c8
L
1708static INLINE i386_cpu_flags
1709cpu_flags_or (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1710{
c6fb90c8 1711 switch (ARRAY_SIZE (x.array))
40fb9820 1712 {
75f8266a
KL
1713 case 5:
1714 x.array [4] |= y.array [4];
1715 /* Fall through. */
53467f57
IT
1716 case 4:
1717 x.array [3] |= y.array [3];
1718 /* Fall through. */
c6fb90c8
L
1719 case 3:
1720 x.array [2] |= y.array [2];
1a0670f3 1721 /* Fall through. */
c6fb90c8
L
1722 case 2:
1723 x.array [1] |= y.array [1];
1a0670f3 1724 /* Fall through. */
c6fb90c8
L
1725 case 1:
1726 x.array [0] |= y.array [0];
40fb9820
L
1727 break;
1728 default:
1729 abort ();
1730 }
40fb9820
L
1731 return x;
1732}
1733
309d3373
JB
1734static INLINE i386_cpu_flags
1735cpu_flags_and_not (i386_cpu_flags x, i386_cpu_flags y)
1736{
1737 switch (ARRAY_SIZE (x.array))
1738 {
75f8266a
KL
1739 case 5:
1740 x.array [4] &= ~y.array [4];
1741 /* Fall through. */
53467f57
IT
1742 case 4:
1743 x.array [3] &= ~y.array [3];
1744 /* Fall through. */
309d3373
JB
1745 case 3:
1746 x.array [2] &= ~y.array [2];
1a0670f3 1747 /* Fall through. */
309d3373
JB
1748 case 2:
1749 x.array [1] &= ~y.array [1];
1a0670f3 1750 /* Fall through. */
309d3373
JB
1751 case 1:
1752 x.array [0] &= ~y.array [0];
1753 break;
1754 default:
1755 abort ();
1756 }
1757 return x;
1758}
1759
6c0946d0
JB
1760static const i386_cpu_flags avx512 = CPU_ANY_AVX512F_FLAGS;
1761
c0f3af97
L
1762#define CPU_FLAGS_ARCH_MATCH 0x1
1763#define CPU_FLAGS_64BIT_MATCH 0x2
1764
c0f3af97 1765#define CPU_FLAGS_PERFECT_MATCH \
db12e14e 1766 (CPU_FLAGS_ARCH_MATCH | CPU_FLAGS_64BIT_MATCH)
c0f3af97
L
1767
1768/* Return CPU flags match bits. */
3629bb00 1769
40fb9820 1770static int
d3ce72d0 1771cpu_flags_match (const insn_template *t)
40fb9820 1772{
c0f3af97
L
1773 i386_cpu_flags x = t->cpu_flags;
1774 int match = cpu_flags_check_cpu64 (x) ? CPU_FLAGS_64BIT_MATCH : 0;
40fb9820
L
1775
1776 x.bitfield.cpu64 = 0;
1777 x.bitfield.cpuno64 = 0;
1778
0dfbf9d7 1779 if (cpu_flags_all_zero (&x))
c0f3af97
L
1780 {
1781 /* This instruction is available on all archs. */
db12e14e 1782 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 1783 }
3629bb00
L
1784 else
1785 {
c0f3af97 1786 /* This instruction is available only on some archs. */
3629bb00
L
1787 i386_cpu_flags cpu = cpu_arch_flags;
1788
ab592e75
JB
1789 /* AVX512VL is no standalone feature - match it and then strip it. */
1790 if (x.bitfield.cpuavx512vl && !cpu.bitfield.cpuavx512vl)
1791 return match;
1792 x.bitfield.cpuavx512vl = 0;
1793
22c36940
JB
1794 /* AVX and AVX2 present at the same time express an operand size
1795 dependency - strip AVX2 for the purposes here. The operand size
1796 dependent check occurs in check_vecOperands(). */
1797 if (x.bitfield.cpuavx && x.bitfield.cpuavx2)
1798 x.bitfield.cpuavx2 = 0;
1799
3629bb00 1800 cpu = cpu_flags_and (x, cpu);
c0f3af97
L
1801 if (!cpu_flags_all_zero (&cpu))
1802 {
57392598 1803 if (x.bitfield.cpuavx)
a5ff0eb2 1804 {
929f69fa 1805 /* We need to check a few extra flags with AVX. */
b9d49817 1806 if (cpu.bitfield.cpuavx
40d231b4
JB
1807 && (!t->opcode_modifier.sse2avx
1808 || (sse2avx && !i.prefix[DATA_PREFIX]))
b9d49817 1809 && (!x.bitfield.cpuaes || cpu.bitfield.cpuaes)
929f69fa 1810 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
b9d49817
JB
1811 && (!x.bitfield.cpupclmul || cpu.bitfield.cpupclmul))
1812 match |= CPU_FLAGS_ARCH_MATCH;
a5ff0eb2 1813 }
929f69fa
JB
1814 else if (x.bitfield.cpuavx512f)
1815 {
1816 /* We need to check a few extra flags with AVX512F. */
1817 if (cpu.bitfield.cpuavx512f
1818 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
1819 && (!x.bitfield.cpuvaes || cpu.bitfield.cpuvaes)
1820 && (!x.bitfield.cpuvpclmulqdq || cpu.bitfield.cpuvpclmulqdq))
1821 match |= CPU_FLAGS_ARCH_MATCH;
1822 }
a5ff0eb2 1823 else
db12e14e 1824 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 1825 }
3629bb00 1826 }
c0f3af97 1827 return match;
40fb9820
L
1828}
1829
c6fb90c8
L
1830static INLINE i386_operand_type
1831operand_type_and (i386_operand_type x, i386_operand_type y)
40fb9820 1832{
bab6aec1
JB
1833 if (x.bitfield.class != y.bitfield.class)
1834 x.bitfield.class = ClassNone;
75e5731b
JB
1835 if (x.bitfield.instance != y.bitfield.instance)
1836 x.bitfield.instance = InstanceNone;
bab6aec1 1837
c6fb90c8
L
1838 switch (ARRAY_SIZE (x.array))
1839 {
1840 case 3:
1841 x.array [2] &= y.array [2];
1a0670f3 1842 /* Fall through. */
c6fb90c8
L
1843 case 2:
1844 x.array [1] &= y.array [1];
1a0670f3 1845 /* Fall through. */
c6fb90c8
L
1846 case 1:
1847 x.array [0] &= y.array [0];
1848 break;
1849 default:
1850 abort ();
1851 }
1852 return x;
40fb9820
L
1853}
1854
73053c1f
JB
1855static INLINE i386_operand_type
1856operand_type_and_not (i386_operand_type x, i386_operand_type y)
1857{
bab6aec1 1858 gas_assert (y.bitfield.class == ClassNone);
75e5731b 1859 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 1860
73053c1f
JB
1861 switch (ARRAY_SIZE (x.array))
1862 {
1863 case 3:
1864 x.array [2] &= ~y.array [2];
1865 /* Fall through. */
1866 case 2:
1867 x.array [1] &= ~y.array [1];
1868 /* Fall through. */
1869 case 1:
1870 x.array [0] &= ~y.array [0];
1871 break;
1872 default:
1873 abort ();
1874 }
1875 return x;
1876}
1877
c6fb90c8
L
1878static INLINE i386_operand_type
1879operand_type_or (i386_operand_type x, i386_operand_type y)
40fb9820 1880{
bab6aec1
JB
1881 gas_assert (x.bitfield.class == ClassNone ||
1882 y.bitfield.class == ClassNone ||
1883 x.bitfield.class == y.bitfield.class);
75e5731b
JB
1884 gas_assert (x.bitfield.instance == InstanceNone ||
1885 y.bitfield.instance == InstanceNone ||
1886 x.bitfield.instance == y.bitfield.instance);
bab6aec1 1887
c6fb90c8 1888 switch (ARRAY_SIZE (x.array))
40fb9820 1889 {
c6fb90c8
L
1890 case 3:
1891 x.array [2] |= y.array [2];
1a0670f3 1892 /* Fall through. */
c6fb90c8
L
1893 case 2:
1894 x.array [1] |= y.array [1];
1a0670f3 1895 /* Fall through. */
c6fb90c8
L
1896 case 1:
1897 x.array [0] |= y.array [0];
40fb9820
L
1898 break;
1899 default:
1900 abort ();
1901 }
c6fb90c8
L
1902 return x;
1903}
40fb9820 1904
c6fb90c8
L
1905static INLINE i386_operand_type
1906operand_type_xor (i386_operand_type x, i386_operand_type y)
1907{
bab6aec1 1908 gas_assert (y.bitfield.class == ClassNone);
75e5731b 1909 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 1910
c6fb90c8
L
1911 switch (ARRAY_SIZE (x.array))
1912 {
1913 case 3:
1914 x.array [2] ^= y.array [2];
1a0670f3 1915 /* Fall through. */
c6fb90c8
L
1916 case 2:
1917 x.array [1] ^= y.array [1];
1a0670f3 1918 /* Fall through. */
c6fb90c8
L
1919 case 1:
1920 x.array [0] ^= y.array [0];
1921 break;
1922 default:
1923 abort ();
1924 }
40fb9820
L
1925 return x;
1926}
1927
40fb9820 1928static const i386_operand_type disp16_32 = OPERAND_TYPE_DISP16_32;
bab6aec1 1929static const i386_operand_type anydisp = OPERAND_TYPE_ANYDISP;
40fb9820
L
1930static const i386_operand_type regxmm = OPERAND_TYPE_REGXMM;
1931static const i386_operand_type imm8 = OPERAND_TYPE_IMM8;
1932static const i386_operand_type imm8s = OPERAND_TYPE_IMM8S;
1933static const i386_operand_type imm16 = OPERAND_TYPE_IMM16;
1934static const i386_operand_type imm32 = OPERAND_TYPE_IMM32;
1935static const i386_operand_type imm32s = OPERAND_TYPE_IMM32S;
1936static const i386_operand_type imm64 = OPERAND_TYPE_IMM64;
1937static const i386_operand_type imm16_32 = OPERAND_TYPE_IMM16_32;
1938static const i386_operand_type imm16_32s = OPERAND_TYPE_IMM16_32S;
1939static const i386_operand_type imm16_32_32s = OPERAND_TYPE_IMM16_32_32S;
1940
1941enum operand_type
1942{
1943 reg,
40fb9820
L
1944 imm,
1945 disp,
1946 anymem
1947};
1948
c6fb90c8 1949static INLINE int
40fb9820
L
1950operand_type_check (i386_operand_type t, enum operand_type c)
1951{
1952 switch (c)
1953 {
1954 case reg:
bab6aec1 1955 return t.bitfield.class == Reg;
40fb9820 1956
40fb9820
L
1957 case imm:
1958 return (t.bitfield.imm8
1959 || t.bitfield.imm8s
1960 || t.bitfield.imm16
1961 || t.bitfield.imm32
1962 || t.bitfield.imm32s
1963 || t.bitfield.imm64);
1964
1965 case disp:
1966 return (t.bitfield.disp8
1967 || t.bitfield.disp16
1968 || t.bitfield.disp32
40fb9820
L
1969 || t.bitfield.disp64);
1970
1971 case anymem:
1972 return (t.bitfield.disp8
1973 || t.bitfield.disp16
1974 || t.bitfield.disp32
40fb9820
L
1975 || t.bitfield.disp64
1976 || t.bitfield.baseindex);
1977
1978 default:
1979 abort ();
1980 }
2cfe26b6
AM
1981
1982 return 0;
40fb9820
L
1983}
1984
7a54636a
L
1985/* Return 1 if there is no conflict in 8bit/16bit/32bit/64bit/80bit size
1986 between operand GIVEN and opeand WANTED for instruction template T. */
5c07affc
L
1987
1988static INLINE int
7a54636a
L
1989match_operand_size (const insn_template *t, unsigned int wanted,
1990 unsigned int given)
5c07affc 1991{
3ac21baa
JB
1992 return !((i.types[given].bitfield.byte
1993 && !t->operand_types[wanted].bitfield.byte)
1994 || (i.types[given].bitfield.word
1995 && !t->operand_types[wanted].bitfield.word)
1996 || (i.types[given].bitfield.dword
1997 && !t->operand_types[wanted].bitfield.dword)
1998 || (i.types[given].bitfield.qword
1999 && !t->operand_types[wanted].bitfield.qword)
2000 || (i.types[given].bitfield.tbyte
2001 && !t->operand_types[wanted].bitfield.tbyte));
5c07affc
L
2002}
2003
dd40ce22
L
2004/* Return 1 if there is no conflict in SIMD register between operand
2005 GIVEN and opeand WANTED for instruction template T. */
1b54b8d7
JB
2006
2007static INLINE int
dd40ce22
L
2008match_simd_size (const insn_template *t, unsigned int wanted,
2009 unsigned int given)
1b54b8d7 2010{
3ac21baa
JB
2011 return !((i.types[given].bitfield.xmmword
2012 && !t->operand_types[wanted].bitfield.xmmword)
2013 || (i.types[given].bitfield.ymmword
2014 && !t->operand_types[wanted].bitfield.ymmword)
2015 || (i.types[given].bitfield.zmmword
260cd341
LC
2016 && !t->operand_types[wanted].bitfield.zmmword)
2017 || (i.types[given].bitfield.tmmword
2018 && !t->operand_types[wanted].bitfield.tmmword));
1b54b8d7
JB
2019}
2020
7a54636a
L
2021/* Return 1 if there is no conflict in any size between operand GIVEN
2022 and opeand WANTED for instruction template T. */
5c07affc
L
2023
2024static INLINE int
dd40ce22
L
2025match_mem_size (const insn_template *t, unsigned int wanted,
2026 unsigned int given)
5c07affc 2027{
7a54636a 2028 return (match_operand_size (t, wanted, given)
3ac21baa 2029 && !((i.types[given].bitfield.unspecified
5273a3cd 2030 && !i.broadcast.type
a5748e0d 2031 && !i.broadcast.bytes
3ac21baa
JB
2032 && !t->operand_types[wanted].bitfield.unspecified)
2033 || (i.types[given].bitfield.fword
2034 && !t->operand_types[wanted].bitfield.fword)
1b54b8d7
JB
2035 /* For scalar opcode templates to allow register and memory
2036 operands at the same time, some special casing is needed
d6793fa1
JB
2037 here. Also for v{,p}broadcast*, {,v}pmov{s,z}*, and
2038 down-conversion vpmov*. */
3528c362 2039 || ((t->operand_types[wanted].bitfield.class == RegSIMD
bc49bfd8
JB
2040 && t->operand_types[wanted].bitfield.byte
2041 + t->operand_types[wanted].bitfield.word
2042 + t->operand_types[wanted].bitfield.dword
2043 + t->operand_types[wanted].bitfield.qword
2044 > !!t->opcode_modifier.broadcast)
3ac21baa
JB
2045 ? (i.types[given].bitfield.xmmword
2046 || i.types[given].bitfield.ymmword
2047 || i.types[given].bitfield.zmmword)
2048 : !match_simd_size(t, wanted, given))));
5c07affc
L
2049}
2050
3ac21baa
JB
2051/* Return value has MATCH_STRAIGHT set if there is no size conflict on any
2052 operands for instruction template T, and it has MATCH_REVERSE set if there
2053 is no size conflict on any operands for the template with operands reversed
2054 (and the template allows for reversing in the first place). */
5c07affc 2055
3ac21baa
JB
2056#define MATCH_STRAIGHT 1
2057#define MATCH_REVERSE 2
2058
2059static INLINE unsigned int
d3ce72d0 2060operand_size_match (const insn_template *t)
5c07affc 2061{
3ac21baa 2062 unsigned int j, match = MATCH_STRAIGHT;
5c07affc 2063
0cfa3eb3 2064 /* Don't check non-absolute jump instructions. */
5c07affc 2065 if (t->opcode_modifier.jump
0cfa3eb3 2066 && t->opcode_modifier.jump != JUMP_ABSOLUTE)
5c07affc
L
2067 return match;
2068
2069 /* Check memory and accumulator operand size. */
2070 for (j = 0; j < i.operands; j++)
2071 {
3528c362
JB
2072 if (i.types[j].bitfield.class != Reg
2073 && i.types[j].bitfield.class != RegSIMD
601e8564 2074 && t->opcode_modifier.anysize)
5c07affc
L
2075 continue;
2076
bab6aec1 2077 if (t->operand_types[j].bitfield.class == Reg
7a54636a 2078 && !match_operand_size (t, j, j))
5c07affc
L
2079 {
2080 match = 0;
2081 break;
2082 }
2083
3528c362 2084 if (t->operand_types[j].bitfield.class == RegSIMD
3ac21baa 2085 && !match_simd_size (t, j, j))
1b54b8d7
JB
2086 {
2087 match = 0;
2088 break;
2089 }
2090
75e5731b 2091 if (t->operand_types[j].bitfield.instance == Accum
7a54636a 2092 && (!match_operand_size (t, j, j) || !match_simd_size (t, j, j)))
1b54b8d7
JB
2093 {
2094 match = 0;
2095 break;
2096 }
2097
c48dadc9 2098 if ((i.flags[j] & Operand_Mem) && !match_mem_size (t, j, j))
5c07affc
L
2099 {
2100 match = 0;
2101 break;
2102 }
2103 }
2104
3ac21baa 2105 if (!t->opcode_modifier.d)
7b94647a 2106 return match;
5c07affc
L
2107
2108 /* Check reverse. */
8bd915b7
JB
2109 gas_assert ((i.operands >= 2 && i.operands <= 3)
2110 || t->opcode_modifier.vexsources);
5c07affc 2111
f5eb1d70 2112 for (j = 0; j < i.operands; j++)
5c07affc 2113 {
f5eb1d70
JB
2114 unsigned int given = i.operands - j - 1;
2115
8bd915b7
JB
2116 /* For 4- and 5-operand insns VEX.W controls just the first two
2117 register operands. */
2118 if (t->opcode_modifier.vexsources)
2119 given = j < 2 ? 1 - j : j;
2120
bab6aec1 2121 if (t->operand_types[j].bitfield.class == Reg
f5eb1d70 2122 && !match_operand_size (t, j, given))
7b94647a 2123 return match;
5c07affc 2124
3528c362 2125 if (t->operand_types[j].bitfield.class == RegSIMD
f5eb1d70 2126 && !match_simd_size (t, j, given))
7b94647a 2127 return match;
dbbc8b7e 2128
75e5731b 2129 if (t->operand_types[j].bitfield.instance == Accum
f5eb1d70
JB
2130 && (!match_operand_size (t, j, given)
2131 || !match_simd_size (t, j, given)))
7b94647a 2132 return match;
dbbc8b7e 2133
f5eb1d70 2134 if ((i.flags[given] & Operand_Mem) && !match_mem_size (t, j, given))
7b94647a 2135 return match;
5c07affc
L
2136 }
2137
3ac21baa 2138 return match | MATCH_REVERSE;
5c07affc
L
2139}
2140
c6fb90c8 2141static INLINE int
40fb9820
L
2142operand_type_match (i386_operand_type overlap,
2143 i386_operand_type given)
2144{
2145 i386_operand_type temp = overlap;
2146
7d5e4556 2147 temp.bitfield.unspecified = 0;
5c07affc
L
2148 temp.bitfield.byte = 0;
2149 temp.bitfield.word = 0;
2150 temp.bitfield.dword = 0;
2151 temp.bitfield.fword = 0;
2152 temp.bitfield.qword = 0;
2153 temp.bitfield.tbyte = 0;
2154 temp.bitfield.xmmword = 0;
c0f3af97 2155 temp.bitfield.ymmword = 0;
43234a1e 2156 temp.bitfield.zmmword = 0;
260cd341 2157 temp.bitfield.tmmword = 0;
0dfbf9d7 2158 if (operand_type_all_zero (&temp))
891edac4 2159 goto mismatch;
40fb9820 2160
6f2f06be 2161 if (given.bitfield.baseindex == overlap.bitfield.baseindex)
891edac4
L
2162 return 1;
2163
dc1e8a47 2164 mismatch:
a65babc9 2165 i.error = operand_type_mismatch;
891edac4 2166 return 0;
40fb9820
L
2167}
2168
7d5e4556 2169/* If given types g0 and g1 are registers they must be of the same type
10c17abd 2170 unless the expected operand type register overlap is null.
5de4d9ef 2171 Some Intel syntax memory operand size checking also happens here. */
40fb9820 2172
c6fb90c8 2173static INLINE int
dc821c5f 2174operand_type_register_match (i386_operand_type g0,
40fb9820 2175 i386_operand_type t0,
40fb9820
L
2176 i386_operand_type g1,
2177 i386_operand_type t1)
2178{
bab6aec1 2179 if (g0.bitfield.class != Reg
3528c362 2180 && g0.bitfield.class != RegSIMD
10c17abd
JB
2181 && (!operand_type_check (g0, anymem)
2182 || g0.bitfield.unspecified
5de4d9ef
JB
2183 || (t0.bitfield.class != Reg
2184 && t0.bitfield.class != RegSIMD)))
40fb9820
L
2185 return 1;
2186
bab6aec1 2187 if (g1.bitfield.class != Reg
3528c362 2188 && g1.bitfield.class != RegSIMD
10c17abd
JB
2189 && (!operand_type_check (g1, anymem)
2190 || g1.bitfield.unspecified
5de4d9ef
JB
2191 || (t1.bitfield.class != Reg
2192 && t1.bitfield.class != RegSIMD)))
40fb9820
L
2193 return 1;
2194
dc821c5f
JB
2195 if (g0.bitfield.byte == g1.bitfield.byte
2196 && g0.bitfield.word == g1.bitfield.word
2197 && g0.bitfield.dword == g1.bitfield.dword
10c17abd
JB
2198 && g0.bitfield.qword == g1.bitfield.qword
2199 && g0.bitfield.xmmword == g1.bitfield.xmmword
2200 && g0.bitfield.ymmword == g1.bitfield.ymmword
2201 && g0.bitfield.zmmword == g1.bitfield.zmmword)
40fb9820
L
2202 return 1;
2203
c4d09633
JB
2204 /* If expectations overlap in no more than a single size, all is fine. */
2205 g0 = operand_type_and (t0, t1);
2206 if (g0.bitfield.byte
2207 + g0.bitfield.word
2208 + g0.bitfield.dword
2209 + g0.bitfield.qword
2210 + g0.bitfield.xmmword
2211 + g0.bitfield.ymmword
2212 + g0.bitfield.zmmword <= 1)
891edac4
L
2213 return 1;
2214
a65babc9 2215 i.error = register_type_mismatch;
891edac4
L
2216
2217 return 0;
40fb9820
L
2218}
2219
4c692bc7
JB
2220static INLINE unsigned int
2221register_number (const reg_entry *r)
2222{
2223 unsigned int nr = r->reg_num;
2224
2225 if (r->reg_flags & RegRex)
2226 nr += 8;
2227
200cbe0f
L
2228 if (r->reg_flags & RegVRex)
2229 nr += 16;
2230
4c692bc7
JB
2231 return nr;
2232}
2233
252b5132 2234static INLINE unsigned int
40fb9820 2235mode_from_disp_size (i386_operand_type t)
252b5132 2236{
b5014f7a 2237 if (t.bitfield.disp8)
40fb9820
L
2238 return 1;
2239 else if (t.bitfield.disp16
a775efc8 2240 || t.bitfield.disp32)
40fb9820
L
2241 return 2;
2242 else
2243 return 0;
252b5132
RH
2244}
2245
2246static INLINE int
65879393 2247fits_in_signed_byte (addressT num)
252b5132 2248{
65879393 2249 return num + 0x80 <= 0xff;
47926f60 2250}
252b5132
RH
2251
2252static INLINE int
65879393 2253fits_in_unsigned_byte (addressT num)
252b5132 2254{
65879393 2255 return num <= 0xff;
47926f60 2256}
252b5132
RH
2257
2258static INLINE int
65879393 2259fits_in_unsigned_word (addressT num)
252b5132 2260{
65879393 2261 return num <= 0xffff;
47926f60 2262}
252b5132
RH
2263
2264static INLINE int
65879393 2265fits_in_signed_word (addressT num)
252b5132 2266{
65879393 2267 return num + 0x8000 <= 0xffff;
47926f60 2268}
2a962e6d 2269
3e73aa7c 2270static INLINE int
65879393 2271fits_in_signed_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2272{
2273#ifndef BFD64
2274 return 1;
2275#else
65879393 2276 return num + 0x80000000 <= 0xffffffff;
3e73aa7c
JH
2277#endif
2278} /* fits_in_signed_long() */
2a962e6d 2279
3e73aa7c 2280static INLINE int
65879393 2281fits_in_unsigned_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2282{
2283#ifndef BFD64
2284 return 1;
2285#else
65879393 2286 return num <= 0xffffffff;
3e73aa7c
JH
2287#endif
2288} /* fits_in_unsigned_long() */
252b5132 2289
a442cac5
JB
2290static INLINE valueT extend_to_32bit_address (addressT num)
2291{
2292#ifdef BFD64
2293 if (fits_in_unsigned_long(num))
2294 return (num ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
2295
2296 if (!fits_in_signed_long (num))
2297 return num & 0xffffffff;
2298#endif
2299
2300 return num;
2301}
2302
43234a1e 2303static INLINE int
b5014f7a 2304fits_in_disp8 (offsetT num)
43234a1e
L
2305{
2306 int shift = i.memshift;
2307 unsigned int mask;
2308
2309 if (shift == -1)
2310 abort ();
2311
2312 mask = (1 << shift) - 1;
2313
2314 /* Return 0 if NUM isn't properly aligned. */
2315 if ((num & mask))
2316 return 0;
2317
2318 /* Check if NUM will fit in 8bit after shift. */
2319 return fits_in_signed_byte (num >> shift);
2320}
2321
a683cc34
SP
2322static INLINE int
2323fits_in_imm4 (offsetT num)
2324{
2325 return (num & 0xf) == num;
2326}
2327
40fb9820 2328static i386_operand_type
e3bb37b5 2329smallest_imm_type (offsetT num)
252b5132 2330{
40fb9820 2331 i386_operand_type t;
7ab9ffdd 2332
0dfbf9d7 2333 operand_type_set (&t, 0);
40fb9820
L
2334 t.bitfield.imm64 = 1;
2335
2336 if (cpu_arch_tune != PROCESSOR_I486 && num == 1)
e413e4e9
AM
2337 {
2338 /* This code is disabled on the 486 because all the Imm1 forms
2339 in the opcode table are slower on the i486. They're the
2340 versions with the implicitly specified single-position
2341 displacement, which has another syntax if you really want to
2342 use that form. */
40fb9820
L
2343 t.bitfield.imm1 = 1;
2344 t.bitfield.imm8 = 1;
2345 t.bitfield.imm8s = 1;
2346 t.bitfield.imm16 = 1;
2347 t.bitfield.imm32 = 1;
2348 t.bitfield.imm32s = 1;
2349 }
2350 else if (fits_in_signed_byte (num))
2351 {
2352 t.bitfield.imm8 = 1;
2353 t.bitfield.imm8s = 1;
2354 t.bitfield.imm16 = 1;
2355 t.bitfield.imm32 = 1;
2356 t.bitfield.imm32s = 1;
2357 }
2358 else if (fits_in_unsigned_byte (num))
2359 {
2360 t.bitfield.imm8 = 1;
2361 t.bitfield.imm16 = 1;
2362 t.bitfield.imm32 = 1;
2363 t.bitfield.imm32s = 1;
2364 }
2365 else if (fits_in_signed_word (num) || fits_in_unsigned_word (num))
2366 {
2367 t.bitfield.imm16 = 1;
2368 t.bitfield.imm32 = 1;
2369 t.bitfield.imm32s = 1;
2370 }
2371 else if (fits_in_signed_long (num))
2372 {
2373 t.bitfield.imm32 = 1;
2374 t.bitfield.imm32s = 1;
2375 }
2376 else if (fits_in_unsigned_long (num))
2377 t.bitfield.imm32 = 1;
2378
2379 return t;
47926f60 2380}
252b5132 2381
847f7ad4 2382static offsetT
e3bb37b5 2383offset_in_range (offsetT val, int size)
847f7ad4 2384{
508866be 2385 addressT mask;
ba2adb93 2386
847f7ad4
AM
2387 switch (size)
2388 {
508866be
L
2389 case 1: mask = ((addressT) 1 << 8) - 1; break;
2390 case 2: mask = ((addressT) 1 << 16) - 1; break;
3e73aa7c 2391#ifdef BFD64
64965897 2392 case 4: mask = ((addressT) 1 << 32) - 1; break;
3e73aa7c 2393#endif
64965897 2394 case sizeof (val): return val;
47926f60 2395 default: abort ();
847f7ad4
AM
2396 }
2397
4fe51f7d 2398 if ((val & ~mask) != 0 && (-val & ~mask) != 0)
f493c217
AM
2399 as_warn (_("0x%" PRIx64 " shortened to 0x%" PRIx64),
2400 (uint64_t) val, (uint64_t) (val & mask));
847f7ad4 2401
847f7ad4
AM
2402 return val & mask;
2403}
2404
c32fa91d
L
2405enum PREFIX_GROUP
2406{
2407 PREFIX_EXIST = 0,
2408 PREFIX_LOCK,
2409 PREFIX_REP,
04ef582a 2410 PREFIX_DS,
c32fa91d
L
2411 PREFIX_OTHER
2412};
2413
2414/* Returns
2415 a. PREFIX_EXIST if attempting to add a prefix where one from the
2416 same class already exists.
2417 b. PREFIX_LOCK if lock prefix is added.
2418 c. PREFIX_REP if rep/repne prefix is added.
04ef582a
L
2419 d. PREFIX_DS if ds prefix is added.
2420 e. PREFIX_OTHER if other prefix is added.
c32fa91d
L
2421 */
2422
2423static enum PREFIX_GROUP
e3bb37b5 2424add_prefix (unsigned int prefix)
252b5132 2425{
c32fa91d 2426 enum PREFIX_GROUP ret = PREFIX_OTHER;
b1905489 2427 unsigned int q;
252b5132 2428
29b0f896
AM
2429 if (prefix >= REX_OPCODE && prefix < REX_OPCODE + 16
2430 && flag_code == CODE_64BIT)
b1905489 2431 {
161a04f6 2432 if ((i.prefix[REX_PREFIX] & prefix & REX_W)
44846f29
JB
2433 || (i.prefix[REX_PREFIX] & prefix & REX_R)
2434 || (i.prefix[REX_PREFIX] & prefix & REX_X)
2435 || (i.prefix[REX_PREFIX] & prefix & REX_B))
c32fa91d 2436 ret = PREFIX_EXIST;
b1905489
JB
2437 q = REX_PREFIX;
2438 }
3e73aa7c 2439 else
b1905489
JB
2440 {
2441 switch (prefix)
2442 {
2443 default:
2444 abort ();
2445
b1905489 2446 case DS_PREFIX_OPCODE:
04ef582a
L
2447 ret = PREFIX_DS;
2448 /* Fall through. */
2449 case CS_PREFIX_OPCODE:
b1905489
JB
2450 case ES_PREFIX_OPCODE:
2451 case FS_PREFIX_OPCODE:
2452 case GS_PREFIX_OPCODE:
2453 case SS_PREFIX_OPCODE:
2454 q = SEG_PREFIX;
2455 break;
2456
2457 case REPNE_PREFIX_OPCODE:
2458 case REPE_PREFIX_OPCODE:
c32fa91d
L
2459 q = REP_PREFIX;
2460 ret = PREFIX_REP;
2461 break;
2462
b1905489 2463 case LOCK_PREFIX_OPCODE:
c32fa91d
L
2464 q = LOCK_PREFIX;
2465 ret = PREFIX_LOCK;
b1905489
JB
2466 break;
2467
2468 case FWAIT_OPCODE:
2469 q = WAIT_PREFIX;
2470 break;
2471
2472 case ADDR_PREFIX_OPCODE:
2473 q = ADDR_PREFIX;
2474 break;
2475
2476 case DATA_PREFIX_OPCODE:
2477 q = DATA_PREFIX;
2478 break;
2479 }
2480 if (i.prefix[q] != 0)
c32fa91d 2481 ret = PREFIX_EXIST;
b1905489 2482 }
252b5132 2483
b1905489 2484 if (ret)
252b5132 2485 {
b1905489
JB
2486 if (!i.prefix[q])
2487 ++i.prefixes;
2488 i.prefix[q] |= prefix;
252b5132 2489 }
b1905489
JB
2490 else
2491 as_bad (_("same type of prefix used twice"));
252b5132 2492
252b5132
RH
2493 return ret;
2494}
2495
2496static void
78f12dd3 2497update_code_flag (int value, int check)
eecb386c 2498{
78f12dd3
L
2499 PRINTF_LIKE ((*as_error));
2500
1e9cc1c2 2501 flag_code = (enum flag_code) value;
40fb9820
L
2502 if (flag_code == CODE_64BIT)
2503 {
2504 cpu_arch_flags.bitfield.cpu64 = 1;
2505 cpu_arch_flags.bitfield.cpuno64 = 0;
40fb9820
L
2506 }
2507 else
2508 {
2509 cpu_arch_flags.bitfield.cpu64 = 0;
2510 cpu_arch_flags.bitfield.cpuno64 = 1;
40fb9820
L
2511 }
2512 if (value == CODE_64BIT && !cpu_arch_flags.bitfield.cpulm )
3e73aa7c 2513 {
78f12dd3
L
2514 if (check)
2515 as_error = as_fatal;
2516 else
2517 as_error = as_bad;
2518 (*as_error) (_("64bit mode not supported on `%s'."),
2519 cpu_arch_name ? cpu_arch_name : default_arch);
3e73aa7c 2520 }
40fb9820 2521 if (value == CODE_32BIT && !cpu_arch_flags.bitfield.cpui386)
3e73aa7c 2522 {
78f12dd3
L
2523 if (check)
2524 as_error = as_fatal;
2525 else
2526 as_error = as_bad;
2527 (*as_error) (_("32bit mode not supported on `%s'."),
2528 cpu_arch_name ? cpu_arch_name : default_arch);
3e73aa7c 2529 }
eecb386c
AM
2530 stackop_size = '\0';
2531}
2532
78f12dd3
L
2533static void
2534set_code_flag (int value)
2535{
2536 update_code_flag (value, 0);
2537}
2538
eecb386c 2539static void
e3bb37b5 2540set_16bit_gcc_code_flag (int new_code_flag)
252b5132 2541{
1e9cc1c2 2542 flag_code = (enum flag_code) new_code_flag;
40fb9820
L
2543 if (flag_code != CODE_16BIT)
2544 abort ();
2545 cpu_arch_flags.bitfield.cpu64 = 0;
2546 cpu_arch_flags.bitfield.cpuno64 = 1;
9306ca4a 2547 stackop_size = LONG_MNEM_SUFFIX;
252b5132
RH
2548}
2549
2550static void
e3bb37b5 2551set_intel_syntax (int syntax_flag)
252b5132
RH
2552{
2553 /* Find out if register prefixing is specified. */
2554 int ask_naked_reg = 0;
2555
2556 SKIP_WHITESPACE ();
29b0f896 2557 if (!is_end_of_line[(unsigned char) *input_line_pointer])
252b5132 2558 {
d02603dc
NC
2559 char *string;
2560 int e = get_symbol_name (&string);
252b5132 2561
47926f60 2562 if (strcmp (string, "prefix") == 0)
252b5132 2563 ask_naked_reg = 1;
47926f60 2564 else if (strcmp (string, "noprefix") == 0)
252b5132
RH
2565 ask_naked_reg = -1;
2566 else
d0b47220 2567 as_bad (_("bad argument to syntax directive."));
d02603dc 2568 (void) restore_line_pointer (e);
252b5132
RH
2569 }
2570 demand_empty_rest_of_line ();
c3332e24 2571
252b5132
RH
2572 intel_syntax = syntax_flag;
2573
2574 if (ask_naked_reg == 0)
f86103b7
AM
2575 allow_naked_reg = (intel_syntax
2576 && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
252b5132
RH
2577 else
2578 allow_naked_reg = (ask_naked_reg < 0);
9306ca4a 2579
ee86248c 2580 expr_set_rank (O_full_ptr, syntax_flag ? 10 : 0);
7ab9ffdd 2581
e4a3b5a4 2582 identifier_chars['%'] = intel_syntax && allow_naked_reg ? '%' : 0;
9306ca4a 2583 identifier_chars['$'] = intel_syntax ? '$' : 0;
e4a3b5a4 2584 register_prefix = allow_naked_reg ? "" : "%";
252b5132
RH
2585}
2586
1efbbeb4
L
2587static void
2588set_intel_mnemonic (int mnemonic_flag)
2589{
e1d4d893 2590 intel_mnemonic = mnemonic_flag;
1efbbeb4
L
2591}
2592
db51cc60
L
2593static void
2594set_allow_index_reg (int flag)
2595{
2596 allow_index_reg = flag;
2597}
2598
cb19c032 2599static void
7bab8ab5 2600set_check (int what)
cb19c032 2601{
7bab8ab5
JB
2602 enum check_kind *kind;
2603 const char *str;
2604
2605 if (what)
2606 {
2607 kind = &operand_check;
2608 str = "operand";
2609 }
2610 else
2611 {
2612 kind = &sse_check;
2613 str = "sse";
2614 }
2615
cb19c032
L
2616 SKIP_WHITESPACE ();
2617
2618 if (!is_end_of_line[(unsigned char) *input_line_pointer])
2619 {
d02603dc
NC
2620 char *string;
2621 int e = get_symbol_name (&string);
cb19c032
L
2622
2623 if (strcmp (string, "none") == 0)
7bab8ab5 2624 *kind = check_none;
cb19c032 2625 else if (strcmp (string, "warning") == 0)
7bab8ab5 2626 *kind = check_warning;
cb19c032 2627 else if (strcmp (string, "error") == 0)
7bab8ab5 2628 *kind = check_error;
cb19c032 2629 else
7bab8ab5 2630 as_bad (_("bad argument to %s_check directive."), str);
d02603dc 2631 (void) restore_line_pointer (e);
cb19c032
L
2632 }
2633 else
7bab8ab5 2634 as_bad (_("missing argument for %s_check directive"), str);
cb19c032
L
2635
2636 demand_empty_rest_of_line ();
2637}
2638
8a9036a4
L
2639static void
2640check_cpu_arch_compatible (const char *name ATTRIBUTE_UNUSED,
1e9cc1c2 2641 i386_cpu_flags new_flag ATTRIBUTE_UNUSED)
8a9036a4
L
2642{
2643#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2644 static const char *arch;
2645
c085ab00 2646 /* Intel MCU is only supported on ELF. */
8a9036a4
L
2647 if (!IS_ELF)
2648 return;
2649
2650 if (!arch)
2651 {
2652 /* Use cpu_arch_name if it is set in md_parse_option. Otherwise
2653 use default_arch. */
2654 arch = cpu_arch_name;
2655 if (!arch)
2656 arch = default_arch;
2657 }
2658
81486035 2659 /* If we are targeting Intel MCU, we must enable it. */
648d04db
JB
2660 if ((get_elf_backend_data (stdoutput)->elf_machine_code == EM_IAMCU)
2661 == new_flag.bitfield.cpuiamcu)
81486035
L
2662 return;
2663
8a9036a4
L
2664 as_bad (_("`%s' is not supported on `%s'"), name, arch);
2665#endif
2666}
2667
8180707f
JB
2668static void
2669extend_cpu_sub_arch_name (const char *name)
2670{
2671 if (cpu_sub_arch_name)
2672 cpu_sub_arch_name = reconcat (cpu_sub_arch_name, cpu_sub_arch_name,
ae89daec 2673 ".", name, (const char *) NULL);
8180707f 2674 else
ae89daec 2675 cpu_sub_arch_name = concat (".", name, (const char *) NULL);
8180707f
JB
2676}
2677
e413e4e9 2678static void
e3bb37b5 2679set_cpu_arch (int dummy ATTRIBUTE_UNUSED)
e413e4e9 2680{
f68697e8
JB
2681 typedef struct arch_stack_entry
2682 {
2683 const struct arch_stack_entry *prev;
2684 const char *name;
2685 char *sub_name;
2686 i386_cpu_flags flags;
2687 i386_cpu_flags isa_flags;
2688 enum processor_type isa;
2689 enum flag_code flag_code;
2690 char stackop_size;
2691 bool no_cond_jump_promotion;
2692 } arch_stack_entry;
2693 static const arch_stack_entry *arch_stack_top;
2694
47926f60 2695 SKIP_WHITESPACE ();
e413e4e9 2696
29b0f896 2697 if (!is_end_of_line[(unsigned char) *input_line_pointer])
e413e4e9 2698 {
3ce2ebcf
JB
2699 char *s;
2700 int e = get_symbol_name (&s);
2701 const char *string = s;
2702 unsigned int j = 0;
40fb9820 2703 i386_cpu_flags flags;
e413e4e9 2704
3ce2ebcf
JB
2705 if (strcmp (string, "default") == 0)
2706 {
2707 if (strcmp (default_arch, "iamcu") == 0)
2708 string = default_arch;
2709 else
2710 {
2711 static const i386_cpu_flags cpu_unknown_flags = CPU_UNKNOWN_FLAGS;
2712
2713 cpu_arch_name = NULL;
2714 free (cpu_sub_arch_name);
2715 cpu_sub_arch_name = NULL;
2716 cpu_arch_flags = cpu_unknown_flags;
2717 if (flag_code == CODE_64BIT)
2718 {
2719 cpu_arch_flags.bitfield.cpu64 = 1;
2720 cpu_arch_flags.bitfield.cpuno64 = 0;
2721 }
2722 else
2723 {
2724 cpu_arch_flags.bitfield.cpu64 = 0;
2725 cpu_arch_flags.bitfield.cpuno64 = 1;
2726 }
2727 cpu_arch_isa = PROCESSOR_UNKNOWN;
ae89daec 2728 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
3ce2ebcf
JB
2729 if (!cpu_arch_tune_set)
2730 {
2731 cpu_arch_tune = cpu_arch_isa;
2732 cpu_arch_tune_flags = cpu_arch_isa_flags;
2733 }
2734
2735 j = ARRAY_SIZE (cpu_arch) + 1;
2736 }
2737 }
f68697e8
JB
2738 else if (strcmp (string, "push") == 0)
2739 {
2740 arch_stack_entry *top = XNEW (arch_stack_entry);
2741
2742 top->name = cpu_arch_name;
2743 if (cpu_sub_arch_name)
2744 top->sub_name = xstrdup (cpu_sub_arch_name);
2745 else
2746 top->sub_name = NULL;
2747 top->flags = cpu_arch_flags;
2748 top->isa = cpu_arch_isa;
2749 top->isa_flags = cpu_arch_isa_flags;
2750 top->flag_code = flag_code;
2751 top->stackop_size = stackop_size;
2752 top->no_cond_jump_promotion = no_cond_jump_promotion;
2753
2754 top->prev = arch_stack_top;
2755 arch_stack_top = top;
2756
2757 (void) restore_line_pointer (e);
2758 demand_empty_rest_of_line ();
2759 return;
2760 }
2761 else if (strcmp (string, "pop") == 0)
2762 {
2763 const arch_stack_entry *top = arch_stack_top;
2764
2765 if (!top)
2766 as_bad (_(".arch stack is empty"));
2767 else if (top->flag_code != flag_code
2768 || top->stackop_size != stackop_size)
2769 {
2770 static const unsigned int bits[] = {
2771 [CODE_16BIT] = 16,
2772 [CODE_32BIT] = 32,
2773 [CODE_64BIT] = 64,
2774 };
2775
2776 as_bad (_("this `.arch pop' requires `.code%u%s' to be in effect"),
2777 bits[top->flag_code],
2778 top->stackop_size == LONG_MNEM_SUFFIX ? "gcc" : "");
2779 }
2780 else
2781 {
2782 arch_stack_top = top->prev;
2783
2784 cpu_arch_name = top->name;
2785 free (cpu_sub_arch_name);
2786 cpu_sub_arch_name = top->sub_name;
2787 cpu_arch_flags = top->flags;
2788 cpu_arch_isa = top->isa;
2789 cpu_arch_isa_flags = top->isa_flags;
2790 no_cond_jump_promotion = top->no_cond_jump_promotion;
2791
2792 XDELETE (top);
2793 }
2794
2795 (void) restore_line_pointer (e);
2796 demand_empty_rest_of_line ();
2797 return;
2798 }
3ce2ebcf
JB
2799
2800 for (; j < ARRAY_SIZE (cpu_arch); j++)
e413e4e9 2801 {
ae89daec
JB
2802 if (strcmp (string + (*string == '.'), cpu_arch[j].name) == 0
2803 && (*string == '.') == (cpu_arch[j].type == PROCESSOR_NONE))
e413e4e9 2804 {
5c6af06e
JB
2805 if (*string != '.')
2806 {
ae89daec 2807 check_cpu_arch_compatible (string, cpu_arch[j].enable);
648d04db 2808
91d6fa6a 2809 cpu_arch_name = cpu_arch[j].name;
d92c7521 2810 free (cpu_sub_arch_name);
5c6af06e 2811 cpu_sub_arch_name = NULL;
ae89daec 2812 cpu_arch_flags = cpu_arch[j].enable;
40fb9820
L
2813 if (flag_code == CODE_64BIT)
2814 {
2815 cpu_arch_flags.bitfield.cpu64 = 1;
2816 cpu_arch_flags.bitfield.cpuno64 = 0;
2817 }
2818 else
2819 {
2820 cpu_arch_flags.bitfield.cpu64 = 0;
2821 cpu_arch_flags.bitfield.cpuno64 = 1;
2822 }
91d6fa6a 2823 cpu_arch_isa = cpu_arch[j].type;
ae89daec 2824 cpu_arch_isa_flags = cpu_arch[j].enable;
ccc9c027
L
2825 if (!cpu_arch_tune_set)
2826 {
2827 cpu_arch_tune = cpu_arch_isa;
2828 cpu_arch_tune_flags = cpu_arch_isa_flags;
2829 }
d59a54c2 2830 pre_386_16bit_warned = false;
5c6af06e
JB
2831 break;
2832 }
40fb9820 2833
ae89daec
JB
2834 if (cpu_flags_all_zero (&cpu_arch[j].enable))
2835 continue;
2836
293f5f65 2837 flags = cpu_flags_or (cpu_arch_flags,
ae89daec 2838 cpu_arch[j].enable);
81486035 2839
5b64d091 2840 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
5c6af06e 2841 {
ae89daec 2842 extend_cpu_sub_arch_name (string + 1);
40fb9820 2843 cpu_arch_flags = flags;
a586129e 2844 cpu_arch_isa_flags = flags;
5c6af06e 2845 }
0089dace
L
2846 else
2847 cpu_arch_isa_flags
2848 = cpu_flags_or (cpu_arch_isa_flags,
ae89daec 2849 cpu_arch[j].enable);
d02603dc 2850 (void) restore_line_pointer (e);
5c6af06e
JB
2851 demand_empty_rest_of_line ();
2852 return;
e413e4e9
AM
2853 }
2854 }
293f5f65 2855
ae89daec 2856 if (startswith (string, ".no") && j >= ARRAY_SIZE (cpu_arch))
293f5f65 2857 {
33eaf5de 2858 /* Disable an ISA extension. */
ae89daec
JB
2859 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
2860 if (cpu_arch[j].type == PROCESSOR_NONE
2861 && strcmp (string + 3, cpu_arch[j].name) == 0)
293f5f65
L
2862 {
2863 flags = cpu_flags_and_not (cpu_arch_flags,
ae89daec 2864 cpu_arch[j].disable);
293f5f65
L
2865 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
2866 {
ae89daec 2867 extend_cpu_sub_arch_name (string + 1);
293f5f65
L
2868 cpu_arch_flags = flags;
2869 cpu_arch_isa_flags = flags;
2870 }
2871 (void) restore_line_pointer (e);
2872 demand_empty_rest_of_line ();
2873 return;
2874 }
293f5f65
L
2875 }
2876
3ce2ebcf 2877 if (j == ARRAY_SIZE (cpu_arch))
e413e4e9
AM
2878 as_bad (_("no such architecture: `%s'"), string);
2879
2880 *input_line_pointer = e;
2881 }
2882 else
2883 as_bad (_("missing cpu architecture"));
2884
fddf5b5b
AM
2885 no_cond_jump_promotion = 0;
2886 if (*input_line_pointer == ','
29b0f896 2887 && !is_end_of_line[(unsigned char) input_line_pointer[1]])
fddf5b5b 2888 {
d02603dc
NC
2889 char *string;
2890 char e;
2891
2892 ++input_line_pointer;
2893 e = get_symbol_name (&string);
fddf5b5b
AM
2894
2895 if (strcmp (string, "nojumps") == 0)
2896 no_cond_jump_promotion = 1;
2897 else if (strcmp (string, "jumps") == 0)
2898 ;
2899 else
2900 as_bad (_("no such architecture modifier: `%s'"), string);
2901
d02603dc 2902 (void) restore_line_pointer (e);
fddf5b5b
AM
2903 }
2904
e413e4e9
AM
2905 demand_empty_rest_of_line ();
2906}
2907
8a9036a4
L
2908enum bfd_architecture
2909i386_arch (void)
2910{
c085ab00 2911 if (cpu_arch_isa == PROCESSOR_IAMCU)
81486035
L
2912 {
2913 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2914 || flag_code == CODE_64BIT)
2915 as_fatal (_("Intel MCU is 32bit ELF only"));
2916 return bfd_arch_iamcu;
2917 }
8a9036a4
L
2918 else
2919 return bfd_arch_i386;
2920}
2921
b9d79e03 2922unsigned long
7016a5d5 2923i386_mach (void)
b9d79e03 2924{
d34049e8 2925 if (startswith (default_arch, "x86_64"))
8a9036a4 2926 {
c085ab00 2927 if (default_arch[6] == '\0')
8a9036a4 2928 return bfd_mach_x86_64;
351f65ca
L
2929 else
2930 return bfd_mach_x64_32;
8a9036a4 2931 }
5197d474
L
2932 else if (!strcmp (default_arch, "i386")
2933 || !strcmp (default_arch, "iamcu"))
81486035
L
2934 {
2935 if (cpu_arch_isa == PROCESSOR_IAMCU)
2936 {
2937 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
2938 as_fatal (_("Intel MCU is 32bit ELF only"));
2939 return bfd_mach_i386_iamcu;
2940 }
2941 else
2942 return bfd_mach_i386_i386;
2943 }
b9d79e03 2944 else
2b5d6a91 2945 as_fatal (_("unknown architecture"));
b9d79e03 2946}
b9d79e03 2947\f
252b5132 2948void
7016a5d5 2949md_begin (void)
252b5132 2950{
86fa6981
L
2951 /* Support pseudo prefixes like {disp32}. */
2952 lex_type ['{'] = LEX_BEGIN_NAME;
2953
47926f60 2954 /* Initialize op_hash hash table. */
629310ab 2955 op_hash = str_htab_create ();
252b5132
RH
2956
2957 {
d3ce72d0 2958 const insn_template *optab;
29b0f896 2959 templates *core_optab;
252b5132 2960
47926f60
KH
2961 /* Setup for loop. */
2962 optab = i386_optab;
654d6f31 2963 core_optab = notes_alloc (sizeof (*core_optab));
252b5132
RH
2964 core_optab->start = optab;
2965
2966 while (1)
2967 {
2968 ++optab;
2969 if (optab->name == NULL
2970 || strcmp (optab->name, (optab - 1)->name) != 0)
2971 {
2972 /* different name --> ship out current template list;
47926f60 2973 add to hash table; & begin anew. */
252b5132 2974 core_optab->end = optab;
fe0e921f
AM
2975 if (str_hash_insert (op_hash, (optab - 1)->name, core_optab, 0))
2976 as_fatal (_("duplicate %s"), (optab - 1)->name);
2977
252b5132
RH
2978 if (optab->name == NULL)
2979 break;
654d6f31 2980 core_optab = notes_alloc (sizeof (*core_optab));
252b5132
RH
2981 core_optab->start = optab;
2982 }
2983 }
2984 }
2985
47926f60 2986 /* Initialize reg_hash hash table. */
629310ab 2987 reg_hash = str_htab_create ();
252b5132 2988 {
29b0f896 2989 const reg_entry *regtab;
c3fe08fa 2990 unsigned int regtab_size = i386_regtab_size;
252b5132 2991
c3fe08fa 2992 for (regtab = i386_regtab; regtab_size--; regtab++)
6225c532 2993 {
6288d05f
JB
2994 switch (regtab->reg_type.bitfield.class)
2995 {
2996 case Reg:
34684862
JB
2997 if (regtab->reg_type.bitfield.dword)
2998 {
2999 if (regtab->reg_type.bitfield.instance == Accum)
3000 reg_eax = regtab;
3001 }
3002 else if (regtab->reg_type.bitfield.tbyte)
6288d05f
JB
3003 {
3004 /* There's no point inserting st(<N>) in the hash table, as
3005 parentheses aren't included in register_chars[] anyway. */
3006 if (regtab->reg_type.bitfield.instance != Accum)
3007 continue;
3008 reg_st0 = regtab;
3009 }
3010 break;
3011
5e042380
JB
3012 case SReg:
3013 switch (regtab->reg_num)
3014 {
3015 case 0: reg_es = regtab; break;
3016 case 2: reg_ss = regtab; break;
3017 case 3: reg_ds = regtab; break;
3018 }
3019 break;
3020
6288d05f
JB
3021 case RegMask:
3022 if (!regtab->reg_num)
3023 reg_k0 = regtab;
3024 break;
3025 }
3026
6225c532
JB
3027 if (str_hash_insert (reg_hash, regtab->reg_name, regtab, 0) != NULL)
3028 as_fatal (_("duplicate %s"), regtab->reg_name);
6225c532 3029 }
252b5132
RH
3030 }
3031
47926f60 3032 /* Fill in lexical tables: mnemonic_chars, operand_chars. */
252b5132 3033 {
29b0f896
AM
3034 int c;
3035 char *p;
252b5132
RH
3036
3037 for (c = 0; c < 256; c++)
3038 {
014fbcda 3039 if (ISDIGIT (c) || ISLOWER (c))
252b5132
RH
3040 {
3041 mnemonic_chars[c] = c;
3042 register_chars[c] = c;
3043 operand_chars[c] = c;
3044 }
3882b010 3045 else if (ISUPPER (c))
252b5132 3046 {
3882b010 3047 mnemonic_chars[c] = TOLOWER (c);
252b5132
RH
3048 register_chars[c] = mnemonic_chars[c];
3049 operand_chars[c] = c;
3050 }
43234a1e 3051 else if (c == '{' || c == '}')
86fa6981
L
3052 {
3053 mnemonic_chars[c] = c;
3054 operand_chars[c] = c;
3055 }
b3983e5f
JB
3056#ifdef SVR4_COMMENT_CHARS
3057 else if (c == '\\' && strchr (i386_comment_chars, '/'))
3058 operand_chars[c] = c;
3059#endif
252b5132 3060
3882b010 3061 if (ISALPHA (c) || ISDIGIT (c))
252b5132
RH
3062 identifier_chars[c] = c;
3063 else if (c >= 128)
3064 {
3065 identifier_chars[c] = c;
3066 operand_chars[c] = c;
3067 }
3068 }
3069
3070#ifdef LEX_AT
3071 identifier_chars['@'] = '@';
32137342
NC
3072#endif
3073#ifdef LEX_QM
3074 identifier_chars['?'] = '?';
3075 operand_chars['?'] = '?';
252b5132 3076#endif
c0f3af97 3077 mnemonic_chars['_'] = '_';
791fe849 3078 mnemonic_chars['-'] = '-';
0003779b 3079 mnemonic_chars['.'] = '.';
252b5132
RH
3080 identifier_chars['_'] = '_';
3081 identifier_chars['.'] = '.';
3082
3083 for (p = operand_special_chars; *p != '\0'; p++)
3084 operand_chars[(unsigned char) *p] = *p;
3085 }
3086
a4447b93
RH
3087 if (flag_code == CODE_64BIT)
3088 {
ca19b261
KT
3089#if defined (OBJ_COFF) && defined (TE_PE)
3090 x86_dwarf2_return_column = (OUTPUT_FLAVOR == bfd_target_coff_flavour
3091 ? 32 : 16);
3092#else
a4447b93 3093 x86_dwarf2_return_column = 16;
ca19b261 3094#endif
61ff971f 3095 x86_cie_data_alignment = -8;
a4447b93
RH
3096 }
3097 else
3098 {
3099 x86_dwarf2_return_column = 8;
3100 x86_cie_data_alignment = -4;
3101 }
e379e5f3
L
3102
3103 /* NB: FUSED_JCC_PADDING frag must have sufficient room so that it
3104 can be turned into BRANCH_PREFIX frag. */
3105 if (align_branch_prefix_size > MAX_FUSED_JCC_PADDING_SIZE)
3106 abort ();
252b5132
RH
3107}
3108
3109void
e3bb37b5 3110i386_print_statistics (FILE *file)
252b5132 3111{
629310ab
ML
3112 htab_print_statistics (file, "i386 opcode", op_hash);
3113 htab_print_statistics (file, "i386 register", reg_hash);
252b5132 3114}
654d6f31
AM
3115
3116void
3117i386_md_end (void)
3118{
3119 htab_delete (op_hash);
3120 htab_delete (reg_hash);
3121}
252b5132 3122\f
252b5132
RH
3123#ifdef DEBUG386
3124
ce8a8b2f 3125/* Debugging routines for md_assemble. */
d3ce72d0 3126static void pte (insn_template *);
40fb9820 3127static void pt (i386_operand_type);
e3bb37b5
L
3128static void pe (expressionS *);
3129static void ps (symbolS *);
252b5132
RH
3130
3131static void
2c703856 3132pi (const char *line, i386_insn *x)
252b5132 3133{
09137c09 3134 unsigned int j;
252b5132
RH
3135
3136 fprintf (stdout, "%s: template ", line);
3137 pte (&x->tm);
09f131f2
JH
3138 fprintf (stdout, " address: base %s index %s scale %x\n",
3139 x->base_reg ? x->base_reg->reg_name : "none",
3140 x->index_reg ? x->index_reg->reg_name : "none",
3141 x->log2_scale_factor);
3142 fprintf (stdout, " modrm: mode %x reg %x reg/mem %x\n",
252b5132 3143 x->rm.mode, x->rm.reg, x->rm.regmem);
09f131f2
JH
3144 fprintf (stdout, " sib: base %x index %x scale %x\n",
3145 x->sib.base, x->sib.index, x->sib.scale);
3146 fprintf (stdout, " rex: 64bit %x extX %x extY %x extZ %x\n",
161a04f6
L
3147 (x->rex & REX_W) != 0,
3148 (x->rex & REX_R) != 0,
3149 (x->rex & REX_X) != 0,
3150 (x->rex & REX_B) != 0);
09137c09 3151 for (j = 0; j < x->operands; j++)
252b5132 3152 {
09137c09
SP
3153 fprintf (stdout, " #%d: ", j + 1);
3154 pt (x->types[j]);
252b5132 3155 fprintf (stdout, "\n");
bab6aec1 3156 if (x->types[j].bitfield.class == Reg
3528c362
JB
3157 || x->types[j].bitfield.class == RegMMX
3158 || x->types[j].bitfield.class == RegSIMD
dd6b8a0b 3159 || x->types[j].bitfield.class == RegMask
00cee14f 3160 || x->types[j].bitfield.class == SReg
4a5c67ed
JB
3161 || x->types[j].bitfield.class == RegCR
3162 || x->types[j].bitfield.class == RegDR
dd6b8a0b
JB
3163 || x->types[j].bitfield.class == RegTR
3164 || x->types[j].bitfield.class == RegBND)
09137c09
SP
3165 fprintf (stdout, "%s\n", x->op[j].regs->reg_name);
3166 if (operand_type_check (x->types[j], imm))
3167 pe (x->op[j].imms);
3168 if (operand_type_check (x->types[j], disp))
3169 pe (x->op[j].disps);
252b5132
RH
3170 }
3171}
3172
3173static void
d3ce72d0 3174pte (insn_template *t)
252b5132 3175{
b933fa4b 3176 static const unsigned char opc_pfx[] = { 0, 0x66, 0xf3, 0xf2 };
441f6aca 3177 static const char *const opc_spc[] = {
0cc78721 3178 NULL, "0f", "0f38", "0f3a", NULL, "evexmap5", "evexmap6", NULL,
441f6aca
JB
3179 "XOP08", "XOP09", "XOP0A",
3180 };
09137c09 3181 unsigned int j;
441f6aca 3182
252b5132 3183 fprintf (stdout, " %d operands ", t->operands);
441f6aca
JB
3184 if (opc_pfx[t->opcode_modifier.opcodeprefix])
3185 fprintf (stdout, "pfx %x ", opc_pfx[t->opcode_modifier.opcodeprefix]);
3186 if (opc_spc[t->opcode_modifier.opcodespace])
3187 fprintf (stdout, "space %s ", opc_spc[t->opcode_modifier.opcodespace]);
47926f60 3188 fprintf (stdout, "opcode %x ", t->base_opcode);
252b5132
RH
3189 if (t->extension_opcode != None)
3190 fprintf (stdout, "ext %x ", t->extension_opcode);
40fb9820 3191 if (t->opcode_modifier.d)
252b5132 3192 fprintf (stdout, "D");
40fb9820 3193 if (t->opcode_modifier.w)
252b5132
RH
3194 fprintf (stdout, "W");
3195 fprintf (stdout, "\n");
09137c09 3196 for (j = 0; j < t->operands; j++)
252b5132 3197 {
09137c09
SP
3198 fprintf (stdout, " #%d type ", j + 1);
3199 pt (t->operand_types[j]);
252b5132
RH
3200 fprintf (stdout, "\n");
3201 }
3202}
3203
3204static void
e3bb37b5 3205pe (expressionS *e)
252b5132 3206{
24eab124 3207 fprintf (stdout, " operation %d\n", e->X_op);
b8281767
AM
3208 fprintf (stdout, " add_number %" PRId64 " (%" PRIx64 ")\n",
3209 (int64_t) e->X_add_number, (uint64_t) (valueT) e->X_add_number);
252b5132
RH
3210 if (e->X_add_symbol)
3211 {
3212 fprintf (stdout, " add_symbol ");
3213 ps (e->X_add_symbol);
3214 fprintf (stdout, "\n");
3215 }
3216 if (e->X_op_symbol)
3217 {
3218 fprintf (stdout, " op_symbol ");
3219 ps (e->X_op_symbol);
3220 fprintf (stdout, "\n");
3221 }
3222}
3223
3224static void
e3bb37b5 3225ps (symbolS *s)
252b5132
RH
3226{
3227 fprintf (stdout, "%s type %s%s",
3228 S_GET_NAME (s),
3229 S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
3230 segment_name (S_GET_SEGMENT (s)));
3231}
3232
7b81dfbb 3233static struct type_name
252b5132 3234 {
40fb9820
L
3235 i386_operand_type mask;
3236 const char *name;
252b5132 3237 }
7b81dfbb 3238const type_names[] =
252b5132 3239{
40fb9820
L
3240 { OPERAND_TYPE_REG8, "r8" },
3241 { OPERAND_TYPE_REG16, "r16" },
3242 { OPERAND_TYPE_REG32, "r32" },
3243 { OPERAND_TYPE_REG64, "r64" },
2c703856
JB
3244 { OPERAND_TYPE_ACC8, "acc8" },
3245 { OPERAND_TYPE_ACC16, "acc16" },
3246 { OPERAND_TYPE_ACC32, "acc32" },
3247 { OPERAND_TYPE_ACC64, "acc64" },
40fb9820
L
3248 { OPERAND_TYPE_IMM8, "i8" },
3249 { OPERAND_TYPE_IMM8, "i8s" },
3250 { OPERAND_TYPE_IMM16, "i16" },
3251 { OPERAND_TYPE_IMM32, "i32" },
3252 { OPERAND_TYPE_IMM32S, "i32s" },
3253 { OPERAND_TYPE_IMM64, "i64" },
3254 { OPERAND_TYPE_IMM1, "i1" },
3255 { OPERAND_TYPE_BASEINDEX, "BaseIndex" },
3256 { OPERAND_TYPE_DISP8, "d8" },
3257 { OPERAND_TYPE_DISP16, "d16" },
3258 { OPERAND_TYPE_DISP32, "d32" },
40fb9820
L
3259 { OPERAND_TYPE_DISP64, "d64" },
3260 { OPERAND_TYPE_INOUTPORTREG, "InOutPortReg" },
3261 { OPERAND_TYPE_SHIFTCOUNT, "ShiftCount" },
3262 { OPERAND_TYPE_CONTROL, "control reg" },
3263 { OPERAND_TYPE_TEST, "test reg" },
3264 { OPERAND_TYPE_DEBUG, "debug reg" },
3265 { OPERAND_TYPE_FLOATREG, "FReg" },
3266 { OPERAND_TYPE_FLOATACC, "FAcc" },
21df382b 3267 { OPERAND_TYPE_SREG, "SReg" },
40fb9820
L
3268 { OPERAND_TYPE_REGMMX, "rMMX" },
3269 { OPERAND_TYPE_REGXMM, "rXMM" },
0349dc08 3270 { OPERAND_TYPE_REGYMM, "rYMM" },
43234a1e 3271 { OPERAND_TYPE_REGZMM, "rZMM" },
260cd341 3272 { OPERAND_TYPE_REGTMM, "rTMM" },
43234a1e 3273 { OPERAND_TYPE_REGMASK, "Mask reg" },
252b5132
RH
3274};
3275
3276static void
40fb9820 3277pt (i386_operand_type t)
252b5132 3278{
40fb9820 3279 unsigned int j;
c6fb90c8 3280 i386_operand_type a;
252b5132 3281
40fb9820 3282 for (j = 0; j < ARRAY_SIZE (type_names); j++)
c6fb90c8
L
3283 {
3284 a = operand_type_and (t, type_names[j].mask);
2c703856 3285 if (operand_type_equal (&a, &type_names[j].mask))
c6fb90c8
L
3286 fprintf (stdout, "%s, ", type_names[j].name);
3287 }
252b5132
RH
3288 fflush (stdout);
3289}
3290
3291#endif /* DEBUG386 */
3292\f
252b5132 3293static bfd_reloc_code_real_type
3956db08 3294reloc (unsigned int size,
64e74474
AM
3295 int pcrel,
3296 int sign,
3297 bfd_reloc_code_real_type other)
252b5132 3298{
47926f60 3299 if (other != NO_RELOC)
3956db08 3300 {
91d6fa6a 3301 reloc_howto_type *rel;
3956db08
JB
3302
3303 if (size == 8)
3304 switch (other)
3305 {
64e74474
AM
3306 case BFD_RELOC_X86_64_GOT32:
3307 return BFD_RELOC_X86_64_GOT64;
3308 break;
553d1284
L
3309 case BFD_RELOC_X86_64_GOTPLT64:
3310 return BFD_RELOC_X86_64_GOTPLT64;
3311 break;
64e74474
AM
3312 case BFD_RELOC_X86_64_PLTOFF64:
3313 return BFD_RELOC_X86_64_PLTOFF64;
3314 break;
3315 case BFD_RELOC_X86_64_GOTPC32:
3316 other = BFD_RELOC_X86_64_GOTPC64;
3317 break;
3318 case BFD_RELOC_X86_64_GOTPCREL:
3319 other = BFD_RELOC_X86_64_GOTPCREL64;
3320 break;
3321 case BFD_RELOC_X86_64_TPOFF32:
3322 other = BFD_RELOC_X86_64_TPOFF64;
3323 break;
3324 case BFD_RELOC_X86_64_DTPOFF32:
3325 other = BFD_RELOC_X86_64_DTPOFF64;
3326 break;
3327 default:
3328 break;
3956db08 3329 }
e05278af 3330
8ce3d284 3331#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
3332 if (other == BFD_RELOC_SIZE32)
3333 {
3334 if (size == 8)
1ab668bf 3335 other = BFD_RELOC_SIZE64;
8fd4256d 3336 if (pcrel)
1ab668bf
AM
3337 {
3338 as_bad (_("there are no pc-relative size relocations"));
3339 return NO_RELOC;
3340 }
8fd4256d 3341 }
8ce3d284 3342#endif
8fd4256d 3343
e05278af 3344 /* Sign-checking 4-byte relocations in 16-/32-bit code is pointless. */
f2d8a97c 3345 if (size == 4 && (flag_code != CODE_64BIT || disallow_64bit_reloc))
e05278af
JB
3346 sign = -1;
3347
91d6fa6a
NC
3348 rel = bfd_reloc_type_lookup (stdoutput, other);
3349 if (!rel)
3956db08 3350 as_bad (_("unknown relocation (%u)"), other);
91d6fa6a 3351 else if (size != bfd_get_reloc_size (rel))
3956db08 3352 as_bad (_("%u-byte relocation cannot be applied to %u-byte field"),
91d6fa6a 3353 bfd_get_reloc_size (rel),
3956db08 3354 size);
91d6fa6a 3355 else if (pcrel && !rel->pc_relative)
3956db08 3356 as_bad (_("non-pc-relative relocation for pc-relative field"));
91d6fa6a 3357 else if ((rel->complain_on_overflow == complain_overflow_signed
3956db08 3358 && !sign)
91d6fa6a 3359 || (rel->complain_on_overflow == complain_overflow_unsigned
64e74474 3360 && sign > 0))
3956db08
JB
3361 as_bad (_("relocated field and relocation type differ in signedness"));
3362 else
3363 return other;
3364 return NO_RELOC;
3365 }
252b5132
RH
3366
3367 if (pcrel)
3368 {
3e73aa7c 3369 if (!sign)
3956db08 3370 as_bad (_("there are no unsigned pc-relative relocations"));
252b5132
RH
3371 switch (size)
3372 {
3373 case 1: return BFD_RELOC_8_PCREL;
3374 case 2: return BFD_RELOC_16_PCREL;
d258b828 3375 case 4: return BFD_RELOC_32_PCREL;
d6ab8113 3376 case 8: return BFD_RELOC_64_PCREL;
252b5132 3377 }
3956db08 3378 as_bad (_("cannot do %u byte pc-relative relocation"), size);
252b5132
RH
3379 }
3380 else
3381 {
3956db08 3382 if (sign > 0)
e5cb08ac 3383 switch (size)
3e73aa7c
JH
3384 {
3385 case 4: return BFD_RELOC_X86_64_32S;
3386 }
3387 else
3388 switch (size)
3389 {
3390 case 1: return BFD_RELOC_8;
3391 case 2: return BFD_RELOC_16;
3392 case 4: return BFD_RELOC_32;
3393 case 8: return BFD_RELOC_64;
3394 }
3956db08
JB
3395 as_bad (_("cannot do %s %u byte relocation"),
3396 sign > 0 ? "signed" : "unsigned", size);
252b5132
RH
3397 }
3398
0cc9e1d3 3399 return NO_RELOC;
252b5132
RH
3400}
3401
47926f60
KH
3402/* Here we decide which fixups can be adjusted to make them relative to
3403 the beginning of the section instead of the symbol. Basically we need
3404 to make sure that the dynamic relocations are done correctly, so in
3405 some cases we force the original symbol to be used. */
3406
252b5132 3407int
e3bb37b5 3408tc_i386_fix_adjustable (fixS *fixP ATTRIBUTE_UNUSED)
252b5132 3409{
6d249963 3410#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 3411 if (!IS_ELF)
31312f95
AM
3412 return 1;
3413
a161fe53
AM
3414 /* Don't adjust pc-relative references to merge sections in 64-bit
3415 mode. */
3416 if (use_rela_relocations
3417 && (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0
3418 && fixP->fx_pcrel)
252b5132 3419 return 0;
31312f95 3420
8d01d9a9
AJ
3421 /* The x86_64 GOTPCREL are represented as 32bit PCrel relocations
3422 and changed later by validate_fix. */
3423 if (GOT_symbol && fixP->fx_subsy == GOT_symbol
3424 && fixP->fx_r_type == BFD_RELOC_32_PCREL)
3425 return 0;
3426
8fd4256d
L
3427 /* Adjust_reloc_syms doesn't know about the GOT. Need to keep symbol
3428 for size relocations. */
3429 if (fixP->fx_r_type == BFD_RELOC_SIZE32
3430 || fixP->fx_r_type == BFD_RELOC_SIZE64
3431 || fixP->fx_r_type == BFD_RELOC_386_GOTOFF
252b5132 3432 || fixP->fx_r_type == BFD_RELOC_386_GOT32
02a86693 3433 || fixP->fx_r_type == BFD_RELOC_386_GOT32X
13ae64f3
JJ
3434 || fixP->fx_r_type == BFD_RELOC_386_TLS_GD
3435 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDM
3436 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDO_32
3437 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE_32
37e55690
JJ
3438 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE
3439 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTIE
13ae64f3
JJ
3440 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE_32
3441 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE
67a4f2b7
AO
3442 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTDESC
3443 || fixP->fx_r_type == BFD_RELOC_386_TLS_DESC_CALL
3e73aa7c 3444 || fixP->fx_r_type == BFD_RELOC_X86_64_GOT32
80b3ee89 3445 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCREL
56ceb5b5
L
3446 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCRELX
3447 || fixP->fx_r_type == BFD_RELOC_X86_64_REX_GOTPCRELX
bffbf940
JJ
3448 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSGD
3449 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSLD
3450 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF32
d6ab8113 3451 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF64
bffbf940
JJ
3452 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF
3453 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF32
d6ab8113
JB
3454 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF64
3455 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTOFF64
67a4f2b7
AO
3456 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
3457 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSDESC_CALL
252b5132
RH
3458 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3459 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3460 return 0;
31312f95 3461#endif
252b5132
RH
3462 return 1;
3463}
252b5132 3464
a9aabc23
JB
3465static INLINE bool
3466want_disp32 (const insn_template *t)
3467{
3468 return flag_code != CODE_64BIT
3469 || i.prefix[ADDR_PREFIX]
3470 || (t->base_opcode == 0x8d
3471 && t->opcode_modifier.opcodespace == SPACE_BASE
fe134c65
JB
3472 && (!i.types[1].bitfield.qword
3473 || t->opcode_modifier.size == SIZE32));
a9aabc23
JB
3474}
3475
b4cac588 3476static int
e3bb37b5 3477intel_float_operand (const char *mnemonic)
252b5132 3478{
9306ca4a
JB
3479 /* Note that the value returned is meaningful only for opcodes with (memory)
3480 operands, hence the code here is free to improperly handle opcodes that
3481 have no operands (for better performance and smaller code). */
3482
3483 if (mnemonic[0] != 'f')
3484 return 0; /* non-math */
3485
3486 switch (mnemonic[1])
3487 {
3488 /* fclex, fdecstp, fdisi, femms, feni, fincstp, finit, fsetpm, and
3489 the fs segment override prefix not currently handled because no
3490 call path can make opcodes without operands get here */
3491 case 'i':
3492 return 2 /* integer op */;
3493 case 'l':
3494 if (mnemonic[2] == 'd' && (mnemonic[3] == 'c' || mnemonic[3] == 'e'))
3495 return 3; /* fldcw/fldenv */
3496 break;
3497 case 'n':
3498 if (mnemonic[2] != 'o' /* fnop */)
3499 return 3; /* non-waiting control op */
3500 break;
3501 case 'r':
3502 if (mnemonic[2] == 's')
3503 return 3; /* frstor/frstpm */
3504 break;
3505 case 's':
3506 if (mnemonic[2] == 'a')
3507 return 3; /* fsave */
3508 if (mnemonic[2] == 't')
3509 {
3510 switch (mnemonic[3])
3511 {
3512 case 'c': /* fstcw */
3513 case 'd': /* fstdw */
3514 case 'e': /* fstenv */
3515 case 's': /* fsts[gw] */
3516 return 3;
3517 }
3518 }
3519 break;
3520 case 'x':
3521 if (mnemonic[2] == 'r' || mnemonic[2] == 's')
3522 return 0; /* fxsave/fxrstor are not really math ops */
3523 break;
3524 }
252b5132 3525
9306ca4a 3526 return 1;
252b5132
RH
3527}
3528
9a182d04
JB
3529static INLINE void
3530install_template (const insn_template *t)
3531{
3532 unsigned int l;
3533
3534 i.tm = *t;
3535
3536 /* Note that for pseudo prefixes this produces a length of 1. But for them
3537 the length isn't interesting at all. */
3538 for (l = 1; l < 4; ++l)
3539 if (!(t->base_opcode >> (8 * l)))
3540 break;
3541
3542 i.opcode_length = l;
3543}
3544
c0f3af97
L
3545/* Build the VEX prefix. */
3546
3547static void
d3ce72d0 3548build_vex_prefix (const insn_template *t)
c0f3af97
L
3549{
3550 unsigned int register_specifier;
c0f3af97 3551 unsigned int vector_length;
03751133 3552 unsigned int w;
c0f3af97
L
3553
3554 /* Check register specifier. */
3555 if (i.vex.register_specifier)
43234a1e
L
3556 {
3557 register_specifier =
3558 ~register_number (i.vex.register_specifier) & 0xf;
3559 gas_assert ((i.vex.register_specifier->reg_flags & RegVRex) == 0);
3560 }
c0f3af97
L
3561 else
3562 register_specifier = 0xf;
3563
79f0fa25
L
3564 /* Use 2-byte VEX prefix by swapping destination and source operand
3565 if there are more than 1 register operand. */
3566 if (i.reg_operands > 1
3567 && i.vec_encoding != vex_encoding_vex3
86fa6981 3568 && i.dir_encoding == dir_encoding_default
fa99fab2 3569 && i.operands == i.reg_operands
dbbc8b7e 3570 && operand_type_equal (&i.types[0], &i.types[i.operands - 1])
441f6aca 3571 && i.tm.opcode_modifier.opcodespace == SPACE_0F
dbbc8b7e 3572 && (i.tm.opcode_modifier.load || i.tm.opcode_modifier.d)
fa99fab2
L
3573 && i.rex == REX_B)
3574 {
3575 unsigned int xchg = i.operands - 1;
3576 union i386_op temp_op;
3577 i386_operand_type temp_type;
3578
3579 temp_type = i.types[xchg];
3580 i.types[xchg] = i.types[0];
3581 i.types[0] = temp_type;
3582 temp_op = i.op[xchg];
3583 i.op[xchg] = i.op[0];
3584 i.op[0] = temp_op;
3585
9c2799c2 3586 gas_assert (i.rm.mode == 3);
fa99fab2
L
3587
3588 i.rex = REX_R;
3589 xchg = i.rm.regmem;
3590 i.rm.regmem = i.rm.reg;
3591 i.rm.reg = xchg;
3592
dbbc8b7e
JB
3593 if (i.tm.opcode_modifier.d)
3594 i.tm.base_opcode ^= (i.tm.base_opcode & 0xee) != 0x6e
2c735193 3595 ? Opcode_ExtD : Opcode_SIMD_IntD;
dbbc8b7e 3596 else /* Use the next insn. */
9a182d04 3597 install_template (&t[1]);
fa99fab2
L
3598 }
3599
79dec6b7
JB
3600 /* Use 2-byte VEX prefix by swapping commutative source operands if there
3601 are no memory operands and at least 3 register ones. */
3602 if (i.reg_operands >= 3
3603 && i.vec_encoding != vex_encoding_vex3
3604 && i.reg_operands == i.operands - i.imm_operands
3605 && i.tm.opcode_modifier.vex
3606 && i.tm.opcode_modifier.commutative
3607 && (i.tm.opcode_modifier.sse2avx || optimize > 1)
3608 && i.rex == REX_B
3609 && i.vex.register_specifier
3610 && !(i.vex.register_specifier->reg_flags & RegRex))
3611 {
3612 unsigned int xchg = i.operands - i.reg_operands;
3613 union i386_op temp_op;
3614 i386_operand_type temp_type;
3615
441f6aca 3616 gas_assert (i.tm.opcode_modifier.opcodespace == SPACE_0F);
79dec6b7
JB
3617 gas_assert (!i.tm.opcode_modifier.sae);
3618 gas_assert (operand_type_equal (&i.types[i.operands - 2],
3619 &i.types[i.operands - 3]));
3620 gas_assert (i.rm.mode == 3);
3621
3622 temp_type = i.types[xchg];
3623 i.types[xchg] = i.types[xchg + 1];
3624 i.types[xchg + 1] = temp_type;
3625 temp_op = i.op[xchg];
3626 i.op[xchg] = i.op[xchg + 1];
3627 i.op[xchg + 1] = temp_op;
3628
3629 i.rex = 0;
3630 xchg = i.rm.regmem | 8;
3631 i.rm.regmem = ~register_specifier & 0xf;
3632 gas_assert (!(i.rm.regmem & 8));
3633 i.vex.register_specifier += xchg - i.rm.regmem;
3634 register_specifier = ~xchg & 0xf;
3635 }
3636
539f890d
L
3637 if (i.tm.opcode_modifier.vex == VEXScalar)
3638 vector_length = avxscalar;
10c17abd
JB
3639 else if (i.tm.opcode_modifier.vex == VEX256)
3640 vector_length = 1;
539f890d 3641 else
10c17abd 3642 {
56522fc5 3643 unsigned int op;
10c17abd 3644
c7213af9
L
3645 /* Determine vector length from the last multi-length vector
3646 operand. */
10c17abd 3647 vector_length = 0;
56522fc5 3648 for (op = t->operands; op--;)
10c17abd
JB
3649 if (t->operand_types[op].bitfield.xmmword
3650 && t->operand_types[op].bitfield.ymmword
3651 && i.types[op].bitfield.ymmword)
3652 {
3653 vector_length = 1;
3654 break;
3655 }
3656 }
c0f3af97 3657
03751133
L
3658 /* Check the REX.W bit and VEXW. */
3659 if (i.tm.opcode_modifier.vexw == VEXWIG)
3660 w = (vexwig == vexw1 || (i.rex & REX_W)) ? 1 : 0;
3661 else if (i.tm.opcode_modifier.vexw)
3662 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3663 else
931d03b7 3664 w = (flag_code == CODE_64BIT ? i.rex & REX_W : vexwig == vexw1) ? 1 : 0;
03751133 3665
c0f3af97 3666 /* Use 2-byte VEX prefix if possible. */
03751133
L
3667 if (w == 0
3668 && i.vec_encoding != vex_encoding_vex3
441f6aca 3669 && i.tm.opcode_modifier.opcodespace == SPACE_0F
c0f3af97
L
3670 && (i.rex & (REX_W | REX_X | REX_B)) == 0)
3671 {
3672 /* 2-byte VEX prefix. */
3673 unsigned int r;
3674
3675 i.vex.length = 2;
3676 i.vex.bytes[0] = 0xc5;
3677
3678 /* Check the REX.R bit. */
3679 r = (i.rex & REX_R) ? 0 : 1;
3680 i.vex.bytes[1] = (r << 7
3681 | register_specifier << 3
3682 | vector_length << 2
35648716 3683 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3684 }
3685 else
3686 {
3687 /* 3-byte VEX prefix. */
f88c9eb0 3688 i.vex.length = 3;
f88c9eb0 3689
441f6aca 3690 switch (i.tm.opcode_modifier.opcodespace)
5dd85c99 3691 {
441f6aca
JB
3692 case SPACE_0F:
3693 case SPACE_0F38:
3694 case SPACE_0F3A:
80de6e00 3695 i.vex.bytes[0] = 0xc4;
7f399153 3696 break;
441f6aca
JB
3697 case SPACE_XOP08:
3698 case SPACE_XOP09:
3699 case SPACE_XOP0A:
f88c9eb0 3700 i.vex.bytes[0] = 0x8f;
7f399153
L
3701 break;
3702 default:
3703 abort ();
f88c9eb0 3704 }
c0f3af97 3705
c0f3af97
L
3706 /* The high 3 bits of the second VEX byte are 1's compliment
3707 of RXB bits from REX. */
441f6aca 3708 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | i.tm.opcode_modifier.opcodespace;
c0f3af97 3709
c0f3af97
L
3710 i.vex.bytes[2] = (w << 7
3711 | register_specifier << 3
3712 | vector_length << 2
35648716 3713 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3714 }
3715}
3716
5b7c81bd 3717static INLINE bool
e771e7c9
JB
3718is_evex_encoding (const insn_template *t)
3719{
7091c612 3720 return t->opcode_modifier.evex || t->opcode_modifier.disp8memshift
e771e7c9 3721 || t->opcode_modifier.broadcast || t->opcode_modifier.masking
a80195f1 3722 || t->opcode_modifier.sae;
e771e7c9
JB
3723}
3724
5b7c81bd 3725static INLINE bool
7a8655d2
JB
3726is_any_vex_encoding (const insn_template *t)
3727{
7b47a312 3728 return t->opcode_modifier.vex || is_evex_encoding (t);
7a8655d2
JB
3729}
3730
a5748e0d
JB
3731static unsigned int
3732get_broadcast_bytes (const insn_template *t, bool diag)
3733{
3734 unsigned int op, bytes;
3735 const i386_operand_type *types;
3736
3737 if (i.broadcast.type)
3738 return i.broadcast.bytes = ((1 << (t->opcode_modifier.broadcast - 1))
3739 * i.broadcast.type);
3740
3741 gas_assert (intel_syntax);
3742
3743 for (op = 0; op < t->operands; ++op)
3744 if (t->operand_types[op].bitfield.baseindex)
3745 break;
3746
3747 gas_assert (op < t->operands);
3748
3749 if (t->opcode_modifier.evex
3750 && t->opcode_modifier.evex != EVEXDYN)
3751 switch (i.broadcast.bytes)
3752 {
3753 case 1:
3754 if (t->operand_types[op].bitfield.word)
3755 return 2;
3756 /* Fall through. */
3757 case 2:
3758 if (t->operand_types[op].bitfield.dword)
3759 return 4;
3760 /* Fall through. */
3761 case 4:
3762 if (t->operand_types[op].bitfield.qword)
3763 return 8;
3764 /* Fall through. */
3765 case 8:
3766 if (t->operand_types[op].bitfield.xmmword)
3767 return 16;
3768 if (t->operand_types[op].bitfield.ymmword)
3769 return 32;
3770 if (t->operand_types[op].bitfield.zmmword)
3771 return 64;
3772 /* Fall through. */
3773 default:
3774 abort ();
3775 }
3776
3777 gas_assert (op + 1 < t->operands);
3778
3779 if (t->operand_types[op + 1].bitfield.xmmword
3780 + t->operand_types[op + 1].bitfield.ymmword
3781 + t->operand_types[op + 1].bitfield.zmmword > 1)
3782 {
3783 types = &i.types[op + 1];
3784 diag = false;
3785 }
3786 else /* Ambiguous - guess with a preference to non-AVX512VL forms. */
3787 types = &t->operand_types[op];
3788
3789 if (types->bitfield.zmmword)
3790 bytes = 64;
3791 else if (types->bitfield.ymmword)
3792 bytes = 32;
3793 else
3794 bytes = 16;
3795
3796 if (diag)
3797 as_warn (_("ambiguous broadcast for `%s', using %u-bit form"),
3798 t->name, bytes * 8);
3799
3800 return bytes;
3801}
3802
43234a1e
L
3803/* Build the EVEX prefix. */
3804
3805static void
3806build_evex_prefix (void)
3807{
35648716 3808 unsigned int register_specifier, w;
43234a1e
L
3809 rex_byte vrex_used = 0;
3810
3811 /* Check register specifier. */
3812 if (i.vex.register_specifier)
3813 {
3814 gas_assert ((i.vrex & REX_X) == 0);
3815
3816 register_specifier = i.vex.register_specifier->reg_num;
3817 if ((i.vex.register_specifier->reg_flags & RegRex))
3818 register_specifier += 8;
3819 /* The upper 16 registers are encoded in the fourth byte of the
3820 EVEX prefix. */
3821 if (!(i.vex.register_specifier->reg_flags & RegVRex))
3822 i.vex.bytes[3] = 0x8;
3823 register_specifier = ~register_specifier & 0xf;
3824 }
3825 else
3826 {
3827 register_specifier = 0xf;
3828
3829 /* Encode upper 16 vector index register in the fourth byte of
3830 the EVEX prefix. */
3831 if (!(i.vrex & REX_X))
3832 i.vex.bytes[3] = 0x8;
3833 else
3834 vrex_used |= REX_X;
3835 }
3836
43234a1e
L
3837 /* 4 byte EVEX prefix. */
3838 i.vex.length = 4;
3839 i.vex.bytes[0] = 0x62;
3840
43234a1e
L
3841 /* The high 3 bits of the second EVEX byte are 1's compliment of RXB
3842 bits from REX. */
441f6aca 3843 gas_assert (i.tm.opcode_modifier.opcodespace >= SPACE_0F);
0cc78721 3844 gas_assert (i.tm.opcode_modifier.opcodespace <= SPACE_EVEXMAP6);
441f6aca 3845 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | i.tm.opcode_modifier.opcodespace;
43234a1e
L
3846
3847 /* The fifth bit of the second EVEX byte is 1's compliment of the
3848 REX_R bit in VREX. */
3849 if (!(i.vrex & REX_R))
3850 i.vex.bytes[1] |= 0x10;
3851 else
3852 vrex_used |= REX_R;
3853
3854 if ((i.reg_operands + i.imm_operands) == i.operands)
3855 {
3856 /* When all operands are registers, the REX_X bit in REX is not
3857 used. We reuse it to encode the upper 16 registers, which is
3858 indicated by the REX_B bit in VREX. The REX_X bit is encoded
3859 as 1's compliment. */
3860 if ((i.vrex & REX_B))
3861 {
3862 vrex_used |= REX_B;
3863 i.vex.bytes[1] &= ~0x40;
3864 }
3865 }
3866
3867 /* EVEX instructions shouldn't need the REX prefix. */
3868 i.vrex &= ~vrex_used;
3869 gas_assert (i.vrex == 0);
3870
6865c043
L
3871 /* Check the REX.W bit and VEXW. */
3872 if (i.tm.opcode_modifier.vexw == VEXWIG)
3873 w = (evexwig == evexw1 || (i.rex & REX_W)) ? 1 : 0;
3874 else if (i.tm.opcode_modifier.vexw)
3875 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3876 else
931d03b7 3877 w = (flag_code == CODE_64BIT ? i.rex & REX_W : evexwig == evexw1) ? 1 : 0;
43234a1e 3878
43234a1e 3879 /* The third byte of the EVEX prefix. */
35648716
JB
3880 i.vex.bytes[2] = ((w << 7)
3881 | (register_specifier << 3)
3882 | 4 /* Encode the U bit. */
3883 | i.tm.opcode_modifier.opcodeprefix);
43234a1e
L
3884
3885 /* The fourth byte of the EVEX prefix. */
3886 /* The zeroing-masking bit. */
6225c532 3887 if (i.mask.reg && i.mask.zeroing)
43234a1e
L
3888 i.vex.bytes[3] |= 0x80;
3889
3890 /* Don't always set the broadcast bit if there is no RC. */
ca5312a2 3891 if (i.rounding.type == rc_none)
43234a1e
L
3892 {
3893 /* Encode the vector length. */
3894 unsigned int vec_length;
3895
e771e7c9
JB
3896 if (!i.tm.opcode_modifier.evex
3897 || i.tm.opcode_modifier.evex == EVEXDYN)
3898 {
56522fc5 3899 unsigned int op;
e771e7c9 3900
c7213af9
L
3901 /* Determine vector length from the last multi-length vector
3902 operand. */
56522fc5 3903 for (op = i.operands; op--;)
e771e7c9
JB
3904 if (i.tm.operand_types[op].bitfield.xmmword
3905 + i.tm.operand_types[op].bitfield.ymmword
3906 + i.tm.operand_types[op].bitfield.zmmword > 1)
3907 {
3908 if (i.types[op].bitfield.zmmword)
c7213af9
L
3909 {
3910 i.tm.opcode_modifier.evex = EVEX512;
3911 break;
3912 }
e771e7c9 3913 else if (i.types[op].bitfield.ymmword)
c7213af9
L
3914 {
3915 i.tm.opcode_modifier.evex = EVEX256;
3916 break;
3917 }
e771e7c9 3918 else if (i.types[op].bitfield.xmmword)
c7213af9
L
3919 {
3920 i.tm.opcode_modifier.evex = EVEX128;
3921 break;
3922 }
a5748e0d 3923 else if (i.broadcast.bytes && op == i.broadcast.operand)
625cbd7a 3924 {
a5748e0d 3925 switch (get_broadcast_bytes (&i.tm, true))
625cbd7a
JB
3926 {
3927 case 64:
3928 i.tm.opcode_modifier.evex = EVEX512;
3929 break;
3930 case 32:
3931 i.tm.opcode_modifier.evex = EVEX256;
3932 break;
3933 case 16:
3934 i.tm.opcode_modifier.evex = EVEX128;
3935 break;
3936 default:
c7213af9 3937 abort ();
625cbd7a 3938 }
c7213af9 3939 break;
625cbd7a 3940 }
e771e7c9 3941 }
c7213af9 3942
56522fc5 3943 if (op >= MAX_OPERANDS)
c7213af9 3944 abort ();
e771e7c9
JB
3945 }
3946
43234a1e
L
3947 switch (i.tm.opcode_modifier.evex)
3948 {
3949 case EVEXLIG: /* LL' is ignored */
3950 vec_length = evexlig << 5;
3951 break;
3952 case EVEX128:
3953 vec_length = 0 << 5;
3954 break;
3955 case EVEX256:
3956 vec_length = 1 << 5;
3957 break;
3958 case EVEX512:
3959 vec_length = 2 << 5;
3960 break;
3961 default:
3962 abort ();
3963 break;
3964 }
3965 i.vex.bytes[3] |= vec_length;
3966 /* Encode the broadcast bit. */
a5748e0d 3967 if (i.broadcast.bytes)
43234a1e
L
3968 i.vex.bytes[3] |= 0x10;
3969 }
ca5312a2
JB
3970 else if (i.rounding.type != saeonly)
3971 i.vex.bytes[3] |= 0x10 | (i.rounding.type << 5);
43234a1e 3972 else
ca5312a2 3973 i.vex.bytes[3] |= 0x10 | (evexrcig << 5);
43234a1e 3974
6225c532
JB
3975 if (i.mask.reg)
3976 i.vex.bytes[3] |= i.mask.reg->reg_num;
43234a1e
L
3977}
3978
65da13b5
L
3979static void
3980process_immext (void)
3981{
3982 expressionS *exp;
3983
c0f3af97 3984 /* These AMD 3DNow! and SSE2 instructions have an opcode suffix
65da13b5
L
3985 which is coded in the same place as an 8-bit immediate field
3986 would be. Here we fake an 8-bit immediate operand from the
3987 opcode suffix stored in tm.extension_opcode.
3988
c1e679ec 3989 AVX instructions also use this encoding, for some of
c0f3af97 3990 3 argument instructions. */
65da13b5 3991
43234a1e 3992 gas_assert (i.imm_operands <= 1
7ab9ffdd 3993 && (i.operands <= 2
7a8655d2 3994 || (is_any_vex_encoding (&i.tm)
7ab9ffdd 3995 && i.operands <= 4)));
65da13b5
L
3996
3997 exp = &im_expressions[i.imm_operands++];
3998 i.op[i.operands].imms = exp;
3999 i.types[i.operands] = imm8;
4000 i.operands++;
4001 exp->X_op = O_constant;
4002 exp->X_add_number = i.tm.extension_opcode;
4003 i.tm.extension_opcode = None;
4004}
4005
42164a71
L
4006
4007static int
4008check_hle (void)
4009{
742732c7 4010 switch (i.tm.opcode_modifier.prefixok)
42164a71
L
4011 {
4012 default:
4013 abort ();
742732c7
JB
4014 case PrefixLock:
4015 case PrefixNone:
4016 case PrefixNoTrack:
4017 case PrefixRep:
165de32a
L
4018 as_bad (_("invalid instruction `%s' after `%s'"),
4019 i.tm.name, i.hle_prefix);
42164a71 4020 return 0;
742732c7 4021 case PrefixHLELock:
42164a71
L
4022 if (i.prefix[LOCK_PREFIX])
4023 return 1;
165de32a 4024 as_bad (_("missing `lock' with `%s'"), i.hle_prefix);
42164a71 4025 return 0;
742732c7 4026 case PrefixHLEAny:
42164a71 4027 return 1;
742732c7 4028 case PrefixHLERelease:
42164a71
L
4029 if (i.prefix[HLE_PREFIX] != XRELEASE_PREFIX_OPCODE)
4030 {
4031 as_bad (_("instruction `%s' after `xacquire' not allowed"),
4032 i.tm.name);
4033 return 0;
4034 }
8dc0818e 4035 if (i.mem_operands == 0 || !(i.flags[i.operands - 1] & Operand_Mem))
42164a71
L
4036 {
4037 as_bad (_("memory destination needed for instruction `%s'"
4038 " after `xrelease'"), i.tm.name);
4039 return 0;
4040 }
4041 return 1;
4042 }
4043}
4044
c8480b58
L
4045/* Encode aligned vector move as unaligned vector move. */
4046
4047static void
4048encode_with_unaligned_vector_move (void)
4049{
4050 switch (i.tm.base_opcode)
4051 {
b3a9fe6f
L
4052 case 0x28: /* Load instructions. */
4053 case 0x29: /* Store instructions. */
c8480b58
L
4054 /* movaps/movapd/vmovaps/vmovapd. */
4055 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4056 && i.tm.opcode_modifier.opcodeprefix <= PREFIX_0X66)
b3a9fe6f 4057 i.tm.base_opcode = 0x10 | (i.tm.base_opcode & 1);
c8480b58 4058 break;
b3a9fe6f
L
4059 case 0x6f: /* Load instructions. */
4060 case 0x7f: /* Store instructions. */
c8480b58
L
4061 /* movdqa/vmovdqa/vmovdqa64/vmovdqa32. */
4062 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4063 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0X66)
4064 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
4065 break;
4066 default:
4067 break;
4068 }
4069}
4070
b6f8c7c4
L
4071/* Try the shortest encoding by shortening operand size. */
4072
4073static void
4074optimize_encoding (void)
4075{
a0a1771e 4076 unsigned int j;
b6f8c7c4 4077
fe134c65
JB
4078 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
4079 && i.tm.base_opcode == 0x8d)
4080 {
4081 /* Optimize: -O:
4082 lea symbol, %rN -> mov $symbol, %rN
4083 lea (%rM), %rN -> mov %rM, %rN
4084 lea (,%rM,1), %rN -> mov %rM, %rN
4085
4086 and in 32-bit mode for 16-bit addressing
4087
4088 lea (%rM), %rN -> movzx %rM, %rN
4089
4090 and in 64-bit mode zap 32-bit addressing in favor of using a
4091 32-bit (or less) destination.
4092 */
4093 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
4094 {
4095 if (!i.op[1].regs->reg_type.bitfield.word)
4096 i.tm.opcode_modifier.size = SIZE32;
4097 i.prefix[ADDR_PREFIX] = 0;
4098 }
4099
4100 if (!i.index_reg && !i.base_reg)
4101 {
4102 /* Handle:
4103 lea symbol, %rN -> mov $symbol, %rN
4104 */
4105 if (flag_code == CODE_64BIT)
4106 {
4107 /* Don't transform a relocation to a 16-bit one. */
4108 if (i.op[0].disps
4109 && i.op[0].disps->X_op != O_constant
4110 && i.op[1].regs->reg_type.bitfield.word)
4111 return;
4112
4113 if (!i.op[1].regs->reg_type.bitfield.qword
4114 || i.tm.opcode_modifier.size == SIZE32)
4115 {
4116 i.tm.base_opcode = 0xb8;
4117 i.tm.opcode_modifier.modrm = 0;
4118 if (!i.op[1].regs->reg_type.bitfield.word)
4119 i.types[0].bitfield.imm32 = 1;
4120 else
4121 {
4122 i.tm.opcode_modifier.size = SIZE16;
4123 i.types[0].bitfield.imm16 = 1;
4124 }
4125 }
4126 else
4127 {
4128 /* Subject to further optimization below. */
4129 i.tm.base_opcode = 0xc7;
4130 i.tm.extension_opcode = 0;
4131 i.types[0].bitfield.imm32s = 1;
4132 i.types[0].bitfield.baseindex = 0;
4133 }
4134 }
4135 /* Outside of 64-bit mode address and operand sizes have to match if
4136 a relocation is involved, as otherwise we wouldn't (currently) or
4137 even couldn't express the relocation correctly. */
4138 else if (i.op[0].disps
4139 && i.op[0].disps->X_op != O_constant
4140 && ((!i.prefix[ADDR_PREFIX])
4141 != (flag_code == CODE_32BIT
4142 ? i.op[1].regs->reg_type.bitfield.dword
4143 : i.op[1].regs->reg_type.bitfield.word)))
4144 return;
7772f168
JB
4145 /* In 16-bit mode converting LEA with 16-bit addressing and a 32-bit
4146 destination is going to grow encoding size. */
4147 else if (flag_code == CODE_16BIT
4148 && (optimize <= 1 || optimize_for_space)
4149 && !i.prefix[ADDR_PREFIX]
4150 && i.op[1].regs->reg_type.bitfield.dword)
4151 return;
fe134c65
JB
4152 else
4153 {
4154 i.tm.base_opcode = 0xb8;
4155 i.tm.opcode_modifier.modrm = 0;
4156 if (i.op[1].regs->reg_type.bitfield.dword)
4157 i.types[0].bitfield.imm32 = 1;
4158 else
4159 i.types[0].bitfield.imm16 = 1;
4160
4161 if (i.op[0].disps
4162 && i.op[0].disps->X_op == O_constant
4163 && i.op[1].regs->reg_type.bitfield.dword
60cfa10c
L
4164 /* NB: Add () to !i.prefix[ADDR_PREFIX] to silence
4165 GCC 5. */
4166 && (!i.prefix[ADDR_PREFIX]) != (flag_code == CODE_32BIT))
fe134c65
JB
4167 i.op[0].disps->X_add_number &= 0xffff;
4168 }
4169
4170 i.tm.operand_types[0] = i.types[0];
4171 i.imm_operands = 1;
4172 if (!i.op[0].imms)
4173 {
4174 i.op[0].imms = &im_expressions[0];
4175 i.op[0].imms->X_op = O_absent;
4176 }
4177 }
4178 else if (i.op[0].disps
4179 && (i.op[0].disps->X_op != O_constant
4180 || i.op[0].disps->X_add_number))
4181 return;
4182 else
4183 {
4184 /* Handle:
4185 lea (%rM), %rN -> mov %rM, %rN
4186 lea (,%rM,1), %rN -> mov %rM, %rN
4187 lea (%rM), %rN -> movzx %rM, %rN
4188 */
4189 const reg_entry *addr_reg;
4190
4191 if (!i.index_reg && i.base_reg->reg_num != RegIP)
4192 addr_reg = i.base_reg;
4193 else if (!i.base_reg
4194 && i.index_reg->reg_num != RegIZ
4195 && !i.log2_scale_factor)
4196 addr_reg = i.index_reg;
4197 else
4198 return;
4199
4200 if (addr_reg->reg_type.bitfield.word
4201 && i.op[1].regs->reg_type.bitfield.dword)
4202 {
4203 if (flag_code != CODE_32BIT)
4204 return;
4205 i.tm.opcode_modifier.opcodespace = SPACE_0F;
4206 i.tm.base_opcode = 0xb7;
4207 }
4208 else
4209 i.tm.base_opcode = 0x8b;
4210
4211 if (addr_reg->reg_type.bitfield.dword
4212 && i.op[1].regs->reg_type.bitfield.qword)
4213 i.tm.opcode_modifier.size = SIZE32;
4214
4215 i.op[0].regs = addr_reg;
4216 i.reg_operands = 2;
4217 }
4218
4219 i.mem_operands = 0;
4220 i.disp_operands = 0;
4221 i.prefix[ADDR_PREFIX] = 0;
4222 i.prefix[SEG_PREFIX] = 0;
4223 i.seg[0] = NULL;
4224 }
4225
b6f8c7c4 4226 if (optimize_for_space
389d00a5 4227 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
b6f8c7c4
L
4228 && i.reg_operands == 1
4229 && i.imm_operands == 1
4230 && !i.types[1].bitfield.byte
4231 && i.op[0].imms->X_op == O_constant
4232 && fits_in_imm7 (i.op[0].imms->X_add_number)
72aea328 4233 && (i.tm.base_opcode == 0xa8
b6f8c7c4
L
4234 || (i.tm.base_opcode == 0xf6
4235 && i.tm.extension_opcode == 0x0)))
4236 {
4237 /* Optimize: -Os:
4238 test $imm7, %r64/%r32/%r16 -> test $imm7, %r8
4239 */
4240 unsigned int base_regnum = i.op[1].regs->reg_num;
4241 if (flag_code == CODE_64BIT || base_regnum < 4)
4242 {
4243 i.types[1].bitfield.byte = 1;
4244 /* Ignore the suffix. */
4245 i.suffix = 0;
7697afb6
JB
4246 /* Convert to byte registers. */
4247 if (i.types[1].bitfield.word)
4248 j = 16;
4249 else if (i.types[1].bitfield.dword)
4250 j = 32;
4251 else
4252 j = 48;
4253 if (!(i.op[1].regs->reg_flags & RegRex) && base_regnum < 4)
4254 j += 8;
4255 i.op[1].regs -= j;
b6f8c7c4
L
4256 }
4257 }
4258 else if (flag_code == CODE_64BIT
389d00a5 4259 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
d3d50934
L
4260 && ((i.types[1].bitfield.qword
4261 && i.reg_operands == 1
b6f8c7c4
L
4262 && i.imm_operands == 1
4263 && i.op[0].imms->X_op == O_constant
507916b8 4264 && ((i.tm.base_opcode == 0xb8
b6f8c7c4
L
4265 && i.tm.extension_opcode == None
4266 && fits_in_unsigned_long (i.op[0].imms->X_add_number))
4267 || (fits_in_imm31 (i.op[0].imms->X_add_number)
72aea328
JB
4268 && ((i.tm.base_opcode == 0x24
4269 || i.tm.base_opcode == 0xa8)
b6f8c7c4
L
4270 || (i.tm.base_opcode == 0x80
4271 && i.tm.extension_opcode == 0x4)
4272 || ((i.tm.base_opcode == 0xf6
507916b8 4273 || (i.tm.base_opcode | 1) == 0xc7)
b8364fa7
JB
4274 && i.tm.extension_opcode == 0x0)))
4275 || (fits_in_imm7 (i.op[0].imms->X_add_number)
4276 && i.tm.base_opcode == 0x83
4277 && i.tm.extension_opcode == 0x4)))
d3d50934
L
4278 || (i.types[0].bitfield.qword
4279 && ((i.reg_operands == 2
4280 && i.op[0].regs == i.op[1].regs
72aea328
JB
4281 && (i.tm.base_opcode == 0x30
4282 || i.tm.base_opcode == 0x28))
d3d50934
L
4283 || (i.reg_operands == 1
4284 && i.operands == 1
72aea328 4285 && i.tm.base_opcode == 0x30)))))
b6f8c7c4
L
4286 {
4287 /* Optimize: -O:
4288 andq $imm31, %r64 -> andl $imm31, %r32
b8364fa7 4289 andq $imm7, %r64 -> andl $imm7, %r32
b6f8c7c4
L
4290 testq $imm31, %r64 -> testl $imm31, %r32
4291 xorq %r64, %r64 -> xorl %r32, %r32
4292 subq %r64, %r64 -> subl %r32, %r32
4293 movq $imm31, %r64 -> movl $imm31, %r32
4294 movq $imm32, %r64 -> movl $imm32, %r32
4295 */
4296 i.tm.opcode_modifier.norex64 = 1;
507916b8 4297 if (i.tm.base_opcode == 0xb8 || (i.tm.base_opcode | 1) == 0xc7)
b6f8c7c4
L
4298 {
4299 /* Handle
4300 movq $imm31, %r64 -> movl $imm31, %r32
4301 movq $imm32, %r64 -> movl $imm32, %r32
4302 */
4303 i.tm.operand_types[0].bitfield.imm32 = 1;
4304 i.tm.operand_types[0].bitfield.imm32s = 0;
4305 i.tm.operand_types[0].bitfield.imm64 = 0;
4306 i.types[0].bitfield.imm32 = 1;
4307 i.types[0].bitfield.imm32s = 0;
4308 i.types[0].bitfield.imm64 = 0;
4309 i.types[1].bitfield.dword = 1;
4310 i.types[1].bitfield.qword = 0;
507916b8 4311 if ((i.tm.base_opcode | 1) == 0xc7)
b6f8c7c4
L
4312 {
4313 /* Handle
4314 movq $imm31, %r64 -> movl $imm31, %r32
4315 */
507916b8 4316 i.tm.base_opcode = 0xb8;
b6f8c7c4 4317 i.tm.extension_opcode = None;
507916b8 4318 i.tm.opcode_modifier.w = 0;
b6f8c7c4
L
4319 i.tm.opcode_modifier.modrm = 0;
4320 }
4321 }
4322 }
5641ec01
JB
4323 else if (optimize > 1
4324 && !optimize_for_space
389d00a5 4325 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
5641ec01
JB
4326 && i.reg_operands == 2
4327 && i.op[0].regs == i.op[1].regs
4328 && ((i.tm.base_opcode & ~(Opcode_D | 1)) == 0x8
4329 || (i.tm.base_opcode & ~(Opcode_D | 1)) == 0x20)
4330 && (flag_code != CODE_64BIT || !i.types[0].bitfield.dword))
4331 {
4332 /* Optimize: -O2:
4333 andb %rN, %rN -> testb %rN, %rN
4334 andw %rN, %rN -> testw %rN, %rN
4335 andq %rN, %rN -> testq %rN, %rN
4336 orb %rN, %rN -> testb %rN, %rN
4337 orw %rN, %rN -> testw %rN, %rN
4338 orq %rN, %rN -> testq %rN, %rN
4339
4340 and outside of 64-bit mode
4341
4342 andl %rN, %rN -> testl %rN, %rN
4343 orl %rN, %rN -> testl %rN, %rN
4344 */
4345 i.tm.base_opcode = 0x84 | (i.tm.base_opcode & 1);
4346 }
99112332 4347 else if (i.reg_operands == 3
b6f8c7c4
L
4348 && i.op[0].regs == i.op[1].regs
4349 && !i.types[2].bitfield.xmmword
4350 && (i.tm.opcode_modifier.vex
6225c532 4351 || ((!i.mask.reg || i.mask.zeroing)
e771e7c9 4352 && is_evex_encoding (&i.tm)
80c34c38 4353 && (i.vec_encoding != vex_encoding_evex
dd22218c 4354 || cpu_arch_isa_flags.bitfield.cpuavx512vl
80c34c38 4355 || i.tm.cpu_flags.bitfield.cpuavx512vl
7091c612 4356 || (i.tm.operand_types[2].bitfield.zmmword
dd22218c 4357 && i.types[2].bitfield.ymmword))))
5844ccaa
JB
4358 && i.tm.opcode_modifier.opcodespace == SPACE_0F
4359 && ((i.tm.base_opcode | 2) == 0x57
4360 || i.tm.base_opcode == 0xdf
4361 || i.tm.base_opcode == 0xef
4362 || (i.tm.base_opcode | 3) == 0xfb
4363 || i.tm.base_opcode == 0x42
4364 || i.tm.base_opcode == 0x47))
b6f8c7c4 4365 {
99112332 4366 /* Optimize: -O1:
8305403a
L
4367 VOP, one of vandnps, vandnpd, vxorps, vxorpd, vpsubb, vpsubd,
4368 vpsubq and vpsubw:
b6f8c7c4
L
4369 EVEX VOP %zmmM, %zmmM, %zmmN
4370 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4371 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4372 EVEX VOP %ymmM, %ymmM, %ymmN
4373 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4374 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4375 VEX VOP %ymmM, %ymmM, %ymmN
4376 -> VEX VOP %xmmM, %xmmM, %xmmN
4377 VOP, one of vpandn and vpxor:
4378 VEX VOP %ymmM, %ymmM, %ymmN
4379 -> VEX VOP %xmmM, %xmmM, %xmmN
4380 VOP, one of vpandnd and vpandnq:
4381 EVEX VOP %zmmM, %zmmM, %zmmN
4382 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4383 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4384 EVEX VOP %ymmM, %ymmM, %ymmN
4385 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4386 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4387 VOP, one of vpxord and vpxorq:
4388 EVEX VOP %zmmM, %zmmM, %zmmN
4389 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4390 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4391 EVEX VOP %ymmM, %ymmM, %ymmN
4392 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4393 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
1424ad86
JB
4394 VOP, one of kxord and kxorq:
4395 VEX VOP %kM, %kM, %kN
4396 -> VEX kxorw %kM, %kM, %kN
4397 VOP, one of kandnd and kandnq:
4398 VEX VOP %kM, %kM, %kN
4399 -> VEX kandnw %kM, %kM, %kN
b6f8c7c4 4400 */
e771e7c9 4401 if (is_evex_encoding (&i.tm))
b6f8c7c4 4402 {
7b1d7ca1 4403 if (i.vec_encoding != vex_encoding_evex)
b6f8c7c4
L
4404 {
4405 i.tm.opcode_modifier.vex = VEX128;
4406 i.tm.opcode_modifier.vexw = VEXW0;
4407 i.tm.opcode_modifier.evex = 0;
4408 }
7b1d7ca1 4409 else if (optimize > 1)
dd22218c
L
4410 i.tm.opcode_modifier.evex = EVEX128;
4411 else
4412 return;
b6f8c7c4 4413 }
f74a6307 4414 else if (i.tm.operand_types[0].bitfield.class == RegMask)
1424ad86 4415 {
35648716 4416 i.tm.opcode_modifier.opcodeprefix = PREFIX_NONE;
1424ad86
JB
4417 i.tm.opcode_modifier.vexw = VEXW0;
4418 }
b6f8c7c4
L
4419 else
4420 i.tm.opcode_modifier.vex = VEX128;
4421
4422 if (i.tm.opcode_modifier.vex)
4423 for (j = 0; j < 3; j++)
4424 {
4425 i.types[j].bitfield.xmmword = 1;
4426 i.types[j].bitfield.ymmword = 0;
4427 }
4428 }
392a5972 4429 else if (i.vec_encoding != vex_encoding_evex
97ed31ae 4430 && !i.types[0].bitfield.zmmword
392a5972 4431 && !i.types[1].bitfield.zmmword
6225c532 4432 && !i.mask.reg
a5748e0d 4433 && !i.broadcast.bytes
97ed31ae 4434 && is_evex_encoding (&i.tm)
35648716
JB
4435 && ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4436 || (i.tm.base_opcode & ~4) == 0xdb
4437 || (i.tm.base_opcode & ~4) == 0xeb)
97ed31ae
L
4438 && i.tm.extension_opcode == None)
4439 {
4440 /* Optimize: -O1:
4441 VOP, one of vmovdqa32, vmovdqa64, vmovdqu8, vmovdqu16,
4442 vmovdqu32 and vmovdqu64:
4443 EVEX VOP %xmmM, %xmmN
4444 -> VEX vmovdqa|vmovdqu %xmmM, %xmmN (M and N < 16)
4445 EVEX VOP %ymmM, %ymmN
4446 -> VEX vmovdqa|vmovdqu %ymmM, %ymmN (M and N < 16)
4447 EVEX VOP %xmmM, mem
4448 -> VEX vmovdqa|vmovdqu %xmmM, mem (M < 16)
4449 EVEX VOP %ymmM, mem
4450 -> VEX vmovdqa|vmovdqu %ymmM, mem (M < 16)
4451 EVEX VOP mem, %xmmN
4452 -> VEX mvmovdqa|vmovdquem, %xmmN (N < 16)
4453 EVEX VOP mem, %ymmN
4454 -> VEX vmovdqa|vmovdqu mem, %ymmN (N < 16)
a0a1771e
JB
4455 VOP, one of vpand, vpandn, vpor, vpxor:
4456 EVEX VOP{d,q} %xmmL, %xmmM, %xmmN
4457 -> VEX VOP %xmmL, %xmmM, %xmmN (L, M, and N < 16)
4458 EVEX VOP{d,q} %ymmL, %ymmM, %ymmN
4459 -> VEX VOP %ymmL, %ymmM, %ymmN (L, M, and N < 16)
4460 EVEX VOP{d,q} mem, %xmmM, %xmmN
4461 -> VEX VOP mem, %xmmM, %xmmN (M and N < 16)
4462 EVEX VOP{d,q} mem, %ymmM, %ymmN
4463 -> VEX VOP mem, %ymmM, %ymmN (M and N < 16)
97ed31ae 4464 */
a0a1771e 4465 for (j = 0; j < i.operands; j++)
392a5972
L
4466 if (operand_type_check (i.types[j], disp)
4467 && i.op[j].disps->X_op == O_constant)
4468 {
4469 /* Since the VEX prefix has 2 or 3 bytes, the EVEX prefix
4470 has 4 bytes, EVEX Disp8 has 1 byte and VEX Disp32 has 4
4471 bytes, we choose EVEX Disp8 over VEX Disp32. */
4472 int evex_disp8, vex_disp8;
4473 unsigned int memshift = i.memshift;
4474 offsetT n = i.op[j].disps->X_add_number;
4475
4476 evex_disp8 = fits_in_disp8 (n);
4477 i.memshift = 0;
4478 vex_disp8 = fits_in_disp8 (n);
4479 if (evex_disp8 != vex_disp8)
4480 {
4481 i.memshift = memshift;
4482 return;
4483 }
4484
4485 i.types[j].bitfield.disp8 = vex_disp8;
4486 break;
4487 }
35648716
JB
4488 if ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4489 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2)
4490 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
97ed31ae
L
4491 i.tm.opcode_modifier.vex
4492 = i.types[0].bitfield.ymmword ? VEX256 : VEX128;
4493 i.tm.opcode_modifier.vexw = VEXW0;
79dec6b7 4494 /* VPAND, VPOR, and VPXOR are commutative. */
35648716 4495 if (i.reg_operands == 3 && i.tm.base_opcode != 0xdf)
79dec6b7 4496 i.tm.opcode_modifier.commutative = 1;
97ed31ae
L
4497 i.tm.opcode_modifier.evex = 0;
4498 i.tm.opcode_modifier.masking = 0;
a0a1771e 4499 i.tm.opcode_modifier.broadcast = 0;
97ed31ae
L
4500 i.tm.opcode_modifier.disp8memshift = 0;
4501 i.memshift = 0;
a0a1771e
JB
4502 if (j < i.operands)
4503 i.types[j].bitfield.disp8
4504 = fits_in_disp8 (i.op[j].disps->X_add_number);
97ed31ae 4505 }
b6f8c7c4
L
4506}
4507
ae531041
L
4508/* Return non-zero for load instruction. */
4509
4510static int
4511load_insn_p (void)
4512{
4513 unsigned int dest;
4514 int any_vex_p = is_any_vex_encoding (&i.tm);
4515 unsigned int base_opcode = i.tm.base_opcode | 1;
4516
4517 if (!any_vex_p)
4518 {
ef07be45
CL
4519 /* Anysize insns: lea, invlpg, clflush, prefetch*, bndmk, bndcl, bndcu,
4520 bndcn, bndstx, bndldx, clflushopt, clwb, cldemote. */
a09f656b 4521 if (i.tm.opcode_modifier.anysize)
ae531041
L
4522 return 0;
4523
389d00a5
JB
4524 /* pop. */
4525 if (strcmp (i.tm.name, "pop") == 0)
4526 return 1;
4527 }
4528
4529 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE)
4530 {
4531 /* popf, popa. */
4532 if (i.tm.base_opcode == 0x9d
a09f656b 4533 || i.tm.base_opcode == 0x61)
ae531041
L
4534 return 1;
4535
4536 /* movs, cmps, lods, scas. */
4537 if ((i.tm.base_opcode | 0xb) == 0xaf)
4538 return 1;
4539
a09f656b 4540 /* outs, xlatb. */
4541 if (base_opcode == 0x6f
4542 || i.tm.base_opcode == 0xd7)
ae531041 4543 return 1;
a09f656b 4544 /* NB: For AMD-specific insns with implicit memory operands,
4545 they're intentionally not covered. */
ae531041
L
4546 }
4547
4548 /* No memory operand. */
4549 if (!i.mem_operands)
4550 return 0;
4551
4552 if (any_vex_p)
4553 {
4554 /* vldmxcsr. */
4555 if (i.tm.base_opcode == 0xae
4556 && i.tm.opcode_modifier.vex
441f6aca 4557 && i.tm.opcode_modifier.opcodespace == SPACE_0F
35648716 4558 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
ae531041
L
4559 && i.tm.extension_opcode == 2)
4560 return 1;
4561 }
389d00a5 4562 else if (i.tm.opcode_modifier.opcodespace == SPACE_BASE)
ae531041
L
4563 {
4564 /* test, not, neg, mul, imul, div, idiv. */
4565 if ((i.tm.base_opcode == 0xf6 || i.tm.base_opcode == 0xf7)
4566 && i.tm.extension_opcode != 1)
4567 return 1;
4568
4569 /* inc, dec. */
4570 if (base_opcode == 0xff && i.tm.extension_opcode <= 1)
4571 return 1;
4572
4573 /* add, or, adc, sbb, and, sub, xor, cmp. */
4574 if (i.tm.base_opcode >= 0x80 && i.tm.base_opcode <= 0x83)
4575 return 1;
4576
ae531041
L
4577 /* rol, ror, rcl, rcr, shl/sal, shr, sar. */
4578 if ((base_opcode == 0xc1
4579 || (i.tm.base_opcode >= 0xd0 && i.tm.base_opcode <= 0xd3))
4580 && i.tm.extension_opcode != 6)
4581 return 1;
4582
ae531041 4583 /* Check for x87 instructions. */
389d00a5 4584 if (base_opcode >= 0xd8 && base_opcode <= 0xdf)
ae531041
L
4585 {
4586 /* Skip fst, fstp, fstenv, fstcw. */
4587 if (i.tm.base_opcode == 0xd9
4588 && (i.tm.extension_opcode == 2
4589 || i.tm.extension_opcode == 3
4590 || i.tm.extension_opcode == 6
4591 || i.tm.extension_opcode == 7))
4592 return 0;
4593
4594 /* Skip fisttp, fist, fistp, fstp. */
4595 if (i.tm.base_opcode == 0xdb
4596 && (i.tm.extension_opcode == 1
4597 || i.tm.extension_opcode == 2
4598 || i.tm.extension_opcode == 3
4599 || i.tm.extension_opcode == 7))
4600 return 0;
4601
4602 /* Skip fisttp, fst, fstp, fsave, fstsw. */
4603 if (i.tm.base_opcode == 0xdd
4604 && (i.tm.extension_opcode == 1
4605 || i.tm.extension_opcode == 2
4606 || i.tm.extension_opcode == 3
4607 || i.tm.extension_opcode == 6
4608 || i.tm.extension_opcode == 7))
4609 return 0;
4610
4611 /* Skip fisttp, fist, fistp, fbstp, fistp. */
4612 if (i.tm.base_opcode == 0xdf
4613 && (i.tm.extension_opcode == 1
4614 || i.tm.extension_opcode == 2
4615 || i.tm.extension_opcode == 3
4616 || i.tm.extension_opcode == 6
4617 || i.tm.extension_opcode == 7))
4618 return 0;
4619
4620 return 1;
4621 }
4622 }
389d00a5
JB
4623 else if (i.tm.opcode_modifier.opcodespace == SPACE_0F)
4624 {
4625 /* bt, bts, btr, btc. */
4626 if (i.tm.base_opcode == 0xba
4627 && (i.tm.extension_opcode >= 4 && i.tm.extension_opcode <= 7))
4628 return 1;
4629
4630 /* cmpxchg8b, cmpxchg16b, xrstors, vmptrld. */
4631 if (i.tm.base_opcode == 0xc7
4632 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
4633 && (i.tm.extension_opcode == 1 || i.tm.extension_opcode == 3
4634 || i.tm.extension_opcode == 6))
4635 return 1;
4636
4637 /* fxrstor, ldmxcsr, xrstor. */
4638 if (i.tm.base_opcode == 0xae
4639 && (i.tm.extension_opcode == 1
4640 || i.tm.extension_opcode == 2
4641 || i.tm.extension_opcode == 5))
4642 return 1;
4643
4644 /* lgdt, lidt, lmsw. */
4645 if (i.tm.base_opcode == 0x01
4646 && (i.tm.extension_opcode == 2
4647 || i.tm.extension_opcode == 3
4648 || i.tm.extension_opcode == 6))
4649 return 1;
4650 }
ae531041
L
4651
4652 dest = i.operands - 1;
4653
4654 /* Check fake imm8 operand and 3 source operands. */
4655 if ((i.tm.opcode_modifier.immext
4656 || i.tm.opcode_modifier.vexsources == VEX3SOURCES)
4657 && i.types[dest].bitfield.imm8)
4658 dest--;
4659
389d00a5
JB
4660 /* add, or, adc, sbb, and, sub, xor, cmp, test, xchg. */
4661 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
ae531041
L
4662 && (base_opcode == 0x1
4663 || base_opcode == 0x9
4664 || base_opcode == 0x11
4665 || base_opcode == 0x19
4666 || base_opcode == 0x21
4667 || base_opcode == 0x29
4668 || base_opcode == 0x31
4669 || base_opcode == 0x39
389d00a5
JB
4670 || (base_opcode | 2) == 0x87))
4671 return 1;
4672
4673 /* xadd. */
4674 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4675 && base_opcode == 0xc1)
ae531041
L
4676 return 1;
4677
4678 /* Check for load instruction. */
4679 return (i.types[dest].bitfield.class != ClassNone
4680 || i.types[dest].bitfield.instance == Accum);
4681}
4682
4683/* Output lfence, 0xfaee8, after instruction. */
4684
4685static void
4686insert_lfence_after (void)
4687{
4688 if (lfence_after_load && load_insn_p ())
4689 {
a09f656b 4690 /* There are also two REP string instructions that require
4691 special treatment. Specifically, the compare string (CMPS)
4692 and scan string (SCAS) instructions set EFLAGS in a manner
4693 that depends on the data being compared/scanned. When used
4694 with a REP prefix, the number of iterations may therefore
4695 vary depending on this data. If the data is a program secret
4696 chosen by the adversary using an LVI method,
4697 then this data-dependent behavior may leak some aspect
4698 of the secret. */
4699 if (((i.tm.base_opcode | 0x1) == 0xa7
4700 || (i.tm.base_opcode | 0x1) == 0xaf)
4701 && i.prefix[REP_PREFIX])
4702 {
4703 as_warn (_("`%s` changes flags which would affect control flow behavior"),
4704 i.tm.name);
4705 }
ae531041
L
4706 char *p = frag_more (3);
4707 *p++ = 0xf;
4708 *p++ = 0xae;
4709 *p = 0xe8;
4710 }
4711}
4712
4713/* Output lfence, 0xfaee8, before instruction. */
4714
4715static void
4716insert_lfence_before (void)
4717{
4718 char *p;
4719
389d00a5 4720 if (i.tm.opcode_modifier.opcodespace != SPACE_BASE)
ae531041
L
4721 return;
4722
4723 if (i.tm.base_opcode == 0xff
4724 && (i.tm.extension_opcode == 2 || i.tm.extension_opcode == 4))
4725 {
4726 /* Insert lfence before indirect branch if needed. */
4727
4728 if (lfence_before_indirect_branch == lfence_branch_none)
4729 return;
4730
4731 if (i.operands != 1)
4732 abort ();
4733
4734 if (i.reg_operands == 1)
4735 {
4736 /* Indirect branch via register. Don't insert lfence with
4737 -mlfence-after-load=yes. */
4738 if (lfence_after_load
4739 || lfence_before_indirect_branch == lfence_branch_memory)
4740 return;
4741 }
4742 else if (i.mem_operands == 1
4743 && lfence_before_indirect_branch != lfence_branch_register)
4744 {
4745 as_warn (_("indirect `%s` with memory operand should be avoided"),
4746 i.tm.name);
4747 return;
4748 }
4749 else
4750 return;
4751
4752 if (last_insn.kind != last_insn_other
4753 && last_insn.seg == now_seg)
4754 {
4755 as_warn_where (last_insn.file, last_insn.line,
4756 _("`%s` skips -mlfence-before-indirect-branch on `%s`"),
4757 last_insn.name, i.tm.name);
4758 return;
4759 }
4760
4761 p = frag_more (3);
4762 *p++ = 0xf;
4763 *p++ = 0xae;
4764 *p = 0xe8;
4765 return;
4766 }
4767
503648e4 4768 /* Output or/not/shl and lfence before near ret. */
ae531041
L
4769 if (lfence_before_ret != lfence_before_ret_none
4770 && (i.tm.base_opcode == 0xc2
503648e4 4771 || i.tm.base_opcode == 0xc3))
ae531041
L
4772 {
4773 if (last_insn.kind != last_insn_other
4774 && last_insn.seg == now_seg)
4775 {
4776 as_warn_where (last_insn.file, last_insn.line,
4777 _("`%s` skips -mlfence-before-ret on `%s`"),
4778 last_insn.name, i.tm.name);
4779 return;
4780 }
a09f656b 4781
a09f656b 4782 /* Near ret ingore operand size override under CPU64. */
503648e4 4783 char prefix = flag_code == CODE_64BIT
4784 ? 0x48
4785 : i.prefix[DATA_PREFIX] ? 0x66 : 0x0;
a09f656b 4786
4787 if (lfence_before_ret == lfence_before_ret_not)
4788 {
4789 /* not: 0xf71424, may add prefix
4790 for operand size override or 64-bit code. */
4791 p = frag_more ((prefix ? 2 : 0) + 6 + 3);
4792 if (prefix)
4793 *p++ = prefix;
ae531041
L
4794 *p++ = 0xf7;
4795 *p++ = 0x14;
4796 *p++ = 0x24;
a09f656b 4797 if (prefix)
4798 *p++ = prefix;
ae531041
L
4799 *p++ = 0xf7;
4800 *p++ = 0x14;
4801 *p++ = 0x24;
4802 }
a09f656b 4803 else
4804 {
4805 p = frag_more ((prefix ? 1 : 0) + 4 + 3);
4806 if (prefix)
4807 *p++ = prefix;
4808 if (lfence_before_ret == lfence_before_ret_or)
4809 {
4810 /* or: 0x830c2400, may add prefix
4811 for operand size override or 64-bit code. */
4812 *p++ = 0x83;
4813 *p++ = 0x0c;
4814 }
4815 else
4816 {
4817 /* shl: 0xc1242400, may add prefix
4818 for operand size override or 64-bit code. */
4819 *p++ = 0xc1;
4820 *p++ = 0x24;
4821 }
4822
4823 *p++ = 0x24;
4824 *p++ = 0x0;
4825 }
4826
ae531041
L
4827 *p++ = 0xf;
4828 *p++ = 0xae;
4829 *p = 0xe8;
4830 }
4831}
4832
252b5132
RH
4833/* This is the guts of the machine-dependent assembler. LINE points to a
4834 machine dependent instruction. This function is supposed to emit
4835 the frags/bytes it assembles to. */
4836
4837void
65da13b5 4838md_assemble (char *line)
252b5132 4839{
40fb9820 4840 unsigned int j;
83b16ac6 4841 char mnemonic[MAX_MNEM_SIZE], mnem_suffix;
d3ce72d0 4842 const insn_template *t;
252b5132 4843
47926f60 4844 /* Initialize globals. */
252b5132 4845 memset (&i, '\0', sizeof (i));
ca5312a2 4846 i.rounding.type = rc_none;
252b5132 4847 for (j = 0; j < MAX_OPERANDS; j++)
1ae12ab7 4848 i.reloc[j] = NO_RELOC;
252b5132
RH
4849 memset (disp_expressions, '\0', sizeof (disp_expressions));
4850 memset (im_expressions, '\0', sizeof (im_expressions));
ce8a8b2f 4851 save_stack_p = save_stack;
252b5132
RH
4852
4853 /* First parse an instruction mnemonic & call i386_operand for the operands.
4854 We assume that the scrubber has arranged it so that line[0] is the valid
47926f60 4855 start of a (possibly prefixed) mnemonic. */
252b5132 4856
29b0f896
AM
4857 line = parse_insn (line, mnemonic);
4858 if (line == NULL)
4859 return;
83b16ac6 4860 mnem_suffix = i.suffix;
252b5132 4861
29b0f896 4862 line = parse_operands (line, mnemonic);
ee86248c 4863 this_operand = -1;
8325cc63
JB
4864 xfree (i.memop1_string);
4865 i.memop1_string = NULL;
29b0f896
AM
4866 if (line == NULL)
4867 return;
252b5132 4868
29b0f896
AM
4869 /* Now we've parsed the mnemonic into a set of templates, and have the
4870 operands at hand. */
4871
b630c145 4872 /* All Intel opcodes have reversed operands except for "bound", "enter",
c0e54661
JB
4873 "invlpg*", "monitor*", "mwait*", "tpause", "umwait", "pvalidate",
4874 "rmpadjust", and "rmpupdate". We also don't reverse intersegment "jmp"
4875 and "call" instructions with 2 immediate operands so that the immediate
4876 segment precedes the offset consistently in Intel and AT&T modes. */
4d456e3d
L
4877 if (intel_syntax
4878 && i.operands > 1
29b0f896 4879 && (strcmp (mnemonic, "bound") != 0)
c0e54661 4880 && (strncmp (mnemonic, "invlpg", 6) != 0)
d34049e8
ML
4881 && !startswith (mnemonic, "monitor")
4882 && !startswith (mnemonic, "mwait")
c0e54661 4883 && (strcmp (mnemonic, "pvalidate") != 0)
d34049e8 4884 && !startswith (mnemonic, "rmp")
b630c145
JB
4885 && (strcmp (mnemonic, "tpause") != 0)
4886 && (strcmp (mnemonic, "umwait") != 0)
40fb9820
L
4887 && !(operand_type_check (i.types[0], imm)
4888 && operand_type_check (i.types[1], imm)))
29b0f896
AM
4889 swap_operands ();
4890
ec56d5c0
JB
4891 /* The order of the immediates should be reversed
4892 for 2 immediates extrq and insertq instructions */
4893 if (i.imm_operands == 2
4894 && (strcmp (mnemonic, "extrq") == 0
4895 || strcmp (mnemonic, "insertq") == 0))
4896 swap_2_operands (0, 1);
4897
29b0f896
AM
4898 if (i.imm_operands)
4899 optimize_imm ();
4900
9386188e
JB
4901 if (i.disp_operands && !want_disp32 (current_templates->start)
4902 && (!current_templates->start->opcode_modifier.jump
4903 || i.jumpabsolute || i.types[0].bitfield.baseindex))
cce08655
JB
4904 {
4905 for (j = 0; j < i.operands; ++j)
4906 {
4907 const expressionS *exp = i.op[j].disps;
4908
4909 if (!operand_type_check (i.types[j], disp))
4910 continue;
4911
4912 if (exp->X_op != O_constant)
4913 continue;
4914
4915 /* Since displacement is signed extended to 64bit, don't allow
a775efc8 4916 disp32 if it is out of range. */
cce08655
JB
4917 if (fits_in_signed_long (exp->X_add_number))
4918 continue;
4919
a775efc8 4920 i.types[j].bitfield.disp32 = 0;
cce08655
JB
4921 if (i.types[j].bitfield.baseindex)
4922 {
f493c217
AM
4923 as_bad (_("0x%" PRIx64 " out of range of signed 32bit displacement"),
4924 (uint64_t) exp->X_add_number);
cce08655
JB
4925 return;
4926 }
4927 }
4928 }
4929
b300c311
L
4930 /* Don't optimize displacement for movabs since it only takes 64bit
4931 displacement. */
4932 if (i.disp_operands
1a42a9fe 4933 && i.disp_encoding <= disp_encoding_8bit
862be3fb
L
4934 && (flag_code != CODE_64BIT
4935 || strcmp (mnemonic, "movabs") != 0))
4936 optimize_disp ();
29b0f896
AM
4937
4938 /* Next, we find a template that matches the given insn,
4939 making sure the overlap of the given operands types is consistent
4940 with the template operand types. */
252b5132 4941
83b16ac6 4942 if (!(t = match_template (mnem_suffix)))
29b0f896 4943 return;
252b5132 4944
7bab8ab5 4945 if (sse_check != check_none
ffb86450
JB
4946 /* The opcode space check isn't strictly needed; it's there only to
4947 bypass the logic below when easily possible. */
4948 && t->opcode_modifier.opcodespace >= SPACE_0F
4949 && t->opcode_modifier.opcodespace <= SPACE_0F3A
4950 && !i.tm.cpu_flags.bitfield.cpusse4a
4951 && !is_any_vex_encoding (t))
daf50ae7 4952 {
ffb86450
JB
4953 bool simd = false;
4954
4955 for (j = 0; j < t->operands; ++j)
4956 {
4957 if (t->operand_types[j].bitfield.class == RegMMX)
4958 break;
4959 if (t->operand_types[j].bitfield.class == RegSIMD)
4960 simd = true;
4961 }
4962
4963 if (j >= t->operands && simd)
4964 (sse_check == check_warning
4965 ? as_warn
4966 : as_bad) (_("SSE instruction `%s' is used"), i.tm.name);
daf50ae7
L
4967 }
4968
40fb9820 4969 if (i.tm.opcode_modifier.fwait)
29b0f896
AM
4970 if (!add_prefix (FWAIT_OPCODE))
4971 return;
252b5132 4972
d5de92cf 4973 /* Check if REP prefix is OK. */
742732c7 4974 if (i.rep_prefix && i.tm.opcode_modifier.prefixok != PrefixRep)
d5de92cf
L
4975 {
4976 as_bad (_("invalid instruction `%s' after `%s'"),
4977 i.tm.name, i.rep_prefix);
4978 return;
4979 }
4980
c1ba0266
L
4981 /* Check for lock without a lockable instruction. Destination operand
4982 must be memory unless it is xchg (0x86). */
c32fa91d 4983 if (i.prefix[LOCK_PREFIX]
742732c7 4984 && (i.tm.opcode_modifier.prefixok < PrefixLock
c1ba0266
L
4985 || i.mem_operands == 0
4986 || (i.tm.base_opcode != 0x86
8dc0818e 4987 && !(i.flags[i.operands - 1] & Operand_Mem))))
c32fa91d
L
4988 {
4989 as_bad (_("expecting lockable instruction after `lock'"));
4990 return;
4991 }
4992
40d231b4
JB
4993 /* Check for data size prefix on VEX/XOP/EVEX encoded and SIMD insns. */
4994 if (i.prefix[DATA_PREFIX]
4995 && (is_any_vex_encoding (&i.tm)
4996 || i.tm.operand_types[i.imm_operands].bitfield.class >= RegMMX
4997 || i.tm.operand_types[i.imm_operands + 1].bitfield.class >= RegMMX))
7a8655d2
JB
4998 {
4999 as_bad (_("data size prefix invalid with `%s'"), i.tm.name);
5000 return;
5001 }
5002
42164a71 5003 /* Check if HLE prefix is OK. */
165de32a 5004 if (i.hle_prefix && !check_hle ())
42164a71
L
5005 return;
5006
7e8b059b
L
5007 /* Check BND prefix. */
5008 if (i.bnd_prefix && !i.tm.opcode_modifier.bndprefixok)
5009 as_bad (_("expecting valid branch instruction after `bnd'"));
5010
04ef582a 5011 /* Check NOTRACK prefix. */
742732c7 5012 if (i.notrack_prefix && i.tm.opcode_modifier.prefixok != PrefixNoTrack)
9fef80d6 5013 as_bad (_("expecting indirect branch instruction after `notrack'"));
04ef582a 5014
327e8c42
JB
5015 if (i.tm.cpu_flags.bitfield.cpumpx)
5016 {
5017 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
5018 as_bad (_("32-bit address isn't allowed in 64-bit MPX instructions."));
5019 else if (flag_code != CODE_16BIT
5020 ? i.prefix[ADDR_PREFIX]
5021 : i.mem_operands && !i.prefix[ADDR_PREFIX])
5022 as_bad (_("16-bit address isn't allowed in MPX instructions"));
5023 }
7e8b059b
L
5024
5025 /* Insert BND prefix. */
76d3a78a
JB
5026 if (add_bnd_prefix && i.tm.opcode_modifier.bndprefixok)
5027 {
5028 if (!i.prefix[BND_PREFIX])
5029 add_prefix (BND_PREFIX_OPCODE);
5030 else if (i.prefix[BND_PREFIX] != BND_PREFIX_OPCODE)
5031 {
5032 as_warn (_("replacing `rep'/`repe' prefix by `bnd'"));
5033 i.prefix[BND_PREFIX] = BND_PREFIX_OPCODE;
5034 }
5035 }
7e8b059b 5036
29b0f896 5037 /* Check string instruction segment overrides. */
51c8edf6 5038 if (i.tm.opcode_modifier.isstring >= IS_STRING_ES_OP0)
29b0f896 5039 {
51c8edf6 5040 gas_assert (i.mem_operands);
29b0f896 5041 if (!check_string ())
5dd0794d 5042 return;
fc0763e6 5043 i.disp_operands = 0;
29b0f896 5044 }
5dd0794d 5045
b6f8c7c4
L
5046 if (optimize && !i.no_optimize && i.tm.opcode_modifier.optimize)
5047 optimize_encoding ();
5048
c8480b58
L
5049 if (use_unaligned_vector_move)
5050 encode_with_unaligned_vector_move ();
5051
29b0f896
AM
5052 if (!process_suffix ())
5053 return;
e413e4e9 5054
ef07be45
CL
5055 /* Check if IP-relative addressing requirements can be satisfied. */
5056 if (i.tm.cpu_flags.bitfield.cpuprefetchi
5057 && !(i.base_reg && i.base_reg->reg_num == RegIP))
f2462532 5058 as_warn (_("'%s' only supports RIP-relative address"), i.tm.name);
ef07be45 5059
921eafea 5060 /* Update operand types and check extended states. */
bc0844ae 5061 for (j = 0; j < i.operands; j++)
921eafea
L
5062 {
5063 i.types[j] = operand_type_and (i.types[j], i.tm.operand_types[j]);
3d70986f 5064 switch (i.tm.operand_types[j].bitfield.class)
921eafea
L
5065 {
5066 default:
5067 break;
5068 case RegMMX:
5069 i.xstate |= xstate_mmx;
5070 break;
5071 case RegMask:
32930e4e 5072 i.xstate |= xstate_mask;
921eafea
L
5073 break;
5074 case RegSIMD:
3d70986f 5075 if (i.tm.operand_types[j].bitfield.tmmword)
921eafea 5076 i.xstate |= xstate_tmm;
3d70986f 5077 else if (i.tm.operand_types[j].bitfield.zmmword)
921eafea 5078 i.xstate |= xstate_zmm;
3d70986f 5079 else if (i.tm.operand_types[j].bitfield.ymmword)
921eafea 5080 i.xstate |= xstate_ymm;
3d70986f 5081 else if (i.tm.operand_types[j].bitfield.xmmword)
921eafea
L
5082 i.xstate |= xstate_xmm;
5083 break;
5084 }
5085 }
bc0844ae 5086
29b0f896
AM
5087 /* Make still unresolved immediate matches conform to size of immediate
5088 given in i.suffix. */
5089 if (!finalize_imm ())
5090 return;
252b5132 5091
40fb9820 5092 if (i.types[0].bitfield.imm1)
29b0f896 5093 i.imm_operands = 0; /* kludge for shift insns. */
252b5132 5094
9afe6eb8
L
5095 /* We only need to check those implicit registers for instructions
5096 with 3 operands or less. */
5097 if (i.operands <= 3)
5098 for (j = 0; j < i.operands; j++)
75e5731b
JB
5099 if (i.types[j].bitfield.instance != InstanceNone
5100 && !i.types[j].bitfield.xmmword)
9afe6eb8 5101 i.reg_operands--;
40fb9820 5102
29b0f896
AM
5103 /* For insns with operands there are more diddles to do to the opcode. */
5104 if (i.operands)
5105 {
5106 if (!process_operands ())
5107 return;
5108 }
8c190ce0 5109 else if (!quiet_warnings && i.tm.opcode_modifier.ugh)
29b0f896
AM
5110 {
5111 /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc. */
5112 as_warn (_("translating to `%sp'"), i.tm.name);
5113 }
252b5132 5114
7a8655d2 5115 if (is_any_vex_encoding (&i.tm))
9e5e5283 5116 {
c1dc7af5 5117 if (!cpu_arch_flags.bitfield.cpui286)
9e5e5283 5118 {
c1dc7af5 5119 as_bad (_("instruction `%s' isn't supported outside of protected mode."),
9e5e5283
L
5120 i.tm.name);
5121 return;
5122 }
c0f3af97 5123
0b9404fd
JB
5124 /* Check for explicit REX prefix. */
5125 if (i.prefix[REX_PREFIX] || i.rex_encoding)
5126 {
5127 as_bad (_("REX prefix invalid with `%s'"), i.tm.name);
5128 return;
5129 }
5130
9e5e5283
L
5131 if (i.tm.opcode_modifier.vex)
5132 build_vex_prefix (t);
5133 else
5134 build_evex_prefix ();
0b9404fd
JB
5135
5136 /* The individual REX.RXBW bits got consumed. */
5137 i.rex &= REX_OPCODE;
9e5e5283 5138 }
43234a1e 5139
5dd85c99
SP
5140 /* Handle conversion of 'int $3' --> special int3 insn. XOP or FMA4
5141 instructions may define INT_OPCODE as well, so avoid this corner
5142 case for those instructions that use MODRM. */
389d00a5
JB
5143 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
5144 && i.tm.base_opcode == INT_OPCODE
a6461c02
SP
5145 && !i.tm.opcode_modifier.modrm
5146 && i.op[0].imms->X_add_number == 3)
29b0f896
AM
5147 {
5148 i.tm.base_opcode = INT3_OPCODE;
5149 i.imm_operands = 0;
5150 }
252b5132 5151
0cfa3eb3
JB
5152 if ((i.tm.opcode_modifier.jump == JUMP
5153 || i.tm.opcode_modifier.jump == JUMP_BYTE
5154 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896
AM
5155 && i.op[0].disps->X_op == O_constant)
5156 {
5157 /* Convert "jmp constant" (and "call constant") to a jump (call) to
5158 the absolute address given by the constant. Since ix86 jumps and
5159 calls are pc relative, we need to generate a reloc. */
5160 i.op[0].disps->X_add_symbol = &abs_symbol;
5161 i.op[0].disps->X_op = O_symbol;
5162 }
252b5132 5163
29b0f896
AM
5164 /* For 8 bit registers we need an empty rex prefix. Also if the
5165 instruction already has a prefix, we need to convert old
5166 registers to new ones. */
773f551c 5167
bab6aec1 5168 if ((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte
29b0f896 5169 && (i.op[0].regs->reg_flags & RegRex64) != 0)
bab6aec1 5170 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte
29b0f896 5171 && (i.op[1].regs->reg_flags & RegRex64) != 0)
bab6aec1
JB
5172 || (((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte)
5173 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte))
29b0f896
AM
5174 && i.rex != 0))
5175 {
5176 int x;
726c5dcd 5177
29b0f896
AM
5178 i.rex |= REX_OPCODE;
5179 for (x = 0; x < 2; x++)
5180 {
5181 /* Look for 8 bit operand that uses old registers. */
bab6aec1 5182 if (i.types[x].bitfield.class == Reg && i.types[x].bitfield.byte
29b0f896 5183 && (i.op[x].regs->reg_flags & RegRex64) == 0)
773f551c 5184 {
3f93af61 5185 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
29b0f896
AM
5186 /* In case it is "hi" register, give up. */
5187 if (i.op[x].regs->reg_num > 3)
a540244d 5188 as_bad (_("can't encode register '%s%s' in an "
4eed87de 5189 "instruction requiring REX prefix."),
a540244d 5190 register_prefix, i.op[x].regs->reg_name);
773f551c 5191
29b0f896
AM
5192 /* Otherwise it is equivalent to the extended register.
5193 Since the encoding doesn't change this is merely
5194 cosmetic cleanup for debug output. */
5195
5196 i.op[x].regs = i.op[x].regs + 8;
773f551c 5197 }
29b0f896
AM
5198 }
5199 }
773f551c 5200
6b6b6807
L
5201 if (i.rex == 0 && i.rex_encoding)
5202 {
5203 /* Check if we can add a REX_OPCODE byte. Look for 8 bit operand
3f93af61 5204 that uses legacy register. If it is "hi" register, don't add
6b6b6807
L
5205 the REX_OPCODE byte. */
5206 int x;
5207 for (x = 0; x < 2; x++)
bab6aec1 5208 if (i.types[x].bitfield.class == Reg
6b6b6807
L
5209 && i.types[x].bitfield.byte
5210 && (i.op[x].regs->reg_flags & RegRex64) == 0
5211 && i.op[x].regs->reg_num > 3)
5212 {
3f93af61 5213 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
5b7c81bd 5214 i.rex_encoding = false;
6b6b6807
L
5215 break;
5216 }
5217
5218 if (i.rex_encoding)
5219 i.rex = REX_OPCODE;
5220 }
5221
7ab9ffdd 5222 if (i.rex != 0)
29b0f896
AM
5223 add_prefix (REX_OPCODE | i.rex);
5224
ae531041
L
5225 insert_lfence_before ();
5226
29b0f896
AM
5227 /* We are ready to output the insn. */
5228 output_insn ();
e379e5f3 5229
ae531041
L
5230 insert_lfence_after ();
5231
e379e5f3
L
5232 last_insn.seg = now_seg;
5233
5234 if (i.tm.opcode_modifier.isprefix)
5235 {
5236 last_insn.kind = last_insn_prefix;
5237 last_insn.name = i.tm.name;
5238 last_insn.file = as_where (&last_insn.line);
5239 }
5240 else
5241 last_insn.kind = last_insn_other;
29b0f896
AM
5242}
5243
5244static char *
e3bb37b5 5245parse_insn (char *line, char *mnemonic)
29b0f896
AM
5246{
5247 char *l = line;
5248 char *token_start = l;
5249 char *mnem_p;
5c6af06e 5250 int supported;
d3ce72d0 5251 const insn_template *t;
b6169b20 5252 char *dot_p = NULL;
29b0f896 5253
29b0f896
AM
5254 while (1)
5255 {
5256 mnem_p = mnemonic;
5257 while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
5258 {
b6169b20
L
5259 if (*mnem_p == '.')
5260 dot_p = mnem_p;
29b0f896
AM
5261 mnem_p++;
5262 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
45288df1 5263 {
29b0f896
AM
5264 as_bad (_("no such instruction: `%s'"), token_start);
5265 return NULL;
5266 }
5267 l++;
5268 }
5269 if (!is_space_char (*l)
5270 && *l != END_OF_INSN
e44823cf
JB
5271 && (intel_syntax
5272 || (*l != PREFIX_SEPARATOR
5273 && *l != ',')))
29b0f896
AM
5274 {
5275 as_bad (_("invalid character %s in mnemonic"),
5276 output_invalid (*l));
5277 return NULL;
5278 }
5279 if (token_start == l)
5280 {
e44823cf 5281 if (!intel_syntax && *l == PREFIX_SEPARATOR)
29b0f896
AM
5282 as_bad (_("expecting prefix; got nothing"));
5283 else
5284 as_bad (_("expecting mnemonic; got nothing"));
5285 return NULL;
5286 }
45288df1 5287
29b0f896 5288 /* Look up instruction (or prefix) via hash table. */
629310ab 5289 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
47926f60 5290
29b0f896
AM
5291 if (*l != END_OF_INSN
5292 && (!is_space_char (*l) || l[1] != END_OF_INSN)
5293 && current_templates
40fb9820 5294 && current_templates->start->opcode_modifier.isprefix)
29b0f896 5295 {
c6fb90c8 5296 if (!cpu_flags_check_cpu64 (current_templates->start->cpu_flags))
2dd88dca
JB
5297 {
5298 as_bad ((flag_code != CODE_64BIT
5299 ? _("`%s' is only supported in 64-bit mode")
5300 : _("`%s' is not supported in 64-bit mode")),
5301 current_templates->start->name);
5302 return NULL;
5303 }
29b0f896
AM
5304 /* If we are in 16-bit mode, do not allow addr16 or data16.
5305 Similarly, in 32-bit mode, do not allow addr32 or data32. */
673fe0f0
JB
5306 if ((current_templates->start->opcode_modifier.size == SIZE16
5307 || current_templates->start->opcode_modifier.size == SIZE32)
29b0f896 5308 && flag_code != CODE_64BIT
673fe0f0 5309 && ((current_templates->start->opcode_modifier.size == SIZE32)
29b0f896
AM
5310 ^ (flag_code == CODE_16BIT)))
5311 {
5312 as_bad (_("redundant %s prefix"),
5313 current_templates->start->name);
5314 return NULL;
45288df1 5315 }
31184569
JB
5316
5317 if (current_templates->start->base_opcode == PSEUDO_PREFIX)
29b0f896 5318 {
86fa6981 5319 /* Handle pseudo prefixes. */
31184569 5320 switch (current_templates->start->extension_opcode)
86fa6981 5321 {
41eb8e88 5322 case Prefix_Disp8:
86fa6981
L
5323 /* {disp8} */
5324 i.disp_encoding = disp_encoding_8bit;
5325 break;
41eb8e88
L
5326 case Prefix_Disp16:
5327 /* {disp16} */
5328 i.disp_encoding = disp_encoding_16bit;
5329 break;
5330 case Prefix_Disp32:
86fa6981
L
5331 /* {disp32} */
5332 i.disp_encoding = disp_encoding_32bit;
5333 break;
41eb8e88 5334 case Prefix_Load:
86fa6981
L
5335 /* {load} */
5336 i.dir_encoding = dir_encoding_load;
5337 break;
41eb8e88 5338 case Prefix_Store:
86fa6981
L
5339 /* {store} */
5340 i.dir_encoding = dir_encoding_store;
5341 break;
41eb8e88 5342 case Prefix_VEX:
42e04b36
L
5343 /* {vex} */
5344 i.vec_encoding = vex_encoding_vex;
86fa6981 5345 break;
41eb8e88 5346 case Prefix_VEX3:
86fa6981
L
5347 /* {vex3} */
5348 i.vec_encoding = vex_encoding_vex3;
5349 break;
41eb8e88 5350 case Prefix_EVEX:
86fa6981
L
5351 /* {evex} */
5352 i.vec_encoding = vex_encoding_evex;
5353 break;
41eb8e88 5354 case Prefix_REX:
6b6b6807 5355 /* {rex} */
5b7c81bd 5356 i.rex_encoding = true;
6b6b6807 5357 break;
41eb8e88 5358 case Prefix_NoOptimize:
b6f8c7c4 5359 /* {nooptimize} */
5b7c81bd 5360 i.no_optimize = true;
b6f8c7c4 5361 break;
86fa6981
L
5362 default:
5363 abort ();
5364 }
5365 }
5366 else
5367 {
5368 /* Add prefix, checking for repeated prefixes. */
4e9ac44a 5369 switch (add_prefix (current_templates->start->base_opcode))
86fa6981 5370 {
4e9ac44a
L
5371 case PREFIX_EXIST:
5372 return NULL;
5373 case PREFIX_DS:
d777820b 5374 if (current_templates->start->cpu_flags.bitfield.cpuibt)
4e9ac44a
L
5375 i.notrack_prefix = current_templates->start->name;
5376 break;
5377 case PREFIX_REP:
5378 if (current_templates->start->cpu_flags.bitfield.cpuhle)
5379 i.hle_prefix = current_templates->start->name;
5380 else if (current_templates->start->cpu_flags.bitfield.cpumpx)
5381 i.bnd_prefix = current_templates->start->name;
5382 else
5383 i.rep_prefix = current_templates->start->name;
5384 break;
5385 default:
5386 break;
86fa6981 5387 }
29b0f896
AM
5388 }
5389 /* Skip past PREFIX_SEPARATOR and reset token_start. */
5390 token_start = ++l;
5391 }
5392 else
5393 break;
5394 }
45288df1 5395
30a55f88 5396 if (!current_templates)
b6169b20 5397 {
07d5e953
JB
5398 /* Deprecated functionality (new code should use pseudo-prefixes instead):
5399 Check if we should swap operand or force 32bit displacement in
f8a5c266 5400 encoding. */
30a55f88 5401 if (mnem_p - 2 == dot_p && dot_p[1] == 's')
64c49ab3 5402 i.dir_encoding = dir_encoding_swap;
8d63c93e 5403 else if (mnem_p - 3 == dot_p
a501d77e
L
5404 && dot_p[1] == 'd'
5405 && dot_p[2] == '8')
5406 i.disp_encoding = disp_encoding_8bit;
8d63c93e 5407 else if (mnem_p - 4 == dot_p
f8a5c266
L
5408 && dot_p[1] == 'd'
5409 && dot_p[2] == '3'
5410 && dot_p[3] == '2')
a501d77e 5411 i.disp_encoding = disp_encoding_32bit;
30a55f88
L
5412 else
5413 goto check_suffix;
5414 mnem_p = dot_p;
5415 *dot_p = '\0';
629310ab 5416 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
b6169b20
L
5417 }
5418
29b0f896
AM
5419 if (!current_templates)
5420 {
dc1e8a47 5421 check_suffix:
1c529385 5422 if (mnem_p > mnemonic)
29b0f896 5423 {
1c529385
LH
5424 /* See if we can get a match by trimming off a suffix. */
5425 switch (mnem_p[-1])
29b0f896 5426 {
1c529385
LH
5427 case WORD_MNEM_SUFFIX:
5428 if (intel_syntax && (intel_float_operand (mnemonic) & 2))
29b0f896
AM
5429 i.suffix = SHORT_MNEM_SUFFIX;
5430 else
1c529385
LH
5431 /* Fall through. */
5432 case BYTE_MNEM_SUFFIX:
5433 case QWORD_MNEM_SUFFIX:
5434 i.suffix = mnem_p[-1];
29b0f896 5435 mnem_p[-1] = '\0';
fe0e921f
AM
5436 current_templates
5437 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5438 break;
5439 case SHORT_MNEM_SUFFIX:
5440 case LONG_MNEM_SUFFIX:
5441 if (!intel_syntax)
5442 {
5443 i.suffix = mnem_p[-1];
5444 mnem_p[-1] = '\0';
fe0e921f
AM
5445 current_templates
5446 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5447 }
5448 break;
5449
5450 /* Intel Syntax. */
5451 case 'd':
5452 if (intel_syntax)
5453 {
5454 if (intel_float_operand (mnemonic) == 1)
5455 i.suffix = SHORT_MNEM_SUFFIX;
5456 else
5457 i.suffix = LONG_MNEM_SUFFIX;
5458 mnem_p[-1] = '\0';
fe0e921f
AM
5459 current_templates
5460 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5461 }
5462 break;
29b0f896 5463 }
29b0f896 5464 }
1c529385 5465
29b0f896
AM
5466 if (!current_templates)
5467 {
5468 as_bad (_("no such instruction: `%s'"), token_start);
5469 return NULL;
5470 }
5471 }
252b5132 5472
0cfa3eb3
JB
5473 if (current_templates->start->opcode_modifier.jump == JUMP
5474 || current_templates->start->opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
5475 {
5476 /* Check for a branch hint. We allow ",pt" and ",pn" for
5477 predict taken and predict not taken respectively.
5478 I'm not sure that branch hints actually do anything on loop
5479 and jcxz insns (JumpByte) for current Pentium4 chips. They
5480 may work in the future and it doesn't hurt to accept them
5481 now. */
5482 if (l[0] == ',' && l[1] == 'p')
5483 {
5484 if (l[2] == 't')
5485 {
5486 if (!add_prefix (DS_PREFIX_OPCODE))
5487 return NULL;
5488 l += 3;
5489 }
5490 else if (l[2] == 'n')
5491 {
5492 if (!add_prefix (CS_PREFIX_OPCODE))
5493 return NULL;
5494 l += 3;
5495 }
5496 }
5497 }
5498 /* Any other comma loses. */
5499 if (*l == ',')
5500 {
5501 as_bad (_("invalid character %s in mnemonic"),
5502 output_invalid (*l));
5503 return NULL;
5504 }
252b5132 5505
29b0f896 5506 /* Check if instruction is supported on specified architecture. */
5c6af06e
JB
5507 supported = 0;
5508 for (t = current_templates->start; t < current_templates->end; ++t)
5509 {
c0f3af97
L
5510 supported |= cpu_flags_match (t);
5511 if (supported == CPU_FLAGS_PERFECT_MATCH)
d59a54c2 5512 return l;
29b0f896 5513 }
3629bb00 5514
548d0ee6
JB
5515 if (!(supported & CPU_FLAGS_64BIT_MATCH))
5516 as_bad (flag_code == CODE_64BIT
5517 ? _("`%s' is not supported in 64-bit mode")
5518 : _("`%s' is only supported in 64-bit mode"),
5519 current_templates->start->name);
5520 else
5521 as_bad (_("`%s' is not supported on `%s%s'"),
5522 current_templates->start->name,
5523 cpu_arch_name ? cpu_arch_name : default_arch,
5524 cpu_sub_arch_name ? cpu_sub_arch_name : "");
252b5132 5525
548d0ee6 5526 return NULL;
29b0f896 5527}
252b5132 5528
29b0f896 5529static char *
e3bb37b5 5530parse_operands (char *l, const char *mnemonic)
29b0f896
AM
5531{
5532 char *token_start;
3138f287 5533
29b0f896
AM
5534 /* 1 if operand is pending after ','. */
5535 unsigned int expecting_operand = 0;
252b5132 5536
29b0f896
AM
5537 while (*l != END_OF_INSN)
5538 {
e68c3d59
JB
5539 /* Non-zero if operand parens not balanced. */
5540 unsigned int paren_not_balanced = 0;
5541 /* True if inside double quotes. */
5542 bool in_quotes = false;
5543
29b0f896
AM
5544 /* Skip optional white space before operand. */
5545 if (is_space_char (*l))
5546 ++l;
d02603dc 5547 if (!is_operand_char (*l) && *l != END_OF_INSN && *l != '"')
29b0f896
AM
5548 {
5549 as_bad (_("invalid character %s before operand %d"),
5550 output_invalid (*l),
5551 i.operands + 1);
5552 return NULL;
5553 }
d02603dc 5554 token_start = l; /* After white space. */
e68c3d59 5555 while (in_quotes || paren_not_balanced || *l != ',')
29b0f896
AM
5556 {
5557 if (*l == END_OF_INSN)
5558 {
e68c3d59
JB
5559 if (in_quotes)
5560 {
5561 as_bad (_("unbalanced double quotes in operand %d."),
5562 i.operands + 1);
5563 return NULL;
5564 }
29b0f896
AM
5565 if (paren_not_balanced)
5566 {
98ff9f1c
JB
5567 know (!intel_syntax);
5568 as_bad (_("unbalanced parenthesis in operand %d."),
5569 i.operands + 1);
29b0f896
AM
5570 return NULL;
5571 }
5572 else
5573 break; /* we are done */
5574 }
e68c3d59
JB
5575 else if (*l == '\\' && l[1] == '"')
5576 ++l;
5577 else if (*l == '"')
5578 in_quotes = !in_quotes;
5579 else if (!in_quotes && !is_operand_char (*l) && !is_space_char (*l))
29b0f896
AM
5580 {
5581 as_bad (_("invalid character %s in operand %d"),
5582 output_invalid (*l),
5583 i.operands + 1);
5584 return NULL;
5585 }
e68c3d59 5586 if (!intel_syntax && !in_quotes)
29b0f896
AM
5587 {
5588 if (*l == '(')
5589 ++paren_not_balanced;
5590 if (*l == ')')
5591 --paren_not_balanced;
5592 }
29b0f896
AM
5593 l++;
5594 }
5595 if (l != token_start)
5596 { /* Yes, we've read in another operand. */
5597 unsigned int operand_ok;
5598 this_operand = i.operands++;
5599 if (i.operands > MAX_OPERANDS)
5600 {
5601 as_bad (_("spurious operands; (%d operands/instruction max)"),
5602 MAX_OPERANDS);
5603 return NULL;
5604 }
9d46ce34 5605 i.types[this_operand].bitfield.unspecified = 1;
29b0f896
AM
5606 /* Now parse operand adding info to 'i' as we go along. */
5607 END_STRING_AND_SAVE (l);
5608
1286ab78
L
5609 if (i.mem_operands > 1)
5610 {
5611 as_bad (_("too many memory references for `%s'"),
5612 mnemonic);
5613 return 0;
5614 }
5615
29b0f896
AM
5616 if (intel_syntax)
5617 operand_ok =
5618 i386_intel_operand (token_start,
5619 intel_float_operand (mnemonic));
5620 else
a7619375 5621 operand_ok = i386_att_operand (token_start);
29b0f896
AM
5622
5623 RESTORE_END_STRING (l);
5624 if (!operand_ok)
5625 return NULL;
5626 }
5627 else
5628 {
5629 if (expecting_operand)
5630 {
5631 expecting_operand_after_comma:
5632 as_bad (_("expecting operand after ','; got nothing"));
5633 return NULL;
5634 }
5635 if (*l == ',')
5636 {
5637 as_bad (_("expecting operand before ','; got nothing"));
5638 return NULL;
5639 }
5640 }
7f3f1ea2 5641
29b0f896
AM
5642 /* Now *l must be either ',' or END_OF_INSN. */
5643 if (*l == ',')
5644 {
5645 if (*++l == END_OF_INSN)
5646 {
5647 /* Just skip it, if it's \n complain. */
5648 goto expecting_operand_after_comma;
5649 }
5650 expecting_operand = 1;
5651 }
5652 }
5653 return l;
5654}
7f3f1ea2 5655
050dfa73 5656static void
783c187b 5657swap_2_operands (unsigned int xchg1, unsigned int xchg2)
050dfa73
MM
5658{
5659 union i386_op temp_op;
40fb9820 5660 i386_operand_type temp_type;
c48dadc9 5661 unsigned int temp_flags;
050dfa73 5662 enum bfd_reloc_code_real temp_reloc;
4eed87de 5663
050dfa73
MM
5664 temp_type = i.types[xchg2];
5665 i.types[xchg2] = i.types[xchg1];
5666 i.types[xchg1] = temp_type;
c48dadc9
JB
5667
5668 temp_flags = i.flags[xchg2];
5669 i.flags[xchg2] = i.flags[xchg1];
5670 i.flags[xchg1] = temp_flags;
5671
050dfa73
MM
5672 temp_op = i.op[xchg2];
5673 i.op[xchg2] = i.op[xchg1];
5674 i.op[xchg1] = temp_op;
c48dadc9 5675
050dfa73
MM
5676 temp_reloc = i.reloc[xchg2];
5677 i.reloc[xchg2] = i.reloc[xchg1];
5678 i.reloc[xchg1] = temp_reloc;
43234a1e 5679
6225c532 5680 if (i.mask.reg)
43234a1e 5681 {
6225c532
JB
5682 if (i.mask.operand == xchg1)
5683 i.mask.operand = xchg2;
5684 else if (i.mask.operand == xchg2)
5685 i.mask.operand = xchg1;
43234a1e 5686 }
a5748e0d 5687 if (i.broadcast.type || i.broadcast.bytes)
43234a1e 5688 {
5273a3cd
JB
5689 if (i.broadcast.operand == xchg1)
5690 i.broadcast.operand = xchg2;
5691 else if (i.broadcast.operand == xchg2)
5692 i.broadcast.operand = xchg1;
43234a1e 5693 }
050dfa73
MM
5694}
5695
29b0f896 5696static void
e3bb37b5 5697swap_operands (void)
29b0f896 5698{
b7c61d9a 5699 switch (i.operands)
050dfa73 5700 {
c0f3af97 5701 case 5:
b7c61d9a 5702 case 4:
4d456e3d 5703 swap_2_operands (1, i.operands - 2);
1a0670f3 5704 /* Fall through. */
b7c61d9a
L
5705 case 3:
5706 case 2:
4d456e3d 5707 swap_2_operands (0, i.operands - 1);
b7c61d9a
L
5708 break;
5709 default:
5710 abort ();
29b0f896 5711 }
29b0f896
AM
5712
5713 if (i.mem_operands == 2)
5714 {
5e042380 5715 const reg_entry *temp_seg;
29b0f896
AM
5716 temp_seg = i.seg[0];
5717 i.seg[0] = i.seg[1];
5718 i.seg[1] = temp_seg;
5719 }
5720}
252b5132 5721
29b0f896
AM
5722/* Try to ensure constant immediates are represented in the smallest
5723 opcode possible. */
5724static void
e3bb37b5 5725optimize_imm (void)
29b0f896
AM
5726{
5727 char guess_suffix = 0;
5728 int op;
252b5132 5729
29b0f896
AM
5730 if (i.suffix)
5731 guess_suffix = i.suffix;
5732 else if (i.reg_operands)
5733 {
5734 /* Figure out a suffix from the last register operand specified.
75e5731b
JB
5735 We can't do this properly yet, i.e. excluding special register
5736 instances, but the following works for instructions with
5737 immediates. In any case, we can't set i.suffix yet. */
29b0f896 5738 for (op = i.operands; --op >= 0;)
bab6aec1
JB
5739 if (i.types[op].bitfield.class != Reg)
5740 continue;
5741 else if (i.types[op].bitfield.byte)
7ab9ffdd 5742 {
40fb9820
L
5743 guess_suffix = BYTE_MNEM_SUFFIX;
5744 break;
5745 }
bab6aec1 5746 else if (i.types[op].bitfield.word)
252b5132 5747 {
40fb9820
L
5748 guess_suffix = WORD_MNEM_SUFFIX;
5749 break;
5750 }
bab6aec1 5751 else if (i.types[op].bitfield.dword)
40fb9820
L
5752 {
5753 guess_suffix = LONG_MNEM_SUFFIX;
5754 break;
5755 }
bab6aec1 5756 else if (i.types[op].bitfield.qword)
40fb9820
L
5757 {
5758 guess_suffix = QWORD_MNEM_SUFFIX;
29b0f896 5759 break;
252b5132 5760 }
29b0f896
AM
5761 }
5762 else if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
5763 guess_suffix = WORD_MNEM_SUFFIX;
5764
5765 for (op = i.operands; --op >= 0;)
40fb9820 5766 if (operand_type_check (i.types[op], imm))
29b0f896
AM
5767 {
5768 switch (i.op[op].imms->X_op)
252b5132 5769 {
29b0f896
AM
5770 case O_constant:
5771 /* If a suffix is given, this operand may be shortened. */
5772 switch (guess_suffix)
252b5132 5773 {
29b0f896 5774 case LONG_MNEM_SUFFIX:
40fb9820
L
5775 i.types[op].bitfield.imm32 = 1;
5776 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
5777 break;
5778 case WORD_MNEM_SUFFIX:
40fb9820
L
5779 i.types[op].bitfield.imm16 = 1;
5780 i.types[op].bitfield.imm32 = 1;
5781 i.types[op].bitfield.imm32s = 1;
5782 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
5783 break;
5784 case BYTE_MNEM_SUFFIX:
40fb9820
L
5785 i.types[op].bitfield.imm8 = 1;
5786 i.types[op].bitfield.imm8s = 1;
5787 i.types[op].bitfield.imm16 = 1;
5788 i.types[op].bitfield.imm32 = 1;
5789 i.types[op].bitfield.imm32s = 1;
5790 i.types[op].bitfield.imm64 = 1;
29b0f896 5791 break;
252b5132 5792 }
252b5132 5793
29b0f896
AM
5794 /* If this operand is at most 16 bits, convert it
5795 to a signed 16 bit number before trying to see
5796 whether it will fit in an even smaller size.
5797 This allows a 16-bit operand such as $0xffe0 to
5798 be recognised as within Imm8S range. */
40fb9820 5799 if ((i.types[op].bitfield.imm16)
7e96fb68 5800 && fits_in_unsigned_word (i.op[op].imms->X_add_number))
252b5132 5801 {
87ed972d
JB
5802 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
5803 ^ 0x8000) - 0x8000);
29b0f896 5804 }
a28def75
L
5805#ifdef BFD64
5806 /* Store 32-bit immediate in 64-bit for 64-bit BFD. */
40fb9820 5807 if ((i.types[op].bitfield.imm32)
7e96fb68 5808 && fits_in_unsigned_long (i.op[op].imms->X_add_number))
29b0f896
AM
5809 {
5810 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
5811 ^ ((offsetT) 1 << 31))
5812 - ((offsetT) 1 << 31));
5813 }
a28def75 5814#endif
40fb9820 5815 i.types[op]
c6fb90c8
L
5816 = operand_type_or (i.types[op],
5817 smallest_imm_type (i.op[op].imms->X_add_number));
252b5132 5818
29b0f896
AM
5819 /* We must avoid matching of Imm32 templates when 64bit
5820 only immediate is available. */
5821 if (guess_suffix == QWORD_MNEM_SUFFIX)
40fb9820 5822 i.types[op].bitfield.imm32 = 0;
29b0f896 5823 break;
252b5132 5824
29b0f896
AM
5825 case O_absent:
5826 case O_register:
5827 abort ();
5828
5829 /* Symbols and expressions. */
5830 default:
9cd96992
JB
5831 /* Convert symbolic operand to proper sizes for matching, but don't
5832 prevent matching a set of insns that only supports sizes other
5833 than those matching the insn suffix. */
5834 {
40fb9820 5835 i386_operand_type mask, allowed;
87ed972d 5836 const insn_template *t = current_templates->start;
9cd96992 5837
0dfbf9d7 5838 operand_type_set (&mask, 0);
9cd96992
JB
5839 switch (guess_suffix)
5840 {
5841 case QWORD_MNEM_SUFFIX:
40fb9820
L
5842 mask.bitfield.imm64 = 1;
5843 mask.bitfield.imm32s = 1;
9cd96992
JB
5844 break;
5845 case LONG_MNEM_SUFFIX:
40fb9820 5846 mask.bitfield.imm32 = 1;
9cd96992
JB
5847 break;
5848 case WORD_MNEM_SUFFIX:
40fb9820 5849 mask.bitfield.imm16 = 1;
9cd96992
JB
5850 break;
5851 case BYTE_MNEM_SUFFIX:
40fb9820 5852 mask.bitfield.imm8 = 1;
9cd96992
JB
5853 break;
5854 default:
9cd96992
JB
5855 break;
5856 }
8f0212ac
JB
5857
5858 allowed = operand_type_and (t->operand_types[op], mask);
5859 while (++t < current_templates->end)
5860 {
5861 allowed = operand_type_or (allowed, t->operand_types[op]);
5862 allowed = operand_type_and (allowed, mask);
5863 }
5864
0dfbf9d7 5865 if (!operand_type_all_zero (&allowed))
c6fb90c8 5866 i.types[op] = operand_type_and (i.types[op], mask);
9cd96992 5867 }
29b0f896 5868 break;
252b5132 5869 }
29b0f896
AM
5870 }
5871}
47926f60 5872
29b0f896
AM
5873/* Try to use the smallest displacement type too. */
5874static void
e3bb37b5 5875optimize_disp (void)
29b0f896
AM
5876{
5877 int op;
3e73aa7c 5878
29b0f896 5879 for (op = i.operands; --op >= 0;)
40fb9820 5880 if (operand_type_check (i.types[op], disp))
252b5132 5881 {
b300c311 5882 if (i.op[op].disps->X_op == O_constant)
252b5132 5883 {
91d6fa6a 5884 offsetT op_disp = i.op[op].disps->X_add_number;
29b0f896 5885
91d6fa6a 5886 if (!op_disp && i.types[op].bitfield.baseindex)
b300c311 5887 {
2f2be86b
JB
5888 i.types[op] = operand_type_and_not (i.types[op], anydisp);
5889 i.op[op].disps = NULL;
b300c311 5890 i.disp_operands--;
f185acdd
JB
5891 continue;
5892 }
5893
5894 if (i.types[op].bitfield.disp16
cd613c1f 5895 && fits_in_unsigned_word (op_disp))
f185acdd
JB
5896 {
5897 /* If this operand is at most 16 bits, convert
5898 to a signed 16 bit number and don't use 64bit
5899 displacement. */
5900 op_disp = ((op_disp ^ 0x8000) - 0x8000);
5901 i.types[op].bitfield.disp64 = 0;
b300c311 5902 }
f185acdd 5903
28a167a4 5904#ifdef BFD64
a50187b2 5905 /* Optimize 64-bit displacement to 32-bit for 64-bit BFD. */
a775efc8
JB
5906 if ((flag_code != CODE_64BIT
5907 ? i.types[op].bitfield.disp32
5908 : want_disp32 (current_templates->start)
5909 && (!current_templates->start->opcode_modifier.jump
5910 || i.jumpabsolute || i.types[op].bitfield.baseindex))
a50187b2 5911 && fits_in_unsigned_long (op_disp))
b300c311 5912 {
a50187b2
JB
5913 /* If this operand is at most 32 bits, convert
5914 to a signed 32 bit number and don't use 64bit
5915 displacement. */
5916 op_disp = (op_disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
5917 i.types[op].bitfield.disp64 = 0;
5918 i.types[op].bitfield.disp32 = 1;
5919 }
28a167a4 5920
a50187b2
JB
5921 if (flag_code == CODE_64BIT && fits_in_signed_long (op_disp))
5922 {
5923 i.types[op].bitfield.disp64 = 0;
a775efc8 5924 i.types[op].bitfield.disp32 = 1;
b300c311 5925 }
28a167a4 5926#endif
40fb9820 5927 if ((i.types[op].bitfield.disp32
40fb9820 5928 || i.types[op].bitfield.disp16)
b5014f7a 5929 && fits_in_disp8 (op_disp))
40fb9820 5930 i.types[op].bitfield.disp8 = 1;
77c59789
JB
5931
5932 i.op[op].disps->X_add_number = op_disp;
252b5132 5933 }
67a4f2b7
AO
5934 else if (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
5935 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL)
5936 {
5937 fix_new_exp (frag_now, frag_more (0) - frag_now->fr_literal, 0,
5938 i.op[op].disps, 0, i.reloc[op]);
2f2be86b 5939 i.types[op] = operand_type_and_not (i.types[op], anydisp);
67a4f2b7
AO
5940 }
5941 else
b300c311 5942 /* We only support 64bit displacement on constants. */
40fb9820 5943 i.types[op].bitfield.disp64 = 0;
252b5132 5944 }
29b0f896
AM
5945}
5946
4a1b91ea
L
5947/* Return 1 if there is a match in broadcast bytes between operand
5948 GIVEN and instruction template T. */
5949
5950static INLINE int
5951match_broadcast_size (const insn_template *t, unsigned int given)
5952{
5953 return ((t->opcode_modifier.broadcast == BYTE_BROADCAST
5954 && i.types[given].bitfield.byte)
5955 || (t->opcode_modifier.broadcast == WORD_BROADCAST
5956 && i.types[given].bitfield.word)
5957 || (t->opcode_modifier.broadcast == DWORD_BROADCAST
5958 && i.types[given].bitfield.dword)
5959 || (t->opcode_modifier.broadcast == QWORD_BROADCAST
5960 && i.types[given].bitfield.qword));
5961}
5962
6c30d220
L
5963/* Check if operands are valid for the instruction. */
5964
5965static int
5966check_VecOperands (const insn_template *t)
5967{
43234a1e 5968 unsigned int op;
e2195274 5969 i386_cpu_flags cpu;
e2195274
JB
5970
5971 /* Templates allowing for ZMMword as well as YMMword and/or XMMword for
5972 any one operand are implicity requiring AVX512VL support if the actual
5973 operand size is YMMword or XMMword. Since this function runs after
5974 template matching, there's no need to check for YMMword/XMMword in
5975 the template. */
5976 cpu = cpu_flags_and (t->cpu_flags, avx512);
5977 if (!cpu_flags_all_zero (&cpu)
5978 && !t->cpu_flags.bitfield.cpuavx512vl
5979 && !cpu_arch_flags.bitfield.cpuavx512vl)
5980 {
5981 for (op = 0; op < t->operands; ++op)
5982 {
5983 if (t->operand_types[op].bitfield.zmmword
5984 && (i.types[op].bitfield.ymmword
5985 || i.types[op].bitfield.xmmword))
5986 {
5987 i.error = unsupported;
5988 return 1;
5989 }
5990 }
5991 }
43234a1e 5992
22c36940
JB
5993 /* Somewhat similarly, templates specifying both AVX and AVX2 are
5994 requiring AVX2 support if the actual operand size is YMMword. */
5995 if (t->cpu_flags.bitfield.cpuavx
5996 && t->cpu_flags.bitfield.cpuavx2
5997 && !cpu_arch_flags.bitfield.cpuavx2)
5998 {
5999 for (op = 0; op < t->operands; ++op)
6000 {
6001 if (t->operand_types[op].bitfield.xmmword
6002 && i.types[op].bitfield.ymmword)
6003 {
6004 i.error = unsupported;
6005 return 1;
6006 }
6007 }
6008 }
6009
6c30d220 6010 /* Without VSIB byte, we can't have a vector register for index. */
63112cd6 6011 if (!t->opcode_modifier.sib
6c30d220 6012 && i.index_reg
1b54b8d7
JB
6013 && (i.index_reg->reg_type.bitfield.xmmword
6014 || i.index_reg->reg_type.bitfield.ymmword
6015 || i.index_reg->reg_type.bitfield.zmmword))
6c30d220
L
6016 {
6017 i.error = unsupported_vector_index_register;
6018 return 1;
6019 }
6020
ad8ecc81
MZ
6021 /* Check if default mask is allowed. */
6022 if (t->opcode_modifier.nodefmask
6225c532 6023 && (!i.mask.reg || i.mask.reg->reg_num == 0))
ad8ecc81
MZ
6024 {
6025 i.error = no_default_mask;
6026 return 1;
6027 }
6028
7bab8ab5
JB
6029 /* For VSIB byte, we need a vector register for index, and all vector
6030 registers must be distinct. */
260cd341 6031 if (t->opcode_modifier.sib && t->opcode_modifier.sib != SIBMEM)
7bab8ab5
JB
6032 {
6033 if (!i.index_reg
63112cd6 6034 || !((t->opcode_modifier.sib == VECSIB128
1b54b8d7 6035 && i.index_reg->reg_type.bitfield.xmmword)
63112cd6 6036 || (t->opcode_modifier.sib == VECSIB256
1b54b8d7 6037 && i.index_reg->reg_type.bitfield.ymmword)
63112cd6 6038 || (t->opcode_modifier.sib == VECSIB512
1b54b8d7 6039 && i.index_reg->reg_type.bitfield.zmmword)))
7bab8ab5
JB
6040 {
6041 i.error = invalid_vsib_address;
6042 return 1;
6043 }
6044
6225c532
JB
6045 gas_assert (i.reg_operands == 2 || i.mask.reg);
6046 if (i.reg_operands == 2 && !i.mask.reg)
43234a1e 6047 {
3528c362 6048 gas_assert (i.types[0].bitfield.class == RegSIMD);
1b54b8d7
JB
6049 gas_assert (i.types[0].bitfield.xmmword
6050 || i.types[0].bitfield.ymmword);
3528c362 6051 gas_assert (i.types[2].bitfield.class == RegSIMD);
1b54b8d7
JB
6052 gas_assert (i.types[2].bitfield.xmmword
6053 || i.types[2].bitfield.ymmword);
43234a1e
L
6054 if (operand_check == check_none)
6055 return 0;
6056 if (register_number (i.op[0].regs)
6057 != register_number (i.index_reg)
6058 && register_number (i.op[2].regs)
6059 != register_number (i.index_reg)
6060 && register_number (i.op[0].regs)
6061 != register_number (i.op[2].regs))
6062 return 0;
6063 if (operand_check == check_error)
6064 {
6065 i.error = invalid_vector_register_set;
6066 return 1;
6067 }
6068 as_warn (_("mask, index, and destination registers should be distinct"));
6069 }
6225c532 6070 else if (i.reg_operands == 1 && i.mask.reg)
8444f82a 6071 {
3528c362 6072 if (i.types[1].bitfield.class == RegSIMD
1b54b8d7
JB
6073 && (i.types[1].bitfield.xmmword
6074 || i.types[1].bitfield.ymmword
6075 || i.types[1].bitfield.zmmword)
8444f82a
MZ
6076 && (register_number (i.op[1].regs)
6077 == register_number (i.index_reg)))
6078 {
6079 if (operand_check == check_error)
6080 {
6081 i.error = invalid_vector_register_set;
6082 return 1;
6083 }
6084 if (operand_check != check_none)
6085 as_warn (_("index and destination registers should be distinct"));
6086 }
6087 }
43234a1e 6088 }
7bab8ab5 6089
fc141319
L
6090 /* For AMX instructions with 3 TMM register operands, all operands
6091 must be distinct. */
6092 if (i.reg_operands == 3
6093 && t->operand_types[0].bitfield.tmmword
6094 && (i.op[0].regs == i.op[1].regs
6095 || i.op[0].regs == i.op[2].regs
6096 || i.op[1].regs == i.op[2].regs))
6097 {
6098 i.error = invalid_tmm_register_set;
6099 return 1;
260cd341
LC
6100 }
6101
0cc78721
CL
6102 /* For some special instructions require that destination must be distinct
6103 from source registers. */
6104 if (t->opcode_modifier.distinctdest)
6105 {
6106 unsigned int dest_reg = i.operands - 1;
6107
6108 know (i.operands >= 3);
6109
6110 /* #UD if dest_reg == src1_reg or dest_reg == src2_reg. */
6111 if (i.op[dest_reg - 1].regs == i.op[dest_reg].regs
6112 || (i.reg_operands > 2
6113 && i.op[dest_reg - 2].regs == i.op[dest_reg].regs))
6114 {
6115 i.error = invalid_dest_and_src_register_set;
6116 return 1;
6117 }
6118 }
6119
43234a1e
L
6120 /* Check if broadcast is supported by the instruction and is applied
6121 to the memory operand. */
a5748e0d 6122 if (i.broadcast.type || i.broadcast.bytes)
43234a1e 6123 {
8e6e0792 6124 i386_operand_type type, overlap;
43234a1e
L
6125
6126 /* Check if specified broadcast is supported in this instruction,
4a1b91ea 6127 and its broadcast bytes match the memory operand. */
5273a3cd 6128 op = i.broadcast.operand;
8e6e0792 6129 if (!t->opcode_modifier.broadcast
c48dadc9 6130 || !(i.flags[op] & Operand_Mem)
c39e5b26 6131 || (!i.types[op].bitfield.unspecified
4a1b91ea 6132 && !match_broadcast_size (t, op)))
43234a1e
L
6133 {
6134 bad_broadcast:
6135 i.error = unsupported_broadcast;
6136 return 1;
6137 }
8e6e0792 6138
a5748e0d
JB
6139 if (i.broadcast.type)
6140 i.broadcast.bytes = ((1 << (t->opcode_modifier.broadcast - 1))
6141 * i.broadcast.type);
8e6e0792 6142 operand_type_set (&type, 0);
a5748e0d 6143 switch (get_broadcast_bytes (t, false))
8e6e0792 6144 {
4a1b91ea
L
6145 case 2:
6146 type.bitfield.word = 1;
6147 break;
6148 case 4:
6149 type.bitfield.dword = 1;
6150 break;
8e6e0792
JB
6151 case 8:
6152 type.bitfield.qword = 1;
6153 break;
6154 case 16:
6155 type.bitfield.xmmword = 1;
6156 break;
6157 case 32:
6158 type.bitfield.ymmword = 1;
6159 break;
6160 case 64:
6161 type.bitfield.zmmword = 1;
6162 break;
6163 default:
6164 goto bad_broadcast;
6165 }
6166
6167 overlap = operand_type_and (type, t->operand_types[op]);
bc49bfd8
JB
6168 if (t->operand_types[op].bitfield.class == RegSIMD
6169 && t->operand_types[op].bitfield.byte
6170 + t->operand_types[op].bitfield.word
6171 + t->operand_types[op].bitfield.dword
6172 + t->operand_types[op].bitfield.qword > 1)
6173 {
6174 overlap.bitfield.xmmword = 0;
6175 overlap.bitfield.ymmword = 0;
6176 overlap.bitfield.zmmword = 0;
6177 }
8e6e0792
JB
6178 if (operand_type_all_zero (&overlap))
6179 goto bad_broadcast;
6180
6181 if (t->opcode_modifier.checkregsize)
6182 {
6183 unsigned int j;
6184
e2195274 6185 type.bitfield.baseindex = 1;
8e6e0792
JB
6186 for (j = 0; j < i.operands; ++j)
6187 {
6188 if (j != op
6189 && !operand_type_register_match(i.types[j],
6190 t->operand_types[j],
6191 type,
6192 t->operand_types[op]))
6193 goto bad_broadcast;
6194 }
6195 }
43234a1e
L
6196 }
6197 /* If broadcast is supported in this instruction, we need to check if
6198 operand of one-element size isn't specified without broadcast. */
6199 else if (t->opcode_modifier.broadcast && i.mem_operands)
6200 {
6201 /* Find memory operand. */
6202 for (op = 0; op < i.operands; op++)
8dc0818e 6203 if (i.flags[op] & Operand_Mem)
43234a1e
L
6204 break;
6205 gas_assert (op < i.operands);
6206 /* Check size of the memory operand. */
4a1b91ea 6207 if (match_broadcast_size (t, op))
43234a1e
L
6208 {
6209 i.error = broadcast_needed;
6210 return 1;
6211 }
6212 }
c39e5b26
JB
6213 else
6214 op = MAX_OPERANDS - 1; /* Avoid uninitialized variable warning. */
43234a1e
L
6215
6216 /* Check if requested masking is supported. */
6225c532 6217 if (i.mask.reg)
43234a1e 6218 {
ae2387fe
JB
6219 switch (t->opcode_modifier.masking)
6220 {
6221 case BOTH_MASKING:
6222 break;
6223 case MERGING_MASKING:
6225c532 6224 if (i.mask.zeroing)
ae2387fe
JB
6225 {
6226 case 0:
6227 i.error = unsupported_masking;
6228 return 1;
6229 }
6230 break;
6231 case DYNAMIC_MASKING:
6232 /* Memory destinations allow only merging masking. */
6225c532 6233 if (i.mask.zeroing && i.mem_operands)
ae2387fe
JB
6234 {
6235 /* Find memory operand. */
6236 for (op = 0; op < i.operands; op++)
c48dadc9 6237 if (i.flags[op] & Operand_Mem)
ae2387fe
JB
6238 break;
6239 gas_assert (op < i.operands);
6240 if (op == i.operands - 1)
6241 {
6242 i.error = unsupported_masking;
6243 return 1;
6244 }
6245 }
6246 break;
6247 default:
6248 abort ();
6249 }
43234a1e
L
6250 }
6251
6252 /* Check if masking is applied to dest operand. */
6225c532 6253 if (i.mask.reg && (i.mask.operand != i.operands - 1))
43234a1e
L
6254 {
6255 i.error = mask_not_on_destination;
6256 return 1;
6257 }
6258
43234a1e 6259 /* Check RC/SAE. */
ca5312a2 6260 if (i.rounding.type != rc_none)
43234a1e 6261 {
a80195f1 6262 if (!t->opcode_modifier.sae
cf665fee
JB
6263 || ((i.rounding.type != saeonly) != t->opcode_modifier.staticrounding)
6264 || i.mem_operands)
43234a1e
L
6265 {
6266 i.error = unsupported_rc_sae;
6267 return 1;
6268 }
cf665fee
JB
6269
6270 /* Non-EVEX.LIG forms need to have a ZMM register as at least one
6271 operand. */
6272 if (t->opcode_modifier.evex != EVEXLIG)
7bab8ab5 6273 {
cf665fee
JB
6274 for (op = 0; op < t->operands; ++op)
6275 if (i.types[op].bitfield.zmmword)
6276 break;
6277 if (op >= t->operands)
6278 {
6279 i.error = operand_size_mismatch;
6280 return 1;
6281 }
7bab8ab5 6282 }
6c30d220
L
6283 }
6284
da4977e0
JB
6285 /* Check the special Imm4 cases; must be the first operand. */
6286 if (t->cpu_flags.bitfield.cpuxop && t->operands == 5)
6287 {
6288 if (i.op[0].imms->X_op != O_constant
6289 || !fits_in_imm4 (i.op[0].imms->X_add_number))
6290 {
6291 i.error = bad_imm4;
6292 return 1;
6293 }
6294
6295 /* Turn off Imm<N> so that update_imm won't complain. */
6296 operand_type_set (&i.types[0], 0);
6297 }
6298
43234a1e 6299 /* Check vector Disp8 operand. */
b5014f7a 6300 if (t->opcode_modifier.disp8memshift
1a42a9fe 6301 && i.disp_encoding <= disp_encoding_8bit)
43234a1e 6302 {
a5748e0d 6303 if (i.broadcast.bytes)
4a1b91ea 6304 i.memshift = t->opcode_modifier.broadcast - 1;
7091c612 6305 else if (t->opcode_modifier.disp8memshift != DISP8_SHIFT_VL)
43234a1e 6306 i.memshift = t->opcode_modifier.disp8memshift;
7091c612
JB
6307 else
6308 {
125ff819 6309 const i386_operand_type *type = NULL, *fallback = NULL;
7091c612
JB
6310
6311 i.memshift = 0;
6312 for (op = 0; op < i.operands; op++)
8dc0818e 6313 if (i.flags[op] & Operand_Mem)
7091c612 6314 {
4174bfff
JB
6315 if (t->opcode_modifier.evex == EVEXLIG)
6316 i.memshift = 2 + (i.suffix == QWORD_MNEM_SUFFIX);
6317 else if (t->operand_types[op].bitfield.xmmword
6318 + t->operand_types[op].bitfield.ymmword
6319 + t->operand_types[op].bitfield.zmmword <= 1)
7091c612
JB
6320 type = &t->operand_types[op];
6321 else if (!i.types[op].bitfield.unspecified)
6322 type = &i.types[op];
125ff819
JB
6323 else /* Ambiguities get resolved elsewhere. */
6324 fallback = &t->operand_types[op];
7091c612 6325 }
3528c362 6326 else if (i.types[op].bitfield.class == RegSIMD
4174bfff 6327 && t->opcode_modifier.evex != EVEXLIG)
7091c612
JB
6328 {
6329 if (i.types[op].bitfield.zmmword)
6330 i.memshift = 6;
6331 else if (i.types[op].bitfield.ymmword && i.memshift < 5)
6332 i.memshift = 5;
6333 else if (i.types[op].bitfield.xmmword && i.memshift < 4)
6334 i.memshift = 4;
6335 }
6336
125ff819
JB
6337 if (!type && !i.memshift)
6338 type = fallback;
7091c612
JB
6339 if (type)
6340 {
6341 if (type->bitfield.zmmword)
6342 i.memshift = 6;
6343 else if (type->bitfield.ymmword)
6344 i.memshift = 5;
6345 else if (type->bitfield.xmmword)
6346 i.memshift = 4;
6347 }
6348
6349 /* For the check in fits_in_disp8(). */
6350 if (i.memshift == 0)
6351 i.memshift = -1;
6352 }
43234a1e
L
6353
6354 for (op = 0; op < i.operands; op++)
6355 if (operand_type_check (i.types[op], disp)
6356 && i.op[op].disps->X_op == O_constant)
6357 {
b5014f7a 6358 if (fits_in_disp8 (i.op[op].disps->X_add_number))
43234a1e 6359 {
b5014f7a
JB
6360 i.types[op].bitfield.disp8 = 1;
6361 return 0;
43234a1e 6362 }
b5014f7a 6363 i.types[op].bitfield.disp8 = 0;
43234a1e
L
6364 }
6365 }
b5014f7a
JB
6366
6367 i.memshift = 0;
43234a1e 6368
6c30d220
L
6369 return 0;
6370}
6371
da4977e0 6372/* Check if encoding requirements are met by the instruction. */
a683cc34
SP
6373
6374static int
da4977e0 6375VEX_check_encoding (const insn_template *t)
a683cc34 6376{
da4977e0
JB
6377 if (i.vec_encoding == vex_encoding_error)
6378 {
6379 i.error = unsupported;
6380 return 1;
6381 }
6382
86fa6981 6383 if (i.vec_encoding == vex_encoding_evex)
43234a1e 6384 {
86fa6981 6385 /* This instruction must be encoded with EVEX prefix. */
e771e7c9 6386 if (!is_evex_encoding (t))
86fa6981
L
6387 {
6388 i.error = unsupported;
6389 return 1;
6390 }
6391 return 0;
43234a1e
L
6392 }
6393
a683cc34 6394 if (!t->opcode_modifier.vex)
86fa6981
L
6395 {
6396 /* This instruction template doesn't have VEX prefix. */
6397 if (i.vec_encoding != vex_encoding_default)
6398 {
6399 i.error = unsupported;
6400 return 1;
6401 }
6402 return 0;
6403 }
a683cc34 6404
a683cc34
SP
6405 return 0;
6406}
6407
7b94647a
JB
6408/* Helper function for the progress() macro in match_template(). */
6409static INLINE enum i386_error progress (enum i386_error new,
6410 enum i386_error last,
6411 unsigned int line, unsigned int *line_p)
6412{
6413 if (line <= *line_p)
6414 return last;
6415 *line_p = line;
6416 return new;
6417}
6418
d3ce72d0 6419static const insn_template *
83b16ac6 6420match_template (char mnem_suffix)
29b0f896
AM
6421{
6422 /* Points to template once we've found it. */
d3ce72d0 6423 const insn_template *t;
40fb9820 6424 i386_operand_type overlap0, overlap1, overlap2, overlap3;
c0f3af97 6425 i386_operand_type overlap4;
29b0f896 6426 unsigned int found_reverse_match;
dc2be329 6427 i386_opcode_modifier suffix_check;
40fb9820 6428 i386_operand_type operand_types [MAX_OPERANDS];
539e75ad 6429 int addr_prefix_disp;
7b94647a
JB
6430 unsigned int j, size_match, check_register, errline = __LINE__;
6431 enum i386_error specific_error = number_of_operands_mismatch;
6432#define progress(err) progress (err, specific_error, __LINE__, &errline)
29b0f896 6433
c0f3af97
L
6434#if MAX_OPERANDS != 5
6435# error "MAX_OPERANDS must be 5."
f48ff2ae
L
6436#endif
6437
29b0f896 6438 found_reverse_match = 0;
539e75ad 6439 addr_prefix_disp = -1;
40fb9820 6440
dc2be329 6441 /* Prepare for mnemonic suffix check. */
40fb9820 6442 memset (&suffix_check, 0, sizeof (suffix_check));
dc2be329
L
6443 switch (mnem_suffix)
6444 {
6445 case BYTE_MNEM_SUFFIX:
6446 suffix_check.no_bsuf = 1;
6447 break;
6448 case WORD_MNEM_SUFFIX:
6449 suffix_check.no_wsuf = 1;
6450 break;
6451 case SHORT_MNEM_SUFFIX:
6452 suffix_check.no_ssuf = 1;
6453 break;
6454 case LONG_MNEM_SUFFIX:
6455 suffix_check.no_lsuf = 1;
6456 break;
6457 case QWORD_MNEM_SUFFIX:
6458 suffix_check.no_qsuf = 1;
6459 break;
6460 default:
6461 /* NB: In Intel syntax, normally we can check for memory operand
6462 size when there is no mnemonic suffix. But jmp and call have
6463 2 different encodings with Dword memory operand size, one with
6464 No_ldSuf and the other without. i.suffix is set to
6465 LONG_DOUBLE_MNEM_SUFFIX to skip the one with No_ldSuf. */
6466 if (i.suffix == LONG_DOUBLE_MNEM_SUFFIX)
6467 suffix_check.no_ldsuf = 1;
83b16ac6
JB
6468 }
6469
45aa61fe 6470 for (t = current_templates->start; t < current_templates->end; t++)
29b0f896 6471 {
539e75ad 6472 addr_prefix_disp = -1;
dbbc8b7e 6473 found_reverse_match = 0;
539e75ad 6474
7b94647a 6475 /* Must have right number of operands. */
29b0f896
AM
6476 if (i.operands != t->operands)
6477 continue;
6478
50aecf8c 6479 /* Check processor support. */
7b94647a 6480 specific_error = progress (unsupported);
45a4bb20 6481 if (cpu_flags_match (t) != CPU_FLAGS_PERFECT_MATCH)
50aecf8c
L
6482 continue;
6483
e1d4d893 6484 /* Check AT&T mnemonic. */
7b94647a 6485 specific_error = progress (unsupported_with_intel_mnemonic);
e1d4d893 6486 if (intel_mnemonic && t->opcode_modifier.attmnemonic)
1efbbeb4
L
6487 continue;
6488
4b5aaf5f 6489 /* Check AT&T/Intel syntax. */
7b94647a 6490 specific_error = progress (unsupported_syntax);
5c07affc 6491 if ((intel_syntax && t->opcode_modifier.attsyntax)
4b5aaf5f 6492 || (!intel_syntax && t->opcode_modifier.intelsyntax))
1efbbeb4
L
6493 continue;
6494
4b5aaf5f
L
6495 /* Check Intel64/AMD64 ISA. */
6496 switch (isa64)
6497 {
6498 default:
6499 /* Default: Don't accept Intel64. */
6500 if (t->opcode_modifier.isa64 == INTEL64)
6501 continue;
6502 break;
6503 case amd64:
6504 /* -mamd64: Don't accept Intel64 and Intel64 only. */
6505 if (t->opcode_modifier.isa64 >= INTEL64)
6506 continue;
6507 break;
6508 case intel64:
6509 /* -mintel64: Don't accept AMD64. */
5990e377 6510 if (t->opcode_modifier.isa64 == AMD64 && flag_code == CODE_64BIT)
4b5aaf5f
L
6511 continue;
6512 break;
6513 }
6514
dc2be329 6515 /* Check the suffix. */
7b94647a 6516 specific_error = progress (invalid_instruction_suffix);
dc2be329
L
6517 if ((t->opcode_modifier.no_bsuf && suffix_check.no_bsuf)
6518 || (t->opcode_modifier.no_wsuf && suffix_check.no_wsuf)
6519 || (t->opcode_modifier.no_lsuf && suffix_check.no_lsuf)
6520 || (t->opcode_modifier.no_ssuf && suffix_check.no_ssuf)
6521 || (t->opcode_modifier.no_qsuf && suffix_check.no_qsuf)
6522 || (t->opcode_modifier.no_ldsuf && suffix_check.no_ldsuf))
83b16ac6 6523 continue;
29b0f896 6524
7b94647a 6525 specific_error = progress (operand_size_mismatch);
3ac21baa
JB
6526 size_match = operand_size_match (t);
6527 if (!size_match)
7d5e4556 6528 continue;
539e75ad 6529
6f2f06be
JB
6530 /* This is intentionally not
6531
0cfa3eb3 6532 if (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE))
6f2f06be
JB
6533
6534 as the case of a missing * on the operand is accepted (perhaps with
6535 a warning, issued further down). */
7b94647a 6536 specific_error = progress (operand_type_mismatch);
0cfa3eb3 6537 if (i.jumpabsolute && t->opcode_modifier.jump != JUMP_ABSOLUTE)
7b94647a 6538 continue;
6f2f06be 6539
5c07affc
L
6540 for (j = 0; j < MAX_OPERANDS; j++)
6541 operand_types[j] = t->operand_types[j];
6542
e365e234
JB
6543 /* In general, don't allow
6544 - 64-bit operands outside of 64-bit mode,
6545 - 32-bit operands on pre-386. */
7b94647a
JB
6546 specific_error = progress (mnem_suffix ? invalid_instruction_suffix
6547 : operand_size_mismatch);
4873e243 6548 j = i.imm_operands + (t->operands > i.imm_operands + 1);
e365e234
JB
6549 if (((i.suffix == QWORD_MNEM_SUFFIX
6550 && flag_code != CODE_64BIT
389d00a5
JB
6551 && !(t->opcode_modifier.opcodespace == SPACE_0F
6552 && t->base_opcode == 0xc7
5e74b495 6553 && t->opcode_modifier.opcodeprefix == PREFIX_NONE
8b65b895 6554 && t->extension_opcode == 1) /* cmpxchg8b */)
e365e234
JB
6555 || (i.suffix == LONG_MNEM_SUFFIX
6556 && !cpu_arch_flags.bitfield.cpui386))
45aa61fe 6557 && (intel_syntax
3cd7f3e3 6558 ? (t->opcode_modifier.mnemonicsize != IGNORESIZE
45aa61fe
AM
6559 && !intel_float_operand (t->name))
6560 : intel_float_operand (t->name) != 2)
4873e243
JB
6561 && (t->operands == i.imm_operands
6562 || (operand_types[i.imm_operands].bitfield.class != RegMMX
6563 && operand_types[i.imm_operands].bitfield.class != RegSIMD
6564 && operand_types[i.imm_operands].bitfield.class != RegMask)
6565 || (operand_types[j].bitfield.class != RegMMX
6566 && operand_types[j].bitfield.class != RegSIMD
6567 && operand_types[j].bitfield.class != RegMask))
63112cd6 6568 && !t->opcode_modifier.sib)
192dc9c6
JB
6569 continue;
6570
29b0f896 6571 /* Do not verify operands when there are none. */
e365e234 6572 if (!t->operands)
da4977e0
JB
6573 {
6574 if (VEX_check_encoding (t))
6575 {
7b94647a 6576 specific_error = progress (i.error);
da4977e0
JB
6577 continue;
6578 }
6579
6580 /* We've found a match; break out of loop. */
6581 break;
6582 }
252b5132 6583
48bcea9f
JB
6584 if (!t->opcode_modifier.jump
6585 || t->opcode_modifier.jump == JUMP_ABSOLUTE)
6586 {
6587 /* There should be only one Disp operand. */
6588 for (j = 0; j < MAX_OPERANDS; j++)
6589 if (operand_type_check (operand_types[j], disp))
539e75ad 6590 break;
48bcea9f
JB
6591 if (j < MAX_OPERANDS)
6592 {
5b7c81bd 6593 bool override = (i.prefix[ADDR_PREFIX] != 0);
48bcea9f
JB
6594
6595 addr_prefix_disp = j;
6596
a775efc8
JB
6597 /* Address size prefix will turn Disp64 operand into Disp32 and
6598 Disp32/Disp16 one into Disp16/Disp32 respectively. */
48bcea9f 6599 switch (flag_code)
40fb9820 6600 {
48bcea9f
JB
6601 case CODE_16BIT:
6602 override = !override;
6603 /* Fall through. */
6604 case CODE_32BIT:
6605 if (operand_types[j].bitfield.disp32
6606 && operand_types[j].bitfield.disp16)
40fb9820 6607 {
48bcea9f
JB
6608 operand_types[j].bitfield.disp16 = override;
6609 operand_types[j].bitfield.disp32 = !override;
40fb9820 6610 }
a775efc8 6611 gas_assert (!operand_types[j].bitfield.disp64);
48bcea9f
JB
6612 break;
6613
6614 case CODE_64BIT:
a775efc8 6615 if (operand_types[j].bitfield.disp64)
40fb9820 6616 {
a775efc8 6617 gas_assert (!operand_types[j].bitfield.disp32);
48bcea9f 6618 operand_types[j].bitfield.disp32 = override;
a775efc8 6619 operand_types[j].bitfield.disp64 = !override;
40fb9820 6620 }
48bcea9f
JB
6621 operand_types[j].bitfield.disp16 = 0;
6622 break;
40fb9820 6623 }
539e75ad 6624 }
48bcea9f 6625 }
539e75ad 6626
d7e3e627
L
6627 switch (i.reloc[0])
6628 {
6629 case BFD_RELOC_386_GOT32:
6630 /* Force 0x8b encoding for "mov foo@GOT, %eax". */
6631 if (t->base_opcode == 0xa0
6632 && t->opcode_modifier.opcodespace == SPACE_BASE)
6633 continue;
6634 break;
6635 case BFD_RELOC_386_TLS_GOTIE:
6636 case BFD_RELOC_386_TLS_LE_32:
6637 case BFD_RELOC_X86_64_GOTTPOFF:
6638 case BFD_RELOC_X86_64_TLSLD:
6639 /* Don't allow KMOV in TLS code sequences. */
6640 if (t->opcode_modifier.vex)
6641 continue;
6642 break;
6643 default:
6644 break;
6645 }
02a86693 6646
56ffb741 6647 /* We check register size if needed. */
e2195274
JB
6648 if (t->opcode_modifier.checkregsize)
6649 {
6650 check_register = (1 << t->operands) - 1;
a5748e0d 6651 if (i.broadcast.type || i.broadcast.bytes)
5273a3cd 6652 check_register &= ~(1 << i.broadcast.operand);
e2195274
JB
6653 }
6654 else
6655 check_register = 0;
6656
c6fb90c8 6657 overlap0 = operand_type_and (i.types[0], operand_types[0]);
29b0f896
AM
6658 switch (t->operands)
6659 {
6660 case 1:
40fb9820 6661 if (!operand_type_match (overlap0, i.types[0]))
29b0f896
AM
6662 continue;
6663 break;
6664 case 2:
33eaf5de 6665 /* xchg %eax, %eax is a special case. It is an alias for nop
8b38ad71
L
6666 only in 32bit mode and we can use opcode 0x90. In 64bit
6667 mode, we can't use 0x90 for xchg %eax, %eax since it should
6668 zero-extend %eax to %rax. */
6669 if (flag_code == CODE_64BIT
6670 && t->base_opcode == 0x90
35648716 6671 && t->opcode_modifier.opcodespace == SPACE_BASE
75e5731b
JB
6672 && i.types[0].bitfield.instance == Accum
6673 && i.types[0].bitfield.dword
6674 && i.types[1].bitfield.instance == Accum
6675 && i.types[1].bitfield.dword)
8b38ad71 6676 continue;
1212781b
JB
6677 /* xrelease mov %eax, <disp> is another special case. It must not
6678 match the accumulator-only encoding of mov. */
6679 if (flag_code != CODE_64BIT
6680 && i.hle_prefix
6681 && t->base_opcode == 0xa0
35648716 6682 && t->opcode_modifier.opcodespace == SPACE_BASE
75e5731b 6683 && i.types[0].bitfield.instance == Accum
8dc0818e 6684 && (i.flags[1] & Operand_Mem))
1212781b 6685 continue;
f5eb1d70
JB
6686 /* Fall through. */
6687
6688 case 3:
3ac21baa
JB
6689 if (!(size_match & MATCH_STRAIGHT))
6690 goto check_reverse;
64c49ab3
JB
6691 /* Reverse direction of operands if swapping is possible in the first
6692 place (operands need to be symmetric) and
6693 - the load form is requested, and the template is a store form,
6694 - the store form is requested, and the template is a load form,
6695 - the non-default (swapped) form is requested. */
6696 overlap1 = operand_type_and (operand_types[0], operand_types[1]);
f5eb1d70 6697 if (t->opcode_modifier.d && i.reg_operands == i.operands
64c49ab3
JB
6698 && !operand_type_all_zero (&overlap1))
6699 switch (i.dir_encoding)
6700 {
6701 case dir_encoding_load:
6702 if (operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 6703 || t->opcode_modifier.regmem)
64c49ab3
JB
6704 goto check_reverse;
6705 break;
6706
6707 case dir_encoding_store:
6708 if (!operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 6709 && !t->opcode_modifier.regmem)
64c49ab3
JB
6710 goto check_reverse;
6711 break;
6712
6713 case dir_encoding_swap:
6714 goto check_reverse;
6715
6716 case dir_encoding_default:
6717 break;
6718 }
86fa6981 6719 /* If we want store form, we skip the current load. */
64c49ab3
JB
6720 if ((i.dir_encoding == dir_encoding_store
6721 || i.dir_encoding == dir_encoding_swap)
86fa6981
L
6722 && i.mem_operands == 0
6723 && t->opcode_modifier.load)
fa99fab2 6724 continue;
1a0670f3 6725 /* Fall through. */
f48ff2ae 6726 case 4:
c0f3af97 6727 case 5:
c6fb90c8 6728 overlap1 = operand_type_and (i.types[1], operand_types[1]);
40fb9820
L
6729 if (!operand_type_match (overlap0, i.types[0])
6730 || !operand_type_match (overlap1, i.types[1])
e2195274 6731 || ((check_register & 3) == 3
dc821c5f 6732 && !operand_type_register_match (i.types[0],
40fb9820 6733 operand_types[0],
dc821c5f 6734 i.types[1],
40fb9820 6735 operand_types[1])))
29b0f896 6736 {
7b94647a
JB
6737 specific_error = progress (i.error);
6738
29b0f896 6739 /* Check if other direction is valid ... */
38e314eb 6740 if (!t->opcode_modifier.d)
29b0f896
AM
6741 continue;
6742
dc1e8a47 6743 check_reverse:
3ac21baa
JB
6744 if (!(size_match & MATCH_REVERSE))
6745 continue;
29b0f896 6746 /* Try reversing direction of operands. */
8bd915b7
JB
6747 j = t->opcode_modifier.vexsources ? 1 : i.operands - 1;
6748 overlap0 = operand_type_and (i.types[0], operand_types[j]);
6749 overlap1 = operand_type_and (i.types[j], operand_types[0]);
c975cec5
JB
6750 overlap2 = operand_type_and (i.types[1], operand_types[1]);
6751 gas_assert (t->operands != 3 || !check_register);
40fb9820 6752 if (!operand_type_match (overlap0, i.types[0])
8bd915b7 6753 || !operand_type_match (overlap1, i.types[j])
c975cec5
JB
6754 || (t->operands == 3
6755 && !operand_type_match (overlap2, i.types[1]))
45664ddb 6756 || (check_register
dc821c5f 6757 && !operand_type_register_match (i.types[0],
8bd915b7
JB
6758 operand_types[j],
6759 i.types[j],
45664ddb 6760 operand_types[0])))
29b0f896
AM
6761 {
6762 /* Does not match either direction. */
7b94647a 6763 specific_error = progress (i.error);
29b0f896
AM
6764 continue;
6765 }
38e314eb 6766 /* found_reverse_match holds which of D or FloatR
29b0f896 6767 we've found. */
38e314eb
JB
6768 if (!t->opcode_modifier.d)
6769 found_reverse_match = 0;
6770 else if (operand_types[0].bitfield.tbyte)
8a2ed489 6771 found_reverse_match = Opcode_FloatD;
8bd915b7
JB
6772 else if (t->opcode_modifier.vexsources)
6773 {
6774 found_reverse_match = Opcode_VexW;
6775 goto check_operands_345;
6776 }
2c735193
JB
6777 else if (t->opcode_modifier.opcodespace != SPACE_BASE
6778 && (t->opcode_modifier.opcodespace != SPACE_0F
6779 /* MOV to/from CR/DR/TR, as an exception, follow
6780 the base opcode space encoding model. */
6781 || (t->base_opcode | 7) != 0x27))
dbbc8b7e 6782 found_reverse_match = (t->base_opcode & 0xee) != 0x6e
2c735193 6783 ? Opcode_ExtD : Opcode_SIMD_IntD;
8a2ed489 6784 else
38e314eb 6785 found_reverse_match = Opcode_D;
40fb9820 6786 if (t->opcode_modifier.floatr)
8a2ed489 6787 found_reverse_match |= Opcode_FloatR;
29b0f896 6788 }
f48ff2ae 6789 else
29b0f896 6790 {
f48ff2ae 6791 /* Found a forward 2 operand match here. */
8bd915b7 6792 check_operands_345:
d1cbb4db
L
6793 switch (t->operands)
6794 {
c0f3af97 6795 case 5:
3d0738af 6796 overlap4 = operand_type_and (i.types[4], operand_types[4]);
c0f3af97 6797 if (!operand_type_match (overlap4, i.types[4])
dc821c5f 6798 || !operand_type_register_match (i.types[3],
c0f3af97 6799 operand_types[3],
c0f3af97
L
6800 i.types[4],
6801 operand_types[4]))
7b94647a
JB
6802 {
6803 specific_error = progress (i.error);
6804 continue;
6805 }
1a0670f3 6806 /* Fall through. */
f48ff2ae 6807 case 4:
3d0738af 6808 overlap3 = operand_type_and (i.types[3], operand_types[3]);
40fb9820 6809 if (!operand_type_match (overlap3, i.types[3])
e2195274
JB
6810 || ((check_register & 0xa) == 0xa
6811 && !operand_type_register_match (i.types[1],
f7768225
JB
6812 operand_types[1],
6813 i.types[3],
e2195274
JB
6814 operand_types[3]))
6815 || ((check_register & 0xc) == 0xc
6816 && !operand_type_register_match (i.types[2],
6817 operand_types[2],
6818 i.types[3],
6819 operand_types[3])))
7b94647a
JB
6820 {
6821 specific_error = progress (i.error);
6822 continue;
6823 }
1a0670f3 6824 /* Fall through. */
f48ff2ae 6825 case 3:
3d0738af 6826 overlap2 = operand_type_and (i.types[2], operand_types[2]);
40fb9820 6827 if (!operand_type_match (overlap2, i.types[2])
e2195274
JB
6828 || ((check_register & 5) == 5
6829 && !operand_type_register_match (i.types[0],
23e42951
JB
6830 operand_types[0],
6831 i.types[2],
e2195274
JB
6832 operand_types[2]))
6833 || ((check_register & 6) == 6
6834 && !operand_type_register_match (i.types[1],
6835 operand_types[1],
6836 i.types[2],
6837 operand_types[2])))
7b94647a
JB
6838 {
6839 specific_error = progress (i.error);
6840 continue;
6841 }
f48ff2ae
L
6842 break;
6843 }
29b0f896 6844 }
f48ff2ae 6845 /* Found either forward/reverse 2, 3 or 4 operand match here:
29b0f896
AM
6846 slip through to break. */
6847 }
c0f3af97 6848
9bb4d860
L
6849 /* Check if VEX/EVEX encoding requirements can be satisfied. */
6850 if (VEX_check_encoding (t))
da4977e0 6851 {
7b94647a 6852 specific_error = progress (i.error);
da4977e0
JB
6853 continue;
6854 }
6855
9bb4d860
L
6856 /* Check if vector operands are valid. */
6857 if (check_VecOperands (t))
5614d22c 6858 {
7b94647a 6859 specific_error = progress (i.error);
5614d22c
JB
6860 continue;
6861 }
a683cc34 6862
29b0f896
AM
6863 /* We've found a match; break out of loop. */
6864 break;
6865 }
6866
7b94647a
JB
6867#undef progress
6868
29b0f896
AM
6869 if (t == current_templates->end)
6870 {
6871 /* We found no match. */
a65babc9 6872 const char *err_msg;
7b94647a 6873 switch (specific_error)
a65babc9
L
6874 {
6875 default:
6876 abort ();
86e026a4 6877 case operand_size_mismatch:
a65babc9
L
6878 err_msg = _("operand size mismatch");
6879 break;
6880 case operand_type_mismatch:
6881 err_msg = _("operand type mismatch");
6882 break;
6883 case register_type_mismatch:
6884 err_msg = _("register type mismatch");
6885 break;
6886 case number_of_operands_mismatch:
6887 err_msg = _("number of operands mismatch");
6888 break;
6889 case invalid_instruction_suffix:
6890 err_msg = _("invalid instruction suffix");
6891 break;
6892 case bad_imm4:
4a2608e3 6893 err_msg = _("constant doesn't fit in 4 bits");
a65babc9 6894 break;
a65babc9
L
6895 case unsupported_with_intel_mnemonic:
6896 err_msg = _("unsupported with Intel mnemonic");
6897 break;
6898 case unsupported_syntax:
6899 err_msg = _("unsupported syntax");
6900 break;
6901 case unsupported:
35262a23 6902 as_bad (_("unsupported instruction `%s'"),
10efe3f6
L
6903 current_templates->start->name);
6904 return NULL;
260cd341
LC
6905 case invalid_sib_address:
6906 err_msg = _("invalid SIB address");
6907 break;
6c30d220
L
6908 case invalid_vsib_address:
6909 err_msg = _("invalid VSIB address");
6910 break;
7bab8ab5
JB
6911 case invalid_vector_register_set:
6912 err_msg = _("mask, index, and destination registers must be distinct");
6913 break;
260cd341
LC
6914 case invalid_tmm_register_set:
6915 err_msg = _("all tmm registers must be distinct");
6916 break;
0cc78721
CL
6917 case invalid_dest_and_src_register_set:
6918 err_msg = _("destination and source registers must be distinct");
6919 break;
6c30d220
L
6920 case unsupported_vector_index_register:
6921 err_msg = _("unsupported vector index register");
6922 break;
43234a1e
L
6923 case unsupported_broadcast:
6924 err_msg = _("unsupported broadcast");
6925 break;
43234a1e
L
6926 case broadcast_needed:
6927 err_msg = _("broadcast is needed for operand of such type");
6928 break;
6929 case unsupported_masking:
6930 err_msg = _("unsupported masking");
6931 break;
6932 case mask_not_on_destination:
6933 err_msg = _("mask not on destination operand");
6934 break;
6935 case no_default_mask:
6936 err_msg = _("default mask isn't allowed");
6937 break;
6938 case unsupported_rc_sae:
6939 err_msg = _("unsupported static rounding/sae");
6940 break;
43234a1e
L
6941 case invalid_register_operand:
6942 err_msg = _("invalid register operand");
6943 break;
a65babc9
L
6944 }
6945 as_bad (_("%s for `%s'"), err_msg,
891edac4 6946 current_templates->start->name);
fa99fab2 6947 return NULL;
29b0f896 6948 }
252b5132 6949
29b0f896
AM
6950 if (!quiet_warnings)
6951 {
6952 if (!intel_syntax
0cfa3eb3 6953 && (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE)))
6f2f06be 6954 as_warn (_("indirect %s without `*'"), t->name);
29b0f896 6955
40fb9820 6956 if (t->opcode_modifier.isprefix
3cd7f3e3 6957 && t->opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
6958 {
6959 /* Warn them that a data or address size prefix doesn't
6960 affect assembly of the next line of code. */
6961 as_warn (_("stand-alone `%s' prefix"), t->name);
6962 }
6963 }
6964
6965 /* Copy the template we found. */
9a182d04 6966 install_template (t);
539e75ad
L
6967
6968 if (addr_prefix_disp != -1)
6969 i.tm.operand_types[addr_prefix_disp]
6970 = operand_types[addr_prefix_disp];
6971
8bd915b7 6972 switch (found_reverse_match)
29b0f896 6973 {
8bd915b7
JB
6974 case 0:
6975 break;
6976
6977 default:
dfd69174
JB
6978 /* If we found a reverse match we must alter the opcode direction
6979 bit and clear/flip the regmem modifier one. found_reverse_match
6980 holds bits to change (different for int & float insns). */
29b0f896
AM
6981
6982 i.tm.base_opcode ^= found_reverse_match;
6983
f5eb1d70
JB
6984 i.tm.operand_types[0] = operand_types[i.operands - 1];
6985 i.tm.operand_types[i.operands - 1] = operand_types[0];
dfd69174
JB
6986
6987 /* Certain SIMD insns have their load forms specified in the opcode
6988 table, and hence we need to _set_ RegMem instead of clearing it.
6989 We need to avoid setting the bit though on insns like KMOVW. */
6990 i.tm.opcode_modifier.regmem
6991 = i.tm.opcode_modifier.modrm && i.tm.opcode_modifier.d
6992 && i.tm.operands > 2U - i.tm.opcode_modifier.sse2avx
6993 && !i.tm.opcode_modifier.regmem;
8bd915b7
JB
6994 break;
6995
6996 case Opcode_VexW:
6997 /* Only the first two register operands need reversing, alongside
6998 flipping VEX.W. */
6999 i.tm.opcode_modifier.vexw ^= VEXW0 ^ VEXW1;
7000
7001 j = i.tm.operand_types[0].bitfield.imm8;
7002 i.tm.operand_types[j] = operand_types[j + 1];
7003 i.tm.operand_types[j + 1] = operand_types[j];
7004 break;
29b0f896
AM
7005 }
7006
fa99fab2 7007 return t;
29b0f896
AM
7008}
7009
7010static int
e3bb37b5 7011check_string (void)
29b0f896 7012{
51c8edf6
JB
7013 unsigned int es_op = i.tm.opcode_modifier.isstring - IS_STRING_ES_OP0;
7014 unsigned int op = i.tm.operand_types[0].bitfield.baseindex ? es_op : 0;
8dc0818e 7015
5e042380 7016 if (i.seg[op] != NULL && i.seg[op] != reg_es)
29b0f896 7017 {
51c8edf6
JB
7018 as_bad (_("`%s' operand %u must use `%ses' segment"),
7019 i.tm.name,
7020 intel_syntax ? i.tm.operands - es_op : es_op + 1,
7021 register_prefix);
7022 return 0;
29b0f896 7023 }
51c8edf6
JB
7024
7025 /* There's only ever one segment override allowed per instruction.
7026 This instruction possibly has a legal segment override on the
7027 second operand, so copy the segment to where non-string
7028 instructions store it, allowing common code. */
7029 i.seg[op] = i.seg[1];
7030
29b0f896
AM
7031 return 1;
7032}
7033
7034static int
543613e9 7035process_suffix (void)
29b0f896 7036{
5b7c81bd 7037 bool is_crc32 = false, is_movx = false;
8b65b895 7038
29b0f896
AM
7039 /* If matched instruction specifies an explicit instruction mnemonic
7040 suffix, use it. */
673fe0f0 7041 if (i.tm.opcode_modifier.size == SIZE16)
40fb9820 7042 i.suffix = WORD_MNEM_SUFFIX;
673fe0f0 7043 else if (i.tm.opcode_modifier.size == SIZE32)
40fb9820 7044 i.suffix = LONG_MNEM_SUFFIX;
673fe0f0 7045 else if (i.tm.opcode_modifier.size == SIZE64)
40fb9820 7046 i.suffix = QWORD_MNEM_SUFFIX;
13e600d0 7047 else if (i.reg_operands
c8f8eebc
JB
7048 && (i.operands > 1 || i.types[0].bitfield.class == Reg)
7049 && !i.tm.opcode_modifier.addrprefixopreg)
29b0f896 7050 {
65fca059 7051 unsigned int numop = i.operands;
389d00a5
JB
7052
7053 /* MOVSX/MOVZX */
7054 is_movx = (i.tm.opcode_modifier.opcodespace == SPACE_0F
7055 && (i.tm.base_opcode | 8) == 0xbe)
7056 || (i.tm.opcode_modifier.opcodespace == SPACE_BASE
7057 && i.tm.base_opcode == 0x63
7058 && i.tm.cpu_flags.bitfield.cpu64);
7059
8b65b895 7060 /* CRC32 */
389d00a5
JB
7061 is_crc32 = (i.tm.base_opcode == 0xf0
7062 && i.tm.opcode_modifier.opcodespace == SPACE_0F38
8b65b895 7063 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2);
65fca059
JB
7064
7065 /* movsx/movzx want only their source operand considered here, for the
7066 ambiguity checking below. The suffix will be replaced afterwards
7067 to represent the destination (register). */
389d00a5 7068 if (is_movx && (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63))
65fca059
JB
7069 --i.operands;
7070
643bb870 7071 /* crc32 needs REX.W set regardless of suffix / source operand size. */
8b65b895 7072 if (is_crc32 && i.tm.operand_types[1].bitfield.qword)
643bb870
JB
7073 i.rex |= REX_W;
7074
29b0f896 7075 /* If there's no instruction mnemonic suffix we try to invent one
13e600d0 7076 based on GPR operands. */
29b0f896
AM
7077 if (!i.suffix)
7078 {
7079 /* We take i.suffix from the last register operand specified,
7080 Destination register type is more significant than source
381d071f
L
7081 register type. crc32 in SSE4.2 prefers source register
7082 type. */
8b65b895 7083 unsigned int op = is_crc32 ? 1 : i.operands;
20592a94 7084
1a035124
JB
7085 while (op--)
7086 if (i.tm.operand_types[op].bitfield.instance == InstanceNone
7087 || i.tm.operand_types[op].bitfield.instance == Accum)
7088 {
7089 if (i.types[op].bitfield.class != Reg)
7090 continue;
7091 if (i.types[op].bitfield.byte)
7092 i.suffix = BYTE_MNEM_SUFFIX;
7093 else if (i.types[op].bitfield.word)
7094 i.suffix = WORD_MNEM_SUFFIX;
7095 else if (i.types[op].bitfield.dword)
7096 i.suffix = LONG_MNEM_SUFFIX;
7097 else if (i.types[op].bitfield.qword)
7098 i.suffix = QWORD_MNEM_SUFFIX;
7099 else
7100 continue;
7101 break;
7102 }
65fca059
JB
7103
7104 /* As an exception, movsx/movzx silently default to a byte source
7105 in AT&T mode. */
389d00a5 7106 if (is_movx && i.tm.opcode_modifier.w && !i.suffix && !intel_syntax)
65fca059 7107 i.suffix = BYTE_MNEM_SUFFIX;
29b0f896
AM
7108 }
7109 else if (i.suffix == BYTE_MNEM_SUFFIX)
7110 {
1cb0ab18 7111 if (!check_byte_reg ())
29b0f896
AM
7112 return 0;
7113 }
7114 else if (i.suffix == LONG_MNEM_SUFFIX)
7115 {
1cb0ab18 7116 if (!check_long_reg ())
29b0f896
AM
7117 return 0;
7118 }
7119 else if (i.suffix == QWORD_MNEM_SUFFIX)
7120 {
1cb0ab18 7121 if (!check_qword_reg ())
29b0f896
AM
7122 return 0;
7123 }
7124 else if (i.suffix == WORD_MNEM_SUFFIX)
7125 {
1cb0ab18 7126 if (!check_word_reg ())
29b0f896
AM
7127 return 0;
7128 }
3cd7f3e3
L
7129 else if (intel_syntax
7130 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
7131 /* Do nothing if the instruction is going to ignore the prefix. */
7132 ;
7133 else
7134 abort ();
65fca059
JB
7135
7136 /* Undo the movsx/movzx change done above. */
7137 i.operands = numop;
29b0f896 7138 }
3cd7f3e3
L
7139 else if (i.tm.opcode_modifier.mnemonicsize == DEFAULTSIZE
7140 && !i.suffix)
29b0f896 7141 {
13e600d0
JB
7142 i.suffix = stackop_size;
7143 if (stackop_size == LONG_MNEM_SUFFIX)
06f74c5c
L
7144 {
7145 /* stackop_size is set to LONG_MNEM_SUFFIX for the
7146 .code16gcc directive to support 16-bit mode with
7147 32-bit address. For IRET without a suffix, generate
7148 16-bit IRET (opcode 0xcf) to return from an interrupt
7149 handler. */
13e600d0
JB
7150 if (i.tm.base_opcode == 0xcf)
7151 {
7152 i.suffix = WORD_MNEM_SUFFIX;
7153 as_warn (_("generating 16-bit `iret' for .code16gcc directive"));
7154 }
7155 /* Warn about changed behavior for segment register push/pop. */
7156 else if ((i.tm.base_opcode | 1) == 0x07)
7157 as_warn (_("generating 32-bit `%s', unlike earlier gas versions"),
7158 i.tm.name);
06f74c5c 7159 }
29b0f896 7160 }
c006a730 7161 else if (!i.suffix
0cfa3eb3
JB
7162 && (i.tm.opcode_modifier.jump == JUMP_ABSOLUTE
7163 || i.tm.opcode_modifier.jump == JUMP_BYTE
7164 || i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT
389d00a5
JB
7165 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
7166 && i.tm.base_opcode == 0x01 /* [ls][gi]dt */
64e74474 7167 && i.tm.extension_opcode <= 3)))
9306ca4a
JB
7168 {
7169 switch (flag_code)
7170 {
7171 case CODE_64BIT:
40fb9820 7172 if (!i.tm.opcode_modifier.no_qsuf)
9306ca4a 7173 {
828c2a25
JB
7174 if (i.tm.opcode_modifier.jump == JUMP_BYTE
7175 || i.tm.opcode_modifier.no_lsuf)
7176 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a
JB
7177 break;
7178 }
1a0670f3 7179 /* Fall through. */
9306ca4a 7180 case CODE_32BIT:
40fb9820 7181 if (!i.tm.opcode_modifier.no_lsuf)
9306ca4a
JB
7182 i.suffix = LONG_MNEM_SUFFIX;
7183 break;
7184 case CODE_16BIT:
40fb9820 7185 if (!i.tm.opcode_modifier.no_wsuf)
9306ca4a
JB
7186 i.suffix = WORD_MNEM_SUFFIX;
7187 break;
7188 }
7189 }
252b5132 7190
c006a730 7191 if (!i.suffix
3cd7f3e3 7192 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8
JB
7193 /* Also cover lret/retf/iret in 64-bit mode. */
7194 || (flag_code == CODE_64BIT
7195 && !i.tm.opcode_modifier.no_lsuf
7196 && !i.tm.opcode_modifier.no_qsuf))
3cd7f3e3 7197 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
8bbb3ad8
JB
7198 /* Explicit sizing prefixes are assumed to disambiguate insns. */
7199 && !i.prefix[DATA_PREFIX] && !(i.prefix[REX_PREFIX] & REX_W)
62b3f548
JB
7200 /* Accept FLDENV et al without suffix. */
7201 && (i.tm.opcode_modifier.no_ssuf || i.tm.opcode_modifier.floatmf))
29b0f896 7202 {
6c0946d0 7203 unsigned int suffixes, evex = 0;
c006a730
JB
7204
7205 suffixes = !i.tm.opcode_modifier.no_bsuf;
7206 if (!i.tm.opcode_modifier.no_wsuf)
7207 suffixes |= 1 << 1;
7208 if (!i.tm.opcode_modifier.no_lsuf)
7209 suffixes |= 1 << 2;
7210 if (!i.tm.opcode_modifier.no_ldsuf)
7211 suffixes |= 1 << 3;
7212 if (!i.tm.opcode_modifier.no_ssuf)
7213 suffixes |= 1 << 4;
7214 if (flag_code == CODE_64BIT && !i.tm.opcode_modifier.no_qsuf)
7215 suffixes |= 1 << 5;
7216
6c0946d0
JB
7217 /* For [XYZ]MMWORD operands inspect operand sizes. While generally
7218 also suitable for AT&T syntax mode, it was requested that this be
7219 restricted to just Intel syntax. */
a5748e0d
JB
7220 if (intel_syntax && is_any_vex_encoding (&i.tm)
7221 && !i.broadcast.type && !i.broadcast.bytes)
6c0946d0 7222 {
b9915cbc 7223 unsigned int op;
6c0946d0 7224
b9915cbc 7225 for (op = 0; op < i.tm.operands; ++op)
6c0946d0 7226 {
b9915cbc
JB
7227 if (is_evex_encoding (&i.tm)
7228 && !cpu_arch_flags.bitfield.cpuavx512vl)
6c0946d0 7229 {
b9915cbc
JB
7230 if (i.tm.operand_types[op].bitfield.ymmword)
7231 i.tm.operand_types[op].bitfield.xmmword = 0;
7232 if (i.tm.operand_types[op].bitfield.zmmword)
7233 i.tm.operand_types[op].bitfield.ymmword = 0;
7234 if (!i.tm.opcode_modifier.evex
7235 || i.tm.opcode_modifier.evex == EVEXDYN)
7236 i.tm.opcode_modifier.evex = EVEX512;
7237 }
6c0946d0 7238
b9915cbc
JB
7239 if (i.tm.operand_types[op].bitfield.xmmword
7240 + i.tm.operand_types[op].bitfield.ymmword
7241 + i.tm.operand_types[op].bitfield.zmmword < 2)
7242 continue;
6c0946d0 7243
b9915cbc
JB
7244 /* Any properly sized operand disambiguates the insn. */
7245 if (i.types[op].bitfield.xmmword
7246 || i.types[op].bitfield.ymmword
7247 || i.types[op].bitfield.zmmword)
7248 {
7249 suffixes &= ~(7 << 6);
7250 evex = 0;
7251 break;
7252 }
6c0946d0 7253
b9915cbc
JB
7254 if ((i.flags[op] & Operand_Mem)
7255 && i.tm.operand_types[op].bitfield.unspecified)
7256 {
7257 if (i.tm.operand_types[op].bitfield.xmmword)
7258 suffixes |= 1 << 6;
7259 if (i.tm.operand_types[op].bitfield.ymmword)
7260 suffixes |= 1 << 7;
7261 if (i.tm.operand_types[op].bitfield.zmmword)
7262 suffixes |= 1 << 8;
7263 if (is_evex_encoding (&i.tm))
7264 evex = EVEX512;
6c0946d0
JB
7265 }
7266 }
7267 }
7268
7269 /* Are multiple suffixes / operand sizes allowed? */
c006a730 7270 if (suffixes & (suffixes - 1))
9306ca4a 7271 {
873494c8 7272 if (intel_syntax
3cd7f3e3 7273 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8 7274 || operand_check == check_error))
9306ca4a 7275 {
c006a730 7276 as_bad (_("ambiguous operand size for `%s'"), i.tm.name);
9306ca4a
JB
7277 return 0;
7278 }
c006a730 7279 if (operand_check == check_error)
9306ca4a 7280 {
c006a730
JB
7281 as_bad (_("no instruction mnemonic suffix given and "
7282 "no register operands; can't size `%s'"), i.tm.name);
9306ca4a
JB
7283 return 0;
7284 }
c006a730 7285 if (operand_check == check_warning)
873494c8
JB
7286 as_warn (_("%s; using default for `%s'"),
7287 intel_syntax
7288 ? _("ambiguous operand size")
7289 : _("no instruction mnemonic suffix given and "
7290 "no register operands"),
7291 i.tm.name);
c006a730
JB
7292
7293 if (i.tm.opcode_modifier.floatmf)
7294 i.suffix = SHORT_MNEM_SUFFIX;
389d00a5 7295 else if (is_movx)
65fca059 7296 /* handled below */;
6c0946d0
JB
7297 else if (evex)
7298 i.tm.opcode_modifier.evex = evex;
c006a730
JB
7299 else if (flag_code == CODE_16BIT)
7300 i.suffix = WORD_MNEM_SUFFIX;
1a035124 7301 else if (!i.tm.opcode_modifier.no_lsuf)
c006a730 7302 i.suffix = LONG_MNEM_SUFFIX;
1a035124
JB
7303 else
7304 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a 7305 }
29b0f896 7306 }
252b5132 7307
389d00a5 7308 if (is_movx)
65fca059
JB
7309 {
7310 /* In Intel syntax, movsx/movzx must have a "suffix" (checked above).
7311 In AT&T syntax, if there is no suffix (warned about above), the default
7312 will be byte extension. */
7313 if (i.tm.opcode_modifier.w && i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
7314 i.tm.base_opcode |= 1;
7315
7316 /* For further processing, the suffix should represent the destination
7317 (register). This is already the case when one was used with
7318 mov[sz][bw]*, but we need to replace it for mov[sz]x, or if there was
7319 no suffix to begin with. */
7320 if (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63 || !i.suffix)
7321 {
7322 if (i.types[1].bitfield.word)
7323 i.suffix = WORD_MNEM_SUFFIX;
7324 else if (i.types[1].bitfield.qword)
7325 i.suffix = QWORD_MNEM_SUFFIX;
7326 else
7327 i.suffix = LONG_MNEM_SUFFIX;
7328
7329 i.tm.opcode_modifier.w = 0;
7330 }
7331 }
7332
50128d0c
JB
7333 if (!i.tm.opcode_modifier.modrm && i.reg_operands && i.tm.operands < 3)
7334 i.short_form = (i.tm.operand_types[0].bitfield.class == Reg)
7335 != (i.tm.operand_types[1].bitfield.class == Reg);
7336
d2224064
JB
7337 /* Change the opcode based on the operand size given by i.suffix. */
7338 switch (i.suffix)
29b0f896 7339 {
d2224064
JB
7340 /* Size floating point instruction. */
7341 case LONG_MNEM_SUFFIX:
7342 if (i.tm.opcode_modifier.floatmf)
7343 {
7344 i.tm.base_opcode ^= 4;
7345 break;
7346 }
7347 /* fall through */
7348 case WORD_MNEM_SUFFIX:
7349 case QWORD_MNEM_SUFFIX:
29b0f896 7350 /* It's not a byte, select word/dword operation. */
40fb9820 7351 if (i.tm.opcode_modifier.w)
29b0f896 7352 {
50128d0c 7353 if (i.short_form)
29b0f896
AM
7354 i.tm.base_opcode |= 8;
7355 else
7356 i.tm.base_opcode |= 1;
7357 }
d2224064
JB
7358 /* fall through */
7359 case SHORT_MNEM_SUFFIX:
29b0f896
AM
7360 /* Now select between word & dword operations via the operand
7361 size prefix, except for instructions that will ignore this
7362 prefix anyway. */
c8f8eebc 7363 if (i.suffix != QWORD_MNEM_SUFFIX
3cd7f3e3 7364 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
c8f8eebc
JB
7365 && !i.tm.opcode_modifier.floatmf
7366 && !is_any_vex_encoding (&i.tm)
7367 && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
7368 || (flag_code == CODE_64BIT
7369 && i.tm.opcode_modifier.jump == JUMP_BYTE)))
24eab124
AM
7370 {
7371 unsigned int prefix = DATA_PREFIX_OPCODE;
543613e9 7372
0cfa3eb3 7373 if (i.tm.opcode_modifier.jump == JUMP_BYTE) /* jcxz, loop */
29b0f896 7374 prefix = ADDR_PREFIX_OPCODE;
252b5132 7375
29b0f896
AM
7376 if (!add_prefix (prefix))
7377 return 0;
24eab124 7378 }
252b5132 7379
29b0f896
AM
7380 /* Set mode64 for an operand. */
7381 if (i.suffix == QWORD_MNEM_SUFFIX
9146926a 7382 && flag_code == CODE_64BIT
d2224064 7383 && !i.tm.opcode_modifier.norex64
4ed21b58 7384 && !i.tm.opcode_modifier.vexw
46e883c5 7385 /* Special case for xchg %rax,%rax. It is NOP and doesn't
d2224064
JB
7386 need rex64. */
7387 && ! (i.operands == 2
7388 && i.tm.base_opcode == 0x90
7389 && i.tm.extension_opcode == None
75e5731b
JB
7390 && i.types[0].bitfield.instance == Accum
7391 && i.types[0].bitfield.qword
7392 && i.types[1].bitfield.instance == Accum
7393 && i.types[1].bitfield.qword))
d2224064 7394 i.rex |= REX_W;
3e73aa7c 7395
d2224064 7396 break;
8bbb3ad8
JB
7397
7398 case 0:
f9a6a8f0 7399 /* Select word/dword/qword operation with explicit data sizing prefix
8bbb3ad8
JB
7400 when there are no suitable register operands. */
7401 if (i.tm.opcode_modifier.w
7402 && (i.prefix[DATA_PREFIX] || (i.prefix[REX_PREFIX] & REX_W))
7403 && (!i.reg_operands
7404 || (i.reg_operands == 1
7405 /* ShiftCount */
7406 && (i.tm.operand_types[0].bitfield.instance == RegC
7407 /* InOutPortReg */
7408 || i.tm.operand_types[0].bitfield.instance == RegD
7409 || i.tm.operand_types[1].bitfield.instance == RegD
7410 /* CRC32 */
8b65b895 7411 || is_crc32))))
8bbb3ad8
JB
7412 i.tm.base_opcode |= 1;
7413 break;
29b0f896 7414 }
7ecd2f8b 7415
c8f8eebc 7416 if (i.tm.opcode_modifier.addrprefixopreg)
c0a30a9f 7417 {
c8f8eebc
JB
7418 gas_assert (!i.suffix);
7419 gas_assert (i.reg_operands);
c0a30a9f 7420
c8f8eebc
JB
7421 if (i.tm.operand_types[0].bitfield.instance == Accum
7422 || i.operands == 1)
7423 {
7424 /* The address size override prefix changes the size of the
7425 first operand. */
7426 if (flag_code == CODE_64BIT
7427 && i.op[0].regs->reg_type.bitfield.word)
7428 {
7429 as_bad (_("16-bit addressing unavailable for `%s'"),
7430 i.tm.name);
7431 return 0;
7432 }
7433
7434 if ((flag_code == CODE_32BIT
7435 ? i.op[0].regs->reg_type.bitfield.word
7436 : i.op[0].regs->reg_type.bitfield.dword)
7437 && !add_prefix (ADDR_PREFIX_OPCODE))
7438 return 0;
7439 }
c0a30a9f
L
7440 else
7441 {
c8f8eebc
JB
7442 /* Check invalid register operand when the address size override
7443 prefix changes the size of register operands. */
7444 unsigned int op;
7445 enum { need_word, need_dword, need_qword } need;
7446
27f13469 7447 /* Check the register operand for the address size prefix if
b3a3496f 7448 the memory operand has no real registers, like symbol, DISP
829f3fe1 7449 or bogus (x32-only) symbol(%rip) when symbol(%eip) is meant. */
27f13469
L
7450 if (i.mem_operands == 1
7451 && i.reg_operands == 1
7452 && i.operands == 2
27f13469 7453 && i.types[1].bitfield.class == Reg
b3a3496f
L
7454 && (flag_code == CODE_32BIT
7455 ? i.op[1].regs->reg_type.bitfield.word
7456 : i.op[1].regs->reg_type.bitfield.dword)
7457 && ((i.base_reg == NULL && i.index_reg == NULL)
829f3fe1
JB
7458#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
7459 || (x86_elf_abi == X86_64_X32_ABI
7460 && i.base_reg
b3a3496f
L
7461 && i.base_reg->reg_num == RegIP
7462 && i.base_reg->reg_type.bitfield.qword))
829f3fe1
JB
7463#else
7464 || 0)
7465#endif
27f13469
L
7466 && !add_prefix (ADDR_PREFIX_OPCODE))
7467 return 0;
7468
c8f8eebc
JB
7469 if (flag_code == CODE_32BIT)
7470 need = i.prefix[ADDR_PREFIX] ? need_word : need_dword;
7471 else if (i.prefix[ADDR_PREFIX])
c0a30a9f
L
7472 need = need_dword;
7473 else
7474 need = flag_code == CODE_64BIT ? need_qword : need_word;
c0a30a9f 7475
c8f8eebc
JB
7476 for (op = 0; op < i.operands; op++)
7477 {
7478 if (i.types[op].bitfield.class != Reg)
7479 continue;
7480
7481 switch (need)
7482 {
7483 case need_word:
7484 if (i.op[op].regs->reg_type.bitfield.word)
7485 continue;
7486 break;
7487 case need_dword:
7488 if (i.op[op].regs->reg_type.bitfield.dword)
7489 continue;
7490 break;
7491 case need_qword:
7492 if (i.op[op].regs->reg_type.bitfield.qword)
7493 continue;
7494 break;
7495 }
7496
7497 as_bad (_("invalid register operand size for `%s'"),
7498 i.tm.name);
7499 return 0;
7500 }
7501 }
c0a30a9f
L
7502 }
7503
29b0f896
AM
7504 return 1;
7505}
3e73aa7c 7506
29b0f896 7507static int
543613e9 7508check_byte_reg (void)
29b0f896
AM
7509{
7510 int op;
543613e9 7511
29b0f896
AM
7512 for (op = i.operands; --op >= 0;)
7513 {
dc821c5f 7514 /* Skip non-register operands. */
bab6aec1 7515 if (i.types[op].bitfield.class != Reg)
dc821c5f
JB
7516 continue;
7517
29b0f896
AM
7518 /* If this is an eight bit register, it's OK. If it's the 16 or
7519 32 bit version of an eight bit register, we will just use the
7520 low portion, and that's OK too. */
dc821c5f 7521 if (i.types[op].bitfield.byte)
29b0f896
AM
7522 continue;
7523
5a819eb9 7524 /* I/O port address operands are OK too. */
75e5731b
JB
7525 if (i.tm.operand_types[op].bitfield.instance == RegD
7526 && i.tm.operand_types[op].bitfield.word)
5a819eb9
JB
7527 continue;
7528
9706160a 7529 /* crc32 only wants its source operand checked here. */
389d00a5
JB
7530 if (i.tm.base_opcode == 0xf0
7531 && i.tm.opcode_modifier.opcodespace == SPACE_0F38
8b65b895
L
7532 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2
7533 && op != 0)
9344ff29
L
7534 continue;
7535
29b0f896 7536 /* Any other register is bad. */
73c76375
JB
7537 as_bad (_("`%s%s' not allowed with `%s%c'"),
7538 register_prefix, i.op[op].regs->reg_name,
7539 i.tm.name, i.suffix);
7540 return 0;
29b0f896
AM
7541 }
7542 return 1;
7543}
7544
7545static int
e3bb37b5 7546check_long_reg (void)
29b0f896
AM
7547{
7548 int op;
7549
7550 for (op = i.operands; --op >= 0;)
dc821c5f 7551 /* Skip non-register operands. */
bab6aec1 7552 if (i.types[op].bitfield.class != Reg)
dc821c5f 7553 continue;
29b0f896
AM
7554 /* Reject eight bit registers, except where the template requires
7555 them. (eg. movzb) */
dc821c5f 7556 else if (i.types[op].bitfield.byte
bab6aec1 7557 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7558 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7559 && (i.tm.operand_types[op].bitfield.word
7560 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7561 {
a540244d
L
7562 as_bad (_("`%s%s' not allowed with `%s%c'"),
7563 register_prefix,
29b0f896
AM
7564 i.op[op].regs->reg_name,
7565 i.tm.name,
7566 i.suffix);
7567 return 0;
7568 }
be4c5e58
L
7569 /* Error if the e prefix on a general reg is missing. */
7570 else if (i.types[op].bitfield.word
bab6aec1 7571 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7572 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7573 && i.tm.operand_types[op].bitfield.dword)
29b0f896 7574 {
be4c5e58
L
7575 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7576 register_prefix, i.op[op].regs->reg_name,
7577 i.suffix);
7578 return 0;
252b5132 7579 }
e4630f71 7580 /* Warn if the r prefix on a general reg is present. */
dc821c5f 7581 else if (i.types[op].bitfield.qword
bab6aec1 7582 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7583 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7584 && i.tm.operand_types[op].bitfield.dword)
252b5132 7585 {
1cb0ab18
JB
7586 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7587 register_prefix, i.op[op].regs->reg_name, i.suffix);
7588 return 0;
29b0f896
AM
7589 }
7590 return 1;
7591}
252b5132 7592
29b0f896 7593static int
e3bb37b5 7594check_qword_reg (void)
29b0f896
AM
7595{
7596 int op;
252b5132 7597
29b0f896 7598 for (op = i.operands; --op >= 0; )
dc821c5f 7599 /* Skip non-register operands. */
bab6aec1 7600 if (i.types[op].bitfield.class != Reg)
dc821c5f 7601 continue;
29b0f896
AM
7602 /* Reject eight bit registers, except where the template requires
7603 them. (eg. movzb) */
dc821c5f 7604 else if (i.types[op].bitfield.byte
bab6aec1 7605 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7606 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7607 && (i.tm.operand_types[op].bitfield.word
7608 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7609 {
a540244d
L
7610 as_bad (_("`%s%s' not allowed with `%s%c'"),
7611 register_prefix,
29b0f896
AM
7612 i.op[op].regs->reg_name,
7613 i.tm.name,
7614 i.suffix);
7615 return 0;
7616 }
e4630f71 7617 /* Warn if the r prefix on a general reg is missing. */
dc821c5f
JB
7618 else if ((i.types[op].bitfield.word
7619 || i.types[op].bitfield.dword)
bab6aec1 7620 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7621 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7622 && i.tm.operand_types[op].bitfield.qword)
29b0f896
AM
7623 {
7624 /* Prohibit these changes in the 64bit mode, since the
7625 lowering is more complicated. */
1cb0ab18
JB
7626 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7627 register_prefix, i.op[op].regs->reg_name, i.suffix);
7628 return 0;
252b5132 7629 }
29b0f896
AM
7630 return 1;
7631}
252b5132 7632
29b0f896 7633static int
e3bb37b5 7634check_word_reg (void)
29b0f896
AM
7635{
7636 int op;
7637 for (op = i.operands; --op >= 0;)
dc821c5f 7638 /* Skip non-register operands. */
bab6aec1 7639 if (i.types[op].bitfield.class != Reg)
dc821c5f 7640 continue;
29b0f896
AM
7641 /* Reject eight bit registers, except where the template requires
7642 them. (eg. movzb) */
dc821c5f 7643 else if (i.types[op].bitfield.byte
bab6aec1 7644 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7645 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7646 && (i.tm.operand_types[op].bitfield.word
7647 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7648 {
a540244d
L
7649 as_bad (_("`%s%s' not allowed with `%s%c'"),
7650 register_prefix,
29b0f896
AM
7651 i.op[op].regs->reg_name,
7652 i.tm.name,
7653 i.suffix);
7654 return 0;
7655 }
9706160a
JB
7656 /* Error if the e or r prefix on a general reg is present. */
7657 else if ((i.types[op].bitfield.dword
dc821c5f 7658 || i.types[op].bitfield.qword)
bab6aec1 7659 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7660 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7661 && i.tm.operand_types[op].bitfield.word)
252b5132 7662 {
9706160a
JB
7663 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7664 register_prefix, i.op[op].regs->reg_name,
7665 i.suffix);
7666 return 0;
29b0f896
AM
7667 }
7668 return 1;
7669}
252b5132 7670
29b0f896 7671static int
40fb9820 7672update_imm (unsigned int j)
29b0f896 7673{
bc0844ae 7674 i386_operand_type overlap = i.types[j];
40fb9820
L
7675 if ((overlap.bitfield.imm8
7676 || overlap.bitfield.imm8s
7677 || overlap.bitfield.imm16
7678 || overlap.bitfield.imm32
7679 || overlap.bitfield.imm32s
7680 || overlap.bitfield.imm64)
0dfbf9d7
L
7681 && !operand_type_equal (&overlap, &imm8)
7682 && !operand_type_equal (&overlap, &imm8s)
7683 && !operand_type_equal (&overlap, &imm16)
7684 && !operand_type_equal (&overlap, &imm32)
7685 && !operand_type_equal (&overlap, &imm32s)
7686 && !operand_type_equal (&overlap, &imm64))
29b0f896
AM
7687 {
7688 if (i.suffix)
7689 {
40fb9820
L
7690 i386_operand_type temp;
7691
0dfbf9d7 7692 operand_type_set (&temp, 0);
7ab9ffdd 7693 if (i.suffix == BYTE_MNEM_SUFFIX)
40fb9820
L
7694 {
7695 temp.bitfield.imm8 = overlap.bitfield.imm8;
7696 temp.bitfield.imm8s = overlap.bitfield.imm8s;
7697 }
7698 else if (i.suffix == WORD_MNEM_SUFFIX)
7699 temp.bitfield.imm16 = overlap.bitfield.imm16;
7700 else if (i.suffix == QWORD_MNEM_SUFFIX)
7701 {
7702 temp.bitfield.imm64 = overlap.bitfield.imm64;
7703 temp.bitfield.imm32s = overlap.bitfield.imm32s;
7704 }
7705 else
7706 temp.bitfield.imm32 = overlap.bitfield.imm32;
7707 overlap = temp;
29b0f896 7708 }
0dfbf9d7
L
7709 else if (operand_type_equal (&overlap, &imm16_32_32s)
7710 || operand_type_equal (&overlap, &imm16_32)
7711 || operand_type_equal (&overlap, &imm16_32s))
29b0f896 7712 {
40fb9820 7713 if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
65da13b5 7714 overlap = imm16;
40fb9820 7715 else
65da13b5 7716 overlap = imm32s;
29b0f896 7717 }
8bbb3ad8
JB
7718 else if (i.prefix[REX_PREFIX] & REX_W)
7719 overlap = operand_type_and (overlap, imm32s);
7720 else if (i.prefix[DATA_PREFIX])
7721 overlap = operand_type_and (overlap,
7722 flag_code != CODE_16BIT ? imm16 : imm32);
0dfbf9d7
L
7723 if (!operand_type_equal (&overlap, &imm8)
7724 && !operand_type_equal (&overlap, &imm8s)
7725 && !operand_type_equal (&overlap, &imm16)
7726 && !operand_type_equal (&overlap, &imm32)
7727 && !operand_type_equal (&overlap, &imm32s)
7728 && !operand_type_equal (&overlap, &imm64))
29b0f896 7729 {
4eed87de
AM
7730 as_bad (_("no instruction mnemonic suffix given; "
7731 "can't determine immediate size"));
29b0f896
AM
7732 return 0;
7733 }
7734 }
40fb9820 7735 i.types[j] = overlap;
29b0f896 7736
40fb9820
L
7737 return 1;
7738}
7739
7740static int
7741finalize_imm (void)
7742{
bc0844ae 7743 unsigned int j, n;
29b0f896 7744
bc0844ae
L
7745 /* Update the first 2 immediate operands. */
7746 n = i.operands > 2 ? 2 : i.operands;
7747 if (n)
7748 {
7749 for (j = 0; j < n; j++)
7750 if (update_imm (j) == 0)
7751 return 0;
40fb9820 7752
bc0844ae
L
7753 /* The 3rd operand can't be immediate operand. */
7754 gas_assert (operand_type_check (i.types[2], imm) == 0);
7755 }
29b0f896
AM
7756
7757 return 1;
7758}
7759
7760static int
e3bb37b5 7761process_operands (void)
29b0f896
AM
7762{
7763 /* Default segment register this instruction will use for memory
7764 accesses. 0 means unknown. This is only for optimizing out
7765 unnecessary segment overrides. */
5e042380 7766 const reg_entry *default_seg = NULL;
29b0f896 7767
a5aeccd9
JB
7768 if (i.tm.opcode_modifier.sse2avx)
7769 {
7770 /* Legacy encoded insns allow explicit REX prefixes, so these prefixes
7771 need converting. */
7772 i.rex |= i.prefix[REX_PREFIX] & (REX_W | REX_R | REX_X | REX_B);
7773 i.prefix[REX_PREFIX] = 0;
7774 i.rex_encoding = 0;
7775 }
c423d21a
JB
7776 /* ImmExt should be processed after SSE2AVX. */
7777 else if (i.tm.opcode_modifier.immext)
7778 process_immext ();
a5aeccd9 7779
2426c15f 7780 if (i.tm.opcode_modifier.sse2avx && i.tm.opcode_modifier.vexvvvv)
29b0f896 7781 {
91d6fa6a
NC
7782 unsigned int dupl = i.operands;
7783 unsigned int dest = dupl - 1;
9fcfb3d7
L
7784 unsigned int j;
7785
c0f3af97 7786 /* The destination must be an xmm register. */
9c2799c2 7787 gas_assert (i.reg_operands
91d6fa6a 7788 && MAX_OPERANDS > dupl
7ab9ffdd 7789 && operand_type_equal (&i.types[dest], &regxmm));
c0f3af97 7790
75e5731b 7791 if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 7792 && i.tm.operand_types[0].bitfield.xmmword)
e2ec9d29 7793 {
8cd7925b 7794 if (i.tm.opcode_modifier.vexsources == VEX3SOURCES)
c0f3af97
L
7795 {
7796 /* Keep xmm0 for instructions with VEX prefix and 3
7797 sources. */
75e5731b 7798 i.tm.operand_types[0].bitfield.instance = InstanceNone;
3528c362 7799 i.tm.operand_types[0].bitfield.class = RegSIMD;
c0f3af97
L
7800 goto duplicate;
7801 }
e2ec9d29 7802 else
c0f3af97
L
7803 {
7804 /* We remove the first xmm0 and keep the number of
7805 operands unchanged, which in fact duplicates the
7806 destination. */
7807 for (j = 1; j < i.operands; j++)
7808 {
7809 i.op[j - 1] = i.op[j];
7810 i.types[j - 1] = i.types[j];
7811 i.tm.operand_types[j - 1] = i.tm.operand_types[j];
8dc0818e 7812 i.flags[j - 1] = i.flags[j];
c0f3af97
L
7813 }
7814 }
7815 }
7816 else if (i.tm.opcode_modifier.implicit1stxmm0)
7ab9ffdd 7817 {
91d6fa6a 7818 gas_assert ((MAX_OPERANDS - 1) > dupl
8cd7925b
L
7819 && (i.tm.opcode_modifier.vexsources
7820 == VEX3SOURCES));
c0f3af97
L
7821
7822 /* Add the implicit xmm0 for instructions with VEX prefix
7823 and 3 sources. */
7824 for (j = i.operands; j > 0; j--)
7825 {
7826 i.op[j] = i.op[j - 1];
7827 i.types[j] = i.types[j - 1];
7828 i.tm.operand_types[j] = i.tm.operand_types[j - 1];
8dc0818e 7829 i.flags[j] = i.flags[j - 1];
c0f3af97
L
7830 }
7831 i.op[0].regs
629310ab 7832 = (const reg_entry *) str_hash_find (reg_hash, "xmm0");
7ab9ffdd 7833 i.types[0] = regxmm;
c0f3af97
L
7834 i.tm.operand_types[0] = regxmm;
7835
7836 i.operands += 2;
7837 i.reg_operands += 2;
7838 i.tm.operands += 2;
7839
91d6fa6a 7840 dupl++;
c0f3af97 7841 dest++;
91d6fa6a
NC
7842 i.op[dupl] = i.op[dest];
7843 i.types[dupl] = i.types[dest];
7844 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 7845 i.flags[dupl] = i.flags[dest];
e2ec9d29 7846 }
c0f3af97
L
7847 else
7848 {
dc1e8a47 7849 duplicate:
c0f3af97
L
7850 i.operands++;
7851 i.reg_operands++;
7852 i.tm.operands++;
7853
91d6fa6a
NC
7854 i.op[dupl] = i.op[dest];
7855 i.types[dupl] = i.types[dest];
7856 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 7857 i.flags[dupl] = i.flags[dest];
c0f3af97
L
7858 }
7859
7860 if (i.tm.opcode_modifier.immext)
7861 process_immext ();
7862 }
75e5731b 7863 else if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 7864 && i.tm.operand_types[0].bitfield.xmmword)
c0f3af97
L
7865 {
7866 unsigned int j;
7867
9fcfb3d7
L
7868 for (j = 1; j < i.operands; j++)
7869 {
7870 i.op[j - 1] = i.op[j];
7871 i.types[j - 1] = i.types[j];
7872
7873 /* We need to adjust fields in i.tm since they are used by
7874 build_modrm_byte. */
7875 i.tm.operand_types [j - 1] = i.tm.operand_types [j];
8dc0818e
JB
7876
7877 i.flags[j - 1] = i.flags[j];
9fcfb3d7
L
7878 }
7879
e2ec9d29
L
7880 i.operands--;
7881 i.reg_operands--;
e2ec9d29
L
7882 i.tm.operands--;
7883 }
920d2ddc
IT
7884 else if (i.tm.opcode_modifier.implicitquadgroup)
7885 {
a477a8c4
JB
7886 unsigned int regnum, first_reg_in_group, last_reg_in_group;
7887
920d2ddc 7888 /* The second operand must be {x,y,z}mmN, where N is a multiple of 4. */
3528c362 7889 gas_assert (i.operands >= 2 && i.types[1].bitfield.class == RegSIMD);
a477a8c4
JB
7890 regnum = register_number (i.op[1].regs);
7891 first_reg_in_group = regnum & ~3;
7892 last_reg_in_group = first_reg_in_group + 3;
7893 if (regnum != first_reg_in_group)
7894 as_warn (_("source register `%s%s' implicitly denotes"
7895 " `%s%.3s%u' to `%s%.3s%u' source group in `%s'"),
7896 register_prefix, i.op[1].regs->reg_name,
7897 register_prefix, i.op[1].regs->reg_name, first_reg_in_group,
7898 register_prefix, i.op[1].regs->reg_name, last_reg_in_group,
7899 i.tm.name);
7900 }
e2ec9d29
L
7901 else if (i.tm.opcode_modifier.regkludge)
7902 {
7903 /* The imul $imm, %reg instruction is converted into
7904 imul $imm, %reg, %reg, and the clr %reg instruction
7905 is converted into xor %reg, %reg. */
7906
7907 unsigned int first_reg_op;
7908
7909 if (operand_type_check (i.types[0], reg))
7910 first_reg_op = 0;
7911 else
7912 first_reg_op = 1;
7913 /* Pretend we saw the extra register operand. */
9c2799c2 7914 gas_assert (i.reg_operands == 1
7ab9ffdd 7915 && i.op[first_reg_op + 1].regs == 0);
e2ec9d29
L
7916 i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
7917 i.types[first_reg_op + 1] = i.types[first_reg_op];
7918 i.operands++;
7919 i.reg_operands++;
29b0f896
AM
7920 }
7921
85b80b0f 7922 if (i.tm.opcode_modifier.modrm)
29b0f896
AM
7923 {
7924 /* The opcode is completed (modulo i.tm.extension_opcode which
52271982
AM
7925 must be put into the modrm byte). Now, we make the modrm and
7926 index base bytes based on all the info we've collected. */
29b0f896
AM
7927
7928 default_seg = build_modrm_byte ();
7929 }
00cee14f 7930 else if (i.types[0].bitfield.class == SReg)
85b80b0f
JB
7931 {
7932 if (flag_code != CODE_64BIT
7933 ? i.tm.base_opcode == POP_SEG_SHORT
7934 && i.op[0].regs->reg_num == 1
389d00a5 7935 : (i.tm.base_opcode | 1) == (POP_SEG386_SHORT & 0xff)
85b80b0f
JB
7936 && i.op[0].regs->reg_num < 4)
7937 {
7938 as_bad (_("you can't `%s %s%s'"),
7939 i.tm.name, register_prefix, i.op[0].regs->reg_name);
7940 return 0;
7941 }
389d00a5
JB
7942 if (i.op[0].regs->reg_num > 3
7943 && i.tm.opcode_modifier.opcodespace == SPACE_BASE )
85b80b0f 7944 {
389d00a5
JB
7945 i.tm.base_opcode ^= (POP_SEG_SHORT ^ POP_SEG386_SHORT) & 0xff;
7946 i.tm.opcode_modifier.opcodespace = SPACE_0F;
85b80b0f
JB
7947 }
7948 i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
7949 }
389d00a5
JB
7950 else if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
7951 && (i.tm.base_opcode & ~3) == MOV_AX_DISP32)
29b0f896 7952 {
5e042380 7953 default_seg = reg_ds;
29b0f896 7954 }
40fb9820 7955 else if (i.tm.opcode_modifier.isstring)
29b0f896
AM
7956 {
7957 /* For the string instructions that allow a segment override
7958 on one of their operands, the default segment is ds. */
5e042380 7959 default_seg = reg_ds;
29b0f896 7960 }
50128d0c 7961 else if (i.short_form)
85b80b0f
JB
7962 {
7963 /* The register or float register operand is in operand
7964 0 or 1. */
bab6aec1 7965 unsigned int op = i.tm.operand_types[0].bitfield.class != Reg;
85b80b0f
JB
7966
7967 /* Register goes in low 3 bits of opcode. */
7968 i.tm.base_opcode |= i.op[op].regs->reg_num;
7969 if ((i.op[op].regs->reg_flags & RegRex) != 0)
7970 i.rex |= REX_B;
7971 if (!quiet_warnings && i.tm.opcode_modifier.ugh)
7972 {
7973 /* Warn about some common errors, but press on regardless.
7974 The first case can be generated by gcc (<= 2.8.1). */
7975 if (i.operands == 2)
7976 {
7977 /* Reversed arguments on faddp, fsubp, etc. */
7978 as_warn (_("translating to `%s %s%s,%s%s'"), i.tm.name,
7979 register_prefix, i.op[!intel_syntax].regs->reg_name,
7980 register_prefix, i.op[intel_syntax].regs->reg_name);
7981 }
7982 else
7983 {
7984 /* Extraneous `l' suffix on fp insn. */
7985 as_warn (_("translating to `%s %s%s'"), i.tm.name,
7986 register_prefix, i.op[0].regs->reg_name);
7987 }
7988 }
7989 }
29b0f896 7990
514a8bb0 7991 if ((i.seg[0] || i.prefix[SEG_PREFIX])
514a8bb0 7992 && i.tm.base_opcode == 0x8d /* lea */
35648716 7993 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
514a8bb0 7994 && !is_any_vex_encoding(&i.tm))
92334ad2
JB
7995 {
7996 if (!quiet_warnings)
7997 as_warn (_("segment override on `%s' is ineffectual"), i.tm.name);
7998 if (optimize)
7999 {
8000 i.seg[0] = NULL;
8001 i.prefix[SEG_PREFIX] = 0;
8002 }
8003 }
52271982
AM
8004
8005 /* If a segment was explicitly specified, and the specified segment
b6773884
JB
8006 is neither the default nor the one already recorded from a prefix,
8007 use an opcode prefix to select it. If we never figured out what
8008 the default segment is, then default_seg will be zero at this
8009 point, and the specified segment prefix will always be used. */
8010 if (i.seg[0]
8011 && i.seg[0] != default_seg
5e042380 8012 && i386_seg_prefixes[i.seg[0]->reg_num] != i.prefix[SEG_PREFIX])
29b0f896 8013 {
5e042380 8014 if (!add_prefix (i386_seg_prefixes[i.seg[0]->reg_num]))
29b0f896
AM
8015 return 0;
8016 }
8017 return 1;
8018}
8019
a5aeccd9 8020static INLINE void set_rex_vrex (const reg_entry *r, unsigned int rex_bit,
5b7c81bd 8021 bool do_sse2avx)
a5aeccd9
JB
8022{
8023 if (r->reg_flags & RegRex)
8024 {
8025 if (i.rex & rex_bit)
8026 as_bad (_("same type of prefix used twice"));
8027 i.rex |= rex_bit;
8028 }
8029 else if (do_sse2avx && (i.rex & rex_bit) && i.vex.register_specifier)
8030 {
8031 gas_assert (i.vex.register_specifier == r);
8032 i.vex.register_specifier += 8;
8033 }
8034
8035 if (r->reg_flags & RegVRex)
8036 i.vrex |= rex_bit;
8037}
8038
5e042380 8039static const reg_entry *
e3bb37b5 8040build_modrm_byte (void)
29b0f896 8041{
5e042380 8042 const reg_entry *default_seg = NULL;
c0f3af97 8043 unsigned int source, dest;
8cd7925b 8044 int vex_3_sources;
c0f3af97 8045
8cd7925b 8046 vex_3_sources = i.tm.opcode_modifier.vexsources == VEX3SOURCES;
c0f3af97
L
8047 if (vex_3_sources)
8048 {
91d6fa6a 8049 unsigned int nds, reg_slot;
4c2c6516 8050 expressionS *exp;
c0f3af97 8051
6b8d3588 8052 dest = i.operands - 1;
c0f3af97 8053 nds = dest - 1;
922d8de8 8054
a683cc34 8055 /* There are 2 kinds of instructions:
bed3d976 8056 1. 5 operands: 4 register operands or 3 register operands
9d3bf266 8057 plus 1 memory operand plus one Imm4 operand, VexXDS, and
bed3d976 8058 VexW0 or VexW1. The destination must be either XMM, YMM or
43234a1e 8059 ZMM register.
bed3d976 8060 2. 4 operands: 4 register operands or 3 register operands
2f1bada2 8061 plus 1 memory operand, with VexXDS. */
922d8de8 8062 gas_assert ((i.reg_operands == 4
bed3d976
JB
8063 || (i.reg_operands == 3 && i.mem_operands == 1))
8064 && i.tm.opcode_modifier.vexvvvv == VEXXDS
dcd7e323 8065 && i.tm.opcode_modifier.vexw
3528c362 8066 && i.tm.operand_types[dest].bitfield.class == RegSIMD);
a683cc34 8067
48db9223
JB
8068 /* If VexW1 is set, the first non-immediate operand is the source and
8069 the second non-immediate one is encoded in the immediate operand. */
8070 if (i.tm.opcode_modifier.vexw == VEXW1)
8071 {
8072 source = i.imm_operands;
8073 reg_slot = i.imm_operands + 1;
8074 }
8075 else
8076 {
8077 source = i.imm_operands + 1;
8078 reg_slot = i.imm_operands;
8079 }
8080
a683cc34 8081 if (i.imm_operands == 0)
bed3d976
JB
8082 {
8083 /* When there is no immediate operand, generate an 8bit
8084 immediate operand to encode the first operand. */
8085 exp = &im_expressions[i.imm_operands++];
8086 i.op[i.operands].imms = exp;
8087 i.types[i.operands] = imm8;
8088 i.operands++;
8089
3528c362 8090 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
bed3d976
JB
8091 exp->X_op = O_constant;
8092 exp->X_add_number = register_number (i.op[reg_slot].regs) << 4;
43234a1e
L
8093 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
8094 }
922d8de8 8095 else
bed3d976 8096 {
9d3bf266
JB
8097 gas_assert (i.imm_operands == 1);
8098 gas_assert (fits_in_imm4 (i.op[0].imms->X_add_number));
8099 gas_assert (!i.tm.opcode_modifier.immext);
a683cc34 8100
9d3bf266
JB
8101 /* Turn on Imm8 again so that output_imm will generate it. */
8102 i.types[0].bitfield.imm8 = 1;
bed3d976 8103
3528c362 8104 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
9d3bf266 8105 i.op[0].imms->X_add_number
bed3d976 8106 |= register_number (i.op[reg_slot].regs) << 4;
43234a1e 8107 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
bed3d976 8108 }
a683cc34 8109
3528c362 8110 gas_assert (i.tm.operand_types[nds].bitfield.class == RegSIMD);
dae39acc 8111 i.vex.register_specifier = i.op[nds].regs;
c0f3af97
L
8112 }
8113 else
8114 source = dest = 0;
29b0f896
AM
8115
8116 /* i.reg_operands MUST be the number of real register operands;
c0f3af97
L
8117 implicit registers do not count. If there are 3 register
8118 operands, it must be a instruction with VexNDS. For a
8119 instruction with VexNDD, the destination register is encoded
8120 in VEX prefix. If there are 4 register operands, it must be
8121 a instruction with VEX prefix and 3 sources. */
7ab9ffdd
L
8122 if (i.mem_operands == 0
8123 && ((i.reg_operands == 2
2426c15f 8124 && i.tm.opcode_modifier.vexvvvv <= VEXXDS)
7ab9ffdd 8125 || (i.reg_operands == 3
2426c15f 8126 && i.tm.opcode_modifier.vexvvvv == VEXXDS)
7ab9ffdd 8127 || (i.reg_operands == 4 && vex_3_sources)))
29b0f896 8128 {
cab737b9
L
8129 switch (i.operands)
8130 {
8131 case 2:
8132 source = 0;
8133 break;
8134 case 3:
c81128dc
L
8135 /* When there are 3 operands, one of them may be immediate,
8136 which may be the first or the last operand. Otherwise,
c0f3af97
L
8137 the first operand must be shift count register (cl) or it
8138 is an instruction with VexNDS. */
9c2799c2 8139 gas_assert (i.imm_operands == 1
7ab9ffdd 8140 || (i.imm_operands == 0
2426c15f 8141 && (i.tm.opcode_modifier.vexvvvv == VEXXDS
75e5731b
JB
8142 || (i.types[0].bitfield.instance == RegC
8143 && i.types[0].bitfield.byte))));
40fb9820 8144 if (operand_type_check (i.types[0], imm)
75e5731b
JB
8145 || (i.types[0].bitfield.instance == RegC
8146 && i.types[0].bitfield.byte))
40fb9820
L
8147 source = 1;
8148 else
8149 source = 0;
cab737b9
L
8150 break;
8151 case 4:
368d64cc
L
8152 /* When there are 4 operands, the first two must be 8bit
8153 immediate operands. The source operand will be the 3rd
c0f3af97
L
8154 one.
8155
8156 For instructions with VexNDS, if the first operand
8157 an imm8, the source operand is the 2nd one. If the last
8158 operand is imm8, the source operand is the first one. */
9c2799c2 8159 gas_assert ((i.imm_operands == 2
7ab9ffdd
L
8160 && i.types[0].bitfield.imm8
8161 && i.types[1].bitfield.imm8)
2426c15f 8162 || (i.tm.opcode_modifier.vexvvvv == VEXXDS
7ab9ffdd
L
8163 && i.imm_operands == 1
8164 && (i.types[0].bitfield.imm8
cf665fee 8165 || i.types[i.operands - 1].bitfield.imm8)));
9f2670f2
L
8166 if (i.imm_operands == 2)
8167 source = 2;
8168 else
c0f3af97
L
8169 {
8170 if (i.types[0].bitfield.imm8)
8171 source = 1;
8172 else
8173 source = 0;
8174 }
c0f3af97
L
8175 break;
8176 case 5:
cf665fee
JB
8177 gas_assert (!is_evex_encoding (&i.tm));
8178 gas_assert (i.imm_operands == 1 && vex_3_sources);
cab737b9
L
8179 break;
8180 default:
8181 abort ();
8182 }
8183
c0f3af97
L
8184 if (!vex_3_sources)
8185 {
8186 dest = source + 1;
8187
2426c15f 8188 if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
c0f3af97 8189 {
43234a1e 8190 /* For instructions with VexNDS, the register-only source
c5d0745b 8191 operand must be a 32/64bit integer, XMM, YMM, ZMM, or mask
dfd69174 8192 register. It is encoded in VEX prefix. */
f12dc422
L
8193
8194 i386_operand_type op;
8195 unsigned int vvvv;
8196
c2ecccb3
L
8197 /* Swap two source operands if needed. */
8198 if (i.tm.opcode_modifier.swapsources)
f12dc422
L
8199 {
8200 vvvv = source;
8201 source = dest;
8202 }
8203 else
8204 vvvv = dest;
8205
8206 op = i.tm.operand_types[vvvv];
c0f3af97 8207 if ((dest + 1) >= i.operands
bab6aec1 8208 || ((op.bitfield.class != Reg
dc821c5f 8209 || (!op.bitfield.dword && !op.bitfield.qword))
3528c362 8210 && op.bitfield.class != RegSIMD
c0f327b8 8211 && op.bitfield.class != RegMask))
c0f3af97 8212 abort ();
f12dc422 8213 i.vex.register_specifier = i.op[vvvv].regs;
c0f3af97
L
8214 dest++;
8215 }
8216 }
29b0f896
AM
8217
8218 i.rm.mode = 3;
dfd69174
JB
8219 /* One of the register operands will be encoded in the i.rm.reg
8220 field, the other in the combined i.rm.mode and i.rm.regmem
29b0f896
AM
8221 fields. If no form of this instruction supports a memory
8222 destination operand, then we assume the source operand may
8223 sometimes be a memory operand and so we need to store the
8224 destination in the i.rm.reg field. */
dfd69174 8225 if (!i.tm.opcode_modifier.regmem
40fb9820 8226 && operand_type_check (i.tm.operand_types[dest], anymem) == 0)
29b0f896
AM
8227 {
8228 i.rm.reg = i.op[dest].regs->reg_num;
8229 i.rm.regmem = i.op[source].regs->reg_num;
a5aeccd9 8230 set_rex_vrex (i.op[dest].regs, REX_R, i.tm.opcode_modifier.sse2avx);
5b7c81bd 8231 set_rex_vrex (i.op[source].regs, REX_B, false);
29b0f896
AM
8232 }
8233 else
8234 {
8235 i.rm.reg = i.op[source].regs->reg_num;
8236 i.rm.regmem = i.op[dest].regs->reg_num;
a5aeccd9 8237 set_rex_vrex (i.op[dest].regs, REX_B, i.tm.opcode_modifier.sse2avx);
5b7c81bd 8238 set_rex_vrex (i.op[source].regs, REX_R, false);
29b0f896 8239 }
e0c7f900 8240 if (flag_code != CODE_64BIT && (i.rex & REX_R))
c4a530c5 8241 {
4a5c67ed 8242 if (i.types[!i.tm.opcode_modifier.regmem].bitfield.class != RegCR)
c4a530c5 8243 abort ();
e0c7f900 8244 i.rex &= ~REX_R;
c4a530c5
JB
8245 add_prefix (LOCK_PREFIX_OPCODE);
8246 }
29b0f896
AM
8247 }
8248 else
8249 { /* If it's not 2 reg operands... */
c0f3af97
L
8250 unsigned int mem;
8251
29b0f896
AM
8252 if (i.mem_operands)
8253 {
8254 unsigned int fake_zero_displacement = 0;
99018f42 8255 unsigned int op;
4eed87de 8256
7ab9ffdd 8257 for (op = 0; op < i.operands; op++)
8dc0818e 8258 if (i.flags[op] & Operand_Mem)
7ab9ffdd 8259 break;
7ab9ffdd 8260 gas_assert (op < i.operands);
29b0f896 8261
63112cd6 8262 if (i.tm.opcode_modifier.sib)
6c30d220 8263 {
260cd341
LC
8264 /* The index register of VSIB shouldn't be RegIZ. */
8265 if (i.tm.opcode_modifier.sib != SIBMEM
8266 && i.index_reg->reg_num == RegIZ)
6c30d220
L
8267 abort ();
8268
8269 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8270 if (!i.base_reg)
8271 {
8272 i.sib.base = NO_BASE_REGISTER;
8273 i.sib.scale = i.log2_scale_factor;
2f2be86b 8274 i.types[op] = operand_type_and_not (i.types[op], anydisp);
a775efc8 8275 i.types[op].bitfield.disp32 = 1;
6c30d220 8276 }
260cd341
LC
8277
8278 /* Since the mandatory SIB always has index register, so
8279 the code logic remains unchanged. The non-mandatory SIB
8280 without index register is allowed and will be handled
8281 later. */
8282 if (i.index_reg)
8283 {
8284 if (i.index_reg->reg_num == RegIZ)
8285 i.sib.index = NO_INDEX_REGISTER;
8286 else
8287 i.sib.index = i.index_reg->reg_num;
5b7c81bd 8288 set_rex_vrex (i.index_reg, REX_X, false);
260cd341 8289 }
6c30d220
L
8290 }
8291
5e042380 8292 default_seg = reg_ds;
29b0f896
AM
8293
8294 if (i.base_reg == 0)
8295 {
8296 i.rm.mode = 0;
8297 if (!i.disp_operands)
9bb129e8 8298 fake_zero_displacement = 1;
29b0f896
AM
8299 if (i.index_reg == 0)
8300 {
260cd341
LC
8301 /* Both check for VSIB and mandatory non-vector SIB. */
8302 gas_assert (!i.tm.opcode_modifier.sib
8303 || i.tm.opcode_modifier.sib == SIBMEM);
29b0f896 8304 /* Operand is just <disp> */
2f2be86b 8305 i.types[op] = operand_type_and_not (i.types[op], anydisp);
20f0a1fc 8306 if (flag_code == CODE_64BIT)
29b0f896
AM
8307 {
8308 /* 64bit mode overwrites the 32bit absolute
8309 addressing by RIP relative addressing and
8310 absolute addressing is encoded by one of the
8311 redundant SIB forms. */
8312 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8313 i.sib.base = NO_BASE_REGISTER;
8314 i.sib.index = NO_INDEX_REGISTER;
a775efc8 8315 i.types[op].bitfield.disp32 = 1;
20f0a1fc 8316 }
fc225355
L
8317 else if ((flag_code == CODE_16BIT)
8318 ^ (i.prefix[ADDR_PREFIX] != 0))
20f0a1fc
NC
8319 {
8320 i.rm.regmem = NO_BASE_REGISTER_16;
2f2be86b 8321 i.types[op].bitfield.disp16 = 1;
20f0a1fc
NC
8322 }
8323 else
8324 {
8325 i.rm.regmem = NO_BASE_REGISTER;
2f2be86b 8326 i.types[op].bitfield.disp32 = 1;
29b0f896
AM
8327 }
8328 }
63112cd6 8329 else if (!i.tm.opcode_modifier.sib)
29b0f896 8330 {
6c30d220 8331 /* !i.base_reg && i.index_reg */
e968fc9b 8332 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8333 i.sib.index = NO_INDEX_REGISTER;
8334 else
8335 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8336 i.sib.base = NO_BASE_REGISTER;
8337 i.sib.scale = i.log2_scale_factor;
8338 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
2f2be86b 8339 i.types[op] = operand_type_and_not (i.types[op], anydisp);
a775efc8 8340 i.types[op].bitfield.disp32 = 1;
29b0f896 8341 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8342 i.rex |= REX_X;
29b0f896
AM
8343 }
8344 }
8345 /* RIP addressing for 64bit mode. */
e968fc9b 8346 else if (i.base_reg->reg_num == RegIP)
29b0f896 8347 {
63112cd6 8348 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896 8349 i.rm.regmem = NO_BASE_REGISTER;
40fb9820
L
8350 i.types[op].bitfield.disp8 = 0;
8351 i.types[op].bitfield.disp16 = 0;
a775efc8 8352 i.types[op].bitfield.disp32 = 1;
40fb9820 8353 i.types[op].bitfield.disp64 = 0;
71903a11 8354 i.flags[op] |= Operand_PCrel;
20f0a1fc
NC
8355 if (! i.disp_operands)
8356 fake_zero_displacement = 1;
29b0f896 8357 }
dc821c5f 8358 else if (i.base_reg->reg_type.bitfield.word)
29b0f896 8359 {
63112cd6 8360 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896
AM
8361 switch (i.base_reg->reg_num)
8362 {
8363 case 3: /* (%bx) */
8364 if (i.index_reg == 0)
8365 i.rm.regmem = 7;
8366 else /* (%bx,%si) -> 0, or (%bx,%di) -> 1 */
8367 i.rm.regmem = i.index_reg->reg_num - 6;
8368 break;
8369 case 5: /* (%bp) */
5e042380 8370 default_seg = reg_ss;
29b0f896
AM
8371 if (i.index_reg == 0)
8372 {
8373 i.rm.regmem = 6;
40fb9820 8374 if (operand_type_check (i.types[op], disp) == 0)
29b0f896
AM
8375 {
8376 /* fake (%bp) into 0(%bp) */
41eb8e88 8377 if (i.disp_encoding == disp_encoding_16bit)
1a02d6b0
L
8378 i.types[op].bitfield.disp16 = 1;
8379 else
8380 i.types[op].bitfield.disp8 = 1;
252b5132 8381 fake_zero_displacement = 1;
29b0f896
AM
8382 }
8383 }
8384 else /* (%bp,%si) -> 2, or (%bp,%di) -> 3 */
8385 i.rm.regmem = i.index_reg->reg_num - 6 + 2;
8386 break;
8387 default: /* (%si) -> 4 or (%di) -> 5 */
8388 i.rm.regmem = i.base_reg->reg_num - 6 + 4;
8389 }
41eb8e88
L
8390 if (!fake_zero_displacement
8391 && !i.disp_operands
8392 && i.disp_encoding)
8393 {
8394 fake_zero_displacement = 1;
8395 if (i.disp_encoding == disp_encoding_8bit)
8396 i.types[op].bitfield.disp8 = 1;
8397 else
8398 i.types[op].bitfield.disp16 = 1;
8399 }
29b0f896
AM
8400 i.rm.mode = mode_from_disp_size (i.types[op]);
8401 }
8402 else /* i.base_reg and 32/64 bit mode */
8403 {
a9aabc23 8404 if (operand_type_check (i.types[op], disp))
40fb9820 8405 {
73053c1f
JB
8406 i.types[op].bitfield.disp16 = 0;
8407 i.types[op].bitfield.disp64 = 0;
a775efc8 8408 i.types[op].bitfield.disp32 = 1;
40fb9820 8409 }
20f0a1fc 8410
63112cd6 8411 if (!i.tm.opcode_modifier.sib)
6c30d220 8412 i.rm.regmem = i.base_reg->reg_num;
29b0f896 8413 if ((i.base_reg->reg_flags & RegRex) != 0)
161a04f6 8414 i.rex |= REX_B;
29b0f896
AM
8415 i.sib.base = i.base_reg->reg_num;
8416 /* x86-64 ignores REX prefix bit here to avoid decoder
8417 complications. */
848930b2
JB
8418 if (!(i.base_reg->reg_flags & RegRex)
8419 && (i.base_reg->reg_num == EBP_REG_NUM
8420 || i.base_reg->reg_num == ESP_REG_NUM))
5e042380 8421 default_seg = reg_ss;
848930b2 8422 if (i.base_reg->reg_num == 5 && i.disp_operands == 0)
29b0f896 8423 {
848930b2 8424 fake_zero_displacement = 1;
1a02d6b0
L
8425 if (i.disp_encoding == disp_encoding_32bit)
8426 i.types[op].bitfield.disp32 = 1;
8427 else
8428 i.types[op].bitfield.disp8 = 1;
29b0f896
AM
8429 }
8430 i.sib.scale = i.log2_scale_factor;
8431 if (i.index_reg == 0)
8432 {
260cd341
LC
8433 /* Only check for VSIB. */
8434 gas_assert (i.tm.opcode_modifier.sib != VECSIB128
8435 && i.tm.opcode_modifier.sib != VECSIB256
8436 && i.tm.opcode_modifier.sib != VECSIB512);
8437
29b0f896
AM
8438 /* <disp>(%esp) becomes two byte modrm with no index
8439 register. We've already stored the code for esp
8440 in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
8441 Any base register besides %esp will not use the
8442 extra modrm byte. */
8443 i.sib.index = NO_INDEX_REGISTER;
29b0f896 8444 }
63112cd6 8445 else if (!i.tm.opcode_modifier.sib)
29b0f896 8446 {
e968fc9b 8447 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8448 i.sib.index = NO_INDEX_REGISTER;
8449 else
8450 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8451 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8452 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8453 i.rex |= REX_X;
29b0f896 8454 }
67a4f2b7
AO
8455
8456 if (i.disp_operands
8457 && (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
8458 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL))
8459 i.rm.mode = 0;
8460 else
a501d77e
L
8461 {
8462 if (!fake_zero_displacement
8463 && !i.disp_operands
8464 && i.disp_encoding)
8465 {
8466 fake_zero_displacement = 1;
8467 if (i.disp_encoding == disp_encoding_8bit)
8468 i.types[op].bitfield.disp8 = 1;
8469 else
8470 i.types[op].bitfield.disp32 = 1;
8471 }
8472 i.rm.mode = mode_from_disp_size (i.types[op]);
8473 }
29b0f896 8474 }
252b5132 8475
29b0f896
AM
8476 if (fake_zero_displacement)
8477 {
8478 /* Fakes a zero displacement assuming that i.types[op]
8479 holds the correct displacement size. */
8480 expressionS *exp;
8481
9c2799c2 8482 gas_assert (i.op[op].disps == 0);
29b0f896
AM
8483 exp = &disp_expressions[i.disp_operands++];
8484 i.op[op].disps = exp;
8485 exp->X_op = O_constant;
8486 exp->X_add_number = 0;
8487 exp->X_add_symbol = (symbolS *) 0;
8488 exp->X_op_symbol = (symbolS *) 0;
8489 }
c0f3af97
L
8490
8491 mem = op;
29b0f896 8492 }
c0f3af97
L
8493 else
8494 mem = ~0;
252b5132 8495
8c43a48b 8496 if (i.tm.opcode_modifier.vexsources == XOP2SOURCES)
5dd85c99
SP
8497 {
8498 if (operand_type_check (i.types[0], imm))
8499 i.vex.register_specifier = NULL;
8500 else
8501 {
8502 /* VEX.vvvv encodes one of the sources when the first
8503 operand is not an immediate. */
1ef99a7b 8504 if (i.tm.opcode_modifier.vexw == VEXW0)
5dd85c99
SP
8505 i.vex.register_specifier = i.op[0].regs;
8506 else
8507 i.vex.register_specifier = i.op[1].regs;
8508 }
8509
8510 /* Destination is a XMM register encoded in the ModRM.reg
8511 and VEX.R bit. */
8512 i.rm.reg = i.op[2].regs->reg_num;
8513 if ((i.op[2].regs->reg_flags & RegRex) != 0)
8514 i.rex |= REX_R;
8515
8516 /* ModRM.rm and VEX.B encodes the other source. */
8517 if (!i.mem_operands)
8518 {
8519 i.rm.mode = 3;
8520
1ef99a7b 8521 if (i.tm.opcode_modifier.vexw == VEXW0)
5dd85c99
SP
8522 i.rm.regmem = i.op[1].regs->reg_num;
8523 else
8524 i.rm.regmem = i.op[0].regs->reg_num;
8525
8526 if ((i.op[1].regs->reg_flags & RegRex) != 0)
8527 i.rex |= REX_B;
8528 }
8529 }
2426c15f 8530 else if (i.tm.opcode_modifier.vexvvvv == VEXLWP)
f88c9eb0
SP
8531 {
8532 i.vex.register_specifier = i.op[2].regs;
8533 if (!i.mem_operands)
8534 {
8535 i.rm.mode = 3;
8536 i.rm.regmem = i.op[1].regs->reg_num;
8537 if ((i.op[1].regs->reg_flags & RegRex) != 0)
8538 i.rex |= REX_B;
8539 }
8540 }
29b0f896
AM
8541 /* Fill in i.rm.reg or i.rm.regmem field with register operand
8542 (if any) based on i.tm.extension_opcode. Again, we must be
8543 careful to make sure that segment/control/debug/test/MMX
8544 registers are coded into the i.rm.reg field. */
f88c9eb0 8545 else if (i.reg_operands)
29b0f896 8546 {
99018f42 8547 unsigned int op;
7ab9ffdd
L
8548 unsigned int vex_reg = ~0;
8549
8550 for (op = 0; op < i.operands; op++)
921eafea
L
8551 if (i.types[op].bitfield.class == Reg
8552 || i.types[op].bitfield.class == RegBND
8553 || i.types[op].bitfield.class == RegMask
8554 || i.types[op].bitfield.class == SReg
8555 || i.types[op].bitfield.class == RegCR
8556 || i.types[op].bitfield.class == RegDR
8557 || i.types[op].bitfield.class == RegTR
8558 || i.types[op].bitfield.class == RegSIMD
8559 || i.types[op].bitfield.class == RegMMX)
8560 break;
c0209578 8561
7ab9ffdd
L
8562 if (vex_3_sources)
8563 op = dest;
2426c15f 8564 else if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
7ab9ffdd
L
8565 {
8566 /* For instructions with VexNDS, the register-only
8567 source operand is encoded in VEX prefix. */
8568 gas_assert (mem != (unsigned int) ~0);
c0f3af97 8569
a93e3234 8570 if (op > mem || i.tm.cpu_flags.bitfield.cpucmpccxadd)
c0f3af97 8571 {
7ab9ffdd
L
8572 vex_reg = op++;
8573 gas_assert (op < i.operands);
c0f3af97
L
8574 }
8575 else
c0f3af97 8576 {
f12dc422
L
8577 /* Check register-only source operand when two source
8578 operands are swapped. */
8579 if (!i.tm.operand_types[op].bitfield.baseindex
8580 && i.tm.operand_types[op + 1].bitfield.baseindex)
8581 {
8582 vex_reg = op;
8583 op += 2;
8584 gas_assert (mem == (vex_reg + 1)
8585 && op < i.operands);
8586 }
8587 else
8588 {
8589 vex_reg = op + 1;
8590 gas_assert (vex_reg < i.operands);
8591 }
c0f3af97 8592 }
7ab9ffdd 8593 }
2426c15f 8594 else if (i.tm.opcode_modifier.vexvvvv == VEXNDD)
7ab9ffdd 8595 {
f12dc422 8596 /* For instructions with VexNDD, the register destination
7ab9ffdd 8597 is encoded in VEX prefix. */
f12dc422
L
8598 if (i.mem_operands == 0)
8599 {
8600 /* There is no memory operand. */
8601 gas_assert ((op + 2) == i.operands);
8602 vex_reg = op + 1;
8603 }
8604 else
8d63c93e 8605 {
ed438a93
JB
8606 /* There are only 2 non-immediate operands. */
8607 gas_assert (op < i.imm_operands + 2
8608 && i.operands == i.imm_operands + 2);
8609 vex_reg = i.imm_operands + 1;
f12dc422 8610 }
7ab9ffdd
L
8611 }
8612 else
8613 gas_assert (op < i.operands);
99018f42 8614
7ab9ffdd
L
8615 if (vex_reg != (unsigned int) ~0)
8616 {
f12dc422 8617 i386_operand_type *type = &i.tm.operand_types[vex_reg];
7ab9ffdd 8618
bab6aec1 8619 if ((type->bitfield.class != Reg
dc821c5f 8620 || (!type->bitfield.dword && !type->bitfield.qword))
3528c362 8621 && type->bitfield.class != RegSIMD
c0f327b8 8622 && type->bitfield.class != RegMask)
7ab9ffdd 8623 abort ();
f88c9eb0 8624
7ab9ffdd
L
8625 i.vex.register_specifier = i.op[vex_reg].regs;
8626 }
8627
1b9f0c97
L
8628 /* Don't set OP operand twice. */
8629 if (vex_reg != op)
7ab9ffdd 8630 {
1b9f0c97
L
8631 /* If there is an extension opcode to put here, the
8632 register number must be put into the regmem field. */
8633 if (i.tm.extension_opcode != None)
8634 {
8635 i.rm.regmem = i.op[op].regs->reg_num;
a5aeccd9
JB
8636 set_rex_vrex (i.op[op].regs, REX_B,
8637 i.tm.opcode_modifier.sse2avx);
1b9f0c97
L
8638 }
8639 else
8640 {
8641 i.rm.reg = i.op[op].regs->reg_num;
a5aeccd9
JB
8642 set_rex_vrex (i.op[op].regs, REX_R,
8643 i.tm.opcode_modifier.sse2avx);
1b9f0c97 8644 }
7ab9ffdd 8645 }
252b5132 8646
29b0f896
AM
8647 /* Now, if no memory operand has set i.rm.mode = 0, 1, 2 we
8648 must set it to 3 to indicate this is a register operand
8649 in the regmem field. */
8650 if (!i.mem_operands)
8651 i.rm.mode = 3;
8652 }
252b5132 8653
29b0f896 8654 /* Fill in i.rm.reg field with extension opcode (if any). */
c1e679ec 8655 if (i.tm.extension_opcode != None)
29b0f896
AM
8656 i.rm.reg = i.tm.extension_opcode;
8657 }
8658 return default_seg;
8659}
252b5132 8660
48ef937e
JB
8661static INLINE void
8662frag_opcode_byte (unsigned char byte)
8663{
8664 if (now_seg != absolute_section)
8665 FRAG_APPEND_1_CHAR (byte);
8666 else
8667 ++abs_section_offset;
8668}
8669
376cd056
JB
8670static unsigned int
8671flip_code16 (unsigned int code16)
8672{
8673 gas_assert (i.tm.operands == 1);
8674
8675 return !(i.prefix[REX_PREFIX] & REX_W)
8676 && (code16 ? i.tm.operand_types[0].bitfield.disp32
376cd056
JB
8677 : i.tm.operand_types[0].bitfield.disp16)
8678 ? CODE16 : 0;
8679}
8680
29b0f896 8681static void
e3bb37b5 8682output_branch (void)
29b0f896
AM
8683{
8684 char *p;
f8a5c266 8685 int size;
29b0f896
AM
8686 int code16;
8687 int prefix;
8688 relax_substateT subtype;
8689 symbolS *sym;
8690 offsetT off;
8691
48ef937e
JB
8692 if (now_seg == absolute_section)
8693 {
8694 as_bad (_("relaxable branches not supported in absolute section"));
8695 return;
8696 }
8697
f8a5c266 8698 code16 = flag_code == CODE_16BIT ? CODE16 : 0;
1a42a9fe 8699 size = i.disp_encoding > disp_encoding_8bit ? BIG : SMALL;
29b0f896
AM
8700
8701 prefix = 0;
8702 if (i.prefix[DATA_PREFIX] != 0)
252b5132 8703 {
29b0f896
AM
8704 prefix = 1;
8705 i.prefixes -= 1;
376cd056 8706 code16 ^= flip_code16(code16);
252b5132 8707 }
29b0f896
AM
8708 /* Pentium4 branch hints. */
8709 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8710 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
2f66722d 8711 {
29b0f896
AM
8712 prefix++;
8713 i.prefixes--;
8714 }
8715 if (i.prefix[REX_PREFIX] != 0)
8716 {
8717 prefix++;
8718 i.prefixes--;
2f66722d
AM
8719 }
8720
7e8b059b
L
8721 /* BND prefixed jump. */
8722 if (i.prefix[BND_PREFIX] != 0)
8723 {
6cb0a70e
JB
8724 prefix++;
8725 i.prefixes--;
7e8b059b
L
8726 }
8727
f2810fe0
JB
8728 if (i.prefixes != 0)
8729 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
29b0f896
AM
8730
8731 /* It's always a symbol; End frag & setup for relax.
8732 Make sure there is enough room in this frag for the largest
8733 instruction we may generate in md_convert_frag. This is 2
8734 bytes for the opcode and room for the prefix and largest
8735 displacement. */
8736 frag_grow (prefix + 2 + 4);
8737 /* Prefix and 1 opcode byte go in fr_fix. */
8738 p = frag_more (prefix + 1);
8739 if (i.prefix[DATA_PREFIX] != 0)
8740 *p++ = DATA_PREFIX_OPCODE;
8741 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
8742 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
8743 *p++ = i.prefix[SEG_PREFIX];
6cb0a70e
JB
8744 if (i.prefix[BND_PREFIX] != 0)
8745 *p++ = BND_PREFIX_OPCODE;
29b0f896
AM
8746 if (i.prefix[REX_PREFIX] != 0)
8747 *p++ = i.prefix[REX_PREFIX];
8748 *p = i.tm.base_opcode;
8749
8750 if ((unsigned char) *p == JUMP_PC_RELATIVE)
f8a5c266 8751 subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, size);
40fb9820 8752 else if (cpu_arch_flags.bitfield.cpui386)
f8a5c266 8753 subtype = ENCODE_RELAX_STATE (COND_JUMP, size);
29b0f896 8754 else
f8a5c266 8755 subtype = ENCODE_RELAX_STATE (COND_JUMP86, size);
29b0f896 8756 subtype |= code16;
3e73aa7c 8757
29b0f896
AM
8758 sym = i.op[0].disps->X_add_symbol;
8759 off = i.op[0].disps->X_add_number;
3e73aa7c 8760
29b0f896
AM
8761 if (i.op[0].disps->X_op != O_constant
8762 && i.op[0].disps->X_op != O_symbol)
3e73aa7c 8763 {
29b0f896
AM
8764 /* Handle complex expressions. */
8765 sym = make_expr_symbol (i.op[0].disps);
8766 off = 0;
8767 }
3e73aa7c 8768
1ef3994a
JB
8769 frag_now->tc_frag_data.code64 = flag_code == CODE_64BIT;
8770
29b0f896
AM
8771 /* 1 possible extra opcode + 4 byte displacement go in var part.
8772 Pass reloc in fr_var. */
d258b828 8773 frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
29b0f896 8774}
3e73aa7c 8775
bd7ab16b
L
8776#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8777/* Return TRUE iff PLT32 relocation should be used for branching to
8778 symbol S. */
8779
5b7c81bd 8780static bool
bd7ab16b
L
8781need_plt32_p (symbolS *s)
8782{
8783 /* PLT32 relocation is ELF only. */
8784 if (!IS_ELF)
5b7c81bd 8785 return false;
bd7ab16b 8786
a5def729
RO
8787#ifdef TE_SOLARIS
8788 /* Don't emit PLT32 relocation on Solaris: neither native linker nor
8789 krtld support it. */
5b7c81bd 8790 return false;
a5def729
RO
8791#endif
8792
bd7ab16b
L
8793 /* Since there is no need to prepare for PLT branch on x86-64, we
8794 can generate R_X86_64_PLT32, instead of R_X86_64_PC32, which can
8795 be used as a marker for 32-bit PC-relative branches. */
8796 if (!object_64bit)
5b7c81bd 8797 return false;
bd7ab16b 8798
44365e88 8799 if (s == NULL)
5b7c81bd 8800 return false;
44365e88 8801
bd7ab16b
L
8802 /* Weak or undefined symbol need PLT32 relocation. */
8803 if (S_IS_WEAK (s) || !S_IS_DEFINED (s))
5b7c81bd 8804 return true;
bd7ab16b
L
8805
8806 /* Non-global symbol doesn't need PLT32 relocation. */
8807 if (! S_IS_EXTERNAL (s))
5b7c81bd 8808 return false;
bd7ab16b
L
8809
8810 /* Other global symbols need PLT32 relocation. NB: Symbol with
8811 non-default visibilities are treated as normal global symbol
8812 so that PLT32 relocation can be used as a marker for 32-bit
8813 PC-relative branches. It is useful for linker relaxation. */
5b7c81bd 8814 return true;
bd7ab16b
L
8815}
8816#endif
8817
29b0f896 8818static void
e3bb37b5 8819output_jump (void)
29b0f896
AM
8820{
8821 char *p;
8822 int size;
3e02c1cc 8823 fixS *fixP;
bd7ab16b 8824 bfd_reloc_code_real_type jump_reloc = i.reloc[0];
29b0f896 8825
0cfa3eb3 8826 if (i.tm.opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
8827 {
8828 /* This is a loop or jecxz type instruction. */
8829 size = 1;
8830 if (i.prefix[ADDR_PREFIX] != 0)
8831 {
48ef937e 8832 frag_opcode_byte (ADDR_PREFIX_OPCODE);
29b0f896
AM
8833 i.prefixes -= 1;
8834 }
8835 /* Pentium4 branch hints. */
8836 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8837 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
8838 {
48ef937e 8839 frag_opcode_byte (i.prefix[SEG_PREFIX]);
29b0f896 8840 i.prefixes--;
3e73aa7c
JH
8841 }
8842 }
29b0f896
AM
8843 else
8844 {
8845 int code16;
3e73aa7c 8846
29b0f896
AM
8847 code16 = 0;
8848 if (flag_code == CODE_16BIT)
8849 code16 = CODE16;
3e73aa7c 8850
29b0f896
AM
8851 if (i.prefix[DATA_PREFIX] != 0)
8852 {
48ef937e 8853 frag_opcode_byte (DATA_PREFIX_OPCODE);
29b0f896 8854 i.prefixes -= 1;
376cd056 8855 code16 ^= flip_code16(code16);
29b0f896 8856 }
252b5132 8857
29b0f896
AM
8858 size = 4;
8859 if (code16)
8860 size = 2;
8861 }
9fcc94b6 8862
6cb0a70e
JB
8863 /* BND prefixed jump. */
8864 if (i.prefix[BND_PREFIX] != 0)
29b0f896 8865 {
48ef937e 8866 frag_opcode_byte (i.prefix[BND_PREFIX]);
29b0f896
AM
8867 i.prefixes -= 1;
8868 }
252b5132 8869
6cb0a70e 8870 if (i.prefix[REX_PREFIX] != 0)
7e8b059b 8871 {
48ef937e 8872 frag_opcode_byte (i.prefix[REX_PREFIX]);
7e8b059b
L
8873 i.prefixes -= 1;
8874 }
8875
f2810fe0
JB
8876 if (i.prefixes != 0)
8877 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
e0890092 8878
48ef937e
JB
8879 if (now_seg == absolute_section)
8880 {
9a182d04 8881 abs_section_offset += i.opcode_length + size;
48ef937e
JB
8882 return;
8883 }
8884
9a182d04
JB
8885 p = frag_more (i.opcode_length + size);
8886 switch (i.opcode_length)
42164a71
L
8887 {
8888 case 2:
8889 *p++ = i.tm.base_opcode >> 8;
1a0670f3 8890 /* Fall through. */
42164a71
L
8891 case 1:
8892 *p++ = i.tm.base_opcode;
8893 break;
8894 default:
8895 abort ();
8896 }
e0890092 8897
bd7ab16b 8898#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1ef3994a
JB
8899 if (flag_code == CODE_64BIT && size == 4
8900 && jump_reloc == NO_RELOC && i.op[0].disps->X_add_number == 0
bd7ab16b
L
8901 && need_plt32_p (i.op[0].disps->X_add_symbol))
8902 jump_reloc = BFD_RELOC_X86_64_PLT32;
8903#endif
8904
8905 jump_reloc = reloc (size, 1, 1, jump_reloc);
8906
3e02c1cc 8907 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
bd7ab16b 8908 i.op[0].disps, 1, jump_reloc);
3e02c1cc 8909
eb19308f
JB
8910 /* All jumps handled here are signed, but don't unconditionally use a
8911 signed limit check for 32 and 16 bit jumps as we want to allow wrap
8912 around at 4G (outside of 64-bit mode) and 64k (except for XBEGIN)
8913 respectively. */
8914 switch (size)
8915 {
8916 case 1:
8917 fixP->fx_signed = 1;
8918 break;
8919
8920 case 2:
8921 if (i.tm.base_opcode == 0xc7f8)
8922 fixP->fx_signed = 1;
8923 break;
8924
8925 case 4:
8926 if (flag_code == CODE_64BIT)
8927 fixP->fx_signed = 1;
8928 break;
8929 }
29b0f896 8930}
e0890092 8931
29b0f896 8932static void
e3bb37b5 8933output_interseg_jump (void)
29b0f896
AM
8934{
8935 char *p;
8936 int size;
8937 int prefix;
8938 int code16;
252b5132 8939
29b0f896
AM
8940 code16 = 0;
8941 if (flag_code == CODE_16BIT)
8942 code16 = CODE16;
a217f122 8943
29b0f896
AM
8944 prefix = 0;
8945 if (i.prefix[DATA_PREFIX] != 0)
8946 {
8947 prefix = 1;
8948 i.prefixes -= 1;
8949 code16 ^= CODE16;
8950 }
6cb0a70e
JB
8951
8952 gas_assert (!i.prefix[REX_PREFIX]);
252b5132 8953
29b0f896
AM
8954 size = 4;
8955 if (code16)
8956 size = 2;
252b5132 8957
f2810fe0
JB
8958 if (i.prefixes != 0)
8959 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
252b5132 8960
48ef937e
JB
8961 if (now_seg == absolute_section)
8962 {
8963 abs_section_offset += prefix + 1 + 2 + size;
8964 return;
8965 }
8966
29b0f896
AM
8967 /* 1 opcode; 2 segment; offset */
8968 p = frag_more (prefix + 1 + 2 + size);
3e73aa7c 8969
29b0f896
AM
8970 if (i.prefix[DATA_PREFIX] != 0)
8971 *p++ = DATA_PREFIX_OPCODE;
252b5132 8972
29b0f896
AM
8973 if (i.prefix[REX_PREFIX] != 0)
8974 *p++ = i.prefix[REX_PREFIX];
252b5132 8975
29b0f896
AM
8976 *p++ = i.tm.base_opcode;
8977 if (i.op[1].imms->X_op == O_constant)
8978 {
8979 offsetT n = i.op[1].imms->X_add_number;
252b5132 8980
29b0f896
AM
8981 if (size == 2
8982 && !fits_in_unsigned_word (n)
8983 && !fits_in_signed_word (n))
8984 {
8985 as_bad (_("16-bit jump out of range"));
8986 return;
8987 }
8988 md_number_to_chars (p, n, size);
8989 }
8990 else
8991 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
d258b828 8992 i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
6d96a594
C
8993
8994 p += size;
8995 if (i.op[0].imms->X_op == O_constant)
8996 md_number_to_chars (p, (valueT) i.op[0].imms->X_add_number, 2);
8997 else
8998 fix_new_exp (frag_now, p - frag_now->fr_literal, 2,
8999 i.op[0].imms, 0, reloc (2, 0, 0, i.reloc[0]));
29b0f896 9000}
a217f122 9001
b4a3a7b4
L
9002#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9003void
9004x86_cleanup (void)
9005{
9006 char *p;
9007 asection *seg = now_seg;
9008 subsegT subseg = now_subseg;
9009 asection *sec;
9010 unsigned int alignment, align_size_1;
9011 unsigned int isa_1_descsz, feature_2_descsz, descsz;
9012 unsigned int isa_1_descsz_raw, feature_2_descsz_raw;
9013 unsigned int padding;
9014
1273b2f8 9015 if (!IS_ELF || !x86_used_note)
b4a3a7b4
L
9016 return;
9017
b4a3a7b4
L
9018 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X86;
9019
9020 /* The .note.gnu.property section layout:
9021
9022 Field Length Contents
9023 ---- ---- ----
9024 n_namsz 4 4
9025 n_descsz 4 The note descriptor size
9026 n_type 4 NT_GNU_PROPERTY_TYPE_0
9027 n_name 4 "GNU"
9028 n_desc n_descsz The program property array
9029 .... .... ....
9030 */
9031
9032 /* Create the .note.gnu.property section. */
9033 sec = subseg_new (NOTE_GNU_PROPERTY_SECTION_NAME, 0);
fd361982 9034 bfd_set_section_flags (sec,
b4a3a7b4
L
9035 (SEC_ALLOC
9036 | SEC_LOAD
9037 | SEC_DATA
9038 | SEC_HAS_CONTENTS
9039 | SEC_READONLY));
9040
9041 if (get_elf_backend_data (stdoutput)->s->elfclass == ELFCLASS64)
9042 {
9043 align_size_1 = 7;
9044 alignment = 3;
9045 }
9046 else
9047 {
9048 align_size_1 = 3;
9049 alignment = 2;
9050 }
9051
fd361982 9052 bfd_set_section_alignment (sec, alignment);
b4a3a7b4
L
9053 elf_section_type (sec) = SHT_NOTE;
9054
1273b2f8
L
9055 /* GNU_PROPERTY_X86_ISA_1_USED: 4-byte type + 4-byte data size
9056 + 4-byte data */
9057 isa_1_descsz_raw = 4 + 4 + 4;
9058 /* Align GNU_PROPERTY_X86_ISA_1_USED. */
9059 isa_1_descsz = (isa_1_descsz_raw + align_size_1) & ~align_size_1;
b4a3a7b4
L
9060
9061 feature_2_descsz_raw = isa_1_descsz;
9062 /* GNU_PROPERTY_X86_FEATURE_2_USED: 4-byte type + 4-byte data size
1273b2f8 9063 + 4-byte data */
b4a3a7b4
L
9064 feature_2_descsz_raw += 4 + 4 + 4;
9065 /* Align GNU_PROPERTY_X86_FEATURE_2_USED. */
9066 feature_2_descsz = ((feature_2_descsz_raw + align_size_1)
9067 & ~align_size_1);
9068
9069 descsz = feature_2_descsz;
9070 /* Section size: n_namsz + n_descsz + n_type + n_name + n_descsz. */
9071 p = frag_more (4 + 4 + 4 + 4 + descsz);
9072
9073 /* Write n_namsz. */
9074 md_number_to_chars (p, (valueT) 4, 4);
9075
9076 /* Write n_descsz. */
9077 md_number_to_chars (p + 4, (valueT) descsz, 4);
9078
9079 /* Write n_type. */
9080 md_number_to_chars (p + 4 * 2, (valueT) NT_GNU_PROPERTY_TYPE_0, 4);
9081
9082 /* Write n_name. */
9083 memcpy (p + 4 * 3, "GNU", 4);
9084
1273b2f8
L
9085 /* Write 4-byte type. */
9086 md_number_to_chars (p + 4 * 4,
9087 (valueT) GNU_PROPERTY_X86_ISA_1_USED, 4);
b4a3a7b4 9088
1273b2f8
L
9089 /* Write 4-byte data size. */
9090 md_number_to_chars (p + 4 * 5, (valueT) 4, 4);
b4a3a7b4 9091
1273b2f8
L
9092 /* Write 4-byte data. */
9093 md_number_to_chars (p + 4 * 6, (valueT) x86_isa_1_used, 4);
b4a3a7b4 9094
1273b2f8
L
9095 /* Zero out paddings. */
9096 padding = isa_1_descsz - isa_1_descsz_raw;
9097 if (padding)
9098 memset (p + 4 * 7, 0, padding);
b4a3a7b4
L
9099
9100 /* Write 4-byte type. */
9101 md_number_to_chars (p + isa_1_descsz + 4 * 4,
9102 (valueT) GNU_PROPERTY_X86_FEATURE_2_USED, 4);
9103
9104 /* Write 4-byte data size. */
9105 md_number_to_chars (p + isa_1_descsz + 4 * 5, (valueT) 4, 4);
9106
9107 /* Write 4-byte data. */
9108 md_number_to_chars (p + isa_1_descsz + 4 * 6,
9109 (valueT) x86_feature_2_used, 4);
9110
9111 /* Zero out paddings. */
9112 padding = feature_2_descsz - feature_2_descsz_raw;
9113 if (padding)
9114 memset (p + isa_1_descsz + 4 * 7, 0, padding);
9115
9116 /* We probably can't restore the current segment, for there likely
9117 isn't one yet... */
9118 if (seg && subseg)
9119 subseg_set (seg, subseg);
9120}
9121#endif
9122
9c33702b
JB
9123static unsigned int
9124encoding_length (const fragS *start_frag, offsetT start_off,
9125 const char *frag_now_ptr)
9126{
9127 unsigned int len = 0;
9128
9129 if (start_frag != frag_now)
9130 {
9131 const fragS *fr = start_frag;
9132
9133 do {
9134 len += fr->fr_fix;
9135 fr = fr->fr_next;
9136 } while (fr && fr != frag_now);
9137 }
9138
9139 return len - start_off + (frag_now_ptr - frag_now->fr_literal);
9140}
9141
e379e5f3 9142/* Return 1 for test, and, cmp, add, sub, inc and dec which may
79d72f45
HL
9143 be macro-fused with conditional jumps.
9144 NB: If TEST/AND/CMP/ADD/SUB/INC/DEC is of RIP relative address,
9145 or is one of the following format:
9146
9147 cmp m, imm
9148 add m, imm
9149 sub m, imm
9150 test m, imm
9151 and m, imm
9152 inc m
9153 dec m
9154
9155 it is unfusible. */
e379e5f3
L
9156
9157static int
79d72f45 9158maybe_fused_with_jcc_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
9159{
9160 /* No RIP address. */
9161 if (i.base_reg && i.base_reg->reg_num == RegIP)
9162 return 0;
9163
389d00a5
JB
9164 /* No opcodes outside of base encoding space. */
9165 if (i.tm.opcode_modifier.opcodespace != SPACE_BASE)
e379e5f3
L
9166 return 0;
9167
79d72f45
HL
9168 /* add, sub without add/sub m, imm. */
9169 if (i.tm.base_opcode <= 5
e379e5f3
L
9170 || (i.tm.base_opcode >= 0x28 && i.tm.base_opcode <= 0x2d)
9171 || ((i.tm.base_opcode | 3) == 0x83
79d72f45 9172 && (i.tm.extension_opcode == 0x5
e379e5f3 9173 || i.tm.extension_opcode == 0x0)))
79d72f45
HL
9174 {
9175 *mf_cmp_p = mf_cmp_alu_cmp;
9176 return !(i.mem_operands && i.imm_operands);
9177 }
e379e5f3 9178
79d72f45
HL
9179 /* and without and m, imm. */
9180 if ((i.tm.base_opcode >= 0x20 && i.tm.base_opcode <= 0x25)
9181 || ((i.tm.base_opcode | 3) == 0x83
9182 && i.tm.extension_opcode == 0x4))
9183 {
9184 *mf_cmp_p = mf_cmp_test_and;
9185 return !(i.mem_operands && i.imm_operands);
9186 }
9187
9188 /* test without test m imm. */
e379e5f3
L
9189 if ((i.tm.base_opcode | 1) == 0x85
9190 || (i.tm.base_opcode | 1) == 0xa9
9191 || ((i.tm.base_opcode | 1) == 0xf7
79d72f45
HL
9192 && i.tm.extension_opcode == 0))
9193 {
9194 *mf_cmp_p = mf_cmp_test_and;
9195 return !(i.mem_operands && i.imm_operands);
9196 }
9197
9198 /* cmp without cmp m, imm. */
9199 if ((i.tm.base_opcode >= 0x38 && i.tm.base_opcode <= 0x3d)
e379e5f3
L
9200 || ((i.tm.base_opcode | 3) == 0x83
9201 && (i.tm.extension_opcode == 0x7)))
79d72f45
HL
9202 {
9203 *mf_cmp_p = mf_cmp_alu_cmp;
9204 return !(i.mem_operands && i.imm_operands);
9205 }
e379e5f3 9206
79d72f45 9207 /* inc, dec without inc/dec m. */
e379e5f3
L
9208 if ((i.tm.cpu_flags.bitfield.cpuno64
9209 && (i.tm.base_opcode | 0xf) == 0x4f)
9210 || ((i.tm.base_opcode | 1) == 0xff
9211 && i.tm.extension_opcode <= 0x1))
79d72f45
HL
9212 {
9213 *mf_cmp_p = mf_cmp_incdec;
9214 return !i.mem_operands;
9215 }
e379e5f3
L
9216
9217 return 0;
9218}
9219
9220/* Return 1 if a FUSED_JCC_PADDING frag should be generated. */
9221
9222static int
79d72f45 9223add_fused_jcc_padding_frag_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
9224{
9225 /* NB: Don't work with COND_JUMP86 without i386. */
9226 if (!align_branch_power
9227 || now_seg == absolute_section
9228 || !cpu_arch_flags.bitfield.cpui386
9229 || !(align_branch & align_branch_fused_bit))
9230 return 0;
9231
79d72f45 9232 if (maybe_fused_with_jcc_p (mf_cmp_p))
e379e5f3
L
9233 {
9234 if (last_insn.kind == last_insn_other
9235 || last_insn.seg != now_seg)
9236 return 1;
9237 if (flag_debug)
9238 as_warn_where (last_insn.file, last_insn.line,
9239 _("`%s` skips -malign-branch-boundary on `%s`"),
9240 last_insn.name, i.tm.name);
9241 }
9242
9243 return 0;
9244}
9245
9246/* Return 1 if a BRANCH_PREFIX frag should be generated. */
9247
9248static int
9249add_branch_prefix_frag_p (void)
9250{
9251 /* NB: Don't work with COND_JUMP86 without i386. Don't add prefix
9252 to PadLock instructions since they include prefixes in opcode. */
9253 if (!align_branch_power
9254 || !align_branch_prefix_size
9255 || now_seg == absolute_section
9256 || i.tm.cpu_flags.bitfield.cpupadlock
9257 || !cpu_arch_flags.bitfield.cpui386)
9258 return 0;
9259
9260 /* Don't add prefix if it is a prefix or there is no operand in case
9261 that segment prefix is special. */
9262 if (!i.operands || i.tm.opcode_modifier.isprefix)
9263 return 0;
9264
9265 if (last_insn.kind == last_insn_other
9266 || last_insn.seg != now_seg)
9267 return 1;
9268
9269 if (flag_debug)
9270 as_warn_where (last_insn.file, last_insn.line,
9271 _("`%s` skips -malign-branch-boundary on `%s`"),
9272 last_insn.name, i.tm.name);
9273
9274 return 0;
9275}
9276
9277/* Return 1 if a BRANCH_PADDING frag should be generated. */
9278
9279static int
79d72f45
HL
9280add_branch_padding_frag_p (enum align_branch_kind *branch_p,
9281 enum mf_jcc_kind *mf_jcc_p)
e379e5f3
L
9282{
9283 int add_padding;
9284
9285 /* NB: Don't work with COND_JUMP86 without i386. */
9286 if (!align_branch_power
9287 || now_seg == absolute_section
389d00a5
JB
9288 || !cpu_arch_flags.bitfield.cpui386
9289 || i.tm.opcode_modifier.opcodespace != SPACE_BASE)
e379e5f3
L
9290 return 0;
9291
9292 add_padding = 0;
9293
9294 /* Check for jcc and direct jmp. */
9295 if (i.tm.opcode_modifier.jump == JUMP)
9296 {
9297 if (i.tm.base_opcode == JUMP_PC_RELATIVE)
9298 {
9299 *branch_p = align_branch_jmp;
9300 add_padding = align_branch & align_branch_jmp_bit;
9301 }
9302 else
9303 {
79d72f45
HL
9304 /* Because J<cc> and JN<cc> share same group in macro-fusible table,
9305 igore the lowest bit. */
9306 *mf_jcc_p = (i.tm.base_opcode & 0x0e) >> 1;
e379e5f3
L
9307 *branch_p = align_branch_jcc;
9308 if ((align_branch & align_branch_jcc_bit))
9309 add_padding = 1;
9310 }
9311 }
e379e5f3
L
9312 else if ((i.tm.base_opcode | 1) == 0xc3)
9313 {
9314 /* Near ret. */
9315 *branch_p = align_branch_ret;
9316 if ((align_branch & align_branch_ret_bit))
9317 add_padding = 1;
9318 }
9319 else
9320 {
9321 /* Check for indirect jmp, direct and indirect calls. */
9322 if (i.tm.base_opcode == 0xe8)
9323 {
9324 /* Direct call. */
9325 *branch_p = align_branch_call;
9326 if ((align_branch & align_branch_call_bit))
9327 add_padding = 1;
9328 }
9329 else if (i.tm.base_opcode == 0xff
9330 && (i.tm.extension_opcode == 2
9331 || i.tm.extension_opcode == 4))
9332 {
9333 /* Indirect call and jmp. */
9334 *branch_p = align_branch_indirect;
9335 if ((align_branch & align_branch_indirect_bit))
9336 add_padding = 1;
9337 }
9338
9339 if (add_padding
9340 && i.disp_operands
9341 && tls_get_addr
9342 && (i.op[0].disps->X_op == O_symbol
9343 || (i.op[0].disps->X_op == O_subtract
9344 && i.op[0].disps->X_op_symbol == GOT_symbol)))
9345 {
9346 symbolS *s = i.op[0].disps->X_add_symbol;
9347 /* No padding to call to global or undefined tls_get_addr. */
9348 if ((S_IS_EXTERNAL (s) || !S_IS_DEFINED (s))
9349 && strcmp (S_GET_NAME (s), tls_get_addr) == 0)
9350 return 0;
9351 }
9352 }
9353
9354 if (add_padding
9355 && last_insn.kind != last_insn_other
9356 && last_insn.seg == now_seg)
9357 {
9358 if (flag_debug)
9359 as_warn_where (last_insn.file, last_insn.line,
9360 _("`%s` skips -malign-branch-boundary on `%s`"),
9361 last_insn.name, i.tm.name);
9362 return 0;
9363 }
9364
9365 return add_padding;
9366}
9367
29b0f896 9368static void
e3bb37b5 9369output_insn (void)
29b0f896 9370{
2bbd9c25
JJ
9371 fragS *insn_start_frag;
9372 offsetT insn_start_off;
e379e5f3
L
9373 fragS *fragP = NULL;
9374 enum align_branch_kind branch = align_branch_none;
79d72f45
HL
9375 /* The initializer is arbitrary just to avoid uninitialized error.
9376 it's actually either assigned in add_branch_padding_frag_p
9377 or never be used. */
9378 enum mf_jcc_kind mf_jcc = mf_jcc_jo;
2bbd9c25 9379
b4a3a7b4 9380#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
48ef937e 9381 if (IS_ELF && x86_used_note && now_seg != absolute_section)
b4a3a7b4 9382 {
32930e4e
L
9383 if ((i.xstate & xstate_tmm) == xstate_tmm
9384 || i.tm.cpu_flags.bitfield.cpuamx_tile)
9385 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_TMM;
9386
b4a3a7b4
L
9387 if (i.tm.cpu_flags.bitfield.cpu8087
9388 || i.tm.cpu_flags.bitfield.cpu287
9389 || i.tm.cpu_flags.bitfield.cpu387
9390 || i.tm.cpu_flags.bitfield.cpu687
9391 || i.tm.cpu_flags.bitfield.cpufisttp)
9392 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X87;
014d61ea 9393
921eafea 9394 if ((i.xstate & xstate_mmx)
389d00a5
JB
9395 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
9396 && !is_any_vex_encoding (&i.tm)
9397 && (i.tm.base_opcode == 0x77 /* emms */
9398 || i.tm.base_opcode == 0x0e /* femms */)))
b4a3a7b4 9399 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MMX;
014d61ea 9400
32930e4e
L
9401 if (i.index_reg)
9402 {
9403 if (i.index_reg->reg_type.bitfield.zmmword)
9404 i.xstate |= xstate_zmm;
9405 else if (i.index_reg->reg_type.bitfield.ymmword)
9406 i.xstate |= xstate_ymm;
9407 else if (i.index_reg->reg_type.bitfield.xmmword)
9408 i.xstate |= xstate_xmm;
9409 }
014d61ea
JB
9410
9411 /* vzeroall / vzeroupper */
9412 if (i.tm.base_opcode == 0x77 && i.tm.cpu_flags.bitfield.cpuavx)
9413 i.xstate |= xstate_ymm;
9414
c4694f17 9415 if ((i.xstate & xstate_xmm)
389d00a5
JB
9416 /* ldmxcsr / stmxcsr / vldmxcsr / vstmxcsr */
9417 || (i.tm.base_opcode == 0xae
9418 && (i.tm.cpu_flags.bitfield.cpusse
9419 || i.tm.cpu_flags.bitfield.cpuavx))
c4694f17
TG
9420 || i.tm.cpu_flags.bitfield.cpuwidekl
9421 || i.tm.cpu_flags.bitfield.cpukl)
b4a3a7b4 9422 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XMM;
014d61ea 9423
921eafea 9424 if ((i.xstate & xstate_ymm) == xstate_ymm)
b4a3a7b4 9425 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_YMM;
921eafea 9426 if ((i.xstate & xstate_zmm) == xstate_zmm)
b4a3a7b4 9427 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_ZMM;
6225c532 9428 if (i.mask.reg || (i.xstate & xstate_mask) == xstate_mask)
32930e4e 9429 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MASK;
b4a3a7b4
L
9430 if (i.tm.cpu_flags.bitfield.cpufxsr)
9431 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_FXSR;
9432 if (i.tm.cpu_flags.bitfield.cpuxsave)
9433 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVE;
9434 if (i.tm.cpu_flags.bitfield.cpuxsaveopt)
9435 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT;
9436 if (i.tm.cpu_flags.bitfield.cpuxsavec)
9437 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEC;
b0ab0693
L
9438
9439 if (x86_feature_2_used
9440 || i.tm.cpu_flags.bitfield.cpucmov
9441 || i.tm.cpu_flags.bitfield.cpusyscall
389d00a5
JB
9442 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
9443 && i.tm.base_opcode == 0xc7
70e95837 9444 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
b0ab0693
L
9445 && i.tm.extension_opcode == 1) /* cmpxchg8b */)
9446 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_BASELINE;
9447 if (i.tm.cpu_flags.bitfield.cpusse3
9448 || i.tm.cpu_flags.bitfield.cpussse3
9449 || i.tm.cpu_flags.bitfield.cpusse4_1
9450 || i.tm.cpu_flags.bitfield.cpusse4_2
9451 || i.tm.cpu_flags.bitfield.cpucx16
9452 || i.tm.cpu_flags.bitfield.cpupopcnt
9453 /* LAHF-SAHF insns in 64-bit mode. */
9454 || (flag_code == CODE_64BIT
35648716
JB
9455 && (i.tm.base_opcode | 1) == 0x9f
9456 && i.tm.opcode_modifier.opcodespace == SPACE_BASE))
b0ab0693
L
9457 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V2;
9458 if (i.tm.cpu_flags.bitfield.cpuavx
9459 || i.tm.cpu_flags.bitfield.cpuavx2
a9860005
JB
9460 /* Any VEX encoded insns execpt for AVX512F, AVX512BW, AVX512DQ,
9461 XOP, FMA4, LPW, TBM, and AMX. */
b0ab0693
L
9462 || (i.tm.opcode_modifier.vex
9463 && !i.tm.cpu_flags.bitfield.cpuavx512f
9464 && !i.tm.cpu_flags.bitfield.cpuavx512bw
9465 && !i.tm.cpu_flags.bitfield.cpuavx512dq
a9860005
JB
9466 && !i.tm.cpu_flags.bitfield.cpuxop
9467 && !i.tm.cpu_flags.bitfield.cpufma4
b0ab0693
L
9468 && !i.tm.cpu_flags.bitfield.cpulwp
9469 && !i.tm.cpu_flags.bitfield.cputbm
9470 && !(x86_feature_2_used & GNU_PROPERTY_X86_FEATURE_2_TMM))
9471 || i.tm.cpu_flags.bitfield.cpuf16c
9472 || i.tm.cpu_flags.bitfield.cpufma
9473 || i.tm.cpu_flags.bitfield.cpulzcnt
9474 || i.tm.cpu_flags.bitfield.cpumovbe
9475 || i.tm.cpu_flags.bitfield.cpuxsaves
9476 || (x86_feature_2_used
9477 & (GNU_PROPERTY_X86_FEATURE_2_XSAVE
9478 | GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT
9479 | GNU_PROPERTY_X86_FEATURE_2_XSAVEC)) != 0)
9480 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V3;
9481 if (i.tm.cpu_flags.bitfield.cpuavx512f
9482 || i.tm.cpu_flags.bitfield.cpuavx512bw
9483 || i.tm.cpu_flags.bitfield.cpuavx512dq
9484 || i.tm.cpu_flags.bitfield.cpuavx512vl
a9860005
JB
9485 /* Any EVEX encoded insns except for AVX512ER, AVX512PF,
9486 AVX512-4FMAPS, and AVX512-4VNNIW. */
b0ab0693
L
9487 || (i.tm.opcode_modifier.evex
9488 && !i.tm.cpu_flags.bitfield.cpuavx512er
9489 && !i.tm.cpu_flags.bitfield.cpuavx512pf
a9860005 9490 && !i.tm.cpu_flags.bitfield.cpuavx512_4fmaps
b0ab0693
L
9491 && !i.tm.cpu_flags.bitfield.cpuavx512_4vnniw))
9492 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V4;
b4a3a7b4
L
9493 }
9494#endif
9495
29b0f896
AM
9496 /* Tie dwarf2 debug info to the address at the start of the insn.
9497 We can't do this after the insn has been output as the current
9498 frag may have been closed off. eg. by frag_var. */
9499 dwarf2_emit_insn (0);
9500
2bbd9c25
JJ
9501 insn_start_frag = frag_now;
9502 insn_start_off = frag_now_fix ();
9503
79d72f45 9504 if (add_branch_padding_frag_p (&branch, &mf_jcc))
e379e5f3
L
9505 {
9506 char *p;
9507 /* Branch can be 8 bytes. Leave some room for prefixes. */
9508 unsigned int max_branch_padding_size = 14;
9509
9510 /* Align section to boundary. */
9511 record_alignment (now_seg, align_branch_power);
9512
9513 /* Make room for padding. */
9514 frag_grow (max_branch_padding_size);
9515
9516 /* Start of the padding. */
9517 p = frag_more (0);
9518
9519 fragP = frag_now;
9520
9521 frag_var (rs_machine_dependent, max_branch_padding_size, 0,
9522 ENCODE_RELAX_STATE (BRANCH_PADDING, 0),
9523 NULL, 0, p);
9524
79d72f45 9525 fragP->tc_frag_data.mf_type = mf_jcc;
e379e5f3
L
9526 fragP->tc_frag_data.branch_type = branch;
9527 fragP->tc_frag_data.max_bytes = max_branch_padding_size;
9528 }
9529
d59a54c2
JB
9530 if (!cpu_arch_flags.bitfield.cpui386 && (flag_code != CODE_16BIT)
9531 && !pre_386_16bit_warned)
9532 {
9533 as_warn (_("use .code16 to ensure correct addressing mode"));
9534 pre_386_16bit_warned = true;
9535 }
9536
29b0f896 9537 /* Output jumps. */
0cfa3eb3 9538 if (i.tm.opcode_modifier.jump == JUMP)
29b0f896 9539 output_branch ();
0cfa3eb3
JB
9540 else if (i.tm.opcode_modifier.jump == JUMP_BYTE
9541 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896 9542 output_jump ();
0cfa3eb3 9543 else if (i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT)
29b0f896
AM
9544 output_interseg_jump ();
9545 else
9546 {
9547 /* Output normal instructions here. */
9548 char *p;
9549 unsigned char *q;
47465058 9550 unsigned int j;
79d72f45 9551 enum mf_cmp_kind mf_cmp;
4dffcebc 9552
e4e00185 9553 if (avoid_fence
389d00a5
JB
9554 && (i.tm.base_opcode == 0xaee8
9555 || i.tm.base_opcode == 0xaef0
9556 || i.tm.base_opcode == 0xaef8))
48ef937e
JB
9557 {
9558 /* Encode lfence, mfence, and sfence as
9559 f0 83 04 24 00 lock addl $0x0, (%{re}sp). */
47f4115a
JB
9560 if (flag_code == CODE_16BIT)
9561 as_bad (_("Cannot convert `%s' in 16-bit mode"), i.tm.name);
9562 else if (omit_lock_prefix)
9563 as_bad (_("Cannot convert `%s' with `-momit-lock-prefix=yes' in effect"),
9564 i.tm.name);
9565 else if (now_seg != absolute_section)
48ef937e
JB
9566 {
9567 offsetT val = 0x240483f0ULL;
9568
9569 p = frag_more (5);
9570 md_number_to_chars (p, val, 5);
9571 }
9572 else
9573 abs_section_offset += 5;
9574 return;
9575 }
e4e00185 9576
d022bddd
IT
9577 /* Some processors fail on LOCK prefix. This options makes
9578 assembler ignore LOCK prefix and serves as a workaround. */
9579 if (omit_lock_prefix)
9580 {
35648716
JB
9581 if (i.tm.base_opcode == LOCK_PREFIX_OPCODE
9582 && i.tm.opcode_modifier.isprefix)
d022bddd
IT
9583 return;
9584 i.prefix[LOCK_PREFIX] = 0;
9585 }
9586
e379e5f3
L
9587 if (branch)
9588 /* Skip if this is a branch. */
9589 ;
79d72f45 9590 else if (add_fused_jcc_padding_frag_p (&mf_cmp))
e379e5f3
L
9591 {
9592 /* Make room for padding. */
9593 frag_grow (MAX_FUSED_JCC_PADDING_SIZE);
9594 p = frag_more (0);
9595
9596 fragP = frag_now;
9597
9598 frag_var (rs_machine_dependent, MAX_FUSED_JCC_PADDING_SIZE, 0,
9599 ENCODE_RELAX_STATE (FUSED_JCC_PADDING, 0),
9600 NULL, 0, p);
9601
79d72f45 9602 fragP->tc_frag_data.mf_type = mf_cmp;
e379e5f3
L
9603 fragP->tc_frag_data.branch_type = align_branch_fused;
9604 fragP->tc_frag_data.max_bytes = MAX_FUSED_JCC_PADDING_SIZE;
9605 }
9606 else if (add_branch_prefix_frag_p ())
9607 {
9608 unsigned int max_prefix_size = align_branch_prefix_size;
9609
9610 /* Make room for padding. */
9611 frag_grow (max_prefix_size);
9612 p = frag_more (0);
9613
9614 fragP = frag_now;
9615
9616 frag_var (rs_machine_dependent, max_prefix_size, 0,
9617 ENCODE_RELAX_STATE (BRANCH_PREFIX, 0),
9618 NULL, 0, p);
9619
9620 fragP->tc_frag_data.max_bytes = max_prefix_size;
9621 }
9622
43234a1e
L
9623 /* Since the VEX/EVEX prefix contains the implicit prefix, we
9624 don't need the explicit prefix. */
cf665fee 9625 if (!is_any_vex_encoding (&i.tm))
bc4bd9ab 9626 {
7b47a312 9627 switch (i.tm.opcode_modifier.opcodeprefix)
bc4bd9ab 9628 {
7b47a312
L
9629 case PREFIX_0X66:
9630 add_prefix (0x66);
9631 break;
9632 case PREFIX_0XF2:
9633 add_prefix (0xf2);
9634 break;
9635 case PREFIX_0XF3:
8b65b895
L
9636 if (!i.tm.cpu_flags.bitfield.cpupadlock
9637 || (i.prefix[REP_PREFIX] != 0xf3))
9638 add_prefix (0xf3);
c0f3af97 9639 break;
7b47a312 9640 case PREFIX_NONE:
9a182d04 9641 switch (i.opcode_length)
c0f3af97 9642 {
7b47a312 9643 case 2:
7b47a312 9644 break;
9a182d04 9645 case 1:
7b47a312 9646 /* Check for pseudo prefixes. */
9a182d04
JB
9647 if (!i.tm.opcode_modifier.isprefix || i.tm.base_opcode)
9648 break;
7b47a312
L
9649 as_bad_where (insn_start_frag->fr_file,
9650 insn_start_frag->fr_line,
9651 _("pseudo prefix without instruction"));
9652 return;
9653 default:
9654 abort ();
4dffcebc 9655 }
c0f3af97 9656 break;
c0f3af97
L
9657 default:
9658 abort ();
bc4bd9ab 9659 }
c0f3af97 9660
6d19a37a 9661#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
cf61b747
L
9662 /* For x32, add a dummy REX_OPCODE prefix for mov/add with
9663 R_X86_64_GOTTPOFF relocation so that linker can safely
14470f07
L
9664 perform IE->LE optimization. A dummy REX_OPCODE prefix
9665 is also needed for lea with R_X86_64_GOTPC32_TLSDESC
9666 relocation for GDesc -> IE/LE optimization. */
cf61b747
L
9667 if (x86_elf_abi == X86_64_X32_ABI
9668 && i.operands == 2
14470f07
L
9669 && (i.reloc[0] == BFD_RELOC_X86_64_GOTTPOFF
9670 || i.reloc[0] == BFD_RELOC_X86_64_GOTPC32_TLSDESC)
cf61b747
L
9671 && i.prefix[REX_PREFIX] == 0)
9672 add_prefix (REX_OPCODE);
6d19a37a 9673#endif
cf61b747 9674
c0f3af97
L
9675 /* The prefix bytes. */
9676 for (j = ARRAY_SIZE (i.prefix), q = i.prefix; j > 0; j--, q++)
9677 if (*q)
48ef937e 9678 frag_opcode_byte (*q);
0f10071e 9679 }
ae5c1c7b 9680 else
c0f3af97
L
9681 {
9682 for (j = 0, q = i.prefix; j < ARRAY_SIZE (i.prefix); j++, q++)
9683 if (*q)
9684 switch (j)
9685 {
c0f3af97
L
9686 case SEG_PREFIX:
9687 case ADDR_PREFIX:
48ef937e 9688 frag_opcode_byte (*q);
c0f3af97
L
9689 break;
9690 default:
9691 /* There should be no other prefixes for instructions
9692 with VEX prefix. */
9693 abort ();
9694 }
9695
43234a1e
L
9696 /* For EVEX instructions i.vrex should become 0 after
9697 build_evex_prefix. For VEX instructions upper 16 registers
9698 aren't available, so VREX should be 0. */
9699 if (i.vrex)
9700 abort ();
c0f3af97 9701 /* Now the VEX prefix. */
48ef937e
JB
9702 if (now_seg != absolute_section)
9703 {
9704 p = frag_more (i.vex.length);
9705 for (j = 0; j < i.vex.length; j++)
9706 p[j] = i.vex.bytes[j];
9707 }
9708 else
9709 abs_section_offset += i.vex.length;
c0f3af97 9710 }
252b5132 9711
29b0f896 9712 /* Now the opcode; be careful about word order here! */
389d00a5
JB
9713 j = i.opcode_length;
9714 if (!i.vex.length)
9715 switch (i.tm.opcode_modifier.opcodespace)
9716 {
9717 case SPACE_BASE:
9718 break;
9719 case SPACE_0F:
9720 ++j;
9721 break;
9722 case SPACE_0F38:
9723 case SPACE_0F3A:
9724 j += 2;
9725 break;
9726 default:
9727 abort ();
9728 }
9729
48ef937e 9730 if (now_seg == absolute_section)
389d00a5
JB
9731 abs_section_offset += j;
9732 else if (j == 1)
29b0f896
AM
9733 {
9734 FRAG_APPEND_1_CHAR (i.tm.base_opcode);
9735 }
9736 else
9737 {
389d00a5
JB
9738 p = frag_more (j);
9739 if (!i.vex.length
9740 && i.tm.opcode_modifier.opcodespace != SPACE_BASE)
9741 {
9742 *p++ = 0x0f;
9743 if (i.tm.opcode_modifier.opcodespace != SPACE_0F)
9744 *p++ = i.tm.opcode_modifier.opcodespace == SPACE_0F38
9745 ? 0x38 : 0x3a;
9746 }
9747
9a182d04 9748 switch (i.opcode_length)
331d2d0d 9749 {
4dffcebc 9750 case 2:
389d00a5
JB
9751 /* Put out high byte first: can't use md_number_to_chars! */
9752 *p++ = (i.tm.base_opcode >> 8) & 0xff;
9753 /* Fall through. */
9754 case 1:
9755 *p = i.tm.base_opcode & 0xff;
4dffcebc
L
9756 break;
9757 default:
9758 abort ();
9759 break;
331d2d0d 9760 }
0f10071e 9761
29b0f896 9762 }
3e73aa7c 9763
29b0f896 9764 /* Now the modrm byte and sib byte (if present). */
40fb9820 9765 if (i.tm.opcode_modifier.modrm)
29b0f896 9766 {
48ef937e
JB
9767 frag_opcode_byte ((i.rm.regmem << 0)
9768 | (i.rm.reg << 3)
9769 | (i.rm.mode << 6));
29b0f896
AM
9770 /* If i.rm.regmem == ESP (4)
9771 && i.rm.mode != (Register mode)
9772 && not 16 bit
9773 ==> need second modrm byte. */
9774 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
9775 && i.rm.mode != 3
dc821c5f 9776 && !(i.base_reg && i.base_reg->reg_type.bitfield.word))
48ef937e
JB
9777 frag_opcode_byte ((i.sib.base << 0)
9778 | (i.sib.index << 3)
9779 | (i.sib.scale << 6));
29b0f896 9780 }
3e73aa7c 9781
29b0f896 9782 if (i.disp_operands)
2bbd9c25 9783 output_disp (insn_start_frag, insn_start_off);
3e73aa7c 9784
29b0f896 9785 if (i.imm_operands)
2bbd9c25 9786 output_imm (insn_start_frag, insn_start_off);
9c33702b
JB
9787
9788 /*
9789 * frag_now_fix () returning plain abs_section_offset when we're in the
9790 * absolute section, and abs_section_offset not getting updated as data
9791 * gets added to the frag breaks the logic below.
9792 */
9793 if (now_seg != absolute_section)
9794 {
9795 j = encoding_length (insn_start_frag, insn_start_off, frag_more (0));
9796 if (j > 15)
9797 as_warn (_("instruction length of %u bytes exceeds the limit of 15"),
9798 j);
e379e5f3
L
9799 else if (fragP)
9800 {
9801 /* NB: Don't add prefix with GOTPC relocation since
9802 output_disp() above depends on the fixed encoding
9803 length. Can't add prefix with TLS relocation since
9804 it breaks TLS linker optimization. */
9805 unsigned int max = i.has_gotpc_tls_reloc ? 0 : 15 - j;
9806 /* Prefix count on the current instruction. */
9807 unsigned int count = i.vex.length;
9808 unsigned int k;
9809 for (k = 0; k < ARRAY_SIZE (i.prefix); k++)
9810 /* REX byte is encoded in VEX/EVEX prefix. */
9811 if (i.prefix[k] && (k != REX_PREFIX || !i.vex.length))
9812 count++;
9813
9814 /* Count prefixes for extended opcode maps. */
9815 if (!i.vex.length)
389d00a5 9816 switch (i.tm.opcode_modifier.opcodespace)
e379e5f3 9817 {
389d00a5 9818 case SPACE_BASE:
e379e5f3 9819 break;
389d00a5
JB
9820 case SPACE_0F:
9821 count++;
e379e5f3 9822 break;
389d00a5
JB
9823 case SPACE_0F38:
9824 case SPACE_0F3A:
9825 count += 2;
e379e5f3
L
9826 break;
9827 default:
9828 abort ();
9829 }
9830
9831 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
9832 == BRANCH_PREFIX)
9833 {
9834 /* Set the maximum prefix size in BRANCH_PREFIX
9835 frag. */
9836 if (fragP->tc_frag_data.max_bytes > max)
9837 fragP->tc_frag_data.max_bytes = max;
9838 if (fragP->tc_frag_data.max_bytes > count)
9839 fragP->tc_frag_data.max_bytes -= count;
9840 else
9841 fragP->tc_frag_data.max_bytes = 0;
9842 }
9843 else
9844 {
9845 /* Remember the maximum prefix size in FUSED_JCC_PADDING
9846 frag. */
9847 unsigned int max_prefix_size;
9848 if (align_branch_prefix_size > max)
9849 max_prefix_size = max;
9850 else
9851 max_prefix_size = align_branch_prefix_size;
9852 if (max_prefix_size > count)
9853 fragP->tc_frag_data.max_prefix_length
9854 = max_prefix_size - count;
9855 }
9856
9857 /* Use existing segment prefix if possible. Use CS
9858 segment prefix in 64-bit mode. In 32-bit mode, use SS
9859 segment prefix with ESP/EBP base register and use DS
9860 segment prefix without ESP/EBP base register. */
9861 if (i.prefix[SEG_PREFIX])
9862 fragP->tc_frag_data.default_prefix = i.prefix[SEG_PREFIX];
9863 else if (flag_code == CODE_64BIT)
9864 fragP->tc_frag_data.default_prefix = CS_PREFIX_OPCODE;
9865 else if (i.base_reg
9866 && (i.base_reg->reg_num == 4
9867 || i.base_reg->reg_num == 5))
9868 fragP->tc_frag_data.default_prefix = SS_PREFIX_OPCODE;
9869 else
9870 fragP->tc_frag_data.default_prefix = DS_PREFIX_OPCODE;
9871 }
9c33702b 9872 }
29b0f896 9873 }
252b5132 9874
e379e5f3
L
9875 /* NB: Don't work with COND_JUMP86 without i386. */
9876 if (align_branch_power
9877 && now_seg != absolute_section
9878 && cpu_arch_flags.bitfield.cpui386)
9879 {
9880 /* Terminate each frag so that we can add prefix and check for
9881 fused jcc. */
9882 frag_wane (frag_now);
9883 frag_new (0);
9884 }
9885
29b0f896
AM
9886#ifdef DEBUG386
9887 if (flag_debug)
9888 {
7b81dfbb 9889 pi ("" /*line*/, &i);
29b0f896
AM
9890 }
9891#endif /* DEBUG386 */
9892}
252b5132 9893
e205caa7
L
9894/* Return the size of the displacement operand N. */
9895
9896static int
9897disp_size (unsigned int n)
9898{
9899 int size = 4;
43234a1e 9900
b5014f7a 9901 if (i.types[n].bitfield.disp64)
40fb9820
L
9902 size = 8;
9903 else if (i.types[n].bitfield.disp8)
9904 size = 1;
9905 else if (i.types[n].bitfield.disp16)
9906 size = 2;
e205caa7
L
9907 return size;
9908}
9909
9910/* Return the size of the immediate operand N. */
9911
9912static int
9913imm_size (unsigned int n)
9914{
9915 int size = 4;
40fb9820
L
9916 if (i.types[n].bitfield.imm64)
9917 size = 8;
9918 else if (i.types[n].bitfield.imm8 || i.types[n].bitfield.imm8s)
9919 size = 1;
9920 else if (i.types[n].bitfield.imm16)
9921 size = 2;
e205caa7
L
9922 return size;
9923}
9924
29b0f896 9925static void
64e74474 9926output_disp (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
9927{
9928 char *p;
9929 unsigned int n;
252b5132 9930
29b0f896
AM
9931 for (n = 0; n < i.operands; n++)
9932 {
b5014f7a 9933 if (operand_type_check (i.types[n], disp))
29b0f896 9934 {
48ef937e
JB
9935 int size = disp_size (n);
9936
9937 if (now_seg == absolute_section)
9938 abs_section_offset += size;
9939 else if (i.op[n].disps->X_op == O_constant)
29b0f896 9940 {
43234a1e 9941 offsetT val = i.op[n].disps->X_add_number;
252b5132 9942
629cfaf1
JB
9943 val = offset_in_range (val >> (size == 1 ? i.memshift : 0),
9944 size);
29b0f896
AM
9945 p = frag_more (size);
9946 md_number_to_chars (p, val, size);
9947 }
9948 else
9949 {
f86103b7 9950 enum bfd_reloc_code_real reloc_type;
a775efc8
JB
9951 bool pcrel = (i.flags[n] & Operand_PCrel) != 0;
9952 bool sign = (flag_code == CODE_64BIT && size == 4
9953 && (!want_disp32 (&i.tm)
9954 || (i.tm.opcode_modifier.jump && !i.jumpabsolute
9955 && !i.types[n].bitfield.baseindex)))
9956 || pcrel;
02a86693 9957 fixS *fixP;
29b0f896 9958
e205caa7 9959 /* We can't have 8 bit displacement here. */
9c2799c2 9960 gas_assert (!i.types[n].bitfield.disp8);
e205caa7 9961
29b0f896
AM
9962 /* The PC relative address is computed relative
9963 to the instruction boundary, so in case immediate
9964 fields follows, we need to adjust the value. */
9965 if (pcrel && i.imm_operands)
9966 {
29b0f896 9967 unsigned int n1;
e205caa7 9968 int sz = 0;
252b5132 9969
29b0f896 9970 for (n1 = 0; n1 < i.operands; n1++)
40fb9820 9971 if (operand_type_check (i.types[n1], imm))
252b5132 9972 {
e205caa7
L
9973 /* Only one immediate is allowed for PC
9974 relative address. */
9c2799c2 9975 gas_assert (sz == 0);
e205caa7
L
9976 sz = imm_size (n1);
9977 i.op[n].disps->X_add_number -= sz;
252b5132 9978 }
29b0f896 9979 /* We should find the immediate. */
9c2799c2 9980 gas_assert (sz != 0);
29b0f896 9981 }
520dc8e8 9982
29b0f896 9983 p = frag_more (size);
d258b828 9984 reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
d6ab8113 9985 if (GOT_symbol
2bbd9c25 9986 && GOT_symbol == i.op[n].disps->X_add_symbol
d6ab8113 9987 && (((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
9988 || reloc_type == BFD_RELOC_X86_64_32S
9989 || (reloc_type == BFD_RELOC_64
9990 && object_64bit))
d6ab8113
JB
9991 && (i.op[n].disps->X_op == O_symbol
9992 || (i.op[n].disps->X_op == O_add
9993 && ((symbol_get_value_expression
9994 (i.op[n].disps->X_op_symbol)->X_op)
9995 == O_subtract))))
9996 || reloc_type == BFD_RELOC_32_PCREL))
2bbd9c25 9997 {
4fa24527 9998 if (!object_64bit)
7b81dfbb
AJ
9999 {
10000 reloc_type = BFD_RELOC_386_GOTPC;
5b7c81bd 10001 i.has_gotpc_tls_reloc = true;
98da05bf 10002 i.op[n].disps->X_add_number +=
d583596c 10003 encoding_length (insn_start_frag, insn_start_off, p);
7b81dfbb
AJ
10004 }
10005 else if (reloc_type == BFD_RELOC_64)
10006 reloc_type = BFD_RELOC_X86_64_GOTPC64;
d6ab8113 10007 else
7b81dfbb
AJ
10008 /* Don't do the adjustment for x86-64, as there
10009 the pcrel addressing is relative to the _next_
10010 insn, and that is taken care of in other code. */
d6ab8113 10011 reloc_type = BFD_RELOC_X86_64_GOTPC32;
2bbd9c25 10012 }
e379e5f3
L
10013 else if (align_branch_power)
10014 {
10015 switch (reloc_type)
10016 {
10017 case BFD_RELOC_386_TLS_GD:
10018 case BFD_RELOC_386_TLS_LDM:
10019 case BFD_RELOC_386_TLS_IE:
10020 case BFD_RELOC_386_TLS_IE_32:
10021 case BFD_RELOC_386_TLS_GOTIE:
10022 case BFD_RELOC_386_TLS_GOTDESC:
10023 case BFD_RELOC_386_TLS_DESC_CALL:
10024 case BFD_RELOC_X86_64_TLSGD:
10025 case BFD_RELOC_X86_64_TLSLD:
10026 case BFD_RELOC_X86_64_GOTTPOFF:
10027 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
10028 case BFD_RELOC_X86_64_TLSDESC_CALL:
5b7c81bd 10029 i.has_gotpc_tls_reloc = true;
e379e5f3
L
10030 default:
10031 break;
10032 }
10033 }
02a86693
L
10034 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal,
10035 size, i.op[n].disps, pcrel,
10036 reloc_type);
eb19308f
JB
10037
10038 if (flag_code == CODE_64BIT && size == 4 && pcrel
10039 && !i.prefix[ADDR_PREFIX])
10040 fixP->fx_signed = 1;
10041
02a86693
L
10042 /* Check for "call/jmp *mem", "mov mem, %reg",
10043 "test %reg, mem" and "binop mem, %reg" where binop
10044 is one of adc, add, and, cmp, or, sbb, sub, xor
e60f4d3b
L
10045 instructions without data prefix. Always generate
10046 R_386_GOT32X for "sym*GOT" operand in 32-bit mode. */
10047 if (i.prefix[DATA_PREFIX] == 0
10048 && (generate_relax_relocations
10049 || (!object_64bit
10050 && i.rm.mode == 0
10051 && i.rm.regmem == 5))
0cb4071e
L
10052 && (i.rm.mode == 2
10053 || (i.rm.mode == 0 && i.rm.regmem == 5))
389d00a5 10054 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
02a86693
L
10055 && ((i.operands == 1
10056 && i.tm.base_opcode == 0xff
10057 && (i.rm.reg == 2 || i.rm.reg == 4))
10058 || (i.operands == 2
10059 && (i.tm.base_opcode == 0x8b
10060 || i.tm.base_opcode == 0x85
2ae4c703 10061 || (i.tm.base_opcode & ~0x38) == 0x03))))
02a86693
L
10062 {
10063 if (object_64bit)
10064 {
10065 fixP->fx_tcbit = i.rex != 0;
10066 if (i.base_reg
e968fc9b 10067 && (i.base_reg->reg_num == RegIP))
02a86693
L
10068 fixP->fx_tcbit2 = 1;
10069 }
10070 else
10071 fixP->fx_tcbit2 = 1;
10072 }
29b0f896
AM
10073 }
10074 }
10075 }
10076}
252b5132 10077
29b0f896 10078static void
64e74474 10079output_imm (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
10080{
10081 char *p;
10082 unsigned int n;
252b5132 10083
29b0f896
AM
10084 for (n = 0; n < i.operands; n++)
10085 {
40fb9820 10086 if (operand_type_check (i.types[n], imm))
29b0f896 10087 {
48ef937e
JB
10088 int size = imm_size (n);
10089
10090 if (now_seg == absolute_section)
10091 abs_section_offset += size;
10092 else if (i.op[n].imms->X_op == O_constant)
29b0f896 10093 {
29b0f896 10094 offsetT val;
b4cac588 10095
29b0f896
AM
10096 val = offset_in_range (i.op[n].imms->X_add_number,
10097 size);
10098 p = frag_more (size);
10099 md_number_to_chars (p, val, size);
10100 }
10101 else
10102 {
10103 /* Not absolute_section.
10104 Need a 32-bit fixup (don't support 8bit
10105 non-absolute imms). Try to support other
10106 sizes ... */
f86103b7 10107 enum bfd_reloc_code_real reloc_type;
e205caa7 10108 int sign;
29b0f896 10109
40fb9820 10110 if (i.types[n].bitfield.imm32s
a7d61044 10111 && (i.suffix == QWORD_MNEM_SUFFIX
40fb9820 10112 || (!i.suffix && i.tm.opcode_modifier.no_lsuf)))
29b0f896 10113 sign = 1;
e205caa7
L
10114 else
10115 sign = 0;
520dc8e8 10116
29b0f896 10117 p = frag_more (size);
d258b828 10118 reloc_type = reloc (size, 0, sign, i.reloc[n]);
f86103b7 10119
2bbd9c25
JJ
10120 /* This is tough to explain. We end up with this one if we
10121 * have operands that look like
10122 * "_GLOBAL_OFFSET_TABLE_+[.-.L284]". The goal here is to
10123 * obtain the absolute address of the GOT, and it is strongly
10124 * preferable from a performance point of view to avoid using
10125 * a runtime relocation for this. The actual sequence of
10126 * instructions often look something like:
10127 *
10128 * call .L66
10129 * .L66:
10130 * popl %ebx
10131 * addl $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
10132 *
10133 * The call and pop essentially return the absolute address
10134 * of the label .L66 and store it in %ebx. The linker itself
10135 * will ultimately change the first operand of the addl so
10136 * that %ebx points to the GOT, but to keep things simple, the
10137 * .o file must have this operand set so that it generates not
10138 * the absolute address of .L66, but the absolute address of
10139 * itself. This allows the linker itself simply treat a GOTPC
10140 * relocation as asking for a pcrel offset to the GOT to be
10141 * added in, and the addend of the relocation is stored in the
10142 * operand field for the instruction itself.
10143 *
10144 * Our job here is to fix the operand so that it would add
10145 * the correct offset so that %ebx would point to itself. The
10146 * thing that is tricky is that .-.L66 will point to the
10147 * beginning of the instruction, so we need to further modify
10148 * the operand so that it will point to itself. There are
10149 * other cases where you have something like:
10150 *
10151 * .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
10152 *
10153 * and here no correction would be required. Internally in
10154 * the assembler we treat operands of this form as not being
10155 * pcrel since the '.' is explicitly mentioned, and I wonder
10156 * whether it would simplify matters to do it this way. Who
10157 * knows. In earlier versions of the PIC patches, the
10158 * pcrel_adjust field was used to store the correction, but
10159 * since the expression is not pcrel, I felt it would be
10160 * confusing to do it this way. */
10161
d6ab8113 10162 if ((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
10163 || reloc_type == BFD_RELOC_X86_64_32S
10164 || reloc_type == BFD_RELOC_64)
29b0f896
AM
10165 && GOT_symbol
10166 && GOT_symbol == i.op[n].imms->X_add_symbol
10167 && (i.op[n].imms->X_op == O_symbol
10168 || (i.op[n].imms->X_op == O_add
10169 && ((symbol_get_value_expression
10170 (i.op[n].imms->X_op_symbol)->X_op)
10171 == O_subtract))))
10172 {
4fa24527 10173 if (!object_64bit)
d6ab8113 10174 reloc_type = BFD_RELOC_386_GOTPC;
7b81dfbb 10175 else if (size == 4)
d6ab8113 10176 reloc_type = BFD_RELOC_X86_64_GOTPC32;
7b81dfbb
AJ
10177 else if (size == 8)
10178 reloc_type = BFD_RELOC_X86_64_GOTPC64;
5b7c81bd 10179 i.has_gotpc_tls_reloc = true;
d583596c
JB
10180 i.op[n].imms->X_add_number +=
10181 encoding_length (insn_start_frag, insn_start_off, p);
29b0f896 10182 }
29b0f896
AM
10183 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
10184 i.op[n].imms, 0, reloc_type);
10185 }
10186 }
10187 }
252b5132
RH
10188}
10189\f
d182319b
JB
10190/* x86_cons_fix_new is called via the expression parsing code when a
10191 reloc is needed. We use this hook to get the correct .got reloc. */
d182319b
JB
10192static int cons_sign = -1;
10193
10194void
e3bb37b5 10195x86_cons_fix_new (fragS *frag, unsigned int off, unsigned int len,
62ebcb5c 10196 expressionS *exp, bfd_reloc_code_real_type r)
d182319b 10197{
d258b828 10198 r = reloc (len, 0, cons_sign, r);
d182319b
JB
10199
10200#ifdef TE_PE
10201 if (exp->X_op == O_secrel)
10202 {
10203 exp->X_op = O_symbol;
10204 r = BFD_RELOC_32_SECREL;
10205 }
145667f8
MH
10206 else if (exp->X_op == O_secidx)
10207 r = BFD_RELOC_16_SECIDX;
d182319b
JB
10208#endif
10209
10210 fix_new_exp (frag, off, len, exp, 0, r);
10211}
10212
357d1bd8
L
10213/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
10214 purpose of the `.dc.a' internal pseudo-op. */
10215
10216int
10217x86_address_bytes (void)
10218{
10219 if ((stdoutput->arch_info->mach & bfd_mach_x64_32))
10220 return 4;
10221 return stdoutput->arch_info->bits_per_address / 8;
10222}
10223
deea4973
JB
10224#if (!(defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined (OBJ_MACH_O)) \
10225 || defined (LEX_AT)) && !defined (TE_PE)
d258b828 10226# define lex_got(reloc, adjust, types) NULL
718ddfc0 10227#else
f3c180ae
AM
10228/* Parse operands of the form
10229 <symbol>@GOTOFF+<nnn>
10230 and similar .plt or .got references.
10231
10232 If we find one, set up the correct relocation in RELOC and copy the
10233 input string, minus the `@GOTOFF' into a malloc'd buffer for
10234 parsing by the calling routine. Return this buffer, and if ADJUST
10235 is non-null set it to the length of the string we removed from the
10236 input line. Otherwise return NULL. */
10237static char *
91d6fa6a 10238lex_got (enum bfd_reloc_code_real *rel,
64e74474 10239 int *adjust,
d258b828 10240 i386_operand_type *types)
f3c180ae 10241{
7b81dfbb
AJ
10242 /* Some of the relocations depend on the size of what field is to
10243 be relocated. But in our callers i386_immediate and i386_displacement
10244 we don't yet know the operand size (this will be set by insn
10245 matching). Hence we record the word32 relocation here,
10246 and adjust the reloc according to the real size in reloc(). */
145667f8
MH
10247 static const struct
10248 {
f3c180ae 10249 const char *str;
cff8d58a 10250 int len;
4fa24527 10251 const enum bfd_reloc_code_real rel[2];
40fb9820 10252 const i386_operand_type types64;
5b7c81bd 10253 bool need_GOT_symbol;
145667f8
MH
10254 }
10255 gotrel[] =
10256 {
deea4973 10257#ifndef TE_PE
8ce3d284 10258#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
10259 { STRING_COMMA_LEN ("SIZE"), { BFD_RELOC_SIZE32,
10260 BFD_RELOC_SIZE32 },
5b7c81bd 10261 OPERAND_TYPE_IMM32_64, false },
8ce3d284 10262#endif
cff8d58a
L
10263 { STRING_COMMA_LEN ("PLTOFF"), { _dummy_first_bfd_reloc_code_real,
10264 BFD_RELOC_X86_64_PLTOFF64 },
5b7c81bd 10265 OPERAND_TYPE_IMM64, true },
cff8d58a
L
10266 { STRING_COMMA_LEN ("PLT"), { BFD_RELOC_386_PLT32,
10267 BFD_RELOC_X86_64_PLT32 },
a775efc8 10268 OPERAND_TYPE_IMM32_32S_DISP32, false },
cff8d58a
L
10269 { STRING_COMMA_LEN ("GOTPLT"), { _dummy_first_bfd_reloc_code_real,
10270 BFD_RELOC_X86_64_GOTPLT64 },
5b7c81bd 10271 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
10272 { STRING_COMMA_LEN ("GOTOFF"), { BFD_RELOC_386_GOTOFF,
10273 BFD_RELOC_X86_64_GOTOFF64 },
5b7c81bd 10274 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
10275 { STRING_COMMA_LEN ("GOTPCREL"), { _dummy_first_bfd_reloc_code_real,
10276 BFD_RELOC_X86_64_GOTPCREL },
a775efc8 10277 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10278 { STRING_COMMA_LEN ("TLSGD"), { BFD_RELOC_386_TLS_GD,
10279 BFD_RELOC_X86_64_TLSGD },
a775efc8 10280 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10281 { STRING_COMMA_LEN ("TLSLDM"), { BFD_RELOC_386_TLS_LDM,
10282 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10283 OPERAND_TYPE_NONE, true },
cff8d58a
L
10284 { STRING_COMMA_LEN ("TLSLD"), { _dummy_first_bfd_reloc_code_real,
10285 BFD_RELOC_X86_64_TLSLD },
a775efc8 10286 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10287 { STRING_COMMA_LEN ("GOTTPOFF"), { BFD_RELOC_386_TLS_IE_32,
10288 BFD_RELOC_X86_64_GOTTPOFF },
a775efc8 10289 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10290 { STRING_COMMA_LEN ("TPOFF"), { BFD_RELOC_386_TLS_LE_32,
10291 BFD_RELOC_X86_64_TPOFF32 },
a775efc8 10292 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
10293 { STRING_COMMA_LEN ("NTPOFF"), { BFD_RELOC_386_TLS_LE,
10294 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10295 OPERAND_TYPE_NONE, true },
cff8d58a
L
10296 { STRING_COMMA_LEN ("DTPOFF"), { BFD_RELOC_386_TLS_LDO_32,
10297 BFD_RELOC_X86_64_DTPOFF32 },
a775efc8 10298 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
10299 { STRING_COMMA_LEN ("GOTNTPOFF"),{ BFD_RELOC_386_TLS_GOTIE,
10300 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10301 OPERAND_TYPE_NONE, true },
cff8d58a
L
10302 { STRING_COMMA_LEN ("INDNTPOFF"),{ BFD_RELOC_386_TLS_IE,
10303 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10304 OPERAND_TYPE_NONE, true },
cff8d58a
L
10305 { STRING_COMMA_LEN ("GOT"), { BFD_RELOC_386_GOT32,
10306 BFD_RELOC_X86_64_GOT32 },
a775efc8 10307 OPERAND_TYPE_IMM32_32S_64_DISP32, true },
cff8d58a
L
10308 { STRING_COMMA_LEN ("TLSDESC"), { BFD_RELOC_386_TLS_GOTDESC,
10309 BFD_RELOC_X86_64_GOTPC32_TLSDESC },
a775efc8 10310 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10311 { STRING_COMMA_LEN ("TLSCALL"), { BFD_RELOC_386_TLS_DESC_CALL,
10312 BFD_RELOC_X86_64_TLSDESC_CALL },
a775efc8 10313 OPERAND_TYPE_IMM32_32S_DISP32, true },
deea4973
JB
10314#else /* TE_PE */
10315 { STRING_COMMA_LEN ("SECREL32"), { BFD_RELOC_32_SECREL,
10316 BFD_RELOC_32_SECREL },
a775efc8 10317 OPERAND_TYPE_IMM32_32S_64_DISP32_64, false },
deea4973 10318#endif
f3c180ae
AM
10319 };
10320 char *cp;
10321 unsigned int j;
10322
deea4973 10323#if defined (OBJ_MAYBE_ELF) && !defined (TE_PE)
718ddfc0
JB
10324 if (!IS_ELF)
10325 return NULL;
d382c579 10326#endif
718ddfc0 10327
f3c180ae 10328 for (cp = input_line_pointer; *cp != '@'; cp++)
67c11a9b 10329 if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
f3c180ae
AM
10330 return NULL;
10331
47465058 10332 for (j = 0; j < ARRAY_SIZE (gotrel); j++)
f3c180ae 10333 {
cff8d58a 10334 int len = gotrel[j].len;
28f81592 10335 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
f3c180ae 10336 {
4fa24527 10337 if (gotrel[j].rel[object_64bit] != 0)
f3c180ae 10338 {
28f81592
AM
10339 int first, second;
10340 char *tmpbuf, *past_reloc;
f3c180ae 10341
91d6fa6a 10342 *rel = gotrel[j].rel[object_64bit];
f3c180ae 10343
3956db08
JB
10344 if (types)
10345 {
10346 if (flag_code != CODE_64BIT)
40fb9820
L
10347 {
10348 types->bitfield.imm32 = 1;
10349 types->bitfield.disp32 = 1;
10350 }
3956db08
JB
10351 else
10352 *types = gotrel[j].types64;
10353 }
10354
844bf810 10355 if (gotrel[j].need_GOT_symbol && GOT_symbol == NULL)
f3c180ae
AM
10356 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
10357
28f81592 10358 /* The length of the first part of our input line. */
f3c180ae 10359 first = cp - input_line_pointer;
28f81592
AM
10360
10361 /* The second part goes from after the reloc token until
67c11a9b 10362 (and including) an end_of_line char or comma. */
28f81592 10363 past_reloc = cp + 1 + len;
67c11a9b
AM
10364 cp = past_reloc;
10365 while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
10366 ++cp;
10367 second = cp + 1 - past_reloc;
28f81592
AM
10368
10369 /* Allocate and copy string. The trailing NUL shouldn't
10370 be necessary, but be safe. */
add39d23 10371 tmpbuf = XNEWVEC (char, first + second + 2);
f3c180ae 10372 memcpy (tmpbuf, input_line_pointer, first);
0787a12d
AM
10373 if (second != 0 && *past_reloc != ' ')
10374 /* Replace the relocation token with ' ', so that
10375 errors like foo@GOTOFF1 will be detected. */
10376 tmpbuf[first++] = ' ';
af89796a
L
10377 else
10378 /* Increment length by 1 if the relocation token is
10379 removed. */
10380 len++;
10381 if (adjust)
10382 *adjust = len;
0787a12d
AM
10383 memcpy (tmpbuf + first, past_reloc, second);
10384 tmpbuf[first + second] = '\0';
f3c180ae
AM
10385 return tmpbuf;
10386 }
10387
4fa24527
JB
10388 as_bad (_("@%s reloc is not supported with %d-bit output format"),
10389 gotrel[j].str, 1 << (5 + object_64bit));
f3c180ae
AM
10390 return NULL;
10391 }
10392 }
10393
10394 /* Might be a symbol version string. Don't as_bad here. */
10395 return NULL;
10396}
4e4f7c87 10397#endif
f3c180ae 10398
62ebcb5c 10399bfd_reloc_code_real_type
e3bb37b5 10400x86_cons (expressionS *exp, int size)
f3c180ae 10401{
62ebcb5c
AM
10402 bfd_reloc_code_real_type got_reloc = NO_RELOC;
10403
2748c1b1
L
10404#if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
10405 && !defined (LEX_AT)) \
10406 || defined (TE_PE)
ee86248c
JB
10407 intel_syntax = -intel_syntax;
10408
3c7b9c2c 10409 exp->X_md = 0;
4fa24527 10410 if (size == 4 || (object_64bit && size == 8))
f3c180ae
AM
10411 {
10412 /* Handle @GOTOFF and the like in an expression. */
10413 char *save;
10414 char *gotfree_input_line;
4a57f2cf 10415 int adjust = 0;
f3c180ae
AM
10416
10417 save = input_line_pointer;
d258b828 10418 gotfree_input_line = lex_got (&got_reloc, &adjust, NULL);
f3c180ae
AM
10419 if (gotfree_input_line)
10420 input_line_pointer = gotfree_input_line;
10421
10422 expression (exp);
10423
10424 if (gotfree_input_line)
10425 {
10426 /* expression () has merrily parsed up to the end of line,
10427 or a comma - in the wrong buffer. Transfer how far
10428 input_line_pointer has moved to the right buffer. */
10429 input_line_pointer = (save
10430 + (input_line_pointer - gotfree_input_line)
10431 + adjust);
10432 free (gotfree_input_line);
3992d3b7
AM
10433 if (exp->X_op == O_constant
10434 || exp->X_op == O_absent
10435 || exp->X_op == O_illegal
0398aac5 10436 || exp->X_op == O_register
3992d3b7
AM
10437 || exp->X_op == O_big)
10438 {
10439 char c = *input_line_pointer;
10440 *input_line_pointer = 0;
10441 as_bad (_("missing or invalid expression `%s'"), save);
10442 *input_line_pointer = c;
10443 }
b9519cfe
L
10444 else if ((got_reloc == BFD_RELOC_386_PLT32
10445 || got_reloc == BFD_RELOC_X86_64_PLT32)
10446 && exp->X_op != O_symbol)
10447 {
10448 char c = *input_line_pointer;
10449 *input_line_pointer = 0;
10450 as_bad (_("invalid PLT expression `%s'"), save);
10451 *input_line_pointer = c;
10452 }
f3c180ae
AM
10453 }
10454 }
10455 else
10456 expression (exp);
ee86248c
JB
10457
10458 intel_syntax = -intel_syntax;
10459
10460 if (intel_syntax)
10461 i386_intel_simplify (exp);
2748c1b1
L
10462#else
10463 expression (exp);
10464#endif
62ebcb5c 10465
a442cac5
JB
10466 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
10467 if (size == 4 && exp->X_op == O_constant && !object_64bit)
10468 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
10469
62ebcb5c 10470 return got_reloc;
f3c180ae 10471}
f3c180ae 10472
9f32dd5b
L
10473static void
10474signed_cons (int size)
6482c264 10475{
a442cac5 10476 if (object_64bit)
d182319b
JB
10477 cons_sign = 1;
10478 cons (size);
10479 cons_sign = -1;
6482c264
NC
10480}
10481
d182319b 10482#ifdef TE_PE
6482c264 10483static void
7016a5d5 10484pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
6482c264
NC
10485{
10486 expressionS exp;
10487
10488 do
10489 {
10490 expression (&exp);
10491 if (exp.X_op == O_symbol)
10492 exp.X_op = O_secrel;
10493
10494 emit_expr (&exp, 4);
10495 }
10496 while (*input_line_pointer++ == ',');
10497
10498 input_line_pointer--;
10499 demand_empty_rest_of_line ();
10500}
145667f8
MH
10501
10502static void
10503pe_directive_secidx (int dummy ATTRIBUTE_UNUSED)
10504{
10505 expressionS exp;
10506
10507 do
10508 {
10509 expression (&exp);
10510 if (exp.X_op == O_symbol)
10511 exp.X_op = O_secidx;
10512
10513 emit_expr (&exp, 2);
10514 }
10515 while (*input_line_pointer++ == ',');
10516
10517 input_line_pointer--;
10518 demand_empty_rest_of_line ();
10519}
6482c264
NC
10520#endif
10521
7063667e
JB
10522/* Handle Rounding Control / SAE specifiers. */
10523
10524static char *
10525RC_SAE_specifier (const char *pstr)
10526{
10527 unsigned int j;
10528
10529 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); j++)
10530 {
10531 if (!strncmp (pstr, RC_NamesTable[j].name, RC_NamesTable[j].len))
10532 {
10533 if (i.rounding.type != rc_none)
10534 {
10535 as_bad (_("duplicated `{%s}'"), RC_NamesTable[j].name);
10536 return NULL;
10537 }
10538
10539 i.rounding.type = RC_NamesTable[j].type;
10540
10541 return (char *)(pstr + RC_NamesTable[j].len);
10542 }
10543 }
10544
10545 return NULL;
10546}
10547
43234a1e
L
10548/* Handle Vector operations. */
10549
10550static char *
f70c6814 10551check_VecOperations (char *op_string)
43234a1e
L
10552{
10553 const reg_entry *mask;
10554 const char *saved;
10555 char *end_op;
10556
f70c6814 10557 while (*op_string)
43234a1e
L
10558 {
10559 saved = op_string;
10560 if (*op_string == '{')
10561 {
10562 op_string++;
10563
10564 /* Check broadcasts. */
d34049e8 10565 if (startswith (op_string, "1to"))
43234a1e 10566 {
5273a3cd 10567 unsigned int bcst_type;
43234a1e 10568
5273a3cd 10569 if (i.broadcast.type)
43234a1e
L
10570 goto duplicated_vec_op;
10571
10572 op_string += 3;
10573 if (*op_string == '8')
8e6e0792 10574 bcst_type = 8;
b28d1bda 10575 else if (*op_string == '4')
8e6e0792 10576 bcst_type = 4;
b28d1bda 10577 else if (*op_string == '2')
8e6e0792 10578 bcst_type = 2;
43234a1e
L
10579 else if (*op_string == '1'
10580 && *(op_string+1) == '6')
10581 {
8e6e0792 10582 bcst_type = 16;
43234a1e
L
10583 op_string++;
10584 }
0cc78721
CL
10585 else if (*op_string == '3'
10586 && *(op_string+1) == '2')
10587 {
10588 bcst_type = 32;
10589 op_string++;
10590 }
43234a1e
L
10591 else
10592 {
10593 as_bad (_("Unsupported broadcast: `%s'"), saved);
10594 return NULL;
10595 }
10596 op_string++;
10597
5273a3cd
JB
10598 i.broadcast.type = bcst_type;
10599 i.broadcast.operand = this_operand;
43234a1e
L
10600 }
10601 /* Check masking operation. */
10602 else if ((mask = parse_register (op_string, &end_op)) != NULL)
10603 {
8a6fb3f9
JB
10604 if (mask == &bad_reg)
10605 return NULL;
10606
43234a1e 10607 /* k0 can't be used for write mask. */
f74a6307 10608 if (mask->reg_type.bitfield.class != RegMask || !mask->reg_num)
43234a1e 10609 {
6d2cd6b2
JB
10610 as_bad (_("`%s%s' can't be used for write mask"),
10611 register_prefix, mask->reg_name);
43234a1e
L
10612 return NULL;
10613 }
10614
6225c532 10615 if (!i.mask.reg)
43234a1e 10616 {
6225c532
JB
10617 i.mask.reg = mask;
10618 i.mask.operand = this_operand;
43234a1e 10619 }
6225c532
JB
10620 else if (i.mask.reg->reg_num)
10621 goto duplicated_vec_op;
43234a1e
L
10622 else
10623 {
6225c532 10624 i.mask.reg = mask;
43234a1e
L
10625
10626 /* Only "{z}" is allowed here. No need to check
10627 zeroing mask explicitly. */
6225c532 10628 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
10629 {
10630 as_bad (_("invalid write mask `%s'"), saved);
10631 return NULL;
10632 }
10633 }
10634
10635 op_string = end_op;
10636 }
10637 /* Check zeroing-flag for masking operation. */
10638 else if (*op_string == 'z')
10639 {
6225c532 10640 if (!i.mask.reg)
43234a1e 10641 {
6225c532
JB
10642 i.mask.reg = reg_k0;
10643 i.mask.zeroing = 1;
10644 i.mask.operand = this_operand;
43234a1e
L
10645 }
10646 else
10647 {
6225c532 10648 if (i.mask.zeroing)
43234a1e
L
10649 {
10650 duplicated_vec_op:
10651 as_bad (_("duplicated `%s'"), saved);
10652 return NULL;
10653 }
10654
6225c532 10655 i.mask.zeroing = 1;
43234a1e
L
10656
10657 /* Only "{%k}" is allowed here. No need to check mask
10658 register explicitly. */
6225c532 10659 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
10660 {
10661 as_bad (_("invalid zeroing-masking `%s'"),
10662 saved);
10663 return NULL;
10664 }
10665 }
10666
10667 op_string++;
10668 }
7063667e
JB
10669 else if (intel_syntax
10670 && (op_string = RC_SAE_specifier (op_string)) != NULL)
10671 i.rounding.modifier = true;
43234a1e
L
10672 else
10673 goto unknown_vec_op;
10674
10675 if (*op_string != '}')
10676 {
10677 as_bad (_("missing `}' in `%s'"), saved);
10678 return NULL;
10679 }
10680 op_string++;
0ba3a731
L
10681
10682 /* Strip whitespace since the addition of pseudo prefixes
10683 changed how the scrubber treats '{'. */
10684 if (is_space_char (*op_string))
10685 ++op_string;
10686
43234a1e
L
10687 continue;
10688 }
10689 unknown_vec_op:
10690 /* We don't know this one. */
10691 as_bad (_("unknown vector operation: `%s'"), saved);
10692 return NULL;
10693 }
10694
6225c532 10695 if (i.mask.reg && i.mask.zeroing && !i.mask.reg->reg_num)
6d2cd6b2
JB
10696 {
10697 as_bad (_("zeroing-masking only allowed with write mask"));
10698 return NULL;
10699 }
10700
43234a1e
L
10701 return op_string;
10702}
10703
252b5132 10704static int
70e41ade 10705i386_immediate (char *imm_start)
252b5132
RH
10706{
10707 char *save_input_line_pointer;
f3c180ae 10708 char *gotfree_input_line;
252b5132 10709 segT exp_seg = 0;
47926f60 10710 expressionS *exp;
40fb9820
L
10711 i386_operand_type types;
10712
0dfbf9d7 10713 operand_type_set (&types, ~0);
252b5132
RH
10714
10715 if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
10716 {
31b2323c
L
10717 as_bad (_("at most %d immediate operands are allowed"),
10718 MAX_IMMEDIATE_OPERANDS);
252b5132
RH
10719 return 0;
10720 }
10721
10722 exp = &im_expressions[i.imm_operands++];
520dc8e8 10723 i.op[this_operand].imms = exp;
252b5132
RH
10724
10725 if (is_space_char (*imm_start))
10726 ++imm_start;
10727
10728 save_input_line_pointer = input_line_pointer;
10729 input_line_pointer = imm_start;
10730
d258b828 10731 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
10732 if (gotfree_input_line)
10733 input_line_pointer = gotfree_input_line;
252b5132
RH
10734
10735 exp_seg = expression (exp);
10736
83183c0c 10737 SKIP_WHITESPACE ();
252b5132 10738 if (*input_line_pointer)
f3c180ae 10739 as_bad (_("junk `%s' after expression"), input_line_pointer);
252b5132
RH
10740
10741 input_line_pointer = save_input_line_pointer;
f3c180ae 10742 if (gotfree_input_line)
ee86248c
JB
10743 {
10744 free (gotfree_input_line);
10745
9aac24b1 10746 if (exp->X_op == O_constant)
ee86248c
JB
10747 exp->X_op = O_illegal;
10748 }
10749
9aac24b1
JB
10750 if (exp_seg == reg_section)
10751 {
10752 as_bad (_("illegal immediate register operand %s"), imm_start);
10753 return 0;
10754 }
10755
ee86248c
JB
10756 return i386_finalize_immediate (exp_seg, exp, types, imm_start);
10757}
252b5132 10758
ee86248c
JB
10759static int
10760i386_finalize_immediate (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
10761 i386_operand_type types, const char *imm_start)
10762{
10763 if (exp->X_op == O_absent || exp->X_op == O_illegal || exp->X_op == O_big)
252b5132 10764 {
313c53d1
L
10765 if (imm_start)
10766 as_bad (_("missing or invalid immediate expression `%s'"),
10767 imm_start);
3992d3b7 10768 return 0;
252b5132 10769 }
3e73aa7c 10770 else if (exp->X_op == O_constant)
252b5132 10771 {
47926f60 10772 /* Size it properly later. */
40fb9820 10773 i.types[this_operand].bitfield.imm64 = 1;
a442cac5
JB
10774
10775 /* If not 64bit, sign/zero extend val, to account for wraparound
10776 when !BFD64. */
10777 if (flag_code != CODE_64BIT)
10778 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
252b5132 10779 }
4c63da97 10780#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
f86103b7 10781 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
31312f95 10782 && exp_seg != absolute_section
47926f60 10783 && exp_seg != text_section
24eab124
AM
10784 && exp_seg != data_section
10785 && exp_seg != bss_section
10786 && exp_seg != undefined_section
f86103b7 10787 && !bfd_is_com_section (exp_seg))
252b5132 10788 {
d0b47220 10789 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
252b5132
RH
10790 return 0;
10791 }
10792#endif
10793 else
10794 {
10795 /* This is an address. The size of the address will be
24eab124 10796 determined later, depending on destination register,
3e73aa7c 10797 suffix, or the default for the section. */
40fb9820
L
10798 i.types[this_operand].bitfield.imm8 = 1;
10799 i.types[this_operand].bitfield.imm16 = 1;
10800 i.types[this_operand].bitfield.imm32 = 1;
10801 i.types[this_operand].bitfield.imm32s = 1;
10802 i.types[this_operand].bitfield.imm64 = 1;
c6fb90c8
L
10803 i.types[this_operand] = operand_type_and (i.types[this_operand],
10804 types);
252b5132
RH
10805 }
10806
10807 return 1;
10808}
10809
551c1ca1 10810static char *
e3bb37b5 10811i386_scale (char *scale)
252b5132 10812{
551c1ca1
AM
10813 offsetT val;
10814 char *save = input_line_pointer;
252b5132 10815
551c1ca1
AM
10816 input_line_pointer = scale;
10817 val = get_absolute_expression ();
10818
10819 switch (val)
252b5132 10820 {
551c1ca1 10821 case 1:
252b5132
RH
10822 i.log2_scale_factor = 0;
10823 break;
551c1ca1 10824 case 2:
252b5132
RH
10825 i.log2_scale_factor = 1;
10826 break;
551c1ca1 10827 case 4:
252b5132
RH
10828 i.log2_scale_factor = 2;
10829 break;
551c1ca1 10830 case 8:
252b5132
RH
10831 i.log2_scale_factor = 3;
10832 break;
10833 default:
a724f0f4
JB
10834 {
10835 char sep = *input_line_pointer;
10836
10837 *input_line_pointer = '\0';
10838 as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
10839 scale);
10840 *input_line_pointer = sep;
10841 input_line_pointer = save;
10842 return NULL;
10843 }
252b5132 10844 }
29b0f896 10845 if (i.log2_scale_factor != 0 && i.index_reg == 0)
252b5132
RH
10846 {
10847 as_warn (_("scale factor of %d without an index register"),
24eab124 10848 1 << i.log2_scale_factor);
252b5132 10849 i.log2_scale_factor = 0;
252b5132 10850 }
551c1ca1
AM
10851 scale = input_line_pointer;
10852 input_line_pointer = save;
10853 return scale;
252b5132
RH
10854}
10855
252b5132 10856static int
e3bb37b5 10857i386_displacement (char *disp_start, char *disp_end)
252b5132 10858{
29b0f896 10859 expressionS *exp;
252b5132
RH
10860 segT exp_seg = 0;
10861 char *save_input_line_pointer;
f3c180ae 10862 char *gotfree_input_line;
40fb9820
L
10863 int override;
10864 i386_operand_type bigdisp, types = anydisp;
3992d3b7 10865 int ret;
252b5132 10866
31b2323c
L
10867 if (i.disp_operands == MAX_MEMORY_OPERANDS)
10868 {
10869 as_bad (_("at most %d displacement operands are allowed"),
10870 MAX_MEMORY_OPERANDS);
10871 return 0;
10872 }
10873
0dfbf9d7 10874 operand_type_set (&bigdisp, 0);
6f2f06be 10875 if (i.jumpabsolute
48bcea9f 10876 || i.types[this_operand].bitfield.baseindex
0cfa3eb3
JB
10877 || (current_templates->start->opcode_modifier.jump != JUMP
10878 && current_templates->start->opcode_modifier.jump != JUMP_DWORD))
e05278af 10879 {
48bcea9f 10880 i386_addressing_mode ();
e05278af 10881 override = (i.prefix[ADDR_PREFIX] != 0);
40fb9820
L
10882 if (flag_code == CODE_64BIT)
10883 {
a775efc8 10884 bigdisp.bitfield.disp32 = 1;
40fb9820 10885 if (!override)
a775efc8 10886 bigdisp.bitfield.disp64 = 1;
40fb9820
L
10887 }
10888 else if ((flag_code == CODE_16BIT) ^ override)
40fb9820 10889 bigdisp.bitfield.disp16 = 1;
48bcea9f
JB
10890 else
10891 bigdisp.bitfield.disp32 = 1;
e05278af
JB
10892 }
10893 else
10894 {
376cd056
JB
10895 /* For PC-relative branches, the width of the displacement may be
10896 dependent upon data size, but is never dependent upon address size.
10897 Also make sure to not unintentionally match against a non-PC-relative
10898 branch template. */
10899 static templates aux_templates;
10900 const insn_template *t = current_templates->start;
5b7c81bd 10901 bool has_intel64 = false;
376cd056
JB
10902
10903 aux_templates.start = t;
10904 while (++t < current_templates->end)
10905 {
10906 if (t->opcode_modifier.jump
10907 != current_templates->start->opcode_modifier.jump)
10908 break;
4b5aaf5f 10909 if ((t->opcode_modifier.isa64 >= INTEL64))
5b7c81bd 10910 has_intel64 = true;
376cd056
JB
10911 }
10912 if (t < current_templates->end)
10913 {
10914 aux_templates.end = t;
10915 current_templates = &aux_templates;
10916 }
10917
e05278af 10918 override = (i.prefix[DATA_PREFIX] != 0);
40fb9820
L
10919 if (flag_code == CODE_64BIT)
10920 {
376cd056
JB
10921 if ((override || i.suffix == WORD_MNEM_SUFFIX)
10922 && (!intel64 || !has_intel64))
40fb9820
L
10923 bigdisp.bitfield.disp16 = 1;
10924 else
a775efc8 10925 bigdisp.bitfield.disp32 = 1;
40fb9820
L
10926 }
10927 else
e05278af
JB
10928 {
10929 if (!override)
10930 override = (i.suffix == (flag_code != CODE_16BIT
10931 ? WORD_MNEM_SUFFIX
10932 : LONG_MNEM_SUFFIX));
40fb9820
L
10933 bigdisp.bitfield.disp32 = 1;
10934 if ((flag_code == CODE_16BIT) ^ override)
10935 {
10936 bigdisp.bitfield.disp32 = 0;
10937 bigdisp.bitfield.disp16 = 1;
10938 }
e05278af 10939 }
e05278af 10940 }
c6fb90c8
L
10941 i.types[this_operand] = operand_type_or (i.types[this_operand],
10942 bigdisp);
252b5132
RH
10943
10944 exp = &disp_expressions[i.disp_operands];
520dc8e8 10945 i.op[this_operand].disps = exp;
252b5132
RH
10946 i.disp_operands++;
10947 save_input_line_pointer = input_line_pointer;
10948 input_line_pointer = disp_start;
10949 END_STRING_AND_SAVE (disp_end);
10950
10951#ifndef GCC_ASM_O_HACK
10952#define GCC_ASM_O_HACK 0
10953#endif
10954#if GCC_ASM_O_HACK
10955 END_STRING_AND_SAVE (disp_end + 1);
40fb9820 10956 if (i.types[this_operand].bitfield.baseIndex
24eab124 10957 && displacement_string_end[-1] == '+')
252b5132
RH
10958 {
10959 /* This hack is to avoid a warning when using the "o"
24eab124
AM
10960 constraint within gcc asm statements.
10961 For instance:
10962
10963 #define _set_tssldt_desc(n,addr,limit,type) \
10964 __asm__ __volatile__ ( \
10965 "movw %w2,%0\n\t" \
10966 "movw %w1,2+%0\n\t" \
10967 "rorl $16,%1\n\t" \
10968 "movb %b1,4+%0\n\t" \
10969 "movb %4,5+%0\n\t" \
10970 "movb $0,6+%0\n\t" \
10971 "movb %h1,7+%0\n\t" \
10972 "rorl $16,%1" \
10973 : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
10974
10975 This works great except that the output assembler ends
10976 up looking a bit weird if it turns out that there is
10977 no offset. You end up producing code that looks like:
10978
10979 #APP
10980 movw $235,(%eax)
10981 movw %dx,2+(%eax)
10982 rorl $16,%edx
10983 movb %dl,4+(%eax)
10984 movb $137,5+(%eax)
10985 movb $0,6+(%eax)
10986 movb %dh,7+(%eax)
10987 rorl $16,%edx
10988 #NO_APP
10989
47926f60 10990 So here we provide the missing zero. */
24eab124
AM
10991
10992 *displacement_string_end = '0';
252b5132
RH
10993 }
10994#endif
d258b828 10995 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
10996 if (gotfree_input_line)
10997 input_line_pointer = gotfree_input_line;
252b5132 10998
24eab124 10999 exp_seg = expression (exp);
252b5132 11000
636c26b0
AM
11001 SKIP_WHITESPACE ();
11002 if (*input_line_pointer)
11003 as_bad (_("junk `%s' after expression"), input_line_pointer);
11004#if GCC_ASM_O_HACK
11005 RESTORE_END_STRING (disp_end + 1);
11006#endif
636c26b0 11007 input_line_pointer = save_input_line_pointer;
636c26b0 11008 if (gotfree_input_line)
ee86248c
JB
11009 {
11010 free (gotfree_input_line);
11011
11012 if (exp->X_op == O_constant || exp->X_op == O_register)
11013 exp->X_op = O_illegal;
11014 }
11015
11016 ret = i386_finalize_displacement (exp_seg, exp, types, disp_start);
11017
11018 RESTORE_END_STRING (disp_end);
11019
11020 return ret;
11021}
11022
11023static int
11024i386_finalize_displacement (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
11025 i386_operand_type types, const char *disp_start)
11026{
ee86248c 11027 int ret = 1;
636c26b0 11028
24eab124
AM
11029 /* We do this to make sure that the section symbol is in
11030 the symbol table. We will ultimately change the relocation
47926f60 11031 to be relative to the beginning of the section. */
1ae12ab7 11032 if (i.reloc[this_operand] == BFD_RELOC_386_GOTOFF
d6ab8113
JB
11033 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
11034 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
24eab124 11035 {
636c26b0 11036 if (exp->X_op != O_symbol)
3992d3b7 11037 goto inv_disp;
636c26b0 11038
e5cb08ac 11039 if (S_IS_LOCAL (exp->X_add_symbol)
c64efb4b
L
11040 && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section
11041 && S_GET_SEGMENT (exp->X_add_symbol) != expr_section)
24eab124 11042 section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
24eab124
AM
11043 exp->X_op = O_subtract;
11044 exp->X_op_symbol = GOT_symbol;
1ae12ab7 11045 if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
29b0f896 11046 i.reloc[this_operand] = BFD_RELOC_32_PCREL;
d6ab8113
JB
11047 else if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
11048 i.reloc[this_operand] = BFD_RELOC_64;
23df1078 11049 else
29b0f896 11050 i.reloc[this_operand] = BFD_RELOC_32;
24eab124 11051 }
252b5132 11052
3992d3b7
AM
11053 else if (exp->X_op == O_absent
11054 || exp->X_op == O_illegal
ee86248c 11055 || exp->X_op == O_big)
2daf4fd8 11056 {
3992d3b7
AM
11057 inv_disp:
11058 as_bad (_("missing or invalid displacement expression `%s'"),
2daf4fd8 11059 disp_start);
3992d3b7 11060 ret = 0;
2daf4fd8
AM
11061 }
11062
a50187b2
JB
11063 else if (exp->X_op == O_constant)
11064 {
11065 /* Sizing gets taken care of by optimize_disp().
11066
11067 If not 64bit, sign/zero extend val, to account for wraparound
11068 when !BFD64. */
11069 if (flag_code != CODE_64BIT)
11070 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
11071 }
11072
4c63da97 11073#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
a50187b2 11074 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
3992d3b7
AM
11075 && exp_seg != absolute_section
11076 && exp_seg != text_section
11077 && exp_seg != data_section
11078 && exp_seg != bss_section
11079 && exp_seg != undefined_section
11080 && !bfd_is_com_section (exp_seg))
24eab124 11081 {
d0b47220 11082 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
3992d3b7 11083 ret = 0;
24eab124 11084 }
252b5132 11085#endif
3956db08 11086
a50187b2 11087 else if (current_templates->start->opcode_modifier.jump == JUMP_BYTE)
48bcea9f
JB
11088 i.types[this_operand].bitfield.disp8 = 1;
11089
40fb9820 11090 /* Check if this is a displacement only operand. */
02b83698
JB
11091 if (!i.types[this_operand].bitfield.baseindex)
11092 i.types[this_operand] =
11093 operand_type_or (operand_type_and_not (i.types[this_operand], anydisp),
11094 operand_type_and (i.types[this_operand], types));
3956db08 11095
3992d3b7 11096 return ret;
252b5132
RH
11097}
11098
2abc2bec
JB
11099/* Return the active addressing mode, taking address override and
11100 registers forming the address into consideration. Update the
11101 address override prefix if necessary. */
47926f60 11102
2abc2bec
JB
11103static enum flag_code
11104i386_addressing_mode (void)
252b5132 11105{
be05d201
L
11106 enum flag_code addr_mode;
11107
11108 if (i.prefix[ADDR_PREFIX])
11109 addr_mode = flag_code == CODE_32BIT ? CODE_16BIT : CODE_32BIT;
a23b33b3
JB
11110 else if (flag_code == CODE_16BIT
11111 && current_templates->start->cpu_flags.bitfield.cpumpx
11112 /* Avoid replacing the "16-bit addressing not allowed" diagnostic
11113 from md_assemble() by "is not a valid base/index expression"
11114 when there is a base and/or index. */
11115 && !i.types[this_operand].bitfield.baseindex)
11116 {
11117 /* MPX insn memory operands with neither base nor index must be forced
11118 to use 32-bit addressing in 16-bit mode. */
11119 addr_mode = CODE_32BIT;
11120 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
11121 ++i.prefixes;
11122 gas_assert (!i.types[this_operand].bitfield.disp16);
11123 gas_assert (!i.types[this_operand].bitfield.disp32);
11124 }
be05d201
L
11125 else
11126 {
11127 addr_mode = flag_code;
11128
24eab124 11129#if INFER_ADDR_PREFIX
be05d201
L
11130 if (i.mem_operands == 0)
11131 {
11132 /* Infer address prefix from the first memory operand. */
11133 const reg_entry *addr_reg = i.base_reg;
11134
11135 if (addr_reg == NULL)
11136 addr_reg = i.index_reg;
eecb386c 11137
be05d201
L
11138 if (addr_reg)
11139 {
e968fc9b 11140 if (addr_reg->reg_type.bitfield.dword)
be05d201
L
11141 addr_mode = CODE_32BIT;
11142 else if (flag_code != CODE_64BIT
dc821c5f 11143 && addr_reg->reg_type.bitfield.word)
be05d201
L
11144 addr_mode = CODE_16BIT;
11145
11146 if (addr_mode != flag_code)
11147 {
11148 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
11149 i.prefixes += 1;
11150 /* Change the size of any displacement too. At most one
11151 of Disp16 or Disp32 is set.
11152 FIXME. There doesn't seem to be any real need for
11153 separate Disp16 and Disp32 flags. The same goes for
11154 Imm16 and Imm32. Removing them would probably clean
11155 up the code quite a lot. */
11156 if (flag_code != CODE_64BIT
11157 && (i.types[this_operand].bitfield.disp16
11158 || i.types[this_operand].bitfield.disp32))
11159 i.types[this_operand]
11160 = operand_type_xor (i.types[this_operand], disp16_32);
11161 }
11162 }
11163 }
24eab124 11164#endif
be05d201
L
11165 }
11166
2abc2bec
JB
11167 return addr_mode;
11168}
11169
11170/* Make sure the memory operand we've been dealt is valid.
11171 Return 1 on success, 0 on a failure. */
11172
11173static int
11174i386_index_check (const char *operand_string)
11175{
11176 const char *kind = "base/index";
11177 enum flag_code addr_mode = i386_addressing_mode ();
a152332d 11178 const insn_template *t = current_templates->start;
2abc2bec 11179
a152332d
JB
11180 if (t->opcode_modifier.isstring
11181 && !t->cpu_flags.bitfield.cpupadlock
fc0763e6
JB
11182 && (current_templates->end[-1].opcode_modifier.isstring
11183 || i.mem_operands))
11184 {
11185 /* Memory operands of string insns are special in that they only allow
11186 a single register (rDI, rSI, or rBX) as their memory address. */
be05d201
L
11187 const reg_entry *expected_reg;
11188 static const char *di_si[][2] =
11189 {
11190 { "esi", "edi" },
11191 { "si", "di" },
11192 { "rsi", "rdi" }
11193 };
11194 static const char *bx[] = { "ebx", "bx", "rbx" };
fc0763e6
JB
11195
11196 kind = "string address";
11197
a152332d 11198 if (t->opcode_modifier.prefixok == PrefixRep)
fc0763e6 11199 {
51c8edf6
JB
11200 int es_op = current_templates->end[-1].opcode_modifier.isstring
11201 - IS_STRING_ES_OP0;
11202 int op = 0;
fc0763e6 11203
51c8edf6 11204 if (!current_templates->end[-1].operand_types[0].bitfield.baseindex
fc0763e6
JB
11205 || ((!i.mem_operands != !intel_syntax)
11206 && current_templates->end[-1].operand_types[1]
11207 .bitfield.baseindex))
51c8edf6 11208 op = 1;
fe0e921f
AM
11209 expected_reg
11210 = (const reg_entry *) str_hash_find (reg_hash,
11211 di_si[addr_mode][op == es_op]);
fc0763e6
JB
11212 }
11213 else
fe0e921f
AM
11214 expected_reg
11215 = (const reg_entry *)str_hash_find (reg_hash, bx[addr_mode]);
fc0763e6 11216
be05d201
L
11217 if (i.base_reg != expected_reg
11218 || i.index_reg
fc0763e6 11219 || operand_type_check (i.types[this_operand], disp))
fc0763e6 11220 {
be05d201
L
11221 /* The second memory operand must have the same size as
11222 the first one. */
11223 if (i.mem_operands
11224 && i.base_reg
11225 && !((addr_mode == CODE_64BIT
dc821c5f 11226 && i.base_reg->reg_type.bitfield.qword)
be05d201 11227 || (addr_mode == CODE_32BIT
dc821c5f
JB
11228 ? i.base_reg->reg_type.bitfield.dword
11229 : i.base_reg->reg_type.bitfield.word)))
be05d201
L
11230 goto bad_address;
11231
fc0763e6
JB
11232 as_warn (_("`%s' is not valid here (expected `%c%s%s%c')"),
11233 operand_string,
11234 intel_syntax ? '[' : '(',
11235 register_prefix,
be05d201 11236 expected_reg->reg_name,
fc0763e6 11237 intel_syntax ? ']' : ')');
be05d201 11238 return 1;
fc0763e6 11239 }
be05d201
L
11240 else
11241 return 1;
11242
dc1e8a47 11243 bad_address:
be05d201
L
11244 as_bad (_("`%s' is not a valid %s expression"),
11245 operand_string, kind);
11246 return 0;
3e73aa7c
JH
11247 }
11248 else
11249 {
be05d201
L
11250 if (addr_mode != CODE_16BIT)
11251 {
11252 /* 32-bit/64-bit checks. */
41eb8e88
L
11253 if (i.disp_encoding == disp_encoding_16bit)
11254 {
11255 bad_disp:
11256 as_bad (_("invalid `%s' prefix"),
11257 addr_mode == CODE_16BIT ? "{disp32}" : "{disp16}");
11258 return 0;
11259 }
11260
be05d201 11261 if ((i.base_reg
e968fc9b
JB
11262 && ((addr_mode == CODE_64BIT
11263 ? !i.base_reg->reg_type.bitfield.qword
11264 : !i.base_reg->reg_type.bitfield.dword)
11265 || (i.index_reg && i.base_reg->reg_num == RegIP)
11266 || i.base_reg->reg_num == RegIZ))
be05d201 11267 || (i.index_reg
1b54b8d7
JB
11268 && !i.index_reg->reg_type.bitfield.xmmword
11269 && !i.index_reg->reg_type.bitfield.ymmword
11270 && !i.index_reg->reg_type.bitfield.zmmword
be05d201 11271 && ((addr_mode == CODE_64BIT
e968fc9b
JB
11272 ? !i.index_reg->reg_type.bitfield.qword
11273 : !i.index_reg->reg_type.bitfield.dword)
be05d201
L
11274 || !i.index_reg->reg_type.bitfield.baseindex)))
11275 goto bad_address;
8178be5b 11276
260cd341 11277 /* bndmk, bndldx, bndstx and mandatory non-vector SIB have special restrictions. */
a152332d 11278 if ((t->opcode_modifier.opcodeprefix == PREFIX_0XF3
389d00a5
JB
11279 && t->opcode_modifier.opcodespace == SPACE_0F
11280 && t->base_opcode == 0x1b)
a152332d 11281 || (t->opcode_modifier.opcodeprefix == PREFIX_NONE
389d00a5
JB
11282 && t->opcode_modifier.opcodespace == SPACE_0F
11283 && (t->base_opcode & ~1) == 0x1a)
a152332d 11284 || t->opcode_modifier.sib == SIBMEM)
8178be5b
JB
11285 {
11286 /* They cannot use RIP-relative addressing. */
e968fc9b 11287 if (i.base_reg && i.base_reg->reg_num == RegIP)
8178be5b
JB
11288 {
11289 as_bad (_("`%s' cannot be used here"), operand_string);
11290 return 0;
11291 }
11292
11293 /* bndldx and bndstx ignore their scale factor. */
a152332d 11294 if (t->opcode_modifier.opcodeprefix == PREFIX_NONE
389d00a5
JB
11295 && t->opcode_modifier.opcodespace == SPACE_0F
11296 && (t->base_opcode & ~1) == 0x1a
8178be5b
JB
11297 && i.log2_scale_factor)
11298 as_warn (_("register scaling is being ignored here"));
11299 }
be05d201
L
11300 }
11301 else
3e73aa7c 11302 {
be05d201 11303 /* 16-bit checks. */
41eb8e88
L
11304 if (i.disp_encoding == disp_encoding_32bit)
11305 goto bad_disp;
11306
3e73aa7c 11307 if ((i.base_reg
dc821c5f 11308 && (!i.base_reg->reg_type.bitfield.word
40fb9820 11309 || !i.base_reg->reg_type.bitfield.baseindex))
3e73aa7c 11310 || (i.index_reg
dc821c5f 11311 && (!i.index_reg->reg_type.bitfield.word
40fb9820 11312 || !i.index_reg->reg_type.bitfield.baseindex
29b0f896
AM
11313 || !(i.base_reg
11314 && i.base_reg->reg_num < 6
11315 && i.index_reg->reg_num >= 6
11316 && i.log2_scale_factor == 0))))
be05d201 11317 goto bad_address;
3e73aa7c
JH
11318 }
11319 }
be05d201 11320 return 1;
24eab124 11321}
252b5132 11322
43234a1e
L
11323/* Handle vector immediates. */
11324
11325static int
11326RC_SAE_immediate (const char *imm_start)
11327{
43234a1e 11328 const char *pstr = imm_start;
43234a1e
L
11329
11330 if (*pstr != '{')
11331 return 0;
11332
7063667e
JB
11333 pstr = RC_SAE_specifier (pstr + 1);
11334 if (pstr == NULL)
43234a1e
L
11335 return 0;
11336
11337 if (*pstr++ != '}')
11338 {
11339 as_bad (_("Missing '}': '%s'"), imm_start);
11340 return 0;
11341 }
11342 /* RC/SAE immediate string should contain nothing more. */;
11343 if (*pstr != 0)
11344 {
11345 as_bad (_("Junk after '}': '%s'"), imm_start);
11346 return 0;
11347 }
11348
cf665fee
JB
11349 /* Internally this doesn't count as an operand. */
11350 --i.operands;
43234a1e 11351
43234a1e
L
11352 return 1;
11353}
11354
8325cc63
JB
11355/* Only string instructions can have a second memory operand, so
11356 reduce current_templates to just those if it contains any. */
11357static int
11358maybe_adjust_templates (void)
11359{
11360 const insn_template *t;
11361
11362 gas_assert (i.mem_operands == 1);
11363
11364 for (t = current_templates->start; t < current_templates->end; ++t)
11365 if (t->opcode_modifier.isstring)
11366 break;
11367
11368 if (t < current_templates->end)
11369 {
11370 static templates aux_templates;
5b7c81bd 11371 bool recheck;
8325cc63
JB
11372
11373 aux_templates.start = t;
11374 for (; t < current_templates->end; ++t)
11375 if (!t->opcode_modifier.isstring)
11376 break;
11377 aux_templates.end = t;
11378
11379 /* Determine whether to re-check the first memory operand. */
11380 recheck = (aux_templates.start != current_templates->start
11381 || t != current_templates->end);
11382
11383 current_templates = &aux_templates;
11384
11385 if (recheck)
11386 {
11387 i.mem_operands = 0;
11388 if (i.memop1_string != NULL
11389 && i386_index_check (i.memop1_string) == 0)
11390 return 0;
11391 i.mem_operands = 1;
11392 }
11393 }
11394
11395 return 1;
11396}
11397
9d299bea
JB
11398static INLINE bool starts_memory_operand (char c)
11399{
014fbcda 11400 return ISDIGIT (c)
9d299bea 11401 || is_identifier_char (c)
014fbcda 11402 || strchr ("([\"+-!~", c);
9d299bea
JB
11403}
11404
fc0763e6 11405/* Parse OPERAND_STRING into the i386_insn structure I. Returns zero
47926f60 11406 on error. */
252b5132 11407
252b5132 11408static int
a7619375 11409i386_att_operand (char *operand_string)
252b5132 11410{
af6bdddf
AM
11411 const reg_entry *r;
11412 char *end_op;
24eab124 11413 char *op_string = operand_string;
252b5132 11414
24eab124 11415 if (is_space_char (*op_string))
252b5132
RH
11416 ++op_string;
11417
24eab124 11418 /* We check for an absolute prefix (differentiating,
47926f60 11419 for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
24eab124
AM
11420 if (*op_string == ABSOLUTE_PREFIX)
11421 {
11422 ++op_string;
11423 if (is_space_char (*op_string))
11424 ++op_string;
5b7c81bd 11425 i.jumpabsolute = true;
24eab124 11426 }
252b5132 11427
47926f60 11428 /* Check if operand is a register. */
4d1bb795 11429 if ((r = parse_register (op_string, &end_op)) != NULL)
24eab124 11430 {
40fb9820
L
11431 i386_operand_type temp;
11432
8a6fb3f9
JB
11433 if (r == &bad_reg)
11434 return 0;
11435
24eab124
AM
11436 /* Check for a segment override by searching for ':' after a
11437 segment register. */
11438 op_string = end_op;
11439 if (is_space_char (*op_string))
11440 ++op_string;
00cee14f 11441 if (*op_string == ':' && r->reg_type.bitfield.class == SReg)
24eab124 11442 {
5e042380 11443 i.seg[i.mem_operands] = r;
252b5132 11444
24eab124 11445 /* Skip the ':' and whitespace. */
252b5132
RH
11446 ++op_string;
11447 if (is_space_char (*op_string))
24eab124 11448 ++op_string;
252b5132 11449
47926f60 11450 /* Handle case of %es:*foo. */
c8d541e2 11451 if (!i.jumpabsolute && *op_string == ABSOLUTE_PREFIX)
24eab124
AM
11452 {
11453 ++op_string;
11454 if (is_space_char (*op_string))
11455 ++op_string;
5b7c81bd 11456 i.jumpabsolute = true;
24eab124 11457 }
c8d541e2 11458
9d299bea 11459 if (!starts_memory_operand (*op_string))
c8d541e2
JB
11460 {
11461 as_bad (_("bad memory operand `%s'"), op_string);
11462 return 0;
11463 }
24eab124
AM
11464 goto do_memory_reference;
11465 }
43234a1e
L
11466
11467 /* Handle vector operations. */
11468 if (*op_string == '{')
11469 {
f70c6814 11470 op_string = check_VecOperations (op_string);
43234a1e
L
11471 if (op_string == NULL)
11472 return 0;
11473 }
11474
24eab124
AM
11475 if (*op_string)
11476 {
d0b47220 11477 as_bad (_("junk `%s' after register"), op_string);
24eab124
AM
11478 return 0;
11479 }
40fb9820
L
11480 temp = r->reg_type;
11481 temp.bitfield.baseindex = 0;
c6fb90c8
L
11482 i.types[this_operand] = operand_type_or (i.types[this_operand],
11483 temp);
7d5e4556 11484 i.types[this_operand].bitfield.unspecified = 0;
520dc8e8 11485 i.op[this_operand].regs = r;
24eab124 11486 i.reg_operands++;
cf665fee
JB
11487
11488 /* A GPR may follow an RC or SAE immediate only if a (vector) register
11489 operand was also present earlier on. */
11490 if (i.rounding.type != rc_none && temp.bitfield.class == Reg
11491 && i.reg_operands == 1)
11492 {
11493 unsigned int j;
11494
11495 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); ++j)
11496 if (i.rounding.type == RC_NamesTable[j].type)
11497 break;
11498 as_bad (_("`%s': misplaced `{%s}'"),
11499 current_templates->start->name, RC_NamesTable[j].name);
11500 return 0;
11501 }
24eab124 11502 }
af6bdddf
AM
11503 else if (*op_string == REGISTER_PREFIX)
11504 {
11505 as_bad (_("bad register name `%s'"), op_string);
11506 return 0;
11507 }
24eab124 11508 else if (*op_string == IMMEDIATE_PREFIX)
ce8a8b2f 11509 {
24eab124 11510 ++op_string;
6f2f06be 11511 if (i.jumpabsolute)
24eab124 11512 {
d0b47220 11513 as_bad (_("immediate operand illegal with absolute jump"));
24eab124
AM
11514 return 0;
11515 }
11516 if (!i386_immediate (op_string))
11517 return 0;
cf665fee
JB
11518 if (i.rounding.type != rc_none)
11519 {
11520 as_bad (_("`%s': RC/SAE operand must follow immediate operands"),
11521 current_templates->start->name);
11522 return 0;
11523 }
24eab124 11524 }
43234a1e
L
11525 else if (RC_SAE_immediate (operand_string))
11526 {
cf665fee
JB
11527 /* If it is a RC or SAE immediate, do the necessary placement check:
11528 Only another immediate or a GPR may precede it. */
11529 if (i.mem_operands || i.reg_operands + i.imm_operands > 1
11530 || (i.reg_operands == 1
11531 && i.op[0].regs->reg_type.bitfield.class != Reg))
11532 {
11533 as_bad (_("`%s': misplaced `%s'"),
11534 current_templates->start->name, operand_string);
11535 return 0;
11536 }
43234a1e 11537 }
9d299bea 11538 else if (starts_memory_operand (*op_string))
24eab124 11539 {
47926f60 11540 /* This is a memory reference of some sort. */
af6bdddf 11541 char *base_string;
252b5132 11542
47926f60 11543 /* Start and end of displacement string expression (if found). */
eecb386c
AM
11544 char *displacement_string_start;
11545 char *displacement_string_end;
252b5132 11546
24eab124 11547 do_memory_reference:
8325cc63
JB
11548 if (i.mem_operands == 1 && !maybe_adjust_templates ())
11549 return 0;
24eab124 11550 if ((i.mem_operands == 1
40fb9820 11551 && !current_templates->start->opcode_modifier.isstring)
24eab124
AM
11552 || i.mem_operands == 2)
11553 {
11554 as_bad (_("too many memory references for `%s'"),
11555 current_templates->start->name);
11556 return 0;
11557 }
252b5132 11558
24eab124
AM
11559 /* Check for base index form. We detect the base index form by
11560 looking for an ')' at the end of the operand, searching
11561 for the '(' matching it, and finding a REGISTER_PREFIX or ','
11562 after the '('. */
af6bdddf 11563 base_string = op_string + strlen (op_string);
c3332e24 11564
43234a1e 11565 /* Handle vector operations. */
6b5ba0d4
JB
11566 --base_string;
11567 if (is_space_char (*base_string))
11568 --base_string;
11569
11570 if (*base_string == '}')
43234a1e 11571 {
6b5ba0d4
JB
11572 char *vop_start = NULL;
11573
11574 while (base_string-- > op_string)
11575 {
11576 if (*base_string == '"')
11577 break;
11578 if (*base_string != '{')
11579 continue;
11580
11581 vop_start = base_string;
11582
11583 --base_string;
11584 if (is_space_char (*base_string))
11585 --base_string;
11586
11587 if (*base_string != '}')
11588 break;
11589
11590 vop_start = NULL;
11591 }
11592
11593 if (!vop_start)
11594 {
11595 as_bad (_("unbalanced figure braces"));
11596 return 0;
11597 }
11598
f70c6814 11599 if (check_VecOperations (vop_start) == NULL)
43234a1e 11600 return 0;
43234a1e
L
11601 }
11602
47926f60 11603 /* If we only have a displacement, set-up for it to be parsed later. */
af6bdddf
AM
11604 displacement_string_start = op_string;
11605 displacement_string_end = base_string + 1;
252b5132 11606
24eab124
AM
11607 if (*base_string == ')')
11608 {
af6bdddf 11609 char *temp_string;
e87fb6a6
JB
11610 unsigned int parens_not_balanced = 0;
11611 bool in_quotes = false;
e68c3d59 11612
24eab124 11613 /* We've already checked that the number of left & right ()'s are
e87fb6a6
JB
11614 equal, and that there's a matching set of double quotes. */
11615 end_op = base_string;
11616 for (temp_string = op_string; temp_string < end_op; temp_string++)
24eab124 11617 {
e87fb6a6
JB
11618 if (*temp_string == '\\' && temp_string[1] == '"')
11619 ++temp_string;
11620 else if (*temp_string == '"')
11621 in_quotes = !in_quotes;
11622 else if (!in_quotes)
11623 {
11624 if (*temp_string == '(' && !parens_not_balanced++)
11625 base_string = temp_string;
11626 if (*temp_string == ')')
11627 --parens_not_balanced;
11628 }
24eab124 11629 }
c3332e24 11630
af6bdddf 11631 temp_string = base_string;
c3332e24 11632
24eab124 11633 /* Skip past '(' and whitespace. */
e87fb6a6
JB
11634 gas_assert (*base_string == '(');
11635 ++base_string;
252b5132 11636 if (is_space_char (*base_string))
24eab124 11637 ++base_string;
252b5132 11638
af6bdddf 11639 if (*base_string == ','
4eed87de
AM
11640 || ((i.base_reg = parse_register (base_string, &end_op))
11641 != NULL))
252b5132 11642 {
af6bdddf 11643 displacement_string_end = temp_string;
252b5132 11644
40fb9820 11645 i.types[this_operand].bitfield.baseindex = 1;
252b5132 11646
af6bdddf 11647 if (i.base_reg)
24eab124 11648 {
8a6fb3f9
JB
11649 if (i.base_reg == &bad_reg)
11650 return 0;
24eab124
AM
11651 base_string = end_op;
11652 if (is_space_char (*base_string))
11653 ++base_string;
af6bdddf
AM
11654 }
11655
11656 /* There may be an index reg or scale factor here. */
11657 if (*base_string == ',')
11658 {
11659 ++base_string;
11660 if (is_space_char (*base_string))
11661 ++base_string;
11662
4eed87de
AM
11663 if ((i.index_reg = parse_register (base_string, &end_op))
11664 != NULL)
24eab124 11665 {
8a6fb3f9
JB
11666 if (i.index_reg == &bad_reg)
11667 return 0;
af6bdddf 11668 base_string = end_op;
24eab124
AM
11669 if (is_space_char (*base_string))
11670 ++base_string;
af6bdddf
AM
11671 if (*base_string == ',')
11672 {
11673 ++base_string;
11674 if (is_space_char (*base_string))
11675 ++base_string;
11676 }
e5cb08ac 11677 else if (*base_string != ')')
af6bdddf 11678 {
4eed87de
AM
11679 as_bad (_("expecting `,' or `)' "
11680 "after index register in `%s'"),
af6bdddf
AM
11681 operand_string);
11682 return 0;
11683 }
24eab124 11684 }
af6bdddf 11685 else if (*base_string == REGISTER_PREFIX)
24eab124 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
AM
11691 return 0;
11692 }
252b5132 11693
47926f60 11694 /* Check for scale factor. */
551c1ca1 11695 if (*base_string != ')')
af6bdddf 11696 {
551c1ca1
AM
11697 char *end_scale = i386_scale (base_string);
11698
11699 if (!end_scale)
af6bdddf 11700 return 0;
24eab124 11701
551c1ca1 11702 base_string = end_scale;
af6bdddf
AM
11703 if (is_space_char (*base_string))
11704 ++base_string;
11705 if (*base_string != ')')
11706 {
4eed87de
AM
11707 as_bad (_("expecting `)' "
11708 "after scale factor in `%s'"),
af6bdddf
AM
11709 operand_string);
11710 return 0;
11711 }
11712 }
11713 else if (!i.index_reg)
24eab124 11714 {
4eed87de
AM
11715 as_bad (_("expecting index register or scale factor "
11716 "after `,'; got '%c'"),
af6bdddf 11717 *base_string);
24eab124
AM
11718 return 0;
11719 }
11720 }
af6bdddf 11721 else if (*base_string != ')')
24eab124 11722 {
4eed87de
AM
11723 as_bad (_("expecting `,' or `)' "
11724 "after base register in `%s'"),
af6bdddf 11725 operand_string);
24eab124
AM
11726 return 0;
11727 }
c3332e24 11728 }
af6bdddf 11729 else if (*base_string == REGISTER_PREFIX)
c3332e24 11730 {
f76bf5e0
L
11731 end_op = strchr (base_string, ',');
11732 if (end_op)
11733 *end_op = '\0';
af6bdddf 11734 as_bad (_("bad register name `%s'"), base_string);
24eab124 11735 return 0;
c3332e24 11736 }
24eab124
AM
11737 }
11738
11739 /* If there's an expression beginning the operand, parse it,
11740 assuming displacement_string_start and
11741 displacement_string_end are meaningful. */
11742 if (displacement_string_start != displacement_string_end)
11743 {
11744 if (!i386_displacement (displacement_string_start,
11745 displacement_string_end))
11746 return 0;
11747 }
11748
11749 /* Special case for (%dx) while doing input/output op. */
11750 if (i.base_reg
75e5731b
JB
11751 && i.base_reg->reg_type.bitfield.instance == RegD
11752 && i.base_reg->reg_type.bitfield.word
24eab124
AM
11753 && i.index_reg == 0
11754 && i.log2_scale_factor == 0
11755 && i.seg[i.mem_operands] == 0
40fb9820 11756 && !operand_type_check (i.types[this_operand], disp))
24eab124 11757 {
2fb5be8d 11758 i.types[this_operand] = i.base_reg->reg_type;
24eab124
AM
11759 return 1;
11760 }
11761
eecb386c
AM
11762 if (i386_index_check (operand_string) == 0)
11763 return 0;
c48dadc9 11764 i.flags[this_operand] |= Operand_Mem;
8325cc63
JB
11765 if (i.mem_operands == 0)
11766 i.memop1_string = xstrdup (operand_string);
24eab124
AM
11767 i.mem_operands++;
11768 }
11769 else
ce8a8b2f
AM
11770 {
11771 /* It's not a memory operand; argh! */
24eab124
AM
11772 as_bad (_("invalid char %s beginning operand %d `%s'"),
11773 output_invalid (*op_string),
11774 this_operand + 1,
11775 op_string);
11776 return 0;
11777 }
47926f60 11778 return 1; /* Normal return. */
252b5132
RH
11779}
11780\f
fa94de6b
RM
11781/* Calculate the maximum variable size (i.e., excluding fr_fix)
11782 that an rs_machine_dependent frag may reach. */
11783
11784unsigned int
11785i386_frag_max_var (fragS *frag)
11786{
11787 /* The only relaxable frags are for jumps.
11788 Unconditional jumps can grow by 4 bytes and others by 5 bytes. */
11789 gas_assert (frag->fr_type == rs_machine_dependent);
11790 return TYPE_FROM_RELAX_STATE (frag->fr_subtype) == UNCOND_JUMP ? 4 : 5;
11791}
11792
b084df0b
L
11793#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
11794static int
8dcea932 11795elf_symbol_resolved_in_segment_p (symbolS *fr_symbol, offsetT fr_var)
b084df0b
L
11796{
11797 /* STT_GNU_IFUNC symbol must go through PLT. */
11798 if ((symbol_get_bfdsym (fr_symbol)->flags
11799 & BSF_GNU_INDIRECT_FUNCTION) != 0)
11800 return 0;
11801
11802 if (!S_IS_EXTERNAL (fr_symbol))
11803 /* Symbol may be weak or local. */
11804 return !S_IS_WEAK (fr_symbol);
11805
8dcea932
L
11806 /* Global symbols with non-default visibility can't be preempted. */
11807 if (ELF_ST_VISIBILITY (S_GET_OTHER (fr_symbol)) != STV_DEFAULT)
11808 return 1;
11809
11810 if (fr_var != NO_RELOC)
11811 switch ((enum bfd_reloc_code_real) fr_var)
11812 {
11813 case BFD_RELOC_386_PLT32:
11814 case BFD_RELOC_X86_64_PLT32:
33eaf5de 11815 /* Symbol with PLT relocation may be preempted. */
8dcea932
L
11816 return 0;
11817 default:
11818 abort ();
11819 }
11820
b084df0b
L
11821 /* Global symbols with default visibility in a shared library may be
11822 preempted by another definition. */
8dcea932 11823 return !shared;
b084df0b
L
11824}
11825#endif
11826
79d72f45
HL
11827/* Table 3-2. Macro-Fusible Instructions in Haswell Microarchitecture
11828 Note also work for Skylake and Cascadelake.
11829---------------------------------------------------------------------
11830| JCC | ADD/SUB/CMP | INC/DEC | TEST/AND |
11831| ------ | ----------- | ------- | -------- |
11832| Jo | N | N | Y |
11833| Jno | N | N | Y |
11834| Jc/Jb | Y | N | Y |
11835| Jae/Jnb | Y | N | Y |
11836| Je/Jz | Y | Y | Y |
11837| Jne/Jnz | Y | Y | Y |
11838| Jna/Jbe | Y | N | Y |
11839| Ja/Jnbe | Y | N | Y |
11840| Js | N | N | Y |
11841| Jns | N | N | Y |
11842| Jp/Jpe | N | N | Y |
11843| Jnp/Jpo | N | N | Y |
11844| Jl/Jnge | Y | Y | Y |
11845| Jge/Jnl | Y | Y | Y |
11846| Jle/Jng | Y | Y | Y |
11847| Jg/Jnle | Y | Y | Y |
11848--------------------------------------------------------------------- */
11849static int
11850i386_macro_fusible_p (enum mf_cmp_kind mf_cmp, enum mf_jcc_kind mf_jcc)
11851{
11852 if (mf_cmp == mf_cmp_alu_cmp)
11853 return ((mf_jcc >= mf_jcc_jc && mf_jcc <= mf_jcc_jna)
11854 || mf_jcc == mf_jcc_jl || mf_jcc == mf_jcc_jle);
11855 if (mf_cmp == mf_cmp_incdec)
11856 return (mf_jcc == mf_jcc_je || mf_jcc == mf_jcc_jl
11857 || mf_jcc == mf_jcc_jle);
11858 if (mf_cmp == mf_cmp_test_and)
11859 return 1;
11860 return 0;
11861}
11862
e379e5f3
L
11863/* Return the next non-empty frag. */
11864
11865static fragS *
11866i386_next_non_empty_frag (fragS *fragP)
11867{
11868 /* There may be a frag with a ".fill 0" when there is no room in
11869 the current frag for frag_grow in output_insn. */
11870 for (fragP = fragP->fr_next;
11871 (fragP != NULL
11872 && fragP->fr_type == rs_fill
11873 && fragP->fr_fix == 0);
11874 fragP = fragP->fr_next)
11875 ;
11876 return fragP;
11877}
11878
11879/* Return the next jcc frag after BRANCH_PADDING. */
11880
11881static fragS *
79d72f45 11882i386_next_fusible_jcc_frag (fragS *maybe_cmp_fragP, fragS *pad_fragP)
e379e5f3 11883{
79d72f45
HL
11884 fragS *branch_fragP;
11885 if (!pad_fragP)
e379e5f3
L
11886 return NULL;
11887
79d72f45
HL
11888 if (pad_fragP->fr_type == rs_machine_dependent
11889 && (TYPE_FROM_RELAX_STATE (pad_fragP->fr_subtype)
e379e5f3
L
11890 == BRANCH_PADDING))
11891 {
79d72f45
HL
11892 branch_fragP = i386_next_non_empty_frag (pad_fragP);
11893 if (branch_fragP->fr_type != rs_machine_dependent)
e379e5f3 11894 return NULL;
79d72f45
HL
11895 if (TYPE_FROM_RELAX_STATE (branch_fragP->fr_subtype) == COND_JUMP
11896 && i386_macro_fusible_p (maybe_cmp_fragP->tc_frag_data.mf_type,
11897 pad_fragP->tc_frag_data.mf_type))
11898 return branch_fragP;
e379e5f3
L
11899 }
11900
11901 return NULL;
11902}
11903
11904/* Classify BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags. */
11905
11906static void
11907i386_classify_machine_dependent_frag (fragS *fragP)
11908{
11909 fragS *cmp_fragP;
11910 fragS *pad_fragP;
11911 fragS *branch_fragP;
11912 fragS *next_fragP;
11913 unsigned int max_prefix_length;
11914
11915 if (fragP->tc_frag_data.classified)
11916 return;
11917
11918 /* First scan for BRANCH_PADDING and FUSED_JCC_PADDING. Convert
11919 FUSED_JCC_PADDING and merge BRANCH_PADDING. */
11920 for (next_fragP = fragP;
11921 next_fragP != NULL;
11922 next_fragP = next_fragP->fr_next)
11923 {
11924 next_fragP->tc_frag_data.classified = 1;
11925 if (next_fragP->fr_type == rs_machine_dependent)
11926 switch (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype))
11927 {
11928 case BRANCH_PADDING:
11929 /* The BRANCH_PADDING frag must be followed by a branch
11930 frag. */
11931 branch_fragP = i386_next_non_empty_frag (next_fragP);
11932 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
11933 break;
11934 case FUSED_JCC_PADDING:
11935 /* Check if this is a fused jcc:
11936 FUSED_JCC_PADDING
11937 CMP like instruction
11938 BRANCH_PADDING
11939 COND_JUMP
11940 */
11941 cmp_fragP = i386_next_non_empty_frag (next_fragP);
11942 pad_fragP = i386_next_non_empty_frag (cmp_fragP);
79d72f45 11943 branch_fragP = i386_next_fusible_jcc_frag (next_fragP, pad_fragP);
e379e5f3
L
11944 if (branch_fragP)
11945 {
11946 /* The BRANCH_PADDING frag is merged with the
11947 FUSED_JCC_PADDING frag. */
11948 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
11949 /* CMP like instruction size. */
11950 next_fragP->tc_frag_data.cmp_size = cmp_fragP->fr_fix;
11951 frag_wane (pad_fragP);
11952 /* Skip to branch_fragP. */
11953 next_fragP = branch_fragP;
11954 }
11955 else if (next_fragP->tc_frag_data.max_prefix_length)
11956 {
11957 /* Turn FUSED_JCC_PADDING into BRANCH_PREFIX if it isn't
11958 a fused jcc. */
11959 next_fragP->fr_subtype
11960 = ENCODE_RELAX_STATE (BRANCH_PREFIX, 0);
11961 next_fragP->tc_frag_data.max_bytes
11962 = next_fragP->tc_frag_data.max_prefix_length;
11963 /* This will be updated in the BRANCH_PREFIX scan. */
11964 next_fragP->tc_frag_data.max_prefix_length = 0;
11965 }
11966 else
11967 frag_wane (next_fragP);
11968 break;
11969 }
11970 }
11971
11972 /* Stop if there is no BRANCH_PREFIX. */
11973 if (!align_branch_prefix_size)
11974 return;
11975
11976 /* Scan for BRANCH_PREFIX. */
11977 for (; fragP != NULL; fragP = fragP->fr_next)
11978 {
11979 if (fragP->fr_type != rs_machine_dependent
11980 || (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
11981 != BRANCH_PREFIX))
11982 continue;
11983
11984 /* Count all BRANCH_PREFIX frags before BRANCH_PADDING and
11985 COND_JUMP_PREFIX. */
11986 max_prefix_length = 0;
11987 for (next_fragP = fragP;
11988 next_fragP != NULL;
11989 next_fragP = next_fragP->fr_next)
11990 {
11991 if (next_fragP->fr_type == rs_fill)
11992 /* Skip rs_fill frags. */
11993 continue;
11994 else if (next_fragP->fr_type != rs_machine_dependent)
11995 /* Stop for all other frags. */
11996 break;
11997
11998 /* rs_machine_dependent frags. */
11999 if (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12000 == BRANCH_PREFIX)
12001 {
12002 /* Count BRANCH_PREFIX frags. */
12003 if (max_prefix_length >= MAX_FUSED_JCC_PADDING_SIZE)
12004 {
12005 max_prefix_length = MAX_FUSED_JCC_PADDING_SIZE;
12006 frag_wane (next_fragP);
12007 }
12008 else
12009 max_prefix_length
12010 += next_fragP->tc_frag_data.max_bytes;
12011 }
12012 else if ((TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12013 == BRANCH_PADDING)
12014 || (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12015 == FUSED_JCC_PADDING))
12016 {
12017 /* Stop at BRANCH_PADDING and FUSED_JCC_PADDING. */
12018 fragP->tc_frag_data.u.padding_fragP = next_fragP;
12019 break;
12020 }
12021 else
12022 /* Stop for other rs_machine_dependent frags. */
12023 break;
12024 }
12025
12026 fragP->tc_frag_data.max_prefix_length = max_prefix_length;
12027
12028 /* Skip to the next frag. */
12029 fragP = next_fragP;
12030 }
12031}
12032
12033/* Compute padding size for
12034
12035 FUSED_JCC_PADDING
12036 CMP like instruction
12037 BRANCH_PADDING
12038 COND_JUMP/UNCOND_JUMP
12039
12040 or
12041
12042 BRANCH_PADDING
12043 COND_JUMP/UNCOND_JUMP
12044 */
12045
12046static int
12047i386_branch_padding_size (fragS *fragP, offsetT address)
12048{
12049 unsigned int offset, size, padding_size;
12050 fragS *branch_fragP = fragP->tc_frag_data.u.branch_fragP;
12051
12052 /* The start address of the BRANCH_PADDING or FUSED_JCC_PADDING frag. */
12053 if (!address)
12054 address = fragP->fr_address;
12055 address += fragP->fr_fix;
12056
12057 /* CMP like instrunction size. */
12058 size = fragP->tc_frag_data.cmp_size;
12059
12060 /* The base size of the branch frag. */
12061 size += branch_fragP->fr_fix;
12062
12063 /* Add opcode and displacement bytes for the rs_machine_dependent
12064 branch frag. */
12065 if (branch_fragP->fr_type == rs_machine_dependent)
12066 size += md_relax_table[branch_fragP->fr_subtype].rlx_length;
12067
12068 /* Check if branch is within boundary and doesn't end at the last
12069 byte. */
12070 offset = address & ((1U << align_branch_power) - 1);
12071 if ((offset + size) >= (1U << align_branch_power))
12072 /* Padding needed to avoid crossing boundary. */
12073 padding_size = (1U << align_branch_power) - offset;
12074 else
12075 /* No padding needed. */
12076 padding_size = 0;
12077
12078 /* The return value may be saved in tc_frag_data.length which is
12079 unsigned byte. */
12080 if (!fits_in_unsigned_byte (padding_size))
12081 abort ();
12082
12083 return padding_size;
12084}
12085
12086/* i386_generic_table_relax_frag()
12087
12088 Handle BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags to
12089 grow/shrink padding to align branch frags. Hand others to
12090 relax_frag(). */
12091
12092long
12093i386_generic_table_relax_frag (segT segment, fragS *fragP, long stretch)
12094{
12095 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12096 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
12097 {
12098 long padding_size = i386_branch_padding_size (fragP, 0);
12099 long grow = padding_size - fragP->tc_frag_data.length;
12100
12101 /* When the BRANCH_PREFIX frag is used, the computed address
12102 must match the actual address and there should be no padding. */
12103 if (fragP->tc_frag_data.padding_address
12104 && (fragP->tc_frag_data.padding_address != fragP->fr_address
12105 || padding_size))
12106 abort ();
12107
12108 /* Update the padding size. */
12109 if (grow)
12110 fragP->tc_frag_data.length = padding_size;
12111
12112 return grow;
12113 }
12114 else if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12115 {
12116 fragS *padding_fragP, *next_fragP;
12117 long padding_size, left_size, last_size;
12118
12119 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
12120 if (!padding_fragP)
12121 /* Use the padding set by the leading BRANCH_PREFIX frag. */
12122 return (fragP->tc_frag_data.length
12123 - fragP->tc_frag_data.last_length);
12124
12125 /* Compute the relative address of the padding frag in the very
12126 first time where the BRANCH_PREFIX frag sizes are zero. */
12127 if (!fragP->tc_frag_data.padding_address)
12128 fragP->tc_frag_data.padding_address
12129 = padding_fragP->fr_address - (fragP->fr_address - stretch);
12130
12131 /* First update the last length from the previous interation. */
12132 left_size = fragP->tc_frag_data.prefix_length;
12133 for (next_fragP = fragP;
12134 next_fragP != padding_fragP;
12135 next_fragP = next_fragP->fr_next)
12136 if (next_fragP->fr_type == rs_machine_dependent
12137 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12138 == BRANCH_PREFIX))
12139 {
12140 if (left_size)
12141 {
12142 int max = next_fragP->tc_frag_data.max_bytes;
12143 if (max)
12144 {
12145 int size;
12146 if (max > left_size)
12147 size = left_size;
12148 else
12149 size = max;
12150 left_size -= size;
12151 next_fragP->tc_frag_data.last_length = size;
12152 }
12153 }
12154 else
12155 next_fragP->tc_frag_data.last_length = 0;
12156 }
12157
12158 /* Check the padding size for the padding frag. */
12159 padding_size = i386_branch_padding_size
12160 (padding_fragP, (fragP->fr_address
12161 + fragP->tc_frag_data.padding_address));
12162
12163 last_size = fragP->tc_frag_data.prefix_length;
12164 /* Check if there is change from the last interation. */
12165 if (padding_size == last_size)
12166 {
12167 /* Update the expected address of the padding frag. */
12168 padding_fragP->tc_frag_data.padding_address
12169 = (fragP->fr_address + padding_size
12170 + fragP->tc_frag_data.padding_address);
12171 return 0;
12172 }
12173
12174 if (padding_size > fragP->tc_frag_data.max_prefix_length)
12175 {
12176 /* No padding if there is no sufficient room. Clear the
12177 expected address of the padding frag. */
12178 padding_fragP->tc_frag_data.padding_address = 0;
12179 padding_size = 0;
12180 }
12181 else
12182 /* Store the expected address of the padding frag. */
12183 padding_fragP->tc_frag_data.padding_address
12184 = (fragP->fr_address + padding_size
12185 + fragP->tc_frag_data.padding_address);
12186
12187 fragP->tc_frag_data.prefix_length = padding_size;
12188
12189 /* Update the length for the current interation. */
12190 left_size = padding_size;
12191 for (next_fragP = fragP;
12192 next_fragP != padding_fragP;
12193 next_fragP = next_fragP->fr_next)
12194 if (next_fragP->fr_type == rs_machine_dependent
12195 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12196 == BRANCH_PREFIX))
12197 {
12198 if (left_size)
12199 {
12200 int max = next_fragP->tc_frag_data.max_bytes;
12201 if (max)
12202 {
12203 int size;
12204 if (max > left_size)
12205 size = left_size;
12206 else
12207 size = max;
12208 left_size -= size;
12209 next_fragP->tc_frag_data.length = size;
12210 }
12211 }
12212 else
12213 next_fragP->tc_frag_data.length = 0;
12214 }
12215
12216 return (fragP->tc_frag_data.length
12217 - fragP->tc_frag_data.last_length);
12218 }
12219 return relax_frag (segment, fragP, stretch);
12220}
12221
ee7fcc42
AM
12222/* md_estimate_size_before_relax()
12223
12224 Called just before relax() for rs_machine_dependent frags. The x86
12225 assembler uses these frags to handle variable size jump
12226 instructions.
12227
12228 Any symbol that is now undefined will not become defined.
12229 Return the correct fr_subtype in the frag.
12230 Return the initial "guess for variable size of frag" to caller.
12231 The guess is actually the growth beyond the fixed part. Whatever
12232 we do to grow the fixed or variable part contributes to our
12233 returned value. */
12234
252b5132 12235int
7016a5d5 12236md_estimate_size_before_relax (fragS *fragP, segT segment)
252b5132 12237{
e379e5f3
L
12238 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12239 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX
12240 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
12241 {
12242 i386_classify_machine_dependent_frag (fragP);
12243 return fragP->tc_frag_data.length;
12244 }
12245
252b5132 12246 /* We've already got fragP->fr_subtype right; all we have to do is
b98ef147
AM
12247 check for un-relaxable symbols. On an ELF system, we can't relax
12248 an externally visible symbol, because it may be overridden by a
12249 shared library. */
12250 if (S_GET_SEGMENT (fragP->fr_symbol) != segment
6d249963 12251#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12252 || (IS_ELF
8dcea932
L
12253 && !elf_symbol_resolved_in_segment_p (fragP->fr_symbol,
12254 fragP->fr_var))
fbeb56a4
DK
12255#endif
12256#if defined (OBJ_COFF) && defined (TE_PE)
7ab9ffdd 12257 || (OUTPUT_FLAVOR == bfd_target_coff_flavour
fbeb56a4 12258 && S_IS_WEAK (fragP->fr_symbol))
b98ef147
AM
12259#endif
12260 )
252b5132 12261 {
b98ef147
AM
12262 /* Symbol is undefined in this segment, or we need to keep a
12263 reloc so that weak symbols can be overridden. */
12264 int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
f86103b7 12265 enum bfd_reloc_code_real reloc_type;
ee7fcc42
AM
12266 unsigned char *opcode;
12267 int old_fr_fix;
eb19308f 12268 fixS *fixP = NULL;
f6af82bd 12269
ee7fcc42 12270 if (fragP->fr_var != NO_RELOC)
1e9cc1c2 12271 reloc_type = (enum bfd_reloc_code_real) fragP->fr_var;
b98ef147 12272 else if (size == 2)
f6af82bd 12273 reloc_type = BFD_RELOC_16_PCREL;
bd7ab16b 12274#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1ef3994a
JB
12275 else if (fragP->tc_frag_data.code64 && fragP->fr_offset == 0
12276 && need_plt32_p (fragP->fr_symbol))
bd7ab16b
L
12277 reloc_type = BFD_RELOC_X86_64_PLT32;
12278#endif
f6af82bd
AM
12279 else
12280 reloc_type = BFD_RELOC_32_PCREL;
252b5132 12281
ee7fcc42
AM
12282 old_fr_fix = fragP->fr_fix;
12283 opcode = (unsigned char *) fragP->fr_opcode;
12284
fddf5b5b 12285 switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
252b5132 12286 {
fddf5b5b
AM
12287 case UNCOND_JUMP:
12288 /* Make jmp (0xeb) a (d)word displacement jump. */
47926f60 12289 opcode[0] = 0xe9;
252b5132 12290 fragP->fr_fix += size;
eb19308f
JB
12291 fixP = fix_new (fragP, old_fr_fix, size,
12292 fragP->fr_symbol,
12293 fragP->fr_offset, 1,
12294 reloc_type);
252b5132
RH
12295 break;
12296
fddf5b5b 12297 case COND_JUMP86:
412167cb
AM
12298 if (size == 2
12299 && (!no_cond_jump_promotion || fragP->fr_var != NO_RELOC))
fddf5b5b
AM
12300 {
12301 /* Negate the condition, and branch past an
12302 unconditional jump. */
12303 opcode[0] ^= 1;
12304 opcode[1] = 3;
12305 /* Insert an unconditional jump. */
12306 opcode[2] = 0xe9;
12307 /* We added two extra opcode bytes, and have a two byte
12308 offset. */
12309 fragP->fr_fix += 2 + 2;
062cd5e7
AS
12310 fix_new (fragP, old_fr_fix + 2, 2,
12311 fragP->fr_symbol,
12312 fragP->fr_offset, 1,
12313 reloc_type);
fddf5b5b
AM
12314 break;
12315 }
12316 /* Fall through. */
12317
12318 case COND_JUMP:
412167cb
AM
12319 if (no_cond_jump_promotion && fragP->fr_var == NO_RELOC)
12320 {
12321 fragP->fr_fix += 1;
3e02c1cc
AM
12322 fixP = fix_new (fragP, old_fr_fix, 1,
12323 fragP->fr_symbol,
12324 fragP->fr_offset, 1,
12325 BFD_RELOC_8_PCREL);
12326 fixP->fx_signed = 1;
412167cb
AM
12327 break;
12328 }
93c2a809 12329
24eab124 12330 /* This changes the byte-displacement jump 0x7N
fddf5b5b 12331 to the (d)word-displacement jump 0x0f,0x8N. */
252b5132 12332 opcode[1] = opcode[0] + 0x10;
f6af82bd 12333 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
47926f60
KH
12334 /* We've added an opcode byte. */
12335 fragP->fr_fix += 1 + size;
eb19308f
JB
12336 fixP = fix_new (fragP, old_fr_fix + 1, size,
12337 fragP->fr_symbol,
12338 fragP->fr_offset, 1,
12339 reloc_type);
252b5132 12340 break;
fddf5b5b
AM
12341
12342 default:
12343 BAD_CASE (fragP->fr_subtype);
12344 break;
252b5132 12345 }
eb19308f
JB
12346
12347 /* All jumps handled here are signed, but don't unconditionally use a
12348 signed limit check for 32 and 16 bit jumps as we want to allow wrap
12349 around at 4G (outside of 64-bit mode) and 64k. */
12350 if (size == 4 && flag_code == CODE_64BIT)
12351 fixP->fx_signed = 1;
12352
252b5132 12353 frag_wane (fragP);
ee7fcc42 12354 return fragP->fr_fix - old_fr_fix;
252b5132 12355 }
93c2a809 12356
93c2a809
AM
12357 /* Guess size depending on current relax state. Initially the relax
12358 state will correspond to a short jump and we return 1, because
12359 the variable part of the frag (the branch offset) is one byte
12360 long. However, we can relax a section more than once and in that
12361 case we must either set fr_subtype back to the unrelaxed state,
12362 or return the value for the appropriate branch. */
12363 return md_relax_table[fragP->fr_subtype].rlx_length;
ee7fcc42
AM
12364}
12365
47926f60
KH
12366/* Called after relax() is finished.
12367
12368 In: Address of frag.
12369 fr_type == rs_machine_dependent.
12370 fr_subtype is what the address relaxed to.
12371
12372 Out: Any fixSs and constants are set up.
12373 Caller will turn frag into a ".space 0". */
12374
252b5132 12375void
7016a5d5
TG
12376md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec ATTRIBUTE_UNUSED,
12377 fragS *fragP)
252b5132 12378{
29b0f896 12379 unsigned char *opcode;
252b5132 12380 unsigned char *where_to_put_displacement = NULL;
847f7ad4
AM
12381 offsetT target_address;
12382 offsetT opcode_address;
252b5132 12383 unsigned int extension = 0;
847f7ad4 12384 offsetT displacement_from_opcode_start;
252b5132 12385
e379e5f3
L
12386 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12387 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING
12388 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12389 {
12390 /* Generate nop padding. */
12391 unsigned int size = fragP->tc_frag_data.length;
12392 if (size)
12393 {
12394 if (size > fragP->tc_frag_data.max_bytes)
12395 abort ();
12396
12397 if (flag_debug)
12398 {
12399 const char *msg;
12400 const char *branch = "branch";
12401 const char *prefix = "";
12402 fragS *padding_fragP;
12403 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
12404 == BRANCH_PREFIX)
12405 {
12406 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
12407 switch (fragP->tc_frag_data.default_prefix)
12408 {
12409 default:
12410 abort ();
12411 break;
12412 case CS_PREFIX_OPCODE:
12413 prefix = " cs";
12414 break;
12415 case DS_PREFIX_OPCODE:
12416 prefix = " ds";
12417 break;
12418 case ES_PREFIX_OPCODE:
12419 prefix = " es";
12420 break;
12421 case FS_PREFIX_OPCODE:
12422 prefix = " fs";
12423 break;
12424 case GS_PREFIX_OPCODE:
12425 prefix = " gs";
12426 break;
12427 case SS_PREFIX_OPCODE:
12428 prefix = " ss";
12429 break;
12430 }
12431 if (padding_fragP)
12432 msg = _("%s:%u: add %d%s at 0x%llx to align "
12433 "%s within %d-byte boundary\n");
12434 else
12435 msg = _("%s:%u: add additional %d%s at 0x%llx to "
12436 "align %s within %d-byte boundary\n");
12437 }
12438 else
12439 {
12440 padding_fragP = fragP;
12441 msg = _("%s:%u: add %d%s-byte nop at 0x%llx to align "
12442 "%s within %d-byte boundary\n");
12443 }
12444
12445 if (padding_fragP)
12446 switch (padding_fragP->tc_frag_data.branch_type)
12447 {
12448 case align_branch_jcc:
12449 branch = "jcc";
12450 break;
12451 case align_branch_fused:
12452 branch = "fused jcc";
12453 break;
12454 case align_branch_jmp:
12455 branch = "jmp";
12456 break;
12457 case align_branch_call:
12458 branch = "call";
12459 break;
12460 case align_branch_indirect:
12461 branch = "indiret branch";
12462 break;
12463 case align_branch_ret:
12464 branch = "ret";
12465 break;
12466 default:
12467 break;
12468 }
12469
12470 fprintf (stdout, msg,
12471 fragP->fr_file, fragP->fr_line, size, prefix,
12472 (long long) fragP->fr_address, branch,
12473 1 << align_branch_power);
12474 }
12475 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12476 memset (fragP->fr_opcode,
12477 fragP->tc_frag_data.default_prefix, size);
12478 else
12479 i386_generate_nops (fragP, (char *) fragP->fr_opcode,
12480 size, 0);
12481 fragP->fr_fix += size;
12482 }
12483 return;
12484 }
12485
252b5132
RH
12486 opcode = (unsigned char *) fragP->fr_opcode;
12487
47926f60 12488 /* Address we want to reach in file space. */
252b5132 12489 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
252b5132 12490
47926f60 12491 /* Address opcode resides at in file space. */
252b5132
RH
12492 opcode_address = fragP->fr_address + fragP->fr_fix;
12493
47926f60 12494 /* Displacement from opcode start to fill into instruction. */
252b5132
RH
12495 displacement_from_opcode_start = target_address - opcode_address;
12496
fddf5b5b 12497 if ((fragP->fr_subtype & BIG) == 0)
252b5132 12498 {
47926f60
KH
12499 /* Don't have to change opcode. */
12500 extension = 1; /* 1 opcode + 1 displacement */
252b5132 12501 where_to_put_displacement = &opcode[1];
fddf5b5b
AM
12502 }
12503 else
12504 {
12505 if (no_cond_jump_promotion
12506 && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
4eed87de
AM
12507 as_warn_where (fragP->fr_file, fragP->fr_line,
12508 _("long jump required"));
252b5132 12509
fddf5b5b
AM
12510 switch (fragP->fr_subtype)
12511 {
12512 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
12513 extension = 4; /* 1 opcode + 4 displacement */
12514 opcode[0] = 0xe9;
12515 where_to_put_displacement = &opcode[1];
12516 break;
252b5132 12517
fddf5b5b
AM
12518 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
12519 extension = 2; /* 1 opcode + 2 displacement */
12520 opcode[0] = 0xe9;
12521 where_to_put_displacement = &opcode[1];
12522 break;
252b5132 12523
fddf5b5b
AM
12524 case ENCODE_RELAX_STATE (COND_JUMP, BIG):
12525 case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
12526 extension = 5; /* 2 opcode + 4 displacement */
12527 opcode[1] = opcode[0] + 0x10;
12528 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12529 where_to_put_displacement = &opcode[2];
12530 break;
252b5132 12531
fddf5b5b
AM
12532 case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
12533 extension = 3; /* 2 opcode + 2 displacement */
12534 opcode[1] = opcode[0] + 0x10;
12535 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12536 where_to_put_displacement = &opcode[2];
12537 break;
252b5132 12538
fddf5b5b
AM
12539 case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
12540 extension = 4;
12541 opcode[0] ^= 1;
12542 opcode[1] = 3;
12543 opcode[2] = 0xe9;
12544 where_to_put_displacement = &opcode[3];
12545 break;
12546
12547 default:
12548 BAD_CASE (fragP->fr_subtype);
12549 break;
12550 }
252b5132 12551 }
fddf5b5b 12552
7b81dfbb
AJ
12553 /* If size if less then four we are sure that the operand fits,
12554 but if it's 4, then it could be that the displacement is larger
12555 then -/+ 2GB. */
12556 if (DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype) == 4
12557 && object_64bit
12558 && ((addressT) (displacement_from_opcode_start - extension
4eed87de
AM
12559 + ((addressT) 1 << 31))
12560 > (((addressT) 2 << 31) - 1)))
7b81dfbb
AJ
12561 {
12562 as_bad_where (fragP->fr_file, fragP->fr_line,
12563 _("jump target out of range"));
12564 /* Make us emit 0. */
12565 displacement_from_opcode_start = extension;
12566 }
47926f60 12567 /* Now put displacement after opcode. */
252b5132
RH
12568 md_number_to_chars ((char *) where_to_put_displacement,
12569 (valueT) (displacement_from_opcode_start - extension),
fddf5b5b 12570 DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
252b5132
RH
12571 fragP->fr_fix += extension;
12572}
12573\f
7016a5d5 12574/* Apply a fixup (fixP) to segment data, once it has been determined
252b5132
RH
12575 by our caller that we have all the info we need to fix it up.
12576
7016a5d5
TG
12577 Parameter valP is the pointer to the value of the bits.
12578
252b5132
RH
12579 On the 386, immediates, displacements, and data pointers are all in
12580 the same (little-endian) format, so we don't need to care about which
12581 we are handling. */
12582
94f592af 12583void
7016a5d5 12584md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 12585{
94f592af 12586 char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
c6682705 12587 valueT value = *valP;
252b5132 12588
f86103b7 12589#if !defined (TE_Mach)
93382f6d
AM
12590 if (fixP->fx_pcrel)
12591 {
12592 switch (fixP->fx_r_type)
12593 {
5865bb77
ILT
12594 default:
12595 break;
12596
d6ab8113
JB
12597 case BFD_RELOC_64:
12598 fixP->fx_r_type = BFD_RELOC_64_PCREL;
12599 break;
93382f6d 12600 case BFD_RELOC_32:
ae8887b5 12601 case BFD_RELOC_X86_64_32S:
93382f6d
AM
12602 fixP->fx_r_type = BFD_RELOC_32_PCREL;
12603 break;
12604 case BFD_RELOC_16:
12605 fixP->fx_r_type = BFD_RELOC_16_PCREL;
12606 break;
12607 case BFD_RELOC_8:
12608 fixP->fx_r_type = BFD_RELOC_8_PCREL;
12609 break;
12610 }
12611 }
252b5132 12612
a161fe53 12613 if (fixP->fx_addsy != NULL
31312f95 12614 && (fixP->fx_r_type == BFD_RELOC_32_PCREL
d6ab8113 12615 || fixP->fx_r_type == BFD_RELOC_64_PCREL
31312f95 12616 || fixP->fx_r_type == BFD_RELOC_16_PCREL
d258b828 12617 || fixP->fx_r_type == BFD_RELOC_8_PCREL)
31312f95 12618 && !use_rela_relocations)
252b5132 12619 {
31312f95
AM
12620 /* This is a hack. There should be a better way to handle this.
12621 This covers for the fact that bfd_install_relocation will
12622 subtract the current location (for partial_inplace, PC relative
12623 relocations); see more below. */
252b5132 12624#ifndef OBJ_AOUT
718ddfc0 12625 if (IS_ELF
252b5132
RH
12626#ifdef TE_PE
12627 || OUTPUT_FLAVOR == bfd_target_coff_flavour
12628#endif
12629 )
12630 value += fixP->fx_where + fixP->fx_frag->fr_address;
12631#endif
12632#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12633 if (IS_ELF)
252b5132 12634 {
6539b54b 12635 segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
2f66722d 12636
6539b54b 12637 if ((sym_seg == seg
2f66722d 12638 || (symbol_section_p (fixP->fx_addsy)
6539b54b 12639 && sym_seg != absolute_section))
af65af87 12640 && !generic_force_reloc (fixP))
2f66722d
AM
12641 {
12642 /* Yes, we add the values in twice. This is because
6539b54b
AM
12643 bfd_install_relocation subtracts them out again. I think
12644 bfd_install_relocation is broken, but I don't dare change
2f66722d
AM
12645 it. FIXME. */
12646 value += fixP->fx_where + fixP->fx_frag->fr_address;
12647 }
252b5132
RH
12648 }
12649#endif
12650#if defined (OBJ_COFF) && defined (TE_PE)
977cdf5a
NC
12651 /* For some reason, the PE format does not store a
12652 section address offset for a PC relative symbol. */
12653 if (S_GET_SEGMENT (fixP->fx_addsy) != seg
7be1c489 12654 || S_IS_WEAK (fixP->fx_addsy))
252b5132
RH
12655 value += md_pcrel_from (fixP);
12656#endif
12657 }
fbeb56a4 12658#if defined (OBJ_COFF) && defined (TE_PE)
f01c1a09
NC
12659 if (fixP->fx_addsy != NULL
12660 && S_IS_WEAK (fixP->fx_addsy)
12661 /* PR 16858: Do not modify weak function references. */
12662 && ! fixP->fx_pcrel)
fbeb56a4 12663 {
296a8689
NC
12664#if !defined (TE_PEP)
12665 /* For x86 PE weak function symbols are neither PC-relative
12666 nor do they set S_IS_FUNCTION. So the only reliable way
12667 to detect them is to check the flags of their containing
12668 section. */
12669 if (S_GET_SEGMENT (fixP->fx_addsy) != NULL
12670 && S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_CODE)
12671 ;
12672 else
12673#endif
fbeb56a4
DK
12674 value -= S_GET_VALUE (fixP->fx_addsy);
12675 }
12676#endif
252b5132
RH
12677
12678 /* Fix a few things - the dynamic linker expects certain values here,
0234cb7c 12679 and we must not disappoint it. */
252b5132 12680#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12681 if (IS_ELF && fixP->fx_addsy)
47926f60
KH
12682 switch (fixP->fx_r_type)
12683 {
12684 case BFD_RELOC_386_PLT32:
3e73aa7c 12685 case BFD_RELOC_X86_64_PLT32:
b9519cfe
L
12686 /* Make the jump instruction point to the address of the operand.
12687 At runtime we merely add the offset to the actual PLT entry.
12688 NB: Subtract the offset size only for jump instructions. */
12689 if (fixP->fx_pcrel)
12690 value = -4;
47926f60 12691 break;
31312f95 12692
13ae64f3
JJ
12693 case BFD_RELOC_386_TLS_GD:
12694 case BFD_RELOC_386_TLS_LDM:
13ae64f3 12695 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
12696 case BFD_RELOC_386_TLS_IE:
12697 case BFD_RELOC_386_TLS_GOTIE:
67a4f2b7 12698 case BFD_RELOC_386_TLS_GOTDESC:
bffbf940
JJ
12699 case BFD_RELOC_X86_64_TLSGD:
12700 case BFD_RELOC_X86_64_TLSLD:
12701 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7 12702 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
00f7efb6
JJ
12703 value = 0; /* Fully resolved at runtime. No addend. */
12704 /* Fallthrough */
12705 case BFD_RELOC_386_TLS_LE:
12706 case BFD_RELOC_386_TLS_LDO_32:
12707 case BFD_RELOC_386_TLS_LE_32:
12708 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 12709 case BFD_RELOC_X86_64_DTPOFF64:
00f7efb6 12710 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113 12711 case BFD_RELOC_X86_64_TPOFF64:
00f7efb6
JJ
12712 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12713 break;
12714
67a4f2b7
AO
12715 case BFD_RELOC_386_TLS_DESC_CALL:
12716 case BFD_RELOC_X86_64_TLSDESC_CALL:
12717 value = 0; /* Fully resolved at runtime. No addend. */
12718 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12719 fixP->fx_done = 0;
12720 return;
12721
47926f60
KH
12722 case BFD_RELOC_VTABLE_INHERIT:
12723 case BFD_RELOC_VTABLE_ENTRY:
12724 fixP->fx_done = 0;
94f592af 12725 return;
47926f60
KH
12726
12727 default:
12728 break;
12729 }
12730#endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) */
a442cac5
JB
12731
12732 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
12733 if (!object_64bit)
12734 value = extend_to_32bit_address (value);
12735
c6682705 12736 *valP = value;
f86103b7 12737#endif /* !defined (TE_Mach) */
3e73aa7c 12738
3e73aa7c 12739 /* Are we finished with this relocation now? */
c6682705 12740 if (fixP->fx_addsy == NULL)
b8188555
JB
12741 {
12742 fixP->fx_done = 1;
12743 switch (fixP->fx_r_type)
12744 {
12745 case BFD_RELOC_X86_64_32S:
12746 fixP->fx_signed = 1;
12747 break;
12748
12749 default:
12750 break;
12751 }
12752 }
fbeb56a4
DK
12753#if defined (OBJ_COFF) && defined (TE_PE)
12754 else if (fixP->fx_addsy != NULL && S_IS_WEAK (fixP->fx_addsy))
12755 {
12756 fixP->fx_done = 0;
12757 /* Remember value for tc_gen_reloc. */
12758 fixP->fx_addnumber = value;
12759 /* Clear out the frag for now. */
12760 value = 0;
12761 }
12762#endif
3e73aa7c
JH
12763 else if (use_rela_relocations)
12764 {
46fb6d5a
JB
12765 if (!disallow_64bit_reloc || fixP->fx_r_type == NO_RELOC)
12766 fixP->fx_no_overflow = 1;
062cd5e7
AS
12767 /* Remember value for tc_gen_reloc. */
12768 fixP->fx_addnumber = value;
3e73aa7c
JH
12769 value = 0;
12770 }
f86103b7 12771
94f592af 12772 md_number_to_chars (p, value, fixP->fx_size);
252b5132 12773}
252b5132 12774\f
6d4af3c2 12775const char *
499ac353 12776md_atof (int type, char *litP, int *sizeP)
252b5132 12777{
499ac353
NC
12778 /* This outputs the LITTLENUMs in REVERSE order;
12779 in accord with the bigendian 386. */
5b7c81bd 12780 return ieee_md_atof (type, litP, sizeP, false);
252b5132
RH
12781}
12782\f
2d545b82 12783static char output_invalid_buf[sizeof (unsigned char) * 2 + 6];
252b5132 12784
252b5132 12785static char *
e3bb37b5 12786output_invalid (int c)
252b5132 12787{
3882b010 12788 if (ISPRINT (c))
f9f21a03
L
12789 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
12790 "'%c'", c);
252b5132 12791 else
f9f21a03 12792 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
2d545b82 12793 "(0x%x)", (unsigned char) c);
252b5132
RH
12794 return output_invalid_buf;
12795}
12796
8a6fb3f9
JB
12797/* Verify that @r can be used in the current context. */
12798
5b7c81bd 12799static bool check_register (const reg_entry *r)
8a6fb3f9
JB
12800{
12801 if (allow_pseudo_reg)
5b7c81bd 12802 return true;
8a6fb3f9
JB
12803
12804 if (operand_type_all_zero (&r->reg_type))
5b7c81bd 12805 return false;
8a6fb3f9
JB
12806
12807 if ((r->reg_type.bitfield.dword
12808 || (r->reg_type.bitfield.class == SReg && r->reg_num > 3)
12809 || r->reg_type.bitfield.class == RegCR
22e00a3f 12810 || r->reg_type.bitfield.class == RegDR)
8a6fb3f9 12811 && !cpu_arch_flags.bitfield.cpui386)
5b7c81bd 12812 return false;
8a6fb3f9 12813
22e00a3f
JB
12814 if (r->reg_type.bitfield.class == RegTR
12815 && (flag_code == CODE_64BIT
12816 || !cpu_arch_flags.bitfield.cpui386
12817 || cpu_arch_isa_flags.bitfield.cpui586
12818 || cpu_arch_isa_flags.bitfield.cpui686))
5b7c81bd 12819 return false;
22e00a3f 12820
8a6fb3f9 12821 if (r->reg_type.bitfield.class == RegMMX && !cpu_arch_flags.bitfield.cpummx)
5b7c81bd 12822 return false;
8a6fb3f9
JB
12823
12824 if (!cpu_arch_flags.bitfield.cpuavx512f)
12825 {
12826 if (r->reg_type.bitfield.zmmword
12827 || r->reg_type.bitfield.class == RegMask)
5b7c81bd 12828 return false;
8a6fb3f9
JB
12829
12830 if (!cpu_arch_flags.bitfield.cpuavx)
12831 {
12832 if (r->reg_type.bitfield.ymmword)
5b7c81bd 12833 return false;
8a6fb3f9
JB
12834
12835 if (!cpu_arch_flags.bitfield.cpusse && r->reg_type.bitfield.xmmword)
5b7c81bd 12836 return false;
8a6fb3f9
JB
12837 }
12838 }
12839
260cd341
LC
12840 if (r->reg_type.bitfield.tmmword
12841 && (!cpu_arch_flags.bitfield.cpuamx_tile
12842 || flag_code != CODE_64BIT))
5b7c81bd 12843 return false;
260cd341 12844
8a6fb3f9 12845 if (r->reg_type.bitfield.class == RegBND && !cpu_arch_flags.bitfield.cpumpx)
5b7c81bd 12846 return false;
8a6fb3f9
JB
12847
12848 /* Don't allow fake index register unless allow_index_reg isn't 0. */
12849 if (!allow_index_reg && r->reg_num == RegIZ)
5b7c81bd 12850 return false;
8a6fb3f9
JB
12851
12852 /* Upper 16 vector registers are only available with VREX in 64bit
12853 mode, and require EVEX encoding. */
12854 if (r->reg_flags & RegVRex)
12855 {
12856 if (!cpu_arch_flags.bitfield.cpuavx512f
12857 || flag_code != CODE_64BIT)
5b7c81bd 12858 return false;
8a6fb3f9 12859
da4977e0
JB
12860 if (i.vec_encoding == vex_encoding_default)
12861 i.vec_encoding = vex_encoding_evex;
12862 else if (i.vec_encoding != vex_encoding_evex)
12863 i.vec_encoding = vex_encoding_error;
8a6fb3f9
JB
12864 }
12865
12866 if (((r->reg_flags & (RegRex64 | RegRex)) || r->reg_type.bitfield.qword)
12867 && (!cpu_arch_flags.bitfield.cpulm || r->reg_type.bitfield.class != RegCR)
12868 && flag_code != CODE_64BIT)
5b7c81bd 12869 return false;
8a6fb3f9
JB
12870
12871 if (r->reg_type.bitfield.class == SReg && r->reg_num == RegFlat
12872 && !intel_syntax)
5b7c81bd 12873 return false;
8a6fb3f9 12874
5b7c81bd 12875 return true;
8a6fb3f9
JB
12876}
12877
af6bdddf 12878/* REG_STRING starts *before* REGISTER_PREFIX. */
252b5132
RH
12879
12880static const reg_entry *
4d1bb795 12881parse_real_register (char *reg_string, char **end_op)
252b5132 12882{
af6bdddf
AM
12883 char *s = reg_string;
12884 char *p;
252b5132
RH
12885 char reg_name_given[MAX_REG_NAME_SIZE + 1];
12886 const reg_entry *r;
12887
12888 /* Skip possible REGISTER_PREFIX and possible whitespace. */
12889 if (*s == REGISTER_PREFIX)
12890 ++s;
12891
12892 if (is_space_char (*s))
12893 ++s;
12894
12895 p = reg_name_given;
af6bdddf 12896 while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
252b5132
RH
12897 {
12898 if (p >= reg_name_given + MAX_REG_NAME_SIZE)
af6bdddf
AM
12899 return (const reg_entry *) NULL;
12900 s++;
252b5132
RH
12901 }
12902
6588847e
DN
12903 /* For naked regs, make sure that we are not dealing with an identifier.
12904 This prevents confusing an identifier like `eax_var' with register
12905 `eax'. */
12906 if (allow_naked_reg && identifier_chars[(unsigned char) *s])
12907 return (const reg_entry *) NULL;
12908
af6bdddf 12909 *end_op = s;
252b5132 12910
629310ab 12911 r = (const reg_entry *) str_hash_find (reg_hash, reg_name_given);
252b5132 12912
5f47d35b 12913 /* Handle floating point regs, allowing spaces in the (i) part. */
6288d05f 12914 if (r == reg_st0)
5f47d35b 12915 {
0e0eea78
JB
12916 if (!cpu_arch_flags.bitfield.cpu8087
12917 && !cpu_arch_flags.bitfield.cpu287
af32b722
JB
12918 && !cpu_arch_flags.bitfield.cpu387
12919 && !allow_pseudo_reg)
0e0eea78
JB
12920 return (const reg_entry *) NULL;
12921
5f47d35b
AM
12922 if (is_space_char (*s))
12923 ++s;
12924 if (*s == '(')
12925 {
af6bdddf 12926 ++s;
5f47d35b
AM
12927 if (is_space_char (*s))
12928 ++s;
12929 if (*s >= '0' && *s <= '7')
12930 {
db557034 12931 int fpr = *s - '0';
af6bdddf 12932 ++s;
5f47d35b
AM
12933 if (is_space_char (*s))
12934 ++s;
12935 if (*s == ')')
12936 {
12937 *end_op = s + 1;
6288d05f 12938 know (r[fpr].reg_num == fpr);
db557034 12939 return r + fpr;
5f47d35b 12940 }
5f47d35b 12941 }
47926f60 12942 /* We have "%st(" then garbage. */
5f47d35b
AM
12943 return (const reg_entry *) NULL;
12944 }
12945 }
12946
8a6fb3f9 12947 return r && check_register (r) ? r : NULL;
252b5132 12948}
4d1bb795
JB
12949
12950/* REG_STRING starts *before* REGISTER_PREFIX. */
12951
12952static const reg_entry *
12953parse_register (char *reg_string, char **end_op)
12954{
12955 const reg_entry *r;
12956
12957 if (*reg_string == REGISTER_PREFIX || allow_naked_reg)
12958 r = parse_real_register (reg_string, end_op);
12959 else
12960 r = NULL;
12961 if (!r)
12962 {
12963 char *save = input_line_pointer;
12964 char c;
12965 symbolS *symbolP;
12966
12967 input_line_pointer = reg_string;
d02603dc 12968 c = get_symbol_name (&reg_string);
4d1bb795 12969 symbolP = symbol_find (reg_string);
64d23078
JB
12970 while (symbolP && S_GET_SEGMENT (symbolP) != reg_section)
12971 {
12972 const expressionS *e = symbol_get_value_expression(symbolP);
12973
12974 if (e->X_op != O_symbol || e->X_add_number)
12975 break;
12976 symbolP = e->X_add_symbol;
12977 }
4d1bb795
JB
12978 if (symbolP && S_GET_SEGMENT (symbolP) == reg_section)
12979 {
12980 const expressionS *e = symbol_get_value_expression (symbolP);
12981
3b55a1d0
JB
12982 know (e->X_op == O_register);
12983 know (e->X_add_number >= 0
12984 && (valueT) e->X_add_number < i386_regtab_size);
12985 r = i386_regtab + e->X_add_number;
12986 if (!check_register (r))
8a6fb3f9 12987 {
3b55a1d0
JB
12988 as_bad (_("register '%s%s' cannot be used here"),
12989 register_prefix, r->reg_name);
12990 r = &bad_reg;
8a6fb3f9 12991 }
3b55a1d0 12992 *end_op = input_line_pointer;
4d1bb795
JB
12993 }
12994 *input_line_pointer = c;
12995 input_line_pointer = save;
12996 }
12997 return r;
12998}
12999
13000int
13001i386_parse_name (char *name, expressionS *e, char *nextcharP)
13002{
4faaa10f 13003 const reg_entry *r = NULL;
4d1bb795
JB
13004 char *end = input_line_pointer;
13005
13006 *end = *nextcharP;
4faaa10f
JB
13007 if (*name == REGISTER_PREFIX || allow_naked_reg)
13008 r = parse_real_register (name, &input_line_pointer);
4d1bb795
JB
13009 if (r && end <= input_line_pointer)
13010 {
13011 *nextcharP = *input_line_pointer;
13012 *input_line_pointer = 0;
8a6fb3f9
JB
13013 if (r != &bad_reg)
13014 {
13015 e->X_op = O_register;
13016 e->X_add_number = r - i386_regtab;
13017 }
13018 else
13019 e->X_op = O_illegal;
4d1bb795
JB
13020 return 1;
13021 }
13022 input_line_pointer = end;
13023 *end = 0;
ee86248c 13024 return intel_syntax ? i386_intel_parse_name (name, e) : 0;
4d1bb795
JB
13025}
13026
13027void
13028md_operand (expressionS *e)
13029{
ee86248c
JB
13030 char *end;
13031 const reg_entry *r;
4d1bb795 13032
ee86248c
JB
13033 switch (*input_line_pointer)
13034 {
13035 case REGISTER_PREFIX:
13036 r = parse_real_register (input_line_pointer, &end);
4d1bb795
JB
13037 if (r)
13038 {
13039 e->X_op = O_register;
13040 e->X_add_number = r - i386_regtab;
13041 input_line_pointer = end;
13042 }
ee86248c
JB
13043 break;
13044
13045 case '[':
9c2799c2 13046 gas_assert (intel_syntax);
ee86248c
JB
13047 end = input_line_pointer++;
13048 expression (e);
13049 if (*input_line_pointer == ']')
13050 {
13051 ++input_line_pointer;
13052 e->X_op_symbol = make_expr_symbol (e);
13053 e->X_add_symbol = NULL;
13054 e->X_add_number = 0;
13055 e->X_op = O_index;
13056 }
13057 else
13058 {
13059 e->X_op = O_absent;
13060 input_line_pointer = end;
13061 }
13062 break;
4d1bb795
JB
13063 }
13064}
13065
252b5132 13066\f
4cc782b5 13067#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
b6f8c7c4 13068const char *md_shortopts = "kVQ:sqnO::";
252b5132 13069#else
b6f8c7c4 13070const char *md_shortopts = "qnO::";
252b5132 13071#endif
6e0b89ee 13072
3e73aa7c 13073#define OPTION_32 (OPTION_MD_BASE + 0)
b3b91714
AM
13074#define OPTION_64 (OPTION_MD_BASE + 1)
13075#define OPTION_DIVIDE (OPTION_MD_BASE + 2)
9103f4f4
L
13076#define OPTION_MARCH (OPTION_MD_BASE + 3)
13077#define OPTION_MTUNE (OPTION_MD_BASE + 4)
1efbbeb4
L
13078#define OPTION_MMNEMONIC (OPTION_MD_BASE + 5)
13079#define OPTION_MSYNTAX (OPTION_MD_BASE + 6)
13080#define OPTION_MINDEX_REG (OPTION_MD_BASE + 7)
13081#define OPTION_MNAKED_REG (OPTION_MD_BASE + 8)
bd5dea88 13082#define OPTION_MRELAX_RELOCATIONS (OPTION_MD_BASE + 9)
c0f3af97 13083#define OPTION_MSSE2AVX (OPTION_MD_BASE + 10)
daf50ae7 13084#define OPTION_MSSE_CHECK (OPTION_MD_BASE + 11)
7bab8ab5
JB
13085#define OPTION_MOPERAND_CHECK (OPTION_MD_BASE + 12)
13086#define OPTION_MAVXSCALAR (OPTION_MD_BASE + 13)
13087#define OPTION_X32 (OPTION_MD_BASE + 14)
7e8b059b 13088#define OPTION_MADD_BND_PREFIX (OPTION_MD_BASE + 15)
43234a1e
L
13089#define OPTION_MEVEXLIG (OPTION_MD_BASE + 16)
13090#define OPTION_MEVEXWIG (OPTION_MD_BASE + 17)
167ad85b 13091#define OPTION_MBIG_OBJ (OPTION_MD_BASE + 18)
d1982f93 13092#define OPTION_MOMIT_LOCK_PREFIX (OPTION_MD_BASE + 19)
d3d3c6db 13093#define OPTION_MEVEXRCIG (OPTION_MD_BASE + 20)
8dcea932 13094#define OPTION_MSHARED (OPTION_MD_BASE + 21)
5db04b09
L
13095#define OPTION_MAMD64 (OPTION_MD_BASE + 22)
13096#define OPTION_MINTEL64 (OPTION_MD_BASE + 23)
e4e00185 13097#define OPTION_MFENCE_AS_LOCK_ADD (OPTION_MD_BASE + 24)
b4a3a7b4 13098#define OPTION_X86_USED_NOTE (OPTION_MD_BASE + 25)
03751133 13099#define OPTION_MVEXWIG (OPTION_MD_BASE + 26)
e379e5f3
L
13100#define OPTION_MALIGN_BRANCH_BOUNDARY (OPTION_MD_BASE + 27)
13101#define OPTION_MALIGN_BRANCH_PREFIX_SIZE (OPTION_MD_BASE + 28)
13102#define OPTION_MALIGN_BRANCH (OPTION_MD_BASE + 29)
76cf450b 13103#define OPTION_MBRANCHES_WITH_32B_BOUNDARIES (OPTION_MD_BASE + 30)
ae531041
L
13104#define OPTION_MLFENCE_AFTER_LOAD (OPTION_MD_BASE + 31)
13105#define OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH (OPTION_MD_BASE + 32)
13106#define OPTION_MLFENCE_BEFORE_RET (OPTION_MD_BASE + 33)
c8480b58 13107#define OPTION_MUSE_UNALIGNED_VECTOR_MOVE (OPTION_MD_BASE + 34)
b3b91714 13108
99ad8390
NC
13109struct option md_longopts[] =
13110{
3e73aa7c 13111 {"32", no_argument, NULL, OPTION_32},
321098a5 13112#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 13113 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c 13114 {"64", no_argument, NULL, OPTION_64},
351f65ca
L
13115#endif
13116#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 13117 {"x32", no_argument, NULL, OPTION_X32},
8dcea932 13118 {"mshared", no_argument, NULL, OPTION_MSHARED},
b4a3a7b4 13119 {"mx86-used-note", required_argument, NULL, OPTION_X86_USED_NOTE},
6e0b89ee 13120#endif
b3b91714 13121 {"divide", no_argument, NULL, OPTION_DIVIDE},
9103f4f4
L
13122 {"march", required_argument, NULL, OPTION_MARCH},
13123 {"mtune", required_argument, NULL, OPTION_MTUNE},
1efbbeb4
L
13124 {"mmnemonic", required_argument, NULL, OPTION_MMNEMONIC},
13125 {"msyntax", required_argument, NULL, OPTION_MSYNTAX},
13126 {"mindex-reg", no_argument, NULL, OPTION_MINDEX_REG},
13127 {"mnaked-reg", no_argument, NULL, OPTION_MNAKED_REG},
c0f3af97 13128 {"msse2avx", no_argument, NULL, OPTION_MSSE2AVX},
c8480b58 13129 {"muse-unaligned-vector-move", no_argument, NULL, OPTION_MUSE_UNALIGNED_VECTOR_MOVE},
daf50ae7 13130 {"msse-check", required_argument, NULL, OPTION_MSSE_CHECK},
7bab8ab5 13131 {"moperand-check", required_argument, NULL, OPTION_MOPERAND_CHECK},
539f890d 13132 {"mavxscalar", required_argument, NULL, OPTION_MAVXSCALAR},
03751133 13133 {"mvexwig", required_argument, NULL, OPTION_MVEXWIG},
7e8b059b 13134 {"madd-bnd-prefix", no_argument, NULL, OPTION_MADD_BND_PREFIX},
43234a1e
L
13135 {"mevexlig", required_argument, NULL, OPTION_MEVEXLIG},
13136 {"mevexwig", required_argument, NULL, OPTION_MEVEXWIG},
167ad85b
TG
13137# if defined (TE_PE) || defined (TE_PEP)
13138 {"mbig-obj", no_argument, NULL, OPTION_MBIG_OBJ},
13139#endif
d1982f93 13140 {"momit-lock-prefix", required_argument, NULL, OPTION_MOMIT_LOCK_PREFIX},
e4e00185 13141 {"mfence-as-lock-add", required_argument, NULL, OPTION_MFENCE_AS_LOCK_ADD},
0cb4071e 13142 {"mrelax-relocations", required_argument, NULL, OPTION_MRELAX_RELOCATIONS},
d3d3c6db 13143 {"mevexrcig", required_argument, NULL, OPTION_MEVEXRCIG},
e379e5f3
L
13144 {"malign-branch-boundary", required_argument, NULL, OPTION_MALIGN_BRANCH_BOUNDARY},
13145 {"malign-branch-prefix-size", required_argument, NULL, OPTION_MALIGN_BRANCH_PREFIX_SIZE},
13146 {"malign-branch", required_argument, NULL, OPTION_MALIGN_BRANCH},
76cf450b 13147 {"mbranches-within-32B-boundaries", no_argument, NULL, OPTION_MBRANCHES_WITH_32B_BOUNDARIES},
ae531041
L
13148 {"mlfence-after-load", required_argument, NULL, OPTION_MLFENCE_AFTER_LOAD},
13149 {"mlfence-before-indirect-branch", required_argument, NULL,
13150 OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH},
13151 {"mlfence-before-ret", required_argument, NULL, OPTION_MLFENCE_BEFORE_RET},
5db04b09
L
13152 {"mamd64", no_argument, NULL, OPTION_MAMD64},
13153 {"mintel64", no_argument, NULL, OPTION_MINTEL64},
252b5132
RH
13154 {NULL, no_argument, NULL, 0}
13155};
13156size_t md_longopts_size = sizeof (md_longopts);
13157
13158int
17b9d67d 13159md_parse_option (int c, const char *arg)
252b5132 13160{
91d6fa6a 13161 unsigned int j;
e379e5f3 13162 char *arch, *next, *saved, *type;
9103f4f4 13163
252b5132
RH
13164 switch (c)
13165 {
12b55ccc
L
13166 case 'n':
13167 optimize_align_code = 0;
13168 break;
13169
a38cf1db
AM
13170 case 'q':
13171 quiet_warnings = 1;
252b5132
RH
13172 break;
13173
13174#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
a38cf1db
AM
13175 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
13176 should be emitted or not. FIXME: Not implemented. */
13177 case 'Q':
d4693039
JB
13178 if ((arg[0] != 'y' && arg[0] != 'n') || arg[1])
13179 return 0;
252b5132
RH
13180 break;
13181
13182 /* -V: SVR4 argument to print version ID. */
13183 case 'V':
13184 print_version_id ();
13185 break;
13186
a38cf1db
AM
13187 /* -k: Ignore for FreeBSD compatibility. */
13188 case 'k':
252b5132 13189 break;
4cc782b5
ILT
13190
13191 case 's':
13192 /* -s: On i386 Solaris, this tells the native assembler to use
29b0f896 13193 .stab instead of .stab.excl. We always use .stab anyhow. */
4cc782b5 13194 break;
8dcea932
L
13195
13196 case OPTION_MSHARED:
13197 shared = 1;
13198 break;
b4a3a7b4
L
13199
13200 case OPTION_X86_USED_NOTE:
13201 if (strcasecmp (arg, "yes") == 0)
13202 x86_used_note = 1;
13203 else if (strcasecmp (arg, "no") == 0)
13204 x86_used_note = 0;
13205 else
13206 as_fatal (_("invalid -mx86-used-note= option: `%s'"), arg);
13207 break;
13208
13209
99ad8390 13210#endif
321098a5 13211#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 13212 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c
JH
13213 case OPTION_64:
13214 {
13215 const char **list, **l;
13216
3e73aa7c
JH
13217 list = bfd_target_list ();
13218 for (l = list; *l != NULL; l++)
08dedd66 13219 if (startswith (*l, "elf64-x86-64")
99ad8390
NC
13220 || strcmp (*l, "coff-x86-64") == 0
13221 || strcmp (*l, "pe-x86-64") == 0
d382c579
TG
13222 || strcmp (*l, "pei-x86-64") == 0
13223 || strcmp (*l, "mach-o-x86-64") == 0)
6e0b89ee
AM
13224 {
13225 default_arch = "x86_64";
13226 break;
13227 }
3e73aa7c 13228 if (*l == NULL)
2b5d6a91 13229 as_fatal (_("no compiled in support for x86_64"));
3e73aa7c
JH
13230 free (list);
13231 }
13232 break;
13233#endif
252b5132 13234
351f65ca 13235#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 13236 case OPTION_X32:
351f65ca
L
13237 if (IS_ELF)
13238 {
13239 const char **list, **l;
13240
13241 list = bfd_target_list ();
13242 for (l = list; *l != NULL; l++)
08dedd66 13243 if (startswith (*l, "elf32-x86-64"))
351f65ca
L
13244 {
13245 default_arch = "x86_64:32";
13246 break;
13247 }
13248 if (*l == NULL)
2b5d6a91 13249 as_fatal (_("no compiled in support for 32bit x86_64"));
351f65ca
L
13250 free (list);
13251 }
13252 else
13253 as_fatal (_("32bit x86_64 is only supported for ELF"));
13254 break;
13255#endif
13256
6e0b89ee
AM
13257 case OPTION_32:
13258 default_arch = "i386";
13259 break;
13260
b3b91714
AM
13261 case OPTION_DIVIDE:
13262#ifdef SVR4_COMMENT_CHARS
13263 {
13264 char *n, *t;
13265 const char *s;
13266
add39d23 13267 n = XNEWVEC (char, strlen (i386_comment_chars) + 1);
b3b91714
AM
13268 t = n;
13269 for (s = i386_comment_chars; *s != '\0'; s++)
13270 if (*s != '/')
13271 *t++ = *s;
13272 *t = '\0';
13273 i386_comment_chars = n;
13274 }
13275#endif
13276 break;
13277
9103f4f4 13278 case OPTION_MARCH:
293f5f65
L
13279 saved = xstrdup (arg);
13280 arch = saved;
13281 /* Allow -march=+nosse. */
13282 if (*arch == '+')
13283 arch++;
6305a203 13284 do
9103f4f4 13285 {
6305a203 13286 if (*arch == '.')
2b5d6a91 13287 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
13288 next = strchr (arch, '+');
13289 if (next)
13290 *next++ = '\0';
91d6fa6a 13291 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 13292 {
ae89daec
JB
13293 if (arch == saved && cpu_arch[j].type != PROCESSOR_NONE
13294 && strcmp (arch, cpu_arch[j].name) == 0)
ccc9c027 13295 {
6305a203 13296 /* Processor. */
ae89daec 13297 if (! cpu_arch[j].enable.bitfield.cpui386)
1ded5609
JB
13298 continue;
13299
91d6fa6a 13300 cpu_arch_name = cpu_arch[j].name;
d92c7521 13301 free (cpu_sub_arch_name);
6305a203 13302 cpu_sub_arch_name = NULL;
ae89daec 13303 cpu_arch_flags = cpu_arch[j].enable;
91d6fa6a 13304 cpu_arch_isa = cpu_arch[j].type;
ae89daec 13305 cpu_arch_isa_flags = cpu_arch[j].enable;
6305a203
L
13306 if (!cpu_arch_tune_set)
13307 {
13308 cpu_arch_tune = cpu_arch_isa;
13309 cpu_arch_tune_flags = cpu_arch_isa_flags;
13310 }
13311 break;
13312 }
ae89daec
JB
13313 else if (cpu_arch[j].type == PROCESSOR_NONE
13314 && strcmp (arch, cpu_arch[j].name) == 0
13315 && !cpu_flags_all_zero (&cpu_arch[j].enable))
6305a203 13316 {
33eaf5de 13317 /* ISA extension. */
6305a203 13318 i386_cpu_flags flags;
309d3373 13319
293f5f65 13320 flags = cpu_flags_or (cpu_arch_flags,
ae89daec 13321 cpu_arch[j].enable);
81486035 13322
5b64d091 13323 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
6305a203 13324 {
ae89daec 13325 extend_cpu_sub_arch_name (arch);
6305a203 13326 cpu_arch_flags = flags;
a586129e 13327 cpu_arch_isa_flags = flags;
6305a203 13328 }
0089dace
L
13329 else
13330 cpu_arch_isa_flags
13331 = cpu_flags_or (cpu_arch_isa_flags,
ae89daec 13332 cpu_arch[j].enable);
6305a203 13333 break;
ccc9c027 13334 }
9103f4f4 13335 }
6305a203 13336
ae89daec 13337 if (j >= ARRAY_SIZE (cpu_arch) && startswith (arch, "no"))
293f5f65 13338 {
33eaf5de 13339 /* Disable an ISA extension. */
ae89daec
JB
13340 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13341 if (cpu_arch[j].type == PROCESSOR_NONE
13342 && strcmp (arch + 2, cpu_arch[j].name) == 0)
293f5f65
L
13343 {
13344 i386_cpu_flags flags;
13345
13346 flags = cpu_flags_and_not (cpu_arch_flags,
ae89daec 13347 cpu_arch[j].disable);
293f5f65
L
13348 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
13349 {
8180707f 13350 extend_cpu_sub_arch_name (arch);
293f5f65
L
13351 cpu_arch_flags = flags;
13352 cpu_arch_isa_flags = flags;
13353 }
13354 break;
13355 }
293f5f65
L
13356 }
13357
91d6fa6a 13358 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 13359 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
13360
13361 arch = next;
9103f4f4 13362 }
293f5f65
L
13363 while (next != NULL);
13364 free (saved);
9103f4f4
L
13365 break;
13366
13367 case OPTION_MTUNE:
13368 if (*arg == '.')
2b5d6a91 13369 as_fatal (_("invalid -mtune= option: `%s'"), arg);
91d6fa6a 13370 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 13371 {
ae89daec
JB
13372 if (cpu_arch[j].type != PROCESSOR_NONE
13373 && strcmp (arg, cpu_arch[j].name) == 0)
9103f4f4 13374 {
ccc9c027 13375 cpu_arch_tune_set = 1;
91d6fa6a 13376 cpu_arch_tune = cpu_arch [j].type;
ae89daec 13377 cpu_arch_tune_flags = cpu_arch[j].enable;
9103f4f4
L
13378 break;
13379 }
13380 }
91d6fa6a 13381 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 13382 as_fatal (_("invalid -mtune= option: `%s'"), arg);
9103f4f4
L
13383 break;
13384
1efbbeb4
L
13385 case OPTION_MMNEMONIC:
13386 if (strcasecmp (arg, "att") == 0)
13387 intel_mnemonic = 0;
13388 else if (strcasecmp (arg, "intel") == 0)
13389 intel_mnemonic = 1;
13390 else
2b5d6a91 13391 as_fatal (_("invalid -mmnemonic= option: `%s'"), arg);
1efbbeb4
L
13392 break;
13393
13394 case OPTION_MSYNTAX:
13395 if (strcasecmp (arg, "att") == 0)
13396 intel_syntax = 0;
13397 else if (strcasecmp (arg, "intel") == 0)
13398 intel_syntax = 1;
13399 else
2b5d6a91 13400 as_fatal (_("invalid -msyntax= option: `%s'"), arg);
1efbbeb4
L
13401 break;
13402
13403 case OPTION_MINDEX_REG:
13404 allow_index_reg = 1;
13405 break;
13406
13407 case OPTION_MNAKED_REG:
13408 allow_naked_reg = 1;
13409 break;
13410
c0f3af97
L
13411 case OPTION_MSSE2AVX:
13412 sse2avx = 1;
13413 break;
13414
c8480b58
L
13415 case OPTION_MUSE_UNALIGNED_VECTOR_MOVE:
13416 use_unaligned_vector_move = 1;
13417 break;
13418
daf50ae7
L
13419 case OPTION_MSSE_CHECK:
13420 if (strcasecmp (arg, "error") == 0)
7bab8ab5 13421 sse_check = check_error;
daf50ae7 13422 else if (strcasecmp (arg, "warning") == 0)
7bab8ab5 13423 sse_check = check_warning;
daf50ae7 13424 else if (strcasecmp (arg, "none") == 0)
7bab8ab5 13425 sse_check = check_none;
daf50ae7 13426 else
2b5d6a91 13427 as_fatal (_("invalid -msse-check= option: `%s'"), arg);
daf50ae7
L
13428 break;
13429
7bab8ab5
JB
13430 case OPTION_MOPERAND_CHECK:
13431 if (strcasecmp (arg, "error") == 0)
13432 operand_check = check_error;
13433 else if (strcasecmp (arg, "warning") == 0)
13434 operand_check = check_warning;
13435 else if (strcasecmp (arg, "none") == 0)
13436 operand_check = check_none;
13437 else
13438 as_fatal (_("invalid -moperand-check= option: `%s'"), arg);
13439 break;
13440
539f890d
L
13441 case OPTION_MAVXSCALAR:
13442 if (strcasecmp (arg, "128") == 0)
13443 avxscalar = vex128;
13444 else if (strcasecmp (arg, "256") == 0)
13445 avxscalar = vex256;
13446 else
2b5d6a91 13447 as_fatal (_("invalid -mavxscalar= option: `%s'"), arg);
539f890d
L
13448 break;
13449
03751133
L
13450 case OPTION_MVEXWIG:
13451 if (strcmp (arg, "0") == 0)
40c9c8de 13452 vexwig = vexw0;
03751133 13453 else if (strcmp (arg, "1") == 0)
40c9c8de 13454 vexwig = vexw1;
03751133
L
13455 else
13456 as_fatal (_("invalid -mvexwig= option: `%s'"), arg);
13457 break;
13458
7e8b059b
L
13459 case OPTION_MADD_BND_PREFIX:
13460 add_bnd_prefix = 1;
13461 break;
13462
43234a1e
L
13463 case OPTION_MEVEXLIG:
13464 if (strcmp (arg, "128") == 0)
13465 evexlig = evexl128;
13466 else if (strcmp (arg, "256") == 0)
13467 evexlig = evexl256;
13468 else if (strcmp (arg, "512") == 0)
13469 evexlig = evexl512;
13470 else
13471 as_fatal (_("invalid -mevexlig= option: `%s'"), arg);
13472 break;
13473
d3d3c6db
IT
13474 case OPTION_MEVEXRCIG:
13475 if (strcmp (arg, "rne") == 0)
13476 evexrcig = rne;
13477 else if (strcmp (arg, "rd") == 0)
13478 evexrcig = rd;
13479 else if (strcmp (arg, "ru") == 0)
13480 evexrcig = ru;
13481 else if (strcmp (arg, "rz") == 0)
13482 evexrcig = rz;
13483 else
13484 as_fatal (_("invalid -mevexrcig= option: `%s'"), arg);
13485 break;
13486
43234a1e
L
13487 case OPTION_MEVEXWIG:
13488 if (strcmp (arg, "0") == 0)
13489 evexwig = evexw0;
13490 else if (strcmp (arg, "1") == 0)
13491 evexwig = evexw1;
13492 else
13493 as_fatal (_("invalid -mevexwig= option: `%s'"), arg);
13494 break;
13495
167ad85b
TG
13496# if defined (TE_PE) || defined (TE_PEP)
13497 case OPTION_MBIG_OBJ:
13498 use_big_obj = 1;
13499 break;
13500#endif
13501
d1982f93 13502 case OPTION_MOMIT_LOCK_PREFIX:
d022bddd
IT
13503 if (strcasecmp (arg, "yes") == 0)
13504 omit_lock_prefix = 1;
13505 else if (strcasecmp (arg, "no") == 0)
13506 omit_lock_prefix = 0;
13507 else
13508 as_fatal (_("invalid -momit-lock-prefix= option: `%s'"), arg);
13509 break;
13510
e4e00185
AS
13511 case OPTION_MFENCE_AS_LOCK_ADD:
13512 if (strcasecmp (arg, "yes") == 0)
13513 avoid_fence = 1;
13514 else if (strcasecmp (arg, "no") == 0)
13515 avoid_fence = 0;
13516 else
13517 as_fatal (_("invalid -mfence-as-lock-add= option: `%s'"), arg);
13518 break;
13519
ae531041
L
13520 case OPTION_MLFENCE_AFTER_LOAD:
13521 if (strcasecmp (arg, "yes") == 0)
13522 lfence_after_load = 1;
13523 else if (strcasecmp (arg, "no") == 0)
13524 lfence_after_load = 0;
13525 else
13526 as_fatal (_("invalid -mlfence-after-load= option: `%s'"), arg);
13527 break;
13528
13529 case OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH:
13530 if (strcasecmp (arg, "all") == 0)
a09f656b 13531 {
13532 lfence_before_indirect_branch = lfence_branch_all;
13533 if (lfence_before_ret == lfence_before_ret_none)
13534 lfence_before_ret = lfence_before_ret_shl;
13535 }
ae531041
L
13536 else if (strcasecmp (arg, "memory") == 0)
13537 lfence_before_indirect_branch = lfence_branch_memory;
13538 else if (strcasecmp (arg, "register") == 0)
13539 lfence_before_indirect_branch = lfence_branch_register;
13540 else if (strcasecmp (arg, "none") == 0)
13541 lfence_before_indirect_branch = lfence_branch_none;
13542 else
13543 as_fatal (_("invalid -mlfence-before-indirect-branch= option: `%s'"),
13544 arg);
13545 break;
13546
13547 case OPTION_MLFENCE_BEFORE_RET:
13548 if (strcasecmp (arg, "or") == 0)
13549 lfence_before_ret = lfence_before_ret_or;
13550 else if (strcasecmp (arg, "not") == 0)
13551 lfence_before_ret = lfence_before_ret_not;
a09f656b 13552 else if (strcasecmp (arg, "shl") == 0 || strcasecmp (arg, "yes") == 0)
13553 lfence_before_ret = lfence_before_ret_shl;
ae531041
L
13554 else if (strcasecmp (arg, "none") == 0)
13555 lfence_before_ret = lfence_before_ret_none;
13556 else
13557 as_fatal (_("invalid -mlfence-before-ret= option: `%s'"),
13558 arg);
13559 break;
13560
0cb4071e
L
13561 case OPTION_MRELAX_RELOCATIONS:
13562 if (strcasecmp (arg, "yes") == 0)
13563 generate_relax_relocations = 1;
13564 else if (strcasecmp (arg, "no") == 0)
13565 generate_relax_relocations = 0;
13566 else
13567 as_fatal (_("invalid -mrelax-relocations= option: `%s'"), arg);
13568 break;
13569
e379e5f3
L
13570 case OPTION_MALIGN_BRANCH_BOUNDARY:
13571 {
13572 char *end;
13573 long int align = strtoul (arg, &end, 0);
13574 if (*end == '\0')
13575 {
13576 if (align == 0)
13577 {
13578 align_branch_power = 0;
13579 break;
13580 }
13581 else if (align >= 16)
13582 {
13583 int align_power;
13584 for (align_power = 0;
13585 (align & 1) == 0;
13586 align >>= 1, align_power++)
13587 continue;
13588 /* Limit alignment power to 31. */
13589 if (align == 1 && align_power < 32)
13590 {
13591 align_branch_power = align_power;
13592 break;
13593 }
13594 }
13595 }
13596 as_fatal (_("invalid -malign-branch-boundary= value: %s"), arg);
13597 }
13598 break;
13599
13600 case OPTION_MALIGN_BRANCH_PREFIX_SIZE:
13601 {
13602 char *end;
13603 int align = strtoul (arg, &end, 0);
13604 /* Some processors only support 5 prefixes. */
13605 if (*end == '\0' && align >= 0 && align < 6)
13606 {
13607 align_branch_prefix_size = align;
13608 break;
13609 }
13610 as_fatal (_("invalid -malign-branch-prefix-size= value: %s"),
13611 arg);
13612 }
13613 break;
13614
13615 case OPTION_MALIGN_BRANCH:
13616 align_branch = 0;
13617 saved = xstrdup (arg);
13618 type = saved;
13619 do
13620 {
13621 next = strchr (type, '+');
13622 if (next)
13623 *next++ = '\0';
13624 if (strcasecmp (type, "jcc") == 0)
13625 align_branch |= align_branch_jcc_bit;
13626 else if (strcasecmp (type, "fused") == 0)
13627 align_branch |= align_branch_fused_bit;
13628 else if (strcasecmp (type, "jmp") == 0)
13629 align_branch |= align_branch_jmp_bit;
13630 else if (strcasecmp (type, "call") == 0)
13631 align_branch |= align_branch_call_bit;
13632 else if (strcasecmp (type, "ret") == 0)
13633 align_branch |= align_branch_ret_bit;
13634 else if (strcasecmp (type, "indirect") == 0)
13635 align_branch |= align_branch_indirect_bit;
13636 else
13637 as_fatal (_("invalid -malign-branch= option: `%s'"), arg);
13638 type = next;
13639 }
13640 while (next != NULL);
13641 free (saved);
13642 break;
13643
76cf450b
L
13644 case OPTION_MBRANCHES_WITH_32B_BOUNDARIES:
13645 align_branch_power = 5;
13646 align_branch_prefix_size = 5;
13647 align_branch = (align_branch_jcc_bit
13648 | align_branch_fused_bit
13649 | align_branch_jmp_bit);
13650 break;
13651
5db04b09 13652 case OPTION_MAMD64:
4b5aaf5f 13653 isa64 = amd64;
5db04b09
L
13654 break;
13655
13656 case OPTION_MINTEL64:
4b5aaf5f 13657 isa64 = intel64;
5db04b09
L
13658 break;
13659
b6f8c7c4
L
13660 case 'O':
13661 if (arg == NULL)
13662 {
13663 optimize = 1;
13664 /* Turn off -Os. */
13665 optimize_for_space = 0;
13666 }
13667 else if (*arg == 's')
13668 {
13669 optimize_for_space = 1;
13670 /* Turn on all encoding optimizations. */
41fd2579 13671 optimize = INT_MAX;
b6f8c7c4
L
13672 }
13673 else
13674 {
13675 optimize = atoi (arg);
13676 /* Turn off -Os. */
13677 optimize_for_space = 0;
13678 }
13679 break;
13680
252b5132
RH
13681 default:
13682 return 0;
13683 }
13684 return 1;
13685}
13686
8a2c8fef
L
13687#define MESSAGE_TEMPLATE \
13688" "
13689
293f5f65
L
13690static char *
13691output_message (FILE *stream, char *p, char *message, char *start,
13692 int *left_p, const char *name, int len)
13693{
13694 int size = sizeof (MESSAGE_TEMPLATE);
13695 int left = *left_p;
13696
13697 /* Reserve 2 spaces for ", " or ",\0" */
13698 left -= len + 2;
13699
13700 /* Check if there is any room. */
13701 if (left >= 0)
13702 {
13703 if (p != start)
13704 {
13705 *p++ = ',';
13706 *p++ = ' ';
13707 }
13708 p = mempcpy (p, name, len);
13709 }
13710 else
13711 {
13712 /* Output the current message now and start a new one. */
13713 *p++ = ',';
13714 *p = '\0';
13715 fprintf (stream, "%s\n", message);
13716 p = start;
13717 left = size - (start - message) - len - 2;
13718
13719 gas_assert (left >= 0);
13720
13721 p = mempcpy (p, name, len);
13722 }
13723
13724 *left_p = left;
13725 return p;
13726}
13727
8a2c8fef 13728static void
1ded5609 13729show_arch (FILE *stream, int ext, int check)
8a2c8fef
L
13730{
13731 static char message[] = MESSAGE_TEMPLATE;
13732 char *start = message + 27;
13733 char *p;
13734 int size = sizeof (MESSAGE_TEMPLATE);
13735 int left;
13736 const char *name;
13737 int len;
13738 unsigned int j;
13739
13740 p = start;
13741 left = size - (start - message);
3ce2ebcf
JB
13742
13743 if (!ext && check)
13744 {
13745 p = output_message (stream, p, message, start, &left,
13746 STRING_COMMA_LEN ("default"));
f68697e8
JB
13747 p = output_message (stream, p, message, start, &left,
13748 STRING_COMMA_LEN ("push"));
13749 p = output_message (stream, p, message, start, &left,
13750 STRING_COMMA_LEN ("pop"));
3ce2ebcf
JB
13751 }
13752
8a2c8fef
L
13753 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13754 {
13755 /* Should it be skipped? */
13756 if (cpu_arch [j].skip)
13757 continue;
13758
13759 name = cpu_arch [j].name;
13760 len = cpu_arch [j].len;
ae89daec 13761 if (cpu_arch[j].type == PROCESSOR_NONE)
8a2c8fef
L
13762 {
13763 /* It is an extension. Skip if we aren't asked to show it. */
ae89daec 13764 if (!ext || cpu_flags_all_zero (&cpu_arch[j].enable))
8a2c8fef
L
13765 continue;
13766 }
13767 else if (ext)
13768 {
13769 /* It is an processor. Skip if we show only extension. */
13770 continue;
13771 }
ae89daec 13772 else if (check && ! cpu_arch[j].enable.bitfield.cpui386)
1ded5609
JB
13773 {
13774 /* It is an impossible processor - skip. */
13775 continue;
13776 }
8a2c8fef 13777
293f5f65 13778 p = output_message (stream, p, message, start, &left, name, len);
8a2c8fef
L
13779 }
13780
293f5f65
L
13781 /* Display disabled extensions. */
13782 if (ext)
ae89daec 13783 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
293f5f65 13784 {
ae89daec
JB
13785 char *str;
13786
13787 if (cpu_arch[j].type != PROCESSOR_NONE
13788 || !cpu_flags_all_zero (&cpu_arch[j].enable))
13789 continue;
13790 str = xasprintf ("no%s", cpu_arch[j].name);
13791 p = output_message (stream, p, message, start, &left, str,
13792 strlen (str));
13793 free (str);
293f5f65
L
13794 }
13795
8a2c8fef
L
13796 *p = '\0';
13797 fprintf (stream, "%s\n", message);
13798}
13799
252b5132 13800void
8a2c8fef 13801md_show_usage (FILE *stream)
252b5132 13802{
4cc782b5
ILT
13803#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13804 fprintf (stream, _("\
d4693039 13805 -Qy, -Qn ignored\n\
a38cf1db 13806 -V print assembler version number\n\
b3b91714
AM
13807 -k ignored\n"));
13808#endif
13809 fprintf (stream, _("\
7ebd68d1
NC
13810 -n do not optimize code alignment\n\
13811 -O{012s} attempt some code optimizations\n\
b3b91714
AM
13812 -q quieten some warnings\n"));
13813#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13814 fprintf (stream, _("\
a38cf1db 13815 -s ignored\n"));
b3b91714 13816#endif
b00af7c8
JB
13817#ifdef BFD64
13818# if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13819 fprintf (stream, _("\
13820 --32/--64/--x32 generate 32bit/64bit/x32 object\n"));
13821# elif defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O)
751d281c 13822 fprintf (stream, _("\
b00af7c8
JB
13823 --32/--64 generate 32bit/64bit object\n"));
13824# endif
751d281c 13825#endif
b3b91714
AM
13826#ifdef SVR4_COMMENT_CHARS
13827 fprintf (stream, _("\
13828 --divide do not treat `/' as a comment character\n"));
a38cf1db
AM
13829#else
13830 fprintf (stream, _("\
b3b91714 13831 --divide ignored\n"));
4cc782b5 13832#endif
9103f4f4 13833 fprintf (stream, _("\
6305a203 13834 -march=CPU[,+EXTENSION...]\n\
8a2c8fef 13835 generate code for CPU and EXTENSION, CPU is one of:\n"));
1ded5609 13836 show_arch (stream, 0, 1);
8a2c8fef 13837 fprintf (stream, _("\
ae89daec 13838 EXTENSION is combination of (possibly \"no\"-prefixed):\n"));
1ded5609 13839 show_arch (stream, 1, 0);
6305a203 13840 fprintf (stream, _("\
8a2c8fef 13841 -mtune=CPU optimize for CPU, CPU is one of:\n"));
1ded5609 13842 show_arch (stream, 0, 0);
ba104c83 13843 fprintf (stream, _("\
c0f3af97
L
13844 -msse2avx encode SSE instructions with VEX prefix\n"));
13845 fprintf (stream, _("\
c8480b58
L
13846 -muse-unaligned-vector-move\n\
13847 encode aligned vector move as unaligned vector move\n"));
13848 fprintf (stream, _("\
7c5c05ef 13849 -msse-check=[none|error|warning] (default: warning)\n\
daf50ae7
L
13850 check SSE instructions\n"));
13851 fprintf (stream, _("\
7c5c05ef 13852 -moperand-check=[none|error|warning] (default: warning)\n\
7bab8ab5
JB
13853 check operand combinations for validity\n"));
13854 fprintf (stream, _("\
7c5c05ef
L
13855 -mavxscalar=[128|256] (default: 128)\n\
13856 encode scalar AVX instructions with specific vector\n\
539f890d
L
13857 length\n"));
13858 fprintf (stream, _("\
03751133
L
13859 -mvexwig=[0|1] (default: 0)\n\
13860 encode VEX instructions with specific VEX.W value\n\
13861 for VEX.W bit ignored instructions\n"));
13862 fprintf (stream, _("\
7c5c05ef
L
13863 -mevexlig=[128|256|512] (default: 128)\n\
13864 encode scalar EVEX instructions with specific vector\n\
43234a1e
L
13865 length\n"));
13866 fprintf (stream, _("\
7c5c05ef
L
13867 -mevexwig=[0|1] (default: 0)\n\
13868 encode EVEX instructions with specific EVEX.W value\n\
43234a1e
L
13869 for EVEX.W bit ignored instructions\n"));
13870 fprintf (stream, _("\
7c5c05ef 13871 -mevexrcig=[rne|rd|ru|rz] (default: rne)\n\
d3d3c6db
IT
13872 encode EVEX instructions with specific EVEX.RC value\n\
13873 for SAE-only ignored instructions\n"));
13874 fprintf (stream, _("\
7c5c05ef
L
13875 -mmnemonic=[att|intel] "));
13876 if (SYSV386_COMPAT)
13877 fprintf (stream, _("(default: att)\n"));
13878 else
13879 fprintf (stream, _("(default: intel)\n"));
13880 fprintf (stream, _("\
13881 use AT&T/Intel mnemonic\n"));
ba104c83 13882 fprintf (stream, _("\
7c5c05ef
L
13883 -msyntax=[att|intel] (default: att)\n\
13884 use AT&T/Intel syntax\n"));
ba104c83
L
13885 fprintf (stream, _("\
13886 -mindex-reg support pseudo index registers\n"));
13887 fprintf (stream, _("\
13888 -mnaked-reg don't require `%%' prefix for registers\n"));
13889 fprintf (stream, _("\
7e8b059b 13890 -madd-bnd-prefix add BND prefix for all valid branches\n"));
b4a3a7b4 13891#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8dcea932
L
13892 fprintf (stream, _("\
13893 -mshared disable branch optimization for shared code\n"));
b4a3a7b4
L
13894 fprintf (stream, _("\
13895 -mx86-used-note=[no|yes] "));
13896 if (DEFAULT_X86_USED_NOTE)
13897 fprintf (stream, _("(default: yes)\n"));
13898 else
13899 fprintf (stream, _("(default: no)\n"));
13900 fprintf (stream, _("\
13901 generate x86 used ISA and feature properties\n"));
13902#endif
13903#if defined (TE_PE) || defined (TE_PEP)
167ad85b
TG
13904 fprintf (stream, _("\
13905 -mbig-obj generate big object files\n"));
13906#endif
d022bddd 13907 fprintf (stream, _("\
7c5c05ef 13908 -momit-lock-prefix=[no|yes] (default: no)\n\
d022bddd 13909 strip all lock prefixes\n"));
5db04b09 13910 fprintf (stream, _("\
7c5c05ef 13911 -mfence-as-lock-add=[no|yes] (default: no)\n\
e4e00185
AS
13912 encode lfence, mfence and sfence as\n\
13913 lock addl $0x0, (%%{re}sp)\n"));
13914 fprintf (stream, _("\
7c5c05ef
L
13915 -mrelax-relocations=[no|yes] "));
13916 if (DEFAULT_GENERATE_X86_RELAX_RELOCATIONS)
13917 fprintf (stream, _("(default: yes)\n"));
13918 else
13919 fprintf (stream, _("(default: no)\n"));
13920 fprintf (stream, _("\
0cb4071e
L
13921 generate relax relocations\n"));
13922 fprintf (stream, _("\
e379e5f3
L
13923 -malign-branch-boundary=NUM (default: 0)\n\
13924 align branches within NUM byte boundary\n"));
13925 fprintf (stream, _("\
13926 -malign-branch=TYPE[+TYPE...] (default: jcc+fused+jmp)\n\
13927 TYPE is combination of jcc, fused, jmp, call, ret,\n\
13928 indirect\n\
13929 specify types of branches to align\n"));
13930 fprintf (stream, _("\
13931 -malign-branch-prefix-size=NUM (default: 5)\n\
13932 align branches with NUM prefixes per instruction\n"));
13933 fprintf (stream, _("\
76cf450b
L
13934 -mbranches-within-32B-boundaries\n\
13935 align branches within 32 byte boundary\n"));
13936 fprintf (stream, _("\
ae531041
L
13937 -mlfence-after-load=[no|yes] (default: no)\n\
13938 generate lfence after load\n"));
13939 fprintf (stream, _("\
13940 -mlfence-before-indirect-branch=[none|all|register|memory] (default: none)\n\
13941 generate lfence before indirect near branch\n"));
13942 fprintf (stream, _("\
a09f656b 13943 -mlfence-before-ret=[none|or|not|shl|yes] (default: none)\n\
ae531041
L
13944 generate lfence before ret\n"));
13945 fprintf (stream, _("\
7c5c05ef 13946 -mamd64 accept only AMD64 ISA [default]\n"));
5db04b09
L
13947 fprintf (stream, _("\
13948 -mintel64 accept only Intel64 ISA\n"));
252b5132
RH
13949}
13950
3e73aa7c 13951#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
321098a5 13952 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
e57f8c65 13953 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
252b5132
RH
13954
13955/* Pick the target format to use. */
13956
47926f60 13957const char *
e3bb37b5 13958i386_target_format (void)
252b5132 13959{
d34049e8 13960 if (startswith (default_arch, "x86_64"))
351f65ca
L
13961 {
13962 update_code_flag (CODE_64BIT, 1);
13963 if (default_arch[6] == '\0')
7f56bc95 13964 x86_elf_abi = X86_64_ABI;
351f65ca 13965 else
7f56bc95 13966 x86_elf_abi = X86_64_X32_ABI;
351f65ca 13967 }
3e73aa7c 13968 else if (!strcmp (default_arch, "i386"))
78f12dd3 13969 update_code_flag (CODE_32BIT, 1);
5197d474
L
13970 else if (!strcmp (default_arch, "iamcu"))
13971 {
13972 update_code_flag (CODE_32BIT, 1);
13973 if (cpu_arch_isa == PROCESSOR_UNKNOWN)
13974 {
13975 static const i386_cpu_flags iamcu_flags = CPU_IAMCU_FLAGS;
13976 cpu_arch_name = "iamcu";
d92c7521 13977 free (cpu_sub_arch_name);
5197d474
L
13978 cpu_sub_arch_name = NULL;
13979 cpu_arch_flags = iamcu_flags;
13980 cpu_arch_isa = PROCESSOR_IAMCU;
13981 cpu_arch_isa_flags = iamcu_flags;
13982 if (!cpu_arch_tune_set)
13983 {
13984 cpu_arch_tune = cpu_arch_isa;
13985 cpu_arch_tune_flags = cpu_arch_isa_flags;
13986 }
13987 }
8d471ec1 13988 else if (cpu_arch_isa != PROCESSOR_IAMCU)
5197d474
L
13989 as_fatal (_("Intel MCU doesn't support `%s' architecture"),
13990 cpu_arch_name);
13991 }
3e73aa7c 13992 else
2b5d6a91 13993 as_fatal (_("unknown architecture"));
89507696
JB
13994
13995 if (cpu_flags_all_zero (&cpu_arch_isa_flags))
ae89daec 13996 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
89507696 13997 if (cpu_flags_all_zero (&cpu_arch_tune_flags))
ae89daec 13998 cpu_arch_tune_flags = cpu_arch[flag_code == CODE_64BIT].enable;
89507696 13999
252b5132
RH
14000 switch (OUTPUT_FLAVOR)
14001 {
9384f2ff 14002#if defined (OBJ_MAYBE_AOUT) || defined (OBJ_AOUT)
4c63da97 14003 case bfd_target_aout_flavour:
47926f60 14004 return AOUT_TARGET_FORMAT;
4c63da97 14005#endif
9384f2ff
AM
14006#if defined (OBJ_MAYBE_COFF) || defined (OBJ_COFF)
14007# if defined (TE_PE) || defined (TE_PEP)
14008 case bfd_target_coff_flavour:
167ad85b 14009 if (flag_code == CODE_64BIT)
eb19308f
JB
14010 {
14011 object_64bit = 1;
14012 return use_big_obj ? "pe-bigobj-x86-64" : "pe-x86-64";
14013 }
14014 return use_big_obj ? "pe-bigobj-i386" : "pe-i386";
9384f2ff 14015# elif defined (TE_GO32)
0561d57c
JK
14016 case bfd_target_coff_flavour:
14017 return "coff-go32";
9384f2ff 14018# else
252b5132
RH
14019 case bfd_target_coff_flavour:
14020 return "coff-i386";
9384f2ff 14021# endif
4c63da97 14022#endif
3e73aa7c 14023#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
252b5132 14024 case bfd_target_elf_flavour:
3e73aa7c 14025 {
351f65ca
L
14026 const char *format;
14027
14028 switch (x86_elf_abi)
4fa24527 14029 {
351f65ca
L
14030 default:
14031 format = ELF_TARGET_FORMAT;
e379e5f3
L
14032#ifndef TE_SOLARIS
14033 tls_get_addr = "___tls_get_addr";
14034#endif
351f65ca 14035 break;
7f56bc95 14036 case X86_64_ABI:
351f65ca 14037 use_rela_relocations = 1;
4fa24527 14038 object_64bit = 1;
e379e5f3
L
14039#ifndef TE_SOLARIS
14040 tls_get_addr = "__tls_get_addr";
14041#endif
351f65ca
L
14042 format = ELF_TARGET_FORMAT64;
14043 break;
7f56bc95 14044 case X86_64_X32_ABI:
4fa24527 14045 use_rela_relocations = 1;
351f65ca 14046 object_64bit = 1;
e379e5f3
L
14047#ifndef TE_SOLARIS
14048 tls_get_addr = "__tls_get_addr";
14049#endif
862be3fb 14050 disallow_64bit_reloc = 1;
351f65ca
L
14051 format = ELF_TARGET_FORMAT32;
14052 break;
4fa24527 14053 }
c085ab00 14054 if (cpu_arch_isa == PROCESSOR_IAMCU)
81486035
L
14055 {
14056 if (x86_elf_abi != I386_ABI)
14057 as_fatal (_("Intel MCU is 32bit only"));
14058 return ELF_TARGET_IAMCU_FORMAT;
14059 }
8a9036a4 14060 else
351f65ca 14061 return format;
3e73aa7c 14062 }
e57f8c65
TG
14063#endif
14064#if defined (OBJ_MACH_O)
14065 case bfd_target_mach_o_flavour:
d382c579
TG
14066 if (flag_code == CODE_64BIT)
14067 {
14068 use_rela_relocations = 1;
14069 object_64bit = 1;
14070 return "mach-o-x86-64";
14071 }
14072 else
14073 return "mach-o-i386";
4c63da97 14074#endif
252b5132
RH
14075 default:
14076 abort ();
14077 return NULL;
14078 }
14079}
14080
47926f60 14081#endif /* OBJ_MAYBE_ more than one */
252b5132 14082\f
252b5132 14083symbolS *
7016a5d5 14084md_undefined_symbol (char *name)
252b5132 14085{
18dc2407
ILT
14086 if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
14087 && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
14088 && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
14089 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
24eab124
AM
14090 {
14091 if (!GOT_symbol)
14092 {
14093 if (symbol_find (name))
14094 as_bad (_("GOT already in symbol table"));
14095 GOT_symbol = symbol_new (name, undefined_section,
e01e1cee 14096 &zero_address_frag, 0);
24eab124
AM
14097 };
14098 return GOT_symbol;
14099 }
252b5132
RH
14100 return 0;
14101}
14102
14103/* Round up a section size to the appropriate boundary. */
47926f60 14104
252b5132 14105valueT
7016a5d5 14106md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
252b5132 14107{
4c63da97
AM
14108#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
14109 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
14110 {
14111 /* For a.out, force the section size to be aligned. If we don't do
14112 this, BFD will align it for us, but it will not write out the
14113 final bytes of the section. This may be a bug in BFD, but it is
14114 easier to fix it here since that is how the other a.out targets
14115 work. */
14116 int align;
14117
fd361982 14118 align = bfd_section_alignment (segment);
8d3842cd 14119 size = ((size + (1 << align) - 1) & (-((valueT) 1 << align)));
4c63da97 14120 }
252b5132
RH
14121#endif
14122
14123 return size;
14124}
14125
14126/* On the i386, PC-relative offsets are relative to the start of the
14127 next instruction. That is, the address of the offset, plus its
14128 size, since the offset is always the last part of the insn. */
14129
14130long
e3bb37b5 14131md_pcrel_from (fixS *fixP)
252b5132
RH
14132{
14133 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
14134}
14135
14136#ifndef I386COFF
14137
14138static void
e3bb37b5 14139s_bss (int ignore ATTRIBUTE_UNUSED)
252b5132 14140{
29b0f896 14141 int temp;
252b5132 14142
8a75718c
JB
14143#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14144 if (IS_ELF)
14145 obj_elf_section_change_hook ();
14146#endif
252b5132
RH
14147 temp = get_absolute_expression ();
14148 subseg_set (bss_section, (subsegT) temp);
14149 demand_empty_rest_of_line ();
14150}
14151
14152#endif
14153
e379e5f3
L
14154/* Remember constant directive. */
14155
14156void
14157i386_cons_align (int ignore ATTRIBUTE_UNUSED)
14158{
14159 if (last_insn.kind != last_insn_directive
14160 && (bfd_section_flags (now_seg) & SEC_CODE))
14161 {
14162 last_insn.seg = now_seg;
14163 last_insn.kind = last_insn_directive;
14164 last_insn.name = "constant directive";
14165 last_insn.file = as_where (&last_insn.line);
ae531041
L
14166 if (lfence_before_ret != lfence_before_ret_none)
14167 {
14168 if (lfence_before_indirect_branch != lfence_branch_none)
14169 as_warn (_("constant directive skips -mlfence-before-ret "
14170 "and -mlfence-before-indirect-branch"));
14171 else
14172 as_warn (_("constant directive skips -mlfence-before-ret"));
14173 }
14174 else if (lfence_before_indirect_branch != lfence_branch_none)
14175 as_warn (_("constant directive skips -mlfence-before-indirect-branch"));
e379e5f3
L
14176 }
14177}
14178
3abbafc2 14179int
e3bb37b5 14180i386_validate_fix (fixS *fixp)
252b5132 14181{
e52a16f2
JB
14182 if (fixp->fx_addsy && S_GET_SEGMENT(fixp->fx_addsy) == reg_section)
14183 {
14184 reloc_howto_type *howto;
14185
14186 howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
14187 as_bad_where (fixp->fx_file, fixp->fx_line,
14188 _("invalid %s relocation against register"),
14189 howto ? howto->name : "<unknown>");
14190 return 0;
14191 }
14192
3abbafc2
JB
14193#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14194 if (fixp->fx_r_type == BFD_RELOC_SIZE32
14195 || fixp->fx_r_type == BFD_RELOC_SIZE64)
14196 return IS_ELF && fixp->fx_addsy
14197 && (!S_IS_DEFINED (fixp->fx_addsy)
14198 || S_IS_EXTERNAL (fixp->fx_addsy));
14199#endif
14200
02a86693 14201 if (fixp->fx_subsy)
252b5132 14202 {
02a86693 14203 if (fixp->fx_subsy == GOT_symbol)
23df1078 14204 {
02a86693
L
14205 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
14206 {
14207 if (!object_64bit)
14208 abort ();
14209#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14210 if (fixp->fx_tcbit2)
56ceb5b5
L
14211 fixp->fx_r_type = (fixp->fx_tcbit
14212 ? BFD_RELOC_X86_64_REX_GOTPCRELX
14213 : BFD_RELOC_X86_64_GOTPCRELX);
02a86693
L
14214 else
14215#endif
14216 fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
14217 }
d6ab8113 14218 else
02a86693
L
14219 {
14220 if (!object_64bit)
14221 fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
14222 else
14223 fixp->fx_r_type = BFD_RELOC_X86_64_GOTOFF64;
14224 }
14225 fixp->fx_subsy = 0;
23df1078 14226 }
252b5132 14227 }
02a86693 14228#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2585b7a5 14229 else
02a86693 14230 {
2585b7a5
L
14231 /* NB: Commit 292676c1 resolved PLT32 reloc aganst local symbol
14232 to section. Since PLT32 relocation must be against symbols,
14233 turn such PLT32 relocation into PC32 relocation. */
14234 if (fixp->fx_addsy
14235 && (fixp->fx_r_type == BFD_RELOC_386_PLT32
14236 || fixp->fx_r_type == BFD_RELOC_X86_64_PLT32)
14237 && symbol_section_p (fixp->fx_addsy))
14238 fixp->fx_r_type = BFD_RELOC_32_PCREL;
14239 if (!object_64bit)
14240 {
14241 if (fixp->fx_r_type == BFD_RELOC_386_GOT32
14242 && fixp->fx_tcbit2)
14243 fixp->fx_r_type = BFD_RELOC_386_GOT32X;
14244 }
02a86693
L
14245 }
14246#endif
3abbafc2
JB
14247
14248 return 1;
252b5132
RH
14249}
14250
252b5132 14251arelent *
7016a5d5 14252tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
14253{
14254 arelent *rel;
14255 bfd_reloc_code_real_type code;
14256
14257 switch (fixp->fx_r_type)
14258 {
8ce3d284 14259#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3abbafc2
JB
14260 symbolS *sym;
14261
8fd4256d
L
14262 case BFD_RELOC_SIZE32:
14263 case BFD_RELOC_SIZE64:
3abbafc2
JB
14264 if (fixp->fx_addsy
14265 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))
14266 && (!fixp->fx_subsy
14267 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))))
14268 sym = fixp->fx_addsy;
14269 else if (fixp->fx_subsy
14270 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))
14271 && (!fixp->fx_addsy
14272 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))))
14273 sym = fixp->fx_subsy;
14274 else
14275 sym = NULL;
14276 if (IS_ELF && sym && S_IS_DEFINED (sym) && !S_IS_EXTERNAL (sym))
8fd4256d
L
14277 {
14278 /* Resolve size relocation against local symbol to size of
14279 the symbol plus addend. */
3abbafc2 14280 valueT value = S_GET_SIZE (sym);
44f87162 14281
3abbafc2
JB
14282 if (symbol_get_bfdsym (sym)->flags & BSF_SECTION_SYM)
14283 value = bfd_section_size (S_GET_SEGMENT (sym));
14284 if (sym == fixp->fx_subsy)
14285 {
14286 value = -value;
14287 if (fixp->fx_addsy)
14288 value += S_GET_VALUE (fixp->fx_addsy);
14289 }
14290 else if (fixp->fx_subsy)
14291 value -= S_GET_VALUE (fixp->fx_subsy);
44f87162 14292 value += fixp->fx_offset;
8fd4256d 14293 if (fixp->fx_r_type == BFD_RELOC_SIZE32
d965814f 14294 && object_64bit
8fd4256d
L
14295 && !fits_in_unsigned_long (value))
14296 as_bad_where (fixp->fx_file, fixp->fx_line,
14297 _("symbol size computation overflow"));
14298 fixp->fx_addsy = NULL;
14299 fixp->fx_subsy = NULL;
14300 md_apply_fix (fixp, (valueT *) &value, NULL);
14301 return NULL;
14302 }
3abbafc2
JB
14303 if (!fixp->fx_addsy || fixp->fx_subsy)
14304 {
14305 as_bad_where (fixp->fx_file, fixp->fx_line,
14306 "unsupported expression involving @size");
14307 return NULL;
14308 }
8ce3d284 14309#endif
1a0670f3 14310 /* Fall through. */
8fd4256d 14311
3e73aa7c
JH
14312 case BFD_RELOC_X86_64_PLT32:
14313 case BFD_RELOC_X86_64_GOT32:
14314 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
14315 case BFD_RELOC_X86_64_GOTPCRELX:
14316 case BFD_RELOC_X86_64_REX_GOTPCRELX:
252b5132
RH
14317 case BFD_RELOC_386_PLT32:
14318 case BFD_RELOC_386_GOT32:
02a86693 14319 case BFD_RELOC_386_GOT32X:
252b5132
RH
14320 case BFD_RELOC_386_GOTOFF:
14321 case BFD_RELOC_386_GOTPC:
13ae64f3
JJ
14322 case BFD_RELOC_386_TLS_GD:
14323 case BFD_RELOC_386_TLS_LDM:
14324 case BFD_RELOC_386_TLS_LDO_32:
14325 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
14326 case BFD_RELOC_386_TLS_IE:
14327 case BFD_RELOC_386_TLS_GOTIE:
13ae64f3
JJ
14328 case BFD_RELOC_386_TLS_LE_32:
14329 case BFD_RELOC_386_TLS_LE:
67a4f2b7
AO
14330 case BFD_RELOC_386_TLS_GOTDESC:
14331 case BFD_RELOC_386_TLS_DESC_CALL:
bffbf940
JJ
14332 case BFD_RELOC_X86_64_TLSGD:
14333 case BFD_RELOC_X86_64_TLSLD:
14334 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 14335 case BFD_RELOC_X86_64_DTPOFF64:
bffbf940
JJ
14336 case BFD_RELOC_X86_64_GOTTPOFF:
14337 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113
JB
14338 case BFD_RELOC_X86_64_TPOFF64:
14339 case BFD_RELOC_X86_64_GOTOFF64:
14340 case BFD_RELOC_X86_64_GOTPC32:
7b81dfbb
AJ
14341 case BFD_RELOC_X86_64_GOT64:
14342 case BFD_RELOC_X86_64_GOTPCREL64:
14343 case BFD_RELOC_X86_64_GOTPC64:
14344 case BFD_RELOC_X86_64_GOTPLT64:
14345 case BFD_RELOC_X86_64_PLTOFF64:
67a4f2b7
AO
14346 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14347 case BFD_RELOC_X86_64_TLSDESC_CALL:
252b5132
RH
14348 case BFD_RELOC_RVA:
14349 case BFD_RELOC_VTABLE_ENTRY:
14350 case BFD_RELOC_VTABLE_INHERIT:
6482c264
NC
14351#ifdef TE_PE
14352 case BFD_RELOC_32_SECREL:
145667f8 14353 case BFD_RELOC_16_SECIDX:
6482c264 14354#endif
252b5132
RH
14355 code = fixp->fx_r_type;
14356 break;
dbbaec26
L
14357 case BFD_RELOC_X86_64_32S:
14358 if (!fixp->fx_pcrel)
14359 {
14360 /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32. */
14361 code = fixp->fx_r_type;
14362 break;
14363 }
1a0670f3 14364 /* Fall through. */
252b5132 14365 default:
93382f6d 14366 if (fixp->fx_pcrel)
252b5132 14367 {
93382f6d
AM
14368 switch (fixp->fx_size)
14369 {
14370 default:
b091f402
AM
14371 as_bad_where (fixp->fx_file, fixp->fx_line,
14372 _("can not do %d byte pc-relative relocation"),
14373 fixp->fx_size);
93382f6d
AM
14374 code = BFD_RELOC_32_PCREL;
14375 break;
14376 case 1: code = BFD_RELOC_8_PCREL; break;
14377 case 2: code = BFD_RELOC_16_PCREL; break;
d258b828 14378 case 4: code = BFD_RELOC_32_PCREL; break;
d6ab8113
JB
14379#ifdef BFD64
14380 case 8: code = BFD_RELOC_64_PCREL; break;
14381#endif
93382f6d
AM
14382 }
14383 }
14384 else
14385 {
14386 switch (fixp->fx_size)
14387 {
14388 default:
b091f402
AM
14389 as_bad_where (fixp->fx_file, fixp->fx_line,
14390 _("can not do %d byte relocation"),
14391 fixp->fx_size);
93382f6d
AM
14392 code = BFD_RELOC_32;
14393 break;
14394 case 1: code = BFD_RELOC_8; break;
14395 case 2: code = BFD_RELOC_16; break;
14396 case 4: code = BFD_RELOC_32; break;
937149dd 14397#ifdef BFD64
3e73aa7c 14398 case 8: code = BFD_RELOC_64; break;
937149dd 14399#endif
93382f6d 14400 }
252b5132
RH
14401 }
14402 break;
14403 }
252b5132 14404
d182319b
JB
14405 if ((code == BFD_RELOC_32
14406 || code == BFD_RELOC_32_PCREL
14407 || code == BFD_RELOC_X86_64_32S)
252b5132
RH
14408 && GOT_symbol
14409 && fixp->fx_addsy == GOT_symbol)
3e73aa7c 14410 {
4fa24527 14411 if (!object_64bit)
d6ab8113
JB
14412 code = BFD_RELOC_386_GOTPC;
14413 else
14414 code = BFD_RELOC_X86_64_GOTPC32;
3e73aa7c 14415 }
7b81dfbb
AJ
14416 if ((code == BFD_RELOC_64 || code == BFD_RELOC_64_PCREL)
14417 && GOT_symbol
14418 && fixp->fx_addsy == GOT_symbol)
14419 {
14420 code = BFD_RELOC_X86_64_GOTPC64;
14421 }
252b5132 14422
add39d23
TS
14423 rel = XNEW (arelent);
14424 rel->sym_ptr_ptr = XNEW (asymbol *);
49309057 14425 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
14426
14427 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
c87db184 14428
3e73aa7c
JH
14429 if (!use_rela_relocations)
14430 {
14431 /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
14432 vtable entry to be used in the relocation's section offset. */
14433 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
14434 rel->address = fixp->fx_offset;
fbeb56a4
DK
14435#if defined (OBJ_COFF) && defined (TE_PE)
14436 else if (fixp->fx_addsy && S_IS_WEAK (fixp->fx_addsy))
14437 rel->addend = fixp->fx_addnumber - (S_GET_VALUE (fixp->fx_addsy) * 2);
14438 else
14439#endif
c6682705 14440 rel->addend = 0;
3e73aa7c
JH
14441 }
14442 /* Use the rela in 64bit mode. */
252b5132 14443 else
3e73aa7c 14444 {
862be3fb
L
14445 if (disallow_64bit_reloc)
14446 switch (code)
14447 {
862be3fb
L
14448 case BFD_RELOC_X86_64_DTPOFF64:
14449 case BFD_RELOC_X86_64_TPOFF64:
14450 case BFD_RELOC_64_PCREL:
14451 case BFD_RELOC_X86_64_GOTOFF64:
14452 case BFD_RELOC_X86_64_GOT64:
14453 case BFD_RELOC_X86_64_GOTPCREL64:
14454 case BFD_RELOC_X86_64_GOTPC64:
14455 case BFD_RELOC_X86_64_GOTPLT64:
14456 case BFD_RELOC_X86_64_PLTOFF64:
14457 as_bad_where (fixp->fx_file, fixp->fx_line,
14458 _("cannot represent relocation type %s in x32 mode"),
14459 bfd_get_reloc_code_name (code));
14460 break;
14461 default:
14462 break;
14463 }
14464
062cd5e7
AS
14465 if (!fixp->fx_pcrel)
14466 rel->addend = fixp->fx_offset;
14467 else
14468 switch (code)
14469 {
14470 case BFD_RELOC_X86_64_PLT32:
14471 case BFD_RELOC_X86_64_GOT32:
14472 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
14473 case BFD_RELOC_X86_64_GOTPCRELX:
14474 case BFD_RELOC_X86_64_REX_GOTPCRELX:
bffbf940
JJ
14475 case BFD_RELOC_X86_64_TLSGD:
14476 case BFD_RELOC_X86_64_TLSLD:
14477 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7
AO
14478 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14479 case BFD_RELOC_X86_64_TLSDESC_CALL:
062cd5e7
AS
14480 rel->addend = fixp->fx_offset - fixp->fx_size;
14481 break;
14482 default:
14483 rel->addend = (section->vma
14484 - fixp->fx_size
14485 + fixp->fx_addnumber
14486 + md_pcrel_from (fixp));
14487 break;
14488 }
3e73aa7c
JH
14489 }
14490
252b5132
RH
14491 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
14492 if (rel->howto == NULL)
14493 {
14494 as_bad_where (fixp->fx_file, fixp->fx_line,
d0b47220 14495 _("cannot represent relocation type %s"),
252b5132
RH
14496 bfd_get_reloc_code_name (code));
14497 /* Set howto to a garbage value so that we can keep going. */
14498 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
9c2799c2 14499 gas_assert (rel->howto != NULL);
252b5132
RH
14500 }
14501
14502 return rel;
14503}
14504
ee86248c 14505#include "tc-i386-intel.c"
54cfded0 14506
a60de03c
JB
14507void
14508tc_x86_parse_to_dw2regnum (expressionS *exp)
54cfded0 14509{
a60de03c
JB
14510 int saved_naked_reg;
14511 char saved_register_dot;
54cfded0 14512
a60de03c
JB
14513 saved_naked_reg = allow_naked_reg;
14514 allow_naked_reg = 1;
14515 saved_register_dot = register_chars['.'];
14516 register_chars['.'] = '.';
14517 allow_pseudo_reg = 1;
14518 expression_and_evaluate (exp);
14519 allow_pseudo_reg = 0;
14520 register_chars['.'] = saved_register_dot;
14521 allow_naked_reg = saved_naked_reg;
14522
e96d56a1 14523 if (exp->X_op == O_register && exp->X_add_number >= 0)
54cfded0 14524 {
a60de03c
JB
14525 if ((addressT) exp->X_add_number < i386_regtab_size)
14526 {
14527 exp->X_op = O_constant;
14528 exp->X_add_number = i386_regtab[exp->X_add_number]
14529 .dw2_regnum[flag_code >> 1];
14530 }
14531 else
14532 exp->X_op = O_illegal;
54cfded0 14533 }
54cfded0
AM
14534}
14535
14536void
14537tc_x86_frame_initial_instructions (void)
14538{
a60de03c
JB
14539 static unsigned int sp_regno[2];
14540
14541 if (!sp_regno[flag_code >> 1])
14542 {
14543 char *saved_input = input_line_pointer;
14544 char sp[][4] = {"esp", "rsp"};
14545 expressionS exp;
a4447b93 14546
a60de03c
JB
14547 input_line_pointer = sp[flag_code >> 1];
14548 tc_x86_parse_to_dw2regnum (&exp);
9c2799c2 14549 gas_assert (exp.X_op == O_constant);
a60de03c
JB
14550 sp_regno[flag_code >> 1] = exp.X_add_number;
14551 input_line_pointer = saved_input;
14552 }
a4447b93 14553
61ff971f
L
14554 cfi_add_CFA_def_cfa (sp_regno[flag_code >> 1], -x86_cie_data_alignment);
14555 cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
54cfded0 14556}
d2b2c203 14557
d7921315
L
14558int
14559x86_dwarf2_addr_size (void)
14560{
14561#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
14562 if (x86_elf_abi == X86_64_X32_ABI)
14563 return 4;
14564#endif
14565 return bfd_arch_bits_per_address (stdoutput) / 8;
14566}
14567
d2b2c203
DJ
14568int
14569i386_elf_section_type (const char *str, size_t len)
14570{
14571 if (flag_code == CODE_64BIT
14572 && len == sizeof ("unwind") - 1
d34049e8 14573 && startswith (str, "unwind"))
d2b2c203
DJ
14574 return SHT_X86_64_UNWIND;
14575
14576 return -1;
14577}
bb41ade5 14578
ad5fec3b
EB
14579#ifdef TE_SOLARIS
14580void
14581i386_solaris_fix_up_eh_frame (segT sec)
14582{
14583 if (flag_code == CODE_64BIT)
14584 elf_section_type (sec) = SHT_X86_64_UNWIND;
14585}
14586#endif
14587
bb41ade5
AM
14588#ifdef TE_PE
14589void
14590tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
14591{
91d6fa6a 14592 expressionS exp;
bb41ade5 14593
91d6fa6a
NC
14594 exp.X_op = O_secrel;
14595 exp.X_add_symbol = symbol;
14596 exp.X_add_number = 0;
14597 emit_expr (&exp, size);
bb41ade5
AM
14598}
14599#endif
3b22753a
L
14600
14601#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14602/* For ELF on x86-64, add support for SHF_X86_64_LARGE. */
14603
01e1a5bc 14604bfd_vma
6d4af3c2 14605x86_64_section_letter (int letter, const char **ptr_msg)
3b22753a
L
14606{
14607 if (flag_code == CODE_64BIT)
14608 {
14609 if (letter == 'l')
14610 return SHF_X86_64_LARGE;
14611
8f3bae45 14612 *ptr_msg = _("bad .section directive: want a,l,w,x,M,S,G,T in string");
64e74474 14613 }
3b22753a 14614 else
8f3bae45 14615 *ptr_msg = _("bad .section directive: want a,w,x,M,S,G,T in string");
3b22753a
L
14616 return -1;
14617}
14618
01e1a5bc 14619bfd_vma
3b22753a
L
14620x86_64_section_word (char *str, size_t len)
14621{
08dedd66 14622 if (len == 5 && flag_code == CODE_64BIT && startswith (str, "large"))
3b22753a
L
14623 return SHF_X86_64_LARGE;
14624
14625 return -1;
14626}
14627
14628static void
14629handle_large_common (int small ATTRIBUTE_UNUSED)
14630{
14631 if (flag_code != CODE_64BIT)
14632 {
14633 s_comm_internal (0, elf_common_parse);
14634 as_warn (_(".largecomm supported only in 64bit mode, producing .comm"));
14635 }
14636 else
14637 {
14638 static segT lbss_section;
14639 asection *saved_com_section_ptr = elf_com_section_ptr;
14640 asection *saved_bss_section = bss_section;
14641
14642 if (lbss_section == NULL)
14643 {
14644 flagword applicable;
14645 segT seg = now_seg;
14646 subsegT subseg = now_subseg;
14647
14648 /* The .lbss section is for local .largecomm symbols. */
14649 lbss_section = subseg_new (".lbss", 0);
14650 applicable = bfd_applicable_section_flags (stdoutput);
fd361982 14651 bfd_set_section_flags (lbss_section, applicable & SEC_ALLOC);
3b22753a
L
14652 seg_info (lbss_section)->bss = 1;
14653
14654 subseg_set (seg, subseg);
14655 }
14656
14657 elf_com_section_ptr = &_bfd_elf_large_com_section;
14658 bss_section = lbss_section;
14659
14660 s_comm_internal (0, elf_common_parse);
14661
14662 elf_com_section_ptr = saved_com_section_ptr;
14663 bss_section = saved_bss_section;
14664 }
14665}
14666#endif /* OBJ_ELF || OBJ_MAYBE_ELF */