]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/config/tc-i386.c
ppc gas: don't leak ppc_hash memory
[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)
ffa5352c
NC
2380 {
2381 char val_buf[128];
2382 char masked_buf[128];
2383
2384 /* Coded this way in order to ease translation. */
2385 sprintf_vma (val_buf, val);
2386 sprintf_vma (masked_buf, val & mask);
2387 as_warn (_("0x%s shortened to 0x%s"), val_buf, masked_buf);
2388 }
847f7ad4 2389
847f7ad4
AM
2390 return val & mask;
2391}
2392
c32fa91d
L
2393enum PREFIX_GROUP
2394{
2395 PREFIX_EXIST = 0,
2396 PREFIX_LOCK,
2397 PREFIX_REP,
04ef582a 2398 PREFIX_DS,
c32fa91d
L
2399 PREFIX_OTHER
2400};
2401
2402/* Returns
2403 a. PREFIX_EXIST if attempting to add a prefix where one from the
2404 same class already exists.
2405 b. PREFIX_LOCK if lock prefix is added.
2406 c. PREFIX_REP if rep/repne prefix is added.
04ef582a
L
2407 d. PREFIX_DS if ds prefix is added.
2408 e. PREFIX_OTHER if other prefix is added.
c32fa91d
L
2409 */
2410
2411static enum PREFIX_GROUP
e3bb37b5 2412add_prefix (unsigned int prefix)
252b5132 2413{
c32fa91d 2414 enum PREFIX_GROUP ret = PREFIX_OTHER;
b1905489 2415 unsigned int q;
252b5132 2416
29b0f896
AM
2417 if (prefix >= REX_OPCODE && prefix < REX_OPCODE + 16
2418 && flag_code == CODE_64BIT)
b1905489 2419 {
161a04f6 2420 if ((i.prefix[REX_PREFIX] & prefix & REX_W)
44846f29
JB
2421 || (i.prefix[REX_PREFIX] & prefix & REX_R)
2422 || (i.prefix[REX_PREFIX] & prefix & REX_X)
2423 || (i.prefix[REX_PREFIX] & prefix & REX_B))
c32fa91d 2424 ret = PREFIX_EXIST;
b1905489
JB
2425 q = REX_PREFIX;
2426 }
3e73aa7c 2427 else
b1905489
JB
2428 {
2429 switch (prefix)
2430 {
2431 default:
2432 abort ();
2433
b1905489 2434 case DS_PREFIX_OPCODE:
04ef582a
L
2435 ret = PREFIX_DS;
2436 /* Fall through. */
2437 case CS_PREFIX_OPCODE:
b1905489
JB
2438 case ES_PREFIX_OPCODE:
2439 case FS_PREFIX_OPCODE:
2440 case GS_PREFIX_OPCODE:
2441 case SS_PREFIX_OPCODE:
2442 q = SEG_PREFIX;
2443 break;
2444
2445 case REPNE_PREFIX_OPCODE:
2446 case REPE_PREFIX_OPCODE:
c32fa91d
L
2447 q = REP_PREFIX;
2448 ret = PREFIX_REP;
2449 break;
2450
b1905489 2451 case LOCK_PREFIX_OPCODE:
c32fa91d
L
2452 q = LOCK_PREFIX;
2453 ret = PREFIX_LOCK;
b1905489
JB
2454 break;
2455
2456 case FWAIT_OPCODE:
2457 q = WAIT_PREFIX;
2458 break;
2459
2460 case ADDR_PREFIX_OPCODE:
2461 q = ADDR_PREFIX;
2462 break;
2463
2464 case DATA_PREFIX_OPCODE:
2465 q = DATA_PREFIX;
2466 break;
2467 }
2468 if (i.prefix[q] != 0)
c32fa91d 2469 ret = PREFIX_EXIST;
b1905489 2470 }
252b5132 2471
b1905489 2472 if (ret)
252b5132 2473 {
b1905489
JB
2474 if (!i.prefix[q])
2475 ++i.prefixes;
2476 i.prefix[q] |= prefix;
252b5132 2477 }
b1905489
JB
2478 else
2479 as_bad (_("same type of prefix used twice"));
252b5132 2480
252b5132
RH
2481 return ret;
2482}
2483
2484static void
78f12dd3 2485update_code_flag (int value, int check)
eecb386c 2486{
78f12dd3
L
2487 PRINTF_LIKE ((*as_error));
2488
1e9cc1c2 2489 flag_code = (enum flag_code) value;
40fb9820
L
2490 if (flag_code == CODE_64BIT)
2491 {
2492 cpu_arch_flags.bitfield.cpu64 = 1;
2493 cpu_arch_flags.bitfield.cpuno64 = 0;
40fb9820
L
2494 }
2495 else
2496 {
2497 cpu_arch_flags.bitfield.cpu64 = 0;
2498 cpu_arch_flags.bitfield.cpuno64 = 1;
40fb9820
L
2499 }
2500 if (value == CODE_64BIT && !cpu_arch_flags.bitfield.cpulm )
3e73aa7c 2501 {
78f12dd3
L
2502 if (check)
2503 as_error = as_fatal;
2504 else
2505 as_error = as_bad;
2506 (*as_error) (_("64bit mode not supported on `%s'."),
2507 cpu_arch_name ? cpu_arch_name : default_arch);
3e73aa7c 2508 }
40fb9820 2509 if (value == CODE_32BIT && !cpu_arch_flags.bitfield.cpui386)
3e73aa7c 2510 {
78f12dd3
L
2511 if (check)
2512 as_error = as_fatal;
2513 else
2514 as_error = as_bad;
2515 (*as_error) (_("32bit mode not supported on `%s'."),
2516 cpu_arch_name ? cpu_arch_name : default_arch);
3e73aa7c 2517 }
eecb386c
AM
2518 stackop_size = '\0';
2519}
2520
78f12dd3
L
2521static void
2522set_code_flag (int value)
2523{
2524 update_code_flag (value, 0);
2525}
2526
eecb386c 2527static void
e3bb37b5 2528set_16bit_gcc_code_flag (int new_code_flag)
252b5132 2529{
1e9cc1c2 2530 flag_code = (enum flag_code) new_code_flag;
40fb9820
L
2531 if (flag_code != CODE_16BIT)
2532 abort ();
2533 cpu_arch_flags.bitfield.cpu64 = 0;
2534 cpu_arch_flags.bitfield.cpuno64 = 1;
9306ca4a 2535 stackop_size = LONG_MNEM_SUFFIX;
252b5132
RH
2536}
2537
2538static void
e3bb37b5 2539set_intel_syntax (int syntax_flag)
252b5132
RH
2540{
2541 /* Find out if register prefixing is specified. */
2542 int ask_naked_reg = 0;
2543
2544 SKIP_WHITESPACE ();
29b0f896 2545 if (!is_end_of_line[(unsigned char) *input_line_pointer])
252b5132 2546 {
d02603dc
NC
2547 char *string;
2548 int e = get_symbol_name (&string);
252b5132 2549
47926f60 2550 if (strcmp (string, "prefix") == 0)
252b5132 2551 ask_naked_reg = 1;
47926f60 2552 else if (strcmp (string, "noprefix") == 0)
252b5132
RH
2553 ask_naked_reg = -1;
2554 else
d0b47220 2555 as_bad (_("bad argument to syntax directive."));
d02603dc 2556 (void) restore_line_pointer (e);
252b5132
RH
2557 }
2558 demand_empty_rest_of_line ();
c3332e24 2559
252b5132
RH
2560 intel_syntax = syntax_flag;
2561
2562 if (ask_naked_reg == 0)
f86103b7
AM
2563 allow_naked_reg = (intel_syntax
2564 && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
252b5132
RH
2565 else
2566 allow_naked_reg = (ask_naked_reg < 0);
9306ca4a 2567
ee86248c 2568 expr_set_rank (O_full_ptr, syntax_flag ? 10 : 0);
7ab9ffdd 2569
e4a3b5a4 2570 identifier_chars['%'] = intel_syntax && allow_naked_reg ? '%' : 0;
9306ca4a 2571 identifier_chars['$'] = intel_syntax ? '$' : 0;
e4a3b5a4 2572 register_prefix = allow_naked_reg ? "" : "%";
252b5132
RH
2573}
2574
1efbbeb4
L
2575static void
2576set_intel_mnemonic (int mnemonic_flag)
2577{
e1d4d893 2578 intel_mnemonic = mnemonic_flag;
1efbbeb4
L
2579}
2580
db51cc60
L
2581static void
2582set_allow_index_reg (int flag)
2583{
2584 allow_index_reg = flag;
2585}
2586
cb19c032 2587static void
7bab8ab5 2588set_check (int what)
cb19c032 2589{
7bab8ab5
JB
2590 enum check_kind *kind;
2591 const char *str;
2592
2593 if (what)
2594 {
2595 kind = &operand_check;
2596 str = "operand";
2597 }
2598 else
2599 {
2600 kind = &sse_check;
2601 str = "sse";
2602 }
2603
cb19c032
L
2604 SKIP_WHITESPACE ();
2605
2606 if (!is_end_of_line[(unsigned char) *input_line_pointer])
2607 {
d02603dc
NC
2608 char *string;
2609 int e = get_symbol_name (&string);
cb19c032
L
2610
2611 if (strcmp (string, "none") == 0)
7bab8ab5 2612 *kind = check_none;
cb19c032 2613 else if (strcmp (string, "warning") == 0)
7bab8ab5 2614 *kind = check_warning;
cb19c032 2615 else if (strcmp (string, "error") == 0)
7bab8ab5 2616 *kind = check_error;
cb19c032 2617 else
7bab8ab5 2618 as_bad (_("bad argument to %s_check directive."), str);
d02603dc 2619 (void) restore_line_pointer (e);
cb19c032
L
2620 }
2621 else
7bab8ab5 2622 as_bad (_("missing argument for %s_check directive"), str);
cb19c032
L
2623
2624 demand_empty_rest_of_line ();
2625}
2626
8a9036a4
L
2627static void
2628check_cpu_arch_compatible (const char *name ATTRIBUTE_UNUSED,
1e9cc1c2 2629 i386_cpu_flags new_flag ATTRIBUTE_UNUSED)
8a9036a4
L
2630{
2631#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2632 static const char *arch;
2633
c085ab00 2634 /* Intel MCU is only supported on ELF. */
8a9036a4
L
2635 if (!IS_ELF)
2636 return;
2637
2638 if (!arch)
2639 {
2640 /* Use cpu_arch_name if it is set in md_parse_option. Otherwise
2641 use default_arch. */
2642 arch = cpu_arch_name;
2643 if (!arch)
2644 arch = default_arch;
2645 }
2646
81486035 2647 /* If we are targeting Intel MCU, we must enable it. */
648d04db
JB
2648 if ((get_elf_backend_data (stdoutput)->elf_machine_code == EM_IAMCU)
2649 == new_flag.bitfield.cpuiamcu)
81486035
L
2650 return;
2651
8a9036a4
L
2652 as_bad (_("`%s' is not supported on `%s'"), name, arch);
2653#endif
2654}
2655
8180707f
JB
2656static void
2657extend_cpu_sub_arch_name (const char *name)
2658{
2659 if (cpu_sub_arch_name)
2660 cpu_sub_arch_name = reconcat (cpu_sub_arch_name, cpu_sub_arch_name,
ae89daec 2661 ".", name, (const char *) NULL);
8180707f 2662 else
ae89daec 2663 cpu_sub_arch_name = concat (".", name, (const char *) NULL);
8180707f
JB
2664}
2665
e413e4e9 2666static void
e3bb37b5 2667set_cpu_arch (int dummy ATTRIBUTE_UNUSED)
e413e4e9 2668{
f68697e8
JB
2669 typedef struct arch_stack_entry
2670 {
2671 const struct arch_stack_entry *prev;
2672 const char *name;
2673 char *sub_name;
2674 i386_cpu_flags flags;
2675 i386_cpu_flags isa_flags;
2676 enum processor_type isa;
2677 enum flag_code flag_code;
2678 char stackop_size;
2679 bool no_cond_jump_promotion;
2680 } arch_stack_entry;
2681 static const arch_stack_entry *arch_stack_top;
2682
47926f60 2683 SKIP_WHITESPACE ();
e413e4e9 2684
29b0f896 2685 if (!is_end_of_line[(unsigned char) *input_line_pointer])
e413e4e9 2686 {
3ce2ebcf
JB
2687 char *s;
2688 int e = get_symbol_name (&s);
2689 const char *string = s;
2690 unsigned int j = 0;
40fb9820 2691 i386_cpu_flags flags;
e413e4e9 2692
3ce2ebcf
JB
2693 if (strcmp (string, "default") == 0)
2694 {
2695 if (strcmp (default_arch, "iamcu") == 0)
2696 string = default_arch;
2697 else
2698 {
2699 static const i386_cpu_flags cpu_unknown_flags = CPU_UNKNOWN_FLAGS;
2700
2701 cpu_arch_name = NULL;
2702 free (cpu_sub_arch_name);
2703 cpu_sub_arch_name = NULL;
2704 cpu_arch_flags = cpu_unknown_flags;
2705 if (flag_code == CODE_64BIT)
2706 {
2707 cpu_arch_flags.bitfield.cpu64 = 1;
2708 cpu_arch_flags.bitfield.cpuno64 = 0;
2709 }
2710 else
2711 {
2712 cpu_arch_flags.bitfield.cpu64 = 0;
2713 cpu_arch_flags.bitfield.cpuno64 = 1;
2714 }
2715 cpu_arch_isa = PROCESSOR_UNKNOWN;
ae89daec 2716 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
3ce2ebcf
JB
2717 if (!cpu_arch_tune_set)
2718 {
2719 cpu_arch_tune = cpu_arch_isa;
2720 cpu_arch_tune_flags = cpu_arch_isa_flags;
2721 }
2722
2723 j = ARRAY_SIZE (cpu_arch) + 1;
2724 }
2725 }
f68697e8
JB
2726 else if (strcmp (string, "push") == 0)
2727 {
2728 arch_stack_entry *top = XNEW (arch_stack_entry);
2729
2730 top->name = cpu_arch_name;
2731 if (cpu_sub_arch_name)
2732 top->sub_name = xstrdup (cpu_sub_arch_name);
2733 else
2734 top->sub_name = NULL;
2735 top->flags = cpu_arch_flags;
2736 top->isa = cpu_arch_isa;
2737 top->isa_flags = cpu_arch_isa_flags;
2738 top->flag_code = flag_code;
2739 top->stackop_size = stackop_size;
2740 top->no_cond_jump_promotion = no_cond_jump_promotion;
2741
2742 top->prev = arch_stack_top;
2743 arch_stack_top = top;
2744
2745 (void) restore_line_pointer (e);
2746 demand_empty_rest_of_line ();
2747 return;
2748 }
2749 else if (strcmp (string, "pop") == 0)
2750 {
2751 const arch_stack_entry *top = arch_stack_top;
2752
2753 if (!top)
2754 as_bad (_(".arch stack is empty"));
2755 else if (top->flag_code != flag_code
2756 || top->stackop_size != stackop_size)
2757 {
2758 static const unsigned int bits[] = {
2759 [CODE_16BIT] = 16,
2760 [CODE_32BIT] = 32,
2761 [CODE_64BIT] = 64,
2762 };
2763
2764 as_bad (_("this `.arch pop' requires `.code%u%s' to be in effect"),
2765 bits[top->flag_code],
2766 top->stackop_size == LONG_MNEM_SUFFIX ? "gcc" : "");
2767 }
2768 else
2769 {
2770 arch_stack_top = top->prev;
2771
2772 cpu_arch_name = top->name;
2773 free (cpu_sub_arch_name);
2774 cpu_sub_arch_name = top->sub_name;
2775 cpu_arch_flags = top->flags;
2776 cpu_arch_isa = top->isa;
2777 cpu_arch_isa_flags = top->isa_flags;
2778 no_cond_jump_promotion = top->no_cond_jump_promotion;
2779
2780 XDELETE (top);
2781 }
2782
2783 (void) restore_line_pointer (e);
2784 demand_empty_rest_of_line ();
2785 return;
2786 }
3ce2ebcf
JB
2787
2788 for (; j < ARRAY_SIZE (cpu_arch); j++)
e413e4e9 2789 {
ae89daec
JB
2790 if (strcmp (string + (*string == '.'), cpu_arch[j].name) == 0
2791 && (*string == '.') == (cpu_arch[j].type == PROCESSOR_NONE))
e413e4e9 2792 {
5c6af06e
JB
2793 if (*string != '.')
2794 {
ae89daec 2795 check_cpu_arch_compatible (string, cpu_arch[j].enable);
648d04db 2796
91d6fa6a 2797 cpu_arch_name = cpu_arch[j].name;
d92c7521 2798 free (cpu_sub_arch_name);
5c6af06e 2799 cpu_sub_arch_name = NULL;
ae89daec 2800 cpu_arch_flags = cpu_arch[j].enable;
40fb9820
L
2801 if (flag_code == CODE_64BIT)
2802 {
2803 cpu_arch_flags.bitfield.cpu64 = 1;
2804 cpu_arch_flags.bitfield.cpuno64 = 0;
2805 }
2806 else
2807 {
2808 cpu_arch_flags.bitfield.cpu64 = 0;
2809 cpu_arch_flags.bitfield.cpuno64 = 1;
2810 }
91d6fa6a 2811 cpu_arch_isa = cpu_arch[j].type;
ae89daec 2812 cpu_arch_isa_flags = cpu_arch[j].enable;
ccc9c027
L
2813 if (!cpu_arch_tune_set)
2814 {
2815 cpu_arch_tune = cpu_arch_isa;
2816 cpu_arch_tune_flags = cpu_arch_isa_flags;
2817 }
5c6af06e
JB
2818 break;
2819 }
40fb9820 2820
ae89daec
JB
2821 if (cpu_flags_all_zero (&cpu_arch[j].enable))
2822 continue;
2823
293f5f65 2824 flags = cpu_flags_or (cpu_arch_flags,
ae89daec 2825 cpu_arch[j].enable);
81486035 2826
5b64d091 2827 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
5c6af06e 2828 {
ae89daec 2829 extend_cpu_sub_arch_name (string + 1);
40fb9820 2830 cpu_arch_flags = flags;
a586129e 2831 cpu_arch_isa_flags = flags;
5c6af06e 2832 }
0089dace
L
2833 else
2834 cpu_arch_isa_flags
2835 = cpu_flags_or (cpu_arch_isa_flags,
ae89daec 2836 cpu_arch[j].enable);
d02603dc 2837 (void) restore_line_pointer (e);
5c6af06e
JB
2838 demand_empty_rest_of_line ();
2839 return;
e413e4e9
AM
2840 }
2841 }
293f5f65 2842
ae89daec 2843 if (startswith (string, ".no") && j >= ARRAY_SIZE (cpu_arch))
293f5f65 2844 {
33eaf5de 2845 /* Disable an ISA extension. */
ae89daec
JB
2846 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
2847 if (cpu_arch[j].type == PROCESSOR_NONE
2848 && strcmp (string + 3, cpu_arch[j].name) == 0)
293f5f65
L
2849 {
2850 flags = cpu_flags_and_not (cpu_arch_flags,
ae89daec 2851 cpu_arch[j].disable);
293f5f65
L
2852 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
2853 {
ae89daec 2854 extend_cpu_sub_arch_name (string + 1);
293f5f65
L
2855 cpu_arch_flags = flags;
2856 cpu_arch_isa_flags = flags;
2857 }
2858 (void) restore_line_pointer (e);
2859 demand_empty_rest_of_line ();
2860 return;
2861 }
293f5f65
L
2862 }
2863
3ce2ebcf 2864 if (j == ARRAY_SIZE (cpu_arch))
e413e4e9
AM
2865 as_bad (_("no such architecture: `%s'"), string);
2866
2867 *input_line_pointer = e;
2868 }
2869 else
2870 as_bad (_("missing cpu architecture"));
2871
fddf5b5b
AM
2872 no_cond_jump_promotion = 0;
2873 if (*input_line_pointer == ','
29b0f896 2874 && !is_end_of_line[(unsigned char) input_line_pointer[1]])
fddf5b5b 2875 {
d02603dc
NC
2876 char *string;
2877 char e;
2878
2879 ++input_line_pointer;
2880 e = get_symbol_name (&string);
fddf5b5b
AM
2881
2882 if (strcmp (string, "nojumps") == 0)
2883 no_cond_jump_promotion = 1;
2884 else if (strcmp (string, "jumps") == 0)
2885 ;
2886 else
2887 as_bad (_("no such architecture modifier: `%s'"), string);
2888
d02603dc 2889 (void) restore_line_pointer (e);
fddf5b5b
AM
2890 }
2891
e413e4e9
AM
2892 demand_empty_rest_of_line ();
2893}
2894
8a9036a4
L
2895enum bfd_architecture
2896i386_arch (void)
2897{
c085ab00 2898 if (cpu_arch_isa == PROCESSOR_IAMCU)
81486035
L
2899 {
2900 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2901 || flag_code == CODE_64BIT)
2902 as_fatal (_("Intel MCU is 32bit ELF only"));
2903 return bfd_arch_iamcu;
2904 }
8a9036a4
L
2905 else
2906 return bfd_arch_i386;
2907}
2908
b9d79e03 2909unsigned long
7016a5d5 2910i386_mach (void)
b9d79e03 2911{
d34049e8 2912 if (startswith (default_arch, "x86_64"))
8a9036a4 2913 {
c085ab00 2914 if (default_arch[6] == '\0')
8a9036a4 2915 return bfd_mach_x86_64;
351f65ca
L
2916 else
2917 return bfd_mach_x64_32;
8a9036a4 2918 }
5197d474
L
2919 else if (!strcmp (default_arch, "i386")
2920 || !strcmp (default_arch, "iamcu"))
81486035
L
2921 {
2922 if (cpu_arch_isa == PROCESSOR_IAMCU)
2923 {
2924 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
2925 as_fatal (_("Intel MCU is 32bit ELF only"));
2926 return bfd_mach_i386_iamcu;
2927 }
2928 else
2929 return bfd_mach_i386_i386;
2930 }
b9d79e03 2931 else
2b5d6a91 2932 as_fatal (_("unknown architecture"));
b9d79e03 2933}
b9d79e03 2934\f
252b5132 2935void
7016a5d5 2936md_begin (void)
252b5132 2937{
86fa6981
L
2938 /* Support pseudo prefixes like {disp32}. */
2939 lex_type ['{'] = LEX_BEGIN_NAME;
2940
47926f60 2941 /* Initialize op_hash hash table. */
629310ab 2942 op_hash = str_htab_create ();
252b5132
RH
2943
2944 {
d3ce72d0 2945 const insn_template *optab;
29b0f896 2946 templates *core_optab;
252b5132 2947
47926f60
KH
2948 /* Setup for loop. */
2949 optab = i386_optab;
add39d23 2950 core_optab = XNEW (templates);
252b5132
RH
2951 core_optab->start = optab;
2952
2953 while (1)
2954 {
2955 ++optab;
2956 if (optab->name == NULL
2957 || strcmp (optab->name, (optab - 1)->name) != 0)
2958 {
2959 /* different name --> ship out current template list;
47926f60 2960 add to hash table; & begin anew. */
252b5132 2961 core_optab->end = optab;
fe0e921f
AM
2962 if (str_hash_insert (op_hash, (optab - 1)->name, core_optab, 0))
2963 as_fatal (_("duplicate %s"), (optab - 1)->name);
2964
252b5132
RH
2965 if (optab->name == NULL)
2966 break;
add39d23 2967 core_optab = XNEW (templates);
252b5132
RH
2968 core_optab->start = optab;
2969 }
2970 }
2971 }
2972
47926f60 2973 /* Initialize reg_hash hash table. */
629310ab 2974 reg_hash = str_htab_create ();
252b5132 2975 {
29b0f896 2976 const reg_entry *regtab;
c3fe08fa 2977 unsigned int regtab_size = i386_regtab_size;
252b5132 2978
c3fe08fa 2979 for (regtab = i386_regtab; regtab_size--; regtab++)
6225c532 2980 {
6288d05f
JB
2981 switch (regtab->reg_type.bitfield.class)
2982 {
2983 case Reg:
34684862
JB
2984 if (regtab->reg_type.bitfield.dword)
2985 {
2986 if (regtab->reg_type.bitfield.instance == Accum)
2987 reg_eax = regtab;
2988 }
2989 else if (regtab->reg_type.bitfield.tbyte)
6288d05f
JB
2990 {
2991 /* There's no point inserting st(<N>) in the hash table, as
2992 parentheses aren't included in register_chars[] anyway. */
2993 if (regtab->reg_type.bitfield.instance != Accum)
2994 continue;
2995 reg_st0 = regtab;
2996 }
2997 break;
2998
5e042380
JB
2999 case SReg:
3000 switch (regtab->reg_num)
3001 {
3002 case 0: reg_es = regtab; break;
3003 case 2: reg_ss = regtab; break;
3004 case 3: reg_ds = regtab; break;
3005 }
3006 break;
3007
6288d05f
JB
3008 case RegMask:
3009 if (!regtab->reg_num)
3010 reg_k0 = regtab;
3011 break;
3012 }
3013
6225c532
JB
3014 if (str_hash_insert (reg_hash, regtab->reg_name, regtab, 0) != NULL)
3015 as_fatal (_("duplicate %s"), regtab->reg_name);
6225c532 3016 }
252b5132
RH
3017 }
3018
47926f60 3019 /* Fill in lexical tables: mnemonic_chars, operand_chars. */
252b5132 3020 {
29b0f896
AM
3021 int c;
3022 char *p;
252b5132
RH
3023
3024 for (c = 0; c < 256; c++)
3025 {
014fbcda 3026 if (ISDIGIT (c) || ISLOWER (c))
252b5132
RH
3027 {
3028 mnemonic_chars[c] = c;
3029 register_chars[c] = c;
3030 operand_chars[c] = c;
3031 }
3882b010 3032 else if (ISUPPER (c))
252b5132 3033 {
3882b010 3034 mnemonic_chars[c] = TOLOWER (c);
252b5132
RH
3035 register_chars[c] = mnemonic_chars[c];
3036 operand_chars[c] = c;
3037 }
43234a1e 3038 else if (c == '{' || c == '}')
86fa6981
L
3039 {
3040 mnemonic_chars[c] = c;
3041 operand_chars[c] = c;
3042 }
b3983e5f
JB
3043#ifdef SVR4_COMMENT_CHARS
3044 else if (c == '\\' && strchr (i386_comment_chars, '/'))
3045 operand_chars[c] = c;
3046#endif
252b5132 3047
3882b010 3048 if (ISALPHA (c) || ISDIGIT (c))
252b5132
RH
3049 identifier_chars[c] = c;
3050 else if (c >= 128)
3051 {
3052 identifier_chars[c] = c;
3053 operand_chars[c] = c;
3054 }
3055 }
3056
3057#ifdef LEX_AT
3058 identifier_chars['@'] = '@';
32137342
NC
3059#endif
3060#ifdef LEX_QM
3061 identifier_chars['?'] = '?';
3062 operand_chars['?'] = '?';
252b5132 3063#endif
c0f3af97 3064 mnemonic_chars['_'] = '_';
791fe849 3065 mnemonic_chars['-'] = '-';
0003779b 3066 mnemonic_chars['.'] = '.';
252b5132
RH
3067 identifier_chars['_'] = '_';
3068 identifier_chars['.'] = '.';
3069
3070 for (p = operand_special_chars; *p != '\0'; p++)
3071 operand_chars[(unsigned char) *p] = *p;
3072 }
3073
a4447b93
RH
3074 if (flag_code == CODE_64BIT)
3075 {
ca19b261
KT
3076#if defined (OBJ_COFF) && defined (TE_PE)
3077 x86_dwarf2_return_column = (OUTPUT_FLAVOR == bfd_target_coff_flavour
3078 ? 32 : 16);
3079#else
a4447b93 3080 x86_dwarf2_return_column = 16;
ca19b261 3081#endif
61ff971f 3082 x86_cie_data_alignment = -8;
a4447b93
RH
3083 }
3084 else
3085 {
3086 x86_dwarf2_return_column = 8;
3087 x86_cie_data_alignment = -4;
3088 }
e379e5f3
L
3089
3090 /* NB: FUSED_JCC_PADDING frag must have sufficient room so that it
3091 can be turned into BRANCH_PREFIX frag. */
3092 if (align_branch_prefix_size > MAX_FUSED_JCC_PADDING_SIZE)
3093 abort ();
252b5132
RH
3094}
3095
3096void
e3bb37b5 3097i386_print_statistics (FILE *file)
252b5132 3098{
629310ab
ML
3099 htab_print_statistics (file, "i386 opcode", op_hash);
3100 htab_print_statistics (file, "i386 register", reg_hash);
252b5132
RH
3101}
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
3575 ? Opcode_SIMD_FloatD : Opcode_SIMD_IntD;
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)
ca5312a2 4332 && i.rounding.type == rc_none
e771e7c9 4333 && is_evex_encoding (&i.tm)
80c34c38 4334 && (i.vec_encoding != vex_encoding_evex
dd22218c 4335 || cpu_arch_isa_flags.bitfield.cpuavx512vl
80c34c38 4336 || i.tm.cpu_flags.bitfield.cpuavx512vl
7091c612 4337 || (i.tm.operand_types[2].bitfield.zmmword
dd22218c 4338 && i.types[2].bitfield.ymmword))))
b6f8c7c4 4339 && ((i.tm.base_opcode == 0x55
b6f8c7c4 4340 || i.tm.base_opcode == 0x57
35648716
JB
4341 || i.tm.base_opcode == 0xdf
4342 || i.tm.base_opcode == 0xef
4343 || i.tm.base_opcode == 0xf8
4344 || i.tm.base_opcode == 0xf9
4345 || i.tm.base_opcode == 0xfa
4346 || i.tm.base_opcode == 0xfb
1424ad86 4347 || i.tm.base_opcode == 0x42
35648716 4348 || i.tm.base_opcode == 0x47)
b6f8c7c4
L
4349 && i.tm.extension_opcode == None))
4350 {
99112332 4351 /* Optimize: -O1:
8305403a
L
4352 VOP, one of vandnps, vandnpd, vxorps, vxorpd, vpsubb, vpsubd,
4353 vpsubq and vpsubw:
b6f8c7c4
L
4354 EVEX VOP %zmmM, %zmmM, %zmmN
4355 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4356 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4357 EVEX VOP %ymmM, %ymmM, %ymmN
4358 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4359 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4360 VEX VOP %ymmM, %ymmM, %ymmN
4361 -> VEX VOP %xmmM, %xmmM, %xmmN
4362 VOP, one of vpandn and vpxor:
4363 VEX VOP %ymmM, %ymmM, %ymmN
4364 -> VEX VOP %xmmM, %xmmM, %xmmN
4365 VOP, one of vpandnd and vpandnq:
4366 EVEX VOP %zmmM, %zmmM, %zmmN
4367 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4368 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4369 EVEX VOP %ymmM, %ymmM, %ymmN
4370 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4371 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4372 VOP, one of vpxord and vpxorq:
4373 EVEX VOP %zmmM, %zmmM, %zmmN
4374 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4375 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4376 EVEX VOP %ymmM, %ymmM, %ymmN
4377 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4378 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
1424ad86
JB
4379 VOP, one of kxord and kxorq:
4380 VEX VOP %kM, %kM, %kN
4381 -> VEX kxorw %kM, %kM, %kN
4382 VOP, one of kandnd and kandnq:
4383 VEX VOP %kM, %kM, %kN
4384 -> VEX kandnw %kM, %kM, %kN
b6f8c7c4 4385 */
e771e7c9 4386 if (is_evex_encoding (&i.tm))
b6f8c7c4 4387 {
7b1d7ca1 4388 if (i.vec_encoding != vex_encoding_evex)
b6f8c7c4
L
4389 {
4390 i.tm.opcode_modifier.vex = VEX128;
4391 i.tm.opcode_modifier.vexw = VEXW0;
4392 i.tm.opcode_modifier.evex = 0;
4393 }
7b1d7ca1 4394 else if (optimize > 1)
dd22218c
L
4395 i.tm.opcode_modifier.evex = EVEX128;
4396 else
4397 return;
b6f8c7c4 4398 }
f74a6307 4399 else if (i.tm.operand_types[0].bitfield.class == RegMask)
1424ad86 4400 {
35648716 4401 i.tm.opcode_modifier.opcodeprefix = PREFIX_NONE;
1424ad86
JB
4402 i.tm.opcode_modifier.vexw = VEXW0;
4403 }
b6f8c7c4
L
4404 else
4405 i.tm.opcode_modifier.vex = VEX128;
4406
4407 if (i.tm.opcode_modifier.vex)
4408 for (j = 0; j < 3; j++)
4409 {
4410 i.types[j].bitfield.xmmword = 1;
4411 i.types[j].bitfield.ymmword = 0;
4412 }
4413 }
392a5972 4414 else if (i.vec_encoding != vex_encoding_evex
97ed31ae 4415 && !i.types[0].bitfield.zmmword
392a5972 4416 && !i.types[1].bitfield.zmmword
6225c532 4417 && !i.mask.reg
a5748e0d 4418 && !i.broadcast.bytes
97ed31ae 4419 && is_evex_encoding (&i.tm)
35648716
JB
4420 && ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4421 || (i.tm.base_opcode & ~4) == 0xdb
4422 || (i.tm.base_opcode & ~4) == 0xeb)
97ed31ae
L
4423 && i.tm.extension_opcode == None)
4424 {
4425 /* Optimize: -O1:
4426 VOP, one of vmovdqa32, vmovdqa64, vmovdqu8, vmovdqu16,
4427 vmovdqu32 and vmovdqu64:
4428 EVEX VOP %xmmM, %xmmN
4429 -> VEX vmovdqa|vmovdqu %xmmM, %xmmN (M and N < 16)
4430 EVEX VOP %ymmM, %ymmN
4431 -> VEX vmovdqa|vmovdqu %ymmM, %ymmN (M and N < 16)
4432 EVEX VOP %xmmM, mem
4433 -> VEX vmovdqa|vmovdqu %xmmM, mem (M < 16)
4434 EVEX VOP %ymmM, mem
4435 -> VEX vmovdqa|vmovdqu %ymmM, mem (M < 16)
4436 EVEX VOP mem, %xmmN
4437 -> VEX mvmovdqa|vmovdquem, %xmmN (N < 16)
4438 EVEX VOP mem, %ymmN
4439 -> VEX vmovdqa|vmovdqu mem, %ymmN (N < 16)
a0a1771e
JB
4440 VOP, one of vpand, vpandn, vpor, vpxor:
4441 EVEX VOP{d,q} %xmmL, %xmmM, %xmmN
4442 -> VEX VOP %xmmL, %xmmM, %xmmN (L, M, and N < 16)
4443 EVEX VOP{d,q} %ymmL, %ymmM, %ymmN
4444 -> VEX VOP %ymmL, %ymmM, %ymmN (L, M, and N < 16)
4445 EVEX VOP{d,q} mem, %xmmM, %xmmN
4446 -> VEX VOP mem, %xmmM, %xmmN (M and N < 16)
4447 EVEX VOP{d,q} mem, %ymmM, %ymmN
4448 -> VEX VOP mem, %ymmM, %ymmN (M and N < 16)
97ed31ae 4449 */
a0a1771e 4450 for (j = 0; j < i.operands; j++)
392a5972
L
4451 if (operand_type_check (i.types[j], disp)
4452 && i.op[j].disps->X_op == O_constant)
4453 {
4454 /* Since the VEX prefix has 2 or 3 bytes, the EVEX prefix
4455 has 4 bytes, EVEX Disp8 has 1 byte and VEX Disp32 has 4
4456 bytes, we choose EVEX Disp8 over VEX Disp32. */
4457 int evex_disp8, vex_disp8;
4458 unsigned int memshift = i.memshift;
4459 offsetT n = i.op[j].disps->X_add_number;
4460
4461 evex_disp8 = fits_in_disp8 (n);
4462 i.memshift = 0;
4463 vex_disp8 = fits_in_disp8 (n);
4464 if (evex_disp8 != vex_disp8)
4465 {
4466 i.memshift = memshift;
4467 return;
4468 }
4469
4470 i.types[j].bitfield.disp8 = vex_disp8;
4471 break;
4472 }
35648716
JB
4473 if ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4474 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2)
4475 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
97ed31ae
L
4476 i.tm.opcode_modifier.vex
4477 = i.types[0].bitfield.ymmword ? VEX256 : VEX128;
4478 i.tm.opcode_modifier.vexw = VEXW0;
79dec6b7 4479 /* VPAND, VPOR, and VPXOR are commutative. */
35648716 4480 if (i.reg_operands == 3 && i.tm.base_opcode != 0xdf)
79dec6b7 4481 i.tm.opcode_modifier.commutative = 1;
97ed31ae
L
4482 i.tm.opcode_modifier.evex = 0;
4483 i.tm.opcode_modifier.masking = 0;
a0a1771e 4484 i.tm.opcode_modifier.broadcast = 0;
97ed31ae
L
4485 i.tm.opcode_modifier.disp8memshift = 0;
4486 i.memshift = 0;
a0a1771e
JB
4487 if (j < i.operands)
4488 i.types[j].bitfield.disp8
4489 = fits_in_disp8 (i.op[j].disps->X_add_number);
97ed31ae 4490 }
b6f8c7c4
L
4491}
4492
ae531041
L
4493/* Return non-zero for load instruction. */
4494
4495static int
4496load_insn_p (void)
4497{
4498 unsigned int dest;
4499 int any_vex_p = is_any_vex_encoding (&i.tm);
4500 unsigned int base_opcode = i.tm.base_opcode | 1;
4501
4502 if (!any_vex_p)
4503 {
a09f656b 4504 /* Anysize insns: lea, invlpg, clflush, prefetchnta, prefetcht0,
4505 prefetcht1, prefetcht2, prefetchtw, bndmk, bndcl, bndcu, bndcn,
4506 bndstx, bndldx, prefetchwt1, clflushopt, clwb, cldemote. */
4507 if (i.tm.opcode_modifier.anysize)
ae531041
L
4508 return 0;
4509
389d00a5
JB
4510 /* pop. */
4511 if (strcmp (i.tm.name, "pop") == 0)
4512 return 1;
4513 }
4514
4515 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE)
4516 {
4517 /* popf, popa. */
4518 if (i.tm.base_opcode == 0x9d
a09f656b 4519 || i.tm.base_opcode == 0x61)
ae531041
L
4520 return 1;
4521
4522 /* movs, cmps, lods, scas. */
4523 if ((i.tm.base_opcode | 0xb) == 0xaf)
4524 return 1;
4525
a09f656b 4526 /* outs, xlatb. */
4527 if (base_opcode == 0x6f
4528 || i.tm.base_opcode == 0xd7)
ae531041 4529 return 1;
a09f656b 4530 /* NB: For AMD-specific insns with implicit memory operands,
4531 they're intentionally not covered. */
ae531041
L
4532 }
4533
4534 /* No memory operand. */
4535 if (!i.mem_operands)
4536 return 0;
4537
4538 if (any_vex_p)
4539 {
4540 /* vldmxcsr. */
4541 if (i.tm.base_opcode == 0xae
4542 && i.tm.opcode_modifier.vex
441f6aca 4543 && i.tm.opcode_modifier.opcodespace == SPACE_0F
35648716 4544 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
ae531041
L
4545 && i.tm.extension_opcode == 2)
4546 return 1;
4547 }
389d00a5 4548 else if (i.tm.opcode_modifier.opcodespace == SPACE_BASE)
ae531041
L
4549 {
4550 /* test, not, neg, mul, imul, div, idiv. */
4551 if ((i.tm.base_opcode == 0xf6 || i.tm.base_opcode == 0xf7)
4552 && i.tm.extension_opcode != 1)
4553 return 1;
4554
4555 /* inc, dec. */
4556 if (base_opcode == 0xff && i.tm.extension_opcode <= 1)
4557 return 1;
4558
4559 /* add, or, adc, sbb, and, sub, xor, cmp. */
4560 if (i.tm.base_opcode >= 0x80 && i.tm.base_opcode <= 0x83)
4561 return 1;
4562
ae531041
L
4563 /* rol, ror, rcl, rcr, shl/sal, shr, sar. */
4564 if ((base_opcode == 0xc1
4565 || (i.tm.base_opcode >= 0xd0 && i.tm.base_opcode <= 0xd3))
4566 && i.tm.extension_opcode != 6)
4567 return 1;
4568
ae531041 4569 /* Check for x87 instructions. */
389d00a5 4570 if (base_opcode >= 0xd8 && base_opcode <= 0xdf)
ae531041
L
4571 {
4572 /* Skip fst, fstp, fstenv, fstcw. */
4573 if (i.tm.base_opcode == 0xd9
4574 && (i.tm.extension_opcode == 2
4575 || i.tm.extension_opcode == 3
4576 || i.tm.extension_opcode == 6
4577 || i.tm.extension_opcode == 7))
4578 return 0;
4579
4580 /* Skip fisttp, fist, fistp, fstp. */
4581 if (i.tm.base_opcode == 0xdb
4582 && (i.tm.extension_opcode == 1
4583 || i.tm.extension_opcode == 2
4584 || i.tm.extension_opcode == 3
4585 || i.tm.extension_opcode == 7))
4586 return 0;
4587
4588 /* Skip fisttp, fst, fstp, fsave, fstsw. */
4589 if (i.tm.base_opcode == 0xdd
4590 && (i.tm.extension_opcode == 1
4591 || i.tm.extension_opcode == 2
4592 || i.tm.extension_opcode == 3
4593 || i.tm.extension_opcode == 6
4594 || i.tm.extension_opcode == 7))
4595 return 0;
4596
4597 /* Skip fisttp, fist, fistp, fbstp, fistp. */
4598 if (i.tm.base_opcode == 0xdf
4599 && (i.tm.extension_opcode == 1
4600 || i.tm.extension_opcode == 2
4601 || i.tm.extension_opcode == 3
4602 || i.tm.extension_opcode == 6
4603 || i.tm.extension_opcode == 7))
4604 return 0;
4605
4606 return 1;
4607 }
4608 }
389d00a5
JB
4609 else if (i.tm.opcode_modifier.opcodespace == SPACE_0F)
4610 {
4611 /* bt, bts, btr, btc. */
4612 if (i.tm.base_opcode == 0xba
4613 && (i.tm.extension_opcode >= 4 && i.tm.extension_opcode <= 7))
4614 return 1;
4615
4616 /* cmpxchg8b, cmpxchg16b, xrstors, vmptrld. */
4617 if (i.tm.base_opcode == 0xc7
4618 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
4619 && (i.tm.extension_opcode == 1 || i.tm.extension_opcode == 3
4620 || i.tm.extension_opcode == 6))
4621 return 1;
4622
4623 /* fxrstor, ldmxcsr, xrstor. */
4624 if (i.tm.base_opcode == 0xae
4625 && (i.tm.extension_opcode == 1
4626 || i.tm.extension_opcode == 2
4627 || i.tm.extension_opcode == 5))
4628 return 1;
4629
4630 /* lgdt, lidt, lmsw. */
4631 if (i.tm.base_opcode == 0x01
4632 && (i.tm.extension_opcode == 2
4633 || i.tm.extension_opcode == 3
4634 || i.tm.extension_opcode == 6))
4635 return 1;
4636 }
ae531041
L
4637
4638 dest = i.operands - 1;
4639
4640 /* Check fake imm8 operand and 3 source operands. */
4641 if ((i.tm.opcode_modifier.immext
4642 || i.tm.opcode_modifier.vexsources == VEX3SOURCES)
4643 && i.types[dest].bitfield.imm8)
4644 dest--;
4645
389d00a5
JB
4646 /* add, or, adc, sbb, and, sub, xor, cmp, test, xchg. */
4647 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
ae531041
L
4648 && (base_opcode == 0x1
4649 || base_opcode == 0x9
4650 || base_opcode == 0x11
4651 || base_opcode == 0x19
4652 || base_opcode == 0x21
4653 || base_opcode == 0x29
4654 || base_opcode == 0x31
4655 || base_opcode == 0x39
389d00a5
JB
4656 || (base_opcode | 2) == 0x87))
4657 return 1;
4658
4659 /* xadd. */
4660 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4661 && base_opcode == 0xc1)
ae531041
L
4662 return 1;
4663
4664 /* Check for load instruction. */
4665 return (i.types[dest].bitfield.class != ClassNone
4666 || i.types[dest].bitfield.instance == Accum);
4667}
4668
4669/* Output lfence, 0xfaee8, after instruction. */
4670
4671static void
4672insert_lfence_after (void)
4673{
4674 if (lfence_after_load && load_insn_p ())
4675 {
a09f656b 4676 /* There are also two REP string instructions that require
4677 special treatment. Specifically, the compare string (CMPS)
4678 and scan string (SCAS) instructions set EFLAGS in a manner
4679 that depends on the data being compared/scanned. When used
4680 with a REP prefix, the number of iterations may therefore
4681 vary depending on this data. If the data is a program secret
4682 chosen by the adversary using an LVI method,
4683 then this data-dependent behavior may leak some aspect
4684 of the secret. */
4685 if (((i.tm.base_opcode | 0x1) == 0xa7
4686 || (i.tm.base_opcode | 0x1) == 0xaf)
4687 && i.prefix[REP_PREFIX])
4688 {
4689 as_warn (_("`%s` changes flags which would affect control flow behavior"),
4690 i.tm.name);
4691 }
ae531041
L
4692 char *p = frag_more (3);
4693 *p++ = 0xf;
4694 *p++ = 0xae;
4695 *p = 0xe8;
4696 }
4697}
4698
4699/* Output lfence, 0xfaee8, before instruction. */
4700
4701static void
4702insert_lfence_before (void)
4703{
4704 char *p;
4705
389d00a5 4706 if (i.tm.opcode_modifier.opcodespace != SPACE_BASE)
ae531041
L
4707 return;
4708
4709 if (i.tm.base_opcode == 0xff
4710 && (i.tm.extension_opcode == 2 || i.tm.extension_opcode == 4))
4711 {
4712 /* Insert lfence before indirect branch if needed. */
4713
4714 if (lfence_before_indirect_branch == lfence_branch_none)
4715 return;
4716
4717 if (i.operands != 1)
4718 abort ();
4719
4720 if (i.reg_operands == 1)
4721 {
4722 /* Indirect branch via register. Don't insert lfence with
4723 -mlfence-after-load=yes. */
4724 if (lfence_after_load
4725 || lfence_before_indirect_branch == lfence_branch_memory)
4726 return;
4727 }
4728 else if (i.mem_operands == 1
4729 && lfence_before_indirect_branch != lfence_branch_register)
4730 {
4731 as_warn (_("indirect `%s` with memory operand should be avoided"),
4732 i.tm.name);
4733 return;
4734 }
4735 else
4736 return;
4737
4738 if (last_insn.kind != last_insn_other
4739 && last_insn.seg == now_seg)
4740 {
4741 as_warn_where (last_insn.file, last_insn.line,
4742 _("`%s` skips -mlfence-before-indirect-branch on `%s`"),
4743 last_insn.name, i.tm.name);
4744 return;
4745 }
4746
4747 p = frag_more (3);
4748 *p++ = 0xf;
4749 *p++ = 0xae;
4750 *p = 0xe8;
4751 return;
4752 }
4753
503648e4 4754 /* Output or/not/shl and lfence before near ret. */
ae531041
L
4755 if (lfence_before_ret != lfence_before_ret_none
4756 && (i.tm.base_opcode == 0xc2
503648e4 4757 || i.tm.base_opcode == 0xc3))
ae531041
L
4758 {
4759 if (last_insn.kind != last_insn_other
4760 && last_insn.seg == now_seg)
4761 {
4762 as_warn_where (last_insn.file, last_insn.line,
4763 _("`%s` skips -mlfence-before-ret on `%s`"),
4764 last_insn.name, i.tm.name);
4765 return;
4766 }
a09f656b 4767
a09f656b 4768 /* Near ret ingore operand size override under CPU64. */
503648e4 4769 char prefix = flag_code == CODE_64BIT
4770 ? 0x48
4771 : i.prefix[DATA_PREFIX] ? 0x66 : 0x0;
a09f656b 4772
4773 if (lfence_before_ret == lfence_before_ret_not)
4774 {
4775 /* not: 0xf71424, may add prefix
4776 for operand size override or 64-bit code. */
4777 p = frag_more ((prefix ? 2 : 0) + 6 + 3);
4778 if (prefix)
4779 *p++ = prefix;
ae531041
L
4780 *p++ = 0xf7;
4781 *p++ = 0x14;
4782 *p++ = 0x24;
a09f656b 4783 if (prefix)
4784 *p++ = prefix;
ae531041
L
4785 *p++ = 0xf7;
4786 *p++ = 0x14;
4787 *p++ = 0x24;
4788 }
a09f656b 4789 else
4790 {
4791 p = frag_more ((prefix ? 1 : 0) + 4 + 3);
4792 if (prefix)
4793 *p++ = prefix;
4794 if (lfence_before_ret == lfence_before_ret_or)
4795 {
4796 /* or: 0x830c2400, may add prefix
4797 for operand size override or 64-bit code. */
4798 *p++ = 0x83;
4799 *p++ = 0x0c;
4800 }
4801 else
4802 {
4803 /* shl: 0xc1242400, may add prefix
4804 for operand size override or 64-bit code. */
4805 *p++ = 0xc1;
4806 *p++ = 0x24;
4807 }
4808
4809 *p++ = 0x24;
4810 *p++ = 0x0;
4811 }
4812
ae531041
L
4813 *p++ = 0xf;
4814 *p++ = 0xae;
4815 *p = 0xe8;
4816 }
4817}
4818
252b5132
RH
4819/* This is the guts of the machine-dependent assembler. LINE points to a
4820 machine dependent instruction. This function is supposed to emit
4821 the frags/bytes it assembles to. */
4822
4823void
65da13b5 4824md_assemble (char *line)
252b5132 4825{
40fb9820 4826 unsigned int j;
83b16ac6 4827 char mnemonic[MAX_MNEM_SIZE], mnem_suffix;
d3ce72d0 4828 const insn_template *t;
252b5132 4829
47926f60 4830 /* Initialize globals. */
252b5132 4831 memset (&i, '\0', sizeof (i));
ca5312a2 4832 i.rounding.type = rc_none;
252b5132 4833 for (j = 0; j < MAX_OPERANDS; j++)
1ae12ab7 4834 i.reloc[j] = NO_RELOC;
252b5132
RH
4835 memset (disp_expressions, '\0', sizeof (disp_expressions));
4836 memset (im_expressions, '\0', sizeof (im_expressions));
ce8a8b2f 4837 save_stack_p = save_stack;
252b5132
RH
4838
4839 /* First parse an instruction mnemonic & call i386_operand for the operands.
4840 We assume that the scrubber has arranged it so that line[0] is the valid
47926f60 4841 start of a (possibly prefixed) mnemonic. */
252b5132 4842
29b0f896
AM
4843 line = parse_insn (line, mnemonic);
4844 if (line == NULL)
4845 return;
83b16ac6 4846 mnem_suffix = i.suffix;
252b5132 4847
29b0f896 4848 line = parse_operands (line, mnemonic);
ee86248c 4849 this_operand = -1;
8325cc63
JB
4850 xfree (i.memop1_string);
4851 i.memop1_string = NULL;
29b0f896
AM
4852 if (line == NULL)
4853 return;
252b5132 4854
29b0f896
AM
4855 /* Now we've parsed the mnemonic into a set of templates, and have the
4856 operands at hand. */
4857
b630c145 4858 /* All Intel opcodes have reversed operands except for "bound", "enter",
c0e54661
JB
4859 "invlpg*", "monitor*", "mwait*", "tpause", "umwait", "pvalidate",
4860 "rmpadjust", and "rmpupdate". We also don't reverse intersegment "jmp"
4861 and "call" instructions with 2 immediate operands so that the immediate
4862 segment precedes the offset consistently in Intel and AT&T modes. */
4d456e3d
L
4863 if (intel_syntax
4864 && i.operands > 1
29b0f896 4865 && (strcmp (mnemonic, "bound") != 0)
c0e54661 4866 && (strncmp (mnemonic, "invlpg", 6) != 0)
d34049e8
ML
4867 && !startswith (mnemonic, "monitor")
4868 && !startswith (mnemonic, "mwait")
c0e54661 4869 && (strcmp (mnemonic, "pvalidate") != 0)
d34049e8 4870 && !startswith (mnemonic, "rmp")
b630c145
JB
4871 && (strcmp (mnemonic, "tpause") != 0)
4872 && (strcmp (mnemonic, "umwait") != 0)
40fb9820
L
4873 && !(operand_type_check (i.types[0], imm)
4874 && operand_type_check (i.types[1], imm)))
29b0f896
AM
4875 swap_operands ();
4876
ec56d5c0
JB
4877 /* The order of the immediates should be reversed
4878 for 2 immediates extrq and insertq instructions */
4879 if (i.imm_operands == 2
4880 && (strcmp (mnemonic, "extrq") == 0
4881 || strcmp (mnemonic, "insertq") == 0))
4882 swap_2_operands (0, 1);
4883
29b0f896
AM
4884 if (i.imm_operands)
4885 optimize_imm ();
4886
9386188e
JB
4887 if (i.disp_operands && !want_disp32 (current_templates->start)
4888 && (!current_templates->start->opcode_modifier.jump
4889 || i.jumpabsolute || i.types[0].bitfield.baseindex))
cce08655
JB
4890 {
4891 for (j = 0; j < i.operands; ++j)
4892 {
4893 const expressionS *exp = i.op[j].disps;
4894
4895 if (!operand_type_check (i.types[j], disp))
4896 continue;
4897
4898 if (exp->X_op != O_constant)
4899 continue;
4900
4901 /* Since displacement is signed extended to 64bit, don't allow
a775efc8 4902 disp32 if it is out of range. */
cce08655
JB
4903 if (fits_in_signed_long (exp->X_add_number))
4904 continue;
4905
a775efc8 4906 i.types[j].bitfield.disp32 = 0;
cce08655
JB
4907 if (i.types[j].bitfield.baseindex)
4908 {
ffa5352c
NC
4909 char number_buf[128];
4910
4911 /* Coded this way in order to allow for ease of translation. */
4912 sprintf_vma (number_buf, exp->X_add_number);
4913 as_bad (_("0x%s out of range of signed 32bit displacement"),
4914 number_buf);
cce08655
JB
4915 return;
4916 }
4917 }
4918 }
4919
b300c311
L
4920 /* Don't optimize displacement for movabs since it only takes 64bit
4921 displacement. */
4922 if (i.disp_operands
1a42a9fe 4923 && i.disp_encoding <= disp_encoding_8bit
862be3fb
L
4924 && (flag_code != CODE_64BIT
4925 || strcmp (mnemonic, "movabs") != 0))
4926 optimize_disp ();
29b0f896
AM
4927
4928 /* Next, we find a template that matches the given insn,
4929 making sure the overlap of the given operands types is consistent
4930 with the template operand types. */
252b5132 4931
83b16ac6 4932 if (!(t = match_template (mnem_suffix)))
29b0f896 4933 return;
252b5132 4934
7bab8ab5 4935 if (sse_check != check_none
ffb86450
JB
4936 /* The opcode space check isn't strictly needed; it's there only to
4937 bypass the logic below when easily possible. */
4938 && t->opcode_modifier.opcodespace >= SPACE_0F
4939 && t->opcode_modifier.opcodespace <= SPACE_0F3A
4940 && !i.tm.cpu_flags.bitfield.cpusse4a
4941 && !is_any_vex_encoding (t))
daf50ae7 4942 {
ffb86450
JB
4943 bool simd = false;
4944
4945 for (j = 0; j < t->operands; ++j)
4946 {
4947 if (t->operand_types[j].bitfield.class == RegMMX)
4948 break;
4949 if (t->operand_types[j].bitfield.class == RegSIMD)
4950 simd = true;
4951 }
4952
4953 if (j >= t->operands && simd)
4954 (sse_check == check_warning
4955 ? as_warn
4956 : as_bad) (_("SSE instruction `%s' is used"), i.tm.name);
daf50ae7
L
4957 }
4958
40fb9820 4959 if (i.tm.opcode_modifier.fwait)
29b0f896
AM
4960 if (!add_prefix (FWAIT_OPCODE))
4961 return;
252b5132 4962
d5de92cf 4963 /* Check if REP prefix is OK. */
742732c7 4964 if (i.rep_prefix && i.tm.opcode_modifier.prefixok != PrefixRep)
d5de92cf
L
4965 {
4966 as_bad (_("invalid instruction `%s' after `%s'"),
4967 i.tm.name, i.rep_prefix);
4968 return;
4969 }
4970
c1ba0266
L
4971 /* Check for lock without a lockable instruction. Destination operand
4972 must be memory unless it is xchg (0x86). */
c32fa91d 4973 if (i.prefix[LOCK_PREFIX]
742732c7 4974 && (i.tm.opcode_modifier.prefixok < PrefixLock
c1ba0266
L
4975 || i.mem_operands == 0
4976 || (i.tm.base_opcode != 0x86
8dc0818e 4977 && !(i.flags[i.operands - 1] & Operand_Mem))))
c32fa91d
L
4978 {
4979 as_bad (_("expecting lockable instruction after `lock'"));
4980 return;
4981 }
4982
40d231b4
JB
4983 /* Check for data size prefix on VEX/XOP/EVEX encoded and SIMD insns. */
4984 if (i.prefix[DATA_PREFIX]
4985 && (is_any_vex_encoding (&i.tm)
4986 || i.tm.operand_types[i.imm_operands].bitfield.class >= RegMMX
4987 || i.tm.operand_types[i.imm_operands + 1].bitfield.class >= RegMMX))
7a8655d2
JB
4988 {
4989 as_bad (_("data size prefix invalid with `%s'"), i.tm.name);
4990 return;
4991 }
4992
42164a71 4993 /* Check if HLE prefix is OK. */
165de32a 4994 if (i.hle_prefix && !check_hle ())
42164a71
L
4995 return;
4996
7e8b059b
L
4997 /* Check BND prefix. */
4998 if (i.bnd_prefix && !i.tm.opcode_modifier.bndprefixok)
4999 as_bad (_("expecting valid branch instruction after `bnd'"));
5000
04ef582a 5001 /* Check NOTRACK prefix. */
742732c7 5002 if (i.notrack_prefix && i.tm.opcode_modifier.prefixok != PrefixNoTrack)
9fef80d6 5003 as_bad (_("expecting indirect branch instruction after `notrack'"));
04ef582a 5004
327e8c42
JB
5005 if (i.tm.cpu_flags.bitfield.cpumpx)
5006 {
5007 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
5008 as_bad (_("32-bit address isn't allowed in 64-bit MPX instructions."));
5009 else if (flag_code != CODE_16BIT
5010 ? i.prefix[ADDR_PREFIX]
5011 : i.mem_operands && !i.prefix[ADDR_PREFIX])
5012 as_bad (_("16-bit address isn't allowed in MPX instructions"));
5013 }
7e8b059b
L
5014
5015 /* Insert BND prefix. */
76d3a78a
JB
5016 if (add_bnd_prefix && i.tm.opcode_modifier.bndprefixok)
5017 {
5018 if (!i.prefix[BND_PREFIX])
5019 add_prefix (BND_PREFIX_OPCODE);
5020 else if (i.prefix[BND_PREFIX] != BND_PREFIX_OPCODE)
5021 {
5022 as_warn (_("replacing `rep'/`repe' prefix by `bnd'"));
5023 i.prefix[BND_PREFIX] = BND_PREFIX_OPCODE;
5024 }
5025 }
7e8b059b 5026
29b0f896 5027 /* Check string instruction segment overrides. */
51c8edf6 5028 if (i.tm.opcode_modifier.isstring >= IS_STRING_ES_OP0)
29b0f896 5029 {
51c8edf6 5030 gas_assert (i.mem_operands);
29b0f896 5031 if (!check_string ())
5dd0794d 5032 return;
fc0763e6 5033 i.disp_operands = 0;
29b0f896 5034 }
5dd0794d 5035
b6f8c7c4
L
5036 if (optimize && !i.no_optimize && i.tm.opcode_modifier.optimize)
5037 optimize_encoding ();
5038
c8480b58
L
5039 if (use_unaligned_vector_move)
5040 encode_with_unaligned_vector_move ();
5041
29b0f896
AM
5042 if (!process_suffix ())
5043 return;
e413e4e9 5044
921eafea 5045 /* Update operand types and check extended states. */
bc0844ae 5046 for (j = 0; j < i.operands; j++)
921eafea
L
5047 {
5048 i.types[j] = operand_type_and (i.types[j], i.tm.operand_types[j]);
3d70986f 5049 switch (i.tm.operand_types[j].bitfield.class)
921eafea
L
5050 {
5051 default:
5052 break;
5053 case RegMMX:
5054 i.xstate |= xstate_mmx;
5055 break;
5056 case RegMask:
32930e4e 5057 i.xstate |= xstate_mask;
921eafea
L
5058 break;
5059 case RegSIMD:
3d70986f 5060 if (i.tm.operand_types[j].bitfield.tmmword)
921eafea 5061 i.xstate |= xstate_tmm;
3d70986f 5062 else if (i.tm.operand_types[j].bitfield.zmmword)
921eafea 5063 i.xstate |= xstate_zmm;
3d70986f 5064 else if (i.tm.operand_types[j].bitfield.ymmword)
921eafea 5065 i.xstate |= xstate_ymm;
3d70986f 5066 else if (i.tm.operand_types[j].bitfield.xmmword)
921eafea
L
5067 i.xstate |= xstate_xmm;
5068 break;
5069 }
5070 }
bc0844ae 5071
29b0f896
AM
5072 /* Make still unresolved immediate matches conform to size of immediate
5073 given in i.suffix. */
5074 if (!finalize_imm ())
5075 return;
252b5132 5076
40fb9820 5077 if (i.types[0].bitfield.imm1)
29b0f896 5078 i.imm_operands = 0; /* kludge for shift insns. */
252b5132 5079
9afe6eb8
L
5080 /* We only need to check those implicit registers for instructions
5081 with 3 operands or less. */
5082 if (i.operands <= 3)
5083 for (j = 0; j < i.operands; j++)
75e5731b
JB
5084 if (i.types[j].bitfield.instance != InstanceNone
5085 && !i.types[j].bitfield.xmmword)
9afe6eb8 5086 i.reg_operands--;
40fb9820 5087
29b0f896
AM
5088 /* For insns with operands there are more diddles to do to the opcode. */
5089 if (i.operands)
5090 {
5091 if (!process_operands ())
5092 return;
5093 }
8c190ce0 5094 else if (!quiet_warnings && i.tm.opcode_modifier.ugh)
29b0f896
AM
5095 {
5096 /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc. */
5097 as_warn (_("translating to `%sp'"), i.tm.name);
5098 }
252b5132 5099
7a8655d2 5100 if (is_any_vex_encoding (&i.tm))
9e5e5283 5101 {
c1dc7af5 5102 if (!cpu_arch_flags.bitfield.cpui286)
9e5e5283 5103 {
c1dc7af5 5104 as_bad (_("instruction `%s' isn't supported outside of protected mode."),
9e5e5283
L
5105 i.tm.name);
5106 return;
5107 }
c0f3af97 5108
0b9404fd
JB
5109 /* Check for explicit REX prefix. */
5110 if (i.prefix[REX_PREFIX] || i.rex_encoding)
5111 {
5112 as_bad (_("REX prefix invalid with `%s'"), i.tm.name);
5113 return;
5114 }
5115
9e5e5283
L
5116 if (i.tm.opcode_modifier.vex)
5117 build_vex_prefix (t);
5118 else
5119 build_evex_prefix ();
0b9404fd
JB
5120
5121 /* The individual REX.RXBW bits got consumed. */
5122 i.rex &= REX_OPCODE;
9e5e5283 5123 }
43234a1e 5124
5dd85c99
SP
5125 /* Handle conversion of 'int $3' --> special int3 insn. XOP or FMA4
5126 instructions may define INT_OPCODE as well, so avoid this corner
5127 case for those instructions that use MODRM. */
389d00a5
JB
5128 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
5129 && i.tm.base_opcode == INT_OPCODE
a6461c02
SP
5130 && !i.tm.opcode_modifier.modrm
5131 && i.op[0].imms->X_add_number == 3)
29b0f896
AM
5132 {
5133 i.tm.base_opcode = INT3_OPCODE;
5134 i.imm_operands = 0;
5135 }
252b5132 5136
0cfa3eb3
JB
5137 if ((i.tm.opcode_modifier.jump == JUMP
5138 || i.tm.opcode_modifier.jump == JUMP_BYTE
5139 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896
AM
5140 && i.op[0].disps->X_op == O_constant)
5141 {
5142 /* Convert "jmp constant" (and "call constant") to a jump (call) to
5143 the absolute address given by the constant. Since ix86 jumps and
5144 calls are pc relative, we need to generate a reloc. */
5145 i.op[0].disps->X_add_symbol = &abs_symbol;
5146 i.op[0].disps->X_op = O_symbol;
5147 }
252b5132 5148
29b0f896
AM
5149 /* For 8 bit registers we need an empty rex prefix. Also if the
5150 instruction already has a prefix, we need to convert old
5151 registers to new ones. */
773f551c 5152
bab6aec1 5153 if ((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte
29b0f896 5154 && (i.op[0].regs->reg_flags & RegRex64) != 0)
bab6aec1 5155 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte
29b0f896 5156 && (i.op[1].regs->reg_flags & RegRex64) != 0)
bab6aec1
JB
5157 || (((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte)
5158 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte))
29b0f896
AM
5159 && i.rex != 0))
5160 {
5161 int x;
726c5dcd 5162
29b0f896
AM
5163 i.rex |= REX_OPCODE;
5164 for (x = 0; x < 2; x++)
5165 {
5166 /* Look for 8 bit operand that uses old registers. */
bab6aec1 5167 if (i.types[x].bitfield.class == Reg && i.types[x].bitfield.byte
29b0f896 5168 && (i.op[x].regs->reg_flags & RegRex64) == 0)
773f551c 5169 {
3f93af61 5170 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
29b0f896
AM
5171 /* In case it is "hi" register, give up. */
5172 if (i.op[x].regs->reg_num > 3)
a540244d 5173 as_bad (_("can't encode register '%s%s' in an "
4eed87de 5174 "instruction requiring REX prefix."),
a540244d 5175 register_prefix, i.op[x].regs->reg_name);
773f551c 5176
29b0f896
AM
5177 /* Otherwise it is equivalent to the extended register.
5178 Since the encoding doesn't change this is merely
5179 cosmetic cleanup for debug output. */
5180
5181 i.op[x].regs = i.op[x].regs + 8;
773f551c 5182 }
29b0f896
AM
5183 }
5184 }
773f551c 5185
6b6b6807
L
5186 if (i.rex == 0 && i.rex_encoding)
5187 {
5188 /* Check if we can add a REX_OPCODE byte. Look for 8 bit operand
3f93af61 5189 that uses legacy register. If it is "hi" register, don't add
6b6b6807
L
5190 the REX_OPCODE byte. */
5191 int x;
5192 for (x = 0; x < 2; x++)
bab6aec1 5193 if (i.types[x].bitfield.class == Reg
6b6b6807
L
5194 && i.types[x].bitfield.byte
5195 && (i.op[x].regs->reg_flags & RegRex64) == 0
5196 && i.op[x].regs->reg_num > 3)
5197 {
3f93af61 5198 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
5b7c81bd 5199 i.rex_encoding = false;
6b6b6807
L
5200 break;
5201 }
5202
5203 if (i.rex_encoding)
5204 i.rex = REX_OPCODE;
5205 }
5206
7ab9ffdd 5207 if (i.rex != 0)
29b0f896
AM
5208 add_prefix (REX_OPCODE | i.rex);
5209
ae531041
L
5210 insert_lfence_before ();
5211
29b0f896
AM
5212 /* We are ready to output the insn. */
5213 output_insn ();
e379e5f3 5214
ae531041
L
5215 insert_lfence_after ();
5216
e379e5f3
L
5217 last_insn.seg = now_seg;
5218
5219 if (i.tm.opcode_modifier.isprefix)
5220 {
5221 last_insn.kind = last_insn_prefix;
5222 last_insn.name = i.tm.name;
5223 last_insn.file = as_where (&last_insn.line);
5224 }
5225 else
5226 last_insn.kind = last_insn_other;
29b0f896
AM
5227}
5228
5229static char *
e3bb37b5 5230parse_insn (char *line, char *mnemonic)
29b0f896
AM
5231{
5232 char *l = line;
5233 char *token_start = l;
5234 char *mnem_p;
5c6af06e 5235 int supported;
d3ce72d0 5236 const insn_template *t;
b6169b20 5237 char *dot_p = NULL;
29b0f896 5238
29b0f896
AM
5239 while (1)
5240 {
5241 mnem_p = mnemonic;
5242 while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
5243 {
b6169b20
L
5244 if (*mnem_p == '.')
5245 dot_p = mnem_p;
29b0f896
AM
5246 mnem_p++;
5247 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
45288df1 5248 {
29b0f896
AM
5249 as_bad (_("no such instruction: `%s'"), token_start);
5250 return NULL;
5251 }
5252 l++;
5253 }
5254 if (!is_space_char (*l)
5255 && *l != END_OF_INSN
e44823cf
JB
5256 && (intel_syntax
5257 || (*l != PREFIX_SEPARATOR
5258 && *l != ',')))
29b0f896
AM
5259 {
5260 as_bad (_("invalid character %s in mnemonic"),
5261 output_invalid (*l));
5262 return NULL;
5263 }
5264 if (token_start == l)
5265 {
e44823cf 5266 if (!intel_syntax && *l == PREFIX_SEPARATOR)
29b0f896
AM
5267 as_bad (_("expecting prefix; got nothing"));
5268 else
5269 as_bad (_("expecting mnemonic; got nothing"));
5270 return NULL;
5271 }
45288df1 5272
29b0f896 5273 /* Look up instruction (or prefix) via hash table. */
629310ab 5274 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
47926f60 5275
29b0f896
AM
5276 if (*l != END_OF_INSN
5277 && (!is_space_char (*l) || l[1] != END_OF_INSN)
5278 && current_templates
40fb9820 5279 && current_templates->start->opcode_modifier.isprefix)
29b0f896 5280 {
c6fb90c8 5281 if (!cpu_flags_check_cpu64 (current_templates->start->cpu_flags))
2dd88dca
JB
5282 {
5283 as_bad ((flag_code != CODE_64BIT
5284 ? _("`%s' is only supported in 64-bit mode")
5285 : _("`%s' is not supported in 64-bit mode")),
5286 current_templates->start->name);
5287 return NULL;
5288 }
29b0f896
AM
5289 /* If we are in 16-bit mode, do not allow addr16 or data16.
5290 Similarly, in 32-bit mode, do not allow addr32 or data32. */
673fe0f0
JB
5291 if ((current_templates->start->opcode_modifier.size == SIZE16
5292 || current_templates->start->opcode_modifier.size == SIZE32)
29b0f896 5293 && flag_code != CODE_64BIT
673fe0f0 5294 && ((current_templates->start->opcode_modifier.size == SIZE32)
29b0f896
AM
5295 ^ (flag_code == CODE_16BIT)))
5296 {
5297 as_bad (_("redundant %s prefix"),
5298 current_templates->start->name);
5299 return NULL;
45288df1 5300 }
31184569
JB
5301
5302 if (current_templates->start->base_opcode == PSEUDO_PREFIX)
29b0f896 5303 {
86fa6981 5304 /* Handle pseudo prefixes. */
31184569 5305 switch (current_templates->start->extension_opcode)
86fa6981 5306 {
41eb8e88 5307 case Prefix_Disp8:
86fa6981
L
5308 /* {disp8} */
5309 i.disp_encoding = disp_encoding_8bit;
5310 break;
41eb8e88
L
5311 case Prefix_Disp16:
5312 /* {disp16} */
5313 i.disp_encoding = disp_encoding_16bit;
5314 break;
5315 case Prefix_Disp32:
86fa6981
L
5316 /* {disp32} */
5317 i.disp_encoding = disp_encoding_32bit;
5318 break;
41eb8e88 5319 case Prefix_Load:
86fa6981
L
5320 /* {load} */
5321 i.dir_encoding = dir_encoding_load;
5322 break;
41eb8e88 5323 case Prefix_Store:
86fa6981
L
5324 /* {store} */
5325 i.dir_encoding = dir_encoding_store;
5326 break;
41eb8e88 5327 case Prefix_VEX:
42e04b36
L
5328 /* {vex} */
5329 i.vec_encoding = vex_encoding_vex;
86fa6981 5330 break;
41eb8e88 5331 case Prefix_VEX3:
86fa6981
L
5332 /* {vex3} */
5333 i.vec_encoding = vex_encoding_vex3;
5334 break;
41eb8e88 5335 case Prefix_EVEX:
86fa6981
L
5336 /* {evex} */
5337 i.vec_encoding = vex_encoding_evex;
5338 break;
41eb8e88 5339 case Prefix_REX:
6b6b6807 5340 /* {rex} */
5b7c81bd 5341 i.rex_encoding = true;
6b6b6807 5342 break;
41eb8e88 5343 case Prefix_NoOptimize:
b6f8c7c4 5344 /* {nooptimize} */
5b7c81bd 5345 i.no_optimize = true;
b6f8c7c4 5346 break;
86fa6981
L
5347 default:
5348 abort ();
5349 }
5350 }
5351 else
5352 {
5353 /* Add prefix, checking for repeated prefixes. */
4e9ac44a 5354 switch (add_prefix (current_templates->start->base_opcode))
86fa6981 5355 {
4e9ac44a
L
5356 case PREFIX_EXIST:
5357 return NULL;
5358 case PREFIX_DS:
d777820b 5359 if (current_templates->start->cpu_flags.bitfield.cpuibt)
4e9ac44a
L
5360 i.notrack_prefix = current_templates->start->name;
5361 break;
5362 case PREFIX_REP:
5363 if (current_templates->start->cpu_flags.bitfield.cpuhle)
5364 i.hle_prefix = current_templates->start->name;
5365 else if (current_templates->start->cpu_flags.bitfield.cpumpx)
5366 i.bnd_prefix = current_templates->start->name;
5367 else
5368 i.rep_prefix = current_templates->start->name;
5369 break;
5370 default:
5371 break;
86fa6981 5372 }
29b0f896
AM
5373 }
5374 /* Skip past PREFIX_SEPARATOR and reset token_start. */
5375 token_start = ++l;
5376 }
5377 else
5378 break;
5379 }
45288df1 5380
30a55f88 5381 if (!current_templates)
b6169b20 5382 {
07d5e953
JB
5383 /* Deprecated functionality (new code should use pseudo-prefixes instead):
5384 Check if we should swap operand or force 32bit displacement in
f8a5c266 5385 encoding. */
30a55f88 5386 if (mnem_p - 2 == dot_p && dot_p[1] == 's')
64c49ab3 5387 i.dir_encoding = dir_encoding_swap;
8d63c93e 5388 else if (mnem_p - 3 == dot_p
a501d77e
L
5389 && dot_p[1] == 'd'
5390 && dot_p[2] == '8')
5391 i.disp_encoding = disp_encoding_8bit;
8d63c93e 5392 else if (mnem_p - 4 == dot_p
f8a5c266
L
5393 && dot_p[1] == 'd'
5394 && dot_p[2] == '3'
5395 && dot_p[3] == '2')
a501d77e 5396 i.disp_encoding = disp_encoding_32bit;
30a55f88
L
5397 else
5398 goto check_suffix;
5399 mnem_p = dot_p;
5400 *dot_p = '\0';
629310ab 5401 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
b6169b20
L
5402 }
5403
29b0f896
AM
5404 if (!current_templates)
5405 {
dc1e8a47 5406 check_suffix:
1c529385 5407 if (mnem_p > mnemonic)
29b0f896 5408 {
1c529385
LH
5409 /* See if we can get a match by trimming off a suffix. */
5410 switch (mnem_p[-1])
29b0f896 5411 {
1c529385
LH
5412 case WORD_MNEM_SUFFIX:
5413 if (intel_syntax && (intel_float_operand (mnemonic) & 2))
29b0f896
AM
5414 i.suffix = SHORT_MNEM_SUFFIX;
5415 else
1c529385
LH
5416 /* Fall through. */
5417 case BYTE_MNEM_SUFFIX:
5418 case QWORD_MNEM_SUFFIX:
5419 i.suffix = mnem_p[-1];
29b0f896 5420 mnem_p[-1] = '\0';
fe0e921f
AM
5421 current_templates
5422 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5423 break;
5424 case SHORT_MNEM_SUFFIX:
5425 case LONG_MNEM_SUFFIX:
5426 if (!intel_syntax)
5427 {
5428 i.suffix = mnem_p[-1];
5429 mnem_p[-1] = '\0';
fe0e921f
AM
5430 current_templates
5431 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5432 }
5433 break;
5434
5435 /* Intel Syntax. */
5436 case 'd':
5437 if (intel_syntax)
5438 {
5439 if (intel_float_operand (mnemonic) == 1)
5440 i.suffix = SHORT_MNEM_SUFFIX;
5441 else
5442 i.suffix = LONG_MNEM_SUFFIX;
5443 mnem_p[-1] = '\0';
fe0e921f
AM
5444 current_templates
5445 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5446 }
5447 break;
29b0f896 5448 }
29b0f896 5449 }
1c529385 5450
29b0f896
AM
5451 if (!current_templates)
5452 {
5453 as_bad (_("no such instruction: `%s'"), token_start);
5454 return NULL;
5455 }
5456 }
252b5132 5457
0cfa3eb3
JB
5458 if (current_templates->start->opcode_modifier.jump == JUMP
5459 || current_templates->start->opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
5460 {
5461 /* Check for a branch hint. We allow ",pt" and ",pn" for
5462 predict taken and predict not taken respectively.
5463 I'm not sure that branch hints actually do anything on loop
5464 and jcxz insns (JumpByte) for current Pentium4 chips. They
5465 may work in the future and it doesn't hurt to accept them
5466 now. */
5467 if (l[0] == ',' && l[1] == 'p')
5468 {
5469 if (l[2] == 't')
5470 {
5471 if (!add_prefix (DS_PREFIX_OPCODE))
5472 return NULL;
5473 l += 3;
5474 }
5475 else if (l[2] == 'n')
5476 {
5477 if (!add_prefix (CS_PREFIX_OPCODE))
5478 return NULL;
5479 l += 3;
5480 }
5481 }
5482 }
5483 /* Any other comma loses. */
5484 if (*l == ',')
5485 {
5486 as_bad (_("invalid character %s in mnemonic"),
5487 output_invalid (*l));
5488 return NULL;
5489 }
252b5132 5490
29b0f896 5491 /* Check if instruction is supported on specified architecture. */
5c6af06e
JB
5492 supported = 0;
5493 for (t = current_templates->start; t < current_templates->end; ++t)
5494 {
c0f3af97
L
5495 supported |= cpu_flags_match (t);
5496 if (supported == CPU_FLAGS_PERFECT_MATCH)
548d0ee6
JB
5497 {
5498 if (!cpu_arch_flags.bitfield.cpui386 && (flag_code != CODE_16BIT))
5499 as_warn (_("use .code16 to ensure correct addressing mode"));
3629bb00 5500
548d0ee6
JB
5501 return l;
5502 }
29b0f896 5503 }
3629bb00 5504
548d0ee6
JB
5505 if (!(supported & CPU_FLAGS_64BIT_MATCH))
5506 as_bad (flag_code == CODE_64BIT
5507 ? _("`%s' is not supported in 64-bit mode")
5508 : _("`%s' is only supported in 64-bit mode"),
5509 current_templates->start->name);
5510 else
5511 as_bad (_("`%s' is not supported on `%s%s'"),
5512 current_templates->start->name,
5513 cpu_arch_name ? cpu_arch_name : default_arch,
5514 cpu_sub_arch_name ? cpu_sub_arch_name : "");
252b5132 5515
548d0ee6 5516 return NULL;
29b0f896 5517}
252b5132 5518
29b0f896 5519static char *
e3bb37b5 5520parse_operands (char *l, const char *mnemonic)
29b0f896
AM
5521{
5522 char *token_start;
3138f287 5523
29b0f896
AM
5524 /* 1 if operand is pending after ','. */
5525 unsigned int expecting_operand = 0;
252b5132 5526
29b0f896
AM
5527 while (*l != END_OF_INSN)
5528 {
e68c3d59
JB
5529 /* Non-zero if operand parens not balanced. */
5530 unsigned int paren_not_balanced = 0;
5531 /* True if inside double quotes. */
5532 bool in_quotes = false;
5533
29b0f896
AM
5534 /* Skip optional white space before operand. */
5535 if (is_space_char (*l))
5536 ++l;
d02603dc 5537 if (!is_operand_char (*l) && *l != END_OF_INSN && *l != '"')
29b0f896
AM
5538 {
5539 as_bad (_("invalid character %s before operand %d"),
5540 output_invalid (*l),
5541 i.operands + 1);
5542 return NULL;
5543 }
d02603dc 5544 token_start = l; /* After white space. */
e68c3d59 5545 while (in_quotes || paren_not_balanced || *l != ',')
29b0f896
AM
5546 {
5547 if (*l == END_OF_INSN)
5548 {
e68c3d59
JB
5549 if (in_quotes)
5550 {
5551 as_bad (_("unbalanced double quotes in operand %d."),
5552 i.operands + 1);
5553 return NULL;
5554 }
29b0f896
AM
5555 if (paren_not_balanced)
5556 {
98ff9f1c
JB
5557 know (!intel_syntax);
5558 as_bad (_("unbalanced parenthesis in operand %d."),
5559 i.operands + 1);
29b0f896
AM
5560 return NULL;
5561 }
5562 else
5563 break; /* we are done */
5564 }
e68c3d59
JB
5565 else if (*l == '\\' && l[1] == '"')
5566 ++l;
5567 else if (*l == '"')
5568 in_quotes = !in_quotes;
5569 else if (!in_quotes && !is_operand_char (*l) && !is_space_char (*l))
29b0f896
AM
5570 {
5571 as_bad (_("invalid character %s in operand %d"),
5572 output_invalid (*l),
5573 i.operands + 1);
5574 return NULL;
5575 }
e68c3d59 5576 if (!intel_syntax && !in_quotes)
29b0f896
AM
5577 {
5578 if (*l == '(')
5579 ++paren_not_balanced;
5580 if (*l == ')')
5581 --paren_not_balanced;
5582 }
29b0f896
AM
5583 l++;
5584 }
5585 if (l != token_start)
5586 { /* Yes, we've read in another operand. */
5587 unsigned int operand_ok;
5588 this_operand = i.operands++;
5589 if (i.operands > MAX_OPERANDS)
5590 {
5591 as_bad (_("spurious operands; (%d operands/instruction max)"),
5592 MAX_OPERANDS);
5593 return NULL;
5594 }
9d46ce34 5595 i.types[this_operand].bitfield.unspecified = 1;
29b0f896
AM
5596 /* Now parse operand adding info to 'i' as we go along. */
5597 END_STRING_AND_SAVE (l);
5598
1286ab78
L
5599 if (i.mem_operands > 1)
5600 {
5601 as_bad (_("too many memory references for `%s'"),
5602 mnemonic);
5603 return 0;
5604 }
5605
29b0f896
AM
5606 if (intel_syntax)
5607 operand_ok =
5608 i386_intel_operand (token_start,
5609 intel_float_operand (mnemonic));
5610 else
a7619375 5611 operand_ok = i386_att_operand (token_start);
29b0f896
AM
5612
5613 RESTORE_END_STRING (l);
5614 if (!operand_ok)
5615 return NULL;
5616 }
5617 else
5618 {
5619 if (expecting_operand)
5620 {
5621 expecting_operand_after_comma:
5622 as_bad (_("expecting operand after ','; got nothing"));
5623 return NULL;
5624 }
5625 if (*l == ',')
5626 {
5627 as_bad (_("expecting operand before ','; got nothing"));
5628 return NULL;
5629 }
5630 }
7f3f1ea2 5631
29b0f896
AM
5632 /* Now *l must be either ',' or END_OF_INSN. */
5633 if (*l == ',')
5634 {
5635 if (*++l == END_OF_INSN)
5636 {
5637 /* Just skip it, if it's \n complain. */
5638 goto expecting_operand_after_comma;
5639 }
5640 expecting_operand = 1;
5641 }
5642 }
5643 return l;
5644}
7f3f1ea2 5645
050dfa73 5646static void
783c187b 5647swap_2_operands (unsigned int xchg1, unsigned int xchg2)
050dfa73
MM
5648{
5649 union i386_op temp_op;
40fb9820 5650 i386_operand_type temp_type;
c48dadc9 5651 unsigned int temp_flags;
050dfa73 5652 enum bfd_reloc_code_real temp_reloc;
4eed87de 5653
050dfa73
MM
5654 temp_type = i.types[xchg2];
5655 i.types[xchg2] = i.types[xchg1];
5656 i.types[xchg1] = temp_type;
c48dadc9
JB
5657
5658 temp_flags = i.flags[xchg2];
5659 i.flags[xchg2] = i.flags[xchg1];
5660 i.flags[xchg1] = temp_flags;
5661
050dfa73
MM
5662 temp_op = i.op[xchg2];
5663 i.op[xchg2] = i.op[xchg1];
5664 i.op[xchg1] = temp_op;
c48dadc9 5665
050dfa73
MM
5666 temp_reloc = i.reloc[xchg2];
5667 i.reloc[xchg2] = i.reloc[xchg1];
5668 i.reloc[xchg1] = temp_reloc;
43234a1e 5669
6225c532 5670 if (i.mask.reg)
43234a1e 5671 {
6225c532
JB
5672 if (i.mask.operand == xchg1)
5673 i.mask.operand = xchg2;
5674 else if (i.mask.operand == xchg2)
5675 i.mask.operand = xchg1;
43234a1e 5676 }
a5748e0d 5677 if (i.broadcast.type || i.broadcast.bytes)
43234a1e 5678 {
5273a3cd
JB
5679 if (i.broadcast.operand == xchg1)
5680 i.broadcast.operand = xchg2;
5681 else if (i.broadcast.operand == xchg2)
5682 i.broadcast.operand = xchg1;
43234a1e 5683 }
050dfa73
MM
5684}
5685
29b0f896 5686static void
e3bb37b5 5687swap_operands (void)
29b0f896 5688{
b7c61d9a 5689 switch (i.operands)
050dfa73 5690 {
c0f3af97 5691 case 5:
b7c61d9a 5692 case 4:
4d456e3d 5693 swap_2_operands (1, i.operands - 2);
1a0670f3 5694 /* Fall through. */
b7c61d9a
L
5695 case 3:
5696 case 2:
4d456e3d 5697 swap_2_operands (0, i.operands - 1);
b7c61d9a
L
5698 break;
5699 default:
5700 abort ();
29b0f896 5701 }
29b0f896
AM
5702
5703 if (i.mem_operands == 2)
5704 {
5e042380 5705 const reg_entry *temp_seg;
29b0f896
AM
5706 temp_seg = i.seg[0];
5707 i.seg[0] = i.seg[1];
5708 i.seg[1] = temp_seg;
5709 }
5710}
252b5132 5711
29b0f896
AM
5712/* Try to ensure constant immediates are represented in the smallest
5713 opcode possible. */
5714static void
e3bb37b5 5715optimize_imm (void)
29b0f896
AM
5716{
5717 char guess_suffix = 0;
5718 int op;
252b5132 5719
29b0f896
AM
5720 if (i.suffix)
5721 guess_suffix = i.suffix;
5722 else if (i.reg_operands)
5723 {
5724 /* Figure out a suffix from the last register operand specified.
75e5731b
JB
5725 We can't do this properly yet, i.e. excluding special register
5726 instances, but the following works for instructions with
5727 immediates. In any case, we can't set i.suffix yet. */
29b0f896 5728 for (op = i.operands; --op >= 0;)
bab6aec1
JB
5729 if (i.types[op].bitfield.class != Reg)
5730 continue;
5731 else if (i.types[op].bitfield.byte)
7ab9ffdd 5732 {
40fb9820
L
5733 guess_suffix = BYTE_MNEM_SUFFIX;
5734 break;
5735 }
bab6aec1 5736 else if (i.types[op].bitfield.word)
252b5132 5737 {
40fb9820
L
5738 guess_suffix = WORD_MNEM_SUFFIX;
5739 break;
5740 }
bab6aec1 5741 else if (i.types[op].bitfield.dword)
40fb9820
L
5742 {
5743 guess_suffix = LONG_MNEM_SUFFIX;
5744 break;
5745 }
bab6aec1 5746 else if (i.types[op].bitfield.qword)
40fb9820
L
5747 {
5748 guess_suffix = QWORD_MNEM_SUFFIX;
29b0f896 5749 break;
252b5132 5750 }
29b0f896
AM
5751 }
5752 else if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
5753 guess_suffix = WORD_MNEM_SUFFIX;
5754
5755 for (op = i.operands; --op >= 0;)
40fb9820 5756 if (operand_type_check (i.types[op], imm))
29b0f896
AM
5757 {
5758 switch (i.op[op].imms->X_op)
252b5132 5759 {
29b0f896
AM
5760 case O_constant:
5761 /* If a suffix is given, this operand may be shortened. */
5762 switch (guess_suffix)
252b5132 5763 {
29b0f896 5764 case LONG_MNEM_SUFFIX:
40fb9820
L
5765 i.types[op].bitfield.imm32 = 1;
5766 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
5767 break;
5768 case WORD_MNEM_SUFFIX:
40fb9820
L
5769 i.types[op].bitfield.imm16 = 1;
5770 i.types[op].bitfield.imm32 = 1;
5771 i.types[op].bitfield.imm32s = 1;
5772 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
5773 break;
5774 case BYTE_MNEM_SUFFIX:
40fb9820
L
5775 i.types[op].bitfield.imm8 = 1;
5776 i.types[op].bitfield.imm8s = 1;
5777 i.types[op].bitfield.imm16 = 1;
5778 i.types[op].bitfield.imm32 = 1;
5779 i.types[op].bitfield.imm32s = 1;
5780 i.types[op].bitfield.imm64 = 1;
29b0f896 5781 break;
252b5132 5782 }
252b5132 5783
29b0f896
AM
5784 /* If this operand is at most 16 bits, convert it
5785 to a signed 16 bit number before trying to see
5786 whether it will fit in an even smaller size.
5787 This allows a 16-bit operand such as $0xffe0 to
5788 be recognised as within Imm8S range. */
40fb9820 5789 if ((i.types[op].bitfield.imm16)
7e96fb68 5790 && fits_in_unsigned_word (i.op[op].imms->X_add_number))
252b5132 5791 {
87ed972d
JB
5792 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
5793 ^ 0x8000) - 0x8000);
29b0f896 5794 }
a28def75
L
5795#ifdef BFD64
5796 /* Store 32-bit immediate in 64-bit for 64-bit BFD. */
40fb9820 5797 if ((i.types[op].bitfield.imm32)
7e96fb68 5798 && fits_in_unsigned_long (i.op[op].imms->X_add_number))
29b0f896
AM
5799 {
5800 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
5801 ^ ((offsetT) 1 << 31))
5802 - ((offsetT) 1 << 31));
5803 }
a28def75 5804#endif
40fb9820 5805 i.types[op]
c6fb90c8
L
5806 = operand_type_or (i.types[op],
5807 smallest_imm_type (i.op[op].imms->X_add_number));
252b5132 5808
29b0f896
AM
5809 /* We must avoid matching of Imm32 templates when 64bit
5810 only immediate is available. */
5811 if (guess_suffix == QWORD_MNEM_SUFFIX)
40fb9820 5812 i.types[op].bitfield.imm32 = 0;
29b0f896 5813 break;
252b5132 5814
29b0f896
AM
5815 case O_absent:
5816 case O_register:
5817 abort ();
5818
5819 /* Symbols and expressions. */
5820 default:
9cd96992
JB
5821 /* Convert symbolic operand to proper sizes for matching, but don't
5822 prevent matching a set of insns that only supports sizes other
5823 than those matching the insn suffix. */
5824 {
40fb9820 5825 i386_operand_type mask, allowed;
87ed972d 5826 const insn_template *t = current_templates->start;
9cd96992 5827
0dfbf9d7 5828 operand_type_set (&mask, 0);
87ed972d 5829 allowed = t->operand_types[op];
40fb9820 5830
87ed972d 5831 while (++t < current_templates->end)
bab6aec1 5832 {
bab6aec1 5833 allowed = operand_type_and (allowed, anyimm);
87ed972d 5834 allowed = operand_type_or (allowed, t->operand_types[op]);
bab6aec1 5835 }
9cd96992
JB
5836 switch (guess_suffix)
5837 {
5838 case QWORD_MNEM_SUFFIX:
40fb9820
L
5839 mask.bitfield.imm64 = 1;
5840 mask.bitfield.imm32s = 1;
9cd96992
JB
5841 break;
5842 case LONG_MNEM_SUFFIX:
40fb9820 5843 mask.bitfield.imm32 = 1;
9cd96992
JB
5844 break;
5845 case WORD_MNEM_SUFFIX:
40fb9820 5846 mask.bitfield.imm16 = 1;
9cd96992
JB
5847 break;
5848 case BYTE_MNEM_SUFFIX:
40fb9820 5849 mask.bitfield.imm8 = 1;
9cd96992
JB
5850 break;
5851 default:
9cd96992
JB
5852 break;
5853 }
c6fb90c8 5854 allowed = operand_type_and (mask, allowed);
0dfbf9d7 5855 if (!operand_type_all_zero (&allowed))
c6fb90c8 5856 i.types[op] = operand_type_and (i.types[op], mask);
9cd96992 5857 }
29b0f896 5858 break;
252b5132 5859 }
29b0f896
AM
5860 }
5861}
47926f60 5862
29b0f896
AM
5863/* Try to use the smallest displacement type too. */
5864static void
e3bb37b5 5865optimize_disp (void)
29b0f896
AM
5866{
5867 int op;
3e73aa7c 5868
29b0f896 5869 for (op = i.operands; --op >= 0;)
40fb9820 5870 if (operand_type_check (i.types[op], disp))
252b5132 5871 {
b300c311 5872 if (i.op[op].disps->X_op == O_constant)
252b5132 5873 {
91d6fa6a 5874 offsetT op_disp = i.op[op].disps->X_add_number;
29b0f896 5875
91d6fa6a 5876 if (!op_disp && i.types[op].bitfield.baseindex)
b300c311 5877 {
2f2be86b
JB
5878 i.types[op] = operand_type_and_not (i.types[op], anydisp);
5879 i.op[op].disps = NULL;
b300c311 5880 i.disp_operands--;
f185acdd
JB
5881 continue;
5882 }
5883
5884 if (i.types[op].bitfield.disp16
cd613c1f 5885 && fits_in_unsigned_word (op_disp))
f185acdd
JB
5886 {
5887 /* If this operand is at most 16 bits, convert
5888 to a signed 16 bit number and don't use 64bit
5889 displacement. */
5890 op_disp = ((op_disp ^ 0x8000) - 0x8000);
5891 i.types[op].bitfield.disp64 = 0;
b300c311 5892 }
f185acdd 5893
28a167a4 5894#ifdef BFD64
a50187b2 5895 /* Optimize 64-bit displacement to 32-bit for 64-bit BFD. */
a775efc8
JB
5896 if ((flag_code != CODE_64BIT
5897 ? i.types[op].bitfield.disp32
5898 : want_disp32 (current_templates->start)
5899 && (!current_templates->start->opcode_modifier.jump
5900 || i.jumpabsolute || i.types[op].bitfield.baseindex))
a50187b2 5901 && fits_in_unsigned_long (op_disp))
b300c311 5902 {
a50187b2
JB
5903 /* If this operand is at most 32 bits, convert
5904 to a signed 32 bit number and don't use 64bit
5905 displacement. */
5906 op_disp = (op_disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
5907 i.types[op].bitfield.disp64 = 0;
5908 i.types[op].bitfield.disp32 = 1;
5909 }
28a167a4 5910
a50187b2
JB
5911 if (flag_code == CODE_64BIT && fits_in_signed_long (op_disp))
5912 {
5913 i.types[op].bitfield.disp64 = 0;
a775efc8 5914 i.types[op].bitfield.disp32 = 1;
b300c311 5915 }
28a167a4 5916#endif
40fb9820 5917 if ((i.types[op].bitfield.disp32
40fb9820 5918 || i.types[op].bitfield.disp16)
b5014f7a 5919 && fits_in_disp8 (op_disp))
40fb9820 5920 i.types[op].bitfield.disp8 = 1;
77c59789
JB
5921
5922 i.op[op].disps->X_add_number = op_disp;
252b5132 5923 }
67a4f2b7
AO
5924 else if (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
5925 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL)
5926 {
5927 fix_new_exp (frag_now, frag_more (0) - frag_now->fr_literal, 0,
5928 i.op[op].disps, 0, i.reloc[op]);
2f2be86b 5929 i.types[op] = operand_type_and_not (i.types[op], anydisp);
67a4f2b7
AO
5930 }
5931 else
b300c311 5932 /* We only support 64bit displacement on constants. */
40fb9820 5933 i.types[op].bitfield.disp64 = 0;
252b5132 5934 }
29b0f896
AM
5935}
5936
4a1b91ea
L
5937/* Return 1 if there is a match in broadcast bytes between operand
5938 GIVEN and instruction template T. */
5939
5940static INLINE int
5941match_broadcast_size (const insn_template *t, unsigned int given)
5942{
5943 return ((t->opcode_modifier.broadcast == BYTE_BROADCAST
5944 && i.types[given].bitfield.byte)
5945 || (t->opcode_modifier.broadcast == WORD_BROADCAST
5946 && i.types[given].bitfield.word)
5947 || (t->opcode_modifier.broadcast == DWORD_BROADCAST
5948 && i.types[given].bitfield.dword)
5949 || (t->opcode_modifier.broadcast == QWORD_BROADCAST
5950 && i.types[given].bitfield.qword));
5951}
5952
6c30d220
L
5953/* Check if operands are valid for the instruction. */
5954
5955static int
5956check_VecOperands (const insn_template *t)
5957{
43234a1e 5958 unsigned int op;
e2195274 5959 i386_cpu_flags cpu;
e2195274
JB
5960
5961 /* Templates allowing for ZMMword as well as YMMword and/or XMMword for
5962 any one operand are implicity requiring AVX512VL support if the actual
5963 operand size is YMMword or XMMword. Since this function runs after
5964 template matching, there's no need to check for YMMword/XMMword in
5965 the template. */
5966 cpu = cpu_flags_and (t->cpu_flags, avx512);
5967 if (!cpu_flags_all_zero (&cpu)
5968 && !t->cpu_flags.bitfield.cpuavx512vl
5969 && !cpu_arch_flags.bitfield.cpuavx512vl)
5970 {
5971 for (op = 0; op < t->operands; ++op)
5972 {
5973 if (t->operand_types[op].bitfield.zmmword
5974 && (i.types[op].bitfield.ymmword
5975 || i.types[op].bitfield.xmmword))
5976 {
5977 i.error = unsupported;
5978 return 1;
5979 }
5980 }
5981 }
43234a1e 5982
22c36940
JB
5983 /* Somewhat similarly, templates specifying both AVX and AVX2 are
5984 requiring AVX2 support if the actual operand size is YMMword. */
5985 if (t->cpu_flags.bitfield.cpuavx
5986 && t->cpu_flags.bitfield.cpuavx2
5987 && !cpu_arch_flags.bitfield.cpuavx2)
5988 {
5989 for (op = 0; op < t->operands; ++op)
5990 {
5991 if (t->operand_types[op].bitfield.xmmword
5992 && i.types[op].bitfield.ymmword)
5993 {
5994 i.error = unsupported;
5995 return 1;
5996 }
5997 }
5998 }
5999
6c30d220 6000 /* Without VSIB byte, we can't have a vector register for index. */
63112cd6 6001 if (!t->opcode_modifier.sib
6c30d220 6002 && i.index_reg
1b54b8d7
JB
6003 && (i.index_reg->reg_type.bitfield.xmmword
6004 || i.index_reg->reg_type.bitfield.ymmword
6005 || i.index_reg->reg_type.bitfield.zmmword))
6c30d220
L
6006 {
6007 i.error = unsupported_vector_index_register;
6008 return 1;
6009 }
6010
ad8ecc81
MZ
6011 /* Check if default mask is allowed. */
6012 if (t->opcode_modifier.nodefmask
6225c532 6013 && (!i.mask.reg || i.mask.reg->reg_num == 0))
ad8ecc81
MZ
6014 {
6015 i.error = no_default_mask;
6016 return 1;
6017 }
6018
7bab8ab5
JB
6019 /* For VSIB byte, we need a vector register for index, and all vector
6020 registers must be distinct. */
260cd341 6021 if (t->opcode_modifier.sib && t->opcode_modifier.sib != SIBMEM)
7bab8ab5
JB
6022 {
6023 if (!i.index_reg
63112cd6 6024 || !((t->opcode_modifier.sib == VECSIB128
1b54b8d7 6025 && i.index_reg->reg_type.bitfield.xmmword)
63112cd6 6026 || (t->opcode_modifier.sib == VECSIB256
1b54b8d7 6027 && i.index_reg->reg_type.bitfield.ymmword)
63112cd6 6028 || (t->opcode_modifier.sib == VECSIB512
1b54b8d7 6029 && i.index_reg->reg_type.bitfield.zmmword)))
7bab8ab5
JB
6030 {
6031 i.error = invalid_vsib_address;
6032 return 1;
6033 }
6034
6225c532
JB
6035 gas_assert (i.reg_operands == 2 || i.mask.reg);
6036 if (i.reg_operands == 2 && !i.mask.reg)
43234a1e 6037 {
3528c362 6038 gas_assert (i.types[0].bitfield.class == RegSIMD);
1b54b8d7
JB
6039 gas_assert (i.types[0].bitfield.xmmword
6040 || i.types[0].bitfield.ymmword);
3528c362 6041 gas_assert (i.types[2].bitfield.class == RegSIMD);
1b54b8d7
JB
6042 gas_assert (i.types[2].bitfield.xmmword
6043 || i.types[2].bitfield.ymmword);
43234a1e
L
6044 if (operand_check == check_none)
6045 return 0;
6046 if (register_number (i.op[0].regs)
6047 != register_number (i.index_reg)
6048 && register_number (i.op[2].regs)
6049 != register_number (i.index_reg)
6050 && register_number (i.op[0].regs)
6051 != register_number (i.op[2].regs))
6052 return 0;
6053 if (operand_check == check_error)
6054 {
6055 i.error = invalid_vector_register_set;
6056 return 1;
6057 }
6058 as_warn (_("mask, index, and destination registers should be distinct"));
6059 }
6225c532 6060 else if (i.reg_operands == 1 && i.mask.reg)
8444f82a 6061 {
3528c362 6062 if (i.types[1].bitfield.class == RegSIMD
1b54b8d7
JB
6063 && (i.types[1].bitfield.xmmword
6064 || i.types[1].bitfield.ymmword
6065 || i.types[1].bitfield.zmmword)
8444f82a
MZ
6066 && (register_number (i.op[1].regs)
6067 == register_number (i.index_reg)))
6068 {
6069 if (operand_check == check_error)
6070 {
6071 i.error = invalid_vector_register_set;
6072 return 1;
6073 }
6074 if (operand_check != check_none)
6075 as_warn (_("index and destination registers should be distinct"));
6076 }
6077 }
43234a1e 6078 }
7bab8ab5 6079
fc141319
L
6080 /* For AMX instructions with 3 TMM register operands, all operands
6081 must be distinct. */
6082 if (i.reg_operands == 3
6083 && t->operand_types[0].bitfield.tmmword
6084 && (i.op[0].regs == i.op[1].regs
6085 || i.op[0].regs == i.op[2].regs
6086 || i.op[1].regs == i.op[2].regs))
6087 {
6088 i.error = invalid_tmm_register_set;
6089 return 1;
260cd341
LC
6090 }
6091
0cc78721
CL
6092 /* For some special instructions require that destination must be distinct
6093 from source registers. */
6094 if (t->opcode_modifier.distinctdest)
6095 {
6096 unsigned int dest_reg = i.operands - 1;
6097
6098 know (i.operands >= 3);
6099
6100 /* #UD if dest_reg == src1_reg or dest_reg == src2_reg. */
6101 if (i.op[dest_reg - 1].regs == i.op[dest_reg].regs
6102 || (i.reg_operands > 2
6103 && i.op[dest_reg - 2].regs == i.op[dest_reg].regs))
6104 {
6105 i.error = invalid_dest_and_src_register_set;
6106 return 1;
6107 }
6108 }
6109
43234a1e
L
6110 /* Check if broadcast is supported by the instruction and is applied
6111 to the memory operand. */
a5748e0d 6112 if (i.broadcast.type || i.broadcast.bytes)
43234a1e 6113 {
8e6e0792 6114 i386_operand_type type, overlap;
43234a1e
L
6115
6116 /* Check if specified broadcast is supported in this instruction,
4a1b91ea 6117 and its broadcast bytes match the memory operand. */
5273a3cd 6118 op = i.broadcast.operand;
8e6e0792 6119 if (!t->opcode_modifier.broadcast
c48dadc9 6120 || !(i.flags[op] & Operand_Mem)
c39e5b26 6121 || (!i.types[op].bitfield.unspecified
4a1b91ea 6122 && !match_broadcast_size (t, op)))
43234a1e
L
6123 {
6124 bad_broadcast:
6125 i.error = unsupported_broadcast;
6126 return 1;
6127 }
8e6e0792 6128
a5748e0d
JB
6129 if (i.broadcast.type)
6130 i.broadcast.bytes = ((1 << (t->opcode_modifier.broadcast - 1))
6131 * i.broadcast.type);
8e6e0792 6132 operand_type_set (&type, 0);
a5748e0d 6133 switch (get_broadcast_bytes (t, false))
8e6e0792 6134 {
4a1b91ea
L
6135 case 2:
6136 type.bitfield.word = 1;
6137 break;
6138 case 4:
6139 type.bitfield.dword = 1;
6140 break;
8e6e0792
JB
6141 case 8:
6142 type.bitfield.qword = 1;
6143 break;
6144 case 16:
6145 type.bitfield.xmmword = 1;
6146 break;
6147 case 32:
6148 type.bitfield.ymmword = 1;
6149 break;
6150 case 64:
6151 type.bitfield.zmmword = 1;
6152 break;
6153 default:
6154 goto bad_broadcast;
6155 }
6156
6157 overlap = operand_type_and (type, t->operand_types[op]);
bc49bfd8
JB
6158 if (t->operand_types[op].bitfield.class == RegSIMD
6159 && t->operand_types[op].bitfield.byte
6160 + t->operand_types[op].bitfield.word
6161 + t->operand_types[op].bitfield.dword
6162 + t->operand_types[op].bitfield.qword > 1)
6163 {
6164 overlap.bitfield.xmmword = 0;
6165 overlap.bitfield.ymmword = 0;
6166 overlap.bitfield.zmmword = 0;
6167 }
8e6e0792
JB
6168 if (operand_type_all_zero (&overlap))
6169 goto bad_broadcast;
6170
6171 if (t->opcode_modifier.checkregsize)
6172 {
6173 unsigned int j;
6174
e2195274 6175 type.bitfield.baseindex = 1;
8e6e0792
JB
6176 for (j = 0; j < i.operands; ++j)
6177 {
6178 if (j != op
6179 && !operand_type_register_match(i.types[j],
6180 t->operand_types[j],
6181 type,
6182 t->operand_types[op]))
6183 goto bad_broadcast;
6184 }
6185 }
43234a1e
L
6186 }
6187 /* If broadcast is supported in this instruction, we need to check if
6188 operand of one-element size isn't specified without broadcast. */
6189 else if (t->opcode_modifier.broadcast && i.mem_operands)
6190 {
6191 /* Find memory operand. */
6192 for (op = 0; op < i.operands; op++)
8dc0818e 6193 if (i.flags[op] & Operand_Mem)
43234a1e
L
6194 break;
6195 gas_assert (op < i.operands);
6196 /* Check size of the memory operand. */
4a1b91ea 6197 if (match_broadcast_size (t, op))
43234a1e
L
6198 {
6199 i.error = broadcast_needed;
6200 return 1;
6201 }
6202 }
c39e5b26
JB
6203 else
6204 op = MAX_OPERANDS - 1; /* Avoid uninitialized variable warning. */
43234a1e
L
6205
6206 /* Check if requested masking is supported. */
6225c532 6207 if (i.mask.reg)
43234a1e 6208 {
ae2387fe
JB
6209 switch (t->opcode_modifier.masking)
6210 {
6211 case BOTH_MASKING:
6212 break;
6213 case MERGING_MASKING:
6225c532 6214 if (i.mask.zeroing)
ae2387fe
JB
6215 {
6216 case 0:
6217 i.error = unsupported_masking;
6218 return 1;
6219 }
6220 break;
6221 case DYNAMIC_MASKING:
6222 /* Memory destinations allow only merging masking. */
6225c532 6223 if (i.mask.zeroing && i.mem_operands)
ae2387fe
JB
6224 {
6225 /* Find memory operand. */
6226 for (op = 0; op < i.operands; op++)
c48dadc9 6227 if (i.flags[op] & Operand_Mem)
ae2387fe
JB
6228 break;
6229 gas_assert (op < i.operands);
6230 if (op == i.operands - 1)
6231 {
6232 i.error = unsupported_masking;
6233 return 1;
6234 }
6235 }
6236 break;
6237 default:
6238 abort ();
6239 }
43234a1e
L
6240 }
6241
6242 /* Check if masking is applied to dest operand. */
6225c532 6243 if (i.mask.reg && (i.mask.operand != i.operands - 1))
43234a1e
L
6244 {
6245 i.error = mask_not_on_destination;
6246 return 1;
6247 }
6248
43234a1e 6249 /* Check RC/SAE. */
ca5312a2 6250 if (i.rounding.type != rc_none)
43234a1e 6251 {
a80195f1 6252 if (!t->opcode_modifier.sae
cf665fee
JB
6253 || ((i.rounding.type != saeonly) != t->opcode_modifier.staticrounding)
6254 || i.mem_operands)
43234a1e
L
6255 {
6256 i.error = unsupported_rc_sae;
6257 return 1;
6258 }
cf665fee
JB
6259
6260 /* Non-EVEX.LIG forms need to have a ZMM register as at least one
6261 operand. */
6262 if (t->opcode_modifier.evex != EVEXLIG)
7bab8ab5 6263 {
cf665fee
JB
6264 for (op = 0; op < t->operands; ++op)
6265 if (i.types[op].bitfield.zmmword)
6266 break;
6267 if (op >= t->operands)
6268 {
6269 i.error = operand_size_mismatch;
6270 return 1;
6271 }
7bab8ab5 6272 }
6c30d220
L
6273 }
6274
da4977e0
JB
6275 /* Check the special Imm4 cases; must be the first operand. */
6276 if (t->cpu_flags.bitfield.cpuxop && t->operands == 5)
6277 {
6278 if (i.op[0].imms->X_op != O_constant
6279 || !fits_in_imm4 (i.op[0].imms->X_add_number))
6280 {
6281 i.error = bad_imm4;
6282 return 1;
6283 }
6284
6285 /* Turn off Imm<N> so that update_imm won't complain. */
6286 operand_type_set (&i.types[0], 0);
6287 }
6288
43234a1e 6289 /* Check vector Disp8 operand. */
b5014f7a 6290 if (t->opcode_modifier.disp8memshift
1a42a9fe 6291 && i.disp_encoding <= disp_encoding_8bit)
43234a1e 6292 {
a5748e0d 6293 if (i.broadcast.bytes)
4a1b91ea 6294 i.memshift = t->opcode_modifier.broadcast - 1;
7091c612 6295 else if (t->opcode_modifier.disp8memshift != DISP8_SHIFT_VL)
43234a1e 6296 i.memshift = t->opcode_modifier.disp8memshift;
7091c612
JB
6297 else
6298 {
125ff819 6299 const i386_operand_type *type = NULL, *fallback = NULL;
7091c612
JB
6300
6301 i.memshift = 0;
6302 for (op = 0; op < i.operands; op++)
8dc0818e 6303 if (i.flags[op] & Operand_Mem)
7091c612 6304 {
4174bfff
JB
6305 if (t->opcode_modifier.evex == EVEXLIG)
6306 i.memshift = 2 + (i.suffix == QWORD_MNEM_SUFFIX);
6307 else if (t->operand_types[op].bitfield.xmmword
6308 + t->operand_types[op].bitfield.ymmword
6309 + t->operand_types[op].bitfield.zmmword <= 1)
7091c612
JB
6310 type = &t->operand_types[op];
6311 else if (!i.types[op].bitfield.unspecified)
6312 type = &i.types[op];
125ff819
JB
6313 else /* Ambiguities get resolved elsewhere. */
6314 fallback = &t->operand_types[op];
7091c612 6315 }
3528c362 6316 else if (i.types[op].bitfield.class == RegSIMD
4174bfff 6317 && t->opcode_modifier.evex != EVEXLIG)
7091c612
JB
6318 {
6319 if (i.types[op].bitfield.zmmword)
6320 i.memshift = 6;
6321 else if (i.types[op].bitfield.ymmword && i.memshift < 5)
6322 i.memshift = 5;
6323 else if (i.types[op].bitfield.xmmword && i.memshift < 4)
6324 i.memshift = 4;
6325 }
6326
125ff819
JB
6327 if (!type && !i.memshift)
6328 type = fallback;
7091c612
JB
6329 if (type)
6330 {
6331 if (type->bitfield.zmmword)
6332 i.memshift = 6;
6333 else if (type->bitfield.ymmword)
6334 i.memshift = 5;
6335 else if (type->bitfield.xmmword)
6336 i.memshift = 4;
6337 }
6338
6339 /* For the check in fits_in_disp8(). */
6340 if (i.memshift == 0)
6341 i.memshift = -1;
6342 }
43234a1e
L
6343
6344 for (op = 0; op < i.operands; op++)
6345 if (operand_type_check (i.types[op], disp)
6346 && i.op[op].disps->X_op == O_constant)
6347 {
b5014f7a 6348 if (fits_in_disp8 (i.op[op].disps->X_add_number))
43234a1e 6349 {
b5014f7a
JB
6350 i.types[op].bitfield.disp8 = 1;
6351 return 0;
43234a1e 6352 }
b5014f7a 6353 i.types[op].bitfield.disp8 = 0;
43234a1e
L
6354 }
6355 }
b5014f7a
JB
6356
6357 i.memshift = 0;
43234a1e 6358
6c30d220
L
6359 return 0;
6360}
6361
da4977e0 6362/* Check if encoding requirements are met by the instruction. */
a683cc34
SP
6363
6364static int
da4977e0 6365VEX_check_encoding (const insn_template *t)
a683cc34 6366{
da4977e0
JB
6367 if (i.vec_encoding == vex_encoding_error)
6368 {
6369 i.error = unsupported;
6370 return 1;
6371 }
6372
86fa6981 6373 if (i.vec_encoding == vex_encoding_evex)
43234a1e 6374 {
86fa6981 6375 /* This instruction must be encoded with EVEX prefix. */
e771e7c9 6376 if (!is_evex_encoding (t))
86fa6981
L
6377 {
6378 i.error = unsupported;
6379 return 1;
6380 }
6381 return 0;
43234a1e
L
6382 }
6383
a683cc34 6384 if (!t->opcode_modifier.vex)
86fa6981
L
6385 {
6386 /* This instruction template doesn't have VEX prefix. */
6387 if (i.vec_encoding != vex_encoding_default)
6388 {
6389 i.error = unsupported;
6390 return 1;
6391 }
6392 return 0;
6393 }
a683cc34 6394
a683cc34
SP
6395 return 0;
6396}
6397
d3ce72d0 6398static const insn_template *
83b16ac6 6399match_template (char mnem_suffix)
29b0f896
AM
6400{
6401 /* Points to template once we've found it. */
d3ce72d0 6402 const insn_template *t;
40fb9820 6403 i386_operand_type overlap0, overlap1, overlap2, overlap3;
c0f3af97 6404 i386_operand_type overlap4;
29b0f896 6405 unsigned int found_reverse_match;
dc2be329 6406 i386_opcode_modifier suffix_check;
40fb9820 6407 i386_operand_type operand_types [MAX_OPERANDS];
539e75ad 6408 int addr_prefix_disp;
45a4bb20 6409 unsigned int j, size_match, check_register;
5614d22c 6410 enum i386_error specific_error = 0;
29b0f896 6411
c0f3af97
L
6412#if MAX_OPERANDS != 5
6413# error "MAX_OPERANDS must be 5."
f48ff2ae
L
6414#endif
6415
29b0f896 6416 found_reverse_match = 0;
539e75ad 6417 addr_prefix_disp = -1;
40fb9820 6418
dc2be329 6419 /* Prepare for mnemonic suffix check. */
40fb9820 6420 memset (&suffix_check, 0, sizeof (suffix_check));
dc2be329
L
6421 switch (mnem_suffix)
6422 {
6423 case BYTE_MNEM_SUFFIX:
6424 suffix_check.no_bsuf = 1;
6425 break;
6426 case WORD_MNEM_SUFFIX:
6427 suffix_check.no_wsuf = 1;
6428 break;
6429 case SHORT_MNEM_SUFFIX:
6430 suffix_check.no_ssuf = 1;
6431 break;
6432 case LONG_MNEM_SUFFIX:
6433 suffix_check.no_lsuf = 1;
6434 break;
6435 case QWORD_MNEM_SUFFIX:
6436 suffix_check.no_qsuf = 1;
6437 break;
6438 default:
6439 /* NB: In Intel syntax, normally we can check for memory operand
6440 size when there is no mnemonic suffix. But jmp and call have
6441 2 different encodings with Dword memory operand size, one with
6442 No_ldSuf and the other without. i.suffix is set to
6443 LONG_DOUBLE_MNEM_SUFFIX to skip the one with No_ldSuf. */
6444 if (i.suffix == LONG_DOUBLE_MNEM_SUFFIX)
6445 suffix_check.no_ldsuf = 1;
83b16ac6
JB
6446 }
6447
01559ecc
L
6448 /* Must have right number of operands. */
6449 i.error = number_of_operands_mismatch;
6450
45aa61fe 6451 for (t = current_templates->start; t < current_templates->end; t++)
29b0f896 6452 {
539e75ad 6453 addr_prefix_disp = -1;
dbbc8b7e 6454 found_reverse_match = 0;
539e75ad 6455
29b0f896
AM
6456 if (i.operands != t->operands)
6457 continue;
6458
50aecf8c 6459 /* Check processor support. */
a65babc9 6460 i.error = unsupported;
45a4bb20 6461 if (cpu_flags_match (t) != CPU_FLAGS_PERFECT_MATCH)
50aecf8c
L
6462 continue;
6463
57392598
CL
6464 /* Check Pseudo Prefix. */
6465 i.error = unsupported;
6466 if (t->opcode_modifier.pseudovexprefix
6467 && !(i.vec_encoding == vex_encoding_vex
6468 || i.vec_encoding == vex_encoding_vex3))
6469 continue;
6470
e1d4d893 6471 /* Check AT&T mnemonic. */
a65babc9 6472 i.error = unsupported_with_intel_mnemonic;
e1d4d893 6473 if (intel_mnemonic && t->opcode_modifier.attmnemonic)
1efbbeb4
L
6474 continue;
6475
4b5aaf5f 6476 /* Check AT&T/Intel syntax. */
a65babc9 6477 i.error = unsupported_syntax;
5c07affc 6478 if ((intel_syntax && t->opcode_modifier.attsyntax)
4b5aaf5f 6479 || (!intel_syntax && t->opcode_modifier.intelsyntax))
1efbbeb4
L
6480 continue;
6481
4b5aaf5f
L
6482 /* Check Intel64/AMD64 ISA. */
6483 switch (isa64)
6484 {
6485 default:
6486 /* Default: Don't accept Intel64. */
6487 if (t->opcode_modifier.isa64 == INTEL64)
6488 continue;
6489 break;
6490 case amd64:
6491 /* -mamd64: Don't accept Intel64 and Intel64 only. */
6492 if (t->opcode_modifier.isa64 >= INTEL64)
6493 continue;
6494 break;
6495 case intel64:
6496 /* -mintel64: Don't accept AMD64. */
5990e377 6497 if (t->opcode_modifier.isa64 == AMD64 && flag_code == CODE_64BIT)
4b5aaf5f
L
6498 continue;
6499 break;
6500 }
6501
dc2be329 6502 /* Check the suffix. */
a65babc9 6503 i.error = invalid_instruction_suffix;
dc2be329
L
6504 if ((t->opcode_modifier.no_bsuf && suffix_check.no_bsuf)
6505 || (t->opcode_modifier.no_wsuf && suffix_check.no_wsuf)
6506 || (t->opcode_modifier.no_lsuf && suffix_check.no_lsuf)
6507 || (t->opcode_modifier.no_ssuf && suffix_check.no_ssuf)
6508 || (t->opcode_modifier.no_qsuf && suffix_check.no_qsuf)
6509 || (t->opcode_modifier.no_ldsuf && suffix_check.no_ldsuf))
83b16ac6 6510 continue;
29b0f896 6511
3ac21baa
JB
6512 size_match = operand_size_match (t);
6513 if (!size_match)
7d5e4556 6514 continue;
539e75ad 6515
6f2f06be
JB
6516 /* This is intentionally not
6517
0cfa3eb3 6518 if (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE))
6f2f06be
JB
6519
6520 as the case of a missing * on the operand is accepted (perhaps with
6521 a warning, issued further down). */
0cfa3eb3 6522 if (i.jumpabsolute && t->opcode_modifier.jump != JUMP_ABSOLUTE)
6f2f06be
JB
6523 {
6524 i.error = operand_type_mismatch;
6525 continue;
6526 }
6527
5c07affc
L
6528 for (j = 0; j < MAX_OPERANDS; j++)
6529 operand_types[j] = t->operand_types[j];
6530
e365e234
JB
6531 /* In general, don't allow
6532 - 64-bit operands outside of 64-bit mode,
6533 - 32-bit operands on pre-386. */
4873e243 6534 j = i.imm_operands + (t->operands > i.imm_operands + 1);
e365e234
JB
6535 if (((i.suffix == QWORD_MNEM_SUFFIX
6536 && flag_code != CODE_64BIT
389d00a5
JB
6537 && !(t->opcode_modifier.opcodespace == SPACE_0F
6538 && t->base_opcode == 0xc7
5e74b495 6539 && t->opcode_modifier.opcodeprefix == PREFIX_NONE
8b65b895 6540 && t->extension_opcode == 1) /* cmpxchg8b */)
e365e234
JB
6541 || (i.suffix == LONG_MNEM_SUFFIX
6542 && !cpu_arch_flags.bitfield.cpui386))
45aa61fe 6543 && (intel_syntax
3cd7f3e3 6544 ? (t->opcode_modifier.mnemonicsize != IGNORESIZE
45aa61fe
AM
6545 && !intel_float_operand (t->name))
6546 : intel_float_operand (t->name) != 2)
4873e243
JB
6547 && (t->operands == i.imm_operands
6548 || (operand_types[i.imm_operands].bitfield.class != RegMMX
6549 && operand_types[i.imm_operands].bitfield.class != RegSIMD
6550 && operand_types[i.imm_operands].bitfield.class != RegMask)
6551 || (operand_types[j].bitfield.class != RegMMX
6552 && operand_types[j].bitfield.class != RegSIMD
6553 && operand_types[j].bitfield.class != RegMask))
63112cd6 6554 && !t->opcode_modifier.sib)
192dc9c6
JB
6555 continue;
6556
29b0f896 6557 /* Do not verify operands when there are none. */
e365e234 6558 if (!t->operands)
da4977e0
JB
6559 {
6560 if (VEX_check_encoding (t))
6561 {
6562 specific_error = i.error;
6563 continue;
6564 }
6565
6566 /* We've found a match; break out of loop. */
6567 break;
6568 }
252b5132 6569
48bcea9f
JB
6570 if (!t->opcode_modifier.jump
6571 || t->opcode_modifier.jump == JUMP_ABSOLUTE)
6572 {
6573 /* There should be only one Disp operand. */
6574 for (j = 0; j < MAX_OPERANDS; j++)
6575 if (operand_type_check (operand_types[j], disp))
539e75ad 6576 break;
48bcea9f
JB
6577 if (j < MAX_OPERANDS)
6578 {
5b7c81bd 6579 bool override = (i.prefix[ADDR_PREFIX] != 0);
48bcea9f
JB
6580
6581 addr_prefix_disp = j;
6582
a775efc8
JB
6583 /* Address size prefix will turn Disp64 operand into Disp32 and
6584 Disp32/Disp16 one into Disp16/Disp32 respectively. */
48bcea9f 6585 switch (flag_code)
40fb9820 6586 {
48bcea9f
JB
6587 case CODE_16BIT:
6588 override = !override;
6589 /* Fall through. */
6590 case CODE_32BIT:
6591 if (operand_types[j].bitfield.disp32
6592 && operand_types[j].bitfield.disp16)
40fb9820 6593 {
48bcea9f
JB
6594 operand_types[j].bitfield.disp16 = override;
6595 operand_types[j].bitfield.disp32 = !override;
40fb9820 6596 }
a775efc8 6597 gas_assert (!operand_types[j].bitfield.disp64);
48bcea9f
JB
6598 break;
6599
6600 case CODE_64BIT:
a775efc8 6601 if (operand_types[j].bitfield.disp64)
40fb9820 6602 {
a775efc8 6603 gas_assert (!operand_types[j].bitfield.disp32);
48bcea9f 6604 operand_types[j].bitfield.disp32 = override;
a775efc8 6605 operand_types[j].bitfield.disp64 = !override;
40fb9820 6606 }
48bcea9f
JB
6607 operand_types[j].bitfield.disp16 = 0;
6608 break;
40fb9820 6609 }
539e75ad 6610 }
48bcea9f 6611 }
539e75ad 6612
d7e3e627
L
6613 switch (i.reloc[0])
6614 {
6615 case BFD_RELOC_386_GOT32:
6616 /* Force 0x8b encoding for "mov foo@GOT, %eax". */
6617 if (t->base_opcode == 0xa0
6618 && t->opcode_modifier.opcodespace == SPACE_BASE)
6619 continue;
6620 break;
6621 case BFD_RELOC_386_TLS_GOTIE:
6622 case BFD_RELOC_386_TLS_LE_32:
6623 case BFD_RELOC_X86_64_GOTTPOFF:
6624 case BFD_RELOC_X86_64_TLSLD:
6625 /* Don't allow KMOV in TLS code sequences. */
6626 if (t->opcode_modifier.vex)
6627 continue;
6628 break;
6629 default:
6630 break;
6631 }
02a86693 6632
56ffb741 6633 /* We check register size if needed. */
e2195274
JB
6634 if (t->opcode_modifier.checkregsize)
6635 {
6636 check_register = (1 << t->operands) - 1;
a5748e0d 6637 if (i.broadcast.type || i.broadcast.bytes)
5273a3cd 6638 check_register &= ~(1 << i.broadcast.operand);
e2195274
JB
6639 }
6640 else
6641 check_register = 0;
6642
c6fb90c8 6643 overlap0 = operand_type_and (i.types[0], operand_types[0]);
29b0f896
AM
6644 switch (t->operands)
6645 {
6646 case 1:
40fb9820 6647 if (!operand_type_match (overlap0, i.types[0]))
29b0f896
AM
6648 continue;
6649 break;
6650 case 2:
33eaf5de 6651 /* xchg %eax, %eax is a special case. It is an alias for nop
8b38ad71
L
6652 only in 32bit mode and we can use opcode 0x90. In 64bit
6653 mode, we can't use 0x90 for xchg %eax, %eax since it should
6654 zero-extend %eax to %rax. */
6655 if (flag_code == CODE_64BIT
6656 && t->base_opcode == 0x90
35648716 6657 && t->opcode_modifier.opcodespace == SPACE_BASE
75e5731b
JB
6658 && i.types[0].bitfield.instance == Accum
6659 && i.types[0].bitfield.dword
6660 && i.types[1].bitfield.instance == Accum
6661 && i.types[1].bitfield.dword)
8b38ad71 6662 continue;
1212781b
JB
6663 /* xrelease mov %eax, <disp> is another special case. It must not
6664 match the accumulator-only encoding of mov. */
6665 if (flag_code != CODE_64BIT
6666 && i.hle_prefix
6667 && t->base_opcode == 0xa0
35648716 6668 && t->opcode_modifier.opcodespace == SPACE_BASE
75e5731b 6669 && i.types[0].bitfield.instance == Accum
8dc0818e 6670 && (i.flags[1] & Operand_Mem))
1212781b 6671 continue;
f5eb1d70
JB
6672 /* Fall through. */
6673
6674 case 3:
3ac21baa
JB
6675 if (!(size_match & MATCH_STRAIGHT))
6676 goto check_reverse;
64c49ab3
JB
6677 /* Reverse direction of operands if swapping is possible in the first
6678 place (operands need to be symmetric) and
6679 - the load form is requested, and the template is a store form,
6680 - the store form is requested, and the template is a load form,
6681 - the non-default (swapped) form is requested. */
6682 overlap1 = operand_type_and (operand_types[0], operand_types[1]);
f5eb1d70 6683 if (t->opcode_modifier.d && i.reg_operands == i.operands
64c49ab3
JB
6684 && !operand_type_all_zero (&overlap1))
6685 switch (i.dir_encoding)
6686 {
6687 case dir_encoding_load:
6688 if (operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 6689 || t->opcode_modifier.regmem)
64c49ab3
JB
6690 goto check_reverse;
6691 break;
6692
6693 case dir_encoding_store:
6694 if (!operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 6695 && !t->opcode_modifier.regmem)
64c49ab3
JB
6696 goto check_reverse;
6697 break;
6698
6699 case dir_encoding_swap:
6700 goto check_reverse;
6701
6702 case dir_encoding_default:
6703 break;
6704 }
86fa6981 6705 /* If we want store form, we skip the current load. */
64c49ab3
JB
6706 if ((i.dir_encoding == dir_encoding_store
6707 || i.dir_encoding == dir_encoding_swap)
86fa6981
L
6708 && i.mem_operands == 0
6709 && t->opcode_modifier.load)
fa99fab2 6710 continue;
1a0670f3 6711 /* Fall through. */
f48ff2ae 6712 case 4:
c0f3af97 6713 case 5:
c6fb90c8 6714 overlap1 = operand_type_and (i.types[1], operand_types[1]);
40fb9820
L
6715 if (!operand_type_match (overlap0, i.types[0])
6716 || !operand_type_match (overlap1, i.types[1])
e2195274 6717 || ((check_register & 3) == 3
dc821c5f 6718 && !operand_type_register_match (i.types[0],
40fb9820 6719 operand_types[0],
dc821c5f 6720 i.types[1],
40fb9820 6721 operand_types[1])))
29b0f896
AM
6722 {
6723 /* Check if other direction is valid ... */
38e314eb 6724 if (!t->opcode_modifier.d)
29b0f896
AM
6725 continue;
6726
dc1e8a47 6727 check_reverse:
3ac21baa
JB
6728 if (!(size_match & MATCH_REVERSE))
6729 continue;
29b0f896 6730 /* Try reversing direction of operands. */
8bd915b7
JB
6731 j = t->opcode_modifier.vexsources ? 1 : i.operands - 1;
6732 overlap0 = operand_type_and (i.types[0], operand_types[j]);
6733 overlap1 = operand_type_and (i.types[j], operand_types[0]);
c975cec5
JB
6734 overlap2 = operand_type_and (i.types[1], operand_types[1]);
6735 gas_assert (t->operands != 3 || !check_register);
40fb9820 6736 if (!operand_type_match (overlap0, i.types[0])
8bd915b7 6737 || !operand_type_match (overlap1, i.types[j])
c975cec5
JB
6738 || (t->operands == 3
6739 && !operand_type_match (overlap2, i.types[1]))
45664ddb 6740 || (check_register
dc821c5f 6741 && !operand_type_register_match (i.types[0],
8bd915b7
JB
6742 operand_types[j],
6743 i.types[j],
45664ddb 6744 operand_types[0])))
29b0f896
AM
6745 {
6746 /* Does not match either direction. */
6747 continue;
6748 }
38e314eb 6749 /* found_reverse_match holds which of D or FloatR
29b0f896 6750 we've found. */
38e314eb
JB
6751 if (!t->opcode_modifier.d)
6752 found_reverse_match = 0;
6753 else if (operand_types[0].bitfield.tbyte)
8a2ed489 6754 found_reverse_match = Opcode_FloatD;
8bd915b7
JB
6755 else if (t->opcode_modifier.vexsources)
6756 {
6757 found_reverse_match = Opcode_VexW;
6758 goto check_operands_345;
6759 }
dbbc8b7e 6760 else if (operand_types[0].bitfield.xmmword
f5eb1d70 6761 || operand_types[i.operands - 1].bitfield.xmmword
3528c362
JB
6762 || operand_types[0].bitfield.class == RegMMX
6763 || operand_types[i.operands - 1].bitfield.class == RegMMX
dbbc8b7e
JB
6764 || is_any_vex_encoding(t))
6765 found_reverse_match = (t->base_opcode & 0xee) != 0x6e
6766 ? Opcode_SIMD_FloatD : Opcode_SIMD_IntD;
8a2ed489 6767 else
38e314eb 6768 found_reverse_match = Opcode_D;
40fb9820 6769 if (t->opcode_modifier.floatr)
8a2ed489 6770 found_reverse_match |= Opcode_FloatR;
29b0f896 6771 }
f48ff2ae 6772 else
29b0f896 6773 {
f48ff2ae 6774 /* Found a forward 2 operand match here. */
8bd915b7 6775 check_operands_345:
d1cbb4db
L
6776 switch (t->operands)
6777 {
c0f3af97 6778 case 5:
3d0738af 6779 overlap4 = operand_type_and (i.types[4], operand_types[4]);
c0f3af97 6780 if (!operand_type_match (overlap4, i.types[4])
dc821c5f 6781 || !operand_type_register_match (i.types[3],
c0f3af97 6782 operand_types[3],
c0f3af97
L
6783 i.types[4],
6784 operand_types[4]))
6785 continue;
1a0670f3 6786 /* Fall through. */
f48ff2ae 6787 case 4:
3d0738af 6788 overlap3 = operand_type_and (i.types[3], operand_types[3]);
40fb9820 6789 if (!operand_type_match (overlap3, i.types[3])
e2195274
JB
6790 || ((check_register & 0xa) == 0xa
6791 && !operand_type_register_match (i.types[1],
f7768225
JB
6792 operand_types[1],
6793 i.types[3],
e2195274
JB
6794 operand_types[3]))
6795 || ((check_register & 0xc) == 0xc
6796 && !operand_type_register_match (i.types[2],
6797 operand_types[2],
6798 i.types[3],
6799 operand_types[3])))
f48ff2ae 6800 continue;
1a0670f3 6801 /* Fall through. */
f48ff2ae 6802 case 3:
3d0738af 6803 overlap2 = operand_type_and (i.types[2], operand_types[2]);
40fb9820 6804 if (!operand_type_match (overlap2, i.types[2])
e2195274
JB
6805 || ((check_register & 5) == 5
6806 && !operand_type_register_match (i.types[0],
23e42951
JB
6807 operand_types[0],
6808 i.types[2],
e2195274
JB
6809 operand_types[2]))
6810 || ((check_register & 6) == 6
6811 && !operand_type_register_match (i.types[1],
6812 operand_types[1],
6813 i.types[2],
6814 operand_types[2])))
f48ff2ae
L
6815 continue;
6816 break;
6817 }
29b0f896 6818 }
f48ff2ae 6819 /* Found either forward/reverse 2, 3 or 4 operand match here:
29b0f896
AM
6820 slip through to break. */
6821 }
c0f3af97 6822
da4977e0
JB
6823 /* Check if vector operands are valid. */
6824 if (check_VecOperands (t))
6825 {
6826 specific_error = i.error;
6827 continue;
6828 }
6829
6830 /* Check if VEX/EVEX encoding requirements can be satisfied. */
6831 if (VEX_check_encoding (t))
5614d22c
JB
6832 {
6833 specific_error = i.error;
6834 continue;
6835 }
a683cc34 6836
29b0f896
AM
6837 /* We've found a match; break out of loop. */
6838 break;
6839 }
6840
6841 if (t == current_templates->end)
6842 {
6843 /* We found no match. */
a65babc9 6844 const char *err_msg;
5614d22c 6845 switch (specific_error ? specific_error : i.error)
a65babc9
L
6846 {
6847 default:
6848 abort ();
86e026a4 6849 case operand_size_mismatch:
a65babc9
L
6850 err_msg = _("operand size mismatch");
6851 break;
6852 case operand_type_mismatch:
6853 err_msg = _("operand type mismatch");
6854 break;
6855 case register_type_mismatch:
6856 err_msg = _("register type mismatch");
6857 break;
6858 case number_of_operands_mismatch:
6859 err_msg = _("number of operands mismatch");
6860 break;
6861 case invalid_instruction_suffix:
6862 err_msg = _("invalid instruction suffix");
6863 break;
6864 case bad_imm4:
4a2608e3 6865 err_msg = _("constant doesn't fit in 4 bits");
a65babc9 6866 break;
a65babc9
L
6867 case unsupported_with_intel_mnemonic:
6868 err_msg = _("unsupported with Intel mnemonic");
6869 break;
6870 case unsupported_syntax:
6871 err_msg = _("unsupported syntax");
6872 break;
6873 case unsupported:
35262a23 6874 as_bad (_("unsupported instruction `%s'"),
10efe3f6
L
6875 current_templates->start->name);
6876 return NULL;
260cd341
LC
6877 case invalid_sib_address:
6878 err_msg = _("invalid SIB address");
6879 break;
6c30d220
L
6880 case invalid_vsib_address:
6881 err_msg = _("invalid VSIB address");
6882 break;
7bab8ab5
JB
6883 case invalid_vector_register_set:
6884 err_msg = _("mask, index, and destination registers must be distinct");
6885 break;
260cd341
LC
6886 case invalid_tmm_register_set:
6887 err_msg = _("all tmm registers must be distinct");
6888 break;
0cc78721
CL
6889 case invalid_dest_and_src_register_set:
6890 err_msg = _("destination and source registers must be distinct");
6891 break;
6c30d220
L
6892 case unsupported_vector_index_register:
6893 err_msg = _("unsupported vector index register");
6894 break;
43234a1e
L
6895 case unsupported_broadcast:
6896 err_msg = _("unsupported broadcast");
6897 break;
43234a1e
L
6898 case broadcast_needed:
6899 err_msg = _("broadcast is needed for operand of such type");
6900 break;
6901 case unsupported_masking:
6902 err_msg = _("unsupported masking");
6903 break;
6904 case mask_not_on_destination:
6905 err_msg = _("mask not on destination operand");
6906 break;
6907 case no_default_mask:
6908 err_msg = _("default mask isn't allowed");
6909 break;
6910 case unsupported_rc_sae:
6911 err_msg = _("unsupported static rounding/sae");
6912 break;
43234a1e
L
6913 case invalid_register_operand:
6914 err_msg = _("invalid register operand");
6915 break;
a65babc9
L
6916 }
6917 as_bad (_("%s for `%s'"), err_msg,
891edac4 6918 current_templates->start->name);
fa99fab2 6919 return NULL;
29b0f896 6920 }
252b5132 6921
29b0f896
AM
6922 if (!quiet_warnings)
6923 {
6924 if (!intel_syntax
0cfa3eb3 6925 && (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE)))
6f2f06be 6926 as_warn (_("indirect %s without `*'"), t->name);
29b0f896 6927
40fb9820 6928 if (t->opcode_modifier.isprefix
3cd7f3e3 6929 && t->opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
6930 {
6931 /* Warn them that a data or address size prefix doesn't
6932 affect assembly of the next line of code. */
6933 as_warn (_("stand-alone `%s' prefix"), t->name);
6934 }
6935 }
6936
6937 /* Copy the template we found. */
9a182d04 6938 install_template (t);
539e75ad
L
6939
6940 if (addr_prefix_disp != -1)
6941 i.tm.operand_types[addr_prefix_disp]
6942 = operand_types[addr_prefix_disp];
6943
8bd915b7 6944 switch (found_reverse_match)
29b0f896 6945 {
8bd915b7
JB
6946 case 0:
6947 break;
6948
6949 default:
dfd69174
JB
6950 /* If we found a reverse match we must alter the opcode direction
6951 bit and clear/flip the regmem modifier one. found_reverse_match
6952 holds bits to change (different for int & float insns). */
29b0f896
AM
6953
6954 i.tm.base_opcode ^= found_reverse_match;
6955
f5eb1d70
JB
6956 i.tm.operand_types[0] = operand_types[i.operands - 1];
6957 i.tm.operand_types[i.operands - 1] = operand_types[0];
dfd69174
JB
6958
6959 /* Certain SIMD insns have their load forms specified in the opcode
6960 table, and hence we need to _set_ RegMem instead of clearing it.
6961 We need to avoid setting the bit though on insns like KMOVW. */
6962 i.tm.opcode_modifier.regmem
6963 = i.tm.opcode_modifier.modrm && i.tm.opcode_modifier.d
6964 && i.tm.operands > 2U - i.tm.opcode_modifier.sse2avx
6965 && !i.tm.opcode_modifier.regmem;
8bd915b7
JB
6966 break;
6967
6968 case Opcode_VexW:
6969 /* Only the first two register operands need reversing, alongside
6970 flipping VEX.W. */
6971 i.tm.opcode_modifier.vexw ^= VEXW0 ^ VEXW1;
6972
6973 j = i.tm.operand_types[0].bitfield.imm8;
6974 i.tm.operand_types[j] = operand_types[j + 1];
6975 i.tm.operand_types[j + 1] = operand_types[j];
6976 break;
29b0f896
AM
6977 }
6978
fa99fab2 6979 return t;
29b0f896
AM
6980}
6981
6982static int
e3bb37b5 6983check_string (void)
29b0f896 6984{
51c8edf6
JB
6985 unsigned int es_op = i.tm.opcode_modifier.isstring - IS_STRING_ES_OP0;
6986 unsigned int op = i.tm.operand_types[0].bitfield.baseindex ? es_op : 0;
8dc0818e 6987
5e042380 6988 if (i.seg[op] != NULL && i.seg[op] != reg_es)
29b0f896 6989 {
51c8edf6
JB
6990 as_bad (_("`%s' operand %u must use `%ses' segment"),
6991 i.tm.name,
6992 intel_syntax ? i.tm.operands - es_op : es_op + 1,
6993 register_prefix);
6994 return 0;
29b0f896 6995 }
51c8edf6
JB
6996
6997 /* There's only ever one segment override allowed per instruction.
6998 This instruction possibly has a legal segment override on the
6999 second operand, so copy the segment to where non-string
7000 instructions store it, allowing common code. */
7001 i.seg[op] = i.seg[1];
7002
29b0f896
AM
7003 return 1;
7004}
7005
7006static int
543613e9 7007process_suffix (void)
29b0f896 7008{
5b7c81bd 7009 bool is_crc32 = false, is_movx = false;
8b65b895 7010
29b0f896
AM
7011 /* If matched instruction specifies an explicit instruction mnemonic
7012 suffix, use it. */
673fe0f0 7013 if (i.tm.opcode_modifier.size == SIZE16)
40fb9820 7014 i.suffix = WORD_MNEM_SUFFIX;
673fe0f0 7015 else if (i.tm.opcode_modifier.size == SIZE32)
40fb9820 7016 i.suffix = LONG_MNEM_SUFFIX;
673fe0f0 7017 else if (i.tm.opcode_modifier.size == SIZE64)
40fb9820 7018 i.suffix = QWORD_MNEM_SUFFIX;
13e600d0 7019 else if (i.reg_operands
c8f8eebc
JB
7020 && (i.operands > 1 || i.types[0].bitfield.class == Reg)
7021 && !i.tm.opcode_modifier.addrprefixopreg)
29b0f896 7022 {
65fca059 7023 unsigned int numop = i.operands;
389d00a5
JB
7024
7025 /* MOVSX/MOVZX */
7026 is_movx = (i.tm.opcode_modifier.opcodespace == SPACE_0F
7027 && (i.tm.base_opcode | 8) == 0xbe)
7028 || (i.tm.opcode_modifier.opcodespace == SPACE_BASE
7029 && i.tm.base_opcode == 0x63
7030 && i.tm.cpu_flags.bitfield.cpu64);
7031
8b65b895 7032 /* CRC32 */
389d00a5
JB
7033 is_crc32 = (i.tm.base_opcode == 0xf0
7034 && i.tm.opcode_modifier.opcodespace == SPACE_0F38
8b65b895 7035 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2);
65fca059
JB
7036
7037 /* movsx/movzx want only their source operand considered here, for the
7038 ambiguity checking below. The suffix will be replaced afterwards
7039 to represent the destination (register). */
389d00a5 7040 if (is_movx && (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63))
65fca059
JB
7041 --i.operands;
7042
643bb870 7043 /* crc32 needs REX.W set regardless of suffix / source operand size. */
8b65b895 7044 if (is_crc32 && i.tm.operand_types[1].bitfield.qword)
643bb870
JB
7045 i.rex |= REX_W;
7046
29b0f896 7047 /* If there's no instruction mnemonic suffix we try to invent one
13e600d0 7048 based on GPR operands. */
29b0f896
AM
7049 if (!i.suffix)
7050 {
7051 /* We take i.suffix from the last register operand specified,
7052 Destination register type is more significant than source
381d071f
L
7053 register type. crc32 in SSE4.2 prefers source register
7054 type. */
8b65b895 7055 unsigned int op = is_crc32 ? 1 : i.operands;
20592a94 7056
1a035124
JB
7057 while (op--)
7058 if (i.tm.operand_types[op].bitfield.instance == InstanceNone
7059 || i.tm.operand_types[op].bitfield.instance == Accum)
7060 {
7061 if (i.types[op].bitfield.class != Reg)
7062 continue;
7063 if (i.types[op].bitfield.byte)
7064 i.suffix = BYTE_MNEM_SUFFIX;
7065 else if (i.types[op].bitfield.word)
7066 i.suffix = WORD_MNEM_SUFFIX;
7067 else if (i.types[op].bitfield.dword)
7068 i.suffix = LONG_MNEM_SUFFIX;
7069 else if (i.types[op].bitfield.qword)
7070 i.suffix = QWORD_MNEM_SUFFIX;
7071 else
7072 continue;
7073 break;
7074 }
65fca059
JB
7075
7076 /* As an exception, movsx/movzx silently default to a byte source
7077 in AT&T mode. */
389d00a5 7078 if (is_movx && i.tm.opcode_modifier.w && !i.suffix && !intel_syntax)
65fca059 7079 i.suffix = BYTE_MNEM_SUFFIX;
29b0f896
AM
7080 }
7081 else if (i.suffix == BYTE_MNEM_SUFFIX)
7082 {
2eb952a4 7083 if (intel_syntax
3cd7f3e3 7084 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
2eb952a4
L
7085 && i.tm.opcode_modifier.no_bsuf)
7086 i.suffix = 0;
7087 else if (!check_byte_reg ())
29b0f896
AM
7088 return 0;
7089 }
7090 else if (i.suffix == LONG_MNEM_SUFFIX)
7091 {
2eb952a4 7092 if (intel_syntax
3cd7f3e3 7093 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
9f123b91
JB
7094 && i.tm.opcode_modifier.no_lsuf
7095 && !i.tm.opcode_modifier.todword
7096 && !i.tm.opcode_modifier.toqword)
2eb952a4
L
7097 i.suffix = 0;
7098 else if (!check_long_reg ())
29b0f896
AM
7099 return 0;
7100 }
7101 else if (i.suffix == QWORD_MNEM_SUFFIX)
7102 {
955e1e6a 7103 if (intel_syntax
3cd7f3e3 7104 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
9f123b91
JB
7105 && i.tm.opcode_modifier.no_qsuf
7106 && !i.tm.opcode_modifier.todword
7107 && !i.tm.opcode_modifier.toqword)
955e1e6a
L
7108 i.suffix = 0;
7109 else if (!check_qword_reg ())
29b0f896
AM
7110 return 0;
7111 }
7112 else if (i.suffix == WORD_MNEM_SUFFIX)
7113 {
2eb952a4 7114 if (intel_syntax
3cd7f3e3 7115 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE
2eb952a4
L
7116 && i.tm.opcode_modifier.no_wsuf)
7117 i.suffix = 0;
7118 else if (!check_word_reg ())
29b0f896
AM
7119 return 0;
7120 }
3cd7f3e3
L
7121 else if (intel_syntax
7122 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
7123 /* Do nothing if the instruction is going to ignore the prefix. */
7124 ;
7125 else
7126 abort ();
65fca059
JB
7127
7128 /* Undo the movsx/movzx change done above. */
7129 i.operands = numop;
29b0f896 7130 }
3cd7f3e3
L
7131 else if (i.tm.opcode_modifier.mnemonicsize == DEFAULTSIZE
7132 && !i.suffix)
29b0f896 7133 {
13e600d0
JB
7134 i.suffix = stackop_size;
7135 if (stackop_size == LONG_MNEM_SUFFIX)
06f74c5c
L
7136 {
7137 /* stackop_size is set to LONG_MNEM_SUFFIX for the
7138 .code16gcc directive to support 16-bit mode with
7139 32-bit address. For IRET without a suffix, generate
7140 16-bit IRET (opcode 0xcf) to return from an interrupt
7141 handler. */
13e600d0
JB
7142 if (i.tm.base_opcode == 0xcf)
7143 {
7144 i.suffix = WORD_MNEM_SUFFIX;
7145 as_warn (_("generating 16-bit `iret' for .code16gcc directive"));
7146 }
7147 /* Warn about changed behavior for segment register push/pop. */
7148 else if ((i.tm.base_opcode | 1) == 0x07)
7149 as_warn (_("generating 32-bit `%s', unlike earlier gas versions"),
7150 i.tm.name);
06f74c5c 7151 }
29b0f896 7152 }
c006a730 7153 else if (!i.suffix
0cfa3eb3
JB
7154 && (i.tm.opcode_modifier.jump == JUMP_ABSOLUTE
7155 || i.tm.opcode_modifier.jump == JUMP_BYTE
7156 || i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT
389d00a5
JB
7157 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
7158 && i.tm.base_opcode == 0x01 /* [ls][gi]dt */
64e74474 7159 && i.tm.extension_opcode <= 3)))
9306ca4a
JB
7160 {
7161 switch (flag_code)
7162 {
7163 case CODE_64BIT:
40fb9820 7164 if (!i.tm.opcode_modifier.no_qsuf)
9306ca4a 7165 {
828c2a25
JB
7166 if (i.tm.opcode_modifier.jump == JUMP_BYTE
7167 || i.tm.opcode_modifier.no_lsuf)
7168 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a
JB
7169 break;
7170 }
1a0670f3 7171 /* Fall through. */
9306ca4a 7172 case CODE_32BIT:
40fb9820 7173 if (!i.tm.opcode_modifier.no_lsuf)
9306ca4a
JB
7174 i.suffix = LONG_MNEM_SUFFIX;
7175 break;
7176 case CODE_16BIT:
40fb9820 7177 if (!i.tm.opcode_modifier.no_wsuf)
9306ca4a
JB
7178 i.suffix = WORD_MNEM_SUFFIX;
7179 break;
7180 }
7181 }
252b5132 7182
c006a730 7183 if (!i.suffix
3cd7f3e3 7184 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8
JB
7185 /* Also cover lret/retf/iret in 64-bit mode. */
7186 || (flag_code == CODE_64BIT
7187 && !i.tm.opcode_modifier.no_lsuf
7188 && !i.tm.opcode_modifier.no_qsuf))
3cd7f3e3 7189 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
8bbb3ad8
JB
7190 /* Explicit sizing prefixes are assumed to disambiguate insns. */
7191 && !i.prefix[DATA_PREFIX] && !(i.prefix[REX_PREFIX] & REX_W)
62b3f548
JB
7192 /* Accept FLDENV et al without suffix. */
7193 && (i.tm.opcode_modifier.no_ssuf || i.tm.opcode_modifier.floatmf))
29b0f896 7194 {
6c0946d0 7195 unsigned int suffixes, evex = 0;
c006a730
JB
7196
7197 suffixes = !i.tm.opcode_modifier.no_bsuf;
7198 if (!i.tm.opcode_modifier.no_wsuf)
7199 suffixes |= 1 << 1;
7200 if (!i.tm.opcode_modifier.no_lsuf)
7201 suffixes |= 1 << 2;
7202 if (!i.tm.opcode_modifier.no_ldsuf)
7203 suffixes |= 1 << 3;
7204 if (!i.tm.opcode_modifier.no_ssuf)
7205 suffixes |= 1 << 4;
7206 if (flag_code == CODE_64BIT && !i.tm.opcode_modifier.no_qsuf)
7207 suffixes |= 1 << 5;
7208
6c0946d0
JB
7209 /* For [XYZ]MMWORD operands inspect operand sizes. While generally
7210 also suitable for AT&T syntax mode, it was requested that this be
7211 restricted to just Intel syntax. */
a5748e0d
JB
7212 if (intel_syntax && is_any_vex_encoding (&i.tm)
7213 && !i.broadcast.type && !i.broadcast.bytes)
6c0946d0 7214 {
b9915cbc 7215 unsigned int op;
6c0946d0 7216
b9915cbc 7217 for (op = 0; op < i.tm.operands; ++op)
6c0946d0 7218 {
b9915cbc
JB
7219 if (is_evex_encoding (&i.tm)
7220 && !cpu_arch_flags.bitfield.cpuavx512vl)
6c0946d0 7221 {
b9915cbc
JB
7222 if (i.tm.operand_types[op].bitfield.ymmword)
7223 i.tm.operand_types[op].bitfield.xmmword = 0;
7224 if (i.tm.operand_types[op].bitfield.zmmword)
7225 i.tm.operand_types[op].bitfield.ymmword = 0;
7226 if (!i.tm.opcode_modifier.evex
7227 || i.tm.opcode_modifier.evex == EVEXDYN)
7228 i.tm.opcode_modifier.evex = EVEX512;
7229 }
6c0946d0 7230
b9915cbc
JB
7231 if (i.tm.operand_types[op].bitfield.xmmword
7232 + i.tm.operand_types[op].bitfield.ymmword
7233 + i.tm.operand_types[op].bitfield.zmmword < 2)
7234 continue;
6c0946d0 7235
b9915cbc
JB
7236 /* Any properly sized operand disambiguates the insn. */
7237 if (i.types[op].bitfield.xmmword
7238 || i.types[op].bitfield.ymmword
7239 || i.types[op].bitfield.zmmword)
7240 {
7241 suffixes &= ~(7 << 6);
7242 evex = 0;
7243 break;
7244 }
6c0946d0 7245
b9915cbc
JB
7246 if ((i.flags[op] & Operand_Mem)
7247 && i.tm.operand_types[op].bitfield.unspecified)
7248 {
7249 if (i.tm.operand_types[op].bitfield.xmmword)
7250 suffixes |= 1 << 6;
7251 if (i.tm.operand_types[op].bitfield.ymmword)
7252 suffixes |= 1 << 7;
7253 if (i.tm.operand_types[op].bitfield.zmmword)
7254 suffixes |= 1 << 8;
7255 if (is_evex_encoding (&i.tm))
7256 evex = EVEX512;
6c0946d0
JB
7257 }
7258 }
7259 }
7260
7261 /* Are multiple suffixes / operand sizes allowed? */
c006a730 7262 if (suffixes & (suffixes - 1))
9306ca4a 7263 {
873494c8 7264 if (intel_syntax
3cd7f3e3 7265 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8 7266 || operand_check == check_error))
9306ca4a 7267 {
c006a730 7268 as_bad (_("ambiguous operand size for `%s'"), i.tm.name);
9306ca4a
JB
7269 return 0;
7270 }
c006a730 7271 if (operand_check == check_error)
9306ca4a 7272 {
c006a730
JB
7273 as_bad (_("no instruction mnemonic suffix given and "
7274 "no register operands; can't size `%s'"), i.tm.name);
9306ca4a
JB
7275 return 0;
7276 }
c006a730 7277 if (operand_check == check_warning)
873494c8
JB
7278 as_warn (_("%s; using default for `%s'"),
7279 intel_syntax
7280 ? _("ambiguous operand size")
7281 : _("no instruction mnemonic suffix given and "
7282 "no register operands"),
7283 i.tm.name);
c006a730
JB
7284
7285 if (i.tm.opcode_modifier.floatmf)
7286 i.suffix = SHORT_MNEM_SUFFIX;
389d00a5 7287 else if (is_movx)
65fca059 7288 /* handled below */;
6c0946d0
JB
7289 else if (evex)
7290 i.tm.opcode_modifier.evex = evex;
c006a730
JB
7291 else if (flag_code == CODE_16BIT)
7292 i.suffix = WORD_MNEM_SUFFIX;
1a035124 7293 else if (!i.tm.opcode_modifier.no_lsuf)
c006a730 7294 i.suffix = LONG_MNEM_SUFFIX;
1a035124
JB
7295 else
7296 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a 7297 }
29b0f896 7298 }
252b5132 7299
389d00a5 7300 if (is_movx)
65fca059
JB
7301 {
7302 /* In Intel syntax, movsx/movzx must have a "suffix" (checked above).
7303 In AT&T syntax, if there is no suffix (warned about above), the default
7304 will be byte extension. */
7305 if (i.tm.opcode_modifier.w && i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
7306 i.tm.base_opcode |= 1;
7307
7308 /* For further processing, the suffix should represent the destination
7309 (register). This is already the case when one was used with
7310 mov[sz][bw]*, but we need to replace it for mov[sz]x, or if there was
7311 no suffix to begin with. */
7312 if (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63 || !i.suffix)
7313 {
7314 if (i.types[1].bitfield.word)
7315 i.suffix = WORD_MNEM_SUFFIX;
7316 else if (i.types[1].bitfield.qword)
7317 i.suffix = QWORD_MNEM_SUFFIX;
7318 else
7319 i.suffix = LONG_MNEM_SUFFIX;
7320
7321 i.tm.opcode_modifier.w = 0;
7322 }
7323 }
7324
50128d0c
JB
7325 if (!i.tm.opcode_modifier.modrm && i.reg_operands && i.tm.operands < 3)
7326 i.short_form = (i.tm.operand_types[0].bitfield.class == Reg)
7327 != (i.tm.operand_types[1].bitfield.class == Reg);
7328
d2224064
JB
7329 /* Change the opcode based on the operand size given by i.suffix. */
7330 switch (i.suffix)
29b0f896 7331 {
d2224064
JB
7332 /* Size floating point instruction. */
7333 case LONG_MNEM_SUFFIX:
7334 if (i.tm.opcode_modifier.floatmf)
7335 {
7336 i.tm.base_opcode ^= 4;
7337 break;
7338 }
7339 /* fall through */
7340 case WORD_MNEM_SUFFIX:
7341 case QWORD_MNEM_SUFFIX:
29b0f896 7342 /* It's not a byte, select word/dword operation. */
40fb9820 7343 if (i.tm.opcode_modifier.w)
29b0f896 7344 {
50128d0c 7345 if (i.short_form)
29b0f896
AM
7346 i.tm.base_opcode |= 8;
7347 else
7348 i.tm.base_opcode |= 1;
7349 }
d2224064
JB
7350 /* fall through */
7351 case SHORT_MNEM_SUFFIX:
29b0f896
AM
7352 /* Now select between word & dword operations via the operand
7353 size prefix, except for instructions that will ignore this
7354 prefix anyway. */
c8f8eebc 7355 if (i.suffix != QWORD_MNEM_SUFFIX
3cd7f3e3 7356 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
c8f8eebc
JB
7357 && !i.tm.opcode_modifier.floatmf
7358 && !is_any_vex_encoding (&i.tm)
7359 && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
7360 || (flag_code == CODE_64BIT
7361 && i.tm.opcode_modifier.jump == JUMP_BYTE)))
24eab124
AM
7362 {
7363 unsigned int prefix = DATA_PREFIX_OPCODE;
543613e9 7364
0cfa3eb3 7365 if (i.tm.opcode_modifier.jump == JUMP_BYTE) /* jcxz, loop */
29b0f896 7366 prefix = ADDR_PREFIX_OPCODE;
252b5132 7367
29b0f896
AM
7368 if (!add_prefix (prefix))
7369 return 0;
24eab124 7370 }
252b5132 7371
29b0f896
AM
7372 /* Set mode64 for an operand. */
7373 if (i.suffix == QWORD_MNEM_SUFFIX
9146926a 7374 && flag_code == CODE_64BIT
d2224064 7375 && !i.tm.opcode_modifier.norex64
4ed21b58 7376 && !i.tm.opcode_modifier.vexw
46e883c5 7377 /* Special case for xchg %rax,%rax. It is NOP and doesn't
d2224064
JB
7378 need rex64. */
7379 && ! (i.operands == 2
7380 && i.tm.base_opcode == 0x90
7381 && i.tm.extension_opcode == None
75e5731b
JB
7382 && i.types[0].bitfield.instance == Accum
7383 && i.types[0].bitfield.qword
7384 && i.types[1].bitfield.instance == Accum
7385 && i.types[1].bitfield.qword))
d2224064 7386 i.rex |= REX_W;
3e73aa7c 7387
d2224064 7388 break;
8bbb3ad8
JB
7389
7390 case 0:
f9a6a8f0 7391 /* Select word/dword/qword operation with explicit data sizing prefix
8bbb3ad8
JB
7392 when there are no suitable register operands. */
7393 if (i.tm.opcode_modifier.w
7394 && (i.prefix[DATA_PREFIX] || (i.prefix[REX_PREFIX] & REX_W))
7395 && (!i.reg_operands
7396 || (i.reg_operands == 1
7397 /* ShiftCount */
7398 && (i.tm.operand_types[0].bitfield.instance == RegC
7399 /* InOutPortReg */
7400 || i.tm.operand_types[0].bitfield.instance == RegD
7401 || i.tm.operand_types[1].bitfield.instance == RegD
7402 /* CRC32 */
8b65b895 7403 || is_crc32))))
8bbb3ad8
JB
7404 i.tm.base_opcode |= 1;
7405 break;
29b0f896 7406 }
7ecd2f8b 7407
c8f8eebc 7408 if (i.tm.opcode_modifier.addrprefixopreg)
c0a30a9f 7409 {
c8f8eebc
JB
7410 gas_assert (!i.suffix);
7411 gas_assert (i.reg_operands);
c0a30a9f 7412
c8f8eebc
JB
7413 if (i.tm.operand_types[0].bitfield.instance == Accum
7414 || i.operands == 1)
7415 {
7416 /* The address size override prefix changes the size of the
7417 first operand. */
7418 if (flag_code == CODE_64BIT
7419 && i.op[0].regs->reg_type.bitfield.word)
7420 {
7421 as_bad (_("16-bit addressing unavailable for `%s'"),
7422 i.tm.name);
7423 return 0;
7424 }
7425
7426 if ((flag_code == CODE_32BIT
7427 ? i.op[0].regs->reg_type.bitfield.word
7428 : i.op[0].regs->reg_type.bitfield.dword)
7429 && !add_prefix (ADDR_PREFIX_OPCODE))
7430 return 0;
7431 }
c0a30a9f
L
7432 else
7433 {
c8f8eebc
JB
7434 /* Check invalid register operand when the address size override
7435 prefix changes the size of register operands. */
7436 unsigned int op;
7437 enum { need_word, need_dword, need_qword } need;
7438
27f13469 7439 /* Check the register operand for the address size prefix if
b3a3496f 7440 the memory operand has no real registers, like symbol, DISP
829f3fe1 7441 or bogus (x32-only) symbol(%rip) when symbol(%eip) is meant. */
27f13469
L
7442 if (i.mem_operands == 1
7443 && i.reg_operands == 1
7444 && i.operands == 2
27f13469 7445 && i.types[1].bitfield.class == Reg
b3a3496f
L
7446 && (flag_code == CODE_32BIT
7447 ? i.op[1].regs->reg_type.bitfield.word
7448 : i.op[1].regs->reg_type.bitfield.dword)
7449 && ((i.base_reg == NULL && i.index_reg == NULL)
829f3fe1
JB
7450#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
7451 || (x86_elf_abi == X86_64_X32_ABI
7452 && i.base_reg
b3a3496f
L
7453 && i.base_reg->reg_num == RegIP
7454 && i.base_reg->reg_type.bitfield.qword))
829f3fe1
JB
7455#else
7456 || 0)
7457#endif
27f13469
L
7458 && !add_prefix (ADDR_PREFIX_OPCODE))
7459 return 0;
7460
c8f8eebc
JB
7461 if (flag_code == CODE_32BIT)
7462 need = i.prefix[ADDR_PREFIX] ? need_word : need_dword;
7463 else if (i.prefix[ADDR_PREFIX])
c0a30a9f
L
7464 need = need_dword;
7465 else
7466 need = flag_code == CODE_64BIT ? need_qword : need_word;
c0a30a9f 7467
c8f8eebc
JB
7468 for (op = 0; op < i.operands; op++)
7469 {
7470 if (i.types[op].bitfield.class != Reg)
7471 continue;
7472
7473 switch (need)
7474 {
7475 case need_word:
7476 if (i.op[op].regs->reg_type.bitfield.word)
7477 continue;
7478 break;
7479 case need_dword:
7480 if (i.op[op].regs->reg_type.bitfield.dword)
7481 continue;
7482 break;
7483 case need_qword:
7484 if (i.op[op].regs->reg_type.bitfield.qword)
7485 continue;
7486 break;
7487 }
7488
7489 as_bad (_("invalid register operand size for `%s'"),
7490 i.tm.name);
7491 return 0;
7492 }
7493 }
c0a30a9f
L
7494 }
7495
29b0f896
AM
7496 return 1;
7497}
3e73aa7c 7498
29b0f896 7499static int
543613e9 7500check_byte_reg (void)
29b0f896
AM
7501{
7502 int op;
543613e9 7503
29b0f896
AM
7504 for (op = i.operands; --op >= 0;)
7505 {
dc821c5f 7506 /* Skip non-register operands. */
bab6aec1 7507 if (i.types[op].bitfield.class != Reg)
dc821c5f
JB
7508 continue;
7509
29b0f896
AM
7510 /* If this is an eight bit register, it's OK. If it's the 16 or
7511 32 bit version of an eight bit register, we will just use the
7512 low portion, and that's OK too. */
dc821c5f 7513 if (i.types[op].bitfield.byte)
29b0f896
AM
7514 continue;
7515
5a819eb9 7516 /* I/O port address operands are OK too. */
75e5731b
JB
7517 if (i.tm.operand_types[op].bitfield.instance == RegD
7518 && i.tm.operand_types[op].bitfield.word)
5a819eb9
JB
7519 continue;
7520
9706160a 7521 /* crc32 only wants its source operand checked here. */
389d00a5
JB
7522 if (i.tm.base_opcode == 0xf0
7523 && i.tm.opcode_modifier.opcodespace == SPACE_0F38
8b65b895
L
7524 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2
7525 && op != 0)
9344ff29
L
7526 continue;
7527
29b0f896 7528 /* Any other register is bad. */
73c76375
JB
7529 as_bad (_("`%s%s' not allowed with `%s%c'"),
7530 register_prefix, i.op[op].regs->reg_name,
7531 i.tm.name, i.suffix);
7532 return 0;
29b0f896
AM
7533 }
7534 return 1;
7535}
7536
7537static int
e3bb37b5 7538check_long_reg (void)
29b0f896
AM
7539{
7540 int op;
7541
7542 for (op = i.operands; --op >= 0;)
dc821c5f 7543 /* Skip non-register operands. */
bab6aec1 7544 if (i.types[op].bitfield.class != Reg)
dc821c5f 7545 continue;
29b0f896
AM
7546 /* Reject eight bit registers, except where the template requires
7547 them. (eg. movzb) */
dc821c5f 7548 else if (i.types[op].bitfield.byte
bab6aec1 7549 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7550 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7551 && (i.tm.operand_types[op].bitfield.word
7552 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7553 {
a540244d
L
7554 as_bad (_("`%s%s' not allowed with `%s%c'"),
7555 register_prefix,
29b0f896
AM
7556 i.op[op].regs->reg_name,
7557 i.tm.name,
7558 i.suffix);
7559 return 0;
7560 }
be4c5e58
L
7561 /* Error if the e prefix on a general reg is missing. */
7562 else if (i.types[op].bitfield.word
bab6aec1 7563 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7564 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7565 && i.tm.operand_types[op].bitfield.dword)
29b0f896 7566 {
be4c5e58
L
7567 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7568 register_prefix, i.op[op].regs->reg_name,
7569 i.suffix);
7570 return 0;
252b5132 7571 }
e4630f71 7572 /* Warn if the r prefix on a general reg is present. */
dc821c5f 7573 else if (i.types[op].bitfield.qword
bab6aec1 7574 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7575 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7576 && i.tm.operand_types[op].bitfield.dword)
252b5132 7577 {
34828aad 7578 if (intel_syntax
65fca059 7579 && i.tm.opcode_modifier.toqword
3528c362 7580 && i.types[0].bitfield.class != RegSIMD)
34828aad 7581 {
ca61edf2 7582 /* Convert to QWORD. We want REX byte. */
34828aad
L
7583 i.suffix = QWORD_MNEM_SUFFIX;
7584 }
7585 else
7586 {
2b5d6a91 7587 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
34828aad
L
7588 register_prefix, i.op[op].regs->reg_name,
7589 i.suffix);
7590 return 0;
7591 }
29b0f896
AM
7592 }
7593 return 1;
7594}
252b5132 7595
29b0f896 7596static int
e3bb37b5 7597check_qword_reg (void)
29b0f896
AM
7598{
7599 int op;
252b5132 7600
29b0f896 7601 for (op = i.operands; --op >= 0; )
dc821c5f 7602 /* Skip non-register operands. */
bab6aec1 7603 if (i.types[op].bitfield.class != Reg)
dc821c5f 7604 continue;
29b0f896
AM
7605 /* Reject eight bit registers, except where the template requires
7606 them. (eg. movzb) */
dc821c5f 7607 else if (i.types[op].bitfield.byte
bab6aec1 7608 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7609 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7610 && (i.tm.operand_types[op].bitfield.word
7611 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7612 {
a540244d
L
7613 as_bad (_("`%s%s' not allowed with `%s%c'"),
7614 register_prefix,
29b0f896
AM
7615 i.op[op].regs->reg_name,
7616 i.tm.name,
7617 i.suffix);
7618 return 0;
7619 }
e4630f71 7620 /* Warn if the r prefix on a general reg is missing. */
dc821c5f
JB
7621 else if ((i.types[op].bitfield.word
7622 || i.types[op].bitfield.dword)
bab6aec1 7623 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7624 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7625 && i.tm.operand_types[op].bitfield.qword)
29b0f896
AM
7626 {
7627 /* Prohibit these changes in the 64bit mode, since the
7628 lowering is more complicated. */
34828aad 7629 if (intel_syntax
ca61edf2 7630 && i.tm.opcode_modifier.todword
3528c362 7631 && i.types[0].bitfield.class != RegSIMD)
34828aad 7632 {
ca61edf2 7633 /* Convert to DWORD. We don't want REX byte. */
34828aad
L
7634 i.suffix = LONG_MNEM_SUFFIX;
7635 }
7636 else
7637 {
2b5d6a91 7638 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
34828aad
L
7639 register_prefix, i.op[op].regs->reg_name,
7640 i.suffix);
7641 return 0;
7642 }
252b5132 7643 }
29b0f896
AM
7644 return 1;
7645}
252b5132 7646
29b0f896 7647static int
e3bb37b5 7648check_word_reg (void)
29b0f896
AM
7649{
7650 int op;
7651 for (op = i.operands; --op >= 0;)
dc821c5f 7652 /* Skip non-register operands. */
bab6aec1 7653 if (i.types[op].bitfield.class != Reg)
dc821c5f 7654 continue;
29b0f896
AM
7655 /* Reject eight bit registers, except where the template requires
7656 them. (eg. movzb) */
dc821c5f 7657 else if (i.types[op].bitfield.byte
bab6aec1 7658 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7659 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7660 && (i.tm.operand_types[op].bitfield.word
7661 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7662 {
a540244d
L
7663 as_bad (_("`%s%s' not allowed with `%s%c'"),
7664 register_prefix,
29b0f896
AM
7665 i.op[op].regs->reg_name,
7666 i.tm.name,
7667 i.suffix);
7668 return 0;
7669 }
9706160a
JB
7670 /* Error if the e or r prefix on a general reg is present. */
7671 else if ((i.types[op].bitfield.dword
dc821c5f 7672 || i.types[op].bitfield.qword)
bab6aec1 7673 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7674 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7675 && i.tm.operand_types[op].bitfield.word)
252b5132 7676 {
9706160a
JB
7677 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7678 register_prefix, i.op[op].regs->reg_name,
7679 i.suffix);
7680 return 0;
29b0f896 7681 }
0cc78721
CL
7682 /* For some instructions need encode as EVEX.W=1 without explicit VexW1. */
7683 else if (i.types[op].bitfield.qword
7684 && intel_syntax
7685 && i.tm.opcode_modifier.toqword)
7686 {
7687 /* Convert to QWORD. We want EVEX.W byte. */
7688 i.suffix = QWORD_MNEM_SUFFIX;
7689 }
29b0f896
AM
7690 return 1;
7691}
252b5132 7692
29b0f896 7693static int
40fb9820 7694update_imm (unsigned int j)
29b0f896 7695{
bc0844ae 7696 i386_operand_type overlap = i.types[j];
40fb9820
L
7697 if ((overlap.bitfield.imm8
7698 || overlap.bitfield.imm8s
7699 || overlap.bitfield.imm16
7700 || overlap.bitfield.imm32
7701 || overlap.bitfield.imm32s
7702 || overlap.bitfield.imm64)
0dfbf9d7
L
7703 && !operand_type_equal (&overlap, &imm8)
7704 && !operand_type_equal (&overlap, &imm8s)
7705 && !operand_type_equal (&overlap, &imm16)
7706 && !operand_type_equal (&overlap, &imm32)
7707 && !operand_type_equal (&overlap, &imm32s)
7708 && !operand_type_equal (&overlap, &imm64))
29b0f896
AM
7709 {
7710 if (i.suffix)
7711 {
40fb9820
L
7712 i386_operand_type temp;
7713
0dfbf9d7 7714 operand_type_set (&temp, 0);
7ab9ffdd 7715 if (i.suffix == BYTE_MNEM_SUFFIX)
40fb9820
L
7716 {
7717 temp.bitfield.imm8 = overlap.bitfield.imm8;
7718 temp.bitfield.imm8s = overlap.bitfield.imm8s;
7719 }
7720 else if (i.suffix == WORD_MNEM_SUFFIX)
7721 temp.bitfield.imm16 = overlap.bitfield.imm16;
7722 else if (i.suffix == QWORD_MNEM_SUFFIX)
7723 {
7724 temp.bitfield.imm64 = overlap.bitfield.imm64;
7725 temp.bitfield.imm32s = overlap.bitfield.imm32s;
7726 }
7727 else
7728 temp.bitfield.imm32 = overlap.bitfield.imm32;
7729 overlap = temp;
29b0f896 7730 }
0dfbf9d7
L
7731 else if (operand_type_equal (&overlap, &imm16_32_32s)
7732 || operand_type_equal (&overlap, &imm16_32)
7733 || operand_type_equal (&overlap, &imm16_32s))
29b0f896 7734 {
40fb9820 7735 if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
65da13b5 7736 overlap = imm16;
40fb9820 7737 else
65da13b5 7738 overlap = imm32s;
29b0f896 7739 }
8bbb3ad8
JB
7740 else if (i.prefix[REX_PREFIX] & REX_W)
7741 overlap = operand_type_and (overlap, imm32s);
7742 else if (i.prefix[DATA_PREFIX])
7743 overlap = operand_type_and (overlap,
7744 flag_code != CODE_16BIT ? imm16 : imm32);
0dfbf9d7
L
7745 if (!operand_type_equal (&overlap, &imm8)
7746 && !operand_type_equal (&overlap, &imm8s)
7747 && !operand_type_equal (&overlap, &imm16)
7748 && !operand_type_equal (&overlap, &imm32)
7749 && !operand_type_equal (&overlap, &imm32s)
7750 && !operand_type_equal (&overlap, &imm64))
29b0f896 7751 {
4eed87de
AM
7752 as_bad (_("no instruction mnemonic suffix given; "
7753 "can't determine immediate size"));
29b0f896
AM
7754 return 0;
7755 }
7756 }
40fb9820 7757 i.types[j] = overlap;
29b0f896 7758
40fb9820
L
7759 return 1;
7760}
7761
7762static int
7763finalize_imm (void)
7764{
bc0844ae 7765 unsigned int j, n;
29b0f896 7766
bc0844ae
L
7767 /* Update the first 2 immediate operands. */
7768 n = i.operands > 2 ? 2 : i.operands;
7769 if (n)
7770 {
7771 for (j = 0; j < n; j++)
7772 if (update_imm (j) == 0)
7773 return 0;
40fb9820 7774
bc0844ae
L
7775 /* The 3rd operand can't be immediate operand. */
7776 gas_assert (operand_type_check (i.types[2], imm) == 0);
7777 }
29b0f896
AM
7778
7779 return 1;
7780}
7781
7782static int
e3bb37b5 7783process_operands (void)
29b0f896
AM
7784{
7785 /* Default segment register this instruction will use for memory
7786 accesses. 0 means unknown. This is only for optimizing out
7787 unnecessary segment overrides. */
5e042380 7788 const reg_entry *default_seg = NULL;
29b0f896 7789
a5aeccd9
JB
7790 if (i.tm.opcode_modifier.sse2avx)
7791 {
7792 /* Legacy encoded insns allow explicit REX prefixes, so these prefixes
7793 need converting. */
7794 i.rex |= i.prefix[REX_PREFIX] & (REX_W | REX_R | REX_X | REX_B);
7795 i.prefix[REX_PREFIX] = 0;
7796 i.rex_encoding = 0;
7797 }
c423d21a
JB
7798 /* ImmExt should be processed after SSE2AVX. */
7799 else if (i.tm.opcode_modifier.immext)
7800 process_immext ();
a5aeccd9 7801
2426c15f 7802 if (i.tm.opcode_modifier.sse2avx && i.tm.opcode_modifier.vexvvvv)
29b0f896 7803 {
91d6fa6a
NC
7804 unsigned int dupl = i.operands;
7805 unsigned int dest = dupl - 1;
9fcfb3d7
L
7806 unsigned int j;
7807
c0f3af97 7808 /* The destination must be an xmm register. */
9c2799c2 7809 gas_assert (i.reg_operands
91d6fa6a 7810 && MAX_OPERANDS > dupl
7ab9ffdd 7811 && operand_type_equal (&i.types[dest], &regxmm));
c0f3af97 7812
75e5731b 7813 if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 7814 && i.tm.operand_types[0].bitfield.xmmword)
e2ec9d29 7815 {
8cd7925b 7816 if (i.tm.opcode_modifier.vexsources == VEX3SOURCES)
c0f3af97
L
7817 {
7818 /* Keep xmm0 for instructions with VEX prefix and 3
7819 sources. */
75e5731b 7820 i.tm.operand_types[0].bitfield.instance = InstanceNone;
3528c362 7821 i.tm.operand_types[0].bitfield.class = RegSIMD;
c0f3af97
L
7822 goto duplicate;
7823 }
e2ec9d29 7824 else
c0f3af97
L
7825 {
7826 /* We remove the first xmm0 and keep the number of
7827 operands unchanged, which in fact duplicates the
7828 destination. */
7829 for (j = 1; j < i.operands; j++)
7830 {
7831 i.op[j - 1] = i.op[j];
7832 i.types[j - 1] = i.types[j];
7833 i.tm.operand_types[j - 1] = i.tm.operand_types[j];
8dc0818e 7834 i.flags[j - 1] = i.flags[j];
c0f3af97
L
7835 }
7836 }
7837 }
7838 else if (i.tm.opcode_modifier.implicit1stxmm0)
7ab9ffdd 7839 {
91d6fa6a 7840 gas_assert ((MAX_OPERANDS - 1) > dupl
8cd7925b
L
7841 && (i.tm.opcode_modifier.vexsources
7842 == VEX3SOURCES));
c0f3af97
L
7843
7844 /* Add the implicit xmm0 for instructions with VEX prefix
7845 and 3 sources. */
7846 for (j = i.operands; j > 0; j--)
7847 {
7848 i.op[j] = i.op[j - 1];
7849 i.types[j] = i.types[j - 1];
7850 i.tm.operand_types[j] = i.tm.operand_types[j - 1];
8dc0818e 7851 i.flags[j] = i.flags[j - 1];
c0f3af97
L
7852 }
7853 i.op[0].regs
629310ab 7854 = (const reg_entry *) str_hash_find (reg_hash, "xmm0");
7ab9ffdd 7855 i.types[0] = regxmm;
c0f3af97
L
7856 i.tm.operand_types[0] = regxmm;
7857
7858 i.operands += 2;
7859 i.reg_operands += 2;
7860 i.tm.operands += 2;
7861
91d6fa6a 7862 dupl++;
c0f3af97 7863 dest++;
91d6fa6a
NC
7864 i.op[dupl] = i.op[dest];
7865 i.types[dupl] = i.types[dest];
7866 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 7867 i.flags[dupl] = i.flags[dest];
e2ec9d29 7868 }
c0f3af97
L
7869 else
7870 {
dc1e8a47 7871 duplicate:
c0f3af97
L
7872 i.operands++;
7873 i.reg_operands++;
7874 i.tm.operands++;
7875
91d6fa6a
NC
7876 i.op[dupl] = i.op[dest];
7877 i.types[dupl] = i.types[dest];
7878 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 7879 i.flags[dupl] = i.flags[dest];
c0f3af97
L
7880 }
7881
7882 if (i.tm.opcode_modifier.immext)
7883 process_immext ();
7884 }
75e5731b 7885 else if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 7886 && i.tm.operand_types[0].bitfield.xmmword)
c0f3af97
L
7887 {
7888 unsigned int j;
7889
9fcfb3d7
L
7890 for (j = 1; j < i.operands; j++)
7891 {
7892 i.op[j - 1] = i.op[j];
7893 i.types[j - 1] = i.types[j];
7894
7895 /* We need to adjust fields in i.tm since they are used by
7896 build_modrm_byte. */
7897 i.tm.operand_types [j - 1] = i.tm.operand_types [j];
8dc0818e
JB
7898
7899 i.flags[j - 1] = i.flags[j];
9fcfb3d7
L
7900 }
7901
e2ec9d29
L
7902 i.operands--;
7903 i.reg_operands--;
e2ec9d29
L
7904 i.tm.operands--;
7905 }
920d2ddc
IT
7906 else if (i.tm.opcode_modifier.implicitquadgroup)
7907 {
a477a8c4
JB
7908 unsigned int regnum, first_reg_in_group, last_reg_in_group;
7909
920d2ddc 7910 /* The second operand must be {x,y,z}mmN, where N is a multiple of 4. */
3528c362 7911 gas_assert (i.operands >= 2 && i.types[1].bitfield.class == RegSIMD);
a477a8c4
JB
7912 regnum = register_number (i.op[1].regs);
7913 first_reg_in_group = regnum & ~3;
7914 last_reg_in_group = first_reg_in_group + 3;
7915 if (regnum != first_reg_in_group)
7916 as_warn (_("source register `%s%s' implicitly denotes"
7917 " `%s%.3s%u' to `%s%.3s%u' source group in `%s'"),
7918 register_prefix, i.op[1].regs->reg_name,
7919 register_prefix, i.op[1].regs->reg_name, first_reg_in_group,
7920 register_prefix, i.op[1].regs->reg_name, last_reg_in_group,
7921 i.tm.name);
7922 }
e2ec9d29
L
7923 else if (i.tm.opcode_modifier.regkludge)
7924 {
7925 /* The imul $imm, %reg instruction is converted into
7926 imul $imm, %reg, %reg, and the clr %reg instruction
7927 is converted into xor %reg, %reg. */
7928
7929 unsigned int first_reg_op;
7930
7931 if (operand_type_check (i.types[0], reg))
7932 first_reg_op = 0;
7933 else
7934 first_reg_op = 1;
7935 /* Pretend we saw the extra register operand. */
9c2799c2 7936 gas_assert (i.reg_operands == 1
7ab9ffdd 7937 && i.op[first_reg_op + 1].regs == 0);
e2ec9d29
L
7938 i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
7939 i.types[first_reg_op + 1] = i.types[first_reg_op];
7940 i.operands++;
7941 i.reg_operands++;
29b0f896
AM
7942 }
7943
85b80b0f 7944 if (i.tm.opcode_modifier.modrm)
29b0f896
AM
7945 {
7946 /* The opcode is completed (modulo i.tm.extension_opcode which
52271982
AM
7947 must be put into the modrm byte). Now, we make the modrm and
7948 index base bytes based on all the info we've collected. */
29b0f896
AM
7949
7950 default_seg = build_modrm_byte ();
7951 }
00cee14f 7952 else if (i.types[0].bitfield.class == SReg)
85b80b0f
JB
7953 {
7954 if (flag_code != CODE_64BIT
7955 ? i.tm.base_opcode == POP_SEG_SHORT
7956 && i.op[0].regs->reg_num == 1
389d00a5 7957 : (i.tm.base_opcode | 1) == (POP_SEG386_SHORT & 0xff)
85b80b0f
JB
7958 && i.op[0].regs->reg_num < 4)
7959 {
7960 as_bad (_("you can't `%s %s%s'"),
7961 i.tm.name, register_prefix, i.op[0].regs->reg_name);
7962 return 0;
7963 }
389d00a5
JB
7964 if (i.op[0].regs->reg_num > 3
7965 && i.tm.opcode_modifier.opcodespace == SPACE_BASE )
85b80b0f 7966 {
389d00a5
JB
7967 i.tm.base_opcode ^= (POP_SEG_SHORT ^ POP_SEG386_SHORT) & 0xff;
7968 i.tm.opcode_modifier.opcodespace = SPACE_0F;
85b80b0f
JB
7969 }
7970 i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
7971 }
389d00a5
JB
7972 else if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
7973 && (i.tm.base_opcode & ~3) == MOV_AX_DISP32)
29b0f896 7974 {
5e042380 7975 default_seg = reg_ds;
29b0f896 7976 }
40fb9820 7977 else if (i.tm.opcode_modifier.isstring)
29b0f896
AM
7978 {
7979 /* For the string instructions that allow a segment override
7980 on one of their operands, the default segment is ds. */
5e042380 7981 default_seg = reg_ds;
29b0f896 7982 }
50128d0c 7983 else if (i.short_form)
85b80b0f
JB
7984 {
7985 /* The register or float register operand is in operand
7986 0 or 1. */
bab6aec1 7987 unsigned int op = i.tm.operand_types[0].bitfield.class != Reg;
85b80b0f
JB
7988
7989 /* Register goes in low 3 bits of opcode. */
7990 i.tm.base_opcode |= i.op[op].regs->reg_num;
7991 if ((i.op[op].regs->reg_flags & RegRex) != 0)
7992 i.rex |= REX_B;
7993 if (!quiet_warnings && i.tm.opcode_modifier.ugh)
7994 {
7995 /* Warn about some common errors, but press on regardless.
7996 The first case can be generated by gcc (<= 2.8.1). */
7997 if (i.operands == 2)
7998 {
7999 /* Reversed arguments on faddp, fsubp, etc. */
8000 as_warn (_("translating to `%s %s%s,%s%s'"), i.tm.name,
8001 register_prefix, i.op[!intel_syntax].regs->reg_name,
8002 register_prefix, i.op[intel_syntax].regs->reg_name);
8003 }
8004 else
8005 {
8006 /* Extraneous `l' suffix on fp insn. */
8007 as_warn (_("translating to `%s %s%s'"), i.tm.name,
8008 register_prefix, i.op[0].regs->reg_name);
8009 }
8010 }
8011 }
29b0f896 8012
514a8bb0 8013 if ((i.seg[0] || i.prefix[SEG_PREFIX])
514a8bb0 8014 && i.tm.base_opcode == 0x8d /* lea */
35648716 8015 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
514a8bb0 8016 && !is_any_vex_encoding(&i.tm))
92334ad2
JB
8017 {
8018 if (!quiet_warnings)
8019 as_warn (_("segment override on `%s' is ineffectual"), i.tm.name);
8020 if (optimize)
8021 {
8022 i.seg[0] = NULL;
8023 i.prefix[SEG_PREFIX] = 0;
8024 }
8025 }
52271982
AM
8026
8027 /* If a segment was explicitly specified, and the specified segment
b6773884
JB
8028 is neither the default nor the one already recorded from a prefix,
8029 use an opcode prefix to select it. If we never figured out what
8030 the default segment is, then default_seg will be zero at this
8031 point, and the specified segment prefix will always be used. */
8032 if (i.seg[0]
8033 && i.seg[0] != default_seg
5e042380 8034 && i386_seg_prefixes[i.seg[0]->reg_num] != i.prefix[SEG_PREFIX])
29b0f896 8035 {
5e042380 8036 if (!add_prefix (i386_seg_prefixes[i.seg[0]->reg_num]))
29b0f896
AM
8037 return 0;
8038 }
8039 return 1;
8040}
8041
a5aeccd9 8042static INLINE void set_rex_vrex (const reg_entry *r, unsigned int rex_bit,
5b7c81bd 8043 bool do_sse2avx)
a5aeccd9
JB
8044{
8045 if (r->reg_flags & RegRex)
8046 {
8047 if (i.rex & rex_bit)
8048 as_bad (_("same type of prefix used twice"));
8049 i.rex |= rex_bit;
8050 }
8051 else if (do_sse2avx && (i.rex & rex_bit) && i.vex.register_specifier)
8052 {
8053 gas_assert (i.vex.register_specifier == r);
8054 i.vex.register_specifier += 8;
8055 }
8056
8057 if (r->reg_flags & RegVRex)
8058 i.vrex |= rex_bit;
8059}
8060
5e042380 8061static const reg_entry *
e3bb37b5 8062build_modrm_byte (void)
29b0f896 8063{
5e042380 8064 const reg_entry *default_seg = NULL;
c0f3af97 8065 unsigned int source, dest;
8cd7925b 8066 int vex_3_sources;
c0f3af97 8067
8cd7925b 8068 vex_3_sources = i.tm.opcode_modifier.vexsources == VEX3SOURCES;
c0f3af97
L
8069 if (vex_3_sources)
8070 {
91d6fa6a 8071 unsigned int nds, reg_slot;
4c2c6516 8072 expressionS *exp;
c0f3af97 8073
6b8d3588 8074 dest = i.operands - 1;
c0f3af97 8075 nds = dest - 1;
922d8de8 8076
a683cc34 8077 /* There are 2 kinds of instructions:
bed3d976 8078 1. 5 operands: 4 register operands or 3 register operands
9d3bf266 8079 plus 1 memory operand plus one Imm4 operand, VexXDS, and
bed3d976 8080 VexW0 or VexW1. The destination must be either XMM, YMM or
43234a1e 8081 ZMM register.
bed3d976 8082 2. 4 operands: 4 register operands or 3 register operands
2f1bada2 8083 plus 1 memory operand, with VexXDS. */
922d8de8 8084 gas_assert ((i.reg_operands == 4
bed3d976
JB
8085 || (i.reg_operands == 3 && i.mem_operands == 1))
8086 && i.tm.opcode_modifier.vexvvvv == VEXXDS
dcd7e323 8087 && i.tm.opcode_modifier.vexw
3528c362 8088 && i.tm.operand_types[dest].bitfield.class == RegSIMD);
a683cc34 8089
48db9223
JB
8090 /* If VexW1 is set, the first non-immediate operand is the source and
8091 the second non-immediate one is encoded in the immediate operand. */
8092 if (i.tm.opcode_modifier.vexw == VEXW1)
8093 {
8094 source = i.imm_operands;
8095 reg_slot = i.imm_operands + 1;
8096 }
8097 else
8098 {
8099 source = i.imm_operands + 1;
8100 reg_slot = i.imm_operands;
8101 }
8102
a683cc34 8103 if (i.imm_operands == 0)
bed3d976
JB
8104 {
8105 /* When there is no immediate operand, generate an 8bit
8106 immediate operand to encode the first operand. */
8107 exp = &im_expressions[i.imm_operands++];
8108 i.op[i.operands].imms = exp;
8109 i.types[i.operands] = imm8;
8110 i.operands++;
8111
3528c362 8112 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
bed3d976
JB
8113 exp->X_op = O_constant;
8114 exp->X_add_number = register_number (i.op[reg_slot].regs) << 4;
43234a1e
L
8115 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
8116 }
922d8de8 8117 else
bed3d976 8118 {
9d3bf266
JB
8119 gas_assert (i.imm_operands == 1);
8120 gas_assert (fits_in_imm4 (i.op[0].imms->X_add_number));
8121 gas_assert (!i.tm.opcode_modifier.immext);
a683cc34 8122
9d3bf266
JB
8123 /* Turn on Imm8 again so that output_imm will generate it. */
8124 i.types[0].bitfield.imm8 = 1;
bed3d976 8125
3528c362 8126 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
9d3bf266 8127 i.op[0].imms->X_add_number
bed3d976 8128 |= register_number (i.op[reg_slot].regs) << 4;
43234a1e 8129 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
bed3d976 8130 }
a683cc34 8131
3528c362 8132 gas_assert (i.tm.operand_types[nds].bitfield.class == RegSIMD);
dae39acc 8133 i.vex.register_specifier = i.op[nds].regs;
c0f3af97
L
8134 }
8135 else
8136 source = dest = 0;
29b0f896
AM
8137
8138 /* i.reg_operands MUST be the number of real register operands;
c0f3af97
L
8139 implicit registers do not count. If there are 3 register
8140 operands, it must be a instruction with VexNDS. For a
8141 instruction with VexNDD, the destination register is encoded
8142 in VEX prefix. If there are 4 register operands, it must be
8143 a instruction with VEX prefix and 3 sources. */
7ab9ffdd
L
8144 if (i.mem_operands == 0
8145 && ((i.reg_operands == 2
2426c15f 8146 && i.tm.opcode_modifier.vexvvvv <= VEXXDS)
7ab9ffdd 8147 || (i.reg_operands == 3
2426c15f 8148 && i.tm.opcode_modifier.vexvvvv == VEXXDS)
7ab9ffdd 8149 || (i.reg_operands == 4 && vex_3_sources)))
29b0f896 8150 {
cab737b9
L
8151 switch (i.operands)
8152 {
8153 case 2:
8154 source = 0;
8155 break;
8156 case 3:
c81128dc
L
8157 /* When there are 3 operands, one of them may be immediate,
8158 which may be the first or the last operand. Otherwise,
c0f3af97
L
8159 the first operand must be shift count register (cl) or it
8160 is an instruction with VexNDS. */
9c2799c2 8161 gas_assert (i.imm_operands == 1
7ab9ffdd 8162 || (i.imm_operands == 0
2426c15f 8163 && (i.tm.opcode_modifier.vexvvvv == VEXXDS
75e5731b
JB
8164 || (i.types[0].bitfield.instance == RegC
8165 && i.types[0].bitfield.byte))));
40fb9820 8166 if (operand_type_check (i.types[0], imm)
75e5731b
JB
8167 || (i.types[0].bitfield.instance == RegC
8168 && i.types[0].bitfield.byte))
40fb9820
L
8169 source = 1;
8170 else
8171 source = 0;
cab737b9
L
8172 break;
8173 case 4:
368d64cc
L
8174 /* When there are 4 operands, the first two must be 8bit
8175 immediate operands. The source operand will be the 3rd
c0f3af97
L
8176 one.
8177
8178 For instructions with VexNDS, if the first operand
8179 an imm8, the source operand is the 2nd one. If the last
8180 operand is imm8, the source operand is the first one. */
9c2799c2 8181 gas_assert ((i.imm_operands == 2
7ab9ffdd
L
8182 && i.types[0].bitfield.imm8
8183 && i.types[1].bitfield.imm8)
2426c15f 8184 || (i.tm.opcode_modifier.vexvvvv == VEXXDS
7ab9ffdd
L
8185 && i.imm_operands == 1
8186 && (i.types[0].bitfield.imm8
cf665fee 8187 || i.types[i.operands - 1].bitfield.imm8)));
9f2670f2
L
8188 if (i.imm_operands == 2)
8189 source = 2;
8190 else
c0f3af97
L
8191 {
8192 if (i.types[0].bitfield.imm8)
8193 source = 1;
8194 else
8195 source = 0;
8196 }
c0f3af97
L
8197 break;
8198 case 5:
cf665fee
JB
8199 gas_assert (!is_evex_encoding (&i.tm));
8200 gas_assert (i.imm_operands == 1 && vex_3_sources);
cab737b9
L
8201 break;
8202 default:
8203 abort ();
8204 }
8205
c0f3af97
L
8206 if (!vex_3_sources)
8207 {
8208 dest = source + 1;
8209
2426c15f 8210 if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
c0f3af97 8211 {
43234a1e 8212 /* For instructions with VexNDS, the register-only source
c5d0745b 8213 operand must be a 32/64bit integer, XMM, YMM, ZMM, or mask
dfd69174 8214 register. It is encoded in VEX prefix. */
f12dc422
L
8215
8216 i386_operand_type op;
8217 unsigned int vvvv;
8218
c2ecccb3
L
8219 /* Swap two source operands if needed. */
8220 if (i.tm.opcode_modifier.swapsources)
f12dc422
L
8221 {
8222 vvvv = source;
8223 source = dest;
8224 }
8225 else
8226 vvvv = dest;
8227
8228 op = i.tm.operand_types[vvvv];
c0f3af97 8229 if ((dest + 1) >= i.operands
bab6aec1 8230 || ((op.bitfield.class != Reg
dc821c5f 8231 || (!op.bitfield.dword && !op.bitfield.qword))
3528c362 8232 && op.bitfield.class != RegSIMD
43234a1e 8233 && !operand_type_equal (&op, &regmask)))
c0f3af97 8234 abort ();
f12dc422 8235 i.vex.register_specifier = i.op[vvvv].regs;
c0f3af97
L
8236 dest++;
8237 }
8238 }
29b0f896
AM
8239
8240 i.rm.mode = 3;
dfd69174
JB
8241 /* One of the register operands will be encoded in the i.rm.reg
8242 field, the other in the combined i.rm.mode and i.rm.regmem
29b0f896
AM
8243 fields. If no form of this instruction supports a memory
8244 destination operand, then we assume the source operand may
8245 sometimes be a memory operand and so we need to store the
8246 destination in the i.rm.reg field. */
dfd69174 8247 if (!i.tm.opcode_modifier.regmem
40fb9820 8248 && operand_type_check (i.tm.operand_types[dest], anymem) == 0)
29b0f896
AM
8249 {
8250 i.rm.reg = i.op[dest].regs->reg_num;
8251 i.rm.regmem = i.op[source].regs->reg_num;
a5aeccd9 8252 set_rex_vrex (i.op[dest].regs, REX_R, i.tm.opcode_modifier.sse2avx);
5b7c81bd 8253 set_rex_vrex (i.op[source].regs, REX_B, false);
29b0f896
AM
8254 }
8255 else
8256 {
8257 i.rm.reg = i.op[source].regs->reg_num;
8258 i.rm.regmem = i.op[dest].regs->reg_num;
a5aeccd9 8259 set_rex_vrex (i.op[dest].regs, REX_B, i.tm.opcode_modifier.sse2avx);
5b7c81bd 8260 set_rex_vrex (i.op[source].regs, REX_R, false);
29b0f896 8261 }
e0c7f900 8262 if (flag_code != CODE_64BIT && (i.rex & REX_R))
c4a530c5 8263 {
4a5c67ed 8264 if (i.types[!i.tm.opcode_modifier.regmem].bitfield.class != RegCR)
c4a530c5 8265 abort ();
e0c7f900 8266 i.rex &= ~REX_R;
c4a530c5
JB
8267 add_prefix (LOCK_PREFIX_OPCODE);
8268 }
29b0f896
AM
8269 }
8270 else
8271 { /* If it's not 2 reg operands... */
c0f3af97
L
8272 unsigned int mem;
8273
29b0f896
AM
8274 if (i.mem_operands)
8275 {
8276 unsigned int fake_zero_displacement = 0;
99018f42 8277 unsigned int op;
4eed87de 8278
7ab9ffdd 8279 for (op = 0; op < i.operands; op++)
8dc0818e 8280 if (i.flags[op] & Operand_Mem)
7ab9ffdd 8281 break;
7ab9ffdd 8282 gas_assert (op < i.operands);
29b0f896 8283
63112cd6 8284 if (i.tm.opcode_modifier.sib)
6c30d220 8285 {
260cd341
LC
8286 /* The index register of VSIB shouldn't be RegIZ. */
8287 if (i.tm.opcode_modifier.sib != SIBMEM
8288 && i.index_reg->reg_num == RegIZ)
6c30d220
L
8289 abort ();
8290
8291 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8292 if (!i.base_reg)
8293 {
8294 i.sib.base = NO_BASE_REGISTER;
8295 i.sib.scale = i.log2_scale_factor;
2f2be86b 8296 i.types[op] = operand_type_and_not (i.types[op], anydisp);
a775efc8 8297 i.types[op].bitfield.disp32 = 1;
6c30d220 8298 }
260cd341
LC
8299
8300 /* Since the mandatory SIB always has index register, so
8301 the code logic remains unchanged. The non-mandatory SIB
8302 without index register is allowed and will be handled
8303 later. */
8304 if (i.index_reg)
8305 {
8306 if (i.index_reg->reg_num == RegIZ)
8307 i.sib.index = NO_INDEX_REGISTER;
8308 else
8309 i.sib.index = i.index_reg->reg_num;
5b7c81bd 8310 set_rex_vrex (i.index_reg, REX_X, false);
260cd341 8311 }
6c30d220
L
8312 }
8313
5e042380 8314 default_seg = reg_ds;
29b0f896
AM
8315
8316 if (i.base_reg == 0)
8317 {
8318 i.rm.mode = 0;
8319 if (!i.disp_operands)
9bb129e8 8320 fake_zero_displacement = 1;
29b0f896
AM
8321 if (i.index_reg == 0)
8322 {
260cd341
LC
8323 /* Both check for VSIB and mandatory non-vector SIB. */
8324 gas_assert (!i.tm.opcode_modifier.sib
8325 || i.tm.opcode_modifier.sib == SIBMEM);
29b0f896 8326 /* Operand is just <disp> */
2f2be86b 8327 i.types[op] = operand_type_and_not (i.types[op], anydisp);
20f0a1fc 8328 if (flag_code == CODE_64BIT)
29b0f896
AM
8329 {
8330 /* 64bit mode overwrites the 32bit absolute
8331 addressing by RIP relative addressing and
8332 absolute addressing is encoded by one of the
8333 redundant SIB forms. */
8334 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8335 i.sib.base = NO_BASE_REGISTER;
8336 i.sib.index = NO_INDEX_REGISTER;
a775efc8 8337 i.types[op].bitfield.disp32 = 1;
20f0a1fc 8338 }
fc225355
L
8339 else if ((flag_code == CODE_16BIT)
8340 ^ (i.prefix[ADDR_PREFIX] != 0))
20f0a1fc
NC
8341 {
8342 i.rm.regmem = NO_BASE_REGISTER_16;
2f2be86b 8343 i.types[op].bitfield.disp16 = 1;
20f0a1fc
NC
8344 }
8345 else
8346 {
8347 i.rm.regmem = NO_BASE_REGISTER;
2f2be86b 8348 i.types[op].bitfield.disp32 = 1;
29b0f896
AM
8349 }
8350 }
63112cd6 8351 else if (!i.tm.opcode_modifier.sib)
29b0f896 8352 {
6c30d220 8353 /* !i.base_reg && i.index_reg */
e968fc9b 8354 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8355 i.sib.index = NO_INDEX_REGISTER;
8356 else
8357 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8358 i.sib.base = NO_BASE_REGISTER;
8359 i.sib.scale = i.log2_scale_factor;
8360 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
2f2be86b 8361 i.types[op] = operand_type_and_not (i.types[op], anydisp);
a775efc8 8362 i.types[op].bitfield.disp32 = 1;
29b0f896 8363 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8364 i.rex |= REX_X;
29b0f896
AM
8365 }
8366 }
8367 /* RIP addressing for 64bit mode. */
e968fc9b 8368 else if (i.base_reg->reg_num == RegIP)
29b0f896 8369 {
63112cd6 8370 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896 8371 i.rm.regmem = NO_BASE_REGISTER;
40fb9820
L
8372 i.types[op].bitfield.disp8 = 0;
8373 i.types[op].bitfield.disp16 = 0;
a775efc8 8374 i.types[op].bitfield.disp32 = 1;
40fb9820 8375 i.types[op].bitfield.disp64 = 0;
71903a11 8376 i.flags[op] |= Operand_PCrel;
20f0a1fc
NC
8377 if (! i.disp_operands)
8378 fake_zero_displacement = 1;
29b0f896 8379 }
dc821c5f 8380 else if (i.base_reg->reg_type.bitfield.word)
29b0f896 8381 {
63112cd6 8382 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896
AM
8383 switch (i.base_reg->reg_num)
8384 {
8385 case 3: /* (%bx) */
8386 if (i.index_reg == 0)
8387 i.rm.regmem = 7;
8388 else /* (%bx,%si) -> 0, or (%bx,%di) -> 1 */
8389 i.rm.regmem = i.index_reg->reg_num - 6;
8390 break;
8391 case 5: /* (%bp) */
5e042380 8392 default_seg = reg_ss;
29b0f896
AM
8393 if (i.index_reg == 0)
8394 {
8395 i.rm.regmem = 6;
40fb9820 8396 if (operand_type_check (i.types[op], disp) == 0)
29b0f896
AM
8397 {
8398 /* fake (%bp) into 0(%bp) */
41eb8e88 8399 if (i.disp_encoding == disp_encoding_16bit)
1a02d6b0
L
8400 i.types[op].bitfield.disp16 = 1;
8401 else
8402 i.types[op].bitfield.disp8 = 1;
252b5132 8403 fake_zero_displacement = 1;
29b0f896
AM
8404 }
8405 }
8406 else /* (%bp,%si) -> 2, or (%bp,%di) -> 3 */
8407 i.rm.regmem = i.index_reg->reg_num - 6 + 2;
8408 break;
8409 default: /* (%si) -> 4 or (%di) -> 5 */
8410 i.rm.regmem = i.base_reg->reg_num - 6 + 4;
8411 }
41eb8e88
L
8412 if (!fake_zero_displacement
8413 && !i.disp_operands
8414 && i.disp_encoding)
8415 {
8416 fake_zero_displacement = 1;
8417 if (i.disp_encoding == disp_encoding_8bit)
8418 i.types[op].bitfield.disp8 = 1;
8419 else
8420 i.types[op].bitfield.disp16 = 1;
8421 }
29b0f896
AM
8422 i.rm.mode = mode_from_disp_size (i.types[op]);
8423 }
8424 else /* i.base_reg and 32/64 bit mode */
8425 {
a9aabc23 8426 if (operand_type_check (i.types[op], disp))
40fb9820 8427 {
73053c1f
JB
8428 i.types[op].bitfield.disp16 = 0;
8429 i.types[op].bitfield.disp64 = 0;
a775efc8 8430 i.types[op].bitfield.disp32 = 1;
40fb9820 8431 }
20f0a1fc 8432
63112cd6 8433 if (!i.tm.opcode_modifier.sib)
6c30d220 8434 i.rm.regmem = i.base_reg->reg_num;
29b0f896 8435 if ((i.base_reg->reg_flags & RegRex) != 0)
161a04f6 8436 i.rex |= REX_B;
29b0f896
AM
8437 i.sib.base = i.base_reg->reg_num;
8438 /* x86-64 ignores REX prefix bit here to avoid decoder
8439 complications. */
848930b2
JB
8440 if (!(i.base_reg->reg_flags & RegRex)
8441 && (i.base_reg->reg_num == EBP_REG_NUM
8442 || i.base_reg->reg_num == ESP_REG_NUM))
5e042380 8443 default_seg = reg_ss;
848930b2 8444 if (i.base_reg->reg_num == 5 && i.disp_operands == 0)
29b0f896 8445 {
848930b2 8446 fake_zero_displacement = 1;
1a02d6b0
L
8447 if (i.disp_encoding == disp_encoding_32bit)
8448 i.types[op].bitfield.disp32 = 1;
8449 else
8450 i.types[op].bitfield.disp8 = 1;
29b0f896
AM
8451 }
8452 i.sib.scale = i.log2_scale_factor;
8453 if (i.index_reg == 0)
8454 {
260cd341
LC
8455 /* Only check for VSIB. */
8456 gas_assert (i.tm.opcode_modifier.sib != VECSIB128
8457 && i.tm.opcode_modifier.sib != VECSIB256
8458 && i.tm.opcode_modifier.sib != VECSIB512);
8459
29b0f896
AM
8460 /* <disp>(%esp) becomes two byte modrm with no index
8461 register. We've already stored the code for esp
8462 in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
8463 Any base register besides %esp will not use the
8464 extra modrm byte. */
8465 i.sib.index = NO_INDEX_REGISTER;
29b0f896 8466 }
63112cd6 8467 else if (!i.tm.opcode_modifier.sib)
29b0f896 8468 {
e968fc9b 8469 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8470 i.sib.index = NO_INDEX_REGISTER;
8471 else
8472 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8473 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8474 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8475 i.rex |= REX_X;
29b0f896 8476 }
67a4f2b7
AO
8477
8478 if (i.disp_operands
8479 && (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
8480 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL))
8481 i.rm.mode = 0;
8482 else
a501d77e
L
8483 {
8484 if (!fake_zero_displacement
8485 && !i.disp_operands
8486 && i.disp_encoding)
8487 {
8488 fake_zero_displacement = 1;
8489 if (i.disp_encoding == disp_encoding_8bit)
8490 i.types[op].bitfield.disp8 = 1;
8491 else
8492 i.types[op].bitfield.disp32 = 1;
8493 }
8494 i.rm.mode = mode_from_disp_size (i.types[op]);
8495 }
29b0f896 8496 }
252b5132 8497
29b0f896
AM
8498 if (fake_zero_displacement)
8499 {
8500 /* Fakes a zero displacement assuming that i.types[op]
8501 holds the correct displacement size. */
8502 expressionS *exp;
8503
9c2799c2 8504 gas_assert (i.op[op].disps == 0);
29b0f896
AM
8505 exp = &disp_expressions[i.disp_operands++];
8506 i.op[op].disps = exp;
8507 exp->X_op = O_constant;
8508 exp->X_add_number = 0;
8509 exp->X_add_symbol = (symbolS *) 0;
8510 exp->X_op_symbol = (symbolS *) 0;
8511 }
c0f3af97
L
8512
8513 mem = op;
29b0f896 8514 }
c0f3af97
L
8515 else
8516 mem = ~0;
252b5132 8517
8c43a48b 8518 if (i.tm.opcode_modifier.vexsources == XOP2SOURCES)
5dd85c99
SP
8519 {
8520 if (operand_type_check (i.types[0], imm))
8521 i.vex.register_specifier = NULL;
8522 else
8523 {
8524 /* VEX.vvvv encodes one of the sources when the first
8525 operand is not an immediate. */
1ef99a7b 8526 if (i.tm.opcode_modifier.vexw == VEXW0)
5dd85c99
SP
8527 i.vex.register_specifier = i.op[0].regs;
8528 else
8529 i.vex.register_specifier = i.op[1].regs;
8530 }
8531
8532 /* Destination is a XMM register encoded in the ModRM.reg
8533 and VEX.R bit. */
8534 i.rm.reg = i.op[2].regs->reg_num;
8535 if ((i.op[2].regs->reg_flags & RegRex) != 0)
8536 i.rex |= REX_R;
8537
8538 /* ModRM.rm and VEX.B encodes the other source. */
8539 if (!i.mem_operands)
8540 {
8541 i.rm.mode = 3;
8542
1ef99a7b 8543 if (i.tm.opcode_modifier.vexw == VEXW0)
5dd85c99
SP
8544 i.rm.regmem = i.op[1].regs->reg_num;
8545 else
8546 i.rm.regmem = i.op[0].regs->reg_num;
8547
8548 if ((i.op[1].regs->reg_flags & RegRex) != 0)
8549 i.rex |= REX_B;
8550 }
8551 }
2426c15f 8552 else if (i.tm.opcode_modifier.vexvvvv == VEXLWP)
f88c9eb0
SP
8553 {
8554 i.vex.register_specifier = i.op[2].regs;
8555 if (!i.mem_operands)
8556 {
8557 i.rm.mode = 3;
8558 i.rm.regmem = i.op[1].regs->reg_num;
8559 if ((i.op[1].regs->reg_flags & RegRex) != 0)
8560 i.rex |= REX_B;
8561 }
8562 }
29b0f896
AM
8563 /* Fill in i.rm.reg or i.rm.regmem field with register operand
8564 (if any) based on i.tm.extension_opcode. Again, we must be
8565 careful to make sure that segment/control/debug/test/MMX
8566 registers are coded into the i.rm.reg field. */
f88c9eb0 8567 else if (i.reg_operands)
29b0f896 8568 {
99018f42 8569 unsigned int op;
7ab9ffdd
L
8570 unsigned int vex_reg = ~0;
8571
8572 for (op = 0; op < i.operands; op++)
921eafea
L
8573 if (i.types[op].bitfield.class == Reg
8574 || i.types[op].bitfield.class == RegBND
8575 || i.types[op].bitfield.class == RegMask
8576 || i.types[op].bitfield.class == SReg
8577 || i.types[op].bitfield.class == RegCR
8578 || i.types[op].bitfield.class == RegDR
8579 || i.types[op].bitfield.class == RegTR
8580 || i.types[op].bitfield.class == RegSIMD
8581 || i.types[op].bitfield.class == RegMMX)
8582 break;
c0209578 8583
7ab9ffdd
L
8584 if (vex_3_sources)
8585 op = dest;
2426c15f 8586 else if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
7ab9ffdd
L
8587 {
8588 /* For instructions with VexNDS, the register-only
8589 source operand is encoded in VEX prefix. */
8590 gas_assert (mem != (unsigned int) ~0);
c0f3af97 8591
7ab9ffdd 8592 if (op > mem)
c0f3af97 8593 {
7ab9ffdd
L
8594 vex_reg = op++;
8595 gas_assert (op < i.operands);
c0f3af97
L
8596 }
8597 else
c0f3af97 8598 {
f12dc422
L
8599 /* Check register-only source operand when two source
8600 operands are swapped. */
8601 if (!i.tm.operand_types[op].bitfield.baseindex
8602 && i.tm.operand_types[op + 1].bitfield.baseindex)
8603 {
8604 vex_reg = op;
8605 op += 2;
8606 gas_assert (mem == (vex_reg + 1)
8607 && op < i.operands);
8608 }
8609 else
8610 {
8611 vex_reg = op + 1;
8612 gas_assert (vex_reg < i.operands);
8613 }
c0f3af97 8614 }
7ab9ffdd 8615 }
2426c15f 8616 else if (i.tm.opcode_modifier.vexvvvv == VEXNDD)
7ab9ffdd 8617 {
f12dc422 8618 /* For instructions with VexNDD, the register destination
7ab9ffdd 8619 is encoded in VEX prefix. */
f12dc422
L
8620 if (i.mem_operands == 0)
8621 {
8622 /* There is no memory operand. */
8623 gas_assert ((op + 2) == i.operands);
8624 vex_reg = op + 1;
8625 }
8626 else
8d63c93e 8627 {
ed438a93
JB
8628 /* There are only 2 non-immediate operands. */
8629 gas_assert (op < i.imm_operands + 2
8630 && i.operands == i.imm_operands + 2);
8631 vex_reg = i.imm_operands + 1;
f12dc422 8632 }
7ab9ffdd
L
8633 }
8634 else
8635 gas_assert (op < i.operands);
99018f42 8636
7ab9ffdd
L
8637 if (vex_reg != (unsigned int) ~0)
8638 {
f12dc422 8639 i386_operand_type *type = &i.tm.operand_types[vex_reg];
7ab9ffdd 8640
bab6aec1 8641 if ((type->bitfield.class != Reg
dc821c5f 8642 || (!type->bitfield.dword && !type->bitfield.qword))
3528c362 8643 && type->bitfield.class != RegSIMD
43234a1e 8644 && !operand_type_equal (type, &regmask))
7ab9ffdd 8645 abort ();
f88c9eb0 8646
7ab9ffdd
L
8647 i.vex.register_specifier = i.op[vex_reg].regs;
8648 }
8649
1b9f0c97
L
8650 /* Don't set OP operand twice. */
8651 if (vex_reg != op)
7ab9ffdd 8652 {
1b9f0c97
L
8653 /* If there is an extension opcode to put here, the
8654 register number must be put into the regmem field. */
8655 if (i.tm.extension_opcode != None)
8656 {
8657 i.rm.regmem = i.op[op].regs->reg_num;
a5aeccd9
JB
8658 set_rex_vrex (i.op[op].regs, REX_B,
8659 i.tm.opcode_modifier.sse2avx);
1b9f0c97
L
8660 }
8661 else
8662 {
8663 i.rm.reg = i.op[op].regs->reg_num;
a5aeccd9
JB
8664 set_rex_vrex (i.op[op].regs, REX_R,
8665 i.tm.opcode_modifier.sse2avx);
1b9f0c97 8666 }
7ab9ffdd 8667 }
252b5132 8668
29b0f896
AM
8669 /* Now, if no memory operand has set i.rm.mode = 0, 1, 2 we
8670 must set it to 3 to indicate this is a register operand
8671 in the regmem field. */
8672 if (!i.mem_operands)
8673 i.rm.mode = 3;
8674 }
252b5132 8675
29b0f896 8676 /* Fill in i.rm.reg field with extension opcode (if any). */
c1e679ec 8677 if (i.tm.extension_opcode != None)
29b0f896
AM
8678 i.rm.reg = i.tm.extension_opcode;
8679 }
8680 return default_seg;
8681}
252b5132 8682
48ef937e
JB
8683static INLINE void
8684frag_opcode_byte (unsigned char byte)
8685{
8686 if (now_seg != absolute_section)
8687 FRAG_APPEND_1_CHAR (byte);
8688 else
8689 ++abs_section_offset;
8690}
8691
376cd056
JB
8692static unsigned int
8693flip_code16 (unsigned int code16)
8694{
8695 gas_assert (i.tm.operands == 1);
8696
8697 return !(i.prefix[REX_PREFIX] & REX_W)
8698 && (code16 ? i.tm.operand_types[0].bitfield.disp32
376cd056
JB
8699 : i.tm.operand_types[0].bitfield.disp16)
8700 ? CODE16 : 0;
8701}
8702
29b0f896 8703static void
e3bb37b5 8704output_branch (void)
29b0f896
AM
8705{
8706 char *p;
f8a5c266 8707 int size;
29b0f896
AM
8708 int code16;
8709 int prefix;
8710 relax_substateT subtype;
8711 symbolS *sym;
8712 offsetT off;
8713
48ef937e
JB
8714 if (now_seg == absolute_section)
8715 {
8716 as_bad (_("relaxable branches not supported in absolute section"));
8717 return;
8718 }
8719
f8a5c266 8720 code16 = flag_code == CODE_16BIT ? CODE16 : 0;
1a42a9fe 8721 size = i.disp_encoding > disp_encoding_8bit ? BIG : SMALL;
29b0f896
AM
8722
8723 prefix = 0;
8724 if (i.prefix[DATA_PREFIX] != 0)
252b5132 8725 {
29b0f896
AM
8726 prefix = 1;
8727 i.prefixes -= 1;
376cd056 8728 code16 ^= flip_code16(code16);
252b5132 8729 }
29b0f896
AM
8730 /* Pentium4 branch hints. */
8731 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8732 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
2f66722d 8733 {
29b0f896
AM
8734 prefix++;
8735 i.prefixes--;
8736 }
8737 if (i.prefix[REX_PREFIX] != 0)
8738 {
8739 prefix++;
8740 i.prefixes--;
2f66722d
AM
8741 }
8742
7e8b059b
L
8743 /* BND prefixed jump. */
8744 if (i.prefix[BND_PREFIX] != 0)
8745 {
6cb0a70e
JB
8746 prefix++;
8747 i.prefixes--;
7e8b059b
L
8748 }
8749
f2810fe0
JB
8750 if (i.prefixes != 0)
8751 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
29b0f896
AM
8752
8753 /* It's always a symbol; End frag & setup for relax.
8754 Make sure there is enough room in this frag for the largest
8755 instruction we may generate in md_convert_frag. This is 2
8756 bytes for the opcode and room for the prefix and largest
8757 displacement. */
8758 frag_grow (prefix + 2 + 4);
8759 /* Prefix and 1 opcode byte go in fr_fix. */
8760 p = frag_more (prefix + 1);
8761 if (i.prefix[DATA_PREFIX] != 0)
8762 *p++ = DATA_PREFIX_OPCODE;
8763 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
8764 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
8765 *p++ = i.prefix[SEG_PREFIX];
6cb0a70e
JB
8766 if (i.prefix[BND_PREFIX] != 0)
8767 *p++ = BND_PREFIX_OPCODE;
29b0f896
AM
8768 if (i.prefix[REX_PREFIX] != 0)
8769 *p++ = i.prefix[REX_PREFIX];
8770 *p = i.tm.base_opcode;
8771
8772 if ((unsigned char) *p == JUMP_PC_RELATIVE)
f8a5c266 8773 subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, size);
40fb9820 8774 else if (cpu_arch_flags.bitfield.cpui386)
f8a5c266 8775 subtype = ENCODE_RELAX_STATE (COND_JUMP, size);
29b0f896 8776 else
f8a5c266 8777 subtype = ENCODE_RELAX_STATE (COND_JUMP86, size);
29b0f896 8778 subtype |= code16;
3e73aa7c 8779
29b0f896
AM
8780 sym = i.op[0].disps->X_add_symbol;
8781 off = i.op[0].disps->X_add_number;
3e73aa7c 8782
29b0f896
AM
8783 if (i.op[0].disps->X_op != O_constant
8784 && i.op[0].disps->X_op != O_symbol)
3e73aa7c 8785 {
29b0f896
AM
8786 /* Handle complex expressions. */
8787 sym = make_expr_symbol (i.op[0].disps);
8788 off = 0;
8789 }
3e73aa7c 8790
1ef3994a
JB
8791 frag_now->tc_frag_data.code64 = flag_code == CODE_64BIT;
8792
29b0f896
AM
8793 /* 1 possible extra opcode + 4 byte displacement go in var part.
8794 Pass reloc in fr_var. */
d258b828 8795 frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
29b0f896 8796}
3e73aa7c 8797
bd7ab16b
L
8798#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8799/* Return TRUE iff PLT32 relocation should be used for branching to
8800 symbol S. */
8801
5b7c81bd 8802static bool
bd7ab16b
L
8803need_plt32_p (symbolS *s)
8804{
8805 /* PLT32 relocation is ELF only. */
8806 if (!IS_ELF)
5b7c81bd 8807 return false;
bd7ab16b 8808
a5def729
RO
8809#ifdef TE_SOLARIS
8810 /* Don't emit PLT32 relocation on Solaris: neither native linker nor
8811 krtld support it. */
5b7c81bd 8812 return false;
a5def729
RO
8813#endif
8814
bd7ab16b
L
8815 /* Since there is no need to prepare for PLT branch on x86-64, we
8816 can generate R_X86_64_PLT32, instead of R_X86_64_PC32, which can
8817 be used as a marker for 32-bit PC-relative branches. */
8818 if (!object_64bit)
5b7c81bd 8819 return false;
bd7ab16b 8820
44365e88 8821 if (s == NULL)
5b7c81bd 8822 return false;
44365e88 8823
bd7ab16b
L
8824 /* Weak or undefined symbol need PLT32 relocation. */
8825 if (S_IS_WEAK (s) || !S_IS_DEFINED (s))
5b7c81bd 8826 return true;
bd7ab16b
L
8827
8828 /* Non-global symbol doesn't need PLT32 relocation. */
8829 if (! S_IS_EXTERNAL (s))
5b7c81bd 8830 return false;
bd7ab16b
L
8831
8832 /* Other global symbols need PLT32 relocation. NB: Symbol with
8833 non-default visibilities are treated as normal global symbol
8834 so that PLT32 relocation can be used as a marker for 32-bit
8835 PC-relative branches. It is useful for linker relaxation. */
5b7c81bd 8836 return true;
bd7ab16b
L
8837}
8838#endif
8839
29b0f896 8840static void
e3bb37b5 8841output_jump (void)
29b0f896
AM
8842{
8843 char *p;
8844 int size;
3e02c1cc 8845 fixS *fixP;
bd7ab16b 8846 bfd_reloc_code_real_type jump_reloc = i.reloc[0];
29b0f896 8847
0cfa3eb3 8848 if (i.tm.opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
8849 {
8850 /* This is a loop or jecxz type instruction. */
8851 size = 1;
8852 if (i.prefix[ADDR_PREFIX] != 0)
8853 {
48ef937e 8854 frag_opcode_byte (ADDR_PREFIX_OPCODE);
29b0f896
AM
8855 i.prefixes -= 1;
8856 }
8857 /* Pentium4 branch hints. */
8858 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8859 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
8860 {
48ef937e 8861 frag_opcode_byte (i.prefix[SEG_PREFIX]);
29b0f896 8862 i.prefixes--;
3e73aa7c
JH
8863 }
8864 }
29b0f896
AM
8865 else
8866 {
8867 int code16;
3e73aa7c 8868
29b0f896
AM
8869 code16 = 0;
8870 if (flag_code == CODE_16BIT)
8871 code16 = CODE16;
3e73aa7c 8872
29b0f896
AM
8873 if (i.prefix[DATA_PREFIX] != 0)
8874 {
48ef937e 8875 frag_opcode_byte (DATA_PREFIX_OPCODE);
29b0f896 8876 i.prefixes -= 1;
376cd056 8877 code16 ^= flip_code16(code16);
29b0f896 8878 }
252b5132 8879
29b0f896
AM
8880 size = 4;
8881 if (code16)
8882 size = 2;
8883 }
9fcc94b6 8884
6cb0a70e
JB
8885 /* BND prefixed jump. */
8886 if (i.prefix[BND_PREFIX] != 0)
29b0f896 8887 {
48ef937e 8888 frag_opcode_byte (i.prefix[BND_PREFIX]);
29b0f896
AM
8889 i.prefixes -= 1;
8890 }
252b5132 8891
6cb0a70e 8892 if (i.prefix[REX_PREFIX] != 0)
7e8b059b 8893 {
48ef937e 8894 frag_opcode_byte (i.prefix[REX_PREFIX]);
7e8b059b
L
8895 i.prefixes -= 1;
8896 }
8897
f2810fe0
JB
8898 if (i.prefixes != 0)
8899 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
e0890092 8900
48ef937e
JB
8901 if (now_seg == absolute_section)
8902 {
9a182d04 8903 abs_section_offset += i.opcode_length + size;
48ef937e
JB
8904 return;
8905 }
8906
9a182d04
JB
8907 p = frag_more (i.opcode_length + size);
8908 switch (i.opcode_length)
42164a71
L
8909 {
8910 case 2:
8911 *p++ = i.tm.base_opcode >> 8;
1a0670f3 8912 /* Fall through. */
42164a71
L
8913 case 1:
8914 *p++ = i.tm.base_opcode;
8915 break;
8916 default:
8917 abort ();
8918 }
e0890092 8919
bd7ab16b 8920#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1ef3994a
JB
8921 if (flag_code == CODE_64BIT && size == 4
8922 && jump_reloc == NO_RELOC && i.op[0].disps->X_add_number == 0
bd7ab16b
L
8923 && need_plt32_p (i.op[0].disps->X_add_symbol))
8924 jump_reloc = BFD_RELOC_X86_64_PLT32;
8925#endif
8926
8927 jump_reloc = reloc (size, 1, 1, jump_reloc);
8928
3e02c1cc 8929 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
bd7ab16b 8930 i.op[0].disps, 1, jump_reloc);
3e02c1cc 8931
eb19308f
JB
8932 /* All jumps handled here are signed, but don't unconditionally use a
8933 signed limit check for 32 and 16 bit jumps as we want to allow wrap
8934 around at 4G (outside of 64-bit mode) and 64k (except for XBEGIN)
8935 respectively. */
8936 switch (size)
8937 {
8938 case 1:
8939 fixP->fx_signed = 1;
8940 break;
8941
8942 case 2:
8943 if (i.tm.base_opcode == 0xc7f8)
8944 fixP->fx_signed = 1;
8945 break;
8946
8947 case 4:
8948 if (flag_code == CODE_64BIT)
8949 fixP->fx_signed = 1;
8950 break;
8951 }
29b0f896 8952}
e0890092 8953
29b0f896 8954static void
e3bb37b5 8955output_interseg_jump (void)
29b0f896
AM
8956{
8957 char *p;
8958 int size;
8959 int prefix;
8960 int code16;
252b5132 8961
29b0f896
AM
8962 code16 = 0;
8963 if (flag_code == CODE_16BIT)
8964 code16 = CODE16;
a217f122 8965
29b0f896
AM
8966 prefix = 0;
8967 if (i.prefix[DATA_PREFIX] != 0)
8968 {
8969 prefix = 1;
8970 i.prefixes -= 1;
8971 code16 ^= CODE16;
8972 }
6cb0a70e
JB
8973
8974 gas_assert (!i.prefix[REX_PREFIX]);
252b5132 8975
29b0f896
AM
8976 size = 4;
8977 if (code16)
8978 size = 2;
252b5132 8979
f2810fe0
JB
8980 if (i.prefixes != 0)
8981 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
252b5132 8982
48ef937e
JB
8983 if (now_seg == absolute_section)
8984 {
8985 abs_section_offset += prefix + 1 + 2 + size;
8986 return;
8987 }
8988
29b0f896
AM
8989 /* 1 opcode; 2 segment; offset */
8990 p = frag_more (prefix + 1 + 2 + size);
3e73aa7c 8991
29b0f896
AM
8992 if (i.prefix[DATA_PREFIX] != 0)
8993 *p++ = DATA_PREFIX_OPCODE;
252b5132 8994
29b0f896
AM
8995 if (i.prefix[REX_PREFIX] != 0)
8996 *p++ = i.prefix[REX_PREFIX];
252b5132 8997
29b0f896
AM
8998 *p++ = i.tm.base_opcode;
8999 if (i.op[1].imms->X_op == O_constant)
9000 {
9001 offsetT n = i.op[1].imms->X_add_number;
252b5132 9002
29b0f896
AM
9003 if (size == 2
9004 && !fits_in_unsigned_word (n)
9005 && !fits_in_signed_word (n))
9006 {
9007 as_bad (_("16-bit jump out of range"));
9008 return;
9009 }
9010 md_number_to_chars (p, n, size);
9011 }
9012 else
9013 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
d258b828 9014 i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
6d96a594
C
9015
9016 p += size;
9017 if (i.op[0].imms->X_op == O_constant)
9018 md_number_to_chars (p, (valueT) i.op[0].imms->X_add_number, 2);
9019 else
9020 fix_new_exp (frag_now, p - frag_now->fr_literal, 2,
9021 i.op[0].imms, 0, reloc (2, 0, 0, i.reloc[0]));
29b0f896 9022}
a217f122 9023
b4a3a7b4
L
9024#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9025void
9026x86_cleanup (void)
9027{
9028 char *p;
9029 asection *seg = now_seg;
9030 subsegT subseg = now_subseg;
9031 asection *sec;
9032 unsigned int alignment, align_size_1;
9033 unsigned int isa_1_descsz, feature_2_descsz, descsz;
9034 unsigned int isa_1_descsz_raw, feature_2_descsz_raw;
9035 unsigned int padding;
9036
1273b2f8 9037 if (!IS_ELF || !x86_used_note)
b4a3a7b4
L
9038 return;
9039
b4a3a7b4
L
9040 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X86;
9041
9042 /* The .note.gnu.property section layout:
9043
9044 Field Length Contents
9045 ---- ---- ----
9046 n_namsz 4 4
9047 n_descsz 4 The note descriptor size
9048 n_type 4 NT_GNU_PROPERTY_TYPE_0
9049 n_name 4 "GNU"
9050 n_desc n_descsz The program property array
9051 .... .... ....
9052 */
9053
9054 /* Create the .note.gnu.property section. */
9055 sec = subseg_new (NOTE_GNU_PROPERTY_SECTION_NAME, 0);
fd361982 9056 bfd_set_section_flags (sec,
b4a3a7b4
L
9057 (SEC_ALLOC
9058 | SEC_LOAD
9059 | SEC_DATA
9060 | SEC_HAS_CONTENTS
9061 | SEC_READONLY));
9062
9063 if (get_elf_backend_data (stdoutput)->s->elfclass == ELFCLASS64)
9064 {
9065 align_size_1 = 7;
9066 alignment = 3;
9067 }
9068 else
9069 {
9070 align_size_1 = 3;
9071 alignment = 2;
9072 }
9073
fd361982 9074 bfd_set_section_alignment (sec, alignment);
b4a3a7b4
L
9075 elf_section_type (sec) = SHT_NOTE;
9076
1273b2f8
L
9077 /* GNU_PROPERTY_X86_ISA_1_USED: 4-byte type + 4-byte data size
9078 + 4-byte data */
9079 isa_1_descsz_raw = 4 + 4 + 4;
9080 /* Align GNU_PROPERTY_X86_ISA_1_USED. */
9081 isa_1_descsz = (isa_1_descsz_raw + align_size_1) & ~align_size_1;
b4a3a7b4
L
9082
9083 feature_2_descsz_raw = isa_1_descsz;
9084 /* GNU_PROPERTY_X86_FEATURE_2_USED: 4-byte type + 4-byte data size
1273b2f8 9085 + 4-byte data */
b4a3a7b4
L
9086 feature_2_descsz_raw += 4 + 4 + 4;
9087 /* Align GNU_PROPERTY_X86_FEATURE_2_USED. */
9088 feature_2_descsz = ((feature_2_descsz_raw + align_size_1)
9089 & ~align_size_1);
9090
9091 descsz = feature_2_descsz;
9092 /* Section size: n_namsz + n_descsz + n_type + n_name + n_descsz. */
9093 p = frag_more (4 + 4 + 4 + 4 + descsz);
9094
9095 /* Write n_namsz. */
9096 md_number_to_chars (p, (valueT) 4, 4);
9097
9098 /* Write n_descsz. */
9099 md_number_to_chars (p + 4, (valueT) descsz, 4);
9100
9101 /* Write n_type. */
9102 md_number_to_chars (p + 4 * 2, (valueT) NT_GNU_PROPERTY_TYPE_0, 4);
9103
9104 /* Write n_name. */
9105 memcpy (p + 4 * 3, "GNU", 4);
9106
1273b2f8
L
9107 /* Write 4-byte type. */
9108 md_number_to_chars (p + 4 * 4,
9109 (valueT) GNU_PROPERTY_X86_ISA_1_USED, 4);
b4a3a7b4 9110
1273b2f8
L
9111 /* Write 4-byte data size. */
9112 md_number_to_chars (p + 4 * 5, (valueT) 4, 4);
b4a3a7b4 9113
1273b2f8
L
9114 /* Write 4-byte data. */
9115 md_number_to_chars (p + 4 * 6, (valueT) x86_isa_1_used, 4);
b4a3a7b4 9116
1273b2f8
L
9117 /* Zero out paddings. */
9118 padding = isa_1_descsz - isa_1_descsz_raw;
9119 if (padding)
9120 memset (p + 4 * 7, 0, padding);
b4a3a7b4
L
9121
9122 /* Write 4-byte type. */
9123 md_number_to_chars (p + isa_1_descsz + 4 * 4,
9124 (valueT) GNU_PROPERTY_X86_FEATURE_2_USED, 4);
9125
9126 /* Write 4-byte data size. */
9127 md_number_to_chars (p + isa_1_descsz + 4 * 5, (valueT) 4, 4);
9128
9129 /* Write 4-byte data. */
9130 md_number_to_chars (p + isa_1_descsz + 4 * 6,
9131 (valueT) x86_feature_2_used, 4);
9132
9133 /* Zero out paddings. */
9134 padding = feature_2_descsz - feature_2_descsz_raw;
9135 if (padding)
9136 memset (p + isa_1_descsz + 4 * 7, 0, padding);
9137
9138 /* We probably can't restore the current segment, for there likely
9139 isn't one yet... */
9140 if (seg && subseg)
9141 subseg_set (seg, subseg);
9142}
9143#endif
9144
9c33702b
JB
9145static unsigned int
9146encoding_length (const fragS *start_frag, offsetT start_off,
9147 const char *frag_now_ptr)
9148{
9149 unsigned int len = 0;
9150
9151 if (start_frag != frag_now)
9152 {
9153 const fragS *fr = start_frag;
9154
9155 do {
9156 len += fr->fr_fix;
9157 fr = fr->fr_next;
9158 } while (fr && fr != frag_now);
9159 }
9160
9161 return len - start_off + (frag_now_ptr - frag_now->fr_literal);
9162}
9163
e379e5f3 9164/* Return 1 for test, and, cmp, add, sub, inc and dec which may
79d72f45
HL
9165 be macro-fused with conditional jumps.
9166 NB: If TEST/AND/CMP/ADD/SUB/INC/DEC is of RIP relative address,
9167 or is one of the following format:
9168
9169 cmp m, imm
9170 add m, imm
9171 sub m, imm
9172 test m, imm
9173 and m, imm
9174 inc m
9175 dec m
9176
9177 it is unfusible. */
e379e5f3
L
9178
9179static int
79d72f45 9180maybe_fused_with_jcc_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
9181{
9182 /* No RIP address. */
9183 if (i.base_reg && i.base_reg->reg_num == RegIP)
9184 return 0;
9185
389d00a5
JB
9186 /* No opcodes outside of base encoding space. */
9187 if (i.tm.opcode_modifier.opcodespace != SPACE_BASE)
e379e5f3
L
9188 return 0;
9189
79d72f45
HL
9190 /* add, sub without add/sub m, imm. */
9191 if (i.tm.base_opcode <= 5
e379e5f3
L
9192 || (i.tm.base_opcode >= 0x28 && i.tm.base_opcode <= 0x2d)
9193 || ((i.tm.base_opcode | 3) == 0x83
79d72f45 9194 && (i.tm.extension_opcode == 0x5
e379e5f3 9195 || i.tm.extension_opcode == 0x0)))
79d72f45
HL
9196 {
9197 *mf_cmp_p = mf_cmp_alu_cmp;
9198 return !(i.mem_operands && i.imm_operands);
9199 }
e379e5f3 9200
79d72f45
HL
9201 /* and without and m, imm. */
9202 if ((i.tm.base_opcode >= 0x20 && i.tm.base_opcode <= 0x25)
9203 || ((i.tm.base_opcode | 3) == 0x83
9204 && i.tm.extension_opcode == 0x4))
9205 {
9206 *mf_cmp_p = mf_cmp_test_and;
9207 return !(i.mem_operands && i.imm_operands);
9208 }
9209
9210 /* test without test m imm. */
e379e5f3
L
9211 if ((i.tm.base_opcode | 1) == 0x85
9212 || (i.tm.base_opcode | 1) == 0xa9
9213 || ((i.tm.base_opcode | 1) == 0xf7
79d72f45
HL
9214 && i.tm.extension_opcode == 0))
9215 {
9216 *mf_cmp_p = mf_cmp_test_and;
9217 return !(i.mem_operands && i.imm_operands);
9218 }
9219
9220 /* cmp without cmp m, imm. */
9221 if ((i.tm.base_opcode >= 0x38 && i.tm.base_opcode <= 0x3d)
e379e5f3
L
9222 || ((i.tm.base_opcode | 3) == 0x83
9223 && (i.tm.extension_opcode == 0x7)))
79d72f45
HL
9224 {
9225 *mf_cmp_p = mf_cmp_alu_cmp;
9226 return !(i.mem_operands && i.imm_operands);
9227 }
e379e5f3 9228
79d72f45 9229 /* inc, dec without inc/dec m. */
e379e5f3
L
9230 if ((i.tm.cpu_flags.bitfield.cpuno64
9231 && (i.tm.base_opcode | 0xf) == 0x4f)
9232 || ((i.tm.base_opcode | 1) == 0xff
9233 && i.tm.extension_opcode <= 0x1))
79d72f45
HL
9234 {
9235 *mf_cmp_p = mf_cmp_incdec;
9236 return !i.mem_operands;
9237 }
e379e5f3
L
9238
9239 return 0;
9240}
9241
9242/* Return 1 if a FUSED_JCC_PADDING frag should be generated. */
9243
9244static int
79d72f45 9245add_fused_jcc_padding_frag_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
9246{
9247 /* NB: Don't work with COND_JUMP86 without i386. */
9248 if (!align_branch_power
9249 || now_seg == absolute_section
9250 || !cpu_arch_flags.bitfield.cpui386
9251 || !(align_branch & align_branch_fused_bit))
9252 return 0;
9253
79d72f45 9254 if (maybe_fused_with_jcc_p (mf_cmp_p))
e379e5f3
L
9255 {
9256 if (last_insn.kind == last_insn_other
9257 || last_insn.seg != now_seg)
9258 return 1;
9259 if (flag_debug)
9260 as_warn_where (last_insn.file, last_insn.line,
9261 _("`%s` skips -malign-branch-boundary on `%s`"),
9262 last_insn.name, i.tm.name);
9263 }
9264
9265 return 0;
9266}
9267
9268/* Return 1 if a BRANCH_PREFIX frag should be generated. */
9269
9270static int
9271add_branch_prefix_frag_p (void)
9272{
9273 /* NB: Don't work with COND_JUMP86 without i386. Don't add prefix
9274 to PadLock instructions since they include prefixes in opcode. */
9275 if (!align_branch_power
9276 || !align_branch_prefix_size
9277 || now_seg == absolute_section
9278 || i.tm.cpu_flags.bitfield.cpupadlock
9279 || !cpu_arch_flags.bitfield.cpui386)
9280 return 0;
9281
9282 /* Don't add prefix if it is a prefix or there is no operand in case
9283 that segment prefix is special. */
9284 if (!i.operands || i.tm.opcode_modifier.isprefix)
9285 return 0;
9286
9287 if (last_insn.kind == last_insn_other
9288 || last_insn.seg != now_seg)
9289 return 1;
9290
9291 if (flag_debug)
9292 as_warn_where (last_insn.file, last_insn.line,
9293 _("`%s` skips -malign-branch-boundary on `%s`"),
9294 last_insn.name, i.tm.name);
9295
9296 return 0;
9297}
9298
9299/* Return 1 if a BRANCH_PADDING frag should be generated. */
9300
9301static int
79d72f45
HL
9302add_branch_padding_frag_p (enum align_branch_kind *branch_p,
9303 enum mf_jcc_kind *mf_jcc_p)
e379e5f3
L
9304{
9305 int add_padding;
9306
9307 /* NB: Don't work with COND_JUMP86 without i386. */
9308 if (!align_branch_power
9309 || now_seg == absolute_section
389d00a5
JB
9310 || !cpu_arch_flags.bitfield.cpui386
9311 || i.tm.opcode_modifier.opcodespace != SPACE_BASE)
e379e5f3
L
9312 return 0;
9313
9314 add_padding = 0;
9315
9316 /* Check for jcc and direct jmp. */
9317 if (i.tm.opcode_modifier.jump == JUMP)
9318 {
9319 if (i.tm.base_opcode == JUMP_PC_RELATIVE)
9320 {
9321 *branch_p = align_branch_jmp;
9322 add_padding = align_branch & align_branch_jmp_bit;
9323 }
9324 else
9325 {
79d72f45
HL
9326 /* Because J<cc> and JN<cc> share same group in macro-fusible table,
9327 igore the lowest bit. */
9328 *mf_jcc_p = (i.tm.base_opcode & 0x0e) >> 1;
e379e5f3
L
9329 *branch_p = align_branch_jcc;
9330 if ((align_branch & align_branch_jcc_bit))
9331 add_padding = 1;
9332 }
9333 }
e379e5f3
L
9334 else if ((i.tm.base_opcode | 1) == 0xc3)
9335 {
9336 /* Near ret. */
9337 *branch_p = align_branch_ret;
9338 if ((align_branch & align_branch_ret_bit))
9339 add_padding = 1;
9340 }
9341 else
9342 {
9343 /* Check for indirect jmp, direct and indirect calls. */
9344 if (i.tm.base_opcode == 0xe8)
9345 {
9346 /* Direct call. */
9347 *branch_p = align_branch_call;
9348 if ((align_branch & align_branch_call_bit))
9349 add_padding = 1;
9350 }
9351 else if (i.tm.base_opcode == 0xff
9352 && (i.tm.extension_opcode == 2
9353 || i.tm.extension_opcode == 4))
9354 {
9355 /* Indirect call and jmp. */
9356 *branch_p = align_branch_indirect;
9357 if ((align_branch & align_branch_indirect_bit))
9358 add_padding = 1;
9359 }
9360
9361 if (add_padding
9362 && i.disp_operands
9363 && tls_get_addr
9364 && (i.op[0].disps->X_op == O_symbol
9365 || (i.op[0].disps->X_op == O_subtract
9366 && i.op[0].disps->X_op_symbol == GOT_symbol)))
9367 {
9368 symbolS *s = i.op[0].disps->X_add_symbol;
9369 /* No padding to call to global or undefined tls_get_addr. */
9370 if ((S_IS_EXTERNAL (s) || !S_IS_DEFINED (s))
9371 && strcmp (S_GET_NAME (s), tls_get_addr) == 0)
9372 return 0;
9373 }
9374 }
9375
9376 if (add_padding
9377 && last_insn.kind != last_insn_other
9378 && last_insn.seg == now_seg)
9379 {
9380 if (flag_debug)
9381 as_warn_where (last_insn.file, last_insn.line,
9382 _("`%s` skips -malign-branch-boundary on `%s`"),
9383 last_insn.name, i.tm.name);
9384 return 0;
9385 }
9386
9387 return add_padding;
9388}
9389
29b0f896 9390static void
e3bb37b5 9391output_insn (void)
29b0f896 9392{
2bbd9c25
JJ
9393 fragS *insn_start_frag;
9394 offsetT insn_start_off;
e379e5f3
L
9395 fragS *fragP = NULL;
9396 enum align_branch_kind branch = align_branch_none;
79d72f45
HL
9397 /* The initializer is arbitrary just to avoid uninitialized error.
9398 it's actually either assigned in add_branch_padding_frag_p
9399 or never be used. */
9400 enum mf_jcc_kind mf_jcc = mf_jcc_jo;
2bbd9c25 9401
b4a3a7b4 9402#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
48ef937e 9403 if (IS_ELF && x86_used_note && now_seg != absolute_section)
b4a3a7b4 9404 {
32930e4e
L
9405 if ((i.xstate & xstate_tmm) == xstate_tmm
9406 || i.tm.cpu_flags.bitfield.cpuamx_tile)
9407 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_TMM;
9408
b4a3a7b4
L
9409 if (i.tm.cpu_flags.bitfield.cpu8087
9410 || i.tm.cpu_flags.bitfield.cpu287
9411 || i.tm.cpu_flags.bitfield.cpu387
9412 || i.tm.cpu_flags.bitfield.cpu687
9413 || i.tm.cpu_flags.bitfield.cpufisttp)
9414 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X87;
014d61ea 9415
921eafea 9416 if ((i.xstate & xstate_mmx)
389d00a5
JB
9417 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
9418 && !is_any_vex_encoding (&i.tm)
9419 && (i.tm.base_opcode == 0x77 /* emms */
9420 || i.tm.base_opcode == 0x0e /* femms */)))
b4a3a7b4 9421 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MMX;
014d61ea 9422
32930e4e
L
9423 if (i.index_reg)
9424 {
9425 if (i.index_reg->reg_type.bitfield.zmmword)
9426 i.xstate |= xstate_zmm;
9427 else if (i.index_reg->reg_type.bitfield.ymmword)
9428 i.xstate |= xstate_ymm;
9429 else if (i.index_reg->reg_type.bitfield.xmmword)
9430 i.xstate |= xstate_xmm;
9431 }
014d61ea
JB
9432
9433 /* vzeroall / vzeroupper */
9434 if (i.tm.base_opcode == 0x77 && i.tm.cpu_flags.bitfield.cpuavx)
9435 i.xstate |= xstate_ymm;
9436
c4694f17 9437 if ((i.xstate & xstate_xmm)
389d00a5
JB
9438 /* ldmxcsr / stmxcsr / vldmxcsr / vstmxcsr */
9439 || (i.tm.base_opcode == 0xae
9440 && (i.tm.cpu_flags.bitfield.cpusse
9441 || i.tm.cpu_flags.bitfield.cpuavx))
c4694f17
TG
9442 || i.tm.cpu_flags.bitfield.cpuwidekl
9443 || i.tm.cpu_flags.bitfield.cpukl)
b4a3a7b4 9444 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XMM;
014d61ea 9445
921eafea 9446 if ((i.xstate & xstate_ymm) == xstate_ymm)
b4a3a7b4 9447 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_YMM;
921eafea 9448 if ((i.xstate & xstate_zmm) == xstate_zmm)
b4a3a7b4 9449 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_ZMM;
6225c532 9450 if (i.mask.reg || (i.xstate & xstate_mask) == xstate_mask)
32930e4e 9451 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MASK;
b4a3a7b4
L
9452 if (i.tm.cpu_flags.bitfield.cpufxsr)
9453 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_FXSR;
9454 if (i.tm.cpu_flags.bitfield.cpuxsave)
9455 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVE;
9456 if (i.tm.cpu_flags.bitfield.cpuxsaveopt)
9457 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT;
9458 if (i.tm.cpu_flags.bitfield.cpuxsavec)
9459 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEC;
b0ab0693
L
9460
9461 if (x86_feature_2_used
9462 || i.tm.cpu_flags.bitfield.cpucmov
9463 || i.tm.cpu_flags.bitfield.cpusyscall
389d00a5
JB
9464 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
9465 && i.tm.base_opcode == 0xc7
70e95837 9466 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
b0ab0693
L
9467 && i.tm.extension_opcode == 1) /* cmpxchg8b */)
9468 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_BASELINE;
9469 if (i.tm.cpu_flags.bitfield.cpusse3
9470 || i.tm.cpu_flags.bitfield.cpussse3
9471 || i.tm.cpu_flags.bitfield.cpusse4_1
9472 || i.tm.cpu_flags.bitfield.cpusse4_2
9473 || i.tm.cpu_flags.bitfield.cpucx16
9474 || i.tm.cpu_flags.bitfield.cpupopcnt
9475 /* LAHF-SAHF insns in 64-bit mode. */
9476 || (flag_code == CODE_64BIT
35648716
JB
9477 && (i.tm.base_opcode | 1) == 0x9f
9478 && i.tm.opcode_modifier.opcodespace == SPACE_BASE))
b0ab0693
L
9479 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V2;
9480 if (i.tm.cpu_flags.bitfield.cpuavx
9481 || i.tm.cpu_flags.bitfield.cpuavx2
a9860005
JB
9482 /* Any VEX encoded insns execpt for AVX512F, AVX512BW, AVX512DQ,
9483 XOP, FMA4, LPW, TBM, and AMX. */
b0ab0693
L
9484 || (i.tm.opcode_modifier.vex
9485 && !i.tm.cpu_flags.bitfield.cpuavx512f
9486 && !i.tm.cpu_flags.bitfield.cpuavx512bw
9487 && !i.tm.cpu_flags.bitfield.cpuavx512dq
a9860005
JB
9488 && !i.tm.cpu_flags.bitfield.cpuxop
9489 && !i.tm.cpu_flags.bitfield.cpufma4
b0ab0693
L
9490 && !i.tm.cpu_flags.bitfield.cpulwp
9491 && !i.tm.cpu_flags.bitfield.cputbm
9492 && !(x86_feature_2_used & GNU_PROPERTY_X86_FEATURE_2_TMM))
9493 || i.tm.cpu_flags.bitfield.cpuf16c
9494 || i.tm.cpu_flags.bitfield.cpufma
9495 || i.tm.cpu_flags.bitfield.cpulzcnt
9496 || i.tm.cpu_flags.bitfield.cpumovbe
9497 || i.tm.cpu_flags.bitfield.cpuxsaves
9498 || (x86_feature_2_used
9499 & (GNU_PROPERTY_X86_FEATURE_2_XSAVE
9500 | GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT
9501 | GNU_PROPERTY_X86_FEATURE_2_XSAVEC)) != 0)
9502 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V3;
9503 if (i.tm.cpu_flags.bitfield.cpuavx512f
9504 || i.tm.cpu_flags.bitfield.cpuavx512bw
9505 || i.tm.cpu_flags.bitfield.cpuavx512dq
9506 || i.tm.cpu_flags.bitfield.cpuavx512vl
a9860005
JB
9507 /* Any EVEX encoded insns except for AVX512ER, AVX512PF,
9508 AVX512-4FMAPS, and AVX512-4VNNIW. */
b0ab0693
L
9509 || (i.tm.opcode_modifier.evex
9510 && !i.tm.cpu_flags.bitfield.cpuavx512er
9511 && !i.tm.cpu_flags.bitfield.cpuavx512pf
a9860005 9512 && !i.tm.cpu_flags.bitfield.cpuavx512_4fmaps
b0ab0693
L
9513 && !i.tm.cpu_flags.bitfield.cpuavx512_4vnniw))
9514 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V4;
b4a3a7b4
L
9515 }
9516#endif
9517
29b0f896
AM
9518 /* Tie dwarf2 debug info to the address at the start of the insn.
9519 We can't do this after the insn has been output as the current
9520 frag may have been closed off. eg. by frag_var. */
9521 dwarf2_emit_insn (0);
9522
2bbd9c25
JJ
9523 insn_start_frag = frag_now;
9524 insn_start_off = frag_now_fix ();
9525
79d72f45 9526 if (add_branch_padding_frag_p (&branch, &mf_jcc))
e379e5f3
L
9527 {
9528 char *p;
9529 /* Branch can be 8 bytes. Leave some room for prefixes. */
9530 unsigned int max_branch_padding_size = 14;
9531
9532 /* Align section to boundary. */
9533 record_alignment (now_seg, align_branch_power);
9534
9535 /* Make room for padding. */
9536 frag_grow (max_branch_padding_size);
9537
9538 /* Start of the padding. */
9539 p = frag_more (0);
9540
9541 fragP = frag_now;
9542
9543 frag_var (rs_machine_dependent, max_branch_padding_size, 0,
9544 ENCODE_RELAX_STATE (BRANCH_PADDING, 0),
9545 NULL, 0, p);
9546
79d72f45 9547 fragP->tc_frag_data.mf_type = mf_jcc;
e379e5f3
L
9548 fragP->tc_frag_data.branch_type = branch;
9549 fragP->tc_frag_data.max_bytes = max_branch_padding_size;
9550 }
9551
29b0f896 9552 /* Output jumps. */
0cfa3eb3 9553 if (i.tm.opcode_modifier.jump == JUMP)
29b0f896 9554 output_branch ();
0cfa3eb3
JB
9555 else if (i.tm.opcode_modifier.jump == JUMP_BYTE
9556 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896 9557 output_jump ();
0cfa3eb3 9558 else if (i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT)
29b0f896
AM
9559 output_interseg_jump ();
9560 else
9561 {
9562 /* Output normal instructions here. */
9563 char *p;
9564 unsigned char *q;
47465058 9565 unsigned int j;
79d72f45 9566 enum mf_cmp_kind mf_cmp;
4dffcebc 9567
e4e00185 9568 if (avoid_fence
389d00a5
JB
9569 && (i.tm.base_opcode == 0xaee8
9570 || i.tm.base_opcode == 0xaef0
9571 || i.tm.base_opcode == 0xaef8))
48ef937e
JB
9572 {
9573 /* Encode lfence, mfence, and sfence as
9574 f0 83 04 24 00 lock addl $0x0, (%{re}sp). */
47f4115a
JB
9575 if (flag_code == CODE_16BIT)
9576 as_bad (_("Cannot convert `%s' in 16-bit mode"), i.tm.name);
9577 else if (omit_lock_prefix)
9578 as_bad (_("Cannot convert `%s' with `-momit-lock-prefix=yes' in effect"),
9579 i.tm.name);
9580 else if (now_seg != absolute_section)
48ef937e
JB
9581 {
9582 offsetT val = 0x240483f0ULL;
9583
9584 p = frag_more (5);
9585 md_number_to_chars (p, val, 5);
9586 }
9587 else
9588 abs_section_offset += 5;
9589 return;
9590 }
e4e00185 9591
d022bddd
IT
9592 /* Some processors fail on LOCK prefix. This options makes
9593 assembler ignore LOCK prefix and serves as a workaround. */
9594 if (omit_lock_prefix)
9595 {
35648716
JB
9596 if (i.tm.base_opcode == LOCK_PREFIX_OPCODE
9597 && i.tm.opcode_modifier.isprefix)
d022bddd
IT
9598 return;
9599 i.prefix[LOCK_PREFIX] = 0;
9600 }
9601
e379e5f3
L
9602 if (branch)
9603 /* Skip if this is a branch. */
9604 ;
79d72f45 9605 else if (add_fused_jcc_padding_frag_p (&mf_cmp))
e379e5f3
L
9606 {
9607 /* Make room for padding. */
9608 frag_grow (MAX_FUSED_JCC_PADDING_SIZE);
9609 p = frag_more (0);
9610
9611 fragP = frag_now;
9612
9613 frag_var (rs_machine_dependent, MAX_FUSED_JCC_PADDING_SIZE, 0,
9614 ENCODE_RELAX_STATE (FUSED_JCC_PADDING, 0),
9615 NULL, 0, p);
9616
79d72f45 9617 fragP->tc_frag_data.mf_type = mf_cmp;
e379e5f3
L
9618 fragP->tc_frag_data.branch_type = align_branch_fused;
9619 fragP->tc_frag_data.max_bytes = MAX_FUSED_JCC_PADDING_SIZE;
9620 }
9621 else if (add_branch_prefix_frag_p ())
9622 {
9623 unsigned int max_prefix_size = align_branch_prefix_size;
9624
9625 /* Make room for padding. */
9626 frag_grow (max_prefix_size);
9627 p = frag_more (0);
9628
9629 fragP = frag_now;
9630
9631 frag_var (rs_machine_dependent, max_prefix_size, 0,
9632 ENCODE_RELAX_STATE (BRANCH_PREFIX, 0),
9633 NULL, 0, p);
9634
9635 fragP->tc_frag_data.max_bytes = max_prefix_size;
9636 }
9637
43234a1e
L
9638 /* Since the VEX/EVEX prefix contains the implicit prefix, we
9639 don't need the explicit prefix. */
cf665fee 9640 if (!is_any_vex_encoding (&i.tm))
bc4bd9ab 9641 {
7b47a312 9642 switch (i.tm.opcode_modifier.opcodeprefix)
bc4bd9ab 9643 {
7b47a312
L
9644 case PREFIX_0X66:
9645 add_prefix (0x66);
9646 break;
9647 case PREFIX_0XF2:
9648 add_prefix (0xf2);
9649 break;
9650 case PREFIX_0XF3:
8b65b895
L
9651 if (!i.tm.cpu_flags.bitfield.cpupadlock
9652 || (i.prefix[REP_PREFIX] != 0xf3))
9653 add_prefix (0xf3);
c0f3af97 9654 break;
7b47a312 9655 case PREFIX_NONE:
9a182d04 9656 switch (i.opcode_length)
c0f3af97 9657 {
7b47a312 9658 case 2:
7b47a312 9659 break;
9a182d04 9660 case 1:
7b47a312 9661 /* Check for pseudo prefixes. */
9a182d04
JB
9662 if (!i.tm.opcode_modifier.isprefix || i.tm.base_opcode)
9663 break;
7b47a312
L
9664 as_bad_where (insn_start_frag->fr_file,
9665 insn_start_frag->fr_line,
9666 _("pseudo prefix without instruction"));
9667 return;
9668 default:
9669 abort ();
4dffcebc 9670 }
c0f3af97 9671 break;
c0f3af97
L
9672 default:
9673 abort ();
bc4bd9ab 9674 }
c0f3af97 9675
6d19a37a 9676#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
cf61b747
L
9677 /* For x32, add a dummy REX_OPCODE prefix for mov/add with
9678 R_X86_64_GOTTPOFF relocation so that linker can safely
14470f07
L
9679 perform IE->LE optimization. A dummy REX_OPCODE prefix
9680 is also needed for lea with R_X86_64_GOTPC32_TLSDESC
9681 relocation for GDesc -> IE/LE optimization. */
cf61b747
L
9682 if (x86_elf_abi == X86_64_X32_ABI
9683 && i.operands == 2
14470f07
L
9684 && (i.reloc[0] == BFD_RELOC_X86_64_GOTTPOFF
9685 || i.reloc[0] == BFD_RELOC_X86_64_GOTPC32_TLSDESC)
cf61b747
L
9686 && i.prefix[REX_PREFIX] == 0)
9687 add_prefix (REX_OPCODE);
6d19a37a 9688#endif
cf61b747 9689
c0f3af97
L
9690 /* The prefix bytes. */
9691 for (j = ARRAY_SIZE (i.prefix), q = i.prefix; j > 0; j--, q++)
9692 if (*q)
48ef937e 9693 frag_opcode_byte (*q);
0f10071e 9694 }
ae5c1c7b 9695 else
c0f3af97
L
9696 {
9697 for (j = 0, q = i.prefix; j < ARRAY_SIZE (i.prefix); j++, q++)
9698 if (*q)
9699 switch (j)
9700 {
c0f3af97
L
9701 case SEG_PREFIX:
9702 case ADDR_PREFIX:
48ef937e 9703 frag_opcode_byte (*q);
c0f3af97
L
9704 break;
9705 default:
9706 /* There should be no other prefixes for instructions
9707 with VEX prefix. */
9708 abort ();
9709 }
9710
43234a1e
L
9711 /* For EVEX instructions i.vrex should become 0 after
9712 build_evex_prefix. For VEX instructions upper 16 registers
9713 aren't available, so VREX should be 0. */
9714 if (i.vrex)
9715 abort ();
c0f3af97 9716 /* Now the VEX prefix. */
48ef937e
JB
9717 if (now_seg != absolute_section)
9718 {
9719 p = frag_more (i.vex.length);
9720 for (j = 0; j < i.vex.length; j++)
9721 p[j] = i.vex.bytes[j];
9722 }
9723 else
9724 abs_section_offset += i.vex.length;
c0f3af97 9725 }
252b5132 9726
29b0f896 9727 /* Now the opcode; be careful about word order here! */
389d00a5
JB
9728 j = i.opcode_length;
9729 if (!i.vex.length)
9730 switch (i.tm.opcode_modifier.opcodespace)
9731 {
9732 case SPACE_BASE:
9733 break;
9734 case SPACE_0F:
9735 ++j;
9736 break;
9737 case SPACE_0F38:
9738 case SPACE_0F3A:
9739 j += 2;
9740 break;
9741 default:
9742 abort ();
9743 }
9744
48ef937e 9745 if (now_seg == absolute_section)
389d00a5
JB
9746 abs_section_offset += j;
9747 else if (j == 1)
29b0f896
AM
9748 {
9749 FRAG_APPEND_1_CHAR (i.tm.base_opcode);
9750 }
9751 else
9752 {
389d00a5
JB
9753 p = frag_more (j);
9754 if (!i.vex.length
9755 && i.tm.opcode_modifier.opcodespace != SPACE_BASE)
9756 {
9757 *p++ = 0x0f;
9758 if (i.tm.opcode_modifier.opcodespace != SPACE_0F)
9759 *p++ = i.tm.opcode_modifier.opcodespace == SPACE_0F38
9760 ? 0x38 : 0x3a;
9761 }
9762
9a182d04 9763 switch (i.opcode_length)
331d2d0d 9764 {
4dffcebc 9765 case 2:
389d00a5
JB
9766 /* Put out high byte first: can't use md_number_to_chars! */
9767 *p++ = (i.tm.base_opcode >> 8) & 0xff;
9768 /* Fall through. */
9769 case 1:
9770 *p = i.tm.base_opcode & 0xff;
4dffcebc
L
9771 break;
9772 default:
9773 abort ();
9774 break;
331d2d0d 9775 }
0f10071e 9776
29b0f896 9777 }
3e73aa7c 9778
29b0f896 9779 /* Now the modrm byte and sib byte (if present). */
40fb9820 9780 if (i.tm.opcode_modifier.modrm)
29b0f896 9781 {
48ef937e
JB
9782 frag_opcode_byte ((i.rm.regmem << 0)
9783 | (i.rm.reg << 3)
9784 | (i.rm.mode << 6));
29b0f896
AM
9785 /* If i.rm.regmem == ESP (4)
9786 && i.rm.mode != (Register mode)
9787 && not 16 bit
9788 ==> need second modrm byte. */
9789 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
9790 && i.rm.mode != 3
dc821c5f 9791 && !(i.base_reg && i.base_reg->reg_type.bitfield.word))
48ef937e
JB
9792 frag_opcode_byte ((i.sib.base << 0)
9793 | (i.sib.index << 3)
9794 | (i.sib.scale << 6));
29b0f896 9795 }
3e73aa7c 9796
29b0f896 9797 if (i.disp_operands)
2bbd9c25 9798 output_disp (insn_start_frag, insn_start_off);
3e73aa7c 9799
29b0f896 9800 if (i.imm_operands)
2bbd9c25 9801 output_imm (insn_start_frag, insn_start_off);
9c33702b
JB
9802
9803 /*
9804 * frag_now_fix () returning plain abs_section_offset when we're in the
9805 * absolute section, and abs_section_offset not getting updated as data
9806 * gets added to the frag breaks the logic below.
9807 */
9808 if (now_seg != absolute_section)
9809 {
9810 j = encoding_length (insn_start_frag, insn_start_off, frag_more (0));
9811 if (j > 15)
9812 as_warn (_("instruction length of %u bytes exceeds the limit of 15"),
9813 j);
e379e5f3
L
9814 else if (fragP)
9815 {
9816 /* NB: Don't add prefix with GOTPC relocation since
9817 output_disp() above depends on the fixed encoding
9818 length. Can't add prefix with TLS relocation since
9819 it breaks TLS linker optimization. */
9820 unsigned int max = i.has_gotpc_tls_reloc ? 0 : 15 - j;
9821 /* Prefix count on the current instruction. */
9822 unsigned int count = i.vex.length;
9823 unsigned int k;
9824 for (k = 0; k < ARRAY_SIZE (i.prefix); k++)
9825 /* REX byte is encoded in VEX/EVEX prefix. */
9826 if (i.prefix[k] && (k != REX_PREFIX || !i.vex.length))
9827 count++;
9828
9829 /* Count prefixes for extended opcode maps. */
9830 if (!i.vex.length)
389d00a5 9831 switch (i.tm.opcode_modifier.opcodespace)
e379e5f3 9832 {
389d00a5 9833 case SPACE_BASE:
e379e5f3 9834 break;
389d00a5
JB
9835 case SPACE_0F:
9836 count++;
e379e5f3 9837 break;
389d00a5
JB
9838 case SPACE_0F38:
9839 case SPACE_0F3A:
9840 count += 2;
e379e5f3
L
9841 break;
9842 default:
9843 abort ();
9844 }
9845
9846 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
9847 == BRANCH_PREFIX)
9848 {
9849 /* Set the maximum prefix size in BRANCH_PREFIX
9850 frag. */
9851 if (fragP->tc_frag_data.max_bytes > max)
9852 fragP->tc_frag_data.max_bytes = max;
9853 if (fragP->tc_frag_data.max_bytes > count)
9854 fragP->tc_frag_data.max_bytes -= count;
9855 else
9856 fragP->tc_frag_data.max_bytes = 0;
9857 }
9858 else
9859 {
9860 /* Remember the maximum prefix size in FUSED_JCC_PADDING
9861 frag. */
9862 unsigned int max_prefix_size;
9863 if (align_branch_prefix_size > max)
9864 max_prefix_size = max;
9865 else
9866 max_prefix_size = align_branch_prefix_size;
9867 if (max_prefix_size > count)
9868 fragP->tc_frag_data.max_prefix_length
9869 = max_prefix_size - count;
9870 }
9871
9872 /* Use existing segment prefix if possible. Use CS
9873 segment prefix in 64-bit mode. In 32-bit mode, use SS
9874 segment prefix with ESP/EBP base register and use DS
9875 segment prefix without ESP/EBP base register. */
9876 if (i.prefix[SEG_PREFIX])
9877 fragP->tc_frag_data.default_prefix = i.prefix[SEG_PREFIX];
9878 else if (flag_code == CODE_64BIT)
9879 fragP->tc_frag_data.default_prefix = CS_PREFIX_OPCODE;
9880 else if (i.base_reg
9881 && (i.base_reg->reg_num == 4
9882 || i.base_reg->reg_num == 5))
9883 fragP->tc_frag_data.default_prefix = SS_PREFIX_OPCODE;
9884 else
9885 fragP->tc_frag_data.default_prefix = DS_PREFIX_OPCODE;
9886 }
9c33702b 9887 }
29b0f896 9888 }
252b5132 9889
e379e5f3
L
9890 /* NB: Don't work with COND_JUMP86 without i386. */
9891 if (align_branch_power
9892 && now_seg != absolute_section
9893 && cpu_arch_flags.bitfield.cpui386)
9894 {
9895 /* Terminate each frag so that we can add prefix and check for
9896 fused jcc. */
9897 frag_wane (frag_now);
9898 frag_new (0);
9899 }
9900
29b0f896
AM
9901#ifdef DEBUG386
9902 if (flag_debug)
9903 {
7b81dfbb 9904 pi ("" /*line*/, &i);
29b0f896
AM
9905 }
9906#endif /* DEBUG386 */
9907}
252b5132 9908
e205caa7
L
9909/* Return the size of the displacement operand N. */
9910
9911static int
9912disp_size (unsigned int n)
9913{
9914 int size = 4;
43234a1e 9915
b5014f7a 9916 if (i.types[n].bitfield.disp64)
40fb9820
L
9917 size = 8;
9918 else if (i.types[n].bitfield.disp8)
9919 size = 1;
9920 else if (i.types[n].bitfield.disp16)
9921 size = 2;
e205caa7
L
9922 return size;
9923}
9924
9925/* Return the size of the immediate operand N. */
9926
9927static int
9928imm_size (unsigned int n)
9929{
9930 int size = 4;
40fb9820
L
9931 if (i.types[n].bitfield.imm64)
9932 size = 8;
9933 else if (i.types[n].bitfield.imm8 || i.types[n].bitfield.imm8s)
9934 size = 1;
9935 else if (i.types[n].bitfield.imm16)
9936 size = 2;
e205caa7
L
9937 return size;
9938}
9939
29b0f896 9940static void
64e74474 9941output_disp (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
9942{
9943 char *p;
9944 unsigned int n;
252b5132 9945
29b0f896
AM
9946 for (n = 0; n < i.operands; n++)
9947 {
b5014f7a 9948 if (operand_type_check (i.types[n], disp))
29b0f896 9949 {
48ef937e
JB
9950 int size = disp_size (n);
9951
9952 if (now_seg == absolute_section)
9953 abs_section_offset += size;
9954 else if (i.op[n].disps->X_op == O_constant)
29b0f896 9955 {
43234a1e 9956 offsetT val = i.op[n].disps->X_add_number;
252b5132 9957
629cfaf1
JB
9958 val = offset_in_range (val >> (size == 1 ? i.memshift : 0),
9959 size);
29b0f896
AM
9960 p = frag_more (size);
9961 md_number_to_chars (p, val, size);
9962 }
9963 else
9964 {
f86103b7 9965 enum bfd_reloc_code_real reloc_type;
a775efc8
JB
9966 bool pcrel = (i.flags[n] & Operand_PCrel) != 0;
9967 bool sign = (flag_code == CODE_64BIT && size == 4
9968 && (!want_disp32 (&i.tm)
9969 || (i.tm.opcode_modifier.jump && !i.jumpabsolute
9970 && !i.types[n].bitfield.baseindex)))
9971 || pcrel;
02a86693 9972 fixS *fixP;
29b0f896 9973
e205caa7 9974 /* We can't have 8 bit displacement here. */
9c2799c2 9975 gas_assert (!i.types[n].bitfield.disp8);
e205caa7 9976
29b0f896
AM
9977 /* The PC relative address is computed relative
9978 to the instruction boundary, so in case immediate
9979 fields follows, we need to adjust the value. */
9980 if (pcrel && i.imm_operands)
9981 {
29b0f896 9982 unsigned int n1;
e205caa7 9983 int sz = 0;
252b5132 9984
29b0f896 9985 for (n1 = 0; n1 < i.operands; n1++)
40fb9820 9986 if (operand_type_check (i.types[n1], imm))
252b5132 9987 {
e205caa7
L
9988 /* Only one immediate is allowed for PC
9989 relative address. */
9c2799c2 9990 gas_assert (sz == 0);
e205caa7
L
9991 sz = imm_size (n1);
9992 i.op[n].disps->X_add_number -= sz;
252b5132 9993 }
29b0f896 9994 /* We should find the immediate. */
9c2799c2 9995 gas_assert (sz != 0);
29b0f896 9996 }
520dc8e8 9997
29b0f896 9998 p = frag_more (size);
d258b828 9999 reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
d6ab8113 10000 if (GOT_symbol
2bbd9c25 10001 && GOT_symbol == i.op[n].disps->X_add_symbol
d6ab8113 10002 && (((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
10003 || reloc_type == BFD_RELOC_X86_64_32S
10004 || (reloc_type == BFD_RELOC_64
10005 && object_64bit))
d6ab8113
JB
10006 && (i.op[n].disps->X_op == O_symbol
10007 || (i.op[n].disps->X_op == O_add
10008 && ((symbol_get_value_expression
10009 (i.op[n].disps->X_op_symbol)->X_op)
10010 == O_subtract))))
10011 || reloc_type == BFD_RELOC_32_PCREL))
2bbd9c25 10012 {
4fa24527 10013 if (!object_64bit)
7b81dfbb
AJ
10014 {
10015 reloc_type = BFD_RELOC_386_GOTPC;
5b7c81bd 10016 i.has_gotpc_tls_reloc = true;
98da05bf 10017 i.op[n].disps->X_add_number +=
d583596c 10018 encoding_length (insn_start_frag, insn_start_off, p);
7b81dfbb
AJ
10019 }
10020 else if (reloc_type == BFD_RELOC_64)
10021 reloc_type = BFD_RELOC_X86_64_GOTPC64;
d6ab8113 10022 else
7b81dfbb
AJ
10023 /* Don't do the adjustment for x86-64, as there
10024 the pcrel addressing is relative to the _next_
10025 insn, and that is taken care of in other code. */
d6ab8113 10026 reloc_type = BFD_RELOC_X86_64_GOTPC32;
2bbd9c25 10027 }
e379e5f3
L
10028 else if (align_branch_power)
10029 {
10030 switch (reloc_type)
10031 {
10032 case BFD_RELOC_386_TLS_GD:
10033 case BFD_RELOC_386_TLS_LDM:
10034 case BFD_RELOC_386_TLS_IE:
10035 case BFD_RELOC_386_TLS_IE_32:
10036 case BFD_RELOC_386_TLS_GOTIE:
10037 case BFD_RELOC_386_TLS_GOTDESC:
10038 case BFD_RELOC_386_TLS_DESC_CALL:
10039 case BFD_RELOC_X86_64_TLSGD:
10040 case BFD_RELOC_X86_64_TLSLD:
10041 case BFD_RELOC_X86_64_GOTTPOFF:
10042 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
10043 case BFD_RELOC_X86_64_TLSDESC_CALL:
5b7c81bd 10044 i.has_gotpc_tls_reloc = true;
e379e5f3
L
10045 default:
10046 break;
10047 }
10048 }
02a86693
L
10049 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal,
10050 size, i.op[n].disps, pcrel,
10051 reloc_type);
eb19308f
JB
10052
10053 if (flag_code == CODE_64BIT && size == 4 && pcrel
10054 && !i.prefix[ADDR_PREFIX])
10055 fixP->fx_signed = 1;
10056
02a86693
L
10057 /* Check for "call/jmp *mem", "mov mem, %reg",
10058 "test %reg, mem" and "binop mem, %reg" where binop
10059 is one of adc, add, and, cmp, or, sbb, sub, xor
e60f4d3b
L
10060 instructions without data prefix. Always generate
10061 R_386_GOT32X for "sym*GOT" operand in 32-bit mode. */
10062 if (i.prefix[DATA_PREFIX] == 0
10063 && (generate_relax_relocations
10064 || (!object_64bit
10065 && i.rm.mode == 0
10066 && i.rm.regmem == 5))
0cb4071e
L
10067 && (i.rm.mode == 2
10068 || (i.rm.mode == 0 && i.rm.regmem == 5))
389d00a5 10069 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
02a86693
L
10070 && ((i.operands == 1
10071 && i.tm.base_opcode == 0xff
10072 && (i.rm.reg == 2 || i.rm.reg == 4))
10073 || (i.operands == 2
10074 && (i.tm.base_opcode == 0x8b
10075 || i.tm.base_opcode == 0x85
2ae4c703 10076 || (i.tm.base_opcode & ~0x38) == 0x03))))
02a86693
L
10077 {
10078 if (object_64bit)
10079 {
10080 fixP->fx_tcbit = i.rex != 0;
10081 if (i.base_reg
e968fc9b 10082 && (i.base_reg->reg_num == RegIP))
02a86693
L
10083 fixP->fx_tcbit2 = 1;
10084 }
10085 else
10086 fixP->fx_tcbit2 = 1;
10087 }
29b0f896
AM
10088 }
10089 }
10090 }
10091}
252b5132 10092
29b0f896 10093static void
64e74474 10094output_imm (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
10095{
10096 char *p;
10097 unsigned int n;
252b5132 10098
29b0f896
AM
10099 for (n = 0; n < i.operands; n++)
10100 {
40fb9820 10101 if (operand_type_check (i.types[n], imm))
29b0f896 10102 {
48ef937e
JB
10103 int size = imm_size (n);
10104
10105 if (now_seg == absolute_section)
10106 abs_section_offset += size;
10107 else if (i.op[n].imms->X_op == O_constant)
29b0f896 10108 {
29b0f896 10109 offsetT val;
b4cac588 10110
29b0f896
AM
10111 val = offset_in_range (i.op[n].imms->X_add_number,
10112 size);
10113 p = frag_more (size);
10114 md_number_to_chars (p, val, size);
10115 }
10116 else
10117 {
10118 /* Not absolute_section.
10119 Need a 32-bit fixup (don't support 8bit
10120 non-absolute imms). Try to support other
10121 sizes ... */
f86103b7 10122 enum bfd_reloc_code_real reloc_type;
e205caa7 10123 int sign;
29b0f896 10124
40fb9820 10125 if (i.types[n].bitfield.imm32s
a7d61044 10126 && (i.suffix == QWORD_MNEM_SUFFIX
40fb9820 10127 || (!i.suffix && i.tm.opcode_modifier.no_lsuf)))
29b0f896 10128 sign = 1;
e205caa7
L
10129 else
10130 sign = 0;
520dc8e8 10131
29b0f896 10132 p = frag_more (size);
d258b828 10133 reloc_type = reloc (size, 0, sign, i.reloc[n]);
f86103b7 10134
2bbd9c25
JJ
10135 /* This is tough to explain. We end up with this one if we
10136 * have operands that look like
10137 * "_GLOBAL_OFFSET_TABLE_+[.-.L284]". The goal here is to
10138 * obtain the absolute address of the GOT, and it is strongly
10139 * preferable from a performance point of view to avoid using
10140 * a runtime relocation for this. The actual sequence of
10141 * instructions often look something like:
10142 *
10143 * call .L66
10144 * .L66:
10145 * popl %ebx
10146 * addl $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
10147 *
10148 * The call and pop essentially return the absolute address
10149 * of the label .L66 and store it in %ebx. The linker itself
10150 * will ultimately change the first operand of the addl so
10151 * that %ebx points to the GOT, but to keep things simple, the
10152 * .o file must have this operand set so that it generates not
10153 * the absolute address of .L66, but the absolute address of
10154 * itself. This allows the linker itself simply treat a GOTPC
10155 * relocation as asking for a pcrel offset to the GOT to be
10156 * added in, and the addend of the relocation is stored in the
10157 * operand field for the instruction itself.
10158 *
10159 * Our job here is to fix the operand so that it would add
10160 * the correct offset so that %ebx would point to itself. The
10161 * thing that is tricky is that .-.L66 will point to the
10162 * beginning of the instruction, so we need to further modify
10163 * the operand so that it will point to itself. There are
10164 * other cases where you have something like:
10165 *
10166 * .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
10167 *
10168 * and here no correction would be required. Internally in
10169 * the assembler we treat operands of this form as not being
10170 * pcrel since the '.' is explicitly mentioned, and I wonder
10171 * whether it would simplify matters to do it this way. Who
10172 * knows. In earlier versions of the PIC patches, the
10173 * pcrel_adjust field was used to store the correction, but
10174 * since the expression is not pcrel, I felt it would be
10175 * confusing to do it this way. */
10176
d6ab8113 10177 if ((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
10178 || reloc_type == BFD_RELOC_X86_64_32S
10179 || reloc_type == BFD_RELOC_64)
29b0f896
AM
10180 && GOT_symbol
10181 && GOT_symbol == i.op[n].imms->X_add_symbol
10182 && (i.op[n].imms->X_op == O_symbol
10183 || (i.op[n].imms->X_op == O_add
10184 && ((symbol_get_value_expression
10185 (i.op[n].imms->X_op_symbol)->X_op)
10186 == O_subtract))))
10187 {
4fa24527 10188 if (!object_64bit)
d6ab8113 10189 reloc_type = BFD_RELOC_386_GOTPC;
7b81dfbb 10190 else if (size == 4)
d6ab8113 10191 reloc_type = BFD_RELOC_X86_64_GOTPC32;
7b81dfbb
AJ
10192 else if (size == 8)
10193 reloc_type = BFD_RELOC_X86_64_GOTPC64;
5b7c81bd 10194 i.has_gotpc_tls_reloc = true;
d583596c
JB
10195 i.op[n].imms->X_add_number +=
10196 encoding_length (insn_start_frag, insn_start_off, p);
29b0f896 10197 }
29b0f896
AM
10198 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
10199 i.op[n].imms, 0, reloc_type);
10200 }
10201 }
10202 }
252b5132
RH
10203}
10204\f
d182319b
JB
10205/* x86_cons_fix_new is called via the expression parsing code when a
10206 reloc is needed. We use this hook to get the correct .got reloc. */
d182319b
JB
10207static int cons_sign = -1;
10208
10209void
e3bb37b5 10210x86_cons_fix_new (fragS *frag, unsigned int off, unsigned int len,
62ebcb5c 10211 expressionS *exp, bfd_reloc_code_real_type r)
d182319b 10212{
d258b828 10213 r = reloc (len, 0, cons_sign, r);
d182319b
JB
10214
10215#ifdef TE_PE
10216 if (exp->X_op == O_secrel)
10217 {
10218 exp->X_op = O_symbol;
10219 r = BFD_RELOC_32_SECREL;
10220 }
145667f8
MH
10221 else if (exp->X_op == O_secidx)
10222 r = BFD_RELOC_16_SECIDX;
d182319b
JB
10223#endif
10224
10225 fix_new_exp (frag, off, len, exp, 0, r);
10226}
10227
357d1bd8
L
10228/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
10229 purpose of the `.dc.a' internal pseudo-op. */
10230
10231int
10232x86_address_bytes (void)
10233{
10234 if ((stdoutput->arch_info->mach & bfd_mach_x64_32))
10235 return 4;
10236 return stdoutput->arch_info->bits_per_address / 8;
10237}
10238
deea4973
JB
10239#if (!(defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined (OBJ_MACH_O)) \
10240 || defined (LEX_AT)) && !defined (TE_PE)
d258b828 10241# define lex_got(reloc, adjust, types) NULL
718ddfc0 10242#else
f3c180ae
AM
10243/* Parse operands of the form
10244 <symbol>@GOTOFF+<nnn>
10245 and similar .plt or .got references.
10246
10247 If we find one, set up the correct relocation in RELOC and copy the
10248 input string, minus the `@GOTOFF' into a malloc'd buffer for
10249 parsing by the calling routine. Return this buffer, and if ADJUST
10250 is non-null set it to the length of the string we removed from the
10251 input line. Otherwise return NULL. */
10252static char *
91d6fa6a 10253lex_got (enum bfd_reloc_code_real *rel,
64e74474 10254 int *adjust,
d258b828 10255 i386_operand_type *types)
f3c180ae 10256{
7b81dfbb
AJ
10257 /* Some of the relocations depend on the size of what field is to
10258 be relocated. But in our callers i386_immediate and i386_displacement
10259 we don't yet know the operand size (this will be set by insn
10260 matching). Hence we record the word32 relocation here,
10261 and adjust the reloc according to the real size in reloc(). */
145667f8
MH
10262 static const struct
10263 {
f3c180ae 10264 const char *str;
cff8d58a 10265 int len;
4fa24527 10266 const enum bfd_reloc_code_real rel[2];
40fb9820 10267 const i386_operand_type types64;
5b7c81bd 10268 bool need_GOT_symbol;
145667f8
MH
10269 }
10270 gotrel[] =
10271 {
deea4973 10272#ifndef TE_PE
8ce3d284 10273#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
10274 { STRING_COMMA_LEN ("SIZE"), { BFD_RELOC_SIZE32,
10275 BFD_RELOC_SIZE32 },
5b7c81bd 10276 OPERAND_TYPE_IMM32_64, false },
8ce3d284 10277#endif
cff8d58a
L
10278 { STRING_COMMA_LEN ("PLTOFF"), { _dummy_first_bfd_reloc_code_real,
10279 BFD_RELOC_X86_64_PLTOFF64 },
5b7c81bd 10280 OPERAND_TYPE_IMM64, true },
cff8d58a
L
10281 { STRING_COMMA_LEN ("PLT"), { BFD_RELOC_386_PLT32,
10282 BFD_RELOC_X86_64_PLT32 },
a775efc8 10283 OPERAND_TYPE_IMM32_32S_DISP32, false },
cff8d58a
L
10284 { STRING_COMMA_LEN ("GOTPLT"), { _dummy_first_bfd_reloc_code_real,
10285 BFD_RELOC_X86_64_GOTPLT64 },
5b7c81bd 10286 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
10287 { STRING_COMMA_LEN ("GOTOFF"), { BFD_RELOC_386_GOTOFF,
10288 BFD_RELOC_X86_64_GOTOFF64 },
5b7c81bd 10289 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
10290 { STRING_COMMA_LEN ("GOTPCREL"), { _dummy_first_bfd_reloc_code_real,
10291 BFD_RELOC_X86_64_GOTPCREL },
a775efc8 10292 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10293 { STRING_COMMA_LEN ("TLSGD"), { BFD_RELOC_386_TLS_GD,
10294 BFD_RELOC_X86_64_TLSGD },
a775efc8 10295 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10296 { STRING_COMMA_LEN ("TLSLDM"), { BFD_RELOC_386_TLS_LDM,
10297 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10298 OPERAND_TYPE_NONE, true },
cff8d58a
L
10299 { STRING_COMMA_LEN ("TLSLD"), { _dummy_first_bfd_reloc_code_real,
10300 BFD_RELOC_X86_64_TLSLD },
a775efc8 10301 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10302 { STRING_COMMA_LEN ("GOTTPOFF"), { BFD_RELOC_386_TLS_IE_32,
10303 BFD_RELOC_X86_64_GOTTPOFF },
a775efc8 10304 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10305 { STRING_COMMA_LEN ("TPOFF"), { BFD_RELOC_386_TLS_LE_32,
10306 BFD_RELOC_X86_64_TPOFF32 },
a775efc8 10307 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
10308 { STRING_COMMA_LEN ("NTPOFF"), { BFD_RELOC_386_TLS_LE,
10309 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10310 OPERAND_TYPE_NONE, true },
cff8d58a
L
10311 { STRING_COMMA_LEN ("DTPOFF"), { BFD_RELOC_386_TLS_LDO_32,
10312 BFD_RELOC_X86_64_DTPOFF32 },
a775efc8 10313 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
10314 { STRING_COMMA_LEN ("GOTNTPOFF"),{ BFD_RELOC_386_TLS_GOTIE,
10315 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10316 OPERAND_TYPE_NONE, true },
cff8d58a
L
10317 { STRING_COMMA_LEN ("INDNTPOFF"),{ BFD_RELOC_386_TLS_IE,
10318 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10319 OPERAND_TYPE_NONE, true },
cff8d58a
L
10320 { STRING_COMMA_LEN ("GOT"), { BFD_RELOC_386_GOT32,
10321 BFD_RELOC_X86_64_GOT32 },
a775efc8 10322 OPERAND_TYPE_IMM32_32S_64_DISP32, true },
cff8d58a
L
10323 { STRING_COMMA_LEN ("TLSDESC"), { BFD_RELOC_386_TLS_GOTDESC,
10324 BFD_RELOC_X86_64_GOTPC32_TLSDESC },
a775efc8 10325 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10326 { STRING_COMMA_LEN ("TLSCALL"), { BFD_RELOC_386_TLS_DESC_CALL,
10327 BFD_RELOC_X86_64_TLSDESC_CALL },
a775efc8 10328 OPERAND_TYPE_IMM32_32S_DISP32, true },
deea4973
JB
10329#else /* TE_PE */
10330 { STRING_COMMA_LEN ("SECREL32"), { BFD_RELOC_32_SECREL,
10331 BFD_RELOC_32_SECREL },
a775efc8 10332 OPERAND_TYPE_IMM32_32S_64_DISP32_64, false },
deea4973 10333#endif
f3c180ae
AM
10334 };
10335 char *cp;
10336 unsigned int j;
10337
deea4973 10338#if defined (OBJ_MAYBE_ELF) && !defined (TE_PE)
718ddfc0
JB
10339 if (!IS_ELF)
10340 return NULL;
d382c579 10341#endif
718ddfc0 10342
f3c180ae 10343 for (cp = input_line_pointer; *cp != '@'; cp++)
67c11a9b 10344 if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
f3c180ae
AM
10345 return NULL;
10346
47465058 10347 for (j = 0; j < ARRAY_SIZE (gotrel); j++)
f3c180ae 10348 {
cff8d58a 10349 int len = gotrel[j].len;
28f81592 10350 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
f3c180ae 10351 {
4fa24527 10352 if (gotrel[j].rel[object_64bit] != 0)
f3c180ae 10353 {
28f81592
AM
10354 int first, second;
10355 char *tmpbuf, *past_reloc;
f3c180ae 10356
91d6fa6a 10357 *rel = gotrel[j].rel[object_64bit];
f3c180ae 10358
3956db08
JB
10359 if (types)
10360 {
10361 if (flag_code != CODE_64BIT)
40fb9820
L
10362 {
10363 types->bitfield.imm32 = 1;
10364 types->bitfield.disp32 = 1;
10365 }
3956db08
JB
10366 else
10367 *types = gotrel[j].types64;
10368 }
10369
844bf810 10370 if (gotrel[j].need_GOT_symbol && GOT_symbol == NULL)
f3c180ae
AM
10371 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
10372
28f81592 10373 /* The length of the first part of our input line. */
f3c180ae 10374 first = cp - input_line_pointer;
28f81592
AM
10375
10376 /* The second part goes from after the reloc token until
67c11a9b 10377 (and including) an end_of_line char or comma. */
28f81592 10378 past_reloc = cp + 1 + len;
67c11a9b
AM
10379 cp = past_reloc;
10380 while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
10381 ++cp;
10382 second = cp + 1 - past_reloc;
28f81592
AM
10383
10384 /* Allocate and copy string. The trailing NUL shouldn't
10385 be necessary, but be safe. */
add39d23 10386 tmpbuf = XNEWVEC (char, first + second + 2);
f3c180ae 10387 memcpy (tmpbuf, input_line_pointer, first);
0787a12d
AM
10388 if (second != 0 && *past_reloc != ' ')
10389 /* Replace the relocation token with ' ', so that
10390 errors like foo@GOTOFF1 will be detected. */
10391 tmpbuf[first++] = ' ';
af89796a
L
10392 else
10393 /* Increment length by 1 if the relocation token is
10394 removed. */
10395 len++;
10396 if (adjust)
10397 *adjust = len;
0787a12d
AM
10398 memcpy (tmpbuf + first, past_reloc, second);
10399 tmpbuf[first + second] = '\0';
f3c180ae
AM
10400 return tmpbuf;
10401 }
10402
4fa24527
JB
10403 as_bad (_("@%s reloc is not supported with %d-bit output format"),
10404 gotrel[j].str, 1 << (5 + object_64bit));
f3c180ae
AM
10405 return NULL;
10406 }
10407 }
10408
10409 /* Might be a symbol version string. Don't as_bad here. */
10410 return NULL;
10411}
4e4f7c87 10412#endif
f3c180ae 10413
62ebcb5c 10414bfd_reloc_code_real_type
e3bb37b5 10415x86_cons (expressionS *exp, int size)
f3c180ae 10416{
62ebcb5c
AM
10417 bfd_reloc_code_real_type got_reloc = NO_RELOC;
10418
2748c1b1
L
10419#if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
10420 && !defined (LEX_AT)) \
10421 || defined (TE_PE)
ee86248c
JB
10422 intel_syntax = -intel_syntax;
10423
3c7b9c2c 10424 exp->X_md = 0;
4fa24527 10425 if (size == 4 || (object_64bit && size == 8))
f3c180ae
AM
10426 {
10427 /* Handle @GOTOFF and the like in an expression. */
10428 char *save;
10429 char *gotfree_input_line;
4a57f2cf 10430 int adjust = 0;
f3c180ae
AM
10431
10432 save = input_line_pointer;
d258b828 10433 gotfree_input_line = lex_got (&got_reloc, &adjust, NULL);
f3c180ae
AM
10434 if (gotfree_input_line)
10435 input_line_pointer = gotfree_input_line;
10436
10437 expression (exp);
10438
10439 if (gotfree_input_line)
10440 {
10441 /* expression () has merrily parsed up to the end of line,
10442 or a comma - in the wrong buffer. Transfer how far
10443 input_line_pointer has moved to the right buffer. */
10444 input_line_pointer = (save
10445 + (input_line_pointer - gotfree_input_line)
10446 + adjust);
10447 free (gotfree_input_line);
3992d3b7
AM
10448 if (exp->X_op == O_constant
10449 || exp->X_op == O_absent
10450 || exp->X_op == O_illegal
0398aac5 10451 || exp->X_op == O_register
3992d3b7
AM
10452 || exp->X_op == O_big)
10453 {
10454 char c = *input_line_pointer;
10455 *input_line_pointer = 0;
10456 as_bad (_("missing or invalid expression `%s'"), save);
10457 *input_line_pointer = c;
10458 }
b9519cfe
L
10459 else if ((got_reloc == BFD_RELOC_386_PLT32
10460 || got_reloc == BFD_RELOC_X86_64_PLT32)
10461 && exp->X_op != O_symbol)
10462 {
10463 char c = *input_line_pointer;
10464 *input_line_pointer = 0;
10465 as_bad (_("invalid PLT expression `%s'"), save);
10466 *input_line_pointer = c;
10467 }
f3c180ae
AM
10468 }
10469 }
10470 else
10471 expression (exp);
ee86248c
JB
10472
10473 intel_syntax = -intel_syntax;
10474
10475 if (intel_syntax)
10476 i386_intel_simplify (exp);
2748c1b1
L
10477#else
10478 expression (exp);
10479#endif
62ebcb5c 10480
a442cac5
JB
10481 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
10482 if (size == 4 && exp->X_op == O_constant && !object_64bit)
10483 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
10484
62ebcb5c 10485 return got_reloc;
f3c180ae 10486}
f3c180ae 10487
9f32dd5b
L
10488static void
10489signed_cons (int size)
6482c264 10490{
a442cac5 10491 if (object_64bit)
d182319b
JB
10492 cons_sign = 1;
10493 cons (size);
10494 cons_sign = -1;
6482c264
NC
10495}
10496
d182319b 10497#ifdef TE_PE
6482c264 10498static void
7016a5d5 10499pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
6482c264
NC
10500{
10501 expressionS exp;
10502
10503 do
10504 {
10505 expression (&exp);
10506 if (exp.X_op == O_symbol)
10507 exp.X_op = O_secrel;
10508
10509 emit_expr (&exp, 4);
10510 }
10511 while (*input_line_pointer++ == ',');
10512
10513 input_line_pointer--;
10514 demand_empty_rest_of_line ();
10515}
145667f8
MH
10516
10517static void
10518pe_directive_secidx (int dummy ATTRIBUTE_UNUSED)
10519{
10520 expressionS exp;
10521
10522 do
10523 {
10524 expression (&exp);
10525 if (exp.X_op == O_symbol)
10526 exp.X_op = O_secidx;
10527
10528 emit_expr (&exp, 2);
10529 }
10530 while (*input_line_pointer++ == ',');
10531
10532 input_line_pointer--;
10533 demand_empty_rest_of_line ();
10534}
6482c264
NC
10535#endif
10536
7063667e
JB
10537/* Handle Rounding Control / SAE specifiers. */
10538
10539static char *
10540RC_SAE_specifier (const char *pstr)
10541{
10542 unsigned int j;
10543
10544 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); j++)
10545 {
10546 if (!strncmp (pstr, RC_NamesTable[j].name, RC_NamesTable[j].len))
10547 {
10548 if (i.rounding.type != rc_none)
10549 {
10550 as_bad (_("duplicated `{%s}'"), RC_NamesTable[j].name);
10551 return NULL;
10552 }
10553
10554 i.rounding.type = RC_NamesTable[j].type;
10555
10556 return (char *)(pstr + RC_NamesTable[j].len);
10557 }
10558 }
10559
10560 return NULL;
10561}
10562
43234a1e
L
10563/* Handle Vector operations. */
10564
10565static char *
f70c6814 10566check_VecOperations (char *op_string)
43234a1e
L
10567{
10568 const reg_entry *mask;
10569 const char *saved;
10570 char *end_op;
10571
f70c6814 10572 while (*op_string)
43234a1e
L
10573 {
10574 saved = op_string;
10575 if (*op_string == '{')
10576 {
10577 op_string++;
10578
10579 /* Check broadcasts. */
d34049e8 10580 if (startswith (op_string, "1to"))
43234a1e 10581 {
5273a3cd 10582 unsigned int bcst_type;
43234a1e 10583
5273a3cd 10584 if (i.broadcast.type)
43234a1e
L
10585 goto duplicated_vec_op;
10586
10587 op_string += 3;
10588 if (*op_string == '8')
8e6e0792 10589 bcst_type = 8;
b28d1bda 10590 else if (*op_string == '4')
8e6e0792 10591 bcst_type = 4;
b28d1bda 10592 else if (*op_string == '2')
8e6e0792 10593 bcst_type = 2;
43234a1e
L
10594 else if (*op_string == '1'
10595 && *(op_string+1) == '6')
10596 {
8e6e0792 10597 bcst_type = 16;
43234a1e
L
10598 op_string++;
10599 }
0cc78721
CL
10600 else if (*op_string == '3'
10601 && *(op_string+1) == '2')
10602 {
10603 bcst_type = 32;
10604 op_string++;
10605 }
43234a1e
L
10606 else
10607 {
10608 as_bad (_("Unsupported broadcast: `%s'"), saved);
10609 return NULL;
10610 }
10611 op_string++;
10612
5273a3cd
JB
10613 i.broadcast.type = bcst_type;
10614 i.broadcast.operand = this_operand;
43234a1e
L
10615 }
10616 /* Check masking operation. */
10617 else if ((mask = parse_register (op_string, &end_op)) != NULL)
10618 {
8a6fb3f9
JB
10619 if (mask == &bad_reg)
10620 return NULL;
10621
43234a1e 10622 /* k0 can't be used for write mask. */
f74a6307 10623 if (mask->reg_type.bitfield.class != RegMask || !mask->reg_num)
43234a1e 10624 {
6d2cd6b2
JB
10625 as_bad (_("`%s%s' can't be used for write mask"),
10626 register_prefix, mask->reg_name);
43234a1e
L
10627 return NULL;
10628 }
10629
6225c532 10630 if (!i.mask.reg)
43234a1e 10631 {
6225c532
JB
10632 i.mask.reg = mask;
10633 i.mask.operand = this_operand;
43234a1e 10634 }
6225c532
JB
10635 else if (i.mask.reg->reg_num)
10636 goto duplicated_vec_op;
43234a1e
L
10637 else
10638 {
6225c532 10639 i.mask.reg = mask;
43234a1e
L
10640
10641 /* Only "{z}" is allowed here. No need to check
10642 zeroing mask explicitly. */
6225c532 10643 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
10644 {
10645 as_bad (_("invalid write mask `%s'"), saved);
10646 return NULL;
10647 }
10648 }
10649
10650 op_string = end_op;
10651 }
10652 /* Check zeroing-flag for masking operation. */
10653 else if (*op_string == 'z')
10654 {
6225c532 10655 if (!i.mask.reg)
43234a1e 10656 {
6225c532
JB
10657 i.mask.reg = reg_k0;
10658 i.mask.zeroing = 1;
10659 i.mask.operand = this_operand;
43234a1e
L
10660 }
10661 else
10662 {
6225c532 10663 if (i.mask.zeroing)
43234a1e
L
10664 {
10665 duplicated_vec_op:
10666 as_bad (_("duplicated `%s'"), saved);
10667 return NULL;
10668 }
10669
6225c532 10670 i.mask.zeroing = 1;
43234a1e
L
10671
10672 /* Only "{%k}" is allowed here. No need to check mask
10673 register explicitly. */
6225c532 10674 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
10675 {
10676 as_bad (_("invalid zeroing-masking `%s'"),
10677 saved);
10678 return NULL;
10679 }
10680 }
10681
10682 op_string++;
10683 }
7063667e
JB
10684 else if (intel_syntax
10685 && (op_string = RC_SAE_specifier (op_string)) != NULL)
10686 i.rounding.modifier = true;
43234a1e
L
10687 else
10688 goto unknown_vec_op;
10689
10690 if (*op_string != '}')
10691 {
10692 as_bad (_("missing `}' in `%s'"), saved);
10693 return NULL;
10694 }
10695 op_string++;
0ba3a731
L
10696
10697 /* Strip whitespace since the addition of pseudo prefixes
10698 changed how the scrubber treats '{'. */
10699 if (is_space_char (*op_string))
10700 ++op_string;
10701
43234a1e
L
10702 continue;
10703 }
10704 unknown_vec_op:
10705 /* We don't know this one. */
10706 as_bad (_("unknown vector operation: `%s'"), saved);
10707 return NULL;
10708 }
10709
6225c532 10710 if (i.mask.reg && i.mask.zeroing && !i.mask.reg->reg_num)
6d2cd6b2
JB
10711 {
10712 as_bad (_("zeroing-masking only allowed with write mask"));
10713 return NULL;
10714 }
10715
43234a1e
L
10716 return op_string;
10717}
10718
252b5132 10719static int
70e41ade 10720i386_immediate (char *imm_start)
252b5132
RH
10721{
10722 char *save_input_line_pointer;
f3c180ae 10723 char *gotfree_input_line;
252b5132 10724 segT exp_seg = 0;
47926f60 10725 expressionS *exp;
40fb9820
L
10726 i386_operand_type types;
10727
0dfbf9d7 10728 operand_type_set (&types, ~0);
252b5132
RH
10729
10730 if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
10731 {
31b2323c
L
10732 as_bad (_("at most %d immediate operands are allowed"),
10733 MAX_IMMEDIATE_OPERANDS);
252b5132
RH
10734 return 0;
10735 }
10736
10737 exp = &im_expressions[i.imm_operands++];
520dc8e8 10738 i.op[this_operand].imms = exp;
252b5132
RH
10739
10740 if (is_space_char (*imm_start))
10741 ++imm_start;
10742
10743 save_input_line_pointer = input_line_pointer;
10744 input_line_pointer = imm_start;
10745
d258b828 10746 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
10747 if (gotfree_input_line)
10748 input_line_pointer = gotfree_input_line;
252b5132
RH
10749
10750 exp_seg = expression (exp);
10751
83183c0c 10752 SKIP_WHITESPACE ();
252b5132 10753 if (*input_line_pointer)
f3c180ae 10754 as_bad (_("junk `%s' after expression"), input_line_pointer);
252b5132
RH
10755
10756 input_line_pointer = save_input_line_pointer;
f3c180ae 10757 if (gotfree_input_line)
ee86248c
JB
10758 {
10759 free (gotfree_input_line);
10760
9aac24b1 10761 if (exp->X_op == O_constant)
ee86248c
JB
10762 exp->X_op = O_illegal;
10763 }
10764
9aac24b1
JB
10765 if (exp_seg == reg_section)
10766 {
10767 as_bad (_("illegal immediate register operand %s"), imm_start);
10768 return 0;
10769 }
10770
ee86248c
JB
10771 return i386_finalize_immediate (exp_seg, exp, types, imm_start);
10772}
252b5132 10773
ee86248c
JB
10774static int
10775i386_finalize_immediate (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
10776 i386_operand_type types, const char *imm_start)
10777{
10778 if (exp->X_op == O_absent || exp->X_op == O_illegal || exp->X_op == O_big)
252b5132 10779 {
313c53d1
L
10780 if (imm_start)
10781 as_bad (_("missing or invalid immediate expression `%s'"),
10782 imm_start);
3992d3b7 10783 return 0;
252b5132 10784 }
3e73aa7c 10785 else if (exp->X_op == O_constant)
252b5132 10786 {
47926f60 10787 /* Size it properly later. */
40fb9820 10788 i.types[this_operand].bitfield.imm64 = 1;
a442cac5
JB
10789
10790 /* If not 64bit, sign/zero extend val, to account for wraparound
10791 when !BFD64. */
10792 if (flag_code != CODE_64BIT)
10793 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
252b5132 10794 }
4c63da97 10795#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
f86103b7 10796 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
31312f95 10797 && exp_seg != absolute_section
47926f60 10798 && exp_seg != text_section
24eab124
AM
10799 && exp_seg != data_section
10800 && exp_seg != bss_section
10801 && exp_seg != undefined_section
f86103b7 10802 && !bfd_is_com_section (exp_seg))
252b5132 10803 {
d0b47220 10804 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
252b5132
RH
10805 return 0;
10806 }
10807#endif
10808 else
10809 {
10810 /* This is an address. The size of the address will be
24eab124 10811 determined later, depending on destination register,
3e73aa7c 10812 suffix, or the default for the section. */
40fb9820
L
10813 i.types[this_operand].bitfield.imm8 = 1;
10814 i.types[this_operand].bitfield.imm16 = 1;
10815 i.types[this_operand].bitfield.imm32 = 1;
10816 i.types[this_operand].bitfield.imm32s = 1;
10817 i.types[this_operand].bitfield.imm64 = 1;
c6fb90c8
L
10818 i.types[this_operand] = operand_type_and (i.types[this_operand],
10819 types);
252b5132
RH
10820 }
10821
10822 return 1;
10823}
10824
551c1ca1 10825static char *
e3bb37b5 10826i386_scale (char *scale)
252b5132 10827{
551c1ca1
AM
10828 offsetT val;
10829 char *save = input_line_pointer;
252b5132 10830
551c1ca1
AM
10831 input_line_pointer = scale;
10832 val = get_absolute_expression ();
10833
10834 switch (val)
252b5132 10835 {
551c1ca1 10836 case 1:
252b5132
RH
10837 i.log2_scale_factor = 0;
10838 break;
551c1ca1 10839 case 2:
252b5132
RH
10840 i.log2_scale_factor = 1;
10841 break;
551c1ca1 10842 case 4:
252b5132
RH
10843 i.log2_scale_factor = 2;
10844 break;
551c1ca1 10845 case 8:
252b5132
RH
10846 i.log2_scale_factor = 3;
10847 break;
10848 default:
a724f0f4
JB
10849 {
10850 char sep = *input_line_pointer;
10851
10852 *input_line_pointer = '\0';
10853 as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
10854 scale);
10855 *input_line_pointer = sep;
10856 input_line_pointer = save;
10857 return NULL;
10858 }
252b5132 10859 }
29b0f896 10860 if (i.log2_scale_factor != 0 && i.index_reg == 0)
252b5132
RH
10861 {
10862 as_warn (_("scale factor of %d without an index register"),
24eab124 10863 1 << i.log2_scale_factor);
252b5132 10864 i.log2_scale_factor = 0;
252b5132 10865 }
551c1ca1
AM
10866 scale = input_line_pointer;
10867 input_line_pointer = save;
10868 return scale;
252b5132
RH
10869}
10870
252b5132 10871static int
e3bb37b5 10872i386_displacement (char *disp_start, char *disp_end)
252b5132 10873{
29b0f896 10874 expressionS *exp;
252b5132
RH
10875 segT exp_seg = 0;
10876 char *save_input_line_pointer;
f3c180ae 10877 char *gotfree_input_line;
40fb9820
L
10878 int override;
10879 i386_operand_type bigdisp, types = anydisp;
3992d3b7 10880 int ret;
252b5132 10881
31b2323c
L
10882 if (i.disp_operands == MAX_MEMORY_OPERANDS)
10883 {
10884 as_bad (_("at most %d displacement operands are allowed"),
10885 MAX_MEMORY_OPERANDS);
10886 return 0;
10887 }
10888
0dfbf9d7 10889 operand_type_set (&bigdisp, 0);
6f2f06be 10890 if (i.jumpabsolute
48bcea9f 10891 || i.types[this_operand].bitfield.baseindex
0cfa3eb3
JB
10892 || (current_templates->start->opcode_modifier.jump != JUMP
10893 && current_templates->start->opcode_modifier.jump != JUMP_DWORD))
e05278af 10894 {
48bcea9f 10895 i386_addressing_mode ();
e05278af 10896 override = (i.prefix[ADDR_PREFIX] != 0);
40fb9820
L
10897 if (flag_code == CODE_64BIT)
10898 {
a775efc8 10899 bigdisp.bitfield.disp32 = 1;
40fb9820 10900 if (!override)
a775efc8 10901 bigdisp.bitfield.disp64 = 1;
40fb9820
L
10902 }
10903 else if ((flag_code == CODE_16BIT) ^ override)
40fb9820 10904 bigdisp.bitfield.disp16 = 1;
48bcea9f
JB
10905 else
10906 bigdisp.bitfield.disp32 = 1;
e05278af
JB
10907 }
10908 else
10909 {
376cd056
JB
10910 /* For PC-relative branches, the width of the displacement may be
10911 dependent upon data size, but is never dependent upon address size.
10912 Also make sure to not unintentionally match against a non-PC-relative
10913 branch template. */
10914 static templates aux_templates;
10915 const insn_template *t = current_templates->start;
5b7c81bd 10916 bool has_intel64 = false;
376cd056
JB
10917
10918 aux_templates.start = t;
10919 while (++t < current_templates->end)
10920 {
10921 if (t->opcode_modifier.jump
10922 != current_templates->start->opcode_modifier.jump)
10923 break;
4b5aaf5f 10924 if ((t->opcode_modifier.isa64 >= INTEL64))
5b7c81bd 10925 has_intel64 = true;
376cd056
JB
10926 }
10927 if (t < current_templates->end)
10928 {
10929 aux_templates.end = t;
10930 current_templates = &aux_templates;
10931 }
10932
e05278af 10933 override = (i.prefix[DATA_PREFIX] != 0);
40fb9820
L
10934 if (flag_code == CODE_64BIT)
10935 {
376cd056
JB
10936 if ((override || i.suffix == WORD_MNEM_SUFFIX)
10937 && (!intel64 || !has_intel64))
40fb9820
L
10938 bigdisp.bitfield.disp16 = 1;
10939 else
a775efc8 10940 bigdisp.bitfield.disp32 = 1;
40fb9820
L
10941 }
10942 else
e05278af
JB
10943 {
10944 if (!override)
10945 override = (i.suffix == (flag_code != CODE_16BIT
10946 ? WORD_MNEM_SUFFIX
10947 : LONG_MNEM_SUFFIX));
40fb9820
L
10948 bigdisp.bitfield.disp32 = 1;
10949 if ((flag_code == CODE_16BIT) ^ override)
10950 {
10951 bigdisp.bitfield.disp32 = 0;
10952 bigdisp.bitfield.disp16 = 1;
10953 }
e05278af 10954 }
e05278af 10955 }
c6fb90c8
L
10956 i.types[this_operand] = operand_type_or (i.types[this_operand],
10957 bigdisp);
252b5132
RH
10958
10959 exp = &disp_expressions[i.disp_operands];
520dc8e8 10960 i.op[this_operand].disps = exp;
252b5132
RH
10961 i.disp_operands++;
10962 save_input_line_pointer = input_line_pointer;
10963 input_line_pointer = disp_start;
10964 END_STRING_AND_SAVE (disp_end);
10965
10966#ifndef GCC_ASM_O_HACK
10967#define GCC_ASM_O_HACK 0
10968#endif
10969#if GCC_ASM_O_HACK
10970 END_STRING_AND_SAVE (disp_end + 1);
40fb9820 10971 if (i.types[this_operand].bitfield.baseIndex
24eab124 10972 && displacement_string_end[-1] == '+')
252b5132
RH
10973 {
10974 /* This hack is to avoid a warning when using the "o"
24eab124
AM
10975 constraint within gcc asm statements.
10976 For instance:
10977
10978 #define _set_tssldt_desc(n,addr,limit,type) \
10979 __asm__ __volatile__ ( \
10980 "movw %w2,%0\n\t" \
10981 "movw %w1,2+%0\n\t" \
10982 "rorl $16,%1\n\t" \
10983 "movb %b1,4+%0\n\t" \
10984 "movb %4,5+%0\n\t" \
10985 "movb $0,6+%0\n\t" \
10986 "movb %h1,7+%0\n\t" \
10987 "rorl $16,%1" \
10988 : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
10989
10990 This works great except that the output assembler ends
10991 up looking a bit weird if it turns out that there is
10992 no offset. You end up producing code that looks like:
10993
10994 #APP
10995 movw $235,(%eax)
10996 movw %dx,2+(%eax)
10997 rorl $16,%edx
10998 movb %dl,4+(%eax)
10999 movb $137,5+(%eax)
11000 movb $0,6+(%eax)
11001 movb %dh,7+(%eax)
11002 rorl $16,%edx
11003 #NO_APP
11004
47926f60 11005 So here we provide the missing zero. */
24eab124
AM
11006
11007 *displacement_string_end = '0';
252b5132
RH
11008 }
11009#endif
d258b828 11010 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
11011 if (gotfree_input_line)
11012 input_line_pointer = gotfree_input_line;
252b5132 11013
24eab124 11014 exp_seg = expression (exp);
252b5132 11015
636c26b0
AM
11016 SKIP_WHITESPACE ();
11017 if (*input_line_pointer)
11018 as_bad (_("junk `%s' after expression"), input_line_pointer);
11019#if GCC_ASM_O_HACK
11020 RESTORE_END_STRING (disp_end + 1);
11021#endif
636c26b0 11022 input_line_pointer = save_input_line_pointer;
636c26b0 11023 if (gotfree_input_line)
ee86248c
JB
11024 {
11025 free (gotfree_input_line);
11026
11027 if (exp->X_op == O_constant || exp->X_op == O_register)
11028 exp->X_op = O_illegal;
11029 }
11030
11031 ret = i386_finalize_displacement (exp_seg, exp, types, disp_start);
11032
11033 RESTORE_END_STRING (disp_end);
11034
11035 return ret;
11036}
11037
11038static int
11039i386_finalize_displacement (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
11040 i386_operand_type types, const char *disp_start)
11041{
ee86248c 11042 int ret = 1;
636c26b0 11043
24eab124
AM
11044 /* We do this to make sure that the section symbol is in
11045 the symbol table. We will ultimately change the relocation
47926f60 11046 to be relative to the beginning of the section. */
1ae12ab7 11047 if (i.reloc[this_operand] == BFD_RELOC_386_GOTOFF
d6ab8113
JB
11048 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
11049 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
24eab124 11050 {
636c26b0 11051 if (exp->X_op != O_symbol)
3992d3b7 11052 goto inv_disp;
636c26b0 11053
e5cb08ac 11054 if (S_IS_LOCAL (exp->X_add_symbol)
c64efb4b
L
11055 && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section
11056 && S_GET_SEGMENT (exp->X_add_symbol) != expr_section)
24eab124 11057 section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
24eab124
AM
11058 exp->X_op = O_subtract;
11059 exp->X_op_symbol = GOT_symbol;
1ae12ab7 11060 if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
29b0f896 11061 i.reloc[this_operand] = BFD_RELOC_32_PCREL;
d6ab8113
JB
11062 else if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
11063 i.reloc[this_operand] = BFD_RELOC_64;
23df1078 11064 else
29b0f896 11065 i.reloc[this_operand] = BFD_RELOC_32;
24eab124 11066 }
252b5132 11067
3992d3b7
AM
11068 else if (exp->X_op == O_absent
11069 || exp->X_op == O_illegal
ee86248c 11070 || exp->X_op == O_big)
2daf4fd8 11071 {
3992d3b7
AM
11072 inv_disp:
11073 as_bad (_("missing or invalid displacement expression `%s'"),
2daf4fd8 11074 disp_start);
3992d3b7 11075 ret = 0;
2daf4fd8
AM
11076 }
11077
a50187b2
JB
11078 else if (exp->X_op == O_constant)
11079 {
11080 /* Sizing gets taken care of by optimize_disp().
11081
11082 If not 64bit, sign/zero extend val, to account for wraparound
11083 when !BFD64. */
11084 if (flag_code != CODE_64BIT)
11085 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
11086 }
11087
4c63da97 11088#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
a50187b2 11089 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
3992d3b7
AM
11090 && exp_seg != absolute_section
11091 && exp_seg != text_section
11092 && exp_seg != data_section
11093 && exp_seg != bss_section
11094 && exp_seg != undefined_section
11095 && !bfd_is_com_section (exp_seg))
24eab124 11096 {
d0b47220 11097 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
3992d3b7 11098 ret = 0;
24eab124 11099 }
252b5132 11100#endif
3956db08 11101
a50187b2 11102 else if (current_templates->start->opcode_modifier.jump == JUMP_BYTE)
48bcea9f
JB
11103 i.types[this_operand].bitfield.disp8 = 1;
11104
40fb9820 11105 /* Check if this is a displacement only operand. */
02b83698
JB
11106 if (!i.types[this_operand].bitfield.baseindex)
11107 i.types[this_operand] =
11108 operand_type_or (operand_type_and_not (i.types[this_operand], anydisp),
11109 operand_type_and (i.types[this_operand], types));
3956db08 11110
3992d3b7 11111 return ret;
252b5132
RH
11112}
11113
2abc2bec
JB
11114/* Return the active addressing mode, taking address override and
11115 registers forming the address into consideration. Update the
11116 address override prefix if necessary. */
47926f60 11117
2abc2bec
JB
11118static enum flag_code
11119i386_addressing_mode (void)
252b5132 11120{
be05d201
L
11121 enum flag_code addr_mode;
11122
11123 if (i.prefix[ADDR_PREFIX])
11124 addr_mode = flag_code == CODE_32BIT ? CODE_16BIT : CODE_32BIT;
a23b33b3
JB
11125 else if (flag_code == CODE_16BIT
11126 && current_templates->start->cpu_flags.bitfield.cpumpx
11127 /* Avoid replacing the "16-bit addressing not allowed" diagnostic
11128 from md_assemble() by "is not a valid base/index expression"
11129 when there is a base and/or index. */
11130 && !i.types[this_operand].bitfield.baseindex)
11131 {
11132 /* MPX insn memory operands with neither base nor index must be forced
11133 to use 32-bit addressing in 16-bit mode. */
11134 addr_mode = CODE_32BIT;
11135 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
11136 ++i.prefixes;
11137 gas_assert (!i.types[this_operand].bitfield.disp16);
11138 gas_assert (!i.types[this_operand].bitfield.disp32);
11139 }
be05d201
L
11140 else
11141 {
11142 addr_mode = flag_code;
11143
24eab124 11144#if INFER_ADDR_PREFIX
be05d201
L
11145 if (i.mem_operands == 0)
11146 {
11147 /* Infer address prefix from the first memory operand. */
11148 const reg_entry *addr_reg = i.base_reg;
11149
11150 if (addr_reg == NULL)
11151 addr_reg = i.index_reg;
eecb386c 11152
be05d201
L
11153 if (addr_reg)
11154 {
e968fc9b 11155 if (addr_reg->reg_type.bitfield.dword)
be05d201
L
11156 addr_mode = CODE_32BIT;
11157 else if (flag_code != CODE_64BIT
dc821c5f 11158 && addr_reg->reg_type.bitfield.word)
be05d201
L
11159 addr_mode = CODE_16BIT;
11160
11161 if (addr_mode != flag_code)
11162 {
11163 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
11164 i.prefixes += 1;
11165 /* Change the size of any displacement too. At most one
11166 of Disp16 or Disp32 is set.
11167 FIXME. There doesn't seem to be any real need for
11168 separate Disp16 and Disp32 flags. The same goes for
11169 Imm16 and Imm32. Removing them would probably clean
11170 up the code quite a lot. */
11171 if (flag_code != CODE_64BIT
11172 && (i.types[this_operand].bitfield.disp16
11173 || i.types[this_operand].bitfield.disp32))
11174 i.types[this_operand]
11175 = operand_type_xor (i.types[this_operand], disp16_32);
11176 }
11177 }
11178 }
24eab124 11179#endif
be05d201
L
11180 }
11181
2abc2bec
JB
11182 return addr_mode;
11183}
11184
11185/* Make sure the memory operand we've been dealt is valid.
11186 Return 1 on success, 0 on a failure. */
11187
11188static int
11189i386_index_check (const char *operand_string)
11190{
11191 const char *kind = "base/index";
11192 enum flag_code addr_mode = i386_addressing_mode ();
a152332d 11193 const insn_template *t = current_templates->start;
2abc2bec 11194
a152332d
JB
11195 if (t->opcode_modifier.isstring
11196 && !t->cpu_flags.bitfield.cpupadlock
fc0763e6
JB
11197 && (current_templates->end[-1].opcode_modifier.isstring
11198 || i.mem_operands))
11199 {
11200 /* Memory operands of string insns are special in that they only allow
11201 a single register (rDI, rSI, or rBX) as their memory address. */
be05d201
L
11202 const reg_entry *expected_reg;
11203 static const char *di_si[][2] =
11204 {
11205 { "esi", "edi" },
11206 { "si", "di" },
11207 { "rsi", "rdi" }
11208 };
11209 static const char *bx[] = { "ebx", "bx", "rbx" };
fc0763e6
JB
11210
11211 kind = "string address";
11212
a152332d 11213 if (t->opcode_modifier.prefixok == PrefixRep)
fc0763e6 11214 {
51c8edf6
JB
11215 int es_op = current_templates->end[-1].opcode_modifier.isstring
11216 - IS_STRING_ES_OP0;
11217 int op = 0;
fc0763e6 11218
51c8edf6 11219 if (!current_templates->end[-1].operand_types[0].bitfield.baseindex
fc0763e6
JB
11220 || ((!i.mem_operands != !intel_syntax)
11221 && current_templates->end[-1].operand_types[1]
11222 .bitfield.baseindex))
51c8edf6 11223 op = 1;
fe0e921f
AM
11224 expected_reg
11225 = (const reg_entry *) str_hash_find (reg_hash,
11226 di_si[addr_mode][op == es_op]);
fc0763e6
JB
11227 }
11228 else
fe0e921f
AM
11229 expected_reg
11230 = (const reg_entry *)str_hash_find (reg_hash, bx[addr_mode]);
fc0763e6 11231
be05d201
L
11232 if (i.base_reg != expected_reg
11233 || i.index_reg
fc0763e6 11234 || operand_type_check (i.types[this_operand], disp))
fc0763e6 11235 {
be05d201
L
11236 /* The second memory operand must have the same size as
11237 the first one. */
11238 if (i.mem_operands
11239 && i.base_reg
11240 && !((addr_mode == CODE_64BIT
dc821c5f 11241 && i.base_reg->reg_type.bitfield.qword)
be05d201 11242 || (addr_mode == CODE_32BIT
dc821c5f
JB
11243 ? i.base_reg->reg_type.bitfield.dword
11244 : i.base_reg->reg_type.bitfield.word)))
be05d201
L
11245 goto bad_address;
11246
fc0763e6
JB
11247 as_warn (_("`%s' is not valid here (expected `%c%s%s%c')"),
11248 operand_string,
11249 intel_syntax ? '[' : '(',
11250 register_prefix,
be05d201 11251 expected_reg->reg_name,
fc0763e6 11252 intel_syntax ? ']' : ')');
be05d201 11253 return 1;
fc0763e6 11254 }
be05d201
L
11255 else
11256 return 1;
11257
dc1e8a47 11258 bad_address:
be05d201
L
11259 as_bad (_("`%s' is not a valid %s expression"),
11260 operand_string, kind);
11261 return 0;
3e73aa7c
JH
11262 }
11263 else
11264 {
be05d201
L
11265 if (addr_mode != CODE_16BIT)
11266 {
11267 /* 32-bit/64-bit checks. */
41eb8e88
L
11268 if (i.disp_encoding == disp_encoding_16bit)
11269 {
11270 bad_disp:
11271 as_bad (_("invalid `%s' prefix"),
11272 addr_mode == CODE_16BIT ? "{disp32}" : "{disp16}");
11273 return 0;
11274 }
11275
be05d201 11276 if ((i.base_reg
e968fc9b
JB
11277 && ((addr_mode == CODE_64BIT
11278 ? !i.base_reg->reg_type.bitfield.qword
11279 : !i.base_reg->reg_type.bitfield.dword)
11280 || (i.index_reg && i.base_reg->reg_num == RegIP)
11281 || i.base_reg->reg_num == RegIZ))
be05d201 11282 || (i.index_reg
1b54b8d7
JB
11283 && !i.index_reg->reg_type.bitfield.xmmword
11284 && !i.index_reg->reg_type.bitfield.ymmword
11285 && !i.index_reg->reg_type.bitfield.zmmword
be05d201 11286 && ((addr_mode == CODE_64BIT
e968fc9b
JB
11287 ? !i.index_reg->reg_type.bitfield.qword
11288 : !i.index_reg->reg_type.bitfield.dword)
be05d201
L
11289 || !i.index_reg->reg_type.bitfield.baseindex)))
11290 goto bad_address;
8178be5b 11291
260cd341 11292 /* bndmk, bndldx, bndstx and mandatory non-vector SIB have special restrictions. */
a152332d 11293 if ((t->opcode_modifier.opcodeprefix == PREFIX_0XF3
389d00a5
JB
11294 && t->opcode_modifier.opcodespace == SPACE_0F
11295 && t->base_opcode == 0x1b)
a152332d 11296 || (t->opcode_modifier.opcodeprefix == PREFIX_NONE
389d00a5
JB
11297 && t->opcode_modifier.opcodespace == SPACE_0F
11298 && (t->base_opcode & ~1) == 0x1a)
a152332d 11299 || t->opcode_modifier.sib == SIBMEM)
8178be5b
JB
11300 {
11301 /* They cannot use RIP-relative addressing. */
e968fc9b 11302 if (i.base_reg && i.base_reg->reg_num == RegIP)
8178be5b
JB
11303 {
11304 as_bad (_("`%s' cannot be used here"), operand_string);
11305 return 0;
11306 }
11307
11308 /* bndldx and bndstx ignore their scale factor. */
a152332d 11309 if (t->opcode_modifier.opcodeprefix == PREFIX_NONE
389d00a5
JB
11310 && t->opcode_modifier.opcodespace == SPACE_0F
11311 && (t->base_opcode & ~1) == 0x1a
8178be5b
JB
11312 && i.log2_scale_factor)
11313 as_warn (_("register scaling is being ignored here"));
11314 }
be05d201
L
11315 }
11316 else
3e73aa7c 11317 {
be05d201 11318 /* 16-bit checks. */
41eb8e88
L
11319 if (i.disp_encoding == disp_encoding_32bit)
11320 goto bad_disp;
11321
3e73aa7c 11322 if ((i.base_reg
dc821c5f 11323 && (!i.base_reg->reg_type.bitfield.word
40fb9820 11324 || !i.base_reg->reg_type.bitfield.baseindex))
3e73aa7c 11325 || (i.index_reg
dc821c5f 11326 && (!i.index_reg->reg_type.bitfield.word
40fb9820 11327 || !i.index_reg->reg_type.bitfield.baseindex
29b0f896
AM
11328 || !(i.base_reg
11329 && i.base_reg->reg_num < 6
11330 && i.index_reg->reg_num >= 6
11331 && i.log2_scale_factor == 0))))
be05d201 11332 goto bad_address;
3e73aa7c
JH
11333 }
11334 }
be05d201 11335 return 1;
24eab124 11336}
252b5132 11337
43234a1e
L
11338/* Handle vector immediates. */
11339
11340static int
11341RC_SAE_immediate (const char *imm_start)
11342{
43234a1e 11343 const char *pstr = imm_start;
43234a1e
L
11344
11345 if (*pstr != '{')
11346 return 0;
11347
7063667e
JB
11348 pstr = RC_SAE_specifier (pstr + 1);
11349 if (pstr == NULL)
43234a1e
L
11350 return 0;
11351
11352 if (*pstr++ != '}')
11353 {
11354 as_bad (_("Missing '}': '%s'"), imm_start);
11355 return 0;
11356 }
11357 /* RC/SAE immediate string should contain nothing more. */;
11358 if (*pstr != 0)
11359 {
11360 as_bad (_("Junk after '}': '%s'"), imm_start);
11361 return 0;
11362 }
11363
cf665fee
JB
11364 /* Internally this doesn't count as an operand. */
11365 --i.operands;
43234a1e 11366
43234a1e
L
11367 return 1;
11368}
11369
8325cc63
JB
11370/* Only string instructions can have a second memory operand, so
11371 reduce current_templates to just those if it contains any. */
11372static int
11373maybe_adjust_templates (void)
11374{
11375 const insn_template *t;
11376
11377 gas_assert (i.mem_operands == 1);
11378
11379 for (t = current_templates->start; t < current_templates->end; ++t)
11380 if (t->opcode_modifier.isstring)
11381 break;
11382
11383 if (t < current_templates->end)
11384 {
11385 static templates aux_templates;
5b7c81bd 11386 bool recheck;
8325cc63
JB
11387
11388 aux_templates.start = t;
11389 for (; t < current_templates->end; ++t)
11390 if (!t->opcode_modifier.isstring)
11391 break;
11392 aux_templates.end = t;
11393
11394 /* Determine whether to re-check the first memory operand. */
11395 recheck = (aux_templates.start != current_templates->start
11396 || t != current_templates->end);
11397
11398 current_templates = &aux_templates;
11399
11400 if (recheck)
11401 {
11402 i.mem_operands = 0;
11403 if (i.memop1_string != NULL
11404 && i386_index_check (i.memop1_string) == 0)
11405 return 0;
11406 i.mem_operands = 1;
11407 }
11408 }
11409
11410 return 1;
11411}
11412
9d299bea
JB
11413static INLINE bool starts_memory_operand (char c)
11414{
014fbcda 11415 return ISDIGIT (c)
9d299bea 11416 || is_identifier_char (c)
014fbcda 11417 || strchr ("([\"+-!~", c);
9d299bea
JB
11418}
11419
fc0763e6 11420/* Parse OPERAND_STRING into the i386_insn structure I. Returns zero
47926f60 11421 on error. */
252b5132 11422
252b5132 11423static int
a7619375 11424i386_att_operand (char *operand_string)
252b5132 11425{
af6bdddf
AM
11426 const reg_entry *r;
11427 char *end_op;
24eab124 11428 char *op_string = operand_string;
252b5132 11429
24eab124 11430 if (is_space_char (*op_string))
252b5132
RH
11431 ++op_string;
11432
24eab124 11433 /* We check for an absolute prefix (differentiating,
47926f60 11434 for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
24eab124
AM
11435 if (*op_string == ABSOLUTE_PREFIX)
11436 {
11437 ++op_string;
11438 if (is_space_char (*op_string))
11439 ++op_string;
5b7c81bd 11440 i.jumpabsolute = true;
24eab124 11441 }
252b5132 11442
47926f60 11443 /* Check if operand is a register. */
4d1bb795 11444 if ((r = parse_register (op_string, &end_op)) != NULL)
24eab124 11445 {
40fb9820
L
11446 i386_operand_type temp;
11447
8a6fb3f9
JB
11448 if (r == &bad_reg)
11449 return 0;
11450
24eab124
AM
11451 /* Check for a segment override by searching for ':' after a
11452 segment register. */
11453 op_string = end_op;
11454 if (is_space_char (*op_string))
11455 ++op_string;
00cee14f 11456 if (*op_string == ':' && r->reg_type.bitfield.class == SReg)
24eab124 11457 {
5e042380 11458 i.seg[i.mem_operands] = r;
252b5132 11459
24eab124 11460 /* Skip the ':' and whitespace. */
252b5132
RH
11461 ++op_string;
11462 if (is_space_char (*op_string))
24eab124 11463 ++op_string;
252b5132 11464
47926f60 11465 /* Handle case of %es:*foo. */
c8d541e2 11466 if (!i.jumpabsolute && *op_string == ABSOLUTE_PREFIX)
24eab124
AM
11467 {
11468 ++op_string;
11469 if (is_space_char (*op_string))
11470 ++op_string;
5b7c81bd 11471 i.jumpabsolute = true;
24eab124 11472 }
c8d541e2 11473
9d299bea 11474 if (!starts_memory_operand (*op_string))
c8d541e2
JB
11475 {
11476 as_bad (_("bad memory operand `%s'"), op_string);
11477 return 0;
11478 }
24eab124
AM
11479 goto do_memory_reference;
11480 }
43234a1e
L
11481
11482 /* Handle vector operations. */
11483 if (*op_string == '{')
11484 {
f70c6814 11485 op_string = check_VecOperations (op_string);
43234a1e
L
11486 if (op_string == NULL)
11487 return 0;
11488 }
11489
24eab124
AM
11490 if (*op_string)
11491 {
d0b47220 11492 as_bad (_("junk `%s' after register"), op_string);
24eab124
AM
11493 return 0;
11494 }
40fb9820
L
11495 temp = r->reg_type;
11496 temp.bitfield.baseindex = 0;
c6fb90c8
L
11497 i.types[this_operand] = operand_type_or (i.types[this_operand],
11498 temp);
7d5e4556 11499 i.types[this_operand].bitfield.unspecified = 0;
520dc8e8 11500 i.op[this_operand].regs = r;
24eab124 11501 i.reg_operands++;
cf665fee
JB
11502
11503 /* A GPR may follow an RC or SAE immediate only if a (vector) register
11504 operand was also present earlier on. */
11505 if (i.rounding.type != rc_none && temp.bitfield.class == Reg
11506 && i.reg_operands == 1)
11507 {
11508 unsigned int j;
11509
11510 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); ++j)
11511 if (i.rounding.type == RC_NamesTable[j].type)
11512 break;
11513 as_bad (_("`%s': misplaced `{%s}'"),
11514 current_templates->start->name, RC_NamesTable[j].name);
11515 return 0;
11516 }
24eab124 11517 }
af6bdddf
AM
11518 else if (*op_string == REGISTER_PREFIX)
11519 {
11520 as_bad (_("bad register name `%s'"), op_string);
11521 return 0;
11522 }
24eab124 11523 else if (*op_string == IMMEDIATE_PREFIX)
ce8a8b2f 11524 {
24eab124 11525 ++op_string;
6f2f06be 11526 if (i.jumpabsolute)
24eab124 11527 {
d0b47220 11528 as_bad (_("immediate operand illegal with absolute jump"));
24eab124
AM
11529 return 0;
11530 }
11531 if (!i386_immediate (op_string))
11532 return 0;
cf665fee
JB
11533 if (i.rounding.type != rc_none)
11534 {
11535 as_bad (_("`%s': RC/SAE operand must follow immediate operands"),
11536 current_templates->start->name);
11537 return 0;
11538 }
24eab124 11539 }
43234a1e
L
11540 else if (RC_SAE_immediate (operand_string))
11541 {
cf665fee
JB
11542 /* If it is a RC or SAE immediate, do the necessary placement check:
11543 Only another immediate or a GPR may precede it. */
11544 if (i.mem_operands || i.reg_operands + i.imm_operands > 1
11545 || (i.reg_operands == 1
11546 && i.op[0].regs->reg_type.bitfield.class != Reg))
11547 {
11548 as_bad (_("`%s': misplaced `%s'"),
11549 current_templates->start->name, operand_string);
11550 return 0;
11551 }
43234a1e 11552 }
9d299bea 11553 else if (starts_memory_operand (*op_string))
24eab124 11554 {
47926f60 11555 /* This is a memory reference of some sort. */
af6bdddf 11556 char *base_string;
252b5132 11557
47926f60 11558 /* Start and end of displacement string expression (if found). */
eecb386c
AM
11559 char *displacement_string_start;
11560 char *displacement_string_end;
252b5132 11561
24eab124 11562 do_memory_reference:
8325cc63
JB
11563 if (i.mem_operands == 1 && !maybe_adjust_templates ())
11564 return 0;
24eab124 11565 if ((i.mem_operands == 1
40fb9820 11566 && !current_templates->start->opcode_modifier.isstring)
24eab124
AM
11567 || i.mem_operands == 2)
11568 {
11569 as_bad (_("too many memory references for `%s'"),
11570 current_templates->start->name);
11571 return 0;
11572 }
252b5132 11573
24eab124
AM
11574 /* Check for base index form. We detect the base index form by
11575 looking for an ')' at the end of the operand, searching
11576 for the '(' matching it, and finding a REGISTER_PREFIX or ','
11577 after the '('. */
af6bdddf 11578 base_string = op_string + strlen (op_string);
c3332e24 11579
43234a1e 11580 /* Handle vector operations. */
6b5ba0d4
JB
11581 --base_string;
11582 if (is_space_char (*base_string))
11583 --base_string;
11584
11585 if (*base_string == '}')
43234a1e 11586 {
6b5ba0d4
JB
11587 char *vop_start = NULL;
11588
11589 while (base_string-- > op_string)
11590 {
11591 if (*base_string == '"')
11592 break;
11593 if (*base_string != '{')
11594 continue;
11595
11596 vop_start = base_string;
11597
11598 --base_string;
11599 if (is_space_char (*base_string))
11600 --base_string;
11601
11602 if (*base_string != '}')
11603 break;
11604
11605 vop_start = NULL;
11606 }
11607
11608 if (!vop_start)
11609 {
11610 as_bad (_("unbalanced figure braces"));
11611 return 0;
11612 }
11613
f70c6814 11614 if (check_VecOperations (vop_start) == NULL)
43234a1e 11615 return 0;
43234a1e
L
11616 }
11617
47926f60 11618 /* If we only have a displacement, set-up for it to be parsed later. */
af6bdddf
AM
11619 displacement_string_start = op_string;
11620 displacement_string_end = base_string + 1;
252b5132 11621
24eab124
AM
11622 if (*base_string == ')')
11623 {
af6bdddf 11624 char *temp_string;
cc0f9635 11625 unsigned int parens_not_balanced = 1;
e68c3d59 11626
24eab124 11627 /* We've already checked that the number of left & right ()'s are
47926f60 11628 equal, so this loop will not be infinite. */
24eab124
AM
11629 do
11630 {
11631 base_string--;
cc0f9635
JB
11632 if (*base_string == ')')
11633 parens_not_balanced++;
11634 if (*base_string == '(')
11635 parens_not_balanced--;
24eab124 11636 }
cc0f9635 11637 while (parens_not_balanced && *base_string != '"');
c3332e24 11638
af6bdddf 11639 temp_string = base_string;
c3332e24 11640
24eab124 11641 /* Skip past '(' and whitespace. */
e68c3d59
JB
11642 if (*base_string == '(')
11643 ++base_string;
252b5132 11644 if (is_space_char (*base_string))
24eab124 11645 ++base_string;
252b5132 11646
af6bdddf 11647 if (*base_string == ','
4eed87de
AM
11648 || ((i.base_reg = parse_register (base_string, &end_op))
11649 != NULL))
252b5132 11650 {
af6bdddf 11651 displacement_string_end = temp_string;
252b5132 11652
40fb9820 11653 i.types[this_operand].bitfield.baseindex = 1;
252b5132 11654
af6bdddf 11655 if (i.base_reg)
24eab124 11656 {
8a6fb3f9
JB
11657 if (i.base_reg == &bad_reg)
11658 return 0;
24eab124
AM
11659 base_string = end_op;
11660 if (is_space_char (*base_string))
11661 ++base_string;
af6bdddf
AM
11662 }
11663
11664 /* There may be an index reg or scale factor here. */
11665 if (*base_string == ',')
11666 {
11667 ++base_string;
11668 if (is_space_char (*base_string))
11669 ++base_string;
11670
4eed87de
AM
11671 if ((i.index_reg = parse_register (base_string, &end_op))
11672 != NULL)
24eab124 11673 {
8a6fb3f9
JB
11674 if (i.index_reg == &bad_reg)
11675 return 0;
af6bdddf 11676 base_string = end_op;
24eab124
AM
11677 if (is_space_char (*base_string))
11678 ++base_string;
af6bdddf
AM
11679 if (*base_string == ',')
11680 {
11681 ++base_string;
11682 if (is_space_char (*base_string))
11683 ++base_string;
11684 }
e5cb08ac 11685 else if (*base_string != ')')
af6bdddf 11686 {
4eed87de
AM
11687 as_bad (_("expecting `,' or `)' "
11688 "after index register in `%s'"),
af6bdddf
AM
11689 operand_string);
11690 return 0;
11691 }
24eab124 11692 }
af6bdddf 11693 else if (*base_string == REGISTER_PREFIX)
24eab124 11694 {
f76bf5e0
L
11695 end_op = strchr (base_string, ',');
11696 if (end_op)
11697 *end_op = '\0';
af6bdddf 11698 as_bad (_("bad register name `%s'"), base_string);
24eab124
AM
11699 return 0;
11700 }
252b5132 11701
47926f60 11702 /* Check for scale factor. */
551c1ca1 11703 if (*base_string != ')')
af6bdddf 11704 {
551c1ca1
AM
11705 char *end_scale = i386_scale (base_string);
11706
11707 if (!end_scale)
af6bdddf 11708 return 0;
24eab124 11709
551c1ca1 11710 base_string = end_scale;
af6bdddf
AM
11711 if (is_space_char (*base_string))
11712 ++base_string;
11713 if (*base_string != ')')
11714 {
4eed87de
AM
11715 as_bad (_("expecting `)' "
11716 "after scale factor in `%s'"),
af6bdddf
AM
11717 operand_string);
11718 return 0;
11719 }
11720 }
11721 else if (!i.index_reg)
24eab124 11722 {
4eed87de
AM
11723 as_bad (_("expecting index register or scale factor "
11724 "after `,'; got '%c'"),
af6bdddf 11725 *base_string);
24eab124
AM
11726 return 0;
11727 }
11728 }
af6bdddf 11729 else if (*base_string != ')')
24eab124 11730 {
4eed87de
AM
11731 as_bad (_("expecting `,' or `)' "
11732 "after base register in `%s'"),
af6bdddf 11733 operand_string);
24eab124
AM
11734 return 0;
11735 }
c3332e24 11736 }
af6bdddf 11737 else if (*base_string == REGISTER_PREFIX)
c3332e24 11738 {
f76bf5e0
L
11739 end_op = strchr (base_string, ',');
11740 if (end_op)
11741 *end_op = '\0';
af6bdddf 11742 as_bad (_("bad register name `%s'"), base_string);
24eab124 11743 return 0;
c3332e24 11744 }
24eab124
AM
11745 }
11746
11747 /* If there's an expression beginning the operand, parse it,
11748 assuming displacement_string_start and
11749 displacement_string_end are meaningful. */
11750 if (displacement_string_start != displacement_string_end)
11751 {
11752 if (!i386_displacement (displacement_string_start,
11753 displacement_string_end))
11754 return 0;
11755 }
11756
11757 /* Special case for (%dx) while doing input/output op. */
11758 if (i.base_reg
75e5731b
JB
11759 && i.base_reg->reg_type.bitfield.instance == RegD
11760 && i.base_reg->reg_type.bitfield.word
24eab124
AM
11761 && i.index_reg == 0
11762 && i.log2_scale_factor == 0
11763 && i.seg[i.mem_operands] == 0
40fb9820 11764 && !operand_type_check (i.types[this_operand], disp))
24eab124 11765 {
2fb5be8d 11766 i.types[this_operand] = i.base_reg->reg_type;
24eab124
AM
11767 return 1;
11768 }
11769
eecb386c
AM
11770 if (i386_index_check (operand_string) == 0)
11771 return 0;
c48dadc9 11772 i.flags[this_operand] |= Operand_Mem;
8325cc63
JB
11773 if (i.mem_operands == 0)
11774 i.memop1_string = xstrdup (operand_string);
24eab124
AM
11775 i.mem_operands++;
11776 }
11777 else
ce8a8b2f
AM
11778 {
11779 /* It's not a memory operand; argh! */
24eab124
AM
11780 as_bad (_("invalid char %s beginning operand %d `%s'"),
11781 output_invalid (*op_string),
11782 this_operand + 1,
11783 op_string);
11784 return 0;
11785 }
47926f60 11786 return 1; /* Normal return. */
252b5132
RH
11787}
11788\f
fa94de6b
RM
11789/* Calculate the maximum variable size (i.e., excluding fr_fix)
11790 that an rs_machine_dependent frag may reach. */
11791
11792unsigned int
11793i386_frag_max_var (fragS *frag)
11794{
11795 /* The only relaxable frags are for jumps.
11796 Unconditional jumps can grow by 4 bytes and others by 5 bytes. */
11797 gas_assert (frag->fr_type == rs_machine_dependent);
11798 return TYPE_FROM_RELAX_STATE (frag->fr_subtype) == UNCOND_JUMP ? 4 : 5;
11799}
11800
b084df0b
L
11801#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
11802static int
8dcea932 11803elf_symbol_resolved_in_segment_p (symbolS *fr_symbol, offsetT fr_var)
b084df0b
L
11804{
11805 /* STT_GNU_IFUNC symbol must go through PLT. */
11806 if ((symbol_get_bfdsym (fr_symbol)->flags
11807 & BSF_GNU_INDIRECT_FUNCTION) != 0)
11808 return 0;
11809
11810 if (!S_IS_EXTERNAL (fr_symbol))
11811 /* Symbol may be weak or local. */
11812 return !S_IS_WEAK (fr_symbol);
11813
8dcea932
L
11814 /* Global symbols with non-default visibility can't be preempted. */
11815 if (ELF_ST_VISIBILITY (S_GET_OTHER (fr_symbol)) != STV_DEFAULT)
11816 return 1;
11817
11818 if (fr_var != NO_RELOC)
11819 switch ((enum bfd_reloc_code_real) fr_var)
11820 {
11821 case BFD_RELOC_386_PLT32:
11822 case BFD_RELOC_X86_64_PLT32:
33eaf5de 11823 /* Symbol with PLT relocation may be preempted. */
8dcea932
L
11824 return 0;
11825 default:
11826 abort ();
11827 }
11828
b084df0b
L
11829 /* Global symbols with default visibility in a shared library may be
11830 preempted by another definition. */
8dcea932 11831 return !shared;
b084df0b
L
11832}
11833#endif
11834
79d72f45
HL
11835/* Table 3-2. Macro-Fusible Instructions in Haswell Microarchitecture
11836 Note also work for Skylake and Cascadelake.
11837---------------------------------------------------------------------
11838| JCC | ADD/SUB/CMP | INC/DEC | TEST/AND |
11839| ------ | ----------- | ------- | -------- |
11840| Jo | N | N | Y |
11841| Jno | N | N | Y |
11842| Jc/Jb | Y | N | Y |
11843| Jae/Jnb | Y | N | Y |
11844| Je/Jz | Y | Y | Y |
11845| Jne/Jnz | Y | Y | Y |
11846| Jna/Jbe | Y | N | Y |
11847| Ja/Jnbe | Y | N | Y |
11848| Js | N | N | Y |
11849| Jns | N | N | Y |
11850| Jp/Jpe | N | N | Y |
11851| Jnp/Jpo | N | N | Y |
11852| Jl/Jnge | Y | Y | Y |
11853| Jge/Jnl | Y | Y | Y |
11854| Jle/Jng | Y | Y | Y |
11855| Jg/Jnle | Y | Y | Y |
11856--------------------------------------------------------------------- */
11857static int
11858i386_macro_fusible_p (enum mf_cmp_kind mf_cmp, enum mf_jcc_kind mf_jcc)
11859{
11860 if (mf_cmp == mf_cmp_alu_cmp)
11861 return ((mf_jcc >= mf_jcc_jc && mf_jcc <= mf_jcc_jna)
11862 || mf_jcc == mf_jcc_jl || mf_jcc == mf_jcc_jle);
11863 if (mf_cmp == mf_cmp_incdec)
11864 return (mf_jcc == mf_jcc_je || mf_jcc == mf_jcc_jl
11865 || mf_jcc == mf_jcc_jle);
11866 if (mf_cmp == mf_cmp_test_and)
11867 return 1;
11868 return 0;
11869}
11870
e379e5f3
L
11871/* Return the next non-empty frag. */
11872
11873static fragS *
11874i386_next_non_empty_frag (fragS *fragP)
11875{
11876 /* There may be a frag with a ".fill 0" when there is no room in
11877 the current frag for frag_grow in output_insn. */
11878 for (fragP = fragP->fr_next;
11879 (fragP != NULL
11880 && fragP->fr_type == rs_fill
11881 && fragP->fr_fix == 0);
11882 fragP = fragP->fr_next)
11883 ;
11884 return fragP;
11885}
11886
11887/* Return the next jcc frag after BRANCH_PADDING. */
11888
11889static fragS *
79d72f45 11890i386_next_fusible_jcc_frag (fragS *maybe_cmp_fragP, fragS *pad_fragP)
e379e5f3 11891{
79d72f45
HL
11892 fragS *branch_fragP;
11893 if (!pad_fragP)
e379e5f3
L
11894 return NULL;
11895
79d72f45
HL
11896 if (pad_fragP->fr_type == rs_machine_dependent
11897 && (TYPE_FROM_RELAX_STATE (pad_fragP->fr_subtype)
e379e5f3
L
11898 == BRANCH_PADDING))
11899 {
79d72f45
HL
11900 branch_fragP = i386_next_non_empty_frag (pad_fragP);
11901 if (branch_fragP->fr_type != rs_machine_dependent)
e379e5f3 11902 return NULL;
79d72f45
HL
11903 if (TYPE_FROM_RELAX_STATE (branch_fragP->fr_subtype) == COND_JUMP
11904 && i386_macro_fusible_p (maybe_cmp_fragP->tc_frag_data.mf_type,
11905 pad_fragP->tc_frag_data.mf_type))
11906 return branch_fragP;
e379e5f3
L
11907 }
11908
11909 return NULL;
11910}
11911
11912/* Classify BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags. */
11913
11914static void
11915i386_classify_machine_dependent_frag (fragS *fragP)
11916{
11917 fragS *cmp_fragP;
11918 fragS *pad_fragP;
11919 fragS *branch_fragP;
11920 fragS *next_fragP;
11921 unsigned int max_prefix_length;
11922
11923 if (fragP->tc_frag_data.classified)
11924 return;
11925
11926 /* First scan for BRANCH_PADDING and FUSED_JCC_PADDING. Convert
11927 FUSED_JCC_PADDING and merge BRANCH_PADDING. */
11928 for (next_fragP = fragP;
11929 next_fragP != NULL;
11930 next_fragP = next_fragP->fr_next)
11931 {
11932 next_fragP->tc_frag_data.classified = 1;
11933 if (next_fragP->fr_type == rs_machine_dependent)
11934 switch (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype))
11935 {
11936 case BRANCH_PADDING:
11937 /* The BRANCH_PADDING frag must be followed by a branch
11938 frag. */
11939 branch_fragP = i386_next_non_empty_frag (next_fragP);
11940 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
11941 break;
11942 case FUSED_JCC_PADDING:
11943 /* Check if this is a fused jcc:
11944 FUSED_JCC_PADDING
11945 CMP like instruction
11946 BRANCH_PADDING
11947 COND_JUMP
11948 */
11949 cmp_fragP = i386_next_non_empty_frag (next_fragP);
11950 pad_fragP = i386_next_non_empty_frag (cmp_fragP);
79d72f45 11951 branch_fragP = i386_next_fusible_jcc_frag (next_fragP, pad_fragP);
e379e5f3
L
11952 if (branch_fragP)
11953 {
11954 /* The BRANCH_PADDING frag is merged with the
11955 FUSED_JCC_PADDING frag. */
11956 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
11957 /* CMP like instruction size. */
11958 next_fragP->tc_frag_data.cmp_size = cmp_fragP->fr_fix;
11959 frag_wane (pad_fragP);
11960 /* Skip to branch_fragP. */
11961 next_fragP = branch_fragP;
11962 }
11963 else if (next_fragP->tc_frag_data.max_prefix_length)
11964 {
11965 /* Turn FUSED_JCC_PADDING into BRANCH_PREFIX if it isn't
11966 a fused jcc. */
11967 next_fragP->fr_subtype
11968 = ENCODE_RELAX_STATE (BRANCH_PREFIX, 0);
11969 next_fragP->tc_frag_data.max_bytes
11970 = next_fragP->tc_frag_data.max_prefix_length;
11971 /* This will be updated in the BRANCH_PREFIX scan. */
11972 next_fragP->tc_frag_data.max_prefix_length = 0;
11973 }
11974 else
11975 frag_wane (next_fragP);
11976 break;
11977 }
11978 }
11979
11980 /* Stop if there is no BRANCH_PREFIX. */
11981 if (!align_branch_prefix_size)
11982 return;
11983
11984 /* Scan for BRANCH_PREFIX. */
11985 for (; fragP != NULL; fragP = fragP->fr_next)
11986 {
11987 if (fragP->fr_type != rs_machine_dependent
11988 || (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
11989 != BRANCH_PREFIX))
11990 continue;
11991
11992 /* Count all BRANCH_PREFIX frags before BRANCH_PADDING and
11993 COND_JUMP_PREFIX. */
11994 max_prefix_length = 0;
11995 for (next_fragP = fragP;
11996 next_fragP != NULL;
11997 next_fragP = next_fragP->fr_next)
11998 {
11999 if (next_fragP->fr_type == rs_fill)
12000 /* Skip rs_fill frags. */
12001 continue;
12002 else if (next_fragP->fr_type != rs_machine_dependent)
12003 /* Stop for all other frags. */
12004 break;
12005
12006 /* rs_machine_dependent frags. */
12007 if (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12008 == BRANCH_PREFIX)
12009 {
12010 /* Count BRANCH_PREFIX frags. */
12011 if (max_prefix_length >= MAX_FUSED_JCC_PADDING_SIZE)
12012 {
12013 max_prefix_length = MAX_FUSED_JCC_PADDING_SIZE;
12014 frag_wane (next_fragP);
12015 }
12016 else
12017 max_prefix_length
12018 += next_fragP->tc_frag_data.max_bytes;
12019 }
12020 else if ((TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12021 == BRANCH_PADDING)
12022 || (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12023 == FUSED_JCC_PADDING))
12024 {
12025 /* Stop at BRANCH_PADDING and FUSED_JCC_PADDING. */
12026 fragP->tc_frag_data.u.padding_fragP = next_fragP;
12027 break;
12028 }
12029 else
12030 /* Stop for other rs_machine_dependent frags. */
12031 break;
12032 }
12033
12034 fragP->tc_frag_data.max_prefix_length = max_prefix_length;
12035
12036 /* Skip to the next frag. */
12037 fragP = next_fragP;
12038 }
12039}
12040
12041/* Compute padding size for
12042
12043 FUSED_JCC_PADDING
12044 CMP like instruction
12045 BRANCH_PADDING
12046 COND_JUMP/UNCOND_JUMP
12047
12048 or
12049
12050 BRANCH_PADDING
12051 COND_JUMP/UNCOND_JUMP
12052 */
12053
12054static int
12055i386_branch_padding_size (fragS *fragP, offsetT address)
12056{
12057 unsigned int offset, size, padding_size;
12058 fragS *branch_fragP = fragP->tc_frag_data.u.branch_fragP;
12059
12060 /* The start address of the BRANCH_PADDING or FUSED_JCC_PADDING frag. */
12061 if (!address)
12062 address = fragP->fr_address;
12063 address += fragP->fr_fix;
12064
12065 /* CMP like instrunction size. */
12066 size = fragP->tc_frag_data.cmp_size;
12067
12068 /* The base size of the branch frag. */
12069 size += branch_fragP->fr_fix;
12070
12071 /* Add opcode and displacement bytes for the rs_machine_dependent
12072 branch frag. */
12073 if (branch_fragP->fr_type == rs_machine_dependent)
12074 size += md_relax_table[branch_fragP->fr_subtype].rlx_length;
12075
12076 /* Check if branch is within boundary and doesn't end at the last
12077 byte. */
12078 offset = address & ((1U << align_branch_power) - 1);
12079 if ((offset + size) >= (1U << align_branch_power))
12080 /* Padding needed to avoid crossing boundary. */
12081 padding_size = (1U << align_branch_power) - offset;
12082 else
12083 /* No padding needed. */
12084 padding_size = 0;
12085
12086 /* The return value may be saved in tc_frag_data.length which is
12087 unsigned byte. */
12088 if (!fits_in_unsigned_byte (padding_size))
12089 abort ();
12090
12091 return padding_size;
12092}
12093
12094/* i386_generic_table_relax_frag()
12095
12096 Handle BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags to
12097 grow/shrink padding to align branch frags. Hand others to
12098 relax_frag(). */
12099
12100long
12101i386_generic_table_relax_frag (segT segment, fragS *fragP, long stretch)
12102{
12103 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12104 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
12105 {
12106 long padding_size = i386_branch_padding_size (fragP, 0);
12107 long grow = padding_size - fragP->tc_frag_data.length;
12108
12109 /* When the BRANCH_PREFIX frag is used, the computed address
12110 must match the actual address and there should be no padding. */
12111 if (fragP->tc_frag_data.padding_address
12112 && (fragP->tc_frag_data.padding_address != fragP->fr_address
12113 || padding_size))
12114 abort ();
12115
12116 /* Update the padding size. */
12117 if (grow)
12118 fragP->tc_frag_data.length = padding_size;
12119
12120 return grow;
12121 }
12122 else if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12123 {
12124 fragS *padding_fragP, *next_fragP;
12125 long padding_size, left_size, last_size;
12126
12127 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
12128 if (!padding_fragP)
12129 /* Use the padding set by the leading BRANCH_PREFIX frag. */
12130 return (fragP->tc_frag_data.length
12131 - fragP->tc_frag_data.last_length);
12132
12133 /* Compute the relative address of the padding frag in the very
12134 first time where the BRANCH_PREFIX frag sizes are zero. */
12135 if (!fragP->tc_frag_data.padding_address)
12136 fragP->tc_frag_data.padding_address
12137 = padding_fragP->fr_address - (fragP->fr_address - stretch);
12138
12139 /* First update the last length from the previous interation. */
12140 left_size = fragP->tc_frag_data.prefix_length;
12141 for (next_fragP = fragP;
12142 next_fragP != padding_fragP;
12143 next_fragP = next_fragP->fr_next)
12144 if (next_fragP->fr_type == rs_machine_dependent
12145 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12146 == BRANCH_PREFIX))
12147 {
12148 if (left_size)
12149 {
12150 int max = next_fragP->tc_frag_data.max_bytes;
12151 if (max)
12152 {
12153 int size;
12154 if (max > left_size)
12155 size = left_size;
12156 else
12157 size = max;
12158 left_size -= size;
12159 next_fragP->tc_frag_data.last_length = size;
12160 }
12161 }
12162 else
12163 next_fragP->tc_frag_data.last_length = 0;
12164 }
12165
12166 /* Check the padding size for the padding frag. */
12167 padding_size = i386_branch_padding_size
12168 (padding_fragP, (fragP->fr_address
12169 + fragP->tc_frag_data.padding_address));
12170
12171 last_size = fragP->tc_frag_data.prefix_length;
12172 /* Check if there is change from the last interation. */
12173 if (padding_size == last_size)
12174 {
12175 /* Update the expected address of the padding frag. */
12176 padding_fragP->tc_frag_data.padding_address
12177 = (fragP->fr_address + padding_size
12178 + fragP->tc_frag_data.padding_address);
12179 return 0;
12180 }
12181
12182 if (padding_size > fragP->tc_frag_data.max_prefix_length)
12183 {
12184 /* No padding if there is no sufficient room. Clear the
12185 expected address of the padding frag. */
12186 padding_fragP->tc_frag_data.padding_address = 0;
12187 padding_size = 0;
12188 }
12189 else
12190 /* Store the expected address of the padding frag. */
12191 padding_fragP->tc_frag_data.padding_address
12192 = (fragP->fr_address + padding_size
12193 + fragP->tc_frag_data.padding_address);
12194
12195 fragP->tc_frag_data.prefix_length = padding_size;
12196
12197 /* Update the length for the current interation. */
12198 left_size = padding_size;
12199 for (next_fragP = fragP;
12200 next_fragP != padding_fragP;
12201 next_fragP = next_fragP->fr_next)
12202 if (next_fragP->fr_type == rs_machine_dependent
12203 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12204 == BRANCH_PREFIX))
12205 {
12206 if (left_size)
12207 {
12208 int max = next_fragP->tc_frag_data.max_bytes;
12209 if (max)
12210 {
12211 int size;
12212 if (max > left_size)
12213 size = left_size;
12214 else
12215 size = max;
12216 left_size -= size;
12217 next_fragP->tc_frag_data.length = size;
12218 }
12219 }
12220 else
12221 next_fragP->tc_frag_data.length = 0;
12222 }
12223
12224 return (fragP->tc_frag_data.length
12225 - fragP->tc_frag_data.last_length);
12226 }
12227 return relax_frag (segment, fragP, stretch);
12228}
12229
ee7fcc42
AM
12230/* md_estimate_size_before_relax()
12231
12232 Called just before relax() for rs_machine_dependent frags. The x86
12233 assembler uses these frags to handle variable size jump
12234 instructions.
12235
12236 Any symbol that is now undefined will not become defined.
12237 Return the correct fr_subtype in the frag.
12238 Return the initial "guess for variable size of frag" to caller.
12239 The guess is actually the growth beyond the fixed part. Whatever
12240 we do to grow the fixed or variable part contributes to our
12241 returned value. */
12242
252b5132 12243int
7016a5d5 12244md_estimate_size_before_relax (fragS *fragP, segT segment)
252b5132 12245{
e379e5f3
L
12246 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12247 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX
12248 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
12249 {
12250 i386_classify_machine_dependent_frag (fragP);
12251 return fragP->tc_frag_data.length;
12252 }
12253
252b5132 12254 /* We've already got fragP->fr_subtype right; all we have to do is
b98ef147
AM
12255 check for un-relaxable symbols. On an ELF system, we can't relax
12256 an externally visible symbol, because it may be overridden by a
12257 shared library. */
12258 if (S_GET_SEGMENT (fragP->fr_symbol) != segment
6d249963 12259#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12260 || (IS_ELF
8dcea932
L
12261 && !elf_symbol_resolved_in_segment_p (fragP->fr_symbol,
12262 fragP->fr_var))
fbeb56a4
DK
12263#endif
12264#if defined (OBJ_COFF) && defined (TE_PE)
7ab9ffdd 12265 || (OUTPUT_FLAVOR == bfd_target_coff_flavour
fbeb56a4 12266 && S_IS_WEAK (fragP->fr_symbol))
b98ef147
AM
12267#endif
12268 )
252b5132 12269 {
b98ef147
AM
12270 /* Symbol is undefined in this segment, or we need to keep a
12271 reloc so that weak symbols can be overridden. */
12272 int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
f86103b7 12273 enum bfd_reloc_code_real reloc_type;
ee7fcc42
AM
12274 unsigned char *opcode;
12275 int old_fr_fix;
eb19308f 12276 fixS *fixP = NULL;
f6af82bd 12277
ee7fcc42 12278 if (fragP->fr_var != NO_RELOC)
1e9cc1c2 12279 reloc_type = (enum bfd_reloc_code_real) fragP->fr_var;
b98ef147 12280 else if (size == 2)
f6af82bd 12281 reloc_type = BFD_RELOC_16_PCREL;
bd7ab16b 12282#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1ef3994a
JB
12283 else if (fragP->tc_frag_data.code64 && fragP->fr_offset == 0
12284 && need_plt32_p (fragP->fr_symbol))
bd7ab16b
L
12285 reloc_type = BFD_RELOC_X86_64_PLT32;
12286#endif
f6af82bd
AM
12287 else
12288 reloc_type = BFD_RELOC_32_PCREL;
252b5132 12289
ee7fcc42
AM
12290 old_fr_fix = fragP->fr_fix;
12291 opcode = (unsigned char *) fragP->fr_opcode;
12292
fddf5b5b 12293 switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
252b5132 12294 {
fddf5b5b
AM
12295 case UNCOND_JUMP:
12296 /* Make jmp (0xeb) a (d)word displacement jump. */
47926f60 12297 opcode[0] = 0xe9;
252b5132 12298 fragP->fr_fix += size;
eb19308f
JB
12299 fixP = fix_new (fragP, old_fr_fix, size,
12300 fragP->fr_symbol,
12301 fragP->fr_offset, 1,
12302 reloc_type);
252b5132
RH
12303 break;
12304
fddf5b5b 12305 case COND_JUMP86:
412167cb
AM
12306 if (size == 2
12307 && (!no_cond_jump_promotion || fragP->fr_var != NO_RELOC))
fddf5b5b
AM
12308 {
12309 /* Negate the condition, and branch past an
12310 unconditional jump. */
12311 opcode[0] ^= 1;
12312 opcode[1] = 3;
12313 /* Insert an unconditional jump. */
12314 opcode[2] = 0xe9;
12315 /* We added two extra opcode bytes, and have a two byte
12316 offset. */
12317 fragP->fr_fix += 2 + 2;
062cd5e7
AS
12318 fix_new (fragP, old_fr_fix + 2, 2,
12319 fragP->fr_symbol,
12320 fragP->fr_offset, 1,
12321 reloc_type);
fddf5b5b
AM
12322 break;
12323 }
12324 /* Fall through. */
12325
12326 case COND_JUMP:
412167cb
AM
12327 if (no_cond_jump_promotion && fragP->fr_var == NO_RELOC)
12328 {
12329 fragP->fr_fix += 1;
3e02c1cc
AM
12330 fixP = fix_new (fragP, old_fr_fix, 1,
12331 fragP->fr_symbol,
12332 fragP->fr_offset, 1,
12333 BFD_RELOC_8_PCREL);
12334 fixP->fx_signed = 1;
412167cb
AM
12335 break;
12336 }
93c2a809 12337
24eab124 12338 /* This changes the byte-displacement jump 0x7N
fddf5b5b 12339 to the (d)word-displacement jump 0x0f,0x8N. */
252b5132 12340 opcode[1] = opcode[0] + 0x10;
f6af82bd 12341 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
47926f60
KH
12342 /* We've added an opcode byte. */
12343 fragP->fr_fix += 1 + size;
eb19308f
JB
12344 fixP = fix_new (fragP, old_fr_fix + 1, size,
12345 fragP->fr_symbol,
12346 fragP->fr_offset, 1,
12347 reloc_type);
252b5132 12348 break;
fddf5b5b
AM
12349
12350 default:
12351 BAD_CASE (fragP->fr_subtype);
12352 break;
252b5132 12353 }
eb19308f
JB
12354
12355 /* All jumps handled here are signed, but don't unconditionally use a
12356 signed limit check for 32 and 16 bit jumps as we want to allow wrap
12357 around at 4G (outside of 64-bit mode) and 64k. */
12358 if (size == 4 && flag_code == CODE_64BIT)
12359 fixP->fx_signed = 1;
12360
252b5132 12361 frag_wane (fragP);
ee7fcc42 12362 return fragP->fr_fix - old_fr_fix;
252b5132 12363 }
93c2a809 12364
93c2a809
AM
12365 /* Guess size depending on current relax state. Initially the relax
12366 state will correspond to a short jump and we return 1, because
12367 the variable part of the frag (the branch offset) is one byte
12368 long. However, we can relax a section more than once and in that
12369 case we must either set fr_subtype back to the unrelaxed state,
12370 or return the value for the appropriate branch. */
12371 return md_relax_table[fragP->fr_subtype].rlx_length;
ee7fcc42
AM
12372}
12373
47926f60
KH
12374/* Called after relax() is finished.
12375
12376 In: Address of frag.
12377 fr_type == rs_machine_dependent.
12378 fr_subtype is what the address relaxed to.
12379
12380 Out: Any fixSs and constants are set up.
12381 Caller will turn frag into a ".space 0". */
12382
252b5132 12383void
7016a5d5
TG
12384md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec ATTRIBUTE_UNUSED,
12385 fragS *fragP)
252b5132 12386{
29b0f896 12387 unsigned char *opcode;
252b5132 12388 unsigned char *where_to_put_displacement = NULL;
847f7ad4
AM
12389 offsetT target_address;
12390 offsetT opcode_address;
252b5132 12391 unsigned int extension = 0;
847f7ad4 12392 offsetT displacement_from_opcode_start;
252b5132 12393
e379e5f3
L
12394 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12395 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING
12396 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12397 {
12398 /* Generate nop padding. */
12399 unsigned int size = fragP->tc_frag_data.length;
12400 if (size)
12401 {
12402 if (size > fragP->tc_frag_data.max_bytes)
12403 abort ();
12404
12405 if (flag_debug)
12406 {
12407 const char *msg;
12408 const char *branch = "branch";
12409 const char *prefix = "";
12410 fragS *padding_fragP;
12411 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
12412 == BRANCH_PREFIX)
12413 {
12414 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
12415 switch (fragP->tc_frag_data.default_prefix)
12416 {
12417 default:
12418 abort ();
12419 break;
12420 case CS_PREFIX_OPCODE:
12421 prefix = " cs";
12422 break;
12423 case DS_PREFIX_OPCODE:
12424 prefix = " ds";
12425 break;
12426 case ES_PREFIX_OPCODE:
12427 prefix = " es";
12428 break;
12429 case FS_PREFIX_OPCODE:
12430 prefix = " fs";
12431 break;
12432 case GS_PREFIX_OPCODE:
12433 prefix = " gs";
12434 break;
12435 case SS_PREFIX_OPCODE:
12436 prefix = " ss";
12437 break;
12438 }
12439 if (padding_fragP)
12440 msg = _("%s:%u: add %d%s at 0x%llx to align "
12441 "%s within %d-byte boundary\n");
12442 else
12443 msg = _("%s:%u: add additional %d%s at 0x%llx to "
12444 "align %s within %d-byte boundary\n");
12445 }
12446 else
12447 {
12448 padding_fragP = fragP;
12449 msg = _("%s:%u: add %d%s-byte nop at 0x%llx to align "
12450 "%s within %d-byte boundary\n");
12451 }
12452
12453 if (padding_fragP)
12454 switch (padding_fragP->tc_frag_data.branch_type)
12455 {
12456 case align_branch_jcc:
12457 branch = "jcc";
12458 break;
12459 case align_branch_fused:
12460 branch = "fused jcc";
12461 break;
12462 case align_branch_jmp:
12463 branch = "jmp";
12464 break;
12465 case align_branch_call:
12466 branch = "call";
12467 break;
12468 case align_branch_indirect:
12469 branch = "indiret branch";
12470 break;
12471 case align_branch_ret:
12472 branch = "ret";
12473 break;
12474 default:
12475 break;
12476 }
12477
12478 fprintf (stdout, msg,
12479 fragP->fr_file, fragP->fr_line, size, prefix,
12480 (long long) fragP->fr_address, branch,
12481 1 << align_branch_power);
12482 }
12483 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12484 memset (fragP->fr_opcode,
12485 fragP->tc_frag_data.default_prefix, size);
12486 else
12487 i386_generate_nops (fragP, (char *) fragP->fr_opcode,
12488 size, 0);
12489 fragP->fr_fix += size;
12490 }
12491 return;
12492 }
12493
252b5132
RH
12494 opcode = (unsigned char *) fragP->fr_opcode;
12495
47926f60 12496 /* Address we want to reach in file space. */
252b5132 12497 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
252b5132 12498
47926f60 12499 /* Address opcode resides at in file space. */
252b5132
RH
12500 opcode_address = fragP->fr_address + fragP->fr_fix;
12501
47926f60 12502 /* Displacement from opcode start to fill into instruction. */
252b5132
RH
12503 displacement_from_opcode_start = target_address - opcode_address;
12504
fddf5b5b 12505 if ((fragP->fr_subtype & BIG) == 0)
252b5132 12506 {
47926f60
KH
12507 /* Don't have to change opcode. */
12508 extension = 1; /* 1 opcode + 1 displacement */
252b5132 12509 where_to_put_displacement = &opcode[1];
fddf5b5b
AM
12510 }
12511 else
12512 {
12513 if (no_cond_jump_promotion
12514 && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
4eed87de
AM
12515 as_warn_where (fragP->fr_file, fragP->fr_line,
12516 _("long jump required"));
252b5132 12517
fddf5b5b
AM
12518 switch (fragP->fr_subtype)
12519 {
12520 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
12521 extension = 4; /* 1 opcode + 4 displacement */
12522 opcode[0] = 0xe9;
12523 where_to_put_displacement = &opcode[1];
12524 break;
252b5132 12525
fddf5b5b
AM
12526 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
12527 extension = 2; /* 1 opcode + 2 displacement */
12528 opcode[0] = 0xe9;
12529 where_to_put_displacement = &opcode[1];
12530 break;
252b5132 12531
fddf5b5b
AM
12532 case ENCODE_RELAX_STATE (COND_JUMP, BIG):
12533 case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
12534 extension = 5; /* 2 opcode + 4 displacement */
12535 opcode[1] = opcode[0] + 0x10;
12536 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12537 where_to_put_displacement = &opcode[2];
12538 break;
252b5132 12539
fddf5b5b
AM
12540 case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
12541 extension = 3; /* 2 opcode + 2 displacement */
12542 opcode[1] = opcode[0] + 0x10;
12543 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12544 where_to_put_displacement = &opcode[2];
12545 break;
252b5132 12546
fddf5b5b
AM
12547 case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
12548 extension = 4;
12549 opcode[0] ^= 1;
12550 opcode[1] = 3;
12551 opcode[2] = 0xe9;
12552 where_to_put_displacement = &opcode[3];
12553 break;
12554
12555 default:
12556 BAD_CASE (fragP->fr_subtype);
12557 break;
12558 }
252b5132 12559 }
fddf5b5b 12560
7b81dfbb
AJ
12561 /* If size if less then four we are sure that the operand fits,
12562 but if it's 4, then it could be that the displacement is larger
12563 then -/+ 2GB. */
12564 if (DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype) == 4
12565 && object_64bit
12566 && ((addressT) (displacement_from_opcode_start - extension
4eed87de
AM
12567 + ((addressT) 1 << 31))
12568 > (((addressT) 2 << 31) - 1)))
7b81dfbb
AJ
12569 {
12570 as_bad_where (fragP->fr_file, fragP->fr_line,
12571 _("jump target out of range"));
12572 /* Make us emit 0. */
12573 displacement_from_opcode_start = extension;
12574 }
47926f60 12575 /* Now put displacement after opcode. */
252b5132
RH
12576 md_number_to_chars ((char *) where_to_put_displacement,
12577 (valueT) (displacement_from_opcode_start - extension),
fddf5b5b 12578 DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
252b5132
RH
12579 fragP->fr_fix += extension;
12580}
12581\f
7016a5d5 12582/* Apply a fixup (fixP) to segment data, once it has been determined
252b5132
RH
12583 by our caller that we have all the info we need to fix it up.
12584
7016a5d5
TG
12585 Parameter valP is the pointer to the value of the bits.
12586
252b5132
RH
12587 On the 386, immediates, displacements, and data pointers are all in
12588 the same (little-endian) format, so we don't need to care about which
12589 we are handling. */
12590
94f592af 12591void
7016a5d5 12592md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 12593{
94f592af 12594 char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
c6682705 12595 valueT value = *valP;
252b5132 12596
f86103b7 12597#if !defined (TE_Mach)
93382f6d
AM
12598 if (fixP->fx_pcrel)
12599 {
12600 switch (fixP->fx_r_type)
12601 {
5865bb77
ILT
12602 default:
12603 break;
12604
d6ab8113
JB
12605 case BFD_RELOC_64:
12606 fixP->fx_r_type = BFD_RELOC_64_PCREL;
12607 break;
93382f6d 12608 case BFD_RELOC_32:
ae8887b5 12609 case BFD_RELOC_X86_64_32S:
93382f6d
AM
12610 fixP->fx_r_type = BFD_RELOC_32_PCREL;
12611 break;
12612 case BFD_RELOC_16:
12613 fixP->fx_r_type = BFD_RELOC_16_PCREL;
12614 break;
12615 case BFD_RELOC_8:
12616 fixP->fx_r_type = BFD_RELOC_8_PCREL;
12617 break;
12618 }
12619 }
252b5132 12620
a161fe53 12621 if (fixP->fx_addsy != NULL
31312f95 12622 && (fixP->fx_r_type == BFD_RELOC_32_PCREL
d6ab8113 12623 || fixP->fx_r_type == BFD_RELOC_64_PCREL
31312f95 12624 || fixP->fx_r_type == BFD_RELOC_16_PCREL
d258b828 12625 || fixP->fx_r_type == BFD_RELOC_8_PCREL)
31312f95 12626 && !use_rela_relocations)
252b5132 12627 {
31312f95
AM
12628 /* This is a hack. There should be a better way to handle this.
12629 This covers for the fact that bfd_install_relocation will
12630 subtract the current location (for partial_inplace, PC relative
12631 relocations); see more below. */
252b5132 12632#ifndef OBJ_AOUT
718ddfc0 12633 if (IS_ELF
252b5132
RH
12634#ifdef TE_PE
12635 || OUTPUT_FLAVOR == bfd_target_coff_flavour
12636#endif
12637 )
12638 value += fixP->fx_where + fixP->fx_frag->fr_address;
12639#endif
12640#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12641 if (IS_ELF)
252b5132 12642 {
6539b54b 12643 segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
2f66722d 12644
6539b54b 12645 if ((sym_seg == seg
2f66722d 12646 || (symbol_section_p (fixP->fx_addsy)
6539b54b 12647 && sym_seg != absolute_section))
af65af87 12648 && !generic_force_reloc (fixP))
2f66722d
AM
12649 {
12650 /* Yes, we add the values in twice. This is because
6539b54b
AM
12651 bfd_install_relocation subtracts them out again. I think
12652 bfd_install_relocation is broken, but I don't dare change
2f66722d
AM
12653 it. FIXME. */
12654 value += fixP->fx_where + fixP->fx_frag->fr_address;
12655 }
252b5132
RH
12656 }
12657#endif
12658#if defined (OBJ_COFF) && defined (TE_PE)
977cdf5a
NC
12659 /* For some reason, the PE format does not store a
12660 section address offset for a PC relative symbol. */
12661 if (S_GET_SEGMENT (fixP->fx_addsy) != seg
7be1c489 12662 || S_IS_WEAK (fixP->fx_addsy))
252b5132
RH
12663 value += md_pcrel_from (fixP);
12664#endif
12665 }
fbeb56a4 12666#if defined (OBJ_COFF) && defined (TE_PE)
f01c1a09
NC
12667 if (fixP->fx_addsy != NULL
12668 && S_IS_WEAK (fixP->fx_addsy)
12669 /* PR 16858: Do not modify weak function references. */
12670 && ! fixP->fx_pcrel)
fbeb56a4 12671 {
296a8689
NC
12672#if !defined (TE_PEP)
12673 /* For x86 PE weak function symbols are neither PC-relative
12674 nor do they set S_IS_FUNCTION. So the only reliable way
12675 to detect them is to check the flags of their containing
12676 section. */
12677 if (S_GET_SEGMENT (fixP->fx_addsy) != NULL
12678 && S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_CODE)
12679 ;
12680 else
12681#endif
fbeb56a4
DK
12682 value -= S_GET_VALUE (fixP->fx_addsy);
12683 }
12684#endif
252b5132
RH
12685
12686 /* Fix a few things - the dynamic linker expects certain values here,
0234cb7c 12687 and we must not disappoint it. */
252b5132 12688#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12689 if (IS_ELF && fixP->fx_addsy)
47926f60
KH
12690 switch (fixP->fx_r_type)
12691 {
12692 case BFD_RELOC_386_PLT32:
3e73aa7c 12693 case BFD_RELOC_X86_64_PLT32:
b9519cfe
L
12694 /* Make the jump instruction point to the address of the operand.
12695 At runtime we merely add the offset to the actual PLT entry.
12696 NB: Subtract the offset size only for jump instructions. */
12697 if (fixP->fx_pcrel)
12698 value = -4;
47926f60 12699 break;
31312f95 12700
13ae64f3
JJ
12701 case BFD_RELOC_386_TLS_GD:
12702 case BFD_RELOC_386_TLS_LDM:
13ae64f3 12703 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
12704 case BFD_RELOC_386_TLS_IE:
12705 case BFD_RELOC_386_TLS_GOTIE:
67a4f2b7 12706 case BFD_RELOC_386_TLS_GOTDESC:
bffbf940
JJ
12707 case BFD_RELOC_X86_64_TLSGD:
12708 case BFD_RELOC_X86_64_TLSLD:
12709 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7 12710 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
00f7efb6
JJ
12711 value = 0; /* Fully resolved at runtime. No addend. */
12712 /* Fallthrough */
12713 case BFD_RELOC_386_TLS_LE:
12714 case BFD_RELOC_386_TLS_LDO_32:
12715 case BFD_RELOC_386_TLS_LE_32:
12716 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 12717 case BFD_RELOC_X86_64_DTPOFF64:
00f7efb6 12718 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113 12719 case BFD_RELOC_X86_64_TPOFF64:
00f7efb6
JJ
12720 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12721 break;
12722
67a4f2b7
AO
12723 case BFD_RELOC_386_TLS_DESC_CALL:
12724 case BFD_RELOC_X86_64_TLSDESC_CALL:
12725 value = 0; /* Fully resolved at runtime. No addend. */
12726 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12727 fixP->fx_done = 0;
12728 return;
12729
47926f60
KH
12730 case BFD_RELOC_VTABLE_INHERIT:
12731 case BFD_RELOC_VTABLE_ENTRY:
12732 fixP->fx_done = 0;
94f592af 12733 return;
47926f60
KH
12734
12735 default:
12736 break;
12737 }
12738#endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) */
a442cac5
JB
12739
12740 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
12741 if (!object_64bit)
12742 value = extend_to_32bit_address (value);
12743
c6682705 12744 *valP = value;
f86103b7 12745#endif /* !defined (TE_Mach) */
3e73aa7c 12746
3e73aa7c 12747 /* Are we finished with this relocation now? */
c6682705 12748 if (fixP->fx_addsy == NULL)
b8188555
JB
12749 {
12750 fixP->fx_done = 1;
12751 switch (fixP->fx_r_type)
12752 {
12753 case BFD_RELOC_X86_64_32S:
12754 fixP->fx_signed = 1;
12755 break;
12756
12757 default:
12758 break;
12759 }
12760 }
fbeb56a4
DK
12761#if defined (OBJ_COFF) && defined (TE_PE)
12762 else if (fixP->fx_addsy != NULL && S_IS_WEAK (fixP->fx_addsy))
12763 {
12764 fixP->fx_done = 0;
12765 /* Remember value for tc_gen_reloc. */
12766 fixP->fx_addnumber = value;
12767 /* Clear out the frag for now. */
12768 value = 0;
12769 }
12770#endif
3e73aa7c
JH
12771 else if (use_rela_relocations)
12772 {
46fb6d5a
JB
12773 if (!disallow_64bit_reloc || fixP->fx_r_type == NO_RELOC)
12774 fixP->fx_no_overflow = 1;
062cd5e7
AS
12775 /* Remember value for tc_gen_reloc. */
12776 fixP->fx_addnumber = value;
3e73aa7c
JH
12777 value = 0;
12778 }
f86103b7 12779
94f592af 12780 md_number_to_chars (p, value, fixP->fx_size);
252b5132 12781}
252b5132 12782\f
6d4af3c2 12783const char *
499ac353 12784md_atof (int type, char *litP, int *sizeP)
252b5132 12785{
499ac353
NC
12786 /* This outputs the LITTLENUMs in REVERSE order;
12787 in accord with the bigendian 386. */
5b7c81bd 12788 return ieee_md_atof (type, litP, sizeP, false);
252b5132
RH
12789}
12790\f
2d545b82 12791static char output_invalid_buf[sizeof (unsigned char) * 2 + 6];
252b5132 12792
252b5132 12793static char *
e3bb37b5 12794output_invalid (int c)
252b5132 12795{
3882b010 12796 if (ISPRINT (c))
f9f21a03
L
12797 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
12798 "'%c'", c);
252b5132 12799 else
f9f21a03 12800 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
2d545b82 12801 "(0x%x)", (unsigned char) c);
252b5132
RH
12802 return output_invalid_buf;
12803}
12804
8a6fb3f9
JB
12805/* Verify that @r can be used in the current context. */
12806
5b7c81bd 12807static bool check_register (const reg_entry *r)
8a6fb3f9
JB
12808{
12809 if (allow_pseudo_reg)
5b7c81bd 12810 return true;
8a6fb3f9
JB
12811
12812 if (operand_type_all_zero (&r->reg_type))
5b7c81bd 12813 return false;
8a6fb3f9
JB
12814
12815 if ((r->reg_type.bitfield.dword
12816 || (r->reg_type.bitfield.class == SReg && r->reg_num > 3)
12817 || r->reg_type.bitfield.class == RegCR
22e00a3f 12818 || r->reg_type.bitfield.class == RegDR)
8a6fb3f9 12819 && !cpu_arch_flags.bitfield.cpui386)
5b7c81bd 12820 return false;
8a6fb3f9 12821
22e00a3f
JB
12822 if (r->reg_type.bitfield.class == RegTR
12823 && (flag_code == CODE_64BIT
12824 || !cpu_arch_flags.bitfield.cpui386
12825 || cpu_arch_isa_flags.bitfield.cpui586
12826 || cpu_arch_isa_flags.bitfield.cpui686))
5b7c81bd 12827 return false;
22e00a3f 12828
8a6fb3f9 12829 if (r->reg_type.bitfield.class == RegMMX && !cpu_arch_flags.bitfield.cpummx)
5b7c81bd 12830 return false;
8a6fb3f9
JB
12831
12832 if (!cpu_arch_flags.bitfield.cpuavx512f)
12833 {
12834 if (r->reg_type.bitfield.zmmword
12835 || r->reg_type.bitfield.class == RegMask)
5b7c81bd 12836 return false;
8a6fb3f9
JB
12837
12838 if (!cpu_arch_flags.bitfield.cpuavx)
12839 {
12840 if (r->reg_type.bitfield.ymmword)
5b7c81bd 12841 return false;
8a6fb3f9
JB
12842
12843 if (!cpu_arch_flags.bitfield.cpusse && r->reg_type.bitfield.xmmword)
5b7c81bd 12844 return false;
8a6fb3f9
JB
12845 }
12846 }
12847
260cd341
LC
12848 if (r->reg_type.bitfield.tmmword
12849 && (!cpu_arch_flags.bitfield.cpuamx_tile
12850 || flag_code != CODE_64BIT))
5b7c81bd 12851 return false;
260cd341 12852
8a6fb3f9 12853 if (r->reg_type.bitfield.class == RegBND && !cpu_arch_flags.bitfield.cpumpx)
5b7c81bd 12854 return false;
8a6fb3f9
JB
12855
12856 /* Don't allow fake index register unless allow_index_reg isn't 0. */
12857 if (!allow_index_reg && r->reg_num == RegIZ)
5b7c81bd 12858 return false;
8a6fb3f9
JB
12859
12860 /* Upper 16 vector registers are only available with VREX in 64bit
12861 mode, and require EVEX encoding. */
12862 if (r->reg_flags & RegVRex)
12863 {
12864 if (!cpu_arch_flags.bitfield.cpuavx512f
12865 || flag_code != CODE_64BIT)
5b7c81bd 12866 return false;
8a6fb3f9 12867
da4977e0
JB
12868 if (i.vec_encoding == vex_encoding_default)
12869 i.vec_encoding = vex_encoding_evex;
12870 else if (i.vec_encoding != vex_encoding_evex)
12871 i.vec_encoding = vex_encoding_error;
8a6fb3f9
JB
12872 }
12873
12874 if (((r->reg_flags & (RegRex64 | RegRex)) || r->reg_type.bitfield.qword)
12875 && (!cpu_arch_flags.bitfield.cpulm || r->reg_type.bitfield.class != RegCR)
12876 && flag_code != CODE_64BIT)
5b7c81bd 12877 return false;
8a6fb3f9
JB
12878
12879 if (r->reg_type.bitfield.class == SReg && r->reg_num == RegFlat
12880 && !intel_syntax)
5b7c81bd 12881 return false;
8a6fb3f9 12882
5b7c81bd 12883 return true;
8a6fb3f9
JB
12884}
12885
af6bdddf 12886/* REG_STRING starts *before* REGISTER_PREFIX. */
252b5132
RH
12887
12888static const reg_entry *
4d1bb795 12889parse_real_register (char *reg_string, char **end_op)
252b5132 12890{
af6bdddf
AM
12891 char *s = reg_string;
12892 char *p;
252b5132
RH
12893 char reg_name_given[MAX_REG_NAME_SIZE + 1];
12894 const reg_entry *r;
12895
12896 /* Skip possible REGISTER_PREFIX and possible whitespace. */
12897 if (*s == REGISTER_PREFIX)
12898 ++s;
12899
12900 if (is_space_char (*s))
12901 ++s;
12902
12903 p = reg_name_given;
af6bdddf 12904 while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
252b5132
RH
12905 {
12906 if (p >= reg_name_given + MAX_REG_NAME_SIZE)
af6bdddf
AM
12907 return (const reg_entry *) NULL;
12908 s++;
252b5132
RH
12909 }
12910
6588847e
DN
12911 /* For naked regs, make sure that we are not dealing with an identifier.
12912 This prevents confusing an identifier like `eax_var' with register
12913 `eax'. */
12914 if (allow_naked_reg && identifier_chars[(unsigned char) *s])
12915 return (const reg_entry *) NULL;
12916
af6bdddf 12917 *end_op = s;
252b5132 12918
629310ab 12919 r = (const reg_entry *) str_hash_find (reg_hash, reg_name_given);
252b5132 12920
5f47d35b 12921 /* Handle floating point regs, allowing spaces in the (i) part. */
6288d05f 12922 if (r == reg_st0)
5f47d35b 12923 {
0e0eea78
JB
12924 if (!cpu_arch_flags.bitfield.cpu8087
12925 && !cpu_arch_flags.bitfield.cpu287
af32b722
JB
12926 && !cpu_arch_flags.bitfield.cpu387
12927 && !allow_pseudo_reg)
0e0eea78
JB
12928 return (const reg_entry *) NULL;
12929
5f47d35b
AM
12930 if (is_space_char (*s))
12931 ++s;
12932 if (*s == '(')
12933 {
af6bdddf 12934 ++s;
5f47d35b
AM
12935 if (is_space_char (*s))
12936 ++s;
12937 if (*s >= '0' && *s <= '7')
12938 {
db557034 12939 int fpr = *s - '0';
af6bdddf 12940 ++s;
5f47d35b
AM
12941 if (is_space_char (*s))
12942 ++s;
12943 if (*s == ')')
12944 {
12945 *end_op = s + 1;
6288d05f 12946 know (r[fpr].reg_num == fpr);
db557034 12947 return r + fpr;
5f47d35b 12948 }
5f47d35b 12949 }
47926f60 12950 /* We have "%st(" then garbage. */
5f47d35b
AM
12951 return (const reg_entry *) NULL;
12952 }
12953 }
12954
8a6fb3f9 12955 return r && check_register (r) ? r : NULL;
252b5132 12956}
4d1bb795
JB
12957
12958/* REG_STRING starts *before* REGISTER_PREFIX. */
12959
12960static const reg_entry *
12961parse_register (char *reg_string, char **end_op)
12962{
12963 const reg_entry *r;
12964
12965 if (*reg_string == REGISTER_PREFIX || allow_naked_reg)
12966 r = parse_real_register (reg_string, end_op);
12967 else
12968 r = NULL;
12969 if (!r)
12970 {
12971 char *save = input_line_pointer;
12972 char c;
12973 symbolS *symbolP;
12974
12975 input_line_pointer = reg_string;
d02603dc 12976 c = get_symbol_name (&reg_string);
4d1bb795 12977 symbolP = symbol_find (reg_string);
64d23078
JB
12978 while (symbolP && S_GET_SEGMENT (symbolP) != reg_section)
12979 {
12980 const expressionS *e = symbol_get_value_expression(symbolP);
12981
12982 if (e->X_op != O_symbol || e->X_add_number)
12983 break;
12984 symbolP = e->X_add_symbol;
12985 }
4d1bb795
JB
12986 if (symbolP && S_GET_SEGMENT (symbolP) == reg_section)
12987 {
12988 const expressionS *e = symbol_get_value_expression (symbolP);
12989
3b55a1d0
JB
12990 know (e->X_op == O_register);
12991 know (e->X_add_number >= 0
12992 && (valueT) e->X_add_number < i386_regtab_size);
12993 r = i386_regtab + e->X_add_number;
12994 if (!check_register (r))
8a6fb3f9 12995 {
3b55a1d0
JB
12996 as_bad (_("register '%s%s' cannot be used here"),
12997 register_prefix, r->reg_name);
12998 r = &bad_reg;
8a6fb3f9 12999 }
3b55a1d0 13000 *end_op = input_line_pointer;
4d1bb795
JB
13001 }
13002 *input_line_pointer = c;
13003 input_line_pointer = save;
13004 }
13005 return r;
13006}
13007
13008int
13009i386_parse_name (char *name, expressionS *e, char *nextcharP)
13010{
4faaa10f 13011 const reg_entry *r = NULL;
4d1bb795
JB
13012 char *end = input_line_pointer;
13013
13014 *end = *nextcharP;
4faaa10f
JB
13015 if (*name == REGISTER_PREFIX || allow_naked_reg)
13016 r = parse_real_register (name, &input_line_pointer);
4d1bb795
JB
13017 if (r && end <= input_line_pointer)
13018 {
13019 *nextcharP = *input_line_pointer;
13020 *input_line_pointer = 0;
8a6fb3f9
JB
13021 if (r != &bad_reg)
13022 {
13023 e->X_op = O_register;
13024 e->X_add_number = r - i386_regtab;
13025 }
13026 else
13027 e->X_op = O_illegal;
4d1bb795
JB
13028 return 1;
13029 }
13030 input_line_pointer = end;
13031 *end = 0;
ee86248c 13032 return intel_syntax ? i386_intel_parse_name (name, e) : 0;
4d1bb795
JB
13033}
13034
13035void
13036md_operand (expressionS *e)
13037{
ee86248c
JB
13038 char *end;
13039 const reg_entry *r;
4d1bb795 13040
ee86248c
JB
13041 switch (*input_line_pointer)
13042 {
13043 case REGISTER_PREFIX:
13044 r = parse_real_register (input_line_pointer, &end);
4d1bb795
JB
13045 if (r)
13046 {
13047 e->X_op = O_register;
13048 e->X_add_number = r - i386_regtab;
13049 input_line_pointer = end;
13050 }
ee86248c
JB
13051 break;
13052
13053 case '[':
9c2799c2 13054 gas_assert (intel_syntax);
ee86248c
JB
13055 end = input_line_pointer++;
13056 expression (e);
13057 if (*input_line_pointer == ']')
13058 {
13059 ++input_line_pointer;
13060 e->X_op_symbol = make_expr_symbol (e);
13061 e->X_add_symbol = NULL;
13062 e->X_add_number = 0;
13063 e->X_op = O_index;
13064 }
13065 else
13066 {
13067 e->X_op = O_absent;
13068 input_line_pointer = end;
13069 }
13070 break;
4d1bb795
JB
13071 }
13072}
13073
252b5132 13074\f
4cc782b5 13075#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
b6f8c7c4 13076const char *md_shortopts = "kVQ:sqnO::";
252b5132 13077#else
b6f8c7c4 13078const char *md_shortopts = "qnO::";
252b5132 13079#endif
6e0b89ee 13080
3e73aa7c 13081#define OPTION_32 (OPTION_MD_BASE + 0)
b3b91714
AM
13082#define OPTION_64 (OPTION_MD_BASE + 1)
13083#define OPTION_DIVIDE (OPTION_MD_BASE + 2)
9103f4f4
L
13084#define OPTION_MARCH (OPTION_MD_BASE + 3)
13085#define OPTION_MTUNE (OPTION_MD_BASE + 4)
1efbbeb4
L
13086#define OPTION_MMNEMONIC (OPTION_MD_BASE + 5)
13087#define OPTION_MSYNTAX (OPTION_MD_BASE + 6)
13088#define OPTION_MINDEX_REG (OPTION_MD_BASE + 7)
13089#define OPTION_MNAKED_REG (OPTION_MD_BASE + 8)
bd5dea88 13090#define OPTION_MRELAX_RELOCATIONS (OPTION_MD_BASE + 9)
c0f3af97 13091#define OPTION_MSSE2AVX (OPTION_MD_BASE + 10)
daf50ae7 13092#define OPTION_MSSE_CHECK (OPTION_MD_BASE + 11)
7bab8ab5
JB
13093#define OPTION_MOPERAND_CHECK (OPTION_MD_BASE + 12)
13094#define OPTION_MAVXSCALAR (OPTION_MD_BASE + 13)
13095#define OPTION_X32 (OPTION_MD_BASE + 14)
7e8b059b 13096#define OPTION_MADD_BND_PREFIX (OPTION_MD_BASE + 15)
43234a1e
L
13097#define OPTION_MEVEXLIG (OPTION_MD_BASE + 16)
13098#define OPTION_MEVEXWIG (OPTION_MD_BASE + 17)
167ad85b 13099#define OPTION_MBIG_OBJ (OPTION_MD_BASE + 18)
d1982f93 13100#define OPTION_MOMIT_LOCK_PREFIX (OPTION_MD_BASE + 19)
d3d3c6db 13101#define OPTION_MEVEXRCIG (OPTION_MD_BASE + 20)
8dcea932 13102#define OPTION_MSHARED (OPTION_MD_BASE + 21)
5db04b09
L
13103#define OPTION_MAMD64 (OPTION_MD_BASE + 22)
13104#define OPTION_MINTEL64 (OPTION_MD_BASE + 23)
e4e00185 13105#define OPTION_MFENCE_AS_LOCK_ADD (OPTION_MD_BASE + 24)
b4a3a7b4 13106#define OPTION_X86_USED_NOTE (OPTION_MD_BASE + 25)
03751133 13107#define OPTION_MVEXWIG (OPTION_MD_BASE + 26)
e379e5f3
L
13108#define OPTION_MALIGN_BRANCH_BOUNDARY (OPTION_MD_BASE + 27)
13109#define OPTION_MALIGN_BRANCH_PREFIX_SIZE (OPTION_MD_BASE + 28)
13110#define OPTION_MALIGN_BRANCH (OPTION_MD_BASE + 29)
76cf450b 13111#define OPTION_MBRANCHES_WITH_32B_BOUNDARIES (OPTION_MD_BASE + 30)
ae531041
L
13112#define OPTION_MLFENCE_AFTER_LOAD (OPTION_MD_BASE + 31)
13113#define OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH (OPTION_MD_BASE + 32)
13114#define OPTION_MLFENCE_BEFORE_RET (OPTION_MD_BASE + 33)
c8480b58 13115#define OPTION_MUSE_UNALIGNED_VECTOR_MOVE (OPTION_MD_BASE + 34)
b3b91714 13116
99ad8390
NC
13117struct option md_longopts[] =
13118{
3e73aa7c 13119 {"32", no_argument, NULL, OPTION_32},
321098a5 13120#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 13121 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c 13122 {"64", no_argument, NULL, OPTION_64},
351f65ca
L
13123#endif
13124#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 13125 {"x32", no_argument, NULL, OPTION_X32},
8dcea932 13126 {"mshared", no_argument, NULL, OPTION_MSHARED},
b4a3a7b4 13127 {"mx86-used-note", required_argument, NULL, OPTION_X86_USED_NOTE},
6e0b89ee 13128#endif
b3b91714 13129 {"divide", no_argument, NULL, OPTION_DIVIDE},
9103f4f4
L
13130 {"march", required_argument, NULL, OPTION_MARCH},
13131 {"mtune", required_argument, NULL, OPTION_MTUNE},
1efbbeb4
L
13132 {"mmnemonic", required_argument, NULL, OPTION_MMNEMONIC},
13133 {"msyntax", required_argument, NULL, OPTION_MSYNTAX},
13134 {"mindex-reg", no_argument, NULL, OPTION_MINDEX_REG},
13135 {"mnaked-reg", no_argument, NULL, OPTION_MNAKED_REG},
c0f3af97 13136 {"msse2avx", no_argument, NULL, OPTION_MSSE2AVX},
c8480b58 13137 {"muse-unaligned-vector-move", no_argument, NULL, OPTION_MUSE_UNALIGNED_VECTOR_MOVE},
daf50ae7 13138 {"msse-check", required_argument, NULL, OPTION_MSSE_CHECK},
7bab8ab5 13139 {"moperand-check", required_argument, NULL, OPTION_MOPERAND_CHECK},
539f890d 13140 {"mavxscalar", required_argument, NULL, OPTION_MAVXSCALAR},
03751133 13141 {"mvexwig", required_argument, NULL, OPTION_MVEXWIG},
7e8b059b 13142 {"madd-bnd-prefix", no_argument, NULL, OPTION_MADD_BND_PREFIX},
43234a1e
L
13143 {"mevexlig", required_argument, NULL, OPTION_MEVEXLIG},
13144 {"mevexwig", required_argument, NULL, OPTION_MEVEXWIG},
167ad85b
TG
13145# if defined (TE_PE) || defined (TE_PEP)
13146 {"mbig-obj", no_argument, NULL, OPTION_MBIG_OBJ},
13147#endif
d1982f93 13148 {"momit-lock-prefix", required_argument, NULL, OPTION_MOMIT_LOCK_PREFIX},
e4e00185 13149 {"mfence-as-lock-add", required_argument, NULL, OPTION_MFENCE_AS_LOCK_ADD},
0cb4071e 13150 {"mrelax-relocations", required_argument, NULL, OPTION_MRELAX_RELOCATIONS},
d3d3c6db 13151 {"mevexrcig", required_argument, NULL, OPTION_MEVEXRCIG},
e379e5f3
L
13152 {"malign-branch-boundary", required_argument, NULL, OPTION_MALIGN_BRANCH_BOUNDARY},
13153 {"malign-branch-prefix-size", required_argument, NULL, OPTION_MALIGN_BRANCH_PREFIX_SIZE},
13154 {"malign-branch", required_argument, NULL, OPTION_MALIGN_BRANCH},
76cf450b 13155 {"mbranches-within-32B-boundaries", no_argument, NULL, OPTION_MBRANCHES_WITH_32B_BOUNDARIES},
ae531041
L
13156 {"mlfence-after-load", required_argument, NULL, OPTION_MLFENCE_AFTER_LOAD},
13157 {"mlfence-before-indirect-branch", required_argument, NULL,
13158 OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH},
13159 {"mlfence-before-ret", required_argument, NULL, OPTION_MLFENCE_BEFORE_RET},
5db04b09
L
13160 {"mamd64", no_argument, NULL, OPTION_MAMD64},
13161 {"mintel64", no_argument, NULL, OPTION_MINTEL64},
252b5132
RH
13162 {NULL, no_argument, NULL, 0}
13163};
13164size_t md_longopts_size = sizeof (md_longopts);
13165
13166int
17b9d67d 13167md_parse_option (int c, const char *arg)
252b5132 13168{
91d6fa6a 13169 unsigned int j;
e379e5f3 13170 char *arch, *next, *saved, *type;
9103f4f4 13171
252b5132
RH
13172 switch (c)
13173 {
12b55ccc
L
13174 case 'n':
13175 optimize_align_code = 0;
13176 break;
13177
a38cf1db
AM
13178 case 'q':
13179 quiet_warnings = 1;
252b5132
RH
13180 break;
13181
13182#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
a38cf1db
AM
13183 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
13184 should be emitted or not. FIXME: Not implemented. */
13185 case 'Q':
d4693039
JB
13186 if ((arg[0] != 'y' && arg[0] != 'n') || arg[1])
13187 return 0;
252b5132
RH
13188 break;
13189
13190 /* -V: SVR4 argument to print version ID. */
13191 case 'V':
13192 print_version_id ();
13193 break;
13194
a38cf1db
AM
13195 /* -k: Ignore for FreeBSD compatibility. */
13196 case 'k':
252b5132 13197 break;
4cc782b5
ILT
13198
13199 case 's':
13200 /* -s: On i386 Solaris, this tells the native assembler to use
29b0f896 13201 .stab instead of .stab.excl. We always use .stab anyhow. */
4cc782b5 13202 break;
8dcea932
L
13203
13204 case OPTION_MSHARED:
13205 shared = 1;
13206 break;
b4a3a7b4
L
13207
13208 case OPTION_X86_USED_NOTE:
13209 if (strcasecmp (arg, "yes") == 0)
13210 x86_used_note = 1;
13211 else if (strcasecmp (arg, "no") == 0)
13212 x86_used_note = 0;
13213 else
13214 as_fatal (_("invalid -mx86-used-note= option: `%s'"), arg);
13215 break;
13216
13217
99ad8390 13218#endif
321098a5 13219#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 13220 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c
JH
13221 case OPTION_64:
13222 {
13223 const char **list, **l;
13224
3e73aa7c
JH
13225 list = bfd_target_list ();
13226 for (l = list; *l != NULL; l++)
08dedd66 13227 if (startswith (*l, "elf64-x86-64")
99ad8390
NC
13228 || strcmp (*l, "coff-x86-64") == 0
13229 || strcmp (*l, "pe-x86-64") == 0
d382c579
TG
13230 || strcmp (*l, "pei-x86-64") == 0
13231 || strcmp (*l, "mach-o-x86-64") == 0)
6e0b89ee
AM
13232 {
13233 default_arch = "x86_64";
13234 break;
13235 }
3e73aa7c 13236 if (*l == NULL)
2b5d6a91 13237 as_fatal (_("no compiled in support for x86_64"));
3e73aa7c
JH
13238 free (list);
13239 }
13240 break;
13241#endif
252b5132 13242
351f65ca 13243#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 13244 case OPTION_X32:
351f65ca
L
13245 if (IS_ELF)
13246 {
13247 const char **list, **l;
13248
13249 list = bfd_target_list ();
13250 for (l = list; *l != NULL; l++)
08dedd66 13251 if (startswith (*l, "elf32-x86-64"))
351f65ca
L
13252 {
13253 default_arch = "x86_64:32";
13254 break;
13255 }
13256 if (*l == NULL)
2b5d6a91 13257 as_fatal (_("no compiled in support for 32bit x86_64"));
351f65ca
L
13258 free (list);
13259 }
13260 else
13261 as_fatal (_("32bit x86_64 is only supported for ELF"));
13262 break;
13263#endif
13264
6e0b89ee
AM
13265 case OPTION_32:
13266 default_arch = "i386";
13267 break;
13268
b3b91714
AM
13269 case OPTION_DIVIDE:
13270#ifdef SVR4_COMMENT_CHARS
13271 {
13272 char *n, *t;
13273 const char *s;
13274
add39d23 13275 n = XNEWVEC (char, strlen (i386_comment_chars) + 1);
b3b91714
AM
13276 t = n;
13277 for (s = i386_comment_chars; *s != '\0'; s++)
13278 if (*s != '/')
13279 *t++ = *s;
13280 *t = '\0';
13281 i386_comment_chars = n;
13282 }
13283#endif
13284 break;
13285
9103f4f4 13286 case OPTION_MARCH:
293f5f65
L
13287 saved = xstrdup (arg);
13288 arch = saved;
13289 /* Allow -march=+nosse. */
13290 if (*arch == '+')
13291 arch++;
6305a203 13292 do
9103f4f4 13293 {
6305a203 13294 if (*arch == '.')
2b5d6a91 13295 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
13296 next = strchr (arch, '+');
13297 if (next)
13298 *next++ = '\0';
91d6fa6a 13299 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 13300 {
ae89daec
JB
13301 if (arch == saved && cpu_arch[j].type != PROCESSOR_NONE
13302 && strcmp (arch, cpu_arch[j].name) == 0)
ccc9c027 13303 {
6305a203 13304 /* Processor. */
ae89daec 13305 if (! cpu_arch[j].enable.bitfield.cpui386)
1ded5609
JB
13306 continue;
13307
91d6fa6a 13308 cpu_arch_name = cpu_arch[j].name;
d92c7521 13309 free (cpu_sub_arch_name);
6305a203 13310 cpu_sub_arch_name = NULL;
ae89daec 13311 cpu_arch_flags = cpu_arch[j].enable;
91d6fa6a 13312 cpu_arch_isa = cpu_arch[j].type;
ae89daec 13313 cpu_arch_isa_flags = cpu_arch[j].enable;
6305a203
L
13314 if (!cpu_arch_tune_set)
13315 {
13316 cpu_arch_tune = cpu_arch_isa;
13317 cpu_arch_tune_flags = cpu_arch_isa_flags;
13318 }
13319 break;
13320 }
ae89daec
JB
13321 else if (cpu_arch[j].type == PROCESSOR_NONE
13322 && strcmp (arch, cpu_arch[j].name) == 0
13323 && !cpu_flags_all_zero (&cpu_arch[j].enable))
6305a203 13324 {
33eaf5de 13325 /* ISA extension. */
6305a203 13326 i386_cpu_flags flags;
309d3373 13327
293f5f65 13328 flags = cpu_flags_or (cpu_arch_flags,
ae89daec 13329 cpu_arch[j].enable);
81486035 13330
5b64d091 13331 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
6305a203 13332 {
ae89daec 13333 extend_cpu_sub_arch_name (arch);
6305a203 13334 cpu_arch_flags = flags;
a586129e 13335 cpu_arch_isa_flags = flags;
6305a203 13336 }
0089dace
L
13337 else
13338 cpu_arch_isa_flags
13339 = cpu_flags_or (cpu_arch_isa_flags,
ae89daec 13340 cpu_arch[j].enable);
6305a203 13341 break;
ccc9c027 13342 }
9103f4f4 13343 }
6305a203 13344
ae89daec 13345 if (j >= ARRAY_SIZE (cpu_arch) && startswith (arch, "no"))
293f5f65 13346 {
33eaf5de 13347 /* Disable an ISA extension. */
ae89daec
JB
13348 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13349 if (cpu_arch[j].type == PROCESSOR_NONE
13350 && strcmp (arch + 2, cpu_arch[j].name) == 0)
293f5f65
L
13351 {
13352 i386_cpu_flags flags;
13353
13354 flags = cpu_flags_and_not (cpu_arch_flags,
ae89daec 13355 cpu_arch[j].disable);
293f5f65
L
13356 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
13357 {
8180707f 13358 extend_cpu_sub_arch_name (arch);
293f5f65
L
13359 cpu_arch_flags = flags;
13360 cpu_arch_isa_flags = flags;
13361 }
13362 break;
13363 }
293f5f65
L
13364 }
13365
91d6fa6a 13366 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 13367 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
13368
13369 arch = next;
9103f4f4 13370 }
293f5f65
L
13371 while (next != NULL);
13372 free (saved);
9103f4f4
L
13373 break;
13374
13375 case OPTION_MTUNE:
13376 if (*arg == '.')
2b5d6a91 13377 as_fatal (_("invalid -mtune= option: `%s'"), arg);
91d6fa6a 13378 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 13379 {
ae89daec
JB
13380 if (cpu_arch[j].type != PROCESSOR_NONE
13381 && strcmp (arg, cpu_arch[j].name) == 0)
9103f4f4 13382 {
ccc9c027 13383 cpu_arch_tune_set = 1;
91d6fa6a 13384 cpu_arch_tune = cpu_arch [j].type;
ae89daec 13385 cpu_arch_tune_flags = cpu_arch[j].enable;
9103f4f4
L
13386 break;
13387 }
13388 }
91d6fa6a 13389 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 13390 as_fatal (_("invalid -mtune= option: `%s'"), arg);
9103f4f4
L
13391 break;
13392
1efbbeb4
L
13393 case OPTION_MMNEMONIC:
13394 if (strcasecmp (arg, "att") == 0)
13395 intel_mnemonic = 0;
13396 else if (strcasecmp (arg, "intel") == 0)
13397 intel_mnemonic = 1;
13398 else
2b5d6a91 13399 as_fatal (_("invalid -mmnemonic= option: `%s'"), arg);
1efbbeb4
L
13400 break;
13401
13402 case OPTION_MSYNTAX:
13403 if (strcasecmp (arg, "att") == 0)
13404 intel_syntax = 0;
13405 else if (strcasecmp (arg, "intel") == 0)
13406 intel_syntax = 1;
13407 else
2b5d6a91 13408 as_fatal (_("invalid -msyntax= option: `%s'"), arg);
1efbbeb4
L
13409 break;
13410
13411 case OPTION_MINDEX_REG:
13412 allow_index_reg = 1;
13413 break;
13414
13415 case OPTION_MNAKED_REG:
13416 allow_naked_reg = 1;
13417 break;
13418
c0f3af97
L
13419 case OPTION_MSSE2AVX:
13420 sse2avx = 1;
13421 break;
13422
c8480b58
L
13423 case OPTION_MUSE_UNALIGNED_VECTOR_MOVE:
13424 use_unaligned_vector_move = 1;
13425 break;
13426
daf50ae7
L
13427 case OPTION_MSSE_CHECK:
13428 if (strcasecmp (arg, "error") == 0)
7bab8ab5 13429 sse_check = check_error;
daf50ae7 13430 else if (strcasecmp (arg, "warning") == 0)
7bab8ab5 13431 sse_check = check_warning;
daf50ae7 13432 else if (strcasecmp (arg, "none") == 0)
7bab8ab5 13433 sse_check = check_none;
daf50ae7 13434 else
2b5d6a91 13435 as_fatal (_("invalid -msse-check= option: `%s'"), arg);
daf50ae7
L
13436 break;
13437
7bab8ab5
JB
13438 case OPTION_MOPERAND_CHECK:
13439 if (strcasecmp (arg, "error") == 0)
13440 operand_check = check_error;
13441 else if (strcasecmp (arg, "warning") == 0)
13442 operand_check = check_warning;
13443 else if (strcasecmp (arg, "none") == 0)
13444 operand_check = check_none;
13445 else
13446 as_fatal (_("invalid -moperand-check= option: `%s'"), arg);
13447 break;
13448
539f890d
L
13449 case OPTION_MAVXSCALAR:
13450 if (strcasecmp (arg, "128") == 0)
13451 avxscalar = vex128;
13452 else if (strcasecmp (arg, "256") == 0)
13453 avxscalar = vex256;
13454 else
2b5d6a91 13455 as_fatal (_("invalid -mavxscalar= option: `%s'"), arg);
539f890d
L
13456 break;
13457
03751133
L
13458 case OPTION_MVEXWIG:
13459 if (strcmp (arg, "0") == 0)
40c9c8de 13460 vexwig = vexw0;
03751133 13461 else if (strcmp (arg, "1") == 0)
40c9c8de 13462 vexwig = vexw1;
03751133
L
13463 else
13464 as_fatal (_("invalid -mvexwig= option: `%s'"), arg);
13465 break;
13466
7e8b059b
L
13467 case OPTION_MADD_BND_PREFIX:
13468 add_bnd_prefix = 1;
13469 break;
13470
43234a1e
L
13471 case OPTION_MEVEXLIG:
13472 if (strcmp (arg, "128") == 0)
13473 evexlig = evexl128;
13474 else if (strcmp (arg, "256") == 0)
13475 evexlig = evexl256;
13476 else if (strcmp (arg, "512") == 0)
13477 evexlig = evexl512;
13478 else
13479 as_fatal (_("invalid -mevexlig= option: `%s'"), arg);
13480 break;
13481
d3d3c6db
IT
13482 case OPTION_MEVEXRCIG:
13483 if (strcmp (arg, "rne") == 0)
13484 evexrcig = rne;
13485 else if (strcmp (arg, "rd") == 0)
13486 evexrcig = rd;
13487 else if (strcmp (arg, "ru") == 0)
13488 evexrcig = ru;
13489 else if (strcmp (arg, "rz") == 0)
13490 evexrcig = rz;
13491 else
13492 as_fatal (_("invalid -mevexrcig= option: `%s'"), arg);
13493 break;
13494
43234a1e
L
13495 case OPTION_MEVEXWIG:
13496 if (strcmp (arg, "0") == 0)
13497 evexwig = evexw0;
13498 else if (strcmp (arg, "1") == 0)
13499 evexwig = evexw1;
13500 else
13501 as_fatal (_("invalid -mevexwig= option: `%s'"), arg);
13502 break;
13503
167ad85b
TG
13504# if defined (TE_PE) || defined (TE_PEP)
13505 case OPTION_MBIG_OBJ:
13506 use_big_obj = 1;
13507 break;
13508#endif
13509
d1982f93 13510 case OPTION_MOMIT_LOCK_PREFIX:
d022bddd
IT
13511 if (strcasecmp (arg, "yes") == 0)
13512 omit_lock_prefix = 1;
13513 else if (strcasecmp (arg, "no") == 0)
13514 omit_lock_prefix = 0;
13515 else
13516 as_fatal (_("invalid -momit-lock-prefix= option: `%s'"), arg);
13517 break;
13518
e4e00185
AS
13519 case OPTION_MFENCE_AS_LOCK_ADD:
13520 if (strcasecmp (arg, "yes") == 0)
13521 avoid_fence = 1;
13522 else if (strcasecmp (arg, "no") == 0)
13523 avoid_fence = 0;
13524 else
13525 as_fatal (_("invalid -mfence-as-lock-add= option: `%s'"), arg);
13526 break;
13527
ae531041
L
13528 case OPTION_MLFENCE_AFTER_LOAD:
13529 if (strcasecmp (arg, "yes") == 0)
13530 lfence_after_load = 1;
13531 else if (strcasecmp (arg, "no") == 0)
13532 lfence_after_load = 0;
13533 else
13534 as_fatal (_("invalid -mlfence-after-load= option: `%s'"), arg);
13535 break;
13536
13537 case OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH:
13538 if (strcasecmp (arg, "all") == 0)
a09f656b 13539 {
13540 lfence_before_indirect_branch = lfence_branch_all;
13541 if (lfence_before_ret == lfence_before_ret_none)
13542 lfence_before_ret = lfence_before_ret_shl;
13543 }
ae531041
L
13544 else if (strcasecmp (arg, "memory") == 0)
13545 lfence_before_indirect_branch = lfence_branch_memory;
13546 else if (strcasecmp (arg, "register") == 0)
13547 lfence_before_indirect_branch = lfence_branch_register;
13548 else if (strcasecmp (arg, "none") == 0)
13549 lfence_before_indirect_branch = lfence_branch_none;
13550 else
13551 as_fatal (_("invalid -mlfence-before-indirect-branch= option: `%s'"),
13552 arg);
13553 break;
13554
13555 case OPTION_MLFENCE_BEFORE_RET:
13556 if (strcasecmp (arg, "or") == 0)
13557 lfence_before_ret = lfence_before_ret_or;
13558 else if (strcasecmp (arg, "not") == 0)
13559 lfence_before_ret = lfence_before_ret_not;
a09f656b 13560 else if (strcasecmp (arg, "shl") == 0 || strcasecmp (arg, "yes") == 0)
13561 lfence_before_ret = lfence_before_ret_shl;
ae531041
L
13562 else if (strcasecmp (arg, "none") == 0)
13563 lfence_before_ret = lfence_before_ret_none;
13564 else
13565 as_fatal (_("invalid -mlfence-before-ret= option: `%s'"),
13566 arg);
13567 break;
13568
0cb4071e
L
13569 case OPTION_MRELAX_RELOCATIONS:
13570 if (strcasecmp (arg, "yes") == 0)
13571 generate_relax_relocations = 1;
13572 else if (strcasecmp (arg, "no") == 0)
13573 generate_relax_relocations = 0;
13574 else
13575 as_fatal (_("invalid -mrelax-relocations= option: `%s'"), arg);
13576 break;
13577
e379e5f3
L
13578 case OPTION_MALIGN_BRANCH_BOUNDARY:
13579 {
13580 char *end;
13581 long int align = strtoul (arg, &end, 0);
13582 if (*end == '\0')
13583 {
13584 if (align == 0)
13585 {
13586 align_branch_power = 0;
13587 break;
13588 }
13589 else if (align >= 16)
13590 {
13591 int align_power;
13592 for (align_power = 0;
13593 (align & 1) == 0;
13594 align >>= 1, align_power++)
13595 continue;
13596 /* Limit alignment power to 31. */
13597 if (align == 1 && align_power < 32)
13598 {
13599 align_branch_power = align_power;
13600 break;
13601 }
13602 }
13603 }
13604 as_fatal (_("invalid -malign-branch-boundary= value: %s"), arg);
13605 }
13606 break;
13607
13608 case OPTION_MALIGN_BRANCH_PREFIX_SIZE:
13609 {
13610 char *end;
13611 int align = strtoul (arg, &end, 0);
13612 /* Some processors only support 5 prefixes. */
13613 if (*end == '\0' && align >= 0 && align < 6)
13614 {
13615 align_branch_prefix_size = align;
13616 break;
13617 }
13618 as_fatal (_("invalid -malign-branch-prefix-size= value: %s"),
13619 arg);
13620 }
13621 break;
13622
13623 case OPTION_MALIGN_BRANCH:
13624 align_branch = 0;
13625 saved = xstrdup (arg);
13626 type = saved;
13627 do
13628 {
13629 next = strchr (type, '+');
13630 if (next)
13631 *next++ = '\0';
13632 if (strcasecmp (type, "jcc") == 0)
13633 align_branch |= align_branch_jcc_bit;
13634 else if (strcasecmp (type, "fused") == 0)
13635 align_branch |= align_branch_fused_bit;
13636 else if (strcasecmp (type, "jmp") == 0)
13637 align_branch |= align_branch_jmp_bit;
13638 else if (strcasecmp (type, "call") == 0)
13639 align_branch |= align_branch_call_bit;
13640 else if (strcasecmp (type, "ret") == 0)
13641 align_branch |= align_branch_ret_bit;
13642 else if (strcasecmp (type, "indirect") == 0)
13643 align_branch |= align_branch_indirect_bit;
13644 else
13645 as_fatal (_("invalid -malign-branch= option: `%s'"), arg);
13646 type = next;
13647 }
13648 while (next != NULL);
13649 free (saved);
13650 break;
13651
76cf450b
L
13652 case OPTION_MBRANCHES_WITH_32B_BOUNDARIES:
13653 align_branch_power = 5;
13654 align_branch_prefix_size = 5;
13655 align_branch = (align_branch_jcc_bit
13656 | align_branch_fused_bit
13657 | align_branch_jmp_bit);
13658 break;
13659
5db04b09 13660 case OPTION_MAMD64:
4b5aaf5f 13661 isa64 = amd64;
5db04b09
L
13662 break;
13663
13664 case OPTION_MINTEL64:
4b5aaf5f 13665 isa64 = intel64;
5db04b09
L
13666 break;
13667
b6f8c7c4
L
13668 case 'O':
13669 if (arg == NULL)
13670 {
13671 optimize = 1;
13672 /* Turn off -Os. */
13673 optimize_for_space = 0;
13674 }
13675 else if (*arg == 's')
13676 {
13677 optimize_for_space = 1;
13678 /* Turn on all encoding optimizations. */
41fd2579 13679 optimize = INT_MAX;
b6f8c7c4
L
13680 }
13681 else
13682 {
13683 optimize = atoi (arg);
13684 /* Turn off -Os. */
13685 optimize_for_space = 0;
13686 }
13687 break;
13688
252b5132
RH
13689 default:
13690 return 0;
13691 }
13692 return 1;
13693}
13694
8a2c8fef
L
13695#define MESSAGE_TEMPLATE \
13696" "
13697
293f5f65
L
13698static char *
13699output_message (FILE *stream, char *p, char *message, char *start,
13700 int *left_p, const char *name, int len)
13701{
13702 int size = sizeof (MESSAGE_TEMPLATE);
13703 int left = *left_p;
13704
13705 /* Reserve 2 spaces for ", " or ",\0" */
13706 left -= len + 2;
13707
13708 /* Check if there is any room. */
13709 if (left >= 0)
13710 {
13711 if (p != start)
13712 {
13713 *p++ = ',';
13714 *p++ = ' ';
13715 }
13716 p = mempcpy (p, name, len);
13717 }
13718 else
13719 {
13720 /* Output the current message now and start a new one. */
13721 *p++ = ',';
13722 *p = '\0';
13723 fprintf (stream, "%s\n", message);
13724 p = start;
13725 left = size - (start - message) - len - 2;
13726
13727 gas_assert (left >= 0);
13728
13729 p = mempcpy (p, name, len);
13730 }
13731
13732 *left_p = left;
13733 return p;
13734}
13735
8a2c8fef 13736static void
1ded5609 13737show_arch (FILE *stream, int ext, int check)
8a2c8fef
L
13738{
13739 static char message[] = MESSAGE_TEMPLATE;
13740 char *start = message + 27;
13741 char *p;
13742 int size = sizeof (MESSAGE_TEMPLATE);
13743 int left;
13744 const char *name;
13745 int len;
13746 unsigned int j;
13747
13748 p = start;
13749 left = size - (start - message);
3ce2ebcf
JB
13750
13751 if (!ext && check)
13752 {
13753 p = output_message (stream, p, message, start, &left,
13754 STRING_COMMA_LEN ("default"));
f68697e8
JB
13755 p = output_message (stream, p, message, start, &left,
13756 STRING_COMMA_LEN ("push"));
13757 p = output_message (stream, p, message, start, &left,
13758 STRING_COMMA_LEN ("pop"));
3ce2ebcf
JB
13759 }
13760
8a2c8fef
L
13761 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13762 {
13763 /* Should it be skipped? */
13764 if (cpu_arch [j].skip)
13765 continue;
13766
13767 name = cpu_arch [j].name;
13768 len = cpu_arch [j].len;
ae89daec 13769 if (cpu_arch[j].type == PROCESSOR_NONE)
8a2c8fef
L
13770 {
13771 /* It is an extension. Skip if we aren't asked to show it. */
ae89daec 13772 if (!ext || cpu_flags_all_zero (&cpu_arch[j].enable))
8a2c8fef
L
13773 continue;
13774 }
13775 else if (ext)
13776 {
13777 /* It is an processor. Skip if we show only extension. */
13778 continue;
13779 }
ae89daec 13780 else if (check && ! cpu_arch[j].enable.bitfield.cpui386)
1ded5609
JB
13781 {
13782 /* It is an impossible processor - skip. */
13783 continue;
13784 }
8a2c8fef 13785
293f5f65 13786 p = output_message (stream, p, message, start, &left, name, len);
8a2c8fef
L
13787 }
13788
293f5f65
L
13789 /* Display disabled extensions. */
13790 if (ext)
ae89daec 13791 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
293f5f65 13792 {
ae89daec
JB
13793 char *str;
13794
13795 if (cpu_arch[j].type != PROCESSOR_NONE
13796 || !cpu_flags_all_zero (&cpu_arch[j].enable))
13797 continue;
13798 str = xasprintf ("no%s", cpu_arch[j].name);
13799 p = output_message (stream, p, message, start, &left, str,
13800 strlen (str));
13801 free (str);
293f5f65
L
13802 }
13803
8a2c8fef
L
13804 *p = '\0';
13805 fprintf (stream, "%s\n", message);
13806}
13807
252b5132 13808void
8a2c8fef 13809md_show_usage (FILE *stream)
252b5132 13810{
4cc782b5
ILT
13811#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13812 fprintf (stream, _("\
d4693039 13813 -Qy, -Qn ignored\n\
a38cf1db 13814 -V print assembler version number\n\
b3b91714
AM
13815 -k ignored\n"));
13816#endif
13817 fprintf (stream, _("\
12b55ccc 13818 -n Do not optimize code alignment\n\
b3b91714
AM
13819 -q quieten some warnings\n"));
13820#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13821 fprintf (stream, _("\
a38cf1db 13822 -s ignored\n"));
b3b91714 13823#endif
b00af7c8
JB
13824#ifdef BFD64
13825# if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13826 fprintf (stream, _("\
13827 --32/--64/--x32 generate 32bit/64bit/x32 object\n"));
13828# elif defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O)
751d281c 13829 fprintf (stream, _("\
b00af7c8
JB
13830 --32/--64 generate 32bit/64bit object\n"));
13831# endif
751d281c 13832#endif
b3b91714
AM
13833#ifdef SVR4_COMMENT_CHARS
13834 fprintf (stream, _("\
13835 --divide do not treat `/' as a comment character\n"));
a38cf1db
AM
13836#else
13837 fprintf (stream, _("\
b3b91714 13838 --divide ignored\n"));
4cc782b5 13839#endif
9103f4f4 13840 fprintf (stream, _("\
6305a203 13841 -march=CPU[,+EXTENSION...]\n\
8a2c8fef 13842 generate code for CPU and EXTENSION, CPU is one of:\n"));
1ded5609 13843 show_arch (stream, 0, 1);
8a2c8fef 13844 fprintf (stream, _("\
ae89daec 13845 EXTENSION is combination of (possibly \"no\"-prefixed):\n"));
1ded5609 13846 show_arch (stream, 1, 0);
6305a203 13847 fprintf (stream, _("\
8a2c8fef 13848 -mtune=CPU optimize for CPU, CPU is one of:\n"));
1ded5609 13849 show_arch (stream, 0, 0);
ba104c83 13850 fprintf (stream, _("\
c0f3af97
L
13851 -msse2avx encode SSE instructions with VEX prefix\n"));
13852 fprintf (stream, _("\
c8480b58
L
13853 -muse-unaligned-vector-move\n\
13854 encode aligned vector move as unaligned vector move\n"));
13855 fprintf (stream, _("\
7c5c05ef 13856 -msse-check=[none|error|warning] (default: warning)\n\
daf50ae7
L
13857 check SSE instructions\n"));
13858 fprintf (stream, _("\
7c5c05ef 13859 -moperand-check=[none|error|warning] (default: warning)\n\
7bab8ab5
JB
13860 check operand combinations for validity\n"));
13861 fprintf (stream, _("\
7c5c05ef
L
13862 -mavxscalar=[128|256] (default: 128)\n\
13863 encode scalar AVX instructions with specific vector\n\
539f890d
L
13864 length\n"));
13865 fprintf (stream, _("\
03751133
L
13866 -mvexwig=[0|1] (default: 0)\n\
13867 encode VEX instructions with specific VEX.W value\n\
13868 for VEX.W bit ignored instructions\n"));
13869 fprintf (stream, _("\
7c5c05ef
L
13870 -mevexlig=[128|256|512] (default: 128)\n\
13871 encode scalar EVEX instructions with specific vector\n\
43234a1e
L
13872 length\n"));
13873 fprintf (stream, _("\
7c5c05ef
L
13874 -mevexwig=[0|1] (default: 0)\n\
13875 encode EVEX instructions with specific EVEX.W value\n\
43234a1e
L
13876 for EVEX.W bit ignored instructions\n"));
13877 fprintf (stream, _("\
7c5c05ef 13878 -mevexrcig=[rne|rd|ru|rz] (default: rne)\n\
d3d3c6db
IT
13879 encode EVEX instructions with specific EVEX.RC value\n\
13880 for SAE-only ignored instructions\n"));
13881 fprintf (stream, _("\
7c5c05ef
L
13882 -mmnemonic=[att|intel] "));
13883 if (SYSV386_COMPAT)
13884 fprintf (stream, _("(default: att)\n"));
13885 else
13886 fprintf (stream, _("(default: intel)\n"));
13887 fprintf (stream, _("\
13888 use AT&T/Intel mnemonic\n"));
ba104c83 13889 fprintf (stream, _("\
7c5c05ef
L
13890 -msyntax=[att|intel] (default: att)\n\
13891 use AT&T/Intel syntax\n"));
ba104c83
L
13892 fprintf (stream, _("\
13893 -mindex-reg support pseudo index registers\n"));
13894 fprintf (stream, _("\
13895 -mnaked-reg don't require `%%' prefix for registers\n"));
13896 fprintf (stream, _("\
7e8b059b 13897 -madd-bnd-prefix add BND prefix for all valid branches\n"));
b4a3a7b4 13898#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8dcea932
L
13899 fprintf (stream, _("\
13900 -mshared disable branch optimization for shared code\n"));
b4a3a7b4
L
13901 fprintf (stream, _("\
13902 -mx86-used-note=[no|yes] "));
13903 if (DEFAULT_X86_USED_NOTE)
13904 fprintf (stream, _("(default: yes)\n"));
13905 else
13906 fprintf (stream, _("(default: no)\n"));
13907 fprintf (stream, _("\
13908 generate x86 used ISA and feature properties\n"));
13909#endif
13910#if defined (TE_PE) || defined (TE_PEP)
167ad85b
TG
13911 fprintf (stream, _("\
13912 -mbig-obj generate big object files\n"));
13913#endif
d022bddd 13914 fprintf (stream, _("\
7c5c05ef 13915 -momit-lock-prefix=[no|yes] (default: no)\n\
d022bddd 13916 strip all lock prefixes\n"));
5db04b09 13917 fprintf (stream, _("\
7c5c05ef 13918 -mfence-as-lock-add=[no|yes] (default: no)\n\
e4e00185
AS
13919 encode lfence, mfence and sfence as\n\
13920 lock addl $0x0, (%%{re}sp)\n"));
13921 fprintf (stream, _("\
7c5c05ef
L
13922 -mrelax-relocations=[no|yes] "));
13923 if (DEFAULT_GENERATE_X86_RELAX_RELOCATIONS)
13924 fprintf (stream, _("(default: yes)\n"));
13925 else
13926 fprintf (stream, _("(default: no)\n"));
13927 fprintf (stream, _("\
0cb4071e
L
13928 generate relax relocations\n"));
13929 fprintf (stream, _("\
e379e5f3
L
13930 -malign-branch-boundary=NUM (default: 0)\n\
13931 align branches within NUM byte boundary\n"));
13932 fprintf (stream, _("\
13933 -malign-branch=TYPE[+TYPE...] (default: jcc+fused+jmp)\n\
13934 TYPE is combination of jcc, fused, jmp, call, ret,\n\
13935 indirect\n\
13936 specify types of branches to align\n"));
13937 fprintf (stream, _("\
13938 -malign-branch-prefix-size=NUM (default: 5)\n\
13939 align branches with NUM prefixes per instruction\n"));
13940 fprintf (stream, _("\
76cf450b
L
13941 -mbranches-within-32B-boundaries\n\
13942 align branches within 32 byte boundary\n"));
13943 fprintf (stream, _("\
ae531041
L
13944 -mlfence-after-load=[no|yes] (default: no)\n\
13945 generate lfence after load\n"));
13946 fprintf (stream, _("\
13947 -mlfence-before-indirect-branch=[none|all|register|memory] (default: none)\n\
13948 generate lfence before indirect near branch\n"));
13949 fprintf (stream, _("\
a09f656b 13950 -mlfence-before-ret=[none|or|not|shl|yes] (default: none)\n\
ae531041
L
13951 generate lfence before ret\n"));
13952 fprintf (stream, _("\
7c5c05ef 13953 -mamd64 accept only AMD64 ISA [default]\n"));
5db04b09
L
13954 fprintf (stream, _("\
13955 -mintel64 accept only Intel64 ISA\n"));
252b5132
RH
13956}
13957
3e73aa7c 13958#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
321098a5 13959 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
e57f8c65 13960 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
252b5132
RH
13961
13962/* Pick the target format to use. */
13963
47926f60 13964const char *
e3bb37b5 13965i386_target_format (void)
252b5132 13966{
d34049e8 13967 if (startswith (default_arch, "x86_64"))
351f65ca
L
13968 {
13969 update_code_flag (CODE_64BIT, 1);
13970 if (default_arch[6] == '\0')
7f56bc95 13971 x86_elf_abi = X86_64_ABI;
351f65ca 13972 else
7f56bc95 13973 x86_elf_abi = X86_64_X32_ABI;
351f65ca 13974 }
3e73aa7c 13975 else if (!strcmp (default_arch, "i386"))
78f12dd3 13976 update_code_flag (CODE_32BIT, 1);
5197d474
L
13977 else if (!strcmp (default_arch, "iamcu"))
13978 {
13979 update_code_flag (CODE_32BIT, 1);
13980 if (cpu_arch_isa == PROCESSOR_UNKNOWN)
13981 {
13982 static const i386_cpu_flags iamcu_flags = CPU_IAMCU_FLAGS;
13983 cpu_arch_name = "iamcu";
d92c7521 13984 free (cpu_sub_arch_name);
5197d474
L
13985 cpu_sub_arch_name = NULL;
13986 cpu_arch_flags = iamcu_flags;
13987 cpu_arch_isa = PROCESSOR_IAMCU;
13988 cpu_arch_isa_flags = iamcu_flags;
13989 if (!cpu_arch_tune_set)
13990 {
13991 cpu_arch_tune = cpu_arch_isa;
13992 cpu_arch_tune_flags = cpu_arch_isa_flags;
13993 }
13994 }
8d471ec1 13995 else if (cpu_arch_isa != PROCESSOR_IAMCU)
5197d474
L
13996 as_fatal (_("Intel MCU doesn't support `%s' architecture"),
13997 cpu_arch_name);
13998 }
3e73aa7c 13999 else
2b5d6a91 14000 as_fatal (_("unknown architecture"));
89507696
JB
14001
14002 if (cpu_flags_all_zero (&cpu_arch_isa_flags))
ae89daec 14003 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
89507696 14004 if (cpu_flags_all_zero (&cpu_arch_tune_flags))
ae89daec 14005 cpu_arch_tune_flags = cpu_arch[flag_code == CODE_64BIT].enable;
89507696 14006
252b5132
RH
14007 switch (OUTPUT_FLAVOR)
14008 {
9384f2ff 14009#if defined (OBJ_MAYBE_AOUT) || defined (OBJ_AOUT)
4c63da97 14010 case bfd_target_aout_flavour:
47926f60 14011 return AOUT_TARGET_FORMAT;
4c63da97 14012#endif
9384f2ff
AM
14013#if defined (OBJ_MAYBE_COFF) || defined (OBJ_COFF)
14014# if defined (TE_PE) || defined (TE_PEP)
14015 case bfd_target_coff_flavour:
167ad85b 14016 if (flag_code == CODE_64BIT)
eb19308f
JB
14017 {
14018 object_64bit = 1;
14019 return use_big_obj ? "pe-bigobj-x86-64" : "pe-x86-64";
14020 }
14021 return use_big_obj ? "pe-bigobj-i386" : "pe-i386";
9384f2ff 14022# elif defined (TE_GO32)
0561d57c
JK
14023 case bfd_target_coff_flavour:
14024 return "coff-go32";
9384f2ff 14025# else
252b5132
RH
14026 case bfd_target_coff_flavour:
14027 return "coff-i386";
9384f2ff 14028# endif
4c63da97 14029#endif
3e73aa7c 14030#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
252b5132 14031 case bfd_target_elf_flavour:
3e73aa7c 14032 {
351f65ca
L
14033 const char *format;
14034
14035 switch (x86_elf_abi)
4fa24527 14036 {
351f65ca
L
14037 default:
14038 format = ELF_TARGET_FORMAT;
e379e5f3
L
14039#ifndef TE_SOLARIS
14040 tls_get_addr = "___tls_get_addr";
14041#endif
351f65ca 14042 break;
7f56bc95 14043 case X86_64_ABI:
351f65ca 14044 use_rela_relocations = 1;
4fa24527 14045 object_64bit = 1;
e379e5f3
L
14046#ifndef TE_SOLARIS
14047 tls_get_addr = "__tls_get_addr";
14048#endif
351f65ca
L
14049 format = ELF_TARGET_FORMAT64;
14050 break;
7f56bc95 14051 case X86_64_X32_ABI:
4fa24527 14052 use_rela_relocations = 1;
351f65ca 14053 object_64bit = 1;
e379e5f3
L
14054#ifndef TE_SOLARIS
14055 tls_get_addr = "__tls_get_addr";
14056#endif
862be3fb 14057 disallow_64bit_reloc = 1;
351f65ca
L
14058 format = ELF_TARGET_FORMAT32;
14059 break;
4fa24527 14060 }
c085ab00 14061 if (cpu_arch_isa == PROCESSOR_IAMCU)
81486035
L
14062 {
14063 if (x86_elf_abi != I386_ABI)
14064 as_fatal (_("Intel MCU is 32bit only"));
14065 return ELF_TARGET_IAMCU_FORMAT;
14066 }
8a9036a4 14067 else
351f65ca 14068 return format;
3e73aa7c 14069 }
e57f8c65
TG
14070#endif
14071#if defined (OBJ_MACH_O)
14072 case bfd_target_mach_o_flavour:
d382c579
TG
14073 if (flag_code == CODE_64BIT)
14074 {
14075 use_rela_relocations = 1;
14076 object_64bit = 1;
14077 return "mach-o-x86-64";
14078 }
14079 else
14080 return "mach-o-i386";
4c63da97 14081#endif
252b5132
RH
14082 default:
14083 abort ();
14084 return NULL;
14085 }
14086}
14087
47926f60 14088#endif /* OBJ_MAYBE_ more than one */
252b5132 14089\f
252b5132 14090symbolS *
7016a5d5 14091md_undefined_symbol (char *name)
252b5132 14092{
18dc2407
ILT
14093 if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
14094 && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
14095 && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
14096 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
24eab124
AM
14097 {
14098 if (!GOT_symbol)
14099 {
14100 if (symbol_find (name))
14101 as_bad (_("GOT already in symbol table"));
14102 GOT_symbol = symbol_new (name, undefined_section,
e01e1cee 14103 &zero_address_frag, 0);
24eab124
AM
14104 };
14105 return GOT_symbol;
14106 }
252b5132
RH
14107 return 0;
14108}
14109
14110/* Round up a section size to the appropriate boundary. */
47926f60 14111
252b5132 14112valueT
7016a5d5 14113md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
252b5132 14114{
4c63da97
AM
14115#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
14116 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
14117 {
14118 /* For a.out, force the section size to be aligned. If we don't do
14119 this, BFD will align it for us, but it will not write out the
14120 final bytes of the section. This may be a bug in BFD, but it is
14121 easier to fix it here since that is how the other a.out targets
14122 work. */
14123 int align;
14124
fd361982 14125 align = bfd_section_alignment (segment);
8d3842cd 14126 size = ((size + (1 << align) - 1) & (-((valueT) 1 << align)));
4c63da97 14127 }
252b5132
RH
14128#endif
14129
14130 return size;
14131}
14132
14133/* On the i386, PC-relative offsets are relative to the start of the
14134 next instruction. That is, the address of the offset, plus its
14135 size, since the offset is always the last part of the insn. */
14136
14137long
e3bb37b5 14138md_pcrel_from (fixS *fixP)
252b5132
RH
14139{
14140 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
14141}
14142
14143#ifndef I386COFF
14144
14145static void
e3bb37b5 14146s_bss (int ignore ATTRIBUTE_UNUSED)
252b5132 14147{
29b0f896 14148 int temp;
252b5132 14149
8a75718c
JB
14150#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14151 if (IS_ELF)
14152 obj_elf_section_change_hook ();
14153#endif
252b5132
RH
14154 temp = get_absolute_expression ();
14155 subseg_set (bss_section, (subsegT) temp);
14156 demand_empty_rest_of_line ();
14157}
14158
14159#endif
14160
e379e5f3
L
14161/* Remember constant directive. */
14162
14163void
14164i386_cons_align (int ignore ATTRIBUTE_UNUSED)
14165{
14166 if (last_insn.kind != last_insn_directive
14167 && (bfd_section_flags (now_seg) & SEC_CODE))
14168 {
14169 last_insn.seg = now_seg;
14170 last_insn.kind = last_insn_directive;
14171 last_insn.name = "constant directive";
14172 last_insn.file = as_where (&last_insn.line);
ae531041
L
14173 if (lfence_before_ret != lfence_before_ret_none)
14174 {
14175 if (lfence_before_indirect_branch != lfence_branch_none)
14176 as_warn (_("constant directive skips -mlfence-before-ret "
14177 "and -mlfence-before-indirect-branch"));
14178 else
14179 as_warn (_("constant directive skips -mlfence-before-ret"));
14180 }
14181 else if (lfence_before_indirect_branch != lfence_branch_none)
14182 as_warn (_("constant directive skips -mlfence-before-indirect-branch"));
e379e5f3
L
14183 }
14184}
14185
3abbafc2 14186int
e3bb37b5 14187i386_validate_fix (fixS *fixp)
252b5132 14188{
e52a16f2
JB
14189 if (fixp->fx_addsy && S_GET_SEGMENT(fixp->fx_addsy) == reg_section)
14190 {
14191 reloc_howto_type *howto;
14192
14193 howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
14194 as_bad_where (fixp->fx_file, fixp->fx_line,
14195 _("invalid %s relocation against register"),
14196 howto ? howto->name : "<unknown>");
14197 return 0;
14198 }
14199
3abbafc2
JB
14200#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14201 if (fixp->fx_r_type == BFD_RELOC_SIZE32
14202 || fixp->fx_r_type == BFD_RELOC_SIZE64)
14203 return IS_ELF && fixp->fx_addsy
14204 && (!S_IS_DEFINED (fixp->fx_addsy)
14205 || S_IS_EXTERNAL (fixp->fx_addsy));
14206#endif
14207
02a86693 14208 if (fixp->fx_subsy)
252b5132 14209 {
02a86693 14210 if (fixp->fx_subsy == GOT_symbol)
23df1078 14211 {
02a86693
L
14212 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
14213 {
14214 if (!object_64bit)
14215 abort ();
14216#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14217 if (fixp->fx_tcbit2)
56ceb5b5
L
14218 fixp->fx_r_type = (fixp->fx_tcbit
14219 ? BFD_RELOC_X86_64_REX_GOTPCRELX
14220 : BFD_RELOC_X86_64_GOTPCRELX);
02a86693
L
14221 else
14222#endif
14223 fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
14224 }
d6ab8113 14225 else
02a86693
L
14226 {
14227 if (!object_64bit)
14228 fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
14229 else
14230 fixp->fx_r_type = BFD_RELOC_X86_64_GOTOFF64;
14231 }
14232 fixp->fx_subsy = 0;
23df1078 14233 }
252b5132 14234 }
02a86693 14235#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2585b7a5 14236 else
02a86693 14237 {
2585b7a5
L
14238 /* NB: Commit 292676c1 resolved PLT32 reloc aganst local symbol
14239 to section. Since PLT32 relocation must be against symbols,
14240 turn such PLT32 relocation into PC32 relocation. */
14241 if (fixp->fx_addsy
14242 && (fixp->fx_r_type == BFD_RELOC_386_PLT32
14243 || fixp->fx_r_type == BFD_RELOC_X86_64_PLT32)
14244 && symbol_section_p (fixp->fx_addsy))
14245 fixp->fx_r_type = BFD_RELOC_32_PCREL;
14246 if (!object_64bit)
14247 {
14248 if (fixp->fx_r_type == BFD_RELOC_386_GOT32
14249 && fixp->fx_tcbit2)
14250 fixp->fx_r_type = BFD_RELOC_386_GOT32X;
14251 }
02a86693
L
14252 }
14253#endif
3abbafc2
JB
14254
14255 return 1;
252b5132
RH
14256}
14257
252b5132 14258arelent *
7016a5d5 14259tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
14260{
14261 arelent *rel;
14262 bfd_reloc_code_real_type code;
14263
14264 switch (fixp->fx_r_type)
14265 {
8ce3d284 14266#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3abbafc2
JB
14267 symbolS *sym;
14268
8fd4256d
L
14269 case BFD_RELOC_SIZE32:
14270 case BFD_RELOC_SIZE64:
3abbafc2
JB
14271 if (fixp->fx_addsy
14272 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))
14273 && (!fixp->fx_subsy
14274 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))))
14275 sym = fixp->fx_addsy;
14276 else if (fixp->fx_subsy
14277 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))
14278 && (!fixp->fx_addsy
14279 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))))
14280 sym = fixp->fx_subsy;
14281 else
14282 sym = NULL;
14283 if (IS_ELF && sym && S_IS_DEFINED (sym) && !S_IS_EXTERNAL (sym))
8fd4256d
L
14284 {
14285 /* Resolve size relocation against local symbol to size of
14286 the symbol plus addend. */
3abbafc2 14287 valueT value = S_GET_SIZE (sym);
44f87162 14288
3abbafc2
JB
14289 if (symbol_get_bfdsym (sym)->flags & BSF_SECTION_SYM)
14290 value = bfd_section_size (S_GET_SEGMENT (sym));
14291 if (sym == fixp->fx_subsy)
14292 {
14293 value = -value;
14294 if (fixp->fx_addsy)
14295 value += S_GET_VALUE (fixp->fx_addsy);
14296 }
14297 else if (fixp->fx_subsy)
14298 value -= S_GET_VALUE (fixp->fx_subsy);
44f87162 14299 value += fixp->fx_offset;
8fd4256d 14300 if (fixp->fx_r_type == BFD_RELOC_SIZE32
d965814f 14301 && object_64bit
8fd4256d
L
14302 && !fits_in_unsigned_long (value))
14303 as_bad_where (fixp->fx_file, fixp->fx_line,
14304 _("symbol size computation overflow"));
14305 fixp->fx_addsy = NULL;
14306 fixp->fx_subsy = NULL;
14307 md_apply_fix (fixp, (valueT *) &value, NULL);
14308 return NULL;
14309 }
3abbafc2
JB
14310 if (!fixp->fx_addsy || fixp->fx_subsy)
14311 {
14312 as_bad_where (fixp->fx_file, fixp->fx_line,
14313 "unsupported expression involving @size");
14314 return NULL;
14315 }
8ce3d284 14316#endif
1a0670f3 14317 /* Fall through. */
8fd4256d 14318
3e73aa7c
JH
14319 case BFD_RELOC_X86_64_PLT32:
14320 case BFD_RELOC_X86_64_GOT32:
14321 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
14322 case BFD_RELOC_X86_64_GOTPCRELX:
14323 case BFD_RELOC_X86_64_REX_GOTPCRELX:
252b5132
RH
14324 case BFD_RELOC_386_PLT32:
14325 case BFD_RELOC_386_GOT32:
02a86693 14326 case BFD_RELOC_386_GOT32X:
252b5132
RH
14327 case BFD_RELOC_386_GOTOFF:
14328 case BFD_RELOC_386_GOTPC:
13ae64f3
JJ
14329 case BFD_RELOC_386_TLS_GD:
14330 case BFD_RELOC_386_TLS_LDM:
14331 case BFD_RELOC_386_TLS_LDO_32:
14332 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
14333 case BFD_RELOC_386_TLS_IE:
14334 case BFD_RELOC_386_TLS_GOTIE:
13ae64f3
JJ
14335 case BFD_RELOC_386_TLS_LE_32:
14336 case BFD_RELOC_386_TLS_LE:
67a4f2b7
AO
14337 case BFD_RELOC_386_TLS_GOTDESC:
14338 case BFD_RELOC_386_TLS_DESC_CALL:
bffbf940
JJ
14339 case BFD_RELOC_X86_64_TLSGD:
14340 case BFD_RELOC_X86_64_TLSLD:
14341 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 14342 case BFD_RELOC_X86_64_DTPOFF64:
bffbf940
JJ
14343 case BFD_RELOC_X86_64_GOTTPOFF:
14344 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113
JB
14345 case BFD_RELOC_X86_64_TPOFF64:
14346 case BFD_RELOC_X86_64_GOTOFF64:
14347 case BFD_RELOC_X86_64_GOTPC32:
7b81dfbb
AJ
14348 case BFD_RELOC_X86_64_GOT64:
14349 case BFD_RELOC_X86_64_GOTPCREL64:
14350 case BFD_RELOC_X86_64_GOTPC64:
14351 case BFD_RELOC_X86_64_GOTPLT64:
14352 case BFD_RELOC_X86_64_PLTOFF64:
67a4f2b7
AO
14353 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14354 case BFD_RELOC_X86_64_TLSDESC_CALL:
252b5132
RH
14355 case BFD_RELOC_RVA:
14356 case BFD_RELOC_VTABLE_ENTRY:
14357 case BFD_RELOC_VTABLE_INHERIT:
6482c264
NC
14358#ifdef TE_PE
14359 case BFD_RELOC_32_SECREL:
145667f8 14360 case BFD_RELOC_16_SECIDX:
6482c264 14361#endif
252b5132
RH
14362 code = fixp->fx_r_type;
14363 break;
dbbaec26
L
14364 case BFD_RELOC_X86_64_32S:
14365 if (!fixp->fx_pcrel)
14366 {
14367 /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32. */
14368 code = fixp->fx_r_type;
14369 break;
14370 }
1a0670f3 14371 /* Fall through. */
252b5132 14372 default:
93382f6d 14373 if (fixp->fx_pcrel)
252b5132 14374 {
93382f6d
AM
14375 switch (fixp->fx_size)
14376 {
14377 default:
b091f402
AM
14378 as_bad_where (fixp->fx_file, fixp->fx_line,
14379 _("can not do %d byte pc-relative relocation"),
14380 fixp->fx_size);
93382f6d
AM
14381 code = BFD_RELOC_32_PCREL;
14382 break;
14383 case 1: code = BFD_RELOC_8_PCREL; break;
14384 case 2: code = BFD_RELOC_16_PCREL; break;
d258b828 14385 case 4: code = BFD_RELOC_32_PCREL; break;
d6ab8113
JB
14386#ifdef BFD64
14387 case 8: code = BFD_RELOC_64_PCREL; break;
14388#endif
93382f6d
AM
14389 }
14390 }
14391 else
14392 {
14393 switch (fixp->fx_size)
14394 {
14395 default:
b091f402
AM
14396 as_bad_where (fixp->fx_file, fixp->fx_line,
14397 _("can not do %d byte relocation"),
14398 fixp->fx_size);
93382f6d
AM
14399 code = BFD_RELOC_32;
14400 break;
14401 case 1: code = BFD_RELOC_8; break;
14402 case 2: code = BFD_RELOC_16; break;
14403 case 4: code = BFD_RELOC_32; break;
937149dd 14404#ifdef BFD64
3e73aa7c 14405 case 8: code = BFD_RELOC_64; break;
937149dd 14406#endif
93382f6d 14407 }
252b5132
RH
14408 }
14409 break;
14410 }
252b5132 14411
d182319b
JB
14412 if ((code == BFD_RELOC_32
14413 || code == BFD_RELOC_32_PCREL
14414 || code == BFD_RELOC_X86_64_32S)
252b5132
RH
14415 && GOT_symbol
14416 && fixp->fx_addsy == GOT_symbol)
3e73aa7c 14417 {
4fa24527 14418 if (!object_64bit)
d6ab8113
JB
14419 code = BFD_RELOC_386_GOTPC;
14420 else
14421 code = BFD_RELOC_X86_64_GOTPC32;
3e73aa7c 14422 }
7b81dfbb
AJ
14423 if ((code == BFD_RELOC_64 || code == BFD_RELOC_64_PCREL)
14424 && GOT_symbol
14425 && fixp->fx_addsy == GOT_symbol)
14426 {
14427 code = BFD_RELOC_X86_64_GOTPC64;
14428 }
252b5132 14429
add39d23
TS
14430 rel = XNEW (arelent);
14431 rel->sym_ptr_ptr = XNEW (asymbol *);
49309057 14432 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
14433
14434 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
c87db184 14435
3e73aa7c
JH
14436 if (!use_rela_relocations)
14437 {
14438 /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
14439 vtable entry to be used in the relocation's section offset. */
14440 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
14441 rel->address = fixp->fx_offset;
fbeb56a4
DK
14442#if defined (OBJ_COFF) && defined (TE_PE)
14443 else if (fixp->fx_addsy && S_IS_WEAK (fixp->fx_addsy))
14444 rel->addend = fixp->fx_addnumber - (S_GET_VALUE (fixp->fx_addsy) * 2);
14445 else
14446#endif
c6682705 14447 rel->addend = 0;
3e73aa7c
JH
14448 }
14449 /* Use the rela in 64bit mode. */
252b5132 14450 else
3e73aa7c 14451 {
862be3fb
L
14452 if (disallow_64bit_reloc)
14453 switch (code)
14454 {
862be3fb
L
14455 case BFD_RELOC_X86_64_DTPOFF64:
14456 case BFD_RELOC_X86_64_TPOFF64:
14457 case BFD_RELOC_64_PCREL:
14458 case BFD_RELOC_X86_64_GOTOFF64:
14459 case BFD_RELOC_X86_64_GOT64:
14460 case BFD_RELOC_X86_64_GOTPCREL64:
14461 case BFD_RELOC_X86_64_GOTPC64:
14462 case BFD_RELOC_X86_64_GOTPLT64:
14463 case BFD_RELOC_X86_64_PLTOFF64:
14464 as_bad_where (fixp->fx_file, fixp->fx_line,
14465 _("cannot represent relocation type %s in x32 mode"),
14466 bfd_get_reloc_code_name (code));
14467 break;
14468 default:
14469 break;
14470 }
14471
062cd5e7
AS
14472 if (!fixp->fx_pcrel)
14473 rel->addend = fixp->fx_offset;
14474 else
14475 switch (code)
14476 {
14477 case BFD_RELOC_X86_64_PLT32:
14478 case BFD_RELOC_X86_64_GOT32:
14479 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
14480 case BFD_RELOC_X86_64_GOTPCRELX:
14481 case BFD_RELOC_X86_64_REX_GOTPCRELX:
bffbf940
JJ
14482 case BFD_RELOC_X86_64_TLSGD:
14483 case BFD_RELOC_X86_64_TLSLD:
14484 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7
AO
14485 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14486 case BFD_RELOC_X86_64_TLSDESC_CALL:
062cd5e7
AS
14487 rel->addend = fixp->fx_offset - fixp->fx_size;
14488 break;
14489 default:
14490 rel->addend = (section->vma
14491 - fixp->fx_size
14492 + fixp->fx_addnumber
14493 + md_pcrel_from (fixp));
14494 break;
14495 }
3e73aa7c
JH
14496 }
14497
252b5132
RH
14498 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
14499 if (rel->howto == NULL)
14500 {
14501 as_bad_where (fixp->fx_file, fixp->fx_line,
d0b47220 14502 _("cannot represent relocation type %s"),
252b5132
RH
14503 bfd_get_reloc_code_name (code));
14504 /* Set howto to a garbage value so that we can keep going. */
14505 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
9c2799c2 14506 gas_assert (rel->howto != NULL);
252b5132
RH
14507 }
14508
14509 return rel;
14510}
14511
ee86248c 14512#include "tc-i386-intel.c"
54cfded0 14513
a60de03c
JB
14514void
14515tc_x86_parse_to_dw2regnum (expressionS *exp)
54cfded0 14516{
a60de03c
JB
14517 int saved_naked_reg;
14518 char saved_register_dot;
54cfded0 14519
a60de03c
JB
14520 saved_naked_reg = allow_naked_reg;
14521 allow_naked_reg = 1;
14522 saved_register_dot = register_chars['.'];
14523 register_chars['.'] = '.';
14524 allow_pseudo_reg = 1;
14525 expression_and_evaluate (exp);
14526 allow_pseudo_reg = 0;
14527 register_chars['.'] = saved_register_dot;
14528 allow_naked_reg = saved_naked_reg;
14529
e96d56a1 14530 if (exp->X_op == O_register && exp->X_add_number >= 0)
54cfded0 14531 {
a60de03c
JB
14532 if ((addressT) exp->X_add_number < i386_regtab_size)
14533 {
14534 exp->X_op = O_constant;
14535 exp->X_add_number = i386_regtab[exp->X_add_number]
14536 .dw2_regnum[flag_code >> 1];
14537 }
14538 else
14539 exp->X_op = O_illegal;
54cfded0 14540 }
54cfded0
AM
14541}
14542
14543void
14544tc_x86_frame_initial_instructions (void)
14545{
a60de03c
JB
14546 static unsigned int sp_regno[2];
14547
14548 if (!sp_regno[flag_code >> 1])
14549 {
14550 char *saved_input = input_line_pointer;
14551 char sp[][4] = {"esp", "rsp"};
14552 expressionS exp;
a4447b93 14553
a60de03c
JB
14554 input_line_pointer = sp[flag_code >> 1];
14555 tc_x86_parse_to_dw2regnum (&exp);
9c2799c2 14556 gas_assert (exp.X_op == O_constant);
a60de03c
JB
14557 sp_regno[flag_code >> 1] = exp.X_add_number;
14558 input_line_pointer = saved_input;
14559 }
a4447b93 14560
61ff971f
L
14561 cfi_add_CFA_def_cfa (sp_regno[flag_code >> 1], -x86_cie_data_alignment);
14562 cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
54cfded0 14563}
d2b2c203 14564
d7921315
L
14565int
14566x86_dwarf2_addr_size (void)
14567{
14568#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
14569 if (x86_elf_abi == X86_64_X32_ABI)
14570 return 4;
14571#endif
14572 return bfd_arch_bits_per_address (stdoutput) / 8;
14573}
14574
d2b2c203
DJ
14575int
14576i386_elf_section_type (const char *str, size_t len)
14577{
14578 if (flag_code == CODE_64BIT
14579 && len == sizeof ("unwind") - 1
d34049e8 14580 && startswith (str, "unwind"))
d2b2c203
DJ
14581 return SHT_X86_64_UNWIND;
14582
14583 return -1;
14584}
bb41ade5 14585
ad5fec3b
EB
14586#ifdef TE_SOLARIS
14587void
14588i386_solaris_fix_up_eh_frame (segT sec)
14589{
14590 if (flag_code == CODE_64BIT)
14591 elf_section_type (sec) = SHT_X86_64_UNWIND;
14592}
14593#endif
14594
bb41ade5
AM
14595#ifdef TE_PE
14596void
14597tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
14598{
91d6fa6a 14599 expressionS exp;
bb41ade5 14600
91d6fa6a
NC
14601 exp.X_op = O_secrel;
14602 exp.X_add_symbol = symbol;
14603 exp.X_add_number = 0;
14604 emit_expr (&exp, size);
bb41ade5
AM
14605}
14606#endif
3b22753a
L
14607
14608#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14609/* For ELF on x86-64, add support for SHF_X86_64_LARGE. */
14610
01e1a5bc 14611bfd_vma
6d4af3c2 14612x86_64_section_letter (int letter, const char **ptr_msg)
3b22753a
L
14613{
14614 if (flag_code == CODE_64BIT)
14615 {
14616 if (letter == 'l')
14617 return SHF_X86_64_LARGE;
14618
8f3bae45 14619 *ptr_msg = _("bad .section directive: want a,l,w,x,M,S,G,T in string");
64e74474 14620 }
3b22753a 14621 else
8f3bae45 14622 *ptr_msg = _("bad .section directive: want a,w,x,M,S,G,T in string");
3b22753a
L
14623 return -1;
14624}
14625
01e1a5bc 14626bfd_vma
3b22753a
L
14627x86_64_section_word (char *str, size_t len)
14628{
08dedd66 14629 if (len == 5 && flag_code == CODE_64BIT && startswith (str, "large"))
3b22753a
L
14630 return SHF_X86_64_LARGE;
14631
14632 return -1;
14633}
14634
14635static void
14636handle_large_common (int small ATTRIBUTE_UNUSED)
14637{
14638 if (flag_code != CODE_64BIT)
14639 {
14640 s_comm_internal (0, elf_common_parse);
14641 as_warn (_(".largecomm supported only in 64bit mode, producing .comm"));
14642 }
14643 else
14644 {
14645 static segT lbss_section;
14646 asection *saved_com_section_ptr = elf_com_section_ptr;
14647 asection *saved_bss_section = bss_section;
14648
14649 if (lbss_section == NULL)
14650 {
14651 flagword applicable;
14652 segT seg = now_seg;
14653 subsegT subseg = now_subseg;
14654
14655 /* The .lbss section is for local .largecomm symbols. */
14656 lbss_section = subseg_new (".lbss", 0);
14657 applicable = bfd_applicable_section_flags (stdoutput);
fd361982 14658 bfd_set_section_flags (lbss_section, applicable & SEC_ALLOC);
3b22753a
L
14659 seg_info (lbss_section)->bss = 1;
14660
14661 subseg_set (seg, subseg);
14662 }
14663
14664 elf_com_section_ptr = &_bfd_elf_large_com_section;
14665 bss_section = lbss_section;
14666
14667 s_comm_internal (0, elf_common_parse);
14668
14669 elf_com_section_ptr = saved_com_section_ptr;
14670 bss_section = saved_bss_section;
14671 }
14672}
14673#endif /* OBJ_ELF || OBJ_MAYBE_ELF */