]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/config/tc-i386.c
sim: Remove unused CXXFLAGS substitution
[thirdparty/binutils-gdb.git] / gas / config / tc-i386.c
CommitLineData
b534c6d3 1/* tc-i386.c -- Assemble code for the Intel 80386
a2c58332 2 Copyright (C) 1989-2022 Free Software Foundation, Inc.
252b5132
RH
3
4 This file is part of GAS, the GNU Assembler.
5
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
ec2655a6 8 the Free Software Foundation; either version 3, or (at your option)
252b5132
RH
9 any later version.
10
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to the Free
4b4da160
NC
18 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
19 02110-1301, USA. */
252b5132 20
47926f60
KH
21/* Intel 80386 machine specific gas.
22 Written by Eliot Dresselhaus (eliot@mgm.mit.edu).
3e73aa7c 23 x86_64 support by Jan Hubicka (jh@suse.cz)
0f10071e 24 VIA PadLock support by Michal Ludvig (mludvig@suse.cz)
47926f60
KH
25 Bugs & suggestions are completely welcome. This is free software.
26 Please help us make it better. */
252b5132 27
252b5132 28#include "as.h"
3882b010 29#include "safe-ctype.h"
252b5132 30#include "subsegs.h"
316e2c05 31#include "dwarf2dbg.h"
54cfded0 32#include "dw2gencfi.h"
d2b2c203 33#include "elf/x86-64.h"
40fb9820 34#include "opcodes/i386-init.h"
41fd2579 35#include <limits.h>
41fd2579 36
c3332e24 37#ifndef INFER_ADDR_PREFIX
eecb386c 38#define INFER_ADDR_PREFIX 1
c3332e24
AM
39#endif
40
29b0f896
AM
41#ifndef DEFAULT_ARCH
42#define DEFAULT_ARCH "i386"
246fcdee 43#endif
252b5132 44
edde18a5
AM
45#ifndef INLINE
46#if __GNUC__ >= 2
47#define INLINE __inline__
48#else
49#define INLINE
50#endif
51#endif
52
6305a203
L
53/* Prefixes will be emitted in the order defined below.
54 WAIT_PREFIX must be the first prefix since FWAIT is really is an
55 instruction, and so must come before any prefixes.
56 The preferred prefix order is SEG_PREFIX, ADDR_PREFIX, DATA_PREFIX,
42164a71 57 REP_PREFIX/HLE_PREFIX, LOCK_PREFIX. */
6305a203
L
58#define WAIT_PREFIX 0
59#define SEG_PREFIX 1
60#define ADDR_PREFIX 2
61#define DATA_PREFIX 3
c32fa91d 62#define REP_PREFIX 4
42164a71 63#define HLE_PREFIX REP_PREFIX
7e8b059b 64#define BND_PREFIX REP_PREFIX
c32fa91d 65#define LOCK_PREFIX 5
4e9ac44a
L
66#define REX_PREFIX 6 /* must come last. */
67#define MAX_PREFIXES 7 /* max prefixes per opcode */
6305a203
L
68
69/* we define the syntax here (modulo base,index,scale syntax) */
70#define REGISTER_PREFIX '%'
71#define IMMEDIATE_PREFIX '$'
72#define ABSOLUTE_PREFIX '*'
73
74/* these are the instruction mnemonic suffixes in AT&T syntax or
75 memory operand size in Intel syntax. */
76#define WORD_MNEM_SUFFIX 'w'
77#define BYTE_MNEM_SUFFIX 'b'
78#define SHORT_MNEM_SUFFIX 's'
79#define LONG_MNEM_SUFFIX 'l'
80#define QWORD_MNEM_SUFFIX 'q'
6305a203
L
81/* Intel Syntax. Use a non-ascii letter since since it never appears
82 in instructions. */
83#define LONG_DOUBLE_MNEM_SUFFIX '\1'
84
85#define END_OF_INSN '\0'
86
79dec6b7
JB
87/* This matches the C -> StaticRounding alias in the opcode table. */
88#define commutative staticrounding
89
6305a203
L
90/*
91 'templates' is for grouping together 'template' structures for opcodes
92 of the same name. This is only used for storing the insns in the grand
93 ole hash table of insns.
94 The templates themselves start at START and range up to (but not including)
95 END.
96 */
97typedef struct
98{
d3ce72d0
NC
99 const insn_template *start;
100 const insn_template *end;
6305a203
L
101}
102templates;
103
104/* 386 operand encoding bytes: see 386 book for details of this. */
105typedef struct
106{
107 unsigned int regmem; /* codes register or memory operand */
108 unsigned int reg; /* codes register operand (or extended opcode) */
109 unsigned int mode; /* how to interpret regmem & reg */
110}
111modrm_byte;
112
113/* x86-64 extension prefix. */
114typedef int rex_byte;
115
6305a203
L
116/* 386 opcode byte to code indirect addressing. */
117typedef struct
118{
119 unsigned base;
120 unsigned index;
121 unsigned scale;
122}
123sib_byte;
124
6305a203
L
125/* x86 arch names, types and features */
126typedef struct
127{
128 const char *name; /* arch name */
6ceeed25
JB
129 unsigned int len:8; /* arch string length */
130 bool skip:1; /* show_arch should skip this. */
6305a203 131 enum processor_type type; /* arch type */
ae89daec
JB
132 i386_cpu_flags enable; /* cpu feature enable flags */
133 i386_cpu_flags disable; /* cpu feature disable flags */
6305a203
L
134}
135arch_entry;
136
78f12dd3 137static void update_code_flag (int, int);
e3bb37b5
L
138static void set_code_flag (int);
139static void set_16bit_gcc_code_flag (int);
140static void set_intel_syntax (int);
1efbbeb4 141static void set_intel_mnemonic (int);
db51cc60 142static void set_allow_index_reg (int);
7bab8ab5 143static void set_check (int);
e3bb37b5 144static void set_cpu_arch (int);
6482c264 145#ifdef TE_PE
e3bb37b5 146static void pe_directive_secrel (int);
145667f8 147static void pe_directive_secidx (int);
6482c264 148#endif
e3bb37b5
L
149static void signed_cons (int);
150static char *output_invalid (int c);
ee86248c
JB
151static int i386_finalize_immediate (segT, expressionS *, i386_operand_type,
152 const char *);
153static int i386_finalize_displacement (segT, expressionS *, i386_operand_type,
154 const char *);
a7619375 155static int i386_att_operand (char *);
e3bb37b5 156static int i386_intel_operand (char *, int);
ee86248c
JB
157static int i386_intel_simplify (expressionS *);
158static int i386_intel_parse_name (const char *, expressionS *);
e3bb37b5
L
159static const reg_entry *parse_register (char *, char **);
160static char *parse_insn (char *, char *);
161static char *parse_operands (char *, const char *);
162static void swap_operands (void);
783c187b 163static void swap_2_operands (unsigned int, unsigned int);
48bcea9f 164static enum flag_code i386_addressing_mode (void);
e3bb37b5
L
165static void optimize_imm (void);
166static void optimize_disp (void);
83b16ac6 167static const insn_template *match_template (char);
e3bb37b5
L
168static int check_string (void);
169static int process_suffix (void);
170static int check_byte_reg (void);
171static int check_long_reg (void);
172static int check_qword_reg (void);
173static int check_word_reg (void);
174static int finalize_imm (void);
175static int process_operands (void);
5e042380 176static const reg_entry *build_modrm_byte (void);
e3bb37b5
L
177static void output_insn (void);
178static void output_imm (fragS *, offsetT);
179static void output_disp (fragS *, offsetT);
29b0f896 180#ifndef I386COFF
e3bb37b5 181static void s_bss (int);
252b5132 182#endif
17d4e2a2
L
183#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
184static void handle_large_common (int small ATTRIBUTE_UNUSED);
b4a3a7b4
L
185
186/* GNU_PROPERTY_X86_ISA_1_USED. */
187static unsigned int x86_isa_1_used;
188/* GNU_PROPERTY_X86_FEATURE_2_USED. */
189static unsigned int x86_feature_2_used;
190/* Generate x86 used ISA and feature properties. */
191static unsigned int x86_used_note = DEFAULT_X86_USED_NOTE;
17d4e2a2 192#endif
252b5132 193
a847613f 194static const char *default_arch = DEFAULT_ARCH;
3e73aa7c 195
8a6fb3f9
JB
196/* parse_register() returns this when a register alias cannot be used. */
197static const reg_entry bad_reg = { "<bad>", OPERAND_TYPE_NONE, 0, 0,
198 { Dw2Inval, Dw2Inval } };
199
34684862 200static const reg_entry *reg_eax;
5e042380
JB
201static const reg_entry *reg_ds;
202static const reg_entry *reg_es;
203static const reg_entry *reg_ss;
6288d05f 204static const reg_entry *reg_st0;
6225c532
JB
205static const reg_entry *reg_k0;
206
c0f3af97
L
207/* VEX prefix. */
208typedef struct
209{
43234a1e
L
210 /* VEX prefix is either 2 byte or 3 byte. EVEX is 4 byte. */
211 unsigned char bytes[4];
c0f3af97
L
212 unsigned int length;
213 /* Destination or source register specifier. */
214 const reg_entry *register_specifier;
215} vex_prefix;
216
252b5132 217/* 'md_assemble ()' gathers together information and puts it into a
47926f60 218 i386_insn. */
252b5132 219
520dc8e8
AM
220union i386_op
221 {
222 expressionS *disps;
223 expressionS *imms;
224 const reg_entry *regs;
225 };
226
a65babc9
L
227enum i386_error
228 {
b4d65f2d 229 no_error, /* Must be first. */
86e026a4 230 operand_size_mismatch,
a65babc9
L
231 operand_type_mismatch,
232 register_type_mismatch,
233 number_of_operands_mismatch,
234 invalid_instruction_suffix,
235 bad_imm4,
a65babc9
L
236 unsupported_with_intel_mnemonic,
237 unsupported_syntax,
6c30d220 238 unsupported,
260cd341 239 invalid_sib_address,
6c30d220 240 invalid_vsib_address,
7bab8ab5 241 invalid_vector_register_set,
260cd341 242 invalid_tmm_register_set,
0cc78721 243 invalid_dest_and_src_register_set,
43234a1e
L
244 unsupported_vector_index_register,
245 unsupported_broadcast,
43234a1e
L
246 broadcast_needed,
247 unsupported_masking,
248 mask_not_on_destination,
249 no_default_mask,
250 unsupported_rc_sae,
43234a1e 251 invalid_register_operand,
a65babc9
L
252 };
253
252b5132
RH
254struct _i386_insn
255 {
47926f60 256 /* TM holds the template for the insn were currently assembling. */
d3ce72d0 257 insn_template tm;
252b5132 258
7d5e4556
L
259 /* SUFFIX holds the instruction size suffix for byte, word, dword
260 or qword, if given. */
252b5132
RH
261 char suffix;
262
9a182d04
JB
263 /* OPCODE_LENGTH holds the number of base opcode bytes. */
264 unsigned char opcode_length;
265
47926f60 266 /* OPERANDS gives the number of given operands. */
252b5132
RH
267 unsigned int operands;
268
269 /* REG_OPERANDS, DISP_OPERANDS, MEM_OPERANDS, IMM_OPERANDS give the number
270 of given register, displacement, memory operands and immediate
47926f60 271 operands. */
252b5132
RH
272 unsigned int reg_operands, disp_operands, mem_operands, imm_operands;
273
274 /* TYPES [i] is the type (see above #defines) which tells us how to
520dc8e8 275 use OP[i] for the corresponding operand. */
40fb9820 276 i386_operand_type types[MAX_OPERANDS];
252b5132 277
520dc8e8
AM
278 /* Displacement expression, immediate expression, or register for each
279 operand. */
280 union i386_op op[MAX_OPERANDS];
252b5132 281
3e73aa7c
JH
282 /* Flags for operands. */
283 unsigned int flags[MAX_OPERANDS];
284#define Operand_PCrel 1
c48dadc9 285#define Operand_Mem 2
3e73aa7c 286
252b5132 287 /* Relocation type for operand */
f86103b7 288 enum bfd_reloc_code_real reloc[MAX_OPERANDS];
252b5132 289
252b5132
RH
290 /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
291 the base index byte below. */
292 const reg_entry *base_reg;
293 const reg_entry *index_reg;
294 unsigned int log2_scale_factor;
295
296 /* SEG gives the seg_entries of this insn. They are zero unless
47926f60 297 explicit segment overrides are given. */
5e042380 298 const reg_entry *seg[2];
252b5132 299
8325cc63
JB
300 /* Copied first memory operand string, for re-checking. */
301 char *memop1_string;
302
252b5132
RH
303 /* PREFIX holds all the given prefix opcodes (usually null).
304 PREFIXES is the number of prefix opcodes. */
305 unsigned int prefixes;
306 unsigned char prefix[MAX_PREFIXES];
307
50128d0c 308 /* Register is in low 3 bits of opcode. */
5b7c81bd 309 bool short_form;
50128d0c 310
6f2f06be 311 /* The operand to a branch insn indicates an absolute branch. */
5b7c81bd 312 bool jumpabsolute;
6f2f06be 313
921eafea
L
314 /* Extended states. */
315 enum
316 {
317 /* Use MMX state. */
318 xstate_mmx = 1 << 0,
319 /* Use XMM state. */
320 xstate_xmm = 1 << 1,
321 /* Use YMM state. */
322 xstate_ymm = 1 << 2 | xstate_xmm,
323 /* Use ZMM state. */
324 xstate_zmm = 1 << 3 | xstate_ymm,
325 /* Use TMM state. */
32930e4e
L
326 xstate_tmm = 1 << 4,
327 /* Use MASK state. */
328 xstate_mask = 1 << 5
921eafea 329 } xstate;
260cd341 330
e379e5f3 331 /* Has GOTPC or TLS relocation. */
5b7c81bd 332 bool has_gotpc_tls_reloc;
e379e5f3 333
252b5132 334 /* RM and SIB are the modrm byte and the sib byte where the
c1e679ec 335 addressing modes of this insn are encoded. */
252b5132 336 modrm_byte rm;
3e73aa7c 337 rex_byte rex;
43234a1e 338 rex_byte vrex;
252b5132 339 sib_byte sib;
c0f3af97 340 vex_prefix vex;
b6169b20 341
6225c532
JB
342 /* Masking attributes.
343
344 The struct describes masking, applied to OPERAND in the instruction.
345 REG is a pointer to the corresponding mask register. ZEROING tells
346 whether merging or zeroing mask is used. */
347 struct Mask_Operation
348 {
349 const reg_entry *reg;
350 unsigned int zeroing;
351 /* The operand where this operation is associated. */
352 unsigned int operand;
353 } mask;
43234a1e
L
354
355 /* Rounding control and SAE attributes. */
ca5312a2
JB
356 struct RC_Operation
357 {
358 enum rc_type
359 {
360 rc_none = -1,
361 rne,
362 rd,
363 ru,
364 rz,
365 saeonly
366 } type;
7063667e
JB
367 /* In Intel syntax the operand modifier form is supposed to be used, but
368 we continue to accept the immediate forms as well. */
369 bool modifier;
ca5312a2 370 } rounding;
43234a1e 371
5273a3cd
JB
372 /* Broadcasting attributes.
373
374 The struct describes broadcasting, applied to OPERAND. TYPE is
375 expresses the broadcast factor. */
376 struct Broadcast_Operation
377 {
0cc78721 378 /* Type of broadcast: {1to2}, {1to4}, {1to8}, {1to16} or {1to32}. */
5273a3cd
JB
379 unsigned int type;
380
381 /* Index of broadcasted operand. */
382 unsigned int operand;
383
384 /* Number of bytes to broadcast. */
385 unsigned int bytes;
386 } broadcast;
43234a1e
L
387
388 /* Compressed disp8*N attribute. */
389 unsigned int memshift;
390
86fa6981
L
391 /* Prefer load or store in encoding. */
392 enum
393 {
394 dir_encoding_default = 0,
395 dir_encoding_load,
64c49ab3
JB
396 dir_encoding_store,
397 dir_encoding_swap
86fa6981 398 } dir_encoding;
891edac4 399
41eb8e88 400 /* Prefer 8bit, 16bit, 32bit displacement in encoding. */
a501d77e
L
401 enum
402 {
403 disp_encoding_default = 0,
404 disp_encoding_8bit,
41eb8e88 405 disp_encoding_16bit,
a501d77e
L
406 disp_encoding_32bit
407 } disp_encoding;
f8a5c266 408
6b6b6807 409 /* Prefer the REX byte in encoding. */
5b7c81bd 410 bool rex_encoding;
6b6b6807 411
b6f8c7c4 412 /* Disable instruction size optimization. */
5b7c81bd 413 bool no_optimize;
b6f8c7c4 414
86fa6981
L
415 /* How to encode vector instructions. */
416 enum
417 {
418 vex_encoding_default = 0,
42e04b36 419 vex_encoding_vex,
86fa6981 420 vex_encoding_vex3,
da4977e0
JB
421 vex_encoding_evex,
422 vex_encoding_error
86fa6981
L
423 } vec_encoding;
424
d5de92cf
L
425 /* REP prefix. */
426 const char *rep_prefix;
427
165de32a
L
428 /* HLE prefix. */
429 const char *hle_prefix;
42164a71 430
7e8b059b
L
431 /* Have BND prefix. */
432 const char *bnd_prefix;
433
04ef582a
L
434 /* Have NOTRACK prefix. */
435 const char *notrack_prefix;
436
891edac4 437 /* Error message. */
a65babc9 438 enum i386_error error;
252b5132
RH
439 };
440
441typedef struct _i386_insn i386_insn;
442
43234a1e
L
443/* Link RC type with corresponding string, that'll be looked for in
444 asm. */
445struct RC_name
446{
447 enum rc_type type;
448 const char *name;
449 unsigned int len;
450};
451
452static const struct RC_name RC_NamesTable[] =
453{
454 { rne, STRING_COMMA_LEN ("rn-sae") },
455 { rd, STRING_COMMA_LEN ("rd-sae") },
456 { ru, STRING_COMMA_LEN ("ru-sae") },
457 { rz, STRING_COMMA_LEN ("rz-sae") },
458 { saeonly, STRING_COMMA_LEN ("sae") },
459};
460
252b5132
RH
461/* List of chars besides those in app.c:symbol_chars that can start an
462 operand. Used to prevent the scrubber eating vital white-space. */
86fa6981 463const char extra_symbol_chars[] = "*%-([{}"
252b5132 464#ifdef LEX_AT
32137342
NC
465 "@"
466#endif
467#ifdef LEX_QM
468 "?"
252b5132 469#endif
32137342 470 ;
252b5132 471
b3983e5f
JB
472#if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
473 && !defined (TE_GNU) \
474 && !defined (TE_LINUX) \
d85e70a3 475 && !defined (TE_Haiku) \
b3983e5f
JB
476 && !defined (TE_FreeBSD) \
477 && !defined (TE_DragonFly) \
478 && !defined (TE_NetBSD))
252b5132 479/* This array holds the chars that always start a comment. If the
b3b91714
AM
480 pre-processor is disabled, these aren't very useful. The option
481 --divide will remove '/' from this list. */
482const char *i386_comment_chars = "#/";
483#define SVR4_COMMENT_CHARS 1
252b5132 484#define PREFIX_SEPARATOR '\\'
252b5132 485
b3b91714
AM
486#else
487const char *i386_comment_chars = "#";
488#define PREFIX_SEPARATOR '/'
489#endif
490
252b5132
RH
491/* This array holds the chars that only start a comment at the beginning of
492 a line. If the line seems to have the form '# 123 filename'
ce8a8b2f
AM
493 .line and .file directives will appear in the pre-processed output.
494 Note that input_file.c hand checks for '#' at the beginning of the
252b5132 495 first line of the input file. This is because the compiler outputs
ce8a8b2f
AM
496 #NO_APP at the beginning of its output.
497 Also note that comments started like this one will always work if
252b5132 498 '/' isn't otherwise defined. */
b3b91714 499const char line_comment_chars[] = "#/";
252b5132 500
63a0b638 501const char line_separator_chars[] = ";";
252b5132 502
ce8a8b2f
AM
503/* Chars that can be used to separate mant from exp in floating point
504 nums. */
252b5132
RH
505const char EXP_CHARS[] = "eE";
506
ce8a8b2f
AM
507/* Chars that mean this number is a floating point constant
508 As in 0f12.456
509 or 0d1.2345e12. */
de133cf9 510const char FLT_CHARS[] = "fFdDxXhHbB";
252b5132 511
ce8a8b2f 512/* Tables for lexical analysis. */
252b5132
RH
513static char mnemonic_chars[256];
514static char register_chars[256];
515static char operand_chars[256];
516static char identifier_chars[256];
252b5132 517
ce8a8b2f 518/* Lexical macros. */
252b5132
RH
519#define is_mnemonic_char(x) (mnemonic_chars[(unsigned char) x])
520#define is_operand_char(x) (operand_chars[(unsigned char) x])
521#define is_register_char(x) (register_chars[(unsigned char) x])
522#define is_space_char(x) ((x) == ' ')
523#define is_identifier_char(x) (identifier_chars[(unsigned char) x])
252b5132 524
0234cb7c 525/* All non-digit non-letter characters that may occur in an operand. */
252b5132
RH
526static char operand_special_chars[] = "%$-+(,)*._~/<>|&^!:[@]";
527
528/* md_assemble() always leaves the strings it's passed unaltered. To
529 effect this we maintain a stack of saved characters that we've smashed
530 with '\0's (indicating end of strings for various sub-fields of the
47926f60 531 assembler instruction). */
252b5132 532static char save_stack[32];
ce8a8b2f 533static char *save_stack_p;
252b5132
RH
534#define END_STRING_AND_SAVE(s) \
535 do { *save_stack_p++ = *(s); *(s) = '\0'; } while (0)
536#define RESTORE_END_STRING(s) \
537 do { *(s) = *--save_stack_p; } while (0)
538
47926f60 539/* The instruction we're assembling. */
252b5132
RH
540static i386_insn i;
541
542/* Possible templates for current insn. */
543static const templates *current_templates;
544
31b2323c
L
545/* Per instruction expressionS buffers: max displacements & immediates. */
546static expressionS disp_expressions[MAX_MEMORY_OPERANDS];
547static expressionS im_expressions[MAX_IMMEDIATE_OPERANDS];
252b5132 548
47926f60 549/* Current operand we are working on. */
ee86248c 550static int this_operand = -1;
252b5132 551
3e73aa7c
JH
552/* We support four different modes. FLAG_CODE variable is used to distinguish
553 these. */
554
555enum flag_code {
556 CODE_32BIT,
557 CODE_16BIT,
558 CODE_64BIT };
559
560static enum flag_code flag_code;
4fa24527 561static unsigned int object_64bit;
862be3fb 562static unsigned int disallow_64bit_reloc;
3e73aa7c 563static int use_rela_relocations = 0;
e379e5f3
L
564/* __tls_get_addr/___tls_get_addr symbol for TLS. */
565static const char *tls_get_addr;
3e73aa7c 566
7af8ed2d
NC
567#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
568 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
569 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
570
351f65ca
L
571/* The ELF ABI to use. */
572enum x86_elf_abi
573{
574 I386_ABI,
7f56bc95
L
575 X86_64_ABI,
576 X86_64_X32_ABI
351f65ca
L
577};
578
579static enum x86_elf_abi x86_elf_abi = I386_ABI;
7af8ed2d 580#endif
351f65ca 581
167ad85b
TG
582#if defined (TE_PE) || defined (TE_PEP)
583/* Use big object file format. */
584static int use_big_obj = 0;
585#endif
586
8dcea932
L
587#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
588/* 1 if generating code for a shared library. */
589static int shared = 0;
590#endif
591
47926f60
KH
592/* 1 for intel syntax,
593 0 if att syntax. */
594static int intel_syntax = 0;
252b5132 595
4b5aaf5f
L
596static enum x86_64_isa
597{
598 amd64 = 1, /* AMD64 ISA. */
599 intel64 /* Intel64 ISA. */
600} isa64;
e89c5eaa 601
1efbbeb4
L
602/* 1 for intel mnemonic,
603 0 if att mnemonic. */
604static int intel_mnemonic = !SYSV386_COMPAT;
605
a60de03c
JB
606/* 1 if pseudo registers are permitted. */
607static int allow_pseudo_reg = 0;
608
47926f60
KH
609/* 1 if register prefix % not required. */
610static int allow_naked_reg = 0;
252b5132 611
33eaf5de 612/* 1 if the assembler should add BND prefix for all control-transferring
7e8b059b
L
613 instructions supporting it, even if this prefix wasn't specified
614 explicitly. */
615static int add_bnd_prefix = 0;
616
ba104c83 617/* 1 if pseudo index register, eiz/riz, is allowed . */
db51cc60
L
618static int allow_index_reg = 0;
619
d022bddd
IT
620/* 1 if the assembler should ignore LOCK prefix, even if it was
621 specified explicitly. */
622static int omit_lock_prefix = 0;
623
e4e00185
AS
624/* 1 if the assembler should encode lfence, mfence, and sfence as
625 "lock addl $0, (%{re}sp)". */
626static int avoid_fence = 0;
627
ae531041
L
628/* 1 if lfence should be inserted after every load. */
629static int lfence_after_load = 0;
630
631/* Non-zero if lfence should be inserted before indirect branch. */
632static enum lfence_before_indirect_branch_kind
633 {
634 lfence_branch_none = 0,
635 lfence_branch_register,
636 lfence_branch_memory,
637 lfence_branch_all
638 }
639lfence_before_indirect_branch;
640
641/* Non-zero if lfence should be inserted before ret. */
642static enum lfence_before_ret_kind
643 {
644 lfence_before_ret_none = 0,
645 lfence_before_ret_not,
a09f656b 646 lfence_before_ret_or,
647 lfence_before_ret_shl
ae531041
L
648 }
649lfence_before_ret;
650
651/* Types of previous instruction is .byte or prefix. */
e379e5f3
L
652static struct
653 {
654 segT seg;
655 const char *file;
656 const char *name;
657 unsigned int line;
658 enum last_insn_kind
659 {
660 last_insn_other = 0,
661 last_insn_directive,
662 last_insn_prefix
663 } kind;
664 } last_insn;
665
0cb4071e
L
666/* 1 if the assembler should generate relax relocations. */
667
668static int generate_relax_relocations
669 = DEFAULT_GENERATE_X86_RELAX_RELOCATIONS;
670
7bab8ab5 671static enum check_kind
daf50ae7 672 {
7bab8ab5
JB
673 check_none = 0,
674 check_warning,
675 check_error
daf50ae7 676 }
7bab8ab5 677sse_check, operand_check = check_warning;
daf50ae7 678
e379e5f3
L
679/* Non-zero if branches should be aligned within power of 2 boundary. */
680static int align_branch_power = 0;
681
682/* Types of branches to align. */
683enum align_branch_kind
684 {
685 align_branch_none = 0,
686 align_branch_jcc = 1,
687 align_branch_fused = 2,
688 align_branch_jmp = 3,
689 align_branch_call = 4,
690 align_branch_indirect = 5,
691 align_branch_ret = 6
692 };
693
694/* Type bits of branches to align. */
695enum align_branch_bit
696 {
697 align_branch_jcc_bit = 1 << align_branch_jcc,
698 align_branch_fused_bit = 1 << align_branch_fused,
699 align_branch_jmp_bit = 1 << align_branch_jmp,
700 align_branch_call_bit = 1 << align_branch_call,
701 align_branch_indirect_bit = 1 << align_branch_indirect,
702 align_branch_ret_bit = 1 << align_branch_ret
703 };
704
705static unsigned int align_branch = (align_branch_jcc_bit
706 | align_branch_fused_bit
707 | align_branch_jmp_bit);
708
79d72f45
HL
709/* Types of condition jump used by macro-fusion. */
710enum mf_jcc_kind
711 {
712 mf_jcc_jo = 0, /* base opcode 0x70 */
713 mf_jcc_jc, /* base opcode 0x72 */
714 mf_jcc_je, /* base opcode 0x74 */
715 mf_jcc_jna, /* base opcode 0x76 */
716 mf_jcc_js, /* base opcode 0x78 */
717 mf_jcc_jp, /* base opcode 0x7a */
718 mf_jcc_jl, /* base opcode 0x7c */
719 mf_jcc_jle, /* base opcode 0x7e */
720 };
721
722/* Types of compare flag-modifying insntructions used by macro-fusion. */
723enum mf_cmp_kind
724 {
725 mf_cmp_test_and, /* test/cmp */
726 mf_cmp_alu_cmp, /* add/sub/cmp */
727 mf_cmp_incdec /* inc/dec */
728 };
729
e379e5f3
L
730/* The maximum padding size for fused jcc. CMP like instruction can
731 be 9 bytes and jcc can be 6 bytes. Leave room just in case for
732 prefixes. */
733#define MAX_FUSED_JCC_PADDING_SIZE 20
734
735/* The maximum number of prefixes added for an instruction. */
736static unsigned int align_branch_prefix_size = 5;
737
b6f8c7c4
L
738/* Optimization:
739 1. Clear the REX_W bit with register operand if possible.
740 2. Above plus use 128bit vector instruction to clear the full vector
741 register.
742 */
743static int optimize = 0;
744
745/* Optimization:
746 1. Clear the REX_W bit with register operand if possible.
747 2. Above plus use 128bit vector instruction to clear the full vector
748 register.
749 3. Above plus optimize "test{q,l,w} $imm8,%r{64,32,16}" to
750 "testb $imm7,%r8".
751 */
752static int optimize_for_space = 0;
753
2ca3ace5
L
754/* Register prefix used for error message. */
755static const char *register_prefix = "%";
756
47926f60
KH
757/* Used in 16 bit gcc mode to add an l suffix to call, ret, enter,
758 leave, push, and pop instructions so that gcc has the same stack
759 frame as in 32 bit mode. */
760static char stackop_size = '\0';
eecb386c 761
12b55ccc
L
762/* Non-zero to optimize code alignment. */
763int optimize_align_code = 1;
764
47926f60
KH
765/* Non-zero to quieten some warnings. */
766static int quiet_warnings = 0;
a38cf1db 767
d59a54c2
JB
768/* Guard to avoid repeated warnings about non-16-bit code on 16-bit CPUs. */
769static bool pre_386_16bit_warned;
770
47926f60
KH
771/* CPU name. */
772static const char *cpu_arch_name = NULL;
6305a203 773static char *cpu_sub_arch_name = NULL;
a38cf1db 774
47926f60 775/* CPU feature flags. */
40fb9820
L
776static i386_cpu_flags cpu_arch_flags = CPU_UNKNOWN_FLAGS;
777
ccc9c027
L
778/* If we have selected a cpu we are generating instructions for. */
779static int cpu_arch_tune_set = 0;
780
9103f4f4 781/* Cpu we are generating instructions for. */
fbf3f584 782enum processor_type cpu_arch_tune = PROCESSOR_UNKNOWN;
9103f4f4
L
783
784/* CPU feature flags of cpu we are generating instructions for. */
40fb9820 785static i386_cpu_flags cpu_arch_tune_flags;
9103f4f4 786
ccc9c027 787/* CPU instruction set architecture used. */
fbf3f584 788enum processor_type cpu_arch_isa = PROCESSOR_UNKNOWN;
ccc9c027 789
9103f4f4 790/* CPU feature flags of instruction set architecture used. */
fbf3f584 791i386_cpu_flags cpu_arch_isa_flags;
9103f4f4 792
fddf5b5b
AM
793/* If set, conditional jumps are not automatically promoted to handle
794 larger than a byte offset. */
f68697e8 795static bool no_cond_jump_promotion = false;
fddf5b5b 796
c0f3af97
L
797/* Encode SSE instructions with VEX prefix. */
798static unsigned int sse2avx;
799
c8480b58
L
800/* Encode aligned vector move as unaligned vector move. */
801static unsigned int use_unaligned_vector_move;
802
539f890d
L
803/* Encode scalar AVX instructions with specific vector length. */
804static enum
805 {
806 vex128 = 0,
807 vex256
808 } avxscalar;
809
03751133
L
810/* Encode VEX WIG instructions with specific vex.w. */
811static enum
812 {
813 vexw0 = 0,
814 vexw1
815 } vexwig;
816
43234a1e
L
817/* Encode scalar EVEX LIG instructions with specific vector length. */
818static enum
819 {
820 evexl128 = 0,
821 evexl256,
822 evexl512
823 } evexlig;
824
825/* Encode EVEX WIG instructions with specific evex.w. */
826static enum
827 {
828 evexw0 = 0,
829 evexw1
830 } evexwig;
831
d3d3c6db
IT
832/* Value to encode in EVEX RC bits, for SAE-only instructions. */
833static enum rc_type evexrcig = rne;
834
29b0f896 835/* Pre-defined "_GLOBAL_OFFSET_TABLE_". */
87c245cc 836static symbolS *GOT_symbol;
29b0f896 837
a4447b93
RH
838/* The dwarf2 return column, adjusted for 32 or 64 bit. */
839unsigned int x86_dwarf2_return_column;
840
841/* The dwarf2 data alignment, adjusted for 32 or 64 bit. */
842int x86_cie_data_alignment;
843
252b5132 844/* Interface to relax_segment.
fddf5b5b
AM
845 There are 3 major relax states for 386 jump insns because the
846 different types of jumps add different sizes to frags when we're
e379e5f3
L
847 figuring out what sort of jump to choose to reach a given label.
848
849 BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING are used to align
850 branches which are handled by md_estimate_size_before_relax() and
851 i386_generic_table_relax_frag(). */
252b5132 852
47926f60 853/* Types. */
93c2a809
AM
854#define UNCOND_JUMP 0
855#define COND_JUMP 1
856#define COND_JUMP86 2
e379e5f3
L
857#define BRANCH_PADDING 3
858#define BRANCH_PREFIX 4
859#define FUSED_JCC_PADDING 5
fddf5b5b 860
47926f60 861/* Sizes. */
252b5132
RH
862#define CODE16 1
863#define SMALL 0
29b0f896 864#define SMALL16 (SMALL | CODE16)
252b5132 865#define BIG 2
29b0f896 866#define BIG16 (BIG | CODE16)
252b5132
RH
867
868#ifndef INLINE
869#ifdef __GNUC__
870#define INLINE __inline__
871#else
872#define INLINE
873#endif
874#endif
875
fddf5b5b
AM
876#define ENCODE_RELAX_STATE(type, size) \
877 ((relax_substateT) (((type) << 2) | (size)))
878#define TYPE_FROM_RELAX_STATE(s) \
879 ((s) >> 2)
880#define DISP_SIZE_FROM_RELAX_STATE(s) \
881 ((((s) & 3) == BIG ? 4 : (((s) & 3) == BIG16 ? 2 : 1)))
252b5132
RH
882
883/* This table is used by relax_frag to promote short jumps to long
884 ones where necessary. SMALL (short) jumps may be promoted to BIG
885 (32 bit long) ones, and SMALL16 jumps to BIG16 (16 bit long). We
886 don't allow a short jump in a 32 bit code segment to be promoted to
887 a 16 bit offset jump because it's slower (requires data size
888 prefix), and doesn't work, unless the destination is in the bottom
889 64k of the code segment (The top 16 bits of eip are zeroed). */
890
891const relax_typeS md_relax_table[] =
892{
24eab124
AM
893 /* The fields are:
894 1) most positive reach of this state,
895 2) most negative reach of this state,
93c2a809 896 3) how many bytes this mode will have in the variable part of the frag
ce8a8b2f 897 4) which index into the table to try if we can't fit into this one. */
252b5132 898
fddf5b5b 899 /* UNCOND_JUMP states. */
93c2a809
AM
900 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG)},
901 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16)},
902 /* dword jmp adds 4 bytes to frag:
903 0 extra opcode bytes, 4 displacement bytes. */
252b5132 904 {0, 0, 4, 0},
93c2a809
AM
905 /* word jmp adds 2 byte2 to frag:
906 0 extra opcode bytes, 2 displacement bytes. */
252b5132
RH
907 {0, 0, 2, 0},
908
93c2a809
AM
909 /* COND_JUMP states. */
910 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG)},
911 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG16)},
912 /* dword conditionals adds 5 bytes to frag:
913 1 extra opcode byte, 4 displacement bytes. */
914 {0, 0, 5, 0},
fddf5b5b 915 /* word conditionals add 3 bytes to frag:
93c2a809
AM
916 1 extra opcode byte, 2 displacement bytes. */
917 {0, 0, 3, 0},
918
919 /* COND_JUMP86 states. */
920 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG)},
921 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG16)},
922 /* dword conditionals adds 5 bytes to frag:
923 1 extra opcode byte, 4 displacement bytes. */
924 {0, 0, 5, 0},
925 /* word conditionals add 4 bytes to frag:
926 1 displacement byte and a 3 byte long branch insn. */
927 {0, 0, 4, 0}
252b5132
RH
928};
929
6ceeed25 930#define ARCH(n, t, f, s) \
ae89daec
JB
931 { STRING_COMMA_LEN (#n), s, PROCESSOR_ ## t, CPU_ ## f ## _FLAGS, \
932 CPU_NONE_FLAGS }
933#define SUBARCH(n, e, d, s) \
934 { STRING_COMMA_LEN (#n), s, PROCESSOR_NONE, CPU_ ## e ## _FLAGS, \
935 CPU_ ## d ## _FLAGS }
6ceeed25 936
9103f4f4
L
937static const arch_entry cpu_arch[] =
938{
3ce2ebcf
JB
939 /* Do not replace the first two entries - i386_target_format() and
940 set_cpu_arch() rely on them being there in this order. */
6ceeed25
JB
941 ARCH (generic32, GENERIC32, GENERIC32, false),
942 ARCH (generic64, GENERIC64, GENERIC64, false),
943 ARCH (i8086, UNKNOWN, NONE, false),
944 ARCH (i186, UNKNOWN, I186, false),
945 ARCH (i286, UNKNOWN, I286, false),
946 ARCH (i386, I386, I386, false),
947 ARCH (i486, I486, I486, false),
948 ARCH (i586, PENTIUM, I586, false),
949 ARCH (i686, PENTIUMPRO, I686, false),
950 ARCH (pentium, PENTIUM, I586, false),
951 ARCH (pentiumpro, PENTIUMPRO, PENTIUMPRO, false),
952 ARCH (pentiumii, PENTIUMPRO, P2, false),
953 ARCH (pentiumiii, PENTIUMPRO, P3, false),
954 ARCH (pentium4, PENTIUM4, P4, false),
955 ARCH (prescott, NOCONA, CORE, false),
956 ARCH (nocona, NOCONA, NOCONA, false),
957 ARCH (yonah, CORE, CORE, true),
958 ARCH (core, CORE, CORE, false),
959 ARCH (merom, CORE2, CORE2, true),
960 ARCH (core2, CORE2, CORE2, false),
961 ARCH (corei7, COREI7, COREI7, false),
962 ARCH (iamcu, IAMCU, IAMCU, false),
963 ARCH (k6, K6, K6, false),
964 ARCH (k6_2, K6, K6_2, false),
965 ARCH (athlon, ATHLON, ATHLON, false),
966 ARCH (sledgehammer, K8, K8, true),
967 ARCH (opteron, K8, K8, false),
968 ARCH (k8, K8, K8, false),
969 ARCH (amdfam10, AMDFAM10, AMDFAM10, false),
970 ARCH (bdver1, BD, BDVER1, false),
971 ARCH (bdver2, BD, BDVER2, false),
972 ARCH (bdver3, BD, BDVER3, false),
973 ARCH (bdver4, BD, BDVER4, false),
974 ARCH (znver1, ZNVER, ZNVER1, false),
975 ARCH (znver2, ZNVER, ZNVER2, false),
976 ARCH (znver3, ZNVER, ZNVER3, false),
977 ARCH (btver1, BT, BTVER1, false),
978 ARCH (btver2, BT, BTVER2, false),
979
ae89daec
JB
980 SUBARCH (8087, 8087, ANY_X87, false),
981 SUBARCH (87, NONE, ANY_X87, false), /* Disable only! */
982 SUBARCH (287, 287, ANY_287, false),
983 SUBARCH (387, 387, ANY_387, false),
984 SUBARCH (687, 687, ANY_687, false),
985 SUBARCH (cmov, CMOV, ANY_CMOV, false),
986 SUBARCH (fxsr, FXSR, ANY_FXSR, false),
987 SUBARCH (mmx, MMX, ANY_MMX, false),
988 SUBARCH (sse, SSE, ANY_SSE, false),
989 SUBARCH (sse2, SSE2, ANY_SSE2, false),
990 SUBARCH (sse3, SSE3, ANY_SSE3, false),
991 SUBARCH (sse4a, SSE4A, ANY_SSE4A, false),
992 SUBARCH (ssse3, SSSE3, ANY_SSSE3, false),
993 SUBARCH (sse4.1, SSE4_1, ANY_SSE4_1, false),
994 SUBARCH (sse4.2, SSE4_2, ANY_SSE4_2, false),
995 SUBARCH (sse4, SSE4_2, ANY_SSE4_1, false),
996 SUBARCH (avx, AVX, ANY_AVX, false),
997 SUBARCH (avx2, AVX2, ANY_AVX2, false),
998 SUBARCH (avx512f, AVX512F, ANY_AVX512F, false),
999 SUBARCH (avx512cd, AVX512CD, ANY_AVX512CD, false),
1000 SUBARCH (avx512er, AVX512ER, ANY_AVX512ER, false),
1001 SUBARCH (avx512pf, AVX512PF, ANY_AVX512PF, false),
1002 SUBARCH (avx512dq, AVX512DQ, ANY_AVX512DQ, false),
1003 SUBARCH (avx512bw, AVX512BW, ANY_AVX512BW, false),
1004 SUBARCH (avx512vl, AVX512VL, ANY_AVX512VL, false),
1005 SUBARCH (vmx, VMX, VMX, false),
1006 SUBARCH (vmfunc, VMFUNC, VMFUNC, false),
1007 SUBARCH (smx, SMX, SMX, false),
1008 SUBARCH (xsave, XSAVE, XSAVE, false),
1009 SUBARCH (xsaveopt, XSAVEOPT, XSAVEOPT, false),
1010 SUBARCH (xsavec, XSAVEC, XSAVEC, false),
1011 SUBARCH (xsaves, XSAVES, XSAVES, false),
1012 SUBARCH (aes, AES, AES, false),
1013 SUBARCH (pclmul, PCLMUL, PCLMUL, false),
1014 SUBARCH (clmul, PCLMUL, PCLMUL, true),
1015 SUBARCH (fsgsbase, FSGSBASE, FSGSBASE, false),
1016 SUBARCH (rdrnd, RDRND, RDRND, false),
1017 SUBARCH (f16c, F16C, F16C, false),
1018 SUBARCH (bmi2, BMI2, BMI2, false),
1019 SUBARCH (fma, FMA, FMA, false),
1020 SUBARCH (fma4, FMA4, FMA4, false),
1021 SUBARCH (xop, XOP, XOP, false),
1022 SUBARCH (lwp, LWP, LWP, false),
1023 SUBARCH (movbe, MOVBE, MOVBE, false),
1024 SUBARCH (cx16, CX16, CX16, false),
1025 SUBARCH (ept, EPT, EPT, false),
1026 SUBARCH (lzcnt, LZCNT, LZCNT, false),
1027 SUBARCH (popcnt, POPCNT, POPCNT, false),
1028 SUBARCH (hle, HLE, HLE, false),
1029 SUBARCH (rtm, RTM, RTM, false),
1030 SUBARCH (invpcid, INVPCID, INVPCID, false),
1031 SUBARCH (clflush, CLFLUSH, CLFLUSH, false),
1032 SUBARCH (nop, NOP, NOP, false),
1033 SUBARCH (syscall, SYSCALL, SYSCALL, false),
1034 SUBARCH (rdtscp, RDTSCP, RDTSCP, false),
1035 SUBARCH (3dnow, 3DNOW, 3DNOW, false),
1036 SUBARCH (3dnowa, 3DNOWA, 3DNOWA, false),
1037 SUBARCH (padlock, PADLOCK, PADLOCK, false),
1038 SUBARCH (pacifica, SVME, SVME, true),
1039 SUBARCH (svme, SVME, SVME, false),
1040 SUBARCH (sse4a, SSE4A, SSE4A, false),
1041 SUBARCH (abm, ABM, ABM, false),
1042 SUBARCH (bmi, BMI, BMI, false),
1043 SUBARCH (tbm, TBM, TBM, false),
1044 SUBARCH (adx, ADX, ADX, false),
1045 SUBARCH (rdseed, RDSEED, RDSEED, false),
1046 SUBARCH (prfchw, PRFCHW, PRFCHW, false),
1047 SUBARCH (smap, SMAP, SMAP, false),
1048 SUBARCH (mpx, MPX, MPX, false),
1049 SUBARCH (sha, SHA, SHA, false),
1050 SUBARCH (clflushopt, CLFLUSHOPT, CLFLUSHOPT, false),
1051 SUBARCH (prefetchwt1, PREFETCHWT1, PREFETCHWT1, false),
1052 SUBARCH (se1, SE1, SE1, false),
1053 SUBARCH (clwb, CLWB, CLWB, false),
1054 SUBARCH (avx512ifma, AVX512IFMA, ANY_AVX512IFMA, false),
1055 SUBARCH (avx512vbmi, AVX512VBMI, ANY_AVX512VBMI, false),
1056 SUBARCH (avx512_4fmaps, AVX512_4FMAPS, ANY_AVX512_4FMAPS, false),
1057 SUBARCH (avx512_4vnniw, AVX512_4VNNIW, ANY_AVX512_4VNNIW, false),
1058 SUBARCH (avx512_vpopcntdq, AVX512_VPOPCNTDQ, ANY_AVX512_VPOPCNTDQ, false),
1059 SUBARCH (avx512_vbmi2, AVX512_VBMI2, ANY_AVX512_VBMI2, false),
1060 SUBARCH (avx512_vnni, AVX512_VNNI, ANY_AVX512_VNNI, false),
1061 SUBARCH (avx512_bitalg, AVX512_BITALG, ANY_AVX512_BITALG, false),
1062 SUBARCH (avx_vnni, AVX_VNNI, ANY_AVX_VNNI, false),
1063 SUBARCH (clzero, CLZERO, CLZERO, false),
1064 SUBARCH (mwaitx, MWAITX, MWAITX, false),
1065 SUBARCH (ospke, OSPKE, OSPKE, false),
1066 SUBARCH (rdpid, RDPID, RDPID, false),
1067 SUBARCH (ptwrite, PTWRITE, PTWRITE, false),
1068 SUBARCH (ibt, IBT, ANY_IBT, false),
1069 SUBARCH (shstk, SHSTK, ANY_SHSTK, false),
1070 SUBARCH (gfni, GFNI, GFNI, false),
1071 SUBARCH (vaes, VAES, VAES, false),
1072 SUBARCH (vpclmulqdq, VPCLMULQDQ, VPCLMULQDQ, false),
1073 SUBARCH (wbnoinvd, WBNOINVD, WBNOINVD, false),
1074 SUBARCH (pconfig, PCONFIG, PCONFIG, false),
1075 SUBARCH (waitpkg, WAITPKG, WAITPKG, false),
1076 SUBARCH (cldemote, CLDEMOTE, CLDEMOTE, false),
1077 SUBARCH (amx_int8, AMX_INT8, ANY_AMX_INT8, false),
1078 SUBARCH (amx_bf16, AMX_BF16, ANY_AMX_BF16, false),
1079 SUBARCH (amx_tile, AMX_TILE, ANY_AMX_TILE, false),
1080 SUBARCH (movdiri, MOVDIRI, ANY_MOVDIRI, false),
1081 SUBARCH (movdir64b, MOVDIR64B, ANY_MOVDIR64B, false),
1082 SUBARCH (avx512_bf16, AVX512_BF16, ANY_AVX512_BF16, false),
1083 SUBARCH (avx512_vp2intersect, AVX512_VP2INTERSECT,
1084 ANY_AVX512_VP2INTERSECT, false),
1085 SUBARCH (tdx, TDX, ANY_TDX, false),
1086 SUBARCH (enqcmd, ENQCMD, ANY_ENQCMD, false),
1087 SUBARCH (serialize, SERIALIZE, ANY_SERIALIZE, false),
1088 SUBARCH (rdpru, RDPRU, RDPRU, false),
1089 SUBARCH (mcommit, MCOMMIT, MCOMMIT, false),
1090 SUBARCH (sev_es, SEV_ES, SEV_ES, false),
1091 SUBARCH (tsxldtrk, TSXLDTRK, ANY_TSXLDTRK, false),
1092 SUBARCH (kl, KL, ANY_KL, false),
1093 SUBARCH (widekl, WIDEKL, ANY_WIDEKL, false),
1094 SUBARCH (uintr, UINTR, ANY_UINTR, false),
1095 SUBARCH (hreset, HRESET, ANY_HRESET, false),
1096 SUBARCH (avx512_fp16, AVX512_FP16, ANY_AVX512_FP16, false),
293f5f65
L
1097};
1098
6ceeed25
JB
1099#undef SUBARCH
1100#undef ARCH
1101
704209c0 1102#ifdef I386COFF
a6c24e68
NC
1103/* Like s_lcomm_internal in gas/read.c but the alignment string
1104 is allowed to be optional. */
1105
1106static symbolS *
1107pe_lcomm_internal (int needs_align, symbolS *symbolP, addressT size)
1108{
1109 addressT align = 0;
1110
1111 SKIP_WHITESPACE ();
1112
7ab9ffdd 1113 if (needs_align
a6c24e68
NC
1114 && *input_line_pointer == ',')
1115 {
1116 align = parse_align (needs_align - 1);
7ab9ffdd 1117
a6c24e68
NC
1118 if (align == (addressT) -1)
1119 return NULL;
1120 }
1121 else
1122 {
1123 if (size >= 8)
1124 align = 3;
1125 else if (size >= 4)
1126 align = 2;
1127 else if (size >= 2)
1128 align = 1;
1129 else
1130 align = 0;
1131 }
1132
1133 bss_alloc (symbolP, size, align);
1134 return symbolP;
1135}
1136
704209c0 1137static void
a6c24e68
NC
1138pe_lcomm (int needs_align)
1139{
1140 s_comm_internal (needs_align * 2, pe_lcomm_internal);
1141}
704209c0 1142#endif
a6c24e68 1143
29b0f896
AM
1144const pseudo_typeS md_pseudo_table[] =
1145{
1146#if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
1147 {"align", s_align_bytes, 0},
1148#else
1149 {"align", s_align_ptwo, 0},
1150#endif
1151 {"arch", set_cpu_arch, 0},
1152#ifndef I386COFF
1153 {"bss", s_bss, 0},
a6c24e68
NC
1154#else
1155 {"lcomm", pe_lcomm, 1},
29b0f896
AM
1156#endif
1157 {"ffloat", float_cons, 'f'},
1158 {"dfloat", float_cons, 'd'},
1159 {"tfloat", float_cons, 'x'},
7d19d096 1160 {"hfloat", float_cons, 'h'},
de133cf9 1161 {"bfloat16", float_cons, 'b'},
29b0f896 1162 {"value", cons, 2},
d182319b 1163 {"slong", signed_cons, 4},
29b0f896
AM
1164 {"noopt", s_ignore, 0},
1165 {"optim", s_ignore, 0},
1166 {"code16gcc", set_16bit_gcc_code_flag, CODE_16BIT},
1167 {"code16", set_code_flag, CODE_16BIT},
1168 {"code32", set_code_flag, CODE_32BIT},
da5f19a2 1169#ifdef BFD64
29b0f896 1170 {"code64", set_code_flag, CODE_64BIT},
da5f19a2 1171#endif
29b0f896
AM
1172 {"intel_syntax", set_intel_syntax, 1},
1173 {"att_syntax", set_intel_syntax, 0},
1efbbeb4
L
1174 {"intel_mnemonic", set_intel_mnemonic, 1},
1175 {"att_mnemonic", set_intel_mnemonic, 0},
db51cc60
L
1176 {"allow_index_reg", set_allow_index_reg, 1},
1177 {"disallow_index_reg", set_allow_index_reg, 0},
7bab8ab5
JB
1178 {"sse_check", set_check, 0},
1179 {"operand_check", set_check, 1},
3b22753a
L
1180#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1181 {"largecomm", handle_large_common, 0},
07a53e5c 1182#else
68d20676 1183 {"file", dwarf2_directive_file, 0},
07a53e5c
RH
1184 {"loc", dwarf2_directive_loc, 0},
1185 {"loc_mark_labels", dwarf2_directive_loc_mark_labels, 0},
3b22753a 1186#endif
6482c264
NC
1187#ifdef TE_PE
1188 {"secrel32", pe_directive_secrel, 0},
145667f8 1189 {"secidx", pe_directive_secidx, 0},
6482c264 1190#endif
29b0f896
AM
1191 {0, 0, 0}
1192};
1193
1194/* For interface with expression (). */
1195extern char *input_line_pointer;
1196
1197/* Hash table for instruction mnemonic lookup. */
629310ab 1198static htab_t op_hash;
29b0f896
AM
1199
1200/* Hash table for register lookup. */
629310ab 1201static htab_t reg_hash;
29b0f896 1202\f
ce8a8b2f
AM
1203 /* Various efficient no-op patterns for aligning code labels.
1204 Note: Don't try to assemble the instructions in the comments.
1205 0L and 0w are not legal. */
62a02d25
L
1206static const unsigned char f32_1[] =
1207 {0x90}; /* nop */
1208static const unsigned char f32_2[] =
1209 {0x66,0x90}; /* xchg %ax,%ax */
1210static const unsigned char f32_3[] =
1211 {0x8d,0x76,0x00}; /* leal 0(%esi),%esi */
1212static const unsigned char f32_4[] =
1213 {0x8d,0x74,0x26,0x00}; /* leal 0(%esi,1),%esi */
62a02d25
L
1214static const unsigned char f32_6[] =
1215 {0x8d,0xb6,0x00,0x00,0x00,0x00}; /* leal 0L(%esi),%esi */
1216static const unsigned char f32_7[] =
1217 {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal 0L(%esi,1),%esi */
62a02d25 1218static const unsigned char f16_3[] =
3ae729d5 1219 {0x8d,0x74,0x00}; /* lea 0(%si),%si */
62a02d25 1220static const unsigned char f16_4[] =
3ae729d5
L
1221 {0x8d,0xb4,0x00,0x00}; /* lea 0W(%si),%si */
1222static const unsigned char jump_disp8[] =
1223 {0xeb}; /* jmp disp8 */
1224static const unsigned char jump32_disp32[] =
1225 {0xe9}; /* jmp disp32 */
1226static const unsigned char jump16_disp32[] =
1227 {0x66,0xe9}; /* jmp disp32 */
62a02d25
L
1228/* 32-bit NOPs patterns. */
1229static const unsigned char *const f32_patt[] = {
3ae729d5 1230 f32_1, f32_2, f32_3, f32_4, NULL, f32_6, f32_7
62a02d25
L
1231};
1232/* 16-bit NOPs patterns. */
1233static const unsigned char *const f16_patt[] = {
3ae729d5 1234 f32_1, f32_2, f16_3, f16_4
62a02d25
L
1235};
1236/* nopl (%[re]ax) */
1237static const unsigned char alt_3[] =
1238 {0x0f,0x1f,0x00};
1239/* nopl 0(%[re]ax) */
1240static const unsigned char alt_4[] =
1241 {0x0f,0x1f,0x40,0x00};
1242/* nopl 0(%[re]ax,%[re]ax,1) */
1243static const unsigned char alt_5[] =
1244 {0x0f,0x1f,0x44,0x00,0x00};
1245/* nopw 0(%[re]ax,%[re]ax,1) */
1246static const unsigned char alt_6[] =
1247 {0x66,0x0f,0x1f,0x44,0x00,0x00};
1248/* nopl 0L(%[re]ax) */
1249static const unsigned char alt_7[] =
1250 {0x0f,0x1f,0x80,0x00,0x00,0x00,0x00};
1251/* nopl 0L(%[re]ax,%[re]ax,1) */
1252static const unsigned char alt_8[] =
1253 {0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1254/* nopw 0L(%[re]ax,%[re]ax,1) */
1255static const unsigned char alt_9[] =
1256 {0x66,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1257/* nopw %cs:0L(%[re]ax,%[re]ax,1) */
1258static const unsigned char alt_10[] =
1259 {0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
3ae729d5
L
1260/* data16 nopw %cs:0L(%eax,%eax,1) */
1261static const unsigned char alt_11[] =
1262 {0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
62a02d25
L
1263/* 32-bit and 64-bit NOPs patterns. */
1264static const unsigned char *const alt_patt[] = {
1265 f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
3ae729d5 1266 alt_9, alt_10, alt_11
62a02d25
L
1267};
1268
1269/* Genenerate COUNT bytes of NOPs to WHERE from PATT with the maximum
1270 size of a single NOP instruction MAX_SINGLE_NOP_SIZE. */
1271
1272static void
1273i386_output_nops (char *where, const unsigned char *const *patt,
1274 int count, int max_single_nop_size)
1275
1276{
3ae729d5
L
1277 /* Place the longer NOP first. */
1278 int last;
1279 int offset;
3076e594
NC
1280 const unsigned char *nops;
1281
1282 if (max_single_nop_size < 1)
1283 {
1284 as_fatal (_("i386_output_nops called to generate nops of at most %d bytes!"),
1285 max_single_nop_size);
1286 return;
1287 }
1288
1289 nops = patt[max_single_nop_size - 1];
3ae729d5
L
1290
1291 /* Use the smaller one if the requsted one isn't available. */
1292 if (nops == NULL)
62a02d25 1293 {
3ae729d5
L
1294 max_single_nop_size--;
1295 nops = patt[max_single_nop_size - 1];
62a02d25
L
1296 }
1297
3ae729d5
L
1298 last = count % max_single_nop_size;
1299
1300 count -= last;
1301 for (offset = 0; offset < count; offset += max_single_nop_size)
1302 memcpy (where + offset, nops, max_single_nop_size);
1303
1304 if (last)
1305 {
1306 nops = patt[last - 1];
1307 if (nops == NULL)
1308 {
1309 /* Use the smaller one plus one-byte NOP if the needed one
1310 isn't available. */
1311 last--;
1312 nops = patt[last - 1];
1313 memcpy (where + offset, nops, last);
1314 where[offset + last] = *patt[0];
1315 }
1316 else
1317 memcpy (where + offset, nops, last);
1318 }
62a02d25
L
1319}
1320
3ae729d5
L
1321static INLINE int
1322fits_in_imm7 (offsetT num)
1323{
1324 return (num & 0x7f) == num;
1325}
1326
1327static INLINE int
1328fits_in_imm31 (offsetT num)
1329{
1330 return (num & 0x7fffffff) == num;
1331}
62a02d25
L
1332
1333/* Genenerate COUNT bytes of NOPs to WHERE with the maximum size of a
1334 single NOP instruction LIMIT. */
1335
1336void
3ae729d5 1337i386_generate_nops (fragS *fragP, char *where, offsetT count, int limit)
62a02d25 1338{
3ae729d5 1339 const unsigned char *const *patt = NULL;
62a02d25 1340 int max_single_nop_size;
3ae729d5
L
1341 /* Maximum number of NOPs before switching to jump over NOPs. */
1342 int max_number_of_nops;
62a02d25 1343
3ae729d5 1344 switch (fragP->fr_type)
62a02d25 1345 {
3ae729d5
L
1346 case rs_fill_nop:
1347 case rs_align_code:
1348 break;
e379e5f3
L
1349 case rs_machine_dependent:
1350 /* Allow NOP padding for jumps and calls. */
1351 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
1352 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
1353 break;
1354 /* Fall through. */
3ae729d5 1355 default:
62a02d25
L
1356 return;
1357 }
1358
ccc9c027
L
1359 /* We need to decide which NOP sequence to use for 32bit and
1360 64bit. When -mtune= is used:
4eed87de 1361
76bc74dc
L
1362 1. For PROCESSOR_I386, PROCESSOR_I486, PROCESSOR_PENTIUM and
1363 PROCESSOR_GENERIC32, f32_patt will be used.
80b8656c
L
1364 2. For the rest, alt_patt will be used.
1365
1366 When -mtune= isn't used, alt_patt will be used if
22109423 1367 cpu_arch_isa_flags has CpuNop. Otherwise, f32_patt will
76bc74dc 1368 be used.
ccc9c027
L
1369
1370 When -march= or .arch is used, we can't use anything beyond
1371 cpu_arch_isa_flags. */
1372
1373 if (flag_code == CODE_16BIT)
1374 {
3ae729d5
L
1375 patt = f16_patt;
1376 max_single_nop_size = sizeof (f16_patt) / sizeof (f16_patt[0]);
1377 /* Limit number of NOPs to 2 in 16-bit mode. */
1378 max_number_of_nops = 2;
252b5132 1379 }
33fef721 1380 else
ccc9c027 1381 {
fbf3f584 1382 if (fragP->tc_frag_data.isa == PROCESSOR_UNKNOWN)
ccc9c027
L
1383 {
1384 /* PROCESSOR_UNKNOWN means that all ISAs may be used. */
1385 switch (cpu_arch_tune)
1386 {
1387 case PROCESSOR_UNKNOWN:
1388 /* We use cpu_arch_isa_flags to check if we SHOULD
22109423
L
1389 optimize with nops. */
1390 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1391 patt = alt_patt;
ccc9c027
L
1392 else
1393 patt = f32_patt;
1394 break;
ccc9c027
L
1395 case PROCESSOR_PENTIUM4:
1396 case PROCESSOR_NOCONA:
ef05d495 1397 case PROCESSOR_CORE:
76bc74dc 1398 case PROCESSOR_CORE2:
bd5295b2 1399 case PROCESSOR_COREI7:
76bc74dc 1400 case PROCESSOR_GENERIC64:
ccc9c027
L
1401 case PROCESSOR_K6:
1402 case PROCESSOR_ATHLON:
1403 case PROCESSOR_K8:
4eed87de 1404 case PROCESSOR_AMDFAM10:
8aedb9fe 1405 case PROCESSOR_BD:
029f3522 1406 case PROCESSOR_ZNVER:
7b458c12 1407 case PROCESSOR_BT:
80b8656c 1408 patt = alt_patt;
ccc9c027 1409 break;
76bc74dc 1410 case PROCESSOR_I386:
ccc9c027
L
1411 case PROCESSOR_I486:
1412 case PROCESSOR_PENTIUM:
2dde1948 1413 case PROCESSOR_PENTIUMPRO:
81486035 1414 case PROCESSOR_IAMCU:
ccc9c027
L
1415 case PROCESSOR_GENERIC32:
1416 patt = f32_patt;
1417 break;
c368d2a8
JB
1418 case PROCESSOR_NONE:
1419 abort ();
4eed87de 1420 }
ccc9c027
L
1421 }
1422 else
1423 {
fbf3f584 1424 switch (fragP->tc_frag_data.tune)
ccc9c027
L
1425 {
1426 case PROCESSOR_UNKNOWN:
e6a14101 1427 /* When cpu_arch_isa is set, cpu_arch_tune shouldn't be
ccc9c027
L
1428 PROCESSOR_UNKNOWN. */
1429 abort ();
1430 break;
1431
76bc74dc 1432 case PROCESSOR_I386:
ccc9c027
L
1433 case PROCESSOR_I486:
1434 case PROCESSOR_PENTIUM:
81486035 1435 case PROCESSOR_IAMCU:
ccc9c027
L
1436 case PROCESSOR_K6:
1437 case PROCESSOR_ATHLON:
1438 case PROCESSOR_K8:
4eed87de 1439 case PROCESSOR_AMDFAM10:
8aedb9fe 1440 case PROCESSOR_BD:
029f3522 1441 case PROCESSOR_ZNVER:
7b458c12 1442 case PROCESSOR_BT:
ccc9c027
L
1443 case PROCESSOR_GENERIC32:
1444 /* We use cpu_arch_isa_flags to check if we CAN optimize
22109423
L
1445 with nops. */
1446 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1447 patt = alt_patt;
ccc9c027
L
1448 else
1449 patt = f32_patt;
1450 break;
76bc74dc
L
1451 case PROCESSOR_PENTIUMPRO:
1452 case PROCESSOR_PENTIUM4:
1453 case PROCESSOR_NOCONA:
1454 case PROCESSOR_CORE:
ef05d495 1455 case PROCESSOR_CORE2:
bd5295b2 1456 case PROCESSOR_COREI7:
22109423 1457 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1458 patt = alt_patt;
ccc9c027
L
1459 else
1460 patt = f32_patt;
1461 break;
1462 case PROCESSOR_GENERIC64:
80b8656c 1463 patt = alt_patt;
ccc9c027 1464 break;
c368d2a8
JB
1465 case PROCESSOR_NONE:
1466 abort ();
4eed87de 1467 }
ccc9c027
L
1468 }
1469
76bc74dc
L
1470 if (patt == f32_patt)
1471 {
3ae729d5
L
1472 max_single_nop_size = sizeof (f32_patt) / sizeof (f32_patt[0]);
1473 /* Limit number of NOPs to 2 for older processors. */
1474 max_number_of_nops = 2;
76bc74dc
L
1475 }
1476 else
1477 {
3ae729d5
L
1478 max_single_nop_size = sizeof (alt_patt) / sizeof (alt_patt[0]);
1479 /* Limit number of NOPs to 7 for newer processors. */
1480 max_number_of_nops = 7;
1481 }
1482 }
1483
1484 if (limit == 0)
1485 limit = max_single_nop_size;
1486
1487 if (fragP->fr_type == rs_fill_nop)
1488 {
1489 /* Output NOPs for .nop directive. */
1490 if (limit > max_single_nop_size)
1491 {
1492 as_bad_where (fragP->fr_file, fragP->fr_line,
1493 _("invalid single nop size: %d "
1494 "(expect within [0, %d])"),
1495 limit, max_single_nop_size);
1496 return;
1497 }
1498 }
e379e5f3 1499 else if (fragP->fr_type != rs_machine_dependent)
3ae729d5
L
1500 fragP->fr_var = count;
1501
1502 if ((count / max_single_nop_size) > max_number_of_nops)
1503 {
1504 /* Generate jump over NOPs. */
1505 offsetT disp = count - 2;
1506 if (fits_in_imm7 (disp))
1507 {
1508 /* Use "jmp disp8" if possible. */
1509 count = disp;
1510 where[0] = jump_disp8[0];
1511 where[1] = count;
1512 where += 2;
1513 }
1514 else
1515 {
1516 unsigned int size_of_jump;
1517
1518 if (flag_code == CODE_16BIT)
1519 {
1520 where[0] = jump16_disp32[0];
1521 where[1] = jump16_disp32[1];
1522 size_of_jump = 2;
1523 }
1524 else
1525 {
1526 where[0] = jump32_disp32[0];
1527 size_of_jump = 1;
1528 }
1529
1530 count -= size_of_jump + 4;
1531 if (!fits_in_imm31 (count))
1532 {
1533 as_bad_where (fragP->fr_file, fragP->fr_line,
1534 _("jump over nop padding out of range"));
1535 return;
1536 }
1537
1538 md_number_to_chars (where + size_of_jump, count, 4);
1539 where += size_of_jump + 4;
76bc74dc 1540 }
ccc9c027 1541 }
3ae729d5
L
1542
1543 /* Generate multiple NOPs. */
1544 i386_output_nops (where, patt, count, limit);
252b5132
RH
1545}
1546
c6fb90c8 1547static INLINE int
0dfbf9d7 1548operand_type_all_zero (const union i386_operand_type *x)
40fb9820 1549{
0dfbf9d7 1550 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1551 {
1552 case 3:
0dfbf9d7 1553 if (x->array[2])
c6fb90c8 1554 return 0;
1a0670f3 1555 /* Fall through. */
c6fb90c8 1556 case 2:
0dfbf9d7 1557 if (x->array[1])
c6fb90c8 1558 return 0;
1a0670f3 1559 /* Fall through. */
c6fb90c8 1560 case 1:
0dfbf9d7 1561 return !x->array[0];
c6fb90c8
L
1562 default:
1563 abort ();
1564 }
40fb9820
L
1565}
1566
c6fb90c8 1567static INLINE void
0dfbf9d7 1568operand_type_set (union i386_operand_type *x, unsigned int v)
40fb9820 1569{
0dfbf9d7 1570 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1571 {
1572 case 3:
0dfbf9d7 1573 x->array[2] = v;
1a0670f3 1574 /* Fall through. */
c6fb90c8 1575 case 2:
0dfbf9d7 1576 x->array[1] = v;
1a0670f3 1577 /* Fall through. */
c6fb90c8 1578 case 1:
0dfbf9d7 1579 x->array[0] = v;
1a0670f3 1580 /* Fall through. */
c6fb90c8
L
1581 break;
1582 default:
1583 abort ();
1584 }
bab6aec1
JB
1585
1586 x->bitfield.class = ClassNone;
75e5731b 1587 x->bitfield.instance = InstanceNone;
c6fb90c8 1588}
40fb9820 1589
c6fb90c8 1590static INLINE int
0dfbf9d7
L
1591operand_type_equal (const union i386_operand_type *x,
1592 const union i386_operand_type *y)
c6fb90c8 1593{
0dfbf9d7 1594 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1595 {
1596 case 3:
0dfbf9d7 1597 if (x->array[2] != y->array[2])
c6fb90c8 1598 return 0;
1a0670f3 1599 /* Fall through. */
c6fb90c8 1600 case 2:
0dfbf9d7 1601 if (x->array[1] != y->array[1])
c6fb90c8 1602 return 0;
1a0670f3 1603 /* Fall through. */
c6fb90c8 1604 case 1:
0dfbf9d7 1605 return x->array[0] == y->array[0];
c6fb90c8
L
1606 break;
1607 default:
1608 abort ();
1609 }
1610}
40fb9820 1611
0dfbf9d7
L
1612static INLINE int
1613cpu_flags_all_zero (const union i386_cpu_flags *x)
1614{
1615 switch (ARRAY_SIZE(x->array))
1616 {
53467f57
IT
1617 case 4:
1618 if (x->array[3])
1619 return 0;
1620 /* Fall through. */
0dfbf9d7
L
1621 case 3:
1622 if (x->array[2])
1623 return 0;
1a0670f3 1624 /* Fall through. */
0dfbf9d7
L
1625 case 2:
1626 if (x->array[1])
1627 return 0;
1a0670f3 1628 /* Fall through. */
0dfbf9d7
L
1629 case 1:
1630 return !x->array[0];
1631 default:
1632 abort ();
1633 }
1634}
1635
0dfbf9d7
L
1636static INLINE int
1637cpu_flags_equal (const union i386_cpu_flags *x,
1638 const union i386_cpu_flags *y)
1639{
1640 switch (ARRAY_SIZE(x->array))
1641 {
53467f57
IT
1642 case 4:
1643 if (x->array[3] != y->array[3])
1644 return 0;
1645 /* Fall through. */
0dfbf9d7
L
1646 case 3:
1647 if (x->array[2] != y->array[2])
1648 return 0;
1a0670f3 1649 /* Fall through. */
0dfbf9d7
L
1650 case 2:
1651 if (x->array[1] != y->array[1])
1652 return 0;
1a0670f3 1653 /* Fall through. */
0dfbf9d7
L
1654 case 1:
1655 return x->array[0] == y->array[0];
1656 break;
1657 default:
1658 abort ();
1659 }
1660}
c6fb90c8
L
1661
1662static INLINE int
1663cpu_flags_check_cpu64 (i386_cpu_flags f)
1664{
1665 return !((flag_code == CODE_64BIT && f.bitfield.cpuno64)
1666 || (flag_code != CODE_64BIT && f.bitfield.cpu64));
40fb9820
L
1667}
1668
c6fb90c8
L
1669static INLINE i386_cpu_flags
1670cpu_flags_and (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1671{
c6fb90c8
L
1672 switch (ARRAY_SIZE (x.array))
1673 {
53467f57
IT
1674 case 4:
1675 x.array [3] &= y.array [3];
1676 /* Fall through. */
c6fb90c8
L
1677 case 3:
1678 x.array [2] &= y.array [2];
1a0670f3 1679 /* Fall through. */
c6fb90c8
L
1680 case 2:
1681 x.array [1] &= y.array [1];
1a0670f3 1682 /* Fall through. */
c6fb90c8
L
1683 case 1:
1684 x.array [0] &= y.array [0];
1685 break;
1686 default:
1687 abort ();
1688 }
1689 return x;
1690}
40fb9820 1691
c6fb90c8
L
1692static INLINE i386_cpu_flags
1693cpu_flags_or (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1694{
c6fb90c8 1695 switch (ARRAY_SIZE (x.array))
40fb9820 1696 {
53467f57
IT
1697 case 4:
1698 x.array [3] |= y.array [3];
1699 /* Fall through. */
c6fb90c8
L
1700 case 3:
1701 x.array [2] |= y.array [2];
1a0670f3 1702 /* Fall through. */
c6fb90c8
L
1703 case 2:
1704 x.array [1] |= y.array [1];
1a0670f3 1705 /* Fall through. */
c6fb90c8
L
1706 case 1:
1707 x.array [0] |= y.array [0];
40fb9820
L
1708 break;
1709 default:
1710 abort ();
1711 }
40fb9820
L
1712 return x;
1713}
1714
309d3373
JB
1715static INLINE i386_cpu_flags
1716cpu_flags_and_not (i386_cpu_flags x, i386_cpu_flags y)
1717{
1718 switch (ARRAY_SIZE (x.array))
1719 {
53467f57
IT
1720 case 4:
1721 x.array [3] &= ~y.array [3];
1722 /* Fall through. */
309d3373
JB
1723 case 3:
1724 x.array [2] &= ~y.array [2];
1a0670f3 1725 /* Fall through. */
309d3373
JB
1726 case 2:
1727 x.array [1] &= ~y.array [1];
1a0670f3 1728 /* Fall through. */
309d3373
JB
1729 case 1:
1730 x.array [0] &= ~y.array [0];
1731 break;
1732 default:
1733 abort ();
1734 }
1735 return x;
1736}
1737
6c0946d0
JB
1738static const i386_cpu_flags avx512 = CPU_ANY_AVX512F_FLAGS;
1739
c0f3af97
L
1740#define CPU_FLAGS_ARCH_MATCH 0x1
1741#define CPU_FLAGS_64BIT_MATCH 0x2
1742
c0f3af97 1743#define CPU_FLAGS_PERFECT_MATCH \
db12e14e 1744 (CPU_FLAGS_ARCH_MATCH | CPU_FLAGS_64BIT_MATCH)
c0f3af97
L
1745
1746/* Return CPU flags match bits. */
3629bb00 1747
40fb9820 1748static int
d3ce72d0 1749cpu_flags_match (const insn_template *t)
40fb9820 1750{
c0f3af97
L
1751 i386_cpu_flags x = t->cpu_flags;
1752 int match = cpu_flags_check_cpu64 (x) ? CPU_FLAGS_64BIT_MATCH : 0;
40fb9820
L
1753
1754 x.bitfield.cpu64 = 0;
1755 x.bitfield.cpuno64 = 0;
1756
0dfbf9d7 1757 if (cpu_flags_all_zero (&x))
c0f3af97
L
1758 {
1759 /* This instruction is available on all archs. */
db12e14e 1760 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 1761 }
3629bb00
L
1762 else
1763 {
c0f3af97 1764 /* This instruction is available only on some archs. */
3629bb00
L
1765 i386_cpu_flags cpu = cpu_arch_flags;
1766
ab592e75
JB
1767 /* AVX512VL is no standalone feature - match it and then strip it. */
1768 if (x.bitfield.cpuavx512vl && !cpu.bitfield.cpuavx512vl)
1769 return match;
1770 x.bitfield.cpuavx512vl = 0;
1771
22c36940
JB
1772 /* AVX and AVX2 present at the same time express an operand size
1773 dependency - strip AVX2 for the purposes here. The operand size
1774 dependent check occurs in check_vecOperands(). */
1775 if (x.bitfield.cpuavx && x.bitfield.cpuavx2)
1776 x.bitfield.cpuavx2 = 0;
1777
3629bb00 1778 cpu = cpu_flags_and (x, cpu);
c0f3af97
L
1779 if (!cpu_flags_all_zero (&cpu))
1780 {
57392598 1781 if (x.bitfield.cpuavx)
a5ff0eb2 1782 {
929f69fa 1783 /* We need to check a few extra flags with AVX. */
b9d49817 1784 if (cpu.bitfield.cpuavx
40d231b4
JB
1785 && (!t->opcode_modifier.sse2avx
1786 || (sse2avx && !i.prefix[DATA_PREFIX]))
b9d49817 1787 && (!x.bitfield.cpuaes || cpu.bitfield.cpuaes)
929f69fa 1788 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
b9d49817
JB
1789 && (!x.bitfield.cpupclmul || cpu.bitfield.cpupclmul))
1790 match |= CPU_FLAGS_ARCH_MATCH;
a5ff0eb2 1791 }
929f69fa
JB
1792 else if (x.bitfield.cpuavx512f)
1793 {
1794 /* We need to check a few extra flags with AVX512F. */
1795 if (cpu.bitfield.cpuavx512f
1796 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
1797 && (!x.bitfield.cpuvaes || cpu.bitfield.cpuvaes)
1798 && (!x.bitfield.cpuvpclmulqdq || cpu.bitfield.cpuvpclmulqdq))
1799 match |= CPU_FLAGS_ARCH_MATCH;
1800 }
a5ff0eb2 1801 else
db12e14e 1802 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 1803 }
3629bb00 1804 }
c0f3af97 1805 return match;
40fb9820
L
1806}
1807
c6fb90c8
L
1808static INLINE i386_operand_type
1809operand_type_and (i386_operand_type x, i386_operand_type y)
40fb9820 1810{
bab6aec1
JB
1811 if (x.bitfield.class != y.bitfield.class)
1812 x.bitfield.class = ClassNone;
75e5731b
JB
1813 if (x.bitfield.instance != y.bitfield.instance)
1814 x.bitfield.instance = InstanceNone;
bab6aec1 1815
c6fb90c8
L
1816 switch (ARRAY_SIZE (x.array))
1817 {
1818 case 3:
1819 x.array [2] &= y.array [2];
1a0670f3 1820 /* Fall through. */
c6fb90c8
L
1821 case 2:
1822 x.array [1] &= y.array [1];
1a0670f3 1823 /* Fall through. */
c6fb90c8
L
1824 case 1:
1825 x.array [0] &= y.array [0];
1826 break;
1827 default:
1828 abort ();
1829 }
1830 return x;
40fb9820
L
1831}
1832
73053c1f
JB
1833static INLINE i386_operand_type
1834operand_type_and_not (i386_operand_type x, i386_operand_type y)
1835{
bab6aec1 1836 gas_assert (y.bitfield.class == ClassNone);
75e5731b 1837 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 1838
73053c1f
JB
1839 switch (ARRAY_SIZE (x.array))
1840 {
1841 case 3:
1842 x.array [2] &= ~y.array [2];
1843 /* Fall through. */
1844 case 2:
1845 x.array [1] &= ~y.array [1];
1846 /* Fall through. */
1847 case 1:
1848 x.array [0] &= ~y.array [0];
1849 break;
1850 default:
1851 abort ();
1852 }
1853 return x;
1854}
1855
c6fb90c8
L
1856static INLINE i386_operand_type
1857operand_type_or (i386_operand_type x, i386_operand_type y)
40fb9820 1858{
bab6aec1
JB
1859 gas_assert (x.bitfield.class == ClassNone ||
1860 y.bitfield.class == ClassNone ||
1861 x.bitfield.class == y.bitfield.class);
75e5731b
JB
1862 gas_assert (x.bitfield.instance == InstanceNone ||
1863 y.bitfield.instance == InstanceNone ||
1864 x.bitfield.instance == y.bitfield.instance);
bab6aec1 1865
c6fb90c8 1866 switch (ARRAY_SIZE (x.array))
40fb9820 1867 {
c6fb90c8
L
1868 case 3:
1869 x.array [2] |= y.array [2];
1a0670f3 1870 /* Fall through. */
c6fb90c8
L
1871 case 2:
1872 x.array [1] |= y.array [1];
1a0670f3 1873 /* Fall through. */
c6fb90c8
L
1874 case 1:
1875 x.array [0] |= y.array [0];
40fb9820
L
1876 break;
1877 default:
1878 abort ();
1879 }
c6fb90c8
L
1880 return x;
1881}
40fb9820 1882
c6fb90c8
L
1883static INLINE i386_operand_type
1884operand_type_xor (i386_operand_type x, i386_operand_type y)
1885{
bab6aec1 1886 gas_assert (y.bitfield.class == ClassNone);
75e5731b 1887 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 1888
c6fb90c8
L
1889 switch (ARRAY_SIZE (x.array))
1890 {
1891 case 3:
1892 x.array [2] ^= y.array [2];
1a0670f3 1893 /* Fall through. */
c6fb90c8
L
1894 case 2:
1895 x.array [1] ^= y.array [1];
1a0670f3 1896 /* Fall through. */
c6fb90c8
L
1897 case 1:
1898 x.array [0] ^= y.array [0];
1899 break;
1900 default:
1901 abort ();
1902 }
40fb9820
L
1903 return x;
1904}
1905
40fb9820 1906static const i386_operand_type disp16_32 = OPERAND_TYPE_DISP16_32;
bab6aec1
JB
1907static const i386_operand_type anydisp = OPERAND_TYPE_ANYDISP;
1908static const i386_operand_type anyimm = OPERAND_TYPE_ANYIMM;
40fb9820
L
1909static const i386_operand_type regxmm = OPERAND_TYPE_REGXMM;
1910static const i386_operand_type imm8 = OPERAND_TYPE_IMM8;
1911static const i386_operand_type imm8s = OPERAND_TYPE_IMM8S;
1912static const i386_operand_type imm16 = OPERAND_TYPE_IMM16;
1913static const i386_operand_type imm32 = OPERAND_TYPE_IMM32;
1914static const i386_operand_type imm32s = OPERAND_TYPE_IMM32S;
1915static const i386_operand_type imm64 = OPERAND_TYPE_IMM64;
1916static const i386_operand_type imm16_32 = OPERAND_TYPE_IMM16_32;
1917static const i386_operand_type imm16_32s = OPERAND_TYPE_IMM16_32S;
1918static const i386_operand_type imm16_32_32s = OPERAND_TYPE_IMM16_32_32S;
1919
1920enum operand_type
1921{
1922 reg,
40fb9820
L
1923 imm,
1924 disp,
1925 anymem
1926};
1927
c6fb90c8 1928static INLINE int
40fb9820
L
1929operand_type_check (i386_operand_type t, enum operand_type c)
1930{
1931 switch (c)
1932 {
1933 case reg:
bab6aec1 1934 return t.bitfield.class == Reg;
40fb9820 1935
40fb9820
L
1936 case imm:
1937 return (t.bitfield.imm8
1938 || t.bitfield.imm8s
1939 || t.bitfield.imm16
1940 || t.bitfield.imm32
1941 || t.bitfield.imm32s
1942 || t.bitfield.imm64);
1943
1944 case disp:
1945 return (t.bitfield.disp8
1946 || t.bitfield.disp16
1947 || t.bitfield.disp32
40fb9820
L
1948 || t.bitfield.disp64);
1949
1950 case anymem:
1951 return (t.bitfield.disp8
1952 || t.bitfield.disp16
1953 || t.bitfield.disp32
40fb9820
L
1954 || t.bitfield.disp64
1955 || t.bitfield.baseindex);
1956
1957 default:
1958 abort ();
1959 }
2cfe26b6
AM
1960
1961 return 0;
40fb9820
L
1962}
1963
7a54636a
L
1964/* Return 1 if there is no conflict in 8bit/16bit/32bit/64bit/80bit size
1965 between operand GIVEN and opeand WANTED for instruction template T. */
5c07affc
L
1966
1967static INLINE int
7a54636a
L
1968match_operand_size (const insn_template *t, unsigned int wanted,
1969 unsigned int given)
5c07affc 1970{
3ac21baa
JB
1971 return !((i.types[given].bitfield.byte
1972 && !t->operand_types[wanted].bitfield.byte)
1973 || (i.types[given].bitfield.word
1974 && !t->operand_types[wanted].bitfield.word)
1975 || (i.types[given].bitfield.dword
1976 && !t->operand_types[wanted].bitfield.dword)
1977 || (i.types[given].bitfield.qword
1978 && !t->operand_types[wanted].bitfield.qword)
1979 || (i.types[given].bitfield.tbyte
1980 && !t->operand_types[wanted].bitfield.tbyte));
5c07affc
L
1981}
1982
dd40ce22
L
1983/* Return 1 if there is no conflict in SIMD register between operand
1984 GIVEN and opeand WANTED for instruction template T. */
1b54b8d7
JB
1985
1986static INLINE int
dd40ce22
L
1987match_simd_size (const insn_template *t, unsigned int wanted,
1988 unsigned int given)
1b54b8d7 1989{
3ac21baa
JB
1990 return !((i.types[given].bitfield.xmmword
1991 && !t->operand_types[wanted].bitfield.xmmword)
1992 || (i.types[given].bitfield.ymmword
1993 && !t->operand_types[wanted].bitfield.ymmword)
1994 || (i.types[given].bitfield.zmmword
260cd341
LC
1995 && !t->operand_types[wanted].bitfield.zmmword)
1996 || (i.types[given].bitfield.tmmword
1997 && !t->operand_types[wanted].bitfield.tmmword));
1b54b8d7
JB
1998}
1999
7a54636a
L
2000/* Return 1 if there is no conflict in any size between operand GIVEN
2001 and opeand WANTED for instruction template T. */
5c07affc
L
2002
2003static INLINE int
dd40ce22
L
2004match_mem_size (const insn_template *t, unsigned int wanted,
2005 unsigned int given)
5c07affc 2006{
7a54636a 2007 return (match_operand_size (t, wanted, given)
3ac21baa 2008 && !((i.types[given].bitfield.unspecified
5273a3cd 2009 && !i.broadcast.type
a5748e0d 2010 && !i.broadcast.bytes
3ac21baa
JB
2011 && !t->operand_types[wanted].bitfield.unspecified)
2012 || (i.types[given].bitfield.fword
2013 && !t->operand_types[wanted].bitfield.fword)
1b54b8d7
JB
2014 /* For scalar opcode templates to allow register and memory
2015 operands at the same time, some special casing is needed
d6793fa1
JB
2016 here. Also for v{,p}broadcast*, {,v}pmov{s,z}*, and
2017 down-conversion vpmov*. */
3528c362 2018 || ((t->operand_types[wanted].bitfield.class == RegSIMD
bc49bfd8
JB
2019 && t->operand_types[wanted].bitfield.byte
2020 + t->operand_types[wanted].bitfield.word
2021 + t->operand_types[wanted].bitfield.dword
2022 + t->operand_types[wanted].bitfield.qword
2023 > !!t->opcode_modifier.broadcast)
3ac21baa
JB
2024 ? (i.types[given].bitfield.xmmword
2025 || i.types[given].bitfield.ymmword
2026 || i.types[given].bitfield.zmmword)
2027 : !match_simd_size(t, wanted, given))));
5c07affc
L
2028}
2029
3ac21baa
JB
2030/* Return value has MATCH_STRAIGHT set if there is no size conflict on any
2031 operands for instruction template T, and it has MATCH_REVERSE set if there
2032 is no size conflict on any operands for the template with operands reversed
2033 (and the template allows for reversing in the first place). */
5c07affc 2034
3ac21baa
JB
2035#define MATCH_STRAIGHT 1
2036#define MATCH_REVERSE 2
2037
2038static INLINE unsigned int
d3ce72d0 2039operand_size_match (const insn_template *t)
5c07affc 2040{
3ac21baa 2041 unsigned int j, match = MATCH_STRAIGHT;
5c07affc 2042
0cfa3eb3 2043 /* Don't check non-absolute jump instructions. */
5c07affc 2044 if (t->opcode_modifier.jump
0cfa3eb3 2045 && t->opcode_modifier.jump != JUMP_ABSOLUTE)
5c07affc
L
2046 return match;
2047
2048 /* Check memory and accumulator operand size. */
2049 for (j = 0; j < i.operands; j++)
2050 {
3528c362
JB
2051 if (i.types[j].bitfield.class != Reg
2052 && i.types[j].bitfield.class != RegSIMD
601e8564 2053 && t->opcode_modifier.anysize)
5c07affc
L
2054 continue;
2055
bab6aec1 2056 if (t->operand_types[j].bitfield.class == Reg
7a54636a 2057 && !match_operand_size (t, j, j))
5c07affc
L
2058 {
2059 match = 0;
2060 break;
2061 }
2062
3528c362 2063 if (t->operand_types[j].bitfield.class == RegSIMD
3ac21baa 2064 && !match_simd_size (t, j, j))
1b54b8d7
JB
2065 {
2066 match = 0;
2067 break;
2068 }
2069
75e5731b 2070 if (t->operand_types[j].bitfield.instance == Accum
7a54636a 2071 && (!match_operand_size (t, j, j) || !match_simd_size (t, j, j)))
1b54b8d7
JB
2072 {
2073 match = 0;
2074 break;
2075 }
2076
c48dadc9 2077 if ((i.flags[j] & Operand_Mem) && !match_mem_size (t, j, j))
5c07affc
L
2078 {
2079 match = 0;
2080 break;
2081 }
2082 }
2083
3ac21baa 2084 if (!t->opcode_modifier.d)
7b94647a 2085 return match;
5c07affc
L
2086
2087 /* Check reverse. */
8bd915b7
JB
2088 gas_assert ((i.operands >= 2 && i.operands <= 3)
2089 || t->opcode_modifier.vexsources);
5c07affc 2090
f5eb1d70 2091 for (j = 0; j < i.operands; j++)
5c07affc 2092 {
f5eb1d70
JB
2093 unsigned int given = i.operands - j - 1;
2094
8bd915b7
JB
2095 /* For 4- and 5-operand insns VEX.W controls just the first two
2096 register operands. */
2097 if (t->opcode_modifier.vexsources)
2098 given = j < 2 ? 1 - j : j;
2099
bab6aec1 2100 if (t->operand_types[j].bitfield.class == Reg
f5eb1d70 2101 && !match_operand_size (t, j, given))
7b94647a 2102 return match;
5c07affc 2103
3528c362 2104 if (t->operand_types[j].bitfield.class == RegSIMD
f5eb1d70 2105 && !match_simd_size (t, j, given))
7b94647a 2106 return match;
dbbc8b7e 2107
75e5731b 2108 if (t->operand_types[j].bitfield.instance == Accum
f5eb1d70
JB
2109 && (!match_operand_size (t, j, given)
2110 || !match_simd_size (t, j, given)))
7b94647a 2111 return match;
dbbc8b7e 2112
f5eb1d70 2113 if ((i.flags[given] & Operand_Mem) && !match_mem_size (t, j, given))
7b94647a 2114 return match;
5c07affc
L
2115 }
2116
3ac21baa 2117 return match | MATCH_REVERSE;
5c07affc
L
2118}
2119
c6fb90c8 2120static INLINE int
40fb9820
L
2121operand_type_match (i386_operand_type overlap,
2122 i386_operand_type given)
2123{
2124 i386_operand_type temp = overlap;
2125
7d5e4556 2126 temp.bitfield.unspecified = 0;
5c07affc
L
2127 temp.bitfield.byte = 0;
2128 temp.bitfield.word = 0;
2129 temp.bitfield.dword = 0;
2130 temp.bitfield.fword = 0;
2131 temp.bitfield.qword = 0;
2132 temp.bitfield.tbyte = 0;
2133 temp.bitfield.xmmword = 0;
c0f3af97 2134 temp.bitfield.ymmword = 0;
43234a1e 2135 temp.bitfield.zmmword = 0;
260cd341 2136 temp.bitfield.tmmword = 0;
0dfbf9d7 2137 if (operand_type_all_zero (&temp))
891edac4 2138 goto mismatch;
40fb9820 2139
6f2f06be 2140 if (given.bitfield.baseindex == overlap.bitfield.baseindex)
891edac4
L
2141 return 1;
2142
dc1e8a47 2143 mismatch:
a65babc9 2144 i.error = operand_type_mismatch;
891edac4 2145 return 0;
40fb9820
L
2146}
2147
7d5e4556 2148/* If given types g0 and g1 are registers they must be of the same type
10c17abd 2149 unless the expected operand type register overlap is null.
5de4d9ef 2150 Some Intel syntax memory operand size checking also happens here. */
40fb9820 2151
c6fb90c8 2152static INLINE int
dc821c5f 2153operand_type_register_match (i386_operand_type g0,
40fb9820 2154 i386_operand_type t0,
40fb9820
L
2155 i386_operand_type g1,
2156 i386_operand_type t1)
2157{
bab6aec1 2158 if (g0.bitfield.class != Reg
3528c362 2159 && g0.bitfield.class != RegSIMD
10c17abd
JB
2160 && (!operand_type_check (g0, anymem)
2161 || g0.bitfield.unspecified
5de4d9ef
JB
2162 || (t0.bitfield.class != Reg
2163 && t0.bitfield.class != RegSIMD)))
40fb9820
L
2164 return 1;
2165
bab6aec1 2166 if (g1.bitfield.class != Reg
3528c362 2167 && g1.bitfield.class != RegSIMD
10c17abd
JB
2168 && (!operand_type_check (g1, anymem)
2169 || g1.bitfield.unspecified
5de4d9ef
JB
2170 || (t1.bitfield.class != Reg
2171 && t1.bitfield.class != RegSIMD)))
40fb9820
L
2172 return 1;
2173
dc821c5f
JB
2174 if (g0.bitfield.byte == g1.bitfield.byte
2175 && g0.bitfield.word == g1.bitfield.word
2176 && g0.bitfield.dword == g1.bitfield.dword
10c17abd
JB
2177 && g0.bitfield.qword == g1.bitfield.qword
2178 && g0.bitfield.xmmword == g1.bitfield.xmmword
2179 && g0.bitfield.ymmword == g1.bitfield.ymmword
2180 && g0.bitfield.zmmword == g1.bitfield.zmmword)
40fb9820
L
2181 return 1;
2182
c4d09633
JB
2183 /* If expectations overlap in no more than a single size, all is fine. */
2184 g0 = operand_type_and (t0, t1);
2185 if (g0.bitfield.byte
2186 + g0.bitfield.word
2187 + g0.bitfield.dword
2188 + g0.bitfield.qword
2189 + g0.bitfield.xmmword
2190 + g0.bitfield.ymmword
2191 + g0.bitfield.zmmword <= 1)
891edac4
L
2192 return 1;
2193
a65babc9 2194 i.error = register_type_mismatch;
891edac4
L
2195
2196 return 0;
40fb9820
L
2197}
2198
4c692bc7
JB
2199static INLINE unsigned int
2200register_number (const reg_entry *r)
2201{
2202 unsigned int nr = r->reg_num;
2203
2204 if (r->reg_flags & RegRex)
2205 nr += 8;
2206
200cbe0f
L
2207 if (r->reg_flags & RegVRex)
2208 nr += 16;
2209
4c692bc7
JB
2210 return nr;
2211}
2212
252b5132 2213static INLINE unsigned int
40fb9820 2214mode_from_disp_size (i386_operand_type t)
252b5132 2215{
b5014f7a 2216 if (t.bitfield.disp8)
40fb9820
L
2217 return 1;
2218 else if (t.bitfield.disp16
a775efc8 2219 || t.bitfield.disp32)
40fb9820
L
2220 return 2;
2221 else
2222 return 0;
252b5132
RH
2223}
2224
2225static INLINE int
65879393 2226fits_in_signed_byte (addressT num)
252b5132 2227{
65879393 2228 return num + 0x80 <= 0xff;
47926f60 2229}
252b5132
RH
2230
2231static INLINE int
65879393 2232fits_in_unsigned_byte (addressT num)
252b5132 2233{
65879393 2234 return num <= 0xff;
47926f60 2235}
252b5132
RH
2236
2237static INLINE int
65879393 2238fits_in_unsigned_word (addressT num)
252b5132 2239{
65879393 2240 return num <= 0xffff;
47926f60 2241}
252b5132
RH
2242
2243static INLINE int
65879393 2244fits_in_signed_word (addressT num)
252b5132 2245{
65879393 2246 return num + 0x8000 <= 0xffff;
47926f60 2247}
2a962e6d 2248
3e73aa7c 2249static INLINE int
65879393 2250fits_in_signed_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2251{
2252#ifndef BFD64
2253 return 1;
2254#else
65879393 2255 return num + 0x80000000 <= 0xffffffff;
3e73aa7c
JH
2256#endif
2257} /* fits_in_signed_long() */
2a962e6d 2258
3e73aa7c 2259static INLINE int
65879393 2260fits_in_unsigned_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2261{
2262#ifndef BFD64
2263 return 1;
2264#else
65879393 2265 return num <= 0xffffffff;
3e73aa7c
JH
2266#endif
2267} /* fits_in_unsigned_long() */
252b5132 2268
a442cac5
JB
2269static INLINE valueT extend_to_32bit_address (addressT num)
2270{
2271#ifdef BFD64
2272 if (fits_in_unsigned_long(num))
2273 return (num ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
2274
2275 if (!fits_in_signed_long (num))
2276 return num & 0xffffffff;
2277#endif
2278
2279 return num;
2280}
2281
43234a1e 2282static INLINE int
b5014f7a 2283fits_in_disp8 (offsetT num)
43234a1e
L
2284{
2285 int shift = i.memshift;
2286 unsigned int mask;
2287
2288 if (shift == -1)
2289 abort ();
2290
2291 mask = (1 << shift) - 1;
2292
2293 /* Return 0 if NUM isn't properly aligned. */
2294 if ((num & mask))
2295 return 0;
2296
2297 /* Check if NUM will fit in 8bit after shift. */
2298 return fits_in_signed_byte (num >> shift);
2299}
2300
a683cc34
SP
2301static INLINE int
2302fits_in_imm4 (offsetT num)
2303{
2304 return (num & 0xf) == num;
2305}
2306
40fb9820 2307static i386_operand_type
e3bb37b5 2308smallest_imm_type (offsetT num)
252b5132 2309{
40fb9820 2310 i386_operand_type t;
7ab9ffdd 2311
0dfbf9d7 2312 operand_type_set (&t, 0);
40fb9820
L
2313 t.bitfield.imm64 = 1;
2314
2315 if (cpu_arch_tune != PROCESSOR_I486 && num == 1)
e413e4e9
AM
2316 {
2317 /* This code is disabled on the 486 because all the Imm1 forms
2318 in the opcode table are slower on the i486. They're the
2319 versions with the implicitly specified single-position
2320 displacement, which has another syntax if you really want to
2321 use that form. */
40fb9820
L
2322 t.bitfield.imm1 = 1;
2323 t.bitfield.imm8 = 1;
2324 t.bitfield.imm8s = 1;
2325 t.bitfield.imm16 = 1;
2326 t.bitfield.imm32 = 1;
2327 t.bitfield.imm32s = 1;
2328 }
2329 else if (fits_in_signed_byte (num))
2330 {
2331 t.bitfield.imm8 = 1;
2332 t.bitfield.imm8s = 1;
2333 t.bitfield.imm16 = 1;
2334 t.bitfield.imm32 = 1;
2335 t.bitfield.imm32s = 1;
2336 }
2337 else if (fits_in_unsigned_byte (num))
2338 {
2339 t.bitfield.imm8 = 1;
2340 t.bitfield.imm16 = 1;
2341 t.bitfield.imm32 = 1;
2342 t.bitfield.imm32s = 1;
2343 }
2344 else if (fits_in_signed_word (num) || fits_in_unsigned_word (num))
2345 {
2346 t.bitfield.imm16 = 1;
2347 t.bitfield.imm32 = 1;
2348 t.bitfield.imm32s = 1;
2349 }
2350 else if (fits_in_signed_long (num))
2351 {
2352 t.bitfield.imm32 = 1;
2353 t.bitfield.imm32s = 1;
2354 }
2355 else if (fits_in_unsigned_long (num))
2356 t.bitfield.imm32 = 1;
2357
2358 return t;
47926f60 2359}
252b5132 2360
847f7ad4 2361static offsetT
e3bb37b5 2362offset_in_range (offsetT val, int size)
847f7ad4 2363{
508866be 2364 addressT mask;
ba2adb93 2365
847f7ad4
AM
2366 switch (size)
2367 {
508866be
L
2368 case 1: mask = ((addressT) 1 << 8) - 1; break;
2369 case 2: mask = ((addressT) 1 << 16) - 1; break;
3e73aa7c 2370#ifdef BFD64
64965897 2371 case 4: mask = ((addressT) 1 << 32) - 1; break;
3e73aa7c 2372#endif
64965897 2373 case sizeof (val): return val;
47926f60 2374 default: abort ();
847f7ad4
AM
2375 }
2376
4fe51f7d 2377 if ((val & ~mask) != 0 && (-val & ~mask) != 0)
f493c217
AM
2378 as_warn (_("0x%" PRIx64 " shortened to 0x%" PRIx64),
2379 (uint64_t) val, (uint64_t) (val & mask));
847f7ad4 2380
847f7ad4
AM
2381 return val & mask;
2382}
2383
c32fa91d
L
2384enum PREFIX_GROUP
2385{
2386 PREFIX_EXIST = 0,
2387 PREFIX_LOCK,
2388 PREFIX_REP,
04ef582a 2389 PREFIX_DS,
c32fa91d
L
2390 PREFIX_OTHER
2391};
2392
2393/* Returns
2394 a. PREFIX_EXIST if attempting to add a prefix where one from the
2395 same class already exists.
2396 b. PREFIX_LOCK if lock prefix is added.
2397 c. PREFIX_REP if rep/repne prefix is added.
04ef582a
L
2398 d. PREFIX_DS if ds prefix is added.
2399 e. PREFIX_OTHER if other prefix is added.
c32fa91d
L
2400 */
2401
2402static enum PREFIX_GROUP
e3bb37b5 2403add_prefix (unsigned int prefix)
252b5132 2404{
c32fa91d 2405 enum PREFIX_GROUP ret = PREFIX_OTHER;
b1905489 2406 unsigned int q;
252b5132 2407
29b0f896
AM
2408 if (prefix >= REX_OPCODE && prefix < REX_OPCODE + 16
2409 && flag_code == CODE_64BIT)
b1905489 2410 {
161a04f6 2411 if ((i.prefix[REX_PREFIX] & prefix & REX_W)
44846f29
JB
2412 || (i.prefix[REX_PREFIX] & prefix & REX_R)
2413 || (i.prefix[REX_PREFIX] & prefix & REX_X)
2414 || (i.prefix[REX_PREFIX] & prefix & REX_B))
c32fa91d 2415 ret = PREFIX_EXIST;
b1905489
JB
2416 q = REX_PREFIX;
2417 }
3e73aa7c 2418 else
b1905489
JB
2419 {
2420 switch (prefix)
2421 {
2422 default:
2423 abort ();
2424
b1905489 2425 case DS_PREFIX_OPCODE:
04ef582a
L
2426 ret = PREFIX_DS;
2427 /* Fall through. */
2428 case CS_PREFIX_OPCODE:
b1905489
JB
2429 case ES_PREFIX_OPCODE:
2430 case FS_PREFIX_OPCODE:
2431 case GS_PREFIX_OPCODE:
2432 case SS_PREFIX_OPCODE:
2433 q = SEG_PREFIX;
2434 break;
2435
2436 case REPNE_PREFIX_OPCODE:
2437 case REPE_PREFIX_OPCODE:
c32fa91d
L
2438 q = REP_PREFIX;
2439 ret = PREFIX_REP;
2440 break;
2441
b1905489 2442 case LOCK_PREFIX_OPCODE:
c32fa91d
L
2443 q = LOCK_PREFIX;
2444 ret = PREFIX_LOCK;
b1905489
JB
2445 break;
2446
2447 case FWAIT_OPCODE:
2448 q = WAIT_PREFIX;
2449 break;
2450
2451 case ADDR_PREFIX_OPCODE:
2452 q = ADDR_PREFIX;
2453 break;
2454
2455 case DATA_PREFIX_OPCODE:
2456 q = DATA_PREFIX;
2457 break;
2458 }
2459 if (i.prefix[q] != 0)
c32fa91d 2460 ret = PREFIX_EXIST;
b1905489 2461 }
252b5132 2462
b1905489 2463 if (ret)
252b5132 2464 {
b1905489
JB
2465 if (!i.prefix[q])
2466 ++i.prefixes;
2467 i.prefix[q] |= prefix;
252b5132 2468 }
b1905489
JB
2469 else
2470 as_bad (_("same type of prefix used twice"));
252b5132 2471
252b5132
RH
2472 return ret;
2473}
2474
2475static void
78f12dd3 2476update_code_flag (int value, int check)
eecb386c 2477{
78f12dd3
L
2478 PRINTF_LIKE ((*as_error));
2479
1e9cc1c2 2480 flag_code = (enum flag_code) value;
40fb9820
L
2481 if (flag_code == CODE_64BIT)
2482 {
2483 cpu_arch_flags.bitfield.cpu64 = 1;
2484 cpu_arch_flags.bitfield.cpuno64 = 0;
40fb9820
L
2485 }
2486 else
2487 {
2488 cpu_arch_flags.bitfield.cpu64 = 0;
2489 cpu_arch_flags.bitfield.cpuno64 = 1;
40fb9820
L
2490 }
2491 if (value == CODE_64BIT && !cpu_arch_flags.bitfield.cpulm )
3e73aa7c 2492 {
78f12dd3
L
2493 if (check)
2494 as_error = as_fatal;
2495 else
2496 as_error = as_bad;
2497 (*as_error) (_("64bit mode not supported on `%s'."),
2498 cpu_arch_name ? cpu_arch_name : default_arch);
3e73aa7c 2499 }
40fb9820 2500 if (value == CODE_32BIT && !cpu_arch_flags.bitfield.cpui386)
3e73aa7c 2501 {
78f12dd3
L
2502 if (check)
2503 as_error = as_fatal;
2504 else
2505 as_error = as_bad;
2506 (*as_error) (_("32bit mode not supported on `%s'."),
2507 cpu_arch_name ? cpu_arch_name : default_arch);
3e73aa7c 2508 }
eecb386c
AM
2509 stackop_size = '\0';
2510}
2511
78f12dd3
L
2512static void
2513set_code_flag (int value)
2514{
2515 update_code_flag (value, 0);
2516}
2517
eecb386c 2518static void
e3bb37b5 2519set_16bit_gcc_code_flag (int new_code_flag)
252b5132 2520{
1e9cc1c2 2521 flag_code = (enum flag_code) new_code_flag;
40fb9820
L
2522 if (flag_code != CODE_16BIT)
2523 abort ();
2524 cpu_arch_flags.bitfield.cpu64 = 0;
2525 cpu_arch_flags.bitfield.cpuno64 = 1;
9306ca4a 2526 stackop_size = LONG_MNEM_SUFFIX;
252b5132
RH
2527}
2528
2529static void
e3bb37b5 2530set_intel_syntax (int syntax_flag)
252b5132
RH
2531{
2532 /* Find out if register prefixing is specified. */
2533 int ask_naked_reg = 0;
2534
2535 SKIP_WHITESPACE ();
29b0f896 2536 if (!is_end_of_line[(unsigned char) *input_line_pointer])
252b5132 2537 {
d02603dc
NC
2538 char *string;
2539 int e = get_symbol_name (&string);
252b5132 2540
47926f60 2541 if (strcmp (string, "prefix") == 0)
252b5132 2542 ask_naked_reg = 1;
47926f60 2543 else if (strcmp (string, "noprefix") == 0)
252b5132
RH
2544 ask_naked_reg = -1;
2545 else
d0b47220 2546 as_bad (_("bad argument to syntax directive."));
d02603dc 2547 (void) restore_line_pointer (e);
252b5132
RH
2548 }
2549 demand_empty_rest_of_line ();
c3332e24 2550
252b5132
RH
2551 intel_syntax = syntax_flag;
2552
2553 if (ask_naked_reg == 0)
f86103b7
AM
2554 allow_naked_reg = (intel_syntax
2555 && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
252b5132
RH
2556 else
2557 allow_naked_reg = (ask_naked_reg < 0);
9306ca4a 2558
ee86248c 2559 expr_set_rank (O_full_ptr, syntax_flag ? 10 : 0);
7ab9ffdd 2560
e4a3b5a4 2561 identifier_chars['%'] = intel_syntax && allow_naked_reg ? '%' : 0;
9306ca4a 2562 identifier_chars['$'] = intel_syntax ? '$' : 0;
e4a3b5a4 2563 register_prefix = allow_naked_reg ? "" : "%";
252b5132
RH
2564}
2565
1efbbeb4
L
2566static void
2567set_intel_mnemonic (int mnemonic_flag)
2568{
e1d4d893 2569 intel_mnemonic = mnemonic_flag;
1efbbeb4
L
2570}
2571
db51cc60
L
2572static void
2573set_allow_index_reg (int flag)
2574{
2575 allow_index_reg = flag;
2576}
2577
cb19c032 2578static void
7bab8ab5 2579set_check (int what)
cb19c032 2580{
7bab8ab5
JB
2581 enum check_kind *kind;
2582 const char *str;
2583
2584 if (what)
2585 {
2586 kind = &operand_check;
2587 str = "operand";
2588 }
2589 else
2590 {
2591 kind = &sse_check;
2592 str = "sse";
2593 }
2594
cb19c032
L
2595 SKIP_WHITESPACE ();
2596
2597 if (!is_end_of_line[(unsigned char) *input_line_pointer])
2598 {
d02603dc
NC
2599 char *string;
2600 int e = get_symbol_name (&string);
cb19c032
L
2601
2602 if (strcmp (string, "none") == 0)
7bab8ab5 2603 *kind = check_none;
cb19c032 2604 else if (strcmp (string, "warning") == 0)
7bab8ab5 2605 *kind = check_warning;
cb19c032 2606 else if (strcmp (string, "error") == 0)
7bab8ab5 2607 *kind = check_error;
cb19c032 2608 else
7bab8ab5 2609 as_bad (_("bad argument to %s_check directive."), str);
d02603dc 2610 (void) restore_line_pointer (e);
cb19c032
L
2611 }
2612 else
7bab8ab5 2613 as_bad (_("missing argument for %s_check directive"), str);
cb19c032
L
2614
2615 demand_empty_rest_of_line ();
2616}
2617
8a9036a4
L
2618static void
2619check_cpu_arch_compatible (const char *name ATTRIBUTE_UNUSED,
1e9cc1c2 2620 i386_cpu_flags new_flag ATTRIBUTE_UNUSED)
8a9036a4
L
2621{
2622#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2623 static const char *arch;
2624
c085ab00 2625 /* Intel MCU is only supported on ELF. */
8a9036a4
L
2626 if (!IS_ELF)
2627 return;
2628
2629 if (!arch)
2630 {
2631 /* Use cpu_arch_name if it is set in md_parse_option. Otherwise
2632 use default_arch. */
2633 arch = cpu_arch_name;
2634 if (!arch)
2635 arch = default_arch;
2636 }
2637
81486035 2638 /* If we are targeting Intel MCU, we must enable it. */
648d04db
JB
2639 if ((get_elf_backend_data (stdoutput)->elf_machine_code == EM_IAMCU)
2640 == new_flag.bitfield.cpuiamcu)
81486035
L
2641 return;
2642
8a9036a4
L
2643 as_bad (_("`%s' is not supported on `%s'"), name, arch);
2644#endif
2645}
2646
8180707f
JB
2647static void
2648extend_cpu_sub_arch_name (const char *name)
2649{
2650 if (cpu_sub_arch_name)
2651 cpu_sub_arch_name = reconcat (cpu_sub_arch_name, cpu_sub_arch_name,
ae89daec 2652 ".", name, (const char *) NULL);
8180707f 2653 else
ae89daec 2654 cpu_sub_arch_name = concat (".", name, (const char *) NULL);
8180707f
JB
2655}
2656
e413e4e9 2657static void
e3bb37b5 2658set_cpu_arch (int dummy ATTRIBUTE_UNUSED)
e413e4e9 2659{
f68697e8
JB
2660 typedef struct arch_stack_entry
2661 {
2662 const struct arch_stack_entry *prev;
2663 const char *name;
2664 char *sub_name;
2665 i386_cpu_flags flags;
2666 i386_cpu_flags isa_flags;
2667 enum processor_type isa;
2668 enum flag_code flag_code;
2669 char stackop_size;
2670 bool no_cond_jump_promotion;
2671 } arch_stack_entry;
2672 static const arch_stack_entry *arch_stack_top;
2673
47926f60 2674 SKIP_WHITESPACE ();
e413e4e9 2675
29b0f896 2676 if (!is_end_of_line[(unsigned char) *input_line_pointer])
e413e4e9 2677 {
3ce2ebcf
JB
2678 char *s;
2679 int e = get_symbol_name (&s);
2680 const char *string = s;
2681 unsigned int j = 0;
40fb9820 2682 i386_cpu_flags flags;
e413e4e9 2683
3ce2ebcf
JB
2684 if (strcmp (string, "default") == 0)
2685 {
2686 if (strcmp (default_arch, "iamcu") == 0)
2687 string = default_arch;
2688 else
2689 {
2690 static const i386_cpu_flags cpu_unknown_flags = CPU_UNKNOWN_FLAGS;
2691
2692 cpu_arch_name = NULL;
2693 free (cpu_sub_arch_name);
2694 cpu_sub_arch_name = NULL;
2695 cpu_arch_flags = cpu_unknown_flags;
2696 if (flag_code == CODE_64BIT)
2697 {
2698 cpu_arch_flags.bitfield.cpu64 = 1;
2699 cpu_arch_flags.bitfield.cpuno64 = 0;
2700 }
2701 else
2702 {
2703 cpu_arch_flags.bitfield.cpu64 = 0;
2704 cpu_arch_flags.bitfield.cpuno64 = 1;
2705 }
2706 cpu_arch_isa = PROCESSOR_UNKNOWN;
ae89daec 2707 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
3ce2ebcf
JB
2708 if (!cpu_arch_tune_set)
2709 {
2710 cpu_arch_tune = cpu_arch_isa;
2711 cpu_arch_tune_flags = cpu_arch_isa_flags;
2712 }
2713
2714 j = ARRAY_SIZE (cpu_arch) + 1;
2715 }
2716 }
f68697e8
JB
2717 else if (strcmp (string, "push") == 0)
2718 {
2719 arch_stack_entry *top = XNEW (arch_stack_entry);
2720
2721 top->name = cpu_arch_name;
2722 if (cpu_sub_arch_name)
2723 top->sub_name = xstrdup (cpu_sub_arch_name);
2724 else
2725 top->sub_name = NULL;
2726 top->flags = cpu_arch_flags;
2727 top->isa = cpu_arch_isa;
2728 top->isa_flags = cpu_arch_isa_flags;
2729 top->flag_code = flag_code;
2730 top->stackop_size = stackop_size;
2731 top->no_cond_jump_promotion = no_cond_jump_promotion;
2732
2733 top->prev = arch_stack_top;
2734 arch_stack_top = top;
2735
2736 (void) restore_line_pointer (e);
2737 demand_empty_rest_of_line ();
2738 return;
2739 }
2740 else if (strcmp (string, "pop") == 0)
2741 {
2742 const arch_stack_entry *top = arch_stack_top;
2743
2744 if (!top)
2745 as_bad (_(".arch stack is empty"));
2746 else if (top->flag_code != flag_code
2747 || top->stackop_size != stackop_size)
2748 {
2749 static const unsigned int bits[] = {
2750 [CODE_16BIT] = 16,
2751 [CODE_32BIT] = 32,
2752 [CODE_64BIT] = 64,
2753 };
2754
2755 as_bad (_("this `.arch pop' requires `.code%u%s' to be in effect"),
2756 bits[top->flag_code],
2757 top->stackop_size == LONG_MNEM_SUFFIX ? "gcc" : "");
2758 }
2759 else
2760 {
2761 arch_stack_top = top->prev;
2762
2763 cpu_arch_name = top->name;
2764 free (cpu_sub_arch_name);
2765 cpu_sub_arch_name = top->sub_name;
2766 cpu_arch_flags = top->flags;
2767 cpu_arch_isa = top->isa;
2768 cpu_arch_isa_flags = top->isa_flags;
2769 no_cond_jump_promotion = top->no_cond_jump_promotion;
2770
2771 XDELETE (top);
2772 }
2773
2774 (void) restore_line_pointer (e);
2775 demand_empty_rest_of_line ();
2776 return;
2777 }
3ce2ebcf
JB
2778
2779 for (; j < ARRAY_SIZE (cpu_arch); j++)
e413e4e9 2780 {
ae89daec
JB
2781 if (strcmp (string + (*string == '.'), cpu_arch[j].name) == 0
2782 && (*string == '.') == (cpu_arch[j].type == PROCESSOR_NONE))
e413e4e9 2783 {
5c6af06e
JB
2784 if (*string != '.')
2785 {
ae89daec 2786 check_cpu_arch_compatible (string, cpu_arch[j].enable);
648d04db 2787
91d6fa6a 2788 cpu_arch_name = cpu_arch[j].name;
d92c7521 2789 free (cpu_sub_arch_name);
5c6af06e 2790 cpu_sub_arch_name = NULL;
ae89daec 2791 cpu_arch_flags = cpu_arch[j].enable;
40fb9820
L
2792 if (flag_code == CODE_64BIT)
2793 {
2794 cpu_arch_flags.bitfield.cpu64 = 1;
2795 cpu_arch_flags.bitfield.cpuno64 = 0;
2796 }
2797 else
2798 {
2799 cpu_arch_flags.bitfield.cpu64 = 0;
2800 cpu_arch_flags.bitfield.cpuno64 = 1;
2801 }
91d6fa6a 2802 cpu_arch_isa = cpu_arch[j].type;
ae89daec 2803 cpu_arch_isa_flags = cpu_arch[j].enable;
ccc9c027
L
2804 if (!cpu_arch_tune_set)
2805 {
2806 cpu_arch_tune = cpu_arch_isa;
2807 cpu_arch_tune_flags = cpu_arch_isa_flags;
2808 }
d59a54c2 2809 pre_386_16bit_warned = false;
5c6af06e
JB
2810 break;
2811 }
40fb9820 2812
ae89daec
JB
2813 if (cpu_flags_all_zero (&cpu_arch[j].enable))
2814 continue;
2815
293f5f65 2816 flags = cpu_flags_or (cpu_arch_flags,
ae89daec 2817 cpu_arch[j].enable);
81486035 2818
5b64d091 2819 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
5c6af06e 2820 {
ae89daec 2821 extend_cpu_sub_arch_name (string + 1);
40fb9820 2822 cpu_arch_flags = flags;
a586129e 2823 cpu_arch_isa_flags = flags;
5c6af06e 2824 }
0089dace
L
2825 else
2826 cpu_arch_isa_flags
2827 = cpu_flags_or (cpu_arch_isa_flags,
ae89daec 2828 cpu_arch[j].enable);
d02603dc 2829 (void) restore_line_pointer (e);
5c6af06e
JB
2830 demand_empty_rest_of_line ();
2831 return;
e413e4e9
AM
2832 }
2833 }
293f5f65 2834
ae89daec 2835 if (startswith (string, ".no") && j >= ARRAY_SIZE (cpu_arch))
293f5f65 2836 {
33eaf5de 2837 /* Disable an ISA extension. */
ae89daec
JB
2838 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
2839 if (cpu_arch[j].type == PROCESSOR_NONE
2840 && strcmp (string + 3, cpu_arch[j].name) == 0)
293f5f65
L
2841 {
2842 flags = cpu_flags_and_not (cpu_arch_flags,
ae89daec 2843 cpu_arch[j].disable);
293f5f65
L
2844 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
2845 {
ae89daec 2846 extend_cpu_sub_arch_name (string + 1);
293f5f65
L
2847 cpu_arch_flags = flags;
2848 cpu_arch_isa_flags = flags;
2849 }
2850 (void) restore_line_pointer (e);
2851 demand_empty_rest_of_line ();
2852 return;
2853 }
293f5f65
L
2854 }
2855
3ce2ebcf 2856 if (j == ARRAY_SIZE (cpu_arch))
e413e4e9
AM
2857 as_bad (_("no such architecture: `%s'"), string);
2858
2859 *input_line_pointer = e;
2860 }
2861 else
2862 as_bad (_("missing cpu architecture"));
2863
fddf5b5b
AM
2864 no_cond_jump_promotion = 0;
2865 if (*input_line_pointer == ','
29b0f896 2866 && !is_end_of_line[(unsigned char) input_line_pointer[1]])
fddf5b5b 2867 {
d02603dc
NC
2868 char *string;
2869 char e;
2870
2871 ++input_line_pointer;
2872 e = get_symbol_name (&string);
fddf5b5b
AM
2873
2874 if (strcmp (string, "nojumps") == 0)
2875 no_cond_jump_promotion = 1;
2876 else if (strcmp (string, "jumps") == 0)
2877 ;
2878 else
2879 as_bad (_("no such architecture modifier: `%s'"), string);
2880
d02603dc 2881 (void) restore_line_pointer (e);
fddf5b5b
AM
2882 }
2883
e413e4e9
AM
2884 demand_empty_rest_of_line ();
2885}
2886
8a9036a4
L
2887enum bfd_architecture
2888i386_arch (void)
2889{
c085ab00 2890 if (cpu_arch_isa == PROCESSOR_IAMCU)
81486035
L
2891 {
2892 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2893 || flag_code == CODE_64BIT)
2894 as_fatal (_("Intel MCU is 32bit ELF only"));
2895 return bfd_arch_iamcu;
2896 }
8a9036a4
L
2897 else
2898 return bfd_arch_i386;
2899}
2900
b9d79e03 2901unsigned long
7016a5d5 2902i386_mach (void)
b9d79e03 2903{
d34049e8 2904 if (startswith (default_arch, "x86_64"))
8a9036a4 2905 {
c085ab00 2906 if (default_arch[6] == '\0')
8a9036a4 2907 return bfd_mach_x86_64;
351f65ca
L
2908 else
2909 return bfd_mach_x64_32;
8a9036a4 2910 }
5197d474
L
2911 else if (!strcmp (default_arch, "i386")
2912 || !strcmp (default_arch, "iamcu"))
81486035
L
2913 {
2914 if (cpu_arch_isa == PROCESSOR_IAMCU)
2915 {
2916 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
2917 as_fatal (_("Intel MCU is 32bit ELF only"));
2918 return bfd_mach_i386_iamcu;
2919 }
2920 else
2921 return bfd_mach_i386_i386;
2922 }
b9d79e03 2923 else
2b5d6a91 2924 as_fatal (_("unknown architecture"));
b9d79e03 2925}
b9d79e03 2926\f
252b5132 2927void
7016a5d5 2928md_begin (void)
252b5132 2929{
86fa6981
L
2930 /* Support pseudo prefixes like {disp32}. */
2931 lex_type ['{'] = LEX_BEGIN_NAME;
2932
47926f60 2933 /* Initialize op_hash hash table. */
629310ab 2934 op_hash = str_htab_create ();
252b5132
RH
2935
2936 {
d3ce72d0 2937 const insn_template *optab;
29b0f896 2938 templates *core_optab;
252b5132 2939
47926f60
KH
2940 /* Setup for loop. */
2941 optab = i386_optab;
654d6f31 2942 core_optab = notes_alloc (sizeof (*core_optab));
252b5132
RH
2943 core_optab->start = optab;
2944
2945 while (1)
2946 {
2947 ++optab;
2948 if (optab->name == NULL
2949 || strcmp (optab->name, (optab - 1)->name) != 0)
2950 {
2951 /* different name --> ship out current template list;
47926f60 2952 add to hash table; & begin anew. */
252b5132 2953 core_optab->end = optab;
fe0e921f
AM
2954 if (str_hash_insert (op_hash, (optab - 1)->name, core_optab, 0))
2955 as_fatal (_("duplicate %s"), (optab - 1)->name);
2956
252b5132
RH
2957 if (optab->name == NULL)
2958 break;
654d6f31 2959 core_optab = notes_alloc (sizeof (*core_optab));
252b5132
RH
2960 core_optab->start = optab;
2961 }
2962 }
2963 }
2964
47926f60 2965 /* Initialize reg_hash hash table. */
629310ab 2966 reg_hash = str_htab_create ();
252b5132 2967 {
29b0f896 2968 const reg_entry *regtab;
c3fe08fa 2969 unsigned int regtab_size = i386_regtab_size;
252b5132 2970
c3fe08fa 2971 for (regtab = i386_regtab; regtab_size--; regtab++)
6225c532 2972 {
6288d05f
JB
2973 switch (regtab->reg_type.bitfield.class)
2974 {
2975 case Reg:
34684862
JB
2976 if (regtab->reg_type.bitfield.dword)
2977 {
2978 if (regtab->reg_type.bitfield.instance == Accum)
2979 reg_eax = regtab;
2980 }
2981 else if (regtab->reg_type.bitfield.tbyte)
6288d05f
JB
2982 {
2983 /* There's no point inserting st(<N>) in the hash table, as
2984 parentheses aren't included in register_chars[] anyway. */
2985 if (regtab->reg_type.bitfield.instance != Accum)
2986 continue;
2987 reg_st0 = regtab;
2988 }
2989 break;
2990
5e042380
JB
2991 case SReg:
2992 switch (regtab->reg_num)
2993 {
2994 case 0: reg_es = regtab; break;
2995 case 2: reg_ss = regtab; break;
2996 case 3: reg_ds = regtab; break;
2997 }
2998 break;
2999
6288d05f
JB
3000 case RegMask:
3001 if (!regtab->reg_num)
3002 reg_k0 = regtab;
3003 break;
3004 }
3005
6225c532
JB
3006 if (str_hash_insert (reg_hash, regtab->reg_name, regtab, 0) != NULL)
3007 as_fatal (_("duplicate %s"), regtab->reg_name);
6225c532 3008 }
252b5132
RH
3009 }
3010
47926f60 3011 /* Fill in lexical tables: mnemonic_chars, operand_chars. */
252b5132 3012 {
29b0f896
AM
3013 int c;
3014 char *p;
252b5132
RH
3015
3016 for (c = 0; c < 256; c++)
3017 {
014fbcda 3018 if (ISDIGIT (c) || ISLOWER (c))
252b5132
RH
3019 {
3020 mnemonic_chars[c] = c;
3021 register_chars[c] = c;
3022 operand_chars[c] = c;
3023 }
3882b010 3024 else if (ISUPPER (c))
252b5132 3025 {
3882b010 3026 mnemonic_chars[c] = TOLOWER (c);
252b5132
RH
3027 register_chars[c] = mnemonic_chars[c];
3028 operand_chars[c] = c;
3029 }
43234a1e 3030 else if (c == '{' || c == '}')
86fa6981
L
3031 {
3032 mnemonic_chars[c] = c;
3033 operand_chars[c] = c;
3034 }
b3983e5f
JB
3035#ifdef SVR4_COMMENT_CHARS
3036 else if (c == '\\' && strchr (i386_comment_chars, '/'))
3037 operand_chars[c] = c;
3038#endif
252b5132 3039
3882b010 3040 if (ISALPHA (c) || ISDIGIT (c))
252b5132
RH
3041 identifier_chars[c] = c;
3042 else if (c >= 128)
3043 {
3044 identifier_chars[c] = c;
3045 operand_chars[c] = c;
3046 }
3047 }
3048
3049#ifdef LEX_AT
3050 identifier_chars['@'] = '@';
32137342
NC
3051#endif
3052#ifdef LEX_QM
3053 identifier_chars['?'] = '?';
3054 operand_chars['?'] = '?';
252b5132 3055#endif
c0f3af97 3056 mnemonic_chars['_'] = '_';
791fe849 3057 mnemonic_chars['-'] = '-';
0003779b 3058 mnemonic_chars['.'] = '.';
252b5132
RH
3059 identifier_chars['_'] = '_';
3060 identifier_chars['.'] = '.';
3061
3062 for (p = operand_special_chars; *p != '\0'; p++)
3063 operand_chars[(unsigned char) *p] = *p;
3064 }
3065
a4447b93
RH
3066 if (flag_code == CODE_64BIT)
3067 {
ca19b261
KT
3068#if defined (OBJ_COFF) && defined (TE_PE)
3069 x86_dwarf2_return_column = (OUTPUT_FLAVOR == bfd_target_coff_flavour
3070 ? 32 : 16);
3071#else
a4447b93 3072 x86_dwarf2_return_column = 16;
ca19b261 3073#endif
61ff971f 3074 x86_cie_data_alignment = -8;
a4447b93
RH
3075 }
3076 else
3077 {
3078 x86_dwarf2_return_column = 8;
3079 x86_cie_data_alignment = -4;
3080 }
e379e5f3
L
3081
3082 /* NB: FUSED_JCC_PADDING frag must have sufficient room so that it
3083 can be turned into BRANCH_PREFIX frag. */
3084 if (align_branch_prefix_size > MAX_FUSED_JCC_PADDING_SIZE)
3085 abort ();
252b5132
RH
3086}
3087
3088void
e3bb37b5 3089i386_print_statistics (FILE *file)
252b5132 3090{
629310ab
ML
3091 htab_print_statistics (file, "i386 opcode", op_hash);
3092 htab_print_statistics (file, "i386 register", reg_hash);
252b5132 3093}
654d6f31
AM
3094
3095void
3096i386_md_end (void)
3097{
3098 htab_delete (op_hash);
3099 htab_delete (reg_hash);
3100}
252b5132 3101\f
252b5132
RH
3102#ifdef DEBUG386
3103
ce8a8b2f 3104/* Debugging routines for md_assemble. */
d3ce72d0 3105static void pte (insn_template *);
40fb9820 3106static void pt (i386_operand_type);
e3bb37b5
L
3107static void pe (expressionS *);
3108static void ps (symbolS *);
252b5132
RH
3109
3110static void
2c703856 3111pi (const char *line, i386_insn *x)
252b5132 3112{
09137c09 3113 unsigned int j;
252b5132
RH
3114
3115 fprintf (stdout, "%s: template ", line);
3116 pte (&x->tm);
09f131f2
JH
3117 fprintf (stdout, " address: base %s index %s scale %x\n",
3118 x->base_reg ? x->base_reg->reg_name : "none",
3119 x->index_reg ? x->index_reg->reg_name : "none",
3120 x->log2_scale_factor);
3121 fprintf (stdout, " modrm: mode %x reg %x reg/mem %x\n",
252b5132 3122 x->rm.mode, x->rm.reg, x->rm.regmem);
09f131f2
JH
3123 fprintf (stdout, " sib: base %x index %x scale %x\n",
3124 x->sib.base, x->sib.index, x->sib.scale);
3125 fprintf (stdout, " rex: 64bit %x extX %x extY %x extZ %x\n",
161a04f6
L
3126 (x->rex & REX_W) != 0,
3127 (x->rex & REX_R) != 0,
3128 (x->rex & REX_X) != 0,
3129 (x->rex & REX_B) != 0);
09137c09 3130 for (j = 0; j < x->operands; j++)
252b5132 3131 {
09137c09
SP
3132 fprintf (stdout, " #%d: ", j + 1);
3133 pt (x->types[j]);
252b5132 3134 fprintf (stdout, "\n");
bab6aec1 3135 if (x->types[j].bitfield.class == Reg
3528c362
JB
3136 || x->types[j].bitfield.class == RegMMX
3137 || x->types[j].bitfield.class == RegSIMD
dd6b8a0b 3138 || x->types[j].bitfield.class == RegMask
00cee14f 3139 || x->types[j].bitfield.class == SReg
4a5c67ed
JB
3140 || x->types[j].bitfield.class == RegCR
3141 || x->types[j].bitfield.class == RegDR
dd6b8a0b
JB
3142 || x->types[j].bitfield.class == RegTR
3143 || x->types[j].bitfield.class == RegBND)
09137c09
SP
3144 fprintf (stdout, "%s\n", x->op[j].regs->reg_name);
3145 if (operand_type_check (x->types[j], imm))
3146 pe (x->op[j].imms);
3147 if (operand_type_check (x->types[j], disp))
3148 pe (x->op[j].disps);
252b5132
RH
3149 }
3150}
3151
3152static void
d3ce72d0 3153pte (insn_template *t)
252b5132 3154{
b933fa4b 3155 static const unsigned char opc_pfx[] = { 0, 0x66, 0xf3, 0xf2 };
441f6aca 3156 static const char *const opc_spc[] = {
0cc78721 3157 NULL, "0f", "0f38", "0f3a", NULL, "evexmap5", "evexmap6", NULL,
441f6aca
JB
3158 "XOP08", "XOP09", "XOP0A",
3159 };
09137c09 3160 unsigned int j;
441f6aca 3161
252b5132 3162 fprintf (stdout, " %d operands ", t->operands);
441f6aca
JB
3163 if (opc_pfx[t->opcode_modifier.opcodeprefix])
3164 fprintf (stdout, "pfx %x ", opc_pfx[t->opcode_modifier.opcodeprefix]);
3165 if (opc_spc[t->opcode_modifier.opcodespace])
3166 fprintf (stdout, "space %s ", opc_spc[t->opcode_modifier.opcodespace]);
47926f60 3167 fprintf (stdout, "opcode %x ", t->base_opcode);
252b5132
RH
3168 if (t->extension_opcode != None)
3169 fprintf (stdout, "ext %x ", t->extension_opcode);
40fb9820 3170 if (t->opcode_modifier.d)
252b5132 3171 fprintf (stdout, "D");
40fb9820 3172 if (t->opcode_modifier.w)
252b5132
RH
3173 fprintf (stdout, "W");
3174 fprintf (stdout, "\n");
09137c09 3175 for (j = 0; j < t->operands; j++)
252b5132 3176 {
09137c09
SP
3177 fprintf (stdout, " #%d type ", j + 1);
3178 pt (t->operand_types[j]);
252b5132
RH
3179 fprintf (stdout, "\n");
3180 }
3181}
3182
3183static void
e3bb37b5 3184pe (expressionS *e)
252b5132 3185{
24eab124 3186 fprintf (stdout, " operation %d\n", e->X_op);
b8281767
AM
3187 fprintf (stdout, " add_number %" PRId64 " (%" PRIx64 ")\n",
3188 (int64_t) e->X_add_number, (uint64_t) (valueT) e->X_add_number);
252b5132
RH
3189 if (e->X_add_symbol)
3190 {
3191 fprintf (stdout, " add_symbol ");
3192 ps (e->X_add_symbol);
3193 fprintf (stdout, "\n");
3194 }
3195 if (e->X_op_symbol)
3196 {
3197 fprintf (stdout, " op_symbol ");
3198 ps (e->X_op_symbol);
3199 fprintf (stdout, "\n");
3200 }
3201}
3202
3203static void
e3bb37b5 3204ps (symbolS *s)
252b5132
RH
3205{
3206 fprintf (stdout, "%s type %s%s",
3207 S_GET_NAME (s),
3208 S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
3209 segment_name (S_GET_SEGMENT (s)));
3210}
3211
7b81dfbb 3212static struct type_name
252b5132 3213 {
40fb9820
L
3214 i386_operand_type mask;
3215 const char *name;
252b5132 3216 }
7b81dfbb 3217const type_names[] =
252b5132 3218{
40fb9820
L
3219 { OPERAND_TYPE_REG8, "r8" },
3220 { OPERAND_TYPE_REG16, "r16" },
3221 { OPERAND_TYPE_REG32, "r32" },
3222 { OPERAND_TYPE_REG64, "r64" },
2c703856
JB
3223 { OPERAND_TYPE_ACC8, "acc8" },
3224 { OPERAND_TYPE_ACC16, "acc16" },
3225 { OPERAND_TYPE_ACC32, "acc32" },
3226 { OPERAND_TYPE_ACC64, "acc64" },
40fb9820
L
3227 { OPERAND_TYPE_IMM8, "i8" },
3228 { OPERAND_TYPE_IMM8, "i8s" },
3229 { OPERAND_TYPE_IMM16, "i16" },
3230 { OPERAND_TYPE_IMM32, "i32" },
3231 { OPERAND_TYPE_IMM32S, "i32s" },
3232 { OPERAND_TYPE_IMM64, "i64" },
3233 { OPERAND_TYPE_IMM1, "i1" },
3234 { OPERAND_TYPE_BASEINDEX, "BaseIndex" },
3235 { OPERAND_TYPE_DISP8, "d8" },
3236 { OPERAND_TYPE_DISP16, "d16" },
3237 { OPERAND_TYPE_DISP32, "d32" },
40fb9820
L
3238 { OPERAND_TYPE_DISP64, "d64" },
3239 { OPERAND_TYPE_INOUTPORTREG, "InOutPortReg" },
3240 { OPERAND_TYPE_SHIFTCOUNT, "ShiftCount" },
3241 { OPERAND_TYPE_CONTROL, "control reg" },
3242 { OPERAND_TYPE_TEST, "test reg" },
3243 { OPERAND_TYPE_DEBUG, "debug reg" },
3244 { OPERAND_TYPE_FLOATREG, "FReg" },
3245 { OPERAND_TYPE_FLOATACC, "FAcc" },
21df382b 3246 { OPERAND_TYPE_SREG, "SReg" },
40fb9820
L
3247 { OPERAND_TYPE_REGMMX, "rMMX" },
3248 { OPERAND_TYPE_REGXMM, "rXMM" },
0349dc08 3249 { OPERAND_TYPE_REGYMM, "rYMM" },
43234a1e 3250 { OPERAND_TYPE_REGZMM, "rZMM" },
260cd341 3251 { OPERAND_TYPE_REGTMM, "rTMM" },
43234a1e 3252 { OPERAND_TYPE_REGMASK, "Mask reg" },
252b5132
RH
3253};
3254
3255static void
40fb9820 3256pt (i386_operand_type t)
252b5132 3257{
40fb9820 3258 unsigned int j;
c6fb90c8 3259 i386_operand_type a;
252b5132 3260
40fb9820 3261 for (j = 0; j < ARRAY_SIZE (type_names); j++)
c6fb90c8
L
3262 {
3263 a = operand_type_and (t, type_names[j].mask);
2c703856 3264 if (operand_type_equal (&a, &type_names[j].mask))
c6fb90c8
L
3265 fprintf (stdout, "%s, ", type_names[j].name);
3266 }
252b5132
RH
3267 fflush (stdout);
3268}
3269
3270#endif /* DEBUG386 */
3271\f
252b5132 3272static bfd_reloc_code_real_type
3956db08 3273reloc (unsigned int size,
64e74474
AM
3274 int pcrel,
3275 int sign,
3276 bfd_reloc_code_real_type other)
252b5132 3277{
47926f60 3278 if (other != NO_RELOC)
3956db08 3279 {
91d6fa6a 3280 reloc_howto_type *rel;
3956db08
JB
3281
3282 if (size == 8)
3283 switch (other)
3284 {
64e74474
AM
3285 case BFD_RELOC_X86_64_GOT32:
3286 return BFD_RELOC_X86_64_GOT64;
3287 break;
553d1284
L
3288 case BFD_RELOC_X86_64_GOTPLT64:
3289 return BFD_RELOC_X86_64_GOTPLT64;
3290 break;
64e74474
AM
3291 case BFD_RELOC_X86_64_PLTOFF64:
3292 return BFD_RELOC_X86_64_PLTOFF64;
3293 break;
3294 case BFD_RELOC_X86_64_GOTPC32:
3295 other = BFD_RELOC_X86_64_GOTPC64;
3296 break;
3297 case BFD_RELOC_X86_64_GOTPCREL:
3298 other = BFD_RELOC_X86_64_GOTPCREL64;
3299 break;
3300 case BFD_RELOC_X86_64_TPOFF32:
3301 other = BFD_RELOC_X86_64_TPOFF64;
3302 break;
3303 case BFD_RELOC_X86_64_DTPOFF32:
3304 other = BFD_RELOC_X86_64_DTPOFF64;
3305 break;
3306 default:
3307 break;
3956db08 3308 }
e05278af 3309
8ce3d284 3310#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
3311 if (other == BFD_RELOC_SIZE32)
3312 {
3313 if (size == 8)
1ab668bf 3314 other = BFD_RELOC_SIZE64;
8fd4256d 3315 if (pcrel)
1ab668bf
AM
3316 {
3317 as_bad (_("there are no pc-relative size relocations"));
3318 return NO_RELOC;
3319 }
8fd4256d 3320 }
8ce3d284 3321#endif
8fd4256d 3322
e05278af 3323 /* Sign-checking 4-byte relocations in 16-/32-bit code is pointless. */
f2d8a97c 3324 if (size == 4 && (flag_code != CODE_64BIT || disallow_64bit_reloc))
e05278af
JB
3325 sign = -1;
3326
91d6fa6a
NC
3327 rel = bfd_reloc_type_lookup (stdoutput, other);
3328 if (!rel)
3956db08 3329 as_bad (_("unknown relocation (%u)"), other);
91d6fa6a 3330 else if (size != bfd_get_reloc_size (rel))
3956db08 3331 as_bad (_("%u-byte relocation cannot be applied to %u-byte field"),
91d6fa6a 3332 bfd_get_reloc_size (rel),
3956db08 3333 size);
91d6fa6a 3334 else if (pcrel && !rel->pc_relative)
3956db08 3335 as_bad (_("non-pc-relative relocation for pc-relative field"));
91d6fa6a 3336 else if ((rel->complain_on_overflow == complain_overflow_signed
3956db08 3337 && !sign)
91d6fa6a 3338 || (rel->complain_on_overflow == complain_overflow_unsigned
64e74474 3339 && sign > 0))
3956db08
JB
3340 as_bad (_("relocated field and relocation type differ in signedness"));
3341 else
3342 return other;
3343 return NO_RELOC;
3344 }
252b5132
RH
3345
3346 if (pcrel)
3347 {
3e73aa7c 3348 if (!sign)
3956db08 3349 as_bad (_("there are no unsigned pc-relative relocations"));
252b5132
RH
3350 switch (size)
3351 {
3352 case 1: return BFD_RELOC_8_PCREL;
3353 case 2: return BFD_RELOC_16_PCREL;
d258b828 3354 case 4: return BFD_RELOC_32_PCREL;
d6ab8113 3355 case 8: return BFD_RELOC_64_PCREL;
252b5132 3356 }
3956db08 3357 as_bad (_("cannot do %u byte pc-relative relocation"), size);
252b5132
RH
3358 }
3359 else
3360 {
3956db08 3361 if (sign > 0)
e5cb08ac 3362 switch (size)
3e73aa7c
JH
3363 {
3364 case 4: return BFD_RELOC_X86_64_32S;
3365 }
3366 else
3367 switch (size)
3368 {
3369 case 1: return BFD_RELOC_8;
3370 case 2: return BFD_RELOC_16;
3371 case 4: return BFD_RELOC_32;
3372 case 8: return BFD_RELOC_64;
3373 }
3956db08
JB
3374 as_bad (_("cannot do %s %u byte relocation"),
3375 sign > 0 ? "signed" : "unsigned", size);
252b5132
RH
3376 }
3377
0cc9e1d3 3378 return NO_RELOC;
252b5132
RH
3379}
3380
47926f60
KH
3381/* Here we decide which fixups can be adjusted to make them relative to
3382 the beginning of the section instead of the symbol. Basically we need
3383 to make sure that the dynamic relocations are done correctly, so in
3384 some cases we force the original symbol to be used. */
3385
252b5132 3386int
e3bb37b5 3387tc_i386_fix_adjustable (fixS *fixP ATTRIBUTE_UNUSED)
252b5132 3388{
6d249963 3389#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 3390 if (!IS_ELF)
31312f95
AM
3391 return 1;
3392
a161fe53
AM
3393 /* Don't adjust pc-relative references to merge sections in 64-bit
3394 mode. */
3395 if (use_rela_relocations
3396 && (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0
3397 && fixP->fx_pcrel)
252b5132 3398 return 0;
31312f95 3399
8d01d9a9
AJ
3400 /* The x86_64 GOTPCREL are represented as 32bit PCrel relocations
3401 and changed later by validate_fix. */
3402 if (GOT_symbol && fixP->fx_subsy == GOT_symbol
3403 && fixP->fx_r_type == BFD_RELOC_32_PCREL)
3404 return 0;
3405
8fd4256d
L
3406 /* Adjust_reloc_syms doesn't know about the GOT. Need to keep symbol
3407 for size relocations. */
3408 if (fixP->fx_r_type == BFD_RELOC_SIZE32
3409 || fixP->fx_r_type == BFD_RELOC_SIZE64
3410 || fixP->fx_r_type == BFD_RELOC_386_GOTOFF
252b5132 3411 || fixP->fx_r_type == BFD_RELOC_386_GOT32
02a86693 3412 || fixP->fx_r_type == BFD_RELOC_386_GOT32X
13ae64f3
JJ
3413 || fixP->fx_r_type == BFD_RELOC_386_TLS_GD
3414 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDM
3415 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDO_32
3416 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE_32
37e55690
JJ
3417 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE
3418 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTIE
13ae64f3
JJ
3419 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE_32
3420 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE
67a4f2b7
AO
3421 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTDESC
3422 || fixP->fx_r_type == BFD_RELOC_386_TLS_DESC_CALL
3e73aa7c 3423 || fixP->fx_r_type == BFD_RELOC_X86_64_GOT32
80b3ee89 3424 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCREL
56ceb5b5
L
3425 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCRELX
3426 || fixP->fx_r_type == BFD_RELOC_X86_64_REX_GOTPCRELX
bffbf940
JJ
3427 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSGD
3428 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSLD
3429 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF32
d6ab8113 3430 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF64
bffbf940
JJ
3431 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF
3432 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF32
d6ab8113
JB
3433 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF64
3434 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTOFF64
67a4f2b7
AO
3435 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
3436 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSDESC_CALL
252b5132
RH
3437 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3438 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3439 return 0;
31312f95 3440#endif
252b5132
RH
3441 return 1;
3442}
252b5132 3443
a9aabc23
JB
3444static INLINE bool
3445want_disp32 (const insn_template *t)
3446{
3447 return flag_code != CODE_64BIT
3448 || i.prefix[ADDR_PREFIX]
3449 || (t->base_opcode == 0x8d
3450 && t->opcode_modifier.opcodespace == SPACE_BASE
fe134c65
JB
3451 && (!i.types[1].bitfield.qword
3452 || t->opcode_modifier.size == SIZE32));
a9aabc23
JB
3453}
3454
b4cac588 3455static int
e3bb37b5 3456intel_float_operand (const char *mnemonic)
252b5132 3457{
9306ca4a
JB
3458 /* Note that the value returned is meaningful only for opcodes with (memory)
3459 operands, hence the code here is free to improperly handle opcodes that
3460 have no operands (for better performance and smaller code). */
3461
3462 if (mnemonic[0] != 'f')
3463 return 0; /* non-math */
3464
3465 switch (mnemonic[1])
3466 {
3467 /* fclex, fdecstp, fdisi, femms, feni, fincstp, finit, fsetpm, and
3468 the fs segment override prefix not currently handled because no
3469 call path can make opcodes without operands get here */
3470 case 'i':
3471 return 2 /* integer op */;
3472 case 'l':
3473 if (mnemonic[2] == 'd' && (mnemonic[3] == 'c' || mnemonic[3] == 'e'))
3474 return 3; /* fldcw/fldenv */
3475 break;
3476 case 'n':
3477 if (mnemonic[2] != 'o' /* fnop */)
3478 return 3; /* non-waiting control op */
3479 break;
3480 case 'r':
3481 if (mnemonic[2] == 's')
3482 return 3; /* frstor/frstpm */
3483 break;
3484 case 's':
3485 if (mnemonic[2] == 'a')
3486 return 3; /* fsave */
3487 if (mnemonic[2] == 't')
3488 {
3489 switch (mnemonic[3])
3490 {
3491 case 'c': /* fstcw */
3492 case 'd': /* fstdw */
3493 case 'e': /* fstenv */
3494 case 's': /* fsts[gw] */
3495 return 3;
3496 }
3497 }
3498 break;
3499 case 'x':
3500 if (mnemonic[2] == 'r' || mnemonic[2] == 's')
3501 return 0; /* fxsave/fxrstor are not really math ops */
3502 break;
3503 }
252b5132 3504
9306ca4a 3505 return 1;
252b5132
RH
3506}
3507
9a182d04
JB
3508static INLINE void
3509install_template (const insn_template *t)
3510{
3511 unsigned int l;
3512
3513 i.tm = *t;
3514
3515 /* Note that for pseudo prefixes this produces a length of 1. But for them
3516 the length isn't interesting at all. */
3517 for (l = 1; l < 4; ++l)
3518 if (!(t->base_opcode >> (8 * l)))
3519 break;
3520
3521 i.opcode_length = l;
3522}
3523
c0f3af97
L
3524/* Build the VEX prefix. */
3525
3526static void
d3ce72d0 3527build_vex_prefix (const insn_template *t)
c0f3af97
L
3528{
3529 unsigned int register_specifier;
c0f3af97 3530 unsigned int vector_length;
03751133 3531 unsigned int w;
c0f3af97
L
3532
3533 /* Check register specifier. */
3534 if (i.vex.register_specifier)
43234a1e
L
3535 {
3536 register_specifier =
3537 ~register_number (i.vex.register_specifier) & 0xf;
3538 gas_assert ((i.vex.register_specifier->reg_flags & RegVRex) == 0);
3539 }
c0f3af97
L
3540 else
3541 register_specifier = 0xf;
3542
79f0fa25
L
3543 /* Use 2-byte VEX prefix by swapping destination and source operand
3544 if there are more than 1 register operand. */
3545 if (i.reg_operands > 1
3546 && i.vec_encoding != vex_encoding_vex3
86fa6981 3547 && i.dir_encoding == dir_encoding_default
fa99fab2 3548 && i.operands == i.reg_operands
dbbc8b7e 3549 && operand_type_equal (&i.types[0], &i.types[i.operands - 1])
441f6aca 3550 && i.tm.opcode_modifier.opcodespace == SPACE_0F
dbbc8b7e 3551 && (i.tm.opcode_modifier.load || i.tm.opcode_modifier.d)
fa99fab2
L
3552 && i.rex == REX_B)
3553 {
3554 unsigned int xchg = i.operands - 1;
3555 union i386_op temp_op;
3556 i386_operand_type temp_type;
3557
3558 temp_type = i.types[xchg];
3559 i.types[xchg] = i.types[0];
3560 i.types[0] = temp_type;
3561 temp_op = i.op[xchg];
3562 i.op[xchg] = i.op[0];
3563 i.op[0] = temp_op;
3564
9c2799c2 3565 gas_assert (i.rm.mode == 3);
fa99fab2
L
3566
3567 i.rex = REX_R;
3568 xchg = i.rm.regmem;
3569 i.rm.regmem = i.rm.reg;
3570 i.rm.reg = xchg;
3571
dbbc8b7e
JB
3572 if (i.tm.opcode_modifier.d)
3573 i.tm.base_opcode ^= (i.tm.base_opcode & 0xee) != 0x6e
2c735193 3574 ? Opcode_ExtD : Opcode_SIMD_IntD;
dbbc8b7e 3575 else /* Use the next insn. */
9a182d04 3576 install_template (&t[1]);
fa99fab2
L
3577 }
3578
79dec6b7
JB
3579 /* Use 2-byte VEX prefix by swapping commutative source operands if there
3580 are no memory operands and at least 3 register ones. */
3581 if (i.reg_operands >= 3
3582 && i.vec_encoding != vex_encoding_vex3
3583 && i.reg_operands == i.operands - i.imm_operands
3584 && i.tm.opcode_modifier.vex
3585 && i.tm.opcode_modifier.commutative
3586 && (i.tm.opcode_modifier.sse2avx || optimize > 1)
3587 && i.rex == REX_B
3588 && i.vex.register_specifier
3589 && !(i.vex.register_specifier->reg_flags & RegRex))
3590 {
3591 unsigned int xchg = i.operands - i.reg_operands;
3592 union i386_op temp_op;
3593 i386_operand_type temp_type;
3594
441f6aca 3595 gas_assert (i.tm.opcode_modifier.opcodespace == SPACE_0F);
79dec6b7
JB
3596 gas_assert (!i.tm.opcode_modifier.sae);
3597 gas_assert (operand_type_equal (&i.types[i.operands - 2],
3598 &i.types[i.operands - 3]));
3599 gas_assert (i.rm.mode == 3);
3600
3601 temp_type = i.types[xchg];
3602 i.types[xchg] = i.types[xchg + 1];
3603 i.types[xchg + 1] = temp_type;
3604 temp_op = i.op[xchg];
3605 i.op[xchg] = i.op[xchg + 1];
3606 i.op[xchg + 1] = temp_op;
3607
3608 i.rex = 0;
3609 xchg = i.rm.regmem | 8;
3610 i.rm.regmem = ~register_specifier & 0xf;
3611 gas_assert (!(i.rm.regmem & 8));
3612 i.vex.register_specifier += xchg - i.rm.regmem;
3613 register_specifier = ~xchg & 0xf;
3614 }
3615
539f890d
L
3616 if (i.tm.opcode_modifier.vex == VEXScalar)
3617 vector_length = avxscalar;
10c17abd
JB
3618 else if (i.tm.opcode_modifier.vex == VEX256)
3619 vector_length = 1;
539f890d 3620 else
10c17abd 3621 {
56522fc5 3622 unsigned int op;
10c17abd 3623
c7213af9
L
3624 /* Determine vector length from the last multi-length vector
3625 operand. */
10c17abd 3626 vector_length = 0;
56522fc5 3627 for (op = t->operands; op--;)
10c17abd
JB
3628 if (t->operand_types[op].bitfield.xmmword
3629 && t->operand_types[op].bitfield.ymmword
3630 && i.types[op].bitfield.ymmword)
3631 {
3632 vector_length = 1;
3633 break;
3634 }
3635 }
c0f3af97 3636
03751133
L
3637 /* Check the REX.W bit and VEXW. */
3638 if (i.tm.opcode_modifier.vexw == VEXWIG)
3639 w = (vexwig == vexw1 || (i.rex & REX_W)) ? 1 : 0;
3640 else if (i.tm.opcode_modifier.vexw)
3641 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3642 else
931d03b7 3643 w = (flag_code == CODE_64BIT ? i.rex & REX_W : vexwig == vexw1) ? 1 : 0;
03751133 3644
c0f3af97 3645 /* Use 2-byte VEX prefix if possible. */
03751133
L
3646 if (w == 0
3647 && i.vec_encoding != vex_encoding_vex3
441f6aca 3648 && i.tm.opcode_modifier.opcodespace == SPACE_0F
c0f3af97
L
3649 && (i.rex & (REX_W | REX_X | REX_B)) == 0)
3650 {
3651 /* 2-byte VEX prefix. */
3652 unsigned int r;
3653
3654 i.vex.length = 2;
3655 i.vex.bytes[0] = 0xc5;
3656
3657 /* Check the REX.R bit. */
3658 r = (i.rex & REX_R) ? 0 : 1;
3659 i.vex.bytes[1] = (r << 7
3660 | register_specifier << 3
3661 | vector_length << 2
35648716 3662 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3663 }
3664 else
3665 {
3666 /* 3-byte VEX prefix. */
f88c9eb0 3667 i.vex.length = 3;
f88c9eb0 3668
441f6aca 3669 switch (i.tm.opcode_modifier.opcodespace)
5dd85c99 3670 {
441f6aca
JB
3671 case SPACE_0F:
3672 case SPACE_0F38:
3673 case SPACE_0F3A:
80de6e00 3674 i.vex.bytes[0] = 0xc4;
7f399153 3675 break;
441f6aca
JB
3676 case SPACE_XOP08:
3677 case SPACE_XOP09:
3678 case SPACE_XOP0A:
f88c9eb0 3679 i.vex.bytes[0] = 0x8f;
7f399153
L
3680 break;
3681 default:
3682 abort ();
f88c9eb0 3683 }
c0f3af97 3684
c0f3af97
L
3685 /* The high 3 bits of the second VEX byte are 1's compliment
3686 of RXB bits from REX. */
441f6aca 3687 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | i.tm.opcode_modifier.opcodespace;
c0f3af97 3688
c0f3af97
L
3689 i.vex.bytes[2] = (w << 7
3690 | register_specifier << 3
3691 | vector_length << 2
35648716 3692 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3693 }
3694}
3695
5b7c81bd 3696static INLINE bool
e771e7c9
JB
3697is_evex_encoding (const insn_template *t)
3698{
7091c612 3699 return t->opcode_modifier.evex || t->opcode_modifier.disp8memshift
e771e7c9 3700 || t->opcode_modifier.broadcast || t->opcode_modifier.masking
a80195f1 3701 || t->opcode_modifier.sae;
e771e7c9
JB
3702}
3703
5b7c81bd 3704static INLINE bool
7a8655d2
JB
3705is_any_vex_encoding (const insn_template *t)
3706{
7b47a312 3707 return t->opcode_modifier.vex || is_evex_encoding (t);
7a8655d2
JB
3708}
3709
a5748e0d
JB
3710static unsigned int
3711get_broadcast_bytes (const insn_template *t, bool diag)
3712{
3713 unsigned int op, bytes;
3714 const i386_operand_type *types;
3715
3716 if (i.broadcast.type)
3717 return i.broadcast.bytes = ((1 << (t->opcode_modifier.broadcast - 1))
3718 * i.broadcast.type);
3719
3720 gas_assert (intel_syntax);
3721
3722 for (op = 0; op < t->operands; ++op)
3723 if (t->operand_types[op].bitfield.baseindex)
3724 break;
3725
3726 gas_assert (op < t->operands);
3727
3728 if (t->opcode_modifier.evex
3729 && t->opcode_modifier.evex != EVEXDYN)
3730 switch (i.broadcast.bytes)
3731 {
3732 case 1:
3733 if (t->operand_types[op].bitfield.word)
3734 return 2;
3735 /* Fall through. */
3736 case 2:
3737 if (t->operand_types[op].bitfield.dword)
3738 return 4;
3739 /* Fall through. */
3740 case 4:
3741 if (t->operand_types[op].bitfield.qword)
3742 return 8;
3743 /* Fall through. */
3744 case 8:
3745 if (t->operand_types[op].bitfield.xmmword)
3746 return 16;
3747 if (t->operand_types[op].bitfield.ymmword)
3748 return 32;
3749 if (t->operand_types[op].bitfield.zmmword)
3750 return 64;
3751 /* Fall through. */
3752 default:
3753 abort ();
3754 }
3755
3756 gas_assert (op + 1 < t->operands);
3757
3758 if (t->operand_types[op + 1].bitfield.xmmword
3759 + t->operand_types[op + 1].bitfield.ymmword
3760 + t->operand_types[op + 1].bitfield.zmmword > 1)
3761 {
3762 types = &i.types[op + 1];
3763 diag = false;
3764 }
3765 else /* Ambiguous - guess with a preference to non-AVX512VL forms. */
3766 types = &t->operand_types[op];
3767
3768 if (types->bitfield.zmmword)
3769 bytes = 64;
3770 else if (types->bitfield.ymmword)
3771 bytes = 32;
3772 else
3773 bytes = 16;
3774
3775 if (diag)
3776 as_warn (_("ambiguous broadcast for `%s', using %u-bit form"),
3777 t->name, bytes * 8);
3778
3779 return bytes;
3780}
3781
43234a1e
L
3782/* Build the EVEX prefix. */
3783
3784static void
3785build_evex_prefix (void)
3786{
35648716 3787 unsigned int register_specifier, w;
43234a1e
L
3788 rex_byte vrex_used = 0;
3789
3790 /* Check register specifier. */
3791 if (i.vex.register_specifier)
3792 {
3793 gas_assert ((i.vrex & REX_X) == 0);
3794
3795 register_specifier = i.vex.register_specifier->reg_num;
3796 if ((i.vex.register_specifier->reg_flags & RegRex))
3797 register_specifier += 8;
3798 /* The upper 16 registers are encoded in the fourth byte of the
3799 EVEX prefix. */
3800 if (!(i.vex.register_specifier->reg_flags & RegVRex))
3801 i.vex.bytes[3] = 0x8;
3802 register_specifier = ~register_specifier & 0xf;
3803 }
3804 else
3805 {
3806 register_specifier = 0xf;
3807
3808 /* Encode upper 16 vector index register in the fourth byte of
3809 the EVEX prefix. */
3810 if (!(i.vrex & REX_X))
3811 i.vex.bytes[3] = 0x8;
3812 else
3813 vrex_used |= REX_X;
3814 }
3815
43234a1e
L
3816 /* 4 byte EVEX prefix. */
3817 i.vex.length = 4;
3818 i.vex.bytes[0] = 0x62;
3819
43234a1e
L
3820 /* The high 3 bits of the second EVEX byte are 1's compliment of RXB
3821 bits from REX. */
441f6aca 3822 gas_assert (i.tm.opcode_modifier.opcodespace >= SPACE_0F);
0cc78721 3823 gas_assert (i.tm.opcode_modifier.opcodespace <= SPACE_EVEXMAP6);
441f6aca 3824 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | i.tm.opcode_modifier.opcodespace;
43234a1e
L
3825
3826 /* The fifth bit of the second EVEX byte is 1's compliment of the
3827 REX_R bit in VREX. */
3828 if (!(i.vrex & REX_R))
3829 i.vex.bytes[1] |= 0x10;
3830 else
3831 vrex_used |= REX_R;
3832
3833 if ((i.reg_operands + i.imm_operands) == i.operands)
3834 {
3835 /* When all operands are registers, the REX_X bit in REX is not
3836 used. We reuse it to encode the upper 16 registers, which is
3837 indicated by the REX_B bit in VREX. The REX_X bit is encoded
3838 as 1's compliment. */
3839 if ((i.vrex & REX_B))
3840 {
3841 vrex_used |= REX_B;
3842 i.vex.bytes[1] &= ~0x40;
3843 }
3844 }
3845
3846 /* EVEX instructions shouldn't need the REX prefix. */
3847 i.vrex &= ~vrex_used;
3848 gas_assert (i.vrex == 0);
3849
6865c043
L
3850 /* Check the REX.W bit and VEXW. */
3851 if (i.tm.opcode_modifier.vexw == VEXWIG)
3852 w = (evexwig == evexw1 || (i.rex & REX_W)) ? 1 : 0;
3853 else if (i.tm.opcode_modifier.vexw)
3854 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3855 else
931d03b7 3856 w = (flag_code == CODE_64BIT ? i.rex & REX_W : evexwig == evexw1) ? 1 : 0;
43234a1e 3857
43234a1e 3858 /* The third byte of the EVEX prefix. */
35648716
JB
3859 i.vex.bytes[2] = ((w << 7)
3860 | (register_specifier << 3)
3861 | 4 /* Encode the U bit. */
3862 | i.tm.opcode_modifier.opcodeprefix);
43234a1e
L
3863
3864 /* The fourth byte of the EVEX prefix. */
3865 /* The zeroing-masking bit. */
6225c532 3866 if (i.mask.reg && i.mask.zeroing)
43234a1e
L
3867 i.vex.bytes[3] |= 0x80;
3868
3869 /* Don't always set the broadcast bit if there is no RC. */
ca5312a2 3870 if (i.rounding.type == rc_none)
43234a1e
L
3871 {
3872 /* Encode the vector length. */
3873 unsigned int vec_length;
3874
e771e7c9
JB
3875 if (!i.tm.opcode_modifier.evex
3876 || i.tm.opcode_modifier.evex == EVEXDYN)
3877 {
56522fc5 3878 unsigned int op;
e771e7c9 3879
c7213af9
L
3880 /* Determine vector length from the last multi-length vector
3881 operand. */
56522fc5 3882 for (op = i.operands; op--;)
e771e7c9
JB
3883 if (i.tm.operand_types[op].bitfield.xmmword
3884 + i.tm.operand_types[op].bitfield.ymmword
3885 + i.tm.operand_types[op].bitfield.zmmword > 1)
3886 {
3887 if (i.types[op].bitfield.zmmword)
c7213af9
L
3888 {
3889 i.tm.opcode_modifier.evex = EVEX512;
3890 break;
3891 }
e771e7c9 3892 else if (i.types[op].bitfield.ymmword)
c7213af9
L
3893 {
3894 i.tm.opcode_modifier.evex = EVEX256;
3895 break;
3896 }
e771e7c9 3897 else if (i.types[op].bitfield.xmmword)
c7213af9
L
3898 {
3899 i.tm.opcode_modifier.evex = EVEX128;
3900 break;
3901 }
a5748e0d 3902 else if (i.broadcast.bytes && op == i.broadcast.operand)
625cbd7a 3903 {
a5748e0d 3904 switch (get_broadcast_bytes (&i.tm, true))
625cbd7a
JB
3905 {
3906 case 64:
3907 i.tm.opcode_modifier.evex = EVEX512;
3908 break;
3909 case 32:
3910 i.tm.opcode_modifier.evex = EVEX256;
3911 break;
3912 case 16:
3913 i.tm.opcode_modifier.evex = EVEX128;
3914 break;
3915 default:
c7213af9 3916 abort ();
625cbd7a 3917 }
c7213af9 3918 break;
625cbd7a 3919 }
e771e7c9 3920 }
c7213af9 3921
56522fc5 3922 if (op >= MAX_OPERANDS)
c7213af9 3923 abort ();
e771e7c9
JB
3924 }
3925
43234a1e
L
3926 switch (i.tm.opcode_modifier.evex)
3927 {
3928 case EVEXLIG: /* LL' is ignored */
3929 vec_length = evexlig << 5;
3930 break;
3931 case EVEX128:
3932 vec_length = 0 << 5;
3933 break;
3934 case EVEX256:
3935 vec_length = 1 << 5;
3936 break;
3937 case EVEX512:
3938 vec_length = 2 << 5;
3939 break;
3940 default:
3941 abort ();
3942 break;
3943 }
3944 i.vex.bytes[3] |= vec_length;
3945 /* Encode the broadcast bit. */
a5748e0d 3946 if (i.broadcast.bytes)
43234a1e
L
3947 i.vex.bytes[3] |= 0x10;
3948 }
ca5312a2
JB
3949 else if (i.rounding.type != saeonly)
3950 i.vex.bytes[3] |= 0x10 | (i.rounding.type << 5);
43234a1e 3951 else
ca5312a2 3952 i.vex.bytes[3] |= 0x10 | (evexrcig << 5);
43234a1e 3953
6225c532
JB
3954 if (i.mask.reg)
3955 i.vex.bytes[3] |= i.mask.reg->reg_num;
43234a1e
L
3956}
3957
65da13b5
L
3958static void
3959process_immext (void)
3960{
3961 expressionS *exp;
3962
c0f3af97 3963 /* These AMD 3DNow! and SSE2 instructions have an opcode suffix
65da13b5
L
3964 which is coded in the same place as an 8-bit immediate field
3965 would be. Here we fake an 8-bit immediate operand from the
3966 opcode suffix stored in tm.extension_opcode.
3967
c1e679ec 3968 AVX instructions also use this encoding, for some of
c0f3af97 3969 3 argument instructions. */
65da13b5 3970
43234a1e 3971 gas_assert (i.imm_operands <= 1
7ab9ffdd 3972 && (i.operands <= 2
7a8655d2 3973 || (is_any_vex_encoding (&i.tm)
7ab9ffdd 3974 && i.operands <= 4)));
65da13b5
L
3975
3976 exp = &im_expressions[i.imm_operands++];
3977 i.op[i.operands].imms = exp;
3978 i.types[i.operands] = imm8;
3979 i.operands++;
3980 exp->X_op = O_constant;
3981 exp->X_add_number = i.tm.extension_opcode;
3982 i.tm.extension_opcode = None;
3983}
3984
42164a71
L
3985
3986static int
3987check_hle (void)
3988{
742732c7 3989 switch (i.tm.opcode_modifier.prefixok)
42164a71
L
3990 {
3991 default:
3992 abort ();
742732c7
JB
3993 case PrefixLock:
3994 case PrefixNone:
3995 case PrefixNoTrack:
3996 case PrefixRep:
165de32a
L
3997 as_bad (_("invalid instruction `%s' after `%s'"),
3998 i.tm.name, i.hle_prefix);
42164a71 3999 return 0;
742732c7 4000 case PrefixHLELock:
42164a71
L
4001 if (i.prefix[LOCK_PREFIX])
4002 return 1;
165de32a 4003 as_bad (_("missing `lock' with `%s'"), i.hle_prefix);
42164a71 4004 return 0;
742732c7 4005 case PrefixHLEAny:
42164a71 4006 return 1;
742732c7 4007 case PrefixHLERelease:
42164a71
L
4008 if (i.prefix[HLE_PREFIX] != XRELEASE_PREFIX_OPCODE)
4009 {
4010 as_bad (_("instruction `%s' after `xacquire' not allowed"),
4011 i.tm.name);
4012 return 0;
4013 }
8dc0818e 4014 if (i.mem_operands == 0 || !(i.flags[i.operands - 1] & Operand_Mem))
42164a71
L
4015 {
4016 as_bad (_("memory destination needed for instruction `%s'"
4017 " after `xrelease'"), i.tm.name);
4018 return 0;
4019 }
4020 return 1;
4021 }
4022}
4023
c8480b58
L
4024/* Encode aligned vector move as unaligned vector move. */
4025
4026static void
4027encode_with_unaligned_vector_move (void)
4028{
4029 switch (i.tm.base_opcode)
4030 {
b3a9fe6f
L
4031 case 0x28: /* Load instructions. */
4032 case 0x29: /* Store instructions. */
c8480b58
L
4033 /* movaps/movapd/vmovaps/vmovapd. */
4034 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4035 && i.tm.opcode_modifier.opcodeprefix <= PREFIX_0X66)
b3a9fe6f 4036 i.tm.base_opcode = 0x10 | (i.tm.base_opcode & 1);
c8480b58 4037 break;
b3a9fe6f
L
4038 case 0x6f: /* Load instructions. */
4039 case 0x7f: /* Store instructions. */
c8480b58
L
4040 /* movdqa/vmovdqa/vmovdqa64/vmovdqa32. */
4041 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4042 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0X66)
4043 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
4044 break;
4045 default:
4046 break;
4047 }
4048}
4049
b6f8c7c4
L
4050/* Try the shortest encoding by shortening operand size. */
4051
4052static void
4053optimize_encoding (void)
4054{
a0a1771e 4055 unsigned int j;
b6f8c7c4 4056
fe134c65
JB
4057 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
4058 && i.tm.base_opcode == 0x8d)
4059 {
4060 /* Optimize: -O:
4061 lea symbol, %rN -> mov $symbol, %rN
4062 lea (%rM), %rN -> mov %rM, %rN
4063 lea (,%rM,1), %rN -> mov %rM, %rN
4064
4065 and in 32-bit mode for 16-bit addressing
4066
4067 lea (%rM), %rN -> movzx %rM, %rN
4068
4069 and in 64-bit mode zap 32-bit addressing in favor of using a
4070 32-bit (or less) destination.
4071 */
4072 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
4073 {
4074 if (!i.op[1].regs->reg_type.bitfield.word)
4075 i.tm.opcode_modifier.size = SIZE32;
4076 i.prefix[ADDR_PREFIX] = 0;
4077 }
4078
4079 if (!i.index_reg && !i.base_reg)
4080 {
4081 /* Handle:
4082 lea symbol, %rN -> mov $symbol, %rN
4083 */
4084 if (flag_code == CODE_64BIT)
4085 {
4086 /* Don't transform a relocation to a 16-bit one. */
4087 if (i.op[0].disps
4088 && i.op[0].disps->X_op != O_constant
4089 && i.op[1].regs->reg_type.bitfield.word)
4090 return;
4091
4092 if (!i.op[1].regs->reg_type.bitfield.qword
4093 || i.tm.opcode_modifier.size == SIZE32)
4094 {
4095 i.tm.base_opcode = 0xb8;
4096 i.tm.opcode_modifier.modrm = 0;
4097 if (!i.op[1].regs->reg_type.bitfield.word)
4098 i.types[0].bitfield.imm32 = 1;
4099 else
4100 {
4101 i.tm.opcode_modifier.size = SIZE16;
4102 i.types[0].bitfield.imm16 = 1;
4103 }
4104 }
4105 else
4106 {
4107 /* Subject to further optimization below. */
4108 i.tm.base_opcode = 0xc7;
4109 i.tm.extension_opcode = 0;
4110 i.types[0].bitfield.imm32s = 1;
4111 i.types[0].bitfield.baseindex = 0;
4112 }
4113 }
4114 /* Outside of 64-bit mode address and operand sizes have to match if
4115 a relocation is involved, as otherwise we wouldn't (currently) or
4116 even couldn't express the relocation correctly. */
4117 else if (i.op[0].disps
4118 && i.op[0].disps->X_op != O_constant
4119 && ((!i.prefix[ADDR_PREFIX])
4120 != (flag_code == CODE_32BIT
4121 ? i.op[1].regs->reg_type.bitfield.dword
4122 : i.op[1].regs->reg_type.bitfield.word)))
4123 return;
7772f168
JB
4124 /* In 16-bit mode converting LEA with 16-bit addressing and a 32-bit
4125 destination is going to grow encoding size. */
4126 else if (flag_code == CODE_16BIT
4127 && (optimize <= 1 || optimize_for_space)
4128 && !i.prefix[ADDR_PREFIX]
4129 && i.op[1].regs->reg_type.bitfield.dword)
4130 return;
fe134c65
JB
4131 else
4132 {
4133 i.tm.base_opcode = 0xb8;
4134 i.tm.opcode_modifier.modrm = 0;
4135 if (i.op[1].regs->reg_type.bitfield.dword)
4136 i.types[0].bitfield.imm32 = 1;
4137 else
4138 i.types[0].bitfield.imm16 = 1;
4139
4140 if (i.op[0].disps
4141 && i.op[0].disps->X_op == O_constant
4142 && i.op[1].regs->reg_type.bitfield.dword
60cfa10c
L
4143 /* NB: Add () to !i.prefix[ADDR_PREFIX] to silence
4144 GCC 5. */
4145 && (!i.prefix[ADDR_PREFIX]) != (flag_code == CODE_32BIT))
fe134c65
JB
4146 i.op[0].disps->X_add_number &= 0xffff;
4147 }
4148
4149 i.tm.operand_types[0] = i.types[0];
4150 i.imm_operands = 1;
4151 if (!i.op[0].imms)
4152 {
4153 i.op[0].imms = &im_expressions[0];
4154 i.op[0].imms->X_op = O_absent;
4155 }
4156 }
4157 else if (i.op[0].disps
4158 && (i.op[0].disps->X_op != O_constant
4159 || i.op[0].disps->X_add_number))
4160 return;
4161 else
4162 {
4163 /* Handle:
4164 lea (%rM), %rN -> mov %rM, %rN
4165 lea (,%rM,1), %rN -> mov %rM, %rN
4166 lea (%rM), %rN -> movzx %rM, %rN
4167 */
4168 const reg_entry *addr_reg;
4169
4170 if (!i.index_reg && i.base_reg->reg_num != RegIP)
4171 addr_reg = i.base_reg;
4172 else if (!i.base_reg
4173 && i.index_reg->reg_num != RegIZ
4174 && !i.log2_scale_factor)
4175 addr_reg = i.index_reg;
4176 else
4177 return;
4178
4179 if (addr_reg->reg_type.bitfield.word
4180 && i.op[1].regs->reg_type.bitfield.dword)
4181 {
4182 if (flag_code != CODE_32BIT)
4183 return;
4184 i.tm.opcode_modifier.opcodespace = SPACE_0F;
4185 i.tm.base_opcode = 0xb7;
4186 }
4187 else
4188 i.tm.base_opcode = 0x8b;
4189
4190 if (addr_reg->reg_type.bitfield.dword
4191 && i.op[1].regs->reg_type.bitfield.qword)
4192 i.tm.opcode_modifier.size = SIZE32;
4193
4194 i.op[0].regs = addr_reg;
4195 i.reg_operands = 2;
4196 }
4197
4198 i.mem_operands = 0;
4199 i.disp_operands = 0;
4200 i.prefix[ADDR_PREFIX] = 0;
4201 i.prefix[SEG_PREFIX] = 0;
4202 i.seg[0] = NULL;
4203 }
4204
b6f8c7c4 4205 if (optimize_for_space
389d00a5 4206 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
b6f8c7c4
L
4207 && i.reg_operands == 1
4208 && i.imm_operands == 1
4209 && !i.types[1].bitfield.byte
4210 && i.op[0].imms->X_op == O_constant
4211 && fits_in_imm7 (i.op[0].imms->X_add_number)
72aea328 4212 && (i.tm.base_opcode == 0xa8
b6f8c7c4
L
4213 || (i.tm.base_opcode == 0xf6
4214 && i.tm.extension_opcode == 0x0)))
4215 {
4216 /* Optimize: -Os:
4217 test $imm7, %r64/%r32/%r16 -> test $imm7, %r8
4218 */
4219 unsigned int base_regnum = i.op[1].regs->reg_num;
4220 if (flag_code == CODE_64BIT || base_regnum < 4)
4221 {
4222 i.types[1].bitfield.byte = 1;
4223 /* Ignore the suffix. */
4224 i.suffix = 0;
7697afb6
JB
4225 /* Convert to byte registers. */
4226 if (i.types[1].bitfield.word)
4227 j = 16;
4228 else if (i.types[1].bitfield.dword)
4229 j = 32;
4230 else
4231 j = 48;
4232 if (!(i.op[1].regs->reg_flags & RegRex) && base_regnum < 4)
4233 j += 8;
4234 i.op[1].regs -= j;
b6f8c7c4
L
4235 }
4236 }
4237 else if (flag_code == CODE_64BIT
389d00a5 4238 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
d3d50934
L
4239 && ((i.types[1].bitfield.qword
4240 && i.reg_operands == 1
b6f8c7c4
L
4241 && i.imm_operands == 1
4242 && i.op[0].imms->X_op == O_constant
507916b8 4243 && ((i.tm.base_opcode == 0xb8
b6f8c7c4
L
4244 && i.tm.extension_opcode == None
4245 && fits_in_unsigned_long (i.op[0].imms->X_add_number))
4246 || (fits_in_imm31 (i.op[0].imms->X_add_number)
72aea328
JB
4247 && ((i.tm.base_opcode == 0x24
4248 || i.tm.base_opcode == 0xa8)
b6f8c7c4
L
4249 || (i.tm.base_opcode == 0x80
4250 && i.tm.extension_opcode == 0x4)
4251 || ((i.tm.base_opcode == 0xf6
507916b8 4252 || (i.tm.base_opcode | 1) == 0xc7)
b8364fa7
JB
4253 && i.tm.extension_opcode == 0x0)))
4254 || (fits_in_imm7 (i.op[0].imms->X_add_number)
4255 && i.tm.base_opcode == 0x83
4256 && i.tm.extension_opcode == 0x4)))
d3d50934
L
4257 || (i.types[0].bitfield.qword
4258 && ((i.reg_operands == 2
4259 && i.op[0].regs == i.op[1].regs
72aea328
JB
4260 && (i.tm.base_opcode == 0x30
4261 || i.tm.base_opcode == 0x28))
d3d50934
L
4262 || (i.reg_operands == 1
4263 && i.operands == 1
72aea328 4264 && i.tm.base_opcode == 0x30)))))
b6f8c7c4
L
4265 {
4266 /* Optimize: -O:
4267 andq $imm31, %r64 -> andl $imm31, %r32
b8364fa7 4268 andq $imm7, %r64 -> andl $imm7, %r32
b6f8c7c4
L
4269 testq $imm31, %r64 -> testl $imm31, %r32
4270 xorq %r64, %r64 -> xorl %r32, %r32
4271 subq %r64, %r64 -> subl %r32, %r32
4272 movq $imm31, %r64 -> movl $imm31, %r32
4273 movq $imm32, %r64 -> movl $imm32, %r32
4274 */
4275 i.tm.opcode_modifier.norex64 = 1;
507916b8 4276 if (i.tm.base_opcode == 0xb8 || (i.tm.base_opcode | 1) == 0xc7)
b6f8c7c4
L
4277 {
4278 /* Handle
4279 movq $imm31, %r64 -> movl $imm31, %r32
4280 movq $imm32, %r64 -> movl $imm32, %r32
4281 */
4282 i.tm.operand_types[0].bitfield.imm32 = 1;
4283 i.tm.operand_types[0].bitfield.imm32s = 0;
4284 i.tm.operand_types[0].bitfield.imm64 = 0;
4285 i.types[0].bitfield.imm32 = 1;
4286 i.types[0].bitfield.imm32s = 0;
4287 i.types[0].bitfield.imm64 = 0;
4288 i.types[1].bitfield.dword = 1;
4289 i.types[1].bitfield.qword = 0;
507916b8 4290 if ((i.tm.base_opcode | 1) == 0xc7)
b6f8c7c4
L
4291 {
4292 /* Handle
4293 movq $imm31, %r64 -> movl $imm31, %r32
4294 */
507916b8 4295 i.tm.base_opcode = 0xb8;
b6f8c7c4 4296 i.tm.extension_opcode = None;
507916b8 4297 i.tm.opcode_modifier.w = 0;
b6f8c7c4
L
4298 i.tm.opcode_modifier.modrm = 0;
4299 }
4300 }
4301 }
5641ec01
JB
4302 else if (optimize > 1
4303 && !optimize_for_space
389d00a5 4304 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
5641ec01
JB
4305 && i.reg_operands == 2
4306 && i.op[0].regs == i.op[1].regs
4307 && ((i.tm.base_opcode & ~(Opcode_D | 1)) == 0x8
4308 || (i.tm.base_opcode & ~(Opcode_D | 1)) == 0x20)
4309 && (flag_code != CODE_64BIT || !i.types[0].bitfield.dword))
4310 {
4311 /* Optimize: -O2:
4312 andb %rN, %rN -> testb %rN, %rN
4313 andw %rN, %rN -> testw %rN, %rN
4314 andq %rN, %rN -> testq %rN, %rN
4315 orb %rN, %rN -> testb %rN, %rN
4316 orw %rN, %rN -> testw %rN, %rN
4317 orq %rN, %rN -> testq %rN, %rN
4318
4319 and outside of 64-bit mode
4320
4321 andl %rN, %rN -> testl %rN, %rN
4322 orl %rN, %rN -> testl %rN, %rN
4323 */
4324 i.tm.base_opcode = 0x84 | (i.tm.base_opcode & 1);
4325 }
99112332 4326 else if (i.reg_operands == 3
b6f8c7c4
L
4327 && i.op[0].regs == i.op[1].regs
4328 && !i.types[2].bitfield.xmmword
4329 && (i.tm.opcode_modifier.vex
6225c532 4330 || ((!i.mask.reg || i.mask.zeroing)
e771e7c9 4331 && is_evex_encoding (&i.tm)
80c34c38 4332 && (i.vec_encoding != vex_encoding_evex
dd22218c 4333 || cpu_arch_isa_flags.bitfield.cpuavx512vl
80c34c38 4334 || i.tm.cpu_flags.bitfield.cpuavx512vl
7091c612 4335 || (i.tm.operand_types[2].bitfield.zmmword
dd22218c 4336 && i.types[2].bitfield.ymmword))))
5844ccaa
JB
4337 && i.tm.opcode_modifier.opcodespace == SPACE_0F
4338 && ((i.tm.base_opcode | 2) == 0x57
4339 || i.tm.base_opcode == 0xdf
4340 || i.tm.base_opcode == 0xef
4341 || (i.tm.base_opcode | 3) == 0xfb
4342 || i.tm.base_opcode == 0x42
4343 || i.tm.base_opcode == 0x47))
b6f8c7c4 4344 {
99112332 4345 /* Optimize: -O1:
8305403a
L
4346 VOP, one of vandnps, vandnpd, vxorps, vxorpd, vpsubb, vpsubd,
4347 vpsubq and vpsubw:
b6f8c7c4
L
4348 EVEX VOP %zmmM, %zmmM, %zmmN
4349 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4350 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4351 EVEX VOP %ymmM, %ymmM, %ymmN
4352 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4353 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4354 VEX VOP %ymmM, %ymmM, %ymmN
4355 -> VEX VOP %xmmM, %xmmM, %xmmN
4356 VOP, one of vpandn and vpxor:
4357 VEX VOP %ymmM, %ymmM, %ymmN
4358 -> VEX VOP %xmmM, %xmmM, %xmmN
4359 VOP, one of vpandnd and vpandnq:
4360 EVEX VOP %zmmM, %zmmM, %zmmN
4361 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4362 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4363 EVEX VOP %ymmM, %ymmM, %ymmN
4364 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4365 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4366 VOP, one of vpxord and vpxorq:
4367 EVEX VOP %zmmM, %zmmM, %zmmN
4368 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4369 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4370 EVEX VOP %ymmM, %ymmM, %ymmN
4371 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4372 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
1424ad86
JB
4373 VOP, one of kxord and kxorq:
4374 VEX VOP %kM, %kM, %kN
4375 -> VEX kxorw %kM, %kM, %kN
4376 VOP, one of kandnd and kandnq:
4377 VEX VOP %kM, %kM, %kN
4378 -> VEX kandnw %kM, %kM, %kN
b6f8c7c4 4379 */
e771e7c9 4380 if (is_evex_encoding (&i.tm))
b6f8c7c4 4381 {
7b1d7ca1 4382 if (i.vec_encoding != vex_encoding_evex)
b6f8c7c4
L
4383 {
4384 i.tm.opcode_modifier.vex = VEX128;
4385 i.tm.opcode_modifier.vexw = VEXW0;
4386 i.tm.opcode_modifier.evex = 0;
4387 }
7b1d7ca1 4388 else if (optimize > 1)
dd22218c
L
4389 i.tm.opcode_modifier.evex = EVEX128;
4390 else
4391 return;
b6f8c7c4 4392 }
f74a6307 4393 else if (i.tm.operand_types[0].bitfield.class == RegMask)
1424ad86 4394 {
35648716 4395 i.tm.opcode_modifier.opcodeprefix = PREFIX_NONE;
1424ad86
JB
4396 i.tm.opcode_modifier.vexw = VEXW0;
4397 }
b6f8c7c4
L
4398 else
4399 i.tm.opcode_modifier.vex = VEX128;
4400
4401 if (i.tm.opcode_modifier.vex)
4402 for (j = 0; j < 3; j++)
4403 {
4404 i.types[j].bitfield.xmmword = 1;
4405 i.types[j].bitfield.ymmword = 0;
4406 }
4407 }
392a5972 4408 else if (i.vec_encoding != vex_encoding_evex
97ed31ae 4409 && !i.types[0].bitfield.zmmword
392a5972 4410 && !i.types[1].bitfield.zmmword
6225c532 4411 && !i.mask.reg
a5748e0d 4412 && !i.broadcast.bytes
97ed31ae 4413 && is_evex_encoding (&i.tm)
35648716
JB
4414 && ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4415 || (i.tm.base_opcode & ~4) == 0xdb
4416 || (i.tm.base_opcode & ~4) == 0xeb)
97ed31ae
L
4417 && i.tm.extension_opcode == None)
4418 {
4419 /* Optimize: -O1:
4420 VOP, one of vmovdqa32, vmovdqa64, vmovdqu8, vmovdqu16,
4421 vmovdqu32 and vmovdqu64:
4422 EVEX VOP %xmmM, %xmmN
4423 -> VEX vmovdqa|vmovdqu %xmmM, %xmmN (M and N < 16)
4424 EVEX VOP %ymmM, %ymmN
4425 -> VEX vmovdqa|vmovdqu %ymmM, %ymmN (M and N < 16)
4426 EVEX VOP %xmmM, mem
4427 -> VEX vmovdqa|vmovdqu %xmmM, mem (M < 16)
4428 EVEX VOP %ymmM, mem
4429 -> VEX vmovdqa|vmovdqu %ymmM, mem (M < 16)
4430 EVEX VOP mem, %xmmN
4431 -> VEX mvmovdqa|vmovdquem, %xmmN (N < 16)
4432 EVEX VOP mem, %ymmN
4433 -> VEX vmovdqa|vmovdqu mem, %ymmN (N < 16)
a0a1771e
JB
4434 VOP, one of vpand, vpandn, vpor, vpxor:
4435 EVEX VOP{d,q} %xmmL, %xmmM, %xmmN
4436 -> VEX VOP %xmmL, %xmmM, %xmmN (L, M, and N < 16)
4437 EVEX VOP{d,q} %ymmL, %ymmM, %ymmN
4438 -> VEX VOP %ymmL, %ymmM, %ymmN (L, M, and N < 16)
4439 EVEX VOP{d,q} mem, %xmmM, %xmmN
4440 -> VEX VOP mem, %xmmM, %xmmN (M and N < 16)
4441 EVEX VOP{d,q} mem, %ymmM, %ymmN
4442 -> VEX VOP mem, %ymmM, %ymmN (M and N < 16)
97ed31ae 4443 */
a0a1771e 4444 for (j = 0; j < i.operands; j++)
392a5972
L
4445 if (operand_type_check (i.types[j], disp)
4446 && i.op[j].disps->X_op == O_constant)
4447 {
4448 /* Since the VEX prefix has 2 or 3 bytes, the EVEX prefix
4449 has 4 bytes, EVEX Disp8 has 1 byte and VEX Disp32 has 4
4450 bytes, we choose EVEX Disp8 over VEX Disp32. */
4451 int evex_disp8, vex_disp8;
4452 unsigned int memshift = i.memshift;
4453 offsetT n = i.op[j].disps->X_add_number;
4454
4455 evex_disp8 = fits_in_disp8 (n);
4456 i.memshift = 0;
4457 vex_disp8 = fits_in_disp8 (n);
4458 if (evex_disp8 != vex_disp8)
4459 {
4460 i.memshift = memshift;
4461 return;
4462 }
4463
4464 i.types[j].bitfield.disp8 = vex_disp8;
4465 break;
4466 }
35648716
JB
4467 if ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4468 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2)
4469 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
97ed31ae
L
4470 i.tm.opcode_modifier.vex
4471 = i.types[0].bitfield.ymmword ? VEX256 : VEX128;
4472 i.tm.opcode_modifier.vexw = VEXW0;
79dec6b7 4473 /* VPAND, VPOR, and VPXOR are commutative. */
35648716 4474 if (i.reg_operands == 3 && i.tm.base_opcode != 0xdf)
79dec6b7 4475 i.tm.opcode_modifier.commutative = 1;
97ed31ae
L
4476 i.tm.opcode_modifier.evex = 0;
4477 i.tm.opcode_modifier.masking = 0;
a0a1771e 4478 i.tm.opcode_modifier.broadcast = 0;
97ed31ae
L
4479 i.tm.opcode_modifier.disp8memshift = 0;
4480 i.memshift = 0;
a0a1771e
JB
4481 if (j < i.operands)
4482 i.types[j].bitfield.disp8
4483 = fits_in_disp8 (i.op[j].disps->X_add_number);
97ed31ae 4484 }
b6f8c7c4
L
4485}
4486
ae531041
L
4487/* Return non-zero for load instruction. */
4488
4489static int
4490load_insn_p (void)
4491{
4492 unsigned int dest;
4493 int any_vex_p = is_any_vex_encoding (&i.tm);
4494 unsigned int base_opcode = i.tm.base_opcode | 1;
4495
4496 if (!any_vex_p)
4497 {
a09f656b 4498 /* Anysize insns: lea, invlpg, clflush, prefetchnta, prefetcht0,
4499 prefetcht1, prefetcht2, prefetchtw, bndmk, bndcl, bndcu, bndcn,
4500 bndstx, bndldx, prefetchwt1, clflushopt, clwb, cldemote. */
4501 if (i.tm.opcode_modifier.anysize)
ae531041
L
4502 return 0;
4503
389d00a5
JB
4504 /* pop. */
4505 if (strcmp (i.tm.name, "pop") == 0)
4506 return 1;
4507 }
4508
4509 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE)
4510 {
4511 /* popf, popa. */
4512 if (i.tm.base_opcode == 0x9d
a09f656b 4513 || i.tm.base_opcode == 0x61)
ae531041
L
4514 return 1;
4515
4516 /* movs, cmps, lods, scas. */
4517 if ((i.tm.base_opcode | 0xb) == 0xaf)
4518 return 1;
4519
a09f656b 4520 /* outs, xlatb. */
4521 if (base_opcode == 0x6f
4522 || i.tm.base_opcode == 0xd7)
ae531041 4523 return 1;
a09f656b 4524 /* NB: For AMD-specific insns with implicit memory operands,
4525 they're intentionally not covered. */
ae531041
L
4526 }
4527
4528 /* No memory operand. */
4529 if (!i.mem_operands)
4530 return 0;
4531
4532 if (any_vex_p)
4533 {
4534 /* vldmxcsr. */
4535 if (i.tm.base_opcode == 0xae
4536 && i.tm.opcode_modifier.vex
441f6aca 4537 && i.tm.opcode_modifier.opcodespace == SPACE_0F
35648716 4538 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
ae531041
L
4539 && i.tm.extension_opcode == 2)
4540 return 1;
4541 }
389d00a5 4542 else if (i.tm.opcode_modifier.opcodespace == SPACE_BASE)
ae531041
L
4543 {
4544 /* test, not, neg, mul, imul, div, idiv. */
4545 if ((i.tm.base_opcode == 0xf6 || i.tm.base_opcode == 0xf7)
4546 && i.tm.extension_opcode != 1)
4547 return 1;
4548
4549 /* inc, dec. */
4550 if (base_opcode == 0xff && i.tm.extension_opcode <= 1)
4551 return 1;
4552
4553 /* add, or, adc, sbb, and, sub, xor, cmp. */
4554 if (i.tm.base_opcode >= 0x80 && i.tm.base_opcode <= 0x83)
4555 return 1;
4556
ae531041
L
4557 /* rol, ror, rcl, rcr, shl/sal, shr, sar. */
4558 if ((base_opcode == 0xc1
4559 || (i.tm.base_opcode >= 0xd0 && i.tm.base_opcode <= 0xd3))
4560 && i.tm.extension_opcode != 6)
4561 return 1;
4562
ae531041 4563 /* Check for x87 instructions. */
389d00a5 4564 if (base_opcode >= 0xd8 && base_opcode <= 0xdf)
ae531041
L
4565 {
4566 /* Skip fst, fstp, fstenv, fstcw. */
4567 if (i.tm.base_opcode == 0xd9
4568 && (i.tm.extension_opcode == 2
4569 || i.tm.extension_opcode == 3
4570 || i.tm.extension_opcode == 6
4571 || i.tm.extension_opcode == 7))
4572 return 0;
4573
4574 /* Skip fisttp, fist, fistp, fstp. */
4575 if (i.tm.base_opcode == 0xdb
4576 && (i.tm.extension_opcode == 1
4577 || i.tm.extension_opcode == 2
4578 || i.tm.extension_opcode == 3
4579 || i.tm.extension_opcode == 7))
4580 return 0;
4581
4582 /* Skip fisttp, fst, fstp, fsave, fstsw. */
4583 if (i.tm.base_opcode == 0xdd
4584 && (i.tm.extension_opcode == 1
4585 || i.tm.extension_opcode == 2
4586 || i.tm.extension_opcode == 3
4587 || i.tm.extension_opcode == 6
4588 || i.tm.extension_opcode == 7))
4589 return 0;
4590
4591 /* Skip fisttp, fist, fistp, fbstp, fistp. */
4592 if (i.tm.base_opcode == 0xdf
4593 && (i.tm.extension_opcode == 1
4594 || i.tm.extension_opcode == 2
4595 || i.tm.extension_opcode == 3
4596 || i.tm.extension_opcode == 6
4597 || i.tm.extension_opcode == 7))
4598 return 0;
4599
4600 return 1;
4601 }
4602 }
389d00a5
JB
4603 else if (i.tm.opcode_modifier.opcodespace == SPACE_0F)
4604 {
4605 /* bt, bts, btr, btc. */
4606 if (i.tm.base_opcode == 0xba
4607 && (i.tm.extension_opcode >= 4 && i.tm.extension_opcode <= 7))
4608 return 1;
4609
4610 /* cmpxchg8b, cmpxchg16b, xrstors, vmptrld. */
4611 if (i.tm.base_opcode == 0xc7
4612 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
4613 && (i.tm.extension_opcode == 1 || i.tm.extension_opcode == 3
4614 || i.tm.extension_opcode == 6))
4615 return 1;
4616
4617 /* fxrstor, ldmxcsr, xrstor. */
4618 if (i.tm.base_opcode == 0xae
4619 && (i.tm.extension_opcode == 1
4620 || i.tm.extension_opcode == 2
4621 || i.tm.extension_opcode == 5))
4622 return 1;
4623
4624 /* lgdt, lidt, lmsw. */
4625 if (i.tm.base_opcode == 0x01
4626 && (i.tm.extension_opcode == 2
4627 || i.tm.extension_opcode == 3
4628 || i.tm.extension_opcode == 6))
4629 return 1;
4630 }
ae531041
L
4631
4632 dest = i.operands - 1;
4633
4634 /* Check fake imm8 operand and 3 source operands. */
4635 if ((i.tm.opcode_modifier.immext
4636 || i.tm.opcode_modifier.vexsources == VEX3SOURCES)
4637 && i.types[dest].bitfield.imm8)
4638 dest--;
4639
389d00a5
JB
4640 /* add, or, adc, sbb, and, sub, xor, cmp, test, xchg. */
4641 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
ae531041
L
4642 && (base_opcode == 0x1
4643 || base_opcode == 0x9
4644 || base_opcode == 0x11
4645 || base_opcode == 0x19
4646 || base_opcode == 0x21
4647 || base_opcode == 0x29
4648 || base_opcode == 0x31
4649 || base_opcode == 0x39
389d00a5
JB
4650 || (base_opcode | 2) == 0x87))
4651 return 1;
4652
4653 /* xadd. */
4654 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4655 && base_opcode == 0xc1)
ae531041
L
4656 return 1;
4657
4658 /* Check for load instruction. */
4659 return (i.types[dest].bitfield.class != ClassNone
4660 || i.types[dest].bitfield.instance == Accum);
4661}
4662
4663/* Output lfence, 0xfaee8, after instruction. */
4664
4665static void
4666insert_lfence_after (void)
4667{
4668 if (lfence_after_load && load_insn_p ())
4669 {
a09f656b 4670 /* There are also two REP string instructions that require
4671 special treatment. Specifically, the compare string (CMPS)
4672 and scan string (SCAS) instructions set EFLAGS in a manner
4673 that depends on the data being compared/scanned. When used
4674 with a REP prefix, the number of iterations may therefore
4675 vary depending on this data. If the data is a program secret
4676 chosen by the adversary using an LVI method,
4677 then this data-dependent behavior may leak some aspect
4678 of the secret. */
4679 if (((i.tm.base_opcode | 0x1) == 0xa7
4680 || (i.tm.base_opcode | 0x1) == 0xaf)
4681 && i.prefix[REP_PREFIX])
4682 {
4683 as_warn (_("`%s` changes flags which would affect control flow behavior"),
4684 i.tm.name);
4685 }
ae531041
L
4686 char *p = frag_more (3);
4687 *p++ = 0xf;
4688 *p++ = 0xae;
4689 *p = 0xe8;
4690 }
4691}
4692
4693/* Output lfence, 0xfaee8, before instruction. */
4694
4695static void
4696insert_lfence_before (void)
4697{
4698 char *p;
4699
389d00a5 4700 if (i.tm.opcode_modifier.opcodespace != SPACE_BASE)
ae531041
L
4701 return;
4702
4703 if (i.tm.base_opcode == 0xff
4704 && (i.tm.extension_opcode == 2 || i.tm.extension_opcode == 4))
4705 {
4706 /* Insert lfence before indirect branch if needed. */
4707
4708 if (lfence_before_indirect_branch == lfence_branch_none)
4709 return;
4710
4711 if (i.operands != 1)
4712 abort ();
4713
4714 if (i.reg_operands == 1)
4715 {
4716 /* Indirect branch via register. Don't insert lfence with
4717 -mlfence-after-load=yes. */
4718 if (lfence_after_load
4719 || lfence_before_indirect_branch == lfence_branch_memory)
4720 return;
4721 }
4722 else if (i.mem_operands == 1
4723 && lfence_before_indirect_branch != lfence_branch_register)
4724 {
4725 as_warn (_("indirect `%s` with memory operand should be avoided"),
4726 i.tm.name);
4727 return;
4728 }
4729 else
4730 return;
4731
4732 if (last_insn.kind != last_insn_other
4733 && last_insn.seg == now_seg)
4734 {
4735 as_warn_where (last_insn.file, last_insn.line,
4736 _("`%s` skips -mlfence-before-indirect-branch on `%s`"),
4737 last_insn.name, i.tm.name);
4738 return;
4739 }
4740
4741 p = frag_more (3);
4742 *p++ = 0xf;
4743 *p++ = 0xae;
4744 *p = 0xe8;
4745 return;
4746 }
4747
503648e4 4748 /* Output or/not/shl and lfence before near ret. */
ae531041
L
4749 if (lfence_before_ret != lfence_before_ret_none
4750 && (i.tm.base_opcode == 0xc2
503648e4 4751 || i.tm.base_opcode == 0xc3))
ae531041
L
4752 {
4753 if (last_insn.kind != last_insn_other
4754 && last_insn.seg == now_seg)
4755 {
4756 as_warn_where (last_insn.file, last_insn.line,
4757 _("`%s` skips -mlfence-before-ret on `%s`"),
4758 last_insn.name, i.tm.name);
4759 return;
4760 }
a09f656b 4761
a09f656b 4762 /* Near ret ingore operand size override under CPU64. */
503648e4 4763 char prefix = flag_code == CODE_64BIT
4764 ? 0x48
4765 : i.prefix[DATA_PREFIX] ? 0x66 : 0x0;
a09f656b 4766
4767 if (lfence_before_ret == lfence_before_ret_not)
4768 {
4769 /* not: 0xf71424, may add prefix
4770 for operand size override or 64-bit code. */
4771 p = frag_more ((prefix ? 2 : 0) + 6 + 3);
4772 if (prefix)
4773 *p++ = prefix;
ae531041
L
4774 *p++ = 0xf7;
4775 *p++ = 0x14;
4776 *p++ = 0x24;
a09f656b 4777 if (prefix)
4778 *p++ = prefix;
ae531041
L
4779 *p++ = 0xf7;
4780 *p++ = 0x14;
4781 *p++ = 0x24;
4782 }
a09f656b 4783 else
4784 {
4785 p = frag_more ((prefix ? 1 : 0) + 4 + 3);
4786 if (prefix)
4787 *p++ = prefix;
4788 if (lfence_before_ret == lfence_before_ret_or)
4789 {
4790 /* or: 0x830c2400, may add prefix
4791 for operand size override or 64-bit code. */
4792 *p++ = 0x83;
4793 *p++ = 0x0c;
4794 }
4795 else
4796 {
4797 /* shl: 0xc1242400, may add prefix
4798 for operand size override or 64-bit code. */
4799 *p++ = 0xc1;
4800 *p++ = 0x24;
4801 }
4802
4803 *p++ = 0x24;
4804 *p++ = 0x0;
4805 }
4806
ae531041
L
4807 *p++ = 0xf;
4808 *p++ = 0xae;
4809 *p = 0xe8;
4810 }
4811}
4812
252b5132
RH
4813/* This is the guts of the machine-dependent assembler. LINE points to a
4814 machine dependent instruction. This function is supposed to emit
4815 the frags/bytes it assembles to. */
4816
4817void
65da13b5 4818md_assemble (char *line)
252b5132 4819{
40fb9820 4820 unsigned int j;
83b16ac6 4821 char mnemonic[MAX_MNEM_SIZE], mnem_suffix;
d3ce72d0 4822 const insn_template *t;
252b5132 4823
47926f60 4824 /* Initialize globals. */
252b5132 4825 memset (&i, '\0', sizeof (i));
ca5312a2 4826 i.rounding.type = rc_none;
252b5132 4827 for (j = 0; j < MAX_OPERANDS; j++)
1ae12ab7 4828 i.reloc[j] = NO_RELOC;
252b5132
RH
4829 memset (disp_expressions, '\0', sizeof (disp_expressions));
4830 memset (im_expressions, '\0', sizeof (im_expressions));
ce8a8b2f 4831 save_stack_p = save_stack;
252b5132
RH
4832
4833 /* First parse an instruction mnemonic & call i386_operand for the operands.
4834 We assume that the scrubber has arranged it so that line[0] is the valid
47926f60 4835 start of a (possibly prefixed) mnemonic. */
252b5132 4836
29b0f896
AM
4837 line = parse_insn (line, mnemonic);
4838 if (line == NULL)
4839 return;
83b16ac6 4840 mnem_suffix = i.suffix;
252b5132 4841
29b0f896 4842 line = parse_operands (line, mnemonic);
ee86248c 4843 this_operand = -1;
8325cc63
JB
4844 xfree (i.memop1_string);
4845 i.memop1_string = NULL;
29b0f896
AM
4846 if (line == NULL)
4847 return;
252b5132 4848
29b0f896
AM
4849 /* Now we've parsed the mnemonic into a set of templates, and have the
4850 operands at hand. */
4851
b630c145 4852 /* All Intel opcodes have reversed operands except for "bound", "enter",
c0e54661
JB
4853 "invlpg*", "monitor*", "mwait*", "tpause", "umwait", "pvalidate",
4854 "rmpadjust", and "rmpupdate". We also don't reverse intersegment "jmp"
4855 and "call" instructions with 2 immediate operands so that the immediate
4856 segment precedes the offset consistently in Intel and AT&T modes. */
4d456e3d
L
4857 if (intel_syntax
4858 && i.operands > 1
29b0f896 4859 && (strcmp (mnemonic, "bound") != 0)
c0e54661 4860 && (strncmp (mnemonic, "invlpg", 6) != 0)
d34049e8
ML
4861 && !startswith (mnemonic, "monitor")
4862 && !startswith (mnemonic, "mwait")
c0e54661 4863 && (strcmp (mnemonic, "pvalidate") != 0)
d34049e8 4864 && !startswith (mnemonic, "rmp")
b630c145
JB
4865 && (strcmp (mnemonic, "tpause") != 0)
4866 && (strcmp (mnemonic, "umwait") != 0)
40fb9820
L
4867 && !(operand_type_check (i.types[0], imm)
4868 && operand_type_check (i.types[1], imm)))
29b0f896
AM
4869 swap_operands ();
4870
ec56d5c0
JB
4871 /* The order of the immediates should be reversed
4872 for 2 immediates extrq and insertq instructions */
4873 if (i.imm_operands == 2
4874 && (strcmp (mnemonic, "extrq") == 0
4875 || strcmp (mnemonic, "insertq") == 0))
4876 swap_2_operands (0, 1);
4877
29b0f896
AM
4878 if (i.imm_operands)
4879 optimize_imm ();
4880
9386188e
JB
4881 if (i.disp_operands && !want_disp32 (current_templates->start)
4882 && (!current_templates->start->opcode_modifier.jump
4883 || i.jumpabsolute || i.types[0].bitfield.baseindex))
cce08655
JB
4884 {
4885 for (j = 0; j < i.operands; ++j)
4886 {
4887 const expressionS *exp = i.op[j].disps;
4888
4889 if (!operand_type_check (i.types[j], disp))
4890 continue;
4891
4892 if (exp->X_op != O_constant)
4893 continue;
4894
4895 /* Since displacement is signed extended to 64bit, don't allow
a775efc8 4896 disp32 if it is out of range. */
cce08655
JB
4897 if (fits_in_signed_long (exp->X_add_number))
4898 continue;
4899
a775efc8 4900 i.types[j].bitfield.disp32 = 0;
cce08655
JB
4901 if (i.types[j].bitfield.baseindex)
4902 {
f493c217
AM
4903 as_bad (_("0x%" PRIx64 " out of range of signed 32bit displacement"),
4904 (uint64_t) exp->X_add_number);
cce08655
JB
4905 return;
4906 }
4907 }
4908 }
4909
b300c311
L
4910 /* Don't optimize displacement for movabs since it only takes 64bit
4911 displacement. */
4912 if (i.disp_operands
1a42a9fe 4913 && i.disp_encoding <= disp_encoding_8bit
862be3fb
L
4914 && (flag_code != CODE_64BIT
4915 || strcmp (mnemonic, "movabs") != 0))
4916 optimize_disp ();
29b0f896
AM
4917
4918 /* Next, we find a template that matches the given insn,
4919 making sure the overlap of the given operands types is consistent
4920 with the template operand types. */
252b5132 4921
83b16ac6 4922 if (!(t = match_template (mnem_suffix)))
29b0f896 4923 return;
252b5132 4924
7bab8ab5 4925 if (sse_check != check_none
ffb86450
JB
4926 /* The opcode space check isn't strictly needed; it's there only to
4927 bypass the logic below when easily possible. */
4928 && t->opcode_modifier.opcodespace >= SPACE_0F
4929 && t->opcode_modifier.opcodespace <= SPACE_0F3A
4930 && !i.tm.cpu_flags.bitfield.cpusse4a
4931 && !is_any_vex_encoding (t))
daf50ae7 4932 {
ffb86450
JB
4933 bool simd = false;
4934
4935 for (j = 0; j < t->operands; ++j)
4936 {
4937 if (t->operand_types[j].bitfield.class == RegMMX)
4938 break;
4939 if (t->operand_types[j].bitfield.class == RegSIMD)
4940 simd = true;
4941 }
4942
4943 if (j >= t->operands && simd)
4944 (sse_check == check_warning
4945 ? as_warn
4946 : as_bad) (_("SSE instruction `%s' is used"), i.tm.name);
daf50ae7
L
4947 }
4948
40fb9820 4949 if (i.tm.opcode_modifier.fwait)
29b0f896
AM
4950 if (!add_prefix (FWAIT_OPCODE))
4951 return;
252b5132 4952
d5de92cf 4953 /* Check if REP prefix is OK. */
742732c7 4954 if (i.rep_prefix && i.tm.opcode_modifier.prefixok != PrefixRep)
d5de92cf
L
4955 {
4956 as_bad (_("invalid instruction `%s' after `%s'"),
4957 i.tm.name, i.rep_prefix);
4958 return;
4959 }
4960
c1ba0266
L
4961 /* Check for lock without a lockable instruction. Destination operand
4962 must be memory unless it is xchg (0x86). */
c32fa91d 4963 if (i.prefix[LOCK_PREFIX]
742732c7 4964 && (i.tm.opcode_modifier.prefixok < PrefixLock
c1ba0266
L
4965 || i.mem_operands == 0
4966 || (i.tm.base_opcode != 0x86
8dc0818e 4967 && !(i.flags[i.operands - 1] & Operand_Mem))))
c32fa91d
L
4968 {
4969 as_bad (_("expecting lockable instruction after `lock'"));
4970 return;
4971 }
4972
40d231b4
JB
4973 /* Check for data size prefix on VEX/XOP/EVEX encoded and SIMD insns. */
4974 if (i.prefix[DATA_PREFIX]
4975 && (is_any_vex_encoding (&i.tm)
4976 || i.tm.operand_types[i.imm_operands].bitfield.class >= RegMMX
4977 || i.tm.operand_types[i.imm_operands + 1].bitfield.class >= RegMMX))
7a8655d2
JB
4978 {
4979 as_bad (_("data size prefix invalid with `%s'"), i.tm.name);
4980 return;
4981 }
4982
42164a71 4983 /* Check if HLE prefix is OK. */
165de32a 4984 if (i.hle_prefix && !check_hle ())
42164a71
L
4985 return;
4986
7e8b059b
L
4987 /* Check BND prefix. */
4988 if (i.bnd_prefix && !i.tm.opcode_modifier.bndprefixok)
4989 as_bad (_("expecting valid branch instruction after `bnd'"));
4990
04ef582a 4991 /* Check NOTRACK prefix. */
742732c7 4992 if (i.notrack_prefix && i.tm.opcode_modifier.prefixok != PrefixNoTrack)
9fef80d6 4993 as_bad (_("expecting indirect branch instruction after `notrack'"));
04ef582a 4994
327e8c42
JB
4995 if (i.tm.cpu_flags.bitfield.cpumpx)
4996 {
4997 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
4998 as_bad (_("32-bit address isn't allowed in 64-bit MPX instructions."));
4999 else if (flag_code != CODE_16BIT
5000 ? i.prefix[ADDR_PREFIX]
5001 : i.mem_operands && !i.prefix[ADDR_PREFIX])
5002 as_bad (_("16-bit address isn't allowed in MPX instructions"));
5003 }
7e8b059b
L
5004
5005 /* Insert BND prefix. */
76d3a78a
JB
5006 if (add_bnd_prefix && i.tm.opcode_modifier.bndprefixok)
5007 {
5008 if (!i.prefix[BND_PREFIX])
5009 add_prefix (BND_PREFIX_OPCODE);
5010 else if (i.prefix[BND_PREFIX] != BND_PREFIX_OPCODE)
5011 {
5012 as_warn (_("replacing `rep'/`repe' prefix by `bnd'"));
5013 i.prefix[BND_PREFIX] = BND_PREFIX_OPCODE;
5014 }
5015 }
7e8b059b 5016
29b0f896 5017 /* Check string instruction segment overrides. */
51c8edf6 5018 if (i.tm.opcode_modifier.isstring >= IS_STRING_ES_OP0)
29b0f896 5019 {
51c8edf6 5020 gas_assert (i.mem_operands);
29b0f896 5021 if (!check_string ())
5dd0794d 5022 return;
fc0763e6 5023 i.disp_operands = 0;
29b0f896 5024 }
5dd0794d 5025
b6f8c7c4
L
5026 if (optimize && !i.no_optimize && i.tm.opcode_modifier.optimize)
5027 optimize_encoding ();
5028
c8480b58
L
5029 if (use_unaligned_vector_move)
5030 encode_with_unaligned_vector_move ();
5031
29b0f896
AM
5032 if (!process_suffix ())
5033 return;
e413e4e9 5034
921eafea 5035 /* Update operand types and check extended states. */
bc0844ae 5036 for (j = 0; j < i.operands; j++)
921eafea
L
5037 {
5038 i.types[j] = operand_type_and (i.types[j], i.tm.operand_types[j]);
3d70986f 5039 switch (i.tm.operand_types[j].bitfield.class)
921eafea
L
5040 {
5041 default:
5042 break;
5043 case RegMMX:
5044 i.xstate |= xstate_mmx;
5045 break;
5046 case RegMask:
32930e4e 5047 i.xstate |= xstate_mask;
921eafea
L
5048 break;
5049 case RegSIMD:
3d70986f 5050 if (i.tm.operand_types[j].bitfield.tmmword)
921eafea 5051 i.xstate |= xstate_tmm;
3d70986f 5052 else if (i.tm.operand_types[j].bitfield.zmmword)
921eafea 5053 i.xstate |= xstate_zmm;
3d70986f 5054 else if (i.tm.operand_types[j].bitfield.ymmword)
921eafea 5055 i.xstate |= xstate_ymm;
3d70986f 5056 else if (i.tm.operand_types[j].bitfield.xmmword)
921eafea
L
5057 i.xstate |= xstate_xmm;
5058 break;
5059 }
5060 }
bc0844ae 5061
29b0f896
AM
5062 /* Make still unresolved immediate matches conform to size of immediate
5063 given in i.suffix. */
5064 if (!finalize_imm ())
5065 return;
252b5132 5066
40fb9820 5067 if (i.types[0].bitfield.imm1)
29b0f896 5068 i.imm_operands = 0; /* kludge for shift insns. */
252b5132 5069
9afe6eb8
L
5070 /* We only need to check those implicit registers for instructions
5071 with 3 operands or less. */
5072 if (i.operands <= 3)
5073 for (j = 0; j < i.operands; j++)
75e5731b
JB
5074 if (i.types[j].bitfield.instance != InstanceNone
5075 && !i.types[j].bitfield.xmmword)
9afe6eb8 5076 i.reg_operands--;
40fb9820 5077
29b0f896
AM
5078 /* For insns with operands there are more diddles to do to the opcode. */
5079 if (i.operands)
5080 {
5081 if (!process_operands ())
5082 return;
5083 }
8c190ce0 5084 else if (!quiet_warnings && i.tm.opcode_modifier.ugh)
29b0f896
AM
5085 {
5086 /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc. */
5087 as_warn (_("translating to `%sp'"), i.tm.name);
5088 }
252b5132 5089
7a8655d2 5090 if (is_any_vex_encoding (&i.tm))
9e5e5283 5091 {
c1dc7af5 5092 if (!cpu_arch_flags.bitfield.cpui286)
9e5e5283 5093 {
c1dc7af5 5094 as_bad (_("instruction `%s' isn't supported outside of protected mode."),
9e5e5283
L
5095 i.tm.name);
5096 return;
5097 }
c0f3af97 5098
0b9404fd
JB
5099 /* Check for explicit REX prefix. */
5100 if (i.prefix[REX_PREFIX] || i.rex_encoding)
5101 {
5102 as_bad (_("REX prefix invalid with `%s'"), i.tm.name);
5103 return;
5104 }
5105
9e5e5283
L
5106 if (i.tm.opcode_modifier.vex)
5107 build_vex_prefix (t);
5108 else
5109 build_evex_prefix ();
0b9404fd
JB
5110
5111 /* The individual REX.RXBW bits got consumed. */
5112 i.rex &= REX_OPCODE;
9e5e5283 5113 }
43234a1e 5114
5dd85c99
SP
5115 /* Handle conversion of 'int $3' --> special int3 insn. XOP or FMA4
5116 instructions may define INT_OPCODE as well, so avoid this corner
5117 case for those instructions that use MODRM. */
389d00a5
JB
5118 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
5119 && i.tm.base_opcode == INT_OPCODE
a6461c02
SP
5120 && !i.tm.opcode_modifier.modrm
5121 && i.op[0].imms->X_add_number == 3)
29b0f896
AM
5122 {
5123 i.tm.base_opcode = INT3_OPCODE;
5124 i.imm_operands = 0;
5125 }
252b5132 5126
0cfa3eb3
JB
5127 if ((i.tm.opcode_modifier.jump == JUMP
5128 || i.tm.opcode_modifier.jump == JUMP_BYTE
5129 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896
AM
5130 && i.op[0].disps->X_op == O_constant)
5131 {
5132 /* Convert "jmp constant" (and "call constant") to a jump (call) to
5133 the absolute address given by the constant. Since ix86 jumps and
5134 calls are pc relative, we need to generate a reloc. */
5135 i.op[0].disps->X_add_symbol = &abs_symbol;
5136 i.op[0].disps->X_op = O_symbol;
5137 }
252b5132 5138
29b0f896
AM
5139 /* For 8 bit registers we need an empty rex prefix. Also if the
5140 instruction already has a prefix, we need to convert old
5141 registers to new ones. */
773f551c 5142
bab6aec1 5143 if ((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte
29b0f896 5144 && (i.op[0].regs->reg_flags & RegRex64) != 0)
bab6aec1 5145 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte
29b0f896 5146 && (i.op[1].regs->reg_flags & RegRex64) != 0)
bab6aec1
JB
5147 || (((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte)
5148 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte))
29b0f896
AM
5149 && i.rex != 0))
5150 {
5151 int x;
726c5dcd 5152
29b0f896
AM
5153 i.rex |= REX_OPCODE;
5154 for (x = 0; x < 2; x++)
5155 {
5156 /* Look for 8 bit operand that uses old registers. */
bab6aec1 5157 if (i.types[x].bitfield.class == Reg && i.types[x].bitfield.byte
29b0f896 5158 && (i.op[x].regs->reg_flags & RegRex64) == 0)
773f551c 5159 {
3f93af61 5160 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
29b0f896
AM
5161 /* In case it is "hi" register, give up. */
5162 if (i.op[x].regs->reg_num > 3)
a540244d 5163 as_bad (_("can't encode register '%s%s' in an "
4eed87de 5164 "instruction requiring REX prefix."),
a540244d 5165 register_prefix, i.op[x].regs->reg_name);
773f551c 5166
29b0f896
AM
5167 /* Otherwise it is equivalent to the extended register.
5168 Since the encoding doesn't change this is merely
5169 cosmetic cleanup for debug output. */
5170
5171 i.op[x].regs = i.op[x].regs + 8;
773f551c 5172 }
29b0f896
AM
5173 }
5174 }
773f551c 5175
6b6b6807
L
5176 if (i.rex == 0 && i.rex_encoding)
5177 {
5178 /* Check if we can add a REX_OPCODE byte. Look for 8 bit operand
3f93af61 5179 that uses legacy register. If it is "hi" register, don't add
6b6b6807
L
5180 the REX_OPCODE byte. */
5181 int x;
5182 for (x = 0; x < 2; x++)
bab6aec1 5183 if (i.types[x].bitfield.class == Reg
6b6b6807
L
5184 && i.types[x].bitfield.byte
5185 && (i.op[x].regs->reg_flags & RegRex64) == 0
5186 && i.op[x].regs->reg_num > 3)
5187 {
3f93af61 5188 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
5b7c81bd 5189 i.rex_encoding = false;
6b6b6807
L
5190 break;
5191 }
5192
5193 if (i.rex_encoding)
5194 i.rex = REX_OPCODE;
5195 }
5196
7ab9ffdd 5197 if (i.rex != 0)
29b0f896
AM
5198 add_prefix (REX_OPCODE | i.rex);
5199
ae531041
L
5200 insert_lfence_before ();
5201
29b0f896
AM
5202 /* We are ready to output the insn. */
5203 output_insn ();
e379e5f3 5204
ae531041
L
5205 insert_lfence_after ();
5206
e379e5f3
L
5207 last_insn.seg = now_seg;
5208
5209 if (i.tm.opcode_modifier.isprefix)
5210 {
5211 last_insn.kind = last_insn_prefix;
5212 last_insn.name = i.tm.name;
5213 last_insn.file = as_where (&last_insn.line);
5214 }
5215 else
5216 last_insn.kind = last_insn_other;
29b0f896
AM
5217}
5218
5219static char *
e3bb37b5 5220parse_insn (char *line, char *mnemonic)
29b0f896
AM
5221{
5222 char *l = line;
5223 char *token_start = l;
5224 char *mnem_p;
5c6af06e 5225 int supported;
d3ce72d0 5226 const insn_template *t;
b6169b20 5227 char *dot_p = NULL;
29b0f896 5228
29b0f896
AM
5229 while (1)
5230 {
5231 mnem_p = mnemonic;
5232 while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
5233 {
b6169b20
L
5234 if (*mnem_p == '.')
5235 dot_p = mnem_p;
29b0f896
AM
5236 mnem_p++;
5237 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
45288df1 5238 {
29b0f896
AM
5239 as_bad (_("no such instruction: `%s'"), token_start);
5240 return NULL;
5241 }
5242 l++;
5243 }
5244 if (!is_space_char (*l)
5245 && *l != END_OF_INSN
e44823cf
JB
5246 && (intel_syntax
5247 || (*l != PREFIX_SEPARATOR
5248 && *l != ',')))
29b0f896
AM
5249 {
5250 as_bad (_("invalid character %s in mnemonic"),
5251 output_invalid (*l));
5252 return NULL;
5253 }
5254 if (token_start == l)
5255 {
e44823cf 5256 if (!intel_syntax && *l == PREFIX_SEPARATOR)
29b0f896
AM
5257 as_bad (_("expecting prefix; got nothing"));
5258 else
5259 as_bad (_("expecting mnemonic; got nothing"));
5260 return NULL;
5261 }
45288df1 5262
29b0f896 5263 /* Look up instruction (or prefix) via hash table. */
629310ab 5264 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
47926f60 5265
29b0f896
AM
5266 if (*l != END_OF_INSN
5267 && (!is_space_char (*l) || l[1] != END_OF_INSN)
5268 && current_templates
40fb9820 5269 && current_templates->start->opcode_modifier.isprefix)
29b0f896 5270 {
c6fb90c8 5271 if (!cpu_flags_check_cpu64 (current_templates->start->cpu_flags))
2dd88dca
JB
5272 {
5273 as_bad ((flag_code != CODE_64BIT
5274 ? _("`%s' is only supported in 64-bit mode")
5275 : _("`%s' is not supported in 64-bit mode")),
5276 current_templates->start->name);
5277 return NULL;
5278 }
29b0f896
AM
5279 /* If we are in 16-bit mode, do not allow addr16 or data16.
5280 Similarly, in 32-bit mode, do not allow addr32 or data32. */
673fe0f0
JB
5281 if ((current_templates->start->opcode_modifier.size == SIZE16
5282 || current_templates->start->opcode_modifier.size == SIZE32)
29b0f896 5283 && flag_code != CODE_64BIT
673fe0f0 5284 && ((current_templates->start->opcode_modifier.size == SIZE32)
29b0f896
AM
5285 ^ (flag_code == CODE_16BIT)))
5286 {
5287 as_bad (_("redundant %s prefix"),
5288 current_templates->start->name);
5289 return NULL;
45288df1 5290 }
31184569
JB
5291
5292 if (current_templates->start->base_opcode == PSEUDO_PREFIX)
29b0f896 5293 {
86fa6981 5294 /* Handle pseudo prefixes. */
31184569 5295 switch (current_templates->start->extension_opcode)
86fa6981 5296 {
41eb8e88 5297 case Prefix_Disp8:
86fa6981
L
5298 /* {disp8} */
5299 i.disp_encoding = disp_encoding_8bit;
5300 break;
41eb8e88
L
5301 case Prefix_Disp16:
5302 /* {disp16} */
5303 i.disp_encoding = disp_encoding_16bit;
5304 break;
5305 case Prefix_Disp32:
86fa6981
L
5306 /* {disp32} */
5307 i.disp_encoding = disp_encoding_32bit;
5308 break;
41eb8e88 5309 case Prefix_Load:
86fa6981
L
5310 /* {load} */
5311 i.dir_encoding = dir_encoding_load;
5312 break;
41eb8e88 5313 case Prefix_Store:
86fa6981
L
5314 /* {store} */
5315 i.dir_encoding = dir_encoding_store;
5316 break;
41eb8e88 5317 case Prefix_VEX:
42e04b36
L
5318 /* {vex} */
5319 i.vec_encoding = vex_encoding_vex;
86fa6981 5320 break;
41eb8e88 5321 case Prefix_VEX3:
86fa6981
L
5322 /* {vex3} */
5323 i.vec_encoding = vex_encoding_vex3;
5324 break;
41eb8e88 5325 case Prefix_EVEX:
86fa6981
L
5326 /* {evex} */
5327 i.vec_encoding = vex_encoding_evex;
5328 break;
41eb8e88 5329 case Prefix_REX:
6b6b6807 5330 /* {rex} */
5b7c81bd 5331 i.rex_encoding = true;
6b6b6807 5332 break;
41eb8e88 5333 case Prefix_NoOptimize:
b6f8c7c4 5334 /* {nooptimize} */
5b7c81bd 5335 i.no_optimize = true;
b6f8c7c4 5336 break;
86fa6981
L
5337 default:
5338 abort ();
5339 }
5340 }
5341 else
5342 {
5343 /* Add prefix, checking for repeated prefixes. */
4e9ac44a 5344 switch (add_prefix (current_templates->start->base_opcode))
86fa6981 5345 {
4e9ac44a
L
5346 case PREFIX_EXIST:
5347 return NULL;
5348 case PREFIX_DS:
d777820b 5349 if (current_templates->start->cpu_flags.bitfield.cpuibt)
4e9ac44a
L
5350 i.notrack_prefix = current_templates->start->name;
5351 break;
5352 case PREFIX_REP:
5353 if (current_templates->start->cpu_flags.bitfield.cpuhle)
5354 i.hle_prefix = current_templates->start->name;
5355 else if (current_templates->start->cpu_flags.bitfield.cpumpx)
5356 i.bnd_prefix = current_templates->start->name;
5357 else
5358 i.rep_prefix = current_templates->start->name;
5359 break;
5360 default:
5361 break;
86fa6981 5362 }
29b0f896
AM
5363 }
5364 /* Skip past PREFIX_SEPARATOR and reset token_start. */
5365 token_start = ++l;
5366 }
5367 else
5368 break;
5369 }
45288df1 5370
30a55f88 5371 if (!current_templates)
b6169b20 5372 {
07d5e953
JB
5373 /* Deprecated functionality (new code should use pseudo-prefixes instead):
5374 Check if we should swap operand or force 32bit displacement in
f8a5c266 5375 encoding. */
30a55f88 5376 if (mnem_p - 2 == dot_p && dot_p[1] == 's')
64c49ab3 5377 i.dir_encoding = dir_encoding_swap;
8d63c93e 5378 else if (mnem_p - 3 == dot_p
a501d77e
L
5379 && dot_p[1] == 'd'
5380 && dot_p[2] == '8')
5381 i.disp_encoding = disp_encoding_8bit;
8d63c93e 5382 else if (mnem_p - 4 == dot_p
f8a5c266
L
5383 && dot_p[1] == 'd'
5384 && dot_p[2] == '3'
5385 && dot_p[3] == '2')
a501d77e 5386 i.disp_encoding = disp_encoding_32bit;
30a55f88
L
5387 else
5388 goto check_suffix;
5389 mnem_p = dot_p;
5390 *dot_p = '\0';
629310ab 5391 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
b6169b20
L
5392 }
5393
29b0f896
AM
5394 if (!current_templates)
5395 {
dc1e8a47 5396 check_suffix:
1c529385 5397 if (mnem_p > mnemonic)
29b0f896 5398 {
1c529385
LH
5399 /* See if we can get a match by trimming off a suffix. */
5400 switch (mnem_p[-1])
29b0f896 5401 {
1c529385
LH
5402 case WORD_MNEM_SUFFIX:
5403 if (intel_syntax && (intel_float_operand (mnemonic) & 2))
29b0f896
AM
5404 i.suffix = SHORT_MNEM_SUFFIX;
5405 else
1c529385
LH
5406 /* Fall through. */
5407 case BYTE_MNEM_SUFFIX:
5408 case QWORD_MNEM_SUFFIX:
5409 i.suffix = mnem_p[-1];
29b0f896 5410 mnem_p[-1] = '\0';
fe0e921f
AM
5411 current_templates
5412 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5413 break;
5414 case SHORT_MNEM_SUFFIX:
5415 case LONG_MNEM_SUFFIX:
5416 if (!intel_syntax)
5417 {
5418 i.suffix = mnem_p[-1];
5419 mnem_p[-1] = '\0';
fe0e921f
AM
5420 current_templates
5421 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5422 }
5423 break;
5424
5425 /* Intel Syntax. */
5426 case 'd':
5427 if (intel_syntax)
5428 {
5429 if (intel_float_operand (mnemonic) == 1)
5430 i.suffix = SHORT_MNEM_SUFFIX;
5431 else
5432 i.suffix = LONG_MNEM_SUFFIX;
5433 mnem_p[-1] = '\0';
fe0e921f
AM
5434 current_templates
5435 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5436 }
5437 break;
29b0f896 5438 }
29b0f896 5439 }
1c529385 5440
29b0f896
AM
5441 if (!current_templates)
5442 {
5443 as_bad (_("no such instruction: `%s'"), token_start);
5444 return NULL;
5445 }
5446 }
252b5132 5447
0cfa3eb3
JB
5448 if (current_templates->start->opcode_modifier.jump == JUMP
5449 || current_templates->start->opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
5450 {
5451 /* Check for a branch hint. We allow ",pt" and ",pn" for
5452 predict taken and predict not taken respectively.
5453 I'm not sure that branch hints actually do anything on loop
5454 and jcxz insns (JumpByte) for current Pentium4 chips. They
5455 may work in the future and it doesn't hurt to accept them
5456 now. */
5457 if (l[0] == ',' && l[1] == 'p')
5458 {
5459 if (l[2] == 't')
5460 {
5461 if (!add_prefix (DS_PREFIX_OPCODE))
5462 return NULL;
5463 l += 3;
5464 }
5465 else if (l[2] == 'n')
5466 {
5467 if (!add_prefix (CS_PREFIX_OPCODE))
5468 return NULL;
5469 l += 3;
5470 }
5471 }
5472 }
5473 /* Any other comma loses. */
5474 if (*l == ',')
5475 {
5476 as_bad (_("invalid character %s in mnemonic"),
5477 output_invalid (*l));
5478 return NULL;
5479 }
252b5132 5480
29b0f896 5481 /* Check if instruction is supported on specified architecture. */
5c6af06e
JB
5482 supported = 0;
5483 for (t = current_templates->start; t < current_templates->end; ++t)
5484 {
c0f3af97
L
5485 supported |= cpu_flags_match (t);
5486 if (supported == CPU_FLAGS_PERFECT_MATCH)
d59a54c2 5487 return l;
29b0f896 5488 }
3629bb00 5489
548d0ee6
JB
5490 if (!(supported & CPU_FLAGS_64BIT_MATCH))
5491 as_bad (flag_code == CODE_64BIT
5492 ? _("`%s' is not supported in 64-bit mode")
5493 : _("`%s' is only supported in 64-bit mode"),
5494 current_templates->start->name);
5495 else
5496 as_bad (_("`%s' is not supported on `%s%s'"),
5497 current_templates->start->name,
5498 cpu_arch_name ? cpu_arch_name : default_arch,
5499 cpu_sub_arch_name ? cpu_sub_arch_name : "");
252b5132 5500
548d0ee6 5501 return NULL;
29b0f896 5502}
252b5132 5503
29b0f896 5504static char *
e3bb37b5 5505parse_operands (char *l, const char *mnemonic)
29b0f896
AM
5506{
5507 char *token_start;
3138f287 5508
29b0f896
AM
5509 /* 1 if operand is pending after ','. */
5510 unsigned int expecting_operand = 0;
252b5132 5511
29b0f896
AM
5512 while (*l != END_OF_INSN)
5513 {
e68c3d59
JB
5514 /* Non-zero if operand parens not balanced. */
5515 unsigned int paren_not_balanced = 0;
5516 /* True if inside double quotes. */
5517 bool in_quotes = false;
5518
29b0f896
AM
5519 /* Skip optional white space before operand. */
5520 if (is_space_char (*l))
5521 ++l;
d02603dc 5522 if (!is_operand_char (*l) && *l != END_OF_INSN && *l != '"')
29b0f896
AM
5523 {
5524 as_bad (_("invalid character %s before operand %d"),
5525 output_invalid (*l),
5526 i.operands + 1);
5527 return NULL;
5528 }
d02603dc 5529 token_start = l; /* After white space. */
e68c3d59 5530 while (in_quotes || paren_not_balanced || *l != ',')
29b0f896
AM
5531 {
5532 if (*l == END_OF_INSN)
5533 {
e68c3d59
JB
5534 if (in_quotes)
5535 {
5536 as_bad (_("unbalanced double quotes in operand %d."),
5537 i.operands + 1);
5538 return NULL;
5539 }
29b0f896
AM
5540 if (paren_not_balanced)
5541 {
98ff9f1c
JB
5542 know (!intel_syntax);
5543 as_bad (_("unbalanced parenthesis in operand %d."),
5544 i.operands + 1);
29b0f896
AM
5545 return NULL;
5546 }
5547 else
5548 break; /* we are done */
5549 }
e68c3d59
JB
5550 else if (*l == '\\' && l[1] == '"')
5551 ++l;
5552 else if (*l == '"')
5553 in_quotes = !in_quotes;
5554 else if (!in_quotes && !is_operand_char (*l) && !is_space_char (*l))
29b0f896
AM
5555 {
5556 as_bad (_("invalid character %s in operand %d"),
5557 output_invalid (*l),
5558 i.operands + 1);
5559 return NULL;
5560 }
e68c3d59 5561 if (!intel_syntax && !in_quotes)
29b0f896
AM
5562 {
5563 if (*l == '(')
5564 ++paren_not_balanced;
5565 if (*l == ')')
5566 --paren_not_balanced;
5567 }
29b0f896
AM
5568 l++;
5569 }
5570 if (l != token_start)
5571 { /* Yes, we've read in another operand. */
5572 unsigned int operand_ok;
5573 this_operand = i.operands++;
5574 if (i.operands > MAX_OPERANDS)
5575 {
5576 as_bad (_("spurious operands; (%d operands/instruction max)"),
5577 MAX_OPERANDS);
5578 return NULL;
5579 }
9d46ce34 5580 i.types[this_operand].bitfield.unspecified = 1;
29b0f896
AM
5581 /* Now parse operand adding info to 'i' as we go along. */
5582 END_STRING_AND_SAVE (l);
5583
1286ab78
L
5584 if (i.mem_operands > 1)
5585 {
5586 as_bad (_("too many memory references for `%s'"),
5587 mnemonic);
5588 return 0;
5589 }
5590
29b0f896
AM
5591 if (intel_syntax)
5592 operand_ok =
5593 i386_intel_operand (token_start,
5594 intel_float_operand (mnemonic));
5595 else
a7619375 5596 operand_ok = i386_att_operand (token_start);
29b0f896
AM
5597
5598 RESTORE_END_STRING (l);
5599 if (!operand_ok)
5600 return NULL;
5601 }
5602 else
5603 {
5604 if (expecting_operand)
5605 {
5606 expecting_operand_after_comma:
5607 as_bad (_("expecting operand after ','; got nothing"));
5608 return NULL;
5609 }
5610 if (*l == ',')
5611 {
5612 as_bad (_("expecting operand before ','; got nothing"));
5613 return NULL;
5614 }
5615 }
7f3f1ea2 5616
29b0f896
AM
5617 /* Now *l must be either ',' or END_OF_INSN. */
5618 if (*l == ',')
5619 {
5620 if (*++l == END_OF_INSN)
5621 {
5622 /* Just skip it, if it's \n complain. */
5623 goto expecting_operand_after_comma;
5624 }
5625 expecting_operand = 1;
5626 }
5627 }
5628 return l;
5629}
7f3f1ea2 5630
050dfa73 5631static void
783c187b 5632swap_2_operands (unsigned int xchg1, unsigned int xchg2)
050dfa73
MM
5633{
5634 union i386_op temp_op;
40fb9820 5635 i386_operand_type temp_type;
c48dadc9 5636 unsigned int temp_flags;
050dfa73 5637 enum bfd_reloc_code_real temp_reloc;
4eed87de 5638
050dfa73
MM
5639 temp_type = i.types[xchg2];
5640 i.types[xchg2] = i.types[xchg1];
5641 i.types[xchg1] = temp_type;
c48dadc9
JB
5642
5643 temp_flags = i.flags[xchg2];
5644 i.flags[xchg2] = i.flags[xchg1];
5645 i.flags[xchg1] = temp_flags;
5646
050dfa73
MM
5647 temp_op = i.op[xchg2];
5648 i.op[xchg2] = i.op[xchg1];
5649 i.op[xchg1] = temp_op;
c48dadc9 5650
050dfa73
MM
5651 temp_reloc = i.reloc[xchg2];
5652 i.reloc[xchg2] = i.reloc[xchg1];
5653 i.reloc[xchg1] = temp_reloc;
43234a1e 5654
6225c532 5655 if (i.mask.reg)
43234a1e 5656 {
6225c532
JB
5657 if (i.mask.operand == xchg1)
5658 i.mask.operand = xchg2;
5659 else if (i.mask.operand == xchg2)
5660 i.mask.operand = xchg1;
43234a1e 5661 }
a5748e0d 5662 if (i.broadcast.type || i.broadcast.bytes)
43234a1e 5663 {
5273a3cd
JB
5664 if (i.broadcast.operand == xchg1)
5665 i.broadcast.operand = xchg2;
5666 else if (i.broadcast.operand == xchg2)
5667 i.broadcast.operand = xchg1;
43234a1e 5668 }
050dfa73
MM
5669}
5670
29b0f896 5671static void
e3bb37b5 5672swap_operands (void)
29b0f896 5673{
b7c61d9a 5674 switch (i.operands)
050dfa73 5675 {
c0f3af97 5676 case 5:
b7c61d9a 5677 case 4:
4d456e3d 5678 swap_2_operands (1, i.operands - 2);
1a0670f3 5679 /* Fall through. */
b7c61d9a
L
5680 case 3:
5681 case 2:
4d456e3d 5682 swap_2_operands (0, i.operands - 1);
b7c61d9a
L
5683 break;
5684 default:
5685 abort ();
29b0f896 5686 }
29b0f896
AM
5687
5688 if (i.mem_operands == 2)
5689 {
5e042380 5690 const reg_entry *temp_seg;
29b0f896
AM
5691 temp_seg = i.seg[0];
5692 i.seg[0] = i.seg[1];
5693 i.seg[1] = temp_seg;
5694 }
5695}
252b5132 5696
29b0f896
AM
5697/* Try to ensure constant immediates are represented in the smallest
5698 opcode possible. */
5699static void
e3bb37b5 5700optimize_imm (void)
29b0f896
AM
5701{
5702 char guess_suffix = 0;
5703 int op;
252b5132 5704
29b0f896
AM
5705 if (i.suffix)
5706 guess_suffix = i.suffix;
5707 else if (i.reg_operands)
5708 {
5709 /* Figure out a suffix from the last register operand specified.
75e5731b
JB
5710 We can't do this properly yet, i.e. excluding special register
5711 instances, but the following works for instructions with
5712 immediates. In any case, we can't set i.suffix yet. */
29b0f896 5713 for (op = i.operands; --op >= 0;)
bab6aec1
JB
5714 if (i.types[op].bitfield.class != Reg)
5715 continue;
5716 else if (i.types[op].bitfield.byte)
7ab9ffdd 5717 {
40fb9820
L
5718 guess_suffix = BYTE_MNEM_SUFFIX;
5719 break;
5720 }
bab6aec1 5721 else if (i.types[op].bitfield.word)
252b5132 5722 {
40fb9820
L
5723 guess_suffix = WORD_MNEM_SUFFIX;
5724 break;
5725 }
bab6aec1 5726 else if (i.types[op].bitfield.dword)
40fb9820
L
5727 {
5728 guess_suffix = LONG_MNEM_SUFFIX;
5729 break;
5730 }
bab6aec1 5731 else if (i.types[op].bitfield.qword)
40fb9820
L
5732 {
5733 guess_suffix = QWORD_MNEM_SUFFIX;
29b0f896 5734 break;
252b5132 5735 }
29b0f896
AM
5736 }
5737 else if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
5738 guess_suffix = WORD_MNEM_SUFFIX;
5739
5740 for (op = i.operands; --op >= 0;)
40fb9820 5741 if (operand_type_check (i.types[op], imm))
29b0f896
AM
5742 {
5743 switch (i.op[op].imms->X_op)
252b5132 5744 {
29b0f896
AM
5745 case O_constant:
5746 /* If a suffix is given, this operand may be shortened. */
5747 switch (guess_suffix)
252b5132 5748 {
29b0f896 5749 case LONG_MNEM_SUFFIX:
40fb9820
L
5750 i.types[op].bitfield.imm32 = 1;
5751 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
5752 break;
5753 case WORD_MNEM_SUFFIX:
40fb9820
L
5754 i.types[op].bitfield.imm16 = 1;
5755 i.types[op].bitfield.imm32 = 1;
5756 i.types[op].bitfield.imm32s = 1;
5757 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
5758 break;
5759 case BYTE_MNEM_SUFFIX:
40fb9820
L
5760 i.types[op].bitfield.imm8 = 1;
5761 i.types[op].bitfield.imm8s = 1;
5762 i.types[op].bitfield.imm16 = 1;
5763 i.types[op].bitfield.imm32 = 1;
5764 i.types[op].bitfield.imm32s = 1;
5765 i.types[op].bitfield.imm64 = 1;
29b0f896 5766 break;
252b5132 5767 }
252b5132 5768
29b0f896
AM
5769 /* If this operand is at most 16 bits, convert it
5770 to a signed 16 bit number before trying to see
5771 whether it will fit in an even smaller size.
5772 This allows a 16-bit operand such as $0xffe0 to
5773 be recognised as within Imm8S range. */
40fb9820 5774 if ((i.types[op].bitfield.imm16)
7e96fb68 5775 && fits_in_unsigned_word (i.op[op].imms->X_add_number))
252b5132 5776 {
87ed972d
JB
5777 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
5778 ^ 0x8000) - 0x8000);
29b0f896 5779 }
a28def75
L
5780#ifdef BFD64
5781 /* Store 32-bit immediate in 64-bit for 64-bit BFD. */
40fb9820 5782 if ((i.types[op].bitfield.imm32)
7e96fb68 5783 && fits_in_unsigned_long (i.op[op].imms->X_add_number))
29b0f896
AM
5784 {
5785 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
5786 ^ ((offsetT) 1 << 31))
5787 - ((offsetT) 1 << 31));
5788 }
a28def75 5789#endif
40fb9820 5790 i.types[op]
c6fb90c8
L
5791 = operand_type_or (i.types[op],
5792 smallest_imm_type (i.op[op].imms->X_add_number));
252b5132 5793
29b0f896
AM
5794 /* We must avoid matching of Imm32 templates when 64bit
5795 only immediate is available. */
5796 if (guess_suffix == QWORD_MNEM_SUFFIX)
40fb9820 5797 i.types[op].bitfield.imm32 = 0;
29b0f896 5798 break;
252b5132 5799
29b0f896
AM
5800 case O_absent:
5801 case O_register:
5802 abort ();
5803
5804 /* Symbols and expressions. */
5805 default:
9cd96992
JB
5806 /* Convert symbolic operand to proper sizes for matching, but don't
5807 prevent matching a set of insns that only supports sizes other
5808 than those matching the insn suffix. */
5809 {
40fb9820 5810 i386_operand_type mask, allowed;
87ed972d 5811 const insn_template *t = current_templates->start;
9cd96992 5812
0dfbf9d7 5813 operand_type_set (&mask, 0);
87ed972d 5814 allowed = t->operand_types[op];
40fb9820 5815
87ed972d 5816 while (++t < current_templates->end)
bab6aec1 5817 {
bab6aec1 5818 allowed = operand_type_and (allowed, anyimm);
87ed972d 5819 allowed = operand_type_or (allowed, t->operand_types[op]);
bab6aec1 5820 }
9cd96992
JB
5821 switch (guess_suffix)
5822 {
5823 case QWORD_MNEM_SUFFIX:
40fb9820
L
5824 mask.bitfield.imm64 = 1;
5825 mask.bitfield.imm32s = 1;
9cd96992
JB
5826 break;
5827 case LONG_MNEM_SUFFIX:
40fb9820 5828 mask.bitfield.imm32 = 1;
9cd96992
JB
5829 break;
5830 case WORD_MNEM_SUFFIX:
40fb9820 5831 mask.bitfield.imm16 = 1;
9cd96992
JB
5832 break;
5833 case BYTE_MNEM_SUFFIX:
40fb9820 5834 mask.bitfield.imm8 = 1;
9cd96992
JB
5835 break;
5836 default:
9cd96992
JB
5837 break;
5838 }
c6fb90c8 5839 allowed = operand_type_and (mask, allowed);
0dfbf9d7 5840 if (!operand_type_all_zero (&allowed))
c6fb90c8 5841 i.types[op] = operand_type_and (i.types[op], mask);
9cd96992 5842 }
29b0f896 5843 break;
252b5132 5844 }
29b0f896
AM
5845 }
5846}
47926f60 5847
29b0f896
AM
5848/* Try to use the smallest displacement type too. */
5849static void
e3bb37b5 5850optimize_disp (void)
29b0f896
AM
5851{
5852 int op;
3e73aa7c 5853
29b0f896 5854 for (op = i.operands; --op >= 0;)
40fb9820 5855 if (operand_type_check (i.types[op], disp))
252b5132 5856 {
b300c311 5857 if (i.op[op].disps->X_op == O_constant)
252b5132 5858 {
91d6fa6a 5859 offsetT op_disp = i.op[op].disps->X_add_number;
29b0f896 5860
91d6fa6a 5861 if (!op_disp && i.types[op].bitfield.baseindex)
b300c311 5862 {
2f2be86b
JB
5863 i.types[op] = operand_type_and_not (i.types[op], anydisp);
5864 i.op[op].disps = NULL;
b300c311 5865 i.disp_operands--;
f185acdd
JB
5866 continue;
5867 }
5868
5869 if (i.types[op].bitfield.disp16
cd613c1f 5870 && fits_in_unsigned_word (op_disp))
f185acdd
JB
5871 {
5872 /* If this operand is at most 16 bits, convert
5873 to a signed 16 bit number and don't use 64bit
5874 displacement. */
5875 op_disp = ((op_disp ^ 0x8000) - 0x8000);
5876 i.types[op].bitfield.disp64 = 0;
b300c311 5877 }
f185acdd 5878
28a167a4 5879#ifdef BFD64
a50187b2 5880 /* Optimize 64-bit displacement to 32-bit for 64-bit BFD. */
a775efc8
JB
5881 if ((flag_code != CODE_64BIT
5882 ? i.types[op].bitfield.disp32
5883 : want_disp32 (current_templates->start)
5884 && (!current_templates->start->opcode_modifier.jump
5885 || i.jumpabsolute || i.types[op].bitfield.baseindex))
a50187b2 5886 && fits_in_unsigned_long (op_disp))
b300c311 5887 {
a50187b2
JB
5888 /* If this operand is at most 32 bits, convert
5889 to a signed 32 bit number and don't use 64bit
5890 displacement. */
5891 op_disp = (op_disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
5892 i.types[op].bitfield.disp64 = 0;
5893 i.types[op].bitfield.disp32 = 1;
5894 }
28a167a4 5895
a50187b2
JB
5896 if (flag_code == CODE_64BIT && fits_in_signed_long (op_disp))
5897 {
5898 i.types[op].bitfield.disp64 = 0;
a775efc8 5899 i.types[op].bitfield.disp32 = 1;
b300c311 5900 }
28a167a4 5901#endif
40fb9820 5902 if ((i.types[op].bitfield.disp32
40fb9820 5903 || i.types[op].bitfield.disp16)
b5014f7a 5904 && fits_in_disp8 (op_disp))
40fb9820 5905 i.types[op].bitfield.disp8 = 1;
77c59789
JB
5906
5907 i.op[op].disps->X_add_number = op_disp;
252b5132 5908 }
67a4f2b7
AO
5909 else if (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
5910 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL)
5911 {
5912 fix_new_exp (frag_now, frag_more (0) - frag_now->fr_literal, 0,
5913 i.op[op].disps, 0, i.reloc[op]);
2f2be86b 5914 i.types[op] = operand_type_and_not (i.types[op], anydisp);
67a4f2b7
AO
5915 }
5916 else
b300c311 5917 /* We only support 64bit displacement on constants. */
40fb9820 5918 i.types[op].bitfield.disp64 = 0;
252b5132 5919 }
29b0f896
AM
5920}
5921
4a1b91ea
L
5922/* Return 1 if there is a match in broadcast bytes between operand
5923 GIVEN and instruction template T. */
5924
5925static INLINE int
5926match_broadcast_size (const insn_template *t, unsigned int given)
5927{
5928 return ((t->opcode_modifier.broadcast == BYTE_BROADCAST
5929 && i.types[given].bitfield.byte)
5930 || (t->opcode_modifier.broadcast == WORD_BROADCAST
5931 && i.types[given].bitfield.word)
5932 || (t->opcode_modifier.broadcast == DWORD_BROADCAST
5933 && i.types[given].bitfield.dword)
5934 || (t->opcode_modifier.broadcast == QWORD_BROADCAST
5935 && i.types[given].bitfield.qword));
5936}
5937
6c30d220
L
5938/* Check if operands are valid for the instruction. */
5939
5940static int
5941check_VecOperands (const insn_template *t)
5942{
43234a1e 5943 unsigned int op;
e2195274 5944 i386_cpu_flags cpu;
e2195274
JB
5945
5946 /* Templates allowing for ZMMword as well as YMMword and/or XMMword for
5947 any one operand are implicity requiring AVX512VL support if the actual
5948 operand size is YMMword or XMMword. Since this function runs after
5949 template matching, there's no need to check for YMMword/XMMword in
5950 the template. */
5951 cpu = cpu_flags_and (t->cpu_flags, avx512);
5952 if (!cpu_flags_all_zero (&cpu)
5953 && !t->cpu_flags.bitfield.cpuavx512vl
5954 && !cpu_arch_flags.bitfield.cpuavx512vl)
5955 {
5956 for (op = 0; op < t->operands; ++op)
5957 {
5958 if (t->operand_types[op].bitfield.zmmword
5959 && (i.types[op].bitfield.ymmword
5960 || i.types[op].bitfield.xmmword))
5961 {
5962 i.error = unsupported;
5963 return 1;
5964 }
5965 }
5966 }
43234a1e 5967
22c36940
JB
5968 /* Somewhat similarly, templates specifying both AVX and AVX2 are
5969 requiring AVX2 support if the actual operand size is YMMword. */
5970 if (t->cpu_flags.bitfield.cpuavx
5971 && t->cpu_flags.bitfield.cpuavx2
5972 && !cpu_arch_flags.bitfield.cpuavx2)
5973 {
5974 for (op = 0; op < t->operands; ++op)
5975 {
5976 if (t->operand_types[op].bitfield.xmmword
5977 && i.types[op].bitfield.ymmword)
5978 {
5979 i.error = unsupported;
5980 return 1;
5981 }
5982 }
5983 }
5984
6c30d220 5985 /* Without VSIB byte, we can't have a vector register for index. */
63112cd6 5986 if (!t->opcode_modifier.sib
6c30d220 5987 && i.index_reg
1b54b8d7
JB
5988 && (i.index_reg->reg_type.bitfield.xmmword
5989 || i.index_reg->reg_type.bitfield.ymmword
5990 || i.index_reg->reg_type.bitfield.zmmword))
6c30d220
L
5991 {
5992 i.error = unsupported_vector_index_register;
5993 return 1;
5994 }
5995
ad8ecc81
MZ
5996 /* Check if default mask is allowed. */
5997 if (t->opcode_modifier.nodefmask
6225c532 5998 && (!i.mask.reg || i.mask.reg->reg_num == 0))
ad8ecc81
MZ
5999 {
6000 i.error = no_default_mask;
6001 return 1;
6002 }
6003
7bab8ab5
JB
6004 /* For VSIB byte, we need a vector register for index, and all vector
6005 registers must be distinct. */
260cd341 6006 if (t->opcode_modifier.sib && t->opcode_modifier.sib != SIBMEM)
7bab8ab5
JB
6007 {
6008 if (!i.index_reg
63112cd6 6009 || !((t->opcode_modifier.sib == VECSIB128
1b54b8d7 6010 && i.index_reg->reg_type.bitfield.xmmword)
63112cd6 6011 || (t->opcode_modifier.sib == VECSIB256
1b54b8d7 6012 && i.index_reg->reg_type.bitfield.ymmword)
63112cd6 6013 || (t->opcode_modifier.sib == VECSIB512
1b54b8d7 6014 && i.index_reg->reg_type.bitfield.zmmword)))
7bab8ab5
JB
6015 {
6016 i.error = invalid_vsib_address;
6017 return 1;
6018 }
6019
6225c532
JB
6020 gas_assert (i.reg_operands == 2 || i.mask.reg);
6021 if (i.reg_operands == 2 && !i.mask.reg)
43234a1e 6022 {
3528c362 6023 gas_assert (i.types[0].bitfield.class == RegSIMD);
1b54b8d7
JB
6024 gas_assert (i.types[0].bitfield.xmmword
6025 || i.types[0].bitfield.ymmword);
3528c362 6026 gas_assert (i.types[2].bitfield.class == RegSIMD);
1b54b8d7
JB
6027 gas_assert (i.types[2].bitfield.xmmword
6028 || i.types[2].bitfield.ymmword);
43234a1e
L
6029 if (operand_check == check_none)
6030 return 0;
6031 if (register_number (i.op[0].regs)
6032 != register_number (i.index_reg)
6033 && register_number (i.op[2].regs)
6034 != register_number (i.index_reg)
6035 && register_number (i.op[0].regs)
6036 != register_number (i.op[2].regs))
6037 return 0;
6038 if (operand_check == check_error)
6039 {
6040 i.error = invalid_vector_register_set;
6041 return 1;
6042 }
6043 as_warn (_("mask, index, and destination registers should be distinct"));
6044 }
6225c532 6045 else if (i.reg_operands == 1 && i.mask.reg)
8444f82a 6046 {
3528c362 6047 if (i.types[1].bitfield.class == RegSIMD
1b54b8d7
JB
6048 && (i.types[1].bitfield.xmmword
6049 || i.types[1].bitfield.ymmword
6050 || i.types[1].bitfield.zmmword)
8444f82a
MZ
6051 && (register_number (i.op[1].regs)
6052 == register_number (i.index_reg)))
6053 {
6054 if (operand_check == check_error)
6055 {
6056 i.error = invalid_vector_register_set;
6057 return 1;
6058 }
6059 if (operand_check != check_none)
6060 as_warn (_("index and destination registers should be distinct"));
6061 }
6062 }
43234a1e 6063 }
7bab8ab5 6064
fc141319
L
6065 /* For AMX instructions with 3 TMM register operands, all operands
6066 must be distinct. */
6067 if (i.reg_operands == 3
6068 && t->operand_types[0].bitfield.tmmword
6069 && (i.op[0].regs == i.op[1].regs
6070 || i.op[0].regs == i.op[2].regs
6071 || i.op[1].regs == i.op[2].regs))
6072 {
6073 i.error = invalid_tmm_register_set;
6074 return 1;
260cd341
LC
6075 }
6076
0cc78721
CL
6077 /* For some special instructions require that destination must be distinct
6078 from source registers. */
6079 if (t->opcode_modifier.distinctdest)
6080 {
6081 unsigned int dest_reg = i.operands - 1;
6082
6083 know (i.operands >= 3);
6084
6085 /* #UD if dest_reg == src1_reg or dest_reg == src2_reg. */
6086 if (i.op[dest_reg - 1].regs == i.op[dest_reg].regs
6087 || (i.reg_operands > 2
6088 && i.op[dest_reg - 2].regs == i.op[dest_reg].regs))
6089 {
6090 i.error = invalid_dest_and_src_register_set;
6091 return 1;
6092 }
6093 }
6094
43234a1e
L
6095 /* Check if broadcast is supported by the instruction and is applied
6096 to the memory operand. */
a5748e0d 6097 if (i.broadcast.type || i.broadcast.bytes)
43234a1e 6098 {
8e6e0792 6099 i386_operand_type type, overlap;
43234a1e
L
6100
6101 /* Check if specified broadcast is supported in this instruction,
4a1b91ea 6102 and its broadcast bytes match the memory operand. */
5273a3cd 6103 op = i.broadcast.operand;
8e6e0792 6104 if (!t->opcode_modifier.broadcast
c48dadc9 6105 || !(i.flags[op] & Operand_Mem)
c39e5b26 6106 || (!i.types[op].bitfield.unspecified
4a1b91ea 6107 && !match_broadcast_size (t, op)))
43234a1e
L
6108 {
6109 bad_broadcast:
6110 i.error = unsupported_broadcast;
6111 return 1;
6112 }
8e6e0792 6113
a5748e0d
JB
6114 if (i.broadcast.type)
6115 i.broadcast.bytes = ((1 << (t->opcode_modifier.broadcast - 1))
6116 * i.broadcast.type);
8e6e0792 6117 operand_type_set (&type, 0);
a5748e0d 6118 switch (get_broadcast_bytes (t, false))
8e6e0792 6119 {
4a1b91ea
L
6120 case 2:
6121 type.bitfield.word = 1;
6122 break;
6123 case 4:
6124 type.bitfield.dword = 1;
6125 break;
8e6e0792
JB
6126 case 8:
6127 type.bitfield.qword = 1;
6128 break;
6129 case 16:
6130 type.bitfield.xmmword = 1;
6131 break;
6132 case 32:
6133 type.bitfield.ymmword = 1;
6134 break;
6135 case 64:
6136 type.bitfield.zmmword = 1;
6137 break;
6138 default:
6139 goto bad_broadcast;
6140 }
6141
6142 overlap = operand_type_and (type, t->operand_types[op]);
bc49bfd8
JB
6143 if (t->operand_types[op].bitfield.class == RegSIMD
6144 && t->operand_types[op].bitfield.byte
6145 + t->operand_types[op].bitfield.word
6146 + t->operand_types[op].bitfield.dword
6147 + t->operand_types[op].bitfield.qword > 1)
6148 {
6149 overlap.bitfield.xmmword = 0;
6150 overlap.bitfield.ymmword = 0;
6151 overlap.bitfield.zmmword = 0;
6152 }
8e6e0792
JB
6153 if (operand_type_all_zero (&overlap))
6154 goto bad_broadcast;
6155
6156 if (t->opcode_modifier.checkregsize)
6157 {
6158 unsigned int j;
6159
e2195274 6160 type.bitfield.baseindex = 1;
8e6e0792
JB
6161 for (j = 0; j < i.operands; ++j)
6162 {
6163 if (j != op
6164 && !operand_type_register_match(i.types[j],
6165 t->operand_types[j],
6166 type,
6167 t->operand_types[op]))
6168 goto bad_broadcast;
6169 }
6170 }
43234a1e
L
6171 }
6172 /* If broadcast is supported in this instruction, we need to check if
6173 operand of one-element size isn't specified without broadcast. */
6174 else if (t->opcode_modifier.broadcast && i.mem_operands)
6175 {
6176 /* Find memory operand. */
6177 for (op = 0; op < i.operands; op++)
8dc0818e 6178 if (i.flags[op] & Operand_Mem)
43234a1e
L
6179 break;
6180 gas_assert (op < i.operands);
6181 /* Check size of the memory operand. */
4a1b91ea 6182 if (match_broadcast_size (t, op))
43234a1e
L
6183 {
6184 i.error = broadcast_needed;
6185 return 1;
6186 }
6187 }
c39e5b26
JB
6188 else
6189 op = MAX_OPERANDS - 1; /* Avoid uninitialized variable warning. */
43234a1e
L
6190
6191 /* Check if requested masking is supported. */
6225c532 6192 if (i.mask.reg)
43234a1e 6193 {
ae2387fe
JB
6194 switch (t->opcode_modifier.masking)
6195 {
6196 case BOTH_MASKING:
6197 break;
6198 case MERGING_MASKING:
6225c532 6199 if (i.mask.zeroing)
ae2387fe
JB
6200 {
6201 case 0:
6202 i.error = unsupported_masking;
6203 return 1;
6204 }
6205 break;
6206 case DYNAMIC_MASKING:
6207 /* Memory destinations allow only merging masking. */
6225c532 6208 if (i.mask.zeroing && i.mem_operands)
ae2387fe
JB
6209 {
6210 /* Find memory operand. */
6211 for (op = 0; op < i.operands; op++)
c48dadc9 6212 if (i.flags[op] & Operand_Mem)
ae2387fe
JB
6213 break;
6214 gas_assert (op < i.operands);
6215 if (op == i.operands - 1)
6216 {
6217 i.error = unsupported_masking;
6218 return 1;
6219 }
6220 }
6221 break;
6222 default:
6223 abort ();
6224 }
43234a1e
L
6225 }
6226
6227 /* Check if masking is applied to dest operand. */
6225c532 6228 if (i.mask.reg && (i.mask.operand != i.operands - 1))
43234a1e
L
6229 {
6230 i.error = mask_not_on_destination;
6231 return 1;
6232 }
6233
43234a1e 6234 /* Check RC/SAE. */
ca5312a2 6235 if (i.rounding.type != rc_none)
43234a1e 6236 {
a80195f1 6237 if (!t->opcode_modifier.sae
cf665fee
JB
6238 || ((i.rounding.type != saeonly) != t->opcode_modifier.staticrounding)
6239 || i.mem_operands)
43234a1e
L
6240 {
6241 i.error = unsupported_rc_sae;
6242 return 1;
6243 }
cf665fee
JB
6244
6245 /* Non-EVEX.LIG forms need to have a ZMM register as at least one
6246 operand. */
6247 if (t->opcode_modifier.evex != EVEXLIG)
7bab8ab5 6248 {
cf665fee
JB
6249 for (op = 0; op < t->operands; ++op)
6250 if (i.types[op].bitfield.zmmword)
6251 break;
6252 if (op >= t->operands)
6253 {
6254 i.error = operand_size_mismatch;
6255 return 1;
6256 }
7bab8ab5 6257 }
6c30d220
L
6258 }
6259
da4977e0
JB
6260 /* Check the special Imm4 cases; must be the first operand. */
6261 if (t->cpu_flags.bitfield.cpuxop && t->operands == 5)
6262 {
6263 if (i.op[0].imms->X_op != O_constant
6264 || !fits_in_imm4 (i.op[0].imms->X_add_number))
6265 {
6266 i.error = bad_imm4;
6267 return 1;
6268 }
6269
6270 /* Turn off Imm<N> so that update_imm won't complain. */
6271 operand_type_set (&i.types[0], 0);
6272 }
6273
43234a1e 6274 /* Check vector Disp8 operand. */
b5014f7a 6275 if (t->opcode_modifier.disp8memshift
1a42a9fe 6276 && i.disp_encoding <= disp_encoding_8bit)
43234a1e 6277 {
a5748e0d 6278 if (i.broadcast.bytes)
4a1b91ea 6279 i.memshift = t->opcode_modifier.broadcast - 1;
7091c612 6280 else if (t->opcode_modifier.disp8memshift != DISP8_SHIFT_VL)
43234a1e 6281 i.memshift = t->opcode_modifier.disp8memshift;
7091c612
JB
6282 else
6283 {
125ff819 6284 const i386_operand_type *type = NULL, *fallback = NULL;
7091c612
JB
6285
6286 i.memshift = 0;
6287 for (op = 0; op < i.operands; op++)
8dc0818e 6288 if (i.flags[op] & Operand_Mem)
7091c612 6289 {
4174bfff
JB
6290 if (t->opcode_modifier.evex == EVEXLIG)
6291 i.memshift = 2 + (i.suffix == QWORD_MNEM_SUFFIX);
6292 else if (t->operand_types[op].bitfield.xmmword
6293 + t->operand_types[op].bitfield.ymmword
6294 + t->operand_types[op].bitfield.zmmword <= 1)
7091c612
JB
6295 type = &t->operand_types[op];
6296 else if (!i.types[op].bitfield.unspecified)
6297 type = &i.types[op];
125ff819
JB
6298 else /* Ambiguities get resolved elsewhere. */
6299 fallback = &t->operand_types[op];
7091c612 6300 }
3528c362 6301 else if (i.types[op].bitfield.class == RegSIMD
4174bfff 6302 && t->opcode_modifier.evex != EVEXLIG)
7091c612
JB
6303 {
6304 if (i.types[op].bitfield.zmmword)
6305 i.memshift = 6;
6306 else if (i.types[op].bitfield.ymmword && i.memshift < 5)
6307 i.memshift = 5;
6308 else if (i.types[op].bitfield.xmmword && i.memshift < 4)
6309 i.memshift = 4;
6310 }
6311
125ff819
JB
6312 if (!type && !i.memshift)
6313 type = fallback;
7091c612
JB
6314 if (type)
6315 {
6316 if (type->bitfield.zmmword)
6317 i.memshift = 6;
6318 else if (type->bitfield.ymmword)
6319 i.memshift = 5;
6320 else if (type->bitfield.xmmword)
6321 i.memshift = 4;
6322 }
6323
6324 /* For the check in fits_in_disp8(). */
6325 if (i.memshift == 0)
6326 i.memshift = -1;
6327 }
43234a1e
L
6328
6329 for (op = 0; op < i.operands; op++)
6330 if (operand_type_check (i.types[op], disp)
6331 && i.op[op].disps->X_op == O_constant)
6332 {
b5014f7a 6333 if (fits_in_disp8 (i.op[op].disps->X_add_number))
43234a1e 6334 {
b5014f7a
JB
6335 i.types[op].bitfield.disp8 = 1;
6336 return 0;
43234a1e 6337 }
b5014f7a 6338 i.types[op].bitfield.disp8 = 0;
43234a1e
L
6339 }
6340 }
b5014f7a
JB
6341
6342 i.memshift = 0;
43234a1e 6343
6c30d220
L
6344 return 0;
6345}
6346
da4977e0 6347/* Check if encoding requirements are met by the instruction. */
a683cc34
SP
6348
6349static int
da4977e0 6350VEX_check_encoding (const insn_template *t)
a683cc34 6351{
da4977e0
JB
6352 if (i.vec_encoding == vex_encoding_error)
6353 {
6354 i.error = unsupported;
6355 return 1;
6356 }
6357
86fa6981 6358 if (i.vec_encoding == vex_encoding_evex)
43234a1e 6359 {
86fa6981 6360 /* This instruction must be encoded with EVEX prefix. */
e771e7c9 6361 if (!is_evex_encoding (t))
86fa6981
L
6362 {
6363 i.error = unsupported;
6364 return 1;
6365 }
6366 return 0;
43234a1e
L
6367 }
6368
a683cc34 6369 if (!t->opcode_modifier.vex)
86fa6981
L
6370 {
6371 /* This instruction template doesn't have VEX prefix. */
6372 if (i.vec_encoding != vex_encoding_default)
6373 {
6374 i.error = unsupported;
6375 return 1;
6376 }
6377 return 0;
6378 }
a683cc34 6379
a683cc34
SP
6380 return 0;
6381}
6382
7b94647a
JB
6383/* Helper function for the progress() macro in match_template(). */
6384static INLINE enum i386_error progress (enum i386_error new,
6385 enum i386_error last,
6386 unsigned int line, unsigned int *line_p)
6387{
6388 if (line <= *line_p)
6389 return last;
6390 *line_p = line;
6391 return new;
6392}
6393
d3ce72d0 6394static const insn_template *
83b16ac6 6395match_template (char mnem_suffix)
29b0f896
AM
6396{
6397 /* Points to template once we've found it. */
d3ce72d0 6398 const insn_template *t;
40fb9820 6399 i386_operand_type overlap0, overlap1, overlap2, overlap3;
c0f3af97 6400 i386_operand_type overlap4;
29b0f896 6401 unsigned int found_reverse_match;
dc2be329 6402 i386_opcode_modifier suffix_check;
40fb9820 6403 i386_operand_type operand_types [MAX_OPERANDS];
539e75ad 6404 int addr_prefix_disp;
7b94647a
JB
6405 unsigned int j, size_match, check_register, errline = __LINE__;
6406 enum i386_error specific_error = number_of_operands_mismatch;
6407#define progress(err) progress (err, specific_error, __LINE__, &errline)
29b0f896 6408
c0f3af97
L
6409#if MAX_OPERANDS != 5
6410# error "MAX_OPERANDS must be 5."
f48ff2ae
L
6411#endif
6412
29b0f896 6413 found_reverse_match = 0;
539e75ad 6414 addr_prefix_disp = -1;
40fb9820 6415
dc2be329 6416 /* Prepare for mnemonic suffix check. */
40fb9820 6417 memset (&suffix_check, 0, sizeof (suffix_check));
dc2be329
L
6418 switch (mnem_suffix)
6419 {
6420 case BYTE_MNEM_SUFFIX:
6421 suffix_check.no_bsuf = 1;
6422 break;
6423 case WORD_MNEM_SUFFIX:
6424 suffix_check.no_wsuf = 1;
6425 break;
6426 case SHORT_MNEM_SUFFIX:
6427 suffix_check.no_ssuf = 1;
6428 break;
6429 case LONG_MNEM_SUFFIX:
6430 suffix_check.no_lsuf = 1;
6431 break;
6432 case QWORD_MNEM_SUFFIX:
6433 suffix_check.no_qsuf = 1;
6434 break;
6435 default:
6436 /* NB: In Intel syntax, normally we can check for memory operand
6437 size when there is no mnemonic suffix. But jmp and call have
6438 2 different encodings with Dword memory operand size, one with
6439 No_ldSuf and the other without. i.suffix is set to
6440 LONG_DOUBLE_MNEM_SUFFIX to skip the one with No_ldSuf. */
6441 if (i.suffix == LONG_DOUBLE_MNEM_SUFFIX)
6442 suffix_check.no_ldsuf = 1;
83b16ac6
JB
6443 }
6444
45aa61fe 6445 for (t = current_templates->start; t < current_templates->end; t++)
29b0f896 6446 {
539e75ad 6447 addr_prefix_disp = -1;
dbbc8b7e 6448 found_reverse_match = 0;
539e75ad 6449
7b94647a 6450 /* Must have right number of operands. */
29b0f896
AM
6451 if (i.operands != t->operands)
6452 continue;
6453
50aecf8c 6454 /* Check processor support. */
7b94647a 6455 specific_error = progress (unsupported);
45a4bb20 6456 if (cpu_flags_match (t) != CPU_FLAGS_PERFECT_MATCH)
50aecf8c
L
6457 continue;
6458
e1d4d893 6459 /* Check AT&T mnemonic. */
7b94647a 6460 specific_error = progress (unsupported_with_intel_mnemonic);
e1d4d893 6461 if (intel_mnemonic && t->opcode_modifier.attmnemonic)
1efbbeb4
L
6462 continue;
6463
4b5aaf5f 6464 /* Check AT&T/Intel syntax. */
7b94647a 6465 specific_error = progress (unsupported_syntax);
5c07affc 6466 if ((intel_syntax && t->opcode_modifier.attsyntax)
4b5aaf5f 6467 || (!intel_syntax && t->opcode_modifier.intelsyntax))
1efbbeb4
L
6468 continue;
6469
4b5aaf5f
L
6470 /* Check Intel64/AMD64 ISA. */
6471 switch (isa64)
6472 {
6473 default:
6474 /* Default: Don't accept Intel64. */
6475 if (t->opcode_modifier.isa64 == INTEL64)
6476 continue;
6477 break;
6478 case amd64:
6479 /* -mamd64: Don't accept Intel64 and Intel64 only. */
6480 if (t->opcode_modifier.isa64 >= INTEL64)
6481 continue;
6482 break;
6483 case intel64:
6484 /* -mintel64: Don't accept AMD64. */
5990e377 6485 if (t->opcode_modifier.isa64 == AMD64 && flag_code == CODE_64BIT)
4b5aaf5f
L
6486 continue;
6487 break;
6488 }
6489
dc2be329 6490 /* Check the suffix. */
7b94647a 6491 specific_error = progress (invalid_instruction_suffix);
dc2be329
L
6492 if ((t->opcode_modifier.no_bsuf && suffix_check.no_bsuf)
6493 || (t->opcode_modifier.no_wsuf && suffix_check.no_wsuf)
6494 || (t->opcode_modifier.no_lsuf && suffix_check.no_lsuf)
6495 || (t->opcode_modifier.no_ssuf && suffix_check.no_ssuf)
6496 || (t->opcode_modifier.no_qsuf && suffix_check.no_qsuf)
6497 || (t->opcode_modifier.no_ldsuf && suffix_check.no_ldsuf))
83b16ac6 6498 continue;
29b0f896 6499
7b94647a 6500 specific_error = progress (operand_size_mismatch);
3ac21baa
JB
6501 size_match = operand_size_match (t);
6502 if (!size_match)
7d5e4556 6503 continue;
539e75ad 6504
6f2f06be
JB
6505 /* This is intentionally not
6506
0cfa3eb3 6507 if (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE))
6f2f06be
JB
6508
6509 as the case of a missing * on the operand is accepted (perhaps with
6510 a warning, issued further down). */
7b94647a 6511 specific_error = progress (operand_type_mismatch);
0cfa3eb3 6512 if (i.jumpabsolute && t->opcode_modifier.jump != JUMP_ABSOLUTE)
7b94647a 6513 continue;
6f2f06be 6514
5c07affc
L
6515 for (j = 0; j < MAX_OPERANDS; j++)
6516 operand_types[j] = t->operand_types[j];
6517
e365e234
JB
6518 /* In general, don't allow
6519 - 64-bit operands outside of 64-bit mode,
6520 - 32-bit operands on pre-386. */
7b94647a
JB
6521 specific_error = progress (mnem_suffix ? invalid_instruction_suffix
6522 : operand_size_mismatch);
4873e243 6523 j = i.imm_operands + (t->operands > i.imm_operands + 1);
e365e234
JB
6524 if (((i.suffix == QWORD_MNEM_SUFFIX
6525 && flag_code != CODE_64BIT
389d00a5
JB
6526 && !(t->opcode_modifier.opcodespace == SPACE_0F
6527 && t->base_opcode == 0xc7
5e74b495 6528 && t->opcode_modifier.opcodeprefix == PREFIX_NONE
8b65b895 6529 && t->extension_opcode == 1) /* cmpxchg8b */)
e365e234
JB
6530 || (i.suffix == LONG_MNEM_SUFFIX
6531 && !cpu_arch_flags.bitfield.cpui386))
45aa61fe 6532 && (intel_syntax
3cd7f3e3 6533 ? (t->opcode_modifier.mnemonicsize != IGNORESIZE
45aa61fe
AM
6534 && !intel_float_operand (t->name))
6535 : intel_float_operand (t->name) != 2)
4873e243
JB
6536 && (t->operands == i.imm_operands
6537 || (operand_types[i.imm_operands].bitfield.class != RegMMX
6538 && operand_types[i.imm_operands].bitfield.class != RegSIMD
6539 && operand_types[i.imm_operands].bitfield.class != RegMask)
6540 || (operand_types[j].bitfield.class != RegMMX
6541 && operand_types[j].bitfield.class != RegSIMD
6542 && operand_types[j].bitfield.class != RegMask))
63112cd6 6543 && !t->opcode_modifier.sib)
192dc9c6
JB
6544 continue;
6545
29b0f896 6546 /* Do not verify operands when there are none. */
e365e234 6547 if (!t->operands)
da4977e0
JB
6548 {
6549 if (VEX_check_encoding (t))
6550 {
7b94647a 6551 specific_error = progress (i.error);
da4977e0
JB
6552 continue;
6553 }
6554
6555 /* We've found a match; break out of loop. */
6556 break;
6557 }
252b5132 6558
48bcea9f
JB
6559 if (!t->opcode_modifier.jump
6560 || t->opcode_modifier.jump == JUMP_ABSOLUTE)
6561 {
6562 /* There should be only one Disp operand. */
6563 for (j = 0; j < MAX_OPERANDS; j++)
6564 if (operand_type_check (operand_types[j], disp))
539e75ad 6565 break;
48bcea9f
JB
6566 if (j < MAX_OPERANDS)
6567 {
5b7c81bd 6568 bool override = (i.prefix[ADDR_PREFIX] != 0);
48bcea9f
JB
6569
6570 addr_prefix_disp = j;
6571
a775efc8
JB
6572 /* Address size prefix will turn Disp64 operand into Disp32 and
6573 Disp32/Disp16 one into Disp16/Disp32 respectively. */
48bcea9f 6574 switch (flag_code)
40fb9820 6575 {
48bcea9f
JB
6576 case CODE_16BIT:
6577 override = !override;
6578 /* Fall through. */
6579 case CODE_32BIT:
6580 if (operand_types[j].bitfield.disp32
6581 && operand_types[j].bitfield.disp16)
40fb9820 6582 {
48bcea9f
JB
6583 operand_types[j].bitfield.disp16 = override;
6584 operand_types[j].bitfield.disp32 = !override;
40fb9820 6585 }
a775efc8 6586 gas_assert (!operand_types[j].bitfield.disp64);
48bcea9f
JB
6587 break;
6588
6589 case CODE_64BIT:
a775efc8 6590 if (operand_types[j].bitfield.disp64)
40fb9820 6591 {
a775efc8 6592 gas_assert (!operand_types[j].bitfield.disp32);
48bcea9f 6593 operand_types[j].bitfield.disp32 = override;
a775efc8 6594 operand_types[j].bitfield.disp64 = !override;
40fb9820 6595 }
48bcea9f
JB
6596 operand_types[j].bitfield.disp16 = 0;
6597 break;
40fb9820 6598 }
539e75ad 6599 }
48bcea9f 6600 }
539e75ad 6601
d7e3e627
L
6602 switch (i.reloc[0])
6603 {
6604 case BFD_RELOC_386_GOT32:
6605 /* Force 0x8b encoding for "mov foo@GOT, %eax". */
6606 if (t->base_opcode == 0xa0
6607 && t->opcode_modifier.opcodespace == SPACE_BASE)
6608 continue;
6609 break;
6610 case BFD_RELOC_386_TLS_GOTIE:
6611 case BFD_RELOC_386_TLS_LE_32:
6612 case BFD_RELOC_X86_64_GOTTPOFF:
6613 case BFD_RELOC_X86_64_TLSLD:
6614 /* Don't allow KMOV in TLS code sequences. */
6615 if (t->opcode_modifier.vex)
6616 continue;
6617 break;
6618 default:
6619 break;
6620 }
02a86693 6621
56ffb741 6622 /* We check register size if needed. */
e2195274
JB
6623 if (t->opcode_modifier.checkregsize)
6624 {
6625 check_register = (1 << t->operands) - 1;
a5748e0d 6626 if (i.broadcast.type || i.broadcast.bytes)
5273a3cd 6627 check_register &= ~(1 << i.broadcast.operand);
e2195274
JB
6628 }
6629 else
6630 check_register = 0;
6631
c6fb90c8 6632 overlap0 = operand_type_and (i.types[0], operand_types[0]);
29b0f896
AM
6633 switch (t->operands)
6634 {
6635 case 1:
40fb9820 6636 if (!operand_type_match (overlap0, i.types[0]))
29b0f896
AM
6637 continue;
6638 break;
6639 case 2:
33eaf5de 6640 /* xchg %eax, %eax is a special case. It is an alias for nop
8b38ad71
L
6641 only in 32bit mode and we can use opcode 0x90. In 64bit
6642 mode, we can't use 0x90 for xchg %eax, %eax since it should
6643 zero-extend %eax to %rax. */
6644 if (flag_code == CODE_64BIT
6645 && t->base_opcode == 0x90
35648716 6646 && t->opcode_modifier.opcodespace == SPACE_BASE
75e5731b
JB
6647 && i.types[0].bitfield.instance == Accum
6648 && i.types[0].bitfield.dword
6649 && i.types[1].bitfield.instance == Accum
6650 && i.types[1].bitfield.dword)
8b38ad71 6651 continue;
1212781b
JB
6652 /* xrelease mov %eax, <disp> is another special case. It must not
6653 match the accumulator-only encoding of mov. */
6654 if (flag_code != CODE_64BIT
6655 && i.hle_prefix
6656 && t->base_opcode == 0xa0
35648716 6657 && t->opcode_modifier.opcodespace == SPACE_BASE
75e5731b 6658 && i.types[0].bitfield.instance == Accum
8dc0818e 6659 && (i.flags[1] & Operand_Mem))
1212781b 6660 continue;
f5eb1d70
JB
6661 /* Fall through. */
6662
6663 case 3:
3ac21baa
JB
6664 if (!(size_match & MATCH_STRAIGHT))
6665 goto check_reverse;
64c49ab3
JB
6666 /* Reverse direction of operands if swapping is possible in the first
6667 place (operands need to be symmetric) and
6668 - the load form is requested, and the template is a store form,
6669 - the store form is requested, and the template is a load form,
6670 - the non-default (swapped) form is requested. */
6671 overlap1 = operand_type_and (operand_types[0], operand_types[1]);
f5eb1d70 6672 if (t->opcode_modifier.d && i.reg_operands == i.operands
64c49ab3
JB
6673 && !operand_type_all_zero (&overlap1))
6674 switch (i.dir_encoding)
6675 {
6676 case dir_encoding_load:
6677 if (operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 6678 || t->opcode_modifier.regmem)
64c49ab3
JB
6679 goto check_reverse;
6680 break;
6681
6682 case dir_encoding_store:
6683 if (!operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 6684 && !t->opcode_modifier.regmem)
64c49ab3
JB
6685 goto check_reverse;
6686 break;
6687
6688 case dir_encoding_swap:
6689 goto check_reverse;
6690
6691 case dir_encoding_default:
6692 break;
6693 }
86fa6981 6694 /* If we want store form, we skip the current load. */
64c49ab3
JB
6695 if ((i.dir_encoding == dir_encoding_store
6696 || i.dir_encoding == dir_encoding_swap)
86fa6981
L
6697 && i.mem_operands == 0
6698 && t->opcode_modifier.load)
fa99fab2 6699 continue;
1a0670f3 6700 /* Fall through. */
f48ff2ae 6701 case 4:
c0f3af97 6702 case 5:
c6fb90c8 6703 overlap1 = operand_type_and (i.types[1], operand_types[1]);
40fb9820
L
6704 if (!operand_type_match (overlap0, i.types[0])
6705 || !operand_type_match (overlap1, i.types[1])
e2195274 6706 || ((check_register & 3) == 3
dc821c5f 6707 && !operand_type_register_match (i.types[0],
40fb9820 6708 operand_types[0],
dc821c5f 6709 i.types[1],
40fb9820 6710 operand_types[1])))
29b0f896 6711 {
7b94647a
JB
6712 specific_error = progress (i.error);
6713
29b0f896 6714 /* Check if other direction is valid ... */
38e314eb 6715 if (!t->opcode_modifier.d)
29b0f896
AM
6716 continue;
6717
dc1e8a47 6718 check_reverse:
3ac21baa
JB
6719 if (!(size_match & MATCH_REVERSE))
6720 continue;
29b0f896 6721 /* Try reversing direction of operands. */
8bd915b7
JB
6722 j = t->opcode_modifier.vexsources ? 1 : i.operands - 1;
6723 overlap0 = operand_type_and (i.types[0], operand_types[j]);
6724 overlap1 = operand_type_and (i.types[j], operand_types[0]);
c975cec5
JB
6725 overlap2 = operand_type_and (i.types[1], operand_types[1]);
6726 gas_assert (t->operands != 3 || !check_register);
40fb9820 6727 if (!operand_type_match (overlap0, i.types[0])
8bd915b7 6728 || !operand_type_match (overlap1, i.types[j])
c975cec5
JB
6729 || (t->operands == 3
6730 && !operand_type_match (overlap2, i.types[1]))
45664ddb 6731 || (check_register
dc821c5f 6732 && !operand_type_register_match (i.types[0],
8bd915b7
JB
6733 operand_types[j],
6734 i.types[j],
45664ddb 6735 operand_types[0])))
29b0f896
AM
6736 {
6737 /* Does not match either direction. */
7b94647a 6738 specific_error = progress (i.error);
29b0f896
AM
6739 continue;
6740 }
38e314eb 6741 /* found_reverse_match holds which of D or FloatR
29b0f896 6742 we've found. */
38e314eb
JB
6743 if (!t->opcode_modifier.d)
6744 found_reverse_match = 0;
6745 else if (operand_types[0].bitfield.tbyte)
8a2ed489 6746 found_reverse_match = Opcode_FloatD;
8bd915b7
JB
6747 else if (t->opcode_modifier.vexsources)
6748 {
6749 found_reverse_match = Opcode_VexW;
6750 goto check_operands_345;
6751 }
2c735193
JB
6752 else if (t->opcode_modifier.opcodespace != SPACE_BASE
6753 && (t->opcode_modifier.opcodespace != SPACE_0F
6754 /* MOV to/from CR/DR/TR, as an exception, follow
6755 the base opcode space encoding model. */
6756 || (t->base_opcode | 7) != 0x27))
dbbc8b7e 6757 found_reverse_match = (t->base_opcode & 0xee) != 0x6e
2c735193 6758 ? Opcode_ExtD : Opcode_SIMD_IntD;
8a2ed489 6759 else
38e314eb 6760 found_reverse_match = Opcode_D;
40fb9820 6761 if (t->opcode_modifier.floatr)
8a2ed489 6762 found_reverse_match |= Opcode_FloatR;
29b0f896 6763 }
f48ff2ae 6764 else
29b0f896 6765 {
f48ff2ae 6766 /* Found a forward 2 operand match here. */
8bd915b7 6767 check_operands_345:
d1cbb4db
L
6768 switch (t->operands)
6769 {
c0f3af97 6770 case 5:
3d0738af 6771 overlap4 = operand_type_and (i.types[4], operand_types[4]);
c0f3af97 6772 if (!operand_type_match (overlap4, i.types[4])
dc821c5f 6773 || !operand_type_register_match (i.types[3],
c0f3af97 6774 operand_types[3],
c0f3af97
L
6775 i.types[4],
6776 operand_types[4]))
7b94647a
JB
6777 {
6778 specific_error = progress (i.error);
6779 continue;
6780 }
1a0670f3 6781 /* Fall through. */
f48ff2ae 6782 case 4:
3d0738af 6783 overlap3 = operand_type_and (i.types[3], operand_types[3]);
40fb9820 6784 if (!operand_type_match (overlap3, i.types[3])
e2195274
JB
6785 || ((check_register & 0xa) == 0xa
6786 && !operand_type_register_match (i.types[1],
f7768225
JB
6787 operand_types[1],
6788 i.types[3],
e2195274
JB
6789 operand_types[3]))
6790 || ((check_register & 0xc) == 0xc
6791 && !operand_type_register_match (i.types[2],
6792 operand_types[2],
6793 i.types[3],
6794 operand_types[3])))
7b94647a
JB
6795 {
6796 specific_error = progress (i.error);
6797 continue;
6798 }
1a0670f3 6799 /* Fall through. */
f48ff2ae 6800 case 3:
3d0738af 6801 overlap2 = operand_type_and (i.types[2], operand_types[2]);
40fb9820 6802 if (!operand_type_match (overlap2, i.types[2])
e2195274
JB
6803 || ((check_register & 5) == 5
6804 && !operand_type_register_match (i.types[0],
23e42951
JB
6805 operand_types[0],
6806 i.types[2],
e2195274
JB
6807 operand_types[2]))
6808 || ((check_register & 6) == 6
6809 && !operand_type_register_match (i.types[1],
6810 operand_types[1],
6811 i.types[2],
6812 operand_types[2])))
7b94647a
JB
6813 {
6814 specific_error = progress (i.error);
6815 continue;
6816 }
f48ff2ae
L
6817 break;
6818 }
29b0f896 6819 }
f48ff2ae 6820 /* Found either forward/reverse 2, 3 or 4 operand match here:
29b0f896
AM
6821 slip through to break. */
6822 }
c0f3af97 6823
9bb4d860
L
6824 /* Check if VEX/EVEX encoding requirements can be satisfied. */
6825 if (VEX_check_encoding (t))
da4977e0 6826 {
7b94647a 6827 specific_error = progress (i.error);
da4977e0
JB
6828 continue;
6829 }
6830
9bb4d860
L
6831 /* Check if vector operands are valid. */
6832 if (check_VecOperands (t))
5614d22c 6833 {
7b94647a 6834 specific_error = progress (i.error);
5614d22c
JB
6835 continue;
6836 }
a683cc34 6837
29b0f896
AM
6838 /* We've found a match; break out of loop. */
6839 break;
6840 }
6841
7b94647a
JB
6842#undef progress
6843
29b0f896
AM
6844 if (t == current_templates->end)
6845 {
6846 /* We found no match. */
a65babc9 6847 const char *err_msg;
7b94647a 6848 switch (specific_error)
a65babc9
L
6849 {
6850 default:
6851 abort ();
86e026a4 6852 case operand_size_mismatch:
a65babc9
L
6853 err_msg = _("operand size mismatch");
6854 break;
6855 case operand_type_mismatch:
6856 err_msg = _("operand type mismatch");
6857 break;
6858 case register_type_mismatch:
6859 err_msg = _("register type mismatch");
6860 break;
6861 case number_of_operands_mismatch:
6862 err_msg = _("number of operands mismatch");
6863 break;
6864 case invalid_instruction_suffix:
6865 err_msg = _("invalid instruction suffix");
6866 break;
6867 case bad_imm4:
4a2608e3 6868 err_msg = _("constant doesn't fit in 4 bits");
a65babc9 6869 break;
a65babc9
L
6870 case unsupported_with_intel_mnemonic:
6871 err_msg = _("unsupported with Intel mnemonic");
6872 break;
6873 case unsupported_syntax:
6874 err_msg = _("unsupported syntax");
6875 break;
6876 case unsupported:
35262a23 6877 as_bad (_("unsupported instruction `%s'"),
10efe3f6
L
6878 current_templates->start->name);
6879 return NULL;
260cd341
LC
6880 case invalid_sib_address:
6881 err_msg = _("invalid SIB address");
6882 break;
6c30d220
L
6883 case invalid_vsib_address:
6884 err_msg = _("invalid VSIB address");
6885 break;
7bab8ab5
JB
6886 case invalid_vector_register_set:
6887 err_msg = _("mask, index, and destination registers must be distinct");
6888 break;
260cd341
LC
6889 case invalid_tmm_register_set:
6890 err_msg = _("all tmm registers must be distinct");
6891 break;
0cc78721
CL
6892 case invalid_dest_and_src_register_set:
6893 err_msg = _("destination and source registers must be distinct");
6894 break;
6c30d220
L
6895 case unsupported_vector_index_register:
6896 err_msg = _("unsupported vector index register");
6897 break;
43234a1e
L
6898 case unsupported_broadcast:
6899 err_msg = _("unsupported broadcast");
6900 break;
43234a1e
L
6901 case broadcast_needed:
6902 err_msg = _("broadcast is needed for operand of such type");
6903 break;
6904 case unsupported_masking:
6905 err_msg = _("unsupported masking");
6906 break;
6907 case mask_not_on_destination:
6908 err_msg = _("mask not on destination operand");
6909 break;
6910 case no_default_mask:
6911 err_msg = _("default mask isn't allowed");
6912 break;
6913 case unsupported_rc_sae:
6914 err_msg = _("unsupported static rounding/sae");
6915 break;
43234a1e
L
6916 case invalid_register_operand:
6917 err_msg = _("invalid register operand");
6918 break;
a65babc9
L
6919 }
6920 as_bad (_("%s for `%s'"), err_msg,
891edac4 6921 current_templates->start->name);
fa99fab2 6922 return NULL;
29b0f896 6923 }
252b5132 6924
29b0f896
AM
6925 if (!quiet_warnings)
6926 {
6927 if (!intel_syntax
0cfa3eb3 6928 && (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE)))
6f2f06be 6929 as_warn (_("indirect %s without `*'"), t->name);
29b0f896 6930
40fb9820 6931 if (t->opcode_modifier.isprefix
3cd7f3e3 6932 && t->opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
6933 {
6934 /* Warn them that a data or address size prefix doesn't
6935 affect assembly of the next line of code. */
6936 as_warn (_("stand-alone `%s' prefix"), t->name);
6937 }
6938 }
6939
6940 /* Copy the template we found. */
9a182d04 6941 install_template (t);
539e75ad
L
6942
6943 if (addr_prefix_disp != -1)
6944 i.tm.operand_types[addr_prefix_disp]
6945 = operand_types[addr_prefix_disp];
6946
8bd915b7 6947 switch (found_reverse_match)
29b0f896 6948 {
8bd915b7
JB
6949 case 0:
6950 break;
6951
6952 default:
dfd69174
JB
6953 /* If we found a reverse match we must alter the opcode direction
6954 bit and clear/flip the regmem modifier one. found_reverse_match
6955 holds bits to change (different for int & float insns). */
29b0f896
AM
6956
6957 i.tm.base_opcode ^= found_reverse_match;
6958
f5eb1d70
JB
6959 i.tm.operand_types[0] = operand_types[i.operands - 1];
6960 i.tm.operand_types[i.operands - 1] = operand_types[0];
dfd69174
JB
6961
6962 /* Certain SIMD insns have their load forms specified in the opcode
6963 table, and hence we need to _set_ RegMem instead of clearing it.
6964 We need to avoid setting the bit though on insns like KMOVW. */
6965 i.tm.opcode_modifier.regmem
6966 = i.tm.opcode_modifier.modrm && i.tm.opcode_modifier.d
6967 && i.tm.operands > 2U - i.tm.opcode_modifier.sse2avx
6968 && !i.tm.opcode_modifier.regmem;
8bd915b7
JB
6969 break;
6970
6971 case Opcode_VexW:
6972 /* Only the first two register operands need reversing, alongside
6973 flipping VEX.W. */
6974 i.tm.opcode_modifier.vexw ^= VEXW0 ^ VEXW1;
6975
6976 j = i.tm.operand_types[0].bitfield.imm8;
6977 i.tm.operand_types[j] = operand_types[j + 1];
6978 i.tm.operand_types[j + 1] = operand_types[j];
6979 break;
29b0f896
AM
6980 }
6981
fa99fab2 6982 return t;
29b0f896
AM
6983}
6984
6985static int
e3bb37b5 6986check_string (void)
29b0f896 6987{
51c8edf6
JB
6988 unsigned int es_op = i.tm.opcode_modifier.isstring - IS_STRING_ES_OP0;
6989 unsigned int op = i.tm.operand_types[0].bitfield.baseindex ? es_op : 0;
8dc0818e 6990
5e042380 6991 if (i.seg[op] != NULL && i.seg[op] != reg_es)
29b0f896 6992 {
51c8edf6
JB
6993 as_bad (_("`%s' operand %u must use `%ses' segment"),
6994 i.tm.name,
6995 intel_syntax ? i.tm.operands - es_op : es_op + 1,
6996 register_prefix);
6997 return 0;
29b0f896 6998 }
51c8edf6
JB
6999
7000 /* There's only ever one segment override allowed per instruction.
7001 This instruction possibly has a legal segment override on the
7002 second operand, so copy the segment to where non-string
7003 instructions store it, allowing common code. */
7004 i.seg[op] = i.seg[1];
7005
29b0f896
AM
7006 return 1;
7007}
7008
7009static int
543613e9 7010process_suffix (void)
29b0f896 7011{
5b7c81bd 7012 bool is_crc32 = false, is_movx = false;
8b65b895 7013
29b0f896
AM
7014 /* If matched instruction specifies an explicit instruction mnemonic
7015 suffix, use it. */
673fe0f0 7016 if (i.tm.opcode_modifier.size == SIZE16)
40fb9820 7017 i.suffix = WORD_MNEM_SUFFIX;
673fe0f0 7018 else if (i.tm.opcode_modifier.size == SIZE32)
40fb9820 7019 i.suffix = LONG_MNEM_SUFFIX;
673fe0f0 7020 else if (i.tm.opcode_modifier.size == SIZE64)
40fb9820 7021 i.suffix = QWORD_MNEM_SUFFIX;
13e600d0 7022 else if (i.reg_operands
c8f8eebc
JB
7023 && (i.operands > 1 || i.types[0].bitfield.class == Reg)
7024 && !i.tm.opcode_modifier.addrprefixopreg)
29b0f896 7025 {
65fca059 7026 unsigned int numop = i.operands;
389d00a5
JB
7027
7028 /* MOVSX/MOVZX */
7029 is_movx = (i.tm.opcode_modifier.opcodespace == SPACE_0F
7030 && (i.tm.base_opcode | 8) == 0xbe)
7031 || (i.tm.opcode_modifier.opcodespace == SPACE_BASE
7032 && i.tm.base_opcode == 0x63
7033 && i.tm.cpu_flags.bitfield.cpu64);
7034
8b65b895 7035 /* CRC32 */
389d00a5
JB
7036 is_crc32 = (i.tm.base_opcode == 0xf0
7037 && i.tm.opcode_modifier.opcodespace == SPACE_0F38
8b65b895 7038 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2);
65fca059
JB
7039
7040 /* movsx/movzx want only their source operand considered here, for the
7041 ambiguity checking below. The suffix will be replaced afterwards
7042 to represent the destination (register). */
389d00a5 7043 if (is_movx && (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63))
65fca059
JB
7044 --i.operands;
7045
643bb870 7046 /* crc32 needs REX.W set regardless of suffix / source operand size. */
8b65b895 7047 if (is_crc32 && i.tm.operand_types[1].bitfield.qword)
643bb870
JB
7048 i.rex |= REX_W;
7049
29b0f896 7050 /* If there's no instruction mnemonic suffix we try to invent one
13e600d0 7051 based on GPR operands. */
29b0f896
AM
7052 if (!i.suffix)
7053 {
7054 /* We take i.suffix from the last register operand specified,
7055 Destination register type is more significant than source
381d071f
L
7056 register type. crc32 in SSE4.2 prefers source register
7057 type. */
8b65b895 7058 unsigned int op = is_crc32 ? 1 : i.operands;
20592a94 7059
1a035124
JB
7060 while (op--)
7061 if (i.tm.operand_types[op].bitfield.instance == InstanceNone
7062 || i.tm.operand_types[op].bitfield.instance == Accum)
7063 {
7064 if (i.types[op].bitfield.class != Reg)
7065 continue;
7066 if (i.types[op].bitfield.byte)
7067 i.suffix = BYTE_MNEM_SUFFIX;
7068 else if (i.types[op].bitfield.word)
7069 i.suffix = WORD_MNEM_SUFFIX;
7070 else if (i.types[op].bitfield.dword)
7071 i.suffix = LONG_MNEM_SUFFIX;
7072 else if (i.types[op].bitfield.qword)
7073 i.suffix = QWORD_MNEM_SUFFIX;
7074 else
7075 continue;
7076 break;
7077 }
65fca059
JB
7078
7079 /* As an exception, movsx/movzx silently default to a byte source
7080 in AT&T mode. */
389d00a5 7081 if (is_movx && i.tm.opcode_modifier.w && !i.suffix && !intel_syntax)
65fca059 7082 i.suffix = BYTE_MNEM_SUFFIX;
29b0f896
AM
7083 }
7084 else if (i.suffix == BYTE_MNEM_SUFFIX)
7085 {
1cb0ab18 7086 if (!check_byte_reg ())
29b0f896
AM
7087 return 0;
7088 }
7089 else if (i.suffix == LONG_MNEM_SUFFIX)
7090 {
1cb0ab18 7091 if (!check_long_reg ())
29b0f896
AM
7092 return 0;
7093 }
7094 else if (i.suffix == QWORD_MNEM_SUFFIX)
7095 {
1cb0ab18 7096 if (!check_qword_reg ())
29b0f896
AM
7097 return 0;
7098 }
7099 else if (i.suffix == WORD_MNEM_SUFFIX)
7100 {
1cb0ab18 7101 if (!check_word_reg ())
29b0f896
AM
7102 return 0;
7103 }
3cd7f3e3
L
7104 else if (intel_syntax
7105 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
7106 /* Do nothing if the instruction is going to ignore the prefix. */
7107 ;
7108 else
7109 abort ();
65fca059
JB
7110
7111 /* Undo the movsx/movzx change done above. */
7112 i.operands = numop;
29b0f896 7113 }
3cd7f3e3
L
7114 else if (i.tm.opcode_modifier.mnemonicsize == DEFAULTSIZE
7115 && !i.suffix)
29b0f896 7116 {
13e600d0
JB
7117 i.suffix = stackop_size;
7118 if (stackop_size == LONG_MNEM_SUFFIX)
06f74c5c
L
7119 {
7120 /* stackop_size is set to LONG_MNEM_SUFFIX for the
7121 .code16gcc directive to support 16-bit mode with
7122 32-bit address. For IRET without a suffix, generate
7123 16-bit IRET (opcode 0xcf) to return from an interrupt
7124 handler. */
13e600d0
JB
7125 if (i.tm.base_opcode == 0xcf)
7126 {
7127 i.suffix = WORD_MNEM_SUFFIX;
7128 as_warn (_("generating 16-bit `iret' for .code16gcc directive"));
7129 }
7130 /* Warn about changed behavior for segment register push/pop. */
7131 else if ((i.tm.base_opcode | 1) == 0x07)
7132 as_warn (_("generating 32-bit `%s', unlike earlier gas versions"),
7133 i.tm.name);
06f74c5c 7134 }
29b0f896 7135 }
c006a730 7136 else if (!i.suffix
0cfa3eb3
JB
7137 && (i.tm.opcode_modifier.jump == JUMP_ABSOLUTE
7138 || i.tm.opcode_modifier.jump == JUMP_BYTE
7139 || i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT
389d00a5
JB
7140 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
7141 && i.tm.base_opcode == 0x01 /* [ls][gi]dt */
64e74474 7142 && i.tm.extension_opcode <= 3)))
9306ca4a
JB
7143 {
7144 switch (flag_code)
7145 {
7146 case CODE_64BIT:
40fb9820 7147 if (!i.tm.opcode_modifier.no_qsuf)
9306ca4a 7148 {
828c2a25
JB
7149 if (i.tm.opcode_modifier.jump == JUMP_BYTE
7150 || i.tm.opcode_modifier.no_lsuf)
7151 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a
JB
7152 break;
7153 }
1a0670f3 7154 /* Fall through. */
9306ca4a 7155 case CODE_32BIT:
40fb9820 7156 if (!i.tm.opcode_modifier.no_lsuf)
9306ca4a
JB
7157 i.suffix = LONG_MNEM_SUFFIX;
7158 break;
7159 case CODE_16BIT:
40fb9820 7160 if (!i.tm.opcode_modifier.no_wsuf)
9306ca4a
JB
7161 i.suffix = WORD_MNEM_SUFFIX;
7162 break;
7163 }
7164 }
252b5132 7165
c006a730 7166 if (!i.suffix
3cd7f3e3 7167 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8
JB
7168 /* Also cover lret/retf/iret in 64-bit mode. */
7169 || (flag_code == CODE_64BIT
7170 && !i.tm.opcode_modifier.no_lsuf
7171 && !i.tm.opcode_modifier.no_qsuf))
3cd7f3e3 7172 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
8bbb3ad8
JB
7173 /* Explicit sizing prefixes are assumed to disambiguate insns. */
7174 && !i.prefix[DATA_PREFIX] && !(i.prefix[REX_PREFIX] & REX_W)
62b3f548
JB
7175 /* Accept FLDENV et al without suffix. */
7176 && (i.tm.opcode_modifier.no_ssuf || i.tm.opcode_modifier.floatmf))
29b0f896 7177 {
6c0946d0 7178 unsigned int suffixes, evex = 0;
c006a730
JB
7179
7180 suffixes = !i.tm.opcode_modifier.no_bsuf;
7181 if (!i.tm.opcode_modifier.no_wsuf)
7182 suffixes |= 1 << 1;
7183 if (!i.tm.opcode_modifier.no_lsuf)
7184 suffixes |= 1 << 2;
7185 if (!i.tm.opcode_modifier.no_ldsuf)
7186 suffixes |= 1 << 3;
7187 if (!i.tm.opcode_modifier.no_ssuf)
7188 suffixes |= 1 << 4;
7189 if (flag_code == CODE_64BIT && !i.tm.opcode_modifier.no_qsuf)
7190 suffixes |= 1 << 5;
7191
6c0946d0
JB
7192 /* For [XYZ]MMWORD operands inspect operand sizes. While generally
7193 also suitable for AT&T syntax mode, it was requested that this be
7194 restricted to just Intel syntax. */
a5748e0d
JB
7195 if (intel_syntax && is_any_vex_encoding (&i.tm)
7196 && !i.broadcast.type && !i.broadcast.bytes)
6c0946d0 7197 {
b9915cbc 7198 unsigned int op;
6c0946d0 7199
b9915cbc 7200 for (op = 0; op < i.tm.operands; ++op)
6c0946d0 7201 {
b9915cbc
JB
7202 if (is_evex_encoding (&i.tm)
7203 && !cpu_arch_flags.bitfield.cpuavx512vl)
6c0946d0 7204 {
b9915cbc
JB
7205 if (i.tm.operand_types[op].bitfield.ymmword)
7206 i.tm.operand_types[op].bitfield.xmmword = 0;
7207 if (i.tm.operand_types[op].bitfield.zmmword)
7208 i.tm.operand_types[op].bitfield.ymmword = 0;
7209 if (!i.tm.opcode_modifier.evex
7210 || i.tm.opcode_modifier.evex == EVEXDYN)
7211 i.tm.opcode_modifier.evex = EVEX512;
7212 }
6c0946d0 7213
b9915cbc
JB
7214 if (i.tm.operand_types[op].bitfield.xmmword
7215 + i.tm.operand_types[op].bitfield.ymmword
7216 + i.tm.operand_types[op].bitfield.zmmword < 2)
7217 continue;
6c0946d0 7218
b9915cbc
JB
7219 /* Any properly sized operand disambiguates the insn. */
7220 if (i.types[op].bitfield.xmmword
7221 || i.types[op].bitfield.ymmword
7222 || i.types[op].bitfield.zmmword)
7223 {
7224 suffixes &= ~(7 << 6);
7225 evex = 0;
7226 break;
7227 }
6c0946d0 7228
b9915cbc
JB
7229 if ((i.flags[op] & Operand_Mem)
7230 && i.tm.operand_types[op].bitfield.unspecified)
7231 {
7232 if (i.tm.operand_types[op].bitfield.xmmword)
7233 suffixes |= 1 << 6;
7234 if (i.tm.operand_types[op].bitfield.ymmword)
7235 suffixes |= 1 << 7;
7236 if (i.tm.operand_types[op].bitfield.zmmword)
7237 suffixes |= 1 << 8;
7238 if (is_evex_encoding (&i.tm))
7239 evex = EVEX512;
6c0946d0
JB
7240 }
7241 }
7242 }
7243
7244 /* Are multiple suffixes / operand sizes allowed? */
c006a730 7245 if (suffixes & (suffixes - 1))
9306ca4a 7246 {
873494c8 7247 if (intel_syntax
3cd7f3e3 7248 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8 7249 || operand_check == check_error))
9306ca4a 7250 {
c006a730 7251 as_bad (_("ambiguous operand size for `%s'"), i.tm.name);
9306ca4a
JB
7252 return 0;
7253 }
c006a730 7254 if (operand_check == check_error)
9306ca4a 7255 {
c006a730
JB
7256 as_bad (_("no instruction mnemonic suffix given and "
7257 "no register operands; can't size `%s'"), i.tm.name);
9306ca4a
JB
7258 return 0;
7259 }
c006a730 7260 if (operand_check == check_warning)
873494c8
JB
7261 as_warn (_("%s; using default for `%s'"),
7262 intel_syntax
7263 ? _("ambiguous operand size")
7264 : _("no instruction mnemonic suffix given and "
7265 "no register operands"),
7266 i.tm.name);
c006a730
JB
7267
7268 if (i.tm.opcode_modifier.floatmf)
7269 i.suffix = SHORT_MNEM_SUFFIX;
389d00a5 7270 else if (is_movx)
65fca059 7271 /* handled below */;
6c0946d0
JB
7272 else if (evex)
7273 i.tm.opcode_modifier.evex = evex;
c006a730
JB
7274 else if (flag_code == CODE_16BIT)
7275 i.suffix = WORD_MNEM_SUFFIX;
1a035124 7276 else if (!i.tm.opcode_modifier.no_lsuf)
c006a730 7277 i.suffix = LONG_MNEM_SUFFIX;
1a035124
JB
7278 else
7279 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a 7280 }
29b0f896 7281 }
252b5132 7282
389d00a5 7283 if (is_movx)
65fca059
JB
7284 {
7285 /* In Intel syntax, movsx/movzx must have a "suffix" (checked above).
7286 In AT&T syntax, if there is no suffix (warned about above), the default
7287 will be byte extension. */
7288 if (i.tm.opcode_modifier.w && i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
7289 i.tm.base_opcode |= 1;
7290
7291 /* For further processing, the suffix should represent the destination
7292 (register). This is already the case when one was used with
7293 mov[sz][bw]*, but we need to replace it for mov[sz]x, or if there was
7294 no suffix to begin with. */
7295 if (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63 || !i.suffix)
7296 {
7297 if (i.types[1].bitfield.word)
7298 i.suffix = WORD_MNEM_SUFFIX;
7299 else if (i.types[1].bitfield.qword)
7300 i.suffix = QWORD_MNEM_SUFFIX;
7301 else
7302 i.suffix = LONG_MNEM_SUFFIX;
7303
7304 i.tm.opcode_modifier.w = 0;
7305 }
7306 }
7307
50128d0c
JB
7308 if (!i.tm.opcode_modifier.modrm && i.reg_operands && i.tm.operands < 3)
7309 i.short_form = (i.tm.operand_types[0].bitfield.class == Reg)
7310 != (i.tm.operand_types[1].bitfield.class == Reg);
7311
d2224064
JB
7312 /* Change the opcode based on the operand size given by i.suffix. */
7313 switch (i.suffix)
29b0f896 7314 {
d2224064
JB
7315 /* Size floating point instruction. */
7316 case LONG_MNEM_SUFFIX:
7317 if (i.tm.opcode_modifier.floatmf)
7318 {
7319 i.tm.base_opcode ^= 4;
7320 break;
7321 }
7322 /* fall through */
7323 case WORD_MNEM_SUFFIX:
7324 case QWORD_MNEM_SUFFIX:
29b0f896 7325 /* It's not a byte, select word/dword operation. */
40fb9820 7326 if (i.tm.opcode_modifier.w)
29b0f896 7327 {
50128d0c 7328 if (i.short_form)
29b0f896
AM
7329 i.tm.base_opcode |= 8;
7330 else
7331 i.tm.base_opcode |= 1;
7332 }
d2224064
JB
7333 /* fall through */
7334 case SHORT_MNEM_SUFFIX:
29b0f896
AM
7335 /* Now select between word & dword operations via the operand
7336 size prefix, except for instructions that will ignore this
7337 prefix anyway. */
c8f8eebc 7338 if (i.suffix != QWORD_MNEM_SUFFIX
3cd7f3e3 7339 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
c8f8eebc
JB
7340 && !i.tm.opcode_modifier.floatmf
7341 && !is_any_vex_encoding (&i.tm)
7342 && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
7343 || (flag_code == CODE_64BIT
7344 && i.tm.opcode_modifier.jump == JUMP_BYTE)))
24eab124
AM
7345 {
7346 unsigned int prefix = DATA_PREFIX_OPCODE;
543613e9 7347
0cfa3eb3 7348 if (i.tm.opcode_modifier.jump == JUMP_BYTE) /* jcxz, loop */
29b0f896 7349 prefix = ADDR_PREFIX_OPCODE;
252b5132 7350
29b0f896
AM
7351 if (!add_prefix (prefix))
7352 return 0;
24eab124 7353 }
252b5132 7354
29b0f896
AM
7355 /* Set mode64 for an operand. */
7356 if (i.suffix == QWORD_MNEM_SUFFIX
9146926a 7357 && flag_code == CODE_64BIT
d2224064 7358 && !i.tm.opcode_modifier.norex64
4ed21b58 7359 && !i.tm.opcode_modifier.vexw
46e883c5 7360 /* Special case for xchg %rax,%rax. It is NOP and doesn't
d2224064
JB
7361 need rex64. */
7362 && ! (i.operands == 2
7363 && i.tm.base_opcode == 0x90
7364 && i.tm.extension_opcode == None
75e5731b
JB
7365 && i.types[0].bitfield.instance == Accum
7366 && i.types[0].bitfield.qword
7367 && i.types[1].bitfield.instance == Accum
7368 && i.types[1].bitfield.qword))
d2224064 7369 i.rex |= REX_W;
3e73aa7c 7370
d2224064 7371 break;
8bbb3ad8
JB
7372
7373 case 0:
f9a6a8f0 7374 /* Select word/dword/qword operation with explicit data sizing prefix
8bbb3ad8
JB
7375 when there are no suitable register operands. */
7376 if (i.tm.opcode_modifier.w
7377 && (i.prefix[DATA_PREFIX] || (i.prefix[REX_PREFIX] & REX_W))
7378 && (!i.reg_operands
7379 || (i.reg_operands == 1
7380 /* ShiftCount */
7381 && (i.tm.operand_types[0].bitfield.instance == RegC
7382 /* InOutPortReg */
7383 || i.tm.operand_types[0].bitfield.instance == RegD
7384 || i.tm.operand_types[1].bitfield.instance == RegD
7385 /* CRC32 */
8b65b895 7386 || is_crc32))))
8bbb3ad8
JB
7387 i.tm.base_opcode |= 1;
7388 break;
29b0f896 7389 }
7ecd2f8b 7390
c8f8eebc 7391 if (i.tm.opcode_modifier.addrprefixopreg)
c0a30a9f 7392 {
c8f8eebc
JB
7393 gas_assert (!i.suffix);
7394 gas_assert (i.reg_operands);
c0a30a9f 7395
c8f8eebc
JB
7396 if (i.tm.operand_types[0].bitfield.instance == Accum
7397 || i.operands == 1)
7398 {
7399 /* The address size override prefix changes the size of the
7400 first operand. */
7401 if (flag_code == CODE_64BIT
7402 && i.op[0].regs->reg_type.bitfield.word)
7403 {
7404 as_bad (_("16-bit addressing unavailable for `%s'"),
7405 i.tm.name);
7406 return 0;
7407 }
7408
7409 if ((flag_code == CODE_32BIT
7410 ? i.op[0].regs->reg_type.bitfield.word
7411 : i.op[0].regs->reg_type.bitfield.dword)
7412 && !add_prefix (ADDR_PREFIX_OPCODE))
7413 return 0;
7414 }
c0a30a9f
L
7415 else
7416 {
c8f8eebc
JB
7417 /* Check invalid register operand when the address size override
7418 prefix changes the size of register operands. */
7419 unsigned int op;
7420 enum { need_word, need_dword, need_qword } need;
7421
27f13469 7422 /* Check the register operand for the address size prefix if
b3a3496f 7423 the memory operand has no real registers, like symbol, DISP
829f3fe1 7424 or bogus (x32-only) symbol(%rip) when symbol(%eip) is meant. */
27f13469
L
7425 if (i.mem_operands == 1
7426 && i.reg_operands == 1
7427 && i.operands == 2
27f13469 7428 && i.types[1].bitfield.class == Reg
b3a3496f
L
7429 && (flag_code == CODE_32BIT
7430 ? i.op[1].regs->reg_type.bitfield.word
7431 : i.op[1].regs->reg_type.bitfield.dword)
7432 && ((i.base_reg == NULL && i.index_reg == NULL)
829f3fe1
JB
7433#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
7434 || (x86_elf_abi == X86_64_X32_ABI
7435 && i.base_reg
b3a3496f
L
7436 && i.base_reg->reg_num == RegIP
7437 && i.base_reg->reg_type.bitfield.qword))
829f3fe1
JB
7438#else
7439 || 0)
7440#endif
27f13469
L
7441 && !add_prefix (ADDR_PREFIX_OPCODE))
7442 return 0;
7443
c8f8eebc
JB
7444 if (flag_code == CODE_32BIT)
7445 need = i.prefix[ADDR_PREFIX] ? need_word : need_dword;
7446 else if (i.prefix[ADDR_PREFIX])
c0a30a9f
L
7447 need = need_dword;
7448 else
7449 need = flag_code == CODE_64BIT ? need_qword : need_word;
c0a30a9f 7450
c8f8eebc
JB
7451 for (op = 0; op < i.operands; op++)
7452 {
7453 if (i.types[op].bitfield.class != Reg)
7454 continue;
7455
7456 switch (need)
7457 {
7458 case need_word:
7459 if (i.op[op].regs->reg_type.bitfield.word)
7460 continue;
7461 break;
7462 case need_dword:
7463 if (i.op[op].regs->reg_type.bitfield.dword)
7464 continue;
7465 break;
7466 case need_qword:
7467 if (i.op[op].regs->reg_type.bitfield.qword)
7468 continue;
7469 break;
7470 }
7471
7472 as_bad (_("invalid register operand size for `%s'"),
7473 i.tm.name);
7474 return 0;
7475 }
7476 }
c0a30a9f
L
7477 }
7478
29b0f896
AM
7479 return 1;
7480}
3e73aa7c 7481
29b0f896 7482static int
543613e9 7483check_byte_reg (void)
29b0f896
AM
7484{
7485 int op;
543613e9 7486
29b0f896
AM
7487 for (op = i.operands; --op >= 0;)
7488 {
dc821c5f 7489 /* Skip non-register operands. */
bab6aec1 7490 if (i.types[op].bitfield.class != Reg)
dc821c5f
JB
7491 continue;
7492
29b0f896
AM
7493 /* If this is an eight bit register, it's OK. If it's the 16 or
7494 32 bit version of an eight bit register, we will just use the
7495 low portion, and that's OK too. */
dc821c5f 7496 if (i.types[op].bitfield.byte)
29b0f896
AM
7497 continue;
7498
5a819eb9 7499 /* I/O port address operands are OK too. */
75e5731b
JB
7500 if (i.tm.operand_types[op].bitfield.instance == RegD
7501 && i.tm.operand_types[op].bitfield.word)
5a819eb9
JB
7502 continue;
7503
9706160a 7504 /* crc32 only wants its source operand checked here. */
389d00a5
JB
7505 if (i.tm.base_opcode == 0xf0
7506 && i.tm.opcode_modifier.opcodespace == SPACE_0F38
8b65b895
L
7507 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2
7508 && op != 0)
9344ff29
L
7509 continue;
7510
29b0f896 7511 /* Any other register is bad. */
73c76375
JB
7512 as_bad (_("`%s%s' not allowed with `%s%c'"),
7513 register_prefix, i.op[op].regs->reg_name,
7514 i.tm.name, i.suffix);
7515 return 0;
29b0f896
AM
7516 }
7517 return 1;
7518}
7519
7520static int
e3bb37b5 7521check_long_reg (void)
29b0f896
AM
7522{
7523 int op;
7524
7525 for (op = i.operands; --op >= 0;)
dc821c5f 7526 /* Skip non-register operands. */
bab6aec1 7527 if (i.types[op].bitfield.class != Reg)
dc821c5f 7528 continue;
29b0f896
AM
7529 /* Reject eight bit registers, except where the template requires
7530 them. (eg. movzb) */
dc821c5f 7531 else if (i.types[op].bitfield.byte
bab6aec1 7532 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7533 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7534 && (i.tm.operand_types[op].bitfield.word
7535 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7536 {
a540244d
L
7537 as_bad (_("`%s%s' not allowed with `%s%c'"),
7538 register_prefix,
29b0f896
AM
7539 i.op[op].regs->reg_name,
7540 i.tm.name,
7541 i.suffix);
7542 return 0;
7543 }
be4c5e58
L
7544 /* Error if the e prefix on a general reg is missing. */
7545 else if (i.types[op].bitfield.word
bab6aec1 7546 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7547 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7548 && i.tm.operand_types[op].bitfield.dword)
29b0f896 7549 {
be4c5e58
L
7550 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7551 register_prefix, i.op[op].regs->reg_name,
7552 i.suffix);
7553 return 0;
252b5132 7554 }
e4630f71 7555 /* Warn if the r prefix on a general reg is present. */
dc821c5f 7556 else if (i.types[op].bitfield.qword
bab6aec1 7557 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7558 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7559 && i.tm.operand_types[op].bitfield.dword)
252b5132 7560 {
1cb0ab18
JB
7561 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7562 register_prefix, i.op[op].regs->reg_name, i.suffix);
7563 return 0;
29b0f896
AM
7564 }
7565 return 1;
7566}
252b5132 7567
29b0f896 7568static int
e3bb37b5 7569check_qword_reg (void)
29b0f896
AM
7570{
7571 int op;
252b5132 7572
29b0f896 7573 for (op = i.operands; --op >= 0; )
dc821c5f 7574 /* Skip non-register operands. */
bab6aec1 7575 if (i.types[op].bitfield.class != Reg)
dc821c5f 7576 continue;
29b0f896
AM
7577 /* Reject eight bit registers, except where the template requires
7578 them. (eg. movzb) */
dc821c5f 7579 else if (i.types[op].bitfield.byte
bab6aec1 7580 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7581 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7582 && (i.tm.operand_types[op].bitfield.word
7583 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7584 {
a540244d
L
7585 as_bad (_("`%s%s' not allowed with `%s%c'"),
7586 register_prefix,
29b0f896
AM
7587 i.op[op].regs->reg_name,
7588 i.tm.name,
7589 i.suffix);
7590 return 0;
7591 }
e4630f71 7592 /* Warn if the r prefix on a general reg is missing. */
dc821c5f
JB
7593 else if ((i.types[op].bitfield.word
7594 || i.types[op].bitfield.dword)
bab6aec1 7595 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7596 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7597 && i.tm.operand_types[op].bitfield.qword)
29b0f896
AM
7598 {
7599 /* Prohibit these changes in the 64bit mode, since the
7600 lowering is more complicated. */
1cb0ab18
JB
7601 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7602 register_prefix, i.op[op].regs->reg_name, i.suffix);
7603 return 0;
252b5132 7604 }
29b0f896
AM
7605 return 1;
7606}
252b5132 7607
29b0f896 7608static int
e3bb37b5 7609check_word_reg (void)
29b0f896
AM
7610{
7611 int op;
7612 for (op = i.operands; --op >= 0;)
dc821c5f 7613 /* Skip non-register operands. */
bab6aec1 7614 if (i.types[op].bitfield.class != Reg)
dc821c5f 7615 continue;
29b0f896
AM
7616 /* Reject eight bit registers, except where the template requires
7617 them. (eg. movzb) */
dc821c5f 7618 else if (i.types[op].bitfield.byte
bab6aec1 7619 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7620 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7621 && (i.tm.operand_types[op].bitfield.word
7622 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7623 {
a540244d
L
7624 as_bad (_("`%s%s' not allowed with `%s%c'"),
7625 register_prefix,
29b0f896
AM
7626 i.op[op].regs->reg_name,
7627 i.tm.name,
7628 i.suffix);
7629 return 0;
7630 }
9706160a
JB
7631 /* Error if the e or r prefix on a general reg is present. */
7632 else if ((i.types[op].bitfield.dword
dc821c5f 7633 || i.types[op].bitfield.qword)
bab6aec1 7634 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7635 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7636 && i.tm.operand_types[op].bitfield.word)
252b5132 7637 {
9706160a
JB
7638 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7639 register_prefix, i.op[op].regs->reg_name,
7640 i.suffix);
7641 return 0;
29b0f896
AM
7642 }
7643 return 1;
7644}
252b5132 7645
29b0f896 7646static int
40fb9820 7647update_imm (unsigned int j)
29b0f896 7648{
bc0844ae 7649 i386_operand_type overlap = i.types[j];
40fb9820
L
7650 if ((overlap.bitfield.imm8
7651 || overlap.bitfield.imm8s
7652 || overlap.bitfield.imm16
7653 || overlap.bitfield.imm32
7654 || overlap.bitfield.imm32s
7655 || overlap.bitfield.imm64)
0dfbf9d7
L
7656 && !operand_type_equal (&overlap, &imm8)
7657 && !operand_type_equal (&overlap, &imm8s)
7658 && !operand_type_equal (&overlap, &imm16)
7659 && !operand_type_equal (&overlap, &imm32)
7660 && !operand_type_equal (&overlap, &imm32s)
7661 && !operand_type_equal (&overlap, &imm64))
29b0f896
AM
7662 {
7663 if (i.suffix)
7664 {
40fb9820
L
7665 i386_operand_type temp;
7666
0dfbf9d7 7667 operand_type_set (&temp, 0);
7ab9ffdd 7668 if (i.suffix == BYTE_MNEM_SUFFIX)
40fb9820
L
7669 {
7670 temp.bitfield.imm8 = overlap.bitfield.imm8;
7671 temp.bitfield.imm8s = overlap.bitfield.imm8s;
7672 }
7673 else if (i.suffix == WORD_MNEM_SUFFIX)
7674 temp.bitfield.imm16 = overlap.bitfield.imm16;
7675 else if (i.suffix == QWORD_MNEM_SUFFIX)
7676 {
7677 temp.bitfield.imm64 = overlap.bitfield.imm64;
7678 temp.bitfield.imm32s = overlap.bitfield.imm32s;
7679 }
7680 else
7681 temp.bitfield.imm32 = overlap.bitfield.imm32;
7682 overlap = temp;
29b0f896 7683 }
0dfbf9d7
L
7684 else if (operand_type_equal (&overlap, &imm16_32_32s)
7685 || operand_type_equal (&overlap, &imm16_32)
7686 || operand_type_equal (&overlap, &imm16_32s))
29b0f896 7687 {
40fb9820 7688 if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
65da13b5 7689 overlap = imm16;
40fb9820 7690 else
65da13b5 7691 overlap = imm32s;
29b0f896 7692 }
8bbb3ad8
JB
7693 else if (i.prefix[REX_PREFIX] & REX_W)
7694 overlap = operand_type_and (overlap, imm32s);
7695 else if (i.prefix[DATA_PREFIX])
7696 overlap = operand_type_and (overlap,
7697 flag_code != CODE_16BIT ? imm16 : imm32);
0dfbf9d7
L
7698 if (!operand_type_equal (&overlap, &imm8)
7699 && !operand_type_equal (&overlap, &imm8s)
7700 && !operand_type_equal (&overlap, &imm16)
7701 && !operand_type_equal (&overlap, &imm32)
7702 && !operand_type_equal (&overlap, &imm32s)
7703 && !operand_type_equal (&overlap, &imm64))
29b0f896 7704 {
4eed87de
AM
7705 as_bad (_("no instruction mnemonic suffix given; "
7706 "can't determine immediate size"));
29b0f896
AM
7707 return 0;
7708 }
7709 }
40fb9820 7710 i.types[j] = overlap;
29b0f896 7711
40fb9820
L
7712 return 1;
7713}
7714
7715static int
7716finalize_imm (void)
7717{
bc0844ae 7718 unsigned int j, n;
29b0f896 7719
bc0844ae
L
7720 /* Update the first 2 immediate operands. */
7721 n = i.operands > 2 ? 2 : i.operands;
7722 if (n)
7723 {
7724 for (j = 0; j < n; j++)
7725 if (update_imm (j) == 0)
7726 return 0;
40fb9820 7727
bc0844ae
L
7728 /* The 3rd operand can't be immediate operand. */
7729 gas_assert (operand_type_check (i.types[2], imm) == 0);
7730 }
29b0f896
AM
7731
7732 return 1;
7733}
7734
7735static int
e3bb37b5 7736process_operands (void)
29b0f896
AM
7737{
7738 /* Default segment register this instruction will use for memory
7739 accesses. 0 means unknown. This is only for optimizing out
7740 unnecessary segment overrides. */
5e042380 7741 const reg_entry *default_seg = NULL;
29b0f896 7742
a5aeccd9
JB
7743 if (i.tm.opcode_modifier.sse2avx)
7744 {
7745 /* Legacy encoded insns allow explicit REX prefixes, so these prefixes
7746 need converting. */
7747 i.rex |= i.prefix[REX_PREFIX] & (REX_W | REX_R | REX_X | REX_B);
7748 i.prefix[REX_PREFIX] = 0;
7749 i.rex_encoding = 0;
7750 }
c423d21a
JB
7751 /* ImmExt should be processed after SSE2AVX. */
7752 else if (i.tm.opcode_modifier.immext)
7753 process_immext ();
a5aeccd9 7754
2426c15f 7755 if (i.tm.opcode_modifier.sse2avx && i.tm.opcode_modifier.vexvvvv)
29b0f896 7756 {
91d6fa6a
NC
7757 unsigned int dupl = i.operands;
7758 unsigned int dest = dupl - 1;
9fcfb3d7
L
7759 unsigned int j;
7760
c0f3af97 7761 /* The destination must be an xmm register. */
9c2799c2 7762 gas_assert (i.reg_operands
91d6fa6a 7763 && MAX_OPERANDS > dupl
7ab9ffdd 7764 && operand_type_equal (&i.types[dest], &regxmm));
c0f3af97 7765
75e5731b 7766 if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 7767 && i.tm.operand_types[0].bitfield.xmmword)
e2ec9d29 7768 {
8cd7925b 7769 if (i.tm.opcode_modifier.vexsources == VEX3SOURCES)
c0f3af97
L
7770 {
7771 /* Keep xmm0 for instructions with VEX prefix and 3
7772 sources. */
75e5731b 7773 i.tm.operand_types[0].bitfield.instance = InstanceNone;
3528c362 7774 i.tm.operand_types[0].bitfield.class = RegSIMD;
c0f3af97
L
7775 goto duplicate;
7776 }
e2ec9d29 7777 else
c0f3af97
L
7778 {
7779 /* We remove the first xmm0 and keep the number of
7780 operands unchanged, which in fact duplicates the
7781 destination. */
7782 for (j = 1; j < i.operands; j++)
7783 {
7784 i.op[j - 1] = i.op[j];
7785 i.types[j - 1] = i.types[j];
7786 i.tm.operand_types[j - 1] = i.tm.operand_types[j];
8dc0818e 7787 i.flags[j - 1] = i.flags[j];
c0f3af97
L
7788 }
7789 }
7790 }
7791 else if (i.tm.opcode_modifier.implicit1stxmm0)
7ab9ffdd 7792 {
91d6fa6a 7793 gas_assert ((MAX_OPERANDS - 1) > dupl
8cd7925b
L
7794 && (i.tm.opcode_modifier.vexsources
7795 == VEX3SOURCES));
c0f3af97
L
7796
7797 /* Add the implicit xmm0 for instructions with VEX prefix
7798 and 3 sources. */
7799 for (j = i.operands; j > 0; j--)
7800 {
7801 i.op[j] = i.op[j - 1];
7802 i.types[j] = i.types[j - 1];
7803 i.tm.operand_types[j] = i.tm.operand_types[j - 1];
8dc0818e 7804 i.flags[j] = i.flags[j - 1];
c0f3af97
L
7805 }
7806 i.op[0].regs
629310ab 7807 = (const reg_entry *) str_hash_find (reg_hash, "xmm0");
7ab9ffdd 7808 i.types[0] = regxmm;
c0f3af97
L
7809 i.tm.operand_types[0] = regxmm;
7810
7811 i.operands += 2;
7812 i.reg_operands += 2;
7813 i.tm.operands += 2;
7814
91d6fa6a 7815 dupl++;
c0f3af97 7816 dest++;
91d6fa6a
NC
7817 i.op[dupl] = i.op[dest];
7818 i.types[dupl] = i.types[dest];
7819 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 7820 i.flags[dupl] = i.flags[dest];
e2ec9d29 7821 }
c0f3af97
L
7822 else
7823 {
dc1e8a47 7824 duplicate:
c0f3af97
L
7825 i.operands++;
7826 i.reg_operands++;
7827 i.tm.operands++;
7828
91d6fa6a
NC
7829 i.op[dupl] = i.op[dest];
7830 i.types[dupl] = i.types[dest];
7831 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 7832 i.flags[dupl] = i.flags[dest];
c0f3af97
L
7833 }
7834
7835 if (i.tm.opcode_modifier.immext)
7836 process_immext ();
7837 }
75e5731b 7838 else if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 7839 && i.tm.operand_types[0].bitfield.xmmword)
c0f3af97
L
7840 {
7841 unsigned int j;
7842
9fcfb3d7
L
7843 for (j = 1; j < i.operands; j++)
7844 {
7845 i.op[j - 1] = i.op[j];
7846 i.types[j - 1] = i.types[j];
7847
7848 /* We need to adjust fields in i.tm since they are used by
7849 build_modrm_byte. */
7850 i.tm.operand_types [j - 1] = i.tm.operand_types [j];
8dc0818e
JB
7851
7852 i.flags[j - 1] = i.flags[j];
9fcfb3d7
L
7853 }
7854
e2ec9d29
L
7855 i.operands--;
7856 i.reg_operands--;
e2ec9d29
L
7857 i.tm.operands--;
7858 }
920d2ddc
IT
7859 else if (i.tm.opcode_modifier.implicitquadgroup)
7860 {
a477a8c4
JB
7861 unsigned int regnum, first_reg_in_group, last_reg_in_group;
7862
920d2ddc 7863 /* The second operand must be {x,y,z}mmN, where N is a multiple of 4. */
3528c362 7864 gas_assert (i.operands >= 2 && i.types[1].bitfield.class == RegSIMD);
a477a8c4
JB
7865 regnum = register_number (i.op[1].regs);
7866 first_reg_in_group = regnum & ~3;
7867 last_reg_in_group = first_reg_in_group + 3;
7868 if (regnum != first_reg_in_group)
7869 as_warn (_("source register `%s%s' implicitly denotes"
7870 " `%s%.3s%u' to `%s%.3s%u' source group in `%s'"),
7871 register_prefix, i.op[1].regs->reg_name,
7872 register_prefix, i.op[1].regs->reg_name, first_reg_in_group,
7873 register_prefix, i.op[1].regs->reg_name, last_reg_in_group,
7874 i.tm.name);
7875 }
e2ec9d29
L
7876 else if (i.tm.opcode_modifier.regkludge)
7877 {
7878 /* The imul $imm, %reg instruction is converted into
7879 imul $imm, %reg, %reg, and the clr %reg instruction
7880 is converted into xor %reg, %reg. */
7881
7882 unsigned int first_reg_op;
7883
7884 if (operand_type_check (i.types[0], reg))
7885 first_reg_op = 0;
7886 else
7887 first_reg_op = 1;
7888 /* Pretend we saw the extra register operand. */
9c2799c2 7889 gas_assert (i.reg_operands == 1
7ab9ffdd 7890 && i.op[first_reg_op + 1].regs == 0);
e2ec9d29
L
7891 i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
7892 i.types[first_reg_op + 1] = i.types[first_reg_op];
7893 i.operands++;
7894 i.reg_operands++;
29b0f896
AM
7895 }
7896
85b80b0f 7897 if (i.tm.opcode_modifier.modrm)
29b0f896
AM
7898 {
7899 /* The opcode is completed (modulo i.tm.extension_opcode which
52271982
AM
7900 must be put into the modrm byte). Now, we make the modrm and
7901 index base bytes based on all the info we've collected. */
29b0f896
AM
7902
7903 default_seg = build_modrm_byte ();
7904 }
00cee14f 7905 else if (i.types[0].bitfield.class == SReg)
85b80b0f
JB
7906 {
7907 if (flag_code != CODE_64BIT
7908 ? i.tm.base_opcode == POP_SEG_SHORT
7909 && i.op[0].regs->reg_num == 1
389d00a5 7910 : (i.tm.base_opcode | 1) == (POP_SEG386_SHORT & 0xff)
85b80b0f
JB
7911 && i.op[0].regs->reg_num < 4)
7912 {
7913 as_bad (_("you can't `%s %s%s'"),
7914 i.tm.name, register_prefix, i.op[0].regs->reg_name);
7915 return 0;
7916 }
389d00a5
JB
7917 if (i.op[0].regs->reg_num > 3
7918 && i.tm.opcode_modifier.opcodespace == SPACE_BASE )
85b80b0f 7919 {
389d00a5
JB
7920 i.tm.base_opcode ^= (POP_SEG_SHORT ^ POP_SEG386_SHORT) & 0xff;
7921 i.tm.opcode_modifier.opcodespace = SPACE_0F;
85b80b0f
JB
7922 }
7923 i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
7924 }
389d00a5
JB
7925 else if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
7926 && (i.tm.base_opcode & ~3) == MOV_AX_DISP32)
29b0f896 7927 {
5e042380 7928 default_seg = reg_ds;
29b0f896 7929 }
40fb9820 7930 else if (i.tm.opcode_modifier.isstring)
29b0f896
AM
7931 {
7932 /* For the string instructions that allow a segment override
7933 on one of their operands, the default segment is ds. */
5e042380 7934 default_seg = reg_ds;
29b0f896 7935 }
50128d0c 7936 else if (i.short_form)
85b80b0f
JB
7937 {
7938 /* The register or float register operand is in operand
7939 0 or 1. */
bab6aec1 7940 unsigned int op = i.tm.operand_types[0].bitfield.class != Reg;
85b80b0f
JB
7941
7942 /* Register goes in low 3 bits of opcode. */
7943 i.tm.base_opcode |= i.op[op].regs->reg_num;
7944 if ((i.op[op].regs->reg_flags & RegRex) != 0)
7945 i.rex |= REX_B;
7946 if (!quiet_warnings && i.tm.opcode_modifier.ugh)
7947 {
7948 /* Warn about some common errors, but press on regardless.
7949 The first case can be generated by gcc (<= 2.8.1). */
7950 if (i.operands == 2)
7951 {
7952 /* Reversed arguments on faddp, fsubp, etc. */
7953 as_warn (_("translating to `%s %s%s,%s%s'"), i.tm.name,
7954 register_prefix, i.op[!intel_syntax].regs->reg_name,
7955 register_prefix, i.op[intel_syntax].regs->reg_name);
7956 }
7957 else
7958 {
7959 /* Extraneous `l' suffix on fp insn. */
7960 as_warn (_("translating to `%s %s%s'"), i.tm.name,
7961 register_prefix, i.op[0].regs->reg_name);
7962 }
7963 }
7964 }
29b0f896 7965
514a8bb0 7966 if ((i.seg[0] || i.prefix[SEG_PREFIX])
514a8bb0 7967 && i.tm.base_opcode == 0x8d /* lea */
35648716 7968 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
514a8bb0 7969 && !is_any_vex_encoding(&i.tm))
92334ad2
JB
7970 {
7971 if (!quiet_warnings)
7972 as_warn (_("segment override on `%s' is ineffectual"), i.tm.name);
7973 if (optimize)
7974 {
7975 i.seg[0] = NULL;
7976 i.prefix[SEG_PREFIX] = 0;
7977 }
7978 }
52271982
AM
7979
7980 /* If a segment was explicitly specified, and the specified segment
b6773884
JB
7981 is neither the default nor the one already recorded from a prefix,
7982 use an opcode prefix to select it. If we never figured out what
7983 the default segment is, then default_seg will be zero at this
7984 point, and the specified segment prefix will always be used. */
7985 if (i.seg[0]
7986 && i.seg[0] != default_seg
5e042380 7987 && i386_seg_prefixes[i.seg[0]->reg_num] != i.prefix[SEG_PREFIX])
29b0f896 7988 {
5e042380 7989 if (!add_prefix (i386_seg_prefixes[i.seg[0]->reg_num]))
29b0f896
AM
7990 return 0;
7991 }
7992 return 1;
7993}
7994
a5aeccd9 7995static INLINE void set_rex_vrex (const reg_entry *r, unsigned int rex_bit,
5b7c81bd 7996 bool do_sse2avx)
a5aeccd9
JB
7997{
7998 if (r->reg_flags & RegRex)
7999 {
8000 if (i.rex & rex_bit)
8001 as_bad (_("same type of prefix used twice"));
8002 i.rex |= rex_bit;
8003 }
8004 else if (do_sse2avx && (i.rex & rex_bit) && i.vex.register_specifier)
8005 {
8006 gas_assert (i.vex.register_specifier == r);
8007 i.vex.register_specifier += 8;
8008 }
8009
8010 if (r->reg_flags & RegVRex)
8011 i.vrex |= rex_bit;
8012}
8013
5e042380 8014static const reg_entry *
e3bb37b5 8015build_modrm_byte (void)
29b0f896 8016{
5e042380 8017 const reg_entry *default_seg = NULL;
c0f3af97 8018 unsigned int source, dest;
8cd7925b 8019 int vex_3_sources;
c0f3af97 8020
8cd7925b 8021 vex_3_sources = i.tm.opcode_modifier.vexsources == VEX3SOURCES;
c0f3af97
L
8022 if (vex_3_sources)
8023 {
91d6fa6a 8024 unsigned int nds, reg_slot;
4c2c6516 8025 expressionS *exp;
c0f3af97 8026
6b8d3588 8027 dest = i.operands - 1;
c0f3af97 8028 nds = dest - 1;
922d8de8 8029
a683cc34 8030 /* There are 2 kinds of instructions:
bed3d976 8031 1. 5 operands: 4 register operands or 3 register operands
9d3bf266 8032 plus 1 memory operand plus one Imm4 operand, VexXDS, and
bed3d976 8033 VexW0 or VexW1. The destination must be either XMM, YMM or
43234a1e 8034 ZMM register.
bed3d976 8035 2. 4 operands: 4 register operands or 3 register operands
2f1bada2 8036 plus 1 memory operand, with VexXDS. */
922d8de8 8037 gas_assert ((i.reg_operands == 4
bed3d976
JB
8038 || (i.reg_operands == 3 && i.mem_operands == 1))
8039 && i.tm.opcode_modifier.vexvvvv == VEXXDS
dcd7e323 8040 && i.tm.opcode_modifier.vexw
3528c362 8041 && i.tm.operand_types[dest].bitfield.class == RegSIMD);
a683cc34 8042
48db9223
JB
8043 /* If VexW1 is set, the first non-immediate operand is the source and
8044 the second non-immediate one is encoded in the immediate operand. */
8045 if (i.tm.opcode_modifier.vexw == VEXW1)
8046 {
8047 source = i.imm_operands;
8048 reg_slot = i.imm_operands + 1;
8049 }
8050 else
8051 {
8052 source = i.imm_operands + 1;
8053 reg_slot = i.imm_operands;
8054 }
8055
a683cc34 8056 if (i.imm_operands == 0)
bed3d976
JB
8057 {
8058 /* When there is no immediate operand, generate an 8bit
8059 immediate operand to encode the first operand. */
8060 exp = &im_expressions[i.imm_operands++];
8061 i.op[i.operands].imms = exp;
8062 i.types[i.operands] = imm8;
8063 i.operands++;
8064
3528c362 8065 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
bed3d976
JB
8066 exp->X_op = O_constant;
8067 exp->X_add_number = register_number (i.op[reg_slot].regs) << 4;
43234a1e
L
8068 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
8069 }
922d8de8 8070 else
bed3d976 8071 {
9d3bf266
JB
8072 gas_assert (i.imm_operands == 1);
8073 gas_assert (fits_in_imm4 (i.op[0].imms->X_add_number));
8074 gas_assert (!i.tm.opcode_modifier.immext);
a683cc34 8075
9d3bf266
JB
8076 /* Turn on Imm8 again so that output_imm will generate it. */
8077 i.types[0].bitfield.imm8 = 1;
bed3d976 8078
3528c362 8079 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
9d3bf266 8080 i.op[0].imms->X_add_number
bed3d976 8081 |= register_number (i.op[reg_slot].regs) << 4;
43234a1e 8082 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
bed3d976 8083 }
a683cc34 8084
3528c362 8085 gas_assert (i.tm.operand_types[nds].bitfield.class == RegSIMD);
dae39acc 8086 i.vex.register_specifier = i.op[nds].regs;
c0f3af97
L
8087 }
8088 else
8089 source = dest = 0;
29b0f896
AM
8090
8091 /* i.reg_operands MUST be the number of real register operands;
c0f3af97
L
8092 implicit registers do not count. If there are 3 register
8093 operands, it must be a instruction with VexNDS. For a
8094 instruction with VexNDD, the destination register is encoded
8095 in VEX prefix. If there are 4 register operands, it must be
8096 a instruction with VEX prefix and 3 sources. */
7ab9ffdd
L
8097 if (i.mem_operands == 0
8098 && ((i.reg_operands == 2
2426c15f 8099 && i.tm.opcode_modifier.vexvvvv <= VEXXDS)
7ab9ffdd 8100 || (i.reg_operands == 3
2426c15f 8101 && i.tm.opcode_modifier.vexvvvv == VEXXDS)
7ab9ffdd 8102 || (i.reg_operands == 4 && vex_3_sources)))
29b0f896 8103 {
cab737b9
L
8104 switch (i.operands)
8105 {
8106 case 2:
8107 source = 0;
8108 break;
8109 case 3:
c81128dc
L
8110 /* When there are 3 operands, one of them may be immediate,
8111 which may be the first or the last operand. Otherwise,
c0f3af97
L
8112 the first operand must be shift count register (cl) or it
8113 is an instruction with VexNDS. */
9c2799c2 8114 gas_assert (i.imm_operands == 1
7ab9ffdd 8115 || (i.imm_operands == 0
2426c15f 8116 && (i.tm.opcode_modifier.vexvvvv == VEXXDS
75e5731b
JB
8117 || (i.types[0].bitfield.instance == RegC
8118 && i.types[0].bitfield.byte))));
40fb9820 8119 if (operand_type_check (i.types[0], imm)
75e5731b
JB
8120 || (i.types[0].bitfield.instance == RegC
8121 && i.types[0].bitfield.byte))
40fb9820
L
8122 source = 1;
8123 else
8124 source = 0;
cab737b9
L
8125 break;
8126 case 4:
368d64cc
L
8127 /* When there are 4 operands, the first two must be 8bit
8128 immediate operands. The source operand will be the 3rd
c0f3af97
L
8129 one.
8130
8131 For instructions with VexNDS, if the first operand
8132 an imm8, the source operand is the 2nd one. If the last
8133 operand is imm8, the source operand is the first one. */
9c2799c2 8134 gas_assert ((i.imm_operands == 2
7ab9ffdd
L
8135 && i.types[0].bitfield.imm8
8136 && i.types[1].bitfield.imm8)
2426c15f 8137 || (i.tm.opcode_modifier.vexvvvv == VEXXDS
7ab9ffdd
L
8138 && i.imm_operands == 1
8139 && (i.types[0].bitfield.imm8
cf665fee 8140 || i.types[i.operands - 1].bitfield.imm8)));
9f2670f2
L
8141 if (i.imm_operands == 2)
8142 source = 2;
8143 else
c0f3af97
L
8144 {
8145 if (i.types[0].bitfield.imm8)
8146 source = 1;
8147 else
8148 source = 0;
8149 }
c0f3af97
L
8150 break;
8151 case 5:
cf665fee
JB
8152 gas_assert (!is_evex_encoding (&i.tm));
8153 gas_assert (i.imm_operands == 1 && vex_3_sources);
cab737b9
L
8154 break;
8155 default:
8156 abort ();
8157 }
8158
c0f3af97
L
8159 if (!vex_3_sources)
8160 {
8161 dest = source + 1;
8162
2426c15f 8163 if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
c0f3af97 8164 {
43234a1e 8165 /* For instructions with VexNDS, the register-only source
c5d0745b 8166 operand must be a 32/64bit integer, XMM, YMM, ZMM, or mask
dfd69174 8167 register. It is encoded in VEX prefix. */
f12dc422
L
8168
8169 i386_operand_type op;
8170 unsigned int vvvv;
8171
c2ecccb3
L
8172 /* Swap two source operands if needed. */
8173 if (i.tm.opcode_modifier.swapsources)
f12dc422
L
8174 {
8175 vvvv = source;
8176 source = dest;
8177 }
8178 else
8179 vvvv = dest;
8180
8181 op = i.tm.operand_types[vvvv];
c0f3af97 8182 if ((dest + 1) >= i.operands
bab6aec1 8183 || ((op.bitfield.class != Reg
dc821c5f 8184 || (!op.bitfield.dword && !op.bitfield.qword))
3528c362 8185 && op.bitfield.class != RegSIMD
c0f327b8 8186 && op.bitfield.class != RegMask))
c0f3af97 8187 abort ();
f12dc422 8188 i.vex.register_specifier = i.op[vvvv].regs;
c0f3af97
L
8189 dest++;
8190 }
8191 }
29b0f896
AM
8192
8193 i.rm.mode = 3;
dfd69174
JB
8194 /* One of the register operands will be encoded in the i.rm.reg
8195 field, the other in the combined i.rm.mode and i.rm.regmem
29b0f896
AM
8196 fields. If no form of this instruction supports a memory
8197 destination operand, then we assume the source operand may
8198 sometimes be a memory operand and so we need to store the
8199 destination in the i.rm.reg field. */
dfd69174 8200 if (!i.tm.opcode_modifier.regmem
40fb9820 8201 && operand_type_check (i.tm.operand_types[dest], anymem) == 0)
29b0f896
AM
8202 {
8203 i.rm.reg = i.op[dest].regs->reg_num;
8204 i.rm.regmem = i.op[source].regs->reg_num;
a5aeccd9 8205 set_rex_vrex (i.op[dest].regs, REX_R, i.tm.opcode_modifier.sse2avx);
5b7c81bd 8206 set_rex_vrex (i.op[source].regs, REX_B, false);
29b0f896
AM
8207 }
8208 else
8209 {
8210 i.rm.reg = i.op[source].regs->reg_num;
8211 i.rm.regmem = i.op[dest].regs->reg_num;
a5aeccd9 8212 set_rex_vrex (i.op[dest].regs, REX_B, i.tm.opcode_modifier.sse2avx);
5b7c81bd 8213 set_rex_vrex (i.op[source].regs, REX_R, false);
29b0f896 8214 }
e0c7f900 8215 if (flag_code != CODE_64BIT && (i.rex & REX_R))
c4a530c5 8216 {
4a5c67ed 8217 if (i.types[!i.tm.opcode_modifier.regmem].bitfield.class != RegCR)
c4a530c5 8218 abort ();
e0c7f900 8219 i.rex &= ~REX_R;
c4a530c5
JB
8220 add_prefix (LOCK_PREFIX_OPCODE);
8221 }
29b0f896
AM
8222 }
8223 else
8224 { /* If it's not 2 reg operands... */
c0f3af97
L
8225 unsigned int mem;
8226
29b0f896
AM
8227 if (i.mem_operands)
8228 {
8229 unsigned int fake_zero_displacement = 0;
99018f42 8230 unsigned int op;
4eed87de 8231
7ab9ffdd 8232 for (op = 0; op < i.operands; op++)
8dc0818e 8233 if (i.flags[op] & Operand_Mem)
7ab9ffdd 8234 break;
7ab9ffdd 8235 gas_assert (op < i.operands);
29b0f896 8236
63112cd6 8237 if (i.tm.opcode_modifier.sib)
6c30d220 8238 {
260cd341
LC
8239 /* The index register of VSIB shouldn't be RegIZ. */
8240 if (i.tm.opcode_modifier.sib != SIBMEM
8241 && i.index_reg->reg_num == RegIZ)
6c30d220
L
8242 abort ();
8243
8244 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8245 if (!i.base_reg)
8246 {
8247 i.sib.base = NO_BASE_REGISTER;
8248 i.sib.scale = i.log2_scale_factor;
2f2be86b 8249 i.types[op] = operand_type_and_not (i.types[op], anydisp);
a775efc8 8250 i.types[op].bitfield.disp32 = 1;
6c30d220 8251 }
260cd341
LC
8252
8253 /* Since the mandatory SIB always has index register, so
8254 the code logic remains unchanged. The non-mandatory SIB
8255 without index register is allowed and will be handled
8256 later. */
8257 if (i.index_reg)
8258 {
8259 if (i.index_reg->reg_num == RegIZ)
8260 i.sib.index = NO_INDEX_REGISTER;
8261 else
8262 i.sib.index = i.index_reg->reg_num;
5b7c81bd 8263 set_rex_vrex (i.index_reg, REX_X, false);
260cd341 8264 }
6c30d220
L
8265 }
8266
5e042380 8267 default_seg = reg_ds;
29b0f896
AM
8268
8269 if (i.base_reg == 0)
8270 {
8271 i.rm.mode = 0;
8272 if (!i.disp_operands)
9bb129e8 8273 fake_zero_displacement = 1;
29b0f896
AM
8274 if (i.index_reg == 0)
8275 {
260cd341
LC
8276 /* Both check for VSIB and mandatory non-vector SIB. */
8277 gas_assert (!i.tm.opcode_modifier.sib
8278 || i.tm.opcode_modifier.sib == SIBMEM);
29b0f896 8279 /* Operand is just <disp> */
2f2be86b 8280 i.types[op] = operand_type_and_not (i.types[op], anydisp);
20f0a1fc 8281 if (flag_code == CODE_64BIT)
29b0f896
AM
8282 {
8283 /* 64bit mode overwrites the 32bit absolute
8284 addressing by RIP relative addressing and
8285 absolute addressing is encoded by one of the
8286 redundant SIB forms. */
8287 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8288 i.sib.base = NO_BASE_REGISTER;
8289 i.sib.index = NO_INDEX_REGISTER;
a775efc8 8290 i.types[op].bitfield.disp32 = 1;
20f0a1fc 8291 }
fc225355
L
8292 else if ((flag_code == CODE_16BIT)
8293 ^ (i.prefix[ADDR_PREFIX] != 0))
20f0a1fc
NC
8294 {
8295 i.rm.regmem = NO_BASE_REGISTER_16;
2f2be86b 8296 i.types[op].bitfield.disp16 = 1;
20f0a1fc
NC
8297 }
8298 else
8299 {
8300 i.rm.regmem = NO_BASE_REGISTER;
2f2be86b 8301 i.types[op].bitfield.disp32 = 1;
29b0f896
AM
8302 }
8303 }
63112cd6 8304 else if (!i.tm.opcode_modifier.sib)
29b0f896 8305 {
6c30d220 8306 /* !i.base_reg && i.index_reg */
e968fc9b 8307 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8308 i.sib.index = NO_INDEX_REGISTER;
8309 else
8310 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8311 i.sib.base = NO_BASE_REGISTER;
8312 i.sib.scale = i.log2_scale_factor;
8313 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
2f2be86b 8314 i.types[op] = operand_type_and_not (i.types[op], anydisp);
a775efc8 8315 i.types[op].bitfield.disp32 = 1;
29b0f896 8316 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8317 i.rex |= REX_X;
29b0f896
AM
8318 }
8319 }
8320 /* RIP addressing for 64bit mode. */
e968fc9b 8321 else if (i.base_reg->reg_num == RegIP)
29b0f896 8322 {
63112cd6 8323 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896 8324 i.rm.regmem = NO_BASE_REGISTER;
40fb9820
L
8325 i.types[op].bitfield.disp8 = 0;
8326 i.types[op].bitfield.disp16 = 0;
a775efc8 8327 i.types[op].bitfield.disp32 = 1;
40fb9820 8328 i.types[op].bitfield.disp64 = 0;
71903a11 8329 i.flags[op] |= Operand_PCrel;
20f0a1fc
NC
8330 if (! i.disp_operands)
8331 fake_zero_displacement = 1;
29b0f896 8332 }
dc821c5f 8333 else if (i.base_reg->reg_type.bitfield.word)
29b0f896 8334 {
63112cd6 8335 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896
AM
8336 switch (i.base_reg->reg_num)
8337 {
8338 case 3: /* (%bx) */
8339 if (i.index_reg == 0)
8340 i.rm.regmem = 7;
8341 else /* (%bx,%si) -> 0, or (%bx,%di) -> 1 */
8342 i.rm.regmem = i.index_reg->reg_num - 6;
8343 break;
8344 case 5: /* (%bp) */
5e042380 8345 default_seg = reg_ss;
29b0f896
AM
8346 if (i.index_reg == 0)
8347 {
8348 i.rm.regmem = 6;
40fb9820 8349 if (operand_type_check (i.types[op], disp) == 0)
29b0f896
AM
8350 {
8351 /* fake (%bp) into 0(%bp) */
41eb8e88 8352 if (i.disp_encoding == disp_encoding_16bit)
1a02d6b0
L
8353 i.types[op].bitfield.disp16 = 1;
8354 else
8355 i.types[op].bitfield.disp8 = 1;
252b5132 8356 fake_zero_displacement = 1;
29b0f896
AM
8357 }
8358 }
8359 else /* (%bp,%si) -> 2, or (%bp,%di) -> 3 */
8360 i.rm.regmem = i.index_reg->reg_num - 6 + 2;
8361 break;
8362 default: /* (%si) -> 4 or (%di) -> 5 */
8363 i.rm.regmem = i.base_reg->reg_num - 6 + 4;
8364 }
41eb8e88
L
8365 if (!fake_zero_displacement
8366 && !i.disp_operands
8367 && i.disp_encoding)
8368 {
8369 fake_zero_displacement = 1;
8370 if (i.disp_encoding == disp_encoding_8bit)
8371 i.types[op].bitfield.disp8 = 1;
8372 else
8373 i.types[op].bitfield.disp16 = 1;
8374 }
29b0f896
AM
8375 i.rm.mode = mode_from_disp_size (i.types[op]);
8376 }
8377 else /* i.base_reg and 32/64 bit mode */
8378 {
a9aabc23 8379 if (operand_type_check (i.types[op], disp))
40fb9820 8380 {
73053c1f
JB
8381 i.types[op].bitfield.disp16 = 0;
8382 i.types[op].bitfield.disp64 = 0;
a775efc8 8383 i.types[op].bitfield.disp32 = 1;
40fb9820 8384 }
20f0a1fc 8385
63112cd6 8386 if (!i.tm.opcode_modifier.sib)
6c30d220 8387 i.rm.regmem = i.base_reg->reg_num;
29b0f896 8388 if ((i.base_reg->reg_flags & RegRex) != 0)
161a04f6 8389 i.rex |= REX_B;
29b0f896
AM
8390 i.sib.base = i.base_reg->reg_num;
8391 /* x86-64 ignores REX prefix bit here to avoid decoder
8392 complications. */
848930b2
JB
8393 if (!(i.base_reg->reg_flags & RegRex)
8394 && (i.base_reg->reg_num == EBP_REG_NUM
8395 || i.base_reg->reg_num == ESP_REG_NUM))
5e042380 8396 default_seg = reg_ss;
848930b2 8397 if (i.base_reg->reg_num == 5 && i.disp_operands == 0)
29b0f896 8398 {
848930b2 8399 fake_zero_displacement = 1;
1a02d6b0
L
8400 if (i.disp_encoding == disp_encoding_32bit)
8401 i.types[op].bitfield.disp32 = 1;
8402 else
8403 i.types[op].bitfield.disp8 = 1;
29b0f896
AM
8404 }
8405 i.sib.scale = i.log2_scale_factor;
8406 if (i.index_reg == 0)
8407 {
260cd341
LC
8408 /* Only check for VSIB. */
8409 gas_assert (i.tm.opcode_modifier.sib != VECSIB128
8410 && i.tm.opcode_modifier.sib != VECSIB256
8411 && i.tm.opcode_modifier.sib != VECSIB512);
8412
29b0f896
AM
8413 /* <disp>(%esp) becomes two byte modrm with no index
8414 register. We've already stored the code for esp
8415 in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
8416 Any base register besides %esp will not use the
8417 extra modrm byte. */
8418 i.sib.index = NO_INDEX_REGISTER;
29b0f896 8419 }
63112cd6 8420 else if (!i.tm.opcode_modifier.sib)
29b0f896 8421 {
e968fc9b 8422 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8423 i.sib.index = NO_INDEX_REGISTER;
8424 else
8425 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8426 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8427 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8428 i.rex |= REX_X;
29b0f896 8429 }
67a4f2b7
AO
8430
8431 if (i.disp_operands
8432 && (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
8433 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL))
8434 i.rm.mode = 0;
8435 else
a501d77e
L
8436 {
8437 if (!fake_zero_displacement
8438 && !i.disp_operands
8439 && i.disp_encoding)
8440 {
8441 fake_zero_displacement = 1;
8442 if (i.disp_encoding == disp_encoding_8bit)
8443 i.types[op].bitfield.disp8 = 1;
8444 else
8445 i.types[op].bitfield.disp32 = 1;
8446 }
8447 i.rm.mode = mode_from_disp_size (i.types[op]);
8448 }
29b0f896 8449 }
252b5132 8450
29b0f896
AM
8451 if (fake_zero_displacement)
8452 {
8453 /* Fakes a zero displacement assuming that i.types[op]
8454 holds the correct displacement size. */
8455 expressionS *exp;
8456
9c2799c2 8457 gas_assert (i.op[op].disps == 0);
29b0f896
AM
8458 exp = &disp_expressions[i.disp_operands++];
8459 i.op[op].disps = exp;
8460 exp->X_op = O_constant;
8461 exp->X_add_number = 0;
8462 exp->X_add_symbol = (symbolS *) 0;
8463 exp->X_op_symbol = (symbolS *) 0;
8464 }
c0f3af97
L
8465
8466 mem = op;
29b0f896 8467 }
c0f3af97
L
8468 else
8469 mem = ~0;
252b5132 8470
8c43a48b 8471 if (i.tm.opcode_modifier.vexsources == XOP2SOURCES)
5dd85c99
SP
8472 {
8473 if (operand_type_check (i.types[0], imm))
8474 i.vex.register_specifier = NULL;
8475 else
8476 {
8477 /* VEX.vvvv encodes one of the sources when the first
8478 operand is not an immediate. */
1ef99a7b 8479 if (i.tm.opcode_modifier.vexw == VEXW0)
5dd85c99
SP
8480 i.vex.register_specifier = i.op[0].regs;
8481 else
8482 i.vex.register_specifier = i.op[1].regs;
8483 }
8484
8485 /* Destination is a XMM register encoded in the ModRM.reg
8486 and VEX.R bit. */
8487 i.rm.reg = i.op[2].regs->reg_num;
8488 if ((i.op[2].regs->reg_flags & RegRex) != 0)
8489 i.rex |= REX_R;
8490
8491 /* ModRM.rm and VEX.B encodes the other source. */
8492 if (!i.mem_operands)
8493 {
8494 i.rm.mode = 3;
8495
1ef99a7b 8496 if (i.tm.opcode_modifier.vexw == VEXW0)
5dd85c99
SP
8497 i.rm.regmem = i.op[1].regs->reg_num;
8498 else
8499 i.rm.regmem = i.op[0].regs->reg_num;
8500
8501 if ((i.op[1].regs->reg_flags & RegRex) != 0)
8502 i.rex |= REX_B;
8503 }
8504 }
2426c15f 8505 else if (i.tm.opcode_modifier.vexvvvv == VEXLWP)
f88c9eb0
SP
8506 {
8507 i.vex.register_specifier = i.op[2].regs;
8508 if (!i.mem_operands)
8509 {
8510 i.rm.mode = 3;
8511 i.rm.regmem = i.op[1].regs->reg_num;
8512 if ((i.op[1].regs->reg_flags & RegRex) != 0)
8513 i.rex |= REX_B;
8514 }
8515 }
29b0f896
AM
8516 /* Fill in i.rm.reg or i.rm.regmem field with register operand
8517 (if any) based on i.tm.extension_opcode. Again, we must be
8518 careful to make sure that segment/control/debug/test/MMX
8519 registers are coded into the i.rm.reg field. */
f88c9eb0 8520 else if (i.reg_operands)
29b0f896 8521 {
99018f42 8522 unsigned int op;
7ab9ffdd
L
8523 unsigned int vex_reg = ~0;
8524
8525 for (op = 0; op < i.operands; op++)
921eafea
L
8526 if (i.types[op].bitfield.class == Reg
8527 || i.types[op].bitfield.class == RegBND
8528 || i.types[op].bitfield.class == RegMask
8529 || i.types[op].bitfield.class == SReg
8530 || i.types[op].bitfield.class == RegCR
8531 || i.types[op].bitfield.class == RegDR
8532 || i.types[op].bitfield.class == RegTR
8533 || i.types[op].bitfield.class == RegSIMD
8534 || i.types[op].bitfield.class == RegMMX)
8535 break;
c0209578 8536
7ab9ffdd
L
8537 if (vex_3_sources)
8538 op = dest;
2426c15f 8539 else if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
7ab9ffdd
L
8540 {
8541 /* For instructions with VexNDS, the register-only
8542 source operand is encoded in VEX prefix. */
8543 gas_assert (mem != (unsigned int) ~0);
c0f3af97 8544
7ab9ffdd 8545 if (op > mem)
c0f3af97 8546 {
7ab9ffdd
L
8547 vex_reg = op++;
8548 gas_assert (op < i.operands);
c0f3af97
L
8549 }
8550 else
c0f3af97 8551 {
f12dc422
L
8552 /* Check register-only source operand when two source
8553 operands are swapped. */
8554 if (!i.tm.operand_types[op].bitfield.baseindex
8555 && i.tm.operand_types[op + 1].bitfield.baseindex)
8556 {
8557 vex_reg = op;
8558 op += 2;
8559 gas_assert (mem == (vex_reg + 1)
8560 && op < i.operands);
8561 }
8562 else
8563 {
8564 vex_reg = op + 1;
8565 gas_assert (vex_reg < i.operands);
8566 }
c0f3af97 8567 }
7ab9ffdd 8568 }
2426c15f 8569 else if (i.tm.opcode_modifier.vexvvvv == VEXNDD)
7ab9ffdd 8570 {
f12dc422 8571 /* For instructions with VexNDD, the register destination
7ab9ffdd 8572 is encoded in VEX prefix. */
f12dc422
L
8573 if (i.mem_operands == 0)
8574 {
8575 /* There is no memory operand. */
8576 gas_assert ((op + 2) == i.operands);
8577 vex_reg = op + 1;
8578 }
8579 else
8d63c93e 8580 {
ed438a93
JB
8581 /* There are only 2 non-immediate operands. */
8582 gas_assert (op < i.imm_operands + 2
8583 && i.operands == i.imm_operands + 2);
8584 vex_reg = i.imm_operands + 1;
f12dc422 8585 }
7ab9ffdd
L
8586 }
8587 else
8588 gas_assert (op < i.operands);
99018f42 8589
7ab9ffdd
L
8590 if (vex_reg != (unsigned int) ~0)
8591 {
f12dc422 8592 i386_operand_type *type = &i.tm.operand_types[vex_reg];
7ab9ffdd 8593
bab6aec1 8594 if ((type->bitfield.class != Reg
dc821c5f 8595 || (!type->bitfield.dword && !type->bitfield.qword))
3528c362 8596 && type->bitfield.class != RegSIMD
c0f327b8 8597 && type->bitfield.class != RegMask)
7ab9ffdd 8598 abort ();
f88c9eb0 8599
7ab9ffdd
L
8600 i.vex.register_specifier = i.op[vex_reg].regs;
8601 }
8602
1b9f0c97
L
8603 /* Don't set OP operand twice. */
8604 if (vex_reg != op)
7ab9ffdd 8605 {
1b9f0c97
L
8606 /* If there is an extension opcode to put here, the
8607 register number must be put into the regmem field. */
8608 if (i.tm.extension_opcode != None)
8609 {
8610 i.rm.regmem = i.op[op].regs->reg_num;
a5aeccd9
JB
8611 set_rex_vrex (i.op[op].regs, REX_B,
8612 i.tm.opcode_modifier.sse2avx);
1b9f0c97
L
8613 }
8614 else
8615 {
8616 i.rm.reg = i.op[op].regs->reg_num;
a5aeccd9
JB
8617 set_rex_vrex (i.op[op].regs, REX_R,
8618 i.tm.opcode_modifier.sse2avx);
1b9f0c97 8619 }
7ab9ffdd 8620 }
252b5132 8621
29b0f896
AM
8622 /* Now, if no memory operand has set i.rm.mode = 0, 1, 2 we
8623 must set it to 3 to indicate this is a register operand
8624 in the regmem field. */
8625 if (!i.mem_operands)
8626 i.rm.mode = 3;
8627 }
252b5132 8628
29b0f896 8629 /* Fill in i.rm.reg field with extension opcode (if any). */
c1e679ec 8630 if (i.tm.extension_opcode != None)
29b0f896
AM
8631 i.rm.reg = i.tm.extension_opcode;
8632 }
8633 return default_seg;
8634}
252b5132 8635
48ef937e
JB
8636static INLINE void
8637frag_opcode_byte (unsigned char byte)
8638{
8639 if (now_seg != absolute_section)
8640 FRAG_APPEND_1_CHAR (byte);
8641 else
8642 ++abs_section_offset;
8643}
8644
376cd056
JB
8645static unsigned int
8646flip_code16 (unsigned int code16)
8647{
8648 gas_assert (i.tm.operands == 1);
8649
8650 return !(i.prefix[REX_PREFIX] & REX_W)
8651 && (code16 ? i.tm.operand_types[0].bitfield.disp32
376cd056
JB
8652 : i.tm.operand_types[0].bitfield.disp16)
8653 ? CODE16 : 0;
8654}
8655
29b0f896 8656static void
e3bb37b5 8657output_branch (void)
29b0f896
AM
8658{
8659 char *p;
f8a5c266 8660 int size;
29b0f896
AM
8661 int code16;
8662 int prefix;
8663 relax_substateT subtype;
8664 symbolS *sym;
8665 offsetT off;
8666
48ef937e
JB
8667 if (now_seg == absolute_section)
8668 {
8669 as_bad (_("relaxable branches not supported in absolute section"));
8670 return;
8671 }
8672
f8a5c266 8673 code16 = flag_code == CODE_16BIT ? CODE16 : 0;
1a42a9fe 8674 size = i.disp_encoding > disp_encoding_8bit ? BIG : SMALL;
29b0f896
AM
8675
8676 prefix = 0;
8677 if (i.prefix[DATA_PREFIX] != 0)
252b5132 8678 {
29b0f896
AM
8679 prefix = 1;
8680 i.prefixes -= 1;
376cd056 8681 code16 ^= flip_code16(code16);
252b5132 8682 }
29b0f896
AM
8683 /* Pentium4 branch hints. */
8684 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8685 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
2f66722d 8686 {
29b0f896
AM
8687 prefix++;
8688 i.prefixes--;
8689 }
8690 if (i.prefix[REX_PREFIX] != 0)
8691 {
8692 prefix++;
8693 i.prefixes--;
2f66722d
AM
8694 }
8695
7e8b059b
L
8696 /* BND prefixed jump. */
8697 if (i.prefix[BND_PREFIX] != 0)
8698 {
6cb0a70e
JB
8699 prefix++;
8700 i.prefixes--;
7e8b059b
L
8701 }
8702
f2810fe0
JB
8703 if (i.prefixes != 0)
8704 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
29b0f896
AM
8705
8706 /* It's always a symbol; End frag & setup for relax.
8707 Make sure there is enough room in this frag for the largest
8708 instruction we may generate in md_convert_frag. This is 2
8709 bytes for the opcode and room for the prefix and largest
8710 displacement. */
8711 frag_grow (prefix + 2 + 4);
8712 /* Prefix and 1 opcode byte go in fr_fix. */
8713 p = frag_more (prefix + 1);
8714 if (i.prefix[DATA_PREFIX] != 0)
8715 *p++ = DATA_PREFIX_OPCODE;
8716 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
8717 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
8718 *p++ = i.prefix[SEG_PREFIX];
6cb0a70e
JB
8719 if (i.prefix[BND_PREFIX] != 0)
8720 *p++ = BND_PREFIX_OPCODE;
29b0f896
AM
8721 if (i.prefix[REX_PREFIX] != 0)
8722 *p++ = i.prefix[REX_PREFIX];
8723 *p = i.tm.base_opcode;
8724
8725 if ((unsigned char) *p == JUMP_PC_RELATIVE)
f8a5c266 8726 subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, size);
40fb9820 8727 else if (cpu_arch_flags.bitfield.cpui386)
f8a5c266 8728 subtype = ENCODE_RELAX_STATE (COND_JUMP, size);
29b0f896 8729 else
f8a5c266 8730 subtype = ENCODE_RELAX_STATE (COND_JUMP86, size);
29b0f896 8731 subtype |= code16;
3e73aa7c 8732
29b0f896
AM
8733 sym = i.op[0].disps->X_add_symbol;
8734 off = i.op[0].disps->X_add_number;
3e73aa7c 8735
29b0f896
AM
8736 if (i.op[0].disps->X_op != O_constant
8737 && i.op[0].disps->X_op != O_symbol)
3e73aa7c 8738 {
29b0f896
AM
8739 /* Handle complex expressions. */
8740 sym = make_expr_symbol (i.op[0].disps);
8741 off = 0;
8742 }
3e73aa7c 8743
1ef3994a
JB
8744 frag_now->tc_frag_data.code64 = flag_code == CODE_64BIT;
8745
29b0f896
AM
8746 /* 1 possible extra opcode + 4 byte displacement go in var part.
8747 Pass reloc in fr_var. */
d258b828 8748 frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
29b0f896 8749}
3e73aa7c 8750
bd7ab16b
L
8751#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8752/* Return TRUE iff PLT32 relocation should be used for branching to
8753 symbol S. */
8754
5b7c81bd 8755static bool
bd7ab16b
L
8756need_plt32_p (symbolS *s)
8757{
8758 /* PLT32 relocation is ELF only. */
8759 if (!IS_ELF)
5b7c81bd 8760 return false;
bd7ab16b 8761
a5def729
RO
8762#ifdef TE_SOLARIS
8763 /* Don't emit PLT32 relocation on Solaris: neither native linker nor
8764 krtld support it. */
5b7c81bd 8765 return false;
a5def729
RO
8766#endif
8767
bd7ab16b
L
8768 /* Since there is no need to prepare for PLT branch on x86-64, we
8769 can generate R_X86_64_PLT32, instead of R_X86_64_PC32, which can
8770 be used as a marker for 32-bit PC-relative branches. */
8771 if (!object_64bit)
5b7c81bd 8772 return false;
bd7ab16b 8773
44365e88 8774 if (s == NULL)
5b7c81bd 8775 return false;
44365e88 8776
bd7ab16b
L
8777 /* Weak or undefined symbol need PLT32 relocation. */
8778 if (S_IS_WEAK (s) || !S_IS_DEFINED (s))
5b7c81bd 8779 return true;
bd7ab16b
L
8780
8781 /* Non-global symbol doesn't need PLT32 relocation. */
8782 if (! S_IS_EXTERNAL (s))
5b7c81bd 8783 return false;
bd7ab16b
L
8784
8785 /* Other global symbols need PLT32 relocation. NB: Symbol with
8786 non-default visibilities are treated as normal global symbol
8787 so that PLT32 relocation can be used as a marker for 32-bit
8788 PC-relative branches. It is useful for linker relaxation. */
5b7c81bd 8789 return true;
bd7ab16b
L
8790}
8791#endif
8792
29b0f896 8793static void
e3bb37b5 8794output_jump (void)
29b0f896
AM
8795{
8796 char *p;
8797 int size;
3e02c1cc 8798 fixS *fixP;
bd7ab16b 8799 bfd_reloc_code_real_type jump_reloc = i.reloc[0];
29b0f896 8800
0cfa3eb3 8801 if (i.tm.opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
8802 {
8803 /* This is a loop or jecxz type instruction. */
8804 size = 1;
8805 if (i.prefix[ADDR_PREFIX] != 0)
8806 {
48ef937e 8807 frag_opcode_byte (ADDR_PREFIX_OPCODE);
29b0f896
AM
8808 i.prefixes -= 1;
8809 }
8810 /* Pentium4 branch hints. */
8811 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8812 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
8813 {
48ef937e 8814 frag_opcode_byte (i.prefix[SEG_PREFIX]);
29b0f896 8815 i.prefixes--;
3e73aa7c
JH
8816 }
8817 }
29b0f896
AM
8818 else
8819 {
8820 int code16;
3e73aa7c 8821
29b0f896
AM
8822 code16 = 0;
8823 if (flag_code == CODE_16BIT)
8824 code16 = CODE16;
3e73aa7c 8825
29b0f896
AM
8826 if (i.prefix[DATA_PREFIX] != 0)
8827 {
48ef937e 8828 frag_opcode_byte (DATA_PREFIX_OPCODE);
29b0f896 8829 i.prefixes -= 1;
376cd056 8830 code16 ^= flip_code16(code16);
29b0f896 8831 }
252b5132 8832
29b0f896
AM
8833 size = 4;
8834 if (code16)
8835 size = 2;
8836 }
9fcc94b6 8837
6cb0a70e
JB
8838 /* BND prefixed jump. */
8839 if (i.prefix[BND_PREFIX] != 0)
29b0f896 8840 {
48ef937e 8841 frag_opcode_byte (i.prefix[BND_PREFIX]);
29b0f896
AM
8842 i.prefixes -= 1;
8843 }
252b5132 8844
6cb0a70e 8845 if (i.prefix[REX_PREFIX] != 0)
7e8b059b 8846 {
48ef937e 8847 frag_opcode_byte (i.prefix[REX_PREFIX]);
7e8b059b
L
8848 i.prefixes -= 1;
8849 }
8850
f2810fe0
JB
8851 if (i.prefixes != 0)
8852 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
e0890092 8853
48ef937e
JB
8854 if (now_seg == absolute_section)
8855 {
9a182d04 8856 abs_section_offset += i.opcode_length + size;
48ef937e
JB
8857 return;
8858 }
8859
9a182d04
JB
8860 p = frag_more (i.opcode_length + size);
8861 switch (i.opcode_length)
42164a71
L
8862 {
8863 case 2:
8864 *p++ = i.tm.base_opcode >> 8;
1a0670f3 8865 /* Fall through. */
42164a71
L
8866 case 1:
8867 *p++ = i.tm.base_opcode;
8868 break;
8869 default:
8870 abort ();
8871 }
e0890092 8872
bd7ab16b 8873#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1ef3994a
JB
8874 if (flag_code == CODE_64BIT && size == 4
8875 && jump_reloc == NO_RELOC && i.op[0].disps->X_add_number == 0
bd7ab16b
L
8876 && need_plt32_p (i.op[0].disps->X_add_symbol))
8877 jump_reloc = BFD_RELOC_X86_64_PLT32;
8878#endif
8879
8880 jump_reloc = reloc (size, 1, 1, jump_reloc);
8881
3e02c1cc 8882 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
bd7ab16b 8883 i.op[0].disps, 1, jump_reloc);
3e02c1cc 8884
eb19308f
JB
8885 /* All jumps handled here are signed, but don't unconditionally use a
8886 signed limit check for 32 and 16 bit jumps as we want to allow wrap
8887 around at 4G (outside of 64-bit mode) and 64k (except for XBEGIN)
8888 respectively. */
8889 switch (size)
8890 {
8891 case 1:
8892 fixP->fx_signed = 1;
8893 break;
8894
8895 case 2:
8896 if (i.tm.base_opcode == 0xc7f8)
8897 fixP->fx_signed = 1;
8898 break;
8899
8900 case 4:
8901 if (flag_code == CODE_64BIT)
8902 fixP->fx_signed = 1;
8903 break;
8904 }
29b0f896 8905}
e0890092 8906
29b0f896 8907static void
e3bb37b5 8908output_interseg_jump (void)
29b0f896
AM
8909{
8910 char *p;
8911 int size;
8912 int prefix;
8913 int code16;
252b5132 8914
29b0f896
AM
8915 code16 = 0;
8916 if (flag_code == CODE_16BIT)
8917 code16 = CODE16;
a217f122 8918
29b0f896
AM
8919 prefix = 0;
8920 if (i.prefix[DATA_PREFIX] != 0)
8921 {
8922 prefix = 1;
8923 i.prefixes -= 1;
8924 code16 ^= CODE16;
8925 }
6cb0a70e
JB
8926
8927 gas_assert (!i.prefix[REX_PREFIX]);
252b5132 8928
29b0f896
AM
8929 size = 4;
8930 if (code16)
8931 size = 2;
252b5132 8932
f2810fe0
JB
8933 if (i.prefixes != 0)
8934 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
252b5132 8935
48ef937e
JB
8936 if (now_seg == absolute_section)
8937 {
8938 abs_section_offset += prefix + 1 + 2 + size;
8939 return;
8940 }
8941
29b0f896
AM
8942 /* 1 opcode; 2 segment; offset */
8943 p = frag_more (prefix + 1 + 2 + size);
3e73aa7c 8944
29b0f896
AM
8945 if (i.prefix[DATA_PREFIX] != 0)
8946 *p++ = DATA_PREFIX_OPCODE;
252b5132 8947
29b0f896
AM
8948 if (i.prefix[REX_PREFIX] != 0)
8949 *p++ = i.prefix[REX_PREFIX];
252b5132 8950
29b0f896
AM
8951 *p++ = i.tm.base_opcode;
8952 if (i.op[1].imms->X_op == O_constant)
8953 {
8954 offsetT n = i.op[1].imms->X_add_number;
252b5132 8955
29b0f896
AM
8956 if (size == 2
8957 && !fits_in_unsigned_word (n)
8958 && !fits_in_signed_word (n))
8959 {
8960 as_bad (_("16-bit jump out of range"));
8961 return;
8962 }
8963 md_number_to_chars (p, n, size);
8964 }
8965 else
8966 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
d258b828 8967 i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
6d96a594
C
8968
8969 p += size;
8970 if (i.op[0].imms->X_op == O_constant)
8971 md_number_to_chars (p, (valueT) i.op[0].imms->X_add_number, 2);
8972 else
8973 fix_new_exp (frag_now, p - frag_now->fr_literal, 2,
8974 i.op[0].imms, 0, reloc (2, 0, 0, i.reloc[0]));
29b0f896 8975}
a217f122 8976
b4a3a7b4
L
8977#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8978void
8979x86_cleanup (void)
8980{
8981 char *p;
8982 asection *seg = now_seg;
8983 subsegT subseg = now_subseg;
8984 asection *sec;
8985 unsigned int alignment, align_size_1;
8986 unsigned int isa_1_descsz, feature_2_descsz, descsz;
8987 unsigned int isa_1_descsz_raw, feature_2_descsz_raw;
8988 unsigned int padding;
8989
1273b2f8 8990 if (!IS_ELF || !x86_used_note)
b4a3a7b4
L
8991 return;
8992
b4a3a7b4
L
8993 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X86;
8994
8995 /* The .note.gnu.property section layout:
8996
8997 Field Length Contents
8998 ---- ---- ----
8999 n_namsz 4 4
9000 n_descsz 4 The note descriptor size
9001 n_type 4 NT_GNU_PROPERTY_TYPE_0
9002 n_name 4 "GNU"
9003 n_desc n_descsz The program property array
9004 .... .... ....
9005 */
9006
9007 /* Create the .note.gnu.property section. */
9008 sec = subseg_new (NOTE_GNU_PROPERTY_SECTION_NAME, 0);
fd361982 9009 bfd_set_section_flags (sec,
b4a3a7b4
L
9010 (SEC_ALLOC
9011 | SEC_LOAD
9012 | SEC_DATA
9013 | SEC_HAS_CONTENTS
9014 | SEC_READONLY));
9015
9016 if (get_elf_backend_data (stdoutput)->s->elfclass == ELFCLASS64)
9017 {
9018 align_size_1 = 7;
9019 alignment = 3;
9020 }
9021 else
9022 {
9023 align_size_1 = 3;
9024 alignment = 2;
9025 }
9026
fd361982 9027 bfd_set_section_alignment (sec, alignment);
b4a3a7b4
L
9028 elf_section_type (sec) = SHT_NOTE;
9029
1273b2f8
L
9030 /* GNU_PROPERTY_X86_ISA_1_USED: 4-byte type + 4-byte data size
9031 + 4-byte data */
9032 isa_1_descsz_raw = 4 + 4 + 4;
9033 /* Align GNU_PROPERTY_X86_ISA_1_USED. */
9034 isa_1_descsz = (isa_1_descsz_raw + align_size_1) & ~align_size_1;
b4a3a7b4
L
9035
9036 feature_2_descsz_raw = isa_1_descsz;
9037 /* GNU_PROPERTY_X86_FEATURE_2_USED: 4-byte type + 4-byte data size
1273b2f8 9038 + 4-byte data */
b4a3a7b4
L
9039 feature_2_descsz_raw += 4 + 4 + 4;
9040 /* Align GNU_PROPERTY_X86_FEATURE_2_USED. */
9041 feature_2_descsz = ((feature_2_descsz_raw + align_size_1)
9042 & ~align_size_1);
9043
9044 descsz = feature_2_descsz;
9045 /* Section size: n_namsz + n_descsz + n_type + n_name + n_descsz. */
9046 p = frag_more (4 + 4 + 4 + 4 + descsz);
9047
9048 /* Write n_namsz. */
9049 md_number_to_chars (p, (valueT) 4, 4);
9050
9051 /* Write n_descsz. */
9052 md_number_to_chars (p + 4, (valueT) descsz, 4);
9053
9054 /* Write n_type. */
9055 md_number_to_chars (p + 4 * 2, (valueT) NT_GNU_PROPERTY_TYPE_0, 4);
9056
9057 /* Write n_name. */
9058 memcpy (p + 4 * 3, "GNU", 4);
9059
1273b2f8
L
9060 /* Write 4-byte type. */
9061 md_number_to_chars (p + 4 * 4,
9062 (valueT) GNU_PROPERTY_X86_ISA_1_USED, 4);
b4a3a7b4 9063
1273b2f8
L
9064 /* Write 4-byte data size. */
9065 md_number_to_chars (p + 4 * 5, (valueT) 4, 4);
b4a3a7b4 9066
1273b2f8
L
9067 /* Write 4-byte data. */
9068 md_number_to_chars (p + 4 * 6, (valueT) x86_isa_1_used, 4);
b4a3a7b4 9069
1273b2f8
L
9070 /* Zero out paddings. */
9071 padding = isa_1_descsz - isa_1_descsz_raw;
9072 if (padding)
9073 memset (p + 4 * 7, 0, padding);
b4a3a7b4
L
9074
9075 /* Write 4-byte type. */
9076 md_number_to_chars (p + isa_1_descsz + 4 * 4,
9077 (valueT) GNU_PROPERTY_X86_FEATURE_2_USED, 4);
9078
9079 /* Write 4-byte data size. */
9080 md_number_to_chars (p + isa_1_descsz + 4 * 5, (valueT) 4, 4);
9081
9082 /* Write 4-byte data. */
9083 md_number_to_chars (p + isa_1_descsz + 4 * 6,
9084 (valueT) x86_feature_2_used, 4);
9085
9086 /* Zero out paddings. */
9087 padding = feature_2_descsz - feature_2_descsz_raw;
9088 if (padding)
9089 memset (p + isa_1_descsz + 4 * 7, 0, padding);
9090
9091 /* We probably can't restore the current segment, for there likely
9092 isn't one yet... */
9093 if (seg && subseg)
9094 subseg_set (seg, subseg);
9095}
9096#endif
9097
9c33702b
JB
9098static unsigned int
9099encoding_length (const fragS *start_frag, offsetT start_off,
9100 const char *frag_now_ptr)
9101{
9102 unsigned int len = 0;
9103
9104 if (start_frag != frag_now)
9105 {
9106 const fragS *fr = start_frag;
9107
9108 do {
9109 len += fr->fr_fix;
9110 fr = fr->fr_next;
9111 } while (fr && fr != frag_now);
9112 }
9113
9114 return len - start_off + (frag_now_ptr - frag_now->fr_literal);
9115}
9116
e379e5f3 9117/* Return 1 for test, and, cmp, add, sub, inc and dec which may
79d72f45
HL
9118 be macro-fused with conditional jumps.
9119 NB: If TEST/AND/CMP/ADD/SUB/INC/DEC is of RIP relative address,
9120 or is one of the following format:
9121
9122 cmp m, imm
9123 add m, imm
9124 sub m, imm
9125 test m, imm
9126 and m, imm
9127 inc m
9128 dec m
9129
9130 it is unfusible. */
e379e5f3
L
9131
9132static int
79d72f45 9133maybe_fused_with_jcc_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
9134{
9135 /* No RIP address. */
9136 if (i.base_reg && i.base_reg->reg_num == RegIP)
9137 return 0;
9138
389d00a5
JB
9139 /* No opcodes outside of base encoding space. */
9140 if (i.tm.opcode_modifier.opcodespace != SPACE_BASE)
e379e5f3
L
9141 return 0;
9142
79d72f45
HL
9143 /* add, sub without add/sub m, imm. */
9144 if (i.tm.base_opcode <= 5
e379e5f3
L
9145 || (i.tm.base_opcode >= 0x28 && i.tm.base_opcode <= 0x2d)
9146 || ((i.tm.base_opcode | 3) == 0x83
79d72f45 9147 && (i.tm.extension_opcode == 0x5
e379e5f3 9148 || i.tm.extension_opcode == 0x0)))
79d72f45
HL
9149 {
9150 *mf_cmp_p = mf_cmp_alu_cmp;
9151 return !(i.mem_operands && i.imm_operands);
9152 }
e379e5f3 9153
79d72f45
HL
9154 /* and without and m, imm. */
9155 if ((i.tm.base_opcode >= 0x20 && i.tm.base_opcode <= 0x25)
9156 || ((i.tm.base_opcode | 3) == 0x83
9157 && i.tm.extension_opcode == 0x4))
9158 {
9159 *mf_cmp_p = mf_cmp_test_and;
9160 return !(i.mem_operands && i.imm_operands);
9161 }
9162
9163 /* test without test m imm. */
e379e5f3
L
9164 if ((i.tm.base_opcode | 1) == 0x85
9165 || (i.tm.base_opcode | 1) == 0xa9
9166 || ((i.tm.base_opcode | 1) == 0xf7
79d72f45
HL
9167 && i.tm.extension_opcode == 0))
9168 {
9169 *mf_cmp_p = mf_cmp_test_and;
9170 return !(i.mem_operands && i.imm_operands);
9171 }
9172
9173 /* cmp without cmp m, imm. */
9174 if ((i.tm.base_opcode >= 0x38 && i.tm.base_opcode <= 0x3d)
e379e5f3
L
9175 || ((i.tm.base_opcode | 3) == 0x83
9176 && (i.tm.extension_opcode == 0x7)))
79d72f45
HL
9177 {
9178 *mf_cmp_p = mf_cmp_alu_cmp;
9179 return !(i.mem_operands && i.imm_operands);
9180 }
e379e5f3 9181
79d72f45 9182 /* inc, dec without inc/dec m. */
e379e5f3
L
9183 if ((i.tm.cpu_flags.bitfield.cpuno64
9184 && (i.tm.base_opcode | 0xf) == 0x4f)
9185 || ((i.tm.base_opcode | 1) == 0xff
9186 && i.tm.extension_opcode <= 0x1))
79d72f45
HL
9187 {
9188 *mf_cmp_p = mf_cmp_incdec;
9189 return !i.mem_operands;
9190 }
e379e5f3
L
9191
9192 return 0;
9193}
9194
9195/* Return 1 if a FUSED_JCC_PADDING frag should be generated. */
9196
9197static int
79d72f45 9198add_fused_jcc_padding_frag_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
9199{
9200 /* NB: Don't work with COND_JUMP86 without i386. */
9201 if (!align_branch_power
9202 || now_seg == absolute_section
9203 || !cpu_arch_flags.bitfield.cpui386
9204 || !(align_branch & align_branch_fused_bit))
9205 return 0;
9206
79d72f45 9207 if (maybe_fused_with_jcc_p (mf_cmp_p))
e379e5f3
L
9208 {
9209 if (last_insn.kind == last_insn_other
9210 || last_insn.seg != now_seg)
9211 return 1;
9212 if (flag_debug)
9213 as_warn_where (last_insn.file, last_insn.line,
9214 _("`%s` skips -malign-branch-boundary on `%s`"),
9215 last_insn.name, i.tm.name);
9216 }
9217
9218 return 0;
9219}
9220
9221/* Return 1 if a BRANCH_PREFIX frag should be generated. */
9222
9223static int
9224add_branch_prefix_frag_p (void)
9225{
9226 /* NB: Don't work with COND_JUMP86 without i386. Don't add prefix
9227 to PadLock instructions since they include prefixes in opcode. */
9228 if (!align_branch_power
9229 || !align_branch_prefix_size
9230 || now_seg == absolute_section
9231 || i.tm.cpu_flags.bitfield.cpupadlock
9232 || !cpu_arch_flags.bitfield.cpui386)
9233 return 0;
9234
9235 /* Don't add prefix if it is a prefix or there is no operand in case
9236 that segment prefix is special. */
9237 if (!i.operands || i.tm.opcode_modifier.isprefix)
9238 return 0;
9239
9240 if (last_insn.kind == last_insn_other
9241 || last_insn.seg != now_seg)
9242 return 1;
9243
9244 if (flag_debug)
9245 as_warn_where (last_insn.file, last_insn.line,
9246 _("`%s` skips -malign-branch-boundary on `%s`"),
9247 last_insn.name, i.tm.name);
9248
9249 return 0;
9250}
9251
9252/* Return 1 if a BRANCH_PADDING frag should be generated. */
9253
9254static int
79d72f45
HL
9255add_branch_padding_frag_p (enum align_branch_kind *branch_p,
9256 enum mf_jcc_kind *mf_jcc_p)
e379e5f3
L
9257{
9258 int add_padding;
9259
9260 /* NB: Don't work with COND_JUMP86 without i386. */
9261 if (!align_branch_power
9262 || now_seg == absolute_section
389d00a5
JB
9263 || !cpu_arch_flags.bitfield.cpui386
9264 || i.tm.opcode_modifier.opcodespace != SPACE_BASE)
e379e5f3
L
9265 return 0;
9266
9267 add_padding = 0;
9268
9269 /* Check for jcc and direct jmp. */
9270 if (i.tm.opcode_modifier.jump == JUMP)
9271 {
9272 if (i.tm.base_opcode == JUMP_PC_RELATIVE)
9273 {
9274 *branch_p = align_branch_jmp;
9275 add_padding = align_branch & align_branch_jmp_bit;
9276 }
9277 else
9278 {
79d72f45
HL
9279 /* Because J<cc> and JN<cc> share same group in macro-fusible table,
9280 igore the lowest bit. */
9281 *mf_jcc_p = (i.tm.base_opcode & 0x0e) >> 1;
e379e5f3
L
9282 *branch_p = align_branch_jcc;
9283 if ((align_branch & align_branch_jcc_bit))
9284 add_padding = 1;
9285 }
9286 }
e379e5f3
L
9287 else if ((i.tm.base_opcode | 1) == 0xc3)
9288 {
9289 /* Near ret. */
9290 *branch_p = align_branch_ret;
9291 if ((align_branch & align_branch_ret_bit))
9292 add_padding = 1;
9293 }
9294 else
9295 {
9296 /* Check for indirect jmp, direct and indirect calls. */
9297 if (i.tm.base_opcode == 0xe8)
9298 {
9299 /* Direct call. */
9300 *branch_p = align_branch_call;
9301 if ((align_branch & align_branch_call_bit))
9302 add_padding = 1;
9303 }
9304 else if (i.tm.base_opcode == 0xff
9305 && (i.tm.extension_opcode == 2
9306 || i.tm.extension_opcode == 4))
9307 {
9308 /* Indirect call and jmp. */
9309 *branch_p = align_branch_indirect;
9310 if ((align_branch & align_branch_indirect_bit))
9311 add_padding = 1;
9312 }
9313
9314 if (add_padding
9315 && i.disp_operands
9316 && tls_get_addr
9317 && (i.op[0].disps->X_op == O_symbol
9318 || (i.op[0].disps->X_op == O_subtract
9319 && i.op[0].disps->X_op_symbol == GOT_symbol)))
9320 {
9321 symbolS *s = i.op[0].disps->X_add_symbol;
9322 /* No padding to call to global or undefined tls_get_addr. */
9323 if ((S_IS_EXTERNAL (s) || !S_IS_DEFINED (s))
9324 && strcmp (S_GET_NAME (s), tls_get_addr) == 0)
9325 return 0;
9326 }
9327 }
9328
9329 if (add_padding
9330 && last_insn.kind != last_insn_other
9331 && last_insn.seg == now_seg)
9332 {
9333 if (flag_debug)
9334 as_warn_where (last_insn.file, last_insn.line,
9335 _("`%s` skips -malign-branch-boundary on `%s`"),
9336 last_insn.name, i.tm.name);
9337 return 0;
9338 }
9339
9340 return add_padding;
9341}
9342
29b0f896 9343static void
e3bb37b5 9344output_insn (void)
29b0f896 9345{
2bbd9c25
JJ
9346 fragS *insn_start_frag;
9347 offsetT insn_start_off;
e379e5f3
L
9348 fragS *fragP = NULL;
9349 enum align_branch_kind branch = align_branch_none;
79d72f45
HL
9350 /* The initializer is arbitrary just to avoid uninitialized error.
9351 it's actually either assigned in add_branch_padding_frag_p
9352 or never be used. */
9353 enum mf_jcc_kind mf_jcc = mf_jcc_jo;
2bbd9c25 9354
b4a3a7b4 9355#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
48ef937e 9356 if (IS_ELF && x86_used_note && now_seg != absolute_section)
b4a3a7b4 9357 {
32930e4e
L
9358 if ((i.xstate & xstate_tmm) == xstate_tmm
9359 || i.tm.cpu_flags.bitfield.cpuamx_tile)
9360 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_TMM;
9361
b4a3a7b4
L
9362 if (i.tm.cpu_flags.bitfield.cpu8087
9363 || i.tm.cpu_flags.bitfield.cpu287
9364 || i.tm.cpu_flags.bitfield.cpu387
9365 || i.tm.cpu_flags.bitfield.cpu687
9366 || i.tm.cpu_flags.bitfield.cpufisttp)
9367 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X87;
014d61ea 9368
921eafea 9369 if ((i.xstate & xstate_mmx)
389d00a5
JB
9370 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
9371 && !is_any_vex_encoding (&i.tm)
9372 && (i.tm.base_opcode == 0x77 /* emms */
9373 || i.tm.base_opcode == 0x0e /* femms */)))
b4a3a7b4 9374 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MMX;
014d61ea 9375
32930e4e
L
9376 if (i.index_reg)
9377 {
9378 if (i.index_reg->reg_type.bitfield.zmmword)
9379 i.xstate |= xstate_zmm;
9380 else if (i.index_reg->reg_type.bitfield.ymmword)
9381 i.xstate |= xstate_ymm;
9382 else if (i.index_reg->reg_type.bitfield.xmmword)
9383 i.xstate |= xstate_xmm;
9384 }
014d61ea
JB
9385
9386 /* vzeroall / vzeroupper */
9387 if (i.tm.base_opcode == 0x77 && i.tm.cpu_flags.bitfield.cpuavx)
9388 i.xstate |= xstate_ymm;
9389
c4694f17 9390 if ((i.xstate & xstate_xmm)
389d00a5
JB
9391 /* ldmxcsr / stmxcsr / vldmxcsr / vstmxcsr */
9392 || (i.tm.base_opcode == 0xae
9393 && (i.tm.cpu_flags.bitfield.cpusse
9394 || i.tm.cpu_flags.bitfield.cpuavx))
c4694f17
TG
9395 || i.tm.cpu_flags.bitfield.cpuwidekl
9396 || i.tm.cpu_flags.bitfield.cpukl)
b4a3a7b4 9397 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XMM;
014d61ea 9398
921eafea 9399 if ((i.xstate & xstate_ymm) == xstate_ymm)
b4a3a7b4 9400 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_YMM;
921eafea 9401 if ((i.xstate & xstate_zmm) == xstate_zmm)
b4a3a7b4 9402 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_ZMM;
6225c532 9403 if (i.mask.reg || (i.xstate & xstate_mask) == xstate_mask)
32930e4e 9404 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MASK;
b4a3a7b4
L
9405 if (i.tm.cpu_flags.bitfield.cpufxsr)
9406 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_FXSR;
9407 if (i.tm.cpu_flags.bitfield.cpuxsave)
9408 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVE;
9409 if (i.tm.cpu_flags.bitfield.cpuxsaveopt)
9410 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT;
9411 if (i.tm.cpu_flags.bitfield.cpuxsavec)
9412 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEC;
b0ab0693
L
9413
9414 if (x86_feature_2_used
9415 || i.tm.cpu_flags.bitfield.cpucmov
9416 || i.tm.cpu_flags.bitfield.cpusyscall
389d00a5
JB
9417 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
9418 && i.tm.base_opcode == 0xc7
70e95837 9419 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
b0ab0693
L
9420 && i.tm.extension_opcode == 1) /* cmpxchg8b */)
9421 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_BASELINE;
9422 if (i.tm.cpu_flags.bitfield.cpusse3
9423 || i.tm.cpu_flags.bitfield.cpussse3
9424 || i.tm.cpu_flags.bitfield.cpusse4_1
9425 || i.tm.cpu_flags.bitfield.cpusse4_2
9426 || i.tm.cpu_flags.bitfield.cpucx16
9427 || i.tm.cpu_flags.bitfield.cpupopcnt
9428 /* LAHF-SAHF insns in 64-bit mode. */
9429 || (flag_code == CODE_64BIT
35648716
JB
9430 && (i.tm.base_opcode | 1) == 0x9f
9431 && i.tm.opcode_modifier.opcodespace == SPACE_BASE))
b0ab0693
L
9432 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V2;
9433 if (i.tm.cpu_flags.bitfield.cpuavx
9434 || i.tm.cpu_flags.bitfield.cpuavx2
a9860005
JB
9435 /* Any VEX encoded insns execpt for AVX512F, AVX512BW, AVX512DQ,
9436 XOP, FMA4, LPW, TBM, and AMX. */
b0ab0693
L
9437 || (i.tm.opcode_modifier.vex
9438 && !i.tm.cpu_flags.bitfield.cpuavx512f
9439 && !i.tm.cpu_flags.bitfield.cpuavx512bw
9440 && !i.tm.cpu_flags.bitfield.cpuavx512dq
a9860005
JB
9441 && !i.tm.cpu_flags.bitfield.cpuxop
9442 && !i.tm.cpu_flags.bitfield.cpufma4
b0ab0693
L
9443 && !i.tm.cpu_flags.bitfield.cpulwp
9444 && !i.tm.cpu_flags.bitfield.cputbm
9445 && !(x86_feature_2_used & GNU_PROPERTY_X86_FEATURE_2_TMM))
9446 || i.tm.cpu_flags.bitfield.cpuf16c
9447 || i.tm.cpu_flags.bitfield.cpufma
9448 || i.tm.cpu_flags.bitfield.cpulzcnt
9449 || i.tm.cpu_flags.bitfield.cpumovbe
9450 || i.tm.cpu_flags.bitfield.cpuxsaves
9451 || (x86_feature_2_used
9452 & (GNU_PROPERTY_X86_FEATURE_2_XSAVE
9453 | GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT
9454 | GNU_PROPERTY_X86_FEATURE_2_XSAVEC)) != 0)
9455 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V3;
9456 if (i.tm.cpu_flags.bitfield.cpuavx512f
9457 || i.tm.cpu_flags.bitfield.cpuavx512bw
9458 || i.tm.cpu_flags.bitfield.cpuavx512dq
9459 || i.tm.cpu_flags.bitfield.cpuavx512vl
a9860005
JB
9460 /* Any EVEX encoded insns except for AVX512ER, AVX512PF,
9461 AVX512-4FMAPS, and AVX512-4VNNIW. */
b0ab0693
L
9462 || (i.tm.opcode_modifier.evex
9463 && !i.tm.cpu_flags.bitfield.cpuavx512er
9464 && !i.tm.cpu_flags.bitfield.cpuavx512pf
a9860005 9465 && !i.tm.cpu_flags.bitfield.cpuavx512_4fmaps
b0ab0693
L
9466 && !i.tm.cpu_flags.bitfield.cpuavx512_4vnniw))
9467 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V4;
b4a3a7b4
L
9468 }
9469#endif
9470
29b0f896
AM
9471 /* Tie dwarf2 debug info to the address at the start of the insn.
9472 We can't do this after the insn has been output as the current
9473 frag may have been closed off. eg. by frag_var. */
9474 dwarf2_emit_insn (0);
9475
2bbd9c25
JJ
9476 insn_start_frag = frag_now;
9477 insn_start_off = frag_now_fix ();
9478
79d72f45 9479 if (add_branch_padding_frag_p (&branch, &mf_jcc))
e379e5f3
L
9480 {
9481 char *p;
9482 /* Branch can be 8 bytes. Leave some room for prefixes. */
9483 unsigned int max_branch_padding_size = 14;
9484
9485 /* Align section to boundary. */
9486 record_alignment (now_seg, align_branch_power);
9487
9488 /* Make room for padding. */
9489 frag_grow (max_branch_padding_size);
9490
9491 /* Start of the padding. */
9492 p = frag_more (0);
9493
9494 fragP = frag_now;
9495
9496 frag_var (rs_machine_dependent, max_branch_padding_size, 0,
9497 ENCODE_RELAX_STATE (BRANCH_PADDING, 0),
9498 NULL, 0, p);
9499
79d72f45 9500 fragP->tc_frag_data.mf_type = mf_jcc;
e379e5f3
L
9501 fragP->tc_frag_data.branch_type = branch;
9502 fragP->tc_frag_data.max_bytes = max_branch_padding_size;
9503 }
9504
d59a54c2
JB
9505 if (!cpu_arch_flags.bitfield.cpui386 && (flag_code != CODE_16BIT)
9506 && !pre_386_16bit_warned)
9507 {
9508 as_warn (_("use .code16 to ensure correct addressing mode"));
9509 pre_386_16bit_warned = true;
9510 }
9511
29b0f896 9512 /* Output jumps. */
0cfa3eb3 9513 if (i.tm.opcode_modifier.jump == JUMP)
29b0f896 9514 output_branch ();
0cfa3eb3
JB
9515 else if (i.tm.opcode_modifier.jump == JUMP_BYTE
9516 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896 9517 output_jump ();
0cfa3eb3 9518 else if (i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT)
29b0f896
AM
9519 output_interseg_jump ();
9520 else
9521 {
9522 /* Output normal instructions here. */
9523 char *p;
9524 unsigned char *q;
47465058 9525 unsigned int j;
79d72f45 9526 enum mf_cmp_kind mf_cmp;
4dffcebc 9527
e4e00185 9528 if (avoid_fence
389d00a5
JB
9529 && (i.tm.base_opcode == 0xaee8
9530 || i.tm.base_opcode == 0xaef0
9531 || i.tm.base_opcode == 0xaef8))
48ef937e
JB
9532 {
9533 /* Encode lfence, mfence, and sfence as
9534 f0 83 04 24 00 lock addl $0x0, (%{re}sp). */
47f4115a
JB
9535 if (flag_code == CODE_16BIT)
9536 as_bad (_("Cannot convert `%s' in 16-bit mode"), i.tm.name);
9537 else if (omit_lock_prefix)
9538 as_bad (_("Cannot convert `%s' with `-momit-lock-prefix=yes' in effect"),
9539 i.tm.name);
9540 else if (now_seg != absolute_section)
48ef937e
JB
9541 {
9542 offsetT val = 0x240483f0ULL;
9543
9544 p = frag_more (5);
9545 md_number_to_chars (p, val, 5);
9546 }
9547 else
9548 abs_section_offset += 5;
9549 return;
9550 }
e4e00185 9551
d022bddd
IT
9552 /* Some processors fail on LOCK prefix. This options makes
9553 assembler ignore LOCK prefix and serves as a workaround. */
9554 if (omit_lock_prefix)
9555 {
35648716
JB
9556 if (i.tm.base_opcode == LOCK_PREFIX_OPCODE
9557 && i.tm.opcode_modifier.isprefix)
d022bddd
IT
9558 return;
9559 i.prefix[LOCK_PREFIX] = 0;
9560 }
9561
e379e5f3
L
9562 if (branch)
9563 /* Skip if this is a branch. */
9564 ;
79d72f45 9565 else if (add_fused_jcc_padding_frag_p (&mf_cmp))
e379e5f3
L
9566 {
9567 /* Make room for padding. */
9568 frag_grow (MAX_FUSED_JCC_PADDING_SIZE);
9569 p = frag_more (0);
9570
9571 fragP = frag_now;
9572
9573 frag_var (rs_machine_dependent, MAX_FUSED_JCC_PADDING_SIZE, 0,
9574 ENCODE_RELAX_STATE (FUSED_JCC_PADDING, 0),
9575 NULL, 0, p);
9576
79d72f45 9577 fragP->tc_frag_data.mf_type = mf_cmp;
e379e5f3
L
9578 fragP->tc_frag_data.branch_type = align_branch_fused;
9579 fragP->tc_frag_data.max_bytes = MAX_FUSED_JCC_PADDING_SIZE;
9580 }
9581 else if (add_branch_prefix_frag_p ())
9582 {
9583 unsigned int max_prefix_size = align_branch_prefix_size;
9584
9585 /* Make room for padding. */
9586 frag_grow (max_prefix_size);
9587 p = frag_more (0);
9588
9589 fragP = frag_now;
9590
9591 frag_var (rs_machine_dependent, max_prefix_size, 0,
9592 ENCODE_RELAX_STATE (BRANCH_PREFIX, 0),
9593 NULL, 0, p);
9594
9595 fragP->tc_frag_data.max_bytes = max_prefix_size;
9596 }
9597
43234a1e
L
9598 /* Since the VEX/EVEX prefix contains the implicit prefix, we
9599 don't need the explicit prefix. */
cf665fee 9600 if (!is_any_vex_encoding (&i.tm))
bc4bd9ab 9601 {
7b47a312 9602 switch (i.tm.opcode_modifier.opcodeprefix)
bc4bd9ab 9603 {
7b47a312
L
9604 case PREFIX_0X66:
9605 add_prefix (0x66);
9606 break;
9607 case PREFIX_0XF2:
9608 add_prefix (0xf2);
9609 break;
9610 case PREFIX_0XF3:
8b65b895
L
9611 if (!i.tm.cpu_flags.bitfield.cpupadlock
9612 || (i.prefix[REP_PREFIX] != 0xf3))
9613 add_prefix (0xf3);
c0f3af97 9614 break;
7b47a312 9615 case PREFIX_NONE:
9a182d04 9616 switch (i.opcode_length)
c0f3af97 9617 {
7b47a312 9618 case 2:
7b47a312 9619 break;
9a182d04 9620 case 1:
7b47a312 9621 /* Check for pseudo prefixes. */
9a182d04
JB
9622 if (!i.tm.opcode_modifier.isprefix || i.tm.base_opcode)
9623 break;
7b47a312
L
9624 as_bad_where (insn_start_frag->fr_file,
9625 insn_start_frag->fr_line,
9626 _("pseudo prefix without instruction"));
9627 return;
9628 default:
9629 abort ();
4dffcebc 9630 }
c0f3af97 9631 break;
c0f3af97
L
9632 default:
9633 abort ();
bc4bd9ab 9634 }
c0f3af97 9635
6d19a37a 9636#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
cf61b747
L
9637 /* For x32, add a dummy REX_OPCODE prefix for mov/add with
9638 R_X86_64_GOTTPOFF relocation so that linker can safely
14470f07
L
9639 perform IE->LE optimization. A dummy REX_OPCODE prefix
9640 is also needed for lea with R_X86_64_GOTPC32_TLSDESC
9641 relocation for GDesc -> IE/LE optimization. */
cf61b747
L
9642 if (x86_elf_abi == X86_64_X32_ABI
9643 && i.operands == 2
14470f07
L
9644 && (i.reloc[0] == BFD_RELOC_X86_64_GOTTPOFF
9645 || i.reloc[0] == BFD_RELOC_X86_64_GOTPC32_TLSDESC)
cf61b747
L
9646 && i.prefix[REX_PREFIX] == 0)
9647 add_prefix (REX_OPCODE);
6d19a37a 9648#endif
cf61b747 9649
c0f3af97
L
9650 /* The prefix bytes. */
9651 for (j = ARRAY_SIZE (i.prefix), q = i.prefix; j > 0; j--, q++)
9652 if (*q)
48ef937e 9653 frag_opcode_byte (*q);
0f10071e 9654 }
ae5c1c7b 9655 else
c0f3af97
L
9656 {
9657 for (j = 0, q = i.prefix; j < ARRAY_SIZE (i.prefix); j++, q++)
9658 if (*q)
9659 switch (j)
9660 {
c0f3af97
L
9661 case SEG_PREFIX:
9662 case ADDR_PREFIX:
48ef937e 9663 frag_opcode_byte (*q);
c0f3af97
L
9664 break;
9665 default:
9666 /* There should be no other prefixes for instructions
9667 with VEX prefix. */
9668 abort ();
9669 }
9670
43234a1e
L
9671 /* For EVEX instructions i.vrex should become 0 after
9672 build_evex_prefix. For VEX instructions upper 16 registers
9673 aren't available, so VREX should be 0. */
9674 if (i.vrex)
9675 abort ();
c0f3af97 9676 /* Now the VEX prefix. */
48ef937e
JB
9677 if (now_seg != absolute_section)
9678 {
9679 p = frag_more (i.vex.length);
9680 for (j = 0; j < i.vex.length; j++)
9681 p[j] = i.vex.bytes[j];
9682 }
9683 else
9684 abs_section_offset += i.vex.length;
c0f3af97 9685 }
252b5132 9686
29b0f896 9687 /* Now the opcode; be careful about word order here! */
389d00a5
JB
9688 j = i.opcode_length;
9689 if (!i.vex.length)
9690 switch (i.tm.opcode_modifier.opcodespace)
9691 {
9692 case SPACE_BASE:
9693 break;
9694 case SPACE_0F:
9695 ++j;
9696 break;
9697 case SPACE_0F38:
9698 case SPACE_0F3A:
9699 j += 2;
9700 break;
9701 default:
9702 abort ();
9703 }
9704
48ef937e 9705 if (now_seg == absolute_section)
389d00a5
JB
9706 abs_section_offset += j;
9707 else if (j == 1)
29b0f896
AM
9708 {
9709 FRAG_APPEND_1_CHAR (i.tm.base_opcode);
9710 }
9711 else
9712 {
389d00a5
JB
9713 p = frag_more (j);
9714 if (!i.vex.length
9715 && i.tm.opcode_modifier.opcodespace != SPACE_BASE)
9716 {
9717 *p++ = 0x0f;
9718 if (i.tm.opcode_modifier.opcodespace != SPACE_0F)
9719 *p++ = i.tm.opcode_modifier.opcodespace == SPACE_0F38
9720 ? 0x38 : 0x3a;
9721 }
9722
9a182d04 9723 switch (i.opcode_length)
331d2d0d 9724 {
4dffcebc 9725 case 2:
389d00a5
JB
9726 /* Put out high byte first: can't use md_number_to_chars! */
9727 *p++ = (i.tm.base_opcode >> 8) & 0xff;
9728 /* Fall through. */
9729 case 1:
9730 *p = i.tm.base_opcode & 0xff;
4dffcebc
L
9731 break;
9732 default:
9733 abort ();
9734 break;
331d2d0d 9735 }
0f10071e 9736
29b0f896 9737 }
3e73aa7c 9738
29b0f896 9739 /* Now the modrm byte and sib byte (if present). */
40fb9820 9740 if (i.tm.opcode_modifier.modrm)
29b0f896 9741 {
48ef937e
JB
9742 frag_opcode_byte ((i.rm.regmem << 0)
9743 | (i.rm.reg << 3)
9744 | (i.rm.mode << 6));
29b0f896
AM
9745 /* If i.rm.regmem == ESP (4)
9746 && i.rm.mode != (Register mode)
9747 && not 16 bit
9748 ==> need second modrm byte. */
9749 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
9750 && i.rm.mode != 3
dc821c5f 9751 && !(i.base_reg && i.base_reg->reg_type.bitfield.word))
48ef937e
JB
9752 frag_opcode_byte ((i.sib.base << 0)
9753 | (i.sib.index << 3)
9754 | (i.sib.scale << 6));
29b0f896 9755 }
3e73aa7c 9756
29b0f896 9757 if (i.disp_operands)
2bbd9c25 9758 output_disp (insn_start_frag, insn_start_off);
3e73aa7c 9759
29b0f896 9760 if (i.imm_operands)
2bbd9c25 9761 output_imm (insn_start_frag, insn_start_off);
9c33702b
JB
9762
9763 /*
9764 * frag_now_fix () returning plain abs_section_offset when we're in the
9765 * absolute section, and abs_section_offset not getting updated as data
9766 * gets added to the frag breaks the logic below.
9767 */
9768 if (now_seg != absolute_section)
9769 {
9770 j = encoding_length (insn_start_frag, insn_start_off, frag_more (0));
9771 if (j > 15)
9772 as_warn (_("instruction length of %u bytes exceeds the limit of 15"),
9773 j);
e379e5f3
L
9774 else if (fragP)
9775 {
9776 /* NB: Don't add prefix with GOTPC relocation since
9777 output_disp() above depends on the fixed encoding
9778 length. Can't add prefix with TLS relocation since
9779 it breaks TLS linker optimization. */
9780 unsigned int max = i.has_gotpc_tls_reloc ? 0 : 15 - j;
9781 /* Prefix count on the current instruction. */
9782 unsigned int count = i.vex.length;
9783 unsigned int k;
9784 for (k = 0; k < ARRAY_SIZE (i.prefix); k++)
9785 /* REX byte is encoded in VEX/EVEX prefix. */
9786 if (i.prefix[k] && (k != REX_PREFIX || !i.vex.length))
9787 count++;
9788
9789 /* Count prefixes for extended opcode maps. */
9790 if (!i.vex.length)
389d00a5 9791 switch (i.tm.opcode_modifier.opcodespace)
e379e5f3 9792 {
389d00a5 9793 case SPACE_BASE:
e379e5f3 9794 break;
389d00a5
JB
9795 case SPACE_0F:
9796 count++;
e379e5f3 9797 break;
389d00a5
JB
9798 case SPACE_0F38:
9799 case SPACE_0F3A:
9800 count += 2;
e379e5f3
L
9801 break;
9802 default:
9803 abort ();
9804 }
9805
9806 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
9807 == BRANCH_PREFIX)
9808 {
9809 /* Set the maximum prefix size in BRANCH_PREFIX
9810 frag. */
9811 if (fragP->tc_frag_data.max_bytes > max)
9812 fragP->tc_frag_data.max_bytes = max;
9813 if (fragP->tc_frag_data.max_bytes > count)
9814 fragP->tc_frag_data.max_bytes -= count;
9815 else
9816 fragP->tc_frag_data.max_bytes = 0;
9817 }
9818 else
9819 {
9820 /* Remember the maximum prefix size in FUSED_JCC_PADDING
9821 frag. */
9822 unsigned int max_prefix_size;
9823 if (align_branch_prefix_size > max)
9824 max_prefix_size = max;
9825 else
9826 max_prefix_size = align_branch_prefix_size;
9827 if (max_prefix_size > count)
9828 fragP->tc_frag_data.max_prefix_length
9829 = max_prefix_size - count;
9830 }
9831
9832 /* Use existing segment prefix if possible. Use CS
9833 segment prefix in 64-bit mode. In 32-bit mode, use SS
9834 segment prefix with ESP/EBP base register and use DS
9835 segment prefix without ESP/EBP base register. */
9836 if (i.prefix[SEG_PREFIX])
9837 fragP->tc_frag_data.default_prefix = i.prefix[SEG_PREFIX];
9838 else if (flag_code == CODE_64BIT)
9839 fragP->tc_frag_data.default_prefix = CS_PREFIX_OPCODE;
9840 else if (i.base_reg
9841 && (i.base_reg->reg_num == 4
9842 || i.base_reg->reg_num == 5))
9843 fragP->tc_frag_data.default_prefix = SS_PREFIX_OPCODE;
9844 else
9845 fragP->tc_frag_data.default_prefix = DS_PREFIX_OPCODE;
9846 }
9c33702b 9847 }
29b0f896 9848 }
252b5132 9849
e379e5f3
L
9850 /* NB: Don't work with COND_JUMP86 without i386. */
9851 if (align_branch_power
9852 && now_seg != absolute_section
9853 && cpu_arch_flags.bitfield.cpui386)
9854 {
9855 /* Terminate each frag so that we can add prefix and check for
9856 fused jcc. */
9857 frag_wane (frag_now);
9858 frag_new (0);
9859 }
9860
29b0f896
AM
9861#ifdef DEBUG386
9862 if (flag_debug)
9863 {
7b81dfbb 9864 pi ("" /*line*/, &i);
29b0f896
AM
9865 }
9866#endif /* DEBUG386 */
9867}
252b5132 9868
e205caa7
L
9869/* Return the size of the displacement operand N. */
9870
9871static int
9872disp_size (unsigned int n)
9873{
9874 int size = 4;
43234a1e 9875
b5014f7a 9876 if (i.types[n].bitfield.disp64)
40fb9820
L
9877 size = 8;
9878 else if (i.types[n].bitfield.disp8)
9879 size = 1;
9880 else if (i.types[n].bitfield.disp16)
9881 size = 2;
e205caa7
L
9882 return size;
9883}
9884
9885/* Return the size of the immediate operand N. */
9886
9887static int
9888imm_size (unsigned int n)
9889{
9890 int size = 4;
40fb9820
L
9891 if (i.types[n].bitfield.imm64)
9892 size = 8;
9893 else if (i.types[n].bitfield.imm8 || i.types[n].bitfield.imm8s)
9894 size = 1;
9895 else if (i.types[n].bitfield.imm16)
9896 size = 2;
e205caa7
L
9897 return size;
9898}
9899
29b0f896 9900static void
64e74474 9901output_disp (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
9902{
9903 char *p;
9904 unsigned int n;
252b5132 9905
29b0f896
AM
9906 for (n = 0; n < i.operands; n++)
9907 {
b5014f7a 9908 if (operand_type_check (i.types[n], disp))
29b0f896 9909 {
48ef937e
JB
9910 int size = disp_size (n);
9911
9912 if (now_seg == absolute_section)
9913 abs_section_offset += size;
9914 else if (i.op[n].disps->X_op == O_constant)
29b0f896 9915 {
43234a1e 9916 offsetT val = i.op[n].disps->X_add_number;
252b5132 9917
629cfaf1
JB
9918 val = offset_in_range (val >> (size == 1 ? i.memshift : 0),
9919 size);
29b0f896
AM
9920 p = frag_more (size);
9921 md_number_to_chars (p, val, size);
9922 }
9923 else
9924 {
f86103b7 9925 enum bfd_reloc_code_real reloc_type;
a775efc8
JB
9926 bool pcrel = (i.flags[n] & Operand_PCrel) != 0;
9927 bool sign = (flag_code == CODE_64BIT && size == 4
9928 && (!want_disp32 (&i.tm)
9929 || (i.tm.opcode_modifier.jump && !i.jumpabsolute
9930 && !i.types[n].bitfield.baseindex)))
9931 || pcrel;
02a86693 9932 fixS *fixP;
29b0f896 9933
e205caa7 9934 /* We can't have 8 bit displacement here. */
9c2799c2 9935 gas_assert (!i.types[n].bitfield.disp8);
e205caa7 9936
29b0f896
AM
9937 /* The PC relative address is computed relative
9938 to the instruction boundary, so in case immediate
9939 fields follows, we need to adjust the value. */
9940 if (pcrel && i.imm_operands)
9941 {
29b0f896 9942 unsigned int n1;
e205caa7 9943 int sz = 0;
252b5132 9944
29b0f896 9945 for (n1 = 0; n1 < i.operands; n1++)
40fb9820 9946 if (operand_type_check (i.types[n1], imm))
252b5132 9947 {
e205caa7
L
9948 /* Only one immediate is allowed for PC
9949 relative address. */
9c2799c2 9950 gas_assert (sz == 0);
e205caa7
L
9951 sz = imm_size (n1);
9952 i.op[n].disps->X_add_number -= sz;
252b5132 9953 }
29b0f896 9954 /* We should find the immediate. */
9c2799c2 9955 gas_assert (sz != 0);
29b0f896 9956 }
520dc8e8 9957
29b0f896 9958 p = frag_more (size);
d258b828 9959 reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
d6ab8113 9960 if (GOT_symbol
2bbd9c25 9961 && GOT_symbol == i.op[n].disps->X_add_symbol
d6ab8113 9962 && (((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
9963 || reloc_type == BFD_RELOC_X86_64_32S
9964 || (reloc_type == BFD_RELOC_64
9965 && object_64bit))
d6ab8113
JB
9966 && (i.op[n].disps->X_op == O_symbol
9967 || (i.op[n].disps->X_op == O_add
9968 && ((symbol_get_value_expression
9969 (i.op[n].disps->X_op_symbol)->X_op)
9970 == O_subtract))))
9971 || reloc_type == BFD_RELOC_32_PCREL))
2bbd9c25 9972 {
4fa24527 9973 if (!object_64bit)
7b81dfbb
AJ
9974 {
9975 reloc_type = BFD_RELOC_386_GOTPC;
5b7c81bd 9976 i.has_gotpc_tls_reloc = true;
98da05bf 9977 i.op[n].disps->X_add_number +=
d583596c 9978 encoding_length (insn_start_frag, insn_start_off, p);
7b81dfbb
AJ
9979 }
9980 else if (reloc_type == BFD_RELOC_64)
9981 reloc_type = BFD_RELOC_X86_64_GOTPC64;
d6ab8113 9982 else
7b81dfbb
AJ
9983 /* Don't do the adjustment for x86-64, as there
9984 the pcrel addressing is relative to the _next_
9985 insn, and that is taken care of in other code. */
d6ab8113 9986 reloc_type = BFD_RELOC_X86_64_GOTPC32;
2bbd9c25 9987 }
e379e5f3
L
9988 else if (align_branch_power)
9989 {
9990 switch (reloc_type)
9991 {
9992 case BFD_RELOC_386_TLS_GD:
9993 case BFD_RELOC_386_TLS_LDM:
9994 case BFD_RELOC_386_TLS_IE:
9995 case BFD_RELOC_386_TLS_IE_32:
9996 case BFD_RELOC_386_TLS_GOTIE:
9997 case BFD_RELOC_386_TLS_GOTDESC:
9998 case BFD_RELOC_386_TLS_DESC_CALL:
9999 case BFD_RELOC_X86_64_TLSGD:
10000 case BFD_RELOC_X86_64_TLSLD:
10001 case BFD_RELOC_X86_64_GOTTPOFF:
10002 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
10003 case BFD_RELOC_X86_64_TLSDESC_CALL:
5b7c81bd 10004 i.has_gotpc_tls_reloc = true;
e379e5f3
L
10005 default:
10006 break;
10007 }
10008 }
02a86693
L
10009 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal,
10010 size, i.op[n].disps, pcrel,
10011 reloc_type);
eb19308f
JB
10012
10013 if (flag_code == CODE_64BIT && size == 4 && pcrel
10014 && !i.prefix[ADDR_PREFIX])
10015 fixP->fx_signed = 1;
10016
02a86693
L
10017 /* Check for "call/jmp *mem", "mov mem, %reg",
10018 "test %reg, mem" and "binop mem, %reg" where binop
10019 is one of adc, add, and, cmp, or, sbb, sub, xor
e60f4d3b
L
10020 instructions without data prefix. Always generate
10021 R_386_GOT32X for "sym*GOT" operand in 32-bit mode. */
10022 if (i.prefix[DATA_PREFIX] == 0
10023 && (generate_relax_relocations
10024 || (!object_64bit
10025 && i.rm.mode == 0
10026 && i.rm.regmem == 5))
0cb4071e
L
10027 && (i.rm.mode == 2
10028 || (i.rm.mode == 0 && i.rm.regmem == 5))
389d00a5 10029 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
02a86693
L
10030 && ((i.operands == 1
10031 && i.tm.base_opcode == 0xff
10032 && (i.rm.reg == 2 || i.rm.reg == 4))
10033 || (i.operands == 2
10034 && (i.tm.base_opcode == 0x8b
10035 || i.tm.base_opcode == 0x85
2ae4c703 10036 || (i.tm.base_opcode & ~0x38) == 0x03))))
02a86693
L
10037 {
10038 if (object_64bit)
10039 {
10040 fixP->fx_tcbit = i.rex != 0;
10041 if (i.base_reg
e968fc9b 10042 && (i.base_reg->reg_num == RegIP))
02a86693
L
10043 fixP->fx_tcbit2 = 1;
10044 }
10045 else
10046 fixP->fx_tcbit2 = 1;
10047 }
29b0f896
AM
10048 }
10049 }
10050 }
10051}
252b5132 10052
29b0f896 10053static void
64e74474 10054output_imm (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
10055{
10056 char *p;
10057 unsigned int n;
252b5132 10058
29b0f896
AM
10059 for (n = 0; n < i.operands; n++)
10060 {
40fb9820 10061 if (operand_type_check (i.types[n], imm))
29b0f896 10062 {
48ef937e
JB
10063 int size = imm_size (n);
10064
10065 if (now_seg == absolute_section)
10066 abs_section_offset += size;
10067 else if (i.op[n].imms->X_op == O_constant)
29b0f896 10068 {
29b0f896 10069 offsetT val;
b4cac588 10070
29b0f896
AM
10071 val = offset_in_range (i.op[n].imms->X_add_number,
10072 size);
10073 p = frag_more (size);
10074 md_number_to_chars (p, val, size);
10075 }
10076 else
10077 {
10078 /* Not absolute_section.
10079 Need a 32-bit fixup (don't support 8bit
10080 non-absolute imms). Try to support other
10081 sizes ... */
f86103b7 10082 enum bfd_reloc_code_real reloc_type;
e205caa7 10083 int sign;
29b0f896 10084
40fb9820 10085 if (i.types[n].bitfield.imm32s
a7d61044 10086 && (i.suffix == QWORD_MNEM_SUFFIX
40fb9820 10087 || (!i.suffix && i.tm.opcode_modifier.no_lsuf)))
29b0f896 10088 sign = 1;
e205caa7
L
10089 else
10090 sign = 0;
520dc8e8 10091
29b0f896 10092 p = frag_more (size);
d258b828 10093 reloc_type = reloc (size, 0, sign, i.reloc[n]);
f86103b7 10094
2bbd9c25
JJ
10095 /* This is tough to explain. We end up with this one if we
10096 * have operands that look like
10097 * "_GLOBAL_OFFSET_TABLE_+[.-.L284]". The goal here is to
10098 * obtain the absolute address of the GOT, and it is strongly
10099 * preferable from a performance point of view to avoid using
10100 * a runtime relocation for this. The actual sequence of
10101 * instructions often look something like:
10102 *
10103 * call .L66
10104 * .L66:
10105 * popl %ebx
10106 * addl $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
10107 *
10108 * The call and pop essentially return the absolute address
10109 * of the label .L66 and store it in %ebx. The linker itself
10110 * will ultimately change the first operand of the addl so
10111 * that %ebx points to the GOT, but to keep things simple, the
10112 * .o file must have this operand set so that it generates not
10113 * the absolute address of .L66, but the absolute address of
10114 * itself. This allows the linker itself simply treat a GOTPC
10115 * relocation as asking for a pcrel offset to the GOT to be
10116 * added in, and the addend of the relocation is stored in the
10117 * operand field for the instruction itself.
10118 *
10119 * Our job here is to fix the operand so that it would add
10120 * the correct offset so that %ebx would point to itself. The
10121 * thing that is tricky is that .-.L66 will point to the
10122 * beginning of the instruction, so we need to further modify
10123 * the operand so that it will point to itself. There are
10124 * other cases where you have something like:
10125 *
10126 * .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
10127 *
10128 * and here no correction would be required. Internally in
10129 * the assembler we treat operands of this form as not being
10130 * pcrel since the '.' is explicitly mentioned, and I wonder
10131 * whether it would simplify matters to do it this way. Who
10132 * knows. In earlier versions of the PIC patches, the
10133 * pcrel_adjust field was used to store the correction, but
10134 * since the expression is not pcrel, I felt it would be
10135 * confusing to do it this way. */
10136
d6ab8113 10137 if ((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
10138 || reloc_type == BFD_RELOC_X86_64_32S
10139 || reloc_type == BFD_RELOC_64)
29b0f896
AM
10140 && GOT_symbol
10141 && GOT_symbol == i.op[n].imms->X_add_symbol
10142 && (i.op[n].imms->X_op == O_symbol
10143 || (i.op[n].imms->X_op == O_add
10144 && ((symbol_get_value_expression
10145 (i.op[n].imms->X_op_symbol)->X_op)
10146 == O_subtract))))
10147 {
4fa24527 10148 if (!object_64bit)
d6ab8113 10149 reloc_type = BFD_RELOC_386_GOTPC;
7b81dfbb 10150 else if (size == 4)
d6ab8113 10151 reloc_type = BFD_RELOC_X86_64_GOTPC32;
7b81dfbb
AJ
10152 else if (size == 8)
10153 reloc_type = BFD_RELOC_X86_64_GOTPC64;
5b7c81bd 10154 i.has_gotpc_tls_reloc = true;
d583596c
JB
10155 i.op[n].imms->X_add_number +=
10156 encoding_length (insn_start_frag, insn_start_off, p);
29b0f896 10157 }
29b0f896
AM
10158 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
10159 i.op[n].imms, 0, reloc_type);
10160 }
10161 }
10162 }
252b5132
RH
10163}
10164\f
d182319b
JB
10165/* x86_cons_fix_new is called via the expression parsing code when a
10166 reloc is needed. We use this hook to get the correct .got reloc. */
d182319b
JB
10167static int cons_sign = -1;
10168
10169void
e3bb37b5 10170x86_cons_fix_new (fragS *frag, unsigned int off, unsigned int len,
62ebcb5c 10171 expressionS *exp, bfd_reloc_code_real_type r)
d182319b 10172{
d258b828 10173 r = reloc (len, 0, cons_sign, r);
d182319b
JB
10174
10175#ifdef TE_PE
10176 if (exp->X_op == O_secrel)
10177 {
10178 exp->X_op = O_symbol;
10179 r = BFD_RELOC_32_SECREL;
10180 }
145667f8
MH
10181 else if (exp->X_op == O_secidx)
10182 r = BFD_RELOC_16_SECIDX;
d182319b
JB
10183#endif
10184
10185 fix_new_exp (frag, off, len, exp, 0, r);
10186}
10187
357d1bd8
L
10188/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
10189 purpose of the `.dc.a' internal pseudo-op. */
10190
10191int
10192x86_address_bytes (void)
10193{
10194 if ((stdoutput->arch_info->mach & bfd_mach_x64_32))
10195 return 4;
10196 return stdoutput->arch_info->bits_per_address / 8;
10197}
10198
deea4973
JB
10199#if (!(defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined (OBJ_MACH_O)) \
10200 || defined (LEX_AT)) && !defined (TE_PE)
d258b828 10201# define lex_got(reloc, adjust, types) NULL
718ddfc0 10202#else
f3c180ae
AM
10203/* Parse operands of the form
10204 <symbol>@GOTOFF+<nnn>
10205 and similar .plt or .got references.
10206
10207 If we find one, set up the correct relocation in RELOC and copy the
10208 input string, minus the `@GOTOFF' into a malloc'd buffer for
10209 parsing by the calling routine. Return this buffer, and if ADJUST
10210 is non-null set it to the length of the string we removed from the
10211 input line. Otherwise return NULL. */
10212static char *
91d6fa6a 10213lex_got (enum bfd_reloc_code_real *rel,
64e74474 10214 int *adjust,
d258b828 10215 i386_operand_type *types)
f3c180ae 10216{
7b81dfbb
AJ
10217 /* Some of the relocations depend on the size of what field is to
10218 be relocated. But in our callers i386_immediate and i386_displacement
10219 we don't yet know the operand size (this will be set by insn
10220 matching). Hence we record the word32 relocation here,
10221 and adjust the reloc according to the real size in reloc(). */
145667f8
MH
10222 static const struct
10223 {
f3c180ae 10224 const char *str;
cff8d58a 10225 int len;
4fa24527 10226 const enum bfd_reloc_code_real rel[2];
40fb9820 10227 const i386_operand_type types64;
5b7c81bd 10228 bool need_GOT_symbol;
145667f8
MH
10229 }
10230 gotrel[] =
10231 {
deea4973 10232#ifndef TE_PE
8ce3d284 10233#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
10234 { STRING_COMMA_LEN ("SIZE"), { BFD_RELOC_SIZE32,
10235 BFD_RELOC_SIZE32 },
5b7c81bd 10236 OPERAND_TYPE_IMM32_64, false },
8ce3d284 10237#endif
cff8d58a
L
10238 { STRING_COMMA_LEN ("PLTOFF"), { _dummy_first_bfd_reloc_code_real,
10239 BFD_RELOC_X86_64_PLTOFF64 },
5b7c81bd 10240 OPERAND_TYPE_IMM64, true },
cff8d58a
L
10241 { STRING_COMMA_LEN ("PLT"), { BFD_RELOC_386_PLT32,
10242 BFD_RELOC_X86_64_PLT32 },
a775efc8 10243 OPERAND_TYPE_IMM32_32S_DISP32, false },
cff8d58a
L
10244 { STRING_COMMA_LEN ("GOTPLT"), { _dummy_first_bfd_reloc_code_real,
10245 BFD_RELOC_X86_64_GOTPLT64 },
5b7c81bd 10246 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
10247 { STRING_COMMA_LEN ("GOTOFF"), { BFD_RELOC_386_GOTOFF,
10248 BFD_RELOC_X86_64_GOTOFF64 },
5b7c81bd 10249 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
10250 { STRING_COMMA_LEN ("GOTPCREL"), { _dummy_first_bfd_reloc_code_real,
10251 BFD_RELOC_X86_64_GOTPCREL },
a775efc8 10252 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10253 { STRING_COMMA_LEN ("TLSGD"), { BFD_RELOC_386_TLS_GD,
10254 BFD_RELOC_X86_64_TLSGD },
a775efc8 10255 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10256 { STRING_COMMA_LEN ("TLSLDM"), { BFD_RELOC_386_TLS_LDM,
10257 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10258 OPERAND_TYPE_NONE, true },
cff8d58a
L
10259 { STRING_COMMA_LEN ("TLSLD"), { _dummy_first_bfd_reloc_code_real,
10260 BFD_RELOC_X86_64_TLSLD },
a775efc8 10261 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10262 { STRING_COMMA_LEN ("GOTTPOFF"), { BFD_RELOC_386_TLS_IE_32,
10263 BFD_RELOC_X86_64_GOTTPOFF },
a775efc8 10264 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10265 { STRING_COMMA_LEN ("TPOFF"), { BFD_RELOC_386_TLS_LE_32,
10266 BFD_RELOC_X86_64_TPOFF32 },
a775efc8 10267 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
10268 { STRING_COMMA_LEN ("NTPOFF"), { BFD_RELOC_386_TLS_LE,
10269 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10270 OPERAND_TYPE_NONE, true },
cff8d58a
L
10271 { STRING_COMMA_LEN ("DTPOFF"), { BFD_RELOC_386_TLS_LDO_32,
10272 BFD_RELOC_X86_64_DTPOFF32 },
a775efc8 10273 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
10274 { STRING_COMMA_LEN ("GOTNTPOFF"),{ BFD_RELOC_386_TLS_GOTIE,
10275 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10276 OPERAND_TYPE_NONE, true },
cff8d58a
L
10277 { STRING_COMMA_LEN ("INDNTPOFF"),{ BFD_RELOC_386_TLS_IE,
10278 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10279 OPERAND_TYPE_NONE, true },
cff8d58a
L
10280 { STRING_COMMA_LEN ("GOT"), { BFD_RELOC_386_GOT32,
10281 BFD_RELOC_X86_64_GOT32 },
a775efc8 10282 OPERAND_TYPE_IMM32_32S_64_DISP32, true },
cff8d58a
L
10283 { STRING_COMMA_LEN ("TLSDESC"), { BFD_RELOC_386_TLS_GOTDESC,
10284 BFD_RELOC_X86_64_GOTPC32_TLSDESC },
a775efc8 10285 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10286 { STRING_COMMA_LEN ("TLSCALL"), { BFD_RELOC_386_TLS_DESC_CALL,
10287 BFD_RELOC_X86_64_TLSDESC_CALL },
a775efc8 10288 OPERAND_TYPE_IMM32_32S_DISP32, true },
deea4973
JB
10289#else /* TE_PE */
10290 { STRING_COMMA_LEN ("SECREL32"), { BFD_RELOC_32_SECREL,
10291 BFD_RELOC_32_SECREL },
a775efc8 10292 OPERAND_TYPE_IMM32_32S_64_DISP32_64, false },
deea4973 10293#endif
f3c180ae
AM
10294 };
10295 char *cp;
10296 unsigned int j;
10297
deea4973 10298#if defined (OBJ_MAYBE_ELF) && !defined (TE_PE)
718ddfc0
JB
10299 if (!IS_ELF)
10300 return NULL;
d382c579 10301#endif
718ddfc0 10302
f3c180ae 10303 for (cp = input_line_pointer; *cp != '@'; cp++)
67c11a9b 10304 if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
f3c180ae
AM
10305 return NULL;
10306
47465058 10307 for (j = 0; j < ARRAY_SIZE (gotrel); j++)
f3c180ae 10308 {
cff8d58a 10309 int len = gotrel[j].len;
28f81592 10310 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
f3c180ae 10311 {
4fa24527 10312 if (gotrel[j].rel[object_64bit] != 0)
f3c180ae 10313 {
28f81592
AM
10314 int first, second;
10315 char *tmpbuf, *past_reloc;
f3c180ae 10316
91d6fa6a 10317 *rel = gotrel[j].rel[object_64bit];
f3c180ae 10318
3956db08
JB
10319 if (types)
10320 {
10321 if (flag_code != CODE_64BIT)
40fb9820
L
10322 {
10323 types->bitfield.imm32 = 1;
10324 types->bitfield.disp32 = 1;
10325 }
3956db08
JB
10326 else
10327 *types = gotrel[j].types64;
10328 }
10329
844bf810 10330 if (gotrel[j].need_GOT_symbol && GOT_symbol == NULL)
f3c180ae
AM
10331 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
10332
28f81592 10333 /* The length of the first part of our input line. */
f3c180ae 10334 first = cp - input_line_pointer;
28f81592
AM
10335
10336 /* The second part goes from after the reloc token until
67c11a9b 10337 (and including) an end_of_line char or comma. */
28f81592 10338 past_reloc = cp + 1 + len;
67c11a9b
AM
10339 cp = past_reloc;
10340 while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
10341 ++cp;
10342 second = cp + 1 - past_reloc;
28f81592
AM
10343
10344 /* Allocate and copy string. The trailing NUL shouldn't
10345 be necessary, but be safe. */
add39d23 10346 tmpbuf = XNEWVEC (char, first + second + 2);
f3c180ae 10347 memcpy (tmpbuf, input_line_pointer, first);
0787a12d
AM
10348 if (second != 0 && *past_reloc != ' ')
10349 /* Replace the relocation token with ' ', so that
10350 errors like foo@GOTOFF1 will be detected. */
10351 tmpbuf[first++] = ' ';
af89796a
L
10352 else
10353 /* Increment length by 1 if the relocation token is
10354 removed. */
10355 len++;
10356 if (adjust)
10357 *adjust = len;
0787a12d
AM
10358 memcpy (tmpbuf + first, past_reloc, second);
10359 tmpbuf[first + second] = '\0';
f3c180ae
AM
10360 return tmpbuf;
10361 }
10362
4fa24527
JB
10363 as_bad (_("@%s reloc is not supported with %d-bit output format"),
10364 gotrel[j].str, 1 << (5 + object_64bit));
f3c180ae
AM
10365 return NULL;
10366 }
10367 }
10368
10369 /* Might be a symbol version string. Don't as_bad here. */
10370 return NULL;
10371}
4e4f7c87 10372#endif
f3c180ae 10373
62ebcb5c 10374bfd_reloc_code_real_type
e3bb37b5 10375x86_cons (expressionS *exp, int size)
f3c180ae 10376{
62ebcb5c
AM
10377 bfd_reloc_code_real_type got_reloc = NO_RELOC;
10378
2748c1b1
L
10379#if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
10380 && !defined (LEX_AT)) \
10381 || defined (TE_PE)
ee86248c
JB
10382 intel_syntax = -intel_syntax;
10383
3c7b9c2c 10384 exp->X_md = 0;
4fa24527 10385 if (size == 4 || (object_64bit && size == 8))
f3c180ae
AM
10386 {
10387 /* Handle @GOTOFF and the like in an expression. */
10388 char *save;
10389 char *gotfree_input_line;
4a57f2cf 10390 int adjust = 0;
f3c180ae
AM
10391
10392 save = input_line_pointer;
d258b828 10393 gotfree_input_line = lex_got (&got_reloc, &adjust, NULL);
f3c180ae
AM
10394 if (gotfree_input_line)
10395 input_line_pointer = gotfree_input_line;
10396
10397 expression (exp);
10398
10399 if (gotfree_input_line)
10400 {
10401 /* expression () has merrily parsed up to the end of line,
10402 or a comma - in the wrong buffer. Transfer how far
10403 input_line_pointer has moved to the right buffer. */
10404 input_line_pointer = (save
10405 + (input_line_pointer - gotfree_input_line)
10406 + adjust);
10407 free (gotfree_input_line);
3992d3b7
AM
10408 if (exp->X_op == O_constant
10409 || exp->X_op == O_absent
10410 || exp->X_op == O_illegal
0398aac5 10411 || exp->X_op == O_register
3992d3b7
AM
10412 || exp->X_op == O_big)
10413 {
10414 char c = *input_line_pointer;
10415 *input_line_pointer = 0;
10416 as_bad (_("missing or invalid expression `%s'"), save);
10417 *input_line_pointer = c;
10418 }
b9519cfe
L
10419 else if ((got_reloc == BFD_RELOC_386_PLT32
10420 || got_reloc == BFD_RELOC_X86_64_PLT32)
10421 && exp->X_op != O_symbol)
10422 {
10423 char c = *input_line_pointer;
10424 *input_line_pointer = 0;
10425 as_bad (_("invalid PLT expression `%s'"), save);
10426 *input_line_pointer = c;
10427 }
f3c180ae
AM
10428 }
10429 }
10430 else
10431 expression (exp);
ee86248c
JB
10432
10433 intel_syntax = -intel_syntax;
10434
10435 if (intel_syntax)
10436 i386_intel_simplify (exp);
2748c1b1
L
10437#else
10438 expression (exp);
10439#endif
62ebcb5c 10440
a442cac5
JB
10441 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
10442 if (size == 4 && exp->X_op == O_constant && !object_64bit)
10443 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
10444
62ebcb5c 10445 return got_reloc;
f3c180ae 10446}
f3c180ae 10447
9f32dd5b
L
10448static void
10449signed_cons (int size)
6482c264 10450{
a442cac5 10451 if (object_64bit)
d182319b
JB
10452 cons_sign = 1;
10453 cons (size);
10454 cons_sign = -1;
6482c264
NC
10455}
10456
d182319b 10457#ifdef TE_PE
6482c264 10458static void
7016a5d5 10459pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
6482c264
NC
10460{
10461 expressionS exp;
10462
10463 do
10464 {
10465 expression (&exp);
10466 if (exp.X_op == O_symbol)
10467 exp.X_op = O_secrel;
10468
10469 emit_expr (&exp, 4);
10470 }
10471 while (*input_line_pointer++ == ',');
10472
10473 input_line_pointer--;
10474 demand_empty_rest_of_line ();
10475}
145667f8
MH
10476
10477static void
10478pe_directive_secidx (int dummy ATTRIBUTE_UNUSED)
10479{
10480 expressionS exp;
10481
10482 do
10483 {
10484 expression (&exp);
10485 if (exp.X_op == O_symbol)
10486 exp.X_op = O_secidx;
10487
10488 emit_expr (&exp, 2);
10489 }
10490 while (*input_line_pointer++ == ',');
10491
10492 input_line_pointer--;
10493 demand_empty_rest_of_line ();
10494}
6482c264
NC
10495#endif
10496
7063667e
JB
10497/* Handle Rounding Control / SAE specifiers. */
10498
10499static char *
10500RC_SAE_specifier (const char *pstr)
10501{
10502 unsigned int j;
10503
10504 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); j++)
10505 {
10506 if (!strncmp (pstr, RC_NamesTable[j].name, RC_NamesTable[j].len))
10507 {
10508 if (i.rounding.type != rc_none)
10509 {
10510 as_bad (_("duplicated `{%s}'"), RC_NamesTable[j].name);
10511 return NULL;
10512 }
10513
10514 i.rounding.type = RC_NamesTable[j].type;
10515
10516 return (char *)(pstr + RC_NamesTable[j].len);
10517 }
10518 }
10519
10520 return NULL;
10521}
10522
43234a1e
L
10523/* Handle Vector operations. */
10524
10525static char *
f70c6814 10526check_VecOperations (char *op_string)
43234a1e
L
10527{
10528 const reg_entry *mask;
10529 const char *saved;
10530 char *end_op;
10531
f70c6814 10532 while (*op_string)
43234a1e
L
10533 {
10534 saved = op_string;
10535 if (*op_string == '{')
10536 {
10537 op_string++;
10538
10539 /* Check broadcasts. */
d34049e8 10540 if (startswith (op_string, "1to"))
43234a1e 10541 {
5273a3cd 10542 unsigned int bcst_type;
43234a1e 10543
5273a3cd 10544 if (i.broadcast.type)
43234a1e
L
10545 goto duplicated_vec_op;
10546
10547 op_string += 3;
10548 if (*op_string == '8')
8e6e0792 10549 bcst_type = 8;
b28d1bda 10550 else if (*op_string == '4')
8e6e0792 10551 bcst_type = 4;
b28d1bda 10552 else if (*op_string == '2')
8e6e0792 10553 bcst_type = 2;
43234a1e
L
10554 else if (*op_string == '1'
10555 && *(op_string+1) == '6')
10556 {
8e6e0792 10557 bcst_type = 16;
43234a1e
L
10558 op_string++;
10559 }
0cc78721
CL
10560 else if (*op_string == '3'
10561 && *(op_string+1) == '2')
10562 {
10563 bcst_type = 32;
10564 op_string++;
10565 }
43234a1e
L
10566 else
10567 {
10568 as_bad (_("Unsupported broadcast: `%s'"), saved);
10569 return NULL;
10570 }
10571 op_string++;
10572
5273a3cd
JB
10573 i.broadcast.type = bcst_type;
10574 i.broadcast.operand = this_operand;
43234a1e
L
10575 }
10576 /* Check masking operation. */
10577 else if ((mask = parse_register (op_string, &end_op)) != NULL)
10578 {
8a6fb3f9
JB
10579 if (mask == &bad_reg)
10580 return NULL;
10581
43234a1e 10582 /* k0 can't be used for write mask. */
f74a6307 10583 if (mask->reg_type.bitfield.class != RegMask || !mask->reg_num)
43234a1e 10584 {
6d2cd6b2
JB
10585 as_bad (_("`%s%s' can't be used for write mask"),
10586 register_prefix, mask->reg_name);
43234a1e
L
10587 return NULL;
10588 }
10589
6225c532 10590 if (!i.mask.reg)
43234a1e 10591 {
6225c532
JB
10592 i.mask.reg = mask;
10593 i.mask.operand = this_operand;
43234a1e 10594 }
6225c532
JB
10595 else if (i.mask.reg->reg_num)
10596 goto duplicated_vec_op;
43234a1e
L
10597 else
10598 {
6225c532 10599 i.mask.reg = mask;
43234a1e
L
10600
10601 /* Only "{z}" is allowed here. No need to check
10602 zeroing mask explicitly. */
6225c532 10603 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
10604 {
10605 as_bad (_("invalid write mask `%s'"), saved);
10606 return NULL;
10607 }
10608 }
10609
10610 op_string = end_op;
10611 }
10612 /* Check zeroing-flag for masking operation. */
10613 else if (*op_string == 'z')
10614 {
6225c532 10615 if (!i.mask.reg)
43234a1e 10616 {
6225c532
JB
10617 i.mask.reg = reg_k0;
10618 i.mask.zeroing = 1;
10619 i.mask.operand = this_operand;
43234a1e
L
10620 }
10621 else
10622 {
6225c532 10623 if (i.mask.zeroing)
43234a1e
L
10624 {
10625 duplicated_vec_op:
10626 as_bad (_("duplicated `%s'"), saved);
10627 return NULL;
10628 }
10629
6225c532 10630 i.mask.zeroing = 1;
43234a1e
L
10631
10632 /* Only "{%k}" is allowed here. No need to check mask
10633 register explicitly. */
6225c532 10634 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
10635 {
10636 as_bad (_("invalid zeroing-masking `%s'"),
10637 saved);
10638 return NULL;
10639 }
10640 }
10641
10642 op_string++;
10643 }
7063667e
JB
10644 else if (intel_syntax
10645 && (op_string = RC_SAE_specifier (op_string)) != NULL)
10646 i.rounding.modifier = true;
43234a1e
L
10647 else
10648 goto unknown_vec_op;
10649
10650 if (*op_string != '}')
10651 {
10652 as_bad (_("missing `}' in `%s'"), saved);
10653 return NULL;
10654 }
10655 op_string++;
0ba3a731
L
10656
10657 /* Strip whitespace since the addition of pseudo prefixes
10658 changed how the scrubber treats '{'. */
10659 if (is_space_char (*op_string))
10660 ++op_string;
10661
43234a1e
L
10662 continue;
10663 }
10664 unknown_vec_op:
10665 /* We don't know this one. */
10666 as_bad (_("unknown vector operation: `%s'"), saved);
10667 return NULL;
10668 }
10669
6225c532 10670 if (i.mask.reg && i.mask.zeroing && !i.mask.reg->reg_num)
6d2cd6b2
JB
10671 {
10672 as_bad (_("zeroing-masking only allowed with write mask"));
10673 return NULL;
10674 }
10675
43234a1e
L
10676 return op_string;
10677}
10678
252b5132 10679static int
70e41ade 10680i386_immediate (char *imm_start)
252b5132
RH
10681{
10682 char *save_input_line_pointer;
f3c180ae 10683 char *gotfree_input_line;
252b5132 10684 segT exp_seg = 0;
47926f60 10685 expressionS *exp;
40fb9820
L
10686 i386_operand_type types;
10687
0dfbf9d7 10688 operand_type_set (&types, ~0);
252b5132
RH
10689
10690 if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
10691 {
31b2323c
L
10692 as_bad (_("at most %d immediate operands are allowed"),
10693 MAX_IMMEDIATE_OPERANDS);
252b5132
RH
10694 return 0;
10695 }
10696
10697 exp = &im_expressions[i.imm_operands++];
520dc8e8 10698 i.op[this_operand].imms = exp;
252b5132
RH
10699
10700 if (is_space_char (*imm_start))
10701 ++imm_start;
10702
10703 save_input_line_pointer = input_line_pointer;
10704 input_line_pointer = imm_start;
10705
d258b828 10706 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
10707 if (gotfree_input_line)
10708 input_line_pointer = gotfree_input_line;
252b5132
RH
10709
10710 exp_seg = expression (exp);
10711
83183c0c 10712 SKIP_WHITESPACE ();
252b5132 10713 if (*input_line_pointer)
f3c180ae 10714 as_bad (_("junk `%s' after expression"), input_line_pointer);
252b5132
RH
10715
10716 input_line_pointer = save_input_line_pointer;
f3c180ae 10717 if (gotfree_input_line)
ee86248c
JB
10718 {
10719 free (gotfree_input_line);
10720
9aac24b1 10721 if (exp->X_op == O_constant)
ee86248c
JB
10722 exp->X_op = O_illegal;
10723 }
10724
9aac24b1
JB
10725 if (exp_seg == reg_section)
10726 {
10727 as_bad (_("illegal immediate register operand %s"), imm_start);
10728 return 0;
10729 }
10730
ee86248c
JB
10731 return i386_finalize_immediate (exp_seg, exp, types, imm_start);
10732}
252b5132 10733
ee86248c
JB
10734static int
10735i386_finalize_immediate (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
10736 i386_operand_type types, const char *imm_start)
10737{
10738 if (exp->X_op == O_absent || exp->X_op == O_illegal || exp->X_op == O_big)
252b5132 10739 {
313c53d1
L
10740 if (imm_start)
10741 as_bad (_("missing or invalid immediate expression `%s'"),
10742 imm_start);
3992d3b7 10743 return 0;
252b5132 10744 }
3e73aa7c 10745 else if (exp->X_op == O_constant)
252b5132 10746 {
47926f60 10747 /* Size it properly later. */
40fb9820 10748 i.types[this_operand].bitfield.imm64 = 1;
a442cac5
JB
10749
10750 /* If not 64bit, sign/zero extend val, to account for wraparound
10751 when !BFD64. */
10752 if (flag_code != CODE_64BIT)
10753 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
252b5132 10754 }
4c63da97 10755#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
f86103b7 10756 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
31312f95 10757 && exp_seg != absolute_section
47926f60 10758 && exp_seg != text_section
24eab124
AM
10759 && exp_seg != data_section
10760 && exp_seg != bss_section
10761 && exp_seg != undefined_section
f86103b7 10762 && !bfd_is_com_section (exp_seg))
252b5132 10763 {
d0b47220 10764 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
252b5132
RH
10765 return 0;
10766 }
10767#endif
10768 else
10769 {
10770 /* This is an address. The size of the address will be
24eab124 10771 determined later, depending on destination register,
3e73aa7c 10772 suffix, or the default for the section. */
40fb9820
L
10773 i.types[this_operand].bitfield.imm8 = 1;
10774 i.types[this_operand].bitfield.imm16 = 1;
10775 i.types[this_operand].bitfield.imm32 = 1;
10776 i.types[this_operand].bitfield.imm32s = 1;
10777 i.types[this_operand].bitfield.imm64 = 1;
c6fb90c8
L
10778 i.types[this_operand] = operand_type_and (i.types[this_operand],
10779 types);
252b5132
RH
10780 }
10781
10782 return 1;
10783}
10784
551c1ca1 10785static char *
e3bb37b5 10786i386_scale (char *scale)
252b5132 10787{
551c1ca1
AM
10788 offsetT val;
10789 char *save = input_line_pointer;
252b5132 10790
551c1ca1
AM
10791 input_line_pointer = scale;
10792 val = get_absolute_expression ();
10793
10794 switch (val)
252b5132 10795 {
551c1ca1 10796 case 1:
252b5132
RH
10797 i.log2_scale_factor = 0;
10798 break;
551c1ca1 10799 case 2:
252b5132
RH
10800 i.log2_scale_factor = 1;
10801 break;
551c1ca1 10802 case 4:
252b5132
RH
10803 i.log2_scale_factor = 2;
10804 break;
551c1ca1 10805 case 8:
252b5132
RH
10806 i.log2_scale_factor = 3;
10807 break;
10808 default:
a724f0f4
JB
10809 {
10810 char sep = *input_line_pointer;
10811
10812 *input_line_pointer = '\0';
10813 as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
10814 scale);
10815 *input_line_pointer = sep;
10816 input_line_pointer = save;
10817 return NULL;
10818 }
252b5132 10819 }
29b0f896 10820 if (i.log2_scale_factor != 0 && i.index_reg == 0)
252b5132
RH
10821 {
10822 as_warn (_("scale factor of %d without an index register"),
24eab124 10823 1 << i.log2_scale_factor);
252b5132 10824 i.log2_scale_factor = 0;
252b5132 10825 }
551c1ca1
AM
10826 scale = input_line_pointer;
10827 input_line_pointer = save;
10828 return scale;
252b5132
RH
10829}
10830
252b5132 10831static int
e3bb37b5 10832i386_displacement (char *disp_start, char *disp_end)
252b5132 10833{
29b0f896 10834 expressionS *exp;
252b5132
RH
10835 segT exp_seg = 0;
10836 char *save_input_line_pointer;
f3c180ae 10837 char *gotfree_input_line;
40fb9820
L
10838 int override;
10839 i386_operand_type bigdisp, types = anydisp;
3992d3b7 10840 int ret;
252b5132 10841
31b2323c
L
10842 if (i.disp_operands == MAX_MEMORY_OPERANDS)
10843 {
10844 as_bad (_("at most %d displacement operands are allowed"),
10845 MAX_MEMORY_OPERANDS);
10846 return 0;
10847 }
10848
0dfbf9d7 10849 operand_type_set (&bigdisp, 0);
6f2f06be 10850 if (i.jumpabsolute
48bcea9f 10851 || i.types[this_operand].bitfield.baseindex
0cfa3eb3
JB
10852 || (current_templates->start->opcode_modifier.jump != JUMP
10853 && current_templates->start->opcode_modifier.jump != JUMP_DWORD))
e05278af 10854 {
48bcea9f 10855 i386_addressing_mode ();
e05278af 10856 override = (i.prefix[ADDR_PREFIX] != 0);
40fb9820
L
10857 if (flag_code == CODE_64BIT)
10858 {
a775efc8 10859 bigdisp.bitfield.disp32 = 1;
40fb9820 10860 if (!override)
a775efc8 10861 bigdisp.bitfield.disp64 = 1;
40fb9820
L
10862 }
10863 else if ((flag_code == CODE_16BIT) ^ override)
40fb9820 10864 bigdisp.bitfield.disp16 = 1;
48bcea9f
JB
10865 else
10866 bigdisp.bitfield.disp32 = 1;
e05278af
JB
10867 }
10868 else
10869 {
376cd056
JB
10870 /* For PC-relative branches, the width of the displacement may be
10871 dependent upon data size, but is never dependent upon address size.
10872 Also make sure to not unintentionally match against a non-PC-relative
10873 branch template. */
10874 static templates aux_templates;
10875 const insn_template *t = current_templates->start;
5b7c81bd 10876 bool has_intel64 = false;
376cd056
JB
10877
10878 aux_templates.start = t;
10879 while (++t < current_templates->end)
10880 {
10881 if (t->opcode_modifier.jump
10882 != current_templates->start->opcode_modifier.jump)
10883 break;
4b5aaf5f 10884 if ((t->opcode_modifier.isa64 >= INTEL64))
5b7c81bd 10885 has_intel64 = true;
376cd056
JB
10886 }
10887 if (t < current_templates->end)
10888 {
10889 aux_templates.end = t;
10890 current_templates = &aux_templates;
10891 }
10892
e05278af 10893 override = (i.prefix[DATA_PREFIX] != 0);
40fb9820
L
10894 if (flag_code == CODE_64BIT)
10895 {
376cd056
JB
10896 if ((override || i.suffix == WORD_MNEM_SUFFIX)
10897 && (!intel64 || !has_intel64))
40fb9820
L
10898 bigdisp.bitfield.disp16 = 1;
10899 else
a775efc8 10900 bigdisp.bitfield.disp32 = 1;
40fb9820
L
10901 }
10902 else
e05278af
JB
10903 {
10904 if (!override)
10905 override = (i.suffix == (flag_code != CODE_16BIT
10906 ? WORD_MNEM_SUFFIX
10907 : LONG_MNEM_SUFFIX));
40fb9820
L
10908 bigdisp.bitfield.disp32 = 1;
10909 if ((flag_code == CODE_16BIT) ^ override)
10910 {
10911 bigdisp.bitfield.disp32 = 0;
10912 bigdisp.bitfield.disp16 = 1;
10913 }
e05278af 10914 }
e05278af 10915 }
c6fb90c8
L
10916 i.types[this_operand] = operand_type_or (i.types[this_operand],
10917 bigdisp);
252b5132
RH
10918
10919 exp = &disp_expressions[i.disp_operands];
520dc8e8 10920 i.op[this_operand].disps = exp;
252b5132
RH
10921 i.disp_operands++;
10922 save_input_line_pointer = input_line_pointer;
10923 input_line_pointer = disp_start;
10924 END_STRING_AND_SAVE (disp_end);
10925
10926#ifndef GCC_ASM_O_HACK
10927#define GCC_ASM_O_HACK 0
10928#endif
10929#if GCC_ASM_O_HACK
10930 END_STRING_AND_SAVE (disp_end + 1);
40fb9820 10931 if (i.types[this_operand].bitfield.baseIndex
24eab124 10932 && displacement_string_end[-1] == '+')
252b5132
RH
10933 {
10934 /* This hack is to avoid a warning when using the "o"
24eab124
AM
10935 constraint within gcc asm statements.
10936 For instance:
10937
10938 #define _set_tssldt_desc(n,addr,limit,type) \
10939 __asm__ __volatile__ ( \
10940 "movw %w2,%0\n\t" \
10941 "movw %w1,2+%0\n\t" \
10942 "rorl $16,%1\n\t" \
10943 "movb %b1,4+%0\n\t" \
10944 "movb %4,5+%0\n\t" \
10945 "movb $0,6+%0\n\t" \
10946 "movb %h1,7+%0\n\t" \
10947 "rorl $16,%1" \
10948 : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
10949
10950 This works great except that the output assembler ends
10951 up looking a bit weird if it turns out that there is
10952 no offset. You end up producing code that looks like:
10953
10954 #APP
10955 movw $235,(%eax)
10956 movw %dx,2+(%eax)
10957 rorl $16,%edx
10958 movb %dl,4+(%eax)
10959 movb $137,5+(%eax)
10960 movb $0,6+(%eax)
10961 movb %dh,7+(%eax)
10962 rorl $16,%edx
10963 #NO_APP
10964
47926f60 10965 So here we provide the missing zero. */
24eab124
AM
10966
10967 *displacement_string_end = '0';
252b5132
RH
10968 }
10969#endif
d258b828 10970 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
10971 if (gotfree_input_line)
10972 input_line_pointer = gotfree_input_line;
252b5132 10973
24eab124 10974 exp_seg = expression (exp);
252b5132 10975
636c26b0
AM
10976 SKIP_WHITESPACE ();
10977 if (*input_line_pointer)
10978 as_bad (_("junk `%s' after expression"), input_line_pointer);
10979#if GCC_ASM_O_HACK
10980 RESTORE_END_STRING (disp_end + 1);
10981#endif
636c26b0 10982 input_line_pointer = save_input_line_pointer;
636c26b0 10983 if (gotfree_input_line)
ee86248c
JB
10984 {
10985 free (gotfree_input_line);
10986
10987 if (exp->X_op == O_constant || exp->X_op == O_register)
10988 exp->X_op = O_illegal;
10989 }
10990
10991 ret = i386_finalize_displacement (exp_seg, exp, types, disp_start);
10992
10993 RESTORE_END_STRING (disp_end);
10994
10995 return ret;
10996}
10997
10998static int
10999i386_finalize_displacement (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
11000 i386_operand_type types, const char *disp_start)
11001{
ee86248c 11002 int ret = 1;
636c26b0 11003
24eab124
AM
11004 /* We do this to make sure that the section symbol is in
11005 the symbol table. We will ultimately change the relocation
47926f60 11006 to be relative to the beginning of the section. */
1ae12ab7 11007 if (i.reloc[this_operand] == BFD_RELOC_386_GOTOFF
d6ab8113
JB
11008 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
11009 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
24eab124 11010 {
636c26b0 11011 if (exp->X_op != O_symbol)
3992d3b7 11012 goto inv_disp;
636c26b0 11013
e5cb08ac 11014 if (S_IS_LOCAL (exp->X_add_symbol)
c64efb4b
L
11015 && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section
11016 && S_GET_SEGMENT (exp->X_add_symbol) != expr_section)
24eab124 11017 section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
24eab124
AM
11018 exp->X_op = O_subtract;
11019 exp->X_op_symbol = GOT_symbol;
1ae12ab7 11020 if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
29b0f896 11021 i.reloc[this_operand] = BFD_RELOC_32_PCREL;
d6ab8113
JB
11022 else if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
11023 i.reloc[this_operand] = BFD_RELOC_64;
23df1078 11024 else
29b0f896 11025 i.reloc[this_operand] = BFD_RELOC_32;
24eab124 11026 }
252b5132 11027
3992d3b7
AM
11028 else if (exp->X_op == O_absent
11029 || exp->X_op == O_illegal
ee86248c 11030 || exp->X_op == O_big)
2daf4fd8 11031 {
3992d3b7
AM
11032 inv_disp:
11033 as_bad (_("missing or invalid displacement expression `%s'"),
2daf4fd8 11034 disp_start);
3992d3b7 11035 ret = 0;
2daf4fd8
AM
11036 }
11037
a50187b2
JB
11038 else if (exp->X_op == O_constant)
11039 {
11040 /* Sizing gets taken care of by optimize_disp().
11041
11042 If not 64bit, sign/zero extend val, to account for wraparound
11043 when !BFD64. */
11044 if (flag_code != CODE_64BIT)
11045 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
11046 }
11047
4c63da97 11048#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
a50187b2 11049 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
3992d3b7
AM
11050 && exp_seg != absolute_section
11051 && exp_seg != text_section
11052 && exp_seg != data_section
11053 && exp_seg != bss_section
11054 && exp_seg != undefined_section
11055 && !bfd_is_com_section (exp_seg))
24eab124 11056 {
d0b47220 11057 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
3992d3b7 11058 ret = 0;
24eab124 11059 }
252b5132 11060#endif
3956db08 11061
a50187b2 11062 else if (current_templates->start->opcode_modifier.jump == JUMP_BYTE)
48bcea9f
JB
11063 i.types[this_operand].bitfield.disp8 = 1;
11064
40fb9820 11065 /* Check if this is a displacement only operand. */
02b83698
JB
11066 if (!i.types[this_operand].bitfield.baseindex)
11067 i.types[this_operand] =
11068 operand_type_or (operand_type_and_not (i.types[this_operand], anydisp),
11069 operand_type_and (i.types[this_operand], types));
3956db08 11070
3992d3b7 11071 return ret;
252b5132
RH
11072}
11073
2abc2bec
JB
11074/* Return the active addressing mode, taking address override and
11075 registers forming the address into consideration. Update the
11076 address override prefix if necessary. */
47926f60 11077
2abc2bec
JB
11078static enum flag_code
11079i386_addressing_mode (void)
252b5132 11080{
be05d201
L
11081 enum flag_code addr_mode;
11082
11083 if (i.prefix[ADDR_PREFIX])
11084 addr_mode = flag_code == CODE_32BIT ? CODE_16BIT : CODE_32BIT;
a23b33b3
JB
11085 else if (flag_code == CODE_16BIT
11086 && current_templates->start->cpu_flags.bitfield.cpumpx
11087 /* Avoid replacing the "16-bit addressing not allowed" diagnostic
11088 from md_assemble() by "is not a valid base/index expression"
11089 when there is a base and/or index. */
11090 && !i.types[this_operand].bitfield.baseindex)
11091 {
11092 /* MPX insn memory operands with neither base nor index must be forced
11093 to use 32-bit addressing in 16-bit mode. */
11094 addr_mode = CODE_32BIT;
11095 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
11096 ++i.prefixes;
11097 gas_assert (!i.types[this_operand].bitfield.disp16);
11098 gas_assert (!i.types[this_operand].bitfield.disp32);
11099 }
be05d201
L
11100 else
11101 {
11102 addr_mode = flag_code;
11103
24eab124 11104#if INFER_ADDR_PREFIX
be05d201
L
11105 if (i.mem_operands == 0)
11106 {
11107 /* Infer address prefix from the first memory operand. */
11108 const reg_entry *addr_reg = i.base_reg;
11109
11110 if (addr_reg == NULL)
11111 addr_reg = i.index_reg;
eecb386c 11112
be05d201
L
11113 if (addr_reg)
11114 {
e968fc9b 11115 if (addr_reg->reg_type.bitfield.dword)
be05d201
L
11116 addr_mode = CODE_32BIT;
11117 else if (flag_code != CODE_64BIT
dc821c5f 11118 && addr_reg->reg_type.bitfield.word)
be05d201
L
11119 addr_mode = CODE_16BIT;
11120
11121 if (addr_mode != flag_code)
11122 {
11123 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
11124 i.prefixes += 1;
11125 /* Change the size of any displacement too. At most one
11126 of Disp16 or Disp32 is set.
11127 FIXME. There doesn't seem to be any real need for
11128 separate Disp16 and Disp32 flags. The same goes for
11129 Imm16 and Imm32. Removing them would probably clean
11130 up the code quite a lot. */
11131 if (flag_code != CODE_64BIT
11132 && (i.types[this_operand].bitfield.disp16
11133 || i.types[this_operand].bitfield.disp32))
11134 i.types[this_operand]
11135 = operand_type_xor (i.types[this_operand], disp16_32);
11136 }
11137 }
11138 }
24eab124 11139#endif
be05d201
L
11140 }
11141
2abc2bec
JB
11142 return addr_mode;
11143}
11144
11145/* Make sure the memory operand we've been dealt is valid.
11146 Return 1 on success, 0 on a failure. */
11147
11148static int
11149i386_index_check (const char *operand_string)
11150{
11151 const char *kind = "base/index";
11152 enum flag_code addr_mode = i386_addressing_mode ();
a152332d 11153 const insn_template *t = current_templates->start;
2abc2bec 11154
a152332d
JB
11155 if (t->opcode_modifier.isstring
11156 && !t->cpu_flags.bitfield.cpupadlock
fc0763e6
JB
11157 && (current_templates->end[-1].opcode_modifier.isstring
11158 || i.mem_operands))
11159 {
11160 /* Memory operands of string insns are special in that they only allow
11161 a single register (rDI, rSI, or rBX) as their memory address. */
be05d201
L
11162 const reg_entry *expected_reg;
11163 static const char *di_si[][2] =
11164 {
11165 { "esi", "edi" },
11166 { "si", "di" },
11167 { "rsi", "rdi" }
11168 };
11169 static const char *bx[] = { "ebx", "bx", "rbx" };
fc0763e6
JB
11170
11171 kind = "string address";
11172
a152332d 11173 if (t->opcode_modifier.prefixok == PrefixRep)
fc0763e6 11174 {
51c8edf6
JB
11175 int es_op = current_templates->end[-1].opcode_modifier.isstring
11176 - IS_STRING_ES_OP0;
11177 int op = 0;
fc0763e6 11178
51c8edf6 11179 if (!current_templates->end[-1].operand_types[0].bitfield.baseindex
fc0763e6
JB
11180 || ((!i.mem_operands != !intel_syntax)
11181 && current_templates->end[-1].operand_types[1]
11182 .bitfield.baseindex))
51c8edf6 11183 op = 1;
fe0e921f
AM
11184 expected_reg
11185 = (const reg_entry *) str_hash_find (reg_hash,
11186 di_si[addr_mode][op == es_op]);
fc0763e6
JB
11187 }
11188 else
fe0e921f
AM
11189 expected_reg
11190 = (const reg_entry *)str_hash_find (reg_hash, bx[addr_mode]);
fc0763e6 11191
be05d201
L
11192 if (i.base_reg != expected_reg
11193 || i.index_reg
fc0763e6 11194 || operand_type_check (i.types[this_operand], disp))
fc0763e6 11195 {
be05d201
L
11196 /* The second memory operand must have the same size as
11197 the first one. */
11198 if (i.mem_operands
11199 && i.base_reg
11200 && !((addr_mode == CODE_64BIT
dc821c5f 11201 && i.base_reg->reg_type.bitfield.qword)
be05d201 11202 || (addr_mode == CODE_32BIT
dc821c5f
JB
11203 ? i.base_reg->reg_type.bitfield.dword
11204 : i.base_reg->reg_type.bitfield.word)))
be05d201
L
11205 goto bad_address;
11206
fc0763e6
JB
11207 as_warn (_("`%s' is not valid here (expected `%c%s%s%c')"),
11208 operand_string,
11209 intel_syntax ? '[' : '(',
11210 register_prefix,
be05d201 11211 expected_reg->reg_name,
fc0763e6 11212 intel_syntax ? ']' : ')');
be05d201 11213 return 1;
fc0763e6 11214 }
be05d201
L
11215 else
11216 return 1;
11217
dc1e8a47 11218 bad_address:
be05d201
L
11219 as_bad (_("`%s' is not a valid %s expression"),
11220 operand_string, kind);
11221 return 0;
3e73aa7c
JH
11222 }
11223 else
11224 {
be05d201
L
11225 if (addr_mode != CODE_16BIT)
11226 {
11227 /* 32-bit/64-bit checks. */
41eb8e88
L
11228 if (i.disp_encoding == disp_encoding_16bit)
11229 {
11230 bad_disp:
11231 as_bad (_("invalid `%s' prefix"),
11232 addr_mode == CODE_16BIT ? "{disp32}" : "{disp16}");
11233 return 0;
11234 }
11235
be05d201 11236 if ((i.base_reg
e968fc9b
JB
11237 && ((addr_mode == CODE_64BIT
11238 ? !i.base_reg->reg_type.bitfield.qword
11239 : !i.base_reg->reg_type.bitfield.dword)
11240 || (i.index_reg && i.base_reg->reg_num == RegIP)
11241 || i.base_reg->reg_num == RegIZ))
be05d201 11242 || (i.index_reg
1b54b8d7
JB
11243 && !i.index_reg->reg_type.bitfield.xmmword
11244 && !i.index_reg->reg_type.bitfield.ymmword
11245 && !i.index_reg->reg_type.bitfield.zmmword
be05d201 11246 && ((addr_mode == CODE_64BIT
e968fc9b
JB
11247 ? !i.index_reg->reg_type.bitfield.qword
11248 : !i.index_reg->reg_type.bitfield.dword)
be05d201
L
11249 || !i.index_reg->reg_type.bitfield.baseindex)))
11250 goto bad_address;
8178be5b 11251
260cd341 11252 /* bndmk, bndldx, bndstx and mandatory non-vector SIB have special restrictions. */
a152332d 11253 if ((t->opcode_modifier.opcodeprefix == PREFIX_0XF3
389d00a5
JB
11254 && t->opcode_modifier.opcodespace == SPACE_0F
11255 && t->base_opcode == 0x1b)
a152332d 11256 || (t->opcode_modifier.opcodeprefix == PREFIX_NONE
389d00a5
JB
11257 && t->opcode_modifier.opcodespace == SPACE_0F
11258 && (t->base_opcode & ~1) == 0x1a)
a152332d 11259 || t->opcode_modifier.sib == SIBMEM)
8178be5b
JB
11260 {
11261 /* They cannot use RIP-relative addressing. */
e968fc9b 11262 if (i.base_reg && i.base_reg->reg_num == RegIP)
8178be5b
JB
11263 {
11264 as_bad (_("`%s' cannot be used here"), operand_string);
11265 return 0;
11266 }
11267
11268 /* bndldx and bndstx ignore their scale factor. */
a152332d 11269 if (t->opcode_modifier.opcodeprefix == PREFIX_NONE
389d00a5
JB
11270 && t->opcode_modifier.opcodespace == SPACE_0F
11271 && (t->base_opcode & ~1) == 0x1a
8178be5b
JB
11272 && i.log2_scale_factor)
11273 as_warn (_("register scaling is being ignored here"));
11274 }
be05d201
L
11275 }
11276 else
3e73aa7c 11277 {
be05d201 11278 /* 16-bit checks. */
41eb8e88
L
11279 if (i.disp_encoding == disp_encoding_32bit)
11280 goto bad_disp;
11281
3e73aa7c 11282 if ((i.base_reg
dc821c5f 11283 && (!i.base_reg->reg_type.bitfield.word
40fb9820 11284 || !i.base_reg->reg_type.bitfield.baseindex))
3e73aa7c 11285 || (i.index_reg
dc821c5f 11286 && (!i.index_reg->reg_type.bitfield.word
40fb9820 11287 || !i.index_reg->reg_type.bitfield.baseindex
29b0f896
AM
11288 || !(i.base_reg
11289 && i.base_reg->reg_num < 6
11290 && i.index_reg->reg_num >= 6
11291 && i.log2_scale_factor == 0))))
be05d201 11292 goto bad_address;
3e73aa7c
JH
11293 }
11294 }
be05d201 11295 return 1;
24eab124 11296}
252b5132 11297
43234a1e
L
11298/* Handle vector immediates. */
11299
11300static int
11301RC_SAE_immediate (const char *imm_start)
11302{
43234a1e 11303 const char *pstr = imm_start;
43234a1e
L
11304
11305 if (*pstr != '{')
11306 return 0;
11307
7063667e
JB
11308 pstr = RC_SAE_specifier (pstr + 1);
11309 if (pstr == NULL)
43234a1e
L
11310 return 0;
11311
11312 if (*pstr++ != '}')
11313 {
11314 as_bad (_("Missing '}': '%s'"), imm_start);
11315 return 0;
11316 }
11317 /* RC/SAE immediate string should contain nothing more. */;
11318 if (*pstr != 0)
11319 {
11320 as_bad (_("Junk after '}': '%s'"), imm_start);
11321 return 0;
11322 }
11323
cf665fee
JB
11324 /* Internally this doesn't count as an operand. */
11325 --i.operands;
43234a1e 11326
43234a1e
L
11327 return 1;
11328}
11329
8325cc63
JB
11330/* Only string instructions can have a second memory operand, so
11331 reduce current_templates to just those if it contains any. */
11332static int
11333maybe_adjust_templates (void)
11334{
11335 const insn_template *t;
11336
11337 gas_assert (i.mem_operands == 1);
11338
11339 for (t = current_templates->start; t < current_templates->end; ++t)
11340 if (t->opcode_modifier.isstring)
11341 break;
11342
11343 if (t < current_templates->end)
11344 {
11345 static templates aux_templates;
5b7c81bd 11346 bool recheck;
8325cc63
JB
11347
11348 aux_templates.start = t;
11349 for (; t < current_templates->end; ++t)
11350 if (!t->opcode_modifier.isstring)
11351 break;
11352 aux_templates.end = t;
11353
11354 /* Determine whether to re-check the first memory operand. */
11355 recheck = (aux_templates.start != current_templates->start
11356 || t != current_templates->end);
11357
11358 current_templates = &aux_templates;
11359
11360 if (recheck)
11361 {
11362 i.mem_operands = 0;
11363 if (i.memop1_string != NULL
11364 && i386_index_check (i.memop1_string) == 0)
11365 return 0;
11366 i.mem_operands = 1;
11367 }
11368 }
11369
11370 return 1;
11371}
11372
9d299bea
JB
11373static INLINE bool starts_memory_operand (char c)
11374{
014fbcda 11375 return ISDIGIT (c)
9d299bea 11376 || is_identifier_char (c)
014fbcda 11377 || strchr ("([\"+-!~", c);
9d299bea
JB
11378}
11379
fc0763e6 11380/* Parse OPERAND_STRING into the i386_insn structure I. Returns zero
47926f60 11381 on error. */
252b5132 11382
252b5132 11383static int
a7619375 11384i386_att_operand (char *operand_string)
252b5132 11385{
af6bdddf
AM
11386 const reg_entry *r;
11387 char *end_op;
24eab124 11388 char *op_string = operand_string;
252b5132 11389
24eab124 11390 if (is_space_char (*op_string))
252b5132
RH
11391 ++op_string;
11392
24eab124 11393 /* We check for an absolute prefix (differentiating,
47926f60 11394 for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
24eab124
AM
11395 if (*op_string == ABSOLUTE_PREFIX)
11396 {
11397 ++op_string;
11398 if (is_space_char (*op_string))
11399 ++op_string;
5b7c81bd 11400 i.jumpabsolute = true;
24eab124 11401 }
252b5132 11402
47926f60 11403 /* Check if operand is a register. */
4d1bb795 11404 if ((r = parse_register (op_string, &end_op)) != NULL)
24eab124 11405 {
40fb9820
L
11406 i386_operand_type temp;
11407
8a6fb3f9
JB
11408 if (r == &bad_reg)
11409 return 0;
11410
24eab124
AM
11411 /* Check for a segment override by searching for ':' after a
11412 segment register. */
11413 op_string = end_op;
11414 if (is_space_char (*op_string))
11415 ++op_string;
00cee14f 11416 if (*op_string == ':' && r->reg_type.bitfield.class == SReg)
24eab124 11417 {
5e042380 11418 i.seg[i.mem_operands] = r;
252b5132 11419
24eab124 11420 /* Skip the ':' and whitespace. */
252b5132
RH
11421 ++op_string;
11422 if (is_space_char (*op_string))
24eab124 11423 ++op_string;
252b5132 11424
47926f60 11425 /* Handle case of %es:*foo. */
c8d541e2 11426 if (!i.jumpabsolute && *op_string == ABSOLUTE_PREFIX)
24eab124
AM
11427 {
11428 ++op_string;
11429 if (is_space_char (*op_string))
11430 ++op_string;
5b7c81bd 11431 i.jumpabsolute = true;
24eab124 11432 }
c8d541e2 11433
9d299bea 11434 if (!starts_memory_operand (*op_string))
c8d541e2
JB
11435 {
11436 as_bad (_("bad memory operand `%s'"), op_string);
11437 return 0;
11438 }
24eab124
AM
11439 goto do_memory_reference;
11440 }
43234a1e
L
11441
11442 /* Handle vector operations. */
11443 if (*op_string == '{')
11444 {
f70c6814 11445 op_string = check_VecOperations (op_string);
43234a1e
L
11446 if (op_string == NULL)
11447 return 0;
11448 }
11449
24eab124
AM
11450 if (*op_string)
11451 {
d0b47220 11452 as_bad (_("junk `%s' after register"), op_string);
24eab124
AM
11453 return 0;
11454 }
40fb9820
L
11455 temp = r->reg_type;
11456 temp.bitfield.baseindex = 0;
c6fb90c8
L
11457 i.types[this_operand] = operand_type_or (i.types[this_operand],
11458 temp);
7d5e4556 11459 i.types[this_operand].bitfield.unspecified = 0;
520dc8e8 11460 i.op[this_operand].regs = r;
24eab124 11461 i.reg_operands++;
cf665fee
JB
11462
11463 /* A GPR may follow an RC or SAE immediate only if a (vector) register
11464 operand was also present earlier on. */
11465 if (i.rounding.type != rc_none && temp.bitfield.class == Reg
11466 && i.reg_operands == 1)
11467 {
11468 unsigned int j;
11469
11470 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); ++j)
11471 if (i.rounding.type == RC_NamesTable[j].type)
11472 break;
11473 as_bad (_("`%s': misplaced `{%s}'"),
11474 current_templates->start->name, RC_NamesTable[j].name);
11475 return 0;
11476 }
24eab124 11477 }
af6bdddf
AM
11478 else if (*op_string == REGISTER_PREFIX)
11479 {
11480 as_bad (_("bad register name `%s'"), op_string);
11481 return 0;
11482 }
24eab124 11483 else if (*op_string == IMMEDIATE_PREFIX)
ce8a8b2f 11484 {
24eab124 11485 ++op_string;
6f2f06be 11486 if (i.jumpabsolute)
24eab124 11487 {
d0b47220 11488 as_bad (_("immediate operand illegal with absolute jump"));
24eab124
AM
11489 return 0;
11490 }
11491 if (!i386_immediate (op_string))
11492 return 0;
cf665fee
JB
11493 if (i.rounding.type != rc_none)
11494 {
11495 as_bad (_("`%s': RC/SAE operand must follow immediate operands"),
11496 current_templates->start->name);
11497 return 0;
11498 }
24eab124 11499 }
43234a1e
L
11500 else if (RC_SAE_immediate (operand_string))
11501 {
cf665fee
JB
11502 /* If it is a RC or SAE immediate, do the necessary placement check:
11503 Only another immediate or a GPR may precede it. */
11504 if (i.mem_operands || i.reg_operands + i.imm_operands > 1
11505 || (i.reg_operands == 1
11506 && i.op[0].regs->reg_type.bitfield.class != Reg))
11507 {
11508 as_bad (_("`%s': misplaced `%s'"),
11509 current_templates->start->name, operand_string);
11510 return 0;
11511 }
43234a1e 11512 }
9d299bea 11513 else if (starts_memory_operand (*op_string))
24eab124 11514 {
47926f60 11515 /* This is a memory reference of some sort. */
af6bdddf 11516 char *base_string;
252b5132 11517
47926f60 11518 /* Start and end of displacement string expression (if found). */
eecb386c
AM
11519 char *displacement_string_start;
11520 char *displacement_string_end;
252b5132 11521
24eab124 11522 do_memory_reference:
8325cc63
JB
11523 if (i.mem_operands == 1 && !maybe_adjust_templates ())
11524 return 0;
24eab124 11525 if ((i.mem_operands == 1
40fb9820 11526 && !current_templates->start->opcode_modifier.isstring)
24eab124
AM
11527 || i.mem_operands == 2)
11528 {
11529 as_bad (_("too many memory references for `%s'"),
11530 current_templates->start->name);
11531 return 0;
11532 }
252b5132 11533
24eab124
AM
11534 /* Check for base index form. We detect the base index form by
11535 looking for an ')' at the end of the operand, searching
11536 for the '(' matching it, and finding a REGISTER_PREFIX or ','
11537 after the '('. */
af6bdddf 11538 base_string = op_string + strlen (op_string);
c3332e24 11539
43234a1e 11540 /* Handle vector operations. */
6b5ba0d4
JB
11541 --base_string;
11542 if (is_space_char (*base_string))
11543 --base_string;
11544
11545 if (*base_string == '}')
43234a1e 11546 {
6b5ba0d4
JB
11547 char *vop_start = NULL;
11548
11549 while (base_string-- > op_string)
11550 {
11551 if (*base_string == '"')
11552 break;
11553 if (*base_string != '{')
11554 continue;
11555
11556 vop_start = base_string;
11557
11558 --base_string;
11559 if (is_space_char (*base_string))
11560 --base_string;
11561
11562 if (*base_string != '}')
11563 break;
11564
11565 vop_start = NULL;
11566 }
11567
11568 if (!vop_start)
11569 {
11570 as_bad (_("unbalanced figure braces"));
11571 return 0;
11572 }
11573
f70c6814 11574 if (check_VecOperations (vop_start) == NULL)
43234a1e 11575 return 0;
43234a1e
L
11576 }
11577
47926f60 11578 /* If we only have a displacement, set-up for it to be parsed later. */
af6bdddf
AM
11579 displacement_string_start = op_string;
11580 displacement_string_end = base_string + 1;
252b5132 11581
24eab124
AM
11582 if (*base_string == ')')
11583 {
af6bdddf 11584 char *temp_string;
e87fb6a6
JB
11585 unsigned int parens_not_balanced = 0;
11586 bool in_quotes = false;
e68c3d59 11587
24eab124 11588 /* We've already checked that the number of left & right ()'s are
e87fb6a6
JB
11589 equal, and that there's a matching set of double quotes. */
11590 end_op = base_string;
11591 for (temp_string = op_string; temp_string < end_op; temp_string++)
24eab124 11592 {
e87fb6a6
JB
11593 if (*temp_string == '\\' && temp_string[1] == '"')
11594 ++temp_string;
11595 else if (*temp_string == '"')
11596 in_quotes = !in_quotes;
11597 else if (!in_quotes)
11598 {
11599 if (*temp_string == '(' && !parens_not_balanced++)
11600 base_string = temp_string;
11601 if (*temp_string == ')')
11602 --parens_not_balanced;
11603 }
24eab124 11604 }
c3332e24 11605
af6bdddf 11606 temp_string = base_string;
c3332e24 11607
24eab124 11608 /* Skip past '(' and whitespace. */
e87fb6a6
JB
11609 gas_assert (*base_string == '(');
11610 ++base_string;
252b5132 11611 if (is_space_char (*base_string))
24eab124 11612 ++base_string;
252b5132 11613
af6bdddf 11614 if (*base_string == ','
4eed87de
AM
11615 || ((i.base_reg = parse_register (base_string, &end_op))
11616 != NULL))
252b5132 11617 {
af6bdddf 11618 displacement_string_end = temp_string;
252b5132 11619
40fb9820 11620 i.types[this_operand].bitfield.baseindex = 1;
252b5132 11621
af6bdddf 11622 if (i.base_reg)
24eab124 11623 {
8a6fb3f9
JB
11624 if (i.base_reg == &bad_reg)
11625 return 0;
24eab124
AM
11626 base_string = end_op;
11627 if (is_space_char (*base_string))
11628 ++base_string;
af6bdddf
AM
11629 }
11630
11631 /* There may be an index reg or scale factor here. */
11632 if (*base_string == ',')
11633 {
11634 ++base_string;
11635 if (is_space_char (*base_string))
11636 ++base_string;
11637
4eed87de
AM
11638 if ((i.index_reg = parse_register (base_string, &end_op))
11639 != NULL)
24eab124 11640 {
8a6fb3f9
JB
11641 if (i.index_reg == &bad_reg)
11642 return 0;
af6bdddf 11643 base_string = end_op;
24eab124
AM
11644 if (is_space_char (*base_string))
11645 ++base_string;
af6bdddf
AM
11646 if (*base_string == ',')
11647 {
11648 ++base_string;
11649 if (is_space_char (*base_string))
11650 ++base_string;
11651 }
e5cb08ac 11652 else if (*base_string != ')')
af6bdddf 11653 {
4eed87de
AM
11654 as_bad (_("expecting `,' or `)' "
11655 "after index register in `%s'"),
af6bdddf
AM
11656 operand_string);
11657 return 0;
11658 }
24eab124 11659 }
af6bdddf 11660 else if (*base_string == REGISTER_PREFIX)
24eab124 11661 {
f76bf5e0
L
11662 end_op = strchr (base_string, ',');
11663 if (end_op)
11664 *end_op = '\0';
af6bdddf 11665 as_bad (_("bad register name `%s'"), base_string);
24eab124
AM
11666 return 0;
11667 }
252b5132 11668
47926f60 11669 /* Check for scale factor. */
551c1ca1 11670 if (*base_string != ')')
af6bdddf 11671 {
551c1ca1
AM
11672 char *end_scale = i386_scale (base_string);
11673
11674 if (!end_scale)
af6bdddf 11675 return 0;
24eab124 11676
551c1ca1 11677 base_string = end_scale;
af6bdddf
AM
11678 if (is_space_char (*base_string))
11679 ++base_string;
11680 if (*base_string != ')')
11681 {
4eed87de
AM
11682 as_bad (_("expecting `)' "
11683 "after scale factor in `%s'"),
af6bdddf
AM
11684 operand_string);
11685 return 0;
11686 }
11687 }
11688 else if (!i.index_reg)
24eab124 11689 {
4eed87de
AM
11690 as_bad (_("expecting index register or scale factor "
11691 "after `,'; got '%c'"),
af6bdddf 11692 *base_string);
24eab124
AM
11693 return 0;
11694 }
11695 }
af6bdddf 11696 else if (*base_string != ')')
24eab124 11697 {
4eed87de
AM
11698 as_bad (_("expecting `,' or `)' "
11699 "after base register in `%s'"),
af6bdddf 11700 operand_string);
24eab124
AM
11701 return 0;
11702 }
c3332e24 11703 }
af6bdddf 11704 else if (*base_string == REGISTER_PREFIX)
c3332e24 11705 {
f76bf5e0
L
11706 end_op = strchr (base_string, ',');
11707 if (end_op)
11708 *end_op = '\0';
af6bdddf 11709 as_bad (_("bad register name `%s'"), base_string);
24eab124 11710 return 0;
c3332e24 11711 }
24eab124
AM
11712 }
11713
11714 /* If there's an expression beginning the operand, parse it,
11715 assuming displacement_string_start and
11716 displacement_string_end are meaningful. */
11717 if (displacement_string_start != displacement_string_end)
11718 {
11719 if (!i386_displacement (displacement_string_start,
11720 displacement_string_end))
11721 return 0;
11722 }
11723
11724 /* Special case for (%dx) while doing input/output op. */
11725 if (i.base_reg
75e5731b
JB
11726 && i.base_reg->reg_type.bitfield.instance == RegD
11727 && i.base_reg->reg_type.bitfield.word
24eab124
AM
11728 && i.index_reg == 0
11729 && i.log2_scale_factor == 0
11730 && i.seg[i.mem_operands] == 0
40fb9820 11731 && !operand_type_check (i.types[this_operand], disp))
24eab124 11732 {
2fb5be8d 11733 i.types[this_operand] = i.base_reg->reg_type;
24eab124
AM
11734 return 1;
11735 }
11736
eecb386c
AM
11737 if (i386_index_check (operand_string) == 0)
11738 return 0;
c48dadc9 11739 i.flags[this_operand] |= Operand_Mem;
8325cc63
JB
11740 if (i.mem_operands == 0)
11741 i.memop1_string = xstrdup (operand_string);
24eab124
AM
11742 i.mem_operands++;
11743 }
11744 else
ce8a8b2f
AM
11745 {
11746 /* It's not a memory operand; argh! */
24eab124
AM
11747 as_bad (_("invalid char %s beginning operand %d `%s'"),
11748 output_invalid (*op_string),
11749 this_operand + 1,
11750 op_string);
11751 return 0;
11752 }
47926f60 11753 return 1; /* Normal return. */
252b5132
RH
11754}
11755\f
fa94de6b
RM
11756/* Calculate the maximum variable size (i.e., excluding fr_fix)
11757 that an rs_machine_dependent frag may reach. */
11758
11759unsigned int
11760i386_frag_max_var (fragS *frag)
11761{
11762 /* The only relaxable frags are for jumps.
11763 Unconditional jumps can grow by 4 bytes and others by 5 bytes. */
11764 gas_assert (frag->fr_type == rs_machine_dependent);
11765 return TYPE_FROM_RELAX_STATE (frag->fr_subtype) == UNCOND_JUMP ? 4 : 5;
11766}
11767
b084df0b
L
11768#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
11769static int
8dcea932 11770elf_symbol_resolved_in_segment_p (symbolS *fr_symbol, offsetT fr_var)
b084df0b
L
11771{
11772 /* STT_GNU_IFUNC symbol must go through PLT. */
11773 if ((symbol_get_bfdsym (fr_symbol)->flags
11774 & BSF_GNU_INDIRECT_FUNCTION) != 0)
11775 return 0;
11776
11777 if (!S_IS_EXTERNAL (fr_symbol))
11778 /* Symbol may be weak or local. */
11779 return !S_IS_WEAK (fr_symbol);
11780
8dcea932
L
11781 /* Global symbols with non-default visibility can't be preempted. */
11782 if (ELF_ST_VISIBILITY (S_GET_OTHER (fr_symbol)) != STV_DEFAULT)
11783 return 1;
11784
11785 if (fr_var != NO_RELOC)
11786 switch ((enum bfd_reloc_code_real) fr_var)
11787 {
11788 case BFD_RELOC_386_PLT32:
11789 case BFD_RELOC_X86_64_PLT32:
33eaf5de 11790 /* Symbol with PLT relocation may be preempted. */
8dcea932
L
11791 return 0;
11792 default:
11793 abort ();
11794 }
11795
b084df0b
L
11796 /* Global symbols with default visibility in a shared library may be
11797 preempted by another definition. */
8dcea932 11798 return !shared;
b084df0b
L
11799}
11800#endif
11801
79d72f45
HL
11802/* Table 3-2. Macro-Fusible Instructions in Haswell Microarchitecture
11803 Note also work for Skylake and Cascadelake.
11804---------------------------------------------------------------------
11805| JCC | ADD/SUB/CMP | INC/DEC | TEST/AND |
11806| ------ | ----------- | ------- | -------- |
11807| Jo | N | N | Y |
11808| Jno | N | N | Y |
11809| Jc/Jb | Y | N | Y |
11810| Jae/Jnb | Y | N | Y |
11811| Je/Jz | Y | Y | Y |
11812| Jne/Jnz | Y | Y | Y |
11813| Jna/Jbe | Y | N | Y |
11814| Ja/Jnbe | Y | N | Y |
11815| Js | N | N | Y |
11816| Jns | N | N | Y |
11817| Jp/Jpe | N | N | Y |
11818| Jnp/Jpo | N | N | Y |
11819| Jl/Jnge | Y | Y | Y |
11820| Jge/Jnl | Y | Y | Y |
11821| Jle/Jng | Y | Y | Y |
11822| Jg/Jnle | Y | Y | Y |
11823--------------------------------------------------------------------- */
11824static int
11825i386_macro_fusible_p (enum mf_cmp_kind mf_cmp, enum mf_jcc_kind mf_jcc)
11826{
11827 if (mf_cmp == mf_cmp_alu_cmp)
11828 return ((mf_jcc >= mf_jcc_jc && mf_jcc <= mf_jcc_jna)
11829 || mf_jcc == mf_jcc_jl || mf_jcc == mf_jcc_jle);
11830 if (mf_cmp == mf_cmp_incdec)
11831 return (mf_jcc == mf_jcc_je || mf_jcc == mf_jcc_jl
11832 || mf_jcc == mf_jcc_jle);
11833 if (mf_cmp == mf_cmp_test_and)
11834 return 1;
11835 return 0;
11836}
11837
e379e5f3
L
11838/* Return the next non-empty frag. */
11839
11840static fragS *
11841i386_next_non_empty_frag (fragS *fragP)
11842{
11843 /* There may be a frag with a ".fill 0" when there is no room in
11844 the current frag for frag_grow in output_insn. */
11845 for (fragP = fragP->fr_next;
11846 (fragP != NULL
11847 && fragP->fr_type == rs_fill
11848 && fragP->fr_fix == 0);
11849 fragP = fragP->fr_next)
11850 ;
11851 return fragP;
11852}
11853
11854/* Return the next jcc frag after BRANCH_PADDING. */
11855
11856static fragS *
79d72f45 11857i386_next_fusible_jcc_frag (fragS *maybe_cmp_fragP, fragS *pad_fragP)
e379e5f3 11858{
79d72f45
HL
11859 fragS *branch_fragP;
11860 if (!pad_fragP)
e379e5f3
L
11861 return NULL;
11862
79d72f45
HL
11863 if (pad_fragP->fr_type == rs_machine_dependent
11864 && (TYPE_FROM_RELAX_STATE (pad_fragP->fr_subtype)
e379e5f3
L
11865 == BRANCH_PADDING))
11866 {
79d72f45
HL
11867 branch_fragP = i386_next_non_empty_frag (pad_fragP);
11868 if (branch_fragP->fr_type != rs_machine_dependent)
e379e5f3 11869 return NULL;
79d72f45
HL
11870 if (TYPE_FROM_RELAX_STATE (branch_fragP->fr_subtype) == COND_JUMP
11871 && i386_macro_fusible_p (maybe_cmp_fragP->tc_frag_data.mf_type,
11872 pad_fragP->tc_frag_data.mf_type))
11873 return branch_fragP;
e379e5f3
L
11874 }
11875
11876 return NULL;
11877}
11878
11879/* Classify BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags. */
11880
11881static void
11882i386_classify_machine_dependent_frag (fragS *fragP)
11883{
11884 fragS *cmp_fragP;
11885 fragS *pad_fragP;
11886 fragS *branch_fragP;
11887 fragS *next_fragP;
11888 unsigned int max_prefix_length;
11889
11890 if (fragP->tc_frag_data.classified)
11891 return;
11892
11893 /* First scan for BRANCH_PADDING and FUSED_JCC_PADDING. Convert
11894 FUSED_JCC_PADDING and merge BRANCH_PADDING. */
11895 for (next_fragP = fragP;
11896 next_fragP != NULL;
11897 next_fragP = next_fragP->fr_next)
11898 {
11899 next_fragP->tc_frag_data.classified = 1;
11900 if (next_fragP->fr_type == rs_machine_dependent)
11901 switch (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype))
11902 {
11903 case BRANCH_PADDING:
11904 /* The BRANCH_PADDING frag must be followed by a branch
11905 frag. */
11906 branch_fragP = i386_next_non_empty_frag (next_fragP);
11907 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
11908 break;
11909 case FUSED_JCC_PADDING:
11910 /* Check if this is a fused jcc:
11911 FUSED_JCC_PADDING
11912 CMP like instruction
11913 BRANCH_PADDING
11914 COND_JUMP
11915 */
11916 cmp_fragP = i386_next_non_empty_frag (next_fragP);
11917 pad_fragP = i386_next_non_empty_frag (cmp_fragP);
79d72f45 11918 branch_fragP = i386_next_fusible_jcc_frag (next_fragP, pad_fragP);
e379e5f3
L
11919 if (branch_fragP)
11920 {
11921 /* The BRANCH_PADDING frag is merged with the
11922 FUSED_JCC_PADDING frag. */
11923 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
11924 /* CMP like instruction size. */
11925 next_fragP->tc_frag_data.cmp_size = cmp_fragP->fr_fix;
11926 frag_wane (pad_fragP);
11927 /* Skip to branch_fragP. */
11928 next_fragP = branch_fragP;
11929 }
11930 else if (next_fragP->tc_frag_data.max_prefix_length)
11931 {
11932 /* Turn FUSED_JCC_PADDING into BRANCH_PREFIX if it isn't
11933 a fused jcc. */
11934 next_fragP->fr_subtype
11935 = ENCODE_RELAX_STATE (BRANCH_PREFIX, 0);
11936 next_fragP->tc_frag_data.max_bytes
11937 = next_fragP->tc_frag_data.max_prefix_length;
11938 /* This will be updated in the BRANCH_PREFIX scan. */
11939 next_fragP->tc_frag_data.max_prefix_length = 0;
11940 }
11941 else
11942 frag_wane (next_fragP);
11943 break;
11944 }
11945 }
11946
11947 /* Stop if there is no BRANCH_PREFIX. */
11948 if (!align_branch_prefix_size)
11949 return;
11950
11951 /* Scan for BRANCH_PREFIX. */
11952 for (; fragP != NULL; fragP = fragP->fr_next)
11953 {
11954 if (fragP->fr_type != rs_machine_dependent
11955 || (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
11956 != BRANCH_PREFIX))
11957 continue;
11958
11959 /* Count all BRANCH_PREFIX frags before BRANCH_PADDING and
11960 COND_JUMP_PREFIX. */
11961 max_prefix_length = 0;
11962 for (next_fragP = fragP;
11963 next_fragP != NULL;
11964 next_fragP = next_fragP->fr_next)
11965 {
11966 if (next_fragP->fr_type == rs_fill)
11967 /* Skip rs_fill frags. */
11968 continue;
11969 else if (next_fragP->fr_type != rs_machine_dependent)
11970 /* Stop for all other frags. */
11971 break;
11972
11973 /* rs_machine_dependent frags. */
11974 if (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
11975 == BRANCH_PREFIX)
11976 {
11977 /* Count BRANCH_PREFIX frags. */
11978 if (max_prefix_length >= MAX_FUSED_JCC_PADDING_SIZE)
11979 {
11980 max_prefix_length = MAX_FUSED_JCC_PADDING_SIZE;
11981 frag_wane (next_fragP);
11982 }
11983 else
11984 max_prefix_length
11985 += next_fragP->tc_frag_data.max_bytes;
11986 }
11987 else if ((TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
11988 == BRANCH_PADDING)
11989 || (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
11990 == FUSED_JCC_PADDING))
11991 {
11992 /* Stop at BRANCH_PADDING and FUSED_JCC_PADDING. */
11993 fragP->tc_frag_data.u.padding_fragP = next_fragP;
11994 break;
11995 }
11996 else
11997 /* Stop for other rs_machine_dependent frags. */
11998 break;
11999 }
12000
12001 fragP->tc_frag_data.max_prefix_length = max_prefix_length;
12002
12003 /* Skip to the next frag. */
12004 fragP = next_fragP;
12005 }
12006}
12007
12008/* Compute padding size for
12009
12010 FUSED_JCC_PADDING
12011 CMP like instruction
12012 BRANCH_PADDING
12013 COND_JUMP/UNCOND_JUMP
12014
12015 or
12016
12017 BRANCH_PADDING
12018 COND_JUMP/UNCOND_JUMP
12019 */
12020
12021static int
12022i386_branch_padding_size (fragS *fragP, offsetT address)
12023{
12024 unsigned int offset, size, padding_size;
12025 fragS *branch_fragP = fragP->tc_frag_data.u.branch_fragP;
12026
12027 /* The start address of the BRANCH_PADDING or FUSED_JCC_PADDING frag. */
12028 if (!address)
12029 address = fragP->fr_address;
12030 address += fragP->fr_fix;
12031
12032 /* CMP like instrunction size. */
12033 size = fragP->tc_frag_data.cmp_size;
12034
12035 /* The base size of the branch frag. */
12036 size += branch_fragP->fr_fix;
12037
12038 /* Add opcode and displacement bytes for the rs_machine_dependent
12039 branch frag. */
12040 if (branch_fragP->fr_type == rs_machine_dependent)
12041 size += md_relax_table[branch_fragP->fr_subtype].rlx_length;
12042
12043 /* Check if branch is within boundary and doesn't end at the last
12044 byte. */
12045 offset = address & ((1U << align_branch_power) - 1);
12046 if ((offset + size) >= (1U << align_branch_power))
12047 /* Padding needed to avoid crossing boundary. */
12048 padding_size = (1U << align_branch_power) - offset;
12049 else
12050 /* No padding needed. */
12051 padding_size = 0;
12052
12053 /* The return value may be saved in tc_frag_data.length which is
12054 unsigned byte. */
12055 if (!fits_in_unsigned_byte (padding_size))
12056 abort ();
12057
12058 return padding_size;
12059}
12060
12061/* i386_generic_table_relax_frag()
12062
12063 Handle BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags to
12064 grow/shrink padding to align branch frags. Hand others to
12065 relax_frag(). */
12066
12067long
12068i386_generic_table_relax_frag (segT segment, fragS *fragP, long stretch)
12069{
12070 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12071 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
12072 {
12073 long padding_size = i386_branch_padding_size (fragP, 0);
12074 long grow = padding_size - fragP->tc_frag_data.length;
12075
12076 /* When the BRANCH_PREFIX frag is used, the computed address
12077 must match the actual address and there should be no padding. */
12078 if (fragP->tc_frag_data.padding_address
12079 && (fragP->tc_frag_data.padding_address != fragP->fr_address
12080 || padding_size))
12081 abort ();
12082
12083 /* Update the padding size. */
12084 if (grow)
12085 fragP->tc_frag_data.length = padding_size;
12086
12087 return grow;
12088 }
12089 else if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12090 {
12091 fragS *padding_fragP, *next_fragP;
12092 long padding_size, left_size, last_size;
12093
12094 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
12095 if (!padding_fragP)
12096 /* Use the padding set by the leading BRANCH_PREFIX frag. */
12097 return (fragP->tc_frag_data.length
12098 - fragP->tc_frag_data.last_length);
12099
12100 /* Compute the relative address of the padding frag in the very
12101 first time where the BRANCH_PREFIX frag sizes are zero. */
12102 if (!fragP->tc_frag_data.padding_address)
12103 fragP->tc_frag_data.padding_address
12104 = padding_fragP->fr_address - (fragP->fr_address - stretch);
12105
12106 /* First update the last length from the previous interation. */
12107 left_size = fragP->tc_frag_data.prefix_length;
12108 for (next_fragP = fragP;
12109 next_fragP != padding_fragP;
12110 next_fragP = next_fragP->fr_next)
12111 if (next_fragP->fr_type == rs_machine_dependent
12112 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12113 == BRANCH_PREFIX))
12114 {
12115 if (left_size)
12116 {
12117 int max = next_fragP->tc_frag_data.max_bytes;
12118 if (max)
12119 {
12120 int size;
12121 if (max > left_size)
12122 size = left_size;
12123 else
12124 size = max;
12125 left_size -= size;
12126 next_fragP->tc_frag_data.last_length = size;
12127 }
12128 }
12129 else
12130 next_fragP->tc_frag_data.last_length = 0;
12131 }
12132
12133 /* Check the padding size for the padding frag. */
12134 padding_size = i386_branch_padding_size
12135 (padding_fragP, (fragP->fr_address
12136 + fragP->tc_frag_data.padding_address));
12137
12138 last_size = fragP->tc_frag_data.prefix_length;
12139 /* Check if there is change from the last interation. */
12140 if (padding_size == last_size)
12141 {
12142 /* Update the expected address of the padding frag. */
12143 padding_fragP->tc_frag_data.padding_address
12144 = (fragP->fr_address + padding_size
12145 + fragP->tc_frag_data.padding_address);
12146 return 0;
12147 }
12148
12149 if (padding_size > fragP->tc_frag_data.max_prefix_length)
12150 {
12151 /* No padding if there is no sufficient room. Clear the
12152 expected address of the padding frag. */
12153 padding_fragP->tc_frag_data.padding_address = 0;
12154 padding_size = 0;
12155 }
12156 else
12157 /* Store the expected address of the padding frag. */
12158 padding_fragP->tc_frag_data.padding_address
12159 = (fragP->fr_address + padding_size
12160 + fragP->tc_frag_data.padding_address);
12161
12162 fragP->tc_frag_data.prefix_length = padding_size;
12163
12164 /* Update the length for the current interation. */
12165 left_size = padding_size;
12166 for (next_fragP = fragP;
12167 next_fragP != padding_fragP;
12168 next_fragP = next_fragP->fr_next)
12169 if (next_fragP->fr_type == rs_machine_dependent
12170 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12171 == BRANCH_PREFIX))
12172 {
12173 if (left_size)
12174 {
12175 int max = next_fragP->tc_frag_data.max_bytes;
12176 if (max)
12177 {
12178 int size;
12179 if (max > left_size)
12180 size = left_size;
12181 else
12182 size = max;
12183 left_size -= size;
12184 next_fragP->tc_frag_data.length = size;
12185 }
12186 }
12187 else
12188 next_fragP->tc_frag_data.length = 0;
12189 }
12190
12191 return (fragP->tc_frag_data.length
12192 - fragP->tc_frag_data.last_length);
12193 }
12194 return relax_frag (segment, fragP, stretch);
12195}
12196
ee7fcc42
AM
12197/* md_estimate_size_before_relax()
12198
12199 Called just before relax() for rs_machine_dependent frags. The x86
12200 assembler uses these frags to handle variable size jump
12201 instructions.
12202
12203 Any symbol that is now undefined will not become defined.
12204 Return the correct fr_subtype in the frag.
12205 Return the initial "guess for variable size of frag" to caller.
12206 The guess is actually the growth beyond the fixed part. Whatever
12207 we do to grow the fixed or variable part contributes to our
12208 returned value. */
12209
252b5132 12210int
7016a5d5 12211md_estimate_size_before_relax (fragS *fragP, segT segment)
252b5132 12212{
e379e5f3
L
12213 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12214 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX
12215 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
12216 {
12217 i386_classify_machine_dependent_frag (fragP);
12218 return fragP->tc_frag_data.length;
12219 }
12220
252b5132 12221 /* We've already got fragP->fr_subtype right; all we have to do is
b98ef147
AM
12222 check for un-relaxable symbols. On an ELF system, we can't relax
12223 an externally visible symbol, because it may be overridden by a
12224 shared library. */
12225 if (S_GET_SEGMENT (fragP->fr_symbol) != segment
6d249963 12226#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12227 || (IS_ELF
8dcea932
L
12228 && !elf_symbol_resolved_in_segment_p (fragP->fr_symbol,
12229 fragP->fr_var))
fbeb56a4
DK
12230#endif
12231#if defined (OBJ_COFF) && defined (TE_PE)
7ab9ffdd 12232 || (OUTPUT_FLAVOR == bfd_target_coff_flavour
fbeb56a4 12233 && S_IS_WEAK (fragP->fr_symbol))
b98ef147
AM
12234#endif
12235 )
252b5132 12236 {
b98ef147
AM
12237 /* Symbol is undefined in this segment, or we need to keep a
12238 reloc so that weak symbols can be overridden. */
12239 int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
f86103b7 12240 enum bfd_reloc_code_real reloc_type;
ee7fcc42
AM
12241 unsigned char *opcode;
12242 int old_fr_fix;
eb19308f 12243 fixS *fixP = NULL;
f6af82bd 12244
ee7fcc42 12245 if (fragP->fr_var != NO_RELOC)
1e9cc1c2 12246 reloc_type = (enum bfd_reloc_code_real) fragP->fr_var;
b98ef147 12247 else if (size == 2)
f6af82bd 12248 reloc_type = BFD_RELOC_16_PCREL;
bd7ab16b 12249#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1ef3994a
JB
12250 else if (fragP->tc_frag_data.code64 && fragP->fr_offset == 0
12251 && need_plt32_p (fragP->fr_symbol))
bd7ab16b
L
12252 reloc_type = BFD_RELOC_X86_64_PLT32;
12253#endif
f6af82bd
AM
12254 else
12255 reloc_type = BFD_RELOC_32_PCREL;
252b5132 12256
ee7fcc42
AM
12257 old_fr_fix = fragP->fr_fix;
12258 opcode = (unsigned char *) fragP->fr_opcode;
12259
fddf5b5b 12260 switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
252b5132 12261 {
fddf5b5b
AM
12262 case UNCOND_JUMP:
12263 /* Make jmp (0xeb) a (d)word displacement jump. */
47926f60 12264 opcode[0] = 0xe9;
252b5132 12265 fragP->fr_fix += size;
eb19308f
JB
12266 fixP = fix_new (fragP, old_fr_fix, size,
12267 fragP->fr_symbol,
12268 fragP->fr_offset, 1,
12269 reloc_type);
252b5132
RH
12270 break;
12271
fddf5b5b 12272 case COND_JUMP86:
412167cb
AM
12273 if (size == 2
12274 && (!no_cond_jump_promotion || fragP->fr_var != NO_RELOC))
fddf5b5b
AM
12275 {
12276 /* Negate the condition, and branch past an
12277 unconditional jump. */
12278 opcode[0] ^= 1;
12279 opcode[1] = 3;
12280 /* Insert an unconditional jump. */
12281 opcode[2] = 0xe9;
12282 /* We added two extra opcode bytes, and have a two byte
12283 offset. */
12284 fragP->fr_fix += 2 + 2;
062cd5e7
AS
12285 fix_new (fragP, old_fr_fix + 2, 2,
12286 fragP->fr_symbol,
12287 fragP->fr_offset, 1,
12288 reloc_type);
fddf5b5b
AM
12289 break;
12290 }
12291 /* Fall through. */
12292
12293 case COND_JUMP:
412167cb
AM
12294 if (no_cond_jump_promotion && fragP->fr_var == NO_RELOC)
12295 {
12296 fragP->fr_fix += 1;
3e02c1cc
AM
12297 fixP = fix_new (fragP, old_fr_fix, 1,
12298 fragP->fr_symbol,
12299 fragP->fr_offset, 1,
12300 BFD_RELOC_8_PCREL);
12301 fixP->fx_signed = 1;
412167cb
AM
12302 break;
12303 }
93c2a809 12304
24eab124 12305 /* This changes the byte-displacement jump 0x7N
fddf5b5b 12306 to the (d)word-displacement jump 0x0f,0x8N. */
252b5132 12307 opcode[1] = opcode[0] + 0x10;
f6af82bd 12308 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
47926f60
KH
12309 /* We've added an opcode byte. */
12310 fragP->fr_fix += 1 + size;
eb19308f
JB
12311 fixP = fix_new (fragP, old_fr_fix + 1, size,
12312 fragP->fr_symbol,
12313 fragP->fr_offset, 1,
12314 reloc_type);
252b5132 12315 break;
fddf5b5b
AM
12316
12317 default:
12318 BAD_CASE (fragP->fr_subtype);
12319 break;
252b5132 12320 }
eb19308f
JB
12321
12322 /* All jumps handled here are signed, but don't unconditionally use a
12323 signed limit check for 32 and 16 bit jumps as we want to allow wrap
12324 around at 4G (outside of 64-bit mode) and 64k. */
12325 if (size == 4 && flag_code == CODE_64BIT)
12326 fixP->fx_signed = 1;
12327
252b5132 12328 frag_wane (fragP);
ee7fcc42 12329 return fragP->fr_fix - old_fr_fix;
252b5132 12330 }
93c2a809 12331
93c2a809
AM
12332 /* Guess size depending on current relax state. Initially the relax
12333 state will correspond to a short jump and we return 1, because
12334 the variable part of the frag (the branch offset) is one byte
12335 long. However, we can relax a section more than once and in that
12336 case we must either set fr_subtype back to the unrelaxed state,
12337 or return the value for the appropriate branch. */
12338 return md_relax_table[fragP->fr_subtype].rlx_length;
ee7fcc42
AM
12339}
12340
47926f60
KH
12341/* Called after relax() is finished.
12342
12343 In: Address of frag.
12344 fr_type == rs_machine_dependent.
12345 fr_subtype is what the address relaxed to.
12346
12347 Out: Any fixSs and constants are set up.
12348 Caller will turn frag into a ".space 0". */
12349
252b5132 12350void
7016a5d5
TG
12351md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec ATTRIBUTE_UNUSED,
12352 fragS *fragP)
252b5132 12353{
29b0f896 12354 unsigned char *opcode;
252b5132 12355 unsigned char *where_to_put_displacement = NULL;
847f7ad4
AM
12356 offsetT target_address;
12357 offsetT opcode_address;
252b5132 12358 unsigned int extension = 0;
847f7ad4 12359 offsetT displacement_from_opcode_start;
252b5132 12360
e379e5f3
L
12361 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12362 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING
12363 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12364 {
12365 /* Generate nop padding. */
12366 unsigned int size = fragP->tc_frag_data.length;
12367 if (size)
12368 {
12369 if (size > fragP->tc_frag_data.max_bytes)
12370 abort ();
12371
12372 if (flag_debug)
12373 {
12374 const char *msg;
12375 const char *branch = "branch";
12376 const char *prefix = "";
12377 fragS *padding_fragP;
12378 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
12379 == BRANCH_PREFIX)
12380 {
12381 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
12382 switch (fragP->tc_frag_data.default_prefix)
12383 {
12384 default:
12385 abort ();
12386 break;
12387 case CS_PREFIX_OPCODE:
12388 prefix = " cs";
12389 break;
12390 case DS_PREFIX_OPCODE:
12391 prefix = " ds";
12392 break;
12393 case ES_PREFIX_OPCODE:
12394 prefix = " es";
12395 break;
12396 case FS_PREFIX_OPCODE:
12397 prefix = " fs";
12398 break;
12399 case GS_PREFIX_OPCODE:
12400 prefix = " gs";
12401 break;
12402 case SS_PREFIX_OPCODE:
12403 prefix = " ss";
12404 break;
12405 }
12406 if (padding_fragP)
12407 msg = _("%s:%u: add %d%s at 0x%llx to align "
12408 "%s within %d-byte boundary\n");
12409 else
12410 msg = _("%s:%u: add additional %d%s at 0x%llx to "
12411 "align %s within %d-byte boundary\n");
12412 }
12413 else
12414 {
12415 padding_fragP = fragP;
12416 msg = _("%s:%u: add %d%s-byte nop at 0x%llx to align "
12417 "%s within %d-byte boundary\n");
12418 }
12419
12420 if (padding_fragP)
12421 switch (padding_fragP->tc_frag_data.branch_type)
12422 {
12423 case align_branch_jcc:
12424 branch = "jcc";
12425 break;
12426 case align_branch_fused:
12427 branch = "fused jcc";
12428 break;
12429 case align_branch_jmp:
12430 branch = "jmp";
12431 break;
12432 case align_branch_call:
12433 branch = "call";
12434 break;
12435 case align_branch_indirect:
12436 branch = "indiret branch";
12437 break;
12438 case align_branch_ret:
12439 branch = "ret";
12440 break;
12441 default:
12442 break;
12443 }
12444
12445 fprintf (stdout, msg,
12446 fragP->fr_file, fragP->fr_line, size, prefix,
12447 (long long) fragP->fr_address, branch,
12448 1 << align_branch_power);
12449 }
12450 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12451 memset (fragP->fr_opcode,
12452 fragP->tc_frag_data.default_prefix, size);
12453 else
12454 i386_generate_nops (fragP, (char *) fragP->fr_opcode,
12455 size, 0);
12456 fragP->fr_fix += size;
12457 }
12458 return;
12459 }
12460
252b5132
RH
12461 opcode = (unsigned char *) fragP->fr_opcode;
12462
47926f60 12463 /* Address we want to reach in file space. */
252b5132 12464 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
252b5132 12465
47926f60 12466 /* Address opcode resides at in file space. */
252b5132
RH
12467 opcode_address = fragP->fr_address + fragP->fr_fix;
12468
47926f60 12469 /* Displacement from opcode start to fill into instruction. */
252b5132
RH
12470 displacement_from_opcode_start = target_address - opcode_address;
12471
fddf5b5b 12472 if ((fragP->fr_subtype & BIG) == 0)
252b5132 12473 {
47926f60
KH
12474 /* Don't have to change opcode. */
12475 extension = 1; /* 1 opcode + 1 displacement */
252b5132 12476 where_to_put_displacement = &opcode[1];
fddf5b5b
AM
12477 }
12478 else
12479 {
12480 if (no_cond_jump_promotion
12481 && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
4eed87de
AM
12482 as_warn_where (fragP->fr_file, fragP->fr_line,
12483 _("long jump required"));
252b5132 12484
fddf5b5b
AM
12485 switch (fragP->fr_subtype)
12486 {
12487 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
12488 extension = 4; /* 1 opcode + 4 displacement */
12489 opcode[0] = 0xe9;
12490 where_to_put_displacement = &opcode[1];
12491 break;
252b5132 12492
fddf5b5b
AM
12493 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
12494 extension = 2; /* 1 opcode + 2 displacement */
12495 opcode[0] = 0xe9;
12496 where_to_put_displacement = &opcode[1];
12497 break;
252b5132 12498
fddf5b5b
AM
12499 case ENCODE_RELAX_STATE (COND_JUMP, BIG):
12500 case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
12501 extension = 5; /* 2 opcode + 4 displacement */
12502 opcode[1] = opcode[0] + 0x10;
12503 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12504 where_to_put_displacement = &opcode[2];
12505 break;
252b5132 12506
fddf5b5b
AM
12507 case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
12508 extension = 3; /* 2 opcode + 2 displacement */
12509 opcode[1] = opcode[0] + 0x10;
12510 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12511 where_to_put_displacement = &opcode[2];
12512 break;
252b5132 12513
fddf5b5b
AM
12514 case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
12515 extension = 4;
12516 opcode[0] ^= 1;
12517 opcode[1] = 3;
12518 opcode[2] = 0xe9;
12519 where_to_put_displacement = &opcode[3];
12520 break;
12521
12522 default:
12523 BAD_CASE (fragP->fr_subtype);
12524 break;
12525 }
252b5132 12526 }
fddf5b5b 12527
7b81dfbb
AJ
12528 /* If size if less then four we are sure that the operand fits,
12529 but if it's 4, then it could be that the displacement is larger
12530 then -/+ 2GB. */
12531 if (DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype) == 4
12532 && object_64bit
12533 && ((addressT) (displacement_from_opcode_start - extension
4eed87de
AM
12534 + ((addressT) 1 << 31))
12535 > (((addressT) 2 << 31) - 1)))
7b81dfbb
AJ
12536 {
12537 as_bad_where (fragP->fr_file, fragP->fr_line,
12538 _("jump target out of range"));
12539 /* Make us emit 0. */
12540 displacement_from_opcode_start = extension;
12541 }
47926f60 12542 /* Now put displacement after opcode. */
252b5132
RH
12543 md_number_to_chars ((char *) where_to_put_displacement,
12544 (valueT) (displacement_from_opcode_start - extension),
fddf5b5b 12545 DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
252b5132
RH
12546 fragP->fr_fix += extension;
12547}
12548\f
7016a5d5 12549/* Apply a fixup (fixP) to segment data, once it has been determined
252b5132
RH
12550 by our caller that we have all the info we need to fix it up.
12551
7016a5d5
TG
12552 Parameter valP is the pointer to the value of the bits.
12553
252b5132
RH
12554 On the 386, immediates, displacements, and data pointers are all in
12555 the same (little-endian) format, so we don't need to care about which
12556 we are handling. */
12557
94f592af 12558void
7016a5d5 12559md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 12560{
94f592af 12561 char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
c6682705 12562 valueT value = *valP;
252b5132 12563
f86103b7 12564#if !defined (TE_Mach)
93382f6d
AM
12565 if (fixP->fx_pcrel)
12566 {
12567 switch (fixP->fx_r_type)
12568 {
5865bb77
ILT
12569 default:
12570 break;
12571
d6ab8113
JB
12572 case BFD_RELOC_64:
12573 fixP->fx_r_type = BFD_RELOC_64_PCREL;
12574 break;
93382f6d 12575 case BFD_RELOC_32:
ae8887b5 12576 case BFD_RELOC_X86_64_32S:
93382f6d
AM
12577 fixP->fx_r_type = BFD_RELOC_32_PCREL;
12578 break;
12579 case BFD_RELOC_16:
12580 fixP->fx_r_type = BFD_RELOC_16_PCREL;
12581 break;
12582 case BFD_RELOC_8:
12583 fixP->fx_r_type = BFD_RELOC_8_PCREL;
12584 break;
12585 }
12586 }
252b5132 12587
a161fe53 12588 if (fixP->fx_addsy != NULL
31312f95 12589 && (fixP->fx_r_type == BFD_RELOC_32_PCREL
d6ab8113 12590 || fixP->fx_r_type == BFD_RELOC_64_PCREL
31312f95 12591 || fixP->fx_r_type == BFD_RELOC_16_PCREL
d258b828 12592 || fixP->fx_r_type == BFD_RELOC_8_PCREL)
31312f95 12593 && !use_rela_relocations)
252b5132 12594 {
31312f95
AM
12595 /* This is a hack. There should be a better way to handle this.
12596 This covers for the fact that bfd_install_relocation will
12597 subtract the current location (for partial_inplace, PC relative
12598 relocations); see more below. */
252b5132 12599#ifndef OBJ_AOUT
718ddfc0 12600 if (IS_ELF
252b5132
RH
12601#ifdef TE_PE
12602 || OUTPUT_FLAVOR == bfd_target_coff_flavour
12603#endif
12604 )
12605 value += fixP->fx_where + fixP->fx_frag->fr_address;
12606#endif
12607#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12608 if (IS_ELF)
252b5132 12609 {
6539b54b 12610 segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
2f66722d 12611
6539b54b 12612 if ((sym_seg == seg
2f66722d 12613 || (symbol_section_p (fixP->fx_addsy)
6539b54b 12614 && sym_seg != absolute_section))
af65af87 12615 && !generic_force_reloc (fixP))
2f66722d
AM
12616 {
12617 /* Yes, we add the values in twice. This is because
6539b54b
AM
12618 bfd_install_relocation subtracts them out again. I think
12619 bfd_install_relocation is broken, but I don't dare change
2f66722d
AM
12620 it. FIXME. */
12621 value += fixP->fx_where + fixP->fx_frag->fr_address;
12622 }
252b5132
RH
12623 }
12624#endif
12625#if defined (OBJ_COFF) && defined (TE_PE)
977cdf5a
NC
12626 /* For some reason, the PE format does not store a
12627 section address offset for a PC relative symbol. */
12628 if (S_GET_SEGMENT (fixP->fx_addsy) != seg
7be1c489 12629 || S_IS_WEAK (fixP->fx_addsy))
252b5132
RH
12630 value += md_pcrel_from (fixP);
12631#endif
12632 }
fbeb56a4 12633#if defined (OBJ_COFF) && defined (TE_PE)
f01c1a09
NC
12634 if (fixP->fx_addsy != NULL
12635 && S_IS_WEAK (fixP->fx_addsy)
12636 /* PR 16858: Do not modify weak function references. */
12637 && ! fixP->fx_pcrel)
fbeb56a4 12638 {
296a8689
NC
12639#if !defined (TE_PEP)
12640 /* For x86 PE weak function symbols are neither PC-relative
12641 nor do they set S_IS_FUNCTION. So the only reliable way
12642 to detect them is to check the flags of their containing
12643 section. */
12644 if (S_GET_SEGMENT (fixP->fx_addsy) != NULL
12645 && S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_CODE)
12646 ;
12647 else
12648#endif
fbeb56a4
DK
12649 value -= S_GET_VALUE (fixP->fx_addsy);
12650 }
12651#endif
252b5132
RH
12652
12653 /* Fix a few things - the dynamic linker expects certain values here,
0234cb7c 12654 and we must not disappoint it. */
252b5132 12655#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12656 if (IS_ELF && fixP->fx_addsy)
47926f60
KH
12657 switch (fixP->fx_r_type)
12658 {
12659 case BFD_RELOC_386_PLT32:
3e73aa7c 12660 case BFD_RELOC_X86_64_PLT32:
b9519cfe
L
12661 /* Make the jump instruction point to the address of the operand.
12662 At runtime we merely add the offset to the actual PLT entry.
12663 NB: Subtract the offset size only for jump instructions. */
12664 if (fixP->fx_pcrel)
12665 value = -4;
47926f60 12666 break;
31312f95 12667
13ae64f3
JJ
12668 case BFD_RELOC_386_TLS_GD:
12669 case BFD_RELOC_386_TLS_LDM:
13ae64f3 12670 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
12671 case BFD_RELOC_386_TLS_IE:
12672 case BFD_RELOC_386_TLS_GOTIE:
67a4f2b7 12673 case BFD_RELOC_386_TLS_GOTDESC:
bffbf940
JJ
12674 case BFD_RELOC_X86_64_TLSGD:
12675 case BFD_RELOC_X86_64_TLSLD:
12676 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7 12677 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
00f7efb6
JJ
12678 value = 0; /* Fully resolved at runtime. No addend. */
12679 /* Fallthrough */
12680 case BFD_RELOC_386_TLS_LE:
12681 case BFD_RELOC_386_TLS_LDO_32:
12682 case BFD_RELOC_386_TLS_LE_32:
12683 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 12684 case BFD_RELOC_X86_64_DTPOFF64:
00f7efb6 12685 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113 12686 case BFD_RELOC_X86_64_TPOFF64:
00f7efb6
JJ
12687 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12688 break;
12689
67a4f2b7
AO
12690 case BFD_RELOC_386_TLS_DESC_CALL:
12691 case BFD_RELOC_X86_64_TLSDESC_CALL:
12692 value = 0; /* Fully resolved at runtime. No addend. */
12693 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12694 fixP->fx_done = 0;
12695 return;
12696
47926f60
KH
12697 case BFD_RELOC_VTABLE_INHERIT:
12698 case BFD_RELOC_VTABLE_ENTRY:
12699 fixP->fx_done = 0;
94f592af 12700 return;
47926f60
KH
12701
12702 default:
12703 break;
12704 }
12705#endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) */
a442cac5
JB
12706
12707 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
12708 if (!object_64bit)
12709 value = extend_to_32bit_address (value);
12710
c6682705 12711 *valP = value;
f86103b7 12712#endif /* !defined (TE_Mach) */
3e73aa7c 12713
3e73aa7c 12714 /* Are we finished with this relocation now? */
c6682705 12715 if (fixP->fx_addsy == NULL)
b8188555
JB
12716 {
12717 fixP->fx_done = 1;
12718 switch (fixP->fx_r_type)
12719 {
12720 case BFD_RELOC_X86_64_32S:
12721 fixP->fx_signed = 1;
12722 break;
12723
12724 default:
12725 break;
12726 }
12727 }
fbeb56a4
DK
12728#if defined (OBJ_COFF) && defined (TE_PE)
12729 else if (fixP->fx_addsy != NULL && S_IS_WEAK (fixP->fx_addsy))
12730 {
12731 fixP->fx_done = 0;
12732 /* Remember value for tc_gen_reloc. */
12733 fixP->fx_addnumber = value;
12734 /* Clear out the frag for now. */
12735 value = 0;
12736 }
12737#endif
3e73aa7c
JH
12738 else if (use_rela_relocations)
12739 {
46fb6d5a
JB
12740 if (!disallow_64bit_reloc || fixP->fx_r_type == NO_RELOC)
12741 fixP->fx_no_overflow = 1;
062cd5e7
AS
12742 /* Remember value for tc_gen_reloc. */
12743 fixP->fx_addnumber = value;
3e73aa7c
JH
12744 value = 0;
12745 }
f86103b7 12746
94f592af 12747 md_number_to_chars (p, value, fixP->fx_size);
252b5132 12748}
252b5132 12749\f
6d4af3c2 12750const char *
499ac353 12751md_atof (int type, char *litP, int *sizeP)
252b5132 12752{
499ac353
NC
12753 /* This outputs the LITTLENUMs in REVERSE order;
12754 in accord with the bigendian 386. */
5b7c81bd 12755 return ieee_md_atof (type, litP, sizeP, false);
252b5132
RH
12756}
12757\f
2d545b82 12758static char output_invalid_buf[sizeof (unsigned char) * 2 + 6];
252b5132 12759
252b5132 12760static char *
e3bb37b5 12761output_invalid (int c)
252b5132 12762{
3882b010 12763 if (ISPRINT (c))
f9f21a03
L
12764 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
12765 "'%c'", c);
252b5132 12766 else
f9f21a03 12767 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
2d545b82 12768 "(0x%x)", (unsigned char) c);
252b5132
RH
12769 return output_invalid_buf;
12770}
12771
8a6fb3f9
JB
12772/* Verify that @r can be used in the current context. */
12773
5b7c81bd 12774static bool check_register (const reg_entry *r)
8a6fb3f9
JB
12775{
12776 if (allow_pseudo_reg)
5b7c81bd 12777 return true;
8a6fb3f9
JB
12778
12779 if (operand_type_all_zero (&r->reg_type))
5b7c81bd 12780 return false;
8a6fb3f9
JB
12781
12782 if ((r->reg_type.bitfield.dword
12783 || (r->reg_type.bitfield.class == SReg && r->reg_num > 3)
12784 || r->reg_type.bitfield.class == RegCR
22e00a3f 12785 || r->reg_type.bitfield.class == RegDR)
8a6fb3f9 12786 && !cpu_arch_flags.bitfield.cpui386)
5b7c81bd 12787 return false;
8a6fb3f9 12788
22e00a3f
JB
12789 if (r->reg_type.bitfield.class == RegTR
12790 && (flag_code == CODE_64BIT
12791 || !cpu_arch_flags.bitfield.cpui386
12792 || cpu_arch_isa_flags.bitfield.cpui586
12793 || cpu_arch_isa_flags.bitfield.cpui686))
5b7c81bd 12794 return false;
22e00a3f 12795
8a6fb3f9 12796 if (r->reg_type.bitfield.class == RegMMX && !cpu_arch_flags.bitfield.cpummx)
5b7c81bd 12797 return false;
8a6fb3f9
JB
12798
12799 if (!cpu_arch_flags.bitfield.cpuavx512f)
12800 {
12801 if (r->reg_type.bitfield.zmmword
12802 || r->reg_type.bitfield.class == RegMask)
5b7c81bd 12803 return false;
8a6fb3f9
JB
12804
12805 if (!cpu_arch_flags.bitfield.cpuavx)
12806 {
12807 if (r->reg_type.bitfield.ymmword)
5b7c81bd 12808 return false;
8a6fb3f9
JB
12809
12810 if (!cpu_arch_flags.bitfield.cpusse && r->reg_type.bitfield.xmmword)
5b7c81bd 12811 return false;
8a6fb3f9
JB
12812 }
12813 }
12814
260cd341
LC
12815 if (r->reg_type.bitfield.tmmword
12816 && (!cpu_arch_flags.bitfield.cpuamx_tile
12817 || flag_code != CODE_64BIT))
5b7c81bd 12818 return false;
260cd341 12819
8a6fb3f9 12820 if (r->reg_type.bitfield.class == RegBND && !cpu_arch_flags.bitfield.cpumpx)
5b7c81bd 12821 return false;
8a6fb3f9
JB
12822
12823 /* Don't allow fake index register unless allow_index_reg isn't 0. */
12824 if (!allow_index_reg && r->reg_num == RegIZ)
5b7c81bd 12825 return false;
8a6fb3f9
JB
12826
12827 /* Upper 16 vector registers are only available with VREX in 64bit
12828 mode, and require EVEX encoding. */
12829 if (r->reg_flags & RegVRex)
12830 {
12831 if (!cpu_arch_flags.bitfield.cpuavx512f
12832 || flag_code != CODE_64BIT)
5b7c81bd 12833 return false;
8a6fb3f9 12834
da4977e0
JB
12835 if (i.vec_encoding == vex_encoding_default)
12836 i.vec_encoding = vex_encoding_evex;
12837 else if (i.vec_encoding != vex_encoding_evex)
12838 i.vec_encoding = vex_encoding_error;
8a6fb3f9
JB
12839 }
12840
12841 if (((r->reg_flags & (RegRex64 | RegRex)) || r->reg_type.bitfield.qword)
12842 && (!cpu_arch_flags.bitfield.cpulm || r->reg_type.bitfield.class != RegCR)
12843 && flag_code != CODE_64BIT)
5b7c81bd 12844 return false;
8a6fb3f9
JB
12845
12846 if (r->reg_type.bitfield.class == SReg && r->reg_num == RegFlat
12847 && !intel_syntax)
5b7c81bd 12848 return false;
8a6fb3f9 12849
5b7c81bd 12850 return true;
8a6fb3f9
JB
12851}
12852
af6bdddf 12853/* REG_STRING starts *before* REGISTER_PREFIX. */
252b5132
RH
12854
12855static const reg_entry *
4d1bb795 12856parse_real_register (char *reg_string, char **end_op)
252b5132 12857{
af6bdddf
AM
12858 char *s = reg_string;
12859 char *p;
252b5132
RH
12860 char reg_name_given[MAX_REG_NAME_SIZE + 1];
12861 const reg_entry *r;
12862
12863 /* Skip possible REGISTER_PREFIX and possible whitespace. */
12864 if (*s == REGISTER_PREFIX)
12865 ++s;
12866
12867 if (is_space_char (*s))
12868 ++s;
12869
12870 p = reg_name_given;
af6bdddf 12871 while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
252b5132
RH
12872 {
12873 if (p >= reg_name_given + MAX_REG_NAME_SIZE)
af6bdddf
AM
12874 return (const reg_entry *) NULL;
12875 s++;
252b5132
RH
12876 }
12877
6588847e
DN
12878 /* For naked regs, make sure that we are not dealing with an identifier.
12879 This prevents confusing an identifier like `eax_var' with register
12880 `eax'. */
12881 if (allow_naked_reg && identifier_chars[(unsigned char) *s])
12882 return (const reg_entry *) NULL;
12883
af6bdddf 12884 *end_op = s;
252b5132 12885
629310ab 12886 r = (const reg_entry *) str_hash_find (reg_hash, reg_name_given);
252b5132 12887
5f47d35b 12888 /* Handle floating point regs, allowing spaces in the (i) part. */
6288d05f 12889 if (r == reg_st0)
5f47d35b 12890 {
0e0eea78
JB
12891 if (!cpu_arch_flags.bitfield.cpu8087
12892 && !cpu_arch_flags.bitfield.cpu287
af32b722
JB
12893 && !cpu_arch_flags.bitfield.cpu387
12894 && !allow_pseudo_reg)
0e0eea78
JB
12895 return (const reg_entry *) NULL;
12896
5f47d35b
AM
12897 if (is_space_char (*s))
12898 ++s;
12899 if (*s == '(')
12900 {
af6bdddf 12901 ++s;
5f47d35b
AM
12902 if (is_space_char (*s))
12903 ++s;
12904 if (*s >= '0' && *s <= '7')
12905 {
db557034 12906 int fpr = *s - '0';
af6bdddf 12907 ++s;
5f47d35b
AM
12908 if (is_space_char (*s))
12909 ++s;
12910 if (*s == ')')
12911 {
12912 *end_op = s + 1;
6288d05f 12913 know (r[fpr].reg_num == fpr);
db557034 12914 return r + fpr;
5f47d35b 12915 }
5f47d35b 12916 }
47926f60 12917 /* We have "%st(" then garbage. */
5f47d35b
AM
12918 return (const reg_entry *) NULL;
12919 }
12920 }
12921
8a6fb3f9 12922 return r && check_register (r) ? r : NULL;
252b5132 12923}
4d1bb795
JB
12924
12925/* REG_STRING starts *before* REGISTER_PREFIX. */
12926
12927static const reg_entry *
12928parse_register (char *reg_string, char **end_op)
12929{
12930 const reg_entry *r;
12931
12932 if (*reg_string == REGISTER_PREFIX || allow_naked_reg)
12933 r = parse_real_register (reg_string, end_op);
12934 else
12935 r = NULL;
12936 if (!r)
12937 {
12938 char *save = input_line_pointer;
12939 char c;
12940 symbolS *symbolP;
12941
12942 input_line_pointer = reg_string;
d02603dc 12943 c = get_symbol_name (&reg_string);
4d1bb795 12944 symbolP = symbol_find (reg_string);
64d23078
JB
12945 while (symbolP && S_GET_SEGMENT (symbolP) != reg_section)
12946 {
12947 const expressionS *e = symbol_get_value_expression(symbolP);
12948
12949 if (e->X_op != O_symbol || e->X_add_number)
12950 break;
12951 symbolP = e->X_add_symbol;
12952 }
4d1bb795
JB
12953 if (symbolP && S_GET_SEGMENT (symbolP) == reg_section)
12954 {
12955 const expressionS *e = symbol_get_value_expression (symbolP);
12956
3b55a1d0
JB
12957 know (e->X_op == O_register);
12958 know (e->X_add_number >= 0
12959 && (valueT) e->X_add_number < i386_regtab_size);
12960 r = i386_regtab + e->X_add_number;
12961 if (!check_register (r))
8a6fb3f9 12962 {
3b55a1d0
JB
12963 as_bad (_("register '%s%s' cannot be used here"),
12964 register_prefix, r->reg_name);
12965 r = &bad_reg;
8a6fb3f9 12966 }
3b55a1d0 12967 *end_op = input_line_pointer;
4d1bb795
JB
12968 }
12969 *input_line_pointer = c;
12970 input_line_pointer = save;
12971 }
12972 return r;
12973}
12974
12975int
12976i386_parse_name (char *name, expressionS *e, char *nextcharP)
12977{
4faaa10f 12978 const reg_entry *r = NULL;
4d1bb795
JB
12979 char *end = input_line_pointer;
12980
12981 *end = *nextcharP;
4faaa10f
JB
12982 if (*name == REGISTER_PREFIX || allow_naked_reg)
12983 r = parse_real_register (name, &input_line_pointer);
4d1bb795
JB
12984 if (r && end <= input_line_pointer)
12985 {
12986 *nextcharP = *input_line_pointer;
12987 *input_line_pointer = 0;
8a6fb3f9
JB
12988 if (r != &bad_reg)
12989 {
12990 e->X_op = O_register;
12991 e->X_add_number = r - i386_regtab;
12992 }
12993 else
12994 e->X_op = O_illegal;
4d1bb795
JB
12995 return 1;
12996 }
12997 input_line_pointer = end;
12998 *end = 0;
ee86248c 12999 return intel_syntax ? i386_intel_parse_name (name, e) : 0;
4d1bb795
JB
13000}
13001
13002void
13003md_operand (expressionS *e)
13004{
ee86248c
JB
13005 char *end;
13006 const reg_entry *r;
4d1bb795 13007
ee86248c
JB
13008 switch (*input_line_pointer)
13009 {
13010 case REGISTER_PREFIX:
13011 r = parse_real_register (input_line_pointer, &end);
4d1bb795
JB
13012 if (r)
13013 {
13014 e->X_op = O_register;
13015 e->X_add_number = r - i386_regtab;
13016 input_line_pointer = end;
13017 }
ee86248c
JB
13018 break;
13019
13020 case '[':
9c2799c2 13021 gas_assert (intel_syntax);
ee86248c
JB
13022 end = input_line_pointer++;
13023 expression (e);
13024 if (*input_line_pointer == ']')
13025 {
13026 ++input_line_pointer;
13027 e->X_op_symbol = make_expr_symbol (e);
13028 e->X_add_symbol = NULL;
13029 e->X_add_number = 0;
13030 e->X_op = O_index;
13031 }
13032 else
13033 {
13034 e->X_op = O_absent;
13035 input_line_pointer = end;
13036 }
13037 break;
4d1bb795
JB
13038 }
13039}
13040
252b5132 13041\f
4cc782b5 13042#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
b6f8c7c4 13043const char *md_shortopts = "kVQ:sqnO::";
252b5132 13044#else
b6f8c7c4 13045const char *md_shortopts = "qnO::";
252b5132 13046#endif
6e0b89ee 13047
3e73aa7c 13048#define OPTION_32 (OPTION_MD_BASE + 0)
b3b91714
AM
13049#define OPTION_64 (OPTION_MD_BASE + 1)
13050#define OPTION_DIVIDE (OPTION_MD_BASE + 2)
9103f4f4
L
13051#define OPTION_MARCH (OPTION_MD_BASE + 3)
13052#define OPTION_MTUNE (OPTION_MD_BASE + 4)
1efbbeb4
L
13053#define OPTION_MMNEMONIC (OPTION_MD_BASE + 5)
13054#define OPTION_MSYNTAX (OPTION_MD_BASE + 6)
13055#define OPTION_MINDEX_REG (OPTION_MD_BASE + 7)
13056#define OPTION_MNAKED_REG (OPTION_MD_BASE + 8)
bd5dea88 13057#define OPTION_MRELAX_RELOCATIONS (OPTION_MD_BASE + 9)
c0f3af97 13058#define OPTION_MSSE2AVX (OPTION_MD_BASE + 10)
daf50ae7 13059#define OPTION_MSSE_CHECK (OPTION_MD_BASE + 11)
7bab8ab5
JB
13060#define OPTION_MOPERAND_CHECK (OPTION_MD_BASE + 12)
13061#define OPTION_MAVXSCALAR (OPTION_MD_BASE + 13)
13062#define OPTION_X32 (OPTION_MD_BASE + 14)
7e8b059b 13063#define OPTION_MADD_BND_PREFIX (OPTION_MD_BASE + 15)
43234a1e
L
13064#define OPTION_MEVEXLIG (OPTION_MD_BASE + 16)
13065#define OPTION_MEVEXWIG (OPTION_MD_BASE + 17)
167ad85b 13066#define OPTION_MBIG_OBJ (OPTION_MD_BASE + 18)
d1982f93 13067#define OPTION_MOMIT_LOCK_PREFIX (OPTION_MD_BASE + 19)
d3d3c6db 13068#define OPTION_MEVEXRCIG (OPTION_MD_BASE + 20)
8dcea932 13069#define OPTION_MSHARED (OPTION_MD_BASE + 21)
5db04b09
L
13070#define OPTION_MAMD64 (OPTION_MD_BASE + 22)
13071#define OPTION_MINTEL64 (OPTION_MD_BASE + 23)
e4e00185 13072#define OPTION_MFENCE_AS_LOCK_ADD (OPTION_MD_BASE + 24)
b4a3a7b4 13073#define OPTION_X86_USED_NOTE (OPTION_MD_BASE + 25)
03751133 13074#define OPTION_MVEXWIG (OPTION_MD_BASE + 26)
e379e5f3
L
13075#define OPTION_MALIGN_BRANCH_BOUNDARY (OPTION_MD_BASE + 27)
13076#define OPTION_MALIGN_BRANCH_PREFIX_SIZE (OPTION_MD_BASE + 28)
13077#define OPTION_MALIGN_BRANCH (OPTION_MD_BASE + 29)
76cf450b 13078#define OPTION_MBRANCHES_WITH_32B_BOUNDARIES (OPTION_MD_BASE + 30)
ae531041
L
13079#define OPTION_MLFENCE_AFTER_LOAD (OPTION_MD_BASE + 31)
13080#define OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH (OPTION_MD_BASE + 32)
13081#define OPTION_MLFENCE_BEFORE_RET (OPTION_MD_BASE + 33)
c8480b58 13082#define OPTION_MUSE_UNALIGNED_VECTOR_MOVE (OPTION_MD_BASE + 34)
b3b91714 13083
99ad8390
NC
13084struct option md_longopts[] =
13085{
3e73aa7c 13086 {"32", no_argument, NULL, OPTION_32},
321098a5 13087#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 13088 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c 13089 {"64", no_argument, NULL, OPTION_64},
351f65ca
L
13090#endif
13091#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 13092 {"x32", no_argument, NULL, OPTION_X32},
8dcea932 13093 {"mshared", no_argument, NULL, OPTION_MSHARED},
b4a3a7b4 13094 {"mx86-used-note", required_argument, NULL, OPTION_X86_USED_NOTE},
6e0b89ee 13095#endif
b3b91714 13096 {"divide", no_argument, NULL, OPTION_DIVIDE},
9103f4f4
L
13097 {"march", required_argument, NULL, OPTION_MARCH},
13098 {"mtune", required_argument, NULL, OPTION_MTUNE},
1efbbeb4
L
13099 {"mmnemonic", required_argument, NULL, OPTION_MMNEMONIC},
13100 {"msyntax", required_argument, NULL, OPTION_MSYNTAX},
13101 {"mindex-reg", no_argument, NULL, OPTION_MINDEX_REG},
13102 {"mnaked-reg", no_argument, NULL, OPTION_MNAKED_REG},
c0f3af97 13103 {"msse2avx", no_argument, NULL, OPTION_MSSE2AVX},
c8480b58 13104 {"muse-unaligned-vector-move", no_argument, NULL, OPTION_MUSE_UNALIGNED_VECTOR_MOVE},
daf50ae7 13105 {"msse-check", required_argument, NULL, OPTION_MSSE_CHECK},
7bab8ab5 13106 {"moperand-check", required_argument, NULL, OPTION_MOPERAND_CHECK},
539f890d 13107 {"mavxscalar", required_argument, NULL, OPTION_MAVXSCALAR},
03751133 13108 {"mvexwig", required_argument, NULL, OPTION_MVEXWIG},
7e8b059b 13109 {"madd-bnd-prefix", no_argument, NULL, OPTION_MADD_BND_PREFIX},
43234a1e
L
13110 {"mevexlig", required_argument, NULL, OPTION_MEVEXLIG},
13111 {"mevexwig", required_argument, NULL, OPTION_MEVEXWIG},
167ad85b
TG
13112# if defined (TE_PE) || defined (TE_PEP)
13113 {"mbig-obj", no_argument, NULL, OPTION_MBIG_OBJ},
13114#endif
d1982f93 13115 {"momit-lock-prefix", required_argument, NULL, OPTION_MOMIT_LOCK_PREFIX},
e4e00185 13116 {"mfence-as-lock-add", required_argument, NULL, OPTION_MFENCE_AS_LOCK_ADD},
0cb4071e 13117 {"mrelax-relocations", required_argument, NULL, OPTION_MRELAX_RELOCATIONS},
d3d3c6db 13118 {"mevexrcig", required_argument, NULL, OPTION_MEVEXRCIG},
e379e5f3
L
13119 {"malign-branch-boundary", required_argument, NULL, OPTION_MALIGN_BRANCH_BOUNDARY},
13120 {"malign-branch-prefix-size", required_argument, NULL, OPTION_MALIGN_BRANCH_PREFIX_SIZE},
13121 {"malign-branch", required_argument, NULL, OPTION_MALIGN_BRANCH},
76cf450b 13122 {"mbranches-within-32B-boundaries", no_argument, NULL, OPTION_MBRANCHES_WITH_32B_BOUNDARIES},
ae531041
L
13123 {"mlfence-after-load", required_argument, NULL, OPTION_MLFENCE_AFTER_LOAD},
13124 {"mlfence-before-indirect-branch", required_argument, NULL,
13125 OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH},
13126 {"mlfence-before-ret", required_argument, NULL, OPTION_MLFENCE_BEFORE_RET},
5db04b09
L
13127 {"mamd64", no_argument, NULL, OPTION_MAMD64},
13128 {"mintel64", no_argument, NULL, OPTION_MINTEL64},
252b5132
RH
13129 {NULL, no_argument, NULL, 0}
13130};
13131size_t md_longopts_size = sizeof (md_longopts);
13132
13133int
17b9d67d 13134md_parse_option (int c, const char *arg)
252b5132 13135{
91d6fa6a 13136 unsigned int j;
e379e5f3 13137 char *arch, *next, *saved, *type;
9103f4f4 13138
252b5132
RH
13139 switch (c)
13140 {
12b55ccc
L
13141 case 'n':
13142 optimize_align_code = 0;
13143 break;
13144
a38cf1db
AM
13145 case 'q':
13146 quiet_warnings = 1;
252b5132
RH
13147 break;
13148
13149#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
a38cf1db
AM
13150 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
13151 should be emitted or not. FIXME: Not implemented. */
13152 case 'Q':
d4693039
JB
13153 if ((arg[0] != 'y' && arg[0] != 'n') || arg[1])
13154 return 0;
252b5132
RH
13155 break;
13156
13157 /* -V: SVR4 argument to print version ID. */
13158 case 'V':
13159 print_version_id ();
13160 break;
13161
a38cf1db
AM
13162 /* -k: Ignore for FreeBSD compatibility. */
13163 case 'k':
252b5132 13164 break;
4cc782b5
ILT
13165
13166 case 's':
13167 /* -s: On i386 Solaris, this tells the native assembler to use
29b0f896 13168 .stab instead of .stab.excl. We always use .stab anyhow. */
4cc782b5 13169 break;
8dcea932
L
13170
13171 case OPTION_MSHARED:
13172 shared = 1;
13173 break;
b4a3a7b4
L
13174
13175 case OPTION_X86_USED_NOTE:
13176 if (strcasecmp (arg, "yes") == 0)
13177 x86_used_note = 1;
13178 else if (strcasecmp (arg, "no") == 0)
13179 x86_used_note = 0;
13180 else
13181 as_fatal (_("invalid -mx86-used-note= option: `%s'"), arg);
13182 break;
13183
13184
99ad8390 13185#endif
321098a5 13186#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 13187 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c
JH
13188 case OPTION_64:
13189 {
13190 const char **list, **l;
13191
3e73aa7c
JH
13192 list = bfd_target_list ();
13193 for (l = list; *l != NULL; l++)
08dedd66 13194 if (startswith (*l, "elf64-x86-64")
99ad8390
NC
13195 || strcmp (*l, "coff-x86-64") == 0
13196 || strcmp (*l, "pe-x86-64") == 0
d382c579
TG
13197 || strcmp (*l, "pei-x86-64") == 0
13198 || strcmp (*l, "mach-o-x86-64") == 0)
6e0b89ee
AM
13199 {
13200 default_arch = "x86_64";
13201 break;
13202 }
3e73aa7c 13203 if (*l == NULL)
2b5d6a91 13204 as_fatal (_("no compiled in support for x86_64"));
3e73aa7c
JH
13205 free (list);
13206 }
13207 break;
13208#endif
252b5132 13209
351f65ca 13210#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 13211 case OPTION_X32:
351f65ca
L
13212 if (IS_ELF)
13213 {
13214 const char **list, **l;
13215
13216 list = bfd_target_list ();
13217 for (l = list; *l != NULL; l++)
08dedd66 13218 if (startswith (*l, "elf32-x86-64"))
351f65ca
L
13219 {
13220 default_arch = "x86_64:32";
13221 break;
13222 }
13223 if (*l == NULL)
2b5d6a91 13224 as_fatal (_("no compiled in support for 32bit x86_64"));
351f65ca
L
13225 free (list);
13226 }
13227 else
13228 as_fatal (_("32bit x86_64 is only supported for ELF"));
13229 break;
13230#endif
13231
6e0b89ee
AM
13232 case OPTION_32:
13233 default_arch = "i386";
13234 break;
13235
b3b91714
AM
13236 case OPTION_DIVIDE:
13237#ifdef SVR4_COMMENT_CHARS
13238 {
13239 char *n, *t;
13240 const char *s;
13241
add39d23 13242 n = XNEWVEC (char, strlen (i386_comment_chars) + 1);
b3b91714
AM
13243 t = n;
13244 for (s = i386_comment_chars; *s != '\0'; s++)
13245 if (*s != '/')
13246 *t++ = *s;
13247 *t = '\0';
13248 i386_comment_chars = n;
13249 }
13250#endif
13251 break;
13252
9103f4f4 13253 case OPTION_MARCH:
293f5f65
L
13254 saved = xstrdup (arg);
13255 arch = saved;
13256 /* Allow -march=+nosse. */
13257 if (*arch == '+')
13258 arch++;
6305a203 13259 do
9103f4f4 13260 {
6305a203 13261 if (*arch == '.')
2b5d6a91 13262 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
13263 next = strchr (arch, '+');
13264 if (next)
13265 *next++ = '\0';
91d6fa6a 13266 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 13267 {
ae89daec
JB
13268 if (arch == saved && cpu_arch[j].type != PROCESSOR_NONE
13269 && strcmp (arch, cpu_arch[j].name) == 0)
ccc9c027 13270 {
6305a203 13271 /* Processor. */
ae89daec 13272 if (! cpu_arch[j].enable.bitfield.cpui386)
1ded5609
JB
13273 continue;
13274
91d6fa6a 13275 cpu_arch_name = cpu_arch[j].name;
d92c7521 13276 free (cpu_sub_arch_name);
6305a203 13277 cpu_sub_arch_name = NULL;
ae89daec 13278 cpu_arch_flags = cpu_arch[j].enable;
91d6fa6a 13279 cpu_arch_isa = cpu_arch[j].type;
ae89daec 13280 cpu_arch_isa_flags = cpu_arch[j].enable;
6305a203
L
13281 if (!cpu_arch_tune_set)
13282 {
13283 cpu_arch_tune = cpu_arch_isa;
13284 cpu_arch_tune_flags = cpu_arch_isa_flags;
13285 }
13286 break;
13287 }
ae89daec
JB
13288 else if (cpu_arch[j].type == PROCESSOR_NONE
13289 && strcmp (arch, cpu_arch[j].name) == 0
13290 && !cpu_flags_all_zero (&cpu_arch[j].enable))
6305a203 13291 {
33eaf5de 13292 /* ISA extension. */
6305a203 13293 i386_cpu_flags flags;
309d3373 13294
293f5f65 13295 flags = cpu_flags_or (cpu_arch_flags,
ae89daec 13296 cpu_arch[j].enable);
81486035 13297
5b64d091 13298 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
6305a203 13299 {
ae89daec 13300 extend_cpu_sub_arch_name (arch);
6305a203 13301 cpu_arch_flags = flags;
a586129e 13302 cpu_arch_isa_flags = flags;
6305a203 13303 }
0089dace
L
13304 else
13305 cpu_arch_isa_flags
13306 = cpu_flags_or (cpu_arch_isa_flags,
ae89daec 13307 cpu_arch[j].enable);
6305a203 13308 break;
ccc9c027 13309 }
9103f4f4 13310 }
6305a203 13311
ae89daec 13312 if (j >= ARRAY_SIZE (cpu_arch) && startswith (arch, "no"))
293f5f65 13313 {
33eaf5de 13314 /* Disable an ISA extension. */
ae89daec
JB
13315 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13316 if (cpu_arch[j].type == PROCESSOR_NONE
13317 && strcmp (arch + 2, cpu_arch[j].name) == 0)
293f5f65
L
13318 {
13319 i386_cpu_flags flags;
13320
13321 flags = cpu_flags_and_not (cpu_arch_flags,
ae89daec 13322 cpu_arch[j].disable);
293f5f65
L
13323 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
13324 {
8180707f 13325 extend_cpu_sub_arch_name (arch);
293f5f65
L
13326 cpu_arch_flags = flags;
13327 cpu_arch_isa_flags = flags;
13328 }
13329 break;
13330 }
293f5f65
L
13331 }
13332
91d6fa6a 13333 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 13334 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
13335
13336 arch = next;
9103f4f4 13337 }
293f5f65
L
13338 while (next != NULL);
13339 free (saved);
9103f4f4
L
13340 break;
13341
13342 case OPTION_MTUNE:
13343 if (*arg == '.')
2b5d6a91 13344 as_fatal (_("invalid -mtune= option: `%s'"), arg);
91d6fa6a 13345 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 13346 {
ae89daec
JB
13347 if (cpu_arch[j].type != PROCESSOR_NONE
13348 && strcmp (arg, cpu_arch[j].name) == 0)
9103f4f4 13349 {
ccc9c027 13350 cpu_arch_tune_set = 1;
91d6fa6a 13351 cpu_arch_tune = cpu_arch [j].type;
ae89daec 13352 cpu_arch_tune_flags = cpu_arch[j].enable;
9103f4f4
L
13353 break;
13354 }
13355 }
91d6fa6a 13356 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 13357 as_fatal (_("invalid -mtune= option: `%s'"), arg);
9103f4f4
L
13358 break;
13359
1efbbeb4
L
13360 case OPTION_MMNEMONIC:
13361 if (strcasecmp (arg, "att") == 0)
13362 intel_mnemonic = 0;
13363 else if (strcasecmp (arg, "intel") == 0)
13364 intel_mnemonic = 1;
13365 else
2b5d6a91 13366 as_fatal (_("invalid -mmnemonic= option: `%s'"), arg);
1efbbeb4
L
13367 break;
13368
13369 case OPTION_MSYNTAX:
13370 if (strcasecmp (arg, "att") == 0)
13371 intel_syntax = 0;
13372 else if (strcasecmp (arg, "intel") == 0)
13373 intel_syntax = 1;
13374 else
2b5d6a91 13375 as_fatal (_("invalid -msyntax= option: `%s'"), arg);
1efbbeb4
L
13376 break;
13377
13378 case OPTION_MINDEX_REG:
13379 allow_index_reg = 1;
13380 break;
13381
13382 case OPTION_MNAKED_REG:
13383 allow_naked_reg = 1;
13384 break;
13385
c0f3af97
L
13386 case OPTION_MSSE2AVX:
13387 sse2avx = 1;
13388 break;
13389
c8480b58
L
13390 case OPTION_MUSE_UNALIGNED_VECTOR_MOVE:
13391 use_unaligned_vector_move = 1;
13392 break;
13393
daf50ae7
L
13394 case OPTION_MSSE_CHECK:
13395 if (strcasecmp (arg, "error") == 0)
7bab8ab5 13396 sse_check = check_error;
daf50ae7 13397 else if (strcasecmp (arg, "warning") == 0)
7bab8ab5 13398 sse_check = check_warning;
daf50ae7 13399 else if (strcasecmp (arg, "none") == 0)
7bab8ab5 13400 sse_check = check_none;
daf50ae7 13401 else
2b5d6a91 13402 as_fatal (_("invalid -msse-check= option: `%s'"), arg);
daf50ae7
L
13403 break;
13404
7bab8ab5
JB
13405 case OPTION_MOPERAND_CHECK:
13406 if (strcasecmp (arg, "error") == 0)
13407 operand_check = check_error;
13408 else if (strcasecmp (arg, "warning") == 0)
13409 operand_check = check_warning;
13410 else if (strcasecmp (arg, "none") == 0)
13411 operand_check = check_none;
13412 else
13413 as_fatal (_("invalid -moperand-check= option: `%s'"), arg);
13414 break;
13415
539f890d
L
13416 case OPTION_MAVXSCALAR:
13417 if (strcasecmp (arg, "128") == 0)
13418 avxscalar = vex128;
13419 else if (strcasecmp (arg, "256") == 0)
13420 avxscalar = vex256;
13421 else
2b5d6a91 13422 as_fatal (_("invalid -mavxscalar= option: `%s'"), arg);
539f890d
L
13423 break;
13424
03751133
L
13425 case OPTION_MVEXWIG:
13426 if (strcmp (arg, "0") == 0)
40c9c8de 13427 vexwig = vexw0;
03751133 13428 else if (strcmp (arg, "1") == 0)
40c9c8de 13429 vexwig = vexw1;
03751133
L
13430 else
13431 as_fatal (_("invalid -mvexwig= option: `%s'"), arg);
13432 break;
13433
7e8b059b
L
13434 case OPTION_MADD_BND_PREFIX:
13435 add_bnd_prefix = 1;
13436 break;
13437
43234a1e
L
13438 case OPTION_MEVEXLIG:
13439 if (strcmp (arg, "128") == 0)
13440 evexlig = evexl128;
13441 else if (strcmp (arg, "256") == 0)
13442 evexlig = evexl256;
13443 else if (strcmp (arg, "512") == 0)
13444 evexlig = evexl512;
13445 else
13446 as_fatal (_("invalid -mevexlig= option: `%s'"), arg);
13447 break;
13448
d3d3c6db
IT
13449 case OPTION_MEVEXRCIG:
13450 if (strcmp (arg, "rne") == 0)
13451 evexrcig = rne;
13452 else if (strcmp (arg, "rd") == 0)
13453 evexrcig = rd;
13454 else if (strcmp (arg, "ru") == 0)
13455 evexrcig = ru;
13456 else if (strcmp (arg, "rz") == 0)
13457 evexrcig = rz;
13458 else
13459 as_fatal (_("invalid -mevexrcig= option: `%s'"), arg);
13460 break;
13461
43234a1e
L
13462 case OPTION_MEVEXWIG:
13463 if (strcmp (arg, "0") == 0)
13464 evexwig = evexw0;
13465 else if (strcmp (arg, "1") == 0)
13466 evexwig = evexw1;
13467 else
13468 as_fatal (_("invalid -mevexwig= option: `%s'"), arg);
13469 break;
13470
167ad85b
TG
13471# if defined (TE_PE) || defined (TE_PEP)
13472 case OPTION_MBIG_OBJ:
13473 use_big_obj = 1;
13474 break;
13475#endif
13476
d1982f93 13477 case OPTION_MOMIT_LOCK_PREFIX:
d022bddd
IT
13478 if (strcasecmp (arg, "yes") == 0)
13479 omit_lock_prefix = 1;
13480 else if (strcasecmp (arg, "no") == 0)
13481 omit_lock_prefix = 0;
13482 else
13483 as_fatal (_("invalid -momit-lock-prefix= option: `%s'"), arg);
13484 break;
13485
e4e00185
AS
13486 case OPTION_MFENCE_AS_LOCK_ADD:
13487 if (strcasecmp (arg, "yes") == 0)
13488 avoid_fence = 1;
13489 else if (strcasecmp (arg, "no") == 0)
13490 avoid_fence = 0;
13491 else
13492 as_fatal (_("invalid -mfence-as-lock-add= option: `%s'"), arg);
13493 break;
13494
ae531041
L
13495 case OPTION_MLFENCE_AFTER_LOAD:
13496 if (strcasecmp (arg, "yes") == 0)
13497 lfence_after_load = 1;
13498 else if (strcasecmp (arg, "no") == 0)
13499 lfence_after_load = 0;
13500 else
13501 as_fatal (_("invalid -mlfence-after-load= option: `%s'"), arg);
13502 break;
13503
13504 case OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH:
13505 if (strcasecmp (arg, "all") == 0)
a09f656b 13506 {
13507 lfence_before_indirect_branch = lfence_branch_all;
13508 if (lfence_before_ret == lfence_before_ret_none)
13509 lfence_before_ret = lfence_before_ret_shl;
13510 }
ae531041
L
13511 else if (strcasecmp (arg, "memory") == 0)
13512 lfence_before_indirect_branch = lfence_branch_memory;
13513 else if (strcasecmp (arg, "register") == 0)
13514 lfence_before_indirect_branch = lfence_branch_register;
13515 else if (strcasecmp (arg, "none") == 0)
13516 lfence_before_indirect_branch = lfence_branch_none;
13517 else
13518 as_fatal (_("invalid -mlfence-before-indirect-branch= option: `%s'"),
13519 arg);
13520 break;
13521
13522 case OPTION_MLFENCE_BEFORE_RET:
13523 if (strcasecmp (arg, "or") == 0)
13524 lfence_before_ret = lfence_before_ret_or;
13525 else if (strcasecmp (arg, "not") == 0)
13526 lfence_before_ret = lfence_before_ret_not;
a09f656b 13527 else if (strcasecmp (arg, "shl") == 0 || strcasecmp (arg, "yes") == 0)
13528 lfence_before_ret = lfence_before_ret_shl;
ae531041
L
13529 else if (strcasecmp (arg, "none") == 0)
13530 lfence_before_ret = lfence_before_ret_none;
13531 else
13532 as_fatal (_("invalid -mlfence-before-ret= option: `%s'"),
13533 arg);
13534 break;
13535
0cb4071e
L
13536 case OPTION_MRELAX_RELOCATIONS:
13537 if (strcasecmp (arg, "yes") == 0)
13538 generate_relax_relocations = 1;
13539 else if (strcasecmp (arg, "no") == 0)
13540 generate_relax_relocations = 0;
13541 else
13542 as_fatal (_("invalid -mrelax-relocations= option: `%s'"), arg);
13543 break;
13544
e379e5f3
L
13545 case OPTION_MALIGN_BRANCH_BOUNDARY:
13546 {
13547 char *end;
13548 long int align = strtoul (arg, &end, 0);
13549 if (*end == '\0')
13550 {
13551 if (align == 0)
13552 {
13553 align_branch_power = 0;
13554 break;
13555 }
13556 else if (align >= 16)
13557 {
13558 int align_power;
13559 for (align_power = 0;
13560 (align & 1) == 0;
13561 align >>= 1, align_power++)
13562 continue;
13563 /* Limit alignment power to 31. */
13564 if (align == 1 && align_power < 32)
13565 {
13566 align_branch_power = align_power;
13567 break;
13568 }
13569 }
13570 }
13571 as_fatal (_("invalid -malign-branch-boundary= value: %s"), arg);
13572 }
13573 break;
13574
13575 case OPTION_MALIGN_BRANCH_PREFIX_SIZE:
13576 {
13577 char *end;
13578 int align = strtoul (arg, &end, 0);
13579 /* Some processors only support 5 prefixes. */
13580 if (*end == '\0' && align >= 0 && align < 6)
13581 {
13582 align_branch_prefix_size = align;
13583 break;
13584 }
13585 as_fatal (_("invalid -malign-branch-prefix-size= value: %s"),
13586 arg);
13587 }
13588 break;
13589
13590 case OPTION_MALIGN_BRANCH:
13591 align_branch = 0;
13592 saved = xstrdup (arg);
13593 type = saved;
13594 do
13595 {
13596 next = strchr (type, '+');
13597 if (next)
13598 *next++ = '\0';
13599 if (strcasecmp (type, "jcc") == 0)
13600 align_branch |= align_branch_jcc_bit;
13601 else if (strcasecmp (type, "fused") == 0)
13602 align_branch |= align_branch_fused_bit;
13603 else if (strcasecmp (type, "jmp") == 0)
13604 align_branch |= align_branch_jmp_bit;
13605 else if (strcasecmp (type, "call") == 0)
13606 align_branch |= align_branch_call_bit;
13607 else if (strcasecmp (type, "ret") == 0)
13608 align_branch |= align_branch_ret_bit;
13609 else if (strcasecmp (type, "indirect") == 0)
13610 align_branch |= align_branch_indirect_bit;
13611 else
13612 as_fatal (_("invalid -malign-branch= option: `%s'"), arg);
13613 type = next;
13614 }
13615 while (next != NULL);
13616 free (saved);
13617 break;
13618
76cf450b
L
13619 case OPTION_MBRANCHES_WITH_32B_BOUNDARIES:
13620 align_branch_power = 5;
13621 align_branch_prefix_size = 5;
13622 align_branch = (align_branch_jcc_bit
13623 | align_branch_fused_bit
13624 | align_branch_jmp_bit);
13625 break;
13626
5db04b09 13627 case OPTION_MAMD64:
4b5aaf5f 13628 isa64 = amd64;
5db04b09
L
13629 break;
13630
13631 case OPTION_MINTEL64:
4b5aaf5f 13632 isa64 = intel64;
5db04b09
L
13633 break;
13634
b6f8c7c4
L
13635 case 'O':
13636 if (arg == NULL)
13637 {
13638 optimize = 1;
13639 /* Turn off -Os. */
13640 optimize_for_space = 0;
13641 }
13642 else if (*arg == 's')
13643 {
13644 optimize_for_space = 1;
13645 /* Turn on all encoding optimizations. */
41fd2579 13646 optimize = INT_MAX;
b6f8c7c4
L
13647 }
13648 else
13649 {
13650 optimize = atoi (arg);
13651 /* Turn off -Os. */
13652 optimize_for_space = 0;
13653 }
13654 break;
13655
252b5132
RH
13656 default:
13657 return 0;
13658 }
13659 return 1;
13660}
13661
8a2c8fef
L
13662#define MESSAGE_TEMPLATE \
13663" "
13664
293f5f65
L
13665static char *
13666output_message (FILE *stream, char *p, char *message, char *start,
13667 int *left_p, const char *name, int len)
13668{
13669 int size = sizeof (MESSAGE_TEMPLATE);
13670 int left = *left_p;
13671
13672 /* Reserve 2 spaces for ", " or ",\0" */
13673 left -= len + 2;
13674
13675 /* Check if there is any room. */
13676 if (left >= 0)
13677 {
13678 if (p != start)
13679 {
13680 *p++ = ',';
13681 *p++ = ' ';
13682 }
13683 p = mempcpy (p, name, len);
13684 }
13685 else
13686 {
13687 /* Output the current message now and start a new one. */
13688 *p++ = ',';
13689 *p = '\0';
13690 fprintf (stream, "%s\n", message);
13691 p = start;
13692 left = size - (start - message) - len - 2;
13693
13694 gas_assert (left >= 0);
13695
13696 p = mempcpy (p, name, len);
13697 }
13698
13699 *left_p = left;
13700 return p;
13701}
13702
8a2c8fef 13703static void
1ded5609 13704show_arch (FILE *stream, int ext, int check)
8a2c8fef
L
13705{
13706 static char message[] = MESSAGE_TEMPLATE;
13707 char *start = message + 27;
13708 char *p;
13709 int size = sizeof (MESSAGE_TEMPLATE);
13710 int left;
13711 const char *name;
13712 int len;
13713 unsigned int j;
13714
13715 p = start;
13716 left = size - (start - message);
3ce2ebcf
JB
13717
13718 if (!ext && check)
13719 {
13720 p = output_message (stream, p, message, start, &left,
13721 STRING_COMMA_LEN ("default"));
f68697e8
JB
13722 p = output_message (stream, p, message, start, &left,
13723 STRING_COMMA_LEN ("push"));
13724 p = output_message (stream, p, message, start, &left,
13725 STRING_COMMA_LEN ("pop"));
3ce2ebcf
JB
13726 }
13727
8a2c8fef
L
13728 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13729 {
13730 /* Should it be skipped? */
13731 if (cpu_arch [j].skip)
13732 continue;
13733
13734 name = cpu_arch [j].name;
13735 len = cpu_arch [j].len;
ae89daec 13736 if (cpu_arch[j].type == PROCESSOR_NONE)
8a2c8fef
L
13737 {
13738 /* It is an extension. Skip if we aren't asked to show it. */
ae89daec 13739 if (!ext || cpu_flags_all_zero (&cpu_arch[j].enable))
8a2c8fef
L
13740 continue;
13741 }
13742 else if (ext)
13743 {
13744 /* It is an processor. Skip if we show only extension. */
13745 continue;
13746 }
ae89daec 13747 else if (check && ! cpu_arch[j].enable.bitfield.cpui386)
1ded5609
JB
13748 {
13749 /* It is an impossible processor - skip. */
13750 continue;
13751 }
8a2c8fef 13752
293f5f65 13753 p = output_message (stream, p, message, start, &left, name, len);
8a2c8fef
L
13754 }
13755
293f5f65
L
13756 /* Display disabled extensions. */
13757 if (ext)
ae89daec 13758 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
293f5f65 13759 {
ae89daec
JB
13760 char *str;
13761
13762 if (cpu_arch[j].type != PROCESSOR_NONE
13763 || !cpu_flags_all_zero (&cpu_arch[j].enable))
13764 continue;
13765 str = xasprintf ("no%s", cpu_arch[j].name);
13766 p = output_message (stream, p, message, start, &left, str,
13767 strlen (str));
13768 free (str);
293f5f65
L
13769 }
13770
8a2c8fef
L
13771 *p = '\0';
13772 fprintf (stream, "%s\n", message);
13773}
13774
252b5132 13775void
8a2c8fef 13776md_show_usage (FILE *stream)
252b5132 13777{
4cc782b5
ILT
13778#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13779 fprintf (stream, _("\
d4693039 13780 -Qy, -Qn ignored\n\
a38cf1db 13781 -V print assembler version number\n\
b3b91714
AM
13782 -k ignored\n"));
13783#endif
13784 fprintf (stream, _("\
7ebd68d1
NC
13785 -n do not optimize code alignment\n\
13786 -O{012s} attempt some code optimizations\n\
b3b91714
AM
13787 -q quieten some warnings\n"));
13788#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13789 fprintf (stream, _("\
a38cf1db 13790 -s ignored\n"));
b3b91714 13791#endif
b00af7c8
JB
13792#ifdef BFD64
13793# if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13794 fprintf (stream, _("\
13795 --32/--64/--x32 generate 32bit/64bit/x32 object\n"));
13796# elif defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O)
751d281c 13797 fprintf (stream, _("\
b00af7c8
JB
13798 --32/--64 generate 32bit/64bit object\n"));
13799# endif
751d281c 13800#endif
b3b91714
AM
13801#ifdef SVR4_COMMENT_CHARS
13802 fprintf (stream, _("\
13803 --divide do not treat `/' as a comment character\n"));
a38cf1db
AM
13804#else
13805 fprintf (stream, _("\
b3b91714 13806 --divide ignored\n"));
4cc782b5 13807#endif
9103f4f4 13808 fprintf (stream, _("\
6305a203 13809 -march=CPU[,+EXTENSION...]\n\
8a2c8fef 13810 generate code for CPU and EXTENSION, CPU is one of:\n"));
1ded5609 13811 show_arch (stream, 0, 1);
8a2c8fef 13812 fprintf (stream, _("\
ae89daec 13813 EXTENSION is combination of (possibly \"no\"-prefixed):\n"));
1ded5609 13814 show_arch (stream, 1, 0);
6305a203 13815 fprintf (stream, _("\
8a2c8fef 13816 -mtune=CPU optimize for CPU, CPU is one of:\n"));
1ded5609 13817 show_arch (stream, 0, 0);
ba104c83 13818 fprintf (stream, _("\
c0f3af97
L
13819 -msse2avx encode SSE instructions with VEX prefix\n"));
13820 fprintf (stream, _("\
c8480b58
L
13821 -muse-unaligned-vector-move\n\
13822 encode aligned vector move as unaligned vector move\n"));
13823 fprintf (stream, _("\
7c5c05ef 13824 -msse-check=[none|error|warning] (default: warning)\n\
daf50ae7
L
13825 check SSE instructions\n"));
13826 fprintf (stream, _("\
7c5c05ef 13827 -moperand-check=[none|error|warning] (default: warning)\n\
7bab8ab5
JB
13828 check operand combinations for validity\n"));
13829 fprintf (stream, _("\
7c5c05ef
L
13830 -mavxscalar=[128|256] (default: 128)\n\
13831 encode scalar AVX instructions with specific vector\n\
539f890d
L
13832 length\n"));
13833 fprintf (stream, _("\
03751133
L
13834 -mvexwig=[0|1] (default: 0)\n\
13835 encode VEX instructions with specific VEX.W value\n\
13836 for VEX.W bit ignored instructions\n"));
13837 fprintf (stream, _("\
7c5c05ef
L
13838 -mevexlig=[128|256|512] (default: 128)\n\
13839 encode scalar EVEX instructions with specific vector\n\
43234a1e
L
13840 length\n"));
13841 fprintf (stream, _("\
7c5c05ef
L
13842 -mevexwig=[0|1] (default: 0)\n\
13843 encode EVEX instructions with specific EVEX.W value\n\
43234a1e
L
13844 for EVEX.W bit ignored instructions\n"));
13845 fprintf (stream, _("\
7c5c05ef 13846 -mevexrcig=[rne|rd|ru|rz] (default: rne)\n\
d3d3c6db
IT
13847 encode EVEX instructions with specific EVEX.RC value\n\
13848 for SAE-only ignored instructions\n"));
13849 fprintf (stream, _("\
7c5c05ef
L
13850 -mmnemonic=[att|intel] "));
13851 if (SYSV386_COMPAT)
13852 fprintf (stream, _("(default: att)\n"));
13853 else
13854 fprintf (stream, _("(default: intel)\n"));
13855 fprintf (stream, _("\
13856 use AT&T/Intel mnemonic\n"));
ba104c83 13857 fprintf (stream, _("\
7c5c05ef
L
13858 -msyntax=[att|intel] (default: att)\n\
13859 use AT&T/Intel syntax\n"));
ba104c83
L
13860 fprintf (stream, _("\
13861 -mindex-reg support pseudo index registers\n"));
13862 fprintf (stream, _("\
13863 -mnaked-reg don't require `%%' prefix for registers\n"));
13864 fprintf (stream, _("\
7e8b059b 13865 -madd-bnd-prefix add BND prefix for all valid branches\n"));
b4a3a7b4 13866#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8dcea932
L
13867 fprintf (stream, _("\
13868 -mshared disable branch optimization for shared code\n"));
b4a3a7b4
L
13869 fprintf (stream, _("\
13870 -mx86-used-note=[no|yes] "));
13871 if (DEFAULT_X86_USED_NOTE)
13872 fprintf (stream, _("(default: yes)\n"));
13873 else
13874 fprintf (stream, _("(default: no)\n"));
13875 fprintf (stream, _("\
13876 generate x86 used ISA and feature properties\n"));
13877#endif
13878#if defined (TE_PE) || defined (TE_PEP)
167ad85b
TG
13879 fprintf (stream, _("\
13880 -mbig-obj generate big object files\n"));
13881#endif
d022bddd 13882 fprintf (stream, _("\
7c5c05ef 13883 -momit-lock-prefix=[no|yes] (default: no)\n\
d022bddd 13884 strip all lock prefixes\n"));
5db04b09 13885 fprintf (stream, _("\
7c5c05ef 13886 -mfence-as-lock-add=[no|yes] (default: no)\n\
e4e00185
AS
13887 encode lfence, mfence and sfence as\n\
13888 lock addl $0x0, (%%{re}sp)\n"));
13889 fprintf (stream, _("\
7c5c05ef
L
13890 -mrelax-relocations=[no|yes] "));
13891 if (DEFAULT_GENERATE_X86_RELAX_RELOCATIONS)
13892 fprintf (stream, _("(default: yes)\n"));
13893 else
13894 fprintf (stream, _("(default: no)\n"));
13895 fprintf (stream, _("\
0cb4071e
L
13896 generate relax relocations\n"));
13897 fprintf (stream, _("\
e379e5f3
L
13898 -malign-branch-boundary=NUM (default: 0)\n\
13899 align branches within NUM byte boundary\n"));
13900 fprintf (stream, _("\
13901 -malign-branch=TYPE[+TYPE...] (default: jcc+fused+jmp)\n\
13902 TYPE is combination of jcc, fused, jmp, call, ret,\n\
13903 indirect\n\
13904 specify types of branches to align\n"));
13905 fprintf (stream, _("\
13906 -malign-branch-prefix-size=NUM (default: 5)\n\
13907 align branches with NUM prefixes per instruction\n"));
13908 fprintf (stream, _("\
76cf450b
L
13909 -mbranches-within-32B-boundaries\n\
13910 align branches within 32 byte boundary\n"));
13911 fprintf (stream, _("\
ae531041
L
13912 -mlfence-after-load=[no|yes] (default: no)\n\
13913 generate lfence after load\n"));
13914 fprintf (stream, _("\
13915 -mlfence-before-indirect-branch=[none|all|register|memory] (default: none)\n\
13916 generate lfence before indirect near branch\n"));
13917 fprintf (stream, _("\
a09f656b 13918 -mlfence-before-ret=[none|or|not|shl|yes] (default: none)\n\
ae531041
L
13919 generate lfence before ret\n"));
13920 fprintf (stream, _("\
7c5c05ef 13921 -mamd64 accept only AMD64 ISA [default]\n"));
5db04b09
L
13922 fprintf (stream, _("\
13923 -mintel64 accept only Intel64 ISA\n"));
252b5132
RH
13924}
13925
3e73aa7c 13926#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
321098a5 13927 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
e57f8c65 13928 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
252b5132
RH
13929
13930/* Pick the target format to use. */
13931
47926f60 13932const char *
e3bb37b5 13933i386_target_format (void)
252b5132 13934{
d34049e8 13935 if (startswith (default_arch, "x86_64"))
351f65ca
L
13936 {
13937 update_code_flag (CODE_64BIT, 1);
13938 if (default_arch[6] == '\0')
7f56bc95 13939 x86_elf_abi = X86_64_ABI;
351f65ca 13940 else
7f56bc95 13941 x86_elf_abi = X86_64_X32_ABI;
351f65ca 13942 }
3e73aa7c 13943 else if (!strcmp (default_arch, "i386"))
78f12dd3 13944 update_code_flag (CODE_32BIT, 1);
5197d474
L
13945 else if (!strcmp (default_arch, "iamcu"))
13946 {
13947 update_code_flag (CODE_32BIT, 1);
13948 if (cpu_arch_isa == PROCESSOR_UNKNOWN)
13949 {
13950 static const i386_cpu_flags iamcu_flags = CPU_IAMCU_FLAGS;
13951 cpu_arch_name = "iamcu";
d92c7521 13952 free (cpu_sub_arch_name);
5197d474
L
13953 cpu_sub_arch_name = NULL;
13954 cpu_arch_flags = iamcu_flags;
13955 cpu_arch_isa = PROCESSOR_IAMCU;
13956 cpu_arch_isa_flags = iamcu_flags;
13957 if (!cpu_arch_tune_set)
13958 {
13959 cpu_arch_tune = cpu_arch_isa;
13960 cpu_arch_tune_flags = cpu_arch_isa_flags;
13961 }
13962 }
8d471ec1 13963 else if (cpu_arch_isa != PROCESSOR_IAMCU)
5197d474
L
13964 as_fatal (_("Intel MCU doesn't support `%s' architecture"),
13965 cpu_arch_name);
13966 }
3e73aa7c 13967 else
2b5d6a91 13968 as_fatal (_("unknown architecture"));
89507696
JB
13969
13970 if (cpu_flags_all_zero (&cpu_arch_isa_flags))
ae89daec 13971 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
89507696 13972 if (cpu_flags_all_zero (&cpu_arch_tune_flags))
ae89daec 13973 cpu_arch_tune_flags = cpu_arch[flag_code == CODE_64BIT].enable;
89507696 13974
252b5132
RH
13975 switch (OUTPUT_FLAVOR)
13976 {
9384f2ff 13977#if defined (OBJ_MAYBE_AOUT) || defined (OBJ_AOUT)
4c63da97 13978 case bfd_target_aout_flavour:
47926f60 13979 return AOUT_TARGET_FORMAT;
4c63da97 13980#endif
9384f2ff
AM
13981#if defined (OBJ_MAYBE_COFF) || defined (OBJ_COFF)
13982# if defined (TE_PE) || defined (TE_PEP)
13983 case bfd_target_coff_flavour:
167ad85b 13984 if (flag_code == CODE_64BIT)
eb19308f
JB
13985 {
13986 object_64bit = 1;
13987 return use_big_obj ? "pe-bigobj-x86-64" : "pe-x86-64";
13988 }
13989 return use_big_obj ? "pe-bigobj-i386" : "pe-i386";
9384f2ff 13990# elif defined (TE_GO32)
0561d57c
JK
13991 case bfd_target_coff_flavour:
13992 return "coff-go32";
9384f2ff 13993# else
252b5132
RH
13994 case bfd_target_coff_flavour:
13995 return "coff-i386";
9384f2ff 13996# endif
4c63da97 13997#endif
3e73aa7c 13998#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
252b5132 13999 case bfd_target_elf_flavour:
3e73aa7c 14000 {
351f65ca
L
14001 const char *format;
14002
14003 switch (x86_elf_abi)
4fa24527 14004 {
351f65ca
L
14005 default:
14006 format = ELF_TARGET_FORMAT;
e379e5f3
L
14007#ifndef TE_SOLARIS
14008 tls_get_addr = "___tls_get_addr";
14009#endif
351f65ca 14010 break;
7f56bc95 14011 case X86_64_ABI:
351f65ca 14012 use_rela_relocations = 1;
4fa24527 14013 object_64bit = 1;
e379e5f3
L
14014#ifndef TE_SOLARIS
14015 tls_get_addr = "__tls_get_addr";
14016#endif
351f65ca
L
14017 format = ELF_TARGET_FORMAT64;
14018 break;
7f56bc95 14019 case X86_64_X32_ABI:
4fa24527 14020 use_rela_relocations = 1;
351f65ca 14021 object_64bit = 1;
e379e5f3
L
14022#ifndef TE_SOLARIS
14023 tls_get_addr = "__tls_get_addr";
14024#endif
862be3fb 14025 disallow_64bit_reloc = 1;
351f65ca
L
14026 format = ELF_TARGET_FORMAT32;
14027 break;
4fa24527 14028 }
c085ab00 14029 if (cpu_arch_isa == PROCESSOR_IAMCU)
81486035
L
14030 {
14031 if (x86_elf_abi != I386_ABI)
14032 as_fatal (_("Intel MCU is 32bit only"));
14033 return ELF_TARGET_IAMCU_FORMAT;
14034 }
8a9036a4 14035 else
351f65ca 14036 return format;
3e73aa7c 14037 }
e57f8c65
TG
14038#endif
14039#if defined (OBJ_MACH_O)
14040 case bfd_target_mach_o_flavour:
d382c579
TG
14041 if (flag_code == CODE_64BIT)
14042 {
14043 use_rela_relocations = 1;
14044 object_64bit = 1;
14045 return "mach-o-x86-64";
14046 }
14047 else
14048 return "mach-o-i386";
4c63da97 14049#endif
252b5132
RH
14050 default:
14051 abort ();
14052 return NULL;
14053 }
14054}
14055
47926f60 14056#endif /* OBJ_MAYBE_ more than one */
252b5132 14057\f
252b5132 14058symbolS *
7016a5d5 14059md_undefined_symbol (char *name)
252b5132 14060{
18dc2407
ILT
14061 if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
14062 && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
14063 && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
14064 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
24eab124
AM
14065 {
14066 if (!GOT_symbol)
14067 {
14068 if (symbol_find (name))
14069 as_bad (_("GOT already in symbol table"));
14070 GOT_symbol = symbol_new (name, undefined_section,
e01e1cee 14071 &zero_address_frag, 0);
24eab124
AM
14072 };
14073 return GOT_symbol;
14074 }
252b5132
RH
14075 return 0;
14076}
14077
14078/* Round up a section size to the appropriate boundary. */
47926f60 14079
252b5132 14080valueT
7016a5d5 14081md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
252b5132 14082{
4c63da97
AM
14083#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
14084 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
14085 {
14086 /* For a.out, force the section size to be aligned. If we don't do
14087 this, BFD will align it for us, but it will not write out the
14088 final bytes of the section. This may be a bug in BFD, but it is
14089 easier to fix it here since that is how the other a.out targets
14090 work. */
14091 int align;
14092
fd361982 14093 align = bfd_section_alignment (segment);
8d3842cd 14094 size = ((size + (1 << align) - 1) & (-((valueT) 1 << align)));
4c63da97 14095 }
252b5132
RH
14096#endif
14097
14098 return size;
14099}
14100
14101/* On the i386, PC-relative offsets are relative to the start of the
14102 next instruction. That is, the address of the offset, plus its
14103 size, since the offset is always the last part of the insn. */
14104
14105long
e3bb37b5 14106md_pcrel_from (fixS *fixP)
252b5132
RH
14107{
14108 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
14109}
14110
14111#ifndef I386COFF
14112
14113static void
e3bb37b5 14114s_bss (int ignore ATTRIBUTE_UNUSED)
252b5132 14115{
29b0f896 14116 int temp;
252b5132 14117
8a75718c
JB
14118#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14119 if (IS_ELF)
14120 obj_elf_section_change_hook ();
14121#endif
252b5132
RH
14122 temp = get_absolute_expression ();
14123 subseg_set (bss_section, (subsegT) temp);
14124 demand_empty_rest_of_line ();
14125}
14126
14127#endif
14128
e379e5f3
L
14129/* Remember constant directive. */
14130
14131void
14132i386_cons_align (int ignore ATTRIBUTE_UNUSED)
14133{
14134 if (last_insn.kind != last_insn_directive
14135 && (bfd_section_flags (now_seg) & SEC_CODE))
14136 {
14137 last_insn.seg = now_seg;
14138 last_insn.kind = last_insn_directive;
14139 last_insn.name = "constant directive";
14140 last_insn.file = as_where (&last_insn.line);
ae531041
L
14141 if (lfence_before_ret != lfence_before_ret_none)
14142 {
14143 if (lfence_before_indirect_branch != lfence_branch_none)
14144 as_warn (_("constant directive skips -mlfence-before-ret "
14145 "and -mlfence-before-indirect-branch"));
14146 else
14147 as_warn (_("constant directive skips -mlfence-before-ret"));
14148 }
14149 else if (lfence_before_indirect_branch != lfence_branch_none)
14150 as_warn (_("constant directive skips -mlfence-before-indirect-branch"));
e379e5f3
L
14151 }
14152}
14153
3abbafc2 14154int
e3bb37b5 14155i386_validate_fix (fixS *fixp)
252b5132 14156{
e52a16f2
JB
14157 if (fixp->fx_addsy && S_GET_SEGMENT(fixp->fx_addsy) == reg_section)
14158 {
14159 reloc_howto_type *howto;
14160
14161 howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
14162 as_bad_where (fixp->fx_file, fixp->fx_line,
14163 _("invalid %s relocation against register"),
14164 howto ? howto->name : "<unknown>");
14165 return 0;
14166 }
14167
3abbafc2
JB
14168#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14169 if (fixp->fx_r_type == BFD_RELOC_SIZE32
14170 || fixp->fx_r_type == BFD_RELOC_SIZE64)
14171 return IS_ELF && fixp->fx_addsy
14172 && (!S_IS_DEFINED (fixp->fx_addsy)
14173 || S_IS_EXTERNAL (fixp->fx_addsy));
14174#endif
14175
02a86693 14176 if (fixp->fx_subsy)
252b5132 14177 {
02a86693 14178 if (fixp->fx_subsy == GOT_symbol)
23df1078 14179 {
02a86693
L
14180 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
14181 {
14182 if (!object_64bit)
14183 abort ();
14184#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14185 if (fixp->fx_tcbit2)
56ceb5b5
L
14186 fixp->fx_r_type = (fixp->fx_tcbit
14187 ? BFD_RELOC_X86_64_REX_GOTPCRELX
14188 : BFD_RELOC_X86_64_GOTPCRELX);
02a86693
L
14189 else
14190#endif
14191 fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
14192 }
d6ab8113 14193 else
02a86693
L
14194 {
14195 if (!object_64bit)
14196 fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
14197 else
14198 fixp->fx_r_type = BFD_RELOC_X86_64_GOTOFF64;
14199 }
14200 fixp->fx_subsy = 0;
23df1078 14201 }
252b5132 14202 }
02a86693 14203#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2585b7a5 14204 else
02a86693 14205 {
2585b7a5
L
14206 /* NB: Commit 292676c1 resolved PLT32 reloc aganst local symbol
14207 to section. Since PLT32 relocation must be against symbols,
14208 turn such PLT32 relocation into PC32 relocation. */
14209 if (fixp->fx_addsy
14210 && (fixp->fx_r_type == BFD_RELOC_386_PLT32
14211 || fixp->fx_r_type == BFD_RELOC_X86_64_PLT32)
14212 && symbol_section_p (fixp->fx_addsy))
14213 fixp->fx_r_type = BFD_RELOC_32_PCREL;
14214 if (!object_64bit)
14215 {
14216 if (fixp->fx_r_type == BFD_RELOC_386_GOT32
14217 && fixp->fx_tcbit2)
14218 fixp->fx_r_type = BFD_RELOC_386_GOT32X;
14219 }
02a86693
L
14220 }
14221#endif
3abbafc2
JB
14222
14223 return 1;
252b5132
RH
14224}
14225
252b5132 14226arelent *
7016a5d5 14227tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
14228{
14229 arelent *rel;
14230 bfd_reloc_code_real_type code;
14231
14232 switch (fixp->fx_r_type)
14233 {
8ce3d284 14234#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3abbafc2
JB
14235 symbolS *sym;
14236
8fd4256d
L
14237 case BFD_RELOC_SIZE32:
14238 case BFD_RELOC_SIZE64:
3abbafc2
JB
14239 if (fixp->fx_addsy
14240 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))
14241 && (!fixp->fx_subsy
14242 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))))
14243 sym = fixp->fx_addsy;
14244 else if (fixp->fx_subsy
14245 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))
14246 && (!fixp->fx_addsy
14247 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))))
14248 sym = fixp->fx_subsy;
14249 else
14250 sym = NULL;
14251 if (IS_ELF && sym && S_IS_DEFINED (sym) && !S_IS_EXTERNAL (sym))
8fd4256d
L
14252 {
14253 /* Resolve size relocation against local symbol to size of
14254 the symbol plus addend. */
3abbafc2 14255 valueT value = S_GET_SIZE (sym);
44f87162 14256
3abbafc2
JB
14257 if (symbol_get_bfdsym (sym)->flags & BSF_SECTION_SYM)
14258 value = bfd_section_size (S_GET_SEGMENT (sym));
14259 if (sym == fixp->fx_subsy)
14260 {
14261 value = -value;
14262 if (fixp->fx_addsy)
14263 value += S_GET_VALUE (fixp->fx_addsy);
14264 }
14265 else if (fixp->fx_subsy)
14266 value -= S_GET_VALUE (fixp->fx_subsy);
44f87162 14267 value += fixp->fx_offset;
8fd4256d 14268 if (fixp->fx_r_type == BFD_RELOC_SIZE32
d965814f 14269 && object_64bit
8fd4256d
L
14270 && !fits_in_unsigned_long (value))
14271 as_bad_where (fixp->fx_file, fixp->fx_line,
14272 _("symbol size computation overflow"));
14273 fixp->fx_addsy = NULL;
14274 fixp->fx_subsy = NULL;
14275 md_apply_fix (fixp, (valueT *) &value, NULL);
14276 return NULL;
14277 }
3abbafc2
JB
14278 if (!fixp->fx_addsy || fixp->fx_subsy)
14279 {
14280 as_bad_where (fixp->fx_file, fixp->fx_line,
14281 "unsupported expression involving @size");
14282 return NULL;
14283 }
8ce3d284 14284#endif
1a0670f3 14285 /* Fall through. */
8fd4256d 14286
3e73aa7c
JH
14287 case BFD_RELOC_X86_64_PLT32:
14288 case BFD_RELOC_X86_64_GOT32:
14289 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
14290 case BFD_RELOC_X86_64_GOTPCRELX:
14291 case BFD_RELOC_X86_64_REX_GOTPCRELX:
252b5132
RH
14292 case BFD_RELOC_386_PLT32:
14293 case BFD_RELOC_386_GOT32:
02a86693 14294 case BFD_RELOC_386_GOT32X:
252b5132
RH
14295 case BFD_RELOC_386_GOTOFF:
14296 case BFD_RELOC_386_GOTPC:
13ae64f3
JJ
14297 case BFD_RELOC_386_TLS_GD:
14298 case BFD_RELOC_386_TLS_LDM:
14299 case BFD_RELOC_386_TLS_LDO_32:
14300 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
14301 case BFD_RELOC_386_TLS_IE:
14302 case BFD_RELOC_386_TLS_GOTIE:
13ae64f3
JJ
14303 case BFD_RELOC_386_TLS_LE_32:
14304 case BFD_RELOC_386_TLS_LE:
67a4f2b7
AO
14305 case BFD_RELOC_386_TLS_GOTDESC:
14306 case BFD_RELOC_386_TLS_DESC_CALL:
bffbf940
JJ
14307 case BFD_RELOC_X86_64_TLSGD:
14308 case BFD_RELOC_X86_64_TLSLD:
14309 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 14310 case BFD_RELOC_X86_64_DTPOFF64:
bffbf940
JJ
14311 case BFD_RELOC_X86_64_GOTTPOFF:
14312 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113
JB
14313 case BFD_RELOC_X86_64_TPOFF64:
14314 case BFD_RELOC_X86_64_GOTOFF64:
14315 case BFD_RELOC_X86_64_GOTPC32:
7b81dfbb
AJ
14316 case BFD_RELOC_X86_64_GOT64:
14317 case BFD_RELOC_X86_64_GOTPCREL64:
14318 case BFD_RELOC_X86_64_GOTPC64:
14319 case BFD_RELOC_X86_64_GOTPLT64:
14320 case BFD_RELOC_X86_64_PLTOFF64:
67a4f2b7
AO
14321 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14322 case BFD_RELOC_X86_64_TLSDESC_CALL:
252b5132
RH
14323 case BFD_RELOC_RVA:
14324 case BFD_RELOC_VTABLE_ENTRY:
14325 case BFD_RELOC_VTABLE_INHERIT:
6482c264
NC
14326#ifdef TE_PE
14327 case BFD_RELOC_32_SECREL:
145667f8 14328 case BFD_RELOC_16_SECIDX:
6482c264 14329#endif
252b5132
RH
14330 code = fixp->fx_r_type;
14331 break;
dbbaec26
L
14332 case BFD_RELOC_X86_64_32S:
14333 if (!fixp->fx_pcrel)
14334 {
14335 /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32. */
14336 code = fixp->fx_r_type;
14337 break;
14338 }
1a0670f3 14339 /* Fall through. */
252b5132 14340 default:
93382f6d 14341 if (fixp->fx_pcrel)
252b5132 14342 {
93382f6d
AM
14343 switch (fixp->fx_size)
14344 {
14345 default:
b091f402
AM
14346 as_bad_where (fixp->fx_file, fixp->fx_line,
14347 _("can not do %d byte pc-relative relocation"),
14348 fixp->fx_size);
93382f6d
AM
14349 code = BFD_RELOC_32_PCREL;
14350 break;
14351 case 1: code = BFD_RELOC_8_PCREL; break;
14352 case 2: code = BFD_RELOC_16_PCREL; break;
d258b828 14353 case 4: code = BFD_RELOC_32_PCREL; break;
d6ab8113
JB
14354#ifdef BFD64
14355 case 8: code = BFD_RELOC_64_PCREL; break;
14356#endif
93382f6d
AM
14357 }
14358 }
14359 else
14360 {
14361 switch (fixp->fx_size)
14362 {
14363 default:
b091f402
AM
14364 as_bad_where (fixp->fx_file, fixp->fx_line,
14365 _("can not do %d byte relocation"),
14366 fixp->fx_size);
93382f6d
AM
14367 code = BFD_RELOC_32;
14368 break;
14369 case 1: code = BFD_RELOC_8; break;
14370 case 2: code = BFD_RELOC_16; break;
14371 case 4: code = BFD_RELOC_32; break;
937149dd 14372#ifdef BFD64
3e73aa7c 14373 case 8: code = BFD_RELOC_64; break;
937149dd 14374#endif
93382f6d 14375 }
252b5132
RH
14376 }
14377 break;
14378 }
252b5132 14379
d182319b
JB
14380 if ((code == BFD_RELOC_32
14381 || code == BFD_RELOC_32_PCREL
14382 || code == BFD_RELOC_X86_64_32S)
252b5132
RH
14383 && GOT_symbol
14384 && fixp->fx_addsy == GOT_symbol)
3e73aa7c 14385 {
4fa24527 14386 if (!object_64bit)
d6ab8113
JB
14387 code = BFD_RELOC_386_GOTPC;
14388 else
14389 code = BFD_RELOC_X86_64_GOTPC32;
3e73aa7c 14390 }
7b81dfbb
AJ
14391 if ((code == BFD_RELOC_64 || code == BFD_RELOC_64_PCREL)
14392 && GOT_symbol
14393 && fixp->fx_addsy == GOT_symbol)
14394 {
14395 code = BFD_RELOC_X86_64_GOTPC64;
14396 }
252b5132 14397
add39d23
TS
14398 rel = XNEW (arelent);
14399 rel->sym_ptr_ptr = XNEW (asymbol *);
49309057 14400 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
14401
14402 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
c87db184 14403
3e73aa7c
JH
14404 if (!use_rela_relocations)
14405 {
14406 /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
14407 vtable entry to be used in the relocation's section offset. */
14408 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
14409 rel->address = fixp->fx_offset;
fbeb56a4
DK
14410#if defined (OBJ_COFF) && defined (TE_PE)
14411 else if (fixp->fx_addsy && S_IS_WEAK (fixp->fx_addsy))
14412 rel->addend = fixp->fx_addnumber - (S_GET_VALUE (fixp->fx_addsy) * 2);
14413 else
14414#endif
c6682705 14415 rel->addend = 0;
3e73aa7c
JH
14416 }
14417 /* Use the rela in 64bit mode. */
252b5132 14418 else
3e73aa7c 14419 {
862be3fb
L
14420 if (disallow_64bit_reloc)
14421 switch (code)
14422 {
862be3fb
L
14423 case BFD_RELOC_X86_64_DTPOFF64:
14424 case BFD_RELOC_X86_64_TPOFF64:
14425 case BFD_RELOC_64_PCREL:
14426 case BFD_RELOC_X86_64_GOTOFF64:
14427 case BFD_RELOC_X86_64_GOT64:
14428 case BFD_RELOC_X86_64_GOTPCREL64:
14429 case BFD_RELOC_X86_64_GOTPC64:
14430 case BFD_RELOC_X86_64_GOTPLT64:
14431 case BFD_RELOC_X86_64_PLTOFF64:
14432 as_bad_where (fixp->fx_file, fixp->fx_line,
14433 _("cannot represent relocation type %s in x32 mode"),
14434 bfd_get_reloc_code_name (code));
14435 break;
14436 default:
14437 break;
14438 }
14439
062cd5e7
AS
14440 if (!fixp->fx_pcrel)
14441 rel->addend = fixp->fx_offset;
14442 else
14443 switch (code)
14444 {
14445 case BFD_RELOC_X86_64_PLT32:
14446 case BFD_RELOC_X86_64_GOT32:
14447 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
14448 case BFD_RELOC_X86_64_GOTPCRELX:
14449 case BFD_RELOC_X86_64_REX_GOTPCRELX:
bffbf940
JJ
14450 case BFD_RELOC_X86_64_TLSGD:
14451 case BFD_RELOC_X86_64_TLSLD:
14452 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7
AO
14453 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14454 case BFD_RELOC_X86_64_TLSDESC_CALL:
062cd5e7
AS
14455 rel->addend = fixp->fx_offset - fixp->fx_size;
14456 break;
14457 default:
14458 rel->addend = (section->vma
14459 - fixp->fx_size
14460 + fixp->fx_addnumber
14461 + md_pcrel_from (fixp));
14462 break;
14463 }
3e73aa7c
JH
14464 }
14465
252b5132
RH
14466 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
14467 if (rel->howto == NULL)
14468 {
14469 as_bad_where (fixp->fx_file, fixp->fx_line,
d0b47220 14470 _("cannot represent relocation type %s"),
252b5132
RH
14471 bfd_get_reloc_code_name (code));
14472 /* Set howto to a garbage value so that we can keep going. */
14473 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
9c2799c2 14474 gas_assert (rel->howto != NULL);
252b5132
RH
14475 }
14476
14477 return rel;
14478}
14479
ee86248c 14480#include "tc-i386-intel.c"
54cfded0 14481
a60de03c
JB
14482void
14483tc_x86_parse_to_dw2regnum (expressionS *exp)
54cfded0 14484{
a60de03c
JB
14485 int saved_naked_reg;
14486 char saved_register_dot;
54cfded0 14487
a60de03c
JB
14488 saved_naked_reg = allow_naked_reg;
14489 allow_naked_reg = 1;
14490 saved_register_dot = register_chars['.'];
14491 register_chars['.'] = '.';
14492 allow_pseudo_reg = 1;
14493 expression_and_evaluate (exp);
14494 allow_pseudo_reg = 0;
14495 register_chars['.'] = saved_register_dot;
14496 allow_naked_reg = saved_naked_reg;
14497
e96d56a1 14498 if (exp->X_op == O_register && exp->X_add_number >= 0)
54cfded0 14499 {
a60de03c
JB
14500 if ((addressT) exp->X_add_number < i386_regtab_size)
14501 {
14502 exp->X_op = O_constant;
14503 exp->X_add_number = i386_regtab[exp->X_add_number]
14504 .dw2_regnum[flag_code >> 1];
14505 }
14506 else
14507 exp->X_op = O_illegal;
54cfded0 14508 }
54cfded0
AM
14509}
14510
14511void
14512tc_x86_frame_initial_instructions (void)
14513{
a60de03c
JB
14514 static unsigned int sp_regno[2];
14515
14516 if (!sp_regno[flag_code >> 1])
14517 {
14518 char *saved_input = input_line_pointer;
14519 char sp[][4] = {"esp", "rsp"};
14520 expressionS exp;
a4447b93 14521
a60de03c
JB
14522 input_line_pointer = sp[flag_code >> 1];
14523 tc_x86_parse_to_dw2regnum (&exp);
9c2799c2 14524 gas_assert (exp.X_op == O_constant);
a60de03c
JB
14525 sp_regno[flag_code >> 1] = exp.X_add_number;
14526 input_line_pointer = saved_input;
14527 }
a4447b93 14528
61ff971f
L
14529 cfi_add_CFA_def_cfa (sp_regno[flag_code >> 1], -x86_cie_data_alignment);
14530 cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
54cfded0 14531}
d2b2c203 14532
d7921315
L
14533int
14534x86_dwarf2_addr_size (void)
14535{
14536#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
14537 if (x86_elf_abi == X86_64_X32_ABI)
14538 return 4;
14539#endif
14540 return bfd_arch_bits_per_address (stdoutput) / 8;
14541}
14542
d2b2c203
DJ
14543int
14544i386_elf_section_type (const char *str, size_t len)
14545{
14546 if (flag_code == CODE_64BIT
14547 && len == sizeof ("unwind") - 1
d34049e8 14548 && startswith (str, "unwind"))
d2b2c203
DJ
14549 return SHT_X86_64_UNWIND;
14550
14551 return -1;
14552}
bb41ade5 14553
ad5fec3b
EB
14554#ifdef TE_SOLARIS
14555void
14556i386_solaris_fix_up_eh_frame (segT sec)
14557{
14558 if (flag_code == CODE_64BIT)
14559 elf_section_type (sec) = SHT_X86_64_UNWIND;
14560}
14561#endif
14562
bb41ade5
AM
14563#ifdef TE_PE
14564void
14565tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
14566{
91d6fa6a 14567 expressionS exp;
bb41ade5 14568
91d6fa6a
NC
14569 exp.X_op = O_secrel;
14570 exp.X_add_symbol = symbol;
14571 exp.X_add_number = 0;
14572 emit_expr (&exp, size);
bb41ade5
AM
14573}
14574#endif
3b22753a
L
14575
14576#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14577/* For ELF on x86-64, add support for SHF_X86_64_LARGE. */
14578
01e1a5bc 14579bfd_vma
6d4af3c2 14580x86_64_section_letter (int letter, const char **ptr_msg)
3b22753a
L
14581{
14582 if (flag_code == CODE_64BIT)
14583 {
14584 if (letter == 'l')
14585 return SHF_X86_64_LARGE;
14586
8f3bae45 14587 *ptr_msg = _("bad .section directive: want a,l,w,x,M,S,G,T in string");
64e74474 14588 }
3b22753a 14589 else
8f3bae45 14590 *ptr_msg = _("bad .section directive: want a,w,x,M,S,G,T in string");
3b22753a
L
14591 return -1;
14592}
14593
01e1a5bc 14594bfd_vma
3b22753a
L
14595x86_64_section_word (char *str, size_t len)
14596{
08dedd66 14597 if (len == 5 && flag_code == CODE_64BIT && startswith (str, "large"))
3b22753a
L
14598 return SHF_X86_64_LARGE;
14599
14600 return -1;
14601}
14602
14603static void
14604handle_large_common (int small ATTRIBUTE_UNUSED)
14605{
14606 if (flag_code != CODE_64BIT)
14607 {
14608 s_comm_internal (0, elf_common_parse);
14609 as_warn (_(".largecomm supported only in 64bit mode, producing .comm"));
14610 }
14611 else
14612 {
14613 static segT lbss_section;
14614 asection *saved_com_section_ptr = elf_com_section_ptr;
14615 asection *saved_bss_section = bss_section;
14616
14617 if (lbss_section == NULL)
14618 {
14619 flagword applicable;
14620 segT seg = now_seg;
14621 subsegT subseg = now_subseg;
14622
14623 /* The .lbss section is for local .largecomm symbols. */
14624 lbss_section = subseg_new (".lbss", 0);
14625 applicable = bfd_applicable_section_flags (stdoutput);
fd361982 14626 bfd_set_section_flags (lbss_section, applicable & SEC_ALLOC);
3b22753a
L
14627 seg_info (lbss_section)->bss = 1;
14628
14629 subseg_set (seg, subseg);
14630 }
14631
14632 elf_com_section_ptr = &_bfd_elf_large_com_section;
14633 bss_section = lbss_section;
14634
14635 s_comm_internal (0, elf_common_parse);
14636
14637 elf_com_section_ptr = saved_com_section_ptr;
14638 bss_section = saved_bss_section;
14639 }
14640}
14641#endif /* OBJ_ELF || OBJ_MAYBE_ELF */