]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/config/tc-i386.c
gdb: make "start" breakpoint inferior-specific
[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),
941f0833 1102 SUBARCH (wrmsrns, WRMSRNS, ANY_WRMSRNS, false),
2188d6ea 1103 SUBARCH (msrlist, MSRLIST, ANY_MSRLIST, false),
01d8ce74 1104 SUBARCH (avx_ne_convert, AVX_NE_CONVERT, ANY_AVX_NE_CONVERT, false),
b06311ad 1105 SUBARCH (rao_int, RAO_INT, ANY_RAO_INT, false),
293f5f65
L
1106};
1107
6ceeed25
JB
1108#undef SUBARCH
1109#undef ARCH
1110
704209c0 1111#ifdef I386COFF
a6c24e68
NC
1112/* Like s_lcomm_internal in gas/read.c but the alignment string
1113 is allowed to be optional. */
1114
1115static symbolS *
1116pe_lcomm_internal (int needs_align, symbolS *symbolP, addressT size)
1117{
1118 addressT align = 0;
1119
1120 SKIP_WHITESPACE ();
1121
7ab9ffdd 1122 if (needs_align
a6c24e68
NC
1123 && *input_line_pointer == ',')
1124 {
1125 align = parse_align (needs_align - 1);
7ab9ffdd 1126
a6c24e68
NC
1127 if (align == (addressT) -1)
1128 return NULL;
1129 }
1130 else
1131 {
1132 if (size >= 8)
1133 align = 3;
1134 else if (size >= 4)
1135 align = 2;
1136 else if (size >= 2)
1137 align = 1;
1138 else
1139 align = 0;
1140 }
1141
1142 bss_alloc (symbolP, size, align);
1143 return symbolP;
1144}
1145
704209c0 1146static void
a6c24e68
NC
1147pe_lcomm (int needs_align)
1148{
1149 s_comm_internal (needs_align * 2, pe_lcomm_internal);
1150}
704209c0 1151#endif
a6c24e68 1152
29b0f896
AM
1153const pseudo_typeS md_pseudo_table[] =
1154{
1155#if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
1156 {"align", s_align_bytes, 0},
1157#else
1158 {"align", s_align_ptwo, 0},
1159#endif
1160 {"arch", set_cpu_arch, 0},
1161#ifndef I386COFF
1162 {"bss", s_bss, 0},
a6c24e68
NC
1163#else
1164 {"lcomm", pe_lcomm, 1},
29b0f896
AM
1165#endif
1166 {"ffloat", float_cons, 'f'},
1167 {"dfloat", float_cons, 'd'},
1168 {"tfloat", float_cons, 'x'},
7d19d096 1169 {"hfloat", float_cons, 'h'},
de133cf9 1170 {"bfloat16", float_cons, 'b'},
29b0f896 1171 {"value", cons, 2},
d182319b 1172 {"slong", signed_cons, 4},
29b0f896
AM
1173 {"noopt", s_ignore, 0},
1174 {"optim", s_ignore, 0},
1175 {"code16gcc", set_16bit_gcc_code_flag, CODE_16BIT},
1176 {"code16", set_code_flag, CODE_16BIT},
1177 {"code32", set_code_flag, CODE_32BIT},
da5f19a2 1178#ifdef BFD64
29b0f896 1179 {"code64", set_code_flag, CODE_64BIT},
da5f19a2 1180#endif
29b0f896
AM
1181 {"intel_syntax", set_intel_syntax, 1},
1182 {"att_syntax", set_intel_syntax, 0},
1efbbeb4
L
1183 {"intel_mnemonic", set_intel_mnemonic, 1},
1184 {"att_mnemonic", set_intel_mnemonic, 0},
db51cc60
L
1185 {"allow_index_reg", set_allow_index_reg, 1},
1186 {"disallow_index_reg", set_allow_index_reg, 0},
7bab8ab5
JB
1187 {"sse_check", set_check, 0},
1188 {"operand_check", set_check, 1},
3b22753a
L
1189#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1190 {"largecomm", handle_large_common, 0},
07a53e5c 1191#else
68d20676 1192 {"file", dwarf2_directive_file, 0},
07a53e5c
RH
1193 {"loc", dwarf2_directive_loc, 0},
1194 {"loc_mark_labels", dwarf2_directive_loc_mark_labels, 0},
3b22753a 1195#endif
6482c264
NC
1196#ifdef TE_PE
1197 {"secrel32", pe_directive_secrel, 0},
145667f8 1198 {"secidx", pe_directive_secidx, 0},
6482c264 1199#endif
29b0f896
AM
1200 {0, 0, 0}
1201};
1202
1203/* For interface with expression (). */
1204extern char *input_line_pointer;
1205
1206/* Hash table for instruction mnemonic lookup. */
629310ab 1207static htab_t op_hash;
29b0f896
AM
1208
1209/* Hash table for register lookup. */
629310ab 1210static htab_t reg_hash;
29b0f896 1211\f
ce8a8b2f
AM
1212 /* Various efficient no-op patterns for aligning code labels.
1213 Note: Don't try to assemble the instructions in the comments.
1214 0L and 0w are not legal. */
62a02d25
L
1215static const unsigned char f32_1[] =
1216 {0x90}; /* nop */
1217static const unsigned char f32_2[] =
1218 {0x66,0x90}; /* xchg %ax,%ax */
1219static const unsigned char f32_3[] =
1220 {0x8d,0x76,0x00}; /* leal 0(%esi),%esi */
1221static const unsigned char f32_4[] =
1222 {0x8d,0x74,0x26,0x00}; /* leal 0(%esi,1),%esi */
62a02d25
L
1223static const unsigned char f32_6[] =
1224 {0x8d,0xb6,0x00,0x00,0x00,0x00}; /* leal 0L(%esi),%esi */
1225static const unsigned char f32_7[] =
1226 {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal 0L(%esi,1),%esi */
62a02d25 1227static const unsigned char f16_3[] =
3ae729d5 1228 {0x8d,0x74,0x00}; /* lea 0(%si),%si */
62a02d25 1229static const unsigned char f16_4[] =
3ae729d5
L
1230 {0x8d,0xb4,0x00,0x00}; /* lea 0W(%si),%si */
1231static const unsigned char jump_disp8[] =
1232 {0xeb}; /* jmp disp8 */
1233static const unsigned char jump32_disp32[] =
1234 {0xe9}; /* jmp disp32 */
1235static const unsigned char jump16_disp32[] =
1236 {0x66,0xe9}; /* jmp disp32 */
62a02d25
L
1237/* 32-bit NOPs patterns. */
1238static const unsigned char *const f32_patt[] = {
3ae729d5 1239 f32_1, f32_2, f32_3, f32_4, NULL, f32_6, f32_7
62a02d25
L
1240};
1241/* 16-bit NOPs patterns. */
1242static const unsigned char *const f16_patt[] = {
3ae729d5 1243 f32_1, f32_2, f16_3, f16_4
62a02d25
L
1244};
1245/* nopl (%[re]ax) */
1246static const unsigned char alt_3[] =
1247 {0x0f,0x1f,0x00};
1248/* nopl 0(%[re]ax) */
1249static const unsigned char alt_4[] =
1250 {0x0f,0x1f,0x40,0x00};
1251/* nopl 0(%[re]ax,%[re]ax,1) */
1252static const unsigned char alt_5[] =
1253 {0x0f,0x1f,0x44,0x00,0x00};
1254/* nopw 0(%[re]ax,%[re]ax,1) */
1255static const unsigned char alt_6[] =
1256 {0x66,0x0f,0x1f,0x44,0x00,0x00};
1257/* nopl 0L(%[re]ax) */
1258static const unsigned char alt_7[] =
1259 {0x0f,0x1f,0x80,0x00,0x00,0x00,0x00};
1260/* nopl 0L(%[re]ax,%[re]ax,1) */
1261static const unsigned char alt_8[] =
1262 {0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1263/* nopw 0L(%[re]ax,%[re]ax,1) */
1264static const unsigned char alt_9[] =
1265 {0x66,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1266/* nopw %cs:0L(%[re]ax,%[re]ax,1) */
1267static const unsigned char alt_10[] =
1268 {0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
3ae729d5
L
1269/* data16 nopw %cs:0L(%eax,%eax,1) */
1270static const unsigned char alt_11[] =
1271 {0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
62a02d25
L
1272/* 32-bit and 64-bit NOPs patterns. */
1273static const unsigned char *const alt_patt[] = {
1274 f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
3ae729d5 1275 alt_9, alt_10, alt_11
62a02d25
L
1276};
1277
1278/* Genenerate COUNT bytes of NOPs to WHERE from PATT with the maximum
1279 size of a single NOP instruction MAX_SINGLE_NOP_SIZE. */
1280
1281static void
1282i386_output_nops (char *where, const unsigned char *const *patt,
1283 int count, int max_single_nop_size)
1284
1285{
3ae729d5
L
1286 /* Place the longer NOP first. */
1287 int last;
1288 int offset;
3076e594
NC
1289 const unsigned char *nops;
1290
1291 if (max_single_nop_size < 1)
1292 {
1293 as_fatal (_("i386_output_nops called to generate nops of at most %d bytes!"),
1294 max_single_nop_size);
1295 return;
1296 }
1297
1298 nops = patt[max_single_nop_size - 1];
3ae729d5
L
1299
1300 /* Use the smaller one if the requsted one isn't available. */
1301 if (nops == NULL)
62a02d25 1302 {
3ae729d5
L
1303 max_single_nop_size--;
1304 nops = patt[max_single_nop_size - 1];
62a02d25
L
1305 }
1306
3ae729d5
L
1307 last = count % max_single_nop_size;
1308
1309 count -= last;
1310 for (offset = 0; offset < count; offset += max_single_nop_size)
1311 memcpy (where + offset, nops, max_single_nop_size);
1312
1313 if (last)
1314 {
1315 nops = patt[last - 1];
1316 if (nops == NULL)
1317 {
1318 /* Use the smaller one plus one-byte NOP if the needed one
1319 isn't available. */
1320 last--;
1321 nops = patt[last - 1];
1322 memcpy (where + offset, nops, last);
1323 where[offset + last] = *patt[0];
1324 }
1325 else
1326 memcpy (where + offset, nops, last);
1327 }
62a02d25
L
1328}
1329
3ae729d5
L
1330static INLINE int
1331fits_in_imm7 (offsetT num)
1332{
1333 return (num & 0x7f) == num;
1334}
1335
1336static INLINE int
1337fits_in_imm31 (offsetT num)
1338{
1339 return (num & 0x7fffffff) == num;
1340}
62a02d25
L
1341
1342/* Genenerate COUNT bytes of NOPs to WHERE with the maximum size of a
1343 single NOP instruction LIMIT. */
1344
1345void
3ae729d5 1346i386_generate_nops (fragS *fragP, char *where, offsetT count, int limit)
62a02d25 1347{
3ae729d5 1348 const unsigned char *const *patt = NULL;
62a02d25 1349 int max_single_nop_size;
3ae729d5
L
1350 /* Maximum number of NOPs before switching to jump over NOPs. */
1351 int max_number_of_nops;
62a02d25 1352
3ae729d5 1353 switch (fragP->fr_type)
62a02d25 1354 {
3ae729d5
L
1355 case rs_fill_nop:
1356 case rs_align_code:
1357 break;
e379e5f3
L
1358 case rs_machine_dependent:
1359 /* Allow NOP padding for jumps and calls. */
1360 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
1361 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
1362 break;
1363 /* Fall through. */
3ae729d5 1364 default:
62a02d25
L
1365 return;
1366 }
1367
ccc9c027
L
1368 /* We need to decide which NOP sequence to use for 32bit and
1369 64bit. When -mtune= is used:
4eed87de 1370
76bc74dc
L
1371 1. For PROCESSOR_I386, PROCESSOR_I486, PROCESSOR_PENTIUM and
1372 PROCESSOR_GENERIC32, f32_patt will be used.
80b8656c
L
1373 2. For the rest, alt_patt will be used.
1374
1375 When -mtune= isn't used, alt_patt will be used if
22109423 1376 cpu_arch_isa_flags has CpuNop. Otherwise, f32_patt will
76bc74dc 1377 be used.
ccc9c027
L
1378
1379 When -march= or .arch is used, we can't use anything beyond
1380 cpu_arch_isa_flags. */
1381
1382 if (flag_code == CODE_16BIT)
1383 {
3ae729d5
L
1384 patt = f16_patt;
1385 max_single_nop_size = sizeof (f16_patt) / sizeof (f16_patt[0]);
1386 /* Limit number of NOPs to 2 in 16-bit mode. */
1387 max_number_of_nops = 2;
252b5132 1388 }
33fef721 1389 else
ccc9c027 1390 {
fbf3f584 1391 if (fragP->tc_frag_data.isa == PROCESSOR_UNKNOWN)
ccc9c027
L
1392 {
1393 /* PROCESSOR_UNKNOWN means that all ISAs may be used. */
1394 switch (cpu_arch_tune)
1395 {
1396 case PROCESSOR_UNKNOWN:
1397 /* We use cpu_arch_isa_flags to check if we SHOULD
22109423
L
1398 optimize with nops. */
1399 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1400 patt = alt_patt;
ccc9c027
L
1401 else
1402 patt = f32_patt;
1403 break;
ccc9c027
L
1404 case PROCESSOR_PENTIUM4:
1405 case PROCESSOR_NOCONA:
ef05d495 1406 case PROCESSOR_CORE:
76bc74dc 1407 case PROCESSOR_CORE2:
bd5295b2 1408 case PROCESSOR_COREI7:
76bc74dc 1409 case PROCESSOR_GENERIC64:
ccc9c027
L
1410 case PROCESSOR_K6:
1411 case PROCESSOR_ATHLON:
1412 case PROCESSOR_K8:
4eed87de 1413 case PROCESSOR_AMDFAM10:
8aedb9fe 1414 case PROCESSOR_BD:
029f3522 1415 case PROCESSOR_ZNVER:
7b458c12 1416 case PROCESSOR_BT:
80b8656c 1417 patt = alt_patt;
ccc9c027 1418 break;
76bc74dc 1419 case PROCESSOR_I386:
ccc9c027
L
1420 case PROCESSOR_I486:
1421 case PROCESSOR_PENTIUM:
2dde1948 1422 case PROCESSOR_PENTIUMPRO:
81486035 1423 case PROCESSOR_IAMCU:
ccc9c027
L
1424 case PROCESSOR_GENERIC32:
1425 patt = f32_patt;
1426 break;
c368d2a8
JB
1427 case PROCESSOR_NONE:
1428 abort ();
4eed87de 1429 }
ccc9c027
L
1430 }
1431 else
1432 {
fbf3f584 1433 switch (fragP->tc_frag_data.tune)
ccc9c027
L
1434 {
1435 case PROCESSOR_UNKNOWN:
e6a14101 1436 /* When cpu_arch_isa is set, cpu_arch_tune shouldn't be
ccc9c027
L
1437 PROCESSOR_UNKNOWN. */
1438 abort ();
1439 break;
1440
76bc74dc 1441 case PROCESSOR_I386:
ccc9c027
L
1442 case PROCESSOR_I486:
1443 case PROCESSOR_PENTIUM:
81486035 1444 case PROCESSOR_IAMCU:
ccc9c027
L
1445 case PROCESSOR_K6:
1446 case PROCESSOR_ATHLON:
1447 case PROCESSOR_K8:
4eed87de 1448 case PROCESSOR_AMDFAM10:
8aedb9fe 1449 case PROCESSOR_BD:
029f3522 1450 case PROCESSOR_ZNVER:
7b458c12 1451 case PROCESSOR_BT:
ccc9c027
L
1452 case PROCESSOR_GENERIC32:
1453 /* We use cpu_arch_isa_flags to check if we CAN optimize
22109423
L
1454 with nops. */
1455 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1456 patt = alt_patt;
ccc9c027
L
1457 else
1458 patt = f32_patt;
1459 break;
76bc74dc
L
1460 case PROCESSOR_PENTIUMPRO:
1461 case PROCESSOR_PENTIUM4:
1462 case PROCESSOR_NOCONA:
1463 case PROCESSOR_CORE:
ef05d495 1464 case PROCESSOR_CORE2:
bd5295b2 1465 case PROCESSOR_COREI7:
22109423 1466 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1467 patt = alt_patt;
ccc9c027
L
1468 else
1469 patt = f32_patt;
1470 break;
1471 case PROCESSOR_GENERIC64:
80b8656c 1472 patt = alt_patt;
ccc9c027 1473 break;
c368d2a8
JB
1474 case PROCESSOR_NONE:
1475 abort ();
4eed87de 1476 }
ccc9c027
L
1477 }
1478
76bc74dc
L
1479 if (patt == f32_patt)
1480 {
3ae729d5
L
1481 max_single_nop_size = sizeof (f32_patt) / sizeof (f32_patt[0]);
1482 /* Limit number of NOPs to 2 for older processors. */
1483 max_number_of_nops = 2;
76bc74dc
L
1484 }
1485 else
1486 {
3ae729d5
L
1487 max_single_nop_size = sizeof (alt_patt) / sizeof (alt_patt[0]);
1488 /* Limit number of NOPs to 7 for newer processors. */
1489 max_number_of_nops = 7;
1490 }
1491 }
1492
1493 if (limit == 0)
1494 limit = max_single_nop_size;
1495
1496 if (fragP->fr_type == rs_fill_nop)
1497 {
1498 /* Output NOPs for .nop directive. */
1499 if (limit > max_single_nop_size)
1500 {
1501 as_bad_where (fragP->fr_file, fragP->fr_line,
1502 _("invalid single nop size: %d "
1503 "(expect within [0, %d])"),
1504 limit, max_single_nop_size);
1505 return;
1506 }
1507 }
e379e5f3 1508 else if (fragP->fr_type != rs_machine_dependent)
3ae729d5
L
1509 fragP->fr_var = count;
1510
1511 if ((count / max_single_nop_size) > max_number_of_nops)
1512 {
1513 /* Generate jump over NOPs. */
1514 offsetT disp = count - 2;
1515 if (fits_in_imm7 (disp))
1516 {
1517 /* Use "jmp disp8" if possible. */
1518 count = disp;
1519 where[0] = jump_disp8[0];
1520 where[1] = count;
1521 where += 2;
1522 }
1523 else
1524 {
1525 unsigned int size_of_jump;
1526
1527 if (flag_code == CODE_16BIT)
1528 {
1529 where[0] = jump16_disp32[0];
1530 where[1] = jump16_disp32[1];
1531 size_of_jump = 2;
1532 }
1533 else
1534 {
1535 where[0] = jump32_disp32[0];
1536 size_of_jump = 1;
1537 }
1538
1539 count -= size_of_jump + 4;
1540 if (!fits_in_imm31 (count))
1541 {
1542 as_bad_where (fragP->fr_file, fragP->fr_line,
1543 _("jump over nop padding out of range"));
1544 return;
1545 }
1546
1547 md_number_to_chars (where + size_of_jump, count, 4);
1548 where += size_of_jump + 4;
76bc74dc 1549 }
ccc9c027 1550 }
3ae729d5
L
1551
1552 /* Generate multiple NOPs. */
1553 i386_output_nops (where, patt, count, limit);
252b5132
RH
1554}
1555
c6fb90c8 1556static INLINE int
0dfbf9d7 1557operand_type_all_zero (const union i386_operand_type *x)
40fb9820 1558{
0dfbf9d7 1559 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1560 {
1561 case 3:
0dfbf9d7 1562 if (x->array[2])
c6fb90c8 1563 return 0;
1a0670f3 1564 /* Fall through. */
c6fb90c8 1565 case 2:
0dfbf9d7 1566 if (x->array[1])
c6fb90c8 1567 return 0;
1a0670f3 1568 /* Fall through. */
c6fb90c8 1569 case 1:
0dfbf9d7 1570 return !x->array[0];
c6fb90c8
L
1571 default:
1572 abort ();
1573 }
40fb9820
L
1574}
1575
c6fb90c8 1576static INLINE void
0dfbf9d7 1577operand_type_set (union i386_operand_type *x, unsigned int v)
40fb9820 1578{
0dfbf9d7 1579 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1580 {
1581 case 3:
0dfbf9d7 1582 x->array[2] = v;
1a0670f3 1583 /* Fall through. */
c6fb90c8 1584 case 2:
0dfbf9d7 1585 x->array[1] = v;
1a0670f3 1586 /* Fall through. */
c6fb90c8 1587 case 1:
0dfbf9d7 1588 x->array[0] = v;
1a0670f3 1589 /* Fall through. */
c6fb90c8
L
1590 break;
1591 default:
1592 abort ();
1593 }
bab6aec1
JB
1594
1595 x->bitfield.class = ClassNone;
75e5731b 1596 x->bitfield.instance = InstanceNone;
c6fb90c8 1597}
40fb9820 1598
c6fb90c8 1599static INLINE int
0dfbf9d7
L
1600operand_type_equal (const union i386_operand_type *x,
1601 const union i386_operand_type *y)
c6fb90c8 1602{
0dfbf9d7 1603 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1604 {
1605 case 3:
0dfbf9d7 1606 if (x->array[2] != y->array[2])
c6fb90c8 1607 return 0;
1a0670f3 1608 /* Fall through. */
c6fb90c8 1609 case 2:
0dfbf9d7 1610 if (x->array[1] != y->array[1])
c6fb90c8 1611 return 0;
1a0670f3 1612 /* Fall through. */
c6fb90c8 1613 case 1:
0dfbf9d7 1614 return x->array[0] == y->array[0];
c6fb90c8
L
1615 break;
1616 default:
1617 abort ();
1618 }
1619}
40fb9820 1620
0dfbf9d7
L
1621static INLINE int
1622cpu_flags_all_zero (const union i386_cpu_flags *x)
1623{
1624 switch (ARRAY_SIZE(x->array))
1625 {
75f8266a
KL
1626 case 5:
1627 if (x->array[4])
1628 return 0;
1629 /* Fall through. */
53467f57
IT
1630 case 4:
1631 if (x->array[3])
1632 return 0;
1633 /* Fall through. */
0dfbf9d7
L
1634 case 3:
1635 if (x->array[2])
1636 return 0;
1a0670f3 1637 /* Fall through. */
0dfbf9d7
L
1638 case 2:
1639 if (x->array[1])
1640 return 0;
1a0670f3 1641 /* Fall through. */
0dfbf9d7
L
1642 case 1:
1643 return !x->array[0];
1644 default:
1645 abort ();
1646 }
1647}
1648
0dfbf9d7
L
1649static INLINE int
1650cpu_flags_equal (const union i386_cpu_flags *x,
1651 const union i386_cpu_flags *y)
1652{
1653 switch (ARRAY_SIZE(x->array))
1654 {
75f8266a
KL
1655 case 5:
1656 if (x->array[4] != y->array[4])
1657 return 0;
1658 /* Fall through. */
53467f57
IT
1659 case 4:
1660 if (x->array[3] != y->array[3])
1661 return 0;
1662 /* Fall through. */
0dfbf9d7
L
1663 case 3:
1664 if (x->array[2] != y->array[2])
1665 return 0;
1a0670f3 1666 /* Fall through. */
0dfbf9d7
L
1667 case 2:
1668 if (x->array[1] != y->array[1])
1669 return 0;
1a0670f3 1670 /* Fall through. */
0dfbf9d7
L
1671 case 1:
1672 return x->array[0] == y->array[0];
1673 break;
1674 default:
1675 abort ();
1676 }
1677}
c6fb90c8
L
1678
1679static INLINE int
1680cpu_flags_check_cpu64 (i386_cpu_flags f)
1681{
1682 return !((flag_code == CODE_64BIT && f.bitfield.cpuno64)
1683 || (flag_code != CODE_64BIT && f.bitfield.cpu64));
40fb9820
L
1684}
1685
c6fb90c8
L
1686static INLINE i386_cpu_flags
1687cpu_flags_and (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1688{
c6fb90c8
L
1689 switch (ARRAY_SIZE (x.array))
1690 {
75f8266a
KL
1691 case 5:
1692 x.array [4] &= y.array [4];
1693 /* Fall through. */
53467f57
IT
1694 case 4:
1695 x.array [3] &= y.array [3];
1696 /* Fall through. */
c6fb90c8
L
1697 case 3:
1698 x.array [2] &= y.array [2];
1a0670f3 1699 /* Fall through. */
c6fb90c8
L
1700 case 2:
1701 x.array [1] &= y.array [1];
1a0670f3 1702 /* Fall through. */
c6fb90c8
L
1703 case 1:
1704 x.array [0] &= y.array [0];
1705 break;
1706 default:
1707 abort ();
1708 }
1709 return x;
1710}
40fb9820 1711
c6fb90c8
L
1712static INLINE i386_cpu_flags
1713cpu_flags_or (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1714{
c6fb90c8 1715 switch (ARRAY_SIZE (x.array))
40fb9820 1716 {
75f8266a
KL
1717 case 5:
1718 x.array [4] |= y.array [4];
1719 /* Fall through. */
53467f57
IT
1720 case 4:
1721 x.array [3] |= y.array [3];
1722 /* Fall through. */
c6fb90c8
L
1723 case 3:
1724 x.array [2] |= y.array [2];
1a0670f3 1725 /* Fall through. */
c6fb90c8
L
1726 case 2:
1727 x.array [1] |= y.array [1];
1a0670f3 1728 /* Fall through. */
c6fb90c8
L
1729 case 1:
1730 x.array [0] |= y.array [0];
40fb9820
L
1731 break;
1732 default:
1733 abort ();
1734 }
40fb9820
L
1735 return x;
1736}
1737
309d3373
JB
1738static INLINE i386_cpu_flags
1739cpu_flags_and_not (i386_cpu_flags x, i386_cpu_flags y)
1740{
1741 switch (ARRAY_SIZE (x.array))
1742 {
75f8266a
KL
1743 case 5:
1744 x.array [4] &= ~y.array [4];
1745 /* Fall through. */
53467f57
IT
1746 case 4:
1747 x.array [3] &= ~y.array [3];
1748 /* Fall through. */
309d3373
JB
1749 case 3:
1750 x.array [2] &= ~y.array [2];
1a0670f3 1751 /* Fall through. */
309d3373
JB
1752 case 2:
1753 x.array [1] &= ~y.array [1];
1a0670f3 1754 /* Fall through. */
309d3373
JB
1755 case 1:
1756 x.array [0] &= ~y.array [0];
1757 break;
1758 default:
1759 abort ();
1760 }
1761 return x;
1762}
1763
6c0946d0
JB
1764static const i386_cpu_flags avx512 = CPU_ANY_AVX512F_FLAGS;
1765
c0f3af97
L
1766#define CPU_FLAGS_ARCH_MATCH 0x1
1767#define CPU_FLAGS_64BIT_MATCH 0x2
1768
c0f3af97 1769#define CPU_FLAGS_PERFECT_MATCH \
db12e14e 1770 (CPU_FLAGS_ARCH_MATCH | CPU_FLAGS_64BIT_MATCH)
c0f3af97
L
1771
1772/* Return CPU flags match bits. */
3629bb00 1773
40fb9820 1774static int
d3ce72d0 1775cpu_flags_match (const insn_template *t)
40fb9820 1776{
c0f3af97
L
1777 i386_cpu_flags x = t->cpu_flags;
1778 int match = cpu_flags_check_cpu64 (x) ? CPU_FLAGS_64BIT_MATCH : 0;
40fb9820
L
1779
1780 x.bitfield.cpu64 = 0;
1781 x.bitfield.cpuno64 = 0;
1782
0dfbf9d7 1783 if (cpu_flags_all_zero (&x))
c0f3af97
L
1784 {
1785 /* This instruction is available on all archs. */
db12e14e 1786 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 1787 }
3629bb00
L
1788 else
1789 {
c0f3af97 1790 /* This instruction is available only on some archs. */
3629bb00
L
1791 i386_cpu_flags cpu = cpu_arch_flags;
1792
ab592e75
JB
1793 /* AVX512VL is no standalone feature - match it and then strip it. */
1794 if (x.bitfield.cpuavx512vl && !cpu.bitfield.cpuavx512vl)
1795 return match;
1796 x.bitfield.cpuavx512vl = 0;
1797
22c36940
JB
1798 /* AVX and AVX2 present at the same time express an operand size
1799 dependency - strip AVX2 for the purposes here. The operand size
1800 dependent check occurs in check_vecOperands(). */
1801 if (x.bitfield.cpuavx && x.bitfield.cpuavx2)
1802 x.bitfield.cpuavx2 = 0;
1803
3629bb00 1804 cpu = cpu_flags_and (x, cpu);
c0f3af97
L
1805 if (!cpu_flags_all_zero (&cpu))
1806 {
57392598 1807 if (x.bitfield.cpuavx)
a5ff0eb2 1808 {
929f69fa 1809 /* We need to check a few extra flags with AVX. */
b9d49817 1810 if (cpu.bitfield.cpuavx
40d231b4
JB
1811 && (!t->opcode_modifier.sse2avx
1812 || (sse2avx && !i.prefix[DATA_PREFIX]))
b9d49817 1813 && (!x.bitfield.cpuaes || cpu.bitfield.cpuaes)
929f69fa 1814 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
b9d49817
JB
1815 && (!x.bitfield.cpupclmul || cpu.bitfield.cpupclmul))
1816 match |= CPU_FLAGS_ARCH_MATCH;
a5ff0eb2 1817 }
929f69fa
JB
1818 else if (x.bitfield.cpuavx512f)
1819 {
1820 /* We need to check a few extra flags with AVX512F. */
1821 if (cpu.bitfield.cpuavx512f
1822 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
1823 && (!x.bitfield.cpuvaes || cpu.bitfield.cpuvaes)
1824 && (!x.bitfield.cpuvpclmulqdq || cpu.bitfield.cpuvpclmulqdq))
1825 match |= CPU_FLAGS_ARCH_MATCH;
1826 }
a5ff0eb2 1827 else
db12e14e 1828 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 1829 }
3629bb00 1830 }
c0f3af97 1831 return match;
40fb9820
L
1832}
1833
c6fb90c8
L
1834static INLINE i386_operand_type
1835operand_type_and (i386_operand_type x, i386_operand_type y)
40fb9820 1836{
bab6aec1
JB
1837 if (x.bitfield.class != y.bitfield.class)
1838 x.bitfield.class = ClassNone;
75e5731b
JB
1839 if (x.bitfield.instance != y.bitfield.instance)
1840 x.bitfield.instance = InstanceNone;
bab6aec1 1841
c6fb90c8
L
1842 switch (ARRAY_SIZE (x.array))
1843 {
1844 case 3:
1845 x.array [2] &= y.array [2];
1a0670f3 1846 /* Fall through. */
c6fb90c8
L
1847 case 2:
1848 x.array [1] &= y.array [1];
1a0670f3 1849 /* Fall through. */
c6fb90c8
L
1850 case 1:
1851 x.array [0] &= y.array [0];
1852 break;
1853 default:
1854 abort ();
1855 }
1856 return x;
40fb9820
L
1857}
1858
73053c1f
JB
1859static INLINE i386_operand_type
1860operand_type_and_not (i386_operand_type x, i386_operand_type y)
1861{
bab6aec1 1862 gas_assert (y.bitfield.class == ClassNone);
75e5731b 1863 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 1864
73053c1f
JB
1865 switch (ARRAY_SIZE (x.array))
1866 {
1867 case 3:
1868 x.array [2] &= ~y.array [2];
1869 /* Fall through. */
1870 case 2:
1871 x.array [1] &= ~y.array [1];
1872 /* Fall through. */
1873 case 1:
1874 x.array [0] &= ~y.array [0];
1875 break;
1876 default:
1877 abort ();
1878 }
1879 return x;
1880}
1881
c6fb90c8
L
1882static INLINE i386_operand_type
1883operand_type_or (i386_operand_type x, i386_operand_type y)
40fb9820 1884{
bab6aec1
JB
1885 gas_assert (x.bitfield.class == ClassNone ||
1886 y.bitfield.class == ClassNone ||
1887 x.bitfield.class == y.bitfield.class);
75e5731b
JB
1888 gas_assert (x.bitfield.instance == InstanceNone ||
1889 y.bitfield.instance == InstanceNone ||
1890 x.bitfield.instance == y.bitfield.instance);
bab6aec1 1891
c6fb90c8 1892 switch (ARRAY_SIZE (x.array))
40fb9820 1893 {
c6fb90c8
L
1894 case 3:
1895 x.array [2] |= y.array [2];
1a0670f3 1896 /* Fall through. */
c6fb90c8
L
1897 case 2:
1898 x.array [1] |= y.array [1];
1a0670f3 1899 /* Fall through. */
c6fb90c8
L
1900 case 1:
1901 x.array [0] |= y.array [0];
40fb9820
L
1902 break;
1903 default:
1904 abort ();
1905 }
c6fb90c8
L
1906 return x;
1907}
40fb9820 1908
c6fb90c8
L
1909static INLINE i386_operand_type
1910operand_type_xor (i386_operand_type x, i386_operand_type y)
1911{
bab6aec1 1912 gas_assert (y.bitfield.class == ClassNone);
75e5731b 1913 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 1914
c6fb90c8
L
1915 switch (ARRAY_SIZE (x.array))
1916 {
1917 case 3:
1918 x.array [2] ^= y.array [2];
1a0670f3 1919 /* Fall through. */
c6fb90c8
L
1920 case 2:
1921 x.array [1] ^= y.array [1];
1a0670f3 1922 /* Fall through. */
c6fb90c8
L
1923 case 1:
1924 x.array [0] ^= y.array [0];
1925 break;
1926 default:
1927 abort ();
1928 }
40fb9820
L
1929 return x;
1930}
1931
40fb9820 1932static const i386_operand_type disp16_32 = OPERAND_TYPE_DISP16_32;
bab6aec1 1933static const i386_operand_type anydisp = OPERAND_TYPE_ANYDISP;
40fb9820 1934static const i386_operand_type regxmm = OPERAND_TYPE_REGXMM;
40fb9820
L
1935static const i386_operand_type imm16 = OPERAND_TYPE_IMM16;
1936static const i386_operand_type imm32 = OPERAND_TYPE_IMM32;
1937static const i386_operand_type imm32s = OPERAND_TYPE_IMM32S;
40fb9820
L
1938static const i386_operand_type imm16_32 = OPERAND_TYPE_IMM16_32;
1939static const i386_operand_type imm16_32s = OPERAND_TYPE_IMM16_32S;
1940static const i386_operand_type imm16_32_32s = OPERAND_TYPE_IMM16_32_32S;
1941
1942enum operand_type
1943{
1944 reg,
40fb9820
L
1945 imm,
1946 disp,
1947 anymem
1948};
1949
c6fb90c8 1950static INLINE int
40fb9820
L
1951operand_type_check (i386_operand_type t, enum operand_type c)
1952{
1953 switch (c)
1954 {
1955 case reg:
bab6aec1 1956 return t.bitfield.class == Reg;
40fb9820 1957
40fb9820
L
1958 case imm:
1959 return (t.bitfield.imm8
1960 || t.bitfield.imm8s
1961 || t.bitfield.imm16
1962 || t.bitfield.imm32
1963 || t.bitfield.imm32s
1964 || t.bitfield.imm64);
1965
1966 case disp:
1967 return (t.bitfield.disp8
1968 || t.bitfield.disp16
1969 || t.bitfield.disp32
40fb9820
L
1970 || t.bitfield.disp64);
1971
1972 case anymem:
1973 return (t.bitfield.disp8
1974 || t.bitfield.disp16
1975 || t.bitfield.disp32
40fb9820
L
1976 || t.bitfield.disp64
1977 || t.bitfield.baseindex);
1978
1979 default:
1980 abort ();
1981 }
2cfe26b6
AM
1982
1983 return 0;
40fb9820
L
1984}
1985
7a54636a
L
1986/* Return 1 if there is no conflict in 8bit/16bit/32bit/64bit/80bit size
1987 between operand GIVEN and opeand WANTED for instruction template T. */
5c07affc
L
1988
1989static INLINE int
7a54636a
L
1990match_operand_size (const insn_template *t, unsigned int wanted,
1991 unsigned int given)
5c07affc 1992{
3ac21baa
JB
1993 return !((i.types[given].bitfield.byte
1994 && !t->operand_types[wanted].bitfield.byte)
1995 || (i.types[given].bitfield.word
1996 && !t->operand_types[wanted].bitfield.word)
1997 || (i.types[given].bitfield.dword
1998 && !t->operand_types[wanted].bitfield.dword)
1999 || (i.types[given].bitfield.qword
2000 && !t->operand_types[wanted].bitfield.qword)
2001 || (i.types[given].bitfield.tbyte
2002 && !t->operand_types[wanted].bitfield.tbyte));
5c07affc
L
2003}
2004
dd40ce22
L
2005/* Return 1 if there is no conflict in SIMD register between operand
2006 GIVEN and opeand WANTED for instruction template T. */
1b54b8d7
JB
2007
2008static INLINE int
dd40ce22
L
2009match_simd_size (const insn_template *t, unsigned int wanted,
2010 unsigned int given)
1b54b8d7 2011{
3ac21baa
JB
2012 return !((i.types[given].bitfield.xmmword
2013 && !t->operand_types[wanted].bitfield.xmmword)
2014 || (i.types[given].bitfield.ymmword
2015 && !t->operand_types[wanted].bitfield.ymmword)
2016 || (i.types[given].bitfield.zmmword
260cd341
LC
2017 && !t->operand_types[wanted].bitfield.zmmword)
2018 || (i.types[given].bitfield.tmmword
2019 && !t->operand_types[wanted].bitfield.tmmword));
1b54b8d7
JB
2020}
2021
7a54636a
L
2022/* Return 1 if there is no conflict in any size between operand GIVEN
2023 and opeand WANTED for instruction template T. */
5c07affc
L
2024
2025static INLINE int
dd40ce22
L
2026match_mem_size (const insn_template *t, unsigned int wanted,
2027 unsigned int given)
5c07affc 2028{
7a54636a 2029 return (match_operand_size (t, wanted, given)
3ac21baa 2030 && !((i.types[given].bitfield.unspecified
5273a3cd 2031 && !i.broadcast.type
a5748e0d 2032 && !i.broadcast.bytes
3ac21baa
JB
2033 && !t->operand_types[wanted].bitfield.unspecified)
2034 || (i.types[given].bitfield.fword
2035 && !t->operand_types[wanted].bitfield.fword)
1b54b8d7
JB
2036 /* For scalar opcode templates to allow register and memory
2037 operands at the same time, some special casing is needed
d6793fa1
JB
2038 here. Also for v{,p}broadcast*, {,v}pmov{s,z}*, and
2039 down-conversion vpmov*. */
3528c362 2040 || ((t->operand_types[wanted].bitfield.class == RegSIMD
bc49bfd8
JB
2041 && t->operand_types[wanted].bitfield.byte
2042 + t->operand_types[wanted].bitfield.word
2043 + t->operand_types[wanted].bitfield.dword
2044 + t->operand_types[wanted].bitfield.qword
2045 > !!t->opcode_modifier.broadcast)
3ac21baa
JB
2046 ? (i.types[given].bitfield.xmmword
2047 || i.types[given].bitfield.ymmword
2048 || i.types[given].bitfield.zmmword)
2049 : !match_simd_size(t, wanted, given))));
5c07affc
L
2050}
2051
3ac21baa
JB
2052/* Return value has MATCH_STRAIGHT set if there is no size conflict on any
2053 operands for instruction template T, and it has MATCH_REVERSE set if there
2054 is no size conflict on any operands for the template with operands reversed
2055 (and the template allows for reversing in the first place). */
5c07affc 2056
3ac21baa
JB
2057#define MATCH_STRAIGHT 1
2058#define MATCH_REVERSE 2
2059
2060static INLINE unsigned int
d3ce72d0 2061operand_size_match (const insn_template *t)
5c07affc 2062{
3ac21baa 2063 unsigned int j, match = MATCH_STRAIGHT;
5c07affc 2064
0cfa3eb3 2065 /* Don't check non-absolute jump instructions. */
5c07affc 2066 if (t->opcode_modifier.jump
0cfa3eb3 2067 && t->opcode_modifier.jump != JUMP_ABSOLUTE)
5c07affc
L
2068 return match;
2069
2070 /* Check memory and accumulator operand size. */
2071 for (j = 0; j < i.operands; j++)
2072 {
3528c362
JB
2073 if (i.types[j].bitfield.class != Reg
2074 && i.types[j].bitfield.class != RegSIMD
601e8564 2075 && t->opcode_modifier.anysize)
5c07affc
L
2076 continue;
2077
bab6aec1 2078 if (t->operand_types[j].bitfield.class == Reg
7a54636a 2079 && !match_operand_size (t, j, j))
5c07affc
L
2080 {
2081 match = 0;
2082 break;
2083 }
2084
3528c362 2085 if (t->operand_types[j].bitfield.class == RegSIMD
3ac21baa 2086 && !match_simd_size (t, j, j))
1b54b8d7
JB
2087 {
2088 match = 0;
2089 break;
2090 }
2091
75e5731b 2092 if (t->operand_types[j].bitfield.instance == Accum
7a54636a 2093 && (!match_operand_size (t, j, j) || !match_simd_size (t, j, j)))
1b54b8d7
JB
2094 {
2095 match = 0;
2096 break;
2097 }
2098
c48dadc9 2099 if ((i.flags[j] & Operand_Mem) && !match_mem_size (t, j, j))
5c07affc
L
2100 {
2101 match = 0;
2102 break;
2103 }
2104 }
2105
3ac21baa 2106 if (!t->opcode_modifier.d)
7b94647a 2107 return match;
5c07affc
L
2108
2109 /* Check reverse. */
8bd915b7
JB
2110 gas_assert ((i.operands >= 2 && i.operands <= 3)
2111 || t->opcode_modifier.vexsources);
5c07affc 2112
f5eb1d70 2113 for (j = 0; j < i.operands; j++)
5c07affc 2114 {
f5eb1d70
JB
2115 unsigned int given = i.operands - j - 1;
2116
8bd915b7
JB
2117 /* For 4- and 5-operand insns VEX.W controls just the first two
2118 register operands. */
2119 if (t->opcode_modifier.vexsources)
2120 given = j < 2 ? 1 - j : j;
2121
bab6aec1 2122 if (t->operand_types[j].bitfield.class == Reg
f5eb1d70 2123 && !match_operand_size (t, j, given))
7b94647a 2124 return match;
5c07affc 2125
3528c362 2126 if (t->operand_types[j].bitfield.class == RegSIMD
f5eb1d70 2127 && !match_simd_size (t, j, given))
7b94647a 2128 return match;
dbbc8b7e 2129
75e5731b 2130 if (t->operand_types[j].bitfield.instance == Accum
f5eb1d70
JB
2131 && (!match_operand_size (t, j, given)
2132 || !match_simd_size (t, j, given)))
7b94647a 2133 return match;
dbbc8b7e 2134
f5eb1d70 2135 if ((i.flags[given] & Operand_Mem) && !match_mem_size (t, j, given))
7b94647a 2136 return match;
5c07affc
L
2137 }
2138
3ac21baa 2139 return match | MATCH_REVERSE;
5c07affc
L
2140}
2141
c6fb90c8 2142static INLINE int
40fb9820
L
2143operand_type_match (i386_operand_type overlap,
2144 i386_operand_type given)
2145{
2146 i386_operand_type temp = overlap;
2147
7d5e4556 2148 temp.bitfield.unspecified = 0;
5c07affc
L
2149 temp.bitfield.byte = 0;
2150 temp.bitfield.word = 0;
2151 temp.bitfield.dword = 0;
2152 temp.bitfield.fword = 0;
2153 temp.bitfield.qword = 0;
2154 temp.bitfield.tbyte = 0;
2155 temp.bitfield.xmmword = 0;
c0f3af97 2156 temp.bitfield.ymmword = 0;
43234a1e 2157 temp.bitfield.zmmword = 0;
260cd341 2158 temp.bitfield.tmmword = 0;
0dfbf9d7 2159 if (operand_type_all_zero (&temp))
891edac4 2160 goto mismatch;
40fb9820 2161
6f2f06be 2162 if (given.bitfield.baseindex == overlap.bitfield.baseindex)
891edac4
L
2163 return 1;
2164
dc1e8a47 2165 mismatch:
a65babc9 2166 i.error = operand_type_mismatch;
891edac4 2167 return 0;
40fb9820
L
2168}
2169
7d5e4556 2170/* If given types g0 and g1 are registers they must be of the same type
10c17abd 2171 unless the expected operand type register overlap is null.
5de4d9ef 2172 Some Intel syntax memory operand size checking also happens here. */
40fb9820 2173
c6fb90c8 2174static INLINE int
dc821c5f 2175operand_type_register_match (i386_operand_type g0,
40fb9820 2176 i386_operand_type t0,
40fb9820
L
2177 i386_operand_type g1,
2178 i386_operand_type t1)
2179{
bab6aec1 2180 if (g0.bitfield.class != Reg
3528c362 2181 && g0.bitfield.class != RegSIMD
10c17abd
JB
2182 && (!operand_type_check (g0, anymem)
2183 || g0.bitfield.unspecified
5de4d9ef
JB
2184 || (t0.bitfield.class != Reg
2185 && t0.bitfield.class != RegSIMD)))
40fb9820
L
2186 return 1;
2187
bab6aec1 2188 if (g1.bitfield.class != Reg
3528c362 2189 && g1.bitfield.class != RegSIMD
10c17abd
JB
2190 && (!operand_type_check (g1, anymem)
2191 || g1.bitfield.unspecified
5de4d9ef
JB
2192 || (t1.bitfield.class != Reg
2193 && t1.bitfield.class != RegSIMD)))
40fb9820
L
2194 return 1;
2195
dc821c5f
JB
2196 if (g0.bitfield.byte == g1.bitfield.byte
2197 && g0.bitfield.word == g1.bitfield.word
2198 && g0.bitfield.dword == g1.bitfield.dword
10c17abd
JB
2199 && g0.bitfield.qword == g1.bitfield.qword
2200 && g0.bitfield.xmmword == g1.bitfield.xmmword
2201 && g0.bitfield.ymmword == g1.bitfield.ymmword
2202 && g0.bitfield.zmmword == g1.bitfield.zmmword)
40fb9820
L
2203 return 1;
2204
c4d09633
JB
2205 /* If expectations overlap in no more than a single size, all is fine. */
2206 g0 = operand_type_and (t0, t1);
2207 if (g0.bitfield.byte
2208 + g0.bitfield.word
2209 + g0.bitfield.dword
2210 + g0.bitfield.qword
2211 + g0.bitfield.xmmword
2212 + g0.bitfield.ymmword
2213 + g0.bitfield.zmmword <= 1)
891edac4
L
2214 return 1;
2215
a65babc9 2216 i.error = register_type_mismatch;
891edac4
L
2217
2218 return 0;
40fb9820
L
2219}
2220
4c692bc7
JB
2221static INLINE unsigned int
2222register_number (const reg_entry *r)
2223{
2224 unsigned int nr = r->reg_num;
2225
2226 if (r->reg_flags & RegRex)
2227 nr += 8;
2228
200cbe0f
L
2229 if (r->reg_flags & RegVRex)
2230 nr += 16;
2231
4c692bc7
JB
2232 return nr;
2233}
2234
252b5132 2235static INLINE unsigned int
40fb9820 2236mode_from_disp_size (i386_operand_type t)
252b5132 2237{
b5014f7a 2238 if (t.bitfield.disp8)
40fb9820
L
2239 return 1;
2240 else if (t.bitfield.disp16
a775efc8 2241 || t.bitfield.disp32)
40fb9820
L
2242 return 2;
2243 else
2244 return 0;
252b5132
RH
2245}
2246
2247static INLINE int
65879393 2248fits_in_signed_byte (addressT num)
252b5132 2249{
65879393 2250 return num + 0x80 <= 0xff;
47926f60 2251}
252b5132
RH
2252
2253static INLINE int
65879393 2254fits_in_unsigned_byte (addressT num)
252b5132 2255{
65879393 2256 return num <= 0xff;
47926f60 2257}
252b5132
RH
2258
2259static INLINE int
65879393 2260fits_in_unsigned_word (addressT num)
252b5132 2261{
65879393 2262 return num <= 0xffff;
47926f60 2263}
252b5132
RH
2264
2265static INLINE int
65879393 2266fits_in_signed_word (addressT num)
252b5132 2267{
65879393 2268 return num + 0x8000 <= 0xffff;
47926f60 2269}
2a962e6d 2270
3e73aa7c 2271static INLINE int
65879393 2272fits_in_signed_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2273{
2274#ifndef BFD64
2275 return 1;
2276#else
65879393 2277 return num + 0x80000000 <= 0xffffffff;
3e73aa7c
JH
2278#endif
2279} /* fits_in_signed_long() */
2a962e6d 2280
3e73aa7c 2281static INLINE int
65879393 2282fits_in_unsigned_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2283{
2284#ifndef BFD64
2285 return 1;
2286#else
65879393 2287 return num <= 0xffffffff;
3e73aa7c
JH
2288#endif
2289} /* fits_in_unsigned_long() */
252b5132 2290
a442cac5
JB
2291static INLINE valueT extend_to_32bit_address (addressT num)
2292{
2293#ifdef BFD64
2294 if (fits_in_unsigned_long(num))
2295 return (num ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
2296
2297 if (!fits_in_signed_long (num))
2298 return num & 0xffffffff;
2299#endif
2300
2301 return num;
2302}
2303
43234a1e 2304static INLINE int
b5014f7a 2305fits_in_disp8 (offsetT num)
43234a1e
L
2306{
2307 int shift = i.memshift;
2308 unsigned int mask;
2309
2310 if (shift == -1)
2311 abort ();
2312
2313 mask = (1 << shift) - 1;
2314
2315 /* Return 0 if NUM isn't properly aligned. */
2316 if ((num & mask))
2317 return 0;
2318
2319 /* Check if NUM will fit in 8bit after shift. */
2320 return fits_in_signed_byte (num >> shift);
2321}
2322
a683cc34
SP
2323static INLINE int
2324fits_in_imm4 (offsetT num)
2325{
2326 return (num & 0xf) == num;
2327}
2328
40fb9820 2329static i386_operand_type
e3bb37b5 2330smallest_imm_type (offsetT num)
252b5132 2331{
40fb9820 2332 i386_operand_type t;
7ab9ffdd 2333
0dfbf9d7 2334 operand_type_set (&t, 0);
40fb9820
L
2335 t.bitfield.imm64 = 1;
2336
2337 if (cpu_arch_tune != PROCESSOR_I486 && num == 1)
e413e4e9
AM
2338 {
2339 /* This code is disabled on the 486 because all the Imm1 forms
2340 in the opcode table are slower on the i486. They're the
2341 versions with the implicitly specified single-position
2342 displacement, which has another syntax if you really want to
2343 use that form. */
40fb9820
L
2344 t.bitfield.imm1 = 1;
2345 t.bitfield.imm8 = 1;
2346 t.bitfield.imm8s = 1;
2347 t.bitfield.imm16 = 1;
2348 t.bitfield.imm32 = 1;
2349 t.bitfield.imm32s = 1;
2350 }
2351 else if (fits_in_signed_byte (num))
2352 {
2353 t.bitfield.imm8 = 1;
2354 t.bitfield.imm8s = 1;
2355 t.bitfield.imm16 = 1;
2356 t.bitfield.imm32 = 1;
2357 t.bitfield.imm32s = 1;
2358 }
2359 else if (fits_in_unsigned_byte (num))
2360 {
2361 t.bitfield.imm8 = 1;
2362 t.bitfield.imm16 = 1;
2363 t.bitfield.imm32 = 1;
2364 t.bitfield.imm32s = 1;
2365 }
2366 else if (fits_in_signed_word (num) || fits_in_unsigned_word (num))
2367 {
2368 t.bitfield.imm16 = 1;
2369 t.bitfield.imm32 = 1;
2370 t.bitfield.imm32s = 1;
2371 }
2372 else if (fits_in_signed_long (num))
2373 {
2374 t.bitfield.imm32 = 1;
2375 t.bitfield.imm32s = 1;
2376 }
2377 else if (fits_in_unsigned_long (num))
2378 t.bitfield.imm32 = 1;
2379
2380 return t;
47926f60 2381}
252b5132 2382
847f7ad4 2383static offsetT
e3bb37b5 2384offset_in_range (offsetT val, int size)
847f7ad4 2385{
508866be 2386 addressT mask;
ba2adb93 2387
847f7ad4
AM
2388 switch (size)
2389 {
508866be
L
2390 case 1: mask = ((addressT) 1 << 8) - 1; break;
2391 case 2: mask = ((addressT) 1 << 16) - 1; break;
3e73aa7c 2392#ifdef BFD64
64965897 2393 case 4: mask = ((addressT) 1 << 32) - 1; break;
3e73aa7c 2394#endif
64965897 2395 case sizeof (val): return val;
47926f60 2396 default: abort ();
847f7ad4
AM
2397 }
2398
4fe51f7d 2399 if ((val & ~mask) != 0 && (-val & ~mask) != 0)
f493c217
AM
2400 as_warn (_("0x%" PRIx64 " shortened to 0x%" PRIx64),
2401 (uint64_t) val, (uint64_t) (val & mask));
847f7ad4 2402
847f7ad4
AM
2403 return val & mask;
2404}
2405
c32fa91d
L
2406enum PREFIX_GROUP
2407{
2408 PREFIX_EXIST = 0,
2409 PREFIX_LOCK,
2410 PREFIX_REP,
04ef582a 2411 PREFIX_DS,
c32fa91d
L
2412 PREFIX_OTHER
2413};
2414
2415/* Returns
2416 a. PREFIX_EXIST if attempting to add a prefix where one from the
2417 same class already exists.
2418 b. PREFIX_LOCK if lock prefix is added.
2419 c. PREFIX_REP if rep/repne prefix is added.
04ef582a
L
2420 d. PREFIX_DS if ds prefix is added.
2421 e. PREFIX_OTHER if other prefix is added.
c32fa91d
L
2422 */
2423
2424static enum PREFIX_GROUP
e3bb37b5 2425add_prefix (unsigned int prefix)
252b5132 2426{
c32fa91d 2427 enum PREFIX_GROUP ret = PREFIX_OTHER;
b1905489 2428 unsigned int q;
252b5132 2429
29b0f896
AM
2430 if (prefix >= REX_OPCODE && prefix < REX_OPCODE + 16
2431 && flag_code == CODE_64BIT)
b1905489 2432 {
161a04f6 2433 if ((i.prefix[REX_PREFIX] & prefix & REX_W)
44846f29
JB
2434 || (i.prefix[REX_PREFIX] & prefix & REX_R)
2435 || (i.prefix[REX_PREFIX] & prefix & REX_X)
2436 || (i.prefix[REX_PREFIX] & prefix & REX_B))
c32fa91d 2437 ret = PREFIX_EXIST;
b1905489
JB
2438 q = REX_PREFIX;
2439 }
3e73aa7c 2440 else
b1905489
JB
2441 {
2442 switch (prefix)
2443 {
2444 default:
2445 abort ();
2446
b1905489 2447 case DS_PREFIX_OPCODE:
04ef582a
L
2448 ret = PREFIX_DS;
2449 /* Fall through. */
2450 case CS_PREFIX_OPCODE:
b1905489
JB
2451 case ES_PREFIX_OPCODE:
2452 case FS_PREFIX_OPCODE:
2453 case GS_PREFIX_OPCODE:
2454 case SS_PREFIX_OPCODE:
2455 q = SEG_PREFIX;
2456 break;
2457
2458 case REPNE_PREFIX_OPCODE:
2459 case REPE_PREFIX_OPCODE:
c32fa91d
L
2460 q = REP_PREFIX;
2461 ret = PREFIX_REP;
2462 break;
2463
b1905489 2464 case LOCK_PREFIX_OPCODE:
c32fa91d
L
2465 q = LOCK_PREFIX;
2466 ret = PREFIX_LOCK;
b1905489
JB
2467 break;
2468
2469 case FWAIT_OPCODE:
2470 q = WAIT_PREFIX;
2471 break;
2472
2473 case ADDR_PREFIX_OPCODE:
2474 q = ADDR_PREFIX;
2475 break;
2476
2477 case DATA_PREFIX_OPCODE:
2478 q = DATA_PREFIX;
2479 break;
2480 }
2481 if (i.prefix[q] != 0)
c32fa91d 2482 ret = PREFIX_EXIST;
b1905489 2483 }
252b5132 2484
b1905489 2485 if (ret)
252b5132 2486 {
b1905489
JB
2487 if (!i.prefix[q])
2488 ++i.prefixes;
2489 i.prefix[q] |= prefix;
252b5132 2490 }
b1905489
JB
2491 else
2492 as_bad (_("same type of prefix used twice"));
252b5132 2493
252b5132
RH
2494 return ret;
2495}
2496
2497static void
78f12dd3 2498update_code_flag (int value, int check)
eecb386c 2499{
78f12dd3
L
2500 PRINTF_LIKE ((*as_error));
2501
1e9cc1c2 2502 flag_code = (enum flag_code) value;
40fb9820
L
2503 if (flag_code == CODE_64BIT)
2504 {
2505 cpu_arch_flags.bitfield.cpu64 = 1;
2506 cpu_arch_flags.bitfield.cpuno64 = 0;
40fb9820
L
2507 }
2508 else
2509 {
2510 cpu_arch_flags.bitfield.cpu64 = 0;
2511 cpu_arch_flags.bitfield.cpuno64 = 1;
40fb9820
L
2512 }
2513 if (value == CODE_64BIT && !cpu_arch_flags.bitfield.cpulm )
3e73aa7c 2514 {
78f12dd3
L
2515 if (check)
2516 as_error = as_fatal;
2517 else
2518 as_error = as_bad;
2519 (*as_error) (_("64bit mode not supported on `%s'."),
2520 cpu_arch_name ? cpu_arch_name : default_arch);
3e73aa7c 2521 }
40fb9820 2522 if (value == CODE_32BIT && !cpu_arch_flags.bitfield.cpui386)
3e73aa7c 2523 {
78f12dd3
L
2524 if (check)
2525 as_error = as_fatal;
2526 else
2527 as_error = as_bad;
2528 (*as_error) (_("32bit mode not supported on `%s'."),
2529 cpu_arch_name ? cpu_arch_name : default_arch);
3e73aa7c 2530 }
eecb386c
AM
2531 stackop_size = '\0';
2532}
2533
78f12dd3
L
2534static void
2535set_code_flag (int value)
2536{
2537 update_code_flag (value, 0);
2538}
2539
eecb386c 2540static void
e3bb37b5 2541set_16bit_gcc_code_flag (int new_code_flag)
252b5132 2542{
1e9cc1c2 2543 flag_code = (enum flag_code) new_code_flag;
40fb9820
L
2544 if (flag_code != CODE_16BIT)
2545 abort ();
2546 cpu_arch_flags.bitfield.cpu64 = 0;
2547 cpu_arch_flags.bitfield.cpuno64 = 1;
9306ca4a 2548 stackop_size = LONG_MNEM_SUFFIX;
252b5132
RH
2549}
2550
2551static void
e3bb37b5 2552set_intel_syntax (int syntax_flag)
252b5132
RH
2553{
2554 /* Find out if register prefixing is specified. */
2555 int ask_naked_reg = 0;
2556
2557 SKIP_WHITESPACE ();
29b0f896 2558 if (!is_end_of_line[(unsigned char) *input_line_pointer])
252b5132 2559 {
d02603dc
NC
2560 char *string;
2561 int e = get_symbol_name (&string);
252b5132 2562
47926f60 2563 if (strcmp (string, "prefix") == 0)
252b5132 2564 ask_naked_reg = 1;
47926f60 2565 else if (strcmp (string, "noprefix") == 0)
252b5132
RH
2566 ask_naked_reg = -1;
2567 else
d0b47220 2568 as_bad (_("bad argument to syntax directive."));
d02603dc 2569 (void) restore_line_pointer (e);
252b5132
RH
2570 }
2571 demand_empty_rest_of_line ();
c3332e24 2572
252b5132
RH
2573 intel_syntax = syntax_flag;
2574
2575 if (ask_naked_reg == 0)
f86103b7
AM
2576 allow_naked_reg = (intel_syntax
2577 && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
252b5132
RH
2578 else
2579 allow_naked_reg = (ask_naked_reg < 0);
9306ca4a 2580
ee86248c 2581 expr_set_rank (O_full_ptr, syntax_flag ? 10 : 0);
7ab9ffdd 2582
e4a3b5a4 2583 identifier_chars['%'] = intel_syntax && allow_naked_reg ? '%' : 0;
9306ca4a 2584 identifier_chars['$'] = intel_syntax ? '$' : 0;
e4a3b5a4 2585 register_prefix = allow_naked_reg ? "" : "%";
252b5132
RH
2586}
2587
1efbbeb4
L
2588static void
2589set_intel_mnemonic (int mnemonic_flag)
2590{
e1d4d893 2591 intel_mnemonic = mnemonic_flag;
1efbbeb4
L
2592}
2593
db51cc60
L
2594static void
2595set_allow_index_reg (int flag)
2596{
2597 allow_index_reg = flag;
2598}
2599
cb19c032 2600static void
7bab8ab5 2601set_check (int what)
cb19c032 2602{
7bab8ab5
JB
2603 enum check_kind *kind;
2604 const char *str;
2605
2606 if (what)
2607 {
2608 kind = &operand_check;
2609 str = "operand";
2610 }
2611 else
2612 {
2613 kind = &sse_check;
2614 str = "sse";
2615 }
2616
cb19c032
L
2617 SKIP_WHITESPACE ();
2618
2619 if (!is_end_of_line[(unsigned char) *input_line_pointer])
2620 {
d02603dc
NC
2621 char *string;
2622 int e = get_symbol_name (&string);
cb19c032
L
2623
2624 if (strcmp (string, "none") == 0)
7bab8ab5 2625 *kind = check_none;
cb19c032 2626 else if (strcmp (string, "warning") == 0)
7bab8ab5 2627 *kind = check_warning;
cb19c032 2628 else if (strcmp (string, "error") == 0)
7bab8ab5 2629 *kind = check_error;
cb19c032 2630 else
7bab8ab5 2631 as_bad (_("bad argument to %s_check directive."), str);
d02603dc 2632 (void) restore_line_pointer (e);
cb19c032
L
2633 }
2634 else
7bab8ab5 2635 as_bad (_("missing argument for %s_check directive"), str);
cb19c032
L
2636
2637 demand_empty_rest_of_line ();
2638}
2639
8a9036a4
L
2640static void
2641check_cpu_arch_compatible (const char *name ATTRIBUTE_UNUSED,
1e9cc1c2 2642 i386_cpu_flags new_flag ATTRIBUTE_UNUSED)
8a9036a4
L
2643{
2644#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2645 static const char *arch;
2646
c085ab00 2647 /* Intel MCU is only supported on ELF. */
8a9036a4
L
2648 if (!IS_ELF)
2649 return;
2650
2651 if (!arch)
2652 {
2653 /* Use cpu_arch_name if it is set in md_parse_option. Otherwise
2654 use default_arch. */
2655 arch = cpu_arch_name;
2656 if (!arch)
2657 arch = default_arch;
2658 }
2659
81486035 2660 /* If we are targeting Intel MCU, we must enable it. */
648d04db
JB
2661 if ((get_elf_backend_data (stdoutput)->elf_machine_code == EM_IAMCU)
2662 == new_flag.bitfield.cpuiamcu)
81486035
L
2663 return;
2664
8a9036a4
L
2665 as_bad (_("`%s' is not supported on `%s'"), name, arch);
2666#endif
2667}
2668
8180707f
JB
2669static void
2670extend_cpu_sub_arch_name (const char *name)
2671{
2672 if (cpu_sub_arch_name)
2673 cpu_sub_arch_name = reconcat (cpu_sub_arch_name, cpu_sub_arch_name,
ae89daec 2674 ".", name, (const char *) NULL);
8180707f 2675 else
ae89daec 2676 cpu_sub_arch_name = concat (".", name, (const char *) NULL);
8180707f
JB
2677}
2678
e413e4e9 2679static void
e3bb37b5 2680set_cpu_arch (int dummy ATTRIBUTE_UNUSED)
e413e4e9 2681{
f68697e8
JB
2682 typedef struct arch_stack_entry
2683 {
2684 const struct arch_stack_entry *prev;
2685 const char *name;
2686 char *sub_name;
2687 i386_cpu_flags flags;
2688 i386_cpu_flags isa_flags;
2689 enum processor_type isa;
2690 enum flag_code flag_code;
2691 char stackop_size;
2692 bool no_cond_jump_promotion;
2693 } arch_stack_entry;
2694 static const arch_stack_entry *arch_stack_top;
2695
47926f60 2696 SKIP_WHITESPACE ();
e413e4e9 2697
29b0f896 2698 if (!is_end_of_line[(unsigned char) *input_line_pointer])
e413e4e9 2699 {
3ce2ebcf
JB
2700 char *s;
2701 int e = get_symbol_name (&s);
2702 const char *string = s;
2703 unsigned int j = 0;
40fb9820 2704 i386_cpu_flags flags;
e413e4e9 2705
3ce2ebcf
JB
2706 if (strcmp (string, "default") == 0)
2707 {
2708 if (strcmp (default_arch, "iamcu") == 0)
2709 string = default_arch;
2710 else
2711 {
2712 static const i386_cpu_flags cpu_unknown_flags = CPU_UNKNOWN_FLAGS;
2713
2714 cpu_arch_name = NULL;
2715 free (cpu_sub_arch_name);
2716 cpu_sub_arch_name = NULL;
2717 cpu_arch_flags = cpu_unknown_flags;
2718 if (flag_code == CODE_64BIT)
2719 {
2720 cpu_arch_flags.bitfield.cpu64 = 1;
2721 cpu_arch_flags.bitfield.cpuno64 = 0;
2722 }
2723 else
2724 {
2725 cpu_arch_flags.bitfield.cpu64 = 0;
2726 cpu_arch_flags.bitfield.cpuno64 = 1;
2727 }
2728 cpu_arch_isa = PROCESSOR_UNKNOWN;
ae89daec 2729 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
3ce2ebcf
JB
2730 if (!cpu_arch_tune_set)
2731 {
2732 cpu_arch_tune = cpu_arch_isa;
2733 cpu_arch_tune_flags = cpu_arch_isa_flags;
2734 }
2735
2736 j = ARRAY_SIZE (cpu_arch) + 1;
2737 }
2738 }
f68697e8
JB
2739 else if (strcmp (string, "push") == 0)
2740 {
2741 arch_stack_entry *top = XNEW (arch_stack_entry);
2742
2743 top->name = cpu_arch_name;
2744 if (cpu_sub_arch_name)
2745 top->sub_name = xstrdup (cpu_sub_arch_name);
2746 else
2747 top->sub_name = NULL;
2748 top->flags = cpu_arch_flags;
2749 top->isa = cpu_arch_isa;
2750 top->isa_flags = cpu_arch_isa_flags;
2751 top->flag_code = flag_code;
2752 top->stackop_size = stackop_size;
2753 top->no_cond_jump_promotion = no_cond_jump_promotion;
2754
2755 top->prev = arch_stack_top;
2756 arch_stack_top = top;
2757
2758 (void) restore_line_pointer (e);
2759 demand_empty_rest_of_line ();
2760 return;
2761 }
2762 else if (strcmp (string, "pop") == 0)
2763 {
2764 const arch_stack_entry *top = arch_stack_top;
2765
2766 if (!top)
2767 as_bad (_(".arch stack is empty"));
2768 else if (top->flag_code != flag_code
2769 || top->stackop_size != stackop_size)
2770 {
2771 static const unsigned int bits[] = {
2772 [CODE_16BIT] = 16,
2773 [CODE_32BIT] = 32,
2774 [CODE_64BIT] = 64,
2775 };
2776
2777 as_bad (_("this `.arch pop' requires `.code%u%s' to be in effect"),
2778 bits[top->flag_code],
2779 top->stackop_size == LONG_MNEM_SUFFIX ? "gcc" : "");
2780 }
2781 else
2782 {
2783 arch_stack_top = top->prev;
2784
2785 cpu_arch_name = top->name;
2786 free (cpu_sub_arch_name);
2787 cpu_sub_arch_name = top->sub_name;
2788 cpu_arch_flags = top->flags;
2789 cpu_arch_isa = top->isa;
2790 cpu_arch_isa_flags = top->isa_flags;
2791 no_cond_jump_promotion = top->no_cond_jump_promotion;
2792
2793 XDELETE (top);
2794 }
2795
2796 (void) restore_line_pointer (e);
2797 demand_empty_rest_of_line ();
2798 return;
2799 }
3ce2ebcf
JB
2800
2801 for (; j < ARRAY_SIZE (cpu_arch); j++)
e413e4e9 2802 {
ae89daec
JB
2803 if (strcmp (string + (*string == '.'), cpu_arch[j].name) == 0
2804 && (*string == '.') == (cpu_arch[j].type == PROCESSOR_NONE))
e413e4e9 2805 {
5c6af06e
JB
2806 if (*string != '.')
2807 {
ae89daec 2808 check_cpu_arch_compatible (string, cpu_arch[j].enable);
648d04db 2809
91d6fa6a 2810 cpu_arch_name = cpu_arch[j].name;
d92c7521 2811 free (cpu_sub_arch_name);
5c6af06e 2812 cpu_sub_arch_name = NULL;
ae89daec 2813 cpu_arch_flags = cpu_arch[j].enable;
40fb9820
L
2814 if (flag_code == CODE_64BIT)
2815 {
2816 cpu_arch_flags.bitfield.cpu64 = 1;
2817 cpu_arch_flags.bitfield.cpuno64 = 0;
2818 }
2819 else
2820 {
2821 cpu_arch_flags.bitfield.cpu64 = 0;
2822 cpu_arch_flags.bitfield.cpuno64 = 1;
2823 }
91d6fa6a 2824 cpu_arch_isa = cpu_arch[j].type;
ae89daec 2825 cpu_arch_isa_flags = cpu_arch[j].enable;
ccc9c027
L
2826 if (!cpu_arch_tune_set)
2827 {
2828 cpu_arch_tune = cpu_arch_isa;
2829 cpu_arch_tune_flags = cpu_arch_isa_flags;
2830 }
d59a54c2 2831 pre_386_16bit_warned = false;
5c6af06e
JB
2832 break;
2833 }
40fb9820 2834
ae89daec
JB
2835 if (cpu_flags_all_zero (&cpu_arch[j].enable))
2836 continue;
2837
293f5f65 2838 flags = cpu_flags_or (cpu_arch_flags,
ae89daec 2839 cpu_arch[j].enable);
81486035 2840
5b64d091 2841 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
5c6af06e 2842 {
ae89daec 2843 extend_cpu_sub_arch_name (string + 1);
40fb9820 2844 cpu_arch_flags = flags;
a586129e 2845 cpu_arch_isa_flags = flags;
5c6af06e 2846 }
0089dace
L
2847 else
2848 cpu_arch_isa_flags
2849 = cpu_flags_or (cpu_arch_isa_flags,
ae89daec 2850 cpu_arch[j].enable);
d02603dc 2851 (void) restore_line_pointer (e);
5c6af06e
JB
2852 demand_empty_rest_of_line ();
2853 return;
e413e4e9
AM
2854 }
2855 }
293f5f65 2856
ae89daec 2857 if (startswith (string, ".no") && j >= ARRAY_SIZE (cpu_arch))
293f5f65 2858 {
33eaf5de 2859 /* Disable an ISA extension. */
ae89daec
JB
2860 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
2861 if (cpu_arch[j].type == PROCESSOR_NONE
2862 && strcmp (string + 3, cpu_arch[j].name) == 0)
293f5f65
L
2863 {
2864 flags = cpu_flags_and_not (cpu_arch_flags,
ae89daec 2865 cpu_arch[j].disable);
293f5f65
L
2866 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
2867 {
ae89daec 2868 extend_cpu_sub_arch_name (string + 1);
293f5f65
L
2869 cpu_arch_flags = flags;
2870 cpu_arch_isa_flags = flags;
2871 }
2872 (void) restore_line_pointer (e);
2873 demand_empty_rest_of_line ();
2874 return;
2875 }
293f5f65
L
2876 }
2877
3ce2ebcf 2878 if (j == ARRAY_SIZE (cpu_arch))
e413e4e9
AM
2879 as_bad (_("no such architecture: `%s'"), string);
2880
2881 *input_line_pointer = e;
2882 }
2883 else
2884 as_bad (_("missing cpu architecture"));
2885
fddf5b5b
AM
2886 no_cond_jump_promotion = 0;
2887 if (*input_line_pointer == ','
29b0f896 2888 && !is_end_of_line[(unsigned char) input_line_pointer[1]])
fddf5b5b 2889 {
d02603dc
NC
2890 char *string;
2891 char e;
2892
2893 ++input_line_pointer;
2894 e = get_symbol_name (&string);
fddf5b5b
AM
2895
2896 if (strcmp (string, "nojumps") == 0)
2897 no_cond_jump_promotion = 1;
2898 else if (strcmp (string, "jumps") == 0)
2899 ;
2900 else
2901 as_bad (_("no such architecture modifier: `%s'"), string);
2902
d02603dc 2903 (void) restore_line_pointer (e);
fddf5b5b
AM
2904 }
2905
e413e4e9
AM
2906 demand_empty_rest_of_line ();
2907}
2908
8a9036a4
L
2909enum bfd_architecture
2910i386_arch (void)
2911{
c085ab00 2912 if (cpu_arch_isa == PROCESSOR_IAMCU)
81486035
L
2913 {
2914 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2915 || flag_code == CODE_64BIT)
2916 as_fatal (_("Intel MCU is 32bit ELF only"));
2917 return bfd_arch_iamcu;
2918 }
8a9036a4
L
2919 else
2920 return bfd_arch_i386;
2921}
2922
b9d79e03 2923unsigned long
7016a5d5 2924i386_mach (void)
b9d79e03 2925{
d34049e8 2926 if (startswith (default_arch, "x86_64"))
8a9036a4 2927 {
c085ab00 2928 if (default_arch[6] == '\0')
8a9036a4 2929 return bfd_mach_x86_64;
351f65ca
L
2930 else
2931 return bfd_mach_x64_32;
8a9036a4 2932 }
5197d474
L
2933 else if (!strcmp (default_arch, "i386")
2934 || !strcmp (default_arch, "iamcu"))
81486035
L
2935 {
2936 if (cpu_arch_isa == PROCESSOR_IAMCU)
2937 {
2938 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
2939 as_fatal (_("Intel MCU is 32bit ELF only"));
2940 return bfd_mach_i386_iamcu;
2941 }
2942 else
2943 return bfd_mach_i386_i386;
2944 }
b9d79e03 2945 else
2b5d6a91 2946 as_fatal (_("unknown architecture"));
b9d79e03 2947}
b9d79e03 2948\f
252b5132 2949void
7016a5d5 2950md_begin (void)
252b5132 2951{
86fa6981
L
2952 /* Support pseudo prefixes like {disp32}. */
2953 lex_type ['{'] = LEX_BEGIN_NAME;
2954
47926f60 2955 /* Initialize op_hash hash table. */
629310ab 2956 op_hash = str_htab_create ();
252b5132
RH
2957
2958 {
d3ce72d0 2959 const insn_template *optab;
29b0f896 2960 templates *core_optab;
252b5132 2961
47926f60
KH
2962 /* Setup for loop. */
2963 optab = i386_optab;
654d6f31 2964 core_optab = notes_alloc (sizeof (*core_optab));
252b5132
RH
2965 core_optab->start = optab;
2966
2967 while (1)
2968 {
2969 ++optab;
2970 if (optab->name == NULL
2971 || strcmp (optab->name, (optab - 1)->name) != 0)
2972 {
2973 /* different name --> ship out current template list;
47926f60 2974 add to hash table; & begin anew. */
252b5132 2975 core_optab->end = optab;
fe0e921f
AM
2976 if (str_hash_insert (op_hash, (optab - 1)->name, core_optab, 0))
2977 as_fatal (_("duplicate %s"), (optab - 1)->name);
2978
252b5132
RH
2979 if (optab->name == NULL)
2980 break;
654d6f31 2981 core_optab = notes_alloc (sizeof (*core_optab));
252b5132
RH
2982 core_optab->start = optab;
2983 }
2984 }
2985 }
2986
47926f60 2987 /* Initialize reg_hash hash table. */
629310ab 2988 reg_hash = str_htab_create ();
252b5132 2989 {
29b0f896 2990 const reg_entry *regtab;
c3fe08fa 2991 unsigned int regtab_size = i386_regtab_size;
252b5132 2992
c3fe08fa 2993 for (regtab = i386_regtab; regtab_size--; regtab++)
6225c532 2994 {
6288d05f
JB
2995 switch (regtab->reg_type.bitfield.class)
2996 {
2997 case Reg:
34684862
JB
2998 if (regtab->reg_type.bitfield.dword)
2999 {
3000 if (regtab->reg_type.bitfield.instance == Accum)
3001 reg_eax = regtab;
3002 }
3003 else if (regtab->reg_type.bitfield.tbyte)
6288d05f
JB
3004 {
3005 /* There's no point inserting st(<N>) in the hash table, as
3006 parentheses aren't included in register_chars[] anyway. */
3007 if (regtab->reg_type.bitfield.instance != Accum)
3008 continue;
3009 reg_st0 = regtab;
3010 }
3011 break;
3012
5e042380
JB
3013 case SReg:
3014 switch (regtab->reg_num)
3015 {
3016 case 0: reg_es = regtab; break;
3017 case 2: reg_ss = regtab; break;
3018 case 3: reg_ds = regtab; break;
3019 }
3020 break;
3021
6288d05f
JB
3022 case RegMask:
3023 if (!regtab->reg_num)
3024 reg_k0 = regtab;
3025 break;
3026 }
3027
6225c532
JB
3028 if (str_hash_insert (reg_hash, regtab->reg_name, regtab, 0) != NULL)
3029 as_fatal (_("duplicate %s"), regtab->reg_name);
6225c532 3030 }
252b5132
RH
3031 }
3032
47926f60 3033 /* Fill in lexical tables: mnemonic_chars, operand_chars. */
252b5132 3034 {
29b0f896
AM
3035 int c;
3036 char *p;
252b5132
RH
3037
3038 for (c = 0; c < 256; c++)
3039 {
014fbcda 3040 if (ISDIGIT (c) || ISLOWER (c))
252b5132
RH
3041 {
3042 mnemonic_chars[c] = c;
3043 register_chars[c] = c;
3044 operand_chars[c] = c;
3045 }
3882b010 3046 else if (ISUPPER (c))
252b5132 3047 {
3882b010 3048 mnemonic_chars[c] = TOLOWER (c);
252b5132
RH
3049 register_chars[c] = mnemonic_chars[c];
3050 operand_chars[c] = c;
3051 }
43234a1e 3052 else if (c == '{' || c == '}')
86fa6981
L
3053 {
3054 mnemonic_chars[c] = c;
3055 operand_chars[c] = c;
3056 }
b3983e5f
JB
3057#ifdef SVR4_COMMENT_CHARS
3058 else if (c == '\\' && strchr (i386_comment_chars, '/'))
3059 operand_chars[c] = c;
3060#endif
252b5132 3061
3882b010 3062 if (ISALPHA (c) || ISDIGIT (c))
252b5132
RH
3063 identifier_chars[c] = c;
3064 else if (c >= 128)
3065 {
3066 identifier_chars[c] = c;
3067 operand_chars[c] = c;
3068 }
3069 }
3070
3071#ifdef LEX_AT
3072 identifier_chars['@'] = '@';
32137342
NC
3073#endif
3074#ifdef LEX_QM
3075 identifier_chars['?'] = '?';
3076 operand_chars['?'] = '?';
252b5132 3077#endif
c0f3af97 3078 mnemonic_chars['_'] = '_';
791fe849 3079 mnemonic_chars['-'] = '-';
0003779b 3080 mnemonic_chars['.'] = '.';
252b5132
RH
3081 identifier_chars['_'] = '_';
3082 identifier_chars['.'] = '.';
3083
3084 for (p = operand_special_chars; *p != '\0'; p++)
3085 operand_chars[(unsigned char) *p] = *p;
3086 }
3087
a4447b93
RH
3088 if (flag_code == CODE_64BIT)
3089 {
ca19b261
KT
3090#if defined (OBJ_COFF) && defined (TE_PE)
3091 x86_dwarf2_return_column = (OUTPUT_FLAVOR == bfd_target_coff_flavour
3092 ? 32 : 16);
3093#else
a4447b93 3094 x86_dwarf2_return_column = 16;
ca19b261 3095#endif
61ff971f 3096 x86_cie_data_alignment = -8;
a4447b93
RH
3097 }
3098 else
3099 {
3100 x86_dwarf2_return_column = 8;
3101 x86_cie_data_alignment = -4;
3102 }
e379e5f3
L
3103
3104 /* NB: FUSED_JCC_PADDING frag must have sufficient room so that it
3105 can be turned into BRANCH_PREFIX frag. */
3106 if (align_branch_prefix_size > MAX_FUSED_JCC_PADDING_SIZE)
3107 abort ();
252b5132
RH
3108}
3109
3110void
e3bb37b5 3111i386_print_statistics (FILE *file)
252b5132 3112{
629310ab
ML
3113 htab_print_statistics (file, "i386 opcode", op_hash);
3114 htab_print_statistics (file, "i386 register", reg_hash);
252b5132 3115}
654d6f31
AM
3116
3117void
3118i386_md_end (void)
3119{
3120 htab_delete (op_hash);
3121 htab_delete (reg_hash);
3122}
252b5132 3123\f
252b5132
RH
3124#ifdef DEBUG386
3125
ce8a8b2f 3126/* Debugging routines for md_assemble. */
d3ce72d0 3127static void pte (insn_template *);
40fb9820 3128static void pt (i386_operand_type);
e3bb37b5
L
3129static void pe (expressionS *);
3130static void ps (symbolS *);
252b5132
RH
3131
3132static void
2c703856 3133pi (const char *line, i386_insn *x)
252b5132 3134{
09137c09 3135 unsigned int j;
252b5132
RH
3136
3137 fprintf (stdout, "%s: template ", line);
3138 pte (&x->tm);
09f131f2
JH
3139 fprintf (stdout, " address: base %s index %s scale %x\n",
3140 x->base_reg ? x->base_reg->reg_name : "none",
3141 x->index_reg ? x->index_reg->reg_name : "none",
3142 x->log2_scale_factor);
3143 fprintf (stdout, " modrm: mode %x reg %x reg/mem %x\n",
252b5132 3144 x->rm.mode, x->rm.reg, x->rm.regmem);
09f131f2
JH
3145 fprintf (stdout, " sib: base %x index %x scale %x\n",
3146 x->sib.base, x->sib.index, x->sib.scale);
3147 fprintf (stdout, " rex: 64bit %x extX %x extY %x extZ %x\n",
161a04f6
L
3148 (x->rex & REX_W) != 0,
3149 (x->rex & REX_R) != 0,
3150 (x->rex & REX_X) != 0,
3151 (x->rex & REX_B) != 0);
09137c09 3152 for (j = 0; j < x->operands; j++)
252b5132 3153 {
09137c09
SP
3154 fprintf (stdout, " #%d: ", j + 1);
3155 pt (x->types[j]);
252b5132 3156 fprintf (stdout, "\n");
bab6aec1 3157 if (x->types[j].bitfield.class == Reg
3528c362
JB
3158 || x->types[j].bitfield.class == RegMMX
3159 || x->types[j].bitfield.class == RegSIMD
dd6b8a0b 3160 || x->types[j].bitfield.class == RegMask
00cee14f 3161 || x->types[j].bitfield.class == SReg
4a5c67ed
JB
3162 || x->types[j].bitfield.class == RegCR
3163 || x->types[j].bitfield.class == RegDR
dd6b8a0b
JB
3164 || x->types[j].bitfield.class == RegTR
3165 || x->types[j].bitfield.class == RegBND)
09137c09
SP
3166 fprintf (stdout, "%s\n", x->op[j].regs->reg_name);
3167 if (operand_type_check (x->types[j], imm))
3168 pe (x->op[j].imms);
3169 if (operand_type_check (x->types[j], disp))
3170 pe (x->op[j].disps);
252b5132
RH
3171 }
3172}
3173
3174static void
d3ce72d0 3175pte (insn_template *t)
252b5132 3176{
b933fa4b 3177 static const unsigned char opc_pfx[] = { 0, 0x66, 0xf3, 0xf2 };
441f6aca 3178 static const char *const opc_spc[] = {
0cc78721 3179 NULL, "0f", "0f38", "0f3a", NULL, "evexmap5", "evexmap6", NULL,
441f6aca
JB
3180 "XOP08", "XOP09", "XOP0A",
3181 };
09137c09 3182 unsigned int j;
441f6aca 3183
252b5132 3184 fprintf (stdout, " %d operands ", t->operands);
441f6aca
JB
3185 if (opc_pfx[t->opcode_modifier.opcodeprefix])
3186 fprintf (stdout, "pfx %x ", opc_pfx[t->opcode_modifier.opcodeprefix]);
3187 if (opc_spc[t->opcode_modifier.opcodespace])
3188 fprintf (stdout, "space %s ", opc_spc[t->opcode_modifier.opcodespace]);
47926f60 3189 fprintf (stdout, "opcode %x ", t->base_opcode);
252b5132
RH
3190 if (t->extension_opcode != None)
3191 fprintf (stdout, "ext %x ", t->extension_opcode);
40fb9820 3192 if (t->opcode_modifier.d)
252b5132 3193 fprintf (stdout, "D");
40fb9820 3194 if (t->opcode_modifier.w)
252b5132
RH
3195 fprintf (stdout, "W");
3196 fprintf (stdout, "\n");
09137c09 3197 for (j = 0; j < t->operands; j++)
252b5132 3198 {
09137c09
SP
3199 fprintf (stdout, " #%d type ", j + 1);
3200 pt (t->operand_types[j]);
252b5132
RH
3201 fprintf (stdout, "\n");
3202 }
3203}
3204
3205static void
e3bb37b5 3206pe (expressionS *e)
252b5132 3207{
24eab124 3208 fprintf (stdout, " operation %d\n", e->X_op);
b8281767
AM
3209 fprintf (stdout, " add_number %" PRId64 " (%" PRIx64 ")\n",
3210 (int64_t) e->X_add_number, (uint64_t) (valueT) e->X_add_number);
252b5132
RH
3211 if (e->X_add_symbol)
3212 {
3213 fprintf (stdout, " add_symbol ");
3214 ps (e->X_add_symbol);
3215 fprintf (stdout, "\n");
3216 }
3217 if (e->X_op_symbol)
3218 {
3219 fprintf (stdout, " op_symbol ");
3220 ps (e->X_op_symbol);
3221 fprintf (stdout, "\n");
3222 }
3223}
3224
3225static void
e3bb37b5 3226ps (symbolS *s)
252b5132
RH
3227{
3228 fprintf (stdout, "%s type %s%s",
3229 S_GET_NAME (s),
3230 S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
3231 segment_name (S_GET_SEGMENT (s)));
3232}
3233
7b81dfbb 3234static struct type_name
252b5132 3235 {
40fb9820
L
3236 i386_operand_type mask;
3237 const char *name;
252b5132 3238 }
7b81dfbb 3239const type_names[] =
252b5132 3240{
40fb9820
L
3241 { OPERAND_TYPE_REG8, "r8" },
3242 { OPERAND_TYPE_REG16, "r16" },
3243 { OPERAND_TYPE_REG32, "r32" },
3244 { OPERAND_TYPE_REG64, "r64" },
2c703856
JB
3245 { OPERAND_TYPE_ACC8, "acc8" },
3246 { OPERAND_TYPE_ACC16, "acc16" },
3247 { OPERAND_TYPE_ACC32, "acc32" },
3248 { OPERAND_TYPE_ACC64, "acc64" },
40fb9820
L
3249 { OPERAND_TYPE_IMM8, "i8" },
3250 { OPERAND_TYPE_IMM8, "i8s" },
3251 { OPERAND_TYPE_IMM16, "i16" },
3252 { OPERAND_TYPE_IMM32, "i32" },
3253 { OPERAND_TYPE_IMM32S, "i32s" },
3254 { OPERAND_TYPE_IMM64, "i64" },
3255 { OPERAND_TYPE_IMM1, "i1" },
3256 { OPERAND_TYPE_BASEINDEX, "BaseIndex" },
3257 { OPERAND_TYPE_DISP8, "d8" },
3258 { OPERAND_TYPE_DISP16, "d16" },
3259 { OPERAND_TYPE_DISP32, "d32" },
40fb9820
L
3260 { OPERAND_TYPE_DISP64, "d64" },
3261 { OPERAND_TYPE_INOUTPORTREG, "InOutPortReg" },
3262 { OPERAND_TYPE_SHIFTCOUNT, "ShiftCount" },
3263 { OPERAND_TYPE_CONTROL, "control reg" },
3264 { OPERAND_TYPE_TEST, "test reg" },
3265 { OPERAND_TYPE_DEBUG, "debug reg" },
3266 { OPERAND_TYPE_FLOATREG, "FReg" },
3267 { OPERAND_TYPE_FLOATACC, "FAcc" },
21df382b 3268 { OPERAND_TYPE_SREG, "SReg" },
40fb9820
L
3269 { OPERAND_TYPE_REGMMX, "rMMX" },
3270 { OPERAND_TYPE_REGXMM, "rXMM" },
0349dc08 3271 { OPERAND_TYPE_REGYMM, "rYMM" },
43234a1e 3272 { OPERAND_TYPE_REGZMM, "rZMM" },
260cd341 3273 { OPERAND_TYPE_REGTMM, "rTMM" },
43234a1e 3274 { OPERAND_TYPE_REGMASK, "Mask reg" },
252b5132
RH
3275};
3276
3277static void
40fb9820 3278pt (i386_operand_type t)
252b5132 3279{
40fb9820 3280 unsigned int j;
c6fb90c8 3281 i386_operand_type a;
252b5132 3282
40fb9820 3283 for (j = 0; j < ARRAY_SIZE (type_names); j++)
c6fb90c8
L
3284 {
3285 a = operand_type_and (t, type_names[j].mask);
2c703856 3286 if (operand_type_equal (&a, &type_names[j].mask))
c6fb90c8
L
3287 fprintf (stdout, "%s, ", type_names[j].name);
3288 }
252b5132
RH
3289 fflush (stdout);
3290}
3291
3292#endif /* DEBUG386 */
3293\f
252b5132 3294static bfd_reloc_code_real_type
3956db08 3295reloc (unsigned int size,
64e74474
AM
3296 int pcrel,
3297 int sign,
3298 bfd_reloc_code_real_type other)
252b5132 3299{
47926f60 3300 if (other != NO_RELOC)
3956db08 3301 {
91d6fa6a 3302 reloc_howto_type *rel;
3956db08
JB
3303
3304 if (size == 8)
3305 switch (other)
3306 {
64e74474
AM
3307 case BFD_RELOC_X86_64_GOT32:
3308 return BFD_RELOC_X86_64_GOT64;
3309 break;
553d1284
L
3310 case BFD_RELOC_X86_64_GOTPLT64:
3311 return BFD_RELOC_X86_64_GOTPLT64;
3312 break;
64e74474
AM
3313 case BFD_RELOC_X86_64_PLTOFF64:
3314 return BFD_RELOC_X86_64_PLTOFF64;
3315 break;
3316 case BFD_RELOC_X86_64_GOTPC32:
3317 other = BFD_RELOC_X86_64_GOTPC64;
3318 break;
3319 case BFD_RELOC_X86_64_GOTPCREL:
3320 other = BFD_RELOC_X86_64_GOTPCREL64;
3321 break;
3322 case BFD_RELOC_X86_64_TPOFF32:
3323 other = BFD_RELOC_X86_64_TPOFF64;
3324 break;
3325 case BFD_RELOC_X86_64_DTPOFF32:
3326 other = BFD_RELOC_X86_64_DTPOFF64;
3327 break;
3328 default:
3329 break;
3956db08 3330 }
e05278af 3331
8ce3d284 3332#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
3333 if (other == BFD_RELOC_SIZE32)
3334 {
3335 if (size == 8)
1ab668bf 3336 other = BFD_RELOC_SIZE64;
8fd4256d 3337 if (pcrel)
1ab668bf
AM
3338 {
3339 as_bad (_("there are no pc-relative size relocations"));
3340 return NO_RELOC;
3341 }
8fd4256d 3342 }
8ce3d284 3343#endif
8fd4256d 3344
e05278af 3345 /* Sign-checking 4-byte relocations in 16-/32-bit code is pointless. */
f2d8a97c 3346 if (size == 4 && (flag_code != CODE_64BIT || disallow_64bit_reloc))
e05278af
JB
3347 sign = -1;
3348
91d6fa6a
NC
3349 rel = bfd_reloc_type_lookup (stdoutput, other);
3350 if (!rel)
3956db08 3351 as_bad (_("unknown relocation (%u)"), other);
91d6fa6a 3352 else if (size != bfd_get_reloc_size (rel))
3956db08 3353 as_bad (_("%u-byte relocation cannot be applied to %u-byte field"),
91d6fa6a 3354 bfd_get_reloc_size (rel),
3956db08 3355 size);
91d6fa6a 3356 else if (pcrel && !rel->pc_relative)
3956db08 3357 as_bad (_("non-pc-relative relocation for pc-relative field"));
91d6fa6a 3358 else if ((rel->complain_on_overflow == complain_overflow_signed
3956db08 3359 && !sign)
91d6fa6a 3360 || (rel->complain_on_overflow == complain_overflow_unsigned
64e74474 3361 && sign > 0))
3956db08
JB
3362 as_bad (_("relocated field and relocation type differ in signedness"));
3363 else
3364 return other;
3365 return NO_RELOC;
3366 }
252b5132
RH
3367
3368 if (pcrel)
3369 {
3e73aa7c 3370 if (!sign)
3956db08 3371 as_bad (_("there are no unsigned pc-relative relocations"));
252b5132
RH
3372 switch (size)
3373 {
3374 case 1: return BFD_RELOC_8_PCREL;
3375 case 2: return BFD_RELOC_16_PCREL;
d258b828 3376 case 4: return BFD_RELOC_32_PCREL;
d6ab8113 3377 case 8: return BFD_RELOC_64_PCREL;
252b5132 3378 }
3956db08 3379 as_bad (_("cannot do %u byte pc-relative relocation"), size);
252b5132
RH
3380 }
3381 else
3382 {
3956db08 3383 if (sign > 0)
e5cb08ac 3384 switch (size)
3e73aa7c
JH
3385 {
3386 case 4: return BFD_RELOC_X86_64_32S;
3387 }
3388 else
3389 switch (size)
3390 {
3391 case 1: return BFD_RELOC_8;
3392 case 2: return BFD_RELOC_16;
3393 case 4: return BFD_RELOC_32;
3394 case 8: return BFD_RELOC_64;
3395 }
3956db08
JB
3396 as_bad (_("cannot do %s %u byte relocation"),
3397 sign > 0 ? "signed" : "unsigned", size);
252b5132
RH
3398 }
3399
0cc9e1d3 3400 return NO_RELOC;
252b5132
RH
3401}
3402
47926f60
KH
3403/* Here we decide which fixups can be adjusted to make them relative to
3404 the beginning of the section instead of the symbol. Basically we need
3405 to make sure that the dynamic relocations are done correctly, so in
3406 some cases we force the original symbol to be used. */
3407
252b5132 3408int
e3bb37b5 3409tc_i386_fix_adjustable (fixS *fixP ATTRIBUTE_UNUSED)
252b5132 3410{
6d249963 3411#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 3412 if (!IS_ELF)
31312f95
AM
3413 return 1;
3414
a161fe53
AM
3415 /* Don't adjust pc-relative references to merge sections in 64-bit
3416 mode. */
3417 if (use_rela_relocations
3418 && (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0
3419 && fixP->fx_pcrel)
252b5132 3420 return 0;
31312f95 3421
8d01d9a9
AJ
3422 /* The x86_64 GOTPCREL are represented as 32bit PCrel relocations
3423 and changed later by validate_fix. */
3424 if (GOT_symbol && fixP->fx_subsy == GOT_symbol
3425 && fixP->fx_r_type == BFD_RELOC_32_PCREL)
3426 return 0;
3427
8fd4256d
L
3428 /* Adjust_reloc_syms doesn't know about the GOT. Need to keep symbol
3429 for size relocations. */
3430 if (fixP->fx_r_type == BFD_RELOC_SIZE32
3431 || fixP->fx_r_type == BFD_RELOC_SIZE64
3432 || fixP->fx_r_type == BFD_RELOC_386_GOTOFF
252b5132 3433 || fixP->fx_r_type == BFD_RELOC_386_GOT32
02a86693 3434 || fixP->fx_r_type == BFD_RELOC_386_GOT32X
13ae64f3
JJ
3435 || fixP->fx_r_type == BFD_RELOC_386_TLS_GD
3436 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDM
3437 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDO_32
3438 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE_32
37e55690
JJ
3439 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE
3440 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTIE
13ae64f3
JJ
3441 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE_32
3442 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE
67a4f2b7
AO
3443 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTDESC
3444 || fixP->fx_r_type == BFD_RELOC_386_TLS_DESC_CALL
3e73aa7c 3445 || fixP->fx_r_type == BFD_RELOC_X86_64_GOT32
80b3ee89 3446 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCREL
56ceb5b5
L
3447 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCRELX
3448 || fixP->fx_r_type == BFD_RELOC_X86_64_REX_GOTPCRELX
bffbf940
JJ
3449 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSGD
3450 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSLD
3451 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF32
d6ab8113 3452 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF64
bffbf940
JJ
3453 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF
3454 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF32
d6ab8113
JB
3455 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF64
3456 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTOFF64
67a4f2b7
AO
3457 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
3458 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSDESC_CALL
252b5132
RH
3459 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3460 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3461 return 0;
31312f95 3462#endif
252b5132
RH
3463 return 1;
3464}
252b5132 3465
a9aabc23
JB
3466static INLINE bool
3467want_disp32 (const insn_template *t)
3468{
3469 return flag_code != CODE_64BIT
3470 || i.prefix[ADDR_PREFIX]
3471 || (t->base_opcode == 0x8d
3472 && t->opcode_modifier.opcodespace == SPACE_BASE
fe134c65
JB
3473 && (!i.types[1].bitfield.qword
3474 || t->opcode_modifier.size == SIZE32));
a9aabc23
JB
3475}
3476
b4cac588 3477static int
e3bb37b5 3478intel_float_operand (const char *mnemonic)
252b5132 3479{
9306ca4a
JB
3480 /* Note that the value returned is meaningful only for opcodes with (memory)
3481 operands, hence the code here is free to improperly handle opcodes that
3482 have no operands (for better performance and smaller code). */
3483
3484 if (mnemonic[0] != 'f')
3485 return 0; /* non-math */
3486
3487 switch (mnemonic[1])
3488 {
3489 /* fclex, fdecstp, fdisi, femms, feni, fincstp, finit, fsetpm, and
3490 the fs segment override prefix not currently handled because no
3491 call path can make opcodes without operands get here */
3492 case 'i':
3493 return 2 /* integer op */;
3494 case 'l':
3495 if (mnemonic[2] == 'd' && (mnemonic[3] == 'c' || mnemonic[3] == 'e'))
3496 return 3; /* fldcw/fldenv */
3497 break;
3498 case 'n':
3499 if (mnemonic[2] != 'o' /* fnop */)
3500 return 3; /* non-waiting control op */
3501 break;
3502 case 'r':
3503 if (mnemonic[2] == 's')
3504 return 3; /* frstor/frstpm */
3505 break;
3506 case 's':
3507 if (mnemonic[2] == 'a')
3508 return 3; /* fsave */
3509 if (mnemonic[2] == 't')
3510 {
3511 switch (mnemonic[3])
3512 {
3513 case 'c': /* fstcw */
3514 case 'd': /* fstdw */
3515 case 'e': /* fstenv */
3516 case 's': /* fsts[gw] */
3517 return 3;
3518 }
3519 }
3520 break;
3521 case 'x':
3522 if (mnemonic[2] == 'r' || mnemonic[2] == 's')
3523 return 0; /* fxsave/fxrstor are not really math ops */
3524 break;
3525 }
252b5132 3526
9306ca4a 3527 return 1;
252b5132
RH
3528}
3529
9a182d04
JB
3530static INLINE void
3531install_template (const insn_template *t)
3532{
3533 unsigned int l;
3534
3535 i.tm = *t;
3536
3537 /* Note that for pseudo prefixes this produces a length of 1. But for them
3538 the length isn't interesting at all. */
3539 for (l = 1; l < 4; ++l)
3540 if (!(t->base_opcode >> (8 * l)))
3541 break;
3542
3543 i.opcode_length = l;
3544}
3545
c0f3af97
L
3546/* Build the VEX prefix. */
3547
3548static void
d3ce72d0 3549build_vex_prefix (const insn_template *t)
c0f3af97
L
3550{
3551 unsigned int register_specifier;
c0f3af97 3552 unsigned int vector_length;
03751133 3553 unsigned int w;
c0f3af97
L
3554
3555 /* Check register specifier. */
3556 if (i.vex.register_specifier)
43234a1e
L
3557 {
3558 register_specifier =
3559 ~register_number (i.vex.register_specifier) & 0xf;
3560 gas_assert ((i.vex.register_specifier->reg_flags & RegVRex) == 0);
3561 }
c0f3af97
L
3562 else
3563 register_specifier = 0xf;
3564
79f0fa25
L
3565 /* Use 2-byte VEX prefix by swapping destination and source operand
3566 if there are more than 1 register operand. */
3567 if (i.reg_operands > 1
3568 && i.vec_encoding != vex_encoding_vex3
86fa6981 3569 && i.dir_encoding == dir_encoding_default
fa99fab2 3570 && i.operands == i.reg_operands
dbbc8b7e 3571 && operand_type_equal (&i.types[0], &i.types[i.operands - 1])
441f6aca 3572 && i.tm.opcode_modifier.opcodespace == SPACE_0F
dbbc8b7e 3573 && (i.tm.opcode_modifier.load || i.tm.opcode_modifier.d)
fa99fab2
L
3574 && i.rex == REX_B)
3575 {
3576 unsigned int xchg = i.operands - 1;
3577 union i386_op temp_op;
3578 i386_operand_type temp_type;
3579
3580 temp_type = i.types[xchg];
3581 i.types[xchg] = i.types[0];
3582 i.types[0] = temp_type;
3583 temp_op = i.op[xchg];
3584 i.op[xchg] = i.op[0];
3585 i.op[0] = temp_op;
3586
9c2799c2 3587 gas_assert (i.rm.mode == 3);
fa99fab2
L
3588
3589 i.rex = REX_R;
3590 xchg = i.rm.regmem;
3591 i.rm.regmem = i.rm.reg;
3592 i.rm.reg = xchg;
3593
dbbc8b7e
JB
3594 if (i.tm.opcode_modifier.d)
3595 i.tm.base_opcode ^= (i.tm.base_opcode & 0xee) != 0x6e
2c735193 3596 ? Opcode_ExtD : Opcode_SIMD_IntD;
dbbc8b7e 3597 else /* Use the next insn. */
9a182d04 3598 install_template (&t[1]);
fa99fab2
L
3599 }
3600
79dec6b7
JB
3601 /* Use 2-byte VEX prefix by swapping commutative source operands if there
3602 are no memory operands and at least 3 register ones. */
3603 if (i.reg_operands >= 3
3604 && i.vec_encoding != vex_encoding_vex3
3605 && i.reg_operands == i.operands - i.imm_operands
3606 && i.tm.opcode_modifier.vex
3607 && i.tm.opcode_modifier.commutative
3608 && (i.tm.opcode_modifier.sse2avx || optimize > 1)
3609 && i.rex == REX_B
3610 && i.vex.register_specifier
3611 && !(i.vex.register_specifier->reg_flags & RegRex))
3612 {
3613 unsigned int xchg = i.operands - i.reg_operands;
3614 union i386_op temp_op;
3615 i386_operand_type temp_type;
3616
441f6aca 3617 gas_assert (i.tm.opcode_modifier.opcodespace == SPACE_0F);
79dec6b7
JB
3618 gas_assert (!i.tm.opcode_modifier.sae);
3619 gas_assert (operand_type_equal (&i.types[i.operands - 2],
3620 &i.types[i.operands - 3]));
3621 gas_assert (i.rm.mode == 3);
3622
3623 temp_type = i.types[xchg];
3624 i.types[xchg] = i.types[xchg + 1];
3625 i.types[xchg + 1] = temp_type;
3626 temp_op = i.op[xchg];
3627 i.op[xchg] = i.op[xchg + 1];
3628 i.op[xchg + 1] = temp_op;
3629
3630 i.rex = 0;
3631 xchg = i.rm.regmem | 8;
3632 i.rm.regmem = ~register_specifier & 0xf;
3633 gas_assert (!(i.rm.regmem & 8));
3634 i.vex.register_specifier += xchg - i.rm.regmem;
3635 register_specifier = ~xchg & 0xf;
3636 }
3637
539f890d
L
3638 if (i.tm.opcode_modifier.vex == VEXScalar)
3639 vector_length = avxscalar;
10c17abd
JB
3640 else if (i.tm.opcode_modifier.vex == VEX256)
3641 vector_length = 1;
539f890d 3642 else
10c17abd 3643 {
56522fc5 3644 unsigned int op;
10c17abd 3645
c7213af9
L
3646 /* Determine vector length from the last multi-length vector
3647 operand. */
10c17abd 3648 vector_length = 0;
56522fc5 3649 for (op = t->operands; op--;)
10c17abd
JB
3650 if (t->operand_types[op].bitfield.xmmword
3651 && t->operand_types[op].bitfield.ymmword
3652 && i.types[op].bitfield.ymmword)
3653 {
3654 vector_length = 1;
3655 break;
3656 }
3657 }
c0f3af97 3658
03751133
L
3659 /* Check the REX.W bit and VEXW. */
3660 if (i.tm.opcode_modifier.vexw == VEXWIG)
3661 w = (vexwig == vexw1 || (i.rex & REX_W)) ? 1 : 0;
3662 else if (i.tm.opcode_modifier.vexw)
3663 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3664 else
931d03b7 3665 w = (flag_code == CODE_64BIT ? i.rex & REX_W : vexwig == vexw1) ? 1 : 0;
03751133 3666
c0f3af97 3667 /* Use 2-byte VEX prefix if possible. */
03751133
L
3668 if (w == 0
3669 && i.vec_encoding != vex_encoding_vex3
441f6aca 3670 && i.tm.opcode_modifier.opcodespace == SPACE_0F
c0f3af97
L
3671 && (i.rex & (REX_W | REX_X | REX_B)) == 0)
3672 {
3673 /* 2-byte VEX prefix. */
3674 unsigned int r;
3675
3676 i.vex.length = 2;
3677 i.vex.bytes[0] = 0xc5;
3678
3679 /* Check the REX.R bit. */
3680 r = (i.rex & REX_R) ? 0 : 1;
3681 i.vex.bytes[1] = (r << 7
3682 | register_specifier << 3
3683 | vector_length << 2
35648716 3684 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3685 }
3686 else
3687 {
3688 /* 3-byte VEX prefix. */
f88c9eb0 3689 i.vex.length = 3;
f88c9eb0 3690
441f6aca 3691 switch (i.tm.opcode_modifier.opcodespace)
5dd85c99 3692 {
441f6aca
JB
3693 case SPACE_0F:
3694 case SPACE_0F38:
3695 case SPACE_0F3A:
80de6e00 3696 i.vex.bytes[0] = 0xc4;
7f399153 3697 break;
441f6aca
JB
3698 case SPACE_XOP08:
3699 case SPACE_XOP09:
3700 case SPACE_XOP0A:
f88c9eb0 3701 i.vex.bytes[0] = 0x8f;
7f399153
L
3702 break;
3703 default:
3704 abort ();
f88c9eb0 3705 }
c0f3af97 3706
c0f3af97
L
3707 /* The high 3 bits of the second VEX byte are 1's compliment
3708 of RXB bits from REX. */
441f6aca 3709 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | i.tm.opcode_modifier.opcodespace;
c0f3af97 3710
c0f3af97
L
3711 i.vex.bytes[2] = (w << 7
3712 | register_specifier << 3
3713 | vector_length << 2
35648716 3714 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3715 }
3716}
3717
5b7c81bd 3718static INLINE bool
e771e7c9
JB
3719is_evex_encoding (const insn_template *t)
3720{
7091c612 3721 return t->opcode_modifier.evex || t->opcode_modifier.disp8memshift
e771e7c9 3722 || t->opcode_modifier.broadcast || t->opcode_modifier.masking
a80195f1 3723 || t->opcode_modifier.sae;
e771e7c9
JB
3724}
3725
5b7c81bd 3726static INLINE bool
7a8655d2
JB
3727is_any_vex_encoding (const insn_template *t)
3728{
7b47a312 3729 return t->opcode_modifier.vex || is_evex_encoding (t);
7a8655d2
JB
3730}
3731
a5748e0d
JB
3732static unsigned int
3733get_broadcast_bytes (const insn_template *t, bool diag)
3734{
3735 unsigned int op, bytes;
3736 const i386_operand_type *types;
3737
3738 if (i.broadcast.type)
3739 return i.broadcast.bytes = ((1 << (t->opcode_modifier.broadcast - 1))
3740 * i.broadcast.type);
3741
3742 gas_assert (intel_syntax);
3743
3744 for (op = 0; op < t->operands; ++op)
3745 if (t->operand_types[op].bitfield.baseindex)
3746 break;
3747
3748 gas_assert (op < t->operands);
3749
3750 if (t->opcode_modifier.evex
3751 && t->opcode_modifier.evex != EVEXDYN)
3752 switch (i.broadcast.bytes)
3753 {
3754 case 1:
3755 if (t->operand_types[op].bitfield.word)
3756 return 2;
3757 /* Fall through. */
3758 case 2:
3759 if (t->operand_types[op].bitfield.dword)
3760 return 4;
3761 /* Fall through. */
3762 case 4:
3763 if (t->operand_types[op].bitfield.qword)
3764 return 8;
3765 /* Fall through. */
3766 case 8:
3767 if (t->operand_types[op].bitfield.xmmword)
3768 return 16;
3769 if (t->operand_types[op].bitfield.ymmword)
3770 return 32;
3771 if (t->operand_types[op].bitfield.zmmword)
3772 return 64;
3773 /* Fall through. */
3774 default:
3775 abort ();
3776 }
3777
3778 gas_assert (op + 1 < t->operands);
3779
3780 if (t->operand_types[op + 1].bitfield.xmmword
3781 + t->operand_types[op + 1].bitfield.ymmword
3782 + t->operand_types[op + 1].bitfield.zmmword > 1)
3783 {
3784 types = &i.types[op + 1];
3785 diag = false;
3786 }
3787 else /* Ambiguous - guess with a preference to non-AVX512VL forms. */
3788 types = &t->operand_types[op];
3789
3790 if (types->bitfield.zmmword)
3791 bytes = 64;
3792 else if (types->bitfield.ymmword)
3793 bytes = 32;
3794 else
3795 bytes = 16;
3796
3797 if (diag)
3798 as_warn (_("ambiguous broadcast for `%s', using %u-bit form"),
3799 t->name, bytes * 8);
3800
3801 return bytes;
3802}
3803
43234a1e
L
3804/* Build the EVEX prefix. */
3805
3806static void
3807build_evex_prefix (void)
3808{
35648716 3809 unsigned int register_specifier, w;
43234a1e
L
3810 rex_byte vrex_used = 0;
3811
3812 /* Check register specifier. */
3813 if (i.vex.register_specifier)
3814 {
3815 gas_assert ((i.vrex & REX_X) == 0);
3816
3817 register_specifier = i.vex.register_specifier->reg_num;
3818 if ((i.vex.register_specifier->reg_flags & RegRex))
3819 register_specifier += 8;
3820 /* The upper 16 registers are encoded in the fourth byte of the
3821 EVEX prefix. */
3822 if (!(i.vex.register_specifier->reg_flags & RegVRex))
3823 i.vex.bytes[3] = 0x8;
3824 register_specifier = ~register_specifier & 0xf;
3825 }
3826 else
3827 {
3828 register_specifier = 0xf;
3829
3830 /* Encode upper 16 vector index register in the fourth byte of
3831 the EVEX prefix. */
3832 if (!(i.vrex & REX_X))
3833 i.vex.bytes[3] = 0x8;
3834 else
3835 vrex_used |= REX_X;
3836 }
3837
43234a1e
L
3838 /* 4 byte EVEX prefix. */
3839 i.vex.length = 4;
3840 i.vex.bytes[0] = 0x62;
3841
43234a1e
L
3842 /* The high 3 bits of the second EVEX byte are 1's compliment of RXB
3843 bits from REX. */
441f6aca 3844 gas_assert (i.tm.opcode_modifier.opcodespace >= SPACE_0F);
0cc78721 3845 gas_assert (i.tm.opcode_modifier.opcodespace <= SPACE_EVEXMAP6);
441f6aca 3846 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | i.tm.opcode_modifier.opcodespace;
43234a1e
L
3847
3848 /* The fifth bit of the second EVEX byte is 1's compliment of the
3849 REX_R bit in VREX. */
3850 if (!(i.vrex & REX_R))
3851 i.vex.bytes[1] |= 0x10;
3852 else
3853 vrex_used |= REX_R;
3854
3855 if ((i.reg_operands + i.imm_operands) == i.operands)
3856 {
3857 /* When all operands are registers, the REX_X bit in REX is not
3858 used. We reuse it to encode the upper 16 registers, which is
3859 indicated by the REX_B bit in VREX. The REX_X bit is encoded
3860 as 1's compliment. */
3861 if ((i.vrex & REX_B))
3862 {
3863 vrex_used |= REX_B;
3864 i.vex.bytes[1] &= ~0x40;
3865 }
3866 }
3867
3868 /* EVEX instructions shouldn't need the REX prefix. */
3869 i.vrex &= ~vrex_used;
3870 gas_assert (i.vrex == 0);
3871
6865c043
L
3872 /* Check the REX.W bit and VEXW. */
3873 if (i.tm.opcode_modifier.vexw == VEXWIG)
3874 w = (evexwig == evexw1 || (i.rex & REX_W)) ? 1 : 0;
3875 else if (i.tm.opcode_modifier.vexw)
3876 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3877 else
931d03b7 3878 w = (flag_code == CODE_64BIT ? i.rex & REX_W : evexwig == evexw1) ? 1 : 0;
43234a1e 3879
43234a1e 3880 /* The third byte of the EVEX prefix. */
35648716
JB
3881 i.vex.bytes[2] = ((w << 7)
3882 | (register_specifier << 3)
3883 | 4 /* Encode the U bit. */
3884 | i.tm.opcode_modifier.opcodeprefix);
43234a1e
L
3885
3886 /* The fourth byte of the EVEX prefix. */
3887 /* The zeroing-masking bit. */
6225c532 3888 if (i.mask.reg && i.mask.zeroing)
43234a1e
L
3889 i.vex.bytes[3] |= 0x80;
3890
3891 /* Don't always set the broadcast bit if there is no RC. */
ca5312a2 3892 if (i.rounding.type == rc_none)
43234a1e
L
3893 {
3894 /* Encode the vector length. */
3895 unsigned int vec_length;
3896
e771e7c9
JB
3897 if (!i.tm.opcode_modifier.evex
3898 || i.tm.opcode_modifier.evex == EVEXDYN)
3899 {
56522fc5 3900 unsigned int op;
e771e7c9 3901
c7213af9
L
3902 /* Determine vector length from the last multi-length vector
3903 operand. */
56522fc5 3904 for (op = i.operands; op--;)
e771e7c9
JB
3905 if (i.tm.operand_types[op].bitfield.xmmword
3906 + i.tm.operand_types[op].bitfield.ymmword
3907 + i.tm.operand_types[op].bitfield.zmmword > 1)
3908 {
3909 if (i.types[op].bitfield.zmmword)
c7213af9
L
3910 {
3911 i.tm.opcode_modifier.evex = EVEX512;
3912 break;
3913 }
e771e7c9 3914 else if (i.types[op].bitfield.ymmword)
c7213af9
L
3915 {
3916 i.tm.opcode_modifier.evex = EVEX256;
3917 break;
3918 }
e771e7c9 3919 else if (i.types[op].bitfield.xmmword)
c7213af9
L
3920 {
3921 i.tm.opcode_modifier.evex = EVEX128;
3922 break;
3923 }
a5748e0d 3924 else if (i.broadcast.bytes && op == i.broadcast.operand)
625cbd7a 3925 {
a5748e0d 3926 switch (get_broadcast_bytes (&i.tm, true))
625cbd7a
JB
3927 {
3928 case 64:
3929 i.tm.opcode_modifier.evex = EVEX512;
3930 break;
3931 case 32:
3932 i.tm.opcode_modifier.evex = EVEX256;
3933 break;
3934 case 16:
3935 i.tm.opcode_modifier.evex = EVEX128;
3936 break;
3937 default:
c7213af9 3938 abort ();
625cbd7a 3939 }
c7213af9 3940 break;
625cbd7a 3941 }
e771e7c9 3942 }
c7213af9 3943
56522fc5 3944 if (op >= MAX_OPERANDS)
c7213af9 3945 abort ();
e771e7c9
JB
3946 }
3947
43234a1e
L
3948 switch (i.tm.opcode_modifier.evex)
3949 {
3950 case EVEXLIG: /* LL' is ignored */
3951 vec_length = evexlig << 5;
3952 break;
3953 case EVEX128:
3954 vec_length = 0 << 5;
3955 break;
3956 case EVEX256:
3957 vec_length = 1 << 5;
3958 break;
3959 case EVEX512:
3960 vec_length = 2 << 5;
3961 break;
3962 default:
3963 abort ();
3964 break;
3965 }
3966 i.vex.bytes[3] |= vec_length;
3967 /* Encode the broadcast bit. */
a5748e0d 3968 if (i.broadcast.bytes)
43234a1e
L
3969 i.vex.bytes[3] |= 0x10;
3970 }
ca5312a2
JB
3971 else if (i.rounding.type != saeonly)
3972 i.vex.bytes[3] |= 0x10 | (i.rounding.type << 5);
43234a1e 3973 else
ca5312a2 3974 i.vex.bytes[3] |= 0x10 | (evexrcig << 5);
43234a1e 3975
6225c532
JB
3976 if (i.mask.reg)
3977 i.vex.bytes[3] |= i.mask.reg->reg_num;
43234a1e
L
3978}
3979
65da13b5
L
3980static void
3981process_immext (void)
3982{
3983 expressionS *exp;
3984
c0f3af97 3985 /* These AMD 3DNow! and SSE2 instructions have an opcode suffix
65da13b5
L
3986 which is coded in the same place as an 8-bit immediate field
3987 would be. Here we fake an 8-bit immediate operand from the
3988 opcode suffix stored in tm.extension_opcode.
3989
c1e679ec 3990 AVX instructions also use this encoding, for some of
c0f3af97 3991 3 argument instructions. */
65da13b5 3992
43234a1e 3993 gas_assert (i.imm_operands <= 1
7ab9ffdd 3994 && (i.operands <= 2
7a8655d2 3995 || (is_any_vex_encoding (&i.tm)
7ab9ffdd 3996 && i.operands <= 4)));
65da13b5
L
3997
3998 exp = &im_expressions[i.imm_operands++];
3999 i.op[i.operands].imms = exp;
be1643ff 4000 i.types[i.operands].bitfield.imm8 = 1;
65da13b5
L
4001 i.operands++;
4002 exp->X_op = O_constant;
4003 exp->X_add_number = i.tm.extension_opcode;
4004 i.tm.extension_opcode = None;
4005}
4006
42164a71
L
4007
4008static int
4009check_hle (void)
4010{
742732c7 4011 switch (i.tm.opcode_modifier.prefixok)
42164a71
L
4012 {
4013 default:
4014 abort ();
742732c7
JB
4015 case PrefixLock:
4016 case PrefixNone:
4017 case PrefixNoTrack:
4018 case PrefixRep:
165de32a
L
4019 as_bad (_("invalid instruction `%s' after `%s'"),
4020 i.tm.name, i.hle_prefix);
42164a71 4021 return 0;
742732c7 4022 case PrefixHLELock:
42164a71
L
4023 if (i.prefix[LOCK_PREFIX])
4024 return 1;
165de32a 4025 as_bad (_("missing `lock' with `%s'"), i.hle_prefix);
42164a71 4026 return 0;
742732c7 4027 case PrefixHLEAny:
42164a71 4028 return 1;
742732c7 4029 case PrefixHLERelease:
42164a71
L
4030 if (i.prefix[HLE_PREFIX] != XRELEASE_PREFIX_OPCODE)
4031 {
4032 as_bad (_("instruction `%s' after `xacquire' not allowed"),
4033 i.tm.name);
4034 return 0;
4035 }
8dc0818e 4036 if (i.mem_operands == 0 || !(i.flags[i.operands - 1] & Operand_Mem))
42164a71
L
4037 {
4038 as_bad (_("memory destination needed for instruction `%s'"
4039 " after `xrelease'"), i.tm.name);
4040 return 0;
4041 }
4042 return 1;
4043 }
4044}
4045
c8480b58
L
4046/* Encode aligned vector move as unaligned vector move. */
4047
4048static void
4049encode_with_unaligned_vector_move (void)
4050{
4051 switch (i.tm.base_opcode)
4052 {
b3a9fe6f
L
4053 case 0x28: /* Load instructions. */
4054 case 0x29: /* Store instructions. */
c8480b58
L
4055 /* movaps/movapd/vmovaps/vmovapd. */
4056 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4057 && i.tm.opcode_modifier.opcodeprefix <= PREFIX_0X66)
b3a9fe6f 4058 i.tm.base_opcode = 0x10 | (i.tm.base_opcode & 1);
c8480b58 4059 break;
b3a9fe6f
L
4060 case 0x6f: /* Load instructions. */
4061 case 0x7f: /* Store instructions. */
c8480b58
L
4062 /* movdqa/vmovdqa/vmovdqa64/vmovdqa32. */
4063 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4064 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0X66)
4065 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
4066 break;
4067 default:
4068 break;
4069 }
4070}
4071
b6f8c7c4
L
4072/* Try the shortest encoding by shortening operand size. */
4073
4074static void
4075optimize_encoding (void)
4076{
a0a1771e 4077 unsigned int j;
b6f8c7c4 4078
fe134c65
JB
4079 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
4080 && i.tm.base_opcode == 0x8d)
4081 {
4082 /* Optimize: -O:
4083 lea symbol, %rN -> mov $symbol, %rN
4084 lea (%rM), %rN -> mov %rM, %rN
4085 lea (,%rM,1), %rN -> mov %rM, %rN
4086
4087 and in 32-bit mode for 16-bit addressing
4088
4089 lea (%rM), %rN -> movzx %rM, %rN
4090
4091 and in 64-bit mode zap 32-bit addressing in favor of using a
4092 32-bit (or less) destination.
4093 */
4094 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
4095 {
4096 if (!i.op[1].regs->reg_type.bitfield.word)
4097 i.tm.opcode_modifier.size = SIZE32;
4098 i.prefix[ADDR_PREFIX] = 0;
4099 }
4100
4101 if (!i.index_reg && !i.base_reg)
4102 {
4103 /* Handle:
4104 lea symbol, %rN -> mov $symbol, %rN
4105 */
4106 if (flag_code == CODE_64BIT)
4107 {
4108 /* Don't transform a relocation to a 16-bit one. */
4109 if (i.op[0].disps
4110 && i.op[0].disps->X_op != O_constant
4111 && i.op[1].regs->reg_type.bitfield.word)
4112 return;
4113
4114 if (!i.op[1].regs->reg_type.bitfield.qword
4115 || i.tm.opcode_modifier.size == SIZE32)
4116 {
4117 i.tm.base_opcode = 0xb8;
4118 i.tm.opcode_modifier.modrm = 0;
4119 if (!i.op[1].regs->reg_type.bitfield.word)
4120 i.types[0].bitfield.imm32 = 1;
4121 else
4122 {
4123 i.tm.opcode_modifier.size = SIZE16;
4124 i.types[0].bitfield.imm16 = 1;
4125 }
4126 }
4127 else
4128 {
4129 /* Subject to further optimization below. */
4130 i.tm.base_opcode = 0xc7;
4131 i.tm.extension_opcode = 0;
4132 i.types[0].bitfield.imm32s = 1;
4133 i.types[0].bitfield.baseindex = 0;
4134 }
4135 }
4136 /* Outside of 64-bit mode address and operand sizes have to match if
4137 a relocation is involved, as otherwise we wouldn't (currently) or
4138 even couldn't express the relocation correctly. */
4139 else if (i.op[0].disps
4140 && i.op[0].disps->X_op != O_constant
4141 && ((!i.prefix[ADDR_PREFIX])
4142 != (flag_code == CODE_32BIT
4143 ? i.op[1].regs->reg_type.bitfield.dword
4144 : i.op[1].regs->reg_type.bitfield.word)))
4145 return;
7772f168
JB
4146 /* In 16-bit mode converting LEA with 16-bit addressing and a 32-bit
4147 destination is going to grow encoding size. */
4148 else if (flag_code == CODE_16BIT
4149 && (optimize <= 1 || optimize_for_space)
4150 && !i.prefix[ADDR_PREFIX]
4151 && i.op[1].regs->reg_type.bitfield.dword)
4152 return;
fe134c65
JB
4153 else
4154 {
4155 i.tm.base_opcode = 0xb8;
4156 i.tm.opcode_modifier.modrm = 0;
4157 if (i.op[1].regs->reg_type.bitfield.dword)
4158 i.types[0].bitfield.imm32 = 1;
4159 else
4160 i.types[0].bitfield.imm16 = 1;
4161
4162 if (i.op[0].disps
4163 && i.op[0].disps->X_op == O_constant
4164 && i.op[1].regs->reg_type.bitfield.dword
60cfa10c
L
4165 /* NB: Add () to !i.prefix[ADDR_PREFIX] to silence
4166 GCC 5. */
4167 && (!i.prefix[ADDR_PREFIX]) != (flag_code == CODE_32BIT))
fe134c65
JB
4168 i.op[0].disps->X_add_number &= 0xffff;
4169 }
4170
4171 i.tm.operand_types[0] = i.types[0];
4172 i.imm_operands = 1;
4173 if (!i.op[0].imms)
4174 {
4175 i.op[0].imms = &im_expressions[0];
4176 i.op[0].imms->X_op = O_absent;
4177 }
4178 }
4179 else if (i.op[0].disps
4180 && (i.op[0].disps->X_op != O_constant
4181 || i.op[0].disps->X_add_number))
4182 return;
4183 else
4184 {
4185 /* Handle:
4186 lea (%rM), %rN -> mov %rM, %rN
4187 lea (,%rM,1), %rN -> mov %rM, %rN
4188 lea (%rM), %rN -> movzx %rM, %rN
4189 */
4190 const reg_entry *addr_reg;
4191
4192 if (!i.index_reg && i.base_reg->reg_num != RegIP)
4193 addr_reg = i.base_reg;
4194 else if (!i.base_reg
4195 && i.index_reg->reg_num != RegIZ
4196 && !i.log2_scale_factor)
4197 addr_reg = i.index_reg;
4198 else
4199 return;
4200
4201 if (addr_reg->reg_type.bitfield.word
4202 && i.op[1].regs->reg_type.bitfield.dword)
4203 {
4204 if (flag_code != CODE_32BIT)
4205 return;
4206 i.tm.opcode_modifier.opcodespace = SPACE_0F;
4207 i.tm.base_opcode = 0xb7;
4208 }
4209 else
4210 i.tm.base_opcode = 0x8b;
4211
4212 if (addr_reg->reg_type.bitfield.dword
4213 && i.op[1].regs->reg_type.bitfield.qword)
4214 i.tm.opcode_modifier.size = SIZE32;
4215
4216 i.op[0].regs = addr_reg;
4217 i.reg_operands = 2;
4218 }
4219
4220 i.mem_operands = 0;
4221 i.disp_operands = 0;
4222 i.prefix[ADDR_PREFIX] = 0;
4223 i.prefix[SEG_PREFIX] = 0;
4224 i.seg[0] = NULL;
4225 }
4226
b6f8c7c4 4227 if (optimize_for_space
389d00a5 4228 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
b6f8c7c4
L
4229 && i.reg_operands == 1
4230 && i.imm_operands == 1
4231 && !i.types[1].bitfield.byte
4232 && i.op[0].imms->X_op == O_constant
4233 && fits_in_imm7 (i.op[0].imms->X_add_number)
72aea328 4234 && (i.tm.base_opcode == 0xa8
b6f8c7c4
L
4235 || (i.tm.base_opcode == 0xf6
4236 && i.tm.extension_opcode == 0x0)))
4237 {
4238 /* Optimize: -Os:
4239 test $imm7, %r64/%r32/%r16 -> test $imm7, %r8
4240 */
4241 unsigned int base_regnum = i.op[1].regs->reg_num;
4242 if (flag_code == CODE_64BIT || base_regnum < 4)
4243 {
4244 i.types[1].bitfield.byte = 1;
4245 /* Ignore the suffix. */
4246 i.suffix = 0;
7697afb6
JB
4247 /* Convert to byte registers. */
4248 if (i.types[1].bitfield.word)
4249 j = 16;
4250 else if (i.types[1].bitfield.dword)
4251 j = 32;
4252 else
4253 j = 48;
4254 if (!(i.op[1].regs->reg_flags & RegRex) && base_regnum < 4)
4255 j += 8;
4256 i.op[1].regs -= j;
b6f8c7c4
L
4257 }
4258 }
4259 else if (flag_code == CODE_64BIT
389d00a5 4260 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
d3d50934
L
4261 && ((i.types[1].bitfield.qword
4262 && i.reg_operands == 1
b6f8c7c4
L
4263 && i.imm_operands == 1
4264 && i.op[0].imms->X_op == O_constant
507916b8 4265 && ((i.tm.base_opcode == 0xb8
b6f8c7c4
L
4266 && i.tm.extension_opcode == None
4267 && fits_in_unsigned_long (i.op[0].imms->X_add_number))
4268 || (fits_in_imm31 (i.op[0].imms->X_add_number)
72aea328
JB
4269 && ((i.tm.base_opcode == 0x24
4270 || i.tm.base_opcode == 0xa8)
b6f8c7c4
L
4271 || (i.tm.base_opcode == 0x80
4272 && i.tm.extension_opcode == 0x4)
4273 || ((i.tm.base_opcode == 0xf6
507916b8 4274 || (i.tm.base_opcode | 1) == 0xc7)
b8364fa7
JB
4275 && i.tm.extension_opcode == 0x0)))
4276 || (fits_in_imm7 (i.op[0].imms->X_add_number)
4277 && i.tm.base_opcode == 0x83
4278 && i.tm.extension_opcode == 0x4)))
d3d50934
L
4279 || (i.types[0].bitfield.qword
4280 && ((i.reg_operands == 2
4281 && i.op[0].regs == i.op[1].regs
72aea328
JB
4282 && (i.tm.base_opcode == 0x30
4283 || i.tm.base_opcode == 0x28))
d3d50934
L
4284 || (i.reg_operands == 1
4285 && i.operands == 1
72aea328 4286 && i.tm.base_opcode == 0x30)))))
b6f8c7c4
L
4287 {
4288 /* Optimize: -O:
4289 andq $imm31, %r64 -> andl $imm31, %r32
b8364fa7 4290 andq $imm7, %r64 -> andl $imm7, %r32
b6f8c7c4
L
4291 testq $imm31, %r64 -> testl $imm31, %r32
4292 xorq %r64, %r64 -> xorl %r32, %r32
4293 subq %r64, %r64 -> subl %r32, %r32
4294 movq $imm31, %r64 -> movl $imm31, %r32
4295 movq $imm32, %r64 -> movl $imm32, %r32
4296 */
4297 i.tm.opcode_modifier.norex64 = 1;
507916b8 4298 if (i.tm.base_opcode == 0xb8 || (i.tm.base_opcode | 1) == 0xc7)
b6f8c7c4
L
4299 {
4300 /* Handle
4301 movq $imm31, %r64 -> movl $imm31, %r32
4302 movq $imm32, %r64 -> movl $imm32, %r32
4303 */
4304 i.tm.operand_types[0].bitfield.imm32 = 1;
4305 i.tm.operand_types[0].bitfield.imm32s = 0;
4306 i.tm.operand_types[0].bitfield.imm64 = 0;
4307 i.types[0].bitfield.imm32 = 1;
4308 i.types[0].bitfield.imm32s = 0;
4309 i.types[0].bitfield.imm64 = 0;
4310 i.types[1].bitfield.dword = 1;
4311 i.types[1].bitfield.qword = 0;
507916b8 4312 if ((i.tm.base_opcode | 1) == 0xc7)
b6f8c7c4
L
4313 {
4314 /* Handle
4315 movq $imm31, %r64 -> movl $imm31, %r32
4316 */
507916b8 4317 i.tm.base_opcode = 0xb8;
b6f8c7c4 4318 i.tm.extension_opcode = None;
507916b8 4319 i.tm.opcode_modifier.w = 0;
b6f8c7c4
L
4320 i.tm.opcode_modifier.modrm = 0;
4321 }
4322 }
4323 }
5641ec01
JB
4324 else if (optimize > 1
4325 && !optimize_for_space
389d00a5 4326 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
5641ec01
JB
4327 && i.reg_operands == 2
4328 && i.op[0].regs == i.op[1].regs
4329 && ((i.tm.base_opcode & ~(Opcode_D | 1)) == 0x8
4330 || (i.tm.base_opcode & ~(Opcode_D | 1)) == 0x20)
4331 && (flag_code != CODE_64BIT || !i.types[0].bitfield.dword))
4332 {
4333 /* Optimize: -O2:
4334 andb %rN, %rN -> testb %rN, %rN
4335 andw %rN, %rN -> testw %rN, %rN
4336 andq %rN, %rN -> testq %rN, %rN
4337 orb %rN, %rN -> testb %rN, %rN
4338 orw %rN, %rN -> testw %rN, %rN
4339 orq %rN, %rN -> testq %rN, %rN
4340
4341 and outside of 64-bit mode
4342
4343 andl %rN, %rN -> testl %rN, %rN
4344 orl %rN, %rN -> testl %rN, %rN
4345 */
4346 i.tm.base_opcode = 0x84 | (i.tm.base_opcode & 1);
4347 }
99112332 4348 else if (i.reg_operands == 3
b6f8c7c4
L
4349 && i.op[0].regs == i.op[1].regs
4350 && !i.types[2].bitfield.xmmword
4351 && (i.tm.opcode_modifier.vex
6225c532 4352 || ((!i.mask.reg || i.mask.zeroing)
e771e7c9 4353 && is_evex_encoding (&i.tm)
80c34c38 4354 && (i.vec_encoding != vex_encoding_evex
dd22218c 4355 || cpu_arch_isa_flags.bitfield.cpuavx512vl
80c34c38 4356 || i.tm.cpu_flags.bitfield.cpuavx512vl
7091c612 4357 || (i.tm.operand_types[2].bitfield.zmmword
dd22218c 4358 && i.types[2].bitfield.ymmword))))
5844ccaa
JB
4359 && i.tm.opcode_modifier.opcodespace == SPACE_0F
4360 && ((i.tm.base_opcode | 2) == 0x57
4361 || i.tm.base_opcode == 0xdf
4362 || i.tm.base_opcode == 0xef
4363 || (i.tm.base_opcode | 3) == 0xfb
4364 || i.tm.base_opcode == 0x42
4365 || i.tm.base_opcode == 0x47))
b6f8c7c4 4366 {
99112332 4367 /* Optimize: -O1:
8305403a
L
4368 VOP, one of vandnps, vandnpd, vxorps, vxorpd, vpsubb, vpsubd,
4369 vpsubq and vpsubw:
b6f8c7c4
L
4370 EVEX VOP %zmmM, %zmmM, %zmmN
4371 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4372 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4373 EVEX VOP %ymmM, %ymmM, %ymmN
4374 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4375 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4376 VEX VOP %ymmM, %ymmM, %ymmN
4377 -> VEX VOP %xmmM, %xmmM, %xmmN
4378 VOP, one of vpandn and vpxor:
4379 VEX VOP %ymmM, %ymmM, %ymmN
4380 -> VEX VOP %xmmM, %xmmM, %xmmN
4381 VOP, one of vpandnd and vpandnq:
4382 EVEX VOP %zmmM, %zmmM, %zmmN
4383 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4384 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4385 EVEX VOP %ymmM, %ymmM, %ymmN
4386 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4387 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4388 VOP, one of vpxord and vpxorq:
4389 EVEX VOP %zmmM, %zmmM, %zmmN
4390 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4391 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4392 EVEX VOP %ymmM, %ymmM, %ymmN
4393 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4394 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
1424ad86
JB
4395 VOP, one of kxord and kxorq:
4396 VEX VOP %kM, %kM, %kN
4397 -> VEX kxorw %kM, %kM, %kN
4398 VOP, one of kandnd and kandnq:
4399 VEX VOP %kM, %kM, %kN
4400 -> VEX kandnw %kM, %kM, %kN
b6f8c7c4 4401 */
e771e7c9 4402 if (is_evex_encoding (&i.tm))
b6f8c7c4 4403 {
7b1d7ca1 4404 if (i.vec_encoding != vex_encoding_evex)
b6f8c7c4
L
4405 {
4406 i.tm.opcode_modifier.vex = VEX128;
4407 i.tm.opcode_modifier.vexw = VEXW0;
4408 i.tm.opcode_modifier.evex = 0;
4409 }
7b1d7ca1 4410 else if (optimize > 1)
dd22218c
L
4411 i.tm.opcode_modifier.evex = EVEX128;
4412 else
4413 return;
b6f8c7c4 4414 }
f74a6307 4415 else if (i.tm.operand_types[0].bitfield.class == RegMask)
1424ad86 4416 {
35648716 4417 i.tm.opcode_modifier.opcodeprefix = PREFIX_NONE;
1424ad86
JB
4418 i.tm.opcode_modifier.vexw = VEXW0;
4419 }
b6f8c7c4
L
4420 else
4421 i.tm.opcode_modifier.vex = VEX128;
4422
4423 if (i.tm.opcode_modifier.vex)
4424 for (j = 0; j < 3; j++)
4425 {
4426 i.types[j].bitfield.xmmword = 1;
4427 i.types[j].bitfield.ymmword = 0;
4428 }
4429 }
392a5972 4430 else if (i.vec_encoding != vex_encoding_evex
97ed31ae 4431 && !i.types[0].bitfield.zmmword
392a5972 4432 && !i.types[1].bitfield.zmmword
6225c532 4433 && !i.mask.reg
a5748e0d 4434 && !i.broadcast.bytes
97ed31ae 4435 && is_evex_encoding (&i.tm)
35648716
JB
4436 && ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4437 || (i.tm.base_opcode & ~4) == 0xdb
4438 || (i.tm.base_opcode & ~4) == 0xeb)
97ed31ae
L
4439 && i.tm.extension_opcode == None)
4440 {
4441 /* Optimize: -O1:
4442 VOP, one of vmovdqa32, vmovdqa64, vmovdqu8, vmovdqu16,
4443 vmovdqu32 and vmovdqu64:
4444 EVEX VOP %xmmM, %xmmN
4445 -> VEX vmovdqa|vmovdqu %xmmM, %xmmN (M and N < 16)
4446 EVEX VOP %ymmM, %ymmN
4447 -> VEX vmovdqa|vmovdqu %ymmM, %ymmN (M and N < 16)
4448 EVEX VOP %xmmM, mem
4449 -> VEX vmovdqa|vmovdqu %xmmM, mem (M < 16)
4450 EVEX VOP %ymmM, mem
4451 -> VEX vmovdqa|vmovdqu %ymmM, mem (M < 16)
4452 EVEX VOP mem, %xmmN
4453 -> VEX mvmovdqa|vmovdquem, %xmmN (N < 16)
4454 EVEX VOP mem, %ymmN
4455 -> VEX vmovdqa|vmovdqu mem, %ymmN (N < 16)
a0a1771e
JB
4456 VOP, one of vpand, vpandn, vpor, vpxor:
4457 EVEX VOP{d,q} %xmmL, %xmmM, %xmmN
4458 -> VEX VOP %xmmL, %xmmM, %xmmN (L, M, and N < 16)
4459 EVEX VOP{d,q} %ymmL, %ymmM, %ymmN
4460 -> VEX VOP %ymmL, %ymmM, %ymmN (L, M, and N < 16)
4461 EVEX VOP{d,q} mem, %xmmM, %xmmN
4462 -> VEX VOP mem, %xmmM, %xmmN (M and N < 16)
4463 EVEX VOP{d,q} mem, %ymmM, %ymmN
4464 -> VEX VOP mem, %ymmM, %ymmN (M and N < 16)
97ed31ae 4465 */
a0a1771e 4466 for (j = 0; j < i.operands; j++)
392a5972
L
4467 if (operand_type_check (i.types[j], disp)
4468 && i.op[j].disps->X_op == O_constant)
4469 {
4470 /* Since the VEX prefix has 2 or 3 bytes, the EVEX prefix
4471 has 4 bytes, EVEX Disp8 has 1 byte and VEX Disp32 has 4
4472 bytes, we choose EVEX Disp8 over VEX Disp32. */
4473 int evex_disp8, vex_disp8;
4474 unsigned int memshift = i.memshift;
4475 offsetT n = i.op[j].disps->X_add_number;
4476
4477 evex_disp8 = fits_in_disp8 (n);
4478 i.memshift = 0;
4479 vex_disp8 = fits_in_disp8 (n);
4480 if (evex_disp8 != vex_disp8)
4481 {
4482 i.memshift = memshift;
4483 return;
4484 }
4485
4486 i.types[j].bitfield.disp8 = vex_disp8;
4487 break;
4488 }
35648716
JB
4489 if ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4490 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2)
4491 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
97ed31ae
L
4492 i.tm.opcode_modifier.vex
4493 = i.types[0].bitfield.ymmword ? VEX256 : VEX128;
4494 i.tm.opcode_modifier.vexw = VEXW0;
79dec6b7 4495 /* VPAND, VPOR, and VPXOR are commutative. */
35648716 4496 if (i.reg_operands == 3 && i.tm.base_opcode != 0xdf)
79dec6b7 4497 i.tm.opcode_modifier.commutative = 1;
97ed31ae
L
4498 i.tm.opcode_modifier.evex = 0;
4499 i.tm.opcode_modifier.masking = 0;
a0a1771e 4500 i.tm.opcode_modifier.broadcast = 0;
97ed31ae
L
4501 i.tm.opcode_modifier.disp8memshift = 0;
4502 i.memshift = 0;
a0a1771e
JB
4503 if (j < i.operands)
4504 i.types[j].bitfield.disp8
4505 = fits_in_disp8 (i.op[j].disps->X_add_number);
97ed31ae 4506 }
b6f8c7c4
L
4507}
4508
ae531041
L
4509/* Return non-zero for load instruction. */
4510
4511static int
4512load_insn_p (void)
4513{
4514 unsigned int dest;
4515 int any_vex_p = is_any_vex_encoding (&i.tm);
4516 unsigned int base_opcode = i.tm.base_opcode | 1;
4517
4518 if (!any_vex_p)
4519 {
ef07be45
CL
4520 /* Anysize insns: lea, invlpg, clflush, prefetch*, bndmk, bndcl, bndcu,
4521 bndcn, bndstx, bndldx, clflushopt, clwb, cldemote. */
a09f656b 4522 if (i.tm.opcode_modifier.anysize)
ae531041
L
4523 return 0;
4524
389d00a5
JB
4525 /* pop. */
4526 if (strcmp (i.tm.name, "pop") == 0)
4527 return 1;
4528 }
4529
4530 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE)
4531 {
4532 /* popf, popa. */
4533 if (i.tm.base_opcode == 0x9d
a09f656b 4534 || i.tm.base_opcode == 0x61)
ae531041
L
4535 return 1;
4536
4537 /* movs, cmps, lods, scas. */
4538 if ((i.tm.base_opcode | 0xb) == 0xaf)
4539 return 1;
4540
a09f656b 4541 /* outs, xlatb. */
4542 if (base_opcode == 0x6f
4543 || i.tm.base_opcode == 0xd7)
ae531041 4544 return 1;
a09f656b 4545 /* NB: For AMD-specific insns with implicit memory operands,
4546 they're intentionally not covered. */
ae531041
L
4547 }
4548
4549 /* No memory operand. */
4550 if (!i.mem_operands)
4551 return 0;
4552
4553 if (any_vex_p)
4554 {
4555 /* vldmxcsr. */
4556 if (i.tm.base_opcode == 0xae
4557 && i.tm.opcode_modifier.vex
441f6aca 4558 && i.tm.opcode_modifier.opcodespace == SPACE_0F
35648716 4559 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
ae531041
L
4560 && i.tm.extension_opcode == 2)
4561 return 1;
4562 }
389d00a5 4563 else if (i.tm.opcode_modifier.opcodespace == SPACE_BASE)
ae531041
L
4564 {
4565 /* test, not, neg, mul, imul, div, idiv. */
4566 if ((i.tm.base_opcode == 0xf6 || i.tm.base_opcode == 0xf7)
4567 && i.tm.extension_opcode != 1)
4568 return 1;
4569
4570 /* inc, dec. */
4571 if (base_opcode == 0xff && i.tm.extension_opcode <= 1)
4572 return 1;
4573
4574 /* add, or, adc, sbb, and, sub, xor, cmp. */
4575 if (i.tm.base_opcode >= 0x80 && i.tm.base_opcode <= 0x83)
4576 return 1;
4577
ae531041
L
4578 /* rol, ror, rcl, rcr, shl/sal, shr, sar. */
4579 if ((base_opcode == 0xc1
4580 || (i.tm.base_opcode >= 0xd0 && i.tm.base_opcode <= 0xd3))
4581 && i.tm.extension_opcode != 6)
4582 return 1;
4583
ae531041 4584 /* Check for x87 instructions. */
389d00a5 4585 if (base_opcode >= 0xd8 && base_opcode <= 0xdf)
ae531041
L
4586 {
4587 /* Skip fst, fstp, fstenv, fstcw. */
4588 if (i.tm.base_opcode == 0xd9
4589 && (i.tm.extension_opcode == 2
4590 || i.tm.extension_opcode == 3
4591 || i.tm.extension_opcode == 6
4592 || i.tm.extension_opcode == 7))
4593 return 0;
4594
4595 /* Skip fisttp, fist, fistp, fstp. */
4596 if (i.tm.base_opcode == 0xdb
4597 && (i.tm.extension_opcode == 1
4598 || i.tm.extension_opcode == 2
4599 || i.tm.extension_opcode == 3
4600 || i.tm.extension_opcode == 7))
4601 return 0;
4602
4603 /* Skip fisttp, fst, fstp, fsave, fstsw. */
4604 if (i.tm.base_opcode == 0xdd
4605 && (i.tm.extension_opcode == 1
4606 || i.tm.extension_opcode == 2
4607 || i.tm.extension_opcode == 3
4608 || i.tm.extension_opcode == 6
4609 || i.tm.extension_opcode == 7))
4610 return 0;
4611
4612 /* Skip fisttp, fist, fistp, fbstp, fistp. */
4613 if (i.tm.base_opcode == 0xdf
4614 && (i.tm.extension_opcode == 1
4615 || i.tm.extension_opcode == 2
4616 || i.tm.extension_opcode == 3
4617 || i.tm.extension_opcode == 6
4618 || i.tm.extension_opcode == 7))
4619 return 0;
4620
4621 return 1;
4622 }
4623 }
389d00a5
JB
4624 else if (i.tm.opcode_modifier.opcodespace == SPACE_0F)
4625 {
4626 /* bt, bts, btr, btc. */
4627 if (i.tm.base_opcode == 0xba
4628 && (i.tm.extension_opcode >= 4 && i.tm.extension_opcode <= 7))
4629 return 1;
4630
4631 /* cmpxchg8b, cmpxchg16b, xrstors, vmptrld. */
4632 if (i.tm.base_opcode == 0xc7
4633 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
4634 && (i.tm.extension_opcode == 1 || i.tm.extension_opcode == 3
4635 || i.tm.extension_opcode == 6))
4636 return 1;
4637
4638 /* fxrstor, ldmxcsr, xrstor. */
4639 if (i.tm.base_opcode == 0xae
4640 && (i.tm.extension_opcode == 1
4641 || i.tm.extension_opcode == 2
4642 || i.tm.extension_opcode == 5))
4643 return 1;
4644
4645 /* lgdt, lidt, lmsw. */
4646 if (i.tm.base_opcode == 0x01
4647 && (i.tm.extension_opcode == 2
4648 || i.tm.extension_opcode == 3
4649 || i.tm.extension_opcode == 6))
4650 return 1;
4651 }
ae531041
L
4652
4653 dest = i.operands - 1;
4654
4655 /* Check fake imm8 operand and 3 source operands. */
4656 if ((i.tm.opcode_modifier.immext
4657 || i.tm.opcode_modifier.vexsources == VEX3SOURCES)
4658 && i.types[dest].bitfield.imm8)
4659 dest--;
4660
389d00a5
JB
4661 /* add, or, adc, sbb, and, sub, xor, cmp, test, xchg. */
4662 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
ae531041
L
4663 && (base_opcode == 0x1
4664 || base_opcode == 0x9
4665 || base_opcode == 0x11
4666 || base_opcode == 0x19
4667 || base_opcode == 0x21
4668 || base_opcode == 0x29
4669 || base_opcode == 0x31
4670 || base_opcode == 0x39
389d00a5
JB
4671 || (base_opcode | 2) == 0x87))
4672 return 1;
4673
4674 /* xadd. */
4675 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4676 && base_opcode == 0xc1)
ae531041
L
4677 return 1;
4678
4679 /* Check for load instruction. */
4680 return (i.types[dest].bitfield.class != ClassNone
4681 || i.types[dest].bitfield.instance == Accum);
4682}
4683
4684/* Output lfence, 0xfaee8, after instruction. */
4685
4686static void
4687insert_lfence_after (void)
4688{
4689 if (lfence_after_load && load_insn_p ())
4690 {
a09f656b 4691 /* There are also two REP string instructions that require
4692 special treatment. Specifically, the compare string (CMPS)
4693 and scan string (SCAS) instructions set EFLAGS in a manner
4694 that depends on the data being compared/scanned. When used
4695 with a REP prefix, the number of iterations may therefore
4696 vary depending on this data. If the data is a program secret
4697 chosen by the adversary using an LVI method,
4698 then this data-dependent behavior may leak some aspect
4699 of the secret. */
4700 if (((i.tm.base_opcode | 0x1) == 0xa7
4701 || (i.tm.base_opcode | 0x1) == 0xaf)
4702 && i.prefix[REP_PREFIX])
4703 {
4704 as_warn (_("`%s` changes flags which would affect control flow behavior"),
4705 i.tm.name);
4706 }
ae531041
L
4707 char *p = frag_more (3);
4708 *p++ = 0xf;
4709 *p++ = 0xae;
4710 *p = 0xe8;
4711 }
4712}
4713
4714/* Output lfence, 0xfaee8, before instruction. */
4715
4716static void
4717insert_lfence_before (void)
4718{
4719 char *p;
4720
389d00a5 4721 if (i.tm.opcode_modifier.opcodespace != SPACE_BASE)
ae531041
L
4722 return;
4723
4724 if (i.tm.base_opcode == 0xff
4725 && (i.tm.extension_opcode == 2 || i.tm.extension_opcode == 4))
4726 {
4727 /* Insert lfence before indirect branch if needed. */
4728
4729 if (lfence_before_indirect_branch == lfence_branch_none)
4730 return;
4731
4732 if (i.operands != 1)
4733 abort ();
4734
4735 if (i.reg_operands == 1)
4736 {
4737 /* Indirect branch via register. Don't insert lfence with
4738 -mlfence-after-load=yes. */
4739 if (lfence_after_load
4740 || lfence_before_indirect_branch == lfence_branch_memory)
4741 return;
4742 }
4743 else if (i.mem_operands == 1
4744 && lfence_before_indirect_branch != lfence_branch_register)
4745 {
4746 as_warn (_("indirect `%s` with memory operand should be avoided"),
4747 i.tm.name);
4748 return;
4749 }
4750 else
4751 return;
4752
4753 if (last_insn.kind != last_insn_other
4754 && last_insn.seg == now_seg)
4755 {
4756 as_warn_where (last_insn.file, last_insn.line,
4757 _("`%s` skips -mlfence-before-indirect-branch on `%s`"),
4758 last_insn.name, i.tm.name);
4759 return;
4760 }
4761
4762 p = frag_more (3);
4763 *p++ = 0xf;
4764 *p++ = 0xae;
4765 *p = 0xe8;
4766 return;
4767 }
4768
503648e4 4769 /* Output or/not/shl and lfence before near ret. */
ae531041
L
4770 if (lfence_before_ret != lfence_before_ret_none
4771 && (i.tm.base_opcode == 0xc2
503648e4 4772 || i.tm.base_opcode == 0xc3))
ae531041
L
4773 {
4774 if (last_insn.kind != last_insn_other
4775 && last_insn.seg == now_seg)
4776 {
4777 as_warn_where (last_insn.file, last_insn.line,
4778 _("`%s` skips -mlfence-before-ret on `%s`"),
4779 last_insn.name, i.tm.name);
4780 return;
4781 }
a09f656b 4782
a09f656b 4783 /* Near ret ingore operand size override under CPU64. */
503648e4 4784 char prefix = flag_code == CODE_64BIT
4785 ? 0x48
4786 : i.prefix[DATA_PREFIX] ? 0x66 : 0x0;
a09f656b 4787
4788 if (lfence_before_ret == lfence_before_ret_not)
4789 {
4790 /* not: 0xf71424, may add prefix
4791 for operand size override or 64-bit code. */
4792 p = frag_more ((prefix ? 2 : 0) + 6 + 3);
4793 if (prefix)
4794 *p++ = prefix;
ae531041
L
4795 *p++ = 0xf7;
4796 *p++ = 0x14;
4797 *p++ = 0x24;
a09f656b 4798 if (prefix)
4799 *p++ = prefix;
ae531041
L
4800 *p++ = 0xf7;
4801 *p++ = 0x14;
4802 *p++ = 0x24;
4803 }
a09f656b 4804 else
4805 {
4806 p = frag_more ((prefix ? 1 : 0) + 4 + 3);
4807 if (prefix)
4808 *p++ = prefix;
4809 if (lfence_before_ret == lfence_before_ret_or)
4810 {
4811 /* or: 0x830c2400, may add prefix
4812 for operand size override or 64-bit code. */
4813 *p++ = 0x83;
4814 *p++ = 0x0c;
4815 }
4816 else
4817 {
4818 /* shl: 0xc1242400, may add prefix
4819 for operand size override or 64-bit code. */
4820 *p++ = 0xc1;
4821 *p++ = 0x24;
4822 }
4823
4824 *p++ = 0x24;
4825 *p++ = 0x0;
4826 }
4827
ae531041
L
4828 *p++ = 0xf;
4829 *p++ = 0xae;
4830 *p = 0xe8;
4831 }
4832}
4833
252b5132
RH
4834/* This is the guts of the machine-dependent assembler. LINE points to a
4835 machine dependent instruction. This function is supposed to emit
4836 the frags/bytes it assembles to. */
4837
4838void
65da13b5 4839md_assemble (char *line)
252b5132 4840{
40fb9820 4841 unsigned int j;
83b16ac6 4842 char mnemonic[MAX_MNEM_SIZE], mnem_suffix;
d3ce72d0 4843 const insn_template *t;
252b5132 4844
47926f60 4845 /* Initialize globals. */
252b5132 4846 memset (&i, '\0', sizeof (i));
ca5312a2 4847 i.rounding.type = rc_none;
252b5132 4848 for (j = 0; j < MAX_OPERANDS; j++)
1ae12ab7 4849 i.reloc[j] = NO_RELOC;
252b5132
RH
4850 memset (disp_expressions, '\0', sizeof (disp_expressions));
4851 memset (im_expressions, '\0', sizeof (im_expressions));
ce8a8b2f 4852 save_stack_p = save_stack;
252b5132
RH
4853
4854 /* First parse an instruction mnemonic & call i386_operand for the operands.
4855 We assume that the scrubber has arranged it so that line[0] is the valid
47926f60 4856 start of a (possibly prefixed) mnemonic. */
252b5132 4857
29b0f896
AM
4858 line = parse_insn (line, mnemonic);
4859 if (line == NULL)
4860 return;
83b16ac6 4861 mnem_suffix = i.suffix;
252b5132 4862
29b0f896 4863 line = parse_operands (line, mnemonic);
ee86248c 4864 this_operand = -1;
8325cc63
JB
4865 xfree (i.memop1_string);
4866 i.memop1_string = NULL;
29b0f896
AM
4867 if (line == NULL)
4868 return;
252b5132 4869
29b0f896
AM
4870 /* Now we've parsed the mnemonic into a set of templates, and have the
4871 operands at hand. */
4872
b630c145 4873 /* All Intel opcodes have reversed operands except for "bound", "enter",
c0e54661
JB
4874 "invlpg*", "monitor*", "mwait*", "tpause", "umwait", "pvalidate",
4875 "rmpadjust", and "rmpupdate". We also don't reverse intersegment "jmp"
4876 and "call" instructions with 2 immediate operands so that the immediate
4877 segment precedes the offset consistently in Intel and AT&T modes. */
4d456e3d
L
4878 if (intel_syntax
4879 && i.operands > 1
29b0f896 4880 && (strcmp (mnemonic, "bound") != 0)
c0e54661 4881 && (strncmp (mnemonic, "invlpg", 6) != 0)
d34049e8
ML
4882 && !startswith (mnemonic, "monitor")
4883 && !startswith (mnemonic, "mwait")
c0e54661 4884 && (strcmp (mnemonic, "pvalidate") != 0)
d34049e8 4885 && !startswith (mnemonic, "rmp")
b630c145
JB
4886 && (strcmp (mnemonic, "tpause") != 0)
4887 && (strcmp (mnemonic, "umwait") != 0)
47c0279b
JB
4888 && !(i.operands == 2
4889 && operand_type_check (i.types[0], imm)
40fb9820 4890 && operand_type_check (i.types[1], imm)))
29b0f896
AM
4891 swap_operands ();
4892
ec56d5c0
JB
4893 /* The order of the immediates should be reversed
4894 for 2 immediates extrq and insertq instructions */
4895 if (i.imm_operands == 2
4896 && (strcmp (mnemonic, "extrq") == 0
4897 || strcmp (mnemonic, "insertq") == 0))
4898 swap_2_operands (0, 1);
4899
29b0f896
AM
4900 if (i.imm_operands)
4901 optimize_imm ();
4902
9386188e
JB
4903 if (i.disp_operands && !want_disp32 (current_templates->start)
4904 && (!current_templates->start->opcode_modifier.jump
4905 || i.jumpabsolute || i.types[0].bitfield.baseindex))
cce08655
JB
4906 {
4907 for (j = 0; j < i.operands; ++j)
4908 {
4909 const expressionS *exp = i.op[j].disps;
4910
4911 if (!operand_type_check (i.types[j], disp))
4912 continue;
4913
4914 if (exp->X_op != O_constant)
4915 continue;
4916
4917 /* Since displacement is signed extended to 64bit, don't allow
a775efc8 4918 disp32 if it is out of range. */
cce08655
JB
4919 if (fits_in_signed_long (exp->X_add_number))
4920 continue;
4921
a775efc8 4922 i.types[j].bitfield.disp32 = 0;
cce08655
JB
4923 if (i.types[j].bitfield.baseindex)
4924 {
f493c217
AM
4925 as_bad (_("0x%" PRIx64 " out of range of signed 32bit displacement"),
4926 (uint64_t) exp->X_add_number);
cce08655
JB
4927 return;
4928 }
4929 }
4930 }
4931
b300c311
L
4932 /* Don't optimize displacement for movabs since it only takes 64bit
4933 displacement. */
4934 if (i.disp_operands
1a42a9fe 4935 && i.disp_encoding <= disp_encoding_8bit
862be3fb
L
4936 && (flag_code != CODE_64BIT
4937 || strcmp (mnemonic, "movabs") != 0))
4938 optimize_disp ();
29b0f896
AM
4939
4940 /* Next, we find a template that matches the given insn,
4941 making sure the overlap of the given operands types is consistent
4942 with the template operand types. */
252b5132 4943
83b16ac6 4944 if (!(t = match_template (mnem_suffix)))
29b0f896 4945 return;
252b5132 4946
7bab8ab5 4947 if (sse_check != check_none
ffb86450
JB
4948 /* The opcode space check isn't strictly needed; it's there only to
4949 bypass the logic below when easily possible. */
4950 && t->opcode_modifier.opcodespace >= SPACE_0F
4951 && t->opcode_modifier.opcodespace <= SPACE_0F3A
4952 && !i.tm.cpu_flags.bitfield.cpusse4a
4953 && !is_any_vex_encoding (t))
daf50ae7 4954 {
ffb86450
JB
4955 bool simd = false;
4956
4957 for (j = 0; j < t->operands; ++j)
4958 {
4959 if (t->operand_types[j].bitfield.class == RegMMX)
4960 break;
4961 if (t->operand_types[j].bitfield.class == RegSIMD)
4962 simd = true;
4963 }
4964
4965 if (j >= t->operands && simd)
4966 (sse_check == check_warning
4967 ? as_warn
4968 : as_bad) (_("SSE instruction `%s' is used"), i.tm.name);
daf50ae7
L
4969 }
4970
40fb9820 4971 if (i.tm.opcode_modifier.fwait)
29b0f896
AM
4972 if (!add_prefix (FWAIT_OPCODE))
4973 return;
252b5132 4974
d5de92cf 4975 /* Check if REP prefix is OK. */
742732c7 4976 if (i.rep_prefix && i.tm.opcode_modifier.prefixok != PrefixRep)
d5de92cf
L
4977 {
4978 as_bad (_("invalid instruction `%s' after `%s'"),
4979 i.tm.name, i.rep_prefix);
4980 return;
4981 }
4982
c1ba0266
L
4983 /* Check for lock without a lockable instruction. Destination operand
4984 must be memory unless it is xchg (0x86). */
c32fa91d 4985 if (i.prefix[LOCK_PREFIX]
742732c7 4986 && (i.tm.opcode_modifier.prefixok < PrefixLock
c1ba0266
L
4987 || i.mem_operands == 0
4988 || (i.tm.base_opcode != 0x86
8dc0818e 4989 && !(i.flags[i.operands - 1] & Operand_Mem))))
c32fa91d
L
4990 {
4991 as_bad (_("expecting lockable instruction after `lock'"));
4992 return;
4993 }
4994
40d231b4
JB
4995 /* Check for data size prefix on VEX/XOP/EVEX encoded and SIMD insns. */
4996 if (i.prefix[DATA_PREFIX]
4997 && (is_any_vex_encoding (&i.tm)
4998 || i.tm.operand_types[i.imm_operands].bitfield.class >= RegMMX
4999 || i.tm.operand_types[i.imm_operands + 1].bitfield.class >= RegMMX))
7a8655d2
JB
5000 {
5001 as_bad (_("data size prefix invalid with `%s'"), i.tm.name);
5002 return;
5003 }
5004
42164a71 5005 /* Check if HLE prefix is OK. */
165de32a 5006 if (i.hle_prefix && !check_hle ())
42164a71
L
5007 return;
5008
7e8b059b
L
5009 /* Check BND prefix. */
5010 if (i.bnd_prefix && !i.tm.opcode_modifier.bndprefixok)
5011 as_bad (_("expecting valid branch instruction after `bnd'"));
5012
04ef582a 5013 /* Check NOTRACK prefix. */
742732c7 5014 if (i.notrack_prefix && i.tm.opcode_modifier.prefixok != PrefixNoTrack)
9fef80d6 5015 as_bad (_("expecting indirect branch instruction after `notrack'"));
04ef582a 5016
327e8c42
JB
5017 if (i.tm.cpu_flags.bitfield.cpumpx)
5018 {
5019 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
5020 as_bad (_("32-bit address isn't allowed in 64-bit MPX instructions."));
5021 else if (flag_code != CODE_16BIT
5022 ? i.prefix[ADDR_PREFIX]
5023 : i.mem_operands && !i.prefix[ADDR_PREFIX])
5024 as_bad (_("16-bit address isn't allowed in MPX instructions"));
5025 }
7e8b059b
L
5026
5027 /* Insert BND prefix. */
76d3a78a
JB
5028 if (add_bnd_prefix && i.tm.opcode_modifier.bndprefixok)
5029 {
5030 if (!i.prefix[BND_PREFIX])
5031 add_prefix (BND_PREFIX_OPCODE);
5032 else if (i.prefix[BND_PREFIX] != BND_PREFIX_OPCODE)
5033 {
5034 as_warn (_("replacing `rep'/`repe' prefix by `bnd'"));
5035 i.prefix[BND_PREFIX] = BND_PREFIX_OPCODE;
5036 }
5037 }
7e8b059b 5038
29b0f896 5039 /* Check string instruction segment overrides. */
51c8edf6 5040 if (i.tm.opcode_modifier.isstring >= IS_STRING_ES_OP0)
29b0f896 5041 {
51c8edf6 5042 gas_assert (i.mem_operands);
29b0f896 5043 if (!check_string ())
5dd0794d 5044 return;
fc0763e6 5045 i.disp_operands = 0;
29b0f896 5046 }
5dd0794d 5047
b6f8c7c4
L
5048 if (optimize && !i.no_optimize && i.tm.opcode_modifier.optimize)
5049 optimize_encoding ();
5050
c8480b58
L
5051 if (use_unaligned_vector_move)
5052 encode_with_unaligned_vector_move ();
5053
29b0f896
AM
5054 if (!process_suffix ())
5055 return;
e413e4e9 5056
ef07be45
CL
5057 /* Check if IP-relative addressing requirements can be satisfied. */
5058 if (i.tm.cpu_flags.bitfield.cpuprefetchi
5059 && !(i.base_reg && i.base_reg->reg_num == RegIP))
f2462532 5060 as_warn (_("'%s' only supports RIP-relative address"), i.tm.name);
ef07be45 5061
921eafea 5062 /* Update operand types and check extended states. */
bc0844ae 5063 for (j = 0; j < i.operands; j++)
921eafea
L
5064 {
5065 i.types[j] = operand_type_and (i.types[j], i.tm.operand_types[j]);
3d70986f 5066 switch (i.tm.operand_types[j].bitfield.class)
921eafea
L
5067 {
5068 default:
5069 break;
5070 case RegMMX:
5071 i.xstate |= xstate_mmx;
5072 break;
5073 case RegMask:
32930e4e 5074 i.xstate |= xstate_mask;
921eafea
L
5075 break;
5076 case RegSIMD:
3d70986f 5077 if (i.tm.operand_types[j].bitfield.tmmword)
921eafea 5078 i.xstate |= xstate_tmm;
3d70986f 5079 else if (i.tm.operand_types[j].bitfield.zmmword)
921eafea 5080 i.xstate |= xstate_zmm;
3d70986f 5081 else if (i.tm.operand_types[j].bitfield.ymmword)
921eafea 5082 i.xstate |= xstate_ymm;
3d70986f 5083 else if (i.tm.operand_types[j].bitfield.xmmword)
921eafea
L
5084 i.xstate |= xstate_xmm;
5085 break;
5086 }
5087 }
bc0844ae 5088
29b0f896
AM
5089 /* Make still unresolved immediate matches conform to size of immediate
5090 given in i.suffix. */
5091 if (!finalize_imm ())
5092 return;
252b5132 5093
40fb9820 5094 if (i.types[0].bitfield.imm1)
29b0f896 5095 i.imm_operands = 0; /* kludge for shift insns. */
252b5132 5096
9afe6eb8
L
5097 /* We only need to check those implicit registers for instructions
5098 with 3 operands or less. */
5099 if (i.operands <= 3)
5100 for (j = 0; j < i.operands; j++)
75e5731b
JB
5101 if (i.types[j].bitfield.instance != InstanceNone
5102 && !i.types[j].bitfield.xmmword)
9afe6eb8 5103 i.reg_operands--;
40fb9820 5104
29b0f896
AM
5105 /* For insns with operands there are more diddles to do to the opcode. */
5106 if (i.operands)
5107 {
5108 if (!process_operands ())
5109 return;
5110 }
8c190ce0 5111 else if (!quiet_warnings && i.tm.opcode_modifier.ugh)
29b0f896
AM
5112 {
5113 /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc. */
5114 as_warn (_("translating to `%sp'"), i.tm.name);
5115 }
252b5132 5116
7a8655d2 5117 if (is_any_vex_encoding (&i.tm))
9e5e5283 5118 {
c1dc7af5 5119 if (!cpu_arch_flags.bitfield.cpui286)
9e5e5283 5120 {
c1dc7af5 5121 as_bad (_("instruction `%s' isn't supported outside of protected mode."),
9e5e5283
L
5122 i.tm.name);
5123 return;
5124 }
c0f3af97 5125
0b9404fd
JB
5126 /* Check for explicit REX prefix. */
5127 if (i.prefix[REX_PREFIX] || i.rex_encoding)
5128 {
5129 as_bad (_("REX prefix invalid with `%s'"), i.tm.name);
5130 return;
5131 }
5132
9e5e5283
L
5133 if (i.tm.opcode_modifier.vex)
5134 build_vex_prefix (t);
5135 else
5136 build_evex_prefix ();
0b9404fd
JB
5137
5138 /* The individual REX.RXBW bits got consumed. */
5139 i.rex &= REX_OPCODE;
9e5e5283 5140 }
43234a1e 5141
5dd85c99
SP
5142 /* Handle conversion of 'int $3' --> special int3 insn. XOP or FMA4
5143 instructions may define INT_OPCODE as well, so avoid this corner
5144 case for those instructions that use MODRM. */
389d00a5
JB
5145 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
5146 && i.tm.base_opcode == INT_OPCODE
a6461c02
SP
5147 && !i.tm.opcode_modifier.modrm
5148 && i.op[0].imms->X_add_number == 3)
29b0f896
AM
5149 {
5150 i.tm.base_opcode = INT3_OPCODE;
5151 i.imm_operands = 0;
5152 }
252b5132 5153
0cfa3eb3
JB
5154 if ((i.tm.opcode_modifier.jump == JUMP
5155 || i.tm.opcode_modifier.jump == JUMP_BYTE
5156 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896
AM
5157 && i.op[0].disps->X_op == O_constant)
5158 {
5159 /* Convert "jmp constant" (and "call constant") to a jump (call) to
5160 the absolute address given by the constant. Since ix86 jumps and
5161 calls are pc relative, we need to generate a reloc. */
5162 i.op[0].disps->X_add_symbol = &abs_symbol;
5163 i.op[0].disps->X_op = O_symbol;
5164 }
252b5132 5165
29b0f896
AM
5166 /* For 8 bit registers we need an empty rex prefix. Also if the
5167 instruction already has a prefix, we need to convert old
5168 registers to new ones. */
773f551c 5169
bab6aec1 5170 if ((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte
29b0f896 5171 && (i.op[0].regs->reg_flags & RegRex64) != 0)
bab6aec1 5172 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte
29b0f896 5173 && (i.op[1].regs->reg_flags & RegRex64) != 0)
bab6aec1
JB
5174 || (((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte)
5175 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte))
29b0f896
AM
5176 && i.rex != 0))
5177 {
5178 int x;
726c5dcd 5179
29b0f896
AM
5180 i.rex |= REX_OPCODE;
5181 for (x = 0; x < 2; x++)
5182 {
5183 /* Look for 8 bit operand that uses old registers. */
bab6aec1 5184 if (i.types[x].bitfield.class == Reg && i.types[x].bitfield.byte
29b0f896 5185 && (i.op[x].regs->reg_flags & RegRex64) == 0)
773f551c 5186 {
3f93af61 5187 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
29b0f896
AM
5188 /* In case it is "hi" register, give up. */
5189 if (i.op[x].regs->reg_num > 3)
a540244d 5190 as_bad (_("can't encode register '%s%s' in an "
4eed87de 5191 "instruction requiring REX prefix."),
a540244d 5192 register_prefix, i.op[x].regs->reg_name);
773f551c 5193
29b0f896
AM
5194 /* Otherwise it is equivalent to the extended register.
5195 Since the encoding doesn't change this is merely
5196 cosmetic cleanup for debug output. */
5197
5198 i.op[x].regs = i.op[x].regs + 8;
773f551c 5199 }
29b0f896
AM
5200 }
5201 }
773f551c 5202
6b6b6807
L
5203 if (i.rex == 0 && i.rex_encoding)
5204 {
5205 /* Check if we can add a REX_OPCODE byte. Look for 8 bit operand
3f93af61 5206 that uses legacy register. If it is "hi" register, don't add
6b6b6807
L
5207 the REX_OPCODE byte. */
5208 int x;
5209 for (x = 0; x < 2; x++)
bab6aec1 5210 if (i.types[x].bitfield.class == Reg
6b6b6807
L
5211 && i.types[x].bitfield.byte
5212 && (i.op[x].regs->reg_flags & RegRex64) == 0
5213 && i.op[x].regs->reg_num > 3)
5214 {
3f93af61 5215 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
5b7c81bd 5216 i.rex_encoding = false;
6b6b6807
L
5217 break;
5218 }
5219
5220 if (i.rex_encoding)
5221 i.rex = REX_OPCODE;
5222 }
5223
7ab9ffdd 5224 if (i.rex != 0)
29b0f896
AM
5225 add_prefix (REX_OPCODE | i.rex);
5226
ae531041
L
5227 insert_lfence_before ();
5228
29b0f896
AM
5229 /* We are ready to output the insn. */
5230 output_insn ();
e379e5f3 5231
ae531041
L
5232 insert_lfence_after ();
5233
e379e5f3
L
5234 last_insn.seg = now_seg;
5235
5236 if (i.tm.opcode_modifier.isprefix)
5237 {
5238 last_insn.kind = last_insn_prefix;
5239 last_insn.name = i.tm.name;
5240 last_insn.file = as_where (&last_insn.line);
5241 }
5242 else
5243 last_insn.kind = last_insn_other;
29b0f896
AM
5244}
5245
5246static char *
e3bb37b5 5247parse_insn (char *line, char *mnemonic)
29b0f896
AM
5248{
5249 char *l = line;
5250 char *token_start = l;
5251 char *mnem_p;
5c6af06e 5252 int supported;
d3ce72d0 5253 const insn_template *t;
b6169b20 5254 char *dot_p = NULL;
29b0f896 5255
29b0f896
AM
5256 while (1)
5257 {
5258 mnem_p = mnemonic;
5259 while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
5260 {
b6169b20
L
5261 if (*mnem_p == '.')
5262 dot_p = mnem_p;
29b0f896
AM
5263 mnem_p++;
5264 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
45288df1 5265 {
29b0f896
AM
5266 as_bad (_("no such instruction: `%s'"), token_start);
5267 return NULL;
5268 }
5269 l++;
5270 }
5271 if (!is_space_char (*l)
5272 && *l != END_OF_INSN
e44823cf
JB
5273 && (intel_syntax
5274 || (*l != PREFIX_SEPARATOR
5275 && *l != ',')))
29b0f896
AM
5276 {
5277 as_bad (_("invalid character %s in mnemonic"),
5278 output_invalid (*l));
5279 return NULL;
5280 }
5281 if (token_start == l)
5282 {
e44823cf 5283 if (!intel_syntax && *l == PREFIX_SEPARATOR)
29b0f896
AM
5284 as_bad (_("expecting prefix; got nothing"));
5285 else
5286 as_bad (_("expecting mnemonic; got nothing"));
5287 return NULL;
5288 }
45288df1 5289
29b0f896 5290 /* Look up instruction (or prefix) via hash table. */
629310ab 5291 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
47926f60 5292
29b0f896
AM
5293 if (*l != END_OF_INSN
5294 && (!is_space_char (*l) || l[1] != END_OF_INSN)
5295 && current_templates
40fb9820 5296 && current_templates->start->opcode_modifier.isprefix)
29b0f896 5297 {
c6fb90c8 5298 if (!cpu_flags_check_cpu64 (current_templates->start->cpu_flags))
2dd88dca
JB
5299 {
5300 as_bad ((flag_code != CODE_64BIT
5301 ? _("`%s' is only supported in 64-bit mode")
5302 : _("`%s' is not supported in 64-bit mode")),
5303 current_templates->start->name);
5304 return NULL;
5305 }
29b0f896
AM
5306 /* If we are in 16-bit mode, do not allow addr16 or data16.
5307 Similarly, in 32-bit mode, do not allow addr32 or data32. */
673fe0f0
JB
5308 if ((current_templates->start->opcode_modifier.size == SIZE16
5309 || current_templates->start->opcode_modifier.size == SIZE32)
29b0f896 5310 && flag_code != CODE_64BIT
673fe0f0 5311 && ((current_templates->start->opcode_modifier.size == SIZE32)
29b0f896
AM
5312 ^ (flag_code == CODE_16BIT)))
5313 {
5314 as_bad (_("redundant %s prefix"),
5315 current_templates->start->name);
5316 return NULL;
45288df1 5317 }
31184569
JB
5318
5319 if (current_templates->start->base_opcode == PSEUDO_PREFIX)
29b0f896 5320 {
86fa6981 5321 /* Handle pseudo prefixes. */
31184569 5322 switch (current_templates->start->extension_opcode)
86fa6981 5323 {
41eb8e88 5324 case Prefix_Disp8:
86fa6981
L
5325 /* {disp8} */
5326 i.disp_encoding = disp_encoding_8bit;
5327 break;
41eb8e88
L
5328 case Prefix_Disp16:
5329 /* {disp16} */
5330 i.disp_encoding = disp_encoding_16bit;
5331 break;
5332 case Prefix_Disp32:
86fa6981
L
5333 /* {disp32} */
5334 i.disp_encoding = disp_encoding_32bit;
5335 break;
41eb8e88 5336 case Prefix_Load:
86fa6981
L
5337 /* {load} */
5338 i.dir_encoding = dir_encoding_load;
5339 break;
41eb8e88 5340 case Prefix_Store:
86fa6981
L
5341 /* {store} */
5342 i.dir_encoding = dir_encoding_store;
5343 break;
41eb8e88 5344 case Prefix_VEX:
42e04b36
L
5345 /* {vex} */
5346 i.vec_encoding = vex_encoding_vex;
86fa6981 5347 break;
41eb8e88 5348 case Prefix_VEX3:
86fa6981
L
5349 /* {vex3} */
5350 i.vec_encoding = vex_encoding_vex3;
5351 break;
41eb8e88 5352 case Prefix_EVEX:
86fa6981
L
5353 /* {evex} */
5354 i.vec_encoding = vex_encoding_evex;
5355 break;
41eb8e88 5356 case Prefix_REX:
6b6b6807 5357 /* {rex} */
5b7c81bd 5358 i.rex_encoding = true;
6b6b6807 5359 break;
41eb8e88 5360 case Prefix_NoOptimize:
b6f8c7c4 5361 /* {nooptimize} */
5b7c81bd 5362 i.no_optimize = true;
b6f8c7c4 5363 break;
86fa6981
L
5364 default:
5365 abort ();
5366 }
5367 }
5368 else
5369 {
5370 /* Add prefix, checking for repeated prefixes. */
4e9ac44a 5371 switch (add_prefix (current_templates->start->base_opcode))
86fa6981 5372 {
4e9ac44a
L
5373 case PREFIX_EXIST:
5374 return NULL;
5375 case PREFIX_DS:
d777820b 5376 if (current_templates->start->cpu_flags.bitfield.cpuibt)
4e9ac44a
L
5377 i.notrack_prefix = current_templates->start->name;
5378 break;
5379 case PREFIX_REP:
5380 if (current_templates->start->cpu_flags.bitfield.cpuhle)
5381 i.hle_prefix = current_templates->start->name;
5382 else if (current_templates->start->cpu_flags.bitfield.cpumpx)
5383 i.bnd_prefix = current_templates->start->name;
5384 else
5385 i.rep_prefix = current_templates->start->name;
5386 break;
5387 default:
5388 break;
86fa6981 5389 }
29b0f896
AM
5390 }
5391 /* Skip past PREFIX_SEPARATOR and reset token_start. */
5392 token_start = ++l;
5393 }
5394 else
5395 break;
5396 }
45288df1 5397
30a55f88 5398 if (!current_templates)
b6169b20 5399 {
07d5e953
JB
5400 /* Deprecated functionality (new code should use pseudo-prefixes instead):
5401 Check if we should swap operand or force 32bit displacement in
f8a5c266 5402 encoding. */
30a55f88 5403 if (mnem_p - 2 == dot_p && dot_p[1] == 's')
64c49ab3 5404 i.dir_encoding = dir_encoding_swap;
8d63c93e 5405 else if (mnem_p - 3 == dot_p
a501d77e
L
5406 && dot_p[1] == 'd'
5407 && dot_p[2] == '8')
5408 i.disp_encoding = disp_encoding_8bit;
8d63c93e 5409 else if (mnem_p - 4 == dot_p
f8a5c266
L
5410 && dot_p[1] == 'd'
5411 && dot_p[2] == '3'
5412 && dot_p[3] == '2')
a501d77e 5413 i.disp_encoding = disp_encoding_32bit;
30a55f88
L
5414 else
5415 goto check_suffix;
5416 mnem_p = dot_p;
5417 *dot_p = '\0';
629310ab 5418 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
b6169b20
L
5419 }
5420
29b0f896
AM
5421 if (!current_templates)
5422 {
dc1e8a47 5423 check_suffix:
1c529385 5424 if (mnem_p > mnemonic)
29b0f896 5425 {
1c529385
LH
5426 /* See if we can get a match by trimming off a suffix. */
5427 switch (mnem_p[-1])
29b0f896 5428 {
1c529385
LH
5429 case WORD_MNEM_SUFFIX:
5430 if (intel_syntax && (intel_float_operand (mnemonic) & 2))
29b0f896
AM
5431 i.suffix = SHORT_MNEM_SUFFIX;
5432 else
1c529385
LH
5433 /* Fall through. */
5434 case BYTE_MNEM_SUFFIX:
5435 case QWORD_MNEM_SUFFIX:
5436 i.suffix = mnem_p[-1];
29b0f896 5437 mnem_p[-1] = '\0';
fe0e921f
AM
5438 current_templates
5439 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5440 break;
5441 case SHORT_MNEM_SUFFIX:
5442 case LONG_MNEM_SUFFIX:
5443 if (!intel_syntax)
5444 {
5445 i.suffix = mnem_p[-1];
5446 mnem_p[-1] = '\0';
fe0e921f
AM
5447 current_templates
5448 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5449 }
5450 break;
5451
5452 /* Intel Syntax. */
5453 case 'd':
5454 if (intel_syntax)
5455 {
5456 if (intel_float_operand (mnemonic) == 1)
5457 i.suffix = SHORT_MNEM_SUFFIX;
5458 else
5459 i.suffix = LONG_MNEM_SUFFIX;
5460 mnem_p[-1] = '\0';
fe0e921f
AM
5461 current_templates
5462 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5463 }
5464 break;
29b0f896 5465 }
29b0f896 5466 }
1c529385 5467
29b0f896
AM
5468 if (!current_templates)
5469 {
5470 as_bad (_("no such instruction: `%s'"), token_start);
5471 return NULL;
5472 }
5473 }
252b5132 5474
0cfa3eb3
JB
5475 if (current_templates->start->opcode_modifier.jump == JUMP
5476 || current_templates->start->opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
5477 {
5478 /* Check for a branch hint. We allow ",pt" and ",pn" for
5479 predict taken and predict not taken respectively.
5480 I'm not sure that branch hints actually do anything on loop
5481 and jcxz insns (JumpByte) for current Pentium4 chips. They
5482 may work in the future and it doesn't hurt to accept them
5483 now. */
5484 if (l[0] == ',' && l[1] == 'p')
5485 {
5486 if (l[2] == 't')
5487 {
5488 if (!add_prefix (DS_PREFIX_OPCODE))
5489 return NULL;
5490 l += 3;
5491 }
5492 else if (l[2] == 'n')
5493 {
5494 if (!add_prefix (CS_PREFIX_OPCODE))
5495 return NULL;
5496 l += 3;
5497 }
5498 }
5499 }
5500 /* Any other comma loses. */
5501 if (*l == ',')
5502 {
5503 as_bad (_("invalid character %s in mnemonic"),
5504 output_invalid (*l));
5505 return NULL;
5506 }
252b5132 5507
29b0f896 5508 /* Check if instruction is supported on specified architecture. */
5c6af06e
JB
5509 supported = 0;
5510 for (t = current_templates->start; t < current_templates->end; ++t)
5511 {
c0f3af97
L
5512 supported |= cpu_flags_match (t);
5513 if (supported == CPU_FLAGS_PERFECT_MATCH)
d59a54c2 5514 return l;
29b0f896 5515 }
3629bb00 5516
548d0ee6
JB
5517 if (!(supported & CPU_FLAGS_64BIT_MATCH))
5518 as_bad (flag_code == CODE_64BIT
5519 ? _("`%s' is not supported in 64-bit mode")
5520 : _("`%s' is only supported in 64-bit mode"),
5521 current_templates->start->name);
5522 else
5523 as_bad (_("`%s' is not supported on `%s%s'"),
5524 current_templates->start->name,
5525 cpu_arch_name ? cpu_arch_name : default_arch,
5526 cpu_sub_arch_name ? cpu_sub_arch_name : "");
252b5132 5527
548d0ee6 5528 return NULL;
29b0f896 5529}
252b5132 5530
29b0f896 5531static char *
e3bb37b5 5532parse_operands (char *l, const char *mnemonic)
29b0f896
AM
5533{
5534 char *token_start;
3138f287 5535
29b0f896
AM
5536 /* 1 if operand is pending after ','. */
5537 unsigned int expecting_operand = 0;
252b5132 5538
29b0f896
AM
5539 while (*l != END_OF_INSN)
5540 {
e68c3d59
JB
5541 /* Non-zero if operand parens not balanced. */
5542 unsigned int paren_not_balanced = 0;
5543 /* True if inside double quotes. */
5544 bool in_quotes = false;
5545
29b0f896
AM
5546 /* Skip optional white space before operand. */
5547 if (is_space_char (*l))
5548 ++l;
d02603dc 5549 if (!is_operand_char (*l) && *l != END_OF_INSN && *l != '"')
29b0f896
AM
5550 {
5551 as_bad (_("invalid character %s before operand %d"),
5552 output_invalid (*l),
5553 i.operands + 1);
5554 return NULL;
5555 }
d02603dc 5556 token_start = l; /* After white space. */
e68c3d59 5557 while (in_quotes || paren_not_balanced || *l != ',')
29b0f896
AM
5558 {
5559 if (*l == END_OF_INSN)
5560 {
e68c3d59
JB
5561 if (in_quotes)
5562 {
5563 as_bad (_("unbalanced double quotes in operand %d."),
5564 i.operands + 1);
5565 return NULL;
5566 }
29b0f896
AM
5567 if (paren_not_balanced)
5568 {
98ff9f1c
JB
5569 know (!intel_syntax);
5570 as_bad (_("unbalanced parenthesis in operand %d."),
5571 i.operands + 1);
29b0f896
AM
5572 return NULL;
5573 }
5574 else
5575 break; /* we are done */
5576 }
e68c3d59
JB
5577 else if (*l == '\\' && l[1] == '"')
5578 ++l;
5579 else if (*l == '"')
5580 in_quotes = !in_quotes;
5581 else if (!in_quotes && !is_operand_char (*l) && !is_space_char (*l))
29b0f896
AM
5582 {
5583 as_bad (_("invalid character %s in operand %d"),
5584 output_invalid (*l),
5585 i.operands + 1);
5586 return NULL;
5587 }
e68c3d59 5588 if (!intel_syntax && !in_quotes)
29b0f896
AM
5589 {
5590 if (*l == '(')
5591 ++paren_not_balanced;
5592 if (*l == ')')
5593 --paren_not_balanced;
5594 }
29b0f896
AM
5595 l++;
5596 }
5597 if (l != token_start)
5598 { /* Yes, we've read in another operand. */
5599 unsigned int operand_ok;
5600 this_operand = i.operands++;
5601 if (i.operands > MAX_OPERANDS)
5602 {
5603 as_bad (_("spurious operands; (%d operands/instruction max)"),
5604 MAX_OPERANDS);
5605 return NULL;
5606 }
9d46ce34 5607 i.types[this_operand].bitfield.unspecified = 1;
29b0f896
AM
5608 /* Now parse operand adding info to 'i' as we go along. */
5609 END_STRING_AND_SAVE (l);
5610
1286ab78
L
5611 if (i.mem_operands > 1)
5612 {
5613 as_bad (_("too many memory references for `%s'"),
5614 mnemonic);
5615 return 0;
5616 }
5617
29b0f896
AM
5618 if (intel_syntax)
5619 operand_ok =
5620 i386_intel_operand (token_start,
5621 intel_float_operand (mnemonic));
5622 else
a7619375 5623 operand_ok = i386_att_operand (token_start);
29b0f896
AM
5624
5625 RESTORE_END_STRING (l);
5626 if (!operand_ok)
5627 return NULL;
5628 }
5629 else
5630 {
5631 if (expecting_operand)
5632 {
5633 expecting_operand_after_comma:
5634 as_bad (_("expecting operand after ','; got nothing"));
5635 return NULL;
5636 }
5637 if (*l == ',')
5638 {
5639 as_bad (_("expecting operand before ','; got nothing"));
5640 return NULL;
5641 }
5642 }
7f3f1ea2 5643
29b0f896
AM
5644 /* Now *l must be either ',' or END_OF_INSN. */
5645 if (*l == ',')
5646 {
5647 if (*++l == END_OF_INSN)
5648 {
5649 /* Just skip it, if it's \n complain. */
5650 goto expecting_operand_after_comma;
5651 }
5652 expecting_operand = 1;
5653 }
5654 }
5655 return l;
5656}
7f3f1ea2 5657
050dfa73 5658static void
783c187b 5659swap_2_operands (unsigned int xchg1, unsigned int xchg2)
050dfa73
MM
5660{
5661 union i386_op temp_op;
40fb9820 5662 i386_operand_type temp_type;
c48dadc9 5663 unsigned int temp_flags;
050dfa73 5664 enum bfd_reloc_code_real temp_reloc;
4eed87de 5665
050dfa73
MM
5666 temp_type = i.types[xchg2];
5667 i.types[xchg2] = i.types[xchg1];
5668 i.types[xchg1] = temp_type;
c48dadc9
JB
5669
5670 temp_flags = i.flags[xchg2];
5671 i.flags[xchg2] = i.flags[xchg1];
5672 i.flags[xchg1] = temp_flags;
5673
050dfa73
MM
5674 temp_op = i.op[xchg2];
5675 i.op[xchg2] = i.op[xchg1];
5676 i.op[xchg1] = temp_op;
c48dadc9 5677
050dfa73
MM
5678 temp_reloc = i.reloc[xchg2];
5679 i.reloc[xchg2] = i.reloc[xchg1];
5680 i.reloc[xchg1] = temp_reloc;
43234a1e 5681
6225c532 5682 if (i.mask.reg)
43234a1e 5683 {
6225c532
JB
5684 if (i.mask.operand == xchg1)
5685 i.mask.operand = xchg2;
5686 else if (i.mask.operand == xchg2)
5687 i.mask.operand = xchg1;
43234a1e 5688 }
a5748e0d 5689 if (i.broadcast.type || i.broadcast.bytes)
43234a1e 5690 {
5273a3cd
JB
5691 if (i.broadcast.operand == xchg1)
5692 i.broadcast.operand = xchg2;
5693 else if (i.broadcast.operand == xchg2)
5694 i.broadcast.operand = xchg1;
43234a1e 5695 }
050dfa73
MM
5696}
5697
29b0f896 5698static void
e3bb37b5 5699swap_operands (void)
29b0f896 5700{
b7c61d9a 5701 switch (i.operands)
050dfa73 5702 {
c0f3af97 5703 case 5:
b7c61d9a 5704 case 4:
4d456e3d 5705 swap_2_operands (1, i.operands - 2);
1a0670f3 5706 /* Fall through. */
b7c61d9a
L
5707 case 3:
5708 case 2:
4d456e3d 5709 swap_2_operands (0, i.operands - 1);
b7c61d9a
L
5710 break;
5711 default:
5712 abort ();
29b0f896 5713 }
29b0f896
AM
5714
5715 if (i.mem_operands == 2)
5716 {
5e042380 5717 const reg_entry *temp_seg;
29b0f896
AM
5718 temp_seg = i.seg[0];
5719 i.seg[0] = i.seg[1];
5720 i.seg[1] = temp_seg;
5721 }
5722}
252b5132 5723
29b0f896
AM
5724/* Try to ensure constant immediates are represented in the smallest
5725 opcode possible. */
5726static void
e3bb37b5 5727optimize_imm (void)
29b0f896
AM
5728{
5729 char guess_suffix = 0;
5730 int op;
252b5132 5731
29b0f896
AM
5732 if (i.suffix)
5733 guess_suffix = i.suffix;
5734 else if (i.reg_operands)
5735 {
5736 /* Figure out a suffix from the last register operand specified.
75e5731b
JB
5737 We can't do this properly yet, i.e. excluding special register
5738 instances, but the following works for instructions with
5739 immediates. In any case, we can't set i.suffix yet. */
29b0f896 5740 for (op = i.operands; --op >= 0;)
bab6aec1
JB
5741 if (i.types[op].bitfield.class != Reg)
5742 continue;
5743 else if (i.types[op].bitfield.byte)
7ab9ffdd 5744 {
40fb9820
L
5745 guess_suffix = BYTE_MNEM_SUFFIX;
5746 break;
5747 }
bab6aec1 5748 else if (i.types[op].bitfield.word)
252b5132 5749 {
40fb9820
L
5750 guess_suffix = WORD_MNEM_SUFFIX;
5751 break;
5752 }
bab6aec1 5753 else if (i.types[op].bitfield.dword)
40fb9820
L
5754 {
5755 guess_suffix = LONG_MNEM_SUFFIX;
5756 break;
5757 }
bab6aec1 5758 else if (i.types[op].bitfield.qword)
40fb9820
L
5759 {
5760 guess_suffix = QWORD_MNEM_SUFFIX;
29b0f896 5761 break;
252b5132 5762 }
29b0f896
AM
5763 }
5764 else if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
5765 guess_suffix = WORD_MNEM_SUFFIX;
5766
5767 for (op = i.operands; --op >= 0;)
40fb9820 5768 if (operand_type_check (i.types[op], imm))
29b0f896
AM
5769 {
5770 switch (i.op[op].imms->X_op)
252b5132 5771 {
29b0f896
AM
5772 case O_constant:
5773 /* If a suffix is given, this operand may be shortened. */
5774 switch (guess_suffix)
252b5132 5775 {
29b0f896 5776 case LONG_MNEM_SUFFIX:
40fb9820
L
5777 i.types[op].bitfield.imm32 = 1;
5778 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
5779 break;
5780 case WORD_MNEM_SUFFIX:
40fb9820
L
5781 i.types[op].bitfield.imm16 = 1;
5782 i.types[op].bitfield.imm32 = 1;
5783 i.types[op].bitfield.imm32s = 1;
5784 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
5785 break;
5786 case BYTE_MNEM_SUFFIX:
40fb9820
L
5787 i.types[op].bitfield.imm8 = 1;
5788 i.types[op].bitfield.imm8s = 1;
5789 i.types[op].bitfield.imm16 = 1;
5790 i.types[op].bitfield.imm32 = 1;
5791 i.types[op].bitfield.imm32s = 1;
5792 i.types[op].bitfield.imm64 = 1;
29b0f896 5793 break;
252b5132 5794 }
252b5132 5795
29b0f896
AM
5796 /* If this operand is at most 16 bits, convert it
5797 to a signed 16 bit number before trying to see
5798 whether it will fit in an even smaller size.
5799 This allows a 16-bit operand such as $0xffe0 to
5800 be recognised as within Imm8S range. */
40fb9820 5801 if ((i.types[op].bitfield.imm16)
7e96fb68 5802 && fits_in_unsigned_word (i.op[op].imms->X_add_number))
252b5132 5803 {
87ed972d
JB
5804 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
5805 ^ 0x8000) - 0x8000);
29b0f896 5806 }
a28def75
L
5807#ifdef BFD64
5808 /* Store 32-bit immediate in 64-bit for 64-bit BFD. */
40fb9820 5809 if ((i.types[op].bitfield.imm32)
7e96fb68 5810 && fits_in_unsigned_long (i.op[op].imms->X_add_number))
29b0f896
AM
5811 {
5812 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
5813 ^ ((offsetT) 1 << 31))
5814 - ((offsetT) 1 << 31));
5815 }
a28def75 5816#endif
40fb9820 5817 i.types[op]
c6fb90c8
L
5818 = operand_type_or (i.types[op],
5819 smallest_imm_type (i.op[op].imms->X_add_number));
252b5132 5820
29b0f896
AM
5821 /* We must avoid matching of Imm32 templates when 64bit
5822 only immediate is available. */
5823 if (guess_suffix == QWORD_MNEM_SUFFIX)
40fb9820 5824 i.types[op].bitfield.imm32 = 0;
29b0f896 5825 break;
252b5132 5826
29b0f896
AM
5827 case O_absent:
5828 case O_register:
5829 abort ();
5830
5831 /* Symbols and expressions. */
5832 default:
9cd96992
JB
5833 /* Convert symbolic operand to proper sizes for matching, but don't
5834 prevent matching a set of insns that only supports sizes other
5835 than those matching the insn suffix. */
5836 {
40fb9820 5837 i386_operand_type mask, allowed;
87ed972d 5838 const insn_template *t = current_templates->start;
9cd96992 5839
0dfbf9d7 5840 operand_type_set (&mask, 0);
9cd96992
JB
5841 switch (guess_suffix)
5842 {
5843 case QWORD_MNEM_SUFFIX:
40fb9820
L
5844 mask.bitfield.imm64 = 1;
5845 mask.bitfield.imm32s = 1;
9cd96992
JB
5846 break;
5847 case LONG_MNEM_SUFFIX:
40fb9820 5848 mask.bitfield.imm32 = 1;
9cd96992
JB
5849 break;
5850 case WORD_MNEM_SUFFIX:
40fb9820 5851 mask.bitfield.imm16 = 1;
9cd96992
JB
5852 break;
5853 case BYTE_MNEM_SUFFIX:
40fb9820 5854 mask.bitfield.imm8 = 1;
9cd96992
JB
5855 break;
5856 default:
9cd96992
JB
5857 break;
5858 }
8f0212ac
JB
5859
5860 allowed = operand_type_and (t->operand_types[op], mask);
5861 while (++t < current_templates->end)
5862 {
5863 allowed = operand_type_or (allowed, t->operand_types[op]);
5864 allowed = operand_type_and (allowed, mask);
5865 }
5866
0dfbf9d7 5867 if (!operand_type_all_zero (&allowed))
c6fb90c8 5868 i.types[op] = operand_type_and (i.types[op], mask);
9cd96992 5869 }
29b0f896 5870 break;
252b5132 5871 }
29b0f896
AM
5872 }
5873}
47926f60 5874
29b0f896
AM
5875/* Try to use the smallest displacement type too. */
5876static void
e3bb37b5 5877optimize_disp (void)
29b0f896
AM
5878{
5879 int op;
3e73aa7c 5880
29b0f896 5881 for (op = i.operands; --op >= 0;)
40fb9820 5882 if (operand_type_check (i.types[op], disp))
252b5132 5883 {
b300c311 5884 if (i.op[op].disps->X_op == O_constant)
252b5132 5885 {
91d6fa6a 5886 offsetT op_disp = i.op[op].disps->X_add_number;
29b0f896 5887
91d6fa6a 5888 if (!op_disp && i.types[op].bitfield.baseindex)
b300c311 5889 {
2f2be86b
JB
5890 i.types[op] = operand_type_and_not (i.types[op], anydisp);
5891 i.op[op].disps = NULL;
b300c311 5892 i.disp_operands--;
f185acdd
JB
5893 continue;
5894 }
5895
5896 if (i.types[op].bitfield.disp16
cd613c1f 5897 && fits_in_unsigned_word (op_disp))
f185acdd
JB
5898 {
5899 /* If this operand is at most 16 bits, convert
5900 to a signed 16 bit number and don't use 64bit
5901 displacement. */
5902 op_disp = ((op_disp ^ 0x8000) - 0x8000);
5903 i.types[op].bitfield.disp64 = 0;
b300c311 5904 }
f185acdd 5905
28a167a4 5906#ifdef BFD64
a50187b2 5907 /* Optimize 64-bit displacement to 32-bit for 64-bit BFD. */
a775efc8
JB
5908 if ((flag_code != CODE_64BIT
5909 ? i.types[op].bitfield.disp32
5910 : want_disp32 (current_templates->start)
5911 && (!current_templates->start->opcode_modifier.jump
5912 || i.jumpabsolute || i.types[op].bitfield.baseindex))
a50187b2 5913 && fits_in_unsigned_long (op_disp))
b300c311 5914 {
a50187b2
JB
5915 /* If this operand is at most 32 bits, convert
5916 to a signed 32 bit number and don't use 64bit
5917 displacement. */
5918 op_disp = (op_disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
5919 i.types[op].bitfield.disp64 = 0;
5920 i.types[op].bitfield.disp32 = 1;
5921 }
28a167a4 5922
a50187b2
JB
5923 if (flag_code == CODE_64BIT && fits_in_signed_long (op_disp))
5924 {
5925 i.types[op].bitfield.disp64 = 0;
a775efc8 5926 i.types[op].bitfield.disp32 = 1;
b300c311 5927 }
28a167a4 5928#endif
40fb9820 5929 if ((i.types[op].bitfield.disp32
40fb9820 5930 || i.types[op].bitfield.disp16)
b5014f7a 5931 && fits_in_disp8 (op_disp))
40fb9820 5932 i.types[op].bitfield.disp8 = 1;
77c59789
JB
5933
5934 i.op[op].disps->X_add_number = op_disp;
252b5132 5935 }
67a4f2b7
AO
5936 else if (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
5937 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL)
5938 {
5939 fix_new_exp (frag_now, frag_more (0) - frag_now->fr_literal, 0,
5940 i.op[op].disps, 0, i.reloc[op]);
2f2be86b 5941 i.types[op] = operand_type_and_not (i.types[op], anydisp);
67a4f2b7
AO
5942 }
5943 else
b300c311 5944 /* We only support 64bit displacement on constants. */
40fb9820 5945 i.types[op].bitfield.disp64 = 0;
252b5132 5946 }
29b0f896
AM
5947}
5948
4a1b91ea
L
5949/* Return 1 if there is a match in broadcast bytes between operand
5950 GIVEN and instruction template T. */
5951
5952static INLINE int
5953match_broadcast_size (const insn_template *t, unsigned int given)
5954{
5955 return ((t->opcode_modifier.broadcast == BYTE_BROADCAST
5956 && i.types[given].bitfield.byte)
5957 || (t->opcode_modifier.broadcast == WORD_BROADCAST
5958 && i.types[given].bitfield.word)
5959 || (t->opcode_modifier.broadcast == DWORD_BROADCAST
5960 && i.types[given].bitfield.dword)
5961 || (t->opcode_modifier.broadcast == QWORD_BROADCAST
5962 && i.types[given].bitfield.qword));
5963}
5964
6c30d220
L
5965/* Check if operands are valid for the instruction. */
5966
5967static int
5968check_VecOperands (const insn_template *t)
5969{
43234a1e 5970 unsigned int op;
e2195274 5971 i386_cpu_flags cpu;
e2195274
JB
5972
5973 /* Templates allowing for ZMMword as well as YMMword and/or XMMword for
5974 any one operand are implicity requiring AVX512VL support if the actual
5975 operand size is YMMword or XMMword. Since this function runs after
5976 template matching, there's no need to check for YMMword/XMMword in
5977 the template. */
5978 cpu = cpu_flags_and (t->cpu_flags, avx512);
5979 if (!cpu_flags_all_zero (&cpu)
5980 && !t->cpu_flags.bitfield.cpuavx512vl
5981 && !cpu_arch_flags.bitfield.cpuavx512vl)
5982 {
5983 for (op = 0; op < t->operands; ++op)
5984 {
5985 if (t->operand_types[op].bitfield.zmmword
5986 && (i.types[op].bitfield.ymmword
5987 || i.types[op].bitfield.xmmword))
5988 {
5989 i.error = unsupported;
5990 return 1;
5991 }
5992 }
5993 }
43234a1e 5994
22c36940
JB
5995 /* Somewhat similarly, templates specifying both AVX and AVX2 are
5996 requiring AVX2 support if the actual operand size is YMMword. */
5997 if (t->cpu_flags.bitfield.cpuavx
5998 && t->cpu_flags.bitfield.cpuavx2
5999 && !cpu_arch_flags.bitfield.cpuavx2)
6000 {
6001 for (op = 0; op < t->operands; ++op)
6002 {
6003 if (t->operand_types[op].bitfield.xmmword
6004 && i.types[op].bitfield.ymmword)
6005 {
6006 i.error = unsupported;
6007 return 1;
6008 }
6009 }
6010 }
6011
6c30d220 6012 /* Without VSIB byte, we can't have a vector register for index. */
63112cd6 6013 if (!t->opcode_modifier.sib
6c30d220 6014 && i.index_reg
1b54b8d7
JB
6015 && (i.index_reg->reg_type.bitfield.xmmword
6016 || i.index_reg->reg_type.bitfield.ymmword
6017 || i.index_reg->reg_type.bitfield.zmmword))
6c30d220
L
6018 {
6019 i.error = unsupported_vector_index_register;
6020 return 1;
6021 }
6022
ad8ecc81
MZ
6023 /* Check if default mask is allowed. */
6024 if (t->opcode_modifier.nodefmask
6225c532 6025 && (!i.mask.reg || i.mask.reg->reg_num == 0))
ad8ecc81
MZ
6026 {
6027 i.error = no_default_mask;
6028 return 1;
6029 }
6030
7bab8ab5
JB
6031 /* For VSIB byte, we need a vector register for index, and all vector
6032 registers must be distinct. */
260cd341 6033 if (t->opcode_modifier.sib && t->opcode_modifier.sib != SIBMEM)
7bab8ab5
JB
6034 {
6035 if (!i.index_reg
63112cd6 6036 || !((t->opcode_modifier.sib == VECSIB128
1b54b8d7 6037 && i.index_reg->reg_type.bitfield.xmmword)
63112cd6 6038 || (t->opcode_modifier.sib == VECSIB256
1b54b8d7 6039 && i.index_reg->reg_type.bitfield.ymmword)
63112cd6 6040 || (t->opcode_modifier.sib == VECSIB512
1b54b8d7 6041 && i.index_reg->reg_type.bitfield.zmmword)))
7bab8ab5
JB
6042 {
6043 i.error = invalid_vsib_address;
6044 return 1;
6045 }
6046
6225c532
JB
6047 gas_assert (i.reg_operands == 2 || i.mask.reg);
6048 if (i.reg_operands == 2 && !i.mask.reg)
43234a1e 6049 {
3528c362 6050 gas_assert (i.types[0].bitfield.class == RegSIMD);
1b54b8d7
JB
6051 gas_assert (i.types[0].bitfield.xmmword
6052 || i.types[0].bitfield.ymmword);
3528c362 6053 gas_assert (i.types[2].bitfield.class == RegSIMD);
1b54b8d7
JB
6054 gas_assert (i.types[2].bitfield.xmmword
6055 || i.types[2].bitfield.ymmword);
43234a1e
L
6056 if (operand_check == check_none)
6057 return 0;
6058 if (register_number (i.op[0].regs)
6059 != register_number (i.index_reg)
6060 && register_number (i.op[2].regs)
6061 != register_number (i.index_reg)
6062 && register_number (i.op[0].regs)
6063 != register_number (i.op[2].regs))
6064 return 0;
6065 if (operand_check == check_error)
6066 {
6067 i.error = invalid_vector_register_set;
6068 return 1;
6069 }
6070 as_warn (_("mask, index, and destination registers should be distinct"));
6071 }
6225c532 6072 else if (i.reg_operands == 1 && i.mask.reg)
8444f82a 6073 {
3528c362 6074 if (i.types[1].bitfield.class == RegSIMD
1b54b8d7
JB
6075 && (i.types[1].bitfield.xmmword
6076 || i.types[1].bitfield.ymmword
6077 || i.types[1].bitfield.zmmword)
8444f82a
MZ
6078 && (register_number (i.op[1].regs)
6079 == register_number (i.index_reg)))
6080 {
6081 if (operand_check == check_error)
6082 {
6083 i.error = invalid_vector_register_set;
6084 return 1;
6085 }
6086 if (operand_check != check_none)
6087 as_warn (_("index and destination registers should be distinct"));
6088 }
6089 }
43234a1e 6090 }
7bab8ab5 6091
fc141319
L
6092 /* For AMX instructions with 3 TMM register operands, all operands
6093 must be distinct. */
6094 if (i.reg_operands == 3
6095 && t->operand_types[0].bitfield.tmmword
6096 && (i.op[0].regs == i.op[1].regs
6097 || i.op[0].regs == i.op[2].regs
6098 || i.op[1].regs == i.op[2].regs))
6099 {
6100 i.error = invalid_tmm_register_set;
6101 return 1;
260cd341
LC
6102 }
6103
0cc78721
CL
6104 /* For some special instructions require that destination must be distinct
6105 from source registers. */
6106 if (t->opcode_modifier.distinctdest)
6107 {
6108 unsigned int dest_reg = i.operands - 1;
6109
6110 know (i.operands >= 3);
6111
6112 /* #UD if dest_reg == src1_reg or dest_reg == src2_reg. */
6113 if (i.op[dest_reg - 1].regs == i.op[dest_reg].regs
6114 || (i.reg_operands > 2
6115 && i.op[dest_reg - 2].regs == i.op[dest_reg].regs))
6116 {
6117 i.error = invalid_dest_and_src_register_set;
6118 return 1;
6119 }
6120 }
6121
43234a1e
L
6122 /* Check if broadcast is supported by the instruction and is applied
6123 to the memory operand. */
a5748e0d 6124 if (i.broadcast.type || i.broadcast.bytes)
43234a1e 6125 {
8e6e0792 6126 i386_operand_type type, overlap;
43234a1e
L
6127
6128 /* Check if specified broadcast is supported in this instruction,
4a1b91ea 6129 and its broadcast bytes match the memory operand. */
5273a3cd 6130 op = i.broadcast.operand;
8e6e0792 6131 if (!t->opcode_modifier.broadcast
c48dadc9 6132 || !(i.flags[op] & Operand_Mem)
c39e5b26 6133 || (!i.types[op].bitfield.unspecified
4a1b91ea 6134 && !match_broadcast_size (t, op)))
43234a1e
L
6135 {
6136 bad_broadcast:
6137 i.error = unsupported_broadcast;
6138 return 1;
6139 }
8e6e0792 6140
a5748e0d
JB
6141 if (i.broadcast.type)
6142 i.broadcast.bytes = ((1 << (t->opcode_modifier.broadcast - 1))
6143 * i.broadcast.type);
8e6e0792 6144 operand_type_set (&type, 0);
a5748e0d 6145 switch (get_broadcast_bytes (t, false))
8e6e0792 6146 {
4a1b91ea
L
6147 case 2:
6148 type.bitfield.word = 1;
6149 break;
6150 case 4:
6151 type.bitfield.dword = 1;
6152 break;
8e6e0792
JB
6153 case 8:
6154 type.bitfield.qword = 1;
6155 break;
6156 case 16:
6157 type.bitfield.xmmword = 1;
6158 break;
6159 case 32:
6160 type.bitfield.ymmword = 1;
6161 break;
6162 case 64:
6163 type.bitfield.zmmword = 1;
6164 break;
6165 default:
6166 goto bad_broadcast;
6167 }
6168
6169 overlap = operand_type_and (type, t->operand_types[op]);
bc49bfd8
JB
6170 if (t->operand_types[op].bitfield.class == RegSIMD
6171 && t->operand_types[op].bitfield.byte
6172 + t->operand_types[op].bitfield.word
6173 + t->operand_types[op].bitfield.dword
6174 + t->operand_types[op].bitfield.qword > 1)
6175 {
6176 overlap.bitfield.xmmword = 0;
6177 overlap.bitfield.ymmword = 0;
6178 overlap.bitfield.zmmword = 0;
6179 }
8e6e0792
JB
6180 if (operand_type_all_zero (&overlap))
6181 goto bad_broadcast;
6182
6183 if (t->opcode_modifier.checkregsize)
6184 {
6185 unsigned int j;
6186
e2195274 6187 type.bitfield.baseindex = 1;
8e6e0792
JB
6188 for (j = 0; j < i.operands; ++j)
6189 {
6190 if (j != op
6191 && !operand_type_register_match(i.types[j],
6192 t->operand_types[j],
6193 type,
6194 t->operand_types[op]))
6195 goto bad_broadcast;
6196 }
6197 }
43234a1e
L
6198 }
6199 /* If broadcast is supported in this instruction, we need to check if
6200 operand of one-element size isn't specified without broadcast. */
6201 else if (t->opcode_modifier.broadcast && i.mem_operands)
6202 {
6203 /* Find memory operand. */
6204 for (op = 0; op < i.operands; op++)
8dc0818e 6205 if (i.flags[op] & Operand_Mem)
43234a1e
L
6206 break;
6207 gas_assert (op < i.operands);
6208 /* Check size of the memory operand. */
4a1b91ea 6209 if (match_broadcast_size (t, op))
43234a1e
L
6210 {
6211 i.error = broadcast_needed;
6212 return 1;
6213 }
6214 }
c39e5b26
JB
6215 else
6216 op = MAX_OPERANDS - 1; /* Avoid uninitialized variable warning. */
43234a1e
L
6217
6218 /* Check if requested masking is supported. */
6225c532 6219 if (i.mask.reg)
43234a1e 6220 {
ae2387fe
JB
6221 switch (t->opcode_modifier.masking)
6222 {
6223 case BOTH_MASKING:
6224 break;
6225 case MERGING_MASKING:
6225c532 6226 if (i.mask.zeroing)
ae2387fe
JB
6227 {
6228 case 0:
6229 i.error = unsupported_masking;
6230 return 1;
6231 }
6232 break;
6233 case DYNAMIC_MASKING:
6234 /* Memory destinations allow only merging masking. */
6225c532 6235 if (i.mask.zeroing && i.mem_operands)
ae2387fe
JB
6236 {
6237 /* Find memory operand. */
6238 for (op = 0; op < i.operands; op++)
c48dadc9 6239 if (i.flags[op] & Operand_Mem)
ae2387fe
JB
6240 break;
6241 gas_assert (op < i.operands);
6242 if (op == i.operands - 1)
6243 {
6244 i.error = unsupported_masking;
6245 return 1;
6246 }
6247 }
6248 break;
6249 default:
6250 abort ();
6251 }
43234a1e
L
6252 }
6253
6254 /* Check if masking is applied to dest operand. */
6225c532 6255 if (i.mask.reg && (i.mask.operand != i.operands - 1))
43234a1e
L
6256 {
6257 i.error = mask_not_on_destination;
6258 return 1;
6259 }
6260
43234a1e 6261 /* Check RC/SAE. */
ca5312a2 6262 if (i.rounding.type != rc_none)
43234a1e 6263 {
a80195f1 6264 if (!t->opcode_modifier.sae
cf665fee
JB
6265 || ((i.rounding.type != saeonly) != t->opcode_modifier.staticrounding)
6266 || i.mem_operands)
43234a1e
L
6267 {
6268 i.error = unsupported_rc_sae;
6269 return 1;
6270 }
cf665fee
JB
6271
6272 /* Non-EVEX.LIG forms need to have a ZMM register as at least one
6273 operand. */
6274 if (t->opcode_modifier.evex != EVEXLIG)
7bab8ab5 6275 {
cf665fee
JB
6276 for (op = 0; op < t->operands; ++op)
6277 if (i.types[op].bitfield.zmmword)
6278 break;
6279 if (op >= t->operands)
6280 {
6281 i.error = operand_size_mismatch;
6282 return 1;
6283 }
7bab8ab5 6284 }
6c30d220
L
6285 }
6286
da4977e0
JB
6287 /* Check the special Imm4 cases; must be the first operand. */
6288 if (t->cpu_flags.bitfield.cpuxop && t->operands == 5)
6289 {
6290 if (i.op[0].imms->X_op != O_constant
6291 || !fits_in_imm4 (i.op[0].imms->X_add_number))
6292 {
6293 i.error = bad_imm4;
6294 return 1;
6295 }
6296
6297 /* Turn off Imm<N> so that update_imm won't complain. */
6298 operand_type_set (&i.types[0], 0);
6299 }
6300
43234a1e 6301 /* Check vector Disp8 operand. */
b5014f7a 6302 if (t->opcode_modifier.disp8memshift
1a42a9fe 6303 && i.disp_encoding <= disp_encoding_8bit)
43234a1e 6304 {
a5748e0d 6305 if (i.broadcast.bytes)
4a1b91ea 6306 i.memshift = t->opcode_modifier.broadcast - 1;
7091c612 6307 else if (t->opcode_modifier.disp8memshift != DISP8_SHIFT_VL)
43234a1e 6308 i.memshift = t->opcode_modifier.disp8memshift;
7091c612
JB
6309 else
6310 {
125ff819 6311 const i386_operand_type *type = NULL, *fallback = NULL;
7091c612
JB
6312
6313 i.memshift = 0;
6314 for (op = 0; op < i.operands; op++)
8dc0818e 6315 if (i.flags[op] & Operand_Mem)
7091c612 6316 {
4174bfff
JB
6317 if (t->opcode_modifier.evex == EVEXLIG)
6318 i.memshift = 2 + (i.suffix == QWORD_MNEM_SUFFIX);
6319 else if (t->operand_types[op].bitfield.xmmword
6320 + t->operand_types[op].bitfield.ymmword
6321 + t->operand_types[op].bitfield.zmmword <= 1)
7091c612
JB
6322 type = &t->operand_types[op];
6323 else if (!i.types[op].bitfield.unspecified)
6324 type = &i.types[op];
125ff819
JB
6325 else /* Ambiguities get resolved elsewhere. */
6326 fallback = &t->operand_types[op];
7091c612 6327 }
3528c362 6328 else if (i.types[op].bitfield.class == RegSIMD
4174bfff 6329 && t->opcode_modifier.evex != EVEXLIG)
7091c612
JB
6330 {
6331 if (i.types[op].bitfield.zmmword)
6332 i.memshift = 6;
6333 else if (i.types[op].bitfield.ymmword && i.memshift < 5)
6334 i.memshift = 5;
6335 else if (i.types[op].bitfield.xmmword && i.memshift < 4)
6336 i.memshift = 4;
6337 }
6338
125ff819
JB
6339 if (!type && !i.memshift)
6340 type = fallback;
7091c612
JB
6341 if (type)
6342 {
6343 if (type->bitfield.zmmword)
6344 i.memshift = 6;
6345 else if (type->bitfield.ymmword)
6346 i.memshift = 5;
6347 else if (type->bitfield.xmmword)
6348 i.memshift = 4;
6349 }
6350
6351 /* For the check in fits_in_disp8(). */
6352 if (i.memshift == 0)
6353 i.memshift = -1;
6354 }
43234a1e
L
6355
6356 for (op = 0; op < i.operands; op++)
6357 if (operand_type_check (i.types[op], disp)
6358 && i.op[op].disps->X_op == O_constant)
6359 {
b5014f7a 6360 if (fits_in_disp8 (i.op[op].disps->X_add_number))
43234a1e 6361 {
b5014f7a
JB
6362 i.types[op].bitfield.disp8 = 1;
6363 return 0;
43234a1e 6364 }
b5014f7a 6365 i.types[op].bitfield.disp8 = 0;
43234a1e
L
6366 }
6367 }
b5014f7a
JB
6368
6369 i.memshift = 0;
43234a1e 6370
6c30d220
L
6371 return 0;
6372}
6373
da4977e0 6374/* Check if encoding requirements are met by the instruction. */
a683cc34
SP
6375
6376static int
da4977e0 6377VEX_check_encoding (const insn_template *t)
a683cc34 6378{
da4977e0
JB
6379 if (i.vec_encoding == vex_encoding_error)
6380 {
6381 i.error = unsupported;
6382 return 1;
6383 }
6384
86fa6981 6385 if (i.vec_encoding == vex_encoding_evex)
43234a1e 6386 {
86fa6981 6387 /* This instruction must be encoded with EVEX prefix. */
e771e7c9 6388 if (!is_evex_encoding (t))
86fa6981
L
6389 {
6390 i.error = unsupported;
6391 return 1;
6392 }
6393 return 0;
43234a1e
L
6394 }
6395
a683cc34 6396 if (!t->opcode_modifier.vex)
86fa6981
L
6397 {
6398 /* This instruction template doesn't have VEX prefix. */
6399 if (i.vec_encoding != vex_encoding_default)
6400 {
6401 i.error = unsupported;
6402 return 1;
6403 }
6404 return 0;
6405 }
a683cc34 6406
a683cc34
SP
6407 return 0;
6408}
6409
7b94647a
JB
6410/* Helper function for the progress() macro in match_template(). */
6411static INLINE enum i386_error progress (enum i386_error new,
6412 enum i386_error last,
6413 unsigned int line, unsigned int *line_p)
6414{
6415 if (line <= *line_p)
6416 return last;
6417 *line_p = line;
6418 return new;
6419}
6420
d3ce72d0 6421static const insn_template *
83b16ac6 6422match_template (char mnem_suffix)
29b0f896
AM
6423{
6424 /* Points to template once we've found it. */
d3ce72d0 6425 const insn_template *t;
40fb9820 6426 i386_operand_type overlap0, overlap1, overlap2, overlap3;
c0f3af97 6427 i386_operand_type overlap4;
29b0f896 6428 unsigned int found_reverse_match;
dc2be329 6429 i386_opcode_modifier suffix_check;
40fb9820 6430 i386_operand_type operand_types [MAX_OPERANDS];
539e75ad 6431 int addr_prefix_disp;
7b94647a
JB
6432 unsigned int j, size_match, check_register, errline = __LINE__;
6433 enum i386_error specific_error = number_of_operands_mismatch;
6434#define progress(err) progress (err, specific_error, __LINE__, &errline)
29b0f896 6435
c0f3af97
L
6436#if MAX_OPERANDS != 5
6437# error "MAX_OPERANDS must be 5."
f48ff2ae
L
6438#endif
6439
29b0f896 6440 found_reverse_match = 0;
539e75ad 6441 addr_prefix_disp = -1;
40fb9820 6442
dc2be329 6443 /* Prepare for mnemonic suffix check. */
40fb9820 6444 memset (&suffix_check, 0, sizeof (suffix_check));
dc2be329
L
6445 switch (mnem_suffix)
6446 {
6447 case BYTE_MNEM_SUFFIX:
6448 suffix_check.no_bsuf = 1;
6449 break;
6450 case WORD_MNEM_SUFFIX:
6451 suffix_check.no_wsuf = 1;
6452 break;
6453 case SHORT_MNEM_SUFFIX:
6454 suffix_check.no_ssuf = 1;
6455 break;
6456 case LONG_MNEM_SUFFIX:
6457 suffix_check.no_lsuf = 1;
6458 break;
6459 case QWORD_MNEM_SUFFIX:
6460 suffix_check.no_qsuf = 1;
6461 break;
6462 default:
6463 /* NB: In Intel syntax, normally we can check for memory operand
6464 size when there is no mnemonic suffix. But jmp and call have
6465 2 different encodings with Dword memory operand size, one with
6466 No_ldSuf and the other without. i.suffix is set to
6467 LONG_DOUBLE_MNEM_SUFFIX to skip the one with No_ldSuf. */
6468 if (i.suffix == LONG_DOUBLE_MNEM_SUFFIX)
6469 suffix_check.no_ldsuf = 1;
83b16ac6
JB
6470 }
6471
45aa61fe 6472 for (t = current_templates->start; t < current_templates->end; t++)
29b0f896 6473 {
539e75ad 6474 addr_prefix_disp = -1;
dbbc8b7e 6475 found_reverse_match = 0;
539e75ad 6476
7b94647a 6477 /* Must have right number of operands. */
29b0f896
AM
6478 if (i.operands != t->operands)
6479 continue;
6480
50aecf8c 6481 /* Check processor support. */
7b94647a 6482 specific_error = progress (unsupported);
45a4bb20 6483 if (cpu_flags_match (t) != CPU_FLAGS_PERFECT_MATCH)
50aecf8c
L
6484 continue;
6485
e1d4d893 6486 /* Check AT&T mnemonic. */
7b94647a 6487 specific_error = progress (unsupported_with_intel_mnemonic);
e1d4d893 6488 if (intel_mnemonic && t->opcode_modifier.attmnemonic)
1efbbeb4
L
6489 continue;
6490
4b5aaf5f 6491 /* Check AT&T/Intel syntax. */
7b94647a 6492 specific_error = progress (unsupported_syntax);
5c07affc 6493 if ((intel_syntax && t->opcode_modifier.attsyntax)
4b5aaf5f 6494 || (!intel_syntax && t->opcode_modifier.intelsyntax))
1efbbeb4
L
6495 continue;
6496
4b5aaf5f
L
6497 /* Check Intel64/AMD64 ISA. */
6498 switch (isa64)
6499 {
6500 default:
6501 /* Default: Don't accept Intel64. */
6502 if (t->opcode_modifier.isa64 == INTEL64)
6503 continue;
6504 break;
6505 case amd64:
6506 /* -mamd64: Don't accept Intel64 and Intel64 only. */
6507 if (t->opcode_modifier.isa64 >= INTEL64)
6508 continue;
6509 break;
6510 case intel64:
6511 /* -mintel64: Don't accept AMD64. */
5990e377 6512 if (t->opcode_modifier.isa64 == AMD64 && flag_code == CODE_64BIT)
4b5aaf5f
L
6513 continue;
6514 break;
6515 }
6516
dc2be329 6517 /* Check the suffix. */
7b94647a 6518 specific_error = progress (invalid_instruction_suffix);
dc2be329
L
6519 if ((t->opcode_modifier.no_bsuf && suffix_check.no_bsuf)
6520 || (t->opcode_modifier.no_wsuf && suffix_check.no_wsuf)
6521 || (t->opcode_modifier.no_lsuf && suffix_check.no_lsuf)
6522 || (t->opcode_modifier.no_ssuf && suffix_check.no_ssuf)
6523 || (t->opcode_modifier.no_qsuf && suffix_check.no_qsuf)
6524 || (t->opcode_modifier.no_ldsuf && suffix_check.no_ldsuf))
83b16ac6 6525 continue;
29b0f896 6526
7b94647a 6527 specific_error = progress (operand_size_mismatch);
3ac21baa
JB
6528 size_match = operand_size_match (t);
6529 if (!size_match)
7d5e4556 6530 continue;
539e75ad 6531
6f2f06be
JB
6532 /* This is intentionally not
6533
0cfa3eb3 6534 if (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE))
6f2f06be
JB
6535
6536 as the case of a missing * on the operand is accepted (perhaps with
6537 a warning, issued further down). */
7b94647a 6538 specific_error = progress (operand_type_mismatch);
0cfa3eb3 6539 if (i.jumpabsolute && t->opcode_modifier.jump != JUMP_ABSOLUTE)
7b94647a 6540 continue;
6f2f06be 6541
5c07affc
L
6542 for (j = 0; j < MAX_OPERANDS; j++)
6543 operand_types[j] = t->operand_types[j];
6544
e365e234
JB
6545 /* In general, don't allow
6546 - 64-bit operands outside of 64-bit mode,
6547 - 32-bit operands on pre-386. */
7b94647a
JB
6548 specific_error = progress (mnem_suffix ? invalid_instruction_suffix
6549 : operand_size_mismatch);
4873e243 6550 j = i.imm_operands + (t->operands > i.imm_operands + 1);
e365e234
JB
6551 if (((i.suffix == QWORD_MNEM_SUFFIX
6552 && flag_code != CODE_64BIT
389d00a5
JB
6553 && !(t->opcode_modifier.opcodespace == SPACE_0F
6554 && t->base_opcode == 0xc7
5e74b495 6555 && t->opcode_modifier.opcodeprefix == PREFIX_NONE
8b65b895 6556 && t->extension_opcode == 1) /* cmpxchg8b */)
e365e234
JB
6557 || (i.suffix == LONG_MNEM_SUFFIX
6558 && !cpu_arch_flags.bitfield.cpui386))
45aa61fe 6559 && (intel_syntax
3cd7f3e3 6560 ? (t->opcode_modifier.mnemonicsize != IGNORESIZE
45aa61fe
AM
6561 && !intel_float_operand (t->name))
6562 : intel_float_operand (t->name) != 2)
4873e243
JB
6563 && (t->operands == i.imm_operands
6564 || (operand_types[i.imm_operands].bitfield.class != RegMMX
6565 && operand_types[i.imm_operands].bitfield.class != RegSIMD
6566 && operand_types[i.imm_operands].bitfield.class != RegMask)
6567 || (operand_types[j].bitfield.class != RegMMX
6568 && operand_types[j].bitfield.class != RegSIMD
6569 && operand_types[j].bitfield.class != RegMask))
63112cd6 6570 && !t->opcode_modifier.sib)
192dc9c6
JB
6571 continue;
6572
29b0f896 6573 /* Do not verify operands when there are none. */
e365e234 6574 if (!t->operands)
da4977e0
JB
6575 {
6576 if (VEX_check_encoding (t))
6577 {
7b94647a 6578 specific_error = progress (i.error);
da4977e0
JB
6579 continue;
6580 }
6581
6582 /* We've found a match; break out of loop. */
6583 break;
6584 }
252b5132 6585
48bcea9f
JB
6586 if (!t->opcode_modifier.jump
6587 || t->opcode_modifier.jump == JUMP_ABSOLUTE)
6588 {
6589 /* There should be only one Disp operand. */
6590 for (j = 0; j < MAX_OPERANDS; j++)
6591 if (operand_type_check (operand_types[j], disp))
539e75ad 6592 break;
48bcea9f
JB
6593 if (j < MAX_OPERANDS)
6594 {
5b7c81bd 6595 bool override = (i.prefix[ADDR_PREFIX] != 0);
48bcea9f
JB
6596
6597 addr_prefix_disp = j;
6598
a775efc8
JB
6599 /* Address size prefix will turn Disp64 operand into Disp32 and
6600 Disp32/Disp16 one into Disp16/Disp32 respectively. */
48bcea9f 6601 switch (flag_code)
40fb9820 6602 {
48bcea9f
JB
6603 case CODE_16BIT:
6604 override = !override;
6605 /* Fall through. */
6606 case CODE_32BIT:
6607 if (operand_types[j].bitfield.disp32
6608 && operand_types[j].bitfield.disp16)
40fb9820 6609 {
48bcea9f
JB
6610 operand_types[j].bitfield.disp16 = override;
6611 operand_types[j].bitfield.disp32 = !override;
40fb9820 6612 }
a775efc8 6613 gas_assert (!operand_types[j].bitfield.disp64);
48bcea9f
JB
6614 break;
6615
6616 case CODE_64BIT:
a775efc8 6617 if (operand_types[j].bitfield.disp64)
40fb9820 6618 {
a775efc8 6619 gas_assert (!operand_types[j].bitfield.disp32);
48bcea9f 6620 operand_types[j].bitfield.disp32 = override;
a775efc8 6621 operand_types[j].bitfield.disp64 = !override;
40fb9820 6622 }
48bcea9f
JB
6623 operand_types[j].bitfield.disp16 = 0;
6624 break;
40fb9820 6625 }
539e75ad 6626 }
48bcea9f 6627 }
539e75ad 6628
d7e3e627
L
6629 switch (i.reloc[0])
6630 {
6631 case BFD_RELOC_386_GOT32:
6632 /* Force 0x8b encoding for "mov foo@GOT, %eax". */
6633 if (t->base_opcode == 0xa0
6634 && t->opcode_modifier.opcodespace == SPACE_BASE)
6635 continue;
6636 break;
6637 case BFD_RELOC_386_TLS_GOTIE:
6638 case BFD_RELOC_386_TLS_LE_32:
6639 case BFD_RELOC_X86_64_GOTTPOFF:
6640 case BFD_RELOC_X86_64_TLSLD:
6641 /* Don't allow KMOV in TLS code sequences. */
6642 if (t->opcode_modifier.vex)
6643 continue;
6644 break;
6645 default:
6646 break;
6647 }
02a86693 6648
56ffb741 6649 /* We check register size if needed. */
e2195274
JB
6650 if (t->opcode_modifier.checkregsize)
6651 {
6652 check_register = (1 << t->operands) - 1;
a5748e0d 6653 if (i.broadcast.type || i.broadcast.bytes)
5273a3cd 6654 check_register &= ~(1 << i.broadcast.operand);
e2195274
JB
6655 }
6656 else
6657 check_register = 0;
6658
c6fb90c8 6659 overlap0 = operand_type_and (i.types[0], operand_types[0]);
29b0f896
AM
6660 switch (t->operands)
6661 {
6662 case 1:
40fb9820 6663 if (!operand_type_match (overlap0, i.types[0]))
29b0f896
AM
6664 continue;
6665 break;
6666 case 2:
33eaf5de 6667 /* xchg %eax, %eax is a special case. It is an alias for nop
8b38ad71
L
6668 only in 32bit mode and we can use opcode 0x90. In 64bit
6669 mode, we can't use 0x90 for xchg %eax, %eax since it should
6670 zero-extend %eax to %rax. */
6671 if (flag_code == CODE_64BIT
6672 && t->base_opcode == 0x90
35648716 6673 && t->opcode_modifier.opcodespace == SPACE_BASE
75e5731b
JB
6674 && i.types[0].bitfield.instance == Accum
6675 && i.types[0].bitfield.dword
6676 && i.types[1].bitfield.instance == Accum
6677 && i.types[1].bitfield.dword)
8b38ad71 6678 continue;
1212781b
JB
6679 /* xrelease mov %eax, <disp> is another special case. It must not
6680 match the accumulator-only encoding of mov. */
6681 if (flag_code != CODE_64BIT
6682 && i.hle_prefix
6683 && t->base_opcode == 0xa0
35648716 6684 && t->opcode_modifier.opcodespace == SPACE_BASE
75e5731b 6685 && i.types[0].bitfield.instance == Accum
8dc0818e 6686 && (i.flags[1] & Operand_Mem))
1212781b 6687 continue;
f5eb1d70
JB
6688 /* Fall through. */
6689
6690 case 3:
3ac21baa
JB
6691 if (!(size_match & MATCH_STRAIGHT))
6692 goto check_reverse;
64c49ab3
JB
6693 /* Reverse direction of operands if swapping is possible in the first
6694 place (operands need to be symmetric) and
6695 - the load form is requested, and the template is a store form,
6696 - the store form is requested, and the template is a load form,
6697 - the non-default (swapped) form is requested. */
6698 overlap1 = operand_type_and (operand_types[0], operand_types[1]);
f5eb1d70 6699 if (t->opcode_modifier.d && i.reg_operands == i.operands
64c49ab3
JB
6700 && !operand_type_all_zero (&overlap1))
6701 switch (i.dir_encoding)
6702 {
6703 case dir_encoding_load:
6704 if (operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 6705 || t->opcode_modifier.regmem)
64c49ab3
JB
6706 goto check_reverse;
6707 break;
6708
6709 case dir_encoding_store:
6710 if (!operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 6711 && !t->opcode_modifier.regmem)
64c49ab3
JB
6712 goto check_reverse;
6713 break;
6714
6715 case dir_encoding_swap:
6716 goto check_reverse;
6717
6718 case dir_encoding_default:
6719 break;
6720 }
86fa6981 6721 /* If we want store form, we skip the current load. */
64c49ab3
JB
6722 if ((i.dir_encoding == dir_encoding_store
6723 || i.dir_encoding == dir_encoding_swap)
86fa6981
L
6724 && i.mem_operands == 0
6725 && t->opcode_modifier.load)
fa99fab2 6726 continue;
1a0670f3 6727 /* Fall through. */
f48ff2ae 6728 case 4:
c0f3af97 6729 case 5:
c6fb90c8 6730 overlap1 = operand_type_and (i.types[1], operand_types[1]);
40fb9820
L
6731 if (!operand_type_match (overlap0, i.types[0])
6732 || !operand_type_match (overlap1, i.types[1])
e2195274 6733 || ((check_register & 3) == 3
dc821c5f 6734 && !operand_type_register_match (i.types[0],
40fb9820 6735 operand_types[0],
dc821c5f 6736 i.types[1],
40fb9820 6737 operand_types[1])))
29b0f896 6738 {
7b94647a
JB
6739 specific_error = progress (i.error);
6740
29b0f896 6741 /* Check if other direction is valid ... */
38e314eb 6742 if (!t->opcode_modifier.d)
29b0f896
AM
6743 continue;
6744
dc1e8a47 6745 check_reverse:
3ac21baa
JB
6746 if (!(size_match & MATCH_REVERSE))
6747 continue;
29b0f896 6748 /* Try reversing direction of operands. */
8bd915b7
JB
6749 j = t->opcode_modifier.vexsources ? 1 : i.operands - 1;
6750 overlap0 = operand_type_and (i.types[0], operand_types[j]);
6751 overlap1 = operand_type_and (i.types[j], operand_types[0]);
c975cec5
JB
6752 overlap2 = operand_type_and (i.types[1], operand_types[1]);
6753 gas_assert (t->operands != 3 || !check_register);
40fb9820 6754 if (!operand_type_match (overlap0, i.types[0])
8bd915b7 6755 || !operand_type_match (overlap1, i.types[j])
c975cec5
JB
6756 || (t->operands == 3
6757 && !operand_type_match (overlap2, i.types[1]))
45664ddb 6758 || (check_register
dc821c5f 6759 && !operand_type_register_match (i.types[0],
8bd915b7
JB
6760 operand_types[j],
6761 i.types[j],
45664ddb 6762 operand_types[0])))
29b0f896
AM
6763 {
6764 /* Does not match either direction. */
7b94647a 6765 specific_error = progress (i.error);
29b0f896
AM
6766 continue;
6767 }
38e314eb 6768 /* found_reverse_match holds which of D or FloatR
29b0f896 6769 we've found. */
38e314eb
JB
6770 if (!t->opcode_modifier.d)
6771 found_reverse_match = 0;
6772 else if (operand_types[0].bitfield.tbyte)
8a2ed489 6773 found_reverse_match = Opcode_FloatD;
8bd915b7
JB
6774 else if (t->opcode_modifier.vexsources)
6775 {
6776 found_reverse_match = Opcode_VexW;
6777 goto check_operands_345;
6778 }
2c735193
JB
6779 else if (t->opcode_modifier.opcodespace != SPACE_BASE
6780 && (t->opcode_modifier.opcodespace != SPACE_0F
6781 /* MOV to/from CR/DR/TR, as an exception, follow
6782 the base opcode space encoding model. */
6783 || (t->base_opcode | 7) != 0x27))
dbbc8b7e 6784 found_reverse_match = (t->base_opcode & 0xee) != 0x6e
2c735193 6785 ? Opcode_ExtD : Opcode_SIMD_IntD;
8a2ed489 6786 else
38e314eb 6787 found_reverse_match = Opcode_D;
40fb9820 6788 if (t->opcode_modifier.floatr)
8a2ed489 6789 found_reverse_match |= Opcode_FloatR;
29b0f896 6790 }
f48ff2ae 6791 else
29b0f896 6792 {
f48ff2ae 6793 /* Found a forward 2 operand match here. */
8bd915b7 6794 check_operands_345:
d1cbb4db
L
6795 switch (t->operands)
6796 {
c0f3af97 6797 case 5:
3d0738af 6798 overlap4 = operand_type_and (i.types[4], operand_types[4]);
c0f3af97 6799 if (!operand_type_match (overlap4, i.types[4])
dc821c5f 6800 || !operand_type_register_match (i.types[3],
c0f3af97 6801 operand_types[3],
c0f3af97
L
6802 i.types[4],
6803 operand_types[4]))
7b94647a
JB
6804 {
6805 specific_error = progress (i.error);
6806 continue;
6807 }
1a0670f3 6808 /* Fall through. */
f48ff2ae 6809 case 4:
3d0738af 6810 overlap3 = operand_type_and (i.types[3], operand_types[3]);
40fb9820 6811 if (!operand_type_match (overlap3, i.types[3])
e2195274
JB
6812 || ((check_register & 0xa) == 0xa
6813 && !operand_type_register_match (i.types[1],
f7768225
JB
6814 operand_types[1],
6815 i.types[3],
e2195274
JB
6816 operand_types[3]))
6817 || ((check_register & 0xc) == 0xc
6818 && !operand_type_register_match (i.types[2],
6819 operand_types[2],
6820 i.types[3],
6821 operand_types[3])))
7b94647a
JB
6822 {
6823 specific_error = progress (i.error);
6824 continue;
6825 }
1a0670f3 6826 /* Fall through. */
f48ff2ae 6827 case 3:
3d0738af 6828 overlap2 = operand_type_and (i.types[2], operand_types[2]);
40fb9820 6829 if (!operand_type_match (overlap2, i.types[2])
e2195274
JB
6830 || ((check_register & 5) == 5
6831 && !operand_type_register_match (i.types[0],
23e42951
JB
6832 operand_types[0],
6833 i.types[2],
e2195274
JB
6834 operand_types[2]))
6835 || ((check_register & 6) == 6
6836 && !operand_type_register_match (i.types[1],
6837 operand_types[1],
6838 i.types[2],
6839 operand_types[2])))
7b94647a
JB
6840 {
6841 specific_error = progress (i.error);
6842 continue;
6843 }
f48ff2ae
L
6844 break;
6845 }
29b0f896 6846 }
f48ff2ae 6847 /* Found either forward/reverse 2, 3 or 4 operand match here:
29b0f896
AM
6848 slip through to break. */
6849 }
c0f3af97 6850
9bb4d860
L
6851 /* Check if VEX/EVEX encoding requirements can be satisfied. */
6852 if (VEX_check_encoding (t))
da4977e0 6853 {
7b94647a 6854 specific_error = progress (i.error);
da4977e0
JB
6855 continue;
6856 }
6857
9bb4d860
L
6858 /* Check if vector operands are valid. */
6859 if (check_VecOperands (t))
5614d22c 6860 {
7b94647a 6861 specific_error = progress (i.error);
5614d22c
JB
6862 continue;
6863 }
a683cc34 6864
29b0f896
AM
6865 /* We've found a match; break out of loop. */
6866 break;
6867 }
6868
7b94647a
JB
6869#undef progress
6870
29b0f896
AM
6871 if (t == current_templates->end)
6872 {
6873 /* We found no match. */
a65babc9 6874 const char *err_msg;
7b94647a 6875 switch (specific_error)
a65babc9
L
6876 {
6877 default:
6878 abort ();
86e026a4 6879 case operand_size_mismatch:
a65babc9
L
6880 err_msg = _("operand size mismatch");
6881 break;
6882 case operand_type_mismatch:
6883 err_msg = _("operand type mismatch");
6884 break;
6885 case register_type_mismatch:
6886 err_msg = _("register type mismatch");
6887 break;
6888 case number_of_operands_mismatch:
6889 err_msg = _("number of operands mismatch");
6890 break;
6891 case invalid_instruction_suffix:
6892 err_msg = _("invalid instruction suffix");
6893 break;
6894 case bad_imm4:
4a2608e3 6895 err_msg = _("constant doesn't fit in 4 bits");
a65babc9 6896 break;
a65babc9
L
6897 case unsupported_with_intel_mnemonic:
6898 err_msg = _("unsupported with Intel mnemonic");
6899 break;
6900 case unsupported_syntax:
6901 err_msg = _("unsupported syntax");
6902 break;
6903 case unsupported:
35262a23 6904 as_bad (_("unsupported instruction `%s'"),
10efe3f6
L
6905 current_templates->start->name);
6906 return NULL;
260cd341
LC
6907 case invalid_sib_address:
6908 err_msg = _("invalid SIB address");
6909 break;
6c30d220
L
6910 case invalid_vsib_address:
6911 err_msg = _("invalid VSIB address");
6912 break;
7bab8ab5
JB
6913 case invalid_vector_register_set:
6914 err_msg = _("mask, index, and destination registers must be distinct");
6915 break;
260cd341
LC
6916 case invalid_tmm_register_set:
6917 err_msg = _("all tmm registers must be distinct");
6918 break;
0cc78721
CL
6919 case invalid_dest_and_src_register_set:
6920 err_msg = _("destination and source registers must be distinct");
6921 break;
6c30d220
L
6922 case unsupported_vector_index_register:
6923 err_msg = _("unsupported vector index register");
6924 break;
43234a1e
L
6925 case unsupported_broadcast:
6926 err_msg = _("unsupported broadcast");
6927 break;
43234a1e
L
6928 case broadcast_needed:
6929 err_msg = _("broadcast is needed for operand of such type");
6930 break;
6931 case unsupported_masking:
6932 err_msg = _("unsupported masking");
6933 break;
6934 case mask_not_on_destination:
6935 err_msg = _("mask not on destination operand");
6936 break;
6937 case no_default_mask:
6938 err_msg = _("default mask isn't allowed");
6939 break;
6940 case unsupported_rc_sae:
6941 err_msg = _("unsupported static rounding/sae");
6942 break;
43234a1e
L
6943 case invalid_register_operand:
6944 err_msg = _("invalid register operand");
6945 break;
a65babc9
L
6946 }
6947 as_bad (_("%s for `%s'"), err_msg,
891edac4 6948 current_templates->start->name);
fa99fab2 6949 return NULL;
29b0f896 6950 }
252b5132 6951
29b0f896
AM
6952 if (!quiet_warnings)
6953 {
6954 if (!intel_syntax
0cfa3eb3 6955 && (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE)))
6f2f06be 6956 as_warn (_("indirect %s without `*'"), t->name);
29b0f896 6957
40fb9820 6958 if (t->opcode_modifier.isprefix
3cd7f3e3 6959 && t->opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
6960 {
6961 /* Warn them that a data or address size prefix doesn't
6962 affect assembly of the next line of code. */
6963 as_warn (_("stand-alone `%s' prefix"), t->name);
6964 }
6965 }
6966
6967 /* Copy the template we found. */
9a182d04 6968 install_template (t);
539e75ad
L
6969
6970 if (addr_prefix_disp != -1)
6971 i.tm.operand_types[addr_prefix_disp]
6972 = operand_types[addr_prefix_disp];
6973
8bd915b7 6974 switch (found_reverse_match)
29b0f896 6975 {
8bd915b7
JB
6976 case 0:
6977 break;
6978
6979 default:
dfd69174
JB
6980 /* If we found a reverse match we must alter the opcode direction
6981 bit and clear/flip the regmem modifier one. found_reverse_match
6982 holds bits to change (different for int & float insns). */
29b0f896
AM
6983
6984 i.tm.base_opcode ^= found_reverse_match;
6985
f5eb1d70
JB
6986 i.tm.operand_types[0] = operand_types[i.operands - 1];
6987 i.tm.operand_types[i.operands - 1] = operand_types[0];
dfd69174
JB
6988
6989 /* Certain SIMD insns have their load forms specified in the opcode
6990 table, and hence we need to _set_ RegMem instead of clearing it.
6991 We need to avoid setting the bit though on insns like KMOVW. */
6992 i.tm.opcode_modifier.regmem
6993 = i.tm.opcode_modifier.modrm && i.tm.opcode_modifier.d
6994 && i.tm.operands > 2U - i.tm.opcode_modifier.sse2avx
6995 && !i.tm.opcode_modifier.regmem;
8bd915b7
JB
6996 break;
6997
6998 case Opcode_VexW:
6999 /* Only the first two register operands need reversing, alongside
7000 flipping VEX.W. */
7001 i.tm.opcode_modifier.vexw ^= VEXW0 ^ VEXW1;
7002
7003 j = i.tm.operand_types[0].bitfield.imm8;
7004 i.tm.operand_types[j] = operand_types[j + 1];
7005 i.tm.operand_types[j + 1] = operand_types[j];
7006 break;
29b0f896
AM
7007 }
7008
fa99fab2 7009 return t;
29b0f896
AM
7010}
7011
7012static int
e3bb37b5 7013check_string (void)
29b0f896 7014{
51c8edf6
JB
7015 unsigned int es_op = i.tm.opcode_modifier.isstring - IS_STRING_ES_OP0;
7016 unsigned int op = i.tm.operand_types[0].bitfield.baseindex ? es_op : 0;
8dc0818e 7017
5e042380 7018 if (i.seg[op] != NULL && i.seg[op] != reg_es)
29b0f896 7019 {
51c8edf6
JB
7020 as_bad (_("`%s' operand %u must use `%ses' segment"),
7021 i.tm.name,
7022 intel_syntax ? i.tm.operands - es_op : es_op + 1,
7023 register_prefix);
7024 return 0;
29b0f896 7025 }
51c8edf6
JB
7026
7027 /* There's only ever one segment override allowed per instruction.
7028 This instruction possibly has a legal segment override on the
7029 second operand, so copy the segment to where non-string
7030 instructions store it, allowing common code. */
7031 i.seg[op] = i.seg[1];
7032
29b0f896
AM
7033 return 1;
7034}
7035
7036static int
543613e9 7037process_suffix (void)
29b0f896 7038{
5b7c81bd 7039 bool is_crc32 = false, is_movx = false;
8b65b895 7040
29b0f896
AM
7041 /* If matched instruction specifies an explicit instruction mnemonic
7042 suffix, use it. */
673fe0f0 7043 if (i.tm.opcode_modifier.size == SIZE16)
40fb9820 7044 i.suffix = WORD_MNEM_SUFFIX;
673fe0f0 7045 else if (i.tm.opcode_modifier.size == SIZE32)
40fb9820 7046 i.suffix = LONG_MNEM_SUFFIX;
673fe0f0 7047 else if (i.tm.opcode_modifier.size == SIZE64)
40fb9820 7048 i.suffix = QWORD_MNEM_SUFFIX;
13e600d0 7049 else if (i.reg_operands
c8f8eebc
JB
7050 && (i.operands > 1 || i.types[0].bitfield.class == Reg)
7051 && !i.tm.opcode_modifier.addrprefixopreg)
29b0f896 7052 {
65fca059 7053 unsigned int numop = i.operands;
389d00a5
JB
7054
7055 /* MOVSX/MOVZX */
7056 is_movx = (i.tm.opcode_modifier.opcodespace == SPACE_0F
7057 && (i.tm.base_opcode | 8) == 0xbe)
7058 || (i.tm.opcode_modifier.opcodespace == SPACE_BASE
7059 && i.tm.base_opcode == 0x63
7060 && i.tm.cpu_flags.bitfield.cpu64);
7061
8b65b895 7062 /* CRC32 */
389d00a5
JB
7063 is_crc32 = (i.tm.base_opcode == 0xf0
7064 && i.tm.opcode_modifier.opcodespace == SPACE_0F38
8b65b895 7065 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2);
65fca059
JB
7066
7067 /* movsx/movzx want only their source operand considered here, for the
7068 ambiguity checking below. The suffix will be replaced afterwards
7069 to represent the destination (register). */
389d00a5 7070 if (is_movx && (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63))
65fca059
JB
7071 --i.operands;
7072
643bb870 7073 /* crc32 needs REX.W set regardless of suffix / source operand size. */
8b65b895 7074 if (is_crc32 && i.tm.operand_types[1].bitfield.qword)
643bb870
JB
7075 i.rex |= REX_W;
7076
29b0f896 7077 /* If there's no instruction mnemonic suffix we try to invent one
13e600d0 7078 based on GPR operands. */
29b0f896
AM
7079 if (!i.suffix)
7080 {
7081 /* We take i.suffix from the last register operand specified,
7082 Destination register type is more significant than source
381d071f
L
7083 register type. crc32 in SSE4.2 prefers source register
7084 type. */
8b65b895 7085 unsigned int op = is_crc32 ? 1 : i.operands;
20592a94 7086
1a035124
JB
7087 while (op--)
7088 if (i.tm.operand_types[op].bitfield.instance == InstanceNone
7089 || i.tm.operand_types[op].bitfield.instance == Accum)
7090 {
7091 if (i.types[op].bitfield.class != Reg)
7092 continue;
7093 if (i.types[op].bitfield.byte)
7094 i.suffix = BYTE_MNEM_SUFFIX;
7095 else if (i.types[op].bitfield.word)
7096 i.suffix = WORD_MNEM_SUFFIX;
7097 else if (i.types[op].bitfield.dword)
7098 i.suffix = LONG_MNEM_SUFFIX;
7099 else if (i.types[op].bitfield.qword)
7100 i.suffix = QWORD_MNEM_SUFFIX;
7101 else
7102 continue;
7103 break;
7104 }
65fca059
JB
7105
7106 /* As an exception, movsx/movzx silently default to a byte source
7107 in AT&T mode. */
389d00a5 7108 if (is_movx && i.tm.opcode_modifier.w && !i.suffix && !intel_syntax)
65fca059 7109 i.suffix = BYTE_MNEM_SUFFIX;
29b0f896
AM
7110 }
7111 else if (i.suffix == BYTE_MNEM_SUFFIX)
7112 {
1cb0ab18 7113 if (!check_byte_reg ())
29b0f896
AM
7114 return 0;
7115 }
7116 else if (i.suffix == LONG_MNEM_SUFFIX)
7117 {
1cb0ab18 7118 if (!check_long_reg ())
29b0f896
AM
7119 return 0;
7120 }
7121 else if (i.suffix == QWORD_MNEM_SUFFIX)
7122 {
1cb0ab18 7123 if (!check_qword_reg ())
29b0f896
AM
7124 return 0;
7125 }
7126 else if (i.suffix == WORD_MNEM_SUFFIX)
7127 {
1cb0ab18 7128 if (!check_word_reg ())
29b0f896
AM
7129 return 0;
7130 }
3cd7f3e3
L
7131 else if (intel_syntax
7132 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
7133 /* Do nothing if the instruction is going to ignore the prefix. */
7134 ;
7135 else
7136 abort ();
65fca059
JB
7137
7138 /* Undo the movsx/movzx change done above. */
7139 i.operands = numop;
29b0f896 7140 }
3cd7f3e3
L
7141 else if (i.tm.opcode_modifier.mnemonicsize == DEFAULTSIZE
7142 && !i.suffix)
29b0f896 7143 {
13e600d0
JB
7144 i.suffix = stackop_size;
7145 if (stackop_size == LONG_MNEM_SUFFIX)
06f74c5c
L
7146 {
7147 /* stackop_size is set to LONG_MNEM_SUFFIX for the
7148 .code16gcc directive to support 16-bit mode with
7149 32-bit address. For IRET without a suffix, generate
7150 16-bit IRET (opcode 0xcf) to return from an interrupt
7151 handler. */
13e600d0
JB
7152 if (i.tm.base_opcode == 0xcf)
7153 {
7154 i.suffix = WORD_MNEM_SUFFIX;
7155 as_warn (_("generating 16-bit `iret' for .code16gcc directive"));
7156 }
7157 /* Warn about changed behavior for segment register push/pop. */
7158 else if ((i.tm.base_opcode | 1) == 0x07)
7159 as_warn (_("generating 32-bit `%s', unlike earlier gas versions"),
7160 i.tm.name);
06f74c5c 7161 }
29b0f896 7162 }
c006a730 7163 else if (!i.suffix
0cfa3eb3
JB
7164 && (i.tm.opcode_modifier.jump == JUMP_ABSOLUTE
7165 || i.tm.opcode_modifier.jump == JUMP_BYTE
7166 || i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT
389d00a5
JB
7167 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
7168 && i.tm.base_opcode == 0x01 /* [ls][gi]dt */
64e74474 7169 && i.tm.extension_opcode <= 3)))
9306ca4a
JB
7170 {
7171 switch (flag_code)
7172 {
7173 case CODE_64BIT:
40fb9820 7174 if (!i.tm.opcode_modifier.no_qsuf)
9306ca4a 7175 {
828c2a25
JB
7176 if (i.tm.opcode_modifier.jump == JUMP_BYTE
7177 || i.tm.opcode_modifier.no_lsuf)
7178 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a
JB
7179 break;
7180 }
1a0670f3 7181 /* Fall through. */
9306ca4a 7182 case CODE_32BIT:
40fb9820 7183 if (!i.tm.opcode_modifier.no_lsuf)
9306ca4a
JB
7184 i.suffix = LONG_MNEM_SUFFIX;
7185 break;
7186 case CODE_16BIT:
40fb9820 7187 if (!i.tm.opcode_modifier.no_wsuf)
9306ca4a
JB
7188 i.suffix = WORD_MNEM_SUFFIX;
7189 break;
7190 }
7191 }
252b5132 7192
c006a730 7193 if (!i.suffix
3cd7f3e3 7194 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8
JB
7195 /* Also cover lret/retf/iret in 64-bit mode. */
7196 || (flag_code == CODE_64BIT
7197 && !i.tm.opcode_modifier.no_lsuf
7198 && !i.tm.opcode_modifier.no_qsuf))
3cd7f3e3 7199 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
8bbb3ad8
JB
7200 /* Explicit sizing prefixes are assumed to disambiguate insns. */
7201 && !i.prefix[DATA_PREFIX] && !(i.prefix[REX_PREFIX] & REX_W)
62b3f548
JB
7202 /* Accept FLDENV et al without suffix. */
7203 && (i.tm.opcode_modifier.no_ssuf || i.tm.opcode_modifier.floatmf))
29b0f896 7204 {
6c0946d0 7205 unsigned int suffixes, evex = 0;
c006a730
JB
7206
7207 suffixes = !i.tm.opcode_modifier.no_bsuf;
7208 if (!i.tm.opcode_modifier.no_wsuf)
7209 suffixes |= 1 << 1;
7210 if (!i.tm.opcode_modifier.no_lsuf)
7211 suffixes |= 1 << 2;
7212 if (!i.tm.opcode_modifier.no_ldsuf)
7213 suffixes |= 1 << 3;
7214 if (!i.tm.opcode_modifier.no_ssuf)
7215 suffixes |= 1 << 4;
7216 if (flag_code == CODE_64BIT && !i.tm.opcode_modifier.no_qsuf)
7217 suffixes |= 1 << 5;
7218
6c0946d0
JB
7219 /* For [XYZ]MMWORD operands inspect operand sizes. While generally
7220 also suitable for AT&T syntax mode, it was requested that this be
7221 restricted to just Intel syntax. */
a5748e0d
JB
7222 if (intel_syntax && is_any_vex_encoding (&i.tm)
7223 && !i.broadcast.type && !i.broadcast.bytes)
6c0946d0 7224 {
b9915cbc 7225 unsigned int op;
6c0946d0 7226
b9915cbc 7227 for (op = 0; op < i.tm.operands; ++op)
6c0946d0 7228 {
b9915cbc
JB
7229 if (is_evex_encoding (&i.tm)
7230 && !cpu_arch_flags.bitfield.cpuavx512vl)
6c0946d0 7231 {
b9915cbc
JB
7232 if (i.tm.operand_types[op].bitfield.ymmword)
7233 i.tm.operand_types[op].bitfield.xmmword = 0;
7234 if (i.tm.operand_types[op].bitfield.zmmword)
7235 i.tm.operand_types[op].bitfield.ymmword = 0;
7236 if (!i.tm.opcode_modifier.evex
7237 || i.tm.opcode_modifier.evex == EVEXDYN)
7238 i.tm.opcode_modifier.evex = EVEX512;
7239 }
6c0946d0 7240
b9915cbc
JB
7241 if (i.tm.operand_types[op].bitfield.xmmword
7242 + i.tm.operand_types[op].bitfield.ymmword
7243 + i.tm.operand_types[op].bitfield.zmmword < 2)
7244 continue;
6c0946d0 7245
b9915cbc
JB
7246 /* Any properly sized operand disambiguates the insn. */
7247 if (i.types[op].bitfield.xmmword
7248 || i.types[op].bitfield.ymmword
7249 || i.types[op].bitfield.zmmword)
7250 {
7251 suffixes &= ~(7 << 6);
7252 evex = 0;
7253 break;
7254 }
6c0946d0 7255
b9915cbc
JB
7256 if ((i.flags[op] & Operand_Mem)
7257 && i.tm.operand_types[op].bitfield.unspecified)
7258 {
7259 if (i.tm.operand_types[op].bitfield.xmmword)
7260 suffixes |= 1 << 6;
7261 if (i.tm.operand_types[op].bitfield.ymmword)
7262 suffixes |= 1 << 7;
7263 if (i.tm.operand_types[op].bitfield.zmmword)
7264 suffixes |= 1 << 8;
7265 if (is_evex_encoding (&i.tm))
7266 evex = EVEX512;
6c0946d0
JB
7267 }
7268 }
7269 }
7270
7271 /* Are multiple suffixes / operand sizes allowed? */
c006a730 7272 if (suffixes & (suffixes - 1))
9306ca4a 7273 {
873494c8 7274 if (intel_syntax
3cd7f3e3 7275 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8 7276 || operand_check == check_error))
9306ca4a 7277 {
c006a730 7278 as_bad (_("ambiguous operand size for `%s'"), i.tm.name);
9306ca4a
JB
7279 return 0;
7280 }
c006a730 7281 if (operand_check == check_error)
9306ca4a 7282 {
c006a730
JB
7283 as_bad (_("no instruction mnemonic suffix given and "
7284 "no register operands; can't size `%s'"), i.tm.name);
9306ca4a
JB
7285 return 0;
7286 }
c006a730 7287 if (operand_check == check_warning)
873494c8
JB
7288 as_warn (_("%s; using default for `%s'"),
7289 intel_syntax
7290 ? _("ambiguous operand size")
7291 : _("no instruction mnemonic suffix given and "
7292 "no register operands"),
7293 i.tm.name);
c006a730
JB
7294
7295 if (i.tm.opcode_modifier.floatmf)
7296 i.suffix = SHORT_MNEM_SUFFIX;
389d00a5 7297 else if (is_movx)
65fca059 7298 /* handled below */;
6c0946d0
JB
7299 else if (evex)
7300 i.tm.opcode_modifier.evex = evex;
c006a730
JB
7301 else if (flag_code == CODE_16BIT)
7302 i.suffix = WORD_MNEM_SUFFIX;
1a035124 7303 else if (!i.tm.opcode_modifier.no_lsuf)
c006a730 7304 i.suffix = LONG_MNEM_SUFFIX;
1a035124
JB
7305 else
7306 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a 7307 }
29b0f896 7308 }
252b5132 7309
389d00a5 7310 if (is_movx)
65fca059
JB
7311 {
7312 /* In Intel syntax, movsx/movzx must have a "suffix" (checked above).
7313 In AT&T syntax, if there is no suffix (warned about above), the default
7314 will be byte extension. */
7315 if (i.tm.opcode_modifier.w && i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
7316 i.tm.base_opcode |= 1;
7317
7318 /* For further processing, the suffix should represent the destination
7319 (register). This is already the case when one was used with
7320 mov[sz][bw]*, but we need to replace it for mov[sz]x, or if there was
7321 no suffix to begin with. */
7322 if (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63 || !i.suffix)
7323 {
7324 if (i.types[1].bitfield.word)
7325 i.suffix = WORD_MNEM_SUFFIX;
7326 else if (i.types[1].bitfield.qword)
7327 i.suffix = QWORD_MNEM_SUFFIX;
7328 else
7329 i.suffix = LONG_MNEM_SUFFIX;
7330
7331 i.tm.opcode_modifier.w = 0;
7332 }
7333 }
7334
50128d0c
JB
7335 if (!i.tm.opcode_modifier.modrm && i.reg_operands && i.tm.operands < 3)
7336 i.short_form = (i.tm.operand_types[0].bitfield.class == Reg)
7337 != (i.tm.operand_types[1].bitfield.class == Reg);
7338
d2224064
JB
7339 /* Change the opcode based on the operand size given by i.suffix. */
7340 switch (i.suffix)
29b0f896 7341 {
d2224064
JB
7342 /* Size floating point instruction. */
7343 case LONG_MNEM_SUFFIX:
7344 if (i.tm.opcode_modifier.floatmf)
7345 {
7346 i.tm.base_opcode ^= 4;
7347 break;
7348 }
7349 /* fall through */
7350 case WORD_MNEM_SUFFIX:
7351 case QWORD_MNEM_SUFFIX:
29b0f896 7352 /* It's not a byte, select word/dword operation. */
40fb9820 7353 if (i.tm.opcode_modifier.w)
29b0f896 7354 {
50128d0c 7355 if (i.short_form)
29b0f896
AM
7356 i.tm.base_opcode |= 8;
7357 else
7358 i.tm.base_opcode |= 1;
7359 }
d2224064
JB
7360 /* fall through */
7361 case SHORT_MNEM_SUFFIX:
29b0f896
AM
7362 /* Now select between word & dword operations via the operand
7363 size prefix, except for instructions that will ignore this
7364 prefix anyway. */
c8f8eebc 7365 if (i.suffix != QWORD_MNEM_SUFFIX
3cd7f3e3 7366 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
c8f8eebc
JB
7367 && !i.tm.opcode_modifier.floatmf
7368 && !is_any_vex_encoding (&i.tm)
7369 && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
7370 || (flag_code == CODE_64BIT
7371 && i.tm.opcode_modifier.jump == JUMP_BYTE)))
24eab124
AM
7372 {
7373 unsigned int prefix = DATA_PREFIX_OPCODE;
543613e9 7374
0cfa3eb3 7375 if (i.tm.opcode_modifier.jump == JUMP_BYTE) /* jcxz, loop */
29b0f896 7376 prefix = ADDR_PREFIX_OPCODE;
252b5132 7377
29b0f896
AM
7378 if (!add_prefix (prefix))
7379 return 0;
24eab124 7380 }
252b5132 7381
29b0f896
AM
7382 /* Set mode64 for an operand. */
7383 if (i.suffix == QWORD_MNEM_SUFFIX
9146926a 7384 && flag_code == CODE_64BIT
d2224064 7385 && !i.tm.opcode_modifier.norex64
4ed21b58 7386 && !i.tm.opcode_modifier.vexw
46e883c5 7387 /* Special case for xchg %rax,%rax. It is NOP and doesn't
d2224064
JB
7388 need rex64. */
7389 && ! (i.operands == 2
7390 && i.tm.base_opcode == 0x90
7391 && i.tm.extension_opcode == None
75e5731b
JB
7392 && i.types[0].bitfield.instance == Accum
7393 && i.types[0].bitfield.qword
7394 && i.types[1].bitfield.instance == Accum
7395 && i.types[1].bitfield.qword))
d2224064 7396 i.rex |= REX_W;
3e73aa7c 7397
d2224064 7398 break;
8bbb3ad8
JB
7399
7400 case 0:
f9a6a8f0 7401 /* Select word/dword/qword operation with explicit data sizing prefix
8bbb3ad8
JB
7402 when there are no suitable register operands. */
7403 if (i.tm.opcode_modifier.w
7404 && (i.prefix[DATA_PREFIX] || (i.prefix[REX_PREFIX] & REX_W))
7405 && (!i.reg_operands
7406 || (i.reg_operands == 1
7407 /* ShiftCount */
7408 && (i.tm.operand_types[0].bitfield.instance == RegC
7409 /* InOutPortReg */
7410 || i.tm.operand_types[0].bitfield.instance == RegD
7411 || i.tm.operand_types[1].bitfield.instance == RegD
7412 /* CRC32 */
8b65b895 7413 || is_crc32))))
8bbb3ad8
JB
7414 i.tm.base_opcode |= 1;
7415 break;
29b0f896 7416 }
7ecd2f8b 7417
c8f8eebc 7418 if (i.tm.opcode_modifier.addrprefixopreg)
c0a30a9f 7419 {
c8f8eebc
JB
7420 gas_assert (!i.suffix);
7421 gas_assert (i.reg_operands);
c0a30a9f 7422
c8f8eebc
JB
7423 if (i.tm.operand_types[0].bitfield.instance == Accum
7424 || i.operands == 1)
7425 {
7426 /* The address size override prefix changes the size of the
7427 first operand. */
7428 if (flag_code == CODE_64BIT
7429 && i.op[0].regs->reg_type.bitfield.word)
7430 {
7431 as_bad (_("16-bit addressing unavailable for `%s'"),
7432 i.tm.name);
7433 return 0;
7434 }
7435
7436 if ((flag_code == CODE_32BIT
7437 ? i.op[0].regs->reg_type.bitfield.word
7438 : i.op[0].regs->reg_type.bitfield.dword)
7439 && !add_prefix (ADDR_PREFIX_OPCODE))
7440 return 0;
7441 }
c0a30a9f
L
7442 else
7443 {
c8f8eebc
JB
7444 /* Check invalid register operand when the address size override
7445 prefix changes the size of register operands. */
7446 unsigned int op;
7447 enum { need_word, need_dword, need_qword } need;
7448
27f13469 7449 /* Check the register operand for the address size prefix if
b3a3496f 7450 the memory operand has no real registers, like symbol, DISP
829f3fe1 7451 or bogus (x32-only) symbol(%rip) when symbol(%eip) is meant. */
27f13469
L
7452 if (i.mem_operands == 1
7453 && i.reg_operands == 1
7454 && i.operands == 2
27f13469 7455 && i.types[1].bitfield.class == Reg
b3a3496f
L
7456 && (flag_code == CODE_32BIT
7457 ? i.op[1].regs->reg_type.bitfield.word
7458 : i.op[1].regs->reg_type.bitfield.dword)
7459 && ((i.base_reg == NULL && i.index_reg == NULL)
829f3fe1
JB
7460#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
7461 || (x86_elf_abi == X86_64_X32_ABI
7462 && i.base_reg
b3a3496f
L
7463 && i.base_reg->reg_num == RegIP
7464 && i.base_reg->reg_type.bitfield.qword))
829f3fe1
JB
7465#else
7466 || 0)
7467#endif
27f13469
L
7468 && !add_prefix (ADDR_PREFIX_OPCODE))
7469 return 0;
7470
c8f8eebc
JB
7471 if (flag_code == CODE_32BIT)
7472 need = i.prefix[ADDR_PREFIX] ? need_word : need_dword;
7473 else if (i.prefix[ADDR_PREFIX])
c0a30a9f
L
7474 need = need_dword;
7475 else
7476 need = flag_code == CODE_64BIT ? need_qword : need_word;
c0a30a9f 7477
c8f8eebc
JB
7478 for (op = 0; op < i.operands; op++)
7479 {
7480 if (i.types[op].bitfield.class != Reg)
7481 continue;
7482
7483 switch (need)
7484 {
7485 case need_word:
7486 if (i.op[op].regs->reg_type.bitfield.word)
7487 continue;
7488 break;
7489 case need_dword:
7490 if (i.op[op].regs->reg_type.bitfield.dword)
7491 continue;
7492 break;
7493 case need_qword:
7494 if (i.op[op].regs->reg_type.bitfield.qword)
7495 continue;
7496 break;
7497 }
7498
7499 as_bad (_("invalid register operand size for `%s'"),
7500 i.tm.name);
7501 return 0;
7502 }
7503 }
c0a30a9f
L
7504 }
7505
29b0f896
AM
7506 return 1;
7507}
3e73aa7c 7508
29b0f896 7509static int
543613e9 7510check_byte_reg (void)
29b0f896
AM
7511{
7512 int op;
543613e9 7513
29b0f896
AM
7514 for (op = i.operands; --op >= 0;)
7515 {
dc821c5f 7516 /* Skip non-register operands. */
bab6aec1 7517 if (i.types[op].bitfield.class != Reg)
dc821c5f
JB
7518 continue;
7519
29b0f896
AM
7520 /* If this is an eight bit register, it's OK. If it's the 16 or
7521 32 bit version of an eight bit register, we will just use the
7522 low portion, and that's OK too. */
dc821c5f 7523 if (i.types[op].bitfield.byte)
29b0f896
AM
7524 continue;
7525
5a819eb9 7526 /* I/O port address operands are OK too. */
75e5731b
JB
7527 if (i.tm.operand_types[op].bitfield.instance == RegD
7528 && i.tm.operand_types[op].bitfield.word)
5a819eb9
JB
7529 continue;
7530
9706160a 7531 /* crc32 only wants its source operand checked here. */
389d00a5
JB
7532 if (i.tm.base_opcode == 0xf0
7533 && i.tm.opcode_modifier.opcodespace == SPACE_0F38
8b65b895
L
7534 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2
7535 && op != 0)
9344ff29
L
7536 continue;
7537
29b0f896 7538 /* Any other register is bad. */
73c76375
JB
7539 as_bad (_("`%s%s' not allowed with `%s%c'"),
7540 register_prefix, i.op[op].regs->reg_name,
7541 i.tm.name, i.suffix);
7542 return 0;
29b0f896
AM
7543 }
7544 return 1;
7545}
7546
7547static int
e3bb37b5 7548check_long_reg (void)
29b0f896
AM
7549{
7550 int op;
7551
7552 for (op = i.operands; --op >= 0;)
dc821c5f 7553 /* Skip non-register operands. */
bab6aec1 7554 if (i.types[op].bitfield.class != Reg)
dc821c5f 7555 continue;
29b0f896
AM
7556 /* Reject eight bit registers, except where the template requires
7557 them. (eg. movzb) */
dc821c5f 7558 else if (i.types[op].bitfield.byte
bab6aec1 7559 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7560 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7561 && (i.tm.operand_types[op].bitfield.word
7562 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7563 {
a540244d
L
7564 as_bad (_("`%s%s' not allowed with `%s%c'"),
7565 register_prefix,
29b0f896
AM
7566 i.op[op].regs->reg_name,
7567 i.tm.name,
7568 i.suffix);
7569 return 0;
7570 }
be4c5e58
L
7571 /* Error if the e prefix on a general reg is missing. */
7572 else if (i.types[op].bitfield.word
bab6aec1 7573 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7574 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7575 && i.tm.operand_types[op].bitfield.dword)
29b0f896 7576 {
be4c5e58
L
7577 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7578 register_prefix, i.op[op].regs->reg_name,
7579 i.suffix);
7580 return 0;
252b5132 7581 }
e4630f71 7582 /* Warn if the r prefix on a general reg is present. */
dc821c5f 7583 else if (i.types[op].bitfield.qword
bab6aec1 7584 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7585 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7586 && i.tm.operand_types[op].bitfield.dword)
252b5132 7587 {
1cb0ab18
JB
7588 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7589 register_prefix, i.op[op].regs->reg_name, i.suffix);
7590 return 0;
29b0f896
AM
7591 }
7592 return 1;
7593}
252b5132 7594
29b0f896 7595static int
e3bb37b5 7596check_qword_reg (void)
29b0f896
AM
7597{
7598 int op;
252b5132 7599
29b0f896 7600 for (op = i.operands; --op >= 0; )
dc821c5f 7601 /* Skip non-register operands. */
bab6aec1 7602 if (i.types[op].bitfield.class != Reg)
dc821c5f 7603 continue;
29b0f896
AM
7604 /* Reject eight bit registers, except where the template requires
7605 them. (eg. movzb) */
dc821c5f 7606 else if (i.types[op].bitfield.byte
bab6aec1 7607 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7608 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7609 && (i.tm.operand_types[op].bitfield.word
7610 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7611 {
a540244d
L
7612 as_bad (_("`%s%s' not allowed with `%s%c'"),
7613 register_prefix,
29b0f896
AM
7614 i.op[op].regs->reg_name,
7615 i.tm.name,
7616 i.suffix);
7617 return 0;
7618 }
e4630f71 7619 /* Warn if the r prefix on a general reg is missing. */
dc821c5f
JB
7620 else if ((i.types[op].bitfield.word
7621 || i.types[op].bitfield.dword)
bab6aec1 7622 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7623 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7624 && i.tm.operand_types[op].bitfield.qword)
29b0f896
AM
7625 {
7626 /* Prohibit these changes in the 64bit mode, since the
7627 lowering is more complicated. */
1cb0ab18
JB
7628 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7629 register_prefix, i.op[op].regs->reg_name, i.suffix);
7630 return 0;
252b5132 7631 }
29b0f896
AM
7632 return 1;
7633}
252b5132 7634
29b0f896 7635static int
e3bb37b5 7636check_word_reg (void)
29b0f896
AM
7637{
7638 int op;
7639 for (op = i.operands; --op >= 0;)
dc821c5f 7640 /* Skip non-register operands. */
bab6aec1 7641 if (i.types[op].bitfield.class != Reg)
dc821c5f 7642 continue;
29b0f896
AM
7643 /* Reject eight bit registers, except where the template requires
7644 them. (eg. movzb) */
dc821c5f 7645 else if (i.types[op].bitfield.byte
bab6aec1 7646 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7647 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7648 && (i.tm.operand_types[op].bitfield.word
7649 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7650 {
a540244d
L
7651 as_bad (_("`%s%s' not allowed with `%s%c'"),
7652 register_prefix,
29b0f896
AM
7653 i.op[op].regs->reg_name,
7654 i.tm.name,
7655 i.suffix);
7656 return 0;
7657 }
9706160a
JB
7658 /* Error if the e or r prefix on a general reg is present. */
7659 else if ((i.types[op].bitfield.dword
dc821c5f 7660 || i.types[op].bitfield.qword)
bab6aec1 7661 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7662 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7663 && i.tm.operand_types[op].bitfield.word)
252b5132 7664 {
9706160a
JB
7665 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7666 register_prefix, i.op[op].regs->reg_name,
7667 i.suffix);
7668 return 0;
29b0f896
AM
7669 }
7670 return 1;
7671}
252b5132 7672
29b0f896 7673static int
40fb9820 7674update_imm (unsigned int j)
29b0f896 7675{
bc0844ae 7676 i386_operand_type overlap = i.types[j];
be1643ff
JB
7677 if (overlap.bitfield.imm8
7678 + overlap.bitfield.imm8s
7679 + overlap.bitfield.imm16
7680 + overlap.bitfield.imm32
7681 + overlap.bitfield.imm32s
7682 + overlap.bitfield.imm64 > 1)
29b0f896
AM
7683 {
7684 if (i.suffix)
7685 {
40fb9820
L
7686 i386_operand_type temp;
7687
0dfbf9d7 7688 operand_type_set (&temp, 0);
7ab9ffdd 7689 if (i.suffix == BYTE_MNEM_SUFFIX)
40fb9820
L
7690 {
7691 temp.bitfield.imm8 = overlap.bitfield.imm8;
7692 temp.bitfield.imm8s = overlap.bitfield.imm8s;
7693 }
7694 else if (i.suffix == WORD_MNEM_SUFFIX)
7695 temp.bitfield.imm16 = overlap.bitfield.imm16;
7696 else if (i.suffix == QWORD_MNEM_SUFFIX)
7697 {
7698 temp.bitfield.imm64 = overlap.bitfield.imm64;
7699 temp.bitfield.imm32s = overlap.bitfield.imm32s;
7700 }
7701 else
7702 temp.bitfield.imm32 = overlap.bitfield.imm32;
7703 overlap = temp;
29b0f896 7704 }
0dfbf9d7
L
7705 else if (operand_type_equal (&overlap, &imm16_32_32s)
7706 || operand_type_equal (&overlap, &imm16_32)
7707 || operand_type_equal (&overlap, &imm16_32s))
29b0f896 7708 {
40fb9820 7709 if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
65da13b5 7710 overlap = imm16;
40fb9820 7711 else
65da13b5 7712 overlap = imm32s;
29b0f896 7713 }
8bbb3ad8
JB
7714 else if (i.prefix[REX_PREFIX] & REX_W)
7715 overlap = operand_type_and (overlap, imm32s);
7716 else if (i.prefix[DATA_PREFIX])
7717 overlap = operand_type_and (overlap,
7718 flag_code != CODE_16BIT ? imm16 : imm32);
be1643ff
JB
7719 if (overlap.bitfield.imm8
7720 + overlap.bitfield.imm8s
7721 + overlap.bitfield.imm16
7722 + overlap.bitfield.imm32
7723 + overlap.bitfield.imm32s
7724 + overlap.bitfield.imm64 != 1)
29b0f896 7725 {
4eed87de
AM
7726 as_bad (_("no instruction mnemonic suffix given; "
7727 "can't determine immediate size"));
29b0f896
AM
7728 return 0;
7729 }
7730 }
40fb9820 7731 i.types[j] = overlap;
29b0f896 7732
40fb9820
L
7733 return 1;
7734}
7735
7736static int
7737finalize_imm (void)
7738{
bc0844ae 7739 unsigned int j, n;
29b0f896 7740
bc0844ae
L
7741 /* Update the first 2 immediate operands. */
7742 n = i.operands > 2 ? 2 : i.operands;
7743 if (n)
7744 {
7745 for (j = 0; j < n; j++)
7746 if (update_imm (j) == 0)
7747 return 0;
40fb9820 7748
bc0844ae
L
7749 /* The 3rd operand can't be immediate operand. */
7750 gas_assert (operand_type_check (i.types[2], imm) == 0);
7751 }
29b0f896
AM
7752
7753 return 1;
7754}
7755
7756static int
e3bb37b5 7757process_operands (void)
29b0f896
AM
7758{
7759 /* Default segment register this instruction will use for memory
7760 accesses. 0 means unknown. This is only for optimizing out
7761 unnecessary segment overrides. */
5e042380 7762 const reg_entry *default_seg = NULL;
29b0f896 7763
a5aeccd9
JB
7764 if (i.tm.opcode_modifier.sse2avx)
7765 {
7766 /* Legacy encoded insns allow explicit REX prefixes, so these prefixes
7767 need converting. */
7768 i.rex |= i.prefix[REX_PREFIX] & (REX_W | REX_R | REX_X | REX_B);
7769 i.prefix[REX_PREFIX] = 0;
7770 i.rex_encoding = 0;
7771 }
c423d21a
JB
7772 /* ImmExt should be processed after SSE2AVX. */
7773 else if (i.tm.opcode_modifier.immext)
7774 process_immext ();
a5aeccd9 7775
2426c15f 7776 if (i.tm.opcode_modifier.sse2avx && i.tm.opcode_modifier.vexvvvv)
29b0f896 7777 {
91d6fa6a
NC
7778 unsigned int dupl = i.operands;
7779 unsigned int dest = dupl - 1;
9fcfb3d7
L
7780 unsigned int j;
7781
c0f3af97 7782 /* The destination must be an xmm register. */
9c2799c2 7783 gas_assert (i.reg_operands
91d6fa6a 7784 && MAX_OPERANDS > dupl
7ab9ffdd 7785 && operand_type_equal (&i.types[dest], &regxmm));
c0f3af97 7786
75e5731b 7787 if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 7788 && i.tm.operand_types[0].bitfield.xmmword)
e2ec9d29 7789 {
8cd7925b 7790 if (i.tm.opcode_modifier.vexsources == VEX3SOURCES)
c0f3af97
L
7791 {
7792 /* Keep xmm0 for instructions with VEX prefix and 3
7793 sources. */
75e5731b 7794 i.tm.operand_types[0].bitfield.instance = InstanceNone;
3528c362 7795 i.tm.operand_types[0].bitfield.class = RegSIMD;
c0f3af97
L
7796 goto duplicate;
7797 }
e2ec9d29 7798 else
c0f3af97
L
7799 {
7800 /* We remove the first xmm0 and keep the number of
7801 operands unchanged, which in fact duplicates the
7802 destination. */
7803 for (j = 1; j < i.operands; j++)
7804 {
7805 i.op[j - 1] = i.op[j];
7806 i.types[j - 1] = i.types[j];
7807 i.tm.operand_types[j - 1] = i.tm.operand_types[j];
8dc0818e 7808 i.flags[j - 1] = i.flags[j];
c0f3af97
L
7809 }
7810 }
7811 }
7812 else if (i.tm.opcode_modifier.implicit1stxmm0)
7ab9ffdd 7813 {
91d6fa6a 7814 gas_assert ((MAX_OPERANDS - 1) > dupl
8cd7925b
L
7815 && (i.tm.opcode_modifier.vexsources
7816 == VEX3SOURCES));
c0f3af97
L
7817
7818 /* Add the implicit xmm0 for instructions with VEX prefix
7819 and 3 sources. */
7820 for (j = i.operands; j > 0; j--)
7821 {
7822 i.op[j] = i.op[j - 1];
7823 i.types[j] = i.types[j - 1];
7824 i.tm.operand_types[j] = i.tm.operand_types[j - 1];
8dc0818e 7825 i.flags[j] = i.flags[j - 1];
c0f3af97
L
7826 }
7827 i.op[0].regs
629310ab 7828 = (const reg_entry *) str_hash_find (reg_hash, "xmm0");
7ab9ffdd 7829 i.types[0] = regxmm;
c0f3af97
L
7830 i.tm.operand_types[0] = regxmm;
7831
7832 i.operands += 2;
7833 i.reg_operands += 2;
7834 i.tm.operands += 2;
7835
91d6fa6a 7836 dupl++;
c0f3af97 7837 dest++;
91d6fa6a
NC
7838 i.op[dupl] = i.op[dest];
7839 i.types[dupl] = i.types[dest];
7840 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 7841 i.flags[dupl] = i.flags[dest];
e2ec9d29 7842 }
c0f3af97
L
7843 else
7844 {
dc1e8a47 7845 duplicate:
c0f3af97
L
7846 i.operands++;
7847 i.reg_operands++;
7848 i.tm.operands++;
7849
91d6fa6a
NC
7850 i.op[dupl] = i.op[dest];
7851 i.types[dupl] = i.types[dest];
7852 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 7853 i.flags[dupl] = i.flags[dest];
c0f3af97
L
7854 }
7855
7856 if (i.tm.opcode_modifier.immext)
7857 process_immext ();
7858 }
75e5731b 7859 else if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 7860 && i.tm.operand_types[0].bitfield.xmmword)
c0f3af97
L
7861 {
7862 unsigned int j;
7863
9fcfb3d7
L
7864 for (j = 1; j < i.operands; j++)
7865 {
7866 i.op[j - 1] = i.op[j];
7867 i.types[j - 1] = i.types[j];
7868
7869 /* We need to adjust fields in i.tm since they are used by
7870 build_modrm_byte. */
7871 i.tm.operand_types [j - 1] = i.tm.operand_types [j];
8dc0818e
JB
7872
7873 i.flags[j - 1] = i.flags[j];
9fcfb3d7
L
7874 }
7875
e2ec9d29
L
7876 i.operands--;
7877 i.reg_operands--;
e2ec9d29
L
7878 i.tm.operands--;
7879 }
920d2ddc
IT
7880 else if (i.tm.opcode_modifier.implicitquadgroup)
7881 {
a477a8c4
JB
7882 unsigned int regnum, first_reg_in_group, last_reg_in_group;
7883
920d2ddc 7884 /* The second operand must be {x,y,z}mmN, where N is a multiple of 4. */
3528c362 7885 gas_assert (i.operands >= 2 && i.types[1].bitfield.class == RegSIMD);
a477a8c4
JB
7886 regnum = register_number (i.op[1].regs);
7887 first_reg_in_group = regnum & ~3;
7888 last_reg_in_group = first_reg_in_group + 3;
7889 if (regnum != first_reg_in_group)
7890 as_warn (_("source register `%s%s' implicitly denotes"
7891 " `%s%.3s%u' to `%s%.3s%u' source group in `%s'"),
7892 register_prefix, i.op[1].regs->reg_name,
7893 register_prefix, i.op[1].regs->reg_name, first_reg_in_group,
7894 register_prefix, i.op[1].regs->reg_name, last_reg_in_group,
7895 i.tm.name);
7896 }
e2ec9d29
L
7897 else if (i.tm.opcode_modifier.regkludge)
7898 {
7899 /* The imul $imm, %reg instruction is converted into
7900 imul $imm, %reg, %reg, and the clr %reg instruction
7901 is converted into xor %reg, %reg. */
7902
7903 unsigned int first_reg_op;
7904
7905 if (operand_type_check (i.types[0], reg))
7906 first_reg_op = 0;
7907 else
7908 first_reg_op = 1;
7909 /* Pretend we saw the extra register operand. */
9c2799c2 7910 gas_assert (i.reg_operands == 1
7ab9ffdd 7911 && i.op[first_reg_op + 1].regs == 0);
e2ec9d29
L
7912 i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
7913 i.types[first_reg_op + 1] = i.types[first_reg_op];
7914 i.operands++;
7915 i.reg_operands++;
29b0f896
AM
7916 }
7917
85b80b0f 7918 if (i.tm.opcode_modifier.modrm)
29b0f896
AM
7919 {
7920 /* The opcode is completed (modulo i.tm.extension_opcode which
52271982
AM
7921 must be put into the modrm byte). Now, we make the modrm and
7922 index base bytes based on all the info we've collected. */
29b0f896
AM
7923
7924 default_seg = build_modrm_byte ();
7925 }
00cee14f 7926 else if (i.types[0].bitfield.class == SReg)
85b80b0f
JB
7927 {
7928 if (flag_code != CODE_64BIT
7929 ? i.tm.base_opcode == POP_SEG_SHORT
7930 && i.op[0].regs->reg_num == 1
389d00a5 7931 : (i.tm.base_opcode | 1) == (POP_SEG386_SHORT & 0xff)
85b80b0f
JB
7932 && i.op[0].regs->reg_num < 4)
7933 {
7934 as_bad (_("you can't `%s %s%s'"),
7935 i.tm.name, register_prefix, i.op[0].regs->reg_name);
7936 return 0;
7937 }
389d00a5
JB
7938 if (i.op[0].regs->reg_num > 3
7939 && i.tm.opcode_modifier.opcodespace == SPACE_BASE )
85b80b0f 7940 {
389d00a5
JB
7941 i.tm.base_opcode ^= (POP_SEG_SHORT ^ POP_SEG386_SHORT) & 0xff;
7942 i.tm.opcode_modifier.opcodespace = SPACE_0F;
85b80b0f
JB
7943 }
7944 i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
7945 }
389d00a5
JB
7946 else if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
7947 && (i.tm.base_opcode & ~3) == MOV_AX_DISP32)
29b0f896 7948 {
5e042380 7949 default_seg = reg_ds;
29b0f896 7950 }
40fb9820 7951 else if (i.tm.opcode_modifier.isstring)
29b0f896
AM
7952 {
7953 /* For the string instructions that allow a segment override
7954 on one of their operands, the default segment is ds. */
5e042380 7955 default_seg = reg_ds;
29b0f896 7956 }
50128d0c 7957 else if (i.short_form)
85b80b0f
JB
7958 {
7959 /* The register or float register operand is in operand
7960 0 or 1. */
bab6aec1 7961 unsigned int op = i.tm.operand_types[0].bitfield.class != Reg;
85b80b0f
JB
7962
7963 /* Register goes in low 3 bits of opcode. */
7964 i.tm.base_opcode |= i.op[op].regs->reg_num;
7965 if ((i.op[op].regs->reg_flags & RegRex) != 0)
7966 i.rex |= REX_B;
7967 if (!quiet_warnings && i.tm.opcode_modifier.ugh)
7968 {
7969 /* Warn about some common errors, but press on regardless.
7970 The first case can be generated by gcc (<= 2.8.1). */
7971 if (i.operands == 2)
7972 {
7973 /* Reversed arguments on faddp, fsubp, etc. */
7974 as_warn (_("translating to `%s %s%s,%s%s'"), i.tm.name,
7975 register_prefix, i.op[!intel_syntax].regs->reg_name,
7976 register_prefix, i.op[intel_syntax].regs->reg_name);
7977 }
7978 else
7979 {
7980 /* Extraneous `l' suffix on fp insn. */
7981 as_warn (_("translating to `%s %s%s'"), i.tm.name,
7982 register_prefix, i.op[0].regs->reg_name);
7983 }
7984 }
7985 }
29b0f896 7986
514a8bb0 7987 if ((i.seg[0] || i.prefix[SEG_PREFIX])
514a8bb0 7988 && i.tm.base_opcode == 0x8d /* lea */
35648716 7989 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
514a8bb0 7990 && !is_any_vex_encoding(&i.tm))
92334ad2
JB
7991 {
7992 if (!quiet_warnings)
7993 as_warn (_("segment override on `%s' is ineffectual"), i.tm.name);
7994 if (optimize)
7995 {
7996 i.seg[0] = NULL;
7997 i.prefix[SEG_PREFIX] = 0;
7998 }
7999 }
52271982
AM
8000
8001 /* If a segment was explicitly specified, and the specified segment
b6773884
JB
8002 is neither the default nor the one already recorded from a prefix,
8003 use an opcode prefix to select it. If we never figured out what
8004 the default segment is, then default_seg will be zero at this
8005 point, and the specified segment prefix will always be used. */
8006 if (i.seg[0]
8007 && i.seg[0] != default_seg
5e042380 8008 && i386_seg_prefixes[i.seg[0]->reg_num] != i.prefix[SEG_PREFIX])
29b0f896 8009 {
5e042380 8010 if (!add_prefix (i386_seg_prefixes[i.seg[0]->reg_num]))
29b0f896
AM
8011 return 0;
8012 }
8013 return 1;
8014}
8015
a5aeccd9 8016static INLINE void set_rex_vrex (const reg_entry *r, unsigned int rex_bit,
5b7c81bd 8017 bool do_sse2avx)
a5aeccd9
JB
8018{
8019 if (r->reg_flags & RegRex)
8020 {
8021 if (i.rex & rex_bit)
8022 as_bad (_("same type of prefix used twice"));
8023 i.rex |= rex_bit;
8024 }
8025 else if (do_sse2avx && (i.rex & rex_bit) && i.vex.register_specifier)
8026 {
8027 gas_assert (i.vex.register_specifier == r);
8028 i.vex.register_specifier += 8;
8029 }
8030
8031 if (r->reg_flags & RegVRex)
8032 i.vrex |= rex_bit;
8033}
8034
5e042380 8035static const reg_entry *
e3bb37b5 8036build_modrm_byte (void)
29b0f896 8037{
5e042380 8038 const reg_entry *default_seg = NULL;
c0f3af97 8039 unsigned int source, dest;
8cd7925b 8040 int vex_3_sources;
c0f3af97 8041
8cd7925b 8042 vex_3_sources = i.tm.opcode_modifier.vexsources == VEX3SOURCES;
c0f3af97
L
8043 if (vex_3_sources)
8044 {
91d6fa6a 8045 unsigned int nds, reg_slot;
4c2c6516 8046 expressionS *exp;
c0f3af97 8047
6b8d3588 8048 dest = i.operands - 1;
c0f3af97 8049 nds = dest - 1;
922d8de8 8050
a683cc34 8051 /* There are 2 kinds of instructions:
bed3d976 8052 1. 5 operands: 4 register operands or 3 register operands
9d3bf266 8053 plus 1 memory operand plus one Imm4 operand, VexXDS, and
bed3d976 8054 VexW0 or VexW1. The destination must be either XMM, YMM or
43234a1e 8055 ZMM register.
bed3d976 8056 2. 4 operands: 4 register operands or 3 register operands
2f1bada2 8057 plus 1 memory operand, with VexXDS. */
922d8de8 8058 gas_assert ((i.reg_operands == 4
bed3d976
JB
8059 || (i.reg_operands == 3 && i.mem_operands == 1))
8060 && i.tm.opcode_modifier.vexvvvv == VEXXDS
dcd7e323 8061 && i.tm.opcode_modifier.vexw
3528c362 8062 && i.tm.operand_types[dest].bitfield.class == RegSIMD);
a683cc34 8063
48db9223
JB
8064 /* If VexW1 is set, the first non-immediate operand is the source and
8065 the second non-immediate one is encoded in the immediate operand. */
8066 if (i.tm.opcode_modifier.vexw == VEXW1)
8067 {
8068 source = i.imm_operands;
8069 reg_slot = i.imm_operands + 1;
8070 }
8071 else
8072 {
8073 source = i.imm_operands + 1;
8074 reg_slot = i.imm_operands;
8075 }
8076
a683cc34 8077 if (i.imm_operands == 0)
bed3d976
JB
8078 {
8079 /* When there is no immediate operand, generate an 8bit
8080 immediate operand to encode the first operand. */
8081 exp = &im_expressions[i.imm_operands++];
8082 i.op[i.operands].imms = exp;
be1643ff 8083 i.types[i.operands].bitfield.imm8 = 1;
bed3d976
JB
8084 i.operands++;
8085
3528c362 8086 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
bed3d976
JB
8087 exp->X_op = O_constant;
8088 exp->X_add_number = register_number (i.op[reg_slot].regs) << 4;
43234a1e
L
8089 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
8090 }
922d8de8 8091 else
bed3d976 8092 {
9d3bf266
JB
8093 gas_assert (i.imm_operands == 1);
8094 gas_assert (fits_in_imm4 (i.op[0].imms->X_add_number));
8095 gas_assert (!i.tm.opcode_modifier.immext);
a683cc34 8096
9d3bf266
JB
8097 /* Turn on Imm8 again so that output_imm will generate it. */
8098 i.types[0].bitfield.imm8 = 1;
bed3d976 8099
3528c362 8100 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
9d3bf266 8101 i.op[0].imms->X_add_number
bed3d976 8102 |= register_number (i.op[reg_slot].regs) << 4;
43234a1e 8103 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
bed3d976 8104 }
a683cc34 8105
3528c362 8106 gas_assert (i.tm.operand_types[nds].bitfield.class == RegSIMD);
dae39acc 8107 i.vex.register_specifier = i.op[nds].regs;
c0f3af97
L
8108 }
8109 else
8110 source = dest = 0;
29b0f896
AM
8111
8112 /* i.reg_operands MUST be the number of real register operands;
c0f3af97
L
8113 implicit registers do not count. If there are 3 register
8114 operands, it must be a instruction with VexNDS. For a
8115 instruction with VexNDD, the destination register is encoded
8116 in VEX prefix. If there are 4 register operands, it must be
8117 a instruction with VEX prefix and 3 sources. */
7ab9ffdd
L
8118 if (i.mem_operands == 0
8119 && ((i.reg_operands == 2
2426c15f 8120 && i.tm.opcode_modifier.vexvvvv <= VEXXDS)
7ab9ffdd 8121 || (i.reg_operands == 3
2426c15f 8122 && i.tm.opcode_modifier.vexvvvv == VEXXDS)
7ab9ffdd 8123 || (i.reg_operands == 4 && vex_3_sources)))
29b0f896 8124 {
cab737b9
L
8125 switch (i.operands)
8126 {
8127 case 2:
8128 source = 0;
8129 break;
8130 case 3:
c81128dc
L
8131 /* When there are 3 operands, one of them may be immediate,
8132 which may be the first or the last operand. Otherwise,
c0f3af97
L
8133 the first operand must be shift count register (cl) or it
8134 is an instruction with VexNDS. */
9c2799c2 8135 gas_assert (i.imm_operands == 1
7ab9ffdd 8136 || (i.imm_operands == 0
2426c15f 8137 && (i.tm.opcode_modifier.vexvvvv == VEXXDS
75e5731b
JB
8138 || (i.types[0].bitfield.instance == RegC
8139 && i.types[0].bitfield.byte))));
40fb9820 8140 if (operand_type_check (i.types[0], imm)
75e5731b
JB
8141 || (i.types[0].bitfield.instance == RegC
8142 && i.types[0].bitfield.byte))
40fb9820
L
8143 source = 1;
8144 else
8145 source = 0;
cab737b9
L
8146 break;
8147 case 4:
368d64cc
L
8148 /* When there are 4 operands, the first two must be 8bit
8149 immediate operands. The source operand will be the 3rd
c0f3af97
L
8150 one.
8151
8152 For instructions with VexNDS, if the first operand
8153 an imm8, the source operand is the 2nd one. If the last
8154 operand is imm8, the source operand is the first one. */
9c2799c2 8155 gas_assert ((i.imm_operands == 2
7ab9ffdd
L
8156 && i.types[0].bitfield.imm8
8157 && i.types[1].bitfield.imm8)
2426c15f 8158 || (i.tm.opcode_modifier.vexvvvv == VEXXDS
7ab9ffdd
L
8159 && i.imm_operands == 1
8160 && (i.types[0].bitfield.imm8
cf665fee 8161 || i.types[i.operands - 1].bitfield.imm8)));
9f2670f2
L
8162 if (i.imm_operands == 2)
8163 source = 2;
8164 else
c0f3af97
L
8165 {
8166 if (i.types[0].bitfield.imm8)
8167 source = 1;
8168 else
8169 source = 0;
8170 }
c0f3af97
L
8171 break;
8172 case 5:
cf665fee
JB
8173 gas_assert (!is_evex_encoding (&i.tm));
8174 gas_assert (i.imm_operands == 1 && vex_3_sources);
cab737b9
L
8175 break;
8176 default:
8177 abort ();
8178 }
8179
c0f3af97
L
8180 if (!vex_3_sources)
8181 {
8182 dest = source + 1;
8183
2426c15f 8184 if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
c0f3af97 8185 {
43234a1e 8186 /* For instructions with VexNDS, the register-only source
c5d0745b 8187 operand must be a 32/64bit integer, XMM, YMM, ZMM, or mask
dfd69174 8188 register. It is encoded in VEX prefix. */
f12dc422
L
8189
8190 i386_operand_type op;
8191 unsigned int vvvv;
8192
c2ecccb3
L
8193 /* Swap two source operands if needed. */
8194 if (i.tm.opcode_modifier.swapsources)
f12dc422
L
8195 {
8196 vvvv = source;
8197 source = dest;
8198 }
8199 else
8200 vvvv = dest;
8201
8202 op = i.tm.operand_types[vvvv];
c0f3af97 8203 if ((dest + 1) >= i.operands
bab6aec1 8204 || ((op.bitfield.class != Reg
dc821c5f 8205 || (!op.bitfield.dword && !op.bitfield.qword))
3528c362 8206 && op.bitfield.class != RegSIMD
c0f327b8 8207 && op.bitfield.class != RegMask))
c0f3af97 8208 abort ();
f12dc422 8209 i.vex.register_specifier = i.op[vvvv].regs;
c0f3af97
L
8210 dest++;
8211 }
8212 }
29b0f896
AM
8213
8214 i.rm.mode = 3;
dfd69174
JB
8215 /* One of the register operands will be encoded in the i.rm.reg
8216 field, the other in the combined i.rm.mode and i.rm.regmem
29b0f896
AM
8217 fields. If no form of this instruction supports a memory
8218 destination operand, then we assume the source operand may
8219 sometimes be a memory operand and so we need to store the
8220 destination in the i.rm.reg field. */
dfd69174 8221 if (!i.tm.opcode_modifier.regmem
40fb9820 8222 && operand_type_check (i.tm.operand_types[dest], anymem) == 0)
29b0f896
AM
8223 {
8224 i.rm.reg = i.op[dest].regs->reg_num;
8225 i.rm.regmem = i.op[source].regs->reg_num;
a5aeccd9 8226 set_rex_vrex (i.op[dest].regs, REX_R, i.tm.opcode_modifier.sse2avx);
5b7c81bd 8227 set_rex_vrex (i.op[source].regs, REX_B, false);
29b0f896
AM
8228 }
8229 else
8230 {
8231 i.rm.reg = i.op[source].regs->reg_num;
8232 i.rm.regmem = i.op[dest].regs->reg_num;
a5aeccd9 8233 set_rex_vrex (i.op[dest].regs, REX_B, i.tm.opcode_modifier.sse2avx);
5b7c81bd 8234 set_rex_vrex (i.op[source].regs, REX_R, false);
29b0f896 8235 }
e0c7f900 8236 if (flag_code != CODE_64BIT && (i.rex & REX_R))
c4a530c5 8237 {
4a5c67ed 8238 if (i.types[!i.tm.opcode_modifier.regmem].bitfield.class != RegCR)
c4a530c5 8239 abort ();
e0c7f900 8240 i.rex &= ~REX_R;
c4a530c5
JB
8241 add_prefix (LOCK_PREFIX_OPCODE);
8242 }
29b0f896
AM
8243 }
8244 else
8245 { /* If it's not 2 reg operands... */
c0f3af97
L
8246 unsigned int mem;
8247
29b0f896
AM
8248 if (i.mem_operands)
8249 {
8250 unsigned int fake_zero_displacement = 0;
99018f42 8251 unsigned int op;
4eed87de 8252
7ab9ffdd 8253 for (op = 0; op < i.operands; op++)
8dc0818e 8254 if (i.flags[op] & Operand_Mem)
7ab9ffdd 8255 break;
7ab9ffdd 8256 gas_assert (op < i.operands);
29b0f896 8257
63112cd6 8258 if (i.tm.opcode_modifier.sib)
6c30d220 8259 {
260cd341
LC
8260 /* The index register of VSIB shouldn't be RegIZ. */
8261 if (i.tm.opcode_modifier.sib != SIBMEM
8262 && i.index_reg->reg_num == RegIZ)
6c30d220
L
8263 abort ();
8264
8265 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8266 if (!i.base_reg)
8267 {
8268 i.sib.base = NO_BASE_REGISTER;
8269 i.sib.scale = i.log2_scale_factor;
2f2be86b 8270 i.types[op] = operand_type_and_not (i.types[op], anydisp);
a775efc8 8271 i.types[op].bitfield.disp32 = 1;
6c30d220 8272 }
260cd341
LC
8273
8274 /* Since the mandatory SIB always has index register, so
8275 the code logic remains unchanged. The non-mandatory SIB
8276 without index register is allowed and will be handled
8277 later. */
8278 if (i.index_reg)
8279 {
8280 if (i.index_reg->reg_num == RegIZ)
8281 i.sib.index = NO_INDEX_REGISTER;
8282 else
8283 i.sib.index = i.index_reg->reg_num;
5b7c81bd 8284 set_rex_vrex (i.index_reg, REX_X, false);
260cd341 8285 }
6c30d220
L
8286 }
8287
5e042380 8288 default_seg = reg_ds;
29b0f896
AM
8289
8290 if (i.base_reg == 0)
8291 {
8292 i.rm.mode = 0;
8293 if (!i.disp_operands)
9bb129e8 8294 fake_zero_displacement = 1;
29b0f896
AM
8295 if (i.index_reg == 0)
8296 {
260cd341
LC
8297 /* Both check for VSIB and mandatory non-vector SIB. */
8298 gas_assert (!i.tm.opcode_modifier.sib
8299 || i.tm.opcode_modifier.sib == SIBMEM);
29b0f896 8300 /* Operand is just <disp> */
2f2be86b 8301 i.types[op] = operand_type_and_not (i.types[op], anydisp);
20f0a1fc 8302 if (flag_code == CODE_64BIT)
29b0f896
AM
8303 {
8304 /* 64bit mode overwrites the 32bit absolute
8305 addressing by RIP relative addressing and
8306 absolute addressing is encoded by one of the
8307 redundant SIB forms. */
8308 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8309 i.sib.base = NO_BASE_REGISTER;
8310 i.sib.index = NO_INDEX_REGISTER;
a775efc8 8311 i.types[op].bitfield.disp32 = 1;
20f0a1fc 8312 }
fc225355
L
8313 else if ((flag_code == CODE_16BIT)
8314 ^ (i.prefix[ADDR_PREFIX] != 0))
20f0a1fc
NC
8315 {
8316 i.rm.regmem = NO_BASE_REGISTER_16;
2f2be86b 8317 i.types[op].bitfield.disp16 = 1;
20f0a1fc
NC
8318 }
8319 else
8320 {
8321 i.rm.regmem = NO_BASE_REGISTER;
2f2be86b 8322 i.types[op].bitfield.disp32 = 1;
29b0f896
AM
8323 }
8324 }
63112cd6 8325 else if (!i.tm.opcode_modifier.sib)
29b0f896 8326 {
6c30d220 8327 /* !i.base_reg && i.index_reg */
e968fc9b 8328 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8329 i.sib.index = NO_INDEX_REGISTER;
8330 else
8331 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8332 i.sib.base = NO_BASE_REGISTER;
8333 i.sib.scale = i.log2_scale_factor;
8334 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
2f2be86b 8335 i.types[op] = operand_type_and_not (i.types[op], anydisp);
a775efc8 8336 i.types[op].bitfield.disp32 = 1;
29b0f896 8337 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8338 i.rex |= REX_X;
29b0f896
AM
8339 }
8340 }
8341 /* RIP addressing for 64bit mode. */
e968fc9b 8342 else if (i.base_reg->reg_num == RegIP)
29b0f896 8343 {
63112cd6 8344 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896 8345 i.rm.regmem = NO_BASE_REGISTER;
40fb9820
L
8346 i.types[op].bitfield.disp8 = 0;
8347 i.types[op].bitfield.disp16 = 0;
a775efc8 8348 i.types[op].bitfield.disp32 = 1;
40fb9820 8349 i.types[op].bitfield.disp64 = 0;
71903a11 8350 i.flags[op] |= Operand_PCrel;
20f0a1fc
NC
8351 if (! i.disp_operands)
8352 fake_zero_displacement = 1;
29b0f896 8353 }
dc821c5f 8354 else if (i.base_reg->reg_type.bitfield.word)
29b0f896 8355 {
63112cd6 8356 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896
AM
8357 switch (i.base_reg->reg_num)
8358 {
8359 case 3: /* (%bx) */
8360 if (i.index_reg == 0)
8361 i.rm.regmem = 7;
8362 else /* (%bx,%si) -> 0, or (%bx,%di) -> 1 */
8363 i.rm.regmem = i.index_reg->reg_num - 6;
8364 break;
8365 case 5: /* (%bp) */
5e042380 8366 default_seg = reg_ss;
29b0f896
AM
8367 if (i.index_reg == 0)
8368 {
8369 i.rm.regmem = 6;
40fb9820 8370 if (operand_type_check (i.types[op], disp) == 0)
29b0f896
AM
8371 {
8372 /* fake (%bp) into 0(%bp) */
41eb8e88 8373 if (i.disp_encoding == disp_encoding_16bit)
1a02d6b0
L
8374 i.types[op].bitfield.disp16 = 1;
8375 else
8376 i.types[op].bitfield.disp8 = 1;
252b5132 8377 fake_zero_displacement = 1;
29b0f896
AM
8378 }
8379 }
8380 else /* (%bp,%si) -> 2, or (%bp,%di) -> 3 */
8381 i.rm.regmem = i.index_reg->reg_num - 6 + 2;
8382 break;
8383 default: /* (%si) -> 4 or (%di) -> 5 */
8384 i.rm.regmem = i.base_reg->reg_num - 6 + 4;
8385 }
41eb8e88
L
8386 if (!fake_zero_displacement
8387 && !i.disp_operands
8388 && i.disp_encoding)
8389 {
8390 fake_zero_displacement = 1;
8391 if (i.disp_encoding == disp_encoding_8bit)
8392 i.types[op].bitfield.disp8 = 1;
8393 else
8394 i.types[op].bitfield.disp16 = 1;
8395 }
29b0f896
AM
8396 i.rm.mode = mode_from_disp_size (i.types[op]);
8397 }
8398 else /* i.base_reg and 32/64 bit mode */
8399 {
a9aabc23 8400 if (operand_type_check (i.types[op], disp))
40fb9820 8401 {
73053c1f
JB
8402 i.types[op].bitfield.disp16 = 0;
8403 i.types[op].bitfield.disp64 = 0;
a775efc8 8404 i.types[op].bitfield.disp32 = 1;
40fb9820 8405 }
20f0a1fc 8406
63112cd6 8407 if (!i.tm.opcode_modifier.sib)
6c30d220 8408 i.rm.regmem = i.base_reg->reg_num;
29b0f896 8409 if ((i.base_reg->reg_flags & RegRex) != 0)
161a04f6 8410 i.rex |= REX_B;
29b0f896
AM
8411 i.sib.base = i.base_reg->reg_num;
8412 /* x86-64 ignores REX prefix bit here to avoid decoder
8413 complications. */
848930b2
JB
8414 if (!(i.base_reg->reg_flags & RegRex)
8415 && (i.base_reg->reg_num == EBP_REG_NUM
8416 || i.base_reg->reg_num == ESP_REG_NUM))
5e042380 8417 default_seg = reg_ss;
848930b2 8418 if (i.base_reg->reg_num == 5 && i.disp_operands == 0)
29b0f896 8419 {
848930b2 8420 fake_zero_displacement = 1;
1a02d6b0
L
8421 if (i.disp_encoding == disp_encoding_32bit)
8422 i.types[op].bitfield.disp32 = 1;
8423 else
8424 i.types[op].bitfield.disp8 = 1;
29b0f896
AM
8425 }
8426 i.sib.scale = i.log2_scale_factor;
8427 if (i.index_reg == 0)
8428 {
260cd341
LC
8429 /* Only check for VSIB. */
8430 gas_assert (i.tm.opcode_modifier.sib != VECSIB128
8431 && i.tm.opcode_modifier.sib != VECSIB256
8432 && i.tm.opcode_modifier.sib != VECSIB512);
8433
29b0f896
AM
8434 /* <disp>(%esp) becomes two byte modrm with no index
8435 register. We've already stored the code for esp
8436 in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
8437 Any base register besides %esp will not use the
8438 extra modrm byte. */
8439 i.sib.index = NO_INDEX_REGISTER;
29b0f896 8440 }
63112cd6 8441 else if (!i.tm.opcode_modifier.sib)
29b0f896 8442 {
e968fc9b 8443 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8444 i.sib.index = NO_INDEX_REGISTER;
8445 else
8446 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8447 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8448 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8449 i.rex |= REX_X;
29b0f896 8450 }
67a4f2b7
AO
8451
8452 if (i.disp_operands
8453 && (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
8454 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL))
8455 i.rm.mode = 0;
8456 else
a501d77e
L
8457 {
8458 if (!fake_zero_displacement
8459 && !i.disp_operands
8460 && i.disp_encoding)
8461 {
8462 fake_zero_displacement = 1;
8463 if (i.disp_encoding == disp_encoding_8bit)
8464 i.types[op].bitfield.disp8 = 1;
8465 else
8466 i.types[op].bitfield.disp32 = 1;
8467 }
8468 i.rm.mode = mode_from_disp_size (i.types[op]);
8469 }
29b0f896 8470 }
252b5132 8471
29b0f896
AM
8472 if (fake_zero_displacement)
8473 {
8474 /* Fakes a zero displacement assuming that i.types[op]
8475 holds the correct displacement size. */
8476 expressionS *exp;
8477
9c2799c2 8478 gas_assert (i.op[op].disps == 0);
29b0f896
AM
8479 exp = &disp_expressions[i.disp_operands++];
8480 i.op[op].disps = exp;
8481 exp->X_op = O_constant;
8482 exp->X_add_number = 0;
8483 exp->X_add_symbol = (symbolS *) 0;
8484 exp->X_op_symbol = (symbolS *) 0;
8485 }
c0f3af97
L
8486
8487 mem = op;
29b0f896 8488 }
c0f3af97
L
8489 else
8490 mem = ~0;
252b5132 8491
8c43a48b 8492 if (i.tm.opcode_modifier.vexsources == XOP2SOURCES)
5dd85c99
SP
8493 {
8494 if (operand_type_check (i.types[0], imm))
8495 i.vex.register_specifier = NULL;
8496 else
8497 {
8498 /* VEX.vvvv encodes one of the sources when the first
8499 operand is not an immediate. */
1ef99a7b 8500 if (i.tm.opcode_modifier.vexw == VEXW0)
5dd85c99
SP
8501 i.vex.register_specifier = i.op[0].regs;
8502 else
8503 i.vex.register_specifier = i.op[1].regs;
8504 }
8505
8506 /* Destination is a XMM register encoded in the ModRM.reg
8507 and VEX.R bit. */
8508 i.rm.reg = i.op[2].regs->reg_num;
8509 if ((i.op[2].regs->reg_flags & RegRex) != 0)
8510 i.rex |= REX_R;
8511
8512 /* ModRM.rm and VEX.B encodes the other source. */
8513 if (!i.mem_operands)
8514 {
8515 i.rm.mode = 3;
8516
1ef99a7b 8517 if (i.tm.opcode_modifier.vexw == VEXW0)
5dd85c99
SP
8518 i.rm.regmem = i.op[1].regs->reg_num;
8519 else
8520 i.rm.regmem = i.op[0].regs->reg_num;
8521
8522 if ((i.op[1].regs->reg_flags & RegRex) != 0)
8523 i.rex |= REX_B;
8524 }
8525 }
2426c15f 8526 else if (i.tm.opcode_modifier.vexvvvv == VEXLWP)
f88c9eb0
SP
8527 {
8528 i.vex.register_specifier = i.op[2].regs;
8529 if (!i.mem_operands)
8530 {
8531 i.rm.mode = 3;
8532 i.rm.regmem = i.op[1].regs->reg_num;
8533 if ((i.op[1].regs->reg_flags & RegRex) != 0)
8534 i.rex |= REX_B;
8535 }
8536 }
29b0f896
AM
8537 /* Fill in i.rm.reg or i.rm.regmem field with register operand
8538 (if any) based on i.tm.extension_opcode. Again, we must be
8539 careful to make sure that segment/control/debug/test/MMX
8540 registers are coded into the i.rm.reg field. */
f88c9eb0 8541 else if (i.reg_operands)
29b0f896 8542 {
99018f42 8543 unsigned int op;
7ab9ffdd
L
8544 unsigned int vex_reg = ~0;
8545
8546 for (op = 0; op < i.operands; op++)
921eafea
L
8547 if (i.types[op].bitfield.class == Reg
8548 || i.types[op].bitfield.class == RegBND
8549 || i.types[op].bitfield.class == RegMask
8550 || i.types[op].bitfield.class == SReg
8551 || i.types[op].bitfield.class == RegCR
8552 || i.types[op].bitfield.class == RegDR
8553 || i.types[op].bitfield.class == RegTR
8554 || i.types[op].bitfield.class == RegSIMD
8555 || i.types[op].bitfield.class == RegMMX)
8556 break;
c0209578 8557
7ab9ffdd
L
8558 if (vex_3_sources)
8559 op = dest;
2426c15f 8560 else if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
7ab9ffdd
L
8561 {
8562 /* For instructions with VexNDS, the register-only
8563 source operand is encoded in VEX prefix. */
8564 gas_assert (mem != (unsigned int) ~0);
c0f3af97 8565
a93e3234 8566 if (op > mem || i.tm.cpu_flags.bitfield.cpucmpccxadd)
c0f3af97 8567 {
7ab9ffdd
L
8568 vex_reg = op++;
8569 gas_assert (op < i.operands);
c0f3af97
L
8570 }
8571 else
c0f3af97 8572 {
f12dc422
L
8573 /* Check register-only source operand when two source
8574 operands are swapped. */
8575 if (!i.tm.operand_types[op].bitfield.baseindex
8576 && i.tm.operand_types[op + 1].bitfield.baseindex)
8577 {
8578 vex_reg = op;
8579 op += 2;
8580 gas_assert (mem == (vex_reg + 1)
8581 && op < i.operands);
8582 }
8583 else
8584 {
8585 vex_reg = op + 1;
8586 gas_assert (vex_reg < i.operands);
8587 }
c0f3af97 8588 }
7ab9ffdd 8589 }
2426c15f 8590 else if (i.tm.opcode_modifier.vexvvvv == VEXNDD)
7ab9ffdd 8591 {
f12dc422 8592 /* For instructions with VexNDD, the register destination
7ab9ffdd 8593 is encoded in VEX prefix. */
f12dc422
L
8594 if (i.mem_operands == 0)
8595 {
8596 /* There is no memory operand. */
8597 gas_assert ((op + 2) == i.operands);
8598 vex_reg = op + 1;
8599 }
8600 else
8d63c93e 8601 {
ed438a93
JB
8602 /* There are only 2 non-immediate operands. */
8603 gas_assert (op < i.imm_operands + 2
8604 && i.operands == i.imm_operands + 2);
8605 vex_reg = i.imm_operands + 1;
f12dc422 8606 }
7ab9ffdd
L
8607 }
8608 else
8609 gas_assert (op < i.operands);
99018f42 8610
7ab9ffdd
L
8611 if (vex_reg != (unsigned int) ~0)
8612 {
f12dc422 8613 i386_operand_type *type = &i.tm.operand_types[vex_reg];
7ab9ffdd 8614
bab6aec1 8615 if ((type->bitfield.class != Reg
dc821c5f 8616 || (!type->bitfield.dword && !type->bitfield.qword))
3528c362 8617 && type->bitfield.class != RegSIMD
c0f327b8 8618 && type->bitfield.class != RegMask)
7ab9ffdd 8619 abort ();
f88c9eb0 8620
7ab9ffdd
L
8621 i.vex.register_specifier = i.op[vex_reg].regs;
8622 }
8623
1b9f0c97
L
8624 /* Don't set OP operand twice. */
8625 if (vex_reg != op)
7ab9ffdd 8626 {
1b9f0c97
L
8627 /* If there is an extension opcode to put here, the
8628 register number must be put into the regmem field. */
8629 if (i.tm.extension_opcode != None)
8630 {
8631 i.rm.regmem = i.op[op].regs->reg_num;
a5aeccd9
JB
8632 set_rex_vrex (i.op[op].regs, REX_B,
8633 i.tm.opcode_modifier.sse2avx);
1b9f0c97
L
8634 }
8635 else
8636 {
8637 i.rm.reg = i.op[op].regs->reg_num;
a5aeccd9
JB
8638 set_rex_vrex (i.op[op].regs, REX_R,
8639 i.tm.opcode_modifier.sse2avx);
1b9f0c97 8640 }
7ab9ffdd 8641 }
252b5132 8642
29b0f896
AM
8643 /* Now, if no memory operand has set i.rm.mode = 0, 1, 2 we
8644 must set it to 3 to indicate this is a register operand
8645 in the regmem field. */
8646 if (!i.mem_operands)
8647 i.rm.mode = 3;
8648 }
252b5132 8649
29b0f896 8650 /* Fill in i.rm.reg field with extension opcode (if any). */
c1e679ec 8651 if (i.tm.extension_opcode != None)
29b0f896
AM
8652 i.rm.reg = i.tm.extension_opcode;
8653 }
8654 return default_seg;
8655}
252b5132 8656
48ef937e
JB
8657static INLINE void
8658frag_opcode_byte (unsigned char byte)
8659{
8660 if (now_seg != absolute_section)
8661 FRAG_APPEND_1_CHAR (byte);
8662 else
8663 ++abs_section_offset;
8664}
8665
376cd056
JB
8666static unsigned int
8667flip_code16 (unsigned int code16)
8668{
8669 gas_assert (i.tm.operands == 1);
8670
8671 return !(i.prefix[REX_PREFIX] & REX_W)
8672 && (code16 ? i.tm.operand_types[0].bitfield.disp32
376cd056
JB
8673 : i.tm.operand_types[0].bitfield.disp16)
8674 ? CODE16 : 0;
8675}
8676
29b0f896 8677static void
e3bb37b5 8678output_branch (void)
29b0f896
AM
8679{
8680 char *p;
f8a5c266 8681 int size;
29b0f896
AM
8682 int code16;
8683 int prefix;
8684 relax_substateT subtype;
8685 symbolS *sym;
8686 offsetT off;
8687
48ef937e
JB
8688 if (now_seg == absolute_section)
8689 {
8690 as_bad (_("relaxable branches not supported in absolute section"));
8691 return;
8692 }
8693
f8a5c266 8694 code16 = flag_code == CODE_16BIT ? CODE16 : 0;
1a42a9fe 8695 size = i.disp_encoding > disp_encoding_8bit ? BIG : SMALL;
29b0f896
AM
8696
8697 prefix = 0;
8698 if (i.prefix[DATA_PREFIX] != 0)
252b5132 8699 {
29b0f896
AM
8700 prefix = 1;
8701 i.prefixes -= 1;
376cd056 8702 code16 ^= flip_code16(code16);
252b5132 8703 }
29b0f896
AM
8704 /* Pentium4 branch hints. */
8705 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8706 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
2f66722d 8707 {
29b0f896
AM
8708 prefix++;
8709 i.prefixes--;
8710 }
8711 if (i.prefix[REX_PREFIX] != 0)
8712 {
8713 prefix++;
8714 i.prefixes--;
2f66722d
AM
8715 }
8716
7e8b059b
L
8717 /* BND prefixed jump. */
8718 if (i.prefix[BND_PREFIX] != 0)
8719 {
6cb0a70e
JB
8720 prefix++;
8721 i.prefixes--;
7e8b059b
L
8722 }
8723
f2810fe0
JB
8724 if (i.prefixes != 0)
8725 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
29b0f896
AM
8726
8727 /* It's always a symbol; End frag & setup for relax.
8728 Make sure there is enough room in this frag for the largest
8729 instruction we may generate in md_convert_frag. This is 2
8730 bytes for the opcode and room for the prefix and largest
8731 displacement. */
8732 frag_grow (prefix + 2 + 4);
8733 /* Prefix and 1 opcode byte go in fr_fix. */
8734 p = frag_more (prefix + 1);
8735 if (i.prefix[DATA_PREFIX] != 0)
8736 *p++ = DATA_PREFIX_OPCODE;
8737 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
8738 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
8739 *p++ = i.prefix[SEG_PREFIX];
6cb0a70e
JB
8740 if (i.prefix[BND_PREFIX] != 0)
8741 *p++ = BND_PREFIX_OPCODE;
29b0f896
AM
8742 if (i.prefix[REX_PREFIX] != 0)
8743 *p++ = i.prefix[REX_PREFIX];
8744 *p = i.tm.base_opcode;
8745
8746 if ((unsigned char) *p == JUMP_PC_RELATIVE)
f8a5c266 8747 subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, size);
40fb9820 8748 else if (cpu_arch_flags.bitfield.cpui386)
f8a5c266 8749 subtype = ENCODE_RELAX_STATE (COND_JUMP, size);
29b0f896 8750 else
f8a5c266 8751 subtype = ENCODE_RELAX_STATE (COND_JUMP86, size);
29b0f896 8752 subtype |= code16;
3e73aa7c 8753
29b0f896
AM
8754 sym = i.op[0].disps->X_add_symbol;
8755 off = i.op[0].disps->X_add_number;
3e73aa7c 8756
29b0f896
AM
8757 if (i.op[0].disps->X_op != O_constant
8758 && i.op[0].disps->X_op != O_symbol)
3e73aa7c 8759 {
29b0f896
AM
8760 /* Handle complex expressions. */
8761 sym = make_expr_symbol (i.op[0].disps);
8762 off = 0;
8763 }
3e73aa7c 8764
1ef3994a
JB
8765 frag_now->tc_frag_data.code64 = flag_code == CODE_64BIT;
8766
29b0f896
AM
8767 /* 1 possible extra opcode + 4 byte displacement go in var part.
8768 Pass reloc in fr_var. */
d258b828 8769 frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
29b0f896 8770}
3e73aa7c 8771
bd7ab16b
L
8772#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8773/* Return TRUE iff PLT32 relocation should be used for branching to
8774 symbol S. */
8775
5b7c81bd 8776static bool
bd7ab16b
L
8777need_plt32_p (symbolS *s)
8778{
8779 /* PLT32 relocation is ELF only. */
8780 if (!IS_ELF)
5b7c81bd 8781 return false;
bd7ab16b 8782
a5def729
RO
8783#ifdef TE_SOLARIS
8784 /* Don't emit PLT32 relocation on Solaris: neither native linker nor
8785 krtld support it. */
5b7c81bd 8786 return false;
a5def729
RO
8787#endif
8788
bd7ab16b
L
8789 /* Since there is no need to prepare for PLT branch on x86-64, we
8790 can generate R_X86_64_PLT32, instead of R_X86_64_PC32, which can
8791 be used as a marker for 32-bit PC-relative branches. */
8792 if (!object_64bit)
5b7c81bd 8793 return false;
bd7ab16b 8794
44365e88 8795 if (s == NULL)
5b7c81bd 8796 return false;
44365e88 8797
bd7ab16b
L
8798 /* Weak or undefined symbol need PLT32 relocation. */
8799 if (S_IS_WEAK (s) || !S_IS_DEFINED (s))
5b7c81bd 8800 return true;
bd7ab16b
L
8801
8802 /* Non-global symbol doesn't need PLT32 relocation. */
8803 if (! S_IS_EXTERNAL (s))
5b7c81bd 8804 return false;
bd7ab16b
L
8805
8806 /* Other global symbols need PLT32 relocation. NB: Symbol with
8807 non-default visibilities are treated as normal global symbol
8808 so that PLT32 relocation can be used as a marker for 32-bit
8809 PC-relative branches. It is useful for linker relaxation. */
5b7c81bd 8810 return true;
bd7ab16b
L
8811}
8812#endif
8813
29b0f896 8814static void
e3bb37b5 8815output_jump (void)
29b0f896
AM
8816{
8817 char *p;
8818 int size;
3e02c1cc 8819 fixS *fixP;
bd7ab16b 8820 bfd_reloc_code_real_type jump_reloc = i.reloc[0];
29b0f896 8821
0cfa3eb3 8822 if (i.tm.opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
8823 {
8824 /* This is a loop or jecxz type instruction. */
8825 size = 1;
8826 if (i.prefix[ADDR_PREFIX] != 0)
8827 {
48ef937e 8828 frag_opcode_byte (ADDR_PREFIX_OPCODE);
29b0f896
AM
8829 i.prefixes -= 1;
8830 }
8831 /* Pentium4 branch hints. */
8832 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8833 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
8834 {
48ef937e 8835 frag_opcode_byte (i.prefix[SEG_PREFIX]);
29b0f896 8836 i.prefixes--;
3e73aa7c
JH
8837 }
8838 }
29b0f896
AM
8839 else
8840 {
8841 int code16;
3e73aa7c 8842
29b0f896
AM
8843 code16 = 0;
8844 if (flag_code == CODE_16BIT)
8845 code16 = CODE16;
3e73aa7c 8846
29b0f896
AM
8847 if (i.prefix[DATA_PREFIX] != 0)
8848 {
48ef937e 8849 frag_opcode_byte (DATA_PREFIX_OPCODE);
29b0f896 8850 i.prefixes -= 1;
376cd056 8851 code16 ^= flip_code16(code16);
29b0f896 8852 }
252b5132 8853
29b0f896
AM
8854 size = 4;
8855 if (code16)
8856 size = 2;
8857 }
9fcc94b6 8858
6cb0a70e
JB
8859 /* BND prefixed jump. */
8860 if (i.prefix[BND_PREFIX] != 0)
29b0f896 8861 {
48ef937e 8862 frag_opcode_byte (i.prefix[BND_PREFIX]);
29b0f896
AM
8863 i.prefixes -= 1;
8864 }
252b5132 8865
6cb0a70e 8866 if (i.prefix[REX_PREFIX] != 0)
7e8b059b 8867 {
48ef937e 8868 frag_opcode_byte (i.prefix[REX_PREFIX]);
7e8b059b
L
8869 i.prefixes -= 1;
8870 }
8871
f2810fe0
JB
8872 if (i.prefixes != 0)
8873 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
e0890092 8874
48ef937e
JB
8875 if (now_seg == absolute_section)
8876 {
9a182d04 8877 abs_section_offset += i.opcode_length + size;
48ef937e
JB
8878 return;
8879 }
8880
9a182d04
JB
8881 p = frag_more (i.opcode_length + size);
8882 switch (i.opcode_length)
42164a71
L
8883 {
8884 case 2:
8885 *p++ = i.tm.base_opcode >> 8;
1a0670f3 8886 /* Fall through. */
42164a71
L
8887 case 1:
8888 *p++ = i.tm.base_opcode;
8889 break;
8890 default:
8891 abort ();
8892 }
e0890092 8893
bd7ab16b 8894#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1ef3994a
JB
8895 if (flag_code == CODE_64BIT && size == 4
8896 && jump_reloc == NO_RELOC && i.op[0].disps->X_add_number == 0
bd7ab16b
L
8897 && need_plt32_p (i.op[0].disps->X_add_symbol))
8898 jump_reloc = BFD_RELOC_X86_64_PLT32;
8899#endif
8900
8901 jump_reloc = reloc (size, 1, 1, jump_reloc);
8902
3e02c1cc 8903 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
bd7ab16b 8904 i.op[0].disps, 1, jump_reloc);
3e02c1cc 8905
eb19308f
JB
8906 /* All jumps handled here are signed, but don't unconditionally use a
8907 signed limit check for 32 and 16 bit jumps as we want to allow wrap
8908 around at 4G (outside of 64-bit mode) and 64k (except for XBEGIN)
8909 respectively. */
8910 switch (size)
8911 {
8912 case 1:
8913 fixP->fx_signed = 1;
8914 break;
8915
8916 case 2:
8917 if (i.tm.base_opcode == 0xc7f8)
8918 fixP->fx_signed = 1;
8919 break;
8920
8921 case 4:
8922 if (flag_code == CODE_64BIT)
8923 fixP->fx_signed = 1;
8924 break;
8925 }
29b0f896 8926}
e0890092 8927
29b0f896 8928static void
e3bb37b5 8929output_interseg_jump (void)
29b0f896
AM
8930{
8931 char *p;
8932 int size;
8933 int prefix;
8934 int code16;
252b5132 8935
29b0f896
AM
8936 code16 = 0;
8937 if (flag_code == CODE_16BIT)
8938 code16 = CODE16;
a217f122 8939
29b0f896
AM
8940 prefix = 0;
8941 if (i.prefix[DATA_PREFIX] != 0)
8942 {
8943 prefix = 1;
8944 i.prefixes -= 1;
8945 code16 ^= CODE16;
8946 }
6cb0a70e
JB
8947
8948 gas_assert (!i.prefix[REX_PREFIX]);
252b5132 8949
29b0f896
AM
8950 size = 4;
8951 if (code16)
8952 size = 2;
252b5132 8953
f2810fe0
JB
8954 if (i.prefixes != 0)
8955 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
252b5132 8956
48ef937e
JB
8957 if (now_seg == absolute_section)
8958 {
8959 abs_section_offset += prefix + 1 + 2 + size;
8960 return;
8961 }
8962
29b0f896
AM
8963 /* 1 opcode; 2 segment; offset */
8964 p = frag_more (prefix + 1 + 2 + size);
3e73aa7c 8965
29b0f896
AM
8966 if (i.prefix[DATA_PREFIX] != 0)
8967 *p++ = DATA_PREFIX_OPCODE;
252b5132 8968
29b0f896
AM
8969 if (i.prefix[REX_PREFIX] != 0)
8970 *p++ = i.prefix[REX_PREFIX];
252b5132 8971
29b0f896
AM
8972 *p++ = i.tm.base_opcode;
8973 if (i.op[1].imms->X_op == O_constant)
8974 {
8975 offsetT n = i.op[1].imms->X_add_number;
252b5132 8976
29b0f896
AM
8977 if (size == 2
8978 && !fits_in_unsigned_word (n)
8979 && !fits_in_signed_word (n))
8980 {
8981 as_bad (_("16-bit jump out of range"));
8982 return;
8983 }
8984 md_number_to_chars (p, n, size);
8985 }
8986 else
8987 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
d258b828 8988 i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
6d96a594
C
8989
8990 p += size;
8991 if (i.op[0].imms->X_op == O_constant)
8992 md_number_to_chars (p, (valueT) i.op[0].imms->X_add_number, 2);
8993 else
8994 fix_new_exp (frag_now, p - frag_now->fr_literal, 2,
8995 i.op[0].imms, 0, reloc (2, 0, 0, i.reloc[0]));
29b0f896 8996}
a217f122 8997
b4a3a7b4
L
8998#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8999void
9000x86_cleanup (void)
9001{
9002 char *p;
9003 asection *seg = now_seg;
9004 subsegT subseg = now_subseg;
9005 asection *sec;
9006 unsigned int alignment, align_size_1;
9007 unsigned int isa_1_descsz, feature_2_descsz, descsz;
9008 unsigned int isa_1_descsz_raw, feature_2_descsz_raw;
9009 unsigned int padding;
9010
1273b2f8 9011 if (!IS_ELF || !x86_used_note)
b4a3a7b4
L
9012 return;
9013
b4a3a7b4
L
9014 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X86;
9015
9016 /* The .note.gnu.property section layout:
9017
9018 Field Length Contents
9019 ---- ---- ----
9020 n_namsz 4 4
9021 n_descsz 4 The note descriptor size
9022 n_type 4 NT_GNU_PROPERTY_TYPE_0
9023 n_name 4 "GNU"
9024 n_desc n_descsz The program property array
9025 .... .... ....
9026 */
9027
9028 /* Create the .note.gnu.property section. */
9029 sec = subseg_new (NOTE_GNU_PROPERTY_SECTION_NAME, 0);
fd361982 9030 bfd_set_section_flags (sec,
b4a3a7b4
L
9031 (SEC_ALLOC
9032 | SEC_LOAD
9033 | SEC_DATA
9034 | SEC_HAS_CONTENTS
9035 | SEC_READONLY));
9036
9037 if (get_elf_backend_data (stdoutput)->s->elfclass == ELFCLASS64)
9038 {
9039 align_size_1 = 7;
9040 alignment = 3;
9041 }
9042 else
9043 {
9044 align_size_1 = 3;
9045 alignment = 2;
9046 }
9047
fd361982 9048 bfd_set_section_alignment (sec, alignment);
b4a3a7b4
L
9049 elf_section_type (sec) = SHT_NOTE;
9050
1273b2f8
L
9051 /* GNU_PROPERTY_X86_ISA_1_USED: 4-byte type + 4-byte data size
9052 + 4-byte data */
9053 isa_1_descsz_raw = 4 + 4 + 4;
9054 /* Align GNU_PROPERTY_X86_ISA_1_USED. */
9055 isa_1_descsz = (isa_1_descsz_raw + align_size_1) & ~align_size_1;
b4a3a7b4
L
9056
9057 feature_2_descsz_raw = isa_1_descsz;
9058 /* GNU_PROPERTY_X86_FEATURE_2_USED: 4-byte type + 4-byte data size
1273b2f8 9059 + 4-byte data */
b4a3a7b4
L
9060 feature_2_descsz_raw += 4 + 4 + 4;
9061 /* Align GNU_PROPERTY_X86_FEATURE_2_USED. */
9062 feature_2_descsz = ((feature_2_descsz_raw + align_size_1)
9063 & ~align_size_1);
9064
9065 descsz = feature_2_descsz;
9066 /* Section size: n_namsz + n_descsz + n_type + n_name + n_descsz. */
9067 p = frag_more (4 + 4 + 4 + 4 + descsz);
9068
9069 /* Write n_namsz. */
9070 md_number_to_chars (p, (valueT) 4, 4);
9071
9072 /* Write n_descsz. */
9073 md_number_to_chars (p + 4, (valueT) descsz, 4);
9074
9075 /* Write n_type. */
9076 md_number_to_chars (p + 4 * 2, (valueT) NT_GNU_PROPERTY_TYPE_0, 4);
9077
9078 /* Write n_name. */
9079 memcpy (p + 4 * 3, "GNU", 4);
9080
1273b2f8
L
9081 /* Write 4-byte type. */
9082 md_number_to_chars (p + 4 * 4,
9083 (valueT) GNU_PROPERTY_X86_ISA_1_USED, 4);
b4a3a7b4 9084
1273b2f8
L
9085 /* Write 4-byte data size. */
9086 md_number_to_chars (p + 4 * 5, (valueT) 4, 4);
b4a3a7b4 9087
1273b2f8
L
9088 /* Write 4-byte data. */
9089 md_number_to_chars (p + 4 * 6, (valueT) x86_isa_1_used, 4);
b4a3a7b4 9090
1273b2f8
L
9091 /* Zero out paddings. */
9092 padding = isa_1_descsz - isa_1_descsz_raw;
9093 if (padding)
9094 memset (p + 4 * 7, 0, padding);
b4a3a7b4
L
9095
9096 /* Write 4-byte type. */
9097 md_number_to_chars (p + isa_1_descsz + 4 * 4,
9098 (valueT) GNU_PROPERTY_X86_FEATURE_2_USED, 4);
9099
9100 /* Write 4-byte data size. */
9101 md_number_to_chars (p + isa_1_descsz + 4 * 5, (valueT) 4, 4);
9102
9103 /* Write 4-byte data. */
9104 md_number_to_chars (p + isa_1_descsz + 4 * 6,
9105 (valueT) x86_feature_2_used, 4);
9106
9107 /* Zero out paddings. */
9108 padding = feature_2_descsz - feature_2_descsz_raw;
9109 if (padding)
9110 memset (p + isa_1_descsz + 4 * 7, 0, padding);
9111
9112 /* We probably can't restore the current segment, for there likely
9113 isn't one yet... */
9114 if (seg && subseg)
9115 subseg_set (seg, subseg);
9116}
9117#endif
9118
9c33702b
JB
9119static unsigned int
9120encoding_length (const fragS *start_frag, offsetT start_off,
9121 const char *frag_now_ptr)
9122{
9123 unsigned int len = 0;
9124
9125 if (start_frag != frag_now)
9126 {
9127 const fragS *fr = start_frag;
9128
9129 do {
9130 len += fr->fr_fix;
9131 fr = fr->fr_next;
9132 } while (fr && fr != frag_now);
9133 }
9134
9135 return len - start_off + (frag_now_ptr - frag_now->fr_literal);
9136}
9137
e379e5f3 9138/* Return 1 for test, and, cmp, add, sub, inc and dec which may
79d72f45
HL
9139 be macro-fused with conditional jumps.
9140 NB: If TEST/AND/CMP/ADD/SUB/INC/DEC is of RIP relative address,
9141 or is one of the following format:
9142
9143 cmp m, imm
9144 add m, imm
9145 sub m, imm
9146 test m, imm
9147 and m, imm
9148 inc m
9149 dec m
9150
9151 it is unfusible. */
e379e5f3
L
9152
9153static int
79d72f45 9154maybe_fused_with_jcc_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
9155{
9156 /* No RIP address. */
9157 if (i.base_reg && i.base_reg->reg_num == RegIP)
9158 return 0;
9159
389d00a5
JB
9160 /* No opcodes outside of base encoding space. */
9161 if (i.tm.opcode_modifier.opcodespace != SPACE_BASE)
e379e5f3
L
9162 return 0;
9163
79d72f45
HL
9164 /* add, sub without add/sub m, imm. */
9165 if (i.tm.base_opcode <= 5
e379e5f3
L
9166 || (i.tm.base_opcode >= 0x28 && i.tm.base_opcode <= 0x2d)
9167 || ((i.tm.base_opcode | 3) == 0x83
79d72f45 9168 && (i.tm.extension_opcode == 0x5
e379e5f3 9169 || i.tm.extension_opcode == 0x0)))
79d72f45
HL
9170 {
9171 *mf_cmp_p = mf_cmp_alu_cmp;
9172 return !(i.mem_operands && i.imm_operands);
9173 }
e379e5f3 9174
79d72f45
HL
9175 /* and without and m, imm. */
9176 if ((i.tm.base_opcode >= 0x20 && i.tm.base_opcode <= 0x25)
9177 || ((i.tm.base_opcode | 3) == 0x83
9178 && i.tm.extension_opcode == 0x4))
9179 {
9180 *mf_cmp_p = mf_cmp_test_and;
9181 return !(i.mem_operands && i.imm_operands);
9182 }
9183
9184 /* test without test m imm. */
e379e5f3
L
9185 if ((i.tm.base_opcode | 1) == 0x85
9186 || (i.tm.base_opcode | 1) == 0xa9
9187 || ((i.tm.base_opcode | 1) == 0xf7
79d72f45
HL
9188 && i.tm.extension_opcode == 0))
9189 {
9190 *mf_cmp_p = mf_cmp_test_and;
9191 return !(i.mem_operands && i.imm_operands);
9192 }
9193
9194 /* cmp without cmp m, imm. */
9195 if ((i.tm.base_opcode >= 0x38 && i.tm.base_opcode <= 0x3d)
e379e5f3
L
9196 || ((i.tm.base_opcode | 3) == 0x83
9197 && (i.tm.extension_opcode == 0x7)))
79d72f45
HL
9198 {
9199 *mf_cmp_p = mf_cmp_alu_cmp;
9200 return !(i.mem_operands && i.imm_operands);
9201 }
e379e5f3 9202
79d72f45 9203 /* inc, dec without inc/dec m. */
e379e5f3
L
9204 if ((i.tm.cpu_flags.bitfield.cpuno64
9205 && (i.tm.base_opcode | 0xf) == 0x4f)
9206 || ((i.tm.base_opcode | 1) == 0xff
9207 && i.tm.extension_opcode <= 0x1))
79d72f45
HL
9208 {
9209 *mf_cmp_p = mf_cmp_incdec;
9210 return !i.mem_operands;
9211 }
e379e5f3
L
9212
9213 return 0;
9214}
9215
9216/* Return 1 if a FUSED_JCC_PADDING frag should be generated. */
9217
9218static int
79d72f45 9219add_fused_jcc_padding_frag_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
9220{
9221 /* NB: Don't work with COND_JUMP86 without i386. */
9222 if (!align_branch_power
9223 || now_seg == absolute_section
9224 || !cpu_arch_flags.bitfield.cpui386
9225 || !(align_branch & align_branch_fused_bit))
9226 return 0;
9227
79d72f45 9228 if (maybe_fused_with_jcc_p (mf_cmp_p))
e379e5f3
L
9229 {
9230 if (last_insn.kind == last_insn_other
9231 || last_insn.seg != now_seg)
9232 return 1;
9233 if (flag_debug)
9234 as_warn_where (last_insn.file, last_insn.line,
9235 _("`%s` skips -malign-branch-boundary on `%s`"),
9236 last_insn.name, i.tm.name);
9237 }
9238
9239 return 0;
9240}
9241
9242/* Return 1 if a BRANCH_PREFIX frag should be generated. */
9243
9244static int
9245add_branch_prefix_frag_p (void)
9246{
9247 /* NB: Don't work with COND_JUMP86 without i386. Don't add prefix
9248 to PadLock instructions since they include prefixes in opcode. */
9249 if (!align_branch_power
9250 || !align_branch_prefix_size
9251 || now_seg == absolute_section
9252 || i.tm.cpu_flags.bitfield.cpupadlock
9253 || !cpu_arch_flags.bitfield.cpui386)
9254 return 0;
9255
9256 /* Don't add prefix if it is a prefix or there is no operand in case
9257 that segment prefix is special. */
9258 if (!i.operands || i.tm.opcode_modifier.isprefix)
9259 return 0;
9260
9261 if (last_insn.kind == last_insn_other
9262 || last_insn.seg != now_seg)
9263 return 1;
9264
9265 if (flag_debug)
9266 as_warn_where (last_insn.file, last_insn.line,
9267 _("`%s` skips -malign-branch-boundary on `%s`"),
9268 last_insn.name, i.tm.name);
9269
9270 return 0;
9271}
9272
9273/* Return 1 if a BRANCH_PADDING frag should be generated. */
9274
9275static int
79d72f45
HL
9276add_branch_padding_frag_p (enum align_branch_kind *branch_p,
9277 enum mf_jcc_kind *mf_jcc_p)
e379e5f3
L
9278{
9279 int add_padding;
9280
9281 /* NB: Don't work with COND_JUMP86 without i386. */
9282 if (!align_branch_power
9283 || now_seg == absolute_section
389d00a5
JB
9284 || !cpu_arch_flags.bitfield.cpui386
9285 || i.tm.opcode_modifier.opcodespace != SPACE_BASE)
e379e5f3
L
9286 return 0;
9287
9288 add_padding = 0;
9289
9290 /* Check for jcc and direct jmp. */
9291 if (i.tm.opcode_modifier.jump == JUMP)
9292 {
9293 if (i.tm.base_opcode == JUMP_PC_RELATIVE)
9294 {
9295 *branch_p = align_branch_jmp;
9296 add_padding = align_branch & align_branch_jmp_bit;
9297 }
9298 else
9299 {
79d72f45
HL
9300 /* Because J<cc> and JN<cc> share same group in macro-fusible table,
9301 igore the lowest bit. */
9302 *mf_jcc_p = (i.tm.base_opcode & 0x0e) >> 1;
e379e5f3
L
9303 *branch_p = align_branch_jcc;
9304 if ((align_branch & align_branch_jcc_bit))
9305 add_padding = 1;
9306 }
9307 }
e379e5f3
L
9308 else if ((i.tm.base_opcode | 1) == 0xc3)
9309 {
9310 /* Near ret. */
9311 *branch_p = align_branch_ret;
9312 if ((align_branch & align_branch_ret_bit))
9313 add_padding = 1;
9314 }
9315 else
9316 {
9317 /* Check for indirect jmp, direct and indirect calls. */
9318 if (i.tm.base_opcode == 0xe8)
9319 {
9320 /* Direct call. */
9321 *branch_p = align_branch_call;
9322 if ((align_branch & align_branch_call_bit))
9323 add_padding = 1;
9324 }
9325 else if (i.tm.base_opcode == 0xff
9326 && (i.tm.extension_opcode == 2
9327 || i.tm.extension_opcode == 4))
9328 {
9329 /* Indirect call and jmp. */
9330 *branch_p = align_branch_indirect;
9331 if ((align_branch & align_branch_indirect_bit))
9332 add_padding = 1;
9333 }
9334
9335 if (add_padding
9336 && i.disp_operands
9337 && tls_get_addr
9338 && (i.op[0].disps->X_op == O_symbol
9339 || (i.op[0].disps->X_op == O_subtract
9340 && i.op[0].disps->X_op_symbol == GOT_symbol)))
9341 {
9342 symbolS *s = i.op[0].disps->X_add_symbol;
9343 /* No padding to call to global or undefined tls_get_addr. */
9344 if ((S_IS_EXTERNAL (s) || !S_IS_DEFINED (s))
9345 && strcmp (S_GET_NAME (s), tls_get_addr) == 0)
9346 return 0;
9347 }
9348 }
9349
9350 if (add_padding
9351 && last_insn.kind != last_insn_other
9352 && last_insn.seg == now_seg)
9353 {
9354 if (flag_debug)
9355 as_warn_where (last_insn.file, last_insn.line,
9356 _("`%s` skips -malign-branch-boundary on `%s`"),
9357 last_insn.name, i.tm.name);
9358 return 0;
9359 }
9360
9361 return add_padding;
9362}
9363
29b0f896 9364static void
e3bb37b5 9365output_insn (void)
29b0f896 9366{
2bbd9c25
JJ
9367 fragS *insn_start_frag;
9368 offsetT insn_start_off;
e379e5f3
L
9369 fragS *fragP = NULL;
9370 enum align_branch_kind branch = align_branch_none;
79d72f45
HL
9371 /* The initializer is arbitrary just to avoid uninitialized error.
9372 it's actually either assigned in add_branch_padding_frag_p
9373 or never be used. */
9374 enum mf_jcc_kind mf_jcc = mf_jcc_jo;
2bbd9c25 9375
b4a3a7b4 9376#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
48ef937e 9377 if (IS_ELF && x86_used_note && now_seg != absolute_section)
b4a3a7b4 9378 {
32930e4e
L
9379 if ((i.xstate & xstate_tmm) == xstate_tmm
9380 || i.tm.cpu_flags.bitfield.cpuamx_tile)
9381 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_TMM;
9382
b4a3a7b4
L
9383 if (i.tm.cpu_flags.bitfield.cpu8087
9384 || i.tm.cpu_flags.bitfield.cpu287
9385 || i.tm.cpu_flags.bitfield.cpu387
9386 || i.tm.cpu_flags.bitfield.cpu687
9387 || i.tm.cpu_flags.bitfield.cpufisttp)
9388 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X87;
014d61ea 9389
921eafea 9390 if ((i.xstate & xstate_mmx)
389d00a5
JB
9391 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
9392 && !is_any_vex_encoding (&i.tm)
9393 && (i.tm.base_opcode == 0x77 /* emms */
9394 || i.tm.base_opcode == 0x0e /* femms */)))
b4a3a7b4 9395 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MMX;
014d61ea 9396
32930e4e
L
9397 if (i.index_reg)
9398 {
9399 if (i.index_reg->reg_type.bitfield.zmmword)
9400 i.xstate |= xstate_zmm;
9401 else if (i.index_reg->reg_type.bitfield.ymmword)
9402 i.xstate |= xstate_ymm;
9403 else if (i.index_reg->reg_type.bitfield.xmmword)
9404 i.xstate |= xstate_xmm;
9405 }
014d61ea
JB
9406
9407 /* vzeroall / vzeroupper */
9408 if (i.tm.base_opcode == 0x77 && i.tm.cpu_flags.bitfield.cpuavx)
9409 i.xstate |= xstate_ymm;
9410
c4694f17 9411 if ((i.xstate & xstate_xmm)
389d00a5
JB
9412 /* ldmxcsr / stmxcsr / vldmxcsr / vstmxcsr */
9413 || (i.tm.base_opcode == 0xae
9414 && (i.tm.cpu_flags.bitfield.cpusse
9415 || i.tm.cpu_flags.bitfield.cpuavx))
c4694f17
TG
9416 || i.tm.cpu_flags.bitfield.cpuwidekl
9417 || i.tm.cpu_flags.bitfield.cpukl)
b4a3a7b4 9418 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XMM;
014d61ea 9419
921eafea 9420 if ((i.xstate & xstate_ymm) == xstate_ymm)
b4a3a7b4 9421 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_YMM;
921eafea 9422 if ((i.xstate & xstate_zmm) == xstate_zmm)
b4a3a7b4 9423 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_ZMM;
6225c532 9424 if (i.mask.reg || (i.xstate & xstate_mask) == xstate_mask)
32930e4e 9425 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MASK;
b4a3a7b4
L
9426 if (i.tm.cpu_flags.bitfield.cpufxsr)
9427 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_FXSR;
9428 if (i.tm.cpu_flags.bitfield.cpuxsave)
9429 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVE;
9430 if (i.tm.cpu_flags.bitfield.cpuxsaveopt)
9431 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT;
9432 if (i.tm.cpu_flags.bitfield.cpuxsavec)
9433 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEC;
b0ab0693
L
9434
9435 if (x86_feature_2_used
9436 || i.tm.cpu_flags.bitfield.cpucmov
9437 || i.tm.cpu_flags.bitfield.cpusyscall
389d00a5
JB
9438 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
9439 && i.tm.base_opcode == 0xc7
70e95837 9440 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
b0ab0693
L
9441 && i.tm.extension_opcode == 1) /* cmpxchg8b */)
9442 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_BASELINE;
9443 if (i.tm.cpu_flags.bitfield.cpusse3
9444 || i.tm.cpu_flags.bitfield.cpussse3
9445 || i.tm.cpu_flags.bitfield.cpusse4_1
9446 || i.tm.cpu_flags.bitfield.cpusse4_2
9447 || i.tm.cpu_flags.bitfield.cpucx16
9448 || i.tm.cpu_flags.bitfield.cpupopcnt
9449 /* LAHF-SAHF insns in 64-bit mode. */
9450 || (flag_code == CODE_64BIT
35648716
JB
9451 && (i.tm.base_opcode | 1) == 0x9f
9452 && i.tm.opcode_modifier.opcodespace == SPACE_BASE))
b0ab0693
L
9453 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V2;
9454 if (i.tm.cpu_flags.bitfield.cpuavx
9455 || i.tm.cpu_flags.bitfield.cpuavx2
a9860005
JB
9456 /* Any VEX encoded insns execpt for AVX512F, AVX512BW, AVX512DQ,
9457 XOP, FMA4, LPW, TBM, and AMX. */
b0ab0693
L
9458 || (i.tm.opcode_modifier.vex
9459 && !i.tm.cpu_flags.bitfield.cpuavx512f
9460 && !i.tm.cpu_flags.bitfield.cpuavx512bw
9461 && !i.tm.cpu_flags.bitfield.cpuavx512dq
a9860005
JB
9462 && !i.tm.cpu_flags.bitfield.cpuxop
9463 && !i.tm.cpu_flags.bitfield.cpufma4
b0ab0693
L
9464 && !i.tm.cpu_flags.bitfield.cpulwp
9465 && !i.tm.cpu_flags.bitfield.cputbm
9466 && !(x86_feature_2_used & GNU_PROPERTY_X86_FEATURE_2_TMM))
9467 || i.tm.cpu_flags.bitfield.cpuf16c
9468 || i.tm.cpu_flags.bitfield.cpufma
9469 || i.tm.cpu_flags.bitfield.cpulzcnt
9470 || i.tm.cpu_flags.bitfield.cpumovbe
9471 || i.tm.cpu_flags.bitfield.cpuxsaves
9472 || (x86_feature_2_used
9473 & (GNU_PROPERTY_X86_FEATURE_2_XSAVE
9474 | GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT
9475 | GNU_PROPERTY_X86_FEATURE_2_XSAVEC)) != 0)
9476 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V3;
9477 if (i.tm.cpu_flags.bitfield.cpuavx512f
9478 || i.tm.cpu_flags.bitfield.cpuavx512bw
9479 || i.tm.cpu_flags.bitfield.cpuavx512dq
9480 || i.tm.cpu_flags.bitfield.cpuavx512vl
a9860005
JB
9481 /* Any EVEX encoded insns except for AVX512ER, AVX512PF,
9482 AVX512-4FMAPS, and AVX512-4VNNIW. */
b0ab0693
L
9483 || (i.tm.opcode_modifier.evex
9484 && !i.tm.cpu_flags.bitfield.cpuavx512er
9485 && !i.tm.cpu_flags.bitfield.cpuavx512pf
a9860005 9486 && !i.tm.cpu_flags.bitfield.cpuavx512_4fmaps
b0ab0693
L
9487 && !i.tm.cpu_flags.bitfield.cpuavx512_4vnniw))
9488 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V4;
b4a3a7b4
L
9489 }
9490#endif
9491
29b0f896
AM
9492 /* Tie dwarf2 debug info to the address at the start of the insn.
9493 We can't do this after the insn has been output as the current
9494 frag may have been closed off. eg. by frag_var. */
9495 dwarf2_emit_insn (0);
9496
2bbd9c25
JJ
9497 insn_start_frag = frag_now;
9498 insn_start_off = frag_now_fix ();
9499
79d72f45 9500 if (add_branch_padding_frag_p (&branch, &mf_jcc))
e379e5f3
L
9501 {
9502 char *p;
9503 /* Branch can be 8 bytes. Leave some room for prefixes. */
9504 unsigned int max_branch_padding_size = 14;
9505
9506 /* Align section to boundary. */
9507 record_alignment (now_seg, align_branch_power);
9508
9509 /* Make room for padding. */
9510 frag_grow (max_branch_padding_size);
9511
9512 /* Start of the padding. */
9513 p = frag_more (0);
9514
9515 fragP = frag_now;
9516
9517 frag_var (rs_machine_dependent, max_branch_padding_size, 0,
9518 ENCODE_RELAX_STATE (BRANCH_PADDING, 0),
9519 NULL, 0, p);
9520
79d72f45 9521 fragP->tc_frag_data.mf_type = mf_jcc;
e379e5f3
L
9522 fragP->tc_frag_data.branch_type = branch;
9523 fragP->tc_frag_data.max_bytes = max_branch_padding_size;
9524 }
9525
d59a54c2
JB
9526 if (!cpu_arch_flags.bitfield.cpui386 && (flag_code != CODE_16BIT)
9527 && !pre_386_16bit_warned)
9528 {
9529 as_warn (_("use .code16 to ensure correct addressing mode"));
9530 pre_386_16bit_warned = true;
9531 }
9532
29b0f896 9533 /* Output jumps. */
0cfa3eb3 9534 if (i.tm.opcode_modifier.jump == JUMP)
29b0f896 9535 output_branch ();
0cfa3eb3
JB
9536 else if (i.tm.opcode_modifier.jump == JUMP_BYTE
9537 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896 9538 output_jump ();
0cfa3eb3 9539 else if (i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT)
29b0f896
AM
9540 output_interseg_jump ();
9541 else
9542 {
9543 /* Output normal instructions here. */
9544 char *p;
9545 unsigned char *q;
47465058 9546 unsigned int j;
79d72f45 9547 enum mf_cmp_kind mf_cmp;
4dffcebc 9548
e4e00185 9549 if (avoid_fence
389d00a5
JB
9550 && (i.tm.base_opcode == 0xaee8
9551 || i.tm.base_opcode == 0xaef0
9552 || i.tm.base_opcode == 0xaef8))
48ef937e
JB
9553 {
9554 /* Encode lfence, mfence, and sfence as
9555 f0 83 04 24 00 lock addl $0x0, (%{re}sp). */
47f4115a
JB
9556 if (flag_code == CODE_16BIT)
9557 as_bad (_("Cannot convert `%s' in 16-bit mode"), i.tm.name);
9558 else if (omit_lock_prefix)
9559 as_bad (_("Cannot convert `%s' with `-momit-lock-prefix=yes' in effect"),
9560 i.tm.name);
9561 else if (now_seg != absolute_section)
48ef937e
JB
9562 {
9563 offsetT val = 0x240483f0ULL;
9564
9565 p = frag_more (5);
9566 md_number_to_chars (p, val, 5);
9567 }
9568 else
9569 abs_section_offset += 5;
9570 return;
9571 }
e4e00185 9572
d022bddd
IT
9573 /* Some processors fail on LOCK prefix. This options makes
9574 assembler ignore LOCK prefix and serves as a workaround. */
9575 if (omit_lock_prefix)
9576 {
35648716
JB
9577 if (i.tm.base_opcode == LOCK_PREFIX_OPCODE
9578 && i.tm.opcode_modifier.isprefix)
d022bddd
IT
9579 return;
9580 i.prefix[LOCK_PREFIX] = 0;
9581 }
9582
e379e5f3
L
9583 if (branch)
9584 /* Skip if this is a branch. */
9585 ;
79d72f45 9586 else if (add_fused_jcc_padding_frag_p (&mf_cmp))
e379e5f3
L
9587 {
9588 /* Make room for padding. */
9589 frag_grow (MAX_FUSED_JCC_PADDING_SIZE);
9590 p = frag_more (0);
9591
9592 fragP = frag_now;
9593
9594 frag_var (rs_machine_dependent, MAX_FUSED_JCC_PADDING_SIZE, 0,
9595 ENCODE_RELAX_STATE (FUSED_JCC_PADDING, 0),
9596 NULL, 0, p);
9597
79d72f45 9598 fragP->tc_frag_data.mf_type = mf_cmp;
e379e5f3
L
9599 fragP->tc_frag_data.branch_type = align_branch_fused;
9600 fragP->tc_frag_data.max_bytes = MAX_FUSED_JCC_PADDING_SIZE;
9601 }
9602 else if (add_branch_prefix_frag_p ())
9603 {
9604 unsigned int max_prefix_size = align_branch_prefix_size;
9605
9606 /* Make room for padding. */
9607 frag_grow (max_prefix_size);
9608 p = frag_more (0);
9609
9610 fragP = frag_now;
9611
9612 frag_var (rs_machine_dependent, max_prefix_size, 0,
9613 ENCODE_RELAX_STATE (BRANCH_PREFIX, 0),
9614 NULL, 0, p);
9615
9616 fragP->tc_frag_data.max_bytes = max_prefix_size;
9617 }
9618
43234a1e
L
9619 /* Since the VEX/EVEX prefix contains the implicit prefix, we
9620 don't need the explicit prefix. */
cf665fee 9621 if (!is_any_vex_encoding (&i.tm))
bc4bd9ab 9622 {
7b47a312 9623 switch (i.tm.opcode_modifier.opcodeprefix)
bc4bd9ab 9624 {
7b47a312
L
9625 case PREFIX_0X66:
9626 add_prefix (0x66);
9627 break;
9628 case PREFIX_0XF2:
9629 add_prefix (0xf2);
9630 break;
9631 case PREFIX_0XF3:
8b65b895
L
9632 if (!i.tm.cpu_flags.bitfield.cpupadlock
9633 || (i.prefix[REP_PREFIX] != 0xf3))
9634 add_prefix (0xf3);
c0f3af97 9635 break;
7b47a312 9636 case PREFIX_NONE:
9a182d04 9637 switch (i.opcode_length)
c0f3af97 9638 {
7b47a312 9639 case 2:
7b47a312 9640 break;
9a182d04 9641 case 1:
7b47a312 9642 /* Check for pseudo prefixes. */
9a182d04
JB
9643 if (!i.tm.opcode_modifier.isprefix || i.tm.base_opcode)
9644 break;
7b47a312
L
9645 as_bad_where (insn_start_frag->fr_file,
9646 insn_start_frag->fr_line,
9647 _("pseudo prefix without instruction"));
9648 return;
9649 default:
9650 abort ();
4dffcebc 9651 }
c0f3af97 9652 break;
c0f3af97
L
9653 default:
9654 abort ();
bc4bd9ab 9655 }
c0f3af97 9656
6d19a37a 9657#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
cf61b747
L
9658 /* For x32, add a dummy REX_OPCODE prefix for mov/add with
9659 R_X86_64_GOTTPOFF relocation so that linker can safely
14470f07
L
9660 perform IE->LE optimization. A dummy REX_OPCODE prefix
9661 is also needed for lea with R_X86_64_GOTPC32_TLSDESC
9662 relocation for GDesc -> IE/LE optimization. */
cf61b747
L
9663 if (x86_elf_abi == X86_64_X32_ABI
9664 && i.operands == 2
14470f07
L
9665 && (i.reloc[0] == BFD_RELOC_X86_64_GOTTPOFF
9666 || i.reloc[0] == BFD_RELOC_X86_64_GOTPC32_TLSDESC)
cf61b747
L
9667 && i.prefix[REX_PREFIX] == 0)
9668 add_prefix (REX_OPCODE);
6d19a37a 9669#endif
cf61b747 9670
c0f3af97
L
9671 /* The prefix bytes. */
9672 for (j = ARRAY_SIZE (i.prefix), q = i.prefix; j > 0; j--, q++)
9673 if (*q)
48ef937e 9674 frag_opcode_byte (*q);
0f10071e 9675 }
ae5c1c7b 9676 else
c0f3af97
L
9677 {
9678 for (j = 0, q = i.prefix; j < ARRAY_SIZE (i.prefix); j++, q++)
9679 if (*q)
9680 switch (j)
9681 {
c0f3af97
L
9682 case SEG_PREFIX:
9683 case ADDR_PREFIX:
48ef937e 9684 frag_opcode_byte (*q);
c0f3af97
L
9685 break;
9686 default:
9687 /* There should be no other prefixes for instructions
9688 with VEX prefix. */
9689 abort ();
9690 }
9691
43234a1e
L
9692 /* For EVEX instructions i.vrex should become 0 after
9693 build_evex_prefix. For VEX instructions upper 16 registers
9694 aren't available, so VREX should be 0. */
9695 if (i.vrex)
9696 abort ();
c0f3af97 9697 /* Now the VEX prefix. */
48ef937e
JB
9698 if (now_seg != absolute_section)
9699 {
9700 p = frag_more (i.vex.length);
9701 for (j = 0; j < i.vex.length; j++)
9702 p[j] = i.vex.bytes[j];
9703 }
9704 else
9705 abs_section_offset += i.vex.length;
c0f3af97 9706 }
252b5132 9707
29b0f896 9708 /* Now the opcode; be careful about word order here! */
389d00a5
JB
9709 j = i.opcode_length;
9710 if (!i.vex.length)
9711 switch (i.tm.opcode_modifier.opcodespace)
9712 {
9713 case SPACE_BASE:
9714 break;
9715 case SPACE_0F:
9716 ++j;
9717 break;
9718 case SPACE_0F38:
9719 case SPACE_0F3A:
9720 j += 2;
9721 break;
9722 default:
9723 abort ();
9724 }
9725
48ef937e 9726 if (now_seg == absolute_section)
389d00a5
JB
9727 abs_section_offset += j;
9728 else if (j == 1)
29b0f896
AM
9729 {
9730 FRAG_APPEND_1_CHAR (i.tm.base_opcode);
9731 }
9732 else
9733 {
389d00a5
JB
9734 p = frag_more (j);
9735 if (!i.vex.length
9736 && i.tm.opcode_modifier.opcodespace != SPACE_BASE)
9737 {
9738 *p++ = 0x0f;
9739 if (i.tm.opcode_modifier.opcodespace != SPACE_0F)
9740 *p++ = i.tm.opcode_modifier.opcodespace == SPACE_0F38
9741 ? 0x38 : 0x3a;
9742 }
9743
9a182d04 9744 switch (i.opcode_length)
331d2d0d 9745 {
4dffcebc 9746 case 2:
389d00a5
JB
9747 /* Put out high byte first: can't use md_number_to_chars! */
9748 *p++ = (i.tm.base_opcode >> 8) & 0xff;
9749 /* Fall through. */
9750 case 1:
9751 *p = i.tm.base_opcode & 0xff;
4dffcebc
L
9752 break;
9753 default:
9754 abort ();
9755 break;
331d2d0d 9756 }
0f10071e 9757
29b0f896 9758 }
3e73aa7c 9759
29b0f896 9760 /* Now the modrm byte and sib byte (if present). */
40fb9820 9761 if (i.tm.opcode_modifier.modrm)
29b0f896 9762 {
48ef937e
JB
9763 frag_opcode_byte ((i.rm.regmem << 0)
9764 | (i.rm.reg << 3)
9765 | (i.rm.mode << 6));
29b0f896
AM
9766 /* If i.rm.regmem == ESP (4)
9767 && i.rm.mode != (Register mode)
9768 && not 16 bit
9769 ==> need second modrm byte. */
9770 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
9771 && i.rm.mode != 3
dc821c5f 9772 && !(i.base_reg && i.base_reg->reg_type.bitfield.word))
48ef937e
JB
9773 frag_opcode_byte ((i.sib.base << 0)
9774 | (i.sib.index << 3)
9775 | (i.sib.scale << 6));
29b0f896 9776 }
3e73aa7c 9777
29b0f896 9778 if (i.disp_operands)
2bbd9c25 9779 output_disp (insn_start_frag, insn_start_off);
3e73aa7c 9780
29b0f896 9781 if (i.imm_operands)
2bbd9c25 9782 output_imm (insn_start_frag, insn_start_off);
9c33702b
JB
9783
9784 /*
9785 * frag_now_fix () returning plain abs_section_offset when we're in the
9786 * absolute section, and abs_section_offset not getting updated as data
9787 * gets added to the frag breaks the logic below.
9788 */
9789 if (now_seg != absolute_section)
9790 {
9791 j = encoding_length (insn_start_frag, insn_start_off, frag_more (0));
9792 if (j > 15)
9793 as_warn (_("instruction length of %u bytes exceeds the limit of 15"),
9794 j);
e379e5f3
L
9795 else if (fragP)
9796 {
9797 /* NB: Don't add prefix with GOTPC relocation since
9798 output_disp() above depends on the fixed encoding
9799 length. Can't add prefix with TLS relocation since
9800 it breaks TLS linker optimization. */
9801 unsigned int max = i.has_gotpc_tls_reloc ? 0 : 15 - j;
9802 /* Prefix count on the current instruction. */
9803 unsigned int count = i.vex.length;
9804 unsigned int k;
9805 for (k = 0; k < ARRAY_SIZE (i.prefix); k++)
9806 /* REX byte is encoded in VEX/EVEX prefix. */
9807 if (i.prefix[k] && (k != REX_PREFIX || !i.vex.length))
9808 count++;
9809
9810 /* Count prefixes for extended opcode maps. */
9811 if (!i.vex.length)
389d00a5 9812 switch (i.tm.opcode_modifier.opcodespace)
e379e5f3 9813 {
389d00a5 9814 case SPACE_BASE:
e379e5f3 9815 break;
389d00a5
JB
9816 case SPACE_0F:
9817 count++;
e379e5f3 9818 break;
389d00a5
JB
9819 case SPACE_0F38:
9820 case SPACE_0F3A:
9821 count += 2;
e379e5f3
L
9822 break;
9823 default:
9824 abort ();
9825 }
9826
9827 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
9828 == BRANCH_PREFIX)
9829 {
9830 /* Set the maximum prefix size in BRANCH_PREFIX
9831 frag. */
9832 if (fragP->tc_frag_data.max_bytes > max)
9833 fragP->tc_frag_data.max_bytes = max;
9834 if (fragP->tc_frag_data.max_bytes > count)
9835 fragP->tc_frag_data.max_bytes -= count;
9836 else
9837 fragP->tc_frag_data.max_bytes = 0;
9838 }
9839 else
9840 {
9841 /* Remember the maximum prefix size in FUSED_JCC_PADDING
9842 frag. */
9843 unsigned int max_prefix_size;
9844 if (align_branch_prefix_size > max)
9845 max_prefix_size = max;
9846 else
9847 max_prefix_size = align_branch_prefix_size;
9848 if (max_prefix_size > count)
9849 fragP->tc_frag_data.max_prefix_length
9850 = max_prefix_size - count;
9851 }
9852
9853 /* Use existing segment prefix if possible. Use CS
9854 segment prefix in 64-bit mode. In 32-bit mode, use SS
9855 segment prefix with ESP/EBP base register and use DS
9856 segment prefix without ESP/EBP base register. */
9857 if (i.prefix[SEG_PREFIX])
9858 fragP->tc_frag_data.default_prefix = i.prefix[SEG_PREFIX];
9859 else if (flag_code == CODE_64BIT)
9860 fragP->tc_frag_data.default_prefix = CS_PREFIX_OPCODE;
9861 else if (i.base_reg
9862 && (i.base_reg->reg_num == 4
9863 || i.base_reg->reg_num == 5))
9864 fragP->tc_frag_data.default_prefix = SS_PREFIX_OPCODE;
9865 else
9866 fragP->tc_frag_data.default_prefix = DS_PREFIX_OPCODE;
9867 }
9c33702b 9868 }
29b0f896 9869 }
252b5132 9870
e379e5f3
L
9871 /* NB: Don't work with COND_JUMP86 without i386. */
9872 if (align_branch_power
9873 && now_seg != absolute_section
9874 && cpu_arch_flags.bitfield.cpui386)
9875 {
9876 /* Terminate each frag so that we can add prefix and check for
9877 fused jcc. */
9878 frag_wane (frag_now);
9879 frag_new (0);
9880 }
9881
29b0f896
AM
9882#ifdef DEBUG386
9883 if (flag_debug)
9884 {
7b81dfbb 9885 pi ("" /*line*/, &i);
29b0f896
AM
9886 }
9887#endif /* DEBUG386 */
9888}
252b5132 9889
e205caa7
L
9890/* Return the size of the displacement operand N. */
9891
9892static int
9893disp_size (unsigned int n)
9894{
9895 int size = 4;
43234a1e 9896
b5014f7a 9897 if (i.types[n].bitfield.disp64)
40fb9820
L
9898 size = 8;
9899 else if (i.types[n].bitfield.disp8)
9900 size = 1;
9901 else if (i.types[n].bitfield.disp16)
9902 size = 2;
e205caa7
L
9903 return size;
9904}
9905
9906/* Return the size of the immediate operand N. */
9907
9908static int
9909imm_size (unsigned int n)
9910{
9911 int size = 4;
40fb9820
L
9912 if (i.types[n].bitfield.imm64)
9913 size = 8;
9914 else if (i.types[n].bitfield.imm8 || i.types[n].bitfield.imm8s)
9915 size = 1;
9916 else if (i.types[n].bitfield.imm16)
9917 size = 2;
e205caa7
L
9918 return size;
9919}
9920
29b0f896 9921static void
64e74474 9922output_disp (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
9923{
9924 char *p;
9925 unsigned int n;
252b5132 9926
29b0f896
AM
9927 for (n = 0; n < i.operands; n++)
9928 {
b5014f7a 9929 if (operand_type_check (i.types[n], disp))
29b0f896 9930 {
48ef937e
JB
9931 int size = disp_size (n);
9932
9933 if (now_seg == absolute_section)
9934 abs_section_offset += size;
9935 else if (i.op[n].disps->X_op == O_constant)
29b0f896 9936 {
43234a1e 9937 offsetT val = i.op[n].disps->X_add_number;
252b5132 9938
629cfaf1
JB
9939 val = offset_in_range (val >> (size == 1 ? i.memshift : 0),
9940 size);
29b0f896
AM
9941 p = frag_more (size);
9942 md_number_to_chars (p, val, size);
9943 }
9944 else
9945 {
f86103b7 9946 enum bfd_reloc_code_real reloc_type;
a775efc8
JB
9947 bool pcrel = (i.flags[n] & Operand_PCrel) != 0;
9948 bool sign = (flag_code == CODE_64BIT && size == 4
9949 && (!want_disp32 (&i.tm)
9950 || (i.tm.opcode_modifier.jump && !i.jumpabsolute
9951 && !i.types[n].bitfield.baseindex)))
9952 || pcrel;
02a86693 9953 fixS *fixP;
29b0f896 9954
e205caa7 9955 /* We can't have 8 bit displacement here. */
9c2799c2 9956 gas_assert (!i.types[n].bitfield.disp8);
e205caa7 9957
29b0f896
AM
9958 /* The PC relative address is computed relative
9959 to the instruction boundary, so in case immediate
9960 fields follows, we need to adjust the value. */
9961 if (pcrel && i.imm_operands)
9962 {
29b0f896 9963 unsigned int n1;
e205caa7 9964 int sz = 0;
252b5132 9965
29b0f896 9966 for (n1 = 0; n1 < i.operands; n1++)
40fb9820 9967 if (operand_type_check (i.types[n1], imm))
252b5132 9968 {
e205caa7
L
9969 /* Only one immediate is allowed for PC
9970 relative address. */
9c2799c2 9971 gas_assert (sz == 0);
e205caa7
L
9972 sz = imm_size (n1);
9973 i.op[n].disps->X_add_number -= sz;
252b5132 9974 }
29b0f896 9975 /* We should find the immediate. */
9c2799c2 9976 gas_assert (sz != 0);
29b0f896 9977 }
520dc8e8 9978
29b0f896 9979 p = frag_more (size);
d258b828 9980 reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
d6ab8113 9981 if (GOT_symbol
2bbd9c25 9982 && GOT_symbol == i.op[n].disps->X_add_symbol
d6ab8113 9983 && (((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
9984 || reloc_type == BFD_RELOC_X86_64_32S
9985 || (reloc_type == BFD_RELOC_64
9986 && object_64bit))
d6ab8113
JB
9987 && (i.op[n].disps->X_op == O_symbol
9988 || (i.op[n].disps->X_op == O_add
9989 && ((symbol_get_value_expression
9990 (i.op[n].disps->X_op_symbol)->X_op)
9991 == O_subtract))))
9992 || reloc_type == BFD_RELOC_32_PCREL))
2bbd9c25 9993 {
4fa24527 9994 if (!object_64bit)
7b81dfbb
AJ
9995 {
9996 reloc_type = BFD_RELOC_386_GOTPC;
5b7c81bd 9997 i.has_gotpc_tls_reloc = true;
98da05bf 9998 i.op[n].disps->X_add_number +=
d583596c 9999 encoding_length (insn_start_frag, insn_start_off, p);
7b81dfbb
AJ
10000 }
10001 else if (reloc_type == BFD_RELOC_64)
10002 reloc_type = BFD_RELOC_X86_64_GOTPC64;
d6ab8113 10003 else
7b81dfbb
AJ
10004 /* Don't do the adjustment for x86-64, as there
10005 the pcrel addressing is relative to the _next_
10006 insn, and that is taken care of in other code. */
d6ab8113 10007 reloc_type = BFD_RELOC_X86_64_GOTPC32;
2bbd9c25 10008 }
e379e5f3
L
10009 else if (align_branch_power)
10010 {
10011 switch (reloc_type)
10012 {
10013 case BFD_RELOC_386_TLS_GD:
10014 case BFD_RELOC_386_TLS_LDM:
10015 case BFD_RELOC_386_TLS_IE:
10016 case BFD_RELOC_386_TLS_IE_32:
10017 case BFD_RELOC_386_TLS_GOTIE:
10018 case BFD_RELOC_386_TLS_GOTDESC:
10019 case BFD_RELOC_386_TLS_DESC_CALL:
10020 case BFD_RELOC_X86_64_TLSGD:
10021 case BFD_RELOC_X86_64_TLSLD:
10022 case BFD_RELOC_X86_64_GOTTPOFF:
10023 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
10024 case BFD_RELOC_X86_64_TLSDESC_CALL:
5b7c81bd 10025 i.has_gotpc_tls_reloc = true;
e379e5f3
L
10026 default:
10027 break;
10028 }
10029 }
02a86693
L
10030 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal,
10031 size, i.op[n].disps, pcrel,
10032 reloc_type);
eb19308f
JB
10033
10034 if (flag_code == CODE_64BIT && size == 4 && pcrel
10035 && !i.prefix[ADDR_PREFIX])
10036 fixP->fx_signed = 1;
10037
02a86693
L
10038 /* Check for "call/jmp *mem", "mov mem, %reg",
10039 "test %reg, mem" and "binop mem, %reg" where binop
10040 is one of adc, add, and, cmp, or, sbb, sub, xor
e60f4d3b
L
10041 instructions without data prefix. Always generate
10042 R_386_GOT32X for "sym*GOT" operand in 32-bit mode. */
10043 if (i.prefix[DATA_PREFIX] == 0
10044 && (generate_relax_relocations
10045 || (!object_64bit
10046 && i.rm.mode == 0
10047 && i.rm.regmem == 5))
0cb4071e
L
10048 && (i.rm.mode == 2
10049 || (i.rm.mode == 0 && i.rm.regmem == 5))
389d00a5 10050 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
02a86693
L
10051 && ((i.operands == 1
10052 && i.tm.base_opcode == 0xff
10053 && (i.rm.reg == 2 || i.rm.reg == 4))
10054 || (i.operands == 2
10055 && (i.tm.base_opcode == 0x8b
10056 || i.tm.base_opcode == 0x85
2ae4c703 10057 || (i.tm.base_opcode & ~0x38) == 0x03))))
02a86693
L
10058 {
10059 if (object_64bit)
10060 {
10061 fixP->fx_tcbit = i.rex != 0;
10062 if (i.base_reg
e968fc9b 10063 && (i.base_reg->reg_num == RegIP))
02a86693
L
10064 fixP->fx_tcbit2 = 1;
10065 }
10066 else
10067 fixP->fx_tcbit2 = 1;
10068 }
29b0f896
AM
10069 }
10070 }
10071 }
10072}
252b5132 10073
29b0f896 10074static void
64e74474 10075output_imm (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
10076{
10077 char *p;
10078 unsigned int n;
252b5132 10079
29b0f896
AM
10080 for (n = 0; n < i.operands; n++)
10081 {
40fb9820 10082 if (operand_type_check (i.types[n], imm))
29b0f896 10083 {
48ef937e
JB
10084 int size = imm_size (n);
10085
10086 if (now_seg == absolute_section)
10087 abs_section_offset += size;
10088 else if (i.op[n].imms->X_op == O_constant)
29b0f896 10089 {
29b0f896 10090 offsetT val;
b4cac588 10091
29b0f896
AM
10092 val = offset_in_range (i.op[n].imms->X_add_number,
10093 size);
10094 p = frag_more (size);
10095 md_number_to_chars (p, val, size);
10096 }
10097 else
10098 {
10099 /* Not absolute_section.
10100 Need a 32-bit fixup (don't support 8bit
10101 non-absolute imms). Try to support other
10102 sizes ... */
f86103b7 10103 enum bfd_reloc_code_real reloc_type;
e205caa7 10104 int sign;
29b0f896 10105
40fb9820 10106 if (i.types[n].bitfield.imm32s
a7d61044 10107 && (i.suffix == QWORD_MNEM_SUFFIX
40fb9820 10108 || (!i.suffix && i.tm.opcode_modifier.no_lsuf)))
29b0f896 10109 sign = 1;
e205caa7
L
10110 else
10111 sign = 0;
520dc8e8 10112
29b0f896 10113 p = frag_more (size);
d258b828 10114 reloc_type = reloc (size, 0, sign, i.reloc[n]);
f86103b7 10115
2bbd9c25
JJ
10116 /* This is tough to explain. We end up with this one if we
10117 * have operands that look like
10118 * "_GLOBAL_OFFSET_TABLE_+[.-.L284]". The goal here is to
10119 * obtain the absolute address of the GOT, and it is strongly
10120 * preferable from a performance point of view to avoid using
10121 * a runtime relocation for this. The actual sequence of
10122 * instructions often look something like:
10123 *
10124 * call .L66
10125 * .L66:
10126 * popl %ebx
10127 * addl $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
10128 *
10129 * The call and pop essentially return the absolute address
10130 * of the label .L66 and store it in %ebx. The linker itself
10131 * will ultimately change the first operand of the addl so
10132 * that %ebx points to the GOT, but to keep things simple, the
10133 * .o file must have this operand set so that it generates not
10134 * the absolute address of .L66, but the absolute address of
10135 * itself. This allows the linker itself simply treat a GOTPC
10136 * relocation as asking for a pcrel offset to the GOT to be
10137 * added in, and the addend of the relocation is stored in the
10138 * operand field for the instruction itself.
10139 *
10140 * Our job here is to fix the operand so that it would add
10141 * the correct offset so that %ebx would point to itself. The
10142 * thing that is tricky is that .-.L66 will point to the
10143 * beginning of the instruction, so we need to further modify
10144 * the operand so that it will point to itself. There are
10145 * other cases where you have something like:
10146 *
10147 * .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
10148 *
10149 * and here no correction would be required. Internally in
10150 * the assembler we treat operands of this form as not being
10151 * pcrel since the '.' is explicitly mentioned, and I wonder
10152 * whether it would simplify matters to do it this way. Who
10153 * knows. In earlier versions of the PIC patches, the
10154 * pcrel_adjust field was used to store the correction, but
10155 * since the expression is not pcrel, I felt it would be
10156 * confusing to do it this way. */
10157
d6ab8113 10158 if ((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
10159 || reloc_type == BFD_RELOC_X86_64_32S
10160 || reloc_type == BFD_RELOC_64)
29b0f896
AM
10161 && GOT_symbol
10162 && GOT_symbol == i.op[n].imms->X_add_symbol
10163 && (i.op[n].imms->X_op == O_symbol
10164 || (i.op[n].imms->X_op == O_add
10165 && ((symbol_get_value_expression
10166 (i.op[n].imms->X_op_symbol)->X_op)
10167 == O_subtract))))
10168 {
4fa24527 10169 if (!object_64bit)
d6ab8113 10170 reloc_type = BFD_RELOC_386_GOTPC;
7b81dfbb 10171 else if (size == 4)
d6ab8113 10172 reloc_type = BFD_RELOC_X86_64_GOTPC32;
7b81dfbb
AJ
10173 else if (size == 8)
10174 reloc_type = BFD_RELOC_X86_64_GOTPC64;
5b7c81bd 10175 i.has_gotpc_tls_reloc = true;
d583596c
JB
10176 i.op[n].imms->X_add_number +=
10177 encoding_length (insn_start_frag, insn_start_off, p);
29b0f896 10178 }
29b0f896
AM
10179 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
10180 i.op[n].imms, 0, reloc_type);
10181 }
10182 }
10183 }
252b5132
RH
10184}
10185\f
d182319b
JB
10186/* x86_cons_fix_new is called via the expression parsing code when a
10187 reloc is needed. We use this hook to get the correct .got reloc. */
d182319b
JB
10188static int cons_sign = -1;
10189
10190void
e3bb37b5 10191x86_cons_fix_new (fragS *frag, unsigned int off, unsigned int len,
62ebcb5c 10192 expressionS *exp, bfd_reloc_code_real_type r)
d182319b 10193{
d258b828 10194 r = reloc (len, 0, cons_sign, r);
d182319b
JB
10195
10196#ifdef TE_PE
10197 if (exp->X_op == O_secrel)
10198 {
10199 exp->X_op = O_symbol;
10200 r = BFD_RELOC_32_SECREL;
10201 }
145667f8
MH
10202 else if (exp->X_op == O_secidx)
10203 r = BFD_RELOC_16_SECIDX;
d182319b
JB
10204#endif
10205
10206 fix_new_exp (frag, off, len, exp, 0, r);
10207}
10208
357d1bd8
L
10209/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
10210 purpose of the `.dc.a' internal pseudo-op. */
10211
10212int
10213x86_address_bytes (void)
10214{
10215 if ((stdoutput->arch_info->mach & bfd_mach_x64_32))
10216 return 4;
10217 return stdoutput->arch_info->bits_per_address / 8;
10218}
10219
deea4973
JB
10220#if (!(defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined (OBJ_MACH_O)) \
10221 || defined (LEX_AT)) && !defined (TE_PE)
d258b828 10222# define lex_got(reloc, adjust, types) NULL
718ddfc0 10223#else
f3c180ae
AM
10224/* Parse operands of the form
10225 <symbol>@GOTOFF+<nnn>
10226 and similar .plt or .got references.
10227
10228 If we find one, set up the correct relocation in RELOC and copy the
10229 input string, minus the `@GOTOFF' into a malloc'd buffer for
10230 parsing by the calling routine. Return this buffer, and if ADJUST
10231 is non-null set it to the length of the string we removed from the
10232 input line. Otherwise return NULL. */
10233static char *
91d6fa6a 10234lex_got (enum bfd_reloc_code_real *rel,
64e74474 10235 int *adjust,
d258b828 10236 i386_operand_type *types)
f3c180ae 10237{
7b81dfbb
AJ
10238 /* Some of the relocations depend on the size of what field is to
10239 be relocated. But in our callers i386_immediate and i386_displacement
10240 we don't yet know the operand size (this will be set by insn
10241 matching). Hence we record the word32 relocation here,
10242 and adjust the reloc according to the real size in reloc(). */
145667f8
MH
10243 static const struct
10244 {
f3c180ae 10245 const char *str;
cff8d58a 10246 int len;
4fa24527 10247 const enum bfd_reloc_code_real rel[2];
40fb9820 10248 const i386_operand_type types64;
5b7c81bd 10249 bool need_GOT_symbol;
145667f8
MH
10250 }
10251 gotrel[] =
10252 {
deea4973 10253#ifndef TE_PE
8ce3d284 10254#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
10255 { STRING_COMMA_LEN ("SIZE"), { BFD_RELOC_SIZE32,
10256 BFD_RELOC_SIZE32 },
5b7c81bd 10257 OPERAND_TYPE_IMM32_64, false },
8ce3d284 10258#endif
cff8d58a
L
10259 { STRING_COMMA_LEN ("PLTOFF"), { _dummy_first_bfd_reloc_code_real,
10260 BFD_RELOC_X86_64_PLTOFF64 },
5b7c81bd 10261 OPERAND_TYPE_IMM64, true },
cff8d58a
L
10262 { STRING_COMMA_LEN ("PLT"), { BFD_RELOC_386_PLT32,
10263 BFD_RELOC_X86_64_PLT32 },
a775efc8 10264 OPERAND_TYPE_IMM32_32S_DISP32, false },
cff8d58a
L
10265 { STRING_COMMA_LEN ("GOTPLT"), { _dummy_first_bfd_reloc_code_real,
10266 BFD_RELOC_X86_64_GOTPLT64 },
5b7c81bd 10267 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
10268 { STRING_COMMA_LEN ("GOTOFF"), { BFD_RELOC_386_GOTOFF,
10269 BFD_RELOC_X86_64_GOTOFF64 },
5b7c81bd 10270 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
10271 { STRING_COMMA_LEN ("GOTPCREL"), { _dummy_first_bfd_reloc_code_real,
10272 BFD_RELOC_X86_64_GOTPCREL },
a775efc8 10273 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10274 { STRING_COMMA_LEN ("TLSGD"), { BFD_RELOC_386_TLS_GD,
10275 BFD_RELOC_X86_64_TLSGD },
a775efc8 10276 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10277 { STRING_COMMA_LEN ("TLSLDM"), { BFD_RELOC_386_TLS_LDM,
10278 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10279 OPERAND_TYPE_NONE, true },
cff8d58a
L
10280 { STRING_COMMA_LEN ("TLSLD"), { _dummy_first_bfd_reloc_code_real,
10281 BFD_RELOC_X86_64_TLSLD },
a775efc8 10282 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10283 { STRING_COMMA_LEN ("GOTTPOFF"), { BFD_RELOC_386_TLS_IE_32,
10284 BFD_RELOC_X86_64_GOTTPOFF },
a775efc8 10285 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10286 { STRING_COMMA_LEN ("TPOFF"), { BFD_RELOC_386_TLS_LE_32,
10287 BFD_RELOC_X86_64_TPOFF32 },
a775efc8 10288 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
10289 { STRING_COMMA_LEN ("NTPOFF"), { BFD_RELOC_386_TLS_LE,
10290 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10291 OPERAND_TYPE_NONE, true },
cff8d58a
L
10292 { STRING_COMMA_LEN ("DTPOFF"), { BFD_RELOC_386_TLS_LDO_32,
10293 BFD_RELOC_X86_64_DTPOFF32 },
a775efc8 10294 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
10295 { STRING_COMMA_LEN ("GOTNTPOFF"),{ BFD_RELOC_386_TLS_GOTIE,
10296 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10297 OPERAND_TYPE_NONE, true },
cff8d58a
L
10298 { STRING_COMMA_LEN ("INDNTPOFF"),{ BFD_RELOC_386_TLS_IE,
10299 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10300 OPERAND_TYPE_NONE, true },
cff8d58a
L
10301 { STRING_COMMA_LEN ("GOT"), { BFD_RELOC_386_GOT32,
10302 BFD_RELOC_X86_64_GOT32 },
a775efc8 10303 OPERAND_TYPE_IMM32_32S_64_DISP32, true },
cff8d58a
L
10304 { STRING_COMMA_LEN ("TLSDESC"), { BFD_RELOC_386_TLS_GOTDESC,
10305 BFD_RELOC_X86_64_GOTPC32_TLSDESC },
a775efc8 10306 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10307 { STRING_COMMA_LEN ("TLSCALL"), { BFD_RELOC_386_TLS_DESC_CALL,
10308 BFD_RELOC_X86_64_TLSDESC_CALL },
a775efc8 10309 OPERAND_TYPE_IMM32_32S_DISP32, true },
deea4973
JB
10310#else /* TE_PE */
10311 { STRING_COMMA_LEN ("SECREL32"), { BFD_RELOC_32_SECREL,
10312 BFD_RELOC_32_SECREL },
a775efc8 10313 OPERAND_TYPE_IMM32_32S_64_DISP32_64, false },
deea4973 10314#endif
f3c180ae
AM
10315 };
10316 char *cp;
10317 unsigned int j;
10318
deea4973 10319#if defined (OBJ_MAYBE_ELF) && !defined (TE_PE)
718ddfc0
JB
10320 if (!IS_ELF)
10321 return NULL;
d382c579 10322#endif
718ddfc0 10323
f3c180ae 10324 for (cp = input_line_pointer; *cp != '@'; cp++)
67c11a9b 10325 if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
f3c180ae
AM
10326 return NULL;
10327
47465058 10328 for (j = 0; j < ARRAY_SIZE (gotrel); j++)
f3c180ae 10329 {
cff8d58a 10330 int len = gotrel[j].len;
28f81592 10331 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
f3c180ae 10332 {
4fa24527 10333 if (gotrel[j].rel[object_64bit] != 0)
f3c180ae 10334 {
28f81592
AM
10335 int first, second;
10336 char *tmpbuf, *past_reloc;
f3c180ae 10337
91d6fa6a 10338 *rel = gotrel[j].rel[object_64bit];
f3c180ae 10339
3956db08
JB
10340 if (types)
10341 {
10342 if (flag_code != CODE_64BIT)
40fb9820
L
10343 {
10344 types->bitfield.imm32 = 1;
10345 types->bitfield.disp32 = 1;
10346 }
3956db08
JB
10347 else
10348 *types = gotrel[j].types64;
10349 }
10350
844bf810 10351 if (gotrel[j].need_GOT_symbol && GOT_symbol == NULL)
f3c180ae
AM
10352 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
10353
28f81592 10354 /* The length of the first part of our input line. */
f3c180ae 10355 first = cp - input_line_pointer;
28f81592
AM
10356
10357 /* The second part goes from after the reloc token until
67c11a9b 10358 (and including) an end_of_line char or comma. */
28f81592 10359 past_reloc = cp + 1 + len;
67c11a9b
AM
10360 cp = past_reloc;
10361 while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
10362 ++cp;
10363 second = cp + 1 - past_reloc;
28f81592
AM
10364
10365 /* Allocate and copy string. The trailing NUL shouldn't
10366 be necessary, but be safe. */
add39d23 10367 tmpbuf = XNEWVEC (char, first + second + 2);
f3c180ae 10368 memcpy (tmpbuf, input_line_pointer, first);
0787a12d
AM
10369 if (second != 0 && *past_reloc != ' ')
10370 /* Replace the relocation token with ' ', so that
10371 errors like foo@GOTOFF1 will be detected. */
10372 tmpbuf[first++] = ' ';
af89796a
L
10373 else
10374 /* Increment length by 1 if the relocation token is
10375 removed. */
10376 len++;
10377 if (adjust)
10378 *adjust = len;
0787a12d
AM
10379 memcpy (tmpbuf + first, past_reloc, second);
10380 tmpbuf[first + second] = '\0';
f3c180ae
AM
10381 return tmpbuf;
10382 }
10383
4fa24527
JB
10384 as_bad (_("@%s reloc is not supported with %d-bit output format"),
10385 gotrel[j].str, 1 << (5 + object_64bit));
f3c180ae
AM
10386 return NULL;
10387 }
10388 }
10389
10390 /* Might be a symbol version string. Don't as_bad here. */
10391 return NULL;
10392}
4e4f7c87 10393#endif
f3c180ae 10394
62ebcb5c 10395bfd_reloc_code_real_type
e3bb37b5 10396x86_cons (expressionS *exp, int size)
f3c180ae 10397{
62ebcb5c
AM
10398 bfd_reloc_code_real_type got_reloc = NO_RELOC;
10399
2748c1b1
L
10400#if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
10401 && !defined (LEX_AT)) \
10402 || defined (TE_PE)
ee86248c
JB
10403 intel_syntax = -intel_syntax;
10404
3c7b9c2c 10405 exp->X_md = 0;
4fa24527 10406 if (size == 4 || (object_64bit && size == 8))
f3c180ae
AM
10407 {
10408 /* Handle @GOTOFF and the like in an expression. */
10409 char *save;
10410 char *gotfree_input_line;
4a57f2cf 10411 int adjust = 0;
f3c180ae
AM
10412
10413 save = input_line_pointer;
d258b828 10414 gotfree_input_line = lex_got (&got_reloc, &adjust, NULL);
f3c180ae
AM
10415 if (gotfree_input_line)
10416 input_line_pointer = gotfree_input_line;
10417
10418 expression (exp);
10419
10420 if (gotfree_input_line)
10421 {
10422 /* expression () has merrily parsed up to the end of line,
10423 or a comma - in the wrong buffer. Transfer how far
10424 input_line_pointer has moved to the right buffer. */
10425 input_line_pointer = (save
10426 + (input_line_pointer - gotfree_input_line)
10427 + adjust);
10428 free (gotfree_input_line);
3992d3b7
AM
10429 if (exp->X_op == O_constant
10430 || exp->X_op == O_absent
10431 || exp->X_op == O_illegal
0398aac5 10432 || exp->X_op == O_register
3992d3b7
AM
10433 || exp->X_op == O_big)
10434 {
10435 char c = *input_line_pointer;
10436 *input_line_pointer = 0;
10437 as_bad (_("missing or invalid expression `%s'"), save);
10438 *input_line_pointer = c;
10439 }
b9519cfe
L
10440 else if ((got_reloc == BFD_RELOC_386_PLT32
10441 || got_reloc == BFD_RELOC_X86_64_PLT32)
10442 && exp->X_op != O_symbol)
10443 {
10444 char c = *input_line_pointer;
10445 *input_line_pointer = 0;
10446 as_bad (_("invalid PLT expression `%s'"), save);
10447 *input_line_pointer = c;
10448 }
f3c180ae
AM
10449 }
10450 }
10451 else
10452 expression (exp);
ee86248c
JB
10453
10454 intel_syntax = -intel_syntax;
10455
10456 if (intel_syntax)
10457 i386_intel_simplify (exp);
2748c1b1
L
10458#else
10459 expression (exp);
10460#endif
62ebcb5c 10461
a442cac5
JB
10462 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
10463 if (size == 4 && exp->X_op == O_constant && !object_64bit)
10464 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
10465
62ebcb5c 10466 return got_reloc;
f3c180ae 10467}
f3c180ae 10468
9f32dd5b
L
10469static void
10470signed_cons (int size)
6482c264 10471{
a442cac5 10472 if (object_64bit)
d182319b
JB
10473 cons_sign = 1;
10474 cons (size);
10475 cons_sign = -1;
6482c264
NC
10476}
10477
d182319b 10478#ifdef TE_PE
6482c264 10479static void
7016a5d5 10480pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
6482c264
NC
10481{
10482 expressionS exp;
10483
10484 do
10485 {
10486 expression (&exp);
10487 if (exp.X_op == O_symbol)
10488 exp.X_op = O_secrel;
10489
10490 emit_expr (&exp, 4);
10491 }
10492 while (*input_line_pointer++ == ',');
10493
10494 input_line_pointer--;
10495 demand_empty_rest_of_line ();
10496}
145667f8
MH
10497
10498static void
10499pe_directive_secidx (int dummy ATTRIBUTE_UNUSED)
10500{
10501 expressionS exp;
10502
10503 do
10504 {
10505 expression (&exp);
10506 if (exp.X_op == O_symbol)
10507 exp.X_op = O_secidx;
10508
10509 emit_expr (&exp, 2);
10510 }
10511 while (*input_line_pointer++ == ',');
10512
10513 input_line_pointer--;
10514 demand_empty_rest_of_line ();
10515}
6482c264
NC
10516#endif
10517
7063667e
JB
10518/* Handle Rounding Control / SAE specifiers. */
10519
10520static char *
10521RC_SAE_specifier (const char *pstr)
10522{
10523 unsigned int j;
10524
10525 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); j++)
10526 {
10527 if (!strncmp (pstr, RC_NamesTable[j].name, RC_NamesTable[j].len))
10528 {
10529 if (i.rounding.type != rc_none)
10530 {
10531 as_bad (_("duplicated `{%s}'"), RC_NamesTable[j].name);
10532 return NULL;
10533 }
10534
10535 i.rounding.type = RC_NamesTable[j].type;
10536
10537 return (char *)(pstr + RC_NamesTable[j].len);
10538 }
10539 }
10540
10541 return NULL;
10542}
10543
43234a1e
L
10544/* Handle Vector operations. */
10545
10546static char *
f70c6814 10547check_VecOperations (char *op_string)
43234a1e
L
10548{
10549 const reg_entry *mask;
10550 const char *saved;
10551 char *end_op;
10552
f70c6814 10553 while (*op_string)
43234a1e
L
10554 {
10555 saved = op_string;
10556 if (*op_string == '{')
10557 {
10558 op_string++;
10559
10560 /* Check broadcasts. */
d34049e8 10561 if (startswith (op_string, "1to"))
43234a1e 10562 {
5273a3cd 10563 unsigned int bcst_type;
43234a1e 10564
5273a3cd 10565 if (i.broadcast.type)
43234a1e
L
10566 goto duplicated_vec_op;
10567
10568 op_string += 3;
10569 if (*op_string == '8')
8e6e0792 10570 bcst_type = 8;
b28d1bda 10571 else if (*op_string == '4')
8e6e0792 10572 bcst_type = 4;
b28d1bda 10573 else if (*op_string == '2')
8e6e0792 10574 bcst_type = 2;
43234a1e
L
10575 else if (*op_string == '1'
10576 && *(op_string+1) == '6')
10577 {
8e6e0792 10578 bcst_type = 16;
43234a1e
L
10579 op_string++;
10580 }
0cc78721
CL
10581 else if (*op_string == '3'
10582 && *(op_string+1) == '2')
10583 {
10584 bcst_type = 32;
10585 op_string++;
10586 }
43234a1e
L
10587 else
10588 {
10589 as_bad (_("Unsupported broadcast: `%s'"), saved);
10590 return NULL;
10591 }
10592 op_string++;
10593
5273a3cd
JB
10594 i.broadcast.type = bcst_type;
10595 i.broadcast.operand = this_operand;
43234a1e
L
10596 }
10597 /* Check masking operation. */
10598 else if ((mask = parse_register (op_string, &end_op)) != NULL)
10599 {
8a6fb3f9
JB
10600 if (mask == &bad_reg)
10601 return NULL;
10602
43234a1e 10603 /* k0 can't be used for write mask. */
f74a6307 10604 if (mask->reg_type.bitfield.class != RegMask || !mask->reg_num)
43234a1e 10605 {
6d2cd6b2
JB
10606 as_bad (_("`%s%s' can't be used for write mask"),
10607 register_prefix, mask->reg_name);
43234a1e
L
10608 return NULL;
10609 }
10610
6225c532 10611 if (!i.mask.reg)
43234a1e 10612 {
6225c532
JB
10613 i.mask.reg = mask;
10614 i.mask.operand = this_operand;
43234a1e 10615 }
6225c532
JB
10616 else if (i.mask.reg->reg_num)
10617 goto duplicated_vec_op;
43234a1e
L
10618 else
10619 {
6225c532 10620 i.mask.reg = mask;
43234a1e
L
10621
10622 /* Only "{z}" is allowed here. No need to check
10623 zeroing mask explicitly. */
6225c532 10624 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
10625 {
10626 as_bad (_("invalid write mask `%s'"), saved);
10627 return NULL;
10628 }
10629 }
10630
10631 op_string = end_op;
10632 }
10633 /* Check zeroing-flag for masking operation. */
10634 else if (*op_string == 'z')
10635 {
6225c532 10636 if (!i.mask.reg)
43234a1e 10637 {
6225c532
JB
10638 i.mask.reg = reg_k0;
10639 i.mask.zeroing = 1;
10640 i.mask.operand = this_operand;
43234a1e
L
10641 }
10642 else
10643 {
6225c532 10644 if (i.mask.zeroing)
43234a1e
L
10645 {
10646 duplicated_vec_op:
10647 as_bad (_("duplicated `%s'"), saved);
10648 return NULL;
10649 }
10650
6225c532 10651 i.mask.zeroing = 1;
43234a1e
L
10652
10653 /* Only "{%k}" is allowed here. No need to check mask
10654 register explicitly. */
6225c532 10655 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
10656 {
10657 as_bad (_("invalid zeroing-masking `%s'"),
10658 saved);
10659 return NULL;
10660 }
10661 }
10662
10663 op_string++;
10664 }
7063667e
JB
10665 else if (intel_syntax
10666 && (op_string = RC_SAE_specifier (op_string)) != NULL)
10667 i.rounding.modifier = true;
43234a1e
L
10668 else
10669 goto unknown_vec_op;
10670
10671 if (*op_string != '}')
10672 {
10673 as_bad (_("missing `}' in `%s'"), saved);
10674 return NULL;
10675 }
10676 op_string++;
0ba3a731
L
10677
10678 /* Strip whitespace since the addition of pseudo prefixes
10679 changed how the scrubber treats '{'. */
10680 if (is_space_char (*op_string))
10681 ++op_string;
10682
43234a1e
L
10683 continue;
10684 }
10685 unknown_vec_op:
10686 /* We don't know this one. */
10687 as_bad (_("unknown vector operation: `%s'"), saved);
10688 return NULL;
10689 }
10690
6225c532 10691 if (i.mask.reg && i.mask.zeroing && !i.mask.reg->reg_num)
6d2cd6b2
JB
10692 {
10693 as_bad (_("zeroing-masking only allowed with write mask"));
10694 return NULL;
10695 }
10696
43234a1e
L
10697 return op_string;
10698}
10699
252b5132 10700static int
70e41ade 10701i386_immediate (char *imm_start)
252b5132
RH
10702{
10703 char *save_input_line_pointer;
f3c180ae 10704 char *gotfree_input_line;
252b5132 10705 segT exp_seg = 0;
47926f60 10706 expressionS *exp;
40fb9820
L
10707 i386_operand_type types;
10708
0dfbf9d7 10709 operand_type_set (&types, ~0);
252b5132
RH
10710
10711 if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
10712 {
31b2323c
L
10713 as_bad (_("at most %d immediate operands are allowed"),
10714 MAX_IMMEDIATE_OPERANDS);
252b5132
RH
10715 return 0;
10716 }
10717
10718 exp = &im_expressions[i.imm_operands++];
520dc8e8 10719 i.op[this_operand].imms = exp;
252b5132
RH
10720
10721 if (is_space_char (*imm_start))
10722 ++imm_start;
10723
10724 save_input_line_pointer = input_line_pointer;
10725 input_line_pointer = imm_start;
10726
d258b828 10727 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
10728 if (gotfree_input_line)
10729 input_line_pointer = gotfree_input_line;
252b5132
RH
10730
10731 exp_seg = expression (exp);
10732
83183c0c 10733 SKIP_WHITESPACE ();
252b5132 10734 if (*input_line_pointer)
f3c180ae 10735 as_bad (_("junk `%s' after expression"), input_line_pointer);
252b5132
RH
10736
10737 input_line_pointer = save_input_line_pointer;
f3c180ae 10738 if (gotfree_input_line)
ee86248c
JB
10739 {
10740 free (gotfree_input_line);
10741
9aac24b1 10742 if (exp->X_op == O_constant)
ee86248c
JB
10743 exp->X_op = O_illegal;
10744 }
10745
9aac24b1
JB
10746 if (exp_seg == reg_section)
10747 {
10748 as_bad (_("illegal immediate register operand %s"), imm_start);
10749 return 0;
10750 }
10751
ee86248c
JB
10752 return i386_finalize_immediate (exp_seg, exp, types, imm_start);
10753}
252b5132 10754
ee86248c
JB
10755static int
10756i386_finalize_immediate (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
10757 i386_operand_type types, const char *imm_start)
10758{
10759 if (exp->X_op == O_absent || exp->X_op == O_illegal || exp->X_op == O_big)
252b5132 10760 {
313c53d1
L
10761 if (imm_start)
10762 as_bad (_("missing or invalid immediate expression `%s'"),
10763 imm_start);
3992d3b7 10764 return 0;
252b5132 10765 }
3e73aa7c 10766 else if (exp->X_op == O_constant)
252b5132 10767 {
47926f60 10768 /* Size it properly later. */
40fb9820 10769 i.types[this_operand].bitfield.imm64 = 1;
a442cac5
JB
10770
10771 /* If not 64bit, sign/zero extend val, to account for wraparound
10772 when !BFD64. */
10773 if (flag_code != CODE_64BIT)
10774 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
252b5132 10775 }
4c63da97 10776#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
f86103b7 10777 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
31312f95 10778 && exp_seg != absolute_section
47926f60 10779 && exp_seg != text_section
24eab124
AM
10780 && exp_seg != data_section
10781 && exp_seg != bss_section
10782 && exp_seg != undefined_section
f86103b7 10783 && !bfd_is_com_section (exp_seg))
252b5132 10784 {
d0b47220 10785 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
252b5132
RH
10786 return 0;
10787 }
10788#endif
10789 else
10790 {
10791 /* This is an address. The size of the address will be
24eab124 10792 determined later, depending on destination register,
3e73aa7c 10793 suffix, or the default for the section. */
40fb9820
L
10794 i.types[this_operand].bitfield.imm8 = 1;
10795 i.types[this_operand].bitfield.imm16 = 1;
10796 i.types[this_operand].bitfield.imm32 = 1;
10797 i.types[this_operand].bitfield.imm32s = 1;
10798 i.types[this_operand].bitfield.imm64 = 1;
c6fb90c8
L
10799 i.types[this_operand] = operand_type_and (i.types[this_operand],
10800 types);
252b5132
RH
10801 }
10802
10803 return 1;
10804}
10805
551c1ca1 10806static char *
e3bb37b5 10807i386_scale (char *scale)
252b5132 10808{
551c1ca1
AM
10809 offsetT val;
10810 char *save = input_line_pointer;
252b5132 10811
551c1ca1
AM
10812 input_line_pointer = scale;
10813 val = get_absolute_expression ();
10814
10815 switch (val)
252b5132 10816 {
551c1ca1 10817 case 1:
252b5132
RH
10818 i.log2_scale_factor = 0;
10819 break;
551c1ca1 10820 case 2:
252b5132
RH
10821 i.log2_scale_factor = 1;
10822 break;
551c1ca1 10823 case 4:
252b5132
RH
10824 i.log2_scale_factor = 2;
10825 break;
551c1ca1 10826 case 8:
252b5132
RH
10827 i.log2_scale_factor = 3;
10828 break;
10829 default:
a724f0f4
JB
10830 {
10831 char sep = *input_line_pointer;
10832
10833 *input_line_pointer = '\0';
10834 as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
10835 scale);
10836 *input_line_pointer = sep;
10837 input_line_pointer = save;
10838 return NULL;
10839 }
252b5132 10840 }
29b0f896 10841 if (i.log2_scale_factor != 0 && i.index_reg == 0)
252b5132
RH
10842 {
10843 as_warn (_("scale factor of %d without an index register"),
24eab124 10844 1 << i.log2_scale_factor);
252b5132 10845 i.log2_scale_factor = 0;
252b5132 10846 }
551c1ca1
AM
10847 scale = input_line_pointer;
10848 input_line_pointer = save;
10849 return scale;
252b5132
RH
10850}
10851
252b5132 10852static int
e3bb37b5 10853i386_displacement (char *disp_start, char *disp_end)
252b5132 10854{
29b0f896 10855 expressionS *exp;
252b5132
RH
10856 segT exp_seg = 0;
10857 char *save_input_line_pointer;
f3c180ae 10858 char *gotfree_input_line;
40fb9820
L
10859 int override;
10860 i386_operand_type bigdisp, types = anydisp;
3992d3b7 10861 int ret;
252b5132 10862
31b2323c
L
10863 if (i.disp_operands == MAX_MEMORY_OPERANDS)
10864 {
10865 as_bad (_("at most %d displacement operands are allowed"),
10866 MAX_MEMORY_OPERANDS);
10867 return 0;
10868 }
10869
0dfbf9d7 10870 operand_type_set (&bigdisp, 0);
6f2f06be 10871 if (i.jumpabsolute
48bcea9f 10872 || i.types[this_operand].bitfield.baseindex
0cfa3eb3
JB
10873 || (current_templates->start->opcode_modifier.jump != JUMP
10874 && current_templates->start->opcode_modifier.jump != JUMP_DWORD))
e05278af 10875 {
48bcea9f 10876 i386_addressing_mode ();
e05278af 10877 override = (i.prefix[ADDR_PREFIX] != 0);
40fb9820
L
10878 if (flag_code == CODE_64BIT)
10879 {
a775efc8 10880 bigdisp.bitfield.disp32 = 1;
40fb9820 10881 if (!override)
a775efc8 10882 bigdisp.bitfield.disp64 = 1;
40fb9820
L
10883 }
10884 else if ((flag_code == CODE_16BIT) ^ override)
40fb9820 10885 bigdisp.bitfield.disp16 = 1;
48bcea9f
JB
10886 else
10887 bigdisp.bitfield.disp32 = 1;
e05278af
JB
10888 }
10889 else
10890 {
376cd056
JB
10891 /* For PC-relative branches, the width of the displacement may be
10892 dependent upon data size, but is never dependent upon address size.
10893 Also make sure to not unintentionally match against a non-PC-relative
10894 branch template. */
10895 static templates aux_templates;
10896 const insn_template *t = current_templates->start;
5b7c81bd 10897 bool has_intel64 = false;
376cd056
JB
10898
10899 aux_templates.start = t;
10900 while (++t < current_templates->end)
10901 {
10902 if (t->opcode_modifier.jump
10903 != current_templates->start->opcode_modifier.jump)
10904 break;
4b5aaf5f 10905 if ((t->opcode_modifier.isa64 >= INTEL64))
5b7c81bd 10906 has_intel64 = true;
376cd056
JB
10907 }
10908 if (t < current_templates->end)
10909 {
10910 aux_templates.end = t;
10911 current_templates = &aux_templates;
10912 }
10913
e05278af 10914 override = (i.prefix[DATA_PREFIX] != 0);
40fb9820
L
10915 if (flag_code == CODE_64BIT)
10916 {
376cd056
JB
10917 if ((override || i.suffix == WORD_MNEM_SUFFIX)
10918 && (!intel64 || !has_intel64))
40fb9820
L
10919 bigdisp.bitfield.disp16 = 1;
10920 else
a775efc8 10921 bigdisp.bitfield.disp32 = 1;
40fb9820
L
10922 }
10923 else
e05278af
JB
10924 {
10925 if (!override)
10926 override = (i.suffix == (flag_code != CODE_16BIT
10927 ? WORD_MNEM_SUFFIX
10928 : LONG_MNEM_SUFFIX));
40fb9820
L
10929 bigdisp.bitfield.disp32 = 1;
10930 if ((flag_code == CODE_16BIT) ^ override)
10931 {
10932 bigdisp.bitfield.disp32 = 0;
10933 bigdisp.bitfield.disp16 = 1;
10934 }
e05278af 10935 }
e05278af 10936 }
c6fb90c8
L
10937 i.types[this_operand] = operand_type_or (i.types[this_operand],
10938 bigdisp);
252b5132
RH
10939
10940 exp = &disp_expressions[i.disp_operands];
520dc8e8 10941 i.op[this_operand].disps = exp;
252b5132
RH
10942 i.disp_operands++;
10943 save_input_line_pointer = input_line_pointer;
10944 input_line_pointer = disp_start;
10945 END_STRING_AND_SAVE (disp_end);
10946
10947#ifndef GCC_ASM_O_HACK
10948#define GCC_ASM_O_HACK 0
10949#endif
10950#if GCC_ASM_O_HACK
10951 END_STRING_AND_SAVE (disp_end + 1);
40fb9820 10952 if (i.types[this_operand].bitfield.baseIndex
24eab124 10953 && displacement_string_end[-1] == '+')
252b5132
RH
10954 {
10955 /* This hack is to avoid a warning when using the "o"
24eab124
AM
10956 constraint within gcc asm statements.
10957 For instance:
10958
10959 #define _set_tssldt_desc(n,addr,limit,type) \
10960 __asm__ __volatile__ ( \
10961 "movw %w2,%0\n\t" \
10962 "movw %w1,2+%0\n\t" \
10963 "rorl $16,%1\n\t" \
10964 "movb %b1,4+%0\n\t" \
10965 "movb %4,5+%0\n\t" \
10966 "movb $0,6+%0\n\t" \
10967 "movb %h1,7+%0\n\t" \
10968 "rorl $16,%1" \
10969 : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
10970
10971 This works great except that the output assembler ends
10972 up looking a bit weird if it turns out that there is
10973 no offset. You end up producing code that looks like:
10974
10975 #APP
10976 movw $235,(%eax)
10977 movw %dx,2+(%eax)
10978 rorl $16,%edx
10979 movb %dl,4+(%eax)
10980 movb $137,5+(%eax)
10981 movb $0,6+(%eax)
10982 movb %dh,7+(%eax)
10983 rorl $16,%edx
10984 #NO_APP
10985
47926f60 10986 So here we provide the missing zero. */
24eab124
AM
10987
10988 *displacement_string_end = '0';
252b5132
RH
10989 }
10990#endif
d258b828 10991 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
10992 if (gotfree_input_line)
10993 input_line_pointer = gotfree_input_line;
252b5132 10994
24eab124 10995 exp_seg = expression (exp);
252b5132 10996
636c26b0
AM
10997 SKIP_WHITESPACE ();
10998 if (*input_line_pointer)
10999 as_bad (_("junk `%s' after expression"), input_line_pointer);
11000#if GCC_ASM_O_HACK
11001 RESTORE_END_STRING (disp_end + 1);
11002#endif
636c26b0 11003 input_line_pointer = save_input_line_pointer;
636c26b0 11004 if (gotfree_input_line)
ee86248c
JB
11005 {
11006 free (gotfree_input_line);
11007
11008 if (exp->X_op == O_constant || exp->X_op == O_register)
11009 exp->X_op = O_illegal;
11010 }
11011
11012 ret = i386_finalize_displacement (exp_seg, exp, types, disp_start);
11013
11014 RESTORE_END_STRING (disp_end);
11015
11016 return ret;
11017}
11018
11019static int
11020i386_finalize_displacement (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
11021 i386_operand_type types, const char *disp_start)
11022{
ee86248c 11023 int ret = 1;
636c26b0 11024
24eab124
AM
11025 /* We do this to make sure that the section symbol is in
11026 the symbol table. We will ultimately change the relocation
47926f60 11027 to be relative to the beginning of the section. */
1ae12ab7 11028 if (i.reloc[this_operand] == BFD_RELOC_386_GOTOFF
d6ab8113
JB
11029 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
11030 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
24eab124 11031 {
636c26b0 11032 if (exp->X_op != O_symbol)
3992d3b7 11033 goto inv_disp;
636c26b0 11034
e5cb08ac 11035 if (S_IS_LOCAL (exp->X_add_symbol)
c64efb4b
L
11036 && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section
11037 && S_GET_SEGMENT (exp->X_add_symbol) != expr_section)
24eab124 11038 section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
24eab124
AM
11039 exp->X_op = O_subtract;
11040 exp->X_op_symbol = GOT_symbol;
1ae12ab7 11041 if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
29b0f896 11042 i.reloc[this_operand] = BFD_RELOC_32_PCREL;
d6ab8113
JB
11043 else if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
11044 i.reloc[this_operand] = BFD_RELOC_64;
23df1078 11045 else
29b0f896 11046 i.reloc[this_operand] = BFD_RELOC_32;
24eab124 11047 }
252b5132 11048
3992d3b7
AM
11049 else if (exp->X_op == O_absent
11050 || exp->X_op == O_illegal
ee86248c 11051 || exp->X_op == O_big)
2daf4fd8 11052 {
3992d3b7
AM
11053 inv_disp:
11054 as_bad (_("missing or invalid displacement expression `%s'"),
2daf4fd8 11055 disp_start);
3992d3b7 11056 ret = 0;
2daf4fd8
AM
11057 }
11058
a50187b2
JB
11059 else if (exp->X_op == O_constant)
11060 {
11061 /* Sizing gets taken care of by optimize_disp().
11062
11063 If not 64bit, sign/zero extend val, to account for wraparound
11064 when !BFD64. */
11065 if (flag_code != CODE_64BIT)
11066 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
11067 }
11068
4c63da97 11069#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
a50187b2 11070 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
3992d3b7
AM
11071 && exp_seg != absolute_section
11072 && exp_seg != text_section
11073 && exp_seg != data_section
11074 && exp_seg != bss_section
11075 && exp_seg != undefined_section
11076 && !bfd_is_com_section (exp_seg))
24eab124 11077 {
d0b47220 11078 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
3992d3b7 11079 ret = 0;
24eab124 11080 }
252b5132 11081#endif
3956db08 11082
a50187b2 11083 else if (current_templates->start->opcode_modifier.jump == JUMP_BYTE)
48bcea9f
JB
11084 i.types[this_operand].bitfield.disp8 = 1;
11085
40fb9820 11086 /* Check if this is a displacement only operand. */
02b83698
JB
11087 if (!i.types[this_operand].bitfield.baseindex)
11088 i.types[this_operand] =
11089 operand_type_or (operand_type_and_not (i.types[this_operand], anydisp),
11090 operand_type_and (i.types[this_operand], types));
3956db08 11091
3992d3b7 11092 return ret;
252b5132
RH
11093}
11094
2abc2bec
JB
11095/* Return the active addressing mode, taking address override and
11096 registers forming the address into consideration. Update the
11097 address override prefix if necessary. */
47926f60 11098
2abc2bec
JB
11099static enum flag_code
11100i386_addressing_mode (void)
252b5132 11101{
be05d201
L
11102 enum flag_code addr_mode;
11103
11104 if (i.prefix[ADDR_PREFIX])
11105 addr_mode = flag_code == CODE_32BIT ? CODE_16BIT : CODE_32BIT;
a23b33b3
JB
11106 else if (flag_code == CODE_16BIT
11107 && current_templates->start->cpu_flags.bitfield.cpumpx
11108 /* Avoid replacing the "16-bit addressing not allowed" diagnostic
11109 from md_assemble() by "is not a valid base/index expression"
11110 when there is a base and/or index. */
11111 && !i.types[this_operand].bitfield.baseindex)
11112 {
11113 /* MPX insn memory operands with neither base nor index must be forced
11114 to use 32-bit addressing in 16-bit mode. */
11115 addr_mode = CODE_32BIT;
11116 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
11117 ++i.prefixes;
11118 gas_assert (!i.types[this_operand].bitfield.disp16);
11119 gas_assert (!i.types[this_operand].bitfield.disp32);
11120 }
be05d201
L
11121 else
11122 {
11123 addr_mode = flag_code;
11124
24eab124 11125#if INFER_ADDR_PREFIX
be05d201
L
11126 if (i.mem_operands == 0)
11127 {
11128 /* Infer address prefix from the first memory operand. */
11129 const reg_entry *addr_reg = i.base_reg;
11130
11131 if (addr_reg == NULL)
11132 addr_reg = i.index_reg;
eecb386c 11133
be05d201
L
11134 if (addr_reg)
11135 {
e968fc9b 11136 if (addr_reg->reg_type.bitfield.dword)
be05d201
L
11137 addr_mode = CODE_32BIT;
11138 else if (flag_code != CODE_64BIT
dc821c5f 11139 && addr_reg->reg_type.bitfield.word)
be05d201
L
11140 addr_mode = CODE_16BIT;
11141
11142 if (addr_mode != flag_code)
11143 {
11144 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
11145 i.prefixes += 1;
11146 /* Change the size of any displacement too. At most one
11147 of Disp16 or Disp32 is set.
11148 FIXME. There doesn't seem to be any real need for
11149 separate Disp16 and Disp32 flags. The same goes for
11150 Imm16 and Imm32. Removing them would probably clean
11151 up the code quite a lot. */
11152 if (flag_code != CODE_64BIT
11153 && (i.types[this_operand].bitfield.disp16
11154 || i.types[this_operand].bitfield.disp32))
11155 i.types[this_operand]
11156 = operand_type_xor (i.types[this_operand], disp16_32);
11157 }
11158 }
11159 }
24eab124 11160#endif
be05d201
L
11161 }
11162
2abc2bec
JB
11163 return addr_mode;
11164}
11165
11166/* Make sure the memory operand we've been dealt is valid.
11167 Return 1 on success, 0 on a failure. */
11168
11169static int
11170i386_index_check (const char *operand_string)
11171{
11172 const char *kind = "base/index";
11173 enum flag_code addr_mode = i386_addressing_mode ();
a152332d 11174 const insn_template *t = current_templates->start;
2abc2bec 11175
a152332d
JB
11176 if (t->opcode_modifier.isstring
11177 && !t->cpu_flags.bitfield.cpupadlock
fc0763e6
JB
11178 && (current_templates->end[-1].opcode_modifier.isstring
11179 || i.mem_operands))
11180 {
11181 /* Memory operands of string insns are special in that they only allow
11182 a single register (rDI, rSI, or rBX) as their memory address. */
be05d201
L
11183 const reg_entry *expected_reg;
11184 static const char *di_si[][2] =
11185 {
11186 { "esi", "edi" },
11187 { "si", "di" },
11188 { "rsi", "rdi" }
11189 };
11190 static const char *bx[] = { "ebx", "bx", "rbx" };
fc0763e6
JB
11191
11192 kind = "string address";
11193
a152332d 11194 if (t->opcode_modifier.prefixok == PrefixRep)
fc0763e6 11195 {
51c8edf6
JB
11196 int es_op = current_templates->end[-1].opcode_modifier.isstring
11197 - IS_STRING_ES_OP0;
11198 int op = 0;
fc0763e6 11199
51c8edf6 11200 if (!current_templates->end[-1].operand_types[0].bitfield.baseindex
fc0763e6
JB
11201 || ((!i.mem_operands != !intel_syntax)
11202 && current_templates->end[-1].operand_types[1]
11203 .bitfield.baseindex))
51c8edf6 11204 op = 1;
fe0e921f
AM
11205 expected_reg
11206 = (const reg_entry *) str_hash_find (reg_hash,
11207 di_si[addr_mode][op == es_op]);
fc0763e6
JB
11208 }
11209 else
fe0e921f
AM
11210 expected_reg
11211 = (const reg_entry *)str_hash_find (reg_hash, bx[addr_mode]);
fc0763e6 11212
be05d201
L
11213 if (i.base_reg != expected_reg
11214 || i.index_reg
fc0763e6 11215 || operand_type_check (i.types[this_operand], disp))
fc0763e6 11216 {
be05d201
L
11217 /* The second memory operand must have the same size as
11218 the first one. */
11219 if (i.mem_operands
11220 && i.base_reg
11221 && !((addr_mode == CODE_64BIT
dc821c5f 11222 && i.base_reg->reg_type.bitfield.qword)
be05d201 11223 || (addr_mode == CODE_32BIT
dc821c5f
JB
11224 ? i.base_reg->reg_type.bitfield.dword
11225 : i.base_reg->reg_type.bitfield.word)))
be05d201
L
11226 goto bad_address;
11227
fc0763e6
JB
11228 as_warn (_("`%s' is not valid here (expected `%c%s%s%c')"),
11229 operand_string,
11230 intel_syntax ? '[' : '(',
11231 register_prefix,
be05d201 11232 expected_reg->reg_name,
fc0763e6 11233 intel_syntax ? ']' : ')');
be05d201 11234 return 1;
fc0763e6 11235 }
be05d201
L
11236 else
11237 return 1;
11238
dc1e8a47 11239 bad_address:
be05d201
L
11240 as_bad (_("`%s' is not a valid %s expression"),
11241 operand_string, kind);
11242 return 0;
3e73aa7c
JH
11243 }
11244 else
11245 {
be05d201
L
11246 if (addr_mode != CODE_16BIT)
11247 {
11248 /* 32-bit/64-bit checks. */
41eb8e88
L
11249 if (i.disp_encoding == disp_encoding_16bit)
11250 {
11251 bad_disp:
11252 as_bad (_("invalid `%s' prefix"),
11253 addr_mode == CODE_16BIT ? "{disp32}" : "{disp16}");
11254 return 0;
11255 }
11256
be05d201 11257 if ((i.base_reg
e968fc9b
JB
11258 && ((addr_mode == CODE_64BIT
11259 ? !i.base_reg->reg_type.bitfield.qword
11260 : !i.base_reg->reg_type.bitfield.dword)
11261 || (i.index_reg && i.base_reg->reg_num == RegIP)
11262 || i.base_reg->reg_num == RegIZ))
be05d201 11263 || (i.index_reg
1b54b8d7
JB
11264 && !i.index_reg->reg_type.bitfield.xmmword
11265 && !i.index_reg->reg_type.bitfield.ymmword
11266 && !i.index_reg->reg_type.bitfield.zmmword
be05d201 11267 && ((addr_mode == CODE_64BIT
e968fc9b
JB
11268 ? !i.index_reg->reg_type.bitfield.qword
11269 : !i.index_reg->reg_type.bitfield.dword)
be05d201
L
11270 || !i.index_reg->reg_type.bitfield.baseindex)))
11271 goto bad_address;
8178be5b 11272
260cd341 11273 /* bndmk, bndldx, bndstx and mandatory non-vector SIB have special restrictions. */
a152332d 11274 if ((t->opcode_modifier.opcodeprefix == PREFIX_0XF3
389d00a5
JB
11275 && t->opcode_modifier.opcodespace == SPACE_0F
11276 && t->base_opcode == 0x1b)
a152332d 11277 || (t->opcode_modifier.opcodeprefix == PREFIX_NONE
389d00a5
JB
11278 && t->opcode_modifier.opcodespace == SPACE_0F
11279 && (t->base_opcode & ~1) == 0x1a)
a152332d 11280 || t->opcode_modifier.sib == SIBMEM)
8178be5b
JB
11281 {
11282 /* They cannot use RIP-relative addressing. */
e968fc9b 11283 if (i.base_reg && i.base_reg->reg_num == RegIP)
8178be5b
JB
11284 {
11285 as_bad (_("`%s' cannot be used here"), operand_string);
11286 return 0;
11287 }
11288
11289 /* bndldx and bndstx ignore their scale factor. */
a152332d 11290 if (t->opcode_modifier.opcodeprefix == PREFIX_NONE
389d00a5
JB
11291 && t->opcode_modifier.opcodespace == SPACE_0F
11292 && (t->base_opcode & ~1) == 0x1a
8178be5b
JB
11293 && i.log2_scale_factor)
11294 as_warn (_("register scaling is being ignored here"));
11295 }
be05d201
L
11296 }
11297 else
3e73aa7c 11298 {
be05d201 11299 /* 16-bit checks. */
41eb8e88
L
11300 if (i.disp_encoding == disp_encoding_32bit)
11301 goto bad_disp;
11302
3e73aa7c 11303 if ((i.base_reg
dc821c5f 11304 && (!i.base_reg->reg_type.bitfield.word
40fb9820 11305 || !i.base_reg->reg_type.bitfield.baseindex))
3e73aa7c 11306 || (i.index_reg
dc821c5f 11307 && (!i.index_reg->reg_type.bitfield.word
40fb9820 11308 || !i.index_reg->reg_type.bitfield.baseindex
29b0f896
AM
11309 || !(i.base_reg
11310 && i.base_reg->reg_num < 6
11311 && i.index_reg->reg_num >= 6
11312 && i.log2_scale_factor == 0))))
be05d201 11313 goto bad_address;
3e73aa7c
JH
11314 }
11315 }
be05d201 11316 return 1;
24eab124 11317}
252b5132 11318
43234a1e
L
11319/* Handle vector immediates. */
11320
11321static int
11322RC_SAE_immediate (const char *imm_start)
11323{
43234a1e 11324 const char *pstr = imm_start;
43234a1e
L
11325
11326 if (*pstr != '{')
11327 return 0;
11328
7063667e
JB
11329 pstr = RC_SAE_specifier (pstr + 1);
11330 if (pstr == NULL)
43234a1e
L
11331 return 0;
11332
11333 if (*pstr++ != '}')
11334 {
11335 as_bad (_("Missing '}': '%s'"), imm_start);
11336 return 0;
11337 }
11338 /* RC/SAE immediate string should contain nothing more. */;
11339 if (*pstr != 0)
11340 {
11341 as_bad (_("Junk after '}': '%s'"), imm_start);
11342 return 0;
11343 }
11344
cf665fee
JB
11345 /* Internally this doesn't count as an operand. */
11346 --i.operands;
43234a1e 11347
43234a1e
L
11348 return 1;
11349}
11350
8325cc63
JB
11351/* Only string instructions can have a second memory operand, so
11352 reduce current_templates to just those if it contains any. */
11353static int
11354maybe_adjust_templates (void)
11355{
11356 const insn_template *t;
11357
11358 gas_assert (i.mem_operands == 1);
11359
11360 for (t = current_templates->start; t < current_templates->end; ++t)
11361 if (t->opcode_modifier.isstring)
11362 break;
11363
11364 if (t < current_templates->end)
11365 {
11366 static templates aux_templates;
5b7c81bd 11367 bool recheck;
8325cc63
JB
11368
11369 aux_templates.start = t;
11370 for (; t < current_templates->end; ++t)
11371 if (!t->opcode_modifier.isstring)
11372 break;
11373 aux_templates.end = t;
11374
11375 /* Determine whether to re-check the first memory operand. */
11376 recheck = (aux_templates.start != current_templates->start
11377 || t != current_templates->end);
11378
11379 current_templates = &aux_templates;
11380
11381 if (recheck)
11382 {
11383 i.mem_operands = 0;
11384 if (i.memop1_string != NULL
11385 && i386_index_check (i.memop1_string) == 0)
11386 return 0;
11387 i.mem_operands = 1;
11388 }
11389 }
11390
11391 return 1;
11392}
11393
9d299bea
JB
11394static INLINE bool starts_memory_operand (char c)
11395{
014fbcda 11396 return ISDIGIT (c)
9d299bea 11397 || is_identifier_char (c)
014fbcda 11398 || strchr ("([\"+-!~", c);
9d299bea
JB
11399}
11400
fc0763e6 11401/* Parse OPERAND_STRING into the i386_insn structure I. Returns zero
47926f60 11402 on error. */
252b5132 11403
252b5132 11404static int
a7619375 11405i386_att_operand (char *operand_string)
252b5132 11406{
af6bdddf
AM
11407 const reg_entry *r;
11408 char *end_op;
24eab124 11409 char *op_string = operand_string;
252b5132 11410
24eab124 11411 if (is_space_char (*op_string))
252b5132
RH
11412 ++op_string;
11413
24eab124 11414 /* We check for an absolute prefix (differentiating,
47926f60 11415 for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
24eab124
AM
11416 if (*op_string == ABSOLUTE_PREFIX)
11417 {
11418 ++op_string;
11419 if (is_space_char (*op_string))
11420 ++op_string;
5b7c81bd 11421 i.jumpabsolute = true;
24eab124 11422 }
252b5132 11423
47926f60 11424 /* Check if operand is a register. */
4d1bb795 11425 if ((r = parse_register (op_string, &end_op)) != NULL)
24eab124 11426 {
40fb9820
L
11427 i386_operand_type temp;
11428
8a6fb3f9
JB
11429 if (r == &bad_reg)
11430 return 0;
11431
24eab124
AM
11432 /* Check for a segment override by searching for ':' after a
11433 segment register. */
11434 op_string = end_op;
11435 if (is_space_char (*op_string))
11436 ++op_string;
00cee14f 11437 if (*op_string == ':' && r->reg_type.bitfield.class == SReg)
24eab124 11438 {
5e042380 11439 i.seg[i.mem_operands] = r;
252b5132 11440
24eab124 11441 /* Skip the ':' and whitespace. */
252b5132
RH
11442 ++op_string;
11443 if (is_space_char (*op_string))
24eab124 11444 ++op_string;
252b5132 11445
47926f60 11446 /* Handle case of %es:*foo. */
c8d541e2 11447 if (!i.jumpabsolute && *op_string == ABSOLUTE_PREFIX)
24eab124
AM
11448 {
11449 ++op_string;
11450 if (is_space_char (*op_string))
11451 ++op_string;
5b7c81bd 11452 i.jumpabsolute = true;
24eab124 11453 }
c8d541e2 11454
9d299bea 11455 if (!starts_memory_operand (*op_string))
c8d541e2
JB
11456 {
11457 as_bad (_("bad memory operand `%s'"), op_string);
11458 return 0;
11459 }
24eab124
AM
11460 goto do_memory_reference;
11461 }
43234a1e
L
11462
11463 /* Handle vector operations. */
11464 if (*op_string == '{')
11465 {
f70c6814 11466 op_string = check_VecOperations (op_string);
43234a1e
L
11467 if (op_string == NULL)
11468 return 0;
11469 }
11470
24eab124
AM
11471 if (*op_string)
11472 {
d0b47220 11473 as_bad (_("junk `%s' after register"), op_string);
24eab124
AM
11474 return 0;
11475 }
40fb9820
L
11476 temp = r->reg_type;
11477 temp.bitfield.baseindex = 0;
c6fb90c8
L
11478 i.types[this_operand] = operand_type_or (i.types[this_operand],
11479 temp);
7d5e4556 11480 i.types[this_operand].bitfield.unspecified = 0;
520dc8e8 11481 i.op[this_operand].regs = r;
24eab124 11482 i.reg_operands++;
cf665fee
JB
11483
11484 /* A GPR may follow an RC or SAE immediate only if a (vector) register
11485 operand was also present earlier on. */
11486 if (i.rounding.type != rc_none && temp.bitfield.class == Reg
11487 && i.reg_operands == 1)
11488 {
11489 unsigned int j;
11490
11491 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); ++j)
11492 if (i.rounding.type == RC_NamesTable[j].type)
11493 break;
11494 as_bad (_("`%s': misplaced `{%s}'"),
11495 current_templates->start->name, RC_NamesTable[j].name);
11496 return 0;
11497 }
24eab124 11498 }
af6bdddf
AM
11499 else if (*op_string == REGISTER_PREFIX)
11500 {
11501 as_bad (_("bad register name `%s'"), op_string);
11502 return 0;
11503 }
24eab124 11504 else if (*op_string == IMMEDIATE_PREFIX)
ce8a8b2f 11505 {
24eab124 11506 ++op_string;
6f2f06be 11507 if (i.jumpabsolute)
24eab124 11508 {
d0b47220 11509 as_bad (_("immediate operand illegal with absolute jump"));
24eab124
AM
11510 return 0;
11511 }
11512 if (!i386_immediate (op_string))
11513 return 0;
cf665fee
JB
11514 if (i.rounding.type != rc_none)
11515 {
11516 as_bad (_("`%s': RC/SAE operand must follow immediate operands"),
11517 current_templates->start->name);
11518 return 0;
11519 }
24eab124 11520 }
43234a1e
L
11521 else if (RC_SAE_immediate (operand_string))
11522 {
cf665fee
JB
11523 /* If it is a RC or SAE immediate, do the necessary placement check:
11524 Only another immediate or a GPR may precede it. */
11525 if (i.mem_operands || i.reg_operands + i.imm_operands > 1
11526 || (i.reg_operands == 1
11527 && i.op[0].regs->reg_type.bitfield.class != Reg))
11528 {
11529 as_bad (_("`%s': misplaced `%s'"),
11530 current_templates->start->name, operand_string);
11531 return 0;
11532 }
43234a1e 11533 }
9d299bea 11534 else if (starts_memory_operand (*op_string))
24eab124 11535 {
47926f60 11536 /* This is a memory reference of some sort. */
af6bdddf 11537 char *base_string;
252b5132 11538
47926f60 11539 /* Start and end of displacement string expression (if found). */
eecb386c
AM
11540 char *displacement_string_start;
11541 char *displacement_string_end;
252b5132 11542
24eab124 11543 do_memory_reference:
8325cc63
JB
11544 if (i.mem_operands == 1 && !maybe_adjust_templates ())
11545 return 0;
24eab124 11546 if ((i.mem_operands == 1
40fb9820 11547 && !current_templates->start->opcode_modifier.isstring)
24eab124
AM
11548 || i.mem_operands == 2)
11549 {
11550 as_bad (_("too many memory references for `%s'"),
11551 current_templates->start->name);
11552 return 0;
11553 }
252b5132 11554
24eab124
AM
11555 /* Check for base index form. We detect the base index form by
11556 looking for an ')' at the end of the operand, searching
11557 for the '(' matching it, and finding a REGISTER_PREFIX or ','
11558 after the '('. */
af6bdddf 11559 base_string = op_string + strlen (op_string);
c3332e24 11560
43234a1e 11561 /* Handle vector operations. */
6b5ba0d4
JB
11562 --base_string;
11563 if (is_space_char (*base_string))
11564 --base_string;
11565
11566 if (*base_string == '}')
43234a1e 11567 {
6b5ba0d4
JB
11568 char *vop_start = NULL;
11569
11570 while (base_string-- > op_string)
11571 {
11572 if (*base_string == '"')
11573 break;
11574 if (*base_string != '{')
11575 continue;
11576
11577 vop_start = base_string;
11578
11579 --base_string;
11580 if (is_space_char (*base_string))
11581 --base_string;
11582
11583 if (*base_string != '}')
11584 break;
11585
11586 vop_start = NULL;
11587 }
11588
11589 if (!vop_start)
11590 {
11591 as_bad (_("unbalanced figure braces"));
11592 return 0;
11593 }
11594
f70c6814 11595 if (check_VecOperations (vop_start) == NULL)
43234a1e 11596 return 0;
43234a1e
L
11597 }
11598
47926f60 11599 /* If we only have a displacement, set-up for it to be parsed later. */
af6bdddf
AM
11600 displacement_string_start = op_string;
11601 displacement_string_end = base_string + 1;
252b5132 11602
24eab124
AM
11603 if (*base_string == ')')
11604 {
af6bdddf 11605 char *temp_string;
e87fb6a6
JB
11606 unsigned int parens_not_balanced = 0;
11607 bool in_quotes = false;
e68c3d59 11608
24eab124 11609 /* We've already checked that the number of left & right ()'s are
e87fb6a6
JB
11610 equal, and that there's a matching set of double quotes. */
11611 end_op = base_string;
11612 for (temp_string = op_string; temp_string < end_op; temp_string++)
24eab124 11613 {
e87fb6a6
JB
11614 if (*temp_string == '\\' && temp_string[1] == '"')
11615 ++temp_string;
11616 else if (*temp_string == '"')
11617 in_quotes = !in_quotes;
11618 else if (!in_quotes)
11619 {
11620 if (*temp_string == '(' && !parens_not_balanced++)
11621 base_string = temp_string;
11622 if (*temp_string == ')')
11623 --parens_not_balanced;
11624 }
24eab124 11625 }
c3332e24 11626
af6bdddf 11627 temp_string = base_string;
c3332e24 11628
24eab124 11629 /* Skip past '(' and whitespace. */
e87fb6a6
JB
11630 gas_assert (*base_string == '(');
11631 ++base_string;
252b5132 11632 if (is_space_char (*base_string))
24eab124 11633 ++base_string;
252b5132 11634
af6bdddf 11635 if (*base_string == ','
4eed87de
AM
11636 || ((i.base_reg = parse_register (base_string, &end_op))
11637 != NULL))
252b5132 11638 {
af6bdddf 11639 displacement_string_end = temp_string;
252b5132 11640
40fb9820 11641 i.types[this_operand].bitfield.baseindex = 1;
252b5132 11642
af6bdddf 11643 if (i.base_reg)
24eab124 11644 {
8a6fb3f9
JB
11645 if (i.base_reg == &bad_reg)
11646 return 0;
24eab124
AM
11647 base_string = end_op;
11648 if (is_space_char (*base_string))
11649 ++base_string;
af6bdddf
AM
11650 }
11651
11652 /* There may be an index reg or scale factor here. */
11653 if (*base_string == ',')
11654 {
11655 ++base_string;
11656 if (is_space_char (*base_string))
11657 ++base_string;
11658
4eed87de
AM
11659 if ((i.index_reg = parse_register (base_string, &end_op))
11660 != NULL)
24eab124 11661 {
8a6fb3f9
JB
11662 if (i.index_reg == &bad_reg)
11663 return 0;
af6bdddf 11664 base_string = end_op;
24eab124
AM
11665 if (is_space_char (*base_string))
11666 ++base_string;
af6bdddf
AM
11667 if (*base_string == ',')
11668 {
11669 ++base_string;
11670 if (is_space_char (*base_string))
11671 ++base_string;
11672 }
e5cb08ac 11673 else if (*base_string != ')')
af6bdddf 11674 {
4eed87de
AM
11675 as_bad (_("expecting `,' or `)' "
11676 "after index register in `%s'"),
af6bdddf
AM
11677 operand_string);
11678 return 0;
11679 }
24eab124 11680 }
af6bdddf 11681 else if (*base_string == REGISTER_PREFIX)
24eab124 11682 {
f76bf5e0
L
11683 end_op = strchr (base_string, ',');
11684 if (end_op)
11685 *end_op = '\0';
af6bdddf 11686 as_bad (_("bad register name `%s'"), base_string);
24eab124
AM
11687 return 0;
11688 }
252b5132 11689
47926f60 11690 /* Check for scale factor. */
551c1ca1 11691 if (*base_string != ')')
af6bdddf 11692 {
551c1ca1
AM
11693 char *end_scale = i386_scale (base_string);
11694
11695 if (!end_scale)
af6bdddf 11696 return 0;
24eab124 11697
551c1ca1 11698 base_string = end_scale;
af6bdddf
AM
11699 if (is_space_char (*base_string))
11700 ++base_string;
11701 if (*base_string != ')')
11702 {
4eed87de
AM
11703 as_bad (_("expecting `)' "
11704 "after scale factor in `%s'"),
af6bdddf
AM
11705 operand_string);
11706 return 0;
11707 }
11708 }
11709 else if (!i.index_reg)
24eab124 11710 {
4eed87de
AM
11711 as_bad (_("expecting index register or scale factor "
11712 "after `,'; got '%c'"),
af6bdddf 11713 *base_string);
24eab124
AM
11714 return 0;
11715 }
11716 }
af6bdddf 11717 else if (*base_string != ')')
24eab124 11718 {
4eed87de
AM
11719 as_bad (_("expecting `,' or `)' "
11720 "after base register in `%s'"),
af6bdddf 11721 operand_string);
24eab124
AM
11722 return 0;
11723 }
c3332e24 11724 }
af6bdddf 11725 else if (*base_string == REGISTER_PREFIX)
c3332e24 11726 {
f76bf5e0
L
11727 end_op = strchr (base_string, ',');
11728 if (end_op)
11729 *end_op = '\0';
af6bdddf 11730 as_bad (_("bad register name `%s'"), base_string);
24eab124 11731 return 0;
c3332e24 11732 }
24eab124
AM
11733 }
11734
11735 /* If there's an expression beginning the operand, parse it,
11736 assuming displacement_string_start and
11737 displacement_string_end are meaningful. */
11738 if (displacement_string_start != displacement_string_end)
11739 {
11740 if (!i386_displacement (displacement_string_start,
11741 displacement_string_end))
11742 return 0;
11743 }
11744
11745 /* Special case for (%dx) while doing input/output op. */
11746 if (i.base_reg
75e5731b
JB
11747 && i.base_reg->reg_type.bitfield.instance == RegD
11748 && i.base_reg->reg_type.bitfield.word
24eab124
AM
11749 && i.index_reg == 0
11750 && i.log2_scale_factor == 0
11751 && i.seg[i.mem_operands] == 0
40fb9820 11752 && !operand_type_check (i.types[this_operand], disp))
24eab124 11753 {
2fb5be8d 11754 i.types[this_operand] = i.base_reg->reg_type;
24eab124
AM
11755 return 1;
11756 }
11757
eecb386c
AM
11758 if (i386_index_check (operand_string) == 0)
11759 return 0;
c48dadc9 11760 i.flags[this_operand] |= Operand_Mem;
8325cc63
JB
11761 if (i.mem_operands == 0)
11762 i.memop1_string = xstrdup (operand_string);
24eab124
AM
11763 i.mem_operands++;
11764 }
11765 else
ce8a8b2f
AM
11766 {
11767 /* It's not a memory operand; argh! */
24eab124
AM
11768 as_bad (_("invalid char %s beginning operand %d `%s'"),
11769 output_invalid (*op_string),
11770 this_operand + 1,
11771 op_string);
11772 return 0;
11773 }
47926f60 11774 return 1; /* Normal return. */
252b5132
RH
11775}
11776\f
fa94de6b
RM
11777/* Calculate the maximum variable size (i.e., excluding fr_fix)
11778 that an rs_machine_dependent frag may reach. */
11779
11780unsigned int
11781i386_frag_max_var (fragS *frag)
11782{
11783 /* The only relaxable frags are for jumps.
11784 Unconditional jumps can grow by 4 bytes and others by 5 bytes. */
11785 gas_assert (frag->fr_type == rs_machine_dependent);
11786 return TYPE_FROM_RELAX_STATE (frag->fr_subtype) == UNCOND_JUMP ? 4 : 5;
11787}
11788
b084df0b
L
11789#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
11790static int
8dcea932 11791elf_symbol_resolved_in_segment_p (symbolS *fr_symbol, offsetT fr_var)
b084df0b
L
11792{
11793 /* STT_GNU_IFUNC symbol must go through PLT. */
11794 if ((symbol_get_bfdsym (fr_symbol)->flags
11795 & BSF_GNU_INDIRECT_FUNCTION) != 0)
11796 return 0;
11797
11798 if (!S_IS_EXTERNAL (fr_symbol))
11799 /* Symbol may be weak or local. */
11800 return !S_IS_WEAK (fr_symbol);
11801
8dcea932
L
11802 /* Global symbols with non-default visibility can't be preempted. */
11803 if (ELF_ST_VISIBILITY (S_GET_OTHER (fr_symbol)) != STV_DEFAULT)
11804 return 1;
11805
11806 if (fr_var != NO_RELOC)
11807 switch ((enum bfd_reloc_code_real) fr_var)
11808 {
11809 case BFD_RELOC_386_PLT32:
11810 case BFD_RELOC_X86_64_PLT32:
33eaf5de 11811 /* Symbol with PLT relocation may be preempted. */
8dcea932
L
11812 return 0;
11813 default:
11814 abort ();
11815 }
11816
b084df0b
L
11817 /* Global symbols with default visibility in a shared library may be
11818 preempted by another definition. */
8dcea932 11819 return !shared;
b084df0b
L
11820}
11821#endif
11822
79d72f45
HL
11823/* Table 3-2. Macro-Fusible Instructions in Haswell Microarchitecture
11824 Note also work for Skylake and Cascadelake.
11825---------------------------------------------------------------------
11826| JCC | ADD/SUB/CMP | INC/DEC | TEST/AND |
11827| ------ | ----------- | ------- | -------- |
11828| Jo | N | N | Y |
11829| Jno | N | N | Y |
11830| Jc/Jb | Y | N | Y |
11831| Jae/Jnb | Y | N | Y |
11832| Je/Jz | Y | Y | Y |
11833| Jne/Jnz | Y | Y | Y |
11834| Jna/Jbe | Y | N | Y |
11835| Ja/Jnbe | Y | N | Y |
11836| Js | N | N | Y |
11837| Jns | N | N | Y |
11838| Jp/Jpe | N | N | Y |
11839| Jnp/Jpo | N | N | Y |
11840| Jl/Jnge | Y | Y | Y |
11841| Jge/Jnl | Y | Y | Y |
11842| Jle/Jng | Y | Y | Y |
11843| Jg/Jnle | Y | Y | Y |
11844--------------------------------------------------------------------- */
11845static int
11846i386_macro_fusible_p (enum mf_cmp_kind mf_cmp, enum mf_jcc_kind mf_jcc)
11847{
11848 if (mf_cmp == mf_cmp_alu_cmp)
11849 return ((mf_jcc >= mf_jcc_jc && mf_jcc <= mf_jcc_jna)
11850 || mf_jcc == mf_jcc_jl || mf_jcc == mf_jcc_jle);
11851 if (mf_cmp == mf_cmp_incdec)
11852 return (mf_jcc == mf_jcc_je || mf_jcc == mf_jcc_jl
11853 || mf_jcc == mf_jcc_jle);
11854 if (mf_cmp == mf_cmp_test_and)
11855 return 1;
11856 return 0;
11857}
11858
e379e5f3
L
11859/* Return the next non-empty frag. */
11860
11861static fragS *
11862i386_next_non_empty_frag (fragS *fragP)
11863{
11864 /* There may be a frag with a ".fill 0" when there is no room in
11865 the current frag for frag_grow in output_insn. */
11866 for (fragP = fragP->fr_next;
11867 (fragP != NULL
11868 && fragP->fr_type == rs_fill
11869 && fragP->fr_fix == 0);
11870 fragP = fragP->fr_next)
11871 ;
11872 return fragP;
11873}
11874
11875/* Return the next jcc frag after BRANCH_PADDING. */
11876
11877static fragS *
79d72f45 11878i386_next_fusible_jcc_frag (fragS *maybe_cmp_fragP, fragS *pad_fragP)
e379e5f3 11879{
79d72f45
HL
11880 fragS *branch_fragP;
11881 if (!pad_fragP)
e379e5f3
L
11882 return NULL;
11883
79d72f45
HL
11884 if (pad_fragP->fr_type == rs_machine_dependent
11885 && (TYPE_FROM_RELAX_STATE (pad_fragP->fr_subtype)
e379e5f3
L
11886 == BRANCH_PADDING))
11887 {
79d72f45
HL
11888 branch_fragP = i386_next_non_empty_frag (pad_fragP);
11889 if (branch_fragP->fr_type != rs_machine_dependent)
e379e5f3 11890 return NULL;
79d72f45
HL
11891 if (TYPE_FROM_RELAX_STATE (branch_fragP->fr_subtype) == COND_JUMP
11892 && i386_macro_fusible_p (maybe_cmp_fragP->tc_frag_data.mf_type,
11893 pad_fragP->tc_frag_data.mf_type))
11894 return branch_fragP;
e379e5f3
L
11895 }
11896
11897 return NULL;
11898}
11899
11900/* Classify BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags. */
11901
11902static void
11903i386_classify_machine_dependent_frag (fragS *fragP)
11904{
11905 fragS *cmp_fragP;
11906 fragS *pad_fragP;
11907 fragS *branch_fragP;
11908 fragS *next_fragP;
11909 unsigned int max_prefix_length;
11910
11911 if (fragP->tc_frag_data.classified)
11912 return;
11913
11914 /* First scan for BRANCH_PADDING and FUSED_JCC_PADDING. Convert
11915 FUSED_JCC_PADDING and merge BRANCH_PADDING. */
11916 for (next_fragP = fragP;
11917 next_fragP != NULL;
11918 next_fragP = next_fragP->fr_next)
11919 {
11920 next_fragP->tc_frag_data.classified = 1;
11921 if (next_fragP->fr_type == rs_machine_dependent)
11922 switch (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype))
11923 {
11924 case BRANCH_PADDING:
11925 /* The BRANCH_PADDING frag must be followed by a branch
11926 frag. */
11927 branch_fragP = i386_next_non_empty_frag (next_fragP);
11928 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
11929 break;
11930 case FUSED_JCC_PADDING:
11931 /* Check if this is a fused jcc:
11932 FUSED_JCC_PADDING
11933 CMP like instruction
11934 BRANCH_PADDING
11935 COND_JUMP
11936 */
11937 cmp_fragP = i386_next_non_empty_frag (next_fragP);
11938 pad_fragP = i386_next_non_empty_frag (cmp_fragP);
79d72f45 11939 branch_fragP = i386_next_fusible_jcc_frag (next_fragP, pad_fragP);
e379e5f3
L
11940 if (branch_fragP)
11941 {
11942 /* The BRANCH_PADDING frag is merged with the
11943 FUSED_JCC_PADDING frag. */
11944 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
11945 /* CMP like instruction size. */
11946 next_fragP->tc_frag_data.cmp_size = cmp_fragP->fr_fix;
11947 frag_wane (pad_fragP);
11948 /* Skip to branch_fragP. */
11949 next_fragP = branch_fragP;
11950 }
11951 else if (next_fragP->tc_frag_data.max_prefix_length)
11952 {
11953 /* Turn FUSED_JCC_PADDING into BRANCH_PREFIX if it isn't
11954 a fused jcc. */
11955 next_fragP->fr_subtype
11956 = ENCODE_RELAX_STATE (BRANCH_PREFIX, 0);
11957 next_fragP->tc_frag_data.max_bytes
11958 = next_fragP->tc_frag_data.max_prefix_length;
11959 /* This will be updated in the BRANCH_PREFIX scan. */
11960 next_fragP->tc_frag_data.max_prefix_length = 0;
11961 }
11962 else
11963 frag_wane (next_fragP);
11964 break;
11965 }
11966 }
11967
11968 /* Stop if there is no BRANCH_PREFIX. */
11969 if (!align_branch_prefix_size)
11970 return;
11971
11972 /* Scan for BRANCH_PREFIX. */
11973 for (; fragP != NULL; fragP = fragP->fr_next)
11974 {
11975 if (fragP->fr_type != rs_machine_dependent
11976 || (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
11977 != BRANCH_PREFIX))
11978 continue;
11979
11980 /* Count all BRANCH_PREFIX frags before BRANCH_PADDING and
11981 COND_JUMP_PREFIX. */
11982 max_prefix_length = 0;
11983 for (next_fragP = fragP;
11984 next_fragP != NULL;
11985 next_fragP = next_fragP->fr_next)
11986 {
11987 if (next_fragP->fr_type == rs_fill)
11988 /* Skip rs_fill frags. */
11989 continue;
11990 else if (next_fragP->fr_type != rs_machine_dependent)
11991 /* Stop for all other frags. */
11992 break;
11993
11994 /* rs_machine_dependent frags. */
11995 if (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
11996 == BRANCH_PREFIX)
11997 {
11998 /* Count BRANCH_PREFIX frags. */
11999 if (max_prefix_length >= MAX_FUSED_JCC_PADDING_SIZE)
12000 {
12001 max_prefix_length = MAX_FUSED_JCC_PADDING_SIZE;
12002 frag_wane (next_fragP);
12003 }
12004 else
12005 max_prefix_length
12006 += next_fragP->tc_frag_data.max_bytes;
12007 }
12008 else if ((TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12009 == BRANCH_PADDING)
12010 || (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12011 == FUSED_JCC_PADDING))
12012 {
12013 /* Stop at BRANCH_PADDING and FUSED_JCC_PADDING. */
12014 fragP->tc_frag_data.u.padding_fragP = next_fragP;
12015 break;
12016 }
12017 else
12018 /* Stop for other rs_machine_dependent frags. */
12019 break;
12020 }
12021
12022 fragP->tc_frag_data.max_prefix_length = max_prefix_length;
12023
12024 /* Skip to the next frag. */
12025 fragP = next_fragP;
12026 }
12027}
12028
12029/* Compute padding size for
12030
12031 FUSED_JCC_PADDING
12032 CMP like instruction
12033 BRANCH_PADDING
12034 COND_JUMP/UNCOND_JUMP
12035
12036 or
12037
12038 BRANCH_PADDING
12039 COND_JUMP/UNCOND_JUMP
12040 */
12041
12042static int
12043i386_branch_padding_size (fragS *fragP, offsetT address)
12044{
12045 unsigned int offset, size, padding_size;
12046 fragS *branch_fragP = fragP->tc_frag_data.u.branch_fragP;
12047
12048 /* The start address of the BRANCH_PADDING or FUSED_JCC_PADDING frag. */
12049 if (!address)
12050 address = fragP->fr_address;
12051 address += fragP->fr_fix;
12052
12053 /* CMP like instrunction size. */
12054 size = fragP->tc_frag_data.cmp_size;
12055
12056 /* The base size of the branch frag. */
12057 size += branch_fragP->fr_fix;
12058
12059 /* Add opcode and displacement bytes for the rs_machine_dependent
12060 branch frag. */
12061 if (branch_fragP->fr_type == rs_machine_dependent)
12062 size += md_relax_table[branch_fragP->fr_subtype].rlx_length;
12063
12064 /* Check if branch is within boundary and doesn't end at the last
12065 byte. */
12066 offset = address & ((1U << align_branch_power) - 1);
12067 if ((offset + size) >= (1U << align_branch_power))
12068 /* Padding needed to avoid crossing boundary. */
12069 padding_size = (1U << align_branch_power) - offset;
12070 else
12071 /* No padding needed. */
12072 padding_size = 0;
12073
12074 /* The return value may be saved in tc_frag_data.length which is
12075 unsigned byte. */
12076 if (!fits_in_unsigned_byte (padding_size))
12077 abort ();
12078
12079 return padding_size;
12080}
12081
12082/* i386_generic_table_relax_frag()
12083
12084 Handle BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags to
12085 grow/shrink padding to align branch frags. Hand others to
12086 relax_frag(). */
12087
12088long
12089i386_generic_table_relax_frag (segT segment, fragS *fragP, long stretch)
12090{
12091 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12092 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
12093 {
12094 long padding_size = i386_branch_padding_size (fragP, 0);
12095 long grow = padding_size - fragP->tc_frag_data.length;
12096
12097 /* When the BRANCH_PREFIX frag is used, the computed address
12098 must match the actual address and there should be no padding. */
12099 if (fragP->tc_frag_data.padding_address
12100 && (fragP->tc_frag_data.padding_address != fragP->fr_address
12101 || padding_size))
12102 abort ();
12103
12104 /* Update the padding size. */
12105 if (grow)
12106 fragP->tc_frag_data.length = padding_size;
12107
12108 return grow;
12109 }
12110 else if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12111 {
12112 fragS *padding_fragP, *next_fragP;
12113 long padding_size, left_size, last_size;
12114
12115 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
12116 if (!padding_fragP)
12117 /* Use the padding set by the leading BRANCH_PREFIX frag. */
12118 return (fragP->tc_frag_data.length
12119 - fragP->tc_frag_data.last_length);
12120
12121 /* Compute the relative address of the padding frag in the very
12122 first time where the BRANCH_PREFIX frag sizes are zero. */
12123 if (!fragP->tc_frag_data.padding_address)
12124 fragP->tc_frag_data.padding_address
12125 = padding_fragP->fr_address - (fragP->fr_address - stretch);
12126
12127 /* First update the last length from the previous interation. */
12128 left_size = fragP->tc_frag_data.prefix_length;
12129 for (next_fragP = fragP;
12130 next_fragP != padding_fragP;
12131 next_fragP = next_fragP->fr_next)
12132 if (next_fragP->fr_type == rs_machine_dependent
12133 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12134 == BRANCH_PREFIX))
12135 {
12136 if (left_size)
12137 {
12138 int max = next_fragP->tc_frag_data.max_bytes;
12139 if (max)
12140 {
12141 int size;
12142 if (max > left_size)
12143 size = left_size;
12144 else
12145 size = max;
12146 left_size -= size;
12147 next_fragP->tc_frag_data.last_length = size;
12148 }
12149 }
12150 else
12151 next_fragP->tc_frag_data.last_length = 0;
12152 }
12153
12154 /* Check the padding size for the padding frag. */
12155 padding_size = i386_branch_padding_size
12156 (padding_fragP, (fragP->fr_address
12157 + fragP->tc_frag_data.padding_address));
12158
12159 last_size = fragP->tc_frag_data.prefix_length;
12160 /* Check if there is change from the last interation. */
12161 if (padding_size == last_size)
12162 {
12163 /* Update the expected address of the padding frag. */
12164 padding_fragP->tc_frag_data.padding_address
12165 = (fragP->fr_address + padding_size
12166 + fragP->tc_frag_data.padding_address);
12167 return 0;
12168 }
12169
12170 if (padding_size > fragP->tc_frag_data.max_prefix_length)
12171 {
12172 /* No padding if there is no sufficient room. Clear the
12173 expected address of the padding frag. */
12174 padding_fragP->tc_frag_data.padding_address = 0;
12175 padding_size = 0;
12176 }
12177 else
12178 /* Store the expected address of the padding frag. */
12179 padding_fragP->tc_frag_data.padding_address
12180 = (fragP->fr_address + padding_size
12181 + fragP->tc_frag_data.padding_address);
12182
12183 fragP->tc_frag_data.prefix_length = padding_size;
12184
12185 /* Update the length for the current interation. */
12186 left_size = padding_size;
12187 for (next_fragP = fragP;
12188 next_fragP != padding_fragP;
12189 next_fragP = next_fragP->fr_next)
12190 if (next_fragP->fr_type == rs_machine_dependent
12191 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12192 == BRANCH_PREFIX))
12193 {
12194 if (left_size)
12195 {
12196 int max = next_fragP->tc_frag_data.max_bytes;
12197 if (max)
12198 {
12199 int size;
12200 if (max > left_size)
12201 size = left_size;
12202 else
12203 size = max;
12204 left_size -= size;
12205 next_fragP->tc_frag_data.length = size;
12206 }
12207 }
12208 else
12209 next_fragP->tc_frag_data.length = 0;
12210 }
12211
12212 return (fragP->tc_frag_data.length
12213 - fragP->tc_frag_data.last_length);
12214 }
12215 return relax_frag (segment, fragP, stretch);
12216}
12217
ee7fcc42
AM
12218/* md_estimate_size_before_relax()
12219
12220 Called just before relax() for rs_machine_dependent frags. The x86
12221 assembler uses these frags to handle variable size jump
12222 instructions.
12223
12224 Any symbol that is now undefined will not become defined.
12225 Return the correct fr_subtype in the frag.
12226 Return the initial "guess for variable size of frag" to caller.
12227 The guess is actually the growth beyond the fixed part. Whatever
12228 we do to grow the fixed or variable part contributes to our
12229 returned value. */
12230
252b5132 12231int
7016a5d5 12232md_estimate_size_before_relax (fragS *fragP, segT segment)
252b5132 12233{
e379e5f3
L
12234 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12235 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX
12236 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
12237 {
12238 i386_classify_machine_dependent_frag (fragP);
12239 return fragP->tc_frag_data.length;
12240 }
12241
252b5132 12242 /* We've already got fragP->fr_subtype right; all we have to do is
b98ef147
AM
12243 check for un-relaxable symbols. On an ELF system, we can't relax
12244 an externally visible symbol, because it may be overridden by a
12245 shared library. */
12246 if (S_GET_SEGMENT (fragP->fr_symbol) != segment
6d249963 12247#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12248 || (IS_ELF
8dcea932
L
12249 && !elf_symbol_resolved_in_segment_p (fragP->fr_symbol,
12250 fragP->fr_var))
fbeb56a4
DK
12251#endif
12252#if defined (OBJ_COFF) && defined (TE_PE)
7ab9ffdd 12253 || (OUTPUT_FLAVOR == bfd_target_coff_flavour
fbeb56a4 12254 && S_IS_WEAK (fragP->fr_symbol))
b98ef147
AM
12255#endif
12256 )
252b5132 12257 {
b98ef147
AM
12258 /* Symbol is undefined in this segment, or we need to keep a
12259 reloc so that weak symbols can be overridden. */
12260 int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
f86103b7 12261 enum bfd_reloc_code_real reloc_type;
ee7fcc42
AM
12262 unsigned char *opcode;
12263 int old_fr_fix;
eb19308f 12264 fixS *fixP = NULL;
f6af82bd 12265
ee7fcc42 12266 if (fragP->fr_var != NO_RELOC)
1e9cc1c2 12267 reloc_type = (enum bfd_reloc_code_real) fragP->fr_var;
b98ef147 12268 else if (size == 2)
f6af82bd 12269 reloc_type = BFD_RELOC_16_PCREL;
bd7ab16b 12270#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1ef3994a
JB
12271 else if (fragP->tc_frag_data.code64 && fragP->fr_offset == 0
12272 && need_plt32_p (fragP->fr_symbol))
bd7ab16b
L
12273 reloc_type = BFD_RELOC_X86_64_PLT32;
12274#endif
f6af82bd
AM
12275 else
12276 reloc_type = BFD_RELOC_32_PCREL;
252b5132 12277
ee7fcc42
AM
12278 old_fr_fix = fragP->fr_fix;
12279 opcode = (unsigned char *) fragP->fr_opcode;
12280
fddf5b5b 12281 switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
252b5132 12282 {
fddf5b5b
AM
12283 case UNCOND_JUMP:
12284 /* Make jmp (0xeb) a (d)word displacement jump. */
47926f60 12285 opcode[0] = 0xe9;
252b5132 12286 fragP->fr_fix += size;
eb19308f
JB
12287 fixP = fix_new (fragP, old_fr_fix, size,
12288 fragP->fr_symbol,
12289 fragP->fr_offset, 1,
12290 reloc_type);
252b5132
RH
12291 break;
12292
fddf5b5b 12293 case COND_JUMP86:
412167cb
AM
12294 if (size == 2
12295 && (!no_cond_jump_promotion || fragP->fr_var != NO_RELOC))
fddf5b5b
AM
12296 {
12297 /* Negate the condition, and branch past an
12298 unconditional jump. */
12299 opcode[0] ^= 1;
12300 opcode[1] = 3;
12301 /* Insert an unconditional jump. */
12302 opcode[2] = 0xe9;
12303 /* We added two extra opcode bytes, and have a two byte
12304 offset. */
12305 fragP->fr_fix += 2 + 2;
062cd5e7
AS
12306 fix_new (fragP, old_fr_fix + 2, 2,
12307 fragP->fr_symbol,
12308 fragP->fr_offset, 1,
12309 reloc_type);
fddf5b5b
AM
12310 break;
12311 }
12312 /* Fall through. */
12313
12314 case COND_JUMP:
412167cb
AM
12315 if (no_cond_jump_promotion && fragP->fr_var == NO_RELOC)
12316 {
12317 fragP->fr_fix += 1;
3e02c1cc
AM
12318 fixP = fix_new (fragP, old_fr_fix, 1,
12319 fragP->fr_symbol,
12320 fragP->fr_offset, 1,
12321 BFD_RELOC_8_PCREL);
12322 fixP->fx_signed = 1;
412167cb
AM
12323 break;
12324 }
93c2a809 12325
24eab124 12326 /* This changes the byte-displacement jump 0x7N
fddf5b5b 12327 to the (d)word-displacement jump 0x0f,0x8N. */
252b5132 12328 opcode[1] = opcode[0] + 0x10;
f6af82bd 12329 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
47926f60
KH
12330 /* We've added an opcode byte. */
12331 fragP->fr_fix += 1 + size;
eb19308f
JB
12332 fixP = fix_new (fragP, old_fr_fix + 1, size,
12333 fragP->fr_symbol,
12334 fragP->fr_offset, 1,
12335 reloc_type);
252b5132 12336 break;
fddf5b5b
AM
12337
12338 default:
12339 BAD_CASE (fragP->fr_subtype);
12340 break;
252b5132 12341 }
eb19308f
JB
12342
12343 /* All jumps handled here are signed, but don't unconditionally use a
12344 signed limit check for 32 and 16 bit jumps as we want to allow wrap
12345 around at 4G (outside of 64-bit mode) and 64k. */
12346 if (size == 4 && flag_code == CODE_64BIT)
12347 fixP->fx_signed = 1;
12348
252b5132 12349 frag_wane (fragP);
ee7fcc42 12350 return fragP->fr_fix - old_fr_fix;
252b5132 12351 }
93c2a809 12352
93c2a809
AM
12353 /* Guess size depending on current relax state. Initially the relax
12354 state will correspond to a short jump and we return 1, because
12355 the variable part of the frag (the branch offset) is one byte
12356 long. However, we can relax a section more than once and in that
12357 case we must either set fr_subtype back to the unrelaxed state,
12358 or return the value for the appropriate branch. */
12359 return md_relax_table[fragP->fr_subtype].rlx_length;
ee7fcc42
AM
12360}
12361
47926f60
KH
12362/* Called after relax() is finished.
12363
12364 In: Address of frag.
12365 fr_type == rs_machine_dependent.
12366 fr_subtype is what the address relaxed to.
12367
12368 Out: Any fixSs and constants are set up.
12369 Caller will turn frag into a ".space 0". */
12370
252b5132 12371void
7016a5d5
TG
12372md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec ATTRIBUTE_UNUSED,
12373 fragS *fragP)
252b5132 12374{
29b0f896 12375 unsigned char *opcode;
252b5132 12376 unsigned char *where_to_put_displacement = NULL;
847f7ad4
AM
12377 offsetT target_address;
12378 offsetT opcode_address;
252b5132 12379 unsigned int extension = 0;
847f7ad4 12380 offsetT displacement_from_opcode_start;
252b5132 12381
e379e5f3
L
12382 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12383 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING
12384 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12385 {
12386 /* Generate nop padding. */
12387 unsigned int size = fragP->tc_frag_data.length;
12388 if (size)
12389 {
12390 if (size > fragP->tc_frag_data.max_bytes)
12391 abort ();
12392
12393 if (flag_debug)
12394 {
12395 const char *msg;
12396 const char *branch = "branch";
12397 const char *prefix = "";
12398 fragS *padding_fragP;
12399 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
12400 == BRANCH_PREFIX)
12401 {
12402 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
12403 switch (fragP->tc_frag_data.default_prefix)
12404 {
12405 default:
12406 abort ();
12407 break;
12408 case CS_PREFIX_OPCODE:
12409 prefix = " cs";
12410 break;
12411 case DS_PREFIX_OPCODE:
12412 prefix = " ds";
12413 break;
12414 case ES_PREFIX_OPCODE:
12415 prefix = " es";
12416 break;
12417 case FS_PREFIX_OPCODE:
12418 prefix = " fs";
12419 break;
12420 case GS_PREFIX_OPCODE:
12421 prefix = " gs";
12422 break;
12423 case SS_PREFIX_OPCODE:
12424 prefix = " ss";
12425 break;
12426 }
12427 if (padding_fragP)
12428 msg = _("%s:%u: add %d%s at 0x%llx to align "
12429 "%s within %d-byte boundary\n");
12430 else
12431 msg = _("%s:%u: add additional %d%s at 0x%llx to "
12432 "align %s within %d-byte boundary\n");
12433 }
12434 else
12435 {
12436 padding_fragP = fragP;
12437 msg = _("%s:%u: add %d%s-byte nop at 0x%llx to align "
12438 "%s within %d-byte boundary\n");
12439 }
12440
12441 if (padding_fragP)
12442 switch (padding_fragP->tc_frag_data.branch_type)
12443 {
12444 case align_branch_jcc:
12445 branch = "jcc";
12446 break;
12447 case align_branch_fused:
12448 branch = "fused jcc";
12449 break;
12450 case align_branch_jmp:
12451 branch = "jmp";
12452 break;
12453 case align_branch_call:
12454 branch = "call";
12455 break;
12456 case align_branch_indirect:
12457 branch = "indiret branch";
12458 break;
12459 case align_branch_ret:
12460 branch = "ret";
12461 break;
12462 default:
12463 break;
12464 }
12465
12466 fprintf (stdout, msg,
12467 fragP->fr_file, fragP->fr_line, size, prefix,
12468 (long long) fragP->fr_address, branch,
12469 1 << align_branch_power);
12470 }
12471 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12472 memset (fragP->fr_opcode,
12473 fragP->tc_frag_data.default_prefix, size);
12474 else
12475 i386_generate_nops (fragP, (char *) fragP->fr_opcode,
12476 size, 0);
12477 fragP->fr_fix += size;
12478 }
12479 return;
12480 }
12481
252b5132
RH
12482 opcode = (unsigned char *) fragP->fr_opcode;
12483
47926f60 12484 /* Address we want to reach in file space. */
252b5132 12485 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
252b5132 12486
47926f60 12487 /* Address opcode resides at in file space. */
252b5132
RH
12488 opcode_address = fragP->fr_address + fragP->fr_fix;
12489
47926f60 12490 /* Displacement from opcode start to fill into instruction. */
252b5132
RH
12491 displacement_from_opcode_start = target_address - opcode_address;
12492
fddf5b5b 12493 if ((fragP->fr_subtype & BIG) == 0)
252b5132 12494 {
47926f60
KH
12495 /* Don't have to change opcode. */
12496 extension = 1; /* 1 opcode + 1 displacement */
252b5132 12497 where_to_put_displacement = &opcode[1];
fddf5b5b
AM
12498 }
12499 else
12500 {
12501 if (no_cond_jump_promotion
12502 && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
4eed87de
AM
12503 as_warn_where (fragP->fr_file, fragP->fr_line,
12504 _("long jump required"));
252b5132 12505
fddf5b5b
AM
12506 switch (fragP->fr_subtype)
12507 {
12508 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
12509 extension = 4; /* 1 opcode + 4 displacement */
12510 opcode[0] = 0xe9;
12511 where_to_put_displacement = &opcode[1];
12512 break;
252b5132 12513
fddf5b5b
AM
12514 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
12515 extension = 2; /* 1 opcode + 2 displacement */
12516 opcode[0] = 0xe9;
12517 where_to_put_displacement = &opcode[1];
12518 break;
252b5132 12519
fddf5b5b
AM
12520 case ENCODE_RELAX_STATE (COND_JUMP, BIG):
12521 case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
12522 extension = 5; /* 2 opcode + 4 displacement */
12523 opcode[1] = opcode[0] + 0x10;
12524 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12525 where_to_put_displacement = &opcode[2];
12526 break;
252b5132 12527
fddf5b5b
AM
12528 case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
12529 extension = 3; /* 2 opcode + 2 displacement */
12530 opcode[1] = opcode[0] + 0x10;
12531 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12532 where_to_put_displacement = &opcode[2];
12533 break;
252b5132 12534
fddf5b5b
AM
12535 case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
12536 extension = 4;
12537 opcode[0] ^= 1;
12538 opcode[1] = 3;
12539 opcode[2] = 0xe9;
12540 where_to_put_displacement = &opcode[3];
12541 break;
12542
12543 default:
12544 BAD_CASE (fragP->fr_subtype);
12545 break;
12546 }
252b5132 12547 }
fddf5b5b 12548
7b81dfbb
AJ
12549 /* If size if less then four we are sure that the operand fits,
12550 but if it's 4, then it could be that the displacement is larger
12551 then -/+ 2GB. */
12552 if (DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype) == 4
12553 && object_64bit
12554 && ((addressT) (displacement_from_opcode_start - extension
4eed87de
AM
12555 + ((addressT) 1 << 31))
12556 > (((addressT) 2 << 31) - 1)))
7b81dfbb
AJ
12557 {
12558 as_bad_where (fragP->fr_file, fragP->fr_line,
12559 _("jump target out of range"));
12560 /* Make us emit 0. */
12561 displacement_from_opcode_start = extension;
12562 }
47926f60 12563 /* Now put displacement after opcode. */
252b5132
RH
12564 md_number_to_chars ((char *) where_to_put_displacement,
12565 (valueT) (displacement_from_opcode_start - extension),
fddf5b5b 12566 DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
252b5132
RH
12567 fragP->fr_fix += extension;
12568}
12569\f
7016a5d5 12570/* Apply a fixup (fixP) to segment data, once it has been determined
252b5132
RH
12571 by our caller that we have all the info we need to fix it up.
12572
7016a5d5
TG
12573 Parameter valP is the pointer to the value of the bits.
12574
252b5132
RH
12575 On the 386, immediates, displacements, and data pointers are all in
12576 the same (little-endian) format, so we don't need to care about which
12577 we are handling. */
12578
94f592af 12579void
7016a5d5 12580md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 12581{
94f592af 12582 char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
c6682705 12583 valueT value = *valP;
252b5132 12584
f86103b7 12585#if !defined (TE_Mach)
93382f6d
AM
12586 if (fixP->fx_pcrel)
12587 {
12588 switch (fixP->fx_r_type)
12589 {
5865bb77
ILT
12590 default:
12591 break;
12592
d6ab8113
JB
12593 case BFD_RELOC_64:
12594 fixP->fx_r_type = BFD_RELOC_64_PCREL;
12595 break;
93382f6d 12596 case BFD_RELOC_32:
ae8887b5 12597 case BFD_RELOC_X86_64_32S:
93382f6d
AM
12598 fixP->fx_r_type = BFD_RELOC_32_PCREL;
12599 break;
12600 case BFD_RELOC_16:
12601 fixP->fx_r_type = BFD_RELOC_16_PCREL;
12602 break;
12603 case BFD_RELOC_8:
12604 fixP->fx_r_type = BFD_RELOC_8_PCREL;
12605 break;
12606 }
12607 }
252b5132 12608
a161fe53 12609 if (fixP->fx_addsy != NULL
31312f95 12610 && (fixP->fx_r_type == BFD_RELOC_32_PCREL
d6ab8113 12611 || fixP->fx_r_type == BFD_RELOC_64_PCREL
31312f95 12612 || fixP->fx_r_type == BFD_RELOC_16_PCREL
d258b828 12613 || fixP->fx_r_type == BFD_RELOC_8_PCREL)
31312f95 12614 && !use_rela_relocations)
252b5132 12615 {
31312f95
AM
12616 /* This is a hack. There should be a better way to handle this.
12617 This covers for the fact that bfd_install_relocation will
12618 subtract the current location (for partial_inplace, PC relative
12619 relocations); see more below. */
252b5132 12620#ifndef OBJ_AOUT
718ddfc0 12621 if (IS_ELF
252b5132
RH
12622#ifdef TE_PE
12623 || OUTPUT_FLAVOR == bfd_target_coff_flavour
12624#endif
12625 )
12626 value += fixP->fx_where + fixP->fx_frag->fr_address;
12627#endif
12628#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12629 if (IS_ELF)
252b5132 12630 {
6539b54b 12631 segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
2f66722d 12632
6539b54b 12633 if ((sym_seg == seg
2f66722d 12634 || (symbol_section_p (fixP->fx_addsy)
6539b54b 12635 && sym_seg != absolute_section))
af65af87 12636 && !generic_force_reloc (fixP))
2f66722d
AM
12637 {
12638 /* Yes, we add the values in twice. This is because
6539b54b
AM
12639 bfd_install_relocation subtracts them out again. I think
12640 bfd_install_relocation is broken, but I don't dare change
2f66722d
AM
12641 it. FIXME. */
12642 value += fixP->fx_where + fixP->fx_frag->fr_address;
12643 }
252b5132
RH
12644 }
12645#endif
12646#if defined (OBJ_COFF) && defined (TE_PE)
977cdf5a
NC
12647 /* For some reason, the PE format does not store a
12648 section address offset for a PC relative symbol. */
12649 if (S_GET_SEGMENT (fixP->fx_addsy) != seg
7be1c489 12650 || S_IS_WEAK (fixP->fx_addsy))
252b5132
RH
12651 value += md_pcrel_from (fixP);
12652#endif
12653 }
fbeb56a4 12654#if defined (OBJ_COFF) && defined (TE_PE)
f01c1a09
NC
12655 if (fixP->fx_addsy != NULL
12656 && S_IS_WEAK (fixP->fx_addsy)
12657 /* PR 16858: Do not modify weak function references. */
12658 && ! fixP->fx_pcrel)
fbeb56a4 12659 {
296a8689
NC
12660#if !defined (TE_PEP)
12661 /* For x86 PE weak function symbols are neither PC-relative
12662 nor do they set S_IS_FUNCTION. So the only reliable way
12663 to detect them is to check the flags of their containing
12664 section. */
12665 if (S_GET_SEGMENT (fixP->fx_addsy) != NULL
12666 && S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_CODE)
12667 ;
12668 else
12669#endif
fbeb56a4
DK
12670 value -= S_GET_VALUE (fixP->fx_addsy);
12671 }
12672#endif
252b5132
RH
12673
12674 /* Fix a few things - the dynamic linker expects certain values here,
0234cb7c 12675 and we must not disappoint it. */
252b5132 12676#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12677 if (IS_ELF && fixP->fx_addsy)
47926f60
KH
12678 switch (fixP->fx_r_type)
12679 {
12680 case BFD_RELOC_386_PLT32:
3e73aa7c 12681 case BFD_RELOC_X86_64_PLT32:
b9519cfe
L
12682 /* Make the jump instruction point to the address of the operand.
12683 At runtime we merely add the offset to the actual PLT entry.
12684 NB: Subtract the offset size only for jump instructions. */
12685 if (fixP->fx_pcrel)
12686 value = -4;
47926f60 12687 break;
31312f95 12688
13ae64f3
JJ
12689 case BFD_RELOC_386_TLS_GD:
12690 case BFD_RELOC_386_TLS_LDM:
13ae64f3 12691 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
12692 case BFD_RELOC_386_TLS_IE:
12693 case BFD_RELOC_386_TLS_GOTIE:
67a4f2b7 12694 case BFD_RELOC_386_TLS_GOTDESC:
bffbf940
JJ
12695 case BFD_RELOC_X86_64_TLSGD:
12696 case BFD_RELOC_X86_64_TLSLD:
12697 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7 12698 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
00f7efb6
JJ
12699 value = 0; /* Fully resolved at runtime. No addend. */
12700 /* Fallthrough */
12701 case BFD_RELOC_386_TLS_LE:
12702 case BFD_RELOC_386_TLS_LDO_32:
12703 case BFD_RELOC_386_TLS_LE_32:
12704 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 12705 case BFD_RELOC_X86_64_DTPOFF64:
00f7efb6 12706 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113 12707 case BFD_RELOC_X86_64_TPOFF64:
00f7efb6
JJ
12708 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12709 break;
12710
67a4f2b7
AO
12711 case BFD_RELOC_386_TLS_DESC_CALL:
12712 case BFD_RELOC_X86_64_TLSDESC_CALL:
12713 value = 0; /* Fully resolved at runtime. No addend. */
12714 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12715 fixP->fx_done = 0;
12716 return;
12717
47926f60
KH
12718 case BFD_RELOC_VTABLE_INHERIT:
12719 case BFD_RELOC_VTABLE_ENTRY:
12720 fixP->fx_done = 0;
94f592af 12721 return;
47926f60
KH
12722
12723 default:
12724 break;
12725 }
12726#endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) */
a442cac5
JB
12727
12728 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
12729 if (!object_64bit)
12730 value = extend_to_32bit_address (value);
12731
c6682705 12732 *valP = value;
f86103b7 12733#endif /* !defined (TE_Mach) */
3e73aa7c 12734
3e73aa7c 12735 /* Are we finished with this relocation now? */
c6682705 12736 if (fixP->fx_addsy == NULL)
b8188555
JB
12737 {
12738 fixP->fx_done = 1;
12739 switch (fixP->fx_r_type)
12740 {
12741 case BFD_RELOC_X86_64_32S:
12742 fixP->fx_signed = 1;
12743 break;
12744
12745 default:
12746 break;
12747 }
12748 }
fbeb56a4
DK
12749#if defined (OBJ_COFF) && defined (TE_PE)
12750 else if (fixP->fx_addsy != NULL && S_IS_WEAK (fixP->fx_addsy))
12751 {
12752 fixP->fx_done = 0;
12753 /* Remember value for tc_gen_reloc. */
12754 fixP->fx_addnumber = value;
12755 /* Clear out the frag for now. */
12756 value = 0;
12757 }
12758#endif
3e73aa7c
JH
12759 else if (use_rela_relocations)
12760 {
46fb6d5a
JB
12761 if (!disallow_64bit_reloc || fixP->fx_r_type == NO_RELOC)
12762 fixP->fx_no_overflow = 1;
062cd5e7
AS
12763 /* Remember value for tc_gen_reloc. */
12764 fixP->fx_addnumber = value;
3e73aa7c
JH
12765 value = 0;
12766 }
f86103b7 12767
94f592af 12768 md_number_to_chars (p, value, fixP->fx_size);
252b5132 12769}
252b5132 12770\f
6d4af3c2 12771const char *
499ac353 12772md_atof (int type, char *litP, int *sizeP)
252b5132 12773{
499ac353
NC
12774 /* This outputs the LITTLENUMs in REVERSE order;
12775 in accord with the bigendian 386. */
5b7c81bd 12776 return ieee_md_atof (type, litP, sizeP, false);
252b5132
RH
12777}
12778\f
2d545b82 12779static char output_invalid_buf[sizeof (unsigned char) * 2 + 6];
252b5132 12780
252b5132 12781static char *
e3bb37b5 12782output_invalid (int c)
252b5132 12783{
3882b010 12784 if (ISPRINT (c))
f9f21a03
L
12785 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
12786 "'%c'", c);
252b5132 12787 else
f9f21a03 12788 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
2d545b82 12789 "(0x%x)", (unsigned char) c);
252b5132
RH
12790 return output_invalid_buf;
12791}
12792
8a6fb3f9
JB
12793/* Verify that @r can be used in the current context. */
12794
5b7c81bd 12795static bool check_register (const reg_entry *r)
8a6fb3f9
JB
12796{
12797 if (allow_pseudo_reg)
5b7c81bd 12798 return true;
8a6fb3f9
JB
12799
12800 if (operand_type_all_zero (&r->reg_type))
5b7c81bd 12801 return false;
8a6fb3f9
JB
12802
12803 if ((r->reg_type.bitfield.dword
12804 || (r->reg_type.bitfield.class == SReg && r->reg_num > 3)
12805 || r->reg_type.bitfield.class == RegCR
22e00a3f 12806 || r->reg_type.bitfield.class == RegDR)
8a6fb3f9 12807 && !cpu_arch_flags.bitfield.cpui386)
5b7c81bd 12808 return false;
8a6fb3f9 12809
22e00a3f
JB
12810 if (r->reg_type.bitfield.class == RegTR
12811 && (flag_code == CODE_64BIT
12812 || !cpu_arch_flags.bitfield.cpui386
12813 || cpu_arch_isa_flags.bitfield.cpui586
12814 || cpu_arch_isa_flags.bitfield.cpui686))
5b7c81bd 12815 return false;
22e00a3f 12816
8a6fb3f9 12817 if (r->reg_type.bitfield.class == RegMMX && !cpu_arch_flags.bitfield.cpummx)
5b7c81bd 12818 return false;
8a6fb3f9
JB
12819
12820 if (!cpu_arch_flags.bitfield.cpuavx512f)
12821 {
12822 if (r->reg_type.bitfield.zmmword
12823 || r->reg_type.bitfield.class == RegMask)
5b7c81bd 12824 return false;
8a6fb3f9
JB
12825
12826 if (!cpu_arch_flags.bitfield.cpuavx)
12827 {
12828 if (r->reg_type.bitfield.ymmword)
5b7c81bd 12829 return false;
8a6fb3f9
JB
12830
12831 if (!cpu_arch_flags.bitfield.cpusse && r->reg_type.bitfield.xmmword)
5b7c81bd 12832 return false;
8a6fb3f9
JB
12833 }
12834 }
12835
260cd341
LC
12836 if (r->reg_type.bitfield.tmmword
12837 && (!cpu_arch_flags.bitfield.cpuamx_tile
12838 || flag_code != CODE_64BIT))
5b7c81bd 12839 return false;
260cd341 12840
8a6fb3f9 12841 if (r->reg_type.bitfield.class == RegBND && !cpu_arch_flags.bitfield.cpumpx)
5b7c81bd 12842 return false;
8a6fb3f9
JB
12843
12844 /* Don't allow fake index register unless allow_index_reg isn't 0. */
12845 if (!allow_index_reg && r->reg_num == RegIZ)
5b7c81bd 12846 return false;
8a6fb3f9
JB
12847
12848 /* Upper 16 vector registers are only available with VREX in 64bit
12849 mode, and require EVEX encoding. */
12850 if (r->reg_flags & RegVRex)
12851 {
12852 if (!cpu_arch_flags.bitfield.cpuavx512f
12853 || flag_code != CODE_64BIT)
5b7c81bd 12854 return false;
8a6fb3f9 12855
da4977e0
JB
12856 if (i.vec_encoding == vex_encoding_default)
12857 i.vec_encoding = vex_encoding_evex;
12858 else if (i.vec_encoding != vex_encoding_evex)
12859 i.vec_encoding = vex_encoding_error;
8a6fb3f9
JB
12860 }
12861
12862 if (((r->reg_flags & (RegRex64 | RegRex)) || r->reg_type.bitfield.qword)
12863 && (!cpu_arch_flags.bitfield.cpulm || r->reg_type.bitfield.class != RegCR)
12864 && flag_code != CODE_64BIT)
5b7c81bd 12865 return false;
8a6fb3f9
JB
12866
12867 if (r->reg_type.bitfield.class == SReg && r->reg_num == RegFlat
12868 && !intel_syntax)
5b7c81bd 12869 return false;
8a6fb3f9 12870
5b7c81bd 12871 return true;
8a6fb3f9
JB
12872}
12873
af6bdddf 12874/* REG_STRING starts *before* REGISTER_PREFIX. */
252b5132
RH
12875
12876static const reg_entry *
4d1bb795 12877parse_real_register (char *reg_string, char **end_op)
252b5132 12878{
af6bdddf
AM
12879 char *s = reg_string;
12880 char *p;
252b5132
RH
12881 char reg_name_given[MAX_REG_NAME_SIZE + 1];
12882 const reg_entry *r;
12883
12884 /* Skip possible REGISTER_PREFIX and possible whitespace. */
12885 if (*s == REGISTER_PREFIX)
12886 ++s;
12887
12888 if (is_space_char (*s))
12889 ++s;
12890
12891 p = reg_name_given;
af6bdddf 12892 while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
252b5132
RH
12893 {
12894 if (p >= reg_name_given + MAX_REG_NAME_SIZE)
af6bdddf
AM
12895 return (const reg_entry *) NULL;
12896 s++;
252b5132
RH
12897 }
12898
6588847e
DN
12899 /* For naked regs, make sure that we are not dealing with an identifier.
12900 This prevents confusing an identifier like `eax_var' with register
12901 `eax'. */
12902 if (allow_naked_reg && identifier_chars[(unsigned char) *s])
12903 return (const reg_entry *) NULL;
12904
af6bdddf 12905 *end_op = s;
252b5132 12906
629310ab 12907 r = (const reg_entry *) str_hash_find (reg_hash, reg_name_given);
252b5132 12908
5f47d35b 12909 /* Handle floating point regs, allowing spaces in the (i) part. */
6288d05f 12910 if (r == reg_st0)
5f47d35b 12911 {
0e0eea78
JB
12912 if (!cpu_arch_flags.bitfield.cpu8087
12913 && !cpu_arch_flags.bitfield.cpu287
af32b722
JB
12914 && !cpu_arch_flags.bitfield.cpu387
12915 && !allow_pseudo_reg)
0e0eea78
JB
12916 return (const reg_entry *) NULL;
12917
5f47d35b
AM
12918 if (is_space_char (*s))
12919 ++s;
12920 if (*s == '(')
12921 {
af6bdddf 12922 ++s;
5f47d35b
AM
12923 if (is_space_char (*s))
12924 ++s;
12925 if (*s >= '0' && *s <= '7')
12926 {
db557034 12927 int fpr = *s - '0';
af6bdddf 12928 ++s;
5f47d35b
AM
12929 if (is_space_char (*s))
12930 ++s;
12931 if (*s == ')')
12932 {
12933 *end_op = s + 1;
6288d05f 12934 know (r[fpr].reg_num == fpr);
db557034 12935 return r + fpr;
5f47d35b 12936 }
5f47d35b 12937 }
47926f60 12938 /* We have "%st(" then garbage. */
5f47d35b
AM
12939 return (const reg_entry *) NULL;
12940 }
12941 }
12942
8a6fb3f9 12943 return r && check_register (r) ? r : NULL;
252b5132 12944}
4d1bb795
JB
12945
12946/* REG_STRING starts *before* REGISTER_PREFIX. */
12947
12948static const reg_entry *
12949parse_register (char *reg_string, char **end_op)
12950{
12951 const reg_entry *r;
12952
12953 if (*reg_string == REGISTER_PREFIX || allow_naked_reg)
12954 r = parse_real_register (reg_string, end_op);
12955 else
12956 r = NULL;
12957 if (!r)
12958 {
12959 char *save = input_line_pointer;
12960 char c;
12961 symbolS *symbolP;
12962
12963 input_line_pointer = reg_string;
d02603dc 12964 c = get_symbol_name (&reg_string);
4d1bb795 12965 symbolP = symbol_find (reg_string);
64d23078
JB
12966 while (symbolP && S_GET_SEGMENT (symbolP) != reg_section)
12967 {
12968 const expressionS *e = symbol_get_value_expression(symbolP);
12969
12970 if (e->X_op != O_symbol || e->X_add_number)
12971 break;
12972 symbolP = e->X_add_symbol;
12973 }
4d1bb795
JB
12974 if (symbolP && S_GET_SEGMENT (symbolP) == reg_section)
12975 {
12976 const expressionS *e = symbol_get_value_expression (symbolP);
12977
3b55a1d0
JB
12978 know (e->X_op == O_register);
12979 know (e->X_add_number >= 0
12980 && (valueT) e->X_add_number < i386_regtab_size);
12981 r = i386_regtab + e->X_add_number;
12982 if (!check_register (r))
8a6fb3f9 12983 {
3b55a1d0
JB
12984 as_bad (_("register '%s%s' cannot be used here"),
12985 register_prefix, r->reg_name);
12986 r = &bad_reg;
8a6fb3f9 12987 }
3b55a1d0 12988 *end_op = input_line_pointer;
4d1bb795
JB
12989 }
12990 *input_line_pointer = c;
12991 input_line_pointer = save;
12992 }
12993 return r;
12994}
12995
12996int
12997i386_parse_name (char *name, expressionS *e, char *nextcharP)
12998{
4faaa10f 12999 const reg_entry *r = NULL;
4d1bb795
JB
13000 char *end = input_line_pointer;
13001
13002 *end = *nextcharP;
4faaa10f
JB
13003 if (*name == REGISTER_PREFIX || allow_naked_reg)
13004 r = parse_real_register (name, &input_line_pointer);
4d1bb795
JB
13005 if (r && end <= input_line_pointer)
13006 {
13007 *nextcharP = *input_line_pointer;
13008 *input_line_pointer = 0;
8a6fb3f9
JB
13009 if (r != &bad_reg)
13010 {
13011 e->X_op = O_register;
13012 e->X_add_number = r - i386_regtab;
13013 }
13014 else
13015 e->X_op = O_illegal;
4d1bb795
JB
13016 return 1;
13017 }
13018 input_line_pointer = end;
13019 *end = 0;
ee86248c 13020 return intel_syntax ? i386_intel_parse_name (name, e) : 0;
4d1bb795
JB
13021}
13022
13023void
13024md_operand (expressionS *e)
13025{
ee86248c
JB
13026 char *end;
13027 const reg_entry *r;
4d1bb795 13028
ee86248c
JB
13029 switch (*input_line_pointer)
13030 {
13031 case REGISTER_PREFIX:
13032 r = parse_real_register (input_line_pointer, &end);
4d1bb795
JB
13033 if (r)
13034 {
13035 e->X_op = O_register;
13036 e->X_add_number = r - i386_regtab;
13037 input_line_pointer = end;
13038 }
ee86248c
JB
13039 break;
13040
13041 case '[':
9c2799c2 13042 gas_assert (intel_syntax);
ee86248c
JB
13043 end = input_line_pointer++;
13044 expression (e);
13045 if (*input_line_pointer == ']')
13046 {
13047 ++input_line_pointer;
13048 e->X_op_symbol = make_expr_symbol (e);
13049 e->X_add_symbol = NULL;
13050 e->X_add_number = 0;
13051 e->X_op = O_index;
13052 }
13053 else
13054 {
13055 e->X_op = O_absent;
13056 input_line_pointer = end;
13057 }
13058 break;
4d1bb795
JB
13059 }
13060}
13061
252b5132 13062\f
4cc782b5 13063#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
b6f8c7c4 13064const char *md_shortopts = "kVQ:sqnO::";
252b5132 13065#else
b6f8c7c4 13066const char *md_shortopts = "qnO::";
252b5132 13067#endif
6e0b89ee 13068
3e73aa7c 13069#define OPTION_32 (OPTION_MD_BASE + 0)
b3b91714
AM
13070#define OPTION_64 (OPTION_MD_BASE + 1)
13071#define OPTION_DIVIDE (OPTION_MD_BASE + 2)
9103f4f4
L
13072#define OPTION_MARCH (OPTION_MD_BASE + 3)
13073#define OPTION_MTUNE (OPTION_MD_BASE + 4)
1efbbeb4
L
13074#define OPTION_MMNEMONIC (OPTION_MD_BASE + 5)
13075#define OPTION_MSYNTAX (OPTION_MD_BASE + 6)
13076#define OPTION_MINDEX_REG (OPTION_MD_BASE + 7)
13077#define OPTION_MNAKED_REG (OPTION_MD_BASE + 8)
bd5dea88 13078#define OPTION_MRELAX_RELOCATIONS (OPTION_MD_BASE + 9)
c0f3af97 13079#define OPTION_MSSE2AVX (OPTION_MD_BASE + 10)
daf50ae7 13080#define OPTION_MSSE_CHECK (OPTION_MD_BASE + 11)
7bab8ab5
JB
13081#define OPTION_MOPERAND_CHECK (OPTION_MD_BASE + 12)
13082#define OPTION_MAVXSCALAR (OPTION_MD_BASE + 13)
13083#define OPTION_X32 (OPTION_MD_BASE + 14)
7e8b059b 13084#define OPTION_MADD_BND_PREFIX (OPTION_MD_BASE + 15)
43234a1e
L
13085#define OPTION_MEVEXLIG (OPTION_MD_BASE + 16)
13086#define OPTION_MEVEXWIG (OPTION_MD_BASE + 17)
167ad85b 13087#define OPTION_MBIG_OBJ (OPTION_MD_BASE + 18)
d1982f93 13088#define OPTION_MOMIT_LOCK_PREFIX (OPTION_MD_BASE + 19)
d3d3c6db 13089#define OPTION_MEVEXRCIG (OPTION_MD_BASE + 20)
8dcea932 13090#define OPTION_MSHARED (OPTION_MD_BASE + 21)
5db04b09
L
13091#define OPTION_MAMD64 (OPTION_MD_BASE + 22)
13092#define OPTION_MINTEL64 (OPTION_MD_BASE + 23)
e4e00185 13093#define OPTION_MFENCE_AS_LOCK_ADD (OPTION_MD_BASE + 24)
b4a3a7b4 13094#define OPTION_X86_USED_NOTE (OPTION_MD_BASE + 25)
03751133 13095#define OPTION_MVEXWIG (OPTION_MD_BASE + 26)
e379e5f3
L
13096#define OPTION_MALIGN_BRANCH_BOUNDARY (OPTION_MD_BASE + 27)
13097#define OPTION_MALIGN_BRANCH_PREFIX_SIZE (OPTION_MD_BASE + 28)
13098#define OPTION_MALIGN_BRANCH (OPTION_MD_BASE + 29)
76cf450b 13099#define OPTION_MBRANCHES_WITH_32B_BOUNDARIES (OPTION_MD_BASE + 30)
ae531041
L
13100#define OPTION_MLFENCE_AFTER_LOAD (OPTION_MD_BASE + 31)
13101#define OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH (OPTION_MD_BASE + 32)
13102#define OPTION_MLFENCE_BEFORE_RET (OPTION_MD_BASE + 33)
c8480b58 13103#define OPTION_MUSE_UNALIGNED_VECTOR_MOVE (OPTION_MD_BASE + 34)
b3b91714 13104
99ad8390
NC
13105struct option md_longopts[] =
13106{
3e73aa7c 13107 {"32", no_argument, NULL, OPTION_32},
321098a5 13108#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 13109 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c 13110 {"64", no_argument, NULL, OPTION_64},
351f65ca
L
13111#endif
13112#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 13113 {"x32", no_argument, NULL, OPTION_X32},
8dcea932 13114 {"mshared", no_argument, NULL, OPTION_MSHARED},
b4a3a7b4 13115 {"mx86-used-note", required_argument, NULL, OPTION_X86_USED_NOTE},
6e0b89ee 13116#endif
b3b91714 13117 {"divide", no_argument, NULL, OPTION_DIVIDE},
9103f4f4
L
13118 {"march", required_argument, NULL, OPTION_MARCH},
13119 {"mtune", required_argument, NULL, OPTION_MTUNE},
1efbbeb4
L
13120 {"mmnemonic", required_argument, NULL, OPTION_MMNEMONIC},
13121 {"msyntax", required_argument, NULL, OPTION_MSYNTAX},
13122 {"mindex-reg", no_argument, NULL, OPTION_MINDEX_REG},
13123 {"mnaked-reg", no_argument, NULL, OPTION_MNAKED_REG},
c0f3af97 13124 {"msse2avx", no_argument, NULL, OPTION_MSSE2AVX},
c8480b58 13125 {"muse-unaligned-vector-move", no_argument, NULL, OPTION_MUSE_UNALIGNED_VECTOR_MOVE},
daf50ae7 13126 {"msse-check", required_argument, NULL, OPTION_MSSE_CHECK},
7bab8ab5 13127 {"moperand-check", required_argument, NULL, OPTION_MOPERAND_CHECK},
539f890d 13128 {"mavxscalar", required_argument, NULL, OPTION_MAVXSCALAR},
03751133 13129 {"mvexwig", required_argument, NULL, OPTION_MVEXWIG},
7e8b059b 13130 {"madd-bnd-prefix", no_argument, NULL, OPTION_MADD_BND_PREFIX},
43234a1e
L
13131 {"mevexlig", required_argument, NULL, OPTION_MEVEXLIG},
13132 {"mevexwig", required_argument, NULL, OPTION_MEVEXWIG},
167ad85b
TG
13133# if defined (TE_PE) || defined (TE_PEP)
13134 {"mbig-obj", no_argument, NULL, OPTION_MBIG_OBJ},
13135#endif
d1982f93 13136 {"momit-lock-prefix", required_argument, NULL, OPTION_MOMIT_LOCK_PREFIX},
e4e00185 13137 {"mfence-as-lock-add", required_argument, NULL, OPTION_MFENCE_AS_LOCK_ADD},
0cb4071e 13138 {"mrelax-relocations", required_argument, NULL, OPTION_MRELAX_RELOCATIONS},
d3d3c6db 13139 {"mevexrcig", required_argument, NULL, OPTION_MEVEXRCIG},
e379e5f3
L
13140 {"malign-branch-boundary", required_argument, NULL, OPTION_MALIGN_BRANCH_BOUNDARY},
13141 {"malign-branch-prefix-size", required_argument, NULL, OPTION_MALIGN_BRANCH_PREFIX_SIZE},
13142 {"malign-branch", required_argument, NULL, OPTION_MALIGN_BRANCH},
76cf450b 13143 {"mbranches-within-32B-boundaries", no_argument, NULL, OPTION_MBRANCHES_WITH_32B_BOUNDARIES},
ae531041
L
13144 {"mlfence-after-load", required_argument, NULL, OPTION_MLFENCE_AFTER_LOAD},
13145 {"mlfence-before-indirect-branch", required_argument, NULL,
13146 OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH},
13147 {"mlfence-before-ret", required_argument, NULL, OPTION_MLFENCE_BEFORE_RET},
5db04b09
L
13148 {"mamd64", no_argument, NULL, OPTION_MAMD64},
13149 {"mintel64", no_argument, NULL, OPTION_MINTEL64},
252b5132
RH
13150 {NULL, no_argument, NULL, 0}
13151};
13152size_t md_longopts_size = sizeof (md_longopts);
13153
13154int
17b9d67d 13155md_parse_option (int c, const char *arg)
252b5132 13156{
91d6fa6a 13157 unsigned int j;
e379e5f3 13158 char *arch, *next, *saved, *type;
9103f4f4 13159
252b5132
RH
13160 switch (c)
13161 {
12b55ccc
L
13162 case 'n':
13163 optimize_align_code = 0;
13164 break;
13165
a38cf1db
AM
13166 case 'q':
13167 quiet_warnings = 1;
252b5132
RH
13168 break;
13169
13170#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
a38cf1db
AM
13171 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
13172 should be emitted or not. FIXME: Not implemented. */
13173 case 'Q':
d4693039
JB
13174 if ((arg[0] != 'y' && arg[0] != 'n') || arg[1])
13175 return 0;
252b5132
RH
13176 break;
13177
13178 /* -V: SVR4 argument to print version ID. */
13179 case 'V':
13180 print_version_id ();
13181 break;
13182
a38cf1db
AM
13183 /* -k: Ignore for FreeBSD compatibility. */
13184 case 'k':
252b5132 13185 break;
4cc782b5
ILT
13186
13187 case 's':
13188 /* -s: On i386 Solaris, this tells the native assembler to use
29b0f896 13189 .stab instead of .stab.excl. We always use .stab anyhow. */
4cc782b5 13190 break;
8dcea932
L
13191
13192 case OPTION_MSHARED:
13193 shared = 1;
13194 break;
b4a3a7b4
L
13195
13196 case OPTION_X86_USED_NOTE:
13197 if (strcasecmp (arg, "yes") == 0)
13198 x86_used_note = 1;
13199 else if (strcasecmp (arg, "no") == 0)
13200 x86_used_note = 0;
13201 else
13202 as_fatal (_("invalid -mx86-used-note= option: `%s'"), arg);
13203 break;
13204
13205
99ad8390 13206#endif
321098a5 13207#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 13208 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c
JH
13209 case OPTION_64:
13210 {
13211 const char **list, **l;
13212
3e73aa7c
JH
13213 list = bfd_target_list ();
13214 for (l = list; *l != NULL; l++)
08dedd66 13215 if (startswith (*l, "elf64-x86-64")
99ad8390
NC
13216 || strcmp (*l, "coff-x86-64") == 0
13217 || strcmp (*l, "pe-x86-64") == 0
d382c579
TG
13218 || strcmp (*l, "pei-x86-64") == 0
13219 || strcmp (*l, "mach-o-x86-64") == 0)
6e0b89ee
AM
13220 {
13221 default_arch = "x86_64";
13222 break;
13223 }
3e73aa7c 13224 if (*l == NULL)
2b5d6a91 13225 as_fatal (_("no compiled in support for x86_64"));
3e73aa7c
JH
13226 free (list);
13227 }
13228 break;
13229#endif
252b5132 13230
351f65ca 13231#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 13232 case OPTION_X32:
351f65ca
L
13233 if (IS_ELF)
13234 {
13235 const char **list, **l;
13236
13237 list = bfd_target_list ();
13238 for (l = list; *l != NULL; l++)
08dedd66 13239 if (startswith (*l, "elf32-x86-64"))
351f65ca
L
13240 {
13241 default_arch = "x86_64:32";
13242 break;
13243 }
13244 if (*l == NULL)
2b5d6a91 13245 as_fatal (_("no compiled in support for 32bit x86_64"));
351f65ca
L
13246 free (list);
13247 }
13248 else
13249 as_fatal (_("32bit x86_64 is only supported for ELF"));
13250 break;
13251#endif
13252
6e0b89ee
AM
13253 case OPTION_32:
13254 default_arch = "i386";
13255 break;
13256
b3b91714
AM
13257 case OPTION_DIVIDE:
13258#ifdef SVR4_COMMENT_CHARS
13259 {
13260 char *n, *t;
13261 const char *s;
13262
add39d23 13263 n = XNEWVEC (char, strlen (i386_comment_chars) + 1);
b3b91714
AM
13264 t = n;
13265 for (s = i386_comment_chars; *s != '\0'; s++)
13266 if (*s != '/')
13267 *t++ = *s;
13268 *t = '\0';
13269 i386_comment_chars = n;
13270 }
13271#endif
13272 break;
13273
9103f4f4 13274 case OPTION_MARCH:
293f5f65
L
13275 saved = xstrdup (arg);
13276 arch = saved;
13277 /* Allow -march=+nosse. */
13278 if (*arch == '+')
13279 arch++;
6305a203 13280 do
9103f4f4 13281 {
6305a203 13282 if (*arch == '.')
2b5d6a91 13283 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
13284 next = strchr (arch, '+');
13285 if (next)
13286 *next++ = '\0';
91d6fa6a 13287 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 13288 {
ae89daec
JB
13289 if (arch == saved && cpu_arch[j].type != PROCESSOR_NONE
13290 && strcmp (arch, cpu_arch[j].name) == 0)
ccc9c027 13291 {
6305a203 13292 /* Processor. */
ae89daec 13293 if (! cpu_arch[j].enable.bitfield.cpui386)
1ded5609
JB
13294 continue;
13295
91d6fa6a 13296 cpu_arch_name = cpu_arch[j].name;
d92c7521 13297 free (cpu_sub_arch_name);
6305a203 13298 cpu_sub_arch_name = NULL;
ae89daec 13299 cpu_arch_flags = cpu_arch[j].enable;
91d6fa6a 13300 cpu_arch_isa = cpu_arch[j].type;
ae89daec 13301 cpu_arch_isa_flags = cpu_arch[j].enable;
6305a203
L
13302 if (!cpu_arch_tune_set)
13303 {
13304 cpu_arch_tune = cpu_arch_isa;
13305 cpu_arch_tune_flags = cpu_arch_isa_flags;
13306 }
13307 break;
13308 }
ae89daec
JB
13309 else if (cpu_arch[j].type == PROCESSOR_NONE
13310 && strcmp (arch, cpu_arch[j].name) == 0
13311 && !cpu_flags_all_zero (&cpu_arch[j].enable))
6305a203 13312 {
33eaf5de 13313 /* ISA extension. */
6305a203 13314 i386_cpu_flags flags;
309d3373 13315
293f5f65 13316 flags = cpu_flags_or (cpu_arch_flags,
ae89daec 13317 cpu_arch[j].enable);
81486035 13318
5b64d091 13319 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
6305a203 13320 {
ae89daec 13321 extend_cpu_sub_arch_name (arch);
6305a203 13322 cpu_arch_flags = flags;
a586129e 13323 cpu_arch_isa_flags = flags;
6305a203 13324 }
0089dace
L
13325 else
13326 cpu_arch_isa_flags
13327 = cpu_flags_or (cpu_arch_isa_flags,
ae89daec 13328 cpu_arch[j].enable);
6305a203 13329 break;
ccc9c027 13330 }
9103f4f4 13331 }
6305a203 13332
ae89daec 13333 if (j >= ARRAY_SIZE (cpu_arch) && startswith (arch, "no"))
293f5f65 13334 {
33eaf5de 13335 /* Disable an ISA extension. */
ae89daec
JB
13336 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13337 if (cpu_arch[j].type == PROCESSOR_NONE
13338 && strcmp (arch + 2, cpu_arch[j].name) == 0)
293f5f65
L
13339 {
13340 i386_cpu_flags flags;
13341
13342 flags = cpu_flags_and_not (cpu_arch_flags,
ae89daec 13343 cpu_arch[j].disable);
293f5f65
L
13344 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
13345 {
8180707f 13346 extend_cpu_sub_arch_name (arch);
293f5f65
L
13347 cpu_arch_flags = flags;
13348 cpu_arch_isa_flags = flags;
13349 }
13350 break;
13351 }
293f5f65
L
13352 }
13353
91d6fa6a 13354 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 13355 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
13356
13357 arch = next;
9103f4f4 13358 }
293f5f65
L
13359 while (next != NULL);
13360 free (saved);
9103f4f4
L
13361 break;
13362
13363 case OPTION_MTUNE:
13364 if (*arg == '.')
2b5d6a91 13365 as_fatal (_("invalid -mtune= option: `%s'"), arg);
91d6fa6a 13366 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 13367 {
ae89daec
JB
13368 if (cpu_arch[j].type != PROCESSOR_NONE
13369 && strcmp (arg, cpu_arch[j].name) == 0)
9103f4f4 13370 {
ccc9c027 13371 cpu_arch_tune_set = 1;
91d6fa6a 13372 cpu_arch_tune = cpu_arch [j].type;
ae89daec 13373 cpu_arch_tune_flags = cpu_arch[j].enable;
9103f4f4
L
13374 break;
13375 }
13376 }
91d6fa6a 13377 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 13378 as_fatal (_("invalid -mtune= option: `%s'"), arg);
9103f4f4
L
13379 break;
13380
1efbbeb4
L
13381 case OPTION_MMNEMONIC:
13382 if (strcasecmp (arg, "att") == 0)
13383 intel_mnemonic = 0;
13384 else if (strcasecmp (arg, "intel") == 0)
13385 intel_mnemonic = 1;
13386 else
2b5d6a91 13387 as_fatal (_("invalid -mmnemonic= option: `%s'"), arg);
1efbbeb4
L
13388 break;
13389
13390 case OPTION_MSYNTAX:
13391 if (strcasecmp (arg, "att") == 0)
13392 intel_syntax = 0;
13393 else if (strcasecmp (arg, "intel") == 0)
13394 intel_syntax = 1;
13395 else
2b5d6a91 13396 as_fatal (_("invalid -msyntax= option: `%s'"), arg);
1efbbeb4
L
13397 break;
13398
13399 case OPTION_MINDEX_REG:
13400 allow_index_reg = 1;
13401 break;
13402
13403 case OPTION_MNAKED_REG:
13404 allow_naked_reg = 1;
13405 break;
13406
c0f3af97
L
13407 case OPTION_MSSE2AVX:
13408 sse2avx = 1;
13409 break;
13410
c8480b58
L
13411 case OPTION_MUSE_UNALIGNED_VECTOR_MOVE:
13412 use_unaligned_vector_move = 1;
13413 break;
13414
daf50ae7
L
13415 case OPTION_MSSE_CHECK:
13416 if (strcasecmp (arg, "error") == 0)
7bab8ab5 13417 sse_check = check_error;
daf50ae7 13418 else if (strcasecmp (arg, "warning") == 0)
7bab8ab5 13419 sse_check = check_warning;
daf50ae7 13420 else if (strcasecmp (arg, "none") == 0)
7bab8ab5 13421 sse_check = check_none;
daf50ae7 13422 else
2b5d6a91 13423 as_fatal (_("invalid -msse-check= option: `%s'"), arg);
daf50ae7
L
13424 break;
13425
7bab8ab5
JB
13426 case OPTION_MOPERAND_CHECK:
13427 if (strcasecmp (arg, "error") == 0)
13428 operand_check = check_error;
13429 else if (strcasecmp (arg, "warning") == 0)
13430 operand_check = check_warning;
13431 else if (strcasecmp (arg, "none") == 0)
13432 operand_check = check_none;
13433 else
13434 as_fatal (_("invalid -moperand-check= option: `%s'"), arg);
13435 break;
13436
539f890d
L
13437 case OPTION_MAVXSCALAR:
13438 if (strcasecmp (arg, "128") == 0)
13439 avxscalar = vex128;
13440 else if (strcasecmp (arg, "256") == 0)
13441 avxscalar = vex256;
13442 else
2b5d6a91 13443 as_fatal (_("invalid -mavxscalar= option: `%s'"), arg);
539f890d
L
13444 break;
13445
03751133
L
13446 case OPTION_MVEXWIG:
13447 if (strcmp (arg, "0") == 0)
40c9c8de 13448 vexwig = vexw0;
03751133 13449 else if (strcmp (arg, "1") == 0)
40c9c8de 13450 vexwig = vexw1;
03751133
L
13451 else
13452 as_fatal (_("invalid -mvexwig= option: `%s'"), arg);
13453 break;
13454
7e8b059b
L
13455 case OPTION_MADD_BND_PREFIX:
13456 add_bnd_prefix = 1;
13457 break;
13458
43234a1e
L
13459 case OPTION_MEVEXLIG:
13460 if (strcmp (arg, "128") == 0)
13461 evexlig = evexl128;
13462 else if (strcmp (arg, "256") == 0)
13463 evexlig = evexl256;
13464 else if (strcmp (arg, "512") == 0)
13465 evexlig = evexl512;
13466 else
13467 as_fatal (_("invalid -mevexlig= option: `%s'"), arg);
13468 break;
13469
d3d3c6db
IT
13470 case OPTION_MEVEXRCIG:
13471 if (strcmp (arg, "rne") == 0)
13472 evexrcig = rne;
13473 else if (strcmp (arg, "rd") == 0)
13474 evexrcig = rd;
13475 else if (strcmp (arg, "ru") == 0)
13476 evexrcig = ru;
13477 else if (strcmp (arg, "rz") == 0)
13478 evexrcig = rz;
13479 else
13480 as_fatal (_("invalid -mevexrcig= option: `%s'"), arg);
13481 break;
13482
43234a1e
L
13483 case OPTION_MEVEXWIG:
13484 if (strcmp (arg, "0") == 0)
13485 evexwig = evexw0;
13486 else if (strcmp (arg, "1") == 0)
13487 evexwig = evexw1;
13488 else
13489 as_fatal (_("invalid -mevexwig= option: `%s'"), arg);
13490 break;
13491
167ad85b
TG
13492# if defined (TE_PE) || defined (TE_PEP)
13493 case OPTION_MBIG_OBJ:
13494 use_big_obj = 1;
13495 break;
13496#endif
13497
d1982f93 13498 case OPTION_MOMIT_LOCK_PREFIX:
d022bddd
IT
13499 if (strcasecmp (arg, "yes") == 0)
13500 omit_lock_prefix = 1;
13501 else if (strcasecmp (arg, "no") == 0)
13502 omit_lock_prefix = 0;
13503 else
13504 as_fatal (_("invalid -momit-lock-prefix= option: `%s'"), arg);
13505 break;
13506
e4e00185
AS
13507 case OPTION_MFENCE_AS_LOCK_ADD:
13508 if (strcasecmp (arg, "yes") == 0)
13509 avoid_fence = 1;
13510 else if (strcasecmp (arg, "no") == 0)
13511 avoid_fence = 0;
13512 else
13513 as_fatal (_("invalid -mfence-as-lock-add= option: `%s'"), arg);
13514 break;
13515
ae531041
L
13516 case OPTION_MLFENCE_AFTER_LOAD:
13517 if (strcasecmp (arg, "yes") == 0)
13518 lfence_after_load = 1;
13519 else if (strcasecmp (arg, "no") == 0)
13520 lfence_after_load = 0;
13521 else
13522 as_fatal (_("invalid -mlfence-after-load= option: `%s'"), arg);
13523 break;
13524
13525 case OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH:
13526 if (strcasecmp (arg, "all") == 0)
a09f656b 13527 {
13528 lfence_before_indirect_branch = lfence_branch_all;
13529 if (lfence_before_ret == lfence_before_ret_none)
13530 lfence_before_ret = lfence_before_ret_shl;
13531 }
ae531041
L
13532 else if (strcasecmp (arg, "memory") == 0)
13533 lfence_before_indirect_branch = lfence_branch_memory;
13534 else if (strcasecmp (arg, "register") == 0)
13535 lfence_before_indirect_branch = lfence_branch_register;
13536 else if (strcasecmp (arg, "none") == 0)
13537 lfence_before_indirect_branch = lfence_branch_none;
13538 else
13539 as_fatal (_("invalid -mlfence-before-indirect-branch= option: `%s'"),
13540 arg);
13541 break;
13542
13543 case OPTION_MLFENCE_BEFORE_RET:
13544 if (strcasecmp (arg, "or") == 0)
13545 lfence_before_ret = lfence_before_ret_or;
13546 else if (strcasecmp (arg, "not") == 0)
13547 lfence_before_ret = lfence_before_ret_not;
a09f656b 13548 else if (strcasecmp (arg, "shl") == 0 || strcasecmp (arg, "yes") == 0)
13549 lfence_before_ret = lfence_before_ret_shl;
ae531041
L
13550 else if (strcasecmp (arg, "none") == 0)
13551 lfence_before_ret = lfence_before_ret_none;
13552 else
13553 as_fatal (_("invalid -mlfence-before-ret= option: `%s'"),
13554 arg);
13555 break;
13556
0cb4071e
L
13557 case OPTION_MRELAX_RELOCATIONS:
13558 if (strcasecmp (arg, "yes") == 0)
13559 generate_relax_relocations = 1;
13560 else if (strcasecmp (arg, "no") == 0)
13561 generate_relax_relocations = 0;
13562 else
13563 as_fatal (_("invalid -mrelax-relocations= option: `%s'"), arg);
13564 break;
13565
e379e5f3
L
13566 case OPTION_MALIGN_BRANCH_BOUNDARY:
13567 {
13568 char *end;
13569 long int align = strtoul (arg, &end, 0);
13570 if (*end == '\0')
13571 {
13572 if (align == 0)
13573 {
13574 align_branch_power = 0;
13575 break;
13576 }
13577 else if (align >= 16)
13578 {
13579 int align_power;
13580 for (align_power = 0;
13581 (align & 1) == 0;
13582 align >>= 1, align_power++)
13583 continue;
13584 /* Limit alignment power to 31. */
13585 if (align == 1 && align_power < 32)
13586 {
13587 align_branch_power = align_power;
13588 break;
13589 }
13590 }
13591 }
13592 as_fatal (_("invalid -malign-branch-boundary= value: %s"), arg);
13593 }
13594 break;
13595
13596 case OPTION_MALIGN_BRANCH_PREFIX_SIZE:
13597 {
13598 char *end;
13599 int align = strtoul (arg, &end, 0);
13600 /* Some processors only support 5 prefixes. */
13601 if (*end == '\0' && align >= 0 && align < 6)
13602 {
13603 align_branch_prefix_size = align;
13604 break;
13605 }
13606 as_fatal (_("invalid -malign-branch-prefix-size= value: %s"),
13607 arg);
13608 }
13609 break;
13610
13611 case OPTION_MALIGN_BRANCH:
13612 align_branch = 0;
13613 saved = xstrdup (arg);
13614 type = saved;
13615 do
13616 {
13617 next = strchr (type, '+');
13618 if (next)
13619 *next++ = '\0';
13620 if (strcasecmp (type, "jcc") == 0)
13621 align_branch |= align_branch_jcc_bit;
13622 else if (strcasecmp (type, "fused") == 0)
13623 align_branch |= align_branch_fused_bit;
13624 else if (strcasecmp (type, "jmp") == 0)
13625 align_branch |= align_branch_jmp_bit;
13626 else if (strcasecmp (type, "call") == 0)
13627 align_branch |= align_branch_call_bit;
13628 else if (strcasecmp (type, "ret") == 0)
13629 align_branch |= align_branch_ret_bit;
13630 else if (strcasecmp (type, "indirect") == 0)
13631 align_branch |= align_branch_indirect_bit;
13632 else
13633 as_fatal (_("invalid -malign-branch= option: `%s'"), arg);
13634 type = next;
13635 }
13636 while (next != NULL);
13637 free (saved);
13638 break;
13639
76cf450b
L
13640 case OPTION_MBRANCHES_WITH_32B_BOUNDARIES:
13641 align_branch_power = 5;
13642 align_branch_prefix_size = 5;
13643 align_branch = (align_branch_jcc_bit
13644 | align_branch_fused_bit
13645 | align_branch_jmp_bit);
13646 break;
13647
5db04b09 13648 case OPTION_MAMD64:
4b5aaf5f 13649 isa64 = amd64;
5db04b09
L
13650 break;
13651
13652 case OPTION_MINTEL64:
4b5aaf5f 13653 isa64 = intel64;
5db04b09
L
13654 break;
13655
b6f8c7c4
L
13656 case 'O':
13657 if (arg == NULL)
13658 {
13659 optimize = 1;
13660 /* Turn off -Os. */
13661 optimize_for_space = 0;
13662 }
13663 else if (*arg == 's')
13664 {
13665 optimize_for_space = 1;
13666 /* Turn on all encoding optimizations. */
41fd2579 13667 optimize = INT_MAX;
b6f8c7c4
L
13668 }
13669 else
13670 {
13671 optimize = atoi (arg);
13672 /* Turn off -Os. */
13673 optimize_for_space = 0;
13674 }
13675 break;
13676
252b5132
RH
13677 default:
13678 return 0;
13679 }
13680 return 1;
13681}
13682
8a2c8fef
L
13683#define MESSAGE_TEMPLATE \
13684" "
13685
293f5f65
L
13686static char *
13687output_message (FILE *stream, char *p, char *message, char *start,
13688 int *left_p, const char *name, int len)
13689{
13690 int size = sizeof (MESSAGE_TEMPLATE);
13691 int left = *left_p;
13692
13693 /* Reserve 2 spaces for ", " or ",\0" */
13694 left -= len + 2;
13695
13696 /* Check if there is any room. */
13697 if (left >= 0)
13698 {
13699 if (p != start)
13700 {
13701 *p++ = ',';
13702 *p++ = ' ';
13703 }
13704 p = mempcpy (p, name, len);
13705 }
13706 else
13707 {
13708 /* Output the current message now and start a new one. */
13709 *p++ = ',';
13710 *p = '\0';
13711 fprintf (stream, "%s\n", message);
13712 p = start;
13713 left = size - (start - message) - len - 2;
13714
13715 gas_assert (left >= 0);
13716
13717 p = mempcpy (p, name, len);
13718 }
13719
13720 *left_p = left;
13721 return p;
13722}
13723
8a2c8fef 13724static void
1ded5609 13725show_arch (FILE *stream, int ext, int check)
8a2c8fef
L
13726{
13727 static char message[] = MESSAGE_TEMPLATE;
13728 char *start = message + 27;
13729 char *p;
13730 int size = sizeof (MESSAGE_TEMPLATE);
13731 int left;
13732 const char *name;
13733 int len;
13734 unsigned int j;
13735
13736 p = start;
13737 left = size - (start - message);
3ce2ebcf
JB
13738
13739 if (!ext && check)
13740 {
13741 p = output_message (stream, p, message, start, &left,
13742 STRING_COMMA_LEN ("default"));
f68697e8
JB
13743 p = output_message (stream, p, message, start, &left,
13744 STRING_COMMA_LEN ("push"));
13745 p = output_message (stream, p, message, start, &left,
13746 STRING_COMMA_LEN ("pop"));
3ce2ebcf
JB
13747 }
13748
8a2c8fef
L
13749 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13750 {
13751 /* Should it be skipped? */
13752 if (cpu_arch [j].skip)
13753 continue;
13754
13755 name = cpu_arch [j].name;
13756 len = cpu_arch [j].len;
ae89daec 13757 if (cpu_arch[j].type == PROCESSOR_NONE)
8a2c8fef
L
13758 {
13759 /* It is an extension. Skip if we aren't asked to show it. */
ae89daec 13760 if (!ext || cpu_flags_all_zero (&cpu_arch[j].enable))
8a2c8fef
L
13761 continue;
13762 }
13763 else if (ext)
13764 {
13765 /* It is an processor. Skip if we show only extension. */
13766 continue;
13767 }
ae89daec 13768 else if (check && ! cpu_arch[j].enable.bitfield.cpui386)
1ded5609
JB
13769 {
13770 /* It is an impossible processor - skip. */
13771 continue;
13772 }
8a2c8fef 13773
293f5f65 13774 p = output_message (stream, p, message, start, &left, name, len);
8a2c8fef
L
13775 }
13776
293f5f65
L
13777 /* Display disabled extensions. */
13778 if (ext)
ae89daec 13779 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
293f5f65 13780 {
ae89daec
JB
13781 char *str;
13782
13783 if (cpu_arch[j].type != PROCESSOR_NONE
13784 || !cpu_flags_all_zero (&cpu_arch[j].enable))
13785 continue;
13786 str = xasprintf ("no%s", cpu_arch[j].name);
13787 p = output_message (stream, p, message, start, &left, str,
13788 strlen (str));
13789 free (str);
293f5f65
L
13790 }
13791
8a2c8fef
L
13792 *p = '\0';
13793 fprintf (stream, "%s\n", message);
13794}
13795
252b5132 13796void
8a2c8fef 13797md_show_usage (FILE *stream)
252b5132 13798{
4cc782b5
ILT
13799#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13800 fprintf (stream, _("\
d4693039 13801 -Qy, -Qn ignored\n\
a38cf1db 13802 -V print assembler version number\n\
b3b91714
AM
13803 -k ignored\n"));
13804#endif
13805 fprintf (stream, _("\
7ebd68d1
NC
13806 -n do not optimize code alignment\n\
13807 -O{012s} attempt some code optimizations\n\
b3b91714
AM
13808 -q quieten some warnings\n"));
13809#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13810 fprintf (stream, _("\
a38cf1db 13811 -s ignored\n"));
b3b91714 13812#endif
b00af7c8
JB
13813#ifdef BFD64
13814# if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13815 fprintf (stream, _("\
13816 --32/--64/--x32 generate 32bit/64bit/x32 object\n"));
13817# elif defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O)
751d281c 13818 fprintf (stream, _("\
b00af7c8
JB
13819 --32/--64 generate 32bit/64bit object\n"));
13820# endif
751d281c 13821#endif
b3b91714
AM
13822#ifdef SVR4_COMMENT_CHARS
13823 fprintf (stream, _("\
13824 --divide do not treat `/' as a comment character\n"));
a38cf1db
AM
13825#else
13826 fprintf (stream, _("\
b3b91714 13827 --divide ignored\n"));
4cc782b5 13828#endif
9103f4f4 13829 fprintf (stream, _("\
6305a203 13830 -march=CPU[,+EXTENSION...]\n\
8a2c8fef 13831 generate code for CPU and EXTENSION, CPU is one of:\n"));
1ded5609 13832 show_arch (stream, 0, 1);
8a2c8fef 13833 fprintf (stream, _("\
ae89daec 13834 EXTENSION is combination of (possibly \"no\"-prefixed):\n"));
1ded5609 13835 show_arch (stream, 1, 0);
6305a203 13836 fprintf (stream, _("\
8a2c8fef 13837 -mtune=CPU optimize for CPU, CPU is one of:\n"));
1ded5609 13838 show_arch (stream, 0, 0);
ba104c83 13839 fprintf (stream, _("\
c0f3af97
L
13840 -msse2avx encode SSE instructions with VEX prefix\n"));
13841 fprintf (stream, _("\
c8480b58
L
13842 -muse-unaligned-vector-move\n\
13843 encode aligned vector move as unaligned vector move\n"));
13844 fprintf (stream, _("\
7c5c05ef 13845 -msse-check=[none|error|warning] (default: warning)\n\
daf50ae7
L
13846 check SSE instructions\n"));
13847 fprintf (stream, _("\
7c5c05ef 13848 -moperand-check=[none|error|warning] (default: warning)\n\
7bab8ab5
JB
13849 check operand combinations for validity\n"));
13850 fprintf (stream, _("\
7c5c05ef
L
13851 -mavxscalar=[128|256] (default: 128)\n\
13852 encode scalar AVX instructions with specific vector\n\
539f890d
L
13853 length\n"));
13854 fprintf (stream, _("\
03751133
L
13855 -mvexwig=[0|1] (default: 0)\n\
13856 encode VEX instructions with specific VEX.W value\n\
13857 for VEX.W bit ignored instructions\n"));
13858 fprintf (stream, _("\
7c5c05ef
L
13859 -mevexlig=[128|256|512] (default: 128)\n\
13860 encode scalar EVEX instructions with specific vector\n\
43234a1e
L
13861 length\n"));
13862 fprintf (stream, _("\
7c5c05ef
L
13863 -mevexwig=[0|1] (default: 0)\n\
13864 encode EVEX instructions with specific EVEX.W value\n\
43234a1e
L
13865 for EVEX.W bit ignored instructions\n"));
13866 fprintf (stream, _("\
7c5c05ef 13867 -mevexrcig=[rne|rd|ru|rz] (default: rne)\n\
d3d3c6db
IT
13868 encode EVEX instructions with specific EVEX.RC value\n\
13869 for SAE-only ignored instructions\n"));
13870 fprintf (stream, _("\
7c5c05ef
L
13871 -mmnemonic=[att|intel] "));
13872 if (SYSV386_COMPAT)
13873 fprintf (stream, _("(default: att)\n"));
13874 else
13875 fprintf (stream, _("(default: intel)\n"));
13876 fprintf (stream, _("\
13877 use AT&T/Intel mnemonic\n"));
ba104c83 13878 fprintf (stream, _("\
7c5c05ef
L
13879 -msyntax=[att|intel] (default: att)\n\
13880 use AT&T/Intel syntax\n"));
ba104c83
L
13881 fprintf (stream, _("\
13882 -mindex-reg support pseudo index registers\n"));
13883 fprintf (stream, _("\
13884 -mnaked-reg don't require `%%' prefix for registers\n"));
13885 fprintf (stream, _("\
7e8b059b 13886 -madd-bnd-prefix add BND prefix for all valid branches\n"));
b4a3a7b4 13887#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8dcea932
L
13888 fprintf (stream, _("\
13889 -mshared disable branch optimization for shared code\n"));
b4a3a7b4
L
13890 fprintf (stream, _("\
13891 -mx86-used-note=[no|yes] "));
13892 if (DEFAULT_X86_USED_NOTE)
13893 fprintf (stream, _("(default: yes)\n"));
13894 else
13895 fprintf (stream, _("(default: no)\n"));
13896 fprintf (stream, _("\
13897 generate x86 used ISA and feature properties\n"));
13898#endif
13899#if defined (TE_PE) || defined (TE_PEP)
167ad85b
TG
13900 fprintf (stream, _("\
13901 -mbig-obj generate big object files\n"));
13902#endif
d022bddd 13903 fprintf (stream, _("\
7c5c05ef 13904 -momit-lock-prefix=[no|yes] (default: no)\n\
d022bddd 13905 strip all lock prefixes\n"));
5db04b09 13906 fprintf (stream, _("\
7c5c05ef 13907 -mfence-as-lock-add=[no|yes] (default: no)\n\
e4e00185
AS
13908 encode lfence, mfence and sfence as\n\
13909 lock addl $0x0, (%%{re}sp)\n"));
13910 fprintf (stream, _("\
7c5c05ef
L
13911 -mrelax-relocations=[no|yes] "));
13912 if (DEFAULT_GENERATE_X86_RELAX_RELOCATIONS)
13913 fprintf (stream, _("(default: yes)\n"));
13914 else
13915 fprintf (stream, _("(default: no)\n"));
13916 fprintf (stream, _("\
0cb4071e
L
13917 generate relax relocations\n"));
13918 fprintf (stream, _("\
e379e5f3
L
13919 -malign-branch-boundary=NUM (default: 0)\n\
13920 align branches within NUM byte boundary\n"));
13921 fprintf (stream, _("\
13922 -malign-branch=TYPE[+TYPE...] (default: jcc+fused+jmp)\n\
13923 TYPE is combination of jcc, fused, jmp, call, ret,\n\
13924 indirect\n\
13925 specify types of branches to align\n"));
13926 fprintf (stream, _("\
13927 -malign-branch-prefix-size=NUM (default: 5)\n\
13928 align branches with NUM prefixes per instruction\n"));
13929 fprintf (stream, _("\
76cf450b
L
13930 -mbranches-within-32B-boundaries\n\
13931 align branches within 32 byte boundary\n"));
13932 fprintf (stream, _("\
ae531041
L
13933 -mlfence-after-load=[no|yes] (default: no)\n\
13934 generate lfence after load\n"));
13935 fprintf (stream, _("\
13936 -mlfence-before-indirect-branch=[none|all|register|memory] (default: none)\n\
13937 generate lfence before indirect near branch\n"));
13938 fprintf (stream, _("\
a09f656b 13939 -mlfence-before-ret=[none|or|not|shl|yes] (default: none)\n\
ae531041
L
13940 generate lfence before ret\n"));
13941 fprintf (stream, _("\
7c5c05ef 13942 -mamd64 accept only AMD64 ISA [default]\n"));
5db04b09
L
13943 fprintf (stream, _("\
13944 -mintel64 accept only Intel64 ISA\n"));
252b5132
RH
13945}
13946
3e73aa7c 13947#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
321098a5 13948 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
e57f8c65 13949 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
252b5132
RH
13950
13951/* Pick the target format to use. */
13952
47926f60 13953const char *
e3bb37b5 13954i386_target_format (void)
252b5132 13955{
d34049e8 13956 if (startswith (default_arch, "x86_64"))
351f65ca
L
13957 {
13958 update_code_flag (CODE_64BIT, 1);
13959 if (default_arch[6] == '\0')
7f56bc95 13960 x86_elf_abi = X86_64_ABI;
351f65ca 13961 else
7f56bc95 13962 x86_elf_abi = X86_64_X32_ABI;
351f65ca 13963 }
3e73aa7c 13964 else if (!strcmp (default_arch, "i386"))
78f12dd3 13965 update_code_flag (CODE_32BIT, 1);
5197d474
L
13966 else if (!strcmp (default_arch, "iamcu"))
13967 {
13968 update_code_flag (CODE_32BIT, 1);
13969 if (cpu_arch_isa == PROCESSOR_UNKNOWN)
13970 {
13971 static const i386_cpu_flags iamcu_flags = CPU_IAMCU_FLAGS;
13972 cpu_arch_name = "iamcu";
d92c7521 13973 free (cpu_sub_arch_name);
5197d474
L
13974 cpu_sub_arch_name = NULL;
13975 cpu_arch_flags = iamcu_flags;
13976 cpu_arch_isa = PROCESSOR_IAMCU;
13977 cpu_arch_isa_flags = iamcu_flags;
13978 if (!cpu_arch_tune_set)
13979 {
13980 cpu_arch_tune = cpu_arch_isa;
13981 cpu_arch_tune_flags = cpu_arch_isa_flags;
13982 }
13983 }
8d471ec1 13984 else if (cpu_arch_isa != PROCESSOR_IAMCU)
5197d474
L
13985 as_fatal (_("Intel MCU doesn't support `%s' architecture"),
13986 cpu_arch_name);
13987 }
3e73aa7c 13988 else
2b5d6a91 13989 as_fatal (_("unknown architecture"));
89507696
JB
13990
13991 if (cpu_flags_all_zero (&cpu_arch_isa_flags))
ae89daec 13992 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
89507696 13993 if (cpu_flags_all_zero (&cpu_arch_tune_flags))
ae89daec 13994 cpu_arch_tune_flags = cpu_arch[flag_code == CODE_64BIT].enable;
89507696 13995
252b5132
RH
13996 switch (OUTPUT_FLAVOR)
13997 {
9384f2ff 13998#if defined (OBJ_MAYBE_AOUT) || defined (OBJ_AOUT)
4c63da97 13999 case bfd_target_aout_flavour:
47926f60 14000 return AOUT_TARGET_FORMAT;
4c63da97 14001#endif
9384f2ff
AM
14002#if defined (OBJ_MAYBE_COFF) || defined (OBJ_COFF)
14003# if defined (TE_PE) || defined (TE_PEP)
14004 case bfd_target_coff_flavour:
167ad85b 14005 if (flag_code == CODE_64BIT)
eb19308f
JB
14006 {
14007 object_64bit = 1;
14008 return use_big_obj ? "pe-bigobj-x86-64" : "pe-x86-64";
14009 }
14010 return use_big_obj ? "pe-bigobj-i386" : "pe-i386";
9384f2ff 14011# elif defined (TE_GO32)
0561d57c
JK
14012 case bfd_target_coff_flavour:
14013 return "coff-go32";
9384f2ff 14014# else
252b5132
RH
14015 case bfd_target_coff_flavour:
14016 return "coff-i386";
9384f2ff 14017# endif
4c63da97 14018#endif
3e73aa7c 14019#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
252b5132 14020 case bfd_target_elf_flavour:
3e73aa7c 14021 {
351f65ca
L
14022 const char *format;
14023
14024 switch (x86_elf_abi)
4fa24527 14025 {
351f65ca
L
14026 default:
14027 format = ELF_TARGET_FORMAT;
e379e5f3
L
14028#ifndef TE_SOLARIS
14029 tls_get_addr = "___tls_get_addr";
14030#endif
351f65ca 14031 break;
7f56bc95 14032 case X86_64_ABI:
351f65ca 14033 use_rela_relocations = 1;
4fa24527 14034 object_64bit = 1;
e379e5f3
L
14035#ifndef TE_SOLARIS
14036 tls_get_addr = "__tls_get_addr";
14037#endif
351f65ca
L
14038 format = ELF_TARGET_FORMAT64;
14039 break;
7f56bc95 14040 case X86_64_X32_ABI:
4fa24527 14041 use_rela_relocations = 1;
351f65ca 14042 object_64bit = 1;
e379e5f3
L
14043#ifndef TE_SOLARIS
14044 tls_get_addr = "__tls_get_addr";
14045#endif
862be3fb 14046 disallow_64bit_reloc = 1;
351f65ca
L
14047 format = ELF_TARGET_FORMAT32;
14048 break;
4fa24527 14049 }
c085ab00 14050 if (cpu_arch_isa == PROCESSOR_IAMCU)
81486035
L
14051 {
14052 if (x86_elf_abi != I386_ABI)
14053 as_fatal (_("Intel MCU is 32bit only"));
14054 return ELF_TARGET_IAMCU_FORMAT;
14055 }
8a9036a4 14056 else
351f65ca 14057 return format;
3e73aa7c 14058 }
e57f8c65
TG
14059#endif
14060#if defined (OBJ_MACH_O)
14061 case bfd_target_mach_o_flavour:
d382c579
TG
14062 if (flag_code == CODE_64BIT)
14063 {
14064 use_rela_relocations = 1;
14065 object_64bit = 1;
14066 return "mach-o-x86-64";
14067 }
14068 else
14069 return "mach-o-i386";
4c63da97 14070#endif
252b5132
RH
14071 default:
14072 abort ();
14073 return NULL;
14074 }
14075}
14076
47926f60 14077#endif /* OBJ_MAYBE_ more than one */
252b5132 14078\f
252b5132 14079symbolS *
7016a5d5 14080md_undefined_symbol (char *name)
252b5132 14081{
18dc2407
ILT
14082 if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
14083 && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
14084 && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
14085 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
24eab124
AM
14086 {
14087 if (!GOT_symbol)
14088 {
14089 if (symbol_find (name))
14090 as_bad (_("GOT already in symbol table"));
14091 GOT_symbol = symbol_new (name, undefined_section,
e01e1cee 14092 &zero_address_frag, 0);
24eab124
AM
14093 };
14094 return GOT_symbol;
14095 }
252b5132
RH
14096 return 0;
14097}
14098
14099/* Round up a section size to the appropriate boundary. */
47926f60 14100
252b5132 14101valueT
7016a5d5 14102md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
252b5132 14103{
4c63da97
AM
14104#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
14105 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
14106 {
14107 /* For a.out, force the section size to be aligned. If we don't do
14108 this, BFD will align it for us, but it will not write out the
14109 final bytes of the section. This may be a bug in BFD, but it is
14110 easier to fix it here since that is how the other a.out targets
14111 work. */
14112 int align;
14113
fd361982 14114 align = bfd_section_alignment (segment);
8d3842cd 14115 size = ((size + (1 << align) - 1) & (-((valueT) 1 << align)));
4c63da97 14116 }
252b5132
RH
14117#endif
14118
14119 return size;
14120}
14121
14122/* On the i386, PC-relative offsets are relative to the start of the
14123 next instruction. That is, the address of the offset, plus its
14124 size, since the offset is always the last part of the insn. */
14125
14126long
e3bb37b5 14127md_pcrel_from (fixS *fixP)
252b5132
RH
14128{
14129 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
14130}
14131
14132#ifndef I386COFF
14133
14134static void
e3bb37b5 14135s_bss (int ignore ATTRIBUTE_UNUSED)
252b5132 14136{
29b0f896 14137 int temp;
252b5132 14138
8a75718c
JB
14139#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14140 if (IS_ELF)
14141 obj_elf_section_change_hook ();
14142#endif
252b5132
RH
14143 temp = get_absolute_expression ();
14144 subseg_set (bss_section, (subsegT) temp);
14145 demand_empty_rest_of_line ();
14146}
14147
14148#endif
14149
e379e5f3
L
14150/* Remember constant directive. */
14151
14152void
14153i386_cons_align (int ignore ATTRIBUTE_UNUSED)
14154{
14155 if (last_insn.kind != last_insn_directive
14156 && (bfd_section_flags (now_seg) & SEC_CODE))
14157 {
14158 last_insn.seg = now_seg;
14159 last_insn.kind = last_insn_directive;
14160 last_insn.name = "constant directive";
14161 last_insn.file = as_where (&last_insn.line);
ae531041
L
14162 if (lfence_before_ret != lfence_before_ret_none)
14163 {
14164 if (lfence_before_indirect_branch != lfence_branch_none)
14165 as_warn (_("constant directive skips -mlfence-before-ret "
14166 "and -mlfence-before-indirect-branch"));
14167 else
14168 as_warn (_("constant directive skips -mlfence-before-ret"));
14169 }
14170 else if (lfence_before_indirect_branch != lfence_branch_none)
14171 as_warn (_("constant directive skips -mlfence-before-indirect-branch"));
e379e5f3
L
14172 }
14173}
14174
3abbafc2 14175int
e3bb37b5 14176i386_validate_fix (fixS *fixp)
252b5132 14177{
e52a16f2
JB
14178 if (fixp->fx_addsy && S_GET_SEGMENT(fixp->fx_addsy) == reg_section)
14179 {
14180 reloc_howto_type *howto;
14181
14182 howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
14183 as_bad_where (fixp->fx_file, fixp->fx_line,
14184 _("invalid %s relocation against register"),
14185 howto ? howto->name : "<unknown>");
14186 return 0;
14187 }
14188
3abbafc2
JB
14189#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14190 if (fixp->fx_r_type == BFD_RELOC_SIZE32
14191 || fixp->fx_r_type == BFD_RELOC_SIZE64)
14192 return IS_ELF && fixp->fx_addsy
14193 && (!S_IS_DEFINED (fixp->fx_addsy)
14194 || S_IS_EXTERNAL (fixp->fx_addsy));
14195#endif
14196
02a86693 14197 if (fixp->fx_subsy)
252b5132 14198 {
02a86693 14199 if (fixp->fx_subsy == GOT_symbol)
23df1078 14200 {
02a86693
L
14201 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
14202 {
14203 if (!object_64bit)
14204 abort ();
14205#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14206 if (fixp->fx_tcbit2)
56ceb5b5
L
14207 fixp->fx_r_type = (fixp->fx_tcbit
14208 ? BFD_RELOC_X86_64_REX_GOTPCRELX
14209 : BFD_RELOC_X86_64_GOTPCRELX);
02a86693
L
14210 else
14211#endif
14212 fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
14213 }
d6ab8113 14214 else
02a86693
L
14215 {
14216 if (!object_64bit)
14217 fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
14218 else
14219 fixp->fx_r_type = BFD_RELOC_X86_64_GOTOFF64;
14220 }
14221 fixp->fx_subsy = 0;
23df1078 14222 }
252b5132 14223 }
02a86693 14224#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2585b7a5 14225 else
02a86693 14226 {
2585b7a5
L
14227 /* NB: Commit 292676c1 resolved PLT32 reloc aganst local symbol
14228 to section. Since PLT32 relocation must be against symbols,
14229 turn such PLT32 relocation into PC32 relocation. */
14230 if (fixp->fx_addsy
14231 && (fixp->fx_r_type == BFD_RELOC_386_PLT32
14232 || fixp->fx_r_type == BFD_RELOC_X86_64_PLT32)
14233 && symbol_section_p (fixp->fx_addsy))
14234 fixp->fx_r_type = BFD_RELOC_32_PCREL;
14235 if (!object_64bit)
14236 {
14237 if (fixp->fx_r_type == BFD_RELOC_386_GOT32
14238 && fixp->fx_tcbit2)
14239 fixp->fx_r_type = BFD_RELOC_386_GOT32X;
14240 }
02a86693
L
14241 }
14242#endif
3abbafc2
JB
14243
14244 return 1;
252b5132
RH
14245}
14246
252b5132 14247arelent *
7016a5d5 14248tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
14249{
14250 arelent *rel;
14251 bfd_reloc_code_real_type code;
14252
14253 switch (fixp->fx_r_type)
14254 {
8ce3d284 14255#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3abbafc2
JB
14256 symbolS *sym;
14257
8fd4256d
L
14258 case BFD_RELOC_SIZE32:
14259 case BFD_RELOC_SIZE64:
3abbafc2
JB
14260 if (fixp->fx_addsy
14261 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))
14262 && (!fixp->fx_subsy
14263 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))))
14264 sym = fixp->fx_addsy;
14265 else if (fixp->fx_subsy
14266 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))
14267 && (!fixp->fx_addsy
14268 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))))
14269 sym = fixp->fx_subsy;
14270 else
14271 sym = NULL;
14272 if (IS_ELF && sym && S_IS_DEFINED (sym) && !S_IS_EXTERNAL (sym))
8fd4256d
L
14273 {
14274 /* Resolve size relocation against local symbol to size of
14275 the symbol plus addend. */
3abbafc2 14276 valueT value = S_GET_SIZE (sym);
44f87162 14277
3abbafc2
JB
14278 if (symbol_get_bfdsym (sym)->flags & BSF_SECTION_SYM)
14279 value = bfd_section_size (S_GET_SEGMENT (sym));
14280 if (sym == fixp->fx_subsy)
14281 {
14282 value = -value;
14283 if (fixp->fx_addsy)
14284 value += S_GET_VALUE (fixp->fx_addsy);
14285 }
14286 else if (fixp->fx_subsy)
14287 value -= S_GET_VALUE (fixp->fx_subsy);
44f87162 14288 value += fixp->fx_offset;
8fd4256d 14289 if (fixp->fx_r_type == BFD_RELOC_SIZE32
d965814f 14290 && object_64bit
8fd4256d
L
14291 && !fits_in_unsigned_long (value))
14292 as_bad_where (fixp->fx_file, fixp->fx_line,
14293 _("symbol size computation overflow"));
14294 fixp->fx_addsy = NULL;
14295 fixp->fx_subsy = NULL;
14296 md_apply_fix (fixp, (valueT *) &value, NULL);
14297 return NULL;
14298 }
3abbafc2
JB
14299 if (!fixp->fx_addsy || fixp->fx_subsy)
14300 {
14301 as_bad_where (fixp->fx_file, fixp->fx_line,
14302 "unsupported expression involving @size");
14303 return NULL;
14304 }
8ce3d284 14305#endif
1a0670f3 14306 /* Fall through. */
8fd4256d 14307
3e73aa7c
JH
14308 case BFD_RELOC_X86_64_PLT32:
14309 case BFD_RELOC_X86_64_GOT32:
14310 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
14311 case BFD_RELOC_X86_64_GOTPCRELX:
14312 case BFD_RELOC_X86_64_REX_GOTPCRELX:
252b5132
RH
14313 case BFD_RELOC_386_PLT32:
14314 case BFD_RELOC_386_GOT32:
02a86693 14315 case BFD_RELOC_386_GOT32X:
252b5132
RH
14316 case BFD_RELOC_386_GOTOFF:
14317 case BFD_RELOC_386_GOTPC:
13ae64f3
JJ
14318 case BFD_RELOC_386_TLS_GD:
14319 case BFD_RELOC_386_TLS_LDM:
14320 case BFD_RELOC_386_TLS_LDO_32:
14321 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
14322 case BFD_RELOC_386_TLS_IE:
14323 case BFD_RELOC_386_TLS_GOTIE:
13ae64f3
JJ
14324 case BFD_RELOC_386_TLS_LE_32:
14325 case BFD_RELOC_386_TLS_LE:
67a4f2b7
AO
14326 case BFD_RELOC_386_TLS_GOTDESC:
14327 case BFD_RELOC_386_TLS_DESC_CALL:
bffbf940
JJ
14328 case BFD_RELOC_X86_64_TLSGD:
14329 case BFD_RELOC_X86_64_TLSLD:
14330 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 14331 case BFD_RELOC_X86_64_DTPOFF64:
bffbf940
JJ
14332 case BFD_RELOC_X86_64_GOTTPOFF:
14333 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113
JB
14334 case BFD_RELOC_X86_64_TPOFF64:
14335 case BFD_RELOC_X86_64_GOTOFF64:
14336 case BFD_RELOC_X86_64_GOTPC32:
7b81dfbb
AJ
14337 case BFD_RELOC_X86_64_GOT64:
14338 case BFD_RELOC_X86_64_GOTPCREL64:
14339 case BFD_RELOC_X86_64_GOTPC64:
14340 case BFD_RELOC_X86_64_GOTPLT64:
14341 case BFD_RELOC_X86_64_PLTOFF64:
67a4f2b7
AO
14342 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14343 case BFD_RELOC_X86_64_TLSDESC_CALL:
252b5132
RH
14344 case BFD_RELOC_RVA:
14345 case BFD_RELOC_VTABLE_ENTRY:
14346 case BFD_RELOC_VTABLE_INHERIT:
6482c264
NC
14347#ifdef TE_PE
14348 case BFD_RELOC_32_SECREL:
145667f8 14349 case BFD_RELOC_16_SECIDX:
6482c264 14350#endif
252b5132
RH
14351 code = fixp->fx_r_type;
14352 break;
dbbaec26
L
14353 case BFD_RELOC_X86_64_32S:
14354 if (!fixp->fx_pcrel)
14355 {
14356 /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32. */
14357 code = fixp->fx_r_type;
14358 break;
14359 }
1a0670f3 14360 /* Fall through. */
252b5132 14361 default:
93382f6d 14362 if (fixp->fx_pcrel)
252b5132 14363 {
93382f6d
AM
14364 switch (fixp->fx_size)
14365 {
14366 default:
b091f402
AM
14367 as_bad_where (fixp->fx_file, fixp->fx_line,
14368 _("can not do %d byte pc-relative relocation"),
14369 fixp->fx_size);
93382f6d
AM
14370 code = BFD_RELOC_32_PCREL;
14371 break;
14372 case 1: code = BFD_RELOC_8_PCREL; break;
14373 case 2: code = BFD_RELOC_16_PCREL; break;
d258b828 14374 case 4: code = BFD_RELOC_32_PCREL; break;
d6ab8113
JB
14375#ifdef BFD64
14376 case 8: code = BFD_RELOC_64_PCREL; break;
14377#endif
93382f6d
AM
14378 }
14379 }
14380 else
14381 {
14382 switch (fixp->fx_size)
14383 {
14384 default:
b091f402
AM
14385 as_bad_where (fixp->fx_file, fixp->fx_line,
14386 _("can not do %d byte relocation"),
14387 fixp->fx_size);
93382f6d
AM
14388 code = BFD_RELOC_32;
14389 break;
14390 case 1: code = BFD_RELOC_8; break;
14391 case 2: code = BFD_RELOC_16; break;
14392 case 4: code = BFD_RELOC_32; break;
937149dd 14393#ifdef BFD64
3e73aa7c 14394 case 8: code = BFD_RELOC_64; break;
937149dd 14395#endif
93382f6d 14396 }
252b5132
RH
14397 }
14398 break;
14399 }
252b5132 14400
d182319b
JB
14401 if ((code == BFD_RELOC_32
14402 || code == BFD_RELOC_32_PCREL
14403 || code == BFD_RELOC_X86_64_32S)
252b5132
RH
14404 && GOT_symbol
14405 && fixp->fx_addsy == GOT_symbol)
3e73aa7c 14406 {
4fa24527 14407 if (!object_64bit)
d6ab8113
JB
14408 code = BFD_RELOC_386_GOTPC;
14409 else
14410 code = BFD_RELOC_X86_64_GOTPC32;
3e73aa7c 14411 }
7b81dfbb
AJ
14412 if ((code == BFD_RELOC_64 || code == BFD_RELOC_64_PCREL)
14413 && GOT_symbol
14414 && fixp->fx_addsy == GOT_symbol)
14415 {
14416 code = BFD_RELOC_X86_64_GOTPC64;
14417 }
252b5132 14418
add39d23
TS
14419 rel = XNEW (arelent);
14420 rel->sym_ptr_ptr = XNEW (asymbol *);
49309057 14421 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
14422
14423 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
c87db184 14424
3e73aa7c
JH
14425 if (!use_rela_relocations)
14426 {
14427 /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
14428 vtable entry to be used in the relocation's section offset. */
14429 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
14430 rel->address = fixp->fx_offset;
fbeb56a4
DK
14431#if defined (OBJ_COFF) && defined (TE_PE)
14432 else if (fixp->fx_addsy && S_IS_WEAK (fixp->fx_addsy))
14433 rel->addend = fixp->fx_addnumber - (S_GET_VALUE (fixp->fx_addsy) * 2);
14434 else
14435#endif
c6682705 14436 rel->addend = 0;
3e73aa7c
JH
14437 }
14438 /* Use the rela in 64bit mode. */
252b5132 14439 else
3e73aa7c 14440 {
862be3fb
L
14441 if (disallow_64bit_reloc)
14442 switch (code)
14443 {
862be3fb
L
14444 case BFD_RELOC_X86_64_DTPOFF64:
14445 case BFD_RELOC_X86_64_TPOFF64:
14446 case BFD_RELOC_64_PCREL:
14447 case BFD_RELOC_X86_64_GOTOFF64:
14448 case BFD_RELOC_X86_64_GOT64:
14449 case BFD_RELOC_X86_64_GOTPCREL64:
14450 case BFD_RELOC_X86_64_GOTPC64:
14451 case BFD_RELOC_X86_64_GOTPLT64:
14452 case BFD_RELOC_X86_64_PLTOFF64:
14453 as_bad_where (fixp->fx_file, fixp->fx_line,
14454 _("cannot represent relocation type %s in x32 mode"),
14455 bfd_get_reloc_code_name (code));
14456 break;
14457 default:
14458 break;
14459 }
14460
062cd5e7
AS
14461 if (!fixp->fx_pcrel)
14462 rel->addend = fixp->fx_offset;
14463 else
14464 switch (code)
14465 {
14466 case BFD_RELOC_X86_64_PLT32:
14467 case BFD_RELOC_X86_64_GOT32:
14468 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
14469 case BFD_RELOC_X86_64_GOTPCRELX:
14470 case BFD_RELOC_X86_64_REX_GOTPCRELX:
bffbf940
JJ
14471 case BFD_RELOC_X86_64_TLSGD:
14472 case BFD_RELOC_X86_64_TLSLD:
14473 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7
AO
14474 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14475 case BFD_RELOC_X86_64_TLSDESC_CALL:
062cd5e7
AS
14476 rel->addend = fixp->fx_offset - fixp->fx_size;
14477 break;
14478 default:
14479 rel->addend = (section->vma
14480 - fixp->fx_size
14481 + fixp->fx_addnumber
14482 + md_pcrel_from (fixp));
14483 break;
14484 }
3e73aa7c
JH
14485 }
14486
252b5132
RH
14487 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
14488 if (rel->howto == NULL)
14489 {
14490 as_bad_where (fixp->fx_file, fixp->fx_line,
d0b47220 14491 _("cannot represent relocation type %s"),
252b5132
RH
14492 bfd_get_reloc_code_name (code));
14493 /* Set howto to a garbage value so that we can keep going. */
14494 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
9c2799c2 14495 gas_assert (rel->howto != NULL);
252b5132
RH
14496 }
14497
14498 return rel;
14499}
14500
ee86248c 14501#include "tc-i386-intel.c"
54cfded0 14502
a60de03c
JB
14503void
14504tc_x86_parse_to_dw2regnum (expressionS *exp)
54cfded0 14505{
a60de03c
JB
14506 int saved_naked_reg;
14507 char saved_register_dot;
54cfded0 14508
a60de03c
JB
14509 saved_naked_reg = allow_naked_reg;
14510 allow_naked_reg = 1;
14511 saved_register_dot = register_chars['.'];
14512 register_chars['.'] = '.';
14513 allow_pseudo_reg = 1;
14514 expression_and_evaluate (exp);
14515 allow_pseudo_reg = 0;
14516 register_chars['.'] = saved_register_dot;
14517 allow_naked_reg = saved_naked_reg;
14518
e96d56a1 14519 if (exp->X_op == O_register && exp->X_add_number >= 0)
54cfded0 14520 {
a60de03c
JB
14521 if ((addressT) exp->X_add_number < i386_regtab_size)
14522 {
14523 exp->X_op = O_constant;
14524 exp->X_add_number = i386_regtab[exp->X_add_number]
14525 .dw2_regnum[flag_code >> 1];
14526 }
14527 else
14528 exp->X_op = O_illegal;
54cfded0 14529 }
54cfded0
AM
14530}
14531
14532void
14533tc_x86_frame_initial_instructions (void)
14534{
a60de03c
JB
14535 static unsigned int sp_regno[2];
14536
14537 if (!sp_regno[flag_code >> 1])
14538 {
14539 char *saved_input = input_line_pointer;
14540 char sp[][4] = {"esp", "rsp"};
14541 expressionS exp;
a4447b93 14542
a60de03c
JB
14543 input_line_pointer = sp[flag_code >> 1];
14544 tc_x86_parse_to_dw2regnum (&exp);
9c2799c2 14545 gas_assert (exp.X_op == O_constant);
a60de03c
JB
14546 sp_regno[flag_code >> 1] = exp.X_add_number;
14547 input_line_pointer = saved_input;
14548 }
a4447b93 14549
61ff971f
L
14550 cfi_add_CFA_def_cfa (sp_regno[flag_code >> 1], -x86_cie_data_alignment);
14551 cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
54cfded0 14552}
d2b2c203 14553
d7921315
L
14554int
14555x86_dwarf2_addr_size (void)
14556{
14557#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
14558 if (x86_elf_abi == X86_64_X32_ABI)
14559 return 4;
14560#endif
14561 return bfd_arch_bits_per_address (stdoutput) / 8;
14562}
14563
d2b2c203
DJ
14564int
14565i386_elf_section_type (const char *str, size_t len)
14566{
14567 if (flag_code == CODE_64BIT
14568 && len == sizeof ("unwind") - 1
d34049e8 14569 && startswith (str, "unwind"))
d2b2c203
DJ
14570 return SHT_X86_64_UNWIND;
14571
14572 return -1;
14573}
bb41ade5 14574
ad5fec3b
EB
14575#ifdef TE_SOLARIS
14576void
14577i386_solaris_fix_up_eh_frame (segT sec)
14578{
14579 if (flag_code == CODE_64BIT)
14580 elf_section_type (sec) = SHT_X86_64_UNWIND;
14581}
14582#endif
14583
bb41ade5
AM
14584#ifdef TE_PE
14585void
14586tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
14587{
91d6fa6a 14588 expressionS exp;
bb41ade5 14589
91d6fa6a
NC
14590 exp.X_op = O_secrel;
14591 exp.X_add_symbol = symbol;
14592 exp.X_add_number = 0;
14593 emit_expr (&exp, size);
bb41ade5
AM
14594}
14595#endif
3b22753a
L
14596
14597#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14598/* For ELF on x86-64, add support for SHF_X86_64_LARGE. */
14599
01e1a5bc 14600bfd_vma
6d4af3c2 14601x86_64_section_letter (int letter, const char **ptr_msg)
3b22753a
L
14602{
14603 if (flag_code == CODE_64BIT)
14604 {
14605 if (letter == 'l')
14606 return SHF_X86_64_LARGE;
14607
8f3bae45 14608 *ptr_msg = _("bad .section directive: want a,l,w,x,M,S,G,T in string");
64e74474 14609 }
3b22753a 14610 else
8f3bae45 14611 *ptr_msg = _("bad .section directive: want a,w,x,M,S,G,T in string");
3b22753a
L
14612 return -1;
14613}
14614
01e1a5bc 14615bfd_vma
3b22753a
L
14616x86_64_section_word (char *str, size_t len)
14617{
08dedd66 14618 if (len == 5 && flag_code == CODE_64BIT && startswith (str, "large"))
3b22753a
L
14619 return SHF_X86_64_LARGE;
14620
14621 return -1;
14622}
14623
14624static void
14625handle_large_common (int small ATTRIBUTE_UNUSED)
14626{
14627 if (flag_code != CODE_64BIT)
14628 {
14629 s_comm_internal (0, elf_common_parse);
14630 as_warn (_(".largecomm supported only in 64bit mode, producing .comm"));
14631 }
14632 else
14633 {
14634 static segT lbss_section;
14635 asection *saved_com_section_ptr = elf_com_section_ptr;
14636 asection *saved_bss_section = bss_section;
14637
14638 if (lbss_section == NULL)
14639 {
14640 flagword applicable;
14641 segT seg = now_seg;
14642 subsegT subseg = now_subseg;
14643
14644 /* The .lbss section is for local .largecomm symbols. */
14645 lbss_section = subseg_new (".lbss", 0);
14646 applicable = bfd_applicable_section_flags (stdoutput);
fd361982 14647 bfd_set_section_flags (lbss_section, applicable & SEC_ALLOC);
3b22753a
L
14648 seg_info (lbss_section)->bss = 1;
14649
14650 subseg_set (seg, subseg);
14651 }
14652
14653 elf_com_section_ptr = &_bfd_elf_large_com_section;
14654 bss_section = lbss_section;
14655
14656 s_comm_internal (0, elf_common_parse);
14657
14658 elf_com_section_ptr = saved_com_section_ptr;
14659 bss_section = saved_bss_section;
14660 }
14661}
14662#endif /* OBJ_ELF || OBJ_MAYBE_ELF */