]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/config/tc-i386.c
Re: Get rid of fprintf_vma and sprintf_vma
[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 {
86e026a4 229 operand_size_mismatch,
a65babc9
L
230 operand_type_mismatch,
231 register_type_mismatch,
232 number_of_operands_mismatch,
233 invalid_instruction_suffix,
234 bad_imm4,
a65babc9
L
235 unsupported_with_intel_mnemonic,
236 unsupported_syntax,
6c30d220 237 unsupported,
260cd341 238 invalid_sib_address,
6c30d220 239 invalid_vsib_address,
7bab8ab5 240 invalid_vector_register_set,
260cd341 241 invalid_tmm_register_set,
0cc78721 242 invalid_dest_and_src_register_set,
43234a1e
L
243 unsupported_vector_index_register,
244 unsupported_broadcast,
43234a1e
L
245 broadcast_needed,
246 unsupported_masking,
247 mask_not_on_destination,
248 no_default_mask,
249 unsupported_rc_sae,
43234a1e 250 invalid_register_operand,
a65babc9
L
251 };
252
252b5132
RH
253struct _i386_insn
254 {
47926f60 255 /* TM holds the template for the insn were currently assembling. */
d3ce72d0 256 insn_template tm;
252b5132 257
7d5e4556
L
258 /* SUFFIX holds the instruction size suffix for byte, word, dword
259 or qword, if given. */
252b5132
RH
260 char suffix;
261
9a182d04
JB
262 /* OPCODE_LENGTH holds the number of base opcode bytes. */
263 unsigned char opcode_length;
264
47926f60 265 /* OPERANDS gives the number of given operands. */
252b5132
RH
266 unsigned int operands;
267
268 /* REG_OPERANDS, DISP_OPERANDS, MEM_OPERANDS, IMM_OPERANDS give the number
269 of given register, displacement, memory operands and immediate
47926f60 270 operands. */
252b5132
RH
271 unsigned int reg_operands, disp_operands, mem_operands, imm_operands;
272
273 /* TYPES [i] is the type (see above #defines) which tells us how to
520dc8e8 274 use OP[i] for the corresponding operand. */
40fb9820 275 i386_operand_type types[MAX_OPERANDS];
252b5132 276
520dc8e8
AM
277 /* Displacement expression, immediate expression, or register for each
278 operand. */
279 union i386_op op[MAX_OPERANDS];
252b5132 280
3e73aa7c
JH
281 /* Flags for operands. */
282 unsigned int flags[MAX_OPERANDS];
283#define Operand_PCrel 1
c48dadc9 284#define Operand_Mem 2
3e73aa7c 285
252b5132 286 /* Relocation type for operand */
f86103b7 287 enum bfd_reloc_code_real reloc[MAX_OPERANDS];
252b5132 288
252b5132
RH
289 /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
290 the base index byte below. */
291 const reg_entry *base_reg;
292 const reg_entry *index_reg;
293 unsigned int log2_scale_factor;
294
295 /* SEG gives the seg_entries of this insn. They are zero unless
47926f60 296 explicit segment overrides are given. */
5e042380 297 const reg_entry *seg[2];
252b5132 298
8325cc63
JB
299 /* Copied first memory operand string, for re-checking. */
300 char *memop1_string;
301
252b5132
RH
302 /* PREFIX holds all the given prefix opcodes (usually null).
303 PREFIXES is the number of prefix opcodes. */
304 unsigned int prefixes;
305 unsigned char prefix[MAX_PREFIXES];
306
50128d0c 307 /* Register is in low 3 bits of opcode. */
5b7c81bd 308 bool short_form;
50128d0c 309
6f2f06be 310 /* The operand to a branch insn indicates an absolute branch. */
5b7c81bd 311 bool jumpabsolute;
6f2f06be 312
921eafea
L
313 /* Extended states. */
314 enum
315 {
316 /* Use MMX state. */
317 xstate_mmx = 1 << 0,
318 /* Use XMM state. */
319 xstate_xmm = 1 << 1,
320 /* Use YMM state. */
321 xstate_ymm = 1 << 2 | xstate_xmm,
322 /* Use ZMM state. */
323 xstate_zmm = 1 << 3 | xstate_ymm,
324 /* Use TMM state. */
32930e4e
L
325 xstate_tmm = 1 << 4,
326 /* Use MASK state. */
327 xstate_mask = 1 << 5
921eafea 328 } xstate;
260cd341 329
e379e5f3 330 /* Has GOTPC or TLS relocation. */
5b7c81bd 331 bool has_gotpc_tls_reloc;
e379e5f3 332
252b5132 333 /* RM and SIB are the modrm byte and the sib byte where the
c1e679ec 334 addressing modes of this insn are encoded. */
252b5132 335 modrm_byte rm;
3e73aa7c 336 rex_byte rex;
43234a1e 337 rex_byte vrex;
252b5132 338 sib_byte sib;
c0f3af97 339 vex_prefix vex;
b6169b20 340
6225c532
JB
341 /* Masking attributes.
342
343 The struct describes masking, applied to OPERAND in the instruction.
344 REG is a pointer to the corresponding mask register. ZEROING tells
345 whether merging or zeroing mask is used. */
346 struct Mask_Operation
347 {
348 const reg_entry *reg;
349 unsigned int zeroing;
350 /* The operand where this operation is associated. */
351 unsigned int operand;
352 } mask;
43234a1e
L
353
354 /* Rounding control and SAE attributes. */
ca5312a2
JB
355 struct RC_Operation
356 {
357 enum rc_type
358 {
359 rc_none = -1,
360 rne,
361 rd,
362 ru,
363 rz,
364 saeonly
365 } type;
7063667e
JB
366 /* In Intel syntax the operand modifier form is supposed to be used, but
367 we continue to accept the immediate forms as well. */
368 bool modifier;
ca5312a2 369 } rounding;
43234a1e 370
5273a3cd
JB
371 /* Broadcasting attributes.
372
373 The struct describes broadcasting, applied to OPERAND. TYPE is
374 expresses the broadcast factor. */
375 struct Broadcast_Operation
376 {
0cc78721 377 /* Type of broadcast: {1to2}, {1to4}, {1to8}, {1to16} or {1to32}. */
5273a3cd
JB
378 unsigned int type;
379
380 /* Index of broadcasted operand. */
381 unsigned int operand;
382
383 /* Number of bytes to broadcast. */
384 unsigned int bytes;
385 } broadcast;
43234a1e
L
386
387 /* Compressed disp8*N attribute. */
388 unsigned int memshift;
389
86fa6981
L
390 /* Prefer load or store in encoding. */
391 enum
392 {
393 dir_encoding_default = 0,
394 dir_encoding_load,
64c49ab3
JB
395 dir_encoding_store,
396 dir_encoding_swap
86fa6981 397 } dir_encoding;
891edac4 398
41eb8e88 399 /* Prefer 8bit, 16bit, 32bit displacement in encoding. */
a501d77e
L
400 enum
401 {
402 disp_encoding_default = 0,
403 disp_encoding_8bit,
41eb8e88 404 disp_encoding_16bit,
a501d77e
L
405 disp_encoding_32bit
406 } disp_encoding;
f8a5c266 407
6b6b6807 408 /* Prefer the REX byte in encoding. */
5b7c81bd 409 bool rex_encoding;
6b6b6807 410
b6f8c7c4 411 /* Disable instruction size optimization. */
5b7c81bd 412 bool no_optimize;
b6f8c7c4 413
86fa6981
L
414 /* How to encode vector instructions. */
415 enum
416 {
417 vex_encoding_default = 0,
42e04b36 418 vex_encoding_vex,
86fa6981 419 vex_encoding_vex3,
da4977e0
JB
420 vex_encoding_evex,
421 vex_encoding_error
86fa6981
L
422 } vec_encoding;
423
d5de92cf
L
424 /* REP prefix. */
425 const char *rep_prefix;
426
165de32a
L
427 /* HLE prefix. */
428 const char *hle_prefix;
42164a71 429
7e8b059b
L
430 /* Have BND prefix. */
431 const char *bnd_prefix;
432
04ef582a
L
433 /* Have NOTRACK prefix. */
434 const char *notrack_prefix;
435
891edac4 436 /* Error message. */
a65babc9 437 enum i386_error error;
252b5132
RH
438 };
439
440typedef struct _i386_insn i386_insn;
441
43234a1e
L
442/* Link RC type with corresponding string, that'll be looked for in
443 asm. */
444struct RC_name
445{
446 enum rc_type type;
447 const char *name;
448 unsigned int len;
449};
450
451static const struct RC_name RC_NamesTable[] =
452{
453 { rne, STRING_COMMA_LEN ("rn-sae") },
454 { rd, STRING_COMMA_LEN ("rd-sae") },
455 { ru, STRING_COMMA_LEN ("ru-sae") },
456 { rz, STRING_COMMA_LEN ("rz-sae") },
457 { saeonly, STRING_COMMA_LEN ("sae") },
458};
459
252b5132
RH
460/* List of chars besides those in app.c:symbol_chars that can start an
461 operand. Used to prevent the scrubber eating vital white-space. */
86fa6981 462const char extra_symbol_chars[] = "*%-([{}"
252b5132 463#ifdef LEX_AT
32137342
NC
464 "@"
465#endif
466#ifdef LEX_QM
467 "?"
252b5132 468#endif
32137342 469 ;
252b5132 470
b3983e5f
JB
471#if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
472 && !defined (TE_GNU) \
473 && !defined (TE_LINUX) \
d85e70a3 474 && !defined (TE_Haiku) \
b3983e5f
JB
475 && !defined (TE_FreeBSD) \
476 && !defined (TE_DragonFly) \
477 && !defined (TE_NetBSD))
252b5132 478/* This array holds the chars that always start a comment. If the
b3b91714
AM
479 pre-processor is disabled, these aren't very useful. The option
480 --divide will remove '/' from this list. */
481const char *i386_comment_chars = "#/";
482#define SVR4_COMMENT_CHARS 1
252b5132 483#define PREFIX_SEPARATOR '\\'
252b5132 484
b3b91714
AM
485#else
486const char *i386_comment_chars = "#";
487#define PREFIX_SEPARATOR '/'
488#endif
489
252b5132
RH
490/* This array holds the chars that only start a comment at the beginning of
491 a line. If the line seems to have the form '# 123 filename'
ce8a8b2f
AM
492 .line and .file directives will appear in the pre-processed output.
493 Note that input_file.c hand checks for '#' at the beginning of the
252b5132 494 first line of the input file. This is because the compiler outputs
ce8a8b2f
AM
495 #NO_APP at the beginning of its output.
496 Also note that comments started like this one will always work if
252b5132 497 '/' isn't otherwise defined. */
b3b91714 498const char line_comment_chars[] = "#/";
252b5132 499
63a0b638 500const char line_separator_chars[] = ";";
252b5132 501
ce8a8b2f
AM
502/* Chars that can be used to separate mant from exp in floating point
503 nums. */
252b5132
RH
504const char EXP_CHARS[] = "eE";
505
ce8a8b2f
AM
506/* Chars that mean this number is a floating point constant
507 As in 0f12.456
508 or 0d1.2345e12. */
de133cf9 509const char FLT_CHARS[] = "fFdDxXhHbB";
252b5132 510
ce8a8b2f 511/* Tables for lexical analysis. */
252b5132
RH
512static char mnemonic_chars[256];
513static char register_chars[256];
514static char operand_chars[256];
515static char identifier_chars[256];
252b5132 516
ce8a8b2f 517/* Lexical macros. */
252b5132
RH
518#define is_mnemonic_char(x) (mnemonic_chars[(unsigned char) x])
519#define is_operand_char(x) (operand_chars[(unsigned char) x])
520#define is_register_char(x) (register_chars[(unsigned char) x])
521#define is_space_char(x) ((x) == ' ')
522#define is_identifier_char(x) (identifier_chars[(unsigned char) x])
252b5132 523
0234cb7c 524/* All non-digit non-letter characters that may occur in an operand. */
252b5132
RH
525static char operand_special_chars[] = "%$-+(,)*._~/<>|&^!:[@]";
526
527/* md_assemble() always leaves the strings it's passed unaltered. To
528 effect this we maintain a stack of saved characters that we've smashed
529 with '\0's (indicating end of strings for various sub-fields of the
47926f60 530 assembler instruction). */
252b5132 531static char save_stack[32];
ce8a8b2f 532static char *save_stack_p;
252b5132
RH
533#define END_STRING_AND_SAVE(s) \
534 do { *save_stack_p++ = *(s); *(s) = '\0'; } while (0)
535#define RESTORE_END_STRING(s) \
536 do { *(s) = *--save_stack_p; } while (0)
537
47926f60 538/* The instruction we're assembling. */
252b5132
RH
539static i386_insn i;
540
541/* Possible templates for current insn. */
542static const templates *current_templates;
543
31b2323c
L
544/* Per instruction expressionS buffers: max displacements & immediates. */
545static expressionS disp_expressions[MAX_MEMORY_OPERANDS];
546static expressionS im_expressions[MAX_IMMEDIATE_OPERANDS];
252b5132 547
47926f60 548/* Current operand we are working on. */
ee86248c 549static int this_operand = -1;
252b5132 550
3e73aa7c
JH
551/* We support four different modes. FLAG_CODE variable is used to distinguish
552 these. */
553
554enum flag_code {
555 CODE_32BIT,
556 CODE_16BIT,
557 CODE_64BIT };
558
559static enum flag_code flag_code;
4fa24527 560static unsigned int object_64bit;
862be3fb 561static unsigned int disallow_64bit_reloc;
3e73aa7c 562static int use_rela_relocations = 0;
e379e5f3
L
563/* __tls_get_addr/___tls_get_addr symbol for TLS. */
564static const char *tls_get_addr;
3e73aa7c 565
7af8ed2d
NC
566#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
567 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
568 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
569
351f65ca
L
570/* The ELF ABI to use. */
571enum x86_elf_abi
572{
573 I386_ABI,
7f56bc95
L
574 X86_64_ABI,
575 X86_64_X32_ABI
351f65ca
L
576};
577
578static enum x86_elf_abi x86_elf_abi = I386_ABI;
7af8ed2d 579#endif
351f65ca 580
167ad85b
TG
581#if defined (TE_PE) || defined (TE_PEP)
582/* Use big object file format. */
583static int use_big_obj = 0;
584#endif
585
8dcea932
L
586#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
587/* 1 if generating code for a shared library. */
588static int shared = 0;
589#endif
590
47926f60
KH
591/* 1 for intel syntax,
592 0 if att syntax. */
593static int intel_syntax = 0;
252b5132 594
4b5aaf5f
L
595static enum x86_64_isa
596{
597 amd64 = 1, /* AMD64 ISA. */
598 intel64 /* Intel64 ISA. */
599} isa64;
e89c5eaa 600
1efbbeb4
L
601/* 1 for intel mnemonic,
602 0 if att mnemonic. */
603static int intel_mnemonic = !SYSV386_COMPAT;
604
a60de03c
JB
605/* 1 if pseudo registers are permitted. */
606static int allow_pseudo_reg = 0;
607
47926f60
KH
608/* 1 if register prefix % not required. */
609static int allow_naked_reg = 0;
252b5132 610
33eaf5de 611/* 1 if the assembler should add BND prefix for all control-transferring
7e8b059b
L
612 instructions supporting it, even if this prefix wasn't specified
613 explicitly. */
614static int add_bnd_prefix = 0;
615
ba104c83 616/* 1 if pseudo index register, eiz/riz, is allowed . */
db51cc60
L
617static int allow_index_reg = 0;
618
d022bddd
IT
619/* 1 if the assembler should ignore LOCK prefix, even if it was
620 specified explicitly. */
621static int omit_lock_prefix = 0;
622
e4e00185
AS
623/* 1 if the assembler should encode lfence, mfence, and sfence as
624 "lock addl $0, (%{re}sp)". */
625static int avoid_fence = 0;
626
ae531041
L
627/* 1 if lfence should be inserted after every load. */
628static int lfence_after_load = 0;
629
630/* Non-zero if lfence should be inserted before indirect branch. */
631static enum lfence_before_indirect_branch_kind
632 {
633 lfence_branch_none = 0,
634 lfence_branch_register,
635 lfence_branch_memory,
636 lfence_branch_all
637 }
638lfence_before_indirect_branch;
639
640/* Non-zero if lfence should be inserted before ret. */
641static enum lfence_before_ret_kind
642 {
643 lfence_before_ret_none = 0,
644 lfence_before_ret_not,
a09f656b 645 lfence_before_ret_or,
646 lfence_before_ret_shl
ae531041
L
647 }
648lfence_before_ret;
649
650/* Types of previous instruction is .byte or prefix. */
e379e5f3
L
651static struct
652 {
653 segT seg;
654 const char *file;
655 const char *name;
656 unsigned int line;
657 enum last_insn_kind
658 {
659 last_insn_other = 0,
660 last_insn_directive,
661 last_insn_prefix
662 } kind;
663 } last_insn;
664
0cb4071e
L
665/* 1 if the assembler should generate relax relocations. */
666
667static int generate_relax_relocations
668 = DEFAULT_GENERATE_X86_RELAX_RELOCATIONS;
669
7bab8ab5 670static enum check_kind
daf50ae7 671 {
7bab8ab5
JB
672 check_none = 0,
673 check_warning,
674 check_error
daf50ae7 675 }
7bab8ab5 676sse_check, operand_check = check_warning;
daf50ae7 677
e379e5f3
L
678/* Non-zero if branches should be aligned within power of 2 boundary. */
679static int align_branch_power = 0;
680
681/* Types of branches to align. */
682enum align_branch_kind
683 {
684 align_branch_none = 0,
685 align_branch_jcc = 1,
686 align_branch_fused = 2,
687 align_branch_jmp = 3,
688 align_branch_call = 4,
689 align_branch_indirect = 5,
690 align_branch_ret = 6
691 };
692
693/* Type bits of branches to align. */
694enum align_branch_bit
695 {
696 align_branch_jcc_bit = 1 << align_branch_jcc,
697 align_branch_fused_bit = 1 << align_branch_fused,
698 align_branch_jmp_bit = 1 << align_branch_jmp,
699 align_branch_call_bit = 1 << align_branch_call,
700 align_branch_indirect_bit = 1 << align_branch_indirect,
701 align_branch_ret_bit = 1 << align_branch_ret
702 };
703
704static unsigned int align_branch = (align_branch_jcc_bit
705 | align_branch_fused_bit
706 | align_branch_jmp_bit);
707
79d72f45
HL
708/* Types of condition jump used by macro-fusion. */
709enum mf_jcc_kind
710 {
711 mf_jcc_jo = 0, /* base opcode 0x70 */
712 mf_jcc_jc, /* base opcode 0x72 */
713 mf_jcc_je, /* base opcode 0x74 */
714 mf_jcc_jna, /* base opcode 0x76 */
715 mf_jcc_js, /* base opcode 0x78 */
716 mf_jcc_jp, /* base opcode 0x7a */
717 mf_jcc_jl, /* base opcode 0x7c */
718 mf_jcc_jle, /* base opcode 0x7e */
719 };
720
721/* Types of compare flag-modifying insntructions used by macro-fusion. */
722enum mf_cmp_kind
723 {
724 mf_cmp_test_and, /* test/cmp */
725 mf_cmp_alu_cmp, /* add/sub/cmp */
726 mf_cmp_incdec /* inc/dec */
727 };
728
e379e5f3
L
729/* The maximum padding size for fused jcc. CMP like instruction can
730 be 9 bytes and jcc can be 6 bytes. Leave room just in case for
731 prefixes. */
732#define MAX_FUSED_JCC_PADDING_SIZE 20
733
734/* The maximum number of prefixes added for an instruction. */
735static unsigned int align_branch_prefix_size = 5;
736
b6f8c7c4
L
737/* Optimization:
738 1. Clear the REX_W bit with register operand if possible.
739 2. Above plus use 128bit vector instruction to clear the full vector
740 register.
741 */
742static int optimize = 0;
743
744/* Optimization:
745 1. Clear the REX_W bit with register operand if possible.
746 2. Above plus use 128bit vector instruction to clear the full vector
747 register.
748 3. Above plus optimize "test{q,l,w} $imm8,%r{64,32,16}" to
749 "testb $imm7,%r8".
750 */
751static int optimize_for_space = 0;
752
2ca3ace5
L
753/* Register prefix used for error message. */
754static const char *register_prefix = "%";
755
47926f60
KH
756/* Used in 16 bit gcc mode to add an l suffix to call, ret, enter,
757 leave, push, and pop instructions so that gcc has the same stack
758 frame as in 32 bit mode. */
759static char stackop_size = '\0';
eecb386c 760
12b55ccc
L
761/* Non-zero to optimize code alignment. */
762int optimize_align_code = 1;
763
47926f60
KH
764/* Non-zero to quieten some warnings. */
765static int quiet_warnings = 0;
a38cf1db 766
47926f60
KH
767/* CPU name. */
768static const char *cpu_arch_name = NULL;
6305a203 769static char *cpu_sub_arch_name = NULL;
a38cf1db 770
47926f60 771/* CPU feature flags. */
40fb9820
L
772static i386_cpu_flags cpu_arch_flags = CPU_UNKNOWN_FLAGS;
773
ccc9c027
L
774/* If we have selected a cpu we are generating instructions for. */
775static int cpu_arch_tune_set = 0;
776
9103f4f4 777/* Cpu we are generating instructions for. */
fbf3f584 778enum processor_type cpu_arch_tune = PROCESSOR_UNKNOWN;
9103f4f4
L
779
780/* CPU feature flags of cpu we are generating instructions for. */
40fb9820 781static i386_cpu_flags cpu_arch_tune_flags;
9103f4f4 782
ccc9c027 783/* CPU instruction set architecture used. */
fbf3f584 784enum processor_type cpu_arch_isa = PROCESSOR_UNKNOWN;
ccc9c027 785
9103f4f4 786/* CPU feature flags of instruction set architecture used. */
fbf3f584 787i386_cpu_flags cpu_arch_isa_flags;
9103f4f4 788
fddf5b5b
AM
789/* If set, conditional jumps are not automatically promoted to handle
790 larger than a byte offset. */
f68697e8 791static bool no_cond_jump_promotion = false;
fddf5b5b 792
c0f3af97
L
793/* Encode SSE instructions with VEX prefix. */
794static unsigned int sse2avx;
795
c8480b58
L
796/* Encode aligned vector move as unaligned vector move. */
797static unsigned int use_unaligned_vector_move;
798
539f890d
L
799/* Encode scalar AVX instructions with specific vector length. */
800static enum
801 {
802 vex128 = 0,
803 vex256
804 } avxscalar;
805
03751133
L
806/* Encode VEX WIG instructions with specific vex.w. */
807static enum
808 {
809 vexw0 = 0,
810 vexw1
811 } vexwig;
812
43234a1e
L
813/* Encode scalar EVEX LIG instructions with specific vector length. */
814static enum
815 {
816 evexl128 = 0,
817 evexl256,
818 evexl512
819 } evexlig;
820
821/* Encode EVEX WIG instructions with specific evex.w. */
822static enum
823 {
824 evexw0 = 0,
825 evexw1
826 } evexwig;
827
d3d3c6db
IT
828/* Value to encode in EVEX RC bits, for SAE-only instructions. */
829static enum rc_type evexrcig = rne;
830
29b0f896 831/* Pre-defined "_GLOBAL_OFFSET_TABLE_". */
87c245cc 832static symbolS *GOT_symbol;
29b0f896 833
a4447b93
RH
834/* The dwarf2 return column, adjusted for 32 or 64 bit. */
835unsigned int x86_dwarf2_return_column;
836
837/* The dwarf2 data alignment, adjusted for 32 or 64 bit. */
838int x86_cie_data_alignment;
839
252b5132 840/* Interface to relax_segment.
fddf5b5b
AM
841 There are 3 major relax states for 386 jump insns because the
842 different types of jumps add different sizes to frags when we're
e379e5f3
L
843 figuring out what sort of jump to choose to reach a given label.
844
845 BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING are used to align
846 branches which are handled by md_estimate_size_before_relax() and
847 i386_generic_table_relax_frag(). */
252b5132 848
47926f60 849/* Types. */
93c2a809
AM
850#define UNCOND_JUMP 0
851#define COND_JUMP 1
852#define COND_JUMP86 2
e379e5f3
L
853#define BRANCH_PADDING 3
854#define BRANCH_PREFIX 4
855#define FUSED_JCC_PADDING 5
fddf5b5b 856
47926f60 857/* Sizes. */
252b5132
RH
858#define CODE16 1
859#define SMALL 0
29b0f896 860#define SMALL16 (SMALL | CODE16)
252b5132 861#define BIG 2
29b0f896 862#define BIG16 (BIG | CODE16)
252b5132
RH
863
864#ifndef INLINE
865#ifdef __GNUC__
866#define INLINE __inline__
867#else
868#define INLINE
869#endif
870#endif
871
fddf5b5b
AM
872#define ENCODE_RELAX_STATE(type, size) \
873 ((relax_substateT) (((type) << 2) | (size)))
874#define TYPE_FROM_RELAX_STATE(s) \
875 ((s) >> 2)
876#define DISP_SIZE_FROM_RELAX_STATE(s) \
877 ((((s) & 3) == BIG ? 4 : (((s) & 3) == BIG16 ? 2 : 1)))
252b5132
RH
878
879/* This table is used by relax_frag to promote short jumps to long
880 ones where necessary. SMALL (short) jumps may be promoted to BIG
881 (32 bit long) ones, and SMALL16 jumps to BIG16 (16 bit long). We
882 don't allow a short jump in a 32 bit code segment to be promoted to
883 a 16 bit offset jump because it's slower (requires data size
884 prefix), and doesn't work, unless the destination is in the bottom
885 64k of the code segment (The top 16 bits of eip are zeroed). */
886
887const relax_typeS md_relax_table[] =
888{
24eab124
AM
889 /* The fields are:
890 1) most positive reach of this state,
891 2) most negative reach of this state,
93c2a809 892 3) how many bytes this mode will have in the variable part of the frag
ce8a8b2f 893 4) which index into the table to try if we can't fit into this one. */
252b5132 894
fddf5b5b 895 /* UNCOND_JUMP states. */
93c2a809
AM
896 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG)},
897 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16)},
898 /* dword jmp adds 4 bytes to frag:
899 0 extra opcode bytes, 4 displacement bytes. */
252b5132 900 {0, 0, 4, 0},
93c2a809
AM
901 /* word jmp adds 2 byte2 to frag:
902 0 extra opcode bytes, 2 displacement bytes. */
252b5132
RH
903 {0, 0, 2, 0},
904
93c2a809
AM
905 /* COND_JUMP states. */
906 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG)},
907 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG16)},
908 /* dword conditionals adds 5 bytes to frag:
909 1 extra opcode byte, 4 displacement bytes. */
910 {0, 0, 5, 0},
fddf5b5b 911 /* word conditionals add 3 bytes to frag:
93c2a809
AM
912 1 extra opcode byte, 2 displacement bytes. */
913 {0, 0, 3, 0},
914
915 /* COND_JUMP86 states. */
916 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG)},
917 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG16)},
918 /* dword conditionals adds 5 bytes to frag:
919 1 extra opcode byte, 4 displacement bytes. */
920 {0, 0, 5, 0},
921 /* word conditionals add 4 bytes to frag:
922 1 displacement byte and a 3 byte long branch insn. */
923 {0, 0, 4, 0}
252b5132
RH
924};
925
6ceeed25 926#define ARCH(n, t, f, s) \
ae89daec
JB
927 { STRING_COMMA_LEN (#n), s, PROCESSOR_ ## t, CPU_ ## f ## _FLAGS, \
928 CPU_NONE_FLAGS }
929#define SUBARCH(n, e, d, s) \
930 { STRING_COMMA_LEN (#n), s, PROCESSOR_NONE, CPU_ ## e ## _FLAGS, \
931 CPU_ ## d ## _FLAGS }
6ceeed25 932
9103f4f4
L
933static const arch_entry cpu_arch[] =
934{
3ce2ebcf
JB
935 /* Do not replace the first two entries - i386_target_format() and
936 set_cpu_arch() rely on them being there in this order. */
6ceeed25
JB
937 ARCH (generic32, GENERIC32, GENERIC32, false),
938 ARCH (generic64, GENERIC64, GENERIC64, false),
939 ARCH (i8086, UNKNOWN, NONE, false),
940 ARCH (i186, UNKNOWN, I186, false),
941 ARCH (i286, UNKNOWN, I286, false),
942 ARCH (i386, I386, I386, false),
943 ARCH (i486, I486, I486, false),
944 ARCH (i586, PENTIUM, I586, false),
945 ARCH (i686, PENTIUMPRO, I686, false),
946 ARCH (pentium, PENTIUM, I586, false),
947 ARCH (pentiumpro, PENTIUMPRO, PENTIUMPRO, false),
948 ARCH (pentiumii, PENTIUMPRO, P2, false),
949 ARCH (pentiumiii, PENTIUMPRO, P3, false),
950 ARCH (pentium4, PENTIUM4, P4, false),
951 ARCH (prescott, NOCONA, CORE, false),
952 ARCH (nocona, NOCONA, NOCONA, false),
953 ARCH (yonah, CORE, CORE, true),
954 ARCH (core, CORE, CORE, false),
955 ARCH (merom, CORE2, CORE2, true),
956 ARCH (core2, CORE2, CORE2, false),
957 ARCH (corei7, COREI7, COREI7, false),
958 ARCH (iamcu, IAMCU, IAMCU, false),
959 ARCH (k6, K6, K6, false),
960 ARCH (k6_2, K6, K6_2, false),
961 ARCH (athlon, ATHLON, ATHLON, false),
962 ARCH (sledgehammer, K8, K8, true),
963 ARCH (opteron, K8, K8, false),
964 ARCH (k8, K8, K8, false),
965 ARCH (amdfam10, AMDFAM10, AMDFAM10, false),
966 ARCH (bdver1, BD, BDVER1, false),
967 ARCH (bdver2, BD, BDVER2, false),
968 ARCH (bdver3, BD, BDVER3, false),
969 ARCH (bdver4, BD, BDVER4, false),
970 ARCH (znver1, ZNVER, ZNVER1, false),
971 ARCH (znver2, ZNVER, ZNVER2, false),
972 ARCH (znver3, ZNVER, ZNVER3, false),
973 ARCH (btver1, BT, BTVER1, false),
974 ARCH (btver2, BT, BTVER2, false),
975
ae89daec
JB
976 SUBARCH (8087, 8087, ANY_X87, false),
977 SUBARCH (87, NONE, ANY_X87, false), /* Disable only! */
978 SUBARCH (287, 287, ANY_287, false),
979 SUBARCH (387, 387, ANY_387, false),
980 SUBARCH (687, 687, ANY_687, false),
981 SUBARCH (cmov, CMOV, ANY_CMOV, false),
982 SUBARCH (fxsr, FXSR, ANY_FXSR, false),
983 SUBARCH (mmx, MMX, ANY_MMX, false),
984 SUBARCH (sse, SSE, ANY_SSE, false),
985 SUBARCH (sse2, SSE2, ANY_SSE2, false),
986 SUBARCH (sse3, SSE3, ANY_SSE3, false),
987 SUBARCH (sse4a, SSE4A, ANY_SSE4A, false),
988 SUBARCH (ssse3, SSSE3, ANY_SSSE3, false),
989 SUBARCH (sse4.1, SSE4_1, ANY_SSE4_1, false),
990 SUBARCH (sse4.2, SSE4_2, ANY_SSE4_2, false),
991 SUBARCH (sse4, SSE4_2, ANY_SSE4_1, false),
992 SUBARCH (avx, AVX, ANY_AVX, false),
993 SUBARCH (avx2, AVX2, ANY_AVX2, false),
994 SUBARCH (avx512f, AVX512F, ANY_AVX512F, false),
995 SUBARCH (avx512cd, AVX512CD, ANY_AVX512CD, false),
996 SUBARCH (avx512er, AVX512ER, ANY_AVX512ER, false),
997 SUBARCH (avx512pf, AVX512PF, ANY_AVX512PF, false),
998 SUBARCH (avx512dq, AVX512DQ, ANY_AVX512DQ, false),
999 SUBARCH (avx512bw, AVX512BW, ANY_AVX512BW, false),
1000 SUBARCH (avx512vl, AVX512VL, ANY_AVX512VL, false),
1001 SUBARCH (vmx, VMX, VMX, false),
1002 SUBARCH (vmfunc, VMFUNC, VMFUNC, false),
1003 SUBARCH (smx, SMX, SMX, false),
1004 SUBARCH (xsave, XSAVE, XSAVE, false),
1005 SUBARCH (xsaveopt, XSAVEOPT, XSAVEOPT, false),
1006 SUBARCH (xsavec, XSAVEC, XSAVEC, false),
1007 SUBARCH (xsaves, XSAVES, XSAVES, false),
1008 SUBARCH (aes, AES, AES, false),
1009 SUBARCH (pclmul, PCLMUL, PCLMUL, false),
1010 SUBARCH (clmul, PCLMUL, PCLMUL, true),
1011 SUBARCH (fsgsbase, FSGSBASE, FSGSBASE, false),
1012 SUBARCH (rdrnd, RDRND, RDRND, false),
1013 SUBARCH (f16c, F16C, F16C, false),
1014 SUBARCH (bmi2, BMI2, BMI2, false),
1015 SUBARCH (fma, FMA, FMA, false),
1016 SUBARCH (fma4, FMA4, FMA4, false),
1017 SUBARCH (xop, XOP, XOP, false),
1018 SUBARCH (lwp, LWP, LWP, false),
1019 SUBARCH (movbe, MOVBE, MOVBE, false),
1020 SUBARCH (cx16, CX16, CX16, false),
1021 SUBARCH (ept, EPT, EPT, false),
1022 SUBARCH (lzcnt, LZCNT, LZCNT, false),
1023 SUBARCH (popcnt, POPCNT, POPCNT, false),
1024 SUBARCH (hle, HLE, HLE, false),
1025 SUBARCH (rtm, RTM, RTM, false),
1026 SUBARCH (invpcid, INVPCID, INVPCID, false),
1027 SUBARCH (clflush, CLFLUSH, CLFLUSH, false),
1028 SUBARCH (nop, NOP, NOP, false),
1029 SUBARCH (syscall, SYSCALL, SYSCALL, false),
1030 SUBARCH (rdtscp, RDTSCP, RDTSCP, false),
1031 SUBARCH (3dnow, 3DNOW, 3DNOW, false),
1032 SUBARCH (3dnowa, 3DNOWA, 3DNOWA, false),
1033 SUBARCH (padlock, PADLOCK, PADLOCK, false),
1034 SUBARCH (pacifica, SVME, SVME, true),
1035 SUBARCH (svme, SVME, SVME, false),
1036 SUBARCH (sse4a, SSE4A, SSE4A, false),
1037 SUBARCH (abm, ABM, ABM, false),
1038 SUBARCH (bmi, BMI, BMI, false),
1039 SUBARCH (tbm, TBM, TBM, false),
1040 SUBARCH (adx, ADX, ADX, false),
1041 SUBARCH (rdseed, RDSEED, RDSEED, false),
1042 SUBARCH (prfchw, PRFCHW, PRFCHW, false),
1043 SUBARCH (smap, SMAP, SMAP, false),
1044 SUBARCH (mpx, MPX, MPX, false),
1045 SUBARCH (sha, SHA, SHA, false),
1046 SUBARCH (clflushopt, CLFLUSHOPT, CLFLUSHOPT, false),
1047 SUBARCH (prefetchwt1, PREFETCHWT1, PREFETCHWT1, false),
1048 SUBARCH (se1, SE1, SE1, false),
1049 SUBARCH (clwb, CLWB, CLWB, false),
1050 SUBARCH (avx512ifma, AVX512IFMA, ANY_AVX512IFMA, false),
1051 SUBARCH (avx512vbmi, AVX512VBMI, ANY_AVX512VBMI, false),
1052 SUBARCH (avx512_4fmaps, AVX512_4FMAPS, ANY_AVX512_4FMAPS, false),
1053 SUBARCH (avx512_4vnniw, AVX512_4VNNIW, ANY_AVX512_4VNNIW, false),
1054 SUBARCH (avx512_vpopcntdq, AVX512_VPOPCNTDQ, ANY_AVX512_VPOPCNTDQ, false),
1055 SUBARCH (avx512_vbmi2, AVX512_VBMI2, ANY_AVX512_VBMI2, false),
1056 SUBARCH (avx512_vnni, AVX512_VNNI, ANY_AVX512_VNNI, false),
1057 SUBARCH (avx512_bitalg, AVX512_BITALG, ANY_AVX512_BITALG, false),
1058 SUBARCH (avx_vnni, AVX_VNNI, ANY_AVX_VNNI, false),
1059 SUBARCH (clzero, CLZERO, CLZERO, false),
1060 SUBARCH (mwaitx, MWAITX, MWAITX, false),
1061 SUBARCH (ospke, OSPKE, OSPKE, false),
1062 SUBARCH (rdpid, RDPID, RDPID, false),
1063 SUBARCH (ptwrite, PTWRITE, PTWRITE, false),
1064 SUBARCH (ibt, IBT, ANY_IBT, false),
1065 SUBARCH (shstk, SHSTK, ANY_SHSTK, false),
1066 SUBARCH (gfni, GFNI, GFNI, false),
1067 SUBARCH (vaes, VAES, VAES, false),
1068 SUBARCH (vpclmulqdq, VPCLMULQDQ, VPCLMULQDQ, false),
1069 SUBARCH (wbnoinvd, WBNOINVD, WBNOINVD, false),
1070 SUBARCH (pconfig, PCONFIG, PCONFIG, false),
1071 SUBARCH (waitpkg, WAITPKG, WAITPKG, false),
1072 SUBARCH (cldemote, CLDEMOTE, CLDEMOTE, false),
1073 SUBARCH (amx_int8, AMX_INT8, ANY_AMX_INT8, false),
1074 SUBARCH (amx_bf16, AMX_BF16, ANY_AMX_BF16, false),
1075 SUBARCH (amx_tile, AMX_TILE, ANY_AMX_TILE, false),
1076 SUBARCH (movdiri, MOVDIRI, ANY_MOVDIRI, false),
1077 SUBARCH (movdir64b, MOVDIR64B, ANY_MOVDIR64B, false),
1078 SUBARCH (avx512_bf16, AVX512_BF16, ANY_AVX512_BF16, false),
1079 SUBARCH (avx512_vp2intersect, AVX512_VP2INTERSECT,
1080 ANY_AVX512_VP2INTERSECT, false),
1081 SUBARCH (tdx, TDX, ANY_TDX, false),
1082 SUBARCH (enqcmd, ENQCMD, ANY_ENQCMD, false),
1083 SUBARCH (serialize, SERIALIZE, ANY_SERIALIZE, false),
1084 SUBARCH (rdpru, RDPRU, RDPRU, false),
1085 SUBARCH (mcommit, MCOMMIT, MCOMMIT, false),
1086 SUBARCH (sev_es, SEV_ES, SEV_ES, false),
1087 SUBARCH (tsxldtrk, TSXLDTRK, ANY_TSXLDTRK, false),
1088 SUBARCH (kl, KL, ANY_KL, false),
1089 SUBARCH (widekl, WIDEKL, ANY_WIDEKL, false),
1090 SUBARCH (uintr, UINTR, ANY_UINTR, false),
1091 SUBARCH (hreset, HRESET, ANY_HRESET, false),
1092 SUBARCH (avx512_fp16, AVX512_FP16, ANY_AVX512_FP16, false),
293f5f65
L
1093};
1094
6ceeed25
JB
1095#undef SUBARCH
1096#undef ARCH
1097
704209c0 1098#ifdef I386COFF
a6c24e68
NC
1099/* Like s_lcomm_internal in gas/read.c but the alignment string
1100 is allowed to be optional. */
1101
1102static symbolS *
1103pe_lcomm_internal (int needs_align, symbolS *symbolP, addressT size)
1104{
1105 addressT align = 0;
1106
1107 SKIP_WHITESPACE ();
1108
7ab9ffdd 1109 if (needs_align
a6c24e68
NC
1110 && *input_line_pointer == ',')
1111 {
1112 align = parse_align (needs_align - 1);
7ab9ffdd 1113
a6c24e68
NC
1114 if (align == (addressT) -1)
1115 return NULL;
1116 }
1117 else
1118 {
1119 if (size >= 8)
1120 align = 3;
1121 else if (size >= 4)
1122 align = 2;
1123 else if (size >= 2)
1124 align = 1;
1125 else
1126 align = 0;
1127 }
1128
1129 bss_alloc (symbolP, size, align);
1130 return symbolP;
1131}
1132
704209c0 1133static void
a6c24e68
NC
1134pe_lcomm (int needs_align)
1135{
1136 s_comm_internal (needs_align * 2, pe_lcomm_internal);
1137}
704209c0 1138#endif
a6c24e68 1139
29b0f896
AM
1140const pseudo_typeS md_pseudo_table[] =
1141{
1142#if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
1143 {"align", s_align_bytes, 0},
1144#else
1145 {"align", s_align_ptwo, 0},
1146#endif
1147 {"arch", set_cpu_arch, 0},
1148#ifndef I386COFF
1149 {"bss", s_bss, 0},
a6c24e68
NC
1150#else
1151 {"lcomm", pe_lcomm, 1},
29b0f896
AM
1152#endif
1153 {"ffloat", float_cons, 'f'},
1154 {"dfloat", float_cons, 'd'},
1155 {"tfloat", float_cons, 'x'},
7d19d096 1156 {"hfloat", float_cons, 'h'},
de133cf9 1157 {"bfloat16", float_cons, 'b'},
29b0f896 1158 {"value", cons, 2},
d182319b 1159 {"slong", signed_cons, 4},
29b0f896
AM
1160 {"noopt", s_ignore, 0},
1161 {"optim", s_ignore, 0},
1162 {"code16gcc", set_16bit_gcc_code_flag, CODE_16BIT},
1163 {"code16", set_code_flag, CODE_16BIT},
1164 {"code32", set_code_flag, CODE_32BIT},
da5f19a2 1165#ifdef BFD64
29b0f896 1166 {"code64", set_code_flag, CODE_64BIT},
da5f19a2 1167#endif
29b0f896
AM
1168 {"intel_syntax", set_intel_syntax, 1},
1169 {"att_syntax", set_intel_syntax, 0},
1efbbeb4
L
1170 {"intel_mnemonic", set_intel_mnemonic, 1},
1171 {"att_mnemonic", set_intel_mnemonic, 0},
db51cc60
L
1172 {"allow_index_reg", set_allow_index_reg, 1},
1173 {"disallow_index_reg", set_allow_index_reg, 0},
7bab8ab5
JB
1174 {"sse_check", set_check, 0},
1175 {"operand_check", set_check, 1},
3b22753a
L
1176#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1177 {"largecomm", handle_large_common, 0},
07a53e5c 1178#else
68d20676 1179 {"file", dwarf2_directive_file, 0},
07a53e5c
RH
1180 {"loc", dwarf2_directive_loc, 0},
1181 {"loc_mark_labels", dwarf2_directive_loc_mark_labels, 0},
3b22753a 1182#endif
6482c264
NC
1183#ifdef TE_PE
1184 {"secrel32", pe_directive_secrel, 0},
145667f8 1185 {"secidx", pe_directive_secidx, 0},
6482c264 1186#endif
29b0f896
AM
1187 {0, 0, 0}
1188};
1189
1190/* For interface with expression (). */
1191extern char *input_line_pointer;
1192
1193/* Hash table for instruction mnemonic lookup. */
629310ab 1194static htab_t op_hash;
29b0f896
AM
1195
1196/* Hash table for register lookup. */
629310ab 1197static htab_t reg_hash;
29b0f896 1198\f
ce8a8b2f
AM
1199 /* Various efficient no-op patterns for aligning code labels.
1200 Note: Don't try to assemble the instructions in the comments.
1201 0L and 0w are not legal. */
62a02d25
L
1202static const unsigned char f32_1[] =
1203 {0x90}; /* nop */
1204static const unsigned char f32_2[] =
1205 {0x66,0x90}; /* xchg %ax,%ax */
1206static const unsigned char f32_3[] =
1207 {0x8d,0x76,0x00}; /* leal 0(%esi),%esi */
1208static const unsigned char f32_4[] =
1209 {0x8d,0x74,0x26,0x00}; /* leal 0(%esi,1),%esi */
62a02d25
L
1210static const unsigned char f32_6[] =
1211 {0x8d,0xb6,0x00,0x00,0x00,0x00}; /* leal 0L(%esi),%esi */
1212static const unsigned char f32_7[] =
1213 {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal 0L(%esi,1),%esi */
62a02d25 1214static const unsigned char f16_3[] =
3ae729d5 1215 {0x8d,0x74,0x00}; /* lea 0(%si),%si */
62a02d25 1216static const unsigned char f16_4[] =
3ae729d5
L
1217 {0x8d,0xb4,0x00,0x00}; /* lea 0W(%si),%si */
1218static const unsigned char jump_disp8[] =
1219 {0xeb}; /* jmp disp8 */
1220static const unsigned char jump32_disp32[] =
1221 {0xe9}; /* jmp disp32 */
1222static const unsigned char jump16_disp32[] =
1223 {0x66,0xe9}; /* jmp disp32 */
62a02d25
L
1224/* 32-bit NOPs patterns. */
1225static const unsigned char *const f32_patt[] = {
3ae729d5 1226 f32_1, f32_2, f32_3, f32_4, NULL, f32_6, f32_7
62a02d25
L
1227};
1228/* 16-bit NOPs patterns. */
1229static const unsigned char *const f16_patt[] = {
3ae729d5 1230 f32_1, f32_2, f16_3, f16_4
62a02d25
L
1231};
1232/* nopl (%[re]ax) */
1233static const unsigned char alt_3[] =
1234 {0x0f,0x1f,0x00};
1235/* nopl 0(%[re]ax) */
1236static const unsigned char alt_4[] =
1237 {0x0f,0x1f,0x40,0x00};
1238/* nopl 0(%[re]ax,%[re]ax,1) */
1239static const unsigned char alt_5[] =
1240 {0x0f,0x1f,0x44,0x00,0x00};
1241/* nopw 0(%[re]ax,%[re]ax,1) */
1242static const unsigned char alt_6[] =
1243 {0x66,0x0f,0x1f,0x44,0x00,0x00};
1244/* nopl 0L(%[re]ax) */
1245static const unsigned char alt_7[] =
1246 {0x0f,0x1f,0x80,0x00,0x00,0x00,0x00};
1247/* nopl 0L(%[re]ax,%[re]ax,1) */
1248static const unsigned char alt_8[] =
1249 {0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1250/* nopw 0L(%[re]ax,%[re]ax,1) */
1251static const unsigned char alt_9[] =
1252 {0x66,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1253/* nopw %cs:0L(%[re]ax,%[re]ax,1) */
1254static const unsigned char alt_10[] =
1255 {0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
3ae729d5
L
1256/* data16 nopw %cs:0L(%eax,%eax,1) */
1257static const unsigned char alt_11[] =
1258 {0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
62a02d25
L
1259/* 32-bit and 64-bit NOPs patterns. */
1260static const unsigned char *const alt_patt[] = {
1261 f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
3ae729d5 1262 alt_9, alt_10, alt_11
62a02d25
L
1263};
1264
1265/* Genenerate COUNT bytes of NOPs to WHERE from PATT with the maximum
1266 size of a single NOP instruction MAX_SINGLE_NOP_SIZE. */
1267
1268static void
1269i386_output_nops (char *where, const unsigned char *const *patt,
1270 int count, int max_single_nop_size)
1271
1272{
3ae729d5
L
1273 /* Place the longer NOP first. */
1274 int last;
1275 int offset;
3076e594
NC
1276 const unsigned char *nops;
1277
1278 if (max_single_nop_size < 1)
1279 {
1280 as_fatal (_("i386_output_nops called to generate nops of at most %d bytes!"),
1281 max_single_nop_size);
1282 return;
1283 }
1284
1285 nops = patt[max_single_nop_size - 1];
3ae729d5
L
1286
1287 /* Use the smaller one if the requsted one isn't available. */
1288 if (nops == NULL)
62a02d25 1289 {
3ae729d5
L
1290 max_single_nop_size--;
1291 nops = patt[max_single_nop_size - 1];
62a02d25
L
1292 }
1293
3ae729d5
L
1294 last = count % max_single_nop_size;
1295
1296 count -= last;
1297 for (offset = 0; offset < count; offset += max_single_nop_size)
1298 memcpy (where + offset, nops, max_single_nop_size);
1299
1300 if (last)
1301 {
1302 nops = patt[last - 1];
1303 if (nops == NULL)
1304 {
1305 /* Use the smaller one plus one-byte NOP if the needed one
1306 isn't available. */
1307 last--;
1308 nops = patt[last - 1];
1309 memcpy (where + offset, nops, last);
1310 where[offset + last] = *patt[0];
1311 }
1312 else
1313 memcpy (where + offset, nops, last);
1314 }
62a02d25
L
1315}
1316
3ae729d5
L
1317static INLINE int
1318fits_in_imm7 (offsetT num)
1319{
1320 return (num & 0x7f) == num;
1321}
1322
1323static INLINE int
1324fits_in_imm31 (offsetT num)
1325{
1326 return (num & 0x7fffffff) == num;
1327}
62a02d25
L
1328
1329/* Genenerate COUNT bytes of NOPs to WHERE with the maximum size of a
1330 single NOP instruction LIMIT. */
1331
1332void
3ae729d5 1333i386_generate_nops (fragS *fragP, char *where, offsetT count, int limit)
62a02d25 1334{
3ae729d5 1335 const unsigned char *const *patt = NULL;
62a02d25 1336 int max_single_nop_size;
3ae729d5
L
1337 /* Maximum number of NOPs before switching to jump over NOPs. */
1338 int max_number_of_nops;
62a02d25 1339
3ae729d5 1340 switch (fragP->fr_type)
62a02d25 1341 {
3ae729d5
L
1342 case rs_fill_nop:
1343 case rs_align_code:
1344 break;
e379e5f3
L
1345 case rs_machine_dependent:
1346 /* Allow NOP padding for jumps and calls. */
1347 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
1348 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
1349 break;
1350 /* Fall through. */
3ae729d5 1351 default:
62a02d25
L
1352 return;
1353 }
1354
ccc9c027
L
1355 /* We need to decide which NOP sequence to use for 32bit and
1356 64bit. When -mtune= is used:
4eed87de 1357
76bc74dc
L
1358 1. For PROCESSOR_I386, PROCESSOR_I486, PROCESSOR_PENTIUM and
1359 PROCESSOR_GENERIC32, f32_patt will be used.
80b8656c
L
1360 2. For the rest, alt_patt will be used.
1361
1362 When -mtune= isn't used, alt_patt will be used if
22109423 1363 cpu_arch_isa_flags has CpuNop. Otherwise, f32_patt will
76bc74dc 1364 be used.
ccc9c027
L
1365
1366 When -march= or .arch is used, we can't use anything beyond
1367 cpu_arch_isa_flags. */
1368
1369 if (flag_code == CODE_16BIT)
1370 {
3ae729d5
L
1371 patt = f16_patt;
1372 max_single_nop_size = sizeof (f16_patt) / sizeof (f16_patt[0]);
1373 /* Limit number of NOPs to 2 in 16-bit mode. */
1374 max_number_of_nops = 2;
252b5132 1375 }
33fef721 1376 else
ccc9c027 1377 {
fbf3f584 1378 if (fragP->tc_frag_data.isa == PROCESSOR_UNKNOWN)
ccc9c027
L
1379 {
1380 /* PROCESSOR_UNKNOWN means that all ISAs may be used. */
1381 switch (cpu_arch_tune)
1382 {
1383 case PROCESSOR_UNKNOWN:
1384 /* We use cpu_arch_isa_flags to check if we SHOULD
22109423
L
1385 optimize with nops. */
1386 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1387 patt = alt_patt;
ccc9c027
L
1388 else
1389 patt = f32_patt;
1390 break;
ccc9c027
L
1391 case PROCESSOR_PENTIUM4:
1392 case PROCESSOR_NOCONA:
ef05d495 1393 case PROCESSOR_CORE:
76bc74dc 1394 case PROCESSOR_CORE2:
bd5295b2 1395 case PROCESSOR_COREI7:
76bc74dc 1396 case PROCESSOR_GENERIC64:
ccc9c027
L
1397 case PROCESSOR_K6:
1398 case PROCESSOR_ATHLON:
1399 case PROCESSOR_K8:
4eed87de 1400 case PROCESSOR_AMDFAM10:
8aedb9fe 1401 case PROCESSOR_BD:
029f3522 1402 case PROCESSOR_ZNVER:
7b458c12 1403 case PROCESSOR_BT:
80b8656c 1404 patt = alt_patt;
ccc9c027 1405 break;
76bc74dc 1406 case PROCESSOR_I386:
ccc9c027
L
1407 case PROCESSOR_I486:
1408 case PROCESSOR_PENTIUM:
2dde1948 1409 case PROCESSOR_PENTIUMPRO:
81486035 1410 case PROCESSOR_IAMCU:
ccc9c027
L
1411 case PROCESSOR_GENERIC32:
1412 patt = f32_patt;
1413 break;
c368d2a8
JB
1414 case PROCESSOR_NONE:
1415 abort ();
4eed87de 1416 }
ccc9c027
L
1417 }
1418 else
1419 {
fbf3f584 1420 switch (fragP->tc_frag_data.tune)
ccc9c027
L
1421 {
1422 case PROCESSOR_UNKNOWN:
e6a14101 1423 /* When cpu_arch_isa is set, cpu_arch_tune shouldn't be
ccc9c027
L
1424 PROCESSOR_UNKNOWN. */
1425 abort ();
1426 break;
1427
76bc74dc 1428 case PROCESSOR_I386:
ccc9c027
L
1429 case PROCESSOR_I486:
1430 case PROCESSOR_PENTIUM:
81486035 1431 case PROCESSOR_IAMCU:
ccc9c027
L
1432 case PROCESSOR_K6:
1433 case PROCESSOR_ATHLON:
1434 case PROCESSOR_K8:
4eed87de 1435 case PROCESSOR_AMDFAM10:
8aedb9fe 1436 case PROCESSOR_BD:
029f3522 1437 case PROCESSOR_ZNVER:
7b458c12 1438 case PROCESSOR_BT:
ccc9c027
L
1439 case PROCESSOR_GENERIC32:
1440 /* We use cpu_arch_isa_flags to check if we CAN optimize
22109423
L
1441 with nops. */
1442 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1443 patt = alt_patt;
ccc9c027
L
1444 else
1445 patt = f32_patt;
1446 break;
76bc74dc
L
1447 case PROCESSOR_PENTIUMPRO:
1448 case PROCESSOR_PENTIUM4:
1449 case PROCESSOR_NOCONA:
1450 case PROCESSOR_CORE:
ef05d495 1451 case PROCESSOR_CORE2:
bd5295b2 1452 case PROCESSOR_COREI7:
22109423 1453 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1454 patt = alt_patt;
ccc9c027
L
1455 else
1456 patt = f32_patt;
1457 break;
1458 case PROCESSOR_GENERIC64:
80b8656c 1459 patt = alt_patt;
ccc9c027 1460 break;
c368d2a8
JB
1461 case PROCESSOR_NONE:
1462 abort ();
4eed87de 1463 }
ccc9c027
L
1464 }
1465
76bc74dc
L
1466 if (patt == f32_patt)
1467 {
3ae729d5
L
1468 max_single_nop_size = sizeof (f32_patt) / sizeof (f32_patt[0]);
1469 /* Limit number of NOPs to 2 for older processors. */
1470 max_number_of_nops = 2;
76bc74dc
L
1471 }
1472 else
1473 {
3ae729d5
L
1474 max_single_nop_size = sizeof (alt_patt) / sizeof (alt_patt[0]);
1475 /* Limit number of NOPs to 7 for newer processors. */
1476 max_number_of_nops = 7;
1477 }
1478 }
1479
1480 if (limit == 0)
1481 limit = max_single_nop_size;
1482
1483 if (fragP->fr_type == rs_fill_nop)
1484 {
1485 /* Output NOPs for .nop directive. */
1486 if (limit > max_single_nop_size)
1487 {
1488 as_bad_where (fragP->fr_file, fragP->fr_line,
1489 _("invalid single nop size: %d "
1490 "(expect within [0, %d])"),
1491 limit, max_single_nop_size);
1492 return;
1493 }
1494 }
e379e5f3 1495 else if (fragP->fr_type != rs_machine_dependent)
3ae729d5
L
1496 fragP->fr_var = count;
1497
1498 if ((count / max_single_nop_size) > max_number_of_nops)
1499 {
1500 /* Generate jump over NOPs. */
1501 offsetT disp = count - 2;
1502 if (fits_in_imm7 (disp))
1503 {
1504 /* Use "jmp disp8" if possible. */
1505 count = disp;
1506 where[0] = jump_disp8[0];
1507 where[1] = count;
1508 where += 2;
1509 }
1510 else
1511 {
1512 unsigned int size_of_jump;
1513
1514 if (flag_code == CODE_16BIT)
1515 {
1516 where[0] = jump16_disp32[0];
1517 where[1] = jump16_disp32[1];
1518 size_of_jump = 2;
1519 }
1520 else
1521 {
1522 where[0] = jump32_disp32[0];
1523 size_of_jump = 1;
1524 }
1525
1526 count -= size_of_jump + 4;
1527 if (!fits_in_imm31 (count))
1528 {
1529 as_bad_where (fragP->fr_file, fragP->fr_line,
1530 _("jump over nop padding out of range"));
1531 return;
1532 }
1533
1534 md_number_to_chars (where + size_of_jump, count, 4);
1535 where += size_of_jump + 4;
76bc74dc 1536 }
ccc9c027 1537 }
3ae729d5
L
1538
1539 /* Generate multiple NOPs. */
1540 i386_output_nops (where, patt, count, limit);
252b5132
RH
1541}
1542
c6fb90c8 1543static INLINE int
0dfbf9d7 1544operand_type_all_zero (const union i386_operand_type *x)
40fb9820 1545{
0dfbf9d7 1546 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1547 {
1548 case 3:
0dfbf9d7 1549 if (x->array[2])
c6fb90c8 1550 return 0;
1a0670f3 1551 /* Fall through. */
c6fb90c8 1552 case 2:
0dfbf9d7 1553 if (x->array[1])
c6fb90c8 1554 return 0;
1a0670f3 1555 /* Fall through. */
c6fb90c8 1556 case 1:
0dfbf9d7 1557 return !x->array[0];
c6fb90c8
L
1558 default:
1559 abort ();
1560 }
40fb9820
L
1561}
1562
c6fb90c8 1563static INLINE void
0dfbf9d7 1564operand_type_set (union i386_operand_type *x, unsigned int v)
40fb9820 1565{
0dfbf9d7 1566 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1567 {
1568 case 3:
0dfbf9d7 1569 x->array[2] = v;
1a0670f3 1570 /* Fall through. */
c6fb90c8 1571 case 2:
0dfbf9d7 1572 x->array[1] = v;
1a0670f3 1573 /* Fall through. */
c6fb90c8 1574 case 1:
0dfbf9d7 1575 x->array[0] = v;
1a0670f3 1576 /* Fall through. */
c6fb90c8
L
1577 break;
1578 default:
1579 abort ();
1580 }
bab6aec1
JB
1581
1582 x->bitfield.class = ClassNone;
75e5731b 1583 x->bitfield.instance = InstanceNone;
c6fb90c8 1584}
40fb9820 1585
c6fb90c8 1586static INLINE int
0dfbf9d7
L
1587operand_type_equal (const union i386_operand_type *x,
1588 const union i386_operand_type *y)
c6fb90c8 1589{
0dfbf9d7 1590 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1591 {
1592 case 3:
0dfbf9d7 1593 if (x->array[2] != y->array[2])
c6fb90c8 1594 return 0;
1a0670f3 1595 /* Fall through. */
c6fb90c8 1596 case 2:
0dfbf9d7 1597 if (x->array[1] != y->array[1])
c6fb90c8 1598 return 0;
1a0670f3 1599 /* Fall through. */
c6fb90c8 1600 case 1:
0dfbf9d7 1601 return x->array[0] == y->array[0];
c6fb90c8
L
1602 break;
1603 default:
1604 abort ();
1605 }
1606}
40fb9820 1607
0dfbf9d7
L
1608static INLINE int
1609cpu_flags_all_zero (const union i386_cpu_flags *x)
1610{
1611 switch (ARRAY_SIZE(x->array))
1612 {
53467f57
IT
1613 case 4:
1614 if (x->array[3])
1615 return 0;
1616 /* Fall through. */
0dfbf9d7
L
1617 case 3:
1618 if (x->array[2])
1619 return 0;
1a0670f3 1620 /* Fall through. */
0dfbf9d7
L
1621 case 2:
1622 if (x->array[1])
1623 return 0;
1a0670f3 1624 /* Fall through. */
0dfbf9d7
L
1625 case 1:
1626 return !x->array[0];
1627 default:
1628 abort ();
1629 }
1630}
1631
0dfbf9d7
L
1632static INLINE int
1633cpu_flags_equal (const union i386_cpu_flags *x,
1634 const union i386_cpu_flags *y)
1635{
1636 switch (ARRAY_SIZE(x->array))
1637 {
53467f57
IT
1638 case 4:
1639 if (x->array[3] != y->array[3])
1640 return 0;
1641 /* Fall through. */
0dfbf9d7
L
1642 case 3:
1643 if (x->array[2] != y->array[2])
1644 return 0;
1a0670f3 1645 /* Fall through. */
0dfbf9d7
L
1646 case 2:
1647 if (x->array[1] != y->array[1])
1648 return 0;
1a0670f3 1649 /* Fall through. */
0dfbf9d7
L
1650 case 1:
1651 return x->array[0] == y->array[0];
1652 break;
1653 default:
1654 abort ();
1655 }
1656}
c6fb90c8
L
1657
1658static INLINE int
1659cpu_flags_check_cpu64 (i386_cpu_flags f)
1660{
1661 return !((flag_code == CODE_64BIT && f.bitfield.cpuno64)
1662 || (flag_code != CODE_64BIT && f.bitfield.cpu64));
40fb9820
L
1663}
1664
c6fb90c8
L
1665static INLINE i386_cpu_flags
1666cpu_flags_and (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1667{
c6fb90c8
L
1668 switch (ARRAY_SIZE (x.array))
1669 {
53467f57
IT
1670 case 4:
1671 x.array [3] &= y.array [3];
1672 /* Fall through. */
c6fb90c8
L
1673 case 3:
1674 x.array [2] &= y.array [2];
1a0670f3 1675 /* Fall through. */
c6fb90c8
L
1676 case 2:
1677 x.array [1] &= y.array [1];
1a0670f3 1678 /* Fall through. */
c6fb90c8
L
1679 case 1:
1680 x.array [0] &= y.array [0];
1681 break;
1682 default:
1683 abort ();
1684 }
1685 return x;
1686}
40fb9820 1687
c6fb90c8
L
1688static INLINE i386_cpu_flags
1689cpu_flags_or (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1690{
c6fb90c8 1691 switch (ARRAY_SIZE (x.array))
40fb9820 1692 {
53467f57
IT
1693 case 4:
1694 x.array [3] |= y.array [3];
1695 /* Fall through. */
c6fb90c8
L
1696 case 3:
1697 x.array [2] |= y.array [2];
1a0670f3 1698 /* Fall through. */
c6fb90c8
L
1699 case 2:
1700 x.array [1] |= y.array [1];
1a0670f3 1701 /* Fall through. */
c6fb90c8
L
1702 case 1:
1703 x.array [0] |= y.array [0];
40fb9820
L
1704 break;
1705 default:
1706 abort ();
1707 }
40fb9820
L
1708 return x;
1709}
1710
309d3373
JB
1711static INLINE i386_cpu_flags
1712cpu_flags_and_not (i386_cpu_flags x, i386_cpu_flags y)
1713{
1714 switch (ARRAY_SIZE (x.array))
1715 {
53467f57
IT
1716 case 4:
1717 x.array [3] &= ~y.array [3];
1718 /* Fall through. */
309d3373
JB
1719 case 3:
1720 x.array [2] &= ~y.array [2];
1a0670f3 1721 /* Fall through. */
309d3373
JB
1722 case 2:
1723 x.array [1] &= ~y.array [1];
1a0670f3 1724 /* Fall through. */
309d3373
JB
1725 case 1:
1726 x.array [0] &= ~y.array [0];
1727 break;
1728 default:
1729 abort ();
1730 }
1731 return x;
1732}
1733
6c0946d0
JB
1734static const i386_cpu_flags avx512 = CPU_ANY_AVX512F_FLAGS;
1735
c0f3af97
L
1736#define CPU_FLAGS_ARCH_MATCH 0x1
1737#define CPU_FLAGS_64BIT_MATCH 0x2
1738
c0f3af97 1739#define CPU_FLAGS_PERFECT_MATCH \
db12e14e 1740 (CPU_FLAGS_ARCH_MATCH | CPU_FLAGS_64BIT_MATCH)
c0f3af97
L
1741
1742/* Return CPU flags match bits. */
3629bb00 1743
40fb9820 1744static int
d3ce72d0 1745cpu_flags_match (const insn_template *t)
40fb9820 1746{
c0f3af97
L
1747 i386_cpu_flags x = t->cpu_flags;
1748 int match = cpu_flags_check_cpu64 (x) ? CPU_FLAGS_64BIT_MATCH : 0;
40fb9820
L
1749
1750 x.bitfield.cpu64 = 0;
1751 x.bitfield.cpuno64 = 0;
1752
0dfbf9d7 1753 if (cpu_flags_all_zero (&x))
c0f3af97
L
1754 {
1755 /* This instruction is available on all archs. */
db12e14e 1756 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 1757 }
3629bb00
L
1758 else
1759 {
c0f3af97 1760 /* This instruction is available only on some archs. */
3629bb00
L
1761 i386_cpu_flags cpu = cpu_arch_flags;
1762
ab592e75
JB
1763 /* AVX512VL is no standalone feature - match it and then strip it. */
1764 if (x.bitfield.cpuavx512vl && !cpu.bitfield.cpuavx512vl)
1765 return match;
1766 x.bitfield.cpuavx512vl = 0;
1767
22c36940
JB
1768 /* AVX and AVX2 present at the same time express an operand size
1769 dependency - strip AVX2 for the purposes here. The operand size
1770 dependent check occurs in check_vecOperands(). */
1771 if (x.bitfield.cpuavx && x.bitfield.cpuavx2)
1772 x.bitfield.cpuavx2 = 0;
1773
3629bb00 1774 cpu = cpu_flags_and (x, cpu);
c0f3af97
L
1775 if (!cpu_flags_all_zero (&cpu))
1776 {
57392598 1777 if (x.bitfield.cpuavx)
a5ff0eb2 1778 {
929f69fa 1779 /* We need to check a few extra flags with AVX. */
b9d49817 1780 if (cpu.bitfield.cpuavx
40d231b4
JB
1781 && (!t->opcode_modifier.sse2avx
1782 || (sse2avx && !i.prefix[DATA_PREFIX]))
b9d49817 1783 && (!x.bitfield.cpuaes || cpu.bitfield.cpuaes)
929f69fa 1784 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
b9d49817
JB
1785 && (!x.bitfield.cpupclmul || cpu.bitfield.cpupclmul))
1786 match |= CPU_FLAGS_ARCH_MATCH;
a5ff0eb2 1787 }
929f69fa
JB
1788 else if (x.bitfield.cpuavx512f)
1789 {
1790 /* We need to check a few extra flags with AVX512F. */
1791 if (cpu.bitfield.cpuavx512f
1792 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
1793 && (!x.bitfield.cpuvaes || cpu.bitfield.cpuvaes)
1794 && (!x.bitfield.cpuvpclmulqdq || cpu.bitfield.cpuvpclmulqdq))
1795 match |= CPU_FLAGS_ARCH_MATCH;
1796 }
a5ff0eb2 1797 else
db12e14e 1798 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 1799 }
3629bb00 1800 }
c0f3af97 1801 return match;
40fb9820
L
1802}
1803
c6fb90c8
L
1804static INLINE i386_operand_type
1805operand_type_and (i386_operand_type x, i386_operand_type y)
40fb9820 1806{
bab6aec1
JB
1807 if (x.bitfield.class != y.bitfield.class)
1808 x.bitfield.class = ClassNone;
75e5731b
JB
1809 if (x.bitfield.instance != y.bitfield.instance)
1810 x.bitfield.instance = InstanceNone;
bab6aec1 1811
c6fb90c8
L
1812 switch (ARRAY_SIZE (x.array))
1813 {
1814 case 3:
1815 x.array [2] &= y.array [2];
1a0670f3 1816 /* Fall through. */
c6fb90c8
L
1817 case 2:
1818 x.array [1] &= y.array [1];
1a0670f3 1819 /* Fall through. */
c6fb90c8
L
1820 case 1:
1821 x.array [0] &= y.array [0];
1822 break;
1823 default:
1824 abort ();
1825 }
1826 return x;
40fb9820
L
1827}
1828
73053c1f
JB
1829static INLINE i386_operand_type
1830operand_type_and_not (i386_operand_type x, i386_operand_type y)
1831{
bab6aec1 1832 gas_assert (y.bitfield.class == ClassNone);
75e5731b 1833 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 1834
73053c1f
JB
1835 switch (ARRAY_SIZE (x.array))
1836 {
1837 case 3:
1838 x.array [2] &= ~y.array [2];
1839 /* Fall through. */
1840 case 2:
1841 x.array [1] &= ~y.array [1];
1842 /* Fall through. */
1843 case 1:
1844 x.array [0] &= ~y.array [0];
1845 break;
1846 default:
1847 abort ();
1848 }
1849 return x;
1850}
1851
c6fb90c8
L
1852static INLINE i386_operand_type
1853operand_type_or (i386_operand_type x, i386_operand_type y)
40fb9820 1854{
bab6aec1
JB
1855 gas_assert (x.bitfield.class == ClassNone ||
1856 y.bitfield.class == ClassNone ||
1857 x.bitfield.class == y.bitfield.class);
75e5731b
JB
1858 gas_assert (x.bitfield.instance == InstanceNone ||
1859 y.bitfield.instance == InstanceNone ||
1860 x.bitfield.instance == y.bitfield.instance);
bab6aec1 1861
c6fb90c8 1862 switch (ARRAY_SIZE (x.array))
40fb9820 1863 {
c6fb90c8
L
1864 case 3:
1865 x.array [2] |= y.array [2];
1a0670f3 1866 /* Fall through. */
c6fb90c8
L
1867 case 2:
1868 x.array [1] |= y.array [1];
1a0670f3 1869 /* Fall through. */
c6fb90c8
L
1870 case 1:
1871 x.array [0] |= y.array [0];
40fb9820
L
1872 break;
1873 default:
1874 abort ();
1875 }
c6fb90c8
L
1876 return x;
1877}
40fb9820 1878
c6fb90c8
L
1879static INLINE i386_operand_type
1880operand_type_xor (i386_operand_type x, i386_operand_type y)
1881{
bab6aec1 1882 gas_assert (y.bitfield.class == ClassNone);
75e5731b 1883 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 1884
c6fb90c8
L
1885 switch (ARRAY_SIZE (x.array))
1886 {
1887 case 3:
1888 x.array [2] ^= y.array [2];
1a0670f3 1889 /* Fall through. */
c6fb90c8
L
1890 case 2:
1891 x.array [1] ^= y.array [1];
1a0670f3 1892 /* Fall through. */
c6fb90c8
L
1893 case 1:
1894 x.array [0] ^= y.array [0];
1895 break;
1896 default:
1897 abort ();
1898 }
40fb9820
L
1899 return x;
1900}
1901
40fb9820 1902static const i386_operand_type disp16_32 = OPERAND_TYPE_DISP16_32;
bab6aec1
JB
1903static const i386_operand_type anydisp = OPERAND_TYPE_ANYDISP;
1904static const i386_operand_type anyimm = OPERAND_TYPE_ANYIMM;
40fb9820 1905static const i386_operand_type regxmm = OPERAND_TYPE_REGXMM;
43234a1e 1906static const i386_operand_type regmask = OPERAND_TYPE_REGMASK;
40fb9820
L
1907static const i386_operand_type imm8 = OPERAND_TYPE_IMM8;
1908static const i386_operand_type imm8s = OPERAND_TYPE_IMM8S;
1909static const i386_operand_type imm16 = OPERAND_TYPE_IMM16;
1910static const i386_operand_type imm32 = OPERAND_TYPE_IMM32;
1911static const i386_operand_type imm32s = OPERAND_TYPE_IMM32S;
1912static const i386_operand_type imm64 = OPERAND_TYPE_IMM64;
1913static const i386_operand_type imm16_32 = OPERAND_TYPE_IMM16_32;
1914static const i386_operand_type imm16_32s = OPERAND_TYPE_IMM16_32S;
1915static const i386_operand_type imm16_32_32s = OPERAND_TYPE_IMM16_32_32S;
1916
1917enum operand_type
1918{
1919 reg,
40fb9820
L
1920 imm,
1921 disp,
1922 anymem
1923};
1924
c6fb90c8 1925static INLINE int
40fb9820
L
1926operand_type_check (i386_operand_type t, enum operand_type c)
1927{
1928 switch (c)
1929 {
1930 case reg:
bab6aec1 1931 return t.bitfield.class == Reg;
40fb9820 1932
40fb9820
L
1933 case imm:
1934 return (t.bitfield.imm8
1935 || t.bitfield.imm8s
1936 || t.bitfield.imm16
1937 || t.bitfield.imm32
1938 || t.bitfield.imm32s
1939 || t.bitfield.imm64);
1940
1941 case disp:
1942 return (t.bitfield.disp8
1943 || t.bitfield.disp16
1944 || t.bitfield.disp32
40fb9820
L
1945 || t.bitfield.disp64);
1946
1947 case anymem:
1948 return (t.bitfield.disp8
1949 || t.bitfield.disp16
1950 || t.bitfield.disp32
40fb9820
L
1951 || t.bitfield.disp64
1952 || t.bitfield.baseindex);
1953
1954 default:
1955 abort ();
1956 }
2cfe26b6
AM
1957
1958 return 0;
40fb9820
L
1959}
1960
7a54636a
L
1961/* Return 1 if there is no conflict in 8bit/16bit/32bit/64bit/80bit size
1962 between operand GIVEN and opeand WANTED for instruction template T. */
5c07affc
L
1963
1964static INLINE int
7a54636a
L
1965match_operand_size (const insn_template *t, unsigned int wanted,
1966 unsigned int given)
5c07affc 1967{
3ac21baa
JB
1968 return !((i.types[given].bitfield.byte
1969 && !t->operand_types[wanted].bitfield.byte)
1970 || (i.types[given].bitfield.word
1971 && !t->operand_types[wanted].bitfield.word)
1972 || (i.types[given].bitfield.dword
1973 && !t->operand_types[wanted].bitfield.dword)
1974 || (i.types[given].bitfield.qword
1975 && !t->operand_types[wanted].bitfield.qword)
1976 || (i.types[given].bitfield.tbyte
1977 && !t->operand_types[wanted].bitfield.tbyte));
5c07affc
L
1978}
1979
dd40ce22
L
1980/* Return 1 if there is no conflict in SIMD register between operand
1981 GIVEN and opeand WANTED for instruction template T. */
1b54b8d7
JB
1982
1983static INLINE int
dd40ce22
L
1984match_simd_size (const insn_template *t, unsigned int wanted,
1985 unsigned int given)
1b54b8d7 1986{
3ac21baa
JB
1987 return !((i.types[given].bitfield.xmmword
1988 && !t->operand_types[wanted].bitfield.xmmword)
1989 || (i.types[given].bitfield.ymmword
1990 && !t->operand_types[wanted].bitfield.ymmword)
1991 || (i.types[given].bitfield.zmmword
260cd341
LC
1992 && !t->operand_types[wanted].bitfield.zmmword)
1993 || (i.types[given].bitfield.tmmword
1994 && !t->operand_types[wanted].bitfield.tmmword));
1b54b8d7
JB
1995}
1996
7a54636a
L
1997/* Return 1 if there is no conflict in any size between operand GIVEN
1998 and opeand WANTED for instruction template T. */
5c07affc
L
1999
2000static INLINE int
dd40ce22
L
2001match_mem_size (const insn_template *t, unsigned int wanted,
2002 unsigned int given)
5c07affc 2003{
7a54636a 2004 return (match_operand_size (t, wanted, given)
3ac21baa 2005 && !((i.types[given].bitfield.unspecified
5273a3cd 2006 && !i.broadcast.type
a5748e0d 2007 && !i.broadcast.bytes
3ac21baa
JB
2008 && !t->operand_types[wanted].bitfield.unspecified)
2009 || (i.types[given].bitfield.fword
2010 && !t->operand_types[wanted].bitfield.fword)
1b54b8d7
JB
2011 /* For scalar opcode templates to allow register and memory
2012 operands at the same time, some special casing is needed
d6793fa1
JB
2013 here. Also for v{,p}broadcast*, {,v}pmov{s,z}*, and
2014 down-conversion vpmov*. */
3528c362 2015 || ((t->operand_types[wanted].bitfield.class == RegSIMD
bc49bfd8
JB
2016 && t->operand_types[wanted].bitfield.byte
2017 + t->operand_types[wanted].bitfield.word
2018 + t->operand_types[wanted].bitfield.dword
2019 + t->operand_types[wanted].bitfield.qword
2020 > !!t->opcode_modifier.broadcast)
3ac21baa
JB
2021 ? (i.types[given].bitfield.xmmword
2022 || i.types[given].bitfield.ymmword
2023 || i.types[given].bitfield.zmmword)
2024 : !match_simd_size(t, wanted, given))));
5c07affc
L
2025}
2026
3ac21baa
JB
2027/* Return value has MATCH_STRAIGHT set if there is no size conflict on any
2028 operands for instruction template T, and it has MATCH_REVERSE set if there
2029 is no size conflict on any operands for the template with operands reversed
2030 (and the template allows for reversing in the first place). */
5c07affc 2031
3ac21baa
JB
2032#define MATCH_STRAIGHT 1
2033#define MATCH_REVERSE 2
2034
2035static INLINE unsigned int
d3ce72d0 2036operand_size_match (const insn_template *t)
5c07affc 2037{
3ac21baa 2038 unsigned int j, match = MATCH_STRAIGHT;
5c07affc 2039
0cfa3eb3 2040 /* Don't check non-absolute jump instructions. */
5c07affc 2041 if (t->opcode_modifier.jump
0cfa3eb3 2042 && t->opcode_modifier.jump != JUMP_ABSOLUTE)
5c07affc
L
2043 return match;
2044
2045 /* Check memory and accumulator operand size. */
2046 for (j = 0; j < i.operands; j++)
2047 {
3528c362
JB
2048 if (i.types[j].bitfield.class != Reg
2049 && i.types[j].bitfield.class != RegSIMD
601e8564 2050 && t->opcode_modifier.anysize)
5c07affc
L
2051 continue;
2052
bab6aec1 2053 if (t->operand_types[j].bitfield.class == Reg
7a54636a 2054 && !match_operand_size (t, j, j))
5c07affc
L
2055 {
2056 match = 0;
2057 break;
2058 }
2059
3528c362 2060 if (t->operand_types[j].bitfield.class == RegSIMD
3ac21baa 2061 && !match_simd_size (t, j, j))
1b54b8d7
JB
2062 {
2063 match = 0;
2064 break;
2065 }
2066
75e5731b 2067 if (t->operand_types[j].bitfield.instance == Accum
7a54636a 2068 && (!match_operand_size (t, j, j) || !match_simd_size (t, j, j)))
1b54b8d7
JB
2069 {
2070 match = 0;
2071 break;
2072 }
2073
c48dadc9 2074 if ((i.flags[j] & Operand_Mem) && !match_mem_size (t, j, j))
5c07affc
L
2075 {
2076 match = 0;
2077 break;
2078 }
2079 }
2080
3ac21baa 2081 if (!t->opcode_modifier.d)
891edac4 2082 {
dc1e8a47 2083 mismatch:
3ac21baa
JB
2084 if (!match)
2085 i.error = operand_size_mismatch;
2086 return match;
891edac4 2087 }
5c07affc
L
2088
2089 /* Check reverse. */
8bd915b7
JB
2090 gas_assert ((i.operands >= 2 && i.operands <= 3)
2091 || t->opcode_modifier.vexsources);
5c07affc 2092
f5eb1d70 2093 for (j = 0; j < i.operands; j++)
5c07affc 2094 {
f5eb1d70
JB
2095 unsigned int given = i.operands - j - 1;
2096
8bd915b7
JB
2097 /* For 4- and 5-operand insns VEX.W controls just the first two
2098 register operands. */
2099 if (t->opcode_modifier.vexsources)
2100 given = j < 2 ? 1 - j : j;
2101
bab6aec1 2102 if (t->operand_types[j].bitfield.class == Reg
f5eb1d70 2103 && !match_operand_size (t, j, given))
891edac4 2104 goto mismatch;
5c07affc 2105
3528c362 2106 if (t->operand_types[j].bitfield.class == RegSIMD
f5eb1d70 2107 && !match_simd_size (t, j, given))
dbbc8b7e
JB
2108 goto mismatch;
2109
75e5731b 2110 if (t->operand_types[j].bitfield.instance == Accum
f5eb1d70
JB
2111 && (!match_operand_size (t, j, given)
2112 || !match_simd_size (t, j, given)))
dbbc8b7e
JB
2113 goto mismatch;
2114
f5eb1d70 2115 if ((i.flags[given] & Operand_Mem) && !match_mem_size (t, j, given))
891edac4 2116 goto mismatch;
5c07affc
L
2117 }
2118
3ac21baa 2119 return match | MATCH_REVERSE;
5c07affc
L
2120}
2121
c6fb90c8 2122static INLINE int
40fb9820
L
2123operand_type_match (i386_operand_type overlap,
2124 i386_operand_type given)
2125{
2126 i386_operand_type temp = overlap;
2127
7d5e4556 2128 temp.bitfield.unspecified = 0;
5c07affc
L
2129 temp.bitfield.byte = 0;
2130 temp.bitfield.word = 0;
2131 temp.bitfield.dword = 0;
2132 temp.bitfield.fword = 0;
2133 temp.bitfield.qword = 0;
2134 temp.bitfield.tbyte = 0;
2135 temp.bitfield.xmmword = 0;
c0f3af97 2136 temp.bitfield.ymmword = 0;
43234a1e 2137 temp.bitfield.zmmword = 0;
260cd341 2138 temp.bitfield.tmmword = 0;
0dfbf9d7 2139 if (operand_type_all_zero (&temp))
891edac4 2140 goto mismatch;
40fb9820 2141
6f2f06be 2142 if (given.bitfield.baseindex == overlap.bitfield.baseindex)
891edac4
L
2143 return 1;
2144
dc1e8a47 2145 mismatch:
a65babc9 2146 i.error = operand_type_mismatch;
891edac4 2147 return 0;
40fb9820
L
2148}
2149
7d5e4556 2150/* If given types g0 and g1 are registers they must be of the same type
10c17abd 2151 unless the expected operand type register overlap is null.
5de4d9ef 2152 Some Intel syntax memory operand size checking also happens here. */
40fb9820 2153
c6fb90c8 2154static INLINE int
dc821c5f 2155operand_type_register_match (i386_operand_type g0,
40fb9820 2156 i386_operand_type t0,
40fb9820
L
2157 i386_operand_type g1,
2158 i386_operand_type t1)
2159{
bab6aec1 2160 if (g0.bitfield.class != Reg
3528c362 2161 && g0.bitfield.class != RegSIMD
10c17abd
JB
2162 && (!operand_type_check (g0, anymem)
2163 || g0.bitfield.unspecified
5de4d9ef
JB
2164 || (t0.bitfield.class != Reg
2165 && t0.bitfield.class != RegSIMD)))
40fb9820
L
2166 return 1;
2167
bab6aec1 2168 if (g1.bitfield.class != Reg
3528c362 2169 && g1.bitfield.class != RegSIMD
10c17abd
JB
2170 && (!operand_type_check (g1, anymem)
2171 || g1.bitfield.unspecified
5de4d9ef
JB
2172 || (t1.bitfield.class != Reg
2173 && t1.bitfield.class != RegSIMD)))
40fb9820
L
2174 return 1;
2175
dc821c5f
JB
2176 if (g0.bitfield.byte == g1.bitfield.byte
2177 && g0.bitfield.word == g1.bitfield.word
2178 && g0.bitfield.dword == g1.bitfield.dword
10c17abd
JB
2179 && g0.bitfield.qword == g1.bitfield.qword
2180 && g0.bitfield.xmmword == g1.bitfield.xmmword
2181 && g0.bitfield.ymmword == g1.bitfield.ymmword
2182 && g0.bitfield.zmmword == g1.bitfield.zmmword)
40fb9820
L
2183 return 1;
2184
c4d09633
JB
2185 /* If expectations overlap in no more than a single size, all is fine. */
2186 g0 = operand_type_and (t0, t1);
2187 if (g0.bitfield.byte
2188 + g0.bitfield.word
2189 + g0.bitfield.dword
2190 + g0.bitfield.qword
2191 + g0.bitfield.xmmword
2192 + g0.bitfield.ymmword
2193 + g0.bitfield.zmmword <= 1)
891edac4
L
2194 return 1;
2195
a65babc9 2196 i.error = register_type_mismatch;
891edac4
L
2197
2198 return 0;
40fb9820
L
2199}
2200
4c692bc7
JB
2201static INLINE unsigned int
2202register_number (const reg_entry *r)
2203{
2204 unsigned int nr = r->reg_num;
2205
2206 if (r->reg_flags & RegRex)
2207 nr += 8;
2208
200cbe0f
L
2209 if (r->reg_flags & RegVRex)
2210 nr += 16;
2211
4c692bc7
JB
2212 return nr;
2213}
2214
252b5132 2215static INLINE unsigned int
40fb9820 2216mode_from_disp_size (i386_operand_type t)
252b5132 2217{
b5014f7a 2218 if (t.bitfield.disp8)
40fb9820
L
2219 return 1;
2220 else if (t.bitfield.disp16
a775efc8 2221 || t.bitfield.disp32)
40fb9820
L
2222 return 2;
2223 else
2224 return 0;
252b5132
RH
2225}
2226
2227static INLINE int
65879393 2228fits_in_signed_byte (addressT num)
252b5132 2229{
65879393 2230 return num + 0x80 <= 0xff;
47926f60 2231}
252b5132
RH
2232
2233static INLINE int
65879393 2234fits_in_unsigned_byte (addressT num)
252b5132 2235{
65879393 2236 return num <= 0xff;
47926f60 2237}
252b5132
RH
2238
2239static INLINE int
65879393 2240fits_in_unsigned_word (addressT num)
252b5132 2241{
65879393 2242 return num <= 0xffff;
47926f60 2243}
252b5132
RH
2244
2245static INLINE int
65879393 2246fits_in_signed_word (addressT num)
252b5132 2247{
65879393 2248 return num + 0x8000 <= 0xffff;
47926f60 2249}
2a962e6d 2250
3e73aa7c 2251static INLINE int
65879393 2252fits_in_signed_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2253{
2254#ifndef BFD64
2255 return 1;
2256#else
65879393 2257 return num + 0x80000000 <= 0xffffffff;
3e73aa7c
JH
2258#endif
2259} /* fits_in_signed_long() */
2a962e6d 2260
3e73aa7c 2261static INLINE int
65879393 2262fits_in_unsigned_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2263{
2264#ifndef BFD64
2265 return 1;
2266#else
65879393 2267 return num <= 0xffffffff;
3e73aa7c
JH
2268#endif
2269} /* fits_in_unsigned_long() */
252b5132 2270
a442cac5
JB
2271static INLINE valueT extend_to_32bit_address (addressT num)
2272{
2273#ifdef BFD64
2274 if (fits_in_unsigned_long(num))
2275 return (num ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
2276
2277 if (!fits_in_signed_long (num))
2278 return num & 0xffffffff;
2279#endif
2280
2281 return num;
2282}
2283
43234a1e 2284static INLINE int
b5014f7a 2285fits_in_disp8 (offsetT num)
43234a1e
L
2286{
2287 int shift = i.memshift;
2288 unsigned int mask;
2289
2290 if (shift == -1)
2291 abort ();
2292
2293 mask = (1 << shift) - 1;
2294
2295 /* Return 0 if NUM isn't properly aligned. */
2296 if ((num & mask))
2297 return 0;
2298
2299 /* Check if NUM will fit in 8bit after shift. */
2300 return fits_in_signed_byte (num >> shift);
2301}
2302
a683cc34
SP
2303static INLINE int
2304fits_in_imm4 (offsetT num)
2305{
2306 return (num & 0xf) == num;
2307}
2308
40fb9820 2309static i386_operand_type
e3bb37b5 2310smallest_imm_type (offsetT num)
252b5132 2311{
40fb9820 2312 i386_operand_type t;
7ab9ffdd 2313
0dfbf9d7 2314 operand_type_set (&t, 0);
40fb9820
L
2315 t.bitfield.imm64 = 1;
2316
2317 if (cpu_arch_tune != PROCESSOR_I486 && num == 1)
e413e4e9
AM
2318 {
2319 /* This code is disabled on the 486 because all the Imm1 forms
2320 in the opcode table are slower on the i486. They're the
2321 versions with the implicitly specified single-position
2322 displacement, which has another syntax if you really want to
2323 use that form. */
40fb9820
L
2324 t.bitfield.imm1 = 1;
2325 t.bitfield.imm8 = 1;
2326 t.bitfield.imm8s = 1;
2327 t.bitfield.imm16 = 1;
2328 t.bitfield.imm32 = 1;
2329 t.bitfield.imm32s = 1;
2330 }
2331 else if (fits_in_signed_byte (num))
2332 {
2333 t.bitfield.imm8 = 1;
2334 t.bitfield.imm8s = 1;
2335 t.bitfield.imm16 = 1;
2336 t.bitfield.imm32 = 1;
2337 t.bitfield.imm32s = 1;
2338 }
2339 else if (fits_in_unsigned_byte (num))
2340 {
2341 t.bitfield.imm8 = 1;
2342 t.bitfield.imm16 = 1;
2343 t.bitfield.imm32 = 1;
2344 t.bitfield.imm32s = 1;
2345 }
2346 else if (fits_in_signed_word (num) || fits_in_unsigned_word (num))
2347 {
2348 t.bitfield.imm16 = 1;
2349 t.bitfield.imm32 = 1;
2350 t.bitfield.imm32s = 1;
2351 }
2352 else if (fits_in_signed_long (num))
2353 {
2354 t.bitfield.imm32 = 1;
2355 t.bitfield.imm32s = 1;
2356 }
2357 else if (fits_in_unsigned_long (num))
2358 t.bitfield.imm32 = 1;
2359
2360 return t;
47926f60 2361}
252b5132 2362
847f7ad4 2363static offsetT
e3bb37b5 2364offset_in_range (offsetT val, int size)
847f7ad4 2365{
508866be 2366 addressT mask;
ba2adb93 2367
847f7ad4
AM
2368 switch (size)
2369 {
508866be
L
2370 case 1: mask = ((addressT) 1 << 8) - 1; break;
2371 case 2: mask = ((addressT) 1 << 16) - 1; break;
3e73aa7c 2372#ifdef BFD64
64965897 2373 case 4: mask = ((addressT) 1 << 32) - 1; break;
3e73aa7c 2374#endif
64965897 2375 case sizeof (val): return val;
47926f60 2376 default: abort ();
847f7ad4
AM
2377 }
2378
4fe51f7d 2379 if ((val & ~mask) != 0 && (-val & ~mask) != 0)
f493c217
AM
2380 as_warn (_("0x%" PRIx64 " shortened to 0x%" PRIx64),
2381 (uint64_t) val, (uint64_t) (val & mask));
847f7ad4 2382
847f7ad4
AM
2383 return val & mask;
2384}
2385
c32fa91d
L
2386enum PREFIX_GROUP
2387{
2388 PREFIX_EXIST = 0,
2389 PREFIX_LOCK,
2390 PREFIX_REP,
04ef582a 2391 PREFIX_DS,
c32fa91d
L
2392 PREFIX_OTHER
2393};
2394
2395/* Returns
2396 a. PREFIX_EXIST if attempting to add a prefix where one from the
2397 same class already exists.
2398 b. PREFIX_LOCK if lock prefix is added.
2399 c. PREFIX_REP if rep/repne prefix is added.
04ef582a
L
2400 d. PREFIX_DS if ds prefix is added.
2401 e. PREFIX_OTHER if other prefix is added.
c32fa91d
L
2402 */
2403
2404static enum PREFIX_GROUP
e3bb37b5 2405add_prefix (unsigned int prefix)
252b5132 2406{
c32fa91d 2407 enum PREFIX_GROUP ret = PREFIX_OTHER;
b1905489 2408 unsigned int q;
252b5132 2409
29b0f896
AM
2410 if (prefix >= REX_OPCODE && prefix < REX_OPCODE + 16
2411 && flag_code == CODE_64BIT)
b1905489 2412 {
161a04f6 2413 if ((i.prefix[REX_PREFIX] & prefix & REX_W)
44846f29
JB
2414 || (i.prefix[REX_PREFIX] & prefix & REX_R)
2415 || (i.prefix[REX_PREFIX] & prefix & REX_X)
2416 || (i.prefix[REX_PREFIX] & prefix & REX_B))
c32fa91d 2417 ret = PREFIX_EXIST;
b1905489
JB
2418 q = REX_PREFIX;
2419 }
3e73aa7c 2420 else
b1905489
JB
2421 {
2422 switch (prefix)
2423 {
2424 default:
2425 abort ();
2426
b1905489 2427 case DS_PREFIX_OPCODE:
04ef582a
L
2428 ret = PREFIX_DS;
2429 /* Fall through. */
2430 case CS_PREFIX_OPCODE:
b1905489
JB
2431 case ES_PREFIX_OPCODE:
2432 case FS_PREFIX_OPCODE:
2433 case GS_PREFIX_OPCODE:
2434 case SS_PREFIX_OPCODE:
2435 q = SEG_PREFIX;
2436 break;
2437
2438 case REPNE_PREFIX_OPCODE:
2439 case REPE_PREFIX_OPCODE:
c32fa91d
L
2440 q = REP_PREFIX;
2441 ret = PREFIX_REP;
2442 break;
2443
b1905489 2444 case LOCK_PREFIX_OPCODE:
c32fa91d
L
2445 q = LOCK_PREFIX;
2446 ret = PREFIX_LOCK;
b1905489
JB
2447 break;
2448
2449 case FWAIT_OPCODE:
2450 q = WAIT_PREFIX;
2451 break;
2452
2453 case ADDR_PREFIX_OPCODE:
2454 q = ADDR_PREFIX;
2455 break;
2456
2457 case DATA_PREFIX_OPCODE:
2458 q = DATA_PREFIX;
2459 break;
2460 }
2461 if (i.prefix[q] != 0)
c32fa91d 2462 ret = PREFIX_EXIST;
b1905489 2463 }
252b5132 2464
b1905489 2465 if (ret)
252b5132 2466 {
b1905489
JB
2467 if (!i.prefix[q])
2468 ++i.prefixes;
2469 i.prefix[q] |= prefix;
252b5132 2470 }
b1905489
JB
2471 else
2472 as_bad (_("same type of prefix used twice"));
252b5132 2473
252b5132
RH
2474 return ret;
2475}
2476
2477static void
78f12dd3 2478update_code_flag (int value, int check)
eecb386c 2479{
78f12dd3
L
2480 PRINTF_LIKE ((*as_error));
2481
1e9cc1c2 2482 flag_code = (enum flag_code) value;
40fb9820
L
2483 if (flag_code == CODE_64BIT)
2484 {
2485 cpu_arch_flags.bitfield.cpu64 = 1;
2486 cpu_arch_flags.bitfield.cpuno64 = 0;
40fb9820
L
2487 }
2488 else
2489 {
2490 cpu_arch_flags.bitfield.cpu64 = 0;
2491 cpu_arch_flags.bitfield.cpuno64 = 1;
40fb9820
L
2492 }
2493 if (value == CODE_64BIT && !cpu_arch_flags.bitfield.cpulm )
3e73aa7c 2494 {
78f12dd3
L
2495 if (check)
2496 as_error = as_fatal;
2497 else
2498 as_error = as_bad;
2499 (*as_error) (_("64bit mode not supported on `%s'."),
2500 cpu_arch_name ? cpu_arch_name : default_arch);
3e73aa7c 2501 }
40fb9820 2502 if (value == CODE_32BIT && !cpu_arch_flags.bitfield.cpui386)
3e73aa7c 2503 {
78f12dd3
L
2504 if (check)
2505 as_error = as_fatal;
2506 else
2507 as_error = as_bad;
2508 (*as_error) (_("32bit mode not supported on `%s'."),
2509 cpu_arch_name ? cpu_arch_name : default_arch);
3e73aa7c 2510 }
eecb386c
AM
2511 stackop_size = '\0';
2512}
2513
78f12dd3
L
2514static void
2515set_code_flag (int value)
2516{
2517 update_code_flag (value, 0);
2518}
2519
eecb386c 2520static void
e3bb37b5 2521set_16bit_gcc_code_flag (int new_code_flag)
252b5132 2522{
1e9cc1c2 2523 flag_code = (enum flag_code) new_code_flag;
40fb9820
L
2524 if (flag_code != CODE_16BIT)
2525 abort ();
2526 cpu_arch_flags.bitfield.cpu64 = 0;
2527 cpu_arch_flags.bitfield.cpuno64 = 1;
9306ca4a 2528 stackop_size = LONG_MNEM_SUFFIX;
252b5132
RH
2529}
2530
2531static void
e3bb37b5 2532set_intel_syntax (int syntax_flag)
252b5132
RH
2533{
2534 /* Find out if register prefixing is specified. */
2535 int ask_naked_reg = 0;
2536
2537 SKIP_WHITESPACE ();
29b0f896 2538 if (!is_end_of_line[(unsigned char) *input_line_pointer])
252b5132 2539 {
d02603dc
NC
2540 char *string;
2541 int e = get_symbol_name (&string);
252b5132 2542
47926f60 2543 if (strcmp (string, "prefix") == 0)
252b5132 2544 ask_naked_reg = 1;
47926f60 2545 else if (strcmp (string, "noprefix") == 0)
252b5132
RH
2546 ask_naked_reg = -1;
2547 else
d0b47220 2548 as_bad (_("bad argument to syntax directive."));
d02603dc 2549 (void) restore_line_pointer (e);
252b5132
RH
2550 }
2551 demand_empty_rest_of_line ();
c3332e24 2552
252b5132
RH
2553 intel_syntax = syntax_flag;
2554
2555 if (ask_naked_reg == 0)
f86103b7
AM
2556 allow_naked_reg = (intel_syntax
2557 && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
252b5132
RH
2558 else
2559 allow_naked_reg = (ask_naked_reg < 0);
9306ca4a 2560
ee86248c 2561 expr_set_rank (O_full_ptr, syntax_flag ? 10 : 0);
7ab9ffdd 2562
e4a3b5a4 2563 identifier_chars['%'] = intel_syntax && allow_naked_reg ? '%' : 0;
9306ca4a 2564 identifier_chars['$'] = intel_syntax ? '$' : 0;
e4a3b5a4 2565 register_prefix = allow_naked_reg ? "" : "%";
252b5132
RH
2566}
2567
1efbbeb4
L
2568static void
2569set_intel_mnemonic (int mnemonic_flag)
2570{
e1d4d893 2571 intel_mnemonic = mnemonic_flag;
1efbbeb4
L
2572}
2573
db51cc60
L
2574static void
2575set_allow_index_reg (int flag)
2576{
2577 allow_index_reg = flag;
2578}
2579
cb19c032 2580static void
7bab8ab5 2581set_check (int what)
cb19c032 2582{
7bab8ab5
JB
2583 enum check_kind *kind;
2584 const char *str;
2585
2586 if (what)
2587 {
2588 kind = &operand_check;
2589 str = "operand";
2590 }
2591 else
2592 {
2593 kind = &sse_check;
2594 str = "sse";
2595 }
2596
cb19c032
L
2597 SKIP_WHITESPACE ();
2598
2599 if (!is_end_of_line[(unsigned char) *input_line_pointer])
2600 {
d02603dc
NC
2601 char *string;
2602 int e = get_symbol_name (&string);
cb19c032
L
2603
2604 if (strcmp (string, "none") == 0)
7bab8ab5 2605 *kind = check_none;
cb19c032 2606 else if (strcmp (string, "warning") == 0)
7bab8ab5 2607 *kind = check_warning;
cb19c032 2608 else if (strcmp (string, "error") == 0)
7bab8ab5 2609 *kind = check_error;
cb19c032 2610 else
7bab8ab5 2611 as_bad (_("bad argument to %s_check directive."), str);
d02603dc 2612 (void) restore_line_pointer (e);
cb19c032
L
2613 }
2614 else
7bab8ab5 2615 as_bad (_("missing argument for %s_check directive"), str);
cb19c032
L
2616
2617 demand_empty_rest_of_line ();
2618}
2619
8a9036a4
L
2620static void
2621check_cpu_arch_compatible (const char *name ATTRIBUTE_UNUSED,
1e9cc1c2 2622 i386_cpu_flags new_flag ATTRIBUTE_UNUSED)
8a9036a4
L
2623{
2624#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2625 static const char *arch;
2626
c085ab00 2627 /* Intel MCU is only supported on ELF. */
8a9036a4
L
2628 if (!IS_ELF)
2629 return;
2630
2631 if (!arch)
2632 {
2633 /* Use cpu_arch_name if it is set in md_parse_option. Otherwise
2634 use default_arch. */
2635 arch = cpu_arch_name;
2636 if (!arch)
2637 arch = default_arch;
2638 }
2639
81486035 2640 /* If we are targeting Intel MCU, we must enable it. */
648d04db
JB
2641 if ((get_elf_backend_data (stdoutput)->elf_machine_code == EM_IAMCU)
2642 == new_flag.bitfield.cpuiamcu)
81486035
L
2643 return;
2644
8a9036a4
L
2645 as_bad (_("`%s' is not supported on `%s'"), name, arch);
2646#endif
2647}
2648
8180707f
JB
2649static void
2650extend_cpu_sub_arch_name (const char *name)
2651{
2652 if (cpu_sub_arch_name)
2653 cpu_sub_arch_name = reconcat (cpu_sub_arch_name, cpu_sub_arch_name,
ae89daec 2654 ".", name, (const char *) NULL);
8180707f 2655 else
ae89daec 2656 cpu_sub_arch_name = concat (".", name, (const char *) NULL);
8180707f
JB
2657}
2658
e413e4e9 2659static void
e3bb37b5 2660set_cpu_arch (int dummy ATTRIBUTE_UNUSED)
e413e4e9 2661{
f68697e8
JB
2662 typedef struct arch_stack_entry
2663 {
2664 const struct arch_stack_entry *prev;
2665 const char *name;
2666 char *sub_name;
2667 i386_cpu_flags flags;
2668 i386_cpu_flags isa_flags;
2669 enum processor_type isa;
2670 enum flag_code flag_code;
2671 char stackop_size;
2672 bool no_cond_jump_promotion;
2673 } arch_stack_entry;
2674 static const arch_stack_entry *arch_stack_top;
2675
47926f60 2676 SKIP_WHITESPACE ();
e413e4e9 2677
29b0f896 2678 if (!is_end_of_line[(unsigned char) *input_line_pointer])
e413e4e9 2679 {
3ce2ebcf
JB
2680 char *s;
2681 int e = get_symbol_name (&s);
2682 const char *string = s;
2683 unsigned int j = 0;
40fb9820 2684 i386_cpu_flags flags;
e413e4e9 2685
3ce2ebcf
JB
2686 if (strcmp (string, "default") == 0)
2687 {
2688 if (strcmp (default_arch, "iamcu") == 0)
2689 string = default_arch;
2690 else
2691 {
2692 static const i386_cpu_flags cpu_unknown_flags = CPU_UNKNOWN_FLAGS;
2693
2694 cpu_arch_name = NULL;
2695 free (cpu_sub_arch_name);
2696 cpu_sub_arch_name = NULL;
2697 cpu_arch_flags = cpu_unknown_flags;
2698 if (flag_code == CODE_64BIT)
2699 {
2700 cpu_arch_flags.bitfield.cpu64 = 1;
2701 cpu_arch_flags.bitfield.cpuno64 = 0;
2702 }
2703 else
2704 {
2705 cpu_arch_flags.bitfield.cpu64 = 0;
2706 cpu_arch_flags.bitfield.cpuno64 = 1;
2707 }
2708 cpu_arch_isa = PROCESSOR_UNKNOWN;
ae89daec 2709 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
3ce2ebcf
JB
2710 if (!cpu_arch_tune_set)
2711 {
2712 cpu_arch_tune = cpu_arch_isa;
2713 cpu_arch_tune_flags = cpu_arch_isa_flags;
2714 }
2715
2716 j = ARRAY_SIZE (cpu_arch) + 1;
2717 }
2718 }
f68697e8
JB
2719 else if (strcmp (string, "push") == 0)
2720 {
2721 arch_stack_entry *top = XNEW (arch_stack_entry);
2722
2723 top->name = cpu_arch_name;
2724 if (cpu_sub_arch_name)
2725 top->sub_name = xstrdup (cpu_sub_arch_name);
2726 else
2727 top->sub_name = NULL;
2728 top->flags = cpu_arch_flags;
2729 top->isa = cpu_arch_isa;
2730 top->isa_flags = cpu_arch_isa_flags;
2731 top->flag_code = flag_code;
2732 top->stackop_size = stackop_size;
2733 top->no_cond_jump_promotion = no_cond_jump_promotion;
2734
2735 top->prev = arch_stack_top;
2736 arch_stack_top = top;
2737
2738 (void) restore_line_pointer (e);
2739 demand_empty_rest_of_line ();
2740 return;
2741 }
2742 else if (strcmp (string, "pop") == 0)
2743 {
2744 const arch_stack_entry *top = arch_stack_top;
2745
2746 if (!top)
2747 as_bad (_(".arch stack is empty"));
2748 else if (top->flag_code != flag_code
2749 || top->stackop_size != stackop_size)
2750 {
2751 static const unsigned int bits[] = {
2752 [CODE_16BIT] = 16,
2753 [CODE_32BIT] = 32,
2754 [CODE_64BIT] = 64,
2755 };
2756
2757 as_bad (_("this `.arch pop' requires `.code%u%s' to be in effect"),
2758 bits[top->flag_code],
2759 top->stackop_size == LONG_MNEM_SUFFIX ? "gcc" : "");
2760 }
2761 else
2762 {
2763 arch_stack_top = top->prev;
2764
2765 cpu_arch_name = top->name;
2766 free (cpu_sub_arch_name);
2767 cpu_sub_arch_name = top->sub_name;
2768 cpu_arch_flags = top->flags;
2769 cpu_arch_isa = top->isa;
2770 cpu_arch_isa_flags = top->isa_flags;
2771 no_cond_jump_promotion = top->no_cond_jump_promotion;
2772
2773 XDELETE (top);
2774 }
2775
2776 (void) restore_line_pointer (e);
2777 demand_empty_rest_of_line ();
2778 return;
2779 }
3ce2ebcf
JB
2780
2781 for (; j < ARRAY_SIZE (cpu_arch); j++)
e413e4e9 2782 {
ae89daec
JB
2783 if (strcmp (string + (*string == '.'), cpu_arch[j].name) == 0
2784 && (*string == '.') == (cpu_arch[j].type == PROCESSOR_NONE))
e413e4e9 2785 {
5c6af06e
JB
2786 if (*string != '.')
2787 {
ae89daec 2788 check_cpu_arch_compatible (string, cpu_arch[j].enable);
648d04db 2789
91d6fa6a 2790 cpu_arch_name = cpu_arch[j].name;
d92c7521 2791 free (cpu_sub_arch_name);
5c6af06e 2792 cpu_sub_arch_name = NULL;
ae89daec 2793 cpu_arch_flags = cpu_arch[j].enable;
40fb9820
L
2794 if (flag_code == CODE_64BIT)
2795 {
2796 cpu_arch_flags.bitfield.cpu64 = 1;
2797 cpu_arch_flags.bitfield.cpuno64 = 0;
2798 }
2799 else
2800 {
2801 cpu_arch_flags.bitfield.cpu64 = 0;
2802 cpu_arch_flags.bitfield.cpuno64 = 1;
2803 }
91d6fa6a 2804 cpu_arch_isa = cpu_arch[j].type;
ae89daec 2805 cpu_arch_isa_flags = cpu_arch[j].enable;
ccc9c027
L
2806 if (!cpu_arch_tune_set)
2807 {
2808 cpu_arch_tune = cpu_arch_isa;
2809 cpu_arch_tune_flags = cpu_arch_isa_flags;
2810 }
5c6af06e
JB
2811 break;
2812 }
40fb9820 2813
ae89daec
JB
2814 if (cpu_flags_all_zero (&cpu_arch[j].enable))
2815 continue;
2816
293f5f65 2817 flags = cpu_flags_or (cpu_arch_flags,
ae89daec 2818 cpu_arch[j].enable);
81486035 2819
5b64d091 2820 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
5c6af06e 2821 {
ae89daec 2822 extend_cpu_sub_arch_name (string + 1);
40fb9820 2823 cpu_arch_flags = flags;
a586129e 2824 cpu_arch_isa_flags = flags;
5c6af06e 2825 }
0089dace
L
2826 else
2827 cpu_arch_isa_flags
2828 = cpu_flags_or (cpu_arch_isa_flags,
ae89daec 2829 cpu_arch[j].enable);
d02603dc 2830 (void) restore_line_pointer (e);
5c6af06e
JB
2831 demand_empty_rest_of_line ();
2832 return;
e413e4e9
AM
2833 }
2834 }
293f5f65 2835
ae89daec 2836 if (startswith (string, ".no") && j >= ARRAY_SIZE (cpu_arch))
293f5f65 2837 {
33eaf5de 2838 /* Disable an ISA extension. */
ae89daec
JB
2839 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
2840 if (cpu_arch[j].type == PROCESSOR_NONE
2841 && strcmp (string + 3, cpu_arch[j].name) == 0)
293f5f65
L
2842 {
2843 flags = cpu_flags_and_not (cpu_arch_flags,
ae89daec 2844 cpu_arch[j].disable);
293f5f65
L
2845 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
2846 {
ae89daec 2847 extend_cpu_sub_arch_name (string + 1);
293f5f65
L
2848 cpu_arch_flags = flags;
2849 cpu_arch_isa_flags = flags;
2850 }
2851 (void) restore_line_pointer (e);
2852 demand_empty_rest_of_line ();
2853 return;
2854 }
293f5f65
L
2855 }
2856
3ce2ebcf 2857 if (j == ARRAY_SIZE (cpu_arch))
e413e4e9
AM
2858 as_bad (_("no such architecture: `%s'"), string);
2859
2860 *input_line_pointer = e;
2861 }
2862 else
2863 as_bad (_("missing cpu architecture"));
2864
fddf5b5b
AM
2865 no_cond_jump_promotion = 0;
2866 if (*input_line_pointer == ','
29b0f896 2867 && !is_end_of_line[(unsigned char) input_line_pointer[1]])
fddf5b5b 2868 {
d02603dc
NC
2869 char *string;
2870 char e;
2871
2872 ++input_line_pointer;
2873 e = get_symbol_name (&string);
fddf5b5b
AM
2874
2875 if (strcmp (string, "nojumps") == 0)
2876 no_cond_jump_promotion = 1;
2877 else if (strcmp (string, "jumps") == 0)
2878 ;
2879 else
2880 as_bad (_("no such architecture modifier: `%s'"), string);
2881
d02603dc 2882 (void) restore_line_pointer (e);
fddf5b5b
AM
2883 }
2884
e413e4e9
AM
2885 demand_empty_rest_of_line ();
2886}
2887
8a9036a4
L
2888enum bfd_architecture
2889i386_arch (void)
2890{
c085ab00 2891 if (cpu_arch_isa == PROCESSOR_IAMCU)
81486035
L
2892 {
2893 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2894 || flag_code == CODE_64BIT)
2895 as_fatal (_("Intel MCU is 32bit ELF only"));
2896 return bfd_arch_iamcu;
2897 }
8a9036a4
L
2898 else
2899 return bfd_arch_i386;
2900}
2901
b9d79e03 2902unsigned long
7016a5d5 2903i386_mach (void)
b9d79e03 2904{
d34049e8 2905 if (startswith (default_arch, "x86_64"))
8a9036a4 2906 {
c085ab00 2907 if (default_arch[6] == '\0')
8a9036a4 2908 return bfd_mach_x86_64;
351f65ca
L
2909 else
2910 return bfd_mach_x64_32;
8a9036a4 2911 }
5197d474
L
2912 else if (!strcmp (default_arch, "i386")
2913 || !strcmp (default_arch, "iamcu"))
81486035
L
2914 {
2915 if (cpu_arch_isa == PROCESSOR_IAMCU)
2916 {
2917 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
2918 as_fatal (_("Intel MCU is 32bit ELF only"));
2919 return bfd_mach_i386_iamcu;
2920 }
2921 else
2922 return bfd_mach_i386_i386;
2923 }
b9d79e03 2924 else
2b5d6a91 2925 as_fatal (_("unknown architecture"));
b9d79e03 2926}
b9d79e03 2927\f
252b5132 2928void
7016a5d5 2929md_begin (void)
252b5132 2930{
86fa6981
L
2931 /* Support pseudo prefixes like {disp32}. */
2932 lex_type ['{'] = LEX_BEGIN_NAME;
2933
47926f60 2934 /* Initialize op_hash hash table. */
629310ab 2935 op_hash = str_htab_create ();
252b5132
RH
2936
2937 {
d3ce72d0 2938 const insn_template *optab;
29b0f896 2939 templates *core_optab;
252b5132 2940
47926f60
KH
2941 /* Setup for loop. */
2942 optab = i386_optab;
654d6f31 2943 core_optab = notes_alloc (sizeof (*core_optab));
252b5132
RH
2944 core_optab->start = optab;
2945
2946 while (1)
2947 {
2948 ++optab;
2949 if (optab->name == NULL
2950 || strcmp (optab->name, (optab - 1)->name) != 0)
2951 {
2952 /* different name --> ship out current template list;
47926f60 2953 add to hash table; & begin anew. */
252b5132 2954 core_optab->end = optab;
fe0e921f
AM
2955 if (str_hash_insert (op_hash, (optab - 1)->name, core_optab, 0))
2956 as_fatal (_("duplicate %s"), (optab - 1)->name);
2957
252b5132
RH
2958 if (optab->name == NULL)
2959 break;
654d6f31 2960 core_optab = notes_alloc (sizeof (*core_optab));
252b5132
RH
2961 core_optab->start = optab;
2962 }
2963 }
2964 }
2965
47926f60 2966 /* Initialize reg_hash hash table. */
629310ab 2967 reg_hash = str_htab_create ();
252b5132 2968 {
29b0f896 2969 const reg_entry *regtab;
c3fe08fa 2970 unsigned int regtab_size = i386_regtab_size;
252b5132 2971
c3fe08fa 2972 for (regtab = i386_regtab; regtab_size--; regtab++)
6225c532 2973 {
6288d05f
JB
2974 switch (regtab->reg_type.bitfield.class)
2975 {
2976 case Reg:
34684862
JB
2977 if (regtab->reg_type.bitfield.dword)
2978 {
2979 if (regtab->reg_type.bitfield.instance == Accum)
2980 reg_eax = regtab;
2981 }
2982 else if (regtab->reg_type.bitfield.tbyte)
6288d05f
JB
2983 {
2984 /* There's no point inserting st(<N>) in the hash table, as
2985 parentheses aren't included in register_chars[] anyway. */
2986 if (regtab->reg_type.bitfield.instance != Accum)
2987 continue;
2988 reg_st0 = regtab;
2989 }
2990 break;
2991
5e042380
JB
2992 case SReg:
2993 switch (regtab->reg_num)
2994 {
2995 case 0: reg_es = regtab; break;
2996 case 2: reg_ss = regtab; break;
2997 case 3: reg_ds = regtab; break;
2998 }
2999 break;
3000
6288d05f
JB
3001 case RegMask:
3002 if (!regtab->reg_num)
3003 reg_k0 = regtab;
3004 break;
3005 }
3006
6225c532
JB
3007 if (str_hash_insert (reg_hash, regtab->reg_name, regtab, 0) != NULL)
3008 as_fatal (_("duplicate %s"), regtab->reg_name);
6225c532 3009 }
252b5132
RH
3010 }
3011
47926f60 3012 /* Fill in lexical tables: mnemonic_chars, operand_chars. */
252b5132 3013 {
29b0f896
AM
3014 int c;
3015 char *p;
252b5132
RH
3016
3017 for (c = 0; c < 256; c++)
3018 {
014fbcda 3019 if (ISDIGIT (c) || ISLOWER (c))
252b5132
RH
3020 {
3021 mnemonic_chars[c] = c;
3022 register_chars[c] = c;
3023 operand_chars[c] = c;
3024 }
3882b010 3025 else if (ISUPPER (c))
252b5132 3026 {
3882b010 3027 mnemonic_chars[c] = TOLOWER (c);
252b5132
RH
3028 register_chars[c] = mnemonic_chars[c];
3029 operand_chars[c] = c;
3030 }
43234a1e 3031 else if (c == '{' || c == '}')
86fa6981
L
3032 {
3033 mnemonic_chars[c] = c;
3034 operand_chars[c] = c;
3035 }
b3983e5f
JB
3036#ifdef SVR4_COMMENT_CHARS
3037 else if (c == '\\' && strchr (i386_comment_chars, '/'))
3038 operand_chars[c] = c;
3039#endif
252b5132 3040
3882b010 3041 if (ISALPHA (c) || ISDIGIT (c))
252b5132
RH
3042 identifier_chars[c] = c;
3043 else if (c >= 128)
3044 {
3045 identifier_chars[c] = c;
3046 operand_chars[c] = c;
3047 }
3048 }
3049
3050#ifdef LEX_AT
3051 identifier_chars['@'] = '@';
32137342
NC
3052#endif
3053#ifdef LEX_QM
3054 identifier_chars['?'] = '?';
3055 operand_chars['?'] = '?';
252b5132 3056#endif
c0f3af97 3057 mnemonic_chars['_'] = '_';
791fe849 3058 mnemonic_chars['-'] = '-';
0003779b 3059 mnemonic_chars['.'] = '.';
252b5132
RH
3060 identifier_chars['_'] = '_';
3061 identifier_chars['.'] = '.';
3062
3063 for (p = operand_special_chars; *p != '\0'; p++)
3064 operand_chars[(unsigned char) *p] = *p;
3065 }
3066
a4447b93
RH
3067 if (flag_code == CODE_64BIT)
3068 {
ca19b261
KT
3069#if defined (OBJ_COFF) && defined (TE_PE)
3070 x86_dwarf2_return_column = (OUTPUT_FLAVOR == bfd_target_coff_flavour
3071 ? 32 : 16);
3072#else
a4447b93 3073 x86_dwarf2_return_column = 16;
ca19b261 3074#endif
61ff971f 3075 x86_cie_data_alignment = -8;
a4447b93
RH
3076 }
3077 else
3078 {
3079 x86_dwarf2_return_column = 8;
3080 x86_cie_data_alignment = -4;
3081 }
e379e5f3
L
3082
3083 /* NB: FUSED_JCC_PADDING frag must have sufficient room so that it
3084 can be turned into BRANCH_PREFIX frag. */
3085 if (align_branch_prefix_size > MAX_FUSED_JCC_PADDING_SIZE)
3086 abort ();
252b5132
RH
3087}
3088
3089void
e3bb37b5 3090i386_print_statistics (FILE *file)
252b5132 3091{
629310ab
ML
3092 htab_print_statistics (file, "i386 opcode", op_hash);
3093 htab_print_statistics (file, "i386 register", reg_hash);
252b5132 3094}
654d6f31
AM
3095
3096void
3097i386_md_end (void)
3098{
3099 htab_delete (op_hash);
3100 htab_delete (reg_hash);
3101}
252b5132 3102\f
252b5132
RH
3103#ifdef DEBUG386
3104
ce8a8b2f 3105/* Debugging routines for md_assemble. */
d3ce72d0 3106static void pte (insn_template *);
40fb9820 3107static void pt (i386_operand_type);
e3bb37b5
L
3108static void pe (expressionS *);
3109static void ps (symbolS *);
252b5132
RH
3110
3111static void
2c703856 3112pi (const char *line, i386_insn *x)
252b5132 3113{
09137c09 3114 unsigned int j;
252b5132
RH
3115
3116 fprintf (stdout, "%s: template ", line);
3117 pte (&x->tm);
09f131f2
JH
3118 fprintf (stdout, " address: base %s index %s scale %x\n",
3119 x->base_reg ? x->base_reg->reg_name : "none",
3120 x->index_reg ? x->index_reg->reg_name : "none",
3121 x->log2_scale_factor);
3122 fprintf (stdout, " modrm: mode %x reg %x reg/mem %x\n",
252b5132 3123 x->rm.mode, x->rm.reg, x->rm.regmem);
09f131f2
JH
3124 fprintf (stdout, " sib: base %x index %x scale %x\n",
3125 x->sib.base, x->sib.index, x->sib.scale);
3126 fprintf (stdout, " rex: 64bit %x extX %x extY %x extZ %x\n",
161a04f6
L
3127 (x->rex & REX_W) != 0,
3128 (x->rex & REX_R) != 0,
3129 (x->rex & REX_X) != 0,
3130 (x->rex & REX_B) != 0);
09137c09 3131 for (j = 0; j < x->operands; j++)
252b5132 3132 {
09137c09
SP
3133 fprintf (stdout, " #%d: ", j + 1);
3134 pt (x->types[j]);
252b5132 3135 fprintf (stdout, "\n");
bab6aec1 3136 if (x->types[j].bitfield.class == Reg
3528c362
JB
3137 || x->types[j].bitfield.class == RegMMX
3138 || x->types[j].bitfield.class == RegSIMD
dd6b8a0b 3139 || x->types[j].bitfield.class == RegMask
00cee14f 3140 || x->types[j].bitfield.class == SReg
4a5c67ed
JB
3141 || x->types[j].bitfield.class == RegCR
3142 || x->types[j].bitfield.class == RegDR
dd6b8a0b
JB
3143 || x->types[j].bitfield.class == RegTR
3144 || x->types[j].bitfield.class == RegBND)
09137c09
SP
3145 fprintf (stdout, "%s\n", x->op[j].regs->reg_name);
3146 if (operand_type_check (x->types[j], imm))
3147 pe (x->op[j].imms);
3148 if (operand_type_check (x->types[j], disp))
3149 pe (x->op[j].disps);
252b5132
RH
3150 }
3151}
3152
3153static void
d3ce72d0 3154pte (insn_template *t)
252b5132 3155{
b933fa4b 3156 static const unsigned char opc_pfx[] = { 0, 0x66, 0xf3, 0xf2 };
441f6aca 3157 static const char *const opc_spc[] = {
0cc78721 3158 NULL, "0f", "0f38", "0f3a", NULL, "evexmap5", "evexmap6", NULL,
441f6aca
JB
3159 "XOP08", "XOP09", "XOP0A",
3160 };
09137c09 3161 unsigned int j;
441f6aca 3162
252b5132 3163 fprintf (stdout, " %d operands ", t->operands);
441f6aca
JB
3164 if (opc_pfx[t->opcode_modifier.opcodeprefix])
3165 fprintf (stdout, "pfx %x ", opc_pfx[t->opcode_modifier.opcodeprefix]);
3166 if (opc_spc[t->opcode_modifier.opcodespace])
3167 fprintf (stdout, "space %s ", opc_spc[t->opcode_modifier.opcodespace]);
47926f60 3168 fprintf (stdout, "opcode %x ", t->base_opcode);
252b5132
RH
3169 if (t->extension_opcode != None)
3170 fprintf (stdout, "ext %x ", t->extension_opcode);
40fb9820 3171 if (t->opcode_modifier.d)
252b5132 3172 fprintf (stdout, "D");
40fb9820 3173 if (t->opcode_modifier.w)
252b5132
RH
3174 fprintf (stdout, "W");
3175 fprintf (stdout, "\n");
09137c09 3176 for (j = 0; j < t->operands; j++)
252b5132 3177 {
09137c09
SP
3178 fprintf (stdout, " #%d type ", j + 1);
3179 pt (t->operand_types[j]);
252b5132
RH
3180 fprintf (stdout, "\n");
3181 }
3182}
3183
3184static void
e3bb37b5 3185pe (expressionS *e)
252b5132 3186{
24eab124 3187 fprintf (stdout, " operation %d\n", e->X_op);
7b025ee8
JB
3188 fprintf (stdout, " add_number %" BFD_VMA_FMT "d (%" BFD_VMA_FMT "x)\n",
3189 e->X_add_number, e->X_add_number);
252b5132
RH
3190 if (e->X_add_symbol)
3191 {
3192 fprintf (stdout, " add_symbol ");
3193 ps (e->X_add_symbol);
3194 fprintf (stdout, "\n");
3195 }
3196 if (e->X_op_symbol)
3197 {
3198 fprintf (stdout, " op_symbol ");
3199 ps (e->X_op_symbol);
3200 fprintf (stdout, "\n");
3201 }
3202}
3203
3204static void
e3bb37b5 3205ps (symbolS *s)
252b5132
RH
3206{
3207 fprintf (stdout, "%s type %s%s",
3208 S_GET_NAME (s),
3209 S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
3210 segment_name (S_GET_SEGMENT (s)));
3211}
3212
7b81dfbb 3213static struct type_name
252b5132 3214 {
40fb9820
L
3215 i386_operand_type mask;
3216 const char *name;
252b5132 3217 }
7b81dfbb 3218const type_names[] =
252b5132 3219{
40fb9820
L
3220 { OPERAND_TYPE_REG8, "r8" },
3221 { OPERAND_TYPE_REG16, "r16" },
3222 { OPERAND_TYPE_REG32, "r32" },
3223 { OPERAND_TYPE_REG64, "r64" },
2c703856
JB
3224 { OPERAND_TYPE_ACC8, "acc8" },
3225 { OPERAND_TYPE_ACC16, "acc16" },
3226 { OPERAND_TYPE_ACC32, "acc32" },
3227 { OPERAND_TYPE_ACC64, "acc64" },
40fb9820
L
3228 { OPERAND_TYPE_IMM8, "i8" },
3229 { OPERAND_TYPE_IMM8, "i8s" },
3230 { OPERAND_TYPE_IMM16, "i16" },
3231 { OPERAND_TYPE_IMM32, "i32" },
3232 { OPERAND_TYPE_IMM32S, "i32s" },
3233 { OPERAND_TYPE_IMM64, "i64" },
3234 { OPERAND_TYPE_IMM1, "i1" },
3235 { OPERAND_TYPE_BASEINDEX, "BaseIndex" },
3236 { OPERAND_TYPE_DISP8, "d8" },
3237 { OPERAND_TYPE_DISP16, "d16" },
3238 { OPERAND_TYPE_DISP32, "d32" },
40fb9820
L
3239 { OPERAND_TYPE_DISP64, "d64" },
3240 { OPERAND_TYPE_INOUTPORTREG, "InOutPortReg" },
3241 { OPERAND_TYPE_SHIFTCOUNT, "ShiftCount" },
3242 { OPERAND_TYPE_CONTROL, "control reg" },
3243 { OPERAND_TYPE_TEST, "test reg" },
3244 { OPERAND_TYPE_DEBUG, "debug reg" },
3245 { OPERAND_TYPE_FLOATREG, "FReg" },
3246 { OPERAND_TYPE_FLOATACC, "FAcc" },
21df382b 3247 { OPERAND_TYPE_SREG, "SReg" },
40fb9820
L
3248 { OPERAND_TYPE_REGMMX, "rMMX" },
3249 { OPERAND_TYPE_REGXMM, "rXMM" },
0349dc08 3250 { OPERAND_TYPE_REGYMM, "rYMM" },
43234a1e 3251 { OPERAND_TYPE_REGZMM, "rZMM" },
260cd341 3252 { OPERAND_TYPE_REGTMM, "rTMM" },
43234a1e 3253 { OPERAND_TYPE_REGMASK, "Mask reg" },
252b5132
RH
3254};
3255
3256static void
40fb9820 3257pt (i386_operand_type t)
252b5132 3258{
40fb9820 3259 unsigned int j;
c6fb90c8 3260 i386_operand_type a;
252b5132 3261
40fb9820 3262 for (j = 0; j < ARRAY_SIZE (type_names); j++)
c6fb90c8
L
3263 {
3264 a = operand_type_and (t, type_names[j].mask);
2c703856 3265 if (operand_type_equal (&a, &type_names[j].mask))
c6fb90c8
L
3266 fprintf (stdout, "%s, ", type_names[j].name);
3267 }
252b5132
RH
3268 fflush (stdout);
3269}
3270
3271#endif /* DEBUG386 */
3272\f
252b5132 3273static bfd_reloc_code_real_type
3956db08 3274reloc (unsigned int size,
64e74474
AM
3275 int pcrel,
3276 int sign,
3277 bfd_reloc_code_real_type other)
252b5132 3278{
47926f60 3279 if (other != NO_RELOC)
3956db08 3280 {
91d6fa6a 3281 reloc_howto_type *rel;
3956db08
JB
3282
3283 if (size == 8)
3284 switch (other)
3285 {
64e74474
AM
3286 case BFD_RELOC_X86_64_GOT32:
3287 return BFD_RELOC_X86_64_GOT64;
3288 break;
553d1284
L
3289 case BFD_RELOC_X86_64_GOTPLT64:
3290 return BFD_RELOC_X86_64_GOTPLT64;
3291 break;
64e74474
AM
3292 case BFD_RELOC_X86_64_PLTOFF64:
3293 return BFD_RELOC_X86_64_PLTOFF64;
3294 break;
3295 case BFD_RELOC_X86_64_GOTPC32:
3296 other = BFD_RELOC_X86_64_GOTPC64;
3297 break;
3298 case BFD_RELOC_X86_64_GOTPCREL:
3299 other = BFD_RELOC_X86_64_GOTPCREL64;
3300 break;
3301 case BFD_RELOC_X86_64_TPOFF32:
3302 other = BFD_RELOC_X86_64_TPOFF64;
3303 break;
3304 case BFD_RELOC_X86_64_DTPOFF32:
3305 other = BFD_RELOC_X86_64_DTPOFF64;
3306 break;
3307 default:
3308 break;
3956db08 3309 }
e05278af 3310
8ce3d284 3311#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
3312 if (other == BFD_RELOC_SIZE32)
3313 {
3314 if (size == 8)
1ab668bf 3315 other = BFD_RELOC_SIZE64;
8fd4256d 3316 if (pcrel)
1ab668bf
AM
3317 {
3318 as_bad (_("there are no pc-relative size relocations"));
3319 return NO_RELOC;
3320 }
8fd4256d 3321 }
8ce3d284 3322#endif
8fd4256d 3323
e05278af 3324 /* Sign-checking 4-byte relocations in 16-/32-bit code is pointless. */
f2d8a97c 3325 if (size == 4 && (flag_code != CODE_64BIT || disallow_64bit_reloc))
e05278af
JB
3326 sign = -1;
3327
91d6fa6a
NC
3328 rel = bfd_reloc_type_lookup (stdoutput, other);
3329 if (!rel)
3956db08 3330 as_bad (_("unknown relocation (%u)"), other);
91d6fa6a 3331 else if (size != bfd_get_reloc_size (rel))
3956db08 3332 as_bad (_("%u-byte relocation cannot be applied to %u-byte field"),
91d6fa6a 3333 bfd_get_reloc_size (rel),
3956db08 3334 size);
91d6fa6a 3335 else if (pcrel && !rel->pc_relative)
3956db08 3336 as_bad (_("non-pc-relative relocation for pc-relative field"));
91d6fa6a 3337 else if ((rel->complain_on_overflow == complain_overflow_signed
3956db08 3338 && !sign)
91d6fa6a 3339 || (rel->complain_on_overflow == complain_overflow_unsigned
64e74474 3340 && sign > 0))
3956db08
JB
3341 as_bad (_("relocated field and relocation type differ in signedness"));
3342 else
3343 return other;
3344 return NO_RELOC;
3345 }
252b5132
RH
3346
3347 if (pcrel)
3348 {
3e73aa7c 3349 if (!sign)
3956db08 3350 as_bad (_("there are no unsigned pc-relative relocations"));
252b5132
RH
3351 switch (size)
3352 {
3353 case 1: return BFD_RELOC_8_PCREL;
3354 case 2: return BFD_RELOC_16_PCREL;
d258b828 3355 case 4: return BFD_RELOC_32_PCREL;
d6ab8113 3356 case 8: return BFD_RELOC_64_PCREL;
252b5132 3357 }
3956db08 3358 as_bad (_("cannot do %u byte pc-relative relocation"), size);
252b5132
RH
3359 }
3360 else
3361 {
3956db08 3362 if (sign > 0)
e5cb08ac 3363 switch (size)
3e73aa7c
JH
3364 {
3365 case 4: return BFD_RELOC_X86_64_32S;
3366 }
3367 else
3368 switch (size)
3369 {
3370 case 1: return BFD_RELOC_8;
3371 case 2: return BFD_RELOC_16;
3372 case 4: return BFD_RELOC_32;
3373 case 8: return BFD_RELOC_64;
3374 }
3956db08
JB
3375 as_bad (_("cannot do %s %u byte relocation"),
3376 sign > 0 ? "signed" : "unsigned", size);
252b5132
RH
3377 }
3378
0cc9e1d3 3379 return NO_RELOC;
252b5132
RH
3380}
3381
47926f60
KH
3382/* Here we decide which fixups can be adjusted to make them relative to
3383 the beginning of the section instead of the symbol. Basically we need
3384 to make sure that the dynamic relocations are done correctly, so in
3385 some cases we force the original symbol to be used. */
3386
252b5132 3387int
e3bb37b5 3388tc_i386_fix_adjustable (fixS *fixP ATTRIBUTE_UNUSED)
252b5132 3389{
6d249963 3390#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 3391 if (!IS_ELF)
31312f95
AM
3392 return 1;
3393
a161fe53
AM
3394 /* Don't adjust pc-relative references to merge sections in 64-bit
3395 mode. */
3396 if (use_rela_relocations
3397 && (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0
3398 && fixP->fx_pcrel)
252b5132 3399 return 0;
31312f95 3400
8d01d9a9
AJ
3401 /* The x86_64 GOTPCREL are represented as 32bit PCrel relocations
3402 and changed later by validate_fix. */
3403 if (GOT_symbol && fixP->fx_subsy == GOT_symbol
3404 && fixP->fx_r_type == BFD_RELOC_32_PCREL)
3405 return 0;
3406
8fd4256d
L
3407 /* Adjust_reloc_syms doesn't know about the GOT. Need to keep symbol
3408 for size relocations. */
3409 if (fixP->fx_r_type == BFD_RELOC_SIZE32
3410 || fixP->fx_r_type == BFD_RELOC_SIZE64
3411 || fixP->fx_r_type == BFD_RELOC_386_GOTOFF
252b5132 3412 || fixP->fx_r_type == BFD_RELOC_386_GOT32
02a86693 3413 || fixP->fx_r_type == BFD_RELOC_386_GOT32X
13ae64f3
JJ
3414 || fixP->fx_r_type == BFD_RELOC_386_TLS_GD
3415 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDM
3416 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDO_32
3417 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE_32
37e55690
JJ
3418 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE
3419 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTIE
13ae64f3
JJ
3420 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE_32
3421 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE
67a4f2b7
AO
3422 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTDESC
3423 || fixP->fx_r_type == BFD_RELOC_386_TLS_DESC_CALL
3e73aa7c 3424 || fixP->fx_r_type == BFD_RELOC_X86_64_GOT32
80b3ee89 3425 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCREL
56ceb5b5
L
3426 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCRELX
3427 || fixP->fx_r_type == BFD_RELOC_X86_64_REX_GOTPCRELX
bffbf940
JJ
3428 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSGD
3429 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSLD
3430 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF32
d6ab8113 3431 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF64
bffbf940
JJ
3432 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF
3433 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF32
d6ab8113
JB
3434 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF64
3435 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTOFF64
67a4f2b7
AO
3436 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
3437 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSDESC_CALL
252b5132
RH
3438 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3439 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3440 return 0;
31312f95 3441#endif
252b5132
RH
3442 return 1;
3443}
252b5132 3444
a9aabc23
JB
3445static INLINE bool
3446want_disp32 (const insn_template *t)
3447{
3448 return flag_code != CODE_64BIT
3449 || i.prefix[ADDR_PREFIX]
3450 || (t->base_opcode == 0x8d
3451 && t->opcode_modifier.opcodespace == SPACE_BASE
fe134c65
JB
3452 && (!i.types[1].bitfield.qword
3453 || t->opcode_modifier.size == SIZE32));
a9aabc23
JB
3454}
3455
b4cac588 3456static int
e3bb37b5 3457intel_float_operand (const char *mnemonic)
252b5132 3458{
9306ca4a
JB
3459 /* Note that the value returned is meaningful only for opcodes with (memory)
3460 operands, hence the code here is free to improperly handle opcodes that
3461 have no operands (for better performance and smaller code). */
3462
3463 if (mnemonic[0] != 'f')
3464 return 0; /* non-math */
3465
3466 switch (mnemonic[1])
3467 {
3468 /* fclex, fdecstp, fdisi, femms, feni, fincstp, finit, fsetpm, and
3469 the fs segment override prefix not currently handled because no
3470 call path can make opcodes without operands get here */
3471 case 'i':
3472 return 2 /* integer op */;
3473 case 'l':
3474 if (mnemonic[2] == 'd' && (mnemonic[3] == 'c' || mnemonic[3] == 'e'))
3475 return 3; /* fldcw/fldenv */
3476 break;
3477 case 'n':
3478 if (mnemonic[2] != 'o' /* fnop */)
3479 return 3; /* non-waiting control op */
3480 break;
3481 case 'r':
3482 if (mnemonic[2] == 's')
3483 return 3; /* frstor/frstpm */
3484 break;
3485 case 's':
3486 if (mnemonic[2] == 'a')
3487 return 3; /* fsave */
3488 if (mnemonic[2] == 't')
3489 {
3490 switch (mnemonic[3])
3491 {
3492 case 'c': /* fstcw */
3493 case 'd': /* fstdw */
3494 case 'e': /* fstenv */
3495 case 's': /* fsts[gw] */
3496 return 3;
3497 }
3498 }
3499 break;
3500 case 'x':
3501 if (mnemonic[2] == 'r' || mnemonic[2] == 's')
3502 return 0; /* fxsave/fxrstor are not really math ops */
3503 break;
3504 }
252b5132 3505
9306ca4a 3506 return 1;
252b5132
RH
3507}
3508
9a182d04
JB
3509static INLINE void
3510install_template (const insn_template *t)
3511{
3512 unsigned int l;
3513
3514 i.tm = *t;
3515
3516 /* Note that for pseudo prefixes this produces a length of 1. But for them
3517 the length isn't interesting at all. */
3518 for (l = 1; l < 4; ++l)
3519 if (!(t->base_opcode >> (8 * l)))
3520 break;
3521
3522 i.opcode_length = l;
3523}
3524
c0f3af97
L
3525/* Build the VEX prefix. */
3526
3527static void
d3ce72d0 3528build_vex_prefix (const insn_template *t)
c0f3af97
L
3529{
3530 unsigned int register_specifier;
c0f3af97 3531 unsigned int vector_length;
03751133 3532 unsigned int w;
c0f3af97
L
3533
3534 /* Check register specifier. */
3535 if (i.vex.register_specifier)
43234a1e
L
3536 {
3537 register_specifier =
3538 ~register_number (i.vex.register_specifier) & 0xf;
3539 gas_assert ((i.vex.register_specifier->reg_flags & RegVRex) == 0);
3540 }
c0f3af97
L
3541 else
3542 register_specifier = 0xf;
3543
79f0fa25
L
3544 /* Use 2-byte VEX prefix by swapping destination and source operand
3545 if there are more than 1 register operand. */
3546 if (i.reg_operands > 1
3547 && i.vec_encoding != vex_encoding_vex3
86fa6981 3548 && i.dir_encoding == dir_encoding_default
fa99fab2 3549 && i.operands == i.reg_operands
dbbc8b7e 3550 && operand_type_equal (&i.types[0], &i.types[i.operands - 1])
441f6aca 3551 && i.tm.opcode_modifier.opcodespace == SPACE_0F
dbbc8b7e 3552 && (i.tm.opcode_modifier.load || i.tm.opcode_modifier.d)
fa99fab2
L
3553 && i.rex == REX_B)
3554 {
3555 unsigned int xchg = i.operands - 1;
3556 union i386_op temp_op;
3557 i386_operand_type temp_type;
3558
3559 temp_type = i.types[xchg];
3560 i.types[xchg] = i.types[0];
3561 i.types[0] = temp_type;
3562 temp_op = i.op[xchg];
3563 i.op[xchg] = i.op[0];
3564 i.op[0] = temp_op;
3565
9c2799c2 3566 gas_assert (i.rm.mode == 3);
fa99fab2
L
3567
3568 i.rex = REX_R;
3569 xchg = i.rm.regmem;
3570 i.rm.regmem = i.rm.reg;
3571 i.rm.reg = xchg;
3572
dbbc8b7e
JB
3573 if (i.tm.opcode_modifier.d)
3574 i.tm.base_opcode ^= (i.tm.base_opcode & 0xee) != 0x6e
2c735193 3575 ? Opcode_ExtD : Opcode_SIMD_IntD;
dbbc8b7e 3576 else /* Use the next insn. */
9a182d04 3577 install_template (&t[1]);
fa99fab2
L
3578 }
3579
79dec6b7
JB
3580 /* Use 2-byte VEX prefix by swapping commutative source operands if there
3581 are no memory operands and at least 3 register ones. */
3582 if (i.reg_operands >= 3
3583 && i.vec_encoding != vex_encoding_vex3
3584 && i.reg_operands == i.operands - i.imm_operands
3585 && i.tm.opcode_modifier.vex
3586 && i.tm.opcode_modifier.commutative
3587 && (i.tm.opcode_modifier.sse2avx || optimize > 1)
3588 && i.rex == REX_B
3589 && i.vex.register_specifier
3590 && !(i.vex.register_specifier->reg_flags & RegRex))
3591 {
3592 unsigned int xchg = i.operands - i.reg_operands;
3593 union i386_op temp_op;
3594 i386_operand_type temp_type;
3595
441f6aca 3596 gas_assert (i.tm.opcode_modifier.opcodespace == SPACE_0F);
79dec6b7
JB
3597 gas_assert (!i.tm.opcode_modifier.sae);
3598 gas_assert (operand_type_equal (&i.types[i.operands - 2],
3599 &i.types[i.operands - 3]));
3600 gas_assert (i.rm.mode == 3);
3601
3602 temp_type = i.types[xchg];
3603 i.types[xchg] = i.types[xchg + 1];
3604 i.types[xchg + 1] = temp_type;
3605 temp_op = i.op[xchg];
3606 i.op[xchg] = i.op[xchg + 1];
3607 i.op[xchg + 1] = temp_op;
3608
3609 i.rex = 0;
3610 xchg = i.rm.regmem | 8;
3611 i.rm.regmem = ~register_specifier & 0xf;
3612 gas_assert (!(i.rm.regmem & 8));
3613 i.vex.register_specifier += xchg - i.rm.regmem;
3614 register_specifier = ~xchg & 0xf;
3615 }
3616
539f890d
L
3617 if (i.tm.opcode_modifier.vex == VEXScalar)
3618 vector_length = avxscalar;
10c17abd
JB
3619 else if (i.tm.opcode_modifier.vex == VEX256)
3620 vector_length = 1;
539f890d 3621 else
10c17abd 3622 {
56522fc5 3623 unsigned int op;
10c17abd 3624
c7213af9
L
3625 /* Determine vector length from the last multi-length vector
3626 operand. */
10c17abd 3627 vector_length = 0;
56522fc5 3628 for (op = t->operands; op--;)
10c17abd
JB
3629 if (t->operand_types[op].bitfield.xmmword
3630 && t->operand_types[op].bitfield.ymmword
3631 && i.types[op].bitfield.ymmword)
3632 {
3633 vector_length = 1;
3634 break;
3635 }
3636 }
c0f3af97 3637
03751133
L
3638 /* Check the REX.W bit and VEXW. */
3639 if (i.tm.opcode_modifier.vexw == VEXWIG)
3640 w = (vexwig == vexw1 || (i.rex & REX_W)) ? 1 : 0;
3641 else if (i.tm.opcode_modifier.vexw)
3642 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3643 else
931d03b7 3644 w = (flag_code == CODE_64BIT ? i.rex & REX_W : vexwig == vexw1) ? 1 : 0;
03751133 3645
c0f3af97 3646 /* Use 2-byte VEX prefix if possible. */
03751133
L
3647 if (w == 0
3648 && i.vec_encoding != vex_encoding_vex3
441f6aca 3649 && i.tm.opcode_modifier.opcodespace == SPACE_0F
c0f3af97
L
3650 && (i.rex & (REX_W | REX_X | REX_B)) == 0)
3651 {
3652 /* 2-byte VEX prefix. */
3653 unsigned int r;
3654
3655 i.vex.length = 2;
3656 i.vex.bytes[0] = 0xc5;
3657
3658 /* Check the REX.R bit. */
3659 r = (i.rex & REX_R) ? 0 : 1;
3660 i.vex.bytes[1] = (r << 7
3661 | register_specifier << 3
3662 | vector_length << 2
35648716 3663 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3664 }
3665 else
3666 {
3667 /* 3-byte VEX prefix. */
f88c9eb0 3668 i.vex.length = 3;
f88c9eb0 3669
441f6aca 3670 switch (i.tm.opcode_modifier.opcodespace)
5dd85c99 3671 {
441f6aca
JB
3672 case SPACE_0F:
3673 case SPACE_0F38:
3674 case SPACE_0F3A:
80de6e00 3675 i.vex.bytes[0] = 0xc4;
7f399153 3676 break;
441f6aca
JB
3677 case SPACE_XOP08:
3678 case SPACE_XOP09:
3679 case SPACE_XOP0A:
f88c9eb0 3680 i.vex.bytes[0] = 0x8f;
7f399153
L
3681 break;
3682 default:
3683 abort ();
f88c9eb0 3684 }
c0f3af97 3685
c0f3af97
L
3686 /* The high 3 bits of the second VEX byte are 1's compliment
3687 of RXB bits from REX. */
441f6aca 3688 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | i.tm.opcode_modifier.opcodespace;
c0f3af97 3689
c0f3af97
L
3690 i.vex.bytes[2] = (w << 7
3691 | register_specifier << 3
3692 | vector_length << 2
35648716 3693 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3694 }
3695}
3696
5b7c81bd 3697static INLINE bool
e771e7c9
JB
3698is_evex_encoding (const insn_template *t)
3699{
7091c612 3700 return t->opcode_modifier.evex || t->opcode_modifier.disp8memshift
e771e7c9 3701 || t->opcode_modifier.broadcast || t->opcode_modifier.masking
a80195f1 3702 || t->opcode_modifier.sae;
e771e7c9
JB
3703}
3704
5b7c81bd 3705static INLINE bool
7a8655d2
JB
3706is_any_vex_encoding (const insn_template *t)
3707{
7b47a312 3708 return t->opcode_modifier.vex || is_evex_encoding (t);
7a8655d2
JB
3709}
3710
a5748e0d
JB
3711static unsigned int
3712get_broadcast_bytes (const insn_template *t, bool diag)
3713{
3714 unsigned int op, bytes;
3715 const i386_operand_type *types;
3716
3717 if (i.broadcast.type)
3718 return i.broadcast.bytes = ((1 << (t->opcode_modifier.broadcast - 1))
3719 * i.broadcast.type);
3720
3721 gas_assert (intel_syntax);
3722
3723 for (op = 0; op < t->operands; ++op)
3724 if (t->operand_types[op].bitfield.baseindex)
3725 break;
3726
3727 gas_assert (op < t->operands);
3728
3729 if (t->opcode_modifier.evex
3730 && t->opcode_modifier.evex != EVEXDYN)
3731 switch (i.broadcast.bytes)
3732 {
3733 case 1:
3734 if (t->operand_types[op].bitfield.word)
3735 return 2;
3736 /* Fall through. */
3737 case 2:
3738 if (t->operand_types[op].bitfield.dword)
3739 return 4;
3740 /* Fall through. */
3741 case 4:
3742 if (t->operand_types[op].bitfield.qword)
3743 return 8;
3744 /* Fall through. */
3745 case 8:
3746 if (t->operand_types[op].bitfield.xmmword)
3747 return 16;
3748 if (t->operand_types[op].bitfield.ymmword)
3749 return 32;
3750 if (t->operand_types[op].bitfield.zmmword)
3751 return 64;
3752 /* Fall through. */
3753 default:
3754 abort ();
3755 }
3756
3757 gas_assert (op + 1 < t->operands);
3758
3759 if (t->operand_types[op + 1].bitfield.xmmword
3760 + t->operand_types[op + 1].bitfield.ymmword
3761 + t->operand_types[op + 1].bitfield.zmmword > 1)
3762 {
3763 types = &i.types[op + 1];
3764 diag = false;
3765 }
3766 else /* Ambiguous - guess with a preference to non-AVX512VL forms. */
3767 types = &t->operand_types[op];
3768
3769 if (types->bitfield.zmmword)
3770 bytes = 64;
3771 else if (types->bitfield.ymmword)
3772 bytes = 32;
3773 else
3774 bytes = 16;
3775
3776 if (diag)
3777 as_warn (_("ambiguous broadcast for `%s', using %u-bit form"),
3778 t->name, bytes * 8);
3779
3780 return bytes;
3781}
3782
43234a1e
L
3783/* Build the EVEX prefix. */
3784
3785static void
3786build_evex_prefix (void)
3787{
35648716 3788 unsigned int register_specifier, w;
43234a1e
L
3789 rex_byte vrex_used = 0;
3790
3791 /* Check register specifier. */
3792 if (i.vex.register_specifier)
3793 {
3794 gas_assert ((i.vrex & REX_X) == 0);
3795
3796 register_specifier = i.vex.register_specifier->reg_num;
3797 if ((i.vex.register_specifier->reg_flags & RegRex))
3798 register_specifier += 8;
3799 /* The upper 16 registers are encoded in the fourth byte of the
3800 EVEX prefix. */
3801 if (!(i.vex.register_specifier->reg_flags & RegVRex))
3802 i.vex.bytes[3] = 0x8;
3803 register_specifier = ~register_specifier & 0xf;
3804 }
3805 else
3806 {
3807 register_specifier = 0xf;
3808
3809 /* Encode upper 16 vector index register in the fourth byte of
3810 the EVEX prefix. */
3811 if (!(i.vrex & REX_X))
3812 i.vex.bytes[3] = 0x8;
3813 else
3814 vrex_used |= REX_X;
3815 }
3816
43234a1e
L
3817 /* 4 byte EVEX prefix. */
3818 i.vex.length = 4;
3819 i.vex.bytes[0] = 0x62;
3820
43234a1e
L
3821 /* The high 3 bits of the second EVEX byte are 1's compliment of RXB
3822 bits from REX. */
441f6aca 3823 gas_assert (i.tm.opcode_modifier.opcodespace >= SPACE_0F);
0cc78721 3824 gas_assert (i.tm.opcode_modifier.opcodespace <= SPACE_EVEXMAP6);
441f6aca 3825 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | i.tm.opcode_modifier.opcodespace;
43234a1e
L
3826
3827 /* The fifth bit of the second EVEX byte is 1's compliment of the
3828 REX_R bit in VREX. */
3829 if (!(i.vrex & REX_R))
3830 i.vex.bytes[1] |= 0x10;
3831 else
3832 vrex_used |= REX_R;
3833
3834 if ((i.reg_operands + i.imm_operands) == i.operands)
3835 {
3836 /* When all operands are registers, the REX_X bit in REX is not
3837 used. We reuse it to encode the upper 16 registers, which is
3838 indicated by the REX_B bit in VREX. The REX_X bit is encoded
3839 as 1's compliment. */
3840 if ((i.vrex & REX_B))
3841 {
3842 vrex_used |= REX_B;
3843 i.vex.bytes[1] &= ~0x40;
3844 }
3845 }
3846
3847 /* EVEX instructions shouldn't need the REX prefix. */
3848 i.vrex &= ~vrex_used;
3849 gas_assert (i.vrex == 0);
3850
6865c043
L
3851 /* Check the REX.W bit and VEXW. */
3852 if (i.tm.opcode_modifier.vexw == VEXWIG)
3853 w = (evexwig == evexw1 || (i.rex & REX_W)) ? 1 : 0;
3854 else if (i.tm.opcode_modifier.vexw)
3855 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3856 else
931d03b7 3857 w = (flag_code == CODE_64BIT ? i.rex & REX_W : evexwig == evexw1) ? 1 : 0;
43234a1e 3858
43234a1e 3859 /* The third byte of the EVEX prefix. */
35648716
JB
3860 i.vex.bytes[2] = ((w << 7)
3861 | (register_specifier << 3)
3862 | 4 /* Encode the U bit. */
3863 | i.tm.opcode_modifier.opcodeprefix);
43234a1e
L
3864
3865 /* The fourth byte of the EVEX prefix. */
3866 /* The zeroing-masking bit. */
6225c532 3867 if (i.mask.reg && i.mask.zeroing)
43234a1e
L
3868 i.vex.bytes[3] |= 0x80;
3869
3870 /* Don't always set the broadcast bit if there is no RC. */
ca5312a2 3871 if (i.rounding.type == rc_none)
43234a1e
L
3872 {
3873 /* Encode the vector length. */
3874 unsigned int vec_length;
3875
e771e7c9
JB
3876 if (!i.tm.opcode_modifier.evex
3877 || i.tm.opcode_modifier.evex == EVEXDYN)
3878 {
56522fc5 3879 unsigned int op;
e771e7c9 3880
c7213af9
L
3881 /* Determine vector length from the last multi-length vector
3882 operand. */
56522fc5 3883 for (op = i.operands; op--;)
e771e7c9
JB
3884 if (i.tm.operand_types[op].bitfield.xmmword
3885 + i.tm.operand_types[op].bitfield.ymmword
3886 + i.tm.operand_types[op].bitfield.zmmword > 1)
3887 {
3888 if (i.types[op].bitfield.zmmword)
c7213af9
L
3889 {
3890 i.tm.opcode_modifier.evex = EVEX512;
3891 break;
3892 }
e771e7c9 3893 else if (i.types[op].bitfield.ymmword)
c7213af9
L
3894 {
3895 i.tm.opcode_modifier.evex = EVEX256;
3896 break;
3897 }
e771e7c9 3898 else if (i.types[op].bitfield.xmmword)
c7213af9
L
3899 {
3900 i.tm.opcode_modifier.evex = EVEX128;
3901 break;
3902 }
a5748e0d 3903 else if (i.broadcast.bytes && op == i.broadcast.operand)
625cbd7a 3904 {
a5748e0d 3905 switch (get_broadcast_bytes (&i.tm, true))
625cbd7a
JB
3906 {
3907 case 64:
3908 i.tm.opcode_modifier.evex = EVEX512;
3909 break;
3910 case 32:
3911 i.tm.opcode_modifier.evex = EVEX256;
3912 break;
3913 case 16:
3914 i.tm.opcode_modifier.evex = EVEX128;
3915 break;
3916 default:
c7213af9 3917 abort ();
625cbd7a 3918 }
c7213af9 3919 break;
625cbd7a 3920 }
e771e7c9 3921 }
c7213af9 3922
56522fc5 3923 if (op >= MAX_OPERANDS)
c7213af9 3924 abort ();
e771e7c9
JB
3925 }
3926
43234a1e
L
3927 switch (i.tm.opcode_modifier.evex)
3928 {
3929 case EVEXLIG: /* LL' is ignored */
3930 vec_length = evexlig << 5;
3931 break;
3932 case EVEX128:
3933 vec_length = 0 << 5;
3934 break;
3935 case EVEX256:
3936 vec_length = 1 << 5;
3937 break;
3938 case EVEX512:
3939 vec_length = 2 << 5;
3940 break;
3941 default:
3942 abort ();
3943 break;
3944 }
3945 i.vex.bytes[3] |= vec_length;
3946 /* Encode the broadcast bit. */
a5748e0d 3947 if (i.broadcast.bytes)
43234a1e
L
3948 i.vex.bytes[3] |= 0x10;
3949 }
ca5312a2
JB
3950 else if (i.rounding.type != saeonly)
3951 i.vex.bytes[3] |= 0x10 | (i.rounding.type << 5);
43234a1e 3952 else
ca5312a2 3953 i.vex.bytes[3] |= 0x10 | (evexrcig << 5);
43234a1e 3954
6225c532
JB
3955 if (i.mask.reg)
3956 i.vex.bytes[3] |= i.mask.reg->reg_num;
43234a1e
L
3957}
3958
65da13b5
L
3959static void
3960process_immext (void)
3961{
3962 expressionS *exp;
3963
c0f3af97 3964 /* These AMD 3DNow! and SSE2 instructions have an opcode suffix
65da13b5
L
3965 which is coded in the same place as an 8-bit immediate field
3966 would be. Here we fake an 8-bit immediate operand from the
3967 opcode suffix stored in tm.extension_opcode.
3968
c1e679ec 3969 AVX instructions also use this encoding, for some of
c0f3af97 3970 3 argument instructions. */
65da13b5 3971
43234a1e 3972 gas_assert (i.imm_operands <= 1
7ab9ffdd 3973 && (i.operands <= 2
7a8655d2 3974 || (is_any_vex_encoding (&i.tm)
7ab9ffdd 3975 && i.operands <= 4)));
65da13b5
L
3976
3977 exp = &im_expressions[i.imm_operands++];
3978 i.op[i.operands].imms = exp;
3979 i.types[i.operands] = imm8;
3980 i.operands++;
3981 exp->X_op = O_constant;
3982 exp->X_add_number = i.tm.extension_opcode;
3983 i.tm.extension_opcode = None;
3984}
3985
42164a71
L
3986
3987static int
3988check_hle (void)
3989{
742732c7 3990 switch (i.tm.opcode_modifier.prefixok)
42164a71
L
3991 {
3992 default:
3993 abort ();
742732c7
JB
3994 case PrefixLock:
3995 case PrefixNone:
3996 case PrefixNoTrack:
3997 case PrefixRep:
165de32a
L
3998 as_bad (_("invalid instruction `%s' after `%s'"),
3999 i.tm.name, i.hle_prefix);
42164a71 4000 return 0;
742732c7 4001 case PrefixHLELock:
42164a71
L
4002 if (i.prefix[LOCK_PREFIX])
4003 return 1;
165de32a 4004 as_bad (_("missing `lock' with `%s'"), i.hle_prefix);
42164a71 4005 return 0;
742732c7 4006 case PrefixHLEAny:
42164a71 4007 return 1;
742732c7 4008 case PrefixHLERelease:
42164a71
L
4009 if (i.prefix[HLE_PREFIX] != XRELEASE_PREFIX_OPCODE)
4010 {
4011 as_bad (_("instruction `%s' after `xacquire' not allowed"),
4012 i.tm.name);
4013 return 0;
4014 }
8dc0818e 4015 if (i.mem_operands == 0 || !(i.flags[i.operands - 1] & Operand_Mem))
42164a71
L
4016 {
4017 as_bad (_("memory destination needed for instruction `%s'"
4018 " after `xrelease'"), i.tm.name);
4019 return 0;
4020 }
4021 return 1;
4022 }
4023}
4024
c8480b58
L
4025/* Encode aligned vector move as unaligned vector move. */
4026
4027static void
4028encode_with_unaligned_vector_move (void)
4029{
4030 switch (i.tm.base_opcode)
4031 {
b3a9fe6f
L
4032 case 0x28: /* Load instructions. */
4033 case 0x29: /* Store instructions. */
c8480b58
L
4034 /* movaps/movapd/vmovaps/vmovapd. */
4035 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4036 && i.tm.opcode_modifier.opcodeprefix <= PREFIX_0X66)
b3a9fe6f 4037 i.tm.base_opcode = 0x10 | (i.tm.base_opcode & 1);
c8480b58 4038 break;
b3a9fe6f
L
4039 case 0x6f: /* Load instructions. */
4040 case 0x7f: /* Store instructions. */
c8480b58
L
4041 /* movdqa/vmovdqa/vmovdqa64/vmovdqa32. */
4042 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4043 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0X66)
4044 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
4045 break;
4046 default:
4047 break;
4048 }
4049}
4050
b6f8c7c4
L
4051/* Try the shortest encoding by shortening operand size. */
4052
4053static void
4054optimize_encoding (void)
4055{
a0a1771e 4056 unsigned int j;
b6f8c7c4 4057
fe134c65
JB
4058 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
4059 && i.tm.base_opcode == 0x8d)
4060 {
4061 /* Optimize: -O:
4062 lea symbol, %rN -> mov $symbol, %rN
4063 lea (%rM), %rN -> mov %rM, %rN
4064 lea (,%rM,1), %rN -> mov %rM, %rN
4065
4066 and in 32-bit mode for 16-bit addressing
4067
4068 lea (%rM), %rN -> movzx %rM, %rN
4069
4070 and in 64-bit mode zap 32-bit addressing in favor of using a
4071 32-bit (or less) destination.
4072 */
4073 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
4074 {
4075 if (!i.op[1].regs->reg_type.bitfield.word)
4076 i.tm.opcode_modifier.size = SIZE32;
4077 i.prefix[ADDR_PREFIX] = 0;
4078 }
4079
4080 if (!i.index_reg && !i.base_reg)
4081 {
4082 /* Handle:
4083 lea symbol, %rN -> mov $symbol, %rN
4084 */
4085 if (flag_code == CODE_64BIT)
4086 {
4087 /* Don't transform a relocation to a 16-bit one. */
4088 if (i.op[0].disps
4089 && i.op[0].disps->X_op != O_constant
4090 && i.op[1].regs->reg_type.bitfield.word)
4091 return;
4092
4093 if (!i.op[1].regs->reg_type.bitfield.qword
4094 || i.tm.opcode_modifier.size == SIZE32)
4095 {
4096 i.tm.base_opcode = 0xb8;
4097 i.tm.opcode_modifier.modrm = 0;
4098 if (!i.op[1].regs->reg_type.bitfield.word)
4099 i.types[0].bitfield.imm32 = 1;
4100 else
4101 {
4102 i.tm.opcode_modifier.size = SIZE16;
4103 i.types[0].bitfield.imm16 = 1;
4104 }
4105 }
4106 else
4107 {
4108 /* Subject to further optimization below. */
4109 i.tm.base_opcode = 0xc7;
4110 i.tm.extension_opcode = 0;
4111 i.types[0].bitfield.imm32s = 1;
4112 i.types[0].bitfield.baseindex = 0;
4113 }
4114 }
4115 /* Outside of 64-bit mode address and operand sizes have to match if
4116 a relocation is involved, as otherwise we wouldn't (currently) or
4117 even couldn't express the relocation correctly. */
4118 else if (i.op[0].disps
4119 && i.op[0].disps->X_op != O_constant
4120 && ((!i.prefix[ADDR_PREFIX])
4121 != (flag_code == CODE_32BIT
4122 ? i.op[1].regs->reg_type.bitfield.dword
4123 : i.op[1].regs->reg_type.bitfield.word)))
4124 return;
7772f168
JB
4125 /* In 16-bit mode converting LEA with 16-bit addressing and a 32-bit
4126 destination is going to grow encoding size. */
4127 else if (flag_code == CODE_16BIT
4128 && (optimize <= 1 || optimize_for_space)
4129 && !i.prefix[ADDR_PREFIX]
4130 && i.op[1].regs->reg_type.bitfield.dword)
4131 return;
fe134c65
JB
4132 else
4133 {
4134 i.tm.base_opcode = 0xb8;
4135 i.tm.opcode_modifier.modrm = 0;
4136 if (i.op[1].regs->reg_type.bitfield.dword)
4137 i.types[0].bitfield.imm32 = 1;
4138 else
4139 i.types[0].bitfield.imm16 = 1;
4140
4141 if (i.op[0].disps
4142 && i.op[0].disps->X_op == O_constant
4143 && i.op[1].regs->reg_type.bitfield.dword
60cfa10c
L
4144 /* NB: Add () to !i.prefix[ADDR_PREFIX] to silence
4145 GCC 5. */
4146 && (!i.prefix[ADDR_PREFIX]) != (flag_code == CODE_32BIT))
fe134c65
JB
4147 i.op[0].disps->X_add_number &= 0xffff;
4148 }
4149
4150 i.tm.operand_types[0] = i.types[0];
4151 i.imm_operands = 1;
4152 if (!i.op[0].imms)
4153 {
4154 i.op[0].imms = &im_expressions[0];
4155 i.op[0].imms->X_op = O_absent;
4156 }
4157 }
4158 else if (i.op[0].disps
4159 && (i.op[0].disps->X_op != O_constant
4160 || i.op[0].disps->X_add_number))
4161 return;
4162 else
4163 {
4164 /* Handle:
4165 lea (%rM), %rN -> mov %rM, %rN
4166 lea (,%rM,1), %rN -> mov %rM, %rN
4167 lea (%rM), %rN -> movzx %rM, %rN
4168 */
4169 const reg_entry *addr_reg;
4170
4171 if (!i.index_reg && i.base_reg->reg_num != RegIP)
4172 addr_reg = i.base_reg;
4173 else if (!i.base_reg
4174 && i.index_reg->reg_num != RegIZ
4175 && !i.log2_scale_factor)
4176 addr_reg = i.index_reg;
4177 else
4178 return;
4179
4180 if (addr_reg->reg_type.bitfield.word
4181 && i.op[1].regs->reg_type.bitfield.dword)
4182 {
4183 if (flag_code != CODE_32BIT)
4184 return;
4185 i.tm.opcode_modifier.opcodespace = SPACE_0F;
4186 i.tm.base_opcode = 0xb7;
4187 }
4188 else
4189 i.tm.base_opcode = 0x8b;
4190
4191 if (addr_reg->reg_type.bitfield.dword
4192 && i.op[1].regs->reg_type.bitfield.qword)
4193 i.tm.opcode_modifier.size = SIZE32;
4194
4195 i.op[0].regs = addr_reg;
4196 i.reg_operands = 2;
4197 }
4198
4199 i.mem_operands = 0;
4200 i.disp_operands = 0;
4201 i.prefix[ADDR_PREFIX] = 0;
4202 i.prefix[SEG_PREFIX] = 0;
4203 i.seg[0] = NULL;
4204 }
4205
b6f8c7c4 4206 if (optimize_for_space
389d00a5 4207 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
b6f8c7c4
L
4208 && i.reg_operands == 1
4209 && i.imm_operands == 1
4210 && !i.types[1].bitfield.byte
4211 && i.op[0].imms->X_op == O_constant
4212 && fits_in_imm7 (i.op[0].imms->X_add_number)
72aea328 4213 && (i.tm.base_opcode == 0xa8
b6f8c7c4
L
4214 || (i.tm.base_opcode == 0xf6
4215 && i.tm.extension_opcode == 0x0)))
4216 {
4217 /* Optimize: -Os:
4218 test $imm7, %r64/%r32/%r16 -> test $imm7, %r8
4219 */
4220 unsigned int base_regnum = i.op[1].regs->reg_num;
4221 if (flag_code == CODE_64BIT || base_regnum < 4)
4222 {
4223 i.types[1].bitfield.byte = 1;
4224 /* Ignore the suffix. */
4225 i.suffix = 0;
7697afb6
JB
4226 /* Convert to byte registers. */
4227 if (i.types[1].bitfield.word)
4228 j = 16;
4229 else if (i.types[1].bitfield.dword)
4230 j = 32;
4231 else
4232 j = 48;
4233 if (!(i.op[1].regs->reg_flags & RegRex) && base_regnum < 4)
4234 j += 8;
4235 i.op[1].regs -= j;
b6f8c7c4
L
4236 }
4237 }
4238 else if (flag_code == CODE_64BIT
389d00a5 4239 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
d3d50934
L
4240 && ((i.types[1].bitfield.qword
4241 && i.reg_operands == 1
b6f8c7c4
L
4242 && i.imm_operands == 1
4243 && i.op[0].imms->X_op == O_constant
507916b8 4244 && ((i.tm.base_opcode == 0xb8
b6f8c7c4
L
4245 && i.tm.extension_opcode == None
4246 && fits_in_unsigned_long (i.op[0].imms->X_add_number))
4247 || (fits_in_imm31 (i.op[0].imms->X_add_number)
72aea328
JB
4248 && ((i.tm.base_opcode == 0x24
4249 || i.tm.base_opcode == 0xa8)
b6f8c7c4
L
4250 || (i.tm.base_opcode == 0x80
4251 && i.tm.extension_opcode == 0x4)
4252 || ((i.tm.base_opcode == 0xf6
507916b8 4253 || (i.tm.base_opcode | 1) == 0xc7)
b8364fa7
JB
4254 && i.tm.extension_opcode == 0x0)))
4255 || (fits_in_imm7 (i.op[0].imms->X_add_number)
4256 && i.tm.base_opcode == 0x83
4257 && i.tm.extension_opcode == 0x4)))
d3d50934
L
4258 || (i.types[0].bitfield.qword
4259 && ((i.reg_operands == 2
4260 && i.op[0].regs == i.op[1].regs
72aea328
JB
4261 && (i.tm.base_opcode == 0x30
4262 || i.tm.base_opcode == 0x28))
d3d50934
L
4263 || (i.reg_operands == 1
4264 && i.operands == 1
72aea328 4265 && i.tm.base_opcode == 0x30)))))
b6f8c7c4
L
4266 {
4267 /* Optimize: -O:
4268 andq $imm31, %r64 -> andl $imm31, %r32
b8364fa7 4269 andq $imm7, %r64 -> andl $imm7, %r32
b6f8c7c4
L
4270 testq $imm31, %r64 -> testl $imm31, %r32
4271 xorq %r64, %r64 -> xorl %r32, %r32
4272 subq %r64, %r64 -> subl %r32, %r32
4273 movq $imm31, %r64 -> movl $imm31, %r32
4274 movq $imm32, %r64 -> movl $imm32, %r32
4275 */
4276 i.tm.opcode_modifier.norex64 = 1;
507916b8 4277 if (i.tm.base_opcode == 0xb8 || (i.tm.base_opcode | 1) == 0xc7)
b6f8c7c4
L
4278 {
4279 /* Handle
4280 movq $imm31, %r64 -> movl $imm31, %r32
4281 movq $imm32, %r64 -> movl $imm32, %r32
4282 */
4283 i.tm.operand_types[0].bitfield.imm32 = 1;
4284 i.tm.operand_types[0].bitfield.imm32s = 0;
4285 i.tm.operand_types[0].bitfield.imm64 = 0;
4286 i.types[0].bitfield.imm32 = 1;
4287 i.types[0].bitfield.imm32s = 0;
4288 i.types[0].bitfield.imm64 = 0;
4289 i.types[1].bitfield.dword = 1;
4290 i.types[1].bitfield.qword = 0;
507916b8 4291 if ((i.tm.base_opcode | 1) == 0xc7)
b6f8c7c4
L
4292 {
4293 /* Handle
4294 movq $imm31, %r64 -> movl $imm31, %r32
4295 */
507916b8 4296 i.tm.base_opcode = 0xb8;
b6f8c7c4 4297 i.tm.extension_opcode = None;
507916b8 4298 i.tm.opcode_modifier.w = 0;
b6f8c7c4
L
4299 i.tm.opcode_modifier.modrm = 0;
4300 }
4301 }
4302 }
5641ec01
JB
4303 else if (optimize > 1
4304 && !optimize_for_space
389d00a5 4305 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
5641ec01
JB
4306 && i.reg_operands == 2
4307 && i.op[0].regs == i.op[1].regs
4308 && ((i.tm.base_opcode & ~(Opcode_D | 1)) == 0x8
4309 || (i.tm.base_opcode & ~(Opcode_D | 1)) == 0x20)
4310 && (flag_code != CODE_64BIT || !i.types[0].bitfield.dword))
4311 {
4312 /* Optimize: -O2:
4313 andb %rN, %rN -> testb %rN, %rN
4314 andw %rN, %rN -> testw %rN, %rN
4315 andq %rN, %rN -> testq %rN, %rN
4316 orb %rN, %rN -> testb %rN, %rN
4317 orw %rN, %rN -> testw %rN, %rN
4318 orq %rN, %rN -> testq %rN, %rN
4319
4320 and outside of 64-bit mode
4321
4322 andl %rN, %rN -> testl %rN, %rN
4323 orl %rN, %rN -> testl %rN, %rN
4324 */
4325 i.tm.base_opcode = 0x84 | (i.tm.base_opcode & 1);
4326 }
99112332 4327 else if (i.reg_operands == 3
b6f8c7c4
L
4328 && i.op[0].regs == i.op[1].regs
4329 && !i.types[2].bitfield.xmmword
4330 && (i.tm.opcode_modifier.vex
6225c532 4331 || ((!i.mask.reg || i.mask.zeroing)
e771e7c9 4332 && is_evex_encoding (&i.tm)
80c34c38 4333 && (i.vec_encoding != vex_encoding_evex
dd22218c 4334 || cpu_arch_isa_flags.bitfield.cpuavx512vl
80c34c38 4335 || i.tm.cpu_flags.bitfield.cpuavx512vl
7091c612 4336 || (i.tm.operand_types[2].bitfield.zmmword
dd22218c 4337 && i.types[2].bitfield.ymmword))))
5844ccaa
JB
4338 && i.tm.opcode_modifier.opcodespace == SPACE_0F
4339 && ((i.tm.base_opcode | 2) == 0x57
4340 || i.tm.base_opcode == 0xdf
4341 || i.tm.base_opcode == 0xef
4342 || (i.tm.base_opcode | 3) == 0xfb
4343 || i.tm.base_opcode == 0x42
4344 || i.tm.base_opcode == 0x47))
b6f8c7c4 4345 {
99112332 4346 /* Optimize: -O1:
8305403a
L
4347 VOP, one of vandnps, vandnpd, vxorps, vxorpd, vpsubb, vpsubd,
4348 vpsubq and vpsubw:
b6f8c7c4
L
4349 EVEX VOP %zmmM, %zmmM, %zmmN
4350 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4351 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4352 EVEX VOP %ymmM, %ymmM, %ymmN
4353 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4354 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4355 VEX VOP %ymmM, %ymmM, %ymmN
4356 -> VEX VOP %xmmM, %xmmM, %xmmN
4357 VOP, one of vpandn and vpxor:
4358 VEX VOP %ymmM, %ymmM, %ymmN
4359 -> VEX VOP %xmmM, %xmmM, %xmmN
4360 VOP, one of vpandnd and vpandnq:
4361 EVEX VOP %zmmM, %zmmM, %zmmN
4362 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4363 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4364 EVEX VOP %ymmM, %ymmM, %ymmN
4365 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4366 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4367 VOP, one of vpxord and vpxorq:
4368 EVEX VOP %zmmM, %zmmM, %zmmN
4369 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4370 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4371 EVEX VOP %ymmM, %ymmM, %ymmN
4372 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4373 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
1424ad86
JB
4374 VOP, one of kxord and kxorq:
4375 VEX VOP %kM, %kM, %kN
4376 -> VEX kxorw %kM, %kM, %kN
4377 VOP, one of kandnd and kandnq:
4378 VEX VOP %kM, %kM, %kN
4379 -> VEX kandnw %kM, %kM, %kN
b6f8c7c4 4380 */
e771e7c9 4381 if (is_evex_encoding (&i.tm))
b6f8c7c4 4382 {
7b1d7ca1 4383 if (i.vec_encoding != vex_encoding_evex)
b6f8c7c4
L
4384 {
4385 i.tm.opcode_modifier.vex = VEX128;
4386 i.tm.opcode_modifier.vexw = VEXW0;
4387 i.tm.opcode_modifier.evex = 0;
4388 }
7b1d7ca1 4389 else if (optimize > 1)
dd22218c
L
4390 i.tm.opcode_modifier.evex = EVEX128;
4391 else
4392 return;
b6f8c7c4 4393 }
f74a6307 4394 else if (i.tm.operand_types[0].bitfield.class == RegMask)
1424ad86 4395 {
35648716 4396 i.tm.opcode_modifier.opcodeprefix = PREFIX_NONE;
1424ad86
JB
4397 i.tm.opcode_modifier.vexw = VEXW0;
4398 }
b6f8c7c4
L
4399 else
4400 i.tm.opcode_modifier.vex = VEX128;
4401
4402 if (i.tm.opcode_modifier.vex)
4403 for (j = 0; j < 3; j++)
4404 {
4405 i.types[j].bitfield.xmmword = 1;
4406 i.types[j].bitfield.ymmword = 0;
4407 }
4408 }
392a5972 4409 else if (i.vec_encoding != vex_encoding_evex
97ed31ae 4410 && !i.types[0].bitfield.zmmword
392a5972 4411 && !i.types[1].bitfield.zmmword
6225c532 4412 && !i.mask.reg
a5748e0d 4413 && !i.broadcast.bytes
97ed31ae 4414 && is_evex_encoding (&i.tm)
35648716
JB
4415 && ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4416 || (i.tm.base_opcode & ~4) == 0xdb
4417 || (i.tm.base_opcode & ~4) == 0xeb)
97ed31ae
L
4418 && i.tm.extension_opcode == None)
4419 {
4420 /* Optimize: -O1:
4421 VOP, one of vmovdqa32, vmovdqa64, vmovdqu8, vmovdqu16,
4422 vmovdqu32 and vmovdqu64:
4423 EVEX VOP %xmmM, %xmmN
4424 -> VEX vmovdqa|vmovdqu %xmmM, %xmmN (M and N < 16)
4425 EVEX VOP %ymmM, %ymmN
4426 -> VEX vmovdqa|vmovdqu %ymmM, %ymmN (M and N < 16)
4427 EVEX VOP %xmmM, mem
4428 -> VEX vmovdqa|vmovdqu %xmmM, mem (M < 16)
4429 EVEX VOP %ymmM, mem
4430 -> VEX vmovdqa|vmovdqu %ymmM, mem (M < 16)
4431 EVEX VOP mem, %xmmN
4432 -> VEX mvmovdqa|vmovdquem, %xmmN (N < 16)
4433 EVEX VOP mem, %ymmN
4434 -> VEX vmovdqa|vmovdqu mem, %ymmN (N < 16)
a0a1771e
JB
4435 VOP, one of vpand, vpandn, vpor, vpxor:
4436 EVEX VOP{d,q} %xmmL, %xmmM, %xmmN
4437 -> VEX VOP %xmmL, %xmmM, %xmmN (L, M, and N < 16)
4438 EVEX VOP{d,q} %ymmL, %ymmM, %ymmN
4439 -> VEX VOP %ymmL, %ymmM, %ymmN (L, M, and N < 16)
4440 EVEX VOP{d,q} mem, %xmmM, %xmmN
4441 -> VEX VOP mem, %xmmM, %xmmN (M and N < 16)
4442 EVEX VOP{d,q} mem, %ymmM, %ymmN
4443 -> VEX VOP mem, %ymmM, %ymmN (M and N < 16)
97ed31ae 4444 */
a0a1771e 4445 for (j = 0; j < i.operands; j++)
392a5972
L
4446 if (operand_type_check (i.types[j], disp)
4447 && i.op[j].disps->X_op == O_constant)
4448 {
4449 /* Since the VEX prefix has 2 or 3 bytes, the EVEX prefix
4450 has 4 bytes, EVEX Disp8 has 1 byte and VEX Disp32 has 4
4451 bytes, we choose EVEX Disp8 over VEX Disp32. */
4452 int evex_disp8, vex_disp8;
4453 unsigned int memshift = i.memshift;
4454 offsetT n = i.op[j].disps->X_add_number;
4455
4456 evex_disp8 = fits_in_disp8 (n);
4457 i.memshift = 0;
4458 vex_disp8 = fits_in_disp8 (n);
4459 if (evex_disp8 != vex_disp8)
4460 {
4461 i.memshift = memshift;
4462 return;
4463 }
4464
4465 i.types[j].bitfield.disp8 = vex_disp8;
4466 break;
4467 }
35648716
JB
4468 if ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4469 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2)
4470 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
97ed31ae
L
4471 i.tm.opcode_modifier.vex
4472 = i.types[0].bitfield.ymmword ? VEX256 : VEX128;
4473 i.tm.opcode_modifier.vexw = VEXW0;
79dec6b7 4474 /* VPAND, VPOR, and VPXOR are commutative. */
35648716 4475 if (i.reg_operands == 3 && i.tm.base_opcode != 0xdf)
79dec6b7 4476 i.tm.opcode_modifier.commutative = 1;
97ed31ae
L
4477 i.tm.opcode_modifier.evex = 0;
4478 i.tm.opcode_modifier.masking = 0;
a0a1771e 4479 i.tm.opcode_modifier.broadcast = 0;
97ed31ae
L
4480 i.tm.opcode_modifier.disp8memshift = 0;
4481 i.memshift = 0;
a0a1771e
JB
4482 if (j < i.operands)
4483 i.types[j].bitfield.disp8
4484 = fits_in_disp8 (i.op[j].disps->X_add_number);
97ed31ae 4485 }
b6f8c7c4
L
4486}
4487
ae531041
L
4488/* Return non-zero for load instruction. */
4489
4490static int
4491load_insn_p (void)
4492{
4493 unsigned int dest;
4494 int any_vex_p = is_any_vex_encoding (&i.tm);
4495 unsigned int base_opcode = i.tm.base_opcode | 1;
4496
4497 if (!any_vex_p)
4498 {
a09f656b 4499 /* Anysize insns: lea, invlpg, clflush, prefetchnta, prefetcht0,
4500 prefetcht1, prefetcht2, prefetchtw, bndmk, bndcl, bndcu, bndcn,
4501 bndstx, bndldx, prefetchwt1, clflushopt, clwb, cldemote. */
4502 if (i.tm.opcode_modifier.anysize)
ae531041
L
4503 return 0;
4504
389d00a5
JB
4505 /* pop. */
4506 if (strcmp (i.tm.name, "pop") == 0)
4507 return 1;
4508 }
4509
4510 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE)
4511 {
4512 /* popf, popa. */
4513 if (i.tm.base_opcode == 0x9d
a09f656b 4514 || i.tm.base_opcode == 0x61)
ae531041
L
4515 return 1;
4516
4517 /* movs, cmps, lods, scas. */
4518 if ((i.tm.base_opcode | 0xb) == 0xaf)
4519 return 1;
4520
a09f656b 4521 /* outs, xlatb. */
4522 if (base_opcode == 0x6f
4523 || i.tm.base_opcode == 0xd7)
ae531041 4524 return 1;
a09f656b 4525 /* NB: For AMD-specific insns with implicit memory operands,
4526 they're intentionally not covered. */
ae531041
L
4527 }
4528
4529 /* No memory operand. */
4530 if (!i.mem_operands)
4531 return 0;
4532
4533 if (any_vex_p)
4534 {
4535 /* vldmxcsr. */
4536 if (i.tm.base_opcode == 0xae
4537 && i.tm.opcode_modifier.vex
441f6aca 4538 && i.tm.opcode_modifier.opcodespace == SPACE_0F
35648716 4539 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
ae531041
L
4540 && i.tm.extension_opcode == 2)
4541 return 1;
4542 }
389d00a5 4543 else if (i.tm.opcode_modifier.opcodespace == SPACE_BASE)
ae531041
L
4544 {
4545 /* test, not, neg, mul, imul, div, idiv. */
4546 if ((i.tm.base_opcode == 0xf6 || i.tm.base_opcode == 0xf7)
4547 && i.tm.extension_opcode != 1)
4548 return 1;
4549
4550 /* inc, dec. */
4551 if (base_opcode == 0xff && i.tm.extension_opcode <= 1)
4552 return 1;
4553
4554 /* add, or, adc, sbb, and, sub, xor, cmp. */
4555 if (i.tm.base_opcode >= 0x80 && i.tm.base_opcode <= 0x83)
4556 return 1;
4557
ae531041
L
4558 /* rol, ror, rcl, rcr, shl/sal, shr, sar. */
4559 if ((base_opcode == 0xc1
4560 || (i.tm.base_opcode >= 0xd0 && i.tm.base_opcode <= 0xd3))
4561 && i.tm.extension_opcode != 6)
4562 return 1;
4563
ae531041 4564 /* Check for x87 instructions. */
389d00a5 4565 if (base_opcode >= 0xd8 && base_opcode <= 0xdf)
ae531041
L
4566 {
4567 /* Skip fst, fstp, fstenv, fstcw. */
4568 if (i.tm.base_opcode == 0xd9
4569 && (i.tm.extension_opcode == 2
4570 || i.tm.extension_opcode == 3
4571 || i.tm.extension_opcode == 6
4572 || i.tm.extension_opcode == 7))
4573 return 0;
4574
4575 /* Skip fisttp, fist, fistp, fstp. */
4576 if (i.tm.base_opcode == 0xdb
4577 && (i.tm.extension_opcode == 1
4578 || i.tm.extension_opcode == 2
4579 || i.tm.extension_opcode == 3
4580 || i.tm.extension_opcode == 7))
4581 return 0;
4582
4583 /* Skip fisttp, fst, fstp, fsave, fstsw. */
4584 if (i.tm.base_opcode == 0xdd
4585 && (i.tm.extension_opcode == 1
4586 || i.tm.extension_opcode == 2
4587 || i.tm.extension_opcode == 3
4588 || i.tm.extension_opcode == 6
4589 || i.tm.extension_opcode == 7))
4590 return 0;
4591
4592 /* Skip fisttp, fist, fistp, fbstp, fistp. */
4593 if (i.tm.base_opcode == 0xdf
4594 && (i.tm.extension_opcode == 1
4595 || i.tm.extension_opcode == 2
4596 || i.tm.extension_opcode == 3
4597 || i.tm.extension_opcode == 6
4598 || i.tm.extension_opcode == 7))
4599 return 0;
4600
4601 return 1;
4602 }
4603 }
389d00a5
JB
4604 else if (i.tm.opcode_modifier.opcodespace == SPACE_0F)
4605 {
4606 /* bt, bts, btr, btc. */
4607 if (i.tm.base_opcode == 0xba
4608 && (i.tm.extension_opcode >= 4 && i.tm.extension_opcode <= 7))
4609 return 1;
4610
4611 /* cmpxchg8b, cmpxchg16b, xrstors, vmptrld. */
4612 if (i.tm.base_opcode == 0xc7
4613 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
4614 && (i.tm.extension_opcode == 1 || i.tm.extension_opcode == 3
4615 || i.tm.extension_opcode == 6))
4616 return 1;
4617
4618 /* fxrstor, ldmxcsr, xrstor. */
4619 if (i.tm.base_opcode == 0xae
4620 && (i.tm.extension_opcode == 1
4621 || i.tm.extension_opcode == 2
4622 || i.tm.extension_opcode == 5))
4623 return 1;
4624
4625 /* lgdt, lidt, lmsw. */
4626 if (i.tm.base_opcode == 0x01
4627 && (i.tm.extension_opcode == 2
4628 || i.tm.extension_opcode == 3
4629 || i.tm.extension_opcode == 6))
4630 return 1;
4631 }
ae531041
L
4632
4633 dest = i.operands - 1;
4634
4635 /* Check fake imm8 operand and 3 source operands. */
4636 if ((i.tm.opcode_modifier.immext
4637 || i.tm.opcode_modifier.vexsources == VEX3SOURCES)
4638 && i.types[dest].bitfield.imm8)
4639 dest--;
4640
389d00a5
JB
4641 /* add, or, adc, sbb, and, sub, xor, cmp, test, xchg. */
4642 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
ae531041
L
4643 && (base_opcode == 0x1
4644 || base_opcode == 0x9
4645 || base_opcode == 0x11
4646 || base_opcode == 0x19
4647 || base_opcode == 0x21
4648 || base_opcode == 0x29
4649 || base_opcode == 0x31
4650 || base_opcode == 0x39
389d00a5
JB
4651 || (base_opcode | 2) == 0x87))
4652 return 1;
4653
4654 /* xadd. */
4655 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4656 && base_opcode == 0xc1)
ae531041
L
4657 return 1;
4658
4659 /* Check for load instruction. */
4660 return (i.types[dest].bitfield.class != ClassNone
4661 || i.types[dest].bitfield.instance == Accum);
4662}
4663
4664/* Output lfence, 0xfaee8, after instruction. */
4665
4666static void
4667insert_lfence_after (void)
4668{
4669 if (lfence_after_load && load_insn_p ())
4670 {
a09f656b 4671 /* There are also two REP string instructions that require
4672 special treatment. Specifically, the compare string (CMPS)
4673 and scan string (SCAS) instructions set EFLAGS in a manner
4674 that depends on the data being compared/scanned. When used
4675 with a REP prefix, the number of iterations may therefore
4676 vary depending on this data. If the data is a program secret
4677 chosen by the adversary using an LVI method,
4678 then this data-dependent behavior may leak some aspect
4679 of the secret. */
4680 if (((i.tm.base_opcode | 0x1) == 0xa7
4681 || (i.tm.base_opcode | 0x1) == 0xaf)
4682 && i.prefix[REP_PREFIX])
4683 {
4684 as_warn (_("`%s` changes flags which would affect control flow behavior"),
4685 i.tm.name);
4686 }
ae531041
L
4687 char *p = frag_more (3);
4688 *p++ = 0xf;
4689 *p++ = 0xae;
4690 *p = 0xe8;
4691 }
4692}
4693
4694/* Output lfence, 0xfaee8, before instruction. */
4695
4696static void
4697insert_lfence_before (void)
4698{
4699 char *p;
4700
389d00a5 4701 if (i.tm.opcode_modifier.opcodespace != SPACE_BASE)
ae531041
L
4702 return;
4703
4704 if (i.tm.base_opcode == 0xff
4705 && (i.tm.extension_opcode == 2 || i.tm.extension_opcode == 4))
4706 {
4707 /* Insert lfence before indirect branch if needed. */
4708
4709 if (lfence_before_indirect_branch == lfence_branch_none)
4710 return;
4711
4712 if (i.operands != 1)
4713 abort ();
4714
4715 if (i.reg_operands == 1)
4716 {
4717 /* Indirect branch via register. Don't insert lfence with
4718 -mlfence-after-load=yes. */
4719 if (lfence_after_load
4720 || lfence_before_indirect_branch == lfence_branch_memory)
4721 return;
4722 }
4723 else if (i.mem_operands == 1
4724 && lfence_before_indirect_branch != lfence_branch_register)
4725 {
4726 as_warn (_("indirect `%s` with memory operand should be avoided"),
4727 i.tm.name);
4728 return;
4729 }
4730 else
4731 return;
4732
4733 if (last_insn.kind != last_insn_other
4734 && last_insn.seg == now_seg)
4735 {
4736 as_warn_where (last_insn.file, last_insn.line,
4737 _("`%s` skips -mlfence-before-indirect-branch on `%s`"),
4738 last_insn.name, i.tm.name);
4739 return;
4740 }
4741
4742 p = frag_more (3);
4743 *p++ = 0xf;
4744 *p++ = 0xae;
4745 *p = 0xe8;
4746 return;
4747 }
4748
503648e4 4749 /* Output or/not/shl and lfence before near ret. */
ae531041
L
4750 if (lfence_before_ret != lfence_before_ret_none
4751 && (i.tm.base_opcode == 0xc2
503648e4 4752 || i.tm.base_opcode == 0xc3))
ae531041
L
4753 {
4754 if (last_insn.kind != last_insn_other
4755 && last_insn.seg == now_seg)
4756 {
4757 as_warn_where (last_insn.file, last_insn.line,
4758 _("`%s` skips -mlfence-before-ret on `%s`"),
4759 last_insn.name, i.tm.name);
4760 return;
4761 }
a09f656b 4762
a09f656b 4763 /* Near ret ingore operand size override under CPU64. */
503648e4 4764 char prefix = flag_code == CODE_64BIT
4765 ? 0x48
4766 : i.prefix[DATA_PREFIX] ? 0x66 : 0x0;
a09f656b 4767
4768 if (lfence_before_ret == lfence_before_ret_not)
4769 {
4770 /* not: 0xf71424, may add prefix
4771 for operand size override or 64-bit code. */
4772 p = frag_more ((prefix ? 2 : 0) + 6 + 3);
4773 if (prefix)
4774 *p++ = prefix;
ae531041
L
4775 *p++ = 0xf7;
4776 *p++ = 0x14;
4777 *p++ = 0x24;
a09f656b 4778 if (prefix)
4779 *p++ = prefix;
ae531041
L
4780 *p++ = 0xf7;
4781 *p++ = 0x14;
4782 *p++ = 0x24;
4783 }
a09f656b 4784 else
4785 {
4786 p = frag_more ((prefix ? 1 : 0) + 4 + 3);
4787 if (prefix)
4788 *p++ = prefix;
4789 if (lfence_before_ret == lfence_before_ret_or)
4790 {
4791 /* or: 0x830c2400, may add prefix
4792 for operand size override or 64-bit code. */
4793 *p++ = 0x83;
4794 *p++ = 0x0c;
4795 }
4796 else
4797 {
4798 /* shl: 0xc1242400, may add prefix
4799 for operand size override or 64-bit code. */
4800 *p++ = 0xc1;
4801 *p++ = 0x24;
4802 }
4803
4804 *p++ = 0x24;
4805 *p++ = 0x0;
4806 }
4807
ae531041
L
4808 *p++ = 0xf;
4809 *p++ = 0xae;
4810 *p = 0xe8;
4811 }
4812}
4813
252b5132
RH
4814/* This is the guts of the machine-dependent assembler. LINE points to a
4815 machine dependent instruction. This function is supposed to emit
4816 the frags/bytes it assembles to. */
4817
4818void
65da13b5 4819md_assemble (char *line)
252b5132 4820{
40fb9820 4821 unsigned int j;
83b16ac6 4822 char mnemonic[MAX_MNEM_SIZE], mnem_suffix;
d3ce72d0 4823 const insn_template *t;
252b5132 4824
47926f60 4825 /* Initialize globals. */
252b5132 4826 memset (&i, '\0', sizeof (i));
ca5312a2 4827 i.rounding.type = rc_none;
252b5132 4828 for (j = 0; j < MAX_OPERANDS; j++)
1ae12ab7 4829 i.reloc[j] = NO_RELOC;
252b5132
RH
4830 memset (disp_expressions, '\0', sizeof (disp_expressions));
4831 memset (im_expressions, '\0', sizeof (im_expressions));
ce8a8b2f 4832 save_stack_p = save_stack;
252b5132
RH
4833
4834 /* First parse an instruction mnemonic & call i386_operand for the operands.
4835 We assume that the scrubber has arranged it so that line[0] is the valid
47926f60 4836 start of a (possibly prefixed) mnemonic. */
252b5132 4837
29b0f896
AM
4838 line = parse_insn (line, mnemonic);
4839 if (line == NULL)
4840 return;
83b16ac6 4841 mnem_suffix = i.suffix;
252b5132 4842
29b0f896 4843 line = parse_operands (line, mnemonic);
ee86248c 4844 this_operand = -1;
8325cc63
JB
4845 xfree (i.memop1_string);
4846 i.memop1_string = NULL;
29b0f896
AM
4847 if (line == NULL)
4848 return;
252b5132 4849
29b0f896
AM
4850 /* Now we've parsed the mnemonic into a set of templates, and have the
4851 operands at hand. */
4852
b630c145 4853 /* All Intel opcodes have reversed operands except for "bound", "enter",
c0e54661
JB
4854 "invlpg*", "monitor*", "mwait*", "tpause", "umwait", "pvalidate",
4855 "rmpadjust", and "rmpupdate". We also don't reverse intersegment "jmp"
4856 and "call" instructions with 2 immediate operands so that the immediate
4857 segment precedes the offset consistently in Intel and AT&T modes. */
4d456e3d
L
4858 if (intel_syntax
4859 && i.operands > 1
29b0f896 4860 && (strcmp (mnemonic, "bound") != 0)
c0e54661 4861 && (strncmp (mnemonic, "invlpg", 6) != 0)
d34049e8
ML
4862 && !startswith (mnemonic, "monitor")
4863 && !startswith (mnemonic, "mwait")
c0e54661 4864 && (strcmp (mnemonic, "pvalidate") != 0)
d34049e8 4865 && !startswith (mnemonic, "rmp")
b630c145
JB
4866 && (strcmp (mnemonic, "tpause") != 0)
4867 && (strcmp (mnemonic, "umwait") != 0)
40fb9820
L
4868 && !(operand_type_check (i.types[0], imm)
4869 && operand_type_check (i.types[1], imm)))
29b0f896
AM
4870 swap_operands ();
4871
ec56d5c0
JB
4872 /* The order of the immediates should be reversed
4873 for 2 immediates extrq and insertq instructions */
4874 if (i.imm_operands == 2
4875 && (strcmp (mnemonic, "extrq") == 0
4876 || strcmp (mnemonic, "insertq") == 0))
4877 swap_2_operands (0, 1);
4878
29b0f896
AM
4879 if (i.imm_operands)
4880 optimize_imm ();
4881
9386188e
JB
4882 if (i.disp_operands && !want_disp32 (current_templates->start)
4883 && (!current_templates->start->opcode_modifier.jump
4884 || i.jumpabsolute || i.types[0].bitfield.baseindex))
cce08655
JB
4885 {
4886 for (j = 0; j < i.operands; ++j)
4887 {
4888 const expressionS *exp = i.op[j].disps;
4889
4890 if (!operand_type_check (i.types[j], disp))
4891 continue;
4892
4893 if (exp->X_op != O_constant)
4894 continue;
4895
4896 /* Since displacement is signed extended to 64bit, don't allow
a775efc8 4897 disp32 if it is out of range. */
cce08655
JB
4898 if (fits_in_signed_long (exp->X_add_number))
4899 continue;
4900
a775efc8 4901 i.types[j].bitfield.disp32 = 0;
cce08655
JB
4902 if (i.types[j].bitfield.baseindex)
4903 {
f493c217
AM
4904 as_bad (_("0x%" PRIx64 " out of range of signed 32bit displacement"),
4905 (uint64_t) exp->X_add_number);
cce08655
JB
4906 return;
4907 }
4908 }
4909 }
4910
b300c311
L
4911 /* Don't optimize displacement for movabs since it only takes 64bit
4912 displacement. */
4913 if (i.disp_operands
1a42a9fe 4914 && i.disp_encoding <= disp_encoding_8bit
862be3fb
L
4915 && (flag_code != CODE_64BIT
4916 || strcmp (mnemonic, "movabs") != 0))
4917 optimize_disp ();
29b0f896
AM
4918
4919 /* Next, we find a template that matches the given insn,
4920 making sure the overlap of the given operands types is consistent
4921 with the template operand types. */
252b5132 4922
83b16ac6 4923 if (!(t = match_template (mnem_suffix)))
29b0f896 4924 return;
252b5132 4925
7bab8ab5 4926 if (sse_check != check_none
ffb86450
JB
4927 /* The opcode space check isn't strictly needed; it's there only to
4928 bypass the logic below when easily possible. */
4929 && t->opcode_modifier.opcodespace >= SPACE_0F
4930 && t->opcode_modifier.opcodespace <= SPACE_0F3A
4931 && !i.tm.cpu_flags.bitfield.cpusse4a
4932 && !is_any_vex_encoding (t))
daf50ae7 4933 {
ffb86450
JB
4934 bool simd = false;
4935
4936 for (j = 0; j < t->operands; ++j)
4937 {
4938 if (t->operand_types[j].bitfield.class == RegMMX)
4939 break;
4940 if (t->operand_types[j].bitfield.class == RegSIMD)
4941 simd = true;
4942 }
4943
4944 if (j >= t->operands && simd)
4945 (sse_check == check_warning
4946 ? as_warn
4947 : as_bad) (_("SSE instruction `%s' is used"), i.tm.name);
daf50ae7
L
4948 }
4949
40fb9820 4950 if (i.tm.opcode_modifier.fwait)
29b0f896
AM
4951 if (!add_prefix (FWAIT_OPCODE))
4952 return;
252b5132 4953
d5de92cf 4954 /* Check if REP prefix is OK. */
742732c7 4955 if (i.rep_prefix && i.tm.opcode_modifier.prefixok != PrefixRep)
d5de92cf
L
4956 {
4957 as_bad (_("invalid instruction `%s' after `%s'"),
4958 i.tm.name, i.rep_prefix);
4959 return;
4960 }
4961
c1ba0266
L
4962 /* Check for lock without a lockable instruction. Destination operand
4963 must be memory unless it is xchg (0x86). */
c32fa91d 4964 if (i.prefix[LOCK_PREFIX]
742732c7 4965 && (i.tm.opcode_modifier.prefixok < PrefixLock
c1ba0266
L
4966 || i.mem_operands == 0
4967 || (i.tm.base_opcode != 0x86
8dc0818e 4968 && !(i.flags[i.operands - 1] & Operand_Mem))))
c32fa91d
L
4969 {
4970 as_bad (_("expecting lockable instruction after `lock'"));
4971 return;
4972 }
4973
40d231b4
JB
4974 /* Check for data size prefix on VEX/XOP/EVEX encoded and SIMD insns. */
4975 if (i.prefix[DATA_PREFIX]
4976 && (is_any_vex_encoding (&i.tm)
4977 || i.tm.operand_types[i.imm_operands].bitfield.class >= RegMMX
4978 || i.tm.operand_types[i.imm_operands + 1].bitfield.class >= RegMMX))
7a8655d2
JB
4979 {
4980 as_bad (_("data size prefix invalid with `%s'"), i.tm.name);
4981 return;
4982 }
4983
42164a71 4984 /* Check if HLE prefix is OK. */
165de32a 4985 if (i.hle_prefix && !check_hle ())
42164a71
L
4986 return;
4987
7e8b059b
L
4988 /* Check BND prefix. */
4989 if (i.bnd_prefix && !i.tm.opcode_modifier.bndprefixok)
4990 as_bad (_("expecting valid branch instruction after `bnd'"));
4991
04ef582a 4992 /* Check NOTRACK prefix. */
742732c7 4993 if (i.notrack_prefix && i.tm.opcode_modifier.prefixok != PrefixNoTrack)
9fef80d6 4994 as_bad (_("expecting indirect branch instruction after `notrack'"));
04ef582a 4995
327e8c42
JB
4996 if (i.tm.cpu_flags.bitfield.cpumpx)
4997 {
4998 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
4999 as_bad (_("32-bit address isn't allowed in 64-bit MPX instructions."));
5000 else if (flag_code != CODE_16BIT
5001 ? i.prefix[ADDR_PREFIX]
5002 : i.mem_operands && !i.prefix[ADDR_PREFIX])
5003 as_bad (_("16-bit address isn't allowed in MPX instructions"));
5004 }
7e8b059b
L
5005
5006 /* Insert BND prefix. */
76d3a78a
JB
5007 if (add_bnd_prefix && i.tm.opcode_modifier.bndprefixok)
5008 {
5009 if (!i.prefix[BND_PREFIX])
5010 add_prefix (BND_PREFIX_OPCODE);
5011 else if (i.prefix[BND_PREFIX] != BND_PREFIX_OPCODE)
5012 {
5013 as_warn (_("replacing `rep'/`repe' prefix by `bnd'"));
5014 i.prefix[BND_PREFIX] = BND_PREFIX_OPCODE;
5015 }
5016 }
7e8b059b 5017
29b0f896 5018 /* Check string instruction segment overrides. */
51c8edf6 5019 if (i.tm.opcode_modifier.isstring >= IS_STRING_ES_OP0)
29b0f896 5020 {
51c8edf6 5021 gas_assert (i.mem_operands);
29b0f896 5022 if (!check_string ())
5dd0794d 5023 return;
fc0763e6 5024 i.disp_operands = 0;
29b0f896 5025 }
5dd0794d 5026
b6f8c7c4
L
5027 if (optimize && !i.no_optimize && i.tm.opcode_modifier.optimize)
5028 optimize_encoding ();
5029
c8480b58
L
5030 if (use_unaligned_vector_move)
5031 encode_with_unaligned_vector_move ();
5032
29b0f896
AM
5033 if (!process_suffix ())
5034 return;
e413e4e9 5035
921eafea 5036 /* Update operand types and check extended states. */
bc0844ae 5037 for (j = 0; j < i.operands; j++)
921eafea
L
5038 {
5039 i.types[j] = operand_type_and (i.types[j], i.tm.operand_types[j]);
3d70986f 5040 switch (i.tm.operand_types[j].bitfield.class)
921eafea
L
5041 {
5042 default:
5043 break;
5044 case RegMMX:
5045 i.xstate |= xstate_mmx;
5046 break;
5047 case RegMask:
32930e4e 5048 i.xstate |= xstate_mask;
921eafea
L
5049 break;
5050 case RegSIMD:
3d70986f 5051 if (i.tm.operand_types[j].bitfield.tmmword)
921eafea 5052 i.xstate |= xstate_tmm;
3d70986f 5053 else if (i.tm.operand_types[j].bitfield.zmmword)
921eafea 5054 i.xstate |= xstate_zmm;
3d70986f 5055 else if (i.tm.operand_types[j].bitfield.ymmword)
921eafea 5056 i.xstate |= xstate_ymm;
3d70986f 5057 else if (i.tm.operand_types[j].bitfield.xmmword)
921eafea
L
5058 i.xstate |= xstate_xmm;
5059 break;
5060 }
5061 }
bc0844ae 5062
29b0f896
AM
5063 /* Make still unresolved immediate matches conform to size of immediate
5064 given in i.suffix. */
5065 if (!finalize_imm ())
5066 return;
252b5132 5067
40fb9820 5068 if (i.types[0].bitfield.imm1)
29b0f896 5069 i.imm_operands = 0; /* kludge for shift insns. */
252b5132 5070
9afe6eb8
L
5071 /* We only need to check those implicit registers for instructions
5072 with 3 operands or less. */
5073 if (i.operands <= 3)
5074 for (j = 0; j < i.operands; j++)
75e5731b
JB
5075 if (i.types[j].bitfield.instance != InstanceNone
5076 && !i.types[j].bitfield.xmmword)
9afe6eb8 5077 i.reg_operands--;
40fb9820 5078
29b0f896
AM
5079 /* For insns with operands there are more diddles to do to the opcode. */
5080 if (i.operands)
5081 {
5082 if (!process_operands ())
5083 return;
5084 }
8c190ce0 5085 else if (!quiet_warnings && i.tm.opcode_modifier.ugh)
29b0f896
AM
5086 {
5087 /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc. */
5088 as_warn (_("translating to `%sp'"), i.tm.name);
5089 }
252b5132 5090
7a8655d2 5091 if (is_any_vex_encoding (&i.tm))
9e5e5283 5092 {
c1dc7af5 5093 if (!cpu_arch_flags.bitfield.cpui286)
9e5e5283 5094 {
c1dc7af5 5095 as_bad (_("instruction `%s' isn't supported outside of protected mode."),
9e5e5283
L
5096 i.tm.name);
5097 return;
5098 }
c0f3af97 5099
0b9404fd
JB
5100 /* Check for explicit REX prefix. */
5101 if (i.prefix[REX_PREFIX] || i.rex_encoding)
5102 {
5103 as_bad (_("REX prefix invalid with `%s'"), i.tm.name);
5104 return;
5105 }
5106
9e5e5283
L
5107 if (i.tm.opcode_modifier.vex)
5108 build_vex_prefix (t);
5109 else
5110 build_evex_prefix ();
0b9404fd
JB
5111
5112 /* The individual REX.RXBW bits got consumed. */
5113 i.rex &= REX_OPCODE;
9e5e5283 5114 }
43234a1e 5115
5dd85c99
SP
5116 /* Handle conversion of 'int $3' --> special int3 insn. XOP or FMA4
5117 instructions may define INT_OPCODE as well, so avoid this corner
5118 case for those instructions that use MODRM. */
389d00a5
JB
5119 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
5120 && i.tm.base_opcode == INT_OPCODE
a6461c02
SP
5121 && !i.tm.opcode_modifier.modrm
5122 && i.op[0].imms->X_add_number == 3)
29b0f896
AM
5123 {
5124 i.tm.base_opcode = INT3_OPCODE;
5125 i.imm_operands = 0;
5126 }
252b5132 5127
0cfa3eb3
JB
5128 if ((i.tm.opcode_modifier.jump == JUMP
5129 || i.tm.opcode_modifier.jump == JUMP_BYTE
5130 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896
AM
5131 && i.op[0].disps->X_op == O_constant)
5132 {
5133 /* Convert "jmp constant" (and "call constant") to a jump (call) to
5134 the absolute address given by the constant. Since ix86 jumps and
5135 calls are pc relative, we need to generate a reloc. */
5136 i.op[0].disps->X_add_symbol = &abs_symbol;
5137 i.op[0].disps->X_op = O_symbol;
5138 }
252b5132 5139
29b0f896
AM
5140 /* For 8 bit registers we need an empty rex prefix. Also if the
5141 instruction already has a prefix, we need to convert old
5142 registers to new ones. */
773f551c 5143
bab6aec1 5144 if ((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte
29b0f896 5145 && (i.op[0].regs->reg_flags & RegRex64) != 0)
bab6aec1 5146 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte
29b0f896 5147 && (i.op[1].regs->reg_flags & RegRex64) != 0)
bab6aec1
JB
5148 || (((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte)
5149 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte))
29b0f896
AM
5150 && i.rex != 0))
5151 {
5152 int x;
726c5dcd 5153
29b0f896
AM
5154 i.rex |= REX_OPCODE;
5155 for (x = 0; x < 2; x++)
5156 {
5157 /* Look for 8 bit operand that uses old registers. */
bab6aec1 5158 if (i.types[x].bitfield.class == Reg && i.types[x].bitfield.byte
29b0f896 5159 && (i.op[x].regs->reg_flags & RegRex64) == 0)
773f551c 5160 {
3f93af61 5161 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
29b0f896
AM
5162 /* In case it is "hi" register, give up. */
5163 if (i.op[x].regs->reg_num > 3)
a540244d 5164 as_bad (_("can't encode register '%s%s' in an "
4eed87de 5165 "instruction requiring REX prefix."),
a540244d 5166 register_prefix, i.op[x].regs->reg_name);
773f551c 5167
29b0f896
AM
5168 /* Otherwise it is equivalent to the extended register.
5169 Since the encoding doesn't change this is merely
5170 cosmetic cleanup for debug output. */
5171
5172 i.op[x].regs = i.op[x].regs + 8;
773f551c 5173 }
29b0f896
AM
5174 }
5175 }
773f551c 5176
6b6b6807
L
5177 if (i.rex == 0 && i.rex_encoding)
5178 {
5179 /* Check if we can add a REX_OPCODE byte. Look for 8 bit operand
3f93af61 5180 that uses legacy register. If it is "hi" register, don't add
6b6b6807
L
5181 the REX_OPCODE byte. */
5182 int x;
5183 for (x = 0; x < 2; x++)
bab6aec1 5184 if (i.types[x].bitfield.class == Reg
6b6b6807
L
5185 && i.types[x].bitfield.byte
5186 && (i.op[x].regs->reg_flags & RegRex64) == 0
5187 && i.op[x].regs->reg_num > 3)
5188 {
3f93af61 5189 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
5b7c81bd 5190 i.rex_encoding = false;
6b6b6807
L
5191 break;
5192 }
5193
5194 if (i.rex_encoding)
5195 i.rex = REX_OPCODE;
5196 }
5197
7ab9ffdd 5198 if (i.rex != 0)
29b0f896
AM
5199 add_prefix (REX_OPCODE | i.rex);
5200
ae531041
L
5201 insert_lfence_before ();
5202
29b0f896
AM
5203 /* We are ready to output the insn. */
5204 output_insn ();
e379e5f3 5205
ae531041
L
5206 insert_lfence_after ();
5207
e379e5f3
L
5208 last_insn.seg = now_seg;
5209
5210 if (i.tm.opcode_modifier.isprefix)
5211 {
5212 last_insn.kind = last_insn_prefix;
5213 last_insn.name = i.tm.name;
5214 last_insn.file = as_where (&last_insn.line);
5215 }
5216 else
5217 last_insn.kind = last_insn_other;
29b0f896
AM
5218}
5219
5220static char *
e3bb37b5 5221parse_insn (char *line, char *mnemonic)
29b0f896
AM
5222{
5223 char *l = line;
5224 char *token_start = l;
5225 char *mnem_p;
5c6af06e 5226 int supported;
d3ce72d0 5227 const insn_template *t;
b6169b20 5228 char *dot_p = NULL;
29b0f896 5229
29b0f896
AM
5230 while (1)
5231 {
5232 mnem_p = mnemonic;
5233 while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
5234 {
b6169b20
L
5235 if (*mnem_p == '.')
5236 dot_p = mnem_p;
29b0f896
AM
5237 mnem_p++;
5238 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
45288df1 5239 {
29b0f896
AM
5240 as_bad (_("no such instruction: `%s'"), token_start);
5241 return NULL;
5242 }
5243 l++;
5244 }
5245 if (!is_space_char (*l)
5246 && *l != END_OF_INSN
e44823cf
JB
5247 && (intel_syntax
5248 || (*l != PREFIX_SEPARATOR
5249 && *l != ',')))
29b0f896
AM
5250 {
5251 as_bad (_("invalid character %s in mnemonic"),
5252 output_invalid (*l));
5253 return NULL;
5254 }
5255 if (token_start == l)
5256 {
e44823cf 5257 if (!intel_syntax && *l == PREFIX_SEPARATOR)
29b0f896
AM
5258 as_bad (_("expecting prefix; got nothing"));
5259 else
5260 as_bad (_("expecting mnemonic; got nothing"));
5261 return NULL;
5262 }
45288df1 5263
29b0f896 5264 /* Look up instruction (or prefix) via hash table. */
629310ab 5265 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
47926f60 5266
29b0f896
AM
5267 if (*l != END_OF_INSN
5268 && (!is_space_char (*l) || l[1] != END_OF_INSN)
5269 && current_templates
40fb9820 5270 && current_templates->start->opcode_modifier.isprefix)
29b0f896 5271 {
c6fb90c8 5272 if (!cpu_flags_check_cpu64 (current_templates->start->cpu_flags))
2dd88dca
JB
5273 {
5274 as_bad ((flag_code != CODE_64BIT
5275 ? _("`%s' is only supported in 64-bit mode")
5276 : _("`%s' is not supported in 64-bit mode")),
5277 current_templates->start->name);
5278 return NULL;
5279 }
29b0f896
AM
5280 /* If we are in 16-bit mode, do not allow addr16 or data16.
5281 Similarly, in 32-bit mode, do not allow addr32 or data32. */
673fe0f0
JB
5282 if ((current_templates->start->opcode_modifier.size == SIZE16
5283 || current_templates->start->opcode_modifier.size == SIZE32)
29b0f896 5284 && flag_code != CODE_64BIT
673fe0f0 5285 && ((current_templates->start->opcode_modifier.size == SIZE32)
29b0f896
AM
5286 ^ (flag_code == CODE_16BIT)))
5287 {
5288 as_bad (_("redundant %s prefix"),
5289 current_templates->start->name);
5290 return NULL;
45288df1 5291 }
31184569
JB
5292
5293 if (current_templates->start->base_opcode == PSEUDO_PREFIX)
29b0f896 5294 {
86fa6981 5295 /* Handle pseudo prefixes. */
31184569 5296 switch (current_templates->start->extension_opcode)
86fa6981 5297 {
41eb8e88 5298 case Prefix_Disp8:
86fa6981
L
5299 /* {disp8} */
5300 i.disp_encoding = disp_encoding_8bit;
5301 break;
41eb8e88
L
5302 case Prefix_Disp16:
5303 /* {disp16} */
5304 i.disp_encoding = disp_encoding_16bit;
5305 break;
5306 case Prefix_Disp32:
86fa6981
L
5307 /* {disp32} */
5308 i.disp_encoding = disp_encoding_32bit;
5309 break;
41eb8e88 5310 case Prefix_Load:
86fa6981
L
5311 /* {load} */
5312 i.dir_encoding = dir_encoding_load;
5313 break;
41eb8e88 5314 case Prefix_Store:
86fa6981
L
5315 /* {store} */
5316 i.dir_encoding = dir_encoding_store;
5317 break;
41eb8e88 5318 case Prefix_VEX:
42e04b36
L
5319 /* {vex} */
5320 i.vec_encoding = vex_encoding_vex;
86fa6981 5321 break;
41eb8e88 5322 case Prefix_VEX3:
86fa6981
L
5323 /* {vex3} */
5324 i.vec_encoding = vex_encoding_vex3;
5325 break;
41eb8e88 5326 case Prefix_EVEX:
86fa6981
L
5327 /* {evex} */
5328 i.vec_encoding = vex_encoding_evex;
5329 break;
41eb8e88 5330 case Prefix_REX:
6b6b6807 5331 /* {rex} */
5b7c81bd 5332 i.rex_encoding = true;
6b6b6807 5333 break;
41eb8e88 5334 case Prefix_NoOptimize:
b6f8c7c4 5335 /* {nooptimize} */
5b7c81bd 5336 i.no_optimize = true;
b6f8c7c4 5337 break;
86fa6981
L
5338 default:
5339 abort ();
5340 }
5341 }
5342 else
5343 {
5344 /* Add prefix, checking for repeated prefixes. */
4e9ac44a 5345 switch (add_prefix (current_templates->start->base_opcode))
86fa6981 5346 {
4e9ac44a
L
5347 case PREFIX_EXIST:
5348 return NULL;
5349 case PREFIX_DS:
d777820b 5350 if (current_templates->start->cpu_flags.bitfield.cpuibt)
4e9ac44a
L
5351 i.notrack_prefix = current_templates->start->name;
5352 break;
5353 case PREFIX_REP:
5354 if (current_templates->start->cpu_flags.bitfield.cpuhle)
5355 i.hle_prefix = current_templates->start->name;
5356 else if (current_templates->start->cpu_flags.bitfield.cpumpx)
5357 i.bnd_prefix = current_templates->start->name;
5358 else
5359 i.rep_prefix = current_templates->start->name;
5360 break;
5361 default:
5362 break;
86fa6981 5363 }
29b0f896
AM
5364 }
5365 /* Skip past PREFIX_SEPARATOR and reset token_start. */
5366 token_start = ++l;
5367 }
5368 else
5369 break;
5370 }
45288df1 5371
30a55f88 5372 if (!current_templates)
b6169b20 5373 {
07d5e953
JB
5374 /* Deprecated functionality (new code should use pseudo-prefixes instead):
5375 Check if we should swap operand or force 32bit displacement in
f8a5c266 5376 encoding. */
30a55f88 5377 if (mnem_p - 2 == dot_p && dot_p[1] == 's')
64c49ab3 5378 i.dir_encoding = dir_encoding_swap;
8d63c93e 5379 else if (mnem_p - 3 == dot_p
a501d77e
L
5380 && dot_p[1] == 'd'
5381 && dot_p[2] == '8')
5382 i.disp_encoding = disp_encoding_8bit;
8d63c93e 5383 else if (mnem_p - 4 == dot_p
f8a5c266
L
5384 && dot_p[1] == 'd'
5385 && dot_p[2] == '3'
5386 && dot_p[3] == '2')
a501d77e 5387 i.disp_encoding = disp_encoding_32bit;
30a55f88
L
5388 else
5389 goto check_suffix;
5390 mnem_p = dot_p;
5391 *dot_p = '\0';
629310ab 5392 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
b6169b20
L
5393 }
5394
29b0f896
AM
5395 if (!current_templates)
5396 {
dc1e8a47 5397 check_suffix:
1c529385 5398 if (mnem_p > mnemonic)
29b0f896 5399 {
1c529385
LH
5400 /* See if we can get a match by trimming off a suffix. */
5401 switch (mnem_p[-1])
29b0f896 5402 {
1c529385
LH
5403 case WORD_MNEM_SUFFIX:
5404 if (intel_syntax && (intel_float_operand (mnemonic) & 2))
29b0f896
AM
5405 i.suffix = SHORT_MNEM_SUFFIX;
5406 else
1c529385
LH
5407 /* Fall through. */
5408 case BYTE_MNEM_SUFFIX:
5409 case QWORD_MNEM_SUFFIX:
5410 i.suffix = mnem_p[-1];
29b0f896 5411 mnem_p[-1] = '\0';
fe0e921f
AM
5412 current_templates
5413 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5414 break;
5415 case SHORT_MNEM_SUFFIX:
5416 case LONG_MNEM_SUFFIX:
5417 if (!intel_syntax)
5418 {
5419 i.suffix = mnem_p[-1];
5420 mnem_p[-1] = '\0';
fe0e921f
AM
5421 current_templates
5422 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5423 }
5424 break;
5425
5426 /* Intel Syntax. */
5427 case 'd':
5428 if (intel_syntax)
5429 {
5430 if (intel_float_operand (mnemonic) == 1)
5431 i.suffix = SHORT_MNEM_SUFFIX;
5432 else
5433 i.suffix = LONG_MNEM_SUFFIX;
5434 mnem_p[-1] = '\0';
fe0e921f
AM
5435 current_templates
5436 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5437 }
5438 break;
29b0f896 5439 }
29b0f896 5440 }
1c529385 5441
29b0f896
AM
5442 if (!current_templates)
5443 {
5444 as_bad (_("no such instruction: `%s'"), token_start);
5445 return NULL;
5446 }
5447 }
252b5132 5448
0cfa3eb3
JB
5449 if (current_templates->start->opcode_modifier.jump == JUMP
5450 || current_templates->start->opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
5451 {
5452 /* Check for a branch hint. We allow ",pt" and ",pn" for
5453 predict taken and predict not taken respectively.
5454 I'm not sure that branch hints actually do anything on loop
5455 and jcxz insns (JumpByte) for current Pentium4 chips. They
5456 may work in the future and it doesn't hurt to accept them
5457 now. */
5458 if (l[0] == ',' && l[1] == 'p')
5459 {
5460 if (l[2] == 't')
5461 {
5462 if (!add_prefix (DS_PREFIX_OPCODE))
5463 return NULL;
5464 l += 3;
5465 }
5466 else if (l[2] == 'n')
5467 {
5468 if (!add_prefix (CS_PREFIX_OPCODE))
5469 return NULL;
5470 l += 3;
5471 }
5472 }
5473 }
5474 /* Any other comma loses. */
5475 if (*l == ',')
5476 {
5477 as_bad (_("invalid character %s in mnemonic"),
5478 output_invalid (*l));
5479 return NULL;
5480 }
252b5132 5481
29b0f896 5482 /* Check if instruction is supported on specified architecture. */
5c6af06e
JB
5483 supported = 0;
5484 for (t = current_templates->start; t < current_templates->end; ++t)
5485 {
c0f3af97
L
5486 supported |= cpu_flags_match (t);
5487 if (supported == CPU_FLAGS_PERFECT_MATCH)
548d0ee6
JB
5488 {
5489 if (!cpu_arch_flags.bitfield.cpui386 && (flag_code != CODE_16BIT))
5490 as_warn (_("use .code16 to ensure correct addressing mode"));
3629bb00 5491
548d0ee6
JB
5492 return l;
5493 }
29b0f896 5494 }
3629bb00 5495
548d0ee6
JB
5496 if (!(supported & CPU_FLAGS_64BIT_MATCH))
5497 as_bad (flag_code == CODE_64BIT
5498 ? _("`%s' is not supported in 64-bit mode")
5499 : _("`%s' is only supported in 64-bit mode"),
5500 current_templates->start->name);
5501 else
5502 as_bad (_("`%s' is not supported on `%s%s'"),
5503 current_templates->start->name,
5504 cpu_arch_name ? cpu_arch_name : default_arch,
5505 cpu_sub_arch_name ? cpu_sub_arch_name : "");
252b5132 5506
548d0ee6 5507 return NULL;
29b0f896 5508}
252b5132 5509
29b0f896 5510static char *
e3bb37b5 5511parse_operands (char *l, const char *mnemonic)
29b0f896
AM
5512{
5513 char *token_start;
3138f287 5514
29b0f896
AM
5515 /* 1 if operand is pending after ','. */
5516 unsigned int expecting_operand = 0;
252b5132 5517
29b0f896
AM
5518 while (*l != END_OF_INSN)
5519 {
e68c3d59
JB
5520 /* Non-zero if operand parens not balanced. */
5521 unsigned int paren_not_balanced = 0;
5522 /* True if inside double quotes. */
5523 bool in_quotes = false;
5524
29b0f896
AM
5525 /* Skip optional white space before operand. */
5526 if (is_space_char (*l))
5527 ++l;
d02603dc 5528 if (!is_operand_char (*l) && *l != END_OF_INSN && *l != '"')
29b0f896
AM
5529 {
5530 as_bad (_("invalid character %s before operand %d"),
5531 output_invalid (*l),
5532 i.operands + 1);
5533 return NULL;
5534 }
d02603dc 5535 token_start = l; /* After white space. */
e68c3d59 5536 while (in_quotes || paren_not_balanced || *l != ',')
29b0f896
AM
5537 {
5538 if (*l == END_OF_INSN)
5539 {
e68c3d59
JB
5540 if (in_quotes)
5541 {
5542 as_bad (_("unbalanced double quotes in operand %d."),
5543 i.operands + 1);
5544 return NULL;
5545 }
29b0f896
AM
5546 if (paren_not_balanced)
5547 {
98ff9f1c
JB
5548 know (!intel_syntax);
5549 as_bad (_("unbalanced parenthesis in operand %d."),
5550 i.operands + 1);
29b0f896
AM
5551 return NULL;
5552 }
5553 else
5554 break; /* we are done */
5555 }
e68c3d59
JB
5556 else if (*l == '\\' && l[1] == '"')
5557 ++l;
5558 else if (*l == '"')
5559 in_quotes = !in_quotes;
5560 else if (!in_quotes && !is_operand_char (*l) && !is_space_char (*l))
29b0f896
AM
5561 {
5562 as_bad (_("invalid character %s in operand %d"),
5563 output_invalid (*l),
5564 i.operands + 1);
5565 return NULL;
5566 }
e68c3d59 5567 if (!intel_syntax && !in_quotes)
29b0f896
AM
5568 {
5569 if (*l == '(')
5570 ++paren_not_balanced;
5571 if (*l == ')')
5572 --paren_not_balanced;
5573 }
29b0f896
AM
5574 l++;
5575 }
5576 if (l != token_start)
5577 { /* Yes, we've read in another operand. */
5578 unsigned int operand_ok;
5579 this_operand = i.operands++;
5580 if (i.operands > MAX_OPERANDS)
5581 {
5582 as_bad (_("spurious operands; (%d operands/instruction max)"),
5583 MAX_OPERANDS);
5584 return NULL;
5585 }
9d46ce34 5586 i.types[this_operand].bitfield.unspecified = 1;
29b0f896
AM
5587 /* Now parse operand adding info to 'i' as we go along. */
5588 END_STRING_AND_SAVE (l);
5589
1286ab78
L
5590 if (i.mem_operands > 1)
5591 {
5592 as_bad (_("too many memory references for `%s'"),
5593 mnemonic);
5594 return 0;
5595 }
5596
29b0f896
AM
5597 if (intel_syntax)
5598 operand_ok =
5599 i386_intel_operand (token_start,
5600 intel_float_operand (mnemonic));
5601 else
a7619375 5602 operand_ok = i386_att_operand (token_start);
29b0f896
AM
5603
5604 RESTORE_END_STRING (l);
5605 if (!operand_ok)
5606 return NULL;
5607 }
5608 else
5609 {
5610 if (expecting_operand)
5611 {
5612 expecting_operand_after_comma:
5613 as_bad (_("expecting operand after ','; got nothing"));
5614 return NULL;
5615 }
5616 if (*l == ',')
5617 {
5618 as_bad (_("expecting operand before ','; got nothing"));
5619 return NULL;
5620 }
5621 }
7f3f1ea2 5622
29b0f896
AM
5623 /* Now *l must be either ',' or END_OF_INSN. */
5624 if (*l == ',')
5625 {
5626 if (*++l == END_OF_INSN)
5627 {
5628 /* Just skip it, if it's \n complain. */
5629 goto expecting_operand_after_comma;
5630 }
5631 expecting_operand = 1;
5632 }
5633 }
5634 return l;
5635}
7f3f1ea2 5636
050dfa73 5637static void
783c187b 5638swap_2_operands (unsigned int xchg1, unsigned int xchg2)
050dfa73
MM
5639{
5640 union i386_op temp_op;
40fb9820 5641 i386_operand_type temp_type;
c48dadc9 5642 unsigned int temp_flags;
050dfa73 5643 enum bfd_reloc_code_real temp_reloc;
4eed87de 5644
050dfa73
MM
5645 temp_type = i.types[xchg2];
5646 i.types[xchg2] = i.types[xchg1];
5647 i.types[xchg1] = temp_type;
c48dadc9
JB
5648
5649 temp_flags = i.flags[xchg2];
5650 i.flags[xchg2] = i.flags[xchg1];
5651 i.flags[xchg1] = temp_flags;
5652
050dfa73
MM
5653 temp_op = i.op[xchg2];
5654 i.op[xchg2] = i.op[xchg1];
5655 i.op[xchg1] = temp_op;
c48dadc9 5656
050dfa73
MM
5657 temp_reloc = i.reloc[xchg2];
5658 i.reloc[xchg2] = i.reloc[xchg1];
5659 i.reloc[xchg1] = temp_reloc;
43234a1e 5660
6225c532 5661 if (i.mask.reg)
43234a1e 5662 {
6225c532
JB
5663 if (i.mask.operand == xchg1)
5664 i.mask.operand = xchg2;
5665 else if (i.mask.operand == xchg2)
5666 i.mask.operand = xchg1;
43234a1e 5667 }
a5748e0d 5668 if (i.broadcast.type || i.broadcast.bytes)
43234a1e 5669 {
5273a3cd
JB
5670 if (i.broadcast.operand == xchg1)
5671 i.broadcast.operand = xchg2;
5672 else if (i.broadcast.operand == xchg2)
5673 i.broadcast.operand = xchg1;
43234a1e 5674 }
050dfa73
MM
5675}
5676
29b0f896 5677static void
e3bb37b5 5678swap_operands (void)
29b0f896 5679{
b7c61d9a 5680 switch (i.operands)
050dfa73 5681 {
c0f3af97 5682 case 5:
b7c61d9a 5683 case 4:
4d456e3d 5684 swap_2_operands (1, i.operands - 2);
1a0670f3 5685 /* Fall through. */
b7c61d9a
L
5686 case 3:
5687 case 2:
4d456e3d 5688 swap_2_operands (0, i.operands - 1);
b7c61d9a
L
5689 break;
5690 default:
5691 abort ();
29b0f896 5692 }
29b0f896
AM
5693
5694 if (i.mem_operands == 2)
5695 {
5e042380 5696 const reg_entry *temp_seg;
29b0f896
AM
5697 temp_seg = i.seg[0];
5698 i.seg[0] = i.seg[1];
5699 i.seg[1] = temp_seg;
5700 }
5701}
252b5132 5702
29b0f896
AM
5703/* Try to ensure constant immediates are represented in the smallest
5704 opcode possible. */
5705static void
e3bb37b5 5706optimize_imm (void)
29b0f896
AM
5707{
5708 char guess_suffix = 0;
5709 int op;
252b5132 5710
29b0f896
AM
5711 if (i.suffix)
5712 guess_suffix = i.suffix;
5713 else if (i.reg_operands)
5714 {
5715 /* Figure out a suffix from the last register operand specified.
75e5731b
JB
5716 We can't do this properly yet, i.e. excluding special register
5717 instances, but the following works for instructions with
5718 immediates. In any case, we can't set i.suffix yet. */
29b0f896 5719 for (op = i.operands; --op >= 0;)
bab6aec1
JB
5720 if (i.types[op].bitfield.class != Reg)
5721 continue;
5722 else if (i.types[op].bitfield.byte)
7ab9ffdd 5723 {
40fb9820
L
5724 guess_suffix = BYTE_MNEM_SUFFIX;
5725 break;
5726 }
bab6aec1 5727 else if (i.types[op].bitfield.word)
252b5132 5728 {
40fb9820
L
5729 guess_suffix = WORD_MNEM_SUFFIX;
5730 break;
5731 }
bab6aec1 5732 else if (i.types[op].bitfield.dword)
40fb9820
L
5733 {
5734 guess_suffix = LONG_MNEM_SUFFIX;
5735 break;
5736 }
bab6aec1 5737 else if (i.types[op].bitfield.qword)
40fb9820
L
5738 {
5739 guess_suffix = QWORD_MNEM_SUFFIX;
29b0f896 5740 break;
252b5132 5741 }
29b0f896
AM
5742 }
5743 else if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
5744 guess_suffix = WORD_MNEM_SUFFIX;
5745
5746 for (op = i.operands; --op >= 0;)
40fb9820 5747 if (operand_type_check (i.types[op], imm))
29b0f896
AM
5748 {
5749 switch (i.op[op].imms->X_op)
252b5132 5750 {
29b0f896
AM
5751 case O_constant:
5752 /* If a suffix is given, this operand may be shortened. */
5753 switch (guess_suffix)
252b5132 5754 {
29b0f896 5755 case LONG_MNEM_SUFFIX:
40fb9820
L
5756 i.types[op].bitfield.imm32 = 1;
5757 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
5758 break;
5759 case WORD_MNEM_SUFFIX:
40fb9820
L
5760 i.types[op].bitfield.imm16 = 1;
5761 i.types[op].bitfield.imm32 = 1;
5762 i.types[op].bitfield.imm32s = 1;
5763 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
5764 break;
5765 case BYTE_MNEM_SUFFIX:
40fb9820
L
5766 i.types[op].bitfield.imm8 = 1;
5767 i.types[op].bitfield.imm8s = 1;
5768 i.types[op].bitfield.imm16 = 1;
5769 i.types[op].bitfield.imm32 = 1;
5770 i.types[op].bitfield.imm32s = 1;
5771 i.types[op].bitfield.imm64 = 1;
29b0f896 5772 break;
252b5132 5773 }
252b5132 5774
29b0f896
AM
5775 /* If this operand is at most 16 bits, convert it
5776 to a signed 16 bit number before trying to see
5777 whether it will fit in an even smaller size.
5778 This allows a 16-bit operand such as $0xffe0 to
5779 be recognised as within Imm8S range. */
40fb9820 5780 if ((i.types[op].bitfield.imm16)
7e96fb68 5781 && fits_in_unsigned_word (i.op[op].imms->X_add_number))
252b5132 5782 {
87ed972d
JB
5783 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
5784 ^ 0x8000) - 0x8000);
29b0f896 5785 }
a28def75
L
5786#ifdef BFD64
5787 /* Store 32-bit immediate in 64-bit for 64-bit BFD. */
40fb9820 5788 if ((i.types[op].bitfield.imm32)
7e96fb68 5789 && fits_in_unsigned_long (i.op[op].imms->X_add_number))
29b0f896
AM
5790 {
5791 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
5792 ^ ((offsetT) 1 << 31))
5793 - ((offsetT) 1 << 31));
5794 }
a28def75 5795#endif
40fb9820 5796 i.types[op]
c6fb90c8
L
5797 = operand_type_or (i.types[op],
5798 smallest_imm_type (i.op[op].imms->X_add_number));
252b5132 5799
29b0f896
AM
5800 /* We must avoid matching of Imm32 templates when 64bit
5801 only immediate is available. */
5802 if (guess_suffix == QWORD_MNEM_SUFFIX)
40fb9820 5803 i.types[op].bitfield.imm32 = 0;
29b0f896 5804 break;
252b5132 5805
29b0f896
AM
5806 case O_absent:
5807 case O_register:
5808 abort ();
5809
5810 /* Symbols and expressions. */
5811 default:
9cd96992
JB
5812 /* Convert symbolic operand to proper sizes for matching, but don't
5813 prevent matching a set of insns that only supports sizes other
5814 than those matching the insn suffix. */
5815 {
40fb9820 5816 i386_operand_type mask, allowed;
87ed972d 5817 const insn_template *t = current_templates->start;
9cd96992 5818
0dfbf9d7 5819 operand_type_set (&mask, 0);
87ed972d 5820 allowed = t->operand_types[op];
40fb9820 5821
87ed972d 5822 while (++t < current_templates->end)
bab6aec1 5823 {
bab6aec1 5824 allowed = operand_type_and (allowed, anyimm);
87ed972d 5825 allowed = operand_type_or (allowed, t->operand_types[op]);
bab6aec1 5826 }
9cd96992
JB
5827 switch (guess_suffix)
5828 {
5829 case QWORD_MNEM_SUFFIX:
40fb9820
L
5830 mask.bitfield.imm64 = 1;
5831 mask.bitfield.imm32s = 1;
9cd96992
JB
5832 break;
5833 case LONG_MNEM_SUFFIX:
40fb9820 5834 mask.bitfield.imm32 = 1;
9cd96992
JB
5835 break;
5836 case WORD_MNEM_SUFFIX:
40fb9820 5837 mask.bitfield.imm16 = 1;
9cd96992
JB
5838 break;
5839 case BYTE_MNEM_SUFFIX:
40fb9820 5840 mask.bitfield.imm8 = 1;
9cd96992
JB
5841 break;
5842 default:
9cd96992
JB
5843 break;
5844 }
c6fb90c8 5845 allowed = operand_type_and (mask, allowed);
0dfbf9d7 5846 if (!operand_type_all_zero (&allowed))
c6fb90c8 5847 i.types[op] = operand_type_and (i.types[op], mask);
9cd96992 5848 }
29b0f896 5849 break;
252b5132 5850 }
29b0f896
AM
5851 }
5852}
47926f60 5853
29b0f896
AM
5854/* Try to use the smallest displacement type too. */
5855static void
e3bb37b5 5856optimize_disp (void)
29b0f896
AM
5857{
5858 int op;
3e73aa7c 5859
29b0f896 5860 for (op = i.operands; --op >= 0;)
40fb9820 5861 if (operand_type_check (i.types[op], disp))
252b5132 5862 {
b300c311 5863 if (i.op[op].disps->X_op == O_constant)
252b5132 5864 {
91d6fa6a 5865 offsetT op_disp = i.op[op].disps->X_add_number;
29b0f896 5866
91d6fa6a 5867 if (!op_disp && i.types[op].bitfield.baseindex)
b300c311 5868 {
2f2be86b
JB
5869 i.types[op] = operand_type_and_not (i.types[op], anydisp);
5870 i.op[op].disps = NULL;
b300c311 5871 i.disp_operands--;
f185acdd
JB
5872 continue;
5873 }
5874
5875 if (i.types[op].bitfield.disp16
cd613c1f 5876 && fits_in_unsigned_word (op_disp))
f185acdd
JB
5877 {
5878 /* If this operand is at most 16 bits, convert
5879 to a signed 16 bit number and don't use 64bit
5880 displacement. */
5881 op_disp = ((op_disp ^ 0x8000) - 0x8000);
5882 i.types[op].bitfield.disp64 = 0;
b300c311 5883 }
f185acdd 5884
28a167a4 5885#ifdef BFD64
a50187b2 5886 /* Optimize 64-bit displacement to 32-bit for 64-bit BFD. */
a775efc8
JB
5887 if ((flag_code != CODE_64BIT
5888 ? i.types[op].bitfield.disp32
5889 : want_disp32 (current_templates->start)
5890 && (!current_templates->start->opcode_modifier.jump
5891 || i.jumpabsolute || i.types[op].bitfield.baseindex))
a50187b2 5892 && fits_in_unsigned_long (op_disp))
b300c311 5893 {
a50187b2
JB
5894 /* If this operand is at most 32 bits, convert
5895 to a signed 32 bit number and don't use 64bit
5896 displacement. */
5897 op_disp = (op_disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
5898 i.types[op].bitfield.disp64 = 0;
5899 i.types[op].bitfield.disp32 = 1;
5900 }
28a167a4 5901
a50187b2
JB
5902 if (flag_code == CODE_64BIT && fits_in_signed_long (op_disp))
5903 {
5904 i.types[op].bitfield.disp64 = 0;
a775efc8 5905 i.types[op].bitfield.disp32 = 1;
b300c311 5906 }
28a167a4 5907#endif
40fb9820 5908 if ((i.types[op].bitfield.disp32
40fb9820 5909 || i.types[op].bitfield.disp16)
b5014f7a 5910 && fits_in_disp8 (op_disp))
40fb9820 5911 i.types[op].bitfield.disp8 = 1;
77c59789
JB
5912
5913 i.op[op].disps->X_add_number = op_disp;
252b5132 5914 }
67a4f2b7
AO
5915 else if (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
5916 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL)
5917 {
5918 fix_new_exp (frag_now, frag_more (0) - frag_now->fr_literal, 0,
5919 i.op[op].disps, 0, i.reloc[op]);
2f2be86b 5920 i.types[op] = operand_type_and_not (i.types[op], anydisp);
67a4f2b7
AO
5921 }
5922 else
b300c311 5923 /* We only support 64bit displacement on constants. */
40fb9820 5924 i.types[op].bitfield.disp64 = 0;
252b5132 5925 }
29b0f896
AM
5926}
5927
4a1b91ea
L
5928/* Return 1 if there is a match in broadcast bytes between operand
5929 GIVEN and instruction template T. */
5930
5931static INLINE int
5932match_broadcast_size (const insn_template *t, unsigned int given)
5933{
5934 return ((t->opcode_modifier.broadcast == BYTE_BROADCAST
5935 && i.types[given].bitfield.byte)
5936 || (t->opcode_modifier.broadcast == WORD_BROADCAST
5937 && i.types[given].bitfield.word)
5938 || (t->opcode_modifier.broadcast == DWORD_BROADCAST
5939 && i.types[given].bitfield.dword)
5940 || (t->opcode_modifier.broadcast == QWORD_BROADCAST
5941 && i.types[given].bitfield.qword));
5942}
5943
6c30d220
L
5944/* Check if operands are valid for the instruction. */
5945
5946static int
5947check_VecOperands (const insn_template *t)
5948{
43234a1e 5949 unsigned int op;
e2195274 5950 i386_cpu_flags cpu;
e2195274
JB
5951
5952 /* Templates allowing for ZMMword as well as YMMword and/or XMMword for
5953 any one operand are implicity requiring AVX512VL support if the actual
5954 operand size is YMMword or XMMword. Since this function runs after
5955 template matching, there's no need to check for YMMword/XMMword in
5956 the template. */
5957 cpu = cpu_flags_and (t->cpu_flags, avx512);
5958 if (!cpu_flags_all_zero (&cpu)
5959 && !t->cpu_flags.bitfield.cpuavx512vl
5960 && !cpu_arch_flags.bitfield.cpuavx512vl)
5961 {
5962 for (op = 0; op < t->operands; ++op)
5963 {
5964 if (t->operand_types[op].bitfield.zmmword
5965 && (i.types[op].bitfield.ymmword
5966 || i.types[op].bitfield.xmmword))
5967 {
5968 i.error = unsupported;
5969 return 1;
5970 }
5971 }
5972 }
43234a1e 5973
22c36940
JB
5974 /* Somewhat similarly, templates specifying both AVX and AVX2 are
5975 requiring AVX2 support if the actual operand size is YMMword. */
5976 if (t->cpu_flags.bitfield.cpuavx
5977 && t->cpu_flags.bitfield.cpuavx2
5978 && !cpu_arch_flags.bitfield.cpuavx2)
5979 {
5980 for (op = 0; op < t->operands; ++op)
5981 {
5982 if (t->operand_types[op].bitfield.xmmword
5983 && i.types[op].bitfield.ymmword)
5984 {
5985 i.error = unsupported;
5986 return 1;
5987 }
5988 }
5989 }
5990
6c30d220 5991 /* Without VSIB byte, we can't have a vector register for index. */
63112cd6 5992 if (!t->opcode_modifier.sib
6c30d220 5993 && i.index_reg
1b54b8d7
JB
5994 && (i.index_reg->reg_type.bitfield.xmmword
5995 || i.index_reg->reg_type.bitfield.ymmword
5996 || i.index_reg->reg_type.bitfield.zmmword))
6c30d220
L
5997 {
5998 i.error = unsupported_vector_index_register;
5999 return 1;
6000 }
6001
ad8ecc81
MZ
6002 /* Check if default mask is allowed. */
6003 if (t->opcode_modifier.nodefmask
6225c532 6004 && (!i.mask.reg || i.mask.reg->reg_num == 0))
ad8ecc81
MZ
6005 {
6006 i.error = no_default_mask;
6007 return 1;
6008 }
6009
7bab8ab5
JB
6010 /* For VSIB byte, we need a vector register for index, and all vector
6011 registers must be distinct. */
260cd341 6012 if (t->opcode_modifier.sib && t->opcode_modifier.sib != SIBMEM)
7bab8ab5
JB
6013 {
6014 if (!i.index_reg
63112cd6 6015 || !((t->opcode_modifier.sib == VECSIB128
1b54b8d7 6016 && i.index_reg->reg_type.bitfield.xmmword)
63112cd6 6017 || (t->opcode_modifier.sib == VECSIB256
1b54b8d7 6018 && i.index_reg->reg_type.bitfield.ymmword)
63112cd6 6019 || (t->opcode_modifier.sib == VECSIB512
1b54b8d7 6020 && i.index_reg->reg_type.bitfield.zmmword)))
7bab8ab5
JB
6021 {
6022 i.error = invalid_vsib_address;
6023 return 1;
6024 }
6025
6225c532
JB
6026 gas_assert (i.reg_operands == 2 || i.mask.reg);
6027 if (i.reg_operands == 2 && !i.mask.reg)
43234a1e 6028 {
3528c362 6029 gas_assert (i.types[0].bitfield.class == RegSIMD);
1b54b8d7
JB
6030 gas_assert (i.types[0].bitfield.xmmword
6031 || i.types[0].bitfield.ymmword);
3528c362 6032 gas_assert (i.types[2].bitfield.class == RegSIMD);
1b54b8d7
JB
6033 gas_assert (i.types[2].bitfield.xmmword
6034 || i.types[2].bitfield.ymmword);
43234a1e
L
6035 if (operand_check == check_none)
6036 return 0;
6037 if (register_number (i.op[0].regs)
6038 != register_number (i.index_reg)
6039 && register_number (i.op[2].regs)
6040 != register_number (i.index_reg)
6041 && register_number (i.op[0].regs)
6042 != register_number (i.op[2].regs))
6043 return 0;
6044 if (operand_check == check_error)
6045 {
6046 i.error = invalid_vector_register_set;
6047 return 1;
6048 }
6049 as_warn (_("mask, index, and destination registers should be distinct"));
6050 }
6225c532 6051 else if (i.reg_operands == 1 && i.mask.reg)
8444f82a 6052 {
3528c362 6053 if (i.types[1].bitfield.class == RegSIMD
1b54b8d7
JB
6054 && (i.types[1].bitfield.xmmword
6055 || i.types[1].bitfield.ymmword
6056 || i.types[1].bitfield.zmmword)
8444f82a
MZ
6057 && (register_number (i.op[1].regs)
6058 == register_number (i.index_reg)))
6059 {
6060 if (operand_check == check_error)
6061 {
6062 i.error = invalid_vector_register_set;
6063 return 1;
6064 }
6065 if (operand_check != check_none)
6066 as_warn (_("index and destination registers should be distinct"));
6067 }
6068 }
43234a1e 6069 }
7bab8ab5 6070
fc141319
L
6071 /* For AMX instructions with 3 TMM register operands, all operands
6072 must be distinct. */
6073 if (i.reg_operands == 3
6074 && t->operand_types[0].bitfield.tmmword
6075 && (i.op[0].regs == i.op[1].regs
6076 || i.op[0].regs == i.op[2].regs
6077 || i.op[1].regs == i.op[2].regs))
6078 {
6079 i.error = invalid_tmm_register_set;
6080 return 1;
260cd341
LC
6081 }
6082
0cc78721
CL
6083 /* For some special instructions require that destination must be distinct
6084 from source registers. */
6085 if (t->opcode_modifier.distinctdest)
6086 {
6087 unsigned int dest_reg = i.operands - 1;
6088
6089 know (i.operands >= 3);
6090
6091 /* #UD if dest_reg == src1_reg or dest_reg == src2_reg. */
6092 if (i.op[dest_reg - 1].regs == i.op[dest_reg].regs
6093 || (i.reg_operands > 2
6094 && i.op[dest_reg - 2].regs == i.op[dest_reg].regs))
6095 {
6096 i.error = invalid_dest_and_src_register_set;
6097 return 1;
6098 }
6099 }
6100
43234a1e
L
6101 /* Check if broadcast is supported by the instruction and is applied
6102 to the memory operand. */
a5748e0d 6103 if (i.broadcast.type || i.broadcast.bytes)
43234a1e 6104 {
8e6e0792 6105 i386_operand_type type, overlap;
43234a1e
L
6106
6107 /* Check if specified broadcast is supported in this instruction,
4a1b91ea 6108 and its broadcast bytes match the memory operand. */
5273a3cd 6109 op = i.broadcast.operand;
8e6e0792 6110 if (!t->opcode_modifier.broadcast
c48dadc9 6111 || !(i.flags[op] & Operand_Mem)
c39e5b26 6112 || (!i.types[op].bitfield.unspecified
4a1b91ea 6113 && !match_broadcast_size (t, op)))
43234a1e
L
6114 {
6115 bad_broadcast:
6116 i.error = unsupported_broadcast;
6117 return 1;
6118 }
8e6e0792 6119
a5748e0d
JB
6120 if (i.broadcast.type)
6121 i.broadcast.bytes = ((1 << (t->opcode_modifier.broadcast - 1))
6122 * i.broadcast.type);
8e6e0792 6123 operand_type_set (&type, 0);
a5748e0d 6124 switch (get_broadcast_bytes (t, false))
8e6e0792 6125 {
4a1b91ea
L
6126 case 2:
6127 type.bitfield.word = 1;
6128 break;
6129 case 4:
6130 type.bitfield.dword = 1;
6131 break;
8e6e0792
JB
6132 case 8:
6133 type.bitfield.qword = 1;
6134 break;
6135 case 16:
6136 type.bitfield.xmmword = 1;
6137 break;
6138 case 32:
6139 type.bitfield.ymmword = 1;
6140 break;
6141 case 64:
6142 type.bitfield.zmmword = 1;
6143 break;
6144 default:
6145 goto bad_broadcast;
6146 }
6147
6148 overlap = operand_type_and (type, t->operand_types[op]);
bc49bfd8
JB
6149 if (t->operand_types[op].bitfield.class == RegSIMD
6150 && t->operand_types[op].bitfield.byte
6151 + t->operand_types[op].bitfield.word
6152 + t->operand_types[op].bitfield.dword
6153 + t->operand_types[op].bitfield.qword > 1)
6154 {
6155 overlap.bitfield.xmmword = 0;
6156 overlap.bitfield.ymmword = 0;
6157 overlap.bitfield.zmmword = 0;
6158 }
8e6e0792
JB
6159 if (operand_type_all_zero (&overlap))
6160 goto bad_broadcast;
6161
6162 if (t->opcode_modifier.checkregsize)
6163 {
6164 unsigned int j;
6165
e2195274 6166 type.bitfield.baseindex = 1;
8e6e0792
JB
6167 for (j = 0; j < i.operands; ++j)
6168 {
6169 if (j != op
6170 && !operand_type_register_match(i.types[j],
6171 t->operand_types[j],
6172 type,
6173 t->operand_types[op]))
6174 goto bad_broadcast;
6175 }
6176 }
43234a1e
L
6177 }
6178 /* If broadcast is supported in this instruction, we need to check if
6179 operand of one-element size isn't specified without broadcast. */
6180 else if (t->opcode_modifier.broadcast && i.mem_operands)
6181 {
6182 /* Find memory operand. */
6183 for (op = 0; op < i.operands; op++)
8dc0818e 6184 if (i.flags[op] & Operand_Mem)
43234a1e
L
6185 break;
6186 gas_assert (op < i.operands);
6187 /* Check size of the memory operand. */
4a1b91ea 6188 if (match_broadcast_size (t, op))
43234a1e
L
6189 {
6190 i.error = broadcast_needed;
6191 return 1;
6192 }
6193 }
c39e5b26
JB
6194 else
6195 op = MAX_OPERANDS - 1; /* Avoid uninitialized variable warning. */
43234a1e
L
6196
6197 /* Check if requested masking is supported. */
6225c532 6198 if (i.mask.reg)
43234a1e 6199 {
ae2387fe
JB
6200 switch (t->opcode_modifier.masking)
6201 {
6202 case BOTH_MASKING:
6203 break;
6204 case MERGING_MASKING:
6225c532 6205 if (i.mask.zeroing)
ae2387fe
JB
6206 {
6207 case 0:
6208 i.error = unsupported_masking;
6209 return 1;
6210 }
6211 break;
6212 case DYNAMIC_MASKING:
6213 /* Memory destinations allow only merging masking. */
6225c532 6214 if (i.mask.zeroing && i.mem_operands)
ae2387fe
JB
6215 {
6216 /* Find memory operand. */
6217 for (op = 0; op < i.operands; op++)
c48dadc9 6218 if (i.flags[op] & Operand_Mem)
ae2387fe
JB
6219 break;
6220 gas_assert (op < i.operands);
6221 if (op == i.operands - 1)
6222 {
6223 i.error = unsupported_masking;
6224 return 1;
6225 }
6226 }
6227 break;
6228 default:
6229 abort ();
6230 }
43234a1e
L
6231 }
6232
6233 /* Check if masking is applied to dest operand. */
6225c532 6234 if (i.mask.reg && (i.mask.operand != i.operands - 1))
43234a1e
L
6235 {
6236 i.error = mask_not_on_destination;
6237 return 1;
6238 }
6239
43234a1e 6240 /* Check RC/SAE. */
ca5312a2 6241 if (i.rounding.type != rc_none)
43234a1e 6242 {
a80195f1 6243 if (!t->opcode_modifier.sae
cf665fee
JB
6244 || ((i.rounding.type != saeonly) != t->opcode_modifier.staticrounding)
6245 || i.mem_operands)
43234a1e
L
6246 {
6247 i.error = unsupported_rc_sae;
6248 return 1;
6249 }
cf665fee
JB
6250
6251 /* Non-EVEX.LIG forms need to have a ZMM register as at least one
6252 operand. */
6253 if (t->opcode_modifier.evex != EVEXLIG)
7bab8ab5 6254 {
cf665fee
JB
6255 for (op = 0; op < t->operands; ++op)
6256 if (i.types[op].bitfield.zmmword)
6257 break;
6258 if (op >= t->operands)
6259 {
6260 i.error = operand_size_mismatch;
6261 return 1;
6262 }
7bab8ab5 6263 }
6c30d220
L
6264 }
6265
da4977e0
JB
6266 /* Check the special Imm4 cases; must be the first operand. */
6267 if (t->cpu_flags.bitfield.cpuxop && t->operands == 5)
6268 {
6269 if (i.op[0].imms->X_op != O_constant
6270 || !fits_in_imm4 (i.op[0].imms->X_add_number))
6271 {
6272 i.error = bad_imm4;
6273 return 1;
6274 }
6275
6276 /* Turn off Imm<N> so that update_imm won't complain. */
6277 operand_type_set (&i.types[0], 0);
6278 }
6279
43234a1e 6280 /* Check vector Disp8 operand. */
b5014f7a 6281 if (t->opcode_modifier.disp8memshift
1a42a9fe 6282 && i.disp_encoding <= disp_encoding_8bit)
43234a1e 6283 {
a5748e0d 6284 if (i.broadcast.bytes)
4a1b91ea 6285 i.memshift = t->opcode_modifier.broadcast - 1;
7091c612 6286 else if (t->opcode_modifier.disp8memshift != DISP8_SHIFT_VL)
43234a1e 6287 i.memshift = t->opcode_modifier.disp8memshift;
7091c612
JB
6288 else
6289 {
125ff819 6290 const i386_operand_type *type = NULL, *fallback = NULL;
7091c612
JB
6291
6292 i.memshift = 0;
6293 for (op = 0; op < i.operands; op++)
8dc0818e 6294 if (i.flags[op] & Operand_Mem)
7091c612 6295 {
4174bfff
JB
6296 if (t->opcode_modifier.evex == EVEXLIG)
6297 i.memshift = 2 + (i.suffix == QWORD_MNEM_SUFFIX);
6298 else if (t->operand_types[op].bitfield.xmmword
6299 + t->operand_types[op].bitfield.ymmword
6300 + t->operand_types[op].bitfield.zmmword <= 1)
7091c612
JB
6301 type = &t->operand_types[op];
6302 else if (!i.types[op].bitfield.unspecified)
6303 type = &i.types[op];
125ff819
JB
6304 else /* Ambiguities get resolved elsewhere. */
6305 fallback = &t->operand_types[op];
7091c612 6306 }
3528c362 6307 else if (i.types[op].bitfield.class == RegSIMD
4174bfff 6308 && t->opcode_modifier.evex != EVEXLIG)
7091c612
JB
6309 {
6310 if (i.types[op].bitfield.zmmword)
6311 i.memshift = 6;
6312 else if (i.types[op].bitfield.ymmword && i.memshift < 5)
6313 i.memshift = 5;
6314 else if (i.types[op].bitfield.xmmword && i.memshift < 4)
6315 i.memshift = 4;
6316 }
6317
125ff819
JB
6318 if (!type && !i.memshift)
6319 type = fallback;
7091c612
JB
6320 if (type)
6321 {
6322 if (type->bitfield.zmmword)
6323 i.memshift = 6;
6324 else if (type->bitfield.ymmword)
6325 i.memshift = 5;
6326 else if (type->bitfield.xmmword)
6327 i.memshift = 4;
6328 }
6329
6330 /* For the check in fits_in_disp8(). */
6331 if (i.memshift == 0)
6332 i.memshift = -1;
6333 }
43234a1e
L
6334
6335 for (op = 0; op < i.operands; op++)
6336 if (operand_type_check (i.types[op], disp)
6337 && i.op[op].disps->X_op == O_constant)
6338 {
b5014f7a 6339 if (fits_in_disp8 (i.op[op].disps->X_add_number))
43234a1e 6340 {
b5014f7a
JB
6341 i.types[op].bitfield.disp8 = 1;
6342 return 0;
43234a1e 6343 }
b5014f7a 6344 i.types[op].bitfield.disp8 = 0;
43234a1e
L
6345 }
6346 }
b5014f7a
JB
6347
6348 i.memshift = 0;
43234a1e 6349
6c30d220
L
6350 return 0;
6351}
6352
da4977e0 6353/* Check if encoding requirements are met by the instruction. */
a683cc34
SP
6354
6355static int
da4977e0 6356VEX_check_encoding (const insn_template *t)
a683cc34 6357{
da4977e0
JB
6358 if (i.vec_encoding == vex_encoding_error)
6359 {
6360 i.error = unsupported;
6361 return 1;
6362 }
6363
86fa6981 6364 if (i.vec_encoding == vex_encoding_evex)
43234a1e 6365 {
86fa6981 6366 /* This instruction must be encoded with EVEX prefix. */
e771e7c9 6367 if (!is_evex_encoding (t))
86fa6981
L
6368 {
6369 i.error = unsupported;
6370 return 1;
6371 }
6372 return 0;
43234a1e
L
6373 }
6374
a683cc34 6375 if (!t->opcode_modifier.vex)
86fa6981
L
6376 {
6377 /* This instruction template doesn't have VEX prefix. */
6378 if (i.vec_encoding != vex_encoding_default)
6379 {
6380 i.error = unsupported;
6381 return 1;
6382 }
6383 return 0;
6384 }
a683cc34 6385
a683cc34
SP
6386 return 0;
6387}
6388
d3ce72d0 6389static const insn_template *
83b16ac6 6390match_template (char mnem_suffix)
29b0f896
AM
6391{
6392 /* Points to template once we've found it. */
d3ce72d0 6393 const insn_template *t;
40fb9820 6394 i386_operand_type overlap0, overlap1, overlap2, overlap3;
c0f3af97 6395 i386_operand_type overlap4;
29b0f896 6396 unsigned int found_reverse_match;
dc2be329 6397 i386_opcode_modifier suffix_check;
40fb9820 6398 i386_operand_type operand_types [MAX_OPERANDS];
539e75ad 6399 int addr_prefix_disp;
45a4bb20 6400 unsigned int j, size_match, check_register;
5614d22c 6401 enum i386_error specific_error = 0;
29b0f896 6402
c0f3af97
L
6403#if MAX_OPERANDS != 5
6404# error "MAX_OPERANDS must be 5."
f48ff2ae
L
6405#endif
6406
29b0f896 6407 found_reverse_match = 0;
539e75ad 6408 addr_prefix_disp = -1;
40fb9820 6409
dc2be329 6410 /* Prepare for mnemonic suffix check. */
40fb9820 6411 memset (&suffix_check, 0, sizeof (suffix_check));
dc2be329
L
6412 switch (mnem_suffix)
6413 {
6414 case BYTE_MNEM_SUFFIX:
6415 suffix_check.no_bsuf = 1;
6416 break;
6417 case WORD_MNEM_SUFFIX:
6418 suffix_check.no_wsuf = 1;
6419 break;
6420 case SHORT_MNEM_SUFFIX:
6421 suffix_check.no_ssuf = 1;
6422 break;
6423 case LONG_MNEM_SUFFIX:
6424 suffix_check.no_lsuf = 1;
6425 break;
6426 case QWORD_MNEM_SUFFIX:
6427 suffix_check.no_qsuf = 1;
6428 break;
6429 default:
6430 /* NB: In Intel syntax, normally we can check for memory operand
6431 size when there is no mnemonic suffix. But jmp and call have
6432 2 different encodings with Dword memory operand size, one with
6433 No_ldSuf and the other without. i.suffix is set to
6434 LONG_DOUBLE_MNEM_SUFFIX to skip the one with No_ldSuf. */
6435 if (i.suffix == LONG_DOUBLE_MNEM_SUFFIX)
6436 suffix_check.no_ldsuf = 1;
83b16ac6
JB
6437 }
6438
01559ecc
L
6439 /* Must have right number of operands. */
6440 i.error = number_of_operands_mismatch;
6441
45aa61fe 6442 for (t = current_templates->start; t < current_templates->end; t++)
29b0f896 6443 {
539e75ad 6444 addr_prefix_disp = -1;
dbbc8b7e 6445 found_reverse_match = 0;
539e75ad 6446
29b0f896
AM
6447 if (i.operands != t->operands)
6448 continue;
6449
50aecf8c 6450 /* Check processor support. */
a65babc9 6451 i.error = unsupported;
45a4bb20 6452 if (cpu_flags_match (t) != CPU_FLAGS_PERFECT_MATCH)
50aecf8c
L
6453 continue;
6454
57392598
CL
6455 /* Check Pseudo Prefix. */
6456 i.error = unsupported;
6457 if (t->opcode_modifier.pseudovexprefix
6458 && !(i.vec_encoding == vex_encoding_vex
6459 || i.vec_encoding == vex_encoding_vex3))
6460 continue;
6461
e1d4d893 6462 /* Check AT&T mnemonic. */
a65babc9 6463 i.error = unsupported_with_intel_mnemonic;
e1d4d893 6464 if (intel_mnemonic && t->opcode_modifier.attmnemonic)
1efbbeb4
L
6465 continue;
6466
4b5aaf5f 6467 /* Check AT&T/Intel syntax. */
a65babc9 6468 i.error = unsupported_syntax;
5c07affc 6469 if ((intel_syntax && t->opcode_modifier.attsyntax)
4b5aaf5f 6470 || (!intel_syntax && t->opcode_modifier.intelsyntax))
1efbbeb4
L
6471 continue;
6472
4b5aaf5f
L
6473 /* Check Intel64/AMD64 ISA. */
6474 switch (isa64)
6475 {
6476 default:
6477 /* Default: Don't accept Intel64. */
6478 if (t->opcode_modifier.isa64 == INTEL64)
6479 continue;
6480 break;
6481 case amd64:
6482 /* -mamd64: Don't accept Intel64 and Intel64 only. */
6483 if (t->opcode_modifier.isa64 >= INTEL64)
6484 continue;
6485 break;
6486 case intel64:
6487 /* -mintel64: Don't accept AMD64. */
5990e377 6488 if (t->opcode_modifier.isa64 == AMD64 && flag_code == CODE_64BIT)
4b5aaf5f
L
6489 continue;
6490 break;
6491 }
6492
dc2be329 6493 /* Check the suffix. */
a65babc9 6494 i.error = invalid_instruction_suffix;
dc2be329
L
6495 if ((t->opcode_modifier.no_bsuf && suffix_check.no_bsuf)
6496 || (t->opcode_modifier.no_wsuf && suffix_check.no_wsuf)
6497 || (t->opcode_modifier.no_lsuf && suffix_check.no_lsuf)
6498 || (t->opcode_modifier.no_ssuf && suffix_check.no_ssuf)
6499 || (t->opcode_modifier.no_qsuf && suffix_check.no_qsuf)
6500 || (t->opcode_modifier.no_ldsuf && suffix_check.no_ldsuf))
83b16ac6 6501 continue;
29b0f896 6502
3ac21baa
JB
6503 size_match = operand_size_match (t);
6504 if (!size_match)
7d5e4556 6505 continue;
539e75ad 6506
6f2f06be
JB
6507 /* This is intentionally not
6508
0cfa3eb3 6509 if (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE))
6f2f06be
JB
6510
6511 as the case of a missing * on the operand is accepted (perhaps with
6512 a warning, issued further down). */
0cfa3eb3 6513 if (i.jumpabsolute && t->opcode_modifier.jump != JUMP_ABSOLUTE)
6f2f06be
JB
6514 {
6515 i.error = operand_type_mismatch;
6516 continue;
6517 }
6518
5c07affc
L
6519 for (j = 0; j < MAX_OPERANDS; j++)
6520 operand_types[j] = t->operand_types[j];
6521
e365e234
JB
6522 /* In general, don't allow
6523 - 64-bit operands outside of 64-bit mode,
6524 - 32-bit operands on pre-386. */
4873e243 6525 j = i.imm_operands + (t->operands > i.imm_operands + 1);
e365e234
JB
6526 if (((i.suffix == QWORD_MNEM_SUFFIX
6527 && flag_code != CODE_64BIT
389d00a5
JB
6528 && !(t->opcode_modifier.opcodespace == SPACE_0F
6529 && t->base_opcode == 0xc7
5e74b495 6530 && t->opcode_modifier.opcodeprefix == PREFIX_NONE
8b65b895 6531 && t->extension_opcode == 1) /* cmpxchg8b */)
e365e234
JB
6532 || (i.suffix == LONG_MNEM_SUFFIX
6533 && !cpu_arch_flags.bitfield.cpui386))
45aa61fe 6534 && (intel_syntax
3cd7f3e3 6535 ? (t->opcode_modifier.mnemonicsize != IGNORESIZE
45aa61fe
AM
6536 && !intel_float_operand (t->name))
6537 : intel_float_operand (t->name) != 2)
4873e243
JB
6538 && (t->operands == i.imm_operands
6539 || (operand_types[i.imm_operands].bitfield.class != RegMMX
6540 && operand_types[i.imm_operands].bitfield.class != RegSIMD
6541 && operand_types[i.imm_operands].bitfield.class != RegMask)
6542 || (operand_types[j].bitfield.class != RegMMX
6543 && operand_types[j].bitfield.class != RegSIMD
6544 && operand_types[j].bitfield.class != RegMask))
63112cd6 6545 && !t->opcode_modifier.sib)
192dc9c6
JB
6546 continue;
6547
29b0f896 6548 /* Do not verify operands when there are none. */
e365e234 6549 if (!t->operands)
da4977e0
JB
6550 {
6551 if (VEX_check_encoding (t))
6552 {
6553 specific_error = i.error;
6554 continue;
6555 }
6556
6557 /* We've found a match; break out of loop. */
6558 break;
6559 }
252b5132 6560
48bcea9f
JB
6561 if (!t->opcode_modifier.jump
6562 || t->opcode_modifier.jump == JUMP_ABSOLUTE)
6563 {
6564 /* There should be only one Disp operand. */
6565 for (j = 0; j < MAX_OPERANDS; j++)
6566 if (operand_type_check (operand_types[j], disp))
539e75ad 6567 break;
48bcea9f
JB
6568 if (j < MAX_OPERANDS)
6569 {
5b7c81bd 6570 bool override = (i.prefix[ADDR_PREFIX] != 0);
48bcea9f
JB
6571
6572 addr_prefix_disp = j;
6573
a775efc8
JB
6574 /* Address size prefix will turn Disp64 operand into Disp32 and
6575 Disp32/Disp16 one into Disp16/Disp32 respectively. */
48bcea9f 6576 switch (flag_code)
40fb9820 6577 {
48bcea9f
JB
6578 case CODE_16BIT:
6579 override = !override;
6580 /* Fall through. */
6581 case CODE_32BIT:
6582 if (operand_types[j].bitfield.disp32
6583 && operand_types[j].bitfield.disp16)
40fb9820 6584 {
48bcea9f
JB
6585 operand_types[j].bitfield.disp16 = override;
6586 operand_types[j].bitfield.disp32 = !override;
40fb9820 6587 }
a775efc8 6588 gas_assert (!operand_types[j].bitfield.disp64);
48bcea9f
JB
6589 break;
6590
6591 case CODE_64BIT:
a775efc8 6592 if (operand_types[j].bitfield.disp64)
40fb9820 6593 {
a775efc8 6594 gas_assert (!operand_types[j].bitfield.disp32);
48bcea9f 6595 operand_types[j].bitfield.disp32 = override;
a775efc8 6596 operand_types[j].bitfield.disp64 = !override;
40fb9820 6597 }
48bcea9f
JB
6598 operand_types[j].bitfield.disp16 = 0;
6599 break;
40fb9820 6600 }
539e75ad 6601 }
48bcea9f 6602 }
539e75ad 6603
d7e3e627
L
6604 switch (i.reloc[0])
6605 {
6606 case BFD_RELOC_386_GOT32:
6607 /* Force 0x8b encoding for "mov foo@GOT, %eax". */
6608 if (t->base_opcode == 0xa0
6609 && t->opcode_modifier.opcodespace == SPACE_BASE)
6610 continue;
6611 break;
6612 case BFD_RELOC_386_TLS_GOTIE:
6613 case BFD_RELOC_386_TLS_LE_32:
6614 case BFD_RELOC_X86_64_GOTTPOFF:
6615 case BFD_RELOC_X86_64_TLSLD:
6616 /* Don't allow KMOV in TLS code sequences. */
6617 if (t->opcode_modifier.vex)
6618 continue;
6619 break;
6620 default:
6621 break;
6622 }
02a86693 6623
56ffb741 6624 /* We check register size if needed. */
e2195274
JB
6625 if (t->opcode_modifier.checkregsize)
6626 {
6627 check_register = (1 << t->operands) - 1;
a5748e0d 6628 if (i.broadcast.type || i.broadcast.bytes)
5273a3cd 6629 check_register &= ~(1 << i.broadcast.operand);
e2195274
JB
6630 }
6631 else
6632 check_register = 0;
6633
c6fb90c8 6634 overlap0 = operand_type_and (i.types[0], operand_types[0]);
29b0f896
AM
6635 switch (t->operands)
6636 {
6637 case 1:
40fb9820 6638 if (!operand_type_match (overlap0, i.types[0]))
29b0f896
AM
6639 continue;
6640 break;
6641 case 2:
33eaf5de 6642 /* xchg %eax, %eax is a special case. It is an alias for nop
8b38ad71
L
6643 only in 32bit mode and we can use opcode 0x90. In 64bit
6644 mode, we can't use 0x90 for xchg %eax, %eax since it should
6645 zero-extend %eax to %rax. */
6646 if (flag_code == CODE_64BIT
6647 && t->base_opcode == 0x90
35648716 6648 && t->opcode_modifier.opcodespace == SPACE_BASE
75e5731b
JB
6649 && i.types[0].bitfield.instance == Accum
6650 && i.types[0].bitfield.dword
6651 && i.types[1].bitfield.instance == Accum
6652 && i.types[1].bitfield.dword)
8b38ad71 6653 continue;
1212781b
JB
6654 /* xrelease mov %eax, <disp> is another special case. It must not
6655 match the accumulator-only encoding of mov. */
6656 if (flag_code != CODE_64BIT
6657 && i.hle_prefix
6658 && t->base_opcode == 0xa0
35648716 6659 && t->opcode_modifier.opcodespace == SPACE_BASE
75e5731b 6660 && i.types[0].bitfield.instance == Accum
8dc0818e 6661 && (i.flags[1] & Operand_Mem))
1212781b 6662 continue;
f5eb1d70
JB
6663 /* Fall through. */
6664
6665 case 3:
3ac21baa
JB
6666 if (!(size_match & MATCH_STRAIGHT))
6667 goto check_reverse;
64c49ab3
JB
6668 /* Reverse direction of operands if swapping is possible in the first
6669 place (operands need to be symmetric) and
6670 - the load form is requested, and the template is a store form,
6671 - the store form is requested, and the template is a load form,
6672 - the non-default (swapped) form is requested. */
6673 overlap1 = operand_type_and (operand_types[0], operand_types[1]);
f5eb1d70 6674 if (t->opcode_modifier.d && i.reg_operands == i.operands
64c49ab3
JB
6675 && !operand_type_all_zero (&overlap1))
6676 switch (i.dir_encoding)
6677 {
6678 case dir_encoding_load:
6679 if (operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 6680 || t->opcode_modifier.regmem)
64c49ab3
JB
6681 goto check_reverse;
6682 break;
6683
6684 case dir_encoding_store:
6685 if (!operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 6686 && !t->opcode_modifier.regmem)
64c49ab3
JB
6687 goto check_reverse;
6688 break;
6689
6690 case dir_encoding_swap:
6691 goto check_reverse;
6692
6693 case dir_encoding_default:
6694 break;
6695 }
86fa6981 6696 /* If we want store form, we skip the current load. */
64c49ab3
JB
6697 if ((i.dir_encoding == dir_encoding_store
6698 || i.dir_encoding == dir_encoding_swap)
86fa6981
L
6699 && i.mem_operands == 0
6700 && t->opcode_modifier.load)
fa99fab2 6701 continue;
1a0670f3 6702 /* Fall through. */
f48ff2ae 6703 case 4:
c0f3af97 6704 case 5:
c6fb90c8 6705 overlap1 = operand_type_and (i.types[1], operand_types[1]);
40fb9820
L
6706 if (!operand_type_match (overlap0, i.types[0])
6707 || !operand_type_match (overlap1, i.types[1])
e2195274 6708 || ((check_register & 3) == 3
dc821c5f 6709 && !operand_type_register_match (i.types[0],
40fb9820 6710 operand_types[0],
dc821c5f 6711 i.types[1],
40fb9820 6712 operand_types[1])))
29b0f896
AM
6713 {
6714 /* Check if other direction is valid ... */
38e314eb 6715 if (!t->opcode_modifier.d)
29b0f896
AM
6716 continue;
6717
dc1e8a47 6718 check_reverse:
3ac21baa
JB
6719 if (!(size_match & MATCH_REVERSE))
6720 continue;
29b0f896 6721 /* Try reversing direction of operands. */
8bd915b7
JB
6722 j = t->opcode_modifier.vexsources ? 1 : i.operands - 1;
6723 overlap0 = operand_type_and (i.types[0], operand_types[j]);
6724 overlap1 = operand_type_and (i.types[j], operand_types[0]);
c975cec5
JB
6725 overlap2 = operand_type_and (i.types[1], operand_types[1]);
6726 gas_assert (t->operands != 3 || !check_register);
40fb9820 6727 if (!operand_type_match (overlap0, i.types[0])
8bd915b7 6728 || !operand_type_match (overlap1, i.types[j])
c975cec5
JB
6729 || (t->operands == 3
6730 && !operand_type_match (overlap2, i.types[1]))
45664ddb 6731 || (check_register
dc821c5f 6732 && !operand_type_register_match (i.types[0],
8bd915b7
JB
6733 operand_types[j],
6734 i.types[j],
45664ddb 6735 operand_types[0])))
29b0f896
AM
6736 {
6737 /* Does not match either direction. */
6738 continue;
6739 }
38e314eb 6740 /* found_reverse_match holds which of D or FloatR
29b0f896 6741 we've found. */
38e314eb
JB
6742 if (!t->opcode_modifier.d)
6743 found_reverse_match = 0;
6744 else if (operand_types[0].bitfield.tbyte)
8a2ed489 6745 found_reverse_match = Opcode_FloatD;
8bd915b7
JB
6746 else if (t->opcode_modifier.vexsources)
6747 {
6748 found_reverse_match = Opcode_VexW;
6749 goto check_operands_345;
6750 }
2c735193
JB
6751 else if (t->opcode_modifier.opcodespace != SPACE_BASE
6752 && (t->opcode_modifier.opcodespace != SPACE_0F
6753 /* MOV to/from CR/DR/TR, as an exception, follow
6754 the base opcode space encoding model. */
6755 || (t->base_opcode | 7) != 0x27))
dbbc8b7e 6756 found_reverse_match = (t->base_opcode & 0xee) != 0x6e
2c735193 6757 ? Opcode_ExtD : Opcode_SIMD_IntD;
8a2ed489 6758 else
38e314eb 6759 found_reverse_match = Opcode_D;
40fb9820 6760 if (t->opcode_modifier.floatr)
8a2ed489 6761 found_reverse_match |= Opcode_FloatR;
29b0f896 6762 }
f48ff2ae 6763 else
29b0f896 6764 {
f48ff2ae 6765 /* Found a forward 2 operand match here. */
8bd915b7 6766 check_operands_345:
d1cbb4db
L
6767 switch (t->operands)
6768 {
c0f3af97 6769 case 5:
3d0738af 6770 overlap4 = operand_type_and (i.types[4], operand_types[4]);
c0f3af97 6771 if (!operand_type_match (overlap4, i.types[4])
dc821c5f 6772 || !operand_type_register_match (i.types[3],
c0f3af97 6773 operand_types[3],
c0f3af97
L
6774 i.types[4],
6775 operand_types[4]))
6776 continue;
1a0670f3 6777 /* Fall through. */
f48ff2ae 6778 case 4:
3d0738af 6779 overlap3 = operand_type_and (i.types[3], operand_types[3]);
40fb9820 6780 if (!operand_type_match (overlap3, i.types[3])
e2195274
JB
6781 || ((check_register & 0xa) == 0xa
6782 && !operand_type_register_match (i.types[1],
f7768225
JB
6783 operand_types[1],
6784 i.types[3],
e2195274
JB
6785 operand_types[3]))
6786 || ((check_register & 0xc) == 0xc
6787 && !operand_type_register_match (i.types[2],
6788 operand_types[2],
6789 i.types[3],
6790 operand_types[3])))
f48ff2ae 6791 continue;
1a0670f3 6792 /* Fall through. */
f48ff2ae 6793 case 3:
3d0738af 6794 overlap2 = operand_type_and (i.types[2], operand_types[2]);
40fb9820 6795 if (!operand_type_match (overlap2, i.types[2])
e2195274
JB
6796 || ((check_register & 5) == 5
6797 && !operand_type_register_match (i.types[0],
23e42951
JB
6798 operand_types[0],
6799 i.types[2],
e2195274
JB
6800 operand_types[2]))
6801 || ((check_register & 6) == 6
6802 && !operand_type_register_match (i.types[1],
6803 operand_types[1],
6804 i.types[2],
6805 operand_types[2])))
f48ff2ae
L
6806 continue;
6807 break;
6808 }
29b0f896 6809 }
f48ff2ae 6810 /* Found either forward/reverse 2, 3 or 4 operand match here:
29b0f896
AM
6811 slip through to break. */
6812 }
c0f3af97 6813
da4977e0
JB
6814 /* Check if vector operands are valid. */
6815 if (check_VecOperands (t))
6816 {
6817 specific_error = i.error;
6818 continue;
6819 }
6820
6821 /* Check if VEX/EVEX encoding requirements can be satisfied. */
6822 if (VEX_check_encoding (t))
5614d22c
JB
6823 {
6824 specific_error = i.error;
6825 continue;
6826 }
a683cc34 6827
29b0f896
AM
6828 /* We've found a match; break out of loop. */
6829 break;
6830 }
6831
6832 if (t == current_templates->end)
6833 {
6834 /* We found no match. */
a65babc9 6835 const char *err_msg;
5614d22c 6836 switch (specific_error ? specific_error : i.error)
a65babc9
L
6837 {
6838 default:
6839 abort ();
86e026a4 6840 case operand_size_mismatch:
a65babc9
L
6841 err_msg = _("operand size mismatch");
6842 break;
6843 case operand_type_mismatch:
6844 err_msg = _("operand type mismatch");
6845 break;
6846 case register_type_mismatch:
6847 err_msg = _("register type mismatch");
6848 break;
6849 case number_of_operands_mismatch:
6850 err_msg = _("number of operands mismatch");
6851 break;
6852 case invalid_instruction_suffix:
6853 err_msg = _("invalid instruction suffix");
6854 break;
6855 case bad_imm4:
4a2608e3 6856 err_msg = _("constant doesn't fit in 4 bits");
a65babc9 6857 break;
a65babc9
L
6858 case unsupported_with_intel_mnemonic:
6859 err_msg = _("unsupported with Intel mnemonic");
6860 break;
6861 case unsupported_syntax:
6862 err_msg = _("unsupported syntax");
6863 break;
6864 case unsupported:
35262a23 6865 as_bad (_("unsupported instruction `%s'"),
10efe3f6
L
6866 current_templates->start->name);
6867 return NULL;
260cd341
LC
6868 case invalid_sib_address:
6869 err_msg = _("invalid SIB address");
6870 break;
6c30d220
L
6871 case invalid_vsib_address:
6872 err_msg = _("invalid VSIB address");
6873 break;
7bab8ab5
JB
6874 case invalid_vector_register_set:
6875 err_msg = _("mask, index, and destination registers must be distinct");
6876 break;
260cd341
LC
6877 case invalid_tmm_register_set:
6878 err_msg = _("all tmm registers must be distinct");
6879 break;
0cc78721
CL
6880 case invalid_dest_and_src_register_set:
6881 err_msg = _("destination and source registers must be distinct");
6882 break;
6c30d220
L
6883 case unsupported_vector_index_register:
6884 err_msg = _("unsupported vector index register");
6885 break;
43234a1e
L
6886 case unsupported_broadcast:
6887 err_msg = _("unsupported broadcast");
6888 break;
43234a1e
L
6889 case broadcast_needed:
6890 err_msg = _("broadcast is needed for operand of such type");
6891 break;
6892 case unsupported_masking:
6893 err_msg = _("unsupported masking");
6894 break;
6895 case mask_not_on_destination:
6896 err_msg = _("mask not on destination operand");
6897 break;
6898 case no_default_mask:
6899 err_msg = _("default mask isn't allowed");
6900 break;
6901 case unsupported_rc_sae:
6902 err_msg = _("unsupported static rounding/sae");
6903 break;
43234a1e
L
6904 case invalid_register_operand:
6905 err_msg = _("invalid register operand");
6906 break;
a65babc9
L
6907 }
6908 as_bad (_("%s for `%s'"), err_msg,
891edac4 6909 current_templates->start->name);
fa99fab2 6910 return NULL;
29b0f896 6911 }
252b5132 6912
29b0f896
AM
6913 if (!quiet_warnings)
6914 {
6915 if (!intel_syntax
0cfa3eb3 6916 && (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE)))
6f2f06be 6917 as_warn (_("indirect %s without `*'"), t->name);
29b0f896 6918
40fb9820 6919 if (t->opcode_modifier.isprefix
3cd7f3e3 6920 && t->opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
6921 {
6922 /* Warn them that a data or address size prefix doesn't
6923 affect assembly of the next line of code. */
6924 as_warn (_("stand-alone `%s' prefix"), t->name);
6925 }
6926 }
6927
6928 /* Copy the template we found. */
9a182d04 6929 install_template (t);
539e75ad
L
6930
6931 if (addr_prefix_disp != -1)
6932 i.tm.operand_types[addr_prefix_disp]
6933 = operand_types[addr_prefix_disp];
6934
8bd915b7 6935 switch (found_reverse_match)
29b0f896 6936 {
8bd915b7
JB
6937 case 0:
6938 break;
6939
6940 default:
dfd69174
JB
6941 /* If we found a reverse match we must alter the opcode direction
6942 bit and clear/flip the regmem modifier one. found_reverse_match
6943 holds bits to change (different for int & float insns). */
29b0f896
AM
6944
6945 i.tm.base_opcode ^= found_reverse_match;
6946
f5eb1d70
JB
6947 i.tm.operand_types[0] = operand_types[i.operands - 1];
6948 i.tm.operand_types[i.operands - 1] = operand_types[0];
dfd69174
JB
6949
6950 /* Certain SIMD insns have their load forms specified in the opcode
6951 table, and hence we need to _set_ RegMem instead of clearing it.
6952 We need to avoid setting the bit though on insns like KMOVW. */
6953 i.tm.opcode_modifier.regmem
6954 = i.tm.opcode_modifier.modrm && i.tm.opcode_modifier.d
6955 && i.tm.operands > 2U - i.tm.opcode_modifier.sse2avx
6956 && !i.tm.opcode_modifier.regmem;
8bd915b7
JB
6957 break;
6958
6959 case Opcode_VexW:
6960 /* Only the first two register operands need reversing, alongside
6961 flipping VEX.W. */
6962 i.tm.opcode_modifier.vexw ^= VEXW0 ^ VEXW1;
6963
6964 j = i.tm.operand_types[0].bitfield.imm8;
6965 i.tm.operand_types[j] = operand_types[j + 1];
6966 i.tm.operand_types[j + 1] = operand_types[j];
6967 break;
29b0f896
AM
6968 }
6969
fa99fab2 6970 return t;
29b0f896
AM
6971}
6972
6973static int
e3bb37b5 6974check_string (void)
29b0f896 6975{
51c8edf6
JB
6976 unsigned int es_op = i.tm.opcode_modifier.isstring - IS_STRING_ES_OP0;
6977 unsigned int op = i.tm.operand_types[0].bitfield.baseindex ? es_op : 0;
8dc0818e 6978
5e042380 6979 if (i.seg[op] != NULL && i.seg[op] != reg_es)
29b0f896 6980 {
51c8edf6
JB
6981 as_bad (_("`%s' operand %u must use `%ses' segment"),
6982 i.tm.name,
6983 intel_syntax ? i.tm.operands - es_op : es_op + 1,
6984 register_prefix);
6985 return 0;
29b0f896 6986 }
51c8edf6
JB
6987
6988 /* There's only ever one segment override allowed per instruction.
6989 This instruction possibly has a legal segment override on the
6990 second operand, so copy the segment to where non-string
6991 instructions store it, allowing common code. */
6992 i.seg[op] = i.seg[1];
6993
29b0f896
AM
6994 return 1;
6995}
6996
6997static int
543613e9 6998process_suffix (void)
29b0f896 6999{
5b7c81bd 7000 bool is_crc32 = false, is_movx = false;
8b65b895 7001
29b0f896
AM
7002 /* If matched instruction specifies an explicit instruction mnemonic
7003 suffix, use it. */
673fe0f0 7004 if (i.tm.opcode_modifier.size == SIZE16)
40fb9820 7005 i.suffix = WORD_MNEM_SUFFIX;
673fe0f0 7006 else if (i.tm.opcode_modifier.size == SIZE32)
40fb9820 7007 i.suffix = LONG_MNEM_SUFFIX;
673fe0f0 7008 else if (i.tm.opcode_modifier.size == SIZE64)
40fb9820 7009 i.suffix = QWORD_MNEM_SUFFIX;
13e600d0 7010 else if (i.reg_operands
c8f8eebc
JB
7011 && (i.operands > 1 || i.types[0].bitfield.class == Reg)
7012 && !i.tm.opcode_modifier.addrprefixopreg)
29b0f896 7013 {
65fca059 7014 unsigned int numop = i.operands;
389d00a5
JB
7015
7016 /* MOVSX/MOVZX */
7017 is_movx = (i.tm.opcode_modifier.opcodespace == SPACE_0F
7018 && (i.tm.base_opcode | 8) == 0xbe)
7019 || (i.tm.opcode_modifier.opcodespace == SPACE_BASE
7020 && i.tm.base_opcode == 0x63
7021 && i.tm.cpu_flags.bitfield.cpu64);
7022
8b65b895 7023 /* CRC32 */
389d00a5
JB
7024 is_crc32 = (i.tm.base_opcode == 0xf0
7025 && i.tm.opcode_modifier.opcodespace == SPACE_0F38
8b65b895 7026 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2);
65fca059
JB
7027
7028 /* movsx/movzx want only their source operand considered here, for the
7029 ambiguity checking below. The suffix will be replaced afterwards
7030 to represent the destination (register). */
389d00a5 7031 if (is_movx && (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63))
65fca059
JB
7032 --i.operands;
7033
643bb870 7034 /* crc32 needs REX.W set regardless of suffix / source operand size. */
8b65b895 7035 if (is_crc32 && i.tm.operand_types[1].bitfield.qword)
643bb870
JB
7036 i.rex |= REX_W;
7037
29b0f896 7038 /* If there's no instruction mnemonic suffix we try to invent one
13e600d0 7039 based on GPR operands. */
29b0f896
AM
7040 if (!i.suffix)
7041 {
7042 /* We take i.suffix from the last register operand specified,
7043 Destination register type is more significant than source
381d071f
L
7044 register type. crc32 in SSE4.2 prefers source register
7045 type. */
8b65b895 7046 unsigned int op = is_crc32 ? 1 : i.operands;
20592a94 7047
1a035124
JB
7048 while (op--)
7049 if (i.tm.operand_types[op].bitfield.instance == InstanceNone
7050 || i.tm.operand_types[op].bitfield.instance == Accum)
7051 {
7052 if (i.types[op].bitfield.class != Reg)
7053 continue;
7054 if (i.types[op].bitfield.byte)
7055 i.suffix = BYTE_MNEM_SUFFIX;
7056 else if (i.types[op].bitfield.word)
7057 i.suffix = WORD_MNEM_SUFFIX;
7058 else if (i.types[op].bitfield.dword)
7059 i.suffix = LONG_MNEM_SUFFIX;
7060 else if (i.types[op].bitfield.qword)
7061 i.suffix = QWORD_MNEM_SUFFIX;
7062 else
7063 continue;
7064 break;
7065 }
65fca059
JB
7066
7067 /* As an exception, movsx/movzx silently default to a byte source
7068 in AT&T mode. */
389d00a5 7069 if (is_movx && i.tm.opcode_modifier.w && !i.suffix && !intel_syntax)
65fca059 7070 i.suffix = BYTE_MNEM_SUFFIX;
29b0f896
AM
7071 }
7072 else if (i.suffix == BYTE_MNEM_SUFFIX)
7073 {
2eb952a4 7074 if (intel_syntax
3cd7f3e3 7075 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
2eb952a4
L
7076 && i.tm.opcode_modifier.no_bsuf)
7077 i.suffix = 0;
7078 else if (!check_byte_reg ())
29b0f896
AM
7079 return 0;
7080 }
7081 else if (i.suffix == LONG_MNEM_SUFFIX)
7082 {
2eb952a4 7083 if (intel_syntax
3cd7f3e3 7084 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
9f123b91
JB
7085 && i.tm.opcode_modifier.no_lsuf
7086 && !i.tm.opcode_modifier.todword
7087 && !i.tm.opcode_modifier.toqword)
2eb952a4
L
7088 i.suffix = 0;
7089 else if (!check_long_reg ())
29b0f896
AM
7090 return 0;
7091 }
7092 else if (i.suffix == QWORD_MNEM_SUFFIX)
7093 {
955e1e6a 7094 if (intel_syntax
3cd7f3e3 7095 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
9f123b91
JB
7096 && i.tm.opcode_modifier.no_qsuf
7097 && !i.tm.opcode_modifier.todword
7098 && !i.tm.opcode_modifier.toqword)
955e1e6a
L
7099 i.suffix = 0;
7100 else if (!check_qword_reg ())
29b0f896
AM
7101 return 0;
7102 }
7103 else if (i.suffix == WORD_MNEM_SUFFIX)
7104 {
2eb952a4 7105 if (intel_syntax
3cd7f3e3 7106 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
2eb952a4
L
7107 && i.tm.opcode_modifier.no_wsuf)
7108 i.suffix = 0;
7109 else if (!check_word_reg ())
29b0f896
AM
7110 return 0;
7111 }
3cd7f3e3
L
7112 else if (intel_syntax
7113 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
7114 /* Do nothing if the instruction is going to ignore the prefix. */
7115 ;
7116 else
7117 abort ();
65fca059
JB
7118
7119 /* Undo the movsx/movzx change done above. */
7120 i.operands = numop;
29b0f896 7121 }
3cd7f3e3
L
7122 else if (i.tm.opcode_modifier.mnemonicsize == DEFAULTSIZE
7123 && !i.suffix)
29b0f896 7124 {
13e600d0
JB
7125 i.suffix = stackop_size;
7126 if (stackop_size == LONG_MNEM_SUFFIX)
06f74c5c
L
7127 {
7128 /* stackop_size is set to LONG_MNEM_SUFFIX for the
7129 .code16gcc directive to support 16-bit mode with
7130 32-bit address. For IRET without a suffix, generate
7131 16-bit IRET (opcode 0xcf) to return from an interrupt
7132 handler. */
13e600d0
JB
7133 if (i.tm.base_opcode == 0xcf)
7134 {
7135 i.suffix = WORD_MNEM_SUFFIX;
7136 as_warn (_("generating 16-bit `iret' for .code16gcc directive"));
7137 }
7138 /* Warn about changed behavior for segment register push/pop. */
7139 else if ((i.tm.base_opcode | 1) == 0x07)
7140 as_warn (_("generating 32-bit `%s', unlike earlier gas versions"),
7141 i.tm.name);
06f74c5c 7142 }
29b0f896 7143 }
c006a730 7144 else if (!i.suffix
0cfa3eb3
JB
7145 && (i.tm.opcode_modifier.jump == JUMP_ABSOLUTE
7146 || i.tm.opcode_modifier.jump == JUMP_BYTE
7147 || i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT
389d00a5
JB
7148 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
7149 && i.tm.base_opcode == 0x01 /* [ls][gi]dt */
64e74474 7150 && i.tm.extension_opcode <= 3)))
9306ca4a
JB
7151 {
7152 switch (flag_code)
7153 {
7154 case CODE_64BIT:
40fb9820 7155 if (!i.tm.opcode_modifier.no_qsuf)
9306ca4a 7156 {
828c2a25
JB
7157 if (i.tm.opcode_modifier.jump == JUMP_BYTE
7158 || i.tm.opcode_modifier.no_lsuf)
7159 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a
JB
7160 break;
7161 }
1a0670f3 7162 /* Fall through. */
9306ca4a 7163 case CODE_32BIT:
40fb9820 7164 if (!i.tm.opcode_modifier.no_lsuf)
9306ca4a
JB
7165 i.suffix = LONG_MNEM_SUFFIX;
7166 break;
7167 case CODE_16BIT:
40fb9820 7168 if (!i.tm.opcode_modifier.no_wsuf)
9306ca4a
JB
7169 i.suffix = WORD_MNEM_SUFFIX;
7170 break;
7171 }
7172 }
252b5132 7173
c006a730 7174 if (!i.suffix
3cd7f3e3 7175 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8
JB
7176 /* Also cover lret/retf/iret in 64-bit mode. */
7177 || (flag_code == CODE_64BIT
7178 && !i.tm.opcode_modifier.no_lsuf
7179 && !i.tm.opcode_modifier.no_qsuf))
3cd7f3e3 7180 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
8bbb3ad8
JB
7181 /* Explicit sizing prefixes are assumed to disambiguate insns. */
7182 && !i.prefix[DATA_PREFIX] && !(i.prefix[REX_PREFIX] & REX_W)
62b3f548
JB
7183 /* Accept FLDENV et al without suffix. */
7184 && (i.tm.opcode_modifier.no_ssuf || i.tm.opcode_modifier.floatmf))
29b0f896 7185 {
6c0946d0 7186 unsigned int suffixes, evex = 0;
c006a730
JB
7187
7188 suffixes = !i.tm.opcode_modifier.no_bsuf;
7189 if (!i.tm.opcode_modifier.no_wsuf)
7190 suffixes |= 1 << 1;
7191 if (!i.tm.opcode_modifier.no_lsuf)
7192 suffixes |= 1 << 2;
7193 if (!i.tm.opcode_modifier.no_ldsuf)
7194 suffixes |= 1 << 3;
7195 if (!i.tm.opcode_modifier.no_ssuf)
7196 suffixes |= 1 << 4;
7197 if (flag_code == CODE_64BIT && !i.tm.opcode_modifier.no_qsuf)
7198 suffixes |= 1 << 5;
7199
6c0946d0
JB
7200 /* For [XYZ]MMWORD operands inspect operand sizes. While generally
7201 also suitable for AT&T syntax mode, it was requested that this be
7202 restricted to just Intel syntax. */
a5748e0d
JB
7203 if (intel_syntax && is_any_vex_encoding (&i.tm)
7204 && !i.broadcast.type && !i.broadcast.bytes)
6c0946d0 7205 {
b9915cbc 7206 unsigned int op;
6c0946d0 7207
b9915cbc 7208 for (op = 0; op < i.tm.operands; ++op)
6c0946d0 7209 {
b9915cbc
JB
7210 if (is_evex_encoding (&i.tm)
7211 && !cpu_arch_flags.bitfield.cpuavx512vl)
6c0946d0 7212 {
b9915cbc
JB
7213 if (i.tm.operand_types[op].bitfield.ymmword)
7214 i.tm.operand_types[op].bitfield.xmmword = 0;
7215 if (i.tm.operand_types[op].bitfield.zmmword)
7216 i.tm.operand_types[op].bitfield.ymmword = 0;
7217 if (!i.tm.opcode_modifier.evex
7218 || i.tm.opcode_modifier.evex == EVEXDYN)
7219 i.tm.opcode_modifier.evex = EVEX512;
7220 }
6c0946d0 7221
b9915cbc
JB
7222 if (i.tm.operand_types[op].bitfield.xmmword
7223 + i.tm.operand_types[op].bitfield.ymmword
7224 + i.tm.operand_types[op].bitfield.zmmword < 2)
7225 continue;
6c0946d0 7226
b9915cbc
JB
7227 /* Any properly sized operand disambiguates the insn. */
7228 if (i.types[op].bitfield.xmmword
7229 || i.types[op].bitfield.ymmword
7230 || i.types[op].bitfield.zmmword)
7231 {
7232 suffixes &= ~(7 << 6);
7233 evex = 0;
7234 break;
7235 }
6c0946d0 7236
b9915cbc
JB
7237 if ((i.flags[op] & Operand_Mem)
7238 && i.tm.operand_types[op].bitfield.unspecified)
7239 {
7240 if (i.tm.operand_types[op].bitfield.xmmword)
7241 suffixes |= 1 << 6;
7242 if (i.tm.operand_types[op].bitfield.ymmword)
7243 suffixes |= 1 << 7;
7244 if (i.tm.operand_types[op].bitfield.zmmword)
7245 suffixes |= 1 << 8;
7246 if (is_evex_encoding (&i.tm))
7247 evex = EVEX512;
6c0946d0
JB
7248 }
7249 }
7250 }
7251
7252 /* Are multiple suffixes / operand sizes allowed? */
c006a730 7253 if (suffixes & (suffixes - 1))
9306ca4a 7254 {
873494c8 7255 if (intel_syntax
3cd7f3e3 7256 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8 7257 || operand_check == check_error))
9306ca4a 7258 {
c006a730 7259 as_bad (_("ambiguous operand size for `%s'"), i.tm.name);
9306ca4a
JB
7260 return 0;
7261 }
c006a730 7262 if (operand_check == check_error)
9306ca4a 7263 {
c006a730
JB
7264 as_bad (_("no instruction mnemonic suffix given and "
7265 "no register operands; can't size `%s'"), i.tm.name);
9306ca4a
JB
7266 return 0;
7267 }
c006a730 7268 if (operand_check == check_warning)
873494c8
JB
7269 as_warn (_("%s; using default for `%s'"),
7270 intel_syntax
7271 ? _("ambiguous operand size")
7272 : _("no instruction mnemonic suffix given and "
7273 "no register operands"),
7274 i.tm.name);
c006a730
JB
7275
7276 if (i.tm.opcode_modifier.floatmf)
7277 i.suffix = SHORT_MNEM_SUFFIX;
389d00a5 7278 else if (is_movx)
65fca059 7279 /* handled below */;
6c0946d0
JB
7280 else if (evex)
7281 i.tm.opcode_modifier.evex = evex;
c006a730
JB
7282 else if (flag_code == CODE_16BIT)
7283 i.suffix = WORD_MNEM_SUFFIX;
1a035124 7284 else if (!i.tm.opcode_modifier.no_lsuf)
c006a730 7285 i.suffix = LONG_MNEM_SUFFIX;
1a035124
JB
7286 else
7287 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a 7288 }
29b0f896 7289 }
252b5132 7290
389d00a5 7291 if (is_movx)
65fca059
JB
7292 {
7293 /* In Intel syntax, movsx/movzx must have a "suffix" (checked above).
7294 In AT&T syntax, if there is no suffix (warned about above), the default
7295 will be byte extension. */
7296 if (i.tm.opcode_modifier.w && i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
7297 i.tm.base_opcode |= 1;
7298
7299 /* For further processing, the suffix should represent the destination
7300 (register). This is already the case when one was used with
7301 mov[sz][bw]*, but we need to replace it for mov[sz]x, or if there was
7302 no suffix to begin with. */
7303 if (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63 || !i.suffix)
7304 {
7305 if (i.types[1].bitfield.word)
7306 i.suffix = WORD_MNEM_SUFFIX;
7307 else if (i.types[1].bitfield.qword)
7308 i.suffix = QWORD_MNEM_SUFFIX;
7309 else
7310 i.suffix = LONG_MNEM_SUFFIX;
7311
7312 i.tm.opcode_modifier.w = 0;
7313 }
7314 }
7315
50128d0c
JB
7316 if (!i.tm.opcode_modifier.modrm && i.reg_operands && i.tm.operands < 3)
7317 i.short_form = (i.tm.operand_types[0].bitfield.class == Reg)
7318 != (i.tm.operand_types[1].bitfield.class == Reg);
7319
d2224064
JB
7320 /* Change the opcode based on the operand size given by i.suffix. */
7321 switch (i.suffix)
29b0f896 7322 {
d2224064
JB
7323 /* Size floating point instruction. */
7324 case LONG_MNEM_SUFFIX:
7325 if (i.tm.opcode_modifier.floatmf)
7326 {
7327 i.tm.base_opcode ^= 4;
7328 break;
7329 }
7330 /* fall through */
7331 case WORD_MNEM_SUFFIX:
7332 case QWORD_MNEM_SUFFIX:
29b0f896 7333 /* It's not a byte, select word/dword operation. */
40fb9820 7334 if (i.tm.opcode_modifier.w)
29b0f896 7335 {
50128d0c 7336 if (i.short_form)
29b0f896
AM
7337 i.tm.base_opcode |= 8;
7338 else
7339 i.tm.base_opcode |= 1;
7340 }
d2224064
JB
7341 /* fall through */
7342 case SHORT_MNEM_SUFFIX:
29b0f896
AM
7343 /* Now select between word & dword operations via the operand
7344 size prefix, except for instructions that will ignore this
7345 prefix anyway. */
c8f8eebc 7346 if (i.suffix != QWORD_MNEM_SUFFIX
3cd7f3e3 7347 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
c8f8eebc
JB
7348 && !i.tm.opcode_modifier.floatmf
7349 && !is_any_vex_encoding (&i.tm)
7350 && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
7351 || (flag_code == CODE_64BIT
7352 && i.tm.opcode_modifier.jump == JUMP_BYTE)))
24eab124
AM
7353 {
7354 unsigned int prefix = DATA_PREFIX_OPCODE;
543613e9 7355
0cfa3eb3 7356 if (i.tm.opcode_modifier.jump == JUMP_BYTE) /* jcxz, loop */
29b0f896 7357 prefix = ADDR_PREFIX_OPCODE;
252b5132 7358
29b0f896
AM
7359 if (!add_prefix (prefix))
7360 return 0;
24eab124 7361 }
252b5132 7362
29b0f896
AM
7363 /* Set mode64 for an operand. */
7364 if (i.suffix == QWORD_MNEM_SUFFIX
9146926a 7365 && flag_code == CODE_64BIT
d2224064 7366 && !i.tm.opcode_modifier.norex64
4ed21b58 7367 && !i.tm.opcode_modifier.vexw
46e883c5 7368 /* Special case for xchg %rax,%rax. It is NOP and doesn't
d2224064
JB
7369 need rex64. */
7370 && ! (i.operands == 2
7371 && i.tm.base_opcode == 0x90
7372 && i.tm.extension_opcode == None
75e5731b
JB
7373 && i.types[0].bitfield.instance == Accum
7374 && i.types[0].bitfield.qword
7375 && i.types[1].bitfield.instance == Accum
7376 && i.types[1].bitfield.qword))
d2224064 7377 i.rex |= REX_W;
3e73aa7c 7378
d2224064 7379 break;
8bbb3ad8
JB
7380
7381 case 0:
f9a6a8f0 7382 /* Select word/dword/qword operation with explicit data sizing prefix
8bbb3ad8
JB
7383 when there are no suitable register operands. */
7384 if (i.tm.opcode_modifier.w
7385 && (i.prefix[DATA_PREFIX] || (i.prefix[REX_PREFIX] & REX_W))
7386 && (!i.reg_operands
7387 || (i.reg_operands == 1
7388 /* ShiftCount */
7389 && (i.tm.operand_types[0].bitfield.instance == RegC
7390 /* InOutPortReg */
7391 || i.tm.operand_types[0].bitfield.instance == RegD
7392 || i.tm.operand_types[1].bitfield.instance == RegD
7393 /* CRC32 */
8b65b895 7394 || is_crc32))))
8bbb3ad8
JB
7395 i.tm.base_opcode |= 1;
7396 break;
29b0f896 7397 }
7ecd2f8b 7398
c8f8eebc 7399 if (i.tm.opcode_modifier.addrprefixopreg)
c0a30a9f 7400 {
c8f8eebc
JB
7401 gas_assert (!i.suffix);
7402 gas_assert (i.reg_operands);
c0a30a9f 7403
c8f8eebc
JB
7404 if (i.tm.operand_types[0].bitfield.instance == Accum
7405 || i.operands == 1)
7406 {
7407 /* The address size override prefix changes the size of the
7408 first operand. */
7409 if (flag_code == CODE_64BIT
7410 && i.op[0].regs->reg_type.bitfield.word)
7411 {
7412 as_bad (_("16-bit addressing unavailable for `%s'"),
7413 i.tm.name);
7414 return 0;
7415 }
7416
7417 if ((flag_code == CODE_32BIT
7418 ? i.op[0].regs->reg_type.bitfield.word
7419 : i.op[0].regs->reg_type.bitfield.dword)
7420 && !add_prefix (ADDR_PREFIX_OPCODE))
7421 return 0;
7422 }
c0a30a9f
L
7423 else
7424 {
c8f8eebc
JB
7425 /* Check invalid register operand when the address size override
7426 prefix changes the size of register operands. */
7427 unsigned int op;
7428 enum { need_word, need_dword, need_qword } need;
7429
27f13469 7430 /* Check the register operand for the address size prefix if
b3a3496f 7431 the memory operand has no real registers, like symbol, DISP
829f3fe1 7432 or bogus (x32-only) symbol(%rip) when symbol(%eip) is meant. */
27f13469
L
7433 if (i.mem_operands == 1
7434 && i.reg_operands == 1
7435 && i.operands == 2
27f13469 7436 && i.types[1].bitfield.class == Reg
b3a3496f
L
7437 && (flag_code == CODE_32BIT
7438 ? i.op[1].regs->reg_type.bitfield.word
7439 : i.op[1].regs->reg_type.bitfield.dword)
7440 && ((i.base_reg == NULL && i.index_reg == NULL)
829f3fe1
JB
7441#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
7442 || (x86_elf_abi == X86_64_X32_ABI
7443 && i.base_reg
b3a3496f
L
7444 && i.base_reg->reg_num == RegIP
7445 && i.base_reg->reg_type.bitfield.qword))
829f3fe1
JB
7446#else
7447 || 0)
7448#endif
27f13469
L
7449 && !add_prefix (ADDR_PREFIX_OPCODE))
7450 return 0;
7451
c8f8eebc
JB
7452 if (flag_code == CODE_32BIT)
7453 need = i.prefix[ADDR_PREFIX] ? need_word : need_dword;
7454 else if (i.prefix[ADDR_PREFIX])
c0a30a9f
L
7455 need = need_dword;
7456 else
7457 need = flag_code == CODE_64BIT ? need_qword : need_word;
c0a30a9f 7458
c8f8eebc
JB
7459 for (op = 0; op < i.operands; op++)
7460 {
7461 if (i.types[op].bitfield.class != Reg)
7462 continue;
7463
7464 switch (need)
7465 {
7466 case need_word:
7467 if (i.op[op].regs->reg_type.bitfield.word)
7468 continue;
7469 break;
7470 case need_dword:
7471 if (i.op[op].regs->reg_type.bitfield.dword)
7472 continue;
7473 break;
7474 case need_qword:
7475 if (i.op[op].regs->reg_type.bitfield.qword)
7476 continue;
7477 break;
7478 }
7479
7480 as_bad (_("invalid register operand size for `%s'"),
7481 i.tm.name);
7482 return 0;
7483 }
7484 }
c0a30a9f
L
7485 }
7486
29b0f896
AM
7487 return 1;
7488}
3e73aa7c 7489
29b0f896 7490static int
543613e9 7491check_byte_reg (void)
29b0f896
AM
7492{
7493 int op;
543613e9 7494
29b0f896
AM
7495 for (op = i.operands; --op >= 0;)
7496 {
dc821c5f 7497 /* Skip non-register operands. */
bab6aec1 7498 if (i.types[op].bitfield.class != Reg)
dc821c5f
JB
7499 continue;
7500
29b0f896
AM
7501 /* If this is an eight bit register, it's OK. If it's the 16 or
7502 32 bit version of an eight bit register, we will just use the
7503 low portion, and that's OK too. */
dc821c5f 7504 if (i.types[op].bitfield.byte)
29b0f896
AM
7505 continue;
7506
5a819eb9 7507 /* I/O port address operands are OK too. */
75e5731b
JB
7508 if (i.tm.operand_types[op].bitfield.instance == RegD
7509 && i.tm.operand_types[op].bitfield.word)
5a819eb9
JB
7510 continue;
7511
9706160a 7512 /* crc32 only wants its source operand checked here. */
389d00a5
JB
7513 if (i.tm.base_opcode == 0xf0
7514 && i.tm.opcode_modifier.opcodespace == SPACE_0F38
8b65b895
L
7515 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2
7516 && op != 0)
9344ff29
L
7517 continue;
7518
29b0f896 7519 /* Any other register is bad. */
73c76375
JB
7520 as_bad (_("`%s%s' not allowed with `%s%c'"),
7521 register_prefix, i.op[op].regs->reg_name,
7522 i.tm.name, i.suffix);
7523 return 0;
29b0f896
AM
7524 }
7525 return 1;
7526}
7527
7528static int
e3bb37b5 7529check_long_reg (void)
29b0f896
AM
7530{
7531 int op;
7532
7533 for (op = i.operands; --op >= 0;)
dc821c5f 7534 /* Skip non-register operands. */
bab6aec1 7535 if (i.types[op].bitfield.class != Reg)
dc821c5f 7536 continue;
29b0f896
AM
7537 /* Reject eight bit registers, except where the template requires
7538 them. (eg. movzb) */
dc821c5f 7539 else if (i.types[op].bitfield.byte
bab6aec1 7540 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7541 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7542 && (i.tm.operand_types[op].bitfield.word
7543 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7544 {
a540244d
L
7545 as_bad (_("`%s%s' not allowed with `%s%c'"),
7546 register_prefix,
29b0f896
AM
7547 i.op[op].regs->reg_name,
7548 i.tm.name,
7549 i.suffix);
7550 return 0;
7551 }
be4c5e58
L
7552 /* Error if the e prefix on a general reg is missing. */
7553 else if (i.types[op].bitfield.word
bab6aec1 7554 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7555 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7556 && i.tm.operand_types[op].bitfield.dword)
29b0f896 7557 {
be4c5e58
L
7558 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7559 register_prefix, i.op[op].regs->reg_name,
7560 i.suffix);
7561 return 0;
252b5132 7562 }
e4630f71 7563 /* Warn if the r prefix on a general reg is present. */
dc821c5f 7564 else if (i.types[op].bitfield.qword
bab6aec1 7565 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7566 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7567 && i.tm.operand_types[op].bitfield.dword)
252b5132 7568 {
34828aad 7569 if (intel_syntax
65fca059 7570 && i.tm.opcode_modifier.toqword
3528c362 7571 && i.types[0].bitfield.class != RegSIMD)
34828aad 7572 {
ca61edf2 7573 /* Convert to QWORD. We want REX byte. */
34828aad
L
7574 i.suffix = QWORD_MNEM_SUFFIX;
7575 }
7576 else
7577 {
2b5d6a91 7578 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
34828aad
L
7579 register_prefix, i.op[op].regs->reg_name,
7580 i.suffix);
7581 return 0;
7582 }
29b0f896
AM
7583 }
7584 return 1;
7585}
252b5132 7586
29b0f896 7587static int
e3bb37b5 7588check_qword_reg (void)
29b0f896
AM
7589{
7590 int op;
252b5132 7591
29b0f896 7592 for (op = i.operands; --op >= 0; )
dc821c5f 7593 /* Skip non-register operands. */
bab6aec1 7594 if (i.types[op].bitfield.class != Reg)
dc821c5f 7595 continue;
29b0f896
AM
7596 /* Reject eight bit registers, except where the template requires
7597 them. (eg. movzb) */
dc821c5f 7598 else if (i.types[op].bitfield.byte
bab6aec1 7599 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7600 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7601 && (i.tm.operand_types[op].bitfield.word
7602 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7603 {
a540244d
L
7604 as_bad (_("`%s%s' not allowed with `%s%c'"),
7605 register_prefix,
29b0f896
AM
7606 i.op[op].regs->reg_name,
7607 i.tm.name,
7608 i.suffix);
7609 return 0;
7610 }
e4630f71 7611 /* Warn if the r prefix on a general reg is missing. */
dc821c5f
JB
7612 else if ((i.types[op].bitfield.word
7613 || i.types[op].bitfield.dword)
bab6aec1 7614 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7615 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7616 && i.tm.operand_types[op].bitfield.qword)
29b0f896
AM
7617 {
7618 /* Prohibit these changes in the 64bit mode, since the
7619 lowering is more complicated. */
34828aad 7620 if (intel_syntax
ca61edf2 7621 && i.tm.opcode_modifier.todword
3528c362 7622 && i.types[0].bitfield.class != RegSIMD)
34828aad 7623 {
ca61edf2 7624 /* Convert to DWORD. We don't want REX byte. */
34828aad
L
7625 i.suffix = LONG_MNEM_SUFFIX;
7626 }
7627 else
7628 {
2b5d6a91 7629 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
34828aad
L
7630 register_prefix, i.op[op].regs->reg_name,
7631 i.suffix);
7632 return 0;
7633 }
252b5132 7634 }
29b0f896
AM
7635 return 1;
7636}
252b5132 7637
29b0f896 7638static int
e3bb37b5 7639check_word_reg (void)
29b0f896
AM
7640{
7641 int op;
7642 for (op = i.operands; --op >= 0;)
dc821c5f 7643 /* Skip non-register operands. */
bab6aec1 7644 if (i.types[op].bitfield.class != Reg)
dc821c5f 7645 continue;
29b0f896
AM
7646 /* Reject eight bit registers, except where the template requires
7647 them. (eg. movzb) */
dc821c5f 7648 else if (i.types[op].bitfield.byte
bab6aec1 7649 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7650 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7651 && (i.tm.operand_types[op].bitfield.word
7652 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7653 {
a540244d
L
7654 as_bad (_("`%s%s' not allowed with `%s%c'"),
7655 register_prefix,
29b0f896
AM
7656 i.op[op].regs->reg_name,
7657 i.tm.name,
7658 i.suffix);
7659 return 0;
7660 }
9706160a
JB
7661 /* Error if the e or r prefix on a general reg is present. */
7662 else if ((i.types[op].bitfield.dword
dc821c5f 7663 || i.types[op].bitfield.qword)
bab6aec1 7664 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7665 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7666 && i.tm.operand_types[op].bitfield.word)
252b5132 7667 {
9706160a
JB
7668 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7669 register_prefix, i.op[op].regs->reg_name,
7670 i.suffix);
7671 return 0;
29b0f896 7672 }
0cc78721
CL
7673 /* For some instructions need encode as EVEX.W=1 without explicit VexW1. */
7674 else if (i.types[op].bitfield.qword
7675 && intel_syntax
7676 && i.tm.opcode_modifier.toqword)
7677 {
7678 /* Convert to QWORD. We want EVEX.W byte. */
7679 i.suffix = QWORD_MNEM_SUFFIX;
7680 }
29b0f896
AM
7681 return 1;
7682}
252b5132 7683
29b0f896 7684static int
40fb9820 7685update_imm (unsigned int j)
29b0f896 7686{
bc0844ae 7687 i386_operand_type overlap = i.types[j];
40fb9820
L
7688 if ((overlap.bitfield.imm8
7689 || overlap.bitfield.imm8s
7690 || overlap.bitfield.imm16
7691 || overlap.bitfield.imm32
7692 || overlap.bitfield.imm32s
7693 || overlap.bitfield.imm64)
0dfbf9d7
L
7694 && !operand_type_equal (&overlap, &imm8)
7695 && !operand_type_equal (&overlap, &imm8s)
7696 && !operand_type_equal (&overlap, &imm16)
7697 && !operand_type_equal (&overlap, &imm32)
7698 && !operand_type_equal (&overlap, &imm32s)
7699 && !operand_type_equal (&overlap, &imm64))
29b0f896
AM
7700 {
7701 if (i.suffix)
7702 {
40fb9820
L
7703 i386_operand_type temp;
7704
0dfbf9d7 7705 operand_type_set (&temp, 0);
7ab9ffdd 7706 if (i.suffix == BYTE_MNEM_SUFFIX)
40fb9820
L
7707 {
7708 temp.bitfield.imm8 = overlap.bitfield.imm8;
7709 temp.bitfield.imm8s = overlap.bitfield.imm8s;
7710 }
7711 else if (i.suffix == WORD_MNEM_SUFFIX)
7712 temp.bitfield.imm16 = overlap.bitfield.imm16;
7713 else if (i.suffix == QWORD_MNEM_SUFFIX)
7714 {
7715 temp.bitfield.imm64 = overlap.bitfield.imm64;
7716 temp.bitfield.imm32s = overlap.bitfield.imm32s;
7717 }
7718 else
7719 temp.bitfield.imm32 = overlap.bitfield.imm32;
7720 overlap = temp;
29b0f896 7721 }
0dfbf9d7
L
7722 else if (operand_type_equal (&overlap, &imm16_32_32s)
7723 || operand_type_equal (&overlap, &imm16_32)
7724 || operand_type_equal (&overlap, &imm16_32s))
29b0f896 7725 {
40fb9820 7726 if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
65da13b5 7727 overlap = imm16;
40fb9820 7728 else
65da13b5 7729 overlap = imm32s;
29b0f896 7730 }
8bbb3ad8
JB
7731 else if (i.prefix[REX_PREFIX] & REX_W)
7732 overlap = operand_type_and (overlap, imm32s);
7733 else if (i.prefix[DATA_PREFIX])
7734 overlap = operand_type_and (overlap,
7735 flag_code != CODE_16BIT ? imm16 : imm32);
0dfbf9d7
L
7736 if (!operand_type_equal (&overlap, &imm8)
7737 && !operand_type_equal (&overlap, &imm8s)
7738 && !operand_type_equal (&overlap, &imm16)
7739 && !operand_type_equal (&overlap, &imm32)
7740 && !operand_type_equal (&overlap, &imm32s)
7741 && !operand_type_equal (&overlap, &imm64))
29b0f896 7742 {
4eed87de
AM
7743 as_bad (_("no instruction mnemonic suffix given; "
7744 "can't determine immediate size"));
29b0f896
AM
7745 return 0;
7746 }
7747 }
40fb9820 7748 i.types[j] = overlap;
29b0f896 7749
40fb9820
L
7750 return 1;
7751}
7752
7753static int
7754finalize_imm (void)
7755{
bc0844ae 7756 unsigned int j, n;
29b0f896 7757
bc0844ae
L
7758 /* Update the first 2 immediate operands. */
7759 n = i.operands > 2 ? 2 : i.operands;
7760 if (n)
7761 {
7762 for (j = 0; j < n; j++)
7763 if (update_imm (j) == 0)
7764 return 0;
40fb9820 7765
bc0844ae
L
7766 /* The 3rd operand can't be immediate operand. */
7767 gas_assert (operand_type_check (i.types[2], imm) == 0);
7768 }
29b0f896
AM
7769
7770 return 1;
7771}
7772
7773static int
e3bb37b5 7774process_operands (void)
29b0f896
AM
7775{
7776 /* Default segment register this instruction will use for memory
7777 accesses. 0 means unknown. This is only for optimizing out
7778 unnecessary segment overrides. */
5e042380 7779 const reg_entry *default_seg = NULL;
29b0f896 7780
a5aeccd9
JB
7781 if (i.tm.opcode_modifier.sse2avx)
7782 {
7783 /* Legacy encoded insns allow explicit REX prefixes, so these prefixes
7784 need converting. */
7785 i.rex |= i.prefix[REX_PREFIX] & (REX_W | REX_R | REX_X | REX_B);
7786 i.prefix[REX_PREFIX] = 0;
7787 i.rex_encoding = 0;
7788 }
c423d21a
JB
7789 /* ImmExt should be processed after SSE2AVX. */
7790 else if (i.tm.opcode_modifier.immext)
7791 process_immext ();
a5aeccd9 7792
2426c15f 7793 if (i.tm.opcode_modifier.sse2avx && i.tm.opcode_modifier.vexvvvv)
29b0f896 7794 {
91d6fa6a
NC
7795 unsigned int dupl = i.operands;
7796 unsigned int dest = dupl - 1;
9fcfb3d7
L
7797 unsigned int j;
7798
c0f3af97 7799 /* The destination must be an xmm register. */
9c2799c2 7800 gas_assert (i.reg_operands
91d6fa6a 7801 && MAX_OPERANDS > dupl
7ab9ffdd 7802 && operand_type_equal (&i.types[dest], &regxmm));
c0f3af97 7803
75e5731b 7804 if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 7805 && i.tm.operand_types[0].bitfield.xmmword)
e2ec9d29 7806 {
8cd7925b 7807 if (i.tm.opcode_modifier.vexsources == VEX3SOURCES)
c0f3af97
L
7808 {
7809 /* Keep xmm0 for instructions with VEX prefix and 3
7810 sources. */
75e5731b 7811 i.tm.operand_types[0].bitfield.instance = InstanceNone;
3528c362 7812 i.tm.operand_types[0].bitfield.class = RegSIMD;
c0f3af97
L
7813 goto duplicate;
7814 }
e2ec9d29 7815 else
c0f3af97
L
7816 {
7817 /* We remove the first xmm0 and keep the number of
7818 operands unchanged, which in fact duplicates the
7819 destination. */
7820 for (j = 1; j < i.operands; j++)
7821 {
7822 i.op[j - 1] = i.op[j];
7823 i.types[j - 1] = i.types[j];
7824 i.tm.operand_types[j - 1] = i.tm.operand_types[j];
8dc0818e 7825 i.flags[j - 1] = i.flags[j];
c0f3af97
L
7826 }
7827 }
7828 }
7829 else if (i.tm.opcode_modifier.implicit1stxmm0)
7ab9ffdd 7830 {
91d6fa6a 7831 gas_assert ((MAX_OPERANDS - 1) > dupl
8cd7925b
L
7832 && (i.tm.opcode_modifier.vexsources
7833 == VEX3SOURCES));
c0f3af97
L
7834
7835 /* Add the implicit xmm0 for instructions with VEX prefix
7836 and 3 sources. */
7837 for (j = i.operands; j > 0; j--)
7838 {
7839 i.op[j] = i.op[j - 1];
7840 i.types[j] = i.types[j - 1];
7841 i.tm.operand_types[j] = i.tm.operand_types[j - 1];
8dc0818e 7842 i.flags[j] = i.flags[j - 1];
c0f3af97
L
7843 }
7844 i.op[0].regs
629310ab 7845 = (const reg_entry *) str_hash_find (reg_hash, "xmm0");
7ab9ffdd 7846 i.types[0] = regxmm;
c0f3af97
L
7847 i.tm.operand_types[0] = regxmm;
7848
7849 i.operands += 2;
7850 i.reg_operands += 2;
7851 i.tm.operands += 2;
7852
91d6fa6a 7853 dupl++;
c0f3af97 7854 dest++;
91d6fa6a
NC
7855 i.op[dupl] = i.op[dest];
7856 i.types[dupl] = i.types[dest];
7857 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 7858 i.flags[dupl] = i.flags[dest];
e2ec9d29 7859 }
c0f3af97
L
7860 else
7861 {
dc1e8a47 7862 duplicate:
c0f3af97
L
7863 i.operands++;
7864 i.reg_operands++;
7865 i.tm.operands++;
7866
91d6fa6a
NC
7867 i.op[dupl] = i.op[dest];
7868 i.types[dupl] = i.types[dest];
7869 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 7870 i.flags[dupl] = i.flags[dest];
c0f3af97
L
7871 }
7872
7873 if (i.tm.opcode_modifier.immext)
7874 process_immext ();
7875 }
75e5731b 7876 else if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 7877 && i.tm.operand_types[0].bitfield.xmmword)
c0f3af97
L
7878 {
7879 unsigned int j;
7880
9fcfb3d7
L
7881 for (j = 1; j < i.operands; j++)
7882 {
7883 i.op[j - 1] = i.op[j];
7884 i.types[j - 1] = i.types[j];
7885
7886 /* We need to adjust fields in i.tm since they are used by
7887 build_modrm_byte. */
7888 i.tm.operand_types [j - 1] = i.tm.operand_types [j];
8dc0818e
JB
7889
7890 i.flags[j - 1] = i.flags[j];
9fcfb3d7
L
7891 }
7892
e2ec9d29
L
7893 i.operands--;
7894 i.reg_operands--;
e2ec9d29
L
7895 i.tm.operands--;
7896 }
920d2ddc
IT
7897 else if (i.tm.opcode_modifier.implicitquadgroup)
7898 {
a477a8c4
JB
7899 unsigned int regnum, first_reg_in_group, last_reg_in_group;
7900
920d2ddc 7901 /* The second operand must be {x,y,z}mmN, where N is a multiple of 4. */
3528c362 7902 gas_assert (i.operands >= 2 && i.types[1].bitfield.class == RegSIMD);
a477a8c4
JB
7903 regnum = register_number (i.op[1].regs);
7904 first_reg_in_group = regnum & ~3;
7905 last_reg_in_group = first_reg_in_group + 3;
7906 if (regnum != first_reg_in_group)
7907 as_warn (_("source register `%s%s' implicitly denotes"
7908 " `%s%.3s%u' to `%s%.3s%u' source group in `%s'"),
7909 register_prefix, i.op[1].regs->reg_name,
7910 register_prefix, i.op[1].regs->reg_name, first_reg_in_group,
7911 register_prefix, i.op[1].regs->reg_name, last_reg_in_group,
7912 i.tm.name);
7913 }
e2ec9d29
L
7914 else if (i.tm.opcode_modifier.regkludge)
7915 {
7916 /* The imul $imm, %reg instruction is converted into
7917 imul $imm, %reg, %reg, and the clr %reg instruction
7918 is converted into xor %reg, %reg. */
7919
7920 unsigned int first_reg_op;
7921
7922 if (operand_type_check (i.types[0], reg))
7923 first_reg_op = 0;
7924 else
7925 first_reg_op = 1;
7926 /* Pretend we saw the extra register operand. */
9c2799c2 7927 gas_assert (i.reg_operands == 1
7ab9ffdd 7928 && i.op[first_reg_op + 1].regs == 0);
e2ec9d29
L
7929 i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
7930 i.types[first_reg_op + 1] = i.types[first_reg_op];
7931 i.operands++;
7932 i.reg_operands++;
29b0f896
AM
7933 }
7934
85b80b0f 7935 if (i.tm.opcode_modifier.modrm)
29b0f896
AM
7936 {
7937 /* The opcode is completed (modulo i.tm.extension_opcode which
52271982
AM
7938 must be put into the modrm byte). Now, we make the modrm and
7939 index base bytes based on all the info we've collected. */
29b0f896
AM
7940
7941 default_seg = build_modrm_byte ();
7942 }
00cee14f 7943 else if (i.types[0].bitfield.class == SReg)
85b80b0f
JB
7944 {
7945 if (flag_code != CODE_64BIT
7946 ? i.tm.base_opcode == POP_SEG_SHORT
7947 && i.op[0].regs->reg_num == 1
389d00a5 7948 : (i.tm.base_opcode | 1) == (POP_SEG386_SHORT & 0xff)
85b80b0f
JB
7949 && i.op[0].regs->reg_num < 4)
7950 {
7951 as_bad (_("you can't `%s %s%s'"),
7952 i.tm.name, register_prefix, i.op[0].regs->reg_name);
7953 return 0;
7954 }
389d00a5
JB
7955 if (i.op[0].regs->reg_num > 3
7956 && i.tm.opcode_modifier.opcodespace == SPACE_BASE )
85b80b0f 7957 {
389d00a5
JB
7958 i.tm.base_opcode ^= (POP_SEG_SHORT ^ POP_SEG386_SHORT) & 0xff;
7959 i.tm.opcode_modifier.opcodespace = SPACE_0F;
85b80b0f
JB
7960 }
7961 i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
7962 }
389d00a5
JB
7963 else if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
7964 && (i.tm.base_opcode & ~3) == MOV_AX_DISP32)
29b0f896 7965 {
5e042380 7966 default_seg = reg_ds;
29b0f896 7967 }
40fb9820 7968 else if (i.tm.opcode_modifier.isstring)
29b0f896
AM
7969 {
7970 /* For the string instructions that allow a segment override
7971 on one of their operands, the default segment is ds. */
5e042380 7972 default_seg = reg_ds;
29b0f896 7973 }
50128d0c 7974 else if (i.short_form)
85b80b0f
JB
7975 {
7976 /* The register or float register operand is in operand
7977 0 or 1. */
bab6aec1 7978 unsigned int op = i.tm.operand_types[0].bitfield.class != Reg;
85b80b0f
JB
7979
7980 /* Register goes in low 3 bits of opcode. */
7981 i.tm.base_opcode |= i.op[op].regs->reg_num;
7982 if ((i.op[op].regs->reg_flags & RegRex) != 0)
7983 i.rex |= REX_B;
7984 if (!quiet_warnings && i.tm.opcode_modifier.ugh)
7985 {
7986 /* Warn about some common errors, but press on regardless.
7987 The first case can be generated by gcc (<= 2.8.1). */
7988 if (i.operands == 2)
7989 {
7990 /* Reversed arguments on faddp, fsubp, etc. */
7991 as_warn (_("translating to `%s %s%s,%s%s'"), i.tm.name,
7992 register_prefix, i.op[!intel_syntax].regs->reg_name,
7993 register_prefix, i.op[intel_syntax].regs->reg_name);
7994 }
7995 else
7996 {
7997 /* Extraneous `l' suffix on fp insn. */
7998 as_warn (_("translating to `%s %s%s'"), i.tm.name,
7999 register_prefix, i.op[0].regs->reg_name);
8000 }
8001 }
8002 }
29b0f896 8003
514a8bb0 8004 if ((i.seg[0] || i.prefix[SEG_PREFIX])
514a8bb0 8005 && i.tm.base_opcode == 0x8d /* lea */
35648716 8006 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
514a8bb0 8007 && !is_any_vex_encoding(&i.tm))
92334ad2
JB
8008 {
8009 if (!quiet_warnings)
8010 as_warn (_("segment override on `%s' is ineffectual"), i.tm.name);
8011 if (optimize)
8012 {
8013 i.seg[0] = NULL;
8014 i.prefix[SEG_PREFIX] = 0;
8015 }
8016 }
52271982
AM
8017
8018 /* If a segment was explicitly specified, and the specified segment
b6773884
JB
8019 is neither the default nor the one already recorded from a prefix,
8020 use an opcode prefix to select it. If we never figured out what
8021 the default segment is, then default_seg will be zero at this
8022 point, and the specified segment prefix will always be used. */
8023 if (i.seg[0]
8024 && i.seg[0] != default_seg
5e042380 8025 && i386_seg_prefixes[i.seg[0]->reg_num] != i.prefix[SEG_PREFIX])
29b0f896 8026 {
5e042380 8027 if (!add_prefix (i386_seg_prefixes[i.seg[0]->reg_num]))
29b0f896
AM
8028 return 0;
8029 }
8030 return 1;
8031}
8032
a5aeccd9 8033static INLINE void set_rex_vrex (const reg_entry *r, unsigned int rex_bit,
5b7c81bd 8034 bool do_sse2avx)
a5aeccd9
JB
8035{
8036 if (r->reg_flags & RegRex)
8037 {
8038 if (i.rex & rex_bit)
8039 as_bad (_("same type of prefix used twice"));
8040 i.rex |= rex_bit;
8041 }
8042 else if (do_sse2avx && (i.rex & rex_bit) && i.vex.register_specifier)
8043 {
8044 gas_assert (i.vex.register_specifier == r);
8045 i.vex.register_specifier += 8;
8046 }
8047
8048 if (r->reg_flags & RegVRex)
8049 i.vrex |= rex_bit;
8050}
8051
5e042380 8052static const reg_entry *
e3bb37b5 8053build_modrm_byte (void)
29b0f896 8054{
5e042380 8055 const reg_entry *default_seg = NULL;
c0f3af97 8056 unsigned int source, dest;
8cd7925b 8057 int vex_3_sources;
c0f3af97 8058
8cd7925b 8059 vex_3_sources = i.tm.opcode_modifier.vexsources == VEX3SOURCES;
c0f3af97
L
8060 if (vex_3_sources)
8061 {
91d6fa6a 8062 unsigned int nds, reg_slot;
4c2c6516 8063 expressionS *exp;
c0f3af97 8064
6b8d3588 8065 dest = i.operands - 1;
c0f3af97 8066 nds = dest - 1;
922d8de8 8067
a683cc34 8068 /* There are 2 kinds of instructions:
bed3d976 8069 1. 5 operands: 4 register operands or 3 register operands
9d3bf266 8070 plus 1 memory operand plus one Imm4 operand, VexXDS, and
bed3d976 8071 VexW0 or VexW1. The destination must be either XMM, YMM or
43234a1e 8072 ZMM register.
bed3d976 8073 2. 4 operands: 4 register operands or 3 register operands
2f1bada2 8074 plus 1 memory operand, with VexXDS. */
922d8de8 8075 gas_assert ((i.reg_operands == 4
bed3d976
JB
8076 || (i.reg_operands == 3 && i.mem_operands == 1))
8077 && i.tm.opcode_modifier.vexvvvv == VEXXDS
dcd7e323 8078 && i.tm.opcode_modifier.vexw
3528c362 8079 && i.tm.operand_types[dest].bitfield.class == RegSIMD);
a683cc34 8080
48db9223
JB
8081 /* If VexW1 is set, the first non-immediate operand is the source and
8082 the second non-immediate one is encoded in the immediate operand. */
8083 if (i.tm.opcode_modifier.vexw == VEXW1)
8084 {
8085 source = i.imm_operands;
8086 reg_slot = i.imm_operands + 1;
8087 }
8088 else
8089 {
8090 source = i.imm_operands + 1;
8091 reg_slot = i.imm_operands;
8092 }
8093
a683cc34 8094 if (i.imm_operands == 0)
bed3d976
JB
8095 {
8096 /* When there is no immediate operand, generate an 8bit
8097 immediate operand to encode the first operand. */
8098 exp = &im_expressions[i.imm_operands++];
8099 i.op[i.operands].imms = exp;
8100 i.types[i.operands] = imm8;
8101 i.operands++;
8102
3528c362 8103 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
bed3d976
JB
8104 exp->X_op = O_constant;
8105 exp->X_add_number = register_number (i.op[reg_slot].regs) << 4;
43234a1e
L
8106 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
8107 }
922d8de8 8108 else
bed3d976 8109 {
9d3bf266
JB
8110 gas_assert (i.imm_operands == 1);
8111 gas_assert (fits_in_imm4 (i.op[0].imms->X_add_number));
8112 gas_assert (!i.tm.opcode_modifier.immext);
a683cc34 8113
9d3bf266
JB
8114 /* Turn on Imm8 again so that output_imm will generate it. */
8115 i.types[0].bitfield.imm8 = 1;
bed3d976 8116
3528c362 8117 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
9d3bf266 8118 i.op[0].imms->X_add_number
bed3d976 8119 |= register_number (i.op[reg_slot].regs) << 4;
43234a1e 8120 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
bed3d976 8121 }
a683cc34 8122
3528c362 8123 gas_assert (i.tm.operand_types[nds].bitfield.class == RegSIMD);
dae39acc 8124 i.vex.register_specifier = i.op[nds].regs;
c0f3af97
L
8125 }
8126 else
8127 source = dest = 0;
29b0f896
AM
8128
8129 /* i.reg_operands MUST be the number of real register operands;
c0f3af97
L
8130 implicit registers do not count. If there are 3 register
8131 operands, it must be a instruction with VexNDS. For a
8132 instruction with VexNDD, the destination register is encoded
8133 in VEX prefix. If there are 4 register operands, it must be
8134 a instruction with VEX prefix and 3 sources. */
7ab9ffdd
L
8135 if (i.mem_operands == 0
8136 && ((i.reg_operands == 2
2426c15f 8137 && i.tm.opcode_modifier.vexvvvv <= VEXXDS)
7ab9ffdd 8138 || (i.reg_operands == 3
2426c15f 8139 && i.tm.opcode_modifier.vexvvvv == VEXXDS)
7ab9ffdd 8140 || (i.reg_operands == 4 && vex_3_sources)))
29b0f896 8141 {
cab737b9
L
8142 switch (i.operands)
8143 {
8144 case 2:
8145 source = 0;
8146 break;
8147 case 3:
c81128dc
L
8148 /* When there are 3 operands, one of them may be immediate,
8149 which may be the first or the last operand. Otherwise,
c0f3af97
L
8150 the first operand must be shift count register (cl) or it
8151 is an instruction with VexNDS. */
9c2799c2 8152 gas_assert (i.imm_operands == 1
7ab9ffdd 8153 || (i.imm_operands == 0
2426c15f 8154 && (i.tm.opcode_modifier.vexvvvv == VEXXDS
75e5731b
JB
8155 || (i.types[0].bitfield.instance == RegC
8156 && i.types[0].bitfield.byte))));
40fb9820 8157 if (operand_type_check (i.types[0], imm)
75e5731b
JB
8158 || (i.types[0].bitfield.instance == RegC
8159 && i.types[0].bitfield.byte))
40fb9820
L
8160 source = 1;
8161 else
8162 source = 0;
cab737b9
L
8163 break;
8164 case 4:
368d64cc
L
8165 /* When there are 4 operands, the first two must be 8bit
8166 immediate operands. The source operand will be the 3rd
c0f3af97
L
8167 one.
8168
8169 For instructions with VexNDS, if the first operand
8170 an imm8, the source operand is the 2nd one. If the last
8171 operand is imm8, the source operand is the first one. */
9c2799c2 8172 gas_assert ((i.imm_operands == 2
7ab9ffdd
L
8173 && i.types[0].bitfield.imm8
8174 && i.types[1].bitfield.imm8)
2426c15f 8175 || (i.tm.opcode_modifier.vexvvvv == VEXXDS
7ab9ffdd
L
8176 && i.imm_operands == 1
8177 && (i.types[0].bitfield.imm8
cf665fee 8178 || i.types[i.operands - 1].bitfield.imm8)));
9f2670f2
L
8179 if (i.imm_operands == 2)
8180 source = 2;
8181 else
c0f3af97
L
8182 {
8183 if (i.types[0].bitfield.imm8)
8184 source = 1;
8185 else
8186 source = 0;
8187 }
c0f3af97
L
8188 break;
8189 case 5:
cf665fee
JB
8190 gas_assert (!is_evex_encoding (&i.tm));
8191 gas_assert (i.imm_operands == 1 && vex_3_sources);
cab737b9
L
8192 break;
8193 default:
8194 abort ();
8195 }
8196
c0f3af97
L
8197 if (!vex_3_sources)
8198 {
8199 dest = source + 1;
8200
2426c15f 8201 if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
c0f3af97 8202 {
43234a1e 8203 /* For instructions with VexNDS, the register-only source
c5d0745b 8204 operand must be a 32/64bit integer, XMM, YMM, ZMM, or mask
dfd69174 8205 register. It is encoded in VEX prefix. */
f12dc422
L
8206
8207 i386_operand_type op;
8208 unsigned int vvvv;
8209
c2ecccb3
L
8210 /* Swap two source operands if needed. */
8211 if (i.tm.opcode_modifier.swapsources)
f12dc422
L
8212 {
8213 vvvv = source;
8214 source = dest;
8215 }
8216 else
8217 vvvv = dest;
8218
8219 op = i.tm.operand_types[vvvv];
c0f3af97 8220 if ((dest + 1) >= i.operands
bab6aec1 8221 || ((op.bitfield.class != Reg
dc821c5f 8222 || (!op.bitfield.dword && !op.bitfield.qword))
3528c362 8223 && op.bitfield.class != RegSIMD
43234a1e 8224 && !operand_type_equal (&op, &regmask)))
c0f3af97 8225 abort ();
f12dc422 8226 i.vex.register_specifier = i.op[vvvv].regs;
c0f3af97
L
8227 dest++;
8228 }
8229 }
29b0f896
AM
8230
8231 i.rm.mode = 3;
dfd69174
JB
8232 /* One of the register operands will be encoded in the i.rm.reg
8233 field, the other in the combined i.rm.mode and i.rm.regmem
29b0f896
AM
8234 fields. If no form of this instruction supports a memory
8235 destination operand, then we assume the source operand may
8236 sometimes be a memory operand and so we need to store the
8237 destination in the i.rm.reg field. */
dfd69174 8238 if (!i.tm.opcode_modifier.regmem
40fb9820 8239 && operand_type_check (i.tm.operand_types[dest], anymem) == 0)
29b0f896
AM
8240 {
8241 i.rm.reg = i.op[dest].regs->reg_num;
8242 i.rm.regmem = i.op[source].regs->reg_num;
a5aeccd9 8243 set_rex_vrex (i.op[dest].regs, REX_R, i.tm.opcode_modifier.sse2avx);
5b7c81bd 8244 set_rex_vrex (i.op[source].regs, REX_B, false);
29b0f896
AM
8245 }
8246 else
8247 {
8248 i.rm.reg = i.op[source].regs->reg_num;
8249 i.rm.regmem = i.op[dest].regs->reg_num;
a5aeccd9 8250 set_rex_vrex (i.op[dest].regs, REX_B, i.tm.opcode_modifier.sse2avx);
5b7c81bd 8251 set_rex_vrex (i.op[source].regs, REX_R, false);
29b0f896 8252 }
e0c7f900 8253 if (flag_code != CODE_64BIT && (i.rex & REX_R))
c4a530c5 8254 {
4a5c67ed 8255 if (i.types[!i.tm.opcode_modifier.regmem].bitfield.class != RegCR)
c4a530c5 8256 abort ();
e0c7f900 8257 i.rex &= ~REX_R;
c4a530c5
JB
8258 add_prefix (LOCK_PREFIX_OPCODE);
8259 }
29b0f896
AM
8260 }
8261 else
8262 { /* If it's not 2 reg operands... */
c0f3af97
L
8263 unsigned int mem;
8264
29b0f896
AM
8265 if (i.mem_operands)
8266 {
8267 unsigned int fake_zero_displacement = 0;
99018f42 8268 unsigned int op;
4eed87de 8269
7ab9ffdd 8270 for (op = 0; op < i.operands; op++)
8dc0818e 8271 if (i.flags[op] & Operand_Mem)
7ab9ffdd 8272 break;
7ab9ffdd 8273 gas_assert (op < i.operands);
29b0f896 8274
63112cd6 8275 if (i.tm.opcode_modifier.sib)
6c30d220 8276 {
260cd341
LC
8277 /* The index register of VSIB shouldn't be RegIZ. */
8278 if (i.tm.opcode_modifier.sib != SIBMEM
8279 && i.index_reg->reg_num == RegIZ)
6c30d220
L
8280 abort ();
8281
8282 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8283 if (!i.base_reg)
8284 {
8285 i.sib.base = NO_BASE_REGISTER;
8286 i.sib.scale = i.log2_scale_factor;
2f2be86b 8287 i.types[op] = operand_type_and_not (i.types[op], anydisp);
a775efc8 8288 i.types[op].bitfield.disp32 = 1;
6c30d220 8289 }
260cd341
LC
8290
8291 /* Since the mandatory SIB always has index register, so
8292 the code logic remains unchanged. The non-mandatory SIB
8293 without index register is allowed and will be handled
8294 later. */
8295 if (i.index_reg)
8296 {
8297 if (i.index_reg->reg_num == RegIZ)
8298 i.sib.index = NO_INDEX_REGISTER;
8299 else
8300 i.sib.index = i.index_reg->reg_num;
5b7c81bd 8301 set_rex_vrex (i.index_reg, REX_X, false);
260cd341 8302 }
6c30d220
L
8303 }
8304
5e042380 8305 default_seg = reg_ds;
29b0f896
AM
8306
8307 if (i.base_reg == 0)
8308 {
8309 i.rm.mode = 0;
8310 if (!i.disp_operands)
9bb129e8 8311 fake_zero_displacement = 1;
29b0f896
AM
8312 if (i.index_reg == 0)
8313 {
260cd341
LC
8314 /* Both check for VSIB and mandatory non-vector SIB. */
8315 gas_assert (!i.tm.opcode_modifier.sib
8316 || i.tm.opcode_modifier.sib == SIBMEM);
29b0f896 8317 /* Operand is just <disp> */
2f2be86b 8318 i.types[op] = operand_type_and_not (i.types[op], anydisp);
20f0a1fc 8319 if (flag_code == CODE_64BIT)
29b0f896
AM
8320 {
8321 /* 64bit mode overwrites the 32bit absolute
8322 addressing by RIP relative addressing and
8323 absolute addressing is encoded by one of the
8324 redundant SIB forms. */
8325 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8326 i.sib.base = NO_BASE_REGISTER;
8327 i.sib.index = NO_INDEX_REGISTER;
a775efc8 8328 i.types[op].bitfield.disp32 = 1;
20f0a1fc 8329 }
fc225355
L
8330 else if ((flag_code == CODE_16BIT)
8331 ^ (i.prefix[ADDR_PREFIX] != 0))
20f0a1fc
NC
8332 {
8333 i.rm.regmem = NO_BASE_REGISTER_16;
2f2be86b 8334 i.types[op].bitfield.disp16 = 1;
20f0a1fc
NC
8335 }
8336 else
8337 {
8338 i.rm.regmem = NO_BASE_REGISTER;
2f2be86b 8339 i.types[op].bitfield.disp32 = 1;
29b0f896
AM
8340 }
8341 }
63112cd6 8342 else if (!i.tm.opcode_modifier.sib)
29b0f896 8343 {
6c30d220 8344 /* !i.base_reg && i.index_reg */
e968fc9b 8345 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8346 i.sib.index = NO_INDEX_REGISTER;
8347 else
8348 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8349 i.sib.base = NO_BASE_REGISTER;
8350 i.sib.scale = i.log2_scale_factor;
8351 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
2f2be86b 8352 i.types[op] = operand_type_and_not (i.types[op], anydisp);
a775efc8 8353 i.types[op].bitfield.disp32 = 1;
29b0f896 8354 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8355 i.rex |= REX_X;
29b0f896
AM
8356 }
8357 }
8358 /* RIP addressing for 64bit mode. */
e968fc9b 8359 else if (i.base_reg->reg_num == RegIP)
29b0f896 8360 {
63112cd6 8361 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896 8362 i.rm.regmem = NO_BASE_REGISTER;
40fb9820
L
8363 i.types[op].bitfield.disp8 = 0;
8364 i.types[op].bitfield.disp16 = 0;
a775efc8 8365 i.types[op].bitfield.disp32 = 1;
40fb9820 8366 i.types[op].bitfield.disp64 = 0;
71903a11 8367 i.flags[op] |= Operand_PCrel;
20f0a1fc
NC
8368 if (! i.disp_operands)
8369 fake_zero_displacement = 1;
29b0f896 8370 }
dc821c5f 8371 else if (i.base_reg->reg_type.bitfield.word)
29b0f896 8372 {
63112cd6 8373 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896
AM
8374 switch (i.base_reg->reg_num)
8375 {
8376 case 3: /* (%bx) */
8377 if (i.index_reg == 0)
8378 i.rm.regmem = 7;
8379 else /* (%bx,%si) -> 0, or (%bx,%di) -> 1 */
8380 i.rm.regmem = i.index_reg->reg_num - 6;
8381 break;
8382 case 5: /* (%bp) */
5e042380 8383 default_seg = reg_ss;
29b0f896
AM
8384 if (i.index_reg == 0)
8385 {
8386 i.rm.regmem = 6;
40fb9820 8387 if (operand_type_check (i.types[op], disp) == 0)
29b0f896
AM
8388 {
8389 /* fake (%bp) into 0(%bp) */
41eb8e88 8390 if (i.disp_encoding == disp_encoding_16bit)
1a02d6b0
L
8391 i.types[op].bitfield.disp16 = 1;
8392 else
8393 i.types[op].bitfield.disp8 = 1;
252b5132 8394 fake_zero_displacement = 1;
29b0f896
AM
8395 }
8396 }
8397 else /* (%bp,%si) -> 2, or (%bp,%di) -> 3 */
8398 i.rm.regmem = i.index_reg->reg_num - 6 + 2;
8399 break;
8400 default: /* (%si) -> 4 or (%di) -> 5 */
8401 i.rm.regmem = i.base_reg->reg_num - 6 + 4;
8402 }
41eb8e88
L
8403 if (!fake_zero_displacement
8404 && !i.disp_operands
8405 && i.disp_encoding)
8406 {
8407 fake_zero_displacement = 1;
8408 if (i.disp_encoding == disp_encoding_8bit)
8409 i.types[op].bitfield.disp8 = 1;
8410 else
8411 i.types[op].bitfield.disp16 = 1;
8412 }
29b0f896
AM
8413 i.rm.mode = mode_from_disp_size (i.types[op]);
8414 }
8415 else /* i.base_reg and 32/64 bit mode */
8416 {
a9aabc23 8417 if (operand_type_check (i.types[op], disp))
40fb9820 8418 {
73053c1f
JB
8419 i.types[op].bitfield.disp16 = 0;
8420 i.types[op].bitfield.disp64 = 0;
a775efc8 8421 i.types[op].bitfield.disp32 = 1;
40fb9820 8422 }
20f0a1fc 8423
63112cd6 8424 if (!i.tm.opcode_modifier.sib)
6c30d220 8425 i.rm.regmem = i.base_reg->reg_num;
29b0f896 8426 if ((i.base_reg->reg_flags & RegRex) != 0)
161a04f6 8427 i.rex |= REX_B;
29b0f896
AM
8428 i.sib.base = i.base_reg->reg_num;
8429 /* x86-64 ignores REX prefix bit here to avoid decoder
8430 complications. */
848930b2
JB
8431 if (!(i.base_reg->reg_flags & RegRex)
8432 && (i.base_reg->reg_num == EBP_REG_NUM
8433 || i.base_reg->reg_num == ESP_REG_NUM))
5e042380 8434 default_seg = reg_ss;
848930b2 8435 if (i.base_reg->reg_num == 5 && i.disp_operands == 0)
29b0f896 8436 {
848930b2 8437 fake_zero_displacement = 1;
1a02d6b0
L
8438 if (i.disp_encoding == disp_encoding_32bit)
8439 i.types[op].bitfield.disp32 = 1;
8440 else
8441 i.types[op].bitfield.disp8 = 1;
29b0f896
AM
8442 }
8443 i.sib.scale = i.log2_scale_factor;
8444 if (i.index_reg == 0)
8445 {
260cd341
LC
8446 /* Only check for VSIB. */
8447 gas_assert (i.tm.opcode_modifier.sib != VECSIB128
8448 && i.tm.opcode_modifier.sib != VECSIB256
8449 && i.tm.opcode_modifier.sib != VECSIB512);
8450
29b0f896
AM
8451 /* <disp>(%esp) becomes two byte modrm with no index
8452 register. We've already stored the code for esp
8453 in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
8454 Any base register besides %esp will not use the
8455 extra modrm byte. */
8456 i.sib.index = NO_INDEX_REGISTER;
29b0f896 8457 }
63112cd6 8458 else if (!i.tm.opcode_modifier.sib)
29b0f896 8459 {
e968fc9b 8460 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8461 i.sib.index = NO_INDEX_REGISTER;
8462 else
8463 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8464 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8465 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8466 i.rex |= REX_X;
29b0f896 8467 }
67a4f2b7
AO
8468
8469 if (i.disp_operands
8470 && (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
8471 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL))
8472 i.rm.mode = 0;
8473 else
a501d77e
L
8474 {
8475 if (!fake_zero_displacement
8476 && !i.disp_operands
8477 && i.disp_encoding)
8478 {
8479 fake_zero_displacement = 1;
8480 if (i.disp_encoding == disp_encoding_8bit)
8481 i.types[op].bitfield.disp8 = 1;
8482 else
8483 i.types[op].bitfield.disp32 = 1;
8484 }
8485 i.rm.mode = mode_from_disp_size (i.types[op]);
8486 }
29b0f896 8487 }
252b5132 8488
29b0f896
AM
8489 if (fake_zero_displacement)
8490 {
8491 /* Fakes a zero displacement assuming that i.types[op]
8492 holds the correct displacement size. */
8493 expressionS *exp;
8494
9c2799c2 8495 gas_assert (i.op[op].disps == 0);
29b0f896
AM
8496 exp = &disp_expressions[i.disp_operands++];
8497 i.op[op].disps = exp;
8498 exp->X_op = O_constant;
8499 exp->X_add_number = 0;
8500 exp->X_add_symbol = (symbolS *) 0;
8501 exp->X_op_symbol = (symbolS *) 0;
8502 }
c0f3af97
L
8503
8504 mem = op;
29b0f896 8505 }
c0f3af97
L
8506 else
8507 mem = ~0;
252b5132 8508
8c43a48b 8509 if (i.tm.opcode_modifier.vexsources == XOP2SOURCES)
5dd85c99
SP
8510 {
8511 if (operand_type_check (i.types[0], imm))
8512 i.vex.register_specifier = NULL;
8513 else
8514 {
8515 /* VEX.vvvv encodes one of the sources when the first
8516 operand is not an immediate. */
1ef99a7b 8517 if (i.tm.opcode_modifier.vexw == VEXW0)
5dd85c99
SP
8518 i.vex.register_specifier = i.op[0].regs;
8519 else
8520 i.vex.register_specifier = i.op[1].regs;
8521 }
8522
8523 /* Destination is a XMM register encoded in the ModRM.reg
8524 and VEX.R bit. */
8525 i.rm.reg = i.op[2].regs->reg_num;
8526 if ((i.op[2].regs->reg_flags & RegRex) != 0)
8527 i.rex |= REX_R;
8528
8529 /* ModRM.rm and VEX.B encodes the other source. */
8530 if (!i.mem_operands)
8531 {
8532 i.rm.mode = 3;
8533
1ef99a7b 8534 if (i.tm.opcode_modifier.vexw == VEXW0)
5dd85c99
SP
8535 i.rm.regmem = i.op[1].regs->reg_num;
8536 else
8537 i.rm.regmem = i.op[0].regs->reg_num;
8538
8539 if ((i.op[1].regs->reg_flags & RegRex) != 0)
8540 i.rex |= REX_B;
8541 }
8542 }
2426c15f 8543 else if (i.tm.opcode_modifier.vexvvvv == VEXLWP)
f88c9eb0
SP
8544 {
8545 i.vex.register_specifier = i.op[2].regs;
8546 if (!i.mem_operands)
8547 {
8548 i.rm.mode = 3;
8549 i.rm.regmem = i.op[1].regs->reg_num;
8550 if ((i.op[1].regs->reg_flags & RegRex) != 0)
8551 i.rex |= REX_B;
8552 }
8553 }
29b0f896
AM
8554 /* Fill in i.rm.reg or i.rm.regmem field with register operand
8555 (if any) based on i.tm.extension_opcode. Again, we must be
8556 careful to make sure that segment/control/debug/test/MMX
8557 registers are coded into the i.rm.reg field. */
f88c9eb0 8558 else if (i.reg_operands)
29b0f896 8559 {
99018f42 8560 unsigned int op;
7ab9ffdd
L
8561 unsigned int vex_reg = ~0;
8562
8563 for (op = 0; op < i.operands; op++)
921eafea
L
8564 if (i.types[op].bitfield.class == Reg
8565 || i.types[op].bitfield.class == RegBND
8566 || i.types[op].bitfield.class == RegMask
8567 || i.types[op].bitfield.class == SReg
8568 || i.types[op].bitfield.class == RegCR
8569 || i.types[op].bitfield.class == RegDR
8570 || i.types[op].bitfield.class == RegTR
8571 || i.types[op].bitfield.class == RegSIMD
8572 || i.types[op].bitfield.class == RegMMX)
8573 break;
c0209578 8574
7ab9ffdd
L
8575 if (vex_3_sources)
8576 op = dest;
2426c15f 8577 else if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
7ab9ffdd
L
8578 {
8579 /* For instructions with VexNDS, the register-only
8580 source operand is encoded in VEX prefix. */
8581 gas_assert (mem != (unsigned int) ~0);
c0f3af97 8582
7ab9ffdd 8583 if (op > mem)
c0f3af97 8584 {
7ab9ffdd
L
8585 vex_reg = op++;
8586 gas_assert (op < i.operands);
c0f3af97
L
8587 }
8588 else
c0f3af97 8589 {
f12dc422
L
8590 /* Check register-only source operand when two source
8591 operands are swapped. */
8592 if (!i.tm.operand_types[op].bitfield.baseindex
8593 && i.tm.operand_types[op + 1].bitfield.baseindex)
8594 {
8595 vex_reg = op;
8596 op += 2;
8597 gas_assert (mem == (vex_reg + 1)
8598 && op < i.operands);
8599 }
8600 else
8601 {
8602 vex_reg = op + 1;
8603 gas_assert (vex_reg < i.operands);
8604 }
c0f3af97 8605 }
7ab9ffdd 8606 }
2426c15f 8607 else if (i.tm.opcode_modifier.vexvvvv == VEXNDD)
7ab9ffdd 8608 {
f12dc422 8609 /* For instructions with VexNDD, the register destination
7ab9ffdd 8610 is encoded in VEX prefix. */
f12dc422
L
8611 if (i.mem_operands == 0)
8612 {
8613 /* There is no memory operand. */
8614 gas_assert ((op + 2) == i.operands);
8615 vex_reg = op + 1;
8616 }
8617 else
8d63c93e 8618 {
ed438a93
JB
8619 /* There are only 2 non-immediate operands. */
8620 gas_assert (op < i.imm_operands + 2
8621 && i.operands == i.imm_operands + 2);
8622 vex_reg = i.imm_operands + 1;
f12dc422 8623 }
7ab9ffdd
L
8624 }
8625 else
8626 gas_assert (op < i.operands);
99018f42 8627
7ab9ffdd
L
8628 if (vex_reg != (unsigned int) ~0)
8629 {
f12dc422 8630 i386_operand_type *type = &i.tm.operand_types[vex_reg];
7ab9ffdd 8631
bab6aec1 8632 if ((type->bitfield.class != Reg
dc821c5f 8633 || (!type->bitfield.dword && !type->bitfield.qword))
3528c362 8634 && type->bitfield.class != RegSIMD
43234a1e 8635 && !operand_type_equal (type, &regmask))
7ab9ffdd 8636 abort ();
f88c9eb0 8637
7ab9ffdd
L
8638 i.vex.register_specifier = i.op[vex_reg].regs;
8639 }
8640
1b9f0c97
L
8641 /* Don't set OP operand twice. */
8642 if (vex_reg != op)
7ab9ffdd 8643 {
1b9f0c97
L
8644 /* If there is an extension opcode to put here, the
8645 register number must be put into the regmem field. */
8646 if (i.tm.extension_opcode != None)
8647 {
8648 i.rm.regmem = i.op[op].regs->reg_num;
a5aeccd9
JB
8649 set_rex_vrex (i.op[op].regs, REX_B,
8650 i.tm.opcode_modifier.sse2avx);
1b9f0c97
L
8651 }
8652 else
8653 {
8654 i.rm.reg = i.op[op].regs->reg_num;
a5aeccd9
JB
8655 set_rex_vrex (i.op[op].regs, REX_R,
8656 i.tm.opcode_modifier.sse2avx);
1b9f0c97 8657 }
7ab9ffdd 8658 }
252b5132 8659
29b0f896
AM
8660 /* Now, if no memory operand has set i.rm.mode = 0, 1, 2 we
8661 must set it to 3 to indicate this is a register operand
8662 in the regmem field. */
8663 if (!i.mem_operands)
8664 i.rm.mode = 3;
8665 }
252b5132 8666
29b0f896 8667 /* Fill in i.rm.reg field with extension opcode (if any). */
c1e679ec 8668 if (i.tm.extension_opcode != None)
29b0f896
AM
8669 i.rm.reg = i.tm.extension_opcode;
8670 }
8671 return default_seg;
8672}
252b5132 8673
48ef937e
JB
8674static INLINE void
8675frag_opcode_byte (unsigned char byte)
8676{
8677 if (now_seg != absolute_section)
8678 FRAG_APPEND_1_CHAR (byte);
8679 else
8680 ++abs_section_offset;
8681}
8682
376cd056
JB
8683static unsigned int
8684flip_code16 (unsigned int code16)
8685{
8686 gas_assert (i.tm.operands == 1);
8687
8688 return !(i.prefix[REX_PREFIX] & REX_W)
8689 && (code16 ? i.tm.operand_types[0].bitfield.disp32
376cd056
JB
8690 : i.tm.operand_types[0].bitfield.disp16)
8691 ? CODE16 : 0;
8692}
8693
29b0f896 8694static void
e3bb37b5 8695output_branch (void)
29b0f896
AM
8696{
8697 char *p;
f8a5c266 8698 int size;
29b0f896
AM
8699 int code16;
8700 int prefix;
8701 relax_substateT subtype;
8702 symbolS *sym;
8703 offsetT off;
8704
48ef937e
JB
8705 if (now_seg == absolute_section)
8706 {
8707 as_bad (_("relaxable branches not supported in absolute section"));
8708 return;
8709 }
8710
f8a5c266 8711 code16 = flag_code == CODE_16BIT ? CODE16 : 0;
1a42a9fe 8712 size = i.disp_encoding > disp_encoding_8bit ? BIG : SMALL;
29b0f896
AM
8713
8714 prefix = 0;
8715 if (i.prefix[DATA_PREFIX] != 0)
252b5132 8716 {
29b0f896
AM
8717 prefix = 1;
8718 i.prefixes -= 1;
376cd056 8719 code16 ^= flip_code16(code16);
252b5132 8720 }
29b0f896
AM
8721 /* Pentium4 branch hints. */
8722 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8723 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
2f66722d 8724 {
29b0f896
AM
8725 prefix++;
8726 i.prefixes--;
8727 }
8728 if (i.prefix[REX_PREFIX] != 0)
8729 {
8730 prefix++;
8731 i.prefixes--;
2f66722d
AM
8732 }
8733
7e8b059b
L
8734 /* BND prefixed jump. */
8735 if (i.prefix[BND_PREFIX] != 0)
8736 {
6cb0a70e
JB
8737 prefix++;
8738 i.prefixes--;
7e8b059b
L
8739 }
8740
f2810fe0
JB
8741 if (i.prefixes != 0)
8742 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
29b0f896
AM
8743
8744 /* It's always a symbol; End frag & setup for relax.
8745 Make sure there is enough room in this frag for the largest
8746 instruction we may generate in md_convert_frag. This is 2
8747 bytes for the opcode and room for the prefix and largest
8748 displacement. */
8749 frag_grow (prefix + 2 + 4);
8750 /* Prefix and 1 opcode byte go in fr_fix. */
8751 p = frag_more (prefix + 1);
8752 if (i.prefix[DATA_PREFIX] != 0)
8753 *p++ = DATA_PREFIX_OPCODE;
8754 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
8755 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
8756 *p++ = i.prefix[SEG_PREFIX];
6cb0a70e
JB
8757 if (i.prefix[BND_PREFIX] != 0)
8758 *p++ = BND_PREFIX_OPCODE;
29b0f896
AM
8759 if (i.prefix[REX_PREFIX] != 0)
8760 *p++ = i.prefix[REX_PREFIX];
8761 *p = i.tm.base_opcode;
8762
8763 if ((unsigned char) *p == JUMP_PC_RELATIVE)
f8a5c266 8764 subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, size);
40fb9820 8765 else if (cpu_arch_flags.bitfield.cpui386)
f8a5c266 8766 subtype = ENCODE_RELAX_STATE (COND_JUMP, size);
29b0f896 8767 else
f8a5c266 8768 subtype = ENCODE_RELAX_STATE (COND_JUMP86, size);
29b0f896 8769 subtype |= code16;
3e73aa7c 8770
29b0f896
AM
8771 sym = i.op[0].disps->X_add_symbol;
8772 off = i.op[0].disps->X_add_number;
3e73aa7c 8773
29b0f896
AM
8774 if (i.op[0].disps->X_op != O_constant
8775 && i.op[0].disps->X_op != O_symbol)
3e73aa7c 8776 {
29b0f896
AM
8777 /* Handle complex expressions. */
8778 sym = make_expr_symbol (i.op[0].disps);
8779 off = 0;
8780 }
3e73aa7c 8781
1ef3994a
JB
8782 frag_now->tc_frag_data.code64 = flag_code == CODE_64BIT;
8783
29b0f896
AM
8784 /* 1 possible extra opcode + 4 byte displacement go in var part.
8785 Pass reloc in fr_var. */
d258b828 8786 frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
29b0f896 8787}
3e73aa7c 8788
bd7ab16b
L
8789#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8790/* Return TRUE iff PLT32 relocation should be used for branching to
8791 symbol S. */
8792
5b7c81bd 8793static bool
bd7ab16b
L
8794need_plt32_p (symbolS *s)
8795{
8796 /* PLT32 relocation is ELF only. */
8797 if (!IS_ELF)
5b7c81bd 8798 return false;
bd7ab16b 8799
a5def729
RO
8800#ifdef TE_SOLARIS
8801 /* Don't emit PLT32 relocation on Solaris: neither native linker nor
8802 krtld support it. */
5b7c81bd 8803 return false;
a5def729
RO
8804#endif
8805
bd7ab16b
L
8806 /* Since there is no need to prepare for PLT branch on x86-64, we
8807 can generate R_X86_64_PLT32, instead of R_X86_64_PC32, which can
8808 be used as a marker for 32-bit PC-relative branches. */
8809 if (!object_64bit)
5b7c81bd 8810 return false;
bd7ab16b 8811
44365e88 8812 if (s == NULL)
5b7c81bd 8813 return false;
44365e88 8814
bd7ab16b
L
8815 /* Weak or undefined symbol need PLT32 relocation. */
8816 if (S_IS_WEAK (s) || !S_IS_DEFINED (s))
5b7c81bd 8817 return true;
bd7ab16b
L
8818
8819 /* Non-global symbol doesn't need PLT32 relocation. */
8820 if (! S_IS_EXTERNAL (s))
5b7c81bd 8821 return false;
bd7ab16b
L
8822
8823 /* Other global symbols need PLT32 relocation. NB: Symbol with
8824 non-default visibilities are treated as normal global symbol
8825 so that PLT32 relocation can be used as a marker for 32-bit
8826 PC-relative branches. It is useful for linker relaxation. */
5b7c81bd 8827 return true;
bd7ab16b
L
8828}
8829#endif
8830
29b0f896 8831static void
e3bb37b5 8832output_jump (void)
29b0f896
AM
8833{
8834 char *p;
8835 int size;
3e02c1cc 8836 fixS *fixP;
bd7ab16b 8837 bfd_reloc_code_real_type jump_reloc = i.reloc[0];
29b0f896 8838
0cfa3eb3 8839 if (i.tm.opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
8840 {
8841 /* This is a loop or jecxz type instruction. */
8842 size = 1;
8843 if (i.prefix[ADDR_PREFIX] != 0)
8844 {
48ef937e 8845 frag_opcode_byte (ADDR_PREFIX_OPCODE);
29b0f896
AM
8846 i.prefixes -= 1;
8847 }
8848 /* Pentium4 branch hints. */
8849 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8850 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
8851 {
48ef937e 8852 frag_opcode_byte (i.prefix[SEG_PREFIX]);
29b0f896 8853 i.prefixes--;
3e73aa7c
JH
8854 }
8855 }
29b0f896
AM
8856 else
8857 {
8858 int code16;
3e73aa7c 8859
29b0f896
AM
8860 code16 = 0;
8861 if (flag_code == CODE_16BIT)
8862 code16 = CODE16;
3e73aa7c 8863
29b0f896
AM
8864 if (i.prefix[DATA_PREFIX] != 0)
8865 {
48ef937e 8866 frag_opcode_byte (DATA_PREFIX_OPCODE);
29b0f896 8867 i.prefixes -= 1;
376cd056 8868 code16 ^= flip_code16(code16);
29b0f896 8869 }
252b5132 8870
29b0f896
AM
8871 size = 4;
8872 if (code16)
8873 size = 2;
8874 }
9fcc94b6 8875
6cb0a70e
JB
8876 /* BND prefixed jump. */
8877 if (i.prefix[BND_PREFIX] != 0)
29b0f896 8878 {
48ef937e 8879 frag_opcode_byte (i.prefix[BND_PREFIX]);
29b0f896
AM
8880 i.prefixes -= 1;
8881 }
252b5132 8882
6cb0a70e 8883 if (i.prefix[REX_PREFIX] != 0)
7e8b059b 8884 {
48ef937e 8885 frag_opcode_byte (i.prefix[REX_PREFIX]);
7e8b059b
L
8886 i.prefixes -= 1;
8887 }
8888
f2810fe0
JB
8889 if (i.prefixes != 0)
8890 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
e0890092 8891
48ef937e
JB
8892 if (now_seg == absolute_section)
8893 {
9a182d04 8894 abs_section_offset += i.opcode_length + size;
48ef937e
JB
8895 return;
8896 }
8897
9a182d04
JB
8898 p = frag_more (i.opcode_length + size);
8899 switch (i.opcode_length)
42164a71
L
8900 {
8901 case 2:
8902 *p++ = i.tm.base_opcode >> 8;
1a0670f3 8903 /* Fall through. */
42164a71
L
8904 case 1:
8905 *p++ = i.tm.base_opcode;
8906 break;
8907 default:
8908 abort ();
8909 }
e0890092 8910
bd7ab16b 8911#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1ef3994a
JB
8912 if (flag_code == CODE_64BIT && size == 4
8913 && jump_reloc == NO_RELOC && i.op[0].disps->X_add_number == 0
bd7ab16b
L
8914 && need_plt32_p (i.op[0].disps->X_add_symbol))
8915 jump_reloc = BFD_RELOC_X86_64_PLT32;
8916#endif
8917
8918 jump_reloc = reloc (size, 1, 1, jump_reloc);
8919
3e02c1cc 8920 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
bd7ab16b 8921 i.op[0].disps, 1, jump_reloc);
3e02c1cc 8922
eb19308f
JB
8923 /* All jumps handled here are signed, but don't unconditionally use a
8924 signed limit check for 32 and 16 bit jumps as we want to allow wrap
8925 around at 4G (outside of 64-bit mode) and 64k (except for XBEGIN)
8926 respectively. */
8927 switch (size)
8928 {
8929 case 1:
8930 fixP->fx_signed = 1;
8931 break;
8932
8933 case 2:
8934 if (i.tm.base_opcode == 0xc7f8)
8935 fixP->fx_signed = 1;
8936 break;
8937
8938 case 4:
8939 if (flag_code == CODE_64BIT)
8940 fixP->fx_signed = 1;
8941 break;
8942 }
29b0f896 8943}
e0890092 8944
29b0f896 8945static void
e3bb37b5 8946output_interseg_jump (void)
29b0f896
AM
8947{
8948 char *p;
8949 int size;
8950 int prefix;
8951 int code16;
252b5132 8952
29b0f896
AM
8953 code16 = 0;
8954 if (flag_code == CODE_16BIT)
8955 code16 = CODE16;
a217f122 8956
29b0f896
AM
8957 prefix = 0;
8958 if (i.prefix[DATA_PREFIX] != 0)
8959 {
8960 prefix = 1;
8961 i.prefixes -= 1;
8962 code16 ^= CODE16;
8963 }
6cb0a70e
JB
8964
8965 gas_assert (!i.prefix[REX_PREFIX]);
252b5132 8966
29b0f896
AM
8967 size = 4;
8968 if (code16)
8969 size = 2;
252b5132 8970
f2810fe0
JB
8971 if (i.prefixes != 0)
8972 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
252b5132 8973
48ef937e
JB
8974 if (now_seg == absolute_section)
8975 {
8976 abs_section_offset += prefix + 1 + 2 + size;
8977 return;
8978 }
8979
29b0f896
AM
8980 /* 1 opcode; 2 segment; offset */
8981 p = frag_more (prefix + 1 + 2 + size);
3e73aa7c 8982
29b0f896
AM
8983 if (i.prefix[DATA_PREFIX] != 0)
8984 *p++ = DATA_PREFIX_OPCODE;
252b5132 8985
29b0f896
AM
8986 if (i.prefix[REX_PREFIX] != 0)
8987 *p++ = i.prefix[REX_PREFIX];
252b5132 8988
29b0f896
AM
8989 *p++ = i.tm.base_opcode;
8990 if (i.op[1].imms->X_op == O_constant)
8991 {
8992 offsetT n = i.op[1].imms->X_add_number;
252b5132 8993
29b0f896
AM
8994 if (size == 2
8995 && !fits_in_unsigned_word (n)
8996 && !fits_in_signed_word (n))
8997 {
8998 as_bad (_("16-bit jump out of range"));
8999 return;
9000 }
9001 md_number_to_chars (p, n, size);
9002 }
9003 else
9004 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
d258b828 9005 i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
6d96a594
C
9006
9007 p += size;
9008 if (i.op[0].imms->X_op == O_constant)
9009 md_number_to_chars (p, (valueT) i.op[0].imms->X_add_number, 2);
9010 else
9011 fix_new_exp (frag_now, p - frag_now->fr_literal, 2,
9012 i.op[0].imms, 0, reloc (2, 0, 0, i.reloc[0]));
29b0f896 9013}
a217f122 9014
b4a3a7b4
L
9015#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9016void
9017x86_cleanup (void)
9018{
9019 char *p;
9020 asection *seg = now_seg;
9021 subsegT subseg = now_subseg;
9022 asection *sec;
9023 unsigned int alignment, align_size_1;
9024 unsigned int isa_1_descsz, feature_2_descsz, descsz;
9025 unsigned int isa_1_descsz_raw, feature_2_descsz_raw;
9026 unsigned int padding;
9027
1273b2f8 9028 if (!IS_ELF || !x86_used_note)
b4a3a7b4
L
9029 return;
9030
b4a3a7b4
L
9031 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X86;
9032
9033 /* The .note.gnu.property section layout:
9034
9035 Field Length Contents
9036 ---- ---- ----
9037 n_namsz 4 4
9038 n_descsz 4 The note descriptor size
9039 n_type 4 NT_GNU_PROPERTY_TYPE_0
9040 n_name 4 "GNU"
9041 n_desc n_descsz The program property array
9042 .... .... ....
9043 */
9044
9045 /* Create the .note.gnu.property section. */
9046 sec = subseg_new (NOTE_GNU_PROPERTY_SECTION_NAME, 0);
fd361982 9047 bfd_set_section_flags (sec,
b4a3a7b4
L
9048 (SEC_ALLOC
9049 | SEC_LOAD
9050 | SEC_DATA
9051 | SEC_HAS_CONTENTS
9052 | SEC_READONLY));
9053
9054 if (get_elf_backend_data (stdoutput)->s->elfclass == ELFCLASS64)
9055 {
9056 align_size_1 = 7;
9057 alignment = 3;
9058 }
9059 else
9060 {
9061 align_size_1 = 3;
9062 alignment = 2;
9063 }
9064
fd361982 9065 bfd_set_section_alignment (sec, alignment);
b4a3a7b4
L
9066 elf_section_type (sec) = SHT_NOTE;
9067
1273b2f8
L
9068 /* GNU_PROPERTY_X86_ISA_1_USED: 4-byte type + 4-byte data size
9069 + 4-byte data */
9070 isa_1_descsz_raw = 4 + 4 + 4;
9071 /* Align GNU_PROPERTY_X86_ISA_1_USED. */
9072 isa_1_descsz = (isa_1_descsz_raw + align_size_1) & ~align_size_1;
b4a3a7b4
L
9073
9074 feature_2_descsz_raw = isa_1_descsz;
9075 /* GNU_PROPERTY_X86_FEATURE_2_USED: 4-byte type + 4-byte data size
1273b2f8 9076 + 4-byte data */
b4a3a7b4
L
9077 feature_2_descsz_raw += 4 + 4 + 4;
9078 /* Align GNU_PROPERTY_X86_FEATURE_2_USED. */
9079 feature_2_descsz = ((feature_2_descsz_raw + align_size_1)
9080 & ~align_size_1);
9081
9082 descsz = feature_2_descsz;
9083 /* Section size: n_namsz + n_descsz + n_type + n_name + n_descsz. */
9084 p = frag_more (4 + 4 + 4 + 4 + descsz);
9085
9086 /* Write n_namsz. */
9087 md_number_to_chars (p, (valueT) 4, 4);
9088
9089 /* Write n_descsz. */
9090 md_number_to_chars (p + 4, (valueT) descsz, 4);
9091
9092 /* Write n_type. */
9093 md_number_to_chars (p + 4 * 2, (valueT) NT_GNU_PROPERTY_TYPE_0, 4);
9094
9095 /* Write n_name. */
9096 memcpy (p + 4 * 3, "GNU", 4);
9097
1273b2f8
L
9098 /* Write 4-byte type. */
9099 md_number_to_chars (p + 4 * 4,
9100 (valueT) GNU_PROPERTY_X86_ISA_1_USED, 4);
b4a3a7b4 9101
1273b2f8
L
9102 /* Write 4-byte data size. */
9103 md_number_to_chars (p + 4 * 5, (valueT) 4, 4);
b4a3a7b4 9104
1273b2f8
L
9105 /* Write 4-byte data. */
9106 md_number_to_chars (p + 4 * 6, (valueT) x86_isa_1_used, 4);
b4a3a7b4 9107
1273b2f8
L
9108 /* Zero out paddings. */
9109 padding = isa_1_descsz - isa_1_descsz_raw;
9110 if (padding)
9111 memset (p + 4 * 7, 0, padding);
b4a3a7b4
L
9112
9113 /* Write 4-byte type. */
9114 md_number_to_chars (p + isa_1_descsz + 4 * 4,
9115 (valueT) GNU_PROPERTY_X86_FEATURE_2_USED, 4);
9116
9117 /* Write 4-byte data size. */
9118 md_number_to_chars (p + isa_1_descsz + 4 * 5, (valueT) 4, 4);
9119
9120 /* Write 4-byte data. */
9121 md_number_to_chars (p + isa_1_descsz + 4 * 6,
9122 (valueT) x86_feature_2_used, 4);
9123
9124 /* Zero out paddings. */
9125 padding = feature_2_descsz - feature_2_descsz_raw;
9126 if (padding)
9127 memset (p + isa_1_descsz + 4 * 7, 0, padding);
9128
9129 /* We probably can't restore the current segment, for there likely
9130 isn't one yet... */
9131 if (seg && subseg)
9132 subseg_set (seg, subseg);
9133}
9134#endif
9135
9c33702b
JB
9136static unsigned int
9137encoding_length (const fragS *start_frag, offsetT start_off,
9138 const char *frag_now_ptr)
9139{
9140 unsigned int len = 0;
9141
9142 if (start_frag != frag_now)
9143 {
9144 const fragS *fr = start_frag;
9145
9146 do {
9147 len += fr->fr_fix;
9148 fr = fr->fr_next;
9149 } while (fr && fr != frag_now);
9150 }
9151
9152 return len - start_off + (frag_now_ptr - frag_now->fr_literal);
9153}
9154
e379e5f3 9155/* Return 1 for test, and, cmp, add, sub, inc and dec which may
79d72f45
HL
9156 be macro-fused with conditional jumps.
9157 NB: If TEST/AND/CMP/ADD/SUB/INC/DEC is of RIP relative address,
9158 or is one of the following format:
9159
9160 cmp m, imm
9161 add m, imm
9162 sub m, imm
9163 test m, imm
9164 and m, imm
9165 inc m
9166 dec m
9167
9168 it is unfusible. */
e379e5f3
L
9169
9170static int
79d72f45 9171maybe_fused_with_jcc_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
9172{
9173 /* No RIP address. */
9174 if (i.base_reg && i.base_reg->reg_num == RegIP)
9175 return 0;
9176
389d00a5
JB
9177 /* No opcodes outside of base encoding space. */
9178 if (i.tm.opcode_modifier.opcodespace != SPACE_BASE)
e379e5f3
L
9179 return 0;
9180
79d72f45
HL
9181 /* add, sub without add/sub m, imm. */
9182 if (i.tm.base_opcode <= 5
e379e5f3
L
9183 || (i.tm.base_opcode >= 0x28 && i.tm.base_opcode <= 0x2d)
9184 || ((i.tm.base_opcode | 3) == 0x83
79d72f45 9185 && (i.tm.extension_opcode == 0x5
e379e5f3 9186 || i.tm.extension_opcode == 0x0)))
79d72f45
HL
9187 {
9188 *mf_cmp_p = mf_cmp_alu_cmp;
9189 return !(i.mem_operands && i.imm_operands);
9190 }
e379e5f3 9191
79d72f45
HL
9192 /* and without and m, imm. */
9193 if ((i.tm.base_opcode >= 0x20 && i.tm.base_opcode <= 0x25)
9194 || ((i.tm.base_opcode | 3) == 0x83
9195 && i.tm.extension_opcode == 0x4))
9196 {
9197 *mf_cmp_p = mf_cmp_test_and;
9198 return !(i.mem_operands && i.imm_operands);
9199 }
9200
9201 /* test without test m imm. */
e379e5f3
L
9202 if ((i.tm.base_opcode | 1) == 0x85
9203 || (i.tm.base_opcode | 1) == 0xa9
9204 || ((i.tm.base_opcode | 1) == 0xf7
79d72f45
HL
9205 && i.tm.extension_opcode == 0))
9206 {
9207 *mf_cmp_p = mf_cmp_test_and;
9208 return !(i.mem_operands && i.imm_operands);
9209 }
9210
9211 /* cmp without cmp m, imm. */
9212 if ((i.tm.base_opcode >= 0x38 && i.tm.base_opcode <= 0x3d)
e379e5f3
L
9213 || ((i.tm.base_opcode | 3) == 0x83
9214 && (i.tm.extension_opcode == 0x7)))
79d72f45
HL
9215 {
9216 *mf_cmp_p = mf_cmp_alu_cmp;
9217 return !(i.mem_operands && i.imm_operands);
9218 }
e379e5f3 9219
79d72f45 9220 /* inc, dec without inc/dec m. */
e379e5f3
L
9221 if ((i.tm.cpu_flags.bitfield.cpuno64
9222 && (i.tm.base_opcode | 0xf) == 0x4f)
9223 || ((i.tm.base_opcode | 1) == 0xff
9224 && i.tm.extension_opcode <= 0x1))
79d72f45
HL
9225 {
9226 *mf_cmp_p = mf_cmp_incdec;
9227 return !i.mem_operands;
9228 }
e379e5f3
L
9229
9230 return 0;
9231}
9232
9233/* Return 1 if a FUSED_JCC_PADDING frag should be generated. */
9234
9235static int
79d72f45 9236add_fused_jcc_padding_frag_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
9237{
9238 /* NB: Don't work with COND_JUMP86 without i386. */
9239 if (!align_branch_power
9240 || now_seg == absolute_section
9241 || !cpu_arch_flags.bitfield.cpui386
9242 || !(align_branch & align_branch_fused_bit))
9243 return 0;
9244
79d72f45 9245 if (maybe_fused_with_jcc_p (mf_cmp_p))
e379e5f3
L
9246 {
9247 if (last_insn.kind == last_insn_other
9248 || last_insn.seg != now_seg)
9249 return 1;
9250 if (flag_debug)
9251 as_warn_where (last_insn.file, last_insn.line,
9252 _("`%s` skips -malign-branch-boundary on `%s`"),
9253 last_insn.name, i.tm.name);
9254 }
9255
9256 return 0;
9257}
9258
9259/* Return 1 if a BRANCH_PREFIX frag should be generated. */
9260
9261static int
9262add_branch_prefix_frag_p (void)
9263{
9264 /* NB: Don't work with COND_JUMP86 without i386. Don't add prefix
9265 to PadLock instructions since they include prefixes in opcode. */
9266 if (!align_branch_power
9267 || !align_branch_prefix_size
9268 || now_seg == absolute_section
9269 || i.tm.cpu_flags.bitfield.cpupadlock
9270 || !cpu_arch_flags.bitfield.cpui386)
9271 return 0;
9272
9273 /* Don't add prefix if it is a prefix or there is no operand in case
9274 that segment prefix is special. */
9275 if (!i.operands || i.tm.opcode_modifier.isprefix)
9276 return 0;
9277
9278 if (last_insn.kind == last_insn_other
9279 || last_insn.seg != now_seg)
9280 return 1;
9281
9282 if (flag_debug)
9283 as_warn_where (last_insn.file, last_insn.line,
9284 _("`%s` skips -malign-branch-boundary on `%s`"),
9285 last_insn.name, i.tm.name);
9286
9287 return 0;
9288}
9289
9290/* Return 1 if a BRANCH_PADDING frag should be generated. */
9291
9292static int
79d72f45
HL
9293add_branch_padding_frag_p (enum align_branch_kind *branch_p,
9294 enum mf_jcc_kind *mf_jcc_p)
e379e5f3
L
9295{
9296 int add_padding;
9297
9298 /* NB: Don't work with COND_JUMP86 without i386. */
9299 if (!align_branch_power
9300 || now_seg == absolute_section
389d00a5
JB
9301 || !cpu_arch_flags.bitfield.cpui386
9302 || i.tm.opcode_modifier.opcodespace != SPACE_BASE)
e379e5f3
L
9303 return 0;
9304
9305 add_padding = 0;
9306
9307 /* Check for jcc and direct jmp. */
9308 if (i.tm.opcode_modifier.jump == JUMP)
9309 {
9310 if (i.tm.base_opcode == JUMP_PC_RELATIVE)
9311 {
9312 *branch_p = align_branch_jmp;
9313 add_padding = align_branch & align_branch_jmp_bit;
9314 }
9315 else
9316 {
79d72f45
HL
9317 /* Because J<cc> and JN<cc> share same group in macro-fusible table,
9318 igore the lowest bit. */
9319 *mf_jcc_p = (i.tm.base_opcode & 0x0e) >> 1;
e379e5f3
L
9320 *branch_p = align_branch_jcc;
9321 if ((align_branch & align_branch_jcc_bit))
9322 add_padding = 1;
9323 }
9324 }
e379e5f3
L
9325 else if ((i.tm.base_opcode | 1) == 0xc3)
9326 {
9327 /* Near ret. */
9328 *branch_p = align_branch_ret;
9329 if ((align_branch & align_branch_ret_bit))
9330 add_padding = 1;
9331 }
9332 else
9333 {
9334 /* Check for indirect jmp, direct and indirect calls. */
9335 if (i.tm.base_opcode == 0xe8)
9336 {
9337 /* Direct call. */
9338 *branch_p = align_branch_call;
9339 if ((align_branch & align_branch_call_bit))
9340 add_padding = 1;
9341 }
9342 else if (i.tm.base_opcode == 0xff
9343 && (i.tm.extension_opcode == 2
9344 || i.tm.extension_opcode == 4))
9345 {
9346 /* Indirect call and jmp. */
9347 *branch_p = align_branch_indirect;
9348 if ((align_branch & align_branch_indirect_bit))
9349 add_padding = 1;
9350 }
9351
9352 if (add_padding
9353 && i.disp_operands
9354 && tls_get_addr
9355 && (i.op[0].disps->X_op == O_symbol
9356 || (i.op[0].disps->X_op == O_subtract
9357 && i.op[0].disps->X_op_symbol == GOT_symbol)))
9358 {
9359 symbolS *s = i.op[0].disps->X_add_symbol;
9360 /* No padding to call to global or undefined tls_get_addr. */
9361 if ((S_IS_EXTERNAL (s) || !S_IS_DEFINED (s))
9362 && strcmp (S_GET_NAME (s), tls_get_addr) == 0)
9363 return 0;
9364 }
9365 }
9366
9367 if (add_padding
9368 && last_insn.kind != last_insn_other
9369 && last_insn.seg == now_seg)
9370 {
9371 if (flag_debug)
9372 as_warn_where (last_insn.file, last_insn.line,
9373 _("`%s` skips -malign-branch-boundary on `%s`"),
9374 last_insn.name, i.tm.name);
9375 return 0;
9376 }
9377
9378 return add_padding;
9379}
9380
29b0f896 9381static void
e3bb37b5 9382output_insn (void)
29b0f896 9383{
2bbd9c25
JJ
9384 fragS *insn_start_frag;
9385 offsetT insn_start_off;
e379e5f3
L
9386 fragS *fragP = NULL;
9387 enum align_branch_kind branch = align_branch_none;
79d72f45
HL
9388 /* The initializer is arbitrary just to avoid uninitialized error.
9389 it's actually either assigned in add_branch_padding_frag_p
9390 or never be used. */
9391 enum mf_jcc_kind mf_jcc = mf_jcc_jo;
2bbd9c25 9392
b4a3a7b4 9393#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
48ef937e 9394 if (IS_ELF && x86_used_note && now_seg != absolute_section)
b4a3a7b4 9395 {
32930e4e
L
9396 if ((i.xstate & xstate_tmm) == xstate_tmm
9397 || i.tm.cpu_flags.bitfield.cpuamx_tile)
9398 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_TMM;
9399
b4a3a7b4
L
9400 if (i.tm.cpu_flags.bitfield.cpu8087
9401 || i.tm.cpu_flags.bitfield.cpu287
9402 || i.tm.cpu_flags.bitfield.cpu387
9403 || i.tm.cpu_flags.bitfield.cpu687
9404 || i.tm.cpu_flags.bitfield.cpufisttp)
9405 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X87;
014d61ea 9406
921eafea 9407 if ((i.xstate & xstate_mmx)
389d00a5
JB
9408 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
9409 && !is_any_vex_encoding (&i.tm)
9410 && (i.tm.base_opcode == 0x77 /* emms */
9411 || i.tm.base_opcode == 0x0e /* femms */)))
b4a3a7b4 9412 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MMX;
014d61ea 9413
32930e4e
L
9414 if (i.index_reg)
9415 {
9416 if (i.index_reg->reg_type.bitfield.zmmword)
9417 i.xstate |= xstate_zmm;
9418 else if (i.index_reg->reg_type.bitfield.ymmword)
9419 i.xstate |= xstate_ymm;
9420 else if (i.index_reg->reg_type.bitfield.xmmword)
9421 i.xstate |= xstate_xmm;
9422 }
014d61ea
JB
9423
9424 /* vzeroall / vzeroupper */
9425 if (i.tm.base_opcode == 0x77 && i.tm.cpu_flags.bitfield.cpuavx)
9426 i.xstate |= xstate_ymm;
9427
c4694f17 9428 if ((i.xstate & xstate_xmm)
389d00a5
JB
9429 /* ldmxcsr / stmxcsr / vldmxcsr / vstmxcsr */
9430 || (i.tm.base_opcode == 0xae
9431 && (i.tm.cpu_flags.bitfield.cpusse
9432 || i.tm.cpu_flags.bitfield.cpuavx))
c4694f17
TG
9433 || i.tm.cpu_flags.bitfield.cpuwidekl
9434 || i.tm.cpu_flags.bitfield.cpukl)
b4a3a7b4 9435 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XMM;
014d61ea 9436
921eafea 9437 if ((i.xstate & xstate_ymm) == xstate_ymm)
b4a3a7b4 9438 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_YMM;
921eafea 9439 if ((i.xstate & xstate_zmm) == xstate_zmm)
b4a3a7b4 9440 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_ZMM;
6225c532 9441 if (i.mask.reg || (i.xstate & xstate_mask) == xstate_mask)
32930e4e 9442 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MASK;
b4a3a7b4
L
9443 if (i.tm.cpu_flags.bitfield.cpufxsr)
9444 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_FXSR;
9445 if (i.tm.cpu_flags.bitfield.cpuxsave)
9446 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVE;
9447 if (i.tm.cpu_flags.bitfield.cpuxsaveopt)
9448 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT;
9449 if (i.tm.cpu_flags.bitfield.cpuxsavec)
9450 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEC;
b0ab0693
L
9451
9452 if (x86_feature_2_used
9453 || i.tm.cpu_flags.bitfield.cpucmov
9454 || i.tm.cpu_flags.bitfield.cpusyscall
389d00a5
JB
9455 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
9456 && i.tm.base_opcode == 0xc7
70e95837 9457 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
b0ab0693
L
9458 && i.tm.extension_opcode == 1) /* cmpxchg8b */)
9459 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_BASELINE;
9460 if (i.tm.cpu_flags.bitfield.cpusse3
9461 || i.tm.cpu_flags.bitfield.cpussse3
9462 || i.tm.cpu_flags.bitfield.cpusse4_1
9463 || i.tm.cpu_flags.bitfield.cpusse4_2
9464 || i.tm.cpu_flags.bitfield.cpucx16
9465 || i.tm.cpu_flags.bitfield.cpupopcnt
9466 /* LAHF-SAHF insns in 64-bit mode. */
9467 || (flag_code == CODE_64BIT
35648716
JB
9468 && (i.tm.base_opcode | 1) == 0x9f
9469 && i.tm.opcode_modifier.opcodespace == SPACE_BASE))
b0ab0693
L
9470 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V2;
9471 if (i.tm.cpu_flags.bitfield.cpuavx
9472 || i.tm.cpu_flags.bitfield.cpuavx2
a9860005
JB
9473 /* Any VEX encoded insns execpt for AVX512F, AVX512BW, AVX512DQ,
9474 XOP, FMA4, LPW, TBM, and AMX. */
b0ab0693
L
9475 || (i.tm.opcode_modifier.vex
9476 && !i.tm.cpu_flags.bitfield.cpuavx512f
9477 && !i.tm.cpu_flags.bitfield.cpuavx512bw
9478 && !i.tm.cpu_flags.bitfield.cpuavx512dq
a9860005
JB
9479 && !i.tm.cpu_flags.bitfield.cpuxop
9480 && !i.tm.cpu_flags.bitfield.cpufma4
b0ab0693
L
9481 && !i.tm.cpu_flags.bitfield.cpulwp
9482 && !i.tm.cpu_flags.bitfield.cputbm
9483 && !(x86_feature_2_used & GNU_PROPERTY_X86_FEATURE_2_TMM))
9484 || i.tm.cpu_flags.bitfield.cpuf16c
9485 || i.tm.cpu_flags.bitfield.cpufma
9486 || i.tm.cpu_flags.bitfield.cpulzcnt
9487 || i.tm.cpu_flags.bitfield.cpumovbe
9488 || i.tm.cpu_flags.bitfield.cpuxsaves
9489 || (x86_feature_2_used
9490 & (GNU_PROPERTY_X86_FEATURE_2_XSAVE
9491 | GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT
9492 | GNU_PROPERTY_X86_FEATURE_2_XSAVEC)) != 0)
9493 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V3;
9494 if (i.tm.cpu_flags.bitfield.cpuavx512f
9495 || i.tm.cpu_flags.bitfield.cpuavx512bw
9496 || i.tm.cpu_flags.bitfield.cpuavx512dq
9497 || i.tm.cpu_flags.bitfield.cpuavx512vl
a9860005
JB
9498 /* Any EVEX encoded insns except for AVX512ER, AVX512PF,
9499 AVX512-4FMAPS, and AVX512-4VNNIW. */
b0ab0693
L
9500 || (i.tm.opcode_modifier.evex
9501 && !i.tm.cpu_flags.bitfield.cpuavx512er
9502 && !i.tm.cpu_flags.bitfield.cpuavx512pf
a9860005 9503 && !i.tm.cpu_flags.bitfield.cpuavx512_4fmaps
b0ab0693
L
9504 && !i.tm.cpu_flags.bitfield.cpuavx512_4vnniw))
9505 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V4;
b4a3a7b4
L
9506 }
9507#endif
9508
29b0f896
AM
9509 /* Tie dwarf2 debug info to the address at the start of the insn.
9510 We can't do this after the insn has been output as the current
9511 frag may have been closed off. eg. by frag_var. */
9512 dwarf2_emit_insn (0);
9513
2bbd9c25
JJ
9514 insn_start_frag = frag_now;
9515 insn_start_off = frag_now_fix ();
9516
79d72f45 9517 if (add_branch_padding_frag_p (&branch, &mf_jcc))
e379e5f3
L
9518 {
9519 char *p;
9520 /* Branch can be 8 bytes. Leave some room for prefixes. */
9521 unsigned int max_branch_padding_size = 14;
9522
9523 /* Align section to boundary. */
9524 record_alignment (now_seg, align_branch_power);
9525
9526 /* Make room for padding. */
9527 frag_grow (max_branch_padding_size);
9528
9529 /* Start of the padding. */
9530 p = frag_more (0);
9531
9532 fragP = frag_now;
9533
9534 frag_var (rs_machine_dependent, max_branch_padding_size, 0,
9535 ENCODE_RELAX_STATE (BRANCH_PADDING, 0),
9536 NULL, 0, p);
9537
79d72f45 9538 fragP->tc_frag_data.mf_type = mf_jcc;
e379e5f3
L
9539 fragP->tc_frag_data.branch_type = branch;
9540 fragP->tc_frag_data.max_bytes = max_branch_padding_size;
9541 }
9542
29b0f896 9543 /* Output jumps. */
0cfa3eb3 9544 if (i.tm.opcode_modifier.jump == JUMP)
29b0f896 9545 output_branch ();
0cfa3eb3
JB
9546 else if (i.tm.opcode_modifier.jump == JUMP_BYTE
9547 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896 9548 output_jump ();
0cfa3eb3 9549 else if (i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT)
29b0f896
AM
9550 output_interseg_jump ();
9551 else
9552 {
9553 /* Output normal instructions here. */
9554 char *p;
9555 unsigned char *q;
47465058 9556 unsigned int j;
79d72f45 9557 enum mf_cmp_kind mf_cmp;
4dffcebc 9558
e4e00185 9559 if (avoid_fence
389d00a5
JB
9560 && (i.tm.base_opcode == 0xaee8
9561 || i.tm.base_opcode == 0xaef0
9562 || i.tm.base_opcode == 0xaef8))
48ef937e
JB
9563 {
9564 /* Encode lfence, mfence, and sfence as
9565 f0 83 04 24 00 lock addl $0x0, (%{re}sp). */
47f4115a
JB
9566 if (flag_code == CODE_16BIT)
9567 as_bad (_("Cannot convert `%s' in 16-bit mode"), i.tm.name);
9568 else if (omit_lock_prefix)
9569 as_bad (_("Cannot convert `%s' with `-momit-lock-prefix=yes' in effect"),
9570 i.tm.name);
9571 else if (now_seg != absolute_section)
48ef937e
JB
9572 {
9573 offsetT val = 0x240483f0ULL;
9574
9575 p = frag_more (5);
9576 md_number_to_chars (p, val, 5);
9577 }
9578 else
9579 abs_section_offset += 5;
9580 return;
9581 }
e4e00185 9582
d022bddd
IT
9583 /* Some processors fail on LOCK prefix. This options makes
9584 assembler ignore LOCK prefix and serves as a workaround. */
9585 if (omit_lock_prefix)
9586 {
35648716
JB
9587 if (i.tm.base_opcode == LOCK_PREFIX_OPCODE
9588 && i.tm.opcode_modifier.isprefix)
d022bddd
IT
9589 return;
9590 i.prefix[LOCK_PREFIX] = 0;
9591 }
9592
e379e5f3
L
9593 if (branch)
9594 /* Skip if this is a branch. */
9595 ;
79d72f45 9596 else if (add_fused_jcc_padding_frag_p (&mf_cmp))
e379e5f3
L
9597 {
9598 /* Make room for padding. */
9599 frag_grow (MAX_FUSED_JCC_PADDING_SIZE);
9600 p = frag_more (0);
9601
9602 fragP = frag_now;
9603
9604 frag_var (rs_machine_dependent, MAX_FUSED_JCC_PADDING_SIZE, 0,
9605 ENCODE_RELAX_STATE (FUSED_JCC_PADDING, 0),
9606 NULL, 0, p);
9607
79d72f45 9608 fragP->tc_frag_data.mf_type = mf_cmp;
e379e5f3
L
9609 fragP->tc_frag_data.branch_type = align_branch_fused;
9610 fragP->tc_frag_data.max_bytes = MAX_FUSED_JCC_PADDING_SIZE;
9611 }
9612 else if (add_branch_prefix_frag_p ())
9613 {
9614 unsigned int max_prefix_size = align_branch_prefix_size;
9615
9616 /* Make room for padding. */
9617 frag_grow (max_prefix_size);
9618 p = frag_more (0);
9619
9620 fragP = frag_now;
9621
9622 frag_var (rs_machine_dependent, max_prefix_size, 0,
9623 ENCODE_RELAX_STATE (BRANCH_PREFIX, 0),
9624 NULL, 0, p);
9625
9626 fragP->tc_frag_data.max_bytes = max_prefix_size;
9627 }
9628
43234a1e
L
9629 /* Since the VEX/EVEX prefix contains the implicit prefix, we
9630 don't need the explicit prefix. */
cf665fee 9631 if (!is_any_vex_encoding (&i.tm))
bc4bd9ab 9632 {
7b47a312 9633 switch (i.tm.opcode_modifier.opcodeprefix)
bc4bd9ab 9634 {
7b47a312
L
9635 case PREFIX_0X66:
9636 add_prefix (0x66);
9637 break;
9638 case PREFIX_0XF2:
9639 add_prefix (0xf2);
9640 break;
9641 case PREFIX_0XF3:
8b65b895
L
9642 if (!i.tm.cpu_flags.bitfield.cpupadlock
9643 || (i.prefix[REP_PREFIX] != 0xf3))
9644 add_prefix (0xf3);
c0f3af97 9645 break;
7b47a312 9646 case PREFIX_NONE:
9a182d04 9647 switch (i.opcode_length)
c0f3af97 9648 {
7b47a312 9649 case 2:
7b47a312 9650 break;
9a182d04 9651 case 1:
7b47a312 9652 /* Check for pseudo prefixes. */
9a182d04
JB
9653 if (!i.tm.opcode_modifier.isprefix || i.tm.base_opcode)
9654 break;
7b47a312
L
9655 as_bad_where (insn_start_frag->fr_file,
9656 insn_start_frag->fr_line,
9657 _("pseudo prefix without instruction"));
9658 return;
9659 default:
9660 abort ();
4dffcebc 9661 }
c0f3af97 9662 break;
c0f3af97
L
9663 default:
9664 abort ();
bc4bd9ab 9665 }
c0f3af97 9666
6d19a37a 9667#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
cf61b747
L
9668 /* For x32, add a dummy REX_OPCODE prefix for mov/add with
9669 R_X86_64_GOTTPOFF relocation so that linker can safely
14470f07
L
9670 perform IE->LE optimization. A dummy REX_OPCODE prefix
9671 is also needed for lea with R_X86_64_GOTPC32_TLSDESC
9672 relocation for GDesc -> IE/LE optimization. */
cf61b747
L
9673 if (x86_elf_abi == X86_64_X32_ABI
9674 && i.operands == 2
14470f07
L
9675 && (i.reloc[0] == BFD_RELOC_X86_64_GOTTPOFF
9676 || i.reloc[0] == BFD_RELOC_X86_64_GOTPC32_TLSDESC)
cf61b747
L
9677 && i.prefix[REX_PREFIX] == 0)
9678 add_prefix (REX_OPCODE);
6d19a37a 9679#endif
cf61b747 9680
c0f3af97
L
9681 /* The prefix bytes. */
9682 for (j = ARRAY_SIZE (i.prefix), q = i.prefix; j > 0; j--, q++)
9683 if (*q)
48ef937e 9684 frag_opcode_byte (*q);
0f10071e 9685 }
ae5c1c7b 9686 else
c0f3af97
L
9687 {
9688 for (j = 0, q = i.prefix; j < ARRAY_SIZE (i.prefix); j++, q++)
9689 if (*q)
9690 switch (j)
9691 {
c0f3af97
L
9692 case SEG_PREFIX:
9693 case ADDR_PREFIX:
48ef937e 9694 frag_opcode_byte (*q);
c0f3af97
L
9695 break;
9696 default:
9697 /* There should be no other prefixes for instructions
9698 with VEX prefix. */
9699 abort ();
9700 }
9701
43234a1e
L
9702 /* For EVEX instructions i.vrex should become 0 after
9703 build_evex_prefix. For VEX instructions upper 16 registers
9704 aren't available, so VREX should be 0. */
9705 if (i.vrex)
9706 abort ();
c0f3af97 9707 /* Now the VEX prefix. */
48ef937e
JB
9708 if (now_seg != absolute_section)
9709 {
9710 p = frag_more (i.vex.length);
9711 for (j = 0; j < i.vex.length; j++)
9712 p[j] = i.vex.bytes[j];
9713 }
9714 else
9715 abs_section_offset += i.vex.length;
c0f3af97 9716 }
252b5132 9717
29b0f896 9718 /* Now the opcode; be careful about word order here! */
389d00a5
JB
9719 j = i.opcode_length;
9720 if (!i.vex.length)
9721 switch (i.tm.opcode_modifier.opcodespace)
9722 {
9723 case SPACE_BASE:
9724 break;
9725 case SPACE_0F:
9726 ++j;
9727 break;
9728 case SPACE_0F38:
9729 case SPACE_0F3A:
9730 j += 2;
9731 break;
9732 default:
9733 abort ();
9734 }
9735
48ef937e 9736 if (now_seg == absolute_section)
389d00a5
JB
9737 abs_section_offset += j;
9738 else if (j == 1)
29b0f896
AM
9739 {
9740 FRAG_APPEND_1_CHAR (i.tm.base_opcode);
9741 }
9742 else
9743 {
389d00a5
JB
9744 p = frag_more (j);
9745 if (!i.vex.length
9746 && i.tm.opcode_modifier.opcodespace != SPACE_BASE)
9747 {
9748 *p++ = 0x0f;
9749 if (i.tm.opcode_modifier.opcodespace != SPACE_0F)
9750 *p++ = i.tm.opcode_modifier.opcodespace == SPACE_0F38
9751 ? 0x38 : 0x3a;
9752 }
9753
9a182d04 9754 switch (i.opcode_length)
331d2d0d 9755 {
4dffcebc 9756 case 2:
389d00a5
JB
9757 /* Put out high byte first: can't use md_number_to_chars! */
9758 *p++ = (i.tm.base_opcode >> 8) & 0xff;
9759 /* Fall through. */
9760 case 1:
9761 *p = i.tm.base_opcode & 0xff;
4dffcebc
L
9762 break;
9763 default:
9764 abort ();
9765 break;
331d2d0d 9766 }
0f10071e 9767
29b0f896 9768 }
3e73aa7c 9769
29b0f896 9770 /* Now the modrm byte and sib byte (if present). */
40fb9820 9771 if (i.tm.opcode_modifier.modrm)
29b0f896 9772 {
48ef937e
JB
9773 frag_opcode_byte ((i.rm.regmem << 0)
9774 | (i.rm.reg << 3)
9775 | (i.rm.mode << 6));
29b0f896
AM
9776 /* If i.rm.regmem == ESP (4)
9777 && i.rm.mode != (Register mode)
9778 && not 16 bit
9779 ==> need second modrm byte. */
9780 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
9781 && i.rm.mode != 3
dc821c5f 9782 && !(i.base_reg && i.base_reg->reg_type.bitfield.word))
48ef937e
JB
9783 frag_opcode_byte ((i.sib.base << 0)
9784 | (i.sib.index << 3)
9785 | (i.sib.scale << 6));
29b0f896 9786 }
3e73aa7c 9787
29b0f896 9788 if (i.disp_operands)
2bbd9c25 9789 output_disp (insn_start_frag, insn_start_off);
3e73aa7c 9790
29b0f896 9791 if (i.imm_operands)
2bbd9c25 9792 output_imm (insn_start_frag, insn_start_off);
9c33702b
JB
9793
9794 /*
9795 * frag_now_fix () returning plain abs_section_offset when we're in the
9796 * absolute section, and abs_section_offset not getting updated as data
9797 * gets added to the frag breaks the logic below.
9798 */
9799 if (now_seg != absolute_section)
9800 {
9801 j = encoding_length (insn_start_frag, insn_start_off, frag_more (0));
9802 if (j > 15)
9803 as_warn (_("instruction length of %u bytes exceeds the limit of 15"),
9804 j);
e379e5f3
L
9805 else if (fragP)
9806 {
9807 /* NB: Don't add prefix with GOTPC relocation since
9808 output_disp() above depends on the fixed encoding
9809 length. Can't add prefix with TLS relocation since
9810 it breaks TLS linker optimization. */
9811 unsigned int max = i.has_gotpc_tls_reloc ? 0 : 15 - j;
9812 /* Prefix count on the current instruction. */
9813 unsigned int count = i.vex.length;
9814 unsigned int k;
9815 for (k = 0; k < ARRAY_SIZE (i.prefix); k++)
9816 /* REX byte is encoded in VEX/EVEX prefix. */
9817 if (i.prefix[k] && (k != REX_PREFIX || !i.vex.length))
9818 count++;
9819
9820 /* Count prefixes for extended opcode maps. */
9821 if (!i.vex.length)
389d00a5 9822 switch (i.tm.opcode_modifier.opcodespace)
e379e5f3 9823 {
389d00a5 9824 case SPACE_BASE:
e379e5f3 9825 break;
389d00a5
JB
9826 case SPACE_0F:
9827 count++;
e379e5f3 9828 break;
389d00a5
JB
9829 case SPACE_0F38:
9830 case SPACE_0F3A:
9831 count += 2;
e379e5f3
L
9832 break;
9833 default:
9834 abort ();
9835 }
9836
9837 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
9838 == BRANCH_PREFIX)
9839 {
9840 /* Set the maximum prefix size in BRANCH_PREFIX
9841 frag. */
9842 if (fragP->tc_frag_data.max_bytes > max)
9843 fragP->tc_frag_data.max_bytes = max;
9844 if (fragP->tc_frag_data.max_bytes > count)
9845 fragP->tc_frag_data.max_bytes -= count;
9846 else
9847 fragP->tc_frag_data.max_bytes = 0;
9848 }
9849 else
9850 {
9851 /* Remember the maximum prefix size in FUSED_JCC_PADDING
9852 frag. */
9853 unsigned int max_prefix_size;
9854 if (align_branch_prefix_size > max)
9855 max_prefix_size = max;
9856 else
9857 max_prefix_size = align_branch_prefix_size;
9858 if (max_prefix_size > count)
9859 fragP->tc_frag_data.max_prefix_length
9860 = max_prefix_size - count;
9861 }
9862
9863 /* Use existing segment prefix if possible. Use CS
9864 segment prefix in 64-bit mode. In 32-bit mode, use SS
9865 segment prefix with ESP/EBP base register and use DS
9866 segment prefix without ESP/EBP base register. */
9867 if (i.prefix[SEG_PREFIX])
9868 fragP->tc_frag_data.default_prefix = i.prefix[SEG_PREFIX];
9869 else if (flag_code == CODE_64BIT)
9870 fragP->tc_frag_data.default_prefix = CS_PREFIX_OPCODE;
9871 else if (i.base_reg
9872 && (i.base_reg->reg_num == 4
9873 || i.base_reg->reg_num == 5))
9874 fragP->tc_frag_data.default_prefix = SS_PREFIX_OPCODE;
9875 else
9876 fragP->tc_frag_data.default_prefix = DS_PREFIX_OPCODE;
9877 }
9c33702b 9878 }
29b0f896 9879 }
252b5132 9880
e379e5f3
L
9881 /* NB: Don't work with COND_JUMP86 without i386. */
9882 if (align_branch_power
9883 && now_seg != absolute_section
9884 && cpu_arch_flags.bitfield.cpui386)
9885 {
9886 /* Terminate each frag so that we can add prefix and check for
9887 fused jcc. */
9888 frag_wane (frag_now);
9889 frag_new (0);
9890 }
9891
29b0f896
AM
9892#ifdef DEBUG386
9893 if (flag_debug)
9894 {
7b81dfbb 9895 pi ("" /*line*/, &i);
29b0f896
AM
9896 }
9897#endif /* DEBUG386 */
9898}
252b5132 9899
e205caa7
L
9900/* Return the size of the displacement operand N. */
9901
9902static int
9903disp_size (unsigned int n)
9904{
9905 int size = 4;
43234a1e 9906
b5014f7a 9907 if (i.types[n].bitfield.disp64)
40fb9820
L
9908 size = 8;
9909 else if (i.types[n].bitfield.disp8)
9910 size = 1;
9911 else if (i.types[n].bitfield.disp16)
9912 size = 2;
e205caa7
L
9913 return size;
9914}
9915
9916/* Return the size of the immediate operand N. */
9917
9918static int
9919imm_size (unsigned int n)
9920{
9921 int size = 4;
40fb9820
L
9922 if (i.types[n].bitfield.imm64)
9923 size = 8;
9924 else if (i.types[n].bitfield.imm8 || i.types[n].bitfield.imm8s)
9925 size = 1;
9926 else if (i.types[n].bitfield.imm16)
9927 size = 2;
e205caa7
L
9928 return size;
9929}
9930
29b0f896 9931static void
64e74474 9932output_disp (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
9933{
9934 char *p;
9935 unsigned int n;
252b5132 9936
29b0f896
AM
9937 for (n = 0; n < i.operands; n++)
9938 {
b5014f7a 9939 if (operand_type_check (i.types[n], disp))
29b0f896 9940 {
48ef937e
JB
9941 int size = disp_size (n);
9942
9943 if (now_seg == absolute_section)
9944 abs_section_offset += size;
9945 else if (i.op[n].disps->X_op == O_constant)
29b0f896 9946 {
43234a1e 9947 offsetT val = i.op[n].disps->X_add_number;
252b5132 9948
629cfaf1
JB
9949 val = offset_in_range (val >> (size == 1 ? i.memshift : 0),
9950 size);
29b0f896
AM
9951 p = frag_more (size);
9952 md_number_to_chars (p, val, size);
9953 }
9954 else
9955 {
f86103b7 9956 enum bfd_reloc_code_real reloc_type;
a775efc8
JB
9957 bool pcrel = (i.flags[n] & Operand_PCrel) != 0;
9958 bool sign = (flag_code == CODE_64BIT && size == 4
9959 && (!want_disp32 (&i.tm)
9960 || (i.tm.opcode_modifier.jump && !i.jumpabsolute
9961 && !i.types[n].bitfield.baseindex)))
9962 || pcrel;
02a86693 9963 fixS *fixP;
29b0f896 9964
e205caa7 9965 /* We can't have 8 bit displacement here. */
9c2799c2 9966 gas_assert (!i.types[n].bitfield.disp8);
e205caa7 9967
29b0f896
AM
9968 /* The PC relative address is computed relative
9969 to the instruction boundary, so in case immediate
9970 fields follows, we need to adjust the value. */
9971 if (pcrel && i.imm_operands)
9972 {
29b0f896 9973 unsigned int n1;
e205caa7 9974 int sz = 0;
252b5132 9975
29b0f896 9976 for (n1 = 0; n1 < i.operands; n1++)
40fb9820 9977 if (operand_type_check (i.types[n1], imm))
252b5132 9978 {
e205caa7
L
9979 /* Only one immediate is allowed for PC
9980 relative address. */
9c2799c2 9981 gas_assert (sz == 0);
e205caa7
L
9982 sz = imm_size (n1);
9983 i.op[n].disps->X_add_number -= sz;
252b5132 9984 }
29b0f896 9985 /* We should find the immediate. */
9c2799c2 9986 gas_assert (sz != 0);
29b0f896 9987 }
520dc8e8 9988
29b0f896 9989 p = frag_more (size);
d258b828 9990 reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
d6ab8113 9991 if (GOT_symbol
2bbd9c25 9992 && GOT_symbol == i.op[n].disps->X_add_symbol
d6ab8113 9993 && (((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
9994 || reloc_type == BFD_RELOC_X86_64_32S
9995 || (reloc_type == BFD_RELOC_64
9996 && object_64bit))
d6ab8113
JB
9997 && (i.op[n].disps->X_op == O_symbol
9998 || (i.op[n].disps->X_op == O_add
9999 && ((symbol_get_value_expression
10000 (i.op[n].disps->X_op_symbol)->X_op)
10001 == O_subtract))))
10002 || reloc_type == BFD_RELOC_32_PCREL))
2bbd9c25 10003 {
4fa24527 10004 if (!object_64bit)
7b81dfbb
AJ
10005 {
10006 reloc_type = BFD_RELOC_386_GOTPC;
5b7c81bd 10007 i.has_gotpc_tls_reloc = true;
98da05bf 10008 i.op[n].disps->X_add_number +=
d583596c 10009 encoding_length (insn_start_frag, insn_start_off, p);
7b81dfbb
AJ
10010 }
10011 else if (reloc_type == BFD_RELOC_64)
10012 reloc_type = BFD_RELOC_X86_64_GOTPC64;
d6ab8113 10013 else
7b81dfbb
AJ
10014 /* Don't do the adjustment for x86-64, as there
10015 the pcrel addressing is relative to the _next_
10016 insn, and that is taken care of in other code. */
d6ab8113 10017 reloc_type = BFD_RELOC_X86_64_GOTPC32;
2bbd9c25 10018 }
e379e5f3
L
10019 else if (align_branch_power)
10020 {
10021 switch (reloc_type)
10022 {
10023 case BFD_RELOC_386_TLS_GD:
10024 case BFD_RELOC_386_TLS_LDM:
10025 case BFD_RELOC_386_TLS_IE:
10026 case BFD_RELOC_386_TLS_IE_32:
10027 case BFD_RELOC_386_TLS_GOTIE:
10028 case BFD_RELOC_386_TLS_GOTDESC:
10029 case BFD_RELOC_386_TLS_DESC_CALL:
10030 case BFD_RELOC_X86_64_TLSGD:
10031 case BFD_RELOC_X86_64_TLSLD:
10032 case BFD_RELOC_X86_64_GOTTPOFF:
10033 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
10034 case BFD_RELOC_X86_64_TLSDESC_CALL:
5b7c81bd 10035 i.has_gotpc_tls_reloc = true;
e379e5f3
L
10036 default:
10037 break;
10038 }
10039 }
02a86693
L
10040 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal,
10041 size, i.op[n].disps, pcrel,
10042 reloc_type);
eb19308f
JB
10043
10044 if (flag_code == CODE_64BIT && size == 4 && pcrel
10045 && !i.prefix[ADDR_PREFIX])
10046 fixP->fx_signed = 1;
10047
02a86693
L
10048 /* Check for "call/jmp *mem", "mov mem, %reg",
10049 "test %reg, mem" and "binop mem, %reg" where binop
10050 is one of adc, add, and, cmp, or, sbb, sub, xor
e60f4d3b
L
10051 instructions without data prefix. Always generate
10052 R_386_GOT32X for "sym*GOT" operand in 32-bit mode. */
10053 if (i.prefix[DATA_PREFIX] == 0
10054 && (generate_relax_relocations
10055 || (!object_64bit
10056 && i.rm.mode == 0
10057 && i.rm.regmem == 5))
0cb4071e
L
10058 && (i.rm.mode == 2
10059 || (i.rm.mode == 0 && i.rm.regmem == 5))
389d00a5 10060 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
02a86693
L
10061 && ((i.operands == 1
10062 && i.tm.base_opcode == 0xff
10063 && (i.rm.reg == 2 || i.rm.reg == 4))
10064 || (i.operands == 2
10065 && (i.tm.base_opcode == 0x8b
10066 || i.tm.base_opcode == 0x85
2ae4c703 10067 || (i.tm.base_opcode & ~0x38) == 0x03))))
02a86693
L
10068 {
10069 if (object_64bit)
10070 {
10071 fixP->fx_tcbit = i.rex != 0;
10072 if (i.base_reg
e968fc9b 10073 && (i.base_reg->reg_num == RegIP))
02a86693
L
10074 fixP->fx_tcbit2 = 1;
10075 }
10076 else
10077 fixP->fx_tcbit2 = 1;
10078 }
29b0f896
AM
10079 }
10080 }
10081 }
10082}
252b5132 10083
29b0f896 10084static void
64e74474 10085output_imm (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
10086{
10087 char *p;
10088 unsigned int n;
252b5132 10089
29b0f896
AM
10090 for (n = 0; n < i.operands; n++)
10091 {
40fb9820 10092 if (operand_type_check (i.types[n], imm))
29b0f896 10093 {
48ef937e
JB
10094 int size = imm_size (n);
10095
10096 if (now_seg == absolute_section)
10097 abs_section_offset += size;
10098 else if (i.op[n].imms->X_op == O_constant)
29b0f896 10099 {
29b0f896 10100 offsetT val;
b4cac588 10101
29b0f896
AM
10102 val = offset_in_range (i.op[n].imms->X_add_number,
10103 size);
10104 p = frag_more (size);
10105 md_number_to_chars (p, val, size);
10106 }
10107 else
10108 {
10109 /* Not absolute_section.
10110 Need a 32-bit fixup (don't support 8bit
10111 non-absolute imms). Try to support other
10112 sizes ... */
f86103b7 10113 enum bfd_reloc_code_real reloc_type;
e205caa7 10114 int sign;
29b0f896 10115
40fb9820 10116 if (i.types[n].bitfield.imm32s
a7d61044 10117 && (i.suffix == QWORD_MNEM_SUFFIX
40fb9820 10118 || (!i.suffix && i.tm.opcode_modifier.no_lsuf)))
29b0f896 10119 sign = 1;
e205caa7
L
10120 else
10121 sign = 0;
520dc8e8 10122
29b0f896 10123 p = frag_more (size);
d258b828 10124 reloc_type = reloc (size, 0, sign, i.reloc[n]);
f86103b7 10125
2bbd9c25
JJ
10126 /* This is tough to explain. We end up with this one if we
10127 * have operands that look like
10128 * "_GLOBAL_OFFSET_TABLE_+[.-.L284]". The goal here is to
10129 * obtain the absolute address of the GOT, and it is strongly
10130 * preferable from a performance point of view to avoid using
10131 * a runtime relocation for this. The actual sequence of
10132 * instructions often look something like:
10133 *
10134 * call .L66
10135 * .L66:
10136 * popl %ebx
10137 * addl $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
10138 *
10139 * The call and pop essentially return the absolute address
10140 * of the label .L66 and store it in %ebx. The linker itself
10141 * will ultimately change the first operand of the addl so
10142 * that %ebx points to the GOT, but to keep things simple, the
10143 * .o file must have this operand set so that it generates not
10144 * the absolute address of .L66, but the absolute address of
10145 * itself. This allows the linker itself simply treat a GOTPC
10146 * relocation as asking for a pcrel offset to the GOT to be
10147 * added in, and the addend of the relocation is stored in the
10148 * operand field for the instruction itself.
10149 *
10150 * Our job here is to fix the operand so that it would add
10151 * the correct offset so that %ebx would point to itself. The
10152 * thing that is tricky is that .-.L66 will point to the
10153 * beginning of the instruction, so we need to further modify
10154 * the operand so that it will point to itself. There are
10155 * other cases where you have something like:
10156 *
10157 * .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
10158 *
10159 * and here no correction would be required. Internally in
10160 * the assembler we treat operands of this form as not being
10161 * pcrel since the '.' is explicitly mentioned, and I wonder
10162 * whether it would simplify matters to do it this way. Who
10163 * knows. In earlier versions of the PIC patches, the
10164 * pcrel_adjust field was used to store the correction, but
10165 * since the expression is not pcrel, I felt it would be
10166 * confusing to do it this way. */
10167
d6ab8113 10168 if ((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
10169 || reloc_type == BFD_RELOC_X86_64_32S
10170 || reloc_type == BFD_RELOC_64)
29b0f896
AM
10171 && GOT_symbol
10172 && GOT_symbol == i.op[n].imms->X_add_symbol
10173 && (i.op[n].imms->X_op == O_symbol
10174 || (i.op[n].imms->X_op == O_add
10175 && ((symbol_get_value_expression
10176 (i.op[n].imms->X_op_symbol)->X_op)
10177 == O_subtract))))
10178 {
4fa24527 10179 if (!object_64bit)
d6ab8113 10180 reloc_type = BFD_RELOC_386_GOTPC;
7b81dfbb 10181 else if (size == 4)
d6ab8113 10182 reloc_type = BFD_RELOC_X86_64_GOTPC32;
7b81dfbb
AJ
10183 else if (size == 8)
10184 reloc_type = BFD_RELOC_X86_64_GOTPC64;
5b7c81bd 10185 i.has_gotpc_tls_reloc = true;
d583596c
JB
10186 i.op[n].imms->X_add_number +=
10187 encoding_length (insn_start_frag, insn_start_off, p);
29b0f896 10188 }
29b0f896
AM
10189 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
10190 i.op[n].imms, 0, reloc_type);
10191 }
10192 }
10193 }
252b5132
RH
10194}
10195\f
d182319b
JB
10196/* x86_cons_fix_new is called via the expression parsing code when a
10197 reloc is needed. We use this hook to get the correct .got reloc. */
d182319b
JB
10198static int cons_sign = -1;
10199
10200void
e3bb37b5 10201x86_cons_fix_new (fragS *frag, unsigned int off, unsigned int len,
62ebcb5c 10202 expressionS *exp, bfd_reloc_code_real_type r)
d182319b 10203{
d258b828 10204 r = reloc (len, 0, cons_sign, r);
d182319b
JB
10205
10206#ifdef TE_PE
10207 if (exp->X_op == O_secrel)
10208 {
10209 exp->X_op = O_symbol;
10210 r = BFD_RELOC_32_SECREL;
10211 }
145667f8
MH
10212 else if (exp->X_op == O_secidx)
10213 r = BFD_RELOC_16_SECIDX;
d182319b
JB
10214#endif
10215
10216 fix_new_exp (frag, off, len, exp, 0, r);
10217}
10218
357d1bd8
L
10219/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
10220 purpose of the `.dc.a' internal pseudo-op. */
10221
10222int
10223x86_address_bytes (void)
10224{
10225 if ((stdoutput->arch_info->mach & bfd_mach_x64_32))
10226 return 4;
10227 return stdoutput->arch_info->bits_per_address / 8;
10228}
10229
deea4973
JB
10230#if (!(defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined (OBJ_MACH_O)) \
10231 || defined (LEX_AT)) && !defined (TE_PE)
d258b828 10232# define lex_got(reloc, adjust, types) NULL
718ddfc0 10233#else
f3c180ae
AM
10234/* Parse operands of the form
10235 <symbol>@GOTOFF+<nnn>
10236 and similar .plt or .got references.
10237
10238 If we find one, set up the correct relocation in RELOC and copy the
10239 input string, minus the `@GOTOFF' into a malloc'd buffer for
10240 parsing by the calling routine. Return this buffer, and if ADJUST
10241 is non-null set it to the length of the string we removed from the
10242 input line. Otherwise return NULL. */
10243static char *
91d6fa6a 10244lex_got (enum bfd_reloc_code_real *rel,
64e74474 10245 int *adjust,
d258b828 10246 i386_operand_type *types)
f3c180ae 10247{
7b81dfbb
AJ
10248 /* Some of the relocations depend on the size of what field is to
10249 be relocated. But in our callers i386_immediate and i386_displacement
10250 we don't yet know the operand size (this will be set by insn
10251 matching). Hence we record the word32 relocation here,
10252 and adjust the reloc according to the real size in reloc(). */
145667f8
MH
10253 static const struct
10254 {
f3c180ae 10255 const char *str;
cff8d58a 10256 int len;
4fa24527 10257 const enum bfd_reloc_code_real rel[2];
40fb9820 10258 const i386_operand_type types64;
5b7c81bd 10259 bool need_GOT_symbol;
145667f8
MH
10260 }
10261 gotrel[] =
10262 {
deea4973 10263#ifndef TE_PE
8ce3d284 10264#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
10265 { STRING_COMMA_LEN ("SIZE"), { BFD_RELOC_SIZE32,
10266 BFD_RELOC_SIZE32 },
5b7c81bd 10267 OPERAND_TYPE_IMM32_64, false },
8ce3d284 10268#endif
cff8d58a
L
10269 { STRING_COMMA_LEN ("PLTOFF"), { _dummy_first_bfd_reloc_code_real,
10270 BFD_RELOC_X86_64_PLTOFF64 },
5b7c81bd 10271 OPERAND_TYPE_IMM64, true },
cff8d58a
L
10272 { STRING_COMMA_LEN ("PLT"), { BFD_RELOC_386_PLT32,
10273 BFD_RELOC_X86_64_PLT32 },
a775efc8 10274 OPERAND_TYPE_IMM32_32S_DISP32, false },
cff8d58a
L
10275 { STRING_COMMA_LEN ("GOTPLT"), { _dummy_first_bfd_reloc_code_real,
10276 BFD_RELOC_X86_64_GOTPLT64 },
5b7c81bd 10277 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
10278 { STRING_COMMA_LEN ("GOTOFF"), { BFD_RELOC_386_GOTOFF,
10279 BFD_RELOC_X86_64_GOTOFF64 },
5b7c81bd 10280 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
10281 { STRING_COMMA_LEN ("GOTPCREL"), { _dummy_first_bfd_reloc_code_real,
10282 BFD_RELOC_X86_64_GOTPCREL },
a775efc8 10283 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10284 { STRING_COMMA_LEN ("TLSGD"), { BFD_RELOC_386_TLS_GD,
10285 BFD_RELOC_X86_64_TLSGD },
a775efc8 10286 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10287 { STRING_COMMA_LEN ("TLSLDM"), { BFD_RELOC_386_TLS_LDM,
10288 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10289 OPERAND_TYPE_NONE, true },
cff8d58a
L
10290 { STRING_COMMA_LEN ("TLSLD"), { _dummy_first_bfd_reloc_code_real,
10291 BFD_RELOC_X86_64_TLSLD },
a775efc8 10292 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10293 { STRING_COMMA_LEN ("GOTTPOFF"), { BFD_RELOC_386_TLS_IE_32,
10294 BFD_RELOC_X86_64_GOTTPOFF },
a775efc8 10295 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10296 { STRING_COMMA_LEN ("TPOFF"), { BFD_RELOC_386_TLS_LE_32,
10297 BFD_RELOC_X86_64_TPOFF32 },
a775efc8 10298 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
10299 { STRING_COMMA_LEN ("NTPOFF"), { BFD_RELOC_386_TLS_LE,
10300 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10301 OPERAND_TYPE_NONE, true },
cff8d58a
L
10302 { STRING_COMMA_LEN ("DTPOFF"), { BFD_RELOC_386_TLS_LDO_32,
10303 BFD_RELOC_X86_64_DTPOFF32 },
a775efc8 10304 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
10305 { STRING_COMMA_LEN ("GOTNTPOFF"),{ BFD_RELOC_386_TLS_GOTIE,
10306 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10307 OPERAND_TYPE_NONE, true },
cff8d58a
L
10308 { STRING_COMMA_LEN ("INDNTPOFF"),{ BFD_RELOC_386_TLS_IE,
10309 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10310 OPERAND_TYPE_NONE, true },
cff8d58a
L
10311 { STRING_COMMA_LEN ("GOT"), { BFD_RELOC_386_GOT32,
10312 BFD_RELOC_X86_64_GOT32 },
a775efc8 10313 OPERAND_TYPE_IMM32_32S_64_DISP32, true },
cff8d58a
L
10314 { STRING_COMMA_LEN ("TLSDESC"), { BFD_RELOC_386_TLS_GOTDESC,
10315 BFD_RELOC_X86_64_GOTPC32_TLSDESC },
a775efc8 10316 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10317 { STRING_COMMA_LEN ("TLSCALL"), { BFD_RELOC_386_TLS_DESC_CALL,
10318 BFD_RELOC_X86_64_TLSDESC_CALL },
a775efc8 10319 OPERAND_TYPE_IMM32_32S_DISP32, true },
deea4973
JB
10320#else /* TE_PE */
10321 { STRING_COMMA_LEN ("SECREL32"), { BFD_RELOC_32_SECREL,
10322 BFD_RELOC_32_SECREL },
a775efc8 10323 OPERAND_TYPE_IMM32_32S_64_DISP32_64, false },
deea4973 10324#endif
f3c180ae
AM
10325 };
10326 char *cp;
10327 unsigned int j;
10328
deea4973 10329#if defined (OBJ_MAYBE_ELF) && !defined (TE_PE)
718ddfc0
JB
10330 if (!IS_ELF)
10331 return NULL;
d382c579 10332#endif
718ddfc0 10333
f3c180ae 10334 for (cp = input_line_pointer; *cp != '@'; cp++)
67c11a9b 10335 if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
f3c180ae
AM
10336 return NULL;
10337
47465058 10338 for (j = 0; j < ARRAY_SIZE (gotrel); j++)
f3c180ae 10339 {
cff8d58a 10340 int len = gotrel[j].len;
28f81592 10341 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
f3c180ae 10342 {
4fa24527 10343 if (gotrel[j].rel[object_64bit] != 0)
f3c180ae 10344 {
28f81592
AM
10345 int first, second;
10346 char *tmpbuf, *past_reloc;
f3c180ae 10347
91d6fa6a 10348 *rel = gotrel[j].rel[object_64bit];
f3c180ae 10349
3956db08
JB
10350 if (types)
10351 {
10352 if (flag_code != CODE_64BIT)
40fb9820
L
10353 {
10354 types->bitfield.imm32 = 1;
10355 types->bitfield.disp32 = 1;
10356 }
3956db08
JB
10357 else
10358 *types = gotrel[j].types64;
10359 }
10360
844bf810 10361 if (gotrel[j].need_GOT_symbol && GOT_symbol == NULL)
f3c180ae
AM
10362 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
10363
28f81592 10364 /* The length of the first part of our input line. */
f3c180ae 10365 first = cp - input_line_pointer;
28f81592
AM
10366
10367 /* The second part goes from after the reloc token until
67c11a9b 10368 (and including) an end_of_line char or comma. */
28f81592 10369 past_reloc = cp + 1 + len;
67c11a9b
AM
10370 cp = past_reloc;
10371 while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
10372 ++cp;
10373 second = cp + 1 - past_reloc;
28f81592
AM
10374
10375 /* Allocate and copy string. The trailing NUL shouldn't
10376 be necessary, but be safe. */
add39d23 10377 tmpbuf = XNEWVEC (char, first + second + 2);
f3c180ae 10378 memcpy (tmpbuf, input_line_pointer, first);
0787a12d
AM
10379 if (second != 0 && *past_reloc != ' ')
10380 /* Replace the relocation token with ' ', so that
10381 errors like foo@GOTOFF1 will be detected. */
10382 tmpbuf[first++] = ' ';
af89796a
L
10383 else
10384 /* Increment length by 1 if the relocation token is
10385 removed. */
10386 len++;
10387 if (adjust)
10388 *adjust = len;
0787a12d
AM
10389 memcpy (tmpbuf + first, past_reloc, second);
10390 tmpbuf[first + second] = '\0';
f3c180ae
AM
10391 return tmpbuf;
10392 }
10393
4fa24527
JB
10394 as_bad (_("@%s reloc is not supported with %d-bit output format"),
10395 gotrel[j].str, 1 << (5 + object_64bit));
f3c180ae
AM
10396 return NULL;
10397 }
10398 }
10399
10400 /* Might be a symbol version string. Don't as_bad here. */
10401 return NULL;
10402}
4e4f7c87 10403#endif
f3c180ae 10404
62ebcb5c 10405bfd_reloc_code_real_type
e3bb37b5 10406x86_cons (expressionS *exp, int size)
f3c180ae 10407{
62ebcb5c
AM
10408 bfd_reloc_code_real_type got_reloc = NO_RELOC;
10409
2748c1b1
L
10410#if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
10411 && !defined (LEX_AT)) \
10412 || defined (TE_PE)
ee86248c
JB
10413 intel_syntax = -intel_syntax;
10414
3c7b9c2c 10415 exp->X_md = 0;
4fa24527 10416 if (size == 4 || (object_64bit && size == 8))
f3c180ae
AM
10417 {
10418 /* Handle @GOTOFF and the like in an expression. */
10419 char *save;
10420 char *gotfree_input_line;
4a57f2cf 10421 int adjust = 0;
f3c180ae
AM
10422
10423 save = input_line_pointer;
d258b828 10424 gotfree_input_line = lex_got (&got_reloc, &adjust, NULL);
f3c180ae
AM
10425 if (gotfree_input_line)
10426 input_line_pointer = gotfree_input_line;
10427
10428 expression (exp);
10429
10430 if (gotfree_input_line)
10431 {
10432 /* expression () has merrily parsed up to the end of line,
10433 or a comma - in the wrong buffer. Transfer how far
10434 input_line_pointer has moved to the right buffer. */
10435 input_line_pointer = (save
10436 + (input_line_pointer - gotfree_input_line)
10437 + adjust);
10438 free (gotfree_input_line);
3992d3b7
AM
10439 if (exp->X_op == O_constant
10440 || exp->X_op == O_absent
10441 || exp->X_op == O_illegal
0398aac5 10442 || exp->X_op == O_register
3992d3b7
AM
10443 || exp->X_op == O_big)
10444 {
10445 char c = *input_line_pointer;
10446 *input_line_pointer = 0;
10447 as_bad (_("missing or invalid expression `%s'"), save);
10448 *input_line_pointer = c;
10449 }
b9519cfe
L
10450 else if ((got_reloc == BFD_RELOC_386_PLT32
10451 || got_reloc == BFD_RELOC_X86_64_PLT32)
10452 && exp->X_op != O_symbol)
10453 {
10454 char c = *input_line_pointer;
10455 *input_line_pointer = 0;
10456 as_bad (_("invalid PLT expression `%s'"), save);
10457 *input_line_pointer = c;
10458 }
f3c180ae
AM
10459 }
10460 }
10461 else
10462 expression (exp);
ee86248c
JB
10463
10464 intel_syntax = -intel_syntax;
10465
10466 if (intel_syntax)
10467 i386_intel_simplify (exp);
2748c1b1
L
10468#else
10469 expression (exp);
10470#endif
62ebcb5c 10471
a442cac5
JB
10472 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
10473 if (size == 4 && exp->X_op == O_constant && !object_64bit)
10474 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
10475
62ebcb5c 10476 return got_reloc;
f3c180ae 10477}
f3c180ae 10478
9f32dd5b
L
10479static void
10480signed_cons (int size)
6482c264 10481{
a442cac5 10482 if (object_64bit)
d182319b
JB
10483 cons_sign = 1;
10484 cons (size);
10485 cons_sign = -1;
6482c264
NC
10486}
10487
d182319b 10488#ifdef TE_PE
6482c264 10489static void
7016a5d5 10490pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
6482c264
NC
10491{
10492 expressionS exp;
10493
10494 do
10495 {
10496 expression (&exp);
10497 if (exp.X_op == O_symbol)
10498 exp.X_op = O_secrel;
10499
10500 emit_expr (&exp, 4);
10501 }
10502 while (*input_line_pointer++ == ',');
10503
10504 input_line_pointer--;
10505 demand_empty_rest_of_line ();
10506}
145667f8
MH
10507
10508static void
10509pe_directive_secidx (int dummy ATTRIBUTE_UNUSED)
10510{
10511 expressionS exp;
10512
10513 do
10514 {
10515 expression (&exp);
10516 if (exp.X_op == O_symbol)
10517 exp.X_op = O_secidx;
10518
10519 emit_expr (&exp, 2);
10520 }
10521 while (*input_line_pointer++ == ',');
10522
10523 input_line_pointer--;
10524 demand_empty_rest_of_line ();
10525}
6482c264
NC
10526#endif
10527
7063667e
JB
10528/* Handle Rounding Control / SAE specifiers. */
10529
10530static char *
10531RC_SAE_specifier (const char *pstr)
10532{
10533 unsigned int j;
10534
10535 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); j++)
10536 {
10537 if (!strncmp (pstr, RC_NamesTable[j].name, RC_NamesTable[j].len))
10538 {
10539 if (i.rounding.type != rc_none)
10540 {
10541 as_bad (_("duplicated `{%s}'"), RC_NamesTable[j].name);
10542 return NULL;
10543 }
10544
10545 i.rounding.type = RC_NamesTable[j].type;
10546
10547 return (char *)(pstr + RC_NamesTable[j].len);
10548 }
10549 }
10550
10551 return NULL;
10552}
10553
43234a1e
L
10554/* Handle Vector operations. */
10555
10556static char *
f70c6814 10557check_VecOperations (char *op_string)
43234a1e
L
10558{
10559 const reg_entry *mask;
10560 const char *saved;
10561 char *end_op;
10562
f70c6814 10563 while (*op_string)
43234a1e
L
10564 {
10565 saved = op_string;
10566 if (*op_string == '{')
10567 {
10568 op_string++;
10569
10570 /* Check broadcasts. */
d34049e8 10571 if (startswith (op_string, "1to"))
43234a1e 10572 {
5273a3cd 10573 unsigned int bcst_type;
43234a1e 10574
5273a3cd 10575 if (i.broadcast.type)
43234a1e
L
10576 goto duplicated_vec_op;
10577
10578 op_string += 3;
10579 if (*op_string == '8')
8e6e0792 10580 bcst_type = 8;
b28d1bda 10581 else if (*op_string == '4')
8e6e0792 10582 bcst_type = 4;
b28d1bda 10583 else if (*op_string == '2')
8e6e0792 10584 bcst_type = 2;
43234a1e
L
10585 else if (*op_string == '1'
10586 && *(op_string+1) == '6')
10587 {
8e6e0792 10588 bcst_type = 16;
43234a1e
L
10589 op_string++;
10590 }
0cc78721
CL
10591 else if (*op_string == '3'
10592 && *(op_string+1) == '2')
10593 {
10594 bcst_type = 32;
10595 op_string++;
10596 }
43234a1e
L
10597 else
10598 {
10599 as_bad (_("Unsupported broadcast: `%s'"), saved);
10600 return NULL;
10601 }
10602 op_string++;
10603
5273a3cd
JB
10604 i.broadcast.type = bcst_type;
10605 i.broadcast.operand = this_operand;
43234a1e
L
10606 }
10607 /* Check masking operation. */
10608 else if ((mask = parse_register (op_string, &end_op)) != NULL)
10609 {
8a6fb3f9
JB
10610 if (mask == &bad_reg)
10611 return NULL;
10612
43234a1e 10613 /* k0 can't be used for write mask. */
f74a6307 10614 if (mask->reg_type.bitfield.class != RegMask || !mask->reg_num)
43234a1e 10615 {
6d2cd6b2
JB
10616 as_bad (_("`%s%s' can't be used for write mask"),
10617 register_prefix, mask->reg_name);
43234a1e
L
10618 return NULL;
10619 }
10620
6225c532 10621 if (!i.mask.reg)
43234a1e 10622 {
6225c532
JB
10623 i.mask.reg = mask;
10624 i.mask.operand = this_operand;
43234a1e 10625 }
6225c532
JB
10626 else if (i.mask.reg->reg_num)
10627 goto duplicated_vec_op;
43234a1e
L
10628 else
10629 {
6225c532 10630 i.mask.reg = mask;
43234a1e
L
10631
10632 /* Only "{z}" is allowed here. No need to check
10633 zeroing mask explicitly. */
6225c532 10634 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
10635 {
10636 as_bad (_("invalid write mask `%s'"), saved);
10637 return NULL;
10638 }
10639 }
10640
10641 op_string = end_op;
10642 }
10643 /* Check zeroing-flag for masking operation. */
10644 else if (*op_string == 'z')
10645 {
6225c532 10646 if (!i.mask.reg)
43234a1e 10647 {
6225c532
JB
10648 i.mask.reg = reg_k0;
10649 i.mask.zeroing = 1;
10650 i.mask.operand = this_operand;
43234a1e
L
10651 }
10652 else
10653 {
6225c532 10654 if (i.mask.zeroing)
43234a1e
L
10655 {
10656 duplicated_vec_op:
10657 as_bad (_("duplicated `%s'"), saved);
10658 return NULL;
10659 }
10660
6225c532 10661 i.mask.zeroing = 1;
43234a1e
L
10662
10663 /* Only "{%k}" is allowed here. No need to check mask
10664 register explicitly. */
6225c532 10665 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
10666 {
10667 as_bad (_("invalid zeroing-masking `%s'"),
10668 saved);
10669 return NULL;
10670 }
10671 }
10672
10673 op_string++;
10674 }
7063667e
JB
10675 else if (intel_syntax
10676 && (op_string = RC_SAE_specifier (op_string)) != NULL)
10677 i.rounding.modifier = true;
43234a1e
L
10678 else
10679 goto unknown_vec_op;
10680
10681 if (*op_string != '}')
10682 {
10683 as_bad (_("missing `}' in `%s'"), saved);
10684 return NULL;
10685 }
10686 op_string++;
0ba3a731
L
10687
10688 /* Strip whitespace since the addition of pseudo prefixes
10689 changed how the scrubber treats '{'. */
10690 if (is_space_char (*op_string))
10691 ++op_string;
10692
43234a1e
L
10693 continue;
10694 }
10695 unknown_vec_op:
10696 /* We don't know this one. */
10697 as_bad (_("unknown vector operation: `%s'"), saved);
10698 return NULL;
10699 }
10700
6225c532 10701 if (i.mask.reg && i.mask.zeroing && !i.mask.reg->reg_num)
6d2cd6b2
JB
10702 {
10703 as_bad (_("zeroing-masking only allowed with write mask"));
10704 return NULL;
10705 }
10706
43234a1e
L
10707 return op_string;
10708}
10709
252b5132 10710static int
70e41ade 10711i386_immediate (char *imm_start)
252b5132
RH
10712{
10713 char *save_input_line_pointer;
f3c180ae 10714 char *gotfree_input_line;
252b5132 10715 segT exp_seg = 0;
47926f60 10716 expressionS *exp;
40fb9820
L
10717 i386_operand_type types;
10718
0dfbf9d7 10719 operand_type_set (&types, ~0);
252b5132
RH
10720
10721 if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
10722 {
31b2323c
L
10723 as_bad (_("at most %d immediate operands are allowed"),
10724 MAX_IMMEDIATE_OPERANDS);
252b5132
RH
10725 return 0;
10726 }
10727
10728 exp = &im_expressions[i.imm_operands++];
520dc8e8 10729 i.op[this_operand].imms = exp;
252b5132
RH
10730
10731 if (is_space_char (*imm_start))
10732 ++imm_start;
10733
10734 save_input_line_pointer = input_line_pointer;
10735 input_line_pointer = imm_start;
10736
d258b828 10737 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
10738 if (gotfree_input_line)
10739 input_line_pointer = gotfree_input_line;
252b5132
RH
10740
10741 exp_seg = expression (exp);
10742
83183c0c 10743 SKIP_WHITESPACE ();
252b5132 10744 if (*input_line_pointer)
f3c180ae 10745 as_bad (_("junk `%s' after expression"), input_line_pointer);
252b5132
RH
10746
10747 input_line_pointer = save_input_line_pointer;
f3c180ae 10748 if (gotfree_input_line)
ee86248c
JB
10749 {
10750 free (gotfree_input_line);
10751
9aac24b1 10752 if (exp->X_op == O_constant)
ee86248c
JB
10753 exp->X_op = O_illegal;
10754 }
10755
9aac24b1
JB
10756 if (exp_seg == reg_section)
10757 {
10758 as_bad (_("illegal immediate register operand %s"), imm_start);
10759 return 0;
10760 }
10761
ee86248c
JB
10762 return i386_finalize_immediate (exp_seg, exp, types, imm_start);
10763}
252b5132 10764
ee86248c
JB
10765static int
10766i386_finalize_immediate (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
10767 i386_operand_type types, const char *imm_start)
10768{
10769 if (exp->X_op == O_absent || exp->X_op == O_illegal || exp->X_op == O_big)
252b5132 10770 {
313c53d1
L
10771 if (imm_start)
10772 as_bad (_("missing or invalid immediate expression `%s'"),
10773 imm_start);
3992d3b7 10774 return 0;
252b5132 10775 }
3e73aa7c 10776 else if (exp->X_op == O_constant)
252b5132 10777 {
47926f60 10778 /* Size it properly later. */
40fb9820 10779 i.types[this_operand].bitfield.imm64 = 1;
a442cac5
JB
10780
10781 /* If not 64bit, sign/zero extend val, to account for wraparound
10782 when !BFD64. */
10783 if (flag_code != CODE_64BIT)
10784 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
252b5132 10785 }
4c63da97 10786#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
f86103b7 10787 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
31312f95 10788 && exp_seg != absolute_section
47926f60 10789 && exp_seg != text_section
24eab124
AM
10790 && exp_seg != data_section
10791 && exp_seg != bss_section
10792 && exp_seg != undefined_section
f86103b7 10793 && !bfd_is_com_section (exp_seg))
252b5132 10794 {
d0b47220 10795 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
252b5132
RH
10796 return 0;
10797 }
10798#endif
10799 else
10800 {
10801 /* This is an address. The size of the address will be
24eab124 10802 determined later, depending on destination register,
3e73aa7c 10803 suffix, or the default for the section. */
40fb9820
L
10804 i.types[this_operand].bitfield.imm8 = 1;
10805 i.types[this_operand].bitfield.imm16 = 1;
10806 i.types[this_operand].bitfield.imm32 = 1;
10807 i.types[this_operand].bitfield.imm32s = 1;
10808 i.types[this_operand].bitfield.imm64 = 1;
c6fb90c8
L
10809 i.types[this_operand] = operand_type_and (i.types[this_operand],
10810 types);
252b5132
RH
10811 }
10812
10813 return 1;
10814}
10815
551c1ca1 10816static char *
e3bb37b5 10817i386_scale (char *scale)
252b5132 10818{
551c1ca1
AM
10819 offsetT val;
10820 char *save = input_line_pointer;
252b5132 10821
551c1ca1
AM
10822 input_line_pointer = scale;
10823 val = get_absolute_expression ();
10824
10825 switch (val)
252b5132 10826 {
551c1ca1 10827 case 1:
252b5132
RH
10828 i.log2_scale_factor = 0;
10829 break;
551c1ca1 10830 case 2:
252b5132
RH
10831 i.log2_scale_factor = 1;
10832 break;
551c1ca1 10833 case 4:
252b5132
RH
10834 i.log2_scale_factor = 2;
10835 break;
551c1ca1 10836 case 8:
252b5132
RH
10837 i.log2_scale_factor = 3;
10838 break;
10839 default:
a724f0f4
JB
10840 {
10841 char sep = *input_line_pointer;
10842
10843 *input_line_pointer = '\0';
10844 as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
10845 scale);
10846 *input_line_pointer = sep;
10847 input_line_pointer = save;
10848 return NULL;
10849 }
252b5132 10850 }
29b0f896 10851 if (i.log2_scale_factor != 0 && i.index_reg == 0)
252b5132
RH
10852 {
10853 as_warn (_("scale factor of %d without an index register"),
24eab124 10854 1 << i.log2_scale_factor);
252b5132 10855 i.log2_scale_factor = 0;
252b5132 10856 }
551c1ca1
AM
10857 scale = input_line_pointer;
10858 input_line_pointer = save;
10859 return scale;
252b5132
RH
10860}
10861
252b5132 10862static int
e3bb37b5 10863i386_displacement (char *disp_start, char *disp_end)
252b5132 10864{
29b0f896 10865 expressionS *exp;
252b5132
RH
10866 segT exp_seg = 0;
10867 char *save_input_line_pointer;
f3c180ae 10868 char *gotfree_input_line;
40fb9820
L
10869 int override;
10870 i386_operand_type bigdisp, types = anydisp;
3992d3b7 10871 int ret;
252b5132 10872
31b2323c
L
10873 if (i.disp_operands == MAX_MEMORY_OPERANDS)
10874 {
10875 as_bad (_("at most %d displacement operands are allowed"),
10876 MAX_MEMORY_OPERANDS);
10877 return 0;
10878 }
10879
0dfbf9d7 10880 operand_type_set (&bigdisp, 0);
6f2f06be 10881 if (i.jumpabsolute
48bcea9f 10882 || i.types[this_operand].bitfield.baseindex
0cfa3eb3
JB
10883 || (current_templates->start->opcode_modifier.jump != JUMP
10884 && current_templates->start->opcode_modifier.jump != JUMP_DWORD))
e05278af 10885 {
48bcea9f 10886 i386_addressing_mode ();
e05278af 10887 override = (i.prefix[ADDR_PREFIX] != 0);
40fb9820
L
10888 if (flag_code == CODE_64BIT)
10889 {
a775efc8 10890 bigdisp.bitfield.disp32 = 1;
40fb9820 10891 if (!override)
a775efc8 10892 bigdisp.bitfield.disp64 = 1;
40fb9820
L
10893 }
10894 else if ((flag_code == CODE_16BIT) ^ override)
40fb9820 10895 bigdisp.bitfield.disp16 = 1;
48bcea9f
JB
10896 else
10897 bigdisp.bitfield.disp32 = 1;
e05278af
JB
10898 }
10899 else
10900 {
376cd056
JB
10901 /* For PC-relative branches, the width of the displacement may be
10902 dependent upon data size, but is never dependent upon address size.
10903 Also make sure to not unintentionally match against a non-PC-relative
10904 branch template. */
10905 static templates aux_templates;
10906 const insn_template *t = current_templates->start;
5b7c81bd 10907 bool has_intel64 = false;
376cd056
JB
10908
10909 aux_templates.start = t;
10910 while (++t < current_templates->end)
10911 {
10912 if (t->opcode_modifier.jump
10913 != current_templates->start->opcode_modifier.jump)
10914 break;
4b5aaf5f 10915 if ((t->opcode_modifier.isa64 >= INTEL64))
5b7c81bd 10916 has_intel64 = true;
376cd056
JB
10917 }
10918 if (t < current_templates->end)
10919 {
10920 aux_templates.end = t;
10921 current_templates = &aux_templates;
10922 }
10923
e05278af 10924 override = (i.prefix[DATA_PREFIX] != 0);
40fb9820
L
10925 if (flag_code == CODE_64BIT)
10926 {
376cd056
JB
10927 if ((override || i.suffix == WORD_MNEM_SUFFIX)
10928 && (!intel64 || !has_intel64))
40fb9820
L
10929 bigdisp.bitfield.disp16 = 1;
10930 else
a775efc8 10931 bigdisp.bitfield.disp32 = 1;
40fb9820
L
10932 }
10933 else
e05278af
JB
10934 {
10935 if (!override)
10936 override = (i.suffix == (flag_code != CODE_16BIT
10937 ? WORD_MNEM_SUFFIX
10938 : LONG_MNEM_SUFFIX));
40fb9820
L
10939 bigdisp.bitfield.disp32 = 1;
10940 if ((flag_code == CODE_16BIT) ^ override)
10941 {
10942 bigdisp.bitfield.disp32 = 0;
10943 bigdisp.bitfield.disp16 = 1;
10944 }
e05278af 10945 }
e05278af 10946 }
c6fb90c8
L
10947 i.types[this_operand] = operand_type_or (i.types[this_operand],
10948 bigdisp);
252b5132
RH
10949
10950 exp = &disp_expressions[i.disp_operands];
520dc8e8 10951 i.op[this_operand].disps = exp;
252b5132
RH
10952 i.disp_operands++;
10953 save_input_line_pointer = input_line_pointer;
10954 input_line_pointer = disp_start;
10955 END_STRING_AND_SAVE (disp_end);
10956
10957#ifndef GCC_ASM_O_HACK
10958#define GCC_ASM_O_HACK 0
10959#endif
10960#if GCC_ASM_O_HACK
10961 END_STRING_AND_SAVE (disp_end + 1);
40fb9820 10962 if (i.types[this_operand].bitfield.baseIndex
24eab124 10963 && displacement_string_end[-1] == '+')
252b5132
RH
10964 {
10965 /* This hack is to avoid a warning when using the "o"
24eab124
AM
10966 constraint within gcc asm statements.
10967 For instance:
10968
10969 #define _set_tssldt_desc(n,addr,limit,type) \
10970 __asm__ __volatile__ ( \
10971 "movw %w2,%0\n\t" \
10972 "movw %w1,2+%0\n\t" \
10973 "rorl $16,%1\n\t" \
10974 "movb %b1,4+%0\n\t" \
10975 "movb %4,5+%0\n\t" \
10976 "movb $0,6+%0\n\t" \
10977 "movb %h1,7+%0\n\t" \
10978 "rorl $16,%1" \
10979 : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
10980
10981 This works great except that the output assembler ends
10982 up looking a bit weird if it turns out that there is
10983 no offset. You end up producing code that looks like:
10984
10985 #APP
10986 movw $235,(%eax)
10987 movw %dx,2+(%eax)
10988 rorl $16,%edx
10989 movb %dl,4+(%eax)
10990 movb $137,5+(%eax)
10991 movb $0,6+(%eax)
10992 movb %dh,7+(%eax)
10993 rorl $16,%edx
10994 #NO_APP
10995
47926f60 10996 So here we provide the missing zero. */
24eab124
AM
10997
10998 *displacement_string_end = '0';
252b5132
RH
10999 }
11000#endif
d258b828 11001 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
11002 if (gotfree_input_line)
11003 input_line_pointer = gotfree_input_line;
252b5132 11004
24eab124 11005 exp_seg = expression (exp);
252b5132 11006
636c26b0
AM
11007 SKIP_WHITESPACE ();
11008 if (*input_line_pointer)
11009 as_bad (_("junk `%s' after expression"), input_line_pointer);
11010#if GCC_ASM_O_HACK
11011 RESTORE_END_STRING (disp_end + 1);
11012#endif
636c26b0 11013 input_line_pointer = save_input_line_pointer;
636c26b0 11014 if (gotfree_input_line)
ee86248c
JB
11015 {
11016 free (gotfree_input_line);
11017
11018 if (exp->X_op == O_constant || exp->X_op == O_register)
11019 exp->X_op = O_illegal;
11020 }
11021
11022 ret = i386_finalize_displacement (exp_seg, exp, types, disp_start);
11023
11024 RESTORE_END_STRING (disp_end);
11025
11026 return ret;
11027}
11028
11029static int
11030i386_finalize_displacement (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
11031 i386_operand_type types, const char *disp_start)
11032{
ee86248c 11033 int ret = 1;
636c26b0 11034
24eab124
AM
11035 /* We do this to make sure that the section symbol is in
11036 the symbol table. We will ultimately change the relocation
47926f60 11037 to be relative to the beginning of the section. */
1ae12ab7 11038 if (i.reloc[this_operand] == BFD_RELOC_386_GOTOFF
d6ab8113
JB
11039 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
11040 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
24eab124 11041 {
636c26b0 11042 if (exp->X_op != O_symbol)
3992d3b7 11043 goto inv_disp;
636c26b0 11044
e5cb08ac 11045 if (S_IS_LOCAL (exp->X_add_symbol)
c64efb4b
L
11046 && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section
11047 && S_GET_SEGMENT (exp->X_add_symbol) != expr_section)
24eab124 11048 section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
24eab124
AM
11049 exp->X_op = O_subtract;
11050 exp->X_op_symbol = GOT_symbol;
1ae12ab7 11051 if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
29b0f896 11052 i.reloc[this_operand] = BFD_RELOC_32_PCREL;
d6ab8113
JB
11053 else if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
11054 i.reloc[this_operand] = BFD_RELOC_64;
23df1078 11055 else
29b0f896 11056 i.reloc[this_operand] = BFD_RELOC_32;
24eab124 11057 }
252b5132 11058
3992d3b7
AM
11059 else if (exp->X_op == O_absent
11060 || exp->X_op == O_illegal
ee86248c 11061 || exp->X_op == O_big)
2daf4fd8 11062 {
3992d3b7
AM
11063 inv_disp:
11064 as_bad (_("missing or invalid displacement expression `%s'"),
2daf4fd8 11065 disp_start);
3992d3b7 11066 ret = 0;
2daf4fd8
AM
11067 }
11068
a50187b2
JB
11069 else if (exp->X_op == O_constant)
11070 {
11071 /* Sizing gets taken care of by optimize_disp().
11072
11073 If not 64bit, sign/zero extend val, to account for wraparound
11074 when !BFD64. */
11075 if (flag_code != CODE_64BIT)
11076 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
11077 }
11078
4c63da97 11079#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
a50187b2 11080 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
3992d3b7
AM
11081 && exp_seg != absolute_section
11082 && exp_seg != text_section
11083 && exp_seg != data_section
11084 && exp_seg != bss_section
11085 && exp_seg != undefined_section
11086 && !bfd_is_com_section (exp_seg))
24eab124 11087 {
d0b47220 11088 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
3992d3b7 11089 ret = 0;
24eab124 11090 }
252b5132 11091#endif
3956db08 11092
a50187b2 11093 else if (current_templates->start->opcode_modifier.jump == JUMP_BYTE)
48bcea9f
JB
11094 i.types[this_operand].bitfield.disp8 = 1;
11095
40fb9820 11096 /* Check if this is a displacement only operand. */
02b83698
JB
11097 if (!i.types[this_operand].bitfield.baseindex)
11098 i.types[this_operand] =
11099 operand_type_or (operand_type_and_not (i.types[this_operand], anydisp),
11100 operand_type_and (i.types[this_operand], types));
3956db08 11101
3992d3b7 11102 return ret;
252b5132
RH
11103}
11104
2abc2bec
JB
11105/* Return the active addressing mode, taking address override and
11106 registers forming the address into consideration. Update the
11107 address override prefix if necessary. */
47926f60 11108
2abc2bec
JB
11109static enum flag_code
11110i386_addressing_mode (void)
252b5132 11111{
be05d201
L
11112 enum flag_code addr_mode;
11113
11114 if (i.prefix[ADDR_PREFIX])
11115 addr_mode = flag_code == CODE_32BIT ? CODE_16BIT : CODE_32BIT;
a23b33b3
JB
11116 else if (flag_code == CODE_16BIT
11117 && current_templates->start->cpu_flags.bitfield.cpumpx
11118 /* Avoid replacing the "16-bit addressing not allowed" diagnostic
11119 from md_assemble() by "is not a valid base/index expression"
11120 when there is a base and/or index. */
11121 && !i.types[this_operand].bitfield.baseindex)
11122 {
11123 /* MPX insn memory operands with neither base nor index must be forced
11124 to use 32-bit addressing in 16-bit mode. */
11125 addr_mode = CODE_32BIT;
11126 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
11127 ++i.prefixes;
11128 gas_assert (!i.types[this_operand].bitfield.disp16);
11129 gas_assert (!i.types[this_operand].bitfield.disp32);
11130 }
be05d201
L
11131 else
11132 {
11133 addr_mode = flag_code;
11134
24eab124 11135#if INFER_ADDR_PREFIX
be05d201
L
11136 if (i.mem_operands == 0)
11137 {
11138 /* Infer address prefix from the first memory operand. */
11139 const reg_entry *addr_reg = i.base_reg;
11140
11141 if (addr_reg == NULL)
11142 addr_reg = i.index_reg;
eecb386c 11143
be05d201
L
11144 if (addr_reg)
11145 {
e968fc9b 11146 if (addr_reg->reg_type.bitfield.dword)
be05d201
L
11147 addr_mode = CODE_32BIT;
11148 else if (flag_code != CODE_64BIT
dc821c5f 11149 && addr_reg->reg_type.bitfield.word)
be05d201
L
11150 addr_mode = CODE_16BIT;
11151
11152 if (addr_mode != flag_code)
11153 {
11154 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
11155 i.prefixes += 1;
11156 /* Change the size of any displacement too. At most one
11157 of Disp16 or Disp32 is set.
11158 FIXME. There doesn't seem to be any real need for
11159 separate Disp16 and Disp32 flags. The same goes for
11160 Imm16 and Imm32. Removing them would probably clean
11161 up the code quite a lot. */
11162 if (flag_code != CODE_64BIT
11163 && (i.types[this_operand].bitfield.disp16
11164 || i.types[this_operand].bitfield.disp32))
11165 i.types[this_operand]
11166 = operand_type_xor (i.types[this_operand], disp16_32);
11167 }
11168 }
11169 }
24eab124 11170#endif
be05d201
L
11171 }
11172
2abc2bec
JB
11173 return addr_mode;
11174}
11175
11176/* Make sure the memory operand we've been dealt is valid.
11177 Return 1 on success, 0 on a failure. */
11178
11179static int
11180i386_index_check (const char *operand_string)
11181{
11182 const char *kind = "base/index";
11183 enum flag_code addr_mode = i386_addressing_mode ();
a152332d 11184 const insn_template *t = current_templates->start;
2abc2bec 11185
a152332d
JB
11186 if (t->opcode_modifier.isstring
11187 && !t->cpu_flags.bitfield.cpupadlock
fc0763e6
JB
11188 && (current_templates->end[-1].opcode_modifier.isstring
11189 || i.mem_operands))
11190 {
11191 /* Memory operands of string insns are special in that they only allow
11192 a single register (rDI, rSI, or rBX) as their memory address. */
be05d201
L
11193 const reg_entry *expected_reg;
11194 static const char *di_si[][2] =
11195 {
11196 { "esi", "edi" },
11197 { "si", "di" },
11198 { "rsi", "rdi" }
11199 };
11200 static const char *bx[] = { "ebx", "bx", "rbx" };
fc0763e6
JB
11201
11202 kind = "string address";
11203
a152332d 11204 if (t->opcode_modifier.prefixok == PrefixRep)
fc0763e6 11205 {
51c8edf6
JB
11206 int es_op = current_templates->end[-1].opcode_modifier.isstring
11207 - IS_STRING_ES_OP0;
11208 int op = 0;
fc0763e6 11209
51c8edf6 11210 if (!current_templates->end[-1].operand_types[0].bitfield.baseindex
fc0763e6
JB
11211 || ((!i.mem_operands != !intel_syntax)
11212 && current_templates->end[-1].operand_types[1]
11213 .bitfield.baseindex))
51c8edf6 11214 op = 1;
fe0e921f
AM
11215 expected_reg
11216 = (const reg_entry *) str_hash_find (reg_hash,
11217 di_si[addr_mode][op == es_op]);
fc0763e6
JB
11218 }
11219 else
fe0e921f
AM
11220 expected_reg
11221 = (const reg_entry *)str_hash_find (reg_hash, bx[addr_mode]);
fc0763e6 11222
be05d201
L
11223 if (i.base_reg != expected_reg
11224 || i.index_reg
fc0763e6 11225 || operand_type_check (i.types[this_operand], disp))
fc0763e6 11226 {
be05d201
L
11227 /* The second memory operand must have the same size as
11228 the first one. */
11229 if (i.mem_operands
11230 && i.base_reg
11231 && !((addr_mode == CODE_64BIT
dc821c5f 11232 && i.base_reg->reg_type.bitfield.qword)
be05d201 11233 || (addr_mode == CODE_32BIT
dc821c5f
JB
11234 ? i.base_reg->reg_type.bitfield.dword
11235 : i.base_reg->reg_type.bitfield.word)))
be05d201
L
11236 goto bad_address;
11237
fc0763e6
JB
11238 as_warn (_("`%s' is not valid here (expected `%c%s%s%c')"),
11239 operand_string,
11240 intel_syntax ? '[' : '(',
11241 register_prefix,
be05d201 11242 expected_reg->reg_name,
fc0763e6 11243 intel_syntax ? ']' : ')');
be05d201 11244 return 1;
fc0763e6 11245 }
be05d201
L
11246 else
11247 return 1;
11248
dc1e8a47 11249 bad_address:
be05d201
L
11250 as_bad (_("`%s' is not a valid %s expression"),
11251 operand_string, kind);
11252 return 0;
3e73aa7c
JH
11253 }
11254 else
11255 {
be05d201
L
11256 if (addr_mode != CODE_16BIT)
11257 {
11258 /* 32-bit/64-bit checks. */
41eb8e88
L
11259 if (i.disp_encoding == disp_encoding_16bit)
11260 {
11261 bad_disp:
11262 as_bad (_("invalid `%s' prefix"),
11263 addr_mode == CODE_16BIT ? "{disp32}" : "{disp16}");
11264 return 0;
11265 }
11266
be05d201 11267 if ((i.base_reg
e968fc9b
JB
11268 && ((addr_mode == CODE_64BIT
11269 ? !i.base_reg->reg_type.bitfield.qword
11270 : !i.base_reg->reg_type.bitfield.dword)
11271 || (i.index_reg && i.base_reg->reg_num == RegIP)
11272 || i.base_reg->reg_num == RegIZ))
be05d201 11273 || (i.index_reg
1b54b8d7
JB
11274 && !i.index_reg->reg_type.bitfield.xmmword
11275 && !i.index_reg->reg_type.bitfield.ymmword
11276 && !i.index_reg->reg_type.bitfield.zmmword
be05d201 11277 && ((addr_mode == CODE_64BIT
e968fc9b
JB
11278 ? !i.index_reg->reg_type.bitfield.qword
11279 : !i.index_reg->reg_type.bitfield.dword)
be05d201
L
11280 || !i.index_reg->reg_type.bitfield.baseindex)))
11281 goto bad_address;
8178be5b 11282
260cd341 11283 /* bndmk, bndldx, bndstx and mandatory non-vector SIB have special restrictions. */
a152332d 11284 if ((t->opcode_modifier.opcodeprefix == PREFIX_0XF3
389d00a5
JB
11285 && t->opcode_modifier.opcodespace == SPACE_0F
11286 && t->base_opcode == 0x1b)
a152332d 11287 || (t->opcode_modifier.opcodeprefix == PREFIX_NONE
389d00a5
JB
11288 && t->opcode_modifier.opcodespace == SPACE_0F
11289 && (t->base_opcode & ~1) == 0x1a)
a152332d 11290 || t->opcode_modifier.sib == SIBMEM)
8178be5b
JB
11291 {
11292 /* They cannot use RIP-relative addressing. */
e968fc9b 11293 if (i.base_reg && i.base_reg->reg_num == RegIP)
8178be5b
JB
11294 {
11295 as_bad (_("`%s' cannot be used here"), operand_string);
11296 return 0;
11297 }
11298
11299 /* bndldx and bndstx ignore their scale factor. */
a152332d 11300 if (t->opcode_modifier.opcodeprefix == PREFIX_NONE
389d00a5
JB
11301 && t->opcode_modifier.opcodespace == SPACE_0F
11302 && (t->base_opcode & ~1) == 0x1a
8178be5b
JB
11303 && i.log2_scale_factor)
11304 as_warn (_("register scaling is being ignored here"));
11305 }
be05d201
L
11306 }
11307 else
3e73aa7c 11308 {
be05d201 11309 /* 16-bit checks. */
41eb8e88
L
11310 if (i.disp_encoding == disp_encoding_32bit)
11311 goto bad_disp;
11312
3e73aa7c 11313 if ((i.base_reg
dc821c5f 11314 && (!i.base_reg->reg_type.bitfield.word
40fb9820 11315 || !i.base_reg->reg_type.bitfield.baseindex))
3e73aa7c 11316 || (i.index_reg
dc821c5f 11317 && (!i.index_reg->reg_type.bitfield.word
40fb9820 11318 || !i.index_reg->reg_type.bitfield.baseindex
29b0f896
AM
11319 || !(i.base_reg
11320 && i.base_reg->reg_num < 6
11321 && i.index_reg->reg_num >= 6
11322 && i.log2_scale_factor == 0))))
be05d201 11323 goto bad_address;
3e73aa7c
JH
11324 }
11325 }
be05d201 11326 return 1;
24eab124 11327}
252b5132 11328
43234a1e
L
11329/* Handle vector immediates. */
11330
11331static int
11332RC_SAE_immediate (const char *imm_start)
11333{
43234a1e 11334 const char *pstr = imm_start;
43234a1e
L
11335
11336 if (*pstr != '{')
11337 return 0;
11338
7063667e
JB
11339 pstr = RC_SAE_specifier (pstr + 1);
11340 if (pstr == NULL)
43234a1e
L
11341 return 0;
11342
11343 if (*pstr++ != '}')
11344 {
11345 as_bad (_("Missing '}': '%s'"), imm_start);
11346 return 0;
11347 }
11348 /* RC/SAE immediate string should contain nothing more. */;
11349 if (*pstr != 0)
11350 {
11351 as_bad (_("Junk after '}': '%s'"), imm_start);
11352 return 0;
11353 }
11354
cf665fee
JB
11355 /* Internally this doesn't count as an operand. */
11356 --i.operands;
43234a1e 11357
43234a1e
L
11358 return 1;
11359}
11360
8325cc63
JB
11361/* Only string instructions can have a second memory operand, so
11362 reduce current_templates to just those if it contains any. */
11363static int
11364maybe_adjust_templates (void)
11365{
11366 const insn_template *t;
11367
11368 gas_assert (i.mem_operands == 1);
11369
11370 for (t = current_templates->start; t < current_templates->end; ++t)
11371 if (t->opcode_modifier.isstring)
11372 break;
11373
11374 if (t < current_templates->end)
11375 {
11376 static templates aux_templates;
5b7c81bd 11377 bool recheck;
8325cc63
JB
11378
11379 aux_templates.start = t;
11380 for (; t < current_templates->end; ++t)
11381 if (!t->opcode_modifier.isstring)
11382 break;
11383 aux_templates.end = t;
11384
11385 /* Determine whether to re-check the first memory operand. */
11386 recheck = (aux_templates.start != current_templates->start
11387 || t != current_templates->end);
11388
11389 current_templates = &aux_templates;
11390
11391 if (recheck)
11392 {
11393 i.mem_operands = 0;
11394 if (i.memop1_string != NULL
11395 && i386_index_check (i.memop1_string) == 0)
11396 return 0;
11397 i.mem_operands = 1;
11398 }
11399 }
11400
11401 return 1;
11402}
11403
9d299bea
JB
11404static INLINE bool starts_memory_operand (char c)
11405{
014fbcda 11406 return ISDIGIT (c)
9d299bea 11407 || is_identifier_char (c)
014fbcda 11408 || strchr ("([\"+-!~", c);
9d299bea
JB
11409}
11410
fc0763e6 11411/* Parse OPERAND_STRING into the i386_insn structure I. Returns zero
47926f60 11412 on error. */
252b5132 11413
252b5132 11414static int
a7619375 11415i386_att_operand (char *operand_string)
252b5132 11416{
af6bdddf
AM
11417 const reg_entry *r;
11418 char *end_op;
24eab124 11419 char *op_string = operand_string;
252b5132 11420
24eab124 11421 if (is_space_char (*op_string))
252b5132
RH
11422 ++op_string;
11423
24eab124 11424 /* We check for an absolute prefix (differentiating,
47926f60 11425 for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
24eab124
AM
11426 if (*op_string == ABSOLUTE_PREFIX)
11427 {
11428 ++op_string;
11429 if (is_space_char (*op_string))
11430 ++op_string;
5b7c81bd 11431 i.jumpabsolute = true;
24eab124 11432 }
252b5132 11433
47926f60 11434 /* Check if operand is a register. */
4d1bb795 11435 if ((r = parse_register (op_string, &end_op)) != NULL)
24eab124 11436 {
40fb9820
L
11437 i386_operand_type temp;
11438
8a6fb3f9
JB
11439 if (r == &bad_reg)
11440 return 0;
11441
24eab124
AM
11442 /* Check for a segment override by searching for ':' after a
11443 segment register. */
11444 op_string = end_op;
11445 if (is_space_char (*op_string))
11446 ++op_string;
00cee14f 11447 if (*op_string == ':' && r->reg_type.bitfield.class == SReg)
24eab124 11448 {
5e042380 11449 i.seg[i.mem_operands] = r;
252b5132 11450
24eab124 11451 /* Skip the ':' and whitespace. */
252b5132
RH
11452 ++op_string;
11453 if (is_space_char (*op_string))
24eab124 11454 ++op_string;
252b5132 11455
47926f60 11456 /* Handle case of %es:*foo. */
c8d541e2 11457 if (!i.jumpabsolute && *op_string == ABSOLUTE_PREFIX)
24eab124
AM
11458 {
11459 ++op_string;
11460 if (is_space_char (*op_string))
11461 ++op_string;
5b7c81bd 11462 i.jumpabsolute = true;
24eab124 11463 }
c8d541e2 11464
9d299bea 11465 if (!starts_memory_operand (*op_string))
c8d541e2
JB
11466 {
11467 as_bad (_("bad memory operand `%s'"), op_string);
11468 return 0;
11469 }
24eab124
AM
11470 goto do_memory_reference;
11471 }
43234a1e
L
11472
11473 /* Handle vector operations. */
11474 if (*op_string == '{')
11475 {
f70c6814 11476 op_string = check_VecOperations (op_string);
43234a1e
L
11477 if (op_string == NULL)
11478 return 0;
11479 }
11480
24eab124
AM
11481 if (*op_string)
11482 {
d0b47220 11483 as_bad (_("junk `%s' after register"), op_string);
24eab124
AM
11484 return 0;
11485 }
40fb9820
L
11486 temp = r->reg_type;
11487 temp.bitfield.baseindex = 0;
c6fb90c8
L
11488 i.types[this_operand] = operand_type_or (i.types[this_operand],
11489 temp);
7d5e4556 11490 i.types[this_operand].bitfield.unspecified = 0;
520dc8e8 11491 i.op[this_operand].regs = r;
24eab124 11492 i.reg_operands++;
cf665fee
JB
11493
11494 /* A GPR may follow an RC or SAE immediate only if a (vector) register
11495 operand was also present earlier on. */
11496 if (i.rounding.type != rc_none && temp.bitfield.class == Reg
11497 && i.reg_operands == 1)
11498 {
11499 unsigned int j;
11500
11501 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); ++j)
11502 if (i.rounding.type == RC_NamesTable[j].type)
11503 break;
11504 as_bad (_("`%s': misplaced `{%s}'"),
11505 current_templates->start->name, RC_NamesTable[j].name);
11506 return 0;
11507 }
24eab124 11508 }
af6bdddf
AM
11509 else if (*op_string == REGISTER_PREFIX)
11510 {
11511 as_bad (_("bad register name `%s'"), op_string);
11512 return 0;
11513 }
24eab124 11514 else if (*op_string == IMMEDIATE_PREFIX)
ce8a8b2f 11515 {
24eab124 11516 ++op_string;
6f2f06be 11517 if (i.jumpabsolute)
24eab124 11518 {
d0b47220 11519 as_bad (_("immediate operand illegal with absolute jump"));
24eab124
AM
11520 return 0;
11521 }
11522 if (!i386_immediate (op_string))
11523 return 0;
cf665fee
JB
11524 if (i.rounding.type != rc_none)
11525 {
11526 as_bad (_("`%s': RC/SAE operand must follow immediate operands"),
11527 current_templates->start->name);
11528 return 0;
11529 }
24eab124 11530 }
43234a1e
L
11531 else if (RC_SAE_immediate (operand_string))
11532 {
cf665fee
JB
11533 /* If it is a RC or SAE immediate, do the necessary placement check:
11534 Only another immediate or a GPR may precede it. */
11535 if (i.mem_operands || i.reg_operands + i.imm_operands > 1
11536 || (i.reg_operands == 1
11537 && i.op[0].regs->reg_type.bitfield.class != Reg))
11538 {
11539 as_bad (_("`%s': misplaced `%s'"),
11540 current_templates->start->name, operand_string);
11541 return 0;
11542 }
43234a1e 11543 }
9d299bea 11544 else if (starts_memory_operand (*op_string))
24eab124 11545 {
47926f60 11546 /* This is a memory reference of some sort. */
af6bdddf 11547 char *base_string;
252b5132 11548
47926f60 11549 /* Start and end of displacement string expression (if found). */
eecb386c
AM
11550 char *displacement_string_start;
11551 char *displacement_string_end;
252b5132 11552
24eab124 11553 do_memory_reference:
8325cc63
JB
11554 if (i.mem_operands == 1 && !maybe_adjust_templates ())
11555 return 0;
24eab124 11556 if ((i.mem_operands == 1
40fb9820 11557 && !current_templates->start->opcode_modifier.isstring)
24eab124
AM
11558 || i.mem_operands == 2)
11559 {
11560 as_bad (_("too many memory references for `%s'"),
11561 current_templates->start->name);
11562 return 0;
11563 }
252b5132 11564
24eab124
AM
11565 /* Check for base index form. We detect the base index form by
11566 looking for an ')' at the end of the operand, searching
11567 for the '(' matching it, and finding a REGISTER_PREFIX or ','
11568 after the '('. */
af6bdddf 11569 base_string = op_string + strlen (op_string);
c3332e24 11570
43234a1e 11571 /* Handle vector operations. */
6b5ba0d4
JB
11572 --base_string;
11573 if (is_space_char (*base_string))
11574 --base_string;
11575
11576 if (*base_string == '}')
43234a1e 11577 {
6b5ba0d4
JB
11578 char *vop_start = NULL;
11579
11580 while (base_string-- > op_string)
11581 {
11582 if (*base_string == '"')
11583 break;
11584 if (*base_string != '{')
11585 continue;
11586
11587 vop_start = base_string;
11588
11589 --base_string;
11590 if (is_space_char (*base_string))
11591 --base_string;
11592
11593 if (*base_string != '}')
11594 break;
11595
11596 vop_start = NULL;
11597 }
11598
11599 if (!vop_start)
11600 {
11601 as_bad (_("unbalanced figure braces"));
11602 return 0;
11603 }
11604
f70c6814 11605 if (check_VecOperations (vop_start) == NULL)
43234a1e 11606 return 0;
43234a1e
L
11607 }
11608
47926f60 11609 /* If we only have a displacement, set-up for it to be parsed later. */
af6bdddf
AM
11610 displacement_string_start = op_string;
11611 displacement_string_end = base_string + 1;
252b5132 11612
24eab124
AM
11613 if (*base_string == ')')
11614 {
af6bdddf 11615 char *temp_string;
cc0f9635 11616 unsigned int parens_not_balanced = 1;
e68c3d59 11617
24eab124 11618 /* We've already checked that the number of left & right ()'s are
47926f60 11619 equal, so this loop will not be infinite. */
24eab124
AM
11620 do
11621 {
11622 base_string--;
cc0f9635
JB
11623 if (*base_string == ')')
11624 parens_not_balanced++;
11625 if (*base_string == '(')
11626 parens_not_balanced--;
24eab124 11627 }
cc0f9635 11628 while (parens_not_balanced && *base_string != '"');
c3332e24 11629
af6bdddf 11630 temp_string = base_string;
c3332e24 11631
24eab124 11632 /* Skip past '(' and whitespace. */
e68c3d59
JB
11633 if (*base_string == '(')
11634 ++base_string;
252b5132 11635 if (is_space_char (*base_string))
24eab124 11636 ++base_string;
252b5132 11637
af6bdddf 11638 if (*base_string == ','
4eed87de
AM
11639 || ((i.base_reg = parse_register (base_string, &end_op))
11640 != NULL))
252b5132 11641 {
af6bdddf 11642 displacement_string_end = temp_string;
252b5132 11643
40fb9820 11644 i.types[this_operand].bitfield.baseindex = 1;
252b5132 11645
af6bdddf 11646 if (i.base_reg)
24eab124 11647 {
8a6fb3f9
JB
11648 if (i.base_reg == &bad_reg)
11649 return 0;
24eab124
AM
11650 base_string = end_op;
11651 if (is_space_char (*base_string))
11652 ++base_string;
af6bdddf
AM
11653 }
11654
11655 /* There may be an index reg or scale factor here. */
11656 if (*base_string == ',')
11657 {
11658 ++base_string;
11659 if (is_space_char (*base_string))
11660 ++base_string;
11661
4eed87de
AM
11662 if ((i.index_reg = parse_register (base_string, &end_op))
11663 != NULL)
24eab124 11664 {
8a6fb3f9
JB
11665 if (i.index_reg == &bad_reg)
11666 return 0;
af6bdddf 11667 base_string = end_op;
24eab124
AM
11668 if (is_space_char (*base_string))
11669 ++base_string;
af6bdddf
AM
11670 if (*base_string == ',')
11671 {
11672 ++base_string;
11673 if (is_space_char (*base_string))
11674 ++base_string;
11675 }
e5cb08ac 11676 else if (*base_string != ')')
af6bdddf 11677 {
4eed87de
AM
11678 as_bad (_("expecting `,' or `)' "
11679 "after index register in `%s'"),
af6bdddf
AM
11680 operand_string);
11681 return 0;
11682 }
24eab124 11683 }
af6bdddf 11684 else if (*base_string == REGISTER_PREFIX)
24eab124 11685 {
f76bf5e0
L
11686 end_op = strchr (base_string, ',');
11687 if (end_op)
11688 *end_op = '\0';
af6bdddf 11689 as_bad (_("bad register name `%s'"), base_string);
24eab124
AM
11690 return 0;
11691 }
252b5132 11692
47926f60 11693 /* Check for scale factor. */
551c1ca1 11694 if (*base_string != ')')
af6bdddf 11695 {
551c1ca1
AM
11696 char *end_scale = i386_scale (base_string);
11697
11698 if (!end_scale)
af6bdddf 11699 return 0;
24eab124 11700
551c1ca1 11701 base_string = end_scale;
af6bdddf
AM
11702 if (is_space_char (*base_string))
11703 ++base_string;
11704 if (*base_string != ')')
11705 {
4eed87de
AM
11706 as_bad (_("expecting `)' "
11707 "after scale factor in `%s'"),
af6bdddf
AM
11708 operand_string);
11709 return 0;
11710 }
11711 }
11712 else if (!i.index_reg)
24eab124 11713 {
4eed87de
AM
11714 as_bad (_("expecting index register or scale factor "
11715 "after `,'; got '%c'"),
af6bdddf 11716 *base_string);
24eab124
AM
11717 return 0;
11718 }
11719 }
af6bdddf 11720 else if (*base_string != ')')
24eab124 11721 {
4eed87de
AM
11722 as_bad (_("expecting `,' or `)' "
11723 "after base register in `%s'"),
af6bdddf 11724 operand_string);
24eab124
AM
11725 return 0;
11726 }
c3332e24 11727 }
af6bdddf 11728 else if (*base_string == REGISTER_PREFIX)
c3332e24 11729 {
f76bf5e0
L
11730 end_op = strchr (base_string, ',');
11731 if (end_op)
11732 *end_op = '\0';
af6bdddf 11733 as_bad (_("bad register name `%s'"), base_string);
24eab124 11734 return 0;
c3332e24 11735 }
24eab124
AM
11736 }
11737
11738 /* If there's an expression beginning the operand, parse it,
11739 assuming displacement_string_start and
11740 displacement_string_end are meaningful. */
11741 if (displacement_string_start != displacement_string_end)
11742 {
11743 if (!i386_displacement (displacement_string_start,
11744 displacement_string_end))
11745 return 0;
11746 }
11747
11748 /* Special case for (%dx) while doing input/output op. */
11749 if (i.base_reg
75e5731b
JB
11750 && i.base_reg->reg_type.bitfield.instance == RegD
11751 && i.base_reg->reg_type.bitfield.word
24eab124
AM
11752 && i.index_reg == 0
11753 && i.log2_scale_factor == 0
11754 && i.seg[i.mem_operands] == 0
40fb9820 11755 && !operand_type_check (i.types[this_operand], disp))
24eab124 11756 {
2fb5be8d 11757 i.types[this_operand] = i.base_reg->reg_type;
24eab124
AM
11758 return 1;
11759 }
11760
eecb386c
AM
11761 if (i386_index_check (operand_string) == 0)
11762 return 0;
c48dadc9 11763 i.flags[this_operand] |= Operand_Mem;
8325cc63
JB
11764 if (i.mem_operands == 0)
11765 i.memop1_string = xstrdup (operand_string);
24eab124
AM
11766 i.mem_operands++;
11767 }
11768 else
ce8a8b2f
AM
11769 {
11770 /* It's not a memory operand; argh! */
24eab124
AM
11771 as_bad (_("invalid char %s beginning operand %d `%s'"),
11772 output_invalid (*op_string),
11773 this_operand + 1,
11774 op_string);
11775 return 0;
11776 }
47926f60 11777 return 1; /* Normal return. */
252b5132
RH
11778}
11779\f
fa94de6b
RM
11780/* Calculate the maximum variable size (i.e., excluding fr_fix)
11781 that an rs_machine_dependent frag may reach. */
11782
11783unsigned int
11784i386_frag_max_var (fragS *frag)
11785{
11786 /* The only relaxable frags are for jumps.
11787 Unconditional jumps can grow by 4 bytes and others by 5 bytes. */
11788 gas_assert (frag->fr_type == rs_machine_dependent);
11789 return TYPE_FROM_RELAX_STATE (frag->fr_subtype) == UNCOND_JUMP ? 4 : 5;
11790}
11791
b084df0b
L
11792#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
11793static int
8dcea932 11794elf_symbol_resolved_in_segment_p (symbolS *fr_symbol, offsetT fr_var)
b084df0b
L
11795{
11796 /* STT_GNU_IFUNC symbol must go through PLT. */
11797 if ((symbol_get_bfdsym (fr_symbol)->flags
11798 & BSF_GNU_INDIRECT_FUNCTION) != 0)
11799 return 0;
11800
11801 if (!S_IS_EXTERNAL (fr_symbol))
11802 /* Symbol may be weak or local. */
11803 return !S_IS_WEAK (fr_symbol);
11804
8dcea932
L
11805 /* Global symbols with non-default visibility can't be preempted. */
11806 if (ELF_ST_VISIBILITY (S_GET_OTHER (fr_symbol)) != STV_DEFAULT)
11807 return 1;
11808
11809 if (fr_var != NO_RELOC)
11810 switch ((enum bfd_reloc_code_real) fr_var)
11811 {
11812 case BFD_RELOC_386_PLT32:
11813 case BFD_RELOC_X86_64_PLT32:
33eaf5de 11814 /* Symbol with PLT relocation may be preempted. */
8dcea932
L
11815 return 0;
11816 default:
11817 abort ();
11818 }
11819
b084df0b
L
11820 /* Global symbols with default visibility in a shared library may be
11821 preempted by another definition. */
8dcea932 11822 return !shared;
b084df0b
L
11823}
11824#endif
11825
79d72f45
HL
11826/* Table 3-2. Macro-Fusible Instructions in Haswell Microarchitecture
11827 Note also work for Skylake and Cascadelake.
11828---------------------------------------------------------------------
11829| JCC | ADD/SUB/CMP | INC/DEC | TEST/AND |
11830| ------ | ----------- | ------- | -------- |
11831| Jo | N | N | Y |
11832| Jno | N | N | Y |
11833| Jc/Jb | Y | N | Y |
11834| Jae/Jnb | Y | N | Y |
11835| Je/Jz | Y | Y | Y |
11836| Jne/Jnz | Y | Y | Y |
11837| Jna/Jbe | Y | N | Y |
11838| Ja/Jnbe | Y | N | Y |
11839| Js | N | N | Y |
11840| Jns | N | N | Y |
11841| Jp/Jpe | N | N | Y |
11842| Jnp/Jpo | N | N | Y |
11843| Jl/Jnge | Y | Y | Y |
11844| Jge/Jnl | Y | Y | Y |
11845| Jle/Jng | Y | Y | Y |
11846| Jg/Jnle | Y | Y | Y |
11847--------------------------------------------------------------------- */
11848static int
11849i386_macro_fusible_p (enum mf_cmp_kind mf_cmp, enum mf_jcc_kind mf_jcc)
11850{
11851 if (mf_cmp == mf_cmp_alu_cmp)
11852 return ((mf_jcc >= mf_jcc_jc && mf_jcc <= mf_jcc_jna)
11853 || mf_jcc == mf_jcc_jl || mf_jcc == mf_jcc_jle);
11854 if (mf_cmp == mf_cmp_incdec)
11855 return (mf_jcc == mf_jcc_je || mf_jcc == mf_jcc_jl
11856 || mf_jcc == mf_jcc_jle);
11857 if (mf_cmp == mf_cmp_test_and)
11858 return 1;
11859 return 0;
11860}
11861
e379e5f3
L
11862/* Return the next non-empty frag. */
11863
11864static fragS *
11865i386_next_non_empty_frag (fragS *fragP)
11866{
11867 /* There may be a frag with a ".fill 0" when there is no room in
11868 the current frag for frag_grow in output_insn. */
11869 for (fragP = fragP->fr_next;
11870 (fragP != NULL
11871 && fragP->fr_type == rs_fill
11872 && fragP->fr_fix == 0);
11873 fragP = fragP->fr_next)
11874 ;
11875 return fragP;
11876}
11877
11878/* Return the next jcc frag after BRANCH_PADDING. */
11879
11880static fragS *
79d72f45 11881i386_next_fusible_jcc_frag (fragS *maybe_cmp_fragP, fragS *pad_fragP)
e379e5f3 11882{
79d72f45
HL
11883 fragS *branch_fragP;
11884 if (!pad_fragP)
e379e5f3
L
11885 return NULL;
11886
79d72f45
HL
11887 if (pad_fragP->fr_type == rs_machine_dependent
11888 && (TYPE_FROM_RELAX_STATE (pad_fragP->fr_subtype)
e379e5f3
L
11889 == BRANCH_PADDING))
11890 {
79d72f45
HL
11891 branch_fragP = i386_next_non_empty_frag (pad_fragP);
11892 if (branch_fragP->fr_type != rs_machine_dependent)
e379e5f3 11893 return NULL;
79d72f45
HL
11894 if (TYPE_FROM_RELAX_STATE (branch_fragP->fr_subtype) == COND_JUMP
11895 && i386_macro_fusible_p (maybe_cmp_fragP->tc_frag_data.mf_type,
11896 pad_fragP->tc_frag_data.mf_type))
11897 return branch_fragP;
e379e5f3
L
11898 }
11899
11900 return NULL;
11901}
11902
11903/* Classify BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags. */
11904
11905static void
11906i386_classify_machine_dependent_frag (fragS *fragP)
11907{
11908 fragS *cmp_fragP;
11909 fragS *pad_fragP;
11910 fragS *branch_fragP;
11911 fragS *next_fragP;
11912 unsigned int max_prefix_length;
11913
11914 if (fragP->tc_frag_data.classified)
11915 return;
11916
11917 /* First scan for BRANCH_PADDING and FUSED_JCC_PADDING. Convert
11918 FUSED_JCC_PADDING and merge BRANCH_PADDING. */
11919 for (next_fragP = fragP;
11920 next_fragP != NULL;
11921 next_fragP = next_fragP->fr_next)
11922 {
11923 next_fragP->tc_frag_data.classified = 1;
11924 if (next_fragP->fr_type == rs_machine_dependent)
11925 switch (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype))
11926 {
11927 case BRANCH_PADDING:
11928 /* The BRANCH_PADDING frag must be followed by a branch
11929 frag. */
11930 branch_fragP = i386_next_non_empty_frag (next_fragP);
11931 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
11932 break;
11933 case FUSED_JCC_PADDING:
11934 /* Check if this is a fused jcc:
11935 FUSED_JCC_PADDING
11936 CMP like instruction
11937 BRANCH_PADDING
11938 COND_JUMP
11939 */
11940 cmp_fragP = i386_next_non_empty_frag (next_fragP);
11941 pad_fragP = i386_next_non_empty_frag (cmp_fragP);
79d72f45 11942 branch_fragP = i386_next_fusible_jcc_frag (next_fragP, pad_fragP);
e379e5f3
L
11943 if (branch_fragP)
11944 {
11945 /* The BRANCH_PADDING frag is merged with the
11946 FUSED_JCC_PADDING frag. */
11947 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
11948 /* CMP like instruction size. */
11949 next_fragP->tc_frag_data.cmp_size = cmp_fragP->fr_fix;
11950 frag_wane (pad_fragP);
11951 /* Skip to branch_fragP. */
11952 next_fragP = branch_fragP;
11953 }
11954 else if (next_fragP->tc_frag_data.max_prefix_length)
11955 {
11956 /* Turn FUSED_JCC_PADDING into BRANCH_PREFIX if it isn't
11957 a fused jcc. */
11958 next_fragP->fr_subtype
11959 = ENCODE_RELAX_STATE (BRANCH_PREFIX, 0);
11960 next_fragP->tc_frag_data.max_bytes
11961 = next_fragP->tc_frag_data.max_prefix_length;
11962 /* This will be updated in the BRANCH_PREFIX scan. */
11963 next_fragP->tc_frag_data.max_prefix_length = 0;
11964 }
11965 else
11966 frag_wane (next_fragP);
11967 break;
11968 }
11969 }
11970
11971 /* Stop if there is no BRANCH_PREFIX. */
11972 if (!align_branch_prefix_size)
11973 return;
11974
11975 /* Scan for BRANCH_PREFIX. */
11976 for (; fragP != NULL; fragP = fragP->fr_next)
11977 {
11978 if (fragP->fr_type != rs_machine_dependent
11979 || (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
11980 != BRANCH_PREFIX))
11981 continue;
11982
11983 /* Count all BRANCH_PREFIX frags before BRANCH_PADDING and
11984 COND_JUMP_PREFIX. */
11985 max_prefix_length = 0;
11986 for (next_fragP = fragP;
11987 next_fragP != NULL;
11988 next_fragP = next_fragP->fr_next)
11989 {
11990 if (next_fragP->fr_type == rs_fill)
11991 /* Skip rs_fill frags. */
11992 continue;
11993 else if (next_fragP->fr_type != rs_machine_dependent)
11994 /* Stop for all other frags. */
11995 break;
11996
11997 /* rs_machine_dependent frags. */
11998 if (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
11999 == BRANCH_PREFIX)
12000 {
12001 /* Count BRANCH_PREFIX frags. */
12002 if (max_prefix_length >= MAX_FUSED_JCC_PADDING_SIZE)
12003 {
12004 max_prefix_length = MAX_FUSED_JCC_PADDING_SIZE;
12005 frag_wane (next_fragP);
12006 }
12007 else
12008 max_prefix_length
12009 += next_fragP->tc_frag_data.max_bytes;
12010 }
12011 else if ((TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12012 == BRANCH_PADDING)
12013 || (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12014 == FUSED_JCC_PADDING))
12015 {
12016 /* Stop at BRANCH_PADDING and FUSED_JCC_PADDING. */
12017 fragP->tc_frag_data.u.padding_fragP = next_fragP;
12018 break;
12019 }
12020 else
12021 /* Stop for other rs_machine_dependent frags. */
12022 break;
12023 }
12024
12025 fragP->tc_frag_data.max_prefix_length = max_prefix_length;
12026
12027 /* Skip to the next frag. */
12028 fragP = next_fragP;
12029 }
12030}
12031
12032/* Compute padding size for
12033
12034 FUSED_JCC_PADDING
12035 CMP like instruction
12036 BRANCH_PADDING
12037 COND_JUMP/UNCOND_JUMP
12038
12039 or
12040
12041 BRANCH_PADDING
12042 COND_JUMP/UNCOND_JUMP
12043 */
12044
12045static int
12046i386_branch_padding_size (fragS *fragP, offsetT address)
12047{
12048 unsigned int offset, size, padding_size;
12049 fragS *branch_fragP = fragP->tc_frag_data.u.branch_fragP;
12050
12051 /* The start address of the BRANCH_PADDING or FUSED_JCC_PADDING frag. */
12052 if (!address)
12053 address = fragP->fr_address;
12054 address += fragP->fr_fix;
12055
12056 /* CMP like instrunction size. */
12057 size = fragP->tc_frag_data.cmp_size;
12058
12059 /* The base size of the branch frag. */
12060 size += branch_fragP->fr_fix;
12061
12062 /* Add opcode and displacement bytes for the rs_machine_dependent
12063 branch frag. */
12064 if (branch_fragP->fr_type == rs_machine_dependent)
12065 size += md_relax_table[branch_fragP->fr_subtype].rlx_length;
12066
12067 /* Check if branch is within boundary and doesn't end at the last
12068 byte. */
12069 offset = address & ((1U << align_branch_power) - 1);
12070 if ((offset + size) >= (1U << align_branch_power))
12071 /* Padding needed to avoid crossing boundary. */
12072 padding_size = (1U << align_branch_power) - offset;
12073 else
12074 /* No padding needed. */
12075 padding_size = 0;
12076
12077 /* The return value may be saved in tc_frag_data.length which is
12078 unsigned byte. */
12079 if (!fits_in_unsigned_byte (padding_size))
12080 abort ();
12081
12082 return padding_size;
12083}
12084
12085/* i386_generic_table_relax_frag()
12086
12087 Handle BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags to
12088 grow/shrink padding to align branch frags. Hand others to
12089 relax_frag(). */
12090
12091long
12092i386_generic_table_relax_frag (segT segment, fragS *fragP, long stretch)
12093{
12094 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12095 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
12096 {
12097 long padding_size = i386_branch_padding_size (fragP, 0);
12098 long grow = padding_size - fragP->tc_frag_data.length;
12099
12100 /* When the BRANCH_PREFIX frag is used, the computed address
12101 must match the actual address and there should be no padding. */
12102 if (fragP->tc_frag_data.padding_address
12103 && (fragP->tc_frag_data.padding_address != fragP->fr_address
12104 || padding_size))
12105 abort ();
12106
12107 /* Update the padding size. */
12108 if (grow)
12109 fragP->tc_frag_data.length = padding_size;
12110
12111 return grow;
12112 }
12113 else if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12114 {
12115 fragS *padding_fragP, *next_fragP;
12116 long padding_size, left_size, last_size;
12117
12118 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
12119 if (!padding_fragP)
12120 /* Use the padding set by the leading BRANCH_PREFIX frag. */
12121 return (fragP->tc_frag_data.length
12122 - fragP->tc_frag_data.last_length);
12123
12124 /* Compute the relative address of the padding frag in the very
12125 first time where the BRANCH_PREFIX frag sizes are zero. */
12126 if (!fragP->tc_frag_data.padding_address)
12127 fragP->tc_frag_data.padding_address
12128 = padding_fragP->fr_address - (fragP->fr_address - stretch);
12129
12130 /* First update the last length from the previous interation. */
12131 left_size = fragP->tc_frag_data.prefix_length;
12132 for (next_fragP = fragP;
12133 next_fragP != padding_fragP;
12134 next_fragP = next_fragP->fr_next)
12135 if (next_fragP->fr_type == rs_machine_dependent
12136 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12137 == BRANCH_PREFIX))
12138 {
12139 if (left_size)
12140 {
12141 int max = next_fragP->tc_frag_data.max_bytes;
12142 if (max)
12143 {
12144 int size;
12145 if (max > left_size)
12146 size = left_size;
12147 else
12148 size = max;
12149 left_size -= size;
12150 next_fragP->tc_frag_data.last_length = size;
12151 }
12152 }
12153 else
12154 next_fragP->tc_frag_data.last_length = 0;
12155 }
12156
12157 /* Check the padding size for the padding frag. */
12158 padding_size = i386_branch_padding_size
12159 (padding_fragP, (fragP->fr_address
12160 + fragP->tc_frag_data.padding_address));
12161
12162 last_size = fragP->tc_frag_data.prefix_length;
12163 /* Check if there is change from the last interation. */
12164 if (padding_size == last_size)
12165 {
12166 /* Update the expected address of the padding frag. */
12167 padding_fragP->tc_frag_data.padding_address
12168 = (fragP->fr_address + padding_size
12169 + fragP->tc_frag_data.padding_address);
12170 return 0;
12171 }
12172
12173 if (padding_size > fragP->tc_frag_data.max_prefix_length)
12174 {
12175 /* No padding if there is no sufficient room. Clear the
12176 expected address of the padding frag. */
12177 padding_fragP->tc_frag_data.padding_address = 0;
12178 padding_size = 0;
12179 }
12180 else
12181 /* Store the expected address of the padding frag. */
12182 padding_fragP->tc_frag_data.padding_address
12183 = (fragP->fr_address + padding_size
12184 + fragP->tc_frag_data.padding_address);
12185
12186 fragP->tc_frag_data.prefix_length = padding_size;
12187
12188 /* Update the length for the current interation. */
12189 left_size = padding_size;
12190 for (next_fragP = fragP;
12191 next_fragP != padding_fragP;
12192 next_fragP = next_fragP->fr_next)
12193 if (next_fragP->fr_type == rs_machine_dependent
12194 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12195 == BRANCH_PREFIX))
12196 {
12197 if (left_size)
12198 {
12199 int max = next_fragP->tc_frag_data.max_bytes;
12200 if (max)
12201 {
12202 int size;
12203 if (max > left_size)
12204 size = left_size;
12205 else
12206 size = max;
12207 left_size -= size;
12208 next_fragP->tc_frag_data.length = size;
12209 }
12210 }
12211 else
12212 next_fragP->tc_frag_data.length = 0;
12213 }
12214
12215 return (fragP->tc_frag_data.length
12216 - fragP->tc_frag_data.last_length);
12217 }
12218 return relax_frag (segment, fragP, stretch);
12219}
12220
ee7fcc42
AM
12221/* md_estimate_size_before_relax()
12222
12223 Called just before relax() for rs_machine_dependent frags. The x86
12224 assembler uses these frags to handle variable size jump
12225 instructions.
12226
12227 Any symbol that is now undefined will not become defined.
12228 Return the correct fr_subtype in the frag.
12229 Return the initial "guess for variable size of frag" to caller.
12230 The guess is actually the growth beyond the fixed part. Whatever
12231 we do to grow the fixed or variable part contributes to our
12232 returned value. */
12233
252b5132 12234int
7016a5d5 12235md_estimate_size_before_relax (fragS *fragP, segT segment)
252b5132 12236{
e379e5f3
L
12237 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12238 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX
12239 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
12240 {
12241 i386_classify_machine_dependent_frag (fragP);
12242 return fragP->tc_frag_data.length;
12243 }
12244
252b5132 12245 /* We've already got fragP->fr_subtype right; all we have to do is
b98ef147
AM
12246 check for un-relaxable symbols. On an ELF system, we can't relax
12247 an externally visible symbol, because it may be overridden by a
12248 shared library. */
12249 if (S_GET_SEGMENT (fragP->fr_symbol) != segment
6d249963 12250#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12251 || (IS_ELF
8dcea932
L
12252 && !elf_symbol_resolved_in_segment_p (fragP->fr_symbol,
12253 fragP->fr_var))
fbeb56a4
DK
12254#endif
12255#if defined (OBJ_COFF) && defined (TE_PE)
7ab9ffdd 12256 || (OUTPUT_FLAVOR == bfd_target_coff_flavour
fbeb56a4 12257 && S_IS_WEAK (fragP->fr_symbol))
b98ef147
AM
12258#endif
12259 )
252b5132 12260 {
b98ef147
AM
12261 /* Symbol is undefined in this segment, or we need to keep a
12262 reloc so that weak symbols can be overridden. */
12263 int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
f86103b7 12264 enum bfd_reloc_code_real reloc_type;
ee7fcc42
AM
12265 unsigned char *opcode;
12266 int old_fr_fix;
eb19308f 12267 fixS *fixP = NULL;
f6af82bd 12268
ee7fcc42 12269 if (fragP->fr_var != NO_RELOC)
1e9cc1c2 12270 reloc_type = (enum bfd_reloc_code_real) fragP->fr_var;
b98ef147 12271 else if (size == 2)
f6af82bd 12272 reloc_type = BFD_RELOC_16_PCREL;
bd7ab16b 12273#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1ef3994a
JB
12274 else if (fragP->tc_frag_data.code64 && fragP->fr_offset == 0
12275 && need_plt32_p (fragP->fr_symbol))
bd7ab16b
L
12276 reloc_type = BFD_RELOC_X86_64_PLT32;
12277#endif
f6af82bd
AM
12278 else
12279 reloc_type = BFD_RELOC_32_PCREL;
252b5132 12280
ee7fcc42
AM
12281 old_fr_fix = fragP->fr_fix;
12282 opcode = (unsigned char *) fragP->fr_opcode;
12283
fddf5b5b 12284 switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
252b5132 12285 {
fddf5b5b
AM
12286 case UNCOND_JUMP:
12287 /* Make jmp (0xeb) a (d)word displacement jump. */
47926f60 12288 opcode[0] = 0xe9;
252b5132 12289 fragP->fr_fix += size;
eb19308f
JB
12290 fixP = fix_new (fragP, old_fr_fix, size,
12291 fragP->fr_symbol,
12292 fragP->fr_offset, 1,
12293 reloc_type);
252b5132
RH
12294 break;
12295
fddf5b5b 12296 case COND_JUMP86:
412167cb
AM
12297 if (size == 2
12298 && (!no_cond_jump_promotion || fragP->fr_var != NO_RELOC))
fddf5b5b
AM
12299 {
12300 /* Negate the condition, and branch past an
12301 unconditional jump. */
12302 opcode[0] ^= 1;
12303 opcode[1] = 3;
12304 /* Insert an unconditional jump. */
12305 opcode[2] = 0xe9;
12306 /* We added two extra opcode bytes, and have a two byte
12307 offset. */
12308 fragP->fr_fix += 2 + 2;
062cd5e7
AS
12309 fix_new (fragP, old_fr_fix + 2, 2,
12310 fragP->fr_symbol,
12311 fragP->fr_offset, 1,
12312 reloc_type);
fddf5b5b
AM
12313 break;
12314 }
12315 /* Fall through. */
12316
12317 case COND_JUMP:
412167cb
AM
12318 if (no_cond_jump_promotion && fragP->fr_var == NO_RELOC)
12319 {
12320 fragP->fr_fix += 1;
3e02c1cc
AM
12321 fixP = fix_new (fragP, old_fr_fix, 1,
12322 fragP->fr_symbol,
12323 fragP->fr_offset, 1,
12324 BFD_RELOC_8_PCREL);
12325 fixP->fx_signed = 1;
412167cb
AM
12326 break;
12327 }
93c2a809 12328
24eab124 12329 /* This changes the byte-displacement jump 0x7N
fddf5b5b 12330 to the (d)word-displacement jump 0x0f,0x8N. */
252b5132 12331 opcode[1] = opcode[0] + 0x10;
f6af82bd 12332 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
47926f60
KH
12333 /* We've added an opcode byte. */
12334 fragP->fr_fix += 1 + size;
eb19308f
JB
12335 fixP = fix_new (fragP, old_fr_fix + 1, size,
12336 fragP->fr_symbol,
12337 fragP->fr_offset, 1,
12338 reloc_type);
252b5132 12339 break;
fddf5b5b
AM
12340
12341 default:
12342 BAD_CASE (fragP->fr_subtype);
12343 break;
252b5132 12344 }
eb19308f
JB
12345
12346 /* All jumps handled here are signed, but don't unconditionally use a
12347 signed limit check for 32 and 16 bit jumps as we want to allow wrap
12348 around at 4G (outside of 64-bit mode) and 64k. */
12349 if (size == 4 && flag_code == CODE_64BIT)
12350 fixP->fx_signed = 1;
12351
252b5132 12352 frag_wane (fragP);
ee7fcc42 12353 return fragP->fr_fix - old_fr_fix;
252b5132 12354 }
93c2a809 12355
93c2a809
AM
12356 /* Guess size depending on current relax state. Initially the relax
12357 state will correspond to a short jump and we return 1, because
12358 the variable part of the frag (the branch offset) is one byte
12359 long. However, we can relax a section more than once and in that
12360 case we must either set fr_subtype back to the unrelaxed state,
12361 or return the value for the appropriate branch. */
12362 return md_relax_table[fragP->fr_subtype].rlx_length;
ee7fcc42
AM
12363}
12364
47926f60
KH
12365/* Called after relax() is finished.
12366
12367 In: Address of frag.
12368 fr_type == rs_machine_dependent.
12369 fr_subtype is what the address relaxed to.
12370
12371 Out: Any fixSs and constants are set up.
12372 Caller will turn frag into a ".space 0". */
12373
252b5132 12374void
7016a5d5
TG
12375md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec ATTRIBUTE_UNUSED,
12376 fragS *fragP)
252b5132 12377{
29b0f896 12378 unsigned char *opcode;
252b5132 12379 unsigned char *where_to_put_displacement = NULL;
847f7ad4
AM
12380 offsetT target_address;
12381 offsetT opcode_address;
252b5132 12382 unsigned int extension = 0;
847f7ad4 12383 offsetT displacement_from_opcode_start;
252b5132 12384
e379e5f3
L
12385 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12386 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING
12387 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12388 {
12389 /* Generate nop padding. */
12390 unsigned int size = fragP->tc_frag_data.length;
12391 if (size)
12392 {
12393 if (size > fragP->tc_frag_data.max_bytes)
12394 abort ();
12395
12396 if (flag_debug)
12397 {
12398 const char *msg;
12399 const char *branch = "branch";
12400 const char *prefix = "";
12401 fragS *padding_fragP;
12402 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
12403 == BRANCH_PREFIX)
12404 {
12405 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
12406 switch (fragP->tc_frag_data.default_prefix)
12407 {
12408 default:
12409 abort ();
12410 break;
12411 case CS_PREFIX_OPCODE:
12412 prefix = " cs";
12413 break;
12414 case DS_PREFIX_OPCODE:
12415 prefix = " ds";
12416 break;
12417 case ES_PREFIX_OPCODE:
12418 prefix = " es";
12419 break;
12420 case FS_PREFIX_OPCODE:
12421 prefix = " fs";
12422 break;
12423 case GS_PREFIX_OPCODE:
12424 prefix = " gs";
12425 break;
12426 case SS_PREFIX_OPCODE:
12427 prefix = " ss";
12428 break;
12429 }
12430 if (padding_fragP)
12431 msg = _("%s:%u: add %d%s at 0x%llx to align "
12432 "%s within %d-byte boundary\n");
12433 else
12434 msg = _("%s:%u: add additional %d%s at 0x%llx to "
12435 "align %s within %d-byte boundary\n");
12436 }
12437 else
12438 {
12439 padding_fragP = fragP;
12440 msg = _("%s:%u: add %d%s-byte nop at 0x%llx to align "
12441 "%s within %d-byte boundary\n");
12442 }
12443
12444 if (padding_fragP)
12445 switch (padding_fragP->tc_frag_data.branch_type)
12446 {
12447 case align_branch_jcc:
12448 branch = "jcc";
12449 break;
12450 case align_branch_fused:
12451 branch = "fused jcc";
12452 break;
12453 case align_branch_jmp:
12454 branch = "jmp";
12455 break;
12456 case align_branch_call:
12457 branch = "call";
12458 break;
12459 case align_branch_indirect:
12460 branch = "indiret branch";
12461 break;
12462 case align_branch_ret:
12463 branch = "ret";
12464 break;
12465 default:
12466 break;
12467 }
12468
12469 fprintf (stdout, msg,
12470 fragP->fr_file, fragP->fr_line, size, prefix,
12471 (long long) fragP->fr_address, branch,
12472 1 << align_branch_power);
12473 }
12474 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12475 memset (fragP->fr_opcode,
12476 fragP->tc_frag_data.default_prefix, size);
12477 else
12478 i386_generate_nops (fragP, (char *) fragP->fr_opcode,
12479 size, 0);
12480 fragP->fr_fix += size;
12481 }
12482 return;
12483 }
12484
252b5132
RH
12485 opcode = (unsigned char *) fragP->fr_opcode;
12486
47926f60 12487 /* Address we want to reach in file space. */
252b5132 12488 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
252b5132 12489
47926f60 12490 /* Address opcode resides at in file space. */
252b5132
RH
12491 opcode_address = fragP->fr_address + fragP->fr_fix;
12492
47926f60 12493 /* Displacement from opcode start to fill into instruction. */
252b5132
RH
12494 displacement_from_opcode_start = target_address - opcode_address;
12495
fddf5b5b 12496 if ((fragP->fr_subtype & BIG) == 0)
252b5132 12497 {
47926f60
KH
12498 /* Don't have to change opcode. */
12499 extension = 1; /* 1 opcode + 1 displacement */
252b5132 12500 where_to_put_displacement = &opcode[1];
fddf5b5b
AM
12501 }
12502 else
12503 {
12504 if (no_cond_jump_promotion
12505 && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
4eed87de
AM
12506 as_warn_where (fragP->fr_file, fragP->fr_line,
12507 _("long jump required"));
252b5132 12508
fddf5b5b
AM
12509 switch (fragP->fr_subtype)
12510 {
12511 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
12512 extension = 4; /* 1 opcode + 4 displacement */
12513 opcode[0] = 0xe9;
12514 where_to_put_displacement = &opcode[1];
12515 break;
252b5132 12516
fddf5b5b
AM
12517 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
12518 extension = 2; /* 1 opcode + 2 displacement */
12519 opcode[0] = 0xe9;
12520 where_to_put_displacement = &opcode[1];
12521 break;
252b5132 12522
fddf5b5b
AM
12523 case ENCODE_RELAX_STATE (COND_JUMP, BIG):
12524 case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
12525 extension = 5; /* 2 opcode + 4 displacement */
12526 opcode[1] = opcode[0] + 0x10;
12527 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12528 where_to_put_displacement = &opcode[2];
12529 break;
252b5132 12530
fddf5b5b
AM
12531 case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
12532 extension = 3; /* 2 opcode + 2 displacement */
12533 opcode[1] = opcode[0] + 0x10;
12534 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12535 where_to_put_displacement = &opcode[2];
12536 break;
252b5132 12537
fddf5b5b
AM
12538 case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
12539 extension = 4;
12540 opcode[0] ^= 1;
12541 opcode[1] = 3;
12542 opcode[2] = 0xe9;
12543 where_to_put_displacement = &opcode[3];
12544 break;
12545
12546 default:
12547 BAD_CASE (fragP->fr_subtype);
12548 break;
12549 }
252b5132 12550 }
fddf5b5b 12551
7b81dfbb
AJ
12552 /* If size if less then four we are sure that the operand fits,
12553 but if it's 4, then it could be that the displacement is larger
12554 then -/+ 2GB. */
12555 if (DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype) == 4
12556 && object_64bit
12557 && ((addressT) (displacement_from_opcode_start - extension
4eed87de
AM
12558 + ((addressT) 1 << 31))
12559 > (((addressT) 2 << 31) - 1)))
7b81dfbb
AJ
12560 {
12561 as_bad_where (fragP->fr_file, fragP->fr_line,
12562 _("jump target out of range"));
12563 /* Make us emit 0. */
12564 displacement_from_opcode_start = extension;
12565 }
47926f60 12566 /* Now put displacement after opcode. */
252b5132
RH
12567 md_number_to_chars ((char *) where_to_put_displacement,
12568 (valueT) (displacement_from_opcode_start - extension),
fddf5b5b 12569 DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
252b5132
RH
12570 fragP->fr_fix += extension;
12571}
12572\f
7016a5d5 12573/* Apply a fixup (fixP) to segment data, once it has been determined
252b5132
RH
12574 by our caller that we have all the info we need to fix it up.
12575
7016a5d5
TG
12576 Parameter valP is the pointer to the value of the bits.
12577
252b5132
RH
12578 On the 386, immediates, displacements, and data pointers are all in
12579 the same (little-endian) format, so we don't need to care about which
12580 we are handling. */
12581
94f592af 12582void
7016a5d5 12583md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 12584{
94f592af 12585 char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
c6682705 12586 valueT value = *valP;
252b5132 12587
f86103b7 12588#if !defined (TE_Mach)
93382f6d
AM
12589 if (fixP->fx_pcrel)
12590 {
12591 switch (fixP->fx_r_type)
12592 {
5865bb77
ILT
12593 default:
12594 break;
12595
d6ab8113
JB
12596 case BFD_RELOC_64:
12597 fixP->fx_r_type = BFD_RELOC_64_PCREL;
12598 break;
93382f6d 12599 case BFD_RELOC_32:
ae8887b5 12600 case BFD_RELOC_X86_64_32S:
93382f6d
AM
12601 fixP->fx_r_type = BFD_RELOC_32_PCREL;
12602 break;
12603 case BFD_RELOC_16:
12604 fixP->fx_r_type = BFD_RELOC_16_PCREL;
12605 break;
12606 case BFD_RELOC_8:
12607 fixP->fx_r_type = BFD_RELOC_8_PCREL;
12608 break;
12609 }
12610 }
252b5132 12611
a161fe53 12612 if (fixP->fx_addsy != NULL
31312f95 12613 && (fixP->fx_r_type == BFD_RELOC_32_PCREL
d6ab8113 12614 || fixP->fx_r_type == BFD_RELOC_64_PCREL
31312f95 12615 || fixP->fx_r_type == BFD_RELOC_16_PCREL
d258b828 12616 || fixP->fx_r_type == BFD_RELOC_8_PCREL)
31312f95 12617 && !use_rela_relocations)
252b5132 12618 {
31312f95
AM
12619 /* This is a hack. There should be a better way to handle this.
12620 This covers for the fact that bfd_install_relocation will
12621 subtract the current location (for partial_inplace, PC relative
12622 relocations); see more below. */
252b5132 12623#ifndef OBJ_AOUT
718ddfc0 12624 if (IS_ELF
252b5132
RH
12625#ifdef TE_PE
12626 || OUTPUT_FLAVOR == bfd_target_coff_flavour
12627#endif
12628 )
12629 value += fixP->fx_where + fixP->fx_frag->fr_address;
12630#endif
12631#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12632 if (IS_ELF)
252b5132 12633 {
6539b54b 12634 segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
2f66722d 12635
6539b54b 12636 if ((sym_seg == seg
2f66722d 12637 || (symbol_section_p (fixP->fx_addsy)
6539b54b 12638 && sym_seg != absolute_section))
af65af87 12639 && !generic_force_reloc (fixP))
2f66722d
AM
12640 {
12641 /* Yes, we add the values in twice. This is because
6539b54b
AM
12642 bfd_install_relocation subtracts them out again. I think
12643 bfd_install_relocation is broken, but I don't dare change
2f66722d
AM
12644 it. FIXME. */
12645 value += fixP->fx_where + fixP->fx_frag->fr_address;
12646 }
252b5132
RH
12647 }
12648#endif
12649#if defined (OBJ_COFF) && defined (TE_PE)
977cdf5a
NC
12650 /* For some reason, the PE format does not store a
12651 section address offset for a PC relative symbol. */
12652 if (S_GET_SEGMENT (fixP->fx_addsy) != seg
7be1c489 12653 || S_IS_WEAK (fixP->fx_addsy))
252b5132
RH
12654 value += md_pcrel_from (fixP);
12655#endif
12656 }
fbeb56a4 12657#if defined (OBJ_COFF) && defined (TE_PE)
f01c1a09
NC
12658 if (fixP->fx_addsy != NULL
12659 && S_IS_WEAK (fixP->fx_addsy)
12660 /* PR 16858: Do not modify weak function references. */
12661 && ! fixP->fx_pcrel)
fbeb56a4 12662 {
296a8689
NC
12663#if !defined (TE_PEP)
12664 /* For x86 PE weak function symbols are neither PC-relative
12665 nor do they set S_IS_FUNCTION. So the only reliable way
12666 to detect them is to check the flags of their containing
12667 section. */
12668 if (S_GET_SEGMENT (fixP->fx_addsy) != NULL
12669 && S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_CODE)
12670 ;
12671 else
12672#endif
fbeb56a4
DK
12673 value -= S_GET_VALUE (fixP->fx_addsy);
12674 }
12675#endif
252b5132
RH
12676
12677 /* Fix a few things - the dynamic linker expects certain values here,
0234cb7c 12678 and we must not disappoint it. */
252b5132 12679#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12680 if (IS_ELF && fixP->fx_addsy)
47926f60
KH
12681 switch (fixP->fx_r_type)
12682 {
12683 case BFD_RELOC_386_PLT32:
3e73aa7c 12684 case BFD_RELOC_X86_64_PLT32:
b9519cfe
L
12685 /* Make the jump instruction point to the address of the operand.
12686 At runtime we merely add the offset to the actual PLT entry.
12687 NB: Subtract the offset size only for jump instructions. */
12688 if (fixP->fx_pcrel)
12689 value = -4;
47926f60 12690 break;
31312f95 12691
13ae64f3
JJ
12692 case BFD_RELOC_386_TLS_GD:
12693 case BFD_RELOC_386_TLS_LDM:
13ae64f3 12694 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
12695 case BFD_RELOC_386_TLS_IE:
12696 case BFD_RELOC_386_TLS_GOTIE:
67a4f2b7 12697 case BFD_RELOC_386_TLS_GOTDESC:
bffbf940
JJ
12698 case BFD_RELOC_X86_64_TLSGD:
12699 case BFD_RELOC_X86_64_TLSLD:
12700 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7 12701 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
00f7efb6
JJ
12702 value = 0; /* Fully resolved at runtime. No addend. */
12703 /* Fallthrough */
12704 case BFD_RELOC_386_TLS_LE:
12705 case BFD_RELOC_386_TLS_LDO_32:
12706 case BFD_RELOC_386_TLS_LE_32:
12707 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 12708 case BFD_RELOC_X86_64_DTPOFF64:
00f7efb6 12709 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113 12710 case BFD_RELOC_X86_64_TPOFF64:
00f7efb6
JJ
12711 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12712 break;
12713
67a4f2b7
AO
12714 case BFD_RELOC_386_TLS_DESC_CALL:
12715 case BFD_RELOC_X86_64_TLSDESC_CALL:
12716 value = 0; /* Fully resolved at runtime. No addend. */
12717 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12718 fixP->fx_done = 0;
12719 return;
12720
47926f60
KH
12721 case BFD_RELOC_VTABLE_INHERIT:
12722 case BFD_RELOC_VTABLE_ENTRY:
12723 fixP->fx_done = 0;
94f592af 12724 return;
47926f60
KH
12725
12726 default:
12727 break;
12728 }
12729#endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) */
a442cac5
JB
12730
12731 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
12732 if (!object_64bit)
12733 value = extend_to_32bit_address (value);
12734
c6682705 12735 *valP = value;
f86103b7 12736#endif /* !defined (TE_Mach) */
3e73aa7c 12737
3e73aa7c 12738 /* Are we finished with this relocation now? */
c6682705 12739 if (fixP->fx_addsy == NULL)
b8188555
JB
12740 {
12741 fixP->fx_done = 1;
12742 switch (fixP->fx_r_type)
12743 {
12744 case BFD_RELOC_X86_64_32S:
12745 fixP->fx_signed = 1;
12746 break;
12747
12748 default:
12749 break;
12750 }
12751 }
fbeb56a4
DK
12752#if defined (OBJ_COFF) && defined (TE_PE)
12753 else if (fixP->fx_addsy != NULL && S_IS_WEAK (fixP->fx_addsy))
12754 {
12755 fixP->fx_done = 0;
12756 /* Remember value for tc_gen_reloc. */
12757 fixP->fx_addnumber = value;
12758 /* Clear out the frag for now. */
12759 value = 0;
12760 }
12761#endif
3e73aa7c
JH
12762 else if (use_rela_relocations)
12763 {
46fb6d5a
JB
12764 if (!disallow_64bit_reloc || fixP->fx_r_type == NO_RELOC)
12765 fixP->fx_no_overflow = 1;
062cd5e7
AS
12766 /* Remember value for tc_gen_reloc. */
12767 fixP->fx_addnumber = value;
3e73aa7c
JH
12768 value = 0;
12769 }
f86103b7 12770
94f592af 12771 md_number_to_chars (p, value, fixP->fx_size);
252b5132 12772}
252b5132 12773\f
6d4af3c2 12774const char *
499ac353 12775md_atof (int type, char *litP, int *sizeP)
252b5132 12776{
499ac353
NC
12777 /* This outputs the LITTLENUMs in REVERSE order;
12778 in accord with the bigendian 386. */
5b7c81bd 12779 return ieee_md_atof (type, litP, sizeP, false);
252b5132
RH
12780}
12781\f
2d545b82 12782static char output_invalid_buf[sizeof (unsigned char) * 2 + 6];
252b5132 12783
252b5132 12784static char *
e3bb37b5 12785output_invalid (int c)
252b5132 12786{
3882b010 12787 if (ISPRINT (c))
f9f21a03
L
12788 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
12789 "'%c'", c);
252b5132 12790 else
f9f21a03 12791 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
2d545b82 12792 "(0x%x)", (unsigned char) c);
252b5132
RH
12793 return output_invalid_buf;
12794}
12795
8a6fb3f9
JB
12796/* Verify that @r can be used in the current context. */
12797
5b7c81bd 12798static bool check_register (const reg_entry *r)
8a6fb3f9
JB
12799{
12800 if (allow_pseudo_reg)
5b7c81bd 12801 return true;
8a6fb3f9
JB
12802
12803 if (operand_type_all_zero (&r->reg_type))
5b7c81bd 12804 return false;
8a6fb3f9
JB
12805
12806 if ((r->reg_type.bitfield.dword
12807 || (r->reg_type.bitfield.class == SReg && r->reg_num > 3)
12808 || r->reg_type.bitfield.class == RegCR
22e00a3f 12809 || r->reg_type.bitfield.class == RegDR)
8a6fb3f9 12810 && !cpu_arch_flags.bitfield.cpui386)
5b7c81bd 12811 return false;
8a6fb3f9 12812
22e00a3f
JB
12813 if (r->reg_type.bitfield.class == RegTR
12814 && (flag_code == CODE_64BIT
12815 || !cpu_arch_flags.bitfield.cpui386
12816 || cpu_arch_isa_flags.bitfield.cpui586
12817 || cpu_arch_isa_flags.bitfield.cpui686))
5b7c81bd 12818 return false;
22e00a3f 12819
8a6fb3f9 12820 if (r->reg_type.bitfield.class == RegMMX && !cpu_arch_flags.bitfield.cpummx)
5b7c81bd 12821 return false;
8a6fb3f9
JB
12822
12823 if (!cpu_arch_flags.bitfield.cpuavx512f)
12824 {
12825 if (r->reg_type.bitfield.zmmword
12826 || r->reg_type.bitfield.class == RegMask)
5b7c81bd 12827 return false;
8a6fb3f9
JB
12828
12829 if (!cpu_arch_flags.bitfield.cpuavx)
12830 {
12831 if (r->reg_type.bitfield.ymmword)
5b7c81bd 12832 return false;
8a6fb3f9
JB
12833
12834 if (!cpu_arch_flags.bitfield.cpusse && r->reg_type.bitfield.xmmword)
5b7c81bd 12835 return false;
8a6fb3f9
JB
12836 }
12837 }
12838
260cd341
LC
12839 if (r->reg_type.bitfield.tmmword
12840 && (!cpu_arch_flags.bitfield.cpuamx_tile
12841 || flag_code != CODE_64BIT))
5b7c81bd 12842 return false;
260cd341 12843
8a6fb3f9 12844 if (r->reg_type.bitfield.class == RegBND && !cpu_arch_flags.bitfield.cpumpx)
5b7c81bd 12845 return false;
8a6fb3f9
JB
12846
12847 /* Don't allow fake index register unless allow_index_reg isn't 0. */
12848 if (!allow_index_reg && r->reg_num == RegIZ)
5b7c81bd 12849 return false;
8a6fb3f9
JB
12850
12851 /* Upper 16 vector registers are only available with VREX in 64bit
12852 mode, and require EVEX encoding. */
12853 if (r->reg_flags & RegVRex)
12854 {
12855 if (!cpu_arch_flags.bitfield.cpuavx512f
12856 || flag_code != CODE_64BIT)
5b7c81bd 12857 return false;
8a6fb3f9 12858
da4977e0
JB
12859 if (i.vec_encoding == vex_encoding_default)
12860 i.vec_encoding = vex_encoding_evex;
12861 else if (i.vec_encoding != vex_encoding_evex)
12862 i.vec_encoding = vex_encoding_error;
8a6fb3f9
JB
12863 }
12864
12865 if (((r->reg_flags & (RegRex64 | RegRex)) || r->reg_type.bitfield.qword)
12866 && (!cpu_arch_flags.bitfield.cpulm || r->reg_type.bitfield.class != RegCR)
12867 && flag_code != CODE_64BIT)
5b7c81bd 12868 return false;
8a6fb3f9
JB
12869
12870 if (r->reg_type.bitfield.class == SReg && r->reg_num == RegFlat
12871 && !intel_syntax)
5b7c81bd 12872 return false;
8a6fb3f9 12873
5b7c81bd 12874 return true;
8a6fb3f9
JB
12875}
12876
af6bdddf 12877/* REG_STRING starts *before* REGISTER_PREFIX. */
252b5132
RH
12878
12879static const reg_entry *
4d1bb795 12880parse_real_register (char *reg_string, char **end_op)
252b5132 12881{
af6bdddf
AM
12882 char *s = reg_string;
12883 char *p;
252b5132
RH
12884 char reg_name_given[MAX_REG_NAME_SIZE + 1];
12885 const reg_entry *r;
12886
12887 /* Skip possible REGISTER_PREFIX and possible whitespace. */
12888 if (*s == REGISTER_PREFIX)
12889 ++s;
12890
12891 if (is_space_char (*s))
12892 ++s;
12893
12894 p = reg_name_given;
af6bdddf 12895 while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
252b5132
RH
12896 {
12897 if (p >= reg_name_given + MAX_REG_NAME_SIZE)
af6bdddf
AM
12898 return (const reg_entry *) NULL;
12899 s++;
252b5132
RH
12900 }
12901
6588847e
DN
12902 /* For naked regs, make sure that we are not dealing with an identifier.
12903 This prevents confusing an identifier like `eax_var' with register
12904 `eax'. */
12905 if (allow_naked_reg && identifier_chars[(unsigned char) *s])
12906 return (const reg_entry *) NULL;
12907
af6bdddf 12908 *end_op = s;
252b5132 12909
629310ab 12910 r = (const reg_entry *) str_hash_find (reg_hash, reg_name_given);
252b5132 12911
5f47d35b 12912 /* Handle floating point regs, allowing spaces in the (i) part. */
6288d05f 12913 if (r == reg_st0)
5f47d35b 12914 {
0e0eea78
JB
12915 if (!cpu_arch_flags.bitfield.cpu8087
12916 && !cpu_arch_flags.bitfield.cpu287
af32b722
JB
12917 && !cpu_arch_flags.bitfield.cpu387
12918 && !allow_pseudo_reg)
0e0eea78
JB
12919 return (const reg_entry *) NULL;
12920
5f47d35b
AM
12921 if (is_space_char (*s))
12922 ++s;
12923 if (*s == '(')
12924 {
af6bdddf 12925 ++s;
5f47d35b
AM
12926 if (is_space_char (*s))
12927 ++s;
12928 if (*s >= '0' && *s <= '7')
12929 {
db557034 12930 int fpr = *s - '0';
af6bdddf 12931 ++s;
5f47d35b
AM
12932 if (is_space_char (*s))
12933 ++s;
12934 if (*s == ')')
12935 {
12936 *end_op = s + 1;
6288d05f 12937 know (r[fpr].reg_num == fpr);
db557034 12938 return r + fpr;
5f47d35b 12939 }
5f47d35b 12940 }
47926f60 12941 /* We have "%st(" then garbage. */
5f47d35b
AM
12942 return (const reg_entry *) NULL;
12943 }
12944 }
12945
8a6fb3f9 12946 return r && check_register (r) ? r : NULL;
252b5132 12947}
4d1bb795
JB
12948
12949/* REG_STRING starts *before* REGISTER_PREFIX. */
12950
12951static const reg_entry *
12952parse_register (char *reg_string, char **end_op)
12953{
12954 const reg_entry *r;
12955
12956 if (*reg_string == REGISTER_PREFIX || allow_naked_reg)
12957 r = parse_real_register (reg_string, end_op);
12958 else
12959 r = NULL;
12960 if (!r)
12961 {
12962 char *save = input_line_pointer;
12963 char c;
12964 symbolS *symbolP;
12965
12966 input_line_pointer = reg_string;
d02603dc 12967 c = get_symbol_name (&reg_string);
4d1bb795 12968 symbolP = symbol_find (reg_string);
64d23078
JB
12969 while (symbolP && S_GET_SEGMENT (symbolP) != reg_section)
12970 {
12971 const expressionS *e = symbol_get_value_expression(symbolP);
12972
12973 if (e->X_op != O_symbol || e->X_add_number)
12974 break;
12975 symbolP = e->X_add_symbol;
12976 }
4d1bb795
JB
12977 if (symbolP && S_GET_SEGMENT (symbolP) == reg_section)
12978 {
12979 const expressionS *e = symbol_get_value_expression (symbolP);
12980
3b55a1d0
JB
12981 know (e->X_op == O_register);
12982 know (e->X_add_number >= 0
12983 && (valueT) e->X_add_number < i386_regtab_size);
12984 r = i386_regtab + e->X_add_number;
12985 if (!check_register (r))
8a6fb3f9 12986 {
3b55a1d0
JB
12987 as_bad (_("register '%s%s' cannot be used here"),
12988 register_prefix, r->reg_name);
12989 r = &bad_reg;
8a6fb3f9 12990 }
3b55a1d0 12991 *end_op = input_line_pointer;
4d1bb795
JB
12992 }
12993 *input_line_pointer = c;
12994 input_line_pointer = save;
12995 }
12996 return r;
12997}
12998
12999int
13000i386_parse_name (char *name, expressionS *e, char *nextcharP)
13001{
4faaa10f 13002 const reg_entry *r = NULL;
4d1bb795
JB
13003 char *end = input_line_pointer;
13004
13005 *end = *nextcharP;
4faaa10f
JB
13006 if (*name == REGISTER_PREFIX || allow_naked_reg)
13007 r = parse_real_register (name, &input_line_pointer);
4d1bb795
JB
13008 if (r && end <= input_line_pointer)
13009 {
13010 *nextcharP = *input_line_pointer;
13011 *input_line_pointer = 0;
8a6fb3f9
JB
13012 if (r != &bad_reg)
13013 {
13014 e->X_op = O_register;
13015 e->X_add_number = r - i386_regtab;
13016 }
13017 else
13018 e->X_op = O_illegal;
4d1bb795
JB
13019 return 1;
13020 }
13021 input_line_pointer = end;
13022 *end = 0;
ee86248c 13023 return intel_syntax ? i386_intel_parse_name (name, e) : 0;
4d1bb795
JB
13024}
13025
13026void
13027md_operand (expressionS *e)
13028{
ee86248c
JB
13029 char *end;
13030 const reg_entry *r;
4d1bb795 13031
ee86248c
JB
13032 switch (*input_line_pointer)
13033 {
13034 case REGISTER_PREFIX:
13035 r = parse_real_register (input_line_pointer, &end);
4d1bb795
JB
13036 if (r)
13037 {
13038 e->X_op = O_register;
13039 e->X_add_number = r - i386_regtab;
13040 input_line_pointer = end;
13041 }
ee86248c
JB
13042 break;
13043
13044 case '[':
9c2799c2 13045 gas_assert (intel_syntax);
ee86248c
JB
13046 end = input_line_pointer++;
13047 expression (e);
13048 if (*input_line_pointer == ']')
13049 {
13050 ++input_line_pointer;
13051 e->X_op_symbol = make_expr_symbol (e);
13052 e->X_add_symbol = NULL;
13053 e->X_add_number = 0;
13054 e->X_op = O_index;
13055 }
13056 else
13057 {
13058 e->X_op = O_absent;
13059 input_line_pointer = end;
13060 }
13061 break;
4d1bb795
JB
13062 }
13063}
13064
252b5132 13065\f
4cc782b5 13066#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
b6f8c7c4 13067const char *md_shortopts = "kVQ:sqnO::";
252b5132 13068#else
b6f8c7c4 13069const char *md_shortopts = "qnO::";
252b5132 13070#endif
6e0b89ee 13071
3e73aa7c 13072#define OPTION_32 (OPTION_MD_BASE + 0)
b3b91714
AM
13073#define OPTION_64 (OPTION_MD_BASE + 1)
13074#define OPTION_DIVIDE (OPTION_MD_BASE + 2)
9103f4f4
L
13075#define OPTION_MARCH (OPTION_MD_BASE + 3)
13076#define OPTION_MTUNE (OPTION_MD_BASE + 4)
1efbbeb4
L
13077#define OPTION_MMNEMONIC (OPTION_MD_BASE + 5)
13078#define OPTION_MSYNTAX (OPTION_MD_BASE + 6)
13079#define OPTION_MINDEX_REG (OPTION_MD_BASE + 7)
13080#define OPTION_MNAKED_REG (OPTION_MD_BASE + 8)
bd5dea88 13081#define OPTION_MRELAX_RELOCATIONS (OPTION_MD_BASE + 9)
c0f3af97 13082#define OPTION_MSSE2AVX (OPTION_MD_BASE + 10)
daf50ae7 13083#define OPTION_MSSE_CHECK (OPTION_MD_BASE + 11)
7bab8ab5
JB
13084#define OPTION_MOPERAND_CHECK (OPTION_MD_BASE + 12)
13085#define OPTION_MAVXSCALAR (OPTION_MD_BASE + 13)
13086#define OPTION_X32 (OPTION_MD_BASE + 14)
7e8b059b 13087#define OPTION_MADD_BND_PREFIX (OPTION_MD_BASE + 15)
43234a1e
L
13088#define OPTION_MEVEXLIG (OPTION_MD_BASE + 16)
13089#define OPTION_MEVEXWIG (OPTION_MD_BASE + 17)
167ad85b 13090#define OPTION_MBIG_OBJ (OPTION_MD_BASE + 18)
d1982f93 13091#define OPTION_MOMIT_LOCK_PREFIX (OPTION_MD_BASE + 19)
d3d3c6db 13092#define OPTION_MEVEXRCIG (OPTION_MD_BASE + 20)
8dcea932 13093#define OPTION_MSHARED (OPTION_MD_BASE + 21)
5db04b09
L
13094#define OPTION_MAMD64 (OPTION_MD_BASE + 22)
13095#define OPTION_MINTEL64 (OPTION_MD_BASE + 23)
e4e00185 13096#define OPTION_MFENCE_AS_LOCK_ADD (OPTION_MD_BASE + 24)
b4a3a7b4 13097#define OPTION_X86_USED_NOTE (OPTION_MD_BASE + 25)
03751133 13098#define OPTION_MVEXWIG (OPTION_MD_BASE + 26)
e379e5f3
L
13099#define OPTION_MALIGN_BRANCH_BOUNDARY (OPTION_MD_BASE + 27)
13100#define OPTION_MALIGN_BRANCH_PREFIX_SIZE (OPTION_MD_BASE + 28)
13101#define OPTION_MALIGN_BRANCH (OPTION_MD_BASE + 29)
76cf450b 13102#define OPTION_MBRANCHES_WITH_32B_BOUNDARIES (OPTION_MD_BASE + 30)
ae531041
L
13103#define OPTION_MLFENCE_AFTER_LOAD (OPTION_MD_BASE + 31)
13104#define OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH (OPTION_MD_BASE + 32)
13105#define OPTION_MLFENCE_BEFORE_RET (OPTION_MD_BASE + 33)
c8480b58 13106#define OPTION_MUSE_UNALIGNED_VECTOR_MOVE (OPTION_MD_BASE + 34)
b3b91714 13107
99ad8390
NC
13108struct option md_longopts[] =
13109{
3e73aa7c 13110 {"32", no_argument, NULL, OPTION_32},
321098a5 13111#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 13112 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c 13113 {"64", no_argument, NULL, OPTION_64},
351f65ca
L
13114#endif
13115#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 13116 {"x32", no_argument, NULL, OPTION_X32},
8dcea932 13117 {"mshared", no_argument, NULL, OPTION_MSHARED},
b4a3a7b4 13118 {"mx86-used-note", required_argument, NULL, OPTION_X86_USED_NOTE},
6e0b89ee 13119#endif
b3b91714 13120 {"divide", no_argument, NULL, OPTION_DIVIDE},
9103f4f4
L
13121 {"march", required_argument, NULL, OPTION_MARCH},
13122 {"mtune", required_argument, NULL, OPTION_MTUNE},
1efbbeb4
L
13123 {"mmnemonic", required_argument, NULL, OPTION_MMNEMONIC},
13124 {"msyntax", required_argument, NULL, OPTION_MSYNTAX},
13125 {"mindex-reg", no_argument, NULL, OPTION_MINDEX_REG},
13126 {"mnaked-reg", no_argument, NULL, OPTION_MNAKED_REG},
c0f3af97 13127 {"msse2avx", no_argument, NULL, OPTION_MSSE2AVX},
c8480b58 13128 {"muse-unaligned-vector-move", no_argument, NULL, OPTION_MUSE_UNALIGNED_VECTOR_MOVE},
daf50ae7 13129 {"msse-check", required_argument, NULL, OPTION_MSSE_CHECK},
7bab8ab5 13130 {"moperand-check", required_argument, NULL, OPTION_MOPERAND_CHECK},
539f890d 13131 {"mavxscalar", required_argument, NULL, OPTION_MAVXSCALAR},
03751133 13132 {"mvexwig", required_argument, NULL, OPTION_MVEXWIG},
7e8b059b 13133 {"madd-bnd-prefix", no_argument, NULL, OPTION_MADD_BND_PREFIX},
43234a1e
L
13134 {"mevexlig", required_argument, NULL, OPTION_MEVEXLIG},
13135 {"mevexwig", required_argument, NULL, OPTION_MEVEXWIG},
167ad85b
TG
13136# if defined (TE_PE) || defined (TE_PEP)
13137 {"mbig-obj", no_argument, NULL, OPTION_MBIG_OBJ},
13138#endif
d1982f93 13139 {"momit-lock-prefix", required_argument, NULL, OPTION_MOMIT_LOCK_PREFIX},
e4e00185 13140 {"mfence-as-lock-add", required_argument, NULL, OPTION_MFENCE_AS_LOCK_ADD},
0cb4071e 13141 {"mrelax-relocations", required_argument, NULL, OPTION_MRELAX_RELOCATIONS},
d3d3c6db 13142 {"mevexrcig", required_argument, NULL, OPTION_MEVEXRCIG},
e379e5f3
L
13143 {"malign-branch-boundary", required_argument, NULL, OPTION_MALIGN_BRANCH_BOUNDARY},
13144 {"malign-branch-prefix-size", required_argument, NULL, OPTION_MALIGN_BRANCH_PREFIX_SIZE},
13145 {"malign-branch", required_argument, NULL, OPTION_MALIGN_BRANCH},
76cf450b 13146 {"mbranches-within-32B-boundaries", no_argument, NULL, OPTION_MBRANCHES_WITH_32B_BOUNDARIES},
ae531041
L
13147 {"mlfence-after-load", required_argument, NULL, OPTION_MLFENCE_AFTER_LOAD},
13148 {"mlfence-before-indirect-branch", required_argument, NULL,
13149 OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH},
13150 {"mlfence-before-ret", required_argument, NULL, OPTION_MLFENCE_BEFORE_RET},
5db04b09
L
13151 {"mamd64", no_argument, NULL, OPTION_MAMD64},
13152 {"mintel64", no_argument, NULL, OPTION_MINTEL64},
252b5132
RH
13153 {NULL, no_argument, NULL, 0}
13154};
13155size_t md_longopts_size = sizeof (md_longopts);
13156
13157int
17b9d67d 13158md_parse_option (int c, const char *arg)
252b5132 13159{
91d6fa6a 13160 unsigned int j;
e379e5f3 13161 char *arch, *next, *saved, *type;
9103f4f4 13162
252b5132
RH
13163 switch (c)
13164 {
12b55ccc
L
13165 case 'n':
13166 optimize_align_code = 0;
13167 break;
13168
a38cf1db
AM
13169 case 'q':
13170 quiet_warnings = 1;
252b5132
RH
13171 break;
13172
13173#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
a38cf1db
AM
13174 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
13175 should be emitted or not. FIXME: Not implemented. */
13176 case 'Q':
d4693039
JB
13177 if ((arg[0] != 'y' && arg[0] != 'n') || arg[1])
13178 return 0;
252b5132
RH
13179 break;
13180
13181 /* -V: SVR4 argument to print version ID. */
13182 case 'V':
13183 print_version_id ();
13184 break;
13185
a38cf1db
AM
13186 /* -k: Ignore for FreeBSD compatibility. */
13187 case 'k':
252b5132 13188 break;
4cc782b5
ILT
13189
13190 case 's':
13191 /* -s: On i386 Solaris, this tells the native assembler to use
29b0f896 13192 .stab instead of .stab.excl. We always use .stab anyhow. */
4cc782b5 13193 break;
8dcea932
L
13194
13195 case OPTION_MSHARED:
13196 shared = 1;
13197 break;
b4a3a7b4
L
13198
13199 case OPTION_X86_USED_NOTE:
13200 if (strcasecmp (arg, "yes") == 0)
13201 x86_used_note = 1;
13202 else if (strcasecmp (arg, "no") == 0)
13203 x86_used_note = 0;
13204 else
13205 as_fatal (_("invalid -mx86-used-note= option: `%s'"), arg);
13206 break;
13207
13208
99ad8390 13209#endif
321098a5 13210#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 13211 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c
JH
13212 case OPTION_64:
13213 {
13214 const char **list, **l;
13215
3e73aa7c
JH
13216 list = bfd_target_list ();
13217 for (l = list; *l != NULL; l++)
08dedd66 13218 if (startswith (*l, "elf64-x86-64")
99ad8390
NC
13219 || strcmp (*l, "coff-x86-64") == 0
13220 || strcmp (*l, "pe-x86-64") == 0
d382c579
TG
13221 || strcmp (*l, "pei-x86-64") == 0
13222 || strcmp (*l, "mach-o-x86-64") == 0)
6e0b89ee
AM
13223 {
13224 default_arch = "x86_64";
13225 break;
13226 }
3e73aa7c 13227 if (*l == NULL)
2b5d6a91 13228 as_fatal (_("no compiled in support for x86_64"));
3e73aa7c
JH
13229 free (list);
13230 }
13231 break;
13232#endif
252b5132 13233
351f65ca 13234#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 13235 case OPTION_X32:
351f65ca
L
13236 if (IS_ELF)
13237 {
13238 const char **list, **l;
13239
13240 list = bfd_target_list ();
13241 for (l = list; *l != NULL; l++)
08dedd66 13242 if (startswith (*l, "elf32-x86-64"))
351f65ca
L
13243 {
13244 default_arch = "x86_64:32";
13245 break;
13246 }
13247 if (*l == NULL)
2b5d6a91 13248 as_fatal (_("no compiled in support for 32bit x86_64"));
351f65ca
L
13249 free (list);
13250 }
13251 else
13252 as_fatal (_("32bit x86_64 is only supported for ELF"));
13253 break;
13254#endif
13255
6e0b89ee
AM
13256 case OPTION_32:
13257 default_arch = "i386";
13258 break;
13259
b3b91714
AM
13260 case OPTION_DIVIDE:
13261#ifdef SVR4_COMMENT_CHARS
13262 {
13263 char *n, *t;
13264 const char *s;
13265
add39d23 13266 n = XNEWVEC (char, strlen (i386_comment_chars) + 1);
b3b91714
AM
13267 t = n;
13268 for (s = i386_comment_chars; *s != '\0'; s++)
13269 if (*s != '/')
13270 *t++ = *s;
13271 *t = '\0';
13272 i386_comment_chars = n;
13273 }
13274#endif
13275 break;
13276
9103f4f4 13277 case OPTION_MARCH:
293f5f65
L
13278 saved = xstrdup (arg);
13279 arch = saved;
13280 /* Allow -march=+nosse. */
13281 if (*arch == '+')
13282 arch++;
6305a203 13283 do
9103f4f4 13284 {
6305a203 13285 if (*arch == '.')
2b5d6a91 13286 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
13287 next = strchr (arch, '+');
13288 if (next)
13289 *next++ = '\0';
91d6fa6a 13290 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 13291 {
ae89daec
JB
13292 if (arch == saved && cpu_arch[j].type != PROCESSOR_NONE
13293 && strcmp (arch, cpu_arch[j].name) == 0)
ccc9c027 13294 {
6305a203 13295 /* Processor. */
ae89daec 13296 if (! cpu_arch[j].enable.bitfield.cpui386)
1ded5609
JB
13297 continue;
13298
91d6fa6a 13299 cpu_arch_name = cpu_arch[j].name;
d92c7521 13300 free (cpu_sub_arch_name);
6305a203 13301 cpu_sub_arch_name = NULL;
ae89daec 13302 cpu_arch_flags = cpu_arch[j].enable;
91d6fa6a 13303 cpu_arch_isa = cpu_arch[j].type;
ae89daec 13304 cpu_arch_isa_flags = cpu_arch[j].enable;
6305a203
L
13305 if (!cpu_arch_tune_set)
13306 {
13307 cpu_arch_tune = cpu_arch_isa;
13308 cpu_arch_tune_flags = cpu_arch_isa_flags;
13309 }
13310 break;
13311 }
ae89daec
JB
13312 else if (cpu_arch[j].type == PROCESSOR_NONE
13313 && strcmp (arch, cpu_arch[j].name) == 0
13314 && !cpu_flags_all_zero (&cpu_arch[j].enable))
6305a203 13315 {
33eaf5de 13316 /* ISA extension. */
6305a203 13317 i386_cpu_flags flags;
309d3373 13318
293f5f65 13319 flags = cpu_flags_or (cpu_arch_flags,
ae89daec 13320 cpu_arch[j].enable);
81486035 13321
5b64d091 13322 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
6305a203 13323 {
ae89daec 13324 extend_cpu_sub_arch_name (arch);
6305a203 13325 cpu_arch_flags = flags;
a586129e 13326 cpu_arch_isa_flags = flags;
6305a203 13327 }
0089dace
L
13328 else
13329 cpu_arch_isa_flags
13330 = cpu_flags_or (cpu_arch_isa_flags,
ae89daec 13331 cpu_arch[j].enable);
6305a203 13332 break;
ccc9c027 13333 }
9103f4f4 13334 }
6305a203 13335
ae89daec 13336 if (j >= ARRAY_SIZE (cpu_arch) && startswith (arch, "no"))
293f5f65 13337 {
33eaf5de 13338 /* Disable an ISA extension. */
ae89daec
JB
13339 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13340 if (cpu_arch[j].type == PROCESSOR_NONE
13341 && strcmp (arch + 2, cpu_arch[j].name) == 0)
293f5f65
L
13342 {
13343 i386_cpu_flags flags;
13344
13345 flags = cpu_flags_and_not (cpu_arch_flags,
ae89daec 13346 cpu_arch[j].disable);
293f5f65
L
13347 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
13348 {
8180707f 13349 extend_cpu_sub_arch_name (arch);
293f5f65
L
13350 cpu_arch_flags = flags;
13351 cpu_arch_isa_flags = flags;
13352 }
13353 break;
13354 }
293f5f65
L
13355 }
13356
91d6fa6a 13357 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 13358 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
13359
13360 arch = next;
9103f4f4 13361 }
293f5f65
L
13362 while (next != NULL);
13363 free (saved);
9103f4f4
L
13364 break;
13365
13366 case OPTION_MTUNE:
13367 if (*arg == '.')
2b5d6a91 13368 as_fatal (_("invalid -mtune= option: `%s'"), arg);
91d6fa6a 13369 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 13370 {
ae89daec
JB
13371 if (cpu_arch[j].type != PROCESSOR_NONE
13372 && strcmp (arg, cpu_arch[j].name) == 0)
9103f4f4 13373 {
ccc9c027 13374 cpu_arch_tune_set = 1;
91d6fa6a 13375 cpu_arch_tune = cpu_arch [j].type;
ae89daec 13376 cpu_arch_tune_flags = cpu_arch[j].enable;
9103f4f4
L
13377 break;
13378 }
13379 }
91d6fa6a 13380 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 13381 as_fatal (_("invalid -mtune= option: `%s'"), arg);
9103f4f4
L
13382 break;
13383
1efbbeb4
L
13384 case OPTION_MMNEMONIC:
13385 if (strcasecmp (arg, "att") == 0)
13386 intel_mnemonic = 0;
13387 else if (strcasecmp (arg, "intel") == 0)
13388 intel_mnemonic = 1;
13389 else
2b5d6a91 13390 as_fatal (_("invalid -mmnemonic= option: `%s'"), arg);
1efbbeb4
L
13391 break;
13392
13393 case OPTION_MSYNTAX:
13394 if (strcasecmp (arg, "att") == 0)
13395 intel_syntax = 0;
13396 else if (strcasecmp (arg, "intel") == 0)
13397 intel_syntax = 1;
13398 else
2b5d6a91 13399 as_fatal (_("invalid -msyntax= option: `%s'"), arg);
1efbbeb4
L
13400 break;
13401
13402 case OPTION_MINDEX_REG:
13403 allow_index_reg = 1;
13404 break;
13405
13406 case OPTION_MNAKED_REG:
13407 allow_naked_reg = 1;
13408 break;
13409
c0f3af97
L
13410 case OPTION_MSSE2AVX:
13411 sse2avx = 1;
13412 break;
13413
c8480b58
L
13414 case OPTION_MUSE_UNALIGNED_VECTOR_MOVE:
13415 use_unaligned_vector_move = 1;
13416 break;
13417
daf50ae7
L
13418 case OPTION_MSSE_CHECK:
13419 if (strcasecmp (arg, "error") == 0)
7bab8ab5 13420 sse_check = check_error;
daf50ae7 13421 else if (strcasecmp (arg, "warning") == 0)
7bab8ab5 13422 sse_check = check_warning;
daf50ae7 13423 else if (strcasecmp (arg, "none") == 0)
7bab8ab5 13424 sse_check = check_none;
daf50ae7 13425 else
2b5d6a91 13426 as_fatal (_("invalid -msse-check= option: `%s'"), arg);
daf50ae7
L
13427 break;
13428
7bab8ab5
JB
13429 case OPTION_MOPERAND_CHECK:
13430 if (strcasecmp (arg, "error") == 0)
13431 operand_check = check_error;
13432 else if (strcasecmp (arg, "warning") == 0)
13433 operand_check = check_warning;
13434 else if (strcasecmp (arg, "none") == 0)
13435 operand_check = check_none;
13436 else
13437 as_fatal (_("invalid -moperand-check= option: `%s'"), arg);
13438 break;
13439
539f890d
L
13440 case OPTION_MAVXSCALAR:
13441 if (strcasecmp (arg, "128") == 0)
13442 avxscalar = vex128;
13443 else if (strcasecmp (arg, "256") == 0)
13444 avxscalar = vex256;
13445 else
2b5d6a91 13446 as_fatal (_("invalid -mavxscalar= option: `%s'"), arg);
539f890d
L
13447 break;
13448
03751133
L
13449 case OPTION_MVEXWIG:
13450 if (strcmp (arg, "0") == 0)
40c9c8de 13451 vexwig = vexw0;
03751133 13452 else if (strcmp (arg, "1") == 0)
40c9c8de 13453 vexwig = vexw1;
03751133
L
13454 else
13455 as_fatal (_("invalid -mvexwig= option: `%s'"), arg);
13456 break;
13457
7e8b059b
L
13458 case OPTION_MADD_BND_PREFIX:
13459 add_bnd_prefix = 1;
13460 break;
13461
43234a1e
L
13462 case OPTION_MEVEXLIG:
13463 if (strcmp (arg, "128") == 0)
13464 evexlig = evexl128;
13465 else if (strcmp (arg, "256") == 0)
13466 evexlig = evexl256;
13467 else if (strcmp (arg, "512") == 0)
13468 evexlig = evexl512;
13469 else
13470 as_fatal (_("invalid -mevexlig= option: `%s'"), arg);
13471 break;
13472
d3d3c6db
IT
13473 case OPTION_MEVEXRCIG:
13474 if (strcmp (arg, "rne") == 0)
13475 evexrcig = rne;
13476 else if (strcmp (arg, "rd") == 0)
13477 evexrcig = rd;
13478 else if (strcmp (arg, "ru") == 0)
13479 evexrcig = ru;
13480 else if (strcmp (arg, "rz") == 0)
13481 evexrcig = rz;
13482 else
13483 as_fatal (_("invalid -mevexrcig= option: `%s'"), arg);
13484 break;
13485
43234a1e
L
13486 case OPTION_MEVEXWIG:
13487 if (strcmp (arg, "0") == 0)
13488 evexwig = evexw0;
13489 else if (strcmp (arg, "1") == 0)
13490 evexwig = evexw1;
13491 else
13492 as_fatal (_("invalid -mevexwig= option: `%s'"), arg);
13493 break;
13494
167ad85b
TG
13495# if defined (TE_PE) || defined (TE_PEP)
13496 case OPTION_MBIG_OBJ:
13497 use_big_obj = 1;
13498 break;
13499#endif
13500
d1982f93 13501 case OPTION_MOMIT_LOCK_PREFIX:
d022bddd
IT
13502 if (strcasecmp (arg, "yes") == 0)
13503 omit_lock_prefix = 1;
13504 else if (strcasecmp (arg, "no") == 0)
13505 omit_lock_prefix = 0;
13506 else
13507 as_fatal (_("invalid -momit-lock-prefix= option: `%s'"), arg);
13508 break;
13509
e4e00185
AS
13510 case OPTION_MFENCE_AS_LOCK_ADD:
13511 if (strcasecmp (arg, "yes") == 0)
13512 avoid_fence = 1;
13513 else if (strcasecmp (arg, "no") == 0)
13514 avoid_fence = 0;
13515 else
13516 as_fatal (_("invalid -mfence-as-lock-add= option: `%s'"), arg);
13517 break;
13518
ae531041
L
13519 case OPTION_MLFENCE_AFTER_LOAD:
13520 if (strcasecmp (arg, "yes") == 0)
13521 lfence_after_load = 1;
13522 else if (strcasecmp (arg, "no") == 0)
13523 lfence_after_load = 0;
13524 else
13525 as_fatal (_("invalid -mlfence-after-load= option: `%s'"), arg);
13526 break;
13527
13528 case OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH:
13529 if (strcasecmp (arg, "all") == 0)
a09f656b 13530 {
13531 lfence_before_indirect_branch = lfence_branch_all;
13532 if (lfence_before_ret == lfence_before_ret_none)
13533 lfence_before_ret = lfence_before_ret_shl;
13534 }
ae531041
L
13535 else if (strcasecmp (arg, "memory") == 0)
13536 lfence_before_indirect_branch = lfence_branch_memory;
13537 else if (strcasecmp (arg, "register") == 0)
13538 lfence_before_indirect_branch = lfence_branch_register;
13539 else if (strcasecmp (arg, "none") == 0)
13540 lfence_before_indirect_branch = lfence_branch_none;
13541 else
13542 as_fatal (_("invalid -mlfence-before-indirect-branch= option: `%s'"),
13543 arg);
13544 break;
13545
13546 case OPTION_MLFENCE_BEFORE_RET:
13547 if (strcasecmp (arg, "or") == 0)
13548 lfence_before_ret = lfence_before_ret_or;
13549 else if (strcasecmp (arg, "not") == 0)
13550 lfence_before_ret = lfence_before_ret_not;
a09f656b 13551 else if (strcasecmp (arg, "shl") == 0 || strcasecmp (arg, "yes") == 0)
13552 lfence_before_ret = lfence_before_ret_shl;
ae531041
L
13553 else if (strcasecmp (arg, "none") == 0)
13554 lfence_before_ret = lfence_before_ret_none;
13555 else
13556 as_fatal (_("invalid -mlfence-before-ret= option: `%s'"),
13557 arg);
13558 break;
13559
0cb4071e
L
13560 case OPTION_MRELAX_RELOCATIONS:
13561 if (strcasecmp (arg, "yes") == 0)
13562 generate_relax_relocations = 1;
13563 else if (strcasecmp (arg, "no") == 0)
13564 generate_relax_relocations = 0;
13565 else
13566 as_fatal (_("invalid -mrelax-relocations= option: `%s'"), arg);
13567 break;
13568
e379e5f3
L
13569 case OPTION_MALIGN_BRANCH_BOUNDARY:
13570 {
13571 char *end;
13572 long int align = strtoul (arg, &end, 0);
13573 if (*end == '\0')
13574 {
13575 if (align == 0)
13576 {
13577 align_branch_power = 0;
13578 break;
13579 }
13580 else if (align >= 16)
13581 {
13582 int align_power;
13583 for (align_power = 0;
13584 (align & 1) == 0;
13585 align >>= 1, align_power++)
13586 continue;
13587 /* Limit alignment power to 31. */
13588 if (align == 1 && align_power < 32)
13589 {
13590 align_branch_power = align_power;
13591 break;
13592 }
13593 }
13594 }
13595 as_fatal (_("invalid -malign-branch-boundary= value: %s"), arg);
13596 }
13597 break;
13598
13599 case OPTION_MALIGN_BRANCH_PREFIX_SIZE:
13600 {
13601 char *end;
13602 int align = strtoul (arg, &end, 0);
13603 /* Some processors only support 5 prefixes. */
13604 if (*end == '\0' && align >= 0 && align < 6)
13605 {
13606 align_branch_prefix_size = align;
13607 break;
13608 }
13609 as_fatal (_("invalid -malign-branch-prefix-size= value: %s"),
13610 arg);
13611 }
13612 break;
13613
13614 case OPTION_MALIGN_BRANCH:
13615 align_branch = 0;
13616 saved = xstrdup (arg);
13617 type = saved;
13618 do
13619 {
13620 next = strchr (type, '+');
13621 if (next)
13622 *next++ = '\0';
13623 if (strcasecmp (type, "jcc") == 0)
13624 align_branch |= align_branch_jcc_bit;
13625 else if (strcasecmp (type, "fused") == 0)
13626 align_branch |= align_branch_fused_bit;
13627 else if (strcasecmp (type, "jmp") == 0)
13628 align_branch |= align_branch_jmp_bit;
13629 else if (strcasecmp (type, "call") == 0)
13630 align_branch |= align_branch_call_bit;
13631 else if (strcasecmp (type, "ret") == 0)
13632 align_branch |= align_branch_ret_bit;
13633 else if (strcasecmp (type, "indirect") == 0)
13634 align_branch |= align_branch_indirect_bit;
13635 else
13636 as_fatal (_("invalid -malign-branch= option: `%s'"), arg);
13637 type = next;
13638 }
13639 while (next != NULL);
13640 free (saved);
13641 break;
13642
76cf450b
L
13643 case OPTION_MBRANCHES_WITH_32B_BOUNDARIES:
13644 align_branch_power = 5;
13645 align_branch_prefix_size = 5;
13646 align_branch = (align_branch_jcc_bit
13647 | align_branch_fused_bit
13648 | align_branch_jmp_bit);
13649 break;
13650
5db04b09 13651 case OPTION_MAMD64:
4b5aaf5f 13652 isa64 = amd64;
5db04b09
L
13653 break;
13654
13655 case OPTION_MINTEL64:
4b5aaf5f 13656 isa64 = intel64;
5db04b09
L
13657 break;
13658
b6f8c7c4
L
13659 case 'O':
13660 if (arg == NULL)
13661 {
13662 optimize = 1;
13663 /* Turn off -Os. */
13664 optimize_for_space = 0;
13665 }
13666 else if (*arg == 's')
13667 {
13668 optimize_for_space = 1;
13669 /* Turn on all encoding optimizations. */
41fd2579 13670 optimize = INT_MAX;
b6f8c7c4
L
13671 }
13672 else
13673 {
13674 optimize = atoi (arg);
13675 /* Turn off -Os. */
13676 optimize_for_space = 0;
13677 }
13678 break;
13679
252b5132
RH
13680 default:
13681 return 0;
13682 }
13683 return 1;
13684}
13685
8a2c8fef
L
13686#define MESSAGE_TEMPLATE \
13687" "
13688
293f5f65
L
13689static char *
13690output_message (FILE *stream, char *p, char *message, char *start,
13691 int *left_p, const char *name, int len)
13692{
13693 int size = sizeof (MESSAGE_TEMPLATE);
13694 int left = *left_p;
13695
13696 /* Reserve 2 spaces for ", " or ",\0" */
13697 left -= len + 2;
13698
13699 /* Check if there is any room. */
13700 if (left >= 0)
13701 {
13702 if (p != start)
13703 {
13704 *p++ = ',';
13705 *p++ = ' ';
13706 }
13707 p = mempcpy (p, name, len);
13708 }
13709 else
13710 {
13711 /* Output the current message now and start a new one. */
13712 *p++ = ',';
13713 *p = '\0';
13714 fprintf (stream, "%s\n", message);
13715 p = start;
13716 left = size - (start - message) - len - 2;
13717
13718 gas_assert (left >= 0);
13719
13720 p = mempcpy (p, name, len);
13721 }
13722
13723 *left_p = left;
13724 return p;
13725}
13726
8a2c8fef 13727static void
1ded5609 13728show_arch (FILE *stream, int ext, int check)
8a2c8fef
L
13729{
13730 static char message[] = MESSAGE_TEMPLATE;
13731 char *start = message + 27;
13732 char *p;
13733 int size = sizeof (MESSAGE_TEMPLATE);
13734 int left;
13735 const char *name;
13736 int len;
13737 unsigned int j;
13738
13739 p = start;
13740 left = size - (start - message);
3ce2ebcf
JB
13741
13742 if (!ext && check)
13743 {
13744 p = output_message (stream, p, message, start, &left,
13745 STRING_COMMA_LEN ("default"));
f68697e8
JB
13746 p = output_message (stream, p, message, start, &left,
13747 STRING_COMMA_LEN ("push"));
13748 p = output_message (stream, p, message, start, &left,
13749 STRING_COMMA_LEN ("pop"));
3ce2ebcf
JB
13750 }
13751
8a2c8fef
L
13752 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13753 {
13754 /* Should it be skipped? */
13755 if (cpu_arch [j].skip)
13756 continue;
13757
13758 name = cpu_arch [j].name;
13759 len = cpu_arch [j].len;
ae89daec 13760 if (cpu_arch[j].type == PROCESSOR_NONE)
8a2c8fef
L
13761 {
13762 /* It is an extension. Skip if we aren't asked to show it. */
ae89daec 13763 if (!ext || cpu_flags_all_zero (&cpu_arch[j].enable))
8a2c8fef
L
13764 continue;
13765 }
13766 else if (ext)
13767 {
13768 /* It is an processor. Skip if we show only extension. */
13769 continue;
13770 }
ae89daec 13771 else if (check && ! cpu_arch[j].enable.bitfield.cpui386)
1ded5609
JB
13772 {
13773 /* It is an impossible processor - skip. */
13774 continue;
13775 }
8a2c8fef 13776
293f5f65 13777 p = output_message (stream, p, message, start, &left, name, len);
8a2c8fef
L
13778 }
13779
293f5f65
L
13780 /* Display disabled extensions. */
13781 if (ext)
ae89daec 13782 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
293f5f65 13783 {
ae89daec
JB
13784 char *str;
13785
13786 if (cpu_arch[j].type != PROCESSOR_NONE
13787 || !cpu_flags_all_zero (&cpu_arch[j].enable))
13788 continue;
13789 str = xasprintf ("no%s", cpu_arch[j].name);
13790 p = output_message (stream, p, message, start, &left, str,
13791 strlen (str));
13792 free (str);
293f5f65
L
13793 }
13794
8a2c8fef
L
13795 *p = '\0';
13796 fprintf (stream, "%s\n", message);
13797}
13798
252b5132 13799void
8a2c8fef 13800md_show_usage (FILE *stream)
252b5132 13801{
4cc782b5
ILT
13802#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13803 fprintf (stream, _("\
d4693039 13804 -Qy, -Qn ignored\n\
a38cf1db 13805 -V print assembler version number\n\
b3b91714
AM
13806 -k ignored\n"));
13807#endif
13808 fprintf (stream, _("\
12b55ccc 13809 -n Do not optimize code alignment\n\
b3b91714
AM
13810 -q quieten some warnings\n"));
13811#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13812 fprintf (stream, _("\
a38cf1db 13813 -s ignored\n"));
b3b91714 13814#endif
b00af7c8
JB
13815#ifdef BFD64
13816# if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13817 fprintf (stream, _("\
13818 --32/--64/--x32 generate 32bit/64bit/x32 object\n"));
13819# elif defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O)
751d281c 13820 fprintf (stream, _("\
b00af7c8
JB
13821 --32/--64 generate 32bit/64bit object\n"));
13822# endif
751d281c 13823#endif
b3b91714
AM
13824#ifdef SVR4_COMMENT_CHARS
13825 fprintf (stream, _("\
13826 --divide do not treat `/' as a comment character\n"));
a38cf1db
AM
13827#else
13828 fprintf (stream, _("\
b3b91714 13829 --divide ignored\n"));
4cc782b5 13830#endif
9103f4f4 13831 fprintf (stream, _("\
6305a203 13832 -march=CPU[,+EXTENSION...]\n\
8a2c8fef 13833 generate code for CPU and EXTENSION, CPU is one of:\n"));
1ded5609 13834 show_arch (stream, 0, 1);
8a2c8fef 13835 fprintf (stream, _("\
ae89daec 13836 EXTENSION is combination of (possibly \"no\"-prefixed):\n"));
1ded5609 13837 show_arch (stream, 1, 0);
6305a203 13838 fprintf (stream, _("\
8a2c8fef 13839 -mtune=CPU optimize for CPU, CPU is one of:\n"));
1ded5609 13840 show_arch (stream, 0, 0);
ba104c83 13841 fprintf (stream, _("\
c0f3af97
L
13842 -msse2avx encode SSE instructions with VEX prefix\n"));
13843 fprintf (stream, _("\
c8480b58
L
13844 -muse-unaligned-vector-move\n\
13845 encode aligned vector move as unaligned vector move\n"));
13846 fprintf (stream, _("\
7c5c05ef 13847 -msse-check=[none|error|warning] (default: warning)\n\
daf50ae7
L
13848 check SSE instructions\n"));
13849 fprintf (stream, _("\
7c5c05ef 13850 -moperand-check=[none|error|warning] (default: warning)\n\
7bab8ab5
JB
13851 check operand combinations for validity\n"));
13852 fprintf (stream, _("\
7c5c05ef
L
13853 -mavxscalar=[128|256] (default: 128)\n\
13854 encode scalar AVX instructions with specific vector\n\
539f890d
L
13855 length\n"));
13856 fprintf (stream, _("\
03751133
L
13857 -mvexwig=[0|1] (default: 0)\n\
13858 encode VEX instructions with specific VEX.W value\n\
13859 for VEX.W bit ignored instructions\n"));
13860 fprintf (stream, _("\
7c5c05ef
L
13861 -mevexlig=[128|256|512] (default: 128)\n\
13862 encode scalar EVEX instructions with specific vector\n\
43234a1e
L
13863 length\n"));
13864 fprintf (stream, _("\
7c5c05ef
L
13865 -mevexwig=[0|1] (default: 0)\n\
13866 encode EVEX instructions with specific EVEX.W value\n\
43234a1e
L
13867 for EVEX.W bit ignored instructions\n"));
13868 fprintf (stream, _("\
7c5c05ef 13869 -mevexrcig=[rne|rd|ru|rz] (default: rne)\n\
d3d3c6db
IT
13870 encode EVEX instructions with specific EVEX.RC value\n\
13871 for SAE-only ignored instructions\n"));
13872 fprintf (stream, _("\
7c5c05ef
L
13873 -mmnemonic=[att|intel] "));
13874 if (SYSV386_COMPAT)
13875 fprintf (stream, _("(default: att)\n"));
13876 else
13877 fprintf (stream, _("(default: intel)\n"));
13878 fprintf (stream, _("\
13879 use AT&T/Intel mnemonic\n"));
ba104c83 13880 fprintf (stream, _("\
7c5c05ef
L
13881 -msyntax=[att|intel] (default: att)\n\
13882 use AT&T/Intel syntax\n"));
ba104c83
L
13883 fprintf (stream, _("\
13884 -mindex-reg support pseudo index registers\n"));
13885 fprintf (stream, _("\
13886 -mnaked-reg don't require `%%' prefix for registers\n"));
13887 fprintf (stream, _("\
7e8b059b 13888 -madd-bnd-prefix add BND prefix for all valid branches\n"));
b4a3a7b4 13889#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8dcea932
L
13890 fprintf (stream, _("\
13891 -mshared disable branch optimization for shared code\n"));
b4a3a7b4
L
13892 fprintf (stream, _("\
13893 -mx86-used-note=[no|yes] "));
13894 if (DEFAULT_X86_USED_NOTE)
13895 fprintf (stream, _("(default: yes)\n"));
13896 else
13897 fprintf (stream, _("(default: no)\n"));
13898 fprintf (stream, _("\
13899 generate x86 used ISA and feature properties\n"));
13900#endif
13901#if defined (TE_PE) || defined (TE_PEP)
167ad85b
TG
13902 fprintf (stream, _("\
13903 -mbig-obj generate big object files\n"));
13904#endif
d022bddd 13905 fprintf (stream, _("\
7c5c05ef 13906 -momit-lock-prefix=[no|yes] (default: no)\n\
d022bddd 13907 strip all lock prefixes\n"));
5db04b09 13908 fprintf (stream, _("\
7c5c05ef 13909 -mfence-as-lock-add=[no|yes] (default: no)\n\
e4e00185
AS
13910 encode lfence, mfence and sfence as\n\
13911 lock addl $0x0, (%%{re}sp)\n"));
13912 fprintf (stream, _("\
7c5c05ef
L
13913 -mrelax-relocations=[no|yes] "));
13914 if (DEFAULT_GENERATE_X86_RELAX_RELOCATIONS)
13915 fprintf (stream, _("(default: yes)\n"));
13916 else
13917 fprintf (stream, _("(default: no)\n"));
13918 fprintf (stream, _("\
0cb4071e
L
13919 generate relax relocations\n"));
13920 fprintf (stream, _("\
e379e5f3
L
13921 -malign-branch-boundary=NUM (default: 0)\n\
13922 align branches within NUM byte boundary\n"));
13923 fprintf (stream, _("\
13924 -malign-branch=TYPE[+TYPE...] (default: jcc+fused+jmp)\n\
13925 TYPE is combination of jcc, fused, jmp, call, ret,\n\
13926 indirect\n\
13927 specify types of branches to align\n"));
13928 fprintf (stream, _("\
13929 -malign-branch-prefix-size=NUM (default: 5)\n\
13930 align branches with NUM prefixes per instruction\n"));
13931 fprintf (stream, _("\
76cf450b
L
13932 -mbranches-within-32B-boundaries\n\
13933 align branches within 32 byte boundary\n"));
13934 fprintf (stream, _("\
ae531041
L
13935 -mlfence-after-load=[no|yes] (default: no)\n\
13936 generate lfence after load\n"));
13937 fprintf (stream, _("\
13938 -mlfence-before-indirect-branch=[none|all|register|memory] (default: none)\n\
13939 generate lfence before indirect near branch\n"));
13940 fprintf (stream, _("\
a09f656b 13941 -mlfence-before-ret=[none|or|not|shl|yes] (default: none)\n\
ae531041
L
13942 generate lfence before ret\n"));
13943 fprintf (stream, _("\
7c5c05ef 13944 -mamd64 accept only AMD64 ISA [default]\n"));
5db04b09
L
13945 fprintf (stream, _("\
13946 -mintel64 accept only Intel64 ISA\n"));
252b5132
RH
13947}
13948
3e73aa7c 13949#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
321098a5 13950 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
e57f8c65 13951 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
252b5132
RH
13952
13953/* Pick the target format to use. */
13954
47926f60 13955const char *
e3bb37b5 13956i386_target_format (void)
252b5132 13957{
d34049e8 13958 if (startswith (default_arch, "x86_64"))
351f65ca
L
13959 {
13960 update_code_flag (CODE_64BIT, 1);
13961 if (default_arch[6] == '\0')
7f56bc95 13962 x86_elf_abi = X86_64_ABI;
351f65ca 13963 else
7f56bc95 13964 x86_elf_abi = X86_64_X32_ABI;
351f65ca 13965 }
3e73aa7c 13966 else if (!strcmp (default_arch, "i386"))
78f12dd3 13967 update_code_flag (CODE_32BIT, 1);
5197d474
L
13968 else if (!strcmp (default_arch, "iamcu"))
13969 {
13970 update_code_flag (CODE_32BIT, 1);
13971 if (cpu_arch_isa == PROCESSOR_UNKNOWN)
13972 {
13973 static const i386_cpu_flags iamcu_flags = CPU_IAMCU_FLAGS;
13974 cpu_arch_name = "iamcu";
d92c7521 13975 free (cpu_sub_arch_name);
5197d474
L
13976 cpu_sub_arch_name = NULL;
13977 cpu_arch_flags = iamcu_flags;
13978 cpu_arch_isa = PROCESSOR_IAMCU;
13979 cpu_arch_isa_flags = iamcu_flags;
13980 if (!cpu_arch_tune_set)
13981 {
13982 cpu_arch_tune = cpu_arch_isa;
13983 cpu_arch_tune_flags = cpu_arch_isa_flags;
13984 }
13985 }
8d471ec1 13986 else if (cpu_arch_isa != PROCESSOR_IAMCU)
5197d474
L
13987 as_fatal (_("Intel MCU doesn't support `%s' architecture"),
13988 cpu_arch_name);
13989 }
3e73aa7c 13990 else
2b5d6a91 13991 as_fatal (_("unknown architecture"));
89507696
JB
13992
13993 if (cpu_flags_all_zero (&cpu_arch_isa_flags))
ae89daec 13994 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
89507696 13995 if (cpu_flags_all_zero (&cpu_arch_tune_flags))
ae89daec 13996 cpu_arch_tune_flags = cpu_arch[flag_code == CODE_64BIT].enable;
89507696 13997
252b5132
RH
13998 switch (OUTPUT_FLAVOR)
13999 {
9384f2ff 14000#if defined (OBJ_MAYBE_AOUT) || defined (OBJ_AOUT)
4c63da97 14001 case bfd_target_aout_flavour:
47926f60 14002 return AOUT_TARGET_FORMAT;
4c63da97 14003#endif
9384f2ff
AM
14004#if defined (OBJ_MAYBE_COFF) || defined (OBJ_COFF)
14005# if defined (TE_PE) || defined (TE_PEP)
14006 case bfd_target_coff_flavour:
167ad85b 14007 if (flag_code == CODE_64BIT)
eb19308f
JB
14008 {
14009 object_64bit = 1;
14010 return use_big_obj ? "pe-bigobj-x86-64" : "pe-x86-64";
14011 }
14012 return use_big_obj ? "pe-bigobj-i386" : "pe-i386";
9384f2ff 14013# elif defined (TE_GO32)
0561d57c
JK
14014 case bfd_target_coff_flavour:
14015 return "coff-go32";
9384f2ff 14016# else
252b5132
RH
14017 case bfd_target_coff_flavour:
14018 return "coff-i386";
9384f2ff 14019# endif
4c63da97 14020#endif
3e73aa7c 14021#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
252b5132 14022 case bfd_target_elf_flavour:
3e73aa7c 14023 {
351f65ca
L
14024 const char *format;
14025
14026 switch (x86_elf_abi)
4fa24527 14027 {
351f65ca
L
14028 default:
14029 format = ELF_TARGET_FORMAT;
e379e5f3
L
14030#ifndef TE_SOLARIS
14031 tls_get_addr = "___tls_get_addr";
14032#endif
351f65ca 14033 break;
7f56bc95 14034 case X86_64_ABI:
351f65ca 14035 use_rela_relocations = 1;
4fa24527 14036 object_64bit = 1;
e379e5f3
L
14037#ifndef TE_SOLARIS
14038 tls_get_addr = "__tls_get_addr";
14039#endif
351f65ca
L
14040 format = ELF_TARGET_FORMAT64;
14041 break;
7f56bc95 14042 case X86_64_X32_ABI:
4fa24527 14043 use_rela_relocations = 1;
351f65ca 14044 object_64bit = 1;
e379e5f3
L
14045#ifndef TE_SOLARIS
14046 tls_get_addr = "__tls_get_addr";
14047#endif
862be3fb 14048 disallow_64bit_reloc = 1;
351f65ca
L
14049 format = ELF_TARGET_FORMAT32;
14050 break;
4fa24527 14051 }
c085ab00 14052 if (cpu_arch_isa == PROCESSOR_IAMCU)
81486035
L
14053 {
14054 if (x86_elf_abi != I386_ABI)
14055 as_fatal (_("Intel MCU is 32bit only"));
14056 return ELF_TARGET_IAMCU_FORMAT;
14057 }
8a9036a4 14058 else
351f65ca 14059 return format;
3e73aa7c 14060 }
e57f8c65
TG
14061#endif
14062#if defined (OBJ_MACH_O)
14063 case bfd_target_mach_o_flavour:
d382c579
TG
14064 if (flag_code == CODE_64BIT)
14065 {
14066 use_rela_relocations = 1;
14067 object_64bit = 1;
14068 return "mach-o-x86-64";
14069 }
14070 else
14071 return "mach-o-i386";
4c63da97 14072#endif
252b5132
RH
14073 default:
14074 abort ();
14075 return NULL;
14076 }
14077}
14078
47926f60 14079#endif /* OBJ_MAYBE_ more than one */
252b5132 14080\f
252b5132 14081symbolS *
7016a5d5 14082md_undefined_symbol (char *name)
252b5132 14083{
18dc2407
ILT
14084 if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
14085 && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
14086 && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
14087 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
24eab124
AM
14088 {
14089 if (!GOT_symbol)
14090 {
14091 if (symbol_find (name))
14092 as_bad (_("GOT already in symbol table"));
14093 GOT_symbol = symbol_new (name, undefined_section,
e01e1cee 14094 &zero_address_frag, 0);
24eab124
AM
14095 };
14096 return GOT_symbol;
14097 }
252b5132
RH
14098 return 0;
14099}
14100
14101/* Round up a section size to the appropriate boundary. */
47926f60 14102
252b5132 14103valueT
7016a5d5 14104md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
252b5132 14105{
4c63da97
AM
14106#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
14107 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
14108 {
14109 /* For a.out, force the section size to be aligned. If we don't do
14110 this, BFD will align it for us, but it will not write out the
14111 final bytes of the section. This may be a bug in BFD, but it is
14112 easier to fix it here since that is how the other a.out targets
14113 work. */
14114 int align;
14115
fd361982 14116 align = bfd_section_alignment (segment);
8d3842cd 14117 size = ((size + (1 << align) - 1) & (-((valueT) 1 << align)));
4c63da97 14118 }
252b5132
RH
14119#endif
14120
14121 return size;
14122}
14123
14124/* On the i386, PC-relative offsets are relative to the start of the
14125 next instruction. That is, the address of the offset, plus its
14126 size, since the offset is always the last part of the insn. */
14127
14128long
e3bb37b5 14129md_pcrel_from (fixS *fixP)
252b5132
RH
14130{
14131 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
14132}
14133
14134#ifndef I386COFF
14135
14136static void
e3bb37b5 14137s_bss (int ignore ATTRIBUTE_UNUSED)
252b5132 14138{
29b0f896 14139 int temp;
252b5132 14140
8a75718c
JB
14141#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14142 if (IS_ELF)
14143 obj_elf_section_change_hook ();
14144#endif
252b5132
RH
14145 temp = get_absolute_expression ();
14146 subseg_set (bss_section, (subsegT) temp);
14147 demand_empty_rest_of_line ();
14148}
14149
14150#endif
14151
e379e5f3
L
14152/* Remember constant directive. */
14153
14154void
14155i386_cons_align (int ignore ATTRIBUTE_UNUSED)
14156{
14157 if (last_insn.kind != last_insn_directive
14158 && (bfd_section_flags (now_seg) & SEC_CODE))
14159 {
14160 last_insn.seg = now_seg;
14161 last_insn.kind = last_insn_directive;
14162 last_insn.name = "constant directive";
14163 last_insn.file = as_where (&last_insn.line);
ae531041
L
14164 if (lfence_before_ret != lfence_before_ret_none)
14165 {
14166 if (lfence_before_indirect_branch != lfence_branch_none)
14167 as_warn (_("constant directive skips -mlfence-before-ret "
14168 "and -mlfence-before-indirect-branch"));
14169 else
14170 as_warn (_("constant directive skips -mlfence-before-ret"));
14171 }
14172 else if (lfence_before_indirect_branch != lfence_branch_none)
14173 as_warn (_("constant directive skips -mlfence-before-indirect-branch"));
e379e5f3
L
14174 }
14175}
14176
3abbafc2 14177int
e3bb37b5 14178i386_validate_fix (fixS *fixp)
252b5132 14179{
e52a16f2
JB
14180 if (fixp->fx_addsy && S_GET_SEGMENT(fixp->fx_addsy) == reg_section)
14181 {
14182 reloc_howto_type *howto;
14183
14184 howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
14185 as_bad_where (fixp->fx_file, fixp->fx_line,
14186 _("invalid %s relocation against register"),
14187 howto ? howto->name : "<unknown>");
14188 return 0;
14189 }
14190
3abbafc2
JB
14191#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14192 if (fixp->fx_r_type == BFD_RELOC_SIZE32
14193 || fixp->fx_r_type == BFD_RELOC_SIZE64)
14194 return IS_ELF && fixp->fx_addsy
14195 && (!S_IS_DEFINED (fixp->fx_addsy)
14196 || S_IS_EXTERNAL (fixp->fx_addsy));
14197#endif
14198
02a86693 14199 if (fixp->fx_subsy)
252b5132 14200 {
02a86693 14201 if (fixp->fx_subsy == GOT_symbol)
23df1078 14202 {
02a86693
L
14203 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
14204 {
14205 if (!object_64bit)
14206 abort ();
14207#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14208 if (fixp->fx_tcbit2)
56ceb5b5
L
14209 fixp->fx_r_type = (fixp->fx_tcbit
14210 ? BFD_RELOC_X86_64_REX_GOTPCRELX
14211 : BFD_RELOC_X86_64_GOTPCRELX);
02a86693
L
14212 else
14213#endif
14214 fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
14215 }
d6ab8113 14216 else
02a86693
L
14217 {
14218 if (!object_64bit)
14219 fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
14220 else
14221 fixp->fx_r_type = BFD_RELOC_X86_64_GOTOFF64;
14222 }
14223 fixp->fx_subsy = 0;
23df1078 14224 }
252b5132 14225 }
02a86693 14226#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2585b7a5 14227 else
02a86693 14228 {
2585b7a5
L
14229 /* NB: Commit 292676c1 resolved PLT32 reloc aganst local symbol
14230 to section. Since PLT32 relocation must be against symbols,
14231 turn such PLT32 relocation into PC32 relocation. */
14232 if (fixp->fx_addsy
14233 && (fixp->fx_r_type == BFD_RELOC_386_PLT32
14234 || fixp->fx_r_type == BFD_RELOC_X86_64_PLT32)
14235 && symbol_section_p (fixp->fx_addsy))
14236 fixp->fx_r_type = BFD_RELOC_32_PCREL;
14237 if (!object_64bit)
14238 {
14239 if (fixp->fx_r_type == BFD_RELOC_386_GOT32
14240 && fixp->fx_tcbit2)
14241 fixp->fx_r_type = BFD_RELOC_386_GOT32X;
14242 }
02a86693
L
14243 }
14244#endif
3abbafc2
JB
14245
14246 return 1;
252b5132
RH
14247}
14248
252b5132 14249arelent *
7016a5d5 14250tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
14251{
14252 arelent *rel;
14253 bfd_reloc_code_real_type code;
14254
14255 switch (fixp->fx_r_type)
14256 {
8ce3d284 14257#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3abbafc2
JB
14258 symbolS *sym;
14259
8fd4256d
L
14260 case BFD_RELOC_SIZE32:
14261 case BFD_RELOC_SIZE64:
3abbafc2
JB
14262 if (fixp->fx_addsy
14263 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))
14264 && (!fixp->fx_subsy
14265 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))))
14266 sym = fixp->fx_addsy;
14267 else if (fixp->fx_subsy
14268 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))
14269 && (!fixp->fx_addsy
14270 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))))
14271 sym = fixp->fx_subsy;
14272 else
14273 sym = NULL;
14274 if (IS_ELF && sym && S_IS_DEFINED (sym) && !S_IS_EXTERNAL (sym))
8fd4256d
L
14275 {
14276 /* Resolve size relocation against local symbol to size of
14277 the symbol plus addend. */
3abbafc2 14278 valueT value = S_GET_SIZE (sym);
44f87162 14279
3abbafc2
JB
14280 if (symbol_get_bfdsym (sym)->flags & BSF_SECTION_SYM)
14281 value = bfd_section_size (S_GET_SEGMENT (sym));
14282 if (sym == fixp->fx_subsy)
14283 {
14284 value = -value;
14285 if (fixp->fx_addsy)
14286 value += S_GET_VALUE (fixp->fx_addsy);
14287 }
14288 else if (fixp->fx_subsy)
14289 value -= S_GET_VALUE (fixp->fx_subsy);
44f87162 14290 value += fixp->fx_offset;
8fd4256d 14291 if (fixp->fx_r_type == BFD_RELOC_SIZE32
d965814f 14292 && object_64bit
8fd4256d
L
14293 && !fits_in_unsigned_long (value))
14294 as_bad_where (fixp->fx_file, fixp->fx_line,
14295 _("symbol size computation overflow"));
14296 fixp->fx_addsy = NULL;
14297 fixp->fx_subsy = NULL;
14298 md_apply_fix (fixp, (valueT *) &value, NULL);
14299 return NULL;
14300 }
3abbafc2
JB
14301 if (!fixp->fx_addsy || fixp->fx_subsy)
14302 {
14303 as_bad_where (fixp->fx_file, fixp->fx_line,
14304 "unsupported expression involving @size");
14305 return NULL;
14306 }
8ce3d284 14307#endif
1a0670f3 14308 /* Fall through. */
8fd4256d 14309
3e73aa7c
JH
14310 case BFD_RELOC_X86_64_PLT32:
14311 case BFD_RELOC_X86_64_GOT32:
14312 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
14313 case BFD_RELOC_X86_64_GOTPCRELX:
14314 case BFD_RELOC_X86_64_REX_GOTPCRELX:
252b5132
RH
14315 case BFD_RELOC_386_PLT32:
14316 case BFD_RELOC_386_GOT32:
02a86693 14317 case BFD_RELOC_386_GOT32X:
252b5132
RH
14318 case BFD_RELOC_386_GOTOFF:
14319 case BFD_RELOC_386_GOTPC:
13ae64f3
JJ
14320 case BFD_RELOC_386_TLS_GD:
14321 case BFD_RELOC_386_TLS_LDM:
14322 case BFD_RELOC_386_TLS_LDO_32:
14323 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
14324 case BFD_RELOC_386_TLS_IE:
14325 case BFD_RELOC_386_TLS_GOTIE:
13ae64f3
JJ
14326 case BFD_RELOC_386_TLS_LE_32:
14327 case BFD_RELOC_386_TLS_LE:
67a4f2b7
AO
14328 case BFD_RELOC_386_TLS_GOTDESC:
14329 case BFD_RELOC_386_TLS_DESC_CALL:
bffbf940
JJ
14330 case BFD_RELOC_X86_64_TLSGD:
14331 case BFD_RELOC_X86_64_TLSLD:
14332 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 14333 case BFD_RELOC_X86_64_DTPOFF64:
bffbf940
JJ
14334 case BFD_RELOC_X86_64_GOTTPOFF:
14335 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113
JB
14336 case BFD_RELOC_X86_64_TPOFF64:
14337 case BFD_RELOC_X86_64_GOTOFF64:
14338 case BFD_RELOC_X86_64_GOTPC32:
7b81dfbb
AJ
14339 case BFD_RELOC_X86_64_GOT64:
14340 case BFD_RELOC_X86_64_GOTPCREL64:
14341 case BFD_RELOC_X86_64_GOTPC64:
14342 case BFD_RELOC_X86_64_GOTPLT64:
14343 case BFD_RELOC_X86_64_PLTOFF64:
67a4f2b7
AO
14344 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14345 case BFD_RELOC_X86_64_TLSDESC_CALL:
252b5132
RH
14346 case BFD_RELOC_RVA:
14347 case BFD_RELOC_VTABLE_ENTRY:
14348 case BFD_RELOC_VTABLE_INHERIT:
6482c264
NC
14349#ifdef TE_PE
14350 case BFD_RELOC_32_SECREL:
145667f8 14351 case BFD_RELOC_16_SECIDX:
6482c264 14352#endif
252b5132
RH
14353 code = fixp->fx_r_type;
14354 break;
dbbaec26
L
14355 case BFD_RELOC_X86_64_32S:
14356 if (!fixp->fx_pcrel)
14357 {
14358 /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32. */
14359 code = fixp->fx_r_type;
14360 break;
14361 }
1a0670f3 14362 /* Fall through. */
252b5132 14363 default:
93382f6d 14364 if (fixp->fx_pcrel)
252b5132 14365 {
93382f6d
AM
14366 switch (fixp->fx_size)
14367 {
14368 default:
b091f402
AM
14369 as_bad_where (fixp->fx_file, fixp->fx_line,
14370 _("can not do %d byte pc-relative relocation"),
14371 fixp->fx_size);
93382f6d
AM
14372 code = BFD_RELOC_32_PCREL;
14373 break;
14374 case 1: code = BFD_RELOC_8_PCREL; break;
14375 case 2: code = BFD_RELOC_16_PCREL; break;
d258b828 14376 case 4: code = BFD_RELOC_32_PCREL; break;
d6ab8113
JB
14377#ifdef BFD64
14378 case 8: code = BFD_RELOC_64_PCREL; break;
14379#endif
93382f6d
AM
14380 }
14381 }
14382 else
14383 {
14384 switch (fixp->fx_size)
14385 {
14386 default:
b091f402
AM
14387 as_bad_where (fixp->fx_file, fixp->fx_line,
14388 _("can not do %d byte relocation"),
14389 fixp->fx_size);
93382f6d
AM
14390 code = BFD_RELOC_32;
14391 break;
14392 case 1: code = BFD_RELOC_8; break;
14393 case 2: code = BFD_RELOC_16; break;
14394 case 4: code = BFD_RELOC_32; break;
937149dd 14395#ifdef BFD64
3e73aa7c 14396 case 8: code = BFD_RELOC_64; break;
937149dd 14397#endif
93382f6d 14398 }
252b5132
RH
14399 }
14400 break;
14401 }
252b5132 14402
d182319b
JB
14403 if ((code == BFD_RELOC_32
14404 || code == BFD_RELOC_32_PCREL
14405 || code == BFD_RELOC_X86_64_32S)
252b5132
RH
14406 && GOT_symbol
14407 && fixp->fx_addsy == GOT_symbol)
3e73aa7c 14408 {
4fa24527 14409 if (!object_64bit)
d6ab8113
JB
14410 code = BFD_RELOC_386_GOTPC;
14411 else
14412 code = BFD_RELOC_X86_64_GOTPC32;
3e73aa7c 14413 }
7b81dfbb
AJ
14414 if ((code == BFD_RELOC_64 || code == BFD_RELOC_64_PCREL)
14415 && GOT_symbol
14416 && fixp->fx_addsy == GOT_symbol)
14417 {
14418 code = BFD_RELOC_X86_64_GOTPC64;
14419 }
252b5132 14420
add39d23
TS
14421 rel = XNEW (arelent);
14422 rel->sym_ptr_ptr = XNEW (asymbol *);
49309057 14423 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
14424
14425 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
c87db184 14426
3e73aa7c
JH
14427 if (!use_rela_relocations)
14428 {
14429 /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
14430 vtable entry to be used in the relocation's section offset. */
14431 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
14432 rel->address = fixp->fx_offset;
fbeb56a4
DK
14433#if defined (OBJ_COFF) && defined (TE_PE)
14434 else if (fixp->fx_addsy && S_IS_WEAK (fixp->fx_addsy))
14435 rel->addend = fixp->fx_addnumber - (S_GET_VALUE (fixp->fx_addsy) * 2);
14436 else
14437#endif
c6682705 14438 rel->addend = 0;
3e73aa7c
JH
14439 }
14440 /* Use the rela in 64bit mode. */
252b5132 14441 else
3e73aa7c 14442 {
862be3fb
L
14443 if (disallow_64bit_reloc)
14444 switch (code)
14445 {
862be3fb
L
14446 case BFD_RELOC_X86_64_DTPOFF64:
14447 case BFD_RELOC_X86_64_TPOFF64:
14448 case BFD_RELOC_64_PCREL:
14449 case BFD_RELOC_X86_64_GOTOFF64:
14450 case BFD_RELOC_X86_64_GOT64:
14451 case BFD_RELOC_X86_64_GOTPCREL64:
14452 case BFD_RELOC_X86_64_GOTPC64:
14453 case BFD_RELOC_X86_64_GOTPLT64:
14454 case BFD_RELOC_X86_64_PLTOFF64:
14455 as_bad_where (fixp->fx_file, fixp->fx_line,
14456 _("cannot represent relocation type %s in x32 mode"),
14457 bfd_get_reloc_code_name (code));
14458 break;
14459 default:
14460 break;
14461 }
14462
062cd5e7
AS
14463 if (!fixp->fx_pcrel)
14464 rel->addend = fixp->fx_offset;
14465 else
14466 switch (code)
14467 {
14468 case BFD_RELOC_X86_64_PLT32:
14469 case BFD_RELOC_X86_64_GOT32:
14470 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
14471 case BFD_RELOC_X86_64_GOTPCRELX:
14472 case BFD_RELOC_X86_64_REX_GOTPCRELX:
bffbf940
JJ
14473 case BFD_RELOC_X86_64_TLSGD:
14474 case BFD_RELOC_X86_64_TLSLD:
14475 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7
AO
14476 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14477 case BFD_RELOC_X86_64_TLSDESC_CALL:
062cd5e7
AS
14478 rel->addend = fixp->fx_offset - fixp->fx_size;
14479 break;
14480 default:
14481 rel->addend = (section->vma
14482 - fixp->fx_size
14483 + fixp->fx_addnumber
14484 + md_pcrel_from (fixp));
14485 break;
14486 }
3e73aa7c
JH
14487 }
14488
252b5132
RH
14489 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
14490 if (rel->howto == NULL)
14491 {
14492 as_bad_where (fixp->fx_file, fixp->fx_line,
d0b47220 14493 _("cannot represent relocation type %s"),
252b5132
RH
14494 bfd_get_reloc_code_name (code));
14495 /* Set howto to a garbage value so that we can keep going. */
14496 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
9c2799c2 14497 gas_assert (rel->howto != NULL);
252b5132
RH
14498 }
14499
14500 return rel;
14501}
14502
ee86248c 14503#include "tc-i386-intel.c"
54cfded0 14504
a60de03c
JB
14505void
14506tc_x86_parse_to_dw2regnum (expressionS *exp)
54cfded0 14507{
a60de03c
JB
14508 int saved_naked_reg;
14509 char saved_register_dot;
54cfded0 14510
a60de03c
JB
14511 saved_naked_reg = allow_naked_reg;
14512 allow_naked_reg = 1;
14513 saved_register_dot = register_chars['.'];
14514 register_chars['.'] = '.';
14515 allow_pseudo_reg = 1;
14516 expression_and_evaluate (exp);
14517 allow_pseudo_reg = 0;
14518 register_chars['.'] = saved_register_dot;
14519 allow_naked_reg = saved_naked_reg;
14520
e96d56a1 14521 if (exp->X_op == O_register && exp->X_add_number >= 0)
54cfded0 14522 {
a60de03c
JB
14523 if ((addressT) exp->X_add_number < i386_regtab_size)
14524 {
14525 exp->X_op = O_constant;
14526 exp->X_add_number = i386_regtab[exp->X_add_number]
14527 .dw2_regnum[flag_code >> 1];
14528 }
14529 else
14530 exp->X_op = O_illegal;
54cfded0 14531 }
54cfded0
AM
14532}
14533
14534void
14535tc_x86_frame_initial_instructions (void)
14536{
a60de03c
JB
14537 static unsigned int sp_regno[2];
14538
14539 if (!sp_regno[flag_code >> 1])
14540 {
14541 char *saved_input = input_line_pointer;
14542 char sp[][4] = {"esp", "rsp"};
14543 expressionS exp;
a4447b93 14544
a60de03c
JB
14545 input_line_pointer = sp[flag_code >> 1];
14546 tc_x86_parse_to_dw2regnum (&exp);
9c2799c2 14547 gas_assert (exp.X_op == O_constant);
a60de03c
JB
14548 sp_regno[flag_code >> 1] = exp.X_add_number;
14549 input_line_pointer = saved_input;
14550 }
a4447b93 14551
61ff971f
L
14552 cfi_add_CFA_def_cfa (sp_regno[flag_code >> 1], -x86_cie_data_alignment);
14553 cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
54cfded0 14554}
d2b2c203 14555
d7921315
L
14556int
14557x86_dwarf2_addr_size (void)
14558{
14559#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
14560 if (x86_elf_abi == X86_64_X32_ABI)
14561 return 4;
14562#endif
14563 return bfd_arch_bits_per_address (stdoutput) / 8;
14564}
14565
d2b2c203
DJ
14566int
14567i386_elf_section_type (const char *str, size_t len)
14568{
14569 if (flag_code == CODE_64BIT
14570 && len == sizeof ("unwind") - 1
d34049e8 14571 && startswith (str, "unwind"))
d2b2c203
DJ
14572 return SHT_X86_64_UNWIND;
14573
14574 return -1;
14575}
bb41ade5 14576
ad5fec3b
EB
14577#ifdef TE_SOLARIS
14578void
14579i386_solaris_fix_up_eh_frame (segT sec)
14580{
14581 if (flag_code == CODE_64BIT)
14582 elf_section_type (sec) = SHT_X86_64_UNWIND;
14583}
14584#endif
14585
bb41ade5
AM
14586#ifdef TE_PE
14587void
14588tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
14589{
91d6fa6a 14590 expressionS exp;
bb41ade5 14591
91d6fa6a
NC
14592 exp.X_op = O_secrel;
14593 exp.X_add_symbol = symbol;
14594 exp.X_add_number = 0;
14595 emit_expr (&exp, size);
bb41ade5
AM
14596}
14597#endif
3b22753a
L
14598
14599#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14600/* For ELF on x86-64, add support for SHF_X86_64_LARGE. */
14601
01e1a5bc 14602bfd_vma
6d4af3c2 14603x86_64_section_letter (int letter, const char **ptr_msg)
3b22753a
L
14604{
14605 if (flag_code == CODE_64BIT)
14606 {
14607 if (letter == 'l')
14608 return SHF_X86_64_LARGE;
14609
8f3bae45 14610 *ptr_msg = _("bad .section directive: want a,l,w,x,M,S,G,T in string");
64e74474 14611 }
3b22753a 14612 else
8f3bae45 14613 *ptr_msg = _("bad .section directive: want a,w,x,M,S,G,T in string");
3b22753a
L
14614 return -1;
14615}
14616
01e1a5bc 14617bfd_vma
3b22753a
L
14618x86_64_section_word (char *str, size_t len)
14619{
08dedd66 14620 if (len == 5 && flag_code == CODE_64BIT && startswith (str, "large"))
3b22753a
L
14621 return SHF_X86_64_LARGE;
14622
14623 return -1;
14624}
14625
14626static void
14627handle_large_common (int small ATTRIBUTE_UNUSED)
14628{
14629 if (flag_code != CODE_64BIT)
14630 {
14631 s_comm_internal (0, elf_common_parse);
14632 as_warn (_(".largecomm supported only in 64bit mode, producing .comm"));
14633 }
14634 else
14635 {
14636 static segT lbss_section;
14637 asection *saved_com_section_ptr = elf_com_section_ptr;
14638 asection *saved_bss_section = bss_section;
14639
14640 if (lbss_section == NULL)
14641 {
14642 flagword applicable;
14643 segT seg = now_seg;
14644 subsegT subseg = now_subseg;
14645
14646 /* The .lbss section is for local .largecomm symbols. */
14647 lbss_section = subseg_new (".lbss", 0);
14648 applicable = bfd_applicable_section_flags (stdoutput);
fd361982 14649 bfd_set_section_flags (lbss_section, applicable & SEC_ALLOC);
3b22753a
L
14650 seg_info (lbss_section)->bss = 1;
14651
14652 subseg_set (seg, subseg);
14653 }
14654
14655 elf_com_section_ptr = &_bfd_elf_large_com_section;
14656 bss_section = lbss_section;
14657
14658 s_comm_internal (0, elf_common_parse);
14659
14660 elf_com_section_ptr = saved_com_section_ptr;
14661 bss_section = saved_bss_section;
14662 }
14663}
14664#endif /* OBJ_ELF || OBJ_MAYBE_ELF */