]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/config/tc-i386.c
RISC-V: Fixed the missing $x+arch when adding odd paddings for alignment.
[thirdparty/binutils-gdb.git] / gas / config / tc-i386.c
CommitLineData
b534c6d3 1/* tc-i386.c -- Assemble code for the Intel 80386
a2c58332 2 Copyright (C) 1989-2022 Free Software Foundation, Inc.
252b5132
RH
3
4 This file is part of GAS, the GNU Assembler.
5
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
ec2655a6 8 the Free Software Foundation; either version 3, or (at your option)
252b5132
RH
9 any later version.
10
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to the Free
4b4da160
NC
18 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
19 02110-1301, USA. */
252b5132 20
47926f60
KH
21/* Intel 80386 machine specific gas.
22 Written by Eliot Dresselhaus (eliot@mgm.mit.edu).
3e73aa7c 23 x86_64 support by Jan Hubicka (jh@suse.cz)
0f10071e 24 VIA PadLock support by Michal Ludvig (mludvig@suse.cz)
47926f60
KH
25 Bugs & suggestions are completely welcome. This is free software.
26 Please help us make it better. */
252b5132 27
252b5132 28#include "as.h"
3882b010 29#include "safe-ctype.h"
252b5132 30#include "subsegs.h"
316e2c05 31#include "dwarf2dbg.h"
54cfded0 32#include "dw2gencfi.h"
d2b2c203 33#include "elf/x86-64.h"
40fb9820 34#include "opcodes/i386-init.h"
41fd2579 35#include <limits.h>
41fd2579 36
c3332e24 37#ifndef INFER_ADDR_PREFIX
eecb386c 38#define INFER_ADDR_PREFIX 1
c3332e24
AM
39#endif
40
29b0f896
AM
41#ifndef DEFAULT_ARCH
42#define DEFAULT_ARCH "i386"
246fcdee 43#endif
252b5132 44
edde18a5
AM
45#ifndef INLINE
46#if __GNUC__ >= 2
47#define INLINE __inline__
48#else
49#define INLINE
50#endif
51#endif
52
6305a203
L
53/* Prefixes will be emitted in the order defined below.
54 WAIT_PREFIX must be the first prefix since FWAIT is really is an
55 instruction, and so must come before any prefixes.
56 The preferred prefix order is SEG_PREFIX, ADDR_PREFIX, DATA_PREFIX,
42164a71 57 REP_PREFIX/HLE_PREFIX, LOCK_PREFIX. */
6305a203
L
58#define WAIT_PREFIX 0
59#define SEG_PREFIX 1
60#define ADDR_PREFIX 2
61#define DATA_PREFIX 3
c32fa91d 62#define REP_PREFIX 4
42164a71 63#define HLE_PREFIX REP_PREFIX
7e8b059b 64#define BND_PREFIX REP_PREFIX
c32fa91d 65#define LOCK_PREFIX 5
4e9ac44a
L
66#define REX_PREFIX 6 /* must come last. */
67#define MAX_PREFIXES 7 /* max prefixes per opcode */
6305a203
L
68
69/* we define the syntax here (modulo base,index,scale syntax) */
70#define REGISTER_PREFIX '%'
71#define IMMEDIATE_PREFIX '$'
72#define ABSOLUTE_PREFIX '*'
73
74/* these are the instruction mnemonic suffixes in AT&T syntax or
75 memory operand size in Intel syntax. */
76#define WORD_MNEM_SUFFIX 'w'
77#define BYTE_MNEM_SUFFIX 'b'
78#define SHORT_MNEM_SUFFIX 's'
79#define LONG_MNEM_SUFFIX 'l'
80#define QWORD_MNEM_SUFFIX 'q'
6305a203
L
81/* Intel Syntax. Use a non-ascii letter since since it never appears
82 in instructions. */
83#define LONG_DOUBLE_MNEM_SUFFIX '\1'
84
85#define END_OF_INSN '\0'
86
79dec6b7
JB
87/* This matches the C -> StaticRounding alias in the opcode table. */
88#define commutative staticrounding
89
6305a203
L
90/*
91 'templates' is for grouping together 'template' structures for opcodes
92 of the same name. This is only used for storing the insns in the grand
93 ole hash table of insns.
94 The templates themselves start at START and range up to (but not including)
95 END.
96 */
97typedef struct
98{
d3ce72d0
NC
99 const insn_template *start;
100 const insn_template *end;
6305a203
L
101}
102templates;
103
104/* 386 operand encoding bytes: see 386 book for details of this. */
105typedef struct
106{
107 unsigned int regmem; /* codes register or memory operand */
108 unsigned int reg; /* codes register operand (or extended opcode) */
109 unsigned int mode; /* how to interpret regmem & reg */
110}
111modrm_byte;
112
113/* x86-64 extension prefix. */
114typedef int rex_byte;
115
6305a203
L
116/* 386 opcode byte to code indirect addressing. */
117typedef struct
118{
119 unsigned base;
120 unsigned index;
121 unsigned scale;
122}
123sib_byte;
124
6305a203
L
125/* x86 arch names, types and features */
126typedef struct
127{
128 const char *name; /* arch name */
6ceeed25
JB
129 unsigned int len:8; /* arch string length */
130 bool skip:1; /* show_arch should skip this. */
6305a203 131 enum processor_type type; /* arch type */
ae89daec
JB
132 i386_cpu_flags enable; /* cpu feature enable flags */
133 i386_cpu_flags disable; /* cpu feature disable flags */
6305a203
L
134}
135arch_entry;
136
78f12dd3 137static void update_code_flag (int, int);
e3bb37b5
L
138static void set_code_flag (int);
139static void set_16bit_gcc_code_flag (int);
140static void set_intel_syntax (int);
1efbbeb4 141static void set_intel_mnemonic (int);
db51cc60 142static void set_allow_index_reg (int);
7bab8ab5 143static void set_check (int);
e3bb37b5 144static void set_cpu_arch (int);
6482c264 145#ifdef TE_PE
e3bb37b5 146static void pe_directive_secrel (int);
145667f8 147static void pe_directive_secidx (int);
6482c264 148#endif
e3bb37b5
L
149static void signed_cons (int);
150static char *output_invalid (int c);
ee86248c
JB
151static int i386_finalize_immediate (segT, expressionS *, i386_operand_type,
152 const char *);
153static int i386_finalize_displacement (segT, expressionS *, i386_operand_type,
154 const char *);
a7619375 155static int i386_att_operand (char *);
e3bb37b5 156static int i386_intel_operand (char *, int);
ee86248c
JB
157static int i386_intel_simplify (expressionS *);
158static int i386_intel_parse_name (const char *, expressionS *);
e3bb37b5
L
159static const reg_entry *parse_register (char *, char **);
160static char *parse_insn (char *, char *);
161static char *parse_operands (char *, const char *);
162static void swap_operands (void);
783c187b 163static void swap_2_operands (unsigned int, unsigned int);
48bcea9f 164static enum flag_code i386_addressing_mode (void);
e3bb37b5
L
165static void optimize_imm (void);
166static void optimize_disp (void);
83b16ac6 167static const insn_template *match_template (char);
e3bb37b5
L
168static int check_string (void);
169static int process_suffix (void);
170static int check_byte_reg (void);
171static int check_long_reg (void);
172static int check_qword_reg (void);
173static int check_word_reg (void);
174static int finalize_imm (void);
175static int process_operands (void);
5e042380 176static const reg_entry *build_modrm_byte (void);
e3bb37b5
L
177static void output_insn (void);
178static void output_imm (fragS *, offsetT);
179static void output_disp (fragS *, offsetT);
29b0f896 180#ifndef I386COFF
e3bb37b5 181static void s_bss (int);
252b5132 182#endif
17d4e2a2
L
183#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
184static void handle_large_common (int small ATTRIBUTE_UNUSED);
b4a3a7b4
L
185
186/* GNU_PROPERTY_X86_ISA_1_USED. */
187static unsigned int x86_isa_1_used;
188/* GNU_PROPERTY_X86_FEATURE_2_USED. */
189static unsigned int x86_feature_2_used;
190/* Generate x86 used ISA and feature properties. */
191static unsigned int x86_used_note = DEFAULT_X86_USED_NOTE;
17d4e2a2 192#endif
252b5132 193
a847613f 194static const char *default_arch = DEFAULT_ARCH;
3e73aa7c 195
8a6fb3f9
JB
196/* parse_register() returns this when a register alias cannot be used. */
197static const reg_entry bad_reg = { "<bad>", OPERAND_TYPE_NONE, 0, 0,
198 { Dw2Inval, Dw2Inval } };
199
34684862 200static const reg_entry *reg_eax;
5e042380
JB
201static const reg_entry *reg_ds;
202static const reg_entry *reg_es;
203static const reg_entry *reg_ss;
6288d05f 204static const reg_entry *reg_st0;
6225c532
JB
205static const reg_entry *reg_k0;
206
c0f3af97
L
207/* VEX prefix. */
208typedef struct
209{
43234a1e
L
210 /* VEX prefix is either 2 byte or 3 byte. EVEX is 4 byte. */
211 unsigned char bytes[4];
c0f3af97
L
212 unsigned int length;
213 /* Destination or source register specifier. */
214 const reg_entry *register_specifier;
215} vex_prefix;
216
252b5132 217/* 'md_assemble ()' gathers together information and puts it into a
47926f60 218 i386_insn. */
252b5132 219
520dc8e8
AM
220union i386_op
221 {
222 expressionS *disps;
223 expressionS *imms;
224 const reg_entry *regs;
225 };
226
a65babc9
L
227enum i386_error
228 {
b4d65f2d 229 no_error, /* Must be first. */
86e026a4 230 operand_size_mismatch,
a65babc9
L
231 operand_type_mismatch,
232 register_type_mismatch,
233 number_of_operands_mismatch,
234 invalid_instruction_suffix,
235 bad_imm4,
a65babc9
L
236 unsupported_with_intel_mnemonic,
237 unsupported_syntax,
6c30d220 238 unsupported,
260cd341 239 invalid_sib_address,
6c30d220 240 invalid_vsib_address,
7bab8ab5 241 invalid_vector_register_set,
260cd341 242 invalid_tmm_register_set,
0cc78721 243 invalid_dest_and_src_register_set,
43234a1e
L
244 unsupported_vector_index_register,
245 unsupported_broadcast,
43234a1e
L
246 broadcast_needed,
247 unsupported_masking,
248 mask_not_on_destination,
249 no_default_mask,
250 unsupported_rc_sae,
43234a1e 251 invalid_register_operand,
a65babc9
L
252 };
253
252b5132
RH
254struct _i386_insn
255 {
47926f60 256 /* TM holds the template for the insn were currently assembling. */
d3ce72d0 257 insn_template tm;
252b5132 258
7d5e4556
L
259 /* SUFFIX holds the instruction size suffix for byte, word, dword
260 or qword, if given. */
252b5132
RH
261 char suffix;
262
9a182d04
JB
263 /* OPCODE_LENGTH holds the number of base opcode bytes. */
264 unsigned char opcode_length;
265
47926f60 266 /* OPERANDS gives the number of given operands. */
252b5132
RH
267 unsigned int operands;
268
269 /* REG_OPERANDS, DISP_OPERANDS, MEM_OPERANDS, IMM_OPERANDS give the number
270 of given register, displacement, memory operands and immediate
47926f60 271 operands. */
252b5132
RH
272 unsigned int reg_operands, disp_operands, mem_operands, imm_operands;
273
274 /* TYPES [i] is the type (see above #defines) which tells us how to
520dc8e8 275 use OP[i] for the corresponding operand. */
40fb9820 276 i386_operand_type types[MAX_OPERANDS];
252b5132 277
520dc8e8
AM
278 /* Displacement expression, immediate expression, or register for each
279 operand. */
280 union i386_op op[MAX_OPERANDS];
252b5132 281
3e73aa7c
JH
282 /* Flags for operands. */
283 unsigned int flags[MAX_OPERANDS];
284#define Operand_PCrel 1
c48dadc9 285#define Operand_Mem 2
3e73aa7c 286
252b5132 287 /* Relocation type for operand */
f86103b7 288 enum bfd_reloc_code_real reloc[MAX_OPERANDS];
252b5132 289
252b5132
RH
290 /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
291 the base index byte below. */
292 const reg_entry *base_reg;
293 const reg_entry *index_reg;
294 unsigned int log2_scale_factor;
295
296 /* SEG gives the seg_entries of this insn. They are zero unless
47926f60 297 explicit segment overrides are given. */
5e042380 298 const reg_entry *seg[2];
252b5132 299
8325cc63
JB
300 /* Copied first memory operand string, for re-checking. */
301 char *memop1_string;
302
252b5132
RH
303 /* PREFIX holds all the given prefix opcodes (usually null).
304 PREFIXES is the number of prefix opcodes. */
305 unsigned int prefixes;
306 unsigned char prefix[MAX_PREFIXES];
307
50128d0c 308 /* Register is in low 3 bits of opcode. */
5b7c81bd 309 bool short_form;
50128d0c 310
6f2f06be 311 /* The operand to a branch insn indicates an absolute branch. */
5b7c81bd 312 bool jumpabsolute;
6f2f06be 313
921eafea
L
314 /* Extended states. */
315 enum
316 {
317 /* Use MMX state. */
318 xstate_mmx = 1 << 0,
319 /* Use XMM state. */
320 xstate_xmm = 1 << 1,
321 /* Use YMM state. */
322 xstate_ymm = 1 << 2 | xstate_xmm,
323 /* Use ZMM state. */
324 xstate_zmm = 1 << 3 | xstate_ymm,
325 /* Use TMM state. */
32930e4e
L
326 xstate_tmm = 1 << 4,
327 /* Use MASK state. */
328 xstate_mask = 1 << 5
921eafea 329 } xstate;
260cd341 330
e379e5f3 331 /* Has GOTPC or TLS relocation. */
5b7c81bd 332 bool has_gotpc_tls_reloc;
e379e5f3 333
252b5132 334 /* RM and SIB are the modrm byte and the sib byte where the
c1e679ec 335 addressing modes of this insn are encoded. */
252b5132 336 modrm_byte rm;
3e73aa7c 337 rex_byte rex;
43234a1e 338 rex_byte vrex;
252b5132 339 sib_byte sib;
c0f3af97 340 vex_prefix vex;
b6169b20 341
6225c532
JB
342 /* Masking attributes.
343
344 The struct describes masking, applied to OPERAND in the instruction.
345 REG is a pointer to the corresponding mask register. ZEROING tells
346 whether merging or zeroing mask is used. */
347 struct Mask_Operation
348 {
349 const reg_entry *reg;
350 unsigned int zeroing;
351 /* The operand where this operation is associated. */
352 unsigned int operand;
353 } mask;
43234a1e
L
354
355 /* Rounding control and SAE attributes. */
ca5312a2
JB
356 struct RC_Operation
357 {
358 enum rc_type
359 {
360 rc_none = -1,
361 rne,
362 rd,
363 ru,
364 rz,
365 saeonly
366 } type;
7063667e
JB
367 /* In Intel syntax the operand modifier form is supposed to be used, but
368 we continue to accept the immediate forms as well. */
369 bool modifier;
ca5312a2 370 } rounding;
43234a1e 371
5273a3cd
JB
372 /* Broadcasting attributes.
373
374 The struct describes broadcasting, applied to OPERAND. TYPE is
375 expresses the broadcast factor. */
376 struct Broadcast_Operation
377 {
0cc78721 378 /* Type of broadcast: {1to2}, {1to4}, {1to8}, {1to16} or {1to32}. */
5273a3cd
JB
379 unsigned int type;
380
381 /* Index of broadcasted operand. */
382 unsigned int operand;
383
384 /* Number of bytes to broadcast. */
385 unsigned int bytes;
386 } broadcast;
43234a1e
L
387
388 /* Compressed disp8*N attribute. */
389 unsigned int memshift;
390
86fa6981
L
391 /* Prefer load or store in encoding. */
392 enum
393 {
394 dir_encoding_default = 0,
395 dir_encoding_load,
64c49ab3
JB
396 dir_encoding_store,
397 dir_encoding_swap
86fa6981 398 } dir_encoding;
891edac4 399
41eb8e88 400 /* Prefer 8bit, 16bit, 32bit displacement in encoding. */
a501d77e
L
401 enum
402 {
403 disp_encoding_default = 0,
404 disp_encoding_8bit,
41eb8e88 405 disp_encoding_16bit,
a501d77e
L
406 disp_encoding_32bit
407 } disp_encoding;
f8a5c266 408
6b6b6807 409 /* Prefer the REX byte in encoding. */
5b7c81bd 410 bool rex_encoding;
6b6b6807 411
b6f8c7c4 412 /* Disable instruction size optimization. */
5b7c81bd 413 bool no_optimize;
b6f8c7c4 414
86fa6981
L
415 /* How to encode vector instructions. */
416 enum
417 {
418 vex_encoding_default = 0,
42e04b36 419 vex_encoding_vex,
86fa6981 420 vex_encoding_vex3,
da4977e0
JB
421 vex_encoding_evex,
422 vex_encoding_error
86fa6981
L
423 } vec_encoding;
424
d5de92cf
L
425 /* REP prefix. */
426 const char *rep_prefix;
427
165de32a
L
428 /* HLE prefix. */
429 const char *hle_prefix;
42164a71 430
7e8b059b
L
431 /* Have BND prefix. */
432 const char *bnd_prefix;
433
04ef582a
L
434 /* Have NOTRACK prefix. */
435 const char *notrack_prefix;
436
891edac4 437 /* Error message. */
a65babc9 438 enum i386_error error;
252b5132
RH
439 };
440
441typedef struct _i386_insn i386_insn;
442
43234a1e
L
443/* Link RC type with corresponding string, that'll be looked for in
444 asm. */
445struct RC_name
446{
447 enum rc_type type;
448 const char *name;
449 unsigned int len;
450};
451
452static const struct RC_name RC_NamesTable[] =
453{
454 { rne, STRING_COMMA_LEN ("rn-sae") },
455 { rd, STRING_COMMA_LEN ("rd-sae") },
456 { ru, STRING_COMMA_LEN ("ru-sae") },
457 { rz, STRING_COMMA_LEN ("rz-sae") },
458 { saeonly, STRING_COMMA_LEN ("sae") },
459};
460
252b5132
RH
461/* List of chars besides those in app.c:symbol_chars that can start an
462 operand. Used to prevent the scrubber eating vital white-space. */
86fa6981 463const char extra_symbol_chars[] = "*%-([{}"
252b5132 464#ifdef LEX_AT
32137342
NC
465 "@"
466#endif
467#ifdef LEX_QM
468 "?"
252b5132 469#endif
32137342 470 ;
252b5132 471
b3983e5f
JB
472#if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
473 && !defined (TE_GNU) \
474 && !defined (TE_LINUX) \
d85e70a3 475 && !defined (TE_Haiku) \
b3983e5f
JB
476 && !defined (TE_FreeBSD) \
477 && !defined (TE_DragonFly) \
478 && !defined (TE_NetBSD))
252b5132 479/* This array holds the chars that always start a comment. If the
b3b91714
AM
480 pre-processor is disabled, these aren't very useful. The option
481 --divide will remove '/' from this list. */
482const char *i386_comment_chars = "#/";
483#define SVR4_COMMENT_CHARS 1
252b5132 484#define PREFIX_SEPARATOR '\\'
252b5132 485
b3b91714
AM
486#else
487const char *i386_comment_chars = "#";
488#define PREFIX_SEPARATOR '/'
489#endif
490
252b5132
RH
491/* This array holds the chars that only start a comment at the beginning of
492 a line. If the line seems to have the form '# 123 filename'
ce8a8b2f
AM
493 .line and .file directives will appear in the pre-processed output.
494 Note that input_file.c hand checks for '#' at the beginning of the
252b5132 495 first line of the input file. This is because the compiler outputs
ce8a8b2f
AM
496 #NO_APP at the beginning of its output.
497 Also note that comments started like this one will always work if
252b5132 498 '/' isn't otherwise defined. */
b3b91714 499const char line_comment_chars[] = "#/";
252b5132 500
63a0b638 501const char line_separator_chars[] = ";";
252b5132 502
ce8a8b2f
AM
503/* Chars that can be used to separate mant from exp in floating point
504 nums. */
252b5132
RH
505const char EXP_CHARS[] = "eE";
506
ce8a8b2f
AM
507/* Chars that mean this number is a floating point constant
508 As in 0f12.456
509 or 0d1.2345e12. */
de133cf9 510const char FLT_CHARS[] = "fFdDxXhHbB";
252b5132 511
ce8a8b2f 512/* Tables for lexical analysis. */
252b5132
RH
513static char mnemonic_chars[256];
514static char register_chars[256];
515static char operand_chars[256];
516static char identifier_chars[256];
252b5132 517
ce8a8b2f 518/* Lexical macros. */
252b5132
RH
519#define is_mnemonic_char(x) (mnemonic_chars[(unsigned char) x])
520#define is_operand_char(x) (operand_chars[(unsigned char) x])
521#define is_register_char(x) (register_chars[(unsigned char) x])
522#define is_space_char(x) ((x) == ' ')
523#define is_identifier_char(x) (identifier_chars[(unsigned char) x])
252b5132 524
0234cb7c 525/* All non-digit non-letter characters that may occur in an operand. */
252b5132
RH
526static char operand_special_chars[] = "%$-+(,)*._~/<>|&^!:[@]";
527
528/* md_assemble() always leaves the strings it's passed unaltered. To
529 effect this we maintain a stack of saved characters that we've smashed
530 with '\0's (indicating end of strings for various sub-fields of the
47926f60 531 assembler instruction). */
252b5132 532static char save_stack[32];
ce8a8b2f 533static char *save_stack_p;
252b5132
RH
534#define END_STRING_AND_SAVE(s) \
535 do { *save_stack_p++ = *(s); *(s) = '\0'; } while (0)
536#define RESTORE_END_STRING(s) \
537 do { *(s) = *--save_stack_p; } while (0)
538
47926f60 539/* The instruction we're assembling. */
252b5132
RH
540static i386_insn i;
541
542/* Possible templates for current insn. */
543static const templates *current_templates;
544
31b2323c
L
545/* Per instruction expressionS buffers: max displacements & immediates. */
546static expressionS disp_expressions[MAX_MEMORY_OPERANDS];
547static expressionS im_expressions[MAX_IMMEDIATE_OPERANDS];
252b5132 548
47926f60 549/* Current operand we are working on. */
ee86248c 550static int this_operand = -1;
252b5132 551
3e73aa7c
JH
552/* We support four different modes. FLAG_CODE variable is used to distinguish
553 these. */
554
555enum flag_code {
556 CODE_32BIT,
557 CODE_16BIT,
558 CODE_64BIT };
559
560static enum flag_code flag_code;
4fa24527 561static unsigned int object_64bit;
862be3fb 562static unsigned int disallow_64bit_reloc;
3e73aa7c 563static int use_rela_relocations = 0;
e379e5f3
L
564/* __tls_get_addr/___tls_get_addr symbol for TLS. */
565static const char *tls_get_addr;
3e73aa7c 566
7af8ed2d
NC
567#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
568 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
569 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
570
351f65ca
L
571/* The ELF ABI to use. */
572enum x86_elf_abi
573{
574 I386_ABI,
7f56bc95
L
575 X86_64_ABI,
576 X86_64_X32_ABI
351f65ca
L
577};
578
579static enum x86_elf_abi x86_elf_abi = I386_ABI;
7af8ed2d 580#endif
351f65ca 581
167ad85b
TG
582#if defined (TE_PE) || defined (TE_PEP)
583/* Use big object file format. */
584static int use_big_obj = 0;
585#endif
586
8dcea932
L
587#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
588/* 1 if generating code for a shared library. */
589static int shared = 0;
590#endif
591
47926f60
KH
592/* 1 for intel syntax,
593 0 if att syntax. */
594static int intel_syntax = 0;
252b5132 595
4b5aaf5f
L
596static enum x86_64_isa
597{
598 amd64 = 1, /* AMD64 ISA. */
599 intel64 /* Intel64 ISA. */
600} isa64;
e89c5eaa 601
1efbbeb4
L
602/* 1 for intel mnemonic,
603 0 if att mnemonic. */
604static int intel_mnemonic = !SYSV386_COMPAT;
605
a60de03c
JB
606/* 1 if pseudo registers are permitted. */
607static int allow_pseudo_reg = 0;
608
47926f60
KH
609/* 1 if register prefix % not required. */
610static int allow_naked_reg = 0;
252b5132 611
33eaf5de 612/* 1 if the assembler should add BND prefix for all control-transferring
7e8b059b
L
613 instructions supporting it, even if this prefix wasn't specified
614 explicitly. */
615static int add_bnd_prefix = 0;
616
ba104c83 617/* 1 if pseudo index register, eiz/riz, is allowed . */
db51cc60
L
618static int allow_index_reg = 0;
619
d022bddd
IT
620/* 1 if the assembler should ignore LOCK prefix, even if it was
621 specified explicitly. */
622static int omit_lock_prefix = 0;
623
e4e00185
AS
624/* 1 if the assembler should encode lfence, mfence, and sfence as
625 "lock addl $0, (%{re}sp)". */
626static int avoid_fence = 0;
627
ae531041
L
628/* 1 if lfence should be inserted after every load. */
629static int lfence_after_load = 0;
630
631/* Non-zero if lfence should be inserted before indirect branch. */
632static enum lfence_before_indirect_branch_kind
633 {
634 lfence_branch_none = 0,
635 lfence_branch_register,
636 lfence_branch_memory,
637 lfence_branch_all
638 }
639lfence_before_indirect_branch;
640
641/* Non-zero if lfence should be inserted before ret. */
642static enum lfence_before_ret_kind
643 {
644 lfence_before_ret_none = 0,
645 lfence_before_ret_not,
a09f656b 646 lfence_before_ret_or,
647 lfence_before_ret_shl
ae531041
L
648 }
649lfence_before_ret;
650
651/* Types of previous instruction is .byte or prefix. */
e379e5f3
L
652static struct
653 {
654 segT seg;
655 const char *file;
656 const char *name;
657 unsigned int line;
658 enum last_insn_kind
659 {
660 last_insn_other = 0,
661 last_insn_directive,
662 last_insn_prefix
663 } kind;
664 } last_insn;
665
0cb4071e
L
666/* 1 if the assembler should generate relax relocations. */
667
668static int generate_relax_relocations
669 = DEFAULT_GENERATE_X86_RELAX_RELOCATIONS;
670
7bab8ab5 671static enum check_kind
daf50ae7 672 {
7bab8ab5
JB
673 check_none = 0,
674 check_warning,
675 check_error
daf50ae7 676 }
7bab8ab5 677sse_check, operand_check = check_warning;
daf50ae7 678
e379e5f3
L
679/* Non-zero if branches should be aligned within power of 2 boundary. */
680static int align_branch_power = 0;
681
682/* Types of branches to align. */
683enum align_branch_kind
684 {
685 align_branch_none = 0,
686 align_branch_jcc = 1,
687 align_branch_fused = 2,
688 align_branch_jmp = 3,
689 align_branch_call = 4,
690 align_branch_indirect = 5,
691 align_branch_ret = 6
692 };
693
694/* Type bits of branches to align. */
695enum align_branch_bit
696 {
697 align_branch_jcc_bit = 1 << align_branch_jcc,
698 align_branch_fused_bit = 1 << align_branch_fused,
699 align_branch_jmp_bit = 1 << align_branch_jmp,
700 align_branch_call_bit = 1 << align_branch_call,
701 align_branch_indirect_bit = 1 << align_branch_indirect,
702 align_branch_ret_bit = 1 << align_branch_ret
703 };
704
705static unsigned int align_branch = (align_branch_jcc_bit
706 | align_branch_fused_bit
707 | align_branch_jmp_bit);
708
79d72f45
HL
709/* Types of condition jump used by macro-fusion. */
710enum mf_jcc_kind
711 {
712 mf_jcc_jo = 0, /* base opcode 0x70 */
713 mf_jcc_jc, /* base opcode 0x72 */
714 mf_jcc_je, /* base opcode 0x74 */
715 mf_jcc_jna, /* base opcode 0x76 */
716 mf_jcc_js, /* base opcode 0x78 */
717 mf_jcc_jp, /* base opcode 0x7a */
718 mf_jcc_jl, /* base opcode 0x7c */
719 mf_jcc_jle, /* base opcode 0x7e */
720 };
721
722/* Types of compare flag-modifying insntructions used by macro-fusion. */
723enum mf_cmp_kind
724 {
725 mf_cmp_test_and, /* test/cmp */
726 mf_cmp_alu_cmp, /* add/sub/cmp */
727 mf_cmp_incdec /* inc/dec */
728 };
729
e379e5f3
L
730/* The maximum padding size for fused jcc. CMP like instruction can
731 be 9 bytes and jcc can be 6 bytes. Leave room just in case for
732 prefixes. */
733#define MAX_FUSED_JCC_PADDING_SIZE 20
734
735/* The maximum number of prefixes added for an instruction. */
736static unsigned int align_branch_prefix_size = 5;
737
b6f8c7c4
L
738/* Optimization:
739 1. Clear the REX_W bit with register operand if possible.
740 2. Above plus use 128bit vector instruction to clear the full vector
741 register.
742 */
743static int optimize = 0;
744
745/* Optimization:
746 1. Clear the REX_W bit with register operand if possible.
747 2. Above plus use 128bit vector instruction to clear the full vector
748 register.
749 3. Above plus optimize "test{q,l,w} $imm8,%r{64,32,16}" to
750 "testb $imm7,%r8".
751 */
752static int optimize_for_space = 0;
753
2ca3ace5
L
754/* Register prefix used for error message. */
755static const char *register_prefix = "%";
756
47926f60
KH
757/* Used in 16 bit gcc mode to add an l suffix to call, ret, enter,
758 leave, push, and pop instructions so that gcc has the same stack
759 frame as in 32 bit mode. */
760static char stackop_size = '\0';
eecb386c 761
12b55ccc
L
762/* Non-zero to optimize code alignment. */
763int optimize_align_code = 1;
764
47926f60
KH
765/* Non-zero to quieten some warnings. */
766static int quiet_warnings = 0;
a38cf1db 767
d59a54c2
JB
768/* Guard to avoid repeated warnings about non-16-bit code on 16-bit CPUs. */
769static bool pre_386_16bit_warned;
770
47926f60
KH
771/* CPU name. */
772static const char *cpu_arch_name = NULL;
6305a203 773static char *cpu_sub_arch_name = NULL;
a38cf1db 774
47926f60 775/* CPU feature flags. */
40fb9820
L
776static i386_cpu_flags cpu_arch_flags = CPU_UNKNOWN_FLAGS;
777
ccc9c027
L
778/* If we have selected a cpu we are generating instructions for. */
779static int cpu_arch_tune_set = 0;
780
9103f4f4 781/* Cpu we are generating instructions for. */
fbf3f584 782enum processor_type cpu_arch_tune = PROCESSOR_UNKNOWN;
9103f4f4
L
783
784/* CPU feature flags of cpu we are generating instructions for. */
40fb9820 785static i386_cpu_flags cpu_arch_tune_flags;
9103f4f4 786
ccc9c027 787/* CPU instruction set architecture used. */
fbf3f584 788enum processor_type cpu_arch_isa = PROCESSOR_UNKNOWN;
ccc9c027 789
9103f4f4 790/* CPU feature flags of instruction set architecture used. */
fbf3f584 791i386_cpu_flags cpu_arch_isa_flags;
9103f4f4 792
fddf5b5b
AM
793/* If set, conditional jumps are not automatically promoted to handle
794 larger than a byte offset. */
f68697e8 795static bool no_cond_jump_promotion = false;
fddf5b5b 796
c0f3af97
L
797/* Encode SSE instructions with VEX prefix. */
798static unsigned int sse2avx;
799
c8480b58
L
800/* Encode aligned vector move as unaligned vector move. */
801static unsigned int use_unaligned_vector_move;
802
539f890d
L
803/* Encode scalar AVX instructions with specific vector length. */
804static enum
805 {
806 vex128 = 0,
807 vex256
808 } avxscalar;
809
03751133
L
810/* Encode VEX WIG instructions with specific vex.w. */
811static enum
812 {
813 vexw0 = 0,
814 vexw1
815 } vexwig;
816
43234a1e
L
817/* Encode scalar EVEX LIG instructions with specific vector length. */
818static enum
819 {
820 evexl128 = 0,
821 evexl256,
822 evexl512
823 } evexlig;
824
825/* Encode EVEX WIG instructions with specific evex.w. */
826static enum
827 {
828 evexw0 = 0,
829 evexw1
830 } evexwig;
831
d3d3c6db
IT
832/* Value to encode in EVEX RC bits, for SAE-only instructions. */
833static enum rc_type evexrcig = rne;
834
29b0f896 835/* Pre-defined "_GLOBAL_OFFSET_TABLE_". */
87c245cc 836static symbolS *GOT_symbol;
29b0f896 837
a4447b93
RH
838/* The dwarf2 return column, adjusted for 32 or 64 bit. */
839unsigned int x86_dwarf2_return_column;
840
841/* The dwarf2 data alignment, adjusted for 32 or 64 bit. */
842int x86_cie_data_alignment;
843
252b5132 844/* Interface to relax_segment.
fddf5b5b
AM
845 There are 3 major relax states for 386 jump insns because the
846 different types of jumps add different sizes to frags when we're
e379e5f3
L
847 figuring out what sort of jump to choose to reach a given label.
848
849 BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING are used to align
850 branches which are handled by md_estimate_size_before_relax() and
851 i386_generic_table_relax_frag(). */
252b5132 852
47926f60 853/* Types. */
93c2a809
AM
854#define UNCOND_JUMP 0
855#define COND_JUMP 1
856#define COND_JUMP86 2
e379e5f3
L
857#define BRANCH_PADDING 3
858#define BRANCH_PREFIX 4
859#define FUSED_JCC_PADDING 5
fddf5b5b 860
47926f60 861/* Sizes. */
252b5132
RH
862#define CODE16 1
863#define SMALL 0
29b0f896 864#define SMALL16 (SMALL | CODE16)
252b5132 865#define BIG 2
29b0f896 866#define BIG16 (BIG | CODE16)
252b5132
RH
867
868#ifndef INLINE
869#ifdef __GNUC__
870#define INLINE __inline__
871#else
872#define INLINE
873#endif
874#endif
875
fddf5b5b
AM
876#define ENCODE_RELAX_STATE(type, size) \
877 ((relax_substateT) (((type) << 2) | (size)))
878#define TYPE_FROM_RELAX_STATE(s) \
879 ((s) >> 2)
880#define DISP_SIZE_FROM_RELAX_STATE(s) \
881 ((((s) & 3) == BIG ? 4 : (((s) & 3) == BIG16 ? 2 : 1)))
252b5132
RH
882
883/* This table is used by relax_frag to promote short jumps to long
884 ones where necessary. SMALL (short) jumps may be promoted to BIG
885 (32 bit long) ones, and SMALL16 jumps to BIG16 (16 bit long). We
886 don't allow a short jump in a 32 bit code segment to be promoted to
887 a 16 bit offset jump because it's slower (requires data size
888 prefix), and doesn't work, unless the destination is in the bottom
889 64k of the code segment (The top 16 bits of eip are zeroed). */
890
891const relax_typeS md_relax_table[] =
892{
24eab124
AM
893 /* The fields are:
894 1) most positive reach of this state,
895 2) most negative reach of this state,
93c2a809 896 3) how many bytes this mode will have in the variable part of the frag
ce8a8b2f 897 4) which index into the table to try if we can't fit into this one. */
252b5132 898
fddf5b5b 899 /* UNCOND_JUMP states. */
93c2a809
AM
900 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG)},
901 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16)},
902 /* dword jmp adds 4 bytes to frag:
903 0 extra opcode bytes, 4 displacement bytes. */
252b5132 904 {0, 0, 4, 0},
93c2a809
AM
905 /* word jmp adds 2 byte2 to frag:
906 0 extra opcode bytes, 2 displacement bytes. */
252b5132
RH
907 {0, 0, 2, 0},
908
93c2a809
AM
909 /* COND_JUMP states. */
910 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG)},
911 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG16)},
912 /* dword conditionals adds 5 bytes to frag:
913 1 extra opcode byte, 4 displacement bytes. */
914 {0, 0, 5, 0},
fddf5b5b 915 /* word conditionals add 3 bytes to frag:
93c2a809
AM
916 1 extra opcode byte, 2 displacement bytes. */
917 {0, 0, 3, 0},
918
919 /* COND_JUMP86 states. */
920 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG)},
921 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG16)},
922 /* dword conditionals adds 5 bytes to frag:
923 1 extra opcode byte, 4 displacement bytes. */
924 {0, 0, 5, 0},
925 /* word conditionals add 4 bytes to frag:
926 1 displacement byte and a 3 byte long branch insn. */
927 {0, 0, 4, 0}
252b5132
RH
928};
929
6ceeed25 930#define ARCH(n, t, f, s) \
ae89daec
JB
931 { STRING_COMMA_LEN (#n), s, PROCESSOR_ ## t, CPU_ ## f ## _FLAGS, \
932 CPU_NONE_FLAGS }
933#define SUBARCH(n, e, d, s) \
934 { STRING_COMMA_LEN (#n), s, PROCESSOR_NONE, CPU_ ## e ## _FLAGS, \
935 CPU_ ## d ## _FLAGS }
6ceeed25 936
9103f4f4
L
937static const arch_entry cpu_arch[] =
938{
3ce2ebcf
JB
939 /* Do not replace the first two entries - i386_target_format() and
940 set_cpu_arch() rely on them being there in this order. */
6ceeed25
JB
941 ARCH (generic32, GENERIC32, GENERIC32, false),
942 ARCH (generic64, GENERIC64, GENERIC64, false),
943 ARCH (i8086, UNKNOWN, NONE, false),
944 ARCH (i186, UNKNOWN, I186, false),
945 ARCH (i286, UNKNOWN, I286, false),
946 ARCH (i386, I386, I386, false),
947 ARCH (i486, I486, I486, false),
948 ARCH (i586, PENTIUM, I586, false),
949 ARCH (i686, PENTIUMPRO, I686, false),
950 ARCH (pentium, PENTIUM, I586, false),
951 ARCH (pentiumpro, PENTIUMPRO, PENTIUMPRO, false),
952 ARCH (pentiumii, PENTIUMPRO, P2, false),
953 ARCH (pentiumiii, PENTIUMPRO, P3, false),
954 ARCH (pentium4, PENTIUM4, P4, false),
955 ARCH (prescott, NOCONA, CORE, false),
956 ARCH (nocona, NOCONA, NOCONA, false),
957 ARCH (yonah, CORE, CORE, true),
958 ARCH (core, CORE, CORE, false),
959 ARCH (merom, CORE2, CORE2, true),
960 ARCH (core2, CORE2, CORE2, false),
961 ARCH (corei7, COREI7, COREI7, false),
962 ARCH (iamcu, IAMCU, IAMCU, false),
963 ARCH (k6, K6, K6, false),
964 ARCH (k6_2, K6, K6_2, false),
965 ARCH (athlon, ATHLON, ATHLON, false),
966 ARCH (sledgehammer, K8, K8, true),
967 ARCH (opteron, K8, K8, false),
968 ARCH (k8, K8, K8, false),
969 ARCH (amdfam10, AMDFAM10, AMDFAM10, false),
970 ARCH (bdver1, BD, BDVER1, false),
971 ARCH (bdver2, BD, BDVER2, false),
972 ARCH (bdver3, BD, BDVER3, false),
973 ARCH (bdver4, BD, BDVER4, false),
974 ARCH (znver1, ZNVER, ZNVER1, false),
975 ARCH (znver2, ZNVER, ZNVER2, false),
976 ARCH (znver3, ZNVER, ZNVER3, false),
977 ARCH (btver1, BT, BTVER1, false),
978 ARCH (btver2, BT, BTVER2, false),
979
ae89daec
JB
980 SUBARCH (8087, 8087, ANY_X87, false),
981 SUBARCH (87, NONE, ANY_X87, false), /* Disable only! */
982 SUBARCH (287, 287, ANY_287, false),
983 SUBARCH (387, 387, ANY_387, false),
984 SUBARCH (687, 687, ANY_687, false),
985 SUBARCH (cmov, CMOV, ANY_CMOV, false),
986 SUBARCH (fxsr, FXSR, ANY_FXSR, false),
987 SUBARCH (mmx, MMX, ANY_MMX, false),
988 SUBARCH (sse, SSE, ANY_SSE, false),
989 SUBARCH (sse2, SSE2, ANY_SSE2, false),
990 SUBARCH (sse3, SSE3, ANY_SSE3, false),
991 SUBARCH (sse4a, SSE4A, ANY_SSE4A, false),
992 SUBARCH (ssse3, SSSE3, ANY_SSSE3, false),
993 SUBARCH (sse4.1, SSE4_1, ANY_SSE4_1, false),
994 SUBARCH (sse4.2, SSE4_2, ANY_SSE4_2, false),
995 SUBARCH (sse4, SSE4_2, ANY_SSE4_1, false),
996 SUBARCH (avx, AVX, ANY_AVX, false),
997 SUBARCH (avx2, AVX2, ANY_AVX2, false),
998 SUBARCH (avx512f, AVX512F, ANY_AVX512F, false),
999 SUBARCH (avx512cd, AVX512CD, ANY_AVX512CD, false),
1000 SUBARCH (avx512er, AVX512ER, ANY_AVX512ER, false),
1001 SUBARCH (avx512pf, AVX512PF, ANY_AVX512PF, false),
1002 SUBARCH (avx512dq, AVX512DQ, ANY_AVX512DQ, false),
1003 SUBARCH (avx512bw, AVX512BW, ANY_AVX512BW, false),
1004 SUBARCH (avx512vl, AVX512VL, ANY_AVX512VL, false),
1005 SUBARCH (vmx, VMX, VMX, false),
1006 SUBARCH (vmfunc, VMFUNC, VMFUNC, false),
1007 SUBARCH (smx, SMX, SMX, false),
1008 SUBARCH (xsave, XSAVE, XSAVE, false),
1009 SUBARCH (xsaveopt, XSAVEOPT, XSAVEOPT, false),
1010 SUBARCH (xsavec, XSAVEC, XSAVEC, false),
1011 SUBARCH (xsaves, XSAVES, XSAVES, false),
1012 SUBARCH (aes, AES, AES, false),
1013 SUBARCH (pclmul, PCLMUL, PCLMUL, false),
1014 SUBARCH (clmul, PCLMUL, PCLMUL, true),
1015 SUBARCH (fsgsbase, FSGSBASE, FSGSBASE, false),
1016 SUBARCH (rdrnd, RDRND, RDRND, false),
1017 SUBARCH (f16c, F16C, F16C, false),
1018 SUBARCH (bmi2, BMI2, BMI2, false),
1019 SUBARCH (fma, FMA, FMA, false),
1020 SUBARCH (fma4, FMA4, FMA4, false),
1021 SUBARCH (xop, XOP, XOP, false),
1022 SUBARCH (lwp, LWP, LWP, false),
1023 SUBARCH (movbe, MOVBE, MOVBE, false),
1024 SUBARCH (cx16, CX16, CX16, false),
1025 SUBARCH (ept, EPT, EPT, false),
1026 SUBARCH (lzcnt, LZCNT, LZCNT, false),
1027 SUBARCH (popcnt, POPCNT, POPCNT, false),
1028 SUBARCH (hle, HLE, HLE, false),
1029 SUBARCH (rtm, RTM, RTM, false),
1030 SUBARCH (invpcid, INVPCID, INVPCID, false),
1031 SUBARCH (clflush, CLFLUSH, CLFLUSH, false),
1032 SUBARCH (nop, NOP, NOP, false),
1033 SUBARCH (syscall, SYSCALL, SYSCALL, false),
1034 SUBARCH (rdtscp, RDTSCP, RDTSCP, false),
1035 SUBARCH (3dnow, 3DNOW, 3DNOW, false),
1036 SUBARCH (3dnowa, 3DNOWA, 3DNOWA, false),
1037 SUBARCH (padlock, PADLOCK, PADLOCK, false),
1038 SUBARCH (pacifica, SVME, SVME, true),
1039 SUBARCH (svme, SVME, SVME, false),
1040 SUBARCH (sse4a, SSE4A, SSE4A, false),
1041 SUBARCH (abm, ABM, ABM, false),
1042 SUBARCH (bmi, BMI, BMI, false),
1043 SUBARCH (tbm, TBM, TBM, false),
1044 SUBARCH (adx, ADX, ADX, false),
1045 SUBARCH (rdseed, RDSEED, RDSEED, false),
1046 SUBARCH (prfchw, PRFCHW, PRFCHW, false),
1047 SUBARCH (smap, SMAP, SMAP, false),
1048 SUBARCH (mpx, MPX, MPX, false),
1049 SUBARCH (sha, SHA, SHA, false),
1050 SUBARCH (clflushopt, CLFLUSHOPT, CLFLUSHOPT, false),
1051 SUBARCH (prefetchwt1, PREFETCHWT1, PREFETCHWT1, false),
1052 SUBARCH (se1, SE1, SE1, false),
1053 SUBARCH (clwb, CLWB, CLWB, false),
1054 SUBARCH (avx512ifma, AVX512IFMA, ANY_AVX512IFMA, false),
1055 SUBARCH (avx512vbmi, AVX512VBMI, ANY_AVX512VBMI, false),
1056 SUBARCH (avx512_4fmaps, AVX512_4FMAPS, ANY_AVX512_4FMAPS, false),
1057 SUBARCH (avx512_4vnniw, AVX512_4VNNIW, ANY_AVX512_4VNNIW, false),
1058 SUBARCH (avx512_vpopcntdq, AVX512_VPOPCNTDQ, ANY_AVX512_VPOPCNTDQ, false),
1059 SUBARCH (avx512_vbmi2, AVX512_VBMI2, ANY_AVX512_VBMI2, false),
1060 SUBARCH (avx512_vnni, AVX512_VNNI, ANY_AVX512_VNNI, false),
1061 SUBARCH (avx512_bitalg, AVX512_BITALG, ANY_AVX512_BITALG, false),
1062 SUBARCH (avx_vnni, AVX_VNNI, ANY_AVX_VNNI, false),
1063 SUBARCH (clzero, CLZERO, CLZERO, false),
1064 SUBARCH (mwaitx, MWAITX, MWAITX, false),
1065 SUBARCH (ospke, OSPKE, OSPKE, false),
1066 SUBARCH (rdpid, RDPID, RDPID, false),
1067 SUBARCH (ptwrite, PTWRITE, PTWRITE, false),
1068 SUBARCH (ibt, IBT, ANY_IBT, false),
1069 SUBARCH (shstk, SHSTK, ANY_SHSTK, false),
1070 SUBARCH (gfni, GFNI, GFNI, false),
1071 SUBARCH (vaes, VAES, VAES, false),
1072 SUBARCH (vpclmulqdq, VPCLMULQDQ, VPCLMULQDQ, false),
1073 SUBARCH (wbnoinvd, WBNOINVD, WBNOINVD, false),
1074 SUBARCH (pconfig, PCONFIG, PCONFIG, false),
1075 SUBARCH (waitpkg, WAITPKG, WAITPKG, false),
1076 SUBARCH (cldemote, CLDEMOTE, CLDEMOTE, false),
1077 SUBARCH (amx_int8, AMX_INT8, ANY_AMX_INT8, false),
1078 SUBARCH (amx_bf16, AMX_BF16, ANY_AMX_BF16, false),
68830fba 1079 SUBARCH (amx_fp16, AMX_FP16, AMX_FP16, false),
ae89daec
JB
1080 SUBARCH (amx_tile, AMX_TILE, ANY_AMX_TILE, false),
1081 SUBARCH (movdiri, MOVDIRI, ANY_MOVDIRI, false),
1082 SUBARCH (movdir64b, MOVDIR64B, ANY_MOVDIR64B, false),
1083 SUBARCH (avx512_bf16, AVX512_BF16, ANY_AVX512_BF16, false),
1084 SUBARCH (avx512_vp2intersect, AVX512_VP2INTERSECT,
1085 ANY_AVX512_VP2INTERSECT, false),
1086 SUBARCH (tdx, TDX, ANY_TDX, false),
1087 SUBARCH (enqcmd, ENQCMD, ANY_ENQCMD, false),
1088 SUBARCH (serialize, SERIALIZE, ANY_SERIALIZE, false),
1089 SUBARCH (rdpru, RDPRU, RDPRU, false),
1090 SUBARCH (mcommit, MCOMMIT, MCOMMIT, false),
1091 SUBARCH (sev_es, SEV_ES, SEV_ES, false),
1092 SUBARCH (tsxldtrk, TSXLDTRK, ANY_TSXLDTRK, false),
1093 SUBARCH (kl, KL, ANY_KL, false),
1094 SUBARCH (widekl, WIDEKL, ANY_WIDEKL, false),
1095 SUBARCH (uintr, UINTR, ANY_UINTR, false),
1096 SUBARCH (hreset, HRESET, ANY_HRESET, false),
1097 SUBARCH (avx512_fp16, AVX512_FP16, ANY_AVX512_FP16, false),
ef07be45 1098 SUBARCH (prefetchi, PREFETCHI, PREFETCHI, false),
4321af3e 1099 SUBARCH (avx_ifma, AVX_IFMA, ANY_AVX_IFMA, false),
23ae61ad 1100 SUBARCH (avx_vnni_int8, AVX_VNNI_INT8, ANY_AVX_VNNI_INT8, false),
a93e3234 1101 SUBARCH (cmpccxadd, CMPCCXADD, ANY_CMPCCXADD, false),
941f0833 1102 SUBARCH (wrmsrns, WRMSRNS, ANY_WRMSRNS, false),
2188d6ea 1103 SUBARCH (msrlist, MSRLIST, ANY_MSRLIST, false),
293f5f65
L
1104};
1105
6ceeed25
JB
1106#undef SUBARCH
1107#undef ARCH
1108
704209c0 1109#ifdef I386COFF
a6c24e68
NC
1110/* Like s_lcomm_internal in gas/read.c but the alignment string
1111 is allowed to be optional. */
1112
1113static symbolS *
1114pe_lcomm_internal (int needs_align, symbolS *symbolP, addressT size)
1115{
1116 addressT align = 0;
1117
1118 SKIP_WHITESPACE ();
1119
7ab9ffdd 1120 if (needs_align
a6c24e68
NC
1121 && *input_line_pointer == ',')
1122 {
1123 align = parse_align (needs_align - 1);
7ab9ffdd 1124
a6c24e68
NC
1125 if (align == (addressT) -1)
1126 return NULL;
1127 }
1128 else
1129 {
1130 if (size >= 8)
1131 align = 3;
1132 else if (size >= 4)
1133 align = 2;
1134 else if (size >= 2)
1135 align = 1;
1136 else
1137 align = 0;
1138 }
1139
1140 bss_alloc (symbolP, size, align);
1141 return symbolP;
1142}
1143
704209c0 1144static void
a6c24e68
NC
1145pe_lcomm (int needs_align)
1146{
1147 s_comm_internal (needs_align * 2, pe_lcomm_internal);
1148}
704209c0 1149#endif
a6c24e68 1150
29b0f896
AM
1151const pseudo_typeS md_pseudo_table[] =
1152{
1153#if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
1154 {"align", s_align_bytes, 0},
1155#else
1156 {"align", s_align_ptwo, 0},
1157#endif
1158 {"arch", set_cpu_arch, 0},
1159#ifndef I386COFF
1160 {"bss", s_bss, 0},
a6c24e68
NC
1161#else
1162 {"lcomm", pe_lcomm, 1},
29b0f896
AM
1163#endif
1164 {"ffloat", float_cons, 'f'},
1165 {"dfloat", float_cons, 'd'},
1166 {"tfloat", float_cons, 'x'},
7d19d096 1167 {"hfloat", float_cons, 'h'},
de133cf9 1168 {"bfloat16", float_cons, 'b'},
29b0f896 1169 {"value", cons, 2},
d182319b 1170 {"slong", signed_cons, 4},
29b0f896
AM
1171 {"noopt", s_ignore, 0},
1172 {"optim", s_ignore, 0},
1173 {"code16gcc", set_16bit_gcc_code_flag, CODE_16BIT},
1174 {"code16", set_code_flag, CODE_16BIT},
1175 {"code32", set_code_flag, CODE_32BIT},
da5f19a2 1176#ifdef BFD64
29b0f896 1177 {"code64", set_code_flag, CODE_64BIT},
da5f19a2 1178#endif
29b0f896
AM
1179 {"intel_syntax", set_intel_syntax, 1},
1180 {"att_syntax", set_intel_syntax, 0},
1efbbeb4
L
1181 {"intel_mnemonic", set_intel_mnemonic, 1},
1182 {"att_mnemonic", set_intel_mnemonic, 0},
db51cc60
L
1183 {"allow_index_reg", set_allow_index_reg, 1},
1184 {"disallow_index_reg", set_allow_index_reg, 0},
7bab8ab5
JB
1185 {"sse_check", set_check, 0},
1186 {"operand_check", set_check, 1},
3b22753a
L
1187#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1188 {"largecomm", handle_large_common, 0},
07a53e5c 1189#else
68d20676 1190 {"file", dwarf2_directive_file, 0},
07a53e5c
RH
1191 {"loc", dwarf2_directive_loc, 0},
1192 {"loc_mark_labels", dwarf2_directive_loc_mark_labels, 0},
3b22753a 1193#endif
6482c264
NC
1194#ifdef TE_PE
1195 {"secrel32", pe_directive_secrel, 0},
145667f8 1196 {"secidx", pe_directive_secidx, 0},
6482c264 1197#endif
29b0f896
AM
1198 {0, 0, 0}
1199};
1200
1201/* For interface with expression (). */
1202extern char *input_line_pointer;
1203
1204/* Hash table for instruction mnemonic lookup. */
629310ab 1205static htab_t op_hash;
29b0f896
AM
1206
1207/* Hash table for register lookup. */
629310ab 1208static htab_t reg_hash;
29b0f896 1209\f
ce8a8b2f
AM
1210 /* Various efficient no-op patterns for aligning code labels.
1211 Note: Don't try to assemble the instructions in the comments.
1212 0L and 0w are not legal. */
62a02d25
L
1213static const unsigned char f32_1[] =
1214 {0x90}; /* nop */
1215static const unsigned char f32_2[] =
1216 {0x66,0x90}; /* xchg %ax,%ax */
1217static const unsigned char f32_3[] =
1218 {0x8d,0x76,0x00}; /* leal 0(%esi),%esi */
1219static const unsigned char f32_4[] =
1220 {0x8d,0x74,0x26,0x00}; /* leal 0(%esi,1),%esi */
62a02d25
L
1221static const unsigned char f32_6[] =
1222 {0x8d,0xb6,0x00,0x00,0x00,0x00}; /* leal 0L(%esi),%esi */
1223static const unsigned char f32_7[] =
1224 {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal 0L(%esi,1),%esi */
62a02d25 1225static const unsigned char f16_3[] =
3ae729d5 1226 {0x8d,0x74,0x00}; /* lea 0(%si),%si */
62a02d25 1227static const unsigned char f16_4[] =
3ae729d5
L
1228 {0x8d,0xb4,0x00,0x00}; /* lea 0W(%si),%si */
1229static const unsigned char jump_disp8[] =
1230 {0xeb}; /* jmp disp8 */
1231static const unsigned char jump32_disp32[] =
1232 {0xe9}; /* jmp disp32 */
1233static const unsigned char jump16_disp32[] =
1234 {0x66,0xe9}; /* jmp disp32 */
62a02d25
L
1235/* 32-bit NOPs patterns. */
1236static const unsigned char *const f32_patt[] = {
3ae729d5 1237 f32_1, f32_2, f32_3, f32_4, NULL, f32_6, f32_7
62a02d25
L
1238};
1239/* 16-bit NOPs patterns. */
1240static const unsigned char *const f16_patt[] = {
3ae729d5 1241 f32_1, f32_2, f16_3, f16_4
62a02d25
L
1242};
1243/* nopl (%[re]ax) */
1244static const unsigned char alt_3[] =
1245 {0x0f,0x1f,0x00};
1246/* nopl 0(%[re]ax) */
1247static const unsigned char alt_4[] =
1248 {0x0f,0x1f,0x40,0x00};
1249/* nopl 0(%[re]ax,%[re]ax,1) */
1250static const unsigned char alt_5[] =
1251 {0x0f,0x1f,0x44,0x00,0x00};
1252/* nopw 0(%[re]ax,%[re]ax,1) */
1253static const unsigned char alt_6[] =
1254 {0x66,0x0f,0x1f,0x44,0x00,0x00};
1255/* nopl 0L(%[re]ax) */
1256static const unsigned char alt_7[] =
1257 {0x0f,0x1f,0x80,0x00,0x00,0x00,0x00};
1258/* nopl 0L(%[re]ax,%[re]ax,1) */
1259static const unsigned char alt_8[] =
1260 {0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1261/* nopw 0L(%[re]ax,%[re]ax,1) */
1262static const unsigned char alt_9[] =
1263 {0x66,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1264/* nopw %cs:0L(%[re]ax,%[re]ax,1) */
1265static const unsigned char alt_10[] =
1266 {0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
3ae729d5
L
1267/* data16 nopw %cs:0L(%eax,%eax,1) */
1268static const unsigned char alt_11[] =
1269 {0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
62a02d25
L
1270/* 32-bit and 64-bit NOPs patterns. */
1271static const unsigned char *const alt_patt[] = {
1272 f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
3ae729d5 1273 alt_9, alt_10, alt_11
62a02d25
L
1274};
1275
1276/* Genenerate COUNT bytes of NOPs to WHERE from PATT with the maximum
1277 size of a single NOP instruction MAX_SINGLE_NOP_SIZE. */
1278
1279static void
1280i386_output_nops (char *where, const unsigned char *const *patt,
1281 int count, int max_single_nop_size)
1282
1283{
3ae729d5
L
1284 /* Place the longer NOP first. */
1285 int last;
1286 int offset;
3076e594
NC
1287 const unsigned char *nops;
1288
1289 if (max_single_nop_size < 1)
1290 {
1291 as_fatal (_("i386_output_nops called to generate nops of at most %d bytes!"),
1292 max_single_nop_size);
1293 return;
1294 }
1295
1296 nops = patt[max_single_nop_size - 1];
3ae729d5
L
1297
1298 /* Use the smaller one if the requsted one isn't available. */
1299 if (nops == NULL)
62a02d25 1300 {
3ae729d5
L
1301 max_single_nop_size--;
1302 nops = patt[max_single_nop_size - 1];
62a02d25
L
1303 }
1304
3ae729d5
L
1305 last = count % max_single_nop_size;
1306
1307 count -= last;
1308 for (offset = 0; offset < count; offset += max_single_nop_size)
1309 memcpy (where + offset, nops, max_single_nop_size);
1310
1311 if (last)
1312 {
1313 nops = patt[last - 1];
1314 if (nops == NULL)
1315 {
1316 /* Use the smaller one plus one-byte NOP if the needed one
1317 isn't available. */
1318 last--;
1319 nops = patt[last - 1];
1320 memcpy (where + offset, nops, last);
1321 where[offset + last] = *patt[0];
1322 }
1323 else
1324 memcpy (where + offset, nops, last);
1325 }
62a02d25
L
1326}
1327
3ae729d5
L
1328static INLINE int
1329fits_in_imm7 (offsetT num)
1330{
1331 return (num & 0x7f) == num;
1332}
1333
1334static INLINE int
1335fits_in_imm31 (offsetT num)
1336{
1337 return (num & 0x7fffffff) == num;
1338}
62a02d25
L
1339
1340/* Genenerate COUNT bytes of NOPs to WHERE with the maximum size of a
1341 single NOP instruction LIMIT. */
1342
1343void
3ae729d5 1344i386_generate_nops (fragS *fragP, char *where, offsetT count, int limit)
62a02d25 1345{
3ae729d5 1346 const unsigned char *const *patt = NULL;
62a02d25 1347 int max_single_nop_size;
3ae729d5
L
1348 /* Maximum number of NOPs before switching to jump over NOPs. */
1349 int max_number_of_nops;
62a02d25 1350
3ae729d5 1351 switch (fragP->fr_type)
62a02d25 1352 {
3ae729d5
L
1353 case rs_fill_nop:
1354 case rs_align_code:
1355 break;
e379e5f3
L
1356 case rs_machine_dependent:
1357 /* Allow NOP padding for jumps and calls. */
1358 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
1359 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
1360 break;
1361 /* Fall through. */
3ae729d5 1362 default:
62a02d25
L
1363 return;
1364 }
1365
ccc9c027
L
1366 /* We need to decide which NOP sequence to use for 32bit and
1367 64bit. When -mtune= is used:
4eed87de 1368
76bc74dc
L
1369 1. For PROCESSOR_I386, PROCESSOR_I486, PROCESSOR_PENTIUM and
1370 PROCESSOR_GENERIC32, f32_patt will be used.
80b8656c
L
1371 2. For the rest, alt_patt will be used.
1372
1373 When -mtune= isn't used, alt_patt will be used if
22109423 1374 cpu_arch_isa_flags has CpuNop. Otherwise, f32_patt will
76bc74dc 1375 be used.
ccc9c027
L
1376
1377 When -march= or .arch is used, we can't use anything beyond
1378 cpu_arch_isa_flags. */
1379
1380 if (flag_code == CODE_16BIT)
1381 {
3ae729d5
L
1382 patt = f16_patt;
1383 max_single_nop_size = sizeof (f16_patt) / sizeof (f16_patt[0]);
1384 /* Limit number of NOPs to 2 in 16-bit mode. */
1385 max_number_of_nops = 2;
252b5132 1386 }
33fef721 1387 else
ccc9c027 1388 {
fbf3f584 1389 if (fragP->tc_frag_data.isa == PROCESSOR_UNKNOWN)
ccc9c027
L
1390 {
1391 /* PROCESSOR_UNKNOWN means that all ISAs may be used. */
1392 switch (cpu_arch_tune)
1393 {
1394 case PROCESSOR_UNKNOWN:
1395 /* We use cpu_arch_isa_flags to check if we SHOULD
22109423
L
1396 optimize with nops. */
1397 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1398 patt = alt_patt;
ccc9c027
L
1399 else
1400 patt = f32_patt;
1401 break;
ccc9c027
L
1402 case PROCESSOR_PENTIUM4:
1403 case PROCESSOR_NOCONA:
ef05d495 1404 case PROCESSOR_CORE:
76bc74dc 1405 case PROCESSOR_CORE2:
bd5295b2 1406 case PROCESSOR_COREI7:
76bc74dc 1407 case PROCESSOR_GENERIC64:
ccc9c027
L
1408 case PROCESSOR_K6:
1409 case PROCESSOR_ATHLON:
1410 case PROCESSOR_K8:
4eed87de 1411 case PROCESSOR_AMDFAM10:
8aedb9fe 1412 case PROCESSOR_BD:
029f3522 1413 case PROCESSOR_ZNVER:
7b458c12 1414 case PROCESSOR_BT:
80b8656c 1415 patt = alt_patt;
ccc9c027 1416 break;
76bc74dc 1417 case PROCESSOR_I386:
ccc9c027
L
1418 case PROCESSOR_I486:
1419 case PROCESSOR_PENTIUM:
2dde1948 1420 case PROCESSOR_PENTIUMPRO:
81486035 1421 case PROCESSOR_IAMCU:
ccc9c027
L
1422 case PROCESSOR_GENERIC32:
1423 patt = f32_patt;
1424 break;
c368d2a8
JB
1425 case PROCESSOR_NONE:
1426 abort ();
4eed87de 1427 }
ccc9c027
L
1428 }
1429 else
1430 {
fbf3f584 1431 switch (fragP->tc_frag_data.tune)
ccc9c027
L
1432 {
1433 case PROCESSOR_UNKNOWN:
e6a14101 1434 /* When cpu_arch_isa is set, cpu_arch_tune shouldn't be
ccc9c027
L
1435 PROCESSOR_UNKNOWN. */
1436 abort ();
1437 break;
1438
76bc74dc 1439 case PROCESSOR_I386:
ccc9c027
L
1440 case PROCESSOR_I486:
1441 case PROCESSOR_PENTIUM:
81486035 1442 case PROCESSOR_IAMCU:
ccc9c027
L
1443 case PROCESSOR_K6:
1444 case PROCESSOR_ATHLON:
1445 case PROCESSOR_K8:
4eed87de 1446 case PROCESSOR_AMDFAM10:
8aedb9fe 1447 case PROCESSOR_BD:
029f3522 1448 case PROCESSOR_ZNVER:
7b458c12 1449 case PROCESSOR_BT:
ccc9c027
L
1450 case PROCESSOR_GENERIC32:
1451 /* We use cpu_arch_isa_flags to check if we CAN optimize
22109423
L
1452 with nops. */
1453 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1454 patt = alt_patt;
ccc9c027
L
1455 else
1456 patt = f32_patt;
1457 break;
76bc74dc
L
1458 case PROCESSOR_PENTIUMPRO:
1459 case PROCESSOR_PENTIUM4:
1460 case PROCESSOR_NOCONA:
1461 case PROCESSOR_CORE:
ef05d495 1462 case PROCESSOR_CORE2:
bd5295b2 1463 case PROCESSOR_COREI7:
22109423 1464 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1465 patt = alt_patt;
ccc9c027
L
1466 else
1467 patt = f32_patt;
1468 break;
1469 case PROCESSOR_GENERIC64:
80b8656c 1470 patt = alt_patt;
ccc9c027 1471 break;
c368d2a8
JB
1472 case PROCESSOR_NONE:
1473 abort ();
4eed87de 1474 }
ccc9c027
L
1475 }
1476
76bc74dc
L
1477 if (patt == f32_patt)
1478 {
3ae729d5
L
1479 max_single_nop_size = sizeof (f32_patt) / sizeof (f32_patt[0]);
1480 /* Limit number of NOPs to 2 for older processors. */
1481 max_number_of_nops = 2;
76bc74dc
L
1482 }
1483 else
1484 {
3ae729d5
L
1485 max_single_nop_size = sizeof (alt_patt) / sizeof (alt_patt[0]);
1486 /* Limit number of NOPs to 7 for newer processors. */
1487 max_number_of_nops = 7;
1488 }
1489 }
1490
1491 if (limit == 0)
1492 limit = max_single_nop_size;
1493
1494 if (fragP->fr_type == rs_fill_nop)
1495 {
1496 /* Output NOPs for .nop directive. */
1497 if (limit > max_single_nop_size)
1498 {
1499 as_bad_where (fragP->fr_file, fragP->fr_line,
1500 _("invalid single nop size: %d "
1501 "(expect within [0, %d])"),
1502 limit, max_single_nop_size);
1503 return;
1504 }
1505 }
e379e5f3 1506 else if (fragP->fr_type != rs_machine_dependent)
3ae729d5
L
1507 fragP->fr_var = count;
1508
1509 if ((count / max_single_nop_size) > max_number_of_nops)
1510 {
1511 /* Generate jump over NOPs. */
1512 offsetT disp = count - 2;
1513 if (fits_in_imm7 (disp))
1514 {
1515 /* Use "jmp disp8" if possible. */
1516 count = disp;
1517 where[0] = jump_disp8[0];
1518 where[1] = count;
1519 where += 2;
1520 }
1521 else
1522 {
1523 unsigned int size_of_jump;
1524
1525 if (flag_code == CODE_16BIT)
1526 {
1527 where[0] = jump16_disp32[0];
1528 where[1] = jump16_disp32[1];
1529 size_of_jump = 2;
1530 }
1531 else
1532 {
1533 where[0] = jump32_disp32[0];
1534 size_of_jump = 1;
1535 }
1536
1537 count -= size_of_jump + 4;
1538 if (!fits_in_imm31 (count))
1539 {
1540 as_bad_where (fragP->fr_file, fragP->fr_line,
1541 _("jump over nop padding out of range"));
1542 return;
1543 }
1544
1545 md_number_to_chars (where + size_of_jump, count, 4);
1546 where += size_of_jump + 4;
76bc74dc 1547 }
ccc9c027 1548 }
3ae729d5
L
1549
1550 /* Generate multiple NOPs. */
1551 i386_output_nops (where, patt, count, limit);
252b5132
RH
1552}
1553
c6fb90c8 1554static INLINE int
0dfbf9d7 1555operand_type_all_zero (const union i386_operand_type *x)
40fb9820 1556{
0dfbf9d7 1557 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1558 {
1559 case 3:
0dfbf9d7 1560 if (x->array[2])
c6fb90c8 1561 return 0;
1a0670f3 1562 /* Fall through. */
c6fb90c8 1563 case 2:
0dfbf9d7 1564 if (x->array[1])
c6fb90c8 1565 return 0;
1a0670f3 1566 /* Fall through. */
c6fb90c8 1567 case 1:
0dfbf9d7 1568 return !x->array[0];
c6fb90c8
L
1569 default:
1570 abort ();
1571 }
40fb9820
L
1572}
1573
c6fb90c8 1574static INLINE void
0dfbf9d7 1575operand_type_set (union i386_operand_type *x, unsigned int v)
40fb9820 1576{
0dfbf9d7 1577 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1578 {
1579 case 3:
0dfbf9d7 1580 x->array[2] = v;
1a0670f3 1581 /* Fall through. */
c6fb90c8 1582 case 2:
0dfbf9d7 1583 x->array[1] = v;
1a0670f3 1584 /* Fall through. */
c6fb90c8 1585 case 1:
0dfbf9d7 1586 x->array[0] = v;
1a0670f3 1587 /* Fall through. */
c6fb90c8
L
1588 break;
1589 default:
1590 abort ();
1591 }
bab6aec1
JB
1592
1593 x->bitfield.class = ClassNone;
75e5731b 1594 x->bitfield.instance = InstanceNone;
c6fb90c8 1595}
40fb9820 1596
c6fb90c8 1597static INLINE int
0dfbf9d7
L
1598operand_type_equal (const union i386_operand_type *x,
1599 const union i386_operand_type *y)
c6fb90c8 1600{
0dfbf9d7 1601 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1602 {
1603 case 3:
0dfbf9d7 1604 if (x->array[2] != y->array[2])
c6fb90c8 1605 return 0;
1a0670f3 1606 /* Fall through. */
c6fb90c8 1607 case 2:
0dfbf9d7 1608 if (x->array[1] != y->array[1])
c6fb90c8 1609 return 0;
1a0670f3 1610 /* Fall through. */
c6fb90c8 1611 case 1:
0dfbf9d7 1612 return x->array[0] == y->array[0];
c6fb90c8
L
1613 break;
1614 default:
1615 abort ();
1616 }
1617}
40fb9820 1618
0dfbf9d7
L
1619static INLINE int
1620cpu_flags_all_zero (const union i386_cpu_flags *x)
1621{
1622 switch (ARRAY_SIZE(x->array))
1623 {
75f8266a
KL
1624 case 5:
1625 if (x->array[4])
1626 return 0;
1627 /* Fall through. */
53467f57
IT
1628 case 4:
1629 if (x->array[3])
1630 return 0;
1631 /* Fall through. */
0dfbf9d7
L
1632 case 3:
1633 if (x->array[2])
1634 return 0;
1a0670f3 1635 /* Fall through. */
0dfbf9d7
L
1636 case 2:
1637 if (x->array[1])
1638 return 0;
1a0670f3 1639 /* Fall through. */
0dfbf9d7
L
1640 case 1:
1641 return !x->array[0];
1642 default:
1643 abort ();
1644 }
1645}
1646
0dfbf9d7
L
1647static INLINE int
1648cpu_flags_equal (const union i386_cpu_flags *x,
1649 const union i386_cpu_flags *y)
1650{
1651 switch (ARRAY_SIZE(x->array))
1652 {
75f8266a
KL
1653 case 5:
1654 if (x->array[4] != y->array[4])
1655 return 0;
1656 /* Fall through. */
53467f57
IT
1657 case 4:
1658 if (x->array[3] != y->array[3])
1659 return 0;
1660 /* Fall through. */
0dfbf9d7
L
1661 case 3:
1662 if (x->array[2] != y->array[2])
1663 return 0;
1a0670f3 1664 /* Fall through. */
0dfbf9d7
L
1665 case 2:
1666 if (x->array[1] != y->array[1])
1667 return 0;
1a0670f3 1668 /* Fall through. */
0dfbf9d7
L
1669 case 1:
1670 return x->array[0] == y->array[0];
1671 break;
1672 default:
1673 abort ();
1674 }
1675}
c6fb90c8
L
1676
1677static INLINE int
1678cpu_flags_check_cpu64 (i386_cpu_flags f)
1679{
1680 return !((flag_code == CODE_64BIT && f.bitfield.cpuno64)
1681 || (flag_code != CODE_64BIT && f.bitfield.cpu64));
40fb9820
L
1682}
1683
c6fb90c8
L
1684static INLINE i386_cpu_flags
1685cpu_flags_and (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1686{
c6fb90c8
L
1687 switch (ARRAY_SIZE (x.array))
1688 {
75f8266a
KL
1689 case 5:
1690 x.array [4] &= y.array [4];
1691 /* Fall through. */
53467f57
IT
1692 case 4:
1693 x.array [3] &= y.array [3];
1694 /* Fall through. */
c6fb90c8
L
1695 case 3:
1696 x.array [2] &= y.array [2];
1a0670f3 1697 /* Fall through. */
c6fb90c8
L
1698 case 2:
1699 x.array [1] &= y.array [1];
1a0670f3 1700 /* Fall through. */
c6fb90c8
L
1701 case 1:
1702 x.array [0] &= y.array [0];
1703 break;
1704 default:
1705 abort ();
1706 }
1707 return x;
1708}
40fb9820 1709
c6fb90c8
L
1710static INLINE i386_cpu_flags
1711cpu_flags_or (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1712{
c6fb90c8 1713 switch (ARRAY_SIZE (x.array))
40fb9820 1714 {
75f8266a
KL
1715 case 5:
1716 x.array [4] |= y.array [4];
1717 /* Fall through. */
53467f57
IT
1718 case 4:
1719 x.array [3] |= y.array [3];
1720 /* Fall through. */
c6fb90c8
L
1721 case 3:
1722 x.array [2] |= y.array [2];
1a0670f3 1723 /* Fall through. */
c6fb90c8
L
1724 case 2:
1725 x.array [1] |= y.array [1];
1a0670f3 1726 /* Fall through. */
c6fb90c8
L
1727 case 1:
1728 x.array [0] |= y.array [0];
40fb9820
L
1729 break;
1730 default:
1731 abort ();
1732 }
40fb9820
L
1733 return x;
1734}
1735
309d3373
JB
1736static INLINE i386_cpu_flags
1737cpu_flags_and_not (i386_cpu_flags x, i386_cpu_flags y)
1738{
1739 switch (ARRAY_SIZE (x.array))
1740 {
75f8266a
KL
1741 case 5:
1742 x.array [4] &= ~y.array [4];
1743 /* Fall through. */
53467f57
IT
1744 case 4:
1745 x.array [3] &= ~y.array [3];
1746 /* Fall through. */
309d3373
JB
1747 case 3:
1748 x.array [2] &= ~y.array [2];
1a0670f3 1749 /* Fall through. */
309d3373
JB
1750 case 2:
1751 x.array [1] &= ~y.array [1];
1a0670f3 1752 /* Fall through. */
309d3373
JB
1753 case 1:
1754 x.array [0] &= ~y.array [0];
1755 break;
1756 default:
1757 abort ();
1758 }
1759 return x;
1760}
1761
6c0946d0
JB
1762static const i386_cpu_flags avx512 = CPU_ANY_AVX512F_FLAGS;
1763
c0f3af97
L
1764#define CPU_FLAGS_ARCH_MATCH 0x1
1765#define CPU_FLAGS_64BIT_MATCH 0x2
1766
c0f3af97 1767#define CPU_FLAGS_PERFECT_MATCH \
db12e14e 1768 (CPU_FLAGS_ARCH_MATCH | CPU_FLAGS_64BIT_MATCH)
c0f3af97
L
1769
1770/* Return CPU flags match bits. */
3629bb00 1771
40fb9820 1772static int
d3ce72d0 1773cpu_flags_match (const insn_template *t)
40fb9820 1774{
c0f3af97
L
1775 i386_cpu_flags x = t->cpu_flags;
1776 int match = cpu_flags_check_cpu64 (x) ? CPU_FLAGS_64BIT_MATCH : 0;
40fb9820
L
1777
1778 x.bitfield.cpu64 = 0;
1779 x.bitfield.cpuno64 = 0;
1780
0dfbf9d7 1781 if (cpu_flags_all_zero (&x))
c0f3af97
L
1782 {
1783 /* This instruction is available on all archs. */
db12e14e 1784 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 1785 }
3629bb00
L
1786 else
1787 {
c0f3af97 1788 /* This instruction is available only on some archs. */
3629bb00
L
1789 i386_cpu_flags cpu = cpu_arch_flags;
1790
ab592e75
JB
1791 /* AVX512VL is no standalone feature - match it and then strip it. */
1792 if (x.bitfield.cpuavx512vl && !cpu.bitfield.cpuavx512vl)
1793 return match;
1794 x.bitfield.cpuavx512vl = 0;
1795
22c36940
JB
1796 /* AVX and AVX2 present at the same time express an operand size
1797 dependency - strip AVX2 for the purposes here. The operand size
1798 dependent check occurs in check_vecOperands(). */
1799 if (x.bitfield.cpuavx && x.bitfield.cpuavx2)
1800 x.bitfield.cpuavx2 = 0;
1801
3629bb00 1802 cpu = cpu_flags_and (x, cpu);
c0f3af97
L
1803 if (!cpu_flags_all_zero (&cpu))
1804 {
57392598 1805 if (x.bitfield.cpuavx)
a5ff0eb2 1806 {
929f69fa 1807 /* We need to check a few extra flags with AVX. */
b9d49817 1808 if (cpu.bitfield.cpuavx
40d231b4
JB
1809 && (!t->opcode_modifier.sse2avx
1810 || (sse2avx && !i.prefix[DATA_PREFIX]))
b9d49817 1811 && (!x.bitfield.cpuaes || cpu.bitfield.cpuaes)
929f69fa 1812 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
b9d49817
JB
1813 && (!x.bitfield.cpupclmul || cpu.bitfield.cpupclmul))
1814 match |= CPU_FLAGS_ARCH_MATCH;
a5ff0eb2 1815 }
929f69fa
JB
1816 else if (x.bitfield.cpuavx512f)
1817 {
1818 /* We need to check a few extra flags with AVX512F. */
1819 if (cpu.bitfield.cpuavx512f
1820 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
1821 && (!x.bitfield.cpuvaes || cpu.bitfield.cpuvaes)
1822 && (!x.bitfield.cpuvpclmulqdq || cpu.bitfield.cpuvpclmulqdq))
1823 match |= CPU_FLAGS_ARCH_MATCH;
1824 }
a5ff0eb2 1825 else
db12e14e 1826 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 1827 }
3629bb00 1828 }
c0f3af97 1829 return match;
40fb9820
L
1830}
1831
c6fb90c8
L
1832static INLINE i386_operand_type
1833operand_type_and (i386_operand_type x, i386_operand_type y)
40fb9820 1834{
bab6aec1
JB
1835 if (x.bitfield.class != y.bitfield.class)
1836 x.bitfield.class = ClassNone;
75e5731b
JB
1837 if (x.bitfield.instance != y.bitfield.instance)
1838 x.bitfield.instance = InstanceNone;
bab6aec1 1839
c6fb90c8
L
1840 switch (ARRAY_SIZE (x.array))
1841 {
1842 case 3:
1843 x.array [2] &= y.array [2];
1a0670f3 1844 /* Fall through. */
c6fb90c8
L
1845 case 2:
1846 x.array [1] &= y.array [1];
1a0670f3 1847 /* Fall through. */
c6fb90c8
L
1848 case 1:
1849 x.array [0] &= y.array [0];
1850 break;
1851 default:
1852 abort ();
1853 }
1854 return x;
40fb9820
L
1855}
1856
73053c1f
JB
1857static INLINE i386_operand_type
1858operand_type_and_not (i386_operand_type x, i386_operand_type y)
1859{
bab6aec1 1860 gas_assert (y.bitfield.class == ClassNone);
75e5731b 1861 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 1862
73053c1f
JB
1863 switch (ARRAY_SIZE (x.array))
1864 {
1865 case 3:
1866 x.array [2] &= ~y.array [2];
1867 /* Fall through. */
1868 case 2:
1869 x.array [1] &= ~y.array [1];
1870 /* Fall through. */
1871 case 1:
1872 x.array [0] &= ~y.array [0];
1873 break;
1874 default:
1875 abort ();
1876 }
1877 return x;
1878}
1879
c6fb90c8
L
1880static INLINE i386_operand_type
1881operand_type_or (i386_operand_type x, i386_operand_type y)
40fb9820 1882{
bab6aec1
JB
1883 gas_assert (x.bitfield.class == ClassNone ||
1884 y.bitfield.class == ClassNone ||
1885 x.bitfield.class == y.bitfield.class);
75e5731b
JB
1886 gas_assert (x.bitfield.instance == InstanceNone ||
1887 y.bitfield.instance == InstanceNone ||
1888 x.bitfield.instance == y.bitfield.instance);
bab6aec1 1889
c6fb90c8 1890 switch (ARRAY_SIZE (x.array))
40fb9820 1891 {
c6fb90c8
L
1892 case 3:
1893 x.array [2] |= y.array [2];
1a0670f3 1894 /* Fall through. */
c6fb90c8
L
1895 case 2:
1896 x.array [1] |= y.array [1];
1a0670f3 1897 /* Fall through. */
c6fb90c8
L
1898 case 1:
1899 x.array [0] |= y.array [0];
40fb9820
L
1900 break;
1901 default:
1902 abort ();
1903 }
c6fb90c8
L
1904 return x;
1905}
40fb9820 1906
c6fb90c8
L
1907static INLINE i386_operand_type
1908operand_type_xor (i386_operand_type x, i386_operand_type y)
1909{
bab6aec1 1910 gas_assert (y.bitfield.class == ClassNone);
75e5731b 1911 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 1912
c6fb90c8
L
1913 switch (ARRAY_SIZE (x.array))
1914 {
1915 case 3:
1916 x.array [2] ^= y.array [2];
1a0670f3 1917 /* Fall through. */
c6fb90c8
L
1918 case 2:
1919 x.array [1] ^= y.array [1];
1a0670f3 1920 /* Fall through. */
c6fb90c8
L
1921 case 1:
1922 x.array [0] ^= y.array [0];
1923 break;
1924 default:
1925 abort ();
1926 }
40fb9820
L
1927 return x;
1928}
1929
40fb9820 1930static const i386_operand_type disp16_32 = OPERAND_TYPE_DISP16_32;
bab6aec1 1931static const i386_operand_type anydisp = OPERAND_TYPE_ANYDISP;
40fb9820
L
1932static const i386_operand_type regxmm = OPERAND_TYPE_REGXMM;
1933static const i386_operand_type imm8 = OPERAND_TYPE_IMM8;
1934static const i386_operand_type imm8s = OPERAND_TYPE_IMM8S;
1935static const i386_operand_type imm16 = OPERAND_TYPE_IMM16;
1936static const i386_operand_type imm32 = OPERAND_TYPE_IMM32;
1937static const i386_operand_type imm32s = OPERAND_TYPE_IMM32S;
1938static const i386_operand_type imm64 = OPERAND_TYPE_IMM64;
1939static const i386_operand_type imm16_32 = OPERAND_TYPE_IMM16_32;
1940static const i386_operand_type imm16_32s = OPERAND_TYPE_IMM16_32S;
1941static const i386_operand_type imm16_32_32s = OPERAND_TYPE_IMM16_32_32S;
1942
1943enum operand_type
1944{
1945 reg,
40fb9820
L
1946 imm,
1947 disp,
1948 anymem
1949};
1950
c6fb90c8 1951static INLINE int
40fb9820
L
1952operand_type_check (i386_operand_type t, enum operand_type c)
1953{
1954 switch (c)
1955 {
1956 case reg:
bab6aec1 1957 return t.bitfield.class == Reg;
40fb9820 1958
40fb9820
L
1959 case imm:
1960 return (t.bitfield.imm8
1961 || t.bitfield.imm8s
1962 || t.bitfield.imm16
1963 || t.bitfield.imm32
1964 || t.bitfield.imm32s
1965 || t.bitfield.imm64);
1966
1967 case disp:
1968 return (t.bitfield.disp8
1969 || t.bitfield.disp16
1970 || t.bitfield.disp32
40fb9820
L
1971 || t.bitfield.disp64);
1972
1973 case anymem:
1974 return (t.bitfield.disp8
1975 || t.bitfield.disp16
1976 || t.bitfield.disp32
40fb9820
L
1977 || t.bitfield.disp64
1978 || t.bitfield.baseindex);
1979
1980 default:
1981 abort ();
1982 }
2cfe26b6
AM
1983
1984 return 0;
40fb9820
L
1985}
1986
7a54636a
L
1987/* Return 1 if there is no conflict in 8bit/16bit/32bit/64bit/80bit size
1988 between operand GIVEN and opeand WANTED for instruction template T. */
5c07affc
L
1989
1990static INLINE int
7a54636a
L
1991match_operand_size (const insn_template *t, unsigned int wanted,
1992 unsigned int given)
5c07affc 1993{
3ac21baa
JB
1994 return !((i.types[given].bitfield.byte
1995 && !t->operand_types[wanted].bitfield.byte)
1996 || (i.types[given].bitfield.word
1997 && !t->operand_types[wanted].bitfield.word)
1998 || (i.types[given].bitfield.dword
1999 && !t->operand_types[wanted].bitfield.dword)
2000 || (i.types[given].bitfield.qword
2001 && !t->operand_types[wanted].bitfield.qword)
2002 || (i.types[given].bitfield.tbyte
2003 && !t->operand_types[wanted].bitfield.tbyte));
5c07affc
L
2004}
2005
dd40ce22
L
2006/* Return 1 if there is no conflict in SIMD register between operand
2007 GIVEN and opeand WANTED for instruction template T. */
1b54b8d7
JB
2008
2009static INLINE int
dd40ce22
L
2010match_simd_size (const insn_template *t, unsigned int wanted,
2011 unsigned int given)
1b54b8d7 2012{
3ac21baa
JB
2013 return !((i.types[given].bitfield.xmmword
2014 && !t->operand_types[wanted].bitfield.xmmword)
2015 || (i.types[given].bitfield.ymmword
2016 && !t->operand_types[wanted].bitfield.ymmword)
2017 || (i.types[given].bitfield.zmmword
260cd341
LC
2018 && !t->operand_types[wanted].bitfield.zmmword)
2019 || (i.types[given].bitfield.tmmword
2020 && !t->operand_types[wanted].bitfield.tmmword));
1b54b8d7
JB
2021}
2022
7a54636a
L
2023/* Return 1 if there is no conflict in any size between operand GIVEN
2024 and opeand WANTED for instruction template T. */
5c07affc
L
2025
2026static INLINE int
dd40ce22
L
2027match_mem_size (const insn_template *t, unsigned int wanted,
2028 unsigned int given)
5c07affc 2029{
7a54636a 2030 return (match_operand_size (t, wanted, given)
3ac21baa 2031 && !((i.types[given].bitfield.unspecified
5273a3cd 2032 && !i.broadcast.type
a5748e0d 2033 && !i.broadcast.bytes
3ac21baa
JB
2034 && !t->operand_types[wanted].bitfield.unspecified)
2035 || (i.types[given].bitfield.fword
2036 && !t->operand_types[wanted].bitfield.fword)
1b54b8d7
JB
2037 /* For scalar opcode templates to allow register and memory
2038 operands at the same time, some special casing is needed
d6793fa1
JB
2039 here. Also for v{,p}broadcast*, {,v}pmov{s,z}*, and
2040 down-conversion vpmov*. */
3528c362 2041 || ((t->operand_types[wanted].bitfield.class == RegSIMD
bc49bfd8
JB
2042 && t->operand_types[wanted].bitfield.byte
2043 + t->operand_types[wanted].bitfield.word
2044 + t->operand_types[wanted].bitfield.dword
2045 + t->operand_types[wanted].bitfield.qword
2046 > !!t->opcode_modifier.broadcast)
3ac21baa
JB
2047 ? (i.types[given].bitfield.xmmword
2048 || i.types[given].bitfield.ymmword
2049 || i.types[given].bitfield.zmmword)
2050 : !match_simd_size(t, wanted, given))));
5c07affc
L
2051}
2052
3ac21baa
JB
2053/* Return value has MATCH_STRAIGHT set if there is no size conflict on any
2054 operands for instruction template T, and it has MATCH_REVERSE set if there
2055 is no size conflict on any operands for the template with operands reversed
2056 (and the template allows for reversing in the first place). */
5c07affc 2057
3ac21baa
JB
2058#define MATCH_STRAIGHT 1
2059#define MATCH_REVERSE 2
2060
2061static INLINE unsigned int
d3ce72d0 2062operand_size_match (const insn_template *t)
5c07affc 2063{
3ac21baa 2064 unsigned int j, match = MATCH_STRAIGHT;
5c07affc 2065
0cfa3eb3 2066 /* Don't check non-absolute jump instructions. */
5c07affc 2067 if (t->opcode_modifier.jump
0cfa3eb3 2068 && t->opcode_modifier.jump != JUMP_ABSOLUTE)
5c07affc
L
2069 return match;
2070
2071 /* Check memory and accumulator operand size. */
2072 for (j = 0; j < i.operands; j++)
2073 {
3528c362
JB
2074 if (i.types[j].bitfield.class != Reg
2075 && i.types[j].bitfield.class != RegSIMD
601e8564 2076 && t->opcode_modifier.anysize)
5c07affc
L
2077 continue;
2078
bab6aec1 2079 if (t->operand_types[j].bitfield.class == Reg
7a54636a 2080 && !match_operand_size (t, j, j))
5c07affc
L
2081 {
2082 match = 0;
2083 break;
2084 }
2085
3528c362 2086 if (t->operand_types[j].bitfield.class == RegSIMD
3ac21baa 2087 && !match_simd_size (t, j, j))
1b54b8d7
JB
2088 {
2089 match = 0;
2090 break;
2091 }
2092
75e5731b 2093 if (t->operand_types[j].bitfield.instance == Accum
7a54636a 2094 && (!match_operand_size (t, j, j) || !match_simd_size (t, j, j)))
1b54b8d7
JB
2095 {
2096 match = 0;
2097 break;
2098 }
2099
c48dadc9 2100 if ((i.flags[j] & Operand_Mem) && !match_mem_size (t, j, j))
5c07affc
L
2101 {
2102 match = 0;
2103 break;
2104 }
2105 }
2106
3ac21baa 2107 if (!t->opcode_modifier.d)
7b94647a 2108 return match;
5c07affc
L
2109
2110 /* Check reverse. */
8bd915b7
JB
2111 gas_assert ((i.operands >= 2 && i.operands <= 3)
2112 || t->opcode_modifier.vexsources);
5c07affc 2113
f5eb1d70 2114 for (j = 0; j < i.operands; j++)
5c07affc 2115 {
f5eb1d70
JB
2116 unsigned int given = i.operands - j - 1;
2117
8bd915b7
JB
2118 /* For 4- and 5-operand insns VEX.W controls just the first two
2119 register operands. */
2120 if (t->opcode_modifier.vexsources)
2121 given = j < 2 ? 1 - j : j;
2122
bab6aec1 2123 if (t->operand_types[j].bitfield.class == Reg
f5eb1d70 2124 && !match_operand_size (t, j, given))
7b94647a 2125 return match;
5c07affc 2126
3528c362 2127 if (t->operand_types[j].bitfield.class == RegSIMD
f5eb1d70 2128 && !match_simd_size (t, j, given))
7b94647a 2129 return match;
dbbc8b7e 2130
75e5731b 2131 if (t->operand_types[j].bitfield.instance == Accum
f5eb1d70
JB
2132 && (!match_operand_size (t, j, given)
2133 || !match_simd_size (t, j, given)))
7b94647a 2134 return match;
dbbc8b7e 2135
f5eb1d70 2136 if ((i.flags[given] & Operand_Mem) && !match_mem_size (t, j, given))
7b94647a 2137 return match;
5c07affc
L
2138 }
2139
3ac21baa 2140 return match | MATCH_REVERSE;
5c07affc
L
2141}
2142
c6fb90c8 2143static INLINE int
40fb9820
L
2144operand_type_match (i386_operand_type overlap,
2145 i386_operand_type given)
2146{
2147 i386_operand_type temp = overlap;
2148
7d5e4556 2149 temp.bitfield.unspecified = 0;
5c07affc
L
2150 temp.bitfield.byte = 0;
2151 temp.bitfield.word = 0;
2152 temp.bitfield.dword = 0;
2153 temp.bitfield.fword = 0;
2154 temp.bitfield.qword = 0;
2155 temp.bitfield.tbyte = 0;
2156 temp.bitfield.xmmword = 0;
c0f3af97 2157 temp.bitfield.ymmword = 0;
43234a1e 2158 temp.bitfield.zmmword = 0;
260cd341 2159 temp.bitfield.tmmword = 0;
0dfbf9d7 2160 if (operand_type_all_zero (&temp))
891edac4 2161 goto mismatch;
40fb9820 2162
6f2f06be 2163 if (given.bitfield.baseindex == overlap.bitfield.baseindex)
891edac4
L
2164 return 1;
2165
dc1e8a47 2166 mismatch:
a65babc9 2167 i.error = operand_type_mismatch;
891edac4 2168 return 0;
40fb9820
L
2169}
2170
7d5e4556 2171/* If given types g0 and g1 are registers they must be of the same type
10c17abd 2172 unless the expected operand type register overlap is null.
5de4d9ef 2173 Some Intel syntax memory operand size checking also happens here. */
40fb9820 2174
c6fb90c8 2175static INLINE int
dc821c5f 2176operand_type_register_match (i386_operand_type g0,
40fb9820 2177 i386_operand_type t0,
40fb9820
L
2178 i386_operand_type g1,
2179 i386_operand_type t1)
2180{
bab6aec1 2181 if (g0.bitfield.class != Reg
3528c362 2182 && g0.bitfield.class != RegSIMD
10c17abd
JB
2183 && (!operand_type_check (g0, anymem)
2184 || g0.bitfield.unspecified
5de4d9ef
JB
2185 || (t0.bitfield.class != Reg
2186 && t0.bitfield.class != RegSIMD)))
40fb9820
L
2187 return 1;
2188
bab6aec1 2189 if (g1.bitfield.class != Reg
3528c362 2190 && g1.bitfield.class != RegSIMD
10c17abd
JB
2191 && (!operand_type_check (g1, anymem)
2192 || g1.bitfield.unspecified
5de4d9ef
JB
2193 || (t1.bitfield.class != Reg
2194 && t1.bitfield.class != RegSIMD)))
40fb9820
L
2195 return 1;
2196
dc821c5f
JB
2197 if (g0.bitfield.byte == g1.bitfield.byte
2198 && g0.bitfield.word == g1.bitfield.word
2199 && g0.bitfield.dword == g1.bitfield.dword
10c17abd
JB
2200 && g0.bitfield.qword == g1.bitfield.qword
2201 && g0.bitfield.xmmword == g1.bitfield.xmmword
2202 && g0.bitfield.ymmword == g1.bitfield.ymmword
2203 && g0.bitfield.zmmword == g1.bitfield.zmmword)
40fb9820
L
2204 return 1;
2205
c4d09633
JB
2206 /* If expectations overlap in no more than a single size, all is fine. */
2207 g0 = operand_type_and (t0, t1);
2208 if (g0.bitfield.byte
2209 + g0.bitfield.word
2210 + g0.bitfield.dword
2211 + g0.bitfield.qword
2212 + g0.bitfield.xmmword
2213 + g0.bitfield.ymmword
2214 + g0.bitfield.zmmword <= 1)
891edac4
L
2215 return 1;
2216
a65babc9 2217 i.error = register_type_mismatch;
891edac4
L
2218
2219 return 0;
40fb9820
L
2220}
2221
4c692bc7
JB
2222static INLINE unsigned int
2223register_number (const reg_entry *r)
2224{
2225 unsigned int nr = r->reg_num;
2226
2227 if (r->reg_flags & RegRex)
2228 nr += 8;
2229
200cbe0f
L
2230 if (r->reg_flags & RegVRex)
2231 nr += 16;
2232
4c692bc7
JB
2233 return nr;
2234}
2235
252b5132 2236static INLINE unsigned int
40fb9820 2237mode_from_disp_size (i386_operand_type t)
252b5132 2238{
b5014f7a 2239 if (t.bitfield.disp8)
40fb9820
L
2240 return 1;
2241 else if (t.bitfield.disp16
a775efc8 2242 || t.bitfield.disp32)
40fb9820
L
2243 return 2;
2244 else
2245 return 0;
252b5132
RH
2246}
2247
2248static INLINE int
65879393 2249fits_in_signed_byte (addressT num)
252b5132 2250{
65879393 2251 return num + 0x80 <= 0xff;
47926f60 2252}
252b5132
RH
2253
2254static INLINE int
65879393 2255fits_in_unsigned_byte (addressT num)
252b5132 2256{
65879393 2257 return num <= 0xff;
47926f60 2258}
252b5132
RH
2259
2260static INLINE int
65879393 2261fits_in_unsigned_word (addressT num)
252b5132 2262{
65879393 2263 return num <= 0xffff;
47926f60 2264}
252b5132
RH
2265
2266static INLINE int
65879393 2267fits_in_signed_word (addressT num)
252b5132 2268{
65879393 2269 return num + 0x8000 <= 0xffff;
47926f60 2270}
2a962e6d 2271
3e73aa7c 2272static INLINE int
65879393 2273fits_in_signed_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2274{
2275#ifndef BFD64
2276 return 1;
2277#else
65879393 2278 return num + 0x80000000 <= 0xffffffff;
3e73aa7c
JH
2279#endif
2280} /* fits_in_signed_long() */
2a962e6d 2281
3e73aa7c 2282static INLINE int
65879393 2283fits_in_unsigned_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2284{
2285#ifndef BFD64
2286 return 1;
2287#else
65879393 2288 return num <= 0xffffffff;
3e73aa7c
JH
2289#endif
2290} /* fits_in_unsigned_long() */
252b5132 2291
a442cac5
JB
2292static INLINE valueT extend_to_32bit_address (addressT num)
2293{
2294#ifdef BFD64
2295 if (fits_in_unsigned_long(num))
2296 return (num ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
2297
2298 if (!fits_in_signed_long (num))
2299 return num & 0xffffffff;
2300#endif
2301
2302 return num;
2303}
2304
43234a1e 2305static INLINE int
b5014f7a 2306fits_in_disp8 (offsetT num)
43234a1e
L
2307{
2308 int shift = i.memshift;
2309 unsigned int mask;
2310
2311 if (shift == -1)
2312 abort ();
2313
2314 mask = (1 << shift) - 1;
2315
2316 /* Return 0 if NUM isn't properly aligned. */
2317 if ((num & mask))
2318 return 0;
2319
2320 /* Check if NUM will fit in 8bit after shift. */
2321 return fits_in_signed_byte (num >> shift);
2322}
2323
a683cc34
SP
2324static INLINE int
2325fits_in_imm4 (offsetT num)
2326{
2327 return (num & 0xf) == num;
2328}
2329
40fb9820 2330static i386_operand_type
e3bb37b5 2331smallest_imm_type (offsetT num)
252b5132 2332{
40fb9820 2333 i386_operand_type t;
7ab9ffdd 2334
0dfbf9d7 2335 operand_type_set (&t, 0);
40fb9820
L
2336 t.bitfield.imm64 = 1;
2337
2338 if (cpu_arch_tune != PROCESSOR_I486 && num == 1)
e413e4e9
AM
2339 {
2340 /* This code is disabled on the 486 because all the Imm1 forms
2341 in the opcode table are slower on the i486. They're the
2342 versions with the implicitly specified single-position
2343 displacement, which has another syntax if you really want to
2344 use that form. */
40fb9820
L
2345 t.bitfield.imm1 = 1;
2346 t.bitfield.imm8 = 1;
2347 t.bitfield.imm8s = 1;
2348 t.bitfield.imm16 = 1;
2349 t.bitfield.imm32 = 1;
2350 t.bitfield.imm32s = 1;
2351 }
2352 else if (fits_in_signed_byte (num))
2353 {
2354 t.bitfield.imm8 = 1;
2355 t.bitfield.imm8s = 1;
2356 t.bitfield.imm16 = 1;
2357 t.bitfield.imm32 = 1;
2358 t.bitfield.imm32s = 1;
2359 }
2360 else if (fits_in_unsigned_byte (num))
2361 {
2362 t.bitfield.imm8 = 1;
2363 t.bitfield.imm16 = 1;
2364 t.bitfield.imm32 = 1;
2365 t.bitfield.imm32s = 1;
2366 }
2367 else if (fits_in_signed_word (num) || fits_in_unsigned_word (num))
2368 {
2369 t.bitfield.imm16 = 1;
2370 t.bitfield.imm32 = 1;
2371 t.bitfield.imm32s = 1;
2372 }
2373 else if (fits_in_signed_long (num))
2374 {
2375 t.bitfield.imm32 = 1;
2376 t.bitfield.imm32s = 1;
2377 }
2378 else if (fits_in_unsigned_long (num))
2379 t.bitfield.imm32 = 1;
2380
2381 return t;
47926f60 2382}
252b5132 2383
847f7ad4 2384static offsetT
e3bb37b5 2385offset_in_range (offsetT val, int size)
847f7ad4 2386{
508866be 2387 addressT mask;
ba2adb93 2388
847f7ad4
AM
2389 switch (size)
2390 {
508866be
L
2391 case 1: mask = ((addressT) 1 << 8) - 1; break;
2392 case 2: mask = ((addressT) 1 << 16) - 1; break;
3e73aa7c 2393#ifdef BFD64
64965897 2394 case 4: mask = ((addressT) 1 << 32) - 1; break;
3e73aa7c 2395#endif
64965897 2396 case sizeof (val): return val;
47926f60 2397 default: abort ();
847f7ad4
AM
2398 }
2399
4fe51f7d 2400 if ((val & ~mask) != 0 && (-val & ~mask) != 0)
f493c217
AM
2401 as_warn (_("0x%" PRIx64 " shortened to 0x%" PRIx64),
2402 (uint64_t) val, (uint64_t) (val & mask));
847f7ad4 2403
847f7ad4
AM
2404 return val & mask;
2405}
2406
c32fa91d
L
2407enum PREFIX_GROUP
2408{
2409 PREFIX_EXIST = 0,
2410 PREFIX_LOCK,
2411 PREFIX_REP,
04ef582a 2412 PREFIX_DS,
c32fa91d
L
2413 PREFIX_OTHER
2414};
2415
2416/* Returns
2417 a. PREFIX_EXIST if attempting to add a prefix where one from the
2418 same class already exists.
2419 b. PREFIX_LOCK if lock prefix is added.
2420 c. PREFIX_REP if rep/repne prefix is added.
04ef582a
L
2421 d. PREFIX_DS if ds prefix is added.
2422 e. PREFIX_OTHER if other prefix is added.
c32fa91d
L
2423 */
2424
2425static enum PREFIX_GROUP
e3bb37b5 2426add_prefix (unsigned int prefix)
252b5132 2427{
c32fa91d 2428 enum PREFIX_GROUP ret = PREFIX_OTHER;
b1905489 2429 unsigned int q;
252b5132 2430
29b0f896
AM
2431 if (prefix >= REX_OPCODE && prefix < REX_OPCODE + 16
2432 && flag_code == CODE_64BIT)
b1905489 2433 {
161a04f6 2434 if ((i.prefix[REX_PREFIX] & prefix & REX_W)
44846f29
JB
2435 || (i.prefix[REX_PREFIX] & prefix & REX_R)
2436 || (i.prefix[REX_PREFIX] & prefix & REX_X)
2437 || (i.prefix[REX_PREFIX] & prefix & REX_B))
c32fa91d 2438 ret = PREFIX_EXIST;
b1905489
JB
2439 q = REX_PREFIX;
2440 }
3e73aa7c 2441 else
b1905489
JB
2442 {
2443 switch (prefix)
2444 {
2445 default:
2446 abort ();
2447
b1905489 2448 case DS_PREFIX_OPCODE:
04ef582a
L
2449 ret = PREFIX_DS;
2450 /* Fall through. */
2451 case CS_PREFIX_OPCODE:
b1905489
JB
2452 case ES_PREFIX_OPCODE:
2453 case FS_PREFIX_OPCODE:
2454 case GS_PREFIX_OPCODE:
2455 case SS_PREFIX_OPCODE:
2456 q = SEG_PREFIX;
2457 break;
2458
2459 case REPNE_PREFIX_OPCODE:
2460 case REPE_PREFIX_OPCODE:
c32fa91d
L
2461 q = REP_PREFIX;
2462 ret = PREFIX_REP;
2463 break;
2464
b1905489 2465 case LOCK_PREFIX_OPCODE:
c32fa91d
L
2466 q = LOCK_PREFIX;
2467 ret = PREFIX_LOCK;
b1905489
JB
2468 break;
2469
2470 case FWAIT_OPCODE:
2471 q = WAIT_PREFIX;
2472 break;
2473
2474 case ADDR_PREFIX_OPCODE:
2475 q = ADDR_PREFIX;
2476 break;
2477
2478 case DATA_PREFIX_OPCODE:
2479 q = DATA_PREFIX;
2480 break;
2481 }
2482 if (i.prefix[q] != 0)
c32fa91d 2483 ret = PREFIX_EXIST;
b1905489 2484 }
252b5132 2485
b1905489 2486 if (ret)
252b5132 2487 {
b1905489
JB
2488 if (!i.prefix[q])
2489 ++i.prefixes;
2490 i.prefix[q] |= prefix;
252b5132 2491 }
b1905489
JB
2492 else
2493 as_bad (_("same type of prefix used twice"));
252b5132 2494
252b5132
RH
2495 return ret;
2496}
2497
2498static void
78f12dd3 2499update_code_flag (int value, int check)
eecb386c 2500{
78f12dd3
L
2501 PRINTF_LIKE ((*as_error));
2502
1e9cc1c2 2503 flag_code = (enum flag_code) value;
40fb9820
L
2504 if (flag_code == CODE_64BIT)
2505 {
2506 cpu_arch_flags.bitfield.cpu64 = 1;
2507 cpu_arch_flags.bitfield.cpuno64 = 0;
40fb9820
L
2508 }
2509 else
2510 {
2511 cpu_arch_flags.bitfield.cpu64 = 0;
2512 cpu_arch_flags.bitfield.cpuno64 = 1;
40fb9820
L
2513 }
2514 if (value == CODE_64BIT && !cpu_arch_flags.bitfield.cpulm )
3e73aa7c 2515 {
78f12dd3
L
2516 if (check)
2517 as_error = as_fatal;
2518 else
2519 as_error = as_bad;
2520 (*as_error) (_("64bit mode not supported on `%s'."),
2521 cpu_arch_name ? cpu_arch_name : default_arch);
3e73aa7c 2522 }
40fb9820 2523 if (value == CODE_32BIT && !cpu_arch_flags.bitfield.cpui386)
3e73aa7c 2524 {
78f12dd3
L
2525 if (check)
2526 as_error = as_fatal;
2527 else
2528 as_error = as_bad;
2529 (*as_error) (_("32bit mode not supported on `%s'."),
2530 cpu_arch_name ? cpu_arch_name : default_arch);
3e73aa7c 2531 }
eecb386c
AM
2532 stackop_size = '\0';
2533}
2534
78f12dd3
L
2535static void
2536set_code_flag (int value)
2537{
2538 update_code_flag (value, 0);
2539}
2540
eecb386c 2541static void
e3bb37b5 2542set_16bit_gcc_code_flag (int new_code_flag)
252b5132 2543{
1e9cc1c2 2544 flag_code = (enum flag_code) new_code_flag;
40fb9820
L
2545 if (flag_code != CODE_16BIT)
2546 abort ();
2547 cpu_arch_flags.bitfield.cpu64 = 0;
2548 cpu_arch_flags.bitfield.cpuno64 = 1;
9306ca4a 2549 stackop_size = LONG_MNEM_SUFFIX;
252b5132
RH
2550}
2551
2552static void
e3bb37b5 2553set_intel_syntax (int syntax_flag)
252b5132
RH
2554{
2555 /* Find out if register prefixing is specified. */
2556 int ask_naked_reg = 0;
2557
2558 SKIP_WHITESPACE ();
29b0f896 2559 if (!is_end_of_line[(unsigned char) *input_line_pointer])
252b5132 2560 {
d02603dc
NC
2561 char *string;
2562 int e = get_symbol_name (&string);
252b5132 2563
47926f60 2564 if (strcmp (string, "prefix") == 0)
252b5132 2565 ask_naked_reg = 1;
47926f60 2566 else if (strcmp (string, "noprefix") == 0)
252b5132
RH
2567 ask_naked_reg = -1;
2568 else
d0b47220 2569 as_bad (_("bad argument to syntax directive."));
d02603dc 2570 (void) restore_line_pointer (e);
252b5132
RH
2571 }
2572 demand_empty_rest_of_line ();
c3332e24 2573
252b5132
RH
2574 intel_syntax = syntax_flag;
2575
2576 if (ask_naked_reg == 0)
f86103b7
AM
2577 allow_naked_reg = (intel_syntax
2578 && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
252b5132
RH
2579 else
2580 allow_naked_reg = (ask_naked_reg < 0);
9306ca4a 2581
ee86248c 2582 expr_set_rank (O_full_ptr, syntax_flag ? 10 : 0);
7ab9ffdd 2583
e4a3b5a4 2584 identifier_chars['%'] = intel_syntax && allow_naked_reg ? '%' : 0;
9306ca4a 2585 identifier_chars['$'] = intel_syntax ? '$' : 0;
e4a3b5a4 2586 register_prefix = allow_naked_reg ? "" : "%";
252b5132
RH
2587}
2588
1efbbeb4
L
2589static void
2590set_intel_mnemonic (int mnemonic_flag)
2591{
e1d4d893 2592 intel_mnemonic = mnemonic_flag;
1efbbeb4
L
2593}
2594
db51cc60
L
2595static void
2596set_allow_index_reg (int flag)
2597{
2598 allow_index_reg = flag;
2599}
2600
cb19c032 2601static void
7bab8ab5 2602set_check (int what)
cb19c032 2603{
7bab8ab5
JB
2604 enum check_kind *kind;
2605 const char *str;
2606
2607 if (what)
2608 {
2609 kind = &operand_check;
2610 str = "operand";
2611 }
2612 else
2613 {
2614 kind = &sse_check;
2615 str = "sse";
2616 }
2617
cb19c032
L
2618 SKIP_WHITESPACE ();
2619
2620 if (!is_end_of_line[(unsigned char) *input_line_pointer])
2621 {
d02603dc
NC
2622 char *string;
2623 int e = get_symbol_name (&string);
cb19c032
L
2624
2625 if (strcmp (string, "none") == 0)
7bab8ab5 2626 *kind = check_none;
cb19c032 2627 else if (strcmp (string, "warning") == 0)
7bab8ab5 2628 *kind = check_warning;
cb19c032 2629 else if (strcmp (string, "error") == 0)
7bab8ab5 2630 *kind = check_error;
cb19c032 2631 else
7bab8ab5 2632 as_bad (_("bad argument to %s_check directive."), str);
d02603dc 2633 (void) restore_line_pointer (e);
cb19c032
L
2634 }
2635 else
7bab8ab5 2636 as_bad (_("missing argument for %s_check directive"), str);
cb19c032
L
2637
2638 demand_empty_rest_of_line ();
2639}
2640
8a9036a4
L
2641static void
2642check_cpu_arch_compatible (const char *name ATTRIBUTE_UNUSED,
1e9cc1c2 2643 i386_cpu_flags new_flag ATTRIBUTE_UNUSED)
8a9036a4
L
2644{
2645#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2646 static const char *arch;
2647
c085ab00 2648 /* Intel MCU is only supported on ELF. */
8a9036a4
L
2649 if (!IS_ELF)
2650 return;
2651
2652 if (!arch)
2653 {
2654 /* Use cpu_arch_name if it is set in md_parse_option. Otherwise
2655 use default_arch. */
2656 arch = cpu_arch_name;
2657 if (!arch)
2658 arch = default_arch;
2659 }
2660
81486035 2661 /* If we are targeting Intel MCU, we must enable it. */
648d04db
JB
2662 if ((get_elf_backend_data (stdoutput)->elf_machine_code == EM_IAMCU)
2663 == new_flag.bitfield.cpuiamcu)
81486035
L
2664 return;
2665
8a9036a4
L
2666 as_bad (_("`%s' is not supported on `%s'"), name, arch);
2667#endif
2668}
2669
8180707f
JB
2670static void
2671extend_cpu_sub_arch_name (const char *name)
2672{
2673 if (cpu_sub_arch_name)
2674 cpu_sub_arch_name = reconcat (cpu_sub_arch_name, cpu_sub_arch_name,
ae89daec 2675 ".", name, (const char *) NULL);
8180707f 2676 else
ae89daec 2677 cpu_sub_arch_name = concat (".", name, (const char *) NULL);
8180707f
JB
2678}
2679
e413e4e9 2680static void
e3bb37b5 2681set_cpu_arch (int dummy ATTRIBUTE_UNUSED)
e413e4e9 2682{
f68697e8
JB
2683 typedef struct arch_stack_entry
2684 {
2685 const struct arch_stack_entry *prev;
2686 const char *name;
2687 char *sub_name;
2688 i386_cpu_flags flags;
2689 i386_cpu_flags isa_flags;
2690 enum processor_type isa;
2691 enum flag_code flag_code;
2692 char stackop_size;
2693 bool no_cond_jump_promotion;
2694 } arch_stack_entry;
2695 static const arch_stack_entry *arch_stack_top;
2696
47926f60 2697 SKIP_WHITESPACE ();
e413e4e9 2698
29b0f896 2699 if (!is_end_of_line[(unsigned char) *input_line_pointer])
e413e4e9 2700 {
3ce2ebcf
JB
2701 char *s;
2702 int e = get_symbol_name (&s);
2703 const char *string = s;
2704 unsigned int j = 0;
40fb9820 2705 i386_cpu_flags flags;
e413e4e9 2706
3ce2ebcf
JB
2707 if (strcmp (string, "default") == 0)
2708 {
2709 if (strcmp (default_arch, "iamcu") == 0)
2710 string = default_arch;
2711 else
2712 {
2713 static const i386_cpu_flags cpu_unknown_flags = CPU_UNKNOWN_FLAGS;
2714
2715 cpu_arch_name = NULL;
2716 free (cpu_sub_arch_name);
2717 cpu_sub_arch_name = NULL;
2718 cpu_arch_flags = cpu_unknown_flags;
2719 if (flag_code == CODE_64BIT)
2720 {
2721 cpu_arch_flags.bitfield.cpu64 = 1;
2722 cpu_arch_flags.bitfield.cpuno64 = 0;
2723 }
2724 else
2725 {
2726 cpu_arch_flags.bitfield.cpu64 = 0;
2727 cpu_arch_flags.bitfield.cpuno64 = 1;
2728 }
2729 cpu_arch_isa = PROCESSOR_UNKNOWN;
ae89daec 2730 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
3ce2ebcf
JB
2731 if (!cpu_arch_tune_set)
2732 {
2733 cpu_arch_tune = cpu_arch_isa;
2734 cpu_arch_tune_flags = cpu_arch_isa_flags;
2735 }
2736
2737 j = ARRAY_SIZE (cpu_arch) + 1;
2738 }
2739 }
f68697e8
JB
2740 else if (strcmp (string, "push") == 0)
2741 {
2742 arch_stack_entry *top = XNEW (arch_stack_entry);
2743
2744 top->name = cpu_arch_name;
2745 if (cpu_sub_arch_name)
2746 top->sub_name = xstrdup (cpu_sub_arch_name);
2747 else
2748 top->sub_name = NULL;
2749 top->flags = cpu_arch_flags;
2750 top->isa = cpu_arch_isa;
2751 top->isa_flags = cpu_arch_isa_flags;
2752 top->flag_code = flag_code;
2753 top->stackop_size = stackop_size;
2754 top->no_cond_jump_promotion = no_cond_jump_promotion;
2755
2756 top->prev = arch_stack_top;
2757 arch_stack_top = top;
2758
2759 (void) restore_line_pointer (e);
2760 demand_empty_rest_of_line ();
2761 return;
2762 }
2763 else if (strcmp (string, "pop") == 0)
2764 {
2765 const arch_stack_entry *top = arch_stack_top;
2766
2767 if (!top)
2768 as_bad (_(".arch stack is empty"));
2769 else if (top->flag_code != flag_code
2770 || top->stackop_size != stackop_size)
2771 {
2772 static const unsigned int bits[] = {
2773 [CODE_16BIT] = 16,
2774 [CODE_32BIT] = 32,
2775 [CODE_64BIT] = 64,
2776 };
2777
2778 as_bad (_("this `.arch pop' requires `.code%u%s' to be in effect"),
2779 bits[top->flag_code],
2780 top->stackop_size == LONG_MNEM_SUFFIX ? "gcc" : "");
2781 }
2782 else
2783 {
2784 arch_stack_top = top->prev;
2785
2786 cpu_arch_name = top->name;
2787 free (cpu_sub_arch_name);
2788 cpu_sub_arch_name = top->sub_name;
2789 cpu_arch_flags = top->flags;
2790 cpu_arch_isa = top->isa;
2791 cpu_arch_isa_flags = top->isa_flags;
2792 no_cond_jump_promotion = top->no_cond_jump_promotion;
2793
2794 XDELETE (top);
2795 }
2796
2797 (void) restore_line_pointer (e);
2798 demand_empty_rest_of_line ();
2799 return;
2800 }
3ce2ebcf
JB
2801
2802 for (; j < ARRAY_SIZE (cpu_arch); j++)
e413e4e9 2803 {
ae89daec
JB
2804 if (strcmp (string + (*string == '.'), cpu_arch[j].name) == 0
2805 && (*string == '.') == (cpu_arch[j].type == PROCESSOR_NONE))
e413e4e9 2806 {
5c6af06e
JB
2807 if (*string != '.')
2808 {
ae89daec 2809 check_cpu_arch_compatible (string, cpu_arch[j].enable);
648d04db 2810
91d6fa6a 2811 cpu_arch_name = cpu_arch[j].name;
d92c7521 2812 free (cpu_sub_arch_name);
5c6af06e 2813 cpu_sub_arch_name = NULL;
ae89daec 2814 cpu_arch_flags = cpu_arch[j].enable;
40fb9820
L
2815 if (flag_code == CODE_64BIT)
2816 {
2817 cpu_arch_flags.bitfield.cpu64 = 1;
2818 cpu_arch_flags.bitfield.cpuno64 = 0;
2819 }
2820 else
2821 {
2822 cpu_arch_flags.bitfield.cpu64 = 0;
2823 cpu_arch_flags.bitfield.cpuno64 = 1;
2824 }
91d6fa6a 2825 cpu_arch_isa = cpu_arch[j].type;
ae89daec 2826 cpu_arch_isa_flags = cpu_arch[j].enable;
ccc9c027
L
2827 if (!cpu_arch_tune_set)
2828 {
2829 cpu_arch_tune = cpu_arch_isa;
2830 cpu_arch_tune_flags = cpu_arch_isa_flags;
2831 }
d59a54c2 2832 pre_386_16bit_warned = false;
5c6af06e
JB
2833 break;
2834 }
40fb9820 2835
ae89daec
JB
2836 if (cpu_flags_all_zero (&cpu_arch[j].enable))
2837 continue;
2838
293f5f65 2839 flags = cpu_flags_or (cpu_arch_flags,
ae89daec 2840 cpu_arch[j].enable);
81486035 2841
5b64d091 2842 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
5c6af06e 2843 {
ae89daec 2844 extend_cpu_sub_arch_name (string + 1);
40fb9820 2845 cpu_arch_flags = flags;
a586129e 2846 cpu_arch_isa_flags = flags;
5c6af06e 2847 }
0089dace
L
2848 else
2849 cpu_arch_isa_flags
2850 = cpu_flags_or (cpu_arch_isa_flags,
ae89daec 2851 cpu_arch[j].enable);
d02603dc 2852 (void) restore_line_pointer (e);
5c6af06e
JB
2853 demand_empty_rest_of_line ();
2854 return;
e413e4e9
AM
2855 }
2856 }
293f5f65 2857
ae89daec 2858 if (startswith (string, ".no") && j >= ARRAY_SIZE (cpu_arch))
293f5f65 2859 {
33eaf5de 2860 /* Disable an ISA extension. */
ae89daec
JB
2861 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
2862 if (cpu_arch[j].type == PROCESSOR_NONE
2863 && strcmp (string + 3, cpu_arch[j].name) == 0)
293f5f65
L
2864 {
2865 flags = cpu_flags_and_not (cpu_arch_flags,
ae89daec 2866 cpu_arch[j].disable);
293f5f65
L
2867 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
2868 {
ae89daec 2869 extend_cpu_sub_arch_name (string + 1);
293f5f65
L
2870 cpu_arch_flags = flags;
2871 cpu_arch_isa_flags = flags;
2872 }
2873 (void) restore_line_pointer (e);
2874 demand_empty_rest_of_line ();
2875 return;
2876 }
293f5f65
L
2877 }
2878
3ce2ebcf 2879 if (j == ARRAY_SIZE (cpu_arch))
e413e4e9
AM
2880 as_bad (_("no such architecture: `%s'"), string);
2881
2882 *input_line_pointer = e;
2883 }
2884 else
2885 as_bad (_("missing cpu architecture"));
2886
fddf5b5b
AM
2887 no_cond_jump_promotion = 0;
2888 if (*input_line_pointer == ','
29b0f896 2889 && !is_end_of_line[(unsigned char) input_line_pointer[1]])
fddf5b5b 2890 {
d02603dc
NC
2891 char *string;
2892 char e;
2893
2894 ++input_line_pointer;
2895 e = get_symbol_name (&string);
fddf5b5b
AM
2896
2897 if (strcmp (string, "nojumps") == 0)
2898 no_cond_jump_promotion = 1;
2899 else if (strcmp (string, "jumps") == 0)
2900 ;
2901 else
2902 as_bad (_("no such architecture modifier: `%s'"), string);
2903
d02603dc 2904 (void) restore_line_pointer (e);
fddf5b5b
AM
2905 }
2906
e413e4e9
AM
2907 demand_empty_rest_of_line ();
2908}
2909
8a9036a4
L
2910enum bfd_architecture
2911i386_arch (void)
2912{
c085ab00 2913 if (cpu_arch_isa == PROCESSOR_IAMCU)
81486035
L
2914 {
2915 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2916 || flag_code == CODE_64BIT)
2917 as_fatal (_("Intel MCU is 32bit ELF only"));
2918 return bfd_arch_iamcu;
2919 }
8a9036a4
L
2920 else
2921 return bfd_arch_i386;
2922}
2923
b9d79e03 2924unsigned long
7016a5d5 2925i386_mach (void)
b9d79e03 2926{
d34049e8 2927 if (startswith (default_arch, "x86_64"))
8a9036a4 2928 {
c085ab00 2929 if (default_arch[6] == '\0')
8a9036a4 2930 return bfd_mach_x86_64;
351f65ca
L
2931 else
2932 return bfd_mach_x64_32;
8a9036a4 2933 }
5197d474
L
2934 else if (!strcmp (default_arch, "i386")
2935 || !strcmp (default_arch, "iamcu"))
81486035
L
2936 {
2937 if (cpu_arch_isa == PROCESSOR_IAMCU)
2938 {
2939 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
2940 as_fatal (_("Intel MCU is 32bit ELF only"));
2941 return bfd_mach_i386_iamcu;
2942 }
2943 else
2944 return bfd_mach_i386_i386;
2945 }
b9d79e03 2946 else
2b5d6a91 2947 as_fatal (_("unknown architecture"));
b9d79e03 2948}
b9d79e03 2949\f
252b5132 2950void
7016a5d5 2951md_begin (void)
252b5132 2952{
86fa6981
L
2953 /* Support pseudo prefixes like {disp32}. */
2954 lex_type ['{'] = LEX_BEGIN_NAME;
2955
47926f60 2956 /* Initialize op_hash hash table. */
629310ab 2957 op_hash = str_htab_create ();
252b5132
RH
2958
2959 {
d3ce72d0 2960 const insn_template *optab;
29b0f896 2961 templates *core_optab;
252b5132 2962
47926f60
KH
2963 /* Setup for loop. */
2964 optab = i386_optab;
654d6f31 2965 core_optab = notes_alloc (sizeof (*core_optab));
252b5132
RH
2966 core_optab->start = optab;
2967
2968 while (1)
2969 {
2970 ++optab;
2971 if (optab->name == NULL
2972 || strcmp (optab->name, (optab - 1)->name) != 0)
2973 {
2974 /* different name --> ship out current template list;
47926f60 2975 add to hash table; & begin anew. */
252b5132 2976 core_optab->end = optab;
fe0e921f
AM
2977 if (str_hash_insert (op_hash, (optab - 1)->name, core_optab, 0))
2978 as_fatal (_("duplicate %s"), (optab - 1)->name);
2979
252b5132
RH
2980 if (optab->name == NULL)
2981 break;
654d6f31 2982 core_optab = notes_alloc (sizeof (*core_optab));
252b5132
RH
2983 core_optab->start = optab;
2984 }
2985 }
2986 }
2987
47926f60 2988 /* Initialize reg_hash hash table. */
629310ab 2989 reg_hash = str_htab_create ();
252b5132 2990 {
29b0f896 2991 const reg_entry *regtab;
c3fe08fa 2992 unsigned int regtab_size = i386_regtab_size;
252b5132 2993
c3fe08fa 2994 for (regtab = i386_regtab; regtab_size--; regtab++)
6225c532 2995 {
6288d05f
JB
2996 switch (regtab->reg_type.bitfield.class)
2997 {
2998 case Reg:
34684862
JB
2999 if (regtab->reg_type.bitfield.dword)
3000 {
3001 if (regtab->reg_type.bitfield.instance == Accum)
3002 reg_eax = regtab;
3003 }
3004 else if (regtab->reg_type.bitfield.tbyte)
6288d05f
JB
3005 {
3006 /* There's no point inserting st(<N>) in the hash table, as
3007 parentheses aren't included in register_chars[] anyway. */
3008 if (regtab->reg_type.bitfield.instance != Accum)
3009 continue;
3010 reg_st0 = regtab;
3011 }
3012 break;
3013
5e042380
JB
3014 case SReg:
3015 switch (regtab->reg_num)
3016 {
3017 case 0: reg_es = regtab; break;
3018 case 2: reg_ss = regtab; break;
3019 case 3: reg_ds = regtab; break;
3020 }
3021 break;
3022
6288d05f
JB
3023 case RegMask:
3024 if (!regtab->reg_num)
3025 reg_k0 = regtab;
3026 break;
3027 }
3028
6225c532
JB
3029 if (str_hash_insert (reg_hash, regtab->reg_name, regtab, 0) != NULL)
3030 as_fatal (_("duplicate %s"), regtab->reg_name);
6225c532 3031 }
252b5132
RH
3032 }
3033
47926f60 3034 /* Fill in lexical tables: mnemonic_chars, operand_chars. */
252b5132 3035 {
29b0f896
AM
3036 int c;
3037 char *p;
252b5132
RH
3038
3039 for (c = 0; c < 256; c++)
3040 {
014fbcda 3041 if (ISDIGIT (c) || ISLOWER (c))
252b5132
RH
3042 {
3043 mnemonic_chars[c] = c;
3044 register_chars[c] = c;
3045 operand_chars[c] = c;
3046 }
3882b010 3047 else if (ISUPPER (c))
252b5132 3048 {
3882b010 3049 mnemonic_chars[c] = TOLOWER (c);
252b5132
RH
3050 register_chars[c] = mnemonic_chars[c];
3051 operand_chars[c] = c;
3052 }
43234a1e 3053 else if (c == '{' || c == '}')
86fa6981
L
3054 {
3055 mnemonic_chars[c] = c;
3056 operand_chars[c] = c;
3057 }
b3983e5f
JB
3058#ifdef SVR4_COMMENT_CHARS
3059 else if (c == '\\' && strchr (i386_comment_chars, '/'))
3060 operand_chars[c] = c;
3061#endif
252b5132 3062
3882b010 3063 if (ISALPHA (c) || ISDIGIT (c))
252b5132
RH
3064 identifier_chars[c] = c;
3065 else if (c >= 128)
3066 {
3067 identifier_chars[c] = c;
3068 operand_chars[c] = c;
3069 }
3070 }
3071
3072#ifdef LEX_AT
3073 identifier_chars['@'] = '@';
32137342
NC
3074#endif
3075#ifdef LEX_QM
3076 identifier_chars['?'] = '?';
3077 operand_chars['?'] = '?';
252b5132 3078#endif
c0f3af97 3079 mnemonic_chars['_'] = '_';
791fe849 3080 mnemonic_chars['-'] = '-';
0003779b 3081 mnemonic_chars['.'] = '.';
252b5132
RH
3082 identifier_chars['_'] = '_';
3083 identifier_chars['.'] = '.';
3084
3085 for (p = operand_special_chars; *p != '\0'; p++)
3086 operand_chars[(unsigned char) *p] = *p;
3087 }
3088
a4447b93
RH
3089 if (flag_code == CODE_64BIT)
3090 {
ca19b261
KT
3091#if defined (OBJ_COFF) && defined (TE_PE)
3092 x86_dwarf2_return_column = (OUTPUT_FLAVOR == bfd_target_coff_flavour
3093 ? 32 : 16);
3094#else
a4447b93 3095 x86_dwarf2_return_column = 16;
ca19b261 3096#endif
61ff971f 3097 x86_cie_data_alignment = -8;
a4447b93
RH
3098 }
3099 else
3100 {
3101 x86_dwarf2_return_column = 8;
3102 x86_cie_data_alignment = -4;
3103 }
e379e5f3
L
3104
3105 /* NB: FUSED_JCC_PADDING frag must have sufficient room so that it
3106 can be turned into BRANCH_PREFIX frag. */
3107 if (align_branch_prefix_size > MAX_FUSED_JCC_PADDING_SIZE)
3108 abort ();
252b5132
RH
3109}
3110
3111void
e3bb37b5 3112i386_print_statistics (FILE *file)
252b5132 3113{
629310ab
ML
3114 htab_print_statistics (file, "i386 opcode", op_hash);
3115 htab_print_statistics (file, "i386 register", reg_hash);
252b5132 3116}
654d6f31
AM
3117
3118void
3119i386_md_end (void)
3120{
3121 htab_delete (op_hash);
3122 htab_delete (reg_hash);
3123}
252b5132 3124\f
252b5132
RH
3125#ifdef DEBUG386
3126
ce8a8b2f 3127/* Debugging routines for md_assemble. */
d3ce72d0 3128static void pte (insn_template *);
40fb9820 3129static void pt (i386_operand_type);
e3bb37b5
L
3130static void pe (expressionS *);
3131static void ps (symbolS *);
252b5132
RH
3132
3133static void
2c703856 3134pi (const char *line, i386_insn *x)
252b5132 3135{
09137c09 3136 unsigned int j;
252b5132
RH
3137
3138 fprintf (stdout, "%s: template ", line);
3139 pte (&x->tm);
09f131f2
JH
3140 fprintf (stdout, " address: base %s index %s scale %x\n",
3141 x->base_reg ? x->base_reg->reg_name : "none",
3142 x->index_reg ? x->index_reg->reg_name : "none",
3143 x->log2_scale_factor);
3144 fprintf (stdout, " modrm: mode %x reg %x reg/mem %x\n",
252b5132 3145 x->rm.mode, x->rm.reg, x->rm.regmem);
09f131f2
JH
3146 fprintf (stdout, " sib: base %x index %x scale %x\n",
3147 x->sib.base, x->sib.index, x->sib.scale);
3148 fprintf (stdout, " rex: 64bit %x extX %x extY %x extZ %x\n",
161a04f6
L
3149 (x->rex & REX_W) != 0,
3150 (x->rex & REX_R) != 0,
3151 (x->rex & REX_X) != 0,
3152 (x->rex & REX_B) != 0);
09137c09 3153 for (j = 0; j < x->operands; j++)
252b5132 3154 {
09137c09
SP
3155 fprintf (stdout, " #%d: ", j + 1);
3156 pt (x->types[j]);
252b5132 3157 fprintf (stdout, "\n");
bab6aec1 3158 if (x->types[j].bitfield.class == Reg
3528c362
JB
3159 || x->types[j].bitfield.class == RegMMX
3160 || x->types[j].bitfield.class == RegSIMD
dd6b8a0b 3161 || x->types[j].bitfield.class == RegMask
00cee14f 3162 || x->types[j].bitfield.class == SReg
4a5c67ed
JB
3163 || x->types[j].bitfield.class == RegCR
3164 || x->types[j].bitfield.class == RegDR
dd6b8a0b
JB
3165 || x->types[j].bitfield.class == RegTR
3166 || x->types[j].bitfield.class == RegBND)
09137c09
SP
3167 fprintf (stdout, "%s\n", x->op[j].regs->reg_name);
3168 if (operand_type_check (x->types[j], imm))
3169 pe (x->op[j].imms);
3170 if (operand_type_check (x->types[j], disp))
3171 pe (x->op[j].disps);
252b5132
RH
3172 }
3173}
3174
3175static void
d3ce72d0 3176pte (insn_template *t)
252b5132 3177{
b933fa4b 3178 static const unsigned char opc_pfx[] = { 0, 0x66, 0xf3, 0xf2 };
441f6aca 3179 static const char *const opc_spc[] = {
0cc78721 3180 NULL, "0f", "0f38", "0f3a", NULL, "evexmap5", "evexmap6", NULL,
441f6aca
JB
3181 "XOP08", "XOP09", "XOP0A",
3182 };
09137c09 3183 unsigned int j;
441f6aca 3184
252b5132 3185 fprintf (stdout, " %d operands ", t->operands);
441f6aca
JB
3186 if (opc_pfx[t->opcode_modifier.opcodeprefix])
3187 fprintf (stdout, "pfx %x ", opc_pfx[t->opcode_modifier.opcodeprefix]);
3188 if (opc_spc[t->opcode_modifier.opcodespace])
3189 fprintf (stdout, "space %s ", opc_spc[t->opcode_modifier.opcodespace]);
47926f60 3190 fprintf (stdout, "opcode %x ", t->base_opcode);
252b5132
RH
3191 if (t->extension_opcode != None)
3192 fprintf (stdout, "ext %x ", t->extension_opcode);
40fb9820 3193 if (t->opcode_modifier.d)
252b5132 3194 fprintf (stdout, "D");
40fb9820 3195 if (t->opcode_modifier.w)
252b5132
RH
3196 fprintf (stdout, "W");
3197 fprintf (stdout, "\n");
09137c09 3198 for (j = 0; j < t->operands; j++)
252b5132 3199 {
09137c09
SP
3200 fprintf (stdout, " #%d type ", j + 1);
3201 pt (t->operand_types[j]);
252b5132
RH
3202 fprintf (stdout, "\n");
3203 }
3204}
3205
3206static void
e3bb37b5 3207pe (expressionS *e)
252b5132 3208{
24eab124 3209 fprintf (stdout, " operation %d\n", e->X_op);
b8281767
AM
3210 fprintf (stdout, " add_number %" PRId64 " (%" PRIx64 ")\n",
3211 (int64_t) e->X_add_number, (uint64_t) (valueT) e->X_add_number);
252b5132
RH
3212 if (e->X_add_symbol)
3213 {
3214 fprintf (stdout, " add_symbol ");
3215 ps (e->X_add_symbol);
3216 fprintf (stdout, "\n");
3217 }
3218 if (e->X_op_symbol)
3219 {
3220 fprintf (stdout, " op_symbol ");
3221 ps (e->X_op_symbol);
3222 fprintf (stdout, "\n");
3223 }
3224}
3225
3226static void
e3bb37b5 3227ps (symbolS *s)
252b5132
RH
3228{
3229 fprintf (stdout, "%s type %s%s",
3230 S_GET_NAME (s),
3231 S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
3232 segment_name (S_GET_SEGMENT (s)));
3233}
3234
7b81dfbb 3235static struct type_name
252b5132 3236 {
40fb9820
L
3237 i386_operand_type mask;
3238 const char *name;
252b5132 3239 }
7b81dfbb 3240const type_names[] =
252b5132 3241{
40fb9820
L
3242 { OPERAND_TYPE_REG8, "r8" },
3243 { OPERAND_TYPE_REG16, "r16" },
3244 { OPERAND_TYPE_REG32, "r32" },
3245 { OPERAND_TYPE_REG64, "r64" },
2c703856
JB
3246 { OPERAND_TYPE_ACC8, "acc8" },
3247 { OPERAND_TYPE_ACC16, "acc16" },
3248 { OPERAND_TYPE_ACC32, "acc32" },
3249 { OPERAND_TYPE_ACC64, "acc64" },
40fb9820
L
3250 { OPERAND_TYPE_IMM8, "i8" },
3251 { OPERAND_TYPE_IMM8, "i8s" },
3252 { OPERAND_TYPE_IMM16, "i16" },
3253 { OPERAND_TYPE_IMM32, "i32" },
3254 { OPERAND_TYPE_IMM32S, "i32s" },
3255 { OPERAND_TYPE_IMM64, "i64" },
3256 { OPERAND_TYPE_IMM1, "i1" },
3257 { OPERAND_TYPE_BASEINDEX, "BaseIndex" },
3258 { OPERAND_TYPE_DISP8, "d8" },
3259 { OPERAND_TYPE_DISP16, "d16" },
3260 { OPERAND_TYPE_DISP32, "d32" },
40fb9820
L
3261 { OPERAND_TYPE_DISP64, "d64" },
3262 { OPERAND_TYPE_INOUTPORTREG, "InOutPortReg" },
3263 { OPERAND_TYPE_SHIFTCOUNT, "ShiftCount" },
3264 { OPERAND_TYPE_CONTROL, "control reg" },
3265 { OPERAND_TYPE_TEST, "test reg" },
3266 { OPERAND_TYPE_DEBUG, "debug reg" },
3267 { OPERAND_TYPE_FLOATREG, "FReg" },
3268 { OPERAND_TYPE_FLOATACC, "FAcc" },
21df382b 3269 { OPERAND_TYPE_SREG, "SReg" },
40fb9820
L
3270 { OPERAND_TYPE_REGMMX, "rMMX" },
3271 { OPERAND_TYPE_REGXMM, "rXMM" },
0349dc08 3272 { OPERAND_TYPE_REGYMM, "rYMM" },
43234a1e 3273 { OPERAND_TYPE_REGZMM, "rZMM" },
260cd341 3274 { OPERAND_TYPE_REGTMM, "rTMM" },
43234a1e 3275 { OPERAND_TYPE_REGMASK, "Mask reg" },
252b5132
RH
3276};
3277
3278static void
40fb9820 3279pt (i386_operand_type t)
252b5132 3280{
40fb9820 3281 unsigned int j;
c6fb90c8 3282 i386_operand_type a;
252b5132 3283
40fb9820 3284 for (j = 0; j < ARRAY_SIZE (type_names); j++)
c6fb90c8
L
3285 {
3286 a = operand_type_and (t, type_names[j].mask);
2c703856 3287 if (operand_type_equal (&a, &type_names[j].mask))
c6fb90c8
L
3288 fprintf (stdout, "%s, ", type_names[j].name);
3289 }
252b5132
RH
3290 fflush (stdout);
3291}
3292
3293#endif /* DEBUG386 */
3294\f
252b5132 3295static bfd_reloc_code_real_type
3956db08 3296reloc (unsigned int size,
64e74474
AM
3297 int pcrel,
3298 int sign,
3299 bfd_reloc_code_real_type other)
252b5132 3300{
47926f60 3301 if (other != NO_RELOC)
3956db08 3302 {
91d6fa6a 3303 reloc_howto_type *rel;
3956db08
JB
3304
3305 if (size == 8)
3306 switch (other)
3307 {
64e74474
AM
3308 case BFD_RELOC_X86_64_GOT32:
3309 return BFD_RELOC_X86_64_GOT64;
3310 break;
553d1284
L
3311 case BFD_RELOC_X86_64_GOTPLT64:
3312 return BFD_RELOC_X86_64_GOTPLT64;
3313 break;
64e74474
AM
3314 case BFD_RELOC_X86_64_PLTOFF64:
3315 return BFD_RELOC_X86_64_PLTOFF64;
3316 break;
3317 case BFD_RELOC_X86_64_GOTPC32:
3318 other = BFD_RELOC_X86_64_GOTPC64;
3319 break;
3320 case BFD_RELOC_X86_64_GOTPCREL:
3321 other = BFD_RELOC_X86_64_GOTPCREL64;
3322 break;
3323 case BFD_RELOC_X86_64_TPOFF32:
3324 other = BFD_RELOC_X86_64_TPOFF64;
3325 break;
3326 case BFD_RELOC_X86_64_DTPOFF32:
3327 other = BFD_RELOC_X86_64_DTPOFF64;
3328 break;
3329 default:
3330 break;
3956db08 3331 }
e05278af 3332
8ce3d284 3333#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
3334 if (other == BFD_RELOC_SIZE32)
3335 {
3336 if (size == 8)
1ab668bf 3337 other = BFD_RELOC_SIZE64;
8fd4256d 3338 if (pcrel)
1ab668bf
AM
3339 {
3340 as_bad (_("there are no pc-relative size relocations"));
3341 return NO_RELOC;
3342 }
8fd4256d 3343 }
8ce3d284 3344#endif
8fd4256d 3345
e05278af 3346 /* Sign-checking 4-byte relocations in 16-/32-bit code is pointless. */
f2d8a97c 3347 if (size == 4 && (flag_code != CODE_64BIT || disallow_64bit_reloc))
e05278af
JB
3348 sign = -1;
3349
91d6fa6a
NC
3350 rel = bfd_reloc_type_lookup (stdoutput, other);
3351 if (!rel)
3956db08 3352 as_bad (_("unknown relocation (%u)"), other);
91d6fa6a 3353 else if (size != bfd_get_reloc_size (rel))
3956db08 3354 as_bad (_("%u-byte relocation cannot be applied to %u-byte field"),
91d6fa6a 3355 bfd_get_reloc_size (rel),
3956db08 3356 size);
91d6fa6a 3357 else if (pcrel && !rel->pc_relative)
3956db08 3358 as_bad (_("non-pc-relative relocation for pc-relative field"));
91d6fa6a 3359 else if ((rel->complain_on_overflow == complain_overflow_signed
3956db08 3360 && !sign)
91d6fa6a 3361 || (rel->complain_on_overflow == complain_overflow_unsigned
64e74474 3362 && sign > 0))
3956db08
JB
3363 as_bad (_("relocated field and relocation type differ in signedness"));
3364 else
3365 return other;
3366 return NO_RELOC;
3367 }
252b5132
RH
3368
3369 if (pcrel)
3370 {
3e73aa7c 3371 if (!sign)
3956db08 3372 as_bad (_("there are no unsigned pc-relative relocations"));
252b5132
RH
3373 switch (size)
3374 {
3375 case 1: return BFD_RELOC_8_PCREL;
3376 case 2: return BFD_RELOC_16_PCREL;
d258b828 3377 case 4: return BFD_RELOC_32_PCREL;
d6ab8113 3378 case 8: return BFD_RELOC_64_PCREL;
252b5132 3379 }
3956db08 3380 as_bad (_("cannot do %u byte pc-relative relocation"), size);
252b5132
RH
3381 }
3382 else
3383 {
3956db08 3384 if (sign > 0)
e5cb08ac 3385 switch (size)
3e73aa7c
JH
3386 {
3387 case 4: return BFD_RELOC_X86_64_32S;
3388 }
3389 else
3390 switch (size)
3391 {
3392 case 1: return BFD_RELOC_8;
3393 case 2: return BFD_RELOC_16;
3394 case 4: return BFD_RELOC_32;
3395 case 8: return BFD_RELOC_64;
3396 }
3956db08
JB
3397 as_bad (_("cannot do %s %u byte relocation"),
3398 sign > 0 ? "signed" : "unsigned", size);
252b5132
RH
3399 }
3400
0cc9e1d3 3401 return NO_RELOC;
252b5132
RH
3402}
3403
47926f60
KH
3404/* Here we decide which fixups can be adjusted to make them relative to
3405 the beginning of the section instead of the symbol. Basically we need
3406 to make sure that the dynamic relocations are done correctly, so in
3407 some cases we force the original symbol to be used. */
3408
252b5132 3409int
e3bb37b5 3410tc_i386_fix_adjustable (fixS *fixP ATTRIBUTE_UNUSED)
252b5132 3411{
6d249963 3412#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 3413 if (!IS_ELF)
31312f95
AM
3414 return 1;
3415
a161fe53
AM
3416 /* Don't adjust pc-relative references to merge sections in 64-bit
3417 mode. */
3418 if (use_rela_relocations
3419 && (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0
3420 && fixP->fx_pcrel)
252b5132 3421 return 0;
31312f95 3422
8d01d9a9
AJ
3423 /* The x86_64 GOTPCREL are represented as 32bit PCrel relocations
3424 and changed later by validate_fix. */
3425 if (GOT_symbol && fixP->fx_subsy == GOT_symbol
3426 && fixP->fx_r_type == BFD_RELOC_32_PCREL)
3427 return 0;
3428
8fd4256d
L
3429 /* Adjust_reloc_syms doesn't know about the GOT. Need to keep symbol
3430 for size relocations. */
3431 if (fixP->fx_r_type == BFD_RELOC_SIZE32
3432 || fixP->fx_r_type == BFD_RELOC_SIZE64
3433 || fixP->fx_r_type == BFD_RELOC_386_GOTOFF
252b5132 3434 || fixP->fx_r_type == BFD_RELOC_386_GOT32
02a86693 3435 || fixP->fx_r_type == BFD_RELOC_386_GOT32X
13ae64f3
JJ
3436 || fixP->fx_r_type == BFD_RELOC_386_TLS_GD
3437 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDM
3438 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDO_32
3439 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE_32
37e55690
JJ
3440 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE
3441 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTIE
13ae64f3
JJ
3442 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE_32
3443 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE
67a4f2b7
AO
3444 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTDESC
3445 || fixP->fx_r_type == BFD_RELOC_386_TLS_DESC_CALL
3e73aa7c 3446 || fixP->fx_r_type == BFD_RELOC_X86_64_GOT32
80b3ee89 3447 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCREL
56ceb5b5
L
3448 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCRELX
3449 || fixP->fx_r_type == BFD_RELOC_X86_64_REX_GOTPCRELX
bffbf940
JJ
3450 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSGD
3451 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSLD
3452 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF32
d6ab8113 3453 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF64
bffbf940
JJ
3454 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF
3455 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF32
d6ab8113
JB
3456 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF64
3457 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTOFF64
67a4f2b7
AO
3458 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
3459 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSDESC_CALL
252b5132
RH
3460 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3461 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3462 return 0;
31312f95 3463#endif
252b5132
RH
3464 return 1;
3465}
252b5132 3466
a9aabc23
JB
3467static INLINE bool
3468want_disp32 (const insn_template *t)
3469{
3470 return flag_code != CODE_64BIT
3471 || i.prefix[ADDR_PREFIX]
3472 || (t->base_opcode == 0x8d
3473 && t->opcode_modifier.opcodespace == SPACE_BASE
fe134c65
JB
3474 && (!i.types[1].bitfield.qword
3475 || t->opcode_modifier.size == SIZE32));
a9aabc23
JB
3476}
3477
b4cac588 3478static int
e3bb37b5 3479intel_float_operand (const char *mnemonic)
252b5132 3480{
9306ca4a
JB
3481 /* Note that the value returned is meaningful only for opcodes with (memory)
3482 operands, hence the code here is free to improperly handle opcodes that
3483 have no operands (for better performance and smaller code). */
3484
3485 if (mnemonic[0] != 'f')
3486 return 0; /* non-math */
3487
3488 switch (mnemonic[1])
3489 {
3490 /* fclex, fdecstp, fdisi, femms, feni, fincstp, finit, fsetpm, and
3491 the fs segment override prefix not currently handled because no
3492 call path can make opcodes without operands get here */
3493 case 'i':
3494 return 2 /* integer op */;
3495 case 'l':
3496 if (mnemonic[2] == 'd' && (mnemonic[3] == 'c' || mnemonic[3] == 'e'))
3497 return 3; /* fldcw/fldenv */
3498 break;
3499 case 'n':
3500 if (mnemonic[2] != 'o' /* fnop */)
3501 return 3; /* non-waiting control op */
3502 break;
3503 case 'r':
3504 if (mnemonic[2] == 's')
3505 return 3; /* frstor/frstpm */
3506 break;
3507 case 's':
3508 if (mnemonic[2] == 'a')
3509 return 3; /* fsave */
3510 if (mnemonic[2] == 't')
3511 {
3512 switch (mnemonic[3])
3513 {
3514 case 'c': /* fstcw */
3515 case 'd': /* fstdw */
3516 case 'e': /* fstenv */
3517 case 's': /* fsts[gw] */
3518 return 3;
3519 }
3520 }
3521 break;
3522 case 'x':
3523 if (mnemonic[2] == 'r' || mnemonic[2] == 's')
3524 return 0; /* fxsave/fxrstor are not really math ops */
3525 break;
3526 }
252b5132 3527
9306ca4a 3528 return 1;
252b5132
RH
3529}
3530
9a182d04
JB
3531static INLINE void
3532install_template (const insn_template *t)
3533{
3534 unsigned int l;
3535
3536 i.tm = *t;
3537
3538 /* Note that for pseudo prefixes this produces a length of 1. But for them
3539 the length isn't interesting at all. */
3540 for (l = 1; l < 4; ++l)
3541 if (!(t->base_opcode >> (8 * l)))
3542 break;
3543
3544 i.opcode_length = l;
3545}
3546
c0f3af97
L
3547/* Build the VEX prefix. */
3548
3549static void
d3ce72d0 3550build_vex_prefix (const insn_template *t)
c0f3af97
L
3551{
3552 unsigned int register_specifier;
c0f3af97 3553 unsigned int vector_length;
03751133 3554 unsigned int w;
c0f3af97
L
3555
3556 /* Check register specifier. */
3557 if (i.vex.register_specifier)
43234a1e
L
3558 {
3559 register_specifier =
3560 ~register_number (i.vex.register_specifier) & 0xf;
3561 gas_assert ((i.vex.register_specifier->reg_flags & RegVRex) == 0);
3562 }
c0f3af97
L
3563 else
3564 register_specifier = 0xf;
3565
79f0fa25
L
3566 /* Use 2-byte VEX prefix by swapping destination and source operand
3567 if there are more than 1 register operand. */
3568 if (i.reg_operands > 1
3569 && i.vec_encoding != vex_encoding_vex3
86fa6981 3570 && i.dir_encoding == dir_encoding_default
fa99fab2 3571 && i.operands == i.reg_operands
dbbc8b7e 3572 && operand_type_equal (&i.types[0], &i.types[i.operands - 1])
441f6aca 3573 && i.tm.opcode_modifier.opcodespace == SPACE_0F
dbbc8b7e 3574 && (i.tm.opcode_modifier.load || i.tm.opcode_modifier.d)
fa99fab2
L
3575 && i.rex == REX_B)
3576 {
3577 unsigned int xchg = i.operands - 1;
3578 union i386_op temp_op;
3579 i386_operand_type temp_type;
3580
3581 temp_type = i.types[xchg];
3582 i.types[xchg] = i.types[0];
3583 i.types[0] = temp_type;
3584 temp_op = i.op[xchg];
3585 i.op[xchg] = i.op[0];
3586 i.op[0] = temp_op;
3587
9c2799c2 3588 gas_assert (i.rm.mode == 3);
fa99fab2
L
3589
3590 i.rex = REX_R;
3591 xchg = i.rm.regmem;
3592 i.rm.regmem = i.rm.reg;
3593 i.rm.reg = xchg;
3594
dbbc8b7e
JB
3595 if (i.tm.opcode_modifier.d)
3596 i.tm.base_opcode ^= (i.tm.base_opcode & 0xee) != 0x6e
2c735193 3597 ? Opcode_ExtD : Opcode_SIMD_IntD;
dbbc8b7e 3598 else /* Use the next insn. */
9a182d04 3599 install_template (&t[1]);
fa99fab2
L
3600 }
3601
79dec6b7
JB
3602 /* Use 2-byte VEX prefix by swapping commutative source operands if there
3603 are no memory operands and at least 3 register ones. */
3604 if (i.reg_operands >= 3
3605 && i.vec_encoding != vex_encoding_vex3
3606 && i.reg_operands == i.operands - i.imm_operands
3607 && i.tm.opcode_modifier.vex
3608 && i.tm.opcode_modifier.commutative
3609 && (i.tm.opcode_modifier.sse2avx || optimize > 1)
3610 && i.rex == REX_B
3611 && i.vex.register_specifier
3612 && !(i.vex.register_specifier->reg_flags & RegRex))
3613 {
3614 unsigned int xchg = i.operands - i.reg_operands;
3615 union i386_op temp_op;
3616 i386_operand_type temp_type;
3617
441f6aca 3618 gas_assert (i.tm.opcode_modifier.opcodespace == SPACE_0F);
79dec6b7
JB
3619 gas_assert (!i.tm.opcode_modifier.sae);
3620 gas_assert (operand_type_equal (&i.types[i.operands - 2],
3621 &i.types[i.operands - 3]));
3622 gas_assert (i.rm.mode == 3);
3623
3624 temp_type = i.types[xchg];
3625 i.types[xchg] = i.types[xchg + 1];
3626 i.types[xchg + 1] = temp_type;
3627 temp_op = i.op[xchg];
3628 i.op[xchg] = i.op[xchg + 1];
3629 i.op[xchg + 1] = temp_op;
3630
3631 i.rex = 0;
3632 xchg = i.rm.regmem | 8;
3633 i.rm.regmem = ~register_specifier & 0xf;
3634 gas_assert (!(i.rm.regmem & 8));
3635 i.vex.register_specifier += xchg - i.rm.regmem;
3636 register_specifier = ~xchg & 0xf;
3637 }
3638
539f890d
L
3639 if (i.tm.opcode_modifier.vex == VEXScalar)
3640 vector_length = avxscalar;
10c17abd
JB
3641 else if (i.tm.opcode_modifier.vex == VEX256)
3642 vector_length = 1;
539f890d 3643 else
10c17abd 3644 {
56522fc5 3645 unsigned int op;
10c17abd 3646
c7213af9
L
3647 /* Determine vector length from the last multi-length vector
3648 operand. */
10c17abd 3649 vector_length = 0;
56522fc5 3650 for (op = t->operands; op--;)
10c17abd
JB
3651 if (t->operand_types[op].bitfield.xmmword
3652 && t->operand_types[op].bitfield.ymmword
3653 && i.types[op].bitfield.ymmword)
3654 {
3655 vector_length = 1;
3656 break;
3657 }
3658 }
c0f3af97 3659
03751133
L
3660 /* Check the REX.W bit and VEXW. */
3661 if (i.tm.opcode_modifier.vexw == VEXWIG)
3662 w = (vexwig == vexw1 || (i.rex & REX_W)) ? 1 : 0;
3663 else if (i.tm.opcode_modifier.vexw)
3664 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3665 else
931d03b7 3666 w = (flag_code == CODE_64BIT ? i.rex & REX_W : vexwig == vexw1) ? 1 : 0;
03751133 3667
c0f3af97 3668 /* Use 2-byte VEX prefix if possible. */
03751133
L
3669 if (w == 0
3670 && i.vec_encoding != vex_encoding_vex3
441f6aca 3671 && i.tm.opcode_modifier.opcodespace == SPACE_0F
c0f3af97
L
3672 && (i.rex & (REX_W | REX_X | REX_B)) == 0)
3673 {
3674 /* 2-byte VEX prefix. */
3675 unsigned int r;
3676
3677 i.vex.length = 2;
3678 i.vex.bytes[0] = 0xc5;
3679
3680 /* Check the REX.R bit. */
3681 r = (i.rex & REX_R) ? 0 : 1;
3682 i.vex.bytes[1] = (r << 7
3683 | register_specifier << 3
3684 | vector_length << 2
35648716 3685 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3686 }
3687 else
3688 {
3689 /* 3-byte VEX prefix. */
f88c9eb0 3690 i.vex.length = 3;
f88c9eb0 3691
441f6aca 3692 switch (i.tm.opcode_modifier.opcodespace)
5dd85c99 3693 {
441f6aca
JB
3694 case SPACE_0F:
3695 case SPACE_0F38:
3696 case SPACE_0F3A:
80de6e00 3697 i.vex.bytes[0] = 0xc4;
7f399153 3698 break;
441f6aca
JB
3699 case SPACE_XOP08:
3700 case SPACE_XOP09:
3701 case SPACE_XOP0A:
f88c9eb0 3702 i.vex.bytes[0] = 0x8f;
7f399153
L
3703 break;
3704 default:
3705 abort ();
f88c9eb0 3706 }
c0f3af97 3707
c0f3af97
L
3708 /* The high 3 bits of the second VEX byte are 1's compliment
3709 of RXB bits from REX. */
441f6aca 3710 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | i.tm.opcode_modifier.opcodespace;
c0f3af97 3711
c0f3af97
L
3712 i.vex.bytes[2] = (w << 7
3713 | register_specifier << 3
3714 | vector_length << 2
35648716 3715 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3716 }
3717}
3718
5b7c81bd 3719static INLINE bool
e771e7c9
JB
3720is_evex_encoding (const insn_template *t)
3721{
7091c612 3722 return t->opcode_modifier.evex || t->opcode_modifier.disp8memshift
e771e7c9 3723 || t->opcode_modifier.broadcast || t->opcode_modifier.masking
a80195f1 3724 || t->opcode_modifier.sae;
e771e7c9
JB
3725}
3726
5b7c81bd 3727static INLINE bool
7a8655d2
JB
3728is_any_vex_encoding (const insn_template *t)
3729{
7b47a312 3730 return t->opcode_modifier.vex || is_evex_encoding (t);
7a8655d2
JB
3731}
3732
a5748e0d
JB
3733static unsigned int
3734get_broadcast_bytes (const insn_template *t, bool diag)
3735{
3736 unsigned int op, bytes;
3737 const i386_operand_type *types;
3738
3739 if (i.broadcast.type)
3740 return i.broadcast.bytes = ((1 << (t->opcode_modifier.broadcast - 1))
3741 * i.broadcast.type);
3742
3743 gas_assert (intel_syntax);
3744
3745 for (op = 0; op < t->operands; ++op)
3746 if (t->operand_types[op].bitfield.baseindex)
3747 break;
3748
3749 gas_assert (op < t->operands);
3750
3751 if (t->opcode_modifier.evex
3752 && t->opcode_modifier.evex != EVEXDYN)
3753 switch (i.broadcast.bytes)
3754 {
3755 case 1:
3756 if (t->operand_types[op].bitfield.word)
3757 return 2;
3758 /* Fall through. */
3759 case 2:
3760 if (t->operand_types[op].bitfield.dword)
3761 return 4;
3762 /* Fall through. */
3763 case 4:
3764 if (t->operand_types[op].bitfield.qword)
3765 return 8;
3766 /* Fall through. */
3767 case 8:
3768 if (t->operand_types[op].bitfield.xmmword)
3769 return 16;
3770 if (t->operand_types[op].bitfield.ymmword)
3771 return 32;
3772 if (t->operand_types[op].bitfield.zmmword)
3773 return 64;
3774 /* Fall through. */
3775 default:
3776 abort ();
3777 }
3778
3779 gas_assert (op + 1 < t->operands);
3780
3781 if (t->operand_types[op + 1].bitfield.xmmword
3782 + t->operand_types[op + 1].bitfield.ymmword
3783 + t->operand_types[op + 1].bitfield.zmmword > 1)
3784 {
3785 types = &i.types[op + 1];
3786 diag = false;
3787 }
3788 else /* Ambiguous - guess with a preference to non-AVX512VL forms. */
3789 types = &t->operand_types[op];
3790
3791 if (types->bitfield.zmmword)
3792 bytes = 64;
3793 else if (types->bitfield.ymmword)
3794 bytes = 32;
3795 else
3796 bytes = 16;
3797
3798 if (diag)
3799 as_warn (_("ambiguous broadcast for `%s', using %u-bit form"),
3800 t->name, bytes * 8);
3801
3802 return bytes;
3803}
3804
43234a1e
L
3805/* Build the EVEX prefix. */
3806
3807static void
3808build_evex_prefix (void)
3809{
35648716 3810 unsigned int register_specifier, w;
43234a1e
L
3811 rex_byte vrex_used = 0;
3812
3813 /* Check register specifier. */
3814 if (i.vex.register_specifier)
3815 {
3816 gas_assert ((i.vrex & REX_X) == 0);
3817
3818 register_specifier = i.vex.register_specifier->reg_num;
3819 if ((i.vex.register_specifier->reg_flags & RegRex))
3820 register_specifier += 8;
3821 /* The upper 16 registers are encoded in the fourth byte of the
3822 EVEX prefix. */
3823 if (!(i.vex.register_specifier->reg_flags & RegVRex))
3824 i.vex.bytes[3] = 0x8;
3825 register_specifier = ~register_specifier & 0xf;
3826 }
3827 else
3828 {
3829 register_specifier = 0xf;
3830
3831 /* Encode upper 16 vector index register in the fourth byte of
3832 the EVEX prefix. */
3833 if (!(i.vrex & REX_X))
3834 i.vex.bytes[3] = 0x8;
3835 else
3836 vrex_used |= REX_X;
3837 }
3838
43234a1e
L
3839 /* 4 byte EVEX prefix. */
3840 i.vex.length = 4;
3841 i.vex.bytes[0] = 0x62;
3842
43234a1e
L
3843 /* The high 3 bits of the second EVEX byte are 1's compliment of RXB
3844 bits from REX. */
441f6aca 3845 gas_assert (i.tm.opcode_modifier.opcodespace >= SPACE_0F);
0cc78721 3846 gas_assert (i.tm.opcode_modifier.opcodespace <= SPACE_EVEXMAP6);
441f6aca 3847 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | i.tm.opcode_modifier.opcodespace;
43234a1e
L
3848
3849 /* The fifth bit of the second EVEX byte is 1's compliment of the
3850 REX_R bit in VREX. */
3851 if (!(i.vrex & REX_R))
3852 i.vex.bytes[1] |= 0x10;
3853 else
3854 vrex_used |= REX_R;
3855
3856 if ((i.reg_operands + i.imm_operands) == i.operands)
3857 {
3858 /* When all operands are registers, the REX_X bit in REX is not
3859 used. We reuse it to encode the upper 16 registers, which is
3860 indicated by the REX_B bit in VREX. The REX_X bit is encoded
3861 as 1's compliment. */
3862 if ((i.vrex & REX_B))
3863 {
3864 vrex_used |= REX_B;
3865 i.vex.bytes[1] &= ~0x40;
3866 }
3867 }
3868
3869 /* EVEX instructions shouldn't need the REX prefix. */
3870 i.vrex &= ~vrex_used;
3871 gas_assert (i.vrex == 0);
3872
6865c043
L
3873 /* Check the REX.W bit and VEXW. */
3874 if (i.tm.opcode_modifier.vexw == VEXWIG)
3875 w = (evexwig == evexw1 || (i.rex & REX_W)) ? 1 : 0;
3876 else if (i.tm.opcode_modifier.vexw)
3877 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3878 else
931d03b7 3879 w = (flag_code == CODE_64BIT ? i.rex & REX_W : evexwig == evexw1) ? 1 : 0;
43234a1e 3880
43234a1e 3881 /* The third byte of the EVEX prefix. */
35648716
JB
3882 i.vex.bytes[2] = ((w << 7)
3883 | (register_specifier << 3)
3884 | 4 /* Encode the U bit. */
3885 | i.tm.opcode_modifier.opcodeprefix);
43234a1e
L
3886
3887 /* The fourth byte of the EVEX prefix. */
3888 /* The zeroing-masking bit. */
6225c532 3889 if (i.mask.reg && i.mask.zeroing)
43234a1e
L
3890 i.vex.bytes[3] |= 0x80;
3891
3892 /* Don't always set the broadcast bit if there is no RC. */
ca5312a2 3893 if (i.rounding.type == rc_none)
43234a1e
L
3894 {
3895 /* Encode the vector length. */
3896 unsigned int vec_length;
3897
e771e7c9
JB
3898 if (!i.tm.opcode_modifier.evex
3899 || i.tm.opcode_modifier.evex == EVEXDYN)
3900 {
56522fc5 3901 unsigned int op;
e771e7c9 3902
c7213af9
L
3903 /* Determine vector length from the last multi-length vector
3904 operand. */
56522fc5 3905 for (op = i.operands; op--;)
e771e7c9
JB
3906 if (i.tm.operand_types[op].bitfield.xmmword
3907 + i.tm.operand_types[op].bitfield.ymmword
3908 + i.tm.operand_types[op].bitfield.zmmword > 1)
3909 {
3910 if (i.types[op].bitfield.zmmword)
c7213af9
L
3911 {
3912 i.tm.opcode_modifier.evex = EVEX512;
3913 break;
3914 }
e771e7c9 3915 else if (i.types[op].bitfield.ymmword)
c7213af9
L
3916 {
3917 i.tm.opcode_modifier.evex = EVEX256;
3918 break;
3919 }
e771e7c9 3920 else if (i.types[op].bitfield.xmmword)
c7213af9
L
3921 {
3922 i.tm.opcode_modifier.evex = EVEX128;
3923 break;
3924 }
a5748e0d 3925 else if (i.broadcast.bytes && op == i.broadcast.operand)
625cbd7a 3926 {
a5748e0d 3927 switch (get_broadcast_bytes (&i.tm, true))
625cbd7a
JB
3928 {
3929 case 64:
3930 i.tm.opcode_modifier.evex = EVEX512;
3931 break;
3932 case 32:
3933 i.tm.opcode_modifier.evex = EVEX256;
3934 break;
3935 case 16:
3936 i.tm.opcode_modifier.evex = EVEX128;
3937 break;
3938 default:
c7213af9 3939 abort ();
625cbd7a 3940 }
c7213af9 3941 break;
625cbd7a 3942 }
e771e7c9 3943 }
c7213af9 3944
56522fc5 3945 if (op >= MAX_OPERANDS)
c7213af9 3946 abort ();
e771e7c9
JB
3947 }
3948
43234a1e
L
3949 switch (i.tm.opcode_modifier.evex)
3950 {
3951 case EVEXLIG: /* LL' is ignored */
3952 vec_length = evexlig << 5;
3953 break;
3954 case EVEX128:
3955 vec_length = 0 << 5;
3956 break;
3957 case EVEX256:
3958 vec_length = 1 << 5;
3959 break;
3960 case EVEX512:
3961 vec_length = 2 << 5;
3962 break;
3963 default:
3964 abort ();
3965 break;
3966 }
3967 i.vex.bytes[3] |= vec_length;
3968 /* Encode the broadcast bit. */
a5748e0d 3969 if (i.broadcast.bytes)
43234a1e
L
3970 i.vex.bytes[3] |= 0x10;
3971 }
ca5312a2
JB
3972 else if (i.rounding.type != saeonly)
3973 i.vex.bytes[3] |= 0x10 | (i.rounding.type << 5);
43234a1e 3974 else
ca5312a2 3975 i.vex.bytes[3] |= 0x10 | (evexrcig << 5);
43234a1e 3976
6225c532
JB
3977 if (i.mask.reg)
3978 i.vex.bytes[3] |= i.mask.reg->reg_num;
43234a1e
L
3979}
3980
65da13b5
L
3981static void
3982process_immext (void)
3983{
3984 expressionS *exp;
3985
c0f3af97 3986 /* These AMD 3DNow! and SSE2 instructions have an opcode suffix
65da13b5
L
3987 which is coded in the same place as an 8-bit immediate field
3988 would be. Here we fake an 8-bit immediate operand from the
3989 opcode suffix stored in tm.extension_opcode.
3990
c1e679ec 3991 AVX instructions also use this encoding, for some of
c0f3af97 3992 3 argument instructions. */
65da13b5 3993
43234a1e 3994 gas_assert (i.imm_operands <= 1
7ab9ffdd 3995 && (i.operands <= 2
7a8655d2 3996 || (is_any_vex_encoding (&i.tm)
7ab9ffdd 3997 && i.operands <= 4)));
65da13b5
L
3998
3999 exp = &im_expressions[i.imm_operands++];
4000 i.op[i.operands].imms = exp;
4001 i.types[i.operands] = imm8;
4002 i.operands++;
4003 exp->X_op = O_constant;
4004 exp->X_add_number = i.tm.extension_opcode;
4005 i.tm.extension_opcode = None;
4006}
4007
42164a71
L
4008
4009static int
4010check_hle (void)
4011{
742732c7 4012 switch (i.tm.opcode_modifier.prefixok)
42164a71
L
4013 {
4014 default:
4015 abort ();
742732c7
JB
4016 case PrefixLock:
4017 case PrefixNone:
4018 case PrefixNoTrack:
4019 case PrefixRep:
165de32a
L
4020 as_bad (_("invalid instruction `%s' after `%s'"),
4021 i.tm.name, i.hle_prefix);
42164a71 4022 return 0;
742732c7 4023 case PrefixHLELock:
42164a71
L
4024 if (i.prefix[LOCK_PREFIX])
4025 return 1;
165de32a 4026 as_bad (_("missing `lock' with `%s'"), i.hle_prefix);
42164a71 4027 return 0;
742732c7 4028 case PrefixHLEAny:
42164a71 4029 return 1;
742732c7 4030 case PrefixHLERelease:
42164a71
L
4031 if (i.prefix[HLE_PREFIX] != XRELEASE_PREFIX_OPCODE)
4032 {
4033 as_bad (_("instruction `%s' after `xacquire' not allowed"),
4034 i.tm.name);
4035 return 0;
4036 }
8dc0818e 4037 if (i.mem_operands == 0 || !(i.flags[i.operands - 1] & Operand_Mem))
42164a71
L
4038 {
4039 as_bad (_("memory destination needed for instruction `%s'"
4040 " after `xrelease'"), i.tm.name);
4041 return 0;
4042 }
4043 return 1;
4044 }
4045}
4046
c8480b58
L
4047/* Encode aligned vector move as unaligned vector move. */
4048
4049static void
4050encode_with_unaligned_vector_move (void)
4051{
4052 switch (i.tm.base_opcode)
4053 {
b3a9fe6f
L
4054 case 0x28: /* Load instructions. */
4055 case 0x29: /* Store instructions. */
c8480b58
L
4056 /* movaps/movapd/vmovaps/vmovapd. */
4057 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4058 && i.tm.opcode_modifier.opcodeprefix <= PREFIX_0X66)
b3a9fe6f 4059 i.tm.base_opcode = 0x10 | (i.tm.base_opcode & 1);
c8480b58 4060 break;
b3a9fe6f
L
4061 case 0x6f: /* Load instructions. */
4062 case 0x7f: /* Store instructions. */
c8480b58
L
4063 /* movdqa/vmovdqa/vmovdqa64/vmovdqa32. */
4064 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4065 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0X66)
4066 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
4067 break;
4068 default:
4069 break;
4070 }
4071}
4072
b6f8c7c4
L
4073/* Try the shortest encoding by shortening operand size. */
4074
4075static void
4076optimize_encoding (void)
4077{
a0a1771e 4078 unsigned int j;
b6f8c7c4 4079
fe134c65
JB
4080 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
4081 && i.tm.base_opcode == 0x8d)
4082 {
4083 /* Optimize: -O:
4084 lea symbol, %rN -> mov $symbol, %rN
4085 lea (%rM), %rN -> mov %rM, %rN
4086 lea (,%rM,1), %rN -> mov %rM, %rN
4087
4088 and in 32-bit mode for 16-bit addressing
4089
4090 lea (%rM), %rN -> movzx %rM, %rN
4091
4092 and in 64-bit mode zap 32-bit addressing in favor of using a
4093 32-bit (or less) destination.
4094 */
4095 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
4096 {
4097 if (!i.op[1].regs->reg_type.bitfield.word)
4098 i.tm.opcode_modifier.size = SIZE32;
4099 i.prefix[ADDR_PREFIX] = 0;
4100 }
4101
4102 if (!i.index_reg && !i.base_reg)
4103 {
4104 /* Handle:
4105 lea symbol, %rN -> mov $symbol, %rN
4106 */
4107 if (flag_code == CODE_64BIT)
4108 {
4109 /* Don't transform a relocation to a 16-bit one. */
4110 if (i.op[0].disps
4111 && i.op[0].disps->X_op != O_constant
4112 && i.op[1].regs->reg_type.bitfield.word)
4113 return;
4114
4115 if (!i.op[1].regs->reg_type.bitfield.qword
4116 || i.tm.opcode_modifier.size == SIZE32)
4117 {
4118 i.tm.base_opcode = 0xb8;
4119 i.tm.opcode_modifier.modrm = 0;
4120 if (!i.op[1].regs->reg_type.bitfield.word)
4121 i.types[0].bitfield.imm32 = 1;
4122 else
4123 {
4124 i.tm.opcode_modifier.size = SIZE16;
4125 i.types[0].bitfield.imm16 = 1;
4126 }
4127 }
4128 else
4129 {
4130 /* Subject to further optimization below. */
4131 i.tm.base_opcode = 0xc7;
4132 i.tm.extension_opcode = 0;
4133 i.types[0].bitfield.imm32s = 1;
4134 i.types[0].bitfield.baseindex = 0;
4135 }
4136 }
4137 /* Outside of 64-bit mode address and operand sizes have to match if
4138 a relocation is involved, as otherwise we wouldn't (currently) or
4139 even couldn't express the relocation correctly. */
4140 else if (i.op[0].disps
4141 && i.op[0].disps->X_op != O_constant
4142 && ((!i.prefix[ADDR_PREFIX])
4143 != (flag_code == CODE_32BIT
4144 ? i.op[1].regs->reg_type.bitfield.dword
4145 : i.op[1].regs->reg_type.bitfield.word)))
4146 return;
7772f168
JB
4147 /* In 16-bit mode converting LEA with 16-bit addressing and a 32-bit
4148 destination is going to grow encoding size. */
4149 else if (flag_code == CODE_16BIT
4150 && (optimize <= 1 || optimize_for_space)
4151 && !i.prefix[ADDR_PREFIX]
4152 && i.op[1].regs->reg_type.bitfield.dword)
4153 return;
fe134c65
JB
4154 else
4155 {
4156 i.tm.base_opcode = 0xb8;
4157 i.tm.opcode_modifier.modrm = 0;
4158 if (i.op[1].regs->reg_type.bitfield.dword)
4159 i.types[0].bitfield.imm32 = 1;
4160 else
4161 i.types[0].bitfield.imm16 = 1;
4162
4163 if (i.op[0].disps
4164 && i.op[0].disps->X_op == O_constant
4165 && i.op[1].regs->reg_type.bitfield.dword
60cfa10c
L
4166 /* NB: Add () to !i.prefix[ADDR_PREFIX] to silence
4167 GCC 5. */
4168 && (!i.prefix[ADDR_PREFIX]) != (flag_code == CODE_32BIT))
fe134c65
JB
4169 i.op[0].disps->X_add_number &= 0xffff;
4170 }
4171
4172 i.tm.operand_types[0] = i.types[0];
4173 i.imm_operands = 1;
4174 if (!i.op[0].imms)
4175 {
4176 i.op[0].imms = &im_expressions[0];
4177 i.op[0].imms->X_op = O_absent;
4178 }
4179 }
4180 else if (i.op[0].disps
4181 && (i.op[0].disps->X_op != O_constant
4182 || i.op[0].disps->X_add_number))
4183 return;
4184 else
4185 {
4186 /* Handle:
4187 lea (%rM), %rN -> mov %rM, %rN
4188 lea (,%rM,1), %rN -> mov %rM, %rN
4189 lea (%rM), %rN -> movzx %rM, %rN
4190 */
4191 const reg_entry *addr_reg;
4192
4193 if (!i.index_reg && i.base_reg->reg_num != RegIP)
4194 addr_reg = i.base_reg;
4195 else if (!i.base_reg
4196 && i.index_reg->reg_num != RegIZ
4197 && !i.log2_scale_factor)
4198 addr_reg = i.index_reg;
4199 else
4200 return;
4201
4202 if (addr_reg->reg_type.bitfield.word
4203 && i.op[1].regs->reg_type.bitfield.dword)
4204 {
4205 if (flag_code != CODE_32BIT)
4206 return;
4207 i.tm.opcode_modifier.opcodespace = SPACE_0F;
4208 i.tm.base_opcode = 0xb7;
4209 }
4210 else
4211 i.tm.base_opcode = 0x8b;
4212
4213 if (addr_reg->reg_type.bitfield.dword
4214 && i.op[1].regs->reg_type.bitfield.qword)
4215 i.tm.opcode_modifier.size = SIZE32;
4216
4217 i.op[0].regs = addr_reg;
4218 i.reg_operands = 2;
4219 }
4220
4221 i.mem_operands = 0;
4222 i.disp_operands = 0;
4223 i.prefix[ADDR_PREFIX] = 0;
4224 i.prefix[SEG_PREFIX] = 0;
4225 i.seg[0] = NULL;
4226 }
4227
b6f8c7c4 4228 if (optimize_for_space
389d00a5 4229 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
b6f8c7c4
L
4230 && i.reg_operands == 1
4231 && i.imm_operands == 1
4232 && !i.types[1].bitfield.byte
4233 && i.op[0].imms->X_op == O_constant
4234 && fits_in_imm7 (i.op[0].imms->X_add_number)
72aea328 4235 && (i.tm.base_opcode == 0xa8
b6f8c7c4
L
4236 || (i.tm.base_opcode == 0xf6
4237 && i.tm.extension_opcode == 0x0)))
4238 {
4239 /* Optimize: -Os:
4240 test $imm7, %r64/%r32/%r16 -> test $imm7, %r8
4241 */
4242 unsigned int base_regnum = i.op[1].regs->reg_num;
4243 if (flag_code == CODE_64BIT || base_regnum < 4)
4244 {
4245 i.types[1].bitfield.byte = 1;
4246 /* Ignore the suffix. */
4247 i.suffix = 0;
7697afb6
JB
4248 /* Convert to byte registers. */
4249 if (i.types[1].bitfield.word)
4250 j = 16;
4251 else if (i.types[1].bitfield.dword)
4252 j = 32;
4253 else
4254 j = 48;
4255 if (!(i.op[1].regs->reg_flags & RegRex) && base_regnum < 4)
4256 j += 8;
4257 i.op[1].regs -= j;
b6f8c7c4
L
4258 }
4259 }
4260 else if (flag_code == CODE_64BIT
389d00a5 4261 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
d3d50934
L
4262 && ((i.types[1].bitfield.qword
4263 && i.reg_operands == 1
b6f8c7c4
L
4264 && i.imm_operands == 1
4265 && i.op[0].imms->X_op == O_constant
507916b8 4266 && ((i.tm.base_opcode == 0xb8
b6f8c7c4
L
4267 && i.tm.extension_opcode == None
4268 && fits_in_unsigned_long (i.op[0].imms->X_add_number))
4269 || (fits_in_imm31 (i.op[0].imms->X_add_number)
72aea328
JB
4270 && ((i.tm.base_opcode == 0x24
4271 || i.tm.base_opcode == 0xa8)
b6f8c7c4
L
4272 || (i.tm.base_opcode == 0x80
4273 && i.tm.extension_opcode == 0x4)
4274 || ((i.tm.base_opcode == 0xf6
507916b8 4275 || (i.tm.base_opcode | 1) == 0xc7)
b8364fa7
JB
4276 && i.tm.extension_opcode == 0x0)))
4277 || (fits_in_imm7 (i.op[0].imms->X_add_number)
4278 && i.tm.base_opcode == 0x83
4279 && i.tm.extension_opcode == 0x4)))
d3d50934
L
4280 || (i.types[0].bitfield.qword
4281 && ((i.reg_operands == 2
4282 && i.op[0].regs == i.op[1].regs
72aea328
JB
4283 && (i.tm.base_opcode == 0x30
4284 || i.tm.base_opcode == 0x28))
d3d50934
L
4285 || (i.reg_operands == 1
4286 && i.operands == 1
72aea328 4287 && i.tm.base_opcode == 0x30)))))
b6f8c7c4
L
4288 {
4289 /* Optimize: -O:
4290 andq $imm31, %r64 -> andl $imm31, %r32
b8364fa7 4291 andq $imm7, %r64 -> andl $imm7, %r32
b6f8c7c4
L
4292 testq $imm31, %r64 -> testl $imm31, %r32
4293 xorq %r64, %r64 -> xorl %r32, %r32
4294 subq %r64, %r64 -> subl %r32, %r32
4295 movq $imm31, %r64 -> movl $imm31, %r32
4296 movq $imm32, %r64 -> movl $imm32, %r32
4297 */
4298 i.tm.opcode_modifier.norex64 = 1;
507916b8 4299 if (i.tm.base_opcode == 0xb8 || (i.tm.base_opcode | 1) == 0xc7)
b6f8c7c4
L
4300 {
4301 /* Handle
4302 movq $imm31, %r64 -> movl $imm31, %r32
4303 movq $imm32, %r64 -> movl $imm32, %r32
4304 */
4305 i.tm.operand_types[0].bitfield.imm32 = 1;
4306 i.tm.operand_types[0].bitfield.imm32s = 0;
4307 i.tm.operand_types[0].bitfield.imm64 = 0;
4308 i.types[0].bitfield.imm32 = 1;
4309 i.types[0].bitfield.imm32s = 0;
4310 i.types[0].bitfield.imm64 = 0;
4311 i.types[1].bitfield.dword = 1;
4312 i.types[1].bitfield.qword = 0;
507916b8 4313 if ((i.tm.base_opcode | 1) == 0xc7)
b6f8c7c4
L
4314 {
4315 /* Handle
4316 movq $imm31, %r64 -> movl $imm31, %r32
4317 */
507916b8 4318 i.tm.base_opcode = 0xb8;
b6f8c7c4 4319 i.tm.extension_opcode = None;
507916b8 4320 i.tm.opcode_modifier.w = 0;
b6f8c7c4
L
4321 i.tm.opcode_modifier.modrm = 0;
4322 }
4323 }
4324 }
5641ec01
JB
4325 else if (optimize > 1
4326 && !optimize_for_space
389d00a5 4327 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
5641ec01
JB
4328 && i.reg_operands == 2
4329 && i.op[0].regs == i.op[1].regs
4330 && ((i.tm.base_opcode & ~(Opcode_D | 1)) == 0x8
4331 || (i.tm.base_opcode & ~(Opcode_D | 1)) == 0x20)
4332 && (flag_code != CODE_64BIT || !i.types[0].bitfield.dword))
4333 {
4334 /* Optimize: -O2:
4335 andb %rN, %rN -> testb %rN, %rN
4336 andw %rN, %rN -> testw %rN, %rN
4337 andq %rN, %rN -> testq %rN, %rN
4338 orb %rN, %rN -> testb %rN, %rN
4339 orw %rN, %rN -> testw %rN, %rN
4340 orq %rN, %rN -> testq %rN, %rN
4341
4342 and outside of 64-bit mode
4343
4344 andl %rN, %rN -> testl %rN, %rN
4345 orl %rN, %rN -> testl %rN, %rN
4346 */
4347 i.tm.base_opcode = 0x84 | (i.tm.base_opcode & 1);
4348 }
99112332 4349 else if (i.reg_operands == 3
b6f8c7c4
L
4350 && i.op[0].regs == i.op[1].regs
4351 && !i.types[2].bitfield.xmmword
4352 && (i.tm.opcode_modifier.vex
6225c532 4353 || ((!i.mask.reg || i.mask.zeroing)
e771e7c9 4354 && is_evex_encoding (&i.tm)
80c34c38 4355 && (i.vec_encoding != vex_encoding_evex
dd22218c 4356 || cpu_arch_isa_flags.bitfield.cpuavx512vl
80c34c38 4357 || i.tm.cpu_flags.bitfield.cpuavx512vl
7091c612 4358 || (i.tm.operand_types[2].bitfield.zmmword
dd22218c 4359 && i.types[2].bitfield.ymmword))))
5844ccaa
JB
4360 && i.tm.opcode_modifier.opcodespace == SPACE_0F
4361 && ((i.tm.base_opcode | 2) == 0x57
4362 || i.tm.base_opcode == 0xdf
4363 || i.tm.base_opcode == 0xef
4364 || (i.tm.base_opcode | 3) == 0xfb
4365 || i.tm.base_opcode == 0x42
4366 || i.tm.base_opcode == 0x47))
b6f8c7c4 4367 {
99112332 4368 /* Optimize: -O1:
8305403a
L
4369 VOP, one of vandnps, vandnpd, vxorps, vxorpd, vpsubb, vpsubd,
4370 vpsubq and vpsubw:
b6f8c7c4
L
4371 EVEX VOP %zmmM, %zmmM, %zmmN
4372 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4373 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4374 EVEX VOP %ymmM, %ymmM, %ymmN
4375 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4376 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4377 VEX VOP %ymmM, %ymmM, %ymmN
4378 -> VEX VOP %xmmM, %xmmM, %xmmN
4379 VOP, one of vpandn and vpxor:
4380 VEX VOP %ymmM, %ymmM, %ymmN
4381 -> VEX VOP %xmmM, %xmmM, %xmmN
4382 VOP, one of vpandnd and vpandnq:
4383 EVEX VOP %zmmM, %zmmM, %zmmN
4384 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4385 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4386 EVEX VOP %ymmM, %ymmM, %ymmN
4387 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4388 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4389 VOP, one of vpxord and vpxorq:
4390 EVEX VOP %zmmM, %zmmM, %zmmN
4391 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4392 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4393 EVEX VOP %ymmM, %ymmM, %ymmN
4394 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4395 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
1424ad86
JB
4396 VOP, one of kxord and kxorq:
4397 VEX VOP %kM, %kM, %kN
4398 -> VEX kxorw %kM, %kM, %kN
4399 VOP, one of kandnd and kandnq:
4400 VEX VOP %kM, %kM, %kN
4401 -> VEX kandnw %kM, %kM, %kN
b6f8c7c4 4402 */
e771e7c9 4403 if (is_evex_encoding (&i.tm))
b6f8c7c4 4404 {
7b1d7ca1 4405 if (i.vec_encoding != vex_encoding_evex)
b6f8c7c4
L
4406 {
4407 i.tm.opcode_modifier.vex = VEX128;
4408 i.tm.opcode_modifier.vexw = VEXW0;
4409 i.tm.opcode_modifier.evex = 0;
4410 }
7b1d7ca1 4411 else if (optimize > 1)
dd22218c
L
4412 i.tm.opcode_modifier.evex = EVEX128;
4413 else
4414 return;
b6f8c7c4 4415 }
f74a6307 4416 else if (i.tm.operand_types[0].bitfield.class == RegMask)
1424ad86 4417 {
35648716 4418 i.tm.opcode_modifier.opcodeprefix = PREFIX_NONE;
1424ad86
JB
4419 i.tm.opcode_modifier.vexw = VEXW0;
4420 }
b6f8c7c4
L
4421 else
4422 i.tm.opcode_modifier.vex = VEX128;
4423
4424 if (i.tm.opcode_modifier.vex)
4425 for (j = 0; j < 3; j++)
4426 {
4427 i.types[j].bitfield.xmmword = 1;
4428 i.types[j].bitfield.ymmword = 0;
4429 }
4430 }
392a5972 4431 else if (i.vec_encoding != vex_encoding_evex
97ed31ae 4432 && !i.types[0].bitfield.zmmword
392a5972 4433 && !i.types[1].bitfield.zmmword
6225c532 4434 && !i.mask.reg
a5748e0d 4435 && !i.broadcast.bytes
97ed31ae 4436 && is_evex_encoding (&i.tm)
35648716
JB
4437 && ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4438 || (i.tm.base_opcode & ~4) == 0xdb
4439 || (i.tm.base_opcode & ~4) == 0xeb)
97ed31ae
L
4440 && i.tm.extension_opcode == None)
4441 {
4442 /* Optimize: -O1:
4443 VOP, one of vmovdqa32, vmovdqa64, vmovdqu8, vmovdqu16,
4444 vmovdqu32 and vmovdqu64:
4445 EVEX VOP %xmmM, %xmmN
4446 -> VEX vmovdqa|vmovdqu %xmmM, %xmmN (M and N < 16)
4447 EVEX VOP %ymmM, %ymmN
4448 -> VEX vmovdqa|vmovdqu %ymmM, %ymmN (M and N < 16)
4449 EVEX VOP %xmmM, mem
4450 -> VEX vmovdqa|vmovdqu %xmmM, mem (M < 16)
4451 EVEX VOP %ymmM, mem
4452 -> VEX vmovdqa|vmovdqu %ymmM, mem (M < 16)
4453 EVEX VOP mem, %xmmN
4454 -> VEX mvmovdqa|vmovdquem, %xmmN (N < 16)
4455 EVEX VOP mem, %ymmN
4456 -> VEX vmovdqa|vmovdqu mem, %ymmN (N < 16)
a0a1771e
JB
4457 VOP, one of vpand, vpandn, vpor, vpxor:
4458 EVEX VOP{d,q} %xmmL, %xmmM, %xmmN
4459 -> VEX VOP %xmmL, %xmmM, %xmmN (L, M, and N < 16)
4460 EVEX VOP{d,q} %ymmL, %ymmM, %ymmN
4461 -> VEX VOP %ymmL, %ymmM, %ymmN (L, M, and N < 16)
4462 EVEX VOP{d,q} mem, %xmmM, %xmmN
4463 -> VEX VOP mem, %xmmM, %xmmN (M and N < 16)
4464 EVEX VOP{d,q} mem, %ymmM, %ymmN
4465 -> VEX VOP mem, %ymmM, %ymmN (M and N < 16)
97ed31ae 4466 */
a0a1771e 4467 for (j = 0; j < i.operands; j++)
392a5972
L
4468 if (operand_type_check (i.types[j], disp)
4469 && i.op[j].disps->X_op == O_constant)
4470 {
4471 /* Since the VEX prefix has 2 or 3 bytes, the EVEX prefix
4472 has 4 bytes, EVEX Disp8 has 1 byte and VEX Disp32 has 4
4473 bytes, we choose EVEX Disp8 over VEX Disp32. */
4474 int evex_disp8, vex_disp8;
4475 unsigned int memshift = i.memshift;
4476 offsetT n = i.op[j].disps->X_add_number;
4477
4478 evex_disp8 = fits_in_disp8 (n);
4479 i.memshift = 0;
4480 vex_disp8 = fits_in_disp8 (n);
4481 if (evex_disp8 != vex_disp8)
4482 {
4483 i.memshift = memshift;
4484 return;
4485 }
4486
4487 i.types[j].bitfield.disp8 = vex_disp8;
4488 break;
4489 }
35648716
JB
4490 if ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4491 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2)
4492 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
97ed31ae
L
4493 i.tm.opcode_modifier.vex
4494 = i.types[0].bitfield.ymmword ? VEX256 : VEX128;
4495 i.tm.opcode_modifier.vexw = VEXW0;
79dec6b7 4496 /* VPAND, VPOR, and VPXOR are commutative. */
35648716 4497 if (i.reg_operands == 3 && i.tm.base_opcode != 0xdf)
79dec6b7 4498 i.tm.opcode_modifier.commutative = 1;
97ed31ae
L
4499 i.tm.opcode_modifier.evex = 0;
4500 i.tm.opcode_modifier.masking = 0;
a0a1771e 4501 i.tm.opcode_modifier.broadcast = 0;
97ed31ae
L
4502 i.tm.opcode_modifier.disp8memshift = 0;
4503 i.memshift = 0;
a0a1771e
JB
4504 if (j < i.operands)
4505 i.types[j].bitfield.disp8
4506 = fits_in_disp8 (i.op[j].disps->X_add_number);
97ed31ae 4507 }
b6f8c7c4
L
4508}
4509
ae531041
L
4510/* Return non-zero for load instruction. */
4511
4512static int
4513load_insn_p (void)
4514{
4515 unsigned int dest;
4516 int any_vex_p = is_any_vex_encoding (&i.tm);
4517 unsigned int base_opcode = i.tm.base_opcode | 1;
4518
4519 if (!any_vex_p)
4520 {
ef07be45
CL
4521 /* Anysize insns: lea, invlpg, clflush, prefetch*, bndmk, bndcl, bndcu,
4522 bndcn, bndstx, bndldx, clflushopt, clwb, cldemote. */
a09f656b 4523 if (i.tm.opcode_modifier.anysize)
ae531041
L
4524 return 0;
4525
389d00a5
JB
4526 /* pop. */
4527 if (strcmp (i.tm.name, "pop") == 0)
4528 return 1;
4529 }
4530
4531 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE)
4532 {
4533 /* popf, popa. */
4534 if (i.tm.base_opcode == 0x9d
a09f656b 4535 || i.tm.base_opcode == 0x61)
ae531041
L
4536 return 1;
4537
4538 /* movs, cmps, lods, scas. */
4539 if ((i.tm.base_opcode | 0xb) == 0xaf)
4540 return 1;
4541
a09f656b 4542 /* outs, xlatb. */
4543 if (base_opcode == 0x6f
4544 || i.tm.base_opcode == 0xd7)
ae531041 4545 return 1;
a09f656b 4546 /* NB: For AMD-specific insns with implicit memory operands,
4547 they're intentionally not covered. */
ae531041
L
4548 }
4549
4550 /* No memory operand. */
4551 if (!i.mem_operands)
4552 return 0;
4553
4554 if (any_vex_p)
4555 {
4556 /* vldmxcsr. */
4557 if (i.tm.base_opcode == 0xae
4558 && i.tm.opcode_modifier.vex
441f6aca 4559 && i.tm.opcode_modifier.opcodespace == SPACE_0F
35648716 4560 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
ae531041
L
4561 && i.tm.extension_opcode == 2)
4562 return 1;
4563 }
389d00a5 4564 else if (i.tm.opcode_modifier.opcodespace == SPACE_BASE)
ae531041
L
4565 {
4566 /* test, not, neg, mul, imul, div, idiv. */
4567 if ((i.tm.base_opcode == 0xf6 || i.tm.base_opcode == 0xf7)
4568 && i.tm.extension_opcode != 1)
4569 return 1;
4570
4571 /* inc, dec. */
4572 if (base_opcode == 0xff && i.tm.extension_opcode <= 1)
4573 return 1;
4574
4575 /* add, or, adc, sbb, and, sub, xor, cmp. */
4576 if (i.tm.base_opcode >= 0x80 && i.tm.base_opcode <= 0x83)
4577 return 1;
4578
ae531041
L
4579 /* rol, ror, rcl, rcr, shl/sal, shr, sar. */
4580 if ((base_opcode == 0xc1
4581 || (i.tm.base_opcode >= 0xd0 && i.tm.base_opcode <= 0xd3))
4582 && i.tm.extension_opcode != 6)
4583 return 1;
4584
ae531041 4585 /* Check for x87 instructions. */
389d00a5 4586 if (base_opcode >= 0xd8 && base_opcode <= 0xdf)
ae531041
L
4587 {
4588 /* Skip fst, fstp, fstenv, fstcw. */
4589 if (i.tm.base_opcode == 0xd9
4590 && (i.tm.extension_opcode == 2
4591 || i.tm.extension_opcode == 3
4592 || i.tm.extension_opcode == 6
4593 || i.tm.extension_opcode == 7))
4594 return 0;
4595
4596 /* Skip fisttp, fist, fistp, fstp. */
4597 if (i.tm.base_opcode == 0xdb
4598 && (i.tm.extension_opcode == 1
4599 || i.tm.extension_opcode == 2
4600 || i.tm.extension_opcode == 3
4601 || i.tm.extension_opcode == 7))
4602 return 0;
4603
4604 /* Skip fisttp, fst, fstp, fsave, fstsw. */
4605 if (i.tm.base_opcode == 0xdd
4606 && (i.tm.extension_opcode == 1
4607 || i.tm.extension_opcode == 2
4608 || i.tm.extension_opcode == 3
4609 || i.tm.extension_opcode == 6
4610 || i.tm.extension_opcode == 7))
4611 return 0;
4612
4613 /* Skip fisttp, fist, fistp, fbstp, fistp. */
4614 if (i.tm.base_opcode == 0xdf
4615 && (i.tm.extension_opcode == 1
4616 || i.tm.extension_opcode == 2
4617 || i.tm.extension_opcode == 3
4618 || i.tm.extension_opcode == 6
4619 || i.tm.extension_opcode == 7))
4620 return 0;
4621
4622 return 1;
4623 }
4624 }
389d00a5
JB
4625 else if (i.tm.opcode_modifier.opcodespace == SPACE_0F)
4626 {
4627 /* bt, bts, btr, btc. */
4628 if (i.tm.base_opcode == 0xba
4629 && (i.tm.extension_opcode >= 4 && i.tm.extension_opcode <= 7))
4630 return 1;
4631
4632 /* cmpxchg8b, cmpxchg16b, xrstors, vmptrld. */
4633 if (i.tm.base_opcode == 0xc7
4634 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
4635 && (i.tm.extension_opcode == 1 || i.tm.extension_opcode == 3
4636 || i.tm.extension_opcode == 6))
4637 return 1;
4638
4639 /* fxrstor, ldmxcsr, xrstor. */
4640 if (i.tm.base_opcode == 0xae
4641 && (i.tm.extension_opcode == 1
4642 || i.tm.extension_opcode == 2
4643 || i.tm.extension_opcode == 5))
4644 return 1;
4645
4646 /* lgdt, lidt, lmsw. */
4647 if (i.tm.base_opcode == 0x01
4648 && (i.tm.extension_opcode == 2
4649 || i.tm.extension_opcode == 3
4650 || i.tm.extension_opcode == 6))
4651 return 1;
4652 }
ae531041
L
4653
4654 dest = i.operands - 1;
4655
4656 /* Check fake imm8 operand and 3 source operands. */
4657 if ((i.tm.opcode_modifier.immext
4658 || i.tm.opcode_modifier.vexsources == VEX3SOURCES)
4659 && i.types[dest].bitfield.imm8)
4660 dest--;
4661
389d00a5
JB
4662 /* add, or, adc, sbb, and, sub, xor, cmp, test, xchg. */
4663 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
ae531041
L
4664 && (base_opcode == 0x1
4665 || base_opcode == 0x9
4666 || base_opcode == 0x11
4667 || base_opcode == 0x19
4668 || base_opcode == 0x21
4669 || base_opcode == 0x29
4670 || base_opcode == 0x31
4671 || base_opcode == 0x39
389d00a5
JB
4672 || (base_opcode | 2) == 0x87))
4673 return 1;
4674
4675 /* xadd. */
4676 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4677 && base_opcode == 0xc1)
ae531041
L
4678 return 1;
4679
4680 /* Check for load instruction. */
4681 return (i.types[dest].bitfield.class != ClassNone
4682 || i.types[dest].bitfield.instance == Accum);
4683}
4684
4685/* Output lfence, 0xfaee8, after instruction. */
4686
4687static void
4688insert_lfence_after (void)
4689{
4690 if (lfence_after_load && load_insn_p ())
4691 {
a09f656b 4692 /* There are also two REP string instructions that require
4693 special treatment. Specifically, the compare string (CMPS)
4694 and scan string (SCAS) instructions set EFLAGS in a manner
4695 that depends on the data being compared/scanned. When used
4696 with a REP prefix, the number of iterations may therefore
4697 vary depending on this data. If the data is a program secret
4698 chosen by the adversary using an LVI method,
4699 then this data-dependent behavior may leak some aspect
4700 of the secret. */
4701 if (((i.tm.base_opcode | 0x1) == 0xa7
4702 || (i.tm.base_opcode | 0x1) == 0xaf)
4703 && i.prefix[REP_PREFIX])
4704 {
4705 as_warn (_("`%s` changes flags which would affect control flow behavior"),
4706 i.tm.name);
4707 }
ae531041
L
4708 char *p = frag_more (3);
4709 *p++ = 0xf;
4710 *p++ = 0xae;
4711 *p = 0xe8;
4712 }
4713}
4714
4715/* Output lfence, 0xfaee8, before instruction. */
4716
4717static void
4718insert_lfence_before (void)
4719{
4720 char *p;
4721
389d00a5 4722 if (i.tm.opcode_modifier.opcodespace != SPACE_BASE)
ae531041
L
4723 return;
4724
4725 if (i.tm.base_opcode == 0xff
4726 && (i.tm.extension_opcode == 2 || i.tm.extension_opcode == 4))
4727 {
4728 /* Insert lfence before indirect branch if needed. */
4729
4730 if (lfence_before_indirect_branch == lfence_branch_none)
4731 return;
4732
4733 if (i.operands != 1)
4734 abort ();
4735
4736 if (i.reg_operands == 1)
4737 {
4738 /* Indirect branch via register. Don't insert lfence with
4739 -mlfence-after-load=yes. */
4740 if (lfence_after_load
4741 || lfence_before_indirect_branch == lfence_branch_memory)
4742 return;
4743 }
4744 else if (i.mem_operands == 1
4745 && lfence_before_indirect_branch != lfence_branch_register)
4746 {
4747 as_warn (_("indirect `%s` with memory operand should be avoided"),
4748 i.tm.name);
4749 return;
4750 }
4751 else
4752 return;
4753
4754 if (last_insn.kind != last_insn_other
4755 && last_insn.seg == now_seg)
4756 {
4757 as_warn_where (last_insn.file, last_insn.line,
4758 _("`%s` skips -mlfence-before-indirect-branch on `%s`"),
4759 last_insn.name, i.tm.name);
4760 return;
4761 }
4762
4763 p = frag_more (3);
4764 *p++ = 0xf;
4765 *p++ = 0xae;
4766 *p = 0xe8;
4767 return;
4768 }
4769
503648e4 4770 /* Output or/not/shl and lfence before near ret. */
ae531041
L
4771 if (lfence_before_ret != lfence_before_ret_none
4772 && (i.tm.base_opcode == 0xc2
503648e4 4773 || i.tm.base_opcode == 0xc3))
ae531041
L
4774 {
4775 if (last_insn.kind != last_insn_other
4776 && last_insn.seg == now_seg)
4777 {
4778 as_warn_where (last_insn.file, last_insn.line,
4779 _("`%s` skips -mlfence-before-ret on `%s`"),
4780 last_insn.name, i.tm.name);
4781 return;
4782 }
a09f656b 4783
a09f656b 4784 /* Near ret ingore operand size override under CPU64. */
503648e4 4785 char prefix = flag_code == CODE_64BIT
4786 ? 0x48
4787 : i.prefix[DATA_PREFIX] ? 0x66 : 0x0;
a09f656b 4788
4789 if (lfence_before_ret == lfence_before_ret_not)
4790 {
4791 /* not: 0xf71424, may add prefix
4792 for operand size override or 64-bit code. */
4793 p = frag_more ((prefix ? 2 : 0) + 6 + 3);
4794 if (prefix)
4795 *p++ = prefix;
ae531041
L
4796 *p++ = 0xf7;
4797 *p++ = 0x14;
4798 *p++ = 0x24;
a09f656b 4799 if (prefix)
4800 *p++ = prefix;
ae531041
L
4801 *p++ = 0xf7;
4802 *p++ = 0x14;
4803 *p++ = 0x24;
4804 }
a09f656b 4805 else
4806 {
4807 p = frag_more ((prefix ? 1 : 0) + 4 + 3);
4808 if (prefix)
4809 *p++ = prefix;
4810 if (lfence_before_ret == lfence_before_ret_or)
4811 {
4812 /* or: 0x830c2400, may add prefix
4813 for operand size override or 64-bit code. */
4814 *p++ = 0x83;
4815 *p++ = 0x0c;
4816 }
4817 else
4818 {
4819 /* shl: 0xc1242400, may add prefix
4820 for operand size override or 64-bit code. */
4821 *p++ = 0xc1;
4822 *p++ = 0x24;
4823 }
4824
4825 *p++ = 0x24;
4826 *p++ = 0x0;
4827 }
4828
ae531041
L
4829 *p++ = 0xf;
4830 *p++ = 0xae;
4831 *p = 0xe8;
4832 }
4833}
4834
252b5132
RH
4835/* This is the guts of the machine-dependent assembler. LINE points to a
4836 machine dependent instruction. This function is supposed to emit
4837 the frags/bytes it assembles to. */
4838
4839void
65da13b5 4840md_assemble (char *line)
252b5132 4841{
40fb9820 4842 unsigned int j;
83b16ac6 4843 char mnemonic[MAX_MNEM_SIZE], mnem_suffix;
d3ce72d0 4844 const insn_template *t;
252b5132 4845
47926f60 4846 /* Initialize globals. */
252b5132 4847 memset (&i, '\0', sizeof (i));
ca5312a2 4848 i.rounding.type = rc_none;
252b5132 4849 for (j = 0; j < MAX_OPERANDS; j++)
1ae12ab7 4850 i.reloc[j] = NO_RELOC;
252b5132
RH
4851 memset (disp_expressions, '\0', sizeof (disp_expressions));
4852 memset (im_expressions, '\0', sizeof (im_expressions));
ce8a8b2f 4853 save_stack_p = save_stack;
252b5132
RH
4854
4855 /* First parse an instruction mnemonic & call i386_operand for the operands.
4856 We assume that the scrubber has arranged it so that line[0] is the valid
47926f60 4857 start of a (possibly prefixed) mnemonic. */
252b5132 4858
29b0f896
AM
4859 line = parse_insn (line, mnemonic);
4860 if (line == NULL)
4861 return;
83b16ac6 4862 mnem_suffix = i.suffix;
252b5132 4863
29b0f896 4864 line = parse_operands (line, mnemonic);
ee86248c 4865 this_operand = -1;
8325cc63
JB
4866 xfree (i.memop1_string);
4867 i.memop1_string = NULL;
29b0f896
AM
4868 if (line == NULL)
4869 return;
252b5132 4870
29b0f896
AM
4871 /* Now we've parsed the mnemonic into a set of templates, and have the
4872 operands at hand. */
4873
b630c145 4874 /* All Intel opcodes have reversed operands except for "bound", "enter",
c0e54661
JB
4875 "invlpg*", "monitor*", "mwait*", "tpause", "umwait", "pvalidate",
4876 "rmpadjust", and "rmpupdate". We also don't reverse intersegment "jmp"
4877 and "call" instructions with 2 immediate operands so that the immediate
4878 segment precedes the offset consistently in Intel and AT&T modes. */
4d456e3d
L
4879 if (intel_syntax
4880 && i.operands > 1
29b0f896 4881 && (strcmp (mnemonic, "bound") != 0)
c0e54661 4882 && (strncmp (mnemonic, "invlpg", 6) != 0)
d34049e8
ML
4883 && !startswith (mnemonic, "monitor")
4884 && !startswith (mnemonic, "mwait")
c0e54661 4885 && (strcmp (mnemonic, "pvalidate") != 0)
d34049e8 4886 && !startswith (mnemonic, "rmp")
b630c145
JB
4887 && (strcmp (mnemonic, "tpause") != 0)
4888 && (strcmp (mnemonic, "umwait") != 0)
40fb9820
L
4889 && !(operand_type_check (i.types[0], imm)
4890 && operand_type_check (i.types[1], imm)))
29b0f896
AM
4891 swap_operands ();
4892
ec56d5c0
JB
4893 /* The order of the immediates should be reversed
4894 for 2 immediates extrq and insertq instructions */
4895 if (i.imm_operands == 2
4896 && (strcmp (mnemonic, "extrq") == 0
4897 || strcmp (mnemonic, "insertq") == 0))
4898 swap_2_operands (0, 1);
4899
29b0f896
AM
4900 if (i.imm_operands)
4901 optimize_imm ();
4902
9386188e
JB
4903 if (i.disp_operands && !want_disp32 (current_templates->start)
4904 && (!current_templates->start->opcode_modifier.jump
4905 || i.jumpabsolute || i.types[0].bitfield.baseindex))
cce08655
JB
4906 {
4907 for (j = 0; j < i.operands; ++j)
4908 {
4909 const expressionS *exp = i.op[j].disps;
4910
4911 if (!operand_type_check (i.types[j], disp))
4912 continue;
4913
4914 if (exp->X_op != O_constant)
4915 continue;
4916
4917 /* Since displacement is signed extended to 64bit, don't allow
a775efc8 4918 disp32 if it is out of range. */
cce08655
JB
4919 if (fits_in_signed_long (exp->X_add_number))
4920 continue;
4921
a775efc8 4922 i.types[j].bitfield.disp32 = 0;
cce08655
JB
4923 if (i.types[j].bitfield.baseindex)
4924 {
f493c217
AM
4925 as_bad (_("0x%" PRIx64 " out of range of signed 32bit displacement"),
4926 (uint64_t) exp->X_add_number);
cce08655
JB
4927 return;
4928 }
4929 }
4930 }
4931
b300c311
L
4932 /* Don't optimize displacement for movabs since it only takes 64bit
4933 displacement. */
4934 if (i.disp_operands
1a42a9fe 4935 && i.disp_encoding <= disp_encoding_8bit
862be3fb
L
4936 && (flag_code != CODE_64BIT
4937 || strcmp (mnemonic, "movabs") != 0))
4938 optimize_disp ();
29b0f896
AM
4939
4940 /* Next, we find a template that matches the given insn,
4941 making sure the overlap of the given operands types is consistent
4942 with the template operand types. */
252b5132 4943
83b16ac6 4944 if (!(t = match_template (mnem_suffix)))
29b0f896 4945 return;
252b5132 4946
7bab8ab5 4947 if (sse_check != check_none
ffb86450
JB
4948 /* The opcode space check isn't strictly needed; it's there only to
4949 bypass the logic below when easily possible. */
4950 && t->opcode_modifier.opcodespace >= SPACE_0F
4951 && t->opcode_modifier.opcodespace <= SPACE_0F3A
4952 && !i.tm.cpu_flags.bitfield.cpusse4a
4953 && !is_any_vex_encoding (t))
daf50ae7 4954 {
ffb86450
JB
4955 bool simd = false;
4956
4957 for (j = 0; j < t->operands; ++j)
4958 {
4959 if (t->operand_types[j].bitfield.class == RegMMX)
4960 break;
4961 if (t->operand_types[j].bitfield.class == RegSIMD)
4962 simd = true;
4963 }
4964
4965 if (j >= t->operands && simd)
4966 (sse_check == check_warning
4967 ? as_warn
4968 : as_bad) (_("SSE instruction `%s' is used"), i.tm.name);
daf50ae7
L
4969 }
4970
40fb9820 4971 if (i.tm.opcode_modifier.fwait)
29b0f896
AM
4972 if (!add_prefix (FWAIT_OPCODE))
4973 return;
252b5132 4974
d5de92cf 4975 /* Check if REP prefix is OK. */
742732c7 4976 if (i.rep_prefix && i.tm.opcode_modifier.prefixok != PrefixRep)
d5de92cf
L
4977 {
4978 as_bad (_("invalid instruction `%s' after `%s'"),
4979 i.tm.name, i.rep_prefix);
4980 return;
4981 }
4982
c1ba0266
L
4983 /* Check for lock without a lockable instruction. Destination operand
4984 must be memory unless it is xchg (0x86). */
c32fa91d 4985 if (i.prefix[LOCK_PREFIX]
742732c7 4986 && (i.tm.opcode_modifier.prefixok < PrefixLock
c1ba0266
L
4987 || i.mem_operands == 0
4988 || (i.tm.base_opcode != 0x86
8dc0818e 4989 && !(i.flags[i.operands - 1] & Operand_Mem))))
c32fa91d
L
4990 {
4991 as_bad (_("expecting lockable instruction after `lock'"));
4992 return;
4993 }
4994
40d231b4
JB
4995 /* Check for data size prefix on VEX/XOP/EVEX encoded and SIMD insns. */
4996 if (i.prefix[DATA_PREFIX]
4997 && (is_any_vex_encoding (&i.tm)
4998 || i.tm.operand_types[i.imm_operands].bitfield.class >= RegMMX
4999 || i.tm.operand_types[i.imm_operands + 1].bitfield.class >= RegMMX))
7a8655d2
JB
5000 {
5001 as_bad (_("data size prefix invalid with `%s'"), i.tm.name);
5002 return;
5003 }
5004
42164a71 5005 /* Check if HLE prefix is OK. */
165de32a 5006 if (i.hle_prefix && !check_hle ())
42164a71
L
5007 return;
5008
7e8b059b
L
5009 /* Check BND prefix. */
5010 if (i.bnd_prefix && !i.tm.opcode_modifier.bndprefixok)
5011 as_bad (_("expecting valid branch instruction after `bnd'"));
5012
04ef582a 5013 /* Check NOTRACK prefix. */
742732c7 5014 if (i.notrack_prefix && i.tm.opcode_modifier.prefixok != PrefixNoTrack)
9fef80d6 5015 as_bad (_("expecting indirect branch instruction after `notrack'"));
04ef582a 5016
327e8c42
JB
5017 if (i.tm.cpu_flags.bitfield.cpumpx)
5018 {
5019 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
5020 as_bad (_("32-bit address isn't allowed in 64-bit MPX instructions."));
5021 else if (flag_code != CODE_16BIT
5022 ? i.prefix[ADDR_PREFIX]
5023 : i.mem_operands && !i.prefix[ADDR_PREFIX])
5024 as_bad (_("16-bit address isn't allowed in MPX instructions"));
5025 }
7e8b059b
L
5026
5027 /* Insert BND prefix. */
76d3a78a
JB
5028 if (add_bnd_prefix && i.tm.opcode_modifier.bndprefixok)
5029 {
5030 if (!i.prefix[BND_PREFIX])
5031 add_prefix (BND_PREFIX_OPCODE);
5032 else if (i.prefix[BND_PREFIX] != BND_PREFIX_OPCODE)
5033 {
5034 as_warn (_("replacing `rep'/`repe' prefix by `bnd'"));
5035 i.prefix[BND_PREFIX] = BND_PREFIX_OPCODE;
5036 }
5037 }
7e8b059b 5038
29b0f896 5039 /* Check string instruction segment overrides. */
51c8edf6 5040 if (i.tm.opcode_modifier.isstring >= IS_STRING_ES_OP0)
29b0f896 5041 {
51c8edf6 5042 gas_assert (i.mem_operands);
29b0f896 5043 if (!check_string ())
5dd0794d 5044 return;
fc0763e6 5045 i.disp_operands = 0;
29b0f896 5046 }
5dd0794d 5047
b6f8c7c4
L
5048 if (optimize && !i.no_optimize && i.tm.opcode_modifier.optimize)
5049 optimize_encoding ();
5050
c8480b58
L
5051 if (use_unaligned_vector_move)
5052 encode_with_unaligned_vector_move ();
5053
29b0f896
AM
5054 if (!process_suffix ())
5055 return;
e413e4e9 5056
ef07be45
CL
5057 /* Check if IP-relative addressing requirements can be satisfied. */
5058 if (i.tm.cpu_flags.bitfield.cpuprefetchi
5059 && !(i.base_reg && i.base_reg->reg_num == RegIP))
f2462532 5060 as_warn (_("'%s' only supports RIP-relative address"), i.tm.name);
ef07be45 5061
921eafea 5062 /* Update operand types and check extended states. */
bc0844ae 5063 for (j = 0; j < i.operands; j++)
921eafea
L
5064 {
5065 i.types[j] = operand_type_and (i.types[j], i.tm.operand_types[j]);
3d70986f 5066 switch (i.tm.operand_types[j].bitfield.class)
921eafea
L
5067 {
5068 default:
5069 break;
5070 case RegMMX:
5071 i.xstate |= xstate_mmx;
5072 break;
5073 case RegMask:
32930e4e 5074 i.xstate |= xstate_mask;
921eafea
L
5075 break;
5076 case RegSIMD:
3d70986f 5077 if (i.tm.operand_types[j].bitfield.tmmword)
921eafea 5078 i.xstate |= xstate_tmm;
3d70986f 5079 else if (i.tm.operand_types[j].bitfield.zmmword)
921eafea 5080 i.xstate |= xstate_zmm;
3d70986f 5081 else if (i.tm.operand_types[j].bitfield.ymmword)
921eafea 5082 i.xstate |= xstate_ymm;
3d70986f 5083 else if (i.tm.operand_types[j].bitfield.xmmword)
921eafea
L
5084 i.xstate |= xstate_xmm;
5085 break;
5086 }
5087 }
bc0844ae 5088
29b0f896
AM
5089 /* Make still unresolved immediate matches conform to size of immediate
5090 given in i.suffix. */
5091 if (!finalize_imm ())
5092 return;
252b5132 5093
40fb9820 5094 if (i.types[0].bitfield.imm1)
29b0f896 5095 i.imm_operands = 0; /* kludge for shift insns. */
252b5132 5096
9afe6eb8
L
5097 /* We only need to check those implicit registers for instructions
5098 with 3 operands or less. */
5099 if (i.operands <= 3)
5100 for (j = 0; j < i.operands; j++)
75e5731b
JB
5101 if (i.types[j].bitfield.instance != InstanceNone
5102 && !i.types[j].bitfield.xmmword)
9afe6eb8 5103 i.reg_operands--;
40fb9820 5104
29b0f896
AM
5105 /* For insns with operands there are more diddles to do to the opcode. */
5106 if (i.operands)
5107 {
5108 if (!process_operands ())
5109 return;
5110 }
8c190ce0 5111 else if (!quiet_warnings && i.tm.opcode_modifier.ugh)
29b0f896
AM
5112 {
5113 /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc. */
5114 as_warn (_("translating to `%sp'"), i.tm.name);
5115 }
252b5132 5116
7a8655d2 5117 if (is_any_vex_encoding (&i.tm))
9e5e5283 5118 {
c1dc7af5 5119 if (!cpu_arch_flags.bitfield.cpui286)
9e5e5283 5120 {
c1dc7af5 5121 as_bad (_("instruction `%s' isn't supported outside of protected mode."),
9e5e5283
L
5122 i.tm.name);
5123 return;
5124 }
c0f3af97 5125
0b9404fd
JB
5126 /* Check for explicit REX prefix. */
5127 if (i.prefix[REX_PREFIX] || i.rex_encoding)
5128 {
5129 as_bad (_("REX prefix invalid with `%s'"), i.tm.name);
5130 return;
5131 }
5132
9e5e5283
L
5133 if (i.tm.opcode_modifier.vex)
5134 build_vex_prefix (t);
5135 else
5136 build_evex_prefix ();
0b9404fd
JB
5137
5138 /* The individual REX.RXBW bits got consumed. */
5139 i.rex &= REX_OPCODE;
9e5e5283 5140 }
43234a1e 5141
5dd85c99
SP
5142 /* Handle conversion of 'int $3' --> special int3 insn. XOP or FMA4
5143 instructions may define INT_OPCODE as well, so avoid this corner
5144 case for those instructions that use MODRM. */
389d00a5
JB
5145 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
5146 && i.tm.base_opcode == INT_OPCODE
a6461c02
SP
5147 && !i.tm.opcode_modifier.modrm
5148 && i.op[0].imms->X_add_number == 3)
29b0f896
AM
5149 {
5150 i.tm.base_opcode = INT3_OPCODE;
5151 i.imm_operands = 0;
5152 }
252b5132 5153
0cfa3eb3
JB
5154 if ((i.tm.opcode_modifier.jump == JUMP
5155 || i.tm.opcode_modifier.jump == JUMP_BYTE
5156 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896
AM
5157 && i.op[0].disps->X_op == O_constant)
5158 {
5159 /* Convert "jmp constant" (and "call constant") to a jump (call) to
5160 the absolute address given by the constant. Since ix86 jumps and
5161 calls are pc relative, we need to generate a reloc. */
5162 i.op[0].disps->X_add_symbol = &abs_symbol;
5163 i.op[0].disps->X_op = O_symbol;
5164 }
252b5132 5165
29b0f896
AM
5166 /* For 8 bit registers we need an empty rex prefix. Also if the
5167 instruction already has a prefix, we need to convert old
5168 registers to new ones. */
773f551c 5169
bab6aec1 5170 if ((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte
29b0f896 5171 && (i.op[0].regs->reg_flags & RegRex64) != 0)
bab6aec1 5172 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte
29b0f896 5173 && (i.op[1].regs->reg_flags & RegRex64) != 0)
bab6aec1
JB
5174 || (((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte)
5175 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte))
29b0f896
AM
5176 && i.rex != 0))
5177 {
5178 int x;
726c5dcd 5179
29b0f896
AM
5180 i.rex |= REX_OPCODE;
5181 for (x = 0; x < 2; x++)
5182 {
5183 /* Look for 8 bit operand that uses old registers. */
bab6aec1 5184 if (i.types[x].bitfield.class == Reg && i.types[x].bitfield.byte
29b0f896 5185 && (i.op[x].regs->reg_flags & RegRex64) == 0)
773f551c 5186 {
3f93af61 5187 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
29b0f896
AM
5188 /* In case it is "hi" register, give up. */
5189 if (i.op[x].regs->reg_num > 3)
a540244d 5190 as_bad (_("can't encode register '%s%s' in an "
4eed87de 5191 "instruction requiring REX prefix."),
a540244d 5192 register_prefix, i.op[x].regs->reg_name);
773f551c 5193
29b0f896
AM
5194 /* Otherwise it is equivalent to the extended register.
5195 Since the encoding doesn't change this is merely
5196 cosmetic cleanup for debug output. */
5197
5198 i.op[x].regs = i.op[x].regs + 8;
773f551c 5199 }
29b0f896
AM
5200 }
5201 }
773f551c 5202
6b6b6807
L
5203 if (i.rex == 0 && i.rex_encoding)
5204 {
5205 /* Check if we can add a REX_OPCODE byte. Look for 8 bit operand
3f93af61 5206 that uses legacy register. If it is "hi" register, don't add
6b6b6807
L
5207 the REX_OPCODE byte. */
5208 int x;
5209 for (x = 0; x < 2; x++)
bab6aec1 5210 if (i.types[x].bitfield.class == Reg
6b6b6807
L
5211 && i.types[x].bitfield.byte
5212 && (i.op[x].regs->reg_flags & RegRex64) == 0
5213 && i.op[x].regs->reg_num > 3)
5214 {
3f93af61 5215 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
5b7c81bd 5216 i.rex_encoding = false;
6b6b6807
L
5217 break;
5218 }
5219
5220 if (i.rex_encoding)
5221 i.rex = REX_OPCODE;
5222 }
5223
7ab9ffdd 5224 if (i.rex != 0)
29b0f896
AM
5225 add_prefix (REX_OPCODE | i.rex);
5226
ae531041
L
5227 insert_lfence_before ();
5228
29b0f896
AM
5229 /* We are ready to output the insn. */
5230 output_insn ();
e379e5f3 5231
ae531041
L
5232 insert_lfence_after ();
5233
e379e5f3
L
5234 last_insn.seg = now_seg;
5235
5236 if (i.tm.opcode_modifier.isprefix)
5237 {
5238 last_insn.kind = last_insn_prefix;
5239 last_insn.name = i.tm.name;
5240 last_insn.file = as_where (&last_insn.line);
5241 }
5242 else
5243 last_insn.kind = last_insn_other;
29b0f896
AM
5244}
5245
5246static char *
e3bb37b5 5247parse_insn (char *line, char *mnemonic)
29b0f896
AM
5248{
5249 char *l = line;
5250 char *token_start = l;
5251 char *mnem_p;
5c6af06e 5252 int supported;
d3ce72d0 5253 const insn_template *t;
b6169b20 5254 char *dot_p = NULL;
29b0f896 5255
29b0f896
AM
5256 while (1)
5257 {
5258 mnem_p = mnemonic;
5259 while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
5260 {
b6169b20
L
5261 if (*mnem_p == '.')
5262 dot_p = mnem_p;
29b0f896
AM
5263 mnem_p++;
5264 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
45288df1 5265 {
29b0f896
AM
5266 as_bad (_("no such instruction: `%s'"), token_start);
5267 return NULL;
5268 }
5269 l++;
5270 }
5271 if (!is_space_char (*l)
5272 && *l != END_OF_INSN
e44823cf
JB
5273 && (intel_syntax
5274 || (*l != PREFIX_SEPARATOR
5275 && *l != ',')))
29b0f896
AM
5276 {
5277 as_bad (_("invalid character %s in mnemonic"),
5278 output_invalid (*l));
5279 return NULL;
5280 }
5281 if (token_start == l)
5282 {
e44823cf 5283 if (!intel_syntax && *l == PREFIX_SEPARATOR)
29b0f896
AM
5284 as_bad (_("expecting prefix; got nothing"));
5285 else
5286 as_bad (_("expecting mnemonic; got nothing"));
5287 return NULL;
5288 }
45288df1 5289
29b0f896 5290 /* Look up instruction (or prefix) via hash table. */
629310ab 5291 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
47926f60 5292
29b0f896
AM
5293 if (*l != END_OF_INSN
5294 && (!is_space_char (*l) || l[1] != END_OF_INSN)
5295 && current_templates
40fb9820 5296 && current_templates->start->opcode_modifier.isprefix)
29b0f896 5297 {
c6fb90c8 5298 if (!cpu_flags_check_cpu64 (current_templates->start->cpu_flags))
2dd88dca
JB
5299 {
5300 as_bad ((flag_code != CODE_64BIT
5301 ? _("`%s' is only supported in 64-bit mode")
5302 : _("`%s' is not supported in 64-bit mode")),
5303 current_templates->start->name);
5304 return NULL;
5305 }
29b0f896
AM
5306 /* If we are in 16-bit mode, do not allow addr16 or data16.
5307 Similarly, in 32-bit mode, do not allow addr32 or data32. */
673fe0f0
JB
5308 if ((current_templates->start->opcode_modifier.size == SIZE16
5309 || current_templates->start->opcode_modifier.size == SIZE32)
29b0f896 5310 && flag_code != CODE_64BIT
673fe0f0 5311 && ((current_templates->start->opcode_modifier.size == SIZE32)
29b0f896
AM
5312 ^ (flag_code == CODE_16BIT)))
5313 {
5314 as_bad (_("redundant %s prefix"),
5315 current_templates->start->name);
5316 return NULL;
45288df1 5317 }
31184569
JB
5318
5319 if (current_templates->start->base_opcode == PSEUDO_PREFIX)
29b0f896 5320 {
86fa6981 5321 /* Handle pseudo prefixes. */
31184569 5322 switch (current_templates->start->extension_opcode)
86fa6981 5323 {
41eb8e88 5324 case Prefix_Disp8:
86fa6981
L
5325 /* {disp8} */
5326 i.disp_encoding = disp_encoding_8bit;
5327 break;
41eb8e88
L
5328 case Prefix_Disp16:
5329 /* {disp16} */
5330 i.disp_encoding = disp_encoding_16bit;
5331 break;
5332 case Prefix_Disp32:
86fa6981
L
5333 /* {disp32} */
5334 i.disp_encoding = disp_encoding_32bit;
5335 break;
41eb8e88 5336 case Prefix_Load:
86fa6981
L
5337 /* {load} */
5338 i.dir_encoding = dir_encoding_load;
5339 break;
41eb8e88 5340 case Prefix_Store:
86fa6981
L
5341 /* {store} */
5342 i.dir_encoding = dir_encoding_store;
5343 break;
41eb8e88 5344 case Prefix_VEX:
42e04b36
L
5345 /* {vex} */
5346 i.vec_encoding = vex_encoding_vex;
86fa6981 5347 break;
41eb8e88 5348 case Prefix_VEX3:
86fa6981
L
5349 /* {vex3} */
5350 i.vec_encoding = vex_encoding_vex3;
5351 break;
41eb8e88 5352 case Prefix_EVEX:
86fa6981
L
5353 /* {evex} */
5354 i.vec_encoding = vex_encoding_evex;
5355 break;
41eb8e88 5356 case Prefix_REX:
6b6b6807 5357 /* {rex} */
5b7c81bd 5358 i.rex_encoding = true;
6b6b6807 5359 break;
41eb8e88 5360 case Prefix_NoOptimize:
b6f8c7c4 5361 /* {nooptimize} */
5b7c81bd 5362 i.no_optimize = true;
b6f8c7c4 5363 break;
86fa6981
L
5364 default:
5365 abort ();
5366 }
5367 }
5368 else
5369 {
5370 /* Add prefix, checking for repeated prefixes. */
4e9ac44a 5371 switch (add_prefix (current_templates->start->base_opcode))
86fa6981 5372 {
4e9ac44a
L
5373 case PREFIX_EXIST:
5374 return NULL;
5375 case PREFIX_DS:
d777820b 5376 if (current_templates->start->cpu_flags.bitfield.cpuibt)
4e9ac44a
L
5377 i.notrack_prefix = current_templates->start->name;
5378 break;
5379 case PREFIX_REP:
5380 if (current_templates->start->cpu_flags.bitfield.cpuhle)
5381 i.hle_prefix = current_templates->start->name;
5382 else if (current_templates->start->cpu_flags.bitfield.cpumpx)
5383 i.bnd_prefix = current_templates->start->name;
5384 else
5385 i.rep_prefix = current_templates->start->name;
5386 break;
5387 default:
5388 break;
86fa6981 5389 }
29b0f896
AM
5390 }
5391 /* Skip past PREFIX_SEPARATOR and reset token_start. */
5392 token_start = ++l;
5393 }
5394 else
5395 break;
5396 }
45288df1 5397
30a55f88 5398 if (!current_templates)
b6169b20 5399 {
07d5e953
JB
5400 /* Deprecated functionality (new code should use pseudo-prefixes instead):
5401 Check if we should swap operand or force 32bit displacement in
f8a5c266 5402 encoding. */
30a55f88 5403 if (mnem_p - 2 == dot_p && dot_p[1] == 's')
64c49ab3 5404 i.dir_encoding = dir_encoding_swap;
8d63c93e 5405 else if (mnem_p - 3 == dot_p
a501d77e
L
5406 && dot_p[1] == 'd'
5407 && dot_p[2] == '8')
5408 i.disp_encoding = disp_encoding_8bit;
8d63c93e 5409 else if (mnem_p - 4 == dot_p
f8a5c266
L
5410 && dot_p[1] == 'd'
5411 && dot_p[2] == '3'
5412 && dot_p[3] == '2')
a501d77e 5413 i.disp_encoding = disp_encoding_32bit;
30a55f88
L
5414 else
5415 goto check_suffix;
5416 mnem_p = dot_p;
5417 *dot_p = '\0';
629310ab 5418 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
b6169b20
L
5419 }
5420
29b0f896
AM
5421 if (!current_templates)
5422 {
dc1e8a47 5423 check_suffix:
1c529385 5424 if (mnem_p > mnemonic)
29b0f896 5425 {
1c529385
LH
5426 /* See if we can get a match by trimming off a suffix. */
5427 switch (mnem_p[-1])
29b0f896 5428 {
1c529385
LH
5429 case WORD_MNEM_SUFFIX:
5430 if (intel_syntax && (intel_float_operand (mnemonic) & 2))
29b0f896
AM
5431 i.suffix = SHORT_MNEM_SUFFIX;
5432 else
1c529385
LH
5433 /* Fall through. */
5434 case BYTE_MNEM_SUFFIX:
5435 case QWORD_MNEM_SUFFIX:
5436 i.suffix = mnem_p[-1];
29b0f896 5437 mnem_p[-1] = '\0';
fe0e921f
AM
5438 current_templates
5439 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5440 break;
5441 case SHORT_MNEM_SUFFIX:
5442 case LONG_MNEM_SUFFIX:
5443 if (!intel_syntax)
5444 {
5445 i.suffix = mnem_p[-1];
5446 mnem_p[-1] = '\0';
fe0e921f
AM
5447 current_templates
5448 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5449 }
5450 break;
5451
5452 /* Intel Syntax. */
5453 case 'd':
5454 if (intel_syntax)
5455 {
5456 if (intel_float_operand (mnemonic) == 1)
5457 i.suffix = SHORT_MNEM_SUFFIX;
5458 else
5459 i.suffix = LONG_MNEM_SUFFIX;
5460 mnem_p[-1] = '\0';
fe0e921f
AM
5461 current_templates
5462 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5463 }
5464 break;
29b0f896 5465 }
29b0f896 5466 }
1c529385 5467
29b0f896
AM
5468 if (!current_templates)
5469 {
5470 as_bad (_("no such instruction: `%s'"), token_start);
5471 return NULL;
5472 }
5473 }
252b5132 5474
0cfa3eb3
JB
5475 if (current_templates->start->opcode_modifier.jump == JUMP
5476 || current_templates->start->opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
5477 {
5478 /* Check for a branch hint. We allow ",pt" and ",pn" for
5479 predict taken and predict not taken respectively.
5480 I'm not sure that branch hints actually do anything on loop
5481 and jcxz insns (JumpByte) for current Pentium4 chips. They
5482 may work in the future and it doesn't hurt to accept them
5483 now. */
5484 if (l[0] == ',' && l[1] == 'p')
5485 {
5486 if (l[2] == 't')
5487 {
5488 if (!add_prefix (DS_PREFIX_OPCODE))
5489 return NULL;
5490 l += 3;
5491 }
5492 else if (l[2] == 'n')
5493 {
5494 if (!add_prefix (CS_PREFIX_OPCODE))
5495 return NULL;
5496 l += 3;
5497 }
5498 }
5499 }
5500 /* Any other comma loses. */
5501 if (*l == ',')
5502 {
5503 as_bad (_("invalid character %s in mnemonic"),
5504 output_invalid (*l));
5505 return NULL;
5506 }
252b5132 5507
29b0f896 5508 /* Check if instruction is supported on specified architecture. */
5c6af06e
JB
5509 supported = 0;
5510 for (t = current_templates->start; t < current_templates->end; ++t)
5511 {
c0f3af97
L
5512 supported |= cpu_flags_match (t);
5513 if (supported == CPU_FLAGS_PERFECT_MATCH)
d59a54c2 5514 return l;
29b0f896 5515 }
3629bb00 5516
548d0ee6
JB
5517 if (!(supported & CPU_FLAGS_64BIT_MATCH))
5518 as_bad (flag_code == CODE_64BIT
5519 ? _("`%s' is not supported in 64-bit mode")
5520 : _("`%s' is only supported in 64-bit mode"),
5521 current_templates->start->name);
5522 else
5523 as_bad (_("`%s' is not supported on `%s%s'"),
5524 current_templates->start->name,
5525 cpu_arch_name ? cpu_arch_name : default_arch,
5526 cpu_sub_arch_name ? cpu_sub_arch_name : "");
252b5132 5527
548d0ee6 5528 return NULL;
29b0f896 5529}
252b5132 5530
29b0f896 5531static char *
e3bb37b5 5532parse_operands (char *l, const char *mnemonic)
29b0f896
AM
5533{
5534 char *token_start;
3138f287 5535
29b0f896
AM
5536 /* 1 if operand is pending after ','. */
5537 unsigned int expecting_operand = 0;
252b5132 5538
29b0f896
AM
5539 while (*l != END_OF_INSN)
5540 {
e68c3d59
JB
5541 /* Non-zero if operand parens not balanced. */
5542 unsigned int paren_not_balanced = 0;
5543 /* True if inside double quotes. */
5544 bool in_quotes = false;
5545
29b0f896
AM
5546 /* Skip optional white space before operand. */
5547 if (is_space_char (*l))
5548 ++l;
d02603dc 5549 if (!is_operand_char (*l) && *l != END_OF_INSN && *l != '"')
29b0f896
AM
5550 {
5551 as_bad (_("invalid character %s before operand %d"),
5552 output_invalid (*l),
5553 i.operands + 1);
5554 return NULL;
5555 }
d02603dc 5556 token_start = l; /* After white space. */
e68c3d59 5557 while (in_quotes || paren_not_balanced || *l != ',')
29b0f896
AM
5558 {
5559 if (*l == END_OF_INSN)
5560 {
e68c3d59
JB
5561 if (in_quotes)
5562 {
5563 as_bad (_("unbalanced double quotes in operand %d."),
5564 i.operands + 1);
5565 return NULL;
5566 }
29b0f896
AM
5567 if (paren_not_balanced)
5568 {
98ff9f1c
JB
5569 know (!intel_syntax);
5570 as_bad (_("unbalanced parenthesis in operand %d."),
5571 i.operands + 1);
29b0f896
AM
5572 return NULL;
5573 }
5574 else
5575 break; /* we are done */
5576 }
e68c3d59
JB
5577 else if (*l == '\\' && l[1] == '"')
5578 ++l;
5579 else if (*l == '"')
5580 in_quotes = !in_quotes;
5581 else if (!in_quotes && !is_operand_char (*l) && !is_space_char (*l))
29b0f896
AM
5582 {
5583 as_bad (_("invalid character %s in operand %d"),
5584 output_invalid (*l),
5585 i.operands + 1);
5586 return NULL;
5587 }
e68c3d59 5588 if (!intel_syntax && !in_quotes)
29b0f896
AM
5589 {
5590 if (*l == '(')
5591 ++paren_not_balanced;
5592 if (*l == ')')
5593 --paren_not_balanced;
5594 }
29b0f896
AM
5595 l++;
5596 }
5597 if (l != token_start)
5598 { /* Yes, we've read in another operand. */
5599 unsigned int operand_ok;
5600 this_operand = i.operands++;
5601 if (i.operands > MAX_OPERANDS)
5602 {
5603 as_bad (_("spurious operands; (%d operands/instruction max)"),
5604 MAX_OPERANDS);
5605 return NULL;
5606 }
9d46ce34 5607 i.types[this_operand].bitfield.unspecified = 1;
29b0f896
AM
5608 /* Now parse operand adding info to 'i' as we go along. */
5609 END_STRING_AND_SAVE (l);
5610
1286ab78
L
5611 if (i.mem_operands > 1)
5612 {
5613 as_bad (_("too many memory references for `%s'"),
5614 mnemonic);
5615 return 0;
5616 }
5617
29b0f896
AM
5618 if (intel_syntax)
5619 operand_ok =
5620 i386_intel_operand (token_start,
5621 intel_float_operand (mnemonic));
5622 else
a7619375 5623 operand_ok = i386_att_operand (token_start);
29b0f896
AM
5624
5625 RESTORE_END_STRING (l);
5626 if (!operand_ok)
5627 return NULL;
5628 }
5629 else
5630 {
5631 if (expecting_operand)
5632 {
5633 expecting_operand_after_comma:
5634 as_bad (_("expecting operand after ','; got nothing"));
5635 return NULL;
5636 }
5637 if (*l == ',')
5638 {
5639 as_bad (_("expecting operand before ','; got nothing"));
5640 return NULL;
5641 }
5642 }
7f3f1ea2 5643
29b0f896
AM
5644 /* Now *l must be either ',' or END_OF_INSN. */
5645 if (*l == ',')
5646 {
5647 if (*++l == END_OF_INSN)
5648 {
5649 /* Just skip it, if it's \n complain. */
5650 goto expecting_operand_after_comma;
5651 }
5652 expecting_operand = 1;
5653 }
5654 }
5655 return l;
5656}
7f3f1ea2 5657
050dfa73 5658static void
783c187b 5659swap_2_operands (unsigned int xchg1, unsigned int xchg2)
050dfa73
MM
5660{
5661 union i386_op temp_op;
40fb9820 5662 i386_operand_type temp_type;
c48dadc9 5663 unsigned int temp_flags;
050dfa73 5664 enum bfd_reloc_code_real temp_reloc;
4eed87de 5665
050dfa73
MM
5666 temp_type = i.types[xchg2];
5667 i.types[xchg2] = i.types[xchg1];
5668 i.types[xchg1] = temp_type;
c48dadc9
JB
5669
5670 temp_flags = i.flags[xchg2];
5671 i.flags[xchg2] = i.flags[xchg1];
5672 i.flags[xchg1] = temp_flags;
5673
050dfa73
MM
5674 temp_op = i.op[xchg2];
5675 i.op[xchg2] = i.op[xchg1];
5676 i.op[xchg1] = temp_op;
c48dadc9 5677
050dfa73
MM
5678 temp_reloc = i.reloc[xchg2];
5679 i.reloc[xchg2] = i.reloc[xchg1];
5680 i.reloc[xchg1] = temp_reloc;
43234a1e 5681
6225c532 5682 if (i.mask.reg)
43234a1e 5683 {
6225c532
JB
5684 if (i.mask.operand == xchg1)
5685 i.mask.operand = xchg2;
5686 else if (i.mask.operand == xchg2)
5687 i.mask.operand = xchg1;
43234a1e 5688 }
a5748e0d 5689 if (i.broadcast.type || i.broadcast.bytes)
43234a1e 5690 {
5273a3cd
JB
5691 if (i.broadcast.operand == xchg1)
5692 i.broadcast.operand = xchg2;
5693 else if (i.broadcast.operand == xchg2)
5694 i.broadcast.operand = xchg1;
43234a1e 5695 }
050dfa73
MM
5696}
5697
29b0f896 5698static void
e3bb37b5 5699swap_operands (void)
29b0f896 5700{
b7c61d9a 5701 switch (i.operands)
050dfa73 5702 {
c0f3af97 5703 case 5:
b7c61d9a 5704 case 4:
4d456e3d 5705 swap_2_operands (1, i.operands - 2);
1a0670f3 5706 /* Fall through. */
b7c61d9a
L
5707 case 3:
5708 case 2:
4d456e3d 5709 swap_2_operands (0, i.operands - 1);
b7c61d9a
L
5710 break;
5711 default:
5712 abort ();
29b0f896 5713 }
29b0f896
AM
5714
5715 if (i.mem_operands == 2)
5716 {
5e042380 5717 const reg_entry *temp_seg;
29b0f896
AM
5718 temp_seg = i.seg[0];
5719 i.seg[0] = i.seg[1];
5720 i.seg[1] = temp_seg;
5721 }
5722}
252b5132 5723
29b0f896
AM
5724/* Try to ensure constant immediates are represented in the smallest
5725 opcode possible. */
5726static void
e3bb37b5 5727optimize_imm (void)
29b0f896
AM
5728{
5729 char guess_suffix = 0;
5730 int op;
252b5132 5731
29b0f896
AM
5732 if (i.suffix)
5733 guess_suffix = i.suffix;
5734 else if (i.reg_operands)
5735 {
5736 /* Figure out a suffix from the last register operand specified.
75e5731b
JB
5737 We can't do this properly yet, i.e. excluding special register
5738 instances, but the following works for instructions with
5739 immediates. In any case, we can't set i.suffix yet. */
29b0f896 5740 for (op = i.operands; --op >= 0;)
bab6aec1
JB
5741 if (i.types[op].bitfield.class != Reg)
5742 continue;
5743 else if (i.types[op].bitfield.byte)
7ab9ffdd 5744 {
40fb9820
L
5745 guess_suffix = BYTE_MNEM_SUFFIX;
5746 break;
5747 }
bab6aec1 5748 else if (i.types[op].bitfield.word)
252b5132 5749 {
40fb9820
L
5750 guess_suffix = WORD_MNEM_SUFFIX;
5751 break;
5752 }
bab6aec1 5753 else if (i.types[op].bitfield.dword)
40fb9820
L
5754 {
5755 guess_suffix = LONG_MNEM_SUFFIX;
5756 break;
5757 }
bab6aec1 5758 else if (i.types[op].bitfield.qword)
40fb9820
L
5759 {
5760 guess_suffix = QWORD_MNEM_SUFFIX;
29b0f896 5761 break;
252b5132 5762 }
29b0f896
AM
5763 }
5764 else if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
5765 guess_suffix = WORD_MNEM_SUFFIX;
5766
5767 for (op = i.operands; --op >= 0;)
40fb9820 5768 if (operand_type_check (i.types[op], imm))
29b0f896
AM
5769 {
5770 switch (i.op[op].imms->X_op)
252b5132 5771 {
29b0f896
AM
5772 case O_constant:
5773 /* If a suffix is given, this operand may be shortened. */
5774 switch (guess_suffix)
252b5132 5775 {
29b0f896 5776 case LONG_MNEM_SUFFIX:
40fb9820
L
5777 i.types[op].bitfield.imm32 = 1;
5778 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
5779 break;
5780 case WORD_MNEM_SUFFIX:
40fb9820
L
5781 i.types[op].bitfield.imm16 = 1;
5782 i.types[op].bitfield.imm32 = 1;
5783 i.types[op].bitfield.imm32s = 1;
5784 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
5785 break;
5786 case BYTE_MNEM_SUFFIX:
40fb9820
L
5787 i.types[op].bitfield.imm8 = 1;
5788 i.types[op].bitfield.imm8s = 1;
5789 i.types[op].bitfield.imm16 = 1;
5790 i.types[op].bitfield.imm32 = 1;
5791 i.types[op].bitfield.imm32s = 1;
5792 i.types[op].bitfield.imm64 = 1;
29b0f896 5793 break;
252b5132 5794 }
252b5132 5795
29b0f896
AM
5796 /* If this operand is at most 16 bits, convert it
5797 to a signed 16 bit number before trying to see
5798 whether it will fit in an even smaller size.
5799 This allows a 16-bit operand such as $0xffe0 to
5800 be recognised as within Imm8S range. */
40fb9820 5801 if ((i.types[op].bitfield.imm16)
7e96fb68 5802 && fits_in_unsigned_word (i.op[op].imms->X_add_number))
252b5132 5803 {
87ed972d
JB
5804 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
5805 ^ 0x8000) - 0x8000);
29b0f896 5806 }
a28def75
L
5807#ifdef BFD64
5808 /* Store 32-bit immediate in 64-bit for 64-bit BFD. */
40fb9820 5809 if ((i.types[op].bitfield.imm32)
7e96fb68 5810 && fits_in_unsigned_long (i.op[op].imms->X_add_number))
29b0f896
AM
5811 {
5812 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
5813 ^ ((offsetT) 1 << 31))
5814 - ((offsetT) 1 << 31));
5815 }
a28def75 5816#endif
40fb9820 5817 i.types[op]
c6fb90c8
L
5818 = operand_type_or (i.types[op],
5819 smallest_imm_type (i.op[op].imms->X_add_number));
252b5132 5820
29b0f896
AM
5821 /* We must avoid matching of Imm32 templates when 64bit
5822 only immediate is available. */
5823 if (guess_suffix == QWORD_MNEM_SUFFIX)
40fb9820 5824 i.types[op].bitfield.imm32 = 0;
29b0f896 5825 break;
252b5132 5826
29b0f896
AM
5827 case O_absent:
5828 case O_register:
5829 abort ();
5830
5831 /* Symbols and expressions. */
5832 default:
9cd96992
JB
5833 /* Convert symbolic operand to proper sizes for matching, but don't
5834 prevent matching a set of insns that only supports sizes other
5835 than those matching the insn suffix. */
5836 {
40fb9820 5837 i386_operand_type mask, allowed;
87ed972d 5838 const insn_template *t = current_templates->start;
9cd96992 5839
0dfbf9d7 5840 operand_type_set (&mask, 0);
9cd96992
JB
5841 switch (guess_suffix)
5842 {
5843 case QWORD_MNEM_SUFFIX:
40fb9820
L
5844 mask.bitfield.imm64 = 1;
5845 mask.bitfield.imm32s = 1;
9cd96992
JB
5846 break;
5847 case LONG_MNEM_SUFFIX:
40fb9820 5848 mask.bitfield.imm32 = 1;
9cd96992
JB
5849 break;
5850 case WORD_MNEM_SUFFIX:
40fb9820 5851 mask.bitfield.imm16 = 1;
9cd96992
JB
5852 break;
5853 case BYTE_MNEM_SUFFIX:
40fb9820 5854 mask.bitfield.imm8 = 1;
9cd96992
JB
5855 break;
5856 default:
9cd96992
JB
5857 break;
5858 }
8f0212ac
JB
5859
5860 allowed = operand_type_and (t->operand_types[op], mask);
5861 while (++t < current_templates->end)
5862 {
5863 allowed = operand_type_or (allowed, t->operand_types[op]);
5864 allowed = operand_type_and (allowed, mask);
5865 }
5866
0dfbf9d7 5867 if (!operand_type_all_zero (&allowed))
c6fb90c8 5868 i.types[op] = operand_type_and (i.types[op], mask);
9cd96992 5869 }
29b0f896 5870 break;
252b5132 5871 }
29b0f896
AM
5872 }
5873}
47926f60 5874
29b0f896
AM
5875/* Try to use the smallest displacement type too. */
5876static void
e3bb37b5 5877optimize_disp (void)
29b0f896
AM
5878{
5879 int op;
3e73aa7c 5880
29b0f896 5881 for (op = i.operands; --op >= 0;)
40fb9820 5882 if (operand_type_check (i.types[op], disp))
252b5132 5883 {
b300c311 5884 if (i.op[op].disps->X_op == O_constant)
252b5132 5885 {
91d6fa6a 5886 offsetT op_disp = i.op[op].disps->X_add_number;
29b0f896 5887
91d6fa6a 5888 if (!op_disp && i.types[op].bitfield.baseindex)
b300c311 5889 {
2f2be86b
JB
5890 i.types[op] = operand_type_and_not (i.types[op], anydisp);
5891 i.op[op].disps = NULL;
b300c311 5892 i.disp_operands--;
f185acdd
JB
5893 continue;
5894 }
5895
5896 if (i.types[op].bitfield.disp16
cd613c1f 5897 && fits_in_unsigned_word (op_disp))
f185acdd
JB
5898 {
5899 /* If this operand is at most 16 bits, convert
5900 to a signed 16 bit number and don't use 64bit
5901 displacement. */
5902 op_disp = ((op_disp ^ 0x8000) - 0x8000);
5903 i.types[op].bitfield.disp64 = 0;
b300c311 5904 }
f185acdd 5905
28a167a4 5906#ifdef BFD64
a50187b2 5907 /* Optimize 64-bit displacement to 32-bit for 64-bit BFD. */
a775efc8
JB
5908 if ((flag_code != CODE_64BIT
5909 ? i.types[op].bitfield.disp32
5910 : want_disp32 (current_templates->start)
5911 && (!current_templates->start->opcode_modifier.jump
5912 || i.jumpabsolute || i.types[op].bitfield.baseindex))
a50187b2 5913 && fits_in_unsigned_long (op_disp))
b300c311 5914 {
a50187b2
JB
5915 /* If this operand is at most 32 bits, convert
5916 to a signed 32 bit number and don't use 64bit
5917 displacement. */
5918 op_disp = (op_disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
5919 i.types[op].bitfield.disp64 = 0;
5920 i.types[op].bitfield.disp32 = 1;
5921 }
28a167a4 5922
a50187b2
JB
5923 if (flag_code == CODE_64BIT && fits_in_signed_long (op_disp))
5924 {
5925 i.types[op].bitfield.disp64 = 0;
a775efc8 5926 i.types[op].bitfield.disp32 = 1;
b300c311 5927 }
28a167a4 5928#endif
40fb9820 5929 if ((i.types[op].bitfield.disp32
40fb9820 5930 || i.types[op].bitfield.disp16)
b5014f7a 5931 && fits_in_disp8 (op_disp))
40fb9820 5932 i.types[op].bitfield.disp8 = 1;
77c59789
JB
5933
5934 i.op[op].disps->X_add_number = op_disp;
252b5132 5935 }
67a4f2b7
AO
5936 else if (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
5937 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL)
5938 {
5939 fix_new_exp (frag_now, frag_more (0) - frag_now->fr_literal, 0,
5940 i.op[op].disps, 0, i.reloc[op]);
2f2be86b 5941 i.types[op] = operand_type_and_not (i.types[op], anydisp);
67a4f2b7
AO
5942 }
5943 else
b300c311 5944 /* We only support 64bit displacement on constants. */
40fb9820 5945 i.types[op].bitfield.disp64 = 0;
252b5132 5946 }
29b0f896
AM
5947}
5948
4a1b91ea
L
5949/* Return 1 if there is a match in broadcast bytes between operand
5950 GIVEN and instruction template T. */
5951
5952static INLINE int
5953match_broadcast_size (const insn_template *t, unsigned int given)
5954{
5955 return ((t->opcode_modifier.broadcast == BYTE_BROADCAST
5956 && i.types[given].bitfield.byte)
5957 || (t->opcode_modifier.broadcast == WORD_BROADCAST
5958 && i.types[given].bitfield.word)
5959 || (t->opcode_modifier.broadcast == DWORD_BROADCAST
5960 && i.types[given].bitfield.dword)
5961 || (t->opcode_modifier.broadcast == QWORD_BROADCAST
5962 && i.types[given].bitfield.qword));
5963}
5964
6c30d220
L
5965/* Check if operands are valid for the instruction. */
5966
5967static int
5968check_VecOperands (const insn_template *t)
5969{
43234a1e 5970 unsigned int op;
e2195274 5971 i386_cpu_flags cpu;
e2195274
JB
5972
5973 /* Templates allowing for ZMMword as well as YMMword and/or XMMword for
5974 any one operand are implicity requiring AVX512VL support if the actual
5975 operand size is YMMword or XMMword. Since this function runs after
5976 template matching, there's no need to check for YMMword/XMMword in
5977 the template. */
5978 cpu = cpu_flags_and (t->cpu_flags, avx512);
5979 if (!cpu_flags_all_zero (&cpu)
5980 && !t->cpu_flags.bitfield.cpuavx512vl
5981 && !cpu_arch_flags.bitfield.cpuavx512vl)
5982 {
5983 for (op = 0; op < t->operands; ++op)
5984 {
5985 if (t->operand_types[op].bitfield.zmmword
5986 && (i.types[op].bitfield.ymmword
5987 || i.types[op].bitfield.xmmword))
5988 {
5989 i.error = unsupported;
5990 return 1;
5991 }
5992 }
5993 }
43234a1e 5994
22c36940
JB
5995 /* Somewhat similarly, templates specifying both AVX and AVX2 are
5996 requiring AVX2 support if the actual operand size is YMMword. */
5997 if (t->cpu_flags.bitfield.cpuavx
5998 && t->cpu_flags.bitfield.cpuavx2
5999 && !cpu_arch_flags.bitfield.cpuavx2)
6000 {
6001 for (op = 0; op < t->operands; ++op)
6002 {
6003 if (t->operand_types[op].bitfield.xmmword
6004 && i.types[op].bitfield.ymmword)
6005 {
6006 i.error = unsupported;
6007 return 1;
6008 }
6009 }
6010 }
6011
6c30d220 6012 /* Without VSIB byte, we can't have a vector register for index. */
63112cd6 6013 if (!t->opcode_modifier.sib
6c30d220 6014 && i.index_reg
1b54b8d7
JB
6015 && (i.index_reg->reg_type.bitfield.xmmword
6016 || i.index_reg->reg_type.bitfield.ymmword
6017 || i.index_reg->reg_type.bitfield.zmmword))
6c30d220
L
6018 {
6019 i.error = unsupported_vector_index_register;
6020 return 1;
6021 }
6022
ad8ecc81
MZ
6023 /* Check if default mask is allowed. */
6024 if (t->opcode_modifier.nodefmask
6225c532 6025 && (!i.mask.reg || i.mask.reg->reg_num == 0))
ad8ecc81
MZ
6026 {
6027 i.error = no_default_mask;
6028 return 1;
6029 }
6030
7bab8ab5
JB
6031 /* For VSIB byte, we need a vector register for index, and all vector
6032 registers must be distinct. */
260cd341 6033 if (t->opcode_modifier.sib && t->opcode_modifier.sib != SIBMEM)
7bab8ab5
JB
6034 {
6035 if (!i.index_reg
63112cd6 6036 || !((t->opcode_modifier.sib == VECSIB128
1b54b8d7 6037 && i.index_reg->reg_type.bitfield.xmmword)
63112cd6 6038 || (t->opcode_modifier.sib == VECSIB256
1b54b8d7 6039 && i.index_reg->reg_type.bitfield.ymmword)
63112cd6 6040 || (t->opcode_modifier.sib == VECSIB512
1b54b8d7 6041 && i.index_reg->reg_type.bitfield.zmmword)))
7bab8ab5
JB
6042 {
6043 i.error = invalid_vsib_address;
6044 return 1;
6045 }
6046
6225c532
JB
6047 gas_assert (i.reg_operands == 2 || i.mask.reg);
6048 if (i.reg_operands == 2 && !i.mask.reg)
43234a1e 6049 {
3528c362 6050 gas_assert (i.types[0].bitfield.class == RegSIMD);
1b54b8d7
JB
6051 gas_assert (i.types[0].bitfield.xmmword
6052 || i.types[0].bitfield.ymmword);
3528c362 6053 gas_assert (i.types[2].bitfield.class == RegSIMD);
1b54b8d7
JB
6054 gas_assert (i.types[2].bitfield.xmmword
6055 || i.types[2].bitfield.ymmword);
43234a1e
L
6056 if (operand_check == check_none)
6057 return 0;
6058 if (register_number (i.op[0].regs)
6059 != register_number (i.index_reg)
6060 && register_number (i.op[2].regs)
6061 != register_number (i.index_reg)
6062 && register_number (i.op[0].regs)
6063 != register_number (i.op[2].regs))
6064 return 0;
6065 if (operand_check == check_error)
6066 {
6067 i.error = invalid_vector_register_set;
6068 return 1;
6069 }
6070 as_warn (_("mask, index, and destination registers should be distinct"));
6071 }
6225c532 6072 else if (i.reg_operands == 1 && i.mask.reg)
8444f82a 6073 {
3528c362 6074 if (i.types[1].bitfield.class == RegSIMD
1b54b8d7
JB
6075 && (i.types[1].bitfield.xmmword
6076 || i.types[1].bitfield.ymmword
6077 || i.types[1].bitfield.zmmword)
8444f82a
MZ
6078 && (register_number (i.op[1].regs)
6079 == register_number (i.index_reg)))
6080 {
6081 if (operand_check == check_error)
6082 {
6083 i.error = invalid_vector_register_set;
6084 return 1;
6085 }
6086 if (operand_check != check_none)
6087 as_warn (_("index and destination registers should be distinct"));
6088 }
6089 }
43234a1e 6090 }
7bab8ab5 6091
fc141319
L
6092 /* For AMX instructions with 3 TMM register operands, all operands
6093 must be distinct. */
6094 if (i.reg_operands == 3
6095 && t->operand_types[0].bitfield.tmmword
6096 && (i.op[0].regs == i.op[1].regs
6097 || i.op[0].regs == i.op[2].regs
6098 || i.op[1].regs == i.op[2].regs))
6099 {
6100 i.error = invalid_tmm_register_set;
6101 return 1;
260cd341
LC
6102 }
6103
0cc78721
CL
6104 /* For some special instructions require that destination must be distinct
6105 from source registers. */
6106 if (t->opcode_modifier.distinctdest)
6107 {
6108 unsigned int dest_reg = i.operands - 1;
6109
6110 know (i.operands >= 3);
6111
6112 /* #UD if dest_reg == src1_reg or dest_reg == src2_reg. */
6113 if (i.op[dest_reg - 1].regs == i.op[dest_reg].regs
6114 || (i.reg_operands > 2
6115 && i.op[dest_reg - 2].regs == i.op[dest_reg].regs))
6116 {
6117 i.error = invalid_dest_and_src_register_set;
6118 return 1;
6119 }
6120 }
6121
43234a1e
L
6122 /* Check if broadcast is supported by the instruction and is applied
6123 to the memory operand. */
a5748e0d 6124 if (i.broadcast.type || i.broadcast.bytes)
43234a1e 6125 {
8e6e0792 6126 i386_operand_type type, overlap;
43234a1e
L
6127
6128 /* Check if specified broadcast is supported in this instruction,
4a1b91ea 6129 and its broadcast bytes match the memory operand. */
5273a3cd 6130 op = i.broadcast.operand;
8e6e0792 6131 if (!t->opcode_modifier.broadcast
c48dadc9 6132 || !(i.flags[op] & Operand_Mem)
c39e5b26 6133 || (!i.types[op].bitfield.unspecified
4a1b91ea 6134 && !match_broadcast_size (t, op)))
43234a1e
L
6135 {
6136 bad_broadcast:
6137 i.error = unsupported_broadcast;
6138 return 1;
6139 }
8e6e0792 6140
a5748e0d
JB
6141 if (i.broadcast.type)
6142 i.broadcast.bytes = ((1 << (t->opcode_modifier.broadcast - 1))
6143 * i.broadcast.type);
8e6e0792 6144 operand_type_set (&type, 0);
a5748e0d 6145 switch (get_broadcast_bytes (t, false))
8e6e0792 6146 {
4a1b91ea
L
6147 case 2:
6148 type.bitfield.word = 1;
6149 break;
6150 case 4:
6151 type.bitfield.dword = 1;
6152 break;
8e6e0792
JB
6153 case 8:
6154 type.bitfield.qword = 1;
6155 break;
6156 case 16:
6157 type.bitfield.xmmword = 1;
6158 break;
6159 case 32:
6160 type.bitfield.ymmword = 1;
6161 break;
6162 case 64:
6163 type.bitfield.zmmword = 1;
6164 break;
6165 default:
6166 goto bad_broadcast;
6167 }
6168
6169 overlap = operand_type_and (type, t->operand_types[op]);
bc49bfd8
JB
6170 if (t->operand_types[op].bitfield.class == RegSIMD
6171 && t->operand_types[op].bitfield.byte
6172 + t->operand_types[op].bitfield.word
6173 + t->operand_types[op].bitfield.dword
6174 + t->operand_types[op].bitfield.qword > 1)
6175 {
6176 overlap.bitfield.xmmword = 0;
6177 overlap.bitfield.ymmword = 0;
6178 overlap.bitfield.zmmword = 0;
6179 }
8e6e0792
JB
6180 if (operand_type_all_zero (&overlap))
6181 goto bad_broadcast;
6182
6183 if (t->opcode_modifier.checkregsize)
6184 {
6185 unsigned int j;
6186
e2195274 6187 type.bitfield.baseindex = 1;
8e6e0792
JB
6188 for (j = 0; j < i.operands; ++j)
6189 {
6190 if (j != op
6191 && !operand_type_register_match(i.types[j],
6192 t->operand_types[j],
6193 type,
6194 t->operand_types[op]))
6195 goto bad_broadcast;
6196 }
6197 }
43234a1e
L
6198 }
6199 /* If broadcast is supported in this instruction, we need to check if
6200 operand of one-element size isn't specified without broadcast. */
6201 else if (t->opcode_modifier.broadcast && i.mem_operands)
6202 {
6203 /* Find memory operand. */
6204 for (op = 0; op < i.operands; op++)
8dc0818e 6205 if (i.flags[op] & Operand_Mem)
43234a1e
L
6206 break;
6207 gas_assert (op < i.operands);
6208 /* Check size of the memory operand. */
4a1b91ea 6209 if (match_broadcast_size (t, op))
43234a1e
L
6210 {
6211 i.error = broadcast_needed;
6212 return 1;
6213 }
6214 }
c39e5b26
JB
6215 else
6216 op = MAX_OPERANDS - 1; /* Avoid uninitialized variable warning. */
43234a1e
L
6217
6218 /* Check if requested masking is supported. */
6225c532 6219 if (i.mask.reg)
43234a1e 6220 {
ae2387fe
JB
6221 switch (t->opcode_modifier.masking)
6222 {
6223 case BOTH_MASKING:
6224 break;
6225 case MERGING_MASKING:
6225c532 6226 if (i.mask.zeroing)
ae2387fe
JB
6227 {
6228 case 0:
6229 i.error = unsupported_masking;
6230 return 1;
6231 }
6232 break;
6233 case DYNAMIC_MASKING:
6234 /* Memory destinations allow only merging masking. */
6225c532 6235 if (i.mask.zeroing && i.mem_operands)
ae2387fe
JB
6236 {
6237 /* Find memory operand. */
6238 for (op = 0; op < i.operands; op++)
c48dadc9 6239 if (i.flags[op] & Operand_Mem)
ae2387fe
JB
6240 break;
6241 gas_assert (op < i.operands);
6242 if (op == i.operands - 1)
6243 {
6244 i.error = unsupported_masking;
6245 return 1;
6246 }
6247 }
6248 break;
6249 default:
6250 abort ();
6251 }
43234a1e
L
6252 }
6253
6254 /* Check if masking is applied to dest operand. */
6225c532 6255 if (i.mask.reg && (i.mask.operand != i.operands - 1))
43234a1e
L
6256 {
6257 i.error = mask_not_on_destination;
6258 return 1;
6259 }
6260
43234a1e 6261 /* Check RC/SAE. */
ca5312a2 6262 if (i.rounding.type != rc_none)
43234a1e 6263 {
a80195f1 6264 if (!t->opcode_modifier.sae
cf665fee
JB
6265 || ((i.rounding.type != saeonly) != t->opcode_modifier.staticrounding)
6266 || i.mem_operands)
43234a1e
L
6267 {
6268 i.error = unsupported_rc_sae;
6269 return 1;
6270 }
cf665fee
JB
6271
6272 /* Non-EVEX.LIG forms need to have a ZMM register as at least one
6273 operand. */
6274 if (t->opcode_modifier.evex != EVEXLIG)
7bab8ab5 6275 {
cf665fee
JB
6276 for (op = 0; op < t->operands; ++op)
6277 if (i.types[op].bitfield.zmmword)
6278 break;
6279 if (op >= t->operands)
6280 {
6281 i.error = operand_size_mismatch;
6282 return 1;
6283 }
7bab8ab5 6284 }
6c30d220
L
6285 }
6286
da4977e0
JB
6287 /* Check the special Imm4 cases; must be the first operand. */
6288 if (t->cpu_flags.bitfield.cpuxop && t->operands == 5)
6289 {
6290 if (i.op[0].imms->X_op != O_constant
6291 || !fits_in_imm4 (i.op[0].imms->X_add_number))
6292 {
6293 i.error = bad_imm4;
6294 return 1;
6295 }
6296
6297 /* Turn off Imm<N> so that update_imm won't complain. */
6298 operand_type_set (&i.types[0], 0);
6299 }
6300
43234a1e 6301 /* Check vector Disp8 operand. */
b5014f7a 6302 if (t->opcode_modifier.disp8memshift
1a42a9fe 6303 && i.disp_encoding <= disp_encoding_8bit)
43234a1e 6304 {
a5748e0d 6305 if (i.broadcast.bytes)
4a1b91ea 6306 i.memshift = t->opcode_modifier.broadcast - 1;
7091c612 6307 else if (t->opcode_modifier.disp8memshift != DISP8_SHIFT_VL)
43234a1e 6308 i.memshift = t->opcode_modifier.disp8memshift;
7091c612
JB
6309 else
6310 {
125ff819 6311 const i386_operand_type *type = NULL, *fallback = NULL;
7091c612
JB
6312
6313 i.memshift = 0;
6314 for (op = 0; op < i.operands; op++)
8dc0818e 6315 if (i.flags[op] & Operand_Mem)
7091c612 6316 {
4174bfff
JB
6317 if (t->opcode_modifier.evex == EVEXLIG)
6318 i.memshift = 2 + (i.suffix == QWORD_MNEM_SUFFIX);
6319 else if (t->operand_types[op].bitfield.xmmword
6320 + t->operand_types[op].bitfield.ymmword
6321 + t->operand_types[op].bitfield.zmmword <= 1)
7091c612
JB
6322 type = &t->operand_types[op];
6323 else if (!i.types[op].bitfield.unspecified)
6324 type = &i.types[op];
125ff819
JB
6325 else /* Ambiguities get resolved elsewhere. */
6326 fallback = &t->operand_types[op];
7091c612 6327 }
3528c362 6328 else if (i.types[op].bitfield.class == RegSIMD
4174bfff 6329 && t->opcode_modifier.evex != EVEXLIG)
7091c612
JB
6330 {
6331 if (i.types[op].bitfield.zmmword)
6332 i.memshift = 6;
6333 else if (i.types[op].bitfield.ymmword && i.memshift < 5)
6334 i.memshift = 5;
6335 else if (i.types[op].bitfield.xmmword && i.memshift < 4)
6336 i.memshift = 4;
6337 }
6338
125ff819
JB
6339 if (!type && !i.memshift)
6340 type = fallback;
7091c612
JB
6341 if (type)
6342 {
6343 if (type->bitfield.zmmword)
6344 i.memshift = 6;
6345 else if (type->bitfield.ymmword)
6346 i.memshift = 5;
6347 else if (type->bitfield.xmmword)
6348 i.memshift = 4;
6349 }
6350
6351 /* For the check in fits_in_disp8(). */
6352 if (i.memshift == 0)
6353 i.memshift = -1;
6354 }
43234a1e
L
6355
6356 for (op = 0; op < i.operands; op++)
6357 if (operand_type_check (i.types[op], disp)
6358 && i.op[op].disps->X_op == O_constant)
6359 {
b5014f7a 6360 if (fits_in_disp8 (i.op[op].disps->X_add_number))
43234a1e 6361 {
b5014f7a
JB
6362 i.types[op].bitfield.disp8 = 1;
6363 return 0;
43234a1e 6364 }
b5014f7a 6365 i.types[op].bitfield.disp8 = 0;
43234a1e
L
6366 }
6367 }
b5014f7a
JB
6368
6369 i.memshift = 0;
43234a1e 6370
6c30d220
L
6371 return 0;
6372}
6373
da4977e0 6374/* Check if encoding requirements are met by the instruction. */
a683cc34
SP
6375
6376static int
da4977e0 6377VEX_check_encoding (const insn_template *t)
a683cc34 6378{
da4977e0
JB
6379 if (i.vec_encoding == vex_encoding_error)
6380 {
6381 i.error = unsupported;
6382 return 1;
6383 }
6384
86fa6981 6385 if (i.vec_encoding == vex_encoding_evex)
43234a1e 6386 {
86fa6981 6387 /* This instruction must be encoded with EVEX prefix. */
e771e7c9 6388 if (!is_evex_encoding (t))
86fa6981
L
6389 {
6390 i.error = unsupported;
6391 return 1;
6392 }
6393 return 0;
43234a1e
L
6394 }
6395
a683cc34 6396 if (!t->opcode_modifier.vex)
86fa6981
L
6397 {
6398 /* This instruction template doesn't have VEX prefix. */
6399 if (i.vec_encoding != vex_encoding_default)
6400 {
6401 i.error = unsupported;
6402 return 1;
6403 }
6404 return 0;
6405 }
a683cc34 6406
a683cc34
SP
6407 return 0;
6408}
6409
7b94647a
JB
6410/* Helper function for the progress() macro in match_template(). */
6411static INLINE enum i386_error progress (enum i386_error new,
6412 enum i386_error last,
6413 unsigned int line, unsigned int *line_p)
6414{
6415 if (line <= *line_p)
6416 return last;
6417 *line_p = line;
6418 return new;
6419}
6420
d3ce72d0 6421static const insn_template *
83b16ac6 6422match_template (char mnem_suffix)
29b0f896
AM
6423{
6424 /* Points to template once we've found it. */
d3ce72d0 6425 const insn_template *t;
40fb9820 6426 i386_operand_type overlap0, overlap1, overlap2, overlap3;
c0f3af97 6427 i386_operand_type overlap4;
29b0f896 6428 unsigned int found_reverse_match;
dc2be329 6429 i386_opcode_modifier suffix_check;
40fb9820 6430 i386_operand_type operand_types [MAX_OPERANDS];
539e75ad 6431 int addr_prefix_disp;
7b94647a
JB
6432 unsigned int j, size_match, check_register, errline = __LINE__;
6433 enum i386_error specific_error = number_of_operands_mismatch;
6434#define progress(err) progress (err, specific_error, __LINE__, &errline)
29b0f896 6435
c0f3af97
L
6436#if MAX_OPERANDS != 5
6437# error "MAX_OPERANDS must be 5."
f48ff2ae
L
6438#endif
6439
29b0f896 6440 found_reverse_match = 0;
539e75ad 6441 addr_prefix_disp = -1;
40fb9820 6442
dc2be329 6443 /* Prepare for mnemonic suffix check. */
40fb9820 6444 memset (&suffix_check, 0, sizeof (suffix_check));
dc2be329
L
6445 switch (mnem_suffix)
6446 {
6447 case BYTE_MNEM_SUFFIX:
6448 suffix_check.no_bsuf = 1;
6449 break;
6450 case WORD_MNEM_SUFFIX:
6451 suffix_check.no_wsuf = 1;
6452 break;
6453 case SHORT_MNEM_SUFFIX:
6454 suffix_check.no_ssuf = 1;
6455 break;
6456 case LONG_MNEM_SUFFIX:
6457 suffix_check.no_lsuf = 1;
6458 break;
6459 case QWORD_MNEM_SUFFIX:
6460 suffix_check.no_qsuf = 1;
6461 break;
6462 default:
6463 /* NB: In Intel syntax, normally we can check for memory operand
6464 size when there is no mnemonic suffix. But jmp and call have
6465 2 different encodings with Dword memory operand size, one with
6466 No_ldSuf and the other without. i.suffix is set to
6467 LONG_DOUBLE_MNEM_SUFFIX to skip the one with No_ldSuf. */
6468 if (i.suffix == LONG_DOUBLE_MNEM_SUFFIX)
6469 suffix_check.no_ldsuf = 1;
83b16ac6
JB
6470 }
6471
45aa61fe 6472 for (t = current_templates->start; t < current_templates->end; t++)
29b0f896 6473 {
539e75ad 6474 addr_prefix_disp = -1;
dbbc8b7e 6475 found_reverse_match = 0;
539e75ad 6476
7b94647a 6477 /* Must have right number of operands. */
29b0f896
AM
6478 if (i.operands != t->operands)
6479 continue;
6480
50aecf8c 6481 /* Check processor support. */
7b94647a 6482 specific_error = progress (unsupported);
45a4bb20 6483 if (cpu_flags_match (t) != CPU_FLAGS_PERFECT_MATCH)
50aecf8c
L
6484 continue;
6485
e1d4d893 6486 /* Check AT&T mnemonic. */
7b94647a 6487 specific_error = progress (unsupported_with_intel_mnemonic);
e1d4d893 6488 if (intel_mnemonic && t->opcode_modifier.attmnemonic)
1efbbeb4
L
6489 continue;
6490
4b5aaf5f 6491 /* Check AT&T/Intel syntax. */
7b94647a 6492 specific_error = progress (unsupported_syntax);
5c07affc 6493 if ((intel_syntax && t->opcode_modifier.attsyntax)
4b5aaf5f 6494 || (!intel_syntax && t->opcode_modifier.intelsyntax))
1efbbeb4
L
6495 continue;
6496
4b5aaf5f
L
6497 /* Check Intel64/AMD64 ISA. */
6498 switch (isa64)
6499 {
6500 default:
6501 /* Default: Don't accept Intel64. */
6502 if (t->opcode_modifier.isa64 == INTEL64)
6503 continue;
6504 break;
6505 case amd64:
6506 /* -mamd64: Don't accept Intel64 and Intel64 only. */
6507 if (t->opcode_modifier.isa64 >= INTEL64)
6508 continue;
6509 break;
6510 case intel64:
6511 /* -mintel64: Don't accept AMD64. */
5990e377 6512 if (t->opcode_modifier.isa64 == AMD64 && flag_code == CODE_64BIT)
4b5aaf5f
L
6513 continue;
6514 break;
6515 }
6516
dc2be329 6517 /* Check the suffix. */
7b94647a 6518 specific_error = progress (invalid_instruction_suffix);
dc2be329
L
6519 if ((t->opcode_modifier.no_bsuf && suffix_check.no_bsuf)
6520 || (t->opcode_modifier.no_wsuf && suffix_check.no_wsuf)
6521 || (t->opcode_modifier.no_lsuf && suffix_check.no_lsuf)
6522 || (t->opcode_modifier.no_ssuf && suffix_check.no_ssuf)
6523 || (t->opcode_modifier.no_qsuf && suffix_check.no_qsuf)
6524 || (t->opcode_modifier.no_ldsuf && suffix_check.no_ldsuf))
83b16ac6 6525 continue;
29b0f896 6526
7b94647a 6527 specific_error = progress (operand_size_mismatch);
3ac21baa
JB
6528 size_match = operand_size_match (t);
6529 if (!size_match)
7d5e4556 6530 continue;
539e75ad 6531
6f2f06be
JB
6532 /* This is intentionally not
6533
0cfa3eb3 6534 if (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE))
6f2f06be
JB
6535
6536 as the case of a missing * on the operand is accepted (perhaps with
6537 a warning, issued further down). */
7b94647a 6538 specific_error = progress (operand_type_mismatch);
0cfa3eb3 6539 if (i.jumpabsolute && t->opcode_modifier.jump != JUMP_ABSOLUTE)
7b94647a 6540 continue;
6f2f06be 6541
5c07affc
L
6542 for (j = 0; j < MAX_OPERANDS; j++)
6543 operand_types[j] = t->operand_types[j];
6544
e365e234
JB
6545 /* In general, don't allow
6546 - 64-bit operands outside of 64-bit mode,
6547 - 32-bit operands on pre-386. */
7b94647a
JB
6548 specific_error = progress (mnem_suffix ? invalid_instruction_suffix
6549 : operand_size_mismatch);
4873e243 6550 j = i.imm_operands + (t->operands > i.imm_operands + 1);
e365e234
JB
6551 if (((i.suffix == QWORD_MNEM_SUFFIX
6552 && flag_code != CODE_64BIT
389d00a5
JB
6553 && !(t->opcode_modifier.opcodespace == SPACE_0F
6554 && t->base_opcode == 0xc7
5e74b495 6555 && t->opcode_modifier.opcodeprefix == PREFIX_NONE
8b65b895 6556 && t->extension_opcode == 1) /* cmpxchg8b */)
e365e234
JB
6557 || (i.suffix == LONG_MNEM_SUFFIX
6558 && !cpu_arch_flags.bitfield.cpui386))
45aa61fe 6559 && (intel_syntax
3cd7f3e3 6560 ? (t->opcode_modifier.mnemonicsize != IGNORESIZE
45aa61fe
AM
6561 && !intel_float_operand (t->name))
6562 : intel_float_operand (t->name) != 2)
4873e243
JB
6563 && (t->operands == i.imm_operands
6564 || (operand_types[i.imm_operands].bitfield.class != RegMMX
6565 && operand_types[i.imm_operands].bitfield.class != RegSIMD
6566 && operand_types[i.imm_operands].bitfield.class != RegMask)
6567 || (operand_types[j].bitfield.class != RegMMX
6568 && operand_types[j].bitfield.class != RegSIMD
6569 && operand_types[j].bitfield.class != RegMask))
63112cd6 6570 && !t->opcode_modifier.sib)
192dc9c6
JB
6571 continue;
6572
29b0f896 6573 /* Do not verify operands when there are none. */
e365e234 6574 if (!t->operands)
da4977e0
JB
6575 {
6576 if (VEX_check_encoding (t))
6577 {
7b94647a 6578 specific_error = progress (i.error);
da4977e0
JB
6579 continue;
6580 }
6581
6582 /* We've found a match; break out of loop. */
6583 break;
6584 }
252b5132 6585
48bcea9f
JB
6586 if (!t->opcode_modifier.jump
6587 || t->opcode_modifier.jump == JUMP_ABSOLUTE)
6588 {
6589 /* There should be only one Disp operand. */
6590 for (j = 0; j < MAX_OPERANDS; j++)
6591 if (operand_type_check (operand_types[j], disp))
539e75ad 6592 break;
48bcea9f
JB
6593 if (j < MAX_OPERANDS)
6594 {
5b7c81bd 6595 bool override = (i.prefix[ADDR_PREFIX] != 0);
48bcea9f
JB
6596
6597 addr_prefix_disp = j;
6598
a775efc8
JB
6599 /* Address size prefix will turn Disp64 operand into Disp32 and
6600 Disp32/Disp16 one into Disp16/Disp32 respectively. */
48bcea9f 6601 switch (flag_code)
40fb9820 6602 {
48bcea9f
JB
6603 case CODE_16BIT:
6604 override = !override;
6605 /* Fall through. */
6606 case CODE_32BIT:
6607 if (operand_types[j].bitfield.disp32
6608 && operand_types[j].bitfield.disp16)
40fb9820 6609 {
48bcea9f
JB
6610 operand_types[j].bitfield.disp16 = override;
6611 operand_types[j].bitfield.disp32 = !override;
40fb9820 6612 }
a775efc8 6613 gas_assert (!operand_types[j].bitfield.disp64);
48bcea9f
JB
6614 break;
6615
6616 case CODE_64BIT:
a775efc8 6617 if (operand_types[j].bitfield.disp64)
40fb9820 6618 {
a775efc8 6619 gas_assert (!operand_types[j].bitfield.disp32);
48bcea9f 6620 operand_types[j].bitfield.disp32 = override;
a775efc8 6621 operand_types[j].bitfield.disp64 = !override;
40fb9820 6622 }
48bcea9f
JB
6623 operand_types[j].bitfield.disp16 = 0;
6624 break;
40fb9820 6625 }
539e75ad 6626 }
48bcea9f 6627 }
539e75ad 6628
d7e3e627
L
6629 switch (i.reloc[0])
6630 {
6631 case BFD_RELOC_386_GOT32:
6632 /* Force 0x8b encoding for "mov foo@GOT, %eax". */
6633 if (t->base_opcode == 0xa0
6634 && t->opcode_modifier.opcodespace == SPACE_BASE)
6635 continue;
6636 break;
6637 case BFD_RELOC_386_TLS_GOTIE:
6638 case BFD_RELOC_386_TLS_LE_32:
6639 case BFD_RELOC_X86_64_GOTTPOFF:
6640 case BFD_RELOC_X86_64_TLSLD:
6641 /* Don't allow KMOV in TLS code sequences. */
6642 if (t->opcode_modifier.vex)
6643 continue;
6644 break;
6645 default:
6646 break;
6647 }
02a86693 6648
56ffb741 6649 /* We check register size if needed. */
e2195274
JB
6650 if (t->opcode_modifier.checkregsize)
6651 {
6652 check_register = (1 << t->operands) - 1;
a5748e0d 6653 if (i.broadcast.type || i.broadcast.bytes)
5273a3cd 6654 check_register &= ~(1 << i.broadcast.operand);
e2195274
JB
6655 }
6656 else
6657 check_register = 0;
6658
c6fb90c8 6659 overlap0 = operand_type_and (i.types[0], operand_types[0]);
29b0f896
AM
6660 switch (t->operands)
6661 {
6662 case 1:
40fb9820 6663 if (!operand_type_match (overlap0, i.types[0]))
29b0f896
AM
6664 continue;
6665 break;
6666 case 2:
33eaf5de 6667 /* xchg %eax, %eax is a special case. It is an alias for nop
8b38ad71
L
6668 only in 32bit mode and we can use opcode 0x90. In 64bit
6669 mode, we can't use 0x90 for xchg %eax, %eax since it should
6670 zero-extend %eax to %rax. */
6671 if (flag_code == CODE_64BIT
6672 && t->base_opcode == 0x90
35648716 6673 && t->opcode_modifier.opcodespace == SPACE_BASE
75e5731b
JB
6674 && i.types[0].bitfield.instance == Accum
6675 && i.types[0].bitfield.dword
6676 && i.types[1].bitfield.instance == Accum
6677 && i.types[1].bitfield.dword)
8b38ad71 6678 continue;
1212781b
JB
6679 /* xrelease mov %eax, <disp> is another special case. It must not
6680 match the accumulator-only encoding of mov. */
6681 if (flag_code != CODE_64BIT
6682 && i.hle_prefix
6683 && t->base_opcode == 0xa0
35648716 6684 && t->opcode_modifier.opcodespace == SPACE_BASE
75e5731b 6685 && i.types[0].bitfield.instance == Accum
8dc0818e 6686 && (i.flags[1] & Operand_Mem))
1212781b 6687 continue;
f5eb1d70
JB
6688 /* Fall through. */
6689
6690 case 3:
3ac21baa
JB
6691 if (!(size_match & MATCH_STRAIGHT))
6692 goto check_reverse;
64c49ab3
JB
6693 /* Reverse direction of operands if swapping is possible in the first
6694 place (operands need to be symmetric) and
6695 - the load form is requested, and the template is a store form,
6696 - the store form is requested, and the template is a load form,
6697 - the non-default (swapped) form is requested. */
6698 overlap1 = operand_type_and (operand_types[0], operand_types[1]);
f5eb1d70 6699 if (t->opcode_modifier.d && i.reg_operands == i.operands
64c49ab3
JB
6700 && !operand_type_all_zero (&overlap1))
6701 switch (i.dir_encoding)
6702 {
6703 case dir_encoding_load:
6704 if (operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 6705 || t->opcode_modifier.regmem)
64c49ab3
JB
6706 goto check_reverse;
6707 break;
6708
6709 case dir_encoding_store:
6710 if (!operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 6711 && !t->opcode_modifier.regmem)
64c49ab3
JB
6712 goto check_reverse;
6713 break;
6714
6715 case dir_encoding_swap:
6716 goto check_reverse;
6717
6718 case dir_encoding_default:
6719 break;
6720 }
86fa6981 6721 /* If we want store form, we skip the current load. */
64c49ab3
JB
6722 if ((i.dir_encoding == dir_encoding_store
6723 || i.dir_encoding == dir_encoding_swap)
86fa6981
L
6724 && i.mem_operands == 0
6725 && t->opcode_modifier.load)
fa99fab2 6726 continue;
1a0670f3 6727 /* Fall through. */
f48ff2ae 6728 case 4:
c0f3af97 6729 case 5:
c6fb90c8 6730 overlap1 = operand_type_and (i.types[1], operand_types[1]);
40fb9820
L
6731 if (!operand_type_match (overlap0, i.types[0])
6732 || !operand_type_match (overlap1, i.types[1])
e2195274 6733 || ((check_register & 3) == 3
dc821c5f 6734 && !operand_type_register_match (i.types[0],
40fb9820 6735 operand_types[0],
dc821c5f 6736 i.types[1],
40fb9820 6737 operand_types[1])))
29b0f896 6738 {
7b94647a
JB
6739 specific_error = progress (i.error);
6740
29b0f896 6741 /* Check if other direction is valid ... */
38e314eb 6742 if (!t->opcode_modifier.d)
29b0f896
AM
6743 continue;
6744
dc1e8a47 6745 check_reverse:
3ac21baa
JB
6746 if (!(size_match & MATCH_REVERSE))
6747 continue;
29b0f896 6748 /* Try reversing direction of operands. */
8bd915b7
JB
6749 j = t->opcode_modifier.vexsources ? 1 : i.operands - 1;
6750 overlap0 = operand_type_and (i.types[0], operand_types[j]);
6751 overlap1 = operand_type_and (i.types[j], operand_types[0]);
c975cec5
JB
6752 overlap2 = operand_type_and (i.types[1], operand_types[1]);
6753 gas_assert (t->operands != 3 || !check_register);
40fb9820 6754 if (!operand_type_match (overlap0, i.types[0])
8bd915b7 6755 || !operand_type_match (overlap1, i.types[j])
c975cec5
JB
6756 || (t->operands == 3
6757 && !operand_type_match (overlap2, i.types[1]))
45664ddb 6758 || (check_register
dc821c5f 6759 && !operand_type_register_match (i.types[0],
8bd915b7
JB
6760 operand_types[j],
6761 i.types[j],
45664ddb 6762 operand_types[0])))
29b0f896
AM
6763 {
6764 /* Does not match either direction. */
7b94647a 6765 specific_error = progress (i.error);
29b0f896
AM
6766 continue;
6767 }
38e314eb 6768 /* found_reverse_match holds which of D or FloatR
29b0f896 6769 we've found. */
38e314eb
JB
6770 if (!t->opcode_modifier.d)
6771 found_reverse_match = 0;
6772 else if (operand_types[0].bitfield.tbyte)
8a2ed489 6773 found_reverse_match = Opcode_FloatD;
8bd915b7
JB
6774 else if (t->opcode_modifier.vexsources)
6775 {
6776 found_reverse_match = Opcode_VexW;
6777 goto check_operands_345;
6778 }
2c735193
JB
6779 else if (t->opcode_modifier.opcodespace != SPACE_BASE
6780 && (t->opcode_modifier.opcodespace != SPACE_0F
6781 /* MOV to/from CR/DR/TR, as an exception, follow
6782 the base opcode space encoding model. */
6783 || (t->base_opcode | 7) != 0x27))
dbbc8b7e 6784 found_reverse_match = (t->base_opcode & 0xee) != 0x6e
2c735193 6785 ? Opcode_ExtD : Opcode_SIMD_IntD;
8a2ed489 6786 else
38e314eb 6787 found_reverse_match = Opcode_D;
40fb9820 6788 if (t->opcode_modifier.floatr)
8a2ed489 6789 found_reverse_match |= Opcode_FloatR;
29b0f896 6790 }
f48ff2ae 6791 else
29b0f896 6792 {
f48ff2ae 6793 /* Found a forward 2 operand match here. */
8bd915b7 6794 check_operands_345:
d1cbb4db
L
6795 switch (t->operands)
6796 {
c0f3af97 6797 case 5:
3d0738af 6798 overlap4 = operand_type_and (i.types[4], operand_types[4]);
c0f3af97 6799 if (!operand_type_match (overlap4, i.types[4])
dc821c5f 6800 || !operand_type_register_match (i.types[3],
c0f3af97 6801 operand_types[3],
c0f3af97
L
6802 i.types[4],
6803 operand_types[4]))
7b94647a
JB
6804 {
6805 specific_error = progress (i.error);
6806 continue;
6807 }
1a0670f3 6808 /* Fall through. */
f48ff2ae 6809 case 4:
3d0738af 6810 overlap3 = operand_type_and (i.types[3], operand_types[3]);
40fb9820 6811 if (!operand_type_match (overlap3, i.types[3])
e2195274
JB
6812 || ((check_register & 0xa) == 0xa
6813 && !operand_type_register_match (i.types[1],
f7768225
JB
6814 operand_types[1],
6815 i.types[3],
e2195274
JB
6816 operand_types[3]))
6817 || ((check_register & 0xc) == 0xc
6818 && !operand_type_register_match (i.types[2],
6819 operand_types[2],
6820 i.types[3],
6821 operand_types[3])))
7b94647a
JB
6822 {
6823 specific_error = progress (i.error);
6824 continue;
6825 }
1a0670f3 6826 /* Fall through. */
f48ff2ae 6827 case 3:
3d0738af 6828 overlap2 = operand_type_and (i.types[2], operand_types[2]);
40fb9820 6829 if (!operand_type_match (overlap2, i.types[2])
e2195274
JB
6830 || ((check_register & 5) == 5
6831 && !operand_type_register_match (i.types[0],
23e42951
JB
6832 operand_types[0],
6833 i.types[2],
e2195274
JB
6834 operand_types[2]))
6835 || ((check_register & 6) == 6
6836 && !operand_type_register_match (i.types[1],
6837 operand_types[1],
6838 i.types[2],
6839 operand_types[2])))
7b94647a
JB
6840 {
6841 specific_error = progress (i.error);
6842 continue;
6843 }
f48ff2ae
L
6844 break;
6845 }
29b0f896 6846 }
f48ff2ae 6847 /* Found either forward/reverse 2, 3 or 4 operand match here:
29b0f896
AM
6848 slip through to break. */
6849 }
c0f3af97 6850
9bb4d860
L
6851 /* Check if VEX/EVEX encoding requirements can be satisfied. */
6852 if (VEX_check_encoding (t))
da4977e0 6853 {
7b94647a 6854 specific_error = progress (i.error);
da4977e0
JB
6855 continue;
6856 }
6857
9bb4d860
L
6858 /* Check if vector operands are valid. */
6859 if (check_VecOperands (t))
5614d22c 6860 {
7b94647a 6861 specific_error = progress (i.error);
5614d22c
JB
6862 continue;
6863 }
a683cc34 6864
29b0f896
AM
6865 /* We've found a match; break out of loop. */
6866 break;
6867 }
6868
7b94647a
JB
6869#undef progress
6870
29b0f896
AM
6871 if (t == current_templates->end)
6872 {
6873 /* We found no match. */
a65babc9 6874 const char *err_msg;
7b94647a 6875 switch (specific_error)
a65babc9
L
6876 {
6877 default:
6878 abort ();
86e026a4 6879 case operand_size_mismatch:
a65babc9
L
6880 err_msg = _("operand size mismatch");
6881 break;
6882 case operand_type_mismatch:
6883 err_msg = _("operand type mismatch");
6884 break;
6885 case register_type_mismatch:
6886 err_msg = _("register type mismatch");
6887 break;
6888 case number_of_operands_mismatch:
6889 err_msg = _("number of operands mismatch");
6890 break;
6891 case invalid_instruction_suffix:
6892 err_msg = _("invalid instruction suffix");
6893 break;
6894 case bad_imm4:
4a2608e3 6895 err_msg = _("constant doesn't fit in 4 bits");
a65babc9 6896 break;
a65babc9
L
6897 case unsupported_with_intel_mnemonic:
6898 err_msg = _("unsupported with Intel mnemonic");
6899 break;
6900 case unsupported_syntax:
6901 err_msg = _("unsupported syntax");
6902 break;
6903 case unsupported:
35262a23 6904 as_bad (_("unsupported instruction `%s'"),
10efe3f6
L
6905 current_templates->start->name);
6906 return NULL;
260cd341
LC
6907 case invalid_sib_address:
6908 err_msg = _("invalid SIB address");
6909 break;
6c30d220
L
6910 case invalid_vsib_address:
6911 err_msg = _("invalid VSIB address");
6912 break;
7bab8ab5
JB
6913 case invalid_vector_register_set:
6914 err_msg = _("mask, index, and destination registers must be distinct");
6915 break;
260cd341
LC
6916 case invalid_tmm_register_set:
6917 err_msg = _("all tmm registers must be distinct");
6918 break;
0cc78721
CL
6919 case invalid_dest_and_src_register_set:
6920 err_msg = _("destination and source registers must be distinct");
6921 break;
6c30d220
L
6922 case unsupported_vector_index_register:
6923 err_msg = _("unsupported vector index register");
6924 break;
43234a1e
L
6925 case unsupported_broadcast:
6926 err_msg = _("unsupported broadcast");
6927 break;
43234a1e
L
6928 case broadcast_needed:
6929 err_msg = _("broadcast is needed for operand of such type");
6930 break;
6931 case unsupported_masking:
6932 err_msg = _("unsupported masking");
6933 break;
6934 case mask_not_on_destination:
6935 err_msg = _("mask not on destination operand");
6936 break;
6937 case no_default_mask:
6938 err_msg = _("default mask isn't allowed");
6939 break;
6940 case unsupported_rc_sae:
6941 err_msg = _("unsupported static rounding/sae");
6942 break;
43234a1e
L
6943 case invalid_register_operand:
6944 err_msg = _("invalid register operand");
6945 break;
a65babc9
L
6946 }
6947 as_bad (_("%s for `%s'"), err_msg,
891edac4 6948 current_templates->start->name);
fa99fab2 6949 return NULL;
29b0f896 6950 }
252b5132 6951
29b0f896
AM
6952 if (!quiet_warnings)
6953 {
6954 if (!intel_syntax
0cfa3eb3 6955 && (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE)))
6f2f06be 6956 as_warn (_("indirect %s without `*'"), t->name);
29b0f896 6957
40fb9820 6958 if (t->opcode_modifier.isprefix
3cd7f3e3 6959 && t->opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
6960 {
6961 /* Warn them that a data or address size prefix doesn't
6962 affect assembly of the next line of code. */
6963 as_warn (_("stand-alone `%s' prefix"), t->name);
6964 }
6965 }
6966
6967 /* Copy the template we found. */
9a182d04 6968 install_template (t);
539e75ad
L
6969
6970 if (addr_prefix_disp != -1)
6971 i.tm.operand_types[addr_prefix_disp]
6972 = operand_types[addr_prefix_disp];
6973
8bd915b7 6974 switch (found_reverse_match)
29b0f896 6975 {
8bd915b7
JB
6976 case 0:
6977 break;
6978
6979 default:
dfd69174
JB
6980 /* If we found a reverse match we must alter the opcode direction
6981 bit and clear/flip the regmem modifier one. found_reverse_match
6982 holds bits to change (different for int & float insns). */
29b0f896
AM
6983
6984 i.tm.base_opcode ^= found_reverse_match;
6985
f5eb1d70
JB
6986 i.tm.operand_types[0] = operand_types[i.operands - 1];
6987 i.tm.operand_types[i.operands - 1] = operand_types[0];
dfd69174
JB
6988
6989 /* Certain SIMD insns have their load forms specified in the opcode
6990 table, and hence we need to _set_ RegMem instead of clearing it.
6991 We need to avoid setting the bit though on insns like KMOVW. */
6992 i.tm.opcode_modifier.regmem
6993 = i.tm.opcode_modifier.modrm && i.tm.opcode_modifier.d
6994 && i.tm.operands > 2U - i.tm.opcode_modifier.sse2avx
6995 && !i.tm.opcode_modifier.regmem;
8bd915b7
JB
6996 break;
6997
6998 case Opcode_VexW:
6999 /* Only the first two register operands need reversing, alongside
7000 flipping VEX.W. */
7001 i.tm.opcode_modifier.vexw ^= VEXW0 ^ VEXW1;
7002
7003 j = i.tm.operand_types[0].bitfield.imm8;
7004 i.tm.operand_types[j] = operand_types[j + 1];
7005 i.tm.operand_types[j + 1] = operand_types[j];
7006 break;
29b0f896
AM
7007 }
7008
fa99fab2 7009 return t;
29b0f896
AM
7010}
7011
7012static int
e3bb37b5 7013check_string (void)
29b0f896 7014{
51c8edf6
JB
7015 unsigned int es_op = i.tm.opcode_modifier.isstring - IS_STRING_ES_OP0;
7016 unsigned int op = i.tm.operand_types[0].bitfield.baseindex ? es_op : 0;
8dc0818e 7017
5e042380 7018 if (i.seg[op] != NULL && i.seg[op] != reg_es)
29b0f896 7019 {
51c8edf6
JB
7020 as_bad (_("`%s' operand %u must use `%ses' segment"),
7021 i.tm.name,
7022 intel_syntax ? i.tm.operands - es_op : es_op + 1,
7023 register_prefix);
7024 return 0;
29b0f896 7025 }
51c8edf6
JB
7026
7027 /* There's only ever one segment override allowed per instruction.
7028 This instruction possibly has a legal segment override on the
7029 second operand, so copy the segment to where non-string
7030 instructions store it, allowing common code. */
7031 i.seg[op] = i.seg[1];
7032
29b0f896
AM
7033 return 1;
7034}
7035
7036static int
543613e9 7037process_suffix (void)
29b0f896 7038{
5b7c81bd 7039 bool is_crc32 = false, is_movx = false;
8b65b895 7040
29b0f896
AM
7041 /* If matched instruction specifies an explicit instruction mnemonic
7042 suffix, use it. */
673fe0f0 7043 if (i.tm.opcode_modifier.size == SIZE16)
40fb9820 7044 i.suffix = WORD_MNEM_SUFFIX;
673fe0f0 7045 else if (i.tm.opcode_modifier.size == SIZE32)
40fb9820 7046 i.suffix = LONG_MNEM_SUFFIX;
673fe0f0 7047 else if (i.tm.opcode_modifier.size == SIZE64)
40fb9820 7048 i.suffix = QWORD_MNEM_SUFFIX;
13e600d0 7049 else if (i.reg_operands
c8f8eebc
JB
7050 && (i.operands > 1 || i.types[0].bitfield.class == Reg)
7051 && !i.tm.opcode_modifier.addrprefixopreg)
29b0f896 7052 {
65fca059 7053 unsigned int numop = i.operands;
389d00a5
JB
7054
7055 /* MOVSX/MOVZX */
7056 is_movx = (i.tm.opcode_modifier.opcodespace == SPACE_0F
7057 && (i.tm.base_opcode | 8) == 0xbe)
7058 || (i.tm.opcode_modifier.opcodespace == SPACE_BASE
7059 && i.tm.base_opcode == 0x63
7060 && i.tm.cpu_flags.bitfield.cpu64);
7061
8b65b895 7062 /* CRC32 */
389d00a5
JB
7063 is_crc32 = (i.tm.base_opcode == 0xf0
7064 && i.tm.opcode_modifier.opcodespace == SPACE_0F38
8b65b895 7065 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2);
65fca059
JB
7066
7067 /* movsx/movzx want only their source operand considered here, for the
7068 ambiguity checking below. The suffix will be replaced afterwards
7069 to represent the destination (register). */
389d00a5 7070 if (is_movx && (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63))
65fca059
JB
7071 --i.operands;
7072
643bb870 7073 /* crc32 needs REX.W set regardless of suffix / source operand size. */
8b65b895 7074 if (is_crc32 && i.tm.operand_types[1].bitfield.qword)
643bb870
JB
7075 i.rex |= REX_W;
7076
29b0f896 7077 /* If there's no instruction mnemonic suffix we try to invent one
13e600d0 7078 based on GPR operands. */
29b0f896
AM
7079 if (!i.suffix)
7080 {
7081 /* We take i.suffix from the last register operand specified,
7082 Destination register type is more significant than source
381d071f
L
7083 register type. crc32 in SSE4.2 prefers source register
7084 type. */
8b65b895 7085 unsigned int op = is_crc32 ? 1 : i.operands;
20592a94 7086
1a035124
JB
7087 while (op--)
7088 if (i.tm.operand_types[op].bitfield.instance == InstanceNone
7089 || i.tm.operand_types[op].bitfield.instance == Accum)
7090 {
7091 if (i.types[op].bitfield.class != Reg)
7092 continue;
7093 if (i.types[op].bitfield.byte)
7094 i.suffix = BYTE_MNEM_SUFFIX;
7095 else if (i.types[op].bitfield.word)
7096 i.suffix = WORD_MNEM_SUFFIX;
7097 else if (i.types[op].bitfield.dword)
7098 i.suffix = LONG_MNEM_SUFFIX;
7099 else if (i.types[op].bitfield.qword)
7100 i.suffix = QWORD_MNEM_SUFFIX;
7101 else
7102 continue;
7103 break;
7104 }
65fca059
JB
7105
7106 /* As an exception, movsx/movzx silently default to a byte source
7107 in AT&T mode. */
389d00a5 7108 if (is_movx && i.tm.opcode_modifier.w && !i.suffix && !intel_syntax)
65fca059 7109 i.suffix = BYTE_MNEM_SUFFIX;
29b0f896
AM
7110 }
7111 else if (i.suffix == BYTE_MNEM_SUFFIX)
7112 {
1cb0ab18 7113 if (!check_byte_reg ())
29b0f896
AM
7114 return 0;
7115 }
7116 else if (i.suffix == LONG_MNEM_SUFFIX)
7117 {
1cb0ab18 7118 if (!check_long_reg ())
29b0f896
AM
7119 return 0;
7120 }
7121 else if (i.suffix == QWORD_MNEM_SUFFIX)
7122 {
1cb0ab18 7123 if (!check_qword_reg ())
29b0f896
AM
7124 return 0;
7125 }
7126 else if (i.suffix == WORD_MNEM_SUFFIX)
7127 {
1cb0ab18 7128 if (!check_word_reg ())
29b0f896
AM
7129 return 0;
7130 }
3cd7f3e3
L
7131 else if (intel_syntax
7132 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
7133 /* Do nothing if the instruction is going to ignore the prefix. */
7134 ;
7135 else
7136 abort ();
65fca059
JB
7137
7138 /* Undo the movsx/movzx change done above. */
7139 i.operands = numop;
29b0f896 7140 }
3cd7f3e3
L
7141 else if (i.tm.opcode_modifier.mnemonicsize == DEFAULTSIZE
7142 && !i.suffix)
29b0f896 7143 {
13e600d0
JB
7144 i.suffix = stackop_size;
7145 if (stackop_size == LONG_MNEM_SUFFIX)
06f74c5c
L
7146 {
7147 /* stackop_size is set to LONG_MNEM_SUFFIX for the
7148 .code16gcc directive to support 16-bit mode with
7149 32-bit address. For IRET without a suffix, generate
7150 16-bit IRET (opcode 0xcf) to return from an interrupt
7151 handler. */
13e600d0
JB
7152 if (i.tm.base_opcode == 0xcf)
7153 {
7154 i.suffix = WORD_MNEM_SUFFIX;
7155 as_warn (_("generating 16-bit `iret' for .code16gcc directive"));
7156 }
7157 /* Warn about changed behavior for segment register push/pop. */
7158 else if ((i.tm.base_opcode | 1) == 0x07)
7159 as_warn (_("generating 32-bit `%s', unlike earlier gas versions"),
7160 i.tm.name);
06f74c5c 7161 }
29b0f896 7162 }
c006a730 7163 else if (!i.suffix
0cfa3eb3
JB
7164 && (i.tm.opcode_modifier.jump == JUMP_ABSOLUTE
7165 || i.tm.opcode_modifier.jump == JUMP_BYTE
7166 || i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT
389d00a5
JB
7167 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
7168 && i.tm.base_opcode == 0x01 /* [ls][gi]dt */
64e74474 7169 && i.tm.extension_opcode <= 3)))
9306ca4a
JB
7170 {
7171 switch (flag_code)
7172 {
7173 case CODE_64BIT:
40fb9820 7174 if (!i.tm.opcode_modifier.no_qsuf)
9306ca4a 7175 {
828c2a25
JB
7176 if (i.tm.opcode_modifier.jump == JUMP_BYTE
7177 || i.tm.opcode_modifier.no_lsuf)
7178 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a
JB
7179 break;
7180 }
1a0670f3 7181 /* Fall through. */
9306ca4a 7182 case CODE_32BIT:
40fb9820 7183 if (!i.tm.opcode_modifier.no_lsuf)
9306ca4a
JB
7184 i.suffix = LONG_MNEM_SUFFIX;
7185 break;
7186 case CODE_16BIT:
40fb9820 7187 if (!i.tm.opcode_modifier.no_wsuf)
9306ca4a
JB
7188 i.suffix = WORD_MNEM_SUFFIX;
7189 break;
7190 }
7191 }
252b5132 7192
c006a730 7193 if (!i.suffix
3cd7f3e3 7194 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8
JB
7195 /* Also cover lret/retf/iret in 64-bit mode. */
7196 || (flag_code == CODE_64BIT
7197 && !i.tm.opcode_modifier.no_lsuf
7198 && !i.tm.opcode_modifier.no_qsuf))
3cd7f3e3 7199 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
8bbb3ad8
JB
7200 /* Explicit sizing prefixes are assumed to disambiguate insns. */
7201 && !i.prefix[DATA_PREFIX] && !(i.prefix[REX_PREFIX] & REX_W)
62b3f548
JB
7202 /* Accept FLDENV et al without suffix. */
7203 && (i.tm.opcode_modifier.no_ssuf || i.tm.opcode_modifier.floatmf))
29b0f896 7204 {
6c0946d0 7205 unsigned int suffixes, evex = 0;
c006a730
JB
7206
7207 suffixes = !i.tm.opcode_modifier.no_bsuf;
7208 if (!i.tm.opcode_modifier.no_wsuf)
7209 suffixes |= 1 << 1;
7210 if (!i.tm.opcode_modifier.no_lsuf)
7211 suffixes |= 1 << 2;
7212 if (!i.tm.opcode_modifier.no_ldsuf)
7213 suffixes |= 1 << 3;
7214 if (!i.tm.opcode_modifier.no_ssuf)
7215 suffixes |= 1 << 4;
7216 if (flag_code == CODE_64BIT && !i.tm.opcode_modifier.no_qsuf)
7217 suffixes |= 1 << 5;
7218
6c0946d0
JB
7219 /* For [XYZ]MMWORD operands inspect operand sizes. While generally
7220 also suitable for AT&T syntax mode, it was requested that this be
7221 restricted to just Intel syntax. */
a5748e0d
JB
7222 if (intel_syntax && is_any_vex_encoding (&i.tm)
7223 && !i.broadcast.type && !i.broadcast.bytes)
6c0946d0 7224 {
b9915cbc 7225 unsigned int op;
6c0946d0 7226
b9915cbc 7227 for (op = 0; op < i.tm.operands; ++op)
6c0946d0 7228 {
b9915cbc
JB
7229 if (is_evex_encoding (&i.tm)
7230 && !cpu_arch_flags.bitfield.cpuavx512vl)
6c0946d0 7231 {
b9915cbc
JB
7232 if (i.tm.operand_types[op].bitfield.ymmword)
7233 i.tm.operand_types[op].bitfield.xmmword = 0;
7234 if (i.tm.operand_types[op].bitfield.zmmword)
7235 i.tm.operand_types[op].bitfield.ymmword = 0;
7236 if (!i.tm.opcode_modifier.evex
7237 || i.tm.opcode_modifier.evex == EVEXDYN)
7238 i.tm.opcode_modifier.evex = EVEX512;
7239 }
6c0946d0 7240
b9915cbc
JB
7241 if (i.tm.operand_types[op].bitfield.xmmword
7242 + i.tm.operand_types[op].bitfield.ymmword
7243 + i.tm.operand_types[op].bitfield.zmmword < 2)
7244 continue;
6c0946d0 7245
b9915cbc
JB
7246 /* Any properly sized operand disambiguates the insn. */
7247 if (i.types[op].bitfield.xmmword
7248 || i.types[op].bitfield.ymmword
7249 || i.types[op].bitfield.zmmword)
7250 {
7251 suffixes &= ~(7 << 6);
7252 evex = 0;
7253 break;
7254 }
6c0946d0 7255
b9915cbc
JB
7256 if ((i.flags[op] & Operand_Mem)
7257 && i.tm.operand_types[op].bitfield.unspecified)
7258 {
7259 if (i.tm.operand_types[op].bitfield.xmmword)
7260 suffixes |= 1 << 6;
7261 if (i.tm.operand_types[op].bitfield.ymmword)
7262 suffixes |= 1 << 7;
7263 if (i.tm.operand_types[op].bitfield.zmmword)
7264 suffixes |= 1 << 8;
7265 if (is_evex_encoding (&i.tm))
7266 evex = EVEX512;
6c0946d0
JB
7267 }
7268 }
7269 }
7270
7271 /* Are multiple suffixes / operand sizes allowed? */
c006a730 7272 if (suffixes & (suffixes - 1))
9306ca4a 7273 {
873494c8 7274 if (intel_syntax
3cd7f3e3 7275 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8 7276 || operand_check == check_error))
9306ca4a 7277 {
c006a730 7278 as_bad (_("ambiguous operand size for `%s'"), i.tm.name);
9306ca4a
JB
7279 return 0;
7280 }
c006a730 7281 if (operand_check == check_error)
9306ca4a 7282 {
c006a730
JB
7283 as_bad (_("no instruction mnemonic suffix given and "
7284 "no register operands; can't size `%s'"), i.tm.name);
9306ca4a
JB
7285 return 0;
7286 }
c006a730 7287 if (operand_check == check_warning)
873494c8
JB
7288 as_warn (_("%s; using default for `%s'"),
7289 intel_syntax
7290 ? _("ambiguous operand size")
7291 : _("no instruction mnemonic suffix given and "
7292 "no register operands"),
7293 i.tm.name);
c006a730
JB
7294
7295 if (i.tm.opcode_modifier.floatmf)
7296 i.suffix = SHORT_MNEM_SUFFIX;
389d00a5 7297 else if (is_movx)
65fca059 7298 /* handled below */;
6c0946d0
JB
7299 else if (evex)
7300 i.tm.opcode_modifier.evex = evex;
c006a730
JB
7301 else if (flag_code == CODE_16BIT)
7302 i.suffix = WORD_MNEM_SUFFIX;
1a035124 7303 else if (!i.tm.opcode_modifier.no_lsuf)
c006a730 7304 i.suffix = LONG_MNEM_SUFFIX;
1a035124
JB
7305 else
7306 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a 7307 }
29b0f896 7308 }
252b5132 7309
389d00a5 7310 if (is_movx)
65fca059
JB
7311 {
7312 /* In Intel syntax, movsx/movzx must have a "suffix" (checked above).
7313 In AT&T syntax, if there is no suffix (warned about above), the default
7314 will be byte extension. */
7315 if (i.tm.opcode_modifier.w && i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
7316 i.tm.base_opcode |= 1;
7317
7318 /* For further processing, the suffix should represent the destination
7319 (register). This is already the case when one was used with
7320 mov[sz][bw]*, but we need to replace it for mov[sz]x, or if there was
7321 no suffix to begin with. */
7322 if (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63 || !i.suffix)
7323 {
7324 if (i.types[1].bitfield.word)
7325 i.suffix = WORD_MNEM_SUFFIX;
7326 else if (i.types[1].bitfield.qword)
7327 i.suffix = QWORD_MNEM_SUFFIX;
7328 else
7329 i.suffix = LONG_MNEM_SUFFIX;
7330
7331 i.tm.opcode_modifier.w = 0;
7332 }
7333 }
7334
50128d0c
JB
7335 if (!i.tm.opcode_modifier.modrm && i.reg_operands && i.tm.operands < 3)
7336 i.short_form = (i.tm.operand_types[0].bitfield.class == Reg)
7337 != (i.tm.operand_types[1].bitfield.class == Reg);
7338
d2224064
JB
7339 /* Change the opcode based on the operand size given by i.suffix. */
7340 switch (i.suffix)
29b0f896 7341 {
d2224064
JB
7342 /* Size floating point instruction. */
7343 case LONG_MNEM_SUFFIX:
7344 if (i.tm.opcode_modifier.floatmf)
7345 {
7346 i.tm.base_opcode ^= 4;
7347 break;
7348 }
7349 /* fall through */
7350 case WORD_MNEM_SUFFIX:
7351 case QWORD_MNEM_SUFFIX:
29b0f896 7352 /* It's not a byte, select word/dword operation. */
40fb9820 7353 if (i.tm.opcode_modifier.w)
29b0f896 7354 {
50128d0c 7355 if (i.short_form)
29b0f896
AM
7356 i.tm.base_opcode |= 8;
7357 else
7358 i.tm.base_opcode |= 1;
7359 }
d2224064
JB
7360 /* fall through */
7361 case SHORT_MNEM_SUFFIX:
29b0f896
AM
7362 /* Now select between word & dword operations via the operand
7363 size prefix, except for instructions that will ignore this
7364 prefix anyway. */
c8f8eebc 7365 if (i.suffix != QWORD_MNEM_SUFFIX
3cd7f3e3 7366 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
c8f8eebc
JB
7367 && !i.tm.opcode_modifier.floatmf
7368 && !is_any_vex_encoding (&i.tm)
7369 && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
7370 || (flag_code == CODE_64BIT
7371 && i.tm.opcode_modifier.jump == JUMP_BYTE)))
24eab124
AM
7372 {
7373 unsigned int prefix = DATA_PREFIX_OPCODE;
543613e9 7374
0cfa3eb3 7375 if (i.tm.opcode_modifier.jump == JUMP_BYTE) /* jcxz, loop */
29b0f896 7376 prefix = ADDR_PREFIX_OPCODE;
252b5132 7377
29b0f896
AM
7378 if (!add_prefix (prefix))
7379 return 0;
24eab124 7380 }
252b5132 7381
29b0f896
AM
7382 /* Set mode64 for an operand. */
7383 if (i.suffix == QWORD_MNEM_SUFFIX
9146926a 7384 && flag_code == CODE_64BIT
d2224064 7385 && !i.tm.opcode_modifier.norex64
4ed21b58 7386 && !i.tm.opcode_modifier.vexw
46e883c5 7387 /* Special case for xchg %rax,%rax. It is NOP and doesn't
d2224064
JB
7388 need rex64. */
7389 && ! (i.operands == 2
7390 && i.tm.base_opcode == 0x90
7391 && i.tm.extension_opcode == None
75e5731b
JB
7392 && i.types[0].bitfield.instance == Accum
7393 && i.types[0].bitfield.qword
7394 && i.types[1].bitfield.instance == Accum
7395 && i.types[1].bitfield.qword))
d2224064 7396 i.rex |= REX_W;
3e73aa7c 7397
d2224064 7398 break;
8bbb3ad8
JB
7399
7400 case 0:
f9a6a8f0 7401 /* Select word/dword/qword operation with explicit data sizing prefix
8bbb3ad8
JB
7402 when there are no suitable register operands. */
7403 if (i.tm.opcode_modifier.w
7404 && (i.prefix[DATA_PREFIX] || (i.prefix[REX_PREFIX] & REX_W))
7405 && (!i.reg_operands
7406 || (i.reg_operands == 1
7407 /* ShiftCount */
7408 && (i.tm.operand_types[0].bitfield.instance == RegC
7409 /* InOutPortReg */
7410 || i.tm.operand_types[0].bitfield.instance == RegD
7411 || i.tm.operand_types[1].bitfield.instance == RegD
7412 /* CRC32 */
8b65b895 7413 || is_crc32))))
8bbb3ad8
JB
7414 i.tm.base_opcode |= 1;
7415 break;
29b0f896 7416 }
7ecd2f8b 7417
c8f8eebc 7418 if (i.tm.opcode_modifier.addrprefixopreg)
c0a30a9f 7419 {
c8f8eebc
JB
7420 gas_assert (!i.suffix);
7421 gas_assert (i.reg_operands);
c0a30a9f 7422
c8f8eebc
JB
7423 if (i.tm.operand_types[0].bitfield.instance == Accum
7424 || i.operands == 1)
7425 {
7426 /* The address size override prefix changes the size of the
7427 first operand. */
7428 if (flag_code == CODE_64BIT
7429 && i.op[0].regs->reg_type.bitfield.word)
7430 {
7431 as_bad (_("16-bit addressing unavailable for `%s'"),
7432 i.tm.name);
7433 return 0;
7434 }
7435
7436 if ((flag_code == CODE_32BIT
7437 ? i.op[0].regs->reg_type.bitfield.word
7438 : i.op[0].regs->reg_type.bitfield.dword)
7439 && !add_prefix (ADDR_PREFIX_OPCODE))
7440 return 0;
7441 }
c0a30a9f
L
7442 else
7443 {
c8f8eebc
JB
7444 /* Check invalid register operand when the address size override
7445 prefix changes the size of register operands. */
7446 unsigned int op;
7447 enum { need_word, need_dword, need_qword } need;
7448
27f13469 7449 /* Check the register operand for the address size prefix if
b3a3496f 7450 the memory operand has no real registers, like symbol, DISP
829f3fe1 7451 or bogus (x32-only) symbol(%rip) when symbol(%eip) is meant. */
27f13469
L
7452 if (i.mem_operands == 1
7453 && i.reg_operands == 1
7454 && i.operands == 2
27f13469 7455 && i.types[1].bitfield.class == Reg
b3a3496f
L
7456 && (flag_code == CODE_32BIT
7457 ? i.op[1].regs->reg_type.bitfield.word
7458 : i.op[1].regs->reg_type.bitfield.dword)
7459 && ((i.base_reg == NULL && i.index_reg == NULL)
829f3fe1
JB
7460#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
7461 || (x86_elf_abi == X86_64_X32_ABI
7462 && i.base_reg
b3a3496f
L
7463 && i.base_reg->reg_num == RegIP
7464 && i.base_reg->reg_type.bitfield.qword))
829f3fe1
JB
7465#else
7466 || 0)
7467#endif
27f13469
L
7468 && !add_prefix (ADDR_PREFIX_OPCODE))
7469 return 0;
7470
c8f8eebc
JB
7471 if (flag_code == CODE_32BIT)
7472 need = i.prefix[ADDR_PREFIX] ? need_word : need_dword;
7473 else if (i.prefix[ADDR_PREFIX])
c0a30a9f
L
7474 need = need_dword;
7475 else
7476 need = flag_code == CODE_64BIT ? need_qword : need_word;
c0a30a9f 7477
c8f8eebc
JB
7478 for (op = 0; op < i.operands; op++)
7479 {
7480 if (i.types[op].bitfield.class != Reg)
7481 continue;
7482
7483 switch (need)
7484 {
7485 case need_word:
7486 if (i.op[op].regs->reg_type.bitfield.word)
7487 continue;
7488 break;
7489 case need_dword:
7490 if (i.op[op].regs->reg_type.bitfield.dword)
7491 continue;
7492 break;
7493 case need_qword:
7494 if (i.op[op].regs->reg_type.bitfield.qword)
7495 continue;
7496 break;
7497 }
7498
7499 as_bad (_("invalid register operand size for `%s'"),
7500 i.tm.name);
7501 return 0;
7502 }
7503 }
c0a30a9f
L
7504 }
7505
29b0f896
AM
7506 return 1;
7507}
3e73aa7c 7508
29b0f896 7509static int
543613e9 7510check_byte_reg (void)
29b0f896
AM
7511{
7512 int op;
543613e9 7513
29b0f896
AM
7514 for (op = i.operands; --op >= 0;)
7515 {
dc821c5f 7516 /* Skip non-register operands. */
bab6aec1 7517 if (i.types[op].bitfield.class != Reg)
dc821c5f
JB
7518 continue;
7519
29b0f896
AM
7520 /* If this is an eight bit register, it's OK. If it's the 16 or
7521 32 bit version of an eight bit register, we will just use the
7522 low portion, and that's OK too. */
dc821c5f 7523 if (i.types[op].bitfield.byte)
29b0f896
AM
7524 continue;
7525
5a819eb9 7526 /* I/O port address operands are OK too. */
75e5731b
JB
7527 if (i.tm.operand_types[op].bitfield.instance == RegD
7528 && i.tm.operand_types[op].bitfield.word)
5a819eb9
JB
7529 continue;
7530
9706160a 7531 /* crc32 only wants its source operand checked here. */
389d00a5
JB
7532 if (i.tm.base_opcode == 0xf0
7533 && i.tm.opcode_modifier.opcodespace == SPACE_0F38
8b65b895
L
7534 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2
7535 && op != 0)
9344ff29
L
7536 continue;
7537
29b0f896 7538 /* Any other register is bad. */
73c76375
JB
7539 as_bad (_("`%s%s' not allowed with `%s%c'"),
7540 register_prefix, i.op[op].regs->reg_name,
7541 i.tm.name, i.suffix);
7542 return 0;
29b0f896
AM
7543 }
7544 return 1;
7545}
7546
7547static int
e3bb37b5 7548check_long_reg (void)
29b0f896
AM
7549{
7550 int op;
7551
7552 for (op = i.operands; --op >= 0;)
dc821c5f 7553 /* Skip non-register operands. */
bab6aec1 7554 if (i.types[op].bitfield.class != Reg)
dc821c5f 7555 continue;
29b0f896
AM
7556 /* Reject eight bit registers, except where the template requires
7557 them. (eg. movzb) */
dc821c5f 7558 else if (i.types[op].bitfield.byte
bab6aec1 7559 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7560 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7561 && (i.tm.operand_types[op].bitfield.word
7562 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7563 {
a540244d
L
7564 as_bad (_("`%s%s' not allowed with `%s%c'"),
7565 register_prefix,
29b0f896
AM
7566 i.op[op].regs->reg_name,
7567 i.tm.name,
7568 i.suffix);
7569 return 0;
7570 }
be4c5e58
L
7571 /* Error if the e prefix on a general reg is missing. */
7572 else if (i.types[op].bitfield.word
bab6aec1 7573 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7574 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7575 && i.tm.operand_types[op].bitfield.dword)
29b0f896 7576 {
be4c5e58
L
7577 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7578 register_prefix, i.op[op].regs->reg_name,
7579 i.suffix);
7580 return 0;
252b5132 7581 }
e4630f71 7582 /* Warn if the r prefix on a general reg is present. */
dc821c5f 7583 else if (i.types[op].bitfield.qword
bab6aec1 7584 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7585 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7586 && i.tm.operand_types[op].bitfield.dword)
252b5132 7587 {
1cb0ab18
JB
7588 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7589 register_prefix, i.op[op].regs->reg_name, i.suffix);
7590 return 0;
29b0f896
AM
7591 }
7592 return 1;
7593}
252b5132 7594
29b0f896 7595static int
e3bb37b5 7596check_qword_reg (void)
29b0f896
AM
7597{
7598 int op;
252b5132 7599
29b0f896 7600 for (op = i.operands; --op >= 0; )
dc821c5f 7601 /* Skip non-register operands. */
bab6aec1 7602 if (i.types[op].bitfield.class != Reg)
dc821c5f 7603 continue;
29b0f896
AM
7604 /* Reject eight bit registers, except where the template requires
7605 them. (eg. movzb) */
dc821c5f 7606 else if (i.types[op].bitfield.byte
bab6aec1 7607 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7608 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7609 && (i.tm.operand_types[op].bitfield.word
7610 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7611 {
a540244d
L
7612 as_bad (_("`%s%s' not allowed with `%s%c'"),
7613 register_prefix,
29b0f896
AM
7614 i.op[op].regs->reg_name,
7615 i.tm.name,
7616 i.suffix);
7617 return 0;
7618 }
e4630f71 7619 /* Warn if the r prefix on a general reg is missing. */
dc821c5f
JB
7620 else if ((i.types[op].bitfield.word
7621 || i.types[op].bitfield.dword)
bab6aec1 7622 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7623 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7624 && i.tm.operand_types[op].bitfield.qword)
29b0f896
AM
7625 {
7626 /* Prohibit these changes in the 64bit mode, since the
7627 lowering is more complicated. */
1cb0ab18
JB
7628 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7629 register_prefix, i.op[op].regs->reg_name, i.suffix);
7630 return 0;
252b5132 7631 }
29b0f896
AM
7632 return 1;
7633}
252b5132 7634
29b0f896 7635static int
e3bb37b5 7636check_word_reg (void)
29b0f896
AM
7637{
7638 int op;
7639 for (op = i.operands; --op >= 0;)
dc821c5f 7640 /* Skip non-register operands. */
bab6aec1 7641 if (i.types[op].bitfield.class != Reg)
dc821c5f 7642 continue;
29b0f896
AM
7643 /* Reject eight bit registers, except where the template requires
7644 them. (eg. movzb) */
dc821c5f 7645 else if (i.types[op].bitfield.byte
bab6aec1 7646 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7647 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7648 && (i.tm.operand_types[op].bitfield.word
7649 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7650 {
a540244d
L
7651 as_bad (_("`%s%s' not allowed with `%s%c'"),
7652 register_prefix,
29b0f896
AM
7653 i.op[op].regs->reg_name,
7654 i.tm.name,
7655 i.suffix);
7656 return 0;
7657 }
9706160a
JB
7658 /* Error if the e or r prefix on a general reg is present. */
7659 else if ((i.types[op].bitfield.dword
dc821c5f 7660 || i.types[op].bitfield.qword)
bab6aec1 7661 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7662 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7663 && i.tm.operand_types[op].bitfield.word)
252b5132 7664 {
9706160a
JB
7665 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7666 register_prefix, i.op[op].regs->reg_name,
7667 i.suffix);
7668 return 0;
29b0f896
AM
7669 }
7670 return 1;
7671}
252b5132 7672
29b0f896 7673static int
40fb9820 7674update_imm (unsigned int j)
29b0f896 7675{
bc0844ae 7676 i386_operand_type overlap = i.types[j];
40fb9820
L
7677 if ((overlap.bitfield.imm8
7678 || overlap.bitfield.imm8s
7679 || overlap.bitfield.imm16
7680 || overlap.bitfield.imm32
7681 || overlap.bitfield.imm32s
7682 || overlap.bitfield.imm64)
0dfbf9d7
L
7683 && !operand_type_equal (&overlap, &imm8)
7684 && !operand_type_equal (&overlap, &imm8s)
7685 && !operand_type_equal (&overlap, &imm16)
7686 && !operand_type_equal (&overlap, &imm32)
7687 && !operand_type_equal (&overlap, &imm32s)
7688 && !operand_type_equal (&overlap, &imm64))
29b0f896
AM
7689 {
7690 if (i.suffix)
7691 {
40fb9820
L
7692 i386_operand_type temp;
7693
0dfbf9d7 7694 operand_type_set (&temp, 0);
7ab9ffdd 7695 if (i.suffix == BYTE_MNEM_SUFFIX)
40fb9820
L
7696 {
7697 temp.bitfield.imm8 = overlap.bitfield.imm8;
7698 temp.bitfield.imm8s = overlap.bitfield.imm8s;
7699 }
7700 else if (i.suffix == WORD_MNEM_SUFFIX)
7701 temp.bitfield.imm16 = overlap.bitfield.imm16;
7702 else if (i.suffix == QWORD_MNEM_SUFFIX)
7703 {
7704 temp.bitfield.imm64 = overlap.bitfield.imm64;
7705 temp.bitfield.imm32s = overlap.bitfield.imm32s;
7706 }
7707 else
7708 temp.bitfield.imm32 = overlap.bitfield.imm32;
7709 overlap = temp;
29b0f896 7710 }
0dfbf9d7
L
7711 else if (operand_type_equal (&overlap, &imm16_32_32s)
7712 || operand_type_equal (&overlap, &imm16_32)
7713 || operand_type_equal (&overlap, &imm16_32s))
29b0f896 7714 {
40fb9820 7715 if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
65da13b5 7716 overlap = imm16;
40fb9820 7717 else
65da13b5 7718 overlap = imm32s;
29b0f896 7719 }
8bbb3ad8
JB
7720 else if (i.prefix[REX_PREFIX] & REX_W)
7721 overlap = operand_type_and (overlap, imm32s);
7722 else if (i.prefix[DATA_PREFIX])
7723 overlap = operand_type_and (overlap,
7724 flag_code != CODE_16BIT ? imm16 : imm32);
0dfbf9d7
L
7725 if (!operand_type_equal (&overlap, &imm8)
7726 && !operand_type_equal (&overlap, &imm8s)
7727 && !operand_type_equal (&overlap, &imm16)
7728 && !operand_type_equal (&overlap, &imm32)
7729 && !operand_type_equal (&overlap, &imm32s)
7730 && !operand_type_equal (&overlap, &imm64))
29b0f896 7731 {
4eed87de
AM
7732 as_bad (_("no instruction mnemonic suffix given; "
7733 "can't determine immediate size"));
29b0f896
AM
7734 return 0;
7735 }
7736 }
40fb9820 7737 i.types[j] = overlap;
29b0f896 7738
40fb9820
L
7739 return 1;
7740}
7741
7742static int
7743finalize_imm (void)
7744{
bc0844ae 7745 unsigned int j, n;
29b0f896 7746
bc0844ae
L
7747 /* Update the first 2 immediate operands. */
7748 n = i.operands > 2 ? 2 : i.operands;
7749 if (n)
7750 {
7751 for (j = 0; j < n; j++)
7752 if (update_imm (j) == 0)
7753 return 0;
40fb9820 7754
bc0844ae
L
7755 /* The 3rd operand can't be immediate operand. */
7756 gas_assert (operand_type_check (i.types[2], imm) == 0);
7757 }
29b0f896
AM
7758
7759 return 1;
7760}
7761
7762static int
e3bb37b5 7763process_operands (void)
29b0f896
AM
7764{
7765 /* Default segment register this instruction will use for memory
7766 accesses. 0 means unknown. This is only for optimizing out
7767 unnecessary segment overrides. */
5e042380 7768 const reg_entry *default_seg = NULL;
29b0f896 7769
a5aeccd9
JB
7770 if (i.tm.opcode_modifier.sse2avx)
7771 {
7772 /* Legacy encoded insns allow explicit REX prefixes, so these prefixes
7773 need converting. */
7774 i.rex |= i.prefix[REX_PREFIX] & (REX_W | REX_R | REX_X | REX_B);
7775 i.prefix[REX_PREFIX] = 0;
7776 i.rex_encoding = 0;
7777 }
c423d21a
JB
7778 /* ImmExt should be processed after SSE2AVX. */
7779 else if (i.tm.opcode_modifier.immext)
7780 process_immext ();
a5aeccd9 7781
2426c15f 7782 if (i.tm.opcode_modifier.sse2avx && i.tm.opcode_modifier.vexvvvv)
29b0f896 7783 {
91d6fa6a
NC
7784 unsigned int dupl = i.operands;
7785 unsigned int dest = dupl - 1;
9fcfb3d7
L
7786 unsigned int j;
7787
c0f3af97 7788 /* The destination must be an xmm register. */
9c2799c2 7789 gas_assert (i.reg_operands
91d6fa6a 7790 && MAX_OPERANDS > dupl
7ab9ffdd 7791 && operand_type_equal (&i.types[dest], &regxmm));
c0f3af97 7792
75e5731b 7793 if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 7794 && i.tm.operand_types[0].bitfield.xmmword)
e2ec9d29 7795 {
8cd7925b 7796 if (i.tm.opcode_modifier.vexsources == VEX3SOURCES)
c0f3af97
L
7797 {
7798 /* Keep xmm0 for instructions with VEX prefix and 3
7799 sources. */
75e5731b 7800 i.tm.operand_types[0].bitfield.instance = InstanceNone;
3528c362 7801 i.tm.operand_types[0].bitfield.class = RegSIMD;
c0f3af97
L
7802 goto duplicate;
7803 }
e2ec9d29 7804 else
c0f3af97
L
7805 {
7806 /* We remove the first xmm0 and keep the number of
7807 operands unchanged, which in fact duplicates the
7808 destination. */
7809 for (j = 1; j < i.operands; j++)
7810 {
7811 i.op[j - 1] = i.op[j];
7812 i.types[j - 1] = i.types[j];
7813 i.tm.operand_types[j - 1] = i.tm.operand_types[j];
8dc0818e 7814 i.flags[j - 1] = i.flags[j];
c0f3af97
L
7815 }
7816 }
7817 }
7818 else if (i.tm.opcode_modifier.implicit1stxmm0)
7ab9ffdd 7819 {
91d6fa6a 7820 gas_assert ((MAX_OPERANDS - 1) > dupl
8cd7925b
L
7821 && (i.tm.opcode_modifier.vexsources
7822 == VEX3SOURCES));
c0f3af97
L
7823
7824 /* Add the implicit xmm0 for instructions with VEX prefix
7825 and 3 sources. */
7826 for (j = i.operands; j > 0; j--)
7827 {
7828 i.op[j] = i.op[j - 1];
7829 i.types[j] = i.types[j - 1];
7830 i.tm.operand_types[j] = i.tm.operand_types[j - 1];
8dc0818e 7831 i.flags[j] = i.flags[j - 1];
c0f3af97
L
7832 }
7833 i.op[0].regs
629310ab 7834 = (const reg_entry *) str_hash_find (reg_hash, "xmm0");
7ab9ffdd 7835 i.types[0] = regxmm;
c0f3af97
L
7836 i.tm.operand_types[0] = regxmm;
7837
7838 i.operands += 2;
7839 i.reg_operands += 2;
7840 i.tm.operands += 2;
7841
91d6fa6a 7842 dupl++;
c0f3af97 7843 dest++;
91d6fa6a
NC
7844 i.op[dupl] = i.op[dest];
7845 i.types[dupl] = i.types[dest];
7846 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 7847 i.flags[dupl] = i.flags[dest];
e2ec9d29 7848 }
c0f3af97
L
7849 else
7850 {
dc1e8a47 7851 duplicate:
c0f3af97
L
7852 i.operands++;
7853 i.reg_operands++;
7854 i.tm.operands++;
7855
91d6fa6a
NC
7856 i.op[dupl] = i.op[dest];
7857 i.types[dupl] = i.types[dest];
7858 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 7859 i.flags[dupl] = i.flags[dest];
c0f3af97
L
7860 }
7861
7862 if (i.tm.opcode_modifier.immext)
7863 process_immext ();
7864 }
75e5731b 7865 else if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 7866 && i.tm.operand_types[0].bitfield.xmmword)
c0f3af97
L
7867 {
7868 unsigned int j;
7869
9fcfb3d7
L
7870 for (j = 1; j < i.operands; j++)
7871 {
7872 i.op[j - 1] = i.op[j];
7873 i.types[j - 1] = i.types[j];
7874
7875 /* We need to adjust fields in i.tm since they are used by
7876 build_modrm_byte. */
7877 i.tm.operand_types [j - 1] = i.tm.operand_types [j];
8dc0818e
JB
7878
7879 i.flags[j - 1] = i.flags[j];
9fcfb3d7
L
7880 }
7881
e2ec9d29
L
7882 i.operands--;
7883 i.reg_operands--;
e2ec9d29
L
7884 i.tm.operands--;
7885 }
920d2ddc
IT
7886 else if (i.tm.opcode_modifier.implicitquadgroup)
7887 {
a477a8c4
JB
7888 unsigned int regnum, first_reg_in_group, last_reg_in_group;
7889
920d2ddc 7890 /* The second operand must be {x,y,z}mmN, where N is a multiple of 4. */
3528c362 7891 gas_assert (i.operands >= 2 && i.types[1].bitfield.class == RegSIMD);
a477a8c4
JB
7892 regnum = register_number (i.op[1].regs);
7893 first_reg_in_group = regnum & ~3;
7894 last_reg_in_group = first_reg_in_group + 3;
7895 if (regnum != first_reg_in_group)
7896 as_warn (_("source register `%s%s' implicitly denotes"
7897 " `%s%.3s%u' to `%s%.3s%u' source group in `%s'"),
7898 register_prefix, i.op[1].regs->reg_name,
7899 register_prefix, i.op[1].regs->reg_name, first_reg_in_group,
7900 register_prefix, i.op[1].regs->reg_name, last_reg_in_group,
7901 i.tm.name);
7902 }
e2ec9d29
L
7903 else if (i.tm.opcode_modifier.regkludge)
7904 {
7905 /* The imul $imm, %reg instruction is converted into
7906 imul $imm, %reg, %reg, and the clr %reg instruction
7907 is converted into xor %reg, %reg. */
7908
7909 unsigned int first_reg_op;
7910
7911 if (operand_type_check (i.types[0], reg))
7912 first_reg_op = 0;
7913 else
7914 first_reg_op = 1;
7915 /* Pretend we saw the extra register operand. */
9c2799c2 7916 gas_assert (i.reg_operands == 1
7ab9ffdd 7917 && i.op[first_reg_op + 1].regs == 0);
e2ec9d29
L
7918 i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
7919 i.types[first_reg_op + 1] = i.types[first_reg_op];
7920 i.operands++;
7921 i.reg_operands++;
29b0f896
AM
7922 }
7923
85b80b0f 7924 if (i.tm.opcode_modifier.modrm)
29b0f896
AM
7925 {
7926 /* The opcode is completed (modulo i.tm.extension_opcode which
52271982
AM
7927 must be put into the modrm byte). Now, we make the modrm and
7928 index base bytes based on all the info we've collected. */
29b0f896
AM
7929
7930 default_seg = build_modrm_byte ();
7931 }
00cee14f 7932 else if (i.types[0].bitfield.class == SReg)
85b80b0f
JB
7933 {
7934 if (flag_code != CODE_64BIT
7935 ? i.tm.base_opcode == POP_SEG_SHORT
7936 && i.op[0].regs->reg_num == 1
389d00a5 7937 : (i.tm.base_opcode | 1) == (POP_SEG386_SHORT & 0xff)
85b80b0f
JB
7938 && i.op[0].regs->reg_num < 4)
7939 {
7940 as_bad (_("you can't `%s %s%s'"),
7941 i.tm.name, register_prefix, i.op[0].regs->reg_name);
7942 return 0;
7943 }
389d00a5
JB
7944 if (i.op[0].regs->reg_num > 3
7945 && i.tm.opcode_modifier.opcodespace == SPACE_BASE )
85b80b0f 7946 {
389d00a5
JB
7947 i.tm.base_opcode ^= (POP_SEG_SHORT ^ POP_SEG386_SHORT) & 0xff;
7948 i.tm.opcode_modifier.opcodespace = SPACE_0F;
85b80b0f
JB
7949 }
7950 i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
7951 }
389d00a5
JB
7952 else if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
7953 && (i.tm.base_opcode & ~3) == MOV_AX_DISP32)
29b0f896 7954 {
5e042380 7955 default_seg = reg_ds;
29b0f896 7956 }
40fb9820 7957 else if (i.tm.opcode_modifier.isstring)
29b0f896
AM
7958 {
7959 /* For the string instructions that allow a segment override
7960 on one of their operands, the default segment is ds. */
5e042380 7961 default_seg = reg_ds;
29b0f896 7962 }
50128d0c 7963 else if (i.short_form)
85b80b0f
JB
7964 {
7965 /* The register or float register operand is in operand
7966 0 or 1. */
bab6aec1 7967 unsigned int op = i.tm.operand_types[0].bitfield.class != Reg;
85b80b0f
JB
7968
7969 /* Register goes in low 3 bits of opcode. */
7970 i.tm.base_opcode |= i.op[op].regs->reg_num;
7971 if ((i.op[op].regs->reg_flags & RegRex) != 0)
7972 i.rex |= REX_B;
7973 if (!quiet_warnings && i.tm.opcode_modifier.ugh)
7974 {
7975 /* Warn about some common errors, but press on regardless.
7976 The first case can be generated by gcc (<= 2.8.1). */
7977 if (i.operands == 2)
7978 {
7979 /* Reversed arguments on faddp, fsubp, etc. */
7980 as_warn (_("translating to `%s %s%s,%s%s'"), i.tm.name,
7981 register_prefix, i.op[!intel_syntax].regs->reg_name,
7982 register_prefix, i.op[intel_syntax].regs->reg_name);
7983 }
7984 else
7985 {
7986 /* Extraneous `l' suffix on fp insn. */
7987 as_warn (_("translating to `%s %s%s'"), i.tm.name,
7988 register_prefix, i.op[0].regs->reg_name);
7989 }
7990 }
7991 }
29b0f896 7992
514a8bb0 7993 if ((i.seg[0] || i.prefix[SEG_PREFIX])
514a8bb0 7994 && i.tm.base_opcode == 0x8d /* lea */
35648716 7995 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
514a8bb0 7996 && !is_any_vex_encoding(&i.tm))
92334ad2
JB
7997 {
7998 if (!quiet_warnings)
7999 as_warn (_("segment override on `%s' is ineffectual"), i.tm.name);
8000 if (optimize)
8001 {
8002 i.seg[0] = NULL;
8003 i.prefix[SEG_PREFIX] = 0;
8004 }
8005 }
52271982
AM
8006
8007 /* If a segment was explicitly specified, and the specified segment
b6773884
JB
8008 is neither the default nor the one already recorded from a prefix,
8009 use an opcode prefix to select it. If we never figured out what
8010 the default segment is, then default_seg will be zero at this
8011 point, and the specified segment prefix will always be used. */
8012 if (i.seg[0]
8013 && i.seg[0] != default_seg
5e042380 8014 && i386_seg_prefixes[i.seg[0]->reg_num] != i.prefix[SEG_PREFIX])
29b0f896 8015 {
5e042380 8016 if (!add_prefix (i386_seg_prefixes[i.seg[0]->reg_num]))
29b0f896
AM
8017 return 0;
8018 }
8019 return 1;
8020}
8021
a5aeccd9 8022static INLINE void set_rex_vrex (const reg_entry *r, unsigned int rex_bit,
5b7c81bd 8023 bool do_sse2avx)
a5aeccd9
JB
8024{
8025 if (r->reg_flags & RegRex)
8026 {
8027 if (i.rex & rex_bit)
8028 as_bad (_("same type of prefix used twice"));
8029 i.rex |= rex_bit;
8030 }
8031 else if (do_sse2avx && (i.rex & rex_bit) && i.vex.register_specifier)
8032 {
8033 gas_assert (i.vex.register_specifier == r);
8034 i.vex.register_specifier += 8;
8035 }
8036
8037 if (r->reg_flags & RegVRex)
8038 i.vrex |= rex_bit;
8039}
8040
5e042380 8041static const reg_entry *
e3bb37b5 8042build_modrm_byte (void)
29b0f896 8043{
5e042380 8044 const reg_entry *default_seg = NULL;
c0f3af97 8045 unsigned int source, dest;
8cd7925b 8046 int vex_3_sources;
c0f3af97 8047
8cd7925b 8048 vex_3_sources = i.tm.opcode_modifier.vexsources == VEX3SOURCES;
c0f3af97
L
8049 if (vex_3_sources)
8050 {
91d6fa6a 8051 unsigned int nds, reg_slot;
4c2c6516 8052 expressionS *exp;
c0f3af97 8053
6b8d3588 8054 dest = i.operands - 1;
c0f3af97 8055 nds = dest - 1;
922d8de8 8056
a683cc34 8057 /* There are 2 kinds of instructions:
bed3d976 8058 1. 5 operands: 4 register operands or 3 register operands
9d3bf266 8059 plus 1 memory operand plus one Imm4 operand, VexXDS, and
bed3d976 8060 VexW0 or VexW1. The destination must be either XMM, YMM or
43234a1e 8061 ZMM register.
bed3d976 8062 2. 4 operands: 4 register operands or 3 register operands
2f1bada2 8063 plus 1 memory operand, with VexXDS. */
922d8de8 8064 gas_assert ((i.reg_operands == 4
bed3d976
JB
8065 || (i.reg_operands == 3 && i.mem_operands == 1))
8066 && i.tm.opcode_modifier.vexvvvv == VEXXDS
dcd7e323 8067 && i.tm.opcode_modifier.vexw
3528c362 8068 && i.tm.operand_types[dest].bitfield.class == RegSIMD);
a683cc34 8069
48db9223
JB
8070 /* If VexW1 is set, the first non-immediate operand is the source and
8071 the second non-immediate one is encoded in the immediate operand. */
8072 if (i.tm.opcode_modifier.vexw == VEXW1)
8073 {
8074 source = i.imm_operands;
8075 reg_slot = i.imm_operands + 1;
8076 }
8077 else
8078 {
8079 source = i.imm_operands + 1;
8080 reg_slot = i.imm_operands;
8081 }
8082
a683cc34 8083 if (i.imm_operands == 0)
bed3d976
JB
8084 {
8085 /* When there is no immediate operand, generate an 8bit
8086 immediate operand to encode the first operand. */
8087 exp = &im_expressions[i.imm_operands++];
8088 i.op[i.operands].imms = exp;
8089 i.types[i.operands] = imm8;
8090 i.operands++;
8091
3528c362 8092 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
bed3d976
JB
8093 exp->X_op = O_constant;
8094 exp->X_add_number = register_number (i.op[reg_slot].regs) << 4;
43234a1e
L
8095 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
8096 }
922d8de8 8097 else
bed3d976 8098 {
9d3bf266
JB
8099 gas_assert (i.imm_operands == 1);
8100 gas_assert (fits_in_imm4 (i.op[0].imms->X_add_number));
8101 gas_assert (!i.tm.opcode_modifier.immext);
a683cc34 8102
9d3bf266
JB
8103 /* Turn on Imm8 again so that output_imm will generate it. */
8104 i.types[0].bitfield.imm8 = 1;
bed3d976 8105
3528c362 8106 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
9d3bf266 8107 i.op[0].imms->X_add_number
bed3d976 8108 |= register_number (i.op[reg_slot].regs) << 4;
43234a1e 8109 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
bed3d976 8110 }
a683cc34 8111
3528c362 8112 gas_assert (i.tm.operand_types[nds].bitfield.class == RegSIMD);
dae39acc 8113 i.vex.register_specifier = i.op[nds].regs;
c0f3af97
L
8114 }
8115 else
8116 source = dest = 0;
29b0f896
AM
8117
8118 /* i.reg_operands MUST be the number of real register operands;
c0f3af97
L
8119 implicit registers do not count. If there are 3 register
8120 operands, it must be a instruction with VexNDS. For a
8121 instruction with VexNDD, the destination register is encoded
8122 in VEX prefix. If there are 4 register operands, it must be
8123 a instruction with VEX prefix and 3 sources. */
7ab9ffdd
L
8124 if (i.mem_operands == 0
8125 && ((i.reg_operands == 2
2426c15f 8126 && i.tm.opcode_modifier.vexvvvv <= VEXXDS)
7ab9ffdd 8127 || (i.reg_operands == 3
2426c15f 8128 && i.tm.opcode_modifier.vexvvvv == VEXXDS)
7ab9ffdd 8129 || (i.reg_operands == 4 && vex_3_sources)))
29b0f896 8130 {
cab737b9
L
8131 switch (i.operands)
8132 {
8133 case 2:
8134 source = 0;
8135 break;
8136 case 3:
c81128dc
L
8137 /* When there are 3 operands, one of them may be immediate,
8138 which may be the first or the last operand. Otherwise,
c0f3af97
L
8139 the first operand must be shift count register (cl) or it
8140 is an instruction with VexNDS. */
9c2799c2 8141 gas_assert (i.imm_operands == 1
7ab9ffdd 8142 || (i.imm_operands == 0
2426c15f 8143 && (i.tm.opcode_modifier.vexvvvv == VEXXDS
75e5731b
JB
8144 || (i.types[0].bitfield.instance == RegC
8145 && i.types[0].bitfield.byte))));
40fb9820 8146 if (operand_type_check (i.types[0], imm)
75e5731b
JB
8147 || (i.types[0].bitfield.instance == RegC
8148 && i.types[0].bitfield.byte))
40fb9820
L
8149 source = 1;
8150 else
8151 source = 0;
cab737b9
L
8152 break;
8153 case 4:
368d64cc
L
8154 /* When there are 4 operands, the first two must be 8bit
8155 immediate operands. The source operand will be the 3rd
c0f3af97
L
8156 one.
8157
8158 For instructions with VexNDS, if the first operand
8159 an imm8, the source operand is the 2nd one. If the last
8160 operand is imm8, the source operand is the first one. */
9c2799c2 8161 gas_assert ((i.imm_operands == 2
7ab9ffdd
L
8162 && i.types[0].bitfield.imm8
8163 && i.types[1].bitfield.imm8)
2426c15f 8164 || (i.tm.opcode_modifier.vexvvvv == VEXXDS
7ab9ffdd
L
8165 && i.imm_operands == 1
8166 && (i.types[0].bitfield.imm8
cf665fee 8167 || i.types[i.operands - 1].bitfield.imm8)));
9f2670f2
L
8168 if (i.imm_operands == 2)
8169 source = 2;
8170 else
c0f3af97
L
8171 {
8172 if (i.types[0].bitfield.imm8)
8173 source = 1;
8174 else
8175 source = 0;
8176 }
c0f3af97
L
8177 break;
8178 case 5:
cf665fee
JB
8179 gas_assert (!is_evex_encoding (&i.tm));
8180 gas_assert (i.imm_operands == 1 && vex_3_sources);
cab737b9
L
8181 break;
8182 default:
8183 abort ();
8184 }
8185
c0f3af97
L
8186 if (!vex_3_sources)
8187 {
8188 dest = source + 1;
8189
2426c15f 8190 if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
c0f3af97 8191 {
43234a1e 8192 /* For instructions with VexNDS, the register-only source
c5d0745b 8193 operand must be a 32/64bit integer, XMM, YMM, ZMM, or mask
dfd69174 8194 register. It is encoded in VEX prefix. */
f12dc422
L
8195
8196 i386_operand_type op;
8197 unsigned int vvvv;
8198
c2ecccb3
L
8199 /* Swap two source operands if needed. */
8200 if (i.tm.opcode_modifier.swapsources)
f12dc422
L
8201 {
8202 vvvv = source;
8203 source = dest;
8204 }
8205 else
8206 vvvv = dest;
8207
8208 op = i.tm.operand_types[vvvv];
c0f3af97 8209 if ((dest + 1) >= i.operands
bab6aec1 8210 || ((op.bitfield.class != Reg
dc821c5f 8211 || (!op.bitfield.dword && !op.bitfield.qword))
3528c362 8212 && op.bitfield.class != RegSIMD
c0f327b8 8213 && op.bitfield.class != RegMask))
c0f3af97 8214 abort ();
f12dc422 8215 i.vex.register_specifier = i.op[vvvv].regs;
c0f3af97
L
8216 dest++;
8217 }
8218 }
29b0f896
AM
8219
8220 i.rm.mode = 3;
dfd69174
JB
8221 /* One of the register operands will be encoded in the i.rm.reg
8222 field, the other in the combined i.rm.mode and i.rm.regmem
29b0f896
AM
8223 fields. If no form of this instruction supports a memory
8224 destination operand, then we assume the source operand may
8225 sometimes be a memory operand and so we need to store the
8226 destination in the i.rm.reg field. */
dfd69174 8227 if (!i.tm.opcode_modifier.regmem
40fb9820 8228 && operand_type_check (i.tm.operand_types[dest], anymem) == 0)
29b0f896
AM
8229 {
8230 i.rm.reg = i.op[dest].regs->reg_num;
8231 i.rm.regmem = i.op[source].regs->reg_num;
a5aeccd9 8232 set_rex_vrex (i.op[dest].regs, REX_R, i.tm.opcode_modifier.sse2avx);
5b7c81bd 8233 set_rex_vrex (i.op[source].regs, REX_B, false);
29b0f896
AM
8234 }
8235 else
8236 {
8237 i.rm.reg = i.op[source].regs->reg_num;
8238 i.rm.regmem = i.op[dest].regs->reg_num;
a5aeccd9 8239 set_rex_vrex (i.op[dest].regs, REX_B, i.tm.opcode_modifier.sse2avx);
5b7c81bd 8240 set_rex_vrex (i.op[source].regs, REX_R, false);
29b0f896 8241 }
e0c7f900 8242 if (flag_code != CODE_64BIT && (i.rex & REX_R))
c4a530c5 8243 {
4a5c67ed 8244 if (i.types[!i.tm.opcode_modifier.regmem].bitfield.class != RegCR)
c4a530c5 8245 abort ();
e0c7f900 8246 i.rex &= ~REX_R;
c4a530c5
JB
8247 add_prefix (LOCK_PREFIX_OPCODE);
8248 }
29b0f896
AM
8249 }
8250 else
8251 { /* If it's not 2 reg operands... */
c0f3af97
L
8252 unsigned int mem;
8253
29b0f896
AM
8254 if (i.mem_operands)
8255 {
8256 unsigned int fake_zero_displacement = 0;
99018f42 8257 unsigned int op;
4eed87de 8258
7ab9ffdd 8259 for (op = 0; op < i.operands; op++)
8dc0818e 8260 if (i.flags[op] & Operand_Mem)
7ab9ffdd 8261 break;
7ab9ffdd 8262 gas_assert (op < i.operands);
29b0f896 8263
63112cd6 8264 if (i.tm.opcode_modifier.sib)
6c30d220 8265 {
260cd341
LC
8266 /* The index register of VSIB shouldn't be RegIZ. */
8267 if (i.tm.opcode_modifier.sib != SIBMEM
8268 && i.index_reg->reg_num == RegIZ)
6c30d220
L
8269 abort ();
8270
8271 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8272 if (!i.base_reg)
8273 {
8274 i.sib.base = NO_BASE_REGISTER;
8275 i.sib.scale = i.log2_scale_factor;
2f2be86b 8276 i.types[op] = operand_type_and_not (i.types[op], anydisp);
a775efc8 8277 i.types[op].bitfield.disp32 = 1;
6c30d220 8278 }
260cd341
LC
8279
8280 /* Since the mandatory SIB always has index register, so
8281 the code logic remains unchanged. The non-mandatory SIB
8282 without index register is allowed and will be handled
8283 later. */
8284 if (i.index_reg)
8285 {
8286 if (i.index_reg->reg_num == RegIZ)
8287 i.sib.index = NO_INDEX_REGISTER;
8288 else
8289 i.sib.index = i.index_reg->reg_num;
5b7c81bd 8290 set_rex_vrex (i.index_reg, REX_X, false);
260cd341 8291 }
6c30d220
L
8292 }
8293
5e042380 8294 default_seg = reg_ds;
29b0f896
AM
8295
8296 if (i.base_reg == 0)
8297 {
8298 i.rm.mode = 0;
8299 if (!i.disp_operands)
9bb129e8 8300 fake_zero_displacement = 1;
29b0f896
AM
8301 if (i.index_reg == 0)
8302 {
260cd341
LC
8303 /* Both check for VSIB and mandatory non-vector SIB. */
8304 gas_assert (!i.tm.opcode_modifier.sib
8305 || i.tm.opcode_modifier.sib == SIBMEM);
29b0f896 8306 /* Operand is just <disp> */
2f2be86b 8307 i.types[op] = operand_type_and_not (i.types[op], anydisp);
20f0a1fc 8308 if (flag_code == CODE_64BIT)
29b0f896
AM
8309 {
8310 /* 64bit mode overwrites the 32bit absolute
8311 addressing by RIP relative addressing and
8312 absolute addressing is encoded by one of the
8313 redundant SIB forms. */
8314 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8315 i.sib.base = NO_BASE_REGISTER;
8316 i.sib.index = NO_INDEX_REGISTER;
a775efc8 8317 i.types[op].bitfield.disp32 = 1;
20f0a1fc 8318 }
fc225355
L
8319 else if ((flag_code == CODE_16BIT)
8320 ^ (i.prefix[ADDR_PREFIX] != 0))
20f0a1fc
NC
8321 {
8322 i.rm.regmem = NO_BASE_REGISTER_16;
2f2be86b 8323 i.types[op].bitfield.disp16 = 1;
20f0a1fc
NC
8324 }
8325 else
8326 {
8327 i.rm.regmem = NO_BASE_REGISTER;
2f2be86b 8328 i.types[op].bitfield.disp32 = 1;
29b0f896
AM
8329 }
8330 }
63112cd6 8331 else if (!i.tm.opcode_modifier.sib)
29b0f896 8332 {
6c30d220 8333 /* !i.base_reg && i.index_reg */
e968fc9b 8334 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8335 i.sib.index = NO_INDEX_REGISTER;
8336 else
8337 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8338 i.sib.base = NO_BASE_REGISTER;
8339 i.sib.scale = i.log2_scale_factor;
8340 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
2f2be86b 8341 i.types[op] = operand_type_and_not (i.types[op], anydisp);
a775efc8 8342 i.types[op].bitfield.disp32 = 1;
29b0f896 8343 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8344 i.rex |= REX_X;
29b0f896
AM
8345 }
8346 }
8347 /* RIP addressing for 64bit mode. */
e968fc9b 8348 else if (i.base_reg->reg_num == RegIP)
29b0f896 8349 {
63112cd6 8350 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896 8351 i.rm.regmem = NO_BASE_REGISTER;
40fb9820
L
8352 i.types[op].bitfield.disp8 = 0;
8353 i.types[op].bitfield.disp16 = 0;
a775efc8 8354 i.types[op].bitfield.disp32 = 1;
40fb9820 8355 i.types[op].bitfield.disp64 = 0;
71903a11 8356 i.flags[op] |= Operand_PCrel;
20f0a1fc
NC
8357 if (! i.disp_operands)
8358 fake_zero_displacement = 1;
29b0f896 8359 }
dc821c5f 8360 else if (i.base_reg->reg_type.bitfield.word)
29b0f896 8361 {
63112cd6 8362 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896
AM
8363 switch (i.base_reg->reg_num)
8364 {
8365 case 3: /* (%bx) */
8366 if (i.index_reg == 0)
8367 i.rm.regmem = 7;
8368 else /* (%bx,%si) -> 0, or (%bx,%di) -> 1 */
8369 i.rm.regmem = i.index_reg->reg_num - 6;
8370 break;
8371 case 5: /* (%bp) */
5e042380 8372 default_seg = reg_ss;
29b0f896
AM
8373 if (i.index_reg == 0)
8374 {
8375 i.rm.regmem = 6;
40fb9820 8376 if (operand_type_check (i.types[op], disp) == 0)
29b0f896
AM
8377 {
8378 /* fake (%bp) into 0(%bp) */
41eb8e88 8379 if (i.disp_encoding == disp_encoding_16bit)
1a02d6b0
L
8380 i.types[op].bitfield.disp16 = 1;
8381 else
8382 i.types[op].bitfield.disp8 = 1;
252b5132 8383 fake_zero_displacement = 1;
29b0f896
AM
8384 }
8385 }
8386 else /* (%bp,%si) -> 2, or (%bp,%di) -> 3 */
8387 i.rm.regmem = i.index_reg->reg_num - 6 + 2;
8388 break;
8389 default: /* (%si) -> 4 or (%di) -> 5 */
8390 i.rm.regmem = i.base_reg->reg_num - 6 + 4;
8391 }
41eb8e88
L
8392 if (!fake_zero_displacement
8393 && !i.disp_operands
8394 && i.disp_encoding)
8395 {
8396 fake_zero_displacement = 1;
8397 if (i.disp_encoding == disp_encoding_8bit)
8398 i.types[op].bitfield.disp8 = 1;
8399 else
8400 i.types[op].bitfield.disp16 = 1;
8401 }
29b0f896
AM
8402 i.rm.mode = mode_from_disp_size (i.types[op]);
8403 }
8404 else /* i.base_reg and 32/64 bit mode */
8405 {
a9aabc23 8406 if (operand_type_check (i.types[op], disp))
40fb9820 8407 {
73053c1f
JB
8408 i.types[op].bitfield.disp16 = 0;
8409 i.types[op].bitfield.disp64 = 0;
a775efc8 8410 i.types[op].bitfield.disp32 = 1;
40fb9820 8411 }
20f0a1fc 8412
63112cd6 8413 if (!i.tm.opcode_modifier.sib)
6c30d220 8414 i.rm.regmem = i.base_reg->reg_num;
29b0f896 8415 if ((i.base_reg->reg_flags & RegRex) != 0)
161a04f6 8416 i.rex |= REX_B;
29b0f896
AM
8417 i.sib.base = i.base_reg->reg_num;
8418 /* x86-64 ignores REX prefix bit here to avoid decoder
8419 complications. */
848930b2
JB
8420 if (!(i.base_reg->reg_flags & RegRex)
8421 && (i.base_reg->reg_num == EBP_REG_NUM
8422 || i.base_reg->reg_num == ESP_REG_NUM))
5e042380 8423 default_seg = reg_ss;
848930b2 8424 if (i.base_reg->reg_num == 5 && i.disp_operands == 0)
29b0f896 8425 {
848930b2 8426 fake_zero_displacement = 1;
1a02d6b0
L
8427 if (i.disp_encoding == disp_encoding_32bit)
8428 i.types[op].bitfield.disp32 = 1;
8429 else
8430 i.types[op].bitfield.disp8 = 1;
29b0f896
AM
8431 }
8432 i.sib.scale = i.log2_scale_factor;
8433 if (i.index_reg == 0)
8434 {
260cd341
LC
8435 /* Only check for VSIB. */
8436 gas_assert (i.tm.opcode_modifier.sib != VECSIB128
8437 && i.tm.opcode_modifier.sib != VECSIB256
8438 && i.tm.opcode_modifier.sib != VECSIB512);
8439
29b0f896
AM
8440 /* <disp>(%esp) becomes two byte modrm with no index
8441 register. We've already stored the code for esp
8442 in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
8443 Any base register besides %esp will not use the
8444 extra modrm byte. */
8445 i.sib.index = NO_INDEX_REGISTER;
29b0f896 8446 }
63112cd6 8447 else if (!i.tm.opcode_modifier.sib)
29b0f896 8448 {
e968fc9b 8449 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8450 i.sib.index = NO_INDEX_REGISTER;
8451 else
8452 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8453 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8454 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8455 i.rex |= REX_X;
29b0f896 8456 }
67a4f2b7
AO
8457
8458 if (i.disp_operands
8459 && (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
8460 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL))
8461 i.rm.mode = 0;
8462 else
a501d77e
L
8463 {
8464 if (!fake_zero_displacement
8465 && !i.disp_operands
8466 && i.disp_encoding)
8467 {
8468 fake_zero_displacement = 1;
8469 if (i.disp_encoding == disp_encoding_8bit)
8470 i.types[op].bitfield.disp8 = 1;
8471 else
8472 i.types[op].bitfield.disp32 = 1;
8473 }
8474 i.rm.mode = mode_from_disp_size (i.types[op]);
8475 }
29b0f896 8476 }
252b5132 8477
29b0f896
AM
8478 if (fake_zero_displacement)
8479 {
8480 /* Fakes a zero displacement assuming that i.types[op]
8481 holds the correct displacement size. */
8482 expressionS *exp;
8483
9c2799c2 8484 gas_assert (i.op[op].disps == 0);
29b0f896
AM
8485 exp = &disp_expressions[i.disp_operands++];
8486 i.op[op].disps = exp;
8487 exp->X_op = O_constant;
8488 exp->X_add_number = 0;
8489 exp->X_add_symbol = (symbolS *) 0;
8490 exp->X_op_symbol = (symbolS *) 0;
8491 }
c0f3af97
L
8492
8493 mem = op;
29b0f896 8494 }
c0f3af97
L
8495 else
8496 mem = ~0;
252b5132 8497
8c43a48b 8498 if (i.tm.opcode_modifier.vexsources == XOP2SOURCES)
5dd85c99
SP
8499 {
8500 if (operand_type_check (i.types[0], imm))
8501 i.vex.register_specifier = NULL;
8502 else
8503 {
8504 /* VEX.vvvv encodes one of the sources when the first
8505 operand is not an immediate. */
1ef99a7b 8506 if (i.tm.opcode_modifier.vexw == VEXW0)
5dd85c99
SP
8507 i.vex.register_specifier = i.op[0].regs;
8508 else
8509 i.vex.register_specifier = i.op[1].regs;
8510 }
8511
8512 /* Destination is a XMM register encoded in the ModRM.reg
8513 and VEX.R bit. */
8514 i.rm.reg = i.op[2].regs->reg_num;
8515 if ((i.op[2].regs->reg_flags & RegRex) != 0)
8516 i.rex |= REX_R;
8517
8518 /* ModRM.rm and VEX.B encodes the other source. */
8519 if (!i.mem_operands)
8520 {
8521 i.rm.mode = 3;
8522
1ef99a7b 8523 if (i.tm.opcode_modifier.vexw == VEXW0)
5dd85c99
SP
8524 i.rm.regmem = i.op[1].regs->reg_num;
8525 else
8526 i.rm.regmem = i.op[0].regs->reg_num;
8527
8528 if ((i.op[1].regs->reg_flags & RegRex) != 0)
8529 i.rex |= REX_B;
8530 }
8531 }
2426c15f 8532 else if (i.tm.opcode_modifier.vexvvvv == VEXLWP)
f88c9eb0
SP
8533 {
8534 i.vex.register_specifier = i.op[2].regs;
8535 if (!i.mem_operands)
8536 {
8537 i.rm.mode = 3;
8538 i.rm.regmem = i.op[1].regs->reg_num;
8539 if ((i.op[1].regs->reg_flags & RegRex) != 0)
8540 i.rex |= REX_B;
8541 }
8542 }
29b0f896
AM
8543 /* Fill in i.rm.reg or i.rm.regmem field with register operand
8544 (if any) based on i.tm.extension_opcode. Again, we must be
8545 careful to make sure that segment/control/debug/test/MMX
8546 registers are coded into the i.rm.reg field. */
f88c9eb0 8547 else if (i.reg_operands)
29b0f896 8548 {
99018f42 8549 unsigned int op;
7ab9ffdd
L
8550 unsigned int vex_reg = ~0;
8551
8552 for (op = 0; op < i.operands; op++)
921eafea
L
8553 if (i.types[op].bitfield.class == Reg
8554 || i.types[op].bitfield.class == RegBND
8555 || i.types[op].bitfield.class == RegMask
8556 || i.types[op].bitfield.class == SReg
8557 || i.types[op].bitfield.class == RegCR
8558 || i.types[op].bitfield.class == RegDR
8559 || i.types[op].bitfield.class == RegTR
8560 || i.types[op].bitfield.class == RegSIMD
8561 || i.types[op].bitfield.class == RegMMX)
8562 break;
c0209578 8563
7ab9ffdd
L
8564 if (vex_3_sources)
8565 op = dest;
2426c15f 8566 else if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
7ab9ffdd
L
8567 {
8568 /* For instructions with VexNDS, the register-only
8569 source operand is encoded in VEX prefix. */
8570 gas_assert (mem != (unsigned int) ~0);
c0f3af97 8571
a93e3234 8572 if (op > mem || i.tm.cpu_flags.bitfield.cpucmpccxadd)
c0f3af97 8573 {
7ab9ffdd
L
8574 vex_reg = op++;
8575 gas_assert (op < i.operands);
c0f3af97
L
8576 }
8577 else
c0f3af97 8578 {
f12dc422
L
8579 /* Check register-only source operand when two source
8580 operands are swapped. */
8581 if (!i.tm.operand_types[op].bitfield.baseindex
8582 && i.tm.operand_types[op + 1].bitfield.baseindex)
8583 {
8584 vex_reg = op;
8585 op += 2;
8586 gas_assert (mem == (vex_reg + 1)
8587 && op < i.operands);
8588 }
8589 else
8590 {
8591 vex_reg = op + 1;
8592 gas_assert (vex_reg < i.operands);
8593 }
c0f3af97 8594 }
7ab9ffdd 8595 }
2426c15f 8596 else if (i.tm.opcode_modifier.vexvvvv == VEXNDD)
7ab9ffdd 8597 {
f12dc422 8598 /* For instructions with VexNDD, the register destination
7ab9ffdd 8599 is encoded in VEX prefix. */
f12dc422
L
8600 if (i.mem_operands == 0)
8601 {
8602 /* There is no memory operand. */
8603 gas_assert ((op + 2) == i.operands);
8604 vex_reg = op + 1;
8605 }
8606 else
8d63c93e 8607 {
ed438a93
JB
8608 /* There are only 2 non-immediate operands. */
8609 gas_assert (op < i.imm_operands + 2
8610 && i.operands == i.imm_operands + 2);
8611 vex_reg = i.imm_operands + 1;
f12dc422 8612 }
7ab9ffdd
L
8613 }
8614 else
8615 gas_assert (op < i.operands);
99018f42 8616
7ab9ffdd
L
8617 if (vex_reg != (unsigned int) ~0)
8618 {
f12dc422 8619 i386_operand_type *type = &i.tm.operand_types[vex_reg];
7ab9ffdd 8620
bab6aec1 8621 if ((type->bitfield.class != Reg
dc821c5f 8622 || (!type->bitfield.dword && !type->bitfield.qword))
3528c362 8623 && type->bitfield.class != RegSIMD
c0f327b8 8624 && type->bitfield.class != RegMask)
7ab9ffdd 8625 abort ();
f88c9eb0 8626
7ab9ffdd
L
8627 i.vex.register_specifier = i.op[vex_reg].regs;
8628 }
8629
1b9f0c97
L
8630 /* Don't set OP operand twice. */
8631 if (vex_reg != op)
7ab9ffdd 8632 {
1b9f0c97
L
8633 /* If there is an extension opcode to put here, the
8634 register number must be put into the regmem field. */
8635 if (i.tm.extension_opcode != None)
8636 {
8637 i.rm.regmem = i.op[op].regs->reg_num;
a5aeccd9
JB
8638 set_rex_vrex (i.op[op].regs, REX_B,
8639 i.tm.opcode_modifier.sse2avx);
1b9f0c97
L
8640 }
8641 else
8642 {
8643 i.rm.reg = i.op[op].regs->reg_num;
a5aeccd9
JB
8644 set_rex_vrex (i.op[op].regs, REX_R,
8645 i.tm.opcode_modifier.sse2avx);
1b9f0c97 8646 }
7ab9ffdd 8647 }
252b5132 8648
29b0f896
AM
8649 /* Now, if no memory operand has set i.rm.mode = 0, 1, 2 we
8650 must set it to 3 to indicate this is a register operand
8651 in the regmem field. */
8652 if (!i.mem_operands)
8653 i.rm.mode = 3;
8654 }
252b5132 8655
29b0f896 8656 /* Fill in i.rm.reg field with extension opcode (if any). */
c1e679ec 8657 if (i.tm.extension_opcode != None)
29b0f896
AM
8658 i.rm.reg = i.tm.extension_opcode;
8659 }
8660 return default_seg;
8661}
252b5132 8662
48ef937e
JB
8663static INLINE void
8664frag_opcode_byte (unsigned char byte)
8665{
8666 if (now_seg != absolute_section)
8667 FRAG_APPEND_1_CHAR (byte);
8668 else
8669 ++abs_section_offset;
8670}
8671
376cd056
JB
8672static unsigned int
8673flip_code16 (unsigned int code16)
8674{
8675 gas_assert (i.tm.operands == 1);
8676
8677 return !(i.prefix[REX_PREFIX] & REX_W)
8678 && (code16 ? i.tm.operand_types[0].bitfield.disp32
376cd056
JB
8679 : i.tm.operand_types[0].bitfield.disp16)
8680 ? CODE16 : 0;
8681}
8682
29b0f896 8683static void
e3bb37b5 8684output_branch (void)
29b0f896
AM
8685{
8686 char *p;
f8a5c266 8687 int size;
29b0f896
AM
8688 int code16;
8689 int prefix;
8690 relax_substateT subtype;
8691 symbolS *sym;
8692 offsetT off;
8693
48ef937e
JB
8694 if (now_seg == absolute_section)
8695 {
8696 as_bad (_("relaxable branches not supported in absolute section"));
8697 return;
8698 }
8699
f8a5c266 8700 code16 = flag_code == CODE_16BIT ? CODE16 : 0;
1a42a9fe 8701 size = i.disp_encoding > disp_encoding_8bit ? BIG : SMALL;
29b0f896
AM
8702
8703 prefix = 0;
8704 if (i.prefix[DATA_PREFIX] != 0)
252b5132 8705 {
29b0f896
AM
8706 prefix = 1;
8707 i.prefixes -= 1;
376cd056 8708 code16 ^= flip_code16(code16);
252b5132 8709 }
29b0f896
AM
8710 /* Pentium4 branch hints. */
8711 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8712 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
2f66722d 8713 {
29b0f896
AM
8714 prefix++;
8715 i.prefixes--;
8716 }
8717 if (i.prefix[REX_PREFIX] != 0)
8718 {
8719 prefix++;
8720 i.prefixes--;
2f66722d
AM
8721 }
8722
7e8b059b
L
8723 /* BND prefixed jump. */
8724 if (i.prefix[BND_PREFIX] != 0)
8725 {
6cb0a70e
JB
8726 prefix++;
8727 i.prefixes--;
7e8b059b
L
8728 }
8729
f2810fe0
JB
8730 if (i.prefixes != 0)
8731 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
29b0f896
AM
8732
8733 /* It's always a symbol; End frag & setup for relax.
8734 Make sure there is enough room in this frag for the largest
8735 instruction we may generate in md_convert_frag. This is 2
8736 bytes for the opcode and room for the prefix and largest
8737 displacement. */
8738 frag_grow (prefix + 2 + 4);
8739 /* Prefix and 1 opcode byte go in fr_fix. */
8740 p = frag_more (prefix + 1);
8741 if (i.prefix[DATA_PREFIX] != 0)
8742 *p++ = DATA_PREFIX_OPCODE;
8743 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
8744 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
8745 *p++ = i.prefix[SEG_PREFIX];
6cb0a70e
JB
8746 if (i.prefix[BND_PREFIX] != 0)
8747 *p++ = BND_PREFIX_OPCODE;
29b0f896
AM
8748 if (i.prefix[REX_PREFIX] != 0)
8749 *p++ = i.prefix[REX_PREFIX];
8750 *p = i.tm.base_opcode;
8751
8752 if ((unsigned char) *p == JUMP_PC_RELATIVE)
f8a5c266 8753 subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, size);
40fb9820 8754 else if (cpu_arch_flags.bitfield.cpui386)
f8a5c266 8755 subtype = ENCODE_RELAX_STATE (COND_JUMP, size);
29b0f896 8756 else
f8a5c266 8757 subtype = ENCODE_RELAX_STATE (COND_JUMP86, size);
29b0f896 8758 subtype |= code16;
3e73aa7c 8759
29b0f896
AM
8760 sym = i.op[0].disps->X_add_symbol;
8761 off = i.op[0].disps->X_add_number;
3e73aa7c 8762
29b0f896
AM
8763 if (i.op[0].disps->X_op != O_constant
8764 && i.op[0].disps->X_op != O_symbol)
3e73aa7c 8765 {
29b0f896
AM
8766 /* Handle complex expressions. */
8767 sym = make_expr_symbol (i.op[0].disps);
8768 off = 0;
8769 }
3e73aa7c 8770
1ef3994a
JB
8771 frag_now->tc_frag_data.code64 = flag_code == CODE_64BIT;
8772
29b0f896
AM
8773 /* 1 possible extra opcode + 4 byte displacement go in var part.
8774 Pass reloc in fr_var. */
d258b828 8775 frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
29b0f896 8776}
3e73aa7c 8777
bd7ab16b
L
8778#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8779/* Return TRUE iff PLT32 relocation should be used for branching to
8780 symbol S. */
8781
5b7c81bd 8782static bool
bd7ab16b
L
8783need_plt32_p (symbolS *s)
8784{
8785 /* PLT32 relocation is ELF only. */
8786 if (!IS_ELF)
5b7c81bd 8787 return false;
bd7ab16b 8788
a5def729
RO
8789#ifdef TE_SOLARIS
8790 /* Don't emit PLT32 relocation on Solaris: neither native linker nor
8791 krtld support it. */
5b7c81bd 8792 return false;
a5def729
RO
8793#endif
8794
bd7ab16b
L
8795 /* Since there is no need to prepare for PLT branch on x86-64, we
8796 can generate R_X86_64_PLT32, instead of R_X86_64_PC32, which can
8797 be used as a marker for 32-bit PC-relative branches. */
8798 if (!object_64bit)
5b7c81bd 8799 return false;
bd7ab16b 8800
44365e88 8801 if (s == NULL)
5b7c81bd 8802 return false;
44365e88 8803
bd7ab16b
L
8804 /* Weak or undefined symbol need PLT32 relocation. */
8805 if (S_IS_WEAK (s) || !S_IS_DEFINED (s))
5b7c81bd 8806 return true;
bd7ab16b
L
8807
8808 /* Non-global symbol doesn't need PLT32 relocation. */
8809 if (! S_IS_EXTERNAL (s))
5b7c81bd 8810 return false;
bd7ab16b
L
8811
8812 /* Other global symbols need PLT32 relocation. NB: Symbol with
8813 non-default visibilities are treated as normal global symbol
8814 so that PLT32 relocation can be used as a marker for 32-bit
8815 PC-relative branches. It is useful for linker relaxation. */
5b7c81bd 8816 return true;
bd7ab16b
L
8817}
8818#endif
8819
29b0f896 8820static void
e3bb37b5 8821output_jump (void)
29b0f896
AM
8822{
8823 char *p;
8824 int size;
3e02c1cc 8825 fixS *fixP;
bd7ab16b 8826 bfd_reloc_code_real_type jump_reloc = i.reloc[0];
29b0f896 8827
0cfa3eb3 8828 if (i.tm.opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
8829 {
8830 /* This is a loop or jecxz type instruction. */
8831 size = 1;
8832 if (i.prefix[ADDR_PREFIX] != 0)
8833 {
48ef937e 8834 frag_opcode_byte (ADDR_PREFIX_OPCODE);
29b0f896
AM
8835 i.prefixes -= 1;
8836 }
8837 /* Pentium4 branch hints. */
8838 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8839 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
8840 {
48ef937e 8841 frag_opcode_byte (i.prefix[SEG_PREFIX]);
29b0f896 8842 i.prefixes--;
3e73aa7c
JH
8843 }
8844 }
29b0f896
AM
8845 else
8846 {
8847 int code16;
3e73aa7c 8848
29b0f896
AM
8849 code16 = 0;
8850 if (flag_code == CODE_16BIT)
8851 code16 = CODE16;
3e73aa7c 8852
29b0f896
AM
8853 if (i.prefix[DATA_PREFIX] != 0)
8854 {
48ef937e 8855 frag_opcode_byte (DATA_PREFIX_OPCODE);
29b0f896 8856 i.prefixes -= 1;
376cd056 8857 code16 ^= flip_code16(code16);
29b0f896 8858 }
252b5132 8859
29b0f896
AM
8860 size = 4;
8861 if (code16)
8862 size = 2;
8863 }
9fcc94b6 8864
6cb0a70e
JB
8865 /* BND prefixed jump. */
8866 if (i.prefix[BND_PREFIX] != 0)
29b0f896 8867 {
48ef937e 8868 frag_opcode_byte (i.prefix[BND_PREFIX]);
29b0f896
AM
8869 i.prefixes -= 1;
8870 }
252b5132 8871
6cb0a70e 8872 if (i.prefix[REX_PREFIX] != 0)
7e8b059b 8873 {
48ef937e 8874 frag_opcode_byte (i.prefix[REX_PREFIX]);
7e8b059b
L
8875 i.prefixes -= 1;
8876 }
8877
f2810fe0
JB
8878 if (i.prefixes != 0)
8879 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
e0890092 8880
48ef937e
JB
8881 if (now_seg == absolute_section)
8882 {
9a182d04 8883 abs_section_offset += i.opcode_length + size;
48ef937e
JB
8884 return;
8885 }
8886
9a182d04
JB
8887 p = frag_more (i.opcode_length + size);
8888 switch (i.opcode_length)
42164a71
L
8889 {
8890 case 2:
8891 *p++ = i.tm.base_opcode >> 8;
1a0670f3 8892 /* Fall through. */
42164a71
L
8893 case 1:
8894 *p++ = i.tm.base_opcode;
8895 break;
8896 default:
8897 abort ();
8898 }
e0890092 8899
bd7ab16b 8900#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1ef3994a
JB
8901 if (flag_code == CODE_64BIT && size == 4
8902 && jump_reloc == NO_RELOC && i.op[0].disps->X_add_number == 0
bd7ab16b
L
8903 && need_plt32_p (i.op[0].disps->X_add_symbol))
8904 jump_reloc = BFD_RELOC_X86_64_PLT32;
8905#endif
8906
8907 jump_reloc = reloc (size, 1, 1, jump_reloc);
8908
3e02c1cc 8909 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
bd7ab16b 8910 i.op[0].disps, 1, jump_reloc);
3e02c1cc 8911
eb19308f
JB
8912 /* All jumps handled here are signed, but don't unconditionally use a
8913 signed limit check for 32 and 16 bit jumps as we want to allow wrap
8914 around at 4G (outside of 64-bit mode) and 64k (except for XBEGIN)
8915 respectively. */
8916 switch (size)
8917 {
8918 case 1:
8919 fixP->fx_signed = 1;
8920 break;
8921
8922 case 2:
8923 if (i.tm.base_opcode == 0xc7f8)
8924 fixP->fx_signed = 1;
8925 break;
8926
8927 case 4:
8928 if (flag_code == CODE_64BIT)
8929 fixP->fx_signed = 1;
8930 break;
8931 }
29b0f896 8932}
e0890092 8933
29b0f896 8934static void
e3bb37b5 8935output_interseg_jump (void)
29b0f896
AM
8936{
8937 char *p;
8938 int size;
8939 int prefix;
8940 int code16;
252b5132 8941
29b0f896
AM
8942 code16 = 0;
8943 if (flag_code == CODE_16BIT)
8944 code16 = CODE16;
a217f122 8945
29b0f896
AM
8946 prefix = 0;
8947 if (i.prefix[DATA_PREFIX] != 0)
8948 {
8949 prefix = 1;
8950 i.prefixes -= 1;
8951 code16 ^= CODE16;
8952 }
6cb0a70e
JB
8953
8954 gas_assert (!i.prefix[REX_PREFIX]);
252b5132 8955
29b0f896
AM
8956 size = 4;
8957 if (code16)
8958 size = 2;
252b5132 8959
f2810fe0
JB
8960 if (i.prefixes != 0)
8961 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
252b5132 8962
48ef937e
JB
8963 if (now_seg == absolute_section)
8964 {
8965 abs_section_offset += prefix + 1 + 2 + size;
8966 return;
8967 }
8968
29b0f896
AM
8969 /* 1 opcode; 2 segment; offset */
8970 p = frag_more (prefix + 1 + 2 + size);
3e73aa7c 8971
29b0f896
AM
8972 if (i.prefix[DATA_PREFIX] != 0)
8973 *p++ = DATA_PREFIX_OPCODE;
252b5132 8974
29b0f896
AM
8975 if (i.prefix[REX_PREFIX] != 0)
8976 *p++ = i.prefix[REX_PREFIX];
252b5132 8977
29b0f896
AM
8978 *p++ = i.tm.base_opcode;
8979 if (i.op[1].imms->X_op == O_constant)
8980 {
8981 offsetT n = i.op[1].imms->X_add_number;
252b5132 8982
29b0f896
AM
8983 if (size == 2
8984 && !fits_in_unsigned_word (n)
8985 && !fits_in_signed_word (n))
8986 {
8987 as_bad (_("16-bit jump out of range"));
8988 return;
8989 }
8990 md_number_to_chars (p, n, size);
8991 }
8992 else
8993 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
d258b828 8994 i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
6d96a594
C
8995
8996 p += size;
8997 if (i.op[0].imms->X_op == O_constant)
8998 md_number_to_chars (p, (valueT) i.op[0].imms->X_add_number, 2);
8999 else
9000 fix_new_exp (frag_now, p - frag_now->fr_literal, 2,
9001 i.op[0].imms, 0, reloc (2, 0, 0, i.reloc[0]));
29b0f896 9002}
a217f122 9003
b4a3a7b4
L
9004#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9005void
9006x86_cleanup (void)
9007{
9008 char *p;
9009 asection *seg = now_seg;
9010 subsegT subseg = now_subseg;
9011 asection *sec;
9012 unsigned int alignment, align_size_1;
9013 unsigned int isa_1_descsz, feature_2_descsz, descsz;
9014 unsigned int isa_1_descsz_raw, feature_2_descsz_raw;
9015 unsigned int padding;
9016
1273b2f8 9017 if (!IS_ELF || !x86_used_note)
b4a3a7b4
L
9018 return;
9019
b4a3a7b4
L
9020 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X86;
9021
9022 /* The .note.gnu.property section layout:
9023
9024 Field Length Contents
9025 ---- ---- ----
9026 n_namsz 4 4
9027 n_descsz 4 The note descriptor size
9028 n_type 4 NT_GNU_PROPERTY_TYPE_0
9029 n_name 4 "GNU"
9030 n_desc n_descsz The program property array
9031 .... .... ....
9032 */
9033
9034 /* Create the .note.gnu.property section. */
9035 sec = subseg_new (NOTE_GNU_PROPERTY_SECTION_NAME, 0);
fd361982 9036 bfd_set_section_flags (sec,
b4a3a7b4
L
9037 (SEC_ALLOC
9038 | SEC_LOAD
9039 | SEC_DATA
9040 | SEC_HAS_CONTENTS
9041 | SEC_READONLY));
9042
9043 if (get_elf_backend_data (stdoutput)->s->elfclass == ELFCLASS64)
9044 {
9045 align_size_1 = 7;
9046 alignment = 3;
9047 }
9048 else
9049 {
9050 align_size_1 = 3;
9051 alignment = 2;
9052 }
9053
fd361982 9054 bfd_set_section_alignment (sec, alignment);
b4a3a7b4
L
9055 elf_section_type (sec) = SHT_NOTE;
9056
1273b2f8
L
9057 /* GNU_PROPERTY_X86_ISA_1_USED: 4-byte type + 4-byte data size
9058 + 4-byte data */
9059 isa_1_descsz_raw = 4 + 4 + 4;
9060 /* Align GNU_PROPERTY_X86_ISA_1_USED. */
9061 isa_1_descsz = (isa_1_descsz_raw + align_size_1) & ~align_size_1;
b4a3a7b4
L
9062
9063 feature_2_descsz_raw = isa_1_descsz;
9064 /* GNU_PROPERTY_X86_FEATURE_2_USED: 4-byte type + 4-byte data size
1273b2f8 9065 + 4-byte data */
b4a3a7b4
L
9066 feature_2_descsz_raw += 4 + 4 + 4;
9067 /* Align GNU_PROPERTY_X86_FEATURE_2_USED. */
9068 feature_2_descsz = ((feature_2_descsz_raw + align_size_1)
9069 & ~align_size_1);
9070
9071 descsz = feature_2_descsz;
9072 /* Section size: n_namsz + n_descsz + n_type + n_name + n_descsz. */
9073 p = frag_more (4 + 4 + 4 + 4 + descsz);
9074
9075 /* Write n_namsz. */
9076 md_number_to_chars (p, (valueT) 4, 4);
9077
9078 /* Write n_descsz. */
9079 md_number_to_chars (p + 4, (valueT) descsz, 4);
9080
9081 /* Write n_type. */
9082 md_number_to_chars (p + 4 * 2, (valueT) NT_GNU_PROPERTY_TYPE_0, 4);
9083
9084 /* Write n_name. */
9085 memcpy (p + 4 * 3, "GNU", 4);
9086
1273b2f8
L
9087 /* Write 4-byte type. */
9088 md_number_to_chars (p + 4 * 4,
9089 (valueT) GNU_PROPERTY_X86_ISA_1_USED, 4);
b4a3a7b4 9090
1273b2f8
L
9091 /* Write 4-byte data size. */
9092 md_number_to_chars (p + 4 * 5, (valueT) 4, 4);
b4a3a7b4 9093
1273b2f8
L
9094 /* Write 4-byte data. */
9095 md_number_to_chars (p + 4 * 6, (valueT) x86_isa_1_used, 4);
b4a3a7b4 9096
1273b2f8
L
9097 /* Zero out paddings. */
9098 padding = isa_1_descsz - isa_1_descsz_raw;
9099 if (padding)
9100 memset (p + 4 * 7, 0, padding);
b4a3a7b4
L
9101
9102 /* Write 4-byte type. */
9103 md_number_to_chars (p + isa_1_descsz + 4 * 4,
9104 (valueT) GNU_PROPERTY_X86_FEATURE_2_USED, 4);
9105
9106 /* Write 4-byte data size. */
9107 md_number_to_chars (p + isa_1_descsz + 4 * 5, (valueT) 4, 4);
9108
9109 /* Write 4-byte data. */
9110 md_number_to_chars (p + isa_1_descsz + 4 * 6,
9111 (valueT) x86_feature_2_used, 4);
9112
9113 /* Zero out paddings. */
9114 padding = feature_2_descsz - feature_2_descsz_raw;
9115 if (padding)
9116 memset (p + isa_1_descsz + 4 * 7, 0, padding);
9117
9118 /* We probably can't restore the current segment, for there likely
9119 isn't one yet... */
9120 if (seg && subseg)
9121 subseg_set (seg, subseg);
9122}
9123#endif
9124
9c33702b
JB
9125static unsigned int
9126encoding_length (const fragS *start_frag, offsetT start_off,
9127 const char *frag_now_ptr)
9128{
9129 unsigned int len = 0;
9130
9131 if (start_frag != frag_now)
9132 {
9133 const fragS *fr = start_frag;
9134
9135 do {
9136 len += fr->fr_fix;
9137 fr = fr->fr_next;
9138 } while (fr && fr != frag_now);
9139 }
9140
9141 return len - start_off + (frag_now_ptr - frag_now->fr_literal);
9142}
9143
e379e5f3 9144/* Return 1 for test, and, cmp, add, sub, inc and dec which may
79d72f45
HL
9145 be macro-fused with conditional jumps.
9146 NB: If TEST/AND/CMP/ADD/SUB/INC/DEC is of RIP relative address,
9147 or is one of the following format:
9148
9149 cmp m, imm
9150 add m, imm
9151 sub m, imm
9152 test m, imm
9153 and m, imm
9154 inc m
9155 dec m
9156
9157 it is unfusible. */
e379e5f3
L
9158
9159static int
79d72f45 9160maybe_fused_with_jcc_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
9161{
9162 /* No RIP address. */
9163 if (i.base_reg && i.base_reg->reg_num == RegIP)
9164 return 0;
9165
389d00a5
JB
9166 /* No opcodes outside of base encoding space. */
9167 if (i.tm.opcode_modifier.opcodespace != SPACE_BASE)
e379e5f3
L
9168 return 0;
9169
79d72f45
HL
9170 /* add, sub without add/sub m, imm. */
9171 if (i.tm.base_opcode <= 5
e379e5f3
L
9172 || (i.tm.base_opcode >= 0x28 && i.tm.base_opcode <= 0x2d)
9173 || ((i.tm.base_opcode | 3) == 0x83
79d72f45 9174 && (i.tm.extension_opcode == 0x5
e379e5f3 9175 || i.tm.extension_opcode == 0x0)))
79d72f45
HL
9176 {
9177 *mf_cmp_p = mf_cmp_alu_cmp;
9178 return !(i.mem_operands && i.imm_operands);
9179 }
e379e5f3 9180
79d72f45
HL
9181 /* and without and m, imm. */
9182 if ((i.tm.base_opcode >= 0x20 && i.tm.base_opcode <= 0x25)
9183 || ((i.tm.base_opcode | 3) == 0x83
9184 && i.tm.extension_opcode == 0x4))
9185 {
9186 *mf_cmp_p = mf_cmp_test_and;
9187 return !(i.mem_operands && i.imm_operands);
9188 }
9189
9190 /* test without test m imm. */
e379e5f3
L
9191 if ((i.tm.base_opcode | 1) == 0x85
9192 || (i.tm.base_opcode | 1) == 0xa9
9193 || ((i.tm.base_opcode | 1) == 0xf7
79d72f45
HL
9194 && i.tm.extension_opcode == 0))
9195 {
9196 *mf_cmp_p = mf_cmp_test_and;
9197 return !(i.mem_operands && i.imm_operands);
9198 }
9199
9200 /* cmp without cmp m, imm. */
9201 if ((i.tm.base_opcode >= 0x38 && i.tm.base_opcode <= 0x3d)
e379e5f3
L
9202 || ((i.tm.base_opcode | 3) == 0x83
9203 && (i.tm.extension_opcode == 0x7)))
79d72f45
HL
9204 {
9205 *mf_cmp_p = mf_cmp_alu_cmp;
9206 return !(i.mem_operands && i.imm_operands);
9207 }
e379e5f3 9208
79d72f45 9209 /* inc, dec without inc/dec m. */
e379e5f3
L
9210 if ((i.tm.cpu_flags.bitfield.cpuno64
9211 && (i.tm.base_opcode | 0xf) == 0x4f)
9212 || ((i.tm.base_opcode | 1) == 0xff
9213 && i.tm.extension_opcode <= 0x1))
79d72f45
HL
9214 {
9215 *mf_cmp_p = mf_cmp_incdec;
9216 return !i.mem_operands;
9217 }
e379e5f3
L
9218
9219 return 0;
9220}
9221
9222/* Return 1 if a FUSED_JCC_PADDING frag should be generated. */
9223
9224static int
79d72f45 9225add_fused_jcc_padding_frag_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
9226{
9227 /* NB: Don't work with COND_JUMP86 without i386. */
9228 if (!align_branch_power
9229 || now_seg == absolute_section
9230 || !cpu_arch_flags.bitfield.cpui386
9231 || !(align_branch & align_branch_fused_bit))
9232 return 0;
9233
79d72f45 9234 if (maybe_fused_with_jcc_p (mf_cmp_p))
e379e5f3
L
9235 {
9236 if (last_insn.kind == last_insn_other
9237 || last_insn.seg != now_seg)
9238 return 1;
9239 if (flag_debug)
9240 as_warn_where (last_insn.file, last_insn.line,
9241 _("`%s` skips -malign-branch-boundary on `%s`"),
9242 last_insn.name, i.tm.name);
9243 }
9244
9245 return 0;
9246}
9247
9248/* Return 1 if a BRANCH_PREFIX frag should be generated. */
9249
9250static int
9251add_branch_prefix_frag_p (void)
9252{
9253 /* NB: Don't work with COND_JUMP86 without i386. Don't add prefix
9254 to PadLock instructions since they include prefixes in opcode. */
9255 if (!align_branch_power
9256 || !align_branch_prefix_size
9257 || now_seg == absolute_section
9258 || i.tm.cpu_flags.bitfield.cpupadlock
9259 || !cpu_arch_flags.bitfield.cpui386)
9260 return 0;
9261
9262 /* Don't add prefix if it is a prefix or there is no operand in case
9263 that segment prefix is special. */
9264 if (!i.operands || i.tm.opcode_modifier.isprefix)
9265 return 0;
9266
9267 if (last_insn.kind == last_insn_other
9268 || last_insn.seg != now_seg)
9269 return 1;
9270
9271 if (flag_debug)
9272 as_warn_where (last_insn.file, last_insn.line,
9273 _("`%s` skips -malign-branch-boundary on `%s`"),
9274 last_insn.name, i.tm.name);
9275
9276 return 0;
9277}
9278
9279/* Return 1 if a BRANCH_PADDING frag should be generated. */
9280
9281static int
79d72f45
HL
9282add_branch_padding_frag_p (enum align_branch_kind *branch_p,
9283 enum mf_jcc_kind *mf_jcc_p)
e379e5f3
L
9284{
9285 int add_padding;
9286
9287 /* NB: Don't work with COND_JUMP86 without i386. */
9288 if (!align_branch_power
9289 || now_seg == absolute_section
389d00a5
JB
9290 || !cpu_arch_flags.bitfield.cpui386
9291 || i.tm.opcode_modifier.opcodespace != SPACE_BASE)
e379e5f3
L
9292 return 0;
9293
9294 add_padding = 0;
9295
9296 /* Check for jcc and direct jmp. */
9297 if (i.tm.opcode_modifier.jump == JUMP)
9298 {
9299 if (i.tm.base_opcode == JUMP_PC_RELATIVE)
9300 {
9301 *branch_p = align_branch_jmp;
9302 add_padding = align_branch & align_branch_jmp_bit;
9303 }
9304 else
9305 {
79d72f45
HL
9306 /* Because J<cc> and JN<cc> share same group in macro-fusible table,
9307 igore the lowest bit. */
9308 *mf_jcc_p = (i.tm.base_opcode & 0x0e) >> 1;
e379e5f3
L
9309 *branch_p = align_branch_jcc;
9310 if ((align_branch & align_branch_jcc_bit))
9311 add_padding = 1;
9312 }
9313 }
e379e5f3
L
9314 else if ((i.tm.base_opcode | 1) == 0xc3)
9315 {
9316 /* Near ret. */
9317 *branch_p = align_branch_ret;
9318 if ((align_branch & align_branch_ret_bit))
9319 add_padding = 1;
9320 }
9321 else
9322 {
9323 /* Check for indirect jmp, direct and indirect calls. */
9324 if (i.tm.base_opcode == 0xe8)
9325 {
9326 /* Direct call. */
9327 *branch_p = align_branch_call;
9328 if ((align_branch & align_branch_call_bit))
9329 add_padding = 1;
9330 }
9331 else if (i.tm.base_opcode == 0xff
9332 && (i.tm.extension_opcode == 2
9333 || i.tm.extension_opcode == 4))
9334 {
9335 /* Indirect call and jmp. */
9336 *branch_p = align_branch_indirect;
9337 if ((align_branch & align_branch_indirect_bit))
9338 add_padding = 1;
9339 }
9340
9341 if (add_padding
9342 && i.disp_operands
9343 && tls_get_addr
9344 && (i.op[0].disps->X_op == O_symbol
9345 || (i.op[0].disps->X_op == O_subtract
9346 && i.op[0].disps->X_op_symbol == GOT_symbol)))
9347 {
9348 symbolS *s = i.op[0].disps->X_add_symbol;
9349 /* No padding to call to global or undefined tls_get_addr. */
9350 if ((S_IS_EXTERNAL (s) || !S_IS_DEFINED (s))
9351 && strcmp (S_GET_NAME (s), tls_get_addr) == 0)
9352 return 0;
9353 }
9354 }
9355
9356 if (add_padding
9357 && last_insn.kind != last_insn_other
9358 && last_insn.seg == now_seg)
9359 {
9360 if (flag_debug)
9361 as_warn_where (last_insn.file, last_insn.line,
9362 _("`%s` skips -malign-branch-boundary on `%s`"),
9363 last_insn.name, i.tm.name);
9364 return 0;
9365 }
9366
9367 return add_padding;
9368}
9369
29b0f896 9370static void
e3bb37b5 9371output_insn (void)
29b0f896 9372{
2bbd9c25
JJ
9373 fragS *insn_start_frag;
9374 offsetT insn_start_off;
e379e5f3
L
9375 fragS *fragP = NULL;
9376 enum align_branch_kind branch = align_branch_none;
79d72f45
HL
9377 /* The initializer is arbitrary just to avoid uninitialized error.
9378 it's actually either assigned in add_branch_padding_frag_p
9379 or never be used. */
9380 enum mf_jcc_kind mf_jcc = mf_jcc_jo;
2bbd9c25 9381
b4a3a7b4 9382#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
48ef937e 9383 if (IS_ELF && x86_used_note && now_seg != absolute_section)
b4a3a7b4 9384 {
32930e4e
L
9385 if ((i.xstate & xstate_tmm) == xstate_tmm
9386 || i.tm.cpu_flags.bitfield.cpuamx_tile)
9387 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_TMM;
9388
b4a3a7b4
L
9389 if (i.tm.cpu_flags.bitfield.cpu8087
9390 || i.tm.cpu_flags.bitfield.cpu287
9391 || i.tm.cpu_flags.bitfield.cpu387
9392 || i.tm.cpu_flags.bitfield.cpu687
9393 || i.tm.cpu_flags.bitfield.cpufisttp)
9394 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X87;
014d61ea 9395
921eafea 9396 if ((i.xstate & xstate_mmx)
389d00a5
JB
9397 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
9398 && !is_any_vex_encoding (&i.tm)
9399 && (i.tm.base_opcode == 0x77 /* emms */
9400 || i.tm.base_opcode == 0x0e /* femms */)))
b4a3a7b4 9401 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MMX;
014d61ea 9402
32930e4e
L
9403 if (i.index_reg)
9404 {
9405 if (i.index_reg->reg_type.bitfield.zmmword)
9406 i.xstate |= xstate_zmm;
9407 else if (i.index_reg->reg_type.bitfield.ymmword)
9408 i.xstate |= xstate_ymm;
9409 else if (i.index_reg->reg_type.bitfield.xmmword)
9410 i.xstate |= xstate_xmm;
9411 }
014d61ea
JB
9412
9413 /* vzeroall / vzeroupper */
9414 if (i.tm.base_opcode == 0x77 && i.tm.cpu_flags.bitfield.cpuavx)
9415 i.xstate |= xstate_ymm;
9416
c4694f17 9417 if ((i.xstate & xstate_xmm)
389d00a5
JB
9418 /* ldmxcsr / stmxcsr / vldmxcsr / vstmxcsr */
9419 || (i.tm.base_opcode == 0xae
9420 && (i.tm.cpu_flags.bitfield.cpusse
9421 || i.tm.cpu_flags.bitfield.cpuavx))
c4694f17
TG
9422 || i.tm.cpu_flags.bitfield.cpuwidekl
9423 || i.tm.cpu_flags.bitfield.cpukl)
b4a3a7b4 9424 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XMM;
014d61ea 9425
921eafea 9426 if ((i.xstate & xstate_ymm) == xstate_ymm)
b4a3a7b4 9427 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_YMM;
921eafea 9428 if ((i.xstate & xstate_zmm) == xstate_zmm)
b4a3a7b4 9429 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_ZMM;
6225c532 9430 if (i.mask.reg || (i.xstate & xstate_mask) == xstate_mask)
32930e4e 9431 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MASK;
b4a3a7b4
L
9432 if (i.tm.cpu_flags.bitfield.cpufxsr)
9433 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_FXSR;
9434 if (i.tm.cpu_flags.bitfield.cpuxsave)
9435 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVE;
9436 if (i.tm.cpu_flags.bitfield.cpuxsaveopt)
9437 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT;
9438 if (i.tm.cpu_flags.bitfield.cpuxsavec)
9439 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEC;
b0ab0693
L
9440
9441 if (x86_feature_2_used
9442 || i.tm.cpu_flags.bitfield.cpucmov
9443 || i.tm.cpu_flags.bitfield.cpusyscall
389d00a5
JB
9444 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
9445 && i.tm.base_opcode == 0xc7
70e95837 9446 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
b0ab0693
L
9447 && i.tm.extension_opcode == 1) /* cmpxchg8b */)
9448 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_BASELINE;
9449 if (i.tm.cpu_flags.bitfield.cpusse3
9450 || i.tm.cpu_flags.bitfield.cpussse3
9451 || i.tm.cpu_flags.bitfield.cpusse4_1
9452 || i.tm.cpu_flags.bitfield.cpusse4_2
9453 || i.tm.cpu_flags.bitfield.cpucx16
9454 || i.tm.cpu_flags.bitfield.cpupopcnt
9455 /* LAHF-SAHF insns in 64-bit mode. */
9456 || (flag_code == CODE_64BIT
35648716
JB
9457 && (i.tm.base_opcode | 1) == 0x9f
9458 && i.tm.opcode_modifier.opcodespace == SPACE_BASE))
b0ab0693
L
9459 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V2;
9460 if (i.tm.cpu_flags.bitfield.cpuavx
9461 || i.tm.cpu_flags.bitfield.cpuavx2
a9860005
JB
9462 /* Any VEX encoded insns execpt for AVX512F, AVX512BW, AVX512DQ,
9463 XOP, FMA4, LPW, TBM, and AMX. */
b0ab0693
L
9464 || (i.tm.opcode_modifier.vex
9465 && !i.tm.cpu_flags.bitfield.cpuavx512f
9466 && !i.tm.cpu_flags.bitfield.cpuavx512bw
9467 && !i.tm.cpu_flags.bitfield.cpuavx512dq
a9860005
JB
9468 && !i.tm.cpu_flags.bitfield.cpuxop
9469 && !i.tm.cpu_flags.bitfield.cpufma4
b0ab0693
L
9470 && !i.tm.cpu_flags.bitfield.cpulwp
9471 && !i.tm.cpu_flags.bitfield.cputbm
9472 && !(x86_feature_2_used & GNU_PROPERTY_X86_FEATURE_2_TMM))
9473 || i.tm.cpu_flags.bitfield.cpuf16c
9474 || i.tm.cpu_flags.bitfield.cpufma
9475 || i.tm.cpu_flags.bitfield.cpulzcnt
9476 || i.tm.cpu_flags.bitfield.cpumovbe
9477 || i.tm.cpu_flags.bitfield.cpuxsaves
9478 || (x86_feature_2_used
9479 & (GNU_PROPERTY_X86_FEATURE_2_XSAVE
9480 | GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT
9481 | GNU_PROPERTY_X86_FEATURE_2_XSAVEC)) != 0)
9482 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V3;
9483 if (i.tm.cpu_flags.bitfield.cpuavx512f
9484 || i.tm.cpu_flags.bitfield.cpuavx512bw
9485 || i.tm.cpu_flags.bitfield.cpuavx512dq
9486 || i.tm.cpu_flags.bitfield.cpuavx512vl
a9860005
JB
9487 /* Any EVEX encoded insns except for AVX512ER, AVX512PF,
9488 AVX512-4FMAPS, and AVX512-4VNNIW. */
b0ab0693
L
9489 || (i.tm.opcode_modifier.evex
9490 && !i.tm.cpu_flags.bitfield.cpuavx512er
9491 && !i.tm.cpu_flags.bitfield.cpuavx512pf
a9860005 9492 && !i.tm.cpu_flags.bitfield.cpuavx512_4fmaps
b0ab0693
L
9493 && !i.tm.cpu_flags.bitfield.cpuavx512_4vnniw))
9494 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V4;
b4a3a7b4
L
9495 }
9496#endif
9497
29b0f896
AM
9498 /* Tie dwarf2 debug info to the address at the start of the insn.
9499 We can't do this after the insn has been output as the current
9500 frag may have been closed off. eg. by frag_var. */
9501 dwarf2_emit_insn (0);
9502
2bbd9c25
JJ
9503 insn_start_frag = frag_now;
9504 insn_start_off = frag_now_fix ();
9505
79d72f45 9506 if (add_branch_padding_frag_p (&branch, &mf_jcc))
e379e5f3
L
9507 {
9508 char *p;
9509 /* Branch can be 8 bytes. Leave some room for prefixes. */
9510 unsigned int max_branch_padding_size = 14;
9511
9512 /* Align section to boundary. */
9513 record_alignment (now_seg, align_branch_power);
9514
9515 /* Make room for padding. */
9516 frag_grow (max_branch_padding_size);
9517
9518 /* Start of the padding. */
9519 p = frag_more (0);
9520
9521 fragP = frag_now;
9522
9523 frag_var (rs_machine_dependent, max_branch_padding_size, 0,
9524 ENCODE_RELAX_STATE (BRANCH_PADDING, 0),
9525 NULL, 0, p);
9526
79d72f45 9527 fragP->tc_frag_data.mf_type = mf_jcc;
e379e5f3
L
9528 fragP->tc_frag_data.branch_type = branch;
9529 fragP->tc_frag_data.max_bytes = max_branch_padding_size;
9530 }
9531
d59a54c2
JB
9532 if (!cpu_arch_flags.bitfield.cpui386 && (flag_code != CODE_16BIT)
9533 && !pre_386_16bit_warned)
9534 {
9535 as_warn (_("use .code16 to ensure correct addressing mode"));
9536 pre_386_16bit_warned = true;
9537 }
9538
29b0f896 9539 /* Output jumps. */
0cfa3eb3 9540 if (i.tm.opcode_modifier.jump == JUMP)
29b0f896 9541 output_branch ();
0cfa3eb3
JB
9542 else if (i.tm.opcode_modifier.jump == JUMP_BYTE
9543 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896 9544 output_jump ();
0cfa3eb3 9545 else if (i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT)
29b0f896
AM
9546 output_interseg_jump ();
9547 else
9548 {
9549 /* Output normal instructions here. */
9550 char *p;
9551 unsigned char *q;
47465058 9552 unsigned int j;
79d72f45 9553 enum mf_cmp_kind mf_cmp;
4dffcebc 9554
e4e00185 9555 if (avoid_fence
389d00a5
JB
9556 && (i.tm.base_opcode == 0xaee8
9557 || i.tm.base_opcode == 0xaef0
9558 || i.tm.base_opcode == 0xaef8))
48ef937e
JB
9559 {
9560 /* Encode lfence, mfence, and sfence as
9561 f0 83 04 24 00 lock addl $0x0, (%{re}sp). */
47f4115a
JB
9562 if (flag_code == CODE_16BIT)
9563 as_bad (_("Cannot convert `%s' in 16-bit mode"), i.tm.name);
9564 else if (omit_lock_prefix)
9565 as_bad (_("Cannot convert `%s' with `-momit-lock-prefix=yes' in effect"),
9566 i.tm.name);
9567 else if (now_seg != absolute_section)
48ef937e
JB
9568 {
9569 offsetT val = 0x240483f0ULL;
9570
9571 p = frag_more (5);
9572 md_number_to_chars (p, val, 5);
9573 }
9574 else
9575 abs_section_offset += 5;
9576 return;
9577 }
e4e00185 9578
d022bddd
IT
9579 /* Some processors fail on LOCK prefix. This options makes
9580 assembler ignore LOCK prefix and serves as a workaround. */
9581 if (omit_lock_prefix)
9582 {
35648716
JB
9583 if (i.tm.base_opcode == LOCK_PREFIX_OPCODE
9584 && i.tm.opcode_modifier.isprefix)
d022bddd
IT
9585 return;
9586 i.prefix[LOCK_PREFIX] = 0;
9587 }
9588
e379e5f3
L
9589 if (branch)
9590 /* Skip if this is a branch. */
9591 ;
79d72f45 9592 else if (add_fused_jcc_padding_frag_p (&mf_cmp))
e379e5f3
L
9593 {
9594 /* Make room for padding. */
9595 frag_grow (MAX_FUSED_JCC_PADDING_SIZE);
9596 p = frag_more (0);
9597
9598 fragP = frag_now;
9599
9600 frag_var (rs_machine_dependent, MAX_FUSED_JCC_PADDING_SIZE, 0,
9601 ENCODE_RELAX_STATE (FUSED_JCC_PADDING, 0),
9602 NULL, 0, p);
9603
79d72f45 9604 fragP->tc_frag_data.mf_type = mf_cmp;
e379e5f3
L
9605 fragP->tc_frag_data.branch_type = align_branch_fused;
9606 fragP->tc_frag_data.max_bytes = MAX_FUSED_JCC_PADDING_SIZE;
9607 }
9608 else if (add_branch_prefix_frag_p ())
9609 {
9610 unsigned int max_prefix_size = align_branch_prefix_size;
9611
9612 /* Make room for padding. */
9613 frag_grow (max_prefix_size);
9614 p = frag_more (0);
9615
9616 fragP = frag_now;
9617
9618 frag_var (rs_machine_dependent, max_prefix_size, 0,
9619 ENCODE_RELAX_STATE (BRANCH_PREFIX, 0),
9620 NULL, 0, p);
9621
9622 fragP->tc_frag_data.max_bytes = max_prefix_size;
9623 }
9624
43234a1e
L
9625 /* Since the VEX/EVEX prefix contains the implicit prefix, we
9626 don't need the explicit prefix. */
cf665fee 9627 if (!is_any_vex_encoding (&i.tm))
bc4bd9ab 9628 {
7b47a312 9629 switch (i.tm.opcode_modifier.opcodeprefix)
bc4bd9ab 9630 {
7b47a312
L
9631 case PREFIX_0X66:
9632 add_prefix (0x66);
9633 break;
9634 case PREFIX_0XF2:
9635 add_prefix (0xf2);
9636 break;
9637 case PREFIX_0XF3:
8b65b895
L
9638 if (!i.tm.cpu_flags.bitfield.cpupadlock
9639 || (i.prefix[REP_PREFIX] != 0xf3))
9640 add_prefix (0xf3);
c0f3af97 9641 break;
7b47a312 9642 case PREFIX_NONE:
9a182d04 9643 switch (i.opcode_length)
c0f3af97 9644 {
7b47a312 9645 case 2:
7b47a312 9646 break;
9a182d04 9647 case 1:
7b47a312 9648 /* Check for pseudo prefixes. */
9a182d04
JB
9649 if (!i.tm.opcode_modifier.isprefix || i.tm.base_opcode)
9650 break;
7b47a312
L
9651 as_bad_where (insn_start_frag->fr_file,
9652 insn_start_frag->fr_line,
9653 _("pseudo prefix without instruction"));
9654 return;
9655 default:
9656 abort ();
4dffcebc 9657 }
c0f3af97 9658 break;
c0f3af97
L
9659 default:
9660 abort ();
bc4bd9ab 9661 }
c0f3af97 9662
6d19a37a 9663#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
cf61b747
L
9664 /* For x32, add a dummy REX_OPCODE prefix for mov/add with
9665 R_X86_64_GOTTPOFF relocation so that linker can safely
14470f07
L
9666 perform IE->LE optimization. A dummy REX_OPCODE prefix
9667 is also needed for lea with R_X86_64_GOTPC32_TLSDESC
9668 relocation for GDesc -> IE/LE optimization. */
cf61b747
L
9669 if (x86_elf_abi == X86_64_X32_ABI
9670 && i.operands == 2
14470f07
L
9671 && (i.reloc[0] == BFD_RELOC_X86_64_GOTTPOFF
9672 || i.reloc[0] == BFD_RELOC_X86_64_GOTPC32_TLSDESC)
cf61b747
L
9673 && i.prefix[REX_PREFIX] == 0)
9674 add_prefix (REX_OPCODE);
6d19a37a 9675#endif
cf61b747 9676
c0f3af97
L
9677 /* The prefix bytes. */
9678 for (j = ARRAY_SIZE (i.prefix), q = i.prefix; j > 0; j--, q++)
9679 if (*q)
48ef937e 9680 frag_opcode_byte (*q);
0f10071e 9681 }
ae5c1c7b 9682 else
c0f3af97
L
9683 {
9684 for (j = 0, q = i.prefix; j < ARRAY_SIZE (i.prefix); j++, q++)
9685 if (*q)
9686 switch (j)
9687 {
c0f3af97
L
9688 case SEG_PREFIX:
9689 case ADDR_PREFIX:
48ef937e 9690 frag_opcode_byte (*q);
c0f3af97
L
9691 break;
9692 default:
9693 /* There should be no other prefixes for instructions
9694 with VEX prefix. */
9695 abort ();
9696 }
9697
43234a1e
L
9698 /* For EVEX instructions i.vrex should become 0 after
9699 build_evex_prefix. For VEX instructions upper 16 registers
9700 aren't available, so VREX should be 0. */
9701 if (i.vrex)
9702 abort ();
c0f3af97 9703 /* Now the VEX prefix. */
48ef937e
JB
9704 if (now_seg != absolute_section)
9705 {
9706 p = frag_more (i.vex.length);
9707 for (j = 0; j < i.vex.length; j++)
9708 p[j] = i.vex.bytes[j];
9709 }
9710 else
9711 abs_section_offset += i.vex.length;
c0f3af97 9712 }
252b5132 9713
29b0f896 9714 /* Now the opcode; be careful about word order here! */
389d00a5
JB
9715 j = i.opcode_length;
9716 if (!i.vex.length)
9717 switch (i.tm.opcode_modifier.opcodespace)
9718 {
9719 case SPACE_BASE:
9720 break;
9721 case SPACE_0F:
9722 ++j;
9723 break;
9724 case SPACE_0F38:
9725 case SPACE_0F3A:
9726 j += 2;
9727 break;
9728 default:
9729 abort ();
9730 }
9731
48ef937e 9732 if (now_seg == absolute_section)
389d00a5
JB
9733 abs_section_offset += j;
9734 else if (j == 1)
29b0f896
AM
9735 {
9736 FRAG_APPEND_1_CHAR (i.tm.base_opcode);
9737 }
9738 else
9739 {
389d00a5
JB
9740 p = frag_more (j);
9741 if (!i.vex.length
9742 && i.tm.opcode_modifier.opcodespace != SPACE_BASE)
9743 {
9744 *p++ = 0x0f;
9745 if (i.tm.opcode_modifier.opcodespace != SPACE_0F)
9746 *p++ = i.tm.opcode_modifier.opcodespace == SPACE_0F38
9747 ? 0x38 : 0x3a;
9748 }
9749
9a182d04 9750 switch (i.opcode_length)
331d2d0d 9751 {
4dffcebc 9752 case 2:
389d00a5
JB
9753 /* Put out high byte first: can't use md_number_to_chars! */
9754 *p++ = (i.tm.base_opcode >> 8) & 0xff;
9755 /* Fall through. */
9756 case 1:
9757 *p = i.tm.base_opcode & 0xff;
4dffcebc
L
9758 break;
9759 default:
9760 abort ();
9761 break;
331d2d0d 9762 }
0f10071e 9763
29b0f896 9764 }
3e73aa7c 9765
29b0f896 9766 /* Now the modrm byte and sib byte (if present). */
40fb9820 9767 if (i.tm.opcode_modifier.modrm)
29b0f896 9768 {
48ef937e
JB
9769 frag_opcode_byte ((i.rm.regmem << 0)
9770 | (i.rm.reg << 3)
9771 | (i.rm.mode << 6));
29b0f896
AM
9772 /* If i.rm.regmem == ESP (4)
9773 && i.rm.mode != (Register mode)
9774 && not 16 bit
9775 ==> need second modrm byte. */
9776 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
9777 && i.rm.mode != 3
dc821c5f 9778 && !(i.base_reg && i.base_reg->reg_type.bitfield.word))
48ef937e
JB
9779 frag_opcode_byte ((i.sib.base << 0)
9780 | (i.sib.index << 3)
9781 | (i.sib.scale << 6));
29b0f896 9782 }
3e73aa7c 9783
29b0f896 9784 if (i.disp_operands)
2bbd9c25 9785 output_disp (insn_start_frag, insn_start_off);
3e73aa7c 9786
29b0f896 9787 if (i.imm_operands)
2bbd9c25 9788 output_imm (insn_start_frag, insn_start_off);
9c33702b
JB
9789
9790 /*
9791 * frag_now_fix () returning plain abs_section_offset when we're in the
9792 * absolute section, and abs_section_offset not getting updated as data
9793 * gets added to the frag breaks the logic below.
9794 */
9795 if (now_seg != absolute_section)
9796 {
9797 j = encoding_length (insn_start_frag, insn_start_off, frag_more (0));
9798 if (j > 15)
9799 as_warn (_("instruction length of %u bytes exceeds the limit of 15"),
9800 j);
e379e5f3
L
9801 else if (fragP)
9802 {
9803 /* NB: Don't add prefix with GOTPC relocation since
9804 output_disp() above depends on the fixed encoding
9805 length. Can't add prefix with TLS relocation since
9806 it breaks TLS linker optimization. */
9807 unsigned int max = i.has_gotpc_tls_reloc ? 0 : 15 - j;
9808 /* Prefix count on the current instruction. */
9809 unsigned int count = i.vex.length;
9810 unsigned int k;
9811 for (k = 0; k < ARRAY_SIZE (i.prefix); k++)
9812 /* REX byte is encoded in VEX/EVEX prefix. */
9813 if (i.prefix[k] && (k != REX_PREFIX || !i.vex.length))
9814 count++;
9815
9816 /* Count prefixes for extended opcode maps. */
9817 if (!i.vex.length)
389d00a5 9818 switch (i.tm.opcode_modifier.opcodespace)
e379e5f3 9819 {
389d00a5 9820 case SPACE_BASE:
e379e5f3 9821 break;
389d00a5
JB
9822 case SPACE_0F:
9823 count++;
e379e5f3 9824 break;
389d00a5
JB
9825 case SPACE_0F38:
9826 case SPACE_0F3A:
9827 count += 2;
e379e5f3
L
9828 break;
9829 default:
9830 abort ();
9831 }
9832
9833 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
9834 == BRANCH_PREFIX)
9835 {
9836 /* Set the maximum prefix size in BRANCH_PREFIX
9837 frag. */
9838 if (fragP->tc_frag_data.max_bytes > max)
9839 fragP->tc_frag_data.max_bytes = max;
9840 if (fragP->tc_frag_data.max_bytes > count)
9841 fragP->tc_frag_data.max_bytes -= count;
9842 else
9843 fragP->tc_frag_data.max_bytes = 0;
9844 }
9845 else
9846 {
9847 /* Remember the maximum prefix size in FUSED_JCC_PADDING
9848 frag. */
9849 unsigned int max_prefix_size;
9850 if (align_branch_prefix_size > max)
9851 max_prefix_size = max;
9852 else
9853 max_prefix_size = align_branch_prefix_size;
9854 if (max_prefix_size > count)
9855 fragP->tc_frag_data.max_prefix_length
9856 = max_prefix_size - count;
9857 }
9858
9859 /* Use existing segment prefix if possible. Use CS
9860 segment prefix in 64-bit mode. In 32-bit mode, use SS
9861 segment prefix with ESP/EBP base register and use DS
9862 segment prefix without ESP/EBP base register. */
9863 if (i.prefix[SEG_PREFIX])
9864 fragP->tc_frag_data.default_prefix = i.prefix[SEG_PREFIX];
9865 else if (flag_code == CODE_64BIT)
9866 fragP->tc_frag_data.default_prefix = CS_PREFIX_OPCODE;
9867 else if (i.base_reg
9868 && (i.base_reg->reg_num == 4
9869 || i.base_reg->reg_num == 5))
9870 fragP->tc_frag_data.default_prefix = SS_PREFIX_OPCODE;
9871 else
9872 fragP->tc_frag_data.default_prefix = DS_PREFIX_OPCODE;
9873 }
9c33702b 9874 }
29b0f896 9875 }
252b5132 9876
e379e5f3
L
9877 /* NB: Don't work with COND_JUMP86 without i386. */
9878 if (align_branch_power
9879 && now_seg != absolute_section
9880 && cpu_arch_flags.bitfield.cpui386)
9881 {
9882 /* Terminate each frag so that we can add prefix and check for
9883 fused jcc. */
9884 frag_wane (frag_now);
9885 frag_new (0);
9886 }
9887
29b0f896
AM
9888#ifdef DEBUG386
9889 if (flag_debug)
9890 {
7b81dfbb 9891 pi ("" /*line*/, &i);
29b0f896
AM
9892 }
9893#endif /* DEBUG386 */
9894}
252b5132 9895
e205caa7
L
9896/* Return the size of the displacement operand N. */
9897
9898static int
9899disp_size (unsigned int n)
9900{
9901 int size = 4;
43234a1e 9902
b5014f7a 9903 if (i.types[n].bitfield.disp64)
40fb9820
L
9904 size = 8;
9905 else if (i.types[n].bitfield.disp8)
9906 size = 1;
9907 else if (i.types[n].bitfield.disp16)
9908 size = 2;
e205caa7
L
9909 return size;
9910}
9911
9912/* Return the size of the immediate operand N. */
9913
9914static int
9915imm_size (unsigned int n)
9916{
9917 int size = 4;
40fb9820
L
9918 if (i.types[n].bitfield.imm64)
9919 size = 8;
9920 else if (i.types[n].bitfield.imm8 || i.types[n].bitfield.imm8s)
9921 size = 1;
9922 else if (i.types[n].bitfield.imm16)
9923 size = 2;
e205caa7
L
9924 return size;
9925}
9926
29b0f896 9927static void
64e74474 9928output_disp (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
9929{
9930 char *p;
9931 unsigned int n;
252b5132 9932
29b0f896
AM
9933 for (n = 0; n < i.operands; n++)
9934 {
b5014f7a 9935 if (operand_type_check (i.types[n], disp))
29b0f896 9936 {
48ef937e
JB
9937 int size = disp_size (n);
9938
9939 if (now_seg == absolute_section)
9940 abs_section_offset += size;
9941 else if (i.op[n].disps->X_op == O_constant)
29b0f896 9942 {
43234a1e 9943 offsetT val = i.op[n].disps->X_add_number;
252b5132 9944
629cfaf1
JB
9945 val = offset_in_range (val >> (size == 1 ? i.memshift : 0),
9946 size);
29b0f896
AM
9947 p = frag_more (size);
9948 md_number_to_chars (p, val, size);
9949 }
9950 else
9951 {
f86103b7 9952 enum bfd_reloc_code_real reloc_type;
a775efc8
JB
9953 bool pcrel = (i.flags[n] & Operand_PCrel) != 0;
9954 bool sign = (flag_code == CODE_64BIT && size == 4
9955 && (!want_disp32 (&i.tm)
9956 || (i.tm.opcode_modifier.jump && !i.jumpabsolute
9957 && !i.types[n].bitfield.baseindex)))
9958 || pcrel;
02a86693 9959 fixS *fixP;
29b0f896 9960
e205caa7 9961 /* We can't have 8 bit displacement here. */
9c2799c2 9962 gas_assert (!i.types[n].bitfield.disp8);
e205caa7 9963
29b0f896
AM
9964 /* The PC relative address is computed relative
9965 to the instruction boundary, so in case immediate
9966 fields follows, we need to adjust the value. */
9967 if (pcrel && i.imm_operands)
9968 {
29b0f896 9969 unsigned int n1;
e205caa7 9970 int sz = 0;
252b5132 9971
29b0f896 9972 for (n1 = 0; n1 < i.operands; n1++)
40fb9820 9973 if (operand_type_check (i.types[n1], imm))
252b5132 9974 {
e205caa7
L
9975 /* Only one immediate is allowed for PC
9976 relative address. */
9c2799c2 9977 gas_assert (sz == 0);
e205caa7
L
9978 sz = imm_size (n1);
9979 i.op[n].disps->X_add_number -= sz;
252b5132 9980 }
29b0f896 9981 /* We should find the immediate. */
9c2799c2 9982 gas_assert (sz != 0);
29b0f896 9983 }
520dc8e8 9984
29b0f896 9985 p = frag_more (size);
d258b828 9986 reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
d6ab8113 9987 if (GOT_symbol
2bbd9c25 9988 && GOT_symbol == i.op[n].disps->X_add_symbol
d6ab8113 9989 && (((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
9990 || reloc_type == BFD_RELOC_X86_64_32S
9991 || (reloc_type == BFD_RELOC_64
9992 && object_64bit))
d6ab8113
JB
9993 && (i.op[n].disps->X_op == O_symbol
9994 || (i.op[n].disps->X_op == O_add
9995 && ((symbol_get_value_expression
9996 (i.op[n].disps->X_op_symbol)->X_op)
9997 == O_subtract))))
9998 || reloc_type == BFD_RELOC_32_PCREL))
2bbd9c25 9999 {
4fa24527 10000 if (!object_64bit)
7b81dfbb
AJ
10001 {
10002 reloc_type = BFD_RELOC_386_GOTPC;
5b7c81bd 10003 i.has_gotpc_tls_reloc = true;
98da05bf 10004 i.op[n].disps->X_add_number +=
d583596c 10005 encoding_length (insn_start_frag, insn_start_off, p);
7b81dfbb
AJ
10006 }
10007 else if (reloc_type == BFD_RELOC_64)
10008 reloc_type = BFD_RELOC_X86_64_GOTPC64;
d6ab8113 10009 else
7b81dfbb
AJ
10010 /* Don't do the adjustment for x86-64, as there
10011 the pcrel addressing is relative to the _next_
10012 insn, and that is taken care of in other code. */
d6ab8113 10013 reloc_type = BFD_RELOC_X86_64_GOTPC32;
2bbd9c25 10014 }
e379e5f3
L
10015 else if (align_branch_power)
10016 {
10017 switch (reloc_type)
10018 {
10019 case BFD_RELOC_386_TLS_GD:
10020 case BFD_RELOC_386_TLS_LDM:
10021 case BFD_RELOC_386_TLS_IE:
10022 case BFD_RELOC_386_TLS_IE_32:
10023 case BFD_RELOC_386_TLS_GOTIE:
10024 case BFD_RELOC_386_TLS_GOTDESC:
10025 case BFD_RELOC_386_TLS_DESC_CALL:
10026 case BFD_RELOC_X86_64_TLSGD:
10027 case BFD_RELOC_X86_64_TLSLD:
10028 case BFD_RELOC_X86_64_GOTTPOFF:
10029 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
10030 case BFD_RELOC_X86_64_TLSDESC_CALL:
5b7c81bd 10031 i.has_gotpc_tls_reloc = true;
e379e5f3
L
10032 default:
10033 break;
10034 }
10035 }
02a86693
L
10036 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal,
10037 size, i.op[n].disps, pcrel,
10038 reloc_type);
eb19308f
JB
10039
10040 if (flag_code == CODE_64BIT && size == 4 && pcrel
10041 && !i.prefix[ADDR_PREFIX])
10042 fixP->fx_signed = 1;
10043
02a86693
L
10044 /* Check for "call/jmp *mem", "mov mem, %reg",
10045 "test %reg, mem" and "binop mem, %reg" where binop
10046 is one of adc, add, and, cmp, or, sbb, sub, xor
e60f4d3b
L
10047 instructions without data prefix. Always generate
10048 R_386_GOT32X for "sym*GOT" operand in 32-bit mode. */
10049 if (i.prefix[DATA_PREFIX] == 0
10050 && (generate_relax_relocations
10051 || (!object_64bit
10052 && i.rm.mode == 0
10053 && i.rm.regmem == 5))
0cb4071e
L
10054 && (i.rm.mode == 2
10055 || (i.rm.mode == 0 && i.rm.regmem == 5))
389d00a5 10056 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
02a86693
L
10057 && ((i.operands == 1
10058 && i.tm.base_opcode == 0xff
10059 && (i.rm.reg == 2 || i.rm.reg == 4))
10060 || (i.operands == 2
10061 && (i.tm.base_opcode == 0x8b
10062 || i.tm.base_opcode == 0x85
2ae4c703 10063 || (i.tm.base_opcode & ~0x38) == 0x03))))
02a86693
L
10064 {
10065 if (object_64bit)
10066 {
10067 fixP->fx_tcbit = i.rex != 0;
10068 if (i.base_reg
e968fc9b 10069 && (i.base_reg->reg_num == RegIP))
02a86693
L
10070 fixP->fx_tcbit2 = 1;
10071 }
10072 else
10073 fixP->fx_tcbit2 = 1;
10074 }
29b0f896
AM
10075 }
10076 }
10077 }
10078}
252b5132 10079
29b0f896 10080static void
64e74474 10081output_imm (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
10082{
10083 char *p;
10084 unsigned int n;
252b5132 10085
29b0f896
AM
10086 for (n = 0; n < i.operands; n++)
10087 {
40fb9820 10088 if (operand_type_check (i.types[n], imm))
29b0f896 10089 {
48ef937e
JB
10090 int size = imm_size (n);
10091
10092 if (now_seg == absolute_section)
10093 abs_section_offset += size;
10094 else if (i.op[n].imms->X_op == O_constant)
29b0f896 10095 {
29b0f896 10096 offsetT val;
b4cac588 10097
29b0f896
AM
10098 val = offset_in_range (i.op[n].imms->X_add_number,
10099 size);
10100 p = frag_more (size);
10101 md_number_to_chars (p, val, size);
10102 }
10103 else
10104 {
10105 /* Not absolute_section.
10106 Need a 32-bit fixup (don't support 8bit
10107 non-absolute imms). Try to support other
10108 sizes ... */
f86103b7 10109 enum bfd_reloc_code_real reloc_type;
e205caa7 10110 int sign;
29b0f896 10111
40fb9820 10112 if (i.types[n].bitfield.imm32s
a7d61044 10113 && (i.suffix == QWORD_MNEM_SUFFIX
40fb9820 10114 || (!i.suffix && i.tm.opcode_modifier.no_lsuf)))
29b0f896 10115 sign = 1;
e205caa7
L
10116 else
10117 sign = 0;
520dc8e8 10118
29b0f896 10119 p = frag_more (size);
d258b828 10120 reloc_type = reloc (size, 0, sign, i.reloc[n]);
f86103b7 10121
2bbd9c25
JJ
10122 /* This is tough to explain. We end up with this one if we
10123 * have operands that look like
10124 * "_GLOBAL_OFFSET_TABLE_+[.-.L284]". The goal here is to
10125 * obtain the absolute address of the GOT, and it is strongly
10126 * preferable from a performance point of view to avoid using
10127 * a runtime relocation for this. The actual sequence of
10128 * instructions often look something like:
10129 *
10130 * call .L66
10131 * .L66:
10132 * popl %ebx
10133 * addl $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
10134 *
10135 * The call and pop essentially return the absolute address
10136 * of the label .L66 and store it in %ebx. The linker itself
10137 * will ultimately change the first operand of the addl so
10138 * that %ebx points to the GOT, but to keep things simple, the
10139 * .o file must have this operand set so that it generates not
10140 * the absolute address of .L66, but the absolute address of
10141 * itself. This allows the linker itself simply treat a GOTPC
10142 * relocation as asking for a pcrel offset to the GOT to be
10143 * added in, and the addend of the relocation is stored in the
10144 * operand field for the instruction itself.
10145 *
10146 * Our job here is to fix the operand so that it would add
10147 * the correct offset so that %ebx would point to itself. The
10148 * thing that is tricky is that .-.L66 will point to the
10149 * beginning of the instruction, so we need to further modify
10150 * the operand so that it will point to itself. There are
10151 * other cases where you have something like:
10152 *
10153 * .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
10154 *
10155 * and here no correction would be required. Internally in
10156 * the assembler we treat operands of this form as not being
10157 * pcrel since the '.' is explicitly mentioned, and I wonder
10158 * whether it would simplify matters to do it this way. Who
10159 * knows. In earlier versions of the PIC patches, the
10160 * pcrel_adjust field was used to store the correction, but
10161 * since the expression is not pcrel, I felt it would be
10162 * confusing to do it this way. */
10163
d6ab8113 10164 if ((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
10165 || reloc_type == BFD_RELOC_X86_64_32S
10166 || reloc_type == BFD_RELOC_64)
29b0f896
AM
10167 && GOT_symbol
10168 && GOT_symbol == i.op[n].imms->X_add_symbol
10169 && (i.op[n].imms->X_op == O_symbol
10170 || (i.op[n].imms->X_op == O_add
10171 && ((symbol_get_value_expression
10172 (i.op[n].imms->X_op_symbol)->X_op)
10173 == O_subtract))))
10174 {
4fa24527 10175 if (!object_64bit)
d6ab8113 10176 reloc_type = BFD_RELOC_386_GOTPC;
7b81dfbb 10177 else if (size == 4)
d6ab8113 10178 reloc_type = BFD_RELOC_X86_64_GOTPC32;
7b81dfbb
AJ
10179 else if (size == 8)
10180 reloc_type = BFD_RELOC_X86_64_GOTPC64;
5b7c81bd 10181 i.has_gotpc_tls_reloc = true;
d583596c
JB
10182 i.op[n].imms->X_add_number +=
10183 encoding_length (insn_start_frag, insn_start_off, p);
29b0f896 10184 }
29b0f896
AM
10185 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
10186 i.op[n].imms, 0, reloc_type);
10187 }
10188 }
10189 }
252b5132
RH
10190}
10191\f
d182319b
JB
10192/* x86_cons_fix_new is called via the expression parsing code when a
10193 reloc is needed. We use this hook to get the correct .got reloc. */
d182319b
JB
10194static int cons_sign = -1;
10195
10196void
e3bb37b5 10197x86_cons_fix_new (fragS *frag, unsigned int off, unsigned int len,
62ebcb5c 10198 expressionS *exp, bfd_reloc_code_real_type r)
d182319b 10199{
d258b828 10200 r = reloc (len, 0, cons_sign, r);
d182319b
JB
10201
10202#ifdef TE_PE
10203 if (exp->X_op == O_secrel)
10204 {
10205 exp->X_op = O_symbol;
10206 r = BFD_RELOC_32_SECREL;
10207 }
145667f8
MH
10208 else if (exp->X_op == O_secidx)
10209 r = BFD_RELOC_16_SECIDX;
d182319b
JB
10210#endif
10211
10212 fix_new_exp (frag, off, len, exp, 0, r);
10213}
10214
357d1bd8
L
10215/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
10216 purpose of the `.dc.a' internal pseudo-op. */
10217
10218int
10219x86_address_bytes (void)
10220{
10221 if ((stdoutput->arch_info->mach & bfd_mach_x64_32))
10222 return 4;
10223 return stdoutput->arch_info->bits_per_address / 8;
10224}
10225
deea4973
JB
10226#if (!(defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined (OBJ_MACH_O)) \
10227 || defined (LEX_AT)) && !defined (TE_PE)
d258b828 10228# define lex_got(reloc, adjust, types) NULL
718ddfc0 10229#else
f3c180ae
AM
10230/* Parse operands of the form
10231 <symbol>@GOTOFF+<nnn>
10232 and similar .plt or .got references.
10233
10234 If we find one, set up the correct relocation in RELOC and copy the
10235 input string, minus the `@GOTOFF' into a malloc'd buffer for
10236 parsing by the calling routine. Return this buffer, and if ADJUST
10237 is non-null set it to the length of the string we removed from the
10238 input line. Otherwise return NULL. */
10239static char *
91d6fa6a 10240lex_got (enum bfd_reloc_code_real *rel,
64e74474 10241 int *adjust,
d258b828 10242 i386_operand_type *types)
f3c180ae 10243{
7b81dfbb
AJ
10244 /* Some of the relocations depend on the size of what field is to
10245 be relocated. But in our callers i386_immediate and i386_displacement
10246 we don't yet know the operand size (this will be set by insn
10247 matching). Hence we record the word32 relocation here,
10248 and adjust the reloc according to the real size in reloc(). */
145667f8
MH
10249 static const struct
10250 {
f3c180ae 10251 const char *str;
cff8d58a 10252 int len;
4fa24527 10253 const enum bfd_reloc_code_real rel[2];
40fb9820 10254 const i386_operand_type types64;
5b7c81bd 10255 bool need_GOT_symbol;
145667f8
MH
10256 }
10257 gotrel[] =
10258 {
deea4973 10259#ifndef TE_PE
8ce3d284 10260#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
10261 { STRING_COMMA_LEN ("SIZE"), { BFD_RELOC_SIZE32,
10262 BFD_RELOC_SIZE32 },
5b7c81bd 10263 OPERAND_TYPE_IMM32_64, false },
8ce3d284 10264#endif
cff8d58a
L
10265 { STRING_COMMA_LEN ("PLTOFF"), { _dummy_first_bfd_reloc_code_real,
10266 BFD_RELOC_X86_64_PLTOFF64 },
5b7c81bd 10267 OPERAND_TYPE_IMM64, true },
cff8d58a
L
10268 { STRING_COMMA_LEN ("PLT"), { BFD_RELOC_386_PLT32,
10269 BFD_RELOC_X86_64_PLT32 },
a775efc8 10270 OPERAND_TYPE_IMM32_32S_DISP32, false },
cff8d58a
L
10271 { STRING_COMMA_LEN ("GOTPLT"), { _dummy_first_bfd_reloc_code_real,
10272 BFD_RELOC_X86_64_GOTPLT64 },
5b7c81bd 10273 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
10274 { STRING_COMMA_LEN ("GOTOFF"), { BFD_RELOC_386_GOTOFF,
10275 BFD_RELOC_X86_64_GOTOFF64 },
5b7c81bd 10276 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
10277 { STRING_COMMA_LEN ("GOTPCREL"), { _dummy_first_bfd_reloc_code_real,
10278 BFD_RELOC_X86_64_GOTPCREL },
a775efc8 10279 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10280 { STRING_COMMA_LEN ("TLSGD"), { BFD_RELOC_386_TLS_GD,
10281 BFD_RELOC_X86_64_TLSGD },
a775efc8 10282 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10283 { STRING_COMMA_LEN ("TLSLDM"), { BFD_RELOC_386_TLS_LDM,
10284 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10285 OPERAND_TYPE_NONE, true },
cff8d58a
L
10286 { STRING_COMMA_LEN ("TLSLD"), { _dummy_first_bfd_reloc_code_real,
10287 BFD_RELOC_X86_64_TLSLD },
a775efc8 10288 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10289 { STRING_COMMA_LEN ("GOTTPOFF"), { BFD_RELOC_386_TLS_IE_32,
10290 BFD_RELOC_X86_64_GOTTPOFF },
a775efc8 10291 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10292 { STRING_COMMA_LEN ("TPOFF"), { BFD_RELOC_386_TLS_LE_32,
10293 BFD_RELOC_X86_64_TPOFF32 },
a775efc8 10294 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
10295 { STRING_COMMA_LEN ("NTPOFF"), { BFD_RELOC_386_TLS_LE,
10296 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10297 OPERAND_TYPE_NONE, true },
cff8d58a
L
10298 { STRING_COMMA_LEN ("DTPOFF"), { BFD_RELOC_386_TLS_LDO_32,
10299 BFD_RELOC_X86_64_DTPOFF32 },
a775efc8 10300 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
10301 { STRING_COMMA_LEN ("GOTNTPOFF"),{ BFD_RELOC_386_TLS_GOTIE,
10302 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10303 OPERAND_TYPE_NONE, true },
cff8d58a
L
10304 { STRING_COMMA_LEN ("INDNTPOFF"),{ BFD_RELOC_386_TLS_IE,
10305 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10306 OPERAND_TYPE_NONE, true },
cff8d58a
L
10307 { STRING_COMMA_LEN ("GOT"), { BFD_RELOC_386_GOT32,
10308 BFD_RELOC_X86_64_GOT32 },
a775efc8 10309 OPERAND_TYPE_IMM32_32S_64_DISP32, true },
cff8d58a
L
10310 { STRING_COMMA_LEN ("TLSDESC"), { BFD_RELOC_386_TLS_GOTDESC,
10311 BFD_RELOC_X86_64_GOTPC32_TLSDESC },
a775efc8 10312 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10313 { STRING_COMMA_LEN ("TLSCALL"), { BFD_RELOC_386_TLS_DESC_CALL,
10314 BFD_RELOC_X86_64_TLSDESC_CALL },
a775efc8 10315 OPERAND_TYPE_IMM32_32S_DISP32, true },
deea4973
JB
10316#else /* TE_PE */
10317 { STRING_COMMA_LEN ("SECREL32"), { BFD_RELOC_32_SECREL,
10318 BFD_RELOC_32_SECREL },
a775efc8 10319 OPERAND_TYPE_IMM32_32S_64_DISP32_64, false },
deea4973 10320#endif
f3c180ae
AM
10321 };
10322 char *cp;
10323 unsigned int j;
10324
deea4973 10325#if defined (OBJ_MAYBE_ELF) && !defined (TE_PE)
718ddfc0
JB
10326 if (!IS_ELF)
10327 return NULL;
d382c579 10328#endif
718ddfc0 10329
f3c180ae 10330 for (cp = input_line_pointer; *cp != '@'; cp++)
67c11a9b 10331 if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
f3c180ae
AM
10332 return NULL;
10333
47465058 10334 for (j = 0; j < ARRAY_SIZE (gotrel); j++)
f3c180ae 10335 {
cff8d58a 10336 int len = gotrel[j].len;
28f81592 10337 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
f3c180ae 10338 {
4fa24527 10339 if (gotrel[j].rel[object_64bit] != 0)
f3c180ae 10340 {
28f81592
AM
10341 int first, second;
10342 char *tmpbuf, *past_reloc;
f3c180ae 10343
91d6fa6a 10344 *rel = gotrel[j].rel[object_64bit];
f3c180ae 10345
3956db08
JB
10346 if (types)
10347 {
10348 if (flag_code != CODE_64BIT)
40fb9820
L
10349 {
10350 types->bitfield.imm32 = 1;
10351 types->bitfield.disp32 = 1;
10352 }
3956db08
JB
10353 else
10354 *types = gotrel[j].types64;
10355 }
10356
844bf810 10357 if (gotrel[j].need_GOT_symbol && GOT_symbol == NULL)
f3c180ae
AM
10358 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
10359
28f81592 10360 /* The length of the first part of our input line. */
f3c180ae 10361 first = cp - input_line_pointer;
28f81592
AM
10362
10363 /* The second part goes from after the reloc token until
67c11a9b 10364 (and including) an end_of_line char or comma. */
28f81592 10365 past_reloc = cp + 1 + len;
67c11a9b
AM
10366 cp = past_reloc;
10367 while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
10368 ++cp;
10369 second = cp + 1 - past_reloc;
28f81592
AM
10370
10371 /* Allocate and copy string. The trailing NUL shouldn't
10372 be necessary, but be safe. */
add39d23 10373 tmpbuf = XNEWVEC (char, first + second + 2);
f3c180ae 10374 memcpy (tmpbuf, input_line_pointer, first);
0787a12d
AM
10375 if (second != 0 && *past_reloc != ' ')
10376 /* Replace the relocation token with ' ', so that
10377 errors like foo@GOTOFF1 will be detected. */
10378 tmpbuf[first++] = ' ';
af89796a
L
10379 else
10380 /* Increment length by 1 if the relocation token is
10381 removed. */
10382 len++;
10383 if (adjust)
10384 *adjust = len;
0787a12d
AM
10385 memcpy (tmpbuf + first, past_reloc, second);
10386 tmpbuf[first + second] = '\0';
f3c180ae
AM
10387 return tmpbuf;
10388 }
10389
4fa24527
JB
10390 as_bad (_("@%s reloc is not supported with %d-bit output format"),
10391 gotrel[j].str, 1 << (5 + object_64bit));
f3c180ae
AM
10392 return NULL;
10393 }
10394 }
10395
10396 /* Might be a symbol version string. Don't as_bad here. */
10397 return NULL;
10398}
4e4f7c87 10399#endif
f3c180ae 10400
62ebcb5c 10401bfd_reloc_code_real_type
e3bb37b5 10402x86_cons (expressionS *exp, int size)
f3c180ae 10403{
62ebcb5c
AM
10404 bfd_reloc_code_real_type got_reloc = NO_RELOC;
10405
2748c1b1
L
10406#if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
10407 && !defined (LEX_AT)) \
10408 || defined (TE_PE)
ee86248c
JB
10409 intel_syntax = -intel_syntax;
10410
3c7b9c2c 10411 exp->X_md = 0;
4fa24527 10412 if (size == 4 || (object_64bit && size == 8))
f3c180ae
AM
10413 {
10414 /* Handle @GOTOFF and the like in an expression. */
10415 char *save;
10416 char *gotfree_input_line;
4a57f2cf 10417 int adjust = 0;
f3c180ae
AM
10418
10419 save = input_line_pointer;
d258b828 10420 gotfree_input_line = lex_got (&got_reloc, &adjust, NULL);
f3c180ae
AM
10421 if (gotfree_input_line)
10422 input_line_pointer = gotfree_input_line;
10423
10424 expression (exp);
10425
10426 if (gotfree_input_line)
10427 {
10428 /* expression () has merrily parsed up to the end of line,
10429 or a comma - in the wrong buffer. Transfer how far
10430 input_line_pointer has moved to the right buffer. */
10431 input_line_pointer = (save
10432 + (input_line_pointer - gotfree_input_line)
10433 + adjust);
10434 free (gotfree_input_line);
3992d3b7
AM
10435 if (exp->X_op == O_constant
10436 || exp->X_op == O_absent
10437 || exp->X_op == O_illegal
0398aac5 10438 || exp->X_op == O_register
3992d3b7
AM
10439 || exp->X_op == O_big)
10440 {
10441 char c = *input_line_pointer;
10442 *input_line_pointer = 0;
10443 as_bad (_("missing or invalid expression `%s'"), save);
10444 *input_line_pointer = c;
10445 }
b9519cfe
L
10446 else if ((got_reloc == BFD_RELOC_386_PLT32
10447 || got_reloc == BFD_RELOC_X86_64_PLT32)
10448 && exp->X_op != O_symbol)
10449 {
10450 char c = *input_line_pointer;
10451 *input_line_pointer = 0;
10452 as_bad (_("invalid PLT expression `%s'"), save);
10453 *input_line_pointer = c;
10454 }
f3c180ae
AM
10455 }
10456 }
10457 else
10458 expression (exp);
ee86248c
JB
10459
10460 intel_syntax = -intel_syntax;
10461
10462 if (intel_syntax)
10463 i386_intel_simplify (exp);
2748c1b1
L
10464#else
10465 expression (exp);
10466#endif
62ebcb5c 10467
a442cac5
JB
10468 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
10469 if (size == 4 && exp->X_op == O_constant && !object_64bit)
10470 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
10471
62ebcb5c 10472 return got_reloc;
f3c180ae 10473}
f3c180ae 10474
9f32dd5b
L
10475static void
10476signed_cons (int size)
6482c264 10477{
a442cac5 10478 if (object_64bit)
d182319b
JB
10479 cons_sign = 1;
10480 cons (size);
10481 cons_sign = -1;
6482c264
NC
10482}
10483
d182319b 10484#ifdef TE_PE
6482c264 10485static void
7016a5d5 10486pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
6482c264
NC
10487{
10488 expressionS exp;
10489
10490 do
10491 {
10492 expression (&exp);
10493 if (exp.X_op == O_symbol)
10494 exp.X_op = O_secrel;
10495
10496 emit_expr (&exp, 4);
10497 }
10498 while (*input_line_pointer++ == ',');
10499
10500 input_line_pointer--;
10501 demand_empty_rest_of_line ();
10502}
145667f8
MH
10503
10504static void
10505pe_directive_secidx (int dummy ATTRIBUTE_UNUSED)
10506{
10507 expressionS exp;
10508
10509 do
10510 {
10511 expression (&exp);
10512 if (exp.X_op == O_symbol)
10513 exp.X_op = O_secidx;
10514
10515 emit_expr (&exp, 2);
10516 }
10517 while (*input_line_pointer++ == ',');
10518
10519 input_line_pointer--;
10520 demand_empty_rest_of_line ();
10521}
6482c264
NC
10522#endif
10523
7063667e
JB
10524/* Handle Rounding Control / SAE specifiers. */
10525
10526static char *
10527RC_SAE_specifier (const char *pstr)
10528{
10529 unsigned int j;
10530
10531 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); j++)
10532 {
10533 if (!strncmp (pstr, RC_NamesTable[j].name, RC_NamesTable[j].len))
10534 {
10535 if (i.rounding.type != rc_none)
10536 {
10537 as_bad (_("duplicated `{%s}'"), RC_NamesTable[j].name);
10538 return NULL;
10539 }
10540
10541 i.rounding.type = RC_NamesTable[j].type;
10542
10543 return (char *)(pstr + RC_NamesTable[j].len);
10544 }
10545 }
10546
10547 return NULL;
10548}
10549
43234a1e
L
10550/* Handle Vector operations. */
10551
10552static char *
f70c6814 10553check_VecOperations (char *op_string)
43234a1e
L
10554{
10555 const reg_entry *mask;
10556 const char *saved;
10557 char *end_op;
10558
f70c6814 10559 while (*op_string)
43234a1e
L
10560 {
10561 saved = op_string;
10562 if (*op_string == '{')
10563 {
10564 op_string++;
10565
10566 /* Check broadcasts. */
d34049e8 10567 if (startswith (op_string, "1to"))
43234a1e 10568 {
5273a3cd 10569 unsigned int bcst_type;
43234a1e 10570
5273a3cd 10571 if (i.broadcast.type)
43234a1e
L
10572 goto duplicated_vec_op;
10573
10574 op_string += 3;
10575 if (*op_string == '8')
8e6e0792 10576 bcst_type = 8;
b28d1bda 10577 else if (*op_string == '4')
8e6e0792 10578 bcst_type = 4;
b28d1bda 10579 else if (*op_string == '2')
8e6e0792 10580 bcst_type = 2;
43234a1e
L
10581 else if (*op_string == '1'
10582 && *(op_string+1) == '6')
10583 {
8e6e0792 10584 bcst_type = 16;
43234a1e
L
10585 op_string++;
10586 }
0cc78721
CL
10587 else if (*op_string == '3'
10588 && *(op_string+1) == '2')
10589 {
10590 bcst_type = 32;
10591 op_string++;
10592 }
43234a1e
L
10593 else
10594 {
10595 as_bad (_("Unsupported broadcast: `%s'"), saved);
10596 return NULL;
10597 }
10598 op_string++;
10599
5273a3cd
JB
10600 i.broadcast.type = bcst_type;
10601 i.broadcast.operand = this_operand;
43234a1e
L
10602 }
10603 /* Check masking operation. */
10604 else if ((mask = parse_register (op_string, &end_op)) != NULL)
10605 {
8a6fb3f9
JB
10606 if (mask == &bad_reg)
10607 return NULL;
10608
43234a1e 10609 /* k0 can't be used for write mask. */
f74a6307 10610 if (mask->reg_type.bitfield.class != RegMask || !mask->reg_num)
43234a1e 10611 {
6d2cd6b2
JB
10612 as_bad (_("`%s%s' can't be used for write mask"),
10613 register_prefix, mask->reg_name);
43234a1e
L
10614 return NULL;
10615 }
10616
6225c532 10617 if (!i.mask.reg)
43234a1e 10618 {
6225c532
JB
10619 i.mask.reg = mask;
10620 i.mask.operand = this_operand;
43234a1e 10621 }
6225c532
JB
10622 else if (i.mask.reg->reg_num)
10623 goto duplicated_vec_op;
43234a1e
L
10624 else
10625 {
6225c532 10626 i.mask.reg = mask;
43234a1e
L
10627
10628 /* Only "{z}" is allowed here. No need to check
10629 zeroing mask explicitly. */
6225c532 10630 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
10631 {
10632 as_bad (_("invalid write mask `%s'"), saved);
10633 return NULL;
10634 }
10635 }
10636
10637 op_string = end_op;
10638 }
10639 /* Check zeroing-flag for masking operation. */
10640 else if (*op_string == 'z')
10641 {
6225c532 10642 if (!i.mask.reg)
43234a1e 10643 {
6225c532
JB
10644 i.mask.reg = reg_k0;
10645 i.mask.zeroing = 1;
10646 i.mask.operand = this_operand;
43234a1e
L
10647 }
10648 else
10649 {
6225c532 10650 if (i.mask.zeroing)
43234a1e
L
10651 {
10652 duplicated_vec_op:
10653 as_bad (_("duplicated `%s'"), saved);
10654 return NULL;
10655 }
10656
6225c532 10657 i.mask.zeroing = 1;
43234a1e
L
10658
10659 /* Only "{%k}" is allowed here. No need to check mask
10660 register explicitly. */
6225c532 10661 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
10662 {
10663 as_bad (_("invalid zeroing-masking `%s'"),
10664 saved);
10665 return NULL;
10666 }
10667 }
10668
10669 op_string++;
10670 }
7063667e
JB
10671 else if (intel_syntax
10672 && (op_string = RC_SAE_specifier (op_string)) != NULL)
10673 i.rounding.modifier = true;
43234a1e
L
10674 else
10675 goto unknown_vec_op;
10676
10677 if (*op_string != '}')
10678 {
10679 as_bad (_("missing `}' in `%s'"), saved);
10680 return NULL;
10681 }
10682 op_string++;
0ba3a731
L
10683
10684 /* Strip whitespace since the addition of pseudo prefixes
10685 changed how the scrubber treats '{'. */
10686 if (is_space_char (*op_string))
10687 ++op_string;
10688
43234a1e
L
10689 continue;
10690 }
10691 unknown_vec_op:
10692 /* We don't know this one. */
10693 as_bad (_("unknown vector operation: `%s'"), saved);
10694 return NULL;
10695 }
10696
6225c532 10697 if (i.mask.reg && i.mask.zeroing && !i.mask.reg->reg_num)
6d2cd6b2
JB
10698 {
10699 as_bad (_("zeroing-masking only allowed with write mask"));
10700 return NULL;
10701 }
10702
43234a1e
L
10703 return op_string;
10704}
10705
252b5132 10706static int
70e41ade 10707i386_immediate (char *imm_start)
252b5132
RH
10708{
10709 char *save_input_line_pointer;
f3c180ae 10710 char *gotfree_input_line;
252b5132 10711 segT exp_seg = 0;
47926f60 10712 expressionS *exp;
40fb9820
L
10713 i386_operand_type types;
10714
0dfbf9d7 10715 operand_type_set (&types, ~0);
252b5132
RH
10716
10717 if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
10718 {
31b2323c
L
10719 as_bad (_("at most %d immediate operands are allowed"),
10720 MAX_IMMEDIATE_OPERANDS);
252b5132
RH
10721 return 0;
10722 }
10723
10724 exp = &im_expressions[i.imm_operands++];
520dc8e8 10725 i.op[this_operand].imms = exp;
252b5132
RH
10726
10727 if (is_space_char (*imm_start))
10728 ++imm_start;
10729
10730 save_input_line_pointer = input_line_pointer;
10731 input_line_pointer = imm_start;
10732
d258b828 10733 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
10734 if (gotfree_input_line)
10735 input_line_pointer = gotfree_input_line;
252b5132
RH
10736
10737 exp_seg = expression (exp);
10738
83183c0c 10739 SKIP_WHITESPACE ();
252b5132 10740 if (*input_line_pointer)
f3c180ae 10741 as_bad (_("junk `%s' after expression"), input_line_pointer);
252b5132
RH
10742
10743 input_line_pointer = save_input_line_pointer;
f3c180ae 10744 if (gotfree_input_line)
ee86248c
JB
10745 {
10746 free (gotfree_input_line);
10747
9aac24b1 10748 if (exp->X_op == O_constant)
ee86248c
JB
10749 exp->X_op = O_illegal;
10750 }
10751
9aac24b1
JB
10752 if (exp_seg == reg_section)
10753 {
10754 as_bad (_("illegal immediate register operand %s"), imm_start);
10755 return 0;
10756 }
10757
ee86248c
JB
10758 return i386_finalize_immediate (exp_seg, exp, types, imm_start);
10759}
252b5132 10760
ee86248c
JB
10761static int
10762i386_finalize_immediate (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
10763 i386_operand_type types, const char *imm_start)
10764{
10765 if (exp->X_op == O_absent || exp->X_op == O_illegal || exp->X_op == O_big)
252b5132 10766 {
313c53d1
L
10767 if (imm_start)
10768 as_bad (_("missing or invalid immediate expression `%s'"),
10769 imm_start);
3992d3b7 10770 return 0;
252b5132 10771 }
3e73aa7c 10772 else if (exp->X_op == O_constant)
252b5132 10773 {
47926f60 10774 /* Size it properly later. */
40fb9820 10775 i.types[this_operand].bitfield.imm64 = 1;
a442cac5
JB
10776
10777 /* If not 64bit, sign/zero extend val, to account for wraparound
10778 when !BFD64. */
10779 if (flag_code != CODE_64BIT)
10780 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
252b5132 10781 }
4c63da97 10782#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
f86103b7 10783 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
31312f95 10784 && exp_seg != absolute_section
47926f60 10785 && exp_seg != text_section
24eab124
AM
10786 && exp_seg != data_section
10787 && exp_seg != bss_section
10788 && exp_seg != undefined_section
f86103b7 10789 && !bfd_is_com_section (exp_seg))
252b5132 10790 {
d0b47220 10791 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
252b5132
RH
10792 return 0;
10793 }
10794#endif
10795 else
10796 {
10797 /* This is an address. The size of the address will be
24eab124 10798 determined later, depending on destination register,
3e73aa7c 10799 suffix, or the default for the section. */
40fb9820
L
10800 i.types[this_operand].bitfield.imm8 = 1;
10801 i.types[this_operand].bitfield.imm16 = 1;
10802 i.types[this_operand].bitfield.imm32 = 1;
10803 i.types[this_operand].bitfield.imm32s = 1;
10804 i.types[this_operand].bitfield.imm64 = 1;
c6fb90c8
L
10805 i.types[this_operand] = operand_type_and (i.types[this_operand],
10806 types);
252b5132
RH
10807 }
10808
10809 return 1;
10810}
10811
551c1ca1 10812static char *
e3bb37b5 10813i386_scale (char *scale)
252b5132 10814{
551c1ca1
AM
10815 offsetT val;
10816 char *save = input_line_pointer;
252b5132 10817
551c1ca1
AM
10818 input_line_pointer = scale;
10819 val = get_absolute_expression ();
10820
10821 switch (val)
252b5132 10822 {
551c1ca1 10823 case 1:
252b5132
RH
10824 i.log2_scale_factor = 0;
10825 break;
551c1ca1 10826 case 2:
252b5132
RH
10827 i.log2_scale_factor = 1;
10828 break;
551c1ca1 10829 case 4:
252b5132
RH
10830 i.log2_scale_factor = 2;
10831 break;
551c1ca1 10832 case 8:
252b5132
RH
10833 i.log2_scale_factor = 3;
10834 break;
10835 default:
a724f0f4
JB
10836 {
10837 char sep = *input_line_pointer;
10838
10839 *input_line_pointer = '\0';
10840 as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
10841 scale);
10842 *input_line_pointer = sep;
10843 input_line_pointer = save;
10844 return NULL;
10845 }
252b5132 10846 }
29b0f896 10847 if (i.log2_scale_factor != 0 && i.index_reg == 0)
252b5132
RH
10848 {
10849 as_warn (_("scale factor of %d without an index register"),
24eab124 10850 1 << i.log2_scale_factor);
252b5132 10851 i.log2_scale_factor = 0;
252b5132 10852 }
551c1ca1
AM
10853 scale = input_line_pointer;
10854 input_line_pointer = save;
10855 return scale;
252b5132
RH
10856}
10857
252b5132 10858static int
e3bb37b5 10859i386_displacement (char *disp_start, char *disp_end)
252b5132 10860{
29b0f896 10861 expressionS *exp;
252b5132
RH
10862 segT exp_seg = 0;
10863 char *save_input_line_pointer;
f3c180ae 10864 char *gotfree_input_line;
40fb9820
L
10865 int override;
10866 i386_operand_type bigdisp, types = anydisp;
3992d3b7 10867 int ret;
252b5132 10868
31b2323c
L
10869 if (i.disp_operands == MAX_MEMORY_OPERANDS)
10870 {
10871 as_bad (_("at most %d displacement operands are allowed"),
10872 MAX_MEMORY_OPERANDS);
10873 return 0;
10874 }
10875
0dfbf9d7 10876 operand_type_set (&bigdisp, 0);
6f2f06be 10877 if (i.jumpabsolute
48bcea9f 10878 || i.types[this_operand].bitfield.baseindex
0cfa3eb3
JB
10879 || (current_templates->start->opcode_modifier.jump != JUMP
10880 && current_templates->start->opcode_modifier.jump != JUMP_DWORD))
e05278af 10881 {
48bcea9f 10882 i386_addressing_mode ();
e05278af 10883 override = (i.prefix[ADDR_PREFIX] != 0);
40fb9820
L
10884 if (flag_code == CODE_64BIT)
10885 {
a775efc8 10886 bigdisp.bitfield.disp32 = 1;
40fb9820 10887 if (!override)
a775efc8 10888 bigdisp.bitfield.disp64 = 1;
40fb9820
L
10889 }
10890 else if ((flag_code == CODE_16BIT) ^ override)
40fb9820 10891 bigdisp.bitfield.disp16 = 1;
48bcea9f
JB
10892 else
10893 bigdisp.bitfield.disp32 = 1;
e05278af
JB
10894 }
10895 else
10896 {
376cd056
JB
10897 /* For PC-relative branches, the width of the displacement may be
10898 dependent upon data size, but is never dependent upon address size.
10899 Also make sure to not unintentionally match against a non-PC-relative
10900 branch template. */
10901 static templates aux_templates;
10902 const insn_template *t = current_templates->start;
5b7c81bd 10903 bool has_intel64 = false;
376cd056
JB
10904
10905 aux_templates.start = t;
10906 while (++t < current_templates->end)
10907 {
10908 if (t->opcode_modifier.jump
10909 != current_templates->start->opcode_modifier.jump)
10910 break;
4b5aaf5f 10911 if ((t->opcode_modifier.isa64 >= INTEL64))
5b7c81bd 10912 has_intel64 = true;
376cd056
JB
10913 }
10914 if (t < current_templates->end)
10915 {
10916 aux_templates.end = t;
10917 current_templates = &aux_templates;
10918 }
10919
e05278af 10920 override = (i.prefix[DATA_PREFIX] != 0);
40fb9820
L
10921 if (flag_code == CODE_64BIT)
10922 {
376cd056
JB
10923 if ((override || i.suffix == WORD_MNEM_SUFFIX)
10924 && (!intel64 || !has_intel64))
40fb9820
L
10925 bigdisp.bitfield.disp16 = 1;
10926 else
a775efc8 10927 bigdisp.bitfield.disp32 = 1;
40fb9820
L
10928 }
10929 else
e05278af
JB
10930 {
10931 if (!override)
10932 override = (i.suffix == (flag_code != CODE_16BIT
10933 ? WORD_MNEM_SUFFIX
10934 : LONG_MNEM_SUFFIX));
40fb9820
L
10935 bigdisp.bitfield.disp32 = 1;
10936 if ((flag_code == CODE_16BIT) ^ override)
10937 {
10938 bigdisp.bitfield.disp32 = 0;
10939 bigdisp.bitfield.disp16 = 1;
10940 }
e05278af 10941 }
e05278af 10942 }
c6fb90c8
L
10943 i.types[this_operand] = operand_type_or (i.types[this_operand],
10944 bigdisp);
252b5132
RH
10945
10946 exp = &disp_expressions[i.disp_operands];
520dc8e8 10947 i.op[this_operand].disps = exp;
252b5132
RH
10948 i.disp_operands++;
10949 save_input_line_pointer = input_line_pointer;
10950 input_line_pointer = disp_start;
10951 END_STRING_AND_SAVE (disp_end);
10952
10953#ifndef GCC_ASM_O_HACK
10954#define GCC_ASM_O_HACK 0
10955#endif
10956#if GCC_ASM_O_HACK
10957 END_STRING_AND_SAVE (disp_end + 1);
40fb9820 10958 if (i.types[this_operand].bitfield.baseIndex
24eab124 10959 && displacement_string_end[-1] == '+')
252b5132
RH
10960 {
10961 /* This hack is to avoid a warning when using the "o"
24eab124
AM
10962 constraint within gcc asm statements.
10963 For instance:
10964
10965 #define _set_tssldt_desc(n,addr,limit,type) \
10966 __asm__ __volatile__ ( \
10967 "movw %w2,%0\n\t" \
10968 "movw %w1,2+%0\n\t" \
10969 "rorl $16,%1\n\t" \
10970 "movb %b1,4+%0\n\t" \
10971 "movb %4,5+%0\n\t" \
10972 "movb $0,6+%0\n\t" \
10973 "movb %h1,7+%0\n\t" \
10974 "rorl $16,%1" \
10975 : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
10976
10977 This works great except that the output assembler ends
10978 up looking a bit weird if it turns out that there is
10979 no offset. You end up producing code that looks like:
10980
10981 #APP
10982 movw $235,(%eax)
10983 movw %dx,2+(%eax)
10984 rorl $16,%edx
10985 movb %dl,4+(%eax)
10986 movb $137,5+(%eax)
10987 movb $0,6+(%eax)
10988 movb %dh,7+(%eax)
10989 rorl $16,%edx
10990 #NO_APP
10991
47926f60 10992 So here we provide the missing zero. */
24eab124
AM
10993
10994 *displacement_string_end = '0';
252b5132
RH
10995 }
10996#endif
d258b828 10997 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
10998 if (gotfree_input_line)
10999 input_line_pointer = gotfree_input_line;
252b5132 11000
24eab124 11001 exp_seg = expression (exp);
252b5132 11002
636c26b0
AM
11003 SKIP_WHITESPACE ();
11004 if (*input_line_pointer)
11005 as_bad (_("junk `%s' after expression"), input_line_pointer);
11006#if GCC_ASM_O_HACK
11007 RESTORE_END_STRING (disp_end + 1);
11008#endif
636c26b0 11009 input_line_pointer = save_input_line_pointer;
636c26b0 11010 if (gotfree_input_line)
ee86248c
JB
11011 {
11012 free (gotfree_input_line);
11013
11014 if (exp->X_op == O_constant || exp->X_op == O_register)
11015 exp->X_op = O_illegal;
11016 }
11017
11018 ret = i386_finalize_displacement (exp_seg, exp, types, disp_start);
11019
11020 RESTORE_END_STRING (disp_end);
11021
11022 return ret;
11023}
11024
11025static int
11026i386_finalize_displacement (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
11027 i386_operand_type types, const char *disp_start)
11028{
ee86248c 11029 int ret = 1;
636c26b0 11030
24eab124
AM
11031 /* We do this to make sure that the section symbol is in
11032 the symbol table. We will ultimately change the relocation
47926f60 11033 to be relative to the beginning of the section. */
1ae12ab7 11034 if (i.reloc[this_operand] == BFD_RELOC_386_GOTOFF
d6ab8113
JB
11035 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
11036 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
24eab124 11037 {
636c26b0 11038 if (exp->X_op != O_symbol)
3992d3b7 11039 goto inv_disp;
636c26b0 11040
e5cb08ac 11041 if (S_IS_LOCAL (exp->X_add_symbol)
c64efb4b
L
11042 && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section
11043 && S_GET_SEGMENT (exp->X_add_symbol) != expr_section)
24eab124 11044 section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
24eab124
AM
11045 exp->X_op = O_subtract;
11046 exp->X_op_symbol = GOT_symbol;
1ae12ab7 11047 if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
29b0f896 11048 i.reloc[this_operand] = BFD_RELOC_32_PCREL;
d6ab8113
JB
11049 else if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
11050 i.reloc[this_operand] = BFD_RELOC_64;
23df1078 11051 else
29b0f896 11052 i.reloc[this_operand] = BFD_RELOC_32;
24eab124 11053 }
252b5132 11054
3992d3b7
AM
11055 else if (exp->X_op == O_absent
11056 || exp->X_op == O_illegal
ee86248c 11057 || exp->X_op == O_big)
2daf4fd8 11058 {
3992d3b7
AM
11059 inv_disp:
11060 as_bad (_("missing or invalid displacement expression `%s'"),
2daf4fd8 11061 disp_start);
3992d3b7 11062 ret = 0;
2daf4fd8
AM
11063 }
11064
a50187b2
JB
11065 else if (exp->X_op == O_constant)
11066 {
11067 /* Sizing gets taken care of by optimize_disp().
11068
11069 If not 64bit, sign/zero extend val, to account for wraparound
11070 when !BFD64. */
11071 if (flag_code != CODE_64BIT)
11072 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
11073 }
11074
4c63da97 11075#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
a50187b2 11076 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
3992d3b7
AM
11077 && exp_seg != absolute_section
11078 && exp_seg != text_section
11079 && exp_seg != data_section
11080 && exp_seg != bss_section
11081 && exp_seg != undefined_section
11082 && !bfd_is_com_section (exp_seg))
24eab124 11083 {
d0b47220 11084 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
3992d3b7 11085 ret = 0;
24eab124 11086 }
252b5132 11087#endif
3956db08 11088
a50187b2 11089 else if (current_templates->start->opcode_modifier.jump == JUMP_BYTE)
48bcea9f
JB
11090 i.types[this_operand].bitfield.disp8 = 1;
11091
40fb9820 11092 /* Check if this is a displacement only operand. */
02b83698
JB
11093 if (!i.types[this_operand].bitfield.baseindex)
11094 i.types[this_operand] =
11095 operand_type_or (operand_type_and_not (i.types[this_operand], anydisp),
11096 operand_type_and (i.types[this_operand], types));
3956db08 11097
3992d3b7 11098 return ret;
252b5132
RH
11099}
11100
2abc2bec
JB
11101/* Return the active addressing mode, taking address override and
11102 registers forming the address into consideration. Update the
11103 address override prefix if necessary. */
47926f60 11104
2abc2bec
JB
11105static enum flag_code
11106i386_addressing_mode (void)
252b5132 11107{
be05d201
L
11108 enum flag_code addr_mode;
11109
11110 if (i.prefix[ADDR_PREFIX])
11111 addr_mode = flag_code == CODE_32BIT ? CODE_16BIT : CODE_32BIT;
a23b33b3
JB
11112 else if (flag_code == CODE_16BIT
11113 && current_templates->start->cpu_flags.bitfield.cpumpx
11114 /* Avoid replacing the "16-bit addressing not allowed" diagnostic
11115 from md_assemble() by "is not a valid base/index expression"
11116 when there is a base and/or index. */
11117 && !i.types[this_operand].bitfield.baseindex)
11118 {
11119 /* MPX insn memory operands with neither base nor index must be forced
11120 to use 32-bit addressing in 16-bit mode. */
11121 addr_mode = CODE_32BIT;
11122 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
11123 ++i.prefixes;
11124 gas_assert (!i.types[this_operand].bitfield.disp16);
11125 gas_assert (!i.types[this_operand].bitfield.disp32);
11126 }
be05d201
L
11127 else
11128 {
11129 addr_mode = flag_code;
11130
24eab124 11131#if INFER_ADDR_PREFIX
be05d201
L
11132 if (i.mem_operands == 0)
11133 {
11134 /* Infer address prefix from the first memory operand. */
11135 const reg_entry *addr_reg = i.base_reg;
11136
11137 if (addr_reg == NULL)
11138 addr_reg = i.index_reg;
eecb386c 11139
be05d201
L
11140 if (addr_reg)
11141 {
e968fc9b 11142 if (addr_reg->reg_type.bitfield.dword)
be05d201
L
11143 addr_mode = CODE_32BIT;
11144 else if (flag_code != CODE_64BIT
dc821c5f 11145 && addr_reg->reg_type.bitfield.word)
be05d201
L
11146 addr_mode = CODE_16BIT;
11147
11148 if (addr_mode != flag_code)
11149 {
11150 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
11151 i.prefixes += 1;
11152 /* Change the size of any displacement too. At most one
11153 of Disp16 or Disp32 is set.
11154 FIXME. There doesn't seem to be any real need for
11155 separate Disp16 and Disp32 flags. The same goes for
11156 Imm16 and Imm32. Removing them would probably clean
11157 up the code quite a lot. */
11158 if (flag_code != CODE_64BIT
11159 && (i.types[this_operand].bitfield.disp16
11160 || i.types[this_operand].bitfield.disp32))
11161 i.types[this_operand]
11162 = operand_type_xor (i.types[this_operand], disp16_32);
11163 }
11164 }
11165 }
24eab124 11166#endif
be05d201
L
11167 }
11168
2abc2bec
JB
11169 return addr_mode;
11170}
11171
11172/* Make sure the memory operand we've been dealt is valid.
11173 Return 1 on success, 0 on a failure. */
11174
11175static int
11176i386_index_check (const char *operand_string)
11177{
11178 const char *kind = "base/index";
11179 enum flag_code addr_mode = i386_addressing_mode ();
a152332d 11180 const insn_template *t = current_templates->start;
2abc2bec 11181
a152332d
JB
11182 if (t->opcode_modifier.isstring
11183 && !t->cpu_flags.bitfield.cpupadlock
fc0763e6
JB
11184 && (current_templates->end[-1].opcode_modifier.isstring
11185 || i.mem_operands))
11186 {
11187 /* Memory operands of string insns are special in that they only allow
11188 a single register (rDI, rSI, or rBX) as their memory address. */
be05d201
L
11189 const reg_entry *expected_reg;
11190 static const char *di_si[][2] =
11191 {
11192 { "esi", "edi" },
11193 { "si", "di" },
11194 { "rsi", "rdi" }
11195 };
11196 static const char *bx[] = { "ebx", "bx", "rbx" };
fc0763e6
JB
11197
11198 kind = "string address";
11199
a152332d 11200 if (t->opcode_modifier.prefixok == PrefixRep)
fc0763e6 11201 {
51c8edf6
JB
11202 int es_op = current_templates->end[-1].opcode_modifier.isstring
11203 - IS_STRING_ES_OP0;
11204 int op = 0;
fc0763e6 11205
51c8edf6 11206 if (!current_templates->end[-1].operand_types[0].bitfield.baseindex
fc0763e6
JB
11207 || ((!i.mem_operands != !intel_syntax)
11208 && current_templates->end[-1].operand_types[1]
11209 .bitfield.baseindex))
51c8edf6 11210 op = 1;
fe0e921f
AM
11211 expected_reg
11212 = (const reg_entry *) str_hash_find (reg_hash,
11213 di_si[addr_mode][op == es_op]);
fc0763e6
JB
11214 }
11215 else
fe0e921f
AM
11216 expected_reg
11217 = (const reg_entry *)str_hash_find (reg_hash, bx[addr_mode]);
fc0763e6 11218
be05d201
L
11219 if (i.base_reg != expected_reg
11220 || i.index_reg
fc0763e6 11221 || operand_type_check (i.types[this_operand], disp))
fc0763e6 11222 {
be05d201
L
11223 /* The second memory operand must have the same size as
11224 the first one. */
11225 if (i.mem_operands
11226 && i.base_reg
11227 && !((addr_mode == CODE_64BIT
dc821c5f 11228 && i.base_reg->reg_type.bitfield.qword)
be05d201 11229 || (addr_mode == CODE_32BIT
dc821c5f
JB
11230 ? i.base_reg->reg_type.bitfield.dword
11231 : i.base_reg->reg_type.bitfield.word)))
be05d201
L
11232 goto bad_address;
11233
fc0763e6
JB
11234 as_warn (_("`%s' is not valid here (expected `%c%s%s%c')"),
11235 operand_string,
11236 intel_syntax ? '[' : '(',
11237 register_prefix,
be05d201 11238 expected_reg->reg_name,
fc0763e6 11239 intel_syntax ? ']' : ')');
be05d201 11240 return 1;
fc0763e6 11241 }
be05d201
L
11242 else
11243 return 1;
11244
dc1e8a47 11245 bad_address:
be05d201
L
11246 as_bad (_("`%s' is not a valid %s expression"),
11247 operand_string, kind);
11248 return 0;
3e73aa7c
JH
11249 }
11250 else
11251 {
be05d201
L
11252 if (addr_mode != CODE_16BIT)
11253 {
11254 /* 32-bit/64-bit checks. */
41eb8e88
L
11255 if (i.disp_encoding == disp_encoding_16bit)
11256 {
11257 bad_disp:
11258 as_bad (_("invalid `%s' prefix"),
11259 addr_mode == CODE_16BIT ? "{disp32}" : "{disp16}");
11260 return 0;
11261 }
11262
be05d201 11263 if ((i.base_reg
e968fc9b
JB
11264 && ((addr_mode == CODE_64BIT
11265 ? !i.base_reg->reg_type.bitfield.qword
11266 : !i.base_reg->reg_type.bitfield.dword)
11267 || (i.index_reg && i.base_reg->reg_num == RegIP)
11268 || i.base_reg->reg_num == RegIZ))
be05d201 11269 || (i.index_reg
1b54b8d7
JB
11270 && !i.index_reg->reg_type.bitfield.xmmword
11271 && !i.index_reg->reg_type.bitfield.ymmword
11272 && !i.index_reg->reg_type.bitfield.zmmword
be05d201 11273 && ((addr_mode == CODE_64BIT
e968fc9b
JB
11274 ? !i.index_reg->reg_type.bitfield.qword
11275 : !i.index_reg->reg_type.bitfield.dword)
be05d201
L
11276 || !i.index_reg->reg_type.bitfield.baseindex)))
11277 goto bad_address;
8178be5b 11278
260cd341 11279 /* bndmk, bndldx, bndstx and mandatory non-vector SIB have special restrictions. */
a152332d 11280 if ((t->opcode_modifier.opcodeprefix == PREFIX_0XF3
389d00a5
JB
11281 && t->opcode_modifier.opcodespace == SPACE_0F
11282 && t->base_opcode == 0x1b)
a152332d 11283 || (t->opcode_modifier.opcodeprefix == PREFIX_NONE
389d00a5
JB
11284 && t->opcode_modifier.opcodespace == SPACE_0F
11285 && (t->base_opcode & ~1) == 0x1a)
a152332d 11286 || t->opcode_modifier.sib == SIBMEM)
8178be5b
JB
11287 {
11288 /* They cannot use RIP-relative addressing. */
e968fc9b 11289 if (i.base_reg && i.base_reg->reg_num == RegIP)
8178be5b
JB
11290 {
11291 as_bad (_("`%s' cannot be used here"), operand_string);
11292 return 0;
11293 }
11294
11295 /* bndldx and bndstx ignore their scale factor. */
a152332d 11296 if (t->opcode_modifier.opcodeprefix == PREFIX_NONE
389d00a5
JB
11297 && t->opcode_modifier.opcodespace == SPACE_0F
11298 && (t->base_opcode & ~1) == 0x1a
8178be5b
JB
11299 && i.log2_scale_factor)
11300 as_warn (_("register scaling is being ignored here"));
11301 }
be05d201
L
11302 }
11303 else
3e73aa7c 11304 {
be05d201 11305 /* 16-bit checks. */
41eb8e88
L
11306 if (i.disp_encoding == disp_encoding_32bit)
11307 goto bad_disp;
11308
3e73aa7c 11309 if ((i.base_reg
dc821c5f 11310 && (!i.base_reg->reg_type.bitfield.word
40fb9820 11311 || !i.base_reg->reg_type.bitfield.baseindex))
3e73aa7c 11312 || (i.index_reg
dc821c5f 11313 && (!i.index_reg->reg_type.bitfield.word
40fb9820 11314 || !i.index_reg->reg_type.bitfield.baseindex
29b0f896
AM
11315 || !(i.base_reg
11316 && i.base_reg->reg_num < 6
11317 && i.index_reg->reg_num >= 6
11318 && i.log2_scale_factor == 0))))
be05d201 11319 goto bad_address;
3e73aa7c
JH
11320 }
11321 }
be05d201 11322 return 1;
24eab124 11323}
252b5132 11324
43234a1e
L
11325/* Handle vector immediates. */
11326
11327static int
11328RC_SAE_immediate (const char *imm_start)
11329{
43234a1e 11330 const char *pstr = imm_start;
43234a1e
L
11331
11332 if (*pstr != '{')
11333 return 0;
11334
7063667e
JB
11335 pstr = RC_SAE_specifier (pstr + 1);
11336 if (pstr == NULL)
43234a1e
L
11337 return 0;
11338
11339 if (*pstr++ != '}')
11340 {
11341 as_bad (_("Missing '}': '%s'"), imm_start);
11342 return 0;
11343 }
11344 /* RC/SAE immediate string should contain nothing more. */;
11345 if (*pstr != 0)
11346 {
11347 as_bad (_("Junk after '}': '%s'"), imm_start);
11348 return 0;
11349 }
11350
cf665fee
JB
11351 /* Internally this doesn't count as an operand. */
11352 --i.operands;
43234a1e 11353
43234a1e
L
11354 return 1;
11355}
11356
8325cc63
JB
11357/* Only string instructions can have a second memory operand, so
11358 reduce current_templates to just those if it contains any. */
11359static int
11360maybe_adjust_templates (void)
11361{
11362 const insn_template *t;
11363
11364 gas_assert (i.mem_operands == 1);
11365
11366 for (t = current_templates->start; t < current_templates->end; ++t)
11367 if (t->opcode_modifier.isstring)
11368 break;
11369
11370 if (t < current_templates->end)
11371 {
11372 static templates aux_templates;
5b7c81bd 11373 bool recheck;
8325cc63
JB
11374
11375 aux_templates.start = t;
11376 for (; t < current_templates->end; ++t)
11377 if (!t->opcode_modifier.isstring)
11378 break;
11379 aux_templates.end = t;
11380
11381 /* Determine whether to re-check the first memory operand. */
11382 recheck = (aux_templates.start != current_templates->start
11383 || t != current_templates->end);
11384
11385 current_templates = &aux_templates;
11386
11387 if (recheck)
11388 {
11389 i.mem_operands = 0;
11390 if (i.memop1_string != NULL
11391 && i386_index_check (i.memop1_string) == 0)
11392 return 0;
11393 i.mem_operands = 1;
11394 }
11395 }
11396
11397 return 1;
11398}
11399
9d299bea
JB
11400static INLINE bool starts_memory_operand (char c)
11401{
014fbcda 11402 return ISDIGIT (c)
9d299bea 11403 || is_identifier_char (c)
014fbcda 11404 || strchr ("([\"+-!~", c);
9d299bea
JB
11405}
11406
fc0763e6 11407/* Parse OPERAND_STRING into the i386_insn structure I. Returns zero
47926f60 11408 on error. */
252b5132 11409
252b5132 11410static int
a7619375 11411i386_att_operand (char *operand_string)
252b5132 11412{
af6bdddf
AM
11413 const reg_entry *r;
11414 char *end_op;
24eab124 11415 char *op_string = operand_string;
252b5132 11416
24eab124 11417 if (is_space_char (*op_string))
252b5132
RH
11418 ++op_string;
11419
24eab124 11420 /* We check for an absolute prefix (differentiating,
47926f60 11421 for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
24eab124
AM
11422 if (*op_string == ABSOLUTE_PREFIX)
11423 {
11424 ++op_string;
11425 if (is_space_char (*op_string))
11426 ++op_string;
5b7c81bd 11427 i.jumpabsolute = true;
24eab124 11428 }
252b5132 11429
47926f60 11430 /* Check if operand is a register. */
4d1bb795 11431 if ((r = parse_register (op_string, &end_op)) != NULL)
24eab124 11432 {
40fb9820
L
11433 i386_operand_type temp;
11434
8a6fb3f9
JB
11435 if (r == &bad_reg)
11436 return 0;
11437
24eab124
AM
11438 /* Check for a segment override by searching for ':' after a
11439 segment register. */
11440 op_string = end_op;
11441 if (is_space_char (*op_string))
11442 ++op_string;
00cee14f 11443 if (*op_string == ':' && r->reg_type.bitfield.class == SReg)
24eab124 11444 {
5e042380 11445 i.seg[i.mem_operands] = r;
252b5132 11446
24eab124 11447 /* Skip the ':' and whitespace. */
252b5132
RH
11448 ++op_string;
11449 if (is_space_char (*op_string))
24eab124 11450 ++op_string;
252b5132 11451
47926f60 11452 /* Handle case of %es:*foo. */
c8d541e2 11453 if (!i.jumpabsolute && *op_string == ABSOLUTE_PREFIX)
24eab124
AM
11454 {
11455 ++op_string;
11456 if (is_space_char (*op_string))
11457 ++op_string;
5b7c81bd 11458 i.jumpabsolute = true;
24eab124 11459 }
c8d541e2 11460
9d299bea 11461 if (!starts_memory_operand (*op_string))
c8d541e2
JB
11462 {
11463 as_bad (_("bad memory operand `%s'"), op_string);
11464 return 0;
11465 }
24eab124
AM
11466 goto do_memory_reference;
11467 }
43234a1e
L
11468
11469 /* Handle vector operations. */
11470 if (*op_string == '{')
11471 {
f70c6814 11472 op_string = check_VecOperations (op_string);
43234a1e
L
11473 if (op_string == NULL)
11474 return 0;
11475 }
11476
24eab124
AM
11477 if (*op_string)
11478 {
d0b47220 11479 as_bad (_("junk `%s' after register"), op_string);
24eab124
AM
11480 return 0;
11481 }
40fb9820
L
11482 temp = r->reg_type;
11483 temp.bitfield.baseindex = 0;
c6fb90c8
L
11484 i.types[this_operand] = operand_type_or (i.types[this_operand],
11485 temp);
7d5e4556 11486 i.types[this_operand].bitfield.unspecified = 0;
520dc8e8 11487 i.op[this_operand].regs = r;
24eab124 11488 i.reg_operands++;
cf665fee
JB
11489
11490 /* A GPR may follow an RC or SAE immediate only if a (vector) register
11491 operand was also present earlier on. */
11492 if (i.rounding.type != rc_none && temp.bitfield.class == Reg
11493 && i.reg_operands == 1)
11494 {
11495 unsigned int j;
11496
11497 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); ++j)
11498 if (i.rounding.type == RC_NamesTable[j].type)
11499 break;
11500 as_bad (_("`%s': misplaced `{%s}'"),
11501 current_templates->start->name, RC_NamesTable[j].name);
11502 return 0;
11503 }
24eab124 11504 }
af6bdddf
AM
11505 else if (*op_string == REGISTER_PREFIX)
11506 {
11507 as_bad (_("bad register name `%s'"), op_string);
11508 return 0;
11509 }
24eab124 11510 else if (*op_string == IMMEDIATE_PREFIX)
ce8a8b2f 11511 {
24eab124 11512 ++op_string;
6f2f06be 11513 if (i.jumpabsolute)
24eab124 11514 {
d0b47220 11515 as_bad (_("immediate operand illegal with absolute jump"));
24eab124
AM
11516 return 0;
11517 }
11518 if (!i386_immediate (op_string))
11519 return 0;
cf665fee
JB
11520 if (i.rounding.type != rc_none)
11521 {
11522 as_bad (_("`%s': RC/SAE operand must follow immediate operands"),
11523 current_templates->start->name);
11524 return 0;
11525 }
24eab124 11526 }
43234a1e
L
11527 else if (RC_SAE_immediate (operand_string))
11528 {
cf665fee
JB
11529 /* If it is a RC or SAE immediate, do the necessary placement check:
11530 Only another immediate or a GPR may precede it. */
11531 if (i.mem_operands || i.reg_operands + i.imm_operands > 1
11532 || (i.reg_operands == 1
11533 && i.op[0].regs->reg_type.bitfield.class != Reg))
11534 {
11535 as_bad (_("`%s': misplaced `%s'"),
11536 current_templates->start->name, operand_string);
11537 return 0;
11538 }
43234a1e 11539 }
9d299bea 11540 else if (starts_memory_operand (*op_string))
24eab124 11541 {
47926f60 11542 /* This is a memory reference of some sort. */
af6bdddf 11543 char *base_string;
252b5132 11544
47926f60 11545 /* Start and end of displacement string expression (if found). */
eecb386c
AM
11546 char *displacement_string_start;
11547 char *displacement_string_end;
252b5132 11548
24eab124 11549 do_memory_reference:
8325cc63
JB
11550 if (i.mem_operands == 1 && !maybe_adjust_templates ())
11551 return 0;
24eab124 11552 if ((i.mem_operands == 1
40fb9820 11553 && !current_templates->start->opcode_modifier.isstring)
24eab124
AM
11554 || i.mem_operands == 2)
11555 {
11556 as_bad (_("too many memory references for `%s'"),
11557 current_templates->start->name);
11558 return 0;
11559 }
252b5132 11560
24eab124
AM
11561 /* Check for base index form. We detect the base index form by
11562 looking for an ')' at the end of the operand, searching
11563 for the '(' matching it, and finding a REGISTER_PREFIX or ','
11564 after the '('. */
af6bdddf 11565 base_string = op_string + strlen (op_string);
c3332e24 11566
43234a1e 11567 /* Handle vector operations. */
6b5ba0d4
JB
11568 --base_string;
11569 if (is_space_char (*base_string))
11570 --base_string;
11571
11572 if (*base_string == '}')
43234a1e 11573 {
6b5ba0d4
JB
11574 char *vop_start = NULL;
11575
11576 while (base_string-- > op_string)
11577 {
11578 if (*base_string == '"')
11579 break;
11580 if (*base_string != '{')
11581 continue;
11582
11583 vop_start = base_string;
11584
11585 --base_string;
11586 if (is_space_char (*base_string))
11587 --base_string;
11588
11589 if (*base_string != '}')
11590 break;
11591
11592 vop_start = NULL;
11593 }
11594
11595 if (!vop_start)
11596 {
11597 as_bad (_("unbalanced figure braces"));
11598 return 0;
11599 }
11600
f70c6814 11601 if (check_VecOperations (vop_start) == NULL)
43234a1e 11602 return 0;
43234a1e
L
11603 }
11604
47926f60 11605 /* If we only have a displacement, set-up for it to be parsed later. */
af6bdddf
AM
11606 displacement_string_start = op_string;
11607 displacement_string_end = base_string + 1;
252b5132 11608
24eab124
AM
11609 if (*base_string == ')')
11610 {
af6bdddf 11611 char *temp_string;
e87fb6a6
JB
11612 unsigned int parens_not_balanced = 0;
11613 bool in_quotes = false;
e68c3d59 11614
24eab124 11615 /* We've already checked that the number of left & right ()'s are
e87fb6a6
JB
11616 equal, and that there's a matching set of double quotes. */
11617 end_op = base_string;
11618 for (temp_string = op_string; temp_string < end_op; temp_string++)
24eab124 11619 {
e87fb6a6
JB
11620 if (*temp_string == '\\' && temp_string[1] == '"')
11621 ++temp_string;
11622 else if (*temp_string == '"')
11623 in_quotes = !in_quotes;
11624 else if (!in_quotes)
11625 {
11626 if (*temp_string == '(' && !parens_not_balanced++)
11627 base_string = temp_string;
11628 if (*temp_string == ')')
11629 --parens_not_balanced;
11630 }
24eab124 11631 }
c3332e24 11632
af6bdddf 11633 temp_string = base_string;
c3332e24 11634
24eab124 11635 /* Skip past '(' and whitespace. */
e87fb6a6
JB
11636 gas_assert (*base_string == '(');
11637 ++base_string;
252b5132 11638 if (is_space_char (*base_string))
24eab124 11639 ++base_string;
252b5132 11640
af6bdddf 11641 if (*base_string == ','
4eed87de
AM
11642 || ((i.base_reg = parse_register (base_string, &end_op))
11643 != NULL))
252b5132 11644 {
af6bdddf 11645 displacement_string_end = temp_string;
252b5132 11646
40fb9820 11647 i.types[this_operand].bitfield.baseindex = 1;
252b5132 11648
af6bdddf 11649 if (i.base_reg)
24eab124 11650 {
8a6fb3f9
JB
11651 if (i.base_reg == &bad_reg)
11652 return 0;
24eab124
AM
11653 base_string = end_op;
11654 if (is_space_char (*base_string))
11655 ++base_string;
af6bdddf
AM
11656 }
11657
11658 /* There may be an index reg or scale factor here. */
11659 if (*base_string == ',')
11660 {
11661 ++base_string;
11662 if (is_space_char (*base_string))
11663 ++base_string;
11664
4eed87de
AM
11665 if ((i.index_reg = parse_register (base_string, &end_op))
11666 != NULL)
24eab124 11667 {
8a6fb3f9
JB
11668 if (i.index_reg == &bad_reg)
11669 return 0;
af6bdddf 11670 base_string = end_op;
24eab124
AM
11671 if (is_space_char (*base_string))
11672 ++base_string;
af6bdddf
AM
11673 if (*base_string == ',')
11674 {
11675 ++base_string;
11676 if (is_space_char (*base_string))
11677 ++base_string;
11678 }
e5cb08ac 11679 else if (*base_string != ')')
af6bdddf 11680 {
4eed87de
AM
11681 as_bad (_("expecting `,' or `)' "
11682 "after index register in `%s'"),
af6bdddf
AM
11683 operand_string);
11684 return 0;
11685 }
24eab124 11686 }
af6bdddf 11687 else if (*base_string == REGISTER_PREFIX)
24eab124 11688 {
f76bf5e0
L
11689 end_op = strchr (base_string, ',');
11690 if (end_op)
11691 *end_op = '\0';
af6bdddf 11692 as_bad (_("bad register name `%s'"), base_string);
24eab124
AM
11693 return 0;
11694 }
252b5132 11695
47926f60 11696 /* Check for scale factor. */
551c1ca1 11697 if (*base_string != ')')
af6bdddf 11698 {
551c1ca1
AM
11699 char *end_scale = i386_scale (base_string);
11700
11701 if (!end_scale)
af6bdddf 11702 return 0;
24eab124 11703
551c1ca1 11704 base_string = end_scale;
af6bdddf
AM
11705 if (is_space_char (*base_string))
11706 ++base_string;
11707 if (*base_string != ')')
11708 {
4eed87de
AM
11709 as_bad (_("expecting `)' "
11710 "after scale factor in `%s'"),
af6bdddf
AM
11711 operand_string);
11712 return 0;
11713 }
11714 }
11715 else if (!i.index_reg)
24eab124 11716 {
4eed87de
AM
11717 as_bad (_("expecting index register or scale factor "
11718 "after `,'; got '%c'"),
af6bdddf 11719 *base_string);
24eab124
AM
11720 return 0;
11721 }
11722 }
af6bdddf 11723 else if (*base_string != ')')
24eab124 11724 {
4eed87de
AM
11725 as_bad (_("expecting `,' or `)' "
11726 "after base register in `%s'"),
af6bdddf 11727 operand_string);
24eab124
AM
11728 return 0;
11729 }
c3332e24 11730 }
af6bdddf 11731 else if (*base_string == REGISTER_PREFIX)
c3332e24 11732 {
f76bf5e0
L
11733 end_op = strchr (base_string, ',');
11734 if (end_op)
11735 *end_op = '\0';
af6bdddf 11736 as_bad (_("bad register name `%s'"), base_string);
24eab124 11737 return 0;
c3332e24 11738 }
24eab124
AM
11739 }
11740
11741 /* If there's an expression beginning the operand, parse it,
11742 assuming displacement_string_start and
11743 displacement_string_end are meaningful. */
11744 if (displacement_string_start != displacement_string_end)
11745 {
11746 if (!i386_displacement (displacement_string_start,
11747 displacement_string_end))
11748 return 0;
11749 }
11750
11751 /* Special case for (%dx) while doing input/output op. */
11752 if (i.base_reg
75e5731b
JB
11753 && i.base_reg->reg_type.bitfield.instance == RegD
11754 && i.base_reg->reg_type.bitfield.word
24eab124
AM
11755 && i.index_reg == 0
11756 && i.log2_scale_factor == 0
11757 && i.seg[i.mem_operands] == 0
40fb9820 11758 && !operand_type_check (i.types[this_operand], disp))
24eab124 11759 {
2fb5be8d 11760 i.types[this_operand] = i.base_reg->reg_type;
24eab124
AM
11761 return 1;
11762 }
11763
eecb386c
AM
11764 if (i386_index_check (operand_string) == 0)
11765 return 0;
c48dadc9 11766 i.flags[this_operand] |= Operand_Mem;
8325cc63
JB
11767 if (i.mem_operands == 0)
11768 i.memop1_string = xstrdup (operand_string);
24eab124
AM
11769 i.mem_operands++;
11770 }
11771 else
ce8a8b2f
AM
11772 {
11773 /* It's not a memory operand; argh! */
24eab124
AM
11774 as_bad (_("invalid char %s beginning operand %d `%s'"),
11775 output_invalid (*op_string),
11776 this_operand + 1,
11777 op_string);
11778 return 0;
11779 }
47926f60 11780 return 1; /* Normal return. */
252b5132
RH
11781}
11782\f
fa94de6b
RM
11783/* Calculate the maximum variable size (i.e., excluding fr_fix)
11784 that an rs_machine_dependent frag may reach. */
11785
11786unsigned int
11787i386_frag_max_var (fragS *frag)
11788{
11789 /* The only relaxable frags are for jumps.
11790 Unconditional jumps can grow by 4 bytes and others by 5 bytes. */
11791 gas_assert (frag->fr_type == rs_machine_dependent);
11792 return TYPE_FROM_RELAX_STATE (frag->fr_subtype) == UNCOND_JUMP ? 4 : 5;
11793}
11794
b084df0b
L
11795#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
11796static int
8dcea932 11797elf_symbol_resolved_in_segment_p (symbolS *fr_symbol, offsetT fr_var)
b084df0b
L
11798{
11799 /* STT_GNU_IFUNC symbol must go through PLT. */
11800 if ((symbol_get_bfdsym (fr_symbol)->flags
11801 & BSF_GNU_INDIRECT_FUNCTION) != 0)
11802 return 0;
11803
11804 if (!S_IS_EXTERNAL (fr_symbol))
11805 /* Symbol may be weak or local. */
11806 return !S_IS_WEAK (fr_symbol);
11807
8dcea932
L
11808 /* Global symbols with non-default visibility can't be preempted. */
11809 if (ELF_ST_VISIBILITY (S_GET_OTHER (fr_symbol)) != STV_DEFAULT)
11810 return 1;
11811
11812 if (fr_var != NO_RELOC)
11813 switch ((enum bfd_reloc_code_real) fr_var)
11814 {
11815 case BFD_RELOC_386_PLT32:
11816 case BFD_RELOC_X86_64_PLT32:
33eaf5de 11817 /* Symbol with PLT relocation may be preempted. */
8dcea932
L
11818 return 0;
11819 default:
11820 abort ();
11821 }
11822
b084df0b
L
11823 /* Global symbols with default visibility in a shared library may be
11824 preempted by another definition. */
8dcea932 11825 return !shared;
b084df0b
L
11826}
11827#endif
11828
79d72f45
HL
11829/* Table 3-2. Macro-Fusible Instructions in Haswell Microarchitecture
11830 Note also work for Skylake and Cascadelake.
11831---------------------------------------------------------------------
11832| JCC | ADD/SUB/CMP | INC/DEC | TEST/AND |
11833| ------ | ----------- | ------- | -------- |
11834| Jo | N | N | Y |
11835| Jno | N | N | Y |
11836| Jc/Jb | Y | N | Y |
11837| Jae/Jnb | Y | N | Y |
11838| Je/Jz | Y | Y | Y |
11839| Jne/Jnz | Y | Y | Y |
11840| Jna/Jbe | Y | N | Y |
11841| Ja/Jnbe | Y | N | Y |
11842| Js | N | N | Y |
11843| Jns | N | N | Y |
11844| Jp/Jpe | N | N | Y |
11845| Jnp/Jpo | N | N | Y |
11846| Jl/Jnge | Y | Y | Y |
11847| Jge/Jnl | Y | Y | Y |
11848| Jle/Jng | Y | Y | Y |
11849| Jg/Jnle | Y | Y | Y |
11850--------------------------------------------------------------------- */
11851static int
11852i386_macro_fusible_p (enum mf_cmp_kind mf_cmp, enum mf_jcc_kind mf_jcc)
11853{
11854 if (mf_cmp == mf_cmp_alu_cmp)
11855 return ((mf_jcc >= mf_jcc_jc && mf_jcc <= mf_jcc_jna)
11856 || mf_jcc == mf_jcc_jl || mf_jcc == mf_jcc_jle);
11857 if (mf_cmp == mf_cmp_incdec)
11858 return (mf_jcc == mf_jcc_je || mf_jcc == mf_jcc_jl
11859 || mf_jcc == mf_jcc_jle);
11860 if (mf_cmp == mf_cmp_test_and)
11861 return 1;
11862 return 0;
11863}
11864
e379e5f3
L
11865/* Return the next non-empty frag. */
11866
11867static fragS *
11868i386_next_non_empty_frag (fragS *fragP)
11869{
11870 /* There may be a frag with a ".fill 0" when there is no room in
11871 the current frag for frag_grow in output_insn. */
11872 for (fragP = fragP->fr_next;
11873 (fragP != NULL
11874 && fragP->fr_type == rs_fill
11875 && fragP->fr_fix == 0);
11876 fragP = fragP->fr_next)
11877 ;
11878 return fragP;
11879}
11880
11881/* Return the next jcc frag after BRANCH_PADDING. */
11882
11883static fragS *
79d72f45 11884i386_next_fusible_jcc_frag (fragS *maybe_cmp_fragP, fragS *pad_fragP)
e379e5f3 11885{
79d72f45
HL
11886 fragS *branch_fragP;
11887 if (!pad_fragP)
e379e5f3
L
11888 return NULL;
11889
79d72f45
HL
11890 if (pad_fragP->fr_type == rs_machine_dependent
11891 && (TYPE_FROM_RELAX_STATE (pad_fragP->fr_subtype)
e379e5f3
L
11892 == BRANCH_PADDING))
11893 {
79d72f45
HL
11894 branch_fragP = i386_next_non_empty_frag (pad_fragP);
11895 if (branch_fragP->fr_type != rs_machine_dependent)
e379e5f3 11896 return NULL;
79d72f45
HL
11897 if (TYPE_FROM_RELAX_STATE (branch_fragP->fr_subtype) == COND_JUMP
11898 && i386_macro_fusible_p (maybe_cmp_fragP->tc_frag_data.mf_type,
11899 pad_fragP->tc_frag_data.mf_type))
11900 return branch_fragP;
e379e5f3
L
11901 }
11902
11903 return NULL;
11904}
11905
11906/* Classify BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags. */
11907
11908static void
11909i386_classify_machine_dependent_frag (fragS *fragP)
11910{
11911 fragS *cmp_fragP;
11912 fragS *pad_fragP;
11913 fragS *branch_fragP;
11914 fragS *next_fragP;
11915 unsigned int max_prefix_length;
11916
11917 if (fragP->tc_frag_data.classified)
11918 return;
11919
11920 /* First scan for BRANCH_PADDING and FUSED_JCC_PADDING. Convert
11921 FUSED_JCC_PADDING and merge BRANCH_PADDING. */
11922 for (next_fragP = fragP;
11923 next_fragP != NULL;
11924 next_fragP = next_fragP->fr_next)
11925 {
11926 next_fragP->tc_frag_data.classified = 1;
11927 if (next_fragP->fr_type == rs_machine_dependent)
11928 switch (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype))
11929 {
11930 case BRANCH_PADDING:
11931 /* The BRANCH_PADDING frag must be followed by a branch
11932 frag. */
11933 branch_fragP = i386_next_non_empty_frag (next_fragP);
11934 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
11935 break;
11936 case FUSED_JCC_PADDING:
11937 /* Check if this is a fused jcc:
11938 FUSED_JCC_PADDING
11939 CMP like instruction
11940 BRANCH_PADDING
11941 COND_JUMP
11942 */
11943 cmp_fragP = i386_next_non_empty_frag (next_fragP);
11944 pad_fragP = i386_next_non_empty_frag (cmp_fragP);
79d72f45 11945 branch_fragP = i386_next_fusible_jcc_frag (next_fragP, pad_fragP);
e379e5f3
L
11946 if (branch_fragP)
11947 {
11948 /* The BRANCH_PADDING frag is merged with the
11949 FUSED_JCC_PADDING frag. */
11950 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
11951 /* CMP like instruction size. */
11952 next_fragP->tc_frag_data.cmp_size = cmp_fragP->fr_fix;
11953 frag_wane (pad_fragP);
11954 /* Skip to branch_fragP. */
11955 next_fragP = branch_fragP;
11956 }
11957 else if (next_fragP->tc_frag_data.max_prefix_length)
11958 {
11959 /* Turn FUSED_JCC_PADDING into BRANCH_PREFIX if it isn't
11960 a fused jcc. */
11961 next_fragP->fr_subtype
11962 = ENCODE_RELAX_STATE (BRANCH_PREFIX, 0);
11963 next_fragP->tc_frag_data.max_bytes
11964 = next_fragP->tc_frag_data.max_prefix_length;
11965 /* This will be updated in the BRANCH_PREFIX scan. */
11966 next_fragP->tc_frag_data.max_prefix_length = 0;
11967 }
11968 else
11969 frag_wane (next_fragP);
11970 break;
11971 }
11972 }
11973
11974 /* Stop if there is no BRANCH_PREFIX. */
11975 if (!align_branch_prefix_size)
11976 return;
11977
11978 /* Scan for BRANCH_PREFIX. */
11979 for (; fragP != NULL; fragP = fragP->fr_next)
11980 {
11981 if (fragP->fr_type != rs_machine_dependent
11982 || (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
11983 != BRANCH_PREFIX))
11984 continue;
11985
11986 /* Count all BRANCH_PREFIX frags before BRANCH_PADDING and
11987 COND_JUMP_PREFIX. */
11988 max_prefix_length = 0;
11989 for (next_fragP = fragP;
11990 next_fragP != NULL;
11991 next_fragP = next_fragP->fr_next)
11992 {
11993 if (next_fragP->fr_type == rs_fill)
11994 /* Skip rs_fill frags. */
11995 continue;
11996 else if (next_fragP->fr_type != rs_machine_dependent)
11997 /* Stop for all other frags. */
11998 break;
11999
12000 /* rs_machine_dependent frags. */
12001 if (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12002 == BRANCH_PREFIX)
12003 {
12004 /* Count BRANCH_PREFIX frags. */
12005 if (max_prefix_length >= MAX_FUSED_JCC_PADDING_SIZE)
12006 {
12007 max_prefix_length = MAX_FUSED_JCC_PADDING_SIZE;
12008 frag_wane (next_fragP);
12009 }
12010 else
12011 max_prefix_length
12012 += next_fragP->tc_frag_data.max_bytes;
12013 }
12014 else if ((TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12015 == BRANCH_PADDING)
12016 || (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12017 == FUSED_JCC_PADDING))
12018 {
12019 /* Stop at BRANCH_PADDING and FUSED_JCC_PADDING. */
12020 fragP->tc_frag_data.u.padding_fragP = next_fragP;
12021 break;
12022 }
12023 else
12024 /* Stop for other rs_machine_dependent frags. */
12025 break;
12026 }
12027
12028 fragP->tc_frag_data.max_prefix_length = max_prefix_length;
12029
12030 /* Skip to the next frag. */
12031 fragP = next_fragP;
12032 }
12033}
12034
12035/* Compute padding size for
12036
12037 FUSED_JCC_PADDING
12038 CMP like instruction
12039 BRANCH_PADDING
12040 COND_JUMP/UNCOND_JUMP
12041
12042 or
12043
12044 BRANCH_PADDING
12045 COND_JUMP/UNCOND_JUMP
12046 */
12047
12048static int
12049i386_branch_padding_size (fragS *fragP, offsetT address)
12050{
12051 unsigned int offset, size, padding_size;
12052 fragS *branch_fragP = fragP->tc_frag_data.u.branch_fragP;
12053
12054 /* The start address of the BRANCH_PADDING or FUSED_JCC_PADDING frag. */
12055 if (!address)
12056 address = fragP->fr_address;
12057 address += fragP->fr_fix;
12058
12059 /* CMP like instrunction size. */
12060 size = fragP->tc_frag_data.cmp_size;
12061
12062 /* The base size of the branch frag. */
12063 size += branch_fragP->fr_fix;
12064
12065 /* Add opcode and displacement bytes for the rs_machine_dependent
12066 branch frag. */
12067 if (branch_fragP->fr_type == rs_machine_dependent)
12068 size += md_relax_table[branch_fragP->fr_subtype].rlx_length;
12069
12070 /* Check if branch is within boundary and doesn't end at the last
12071 byte. */
12072 offset = address & ((1U << align_branch_power) - 1);
12073 if ((offset + size) >= (1U << align_branch_power))
12074 /* Padding needed to avoid crossing boundary. */
12075 padding_size = (1U << align_branch_power) - offset;
12076 else
12077 /* No padding needed. */
12078 padding_size = 0;
12079
12080 /* The return value may be saved in tc_frag_data.length which is
12081 unsigned byte. */
12082 if (!fits_in_unsigned_byte (padding_size))
12083 abort ();
12084
12085 return padding_size;
12086}
12087
12088/* i386_generic_table_relax_frag()
12089
12090 Handle BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags to
12091 grow/shrink padding to align branch frags. Hand others to
12092 relax_frag(). */
12093
12094long
12095i386_generic_table_relax_frag (segT segment, fragS *fragP, long stretch)
12096{
12097 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12098 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
12099 {
12100 long padding_size = i386_branch_padding_size (fragP, 0);
12101 long grow = padding_size - fragP->tc_frag_data.length;
12102
12103 /* When the BRANCH_PREFIX frag is used, the computed address
12104 must match the actual address and there should be no padding. */
12105 if (fragP->tc_frag_data.padding_address
12106 && (fragP->tc_frag_data.padding_address != fragP->fr_address
12107 || padding_size))
12108 abort ();
12109
12110 /* Update the padding size. */
12111 if (grow)
12112 fragP->tc_frag_data.length = padding_size;
12113
12114 return grow;
12115 }
12116 else if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12117 {
12118 fragS *padding_fragP, *next_fragP;
12119 long padding_size, left_size, last_size;
12120
12121 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
12122 if (!padding_fragP)
12123 /* Use the padding set by the leading BRANCH_PREFIX frag. */
12124 return (fragP->tc_frag_data.length
12125 - fragP->tc_frag_data.last_length);
12126
12127 /* Compute the relative address of the padding frag in the very
12128 first time where the BRANCH_PREFIX frag sizes are zero. */
12129 if (!fragP->tc_frag_data.padding_address)
12130 fragP->tc_frag_data.padding_address
12131 = padding_fragP->fr_address - (fragP->fr_address - stretch);
12132
12133 /* First update the last length from the previous interation. */
12134 left_size = fragP->tc_frag_data.prefix_length;
12135 for (next_fragP = fragP;
12136 next_fragP != padding_fragP;
12137 next_fragP = next_fragP->fr_next)
12138 if (next_fragP->fr_type == rs_machine_dependent
12139 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12140 == BRANCH_PREFIX))
12141 {
12142 if (left_size)
12143 {
12144 int max = next_fragP->tc_frag_data.max_bytes;
12145 if (max)
12146 {
12147 int size;
12148 if (max > left_size)
12149 size = left_size;
12150 else
12151 size = max;
12152 left_size -= size;
12153 next_fragP->tc_frag_data.last_length = size;
12154 }
12155 }
12156 else
12157 next_fragP->tc_frag_data.last_length = 0;
12158 }
12159
12160 /* Check the padding size for the padding frag. */
12161 padding_size = i386_branch_padding_size
12162 (padding_fragP, (fragP->fr_address
12163 + fragP->tc_frag_data.padding_address));
12164
12165 last_size = fragP->tc_frag_data.prefix_length;
12166 /* Check if there is change from the last interation. */
12167 if (padding_size == last_size)
12168 {
12169 /* Update the expected address of the padding frag. */
12170 padding_fragP->tc_frag_data.padding_address
12171 = (fragP->fr_address + padding_size
12172 + fragP->tc_frag_data.padding_address);
12173 return 0;
12174 }
12175
12176 if (padding_size > fragP->tc_frag_data.max_prefix_length)
12177 {
12178 /* No padding if there is no sufficient room. Clear the
12179 expected address of the padding frag. */
12180 padding_fragP->tc_frag_data.padding_address = 0;
12181 padding_size = 0;
12182 }
12183 else
12184 /* Store the expected address of the padding frag. */
12185 padding_fragP->tc_frag_data.padding_address
12186 = (fragP->fr_address + padding_size
12187 + fragP->tc_frag_data.padding_address);
12188
12189 fragP->tc_frag_data.prefix_length = padding_size;
12190
12191 /* Update the length for the current interation. */
12192 left_size = padding_size;
12193 for (next_fragP = fragP;
12194 next_fragP != padding_fragP;
12195 next_fragP = next_fragP->fr_next)
12196 if (next_fragP->fr_type == rs_machine_dependent
12197 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12198 == BRANCH_PREFIX))
12199 {
12200 if (left_size)
12201 {
12202 int max = next_fragP->tc_frag_data.max_bytes;
12203 if (max)
12204 {
12205 int size;
12206 if (max > left_size)
12207 size = left_size;
12208 else
12209 size = max;
12210 left_size -= size;
12211 next_fragP->tc_frag_data.length = size;
12212 }
12213 }
12214 else
12215 next_fragP->tc_frag_data.length = 0;
12216 }
12217
12218 return (fragP->tc_frag_data.length
12219 - fragP->tc_frag_data.last_length);
12220 }
12221 return relax_frag (segment, fragP, stretch);
12222}
12223
ee7fcc42
AM
12224/* md_estimate_size_before_relax()
12225
12226 Called just before relax() for rs_machine_dependent frags. The x86
12227 assembler uses these frags to handle variable size jump
12228 instructions.
12229
12230 Any symbol that is now undefined will not become defined.
12231 Return the correct fr_subtype in the frag.
12232 Return the initial "guess for variable size of frag" to caller.
12233 The guess is actually the growth beyond the fixed part. Whatever
12234 we do to grow the fixed or variable part contributes to our
12235 returned value. */
12236
252b5132 12237int
7016a5d5 12238md_estimate_size_before_relax (fragS *fragP, segT segment)
252b5132 12239{
e379e5f3
L
12240 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12241 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX
12242 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
12243 {
12244 i386_classify_machine_dependent_frag (fragP);
12245 return fragP->tc_frag_data.length;
12246 }
12247
252b5132 12248 /* We've already got fragP->fr_subtype right; all we have to do is
b98ef147
AM
12249 check for un-relaxable symbols. On an ELF system, we can't relax
12250 an externally visible symbol, because it may be overridden by a
12251 shared library. */
12252 if (S_GET_SEGMENT (fragP->fr_symbol) != segment
6d249963 12253#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12254 || (IS_ELF
8dcea932
L
12255 && !elf_symbol_resolved_in_segment_p (fragP->fr_symbol,
12256 fragP->fr_var))
fbeb56a4
DK
12257#endif
12258#if defined (OBJ_COFF) && defined (TE_PE)
7ab9ffdd 12259 || (OUTPUT_FLAVOR == bfd_target_coff_flavour
fbeb56a4 12260 && S_IS_WEAK (fragP->fr_symbol))
b98ef147
AM
12261#endif
12262 )
252b5132 12263 {
b98ef147
AM
12264 /* Symbol is undefined in this segment, or we need to keep a
12265 reloc so that weak symbols can be overridden. */
12266 int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
f86103b7 12267 enum bfd_reloc_code_real reloc_type;
ee7fcc42
AM
12268 unsigned char *opcode;
12269 int old_fr_fix;
eb19308f 12270 fixS *fixP = NULL;
f6af82bd 12271
ee7fcc42 12272 if (fragP->fr_var != NO_RELOC)
1e9cc1c2 12273 reloc_type = (enum bfd_reloc_code_real) fragP->fr_var;
b98ef147 12274 else if (size == 2)
f6af82bd 12275 reloc_type = BFD_RELOC_16_PCREL;
bd7ab16b 12276#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1ef3994a
JB
12277 else if (fragP->tc_frag_data.code64 && fragP->fr_offset == 0
12278 && need_plt32_p (fragP->fr_symbol))
bd7ab16b
L
12279 reloc_type = BFD_RELOC_X86_64_PLT32;
12280#endif
f6af82bd
AM
12281 else
12282 reloc_type = BFD_RELOC_32_PCREL;
252b5132 12283
ee7fcc42
AM
12284 old_fr_fix = fragP->fr_fix;
12285 opcode = (unsigned char *) fragP->fr_opcode;
12286
fddf5b5b 12287 switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
252b5132 12288 {
fddf5b5b
AM
12289 case UNCOND_JUMP:
12290 /* Make jmp (0xeb) a (d)word displacement jump. */
47926f60 12291 opcode[0] = 0xe9;
252b5132 12292 fragP->fr_fix += size;
eb19308f
JB
12293 fixP = fix_new (fragP, old_fr_fix, size,
12294 fragP->fr_symbol,
12295 fragP->fr_offset, 1,
12296 reloc_type);
252b5132
RH
12297 break;
12298
fddf5b5b 12299 case COND_JUMP86:
412167cb
AM
12300 if (size == 2
12301 && (!no_cond_jump_promotion || fragP->fr_var != NO_RELOC))
fddf5b5b
AM
12302 {
12303 /* Negate the condition, and branch past an
12304 unconditional jump. */
12305 opcode[0] ^= 1;
12306 opcode[1] = 3;
12307 /* Insert an unconditional jump. */
12308 opcode[2] = 0xe9;
12309 /* We added two extra opcode bytes, and have a two byte
12310 offset. */
12311 fragP->fr_fix += 2 + 2;
062cd5e7
AS
12312 fix_new (fragP, old_fr_fix + 2, 2,
12313 fragP->fr_symbol,
12314 fragP->fr_offset, 1,
12315 reloc_type);
fddf5b5b
AM
12316 break;
12317 }
12318 /* Fall through. */
12319
12320 case COND_JUMP:
412167cb
AM
12321 if (no_cond_jump_promotion && fragP->fr_var == NO_RELOC)
12322 {
12323 fragP->fr_fix += 1;
3e02c1cc
AM
12324 fixP = fix_new (fragP, old_fr_fix, 1,
12325 fragP->fr_symbol,
12326 fragP->fr_offset, 1,
12327 BFD_RELOC_8_PCREL);
12328 fixP->fx_signed = 1;
412167cb
AM
12329 break;
12330 }
93c2a809 12331
24eab124 12332 /* This changes the byte-displacement jump 0x7N
fddf5b5b 12333 to the (d)word-displacement jump 0x0f,0x8N. */
252b5132 12334 opcode[1] = opcode[0] + 0x10;
f6af82bd 12335 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
47926f60
KH
12336 /* We've added an opcode byte. */
12337 fragP->fr_fix += 1 + size;
eb19308f
JB
12338 fixP = fix_new (fragP, old_fr_fix + 1, size,
12339 fragP->fr_symbol,
12340 fragP->fr_offset, 1,
12341 reloc_type);
252b5132 12342 break;
fddf5b5b
AM
12343
12344 default:
12345 BAD_CASE (fragP->fr_subtype);
12346 break;
252b5132 12347 }
eb19308f
JB
12348
12349 /* All jumps handled here are signed, but don't unconditionally use a
12350 signed limit check for 32 and 16 bit jumps as we want to allow wrap
12351 around at 4G (outside of 64-bit mode) and 64k. */
12352 if (size == 4 && flag_code == CODE_64BIT)
12353 fixP->fx_signed = 1;
12354
252b5132 12355 frag_wane (fragP);
ee7fcc42 12356 return fragP->fr_fix - old_fr_fix;
252b5132 12357 }
93c2a809 12358
93c2a809
AM
12359 /* Guess size depending on current relax state. Initially the relax
12360 state will correspond to a short jump and we return 1, because
12361 the variable part of the frag (the branch offset) is one byte
12362 long. However, we can relax a section more than once and in that
12363 case we must either set fr_subtype back to the unrelaxed state,
12364 or return the value for the appropriate branch. */
12365 return md_relax_table[fragP->fr_subtype].rlx_length;
ee7fcc42
AM
12366}
12367
47926f60
KH
12368/* Called after relax() is finished.
12369
12370 In: Address of frag.
12371 fr_type == rs_machine_dependent.
12372 fr_subtype is what the address relaxed to.
12373
12374 Out: Any fixSs and constants are set up.
12375 Caller will turn frag into a ".space 0". */
12376
252b5132 12377void
7016a5d5
TG
12378md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec ATTRIBUTE_UNUSED,
12379 fragS *fragP)
252b5132 12380{
29b0f896 12381 unsigned char *opcode;
252b5132 12382 unsigned char *where_to_put_displacement = NULL;
847f7ad4
AM
12383 offsetT target_address;
12384 offsetT opcode_address;
252b5132 12385 unsigned int extension = 0;
847f7ad4 12386 offsetT displacement_from_opcode_start;
252b5132 12387
e379e5f3
L
12388 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12389 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING
12390 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12391 {
12392 /* Generate nop padding. */
12393 unsigned int size = fragP->tc_frag_data.length;
12394 if (size)
12395 {
12396 if (size > fragP->tc_frag_data.max_bytes)
12397 abort ();
12398
12399 if (flag_debug)
12400 {
12401 const char *msg;
12402 const char *branch = "branch";
12403 const char *prefix = "";
12404 fragS *padding_fragP;
12405 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
12406 == BRANCH_PREFIX)
12407 {
12408 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
12409 switch (fragP->tc_frag_data.default_prefix)
12410 {
12411 default:
12412 abort ();
12413 break;
12414 case CS_PREFIX_OPCODE:
12415 prefix = " cs";
12416 break;
12417 case DS_PREFIX_OPCODE:
12418 prefix = " ds";
12419 break;
12420 case ES_PREFIX_OPCODE:
12421 prefix = " es";
12422 break;
12423 case FS_PREFIX_OPCODE:
12424 prefix = " fs";
12425 break;
12426 case GS_PREFIX_OPCODE:
12427 prefix = " gs";
12428 break;
12429 case SS_PREFIX_OPCODE:
12430 prefix = " ss";
12431 break;
12432 }
12433 if (padding_fragP)
12434 msg = _("%s:%u: add %d%s at 0x%llx to align "
12435 "%s within %d-byte boundary\n");
12436 else
12437 msg = _("%s:%u: add additional %d%s at 0x%llx to "
12438 "align %s within %d-byte boundary\n");
12439 }
12440 else
12441 {
12442 padding_fragP = fragP;
12443 msg = _("%s:%u: add %d%s-byte nop at 0x%llx to align "
12444 "%s within %d-byte boundary\n");
12445 }
12446
12447 if (padding_fragP)
12448 switch (padding_fragP->tc_frag_data.branch_type)
12449 {
12450 case align_branch_jcc:
12451 branch = "jcc";
12452 break;
12453 case align_branch_fused:
12454 branch = "fused jcc";
12455 break;
12456 case align_branch_jmp:
12457 branch = "jmp";
12458 break;
12459 case align_branch_call:
12460 branch = "call";
12461 break;
12462 case align_branch_indirect:
12463 branch = "indiret branch";
12464 break;
12465 case align_branch_ret:
12466 branch = "ret";
12467 break;
12468 default:
12469 break;
12470 }
12471
12472 fprintf (stdout, msg,
12473 fragP->fr_file, fragP->fr_line, size, prefix,
12474 (long long) fragP->fr_address, branch,
12475 1 << align_branch_power);
12476 }
12477 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12478 memset (fragP->fr_opcode,
12479 fragP->tc_frag_data.default_prefix, size);
12480 else
12481 i386_generate_nops (fragP, (char *) fragP->fr_opcode,
12482 size, 0);
12483 fragP->fr_fix += size;
12484 }
12485 return;
12486 }
12487
252b5132
RH
12488 opcode = (unsigned char *) fragP->fr_opcode;
12489
47926f60 12490 /* Address we want to reach in file space. */
252b5132 12491 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
252b5132 12492
47926f60 12493 /* Address opcode resides at in file space. */
252b5132
RH
12494 opcode_address = fragP->fr_address + fragP->fr_fix;
12495
47926f60 12496 /* Displacement from opcode start to fill into instruction. */
252b5132
RH
12497 displacement_from_opcode_start = target_address - opcode_address;
12498
fddf5b5b 12499 if ((fragP->fr_subtype & BIG) == 0)
252b5132 12500 {
47926f60
KH
12501 /* Don't have to change opcode. */
12502 extension = 1; /* 1 opcode + 1 displacement */
252b5132 12503 where_to_put_displacement = &opcode[1];
fddf5b5b
AM
12504 }
12505 else
12506 {
12507 if (no_cond_jump_promotion
12508 && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
4eed87de
AM
12509 as_warn_where (fragP->fr_file, fragP->fr_line,
12510 _("long jump required"));
252b5132 12511
fddf5b5b
AM
12512 switch (fragP->fr_subtype)
12513 {
12514 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
12515 extension = 4; /* 1 opcode + 4 displacement */
12516 opcode[0] = 0xe9;
12517 where_to_put_displacement = &opcode[1];
12518 break;
252b5132 12519
fddf5b5b
AM
12520 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
12521 extension = 2; /* 1 opcode + 2 displacement */
12522 opcode[0] = 0xe9;
12523 where_to_put_displacement = &opcode[1];
12524 break;
252b5132 12525
fddf5b5b
AM
12526 case ENCODE_RELAX_STATE (COND_JUMP, BIG):
12527 case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
12528 extension = 5; /* 2 opcode + 4 displacement */
12529 opcode[1] = opcode[0] + 0x10;
12530 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12531 where_to_put_displacement = &opcode[2];
12532 break;
252b5132 12533
fddf5b5b
AM
12534 case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
12535 extension = 3; /* 2 opcode + 2 displacement */
12536 opcode[1] = opcode[0] + 0x10;
12537 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12538 where_to_put_displacement = &opcode[2];
12539 break;
252b5132 12540
fddf5b5b
AM
12541 case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
12542 extension = 4;
12543 opcode[0] ^= 1;
12544 opcode[1] = 3;
12545 opcode[2] = 0xe9;
12546 where_to_put_displacement = &opcode[3];
12547 break;
12548
12549 default:
12550 BAD_CASE (fragP->fr_subtype);
12551 break;
12552 }
252b5132 12553 }
fddf5b5b 12554
7b81dfbb
AJ
12555 /* If size if less then four we are sure that the operand fits,
12556 but if it's 4, then it could be that the displacement is larger
12557 then -/+ 2GB. */
12558 if (DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype) == 4
12559 && object_64bit
12560 && ((addressT) (displacement_from_opcode_start - extension
4eed87de
AM
12561 + ((addressT) 1 << 31))
12562 > (((addressT) 2 << 31) - 1)))
7b81dfbb
AJ
12563 {
12564 as_bad_where (fragP->fr_file, fragP->fr_line,
12565 _("jump target out of range"));
12566 /* Make us emit 0. */
12567 displacement_from_opcode_start = extension;
12568 }
47926f60 12569 /* Now put displacement after opcode. */
252b5132
RH
12570 md_number_to_chars ((char *) where_to_put_displacement,
12571 (valueT) (displacement_from_opcode_start - extension),
fddf5b5b 12572 DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
252b5132
RH
12573 fragP->fr_fix += extension;
12574}
12575\f
7016a5d5 12576/* Apply a fixup (fixP) to segment data, once it has been determined
252b5132
RH
12577 by our caller that we have all the info we need to fix it up.
12578
7016a5d5
TG
12579 Parameter valP is the pointer to the value of the bits.
12580
252b5132
RH
12581 On the 386, immediates, displacements, and data pointers are all in
12582 the same (little-endian) format, so we don't need to care about which
12583 we are handling. */
12584
94f592af 12585void
7016a5d5 12586md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 12587{
94f592af 12588 char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
c6682705 12589 valueT value = *valP;
252b5132 12590
f86103b7 12591#if !defined (TE_Mach)
93382f6d
AM
12592 if (fixP->fx_pcrel)
12593 {
12594 switch (fixP->fx_r_type)
12595 {
5865bb77
ILT
12596 default:
12597 break;
12598
d6ab8113
JB
12599 case BFD_RELOC_64:
12600 fixP->fx_r_type = BFD_RELOC_64_PCREL;
12601 break;
93382f6d 12602 case BFD_RELOC_32:
ae8887b5 12603 case BFD_RELOC_X86_64_32S:
93382f6d
AM
12604 fixP->fx_r_type = BFD_RELOC_32_PCREL;
12605 break;
12606 case BFD_RELOC_16:
12607 fixP->fx_r_type = BFD_RELOC_16_PCREL;
12608 break;
12609 case BFD_RELOC_8:
12610 fixP->fx_r_type = BFD_RELOC_8_PCREL;
12611 break;
12612 }
12613 }
252b5132 12614
a161fe53 12615 if (fixP->fx_addsy != NULL
31312f95 12616 && (fixP->fx_r_type == BFD_RELOC_32_PCREL
d6ab8113 12617 || fixP->fx_r_type == BFD_RELOC_64_PCREL
31312f95 12618 || fixP->fx_r_type == BFD_RELOC_16_PCREL
d258b828 12619 || fixP->fx_r_type == BFD_RELOC_8_PCREL)
31312f95 12620 && !use_rela_relocations)
252b5132 12621 {
31312f95
AM
12622 /* This is a hack. There should be a better way to handle this.
12623 This covers for the fact that bfd_install_relocation will
12624 subtract the current location (for partial_inplace, PC relative
12625 relocations); see more below. */
252b5132 12626#ifndef OBJ_AOUT
718ddfc0 12627 if (IS_ELF
252b5132
RH
12628#ifdef TE_PE
12629 || OUTPUT_FLAVOR == bfd_target_coff_flavour
12630#endif
12631 )
12632 value += fixP->fx_where + fixP->fx_frag->fr_address;
12633#endif
12634#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12635 if (IS_ELF)
252b5132 12636 {
6539b54b 12637 segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
2f66722d 12638
6539b54b 12639 if ((sym_seg == seg
2f66722d 12640 || (symbol_section_p (fixP->fx_addsy)
6539b54b 12641 && sym_seg != absolute_section))
af65af87 12642 && !generic_force_reloc (fixP))
2f66722d
AM
12643 {
12644 /* Yes, we add the values in twice. This is because
6539b54b
AM
12645 bfd_install_relocation subtracts them out again. I think
12646 bfd_install_relocation is broken, but I don't dare change
2f66722d
AM
12647 it. FIXME. */
12648 value += fixP->fx_where + fixP->fx_frag->fr_address;
12649 }
252b5132
RH
12650 }
12651#endif
12652#if defined (OBJ_COFF) && defined (TE_PE)
977cdf5a
NC
12653 /* For some reason, the PE format does not store a
12654 section address offset for a PC relative symbol. */
12655 if (S_GET_SEGMENT (fixP->fx_addsy) != seg
7be1c489 12656 || S_IS_WEAK (fixP->fx_addsy))
252b5132
RH
12657 value += md_pcrel_from (fixP);
12658#endif
12659 }
fbeb56a4 12660#if defined (OBJ_COFF) && defined (TE_PE)
f01c1a09
NC
12661 if (fixP->fx_addsy != NULL
12662 && S_IS_WEAK (fixP->fx_addsy)
12663 /* PR 16858: Do not modify weak function references. */
12664 && ! fixP->fx_pcrel)
fbeb56a4 12665 {
296a8689
NC
12666#if !defined (TE_PEP)
12667 /* For x86 PE weak function symbols are neither PC-relative
12668 nor do they set S_IS_FUNCTION. So the only reliable way
12669 to detect them is to check the flags of their containing
12670 section. */
12671 if (S_GET_SEGMENT (fixP->fx_addsy) != NULL
12672 && S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_CODE)
12673 ;
12674 else
12675#endif
fbeb56a4
DK
12676 value -= S_GET_VALUE (fixP->fx_addsy);
12677 }
12678#endif
252b5132
RH
12679
12680 /* Fix a few things - the dynamic linker expects certain values here,
0234cb7c 12681 and we must not disappoint it. */
252b5132 12682#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12683 if (IS_ELF && fixP->fx_addsy)
47926f60
KH
12684 switch (fixP->fx_r_type)
12685 {
12686 case BFD_RELOC_386_PLT32:
3e73aa7c 12687 case BFD_RELOC_X86_64_PLT32:
b9519cfe
L
12688 /* Make the jump instruction point to the address of the operand.
12689 At runtime we merely add the offset to the actual PLT entry.
12690 NB: Subtract the offset size only for jump instructions. */
12691 if (fixP->fx_pcrel)
12692 value = -4;
47926f60 12693 break;
31312f95 12694
13ae64f3
JJ
12695 case BFD_RELOC_386_TLS_GD:
12696 case BFD_RELOC_386_TLS_LDM:
13ae64f3 12697 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
12698 case BFD_RELOC_386_TLS_IE:
12699 case BFD_RELOC_386_TLS_GOTIE:
67a4f2b7 12700 case BFD_RELOC_386_TLS_GOTDESC:
bffbf940
JJ
12701 case BFD_RELOC_X86_64_TLSGD:
12702 case BFD_RELOC_X86_64_TLSLD:
12703 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7 12704 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
00f7efb6
JJ
12705 value = 0; /* Fully resolved at runtime. No addend. */
12706 /* Fallthrough */
12707 case BFD_RELOC_386_TLS_LE:
12708 case BFD_RELOC_386_TLS_LDO_32:
12709 case BFD_RELOC_386_TLS_LE_32:
12710 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 12711 case BFD_RELOC_X86_64_DTPOFF64:
00f7efb6 12712 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113 12713 case BFD_RELOC_X86_64_TPOFF64:
00f7efb6
JJ
12714 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12715 break;
12716
67a4f2b7
AO
12717 case BFD_RELOC_386_TLS_DESC_CALL:
12718 case BFD_RELOC_X86_64_TLSDESC_CALL:
12719 value = 0; /* Fully resolved at runtime. No addend. */
12720 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12721 fixP->fx_done = 0;
12722 return;
12723
47926f60
KH
12724 case BFD_RELOC_VTABLE_INHERIT:
12725 case BFD_RELOC_VTABLE_ENTRY:
12726 fixP->fx_done = 0;
94f592af 12727 return;
47926f60
KH
12728
12729 default:
12730 break;
12731 }
12732#endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) */
a442cac5
JB
12733
12734 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
12735 if (!object_64bit)
12736 value = extend_to_32bit_address (value);
12737
c6682705 12738 *valP = value;
f86103b7 12739#endif /* !defined (TE_Mach) */
3e73aa7c 12740
3e73aa7c 12741 /* Are we finished with this relocation now? */
c6682705 12742 if (fixP->fx_addsy == NULL)
b8188555
JB
12743 {
12744 fixP->fx_done = 1;
12745 switch (fixP->fx_r_type)
12746 {
12747 case BFD_RELOC_X86_64_32S:
12748 fixP->fx_signed = 1;
12749 break;
12750
12751 default:
12752 break;
12753 }
12754 }
fbeb56a4
DK
12755#if defined (OBJ_COFF) && defined (TE_PE)
12756 else if (fixP->fx_addsy != NULL && S_IS_WEAK (fixP->fx_addsy))
12757 {
12758 fixP->fx_done = 0;
12759 /* Remember value for tc_gen_reloc. */
12760 fixP->fx_addnumber = value;
12761 /* Clear out the frag for now. */
12762 value = 0;
12763 }
12764#endif
3e73aa7c
JH
12765 else if (use_rela_relocations)
12766 {
46fb6d5a
JB
12767 if (!disallow_64bit_reloc || fixP->fx_r_type == NO_RELOC)
12768 fixP->fx_no_overflow = 1;
062cd5e7
AS
12769 /* Remember value for tc_gen_reloc. */
12770 fixP->fx_addnumber = value;
3e73aa7c
JH
12771 value = 0;
12772 }
f86103b7 12773
94f592af 12774 md_number_to_chars (p, value, fixP->fx_size);
252b5132 12775}
252b5132 12776\f
6d4af3c2 12777const char *
499ac353 12778md_atof (int type, char *litP, int *sizeP)
252b5132 12779{
499ac353
NC
12780 /* This outputs the LITTLENUMs in REVERSE order;
12781 in accord with the bigendian 386. */
5b7c81bd 12782 return ieee_md_atof (type, litP, sizeP, false);
252b5132
RH
12783}
12784\f
2d545b82 12785static char output_invalid_buf[sizeof (unsigned char) * 2 + 6];
252b5132 12786
252b5132 12787static char *
e3bb37b5 12788output_invalid (int c)
252b5132 12789{
3882b010 12790 if (ISPRINT (c))
f9f21a03
L
12791 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
12792 "'%c'", c);
252b5132 12793 else
f9f21a03 12794 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
2d545b82 12795 "(0x%x)", (unsigned char) c);
252b5132
RH
12796 return output_invalid_buf;
12797}
12798
8a6fb3f9
JB
12799/* Verify that @r can be used in the current context. */
12800
5b7c81bd 12801static bool check_register (const reg_entry *r)
8a6fb3f9
JB
12802{
12803 if (allow_pseudo_reg)
5b7c81bd 12804 return true;
8a6fb3f9
JB
12805
12806 if (operand_type_all_zero (&r->reg_type))
5b7c81bd 12807 return false;
8a6fb3f9
JB
12808
12809 if ((r->reg_type.bitfield.dword
12810 || (r->reg_type.bitfield.class == SReg && r->reg_num > 3)
12811 || r->reg_type.bitfield.class == RegCR
22e00a3f 12812 || r->reg_type.bitfield.class == RegDR)
8a6fb3f9 12813 && !cpu_arch_flags.bitfield.cpui386)
5b7c81bd 12814 return false;
8a6fb3f9 12815
22e00a3f
JB
12816 if (r->reg_type.bitfield.class == RegTR
12817 && (flag_code == CODE_64BIT
12818 || !cpu_arch_flags.bitfield.cpui386
12819 || cpu_arch_isa_flags.bitfield.cpui586
12820 || cpu_arch_isa_flags.bitfield.cpui686))
5b7c81bd 12821 return false;
22e00a3f 12822
8a6fb3f9 12823 if (r->reg_type.bitfield.class == RegMMX && !cpu_arch_flags.bitfield.cpummx)
5b7c81bd 12824 return false;
8a6fb3f9
JB
12825
12826 if (!cpu_arch_flags.bitfield.cpuavx512f)
12827 {
12828 if (r->reg_type.bitfield.zmmword
12829 || r->reg_type.bitfield.class == RegMask)
5b7c81bd 12830 return false;
8a6fb3f9
JB
12831
12832 if (!cpu_arch_flags.bitfield.cpuavx)
12833 {
12834 if (r->reg_type.bitfield.ymmword)
5b7c81bd 12835 return false;
8a6fb3f9
JB
12836
12837 if (!cpu_arch_flags.bitfield.cpusse && r->reg_type.bitfield.xmmword)
5b7c81bd 12838 return false;
8a6fb3f9
JB
12839 }
12840 }
12841
260cd341
LC
12842 if (r->reg_type.bitfield.tmmword
12843 && (!cpu_arch_flags.bitfield.cpuamx_tile
12844 || flag_code != CODE_64BIT))
5b7c81bd 12845 return false;
260cd341 12846
8a6fb3f9 12847 if (r->reg_type.bitfield.class == RegBND && !cpu_arch_flags.bitfield.cpumpx)
5b7c81bd 12848 return false;
8a6fb3f9
JB
12849
12850 /* Don't allow fake index register unless allow_index_reg isn't 0. */
12851 if (!allow_index_reg && r->reg_num == RegIZ)
5b7c81bd 12852 return false;
8a6fb3f9
JB
12853
12854 /* Upper 16 vector registers are only available with VREX in 64bit
12855 mode, and require EVEX encoding. */
12856 if (r->reg_flags & RegVRex)
12857 {
12858 if (!cpu_arch_flags.bitfield.cpuavx512f
12859 || flag_code != CODE_64BIT)
5b7c81bd 12860 return false;
8a6fb3f9 12861
da4977e0
JB
12862 if (i.vec_encoding == vex_encoding_default)
12863 i.vec_encoding = vex_encoding_evex;
12864 else if (i.vec_encoding != vex_encoding_evex)
12865 i.vec_encoding = vex_encoding_error;
8a6fb3f9
JB
12866 }
12867
12868 if (((r->reg_flags & (RegRex64 | RegRex)) || r->reg_type.bitfield.qword)
12869 && (!cpu_arch_flags.bitfield.cpulm || r->reg_type.bitfield.class != RegCR)
12870 && flag_code != CODE_64BIT)
5b7c81bd 12871 return false;
8a6fb3f9
JB
12872
12873 if (r->reg_type.bitfield.class == SReg && r->reg_num == RegFlat
12874 && !intel_syntax)
5b7c81bd 12875 return false;
8a6fb3f9 12876
5b7c81bd 12877 return true;
8a6fb3f9
JB
12878}
12879
af6bdddf 12880/* REG_STRING starts *before* REGISTER_PREFIX. */
252b5132
RH
12881
12882static const reg_entry *
4d1bb795 12883parse_real_register (char *reg_string, char **end_op)
252b5132 12884{
af6bdddf
AM
12885 char *s = reg_string;
12886 char *p;
252b5132
RH
12887 char reg_name_given[MAX_REG_NAME_SIZE + 1];
12888 const reg_entry *r;
12889
12890 /* Skip possible REGISTER_PREFIX and possible whitespace. */
12891 if (*s == REGISTER_PREFIX)
12892 ++s;
12893
12894 if (is_space_char (*s))
12895 ++s;
12896
12897 p = reg_name_given;
af6bdddf 12898 while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
252b5132
RH
12899 {
12900 if (p >= reg_name_given + MAX_REG_NAME_SIZE)
af6bdddf
AM
12901 return (const reg_entry *) NULL;
12902 s++;
252b5132
RH
12903 }
12904
6588847e
DN
12905 /* For naked regs, make sure that we are not dealing with an identifier.
12906 This prevents confusing an identifier like `eax_var' with register
12907 `eax'. */
12908 if (allow_naked_reg && identifier_chars[(unsigned char) *s])
12909 return (const reg_entry *) NULL;
12910
af6bdddf 12911 *end_op = s;
252b5132 12912
629310ab 12913 r = (const reg_entry *) str_hash_find (reg_hash, reg_name_given);
252b5132 12914
5f47d35b 12915 /* Handle floating point regs, allowing spaces in the (i) part. */
6288d05f 12916 if (r == reg_st0)
5f47d35b 12917 {
0e0eea78
JB
12918 if (!cpu_arch_flags.bitfield.cpu8087
12919 && !cpu_arch_flags.bitfield.cpu287
af32b722
JB
12920 && !cpu_arch_flags.bitfield.cpu387
12921 && !allow_pseudo_reg)
0e0eea78
JB
12922 return (const reg_entry *) NULL;
12923
5f47d35b
AM
12924 if (is_space_char (*s))
12925 ++s;
12926 if (*s == '(')
12927 {
af6bdddf 12928 ++s;
5f47d35b
AM
12929 if (is_space_char (*s))
12930 ++s;
12931 if (*s >= '0' && *s <= '7')
12932 {
db557034 12933 int fpr = *s - '0';
af6bdddf 12934 ++s;
5f47d35b
AM
12935 if (is_space_char (*s))
12936 ++s;
12937 if (*s == ')')
12938 {
12939 *end_op = s + 1;
6288d05f 12940 know (r[fpr].reg_num == fpr);
db557034 12941 return r + fpr;
5f47d35b 12942 }
5f47d35b 12943 }
47926f60 12944 /* We have "%st(" then garbage. */
5f47d35b
AM
12945 return (const reg_entry *) NULL;
12946 }
12947 }
12948
8a6fb3f9 12949 return r && check_register (r) ? r : NULL;
252b5132 12950}
4d1bb795
JB
12951
12952/* REG_STRING starts *before* REGISTER_PREFIX. */
12953
12954static const reg_entry *
12955parse_register (char *reg_string, char **end_op)
12956{
12957 const reg_entry *r;
12958
12959 if (*reg_string == REGISTER_PREFIX || allow_naked_reg)
12960 r = parse_real_register (reg_string, end_op);
12961 else
12962 r = NULL;
12963 if (!r)
12964 {
12965 char *save = input_line_pointer;
12966 char c;
12967 symbolS *symbolP;
12968
12969 input_line_pointer = reg_string;
d02603dc 12970 c = get_symbol_name (&reg_string);
4d1bb795 12971 symbolP = symbol_find (reg_string);
64d23078
JB
12972 while (symbolP && S_GET_SEGMENT (symbolP) != reg_section)
12973 {
12974 const expressionS *e = symbol_get_value_expression(symbolP);
12975
12976 if (e->X_op != O_symbol || e->X_add_number)
12977 break;
12978 symbolP = e->X_add_symbol;
12979 }
4d1bb795
JB
12980 if (symbolP && S_GET_SEGMENT (symbolP) == reg_section)
12981 {
12982 const expressionS *e = symbol_get_value_expression (symbolP);
12983
3b55a1d0
JB
12984 know (e->X_op == O_register);
12985 know (e->X_add_number >= 0
12986 && (valueT) e->X_add_number < i386_regtab_size);
12987 r = i386_regtab + e->X_add_number;
12988 if (!check_register (r))
8a6fb3f9 12989 {
3b55a1d0
JB
12990 as_bad (_("register '%s%s' cannot be used here"),
12991 register_prefix, r->reg_name);
12992 r = &bad_reg;
8a6fb3f9 12993 }
3b55a1d0 12994 *end_op = input_line_pointer;
4d1bb795
JB
12995 }
12996 *input_line_pointer = c;
12997 input_line_pointer = save;
12998 }
12999 return r;
13000}
13001
13002int
13003i386_parse_name (char *name, expressionS *e, char *nextcharP)
13004{
4faaa10f 13005 const reg_entry *r = NULL;
4d1bb795
JB
13006 char *end = input_line_pointer;
13007
13008 *end = *nextcharP;
4faaa10f
JB
13009 if (*name == REGISTER_PREFIX || allow_naked_reg)
13010 r = parse_real_register (name, &input_line_pointer);
4d1bb795
JB
13011 if (r && end <= input_line_pointer)
13012 {
13013 *nextcharP = *input_line_pointer;
13014 *input_line_pointer = 0;
8a6fb3f9
JB
13015 if (r != &bad_reg)
13016 {
13017 e->X_op = O_register;
13018 e->X_add_number = r - i386_regtab;
13019 }
13020 else
13021 e->X_op = O_illegal;
4d1bb795
JB
13022 return 1;
13023 }
13024 input_line_pointer = end;
13025 *end = 0;
ee86248c 13026 return intel_syntax ? i386_intel_parse_name (name, e) : 0;
4d1bb795
JB
13027}
13028
13029void
13030md_operand (expressionS *e)
13031{
ee86248c
JB
13032 char *end;
13033 const reg_entry *r;
4d1bb795 13034
ee86248c
JB
13035 switch (*input_line_pointer)
13036 {
13037 case REGISTER_PREFIX:
13038 r = parse_real_register (input_line_pointer, &end);
4d1bb795
JB
13039 if (r)
13040 {
13041 e->X_op = O_register;
13042 e->X_add_number = r - i386_regtab;
13043 input_line_pointer = end;
13044 }
ee86248c
JB
13045 break;
13046
13047 case '[':
9c2799c2 13048 gas_assert (intel_syntax);
ee86248c
JB
13049 end = input_line_pointer++;
13050 expression (e);
13051 if (*input_line_pointer == ']')
13052 {
13053 ++input_line_pointer;
13054 e->X_op_symbol = make_expr_symbol (e);
13055 e->X_add_symbol = NULL;
13056 e->X_add_number = 0;
13057 e->X_op = O_index;
13058 }
13059 else
13060 {
13061 e->X_op = O_absent;
13062 input_line_pointer = end;
13063 }
13064 break;
4d1bb795
JB
13065 }
13066}
13067
252b5132 13068\f
4cc782b5 13069#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
b6f8c7c4 13070const char *md_shortopts = "kVQ:sqnO::";
252b5132 13071#else
b6f8c7c4 13072const char *md_shortopts = "qnO::";
252b5132 13073#endif
6e0b89ee 13074
3e73aa7c 13075#define OPTION_32 (OPTION_MD_BASE + 0)
b3b91714
AM
13076#define OPTION_64 (OPTION_MD_BASE + 1)
13077#define OPTION_DIVIDE (OPTION_MD_BASE + 2)
9103f4f4
L
13078#define OPTION_MARCH (OPTION_MD_BASE + 3)
13079#define OPTION_MTUNE (OPTION_MD_BASE + 4)
1efbbeb4
L
13080#define OPTION_MMNEMONIC (OPTION_MD_BASE + 5)
13081#define OPTION_MSYNTAX (OPTION_MD_BASE + 6)
13082#define OPTION_MINDEX_REG (OPTION_MD_BASE + 7)
13083#define OPTION_MNAKED_REG (OPTION_MD_BASE + 8)
bd5dea88 13084#define OPTION_MRELAX_RELOCATIONS (OPTION_MD_BASE + 9)
c0f3af97 13085#define OPTION_MSSE2AVX (OPTION_MD_BASE + 10)
daf50ae7 13086#define OPTION_MSSE_CHECK (OPTION_MD_BASE + 11)
7bab8ab5
JB
13087#define OPTION_MOPERAND_CHECK (OPTION_MD_BASE + 12)
13088#define OPTION_MAVXSCALAR (OPTION_MD_BASE + 13)
13089#define OPTION_X32 (OPTION_MD_BASE + 14)
7e8b059b 13090#define OPTION_MADD_BND_PREFIX (OPTION_MD_BASE + 15)
43234a1e
L
13091#define OPTION_MEVEXLIG (OPTION_MD_BASE + 16)
13092#define OPTION_MEVEXWIG (OPTION_MD_BASE + 17)
167ad85b 13093#define OPTION_MBIG_OBJ (OPTION_MD_BASE + 18)
d1982f93 13094#define OPTION_MOMIT_LOCK_PREFIX (OPTION_MD_BASE + 19)
d3d3c6db 13095#define OPTION_MEVEXRCIG (OPTION_MD_BASE + 20)
8dcea932 13096#define OPTION_MSHARED (OPTION_MD_BASE + 21)
5db04b09
L
13097#define OPTION_MAMD64 (OPTION_MD_BASE + 22)
13098#define OPTION_MINTEL64 (OPTION_MD_BASE + 23)
e4e00185 13099#define OPTION_MFENCE_AS_LOCK_ADD (OPTION_MD_BASE + 24)
b4a3a7b4 13100#define OPTION_X86_USED_NOTE (OPTION_MD_BASE + 25)
03751133 13101#define OPTION_MVEXWIG (OPTION_MD_BASE + 26)
e379e5f3
L
13102#define OPTION_MALIGN_BRANCH_BOUNDARY (OPTION_MD_BASE + 27)
13103#define OPTION_MALIGN_BRANCH_PREFIX_SIZE (OPTION_MD_BASE + 28)
13104#define OPTION_MALIGN_BRANCH (OPTION_MD_BASE + 29)
76cf450b 13105#define OPTION_MBRANCHES_WITH_32B_BOUNDARIES (OPTION_MD_BASE + 30)
ae531041
L
13106#define OPTION_MLFENCE_AFTER_LOAD (OPTION_MD_BASE + 31)
13107#define OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH (OPTION_MD_BASE + 32)
13108#define OPTION_MLFENCE_BEFORE_RET (OPTION_MD_BASE + 33)
c8480b58 13109#define OPTION_MUSE_UNALIGNED_VECTOR_MOVE (OPTION_MD_BASE + 34)
b3b91714 13110
99ad8390
NC
13111struct option md_longopts[] =
13112{
3e73aa7c 13113 {"32", no_argument, NULL, OPTION_32},
321098a5 13114#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 13115 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c 13116 {"64", no_argument, NULL, OPTION_64},
351f65ca
L
13117#endif
13118#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 13119 {"x32", no_argument, NULL, OPTION_X32},
8dcea932 13120 {"mshared", no_argument, NULL, OPTION_MSHARED},
b4a3a7b4 13121 {"mx86-used-note", required_argument, NULL, OPTION_X86_USED_NOTE},
6e0b89ee 13122#endif
b3b91714 13123 {"divide", no_argument, NULL, OPTION_DIVIDE},
9103f4f4
L
13124 {"march", required_argument, NULL, OPTION_MARCH},
13125 {"mtune", required_argument, NULL, OPTION_MTUNE},
1efbbeb4
L
13126 {"mmnemonic", required_argument, NULL, OPTION_MMNEMONIC},
13127 {"msyntax", required_argument, NULL, OPTION_MSYNTAX},
13128 {"mindex-reg", no_argument, NULL, OPTION_MINDEX_REG},
13129 {"mnaked-reg", no_argument, NULL, OPTION_MNAKED_REG},
c0f3af97 13130 {"msse2avx", no_argument, NULL, OPTION_MSSE2AVX},
c8480b58 13131 {"muse-unaligned-vector-move", no_argument, NULL, OPTION_MUSE_UNALIGNED_VECTOR_MOVE},
daf50ae7 13132 {"msse-check", required_argument, NULL, OPTION_MSSE_CHECK},
7bab8ab5 13133 {"moperand-check", required_argument, NULL, OPTION_MOPERAND_CHECK},
539f890d 13134 {"mavxscalar", required_argument, NULL, OPTION_MAVXSCALAR},
03751133 13135 {"mvexwig", required_argument, NULL, OPTION_MVEXWIG},
7e8b059b 13136 {"madd-bnd-prefix", no_argument, NULL, OPTION_MADD_BND_PREFIX},
43234a1e
L
13137 {"mevexlig", required_argument, NULL, OPTION_MEVEXLIG},
13138 {"mevexwig", required_argument, NULL, OPTION_MEVEXWIG},
167ad85b
TG
13139# if defined (TE_PE) || defined (TE_PEP)
13140 {"mbig-obj", no_argument, NULL, OPTION_MBIG_OBJ},
13141#endif
d1982f93 13142 {"momit-lock-prefix", required_argument, NULL, OPTION_MOMIT_LOCK_PREFIX},
e4e00185 13143 {"mfence-as-lock-add", required_argument, NULL, OPTION_MFENCE_AS_LOCK_ADD},
0cb4071e 13144 {"mrelax-relocations", required_argument, NULL, OPTION_MRELAX_RELOCATIONS},
d3d3c6db 13145 {"mevexrcig", required_argument, NULL, OPTION_MEVEXRCIG},
e379e5f3
L
13146 {"malign-branch-boundary", required_argument, NULL, OPTION_MALIGN_BRANCH_BOUNDARY},
13147 {"malign-branch-prefix-size", required_argument, NULL, OPTION_MALIGN_BRANCH_PREFIX_SIZE},
13148 {"malign-branch", required_argument, NULL, OPTION_MALIGN_BRANCH},
76cf450b 13149 {"mbranches-within-32B-boundaries", no_argument, NULL, OPTION_MBRANCHES_WITH_32B_BOUNDARIES},
ae531041
L
13150 {"mlfence-after-load", required_argument, NULL, OPTION_MLFENCE_AFTER_LOAD},
13151 {"mlfence-before-indirect-branch", required_argument, NULL,
13152 OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH},
13153 {"mlfence-before-ret", required_argument, NULL, OPTION_MLFENCE_BEFORE_RET},
5db04b09
L
13154 {"mamd64", no_argument, NULL, OPTION_MAMD64},
13155 {"mintel64", no_argument, NULL, OPTION_MINTEL64},
252b5132
RH
13156 {NULL, no_argument, NULL, 0}
13157};
13158size_t md_longopts_size = sizeof (md_longopts);
13159
13160int
17b9d67d 13161md_parse_option (int c, const char *arg)
252b5132 13162{
91d6fa6a 13163 unsigned int j;
e379e5f3 13164 char *arch, *next, *saved, *type;
9103f4f4 13165
252b5132
RH
13166 switch (c)
13167 {
12b55ccc
L
13168 case 'n':
13169 optimize_align_code = 0;
13170 break;
13171
a38cf1db
AM
13172 case 'q':
13173 quiet_warnings = 1;
252b5132
RH
13174 break;
13175
13176#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
a38cf1db
AM
13177 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
13178 should be emitted or not. FIXME: Not implemented. */
13179 case 'Q':
d4693039
JB
13180 if ((arg[0] != 'y' && arg[0] != 'n') || arg[1])
13181 return 0;
252b5132
RH
13182 break;
13183
13184 /* -V: SVR4 argument to print version ID. */
13185 case 'V':
13186 print_version_id ();
13187 break;
13188
a38cf1db
AM
13189 /* -k: Ignore for FreeBSD compatibility. */
13190 case 'k':
252b5132 13191 break;
4cc782b5
ILT
13192
13193 case 's':
13194 /* -s: On i386 Solaris, this tells the native assembler to use
29b0f896 13195 .stab instead of .stab.excl. We always use .stab anyhow. */
4cc782b5 13196 break;
8dcea932
L
13197
13198 case OPTION_MSHARED:
13199 shared = 1;
13200 break;
b4a3a7b4
L
13201
13202 case OPTION_X86_USED_NOTE:
13203 if (strcasecmp (arg, "yes") == 0)
13204 x86_used_note = 1;
13205 else if (strcasecmp (arg, "no") == 0)
13206 x86_used_note = 0;
13207 else
13208 as_fatal (_("invalid -mx86-used-note= option: `%s'"), arg);
13209 break;
13210
13211
99ad8390 13212#endif
321098a5 13213#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 13214 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c
JH
13215 case OPTION_64:
13216 {
13217 const char **list, **l;
13218
3e73aa7c
JH
13219 list = bfd_target_list ();
13220 for (l = list; *l != NULL; l++)
08dedd66 13221 if (startswith (*l, "elf64-x86-64")
99ad8390
NC
13222 || strcmp (*l, "coff-x86-64") == 0
13223 || strcmp (*l, "pe-x86-64") == 0
d382c579
TG
13224 || strcmp (*l, "pei-x86-64") == 0
13225 || strcmp (*l, "mach-o-x86-64") == 0)
6e0b89ee
AM
13226 {
13227 default_arch = "x86_64";
13228 break;
13229 }
3e73aa7c 13230 if (*l == NULL)
2b5d6a91 13231 as_fatal (_("no compiled in support for x86_64"));
3e73aa7c
JH
13232 free (list);
13233 }
13234 break;
13235#endif
252b5132 13236
351f65ca 13237#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 13238 case OPTION_X32:
351f65ca
L
13239 if (IS_ELF)
13240 {
13241 const char **list, **l;
13242
13243 list = bfd_target_list ();
13244 for (l = list; *l != NULL; l++)
08dedd66 13245 if (startswith (*l, "elf32-x86-64"))
351f65ca
L
13246 {
13247 default_arch = "x86_64:32";
13248 break;
13249 }
13250 if (*l == NULL)
2b5d6a91 13251 as_fatal (_("no compiled in support for 32bit x86_64"));
351f65ca
L
13252 free (list);
13253 }
13254 else
13255 as_fatal (_("32bit x86_64 is only supported for ELF"));
13256 break;
13257#endif
13258
6e0b89ee
AM
13259 case OPTION_32:
13260 default_arch = "i386";
13261 break;
13262
b3b91714
AM
13263 case OPTION_DIVIDE:
13264#ifdef SVR4_COMMENT_CHARS
13265 {
13266 char *n, *t;
13267 const char *s;
13268
add39d23 13269 n = XNEWVEC (char, strlen (i386_comment_chars) + 1);
b3b91714
AM
13270 t = n;
13271 for (s = i386_comment_chars; *s != '\0'; s++)
13272 if (*s != '/')
13273 *t++ = *s;
13274 *t = '\0';
13275 i386_comment_chars = n;
13276 }
13277#endif
13278 break;
13279
9103f4f4 13280 case OPTION_MARCH:
293f5f65
L
13281 saved = xstrdup (arg);
13282 arch = saved;
13283 /* Allow -march=+nosse. */
13284 if (*arch == '+')
13285 arch++;
6305a203 13286 do
9103f4f4 13287 {
6305a203 13288 if (*arch == '.')
2b5d6a91 13289 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
13290 next = strchr (arch, '+');
13291 if (next)
13292 *next++ = '\0';
91d6fa6a 13293 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 13294 {
ae89daec
JB
13295 if (arch == saved && cpu_arch[j].type != PROCESSOR_NONE
13296 && strcmp (arch, cpu_arch[j].name) == 0)
ccc9c027 13297 {
6305a203 13298 /* Processor. */
ae89daec 13299 if (! cpu_arch[j].enable.bitfield.cpui386)
1ded5609
JB
13300 continue;
13301
91d6fa6a 13302 cpu_arch_name = cpu_arch[j].name;
d92c7521 13303 free (cpu_sub_arch_name);
6305a203 13304 cpu_sub_arch_name = NULL;
ae89daec 13305 cpu_arch_flags = cpu_arch[j].enable;
91d6fa6a 13306 cpu_arch_isa = cpu_arch[j].type;
ae89daec 13307 cpu_arch_isa_flags = cpu_arch[j].enable;
6305a203
L
13308 if (!cpu_arch_tune_set)
13309 {
13310 cpu_arch_tune = cpu_arch_isa;
13311 cpu_arch_tune_flags = cpu_arch_isa_flags;
13312 }
13313 break;
13314 }
ae89daec
JB
13315 else if (cpu_arch[j].type == PROCESSOR_NONE
13316 && strcmp (arch, cpu_arch[j].name) == 0
13317 && !cpu_flags_all_zero (&cpu_arch[j].enable))
6305a203 13318 {
33eaf5de 13319 /* ISA extension. */
6305a203 13320 i386_cpu_flags flags;
309d3373 13321
293f5f65 13322 flags = cpu_flags_or (cpu_arch_flags,
ae89daec 13323 cpu_arch[j].enable);
81486035 13324
5b64d091 13325 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
6305a203 13326 {
ae89daec 13327 extend_cpu_sub_arch_name (arch);
6305a203 13328 cpu_arch_flags = flags;
a586129e 13329 cpu_arch_isa_flags = flags;
6305a203 13330 }
0089dace
L
13331 else
13332 cpu_arch_isa_flags
13333 = cpu_flags_or (cpu_arch_isa_flags,
ae89daec 13334 cpu_arch[j].enable);
6305a203 13335 break;
ccc9c027 13336 }
9103f4f4 13337 }
6305a203 13338
ae89daec 13339 if (j >= ARRAY_SIZE (cpu_arch) && startswith (arch, "no"))
293f5f65 13340 {
33eaf5de 13341 /* Disable an ISA extension. */
ae89daec
JB
13342 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13343 if (cpu_arch[j].type == PROCESSOR_NONE
13344 && strcmp (arch + 2, cpu_arch[j].name) == 0)
293f5f65
L
13345 {
13346 i386_cpu_flags flags;
13347
13348 flags = cpu_flags_and_not (cpu_arch_flags,
ae89daec 13349 cpu_arch[j].disable);
293f5f65
L
13350 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
13351 {
8180707f 13352 extend_cpu_sub_arch_name (arch);
293f5f65
L
13353 cpu_arch_flags = flags;
13354 cpu_arch_isa_flags = flags;
13355 }
13356 break;
13357 }
293f5f65
L
13358 }
13359
91d6fa6a 13360 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 13361 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
13362
13363 arch = next;
9103f4f4 13364 }
293f5f65
L
13365 while (next != NULL);
13366 free (saved);
9103f4f4
L
13367 break;
13368
13369 case OPTION_MTUNE:
13370 if (*arg == '.')
2b5d6a91 13371 as_fatal (_("invalid -mtune= option: `%s'"), arg);
91d6fa6a 13372 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 13373 {
ae89daec
JB
13374 if (cpu_arch[j].type != PROCESSOR_NONE
13375 && strcmp (arg, cpu_arch[j].name) == 0)
9103f4f4 13376 {
ccc9c027 13377 cpu_arch_tune_set = 1;
91d6fa6a 13378 cpu_arch_tune = cpu_arch [j].type;
ae89daec 13379 cpu_arch_tune_flags = cpu_arch[j].enable;
9103f4f4
L
13380 break;
13381 }
13382 }
91d6fa6a 13383 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 13384 as_fatal (_("invalid -mtune= option: `%s'"), arg);
9103f4f4
L
13385 break;
13386
1efbbeb4
L
13387 case OPTION_MMNEMONIC:
13388 if (strcasecmp (arg, "att") == 0)
13389 intel_mnemonic = 0;
13390 else if (strcasecmp (arg, "intel") == 0)
13391 intel_mnemonic = 1;
13392 else
2b5d6a91 13393 as_fatal (_("invalid -mmnemonic= option: `%s'"), arg);
1efbbeb4
L
13394 break;
13395
13396 case OPTION_MSYNTAX:
13397 if (strcasecmp (arg, "att") == 0)
13398 intel_syntax = 0;
13399 else if (strcasecmp (arg, "intel") == 0)
13400 intel_syntax = 1;
13401 else
2b5d6a91 13402 as_fatal (_("invalid -msyntax= option: `%s'"), arg);
1efbbeb4
L
13403 break;
13404
13405 case OPTION_MINDEX_REG:
13406 allow_index_reg = 1;
13407 break;
13408
13409 case OPTION_MNAKED_REG:
13410 allow_naked_reg = 1;
13411 break;
13412
c0f3af97
L
13413 case OPTION_MSSE2AVX:
13414 sse2avx = 1;
13415 break;
13416
c8480b58
L
13417 case OPTION_MUSE_UNALIGNED_VECTOR_MOVE:
13418 use_unaligned_vector_move = 1;
13419 break;
13420
daf50ae7
L
13421 case OPTION_MSSE_CHECK:
13422 if (strcasecmp (arg, "error") == 0)
7bab8ab5 13423 sse_check = check_error;
daf50ae7 13424 else if (strcasecmp (arg, "warning") == 0)
7bab8ab5 13425 sse_check = check_warning;
daf50ae7 13426 else if (strcasecmp (arg, "none") == 0)
7bab8ab5 13427 sse_check = check_none;
daf50ae7 13428 else
2b5d6a91 13429 as_fatal (_("invalid -msse-check= option: `%s'"), arg);
daf50ae7
L
13430 break;
13431
7bab8ab5
JB
13432 case OPTION_MOPERAND_CHECK:
13433 if (strcasecmp (arg, "error") == 0)
13434 operand_check = check_error;
13435 else if (strcasecmp (arg, "warning") == 0)
13436 operand_check = check_warning;
13437 else if (strcasecmp (arg, "none") == 0)
13438 operand_check = check_none;
13439 else
13440 as_fatal (_("invalid -moperand-check= option: `%s'"), arg);
13441 break;
13442
539f890d
L
13443 case OPTION_MAVXSCALAR:
13444 if (strcasecmp (arg, "128") == 0)
13445 avxscalar = vex128;
13446 else if (strcasecmp (arg, "256") == 0)
13447 avxscalar = vex256;
13448 else
2b5d6a91 13449 as_fatal (_("invalid -mavxscalar= option: `%s'"), arg);
539f890d
L
13450 break;
13451
03751133
L
13452 case OPTION_MVEXWIG:
13453 if (strcmp (arg, "0") == 0)
40c9c8de 13454 vexwig = vexw0;
03751133 13455 else if (strcmp (arg, "1") == 0)
40c9c8de 13456 vexwig = vexw1;
03751133
L
13457 else
13458 as_fatal (_("invalid -mvexwig= option: `%s'"), arg);
13459 break;
13460
7e8b059b
L
13461 case OPTION_MADD_BND_PREFIX:
13462 add_bnd_prefix = 1;
13463 break;
13464
43234a1e
L
13465 case OPTION_MEVEXLIG:
13466 if (strcmp (arg, "128") == 0)
13467 evexlig = evexl128;
13468 else if (strcmp (arg, "256") == 0)
13469 evexlig = evexl256;
13470 else if (strcmp (arg, "512") == 0)
13471 evexlig = evexl512;
13472 else
13473 as_fatal (_("invalid -mevexlig= option: `%s'"), arg);
13474 break;
13475
d3d3c6db
IT
13476 case OPTION_MEVEXRCIG:
13477 if (strcmp (arg, "rne") == 0)
13478 evexrcig = rne;
13479 else if (strcmp (arg, "rd") == 0)
13480 evexrcig = rd;
13481 else if (strcmp (arg, "ru") == 0)
13482 evexrcig = ru;
13483 else if (strcmp (arg, "rz") == 0)
13484 evexrcig = rz;
13485 else
13486 as_fatal (_("invalid -mevexrcig= option: `%s'"), arg);
13487 break;
13488
43234a1e
L
13489 case OPTION_MEVEXWIG:
13490 if (strcmp (arg, "0") == 0)
13491 evexwig = evexw0;
13492 else if (strcmp (arg, "1") == 0)
13493 evexwig = evexw1;
13494 else
13495 as_fatal (_("invalid -mevexwig= option: `%s'"), arg);
13496 break;
13497
167ad85b
TG
13498# if defined (TE_PE) || defined (TE_PEP)
13499 case OPTION_MBIG_OBJ:
13500 use_big_obj = 1;
13501 break;
13502#endif
13503
d1982f93 13504 case OPTION_MOMIT_LOCK_PREFIX:
d022bddd
IT
13505 if (strcasecmp (arg, "yes") == 0)
13506 omit_lock_prefix = 1;
13507 else if (strcasecmp (arg, "no") == 0)
13508 omit_lock_prefix = 0;
13509 else
13510 as_fatal (_("invalid -momit-lock-prefix= option: `%s'"), arg);
13511 break;
13512
e4e00185
AS
13513 case OPTION_MFENCE_AS_LOCK_ADD:
13514 if (strcasecmp (arg, "yes") == 0)
13515 avoid_fence = 1;
13516 else if (strcasecmp (arg, "no") == 0)
13517 avoid_fence = 0;
13518 else
13519 as_fatal (_("invalid -mfence-as-lock-add= option: `%s'"), arg);
13520 break;
13521
ae531041
L
13522 case OPTION_MLFENCE_AFTER_LOAD:
13523 if (strcasecmp (arg, "yes") == 0)
13524 lfence_after_load = 1;
13525 else if (strcasecmp (arg, "no") == 0)
13526 lfence_after_load = 0;
13527 else
13528 as_fatal (_("invalid -mlfence-after-load= option: `%s'"), arg);
13529 break;
13530
13531 case OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH:
13532 if (strcasecmp (arg, "all") == 0)
a09f656b 13533 {
13534 lfence_before_indirect_branch = lfence_branch_all;
13535 if (lfence_before_ret == lfence_before_ret_none)
13536 lfence_before_ret = lfence_before_ret_shl;
13537 }
ae531041
L
13538 else if (strcasecmp (arg, "memory") == 0)
13539 lfence_before_indirect_branch = lfence_branch_memory;
13540 else if (strcasecmp (arg, "register") == 0)
13541 lfence_before_indirect_branch = lfence_branch_register;
13542 else if (strcasecmp (arg, "none") == 0)
13543 lfence_before_indirect_branch = lfence_branch_none;
13544 else
13545 as_fatal (_("invalid -mlfence-before-indirect-branch= option: `%s'"),
13546 arg);
13547 break;
13548
13549 case OPTION_MLFENCE_BEFORE_RET:
13550 if (strcasecmp (arg, "or") == 0)
13551 lfence_before_ret = lfence_before_ret_or;
13552 else if (strcasecmp (arg, "not") == 0)
13553 lfence_before_ret = lfence_before_ret_not;
a09f656b 13554 else if (strcasecmp (arg, "shl") == 0 || strcasecmp (arg, "yes") == 0)
13555 lfence_before_ret = lfence_before_ret_shl;
ae531041
L
13556 else if (strcasecmp (arg, "none") == 0)
13557 lfence_before_ret = lfence_before_ret_none;
13558 else
13559 as_fatal (_("invalid -mlfence-before-ret= option: `%s'"),
13560 arg);
13561 break;
13562
0cb4071e
L
13563 case OPTION_MRELAX_RELOCATIONS:
13564 if (strcasecmp (arg, "yes") == 0)
13565 generate_relax_relocations = 1;
13566 else if (strcasecmp (arg, "no") == 0)
13567 generate_relax_relocations = 0;
13568 else
13569 as_fatal (_("invalid -mrelax-relocations= option: `%s'"), arg);
13570 break;
13571
e379e5f3
L
13572 case OPTION_MALIGN_BRANCH_BOUNDARY:
13573 {
13574 char *end;
13575 long int align = strtoul (arg, &end, 0);
13576 if (*end == '\0')
13577 {
13578 if (align == 0)
13579 {
13580 align_branch_power = 0;
13581 break;
13582 }
13583 else if (align >= 16)
13584 {
13585 int align_power;
13586 for (align_power = 0;
13587 (align & 1) == 0;
13588 align >>= 1, align_power++)
13589 continue;
13590 /* Limit alignment power to 31. */
13591 if (align == 1 && align_power < 32)
13592 {
13593 align_branch_power = align_power;
13594 break;
13595 }
13596 }
13597 }
13598 as_fatal (_("invalid -malign-branch-boundary= value: %s"), arg);
13599 }
13600 break;
13601
13602 case OPTION_MALIGN_BRANCH_PREFIX_SIZE:
13603 {
13604 char *end;
13605 int align = strtoul (arg, &end, 0);
13606 /* Some processors only support 5 prefixes. */
13607 if (*end == '\0' && align >= 0 && align < 6)
13608 {
13609 align_branch_prefix_size = align;
13610 break;
13611 }
13612 as_fatal (_("invalid -malign-branch-prefix-size= value: %s"),
13613 arg);
13614 }
13615 break;
13616
13617 case OPTION_MALIGN_BRANCH:
13618 align_branch = 0;
13619 saved = xstrdup (arg);
13620 type = saved;
13621 do
13622 {
13623 next = strchr (type, '+');
13624 if (next)
13625 *next++ = '\0';
13626 if (strcasecmp (type, "jcc") == 0)
13627 align_branch |= align_branch_jcc_bit;
13628 else if (strcasecmp (type, "fused") == 0)
13629 align_branch |= align_branch_fused_bit;
13630 else if (strcasecmp (type, "jmp") == 0)
13631 align_branch |= align_branch_jmp_bit;
13632 else if (strcasecmp (type, "call") == 0)
13633 align_branch |= align_branch_call_bit;
13634 else if (strcasecmp (type, "ret") == 0)
13635 align_branch |= align_branch_ret_bit;
13636 else if (strcasecmp (type, "indirect") == 0)
13637 align_branch |= align_branch_indirect_bit;
13638 else
13639 as_fatal (_("invalid -malign-branch= option: `%s'"), arg);
13640 type = next;
13641 }
13642 while (next != NULL);
13643 free (saved);
13644 break;
13645
76cf450b
L
13646 case OPTION_MBRANCHES_WITH_32B_BOUNDARIES:
13647 align_branch_power = 5;
13648 align_branch_prefix_size = 5;
13649 align_branch = (align_branch_jcc_bit
13650 | align_branch_fused_bit
13651 | align_branch_jmp_bit);
13652 break;
13653
5db04b09 13654 case OPTION_MAMD64:
4b5aaf5f 13655 isa64 = amd64;
5db04b09
L
13656 break;
13657
13658 case OPTION_MINTEL64:
4b5aaf5f 13659 isa64 = intel64;
5db04b09
L
13660 break;
13661
b6f8c7c4
L
13662 case 'O':
13663 if (arg == NULL)
13664 {
13665 optimize = 1;
13666 /* Turn off -Os. */
13667 optimize_for_space = 0;
13668 }
13669 else if (*arg == 's')
13670 {
13671 optimize_for_space = 1;
13672 /* Turn on all encoding optimizations. */
41fd2579 13673 optimize = INT_MAX;
b6f8c7c4
L
13674 }
13675 else
13676 {
13677 optimize = atoi (arg);
13678 /* Turn off -Os. */
13679 optimize_for_space = 0;
13680 }
13681 break;
13682
252b5132
RH
13683 default:
13684 return 0;
13685 }
13686 return 1;
13687}
13688
8a2c8fef
L
13689#define MESSAGE_TEMPLATE \
13690" "
13691
293f5f65
L
13692static char *
13693output_message (FILE *stream, char *p, char *message, char *start,
13694 int *left_p, const char *name, int len)
13695{
13696 int size = sizeof (MESSAGE_TEMPLATE);
13697 int left = *left_p;
13698
13699 /* Reserve 2 spaces for ", " or ",\0" */
13700 left -= len + 2;
13701
13702 /* Check if there is any room. */
13703 if (left >= 0)
13704 {
13705 if (p != start)
13706 {
13707 *p++ = ',';
13708 *p++ = ' ';
13709 }
13710 p = mempcpy (p, name, len);
13711 }
13712 else
13713 {
13714 /* Output the current message now and start a new one. */
13715 *p++ = ',';
13716 *p = '\0';
13717 fprintf (stream, "%s\n", message);
13718 p = start;
13719 left = size - (start - message) - len - 2;
13720
13721 gas_assert (left >= 0);
13722
13723 p = mempcpy (p, name, len);
13724 }
13725
13726 *left_p = left;
13727 return p;
13728}
13729
8a2c8fef 13730static void
1ded5609 13731show_arch (FILE *stream, int ext, int check)
8a2c8fef
L
13732{
13733 static char message[] = MESSAGE_TEMPLATE;
13734 char *start = message + 27;
13735 char *p;
13736 int size = sizeof (MESSAGE_TEMPLATE);
13737 int left;
13738 const char *name;
13739 int len;
13740 unsigned int j;
13741
13742 p = start;
13743 left = size - (start - message);
3ce2ebcf
JB
13744
13745 if (!ext && check)
13746 {
13747 p = output_message (stream, p, message, start, &left,
13748 STRING_COMMA_LEN ("default"));
f68697e8
JB
13749 p = output_message (stream, p, message, start, &left,
13750 STRING_COMMA_LEN ("push"));
13751 p = output_message (stream, p, message, start, &left,
13752 STRING_COMMA_LEN ("pop"));
3ce2ebcf
JB
13753 }
13754
8a2c8fef
L
13755 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13756 {
13757 /* Should it be skipped? */
13758 if (cpu_arch [j].skip)
13759 continue;
13760
13761 name = cpu_arch [j].name;
13762 len = cpu_arch [j].len;
ae89daec 13763 if (cpu_arch[j].type == PROCESSOR_NONE)
8a2c8fef
L
13764 {
13765 /* It is an extension. Skip if we aren't asked to show it. */
ae89daec 13766 if (!ext || cpu_flags_all_zero (&cpu_arch[j].enable))
8a2c8fef
L
13767 continue;
13768 }
13769 else if (ext)
13770 {
13771 /* It is an processor. Skip if we show only extension. */
13772 continue;
13773 }
ae89daec 13774 else if (check && ! cpu_arch[j].enable.bitfield.cpui386)
1ded5609
JB
13775 {
13776 /* It is an impossible processor - skip. */
13777 continue;
13778 }
8a2c8fef 13779
293f5f65 13780 p = output_message (stream, p, message, start, &left, name, len);
8a2c8fef
L
13781 }
13782
293f5f65
L
13783 /* Display disabled extensions. */
13784 if (ext)
ae89daec 13785 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
293f5f65 13786 {
ae89daec
JB
13787 char *str;
13788
13789 if (cpu_arch[j].type != PROCESSOR_NONE
13790 || !cpu_flags_all_zero (&cpu_arch[j].enable))
13791 continue;
13792 str = xasprintf ("no%s", cpu_arch[j].name);
13793 p = output_message (stream, p, message, start, &left, str,
13794 strlen (str));
13795 free (str);
293f5f65
L
13796 }
13797
8a2c8fef
L
13798 *p = '\0';
13799 fprintf (stream, "%s\n", message);
13800}
13801
252b5132 13802void
8a2c8fef 13803md_show_usage (FILE *stream)
252b5132 13804{
4cc782b5
ILT
13805#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13806 fprintf (stream, _("\
d4693039 13807 -Qy, -Qn ignored\n\
a38cf1db 13808 -V print assembler version number\n\
b3b91714
AM
13809 -k ignored\n"));
13810#endif
13811 fprintf (stream, _("\
7ebd68d1
NC
13812 -n do not optimize code alignment\n\
13813 -O{012s} attempt some code optimizations\n\
b3b91714
AM
13814 -q quieten some warnings\n"));
13815#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13816 fprintf (stream, _("\
a38cf1db 13817 -s ignored\n"));
b3b91714 13818#endif
b00af7c8
JB
13819#ifdef BFD64
13820# if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13821 fprintf (stream, _("\
13822 --32/--64/--x32 generate 32bit/64bit/x32 object\n"));
13823# elif defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O)
751d281c 13824 fprintf (stream, _("\
b00af7c8
JB
13825 --32/--64 generate 32bit/64bit object\n"));
13826# endif
751d281c 13827#endif
b3b91714
AM
13828#ifdef SVR4_COMMENT_CHARS
13829 fprintf (stream, _("\
13830 --divide do not treat `/' as a comment character\n"));
a38cf1db
AM
13831#else
13832 fprintf (stream, _("\
b3b91714 13833 --divide ignored\n"));
4cc782b5 13834#endif
9103f4f4 13835 fprintf (stream, _("\
6305a203 13836 -march=CPU[,+EXTENSION...]\n\
8a2c8fef 13837 generate code for CPU and EXTENSION, CPU is one of:\n"));
1ded5609 13838 show_arch (stream, 0, 1);
8a2c8fef 13839 fprintf (stream, _("\
ae89daec 13840 EXTENSION is combination of (possibly \"no\"-prefixed):\n"));
1ded5609 13841 show_arch (stream, 1, 0);
6305a203 13842 fprintf (stream, _("\
8a2c8fef 13843 -mtune=CPU optimize for CPU, CPU is one of:\n"));
1ded5609 13844 show_arch (stream, 0, 0);
ba104c83 13845 fprintf (stream, _("\
c0f3af97
L
13846 -msse2avx encode SSE instructions with VEX prefix\n"));
13847 fprintf (stream, _("\
c8480b58
L
13848 -muse-unaligned-vector-move\n\
13849 encode aligned vector move as unaligned vector move\n"));
13850 fprintf (stream, _("\
7c5c05ef 13851 -msse-check=[none|error|warning] (default: warning)\n\
daf50ae7
L
13852 check SSE instructions\n"));
13853 fprintf (stream, _("\
7c5c05ef 13854 -moperand-check=[none|error|warning] (default: warning)\n\
7bab8ab5
JB
13855 check operand combinations for validity\n"));
13856 fprintf (stream, _("\
7c5c05ef
L
13857 -mavxscalar=[128|256] (default: 128)\n\
13858 encode scalar AVX instructions with specific vector\n\
539f890d
L
13859 length\n"));
13860 fprintf (stream, _("\
03751133
L
13861 -mvexwig=[0|1] (default: 0)\n\
13862 encode VEX instructions with specific VEX.W value\n\
13863 for VEX.W bit ignored instructions\n"));
13864 fprintf (stream, _("\
7c5c05ef
L
13865 -mevexlig=[128|256|512] (default: 128)\n\
13866 encode scalar EVEX instructions with specific vector\n\
43234a1e
L
13867 length\n"));
13868 fprintf (stream, _("\
7c5c05ef
L
13869 -mevexwig=[0|1] (default: 0)\n\
13870 encode EVEX instructions with specific EVEX.W value\n\
43234a1e
L
13871 for EVEX.W bit ignored instructions\n"));
13872 fprintf (stream, _("\
7c5c05ef 13873 -mevexrcig=[rne|rd|ru|rz] (default: rne)\n\
d3d3c6db
IT
13874 encode EVEX instructions with specific EVEX.RC value\n\
13875 for SAE-only ignored instructions\n"));
13876 fprintf (stream, _("\
7c5c05ef
L
13877 -mmnemonic=[att|intel] "));
13878 if (SYSV386_COMPAT)
13879 fprintf (stream, _("(default: att)\n"));
13880 else
13881 fprintf (stream, _("(default: intel)\n"));
13882 fprintf (stream, _("\
13883 use AT&T/Intel mnemonic\n"));
ba104c83 13884 fprintf (stream, _("\
7c5c05ef
L
13885 -msyntax=[att|intel] (default: att)\n\
13886 use AT&T/Intel syntax\n"));
ba104c83
L
13887 fprintf (stream, _("\
13888 -mindex-reg support pseudo index registers\n"));
13889 fprintf (stream, _("\
13890 -mnaked-reg don't require `%%' prefix for registers\n"));
13891 fprintf (stream, _("\
7e8b059b 13892 -madd-bnd-prefix add BND prefix for all valid branches\n"));
b4a3a7b4 13893#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8dcea932
L
13894 fprintf (stream, _("\
13895 -mshared disable branch optimization for shared code\n"));
b4a3a7b4
L
13896 fprintf (stream, _("\
13897 -mx86-used-note=[no|yes] "));
13898 if (DEFAULT_X86_USED_NOTE)
13899 fprintf (stream, _("(default: yes)\n"));
13900 else
13901 fprintf (stream, _("(default: no)\n"));
13902 fprintf (stream, _("\
13903 generate x86 used ISA and feature properties\n"));
13904#endif
13905#if defined (TE_PE) || defined (TE_PEP)
167ad85b
TG
13906 fprintf (stream, _("\
13907 -mbig-obj generate big object files\n"));
13908#endif
d022bddd 13909 fprintf (stream, _("\
7c5c05ef 13910 -momit-lock-prefix=[no|yes] (default: no)\n\
d022bddd 13911 strip all lock prefixes\n"));
5db04b09 13912 fprintf (stream, _("\
7c5c05ef 13913 -mfence-as-lock-add=[no|yes] (default: no)\n\
e4e00185
AS
13914 encode lfence, mfence and sfence as\n\
13915 lock addl $0x0, (%%{re}sp)\n"));
13916 fprintf (stream, _("\
7c5c05ef
L
13917 -mrelax-relocations=[no|yes] "));
13918 if (DEFAULT_GENERATE_X86_RELAX_RELOCATIONS)
13919 fprintf (stream, _("(default: yes)\n"));
13920 else
13921 fprintf (stream, _("(default: no)\n"));
13922 fprintf (stream, _("\
0cb4071e
L
13923 generate relax relocations\n"));
13924 fprintf (stream, _("\
e379e5f3
L
13925 -malign-branch-boundary=NUM (default: 0)\n\
13926 align branches within NUM byte boundary\n"));
13927 fprintf (stream, _("\
13928 -malign-branch=TYPE[+TYPE...] (default: jcc+fused+jmp)\n\
13929 TYPE is combination of jcc, fused, jmp, call, ret,\n\
13930 indirect\n\
13931 specify types of branches to align\n"));
13932 fprintf (stream, _("\
13933 -malign-branch-prefix-size=NUM (default: 5)\n\
13934 align branches with NUM prefixes per instruction\n"));
13935 fprintf (stream, _("\
76cf450b
L
13936 -mbranches-within-32B-boundaries\n\
13937 align branches within 32 byte boundary\n"));
13938 fprintf (stream, _("\
ae531041
L
13939 -mlfence-after-load=[no|yes] (default: no)\n\
13940 generate lfence after load\n"));
13941 fprintf (stream, _("\
13942 -mlfence-before-indirect-branch=[none|all|register|memory] (default: none)\n\
13943 generate lfence before indirect near branch\n"));
13944 fprintf (stream, _("\
a09f656b 13945 -mlfence-before-ret=[none|or|not|shl|yes] (default: none)\n\
ae531041
L
13946 generate lfence before ret\n"));
13947 fprintf (stream, _("\
7c5c05ef 13948 -mamd64 accept only AMD64 ISA [default]\n"));
5db04b09
L
13949 fprintf (stream, _("\
13950 -mintel64 accept only Intel64 ISA\n"));
252b5132
RH
13951}
13952
3e73aa7c 13953#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
321098a5 13954 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
e57f8c65 13955 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
252b5132
RH
13956
13957/* Pick the target format to use. */
13958
47926f60 13959const char *
e3bb37b5 13960i386_target_format (void)
252b5132 13961{
d34049e8 13962 if (startswith (default_arch, "x86_64"))
351f65ca
L
13963 {
13964 update_code_flag (CODE_64BIT, 1);
13965 if (default_arch[6] == '\0')
7f56bc95 13966 x86_elf_abi = X86_64_ABI;
351f65ca 13967 else
7f56bc95 13968 x86_elf_abi = X86_64_X32_ABI;
351f65ca 13969 }
3e73aa7c 13970 else if (!strcmp (default_arch, "i386"))
78f12dd3 13971 update_code_flag (CODE_32BIT, 1);
5197d474
L
13972 else if (!strcmp (default_arch, "iamcu"))
13973 {
13974 update_code_flag (CODE_32BIT, 1);
13975 if (cpu_arch_isa == PROCESSOR_UNKNOWN)
13976 {
13977 static const i386_cpu_flags iamcu_flags = CPU_IAMCU_FLAGS;
13978 cpu_arch_name = "iamcu";
d92c7521 13979 free (cpu_sub_arch_name);
5197d474
L
13980 cpu_sub_arch_name = NULL;
13981 cpu_arch_flags = iamcu_flags;
13982 cpu_arch_isa = PROCESSOR_IAMCU;
13983 cpu_arch_isa_flags = iamcu_flags;
13984 if (!cpu_arch_tune_set)
13985 {
13986 cpu_arch_tune = cpu_arch_isa;
13987 cpu_arch_tune_flags = cpu_arch_isa_flags;
13988 }
13989 }
8d471ec1 13990 else if (cpu_arch_isa != PROCESSOR_IAMCU)
5197d474
L
13991 as_fatal (_("Intel MCU doesn't support `%s' architecture"),
13992 cpu_arch_name);
13993 }
3e73aa7c 13994 else
2b5d6a91 13995 as_fatal (_("unknown architecture"));
89507696
JB
13996
13997 if (cpu_flags_all_zero (&cpu_arch_isa_flags))
ae89daec 13998 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
89507696 13999 if (cpu_flags_all_zero (&cpu_arch_tune_flags))
ae89daec 14000 cpu_arch_tune_flags = cpu_arch[flag_code == CODE_64BIT].enable;
89507696 14001
252b5132
RH
14002 switch (OUTPUT_FLAVOR)
14003 {
9384f2ff 14004#if defined (OBJ_MAYBE_AOUT) || defined (OBJ_AOUT)
4c63da97 14005 case bfd_target_aout_flavour:
47926f60 14006 return AOUT_TARGET_FORMAT;
4c63da97 14007#endif
9384f2ff
AM
14008#if defined (OBJ_MAYBE_COFF) || defined (OBJ_COFF)
14009# if defined (TE_PE) || defined (TE_PEP)
14010 case bfd_target_coff_flavour:
167ad85b 14011 if (flag_code == CODE_64BIT)
eb19308f
JB
14012 {
14013 object_64bit = 1;
14014 return use_big_obj ? "pe-bigobj-x86-64" : "pe-x86-64";
14015 }
14016 return use_big_obj ? "pe-bigobj-i386" : "pe-i386";
9384f2ff 14017# elif defined (TE_GO32)
0561d57c
JK
14018 case bfd_target_coff_flavour:
14019 return "coff-go32";
9384f2ff 14020# else
252b5132
RH
14021 case bfd_target_coff_flavour:
14022 return "coff-i386";
9384f2ff 14023# endif
4c63da97 14024#endif
3e73aa7c 14025#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
252b5132 14026 case bfd_target_elf_flavour:
3e73aa7c 14027 {
351f65ca
L
14028 const char *format;
14029
14030 switch (x86_elf_abi)
4fa24527 14031 {
351f65ca
L
14032 default:
14033 format = ELF_TARGET_FORMAT;
e379e5f3
L
14034#ifndef TE_SOLARIS
14035 tls_get_addr = "___tls_get_addr";
14036#endif
351f65ca 14037 break;
7f56bc95 14038 case X86_64_ABI:
351f65ca 14039 use_rela_relocations = 1;
4fa24527 14040 object_64bit = 1;
e379e5f3
L
14041#ifndef TE_SOLARIS
14042 tls_get_addr = "__tls_get_addr";
14043#endif
351f65ca
L
14044 format = ELF_TARGET_FORMAT64;
14045 break;
7f56bc95 14046 case X86_64_X32_ABI:
4fa24527 14047 use_rela_relocations = 1;
351f65ca 14048 object_64bit = 1;
e379e5f3
L
14049#ifndef TE_SOLARIS
14050 tls_get_addr = "__tls_get_addr";
14051#endif
862be3fb 14052 disallow_64bit_reloc = 1;
351f65ca
L
14053 format = ELF_TARGET_FORMAT32;
14054 break;
4fa24527 14055 }
c085ab00 14056 if (cpu_arch_isa == PROCESSOR_IAMCU)
81486035
L
14057 {
14058 if (x86_elf_abi != I386_ABI)
14059 as_fatal (_("Intel MCU is 32bit only"));
14060 return ELF_TARGET_IAMCU_FORMAT;
14061 }
8a9036a4 14062 else
351f65ca 14063 return format;
3e73aa7c 14064 }
e57f8c65
TG
14065#endif
14066#if defined (OBJ_MACH_O)
14067 case bfd_target_mach_o_flavour:
d382c579
TG
14068 if (flag_code == CODE_64BIT)
14069 {
14070 use_rela_relocations = 1;
14071 object_64bit = 1;
14072 return "mach-o-x86-64";
14073 }
14074 else
14075 return "mach-o-i386";
4c63da97 14076#endif
252b5132
RH
14077 default:
14078 abort ();
14079 return NULL;
14080 }
14081}
14082
47926f60 14083#endif /* OBJ_MAYBE_ more than one */
252b5132 14084\f
252b5132 14085symbolS *
7016a5d5 14086md_undefined_symbol (char *name)
252b5132 14087{
18dc2407
ILT
14088 if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
14089 && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
14090 && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
14091 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
24eab124
AM
14092 {
14093 if (!GOT_symbol)
14094 {
14095 if (symbol_find (name))
14096 as_bad (_("GOT already in symbol table"));
14097 GOT_symbol = symbol_new (name, undefined_section,
e01e1cee 14098 &zero_address_frag, 0);
24eab124
AM
14099 };
14100 return GOT_symbol;
14101 }
252b5132
RH
14102 return 0;
14103}
14104
14105/* Round up a section size to the appropriate boundary. */
47926f60 14106
252b5132 14107valueT
7016a5d5 14108md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
252b5132 14109{
4c63da97
AM
14110#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
14111 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
14112 {
14113 /* For a.out, force the section size to be aligned. If we don't do
14114 this, BFD will align it for us, but it will not write out the
14115 final bytes of the section. This may be a bug in BFD, but it is
14116 easier to fix it here since that is how the other a.out targets
14117 work. */
14118 int align;
14119
fd361982 14120 align = bfd_section_alignment (segment);
8d3842cd 14121 size = ((size + (1 << align) - 1) & (-((valueT) 1 << align)));
4c63da97 14122 }
252b5132
RH
14123#endif
14124
14125 return size;
14126}
14127
14128/* On the i386, PC-relative offsets are relative to the start of the
14129 next instruction. That is, the address of the offset, plus its
14130 size, since the offset is always the last part of the insn. */
14131
14132long
e3bb37b5 14133md_pcrel_from (fixS *fixP)
252b5132
RH
14134{
14135 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
14136}
14137
14138#ifndef I386COFF
14139
14140static void
e3bb37b5 14141s_bss (int ignore ATTRIBUTE_UNUSED)
252b5132 14142{
29b0f896 14143 int temp;
252b5132 14144
8a75718c
JB
14145#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14146 if (IS_ELF)
14147 obj_elf_section_change_hook ();
14148#endif
252b5132
RH
14149 temp = get_absolute_expression ();
14150 subseg_set (bss_section, (subsegT) temp);
14151 demand_empty_rest_of_line ();
14152}
14153
14154#endif
14155
e379e5f3
L
14156/* Remember constant directive. */
14157
14158void
14159i386_cons_align (int ignore ATTRIBUTE_UNUSED)
14160{
14161 if (last_insn.kind != last_insn_directive
14162 && (bfd_section_flags (now_seg) & SEC_CODE))
14163 {
14164 last_insn.seg = now_seg;
14165 last_insn.kind = last_insn_directive;
14166 last_insn.name = "constant directive";
14167 last_insn.file = as_where (&last_insn.line);
ae531041
L
14168 if (lfence_before_ret != lfence_before_ret_none)
14169 {
14170 if (lfence_before_indirect_branch != lfence_branch_none)
14171 as_warn (_("constant directive skips -mlfence-before-ret "
14172 "and -mlfence-before-indirect-branch"));
14173 else
14174 as_warn (_("constant directive skips -mlfence-before-ret"));
14175 }
14176 else if (lfence_before_indirect_branch != lfence_branch_none)
14177 as_warn (_("constant directive skips -mlfence-before-indirect-branch"));
e379e5f3
L
14178 }
14179}
14180
3abbafc2 14181int
e3bb37b5 14182i386_validate_fix (fixS *fixp)
252b5132 14183{
e52a16f2
JB
14184 if (fixp->fx_addsy && S_GET_SEGMENT(fixp->fx_addsy) == reg_section)
14185 {
14186 reloc_howto_type *howto;
14187
14188 howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
14189 as_bad_where (fixp->fx_file, fixp->fx_line,
14190 _("invalid %s relocation against register"),
14191 howto ? howto->name : "<unknown>");
14192 return 0;
14193 }
14194
3abbafc2
JB
14195#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14196 if (fixp->fx_r_type == BFD_RELOC_SIZE32
14197 || fixp->fx_r_type == BFD_RELOC_SIZE64)
14198 return IS_ELF && fixp->fx_addsy
14199 && (!S_IS_DEFINED (fixp->fx_addsy)
14200 || S_IS_EXTERNAL (fixp->fx_addsy));
14201#endif
14202
02a86693 14203 if (fixp->fx_subsy)
252b5132 14204 {
02a86693 14205 if (fixp->fx_subsy == GOT_symbol)
23df1078 14206 {
02a86693
L
14207 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
14208 {
14209 if (!object_64bit)
14210 abort ();
14211#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14212 if (fixp->fx_tcbit2)
56ceb5b5
L
14213 fixp->fx_r_type = (fixp->fx_tcbit
14214 ? BFD_RELOC_X86_64_REX_GOTPCRELX
14215 : BFD_RELOC_X86_64_GOTPCRELX);
02a86693
L
14216 else
14217#endif
14218 fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
14219 }
d6ab8113 14220 else
02a86693
L
14221 {
14222 if (!object_64bit)
14223 fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
14224 else
14225 fixp->fx_r_type = BFD_RELOC_X86_64_GOTOFF64;
14226 }
14227 fixp->fx_subsy = 0;
23df1078 14228 }
252b5132 14229 }
02a86693 14230#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2585b7a5 14231 else
02a86693 14232 {
2585b7a5
L
14233 /* NB: Commit 292676c1 resolved PLT32 reloc aganst local symbol
14234 to section. Since PLT32 relocation must be against symbols,
14235 turn such PLT32 relocation into PC32 relocation. */
14236 if (fixp->fx_addsy
14237 && (fixp->fx_r_type == BFD_RELOC_386_PLT32
14238 || fixp->fx_r_type == BFD_RELOC_X86_64_PLT32)
14239 && symbol_section_p (fixp->fx_addsy))
14240 fixp->fx_r_type = BFD_RELOC_32_PCREL;
14241 if (!object_64bit)
14242 {
14243 if (fixp->fx_r_type == BFD_RELOC_386_GOT32
14244 && fixp->fx_tcbit2)
14245 fixp->fx_r_type = BFD_RELOC_386_GOT32X;
14246 }
02a86693
L
14247 }
14248#endif
3abbafc2
JB
14249
14250 return 1;
252b5132
RH
14251}
14252
252b5132 14253arelent *
7016a5d5 14254tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
14255{
14256 arelent *rel;
14257 bfd_reloc_code_real_type code;
14258
14259 switch (fixp->fx_r_type)
14260 {
8ce3d284 14261#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3abbafc2
JB
14262 symbolS *sym;
14263
8fd4256d
L
14264 case BFD_RELOC_SIZE32:
14265 case BFD_RELOC_SIZE64:
3abbafc2
JB
14266 if (fixp->fx_addsy
14267 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))
14268 && (!fixp->fx_subsy
14269 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))))
14270 sym = fixp->fx_addsy;
14271 else if (fixp->fx_subsy
14272 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))
14273 && (!fixp->fx_addsy
14274 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))))
14275 sym = fixp->fx_subsy;
14276 else
14277 sym = NULL;
14278 if (IS_ELF && sym && S_IS_DEFINED (sym) && !S_IS_EXTERNAL (sym))
8fd4256d
L
14279 {
14280 /* Resolve size relocation against local symbol to size of
14281 the symbol plus addend. */
3abbafc2 14282 valueT value = S_GET_SIZE (sym);
44f87162 14283
3abbafc2
JB
14284 if (symbol_get_bfdsym (sym)->flags & BSF_SECTION_SYM)
14285 value = bfd_section_size (S_GET_SEGMENT (sym));
14286 if (sym == fixp->fx_subsy)
14287 {
14288 value = -value;
14289 if (fixp->fx_addsy)
14290 value += S_GET_VALUE (fixp->fx_addsy);
14291 }
14292 else if (fixp->fx_subsy)
14293 value -= S_GET_VALUE (fixp->fx_subsy);
44f87162 14294 value += fixp->fx_offset;
8fd4256d 14295 if (fixp->fx_r_type == BFD_RELOC_SIZE32
d965814f 14296 && object_64bit
8fd4256d
L
14297 && !fits_in_unsigned_long (value))
14298 as_bad_where (fixp->fx_file, fixp->fx_line,
14299 _("symbol size computation overflow"));
14300 fixp->fx_addsy = NULL;
14301 fixp->fx_subsy = NULL;
14302 md_apply_fix (fixp, (valueT *) &value, NULL);
14303 return NULL;
14304 }
3abbafc2
JB
14305 if (!fixp->fx_addsy || fixp->fx_subsy)
14306 {
14307 as_bad_where (fixp->fx_file, fixp->fx_line,
14308 "unsupported expression involving @size");
14309 return NULL;
14310 }
8ce3d284 14311#endif
1a0670f3 14312 /* Fall through. */
8fd4256d 14313
3e73aa7c
JH
14314 case BFD_RELOC_X86_64_PLT32:
14315 case BFD_RELOC_X86_64_GOT32:
14316 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
14317 case BFD_RELOC_X86_64_GOTPCRELX:
14318 case BFD_RELOC_X86_64_REX_GOTPCRELX:
252b5132
RH
14319 case BFD_RELOC_386_PLT32:
14320 case BFD_RELOC_386_GOT32:
02a86693 14321 case BFD_RELOC_386_GOT32X:
252b5132
RH
14322 case BFD_RELOC_386_GOTOFF:
14323 case BFD_RELOC_386_GOTPC:
13ae64f3
JJ
14324 case BFD_RELOC_386_TLS_GD:
14325 case BFD_RELOC_386_TLS_LDM:
14326 case BFD_RELOC_386_TLS_LDO_32:
14327 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
14328 case BFD_RELOC_386_TLS_IE:
14329 case BFD_RELOC_386_TLS_GOTIE:
13ae64f3
JJ
14330 case BFD_RELOC_386_TLS_LE_32:
14331 case BFD_RELOC_386_TLS_LE:
67a4f2b7
AO
14332 case BFD_RELOC_386_TLS_GOTDESC:
14333 case BFD_RELOC_386_TLS_DESC_CALL:
bffbf940
JJ
14334 case BFD_RELOC_X86_64_TLSGD:
14335 case BFD_RELOC_X86_64_TLSLD:
14336 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 14337 case BFD_RELOC_X86_64_DTPOFF64:
bffbf940
JJ
14338 case BFD_RELOC_X86_64_GOTTPOFF:
14339 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113
JB
14340 case BFD_RELOC_X86_64_TPOFF64:
14341 case BFD_RELOC_X86_64_GOTOFF64:
14342 case BFD_RELOC_X86_64_GOTPC32:
7b81dfbb
AJ
14343 case BFD_RELOC_X86_64_GOT64:
14344 case BFD_RELOC_X86_64_GOTPCREL64:
14345 case BFD_RELOC_X86_64_GOTPC64:
14346 case BFD_RELOC_X86_64_GOTPLT64:
14347 case BFD_RELOC_X86_64_PLTOFF64:
67a4f2b7
AO
14348 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14349 case BFD_RELOC_X86_64_TLSDESC_CALL:
252b5132
RH
14350 case BFD_RELOC_RVA:
14351 case BFD_RELOC_VTABLE_ENTRY:
14352 case BFD_RELOC_VTABLE_INHERIT:
6482c264
NC
14353#ifdef TE_PE
14354 case BFD_RELOC_32_SECREL:
145667f8 14355 case BFD_RELOC_16_SECIDX:
6482c264 14356#endif
252b5132
RH
14357 code = fixp->fx_r_type;
14358 break;
dbbaec26
L
14359 case BFD_RELOC_X86_64_32S:
14360 if (!fixp->fx_pcrel)
14361 {
14362 /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32. */
14363 code = fixp->fx_r_type;
14364 break;
14365 }
1a0670f3 14366 /* Fall through. */
252b5132 14367 default:
93382f6d 14368 if (fixp->fx_pcrel)
252b5132 14369 {
93382f6d
AM
14370 switch (fixp->fx_size)
14371 {
14372 default:
b091f402
AM
14373 as_bad_where (fixp->fx_file, fixp->fx_line,
14374 _("can not do %d byte pc-relative relocation"),
14375 fixp->fx_size);
93382f6d
AM
14376 code = BFD_RELOC_32_PCREL;
14377 break;
14378 case 1: code = BFD_RELOC_8_PCREL; break;
14379 case 2: code = BFD_RELOC_16_PCREL; break;
d258b828 14380 case 4: code = BFD_RELOC_32_PCREL; break;
d6ab8113
JB
14381#ifdef BFD64
14382 case 8: code = BFD_RELOC_64_PCREL; break;
14383#endif
93382f6d
AM
14384 }
14385 }
14386 else
14387 {
14388 switch (fixp->fx_size)
14389 {
14390 default:
b091f402
AM
14391 as_bad_where (fixp->fx_file, fixp->fx_line,
14392 _("can not do %d byte relocation"),
14393 fixp->fx_size);
93382f6d
AM
14394 code = BFD_RELOC_32;
14395 break;
14396 case 1: code = BFD_RELOC_8; break;
14397 case 2: code = BFD_RELOC_16; break;
14398 case 4: code = BFD_RELOC_32; break;
937149dd 14399#ifdef BFD64
3e73aa7c 14400 case 8: code = BFD_RELOC_64; break;
937149dd 14401#endif
93382f6d 14402 }
252b5132
RH
14403 }
14404 break;
14405 }
252b5132 14406
d182319b
JB
14407 if ((code == BFD_RELOC_32
14408 || code == BFD_RELOC_32_PCREL
14409 || code == BFD_RELOC_X86_64_32S)
252b5132
RH
14410 && GOT_symbol
14411 && fixp->fx_addsy == GOT_symbol)
3e73aa7c 14412 {
4fa24527 14413 if (!object_64bit)
d6ab8113
JB
14414 code = BFD_RELOC_386_GOTPC;
14415 else
14416 code = BFD_RELOC_X86_64_GOTPC32;
3e73aa7c 14417 }
7b81dfbb
AJ
14418 if ((code == BFD_RELOC_64 || code == BFD_RELOC_64_PCREL)
14419 && GOT_symbol
14420 && fixp->fx_addsy == GOT_symbol)
14421 {
14422 code = BFD_RELOC_X86_64_GOTPC64;
14423 }
252b5132 14424
add39d23
TS
14425 rel = XNEW (arelent);
14426 rel->sym_ptr_ptr = XNEW (asymbol *);
49309057 14427 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
14428
14429 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
c87db184 14430
3e73aa7c
JH
14431 if (!use_rela_relocations)
14432 {
14433 /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
14434 vtable entry to be used in the relocation's section offset. */
14435 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
14436 rel->address = fixp->fx_offset;
fbeb56a4
DK
14437#if defined (OBJ_COFF) && defined (TE_PE)
14438 else if (fixp->fx_addsy && S_IS_WEAK (fixp->fx_addsy))
14439 rel->addend = fixp->fx_addnumber - (S_GET_VALUE (fixp->fx_addsy) * 2);
14440 else
14441#endif
c6682705 14442 rel->addend = 0;
3e73aa7c
JH
14443 }
14444 /* Use the rela in 64bit mode. */
252b5132 14445 else
3e73aa7c 14446 {
862be3fb
L
14447 if (disallow_64bit_reloc)
14448 switch (code)
14449 {
862be3fb
L
14450 case BFD_RELOC_X86_64_DTPOFF64:
14451 case BFD_RELOC_X86_64_TPOFF64:
14452 case BFD_RELOC_64_PCREL:
14453 case BFD_RELOC_X86_64_GOTOFF64:
14454 case BFD_RELOC_X86_64_GOT64:
14455 case BFD_RELOC_X86_64_GOTPCREL64:
14456 case BFD_RELOC_X86_64_GOTPC64:
14457 case BFD_RELOC_X86_64_GOTPLT64:
14458 case BFD_RELOC_X86_64_PLTOFF64:
14459 as_bad_where (fixp->fx_file, fixp->fx_line,
14460 _("cannot represent relocation type %s in x32 mode"),
14461 bfd_get_reloc_code_name (code));
14462 break;
14463 default:
14464 break;
14465 }
14466
062cd5e7
AS
14467 if (!fixp->fx_pcrel)
14468 rel->addend = fixp->fx_offset;
14469 else
14470 switch (code)
14471 {
14472 case BFD_RELOC_X86_64_PLT32:
14473 case BFD_RELOC_X86_64_GOT32:
14474 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
14475 case BFD_RELOC_X86_64_GOTPCRELX:
14476 case BFD_RELOC_X86_64_REX_GOTPCRELX:
bffbf940
JJ
14477 case BFD_RELOC_X86_64_TLSGD:
14478 case BFD_RELOC_X86_64_TLSLD:
14479 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7
AO
14480 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14481 case BFD_RELOC_X86_64_TLSDESC_CALL:
062cd5e7
AS
14482 rel->addend = fixp->fx_offset - fixp->fx_size;
14483 break;
14484 default:
14485 rel->addend = (section->vma
14486 - fixp->fx_size
14487 + fixp->fx_addnumber
14488 + md_pcrel_from (fixp));
14489 break;
14490 }
3e73aa7c
JH
14491 }
14492
252b5132
RH
14493 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
14494 if (rel->howto == NULL)
14495 {
14496 as_bad_where (fixp->fx_file, fixp->fx_line,
d0b47220 14497 _("cannot represent relocation type %s"),
252b5132
RH
14498 bfd_get_reloc_code_name (code));
14499 /* Set howto to a garbage value so that we can keep going. */
14500 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
9c2799c2 14501 gas_assert (rel->howto != NULL);
252b5132
RH
14502 }
14503
14504 return rel;
14505}
14506
ee86248c 14507#include "tc-i386-intel.c"
54cfded0 14508
a60de03c
JB
14509void
14510tc_x86_parse_to_dw2regnum (expressionS *exp)
54cfded0 14511{
a60de03c
JB
14512 int saved_naked_reg;
14513 char saved_register_dot;
54cfded0 14514
a60de03c
JB
14515 saved_naked_reg = allow_naked_reg;
14516 allow_naked_reg = 1;
14517 saved_register_dot = register_chars['.'];
14518 register_chars['.'] = '.';
14519 allow_pseudo_reg = 1;
14520 expression_and_evaluate (exp);
14521 allow_pseudo_reg = 0;
14522 register_chars['.'] = saved_register_dot;
14523 allow_naked_reg = saved_naked_reg;
14524
e96d56a1 14525 if (exp->X_op == O_register && exp->X_add_number >= 0)
54cfded0 14526 {
a60de03c
JB
14527 if ((addressT) exp->X_add_number < i386_regtab_size)
14528 {
14529 exp->X_op = O_constant;
14530 exp->X_add_number = i386_regtab[exp->X_add_number]
14531 .dw2_regnum[flag_code >> 1];
14532 }
14533 else
14534 exp->X_op = O_illegal;
54cfded0 14535 }
54cfded0
AM
14536}
14537
14538void
14539tc_x86_frame_initial_instructions (void)
14540{
a60de03c
JB
14541 static unsigned int sp_regno[2];
14542
14543 if (!sp_regno[flag_code >> 1])
14544 {
14545 char *saved_input = input_line_pointer;
14546 char sp[][4] = {"esp", "rsp"};
14547 expressionS exp;
a4447b93 14548
a60de03c
JB
14549 input_line_pointer = sp[flag_code >> 1];
14550 tc_x86_parse_to_dw2regnum (&exp);
9c2799c2 14551 gas_assert (exp.X_op == O_constant);
a60de03c
JB
14552 sp_regno[flag_code >> 1] = exp.X_add_number;
14553 input_line_pointer = saved_input;
14554 }
a4447b93 14555
61ff971f
L
14556 cfi_add_CFA_def_cfa (sp_regno[flag_code >> 1], -x86_cie_data_alignment);
14557 cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
54cfded0 14558}
d2b2c203 14559
d7921315
L
14560int
14561x86_dwarf2_addr_size (void)
14562{
14563#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
14564 if (x86_elf_abi == X86_64_X32_ABI)
14565 return 4;
14566#endif
14567 return bfd_arch_bits_per_address (stdoutput) / 8;
14568}
14569
d2b2c203
DJ
14570int
14571i386_elf_section_type (const char *str, size_t len)
14572{
14573 if (flag_code == CODE_64BIT
14574 && len == sizeof ("unwind") - 1
d34049e8 14575 && startswith (str, "unwind"))
d2b2c203
DJ
14576 return SHT_X86_64_UNWIND;
14577
14578 return -1;
14579}
bb41ade5 14580
ad5fec3b
EB
14581#ifdef TE_SOLARIS
14582void
14583i386_solaris_fix_up_eh_frame (segT sec)
14584{
14585 if (flag_code == CODE_64BIT)
14586 elf_section_type (sec) = SHT_X86_64_UNWIND;
14587}
14588#endif
14589
bb41ade5
AM
14590#ifdef TE_PE
14591void
14592tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
14593{
91d6fa6a 14594 expressionS exp;
bb41ade5 14595
91d6fa6a
NC
14596 exp.X_op = O_secrel;
14597 exp.X_add_symbol = symbol;
14598 exp.X_add_number = 0;
14599 emit_expr (&exp, size);
bb41ade5
AM
14600}
14601#endif
3b22753a
L
14602
14603#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14604/* For ELF on x86-64, add support for SHF_X86_64_LARGE. */
14605
01e1a5bc 14606bfd_vma
6d4af3c2 14607x86_64_section_letter (int letter, const char **ptr_msg)
3b22753a
L
14608{
14609 if (flag_code == CODE_64BIT)
14610 {
14611 if (letter == 'l')
14612 return SHF_X86_64_LARGE;
14613
8f3bae45 14614 *ptr_msg = _("bad .section directive: want a,l,w,x,M,S,G,T in string");
64e74474 14615 }
3b22753a 14616 else
8f3bae45 14617 *ptr_msg = _("bad .section directive: want a,w,x,M,S,G,T in string");
3b22753a
L
14618 return -1;
14619}
14620
01e1a5bc 14621bfd_vma
3b22753a
L
14622x86_64_section_word (char *str, size_t len)
14623{
08dedd66 14624 if (len == 5 && flag_code == CODE_64BIT && startswith (str, "large"))
3b22753a
L
14625 return SHF_X86_64_LARGE;
14626
14627 return -1;
14628}
14629
14630static void
14631handle_large_common (int small ATTRIBUTE_UNUSED)
14632{
14633 if (flag_code != CODE_64BIT)
14634 {
14635 s_comm_internal (0, elf_common_parse);
14636 as_warn (_(".largecomm supported only in 64bit mode, producing .comm"));
14637 }
14638 else
14639 {
14640 static segT lbss_section;
14641 asection *saved_com_section_ptr = elf_com_section_ptr;
14642 asection *saved_bss_section = bss_section;
14643
14644 if (lbss_section == NULL)
14645 {
14646 flagword applicable;
14647 segT seg = now_seg;
14648 subsegT subseg = now_subseg;
14649
14650 /* The .lbss section is for local .largecomm symbols. */
14651 lbss_section = subseg_new (".lbss", 0);
14652 applicable = bfd_applicable_section_flags (stdoutput);
fd361982 14653 bfd_set_section_flags (lbss_section, applicable & SEC_ALLOC);
3b22753a
L
14654 seg_info (lbss_section)->bss = 1;
14655
14656 subseg_set (seg, subseg);
14657 }
14658
14659 elf_com_section_ptr = &_bfd_elf_large_com_section;
14660 bss_section = lbss_section;
14661
14662 s_comm_internal (0, elf_common_parse);
14663
14664 elf_com_section_ptr = saved_com_section_ptr;
14665 bss_section = saved_bss_section;
14666 }
14667}
14668#endif /* OBJ_ELF || OBJ_MAYBE_ELF */