]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/config/tc-i386.c
x86: drop (now) stray IsString
[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"
b52c4ee4
IB
33#include "gen-sframe.h"
34#include "sframe.h"
d2b2c203 35#include "elf/x86-64.h"
40fb9820 36#include "opcodes/i386-init.h"
41fd2579 37#include <limits.h>
41fd2579 38
c3332e24 39#ifndef INFER_ADDR_PREFIX
eecb386c 40#define INFER_ADDR_PREFIX 1
c3332e24
AM
41#endif
42
29b0f896
AM
43#ifndef DEFAULT_ARCH
44#define DEFAULT_ARCH "i386"
246fcdee 45#endif
252b5132 46
edde18a5
AM
47#ifndef INLINE
48#if __GNUC__ >= 2
49#define INLINE __inline__
50#else
51#define INLINE
52#endif
53#endif
54
6305a203
L
55/* Prefixes will be emitted in the order defined below.
56 WAIT_PREFIX must be the first prefix since FWAIT is really is an
57 instruction, and so must come before any prefixes.
58 The preferred prefix order is SEG_PREFIX, ADDR_PREFIX, DATA_PREFIX,
42164a71 59 REP_PREFIX/HLE_PREFIX, LOCK_PREFIX. */
6305a203
L
60#define WAIT_PREFIX 0
61#define SEG_PREFIX 1
62#define ADDR_PREFIX 2
63#define DATA_PREFIX 3
c32fa91d 64#define REP_PREFIX 4
42164a71 65#define HLE_PREFIX REP_PREFIX
7e8b059b 66#define BND_PREFIX REP_PREFIX
c32fa91d 67#define LOCK_PREFIX 5
4e9ac44a
L
68#define REX_PREFIX 6 /* must come last. */
69#define MAX_PREFIXES 7 /* max prefixes per opcode */
6305a203
L
70
71/* we define the syntax here (modulo base,index,scale syntax) */
72#define REGISTER_PREFIX '%'
73#define IMMEDIATE_PREFIX '$'
74#define ABSOLUTE_PREFIX '*'
75
76/* these are the instruction mnemonic suffixes in AT&T syntax or
77 memory operand size in Intel syntax. */
78#define WORD_MNEM_SUFFIX 'w'
79#define BYTE_MNEM_SUFFIX 'b'
80#define SHORT_MNEM_SUFFIX 's'
81#define LONG_MNEM_SUFFIX 'l'
82#define QWORD_MNEM_SUFFIX 'q'
6305a203
L
83
84#define END_OF_INSN '\0'
85
05909f23
JB
86#define OPERAND_TYPE_NONE { .bitfield = { .class = ClassNone } }
87
79dec6b7
JB
88/* This matches the C -> StaticRounding alias in the opcode table. */
89#define commutative staticrounding
90
6305a203
L
91/*
92 'templates' is for grouping together 'template' structures for opcodes
93 of the same name. This is only used for storing the insns in the grand
94 ole hash table of insns.
95 The templates themselves start at START and range up to (but not including)
96 END.
97 */
98typedef struct
99{
d3ce72d0
NC
100 const insn_template *start;
101 const insn_template *end;
6305a203
L
102}
103templates;
104
105/* 386 operand encoding bytes: see 386 book for details of this. */
106typedef struct
107{
108 unsigned int regmem; /* codes register or memory operand */
109 unsigned int reg; /* codes register operand (or extended opcode) */
110 unsigned int mode; /* how to interpret regmem & reg */
111}
112modrm_byte;
113
114/* x86-64 extension prefix. */
115typedef int rex_byte;
116
6305a203
L
117/* 386 opcode byte to code indirect addressing. */
118typedef struct
119{
120 unsigned base;
121 unsigned index;
122 unsigned scale;
123}
124sib_byte;
125
6305a203
L
126/* x86 arch names, types and features */
127typedef struct
128{
129 const char *name; /* arch name */
6ceeed25
JB
130 unsigned int len:8; /* arch string length */
131 bool skip:1; /* show_arch should skip this. */
6305a203 132 enum processor_type type; /* arch type */
ae89daec
JB
133 i386_cpu_flags enable; /* cpu feature enable flags */
134 i386_cpu_flags disable; /* cpu feature disable flags */
6305a203
L
135}
136arch_entry;
137
78f12dd3 138static void update_code_flag (int, int);
e3bb37b5
L
139static void set_code_flag (int);
140static void set_16bit_gcc_code_flag (int);
141static void set_intel_syntax (int);
1efbbeb4 142static void set_intel_mnemonic (int);
db51cc60 143static void set_allow_index_reg (int);
7bab8ab5 144static void set_check (int);
e3bb37b5 145static void set_cpu_arch (int);
6482c264 146#ifdef TE_PE
e3bb37b5 147static void pe_directive_secrel (int);
145667f8 148static void pe_directive_secidx (int);
6482c264 149#endif
e3bb37b5
L
150static void signed_cons (int);
151static char *output_invalid (int c);
ee86248c
JB
152static int i386_finalize_immediate (segT, expressionS *, i386_operand_type,
153 const char *);
154static int i386_finalize_displacement (segT, expressionS *, i386_operand_type,
155 const char *);
a7619375 156static int i386_att_operand (char *);
e3bb37b5 157static int i386_intel_operand (char *, int);
ee86248c
JB
158static int i386_intel_simplify (expressionS *);
159static int i386_intel_parse_name (const char *, expressionS *);
e3bb37b5 160static const reg_entry *parse_register (char *, char **);
5317ad2c 161static const char *parse_insn (const char *, char *);
e3bb37b5
L
162static char *parse_operands (char *, const char *);
163static void swap_operands (void);
783c187b 164static void swap_2_operands (unsigned int, unsigned int);
48bcea9f 165static enum flag_code i386_addressing_mode (void);
e3bb37b5
L
166static void optimize_imm (void);
167static void optimize_disp (void);
83b16ac6 168static const insn_template *match_template (char);
e3bb37b5
L
169static int check_string (void);
170static int process_suffix (void);
171static int check_byte_reg (void);
172static int check_long_reg (void);
173static int check_qword_reg (void);
174static int check_word_reg (void);
175static int finalize_imm (void);
176static int process_operands (void);
5e042380 177static const reg_entry *build_modrm_byte (void);
e3bb37b5
L
178static void output_insn (void);
179static void output_imm (fragS *, offsetT);
180static void output_disp (fragS *, offsetT);
29b0f896 181#ifndef I386COFF
e3bb37b5 182static void s_bss (int);
252b5132 183#endif
17d4e2a2
L
184#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
185static void handle_large_common (int small ATTRIBUTE_UNUSED);
b4a3a7b4
L
186
187/* GNU_PROPERTY_X86_ISA_1_USED. */
188static unsigned int x86_isa_1_used;
189/* GNU_PROPERTY_X86_FEATURE_2_USED. */
190static unsigned int x86_feature_2_used;
191/* Generate x86 used ISA and feature properties. */
192static unsigned int x86_used_note = DEFAULT_X86_USED_NOTE;
17d4e2a2 193#endif
252b5132 194
a847613f 195static const char *default_arch = DEFAULT_ARCH;
3e73aa7c 196
8a6fb3f9
JB
197/* parse_register() returns this when a register alias cannot be used. */
198static const reg_entry bad_reg = { "<bad>", OPERAND_TYPE_NONE, 0, 0,
199 { Dw2Inval, Dw2Inval } };
200
34684862 201static const reg_entry *reg_eax;
5e042380
JB
202static const reg_entry *reg_ds;
203static const reg_entry *reg_es;
204static const reg_entry *reg_ss;
6288d05f 205static const reg_entry *reg_st0;
6225c532
JB
206static const reg_entry *reg_k0;
207
c0f3af97
L
208/* VEX prefix. */
209typedef struct
210{
43234a1e
L
211 /* VEX prefix is either 2 byte or 3 byte. EVEX is 4 byte. */
212 unsigned char bytes[4];
c0f3af97
L
213 unsigned int length;
214 /* Destination or source register specifier. */
215 const reg_entry *register_specifier;
216} vex_prefix;
217
252b5132 218/* 'md_assemble ()' gathers together information and puts it into a
47926f60 219 i386_insn. */
252b5132 220
520dc8e8
AM
221union i386_op
222 {
223 expressionS *disps;
224 expressionS *imms;
225 const reg_entry *regs;
226 };
227
a65babc9
L
228enum i386_error
229 {
b4d65f2d 230 no_error, /* Must be first. */
86e026a4 231 operand_size_mismatch,
a65babc9
L
232 operand_type_mismatch,
233 register_type_mismatch,
234 number_of_operands_mismatch,
235 invalid_instruction_suffix,
236 bad_imm4,
a65babc9
L
237 unsupported_with_intel_mnemonic,
238 unsupported_syntax,
6c30d220 239 unsupported,
9db83a32
JB
240 unsupported_on_arch,
241 unsupported_64bit,
260cd341 242 invalid_sib_address,
6c30d220 243 invalid_vsib_address,
7bab8ab5 244 invalid_vector_register_set,
260cd341 245 invalid_tmm_register_set,
0cc78721 246 invalid_dest_and_src_register_set,
43234a1e
L
247 unsupported_vector_index_register,
248 unsupported_broadcast,
43234a1e
L
249 broadcast_needed,
250 unsupported_masking,
251 mask_not_on_destination,
252 no_default_mask,
253 unsupported_rc_sae,
43234a1e 254 invalid_register_operand,
a65babc9
L
255 };
256
252b5132
RH
257struct _i386_insn
258 {
47926f60 259 /* TM holds the template for the insn were currently assembling. */
d3ce72d0 260 insn_template tm;
252b5132 261
7d5e4556
L
262 /* SUFFIX holds the instruction size suffix for byte, word, dword
263 or qword, if given. */
252b5132
RH
264 char suffix;
265
9a182d04
JB
266 /* OPCODE_LENGTH holds the number of base opcode bytes. */
267 unsigned char opcode_length;
268
47926f60 269 /* OPERANDS gives the number of given operands. */
252b5132
RH
270 unsigned int operands;
271
272 /* REG_OPERANDS, DISP_OPERANDS, MEM_OPERANDS, IMM_OPERANDS give the number
273 of given register, displacement, memory operands and immediate
47926f60 274 operands. */
252b5132
RH
275 unsigned int reg_operands, disp_operands, mem_operands, imm_operands;
276
277 /* TYPES [i] is the type (see above #defines) which tells us how to
520dc8e8 278 use OP[i] for the corresponding operand. */
40fb9820 279 i386_operand_type types[MAX_OPERANDS];
252b5132 280
520dc8e8
AM
281 /* Displacement expression, immediate expression, or register for each
282 operand. */
283 union i386_op op[MAX_OPERANDS];
252b5132 284
3e73aa7c
JH
285 /* Flags for operands. */
286 unsigned int flags[MAX_OPERANDS];
287#define Operand_PCrel 1
c48dadc9 288#define Operand_Mem 2
3e73aa7c 289
252b5132 290 /* Relocation type for operand */
f86103b7 291 enum bfd_reloc_code_real reloc[MAX_OPERANDS];
252b5132 292
252b5132
RH
293 /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
294 the base index byte below. */
295 const reg_entry *base_reg;
296 const reg_entry *index_reg;
297 unsigned int log2_scale_factor;
298
299 /* SEG gives the seg_entries of this insn. They are zero unless
47926f60 300 explicit segment overrides are given. */
5e042380 301 const reg_entry *seg[2];
252b5132
RH
302
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
a4d3acd2
JB
314 /* The operand to a branch insn indicates a far branch. */
315 bool far_branch;
316
9373f275
L
317 /* There is a memory operand of (%dx) which should be only used
318 with input/output instructions. */
319 bool input_output_operand;
320
921eafea
L
321 /* Extended states. */
322 enum
323 {
324 /* Use MMX state. */
325 xstate_mmx = 1 << 0,
326 /* Use XMM state. */
327 xstate_xmm = 1 << 1,
328 /* Use YMM state. */
329 xstate_ymm = 1 << 2 | xstate_xmm,
330 /* Use ZMM state. */
331 xstate_zmm = 1 << 3 | xstate_ymm,
332 /* Use TMM state. */
32930e4e
L
333 xstate_tmm = 1 << 4,
334 /* Use MASK state. */
335 xstate_mask = 1 << 5
921eafea 336 } xstate;
260cd341 337
e379e5f3 338 /* Has GOTPC or TLS relocation. */
5b7c81bd 339 bool has_gotpc_tls_reloc;
e379e5f3 340
252b5132 341 /* RM and SIB are the modrm byte and the sib byte where the
c1e679ec 342 addressing modes of this insn are encoded. */
252b5132 343 modrm_byte rm;
3e73aa7c 344 rex_byte rex;
43234a1e 345 rex_byte vrex;
252b5132 346 sib_byte sib;
c0f3af97 347 vex_prefix vex;
b6169b20 348
6225c532
JB
349 /* Masking attributes.
350
351 The struct describes masking, applied to OPERAND in the instruction.
352 REG is a pointer to the corresponding mask register. ZEROING tells
353 whether merging or zeroing mask is used. */
354 struct Mask_Operation
355 {
356 const reg_entry *reg;
357 unsigned int zeroing;
358 /* The operand where this operation is associated. */
359 unsigned int operand;
360 } mask;
43234a1e
L
361
362 /* Rounding control and SAE attributes. */
ca5312a2
JB
363 struct RC_Operation
364 {
365 enum rc_type
366 {
367 rc_none = -1,
368 rne,
369 rd,
370 ru,
371 rz,
372 saeonly
373 } type;
7063667e
JB
374 /* In Intel syntax the operand modifier form is supposed to be used, but
375 we continue to accept the immediate forms as well. */
376 bool modifier;
ca5312a2 377 } rounding;
43234a1e 378
5273a3cd
JB
379 /* Broadcasting attributes.
380
381 The struct describes broadcasting, applied to OPERAND. TYPE is
382 expresses the broadcast factor. */
383 struct Broadcast_Operation
384 {
0cc78721 385 /* Type of broadcast: {1to2}, {1to4}, {1to8}, {1to16} or {1to32}. */
5273a3cd
JB
386 unsigned int type;
387
388 /* Index of broadcasted operand. */
389 unsigned int operand;
390
391 /* Number of bytes to broadcast. */
392 unsigned int bytes;
393 } broadcast;
43234a1e
L
394
395 /* Compressed disp8*N attribute. */
396 unsigned int memshift;
397
86fa6981
L
398 /* Prefer load or store in encoding. */
399 enum
400 {
401 dir_encoding_default = 0,
402 dir_encoding_load,
64c49ab3
JB
403 dir_encoding_store,
404 dir_encoding_swap
86fa6981 405 } dir_encoding;
891edac4 406
41eb8e88 407 /* Prefer 8bit, 16bit, 32bit displacement in encoding. */
a501d77e
L
408 enum
409 {
410 disp_encoding_default = 0,
411 disp_encoding_8bit,
41eb8e88 412 disp_encoding_16bit,
a501d77e
L
413 disp_encoding_32bit
414 } disp_encoding;
f8a5c266 415
6b6b6807 416 /* Prefer the REX byte in encoding. */
5b7c81bd 417 bool rex_encoding;
6b6b6807 418
b6f8c7c4 419 /* Disable instruction size optimization. */
5b7c81bd 420 bool no_optimize;
b6f8c7c4 421
86fa6981
L
422 /* How to encode vector instructions. */
423 enum
424 {
425 vex_encoding_default = 0,
42e04b36 426 vex_encoding_vex,
86fa6981 427 vex_encoding_vex3,
da4977e0
JB
428 vex_encoding_evex,
429 vex_encoding_error
86fa6981
L
430 } vec_encoding;
431
d5de92cf
L
432 /* REP prefix. */
433 const char *rep_prefix;
434
165de32a
L
435 /* HLE prefix. */
436 const char *hle_prefix;
42164a71 437
7e8b059b
L
438 /* Have BND prefix. */
439 const char *bnd_prefix;
440
04ef582a
L
441 /* Have NOTRACK prefix. */
442 const char *notrack_prefix;
443
891edac4 444 /* Error message. */
a65babc9 445 enum i386_error error;
252b5132
RH
446 };
447
448typedef struct _i386_insn i386_insn;
449
43234a1e
L
450/* Link RC type with corresponding string, that'll be looked for in
451 asm. */
452struct RC_name
453{
454 enum rc_type type;
455 const char *name;
456 unsigned int len;
457};
458
459static const struct RC_name RC_NamesTable[] =
460{
461 { rne, STRING_COMMA_LEN ("rn-sae") },
462 { rd, STRING_COMMA_LEN ("rd-sae") },
463 { ru, STRING_COMMA_LEN ("ru-sae") },
464 { rz, STRING_COMMA_LEN ("rz-sae") },
465 { saeonly, STRING_COMMA_LEN ("sae") },
466};
467
3bfea8ba
L
468/* To be indexed by segment register number. */
469static const unsigned char i386_seg_prefixes[] = {
470 ES_PREFIX_OPCODE,
471 CS_PREFIX_OPCODE,
472 SS_PREFIX_OPCODE,
473 DS_PREFIX_OPCODE,
474 FS_PREFIX_OPCODE,
475 GS_PREFIX_OPCODE
476};
477
252b5132
RH
478/* List of chars besides those in app.c:symbol_chars that can start an
479 operand. Used to prevent the scrubber eating vital white-space. */
86fa6981 480const char extra_symbol_chars[] = "*%-([{}"
252b5132 481#ifdef LEX_AT
32137342
NC
482 "@"
483#endif
484#ifdef LEX_QM
485 "?"
252b5132 486#endif
32137342 487 ;
252b5132 488
b3983e5f
JB
489#if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
490 && !defined (TE_GNU) \
491 && !defined (TE_LINUX) \
d85e70a3 492 && !defined (TE_Haiku) \
b3983e5f
JB
493 && !defined (TE_FreeBSD) \
494 && !defined (TE_DragonFly) \
495 && !defined (TE_NetBSD))
252b5132 496/* This array holds the chars that always start a comment. If the
b3b91714
AM
497 pre-processor is disabled, these aren't very useful. The option
498 --divide will remove '/' from this list. */
499const char *i386_comment_chars = "#/";
500#define SVR4_COMMENT_CHARS 1
252b5132 501#define PREFIX_SEPARATOR '\\'
252b5132 502
b3b91714
AM
503#else
504const char *i386_comment_chars = "#";
505#define PREFIX_SEPARATOR '/'
506#endif
507
252b5132
RH
508/* This array holds the chars that only start a comment at the beginning of
509 a line. If the line seems to have the form '# 123 filename'
ce8a8b2f
AM
510 .line and .file directives will appear in the pre-processed output.
511 Note that input_file.c hand checks for '#' at the beginning of the
252b5132 512 first line of the input file. This is because the compiler outputs
ce8a8b2f
AM
513 #NO_APP at the beginning of its output.
514 Also note that comments started like this one will always work if
252b5132 515 '/' isn't otherwise defined. */
b3b91714 516const char line_comment_chars[] = "#/";
252b5132 517
63a0b638 518const char line_separator_chars[] = ";";
252b5132 519
ce8a8b2f
AM
520/* Chars that can be used to separate mant from exp in floating point
521 nums. */
252b5132
RH
522const char EXP_CHARS[] = "eE";
523
ce8a8b2f
AM
524/* Chars that mean this number is a floating point constant
525 As in 0f12.456
526 or 0d1.2345e12. */
de133cf9 527const char FLT_CHARS[] = "fFdDxXhHbB";
252b5132 528
ce8a8b2f 529/* Tables for lexical analysis. */
252b5132
RH
530static char mnemonic_chars[256];
531static char register_chars[256];
532static char operand_chars[256];
533static char identifier_chars[256];
252b5132 534
ce8a8b2f 535/* Lexical macros. */
252b5132
RH
536#define is_mnemonic_char(x) (mnemonic_chars[(unsigned char) x])
537#define is_operand_char(x) (operand_chars[(unsigned char) x])
538#define is_register_char(x) (register_chars[(unsigned char) x])
539#define is_space_char(x) ((x) == ' ')
540#define is_identifier_char(x) (identifier_chars[(unsigned char) x])
252b5132 541
0234cb7c 542/* All non-digit non-letter characters that may occur in an operand. */
252b5132
RH
543static char operand_special_chars[] = "%$-+(,)*._~/<>|&^!:[@]";
544
545/* md_assemble() always leaves the strings it's passed unaltered. To
546 effect this we maintain a stack of saved characters that we've smashed
547 with '\0's (indicating end of strings for various sub-fields of the
47926f60 548 assembler instruction). */
252b5132 549static char save_stack[32];
ce8a8b2f 550static char *save_stack_p;
252b5132
RH
551#define END_STRING_AND_SAVE(s) \
552 do { *save_stack_p++ = *(s); *(s) = '\0'; } while (0)
553#define RESTORE_END_STRING(s) \
554 do { *(s) = *--save_stack_p; } while (0)
555
47926f60 556/* The instruction we're assembling. */
252b5132
RH
557static i386_insn i;
558
559/* Possible templates for current insn. */
560static const templates *current_templates;
561
31b2323c
L
562/* Per instruction expressionS buffers: max displacements & immediates. */
563static expressionS disp_expressions[MAX_MEMORY_OPERANDS];
564static expressionS im_expressions[MAX_IMMEDIATE_OPERANDS];
252b5132 565
47926f60 566/* Current operand we are working on. */
ee86248c 567static int this_operand = -1;
252b5132 568
3e73aa7c
JH
569/* We support four different modes. FLAG_CODE variable is used to distinguish
570 these. */
571
572enum flag_code {
573 CODE_32BIT,
574 CODE_16BIT,
575 CODE_64BIT };
576
577static enum flag_code flag_code;
4fa24527 578static unsigned int object_64bit;
862be3fb 579static unsigned int disallow_64bit_reloc;
3e73aa7c 580static int use_rela_relocations = 0;
e379e5f3
L
581/* __tls_get_addr/___tls_get_addr symbol for TLS. */
582static const char *tls_get_addr;
3e73aa7c 583
7af8ed2d
NC
584#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
585 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
586 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
587
351f65ca
L
588/* The ELF ABI to use. */
589enum x86_elf_abi
590{
591 I386_ABI,
7f56bc95
L
592 X86_64_ABI,
593 X86_64_X32_ABI
351f65ca
L
594};
595
596static enum x86_elf_abi x86_elf_abi = I386_ABI;
7af8ed2d 597#endif
351f65ca 598
167ad85b
TG
599#if defined (TE_PE) || defined (TE_PEP)
600/* Use big object file format. */
601static int use_big_obj = 0;
602#endif
603
8dcea932
L
604#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
605/* 1 if generating code for a shared library. */
606static int shared = 0;
b52c4ee4
IB
607
608unsigned int x86_sframe_cfa_sp_reg;
609/* The other CFA base register for SFrame unwind info. */
610unsigned int x86_sframe_cfa_fp_reg;
611unsigned int x86_sframe_cfa_ra_reg;
612
8dcea932
L
613#endif
614
47926f60
KH
615/* 1 for intel syntax,
616 0 if att syntax. */
617static int intel_syntax = 0;
252b5132 618
4b5aaf5f
L
619static enum x86_64_isa
620{
621 amd64 = 1, /* AMD64 ISA. */
622 intel64 /* Intel64 ISA. */
623} isa64;
e89c5eaa 624
1efbbeb4
L
625/* 1 for intel mnemonic,
626 0 if att mnemonic. */
627static int intel_mnemonic = !SYSV386_COMPAT;
628
a60de03c
JB
629/* 1 if pseudo registers are permitted. */
630static int allow_pseudo_reg = 0;
631
47926f60
KH
632/* 1 if register prefix % not required. */
633static int allow_naked_reg = 0;
252b5132 634
33eaf5de 635/* 1 if the assembler should add BND prefix for all control-transferring
7e8b059b
L
636 instructions supporting it, even if this prefix wasn't specified
637 explicitly. */
638static int add_bnd_prefix = 0;
639
ba104c83 640/* 1 if pseudo index register, eiz/riz, is allowed . */
db51cc60
L
641static int allow_index_reg = 0;
642
d022bddd
IT
643/* 1 if the assembler should ignore LOCK prefix, even if it was
644 specified explicitly. */
645static int omit_lock_prefix = 0;
646
e4e00185
AS
647/* 1 if the assembler should encode lfence, mfence, and sfence as
648 "lock addl $0, (%{re}sp)". */
649static int avoid_fence = 0;
650
ae531041
L
651/* 1 if lfence should be inserted after every load. */
652static int lfence_after_load = 0;
653
654/* Non-zero if lfence should be inserted before indirect branch. */
655static enum lfence_before_indirect_branch_kind
656 {
657 lfence_branch_none = 0,
658 lfence_branch_register,
659 lfence_branch_memory,
660 lfence_branch_all
661 }
662lfence_before_indirect_branch;
663
664/* Non-zero if lfence should be inserted before ret. */
665static enum lfence_before_ret_kind
666 {
667 lfence_before_ret_none = 0,
668 lfence_before_ret_not,
a09f656b 669 lfence_before_ret_or,
670 lfence_before_ret_shl
ae531041
L
671 }
672lfence_before_ret;
673
674/* Types of previous instruction is .byte or prefix. */
e379e5f3
L
675static struct
676 {
677 segT seg;
678 const char *file;
679 const char *name;
680 unsigned int line;
681 enum last_insn_kind
682 {
683 last_insn_other = 0,
684 last_insn_directive,
685 last_insn_prefix
686 } kind;
687 } last_insn;
688
0cb4071e
L
689/* 1 if the assembler should generate relax relocations. */
690
691static int generate_relax_relocations
692 = DEFAULT_GENERATE_X86_RELAX_RELOCATIONS;
693
7bab8ab5 694static enum check_kind
daf50ae7 695 {
7bab8ab5
JB
696 check_none = 0,
697 check_warning,
698 check_error
daf50ae7 699 }
7bab8ab5 700sse_check, operand_check = check_warning;
daf50ae7 701
e379e5f3
L
702/* Non-zero if branches should be aligned within power of 2 boundary. */
703static int align_branch_power = 0;
704
705/* Types of branches to align. */
706enum align_branch_kind
707 {
708 align_branch_none = 0,
709 align_branch_jcc = 1,
710 align_branch_fused = 2,
711 align_branch_jmp = 3,
712 align_branch_call = 4,
713 align_branch_indirect = 5,
714 align_branch_ret = 6
715 };
716
717/* Type bits of branches to align. */
718enum align_branch_bit
719 {
720 align_branch_jcc_bit = 1 << align_branch_jcc,
721 align_branch_fused_bit = 1 << align_branch_fused,
722 align_branch_jmp_bit = 1 << align_branch_jmp,
723 align_branch_call_bit = 1 << align_branch_call,
724 align_branch_indirect_bit = 1 << align_branch_indirect,
725 align_branch_ret_bit = 1 << align_branch_ret
726 };
727
728static unsigned int align_branch = (align_branch_jcc_bit
729 | align_branch_fused_bit
730 | align_branch_jmp_bit);
731
79d72f45
HL
732/* Types of condition jump used by macro-fusion. */
733enum mf_jcc_kind
734 {
735 mf_jcc_jo = 0, /* base opcode 0x70 */
736 mf_jcc_jc, /* base opcode 0x72 */
737 mf_jcc_je, /* base opcode 0x74 */
738 mf_jcc_jna, /* base opcode 0x76 */
739 mf_jcc_js, /* base opcode 0x78 */
740 mf_jcc_jp, /* base opcode 0x7a */
741 mf_jcc_jl, /* base opcode 0x7c */
742 mf_jcc_jle, /* base opcode 0x7e */
743 };
744
745/* Types of compare flag-modifying insntructions used by macro-fusion. */
746enum mf_cmp_kind
747 {
748 mf_cmp_test_and, /* test/cmp */
749 mf_cmp_alu_cmp, /* add/sub/cmp */
750 mf_cmp_incdec /* inc/dec */
751 };
752
e379e5f3
L
753/* The maximum padding size for fused jcc. CMP like instruction can
754 be 9 bytes and jcc can be 6 bytes. Leave room just in case for
755 prefixes. */
756#define MAX_FUSED_JCC_PADDING_SIZE 20
757
758/* The maximum number of prefixes added for an instruction. */
759static unsigned int align_branch_prefix_size = 5;
760
b6f8c7c4
L
761/* Optimization:
762 1. Clear the REX_W bit with register operand if possible.
763 2. Above plus use 128bit vector instruction to clear the full vector
764 register.
765 */
766static int optimize = 0;
767
768/* Optimization:
769 1. Clear the REX_W bit with register operand if possible.
770 2. Above plus use 128bit vector instruction to clear the full vector
771 register.
772 3. Above plus optimize "test{q,l,w} $imm8,%r{64,32,16}" to
773 "testb $imm7,%r8".
774 */
775static int optimize_for_space = 0;
776
2ca3ace5
L
777/* Register prefix used for error message. */
778static const char *register_prefix = "%";
779
47926f60
KH
780/* Used in 16 bit gcc mode to add an l suffix to call, ret, enter,
781 leave, push, and pop instructions so that gcc has the same stack
782 frame as in 32 bit mode. */
783static char stackop_size = '\0';
eecb386c 784
12b55ccc
L
785/* Non-zero to optimize code alignment. */
786int optimize_align_code = 1;
787
47926f60
KH
788/* Non-zero to quieten some warnings. */
789static int quiet_warnings = 0;
a38cf1db 790
d59a54c2
JB
791/* Guard to avoid repeated warnings about non-16-bit code on 16-bit CPUs. */
792static bool pre_386_16bit_warned;
793
47926f60
KH
794/* CPU name. */
795static const char *cpu_arch_name = NULL;
6305a203 796static char *cpu_sub_arch_name = NULL;
a38cf1db 797
47926f60 798/* CPU feature flags. */
40fb9820
L
799static i386_cpu_flags cpu_arch_flags = CPU_UNKNOWN_FLAGS;
800
ccc9c027
L
801/* If we have selected a cpu we are generating instructions for. */
802static int cpu_arch_tune_set = 0;
803
9103f4f4 804/* Cpu we are generating instructions for. */
fbf3f584 805enum processor_type cpu_arch_tune = PROCESSOR_UNKNOWN;
9103f4f4
L
806
807/* CPU feature flags of cpu we are generating instructions for. */
40fb9820 808static i386_cpu_flags cpu_arch_tune_flags;
9103f4f4 809
ccc9c027 810/* CPU instruction set architecture used. */
fbf3f584 811enum processor_type cpu_arch_isa = PROCESSOR_UNKNOWN;
ccc9c027 812
9103f4f4 813/* CPU feature flags of instruction set architecture used. */
fbf3f584 814i386_cpu_flags cpu_arch_isa_flags;
9103f4f4 815
fddf5b5b
AM
816/* If set, conditional jumps are not automatically promoted to handle
817 larger than a byte offset. */
f68697e8 818static bool no_cond_jump_promotion = false;
fddf5b5b 819
c0f3af97
L
820/* Encode SSE instructions with VEX prefix. */
821static unsigned int sse2avx;
822
c8480b58
L
823/* Encode aligned vector move as unaligned vector move. */
824static unsigned int use_unaligned_vector_move;
825
539f890d
L
826/* Encode scalar AVX instructions with specific vector length. */
827static enum
828 {
829 vex128 = 0,
830 vex256
831 } avxscalar;
832
03751133
L
833/* Encode VEX WIG instructions with specific vex.w. */
834static enum
835 {
836 vexw0 = 0,
837 vexw1
838 } vexwig;
839
43234a1e
L
840/* Encode scalar EVEX LIG instructions with specific vector length. */
841static enum
842 {
843 evexl128 = 0,
844 evexl256,
845 evexl512
846 } evexlig;
847
848/* Encode EVEX WIG instructions with specific evex.w. */
849static enum
850 {
851 evexw0 = 0,
852 evexw1
853 } evexwig;
854
d3d3c6db
IT
855/* Value to encode in EVEX RC bits, for SAE-only instructions. */
856static enum rc_type evexrcig = rne;
857
29b0f896 858/* Pre-defined "_GLOBAL_OFFSET_TABLE_". */
87c245cc 859static symbolS *GOT_symbol;
29b0f896 860
a4447b93
RH
861/* The dwarf2 return column, adjusted for 32 or 64 bit. */
862unsigned int x86_dwarf2_return_column;
863
864/* The dwarf2 data alignment, adjusted for 32 or 64 bit. */
865int x86_cie_data_alignment;
866
252b5132 867/* Interface to relax_segment.
fddf5b5b
AM
868 There are 3 major relax states for 386 jump insns because the
869 different types of jumps add different sizes to frags when we're
e379e5f3
L
870 figuring out what sort of jump to choose to reach a given label.
871
872 BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING are used to align
873 branches which are handled by md_estimate_size_before_relax() and
874 i386_generic_table_relax_frag(). */
252b5132 875
47926f60 876/* Types. */
93c2a809
AM
877#define UNCOND_JUMP 0
878#define COND_JUMP 1
879#define COND_JUMP86 2
e379e5f3
L
880#define BRANCH_PADDING 3
881#define BRANCH_PREFIX 4
882#define FUSED_JCC_PADDING 5
fddf5b5b 883
47926f60 884/* Sizes. */
252b5132
RH
885#define CODE16 1
886#define SMALL 0
29b0f896 887#define SMALL16 (SMALL | CODE16)
252b5132 888#define BIG 2
29b0f896 889#define BIG16 (BIG | CODE16)
252b5132
RH
890
891#ifndef INLINE
892#ifdef __GNUC__
893#define INLINE __inline__
894#else
895#define INLINE
896#endif
897#endif
898
fddf5b5b
AM
899#define ENCODE_RELAX_STATE(type, size) \
900 ((relax_substateT) (((type) << 2) | (size)))
901#define TYPE_FROM_RELAX_STATE(s) \
902 ((s) >> 2)
903#define DISP_SIZE_FROM_RELAX_STATE(s) \
904 ((((s) & 3) == BIG ? 4 : (((s) & 3) == BIG16 ? 2 : 1)))
252b5132
RH
905
906/* This table is used by relax_frag to promote short jumps to long
907 ones where necessary. SMALL (short) jumps may be promoted to BIG
908 (32 bit long) ones, and SMALL16 jumps to BIG16 (16 bit long). We
909 don't allow a short jump in a 32 bit code segment to be promoted to
910 a 16 bit offset jump because it's slower (requires data size
911 prefix), and doesn't work, unless the destination is in the bottom
912 64k of the code segment (The top 16 bits of eip are zeroed). */
913
914const relax_typeS md_relax_table[] =
915{
24eab124
AM
916 /* The fields are:
917 1) most positive reach of this state,
918 2) most negative reach of this state,
93c2a809 919 3) how many bytes this mode will have in the variable part of the frag
ce8a8b2f 920 4) which index into the table to try if we can't fit into this one. */
252b5132 921
fddf5b5b 922 /* UNCOND_JUMP states. */
93c2a809
AM
923 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG)},
924 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16)},
925 /* dword jmp adds 4 bytes to frag:
926 0 extra opcode bytes, 4 displacement bytes. */
252b5132 927 {0, 0, 4, 0},
93c2a809
AM
928 /* word jmp adds 2 byte2 to frag:
929 0 extra opcode bytes, 2 displacement bytes. */
252b5132
RH
930 {0, 0, 2, 0},
931
93c2a809
AM
932 /* COND_JUMP states. */
933 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG)},
934 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG16)},
935 /* dword conditionals adds 5 bytes to frag:
936 1 extra opcode byte, 4 displacement bytes. */
937 {0, 0, 5, 0},
fddf5b5b 938 /* word conditionals add 3 bytes to frag:
93c2a809
AM
939 1 extra opcode byte, 2 displacement bytes. */
940 {0, 0, 3, 0},
941
942 /* COND_JUMP86 states. */
943 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG)},
944 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG16)},
945 /* dword conditionals adds 5 bytes to frag:
946 1 extra opcode byte, 4 displacement bytes. */
947 {0, 0, 5, 0},
948 /* word conditionals add 4 bytes to frag:
949 1 displacement byte and a 3 byte long branch insn. */
950 {0, 0, 4, 0}
252b5132
RH
951};
952
6ceeed25 953#define ARCH(n, t, f, s) \
ae89daec
JB
954 { STRING_COMMA_LEN (#n), s, PROCESSOR_ ## t, CPU_ ## f ## _FLAGS, \
955 CPU_NONE_FLAGS }
956#define SUBARCH(n, e, d, s) \
957 { STRING_COMMA_LEN (#n), s, PROCESSOR_NONE, CPU_ ## e ## _FLAGS, \
958 CPU_ ## d ## _FLAGS }
6ceeed25 959
9103f4f4
L
960static const arch_entry cpu_arch[] =
961{
3ce2ebcf
JB
962 /* Do not replace the first two entries - i386_target_format() and
963 set_cpu_arch() rely on them being there in this order. */
6ceeed25
JB
964 ARCH (generic32, GENERIC32, GENERIC32, false),
965 ARCH (generic64, GENERIC64, GENERIC64, false),
966 ARCH (i8086, UNKNOWN, NONE, false),
967 ARCH (i186, UNKNOWN, I186, false),
968 ARCH (i286, UNKNOWN, I286, false),
969 ARCH (i386, I386, I386, false),
970 ARCH (i486, I486, I486, false),
971 ARCH (i586, PENTIUM, I586, false),
972 ARCH (i686, PENTIUMPRO, I686, false),
973 ARCH (pentium, PENTIUM, I586, false),
974 ARCH (pentiumpro, PENTIUMPRO, PENTIUMPRO, false),
975 ARCH (pentiumii, PENTIUMPRO, P2, false),
976 ARCH (pentiumiii, PENTIUMPRO, P3, false),
977 ARCH (pentium4, PENTIUM4, P4, false),
978 ARCH (prescott, NOCONA, CORE, false),
979 ARCH (nocona, NOCONA, NOCONA, false),
980 ARCH (yonah, CORE, CORE, true),
981 ARCH (core, CORE, CORE, false),
982 ARCH (merom, CORE2, CORE2, true),
983 ARCH (core2, CORE2, CORE2, false),
984 ARCH (corei7, COREI7, COREI7, false),
985 ARCH (iamcu, IAMCU, IAMCU, false),
986 ARCH (k6, K6, K6, false),
987 ARCH (k6_2, K6, K6_2, false),
988 ARCH (athlon, ATHLON, ATHLON, false),
989 ARCH (sledgehammer, K8, K8, true),
990 ARCH (opteron, K8, K8, false),
991 ARCH (k8, K8, K8, false),
992 ARCH (amdfam10, AMDFAM10, AMDFAM10, false),
993 ARCH (bdver1, BD, BDVER1, false),
994 ARCH (bdver2, BD, BDVER2, false),
995 ARCH (bdver3, BD, BDVER3, false),
996 ARCH (bdver4, BD, BDVER4, false),
997 ARCH (znver1, ZNVER, ZNVER1, false),
998 ARCH (znver2, ZNVER, ZNVER2, false),
999 ARCH (znver3, ZNVER, ZNVER3, false),
b0e8fa7f 1000 ARCH (znver4, ZNVER, ZNVER4, false),
6ceeed25
JB
1001 ARCH (btver1, BT, BTVER1, false),
1002 ARCH (btver2, BT, BTVER2, false),
1003
ae89daec
JB
1004 SUBARCH (8087, 8087, ANY_X87, false),
1005 SUBARCH (87, NONE, ANY_X87, false), /* Disable only! */
1006 SUBARCH (287, 287, ANY_287, false),
1007 SUBARCH (387, 387, ANY_387, false),
1008 SUBARCH (687, 687, ANY_687, false),
1009 SUBARCH (cmov, CMOV, ANY_CMOV, false),
1010 SUBARCH (fxsr, FXSR, ANY_FXSR, false),
1011 SUBARCH (mmx, MMX, ANY_MMX, false),
1012 SUBARCH (sse, SSE, ANY_SSE, false),
1013 SUBARCH (sse2, SSE2, ANY_SSE2, false),
1014 SUBARCH (sse3, SSE3, ANY_SSE3, false),
1015 SUBARCH (sse4a, SSE4A, ANY_SSE4A, false),
1016 SUBARCH (ssse3, SSSE3, ANY_SSSE3, false),
1017 SUBARCH (sse4.1, SSE4_1, ANY_SSE4_1, false),
1018 SUBARCH (sse4.2, SSE4_2, ANY_SSE4_2, false),
1019 SUBARCH (sse4, SSE4_2, ANY_SSE4_1, false),
1020 SUBARCH (avx, AVX, ANY_AVX, false),
1021 SUBARCH (avx2, AVX2, ANY_AVX2, false),
1022 SUBARCH (avx512f, AVX512F, ANY_AVX512F, false),
1023 SUBARCH (avx512cd, AVX512CD, ANY_AVX512CD, false),
1024 SUBARCH (avx512er, AVX512ER, ANY_AVX512ER, false),
1025 SUBARCH (avx512pf, AVX512PF, ANY_AVX512PF, false),
1026 SUBARCH (avx512dq, AVX512DQ, ANY_AVX512DQ, false),
1027 SUBARCH (avx512bw, AVX512BW, ANY_AVX512BW, false),
1028 SUBARCH (avx512vl, AVX512VL, ANY_AVX512VL, false),
1029 SUBARCH (vmx, VMX, VMX, false),
1030 SUBARCH (vmfunc, VMFUNC, VMFUNC, false),
1031 SUBARCH (smx, SMX, SMX, false),
1032 SUBARCH (xsave, XSAVE, XSAVE, false),
1033 SUBARCH (xsaveopt, XSAVEOPT, XSAVEOPT, false),
1034 SUBARCH (xsavec, XSAVEC, XSAVEC, false),
1035 SUBARCH (xsaves, XSAVES, XSAVES, false),
1036 SUBARCH (aes, AES, AES, false),
1037 SUBARCH (pclmul, PCLMUL, PCLMUL, false),
1038 SUBARCH (clmul, PCLMUL, PCLMUL, true),
1039 SUBARCH (fsgsbase, FSGSBASE, FSGSBASE, false),
1040 SUBARCH (rdrnd, RDRND, RDRND, false),
1041 SUBARCH (f16c, F16C, F16C, false),
1042 SUBARCH (bmi2, BMI2, BMI2, false),
1043 SUBARCH (fma, FMA, FMA, false),
1044 SUBARCH (fma4, FMA4, FMA4, false),
1045 SUBARCH (xop, XOP, XOP, false),
1046 SUBARCH (lwp, LWP, LWP, false),
1047 SUBARCH (movbe, MOVBE, MOVBE, false),
1048 SUBARCH (cx16, CX16, CX16, false),
1049 SUBARCH (ept, EPT, EPT, false),
1050 SUBARCH (lzcnt, LZCNT, LZCNT, false),
1051 SUBARCH (popcnt, POPCNT, POPCNT, false),
1052 SUBARCH (hle, HLE, HLE, false),
1053 SUBARCH (rtm, RTM, RTM, false),
1054 SUBARCH (invpcid, INVPCID, INVPCID, false),
1055 SUBARCH (clflush, CLFLUSH, CLFLUSH, false),
1056 SUBARCH (nop, NOP, NOP, false),
1057 SUBARCH (syscall, SYSCALL, SYSCALL, false),
1058 SUBARCH (rdtscp, RDTSCP, RDTSCP, false),
1059 SUBARCH (3dnow, 3DNOW, 3DNOW, false),
1060 SUBARCH (3dnowa, 3DNOWA, 3DNOWA, false),
1061 SUBARCH (padlock, PADLOCK, PADLOCK, false),
1062 SUBARCH (pacifica, SVME, SVME, true),
1063 SUBARCH (svme, SVME, SVME, false),
ae89daec
JB
1064 SUBARCH (abm, ABM, ABM, false),
1065 SUBARCH (bmi, BMI, BMI, false),
1066 SUBARCH (tbm, TBM, TBM, false),
1067 SUBARCH (adx, ADX, ADX, false),
1068 SUBARCH (rdseed, RDSEED, RDSEED, false),
1069 SUBARCH (prfchw, PRFCHW, PRFCHW, false),
1070 SUBARCH (smap, SMAP, SMAP, false),
1071 SUBARCH (mpx, MPX, MPX, false),
1072 SUBARCH (sha, SHA, SHA, false),
1073 SUBARCH (clflushopt, CLFLUSHOPT, CLFLUSHOPT, false),
1074 SUBARCH (prefetchwt1, PREFETCHWT1, PREFETCHWT1, false),
1075 SUBARCH (se1, SE1, SE1, false),
1076 SUBARCH (clwb, CLWB, CLWB, false),
1077 SUBARCH (avx512ifma, AVX512IFMA, ANY_AVX512IFMA, false),
1078 SUBARCH (avx512vbmi, AVX512VBMI, ANY_AVX512VBMI, false),
1079 SUBARCH (avx512_4fmaps, AVX512_4FMAPS, ANY_AVX512_4FMAPS, false),
1080 SUBARCH (avx512_4vnniw, AVX512_4VNNIW, ANY_AVX512_4VNNIW, false),
1081 SUBARCH (avx512_vpopcntdq, AVX512_VPOPCNTDQ, ANY_AVX512_VPOPCNTDQ, false),
1082 SUBARCH (avx512_vbmi2, AVX512_VBMI2, ANY_AVX512_VBMI2, false),
1083 SUBARCH (avx512_vnni, AVX512_VNNI, ANY_AVX512_VNNI, false),
1084 SUBARCH (avx512_bitalg, AVX512_BITALG, ANY_AVX512_BITALG, false),
1085 SUBARCH (avx_vnni, AVX_VNNI, ANY_AVX_VNNI, false),
1086 SUBARCH (clzero, CLZERO, CLZERO, false),
1087 SUBARCH (mwaitx, MWAITX, MWAITX, false),
1088 SUBARCH (ospke, OSPKE, OSPKE, false),
1089 SUBARCH (rdpid, RDPID, RDPID, false),
1090 SUBARCH (ptwrite, PTWRITE, PTWRITE, false),
1091 SUBARCH (ibt, IBT, ANY_IBT, false),
1092 SUBARCH (shstk, SHSTK, ANY_SHSTK, false),
1093 SUBARCH (gfni, GFNI, GFNI, false),
1094 SUBARCH (vaes, VAES, VAES, false),
1095 SUBARCH (vpclmulqdq, VPCLMULQDQ, VPCLMULQDQ, false),
1096 SUBARCH (wbnoinvd, WBNOINVD, WBNOINVD, false),
1097 SUBARCH (pconfig, PCONFIG, PCONFIG, false),
1098 SUBARCH (waitpkg, WAITPKG, WAITPKG, false),
1099 SUBARCH (cldemote, CLDEMOTE, CLDEMOTE, false),
1100 SUBARCH (amx_int8, AMX_INT8, ANY_AMX_INT8, false),
1101 SUBARCH (amx_bf16, AMX_BF16, ANY_AMX_BF16, false),
68830fba 1102 SUBARCH (amx_fp16, AMX_FP16, AMX_FP16, false),
ae89daec
JB
1103 SUBARCH (amx_tile, AMX_TILE, ANY_AMX_TILE, false),
1104 SUBARCH (movdiri, MOVDIRI, ANY_MOVDIRI, false),
1105 SUBARCH (movdir64b, MOVDIR64B, ANY_MOVDIR64B, false),
1106 SUBARCH (avx512_bf16, AVX512_BF16, ANY_AVX512_BF16, false),
1107 SUBARCH (avx512_vp2intersect, AVX512_VP2INTERSECT,
1108 ANY_AVX512_VP2INTERSECT, false),
1109 SUBARCH (tdx, TDX, ANY_TDX, false),
1110 SUBARCH (enqcmd, ENQCMD, ANY_ENQCMD, false),
1111 SUBARCH (serialize, SERIALIZE, ANY_SERIALIZE, false),
1112 SUBARCH (rdpru, RDPRU, RDPRU, false),
1113 SUBARCH (mcommit, MCOMMIT, MCOMMIT, false),
1114 SUBARCH (sev_es, SEV_ES, SEV_ES, false),
1115 SUBARCH (tsxldtrk, TSXLDTRK, ANY_TSXLDTRK, false),
1116 SUBARCH (kl, KL, ANY_KL, false),
1117 SUBARCH (widekl, WIDEKL, ANY_WIDEKL, false),
1118 SUBARCH (uintr, UINTR, ANY_UINTR, false),
1119 SUBARCH (hreset, HRESET, ANY_HRESET, false),
1120 SUBARCH (avx512_fp16, AVX512_FP16, ANY_AVX512_FP16, false),
ef07be45 1121 SUBARCH (prefetchi, PREFETCHI, PREFETCHI, false),
4321af3e 1122 SUBARCH (avx_ifma, AVX_IFMA, ANY_AVX_IFMA, false),
23ae61ad 1123 SUBARCH (avx_vnni_int8, AVX_VNNI_INT8, ANY_AVX_VNNI_INT8, false),
a93e3234 1124 SUBARCH (cmpccxadd, CMPCCXADD, ANY_CMPCCXADD, false),
941f0833 1125 SUBARCH (wrmsrns, WRMSRNS, ANY_WRMSRNS, false),
2188d6ea 1126 SUBARCH (msrlist, MSRLIST, ANY_MSRLIST, false),
01d8ce74 1127 SUBARCH (avx_ne_convert, AVX_NE_CONVERT, ANY_AVX_NE_CONVERT, false),
b06311ad 1128 SUBARCH (rao_int, RAO_INT, ANY_RAO_INT, false),
b0e8fa7f 1129 SUBARCH (rmpquery, RMPQUERY, RMPQUERY, false),
293f5f65
L
1130};
1131
6ceeed25
JB
1132#undef SUBARCH
1133#undef ARCH
1134
704209c0 1135#ifdef I386COFF
a6c24e68
NC
1136/* Like s_lcomm_internal in gas/read.c but the alignment string
1137 is allowed to be optional. */
1138
1139static symbolS *
1140pe_lcomm_internal (int needs_align, symbolS *symbolP, addressT size)
1141{
1142 addressT align = 0;
1143
1144 SKIP_WHITESPACE ();
1145
7ab9ffdd 1146 if (needs_align
a6c24e68
NC
1147 && *input_line_pointer == ',')
1148 {
1149 align = parse_align (needs_align - 1);
7ab9ffdd 1150
a6c24e68
NC
1151 if (align == (addressT) -1)
1152 return NULL;
1153 }
1154 else
1155 {
1156 if (size >= 8)
1157 align = 3;
1158 else if (size >= 4)
1159 align = 2;
1160 else if (size >= 2)
1161 align = 1;
1162 else
1163 align = 0;
1164 }
1165
1166 bss_alloc (symbolP, size, align);
1167 return symbolP;
1168}
1169
704209c0 1170static void
a6c24e68
NC
1171pe_lcomm (int needs_align)
1172{
1173 s_comm_internal (needs_align * 2, pe_lcomm_internal);
1174}
704209c0 1175#endif
a6c24e68 1176
29b0f896
AM
1177const pseudo_typeS md_pseudo_table[] =
1178{
1179#if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
1180 {"align", s_align_bytes, 0},
1181#else
1182 {"align", s_align_ptwo, 0},
1183#endif
1184 {"arch", set_cpu_arch, 0},
1185#ifndef I386COFF
1186 {"bss", s_bss, 0},
a6c24e68
NC
1187#else
1188 {"lcomm", pe_lcomm, 1},
29b0f896
AM
1189#endif
1190 {"ffloat", float_cons, 'f'},
1191 {"dfloat", float_cons, 'd'},
1192 {"tfloat", float_cons, 'x'},
7d19d096 1193 {"hfloat", float_cons, 'h'},
de133cf9 1194 {"bfloat16", float_cons, 'b'},
29b0f896 1195 {"value", cons, 2},
d182319b 1196 {"slong", signed_cons, 4},
29b0f896
AM
1197 {"noopt", s_ignore, 0},
1198 {"optim", s_ignore, 0},
1199 {"code16gcc", set_16bit_gcc_code_flag, CODE_16BIT},
1200 {"code16", set_code_flag, CODE_16BIT},
1201 {"code32", set_code_flag, CODE_32BIT},
da5f19a2 1202#ifdef BFD64
29b0f896 1203 {"code64", set_code_flag, CODE_64BIT},
da5f19a2 1204#endif
29b0f896
AM
1205 {"intel_syntax", set_intel_syntax, 1},
1206 {"att_syntax", set_intel_syntax, 0},
1efbbeb4
L
1207 {"intel_mnemonic", set_intel_mnemonic, 1},
1208 {"att_mnemonic", set_intel_mnemonic, 0},
db51cc60
L
1209 {"allow_index_reg", set_allow_index_reg, 1},
1210 {"disallow_index_reg", set_allow_index_reg, 0},
7bab8ab5
JB
1211 {"sse_check", set_check, 0},
1212 {"operand_check", set_check, 1},
3b22753a
L
1213#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1214 {"largecomm", handle_large_common, 0},
07a53e5c 1215#else
68d20676 1216 {"file", dwarf2_directive_file, 0},
07a53e5c
RH
1217 {"loc", dwarf2_directive_loc, 0},
1218 {"loc_mark_labels", dwarf2_directive_loc_mark_labels, 0},
3b22753a 1219#endif
6482c264
NC
1220#ifdef TE_PE
1221 {"secrel32", pe_directive_secrel, 0},
145667f8 1222 {"secidx", pe_directive_secidx, 0},
6482c264 1223#endif
29b0f896
AM
1224 {0, 0, 0}
1225};
1226
1227/* For interface with expression (). */
1228extern char *input_line_pointer;
1229
1230/* Hash table for instruction mnemonic lookup. */
629310ab 1231static htab_t op_hash;
29b0f896
AM
1232
1233/* Hash table for register lookup. */
629310ab 1234static htab_t reg_hash;
29b0f896 1235\f
ce8a8b2f
AM
1236 /* Various efficient no-op patterns for aligning code labels.
1237 Note: Don't try to assemble the instructions in the comments.
1238 0L and 0w are not legal. */
62a02d25
L
1239static const unsigned char f32_1[] =
1240 {0x90}; /* nop */
1241static const unsigned char f32_2[] =
1242 {0x66,0x90}; /* xchg %ax,%ax */
1243static const unsigned char f32_3[] =
1244 {0x8d,0x76,0x00}; /* leal 0(%esi),%esi */
1245static const unsigned char f32_4[] =
1246 {0x8d,0x74,0x26,0x00}; /* leal 0(%esi,1),%esi */
62a02d25
L
1247static const unsigned char f32_6[] =
1248 {0x8d,0xb6,0x00,0x00,0x00,0x00}; /* leal 0L(%esi),%esi */
1249static const unsigned char f32_7[] =
1250 {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal 0L(%esi,1),%esi */
62a02d25 1251static const unsigned char f16_3[] =
3ae729d5 1252 {0x8d,0x74,0x00}; /* lea 0(%si),%si */
62a02d25 1253static const unsigned char f16_4[] =
3ae729d5
L
1254 {0x8d,0xb4,0x00,0x00}; /* lea 0W(%si),%si */
1255static const unsigned char jump_disp8[] =
1256 {0xeb}; /* jmp disp8 */
1257static const unsigned char jump32_disp32[] =
1258 {0xe9}; /* jmp disp32 */
1259static const unsigned char jump16_disp32[] =
1260 {0x66,0xe9}; /* jmp disp32 */
62a02d25
L
1261/* 32-bit NOPs patterns. */
1262static const unsigned char *const f32_patt[] = {
3ae729d5 1263 f32_1, f32_2, f32_3, f32_4, NULL, f32_6, f32_7
62a02d25
L
1264};
1265/* 16-bit NOPs patterns. */
1266static const unsigned char *const f16_patt[] = {
3ae729d5 1267 f32_1, f32_2, f16_3, f16_4
62a02d25
L
1268};
1269/* nopl (%[re]ax) */
1270static const unsigned char alt_3[] =
1271 {0x0f,0x1f,0x00};
1272/* nopl 0(%[re]ax) */
1273static const unsigned char alt_4[] =
1274 {0x0f,0x1f,0x40,0x00};
1275/* nopl 0(%[re]ax,%[re]ax,1) */
1276static const unsigned char alt_5[] =
1277 {0x0f,0x1f,0x44,0x00,0x00};
1278/* nopw 0(%[re]ax,%[re]ax,1) */
1279static const unsigned char alt_6[] =
1280 {0x66,0x0f,0x1f,0x44,0x00,0x00};
1281/* nopl 0L(%[re]ax) */
1282static const unsigned char alt_7[] =
1283 {0x0f,0x1f,0x80,0x00,0x00,0x00,0x00};
1284/* nopl 0L(%[re]ax,%[re]ax,1) */
1285static const unsigned char alt_8[] =
1286 {0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1287/* nopw 0L(%[re]ax,%[re]ax,1) */
1288static const unsigned char alt_9[] =
1289 {0x66,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1290/* nopw %cs:0L(%[re]ax,%[re]ax,1) */
1291static const unsigned char alt_10[] =
1292 {0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
3ae729d5
L
1293/* data16 nopw %cs:0L(%eax,%eax,1) */
1294static const unsigned char alt_11[] =
1295 {0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
62a02d25
L
1296/* 32-bit and 64-bit NOPs patterns. */
1297static const unsigned char *const alt_patt[] = {
1298 f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
3ae729d5 1299 alt_9, alt_10, alt_11
62a02d25
L
1300};
1301
1302/* Genenerate COUNT bytes of NOPs to WHERE from PATT with the maximum
1303 size of a single NOP instruction MAX_SINGLE_NOP_SIZE. */
1304
1305static void
1306i386_output_nops (char *where, const unsigned char *const *patt,
1307 int count, int max_single_nop_size)
1308
1309{
3ae729d5
L
1310 /* Place the longer NOP first. */
1311 int last;
1312 int offset;
3076e594
NC
1313 const unsigned char *nops;
1314
1315 if (max_single_nop_size < 1)
1316 {
1317 as_fatal (_("i386_output_nops called to generate nops of at most %d bytes!"),
1318 max_single_nop_size);
1319 return;
1320 }
1321
1322 nops = patt[max_single_nop_size - 1];
3ae729d5
L
1323
1324 /* Use the smaller one if the requsted one isn't available. */
1325 if (nops == NULL)
62a02d25 1326 {
3ae729d5
L
1327 max_single_nop_size--;
1328 nops = patt[max_single_nop_size - 1];
62a02d25
L
1329 }
1330
3ae729d5
L
1331 last = count % max_single_nop_size;
1332
1333 count -= last;
1334 for (offset = 0; offset < count; offset += max_single_nop_size)
1335 memcpy (where + offset, nops, max_single_nop_size);
1336
1337 if (last)
1338 {
1339 nops = patt[last - 1];
1340 if (nops == NULL)
1341 {
1342 /* Use the smaller one plus one-byte NOP if the needed one
1343 isn't available. */
1344 last--;
1345 nops = patt[last - 1];
1346 memcpy (where + offset, nops, last);
1347 where[offset + last] = *patt[0];
1348 }
1349 else
1350 memcpy (where + offset, nops, last);
1351 }
62a02d25
L
1352}
1353
3ae729d5
L
1354static INLINE int
1355fits_in_imm7 (offsetT num)
1356{
1357 return (num & 0x7f) == num;
1358}
1359
1360static INLINE int
1361fits_in_imm31 (offsetT num)
1362{
1363 return (num & 0x7fffffff) == num;
1364}
62a02d25
L
1365
1366/* Genenerate COUNT bytes of NOPs to WHERE with the maximum size of a
1367 single NOP instruction LIMIT. */
1368
1369void
3ae729d5 1370i386_generate_nops (fragS *fragP, char *where, offsetT count, int limit)
62a02d25 1371{
3ae729d5 1372 const unsigned char *const *patt = NULL;
62a02d25 1373 int max_single_nop_size;
3ae729d5
L
1374 /* Maximum number of NOPs before switching to jump over NOPs. */
1375 int max_number_of_nops;
62a02d25 1376
3ae729d5 1377 switch (fragP->fr_type)
62a02d25 1378 {
3ae729d5
L
1379 case rs_fill_nop:
1380 case rs_align_code:
1381 break;
e379e5f3
L
1382 case rs_machine_dependent:
1383 /* Allow NOP padding for jumps and calls. */
1384 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
1385 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
1386 break;
1387 /* Fall through. */
3ae729d5 1388 default:
62a02d25
L
1389 return;
1390 }
1391
ccc9c027
L
1392 /* We need to decide which NOP sequence to use for 32bit and
1393 64bit. When -mtune= is used:
4eed87de 1394
76bc74dc
L
1395 1. For PROCESSOR_I386, PROCESSOR_I486, PROCESSOR_PENTIUM and
1396 PROCESSOR_GENERIC32, f32_patt will be used.
80b8656c
L
1397 2. For the rest, alt_patt will be used.
1398
1399 When -mtune= isn't used, alt_patt will be used if
22109423 1400 cpu_arch_isa_flags has CpuNop. Otherwise, f32_patt will
76bc74dc 1401 be used.
ccc9c027
L
1402
1403 When -march= or .arch is used, we can't use anything beyond
1404 cpu_arch_isa_flags. */
1405
1406 if (flag_code == CODE_16BIT)
1407 {
3ae729d5
L
1408 patt = f16_patt;
1409 max_single_nop_size = sizeof (f16_patt) / sizeof (f16_patt[0]);
1410 /* Limit number of NOPs to 2 in 16-bit mode. */
1411 max_number_of_nops = 2;
252b5132 1412 }
33fef721 1413 else
ccc9c027 1414 {
fbf3f584 1415 if (fragP->tc_frag_data.isa == PROCESSOR_UNKNOWN)
ccc9c027
L
1416 {
1417 /* PROCESSOR_UNKNOWN means that all ISAs may be used. */
1418 switch (cpu_arch_tune)
1419 {
1420 case PROCESSOR_UNKNOWN:
1421 /* We use cpu_arch_isa_flags to check if we SHOULD
22109423
L
1422 optimize with nops. */
1423 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1424 patt = alt_patt;
ccc9c027
L
1425 else
1426 patt = f32_patt;
1427 break;
ccc9c027
L
1428 case PROCESSOR_PENTIUM4:
1429 case PROCESSOR_NOCONA:
ef05d495 1430 case PROCESSOR_CORE:
76bc74dc 1431 case PROCESSOR_CORE2:
bd5295b2 1432 case PROCESSOR_COREI7:
76bc74dc 1433 case PROCESSOR_GENERIC64:
ccc9c027
L
1434 case PROCESSOR_K6:
1435 case PROCESSOR_ATHLON:
1436 case PROCESSOR_K8:
4eed87de 1437 case PROCESSOR_AMDFAM10:
8aedb9fe 1438 case PROCESSOR_BD:
029f3522 1439 case PROCESSOR_ZNVER:
7b458c12 1440 case PROCESSOR_BT:
80b8656c 1441 patt = alt_patt;
ccc9c027 1442 break;
76bc74dc 1443 case PROCESSOR_I386:
ccc9c027
L
1444 case PROCESSOR_I486:
1445 case PROCESSOR_PENTIUM:
2dde1948 1446 case PROCESSOR_PENTIUMPRO:
81486035 1447 case PROCESSOR_IAMCU:
ccc9c027
L
1448 case PROCESSOR_GENERIC32:
1449 patt = f32_patt;
1450 break;
c368d2a8
JB
1451 case PROCESSOR_NONE:
1452 abort ();
4eed87de 1453 }
ccc9c027
L
1454 }
1455 else
1456 {
fbf3f584 1457 switch (fragP->tc_frag_data.tune)
ccc9c027
L
1458 {
1459 case PROCESSOR_UNKNOWN:
e6a14101 1460 /* When cpu_arch_isa is set, cpu_arch_tune shouldn't be
ccc9c027
L
1461 PROCESSOR_UNKNOWN. */
1462 abort ();
1463 break;
1464
76bc74dc 1465 case PROCESSOR_I386:
ccc9c027
L
1466 case PROCESSOR_I486:
1467 case PROCESSOR_PENTIUM:
81486035 1468 case PROCESSOR_IAMCU:
ccc9c027
L
1469 case PROCESSOR_K6:
1470 case PROCESSOR_ATHLON:
1471 case PROCESSOR_K8:
4eed87de 1472 case PROCESSOR_AMDFAM10:
8aedb9fe 1473 case PROCESSOR_BD:
029f3522 1474 case PROCESSOR_ZNVER:
7b458c12 1475 case PROCESSOR_BT:
ccc9c027
L
1476 case PROCESSOR_GENERIC32:
1477 /* We use cpu_arch_isa_flags to check if we CAN optimize
22109423
L
1478 with nops. */
1479 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1480 patt = alt_patt;
ccc9c027
L
1481 else
1482 patt = f32_patt;
1483 break;
76bc74dc
L
1484 case PROCESSOR_PENTIUMPRO:
1485 case PROCESSOR_PENTIUM4:
1486 case PROCESSOR_NOCONA:
1487 case PROCESSOR_CORE:
ef05d495 1488 case PROCESSOR_CORE2:
bd5295b2 1489 case PROCESSOR_COREI7:
22109423 1490 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1491 patt = alt_patt;
ccc9c027
L
1492 else
1493 patt = f32_patt;
1494 break;
1495 case PROCESSOR_GENERIC64:
80b8656c 1496 patt = alt_patt;
ccc9c027 1497 break;
c368d2a8
JB
1498 case PROCESSOR_NONE:
1499 abort ();
4eed87de 1500 }
ccc9c027
L
1501 }
1502
76bc74dc
L
1503 if (patt == f32_patt)
1504 {
3ae729d5
L
1505 max_single_nop_size = sizeof (f32_patt) / sizeof (f32_patt[0]);
1506 /* Limit number of NOPs to 2 for older processors. */
1507 max_number_of_nops = 2;
76bc74dc
L
1508 }
1509 else
1510 {
3ae729d5
L
1511 max_single_nop_size = sizeof (alt_patt) / sizeof (alt_patt[0]);
1512 /* Limit number of NOPs to 7 for newer processors. */
1513 max_number_of_nops = 7;
1514 }
1515 }
1516
1517 if (limit == 0)
1518 limit = max_single_nop_size;
1519
1520 if (fragP->fr_type == rs_fill_nop)
1521 {
1522 /* Output NOPs for .nop directive. */
1523 if (limit > max_single_nop_size)
1524 {
1525 as_bad_where (fragP->fr_file, fragP->fr_line,
1526 _("invalid single nop size: %d "
1527 "(expect within [0, %d])"),
1528 limit, max_single_nop_size);
1529 return;
1530 }
1531 }
e379e5f3 1532 else if (fragP->fr_type != rs_machine_dependent)
3ae729d5
L
1533 fragP->fr_var = count;
1534
1535 if ((count / max_single_nop_size) > max_number_of_nops)
1536 {
1537 /* Generate jump over NOPs. */
1538 offsetT disp = count - 2;
1539 if (fits_in_imm7 (disp))
1540 {
1541 /* Use "jmp disp8" if possible. */
1542 count = disp;
1543 where[0] = jump_disp8[0];
1544 where[1] = count;
1545 where += 2;
1546 }
1547 else
1548 {
1549 unsigned int size_of_jump;
1550
1551 if (flag_code == CODE_16BIT)
1552 {
1553 where[0] = jump16_disp32[0];
1554 where[1] = jump16_disp32[1];
1555 size_of_jump = 2;
1556 }
1557 else
1558 {
1559 where[0] = jump32_disp32[0];
1560 size_of_jump = 1;
1561 }
1562
1563 count -= size_of_jump + 4;
1564 if (!fits_in_imm31 (count))
1565 {
1566 as_bad_where (fragP->fr_file, fragP->fr_line,
1567 _("jump over nop padding out of range"));
1568 return;
1569 }
1570
1571 md_number_to_chars (where + size_of_jump, count, 4);
1572 where += size_of_jump + 4;
76bc74dc 1573 }
ccc9c027 1574 }
3ae729d5
L
1575
1576 /* Generate multiple NOPs. */
1577 i386_output_nops (where, patt, count, limit);
252b5132
RH
1578}
1579
c6fb90c8 1580static INLINE int
0dfbf9d7 1581operand_type_all_zero (const union i386_operand_type *x)
40fb9820 1582{
0dfbf9d7 1583 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1584 {
1585 case 3:
0dfbf9d7 1586 if (x->array[2])
c6fb90c8 1587 return 0;
1a0670f3 1588 /* Fall through. */
c6fb90c8 1589 case 2:
0dfbf9d7 1590 if (x->array[1])
c6fb90c8 1591 return 0;
1a0670f3 1592 /* Fall through. */
c6fb90c8 1593 case 1:
0dfbf9d7 1594 return !x->array[0];
c6fb90c8
L
1595 default:
1596 abort ();
1597 }
40fb9820
L
1598}
1599
c6fb90c8 1600static INLINE void
0dfbf9d7 1601operand_type_set (union i386_operand_type *x, unsigned int v)
40fb9820 1602{
0dfbf9d7 1603 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1604 {
1605 case 3:
0dfbf9d7 1606 x->array[2] = v;
1a0670f3 1607 /* Fall through. */
c6fb90c8 1608 case 2:
0dfbf9d7 1609 x->array[1] = v;
1a0670f3 1610 /* Fall through. */
c6fb90c8 1611 case 1:
0dfbf9d7 1612 x->array[0] = v;
1a0670f3 1613 /* Fall through. */
c6fb90c8
L
1614 break;
1615 default:
1616 abort ();
1617 }
bab6aec1
JB
1618
1619 x->bitfield.class = ClassNone;
75e5731b 1620 x->bitfield.instance = InstanceNone;
c6fb90c8 1621}
40fb9820 1622
c6fb90c8 1623static INLINE int
0dfbf9d7
L
1624operand_type_equal (const union i386_operand_type *x,
1625 const union i386_operand_type *y)
c6fb90c8 1626{
0dfbf9d7 1627 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1628 {
1629 case 3:
0dfbf9d7 1630 if (x->array[2] != y->array[2])
c6fb90c8 1631 return 0;
1a0670f3 1632 /* Fall through. */
c6fb90c8 1633 case 2:
0dfbf9d7 1634 if (x->array[1] != y->array[1])
c6fb90c8 1635 return 0;
1a0670f3 1636 /* Fall through. */
c6fb90c8 1637 case 1:
0dfbf9d7 1638 return x->array[0] == y->array[0];
c6fb90c8
L
1639 break;
1640 default:
1641 abort ();
1642 }
1643}
40fb9820 1644
0dfbf9d7
L
1645static INLINE int
1646cpu_flags_all_zero (const union i386_cpu_flags *x)
1647{
1648 switch (ARRAY_SIZE(x->array))
1649 {
75f8266a
KL
1650 case 5:
1651 if (x->array[4])
1652 return 0;
1653 /* Fall through. */
53467f57
IT
1654 case 4:
1655 if (x->array[3])
1656 return 0;
1657 /* Fall through. */
0dfbf9d7
L
1658 case 3:
1659 if (x->array[2])
1660 return 0;
1a0670f3 1661 /* Fall through. */
0dfbf9d7
L
1662 case 2:
1663 if (x->array[1])
1664 return 0;
1a0670f3 1665 /* Fall through. */
0dfbf9d7
L
1666 case 1:
1667 return !x->array[0];
1668 default:
1669 abort ();
1670 }
1671}
1672
0dfbf9d7
L
1673static INLINE int
1674cpu_flags_equal (const union i386_cpu_flags *x,
1675 const union i386_cpu_flags *y)
1676{
1677 switch (ARRAY_SIZE(x->array))
1678 {
75f8266a
KL
1679 case 5:
1680 if (x->array[4] != y->array[4])
1681 return 0;
1682 /* Fall through. */
53467f57
IT
1683 case 4:
1684 if (x->array[3] != y->array[3])
1685 return 0;
1686 /* Fall through. */
0dfbf9d7
L
1687 case 3:
1688 if (x->array[2] != y->array[2])
1689 return 0;
1a0670f3 1690 /* Fall through. */
0dfbf9d7
L
1691 case 2:
1692 if (x->array[1] != y->array[1])
1693 return 0;
1a0670f3 1694 /* Fall through. */
0dfbf9d7
L
1695 case 1:
1696 return x->array[0] == y->array[0];
1697 break;
1698 default:
1699 abort ();
1700 }
1701}
c6fb90c8
L
1702
1703static INLINE int
1704cpu_flags_check_cpu64 (i386_cpu_flags f)
1705{
1706 return !((flag_code == CODE_64BIT && f.bitfield.cpuno64)
1707 || (flag_code != CODE_64BIT && f.bitfield.cpu64));
40fb9820
L
1708}
1709
c6fb90c8
L
1710static INLINE i386_cpu_flags
1711cpu_flags_and (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1712{
c6fb90c8
L
1713 switch (ARRAY_SIZE (x.array))
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];
1729 break;
1730 default:
1731 abort ();
1732 }
1733 return x;
1734}
40fb9820 1735
c6fb90c8
L
1736static INLINE i386_cpu_flags
1737cpu_flags_or (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1738{
c6fb90c8 1739 switch (ARRAY_SIZE (x.array))
40fb9820 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. */
c6fb90c8
L
1747 case 3:
1748 x.array [2] |= y.array [2];
1a0670f3 1749 /* Fall through. */
c6fb90c8
L
1750 case 2:
1751 x.array [1] |= y.array [1];
1a0670f3 1752 /* Fall through. */
c6fb90c8
L
1753 case 1:
1754 x.array [0] |= y.array [0];
40fb9820
L
1755 break;
1756 default:
1757 abort ();
1758 }
40fb9820
L
1759 return x;
1760}
1761
309d3373
JB
1762static INLINE i386_cpu_flags
1763cpu_flags_and_not (i386_cpu_flags x, i386_cpu_flags y)
1764{
1765 switch (ARRAY_SIZE (x.array))
1766 {
75f8266a
KL
1767 case 5:
1768 x.array [4] &= ~y.array [4];
1769 /* Fall through. */
53467f57
IT
1770 case 4:
1771 x.array [3] &= ~y.array [3];
1772 /* Fall through. */
309d3373
JB
1773 case 3:
1774 x.array [2] &= ~y.array [2];
1a0670f3 1775 /* Fall through. */
309d3373
JB
1776 case 2:
1777 x.array [1] &= ~y.array [1];
1a0670f3 1778 /* Fall through. */
309d3373
JB
1779 case 1:
1780 x.array [0] &= ~y.array [0];
1781 break;
1782 default:
1783 abort ();
1784 }
1785 return x;
1786}
1787
6c0946d0
JB
1788static const i386_cpu_flags avx512 = CPU_ANY_AVX512F_FLAGS;
1789
c0f3af97
L
1790#define CPU_FLAGS_ARCH_MATCH 0x1
1791#define CPU_FLAGS_64BIT_MATCH 0x2
1792
c0f3af97 1793#define CPU_FLAGS_PERFECT_MATCH \
db12e14e 1794 (CPU_FLAGS_ARCH_MATCH | CPU_FLAGS_64BIT_MATCH)
c0f3af97
L
1795
1796/* Return CPU flags match bits. */
3629bb00 1797
40fb9820 1798static int
d3ce72d0 1799cpu_flags_match (const insn_template *t)
40fb9820 1800{
c0f3af97
L
1801 i386_cpu_flags x = t->cpu_flags;
1802 int match = cpu_flags_check_cpu64 (x) ? CPU_FLAGS_64BIT_MATCH : 0;
40fb9820
L
1803
1804 x.bitfield.cpu64 = 0;
1805 x.bitfield.cpuno64 = 0;
1806
0dfbf9d7 1807 if (cpu_flags_all_zero (&x))
c0f3af97
L
1808 {
1809 /* This instruction is available on all archs. */
db12e14e 1810 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 1811 }
3629bb00
L
1812 else
1813 {
c0f3af97 1814 /* This instruction is available only on some archs. */
3629bb00
L
1815 i386_cpu_flags cpu = cpu_arch_flags;
1816
ab592e75
JB
1817 /* AVX512VL is no standalone feature - match it and then strip it. */
1818 if (x.bitfield.cpuavx512vl && !cpu.bitfield.cpuavx512vl)
1819 return match;
1820 x.bitfield.cpuavx512vl = 0;
1821
22c36940
JB
1822 /* AVX and AVX2 present at the same time express an operand size
1823 dependency - strip AVX2 for the purposes here. The operand size
1824 dependent check occurs in check_vecOperands(). */
1825 if (x.bitfield.cpuavx && x.bitfield.cpuavx2)
1826 x.bitfield.cpuavx2 = 0;
1827
3629bb00 1828 cpu = cpu_flags_and (x, cpu);
c0f3af97
L
1829 if (!cpu_flags_all_zero (&cpu))
1830 {
57392598 1831 if (x.bitfield.cpuavx)
a5ff0eb2 1832 {
929f69fa 1833 /* We need to check a few extra flags with AVX. */
b9d49817 1834 if (cpu.bitfield.cpuavx
40d231b4
JB
1835 && (!t->opcode_modifier.sse2avx
1836 || (sse2avx && !i.prefix[DATA_PREFIX]))
b9d49817 1837 && (!x.bitfield.cpuaes || cpu.bitfield.cpuaes)
929f69fa 1838 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
b9d49817
JB
1839 && (!x.bitfield.cpupclmul || cpu.bitfield.cpupclmul))
1840 match |= CPU_FLAGS_ARCH_MATCH;
a5ff0eb2 1841 }
929f69fa
JB
1842 else if (x.bitfield.cpuavx512f)
1843 {
1844 /* We need to check a few extra flags with AVX512F. */
1845 if (cpu.bitfield.cpuavx512f
1846 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
1847 && (!x.bitfield.cpuvaes || cpu.bitfield.cpuvaes)
1848 && (!x.bitfield.cpuvpclmulqdq || cpu.bitfield.cpuvpclmulqdq))
1849 match |= CPU_FLAGS_ARCH_MATCH;
1850 }
a5ff0eb2 1851 else
db12e14e 1852 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 1853 }
3629bb00 1854 }
c0f3af97 1855 return match;
40fb9820
L
1856}
1857
c6fb90c8
L
1858static INLINE i386_operand_type
1859operand_type_and (i386_operand_type x, i386_operand_type y)
40fb9820 1860{
bab6aec1
JB
1861 if (x.bitfield.class != y.bitfield.class)
1862 x.bitfield.class = ClassNone;
75e5731b
JB
1863 if (x.bitfield.instance != y.bitfield.instance)
1864 x.bitfield.instance = InstanceNone;
bab6aec1 1865
c6fb90c8
L
1866 switch (ARRAY_SIZE (x.array))
1867 {
1868 case 3:
1869 x.array [2] &= y.array [2];
1a0670f3 1870 /* Fall through. */
c6fb90c8
L
1871 case 2:
1872 x.array [1] &= y.array [1];
1a0670f3 1873 /* Fall through. */
c6fb90c8
L
1874 case 1:
1875 x.array [0] &= y.array [0];
1876 break;
1877 default:
1878 abort ();
1879 }
1880 return x;
40fb9820
L
1881}
1882
73053c1f
JB
1883static INLINE i386_operand_type
1884operand_type_and_not (i386_operand_type x, i386_operand_type y)
1885{
bab6aec1 1886 gas_assert (y.bitfield.class == ClassNone);
75e5731b 1887 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 1888
73053c1f
JB
1889 switch (ARRAY_SIZE (x.array))
1890 {
1891 case 3:
1892 x.array [2] &= ~y.array [2];
1893 /* Fall through. */
1894 case 2:
1895 x.array [1] &= ~y.array [1];
1896 /* Fall through. */
1897 case 1:
1898 x.array [0] &= ~y.array [0];
1899 break;
1900 default:
1901 abort ();
1902 }
1903 return x;
1904}
1905
c6fb90c8
L
1906static INLINE i386_operand_type
1907operand_type_or (i386_operand_type x, i386_operand_type y)
40fb9820 1908{
bab6aec1
JB
1909 gas_assert (x.bitfield.class == ClassNone ||
1910 y.bitfield.class == ClassNone ||
1911 x.bitfield.class == y.bitfield.class);
75e5731b
JB
1912 gas_assert (x.bitfield.instance == InstanceNone ||
1913 y.bitfield.instance == InstanceNone ||
1914 x.bitfield.instance == y.bitfield.instance);
bab6aec1 1915
c6fb90c8 1916 switch (ARRAY_SIZE (x.array))
40fb9820 1917 {
c6fb90c8
L
1918 case 3:
1919 x.array [2] |= y.array [2];
1a0670f3 1920 /* Fall through. */
c6fb90c8
L
1921 case 2:
1922 x.array [1] |= y.array [1];
1a0670f3 1923 /* Fall through. */
c6fb90c8
L
1924 case 1:
1925 x.array [0] |= y.array [0];
40fb9820
L
1926 break;
1927 default:
1928 abort ();
1929 }
c6fb90c8
L
1930 return x;
1931}
40fb9820 1932
c6fb90c8
L
1933static INLINE i386_operand_type
1934operand_type_xor (i386_operand_type x, i386_operand_type y)
1935{
bab6aec1 1936 gas_assert (y.bitfield.class == ClassNone);
75e5731b 1937 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 1938
c6fb90c8
L
1939 switch (ARRAY_SIZE (x.array))
1940 {
1941 case 3:
1942 x.array [2] ^= y.array [2];
1a0670f3 1943 /* Fall through. */
c6fb90c8
L
1944 case 2:
1945 x.array [1] ^= y.array [1];
1a0670f3 1946 /* Fall through. */
c6fb90c8
L
1947 case 1:
1948 x.array [0] ^= y.array [0];
1949 break;
1950 default:
1951 abort ();
1952 }
40fb9820
L
1953 return x;
1954}
1955
05909f23
JB
1956static const i386_operand_type anydisp = {
1957 .bitfield = { .disp8 = 1, .disp16 = 1, .disp32 = 1, .disp64 = 1 }
1958};
40fb9820
L
1959
1960enum operand_type
1961{
1962 reg,
40fb9820
L
1963 imm,
1964 disp,
1965 anymem
1966};
1967
c6fb90c8 1968static INLINE int
40fb9820
L
1969operand_type_check (i386_operand_type t, enum operand_type c)
1970{
1971 switch (c)
1972 {
1973 case reg:
bab6aec1 1974 return t.bitfield.class == Reg;
40fb9820 1975
40fb9820
L
1976 case imm:
1977 return (t.bitfield.imm8
1978 || t.bitfield.imm8s
1979 || t.bitfield.imm16
1980 || t.bitfield.imm32
1981 || t.bitfield.imm32s
1982 || t.bitfield.imm64);
1983
1984 case disp:
1985 return (t.bitfield.disp8
1986 || t.bitfield.disp16
1987 || t.bitfield.disp32
40fb9820
L
1988 || t.bitfield.disp64);
1989
1990 case anymem:
1991 return (t.bitfield.disp8
1992 || t.bitfield.disp16
1993 || t.bitfield.disp32
40fb9820
L
1994 || t.bitfield.disp64
1995 || t.bitfield.baseindex);
1996
1997 default:
1998 abort ();
1999 }
2cfe26b6
AM
2000
2001 return 0;
40fb9820
L
2002}
2003
7a54636a
L
2004/* Return 1 if there is no conflict in 8bit/16bit/32bit/64bit/80bit size
2005 between operand GIVEN and opeand WANTED for instruction template T. */
5c07affc
L
2006
2007static INLINE int
7a54636a
L
2008match_operand_size (const insn_template *t, unsigned int wanted,
2009 unsigned int given)
5c07affc 2010{
3ac21baa
JB
2011 return !((i.types[given].bitfield.byte
2012 && !t->operand_types[wanted].bitfield.byte)
2013 || (i.types[given].bitfield.word
2014 && !t->operand_types[wanted].bitfield.word)
2015 || (i.types[given].bitfield.dword
2016 && !t->operand_types[wanted].bitfield.dword)
2017 || (i.types[given].bitfield.qword
9db83a32
JB
2018 && (!t->operand_types[wanted].bitfield.qword
2019 /* Don't allow 64-bit (memory) operands outside of 64-bit
2020 mode, when they're used where a 64-bit GPR could also
2021 be used. Checking is needed for Intel Syntax only. */
2022 || (intel_syntax
2023 && flag_code != CODE_64BIT
2024 && (t->operand_types[wanted].bitfield.class == Reg
2025 || t->operand_types[wanted].bitfield.class == Accum
2026 || t->opcode_modifier.isstring))))
3ac21baa
JB
2027 || (i.types[given].bitfield.tbyte
2028 && !t->operand_types[wanted].bitfield.tbyte));
5c07affc
L
2029}
2030
dd40ce22
L
2031/* Return 1 if there is no conflict in SIMD register between operand
2032 GIVEN and opeand WANTED for instruction template T. */
1b54b8d7
JB
2033
2034static INLINE int
dd40ce22
L
2035match_simd_size (const insn_template *t, unsigned int wanted,
2036 unsigned int given)
1b54b8d7 2037{
3ac21baa
JB
2038 return !((i.types[given].bitfield.xmmword
2039 && !t->operand_types[wanted].bitfield.xmmword)
2040 || (i.types[given].bitfield.ymmword
2041 && !t->operand_types[wanted].bitfield.ymmword)
2042 || (i.types[given].bitfield.zmmword
260cd341
LC
2043 && !t->operand_types[wanted].bitfield.zmmword)
2044 || (i.types[given].bitfield.tmmword
2045 && !t->operand_types[wanted].bitfield.tmmword));
1b54b8d7
JB
2046}
2047
7a54636a
L
2048/* Return 1 if there is no conflict in any size between operand GIVEN
2049 and opeand WANTED for instruction template T. */
5c07affc
L
2050
2051static INLINE int
dd40ce22
L
2052match_mem_size (const insn_template *t, unsigned int wanted,
2053 unsigned int given)
5c07affc 2054{
7a54636a 2055 return (match_operand_size (t, wanted, given)
3ac21baa 2056 && !((i.types[given].bitfield.unspecified
5273a3cd 2057 && !i.broadcast.type
a5748e0d 2058 && !i.broadcast.bytes
3ac21baa
JB
2059 && !t->operand_types[wanted].bitfield.unspecified)
2060 || (i.types[given].bitfield.fword
2061 && !t->operand_types[wanted].bitfield.fword)
1b54b8d7
JB
2062 /* For scalar opcode templates to allow register and memory
2063 operands at the same time, some special casing is needed
d6793fa1
JB
2064 here. Also for v{,p}broadcast*, {,v}pmov{s,z}*, and
2065 down-conversion vpmov*. */
3528c362 2066 || ((t->operand_types[wanted].bitfield.class == RegSIMD
bc49bfd8
JB
2067 && t->operand_types[wanted].bitfield.byte
2068 + t->operand_types[wanted].bitfield.word
2069 + t->operand_types[wanted].bitfield.dword
2070 + t->operand_types[wanted].bitfield.qword
2071 > !!t->opcode_modifier.broadcast)
3ac21baa
JB
2072 ? (i.types[given].bitfield.xmmword
2073 || i.types[given].bitfield.ymmword
2074 || i.types[given].bitfield.zmmword)
2075 : !match_simd_size(t, wanted, given))));
5c07affc
L
2076}
2077
3ac21baa
JB
2078/* Return value has MATCH_STRAIGHT set if there is no size conflict on any
2079 operands for instruction template T, and it has MATCH_REVERSE set if there
2080 is no size conflict on any operands for the template with operands reversed
2081 (and the template allows for reversing in the first place). */
5c07affc 2082
3ac21baa
JB
2083#define MATCH_STRAIGHT 1
2084#define MATCH_REVERSE 2
2085
2086static INLINE unsigned int
d3ce72d0 2087operand_size_match (const insn_template *t)
5c07affc 2088{
3ac21baa 2089 unsigned int j, match = MATCH_STRAIGHT;
5c07affc 2090
0cfa3eb3 2091 /* Don't check non-absolute jump instructions. */
5c07affc 2092 if (t->opcode_modifier.jump
0cfa3eb3 2093 && t->opcode_modifier.jump != JUMP_ABSOLUTE)
5c07affc
L
2094 return match;
2095
2096 /* Check memory and accumulator operand size. */
2097 for (j = 0; j < i.operands; j++)
2098 {
3528c362
JB
2099 if (i.types[j].bitfield.class != Reg
2100 && i.types[j].bitfield.class != RegSIMD
255571cd 2101 && t->opcode_modifier.operandconstraint == ANY_SIZE)
5c07affc
L
2102 continue;
2103
bab6aec1 2104 if (t->operand_types[j].bitfield.class == Reg
7a54636a 2105 && !match_operand_size (t, j, j))
5c07affc
L
2106 {
2107 match = 0;
2108 break;
2109 }
2110
3528c362 2111 if (t->operand_types[j].bitfield.class == RegSIMD
3ac21baa 2112 && !match_simd_size (t, j, j))
1b54b8d7
JB
2113 {
2114 match = 0;
2115 break;
2116 }
2117
75e5731b 2118 if (t->operand_types[j].bitfield.instance == Accum
7a54636a 2119 && (!match_operand_size (t, j, j) || !match_simd_size (t, j, j)))
1b54b8d7
JB
2120 {
2121 match = 0;
2122 break;
2123 }
2124
c48dadc9 2125 if ((i.flags[j] & Operand_Mem) && !match_mem_size (t, j, j))
5c07affc
L
2126 {
2127 match = 0;
2128 break;
2129 }
2130 }
2131
3ac21baa 2132 if (!t->opcode_modifier.d)
7b94647a 2133 return match;
5c07affc
L
2134
2135 /* Check reverse. */
8bd915b7
JB
2136 gas_assert ((i.operands >= 2 && i.operands <= 3)
2137 || t->opcode_modifier.vexsources);
5c07affc 2138
f5eb1d70 2139 for (j = 0; j < i.operands; j++)
5c07affc 2140 {
f5eb1d70
JB
2141 unsigned int given = i.operands - j - 1;
2142
8bd915b7
JB
2143 /* For 4- and 5-operand insns VEX.W controls just the first two
2144 register operands. */
2145 if (t->opcode_modifier.vexsources)
2146 given = j < 2 ? 1 - j : j;
2147
bab6aec1 2148 if (t->operand_types[j].bitfield.class == Reg
f5eb1d70 2149 && !match_operand_size (t, j, given))
7b94647a 2150 return match;
5c07affc 2151
3528c362 2152 if (t->operand_types[j].bitfield.class == RegSIMD
f5eb1d70 2153 && !match_simd_size (t, j, given))
7b94647a 2154 return match;
dbbc8b7e 2155
75e5731b 2156 if (t->operand_types[j].bitfield.instance == Accum
f5eb1d70
JB
2157 && (!match_operand_size (t, j, given)
2158 || !match_simd_size (t, j, given)))
7b94647a 2159 return match;
dbbc8b7e 2160
f5eb1d70 2161 if ((i.flags[given] & Operand_Mem) && !match_mem_size (t, j, given))
7b94647a 2162 return match;
5c07affc
L
2163 }
2164
3ac21baa 2165 return match | MATCH_REVERSE;
5c07affc
L
2166}
2167
c6fb90c8 2168static INLINE int
40fb9820
L
2169operand_type_match (i386_operand_type overlap,
2170 i386_operand_type given)
2171{
2172 i386_operand_type temp = overlap;
2173
7d5e4556 2174 temp.bitfield.unspecified = 0;
5c07affc
L
2175 temp.bitfield.byte = 0;
2176 temp.bitfield.word = 0;
2177 temp.bitfield.dword = 0;
2178 temp.bitfield.fword = 0;
2179 temp.bitfield.qword = 0;
2180 temp.bitfield.tbyte = 0;
2181 temp.bitfield.xmmword = 0;
c0f3af97 2182 temp.bitfield.ymmword = 0;
43234a1e 2183 temp.bitfield.zmmword = 0;
260cd341 2184 temp.bitfield.tmmword = 0;
0dfbf9d7 2185 if (operand_type_all_zero (&temp))
891edac4 2186 goto mismatch;
40fb9820 2187
6f2f06be 2188 if (given.bitfield.baseindex == overlap.bitfield.baseindex)
891edac4
L
2189 return 1;
2190
dc1e8a47 2191 mismatch:
a65babc9 2192 i.error = operand_type_mismatch;
891edac4 2193 return 0;
40fb9820
L
2194}
2195
7d5e4556 2196/* If given types g0 and g1 are registers they must be of the same type
10c17abd 2197 unless the expected operand type register overlap is null.
8ee52bcf 2198 Intel syntax sized memory operands are also checked here. */
40fb9820 2199
c6fb90c8 2200static INLINE int
dc821c5f 2201operand_type_register_match (i386_operand_type g0,
40fb9820 2202 i386_operand_type t0,
40fb9820
L
2203 i386_operand_type g1,
2204 i386_operand_type t1)
2205{
bab6aec1 2206 if (g0.bitfield.class != Reg
3528c362 2207 && g0.bitfield.class != RegSIMD
8ee52bcf
JB
2208 && (g0.bitfield.unspecified
2209 || !operand_type_check (g0, anymem)))
40fb9820
L
2210 return 1;
2211
bab6aec1 2212 if (g1.bitfield.class != Reg
3528c362 2213 && g1.bitfield.class != RegSIMD
8ee52bcf
JB
2214 && (g1.bitfield.unspecified
2215 || !operand_type_check (g1, anymem)))
40fb9820
L
2216 return 1;
2217
dc821c5f
JB
2218 if (g0.bitfield.byte == g1.bitfield.byte
2219 && g0.bitfield.word == g1.bitfield.word
2220 && g0.bitfield.dword == g1.bitfield.dword
10c17abd
JB
2221 && g0.bitfield.qword == g1.bitfield.qword
2222 && g0.bitfield.xmmword == g1.bitfield.xmmword
2223 && g0.bitfield.ymmword == g1.bitfield.ymmword
2224 && g0.bitfield.zmmword == g1.bitfield.zmmword)
40fb9820
L
2225 return 1;
2226
c4d09633
JB
2227 /* If expectations overlap in no more than a single size, all is fine. */
2228 g0 = operand_type_and (t0, t1);
2229 if (g0.bitfield.byte
2230 + g0.bitfield.word
2231 + g0.bitfield.dword
2232 + g0.bitfield.qword
2233 + g0.bitfield.xmmword
2234 + g0.bitfield.ymmword
2235 + g0.bitfield.zmmword <= 1)
891edac4
L
2236 return 1;
2237
a65babc9 2238 i.error = register_type_mismatch;
891edac4
L
2239
2240 return 0;
40fb9820
L
2241}
2242
4c692bc7
JB
2243static INLINE unsigned int
2244register_number (const reg_entry *r)
2245{
2246 unsigned int nr = r->reg_num;
2247
2248 if (r->reg_flags & RegRex)
2249 nr += 8;
2250
200cbe0f
L
2251 if (r->reg_flags & RegVRex)
2252 nr += 16;
2253
4c692bc7
JB
2254 return nr;
2255}
2256
252b5132 2257static INLINE unsigned int
40fb9820 2258mode_from_disp_size (i386_operand_type t)
252b5132 2259{
b5014f7a 2260 if (t.bitfield.disp8)
40fb9820
L
2261 return 1;
2262 else if (t.bitfield.disp16
a775efc8 2263 || t.bitfield.disp32)
40fb9820
L
2264 return 2;
2265 else
2266 return 0;
252b5132
RH
2267}
2268
2269static INLINE int
65879393 2270fits_in_signed_byte (addressT num)
252b5132 2271{
65879393 2272 return num + 0x80 <= 0xff;
47926f60 2273}
252b5132
RH
2274
2275static INLINE int
65879393 2276fits_in_unsigned_byte (addressT num)
252b5132 2277{
65879393 2278 return num <= 0xff;
47926f60 2279}
252b5132
RH
2280
2281static INLINE int
65879393 2282fits_in_unsigned_word (addressT num)
252b5132 2283{
65879393 2284 return num <= 0xffff;
47926f60 2285}
252b5132
RH
2286
2287static INLINE int
65879393 2288fits_in_signed_word (addressT num)
252b5132 2289{
65879393 2290 return num + 0x8000 <= 0xffff;
47926f60 2291}
2a962e6d 2292
3e73aa7c 2293static INLINE int
65879393 2294fits_in_signed_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2295{
2296#ifndef BFD64
2297 return 1;
2298#else
65879393 2299 return num + 0x80000000 <= 0xffffffff;
3e73aa7c
JH
2300#endif
2301} /* fits_in_signed_long() */
2a962e6d 2302
3e73aa7c 2303static INLINE int
65879393 2304fits_in_unsigned_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2305{
2306#ifndef BFD64
2307 return 1;
2308#else
65879393 2309 return num <= 0xffffffff;
3e73aa7c
JH
2310#endif
2311} /* fits_in_unsigned_long() */
252b5132 2312
a442cac5
JB
2313static INLINE valueT extend_to_32bit_address (addressT num)
2314{
2315#ifdef BFD64
2316 if (fits_in_unsigned_long(num))
2317 return (num ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
2318
2319 if (!fits_in_signed_long (num))
2320 return num & 0xffffffff;
2321#endif
2322
2323 return num;
2324}
2325
43234a1e 2326static INLINE int
b5014f7a 2327fits_in_disp8 (offsetT num)
43234a1e
L
2328{
2329 int shift = i.memshift;
2330 unsigned int mask;
2331
2332 if (shift == -1)
2333 abort ();
2334
2335 mask = (1 << shift) - 1;
2336
2337 /* Return 0 if NUM isn't properly aligned. */
2338 if ((num & mask))
2339 return 0;
2340
2341 /* Check if NUM will fit in 8bit after shift. */
2342 return fits_in_signed_byte (num >> shift);
2343}
2344
a683cc34
SP
2345static INLINE int
2346fits_in_imm4 (offsetT num)
2347{
2348 return (num & 0xf) == num;
2349}
2350
40fb9820 2351static i386_operand_type
e3bb37b5 2352smallest_imm_type (offsetT num)
252b5132 2353{
40fb9820 2354 i386_operand_type t;
7ab9ffdd 2355
0dfbf9d7 2356 operand_type_set (&t, 0);
40fb9820
L
2357 t.bitfield.imm64 = 1;
2358
2359 if (cpu_arch_tune != PROCESSOR_I486 && num == 1)
e413e4e9
AM
2360 {
2361 /* This code is disabled on the 486 because all the Imm1 forms
2362 in the opcode table are slower on the i486. They're the
2363 versions with the implicitly specified single-position
2364 displacement, which has another syntax if you really want to
2365 use that form. */
40fb9820
L
2366 t.bitfield.imm1 = 1;
2367 t.bitfield.imm8 = 1;
2368 t.bitfield.imm8s = 1;
2369 t.bitfield.imm16 = 1;
2370 t.bitfield.imm32 = 1;
2371 t.bitfield.imm32s = 1;
2372 }
2373 else if (fits_in_signed_byte (num))
2374 {
2375 t.bitfield.imm8 = 1;
2376 t.bitfield.imm8s = 1;
2377 t.bitfield.imm16 = 1;
2378 t.bitfield.imm32 = 1;
2379 t.bitfield.imm32s = 1;
2380 }
2381 else if (fits_in_unsigned_byte (num))
2382 {
2383 t.bitfield.imm8 = 1;
2384 t.bitfield.imm16 = 1;
2385 t.bitfield.imm32 = 1;
2386 t.bitfield.imm32s = 1;
2387 }
2388 else if (fits_in_signed_word (num) || fits_in_unsigned_word (num))
2389 {
2390 t.bitfield.imm16 = 1;
2391 t.bitfield.imm32 = 1;
2392 t.bitfield.imm32s = 1;
2393 }
2394 else if (fits_in_signed_long (num))
2395 {
2396 t.bitfield.imm32 = 1;
2397 t.bitfield.imm32s = 1;
2398 }
2399 else if (fits_in_unsigned_long (num))
2400 t.bitfield.imm32 = 1;
2401
2402 return t;
47926f60 2403}
252b5132 2404
847f7ad4 2405static offsetT
e3bb37b5 2406offset_in_range (offsetT val, int size)
847f7ad4 2407{
508866be 2408 addressT mask;
ba2adb93 2409
847f7ad4
AM
2410 switch (size)
2411 {
508866be
L
2412 case 1: mask = ((addressT) 1 << 8) - 1; break;
2413 case 2: mask = ((addressT) 1 << 16) - 1; break;
3e73aa7c 2414#ifdef BFD64
64965897 2415 case 4: mask = ((addressT) 1 << 32) - 1; break;
3e73aa7c 2416#endif
64965897 2417 case sizeof (val): return val;
47926f60 2418 default: abort ();
847f7ad4
AM
2419 }
2420
4fe51f7d 2421 if ((val & ~mask) != 0 && (-val & ~mask) != 0)
f493c217
AM
2422 as_warn (_("0x%" PRIx64 " shortened to 0x%" PRIx64),
2423 (uint64_t) val, (uint64_t) (val & mask));
847f7ad4 2424
847f7ad4
AM
2425 return val & mask;
2426}
2427
c32fa91d
L
2428enum PREFIX_GROUP
2429{
2430 PREFIX_EXIST = 0,
2431 PREFIX_LOCK,
2432 PREFIX_REP,
04ef582a 2433 PREFIX_DS,
c32fa91d
L
2434 PREFIX_OTHER
2435};
2436
2437/* Returns
2438 a. PREFIX_EXIST if attempting to add a prefix where one from the
2439 same class already exists.
2440 b. PREFIX_LOCK if lock prefix is added.
2441 c. PREFIX_REP if rep/repne prefix is added.
04ef582a
L
2442 d. PREFIX_DS if ds prefix is added.
2443 e. PREFIX_OTHER if other prefix is added.
c32fa91d
L
2444 */
2445
2446static enum PREFIX_GROUP
e3bb37b5 2447add_prefix (unsigned int prefix)
252b5132 2448{
c32fa91d 2449 enum PREFIX_GROUP ret = PREFIX_OTHER;
b1905489 2450 unsigned int q;
252b5132 2451
29b0f896
AM
2452 if (prefix >= REX_OPCODE && prefix < REX_OPCODE + 16
2453 && flag_code == CODE_64BIT)
b1905489 2454 {
161a04f6 2455 if ((i.prefix[REX_PREFIX] & prefix & REX_W)
44846f29
JB
2456 || (i.prefix[REX_PREFIX] & prefix & REX_R)
2457 || (i.prefix[REX_PREFIX] & prefix & REX_X)
2458 || (i.prefix[REX_PREFIX] & prefix & REX_B))
c32fa91d 2459 ret = PREFIX_EXIST;
b1905489
JB
2460 q = REX_PREFIX;
2461 }
3e73aa7c 2462 else
b1905489
JB
2463 {
2464 switch (prefix)
2465 {
2466 default:
2467 abort ();
2468
b1905489 2469 case DS_PREFIX_OPCODE:
04ef582a
L
2470 ret = PREFIX_DS;
2471 /* Fall through. */
2472 case CS_PREFIX_OPCODE:
b1905489
JB
2473 case ES_PREFIX_OPCODE:
2474 case FS_PREFIX_OPCODE:
2475 case GS_PREFIX_OPCODE:
2476 case SS_PREFIX_OPCODE:
2477 q = SEG_PREFIX;
2478 break;
2479
2480 case REPNE_PREFIX_OPCODE:
2481 case REPE_PREFIX_OPCODE:
c32fa91d
L
2482 q = REP_PREFIX;
2483 ret = PREFIX_REP;
2484 break;
2485
b1905489 2486 case LOCK_PREFIX_OPCODE:
c32fa91d
L
2487 q = LOCK_PREFIX;
2488 ret = PREFIX_LOCK;
b1905489
JB
2489 break;
2490
2491 case FWAIT_OPCODE:
2492 q = WAIT_PREFIX;
2493 break;
2494
2495 case ADDR_PREFIX_OPCODE:
2496 q = ADDR_PREFIX;
2497 break;
2498
2499 case DATA_PREFIX_OPCODE:
2500 q = DATA_PREFIX;
2501 break;
2502 }
2503 if (i.prefix[q] != 0)
c32fa91d 2504 ret = PREFIX_EXIST;
b1905489 2505 }
252b5132 2506
b1905489 2507 if (ret)
252b5132 2508 {
b1905489
JB
2509 if (!i.prefix[q])
2510 ++i.prefixes;
2511 i.prefix[q] |= prefix;
252b5132 2512 }
b1905489
JB
2513 else
2514 as_bad (_("same type of prefix used twice"));
252b5132 2515
252b5132
RH
2516 return ret;
2517}
2518
2519static void
78f12dd3 2520update_code_flag (int value, int check)
eecb386c 2521{
78f12dd3
L
2522 PRINTF_LIKE ((*as_error));
2523
1e9cc1c2 2524 flag_code = (enum flag_code) value;
40fb9820
L
2525 if (flag_code == CODE_64BIT)
2526 {
2527 cpu_arch_flags.bitfield.cpu64 = 1;
2528 cpu_arch_flags.bitfield.cpuno64 = 0;
40fb9820
L
2529 }
2530 else
2531 {
2532 cpu_arch_flags.bitfield.cpu64 = 0;
2533 cpu_arch_flags.bitfield.cpuno64 = 1;
40fb9820
L
2534 }
2535 if (value == CODE_64BIT && !cpu_arch_flags.bitfield.cpulm )
3e73aa7c 2536 {
78f12dd3
L
2537 if (check)
2538 as_error = as_fatal;
2539 else
2540 as_error = as_bad;
2541 (*as_error) (_("64bit mode not supported on `%s'."),
2542 cpu_arch_name ? cpu_arch_name : default_arch);
3e73aa7c 2543 }
40fb9820 2544 if (value == CODE_32BIT && !cpu_arch_flags.bitfield.cpui386)
3e73aa7c 2545 {
78f12dd3
L
2546 if (check)
2547 as_error = as_fatal;
2548 else
2549 as_error = as_bad;
2550 (*as_error) (_("32bit mode not supported on `%s'."),
2551 cpu_arch_name ? cpu_arch_name : default_arch);
3e73aa7c 2552 }
eecb386c
AM
2553 stackop_size = '\0';
2554}
2555
78f12dd3
L
2556static void
2557set_code_flag (int value)
2558{
2559 update_code_flag (value, 0);
2560}
2561
eecb386c 2562static void
e3bb37b5 2563set_16bit_gcc_code_flag (int new_code_flag)
252b5132 2564{
1e9cc1c2 2565 flag_code = (enum flag_code) new_code_flag;
40fb9820
L
2566 if (flag_code != CODE_16BIT)
2567 abort ();
2568 cpu_arch_flags.bitfield.cpu64 = 0;
2569 cpu_arch_flags.bitfield.cpuno64 = 1;
9306ca4a 2570 stackop_size = LONG_MNEM_SUFFIX;
252b5132
RH
2571}
2572
2573static void
e3bb37b5 2574set_intel_syntax (int syntax_flag)
252b5132
RH
2575{
2576 /* Find out if register prefixing is specified. */
2577 int ask_naked_reg = 0;
2578
2579 SKIP_WHITESPACE ();
29b0f896 2580 if (!is_end_of_line[(unsigned char) *input_line_pointer])
252b5132 2581 {
d02603dc
NC
2582 char *string;
2583 int e = get_symbol_name (&string);
252b5132 2584
47926f60 2585 if (strcmp (string, "prefix") == 0)
252b5132 2586 ask_naked_reg = 1;
47926f60 2587 else if (strcmp (string, "noprefix") == 0)
252b5132
RH
2588 ask_naked_reg = -1;
2589 else
d0b47220 2590 as_bad (_("bad argument to syntax directive."));
d02603dc 2591 (void) restore_line_pointer (e);
252b5132
RH
2592 }
2593 demand_empty_rest_of_line ();
c3332e24 2594
252b5132
RH
2595 intel_syntax = syntax_flag;
2596
2597 if (ask_naked_reg == 0)
f86103b7
AM
2598 allow_naked_reg = (intel_syntax
2599 && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
252b5132
RH
2600 else
2601 allow_naked_reg = (ask_naked_reg < 0);
9306ca4a 2602
ee86248c 2603 expr_set_rank (O_full_ptr, syntax_flag ? 10 : 0);
7ab9ffdd 2604
e4a3b5a4 2605 identifier_chars['%'] = intel_syntax && allow_naked_reg ? '%' : 0;
9306ca4a 2606 identifier_chars['$'] = intel_syntax ? '$' : 0;
e4a3b5a4 2607 register_prefix = allow_naked_reg ? "" : "%";
252b5132
RH
2608}
2609
1efbbeb4
L
2610static void
2611set_intel_mnemonic (int mnemonic_flag)
2612{
e1d4d893 2613 intel_mnemonic = mnemonic_flag;
1efbbeb4
L
2614}
2615
db51cc60
L
2616static void
2617set_allow_index_reg (int flag)
2618{
2619 allow_index_reg = flag;
2620}
2621
cb19c032 2622static void
7bab8ab5 2623set_check (int what)
cb19c032 2624{
7bab8ab5
JB
2625 enum check_kind *kind;
2626 const char *str;
2627
2628 if (what)
2629 {
2630 kind = &operand_check;
2631 str = "operand";
2632 }
2633 else
2634 {
2635 kind = &sse_check;
2636 str = "sse";
2637 }
2638
cb19c032
L
2639 SKIP_WHITESPACE ();
2640
2641 if (!is_end_of_line[(unsigned char) *input_line_pointer])
2642 {
d02603dc
NC
2643 char *string;
2644 int e = get_symbol_name (&string);
cb19c032
L
2645
2646 if (strcmp (string, "none") == 0)
7bab8ab5 2647 *kind = check_none;
cb19c032 2648 else if (strcmp (string, "warning") == 0)
7bab8ab5 2649 *kind = check_warning;
cb19c032 2650 else if (strcmp (string, "error") == 0)
7bab8ab5 2651 *kind = check_error;
cb19c032 2652 else
7bab8ab5 2653 as_bad (_("bad argument to %s_check directive."), str);
d02603dc 2654 (void) restore_line_pointer (e);
cb19c032
L
2655 }
2656 else
7bab8ab5 2657 as_bad (_("missing argument for %s_check directive"), str);
cb19c032
L
2658
2659 demand_empty_rest_of_line ();
2660}
2661
8a9036a4
L
2662static void
2663check_cpu_arch_compatible (const char *name ATTRIBUTE_UNUSED,
1e9cc1c2 2664 i386_cpu_flags new_flag ATTRIBUTE_UNUSED)
8a9036a4
L
2665{
2666#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2667 static const char *arch;
2668
c085ab00 2669 /* Intel MCU is only supported on ELF. */
8a9036a4
L
2670 if (!IS_ELF)
2671 return;
2672
2673 if (!arch)
2674 {
2675 /* Use cpu_arch_name if it is set in md_parse_option. Otherwise
2676 use default_arch. */
2677 arch = cpu_arch_name;
2678 if (!arch)
2679 arch = default_arch;
2680 }
2681
81486035 2682 /* If we are targeting Intel MCU, we must enable it. */
648d04db
JB
2683 if ((get_elf_backend_data (stdoutput)->elf_machine_code == EM_IAMCU)
2684 == new_flag.bitfield.cpuiamcu)
81486035
L
2685 return;
2686
8a9036a4
L
2687 as_bad (_("`%s' is not supported on `%s'"), name, arch);
2688#endif
2689}
2690
8180707f
JB
2691static void
2692extend_cpu_sub_arch_name (const char *name)
2693{
2694 if (cpu_sub_arch_name)
2695 cpu_sub_arch_name = reconcat (cpu_sub_arch_name, cpu_sub_arch_name,
ae89daec 2696 ".", name, (const char *) NULL);
8180707f 2697 else
ae89daec 2698 cpu_sub_arch_name = concat (".", name, (const char *) NULL);
8180707f
JB
2699}
2700
e413e4e9 2701static void
e3bb37b5 2702set_cpu_arch (int dummy ATTRIBUTE_UNUSED)
e413e4e9 2703{
f68697e8
JB
2704 typedef struct arch_stack_entry
2705 {
2706 const struct arch_stack_entry *prev;
2707 const char *name;
2708 char *sub_name;
2709 i386_cpu_flags flags;
2710 i386_cpu_flags isa_flags;
2711 enum processor_type isa;
2712 enum flag_code flag_code;
2713 char stackop_size;
2714 bool no_cond_jump_promotion;
2715 } arch_stack_entry;
2716 static const arch_stack_entry *arch_stack_top;
2717
47926f60 2718 SKIP_WHITESPACE ();
e413e4e9 2719
29b0f896 2720 if (!is_end_of_line[(unsigned char) *input_line_pointer])
e413e4e9 2721 {
3ce2ebcf
JB
2722 char *s;
2723 int e = get_symbol_name (&s);
2724 const char *string = s;
2725 unsigned int j = 0;
40fb9820 2726 i386_cpu_flags flags;
e413e4e9 2727
3ce2ebcf
JB
2728 if (strcmp (string, "default") == 0)
2729 {
2730 if (strcmp (default_arch, "iamcu") == 0)
2731 string = default_arch;
2732 else
2733 {
2734 static const i386_cpu_flags cpu_unknown_flags = CPU_UNKNOWN_FLAGS;
2735
2736 cpu_arch_name = NULL;
2737 free (cpu_sub_arch_name);
2738 cpu_sub_arch_name = NULL;
2739 cpu_arch_flags = cpu_unknown_flags;
2740 if (flag_code == CODE_64BIT)
2741 {
2742 cpu_arch_flags.bitfield.cpu64 = 1;
2743 cpu_arch_flags.bitfield.cpuno64 = 0;
2744 }
2745 else
2746 {
2747 cpu_arch_flags.bitfield.cpu64 = 0;
2748 cpu_arch_flags.bitfield.cpuno64 = 1;
2749 }
2750 cpu_arch_isa = PROCESSOR_UNKNOWN;
ae89daec 2751 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
3ce2ebcf
JB
2752 if (!cpu_arch_tune_set)
2753 {
2754 cpu_arch_tune = cpu_arch_isa;
2755 cpu_arch_tune_flags = cpu_arch_isa_flags;
2756 }
2757
2758 j = ARRAY_SIZE (cpu_arch) + 1;
2759 }
2760 }
f68697e8
JB
2761 else if (strcmp (string, "push") == 0)
2762 {
2763 arch_stack_entry *top = XNEW (arch_stack_entry);
2764
2765 top->name = cpu_arch_name;
2766 if (cpu_sub_arch_name)
2767 top->sub_name = xstrdup (cpu_sub_arch_name);
2768 else
2769 top->sub_name = NULL;
2770 top->flags = cpu_arch_flags;
2771 top->isa = cpu_arch_isa;
2772 top->isa_flags = cpu_arch_isa_flags;
2773 top->flag_code = flag_code;
2774 top->stackop_size = stackop_size;
2775 top->no_cond_jump_promotion = no_cond_jump_promotion;
2776
2777 top->prev = arch_stack_top;
2778 arch_stack_top = top;
2779
2780 (void) restore_line_pointer (e);
2781 demand_empty_rest_of_line ();
2782 return;
2783 }
2784 else if (strcmp (string, "pop") == 0)
2785 {
2786 const arch_stack_entry *top = arch_stack_top;
2787
2788 if (!top)
2789 as_bad (_(".arch stack is empty"));
2790 else if (top->flag_code != flag_code
2791 || top->stackop_size != stackop_size)
2792 {
2793 static const unsigned int bits[] = {
2794 [CODE_16BIT] = 16,
2795 [CODE_32BIT] = 32,
2796 [CODE_64BIT] = 64,
2797 };
2798
2799 as_bad (_("this `.arch pop' requires `.code%u%s' to be in effect"),
2800 bits[top->flag_code],
2801 top->stackop_size == LONG_MNEM_SUFFIX ? "gcc" : "");
2802 }
2803 else
2804 {
2805 arch_stack_top = top->prev;
2806
2807 cpu_arch_name = top->name;
2808 free (cpu_sub_arch_name);
2809 cpu_sub_arch_name = top->sub_name;
2810 cpu_arch_flags = top->flags;
2811 cpu_arch_isa = top->isa;
2812 cpu_arch_isa_flags = top->isa_flags;
2813 no_cond_jump_promotion = top->no_cond_jump_promotion;
2814
2815 XDELETE (top);
2816 }
2817
2818 (void) restore_line_pointer (e);
2819 demand_empty_rest_of_line ();
2820 return;
2821 }
3ce2ebcf
JB
2822
2823 for (; j < ARRAY_SIZE (cpu_arch); j++)
e413e4e9 2824 {
ae89daec
JB
2825 if (strcmp (string + (*string == '.'), cpu_arch[j].name) == 0
2826 && (*string == '.') == (cpu_arch[j].type == PROCESSOR_NONE))
e413e4e9 2827 {
5c6af06e
JB
2828 if (*string != '.')
2829 {
ae89daec 2830 check_cpu_arch_compatible (string, cpu_arch[j].enable);
648d04db 2831
91d6fa6a 2832 cpu_arch_name = cpu_arch[j].name;
d92c7521 2833 free (cpu_sub_arch_name);
5c6af06e 2834 cpu_sub_arch_name = NULL;
ae89daec 2835 cpu_arch_flags = cpu_arch[j].enable;
40fb9820
L
2836 if (flag_code == CODE_64BIT)
2837 {
2838 cpu_arch_flags.bitfield.cpu64 = 1;
2839 cpu_arch_flags.bitfield.cpuno64 = 0;
2840 }
2841 else
2842 {
2843 cpu_arch_flags.bitfield.cpu64 = 0;
2844 cpu_arch_flags.bitfield.cpuno64 = 1;
2845 }
91d6fa6a 2846 cpu_arch_isa = cpu_arch[j].type;
ae89daec 2847 cpu_arch_isa_flags = cpu_arch[j].enable;
ccc9c027
L
2848 if (!cpu_arch_tune_set)
2849 {
2850 cpu_arch_tune = cpu_arch_isa;
2851 cpu_arch_tune_flags = cpu_arch_isa_flags;
2852 }
d59a54c2 2853 pre_386_16bit_warned = false;
5c6af06e
JB
2854 break;
2855 }
40fb9820 2856
ae89daec
JB
2857 if (cpu_flags_all_zero (&cpu_arch[j].enable))
2858 continue;
2859
293f5f65 2860 flags = cpu_flags_or (cpu_arch_flags,
ae89daec 2861 cpu_arch[j].enable);
81486035 2862
5b64d091 2863 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
5c6af06e 2864 {
ae89daec 2865 extend_cpu_sub_arch_name (string + 1);
40fb9820 2866 cpu_arch_flags = flags;
a586129e 2867 cpu_arch_isa_flags = flags;
5c6af06e 2868 }
0089dace
L
2869 else
2870 cpu_arch_isa_flags
2871 = cpu_flags_or (cpu_arch_isa_flags,
ae89daec 2872 cpu_arch[j].enable);
d02603dc 2873 (void) restore_line_pointer (e);
5c6af06e
JB
2874 demand_empty_rest_of_line ();
2875 return;
e413e4e9
AM
2876 }
2877 }
293f5f65 2878
ae89daec 2879 if (startswith (string, ".no") && j >= ARRAY_SIZE (cpu_arch))
293f5f65 2880 {
33eaf5de 2881 /* Disable an ISA extension. */
ae89daec
JB
2882 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
2883 if (cpu_arch[j].type == PROCESSOR_NONE
2884 && strcmp (string + 3, cpu_arch[j].name) == 0)
293f5f65
L
2885 {
2886 flags = cpu_flags_and_not (cpu_arch_flags,
ae89daec 2887 cpu_arch[j].disable);
293f5f65
L
2888 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
2889 {
ae89daec 2890 extend_cpu_sub_arch_name (string + 1);
293f5f65
L
2891 cpu_arch_flags = flags;
2892 cpu_arch_isa_flags = flags;
2893 }
2894 (void) restore_line_pointer (e);
2895 demand_empty_rest_of_line ();
2896 return;
2897 }
293f5f65
L
2898 }
2899
3ce2ebcf 2900 if (j == ARRAY_SIZE (cpu_arch))
e413e4e9
AM
2901 as_bad (_("no such architecture: `%s'"), string);
2902
2903 *input_line_pointer = e;
2904 }
2905 else
2906 as_bad (_("missing cpu architecture"));
2907
fddf5b5b
AM
2908 no_cond_jump_promotion = 0;
2909 if (*input_line_pointer == ','
29b0f896 2910 && !is_end_of_line[(unsigned char) input_line_pointer[1]])
fddf5b5b 2911 {
d02603dc
NC
2912 char *string;
2913 char e;
2914
2915 ++input_line_pointer;
2916 e = get_symbol_name (&string);
fddf5b5b
AM
2917
2918 if (strcmp (string, "nojumps") == 0)
2919 no_cond_jump_promotion = 1;
2920 else if (strcmp (string, "jumps") == 0)
2921 ;
2922 else
2923 as_bad (_("no such architecture modifier: `%s'"), string);
2924
d02603dc 2925 (void) restore_line_pointer (e);
fddf5b5b
AM
2926 }
2927
e413e4e9
AM
2928 demand_empty_rest_of_line ();
2929}
2930
8a9036a4
L
2931enum bfd_architecture
2932i386_arch (void)
2933{
c085ab00 2934 if (cpu_arch_isa == PROCESSOR_IAMCU)
81486035
L
2935 {
2936 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2937 || flag_code == CODE_64BIT)
2938 as_fatal (_("Intel MCU is 32bit ELF only"));
2939 return bfd_arch_iamcu;
2940 }
8a9036a4
L
2941 else
2942 return bfd_arch_i386;
2943}
2944
b9d79e03 2945unsigned long
7016a5d5 2946i386_mach (void)
b9d79e03 2947{
d34049e8 2948 if (startswith (default_arch, "x86_64"))
8a9036a4 2949 {
c085ab00 2950 if (default_arch[6] == '\0')
8a9036a4 2951 return bfd_mach_x86_64;
351f65ca
L
2952 else
2953 return bfd_mach_x64_32;
8a9036a4 2954 }
5197d474
L
2955 else if (!strcmp (default_arch, "i386")
2956 || !strcmp (default_arch, "iamcu"))
81486035
L
2957 {
2958 if (cpu_arch_isa == PROCESSOR_IAMCU)
2959 {
2960 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
2961 as_fatal (_("Intel MCU is 32bit ELF only"));
2962 return bfd_mach_i386_iamcu;
2963 }
2964 else
2965 return bfd_mach_i386_i386;
2966 }
b9d79e03 2967 else
2b5d6a91 2968 as_fatal (_("unknown architecture"));
b9d79e03 2969}
b9d79e03 2970\f
99f0fb12
JB
2971#include "opcodes/i386-tbl.h"
2972
252b5132 2973void
7016a5d5 2974md_begin (void)
252b5132 2975{
86fa6981
L
2976 /* Support pseudo prefixes like {disp32}. */
2977 lex_type ['{'] = LEX_BEGIN_NAME;
2978
47926f60 2979 /* Initialize op_hash hash table. */
629310ab 2980 op_hash = str_htab_create ();
252b5132
RH
2981
2982 {
65f440c8
JB
2983 const insn_template *const *sets = i386_op_sets;
2984 const insn_template *const *end = sets + ARRAY_SIZE (i386_op_sets) - 1;
2985
2986 /* Type checks to compensate for the conversion through void * which
2987 occurs during hash table insertion / lookup. */
2988 (void)(sets == &current_templates->start);
2989 (void)(end == &current_templates->end);
2990 for (; sets < end; ++sets)
2991 if (str_hash_insert (op_hash, (*sets)->name, sets, 0))
2992 as_fatal (_("duplicate %s"), (*sets)->name);
252b5132
RH
2993 }
2994
47926f60 2995 /* Initialize reg_hash hash table. */
629310ab 2996 reg_hash = str_htab_create ();
252b5132 2997 {
29b0f896 2998 const reg_entry *regtab;
c3fe08fa 2999 unsigned int regtab_size = i386_regtab_size;
252b5132 3000
c3fe08fa 3001 for (regtab = i386_regtab; regtab_size--; regtab++)
6225c532 3002 {
6288d05f
JB
3003 switch (regtab->reg_type.bitfield.class)
3004 {
3005 case Reg:
34684862
JB
3006 if (regtab->reg_type.bitfield.dword)
3007 {
3008 if (regtab->reg_type.bitfield.instance == Accum)
3009 reg_eax = regtab;
3010 }
3011 else if (regtab->reg_type.bitfield.tbyte)
6288d05f
JB
3012 {
3013 /* There's no point inserting st(<N>) in the hash table, as
3014 parentheses aren't included in register_chars[] anyway. */
3015 if (regtab->reg_type.bitfield.instance != Accum)
3016 continue;
3017 reg_st0 = regtab;
3018 }
3019 break;
3020
5e042380
JB
3021 case SReg:
3022 switch (regtab->reg_num)
3023 {
3024 case 0: reg_es = regtab; break;
3025 case 2: reg_ss = regtab; break;
3026 case 3: reg_ds = regtab; break;
3027 }
3028 break;
3029
6288d05f
JB
3030 case RegMask:
3031 if (!regtab->reg_num)
3032 reg_k0 = regtab;
3033 break;
3034 }
3035
6225c532
JB
3036 if (str_hash_insert (reg_hash, regtab->reg_name, regtab, 0) != NULL)
3037 as_fatal (_("duplicate %s"), regtab->reg_name);
6225c532 3038 }
252b5132
RH
3039 }
3040
47926f60 3041 /* Fill in lexical tables: mnemonic_chars, operand_chars. */
252b5132 3042 {
29b0f896
AM
3043 int c;
3044 char *p;
252b5132
RH
3045
3046 for (c = 0; c < 256; c++)
3047 {
014fbcda 3048 if (ISDIGIT (c) || ISLOWER (c))
252b5132
RH
3049 {
3050 mnemonic_chars[c] = c;
3051 register_chars[c] = c;
3052 operand_chars[c] = c;
3053 }
3882b010 3054 else if (ISUPPER (c))
252b5132 3055 {
3882b010 3056 mnemonic_chars[c] = TOLOWER (c);
252b5132
RH
3057 register_chars[c] = mnemonic_chars[c];
3058 operand_chars[c] = c;
3059 }
43234a1e 3060 else if (c == '{' || c == '}')
86fa6981
L
3061 {
3062 mnemonic_chars[c] = c;
3063 operand_chars[c] = c;
3064 }
b3983e5f
JB
3065#ifdef SVR4_COMMENT_CHARS
3066 else if (c == '\\' && strchr (i386_comment_chars, '/'))
3067 operand_chars[c] = c;
3068#endif
252b5132 3069
3882b010 3070 if (ISALPHA (c) || ISDIGIT (c))
252b5132
RH
3071 identifier_chars[c] = c;
3072 else if (c >= 128)
3073 {
3074 identifier_chars[c] = c;
3075 operand_chars[c] = c;
3076 }
3077 }
3078
3079#ifdef LEX_AT
3080 identifier_chars['@'] = '@';
32137342
NC
3081#endif
3082#ifdef LEX_QM
3083 identifier_chars['?'] = '?';
3084 operand_chars['?'] = '?';
252b5132 3085#endif
c0f3af97 3086 mnemonic_chars['_'] = '_';
791fe849 3087 mnemonic_chars['-'] = '-';
0003779b 3088 mnemonic_chars['.'] = '.';
252b5132
RH
3089 identifier_chars['_'] = '_';
3090 identifier_chars['.'] = '.';
3091
3092 for (p = operand_special_chars; *p != '\0'; p++)
3093 operand_chars[(unsigned char) *p] = *p;
3094 }
3095
a4447b93
RH
3096 if (flag_code == CODE_64BIT)
3097 {
ca19b261
KT
3098#if defined (OBJ_COFF) && defined (TE_PE)
3099 x86_dwarf2_return_column = (OUTPUT_FLAVOR == bfd_target_coff_flavour
3100 ? 32 : 16);
3101#else
a4447b93 3102 x86_dwarf2_return_column = 16;
ca19b261 3103#endif
61ff971f 3104 x86_cie_data_alignment = -8;
b52c4ee4
IB
3105#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3106 x86_sframe_cfa_sp_reg = 7;
3107 x86_sframe_cfa_fp_reg = 6;
3108#endif
a4447b93
RH
3109 }
3110 else
3111 {
3112 x86_dwarf2_return_column = 8;
3113 x86_cie_data_alignment = -4;
3114 }
e379e5f3
L
3115
3116 /* NB: FUSED_JCC_PADDING frag must have sufficient room so that it
3117 can be turned into BRANCH_PREFIX frag. */
3118 if (align_branch_prefix_size > MAX_FUSED_JCC_PADDING_SIZE)
3119 abort ();
252b5132
RH
3120}
3121
3122void
e3bb37b5 3123i386_print_statistics (FILE *file)
252b5132 3124{
629310ab
ML
3125 htab_print_statistics (file, "i386 opcode", op_hash);
3126 htab_print_statistics (file, "i386 register", reg_hash);
252b5132 3127}
654d6f31
AM
3128
3129void
3130i386_md_end (void)
3131{
3132 htab_delete (op_hash);
3133 htab_delete (reg_hash);
3134}
252b5132 3135\f
252b5132
RH
3136#ifdef DEBUG386
3137
ce8a8b2f 3138/* Debugging routines for md_assemble. */
d3ce72d0 3139static void pte (insn_template *);
40fb9820 3140static void pt (i386_operand_type);
e3bb37b5
L
3141static void pe (expressionS *);
3142static void ps (symbolS *);
252b5132
RH
3143
3144static void
2c703856 3145pi (const char *line, i386_insn *x)
252b5132 3146{
09137c09 3147 unsigned int j;
252b5132
RH
3148
3149 fprintf (stdout, "%s: template ", line);
3150 pte (&x->tm);
09f131f2
JH
3151 fprintf (stdout, " address: base %s index %s scale %x\n",
3152 x->base_reg ? x->base_reg->reg_name : "none",
3153 x->index_reg ? x->index_reg->reg_name : "none",
3154 x->log2_scale_factor);
3155 fprintf (stdout, " modrm: mode %x reg %x reg/mem %x\n",
252b5132 3156 x->rm.mode, x->rm.reg, x->rm.regmem);
09f131f2
JH
3157 fprintf (stdout, " sib: base %x index %x scale %x\n",
3158 x->sib.base, x->sib.index, x->sib.scale);
3159 fprintf (stdout, " rex: 64bit %x extX %x extY %x extZ %x\n",
161a04f6
L
3160 (x->rex & REX_W) != 0,
3161 (x->rex & REX_R) != 0,
3162 (x->rex & REX_X) != 0,
3163 (x->rex & REX_B) != 0);
09137c09 3164 for (j = 0; j < x->operands; j++)
252b5132 3165 {
09137c09
SP
3166 fprintf (stdout, " #%d: ", j + 1);
3167 pt (x->types[j]);
252b5132 3168 fprintf (stdout, "\n");
bab6aec1 3169 if (x->types[j].bitfield.class == Reg
3528c362
JB
3170 || x->types[j].bitfield.class == RegMMX
3171 || x->types[j].bitfield.class == RegSIMD
dd6b8a0b 3172 || x->types[j].bitfield.class == RegMask
00cee14f 3173 || x->types[j].bitfield.class == SReg
4a5c67ed
JB
3174 || x->types[j].bitfield.class == RegCR
3175 || x->types[j].bitfield.class == RegDR
dd6b8a0b
JB
3176 || x->types[j].bitfield.class == RegTR
3177 || x->types[j].bitfield.class == RegBND)
09137c09
SP
3178 fprintf (stdout, "%s\n", x->op[j].regs->reg_name);
3179 if (operand_type_check (x->types[j], imm))
3180 pe (x->op[j].imms);
3181 if (operand_type_check (x->types[j], disp))
3182 pe (x->op[j].disps);
252b5132
RH
3183 }
3184}
3185
3186static void
d3ce72d0 3187pte (insn_template *t)
252b5132 3188{
b933fa4b 3189 static const unsigned char opc_pfx[] = { 0, 0x66, 0xf3, 0xf2 };
441f6aca 3190 static const char *const opc_spc[] = {
0cc78721 3191 NULL, "0f", "0f38", "0f3a", NULL, "evexmap5", "evexmap6", NULL,
441f6aca
JB
3192 "XOP08", "XOP09", "XOP0A",
3193 };
09137c09 3194 unsigned int j;
441f6aca 3195
252b5132 3196 fprintf (stdout, " %d operands ", t->operands);
441f6aca
JB
3197 if (opc_pfx[t->opcode_modifier.opcodeprefix])
3198 fprintf (stdout, "pfx %x ", opc_pfx[t->opcode_modifier.opcodeprefix]);
3199 if (opc_spc[t->opcode_modifier.opcodespace])
3200 fprintf (stdout, "space %s ", opc_spc[t->opcode_modifier.opcodespace]);
47926f60 3201 fprintf (stdout, "opcode %x ", t->base_opcode);
252b5132
RH
3202 if (t->extension_opcode != None)
3203 fprintf (stdout, "ext %x ", t->extension_opcode);
40fb9820 3204 if (t->opcode_modifier.d)
252b5132 3205 fprintf (stdout, "D");
40fb9820 3206 if (t->opcode_modifier.w)
252b5132
RH
3207 fprintf (stdout, "W");
3208 fprintf (stdout, "\n");
09137c09 3209 for (j = 0; j < t->operands; j++)
252b5132 3210 {
09137c09
SP
3211 fprintf (stdout, " #%d type ", j + 1);
3212 pt (t->operand_types[j]);
252b5132
RH
3213 fprintf (stdout, "\n");
3214 }
3215}
3216
3217static void
e3bb37b5 3218pe (expressionS *e)
252b5132 3219{
24eab124 3220 fprintf (stdout, " operation %d\n", e->X_op);
b8281767
AM
3221 fprintf (stdout, " add_number %" PRId64 " (%" PRIx64 ")\n",
3222 (int64_t) e->X_add_number, (uint64_t) (valueT) e->X_add_number);
252b5132
RH
3223 if (e->X_add_symbol)
3224 {
3225 fprintf (stdout, " add_symbol ");
3226 ps (e->X_add_symbol);
3227 fprintf (stdout, "\n");
3228 }
3229 if (e->X_op_symbol)
3230 {
3231 fprintf (stdout, " op_symbol ");
3232 ps (e->X_op_symbol);
3233 fprintf (stdout, "\n");
3234 }
3235}
3236
3237static void
e3bb37b5 3238ps (symbolS *s)
252b5132
RH
3239{
3240 fprintf (stdout, "%s type %s%s",
3241 S_GET_NAME (s),
3242 S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
3243 segment_name (S_GET_SEGMENT (s)));
3244}
3245
7b81dfbb 3246static struct type_name
252b5132 3247 {
40fb9820
L
3248 i386_operand_type mask;
3249 const char *name;
252b5132 3250 }
7b81dfbb 3251const type_names[] =
252b5132 3252{
05909f23
JB
3253 { { .bitfield = { .class = Reg, .byte = 1 } }, "r8" },
3254 { { .bitfield = { .class = Reg, .word = 1 } }, "r16" },
3255 { { .bitfield = { .class = Reg, .dword = 1 } }, "r32" },
3256 { { .bitfield = { .class = Reg, .qword = 1 } }, "r64" },
3257 { { .bitfield = { .instance = Accum, .byte = 1 } }, "acc8" },
3258 { { .bitfield = { .instance = Accum, .word = 1 } }, "acc16" },
3259 { { .bitfield = { .instance = Accum, .dword = 1 } }, "acc32" },
3260 { { .bitfield = { .instance = Accum, .qword = 1 } }, "acc64" },
3261 { { .bitfield = { .imm8 = 1 } }, "i8" },
3262 { { .bitfield = { .imm8s = 1 } }, "i8s" },
3263 { { .bitfield = { .imm16 = 1 } }, "i16" },
3264 { { .bitfield = { .imm32 = 1 } }, "i32" },
3265 { { .bitfield = { .imm32s = 1 } }, "i32s" },
3266 { { .bitfield = { .imm64 = 1 } }, "i64" },
3267 { { .bitfield = { .imm1 = 1 } }, "i1" },
3268 { { .bitfield = { .baseindex = 1 } }, "BaseIndex" },
3269 { { .bitfield = { .disp8 = 1 } }, "d8" },
3270 { { .bitfield = { .disp16 = 1 } }, "d16" },
3271 { { .bitfield = { .disp32 = 1 } }, "d32" },
3272 { { .bitfield = { .disp64 = 1 } }, "d64" },
3273 { { .bitfield = { .instance = RegD, .word = 1 } }, "InOutPortReg" },
3274 { { .bitfield = { .instance = RegC, .byte = 1 } }, "ShiftCount" },
3275 { { .bitfield = { .class = RegCR } }, "control reg" },
3276 { { .bitfield = { .class = RegTR } }, "test reg" },
3277 { { .bitfield = { .class = RegDR } }, "debug reg" },
3278 { { .bitfield = { .class = Reg, .tbyte = 1 } }, "FReg" },
3279 { { .bitfield = { .instance = Accum, .tbyte = 1 } }, "FAcc" },
3280 { { .bitfield = { .class = SReg } }, "SReg" },
3281 { { .bitfield = { .class = RegMMX } }, "rMMX" },
3282 { { .bitfield = { .class = RegSIMD, .xmmword = 1 } }, "rXMM" },
3283 { { .bitfield = { .class = RegSIMD, .ymmword = 1 } }, "rYMM" },
3284 { { .bitfield = { .class = RegSIMD, .zmmword = 1 } }, "rZMM" },
3285 { { .bitfield = { .class = RegSIMD, .tmmword = 1 } }, "rTMM" },
3286 { { .bitfield = { .class = RegMask } }, "Mask reg" },
252b5132
RH
3287};
3288
3289static void
40fb9820 3290pt (i386_operand_type t)
252b5132 3291{
40fb9820 3292 unsigned int j;
c6fb90c8 3293 i386_operand_type a;
252b5132 3294
40fb9820 3295 for (j = 0; j < ARRAY_SIZE (type_names); j++)
c6fb90c8
L
3296 {
3297 a = operand_type_and (t, type_names[j].mask);
2c703856 3298 if (operand_type_equal (&a, &type_names[j].mask))
c6fb90c8
L
3299 fprintf (stdout, "%s, ", type_names[j].name);
3300 }
252b5132
RH
3301 fflush (stdout);
3302}
3303
3304#endif /* DEBUG386 */
3305\f
252b5132 3306static bfd_reloc_code_real_type
3956db08 3307reloc (unsigned int size,
64e74474
AM
3308 int pcrel,
3309 int sign,
3310 bfd_reloc_code_real_type other)
252b5132 3311{
47926f60 3312 if (other != NO_RELOC)
3956db08 3313 {
91d6fa6a 3314 reloc_howto_type *rel;
3956db08
JB
3315
3316 if (size == 8)
3317 switch (other)
3318 {
64e74474
AM
3319 case BFD_RELOC_X86_64_GOT32:
3320 return BFD_RELOC_X86_64_GOT64;
3321 break;
553d1284
L
3322 case BFD_RELOC_X86_64_GOTPLT64:
3323 return BFD_RELOC_X86_64_GOTPLT64;
3324 break;
64e74474
AM
3325 case BFD_RELOC_X86_64_PLTOFF64:
3326 return BFD_RELOC_X86_64_PLTOFF64;
3327 break;
3328 case BFD_RELOC_X86_64_GOTPC32:
3329 other = BFD_RELOC_X86_64_GOTPC64;
3330 break;
3331 case BFD_RELOC_X86_64_GOTPCREL:
3332 other = BFD_RELOC_X86_64_GOTPCREL64;
3333 break;
3334 case BFD_RELOC_X86_64_TPOFF32:
3335 other = BFD_RELOC_X86_64_TPOFF64;
3336 break;
3337 case BFD_RELOC_X86_64_DTPOFF32:
3338 other = BFD_RELOC_X86_64_DTPOFF64;
3339 break;
3340 default:
3341 break;
3956db08 3342 }
e05278af 3343
8ce3d284 3344#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
3345 if (other == BFD_RELOC_SIZE32)
3346 {
3347 if (size == 8)
1ab668bf 3348 other = BFD_RELOC_SIZE64;
8fd4256d 3349 if (pcrel)
1ab668bf
AM
3350 {
3351 as_bad (_("there are no pc-relative size relocations"));
3352 return NO_RELOC;
3353 }
8fd4256d 3354 }
8ce3d284 3355#endif
8fd4256d 3356
e05278af 3357 /* Sign-checking 4-byte relocations in 16-/32-bit code is pointless. */
f2d8a97c 3358 if (size == 4 && (flag_code != CODE_64BIT || disallow_64bit_reloc))
e05278af
JB
3359 sign = -1;
3360
91d6fa6a
NC
3361 rel = bfd_reloc_type_lookup (stdoutput, other);
3362 if (!rel)
3956db08 3363 as_bad (_("unknown relocation (%u)"), other);
91d6fa6a 3364 else if (size != bfd_get_reloc_size (rel))
3956db08 3365 as_bad (_("%u-byte relocation cannot be applied to %u-byte field"),
91d6fa6a 3366 bfd_get_reloc_size (rel),
3956db08 3367 size);
91d6fa6a 3368 else if (pcrel && !rel->pc_relative)
3956db08 3369 as_bad (_("non-pc-relative relocation for pc-relative field"));
91d6fa6a 3370 else if ((rel->complain_on_overflow == complain_overflow_signed
3956db08 3371 && !sign)
91d6fa6a 3372 || (rel->complain_on_overflow == complain_overflow_unsigned
64e74474 3373 && sign > 0))
3956db08
JB
3374 as_bad (_("relocated field and relocation type differ in signedness"));
3375 else
3376 return other;
3377 return NO_RELOC;
3378 }
252b5132
RH
3379
3380 if (pcrel)
3381 {
3e73aa7c 3382 if (!sign)
3956db08 3383 as_bad (_("there are no unsigned pc-relative relocations"));
252b5132
RH
3384 switch (size)
3385 {
3386 case 1: return BFD_RELOC_8_PCREL;
3387 case 2: return BFD_RELOC_16_PCREL;
d258b828 3388 case 4: return BFD_RELOC_32_PCREL;
d6ab8113 3389 case 8: return BFD_RELOC_64_PCREL;
252b5132 3390 }
3956db08 3391 as_bad (_("cannot do %u byte pc-relative relocation"), size);
252b5132
RH
3392 }
3393 else
3394 {
3956db08 3395 if (sign > 0)
e5cb08ac 3396 switch (size)
3e73aa7c
JH
3397 {
3398 case 4: return BFD_RELOC_X86_64_32S;
3399 }
3400 else
3401 switch (size)
3402 {
3403 case 1: return BFD_RELOC_8;
3404 case 2: return BFD_RELOC_16;
3405 case 4: return BFD_RELOC_32;
3406 case 8: return BFD_RELOC_64;
3407 }
3956db08
JB
3408 as_bad (_("cannot do %s %u byte relocation"),
3409 sign > 0 ? "signed" : "unsigned", size);
252b5132
RH
3410 }
3411
0cc9e1d3 3412 return NO_RELOC;
252b5132
RH
3413}
3414
47926f60
KH
3415/* Here we decide which fixups can be adjusted to make them relative to
3416 the beginning of the section instead of the symbol. Basically we need
3417 to make sure that the dynamic relocations are done correctly, so in
3418 some cases we force the original symbol to be used. */
3419
252b5132 3420int
e3bb37b5 3421tc_i386_fix_adjustable (fixS *fixP ATTRIBUTE_UNUSED)
252b5132 3422{
6d249963 3423#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 3424 if (!IS_ELF)
31312f95
AM
3425 return 1;
3426
a161fe53
AM
3427 /* Don't adjust pc-relative references to merge sections in 64-bit
3428 mode. */
3429 if (use_rela_relocations
3430 && (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0
3431 && fixP->fx_pcrel)
252b5132 3432 return 0;
31312f95 3433
8d01d9a9
AJ
3434 /* The x86_64 GOTPCREL are represented as 32bit PCrel relocations
3435 and changed later by validate_fix. */
3436 if (GOT_symbol && fixP->fx_subsy == GOT_symbol
3437 && fixP->fx_r_type == BFD_RELOC_32_PCREL)
3438 return 0;
3439
8fd4256d
L
3440 /* Adjust_reloc_syms doesn't know about the GOT. Need to keep symbol
3441 for size relocations. */
3442 if (fixP->fx_r_type == BFD_RELOC_SIZE32
3443 || fixP->fx_r_type == BFD_RELOC_SIZE64
3444 || fixP->fx_r_type == BFD_RELOC_386_GOTOFF
252b5132 3445 || fixP->fx_r_type == BFD_RELOC_386_GOT32
02a86693 3446 || fixP->fx_r_type == BFD_RELOC_386_GOT32X
13ae64f3
JJ
3447 || fixP->fx_r_type == BFD_RELOC_386_TLS_GD
3448 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDM
3449 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDO_32
3450 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE_32
37e55690
JJ
3451 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE
3452 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTIE
13ae64f3
JJ
3453 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE_32
3454 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE
67a4f2b7
AO
3455 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTDESC
3456 || fixP->fx_r_type == BFD_RELOC_386_TLS_DESC_CALL
3e73aa7c 3457 || fixP->fx_r_type == BFD_RELOC_X86_64_GOT32
80b3ee89 3458 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCREL
56ceb5b5
L
3459 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCRELX
3460 || fixP->fx_r_type == BFD_RELOC_X86_64_REX_GOTPCRELX
bffbf940
JJ
3461 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSGD
3462 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSLD
3463 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF32
d6ab8113 3464 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF64
bffbf940
JJ
3465 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF
3466 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF32
d6ab8113
JB
3467 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF64
3468 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTOFF64
67a4f2b7
AO
3469 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
3470 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSDESC_CALL
252b5132
RH
3471 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3472 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3473 return 0;
31312f95 3474#endif
252b5132
RH
3475 return 1;
3476}
252b5132 3477
a9aabc23
JB
3478static INLINE bool
3479want_disp32 (const insn_template *t)
3480{
3481 return flag_code != CODE_64BIT
3482 || i.prefix[ADDR_PREFIX]
3483 || (t->base_opcode == 0x8d
3484 && t->opcode_modifier.opcodespace == SPACE_BASE
fe134c65
JB
3485 && (!i.types[1].bitfield.qword
3486 || t->opcode_modifier.size == SIZE32));
a9aabc23
JB
3487}
3488
b4cac588 3489static int
e3bb37b5 3490intel_float_operand (const char *mnemonic)
252b5132 3491{
9306ca4a
JB
3492 /* Note that the value returned is meaningful only for opcodes with (memory)
3493 operands, hence the code here is free to improperly handle opcodes that
3494 have no operands (for better performance and smaller code). */
3495
3496 if (mnemonic[0] != 'f')
3497 return 0; /* non-math */
3498
3499 switch (mnemonic[1])
3500 {
3501 /* fclex, fdecstp, fdisi, femms, feni, fincstp, finit, fsetpm, and
3502 the fs segment override prefix not currently handled because no
3503 call path can make opcodes without operands get here */
3504 case 'i':
3505 return 2 /* integer op */;
3506 case 'l':
3507 if (mnemonic[2] == 'd' && (mnemonic[3] == 'c' || mnemonic[3] == 'e'))
3508 return 3; /* fldcw/fldenv */
3509 break;
3510 case 'n':
3511 if (mnemonic[2] != 'o' /* fnop */)
3512 return 3; /* non-waiting control op */
3513 break;
3514 case 'r':
3515 if (mnemonic[2] == 's')
3516 return 3; /* frstor/frstpm */
3517 break;
3518 case 's':
3519 if (mnemonic[2] == 'a')
3520 return 3; /* fsave */
3521 if (mnemonic[2] == 't')
3522 {
3523 switch (mnemonic[3])
3524 {
3525 case 'c': /* fstcw */
3526 case 'd': /* fstdw */
3527 case 'e': /* fstenv */
3528 case 's': /* fsts[gw] */
3529 return 3;
3530 }
3531 }
3532 break;
3533 case 'x':
3534 if (mnemonic[2] == 'r' || mnemonic[2] == 's')
3535 return 0; /* fxsave/fxrstor are not really math ops */
3536 break;
3537 }
252b5132 3538
9306ca4a 3539 return 1;
252b5132
RH
3540}
3541
9a182d04
JB
3542static INLINE void
3543install_template (const insn_template *t)
3544{
3545 unsigned int l;
3546
3547 i.tm = *t;
3548
3549 /* Note that for pseudo prefixes this produces a length of 1. But for them
3550 the length isn't interesting at all. */
3551 for (l = 1; l < 4; ++l)
3552 if (!(t->base_opcode >> (8 * l)))
3553 break;
3554
3555 i.opcode_length = l;
3556}
3557
c0f3af97
L
3558/* Build the VEX prefix. */
3559
3560static void
d3ce72d0 3561build_vex_prefix (const insn_template *t)
c0f3af97
L
3562{
3563 unsigned int register_specifier;
c0f3af97 3564 unsigned int vector_length;
03751133 3565 unsigned int w;
c0f3af97
L
3566
3567 /* Check register specifier. */
3568 if (i.vex.register_specifier)
43234a1e
L
3569 {
3570 register_specifier =
3571 ~register_number (i.vex.register_specifier) & 0xf;
3572 gas_assert ((i.vex.register_specifier->reg_flags & RegVRex) == 0);
3573 }
c0f3af97
L
3574 else
3575 register_specifier = 0xf;
3576
79f0fa25
L
3577 /* Use 2-byte VEX prefix by swapping destination and source operand
3578 if there are more than 1 register operand. */
3579 if (i.reg_operands > 1
3580 && i.vec_encoding != vex_encoding_vex3
86fa6981 3581 && i.dir_encoding == dir_encoding_default
fa99fab2 3582 && i.operands == i.reg_operands
dbbc8b7e 3583 && operand_type_equal (&i.types[0], &i.types[i.operands - 1])
441f6aca 3584 && i.tm.opcode_modifier.opcodespace == SPACE_0F
dbbc8b7e 3585 && (i.tm.opcode_modifier.load || i.tm.opcode_modifier.d)
fa99fab2
L
3586 && i.rex == REX_B)
3587 {
3588 unsigned int xchg = i.operands - 1;
3589 union i386_op temp_op;
3590 i386_operand_type temp_type;
3591
3592 temp_type = i.types[xchg];
3593 i.types[xchg] = i.types[0];
3594 i.types[0] = temp_type;
3595 temp_op = i.op[xchg];
3596 i.op[xchg] = i.op[0];
3597 i.op[0] = temp_op;
3598
9c2799c2 3599 gas_assert (i.rm.mode == 3);
fa99fab2
L
3600
3601 i.rex = REX_R;
3602 xchg = i.rm.regmem;
3603 i.rm.regmem = i.rm.reg;
3604 i.rm.reg = xchg;
3605
dbbc8b7e
JB
3606 if (i.tm.opcode_modifier.d)
3607 i.tm.base_opcode ^= (i.tm.base_opcode & 0xee) != 0x6e
2c735193 3608 ? Opcode_ExtD : Opcode_SIMD_IntD;
dbbc8b7e 3609 else /* Use the next insn. */
9a182d04 3610 install_template (&t[1]);
fa99fab2
L
3611 }
3612
79dec6b7
JB
3613 /* Use 2-byte VEX prefix by swapping commutative source operands if there
3614 are no memory operands and at least 3 register ones. */
3615 if (i.reg_operands >= 3
3616 && i.vec_encoding != vex_encoding_vex3
3617 && i.reg_operands == i.operands - i.imm_operands
3618 && i.tm.opcode_modifier.vex
3619 && i.tm.opcode_modifier.commutative
3620 && (i.tm.opcode_modifier.sse2avx || optimize > 1)
3621 && i.rex == REX_B
3622 && i.vex.register_specifier
3623 && !(i.vex.register_specifier->reg_flags & RegRex))
3624 {
3625 unsigned int xchg = i.operands - i.reg_operands;
3626 union i386_op temp_op;
3627 i386_operand_type temp_type;
3628
441f6aca 3629 gas_assert (i.tm.opcode_modifier.opcodespace == SPACE_0F);
79dec6b7
JB
3630 gas_assert (!i.tm.opcode_modifier.sae);
3631 gas_assert (operand_type_equal (&i.types[i.operands - 2],
3632 &i.types[i.operands - 3]));
3633 gas_assert (i.rm.mode == 3);
3634
3635 temp_type = i.types[xchg];
3636 i.types[xchg] = i.types[xchg + 1];
3637 i.types[xchg + 1] = temp_type;
3638 temp_op = i.op[xchg];
3639 i.op[xchg] = i.op[xchg + 1];
3640 i.op[xchg + 1] = temp_op;
3641
3642 i.rex = 0;
3643 xchg = i.rm.regmem | 8;
3644 i.rm.regmem = ~register_specifier & 0xf;
3645 gas_assert (!(i.rm.regmem & 8));
3646 i.vex.register_specifier += xchg - i.rm.regmem;
3647 register_specifier = ~xchg & 0xf;
3648 }
3649
539f890d
L
3650 if (i.tm.opcode_modifier.vex == VEXScalar)
3651 vector_length = avxscalar;
10c17abd
JB
3652 else if (i.tm.opcode_modifier.vex == VEX256)
3653 vector_length = 1;
539f890d 3654 else
10c17abd 3655 {
56522fc5 3656 unsigned int op;
10c17abd 3657
c7213af9
L
3658 /* Determine vector length from the last multi-length vector
3659 operand. */
10c17abd 3660 vector_length = 0;
56522fc5 3661 for (op = t->operands; op--;)
10c17abd
JB
3662 if (t->operand_types[op].bitfield.xmmword
3663 && t->operand_types[op].bitfield.ymmword
3664 && i.types[op].bitfield.ymmword)
3665 {
3666 vector_length = 1;
3667 break;
3668 }
3669 }
c0f3af97 3670
03751133
L
3671 /* Check the REX.W bit and VEXW. */
3672 if (i.tm.opcode_modifier.vexw == VEXWIG)
3673 w = (vexwig == vexw1 || (i.rex & REX_W)) ? 1 : 0;
3674 else if (i.tm.opcode_modifier.vexw)
3675 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3676 else
931d03b7 3677 w = (flag_code == CODE_64BIT ? i.rex & REX_W : vexwig == vexw1) ? 1 : 0;
03751133 3678
c0f3af97 3679 /* Use 2-byte VEX prefix if possible. */
03751133
L
3680 if (w == 0
3681 && i.vec_encoding != vex_encoding_vex3
441f6aca 3682 && i.tm.opcode_modifier.opcodespace == SPACE_0F
c0f3af97
L
3683 && (i.rex & (REX_W | REX_X | REX_B)) == 0)
3684 {
3685 /* 2-byte VEX prefix. */
3686 unsigned int r;
3687
3688 i.vex.length = 2;
3689 i.vex.bytes[0] = 0xc5;
3690
3691 /* Check the REX.R bit. */
3692 r = (i.rex & REX_R) ? 0 : 1;
3693 i.vex.bytes[1] = (r << 7
3694 | register_specifier << 3
3695 | vector_length << 2
35648716 3696 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3697 }
3698 else
3699 {
3700 /* 3-byte VEX prefix. */
f88c9eb0 3701 i.vex.length = 3;
f88c9eb0 3702
441f6aca 3703 switch (i.tm.opcode_modifier.opcodespace)
5dd85c99 3704 {
441f6aca
JB
3705 case SPACE_0F:
3706 case SPACE_0F38:
3707 case SPACE_0F3A:
80de6e00 3708 i.vex.bytes[0] = 0xc4;
7f399153 3709 break;
441f6aca
JB
3710 case SPACE_XOP08:
3711 case SPACE_XOP09:
3712 case SPACE_XOP0A:
f88c9eb0 3713 i.vex.bytes[0] = 0x8f;
7f399153
L
3714 break;
3715 default:
3716 abort ();
f88c9eb0 3717 }
c0f3af97 3718
c0f3af97
L
3719 /* The high 3 bits of the second VEX byte are 1's compliment
3720 of RXB bits from REX. */
441f6aca 3721 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | i.tm.opcode_modifier.opcodespace;
c0f3af97 3722
c0f3af97
L
3723 i.vex.bytes[2] = (w << 7
3724 | register_specifier << 3
3725 | vector_length << 2
35648716 3726 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3727 }
3728}
3729
5b7c81bd 3730static INLINE bool
e771e7c9
JB
3731is_evex_encoding (const insn_template *t)
3732{
7091c612 3733 return t->opcode_modifier.evex || t->opcode_modifier.disp8memshift
e771e7c9 3734 || t->opcode_modifier.broadcast || t->opcode_modifier.masking
a80195f1 3735 || t->opcode_modifier.sae;
e771e7c9
JB
3736}
3737
5b7c81bd 3738static INLINE bool
7a8655d2
JB
3739is_any_vex_encoding (const insn_template *t)
3740{
7b47a312 3741 return t->opcode_modifier.vex || is_evex_encoding (t);
7a8655d2
JB
3742}
3743
a5748e0d
JB
3744static unsigned int
3745get_broadcast_bytes (const insn_template *t, bool diag)
3746{
3747 unsigned int op, bytes;
3748 const i386_operand_type *types;
3749
3750 if (i.broadcast.type)
3751 return i.broadcast.bytes = ((1 << (t->opcode_modifier.broadcast - 1))
3752 * i.broadcast.type);
3753
3754 gas_assert (intel_syntax);
3755
3756 for (op = 0; op < t->operands; ++op)
3757 if (t->operand_types[op].bitfield.baseindex)
3758 break;
3759
3760 gas_assert (op < t->operands);
3761
3762 if (t->opcode_modifier.evex
3763 && t->opcode_modifier.evex != EVEXDYN)
3764 switch (i.broadcast.bytes)
3765 {
3766 case 1:
3767 if (t->operand_types[op].bitfield.word)
3768 return 2;
3769 /* Fall through. */
3770 case 2:
3771 if (t->operand_types[op].bitfield.dword)
3772 return 4;
3773 /* Fall through. */
3774 case 4:
3775 if (t->operand_types[op].bitfield.qword)
3776 return 8;
3777 /* Fall through. */
3778 case 8:
3779 if (t->operand_types[op].bitfield.xmmword)
3780 return 16;
3781 if (t->operand_types[op].bitfield.ymmword)
3782 return 32;
3783 if (t->operand_types[op].bitfield.zmmword)
3784 return 64;
3785 /* Fall through. */
3786 default:
3787 abort ();
3788 }
3789
3790 gas_assert (op + 1 < t->operands);
3791
3792 if (t->operand_types[op + 1].bitfield.xmmword
3793 + t->operand_types[op + 1].bitfield.ymmword
3794 + t->operand_types[op + 1].bitfield.zmmword > 1)
3795 {
3796 types = &i.types[op + 1];
3797 diag = false;
3798 }
3799 else /* Ambiguous - guess with a preference to non-AVX512VL forms. */
3800 types = &t->operand_types[op];
3801
3802 if (types->bitfield.zmmword)
3803 bytes = 64;
3804 else if (types->bitfield.ymmword)
3805 bytes = 32;
3806 else
3807 bytes = 16;
3808
3809 if (diag)
3810 as_warn (_("ambiguous broadcast for `%s', using %u-bit form"),
3811 t->name, bytes * 8);
3812
3813 return bytes;
3814}
3815
43234a1e
L
3816/* Build the EVEX prefix. */
3817
3818static void
3819build_evex_prefix (void)
3820{
35648716 3821 unsigned int register_specifier, w;
43234a1e
L
3822 rex_byte vrex_used = 0;
3823
3824 /* Check register specifier. */
3825 if (i.vex.register_specifier)
3826 {
3827 gas_assert ((i.vrex & REX_X) == 0);
3828
3829 register_specifier = i.vex.register_specifier->reg_num;
3830 if ((i.vex.register_specifier->reg_flags & RegRex))
3831 register_specifier += 8;
3832 /* The upper 16 registers are encoded in the fourth byte of the
3833 EVEX prefix. */
3834 if (!(i.vex.register_specifier->reg_flags & RegVRex))
3835 i.vex.bytes[3] = 0x8;
3836 register_specifier = ~register_specifier & 0xf;
3837 }
3838 else
3839 {
3840 register_specifier = 0xf;
3841
3842 /* Encode upper 16 vector index register in the fourth byte of
3843 the EVEX prefix. */
3844 if (!(i.vrex & REX_X))
3845 i.vex.bytes[3] = 0x8;
3846 else
3847 vrex_used |= REX_X;
3848 }
3849
43234a1e
L
3850 /* 4 byte EVEX prefix. */
3851 i.vex.length = 4;
3852 i.vex.bytes[0] = 0x62;
3853
43234a1e
L
3854 /* The high 3 bits of the second EVEX byte are 1's compliment of RXB
3855 bits from REX. */
441f6aca 3856 gas_assert (i.tm.opcode_modifier.opcodespace >= SPACE_0F);
0cc78721 3857 gas_assert (i.tm.opcode_modifier.opcodespace <= SPACE_EVEXMAP6);
441f6aca 3858 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | i.tm.opcode_modifier.opcodespace;
43234a1e
L
3859
3860 /* The fifth bit of the second EVEX byte is 1's compliment of the
3861 REX_R bit in VREX. */
3862 if (!(i.vrex & REX_R))
3863 i.vex.bytes[1] |= 0x10;
3864 else
3865 vrex_used |= REX_R;
3866
3867 if ((i.reg_operands + i.imm_operands) == i.operands)
3868 {
3869 /* When all operands are registers, the REX_X bit in REX is not
3870 used. We reuse it to encode the upper 16 registers, which is
3871 indicated by the REX_B bit in VREX. The REX_X bit is encoded
3872 as 1's compliment. */
3873 if ((i.vrex & REX_B))
3874 {
3875 vrex_used |= REX_B;
3876 i.vex.bytes[1] &= ~0x40;
3877 }
3878 }
3879
3880 /* EVEX instructions shouldn't need the REX prefix. */
3881 i.vrex &= ~vrex_used;
3882 gas_assert (i.vrex == 0);
3883
6865c043
L
3884 /* Check the REX.W bit and VEXW. */
3885 if (i.tm.opcode_modifier.vexw == VEXWIG)
3886 w = (evexwig == evexw1 || (i.rex & REX_W)) ? 1 : 0;
3887 else if (i.tm.opcode_modifier.vexw)
3888 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3889 else
931d03b7 3890 w = (flag_code == CODE_64BIT ? i.rex & REX_W : evexwig == evexw1) ? 1 : 0;
43234a1e 3891
43234a1e 3892 /* The third byte of the EVEX prefix. */
35648716
JB
3893 i.vex.bytes[2] = ((w << 7)
3894 | (register_specifier << 3)
3895 | 4 /* Encode the U bit. */
3896 | i.tm.opcode_modifier.opcodeprefix);
43234a1e
L
3897
3898 /* The fourth byte of the EVEX prefix. */
3899 /* The zeroing-masking bit. */
6225c532 3900 if (i.mask.reg && i.mask.zeroing)
43234a1e
L
3901 i.vex.bytes[3] |= 0x80;
3902
3903 /* Don't always set the broadcast bit if there is no RC. */
ca5312a2 3904 if (i.rounding.type == rc_none)
43234a1e
L
3905 {
3906 /* Encode the vector length. */
3907 unsigned int vec_length;
3908
e771e7c9
JB
3909 if (!i.tm.opcode_modifier.evex
3910 || i.tm.opcode_modifier.evex == EVEXDYN)
3911 {
56522fc5 3912 unsigned int op;
e771e7c9 3913
c7213af9
L
3914 /* Determine vector length from the last multi-length vector
3915 operand. */
56522fc5 3916 for (op = i.operands; op--;)
e771e7c9
JB
3917 if (i.tm.operand_types[op].bitfield.xmmword
3918 + i.tm.operand_types[op].bitfield.ymmword
3919 + i.tm.operand_types[op].bitfield.zmmword > 1)
3920 {
3921 if (i.types[op].bitfield.zmmword)
c7213af9
L
3922 {
3923 i.tm.opcode_modifier.evex = EVEX512;
3924 break;
3925 }
e771e7c9 3926 else if (i.types[op].bitfield.ymmword)
c7213af9
L
3927 {
3928 i.tm.opcode_modifier.evex = EVEX256;
3929 break;
3930 }
e771e7c9 3931 else if (i.types[op].bitfield.xmmword)
c7213af9
L
3932 {
3933 i.tm.opcode_modifier.evex = EVEX128;
3934 break;
3935 }
a5748e0d 3936 else if (i.broadcast.bytes && op == i.broadcast.operand)
625cbd7a 3937 {
a5748e0d 3938 switch (get_broadcast_bytes (&i.tm, true))
625cbd7a
JB
3939 {
3940 case 64:
3941 i.tm.opcode_modifier.evex = EVEX512;
3942 break;
3943 case 32:
3944 i.tm.opcode_modifier.evex = EVEX256;
3945 break;
3946 case 16:
3947 i.tm.opcode_modifier.evex = EVEX128;
3948 break;
3949 default:
c7213af9 3950 abort ();
625cbd7a 3951 }
c7213af9 3952 break;
625cbd7a 3953 }
e771e7c9 3954 }
c7213af9 3955
56522fc5 3956 if (op >= MAX_OPERANDS)
c7213af9 3957 abort ();
e771e7c9
JB
3958 }
3959
43234a1e
L
3960 switch (i.tm.opcode_modifier.evex)
3961 {
3962 case EVEXLIG: /* LL' is ignored */
3963 vec_length = evexlig << 5;
3964 break;
3965 case EVEX128:
3966 vec_length = 0 << 5;
3967 break;
3968 case EVEX256:
3969 vec_length = 1 << 5;
3970 break;
3971 case EVEX512:
3972 vec_length = 2 << 5;
3973 break;
3974 default:
3975 abort ();
3976 break;
3977 }
3978 i.vex.bytes[3] |= vec_length;
3979 /* Encode the broadcast bit. */
a5748e0d 3980 if (i.broadcast.bytes)
43234a1e
L
3981 i.vex.bytes[3] |= 0x10;
3982 }
ca5312a2
JB
3983 else if (i.rounding.type != saeonly)
3984 i.vex.bytes[3] |= 0x10 | (i.rounding.type << 5);
43234a1e 3985 else
ca5312a2 3986 i.vex.bytes[3] |= 0x10 | (evexrcig << 5);
43234a1e 3987
6225c532
JB
3988 if (i.mask.reg)
3989 i.vex.bytes[3] |= i.mask.reg->reg_num;
43234a1e
L
3990}
3991
65da13b5
L
3992static void
3993process_immext (void)
3994{
3995 expressionS *exp;
3996
c0f3af97 3997 /* These AMD 3DNow! and SSE2 instructions have an opcode suffix
65da13b5
L
3998 which is coded in the same place as an 8-bit immediate field
3999 would be. Here we fake an 8-bit immediate operand from the
4000 opcode suffix stored in tm.extension_opcode.
4001
c1e679ec 4002 AVX instructions also use this encoding, for some of
c0f3af97 4003 3 argument instructions. */
65da13b5 4004
43234a1e 4005 gas_assert (i.imm_operands <= 1
7ab9ffdd 4006 && (i.operands <= 2
7a8655d2 4007 || (is_any_vex_encoding (&i.tm)
7ab9ffdd 4008 && i.operands <= 4)));
65da13b5
L
4009
4010 exp = &im_expressions[i.imm_operands++];
4011 i.op[i.operands].imms = exp;
be1643ff 4012 i.types[i.operands].bitfield.imm8 = 1;
65da13b5
L
4013 i.operands++;
4014 exp->X_op = O_constant;
4015 exp->X_add_number = i.tm.extension_opcode;
4016 i.tm.extension_opcode = None;
4017}
4018
42164a71
L
4019
4020static int
4021check_hle (void)
4022{
742732c7 4023 switch (i.tm.opcode_modifier.prefixok)
42164a71
L
4024 {
4025 default:
4026 abort ();
742732c7
JB
4027 case PrefixLock:
4028 case PrefixNone:
4029 case PrefixNoTrack:
4030 case PrefixRep:
165de32a
L
4031 as_bad (_("invalid instruction `%s' after `%s'"),
4032 i.tm.name, i.hle_prefix);
42164a71 4033 return 0;
742732c7 4034 case PrefixHLELock:
42164a71
L
4035 if (i.prefix[LOCK_PREFIX])
4036 return 1;
165de32a 4037 as_bad (_("missing `lock' with `%s'"), i.hle_prefix);
42164a71 4038 return 0;
742732c7 4039 case PrefixHLEAny:
42164a71 4040 return 1;
742732c7 4041 case PrefixHLERelease:
42164a71
L
4042 if (i.prefix[HLE_PREFIX] != XRELEASE_PREFIX_OPCODE)
4043 {
4044 as_bad (_("instruction `%s' after `xacquire' not allowed"),
4045 i.tm.name);
4046 return 0;
4047 }
8dc0818e 4048 if (i.mem_operands == 0 || !(i.flags[i.operands - 1] & Operand_Mem))
42164a71
L
4049 {
4050 as_bad (_("memory destination needed for instruction `%s'"
4051 " after `xrelease'"), i.tm.name);
4052 return 0;
4053 }
4054 return 1;
4055 }
4056}
4057
c8480b58
L
4058/* Encode aligned vector move as unaligned vector move. */
4059
4060static void
4061encode_with_unaligned_vector_move (void)
4062{
4063 switch (i.tm.base_opcode)
4064 {
b3a9fe6f
L
4065 case 0x28: /* Load instructions. */
4066 case 0x29: /* Store instructions. */
c8480b58
L
4067 /* movaps/movapd/vmovaps/vmovapd. */
4068 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4069 && i.tm.opcode_modifier.opcodeprefix <= PREFIX_0X66)
b3a9fe6f 4070 i.tm.base_opcode = 0x10 | (i.tm.base_opcode & 1);
c8480b58 4071 break;
b3a9fe6f
L
4072 case 0x6f: /* Load instructions. */
4073 case 0x7f: /* Store instructions. */
c8480b58
L
4074 /* movdqa/vmovdqa/vmovdqa64/vmovdqa32. */
4075 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4076 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0X66)
4077 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
4078 break;
4079 default:
4080 break;
4081 }
4082}
4083
b6f8c7c4
L
4084/* Try the shortest encoding by shortening operand size. */
4085
4086static void
4087optimize_encoding (void)
4088{
a0a1771e 4089 unsigned int j;
b6f8c7c4 4090
fe134c65
JB
4091 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
4092 && i.tm.base_opcode == 0x8d)
4093 {
4094 /* Optimize: -O:
4095 lea symbol, %rN -> mov $symbol, %rN
4096 lea (%rM), %rN -> mov %rM, %rN
4097 lea (,%rM,1), %rN -> mov %rM, %rN
4098
4099 and in 32-bit mode for 16-bit addressing
4100
4101 lea (%rM), %rN -> movzx %rM, %rN
4102
4103 and in 64-bit mode zap 32-bit addressing in favor of using a
4104 32-bit (or less) destination.
4105 */
4106 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
4107 {
4108 if (!i.op[1].regs->reg_type.bitfield.word)
4109 i.tm.opcode_modifier.size = SIZE32;
4110 i.prefix[ADDR_PREFIX] = 0;
4111 }
4112
4113 if (!i.index_reg && !i.base_reg)
4114 {
4115 /* Handle:
4116 lea symbol, %rN -> mov $symbol, %rN
4117 */
4118 if (flag_code == CODE_64BIT)
4119 {
4120 /* Don't transform a relocation to a 16-bit one. */
4121 if (i.op[0].disps
4122 && i.op[0].disps->X_op != O_constant
4123 && i.op[1].regs->reg_type.bitfield.word)
4124 return;
4125
4126 if (!i.op[1].regs->reg_type.bitfield.qword
4127 || i.tm.opcode_modifier.size == SIZE32)
4128 {
4129 i.tm.base_opcode = 0xb8;
4130 i.tm.opcode_modifier.modrm = 0;
4131 if (!i.op[1].regs->reg_type.bitfield.word)
4132 i.types[0].bitfield.imm32 = 1;
4133 else
4134 {
4135 i.tm.opcode_modifier.size = SIZE16;
4136 i.types[0].bitfield.imm16 = 1;
4137 }
4138 }
4139 else
4140 {
4141 /* Subject to further optimization below. */
4142 i.tm.base_opcode = 0xc7;
4143 i.tm.extension_opcode = 0;
4144 i.types[0].bitfield.imm32s = 1;
4145 i.types[0].bitfield.baseindex = 0;
4146 }
4147 }
4148 /* Outside of 64-bit mode address and operand sizes have to match if
4149 a relocation is involved, as otherwise we wouldn't (currently) or
4150 even couldn't express the relocation correctly. */
4151 else if (i.op[0].disps
4152 && i.op[0].disps->X_op != O_constant
4153 && ((!i.prefix[ADDR_PREFIX])
4154 != (flag_code == CODE_32BIT
4155 ? i.op[1].regs->reg_type.bitfield.dword
4156 : i.op[1].regs->reg_type.bitfield.word)))
4157 return;
7772f168
JB
4158 /* In 16-bit mode converting LEA with 16-bit addressing and a 32-bit
4159 destination is going to grow encoding size. */
4160 else if (flag_code == CODE_16BIT
4161 && (optimize <= 1 || optimize_for_space)
4162 && !i.prefix[ADDR_PREFIX]
4163 && i.op[1].regs->reg_type.bitfield.dword)
4164 return;
fe134c65
JB
4165 else
4166 {
4167 i.tm.base_opcode = 0xb8;
4168 i.tm.opcode_modifier.modrm = 0;
4169 if (i.op[1].regs->reg_type.bitfield.dword)
4170 i.types[0].bitfield.imm32 = 1;
4171 else
4172 i.types[0].bitfield.imm16 = 1;
4173
4174 if (i.op[0].disps
4175 && i.op[0].disps->X_op == O_constant
4176 && i.op[1].regs->reg_type.bitfield.dword
60cfa10c
L
4177 /* NB: Add () to !i.prefix[ADDR_PREFIX] to silence
4178 GCC 5. */
4179 && (!i.prefix[ADDR_PREFIX]) != (flag_code == CODE_32BIT))
fe134c65
JB
4180 i.op[0].disps->X_add_number &= 0xffff;
4181 }
4182
4183 i.tm.operand_types[0] = i.types[0];
4184 i.imm_operands = 1;
4185 if (!i.op[0].imms)
4186 {
4187 i.op[0].imms = &im_expressions[0];
4188 i.op[0].imms->X_op = O_absent;
4189 }
4190 }
4191 else if (i.op[0].disps
4192 && (i.op[0].disps->X_op != O_constant
4193 || i.op[0].disps->X_add_number))
4194 return;
4195 else
4196 {
4197 /* Handle:
4198 lea (%rM), %rN -> mov %rM, %rN
4199 lea (,%rM,1), %rN -> mov %rM, %rN
4200 lea (%rM), %rN -> movzx %rM, %rN
4201 */
4202 const reg_entry *addr_reg;
4203
4204 if (!i.index_reg && i.base_reg->reg_num != RegIP)
4205 addr_reg = i.base_reg;
4206 else if (!i.base_reg
4207 && i.index_reg->reg_num != RegIZ
4208 && !i.log2_scale_factor)
4209 addr_reg = i.index_reg;
4210 else
4211 return;
4212
4213 if (addr_reg->reg_type.bitfield.word
4214 && i.op[1].regs->reg_type.bitfield.dword)
4215 {
4216 if (flag_code != CODE_32BIT)
4217 return;
4218 i.tm.opcode_modifier.opcodespace = SPACE_0F;
4219 i.tm.base_opcode = 0xb7;
4220 }
4221 else
4222 i.tm.base_opcode = 0x8b;
4223
4224 if (addr_reg->reg_type.bitfield.dword
4225 && i.op[1].regs->reg_type.bitfield.qword)
4226 i.tm.opcode_modifier.size = SIZE32;
4227
4228 i.op[0].regs = addr_reg;
4229 i.reg_operands = 2;
4230 }
4231
4232 i.mem_operands = 0;
4233 i.disp_operands = 0;
4234 i.prefix[ADDR_PREFIX] = 0;
4235 i.prefix[SEG_PREFIX] = 0;
4236 i.seg[0] = NULL;
4237 }
4238
b6f8c7c4 4239 if (optimize_for_space
389d00a5 4240 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
b6f8c7c4
L
4241 && i.reg_operands == 1
4242 && i.imm_operands == 1
4243 && !i.types[1].bitfield.byte
4244 && i.op[0].imms->X_op == O_constant
4245 && fits_in_imm7 (i.op[0].imms->X_add_number)
72aea328 4246 && (i.tm.base_opcode == 0xa8
b6f8c7c4
L
4247 || (i.tm.base_opcode == 0xf6
4248 && i.tm.extension_opcode == 0x0)))
4249 {
4250 /* Optimize: -Os:
4251 test $imm7, %r64/%r32/%r16 -> test $imm7, %r8
4252 */
4253 unsigned int base_regnum = i.op[1].regs->reg_num;
4254 if (flag_code == CODE_64BIT || base_regnum < 4)
4255 {
4256 i.types[1].bitfield.byte = 1;
4257 /* Ignore the suffix. */
4258 i.suffix = 0;
7697afb6
JB
4259 /* Convert to byte registers. */
4260 if (i.types[1].bitfield.word)
4261 j = 16;
4262 else if (i.types[1].bitfield.dword)
4263 j = 32;
4264 else
4265 j = 48;
4266 if (!(i.op[1].regs->reg_flags & RegRex) && base_regnum < 4)
4267 j += 8;
4268 i.op[1].regs -= j;
b6f8c7c4
L
4269 }
4270 }
4271 else if (flag_code == CODE_64BIT
389d00a5 4272 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
d3d50934
L
4273 && ((i.types[1].bitfield.qword
4274 && i.reg_operands == 1
b6f8c7c4
L
4275 && i.imm_operands == 1
4276 && i.op[0].imms->X_op == O_constant
507916b8 4277 && ((i.tm.base_opcode == 0xb8
b6f8c7c4
L
4278 && i.tm.extension_opcode == None
4279 && fits_in_unsigned_long (i.op[0].imms->X_add_number))
4280 || (fits_in_imm31 (i.op[0].imms->X_add_number)
72aea328
JB
4281 && ((i.tm.base_opcode == 0x24
4282 || i.tm.base_opcode == 0xa8)
b6f8c7c4
L
4283 || (i.tm.base_opcode == 0x80
4284 && i.tm.extension_opcode == 0x4)
4285 || ((i.tm.base_opcode == 0xf6
507916b8 4286 || (i.tm.base_opcode | 1) == 0xc7)
b8364fa7
JB
4287 && i.tm.extension_opcode == 0x0)))
4288 || (fits_in_imm7 (i.op[0].imms->X_add_number)
4289 && i.tm.base_opcode == 0x83
4290 && i.tm.extension_opcode == 0x4)))
d3d50934
L
4291 || (i.types[0].bitfield.qword
4292 && ((i.reg_operands == 2
4293 && i.op[0].regs == i.op[1].regs
72aea328
JB
4294 && (i.tm.base_opcode == 0x30
4295 || i.tm.base_opcode == 0x28))
d3d50934
L
4296 || (i.reg_operands == 1
4297 && i.operands == 1
72aea328 4298 && i.tm.base_opcode == 0x30)))))
b6f8c7c4
L
4299 {
4300 /* Optimize: -O:
4301 andq $imm31, %r64 -> andl $imm31, %r32
b8364fa7 4302 andq $imm7, %r64 -> andl $imm7, %r32
b6f8c7c4
L
4303 testq $imm31, %r64 -> testl $imm31, %r32
4304 xorq %r64, %r64 -> xorl %r32, %r32
4305 subq %r64, %r64 -> subl %r32, %r32
4306 movq $imm31, %r64 -> movl $imm31, %r32
4307 movq $imm32, %r64 -> movl $imm32, %r32
4308 */
04784e33
JB
4309 i.tm.opcode_modifier.size = SIZE32;
4310 if (i.imm_operands)
4311 {
4312 i.types[0].bitfield.imm32 = 1;
4313 i.types[0].bitfield.imm32s = 0;
4314 i.types[0].bitfield.imm64 = 0;
4315 }
4316 else
4317 {
4318 i.types[0].bitfield.dword = 1;
4319 i.types[0].bitfield.qword = 0;
4320 }
4321 i.types[1].bitfield.dword = 1;
4322 i.types[1].bitfield.qword = 0;
507916b8 4323 if (i.tm.base_opcode == 0xb8 || (i.tm.base_opcode | 1) == 0xc7)
b6f8c7c4
L
4324 {
4325 /* Handle
4326 movq $imm31, %r64 -> movl $imm31, %r32
4327 movq $imm32, %r64 -> movl $imm32, %r32
4328 */
4329 i.tm.operand_types[0].bitfield.imm32 = 1;
4330 i.tm.operand_types[0].bitfield.imm32s = 0;
4331 i.tm.operand_types[0].bitfield.imm64 = 0;
507916b8 4332 if ((i.tm.base_opcode | 1) == 0xc7)
b6f8c7c4
L
4333 {
4334 /* Handle
4335 movq $imm31, %r64 -> movl $imm31, %r32
4336 */
507916b8 4337 i.tm.base_opcode = 0xb8;
b6f8c7c4 4338 i.tm.extension_opcode = None;
507916b8 4339 i.tm.opcode_modifier.w = 0;
b6f8c7c4
L
4340 i.tm.opcode_modifier.modrm = 0;
4341 }
4342 }
4343 }
5641ec01
JB
4344 else if (optimize > 1
4345 && !optimize_for_space
389d00a5 4346 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
5641ec01
JB
4347 && i.reg_operands == 2
4348 && i.op[0].regs == i.op[1].regs
4349 && ((i.tm.base_opcode & ~(Opcode_D | 1)) == 0x8
4350 || (i.tm.base_opcode & ~(Opcode_D | 1)) == 0x20)
4351 && (flag_code != CODE_64BIT || !i.types[0].bitfield.dword))
4352 {
4353 /* Optimize: -O2:
4354 andb %rN, %rN -> testb %rN, %rN
4355 andw %rN, %rN -> testw %rN, %rN
4356 andq %rN, %rN -> testq %rN, %rN
4357 orb %rN, %rN -> testb %rN, %rN
4358 orw %rN, %rN -> testw %rN, %rN
4359 orq %rN, %rN -> testq %rN, %rN
4360
4361 and outside of 64-bit mode
4362
4363 andl %rN, %rN -> testl %rN, %rN
4364 orl %rN, %rN -> testl %rN, %rN
4365 */
4366 i.tm.base_opcode = 0x84 | (i.tm.base_opcode & 1);
4367 }
99112332 4368 else if (i.reg_operands == 3
b6f8c7c4
L
4369 && i.op[0].regs == i.op[1].regs
4370 && !i.types[2].bitfield.xmmword
4371 && (i.tm.opcode_modifier.vex
6225c532 4372 || ((!i.mask.reg || i.mask.zeroing)
e771e7c9 4373 && is_evex_encoding (&i.tm)
80c34c38 4374 && (i.vec_encoding != vex_encoding_evex
dd22218c 4375 || cpu_arch_isa_flags.bitfield.cpuavx512vl
80c34c38 4376 || i.tm.cpu_flags.bitfield.cpuavx512vl
7091c612 4377 || (i.tm.operand_types[2].bitfield.zmmword
dd22218c 4378 && i.types[2].bitfield.ymmword))))
5844ccaa
JB
4379 && i.tm.opcode_modifier.opcodespace == SPACE_0F
4380 && ((i.tm.base_opcode | 2) == 0x57
4381 || i.tm.base_opcode == 0xdf
4382 || i.tm.base_opcode == 0xef
4383 || (i.tm.base_opcode | 3) == 0xfb
4384 || i.tm.base_opcode == 0x42
4385 || i.tm.base_opcode == 0x47))
b6f8c7c4 4386 {
99112332 4387 /* Optimize: -O1:
8305403a
L
4388 VOP, one of vandnps, vandnpd, vxorps, vxorpd, vpsubb, vpsubd,
4389 vpsubq and vpsubw:
b6f8c7c4
L
4390 EVEX VOP %zmmM, %zmmM, %zmmN
4391 -> VEX VOP %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 VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4395 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4396 VEX VOP %ymmM, %ymmM, %ymmN
4397 -> VEX VOP %xmmM, %xmmM, %xmmN
4398 VOP, one of vpandn and vpxor:
4399 VEX VOP %ymmM, %ymmM, %ymmN
4400 -> VEX VOP %xmmM, %xmmM, %xmmN
4401 VOP, one of vpandnd and vpandnq:
4402 EVEX VOP %zmmM, %zmmM, %zmmN
4403 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4404 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4405 EVEX VOP %ymmM, %ymmM, %ymmN
4406 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4407 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4408 VOP, one of vpxord and vpxorq:
4409 EVEX VOP %zmmM, %zmmM, %zmmN
4410 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4411 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4412 EVEX VOP %ymmM, %ymmM, %ymmN
4413 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4414 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
1424ad86
JB
4415 VOP, one of kxord and kxorq:
4416 VEX VOP %kM, %kM, %kN
4417 -> VEX kxorw %kM, %kM, %kN
4418 VOP, one of kandnd and kandnq:
4419 VEX VOP %kM, %kM, %kN
4420 -> VEX kandnw %kM, %kM, %kN
b6f8c7c4 4421 */
e771e7c9 4422 if (is_evex_encoding (&i.tm))
b6f8c7c4 4423 {
7b1d7ca1 4424 if (i.vec_encoding != vex_encoding_evex)
b6f8c7c4
L
4425 {
4426 i.tm.opcode_modifier.vex = VEX128;
4427 i.tm.opcode_modifier.vexw = VEXW0;
4428 i.tm.opcode_modifier.evex = 0;
4429 }
7b1d7ca1 4430 else if (optimize > 1)
dd22218c
L
4431 i.tm.opcode_modifier.evex = EVEX128;
4432 else
4433 return;
b6f8c7c4 4434 }
f74a6307 4435 else if (i.tm.operand_types[0].bitfield.class == RegMask)
1424ad86 4436 {
35648716 4437 i.tm.opcode_modifier.opcodeprefix = PREFIX_NONE;
1424ad86
JB
4438 i.tm.opcode_modifier.vexw = VEXW0;
4439 }
b6f8c7c4
L
4440 else
4441 i.tm.opcode_modifier.vex = VEX128;
4442
4443 if (i.tm.opcode_modifier.vex)
4444 for (j = 0; j < 3; j++)
4445 {
4446 i.types[j].bitfield.xmmword = 1;
4447 i.types[j].bitfield.ymmword = 0;
4448 }
4449 }
392a5972 4450 else if (i.vec_encoding != vex_encoding_evex
97ed31ae 4451 && !i.types[0].bitfield.zmmword
392a5972 4452 && !i.types[1].bitfield.zmmword
6225c532 4453 && !i.mask.reg
a5748e0d 4454 && !i.broadcast.bytes
97ed31ae 4455 && is_evex_encoding (&i.tm)
35648716
JB
4456 && ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4457 || (i.tm.base_opcode & ~4) == 0xdb
4458 || (i.tm.base_opcode & ~4) == 0xeb)
97ed31ae
L
4459 && i.tm.extension_opcode == None)
4460 {
4461 /* Optimize: -O1:
4462 VOP, one of vmovdqa32, vmovdqa64, vmovdqu8, vmovdqu16,
4463 vmovdqu32 and vmovdqu64:
4464 EVEX VOP %xmmM, %xmmN
4465 -> VEX vmovdqa|vmovdqu %xmmM, %xmmN (M and N < 16)
4466 EVEX VOP %ymmM, %ymmN
4467 -> VEX vmovdqa|vmovdqu %ymmM, %ymmN (M and N < 16)
4468 EVEX VOP %xmmM, mem
4469 -> VEX vmovdqa|vmovdqu %xmmM, mem (M < 16)
4470 EVEX VOP %ymmM, mem
4471 -> VEX vmovdqa|vmovdqu %ymmM, mem (M < 16)
4472 EVEX VOP mem, %xmmN
4473 -> VEX mvmovdqa|vmovdquem, %xmmN (N < 16)
4474 EVEX VOP mem, %ymmN
4475 -> VEX vmovdqa|vmovdqu mem, %ymmN (N < 16)
a0a1771e
JB
4476 VOP, one of vpand, vpandn, vpor, vpxor:
4477 EVEX VOP{d,q} %xmmL, %xmmM, %xmmN
4478 -> VEX VOP %xmmL, %xmmM, %xmmN (L, M, and N < 16)
4479 EVEX VOP{d,q} %ymmL, %ymmM, %ymmN
4480 -> VEX VOP %ymmL, %ymmM, %ymmN (L, M, and N < 16)
4481 EVEX VOP{d,q} mem, %xmmM, %xmmN
4482 -> VEX VOP mem, %xmmM, %xmmN (M and N < 16)
4483 EVEX VOP{d,q} mem, %ymmM, %ymmN
4484 -> VEX VOP mem, %ymmM, %ymmN (M and N < 16)
97ed31ae 4485 */
a0a1771e 4486 for (j = 0; j < i.operands; j++)
392a5972
L
4487 if (operand_type_check (i.types[j], disp)
4488 && i.op[j].disps->X_op == O_constant)
4489 {
4490 /* Since the VEX prefix has 2 or 3 bytes, the EVEX prefix
4491 has 4 bytes, EVEX Disp8 has 1 byte and VEX Disp32 has 4
4492 bytes, we choose EVEX Disp8 over VEX Disp32. */
4493 int evex_disp8, vex_disp8;
4494 unsigned int memshift = i.memshift;
4495 offsetT n = i.op[j].disps->X_add_number;
4496
4497 evex_disp8 = fits_in_disp8 (n);
4498 i.memshift = 0;
4499 vex_disp8 = fits_in_disp8 (n);
4500 if (evex_disp8 != vex_disp8)
4501 {
4502 i.memshift = memshift;
4503 return;
4504 }
4505
4506 i.types[j].bitfield.disp8 = vex_disp8;
4507 break;
4508 }
35648716
JB
4509 if ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4510 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2)
4511 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
97ed31ae
L
4512 i.tm.opcode_modifier.vex
4513 = i.types[0].bitfield.ymmword ? VEX256 : VEX128;
4514 i.tm.opcode_modifier.vexw = VEXW0;
79dec6b7 4515 /* VPAND, VPOR, and VPXOR are commutative. */
35648716 4516 if (i.reg_operands == 3 && i.tm.base_opcode != 0xdf)
79dec6b7 4517 i.tm.opcode_modifier.commutative = 1;
97ed31ae
L
4518 i.tm.opcode_modifier.evex = 0;
4519 i.tm.opcode_modifier.masking = 0;
a0a1771e 4520 i.tm.opcode_modifier.broadcast = 0;
97ed31ae
L
4521 i.tm.opcode_modifier.disp8memshift = 0;
4522 i.memshift = 0;
a0a1771e
JB
4523 if (j < i.operands)
4524 i.types[j].bitfield.disp8
4525 = fits_in_disp8 (i.op[j].disps->X_add_number);
97ed31ae 4526 }
b6f8c7c4
L
4527}
4528
ae531041
L
4529/* Return non-zero for load instruction. */
4530
4531static int
4532load_insn_p (void)
4533{
4534 unsigned int dest;
4535 int any_vex_p = is_any_vex_encoding (&i.tm);
4536 unsigned int base_opcode = i.tm.base_opcode | 1;
4537
4538 if (!any_vex_p)
4539 {
ef07be45
CL
4540 /* Anysize insns: lea, invlpg, clflush, prefetch*, bndmk, bndcl, bndcu,
4541 bndcn, bndstx, bndldx, clflushopt, clwb, cldemote. */
255571cd 4542 if (i.tm.opcode_modifier.operandconstraint == ANY_SIZE)
ae531041
L
4543 return 0;
4544
389d00a5
JB
4545 /* pop. */
4546 if (strcmp (i.tm.name, "pop") == 0)
4547 return 1;
4548 }
4549
4550 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE)
4551 {
4552 /* popf, popa. */
4553 if (i.tm.base_opcode == 0x9d
a09f656b 4554 || i.tm.base_opcode == 0x61)
ae531041
L
4555 return 1;
4556
4557 /* movs, cmps, lods, scas. */
4558 if ((i.tm.base_opcode | 0xb) == 0xaf)
4559 return 1;
4560
a09f656b 4561 /* outs, xlatb. */
4562 if (base_opcode == 0x6f
4563 || i.tm.base_opcode == 0xd7)
ae531041 4564 return 1;
a09f656b 4565 /* NB: For AMD-specific insns with implicit memory operands,
4566 they're intentionally not covered. */
ae531041
L
4567 }
4568
4569 /* No memory operand. */
4570 if (!i.mem_operands)
4571 return 0;
4572
4573 if (any_vex_p)
4574 {
4575 /* vldmxcsr. */
4576 if (i.tm.base_opcode == 0xae
4577 && i.tm.opcode_modifier.vex
441f6aca 4578 && i.tm.opcode_modifier.opcodespace == SPACE_0F
35648716 4579 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
ae531041
L
4580 && i.tm.extension_opcode == 2)
4581 return 1;
4582 }
389d00a5 4583 else if (i.tm.opcode_modifier.opcodespace == SPACE_BASE)
ae531041
L
4584 {
4585 /* test, not, neg, mul, imul, div, idiv. */
4586 if ((i.tm.base_opcode == 0xf6 || i.tm.base_opcode == 0xf7)
4587 && i.tm.extension_opcode != 1)
4588 return 1;
4589
4590 /* inc, dec. */
4591 if (base_opcode == 0xff && i.tm.extension_opcode <= 1)
4592 return 1;
4593
4594 /* add, or, adc, sbb, and, sub, xor, cmp. */
4595 if (i.tm.base_opcode >= 0x80 && i.tm.base_opcode <= 0x83)
4596 return 1;
4597
ae531041
L
4598 /* rol, ror, rcl, rcr, shl/sal, shr, sar. */
4599 if ((base_opcode == 0xc1
4600 || (i.tm.base_opcode >= 0xd0 && i.tm.base_opcode <= 0xd3))
4601 && i.tm.extension_opcode != 6)
4602 return 1;
4603
ae531041 4604 /* Check for x87 instructions. */
389d00a5 4605 if (base_opcode >= 0xd8 && base_opcode <= 0xdf)
ae531041
L
4606 {
4607 /* Skip fst, fstp, fstenv, fstcw. */
4608 if (i.tm.base_opcode == 0xd9
4609 && (i.tm.extension_opcode == 2
4610 || i.tm.extension_opcode == 3
4611 || i.tm.extension_opcode == 6
4612 || i.tm.extension_opcode == 7))
4613 return 0;
4614
4615 /* Skip fisttp, fist, fistp, fstp. */
4616 if (i.tm.base_opcode == 0xdb
4617 && (i.tm.extension_opcode == 1
4618 || i.tm.extension_opcode == 2
4619 || i.tm.extension_opcode == 3
4620 || i.tm.extension_opcode == 7))
4621 return 0;
4622
4623 /* Skip fisttp, fst, fstp, fsave, fstsw. */
4624 if (i.tm.base_opcode == 0xdd
4625 && (i.tm.extension_opcode == 1
4626 || i.tm.extension_opcode == 2
4627 || i.tm.extension_opcode == 3
4628 || i.tm.extension_opcode == 6
4629 || i.tm.extension_opcode == 7))
4630 return 0;
4631
4632 /* Skip fisttp, fist, fistp, fbstp, fistp. */
4633 if (i.tm.base_opcode == 0xdf
4634 && (i.tm.extension_opcode == 1
4635 || i.tm.extension_opcode == 2
4636 || i.tm.extension_opcode == 3
4637 || i.tm.extension_opcode == 6
4638 || i.tm.extension_opcode == 7))
4639 return 0;
4640
4641 return 1;
4642 }
4643 }
389d00a5
JB
4644 else if (i.tm.opcode_modifier.opcodespace == SPACE_0F)
4645 {
4646 /* bt, bts, btr, btc. */
4647 if (i.tm.base_opcode == 0xba
4648 && (i.tm.extension_opcode >= 4 && i.tm.extension_opcode <= 7))
4649 return 1;
4650
4651 /* cmpxchg8b, cmpxchg16b, xrstors, vmptrld. */
4652 if (i.tm.base_opcode == 0xc7
4653 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
4654 && (i.tm.extension_opcode == 1 || i.tm.extension_opcode == 3
4655 || i.tm.extension_opcode == 6))
4656 return 1;
4657
4658 /* fxrstor, ldmxcsr, xrstor. */
4659 if (i.tm.base_opcode == 0xae
4660 && (i.tm.extension_opcode == 1
4661 || i.tm.extension_opcode == 2
4662 || i.tm.extension_opcode == 5))
4663 return 1;
4664
4665 /* lgdt, lidt, lmsw. */
4666 if (i.tm.base_opcode == 0x01
4667 && (i.tm.extension_opcode == 2
4668 || i.tm.extension_opcode == 3
4669 || i.tm.extension_opcode == 6))
4670 return 1;
4671 }
ae531041
L
4672
4673 dest = i.operands - 1;
4674
4675 /* Check fake imm8 operand and 3 source operands. */
4676 if ((i.tm.opcode_modifier.immext
4677 || i.tm.opcode_modifier.vexsources == VEX3SOURCES)
4678 && i.types[dest].bitfield.imm8)
4679 dest--;
4680
389d00a5
JB
4681 /* add, or, adc, sbb, and, sub, xor, cmp, test, xchg. */
4682 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
ae531041
L
4683 && (base_opcode == 0x1
4684 || base_opcode == 0x9
4685 || base_opcode == 0x11
4686 || base_opcode == 0x19
4687 || base_opcode == 0x21
4688 || base_opcode == 0x29
4689 || base_opcode == 0x31
4690 || base_opcode == 0x39
389d00a5
JB
4691 || (base_opcode | 2) == 0x87))
4692 return 1;
4693
4694 /* xadd. */
4695 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4696 && base_opcode == 0xc1)
ae531041
L
4697 return 1;
4698
4699 /* Check for load instruction. */
4700 return (i.types[dest].bitfield.class != ClassNone
4701 || i.types[dest].bitfield.instance == Accum);
4702}
4703
4704/* Output lfence, 0xfaee8, after instruction. */
4705
4706static void
4707insert_lfence_after (void)
4708{
4709 if (lfence_after_load && load_insn_p ())
4710 {
a09f656b 4711 /* There are also two REP string instructions that require
4712 special treatment. Specifically, the compare string (CMPS)
4713 and scan string (SCAS) instructions set EFLAGS in a manner
4714 that depends on the data being compared/scanned. When used
4715 with a REP prefix, the number of iterations may therefore
4716 vary depending on this data. If the data is a program secret
4717 chosen by the adversary using an LVI method,
4718 then this data-dependent behavior may leak some aspect
4719 of the secret. */
4720 if (((i.tm.base_opcode | 0x1) == 0xa7
4721 || (i.tm.base_opcode | 0x1) == 0xaf)
4722 && i.prefix[REP_PREFIX])
4723 {
4724 as_warn (_("`%s` changes flags which would affect control flow behavior"),
4725 i.tm.name);
4726 }
ae531041
L
4727 char *p = frag_more (3);
4728 *p++ = 0xf;
4729 *p++ = 0xae;
4730 *p = 0xe8;
4731 }
4732}
4733
4734/* Output lfence, 0xfaee8, before instruction. */
4735
4736static void
4737insert_lfence_before (void)
4738{
4739 char *p;
4740
389d00a5 4741 if (i.tm.opcode_modifier.opcodespace != SPACE_BASE)
ae531041
L
4742 return;
4743
4744 if (i.tm.base_opcode == 0xff
4745 && (i.tm.extension_opcode == 2 || i.tm.extension_opcode == 4))
4746 {
4747 /* Insert lfence before indirect branch if needed. */
4748
4749 if (lfence_before_indirect_branch == lfence_branch_none)
4750 return;
4751
4752 if (i.operands != 1)
4753 abort ();
4754
4755 if (i.reg_operands == 1)
4756 {
4757 /* Indirect branch via register. Don't insert lfence with
4758 -mlfence-after-load=yes. */
4759 if (lfence_after_load
4760 || lfence_before_indirect_branch == lfence_branch_memory)
4761 return;
4762 }
4763 else if (i.mem_operands == 1
4764 && lfence_before_indirect_branch != lfence_branch_register)
4765 {
4766 as_warn (_("indirect `%s` with memory operand should be avoided"),
4767 i.tm.name);
4768 return;
4769 }
4770 else
4771 return;
4772
4773 if (last_insn.kind != last_insn_other
4774 && last_insn.seg == now_seg)
4775 {
4776 as_warn_where (last_insn.file, last_insn.line,
4777 _("`%s` skips -mlfence-before-indirect-branch on `%s`"),
4778 last_insn.name, i.tm.name);
4779 return;
4780 }
4781
4782 p = frag_more (3);
4783 *p++ = 0xf;
4784 *p++ = 0xae;
4785 *p = 0xe8;
4786 return;
4787 }
4788
503648e4 4789 /* Output or/not/shl and lfence before near ret. */
ae531041
L
4790 if (lfence_before_ret != lfence_before_ret_none
4791 && (i.tm.base_opcode == 0xc2
503648e4 4792 || i.tm.base_opcode == 0xc3))
ae531041
L
4793 {
4794 if (last_insn.kind != last_insn_other
4795 && last_insn.seg == now_seg)
4796 {
4797 as_warn_where (last_insn.file, last_insn.line,
4798 _("`%s` skips -mlfence-before-ret on `%s`"),
4799 last_insn.name, i.tm.name);
4800 return;
4801 }
a09f656b 4802
a09f656b 4803 /* Near ret ingore operand size override under CPU64. */
503648e4 4804 char prefix = flag_code == CODE_64BIT
4805 ? 0x48
4806 : i.prefix[DATA_PREFIX] ? 0x66 : 0x0;
a09f656b 4807
4808 if (lfence_before_ret == lfence_before_ret_not)
4809 {
4810 /* not: 0xf71424, may add prefix
4811 for operand size override or 64-bit code. */
4812 p = frag_more ((prefix ? 2 : 0) + 6 + 3);
4813 if (prefix)
4814 *p++ = prefix;
ae531041
L
4815 *p++ = 0xf7;
4816 *p++ = 0x14;
4817 *p++ = 0x24;
a09f656b 4818 if (prefix)
4819 *p++ = prefix;
ae531041
L
4820 *p++ = 0xf7;
4821 *p++ = 0x14;
4822 *p++ = 0x24;
4823 }
a09f656b 4824 else
4825 {
4826 p = frag_more ((prefix ? 1 : 0) + 4 + 3);
4827 if (prefix)
4828 *p++ = prefix;
4829 if (lfence_before_ret == lfence_before_ret_or)
4830 {
4831 /* or: 0x830c2400, may add prefix
4832 for operand size override or 64-bit code. */
4833 *p++ = 0x83;
4834 *p++ = 0x0c;
4835 }
4836 else
4837 {
4838 /* shl: 0xc1242400, may add prefix
4839 for operand size override or 64-bit code. */
4840 *p++ = 0xc1;
4841 *p++ = 0x24;
4842 }
4843
4844 *p++ = 0x24;
4845 *p++ = 0x0;
4846 }
4847
ae531041
L
4848 *p++ = 0xf;
4849 *p++ = 0xae;
4850 *p = 0xe8;
4851 }
4852}
4853
04784e33
JB
4854/* Helper for md_assemble() to decide whether to prepare for a possible 2nd
4855 parsing pass. Instead of introducing a rarely use new insn attribute this
4856 utilizes a common pattern between affected templates. It is deemed
4857 acceptable that this will lead to unnecessary pass 2 preparations in a
4858 limited set of cases. */
4859static INLINE bool may_need_pass2 (const insn_template *t)
4860{
4861 return t->opcode_modifier.sse2avx
4862 /* Note that all SSE2AVX templates have at least one operand. */
4863 && t->operand_types[t->operands - 1].bitfield.class == RegSIMD;
4864}
4865
252b5132
RH
4866/* This is the guts of the machine-dependent assembler. LINE points to a
4867 machine dependent instruction. This function is supposed to emit
4868 the frags/bytes it assembles to. */
4869
4870void
65da13b5 4871md_assemble (char *line)
252b5132 4872{
40fb9820 4873 unsigned int j;
9db83a32 4874 char mnemonic[MAX_MNEM_SIZE], mnem_suffix = 0, *copy = NULL;
04784e33
JB
4875 const char *end, *pass1_mnem = NULL;
4876 enum i386_error pass1_err = 0;
d3ce72d0 4877 const insn_template *t;
252b5132 4878
47926f60 4879 /* Initialize globals. */
04784e33
JB
4880 current_templates = NULL;
4881 retry:
252b5132 4882 memset (&i, '\0', sizeof (i));
ca5312a2 4883 i.rounding.type = rc_none;
252b5132 4884 for (j = 0; j < MAX_OPERANDS; j++)
1ae12ab7 4885 i.reloc[j] = NO_RELOC;
252b5132
RH
4886 memset (disp_expressions, '\0', sizeof (disp_expressions));
4887 memset (im_expressions, '\0', sizeof (im_expressions));
ce8a8b2f 4888 save_stack_p = save_stack;
252b5132
RH
4889
4890 /* First parse an instruction mnemonic & call i386_operand for the operands.
4891 We assume that the scrubber has arranged it so that line[0] is the valid
47926f60 4892 start of a (possibly prefixed) mnemonic. */
252b5132 4893
5317ad2c
JB
4894 end = parse_insn (line, mnemonic);
4895 if (end == NULL)
04784e33
JB
4896 {
4897 if (pass1_mnem != NULL)
4898 goto match_error;
9db83a32
JB
4899 if (i.error != no_error)
4900 {
4901 gas_assert (current_templates != NULL);
4902 if (may_need_pass2 (current_templates->start) && !i.suffix)
4903 goto no_match;
4904 /* No point in trying a 2nd pass - it'll only find the same suffix
4905 again. */
4906 mnem_suffix = i.suffix;
4907 goto match_error;
4908 }
04784e33
JB
4909 return;
4910 }
4911 if (may_need_pass2 (current_templates->start))
4912 {
4913 /* Make a copy of the full line in case we need to retry. */
4914 copy = xstrdup (line);
4915 }
5317ad2c 4916 line += end - line;
83b16ac6 4917 mnem_suffix = i.suffix;
252b5132 4918
29b0f896 4919 line = parse_operands (line, mnemonic);
ee86248c 4920 this_operand = -1;
29b0f896 4921 if (line == NULL)
04784e33
JB
4922 {
4923 free (copy);
4924 return;
4925 }
252b5132 4926
29b0f896
AM
4927 /* Now we've parsed the mnemonic into a set of templates, and have the
4928 operands at hand. */
4929
b630c145 4930 /* All Intel opcodes have reversed operands except for "bound", "enter",
c0e54661 4931 "invlpg*", "monitor*", "mwait*", "tpause", "umwait", "pvalidate",
b0e8fa7f
TJ
4932 "rmpadjust", "rmpupdate", and "rmpquery". We also don't reverse
4933 intersegment "jmp" and "call" instructions with 2 immediate operands so
4934 that the immediate segment precedes the offset consistently in Intel and
4935 AT&T modes. */
4d456e3d
L
4936 if (intel_syntax
4937 && i.operands > 1
29b0f896 4938 && (strcmp (mnemonic, "bound") != 0)
c0e54661 4939 && (strncmp (mnemonic, "invlpg", 6) != 0)
d34049e8
ML
4940 && !startswith (mnemonic, "monitor")
4941 && !startswith (mnemonic, "mwait")
c0e54661 4942 && (strcmp (mnemonic, "pvalidate") != 0)
d34049e8 4943 && !startswith (mnemonic, "rmp")
b630c145
JB
4944 && (strcmp (mnemonic, "tpause") != 0)
4945 && (strcmp (mnemonic, "umwait") != 0)
47c0279b
JB
4946 && !(i.operands == 2
4947 && operand_type_check (i.types[0], imm)
40fb9820 4948 && operand_type_check (i.types[1], imm)))
29b0f896
AM
4949 swap_operands ();
4950
ec56d5c0
JB
4951 /* The order of the immediates should be reversed
4952 for 2 immediates extrq and insertq instructions */
4953 if (i.imm_operands == 2
4954 && (strcmp (mnemonic, "extrq") == 0
4955 || strcmp (mnemonic, "insertq") == 0))
4956 swap_2_operands (0, 1);
4957
29b0f896
AM
4958 if (i.imm_operands)
4959 optimize_imm ();
4960
9386188e
JB
4961 if (i.disp_operands && !want_disp32 (current_templates->start)
4962 && (!current_templates->start->opcode_modifier.jump
4963 || i.jumpabsolute || i.types[0].bitfield.baseindex))
cce08655
JB
4964 {
4965 for (j = 0; j < i.operands; ++j)
4966 {
4967 const expressionS *exp = i.op[j].disps;
4968
4969 if (!operand_type_check (i.types[j], disp))
4970 continue;
4971
4972 if (exp->X_op != O_constant)
4973 continue;
4974
4975 /* Since displacement is signed extended to 64bit, don't allow
a775efc8 4976 disp32 if it is out of range. */
cce08655
JB
4977 if (fits_in_signed_long (exp->X_add_number))
4978 continue;
4979
a775efc8 4980 i.types[j].bitfield.disp32 = 0;
cce08655
JB
4981 if (i.types[j].bitfield.baseindex)
4982 {
f493c217
AM
4983 as_bad (_("0x%" PRIx64 " out of range of signed 32bit displacement"),
4984 (uint64_t) exp->X_add_number);
cce08655
JB
4985 return;
4986 }
4987 }
4988 }
4989
b300c311
L
4990 /* Don't optimize displacement for movabs since it only takes 64bit
4991 displacement. */
4992 if (i.disp_operands
1a42a9fe 4993 && i.disp_encoding <= disp_encoding_8bit
862be3fb
L
4994 && (flag_code != CODE_64BIT
4995 || strcmp (mnemonic, "movabs") != 0))
4996 optimize_disp ();
29b0f896
AM
4997
4998 /* Next, we find a template that matches the given insn,
4999 making sure the overlap of the given operands types is consistent
5000 with the template operand types. */
252b5132 5001
83b16ac6 5002 if (!(t = match_template (mnem_suffix)))
04784e33
JB
5003 {
5004 const char *err_msg;
5005
5006 if (copy && !mnem_suffix)
5007 {
5008 line = copy;
5009 copy = NULL;
9db83a32 5010 no_match:
04784e33
JB
5011 pass1_err = i.error;
5012 pass1_mnem = current_templates->start->name;
5013 goto retry;
5014 }
9db83a32
JB
5015
5016 /* If a non-/only-64bit template (group) was found in pass 1, and if
5017 _some_ template (group) was found in pass 2, squash pass 1's
5018 error. */
5019 if (pass1_err == unsupported_64bit)
5020 pass1_mnem = NULL;
5021
04784e33 5022 match_error:
9db83a32
JB
5023 free (copy);
5024
04784e33
JB
5025 switch (pass1_mnem ? pass1_err : i.error)
5026 {
5027 default:
5028 abort ();
5029 case operand_size_mismatch:
5030 err_msg = _("operand size mismatch");
5031 break;
5032 case operand_type_mismatch:
5033 err_msg = _("operand type mismatch");
5034 break;
5035 case register_type_mismatch:
5036 err_msg = _("register type mismatch");
5037 break;
5038 case number_of_operands_mismatch:
5039 err_msg = _("number of operands mismatch");
5040 break;
5041 case invalid_instruction_suffix:
5042 err_msg = _("invalid instruction suffix");
5043 break;
5044 case bad_imm4:
5045 err_msg = _("constant doesn't fit in 4 bits");
5046 break;
5047 case unsupported_with_intel_mnemonic:
5048 err_msg = _("unsupported with Intel mnemonic");
5049 break;
5050 case unsupported_syntax:
5051 err_msg = _("unsupported syntax");
5052 break;
5053 case unsupported:
5054 as_bad (_("unsupported instruction `%s'"),
5055 pass1_mnem ? pass1_mnem : current_templates->start->name);
5056 return;
9db83a32
JB
5057 case unsupported_on_arch:
5058 as_bad (_("`%s' is not supported on `%s%s'"),
5059 pass1_mnem ? pass1_mnem : current_templates->start->name,
5060 cpu_arch_name ? cpu_arch_name : default_arch,
5061 cpu_sub_arch_name ? cpu_sub_arch_name : "");
5062 return;
5063 case unsupported_64bit:
5064 if (ISLOWER (mnem_suffix))
5065 as_bad (_("`%s%c' is %s supported in 64-bit mode"),
5066 pass1_mnem ? pass1_mnem : current_templates->start->name,
5067 mnem_suffix,
5068 flag_code == CODE_64BIT ? _("not") : _("only"));
5069 else
5070 as_bad (_("`%s' is %s supported in 64-bit mode"),
5071 pass1_mnem ? pass1_mnem : current_templates->start->name,
5072 flag_code == CODE_64BIT ? _("not") : _("only"));
5073 return;
04784e33
JB
5074 case invalid_sib_address:
5075 err_msg = _("invalid SIB address");
5076 break;
5077 case invalid_vsib_address:
5078 err_msg = _("invalid VSIB address");
5079 break;
5080 case invalid_vector_register_set:
5081 err_msg = _("mask, index, and destination registers must be distinct");
5082 break;
5083 case invalid_tmm_register_set:
5084 err_msg = _("all tmm registers must be distinct");
5085 break;
5086 case invalid_dest_and_src_register_set:
5087 err_msg = _("destination and source registers must be distinct");
5088 break;
5089 case unsupported_vector_index_register:
5090 err_msg = _("unsupported vector index register");
5091 break;
5092 case unsupported_broadcast:
5093 err_msg = _("unsupported broadcast");
5094 break;
5095 case broadcast_needed:
5096 err_msg = _("broadcast is needed for operand of such type");
5097 break;
5098 case unsupported_masking:
5099 err_msg = _("unsupported masking");
5100 break;
5101 case mask_not_on_destination:
5102 err_msg = _("mask not on destination operand");
5103 break;
5104 case no_default_mask:
5105 err_msg = _("default mask isn't allowed");
5106 break;
5107 case unsupported_rc_sae:
5108 err_msg = _("unsupported static rounding/sae");
5109 break;
5110 case invalid_register_operand:
5111 err_msg = _("invalid register operand");
5112 break;
5113 }
5114 as_bad (_("%s for `%s'"), err_msg,
5115 pass1_mnem ? pass1_mnem : current_templates->start->name);
5116 return;
5117 }
5118
5119 free (copy);
252b5132 5120
7bab8ab5 5121 if (sse_check != check_none
ffb86450
JB
5122 /* The opcode space check isn't strictly needed; it's there only to
5123 bypass the logic below when easily possible. */
5124 && t->opcode_modifier.opcodespace >= SPACE_0F
5125 && t->opcode_modifier.opcodespace <= SPACE_0F3A
5126 && !i.tm.cpu_flags.bitfield.cpusse4a
5127 && !is_any_vex_encoding (t))
daf50ae7 5128 {
ffb86450
JB
5129 bool simd = false;
5130
5131 for (j = 0; j < t->operands; ++j)
5132 {
5133 if (t->operand_types[j].bitfield.class == RegMMX)
5134 break;
5135 if (t->operand_types[j].bitfield.class == RegSIMD)
5136 simd = true;
5137 }
5138
5139 if (j >= t->operands && simd)
5140 (sse_check == check_warning
5141 ? as_warn
5142 : as_bad) (_("SSE instruction `%s' is used"), i.tm.name);
daf50ae7
L
5143 }
5144
40fb9820 5145 if (i.tm.opcode_modifier.fwait)
29b0f896
AM
5146 if (!add_prefix (FWAIT_OPCODE))
5147 return;
252b5132 5148
d5de92cf 5149 /* Check if REP prefix is OK. */
742732c7 5150 if (i.rep_prefix && i.tm.opcode_modifier.prefixok != PrefixRep)
d5de92cf
L
5151 {
5152 as_bad (_("invalid instruction `%s' after `%s'"),
5153 i.tm.name, i.rep_prefix);
5154 return;
5155 }
5156
c1ba0266
L
5157 /* Check for lock without a lockable instruction. Destination operand
5158 must be memory unless it is xchg (0x86). */
c32fa91d 5159 if (i.prefix[LOCK_PREFIX]
742732c7 5160 && (i.tm.opcode_modifier.prefixok < PrefixLock
c1ba0266
L
5161 || i.mem_operands == 0
5162 || (i.tm.base_opcode != 0x86
8dc0818e 5163 && !(i.flags[i.operands - 1] & Operand_Mem))))
c32fa91d
L
5164 {
5165 as_bad (_("expecting lockable instruction after `lock'"));
5166 return;
5167 }
5168
e3669c7f
JB
5169 if (is_any_vex_encoding (&i.tm)
5170 || i.tm.operand_types[i.imm_operands].bitfield.class >= RegMMX
5171 || i.tm.operand_types[i.imm_operands + 1].bitfield.class >= RegMMX)
7a8655d2 5172 {
e3669c7f
JB
5173 /* Check for data size prefix on VEX/XOP/EVEX encoded and SIMD insns. */
5174 if (i.prefix[DATA_PREFIX])
5175 {
5176 as_bad (_("data size prefix invalid with `%s'"), i.tm.name);
5177 return;
5178 }
5179
5180 /* Don't allow e.g. KMOV in TLS code sequences. */
5181 for (j = i.imm_operands; j < i.operands; ++j)
5182 switch (i.reloc[j])
5183 {
5184 case BFD_RELOC_386_TLS_GOTIE:
5185 case BFD_RELOC_386_TLS_LE_32:
5186 case BFD_RELOC_X86_64_GOTTPOFF:
5187 case BFD_RELOC_X86_64_TLSLD:
5188 as_bad (_("TLS relocation cannot be used with `%s'"), i.tm.name);
5189 return;
5190 default:
5191 break;
5192 }
7a8655d2
JB
5193 }
5194
42164a71 5195 /* Check if HLE prefix is OK. */
165de32a 5196 if (i.hle_prefix && !check_hle ())
42164a71
L
5197 return;
5198
7e8b059b
L
5199 /* Check BND prefix. */
5200 if (i.bnd_prefix && !i.tm.opcode_modifier.bndprefixok)
5201 as_bad (_("expecting valid branch instruction after `bnd'"));
5202
04ef582a 5203 /* Check NOTRACK prefix. */
742732c7 5204 if (i.notrack_prefix && i.tm.opcode_modifier.prefixok != PrefixNoTrack)
9fef80d6 5205 as_bad (_("expecting indirect branch instruction after `notrack'"));
04ef582a 5206
327e8c42
JB
5207 if (i.tm.cpu_flags.bitfield.cpumpx)
5208 {
5209 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
5210 as_bad (_("32-bit address isn't allowed in 64-bit MPX instructions."));
5211 else if (flag_code != CODE_16BIT
5212 ? i.prefix[ADDR_PREFIX]
5213 : i.mem_operands && !i.prefix[ADDR_PREFIX])
5214 as_bad (_("16-bit address isn't allowed in MPX instructions"));
5215 }
7e8b059b
L
5216
5217 /* Insert BND prefix. */
76d3a78a
JB
5218 if (add_bnd_prefix && i.tm.opcode_modifier.bndprefixok)
5219 {
5220 if (!i.prefix[BND_PREFIX])
5221 add_prefix (BND_PREFIX_OPCODE);
5222 else if (i.prefix[BND_PREFIX] != BND_PREFIX_OPCODE)
5223 {
5224 as_warn (_("replacing `rep'/`repe' prefix by `bnd'"));
5225 i.prefix[BND_PREFIX] = BND_PREFIX_OPCODE;
5226 }
5227 }
7e8b059b 5228
29b0f896 5229 /* Check string instruction segment overrides. */
51c8edf6 5230 if (i.tm.opcode_modifier.isstring >= IS_STRING_ES_OP0)
29b0f896 5231 {
51c8edf6 5232 gas_assert (i.mem_operands);
29b0f896 5233 if (!check_string ())
5dd0794d 5234 return;
fc0763e6 5235 i.disp_operands = 0;
29b0f896 5236 }
5dd0794d 5237
9373f275
L
5238 /* The memory operand of (%dx) should be only used with input/output
5239 instructions (base opcodes: 0x6c, 0x6e, 0xec, 0xee). */
5240 if (i.input_output_operand
5241 && ((i.tm.base_opcode | 0x82) != 0xee
5242 || i.tm.opcode_modifier.opcodespace != SPACE_BASE))
5243 {
5244 as_bad (_("input/output port address isn't allowed with `%s'"),
5245 i.tm.name);
5246 return;
5247 }
5248
b6f8c7c4
L
5249 if (optimize && !i.no_optimize && i.tm.opcode_modifier.optimize)
5250 optimize_encoding ();
5251
c8480b58
L
5252 if (use_unaligned_vector_move)
5253 encode_with_unaligned_vector_move ();
5254
29b0f896
AM
5255 if (!process_suffix ())
5256 return;
e413e4e9 5257
ef07be45
CL
5258 /* Check if IP-relative addressing requirements can be satisfied. */
5259 if (i.tm.cpu_flags.bitfield.cpuprefetchi
5260 && !(i.base_reg && i.base_reg->reg_num == RegIP))
f2462532 5261 as_warn (_("'%s' only supports RIP-relative address"), i.tm.name);
ef07be45 5262
921eafea 5263 /* Update operand types and check extended states. */
bc0844ae 5264 for (j = 0; j < i.operands; j++)
921eafea
L
5265 {
5266 i.types[j] = operand_type_and (i.types[j], i.tm.operand_types[j]);
3d70986f 5267 switch (i.tm.operand_types[j].bitfield.class)
921eafea
L
5268 {
5269 default:
5270 break;
5271 case RegMMX:
5272 i.xstate |= xstate_mmx;
5273 break;
5274 case RegMask:
32930e4e 5275 i.xstate |= xstate_mask;
921eafea
L
5276 break;
5277 case RegSIMD:
3d70986f 5278 if (i.tm.operand_types[j].bitfield.tmmword)
921eafea 5279 i.xstate |= xstate_tmm;
3d70986f 5280 else if (i.tm.operand_types[j].bitfield.zmmword)
921eafea 5281 i.xstate |= xstate_zmm;
3d70986f 5282 else if (i.tm.operand_types[j].bitfield.ymmword)
921eafea 5283 i.xstate |= xstate_ymm;
3d70986f 5284 else if (i.tm.operand_types[j].bitfield.xmmword)
921eafea
L
5285 i.xstate |= xstate_xmm;
5286 break;
5287 }
5288 }
bc0844ae 5289
29b0f896
AM
5290 /* Make still unresolved immediate matches conform to size of immediate
5291 given in i.suffix. */
5292 if (!finalize_imm ())
5293 return;
252b5132 5294
40fb9820 5295 if (i.types[0].bitfield.imm1)
29b0f896 5296 i.imm_operands = 0; /* kludge for shift insns. */
252b5132 5297
9afe6eb8
L
5298 /* We only need to check those implicit registers for instructions
5299 with 3 operands or less. */
5300 if (i.operands <= 3)
5301 for (j = 0; j < i.operands; j++)
75e5731b
JB
5302 if (i.types[j].bitfield.instance != InstanceNone
5303 && !i.types[j].bitfield.xmmword)
9afe6eb8 5304 i.reg_operands--;
40fb9820 5305
29b0f896
AM
5306 /* For insns with operands there are more diddles to do to the opcode. */
5307 if (i.operands)
5308 {
5309 if (!process_operands ())
5310 return;
5311 }
255571cd 5312 else if (!quiet_warnings && i.tm.opcode_modifier.operandconstraint == UGH)
29b0f896
AM
5313 {
5314 /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc. */
5315 as_warn (_("translating to `%sp'"), i.tm.name);
5316 }
252b5132 5317
7a8655d2 5318 if (is_any_vex_encoding (&i.tm))
9e5e5283 5319 {
c1dc7af5 5320 if (!cpu_arch_flags.bitfield.cpui286)
9e5e5283 5321 {
c1dc7af5 5322 as_bad (_("instruction `%s' isn't supported outside of protected mode."),
9e5e5283
L
5323 i.tm.name);
5324 return;
5325 }
c0f3af97 5326
0b9404fd
JB
5327 /* Check for explicit REX prefix. */
5328 if (i.prefix[REX_PREFIX] || i.rex_encoding)
5329 {
5330 as_bad (_("REX prefix invalid with `%s'"), i.tm.name);
5331 return;
5332 }
5333
9e5e5283
L
5334 if (i.tm.opcode_modifier.vex)
5335 build_vex_prefix (t);
5336 else
5337 build_evex_prefix ();
0b9404fd
JB
5338
5339 /* The individual REX.RXBW bits got consumed. */
5340 i.rex &= REX_OPCODE;
9e5e5283 5341 }
43234a1e 5342
5dd85c99
SP
5343 /* Handle conversion of 'int $3' --> special int3 insn. XOP or FMA4
5344 instructions may define INT_OPCODE as well, so avoid this corner
5345 case for those instructions that use MODRM. */
389d00a5
JB
5346 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
5347 && i.tm.base_opcode == INT_OPCODE
a6461c02
SP
5348 && !i.tm.opcode_modifier.modrm
5349 && i.op[0].imms->X_add_number == 3)
29b0f896
AM
5350 {
5351 i.tm.base_opcode = INT3_OPCODE;
5352 i.imm_operands = 0;
5353 }
252b5132 5354
0cfa3eb3
JB
5355 if ((i.tm.opcode_modifier.jump == JUMP
5356 || i.tm.opcode_modifier.jump == JUMP_BYTE
5357 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896
AM
5358 && i.op[0].disps->X_op == O_constant)
5359 {
5360 /* Convert "jmp constant" (and "call constant") to a jump (call) to
5361 the absolute address given by the constant. Since ix86 jumps and
5362 calls are pc relative, we need to generate a reloc. */
5363 i.op[0].disps->X_add_symbol = &abs_symbol;
5364 i.op[0].disps->X_op = O_symbol;
5365 }
252b5132 5366
29b0f896
AM
5367 /* For 8 bit registers we need an empty rex prefix. Also if the
5368 instruction already has a prefix, we need to convert old
5369 registers to new ones. */
773f551c 5370
bab6aec1 5371 if ((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte
29b0f896 5372 && (i.op[0].regs->reg_flags & RegRex64) != 0)
bab6aec1 5373 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte
29b0f896 5374 && (i.op[1].regs->reg_flags & RegRex64) != 0)
bab6aec1
JB
5375 || (((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte)
5376 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte))
29b0f896
AM
5377 && i.rex != 0))
5378 {
5379 int x;
726c5dcd 5380
29b0f896
AM
5381 i.rex |= REX_OPCODE;
5382 for (x = 0; x < 2; x++)
5383 {
5384 /* Look for 8 bit operand that uses old registers. */
bab6aec1 5385 if (i.types[x].bitfield.class == Reg && i.types[x].bitfield.byte
29b0f896 5386 && (i.op[x].regs->reg_flags & RegRex64) == 0)
773f551c 5387 {
3f93af61 5388 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
29b0f896
AM
5389 /* In case it is "hi" register, give up. */
5390 if (i.op[x].regs->reg_num > 3)
a540244d 5391 as_bad (_("can't encode register '%s%s' in an "
4eed87de 5392 "instruction requiring REX prefix."),
a540244d 5393 register_prefix, i.op[x].regs->reg_name);
773f551c 5394
29b0f896
AM
5395 /* Otherwise it is equivalent to the extended register.
5396 Since the encoding doesn't change this is merely
5397 cosmetic cleanup for debug output. */
5398
5399 i.op[x].regs = i.op[x].regs + 8;
773f551c 5400 }
29b0f896
AM
5401 }
5402 }
773f551c 5403
6b6b6807
L
5404 if (i.rex == 0 && i.rex_encoding)
5405 {
5406 /* Check if we can add a REX_OPCODE byte. Look for 8 bit operand
3f93af61 5407 that uses legacy register. If it is "hi" register, don't add
6b6b6807
L
5408 the REX_OPCODE byte. */
5409 int x;
5410 for (x = 0; x < 2; x++)
bab6aec1 5411 if (i.types[x].bitfield.class == Reg
6b6b6807
L
5412 && i.types[x].bitfield.byte
5413 && (i.op[x].regs->reg_flags & RegRex64) == 0
5414 && i.op[x].regs->reg_num > 3)
5415 {
3f93af61 5416 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
5b7c81bd 5417 i.rex_encoding = false;
6b6b6807
L
5418 break;
5419 }
5420
5421 if (i.rex_encoding)
5422 i.rex = REX_OPCODE;
5423 }
5424
7ab9ffdd 5425 if (i.rex != 0)
29b0f896
AM
5426 add_prefix (REX_OPCODE | i.rex);
5427
ae531041
L
5428 insert_lfence_before ();
5429
29b0f896
AM
5430 /* We are ready to output the insn. */
5431 output_insn ();
e379e5f3 5432
ae531041
L
5433 insert_lfence_after ();
5434
e379e5f3
L
5435 last_insn.seg = now_seg;
5436
5437 if (i.tm.opcode_modifier.isprefix)
5438 {
5439 last_insn.kind = last_insn_prefix;
5440 last_insn.name = i.tm.name;
5441 last_insn.file = as_where (&last_insn.line);
5442 }
5443 else
5444 last_insn.kind = last_insn_other;
29b0f896
AM
5445}
5446
9db83a32
JB
5447/* The Q suffix is generally valid only in 64-bit mode, with very few
5448 exceptions: fild, fistp, fisttp, and cmpxchg8b. Note that for fild
5449 and fisttp only one of their two templates is matched below: That's
5450 sufficient since other relevant attributes are the same between both
5451 respective templates. */
5452static INLINE bool q_suffix_allowed(const insn_template *t)
5453{
5454 return flag_code == CODE_64BIT
5455 || (t->opcode_modifier.opcodespace == SPACE_BASE
5456 && t->base_opcode == 0xdf
5457 && (t->extension_opcode & 1)) /* fild / fistp / fisttp */
5458 || (t->opcode_modifier.opcodespace == SPACE_0F
5459 && t->base_opcode == 0xc7
5460 && t->opcode_modifier.opcodeprefix == PREFIX_NONE
5461 && t->extension_opcode == 1) /* cmpxchg8b */;
5462}
5463
5317ad2c
JB
5464static const char *
5465parse_insn (const char *line, char *mnemonic)
29b0f896 5466{
5317ad2c 5467 const char *l = line, *token_start = l;
29b0f896 5468 char *mnem_p;
04784e33 5469 bool pass1 = !current_templates;
5c6af06e 5470 int supported;
d3ce72d0 5471 const insn_template *t;
b6169b20 5472 char *dot_p = NULL;
29b0f896 5473
29b0f896
AM
5474 while (1)
5475 {
5476 mnem_p = mnemonic;
5477 while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
5478 {
b6169b20
L
5479 if (*mnem_p == '.')
5480 dot_p = mnem_p;
29b0f896
AM
5481 mnem_p++;
5482 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
45288df1 5483 {
29b0f896
AM
5484 as_bad (_("no such instruction: `%s'"), token_start);
5485 return NULL;
5486 }
5487 l++;
5488 }
5489 if (!is_space_char (*l)
5490 && *l != END_OF_INSN
e44823cf
JB
5491 && (intel_syntax
5492 || (*l != PREFIX_SEPARATOR
5493 && *l != ',')))
29b0f896
AM
5494 {
5495 as_bad (_("invalid character %s in mnemonic"),
5496 output_invalid (*l));
5497 return NULL;
5498 }
5499 if (token_start == l)
5500 {
e44823cf 5501 if (!intel_syntax && *l == PREFIX_SEPARATOR)
29b0f896
AM
5502 as_bad (_("expecting prefix; got nothing"));
5503 else
5504 as_bad (_("expecting mnemonic; got nothing"));
5505 return NULL;
5506 }
45288df1 5507
29b0f896 5508 /* Look up instruction (or prefix) via hash table. */
629310ab 5509 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
47926f60 5510
29b0f896
AM
5511 if (*l != END_OF_INSN
5512 && (!is_space_char (*l) || l[1] != END_OF_INSN)
5513 && current_templates
40fb9820 5514 && current_templates->start->opcode_modifier.isprefix)
29b0f896 5515 {
c6fb90c8 5516 if (!cpu_flags_check_cpu64 (current_templates->start->cpu_flags))
2dd88dca
JB
5517 {
5518 as_bad ((flag_code != CODE_64BIT
5519 ? _("`%s' is only supported in 64-bit mode")
5520 : _("`%s' is not supported in 64-bit mode")),
5521 current_templates->start->name);
5522 return NULL;
5523 }
29b0f896
AM
5524 /* If we are in 16-bit mode, do not allow addr16 or data16.
5525 Similarly, in 32-bit mode, do not allow addr32 or data32. */
673fe0f0
JB
5526 if ((current_templates->start->opcode_modifier.size == SIZE16
5527 || current_templates->start->opcode_modifier.size == SIZE32)
29b0f896 5528 && flag_code != CODE_64BIT
673fe0f0 5529 && ((current_templates->start->opcode_modifier.size == SIZE32)
29b0f896
AM
5530 ^ (flag_code == CODE_16BIT)))
5531 {
5532 as_bad (_("redundant %s prefix"),
5533 current_templates->start->name);
5534 return NULL;
45288df1 5535 }
31184569
JB
5536
5537 if (current_templates->start->base_opcode == PSEUDO_PREFIX)
29b0f896 5538 {
86fa6981 5539 /* Handle pseudo prefixes. */
31184569 5540 switch (current_templates->start->extension_opcode)
86fa6981 5541 {
41eb8e88 5542 case Prefix_Disp8:
86fa6981
L
5543 /* {disp8} */
5544 i.disp_encoding = disp_encoding_8bit;
5545 break;
41eb8e88
L
5546 case Prefix_Disp16:
5547 /* {disp16} */
5548 i.disp_encoding = disp_encoding_16bit;
5549 break;
5550 case Prefix_Disp32:
86fa6981
L
5551 /* {disp32} */
5552 i.disp_encoding = disp_encoding_32bit;
5553 break;
41eb8e88 5554 case Prefix_Load:
86fa6981
L
5555 /* {load} */
5556 i.dir_encoding = dir_encoding_load;
5557 break;
41eb8e88 5558 case Prefix_Store:
86fa6981
L
5559 /* {store} */
5560 i.dir_encoding = dir_encoding_store;
5561 break;
41eb8e88 5562 case Prefix_VEX:
42e04b36
L
5563 /* {vex} */
5564 i.vec_encoding = vex_encoding_vex;
86fa6981 5565 break;
41eb8e88 5566 case Prefix_VEX3:
86fa6981
L
5567 /* {vex3} */
5568 i.vec_encoding = vex_encoding_vex3;
5569 break;
41eb8e88 5570 case Prefix_EVEX:
86fa6981
L
5571 /* {evex} */
5572 i.vec_encoding = vex_encoding_evex;
5573 break;
41eb8e88 5574 case Prefix_REX:
6b6b6807 5575 /* {rex} */
5b7c81bd 5576 i.rex_encoding = true;
6b6b6807 5577 break;
41eb8e88 5578 case Prefix_NoOptimize:
b6f8c7c4 5579 /* {nooptimize} */
5b7c81bd 5580 i.no_optimize = true;
b6f8c7c4 5581 break;
86fa6981
L
5582 default:
5583 abort ();
5584 }
5585 }
5586 else
5587 {
5588 /* Add prefix, checking for repeated prefixes. */
4e9ac44a 5589 switch (add_prefix (current_templates->start->base_opcode))
86fa6981 5590 {
4e9ac44a
L
5591 case PREFIX_EXIST:
5592 return NULL;
5593 case PREFIX_DS:
d777820b 5594 if (current_templates->start->cpu_flags.bitfield.cpuibt)
4e9ac44a
L
5595 i.notrack_prefix = current_templates->start->name;
5596 break;
5597 case PREFIX_REP:
5598 if (current_templates->start->cpu_flags.bitfield.cpuhle)
5599 i.hle_prefix = current_templates->start->name;
5600 else if (current_templates->start->cpu_flags.bitfield.cpumpx)
5601 i.bnd_prefix = current_templates->start->name;
5602 else
5603 i.rep_prefix = current_templates->start->name;
5604 break;
5605 default:
5606 break;
86fa6981 5607 }
29b0f896
AM
5608 }
5609 /* Skip past PREFIX_SEPARATOR and reset token_start. */
5610 token_start = ++l;
5611 }
5612 else
5613 break;
5614 }
45288df1 5615
30a55f88 5616 if (!current_templates)
b6169b20 5617 {
07d5e953
JB
5618 /* Deprecated functionality (new code should use pseudo-prefixes instead):
5619 Check if we should swap operand or force 32bit displacement in
f8a5c266 5620 encoding. */
30a55f88 5621 if (mnem_p - 2 == dot_p && dot_p[1] == 's')
64c49ab3 5622 i.dir_encoding = dir_encoding_swap;
8d63c93e 5623 else if (mnem_p - 3 == dot_p
a501d77e
L
5624 && dot_p[1] == 'd'
5625 && dot_p[2] == '8')
5626 i.disp_encoding = disp_encoding_8bit;
8d63c93e 5627 else if (mnem_p - 4 == dot_p
f8a5c266
L
5628 && dot_p[1] == 'd'
5629 && dot_p[2] == '3'
5630 && dot_p[3] == '2')
a501d77e 5631 i.disp_encoding = disp_encoding_32bit;
30a55f88
L
5632 else
5633 goto check_suffix;
5634 mnem_p = dot_p;
5635 *dot_p = '\0';
629310ab 5636 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
b6169b20
L
5637 }
5638
04784e33 5639 if (!current_templates || !pass1)
29b0f896 5640 {
04784e33
JB
5641 current_templates = NULL;
5642
dc1e8a47 5643 check_suffix:
1c529385 5644 if (mnem_p > mnemonic)
29b0f896 5645 {
1c529385
LH
5646 /* See if we can get a match by trimming off a suffix. */
5647 switch (mnem_p[-1])
29b0f896 5648 {
1c529385
LH
5649 case WORD_MNEM_SUFFIX:
5650 if (intel_syntax && (intel_float_operand (mnemonic) & 2))
29b0f896
AM
5651 i.suffix = SHORT_MNEM_SUFFIX;
5652 else
1c529385
LH
5653 /* Fall through. */
5654 case BYTE_MNEM_SUFFIX:
5655 case QWORD_MNEM_SUFFIX:
5656 i.suffix = mnem_p[-1];
29b0f896 5657 mnem_p[-1] = '\0';
fe0e921f
AM
5658 current_templates
5659 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5660 break;
5661 case SHORT_MNEM_SUFFIX:
5662 case LONG_MNEM_SUFFIX:
5663 if (!intel_syntax)
5664 {
5665 i.suffix = mnem_p[-1];
5666 mnem_p[-1] = '\0';
fe0e921f
AM
5667 current_templates
5668 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5669 }
5670 break;
5671
5672 /* Intel Syntax. */
5673 case 'd':
5674 if (intel_syntax)
5675 {
5676 if (intel_float_operand (mnemonic) == 1)
5677 i.suffix = SHORT_MNEM_SUFFIX;
5678 else
5679 i.suffix = LONG_MNEM_SUFFIX;
5680 mnem_p[-1] = '\0';
fe0e921f
AM
5681 current_templates
5682 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385 5683 }
04784e33
JB
5684 /* For compatibility reasons accept MOVSD and CMPSD without
5685 operands even in AT&T mode. */
5686 else if (*l == END_OF_INSN
5687 || (is_space_char (*l) && l[1] == END_OF_INSN))
5688 {
5689 mnem_p[-1] = '\0';
5690 current_templates
5691 = (const templates *) str_hash_find (op_hash, mnemonic);
5692 if (current_templates != NULL
5693 /* MOVS or CMPS */
5694 && (current_templates->start->base_opcode | 2) == 0xa6
5695 && current_templates->start->opcode_modifier.opcodespace
5696 == SPACE_BASE
5697 && mnem_p[-2] == 's')
5698 {
5699 as_warn (_("found `%sd'; assuming `%sl' was meant"),
5700 mnemonic, mnemonic);
5701 i.suffix = LONG_MNEM_SUFFIX;
5702 }
5703 else
5704 {
5705 current_templates = NULL;
5706 mnem_p[-1] = 'd';
5707 }
5708 }
1c529385 5709 break;
29b0f896 5710 }
29b0f896 5711 }
1c529385 5712
29b0f896
AM
5713 if (!current_templates)
5714 {
04784e33
JB
5715 if (pass1)
5716 as_bad (_("no such instruction: `%s'"), token_start);
29b0f896
AM
5717 return NULL;
5718 }
5719 }
252b5132 5720
0cfa3eb3
JB
5721 if (current_templates->start->opcode_modifier.jump == JUMP
5722 || current_templates->start->opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
5723 {
5724 /* Check for a branch hint. We allow ",pt" and ",pn" for
5725 predict taken and predict not taken respectively.
5726 I'm not sure that branch hints actually do anything on loop
5727 and jcxz insns (JumpByte) for current Pentium4 chips. They
5728 may work in the future and it doesn't hurt to accept them
5729 now. */
5730 if (l[0] == ',' && l[1] == 'p')
5731 {
5732 if (l[2] == 't')
5733 {
5734 if (!add_prefix (DS_PREFIX_OPCODE))
5735 return NULL;
5736 l += 3;
5737 }
5738 else if (l[2] == 'n')
5739 {
5740 if (!add_prefix (CS_PREFIX_OPCODE))
5741 return NULL;
5742 l += 3;
5743 }
5744 }
5745 }
5746 /* Any other comma loses. */
5747 if (*l == ',')
5748 {
5749 as_bad (_("invalid character %s in mnemonic"),
5750 output_invalid (*l));
5751 return NULL;
5752 }
252b5132 5753
29b0f896 5754 /* Check if instruction is supported on specified architecture. */
5c6af06e
JB
5755 supported = 0;
5756 for (t = current_templates->start; t < current_templates->end; ++t)
5757 {
c0f3af97 5758 supported |= cpu_flags_match (t);
9db83a32
JB
5759
5760 if (i.suffix == QWORD_MNEM_SUFFIX && !q_suffix_allowed (t))
5761 supported &= ~CPU_FLAGS_64BIT_MATCH;
5762
c0f3af97 5763 if (supported == CPU_FLAGS_PERFECT_MATCH)
d59a54c2 5764 return l;
29b0f896 5765 }
3629bb00 5766
9db83a32
JB
5767 if (pass1)
5768 {
5769 if (supported & CPU_FLAGS_64BIT_MATCH)
5770 i.error = unsupported_on_arch;
5771 else
5772 i.error = unsupported_64bit;
5773 }
252b5132 5774
548d0ee6 5775 return NULL;
29b0f896 5776}
252b5132 5777
29b0f896 5778static char *
e3bb37b5 5779parse_operands (char *l, const char *mnemonic)
29b0f896
AM
5780{
5781 char *token_start;
3138f287 5782
29b0f896
AM
5783 /* 1 if operand is pending after ','. */
5784 unsigned int expecting_operand = 0;
252b5132 5785
29b0f896
AM
5786 while (*l != END_OF_INSN)
5787 {
e68c3d59
JB
5788 /* Non-zero if operand parens not balanced. */
5789 unsigned int paren_not_balanced = 0;
5790 /* True if inside double quotes. */
5791 bool in_quotes = false;
5792
29b0f896
AM
5793 /* Skip optional white space before operand. */
5794 if (is_space_char (*l))
5795 ++l;
d02603dc 5796 if (!is_operand_char (*l) && *l != END_OF_INSN && *l != '"')
29b0f896
AM
5797 {
5798 as_bad (_("invalid character %s before operand %d"),
5799 output_invalid (*l),
5800 i.operands + 1);
5801 return NULL;
5802 }
d02603dc 5803 token_start = l; /* After white space. */
e68c3d59 5804 while (in_quotes || paren_not_balanced || *l != ',')
29b0f896
AM
5805 {
5806 if (*l == END_OF_INSN)
5807 {
e68c3d59
JB
5808 if (in_quotes)
5809 {
5810 as_bad (_("unbalanced double quotes in operand %d."),
5811 i.operands + 1);
5812 return NULL;
5813 }
29b0f896
AM
5814 if (paren_not_balanced)
5815 {
98ff9f1c
JB
5816 know (!intel_syntax);
5817 as_bad (_("unbalanced parenthesis in operand %d."),
5818 i.operands + 1);
29b0f896
AM
5819 return NULL;
5820 }
5821 else
5822 break; /* we are done */
5823 }
e68c3d59
JB
5824 else if (*l == '\\' && l[1] == '"')
5825 ++l;
5826 else if (*l == '"')
5827 in_quotes = !in_quotes;
5828 else if (!in_quotes && !is_operand_char (*l) && !is_space_char (*l))
29b0f896
AM
5829 {
5830 as_bad (_("invalid character %s in operand %d"),
5831 output_invalid (*l),
5832 i.operands + 1);
5833 return NULL;
5834 }
e68c3d59 5835 if (!intel_syntax && !in_quotes)
29b0f896
AM
5836 {
5837 if (*l == '(')
5838 ++paren_not_balanced;
5839 if (*l == ')')
5840 --paren_not_balanced;
5841 }
29b0f896
AM
5842 l++;
5843 }
5844 if (l != token_start)
5845 { /* Yes, we've read in another operand. */
5846 unsigned int operand_ok;
5847 this_operand = i.operands++;
5848 if (i.operands > MAX_OPERANDS)
5849 {
5850 as_bad (_("spurious operands; (%d operands/instruction max)"),
5851 MAX_OPERANDS);
5852 return NULL;
5853 }
9d46ce34 5854 i.types[this_operand].bitfield.unspecified = 1;
29b0f896
AM
5855 /* Now parse operand adding info to 'i' as we go along. */
5856 END_STRING_AND_SAVE (l);
5857
1286ab78
L
5858 if (i.mem_operands > 1)
5859 {
5860 as_bad (_("too many memory references for `%s'"),
5861 mnemonic);
5862 return 0;
5863 }
5864
29b0f896
AM
5865 if (intel_syntax)
5866 operand_ok =
5867 i386_intel_operand (token_start,
5868 intel_float_operand (mnemonic));
5869 else
a7619375 5870 operand_ok = i386_att_operand (token_start);
29b0f896
AM
5871
5872 RESTORE_END_STRING (l);
5873 if (!operand_ok)
5874 return NULL;
5875 }
5876 else
5877 {
5878 if (expecting_operand)
5879 {
5880 expecting_operand_after_comma:
5881 as_bad (_("expecting operand after ','; got nothing"));
5882 return NULL;
5883 }
5884 if (*l == ',')
5885 {
5886 as_bad (_("expecting operand before ','; got nothing"));
5887 return NULL;
5888 }
5889 }
7f3f1ea2 5890
29b0f896
AM
5891 /* Now *l must be either ',' or END_OF_INSN. */
5892 if (*l == ',')
5893 {
5894 if (*++l == END_OF_INSN)
5895 {
5896 /* Just skip it, if it's \n complain. */
5897 goto expecting_operand_after_comma;
5898 }
5899 expecting_operand = 1;
5900 }
5901 }
5902 return l;
5903}
7f3f1ea2 5904
050dfa73 5905static void
783c187b 5906swap_2_operands (unsigned int xchg1, unsigned int xchg2)
050dfa73
MM
5907{
5908 union i386_op temp_op;
40fb9820 5909 i386_operand_type temp_type;
c48dadc9 5910 unsigned int temp_flags;
050dfa73 5911 enum bfd_reloc_code_real temp_reloc;
4eed87de 5912
050dfa73
MM
5913 temp_type = i.types[xchg2];
5914 i.types[xchg2] = i.types[xchg1];
5915 i.types[xchg1] = temp_type;
c48dadc9
JB
5916
5917 temp_flags = i.flags[xchg2];
5918 i.flags[xchg2] = i.flags[xchg1];
5919 i.flags[xchg1] = temp_flags;
5920
050dfa73
MM
5921 temp_op = i.op[xchg2];
5922 i.op[xchg2] = i.op[xchg1];
5923 i.op[xchg1] = temp_op;
c48dadc9 5924
050dfa73
MM
5925 temp_reloc = i.reloc[xchg2];
5926 i.reloc[xchg2] = i.reloc[xchg1];
5927 i.reloc[xchg1] = temp_reloc;
43234a1e 5928
6225c532 5929 if (i.mask.reg)
43234a1e 5930 {
6225c532
JB
5931 if (i.mask.operand == xchg1)
5932 i.mask.operand = xchg2;
5933 else if (i.mask.operand == xchg2)
5934 i.mask.operand = xchg1;
43234a1e 5935 }
a5748e0d 5936 if (i.broadcast.type || i.broadcast.bytes)
43234a1e 5937 {
5273a3cd
JB
5938 if (i.broadcast.operand == xchg1)
5939 i.broadcast.operand = xchg2;
5940 else if (i.broadcast.operand == xchg2)
5941 i.broadcast.operand = xchg1;
43234a1e 5942 }
050dfa73
MM
5943}
5944
29b0f896 5945static void
e3bb37b5 5946swap_operands (void)
29b0f896 5947{
b7c61d9a 5948 switch (i.operands)
050dfa73 5949 {
c0f3af97 5950 case 5:
b7c61d9a 5951 case 4:
4d456e3d 5952 swap_2_operands (1, i.operands - 2);
1a0670f3 5953 /* Fall through. */
b7c61d9a
L
5954 case 3:
5955 case 2:
4d456e3d 5956 swap_2_operands (0, i.operands - 1);
b7c61d9a
L
5957 break;
5958 default:
5959 abort ();
29b0f896 5960 }
29b0f896
AM
5961
5962 if (i.mem_operands == 2)
5963 {
5e042380 5964 const reg_entry *temp_seg;
29b0f896
AM
5965 temp_seg = i.seg[0];
5966 i.seg[0] = i.seg[1];
5967 i.seg[1] = temp_seg;
5968 }
5969}
252b5132 5970
29b0f896
AM
5971/* Try to ensure constant immediates are represented in the smallest
5972 opcode possible. */
5973static void
e3bb37b5 5974optimize_imm (void)
29b0f896
AM
5975{
5976 char guess_suffix = 0;
5977 int op;
252b5132 5978
29b0f896
AM
5979 if (i.suffix)
5980 guess_suffix = i.suffix;
5981 else if (i.reg_operands)
5982 {
5983 /* Figure out a suffix from the last register operand specified.
75e5731b
JB
5984 We can't do this properly yet, i.e. excluding special register
5985 instances, but the following works for instructions with
5986 immediates. In any case, we can't set i.suffix yet. */
29b0f896 5987 for (op = i.operands; --op >= 0;)
bab6aec1
JB
5988 if (i.types[op].bitfield.class != Reg)
5989 continue;
5990 else if (i.types[op].bitfield.byte)
7ab9ffdd 5991 {
40fb9820
L
5992 guess_suffix = BYTE_MNEM_SUFFIX;
5993 break;
5994 }
bab6aec1 5995 else if (i.types[op].bitfield.word)
252b5132 5996 {
40fb9820
L
5997 guess_suffix = WORD_MNEM_SUFFIX;
5998 break;
5999 }
bab6aec1 6000 else if (i.types[op].bitfield.dword)
40fb9820
L
6001 {
6002 guess_suffix = LONG_MNEM_SUFFIX;
6003 break;
6004 }
bab6aec1 6005 else if (i.types[op].bitfield.qword)
40fb9820
L
6006 {
6007 guess_suffix = QWORD_MNEM_SUFFIX;
29b0f896 6008 break;
252b5132 6009 }
29b0f896
AM
6010 }
6011 else if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
6012 guess_suffix = WORD_MNEM_SUFFIX;
6013
6014 for (op = i.operands; --op >= 0;)
40fb9820 6015 if (operand_type_check (i.types[op], imm))
29b0f896
AM
6016 {
6017 switch (i.op[op].imms->X_op)
252b5132 6018 {
29b0f896
AM
6019 case O_constant:
6020 /* If a suffix is given, this operand may be shortened. */
6021 switch (guess_suffix)
252b5132 6022 {
29b0f896 6023 case LONG_MNEM_SUFFIX:
40fb9820
L
6024 i.types[op].bitfield.imm32 = 1;
6025 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
6026 break;
6027 case WORD_MNEM_SUFFIX:
40fb9820
L
6028 i.types[op].bitfield.imm16 = 1;
6029 i.types[op].bitfield.imm32 = 1;
6030 i.types[op].bitfield.imm32s = 1;
6031 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
6032 break;
6033 case BYTE_MNEM_SUFFIX:
40fb9820
L
6034 i.types[op].bitfield.imm8 = 1;
6035 i.types[op].bitfield.imm8s = 1;
6036 i.types[op].bitfield.imm16 = 1;
6037 i.types[op].bitfield.imm32 = 1;
6038 i.types[op].bitfield.imm32s = 1;
6039 i.types[op].bitfield.imm64 = 1;
29b0f896 6040 break;
252b5132 6041 }
252b5132 6042
29b0f896
AM
6043 /* If this operand is at most 16 bits, convert it
6044 to a signed 16 bit number before trying to see
6045 whether it will fit in an even smaller size.
6046 This allows a 16-bit operand such as $0xffe0 to
6047 be recognised as within Imm8S range. */
40fb9820 6048 if ((i.types[op].bitfield.imm16)
7e96fb68 6049 && fits_in_unsigned_word (i.op[op].imms->X_add_number))
252b5132 6050 {
87ed972d
JB
6051 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
6052 ^ 0x8000) - 0x8000);
29b0f896 6053 }
a28def75
L
6054#ifdef BFD64
6055 /* Store 32-bit immediate in 64-bit for 64-bit BFD. */
40fb9820 6056 if ((i.types[op].bitfield.imm32)
7e96fb68 6057 && fits_in_unsigned_long (i.op[op].imms->X_add_number))
29b0f896
AM
6058 {
6059 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
6060 ^ ((offsetT) 1 << 31))
6061 - ((offsetT) 1 << 31));
6062 }
a28def75 6063#endif
40fb9820 6064 i.types[op]
c6fb90c8
L
6065 = operand_type_or (i.types[op],
6066 smallest_imm_type (i.op[op].imms->X_add_number));
252b5132 6067
29b0f896
AM
6068 /* We must avoid matching of Imm32 templates when 64bit
6069 only immediate is available. */
6070 if (guess_suffix == QWORD_MNEM_SUFFIX)
40fb9820 6071 i.types[op].bitfield.imm32 = 0;
29b0f896 6072 break;
252b5132 6073
29b0f896
AM
6074 case O_absent:
6075 case O_register:
6076 abort ();
6077
6078 /* Symbols and expressions. */
6079 default:
9cd96992
JB
6080 /* Convert symbolic operand to proper sizes for matching, but don't
6081 prevent matching a set of insns that only supports sizes other
6082 than those matching the insn suffix. */
6083 {
40fb9820 6084 i386_operand_type mask, allowed;
87ed972d 6085 const insn_template *t = current_templates->start;
9cd96992 6086
0dfbf9d7 6087 operand_type_set (&mask, 0);
9cd96992
JB
6088 switch (guess_suffix)
6089 {
6090 case QWORD_MNEM_SUFFIX:
40fb9820
L
6091 mask.bitfield.imm64 = 1;
6092 mask.bitfield.imm32s = 1;
9cd96992
JB
6093 break;
6094 case LONG_MNEM_SUFFIX:
40fb9820 6095 mask.bitfield.imm32 = 1;
9cd96992
JB
6096 break;
6097 case WORD_MNEM_SUFFIX:
40fb9820 6098 mask.bitfield.imm16 = 1;
9cd96992
JB
6099 break;
6100 case BYTE_MNEM_SUFFIX:
40fb9820 6101 mask.bitfield.imm8 = 1;
9cd96992
JB
6102 break;
6103 default:
9cd96992
JB
6104 break;
6105 }
8f0212ac
JB
6106
6107 allowed = operand_type_and (t->operand_types[op], mask);
6108 while (++t < current_templates->end)
6109 {
6110 allowed = operand_type_or (allowed, t->operand_types[op]);
6111 allowed = operand_type_and (allowed, mask);
6112 }
6113
0dfbf9d7 6114 if (!operand_type_all_zero (&allowed))
c6fb90c8 6115 i.types[op] = operand_type_and (i.types[op], mask);
9cd96992 6116 }
29b0f896 6117 break;
252b5132 6118 }
29b0f896
AM
6119 }
6120}
47926f60 6121
29b0f896
AM
6122/* Try to use the smallest displacement type too. */
6123static void
e3bb37b5 6124optimize_disp (void)
29b0f896
AM
6125{
6126 int op;
3e73aa7c 6127
29b0f896 6128 for (op = i.operands; --op >= 0;)
40fb9820 6129 if (operand_type_check (i.types[op], disp))
252b5132 6130 {
b300c311 6131 if (i.op[op].disps->X_op == O_constant)
252b5132 6132 {
91d6fa6a 6133 offsetT op_disp = i.op[op].disps->X_add_number;
29b0f896 6134
91d6fa6a 6135 if (!op_disp && i.types[op].bitfield.baseindex)
b300c311 6136 {
2f2be86b
JB
6137 i.types[op] = operand_type_and_not (i.types[op], anydisp);
6138 i.op[op].disps = NULL;
b300c311 6139 i.disp_operands--;
f185acdd
JB
6140 continue;
6141 }
6142
6143 if (i.types[op].bitfield.disp16
cd613c1f 6144 && fits_in_unsigned_word (op_disp))
f185acdd
JB
6145 {
6146 /* If this operand is at most 16 bits, convert
6147 to a signed 16 bit number and don't use 64bit
6148 displacement. */
6149 op_disp = ((op_disp ^ 0x8000) - 0x8000);
6150 i.types[op].bitfield.disp64 = 0;
b300c311 6151 }
f185acdd 6152
28a167a4 6153#ifdef BFD64
a50187b2 6154 /* Optimize 64-bit displacement to 32-bit for 64-bit BFD. */
a775efc8
JB
6155 if ((flag_code != CODE_64BIT
6156 ? i.types[op].bitfield.disp32
6157 : want_disp32 (current_templates->start)
6158 && (!current_templates->start->opcode_modifier.jump
6159 || i.jumpabsolute || i.types[op].bitfield.baseindex))
a50187b2 6160 && fits_in_unsigned_long (op_disp))
b300c311 6161 {
a50187b2
JB
6162 /* If this operand is at most 32 bits, convert
6163 to a signed 32 bit number and don't use 64bit
6164 displacement. */
6165 op_disp = (op_disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
6166 i.types[op].bitfield.disp64 = 0;
6167 i.types[op].bitfield.disp32 = 1;
6168 }
28a167a4 6169
a50187b2
JB
6170 if (flag_code == CODE_64BIT && fits_in_signed_long (op_disp))
6171 {
6172 i.types[op].bitfield.disp64 = 0;
a775efc8 6173 i.types[op].bitfield.disp32 = 1;
b300c311 6174 }
28a167a4 6175#endif
40fb9820 6176 if ((i.types[op].bitfield.disp32
40fb9820 6177 || i.types[op].bitfield.disp16)
b5014f7a 6178 && fits_in_disp8 (op_disp))
40fb9820 6179 i.types[op].bitfield.disp8 = 1;
77c59789
JB
6180
6181 i.op[op].disps->X_add_number = op_disp;
252b5132 6182 }
67a4f2b7
AO
6183 else if (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
6184 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL)
6185 {
6186 fix_new_exp (frag_now, frag_more (0) - frag_now->fr_literal, 0,
6187 i.op[op].disps, 0, i.reloc[op]);
2f2be86b 6188 i.types[op] = operand_type_and_not (i.types[op], anydisp);
67a4f2b7
AO
6189 }
6190 else
b300c311 6191 /* We only support 64bit displacement on constants. */
40fb9820 6192 i.types[op].bitfield.disp64 = 0;
252b5132 6193 }
29b0f896
AM
6194}
6195
4a1b91ea
L
6196/* Return 1 if there is a match in broadcast bytes between operand
6197 GIVEN and instruction template T. */
6198
6199static INLINE int
6200match_broadcast_size (const insn_template *t, unsigned int given)
6201{
6202 return ((t->opcode_modifier.broadcast == BYTE_BROADCAST
6203 && i.types[given].bitfield.byte)
6204 || (t->opcode_modifier.broadcast == WORD_BROADCAST
6205 && i.types[given].bitfield.word)
6206 || (t->opcode_modifier.broadcast == DWORD_BROADCAST
6207 && i.types[given].bitfield.dword)
6208 || (t->opcode_modifier.broadcast == QWORD_BROADCAST
6209 && i.types[given].bitfield.qword));
6210}
6211
6c30d220
L
6212/* Check if operands are valid for the instruction. */
6213
6214static int
6215check_VecOperands (const insn_template *t)
6216{
43234a1e 6217 unsigned int op;
e2195274 6218 i386_cpu_flags cpu;
e2195274
JB
6219
6220 /* Templates allowing for ZMMword as well as YMMword and/or XMMword for
6221 any one operand are implicity requiring AVX512VL support if the actual
6222 operand size is YMMword or XMMword. Since this function runs after
6223 template matching, there's no need to check for YMMword/XMMword in
6224 the template. */
6225 cpu = cpu_flags_and (t->cpu_flags, avx512);
6226 if (!cpu_flags_all_zero (&cpu)
6227 && !t->cpu_flags.bitfield.cpuavx512vl
6228 && !cpu_arch_flags.bitfield.cpuavx512vl)
6229 {
6230 for (op = 0; op < t->operands; ++op)
6231 {
6232 if (t->operand_types[op].bitfield.zmmword
6233 && (i.types[op].bitfield.ymmword
6234 || i.types[op].bitfield.xmmword))
6235 {
6236 i.error = unsupported;
6237 return 1;
6238 }
6239 }
6240 }
43234a1e 6241
22c36940
JB
6242 /* Somewhat similarly, templates specifying both AVX and AVX2 are
6243 requiring AVX2 support if the actual operand size is YMMword. */
6244 if (t->cpu_flags.bitfield.cpuavx
6245 && t->cpu_flags.bitfield.cpuavx2
6246 && !cpu_arch_flags.bitfield.cpuavx2)
6247 {
6248 for (op = 0; op < t->operands; ++op)
6249 {
6250 if (t->operand_types[op].bitfield.xmmword
6251 && i.types[op].bitfield.ymmword)
6252 {
6253 i.error = unsupported;
6254 return 1;
6255 }
6256 }
6257 }
6258
6c30d220 6259 /* Without VSIB byte, we can't have a vector register for index. */
63112cd6 6260 if (!t->opcode_modifier.sib
6c30d220 6261 && i.index_reg
1b54b8d7
JB
6262 && (i.index_reg->reg_type.bitfield.xmmword
6263 || i.index_reg->reg_type.bitfield.ymmword
6264 || i.index_reg->reg_type.bitfield.zmmword))
6c30d220
L
6265 {
6266 i.error = unsupported_vector_index_register;
6267 return 1;
6268 }
6269
ad8ecc81 6270 /* Check if default mask is allowed. */
255571cd 6271 if (t->opcode_modifier.operandconstraint == NO_DEFAULT_MASK
6225c532 6272 && (!i.mask.reg || i.mask.reg->reg_num == 0))
ad8ecc81
MZ
6273 {
6274 i.error = no_default_mask;
6275 return 1;
6276 }
6277
7bab8ab5
JB
6278 /* For VSIB byte, we need a vector register for index, and all vector
6279 registers must be distinct. */
260cd341 6280 if (t->opcode_modifier.sib && t->opcode_modifier.sib != SIBMEM)
7bab8ab5
JB
6281 {
6282 if (!i.index_reg
63112cd6 6283 || !((t->opcode_modifier.sib == VECSIB128
1b54b8d7 6284 && i.index_reg->reg_type.bitfield.xmmword)
63112cd6 6285 || (t->opcode_modifier.sib == VECSIB256
1b54b8d7 6286 && i.index_reg->reg_type.bitfield.ymmword)
63112cd6 6287 || (t->opcode_modifier.sib == VECSIB512
1b54b8d7 6288 && i.index_reg->reg_type.bitfield.zmmword)))
7bab8ab5
JB
6289 {
6290 i.error = invalid_vsib_address;
6291 return 1;
6292 }
6293
6225c532
JB
6294 gas_assert (i.reg_operands == 2 || i.mask.reg);
6295 if (i.reg_operands == 2 && !i.mask.reg)
43234a1e 6296 {
3528c362 6297 gas_assert (i.types[0].bitfield.class == RegSIMD);
1b54b8d7
JB
6298 gas_assert (i.types[0].bitfield.xmmword
6299 || i.types[0].bitfield.ymmword);
3528c362 6300 gas_assert (i.types[2].bitfield.class == RegSIMD);
1b54b8d7
JB
6301 gas_assert (i.types[2].bitfield.xmmword
6302 || i.types[2].bitfield.ymmword);
43234a1e
L
6303 if (operand_check == check_none)
6304 return 0;
6305 if (register_number (i.op[0].regs)
6306 != register_number (i.index_reg)
6307 && register_number (i.op[2].regs)
6308 != register_number (i.index_reg)
6309 && register_number (i.op[0].regs)
6310 != register_number (i.op[2].regs))
6311 return 0;
6312 if (operand_check == check_error)
6313 {
6314 i.error = invalid_vector_register_set;
6315 return 1;
6316 }
6317 as_warn (_("mask, index, and destination registers should be distinct"));
6318 }
6225c532 6319 else if (i.reg_operands == 1 && i.mask.reg)
8444f82a 6320 {
3528c362 6321 if (i.types[1].bitfield.class == RegSIMD
1b54b8d7
JB
6322 && (i.types[1].bitfield.xmmword
6323 || i.types[1].bitfield.ymmword
6324 || i.types[1].bitfield.zmmword)
8444f82a
MZ
6325 && (register_number (i.op[1].regs)
6326 == register_number (i.index_reg)))
6327 {
6328 if (operand_check == check_error)
6329 {
6330 i.error = invalid_vector_register_set;
6331 return 1;
6332 }
6333 if (operand_check != check_none)
6334 as_warn (_("index and destination registers should be distinct"));
6335 }
6336 }
43234a1e 6337 }
7bab8ab5 6338
fc141319
L
6339 /* For AMX instructions with 3 TMM register operands, all operands
6340 must be distinct. */
6341 if (i.reg_operands == 3
6342 && t->operand_types[0].bitfield.tmmword
6343 && (i.op[0].regs == i.op[1].regs
6344 || i.op[0].regs == i.op[2].regs
6345 || i.op[1].regs == i.op[2].regs))
6346 {
6347 i.error = invalid_tmm_register_set;
6348 return 1;
260cd341
LC
6349 }
6350
0cc78721
CL
6351 /* For some special instructions require that destination must be distinct
6352 from source registers. */
255571cd 6353 if (t->opcode_modifier.operandconstraint == DISTINCT_DEST)
0cc78721
CL
6354 {
6355 unsigned int dest_reg = i.operands - 1;
6356
6357 know (i.operands >= 3);
6358
6359 /* #UD if dest_reg == src1_reg or dest_reg == src2_reg. */
6360 if (i.op[dest_reg - 1].regs == i.op[dest_reg].regs
6361 || (i.reg_operands > 2
6362 && i.op[dest_reg - 2].regs == i.op[dest_reg].regs))
6363 {
6364 i.error = invalid_dest_and_src_register_set;
6365 return 1;
6366 }
6367 }
6368
43234a1e
L
6369 /* Check if broadcast is supported by the instruction and is applied
6370 to the memory operand. */
a5748e0d 6371 if (i.broadcast.type || i.broadcast.bytes)
43234a1e 6372 {
8e6e0792 6373 i386_operand_type type, overlap;
43234a1e
L
6374
6375 /* Check if specified broadcast is supported in this instruction,
4a1b91ea 6376 and its broadcast bytes match the memory operand. */
5273a3cd 6377 op = i.broadcast.operand;
8e6e0792 6378 if (!t->opcode_modifier.broadcast
c48dadc9 6379 || !(i.flags[op] & Operand_Mem)
c39e5b26 6380 || (!i.types[op].bitfield.unspecified
4a1b91ea 6381 && !match_broadcast_size (t, op)))
43234a1e
L
6382 {
6383 bad_broadcast:
6384 i.error = unsupported_broadcast;
6385 return 1;
6386 }
8e6e0792 6387
a5748e0d
JB
6388 if (i.broadcast.type)
6389 i.broadcast.bytes = ((1 << (t->opcode_modifier.broadcast - 1))
6390 * i.broadcast.type);
8e6e0792 6391 operand_type_set (&type, 0);
a5748e0d 6392 switch (get_broadcast_bytes (t, false))
8e6e0792 6393 {
4a1b91ea
L
6394 case 2:
6395 type.bitfield.word = 1;
6396 break;
6397 case 4:
6398 type.bitfield.dword = 1;
6399 break;
8e6e0792
JB
6400 case 8:
6401 type.bitfield.qword = 1;
6402 break;
6403 case 16:
6404 type.bitfield.xmmword = 1;
6405 break;
6406 case 32:
6407 type.bitfield.ymmword = 1;
6408 break;
6409 case 64:
6410 type.bitfield.zmmword = 1;
6411 break;
6412 default:
6413 goto bad_broadcast;
6414 }
6415
6416 overlap = operand_type_and (type, t->operand_types[op]);
bc49bfd8
JB
6417 if (t->operand_types[op].bitfield.class == RegSIMD
6418 && t->operand_types[op].bitfield.byte
6419 + t->operand_types[op].bitfield.word
6420 + t->operand_types[op].bitfield.dword
6421 + t->operand_types[op].bitfield.qword > 1)
6422 {
6423 overlap.bitfield.xmmword = 0;
6424 overlap.bitfield.ymmword = 0;
6425 overlap.bitfield.zmmword = 0;
6426 }
8e6e0792
JB
6427 if (operand_type_all_zero (&overlap))
6428 goto bad_broadcast;
6429
6430 if (t->opcode_modifier.checkregsize)
6431 {
6432 unsigned int j;
6433
e2195274 6434 type.bitfield.baseindex = 1;
8e6e0792
JB
6435 for (j = 0; j < i.operands; ++j)
6436 {
6437 if (j != op
6438 && !operand_type_register_match(i.types[j],
6439 t->operand_types[j],
6440 type,
6441 t->operand_types[op]))
6442 goto bad_broadcast;
6443 }
6444 }
43234a1e
L
6445 }
6446 /* If broadcast is supported in this instruction, we need to check if
6447 operand of one-element size isn't specified without broadcast. */
6448 else if (t->opcode_modifier.broadcast && i.mem_operands)
6449 {
6450 /* Find memory operand. */
6451 for (op = 0; op < i.operands; op++)
8dc0818e 6452 if (i.flags[op] & Operand_Mem)
43234a1e
L
6453 break;
6454 gas_assert (op < i.operands);
6455 /* Check size of the memory operand. */
4a1b91ea 6456 if (match_broadcast_size (t, op))
43234a1e
L
6457 {
6458 i.error = broadcast_needed;
6459 return 1;
6460 }
6461 }
c39e5b26
JB
6462 else
6463 op = MAX_OPERANDS - 1; /* Avoid uninitialized variable warning. */
43234a1e
L
6464
6465 /* Check if requested masking is supported. */
6225c532 6466 if (i.mask.reg)
43234a1e 6467 {
ae2387fe
JB
6468 switch (t->opcode_modifier.masking)
6469 {
6470 case BOTH_MASKING:
6471 break;
6472 case MERGING_MASKING:
6225c532 6473 if (i.mask.zeroing)
ae2387fe
JB
6474 {
6475 case 0:
6476 i.error = unsupported_masking;
6477 return 1;
6478 }
6479 break;
6480 case DYNAMIC_MASKING:
6481 /* Memory destinations allow only merging masking. */
6225c532 6482 if (i.mask.zeroing && i.mem_operands)
ae2387fe
JB
6483 {
6484 /* Find memory operand. */
6485 for (op = 0; op < i.operands; op++)
c48dadc9 6486 if (i.flags[op] & Operand_Mem)
ae2387fe
JB
6487 break;
6488 gas_assert (op < i.operands);
6489 if (op == i.operands - 1)
6490 {
6491 i.error = unsupported_masking;
6492 return 1;
6493 }
6494 }
6495 break;
6496 default:
6497 abort ();
6498 }
43234a1e
L
6499 }
6500
6501 /* Check if masking is applied to dest operand. */
6225c532 6502 if (i.mask.reg && (i.mask.operand != i.operands - 1))
43234a1e
L
6503 {
6504 i.error = mask_not_on_destination;
6505 return 1;
6506 }
6507
43234a1e 6508 /* Check RC/SAE. */
ca5312a2 6509 if (i.rounding.type != rc_none)
43234a1e 6510 {
a80195f1 6511 if (!t->opcode_modifier.sae
cf665fee
JB
6512 || ((i.rounding.type != saeonly) != t->opcode_modifier.staticrounding)
6513 || i.mem_operands)
43234a1e
L
6514 {
6515 i.error = unsupported_rc_sae;
6516 return 1;
6517 }
cf665fee
JB
6518
6519 /* Non-EVEX.LIG forms need to have a ZMM register as at least one
6520 operand. */
6521 if (t->opcode_modifier.evex != EVEXLIG)
7bab8ab5 6522 {
cf665fee
JB
6523 for (op = 0; op < t->operands; ++op)
6524 if (i.types[op].bitfield.zmmword)
6525 break;
6526 if (op >= t->operands)
6527 {
6528 i.error = operand_size_mismatch;
6529 return 1;
6530 }
7bab8ab5 6531 }
6c30d220
L
6532 }
6533
da4977e0
JB
6534 /* Check the special Imm4 cases; must be the first operand. */
6535 if (t->cpu_flags.bitfield.cpuxop && t->operands == 5)
6536 {
6537 if (i.op[0].imms->X_op != O_constant
6538 || !fits_in_imm4 (i.op[0].imms->X_add_number))
6539 {
6540 i.error = bad_imm4;
6541 return 1;
6542 }
6543
6544 /* Turn off Imm<N> so that update_imm won't complain. */
6545 operand_type_set (&i.types[0], 0);
6546 }
6547
43234a1e 6548 /* Check vector Disp8 operand. */
b5014f7a 6549 if (t->opcode_modifier.disp8memshift
1a42a9fe 6550 && i.disp_encoding <= disp_encoding_8bit)
43234a1e 6551 {
a5748e0d 6552 if (i.broadcast.bytes)
4a1b91ea 6553 i.memshift = t->opcode_modifier.broadcast - 1;
7091c612 6554 else if (t->opcode_modifier.disp8memshift != DISP8_SHIFT_VL)
43234a1e 6555 i.memshift = t->opcode_modifier.disp8memshift;
7091c612
JB
6556 else
6557 {
125ff819 6558 const i386_operand_type *type = NULL, *fallback = NULL;
7091c612
JB
6559
6560 i.memshift = 0;
6561 for (op = 0; op < i.operands; op++)
8dc0818e 6562 if (i.flags[op] & Operand_Mem)
7091c612 6563 {
4174bfff
JB
6564 if (t->opcode_modifier.evex == EVEXLIG)
6565 i.memshift = 2 + (i.suffix == QWORD_MNEM_SUFFIX);
6566 else if (t->operand_types[op].bitfield.xmmword
6567 + t->operand_types[op].bitfield.ymmword
6568 + t->operand_types[op].bitfield.zmmword <= 1)
7091c612
JB
6569 type = &t->operand_types[op];
6570 else if (!i.types[op].bitfield.unspecified)
6571 type = &i.types[op];
125ff819
JB
6572 else /* Ambiguities get resolved elsewhere. */
6573 fallback = &t->operand_types[op];
7091c612 6574 }
3528c362 6575 else if (i.types[op].bitfield.class == RegSIMD
4174bfff 6576 && t->opcode_modifier.evex != EVEXLIG)
7091c612
JB
6577 {
6578 if (i.types[op].bitfield.zmmword)
6579 i.memshift = 6;
6580 else if (i.types[op].bitfield.ymmword && i.memshift < 5)
6581 i.memshift = 5;
6582 else if (i.types[op].bitfield.xmmword && i.memshift < 4)
6583 i.memshift = 4;
6584 }
6585
125ff819
JB
6586 if (!type && !i.memshift)
6587 type = fallback;
7091c612
JB
6588 if (type)
6589 {
6590 if (type->bitfield.zmmword)
6591 i.memshift = 6;
6592 else if (type->bitfield.ymmword)
6593 i.memshift = 5;
6594 else if (type->bitfield.xmmword)
6595 i.memshift = 4;
6596 }
6597
6598 /* For the check in fits_in_disp8(). */
6599 if (i.memshift == 0)
6600 i.memshift = -1;
6601 }
43234a1e
L
6602
6603 for (op = 0; op < i.operands; op++)
6604 if (operand_type_check (i.types[op], disp)
6605 && i.op[op].disps->X_op == O_constant)
6606 {
b5014f7a 6607 if (fits_in_disp8 (i.op[op].disps->X_add_number))
43234a1e 6608 {
b5014f7a
JB
6609 i.types[op].bitfield.disp8 = 1;
6610 return 0;
43234a1e 6611 }
b5014f7a 6612 i.types[op].bitfield.disp8 = 0;
43234a1e
L
6613 }
6614 }
b5014f7a
JB
6615
6616 i.memshift = 0;
43234a1e 6617
6c30d220
L
6618 return 0;
6619}
6620
da4977e0 6621/* Check if encoding requirements are met by the instruction. */
a683cc34
SP
6622
6623static int
da4977e0 6624VEX_check_encoding (const insn_template *t)
a683cc34 6625{
da4977e0
JB
6626 if (i.vec_encoding == vex_encoding_error)
6627 {
6628 i.error = unsupported;
6629 return 1;
6630 }
6631
86fa6981 6632 if (i.vec_encoding == vex_encoding_evex)
43234a1e 6633 {
86fa6981 6634 /* This instruction must be encoded with EVEX prefix. */
e771e7c9 6635 if (!is_evex_encoding (t))
86fa6981
L
6636 {
6637 i.error = unsupported;
6638 return 1;
6639 }
6640 return 0;
43234a1e
L
6641 }
6642
a683cc34 6643 if (!t->opcode_modifier.vex)
86fa6981
L
6644 {
6645 /* This instruction template doesn't have VEX prefix. */
6646 if (i.vec_encoding != vex_encoding_default)
6647 {
6648 i.error = unsupported;
6649 return 1;
6650 }
6651 return 0;
6652 }
a683cc34 6653
a683cc34
SP
6654 return 0;
6655}
6656
7b94647a
JB
6657/* Helper function for the progress() macro in match_template(). */
6658static INLINE enum i386_error progress (enum i386_error new,
6659 enum i386_error last,
6660 unsigned int line, unsigned int *line_p)
6661{
6662 if (line <= *line_p)
6663 return last;
6664 *line_p = line;
6665 return new;
6666}
6667
d3ce72d0 6668static const insn_template *
83b16ac6 6669match_template (char mnem_suffix)
29b0f896
AM
6670{
6671 /* Points to template once we've found it. */
d3ce72d0 6672 const insn_template *t;
40fb9820 6673 i386_operand_type overlap0, overlap1, overlap2, overlap3;
c0f3af97 6674 i386_operand_type overlap4;
29b0f896 6675 unsigned int found_reverse_match;
40fb9820 6676 i386_operand_type operand_types [MAX_OPERANDS];
539e75ad 6677 int addr_prefix_disp;
7b94647a
JB
6678 unsigned int j, size_match, check_register, errline = __LINE__;
6679 enum i386_error specific_error = number_of_operands_mismatch;
6680#define progress(err) progress (err, specific_error, __LINE__, &errline)
29b0f896 6681
c0f3af97
L
6682#if MAX_OPERANDS != 5
6683# error "MAX_OPERANDS must be 5."
f48ff2ae
L
6684#endif
6685
29b0f896 6686 found_reverse_match = 0;
539e75ad 6687 addr_prefix_disp = -1;
40fb9820 6688
45aa61fe 6689 for (t = current_templates->start; t < current_templates->end; t++)
29b0f896 6690 {
539e75ad 6691 addr_prefix_disp = -1;
dbbc8b7e 6692 found_reverse_match = 0;
539e75ad 6693
7b94647a 6694 /* Must have right number of operands. */
29b0f896
AM
6695 if (i.operands != t->operands)
6696 continue;
6697
50aecf8c 6698 /* Check processor support. */
7b94647a 6699 specific_error = progress (unsupported);
45a4bb20 6700 if (cpu_flags_match (t) != CPU_FLAGS_PERFECT_MATCH)
50aecf8c
L
6701 continue;
6702
e1d4d893 6703 /* Check AT&T mnemonic. */
7b94647a 6704 specific_error = progress (unsupported_with_intel_mnemonic);
e1d4d893 6705 if (intel_mnemonic && t->opcode_modifier.attmnemonic)
1efbbeb4
L
6706 continue;
6707
4b5aaf5f 6708 /* Check AT&T/Intel syntax. */
7b94647a 6709 specific_error = progress (unsupported_syntax);
5c07affc 6710 if ((intel_syntax && t->opcode_modifier.attsyntax)
4b5aaf5f 6711 || (!intel_syntax && t->opcode_modifier.intelsyntax))
1efbbeb4
L
6712 continue;
6713
4b5aaf5f
L
6714 /* Check Intel64/AMD64 ISA. */
6715 switch (isa64)
6716 {
6717 default:
6718 /* Default: Don't accept Intel64. */
6719 if (t->opcode_modifier.isa64 == INTEL64)
6720 continue;
6721 break;
6722 case amd64:
6723 /* -mamd64: Don't accept Intel64 and Intel64 only. */
6724 if (t->opcode_modifier.isa64 >= INTEL64)
6725 continue;
6726 break;
6727 case intel64:
6728 /* -mintel64: Don't accept AMD64. */
5990e377 6729 if (t->opcode_modifier.isa64 == AMD64 && flag_code == CODE_64BIT)
4b5aaf5f
L
6730 continue;
6731 break;
6732 }
6733
dc2be329 6734 /* Check the suffix. */
7b94647a 6735 specific_error = progress (invalid_instruction_suffix);
7505bb03
JB
6736 if ((t->opcode_modifier.no_bsuf && mnem_suffix == BYTE_MNEM_SUFFIX)
6737 || (t->opcode_modifier.no_wsuf && mnem_suffix == WORD_MNEM_SUFFIX)
6738 || (t->opcode_modifier.no_lsuf && mnem_suffix == LONG_MNEM_SUFFIX)
6739 || (t->opcode_modifier.no_ssuf && mnem_suffix == SHORT_MNEM_SUFFIX)
6740 || (t->opcode_modifier.no_qsuf && mnem_suffix == QWORD_MNEM_SUFFIX))
83b16ac6 6741 continue;
29b0f896 6742
7b94647a 6743 specific_error = progress (operand_size_mismatch);
3ac21baa
JB
6744 size_match = operand_size_match (t);
6745 if (!size_match)
7d5e4556 6746 continue;
539e75ad 6747
6f2f06be
JB
6748 /* This is intentionally not
6749
0cfa3eb3 6750 if (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE))
6f2f06be
JB
6751
6752 as the case of a missing * on the operand is accepted (perhaps with
6753 a warning, issued further down). */
7b94647a 6754 specific_error = progress (operand_type_mismatch);
0cfa3eb3 6755 if (i.jumpabsolute && t->opcode_modifier.jump != JUMP_ABSOLUTE)
7b94647a 6756 continue;
6f2f06be 6757
a4d3acd2
JB
6758 /* In Intel syntax, normally we can check for memory operand size when
6759 there is no mnemonic suffix. But jmp and call have 2 different
6760 encodings with Dword memory operand size. Skip the "near" one
6761 (permitting a register operand) when "far" was requested. */
6762 if (i.far_branch
6763 && t->opcode_modifier.jump == JUMP_ABSOLUTE
6764 && t->operand_types[0].bitfield.class == Reg)
6765 continue;
6766
5c07affc
L
6767 for (j = 0; j < MAX_OPERANDS; j++)
6768 operand_types[j] = t->operand_types[j];
6769
9db83a32 6770 /* In general, don't allow 32-bit operands on pre-386. */
7b94647a
JB
6771 specific_error = progress (mnem_suffix ? invalid_instruction_suffix
6772 : operand_size_mismatch);
4873e243 6773 j = i.imm_operands + (t->operands > i.imm_operands + 1);
9db83a32
JB
6774 if (i.suffix == LONG_MNEM_SUFFIX
6775 && !cpu_arch_flags.bitfield.cpui386
45aa61fe 6776 && (intel_syntax
3cd7f3e3 6777 ? (t->opcode_modifier.mnemonicsize != IGNORESIZE
45aa61fe
AM
6778 && !intel_float_operand (t->name))
6779 : intel_float_operand (t->name) != 2)
4873e243
JB
6780 && (t->operands == i.imm_operands
6781 || (operand_types[i.imm_operands].bitfield.class != RegMMX
6782 && operand_types[i.imm_operands].bitfield.class != RegSIMD
6783 && operand_types[i.imm_operands].bitfield.class != RegMask)
6784 || (operand_types[j].bitfield.class != RegMMX
6785 && operand_types[j].bitfield.class != RegSIMD
6786 && operand_types[j].bitfield.class != RegMask))
63112cd6 6787 && !t->opcode_modifier.sib)
192dc9c6
JB
6788 continue;
6789
29b0f896 6790 /* Do not verify operands when there are none. */
e365e234 6791 if (!t->operands)
da4977e0
JB
6792 {
6793 if (VEX_check_encoding (t))
6794 {
7b94647a 6795 specific_error = progress (i.error);
da4977e0
JB
6796 continue;
6797 }
6798
6799 /* We've found a match; break out of loop. */
6800 break;
6801 }
252b5132 6802
48bcea9f
JB
6803 if (!t->opcode_modifier.jump
6804 || t->opcode_modifier.jump == JUMP_ABSOLUTE)
6805 {
6806 /* There should be only one Disp operand. */
6807 for (j = 0; j < MAX_OPERANDS; j++)
6808 if (operand_type_check (operand_types[j], disp))
539e75ad 6809 break;
48bcea9f
JB
6810 if (j < MAX_OPERANDS)
6811 {
5b7c81bd 6812 bool override = (i.prefix[ADDR_PREFIX] != 0);
48bcea9f
JB
6813
6814 addr_prefix_disp = j;
6815
a775efc8
JB
6816 /* Address size prefix will turn Disp64 operand into Disp32 and
6817 Disp32/Disp16 one into Disp16/Disp32 respectively. */
48bcea9f 6818 switch (flag_code)
40fb9820 6819 {
48bcea9f
JB
6820 case CODE_16BIT:
6821 override = !override;
6822 /* Fall through. */
6823 case CODE_32BIT:
6824 if (operand_types[j].bitfield.disp32
6825 && operand_types[j].bitfield.disp16)
40fb9820 6826 {
48bcea9f
JB
6827 operand_types[j].bitfield.disp16 = override;
6828 operand_types[j].bitfield.disp32 = !override;
40fb9820 6829 }
a775efc8 6830 gas_assert (!operand_types[j].bitfield.disp64);
48bcea9f
JB
6831 break;
6832
6833 case CODE_64BIT:
a775efc8 6834 if (operand_types[j].bitfield.disp64)
40fb9820 6835 {
a775efc8 6836 gas_assert (!operand_types[j].bitfield.disp32);
48bcea9f 6837 operand_types[j].bitfield.disp32 = override;
a775efc8 6838 operand_types[j].bitfield.disp64 = !override;
40fb9820 6839 }
48bcea9f
JB
6840 operand_types[j].bitfield.disp16 = 0;
6841 break;
40fb9820 6842 }
539e75ad 6843 }
48bcea9f 6844 }
539e75ad 6845
56ffb741 6846 /* We check register size if needed. */
e2195274
JB
6847 if (t->opcode_modifier.checkregsize)
6848 {
6849 check_register = (1 << t->operands) - 1;
a5748e0d 6850 if (i.broadcast.type || i.broadcast.bytes)
5273a3cd 6851 check_register &= ~(1 << i.broadcast.operand);
e2195274
JB
6852 }
6853 else
6854 check_register = 0;
6855
c6fb90c8 6856 overlap0 = operand_type_and (i.types[0], operand_types[0]);
29b0f896
AM
6857 switch (t->operands)
6858 {
6859 case 1:
40fb9820 6860 if (!operand_type_match (overlap0, i.types[0]))
29b0f896
AM
6861 continue;
6862 break;
6863 case 2:
33eaf5de 6864 /* xchg %eax, %eax is a special case. It is an alias for nop
8b38ad71
L
6865 only in 32bit mode and we can use opcode 0x90. In 64bit
6866 mode, we can't use 0x90 for xchg %eax, %eax since it should
6867 zero-extend %eax to %rax. */
6868 if (flag_code == CODE_64BIT
6869 && t->base_opcode == 0x90
35648716 6870 && t->opcode_modifier.opcodespace == SPACE_BASE
75e5731b
JB
6871 && i.types[0].bitfield.instance == Accum
6872 && i.types[0].bitfield.dword
44732014 6873 && i.types[1].bitfield.instance == Accum)
8b38ad71 6874 continue;
e3669c7f
JB
6875
6876 if (t->base_opcode == MOV_AX_DISP32
e2b983d4 6877 && t->opcode_modifier.opcodespace == SPACE_BASE)
e3669c7f
JB
6878 {
6879 /* Force 0x8b encoding for "mov foo@GOT, %eax". */
6880 if (i.reloc[0] == BFD_RELOC_386_GOT32)
6881 continue;
6882
6883 /* xrelease mov %eax, <disp> is another special case. It must not
6884 match the accumulator-only encoding of mov. */
6885 if (i.hle_prefix)
6886 continue;
6887 }
f5eb1d70
JB
6888 /* Fall through. */
6889
6890 case 3:
3ac21baa
JB
6891 if (!(size_match & MATCH_STRAIGHT))
6892 goto check_reverse;
64c49ab3
JB
6893 /* Reverse direction of operands if swapping is possible in the first
6894 place (operands need to be symmetric) and
6895 - the load form is requested, and the template is a store form,
6896 - the store form is requested, and the template is a load form,
6897 - the non-default (swapped) form is requested. */
6898 overlap1 = operand_type_and (operand_types[0], operand_types[1]);
f5eb1d70 6899 if (t->opcode_modifier.d && i.reg_operands == i.operands
64c49ab3
JB
6900 && !operand_type_all_zero (&overlap1))
6901 switch (i.dir_encoding)
6902 {
6903 case dir_encoding_load:
6904 if (operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 6905 || t->opcode_modifier.regmem)
64c49ab3
JB
6906 goto check_reverse;
6907 break;
6908
6909 case dir_encoding_store:
6910 if (!operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 6911 && !t->opcode_modifier.regmem)
64c49ab3
JB
6912 goto check_reverse;
6913 break;
6914
6915 case dir_encoding_swap:
6916 goto check_reverse;
6917
6918 case dir_encoding_default:
6919 break;
6920 }
86fa6981 6921 /* If we want store form, we skip the current load. */
64c49ab3
JB
6922 if ((i.dir_encoding == dir_encoding_store
6923 || i.dir_encoding == dir_encoding_swap)
86fa6981
L
6924 && i.mem_operands == 0
6925 && t->opcode_modifier.load)
fa99fab2 6926 continue;
1a0670f3 6927 /* Fall through. */
f48ff2ae 6928 case 4:
c0f3af97 6929 case 5:
c6fb90c8 6930 overlap1 = operand_type_and (i.types[1], operand_types[1]);
40fb9820
L
6931 if (!operand_type_match (overlap0, i.types[0])
6932 || !operand_type_match (overlap1, i.types[1])
e2195274 6933 || ((check_register & 3) == 3
dc821c5f 6934 && !operand_type_register_match (i.types[0],
40fb9820 6935 operand_types[0],
dc821c5f 6936 i.types[1],
40fb9820 6937 operand_types[1])))
29b0f896 6938 {
7b94647a
JB
6939 specific_error = progress (i.error);
6940
29b0f896 6941 /* Check if other direction is valid ... */
38e314eb 6942 if (!t->opcode_modifier.d)
29b0f896
AM
6943 continue;
6944
dc1e8a47 6945 check_reverse:
3ac21baa
JB
6946 if (!(size_match & MATCH_REVERSE))
6947 continue;
29b0f896 6948 /* Try reversing direction of operands. */
8bd915b7
JB
6949 j = t->opcode_modifier.vexsources ? 1 : i.operands - 1;
6950 overlap0 = operand_type_and (i.types[0], operand_types[j]);
6951 overlap1 = operand_type_and (i.types[j], operand_types[0]);
c975cec5
JB
6952 overlap2 = operand_type_and (i.types[1], operand_types[1]);
6953 gas_assert (t->operands != 3 || !check_register);
40fb9820 6954 if (!operand_type_match (overlap0, i.types[0])
8bd915b7 6955 || !operand_type_match (overlap1, i.types[j])
c975cec5
JB
6956 || (t->operands == 3
6957 && !operand_type_match (overlap2, i.types[1]))
45664ddb 6958 || (check_register
dc821c5f 6959 && !operand_type_register_match (i.types[0],
8bd915b7
JB
6960 operand_types[j],
6961 i.types[j],
45664ddb 6962 operand_types[0])))
29b0f896
AM
6963 {
6964 /* Does not match either direction. */
7b94647a 6965 specific_error = progress (i.error);
29b0f896
AM
6966 continue;
6967 }
ac9226cf 6968 /* found_reverse_match holds which variant of D
29b0f896 6969 we've found. */
38e314eb
JB
6970 if (!t->opcode_modifier.d)
6971 found_reverse_match = 0;
6972 else if (operand_types[0].bitfield.tbyte)
ac9226cf 6973 {
4943d587
JB
6974 if (t->opcode_modifier.operandconstraint != UGH)
6975 found_reverse_match = Opcode_FloatD;
ac9226cf
JB
6976 /* FSUB{,R} and FDIV{,R} may need a 2nd bit flipped. */
6977 if ((t->base_opcode & 0x20)
6978 && (intel_syntax || intel_mnemonic))
6979 found_reverse_match |= Opcode_FloatR;
6980 }
8bd915b7
JB
6981 else if (t->opcode_modifier.vexsources)
6982 {
6983 found_reverse_match = Opcode_VexW;
6984 goto check_operands_345;
6985 }
2c735193
JB
6986 else if (t->opcode_modifier.opcodespace != SPACE_BASE
6987 && (t->opcode_modifier.opcodespace != SPACE_0F
6988 /* MOV to/from CR/DR/TR, as an exception, follow
6989 the base opcode space encoding model. */
6990 || (t->base_opcode | 7) != 0x27))
dbbc8b7e 6991 found_reverse_match = (t->base_opcode & 0xee) != 0x6e
2c735193 6992 ? Opcode_ExtD : Opcode_SIMD_IntD;
a33ef3c2 6993 else if (!t->opcode_modifier.commutative)
38e314eb 6994 found_reverse_match = Opcode_D;
a33ef3c2
JB
6995 else
6996 found_reverse_match = ~0;
29b0f896 6997 }
f48ff2ae 6998 else
29b0f896 6999 {
f48ff2ae 7000 /* Found a forward 2 operand match here. */
8bd915b7 7001 check_operands_345:
d1cbb4db
L
7002 switch (t->operands)
7003 {
c0f3af97 7004 case 5:
3d0738af 7005 overlap4 = operand_type_and (i.types[4], operand_types[4]);
c0f3af97 7006 if (!operand_type_match (overlap4, i.types[4])
dc821c5f 7007 || !operand_type_register_match (i.types[3],
c0f3af97 7008 operand_types[3],
c0f3af97
L
7009 i.types[4],
7010 operand_types[4]))
7b94647a
JB
7011 {
7012 specific_error = progress (i.error);
7013 continue;
7014 }
1a0670f3 7015 /* Fall through. */
f48ff2ae 7016 case 4:
3d0738af 7017 overlap3 = operand_type_and (i.types[3], operand_types[3]);
40fb9820 7018 if (!operand_type_match (overlap3, i.types[3])
e2195274
JB
7019 || ((check_register & 0xa) == 0xa
7020 && !operand_type_register_match (i.types[1],
f7768225
JB
7021 operand_types[1],
7022 i.types[3],
e2195274
JB
7023 operand_types[3]))
7024 || ((check_register & 0xc) == 0xc
7025 && !operand_type_register_match (i.types[2],
7026 operand_types[2],
7027 i.types[3],
7028 operand_types[3])))
7b94647a
JB
7029 {
7030 specific_error = progress (i.error);
7031 continue;
7032 }
1a0670f3 7033 /* Fall through. */
f48ff2ae 7034 case 3:
3d0738af 7035 overlap2 = operand_type_and (i.types[2], operand_types[2]);
40fb9820 7036 if (!operand_type_match (overlap2, i.types[2])
e2195274
JB
7037 || ((check_register & 5) == 5
7038 && !operand_type_register_match (i.types[0],
23e42951
JB
7039 operand_types[0],
7040 i.types[2],
e2195274
JB
7041 operand_types[2]))
7042 || ((check_register & 6) == 6
7043 && !operand_type_register_match (i.types[1],
7044 operand_types[1],
7045 i.types[2],
7046 operand_types[2])))
7b94647a
JB
7047 {
7048 specific_error = progress (i.error);
7049 continue;
7050 }
f48ff2ae
L
7051 break;
7052 }
29b0f896 7053 }
f48ff2ae 7054 /* Found either forward/reverse 2, 3 or 4 operand match here:
29b0f896
AM
7055 slip through to break. */
7056 }
c0f3af97 7057
9bb4d860
L
7058 /* Check if VEX/EVEX encoding requirements can be satisfied. */
7059 if (VEX_check_encoding (t))
da4977e0 7060 {
7b94647a 7061 specific_error = progress (i.error);
da4977e0
JB
7062 continue;
7063 }
7064
9bb4d860
L
7065 /* Check if vector operands are valid. */
7066 if (check_VecOperands (t))
5614d22c 7067 {
7b94647a 7068 specific_error = progress (i.error);
5614d22c
JB
7069 continue;
7070 }
a683cc34 7071
29b0f896
AM
7072 /* We've found a match; break out of loop. */
7073 break;
7074 }
7075
7b94647a
JB
7076#undef progress
7077
29b0f896
AM
7078 if (t == current_templates->end)
7079 {
7080 /* We found no match. */
04784e33 7081 i.error = specific_error;
fa99fab2 7082 return NULL;
29b0f896 7083 }
252b5132 7084
29b0f896
AM
7085 if (!quiet_warnings)
7086 {
7087 if (!intel_syntax
0cfa3eb3 7088 && (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE)))
6f2f06be 7089 as_warn (_("indirect %s without `*'"), t->name);
29b0f896 7090
40fb9820 7091 if (t->opcode_modifier.isprefix
3cd7f3e3 7092 && t->opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
7093 {
7094 /* Warn them that a data or address size prefix doesn't
7095 affect assembly of the next line of code. */
7096 as_warn (_("stand-alone `%s' prefix"), t->name);
7097 }
7098 }
7099
7100 /* Copy the template we found. */
9a182d04 7101 install_template (t);
539e75ad
L
7102
7103 if (addr_prefix_disp != -1)
7104 i.tm.operand_types[addr_prefix_disp]
7105 = operand_types[addr_prefix_disp];
7106
8bd915b7 7107 switch (found_reverse_match)
29b0f896 7108 {
8bd915b7
JB
7109 case 0:
7110 break;
7111
7112 default:
dfd69174
JB
7113 /* If we found a reverse match we must alter the opcode direction
7114 bit and clear/flip the regmem modifier one. found_reverse_match
7115 holds bits to change (different for int & float insns). */
29b0f896
AM
7116
7117 i.tm.base_opcode ^= found_reverse_match;
7118
dfd69174
JB
7119 /* Certain SIMD insns have their load forms specified in the opcode
7120 table, and hence we need to _set_ RegMem instead of clearing it.
7121 We need to avoid setting the bit though on insns like KMOVW. */
7122 i.tm.opcode_modifier.regmem
7123 = i.tm.opcode_modifier.modrm && i.tm.opcode_modifier.d
7124 && i.tm.operands > 2U - i.tm.opcode_modifier.sse2avx
7125 && !i.tm.opcode_modifier.regmem;
a33ef3c2
JB
7126
7127 /* Fall through. */
7128 case ~0:
7129 i.tm.operand_types[0] = operand_types[i.operands - 1];
7130 i.tm.operand_types[i.operands - 1] = operand_types[0];
8bd915b7
JB
7131 break;
7132
7133 case Opcode_VexW:
7134 /* Only the first two register operands need reversing, alongside
7135 flipping VEX.W. */
7136 i.tm.opcode_modifier.vexw ^= VEXW0 ^ VEXW1;
7137
7138 j = i.tm.operand_types[0].bitfield.imm8;
7139 i.tm.operand_types[j] = operand_types[j + 1];
7140 i.tm.operand_types[j + 1] = operand_types[j];
7141 break;
29b0f896
AM
7142 }
7143
fa99fab2 7144 return t;
29b0f896
AM
7145}
7146
7147static int
e3bb37b5 7148check_string (void)
29b0f896 7149{
51c8edf6
JB
7150 unsigned int es_op = i.tm.opcode_modifier.isstring - IS_STRING_ES_OP0;
7151 unsigned int op = i.tm.operand_types[0].bitfield.baseindex ? es_op : 0;
8dc0818e 7152
5e042380 7153 if (i.seg[op] != NULL && i.seg[op] != reg_es)
29b0f896 7154 {
51c8edf6
JB
7155 as_bad (_("`%s' operand %u must use `%ses' segment"),
7156 i.tm.name,
7157 intel_syntax ? i.tm.operands - es_op : es_op + 1,
7158 register_prefix);
7159 return 0;
29b0f896 7160 }
51c8edf6
JB
7161
7162 /* There's only ever one segment override allowed per instruction.
7163 This instruction possibly has a legal segment override on the
7164 second operand, so copy the segment to where non-string
7165 instructions store it, allowing common code. */
7166 i.seg[op] = i.seg[1];
7167
29b0f896
AM
7168 return 1;
7169}
7170
7171static int
543613e9 7172process_suffix (void)
29b0f896 7173{
5b7c81bd 7174 bool is_crc32 = false, is_movx = false;
8b65b895 7175
29b0f896
AM
7176 /* If matched instruction specifies an explicit instruction mnemonic
7177 suffix, use it. */
673fe0f0 7178 if (i.tm.opcode_modifier.size == SIZE16)
40fb9820 7179 i.suffix = WORD_MNEM_SUFFIX;
673fe0f0 7180 else if (i.tm.opcode_modifier.size == SIZE32)
40fb9820 7181 i.suffix = LONG_MNEM_SUFFIX;
673fe0f0 7182 else if (i.tm.opcode_modifier.size == SIZE64)
40fb9820 7183 i.suffix = QWORD_MNEM_SUFFIX;
13e600d0 7184 else if (i.reg_operands
c8f8eebc 7185 && (i.operands > 1 || i.types[0].bitfield.class == Reg)
255571cd 7186 && i.tm.opcode_modifier.operandconstraint != ADDR_PREFIX_OP_REG)
29b0f896 7187 {
65fca059 7188 unsigned int numop = i.operands;
389d00a5
JB
7189
7190 /* MOVSX/MOVZX */
7191 is_movx = (i.tm.opcode_modifier.opcodespace == SPACE_0F
7192 && (i.tm.base_opcode | 8) == 0xbe)
7193 || (i.tm.opcode_modifier.opcodespace == SPACE_BASE
7194 && i.tm.base_opcode == 0x63
7195 && i.tm.cpu_flags.bitfield.cpu64);
7196
8b65b895 7197 /* CRC32 */
389d00a5
JB
7198 is_crc32 = (i.tm.base_opcode == 0xf0
7199 && i.tm.opcode_modifier.opcodespace == SPACE_0F38
8b65b895 7200 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2);
65fca059
JB
7201
7202 /* movsx/movzx want only their source operand considered here, for the
7203 ambiguity checking below. The suffix will be replaced afterwards
7204 to represent the destination (register). */
389d00a5 7205 if (is_movx && (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63))
65fca059
JB
7206 --i.operands;
7207
643bb870 7208 /* crc32 needs REX.W set regardless of suffix / source operand size. */
8b65b895 7209 if (is_crc32 && i.tm.operand_types[1].bitfield.qword)
643bb870
JB
7210 i.rex |= REX_W;
7211
29b0f896 7212 /* If there's no instruction mnemonic suffix we try to invent one
13e600d0 7213 based on GPR operands. */
29b0f896
AM
7214 if (!i.suffix)
7215 {
7216 /* We take i.suffix from the last register operand specified,
7217 Destination register type is more significant than source
381d071f
L
7218 register type. crc32 in SSE4.2 prefers source register
7219 type. */
8b65b895 7220 unsigned int op = is_crc32 ? 1 : i.operands;
20592a94 7221
1a035124
JB
7222 while (op--)
7223 if (i.tm.operand_types[op].bitfield.instance == InstanceNone
7224 || i.tm.operand_types[op].bitfield.instance == Accum)
7225 {
7226 if (i.types[op].bitfield.class != Reg)
7227 continue;
7228 if (i.types[op].bitfield.byte)
7229 i.suffix = BYTE_MNEM_SUFFIX;
7230 else if (i.types[op].bitfield.word)
7231 i.suffix = WORD_MNEM_SUFFIX;
7232 else if (i.types[op].bitfield.dword)
7233 i.suffix = LONG_MNEM_SUFFIX;
7234 else if (i.types[op].bitfield.qword)
7235 i.suffix = QWORD_MNEM_SUFFIX;
7236 else
7237 continue;
7238 break;
7239 }
65fca059
JB
7240
7241 /* As an exception, movsx/movzx silently default to a byte source
7242 in AT&T mode. */
389d00a5 7243 if (is_movx && i.tm.opcode_modifier.w && !i.suffix && !intel_syntax)
65fca059 7244 i.suffix = BYTE_MNEM_SUFFIX;
29b0f896
AM
7245 }
7246 else if (i.suffix == BYTE_MNEM_SUFFIX)
7247 {
1cb0ab18 7248 if (!check_byte_reg ())
29b0f896
AM
7249 return 0;
7250 }
7251 else if (i.suffix == LONG_MNEM_SUFFIX)
7252 {
1cb0ab18 7253 if (!check_long_reg ())
29b0f896
AM
7254 return 0;
7255 }
7256 else if (i.suffix == QWORD_MNEM_SUFFIX)
7257 {
1cb0ab18 7258 if (!check_qword_reg ())
29b0f896
AM
7259 return 0;
7260 }
7261 else if (i.suffix == WORD_MNEM_SUFFIX)
7262 {
1cb0ab18 7263 if (!check_word_reg ())
29b0f896
AM
7264 return 0;
7265 }
3cd7f3e3
L
7266 else if (intel_syntax
7267 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
7268 /* Do nothing if the instruction is going to ignore the prefix. */
7269 ;
7270 else
7271 abort ();
65fca059
JB
7272
7273 /* Undo the movsx/movzx change done above. */
7274 i.operands = numop;
29b0f896 7275 }
3cd7f3e3
L
7276 else if (i.tm.opcode_modifier.mnemonicsize == DEFAULTSIZE
7277 && !i.suffix)
29b0f896 7278 {
13e600d0
JB
7279 i.suffix = stackop_size;
7280 if (stackop_size == LONG_MNEM_SUFFIX)
06f74c5c
L
7281 {
7282 /* stackop_size is set to LONG_MNEM_SUFFIX for the
7283 .code16gcc directive to support 16-bit mode with
7284 32-bit address. For IRET without a suffix, generate
7285 16-bit IRET (opcode 0xcf) to return from an interrupt
7286 handler. */
13e600d0
JB
7287 if (i.tm.base_opcode == 0xcf)
7288 {
7289 i.suffix = WORD_MNEM_SUFFIX;
7290 as_warn (_("generating 16-bit `iret' for .code16gcc directive"));
7291 }
7292 /* Warn about changed behavior for segment register push/pop. */
7293 else if ((i.tm.base_opcode | 1) == 0x07)
7294 as_warn (_("generating 32-bit `%s', unlike earlier gas versions"),
7295 i.tm.name);
06f74c5c 7296 }
29b0f896 7297 }
c006a730 7298 else if (!i.suffix
0cfa3eb3
JB
7299 && (i.tm.opcode_modifier.jump == JUMP_ABSOLUTE
7300 || i.tm.opcode_modifier.jump == JUMP_BYTE
7301 || i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT
389d00a5
JB
7302 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
7303 && i.tm.base_opcode == 0x01 /* [ls][gi]dt */
64e74474 7304 && i.tm.extension_opcode <= 3)))
9306ca4a
JB
7305 {
7306 switch (flag_code)
7307 {
7308 case CODE_64BIT:
40fb9820 7309 if (!i.tm.opcode_modifier.no_qsuf)
9306ca4a 7310 {
828c2a25
JB
7311 if (i.tm.opcode_modifier.jump == JUMP_BYTE
7312 || i.tm.opcode_modifier.no_lsuf)
7313 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a
JB
7314 break;
7315 }
1a0670f3 7316 /* Fall through. */
9306ca4a 7317 case CODE_32BIT:
40fb9820 7318 if (!i.tm.opcode_modifier.no_lsuf)
9306ca4a
JB
7319 i.suffix = LONG_MNEM_SUFFIX;
7320 break;
7321 case CODE_16BIT:
40fb9820 7322 if (!i.tm.opcode_modifier.no_wsuf)
9306ca4a
JB
7323 i.suffix = WORD_MNEM_SUFFIX;
7324 break;
7325 }
7326 }
252b5132 7327
c006a730 7328 if (!i.suffix
3cd7f3e3 7329 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8
JB
7330 /* Also cover lret/retf/iret in 64-bit mode. */
7331 || (flag_code == CODE_64BIT
7332 && !i.tm.opcode_modifier.no_lsuf
7333 && !i.tm.opcode_modifier.no_qsuf))
3cd7f3e3 7334 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
8bbb3ad8
JB
7335 /* Explicit sizing prefixes are assumed to disambiguate insns. */
7336 && !i.prefix[DATA_PREFIX] && !(i.prefix[REX_PREFIX] & REX_W)
62b3f548
JB
7337 /* Accept FLDENV et al without suffix. */
7338 && (i.tm.opcode_modifier.no_ssuf || i.tm.opcode_modifier.floatmf))
29b0f896 7339 {
6c0946d0 7340 unsigned int suffixes, evex = 0;
c006a730
JB
7341
7342 suffixes = !i.tm.opcode_modifier.no_bsuf;
7343 if (!i.tm.opcode_modifier.no_wsuf)
7344 suffixes |= 1 << 1;
7345 if (!i.tm.opcode_modifier.no_lsuf)
7346 suffixes |= 1 << 2;
c006a730
JB
7347 if (!i.tm.opcode_modifier.no_ssuf)
7348 suffixes |= 1 << 4;
7349 if (flag_code == CODE_64BIT && !i.tm.opcode_modifier.no_qsuf)
7350 suffixes |= 1 << 5;
7351
6c0946d0
JB
7352 /* For [XYZ]MMWORD operands inspect operand sizes. While generally
7353 also suitable for AT&T syntax mode, it was requested that this be
7354 restricted to just Intel syntax. */
a5748e0d
JB
7355 if (intel_syntax && is_any_vex_encoding (&i.tm)
7356 && !i.broadcast.type && !i.broadcast.bytes)
6c0946d0 7357 {
b9915cbc 7358 unsigned int op;
6c0946d0 7359
b9915cbc 7360 for (op = 0; op < i.tm.operands; ++op)
6c0946d0 7361 {
b9915cbc
JB
7362 if (is_evex_encoding (&i.tm)
7363 && !cpu_arch_flags.bitfield.cpuavx512vl)
6c0946d0 7364 {
b9915cbc
JB
7365 if (i.tm.operand_types[op].bitfield.ymmword)
7366 i.tm.operand_types[op].bitfield.xmmword = 0;
7367 if (i.tm.operand_types[op].bitfield.zmmword)
7368 i.tm.operand_types[op].bitfield.ymmword = 0;
7369 if (!i.tm.opcode_modifier.evex
7370 || i.tm.opcode_modifier.evex == EVEXDYN)
7371 i.tm.opcode_modifier.evex = EVEX512;
7372 }
6c0946d0 7373
b9915cbc
JB
7374 if (i.tm.operand_types[op].bitfield.xmmword
7375 + i.tm.operand_types[op].bitfield.ymmword
7376 + i.tm.operand_types[op].bitfield.zmmword < 2)
7377 continue;
6c0946d0 7378
b9915cbc
JB
7379 /* Any properly sized operand disambiguates the insn. */
7380 if (i.types[op].bitfield.xmmword
7381 || i.types[op].bitfield.ymmword
7382 || i.types[op].bitfield.zmmword)
7383 {
7384 suffixes &= ~(7 << 6);
7385 evex = 0;
7386 break;
7387 }
6c0946d0 7388
b9915cbc
JB
7389 if ((i.flags[op] & Operand_Mem)
7390 && i.tm.operand_types[op].bitfield.unspecified)
7391 {
7392 if (i.tm.operand_types[op].bitfield.xmmword)
7393 suffixes |= 1 << 6;
7394 if (i.tm.operand_types[op].bitfield.ymmword)
7395 suffixes |= 1 << 7;
7396 if (i.tm.operand_types[op].bitfield.zmmword)
7397 suffixes |= 1 << 8;
7398 if (is_evex_encoding (&i.tm))
7399 evex = EVEX512;
6c0946d0
JB
7400 }
7401 }
7402 }
7403
7404 /* Are multiple suffixes / operand sizes allowed? */
c006a730 7405 if (suffixes & (suffixes - 1))
9306ca4a 7406 {
873494c8 7407 if (intel_syntax
3cd7f3e3 7408 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8 7409 || operand_check == check_error))
9306ca4a 7410 {
c006a730 7411 as_bad (_("ambiguous operand size for `%s'"), i.tm.name);
9306ca4a
JB
7412 return 0;
7413 }
c006a730 7414 if (operand_check == check_error)
9306ca4a 7415 {
c006a730
JB
7416 as_bad (_("no instruction mnemonic suffix given and "
7417 "no register operands; can't size `%s'"), i.tm.name);
9306ca4a
JB
7418 return 0;
7419 }
c006a730 7420 if (operand_check == check_warning)
873494c8
JB
7421 as_warn (_("%s; using default for `%s'"),
7422 intel_syntax
7423 ? _("ambiguous operand size")
7424 : _("no instruction mnemonic suffix given and "
7425 "no register operands"),
7426 i.tm.name);
c006a730
JB
7427
7428 if (i.tm.opcode_modifier.floatmf)
7429 i.suffix = SHORT_MNEM_SUFFIX;
389d00a5 7430 else if (is_movx)
65fca059 7431 /* handled below */;
6c0946d0
JB
7432 else if (evex)
7433 i.tm.opcode_modifier.evex = evex;
c006a730
JB
7434 else if (flag_code == CODE_16BIT)
7435 i.suffix = WORD_MNEM_SUFFIX;
1a035124 7436 else if (!i.tm.opcode_modifier.no_lsuf)
c006a730 7437 i.suffix = LONG_MNEM_SUFFIX;
1a035124
JB
7438 else
7439 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a 7440 }
29b0f896 7441 }
252b5132 7442
389d00a5 7443 if (is_movx)
65fca059
JB
7444 {
7445 /* In Intel syntax, movsx/movzx must have a "suffix" (checked above).
7446 In AT&T syntax, if there is no suffix (warned about above), the default
7447 will be byte extension. */
7448 if (i.tm.opcode_modifier.w && i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
7449 i.tm.base_opcode |= 1;
7450
7451 /* For further processing, the suffix should represent the destination
7452 (register). This is already the case when one was used with
7453 mov[sz][bw]*, but we need to replace it for mov[sz]x, or if there was
7454 no suffix to begin with. */
7455 if (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63 || !i.suffix)
7456 {
7457 if (i.types[1].bitfield.word)
7458 i.suffix = WORD_MNEM_SUFFIX;
7459 else if (i.types[1].bitfield.qword)
7460 i.suffix = QWORD_MNEM_SUFFIX;
7461 else
7462 i.suffix = LONG_MNEM_SUFFIX;
7463
7464 i.tm.opcode_modifier.w = 0;
7465 }
7466 }
7467
50128d0c
JB
7468 if (!i.tm.opcode_modifier.modrm && i.reg_operands && i.tm.operands < 3)
7469 i.short_form = (i.tm.operand_types[0].bitfield.class == Reg)
7470 != (i.tm.operand_types[1].bitfield.class == Reg);
7471
d2224064
JB
7472 /* Change the opcode based on the operand size given by i.suffix. */
7473 switch (i.suffix)
29b0f896 7474 {
d2224064
JB
7475 /* Size floating point instruction. */
7476 case LONG_MNEM_SUFFIX:
7477 if (i.tm.opcode_modifier.floatmf)
7478 {
7479 i.tm.base_opcode ^= 4;
7480 break;
7481 }
7482 /* fall through */
7483 case WORD_MNEM_SUFFIX:
7484 case QWORD_MNEM_SUFFIX:
29b0f896 7485 /* It's not a byte, select word/dword operation. */
40fb9820 7486 if (i.tm.opcode_modifier.w)
29b0f896 7487 {
50128d0c 7488 if (i.short_form)
29b0f896
AM
7489 i.tm.base_opcode |= 8;
7490 else
7491 i.tm.base_opcode |= 1;
7492 }
d2224064
JB
7493 /* fall through */
7494 case SHORT_MNEM_SUFFIX:
29b0f896
AM
7495 /* Now select between word & dword operations via the operand
7496 size prefix, except for instructions that will ignore this
7497 prefix anyway. */
c8f8eebc 7498 if (i.suffix != QWORD_MNEM_SUFFIX
3cd7f3e3 7499 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
c8f8eebc
JB
7500 && !i.tm.opcode_modifier.floatmf
7501 && !is_any_vex_encoding (&i.tm)
7502 && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
7503 || (flag_code == CODE_64BIT
7504 && i.tm.opcode_modifier.jump == JUMP_BYTE)))
24eab124
AM
7505 {
7506 unsigned int prefix = DATA_PREFIX_OPCODE;
543613e9 7507
0cfa3eb3 7508 if (i.tm.opcode_modifier.jump == JUMP_BYTE) /* jcxz, loop */
29b0f896 7509 prefix = ADDR_PREFIX_OPCODE;
252b5132 7510
29b0f896
AM
7511 if (!add_prefix (prefix))
7512 return 0;
24eab124 7513 }
252b5132 7514
29b0f896
AM
7515 /* Set mode64 for an operand. */
7516 if (i.suffix == QWORD_MNEM_SUFFIX
9146926a 7517 && flag_code == CODE_64BIT
d2224064 7518 && !i.tm.opcode_modifier.norex64
4ed21b58 7519 && !i.tm.opcode_modifier.vexw
46e883c5 7520 /* Special case for xchg %rax,%rax. It is NOP and doesn't
d2224064
JB
7521 need rex64. */
7522 && ! (i.operands == 2
7523 && i.tm.base_opcode == 0x90
44732014 7524 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
75e5731b
JB
7525 && i.types[0].bitfield.instance == Accum
7526 && i.types[0].bitfield.qword
44732014 7527 && i.types[1].bitfield.instance == Accum))
d2224064 7528 i.rex |= REX_W;
3e73aa7c 7529
d2224064 7530 break;
8bbb3ad8
JB
7531
7532 case 0:
f9a6a8f0 7533 /* Select word/dword/qword operation with explicit data sizing prefix
8bbb3ad8
JB
7534 when there are no suitable register operands. */
7535 if (i.tm.opcode_modifier.w
7536 && (i.prefix[DATA_PREFIX] || (i.prefix[REX_PREFIX] & REX_W))
7537 && (!i.reg_operands
7538 || (i.reg_operands == 1
7539 /* ShiftCount */
7540 && (i.tm.operand_types[0].bitfield.instance == RegC
7541 /* InOutPortReg */
7542 || i.tm.operand_types[0].bitfield.instance == RegD
7543 || i.tm.operand_types[1].bitfield.instance == RegD
7544 /* CRC32 */
8b65b895 7545 || is_crc32))))
8bbb3ad8
JB
7546 i.tm.base_opcode |= 1;
7547 break;
29b0f896 7548 }
7ecd2f8b 7549
255571cd 7550 if (i.tm.opcode_modifier.operandconstraint == ADDR_PREFIX_OP_REG)
c0a30a9f 7551 {
c8f8eebc
JB
7552 gas_assert (!i.suffix);
7553 gas_assert (i.reg_operands);
c0a30a9f 7554
c8f8eebc
JB
7555 if (i.tm.operand_types[0].bitfield.instance == Accum
7556 || i.operands == 1)
7557 {
7558 /* The address size override prefix changes the size of the
7559 first operand. */
7560 if (flag_code == CODE_64BIT
7561 && i.op[0].regs->reg_type.bitfield.word)
7562 {
7563 as_bad (_("16-bit addressing unavailable for `%s'"),
7564 i.tm.name);
7565 return 0;
7566 }
7567
7568 if ((flag_code == CODE_32BIT
7569 ? i.op[0].regs->reg_type.bitfield.word
7570 : i.op[0].regs->reg_type.bitfield.dword)
7571 && !add_prefix (ADDR_PREFIX_OPCODE))
7572 return 0;
7573 }
c0a30a9f
L
7574 else
7575 {
c8f8eebc
JB
7576 /* Check invalid register operand when the address size override
7577 prefix changes the size of register operands. */
7578 unsigned int op;
7579 enum { need_word, need_dword, need_qword } need;
7580
27f13469 7581 /* Check the register operand for the address size prefix if
b3a3496f 7582 the memory operand has no real registers, like symbol, DISP
829f3fe1 7583 or bogus (x32-only) symbol(%rip) when symbol(%eip) is meant. */
27f13469
L
7584 if (i.mem_operands == 1
7585 && i.reg_operands == 1
7586 && i.operands == 2
27f13469 7587 && i.types[1].bitfield.class == Reg
b3a3496f
L
7588 && (flag_code == CODE_32BIT
7589 ? i.op[1].regs->reg_type.bitfield.word
7590 : i.op[1].regs->reg_type.bitfield.dword)
7591 && ((i.base_reg == NULL && i.index_reg == NULL)
829f3fe1
JB
7592#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
7593 || (x86_elf_abi == X86_64_X32_ABI
7594 && i.base_reg
b3a3496f
L
7595 && i.base_reg->reg_num == RegIP
7596 && i.base_reg->reg_type.bitfield.qword))
829f3fe1
JB
7597#else
7598 || 0)
7599#endif
27f13469
L
7600 && !add_prefix (ADDR_PREFIX_OPCODE))
7601 return 0;
7602
c8f8eebc
JB
7603 if (flag_code == CODE_32BIT)
7604 need = i.prefix[ADDR_PREFIX] ? need_word : need_dword;
7605 else if (i.prefix[ADDR_PREFIX])
c0a30a9f
L
7606 need = need_dword;
7607 else
7608 need = flag_code == CODE_64BIT ? need_qword : need_word;
c0a30a9f 7609
c8f8eebc
JB
7610 for (op = 0; op < i.operands; op++)
7611 {
7612 if (i.types[op].bitfield.class != Reg)
7613 continue;
7614
7615 switch (need)
7616 {
7617 case need_word:
7618 if (i.op[op].regs->reg_type.bitfield.word)
7619 continue;
7620 break;
7621 case need_dword:
7622 if (i.op[op].regs->reg_type.bitfield.dword)
7623 continue;
7624 break;
7625 case need_qword:
7626 if (i.op[op].regs->reg_type.bitfield.qword)
7627 continue;
7628 break;
7629 }
7630
7631 as_bad (_("invalid register operand size for `%s'"),
7632 i.tm.name);
7633 return 0;
7634 }
7635 }
c0a30a9f
L
7636 }
7637
29b0f896
AM
7638 return 1;
7639}
3e73aa7c 7640
29b0f896 7641static int
543613e9 7642check_byte_reg (void)
29b0f896
AM
7643{
7644 int op;
543613e9 7645
29b0f896
AM
7646 for (op = i.operands; --op >= 0;)
7647 {
dc821c5f 7648 /* Skip non-register operands. */
bab6aec1 7649 if (i.types[op].bitfield.class != Reg)
dc821c5f
JB
7650 continue;
7651
29b0f896
AM
7652 /* If this is an eight bit register, it's OK. If it's the 16 or
7653 32 bit version of an eight bit register, we will just use the
7654 low portion, and that's OK too. */
dc821c5f 7655 if (i.types[op].bitfield.byte)
29b0f896
AM
7656 continue;
7657
5a819eb9 7658 /* I/O port address operands are OK too. */
75e5731b
JB
7659 if (i.tm.operand_types[op].bitfield.instance == RegD
7660 && i.tm.operand_types[op].bitfield.word)
5a819eb9
JB
7661 continue;
7662
9706160a 7663 /* crc32 only wants its source operand checked here. */
389d00a5
JB
7664 if (i.tm.base_opcode == 0xf0
7665 && i.tm.opcode_modifier.opcodespace == SPACE_0F38
8b65b895
L
7666 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2
7667 && op != 0)
9344ff29
L
7668 continue;
7669
29b0f896 7670 /* Any other register is bad. */
73c76375
JB
7671 as_bad (_("`%s%s' not allowed with `%s%c'"),
7672 register_prefix, i.op[op].regs->reg_name,
7673 i.tm.name, i.suffix);
7674 return 0;
29b0f896
AM
7675 }
7676 return 1;
7677}
7678
7679static int
e3bb37b5 7680check_long_reg (void)
29b0f896
AM
7681{
7682 int op;
7683
7684 for (op = i.operands; --op >= 0;)
dc821c5f 7685 /* Skip non-register operands. */
bab6aec1 7686 if (i.types[op].bitfield.class != Reg)
dc821c5f 7687 continue;
29b0f896
AM
7688 /* Reject eight bit registers, except where the template requires
7689 them. (eg. movzb) */
dc821c5f 7690 else if (i.types[op].bitfield.byte
bab6aec1 7691 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7692 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7693 && (i.tm.operand_types[op].bitfield.word
7694 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7695 {
a540244d
L
7696 as_bad (_("`%s%s' not allowed with `%s%c'"),
7697 register_prefix,
29b0f896
AM
7698 i.op[op].regs->reg_name,
7699 i.tm.name,
7700 i.suffix);
7701 return 0;
7702 }
be4c5e58
L
7703 /* Error if the e prefix on a general reg is missing. */
7704 else if (i.types[op].bitfield.word
bab6aec1 7705 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7706 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7707 && i.tm.operand_types[op].bitfield.dword)
29b0f896 7708 {
be4c5e58
L
7709 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7710 register_prefix, i.op[op].regs->reg_name,
7711 i.suffix);
7712 return 0;
252b5132 7713 }
e4630f71 7714 /* Warn if the r prefix on a general reg is present. */
dc821c5f 7715 else if (i.types[op].bitfield.qword
bab6aec1 7716 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7717 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7718 && i.tm.operand_types[op].bitfield.dword)
252b5132 7719 {
1cb0ab18
JB
7720 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7721 register_prefix, i.op[op].regs->reg_name, i.suffix);
7722 return 0;
29b0f896
AM
7723 }
7724 return 1;
7725}
252b5132 7726
29b0f896 7727static int
e3bb37b5 7728check_qword_reg (void)
29b0f896
AM
7729{
7730 int op;
252b5132 7731
29b0f896 7732 for (op = i.operands; --op >= 0; )
dc821c5f 7733 /* Skip non-register operands. */
bab6aec1 7734 if (i.types[op].bitfield.class != Reg)
dc821c5f 7735 continue;
29b0f896
AM
7736 /* Reject eight bit registers, except where the template requires
7737 them. (eg. movzb) */
dc821c5f 7738 else if (i.types[op].bitfield.byte
bab6aec1 7739 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7740 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7741 && (i.tm.operand_types[op].bitfield.word
7742 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7743 {
a540244d
L
7744 as_bad (_("`%s%s' not allowed with `%s%c'"),
7745 register_prefix,
29b0f896
AM
7746 i.op[op].regs->reg_name,
7747 i.tm.name,
7748 i.suffix);
7749 return 0;
7750 }
e4630f71 7751 /* Warn if the r prefix on a general reg is missing. */
dc821c5f
JB
7752 else if ((i.types[op].bitfield.word
7753 || i.types[op].bitfield.dword)
bab6aec1 7754 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7755 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7756 && i.tm.operand_types[op].bitfield.qword)
29b0f896
AM
7757 {
7758 /* Prohibit these changes in the 64bit mode, since the
7759 lowering is more complicated. */
1cb0ab18
JB
7760 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7761 register_prefix, i.op[op].regs->reg_name, i.suffix);
7762 return 0;
252b5132 7763 }
29b0f896
AM
7764 return 1;
7765}
252b5132 7766
29b0f896 7767static int
e3bb37b5 7768check_word_reg (void)
29b0f896
AM
7769{
7770 int op;
7771 for (op = i.operands; --op >= 0;)
dc821c5f 7772 /* Skip non-register operands. */
bab6aec1 7773 if (i.types[op].bitfield.class != Reg)
dc821c5f 7774 continue;
29b0f896
AM
7775 /* Reject eight bit registers, except where the template requires
7776 them. (eg. movzb) */
dc821c5f 7777 else if (i.types[op].bitfield.byte
bab6aec1 7778 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7779 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7780 && (i.tm.operand_types[op].bitfield.word
7781 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7782 {
a540244d
L
7783 as_bad (_("`%s%s' not allowed with `%s%c'"),
7784 register_prefix,
29b0f896
AM
7785 i.op[op].regs->reg_name,
7786 i.tm.name,
7787 i.suffix);
7788 return 0;
7789 }
9706160a
JB
7790 /* Error if the e or r prefix on a general reg is present. */
7791 else if ((i.types[op].bitfield.dword
dc821c5f 7792 || i.types[op].bitfield.qword)
bab6aec1 7793 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7794 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7795 && i.tm.operand_types[op].bitfield.word)
252b5132 7796 {
9706160a
JB
7797 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7798 register_prefix, i.op[op].regs->reg_name,
7799 i.suffix);
7800 return 0;
29b0f896
AM
7801 }
7802 return 1;
7803}
252b5132 7804
29b0f896 7805static int
40fb9820 7806update_imm (unsigned int j)
29b0f896 7807{
bc0844ae 7808 i386_operand_type overlap = i.types[j];
be1643ff
JB
7809 if (overlap.bitfield.imm8
7810 + overlap.bitfield.imm8s
7811 + overlap.bitfield.imm16
7812 + overlap.bitfield.imm32
7813 + overlap.bitfield.imm32s
7814 + overlap.bitfield.imm64 > 1)
29b0f896 7815 {
05909f23
JB
7816 static const i386_operand_type imm16 = { .bitfield = { .imm16 = 1 } };
7817 static const i386_operand_type imm32 = { .bitfield = { .imm32 = 1 } };
7818 static const i386_operand_type imm32s = { .bitfield = { .imm32s = 1 } };
7819 static const i386_operand_type imm16_32 = { .bitfield =
7820 { .imm16 = 1, .imm32 = 1 }
7821 };
7822 static const i386_operand_type imm16_32s = { .bitfield =
7823 { .imm16 = 1, .imm32s = 1 }
7824 };
7825 static const i386_operand_type imm16_32_32s = { .bitfield =
7826 { .imm16 = 1, .imm32 = 1, .imm32s = 1 }
7827 };
7828
29b0f896
AM
7829 if (i.suffix)
7830 {
40fb9820
L
7831 i386_operand_type temp;
7832
0dfbf9d7 7833 operand_type_set (&temp, 0);
7ab9ffdd 7834 if (i.suffix == BYTE_MNEM_SUFFIX)
40fb9820
L
7835 {
7836 temp.bitfield.imm8 = overlap.bitfield.imm8;
7837 temp.bitfield.imm8s = overlap.bitfield.imm8s;
7838 }
7839 else if (i.suffix == WORD_MNEM_SUFFIX)
7840 temp.bitfield.imm16 = overlap.bitfield.imm16;
7841 else if (i.suffix == QWORD_MNEM_SUFFIX)
7842 {
7843 temp.bitfield.imm64 = overlap.bitfield.imm64;
7844 temp.bitfield.imm32s = overlap.bitfield.imm32s;
7845 }
7846 else
7847 temp.bitfield.imm32 = overlap.bitfield.imm32;
7848 overlap = temp;
29b0f896 7849 }
0dfbf9d7
L
7850 else if (operand_type_equal (&overlap, &imm16_32_32s)
7851 || operand_type_equal (&overlap, &imm16_32)
7852 || operand_type_equal (&overlap, &imm16_32s))
29b0f896 7853 {
40fb9820 7854 if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
65da13b5 7855 overlap = imm16;
40fb9820 7856 else
65da13b5 7857 overlap = imm32s;
29b0f896 7858 }
8bbb3ad8
JB
7859 else if (i.prefix[REX_PREFIX] & REX_W)
7860 overlap = operand_type_and (overlap, imm32s);
7861 else if (i.prefix[DATA_PREFIX])
7862 overlap = operand_type_and (overlap,
7863 flag_code != CODE_16BIT ? imm16 : imm32);
be1643ff
JB
7864 if (overlap.bitfield.imm8
7865 + overlap.bitfield.imm8s
7866 + overlap.bitfield.imm16
7867 + overlap.bitfield.imm32
7868 + overlap.bitfield.imm32s
7869 + overlap.bitfield.imm64 != 1)
29b0f896 7870 {
4eed87de
AM
7871 as_bad (_("no instruction mnemonic suffix given; "
7872 "can't determine immediate size"));
29b0f896
AM
7873 return 0;
7874 }
7875 }
40fb9820 7876 i.types[j] = overlap;
29b0f896 7877
40fb9820
L
7878 return 1;
7879}
7880
7881static int
7882finalize_imm (void)
7883{
bc0844ae 7884 unsigned int j, n;
29b0f896 7885
bc0844ae
L
7886 /* Update the first 2 immediate operands. */
7887 n = i.operands > 2 ? 2 : i.operands;
7888 if (n)
7889 {
7890 for (j = 0; j < n; j++)
7891 if (update_imm (j) == 0)
7892 return 0;
40fb9820 7893
bc0844ae
L
7894 /* The 3rd operand can't be immediate operand. */
7895 gas_assert (operand_type_check (i.types[2], imm) == 0);
7896 }
29b0f896
AM
7897
7898 return 1;
7899}
7900
7901static int
e3bb37b5 7902process_operands (void)
29b0f896
AM
7903{
7904 /* Default segment register this instruction will use for memory
7905 accesses. 0 means unknown. This is only for optimizing out
7906 unnecessary segment overrides. */
5e042380 7907 const reg_entry *default_seg = NULL;
29b0f896 7908
a5aeccd9
JB
7909 if (i.tm.opcode_modifier.sse2avx)
7910 {
7911 /* Legacy encoded insns allow explicit REX prefixes, so these prefixes
7912 need converting. */
7913 i.rex |= i.prefix[REX_PREFIX] & (REX_W | REX_R | REX_X | REX_B);
7914 i.prefix[REX_PREFIX] = 0;
7915 i.rex_encoding = 0;
7916 }
c423d21a
JB
7917 /* ImmExt should be processed after SSE2AVX. */
7918 else if (i.tm.opcode_modifier.immext)
7919 process_immext ();
a5aeccd9 7920
2426c15f 7921 if (i.tm.opcode_modifier.sse2avx && i.tm.opcode_modifier.vexvvvv)
29b0f896 7922 {
05909f23
JB
7923 static const i386_operand_type regxmm = {
7924 .bitfield = { .class = RegSIMD, .xmmword = 1 }
7925 };
91d6fa6a
NC
7926 unsigned int dupl = i.operands;
7927 unsigned int dest = dupl - 1;
9fcfb3d7
L
7928 unsigned int j;
7929
c0f3af97 7930 /* The destination must be an xmm register. */
9c2799c2 7931 gas_assert (i.reg_operands
91d6fa6a 7932 && MAX_OPERANDS > dupl
7ab9ffdd 7933 && operand_type_equal (&i.types[dest], &regxmm));
c0f3af97 7934
75e5731b 7935 if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 7936 && i.tm.operand_types[0].bitfield.xmmword)
e2ec9d29 7937 {
8cd7925b 7938 if (i.tm.opcode_modifier.vexsources == VEX3SOURCES)
c0f3af97
L
7939 {
7940 /* Keep xmm0 for instructions with VEX prefix and 3
7941 sources. */
75e5731b 7942 i.tm.operand_types[0].bitfield.instance = InstanceNone;
3528c362 7943 i.tm.operand_types[0].bitfield.class = RegSIMD;
c0f3af97
L
7944 goto duplicate;
7945 }
e2ec9d29 7946 else
c0f3af97
L
7947 {
7948 /* We remove the first xmm0 and keep the number of
7949 operands unchanged, which in fact duplicates the
7950 destination. */
7951 for (j = 1; j < i.operands; j++)
7952 {
7953 i.op[j - 1] = i.op[j];
7954 i.types[j - 1] = i.types[j];
7955 i.tm.operand_types[j - 1] = i.tm.operand_types[j];
8dc0818e 7956 i.flags[j - 1] = i.flags[j];
c0f3af97
L
7957 }
7958 }
7959 }
255571cd 7960 else if (i.tm.opcode_modifier.operandconstraint == IMPLICIT_1ST_XMM0)
7ab9ffdd 7961 {
91d6fa6a 7962 gas_assert ((MAX_OPERANDS - 1) > dupl
8cd7925b
L
7963 && (i.tm.opcode_modifier.vexsources
7964 == VEX3SOURCES));
c0f3af97
L
7965
7966 /* Add the implicit xmm0 for instructions with VEX prefix
7967 and 3 sources. */
7968 for (j = i.operands; j > 0; j--)
7969 {
7970 i.op[j] = i.op[j - 1];
7971 i.types[j] = i.types[j - 1];
7972 i.tm.operand_types[j] = i.tm.operand_types[j - 1];
8dc0818e 7973 i.flags[j] = i.flags[j - 1];
c0f3af97
L
7974 }
7975 i.op[0].regs
629310ab 7976 = (const reg_entry *) str_hash_find (reg_hash, "xmm0");
7ab9ffdd 7977 i.types[0] = regxmm;
c0f3af97
L
7978 i.tm.operand_types[0] = regxmm;
7979
7980 i.operands += 2;
7981 i.reg_operands += 2;
7982 i.tm.operands += 2;
7983
91d6fa6a 7984 dupl++;
c0f3af97 7985 dest++;
91d6fa6a
NC
7986 i.op[dupl] = i.op[dest];
7987 i.types[dupl] = i.types[dest];
7988 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 7989 i.flags[dupl] = i.flags[dest];
e2ec9d29 7990 }
c0f3af97
L
7991 else
7992 {
dc1e8a47 7993 duplicate:
c0f3af97
L
7994 i.operands++;
7995 i.reg_operands++;
7996 i.tm.operands++;
7997
91d6fa6a
NC
7998 i.op[dupl] = i.op[dest];
7999 i.types[dupl] = i.types[dest];
8000 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 8001 i.flags[dupl] = i.flags[dest];
c0f3af97
L
8002 }
8003
8004 if (i.tm.opcode_modifier.immext)
8005 process_immext ();
8006 }
75e5731b 8007 else if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 8008 && i.tm.operand_types[0].bitfield.xmmword)
c0f3af97
L
8009 {
8010 unsigned int j;
8011
9fcfb3d7
L
8012 for (j = 1; j < i.operands; j++)
8013 {
8014 i.op[j - 1] = i.op[j];
8015 i.types[j - 1] = i.types[j];
8016
8017 /* We need to adjust fields in i.tm since they are used by
8018 build_modrm_byte. */
8019 i.tm.operand_types [j - 1] = i.tm.operand_types [j];
8dc0818e
JB
8020
8021 i.flags[j - 1] = i.flags[j];
9fcfb3d7
L
8022 }
8023
e2ec9d29
L
8024 i.operands--;
8025 i.reg_operands--;
e2ec9d29
L
8026 i.tm.operands--;
8027 }
255571cd 8028 else if (i.tm.opcode_modifier.operandconstraint == IMPLICIT_QUAD_GROUP)
920d2ddc 8029 {
a477a8c4
JB
8030 unsigned int regnum, first_reg_in_group, last_reg_in_group;
8031
920d2ddc 8032 /* The second operand must be {x,y,z}mmN, where N is a multiple of 4. */
3528c362 8033 gas_assert (i.operands >= 2 && i.types[1].bitfield.class == RegSIMD);
a477a8c4
JB
8034 regnum = register_number (i.op[1].regs);
8035 first_reg_in_group = regnum & ~3;
8036 last_reg_in_group = first_reg_in_group + 3;
8037 if (regnum != first_reg_in_group)
8038 as_warn (_("source register `%s%s' implicitly denotes"
8039 " `%s%.3s%u' to `%s%.3s%u' source group in `%s'"),
8040 register_prefix, i.op[1].regs->reg_name,
8041 register_prefix, i.op[1].regs->reg_name, first_reg_in_group,
8042 register_prefix, i.op[1].regs->reg_name, last_reg_in_group,
8043 i.tm.name);
8044 }
255571cd 8045 else if (i.tm.opcode_modifier.operandconstraint == REG_KLUDGE)
e2ec9d29
L
8046 {
8047 /* The imul $imm, %reg instruction is converted into
8048 imul $imm, %reg, %reg, and the clr %reg instruction
8049 is converted into xor %reg, %reg. */
8050
8051 unsigned int first_reg_op;
8052
8053 if (operand_type_check (i.types[0], reg))
8054 first_reg_op = 0;
8055 else
8056 first_reg_op = 1;
8057 /* Pretend we saw the extra register operand. */
9c2799c2 8058 gas_assert (i.reg_operands == 1
7ab9ffdd 8059 && i.op[first_reg_op + 1].regs == 0);
e2ec9d29
L
8060 i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
8061 i.types[first_reg_op + 1] = i.types[first_reg_op];
8062 i.operands++;
8063 i.reg_operands++;
29b0f896
AM
8064 }
8065
85b80b0f 8066 if (i.tm.opcode_modifier.modrm)
29b0f896
AM
8067 {
8068 /* The opcode is completed (modulo i.tm.extension_opcode which
52271982
AM
8069 must be put into the modrm byte). Now, we make the modrm and
8070 index base bytes based on all the info we've collected. */
29b0f896
AM
8071
8072 default_seg = build_modrm_byte ();
8073 }
00cee14f 8074 else if (i.types[0].bitfield.class == SReg)
85b80b0f
JB
8075 {
8076 if (flag_code != CODE_64BIT
8077 ? i.tm.base_opcode == POP_SEG_SHORT
8078 && i.op[0].regs->reg_num == 1
389d00a5 8079 : (i.tm.base_opcode | 1) == (POP_SEG386_SHORT & 0xff)
85b80b0f
JB
8080 && i.op[0].regs->reg_num < 4)
8081 {
8082 as_bad (_("you can't `%s %s%s'"),
8083 i.tm.name, register_prefix, i.op[0].regs->reg_name);
8084 return 0;
8085 }
389d00a5
JB
8086 if (i.op[0].regs->reg_num > 3
8087 && i.tm.opcode_modifier.opcodespace == SPACE_BASE )
85b80b0f 8088 {
389d00a5
JB
8089 i.tm.base_opcode ^= (POP_SEG_SHORT ^ POP_SEG386_SHORT) & 0xff;
8090 i.tm.opcode_modifier.opcodespace = SPACE_0F;
85b80b0f
JB
8091 }
8092 i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
8093 }
389d00a5
JB
8094 else if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
8095 && (i.tm.base_opcode & ~3) == MOV_AX_DISP32)
29b0f896 8096 {
5e042380 8097 default_seg = reg_ds;
29b0f896 8098 }
40fb9820 8099 else if (i.tm.opcode_modifier.isstring)
29b0f896
AM
8100 {
8101 /* For the string instructions that allow a segment override
8102 on one of their operands, the default segment is ds. */
5e042380 8103 default_seg = reg_ds;
29b0f896 8104 }
50128d0c 8105 else if (i.short_form)
85b80b0f
JB
8106 {
8107 /* The register or float register operand is in operand
8108 0 or 1. */
4943d587 8109 const reg_entry *r = i.op[0].regs;
85b80b0f 8110
4943d587
JB
8111 if (i.imm_operands
8112 || (r->reg_type.bitfield.instance == Accum && i.op[1].regs))
8113 r = i.op[1].regs;
85b80b0f 8114 /* Register goes in low 3 bits of opcode. */
4943d587
JB
8115 i.tm.base_opcode |= r->reg_num;
8116 if ((r->reg_flags & RegRex) != 0)
85b80b0f 8117 i.rex |= REX_B;
255571cd 8118 if (!quiet_warnings && i.tm.opcode_modifier.operandconstraint == UGH)
85b80b0f 8119 {
4943d587
JB
8120 /* Warn about some common errors, but press on regardless. */
8121 if (i.operands != 2)
85b80b0f
JB
8122 {
8123 /* Extraneous `l' suffix on fp insn. */
8124 as_warn (_("translating to `%s %s%s'"), i.tm.name,
8125 register_prefix, i.op[0].regs->reg_name);
8126 }
4943d587
JB
8127 else if (i.op[0].regs->reg_type.bitfield.instance != Accum)
8128 {
8129 /* Reversed arguments on faddp or fmulp. */
8130 as_warn (_("translating to `%s %s%s,%s%s'"), i.tm.name,
8131 register_prefix, i.op[!intel_syntax].regs->reg_name,
8132 register_prefix, i.op[intel_syntax].regs->reg_name);
8133 }
85b80b0f
JB
8134 }
8135 }
29b0f896 8136
514a8bb0 8137 if ((i.seg[0] || i.prefix[SEG_PREFIX])
514a8bb0 8138 && i.tm.base_opcode == 0x8d /* lea */
35648716 8139 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
514a8bb0 8140 && !is_any_vex_encoding(&i.tm))
92334ad2
JB
8141 {
8142 if (!quiet_warnings)
8143 as_warn (_("segment override on `%s' is ineffectual"), i.tm.name);
8144 if (optimize)
8145 {
8146 i.seg[0] = NULL;
8147 i.prefix[SEG_PREFIX] = 0;
8148 }
8149 }
52271982
AM
8150
8151 /* If a segment was explicitly specified, and the specified segment
b6773884
JB
8152 is neither the default nor the one already recorded from a prefix,
8153 use an opcode prefix to select it. If we never figured out what
8154 the default segment is, then default_seg will be zero at this
8155 point, and the specified segment prefix will always be used. */
8156 if (i.seg[0]
8157 && i.seg[0] != default_seg
5e042380 8158 && i386_seg_prefixes[i.seg[0]->reg_num] != i.prefix[SEG_PREFIX])
29b0f896 8159 {
5e042380 8160 if (!add_prefix (i386_seg_prefixes[i.seg[0]->reg_num]))
29b0f896
AM
8161 return 0;
8162 }
8163 return 1;
8164}
8165
a5aeccd9 8166static INLINE void set_rex_vrex (const reg_entry *r, unsigned int rex_bit,
5b7c81bd 8167 bool do_sse2avx)
a5aeccd9
JB
8168{
8169 if (r->reg_flags & RegRex)
8170 {
8171 if (i.rex & rex_bit)
8172 as_bad (_("same type of prefix used twice"));
8173 i.rex |= rex_bit;
8174 }
8175 else if (do_sse2avx && (i.rex & rex_bit) && i.vex.register_specifier)
8176 {
8177 gas_assert (i.vex.register_specifier == r);
8178 i.vex.register_specifier += 8;
8179 }
8180
8181 if (r->reg_flags & RegVRex)
8182 i.vrex |= rex_bit;
8183}
8184
5e042380 8185static const reg_entry *
e3bb37b5 8186build_modrm_byte (void)
29b0f896 8187{
5e042380 8188 const reg_entry *default_seg = NULL;
c0f3af97 8189 unsigned int source, dest;
8cd7925b 8190 int vex_3_sources;
c0f3af97 8191
8cd7925b 8192 vex_3_sources = i.tm.opcode_modifier.vexsources == VEX3SOURCES;
c0f3af97
L
8193 if (vex_3_sources)
8194 {
91d6fa6a 8195 unsigned int nds, reg_slot;
4c2c6516 8196 expressionS *exp;
c0f3af97 8197
6b8d3588 8198 dest = i.operands - 1;
c0f3af97 8199 nds = dest - 1;
922d8de8 8200
a683cc34 8201 /* There are 2 kinds of instructions:
bed3d976 8202 1. 5 operands: 4 register operands or 3 register operands
9d3bf266 8203 plus 1 memory operand plus one Imm4 operand, VexXDS, and
bed3d976 8204 VexW0 or VexW1. The destination must be either XMM, YMM or
43234a1e 8205 ZMM register.
bed3d976 8206 2. 4 operands: 4 register operands or 3 register operands
2f1bada2 8207 plus 1 memory operand, with VexXDS. */
922d8de8 8208 gas_assert ((i.reg_operands == 4
bed3d976
JB
8209 || (i.reg_operands == 3 && i.mem_operands == 1))
8210 && i.tm.opcode_modifier.vexvvvv == VEXXDS
dcd7e323 8211 && i.tm.opcode_modifier.vexw
3528c362 8212 && i.tm.operand_types[dest].bitfield.class == RegSIMD);
a683cc34 8213
48db9223
JB
8214 /* If VexW1 is set, the first non-immediate operand is the source and
8215 the second non-immediate one is encoded in the immediate operand. */
8216 if (i.tm.opcode_modifier.vexw == VEXW1)
8217 {
8218 source = i.imm_operands;
8219 reg_slot = i.imm_operands + 1;
8220 }
8221 else
8222 {
8223 source = i.imm_operands + 1;
8224 reg_slot = i.imm_operands;
8225 }
8226
a683cc34 8227 if (i.imm_operands == 0)
bed3d976
JB
8228 {
8229 /* When there is no immediate operand, generate an 8bit
8230 immediate operand to encode the first operand. */
8231 exp = &im_expressions[i.imm_operands++];
8232 i.op[i.operands].imms = exp;
be1643ff 8233 i.types[i.operands].bitfield.imm8 = 1;
bed3d976
JB
8234 i.operands++;
8235
3528c362 8236 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
bed3d976
JB
8237 exp->X_op = O_constant;
8238 exp->X_add_number = register_number (i.op[reg_slot].regs) << 4;
43234a1e
L
8239 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
8240 }
922d8de8 8241 else
bed3d976 8242 {
9d3bf266
JB
8243 gas_assert (i.imm_operands == 1);
8244 gas_assert (fits_in_imm4 (i.op[0].imms->X_add_number));
8245 gas_assert (!i.tm.opcode_modifier.immext);
a683cc34 8246
9d3bf266
JB
8247 /* Turn on Imm8 again so that output_imm will generate it. */
8248 i.types[0].bitfield.imm8 = 1;
bed3d976 8249
3528c362 8250 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
9d3bf266 8251 i.op[0].imms->X_add_number
bed3d976 8252 |= register_number (i.op[reg_slot].regs) << 4;
43234a1e 8253 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
bed3d976 8254 }
a683cc34 8255
3528c362 8256 gas_assert (i.tm.operand_types[nds].bitfield.class == RegSIMD);
dae39acc 8257 i.vex.register_specifier = i.op[nds].regs;
c0f3af97
L
8258 }
8259 else
8260 source = dest = 0;
29b0f896
AM
8261
8262 /* i.reg_operands MUST be the number of real register operands;
c0f3af97
L
8263 implicit registers do not count. If there are 3 register
8264 operands, it must be a instruction with VexNDS. For a
8265 instruction with VexNDD, the destination register is encoded
8266 in VEX prefix. If there are 4 register operands, it must be
8267 a instruction with VEX prefix and 3 sources. */
7ab9ffdd
L
8268 if (i.mem_operands == 0
8269 && ((i.reg_operands == 2
2426c15f 8270 && i.tm.opcode_modifier.vexvvvv <= VEXXDS)
7ab9ffdd 8271 || (i.reg_operands == 3
2426c15f 8272 && i.tm.opcode_modifier.vexvvvv == VEXXDS)
7ab9ffdd 8273 || (i.reg_operands == 4 && vex_3_sources)))
29b0f896 8274 {
cab737b9
L
8275 switch (i.operands)
8276 {
8277 case 2:
8278 source = 0;
8279 break;
8280 case 3:
c81128dc
L
8281 /* When there are 3 operands, one of them may be immediate,
8282 which may be the first or the last operand. Otherwise,
c0f3af97
L
8283 the first operand must be shift count register (cl) or it
8284 is an instruction with VexNDS. */
9c2799c2 8285 gas_assert (i.imm_operands == 1
7ab9ffdd 8286 || (i.imm_operands == 0
2426c15f 8287 && (i.tm.opcode_modifier.vexvvvv == VEXXDS
75e5731b
JB
8288 || (i.types[0].bitfield.instance == RegC
8289 && i.types[0].bitfield.byte))));
40fb9820 8290 if (operand_type_check (i.types[0], imm)
75e5731b
JB
8291 || (i.types[0].bitfield.instance == RegC
8292 && i.types[0].bitfield.byte))
40fb9820
L
8293 source = 1;
8294 else
8295 source = 0;
cab737b9
L
8296 break;
8297 case 4:
368d64cc
L
8298 /* When there are 4 operands, the first two must be 8bit
8299 immediate operands. The source operand will be the 3rd
c0f3af97
L
8300 one.
8301
8302 For instructions with VexNDS, if the first operand
8303 an imm8, the source operand is the 2nd one. If the last
8304 operand is imm8, the source operand is the first one. */
9c2799c2 8305 gas_assert ((i.imm_operands == 2
7ab9ffdd
L
8306 && i.types[0].bitfield.imm8
8307 && i.types[1].bitfield.imm8)
2426c15f 8308 || (i.tm.opcode_modifier.vexvvvv == VEXXDS
7ab9ffdd
L
8309 && i.imm_operands == 1
8310 && (i.types[0].bitfield.imm8
cf665fee 8311 || i.types[i.operands - 1].bitfield.imm8)));
9f2670f2
L
8312 if (i.imm_operands == 2)
8313 source = 2;
8314 else
c0f3af97
L
8315 {
8316 if (i.types[0].bitfield.imm8)
8317 source = 1;
8318 else
8319 source = 0;
8320 }
c0f3af97
L
8321 break;
8322 case 5:
cf665fee
JB
8323 gas_assert (!is_evex_encoding (&i.tm));
8324 gas_assert (i.imm_operands == 1 && vex_3_sources);
cab737b9
L
8325 break;
8326 default:
8327 abort ();
8328 }
8329
c0f3af97
L
8330 if (!vex_3_sources)
8331 {
8332 dest = source + 1;
8333
2426c15f 8334 if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
c0f3af97 8335 {
43234a1e 8336 /* For instructions with VexNDS, the register-only source
c5d0745b 8337 operand must be a 32/64bit integer, XMM, YMM, ZMM, or mask
dfd69174 8338 register. It is encoded in VEX prefix. */
f12dc422
L
8339
8340 i386_operand_type op;
8341 unsigned int vvvv;
8342
c2ecccb3 8343 /* Swap two source operands if needed. */
255571cd 8344 if (i.tm.opcode_modifier.operandconstraint == SWAP_SOURCES)
f12dc422
L
8345 {
8346 vvvv = source;
8347 source = dest;
8348 }
8349 else
8350 vvvv = dest;
8351
8352 op = i.tm.operand_types[vvvv];
c0f3af97 8353 if ((dest + 1) >= i.operands
bab6aec1 8354 || ((op.bitfield.class != Reg
dc821c5f 8355 || (!op.bitfield.dword && !op.bitfield.qword))
3528c362 8356 && op.bitfield.class != RegSIMD
c0f327b8 8357 && op.bitfield.class != RegMask))
c0f3af97 8358 abort ();
f12dc422 8359 i.vex.register_specifier = i.op[vvvv].regs;
c0f3af97
L
8360 dest++;
8361 }
8362 }
29b0f896
AM
8363
8364 i.rm.mode = 3;
dfd69174
JB
8365 /* One of the register operands will be encoded in the i.rm.reg
8366 field, the other in the combined i.rm.mode and i.rm.regmem
29b0f896
AM
8367 fields. If no form of this instruction supports a memory
8368 destination operand, then we assume the source operand may
8369 sometimes be a memory operand and so we need to store the
8370 destination in the i.rm.reg field. */
dfd69174 8371 if (!i.tm.opcode_modifier.regmem
40fb9820 8372 && operand_type_check (i.tm.operand_types[dest], anymem) == 0)
29b0f896
AM
8373 {
8374 i.rm.reg = i.op[dest].regs->reg_num;
8375 i.rm.regmem = i.op[source].regs->reg_num;
a5aeccd9 8376 set_rex_vrex (i.op[dest].regs, REX_R, i.tm.opcode_modifier.sse2avx);
5b7c81bd 8377 set_rex_vrex (i.op[source].regs, REX_B, false);
29b0f896
AM
8378 }
8379 else
8380 {
8381 i.rm.reg = i.op[source].regs->reg_num;
8382 i.rm.regmem = i.op[dest].regs->reg_num;
a5aeccd9 8383 set_rex_vrex (i.op[dest].regs, REX_B, i.tm.opcode_modifier.sse2avx);
5b7c81bd 8384 set_rex_vrex (i.op[source].regs, REX_R, false);
29b0f896 8385 }
e0c7f900 8386 if (flag_code != CODE_64BIT && (i.rex & REX_R))
c4a530c5 8387 {
4a5c67ed 8388 if (i.types[!i.tm.opcode_modifier.regmem].bitfield.class != RegCR)
c4a530c5 8389 abort ();
e0c7f900 8390 i.rex &= ~REX_R;
c4a530c5
JB
8391 add_prefix (LOCK_PREFIX_OPCODE);
8392 }
29b0f896
AM
8393 }
8394 else
8395 { /* If it's not 2 reg operands... */
c0f3af97
L
8396 unsigned int mem;
8397
29b0f896
AM
8398 if (i.mem_operands)
8399 {
8400 unsigned int fake_zero_displacement = 0;
99018f42 8401 unsigned int op;
4eed87de 8402
7ab9ffdd 8403 for (op = 0; op < i.operands; op++)
8dc0818e 8404 if (i.flags[op] & Operand_Mem)
7ab9ffdd 8405 break;
7ab9ffdd 8406 gas_assert (op < i.operands);
29b0f896 8407
63112cd6 8408 if (i.tm.opcode_modifier.sib)
6c30d220 8409 {
260cd341
LC
8410 /* The index register of VSIB shouldn't be RegIZ. */
8411 if (i.tm.opcode_modifier.sib != SIBMEM
8412 && i.index_reg->reg_num == RegIZ)
6c30d220
L
8413 abort ();
8414
8415 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8416 if (!i.base_reg)
8417 {
8418 i.sib.base = NO_BASE_REGISTER;
8419 i.sib.scale = i.log2_scale_factor;
2f2be86b 8420 i.types[op] = operand_type_and_not (i.types[op], anydisp);
a775efc8 8421 i.types[op].bitfield.disp32 = 1;
6c30d220 8422 }
260cd341
LC
8423
8424 /* Since the mandatory SIB always has index register, so
8425 the code logic remains unchanged. The non-mandatory SIB
8426 without index register is allowed and will be handled
8427 later. */
8428 if (i.index_reg)
8429 {
8430 if (i.index_reg->reg_num == RegIZ)
8431 i.sib.index = NO_INDEX_REGISTER;
8432 else
8433 i.sib.index = i.index_reg->reg_num;
5b7c81bd 8434 set_rex_vrex (i.index_reg, REX_X, false);
260cd341 8435 }
6c30d220
L
8436 }
8437
5e042380 8438 default_seg = reg_ds;
29b0f896
AM
8439
8440 if (i.base_reg == 0)
8441 {
8442 i.rm.mode = 0;
8443 if (!i.disp_operands)
9bb129e8 8444 fake_zero_displacement = 1;
29b0f896
AM
8445 if (i.index_reg == 0)
8446 {
260cd341
LC
8447 /* Both check for VSIB and mandatory non-vector SIB. */
8448 gas_assert (!i.tm.opcode_modifier.sib
8449 || i.tm.opcode_modifier.sib == SIBMEM);
29b0f896 8450 /* Operand is just <disp> */
2f2be86b 8451 i.types[op] = operand_type_and_not (i.types[op], anydisp);
20f0a1fc 8452 if (flag_code == CODE_64BIT)
29b0f896
AM
8453 {
8454 /* 64bit mode overwrites the 32bit absolute
8455 addressing by RIP relative addressing and
8456 absolute addressing is encoded by one of the
8457 redundant SIB forms. */
8458 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8459 i.sib.base = NO_BASE_REGISTER;
8460 i.sib.index = NO_INDEX_REGISTER;
a775efc8 8461 i.types[op].bitfield.disp32 = 1;
20f0a1fc 8462 }
fc225355
L
8463 else if ((flag_code == CODE_16BIT)
8464 ^ (i.prefix[ADDR_PREFIX] != 0))
20f0a1fc
NC
8465 {
8466 i.rm.regmem = NO_BASE_REGISTER_16;
2f2be86b 8467 i.types[op].bitfield.disp16 = 1;
20f0a1fc
NC
8468 }
8469 else
8470 {
8471 i.rm.regmem = NO_BASE_REGISTER;
2f2be86b 8472 i.types[op].bitfield.disp32 = 1;
29b0f896
AM
8473 }
8474 }
63112cd6 8475 else if (!i.tm.opcode_modifier.sib)
29b0f896 8476 {
6c30d220 8477 /* !i.base_reg && i.index_reg */
e968fc9b 8478 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8479 i.sib.index = NO_INDEX_REGISTER;
8480 else
8481 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8482 i.sib.base = NO_BASE_REGISTER;
8483 i.sib.scale = i.log2_scale_factor;
8484 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
2f2be86b 8485 i.types[op] = operand_type_and_not (i.types[op], anydisp);
a775efc8 8486 i.types[op].bitfield.disp32 = 1;
29b0f896 8487 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8488 i.rex |= REX_X;
29b0f896
AM
8489 }
8490 }
8491 /* RIP addressing for 64bit mode. */
e968fc9b 8492 else if (i.base_reg->reg_num == RegIP)
29b0f896 8493 {
63112cd6 8494 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896 8495 i.rm.regmem = NO_BASE_REGISTER;
40fb9820
L
8496 i.types[op].bitfield.disp8 = 0;
8497 i.types[op].bitfield.disp16 = 0;
a775efc8 8498 i.types[op].bitfield.disp32 = 1;
40fb9820 8499 i.types[op].bitfield.disp64 = 0;
71903a11 8500 i.flags[op] |= Operand_PCrel;
20f0a1fc
NC
8501 if (! i.disp_operands)
8502 fake_zero_displacement = 1;
29b0f896 8503 }
dc821c5f 8504 else if (i.base_reg->reg_type.bitfield.word)
29b0f896 8505 {
63112cd6 8506 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896
AM
8507 switch (i.base_reg->reg_num)
8508 {
8509 case 3: /* (%bx) */
8510 if (i.index_reg == 0)
8511 i.rm.regmem = 7;
8512 else /* (%bx,%si) -> 0, or (%bx,%di) -> 1 */
8513 i.rm.regmem = i.index_reg->reg_num - 6;
8514 break;
8515 case 5: /* (%bp) */
5e042380 8516 default_seg = reg_ss;
29b0f896
AM
8517 if (i.index_reg == 0)
8518 {
8519 i.rm.regmem = 6;
40fb9820 8520 if (operand_type_check (i.types[op], disp) == 0)
29b0f896
AM
8521 {
8522 /* fake (%bp) into 0(%bp) */
41eb8e88 8523 if (i.disp_encoding == disp_encoding_16bit)
1a02d6b0
L
8524 i.types[op].bitfield.disp16 = 1;
8525 else
8526 i.types[op].bitfield.disp8 = 1;
252b5132 8527 fake_zero_displacement = 1;
29b0f896
AM
8528 }
8529 }
8530 else /* (%bp,%si) -> 2, or (%bp,%di) -> 3 */
8531 i.rm.regmem = i.index_reg->reg_num - 6 + 2;
8532 break;
8533 default: /* (%si) -> 4 or (%di) -> 5 */
8534 i.rm.regmem = i.base_reg->reg_num - 6 + 4;
8535 }
41eb8e88
L
8536 if (!fake_zero_displacement
8537 && !i.disp_operands
8538 && i.disp_encoding)
8539 {
8540 fake_zero_displacement = 1;
8541 if (i.disp_encoding == disp_encoding_8bit)
8542 i.types[op].bitfield.disp8 = 1;
8543 else
8544 i.types[op].bitfield.disp16 = 1;
8545 }
29b0f896
AM
8546 i.rm.mode = mode_from_disp_size (i.types[op]);
8547 }
8548 else /* i.base_reg and 32/64 bit mode */
8549 {
a9aabc23 8550 if (operand_type_check (i.types[op], disp))
40fb9820 8551 {
73053c1f
JB
8552 i.types[op].bitfield.disp16 = 0;
8553 i.types[op].bitfield.disp64 = 0;
a775efc8 8554 i.types[op].bitfield.disp32 = 1;
40fb9820 8555 }
20f0a1fc 8556
63112cd6 8557 if (!i.tm.opcode_modifier.sib)
6c30d220 8558 i.rm.regmem = i.base_reg->reg_num;
29b0f896 8559 if ((i.base_reg->reg_flags & RegRex) != 0)
161a04f6 8560 i.rex |= REX_B;
29b0f896
AM
8561 i.sib.base = i.base_reg->reg_num;
8562 /* x86-64 ignores REX prefix bit here to avoid decoder
8563 complications. */
848930b2
JB
8564 if (!(i.base_reg->reg_flags & RegRex)
8565 && (i.base_reg->reg_num == EBP_REG_NUM
8566 || i.base_reg->reg_num == ESP_REG_NUM))
5e042380 8567 default_seg = reg_ss;
848930b2 8568 if (i.base_reg->reg_num == 5 && i.disp_operands == 0)
29b0f896 8569 {
848930b2 8570 fake_zero_displacement = 1;
1a02d6b0
L
8571 if (i.disp_encoding == disp_encoding_32bit)
8572 i.types[op].bitfield.disp32 = 1;
8573 else
8574 i.types[op].bitfield.disp8 = 1;
29b0f896
AM
8575 }
8576 i.sib.scale = i.log2_scale_factor;
8577 if (i.index_reg == 0)
8578 {
260cd341
LC
8579 /* Only check for VSIB. */
8580 gas_assert (i.tm.opcode_modifier.sib != VECSIB128
8581 && i.tm.opcode_modifier.sib != VECSIB256
8582 && i.tm.opcode_modifier.sib != VECSIB512);
8583
29b0f896
AM
8584 /* <disp>(%esp) becomes two byte modrm with no index
8585 register. We've already stored the code for esp
8586 in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
8587 Any base register besides %esp will not use the
8588 extra modrm byte. */
8589 i.sib.index = NO_INDEX_REGISTER;
29b0f896 8590 }
63112cd6 8591 else if (!i.tm.opcode_modifier.sib)
29b0f896 8592 {
e968fc9b 8593 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8594 i.sib.index = NO_INDEX_REGISTER;
8595 else
8596 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8597 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8598 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8599 i.rex |= REX_X;
29b0f896 8600 }
67a4f2b7
AO
8601
8602 if (i.disp_operands
8603 && (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
8604 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL))
8605 i.rm.mode = 0;
8606 else
a501d77e
L
8607 {
8608 if (!fake_zero_displacement
8609 && !i.disp_operands
8610 && i.disp_encoding)
8611 {
8612 fake_zero_displacement = 1;
8613 if (i.disp_encoding == disp_encoding_8bit)
8614 i.types[op].bitfield.disp8 = 1;
8615 else
8616 i.types[op].bitfield.disp32 = 1;
8617 }
8618 i.rm.mode = mode_from_disp_size (i.types[op]);
8619 }
29b0f896 8620 }
252b5132 8621
29b0f896
AM
8622 if (fake_zero_displacement)
8623 {
8624 /* Fakes a zero displacement assuming that i.types[op]
8625 holds the correct displacement size. */
8626 expressionS *exp;
8627
9c2799c2 8628 gas_assert (i.op[op].disps == 0);
29b0f896
AM
8629 exp = &disp_expressions[i.disp_operands++];
8630 i.op[op].disps = exp;
8631 exp->X_op = O_constant;
8632 exp->X_add_number = 0;
8633 exp->X_add_symbol = (symbolS *) 0;
8634 exp->X_op_symbol = (symbolS *) 0;
8635 }
c0f3af97
L
8636
8637 mem = op;
29b0f896 8638 }
c0f3af97
L
8639 else
8640 mem = ~0;
252b5132 8641
8c43a48b 8642 if (i.tm.opcode_modifier.vexsources == XOP2SOURCES)
5dd85c99
SP
8643 {
8644 if (operand_type_check (i.types[0], imm))
8645 i.vex.register_specifier = NULL;
8646 else
8647 {
8648 /* VEX.vvvv encodes one of the sources when the first
8649 operand is not an immediate. */
1ef99a7b 8650 if (i.tm.opcode_modifier.vexw == VEXW0)
5dd85c99
SP
8651 i.vex.register_specifier = i.op[0].regs;
8652 else
8653 i.vex.register_specifier = i.op[1].regs;
8654 }
8655
8656 /* Destination is a XMM register encoded in the ModRM.reg
8657 and VEX.R bit. */
8658 i.rm.reg = i.op[2].regs->reg_num;
8659 if ((i.op[2].regs->reg_flags & RegRex) != 0)
8660 i.rex |= REX_R;
8661
8662 /* ModRM.rm and VEX.B encodes the other source. */
8663 if (!i.mem_operands)
8664 {
8665 i.rm.mode = 3;
8666
1ef99a7b 8667 if (i.tm.opcode_modifier.vexw == VEXW0)
5dd85c99
SP
8668 i.rm.regmem = i.op[1].regs->reg_num;
8669 else
8670 i.rm.regmem = i.op[0].regs->reg_num;
8671
8672 if ((i.op[1].regs->reg_flags & RegRex) != 0)
8673 i.rex |= REX_B;
8674 }
8675 }
2426c15f 8676 else if (i.tm.opcode_modifier.vexvvvv == VEXLWP)
f88c9eb0
SP
8677 {
8678 i.vex.register_specifier = i.op[2].regs;
8679 if (!i.mem_operands)
8680 {
8681 i.rm.mode = 3;
8682 i.rm.regmem = i.op[1].regs->reg_num;
8683 if ((i.op[1].regs->reg_flags & RegRex) != 0)
8684 i.rex |= REX_B;
8685 }
8686 }
29b0f896
AM
8687 /* Fill in i.rm.reg or i.rm.regmem field with register operand
8688 (if any) based on i.tm.extension_opcode. Again, we must be
8689 careful to make sure that segment/control/debug/test/MMX
8690 registers are coded into the i.rm.reg field. */
f88c9eb0 8691 else if (i.reg_operands)
29b0f896 8692 {
99018f42 8693 unsigned int op;
7ab9ffdd
L
8694 unsigned int vex_reg = ~0;
8695
8696 for (op = 0; op < i.operands; op++)
921eafea
L
8697 if (i.types[op].bitfield.class == Reg
8698 || i.types[op].bitfield.class == RegBND
8699 || i.types[op].bitfield.class == RegMask
8700 || i.types[op].bitfield.class == SReg
8701 || i.types[op].bitfield.class == RegCR
8702 || i.types[op].bitfield.class == RegDR
8703 || i.types[op].bitfield.class == RegTR
8704 || i.types[op].bitfield.class == RegSIMD
8705 || i.types[op].bitfield.class == RegMMX)
8706 break;
c0209578 8707
7ab9ffdd
L
8708 if (vex_3_sources)
8709 op = dest;
2426c15f 8710 else if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
7ab9ffdd
L
8711 {
8712 /* For instructions with VexNDS, the register-only
8713 source operand is encoded in VEX prefix. */
8714 gas_assert (mem != (unsigned int) ~0);
c0f3af97 8715
a93e3234 8716 if (op > mem || i.tm.cpu_flags.bitfield.cpucmpccxadd)
c0f3af97 8717 {
7ab9ffdd
L
8718 vex_reg = op++;
8719 gas_assert (op < i.operands);
c0f3af97
L
8720 }
8721 else
c0f3af97 8722 {
f12dc422
L
8723 /* Check register-only source operand when two source
8724 operands are swapped. */
8725 if (!i.tm.operand_types[op].bitfield.baseindex
8726 && i.tm.operand_types[op + 1].bitfield.baseindex)
8727 {
8728 vex_reg = op;
8729 op += 2;
8730 gas_assert (mem == (vex_reg + 1)
8731 && op < i.operands);
8732 }
8733 else
8734 {
8735 vex_reg = op + 1;
8736 gas_assert (vex_reg < i.operands);
8737 }
c0f3af97 8738 }
7ab9ffdd 8739 }
2426c15f 8740 else if (i.tm.opcode_modifier.vexvvvv == VEXNDD)
7ab9ffdd 8741 {
f12dc422 8742 /* For instructions with VexNDD, the register destination
7ab9ffdd 8743 is encoded in VEX prefix. */
f12dc422
L
8744 if (i.mem_operands == 0)
8745 {
8746 /* There is no memory operand. */
8747 gas_assert ((op + 2) == i.operands);
8748 vex_reg = op + 1;
8749 }
8750 else
8d63c93e 8751 {
ed438a93
JB
8752 /* There are only 2 non-immediate operands. */
8753 gas_assert (op < i.imm_operands + 2
8754 && i.operands == i.imm_operands + 2);
8755 vex_reg = i.imm_operands + 1;
f12dc422 8756 }
7ab9ffdd
L
8757 }
8758 else
8759 gas_assert (op < i.operands);
99018f42 8760
7ab9ffdd
L
8761 if (vex_reg != (unsigned int) ~0)
8762 {
f12dc422 8763 i386_operand_type *type = &i.tm.operand_types[vex_reg];
7ab9ffdd 8764
bab6aec1 8765 if ((type->bitfield.class != Reg
dc821c5f 8766 || (!type->bitfield.dword && !type->bitfield.qword))
3528c362 8767 && type->bitfield.class != RegSIMD
c0f327b8 8768 && type->bitfield.class != RegMask)
7ab9ffdd 8769 abort ();
f88c9eb0 8770
7ab9ffdd
L
8771 i.vex.register_specifier = i.op[vex_reg].regs;
8772 }
8773
1b9f0c97
L
8774 /* Don't set OP operand twice. */
8775 if (vex_reg != op)
7ab9ffdd 8776 {
1b9f0c97
L
8777 /* If there is an extension opcode to put here, the
8778 register number must be put into the regmem field. */
8779 if (i.tm.extension_opcode != None)
8780 {
8781 i.rm.regmem = i.op[op].regs->reg_num;
a5aeccd9
JB
8782 set_rex_vrex (i.op[op].regs, REX_B,
8783 i.tm.opcode_modifier.sse2avx);
1b9f0c97
L
8784 }
8785 else
8786 {
8787 i.rm.reg = i.op[op].regs->reg_num;
a5aeccd9
JB
8788 set_rex_vrex (i.op[op].regs, REX_R,
8789 i.tm.opcode_modifier.sse2avx);
1b9f0c97 8790 }
7ab9ffdd 8791 }
252b5132 8792
29b0f896
AM
8793 /* Now, if no memory operand has set i.rm.mode = 0, 1, 2 we
8794 must set it to 3 to indicate this is a register operand
8795 in the regmem field. */
8796 if (!i.mem_operands)
8797 i.rm.mode = 3;
8798 }
252b5132 8799
29b0f896 8800 /* Fill in i.rm.reg field with extension opcode (if any). */
c1e679ec 8801 if (i.tm.extension_opcode != None)
29b0f896
AM
8802 i.rm.reg = i.tm.extension_opcode;
8803 }
8804 return default_seg;
8805}
252b5132 8806
48ef937e
JB
8807static INLINE void
8808frag_opcode_byte (unsigned char byte)
8809{
8810 if (now_seg != absolute_section)
8811 FRAG_APPEND_1_CHAR (byte);
8812 else
8813 ++abs_section_offset;
8814}
8815
376cd056
JB
8816static unsigned int
8817flip_code16 (unsigned int code16)
8818{
8819 gas_assert (i.tm.operands == 1);
8820
8821 return !(i.prefix[REX_PREFIX] & REX_W)
8822 && (code16 ? i.tm.operand_types[0].bitfield.disp32
376cd056
JB
8823 : i.tm.operand_types[0].bitfield.disp16)
8824 ? CODE16 : 0;
8825}
8826
29b0f896 8827static void
e3bb37b5 8828output_branch (void)
29b0f896
AM
8829{
8830 char *p;
f8a5c266 8831 int size;
29b0f896
AM
8832 int code16;
8833 int prefix;
8834 relax_substateT subtype;
8835 symbolS *sym;
8836 offsetT off;
8837
48ef937e
JB
8838 if (now_seg == absolute_section)
8839 {
8840 as_bad (_("relaxable branches not supported in absolute section"));
8841 return;
8842 }
8843
f8a5c266 8844 code16 = flag_code == CODE_16BIT ? CODE16 : 0;
1a42a9fe 8845 size = i.disp_encoding > disp_encoding_8bit ? BIG : SMALL;
29b0f896
AM
8846
8847 prefix = 0;
8848 if (i.prefix[DATA_PREFIX] != 0)
252b5132 8849 {
29b0f896
AM
8850 prefix = 1;
8851 i.prefixes -= 1;
376cd056 8852 code16 ^= flip_code16(code16);
252b5132 8853 }
29b0f896
AM
8854 /* Pentium4 branch hints. */
8855 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8856 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
2f66722d 8857 {
29b0f896
AM
8858 prefix++;
8859 i.prefixes--;
8860 }
8861 if (i.prefix[REX_PREFIX] != 0)
8862 {
8863 prefix++;
8864 i.prefixes--;
2f66722d
AM
8865 }
8866
7e8b059b
L
8867 /* BND prefixed jump. */
8868 if (i.prefix[BND_PREFIX] != 0)
8869 {
6cb0a70e
JB
8870 prefix++;
8871 i.prefixes--;
7e8b059b
L
8872 }
8873
f2810fe0
JB
8874 if (i.prefixes != 0)
8875 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
29b0f896
AM
8876
8877 /* It's always a symbol; End frag & setup for relax.
8878 Make sure there is enough room in this frag for the largest
8879 instruction we may generate in md_convert_frag. This is 2
8880 bytes for the opcode and room for the prefix and largest
8881 displacement. */
8882 frag_grow (prefix + 2 + 4);
8883 /* Prefix and 1 opcode byte go in fr_fix. */
8884 p = frag_more (prefix + 1);
8885 if (i.prefix[DATA_PREFIX] != 0)
8886 *p++ = DATA_PREFIX_OPCODE;
8887 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
8888 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
8889 *p++ = i.prefix[SEG_PREFIX];
6cb0a70e
JB
8890 if (i.prefix[BND_PREFIX] != 0)
8891 *p++ = BND_PREFIX_OPCODE;
29b0f896
AM
8892 if (i.prefix[REX_PREFIX] != 0)
8893 *p++ = i.prefix[REX_PREFIX];
8894 *p = i.tm.base_opcode;
8895
8896 if ((unsigned char) *p == JUMP_PC_RELATIVE)
f8a5c266 8897 subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, size);
40fb9820 8898 else if (cpu_arch_flags.bitfield.cpui386)
f8a5c266 8899 subtype = ENCODE_RELAX_STATE (COND_JUMP, size);
29b0f896 8900 else
f8a5c266 8901 subtype = ENCODE_RELAX_STATE (COND_JUMP86, size);
29b0f896 8902 subtype |= code16;
3e73aa7c 8903
29b0f896
AM
8904 sym = i.op[0].disps->X_add_symbol;
8905 off = i.op[0].disps->X_add_number;
3e73aa7c 8906
29b0f896
AM
8907 if (i.op[0].disps->X_op != O_constant
8908 && i.op[0].disps->X_op != O_symbol)
3e73aa7c 8909 {
29b0f896
AM
8910 /* Handle complex expressions. */
8911 sym = make_expr_symbol (i.op[0].disps);
8912 off = 0;
8913 }
3e73aa7c 8914
1ef3994a
JB
8915 frag_now->tc_frag_data.code64 = flag_code == CODE_64BIT;
8916
29b0f896
AM
8917 /* 1 possible extra opcode + 4 byte displacement go in var part.
8918 Pass reloc in fr_var. */
d258b828 8919 frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
29b0f896 8920}
3e73aa7c 8921
bd7ab16b
L
8922#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8923/* Return TRUE iff PLT32 relocation should be used for branching to
8924 symbol S. */
8925
5b7c81bd 8926static bool
bd7ab16b
L
8927need_plt32_p (symbolS *s)
8928{
8929 /* PLT32 relocation is ELF only. */
8930 if (!IS_ELF)
5b7c81bd 8931 return false;
bd7ab16b 8932
a5def729
RO
8933#ifdef TE_SOLARIS
8934 /* Don't emit PLT32 relocation on Solaris: neither native linker nor
8935 krtld support it. */
5b7c81bd 8936 return false;
a5def729
RO
8937#endif
8938
bd7ab16b
L
8939 /* Since there is no need to prepare for PLT branch on x86-64, we
8940 can generate R_X86_64_PLT32, instead of R_X86_64_PC32, which can
8941 be used as a marker for 32-bit PC-relative branches. */
8942 if (!object_64bit)
5b7c81bd 8943 return false;
bd7ab16b 8944
44365e88 8945 if (s == NULL)
5b7c81bd 8946 return false;
44365e88 8947
bd7ab16b
L
8948 /* Weak or undefined symbol need PLT32 relocation. */
8949 if (S_IS_WEAK (s) || !S_IS_DEFINED (s))
5b7c81bd 8950 return true;
bd7ab16b
L
8951
8952 /* Non-global symbol doesn't need PLT32 relocation. */
8953 if (! S_IS_EXTERNAL (s))
5b7c81bd 8954 return false;
bd7ab16b
L
8955
8956 /* Other global symbols need PLT32 relocation. NB: Symbol with
8957 non-default visibilities are treated as normal global symbol
8958 so that PLT32 relocation can be used as a marker for 32-bit
8959 PC-relative branches. It is useful for linker relaxation. */
5b7c81bd 8960 return true;
bd7ab16b
L
8961}
8962#endif
8963
29b0f896 8964static void
e3bb37b5 8965output_jump (void)
29b0f896
AM
8966{
8967 char *p;
8968 int size;
3e02c1cc 8969 fixS *fixP;
bd7ab16b 8970 bfd_reloc_code_real_type jump_reloc = i.reloc[0];
29b0f896 8971
0cfa3eb3 8972 if (i.tm.opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
8973 {
8974 /* This is a loop or jecxz type instruction. */
8975 size = 1;
8976 if (i.prefix[ADDR_PREFIX] != 0)
8977 {
48ef937e 8978 frag_opcode_byte (ADDR_PREFIX_OPCODE);
29b0f896
AM
8979 i.prefixes -= 1;
8980 }
8981 /* Pentium4 branch hints. */
8982 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8983 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
8984 {
48ef937e 8985 frag_opcode_byte (i.prefix[SEG_PREFIX]);
29b0f896 8986 i.prefixes--;
3e73aa7c
JH
8987 }
8988 }
29b0f896
AM
8989 else
8990 {
8991 int code16;
3e73aa7c 8992
29b0f896
AM
8993 code16 = 0;
8994 if (flag_code == CODE_16BIT)
8995 code16 = CODE16;
3e73aa7c 8996
29b0f896
AM
8997 if (i.prefix[DATA_PREFIX] != 0)
8998 {
48ef937e 8999 frag_opcode_byte (DATA_PREFIX_OPCODE);
29b0f896 9000 i.prefixes -= 1;
376cd056 9001 code16 ^= flip_code16(code16);
29b0f896 9002 }
252b5132 9003
29b0f896
AM
9004 size = 4;
9005 if (code16)
9006 size = 2;
9007 }
9fcc94b6 9008
6cb0a70e
JB
9009 /* BND prefixed jump. */
9010 if (i.prefix[BND_PREFIX] != 0)
29b0f896 9011 {
48ef937e 9012 frag_opcode_byte (i.prefix[BND_PREFIX]);
29b0f896
AM
9013 i.prefixes -= 1;
9014 }
252b5132 9015
6cb0a70e 9016 if (i.prefix[REX_PREFIX] != 0)
7e8b059b 9017 {
48ef937e 9018 frag_opcode_byte (i.prefix[REX_PREFIX]);
7e8b059b
L
9019 i.prefixes -= 1;
9020 }
9021
f2810fe0
JB
9022 if (i.prefixes != 0)
9023 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
e0890092 9024
48ef937e
JB
9025 if (now_seg == absolute_section)
9026 {
9a182d04 9027 abs_section_offset += i.opcode_length + size;
48ef937e
JB
9028 return;
9029 }
9030
9a182d04
JB
9031 p = frag_more (i.opcode_length + size);
9032 switch (i.opcode_length)
42164a71
L
9033 {
9034 case 2:
9035 *p++ = i.tm.base_opcode >> 8;
1a0670f3 9036 /* Fall through. */
42164a71
L
9037 case 1:
9038 *p++ = i.tm.base_opcode;
9039 break;
9040 default:
9041 abort ();
9042 }
e0890092 9043
bd7ab16b 9044#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1ef3994a
JB
9045 if (flag_code == CODE_64BIT && size == 4
9046 && jump_reloc == NO_RELOC && i.op[0].disps->X_add_number == 0
bd7ab16b
L
9047 && need_plt32_p (i.op[0].disps->X_add_symbol))
9048 jump_reloc = BFD_RELOC_X86_64_PLT32;
9049#endif
9050
9051 jump_reloc = reloc (size, 1, 1, jump_reloc);
9052
3e02c1cc 9053 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
bd7ab16b 9054 i.op[0].disps, 1, jump_reloc);
3e02c1cc 9055
eb19308f
JB
9056 /* All jumps handled here are signed, but don't unconditionally use a
9057 signed limit check for 32 and 16 bit jumps as we want to allow wrap
9058 around at 4G (outside of 64-bit mode) and 64k (except for XBEGIN)
9059 respectively. */
9060 switch (size)
9061 {
9062 case 1:
9063 fixP->fx_signed = 1;
9064 break;
9065
9066 case 2:
9067 if (i.tm.base_opcode == 0xc7f8)
9068 fixP->fx_signed = 1;
9069 break;
9070
9071 case 4:
9072 if (flag_code == CODE_64BIT)
9073 fixP->fx_signed = 1;
9074 break;
9075 }
29b0f896 9076}
e0890092 9077
29b0f896 9078static void
e3bb37b5 9079output_interseg_jump (void)
29b0f896
AM
9080{
9081 char *p;
9082 int size;
9083 int prefix;
9084 int code16;
252b5132 9085
29b0f896
AM
9086 code16 = 0;
9087 if (flag_code == CODE_16BIT)
9088 code16 = CODE16;
a217f122 9089
29b0f896
AM
9090 prefix = 0;
9091 if (i.prefix[DATA_PREFIX] != 0)
9092 {
9093 prefix = 1;
9094 i.prefixes -= 1;
9095 code16 ^= CODE16;
9096 }
6cb0a70e
JB
9097
9098 gas_assert (!i.prefix[REX_PREFIX]);
252b5132 9099
29b0f896
AM
9100 size = 4;
9101 if (code16)
9102 size = 2;
252b5132 9103
f2810fe0
JB
9104 if (i.prefixes != 0)
9105 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
252b5132 9106
48ef937e
JB
9107 if (now_seg == absolute_section)
9108 {
9109 abs_section_offset += prefix + 1 + 2 + size;
9110 return;
9111 }
9112
29b0f896
AM
9113 /* 1 opcode; 2 segment; offset */
9114 p = frag_more (prefix + 1 + 2 + size);
3e73aa7c 9115
29b0f896
AM
9116 if (i.prefix[DATA_PREFIX] != 0)
9117 *p++ = DATA_PREFIX_OPCODE;
252b5132 9118
29b0f896
AM
9119 if (i.prefix[REX_PREFIX] != 0)
9120 *p++ = i.prefix[REX_PREFIX];
252b5132 9121
29b0f896
AM
9122 *p++ = i.tm.base_opcode;
9123 if (i.op[1].imms->X_op == O_constant)
9124 {
9125 offsetT n = i.op[1].imms->X_add_number;
252b5132 9126
29b0f896
AM
9127 if (size == 2
9128 && !fits_in_unsigned_word (n)
9129 && !fits_in_signed_word (n))
9130 {
9131 as_bad (_("16-bit jump out of range"));
9132 return;
9133 }
9134 md_number_to_chars (p, n, size);
9135 }
9136 else
9137 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
d258b828 9138 i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
6d96a594
C
9139
9140 p += size;
9141 if (i.op[0].imms->X_op == O_constant)
9142 md_number_to_chars (p, (valueT) i.op[0].imms->X_add_number, 2);
9143 else
9144 fix_new_exp (frag_now, p - frag_now->fr_literal, 2,
9145 i.op[0].imms, 0, reloc (2, 0, 0, i.reloc[0]));
29b0f896 9146}
a217f122 9147
b4a3a7b4
L
9148#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9149void
9150x86_cleanup (void)
9151{
9152 char *p;
9153 asection *seg = now_seg;
9154 subsegT subseg = now_subseg;
9155 asection *sec;
9156 unsigned int alignment, align_size_1;
9157 unsigned int isa_1_descsz, feature_2_descsz, descsz;
9158 unsigned int isa_1_descsz_raw, feature_2_descsz_raw;
9159 unsigned int padding;
9160
1273b2f8 9161 if (!IS_ELF || !x86_used_note)
b4a3a7b4
L
9162 return;
9163
b4a3a7b4
L
9164 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X86;
9165
9166 /* The .note.gnu.property section layout:
9167
9168 Field Length Contents
9169 ---- ---- ----
9170 n_namsz 4 4
9171 n_descsz 4 The note descriptor size
9172 n_type 4 NT_GNU_PROPERTY_TYPE_0
9173 n_name 4 "GNU"
9174 n_desc n_descsz The program property array
9175 .... .... ....
9176 */
9177
9178 /* Create the .note.gnu.property section. */
9179 sec = subseg_new (NOTE_GNU_PROPERTY_SECTION_NAME, 0);
fd361982 9180 bfd_set_section_flags (sec,
b4a3a7b4
L
9181 (SEC_ALLOC
9182 | SEC_LOAD
9183 | SEC_DATA
9184 | SEC_HAS_CONTENTS
9185 | SEC_READONLY));
9186
9187 if (get_elf_backend_data (stdoutput)->s->elfclass == ELFCLASS64)
9188 {
9189 align_size_1 = 7;
9190 alignment = 3;
9191 }
9192 else
9193 {
9194 align_size_1 = 3;
9195 alignment = 2;
9196 }
9197
fd361982 9198 bfd_set_section_alignment (sec, alignment);
b4a3a7b4
L
9199 elf_section_type (sec) = SHT_NOTE;
9200
1273b2f8
L
9201 /* GNU_PROPERTY_X86_ISA_1_USED: 4-byte type + 4-byte data size
9202 + 4-byte data */
9203 isa_1_descsz_raw = 4 + 4 + 4;
9204 /* Align GNU_PROPERTY_X86_ISA_1_USED. */
9205 isa_1_descsz = (isa_1_descsz_raw + align_size_1) & ~align_size_1;
b4a3a7b4
L
9206
9207 feature_2_descsz_raw = isa_1_descsz;
9208 /* GNU_PROPERTY_X86_FEATURE_2_USED: 4-byte type + 4-byte data size
1273b2f8 9209 + 4-byte data */
b4a3a7b4
L
9210 feature_2_descsz_raw += 4 + 4 + 4;
9211 /* Align GNU_PROPERTY_X86_FEATURE_2_USED. */
9212 feature_2_descsz = ((feature_2_descsz_raw + align_size_1)
9213 & ~align_size_1);
9214
9215 descsz = feature_2_descsz;
9216 /* Section size: n_namsz + n_descsz + n_type + n_name + n_descsz. */
9217 p = frag_more (4 + 4 + 4 + 4 + descsz);
9218
9219 /* Write n_namsz. */
9220 md_number_to_chars (p, (valueT) 4, 4);
9221
9222 /* Write n_descsz. */
9223 md_number_to_chars (p + 4, (valueT) descsz, 4);
9224
9225 /* Write n_type. */
9226 md_number_to_chars (p + 4 * 2, (valueT) NT_GNU_PROPERTY_TYPE_0, 4);
9227
9228 /* Write n_name. */
9229 memcpy (p + 4 * 3, "GNU", 4);
9230
1273b2f8
L
9231 /* Write 4-byte type. */
9232 md_number_to_chars (p + 4 * 4,
9233 (valueT) GNU_PROPERTY_X86_ISA_1_USED, 4);
b4a3a7b4 9234
1273b2f8
L
9235 /* Write 4-byte data size. */
9236 md_number_to_chars (p + 4 * 5, (valueT) 4, 4);
b4a3a7b4 9237
1273b2f8
L
9238 /* Write 4-byte data. */
9239 md_number_to_chars (p + 4 * 6, (valueT) x86_isa_1_used, 4);
b4a3a7b4 9240
1273b2f8
L
9241 /* Zero out paddings. */
9242 padding = isa_1_descsz - isa_1_descsz_raw;
9243 if (padding)
9244 memset (p + 4 * 7, 0, padding);
b4a3a7b4
L
9245
9246 /* Write 4-byte type. */
9247 md_number_to_chars (p + isa_1_descsz + 4 * 4,
9248 (valueT) GNU_PROPERTY_X86_FEATURE_2_USED, 4);
9249
9250 /* Write 4-byte data size. */
9251 md_number_to_chars (p + isa_1_descsz + 4 * 5, (valueT) 4, 4);
9252
9253 /* Write 4-byte data. */
9254 md_number_to_chars (p + isa_1_descsz + 4 * 6,
9255 (valueT) x86_feature_2_used, 4);
9256
9257 /* Zero out paddings. */
9258 padding = feature_2_descsz - feature_2_descsz_raw;
9259 if (padding)
9260 memset (p + isa_1_descsz + 4 * 7, 0, padding);
9261
9262 /* We probably can't restore the current segment, for there likely
9263 isn't one yet... */
9264 if (seg && subseg)
9265 subseg_set (seg, subseg);
9266}
b52c4ee4
IB
9267
9268bool
9269x86_support_sframe_p (void)
9270{
9271 /* At this time, SFrame unwind is supported for AMD64 ABI only. */
9272 return (x86_elf_abi == X86_64_ABI);
9273}
9274
9275bool
9276x86_sframe_ra_tracking_p (void)
9277{
9278 /* In AMD64, return address is always stored on the stack at a fixed offset
9279 from the CFA (provided via x86_sframe_cfa_ra_offset ()).
9280 Do not track explicitly via an SFrame Frame Row Entry. */
9281 return false;
9282}
9283
9284offsetT
9285x86_sframe_cfa_ra_offset (void)
9286{
9287 gas_assert (x86_elf_abi == X86_64_ABI);
9288 return (offsetT) -8;
9289}
9290
9291unsigned char
9292x86_sframe_get_abi_arch (void)
9293{
9294 unsigned char sframe_abi_arch = 0;
9295
9296 if (x86_support_sframe_p ())
9297 {
9298 gas_assert (!target_big_endian);
9299 sframe_abi_arch = SFRAME_ABI_AMD64_ENDIAN_LITTLE;
9300 }
9301
9302 return sframe_abi_arch;
9303}
9304
b4a3a7b4
L
9305#endif
9306
9c33702b
JB
9307static unsigned int
9308encoding_length (const fragS *start_frag, offsetT start_off,
9309 const char *frag_now_ptr)
9310{
9311 unsigned int len = 0;
9312
9313 if (start_frag != frag_now)
9314 {
9315 const fragS *fr = start_frag;
9316
9317 do {
9318 len += fr->fr_fix;
9319 fr = fr->fr_next;
9320 } while (fr && fr != frag_now);
9321 }
9322
9323 return len - start_off + (frag_now_ptr - frag_now->fr_literal);
9324}
9325
e379e5f3 9326/* Return 1 for test, and, cmp, add, sub, inc and dec which may
79d72f45
HL
9327 be macro-fused with conditional jumps.
9328 NB: If TEST/AND/CMP/ADD/SUB/INC/DEC is of RIP relative address,
9329 or is one of the following format:
9330
9331 cmp m, imm
9332 add m, imm
9333 sub m, imm
9334 test m, imm
9335 and m, imm
9336 inc m
9337 dec m
9338
9339 it is unfusible. */
e379e5f3
L
9340
9341static int
79d72f45 9342maybe_fused_with_jcc_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
9343{
9344 /* No RIP address. */
9345 if (i.base_reg && i.base_reg->reg_num == RegIP)
9346 return 0;
9347
389d00a5
JB
9348 /* No opcodes outside of base encoding space. */
9349 if (i.tm.opcode_modifier.opcodespace != SPACE_BASE)
e379e5f3
L
9350 return 0;
9351
79d72f45
HL
9352 /* add, sub without add/sub m, imm. */
9353 if (i.tm.base_opcode <= 5
e379e5f3
L
9354 || (i.tm.base_opcode >= 0x28 && i.tm.base_opcode <= 0x2d)
9355 || ((i.tm.base_opcode | 3) == 0x83
79d72f45 9356 && (i.tm.extension_opcode == 0x5
e379e5f3 9357 || i.tm.extension_opcode == 0x0)))
79d72f45
HL
9358 {
9359 *mf_cmp_p = mf_cmp_alu_cmp;
9360 return !(i.mem_operands && i.imm_operands);
9361 }
e379e5f3 9362
79d72f45
HL
9363 /* and without and m, imm. */
9364 if ((i.tm.base_opcode >= 0x20 && i.tm.base_opcode <= 0x25)
9365 || ((i.tm.base_opcode | 3) == 0x83
9366 && i.tm.extension_opcode == 0x4))
9367 {
9368 *mf_cmp_p = mf_cmp_test_and;
9369 return !(i.mem_operands && i.imm_operands);
9370 }
9371
9372 /* test without test m imm. */
e379e5f3
L
9373 if ((i.tm.base_opcode | 1) == 0x85
9374 || (i.tm.base_opcode | 1) == 0xa9
9375 || ((i.tm.base_opcode | 1) == 0xf7
79d72f45
HL
9376 && i.tm.extension_opcode == 0))
9377 {
9378 *mf_cmp_p = mf_cmp_test_and;
9379 return !(i.mem_operands && i.imm_operands);
9380 }
9381
9382 /* cmp without cmp m, imm. */
9383 if ((i.tm.base_opcode >= 0x38 && i.tm.base_opcode <= 0x3d)
e379e5f3
L
9384 || ((i.tm.base_opcode | 3) == 0x83
9385 && (i.tm.extension_opcode == 0x7)))
79d72f45
HL
9386 {
9387 *mf_cmp_p = mf_cmp_alu_cmp;
9388 return !(i.mem_operands && i.imm_operands);
9389 }
e379e5f3 9390
79d72f45 9391 /* inc, dec without inc/dec m. */
e379e5f3
L
9392 if ((i.tm.cpu_flags.bitfield.cpuno64
9393 && (i.tm.base_opcode | 0xf) == 0x4f)
9394 || ((i.tm.base_opcode | 1) == 0xff
9395 && i.tm.extension_opcode <= 0x1))
79d72f45
HL
9396 {
9397 *mf_cmp_p = mf_cmp_incdec;
9398 return !i.mem_operands;
9399 }
e379e5f3
L
9400
9401 return 0;
9402}
9403
9404/* Return 1 if a FUSED_JCC_PADDING frag should be generated. */
9405
9406static int
79d72f45 9407add_fused_jcc_padding_frag_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
9408{
9409 /* NB: Don't work with COND_JUMP86 without i386. */
9410 if (!align_branch_power
9411 || now_seg == absolute_section
9412 || !cpu_arch_flags.bitfield.cpui386
9413 || !(align_branch & align_branch_fused_bit))
9414 return 0;
9415
79d72f45 9416 if (maybe_fused_with_jcc_p (mf_cmp_p))
e379e5f3
L
9417 {
9418 if (last_insn.kind == last_insn_other
9419 || last_insn.seg != now_seg)
9420 return 1;
9421 if (flag_debug)
9422 as_warn_where (last_insn.file, last_insn.line,
9423 _("`%s` skips -malign-branch-boundary on `%s`"),
9424 last_insn.name, i.tm.name);
9425 }
9426
9427 return 0;
9428}
9429
9430/* Return 1 if a BRANCH_PREFIX frag should be generated. */
9431
9432static int
9433add_branch_prefix_frag_p (void)
9434{
9435 /* NB: Don't work with COND_JUMP86 without i386. Don't add prefix
9436 to PadLock instructions since they include prefixes in opcode. */
9437 if (!align_branch_power
9438 || !align_branch_prefix_size
9439 || now_seg == absolute_section
9440 || i.tm.cpu_flags.bitfield.cpupadlock
9441 || !cpu_arch_flags.bitfield.cpui386)
9442 return 0;
9443
9444 /* Don't add prefix if it is a prefix or there is no operand in case
9445 that segment prefix is special. */
9446 if (!i.operands || i.tm.opcode_modifier.isprefix)
9447 return 0;
9448
9449 if (last_insn.kind == last_insn_other
9450 || last_insn.seg != now_seg)
9451 return 1;
9452
9453 if (flag_debug)
9454 as_warn_where (last_insn.file, last_insn.line,
9455 _("`%s` skips -malign-branch-boundary on `%s`"),
9456 last_insn.name, i.tm.name);
9457
9458 return 0;
9459}
9460
9461/* Return 1 if a BRANCH_PADDING frag should be generated. */
9462
9463static int
79d72f45
HL
9464add_branch_padding_frag_p (enum align_branch_kind *branch_p,
9465 enum mf_jcc_kind *mf_jcc_p)
e379e5f3
L
9466{
9467 int add_padding;
9468
9469 /* NB: Don't work with COND_JUMP86 without i386. */
9470 if (!align_branch_power
9471 || now_seg == absolute_section
389d00a5
JB
9472 || !cpu_arch_flags.bitfield.cpui386
9473 || i.tm.opcode_modifier.opcodespace != SPACE_BASE)
e379e5f3
L
9474 return 0;
9475
9476 add_padding = 0;
9477
9478 /* Check for jcc and direct jmp. */
9479 if (i.tm.opcode_modifier.jump == JUMP)
9480 {
9481 if (i.tm.base_opcode == JUMP_PC_RELATIVE)
9482 {
9483 *branch_p = align_branch_jmp;
9484 add_padding = align_branch & align_branch_jmp_bit;
9485 }
9486 else
9487 {
79d72f45
HL
9488 /* Because J<cc> and JN<cc> share same group in macro-fusible table,
9489 igore the lowest bit. */
9490 *mf_jcc_p = (i.tm.base_opcode & 0x0e) >> 1;
e379e5f3
L
9491 *branch_p = align_branch_jcc;
9492 if ((align_branch & align_branch_jcc_bit))
9493 add_padding = 1;
9494 }
9495 }
e379e5f3
L
9496 else if ((i.tm.base_opcode | 1) == 0xc3)
9497 {
9498 /* Near ret. */
9499 *branch_p = align_branch_ret;
9500 if ((align_branch & align_branch_ret_bit))
9501 add_padding = 1;
9502 }
9503 else
9504 {
9505 /* Check for indirect jmp, direct and indirect calls. */
9506 if (i.tm.base_opcode == 0xe8)
9507 {
9508 /* Direct call. */
9509 *branch_p = align_branch_call;
9510 if ((align_branch & align_branch_call_bit))
9511 add_padding = 1;
9512 }
9513 else if (i.tm.base_opcode == 0xff
9514 && (i.tm.extension_opcode == 2
9515 || i.tm.extension_opcode == 4))
9516 {
9517 /* Indirect call and jmp. */
9518 *branch_p = align_branch_indirect;
9519 if ((align_branch & align_branch_indirect_bit))
9520 add_padding = 1;
9521 }
9522
9523 if (add_padding
9524 && i.disp_operands
9525 && tls_get_addr
9526 && (i.op[0].disps->X_op == O_symbol
9527 || (i.op[0].disps->X_op == O_subtract
9528 && i.op[0].disps->X_op_symbol == GOT_symbol)))
9529 {
9530 symbolS *s = i.op[0].disps->X_add_symbol;
9531 /* No padding to call to global or undefined tls_get_addr. */
9532 if ((S_IS_EXTERNAL (s) || !S_IS_DEFINED (s))
9533 && strcmp (S_GET_NAME (s), tls_get_addr) == 0)
9534 return 0;
9535 }
9536 }
9537
9538 if (add_padding
9539 && last_insn.kind != last_insn_other
9540 && last_insn.seg == now_seg)
9541 {
9542 if (flag_debug)
9543 as_warn_where (last_insn.file, last_insn.line,
9544 _("`%s` skips -malign-branch-boundary on `%s`"),
9545 last_insn.name, i.tm.name);
9546 return 0;
9547 }
9548
9549 return add_padding;
9550}
9551
29b0f896 9552static void
e3bb37b5 9553output_insn (void)
29b0f896 9554{
2bbd9c25
JJ
9555 fragS *insn_start_frag;
9556 offsetT insn_start_off;
e379e5f3
L
9557 fragS *fragP = NULL;
9558 enum align_branch_kind branch = align_branch_none;
79d72f45
HL
9559 /* The initializer is arbitrary just to avoid uninitialized error.
9560 it's actually either assigned in add_branch_padding_frag_p
9561 or never be used. */
9562 enum mf_jcc_kind mf_jcc = mf_jcc_jo;
2bbd9c25 9563
b4a3a7b4 9564#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
48ef937e 9565 if (IS_ELF && x86_used_note && now_seg != absolute_section)
b4a3a7b4 9566 {
32930e4e
L
9567 if ((i.xstate & xstate_tmm) == xstate_tmm
9568 || i.tm.cpu_flags.bitfield.cpuamx_tile)
9569 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_TMM;
9570
b4a3a7b4
L
9571 if (i.tm.cpu_flags.bitfield.cpu8087
9572 || i.tm.cpu_flags.bitfield.cpu287
9573 || i.tm.cpu_flags.bitfield.cpu387
9574 || i.tm.cpu_flags.bitfield.cpu687
9575 || i.tm.cpu_flags.bitfield.cpufisttp)
9576 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X87;
014d61ea 9577
921eafea 9578 if ((i.xstate & xstate_mmx)
389d00a5
JB
9579 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
9580 && !is_any_vex_encoding (&i.tm)
9581 && (i.tm.base_opcode == 0x77 /* emms */
9582 || i.tm.base_opcode == 0x0e /* femms */)))
b4a3a7b4 9583 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MMX;
014d61ea 9584
32930e4e
L
9585 if (i.index_reg)
9586 {
9587 if (i.index_reg->reg_type.bitfield.zmmword)
9588 i.xstate |= xstate_zmm;
9589 else if (i.index_reg->reg_type.bitfield.ymmword)
9590 i.xstate |= xstate_ymm;
9591 else if (i.index_reg->reg_type.bitfield.xmmword)
9592 i.xstate |= xstate_xmm;
9593 }
014d61ea
JB
9594
9595 /* vzeroall / vzeroupper */
9596 if (i.tm.base_opcode == 0x77 && i.tm.cpu_flags.bitfield.cpuavx)
9597 i.xstate |= xstate_ymm;
9598
c4694f17 9599 if ((i.xstate & xstate_xmm)
389d00a5
JB
9600 /* ldmxcsr / stmxcsr / vldmxcsr / vstmxcsr */
9601 || (i.tm.base_opcode == 0xae
9602 && (i.tm.cpu_flags.bitfield.cpusse
9603 || i.tm.cpu_flags.bitfield.cpuavx))
c4694f17
TG
9604 || i.tm.cpu_flags.bitfield.cpuwidekl
9605 || i.tm.cpu_flags.bitfield.cpukl)
b4a3a7b4 9606 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XMM;
014d61ea 9607
921eafea 9608 if ((i.xstate & xstate_ymm) == xstate_ymm)
b4a3a7b4 9609 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_YMM;
921eafea 9610 if ((i.xstate & xstate_zmm) == xstate_zmm)
b4a3a7b4 9611 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_ZMM;
6225c532 9612 if (i.mask.reg || (i.xstate & xstate_mask) == xstate_mask)
32930e4e 9613 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MASK;
b4a3a7b4
L
9614 if (i.tm.cpu_flags.bitfield.cpufxsr)
9615 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_FXSR;
9616 if (i.tm.cpu_flags.bitfield.cpuxsave)
9617 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVE;
9618 if (i.tm.cpu_flags.bitfield.cpuxsaveopt)
9619 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT;
9620 if (i.tm.cpu_flags.bitfield.cpuxsavec)
9621 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEC;
b0ab0693
L
9622
9623 if (x86_feature_2_used
9624 || i.tm.cpu_flags.bitfield.cpucmov
9625 || i.tm.cpu_flags.bitfield.cpusyscall
389d00a5
JB
9626 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
9627 && i.tm.base_opcode == 0xc7
70e95837 9628 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
b0ab0693
L
9629 && i.tm.extension_opcode == 1) /* cmpxchg8b */)
9630 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_BASELINE;
9631 if (i.tm.cpu_flags.bitfield.cpusse3
9632 || i.tm.cpu_flags.bitfield.cpussse3
9633 || i.tm.cpu_flags.bitfield.cpusse4_1
9634 || i.tm.cpu_flags.bitfield.cpusse4_2
9635 || i.tm.cpu_flags.bitfield.cpucx16
9636 || i.tm.cpu_flags.bitfield.cpupopcnt
9637 /* LAHF-SAHF insns in 64-bit mode. */
9638 || (flag_code == CODE_64BIT
35648716
JB
9639 && (i.tm.base_opcode | 1) == 0x9f
9640 && i.tm.opcode_modifier.opcodespace == SPACE_BASE))
b0ab0693
L
9641 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V2;
9642 if (i.tm.cpu_flags.bitfield.cpuavx
9643 || i.tm.cpu_flags.bitfield.cpuavx2
a9860005
JB
9644 /* Any VEX encoded insns execpt for AVX512F, AVX512BW, AVX512DQ,
9645 XOP, FMA4, LPW, TBM, and AMX. */
b0ab0693
L
9646 || (i.tm.opcode_modifier.vex
9647 && !i.tm.cpu_flags.bitfield.cpuavx512f
9648 && !i.tm.cpu_flags.bitfield.cpuavx512bw
9649 && !i.tm.cpu_flags.bitfield.cpuavx512dq
a9860005
JB
9650 && !i.tm.cpu_flags.bitfield.cpuxop
9651 && !i.tm.cpu_flags.bitfield.cpufma4
b0ab0693
L
9652 && !i.tm.cpu_flags.bitfield.cpulwp
9653 && !i.tm.cpu_flags.bitfield.cputbm
9654 && !(x86_feature_2_used & GNU_PROPERTY_X86_FEATURE_2_TMM))
9655 || i.tm.cpu_flags.bitfield.cpuf16c
9656 || i.tm.cpu_flags.bitfield.cpufma
9657 || i.tm.cpu_flags.bitfield.cpulzcnt
9658 || i.tm.cpu_flags.bitfield.cpumovbe
9659 || i.tm.cpu_flags.bitfield.cpuxsaves
9660 || (x86_feature_2_used
9661 & (GNU_PROPERTY_X86_FEATURE_2_XSAVE
9662 | GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT
9663 | GNU_PROPERTY_X86_FEATURE_2_XSAVEC)) != 0)
9664 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V3;
9665 if (i.tm.cpu_flags.bitfield.cpuavx512f
9666 || i.tm.cpu_flags.bitfield.cpuavx512bw
9667 || i.tm.cpu_flags.bitfield.cpuavx512dq
9668 || i.tm.cpu_flags.bitfield.cpuavx512vl
a9860005
JB
9669 /* Any EVEX encoded insns except for AVX512ER, AVX512PF,
9670 AVX512-4FMAPS, and AVX512-4VNNIW. */
b0ab0693
L
9671 || (i.tm.opcode_modifier.evex
9672 && !i.tm.cpu_flags.bitfield.cpuavx512er
9673 && !i.tm.cpu_flags.bitfield.cpuavx512pf
a9860005 9674 && !i.tm.cpu_flags.bitfield.cpuavx512_4fmaps
b0ab0693
L
9675 && !i.tm.cpu_flags.bitfield.cpuavx512_4vnniw))
9676 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V4;
b4a3a7b4
L
9677 }
9678#endif
9679
29b0f896
AM
9680 /* Tie dwarf2 debug info to the address at the start of the insn.
9681 We can't do this after the insn has been output as the current
9682 frag may have been closed off. eg. by frag_var. */
9683 dwarf2_emit_insn (0);
9684
2bbd9c25
JJ
9685 insn_start_frag = frag_now;
9686 insn_start_off = frag_now_fix ();
9687
79d72f45 9688 if (add_branch_padding_frag_p (&branch, &mf_jcc))
e379e5f3
L
9689 {
9690 char *p;
9691 /* Branch can be 8 bytes. Leave some room for prefixes. */
9692 unsigned int max_branch_padding_size = 14;
9693
9694 /* Align section to boundary. */
9695 record_alignment (now_seg, align_branch_power);
9696
9697 /* Make room for padding. */
9698 frag_grow (max_branch_padding_size);
9699
9700 /* Start of the padding. */
9701 p = frag_more (0);
9702
9703 fragP = frag_now;
9704
9705 frag_var (rs_machine_dependent, max_branch_padding_size, 0,
9706 ENCODE_RELAX_STATE (BRANCH_PADDING, 0),
9707 NULL, 0, p);
9708
79d72f45 9709 fragP->tc_frag_data.mf_type = mf_jcc;
e379e5f3
L
9710 fragP->tc_frag_data.branch_type = branch;
9711 fragP->tc_frag_data.max_bytes = max_branch_padding_size;
9712 }
9713
d59a54c2
JB
9714 if (!cpu_arch_flags.bitfield.cpui386 && (flag_code != CODE_16BIT)
9715 && !pre_386_16bit_warned)
9716 {
9717 as_warn (_("use .code16 to ensure correct addressing mode"));
9718 pre_386_16bit_warned = true;
9719 }
9720
29b0f896 9721 /* Output jumps. */
0cfa3eb3 9722 if (i.tm.opcode_modifier.jump == JUMP)
29b0f896 9723 output_branch ();
0cfa3eb3
JB
9724 else if (i.tm.opcode_modifier.jump == JUMP_BYTE
9725 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896 9726 output_jump ();
0cfa3eb3 9727 else if (i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT)
29b0f896
AM
9728 output_interseg_jump ();
9729 else
9730 {
9731 /* Output normal instructions here. */
9732 char *p;
9733 unsigned char *q;
47465058 9734 unsigned int j;
79d72f45 9735 enum mf_cmp_kind mf_cmp;
4dffcebc 9736
e4e00185 9737 if (avoid_fence
389d00a5
JB
9738 && (i.tm.base_opcode == 0xaee8
9739 || i.tm.base_opcode == 0xaef0
9740 || i.tm.base_opcode == 0xaef8))
48ef937e
JB
9741 {
9742 /* Encode lfence, mfence, and sfence as
9743 f0 83 04 24 00 lock addl $0x0, (%{re}sp). */
47f4115a
JB
9744 if (flag_code == CODE_16BIT)
9745 as_bad (_("Cannot convert `%s' in 16-bit mode"), i.tm.name);
9746 else if (omit_lock_prefix)
9747 as_bad (_("Cannot convert `%s' with `-momit-lock-prefix=yes' in effect"),
9748 i.tm.name);
9749 else if (now_seg != absolute_section)
48ef937e
JB
9750 {
9751 offsetT val = 0x240483f0ULL;
9752
9753 p = frag_more (5);
9754 md_number_to_chars (p, val, 5);
9755 }
9756 else
9757 abs_section_offset += 5;
9758 return;
9759 }
e4e00185 9760
d022bddd
IT
9761 /* Some processors fail on LOCK prefix. This options makes
9762 assembler ignore LOCK prefix and serves as a workaround. */
9763 if (omit_lock_prefix)
9764 {
35648716
JB
9765 if (i.tm.base_opcode == LOCK_PREFIX_OPCODE
9766 && i.tm.opcode_modifier.isprefix)
d022bddd
IT
9767 return;
9768 i.prefix[LOCK_PREFIX] = 0;
9769 }
9770
e379e5f3
L
9771 if (branch)
9772 /* Skip if this is a branch. */
9773 ;
79d72f45 9774 else if (add_fused_jcc_padding_frag_p (&mf_cmp))
e379e5f3
L
9775 {
9776 /* Make room for padding. */
9777 frag_grow (MAX_FUSED_JCC_PADDING_SIZE);
9778 p = frag_more (0);
9779
9780 fragP = frag_now;
9781
9782 frag_var (rs_machine_dependent, MAX_FUSED_JCC_PADDING_SIZE, 0,
9783 ENCODE_RELAX_STATE (FUSED_JCC_PADDING, 0),
9784 NULL, 0, p);
9785
79d72f45 9786 fragP->tc_frag_data.mf_type = mf_cmp;
e379e5f3
L
9787 fragP->tc_frag_data.branch_type = align_branch_fused;
9788 fragP->tc_frag_data.max_bytes = MAX_FUSED_JCC_PADDING_SIZE;
9789 }
9790 else if (add_branch_prefix_frag_p ())
9791 {
9792 unsigned int max_prefix_size = align_branch_prefix_size;
9793
9794 /* Make room for padding. */
9795 frag_grow (max_prefix_size);
9796 p = frag_more (0);
9797
9798 fragP = frag_now;
9799
9800 frag_var (rs_machine_dependent, max_prefix_size, 0,
9801 ENCODE_RELAX_STATE (BRANCH_PREFIX, 0),
9802 NULL, 0, p);
9803
9804 fragP->tc_frag_data.max_bytes = max_prefix_size;
9805 }
9806
43234a1e
L
9807 /* Since the VEX/EVEX prefix contains the implicit prefix, we
9808 don't need the explicit prefix. */
cf665fee 9809 if (!is_any_vex_encoding (&i.tm))
bc4bd9ab 9810 {
7b47a312 9811 switch (i.tm.opcode_modifier.opcodeprefix)
bc4bd9ab 9812 {
7b47a312
L
9813 case PREFIX_0X66:
9814 add_prefix (0x66);
9815 break;
9816 case PREFIX_0XF2:
9817 add_prefix (0xf2);
9818 break;
9819 case PREFIX_0XF3:
8b65b895
L
9820 if (!i.tm.cpu_flags.bitfield.cpupadlock
9821 || (i.prefix[REP_PREFIX] != 0xf3))
9822 add_prefix (0xf3);
c0f3af97 9823 break;
7b47a312 9824 case PREFIX_NONE:
9a182d04 9825 switch (i.opcode_length)
c0f3af97 9826 {
7b47a312 9827 case 2:
7b47a312 9828 break;
9a182d04 9829 case 1:
7b47a312 9830 /* Check for pseudo prefixes. */
9a182d04
JB
9831 if (!i.tm.opcode_modifier.isprefix || i.tm.base_opcode)
9832 break;
7b47a312
L
9833 as_bad_where (insn_start_frag->fr_file,
9834 insn_start_frag->fr_line,
9835 _("pseudo prefix without instruction"));
9836 return;
9837 default:
9838 abort ();
4dffcebc 9839 }
c0f3af97 9840 break;
c0f3af97
L
9841 default:
9842 abort ();
bc4bd9ab 9843 }
c0f3af97 9844
6d19a37a 9845#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
cf61b747
L
9846 /* For x32, add a dummy REX_OPCODE prefix for mov/add with
9847 R_X86_64_GOTTPOFF relocation so that linker can safely
14470f07
L
9848 perform IE->LE optimization. A dummy REX_OPCODE prefix
9849 is also needed for lea with R_X86_64_GOTPC32_TLSDESC
9850 relocation for GDesc -> IE/LE optimization. */
cf61b747
L
9851 if (x86_elf_abi == X86_64_X32_ABI
9852 && i.operands == 2
14470f07
L
9853 && (i.reloc[0] == BFD_RELOC_X86_64_GOTTPOFF
9854 || i.reloc[0] == BFD_RELOC_X86_64_GOTPC32_TLSDESC)
cf61b747
L
9855 && i.prefix[REX_PREFIX] == 0)
9856 add_prefix (REX_OPCODE);
6d19a37a 9857#endif
cf61b747 9858
c0f3af97
L
9859 /* The prefix bytes. */
9860 for (j = ARRAY_SIZE (i.prefix), q = i.prefix; j > 0; j--, q++)
9861 if (*q)
48ef937e 9862 frag_opcode_byte (*q);
0f10071e 9863 }
ae5c1c7b 9864 else
c0f3af97
L
9865 {
9866 for (j = 0, q = i.prefix; j < ARRAY_SIZE (i.prefix); j++, q++)
9867 if (*q)
9868 switch (j)
9869 {
c0f3af97
L
9870 case SEG_PREFIX:
9871 case ADDR_PREFIX:
48ef937e 9872 frag_opcode_byte (*q);
c0f3af97
L
9873 break;
9874 default:
9875 /* There should be no other prefixes for instructions
9876 with VEX prefix. */
9877 abort ();
9878 }
9879
43234a1e
L
9880 /* For EVEX instructions i.vrex should become 0 after
9881 build_evex_prefix. For VEX instructions upper 16 registers
9882 aren't available, so VREX should be 0. */
9883 if (i.vrex)
9884 abort ();
c0f3af97 9885 /* Now the VEX prefix. */
48ef937e
JB
9886 if (now_seg != absolute_section)
9887 {
9888 p = frag_more (i.vex.length);
9889 for (j = 0; j < i.vex.length; j++)
9890 p[j] = i.vex.bytes[j];
9891 }
9892 else
9893 abs_section_offset += i.vex.length;
c0f3af97 9894 }
252b5132 9895
29b0f896 9896 /* Now the opcode; be careful about word order here! */
389d00a5
JB
9897 j = i.opcode_length;
9898 if (!i.vex.length)
9899 switch (i.tm.opcode_modifier.opcodespace)
9900 {
9901 case SPACE_BASE:
9902 break;
9903 case SPACE_0F:
9904 ++j;
9905 break;
9906 case SPACE_0F38:
9907 case SPACE_0F3A:
9908 j += 2;
9909 break;
9910 default:
9911 abort ();
9912 }
9913
48ef937e 9914 if (now_seg == absolute_section)
389d00a5
JB
9915 abs_section_offset += j;
9916 else if (j == 1)
29b0f896
AM
9917 {
9918 FRAG_APPEND_1_CHAR (i.tm.base_opcode);
9919 }
9920 else
9921 {
389d00a5
JB
9922 p = frag_more (j);
9923 if (!i.vex.length
9924 && i.tm.opcode_modifier.opcodespace != SPACE_BASE)
9925 {
9926 *p++ = 0x0f;
9927 if (i.tm.opcode_modifier.opcodespace != SPACE_0F)
9928 *p++ = i.tm.opcode_modifier.opcodespace == SPACE_0F38
9929 ? 0x38 : 0x3a;
9930 }
9931
9a182d04 9932 switch (i.opcode_length)
331d2d0d 9933 {
4dffcebc 9934 case 2:
389d00a5
JB
9935 /* Put out high byte first: can't use md_number_to_chars! */
9936 *p++ = (i.tm.base_opcode >> 8) & 0xff;
9937 /* Fall through. */
9938 case 1:
9939 *p = i.tm.base_opcode & 0xff;
4dffcebc
L
9940 break;
9941 default:
9942 abort ();
9943 break;
331d2d0d 9944 }
0f10071e 9945
29b0f896 9946 }
3e73aa7c 9947
29b0f896 9948 /* Now the modrm byte and sib byte (if present). */
40fb9820 9949 if (i.tm.opcode_modifier.modrm)
29b0f896 9950 {
48ef937e
JB
9951 frag_opcode_byte ((i.rm.regmem << 0)
9952 | (i.rm.reg << 3)
9953 | (i.rm.mode << 6));
29b0f896
AM
9954 /* If i.rm.regmem == ESP (4)
9955 && i.rm.mode != (Register mode)
9956 && not 16 bit
9957 ==> need second modrm byte. */
9958 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
9959 && i.rm.mode != 3
dc821c5f 9960 && !(i.base_reg && i.base_reg->reg_type.bitfield.word))
48ef937e
JB
9961 frag_opcode_byte ((i.sib.base << 0)
9962 | (i.sib.index << 3)
9963 | (i.sib.scale << 6));
29b0f896 9964 }
3e73aa7c 9965
29b0f896 9966 if (i.disp_operands)
2bbd9c25 9967 output_disp (insn_start_frag, insn_start_off);
3e73aa7c 9968
29b0f896 9969 if (i.imm_operands)
2bbd9c25 9970 output_imm (insn_start_frag, insn_start_off);
9c33702b
JB
9971
9972 /*
9973 * frag_now_fix () returning plain abs_section_offset when we're in the
9974 * absolute section, and abs_section_offset not getting updated as data
9975 * gets added to the frag breaks the logic below.
9976 */
9977 if (now_seg != absolute_section)
9978 {
9979 j = encoding_length (insn_start_frag, insn_start_off, frag_more (0));
9980 if (j > 15)
9981 as_warn (_("instruction length of %u bytes exceeds the limit of 15"),
9982 j);
e379e5f3
L
9983 else if (fragP)
9984 {
9985 /* NB: Don't add prefix with GOTPC relocation since
9986 output_disp() above depends on the fixed encoding
9987 length. Can't add prefix with TLS relocation since
9988 it breaks TLS linker optimization. */
9989 unsigned int max = i.has_gotpc_tls_reloc ? 0 : 15 - j;
9990 /* Prefix count on the current instruction. */
9991 unsigned int count = i.vex.length;
9992 unsigned int k;
9993 for (k = 0; k < ARRAY_SIZE (i.prefix); k++)
9994 /* REX byte is encoded in VEX/EVEX prefix. */
9995 if (i.prefix[k] && (k != REX_PREFIX || !i.vex.length))
9996 count++;
9997
9998 /* Count prefixes for extended opcode maps. */
9999 if (!i.vex.length)
389d00a5 10000 switch (i.tm.opcode_modifier.opcodespace)
e379e5f3 10001 {
389d00a5 10002 case SPACE_BASE:
e379e5f3 10003 break;
389d00a5
JB
10004 case SPACE_0F:
10005 count++;
e379e5f3 10006 break;
389d00a5
JB
10007 case SPACE_0F38:
10008 case SPACE_0F3A:
10009 count += 2;
e379e5f3
L
10010 break;
10011 default:
10012 abort ();
10013 }
10014
10015 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
10016 == BRANCH_PREFIX)
10017 {
10018 /* Set the maximum prefix size in BRANCH_PREFIX
10019 frag. */
10020 if (fragP->tc_frag_data.max_bytes > max)
10021 fragP->tc_frag_data.max_bytes = max;
10022 if (fragP->tc_frag_data.max_bytes > count)
10023 fragP->tc_frag_data.max_bytes -= count;
10024 else
10025 fragP->tc_frag_data.max_bytes = 0;
10026 }
10027 else
10028 {
10029 /* Remember the maximum prefix size in FUSED_JCC_PADDING
10030 frag. */
10031 unsigned int max_prefix_size;
10032 if (align_branch_prefix_size > max)
10033 max_prefix_size = max;
10034 else
10035 max_prefix_size = align_branch_prefix_size;
10036 if (max_prefix_size > count)
10037 fragP->tc_frag_data.max_prefix_length
10038 = max_prefix_size - count;
10039 }
10040
10041 /* Use existing segment prefix if possible. Use CS
10042 segment prefix in 64-bit mode. In 32-bit mode, use SS
10043 segment prefix with ESP/EBP base register and use DS
10044 segment prefix without ESP/EBP base register. */
10045 if (i.prefix[SEG_PREFIX])
10046 fragP->tc_frag_data.default_prefix = i.prefix[SEG_PREFIX];
10047 else if (flag_code == CODE_64BIT)
10048 fragP->tc_frag_data.default_prefix = CS_PREFIX_OPCODE;
10049 else if (i.base_reg
10050 && (i.base_reg->reg_num == 4
10051 || i.base_reg->reg_num == 5))
10052 fragP->tc_frag_data.default_prefix = SS_PREFIX_OPCODE;
10053 else
10054 fragP->tc_frag_data.default_prefix = DS_PREFIX_OPCODE;
10055 }
9c33702b 10056 }
29b0f896 10057 }
252b5132 10058
e379e5f3
L
10059 /* NB: Don't work with COND_JUMP86 without i386. */
10060 if (align_branch_power
10061 && now_seg != absolute_section
10062 && cpu_arch_flags.bitfield.cpui386)
10063 {
10064 /* Terminate each frag so that we can add prefix and check for
10065 fused jcc. */
10066 frag_wane (frag_now);
10067 frag_new (0);
10068 }
10069
29b0f896
AM
10070#ifdef DEBUG386
10071 if (flag_debug)
10072 {
7b81dfbb 10073 pi ("" /*line*/, &i);
29b0f896
AM
10074 }
10075#endif /* DEBUG386 */
10076}
252b5132 10077
e205caa7
L
10078/* Return the size of the displacement operand N. */
10079
10080static int
10081disp_size (unsigned int n)
10082{
10083 int size = 4;
43234a1e 10084
b5014f7a 10085 if (i.types[n].bitfield.disp64)
40fb9820
L
10086 size = 8;
10087 else if (i.types[n].bitfield.disp8)
10088 size = 1;
10089 else if (i.types[n].bitfield.disp16)
10090 size = 2;
e205caa7
L
10091 return size;
10092}
10093
10094/* Return the size of the immediate operand N. */
10095
10096static int
10097imm_size (unsigned int n)
10098{
10099 int size = 4;
40fb9820
L
10100 if (i.types[n].bitfield.imm64)
10101 size = 8;
10102 else if (i.types[n].bitfield.imm8 || i.types[n].bitfield.imm8s)
10103 size = 1;
10104 else if (i.types[n].bitfield.imm16)
10105 size = 2;
e205caa7
L
10106 return size;
10107}
10108
29b0f896 10109static void
64e74474 10110output_disp (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
10111{
10112 char *p;
10113 unsigned int n;
252b5132 10114
29b0f896
AM
10115 for (n = 0; n < i.operands; n++)
10116 {
b5014f7a 10117 if (operand_type_check (i.types[n], disp))
29b0f896 10118 {
48ef937e
JB
10119 int size = disp_size (n);
10120
10121 if (now_seg == absolute_section)
10122 abs_section_offset += size;
10123 else if (i.op[n].disps->X_op == O_constant)
29b0f896 10124 {
43234a1e 10125 offsetT val = i.op[n].disps->X_add_number;
252b5132 10126
629cfaf1
JB
10127 val = offset_in_range (val >> (size == 1 ? i.memshift : 0),
10128 size);
29b0f896
AM
10129 p = frag_more (size);
10130 md_number_to_chars (p, val, size);
10131 }
10132 else
10133 {
f86103b7 10134 enum bfd_reloc_code_real reloc_type;
a775efc8
JB
10135 bool pcrel = (i.flags[n] & Operand_PCrel) != 0;
10136 bool sign = (flag_code == CODE_64BIT && size == 4
10137 && (!want_disp32 (&i.tm)
10138 || (i.tm.opcode_modifier.jump && !i.jumpabsolute
10139 && !i.types[n].bitfield.baseindex)))
10140 || pcrel;
02a86693 10141 fixS *fixP;
29b0f896 10142
e205caa7 10143 /* We can't have 8 bit displacement here. */
9c2799c2 10144 gas_assert (!i.types[n].bitfield.disp8);
e205caa7 10145
29b0f896
AM
10146 /* The PC relative address is computed relative
10147 to the instruction boundary, so in case immediate
10148 fields follows, we need to adjust the value. */
10149 if (pcrel && i.imm_operands)
10150 {
29b0f896 10151 unsigned int n1;
e205caa7 10152 int sz = 0;
252b5132 10153
29b0f896 10154 for (n1 = 0; n1 < i.operands; n1++)
40fb9820 10155 if (operand_type_check (i.types[n1], imm))
252b5132 10156 {
e205caa7
L
10157 /* Only one immediate is allowed for PC
10158 relative address. */
9c2799c2 10159 gas_assert (sz == 0);
e205caa7
L
10160 sz = imm_size (n1);
10161 i.op[n].disps->X_add_number -= sz;
252b5132 10162 }
29b0f896 10163 /* We should find the immediate. */
9c2799c2 10164 gas_assert (sz != 0);
29b0f896 10165 }
520dc8e8 10166
29b0f896 10167 p = frag_more (size);
d258b828 10168 reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
d6ab8113 10169 if (GOT_symbol
2bbd9c25 10170 && GOT_symbol == i.op[n].disps->X_add_symbol
d6ab8113 10171 && (((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
10172 || reloc_type == BFD_RELOC_X86_64_32S
10173 || (reloc_type == BFD_RELOC_64
10174 && object_64bit))
d6ab8113
JB
10175 && (i.op[n].disps->X_op == O_symbol
10176 || (i.op[n].disps->X_op == O_add
10177 && ((symbol_get_value_expression
10178 (i.op[n].disps->X_op_symbol)->X_op)
10179 == O_subtract))))
10180 || reloc_type == BFD_RELOC_32_PCREL))
2bbd9c25 10181 {
4fa24527 10182 if (!object_64bit)
7b81dfbb
AJ
10183 {
10184 reloc_type = BFD_RELOC_386_GOTPC;
5b7c81bd 10185 i.has_gotpc_tls_reloc = true;
98da05bf 10186 i.op[n].disps->X_add_number +=
d583596c 10187 encoding_length (insn_start_frag, insn_start_off, p);
7b81dfbb
AJ
10188 }
10189 else if (reloc_type == BFD_RELOC_64)
10190 reloc_type = BFD_RELOC_X86_64_GOTPC64;
d6ab8113 10191 else
7b81dfbb
AJ
10192 /* Don't do the adjustment for x86-64, as there
10193 the pcrel addressing is relative to the _next_
10194 insn, and that is taken care of in other code. */
d6ab8113 10195 reloc_type = BFD_RELOC_X86_64_GOTPC32;
2bbd9c25 10196 }
e379e5f3
L
10197 else if (align_branch_power)
10198 {
10199 switch (reloc_type)
10200 {
10201 case BFD_RELOC_386_TLS_GD:
10202 case BFD_RELOC_386_TLS_LDM:
10203 case BFD_RELOC_386_TLS_IE:
10204 case BFD_RELOC_386_TLS_IE_32:
10205 case BFD_RELOC_386_TLS_GOTIE:
10206 case BFD_RELOC_386_TLS_GOTDESC:
10207 case BFD_RELOC_386_TLS_DESC_CALL:
10208 case BFD_RELOC_X86_64_TLSGD:
10209 case BFD_RELOC_X86_64_TLSLD:
10210 case BFD_RELOC_X86_64_GOTTPOFF:
10211 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
10212 case BFD_RELOC_X86_64_TLSDESC_CALL:
5b7c81bd 10213 i.has_gotpc_tls_reloc = true;
e379e5f3
L
10214 default:
10215 break;
10216 }
10217 }
02a86693
L
10218 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal,
10219 size, i.op[n].disps, pcrel,
10220 reloc_type);
eb19308f
JB
10221
10222 if (flag_code == CODE_64BIT && size == 4 && pcrel
10223 && !i.prefix[ADDR_PREFIX])
10224 fixP->fx_signed = 1;
10225
02a86693
L
10226 /* Check for "call/jmp *mem", "mov mem, %reg",
10227 "test %reg, mem" and "binop mem, %reg" where binop
10228 is one of adc, add, and, cmp, or, sbb, sub, xor
e60f4d3b
L
10229 instructions without data prefix. Always generate
10230 R_386_GOT32X for "sym*GOT" operand in 32-bit mode. */
10231 if (i.prefix[DATA_PREFIX] == 0
10232 && (generate_relax_relocations
10233 || (!object_64bit
10234 && i.rm.mode == 0
10235 && i.rm.regmem == 5))
0cb4071e
L
10236 && (i.rm.mode == 2
10237 || (i.rm.mode == 0 && i.rm.regmem == 5))
389d00a5 10238 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
02a86693
L
10239 && ((i.operands == 1
10240 && i.tm.base_opcode == 0xff
10241 && (i.rm.reg == 2 || i.rm.reg == 4))
10242 || (i.operands == 2
10243 && (i.tm.base_opcode == 0x8b
10244 || i.tm.base_opcode == 0x85
2ae4c703 10245 || (i.tm.base_opcode & ~0x38) == 0x03))))
02a86693
L
10246 {
10247 if (object_64bit)
10248 {
10249 fixP->fx_tcbit = i.rex != 0;
10250 if (i.base_reg
e968fc9b 10251 && (i.base_reg->reg_num == RegIP))
02a86693
L
10252 fixP->fx_tcbit2 = 1;
10253 }
10254 else
10255 fixP->fx_tcbit2 = 1;
10256 }
29b0f896
AM
10257 }
10258 }
10259 }
10260}
252b5132 10261
29b0f896 10262static void
64e74474 10263output_imm (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
10264{
10265 char *p;
10266 unsigned int n;
252b5132 10267
29b0f896
AM
10268 for (n = 0; n < i.operands; n++)
10269 {
40fb9820 10270 if (operand_type_check (i.types[n], imm))
29b0f896 10271 {
48ef937e
JB
10272 int size = imm_size (n);
10273
10274 if (now_seg == absolute_section)
10275 abs_section_offset += size;
10276 else if (i.op[n].imms->X_op == O_constant)
29b0f896 10277 {
29b0f896 10278 offsetT val;
b4cac588 10279
29b0f896
AM
10280 val = offset_in_range (i.op[n].imms->X_add_number,
10281 size);
10282 p = frag_more (size);
10283 md_number_to_chars (p, val, size);
10284 }
10285 else
10286 {
10287 /* Not absolute_section.
10288 Need a 32-bit fixup (don't support 8bit
10289 non-absolute imms). Try to support other
10290 sizes ... */
f86103b7 10291 enum bfd_reloc_code_real reloc_type;
e205caa7 10292 int sign;
29b0f896 10293
40fb9820 10294 if (i.types[n].bitfield.imm32s
a7d61044 10295 && (i.suffix == QWORD_MNEM_SUFFIX
40fb9820 10296 || (!i.suffix && i.tm.opcode_modifier.no_lsuf)))
29b0f896 10297 sign = 1;
e205caa7
L
10298 else
10299 sign = 0;
520dc8e8 10300
29b0f896 10301 p = frag_more (size);
d258b828 10302 reloc_type = reloc (size, 0, sign, i.reloc[n]);
f86103b7 10303
2bbd9c25
JJ
10304 /* This is tough to explain. We end up with this one if we
10305 * have operands that look like
10306 * "_GLOBAL_OFFSET_TABLE_+[.-.L284]". The goal here is to
10307 * obtain the absolute address of the GOT, and it is strongly
10308 * preferable from a performance point of view to avoid using
10309 * a runtime relocation for this. The actual sequence of
10310 * instructions often look something like:
10311 *
10312 * call .L66
10313 * .L66:
10314 * popl %ebx
10315 * addl $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
10316 *
10317 * The call and pop essentially return the absolute address
10318 * of the label .L66 and store it in %ebx. The linker itself
10319 * will ultimately change the first operand of the addl so
10320 * that %ebx points to the GOT, but to keep things simple, the
10321 * .o file must have this operand set so that it generates not
10322 * the absolute address of .L66, but the absolute address of
10323 * itself. This allows the linker itself simply treat a GOTPC
10324 * relocation as asking for a pcrel offset to the GOT to be
10325 * added in, and the addend of the relocation is stored in the
10326 * operand field for the instruction itself.
10327 *
10328 * Our job here is to fix the operand so that it would add
10329 * the correct offset so that %ebx would point to itself. The
10330 * thing that is tricky is that .-.L66 will point to the
10331 * beginning of the instruction, so we need to further modify
10332 * the operand so that it will point to itself. There are
10333 * other cases where you have something like:
10334 *
10335 * .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
10336 *
10337 * and here no correction would be required. Internally in
10338 * the assembler we treat operands of this form as not being
10339 * pcrel since the '.' is explicitly mentioned, and I wonder
10340 * whether it would simplify matters to do it this way. Who
10341 * knows. In earlier versions of the PIC patches, the
10342 * pcrel_adjust field was used to store the correction, but
10343 * since the expression is not pcrel, I felt it would be
10344 * confusing to do it this way. */
10345
d6ab8113 10346 if ((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
10347 || reloc_type == BFD_RELOC_X86_64_32S
10348 || reloc_type == BFD_RELOC_64)
29b0f896
AM
10349 && GOT_symbol
10350 && GOT_symbol == i.op[n].imms->X_add_symbol
10351 && (i.op[n].imms->X_op == O_symbol
10352 || (i.op[n].imms->X_op == O_add
10353 && ((symbol_get_value_expression
10354 (i.op[n].imms->X_op_symbol)->X_op)
10355 == O_subtract))))
10356 {
4fa24527 10357 if (!object_64bit)
d6ab8113 10358 reloc_type = BFD_RELOC_386_GOTPC;
7b81dfbb 10359 else if (size == 4)
d6ab8113 10360 reloc_type = BFD_RELOC_X86_64_GOTPC32;
7b81dfbb
AJ
10361 else if (size == 8)
10362 reloc_type = BFD_RELOC_X86_64_GOTPC64;
5b7c81bd 10363 i.has_gotpc_tls_reloc = true;
d583596c
JB
10364 i.op[n].imms->X_add_number +=
10365 encoding_length (insn_start_frag, insn_start_off, p);
29b0f896 10366 }
29b0f896
AM
10367 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
10368 i.op[n].imms, 0, reloc_type);
10369 }
10370 }
10371 }
252b5132
RH
10372}
10373\f
d182319b
JB
10374/* x86_cons_fix_new is called via the expression parsing code when a
10375 reloc is needed. We use this hook to get the correct .got reloc. */
d182319b
JB
10376static int cons_sign = -1;
10377
10378void
e3bb37b5 10379x86_cons_fix_new (fragS *frag, unsigned int off, unsigned int len,
62ebcb5c 10380 expressionS *exp, bfd_reloc_code_real_type r)
d182319b 10381{
d258b828 10382 r = reloc (len, 0, cons_sign, r);
d182319b
JB
10383
10384#ifdef TE_PE
10385 if (exp->X_op == O_secrel)
10386 {
10387 exp->X_op = O_symbol;
10388 r = BFD_RELOC_32_SECREL;
10389 }
145667f8
MH
10390 else if (exp->X_op == O_secidx)
10391 r = BFD_RELOC_16_SECIDX;
d182319b
JB
10392#endif
10393
10394 fix_new_exp (frag, off, len, exp, 0, r);
10395}
10396
357d1bd8
L
10397/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
10398 purpose of the `.dc.a' internal pseudo-op. */
10399
10400int
10401x86_address_bytes (void)
10402{
10403 if ((stdoutput->arch_info->mach & bfd_mach_x64_32))
10404 return 4;
10405 return stdoutput->arch_info->bits_per_address / 8;
10406}
10407
deea4973
JB
10408#if (!(defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined (OBJ_MACH_O)) \
10409 || defined (LEX_AT)) && !defined (TE_PE)
d258b828 10410# define lex_got(reloc, adjust, types) NULL
718ddfc0 10411#else
f3c180ae
AM
10412/* Parse operands of the form
10413 <symbol>@GOTOFF+<nnn>
10414 and similar .plt or .got references.
10415
10416 If we find one, set up the correct relocation in RELOC and copy the
10417 input string, minus the `@GOTOFF' into a malloc'd buffer for
10418 parsing by the calling routine. Return this buffer, and if ADJUST
10419 is non-null set it to the length of the string we removed from the
10420 input line. Otherwise return NULL. */
10421static char *
91d6fa6a 10422lex_got (enum bfd_reloc_code_real *rel,
64e74474 10423 int *adjust,
d258b828 10424 i386_operand_type *types)
f3c180ae 10425{
7b81dfbb
AJ
10426 /* Some of the relocations depend on the size of what field is to
10427 be relocated. But in our callers i386_immediate and i386_displacement
10428 we don't yet know the operand size (this will be set by insn
10429 matching). Hence we record the word32 relocation here,
10430 and adjust the reloc according to the real size in reloc(). */
145667f8
MH
10431 static const struct
10432 {
f3c180ae 10433 const char *str;
cff8d58a 10434 int len;
4fa24527 10435 const enum bfd_reloc_code_real rel[2];
40fb9820 10436 const i386_operand_type types64;
5b7c81bd 10437 bool need_GOT_symbol;
145667f8
MH
10438 }
10439 gotrel[] =
10440 {
05909f23
JB
10441
10442#define OPERAND_TYPE_IMM32_32S_DISP32 { .bitfield = \
10443 { .imm32 = 1, .imm32s = 1, .disp32 = 1 } }
10444#define OPERAND_TYPE_IMM32_32S_64_DISP32 { .bitfield = \
10445 { .imm32 = 1, .imm32s = 1, .imm64 = 1, .disp32 = 1 } }
10446#define OPERAND_TYPE_IMM32_32S_64_DISP32_64 { .bitfield = \
10447 { .imm32 = 1, .imm32s = 1, .imm64 = 1, .disp32 = 1, .disp64 = 1 } }
10448#define OPERAND_TYPE_IMM64_DISP64 { .bitfield = \
10449 { .imm64 = 1, .disp64 = 1 } }
10450
deea4973 10451#ifndef TE_PE
8ce3d284 10452#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
10453 { STRING_COMMA_LEN ("SIZE"), { BFD_RELOC_SIZE32,
10454 BFD_RELOC_SIZE32 },
05909f23 10455 { .bitfield = { .imm32 = 1, .imm64 = 1 } }, false },
8ce3d284 10456#endif
cff8d58a
L
10457 { STRING_COMMA_LEN ("PLTOFF"), { _dummy_first_bfd_reloc_code_real,
10458 BFD_RELOC_X86_64_PLTOFF64 },
05909f23 10459 { .bitfield = { .imm64 = 1 } }, true },
cff8d58a
L
10460 { STRING_COMMA_LEN ("PLT"), { BFD_RELOC_386_PLT32,
10461 BFD_RELOC_X86_64_PLT32 },
a775efc8 10462 OPERAND_TYPE_IMM32_32S_DISP32, false },
cff8d58a
L
10463 { STRING_COMMA_LEN ("GOTPLT"), { _dummy_first_bfd_reloc_code_real,
10464 BFD_RELOC_X86_64_GOTPLT64 },
5b7c81bd 10465 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
10466 { STRING_COMMA_LEN ("GOTOFF"), { BFD_RELOC_386_GOTOFF,
10467 BFD_RELOC_X86_64_GOTOFF64 },
5b7c81bd 10468 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
10469 { STRING_COMMA_LEN ("GOTPCREL"), { _dummy_first_bfd_reloc_code_real,
10470 BFD_RELOC_X86_64_GOTPCREL },
a775efc8 10471 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10472 { STRING_COMMA_LEN ("TLSGD"), { BFD_RELOC_386_TLS_GD,
10473 BFD_RELOC_X86_64_TLSGD },
a775efc8 10474 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10475 { STRING_COMMA_LEN ("TLSLDM"), { BFD_RELOC_386_TLS_LDM,
10476 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10477 OPERAND_TYPE_NONE, true },
cff8d58a
L
10478 { STRING_COMMA_LEN ("TLSLD"), { _dummy_first_bfd_reloc_code_real,
10479 BFD_RELOC_X86_64_TLSLD },
a775efc8 10480 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10481 { STRING_COMMA_LEN ("GOTTPOFF"), { BFD_RELOC_386_TLS_IE_32,
10482 BFD_RELOC_X86_64_GOTTPOFF },
a775efc8 10483 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10484 { STRING_COMMA_LEN ("TPOFF"), { BFD_RELOC_386_TLS_LE_32,
10485 BFD_RELOC_X86_64_TPOFF32 },
a775efc8 10486 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
10487 { STRING_COMMA_LEN ("NTPOFF"), { BFD_RELOC_386_TLS_LE,
10488 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10489 OPERAND_TYPE_NONE, true },
cff8d58a
L
10490 { STRING_COMMA_LEN ("DTPOFF"), { BFD_RELOC_386_TLS_LDO_32,
10491 BFD_RELOC_X86_64_DTPOFF32 },
a775efc8 10492 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
10493 { STRING_COMMA_LEN ("GOTNTPOFF"),{ BFD_RELOC_386_TLS_GOTIE,
10494 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10495 OPERAND_TYPE_NONE, true },
cff8d58a
L
10496 { STRING_COMMA_LEN ("INDNTPOFF"),{ BFD_RELOC_386_TLS_IE,
10497 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10498 OPERAND_TYPE_NONE, true },
cff8d58a
L
10499 { STRING_COMMA_LEN ("GOT"), { BFD_RELOC_386_GOT32,
10500 BFD_RELOC_X86_64_GOT32 },
a775efc8 10501 OPERAND_TYPE_IMM32_32S_64_DISP32, true },
cff8d58a
L
10502 { STRING_COMMA_LEN ("TLSDESC"), { BFD_RELOC_386_TLS_GOTDESC,
10503 BFD_RELOC_X86_64_GOTPC32_TLSDESC },
a775efc8 10504 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10505 { STRING_COMMA_LEN ("TLSCALL"), { BFD_RELOC_386_TLS_DESC_CALL,
10506 BFD_RELOC_X86_64_TLSDESC_CALL },
a775efc8 10507 OPERAND_TYPE_IMM32_32S_DISP32, true },
deea4973
JB
10508#else /* TE_PE */
10509 { STRING_COMMA_LEN ("SECREL32"), { BFD_RELOC_32_SECREL,
10510 BFD_RELOC_32_SECREL },
a775efc8 10511 OPERAND_TYPE_IMM32_32S_64_DISP32_64, false },
deea4973 10512#endif
05909f23
JB
10513
10514#undef OPERAND_TYPE_IMM32_32S_DISP32
10515#undef OPERAND_TYPE_IMM32_32S_64_DISP32
10516#undef OPERAND_TYPE_IMM32_32S_64_DISP32_64
10517#undef OPERAND_TYPE_IMM64_DISP64
10518
f3c180ae
AM
10519 };
10520 char *cp;
10521 unsigned int j;
10522
deea4973 10523#if defined (OBJ_MAYBE_ELF) && !defined (TE_PE)
718ddfc0
JB
10524 if (!IS_ELF)
10525 return NULL;
d382c579 10526#endif
718ddfc0 10527
f3c180ae 10528 for (cp = input_line_pointer; *cp != '@'; cp++)
67c11a9b 10529 if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
f3c180ae
AM
10530 return NULL;
10531
47465058 10532 for (j = 0; j < ARRAY_SIZE (gotrel); j++)
f3c180ae 10533 {
cff8d58a 10534 int len = gotrel[j].len;
28f81592 10535 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
f3c180ae 10536 {
4fa24527 10537 if (gotrel[j].rel[object_64bit] != 0)
f3c180ae 10538 {
28f81592
AM
10539 int first, second;
10540 char *tmpbuf, *past_reloc;
f3c180ae 10541
91d6fa6a 10542 *rel = gotrel[j].rel[object_64bit];
f3c180ae 10543
3956db08
JB
10544 if (types)
10545 {
10546 if (flag_code != CODE_64BIT)
40fb9820
L
10547 {
10548 types->bitfield.imm32 = 1;
10549 types->bitfield.disp32 = 1;
10550 }
3956db08
JB
10551 else
10552 *types = gotrel[j].types64;
10553 }
10554
844bf810 10555 if (gotrel[j].need_GOT_symbol && GOT_symbol == NULL)
f3c180ae
AM
10556 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
10557
28f81592 10558 /* The length of the first part of our input line. */
f3c180ae 10559 first = cp - input_line_pointer;
28f81592
AM
10560
10561 /* The second part goes from after the reloc token until
67c11a9b 10562 (and including) an end_of_line char or comma. */
28f81592 10563 past_reloc = cp + 1 + len;
67c11a9b
AM
10564 cp = past_reloc;
10565 while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
10566 ++cp;
10567 second = cp + 1 - past_reloc;
28f81592
AM
10568
10569 /* Allocate and copy string. The trailing NUL shouldn't
10570 be necessary, but be safe. */
add39d23 10571 tmpbuf = XNEWVEC (char, first + second + 2);
f3c180ae 10572 memcpy (tmpbuf, input_line_pointer, first);
0787a12d
AM
10573 if (second != 0 && *past_reloc != ' ')
10574 /* Replace the relocation token with ' ', so that
10575 errors like foo@GOTOFF1 will be detected. */
10576 tmpbuf[first++] = ' ';
af89796a
L
10577 else
10578 /* Increment length by 1 if the relocation token is
10579 removed. */
10580 len++;
10581 if (adjust)
10582 *adjust = len;
0787a12d
AM
10583 memcpy (tmpbuf + first, past_reloc, second);
10584 tmpbuf[first + second] = '\0';
f3c180ae
AM
10585 return tmpbuf;
10586 }
10587
4fa24527
JB
10588 as_bad (_("@%s reloc is not supported with %d-bit output format"),
10589 gotrel[j].str, 1 << (5 + object_64bit));
f3c180ae
AM
10590 return NULL;
10591 }
10592 }
10593
10594 /* Might be a symbol version string. Don't as_bad here. */
10595 return NULL;
10596}
4e4f7c87 10597#endif
f3c180ae 10598
62ebcb5c 10599bfd_reloc_code_real_type
e3bb37b5 10600x86_cons (expressionS *exp, int size)
f3c180ae 10601{
62ebcb5c
AM
10602 bfd_reloc_code_real_type got_reloc = NO_RELOC;
10603
2748c1b1
L
10604#if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
10605 && !defined (LEX_AT)) \
10606 || defined (TE_PE)
ee86248c
JB
10607 intel_syntax = -intel_syntax;
10608
3c7b9c2c 10609 exp->X_md = 0;
4fa24527 10610 if (size == 4 || (object_64bit && size == 8))
f3c180ae
AM
10611 {
10612 /* Handle @GOTOFF and the like in an expression. */
10613 char *save;
10614 char *gotfree_input_line;
4a57f2cf 10615 int adjust = 0;
f3c180ae
AM
10616
10617 save = input_line_pointer;
d258b828 10618 gotfree_input_line = lex_got (&got_reloc, &adjust, NULL);
f3c180ae
AM
10619 if (gotfree_input_line)
10620 input_line_pointer = gotfree_input_line;
10621
10622 expression (exp);
10623
10624 if (gotfree_input_line)
10625 {
10626 /* expression () has merrily parsed up to the end of line,
10627 or a comma - in the wrong buffer. Transfer how far
10628 input_line_pointer has moved to the right buffer. */
10629 input_line_pointer = (save
10630 + (input_line_pointer - gotfree_input_line)
10631 + adjust);
10632 free (gotfree_input_line);
3992d3b7
AM
10633 if (exp->X_op == O_constant
10634 || exp->X_op == O_absent
10635 || exp->X_op == O_illegal
0398aac5 10636 || exp->X_op == O_register
3992d3b7
AM
10637 || exp->X_op == O_big)
10638 {
10639 char c = *input_line_pointer;
10640 *input_line_pointer = 0;
10641 as_bad (_("missing or invalid expression `%s'"), save);
10642 *input_line_pointer = c;
10643 }
b9519cfe
L
10644 else if ((got_reloc == BFD_RELOC_386_PLT32
10645 || got_reloc == BFD_RELOC_X86_64_PLT32)
10646 && exp->X_op != O_symbol)
10647 {
10648 char c = *input_line_pointer;
10649 *input_line_pointer = 0;
10650 as_bad (_("invalid PLT expression `%s'"), save);
10651 *input_line_pointer = c;
10652 }
f3c180ae
AM
10653 }
10654 }
10655 else
10656 expression (exp);
ee86248c
JB
10657
10658 intel_syntax = -intel_syntax;
10659
10660 if (intel_syntax)
10661 i386_intel_simplify (exp);
2748c1b1
L
10662#else
10663 expression (exp);
10664#endif
62ebcb5c 10665
a442cac5
JB
10666 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
10667 if (size == 4 && exp->X_op == O_constant && !object_64bit)
10668 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
10669
62ebcb5c 10670 return got_reloc;
f3c180ae 10671}
f3c180ae 10672
9f32dd5b
L
10673static void
10674signed_cons (int size)
6482c264 10675{
a442cac5 10676 if (object_64bit)
d182319b
JB
10677 cons_sign = 1;
10678 cons (size);
10679 cons_sign = -1;
6482c264
NC
10680}
10681
d182319b 10682#ifdef TE_PE
6482c264 10683static void
7016a5d5 10684pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
6482c264
NC
10685{
10686 expressionS exp;
10687
10688 do
10689 {
10690 expression (&exp);
10691 if (exp.X_op == O_symbol)
10692 exp.X_op = O_secrel;
10693
10694 emit_expr (&exp, 4);
10695 }
10696 while (*input_line_pointer++ == ',');
10697
10698 input_line_pointer--;
10699 demand_empty_rest_of_line ();
10700}
145667f8
MH
10701
10702static void
10703pe_directive_secidx (int dummy ATTRIBUTE_UNUSED)
10704{
10705 expressionS exp;
10706
10707 do
10708 {
10709 expression (&exp);
10710 if (exp.X_op == O_symbol)
10711 exp.X_op = O_secidx;
10712
10713 emit_expr (&exp, 2);
10714 }
10715 while (*input_line_pointer++ == ',');
10716
10717 input_line_pointer--;
10718 demand_empty_rest_of_line ();
10719}
6482c264
NC
10720#endif
10721
7063667e
JB
10722/* Handle Rounding Control / SAE specifiers. */
10723
10724static char *
10725RC_SAE_specifier (const char *pstr)
10726{
10727 unsigned int j;
10728
10729 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); j++)
10730 {
10731 if (!strncmp (pstr, RC_NamesTable[j].name, RC_NamesTable[j].len))
10732 {
10733 if (i.rounding.type != rc_none)
10734 {
10735 as_bad (_("duplicated `{%s}'"), RC_NamesTable[j].name);
10736 return NULL;
10737 }
10738
10739 i.rounding.type = RC_NamesTable[j].type;
10740
10741 return (char *)(pstr + RC_NamesTable[j].len);
10742 }
10743 }
10744
10745 return NULL;
10746}
10747
43234a1e
L
10748/* Handle Vector operations. */
10749
10750static char *
f70c6814 10751check_VecOperations (char *op_string)
43234a1e
L
10752{
10753 const reg_entry *mask;
10754 const char *saved;
10755 char *end_op;
10756
f70c6814 10757 while (*op_string)
43234a1e
L
10758 {
10759 saved = op_string;
10760 if (*op_string == '{')
10761 {
10762 op_string++;
10763
10764 /* Check broadcasts. */
d34049e8 10765 if (startswith (op_string, "1to"))
43234a1e 10766 {
5273a3cd 10767 unsigned int bcst_type;
43234a1e 10768
5273a3cd 10769 if (i.broadcast.type)
43234a1e
L
10770 goto duplicated_vec_op;
10771
10772 op_string += 3;
10773 if (*op_string == '8')
8e6e0792 10774 bcst_type = 8;
b28d1bda 10775 else if (*op_string == '4')
8e6e0792 10776 bcst_type = 4;
b28d1bda 10777 else if (*op_string == '2')
8e6e0792 10778 bcst_type = 2;
43234a1e
L
10779 else if (*op_string == '1'
10780 && *(op_string+1) == '6')
10781 {
8e6e0792 10782 bcst_type = 16;
43234a1e
L
10783 op_string++;
10784 }
0cc78721
CL
10785 else if (*op_string == '3'
10786 && *(op_string+1) == '2')
10787 {
10788 bcst_type = 32;
10789 op_string++;
10790 }
43234a1e
L
10791 else
10792 {
10793 as_bad (_("Unsupported broadcast: `%s'"), saved);
10794 return NULL;
10795 }
10796 op_string++;
10797
5273a3cd
JB
10798 i.broadcast.type = bcst_type;
10799 i.broadcast.operand = this_operand;
43234a1e
L
10800 }
10801 /* Check masking operation. */
10802 else if ((mask = parse_register (op_string, &end_op)) != NULL)
10803 {
8a6fb3f9
JB
10804 if (mask == &bad_reg)
10805 return NULL;
10806
43234a1e 10807 /* k0 can't be used for write mask. */
f74a6307 10808 if (mask->reg_type.bitfield.class != RegMask || !mask->reg_num)
43234a1e 10809 {
6d2cd6b2
JB
10810 as_bad (_("`%s%s' can't be used for write mask"),
10811 register_prefix, mask->reg_name);
43234a1e
L
10812 return NULL;
10813 }
10814
6225c532 10815 if (!i.mask.reg)
43234a1e 10816 {
6225c532
JB
10817 i.mask.reg = mask;
10818 i.mask.operand = this_operand;
43234a1e 10819 }
6225c532
JB
10820 else if (i.mask.reg->reg_num)
10821 goto duplicated_vec_op;
43234a1e
L
10822 else
10823 {
6225c532 10824 i.mask.reg = mask;
43234a1e
L
10825
10826 /* Only "{z}" is allowed here. No need to check
10827 zeroing mask explicitly. */
6225c532 10828 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
10829 {
10830 as_bad (_("invalid write mask `%s'"), saved);
10831 return NULL;
10832 }
10833 }
10834
10835 op_string = end_op;
10836 }
10837 /* Check zeroing-flag for masking operation. */
10838 else if (*op_string == 'z')
10839 {
6225c532 10840 if (!i.mask.reg)
43234a1e 10841 {
6225c532
JB
10842 i.mask.reg = reg_k0;
10843 i.mask.zeroing = 1;
10844 i.mask.operand = this_operand;
43234a1e
L
10845 }
10846 else
10847 {
6225c532 10848 if (i.mask.zeroing)
43234a1e
L
10849 {
10850 duplicated_vec_op:
10851 as_bad (_("duplicated `%s'"), saved);
10852 return NULL;
10853 }
10854
6225c532 10855 i.mask.zeroing = 1;
43234a1e
L
10856
10857 /* Only "{%k}" is allowed here. No need to check mask
10858 register explicitly. */
6225c532 10859 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
10860 {
10861 as_bad (_("invalid zeroing-masking `%s'"),
10862 saved);
10863 return NULL;
10864 }
10865 }
10866
10867 op_string++;
10868 }
7063667e
JB
10869 else if (intel_syntax
10870 && (op_string = RC_SAE_specifier (op_string)) != NULL)
10871 i.rounding.modifier = true;
43234a1e
L
10872 else
10873 goto unknown_vec_op;
10874
10875 if (*op_string != '}')
10876 {
10877 as_bad (_("missing `}' in `%s'"), saved);
10878 return NULL;
10879 }
10880 op_string++;
0ba3a731
L
10881
10882 /* Strip whitespace since the addition of pseudo prefixes
10883 changed how the scrubber treats '{'. */
10884 if (is_space_char (*op_string))
10885 ++op_string;
10886
43234a1e
L
10887 continue;
10888 }
10889 unknown_vec_op:
10890 /* We don't know this one. */
10891 as_bad (_("unknown vector operation: `%s'"), saved);
10892 return NULL;
10893 }
10894
6225c532 10895 if (i.mask.reg && i.mask.zeroing && !i.mask.reg->reg_num)
6d2cd6b2
JB
10896 {
10897 as_bad (_("zeroing-masking only allowed with write mask"));
10898 return NULL;
10899 }
10900
43234a1e
L
10901 return op_string;
10902}
10903
252b5132 10904static int
70e41ade 10905i386_immediate (char *imm_start)
252b5132
RH
10906{
10907 char *save_input_line_pointer;
f3c180ae 10908 char *gotfree_input_line;
252b5132 10909 segT exp_seg = 0;
47926f60 10910 expressionS *exp;
40fb9820
L
10911 i386_operand_type types;
10912
0dfbf9d7 10913 operand_type_set (&types, ~0);
252b5132
RH
10914
10915 if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
10916 {
31b2323c
L
10917 as_bad (_("at most %d immediate operands are allowed"),
10918 MAX_IMMEDIATE_OPERANDS);
252b5132
RH
10919 return 0;
10920 }
10921
10922 exp = &im_expressions[i.imm_operands++];
520dc8e8 10923 i.op[this_operand].imms = exp;
252b5132
RH
10924
10925 if (is_space_char (*imm_start))
10926 ++imm_start;
10927
10928 save_input_line_pointer = input_line_pointer;
10929 input_line_pointer = imm_start;
10930
d258b828 10931 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
10932 if (gotfree_input_line)
10933 input_line_pointer = gotfree_input_line;
252b5132
RH
10934
10935 exp_seg = expression (exp);
10936
83183c0c 10937 SKIP_WHITESPACE ();
252b5132 10938 if (*input_line_pointer)
f3c180ae 10939 as_bad (_("junk `%s' after expression"), input_line_pointer);
252b5132
RH
10940
10941 input_line_pointer = save_input_line_pointer;
f3c180ae 10942 if (gotfree_input_line)
ee86248c
JB
10943 {
10944 free (gotfree_input_line);
10945
9aac24b1 10946 if (exp->X_op == O_constant)
ee86248c
JB
10947 exp->X_op = O_illegal;
10948 }
10949
9aac24b1
JB
10950 if (exp_seg == reg_section)
10951 {
10952 as_bad (_("illegal immediate register operand %s"), imm_start);
10953 return 0;
10954 }
10955
ee86248c
JB
10956 return i386_finalize_immediate (exp_seg, exp, types, imm_start);
10957}
252b5132 10958
ee86248c
JB
10959static int
10960i386_finalize_immediate (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
10961 i386_operand_type types, const char *imm_start)
10962{
10963 if (exp->X_op == O_absent || exp->X_op == O_illegal || exp->X_op == O_big)
252b5132 10964 {
313c53d1
L
10965 if (imm_start)
10966 as_bad (_("missing or invalid immediate expression `%s'"),
10967 imm_start);
3992d3b7 10968 return 0;
252b5132 10969 }
3e73aa7c 10970 else if (exp->X_op == O_constant)
252b5132 10971 {
47926f60 10972 /* Size it properly later. */
40fb9820 10973 i.types[this_operand].bitfield.imm64 = 1;
a442cac5
JB
10974
10975 /* If not 64bit, sign/zero extend val, to account for wraparound
10976 when !BFD64. */
10977 if (flag_code != CODE_64BIT)
10978 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
252b5132 10979 }
4c63da97 10980#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
f86103b7 10981 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
31312f95 10982 && exp_seg != absolute_section
47926f60 10983 && exp_seg != text_section
24eab124
AM
10984 && exp_seg != data_section
10985 && exp_seg != bss_section
10986 && exp_seg != undefined_section
f86103b7 10987 && !bfd_is_com_section (exp_seg))
252b5132 10988 {
d0b47220 10989 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
252b5132
RH
10990 return 0;
10991 }
10992#endif
10993 else
10994 {
10995 /* This is an address. The size of the address will be
24eab124 10996 determined later, depending on destination register,
3e73aa7c 10997 suffix, or the default for the section. */
40fb9820
L
10998 i.types[this_operand].bitfield.imm8 = 1;
10999 i.types[this_operand].bitfield.imm16 = 1;
11000 i.types[this_operand].bitfield.imm32 = 1;
11001 i.types[this_operand].bitfield.imm32s = 1;
11002 i.types[this_operand].bitfield.imm64 = 1;
c6fb90c8
L
11003 i.types[this_operand] = operand_type_and (i.types[this_operand],
11004 types);
252b5132
RH
11005 }
11006
11007 return 1;
11008}
11009
551c1ca1 11010static char *
e3bb37b5 11011i386_scale (char *scale)
252b5132 11012{
551c1ca1
AM
11013 offsetT val;
11014 char *save = input_line_pointer;
252b5132 11015
551c1ca1
AM
11016 input_line_pointer = scale;
11017 val = get_absolute_expression ();
11018
11019 switch (val)
252b5132 11020 {
551c1ca1 11021 case 1:
252b5132
RH
11022 i.log2_scale_factor = 0;
11023 break;
551c1ca1 11024 case 2:
252b5132
RH
11025 i.log2_scale_factor = 1;
11026 break;
551c1ca1 11027 case 4:
252b5132
RH
11028 i.log2_scale_factor = 2;
11029 break;
551c1ca1 11030 case 8:
252b5132
RH
11031 i.log2_scale_factor = 3;
11032 break;
11033 default:
a724f0f4
JB
11034 {
11035 char sep = *input_line_pointer;
11036
11037 *input_line_pointer = '\0';
11038 as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
11039 scale);
11040 *input_line_pointer = sep;
11041 input_line_pointer = save;
11042 return NULL;
11043 }
252b5132 11044 }
29b0f896 11045 if (i.log2_scale_factor != 0 && i.index_reg == 0)
252b5132
RH
11046 {
11047 as_warn (_("scale factor of %d without an index register"),
24eab124 11048 1 << i.log2_scale_factor);
252b5132 11049 i.log2_scale_factor = 0;
252b5132 11050 }
551c1ca1
AM
11051 scale = input_line_pointer;
11052 input_line_pointer = save;
11053 return scale;
252b5132
RH
11054}
11055
252b5132 11056static int
e3bb37b5 11057i386_displacement (char *disp_start, char *disp_end)
252b5132 11058{
29b0f896 11059 expressionS *exp;
252b5132
RH
11060 segT exp_seg = 0;
11061 char *save_input_line_pointer;
f3c180ae 11062 char *gotfree_input_line;
40fb9820
L
11063 int override;
11064 i386_operand_type bigdisp, types = anydisp;
3992d3b7 11065 int ret;
252b5132 11066
31b2323c
L
11067 if (i.disp_operands == MAX_MEMORY_OPERANDS)
11068 {
11069 as_bad (_("at most %d displacement operands are allowed"),
11070 MAX_MEMORY_OPERANDS);
11071 return 0;
11072 }
11073
0dfbf9d7 11074 operand_type_set (&bigdisp, 0);
6f2f06be 11075 if (i.jumpabsolute
48bcea9f 11076 || i.types[this_operand].bitfield.baseindex
0cfa3eb3
JB
11077 || (current_templates->start->opcode_modifier.jump != JUMP
11078 && current_templates->start->opcode_modifier.jump != JUMP_DWORD))
e05278af 11079 {
48bcea9f 11080 i386_addressing_mode ();
e05278af 11081 override = (i.prefix[ADDR_PREFIX] != 0);
40fb9820
L
11082 if (flag_code == CODE_64BIT)
11083 {
a775efc8 11084 bigdisp.bitfield.disp32 = 1;
40fb9820 11085 if (!override)
a775efc8 11086 bigdisp.bitfield.disp64 = 1;
40fb9820
L
11087 }
11088 else if ((flag_code == CODE_16BIT) ^ override)
40fb9820 11089 bigdisp.bitfield.disp16 = 1;
48bcea9f
JB
11090 else
11091 bigdisp.bitfield.disp32 = 1;
e05278af
JB
11092 }
11093 else
11094 {
376cd056
JB
11095 /* For PC-relative branches, the width of the displacement may be
11096 dependent upon data size, but is never dependent upon address size.
11097 Also make sure to not unintentionally match against a non-PC-relative
11098 branch template. */
11099 static templates aux_templates;
11100 const insn_template *t = current_templates->start;
5b7c81bd 11101 bool has_intel64 = false;
376cd056
JB
11102
11103 aux_templates.start = t;
11104 while (++t < current_templates->end)
11105 {
11106 if (t->opcode_modifier.jump
11107 != current_templates->start->opcode_modifier.jump)
11108 break;
4b5aaf5f 11109 if ((t->opcode_modifier.isa64 >= INTEL64))
5b7c81bd 11110 has_intel64 = true;
376cd056
JB
11111 }
11112 if (t < current_templates->end)
11113 {
11114 aux_templates.end = t;
11115 current_templates = &aux_templates;
11116 }
11117
e05278af 11118 override = (i.prefix[DATA_PREFIX] != 0);
40fb9820
L
11119 if (flag_code == CODE_64BIT)
11120 {
376cd056
JB
11121 if ((override || i.suffix == WORD_MNEM_SUFFIX)
11122 && (!intel64 || !has_intel64))
40fb9820
L
11123 bigdisp.bitfield.disp16 = 1;
11124 else
a775efc8 11125 bigdisp.bitfield.disp32 = 1;
40fb9820
L
11126 }
11127 else
e05278af
JB
11128 {
11129 if (!override)
11130 override = (i.suffix == (flag_code != CODE_16BIT
11131 ? WORD_MNEM_SUFFIX
11132 : LONG_MNEM_SUFFIX));
40fb9820
L
11133 bigdisp.bitfield.disp32 = 1;
11134 if ((flag_code == CODE_16BIT) ^ override)
11135 {
11136 bigdisp.bitfield.disp32 = 0;
11137 bigdisp.bitfield.disp16 = 1;
11138 }
e05278af 11139 }
e05278af 11140 }
c6fb90c8
L
11141 i.types[this_operand] = operand_type_or (i.types[this_operand],
11142 bigdisp);
252b5132
RH
11143
11144 exp = &disp_expressions[i.disp_operands];
520dc8e8 11145 i.op[this_operand].disps = exp;
252b5132
RH
11146 i.disp_operands++;
11147 save_input_line_pointer = input_line_pointer;
11148 input_line_pointer = disp_start;
11149 END_STRING_AND_SAVE (disp_end);
11150
11151#ifndef GCC_ASM_O_HACK
11152#define GCC_ASM_O_HACK 0
11153#endif
11154#if GCC_ASM_O_HACK
11155 END_STRING_AND_SAVE (disp_end + 1);
40fb9820 11156 if (i.types[this_operand].bitfield.baseIndex
24eab124 11157 && displacement_string_end[-1] == '+')
252b5132
RH
11158 {
11159 /* This hack is to avoid a warning when using the "o"
24eab124
AM
11160 constraint within gcc asm statements.
11161 For instance:
11162
11163 #define _set_tssldt_desc(n,addr,limit,type) \
11164 __asm__ __volatile__ ( \
11165 "movw %w2,%0\n\t" \
11166 "movw %w1,2+%0\n\t" \
11167 "rorl $16,%1\n\t" \
11168 "movb %b1,4+%0\n\t" \
11169 "movb %4,5+%0\n\t" \
11170 "movb $0,6+%0\n\t" \
11171 "movb %h1,7+%0\n\t" \
11172 "rorl $16,%1" \
11173 : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
11174
11175 This works great except that the output assembler ends
11176 up looking a bit weird if it turns out that there is
11177 no offset. You end up producing code that looks like:
11178
11179 #APP
11180 movw $235,(%eax)
11181 movw %dx,2+(%eax)
11182 rorl $16,%edx
11183 movb %dl,4+(%eax)
11184 movb $137,5+(%eax)
11185 movb $0,6+(%eax)
11186 movb %dh,7+(%eax)
11187 rorl $16,%edx
11188 #NO_APP
11189
47926f60 11190 So here we provide the missing zero. */
24eab124
AM
11191
11192 *displacement_string_end = '0';
252b5132
RH
11193 }
11194#endif
d258b828 11195 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
11196 if (gotfree_input_line)
11197 input_line_pointer = gotfree_input_line;
252b5132 11198
24eab124 11199 exp_seg = expression (exp);
252b5132 11200
636c26b0
AM
11201 SKIP_WHITESPACE ();
11202 if (*input_line_pointer)
11203 as_bad (_("junk `%s' after expression"), input_line_pointer);
11204#if GCC_ASM_O_HACK
11205 RESTORE_END_STRING (disp_end + 1);
11206#endif
636c26b0 11207 input_line_pointer = save_input_line_pointer;
636c26b0 11208 if (gotfree_input_line)
ee86248c
JB
11209 {
11210 free (gotfree_input_line);
11211
11212 if (exp->X_op == O_constant || exp->X_op == O_register)
11213 exp->X_op = O_illegal;
11214 }
11215
11216 ret = i386_finalize_displacement (exp_seg, exp, types, disp_start);
11217
11218 RESTORE_END_STRING (disp_end);
11219
11220 return ret;
11221}
11222
11223static int
11224i386_finalize_displacement (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
11225 i386_operand_type types, const char *disp_start)
11226{
ee86248c 11227 int ret = 1;
636c26b0 11228
24eab124
AM
11229 /* We do this to make sure that the section symbol is in
11230 the symbol table. We will ultimately change the relocation
47926f60 11231 to be relative to the beginning of the section. */
1ae12ab7 11232 if (i.reloc[this_operand] == BFD_RELOC_386_GOTOFF
d6ab8113
JB
11233 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
11234 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
24eab124 11235 {
636c26b0 11236 if (exp->X_op != O_symbol)
3992d3b7 11237 goto inv_disp;
636c26b0 11238
e5cb08ac 11239 if (S_IS_LOCAL (exp->X_add_symbol)
c64efb4b
L
11240 && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section
11241 && S_GET_SEGMENT (exp->X_add_symbol) != expr_section)
24eab124 11242 section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
24eab124
AM
11243 exp->X_op = O_subtract;
11244 exp->X_op_symbol = GOT_symbol;
1ae12ab7 11245 if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
29b0f896 11246 i.reloc[this_operand] = BFD_RELOC_32_PCREL;
d6ab8113
JB
11247 else if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
11248 i.reloc[this_operand] = BFD_RELOC_64;
23df1078 11249 else
29b0f896 11250 i.reloc[this_operand] = BFD_RELOC_32;
24eab124 11251 }
252b5132 11252
3992d3b7
AM
11253 else if (exp->X_op == O_absent
11254 || exp->X_op == O_illegal
ee86248c 11255 || exp->X_op == O_big)
2daf4fd8 11256 {
3992d3b7
AM
11257 inv_disp:
11258 as_bad (_("missing or invalid displacement expression `%s'"),
2daf4fd8 11259 disp_start);
3992d3b7 11260 ret = 0;
2daf4fd8
AM
11261 }
11262
a50187b2
JB
11263 else if (exp->X_op == O_constant)
11264 {
11265 /* Sizing gets taken care of by optimize_disp().
11266
11267 If not 64bit, sign/zero extend val, to account for wraparound
11268 when !BFD64. */
11269 if (flag_code != CODE_64BIT)
11270 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
11271 }
11272
4c63da97 11273#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
a50187b2 11274 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
3992d3b7
AM
11275 && exp_seg != absolute_section
11276 && exp_seg != text_section
11277 && exp_seg != data_section
11278 && exp_seg != bss_section
11279 && exp_seg != undefined_section
11280 && !bfd_is_com_section (exp_seg))
24eab124 11281 {
d0b47220 11282 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
3992d3b7 11283 ret = 0;
24eab124 11284 }
252b5132 11285#endif
3956db08 11286
a50187b2 11287 else if (current_templates->start->opcode_modifier.jump == JUMP_BYTE)
48bcea9f
JB
11288 i.types[this_operand].bitfield.disp8 = 1;
11289
40fb9820 11290 /* Check if this is a displacement only operand. */
02b83698
JB
11291 if (!i.types[this_operand].bitfield.baseindex)
11292 i.types[this_operand] =
11293 operand_type_or (operand_type_and_not (i.types[this_operand], anydisp),
11294 operand_type_and (i.types[this_operand], types));
3956db08 11295
3992d3b7 11296 return ret;
252b5132
RH
11297}
11298
2abc2bec
JB
11299/* Return the active addressing mode, taking address override and
11300 registers forming the address into consideration. Update the
11301 address override prefix if necessary. */
47926f60 11302
2abc2bec
JB
11303static enum flag_code
11304i386_addressing_mode (void)
252b5132 11305{
be05d201
L
11306 enum flag_code addr_mode;
11307
11308 if (i.prefix[ADDR_PREFIX])
11309 addr_mode = flag_code == CODE_32BIT ? CODE_16BIT : CODE_32BIT;
a23b33b3
JB
11310 else if (flag_code == CODE_16BIT
11311 && current_templates->start->cpu_flags.bitfield.cpumpx
11312 /* Avoid replacing the "16-bit addressing not allowed" diagnostic
11313 from md_assemble() by "is not a valid base/index expression"
11314 when there is a base and/or index. */
11315 && !i.types[this_operand].bitfield.baseindex)
11316 {
11317 /* MPX insn memory operands with neither base nor index must be forced
11318 to use 32-bit addressing in 16-bit mode. */
11319 addr_mode = CODE_32BIT;
11320 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
11321 ++i.prefixes;
11322 gas_assert (!i.types[this_operand].bitfield.disp16);
11323 gas_assert (!i.types[this_operand].bitfield.disp32);
11324 }
be05d201
L
11325 else
11326 {
11327 addr_mode = flag_code;
11328
24eab124 11329#if INFER_ADDR_PREFIX
be05d201
L
11330 if (i.mem_operands == 0)
11331 {
11332 /* Infer address prefix from the first memory operand. */
11333 const reg_entry *addr_reg = i.base_reg;
11334
11335 if (addr_reg == NULL)
11336 addr_reg = i.index_reg;
eecb386c 11337
be05d201
L
11338 if (addr_reg)
11339 {
e968fc9b 11340 if (addr_reg->reg_type.bitfield.dword)
be05d201
L
11341 addr_mode = CODE_32BIT;
11342 else if (flag_code != CODE_64BIT
dc821c5f 11343 && addr_reg->reg_type.bitfield.word)
be05d201
L
11344 addr_mode = CODE_16BIT;
11345
11346 if (addr_mode != flag_code)
11347 {
11348 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
11349 i.prefixes += 1;
11350 /* Change the size of any displacement too. At most one
11351 of Disp16 or Disp32 is set.
11352 FIXME. There doesn't seem to be any real need for
11353 separate Disp16 and Disp32 flags. The same goes for
11354 Imm16 and Imm32. Removing them would probably clean
11355 up the code quite a lot. */
11356 if (flag_code != CODE_64BIT
11357 && (i.types[this_operand].bitfield.disp16
11358 || i.types[this_operand].bitfield.disp32))
05909f23
JB
11359 {
11360 static const i386_operand_type disp16_32 = {
11361 .bitfield = { .disp16 = 1, .disp32 = 1 }
11362 };
11363
11364 i.types[this_operand]
11365 = operand_type_xor (i.types[this_operand], disp16_32);
11366 }
be05d201
L
11367 }
11368 }
11369 }
24eab124 11370#endif
be05d201
L
11371 }
11372
2abc2bec
JB
11373 return addr_mode;
11374}
11375
11376/* Make sure the memory operand we've been dealt is valid.
11377 Return 1 on success, 0 on a failure. */
11378
11379static int
11380i386_index_check (const char *operand_string)
11381{
11382 const char *kind = "base/index";
11383 enum flag_code addr_mode = i386_addressing_mode ();
77a19f0e 11384 const insn_template *t = current_templates->end - 1;
2abc2bec 11385
77a19f0e 11386 if (t->opcode_modifier.isstring)
fc0763e6
JB
11387 {
11388 /* Memory operands of string insns are special in that they only allow
11389 a single register (rDI, rSI, or rBX) as their memory address. */
be05d201
L
11390 const reg_entry *expected_reg;
11391 static const char *di_si[][2] =
11392 {
11393 { "esi", "edi" },
11394 { "si", "di" },
11395 { "rsi", "rdi" }
11396 };
11397 static const char *bx[] = { "ebx", "bx", "rbx" };
fc0763e6
JB
11398
11399 kind = "string address";
11400
a152332d 11401 if (t->opcode_modifier.prefixok == PrefixRep)
fc0763e6 11402 {
77a19f0e 11403 int es_op = t->opcode_modifier.isstring - IS_STRING_ES_OP0;
51c8edf6 11404 int op = 0;
fc0763e6 11405
77a19f0e 11406 if (!t->operand_types[0].bitfield.baseindex
fc0763e6 11407 || ((!i.mem_operands != !intel_syntax)
77a19f0e 11408 && t->operand_types[1].bitfield.baseindex))
51c8edf6 11409 op = 1;
fe0e921f
AM
11410 expected_reg
11411 = (const reg_entry *) str_hash_find (reg_hash,
11412 di_si[addr_mode][op == es_op]);
fc0763e6
JB
11413 }
11414 else
fe0e921f
AM
11415 expected_reg
11416 = (const reg_entry *)str_hash_find (reg_hash, bx[addr_mode]);
fc0763e6 11417
be05d201
L
11418 if (i.base_reg != expected_reg
11419 || i.index_reg
fc0763e6 11420 || operand_type_check (i.types[this_operand], disp))
fc0763e6 11421 {
be05d201
L
11422 /* The second memory operand must have the same size as
11423 the first one. */
11424 if (i.mem_operands
11425 && i.base_reg
11426 && !((addr_mode == CODE_64BIT
dc821c5f 11427 && i.base_reg->reg_type.bitfield.qword)
be05d201 11428 || (addr_mode == CODE_32BIT
dc821c5f
JB
11429 ? i.base_reg->reg_type.bitfield.dword
11430 : i.base_reg->reg_type.bitfield.word)))
be05d201
L
11431 goto bad_address;
11432
fc0763e6
JB
11433 as_warn (_("`%s' is not valid here (expected `%c%s%s%c')"),
11434 operand_string,
11435 intel_syntax ? '[' : '(',
11436 register_prefix,
be05d201 11437 expected_reg->reg_name,
fc0763e6 11438 intel_syntax ? ']' : ')');
be05d201 11439 return 1;
fc0763e6 11440 }
be05d201
L
11441 else
11442 return 1;
11443
dc1e8a47 11444 bad_address:
be05d201
L
11445 as_bad (_("`%s' is not a valid %s expression"),
11446 operand_string, kind);
11447 return 0;
3e73aa7c
JH
11448 }
11449 else
11450 {
77a19f0e
JB
11451 t = current_templates->start;
11452
be05d201
L
11453 if (addr_mode != CODE_16BIT)
11454 {
11455 /* 32-bit/64-bit checks. */
41eb8e88
L
11456 if (i.disp_encoding == disp_encoding_16bit)
11457 {
11458 bad_disp:
11459 as_bad (_("invalid `%s' prefix"),
11460 addr_mode == CODE_16BIT ? "{disp32}" : "{disp16}");
11461 return 0;
11462 }
11463
be05d201 11464 if ((i.base_reg
e968fc9b
JB
11465 && ((addr_mode == CODE_64BIT
11466 ? !i.base_reg->reg_type.bitfield.qword
11467 : !i.base_reg->reg_type.bitfield.dword)
11468 || (i.index_reg && i.base_reg->reg_num == RegIP)
11469 || i.base_reg->reg_num == RegIZ))
be05d201 11470 || (i.index_reg
1b54b8d7
JB
11471 && !i.index_reg->reg_type.bitfield.xmmword
11472 && !i.index_reg->reg_type.bitfield.ymmword
11473 && !i.index_reg->reg_type.bitfield.zmmword
be05d201 11474 && ((addr_mode == CODE_64BIT
e968fc9b
JB
11475 ? !i.index_reg->reg_type.bitfield.qword
11476 : !i.index_reg->reg_type.bitfield.dword)
be05d201
L
11477 || !i.index_reg->reg_type.bitfield.baseindex)))
11478 goto bad_address;
8178be5b 11479
260cd341 11480 /* bndmk, bndldx, bndstx and mandatory non-vector SIB have special restrictions. */
a152332d 11481 if ((t->opcode_modifier.opcodeprefix == PREFIX_0XF3
389d00a5
JB
11482 && t->opcode_modifier.opcodespace == SPACE_0F
11483 && t->base_opcode == 0x1b)
a152332d 11484 || (t->opcode_modifier.opcodeprefix == PREFIX_NONE
389d00a5
JB
11485 && t->opcode_modifier.opcodespace == SPACE_0F
11486 && (t->base_opcode & ~1) == 0x1a)
a152332d 11487 || t->opcode_modifier.sib == SIBMEM)
8178be5b
JB
11488 {
11489 /* They cannot use RIP-relative addressing. */
e968fc9b 11490 if (i.base_reg && i.base_reg->reg_num == RegIP)
8178be5b
JB
11491 {
11492 as_bad (_("`%s' cannot be used here"), operand_string);
11493 return 0;
11494 }
11495
11496 /* bndldx and bndstx ignore their scale factor. */
a152332d 11497 if (t->opcode_modifier.opcodeprefix == PREFIX_NONE
389d00a5
JB
11498 && t->opcode_modifier.opcodespace == SPACE_0F
11499 && (t->base_opcode & ~1) == 0x1a
8178be5b
JB
11500 && i.log2_scale_factor)
11501 as_warn (_("register scaling is being ignored here"));
11502 }
be05d201
L
11503 }
11504 else
3e73aa7c 11505 {
be05d201 11506 /* 16-bit checks. */
41eb8e88
L
11507 if (i.disp_encoding == disp_encoding_32bit)
11508 goto bad_disp;
11509
3e73aa7c 11510 if ((i.base_reg
dc821c5f 11511 && (!i.base_reg->reg_type.bitfield.word
40fb9820 11512 || !i.base_reg->reg_type.bitfield.baseindex))
3e73aa7c 11513 || (i.index_reg
dc821c5f 11514 && (!i.index_reg->reg_type.bitfield.word
40fb9820 11515 || !i.index_reg->reg_type.bitfield.baseindex
29b0f896
AM
11516 || !(i.base_reg
11517 && i.base_reg->reg_num < 6
11518 && i.index_reg->reg_num >= 6
11519 && i.log2_scale_factor == 0))))
be05d201 11520 goto bad_address;
3e73aa7c
JH
11521 }
11522 }
be05d201 11523 return 1;
24eab124 11524}
252b5132 11525
43234a1e
L
11526/* Handle vector immediates. */
11527
11528static int
11529RC_SAE_immediate (const char *imm_start)
11530{
43234a1e 11531 const char *pstr = imm_start;
43234a1e
L
11532
11533 if (*pstr != '{')
11534 return 0;
11535
7063667e
JB
11536 pstr = RC_SAE_specifier (pstr + 1);
11537 if (pstr == NULL)
43234a1e
L
11538 return 0;
11539
11540 if (*pstr++ != '}')
11541 {
11542 as_bad (_("Missing '}': '%s'"), imm_start);
11543 return 0;
11544 }
11545 /* RC/SAE immediate string should contain nothing more. */;
11546 if (*pstr != 0)
11547 {
11548 as_bad (_("Junk after '}': '%s'"), imm_start);
11549 return 0;
11550 }
11551
cf665fee
JB
11552 /* Internally this doesn't count as an operand. */
11553 --i.operands;
43234a1e 11554
43234a1e
L
11555 return 1;
11556}
11557
9d299bea
JB
11558static INLINE bool starts_memory_operand (char c)
11559{
014fbcda 11560 return ISDIGIT (c)
9d299bea 11561 || is_identifier_char (c)
014fbcda 11562 || strchr ("([\"+-!~", c);
9d299bea
JB
11563}
11564
fc0763e6 11565/* Parse OPERAND_STRING into the i386_insn structure I. Returns zero
47926f60 11566 on error. */
252b5132 11567
252b5132 11568static int
a7619375 11569i386_att_operand (char *operand_string)
252b5132 11570{
af6bdddf
AM
11571 const reg_entry *r;
11572 char *end_op;
24eab124 11573 char *op_string = operand_string;
252b5132 11574
24eab124 11575 if (is_space_char (*op_string))
252b5132
RH
11576 ++op_string;
11577
24eab124 11578 /* We check for an absolute prefix (differentiating,
47926f60 11579 for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
24eab124
AM
11580 if (*op_string == ABSOLUTE_PREFIX)
11581 {
11582 ++op_string;
11583 if (is_space_char (*op_string))
11584 ++op_string;
5b7c81bd 11585 i.jumpabsolute = true;
24eab124 11586 }
252b5132 11587
47926f60 11588 /* Check if operand is a register. */
4d1bb795 11589 if ((r = parse_register (op_string, &end_op)) != NULL)
24eab124 11590 {
40fb9820
L
11591 i386_operand_type temp;
11592
8a6fb3f9
JB
11593 if (r == &bad_reg)
11594 return 0;
11595
24eab124
AM
11596 /* Check for a segment override by searching for ':' after a
11597 segment register. */
11598 op_string = end_op;
11599 if (is_space_char (*op_string))
11600 ++op_string;
00cee14f 11601 if (*op_string == ':' && r->reg_type.bitfield.class == SReg)
24eab124 11602 {
5e042380 11603 i.seg[i.mem_operands] = r;
252b5132 11604
24eab124 11605 /* Skip the ':' and whitespace. */
252b5132
RH
11606 ++op_string;
11607 if (is_space_char (*op_string))
24eab124 11608 ++op_string;
252b5132 11609
47926f60 11610 /* Handle case of %es:*foo. */
c8d541e2 11611 if (!i.jumpabsolute && *op_string == ABSOLUTE_PREFIX)
24eab124
AM
11612 {
11613 ++op_string;
11614 if (is_space_char (*op_string))
11615 ++op_string;
5b7c81bd 11616 i.jumpabsolute = true;
24eab124 11617 }
c8d541e2 11618
9d299bea 11619 if (!starts_memory_operand (*op_string))
c8d541e2
JB
11620 {
11621 as_bad (_("bad memory operand `%s'"), op_string);
11622 return 0;
11623 }
24eab124
AM
11624 goto do_memory_reference;
11625 }
43234a1e
L
11626
11627 /* Handle vector operations. */
11628 if (*op_string == '{')
11629 {
f70c6814 11630 op_string = check_VecOperations (op_string);
43234a1e
L
11631 if (op_string == NULL)
11632 return 0;
11633 }
11634
24eab124
AM
11635 if (*op_string)
11636 {
d0b47220 11637 as_bad (_("junk `%s' after register"), op_string);
24eab124
AM
11638 return 0;
11639 }
40fb9820
L
11640 temp = r->reg_type;
11641 temp.bitfield.baseindex = 0;
c6fb90c8
L
11642 i.types[this_operand] = operand_type_or (i.types[this_operand],
11643 temp);
7d5e4556 11644 i.types[this_operand].bitfield.unspecified = 0;
520dc8e8 11645 i.op[this_operand].regs = r;
24eab124 11646 i.reg_operands++;
cf665fee
JB
11647
11648 /* A GPR may follow an RC or SAE immediate only if a (vector) register
11649 operand was also present earlier on. */
11650 if (i.rounding.type != rc_none && temp.bitfield.class == Reg
11651 && i.reg_operands == 1)
11652 {
11653 unsigned int j;
11654
11655 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); ++j)
11656 if (i.rounding.type == RC_NamesTable[j].type)
11657 break;
11658 as_bad (_("`%s': misplaced `{%s}'"),
11659 current_templates->start->name, RC_NamesTable[j].name);
11660 return 0;
11661 }
24eab124 11662 }
af6bdddf
AM
11663 else if (*op_string == REGISTER_PREFIX)
11664 {
11665 as_bad (_("bad register name `%s'"), op_string);
11666 return 0;
11667 }
24eab124 11668 else if (*op_string == IMMEDIATE_PREFIX)
ce8a8b2f 11669 {
24eab124 11670 ++op_string;
6f2f06be 11671 if (i.jumpabsolute)
24eab124 11672 {
d0b47220 11673 as_bad (_("immediate operand illegal with absolute jump"));
24eab124
AM
11674 return 0;
11675 }
11676 if (!i386_immediate (op_string))
11677 return 0;
cf665fee
JB
11678 if (i.rounding.type != rc_none)
11679 {
11680 as_bad (_("`%s': RC/SAE operand must follow immediate operands"),
11681 current_templates->start->name);
11682 return 0;
11683 }
24eab124 11684 }
43234a1e
L
11685 else if (RC_SAE_immediate (operand_string))
11686 {
cf665fee
JB
11687 /* If it is a RC or SAE immediate, do the necessary placement check:
11688 Only another immediate or a GPR may precede it. */
11689 if (i.mem_operands || i.reg_operands + i.imm_operands > 1
11690 || (i.reg_operands == 1
11691 && i.op[0].regs->reg_type.bitfield.class != Reg))
11692 {
11693 as_bad (_("`%s': misplaced `%s'"),
11694 current_templates->start->name, operand_string);
11695 return 0;
11696 }
43234a1e 11697 }
9d299bea 11698 else if (starts_memory_operand (*op_string))
24eab124 11699 {
47926f60 11700 /* This is a memory reference of some sort. */
af6bdddf 11701 char *base_string;
252b5132 11702
47926f60 11703 /* Start and end of displacement string expression (if found). */
eecb386c
AM
11704 char *displacement_string_start;
11705 char *displacement_string_end;
252b5132 11706
24eab124 11707 do_memory_reference:
24eab124
AM
11708 /* Check for base index form. We detect the base index form by
11709 looking for an ')' at the end of the operand, searching
11710 for the '(' matching it, and finding a REGISTER_PREFIX or ','
11711 after the '('. */
af6bdddf 11712 base_string = op_string + strlen (op_string);
c3332e24 11713
43234a1e 11714 /* Handle vector operations. */
6b5ba0d4
JB
11715 --base_string;
11716 if (is_space_char (*base_string))
11717 --base_string;
11718
11719 if (*base_string == '}')
43234a1e 11720 {
6b5ba0d4
JB
11721 char *vop_start = NULL;
11722
11723 while (base_string-- > op_string)
11724 {
11725 if (*base_string == '"')
11726 break;
11727 if (*base_string != '{')
11728 continue;
11729
11730 vop_start = base_string;
11731
11732 --base_string;
11733 if (is_space_char (*base_string))
11734 --base_string;
11735
11736 if (*base_string != '}')
11737 break;
11738
11739 vop_start = NULL;
11740 }
11741
11742 if (!vop_start)
11743 {
11744 as_bad (_("unbalanced figure braces"));
11745 return 0;
11746 }
11747
f70c6814 11748 if (check_VecOperations (vop_start) == NULL)
43234a1e 11749 return 0;
43234a1e
L
11750 }
11751
47926f60 11752 /* If we only have a displacement, set-up for it to be parsed later. */
af6bdddf
AM
11753 displacement_string_start = op_string;
11754 displacement_string_end = base_string + 1;
252b5132 11755
24eab124
AM
11756 if (*base_string == ')')
11757 {
af6bdddf 11758 char *temp_string;
e87fb6a6
JB
11759 unsigned int parens_not_balanced = 0;
11760 bool in_quotes = false;
e68c3d59 11761
24eab124 11762 /* We've already checked that the number of left & right ()'s are
e87fb6a6
JB
11763 equal, and that there's a matching set of double quotes. */
11764 end_op = base_string;
11765 for (temp_string = op_string; temp_string < end_op; temp_string++)
24eab124 11766 {
e87fb6a6
JB
11767 if (*temp_string == '\\' && temp_string[1] == '"')
11768 ++temp_string;
11769 else if (*temp_string == '"')
11770 in_quotes = !in_quotes;
11771 else if (!in_quotes)
11772 {
11773 if (*temp_string == '(' && !parens_not_balanced++)
11774 base_string = temp_string;
11775 if (*temp_string == ')')
11776 --parens_not_balanced;
11777 }
24eab124 11778 }
c3332e24 11779
af6bdddf 11780 temp_string = base_string;
c3332e24 11781
24eab124 11782 /* Skip past '(' and whitespace. */
e87fb6a6
JB
11783 gas_assert (*base_string == '(');
11784 ++base_string;
252b5132 11785 if (is_space_char (*base_string))
24eab124 11786 ++base_string;
252b5132 11787
af6bdddf 11788 if (*base_string == ','
4eed87de
AM
11789 || ((i.base_reg = parse_register (base_string, &end_op))
11790 != NULL))
252b5132 11791 {
af6bdddf 11792 displacement_string_end = temp_string;
252b5132 11793
40fb9820 11794 i.types[this_operand].bitfield.baseindex = 1;
252b5132 11795
af6bdddf 11796 if (i.base_reg)
24eab124 11797 {
8a6fb3f9
JB
11798 if (i.base_reg == &bad_reg)
11799 return 0;
24eab124
AM
11800 base_string = end_op;
11801 if (is_space_char (*base_string))
11802 ++base_string;
af6bdddf
AM
11803 }
11804
11805 /* There may be an index reg or scale factor here. */
11806 if (*base_string == ',')
11807 {
11808 ++base_string;
11809 if (is_space_char (*base_string))
11810 ++base_string;
11811
4eed87de
AM
11812 if ((i.index_reg = parse_register (base_string, &end_op))
11813 != NULL)
24eab124 11814 {
8a6fb3f9
JB
11815 if (i.index_reg == &bad_reg)
11816 return 0;
af6bdddf 11817 base_string = end_op;
24eab124
AM
11818 if (is_space_char (*base_string))
11819 ++base_string;
af6bdddf
AM
11820 if (*base_string == ',')
11821 {
11822 ++base_string;
11823 if (is_space_char (*base_string))
11824 ++base_string;
11825 }
e5cb08ac 11826 else if (*base_string != ')')
af6bdddf 11827 {
4eed87de
AM
11828 as_bad (_("expecting `,' or `)' "
11829 "after index register in `%s'"),
af6bdddf
AM
11830 operand_string);
11831 return 0;
11832 }
24eab124 11833 }
af6bdddf 11834 else if (*base_string == REGISTER_PREFIX)
24eab124 11835 {
f76bf5e0
L
11836 end_op = strchr (base_string, ',');
11837 if (end_op)
11838 *end_op = '\0';
af6bdddf 11839 as_bad (_("bad register name `%s'"), base_string);
24eab124
AM
11840 return 0;
11841 }
252b5132 11842
47926f60 11843 /* Check for scale factor. */
551c1ca1 11844 if (*base_string != ')')
af6bdddf 11845 {
551c1ca1
AM
11846 char *end_scale = i386_scale (base_string);
11847
11848 if (!end_scale)
af6bdddf 11849 return 0;
24eab124 11850
551c1ca1 11851 base_string = end_scale;
af6bdddf
AM
11852 if (is_space_char (*base_string))
11853 ++base_string;
11854 if (*base_string != ')')
11855 {
4eed87de
AM
11856 as_bad (_("expecting `)' "
11857 "after scale factor in `%s'"),
af6bdddf
AM
11858 operand_string);
11859 return 0;
11860 }
11861 }
11862 else if (!i.index_reg)
24eab124 11863 {
4eed87de
AM
11864 as_bad (_("expecting index register or scale factor "
11865 "after `,'; got '%c'"),
af6bdddf 11866 *base_string);
24eab124
AM
11867 return 0;
11868 }
11869 }
af6bdddf 11870 else if (*base_string != ')')
24eab124 11871 {
4eed87de
AM
11872 as_bad (_("expecting `,' or `)' "
11873 "after base register in `%s'"),
af6bdddf 11874 operand_string);
24eab124
AM
11875 return 0;
11876 }
c3332e24 11877 }
af6bdddf 11878 else if (*base_string == REGISTER_PREFIX)
c3332e24 11879 {
f76bf5e0
L
11880 end_op = strchr (base_string, ',');
11881 if (end_op)
11882 *end_op = '\0';
af6bdddf 11883 as_bad (_("bad register name `%s'"), base_string);
24eab124 11884 return 0;
c3332e24 11885 }
24eab124
AM
11886 }
11887
11888 /* If there's an expression beginning the operand, parse it,
11889 assuming displacement_string_start and
11890 displacement_string_end are meaningful. */
11891 if (displacement_string_start != displacement_string_end)
11892 {
11893 if (!i386_displacement (displacement_string_start,
11894 displacement_string_end))
11895 return 0;
11896 }
11897
11898 /* Special case for (%dx) while doing input/output op. */
11899 if (i.base_reg
75e5731b
JB
11900 && i.base_reg->reg_type.bitfield.instance == RegD
11901 && i.base_reg->reg_type.bitfield.word
24eab124
AM
11902 && i.index_reg == 0
11903 && i.log2_scale_factor == 0
11904 && i.seg[i.mem_operands] == 0
40fb9820 11905 && !operand_type_check (i.types[this_operand], disp))
24eab124 11906 {
2fb5be8d 11907 i.types[this_operand] = i.base_reg->reg_type;
9373f275 11908 i.input_output_operand = true;
24eab124
AM
11909 return 1;
11910 }
11911
eecb386c
AM
11912 if (i386_index_check (operand_string) == 0)
11913 return 0;
c48dadc9 11914 i.flags[this_operand] |= Operand_Mem;
24eab124
AM
11915 i.mem_operands++;
11916 }
11917 else
ce8a8b2f
AM
11918 {
11919 /* It's not a memory operand; argh! */
24eab124
AM
11920 as_bad (_("invalid char %s beginning operand %d `%s'"),
11921 output_invalid (*op_string),
11922 this_operand + 1,
11923 op_string);
11924 return 0;
11925 }
47926f60 11926 return 1; /* Normal return. */
252b5132
RH
11927}
11928\f
fa94de6b
RM
11929/* Calculate the maximum variable size (i.e., excluding fr_fix)
11930 that an rs_machine_dependent frag may reach. */
11931
11932unsigned int
11933i386_frag_max_var (fragS *frag)
11934{
11935 /* The only relaxable frags are for jumps.
11936 Unconditional jumps can grow by 4 bytes and others by 5 bytes. */
11937 gas_assert (frag->fr_type == rs_machine_dependent);
11938 return TYPE_FROM_RELAX_STATE (frag->fr_subtype) == UNCOND_JUMP ? 4 : 5;
11939}
11940
b084df0b
L
11941#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
11942static int
8dcea932 11943elf_symbol_resolved_in_segment_p (symbolS *fr_symbol, offsetT fr_var)
b084df0b
L
11944{
11945 /* STT_GNU_IFUNC symbol must go through PLT. */
11946 if ((symbol_get_bfdsym (fr_symbol)->flags
11947 & BSF_GNU_INDIRECT_FUNCTION) != 0)
11948 return 0;
11949
11950 if (!S_IS_EXTERNAL (fr_symbol))
11951 /* Symbol may be weak or local. */
11952 return !S_IS_WEAK (fr_symbol);
11953
8dcea932
L
11954 /* Global symbols with non-default visibility can't be preempted. */
11955 if (ELF_ST_VISIBILITY (S_GET_OTHER (fr_symbol)) != STV_DEFAULT)
11956 return 1;
11957
11958 if (fr_var != NO_RELOC)
11959 switch ((enum bfd_reloc_code_real) fr_var)
11960 {
11961 case BFD_RELOC_386_PLT32:
11962 case BFD_RELOC_X86_64_PLT32:
33eaf5de 11963 /* Symbol with PLT relocation may be preempted. */
8dcea932
L
11964 return 0;
11965 default:
11966 abort ();
11967 }
11968
b084df0b
L
11969 /* Global symbols with default visibility in a shared library may be
11970 preempted by another definition. */
8dcea932 11971 return !shared;
b084df0b
L
11972}
11973#endif
11974
79d72f45
HL
11975/* Table 3-2. Macro-Fusible Instructions in Haswell Microarchitecture
11976 Note also work for Skylake and Cascadelake.
11977---------------------------------------------------------------------
11978| JCC | ADD/SUB/CMP | INC/DEC | TEST/AND |
11979| ------ | ----------- | ------- | -------- |
11980| Jo | N | N | Y |
11981| Jno | N | N | Y |
11982| Jc/Jb | Y | N | Y |
11983| Jae/Jnb | Y | N | Y |
11984| Je/Jz | Y | Y | Y |
11985| Jne/Jnz | Y | Y | Y |
11986| Jna/Jbe | Y | N | Y |
11987| Ja/Jnbe | Y | N | Y |
11988| Js | N | N | Y |
11989| Jns | N | N | Y |
11990| Jp/Jpe | N | N | Y |
11991| Jnp/Jpo | N | N | Y |
11992| Jl/Jnge | Y | Y | Y |
11993| Jge/Jnl | Y | Y | Y |
11994| Jle/Jng | Y | Y | Y |
11995| Jg/Jnle | Y | Y | Y |
11996--------------------------------------------------------------------- */
11997static int
11998i386_macro_fusible_p (enum mf_cmp_kind mf_cmp, enum mf_jcc_kind mf_jcc)
11999{
12000 if (mf_cmp == mf_cmp_alu_cmp)
12001 return ((mf_jcc >= mf_jcc_jc && mf_jcc <= mf_jcc_jna)
12002 || mf_jcc == mf_jcc_jl || mf_jcc == mf_jcc_jle);
12003 if (mf_cmp == mf_cmp_incdec)
12004 return (mf_jcc == mf_jcc_je || mf_jcc == mf_jcc_jl
12005 || mf_jcc == mf_jcc_jle);
12006 if (mf_cmp == mf_cmp_test_and)
12007 return 1;
12008 return 0;
12009}
12010
e379e5f3
L
12011/* Return the next non-empty frag. */
12012
12013static fragS *
12014i386_next_non_empty_frag (fragS *fragP)
12015{
12016 /* There may be a frag with a ".fill 0" when there is no room in
12017 the current frag for frag_grow in output_insn. */
12018 for (fragP = fragP->fr_next;
12019 (fragP != NULL
12020 && fragP->fr_type == rs_fill
12021 && fragP->fr_fix == 0);
12022 fragP = fragP->fr_next)
12023 ;
12024 return fragP;
12025}
12026
12027/* Return the next jcc frag after BRANCH_PADDING. */
12028
12029static fragS *
79d72f45 12030i386_next_fusible_jcc_frag (fragS *maybe_cmp_fragP, fragS *pad_fragP)
e379e5f3 12031{
79d72f45
HL
12032 fragS *branch_fragP;
12033 if (!pad_fragP)
e379e5f3
L
12034 return NULL;
12035
79d72f45
HL
12036 if (pad_fragP->fr_type == rs_machine_dependent
12037 && (TYPE_FROM_RELAX_STATE (pad_fragP->fr_subtype)
e379e5f3
L
12038 == BRANCH_PADDING))
12039 {
79d72f45
HL
12040 branch_fragP = i386_next_non_empty_frag (pad_fragP);
12041 if (branch_fragP->fr_type != rs_machine_dependent)
e379e5f3 12042 return NULL;
79d72f45
HL
12043 if (TYPE_FROM_RELAX_STATE (branch_fragP->fr_subtype) == COND_JUMP
12044 && i386_macro_fusible_p (maybe_cmp_fragP->tc_frag_data.mf_type,
12045 pad_fragP->tc_frag_data.mf_type))
12046 return branch_fragP;
e379e5f3
L
12047 }
12048
12049 return NULL;
12050}
12051
12052/* Classify BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags. */
12053
12054static void
12055i386_classify_machine_dependent_frag (fragS *fragP)
12056{
12057 fragS *cmp_fragP;
12058 fragS *pad_fragP;
12059 fragS *branch_fragP;
12060 fragS *next_fragP;
12061 unsigned int max_prefix_length;
12062
12063 if (fragP->tc_frag_data.classified)
12064 return;
12065
12066 /* First scan for BRANCH_PADDING and FUSED_JCC_PADDING. Convert
12067 FUSED_JCC_PADDING and merge BRANCH_PADDING. */
12068 for (next_fragP = fragP;
12069 next_fragP != NULL;
12070 next_fragP = next_fragP->fr_next)
12071 {
12072 next_fragP->tc_frag_data.classified = 1;
12073 if (next_fragP->fr_type == rs_machine_dependent)
12074 switch (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype))
12075 {
12076 case BRANCH_PADDING:
12077 /* The BRANCH_PADDING frag must be followed by a branch
12078 frag. */
12079 branch_fragP = i386_next_non_empty_frag (next_fragP);
12080 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
12081 break;
12082 case FUSED_JCC_PADDING:
12083 /* Check if this is a fused jcc:
12084 FUSED_JCC_PADDING
12085 CMP like instruction
12086 BRANCH_PADDING
12087 COND_JUMP
12088 */
12089 cmp_fragP = i386_next_non_empty_frag (next_fragP);
12090 pad_fragP = i386_next_non_empty_frag (cmp_fragP);
79d72f45 12091 branch_fragP = i386_next_fusible_jcc_frag (next_fragP, pad_fragP);
e379e5f3
L
12092 if (branch_fragP)
12093 {
12094 /* The BRANCH_PADDING frag is merged with the
12095 FUSED_JCC_PADDING frag. */
12096 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
12097 /* CMP like instruction size. */
12098 next_fragP->tc_frag_data.cmp_size = cmp_fragP->fr_fix;
12099 frag_wane (pad_fragP);
12100 /* Skip to branch_fragP. */
12101 next_fragP = branch_fragP;
12102 }
12103 else if (next_fragP->tc_frag_data.max_prefix_length)
12104 {
12105 /* Turn FUSED_JCC_PADDING into BRANCH_PREFIX if it isn't
12106 a fused jcc. */
12107 next_fragP->fr_subtype
12108 = ENCODE_RELAX_STATE (BRANCH_PREFIX, 0);
12109 next_fragP->tc_frag_data.max_bytes
12110 = next_fragP->tc_frag_data.max_prefix_length;
12111 /* This will be updated in the BRANCH_PREFIX scan. */
12112 next_fragP->tc_frag_data.max_prefix_length = 0;
12113 }
12114 else
12115 frag_wane (next_fragP);
12116 break;
12117 }
12118 }
12119
12120 /* Stop if there is no BRANCH_PREFIX. */
12121 if (!align_branch_prefix_size)
12122 return;
12123
12124 /* Scan for BRANCH_PREFIX. */
12125 for (; fragP != NULL; fragP = fragP->fr_next)
12126 {
12127 if (fragP->fr_type != rs_machine_dependent
12128 || (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
12129 != BRANCH_PREFIX))
12130 continue;
12131
12132 /* Count all BRANCH_PREFIX frags before BRANCH_PADDING and
12133 COND_JUMP_PREFIX. */
12134 max_prefix_length = 0;
12135 for (next_fragP = fragP;
12136 next_fragP != NULL;
12137 next_fragP = next_fragP->fr_next)
12138 {
12139 if (next_fragP->fr_type == rs_fill)
12140 /* Skip rs_fill frags. */
12141 continue;
12142 else if (next_fragP->fr_type != rs_machine_dependent)
12143 /* Stop for all other frags. */
12144 break;
12145
12146 /* rs_machine_dependent frags. */
12147 if (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12148 == BRANCH_PREFIX)
12149 {
12150 /* Count BRANCH_PREFIX frags. */
12151 if (max_prefix_length >= MAX_FUSED_JCC_PADDING_SIZE)
12152 {
12153 max_prefix_length = MAX_FUSED_JCC_PADDING_SIZE;
12154 frag_wane (next_fragP);
12155 }
12156 else
12157 max_prefix_length
12158 += next_fragP->tc_frag_data.max_bytes;
12159 }
12160 else if ((TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12161 == BRANCH_PADDING)
12162 || (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12163 == FUSED_JCC_PADDING))
12164 {
12165 /* Stop at BRANCH_PADDING and FUSED_JCC_PADDING. */
12166 fragP->tc_frag_data.u.padding_fragP = next_fragP;
12167 break;
12168 }
12169 else
12170 /* Stop for other rs_machine_dependent frags. */
12171 break;
12172 }
12173
12174 fragP->tc_frag_data.max_prefix_length = max_prefix_length;
12175
12176 /* Skip to the next frag. */
12177 fragP = next_fragP;
12178 }
12179}
12180
12181/* Compute padding size for
12182
12183 FUSED_JCC_PADDING
12184 CMP like instruction
12185 BRANCH_PADDING
12186 COND_JUMP/UNCOND_JUMP
12187
12188 or
12189
12190 BRANCH_PADDING
12191 COND_JUMP/UNCOND_JUMP
12192 */
12193
12194static int
12195i386_branch_padding_size (fragS *fragP, offsetT address)
12196{
12197 unsigned int offset, size, padding_size;
12198 fragS *branch_fragP = fragP->tc_frag_data.u.branch_fragP;
12199
12200 /* The start address of the BRANCH_PADDING or FUSED_JCC_PADDING frag. */
12201 if (!address)
12202 address = fragP->fr_address;
12203 address += fragP->fr_fix;
12204
12205 /* CMP like instrunction size. */
12206 size = fragP->tc_frag_data.cmp_size;
12207
12208 /* The base size of the branch frag. */
12209 size += branch_fragP->fr_fix;
12210
12211 /* Add opcode and displacement bytes for the rs_machine_dependent
12212 branch frag. */
12213 if (branch_fragP->fr_type == rs_machine_dependent)
12214 size += md_relax_table[branch_fragP->fr_subtype].rlx_length;
12215
12216 /* Check if branch is within boundary and doesn't end at the last
12217 byte. */
12218 offset = address & ((1U << align_branch_power) - 1);
12219 if ((offset + size) >= (1U << align_branch_power))
12220 /* Padding needed to avoid crossing boundary. */
12221 padding_size = (1U << align_branch_power) - offset;
12222 else
12223 /* No padding needed. */
12224 padding_size = 0;
12225
12226 /* The return value may be saved in tc_frag_data.length which is
12227 unsigned byte. */
12228 if (!fits_in_unsigned_byte (padding_size))
12229 abort ();
12230
12231 return padding_size;
12232}
12233
12234/* i386_generic_table_relax_frag()
12235
12236 Handle BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags to
12237 grow/shrink padding to align branch frags. Hand others to
12238 relax_frag(). */
12239
12240long
12241i386_generic_table_relax_frag (segT segment, fragS *fragP, long stretch)
12242{
12243 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12244 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
12245 {
12246 long padding_size = i386_branch_padding_size (fragP, 0);
12247 long grow = padding_size - fragP->tc_frag_data.length;
12248
12249 /* When the BRANCH_PREFIX frag is used, the computed address
12250 must match the actual address and there should be no padding. */
12251 if (fragP->tc_frag_data.padding_address
12252 && (fragP->tc_frag_data.padding_address != fragP->fr_address
12253 || padding_size))
12254 abort ();
12255
12256 /* Update the padding size. */
12257 if (grow)
12258 fragP->tc_frag_data.length = padding_size;
12259
12260 return grow;
12261 }
12262 else if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12263 {
12264 fragS *padding_fragP, *next_fragP;
12265 long padding_size, left_size, last_size;
12266
12267 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
12268 if (!padding_fragP)
12269 /* Use the padding set by the leading BRANCH_PREFIX frag. */
12270 return (fragP->tc_frag_data.length
12271 - fragP->tc_frag_data.last_length);
12272
12273 /* Compute the relative address of the padding frag in the very
12274 first time where the BRANCH_PREFIX frag sizes are zero. */
12275 if (!fragP->tc_frag_data.padding_address)
12276 fragP->tc_frag_data.padding_address
12277 = padding_fragP->fr_address - (fragP->fr_address - stretch);
12278
12279 /* First update the last length from the previous interation. */
12280 left_size = fragP->tc_frag_data.prefix_length;
12281 for (next_fragP = fragP;
12282 next_fragP != padding_fragP;
12283 next_fragP = next_fragP->fr_next)
12284 if (next_fragP->fr_type == rs_machine_dependent
12285 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12286 == BRANCH_PREFIX))
12287 {
12288 if (left_size)
12289 {
12290 int max = next_fragP->tc_frag_data.max_bytes;
12291 if (max)
12292 {
12293 int size;
12294 if (max > left_size)
12295 size = left_size;
12296 else
12297 size = max;
12298 left_size -= size;
12299 next_fragP->tc_frag_data.last_length = size;
12300 }
12301 }
12302 else
12303 next_fragP->tc_frag_data.last_length = 0;
12304 }
12305
12306 /* Check the padding size for the padding frag. */
12307 padding_size = i386_branch_padding_size
12308 (padding_fragP, (fragP->fr_address
12309 + fragP->tc_frag_data.padding_address));
12310
12311 last_size = fragP->tc_frag_data.prefix_length;
12312 /* Check if there is change from the last interation. */
12313 if (padding_size == last_size)
12314 {
12315 /* Update the expected address of the padding frag. */
12316 padding_fragP->tc_frag_data.padding_address
12317 = (fragP->fr_address + padding_size
12318 + fragP->tc_frag_data.padding_address);
12319 return 0;
12320 }
12321
12322 if (padding_size > fragP->tc_frag_data.max_prefix_length)
12323 {
12324 /* No padding if there is no sufficient room. Clear the
12325 expected address of the padding frag. */
12326 padding_fragP->tc_frag_data.padding_address = 0;
12327 padding_size = 0;
12328 }
12329 else
12330 /* Store the expected address of the padding frag. */
12331 padding_fragP->tc_frag_data.padding_address
12332 = (fragP->fr_address + padding_size
12333 + fragP->tc_frag_data.padding_address);
12334
12335 fragP->tc_frag_data.prefix_length = padding_size;
12336
12337 /* Update the length for the current interation. */
12338 left_size = padding_size;
12339 for (next_fragP = fragP;
12340 next_fragP != padding_fragP;
12341 next_fragP = next_fragP->fr_next)
12342 if (next_fragP->fr_type == rs_machine_dependent
12343 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12344 == BRANCH_PREFIX))
12345 {
12346 if (left_size)
12347 {
12348 int max = next_fragP->tc_frag_data.max_bytes;
12349 if (max)
12350 {
12351 int size;
12352 if (max > left_size)
12353 size = left_size;
12354 else
12355 size = max;
12356 left_size -= size;
12357 next_fragP->tc_frag_data.length = size;
12358 }
12359 }
12360 else
12361 next_fragP->tc_frag_data.length = 0;
12362 }
12363
12364 return (fragP->tc_frag_data.length
12365 - fragP->tc_frag_data.last_length);
12366 }
12367 return relax_frag (segment, fragP, stretch);
12368}
12369
ee7fcc42
AM
12370/* md_estimate_size_before_relax()
12371
12372 Called just before relax() for rs_machine_dependent frags. The x86
12373 assembler uses these frags to handle variable size jump
12374 instructions.
12375
12376 Any symbol that is now undefined will not become defined.
12377 Return the correct fr_subtype in the frag.
12378 Return the initial "guess for variable size of frag" to caller.
12379 The guess is actually the growth beyond the fixed part. Whatever
12380 we do to grow the fixed or variable part contributes to our
12381 returned value. */
12382
252b5132 12383int
7016a5d5 12384md_estimate_size_before_relax (fragS *fragP, segT segment)
252b5132 12385{
e379e5f3
L
12386 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12387 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX
12388 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
12389 {
12390 i386_classify_machine_dependent_frag (fragP);
12391 return fragP->tc_frag_data.length;
12392 }
12393
252b5132 12394 /* We've already got fragP->fr_subtype right; all we have to do is
b98ef147
AM
12395 check for un-relaxable symbols. On an ELF system, we can't relax
12396 an externally visible symbol, because it may be overridden by a
12397 shared library. */
12398 if (S_GET_SEGMENT (fragP->fr_symbol) != segment
6d249963 12399#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12400 || (IS_ELF
8dcea932
L
12401 && !elf_symbol_resolved_in_segment_p (fragP->fr_symbol,
12402 fragP->fr_var))
fbeb56a4
DK
12403#endif
12404#if defined (OBJ_COFF) && defined (TE_PE)
7ab9ffdd 12405 || (OUTPUT_FLAVOR == bfd_target_coff_flavour
fbeb56a4 12406 && S_IS_WEAK (fragP->fr_symbol))
b98ef147
AM
12407#endif
12408 )
252b5132 12409 {
b98ef147
AM
12410 /* Symbol is undefined in this segment, or we need to keep a
12411 reloc so that weak symbols can be overridden. */
12412 int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
f86103b7 12413 enum bfd_reloc_code_real reloc_type;
ee7fcc42
AM
12414 unsigned char *opcode;
12415 int old_fr_fix;
eb19308f 12416 fixS *fixP = NULL;
f6af82bd 12417
ee7fcc42 12418 if (fragP->fr_var != NO_RELOC)
1e9cc1c2 12419 reloc_type = (enum bfd_reloc_code_real) fragP->fr_var;
b98ef147 12420 else if (size == 2)
f6af82bd 12421 reloc_type = BFD_RELOC_16_PCREL;
bd7ab16b 12422#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1ef3994a
JB
12423 else if (fragP->tc_frag_data.code64 && fragP->fr_offset == 0
12424 && need_plt32_p (fragP->fr_symbol))
bd7ab16b
L
12425 reloc_type = BFD_RELOC_X86_64_PLT32;
12426#endif
f6af82bd
AM
12427 else
12428 reloc_type = BFD_RELOC_32_PCREL;
252b5132 12429
ee7fcc42
AM
12430 old_fr_fix = fragP->fr_fix;
12431 opcode = (unsigned char *) fragP->fr_opcode;
12432
fddf5b5b 12433 switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
252b5132 12434 {
fddf5b5b
AM
12435 case UNCOND_JUMP:
12436 /* Make jmp (0xeb) a (d)word displacement jump. */
47926f60 12437 opcode[0] = 0xe9;
252b5132 12438 fragP->fr_fix += size;
eb19308f
JB
12439 fixP = fix_new (fragP, old_fr_fix, size,
12440 fragP->fr_symbol,
12441 fragP->fr_offset, 1,
12442 reloc_type);
252b5132
RH
12443 break;
12444
fddf5b5b 12445 case COND_JUMP86:
412167cb
AM
12446 if (size == 2
12447 && (!no_cond_jump_promotion || fragP->fr_var != NO_RELOC))
fddf5b5b
AM
12448 {
12449 /* Negate the condition, and branch past an
12450 unconditional jump. */
12451 opcode[0] ^= 1;
12452 opcode[1] = 3;
12453 /* Insert an unconditional jump. */
12454 opcode[2] = 0xe9;
12455 /* We added two extra opcode bytes, and have a two byte
12456 offset. */
12457 fragP->fr_fix += 2 + 2;
062cd5e7
AS
12458 fix_new (fragP, old_fr_fix + 2, 2,
12459 fragP->fr_symbol,
12460 fragP->fr_offset, 1,
12461 reloc_type);
fddf5b5b
AM
12462 break;
12463 }
12464 /* Fall through. */
12465
12466 case COND_JUMP:
412167cb
AM
12467 if (no_cond_jump_promotion && fragP->fr_var == NO_RELOC)
12468 {
12469 fragP->fr_fix += 1;
3e02c1cc
AM
12470 fixP = fix_new (fragP, old_fr_fix, 1,
12471 fragP->fr_symbol,
12472 fragP->fr_offset, 1,
12473 BFD_RELOC_8_PCREL);
12474 fixP->fx_signed = 1;
412167cb
AM
12475 break;
12476 }
93c2a809 12477
24eab124 12478 /* This changes the byte-displacement jump 0x7N
fddf5b5b 12479 to the (d)word-displacement jump 0x0f,0x8N. */
252b5132 12480 opcode[1] = opcode[0] + 0x10;
f6af82bd 12481 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
47926f60
KH
12482 /* We've added an opcode byte. */
12483 fragP->fr_fix += 1 + size;
eb19308f
JB
12484 fixP = fix_new (fragP, old_fr_fix + 1, size,
12485 fragP->fr_symbol,
12486 fragP->fr_offset, 1,
12487 reloc_type);
252b5132 12488 break;
fddf5b5b
AM
12489
12490 default:
12491 BAD_CASE (fragP->fr_subtype);
12492 break;
252b5132 12493 }
eb19308f
JB
12494
12495 /* All jumps handled here are signed, but don't unconditionally use a
12496 signed limit check for 32 and 16 bit jumps as we want to allow wrap
12497 around at 4G (outside of 64-bit mode) and 64k. */
12498 if (size == 4 && flag_code == CODE_64BIT)
12499 fixP->fx_signed = 1;
12500
252b5132 12501 frag_wane (fragP);
ee7fcc42 12502 return fragP->fr_fix - old_fr_fix;
252b5132 12503 }
93c2a809 12504
93c2a809
AM
12505 /* Guess size depending on current relax state. Initially the relax
12506 state will correspond to a short jump and we return 1, because
12507 the variable part of the frag (the branch offset) is one byte
12508 long. However, we can relax a section more than once and in that
12509 case we must either set fr_subtype back to the unrelaxed state,
12510 or return the value for the appropriate branch. */
12511 return md_relax_table[fragP->fr_subtype].rlx_length;
ee7fcc42
AM
12512}
12513
47926f60
KH
12514/* Called after relax() is finished.
12515
12516 In: Address of frag.
12517 fr_type == rs_machine_dependent.
12518 fr_subtype is what the address relaxed to.
12519
12520 Out: Any fixSs and constants are set up.
12521 Caller will turn frag into a ".space 0". */
12522
252b5132 12523void
7016a5d5
TG
12524md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec ATTRIBUTE_UNUSED,
12525 fragS *fragP)
252b5132 12526{
29b0f896 12527 unsigned char *opcode;
252b5132 12528 unsigned char *where_to_put_displacement = NULL;
847f7ad4
AM
12529 offsetT target_address;
12530 offsetT opcode_address;
252b5132 12531 unsigned int extension = 0;
847f7ad4 12532 offsetT displacement_from_opcode_start;
252b5132 12533
e379e5f3
L
12534 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12535 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING
12536 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12537 {
12538 /* Generate nop padding. */
12539 unsigned int size = fragP->tc_frag_data.length;
12540 if (size)
12541 {
12542 if (size > fragP->tc_frag_data.max_bytes)
12543 abort ();
12544
12545 if (flag_debug)
12546 {
12547 const char *msg;
12548 const char *branch = "branch";
12549 const char *prefix = "";
12550 fragS *padding_fragP;
12551 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
12552 == BRANCH_PREFIX)
12553 {
12554 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
12555 switch (fragP->tc_frag_data.default_prefix)
12556 {
12557 default:
12558 abort ();
12559 break;
12560 case CS_PREFIX_OPCODE:
12561 prefix = " cs";
12562 break;
12563 case DS_PREFIX_OPCODE:
12564 prefix = " ds";
12565 break;
12566 case ES_PREFIX_OPCODE:
12567 prefix = " es";
12568 break;
12569 case FS_PREFIX_OPCODE:
12570 prefix = " fs";
12571 break;
12572 case GS_PREFIX_OPCODE:
12573 prefix = " gs";
12574 break;
12575 case SS_PREFIX_OPCODE:
12576 prefix = " ss";
12577 break;
12578 }
12579 if (padding_fragP)
12580 msg = _("%s:%u: add %d%s at 0x%llx to align "
12581 "%s within %d-byte boundary\n");
12582 else
12583 msg = _("%s:%u: add additional %d%s at 0x%llx to "
12584 "align %s within %d-byte boundary\n");
12585 }
12586 else
12587 {
12588 padding_fragP = fragP;
12589 msg = _("%s:%u: add %d%s-byte nop at 0x%llx to align "
12590 "%s within %d-byte boundary\n");
12591 }
12592
12593 if (padding_fragP)
12594 switch (padding_fragP->tc_frag_data.branch_type)
12595 {
12596 case align_branch_jcc:
12597 branch = "jcc";
12598 break;
12599 case align_branch_fused:
12600 branch = "fused jcc";
12601 break;
12602 case align_branch_jmp:
12603 branch = "jmp";
12604 break;
12605 case align_branch_call:
12606 branch = "call";
12607 break;
12608 case align_branch_indirect:
12609 branch = "indiret branch";
12610 break;
12611 case align_branch_ret:
12612 branch = "ret";
12613 break;
12614 default:
12615 break;
12616 }
12617
12618 fprintf (stdout, msg,
12619 fragP->fr_file, fragP->fr_line, size, prefix,
12620 (long long) fragP->fr_address, branch,
12621 1 << align_branch_power);
12622 }
12623 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12624 memset (fragP->fr_opcode,
12625 fragP->tc_frag_data.default_prefix, size);
12626 else
12627 i386_generate_nops (fragP, (char *) fragP->fr_opcode,
12628 size, 0);
12629 fragP->fr_fix += size;
12630 }
12631 return;
12632 }
12633
252b5132
RH
12634 opcode = (unsigned char *) fragP->fr_opcode;
12635
47926f60 12636 /* Address we want to reach in file space. */
252b5132 12637 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
252b5132 12638
47926f60 12639 /* Address opcode resides at in file space. */
252b5132
RH
12640 opcode_address = fragP->fr_address + fragP->fr_fix;
12641
47926f60 12642 /* Displacement from opcode start to fill into instruction. */
252b5132
RH
12643 displacement_from_opcode_start = target_address - opcode_address;
12644
fddf5b5b 12645 if ((fragP->fr_subtype & BIG) == 0)
252b5132 12646 {
47926f60
KH
12647 /* Don't have to change opcode. */
12648 extension = 1; /* 1 opcode + 1 displacement */
252b5132 12649 where_to_put_displacement = &opcode[1];
fddf5b5b
AM
12650 }
12651 else
12652 {
12653 if (no_cond_jump_promotion
12654 && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
4eed87de
AM
12655 as_warn_where (fragP->fr_file, fragP->fr_line,
12656 _("long jump required"));
252b5132 12657
fddf5b5b
AM
12658 switch (fragP->fr_subtype)
12659 {
12660 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
12661 extension = 4; /* 1 opcode + 4 displacement */
12662 opcode[0] = 0xe9;
12663 where_to_put_displacement = &opcode[1];
12664 break;
252b5132 12665
fddf5b5b
AM
12666 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
12667 extension = 2; /* 1 opcode + 2 displacement */
12668 opcode[0] = 0xe9;
12669 where_to_put_displacement = &opcode[1];
12670 break;
252b5132 12671
fddf5b5b
AM
12672 case ENCODE_RELAX_STATE (COND_JUMP, BIG):
12673 case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
12674 extension = 5; /* 2 opcode + 4 displacement */
12675 opcode[1] = opcode[0] + 0x10;
12676 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12677 where_to_put_displacement = &opcode[2];
12678 break;
252b5132 12679
fddf5b5b
AM
12680 case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
12681 extension = 3; /* 2 opcode + 2 displacement */
12682 opcode[1] = opcode[0] + 0x10;
12683 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12684 where_to_put_displacement = &opcode[2];
12685 break;
252b5132 12686
fddf5b5b
AM
12687 case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
12688 extension = 4;
12689 opcode[0] ^= 1;
12690 opcode[1] = 3;
12691 opcode[2] = 0xe9;
12692 where_to_put_displacement = &opcode[3];
12693 break;
12694
12695 default:
12696 BAD_CASE (fragP->fr_subtype);
12697 break;
12698 }
252b5132 12699 }
fddf5b5b 12700
7b81dfbb
AJ
12701 /* If size if less then four we are sure that the operand fits,
12702 but if it's 4, then it could be that the displacement is larger
12703 then -/+ 2GB. */
12704 if (DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype) == 4
12705 && object_64bit
12706 && ((addressT) (displacement_from_opcode_start - extension
4eed87de
AM
12707 + ((addressT) 1 << 31))
12708 > (((addressT) 2 << 31) - 1)))
7b81dfbb
AJ
12709 {
12710 as_bad_where (fragP->fr_file, fragP->fr_line,
12711 _("jump target out of range"));
12712 /* Make us emit 0. */
12713 displacement_from_opcode_start = extension;
12714 }
47926f60 12715 /* Now put displacement after opcode. */
252b5132
RH
12716 md_number_to_chars ((char *) where_to_put_displacement,
12717 (valueT) (displacement_from_opcode_start - extension),
fddf5b5b 12718 DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
252b5132
RH
12719 fragP->fr_fix += extension;
12720}
12721\f
7016a5d5 12722/* Apply a fixup (fixP) to segment data, once it has been determined
252b5132
RH
12723 by our caller that we have all the info we need to fix it up.
12724
7016a5d5
TG
12725 Parameter valP is the pointer to the value of the bits.
12726
252b5132
RH
12727 On the 386, immediates, displacements, and data pointers are all in
12728 the same (little-endian) format, so we don't need to care about which
12729 we are handling. */
12730
94f592af 12731void
7016a5d5 12732md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 12733{
94f592af 12734 char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
c6682705 12735 valueT value = *valP;
252b5132 12736
f86103b7 12737#if !defined (TE_Mach)
93382f6d
AM
12738 if (fixP->fx_pcrel)
12739 {
12740 switch (fixP->fx_r_type)
12741 {
5865bb77
ILT
12742 default:
12743 break;
12744
d6ab8113
JB
12745 case BFD_RELOC_64:
12746 fixP->fx_r_type = BFD_RELOC_64_PCREL;
12747 break;
93382f6d 12748 case BFD_RELOC_32:
ae8887b5 12749 case BFD_RELOC_X86_64_32S:
93382f6d
AM
12750 fixP->fx_r_type = BFD_RELOC_32_PCREL;
12751 break;
12752 case BFD_RELOC_16:
12753 fixP->fx_r_type = BFD_RELOC_16_PCREL;
12754 break;
12755 case BFD_RELOC_8:
12756 fixP->fx_r_type = BFD_RELOC_8_PCREL;
12757 break;
12758 }
12759 }
252b5132 12760
a161fe53 12761 if (fixP->fx_addsy != NULL
31312f95 12762 && (fixP->fx_r_type == BFD_RELOC_32_PCREL
d6ab8113 12763 || fixP->fx_r_type == BFD_RELOC_64_PCREL
31312f95 12764 || fixP->fx_r_type == BFD_RELOC_16_PCREL
d258b828 12765 || fixP->fx_r_type == BFD_RELOC_8_PCREL)
31312f95 12766 && !use_rela_relocations)
252b5132 12767 {
31312f95
AM
12768 /* This is a hack. There should be a better way to handle this.
12769 This covers for the fact that bfd_install_relocation will
12770 subtract the current location (for partial_inplace, PC relative
12771 relocations); see more below. */
252b5132 12772#ifndef OBJ_AOUT
718ddfc0 12773 if (IS_ELF
252b5132
RH
12774#ifdef TE_PE
12775 || OUTPUT_FLAVOR == bfd_target_coff_flavour
12776#endif
12777 )
12778 value += fixP->fx_where + fixP->fx_frag->fr_address;
12779#endif
12780#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12781 if (IS_ELF)
252b5132 12782 {
6539b54b 12783 segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
2f66722d 12784
6539b54b 12785 if ((sym_seg == seg
2f66722d 12786 || (symbol_section_p (fixP->fx_addsy)
6539b54b 12787 && sym_seg != absolute_section))
af65af87 12788 && !generic_force_reloc (fixP))
2f66722d
AM
12789 {
12790 /* Yes, we add the values in twice. This is because
6539b54b
AM
12791 bfd_install_relocation subtracts them out again. I think
12792 bfd_install_relocation is broken, but I don't dare change
2f66722d
AM
12793 it. FIXME. */
12794 value += fixP->fx_where + fixP->fx_frag->fr_address;
12795 }
252b5132
RH
12796 }
12797#endif
12798#if defined (OBJ_COFF) && defined (TE_PE)
977cdf5a
NC
12799 /* For some reason, the PE format does not store a
12800 section address offset for a PC relative symbol. */
12801 if (S_GET_SEGMENT (fixP->fx_addsy) != seg
7be1c489 12802 || S_IS_WEAK (fixP->fx_addsy))
252b5132
RH
12803 value += md_pcrel_from (fixP);
12804#endif
12805 }
fbeb56a4 12806#if defined (OBJ_COFF) && defined (TE_PE)
f01c1a09
NC
12807 if (fixP->fx_addsy != NULL
12808 && S_IS_WEAK (fixP->fx_addsy)
12809 /* PR 16858: Do not modify weak function references. */
12810 && ! fixP->fx_pcrel)
fbeb56a4 12811 {
296a8689
NC
12812#if !defined (TE_PEP)
12813 /* For x86 PE weak function symbols are neither PC-relative
12814 nor do they set S_IS_FUNCTION. So the only reliable way
12815 to detect them is to check the flags of their containing
12816 section. */
12817 if (S_GET_SEGMENT (fixP->fx_addsy) != NULL
12818 && S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_CODE)
12819 ;
12820 else
12821#endif
fbeb56a4
DK
12822 value -= S_GET_VALUE (fixP->fx_addsy);
12823 }
12824#endif
252b5132
RH
12825
12826 /* Fix a few things - the dynamic linker expects certain values here,
0234cb7c 12827 and we must not disappoint it. */
252b5132 12828#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12829 if (IS_ELF && fixP->fx_addsy)
47926f60
KH
12830 switch (fixP->fx_r_type)
12831 {
12832 case BFD_RELOC_386_PLT32:
3e73aa7c 12833 case BFD_RELOC_X86_64_PLT32:
b9519cfe
L
12834 /* Make the jump instruction point to the address of the operand.
12835 At runtime we merely add the offset to the actual PLT entry.
12836 NB: Subtract the offset size only for jump instructions. */
12837 if (fixP->fx_pcrel)
12838 value = -4;
47926f60 12839 break;
31312f95 12840
13ae64f3
JJ
12841 case BFD_RELOC_386_TLS_GD:
12842 case BFD_RELOC_386_TLS_LDM:
13ae64f3 12843 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
12844 case BFD_RELOC_386_TLS_IE:
12845 case BFD_RELOC_386_TLS_GOTIE:
67a4f2b7 12846 case BFD_RELOC_386_TLS_GOTDESC:
bffbf940
JJ
12847 case BFD_RELOC_X86_64_TLSGD:
12848 case BFD_RELOC_X86_64_TLSLD:
12849 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7 12850 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
00f7efb6
JJ
12851 value = 0; /* Fully resolved at runtime. No addend. */
12852 /* Fallthrough */
12853 case BFD_RELOC_386_TLS_LE:
12854 case BFD_RELOC_386_TLS_LDO_32:
12855 case BFD_RELOC_386_TLS_LE_32:
12856 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 12857 case BFD_RELOC_X86_64_DTPOFF64:
00f7efb6 12858 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113 12859 case BFD_RELOC_X86_64_TPOFF64:
00f7efb6
JJ
12860 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12861 break;
12862
67a4f2b7
AO
12863 case BFD_RELOC_386_TLS_DESC_CALL:
12864 case BFD_RELOC_X86_64_TLSDESC_CALL:
12865 value = 0; /* Fully resolved at runtime. No addend. */
12866 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12867 fixP->fx_done = 0;
12868 return;
12869
47926f60
KH
12870 case BFD_RELOC_VTABLE_INHERIT:
12871 case BFD_RELOC_VTABLE_ENTRY:
12872 fixP->fx_done = 0;
94f592af 12873 return;
47926f60
KH
12874
12875 default:
12876 break;
12877 }
12878#endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) */
a442cac5
JB
12879
12880 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
12881 if (!object_64bit)
12882 value = extend_to_32bit_address (value);
12883
c6682705 12884 *valP = value;
f86103b7 12885#endif /* !defined (TE_Mach) */
3e73aa7c 12886
3e73aa7c 12887 /* Are we finished with this relocation now? */
c6682705 12888 if (fixP->fx_addsy == NULL)
b8188555
JB
12889 {
12890 fixP->fx_done = 1;
12891 switch (fixP->fx_r_type)
12892 {
12893 case BFD_RELOC_X86_64_32S:
12894 fixP->fx_signed = 1;
12895 break;
12896
12897 default:
12898 break;
12899 }
12900 }
fbeb56a4
DK
12901#if defined (OBJ_COFF) && defined (TE_PE)
12902 else if (fixP->fx_addsy != NULL && S_IS_WEAK (fixP->fx_addsy))
12903 {
12904 fixP->fx_done = 0;
12905 /* Remember value for tc_gen_reloc. */
12906 fixP->fx_addnumber = value;
12907 /* Clear out the frag for now. */
12908 value = 0;
12909 }
12910#endif
3e73aa7c
JH
12911 else if (use_rela_relocations)
12912 {
46fb6d5a
JB
12913 if (!disallow_64bit_reloc || fixP->fx_r_type == NO_RELOC)
12914 fixP->fx_no_overflow = 1;
062cd5e7
AS
12915 /* Remember value for tc_gen_reloc. */
12916 fixP->fx_addnumber = value;
3e73aa7c
JH
12917 value = 0;
12918 }
f86103b7 12919
94f592af 12920 md_number_to_chars (p, value, fixP->fx_size);
252b5132 12921}
252b5132 12922\f
6d4af3c2 12923const char *
499ac353 12924md_atof (int type, char *litP, int *sizeP)
252b5132 12925{
499ac353
NC
12926 /* This outputs the LITTLENUMs in REVERSE order;
12927 in accord with the bigendian 386. */
5b7c81bd 12928 return ieee_md_atof (type, litP, sizeP, false);
252b5132
RH
12929}
12930\f
2d545b82 12931static char output_invalid_buf[sizeof (unsigned char) * 2 + 6];
252b5132 12932
252b5132 12933static char *
e3bb37b5 12934output_invalid (int c)
252b5132 12935{
3882b010 12936 if (ISPRINT (c))
f9f21a03
L
12937 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
12938 "'%c'", c);
252b5132 12939 else
f9f21a03 12940 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
2d545b82 12941 "(0x%x)", (unsigned char) c);
252b5132
RH
12942 return output_invalid_buf;
12943}
12944
8a6fb3f9
JB
12945/* Verify that @r can be used in the current context. */
12946
5b7c81bd 12947static bool check_register (const reg_entry *r)
8a6fb3f9
JB
12948{
12949 if (allow_pseudo_reg)
5b7c81bd 12950 return true;
8a6fb3f9
JB
12951
12952 if (operand_type_all_zero (&r->reg_type))
5b7c81bd 12953 return false;
8a6fb3f9
JB
12954
12955 if ((r->reg_type.bitfield.dword
12956 || (r->reg_type.bitfield.class == SReg && r->reg_num > 3)
12957 || r->reg_type.bitfield.class == RegCR
22e00a3f 12958 || r->reg_type.bitfield.class == RegDR)
8a6fb3f9 12959 && !cpu_arch_flags.bitfield.cpui386)
5b7c81bd 12960 return false;
8a6fb3f9 12961
22e00a3f
JB
12962 if (r->reg_type.bitfield.class == RegTR
12963 && (flag_code == CODE_64BIT
12964 || !cpu_arch_flags.bitfield.cpui386
12965 || cpu_arch_isa_flags.bitfield.cpui586
12966 || cpu_arch_isa_flags.bitfield.cpui686))
5b7c81bd 12967 return false;
22e00a3f 12968
8a6fb3f9 12969 if (r->reg_type.bitfield.class == RegMMX && !cpu_arch_flags.bitfield.cpummx)
5b7c81bd 12970 return false;
8a6fb3f9
JB
12971
12972 if (!cpu_arch_flags.bitfield.cpuavx512f)
12973 {
12974 if (r->reg_type.bitfield.zmmword
12975 || r->reg_type.bitfield.class == RegMask)
5b7c81bd 12976 return false;
8a6fb3f9
JB
12977
12978 if (!cpu_arch_flags.bitfield.cpuavx)
12979 {
12980 if (r->reg_type.bitfield.ymmword)
5b7c81bd 12981 return false;
8a6fb3f9
JB
12982
12983 if (!cpu_arch_flags.bitfield.cpusse && r->reg_type.bitfield.xmmword)
5b7c81bd 12984 return false;
8a6fb3f9
JB
12985 }
12986 }
12987
260cd341
LC
12988 if (r->reg_type.bitfield.tmmword
12989 && (!cpu_arch_flags.bitfield.cpuamx_tile
12990 || flag_code != CODE_64BIT))
5b7c81bd 12991 return false;
260cd341 12992
8a6fb3f9 12993 if (r->reg_type.bitfield.class == RegBND && !cpu_arch_flags.bitfield.cpumpx)
5b7c81bd 12994 return false;
8a6fb3f9
JB
12995
12996 /* Don't allow fake index register unless allow_index_reg isn't 0. */
12997 if (!allow_index_reg && r->reg_num == RegIZ)
5b7c81bd 12998 return false;
8a6fb3f9
JB
12999
13000 /* Upper 16 vector registers are only available with VREX in 64bit
13001 mode, and require EVEX encoding. */
13002 if (r->reg_flags & RegVRex)
13003 {
13004 if (!cpu_arch_flags.bitfield.cpuavx512f
13005 || flag_code != CODE_64BIT)
5b7c81bd 13006 return false;
8a6fb3f9 13007
da4977e0
JB
13008 if (i.vec_encoding == vex_encoding_default)
13009 i.vec_encoding = vex_encoding_evex;
13010 else if (i.vec_encoding != vex_encoding_evex)
13011 i.vec_encoding = vex_encoding_error;
8a6fb3f9
JB
13012 }
13013
13014 if (((r->reg_flags & (RegRex64 | RegRex)) || r->reg_type.bitfield.qword)
13015 && (!cpu_arch_flags.bitfield.cpulm || r->reg_type.bitfield.class != RegCR)
13016 && flag_code != CODE_64BIT)
5b7c81bd 13017 return false;
8a6fb3f9
JB
13018
13019 if (r->reg_type.bitfield.class == SReg && r->reg_num == RegFlat
13020 && !intel_syntax)
5b7c81bd 13021 return false;
8a6fb3f9 13022
5b7c81bd 13023 return true;
8a6fb3f9
JB
13024}
13025
af6bdddf 13026/* REG_STRING starts *before* REGISTER_PREFIX. */
252b5132
RH
13027
13028static const reg_entry *
4d1bb795 13029parse_real_register (char *reg_string, char **end_op)
252b5132 13030{
af6bdddf
AM
13031 char *s = reg_string;
13032 char *p;
252b5132
RH
13033 char reg_name_given[MAX_REG_NAME_SIZE + 1];
13034 const reg_entry *r;
13035
13036 /* Skip possible REGISTER_PREFIX and possible whitespace. */
13037 if (*s == REGISTER_PREFIX)
13038 ++s;
13039
13040 if (is_space_char (*s))
13041 ++s;
13042
13043 p = reg_name_given;
af6bdddf 13044 while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
252b5132
RH
13045 {
13046 if (p >= reg_name_given + MAX_REG_NAME_SIZE)
af6bdddf
AM
13047 return (const reg_entry *) NULL;
13048 s++;
252b5132
RH
13049 }
13050
6588847e
DN
13051 /* For naked regs, make sure that we are not dealing with an identifier.
13052 This prevents confusing an identifier like `eax_var' with register
13053 `eax'. */
13054 if (allow_naked_reg && identifier_chars[(unsigned char) *s])
13055 return (const reg_entry *) NULL;
13056
af6bdddf 13057 *end_op = s;
252b5132 13058
629310ab 13059 r = (const reg_entry *) str_hash_find (reg_hash, reg_name_given);
252b5132 13060
5f47d35b 13061 /* Handle floating point regs, allowing spaces in the (i) part. */
6288d05f 13062 if (r == reg_st0)
5f47d35b 13063 {
0e0eea78
JB
13064 if (!cpu_arch_flags.bitfield.cpu8087
13065 && !cpu_arch_flags.bitfield.cpu287
af32b722
JB
13066 && !cpu_arch_flags.bitfield.cpu387
13067 && !allow_pseudo_reg)
0e0eea78
JB
13068 return (const reg_entry *) NULL;
13069
5f47d35b
AM
13070 if (is_space_char (*s))
13071 ++s;
13072 if (*s == '(')
13073 {
af6bdddf 13074 ++s;
5f47d35b
AM
13075 if (is_space_char (*s))
13076 ++s;
13077 if (*s >= '0' && *s <= '7')
13078 {
db557034 13079 int fpr = *s - '0';
af6bdddf 13080 ++s;
5f47d35b
AM
13081 if (is_space_char (*s))
13082 ++s;
13083 if (*s == ')')
13084 {
13085 *end_op = s + 1;
6288d05f 13086 know (r[fpr].reg_num == fpr);
db557034 13087 return r + fpr;
5f47d35b 13088 }
5f47d35b 13089 }
47926f60 13090 /* We have "%st(" then garbage. */
5f47d35b
AM
13091 return (const reg_entry *) NULL;
13092 }
13093 }
13094
8a6fb3f9 13095 return r && check_register (r) ? r : NULL;
252b5132 13096}
4d1bb795
JB
13097
13098/* REG_STRING starts *before* REGISTER_PREFIX. */
13099
13100static const reg_entry *
13101parse_register (char *reg_string, char **end_op)
13102{
13103 const reg_entry *r;
13104
13105 if (*reg_string == REGISTER_PREFIX || allow_naked_reg)
13106 r = parse_real_register (reg_string, end_op);
13107 else
13108 r = NULL;
13109 if (!r)
13110 {
13111 char *save = input_line_pointer;
13112 char c;
13113 symbolS *symbolP;
13114
13115 input_line_pointer = reg_string;
d02603dc 13116 c = get_symbol_name (&reg_string);
4d1bb795 13117 symbolP = symbol_find (reg_string);
64d23078
JB
13118 while (symbolP && S_GET_SEGMENT (symbolP) != reg_section)
13119 {
13120 const expressionS *e = symbol_get_value_expression(symbolP);
13121
13122 if (e->X_op != O_symbol || e->X_add_number)
13123 break;
13124 symbolP = e->X_add_symbol;
13125 }
4d1bb795
JB
13126 if (symbolP && S_GET_SEGMENT (symbolP) == reg_section)
13127 {
13128 const expressionS *e = symbol_get_value_expression (symbolP);
13129
3b55a1d0
JB
13130 know (e->X_op == O_register);
13131 know (e->X_add_number >= 0
13132 && (valueT) e->X_add_number < i386_regtab_size);
13133 r = i386_regtab + e->X_add_number;
13134 if (!check_register (r))
8a6fb3f9 13135 {
3b55a1d0
JB
13136 as_bad (_("register '%s%s' cannot be used here"),
13137 register_prefix, r->reg_name);
13138 r = &bad_reg;
8a6fb3f9 13139 }
3b55a1d0 13140 *end_op = input_line_pointer;
4d1bb795
JB
13141 }
13142 *input_line_pointer = c;
13143 input_line_pointer = save;
13144 }
13145 return r;
13146}
13147
13148int
13149i386_parse_name (char *name, expressionS *e, char *nextcharP)
13150{
4faaa10f 13151 const reg_entry *r = NULL;
4d1bb795
JB
13152 char *end = input_line_pointer;
13153
13154 *end = *nextcharP;
4faaa10f
JB
13155 if (*name == REGISTER_PREFIX || allow_naked_reg)
13156 r = parse_real_register (name, &input_line_pointer);
4d1bb795
JB
13157 if (r && end <= input_line_pointer)
13158 {
13159 *nextcharP = *input_line_pointer;
13160 *input_line_pointer = 0;
8a6fb3f9
JB
13161 if (r != &bad_reg)
13162 {
13163 e->X_op = O_register;
13164 e->X_add_number = r - i386_regtab;
13165 }
13166 else
13167 e->X_op = O_illegal;
4d1bb795
JB
13168 return 1;
13169 }
13170 input_line_pointer = end;
13171 *end = 0;
ee86248c 13172 return intel_syntax ? i386_intel_parse_name (name, e) : 0;
4d1bb795
JB
13173}
13174
13175void
13176md_operand (expressionS *e)
13177{
ee86248c
JB
13178 char *end;
13179 const reg_entry *r;
4d1bb795 13180
ee86248c
JB
13181 switch (*input_line_pointer)
13182 {
13183 case REGISTER_PREFIX:
13184 r = parse_real_register (input_line_pointer, &end);
4d1bb795
JB
13185 if (r)
13186 {
13187 e->X_op = O_register;
13188 e->X_add_number = r - i386_regtab;
13189 input_line_pointer = end;
13190 }
ee86248c
JB
13191 break;
13192
13193 case '[':
9c2799c2 13194 gas_assert (intel_syntax);
ee86248c
JB
13195 end = input_line_pointer++;
13196 expression (e);
13197 if (*input_line_pointer == ']')
13198 {
13199 ++input_line_pointer;
13200 e->X_op_symbol = make_expr_symbol (e);
13201 e->X_add_symbol = NULL;
13202 e->X_add_number = 0;
13203 e->X_op = O_index;
13204 }
13205 else
13206 {
13207 e->X_op = O_absent;
13208 input_line_pointer = end;
13209 }
13210 break;
4d1bb795
JB
13211 }
13212}
13213
252b5132 13214\f
4cc782b5 13215#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
b6f8c7c4 13216const char *md_shortopts = "kVQ:sqnO::";
252b5132 13217#else
b6f8c7c4 13218const char *md_shortopts = "qnO::";
252b5132 13219#endif
6e0b89ee 13220
3e73aa7c 13221#define OPTION_32 (OPTION_MD_BASE + 0)
b3b91714
AM
13222#define OPTION_64 (OPTION_MD_BASE + 1)
13223#define OPTION_DIVIDE (OPTION_MD_BASE + 2)
9103f4f4
L
13224#define OPTION_MARCH (OPTION_MD_BASE + 3)
13225#define OPTION_MTUNE (OPTION_MD_BASE + 4)
1efbbeb4
L
13226#define OPTION_MMNEMONIC (OPTION_MD_BASE + 5)
13227#define OPTION_MSYNTAX (OPTION_MD_BASE + 6)
13228#define OPTION_MINDEX_REG (OPTION_MD_BASE + 7)
13229#define OPTION_MNAKED_REG (OPTION_MD_BASE + 8)
bd5dea88 13230#define OPTION_MRELAX_RELOCATIONS (OPTION_MD_BASE + 9)
c0f3af97 13231#define OPTION_MSSE2AVX (OPTION_MD_BASE + 10)
daf50ae7 13232#define OPTION_MSSE_CHECK (OPTION_MD_BASE + 11)
7bab8ab5
JB
13233#define OPTION_MOPERAND_CHECK (OPTION_MD_BASE + 12)
13234#define OPTION_MAVXSCALAR (OPTION_MD_BASE + 13)
13235#define OPTION_X32 (OPTION_MD_BASE + 14)
7e8b059b 13236#define OPTION_MADD_BND_PREFIX (OPTION_MD_BASE + 15)
43234a1e
L
13237#define OPTION_MEVEXLIG (OPTION_MD_BASE + 16)
13238#define OPTION_MEVEXWIG (OPTION_MD_BASE + 17)
167ad85b 13239#define OPTION_MBIG_OBJ (OPTION_MD_BASE + 18)
d1982f93 13240#define OPTION_MOMIT_LOCK_PREFIX (OPTION_MD_BASE + 19)
d3d3c6db 13241#define OPTION_MEVEXRCIG (OPTION_MD_BASE + 20)
8dcea932 13242#define OPTION_MSHARED (OPTION_MD_BASE + 21)
5db04b09
L
13243#define OPTION_MAMD64 (OPTION_MD_BASE + 22)
13244#define OPTION_MINTEL64 (OPTION_MD_BASE + 23)
e4e00185 13245#define OPTION_MFENCE_AS_LOCK_ADD (OPTION_MD_BASE + 24)
b4a3a7b4 13246#define OPTION_X86_USED_NOTE (OPTION_MD_BASE + 25)
03751133 13247#define OPTION_MVEXWIG (OPTION_MD_BASE + 26)
e379e5f3
L
13248#define OPTION_MALIGN_BRANCH_BOUNDARY (OPTION_MD_BASE + 27)
13249#define OPTION_MALIGN_BRANCH_PREFIX_SIZE (OPTION_MD_BASE + 28)
13250#define OPTION_MALIGN_BRANCH (OPTION_MD_BASE + 29)
76cf450b 13251#define OPTION_MBRANCHES_WITH_32B_BOUNDARIES (OPTION_MD_BASE + 30)
ae531041
L
13252#define OPTION_MLFENCE_AFTER_LOAD (OPTION_MD_BASE + 31)
13253#define OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH (OPTION_MD_BASE + 32)
13254#define OPTION_MLFENCE_BEFORE_RET (OPTION_MD_BASE + 33)
c8480b58 13255#define OPTION_MUSE_UNALIGNED_VECTOR_MOVE (OPTION_MD_BASE + 34)
b3b91714 13256
99ad8390
NC
13257struct option md_longopts[] =
13258{
3e73aa7c 13259 {"32", no_argument, NULL, OPTION_32},
321098a5 13260#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 13261 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c 13262 {"64", no_argument, NULL, OPTION_64},
351f65ca
L
13263#endif
13264#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 13265 {"x32", no_argument, NULL, OPTION_X32},
8dcea932 13266 {"mshared", no_argument, NULL, OPTION_MSHARED},
b4a3a7b4 13267 {"mx86-used-note", required_argument, NULL, OPTION_X86_USED_NOTE},
6e0b89ee 13268#endif
b3b91714 13269 {"divide", no_argument, NULL, OPTION_DIVIDE},
9103f4f4
L
13270 {"march", required_argument, NULL, OPTION_MARCH},
13271 {"mtune", required_argument, NULL, OPTION_MTUNE},
1efbbeb4
L
13272 {"mmnemonic", required_argument, NULL, OPTION_MMNEMONIC},
13273 {"msyntax", required_argument, NULL, OPTION_MSYNTAX},
13274 {"mindex-reg", no_argument, NULL, OPTION_MINDEX_REG},
13275 {"mnaked-reg", no_argument, NULL, OPTION_MNAKED_REG},
c0f3af97 13276 {"msse2avx", no_argument, NULL, OPTION_MSSE2AVX},
c8480b58 13277 {"muse-unaligned-vector-move", no_argument, NULL, OPTION_MUSE_UNALIGNED_VECTOR_MOVE},
daf50ae7 13278 {"msse-check", required_argument, NULL, OPTION_MSSE_CHECK},
7bab8ab5 13279 {"moperand-check", required_argument, NULL, OPTION_MOPERAND_CHECK},
539f890d 13280 {"mavxscalar", required_argument, NULL, OPTION_MAVXSCALAR},
03751133 13281 {"mvexwig", required_argument, NULL, OPTION_MVEXWIG},
7e8b059b 13282 {"madd-bnd-prefix", no_argument, NULL, OPTION_MADD_BND_PREFIX},
43234a1e
L
13283 {"mevexlig", required_argument, NULL, OPTION_MEVEXLIG},
13284 {"mevexwig", required_argument, NULL, OPTION_MEVEXWIG},
167ad85b
TG
13285# if defined (TE_PE) || defined (TE_PEP)
13286 {"mbig-obj", no_argument, NULL, OPTION_MBIG_OBJ},
13287#endif
d1982f93 13288 {"momit-lock-prefix", required_argument, NULL, OPTION_MOMIT_LOCK_PREFIX},
e4e00185 13289 {"mfence-as-lock-add", required_argument, NULL, OPTION_MFENCE_AS_LOCK_ADD},
0cb4071e 13290 {"mrelax-relocations", required_argument, NULL, OPTION_MRELAX_RELOCATIONS},
d3d3c6db 13291 {"mevexrcig", required_argument, NULL, OPTION_MEVEXRCIG},
e379e5f3
L
13292 {"malign-branch-boundary", required_argument, NULL, OPTION_MALIGN_BRANCH_BOUNDARY},
13293 {"malign-branch-prefix-size", required_argument, NULL, OPTION_MALIGN_BRANCH_PREFIX_SIZE},
13294 {"malign-branch", required_argument, NULL, OPTION_MALIGN_BRANCH},
76cf450b 13295 {"mbranches-within-32B-boundaries", no_argument, NULL, OPTION_MBRANCHES_WITH_32B_BOUNDARIES},
ae531041
L
13296 {"mlfence-after-load", required_argument, NULL, OPTION_MLFENCE_AFTER_LOAD},
13297 {"mlfence-before-indirect-branch", required_argument, NULL,
13298 OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH},
13299 {"mlfence-before-ret", required_argument, NULL, OPTION_MLFENCE_BEFORE_RET},
5db04b09
L
13300 {"mamd64", no_argument, NULL, OPTION_MAMD64},
13301 {"mintel64", no_argument, NULL, OPTION_MINTEL64},
252b5132
RH
13302 {NULL, no_argument, NULL, 0}
13303};
13304size_t md_longopts_size = sizeof (md_longopts);
13305
13306int
17b9d67d 13307md_parse_option (int c, const char *arg)
252b5132 13308{
91d6fa6a 13309 unsigned int j;
e379e5f3 13310 char *arch, *next, *saved, *type;
9103f4f4 13311
252b5132
RH
13312 switch (c)
13313 {
12b55ccc
L
13314 case 'n':
13315 optimize_align_code = 0;
13316 break;
13317
a38cf1db
AM
13318 case 'q':
13319 quiet_warnings = 1;
252b5132
RH
13320 break;
13321
13322#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
a38cf1db
AM
13323 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
13324 should be emitted or not. FIXME: Not implemented. */
13325 case 'Q':
d4693039
JB
13326 if ((arg[0] != 'y' && arg[0] != 'n') || arg[1])
13327 return 0;
252b5132
RH
13328 break;
13329
13330 /* -V: SVR4 argument to print version ID. */
13331 case 'V':
13332 print_version_id ();
13333 break;
13334
a38cf1db
AM
13335 /* -k: Ignore for FreeBSD compatibility. */
13336 case 'k':
252b5132 13337 break;
4cc782b5
ILT
13338
13339 case 's':
13340 /* -s: On i386 Solaris, this tells the native assembler to use
29b0f896 13341 .stab instead of .stab.excl. We always use .stab anyhow. */
4cc782b5 13342 break;
8dcea932
L
13343
13344 case OPTION_MSHARED:
13345 shared = 1;
13346 break;
b4a3a7b4
L
13347
13348 case OPTION_X86_USED_NOTE:
13349 if (strcasecmp (arg, "yes") == 0)
13350 x86_used_note = 1;
13351 else if (strcasecmp (arg, "no") == 0)
13352 x86_used_note = 0;
13353 else
13354 as_fatal (_("invalid -mx86-used-note= option: `%s'"), arg);
13355 break;
13356
13357
99ad8390 13358#endif
321098a5 13359#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 13360 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c
JH
13361 case OPTION_64:
13362 {
13363 const char **list, **l;
13364
3e73aa7c
JH
13365 list = bfd_target_list ();
13366 for (l = list; *l != NULL; l++)
08dedd66 13367 if (startswith (*l, "elf64-x86-64")
99ad8390
NC
13368 || strcmp (*l, "coff-x86-64") == 0
13369 || strcmp (*l, "pe-x86-64") == 0
d382c579
TG
13370 || strcmp (*l, "pei-x86-64") == 0
13371 || strcmp (*l, "mach-o-x86-64") == 0)
6e0b89ee
AM
13372 {
13373 default_arch = "x86_64";
13374 break;
13375 }
3e73aa7c 13376 if (*l == NULL)
2b5d6a91 13377 as_fatal (_("no compiled in support for x86_64"));
3e73aa7c
JH
13378 free (list);
13379 }
13380 break;
13381#endif
252b5132 13382
351f65ca 13383#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 13384 case OPTION_X32:
351f65ca
L
13385 if (IS_ELF)
13386 {
13387 const char **list, **l;
13388
13389 list = bfd_target_list ();
13390 for (l = list; *l != NULL; l++)
08dedd66 13391 if (startswith (*l, "elf32-x86-64"))
351f65ca
L
13392 {
13393 default_arch = "x86_64:32";
13394 break;
13395 }
13396 if (*l == NULL)
2b5d6a91 13397 as_fatal (_("no compiled in support for 32bit x86_64"));
351f65ca
L
13398 free (list);
13399 }
13400 else
13401 as_fatal (_("32bit x86_64 is only supported for ELF"));
13402 break;
13403#endif
13404
6e0b89ee
AM
13405 case OPTION_32:
13406 default_arch = "i386";
13407 break;
13408
b3b91714
AM
13409 case OPTION_DIVIDE:
13410#ifdef SVR4_COMMENT_CHARS
13411 {
13412 char *n, *t;
13413 const char *s;
13414
add39d23 13415 n = XNEWVEC (char, strlen (i386_comment_chars) + 1);
b3b91714
AM
13416 t = n;
13417 for (s = i386_comment_chars; *s != '\0'; s++)
13418 if (*s != '/')
13419 *t++ = *s;
13420 *t = '\0';
13421 i386_comment_chars = n;
13422 }
13423#endif
13424 break;
13425
9103f4f4 13426 case OPTION_MARCH:
293f5f65
L
13427 saved = xstrdup (arg);
13428 arch = saved;
13429 /* Allow -march=+nosse. */
13430 if (*arch == '+')
13431 arch++;
6305a203 13432 do
9103f4f4 13433 {
6305a203 13434 if (*arch == '.')
2b5d6a91 13435 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
13436 next = strchr (arch, '+');
13437 if (next)
13438 *next++ = '\0';
91d6fa6a 13439 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 13440 {
ae89daec
JB
13441 if (arch == saved && cpu_arch[j].type != PROCESSOR_NONE
13442 && strcmp (arch, cpu_arch[j].name) == 0)
ccc9c027 13443 {
6305a203 13444 /* Processor. */
ae89daec 13445 if (! cpu_arch[j].enable.bitfield.cpui386)
1ded5609
JB
13446 continue;
13447
91d6fa6a 13448 cpu_arch_name = cpu_arch[j].name;
d92c7521 13449 free (cpu_sub_arch_name);
6305a203 13450 cpu_sub_arch_name = NULL;
ae89daec 13451 cpu_arch_flags = cpu_arch[j].enable;
91d6fa6a 13452 cpu_arch_isa = cpu_arch[j].type;
ae89daec 13453 cpu_arch_isa_flags = cpu_arch[j].enable;
6305a203
L
13454 if (!cpu_arch_tune_set)
13455 {
13456 cpu_arch_tune = cpu_arch_isa;
13457 cpu_arch_tune_flags = cpu_arch_isa_flags;
13458 }
13459 break;
13460 }
ae89daec
JB
13461 else if (cpu_arch[j].type == PROCESSOR_NONE
13462 && strcmp (arch, cpu_arch[j].name) == 0
13463 && !cpu_flags_all_zero (&cpu_arch[j].enable))
6305a203 13464 {
33eaf5de 13465 /* ISA extension. */
6305a203 13466 i386_cpu_flags flags;
309d3373 13467
293f5f65 13468 flags = cpu_flags_or (cpu_arch_flags,
ae89daec 13469 cpu_arch[j].enable);
81486035 13470
5b64d091 13471 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
6305a203 13472 {
ae89daec 13473 extend_cpu_sub_arch_name (arch);
6305a203 13474 cpu_arch_flags = flags;
a586129e 13475 cpu_arch_isa_flags = flags;
6305a203 13476 }
0089dace
L
13477 else
13478 cpu_arch_isa_flags
13479 = cpu_flags_or (cpu_arch_isa_flags,
ae89daec 13480 cpu_arch[j].enable);
6305a203 13481 break;
ccc9c027 13482 }
9103f4f4 13483 }
6305a203 13484
ae89daec 13485 if (j >= ARRAY_SIZE (cpu_arch) && startswith (arch, "no"))
293f5f65 13486 {
33eaf5de 13487 /* Disable an ISA extension. */
ae89daec
JB
13488 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13489 if (cpu_arch[j].type == PROCESSOR_NONE
13490 && strcmp (arch + 2, cpu_arch[j].name) == 0)
293f5f65
L
13491 {
13492 i386_cpu_flags flags;
13493
13494 flags = cpu_flags_and_not (cpu_arch_flags,
ae89daec 13495 cpu_arch[j].disable);
293f5f65
L
13496 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
13497 {
8180707f 13498 extend_cpu_sub_arch_name (arch);
293f5f65
L
13499 cpu_arch_flags = flags;
13500 cpu_arch_isa_flags = flags;
13501 }
13502 break;
13503 }
293f5f65
L
13504 }
13505
91d6fa6a 13506 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 13507 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
13508
13509 arch = next;
9103f4f4 13510 }
293f5f65
L
13511 while (next != NULL);
13512 free (saved);
9103f4f4
L
13513 break;
13514
13515 case OPTION_MTUNE:
13516 if (*arg == '.')
2b5d6a91 13517 as_fatal (_("invalid -mtune= option: `%s'"), arg);
91d6fa6a 13518 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 13519 {
ae89daec
JB
13520 if (cpu_arch[j].type != PROCESSOR_NONE
13521 && strcmp (arg, cpu_arch[j].name) == 0)
9103f4f4 13522 {
ccc9c027 13523 cpu_arch_tune_set = 1;
91d6fa6a 13524 cpu_arch_tune = cpu_arch [j].type;
ae89daec 13525 cpu_arch_tune_flags = cpu_arch[j].enable;
9103f4f4
L
13526 break;
13527 }
13528 }
91d6fa6a 13529 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 13530 as_fatal (_("invalid -mtune= option: `%s'"), arg);
9103f4f4
L
13531 break;
13532
1efbbeb4
L
13533 case OPTION_MMNEMONIC:
13534 if (strcasecmp (arg, "att") == 0)
13535 intel_mnemonic = 0;
13536 else if (strcasecmp (arg, "intel") == 0)
13537 intel_mnemonic = 1;
13538 else
2b5d6a91 13539 as_fatal (_("invalid -mmnemonic= option: `%s'"), arg);
1efbbeb4
L
13540 break;
13541
13542 case OPTION_MSYNTAX:
13543 if (strcasecmp (arg, "att") == 0)
13544 intel_syntax = 0;
13545 else if (strcasecmp (arg, "intel") == 0)
13546 intel_syntax = 1;
13547 else
2b5d6a91 13548 as_fatal (_("invalid -msyntax= option: `%s'"), arg);
1efbbeb4
L
13549 break;
13550
13551 case OPTION_MINDEX_REG:
13552 allow_index_reg = 1;
13553 break;
13554
13555 case OPTION_MNAKED_REG:
13556 allow_naked_reg = 1;
13557 break;
13558
c0f3af97
L
13559 case OPTION_MSSE2AVX:
13560 sse2avx = 1;
13561 break;
13562
c8480b58
L
13563 case OPTION_MUSE_UNALIGNED_VECTOR_MOVE:
13564 use_unaligned_vector_move = 1;
13565 break;
13566
daf50ae7
L
13567 case OPTION_MSSE_CHECK:
13568 if (strcasecmp (arg, "error") == 0)
7bab8ab5 13569 sse_check = check_error;
daf50ae7 13570 else if (strcasecmp (arg, "warning") == 0)
7bab8ab5 13571 sse_check = check_warning;
daf50ae7 13572 else if (strcasecmp (arg, "none") == 0)
7bab8ab5 13573 sse_check = check_none;
daf50ae7 13574 else
2b5d6a91 13575 as_fatal (_("invalid -msse-check= option: `%s'"), arg);
daf50ae7
L
13576 break;
13577
7bab8ab5
JB
13578 case OPTION_MOPERAND_CHECK:
13579 if (strcasecmp (arg, "error") == 0)
13580 operand_check = check_error;
13581 else if (strcasecmp (arg, "warning") == 0)
13582 operand_check = check_warning;
13583 else if (strcasecmp (arg, "none") == 0)
13584 operand_check = check_none;
13585 else
13586 as_fatal (_("invalid -moperand-check= option: `%s'"), arg);
13587 break;
13588
539f890d
L
13589 case OPTION_MAVXSCALAR:
13590 if (strcasecmp (arg, "128") == 0)
13591 avxscalar = vex128;
13592 else if (strcasecmp (arg, "256") == 0)
13593 avxscalar = vex256;
13594 else
2b5d6a91 13595 as_fatal (_("invalid -mavxscalar= option: `%s'"), arg);
539f890d
L
13596 break;
13597
03751133
L
13598 case OPTION_MVEXWIG:
13599 if (strcmp (arg, "0") == 0)
40c9c8de 13600 vexwig = vexw0;
03751133 13601 else if (strcmp (arg, "1") == 0)
40c9c8de 13602 vexwig = vexw1;
03751133
L
13603 else
13604 as_fatal (_("invalid -mvexwig= option: `%s'"), arg);
13605 break;
13606
7e8b059b
L
13607 case OPTION_MADD_BND_PREFIX:
13608 add_bnd_prefix = 1;
13609 break;
13610
43234a1e
L
13611 case OPTION_MEVEXLIG:
13612 if (strcmp (arg, "128") == 0)
13613 evexlig = evexl128;
13614 else if (strcmp (arg, "256") == 0)
13615 evexlig = evexl256;
13616 else if (strcmp (arg, "512") == 0)
13617 evexlig = evexl512;
13618 else
13619 as_fatal (_("invalid -mevexlig= option: `%s'"), arg);
13620 break;
13621
d3d3c6db
IT
13622 case OPTION_MEVEXRCIG:
13623 if (strcmp (arg, "rne") == 0)
13624 evexrcig = rne;
13625 else if (strcmp (arg, "rd") == 0)
13626 evexrcig = rd;
13627 else if (strcmp (arg, "ru") == 0)
13628 evexrcig = ru;
13629 else if (strcmp (arg, "rz") == 0)
13630 evexrcig = rz;
13631 else
13632 as_fatal (_("invalid -mevexrcig= option: `%s'"), arg);
13633 break;
13634
43234a1e
L
13635 case OPTION_MEVEXWIG:
13636 if (strcmp (arg, "0") == 0)
13637 evexwig = evexw0;
13638 else if (strcmp (arg, "1") == 0)
13639 evexwig = evexw1;
13640 else
13641 as_fatal (_("invalid -mevexwig= option: `%s'"), arg);
13642 break;
13643
167ad85b
TG
13644# if defined (TE_PE) || defined (TE_PEP)
13645 case OPTION_MBIG_OBJ:
13646 use_big_obj = 1;
13647 break;
13648#endif
13649
d1982f93 13650 case OPTION_MOMIT_LOCK_PREFIX:
d022bddd
IT
13651 if (strcasecmp (arg, "yes") == 0)
13652 omit_lock_prefix = 1;
13653 else if (strcasecmp (arg, "no") == 0)
13654 omit_lock_prefix = 0;
13655 else
13656 as_fatal (_("invalid -momit-lock-prefix= option: `%s'"), arg);
13657 break;
13658
e4e00185
AS
13659 case OPTION_MFENCE_AS_LOCK_ADD:
13660 if (strcasecmp (arg, "yes") == 0)
13661 avoid_fence = 1;
13662 else if (strcasecmp (arg, "no") == 0)
13663 avoid_fence = 0;
13664 else
13665 as_fatal (_("invalid -mfence-as-lock-add= option: `%s'"), arg);
13666 break;
13667
ae531041
L
13668 case OPTION_MLFENCE_AFTER_LOAD:
13669 if (strcasecmp (arg, "yes") == 0)
13670 lfence_after_load = 1;
13671 else if (strcasecmp (arg, "no") == 0)
13672 lfence_after_load = 0;
13673 else
13674 as_fatal (_("invalid -mlfence-after-load= option: `%s'"), arg);
13675 break;
13676
13677 case OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH:
13678 if (strcasecmp (arg, "all") == 0)
a09f656b 13679 {
13680 lfence_before_indirect_branch = lfence_branch_all;
13681 if (lfence_before_ret == lfence_before_ret_none)
13682 lfence_before_ret = lfence_before_ret_shl;
13683 }
ae531041
L
13684 else if (strcasecmp (arg, "memory") == 0)
13685 lfence_before_indirect_branch = lfence_branch_memory;
13686 else if (strcasecmp (arg, "register") == 0)
13687 lfence_before_indirect_branch = lfence_branch_register;
13688 else if (strcasecmp (arg, "none") == 0)
13689 lfence_before_indirect_branch = lfence_branch_none;
13690 else
13691 as_fatal (_("invalid -mlfence-before-indirect-branch= option: `%s'"),
13692 arg);
13693 break;
13694
13695 case OPTION_MLFENCE_BEFORE_RET:
13696 if (strcasecmp (arg, "or") == 0)
13697 lfence_before_ret = lfence_before_ret_or;
13698 else if (strcasecmp (arg, "not") == 0)
13699 lfence_before_ret = lfence_before_ret_not;
a09f656b 13700 else if (strcasecmp (arg, "shl") == 0 || strcasecmp (arg, "yes") == 0)
13701 lfence_before_ret = lfence_before_ret_shl;
ae531041
L
13702 else if (strcasecmp (arg, "none") == 0)
13703 lfence_before_ret = lfence_before_ret_none;
13704 else
13705 as_fatal (_("invalid -mlfence-before-ret= option: `%s'"),
13706 arg);
13707 break;
13708
0cb4071e
L
13709 case OPTION_MRELAX_RELOCATIONS:
13710 if (strcasecmp (arg, "yes") == 0)
13711 generate_relax_relocations = 1;
13712 else if (strcasecmp (arg, "no") == 0)
13713 generate_relax_relocations = 0;
13714 else
13715 as_fatal (_("invalid -mrelax-relocations= option: `%s'"), arg);
13716 break;
13717
e379e5f3
L
13718 case OPTION_MALIGN_BRANCH_BOUNDARY:
13719 {
13720 char *end;
13721 long int align = strtoul (arg, &end, 0);
13722 if (*end == '\0')
13723 {
13724 if (align == 0)
13725 {
13726 align_branch_power = 0;
13727 break;
13728 }
13729 else if (align >= 16)
13730 {
13731 int align_power;
13732 for (align_power = 0;
13733 (align & 1) == 0;
13734 align >>= 1, align_power++)
13735 continue;
13736 /* Limit alignment power to 31. */
13737 if (align == 1 && align_power < 32)
13738 {
13739 align_branch_power = align_power;
13740 break;
13741 }
13742 }
13743 }
13744 as_fatal (_("invalid -malign-branch-boundary= value: %s"), arg);
13745 }
13746 break;
13747
13748 case OPTION_MALIGN_BRANCH_PREFIX_SIZE:
13749 {
13750 char *end;
13751 int align = strtoul (arg, &end, 0);
13752 /* Some processors only support 5 prefixes. */
13753 if (*end == '\0' && align >= 0 && align < 6)
13754 {
13755 align_branch_prefix_size = align;
13756 break;
13757 }
13758 as_fatal (_("invalid -malign-branch-prefix-size= value: %s"),
13759 arg);
13760 }
13761 break;
13762
13763 case OPTION_MALIGN_BRANCH:
13764 align_branch = 0;
13765 saved = xstrdup (arg);
13766 type = saved;
13767 do
13768 {
13769 next = strchr (type, '+');
13770 if (next)
13771 *next++ = '\0';
13772 if (strcasecmp (type, "jcc") == 0)
13773 align_branch |= align_branch_jcc_bit;
13774 else if (strcasecmp (type, "fused") == 0)
13775 align_branch |= align_branch_fused_bit;
13776 else if (strcasecmp (type, "jmp") == 0)
13777 align_branch |= align_branch_jmp_bit;
13778 else if (strcasecmp (type, "call") == 0)
13779 align_branch |= align_branch_call_bit;
13780 else if (strcasecmp (type, "ret") == 0)
13781 align_branch |= align_branch_ret_bit;
13782 else if (strcasecmp (type, "indirect") == 0)
13783 align_branch |= align_branch_indirect_bit;
13784 else
13785 as_fatal (_("invalid -malign-branch= option: `%s'"), arg);
13786 type = next;
13787 }
13788 while (next != NULL);
13789 free (saved);
13790 break;
13791
76cf450b
L
13792 case OPTION_MBRANCHES_WITH_32B_BOUNDARIES:
13793 align_branch_power = 5;
13794 align_branch_prefix_size = 5;
13795 align_branch = (align_branch_jcc_bit
13796 | align_branch_fused_bit
13797 | align_branch_jmp_bit);
13798 break;
13799
5db04b09 13800 case OPTION_MAMD64:
4b5aaf5f 13801 isa64 = amd64;
5db04b09
L
13802 break;
13803
13804 case OPTION_MINTEL64:
4b5aaf5f 13805 isa64 = intel64;
5db04b09
L
13806 break;
13807
b6f8c7c4
L
13808 case 'O':
13809 if (arg == NULL)
13810 {
13811 optimize = 1;
13812 /* Turn off -Os. */
13813 optimize_for_space = 0;
13814 }
13815 else if (*arg == 's')
13816 {
13817 optimize_for_space = 1;
13818 /* Turn on all encoding optimizations. */
41fd2579 13819 optimize = INT_MAX;
b6f8c7c4
L
13820 }
13821 else
13822 {
13823 optimize = atoi (arg);
13824 /* Turn off -Os. */
13825 optimize_for_space = 0;
13826 }
13827 break;
13828
252b5132
RH
13829 default:
13830 return 0;
13831 }
13832 return 1;
13833}
13834
8a2c8fef
L
13835#define MESSAGE_TEMPLATE \
13836" "
13837
293f5f65
L
13838static char *
13839output_message (FILE *stream, char *p, char *message, char *start,
13840 int *left_p, const char *name, int len)
13841{
13842 int size = sizeof (MESSAGE_TEMPLATE);
13843 int left = *left_p;
13844
13845 /* Reserve 2 spaces for ", " or ",\0" */
13846 left -= len + 2;
13847
13848 /* Check if there is any room. */
13849 if (left >= 0)
13850 {
13851 if (p != start)
13852 {
13853 *p++ = ',';
13854 *p++ = ' ';
13855 }
13856 p = mempcpy (p, name, len);
13857 }
13858 else
13859 {
13860 /* Output the current message now and start a new one. */
13861 *p++ = ',';
13862 *p = '\0';
13863 fprintf (stream, "%s\n", message);
13864 p = start;
13865 left = size - (start - message) - len - 2;
13866
13867 gas_assert (left >= 0);
13868
13869 p = mempcpy (p, name, len);
13870 }
13871
13872 *left_p = left;
13873 return p;
13874}
13875
8a2c8fef 13876static void
1ded5609 13877show_arch (FILE *stream, int ext, int check)
8a2c8fef
L
13878{
13879 static char message[] = MESSAGE_TEMPLATE;
13880 char *start = message + 27;
13881 char *p;
13882 int size = sizeof (MESSAGE_TEMPLATE);
13883 int left;
13884 const char *name;
13885 int len;
13886 unsigned int j;
13887
13888 p = start;
13889 left = size - (start - message);
3ce2ebcf
JB
13890
13891 if (!ext && check)
13892 {
13893 p = output_message (stream, p, message, start, &left,
13894 STRING_COMMA_LEN ("default"));
f68697e8
JB
13895 p = output_message (stream, p, message, start, &left,
13896 STRING_COMMA_LEN ("push"));
13897 p = output_message (stream, p, message, start, &left,
13898 STRING_COMMA_LEN ("pop"));
3ce2ebcf
JB
13899 }
13900
8a2c8fef
L
13901 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13902 {
13903 /* Should it be skipped? */
13904 if (cpu_arch [j].skip)
13905 continue;
13906
13907 name = cpu_arch [j].name;
13908 len = cpu_arch [j].len;
ae89daec 13909 if (cpu_arch[j].type == PROCESSOR_NONE)
8a2c8fef
L
13910 {
13911 /* It is an extension. Skip if we aren't asked to show it. */
ae89daec 13912 if (!ext || cpu_flags_all_zero (&cpu_arch[j].enable))
8a2c8fef
L
13913 continue;
13914 }
13915 else if (ext)
13916 {
13917 /* It is an processor. Skip if we show only extension. */
13918 continue;
13919 }
ae89daec 13920 else if (check && ! cpu_arch[j].enable.bitfield.cpui386)
1ded5609
JB
13921 {
13922 /* It is an impossible processor - skip. */
13923 continue;
13924 }
8a2c8fef 13925
293f5f65 13926 p = output_message (stream, p, message, start, &left, name, len);
8a2c8fef
L
13927 }
13928
293f5f65
L
13929 /* Display disabled extensions. */
13930 if (ext)
ae89daec 13931 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
293f5f65 13932 {
ae89daec
JB
13933 char *str;
13934
13935 if (cpu_arch[j].type != PROCESSOR_NONE
13936 || !cpu_flags_all_zero (&cpu_arch[j].enable))
13937 continue;
13938 str = xasprintf ("no%s", cpu_arch[j].name);
13939 p = output_message (stream, p, message, start, &left, str,
13940 strlen (str));
13941 free (str);
293f5f65
L
13942 }
13943
8a2c8fef
L
13944 *p = '\0';
13945 fprintf (stream, "%s\n", message);
13946}
13947
252b5132 13948void
8a2c8fef 13949md_show_usage (FILE *stream)
252b5132 13950{
4cc782b5
ILT
13951#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13952 fprintf (stream, _("\
d4693039 13953 -Qy, -Qn ignored\n\
a38cf1db 13954 -V print assembler version number\n\
b3b91714
AM
13955 -k ignored\n"));
13956#endif
13957 fprintf (stream, _("\
7ebd68d1
NC
13958 -n do not optimize code alignment\n\
13959 -O{012s} attempt some code optimizations\n\
b3b91714
AM
13960 -q quieten some warnings\n"));
13961#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13962 fprintf (stream, _("\
a38cf1db 13963 -s ignored\n"));
b3b91714 13964#endif
b00af7c8
JB
13965#ifdef BFD64
13966# if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13967 fprintf (stream, _("\
13968 --32/--64/--x32 generate 32bit/64bit/x32 object\n"));
13969# elif defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O)
751d281c 13970 fprintf (stream, _("\
b00af7c8
JB
13971 --32/--64 generate 32bit/64bit object\n"));
13972# endif
751d281c 13973#endif
b3b91714
AM
13974#ifdef SVR4_COMMENT_CHARS
13975 fprintf (stream, _("\
13976 --divide do not treat `/' as a comment character\n"));
a38cf1db
AM
13977#else
13978 fprintf (stream, _("\
b3b91714 13979 --divide ignored\n"));
4cc782b5 13980#endif
9103f4f4 13981 fprintf (stream, _("\
6305a203 13982 -march=CPU[,+EXTENSION...]\n\
8a2c8fef 13983 generate code for CPU and EXTENSION, CPU is one of:\n"));
1ded5609 13984 show_arch (stream, 0, 1);
8a2c8fef 13985 fprintf (stream, _("\
ae89daec 13986 EXTENSION is combination of (possibly \"no\"-prefixed):\n"));
1ded5609 13987 show_arch (stream, 1, 0);
6305a203 13988 fprintf (stream, _("\
8a2c8fef 13989 -mtune=CPU optimize for CPU, CPU is one of:\n"));
1ded5609 13990 show_arch (stream, 0, 0);
ba104c83 13991 fprintf (stream, _("\
c0f3af97
L
13992 -msse2avx encode SSE instructions with VEX prefix\n"));
13993 fprintf (stream, _("\
c8480b58
L
13994 -muse-unaligned-vector-move\n\
13995 encode aligned vector move as unaligned vector move\n"));
13996 fprintf (stream, _("\
7c5c05ef 13997 -msse-check=[none|error|warning] (default: warning)\n\
daf50ae7
L
13998 check SSE instructions\n"));
13999 fprintf (stream, _("\
7c5c05ef 14000 -moperand-check=[none|error|warning] (default: warning)\n\
7bab8ab5
JB
14001 check operand combinations for validity\n"));
14002 fprintf (stream, _("\
7c5c05ef
L
14003 -mavxscalar=[128|256] (default: 128)\n\
14004 encode scalar AVX instructions with specific vector\n\
539f890d
L
14005 length\n"));
14006 fprintf (stream, _("\
03751133
L
14007 -mvexwig=[0|1] (default: 0)\n\
14008 encode VEX instructions with specific VEX.W value\n\
14009 for VEX.W bit ignored instructions\n"));
14010 fprintf (stream, _("\
7c5c05ef
L
14011 -mevexlig=[128|256|512] (default: 128)\n\
14012 encode scalar EVEX instructions with specific vector\n\
43234a1e
L
14013 length\n"));
14014 fprintf (stream, _("\
7c5c05ef
L
14015 -mevexwig=[0|1] (default: 0)\n\
14016 encode EVEX instructions with specific EVEX.W value\n\
43234a1e
L
14017 for EVEX.W bit ignored instructions\n"));
14018 fprintf (stream, _("\
7c5c05ef 14019 -mevexrcig=[rne|rd|ru|rz] (default: rne)\n\
d3d3c6db
IT
14020 encode EVEX instructions with specific EVEX.RC value\n\
14021 for SAE-only ignored instructions\n"));
14022 fprintf (stream, _("\
7c5c05ef
L
14023 -mmnemonic=[att|intel] "));
14024 if (SYSV386_COMPAT)
14025 fprintf (stream, _("(default: att)\n"));
14026 else
14027 fprintf (stream, _("(default: intel)\n"));
14028 fprintf (stream, _("\
14029 use AT&T/Intel mnemonic\n"));
ba104c83 14030 fprintf (stream, _("\
7c5c05ef
L
14031 -msyntax=[att|intel] (default: att)\n\
14032 use AT&T/Intel syntax\n"));
ba104c83
L
14033 fprintf (stream, _("\
14034 -mindex-reg support pseudo index registers\n"));
14035 fprintf (stream, _("\
14036 -mnaked-reg don't require `%%' prefix for registers\n"));
14037 fprintf (stream, _("\
7e8b059b 14038 -madd-bnd-prefix add BND prefix for all valid branches\n"));
b4a3a7b4 14039#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8dcea932
L
14040 fprintf (stream, _("\
14041 -mshared disable branch optimization for shared code\n"));
b4a3a7b4
L
14042 fprintf (stream, _("\
14043 -mx86-used-note=[no|yes] "));
14044 if (DEFAULT_X86_USED_NOTE)
14045 fprintf (stream, _("(default: yes)\n"));
14046 else
14047 fprintf (stream, _("(default: no)\n"));
14048 fprintf (stream, _("\
14049 generate x86 used ISA and feature properties\n"));
14050#endif
14051#if defined (TE_PE) || defined (TE_PEP)
167ad85b
TG
14052 fprintf (stream, _("\
14053 -mbig-obj generate big object files\n"));
14054#endif
d022bddd 14055 fprintf (stream, _("\
7c5c05ef 14056 -momit-lock-prefix=[no|yes] (default: no)\n\
d022bddd 14057 strip all lock prefixes\n"));
5db04b09 14058 fprintf (stream, _("\
7c5c05ef 14059 -mfence-as-lock-add=[no|yes] (default: no)\n\
e4e00185
AS
14060 encode lfence, mfence and sfence as\n\
14061 lock addl $0x0, (%%{re}sp)\n"));
14062 fprintf (stream, _("\
7c5c05ef
L
14063 -mrelax-relocations=[no|yes] "));
14064 if (DEFAULT_GENERATE_X86_RELAX_RELOCATIONS)
14065 fprintf (stream, _("(default: yes)\n"));
14066 else
14067 fprintf (stream, _("(default: no)\n"));
14068 fprintf (stream, _("\
0cb4071e
L
14069 generate relax relocations\n"));
14070 fprintf (stream, _("\
e379e5f3
L
14071 -malign-branch-boundary=NUM (default: 0)\n\
14072 align branches within NUM byte boundary\n"));
14073 fprintf (stream, _("\
14074 -malign-branch=TYPE[+TYPE...] (default: jcc+fused+jmp)\n\
14075 TYPE is combination of jcc, fused, jmp, call, ret,\n\
14076 indirect\n\
14077 specify types of branches to align\n"));
14078 fprintf (stream, _("\
14079 -malign-branch-prefix-size=NUM (default: 5)\n\
14080 align branches with NUM prefixes per instruction\n"));
14081 fprintf (stream, _("\
76cf450b
L
14082 -mbranches-within-32B-boundaries\n\
14083 align branches within 32 byte boundary\n"));
14084 fprintf (stream, _("\
ae531041
L
14085 -mlfence-after-load=[no|yes] (default: no)\n\
14086 generate lfence after load\n"));
14087 fprintf (stream, _("\
14088 -mlfence-before-indirect-branch=[none|all|register|memory] (default: none)\n\
14089 generate lfence before indirect near branch\n"));
14090 fprintf (stream, _("\
a09f656b 14091 -mlfence-before-ret=[none|or|not|shl|yes] (default: none)\n\
ae531041
L
14092 generate lfence before ret\n"));
14093 fprintf (stream, _("\
7c5c05ef 14094 -mamd64 accept only AMD64 ISA [default]\n"));
5db04b09
L
14095 fprintf (stream, _("\
14096 -mintel64 accept only Intel64 ISA\n"));
252b5132
RH
14097}
14098
3e73aa7c 14099#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
321098a5 14100 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
e57f8c65 14101 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
252b5132
RH
14102
14103/* Pick the target format to use. */
14104
47926f60 14105const char *
e3bb37b5 14106i386_target_format (void)
252b5132 14107{
d34049e8 14108 if (startswith (default_arch, "x86_64"))
351f65ca
L
14109 {
14110 update_code_flag (CODE_64BIT, 1);
14111 if (default_arch[6] == '\0')
7f56bc95 14112 x86_elf_abi = X86_64_ABI;
351f65ca 14113 else
7f56bc95 14114 x86_elf_abi = X86_64_X32_ABI;
351f65ca 14115 }
3e73aa7c 14116 else if (!strcmp (default_arch, "i386"))
78f12dd3 14117 update_code_flag (CODE_32BIT, 1);
5197d474
L
14118 else if (!strcmp (default_arch, "iamcu"))
14119 {
14120 update_code_flag (CODE_32BIT, 1);
14121 if (cpu_arch_isa == PROCESSOR_UNKNOWN)
14122 {
14123 static const i386_cpu_flags iamcu_flags = CPU_IAMCU_FLAGS;
14124 cpu_arch_name = "iamcu";
d92c7521 14125 free (cpu_sub_arch_name);
5197d474
L
14126 cpu_sub_arch_name = NULL;
14127 cpu_arch_flags = iamcu_flags;
14128 cpu_arch_isa = PROCESSOR_IAMCU;
14129 cpu_arch_isa_flags = iamcu_flags;
14130 if (!cpu_arch_tune_set)
14131 {
14132 cpu_arch_tune = cpu_arch_isa;
14133 cpu_arch_tune_flags = cpu_arch_isa_flags;
14134 }
14135 }
8d471ec1 14136 else if (cpu_arch_isa != PROCESSOR_IAMCU)
5197d474
L
14137 as_fatal (_("Intel MCU doesn't support `%s' architecture"),
14138 cpu_arch_name);
14139 }
3e73aa7c 14140 else
2b5d6a91 14141 as_fatal (_("unknown architecture"));
89507696
JB
14142
14143 if (cpu_flags_all_zero (&cpu_arch_isa_flags))
ae89daec 14144 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
89507696 14145 if (cpu_flags_all_zero (&cpu_arch_tune_flags))
ae89daec 14146 cpu_arch_tune_flags = cpu_arch[flag_code == CODE_64BIT].enable;
89507696 14147
252b5132
RH
14148 switch (OUTPUT_FLAVOR)
14149 {
9384f2ff 14150#if defined (OBJ_MAYBE_AOUT) || defined (OBJ_AOUT)
4c63da97 14151 case bfd_target_aout_flavour:
47926f60 14152 return AOUT_TARGET_FORMAT;
4c63da97 14153#endif
9384f2ff
AM
14154#if defined (OBJ_MAYBE_COFF) || defined (OBJ_COFF)
14155# if defined (TE_PE) || defined (TE_PEP)
14156 case bfd_target_coff_flavour:
167ad85b 14157 if (flag_code == CODE_64BIT)
eb19308f
JB
14158 {
14159 object_64bit = 1;
14160 return use_big_obj ? "pe-bigobj-x86-64" : "pe-x86-64";
14161 }
14162 return use_big_obj ? "pe-bigobj-i386" : "pe-i386";
9384f2ff 14163# elif defined (TE_GO32)
0561d57c
JK
14164 case bfd_target_coff_flavour:
14165 return "coff-go32";
9384f2ff 14166# else
252b5132
RH
14167 case bfd_target_coff_flavour:
14168 return "coff-i386";
9384f2ff 14169# endif
4c63da97 14170#endif
3e73aa7c 14171#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
252b5132 14172 case bfd_target_elf_flavour:
3e73aa7c 14173 {
351f65ca
L
14174 const char *format;
14175
14176 switch (x86_elf_abi)
4fa24527 14177 {
351f65ca
L
14178 default:
14179 format = ELF_TARGET_FORMAT;
e379e5f3
L
14180#ifndef TE_SOLARIS
14181 tls_get_addr = "___tls_get_addr";
14182#endif
351f65ca 14183 break;
7f56bc95 14184 case X86_64_ABI:
351f65ca 14185 use_rela_relocations = 1;
4fa24527 14186 object_64bit = 1;
e379e5f3
L
14187#ifndef TE_SOLARIS
14188 tls_get_addr = "__tls_get_addr";
14189#endif
351f65ca
L
14190 format = ELF_TARGET_FORMAT64;
14191 break;
7f56bc95 14192 case X86_64_X32_ABI:
4fa24527 14193 use_rela_relocations = 1;
351f65ca 14194 object_64bit = 1;
e379e5f3
L
14195#ifndef TE_SOLARIS
14196 tls_get_addr = "__tls_get_addr";
14197#endif
862be3fb 14198 disallow_64bit_reloc = 1;
351f65ca
L
14199 format = ELF_TARGET_FORMAT32;
14200 break;
4fa24527 14201 }
c085ab00 14202 if (cpu_arch_isa == PROCESSOR_IAMCU)
81486035
L
14203 {
14204 if (x86_elf_abi != I386_ABI)
14205 as_fatal (_("Intel MCU is 32bit only"));
14206 return ELF_TARGET_IAMCU_FORMAT;
14207 }
8a9036a4 14208 else
351f65ca 14209 return format;
3e73aa7c 14210 }
e57f8c65
TG
14211#endif
14212#if defined (OBJ_MACH_O)
14213 case bfd_target_mach_o_flavour:
d382c579
TG
14214 if (flag_code == CODE_64BIT)
14215 {
14216 use_rela_relocations = 1;
14217 object_64bit = 1;
14218 return "mach-o-x86-64";
14219 }
14220 else
14221 return "mach-o-i386";
4c63da97 14222#endif
252b5132
RH
14223 default:
14224 abort ();
14225 return NULL;
14226 }
14227}
14228
47926f60 14229#endif /* OBJ_MAYBE_ more than one */
252b5132 14230\f
252b5132 14231symbolS *
7016a5d5 14232md_undefined_symbol (char *name)
252b5132 14233{
18dc2407
ILT
14234 if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
14235 && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
14236 && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
14237 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
24eab124
AM
14238 {
14239 if (!GOT_symbol)
14240 {
14241 if (symbol_find (name))
14242 as_bad (_("GOT already in symbol table"));
14243 GOT_symbol = symbol_new (name, undefined_section,
e01e1cee 14244 &zero_address_frag, 0);
24eab124
AM
14245 };
14246 return GOT_symbol;
14247 }
252b5132
RH
14248 return 0;
14249}
14250
14251/* Round up a section size to the appropriate boundary. */
47926f60 14252
252b5132 14253valueT
7016a5d5 14254md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
252b5132 14255{
4c63da97
AM
14256#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
14257 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
14258 {
14259 /* For a.out, force the section size to be aligned. If we don't do
14260 this, BFD will align it for us, but it will not write out the
14261 final bytes of the section. This may be a bug in BFD, but it is
14262 easier to fix it here since that is how the other a.out targets
14263 work. */
14264 int align;
14265
fd361982 14266 align = bfd_section_alignment (segment);
8d3842cd 14267 size = ((size + (1 << align) - 1) & (-((valueT) 1 << align)));
4c63da97 14268 }
252b5132
RH
14269#endif
14270
14271 return size;
14272}
14273
14274/* On the i386, PC-relative offsets are relative to the start of the
14275 next instruction. That is, the address of the offset, plus its
14276 size, since the offset is always the last part of the insn. */
14277
14278long
e3bb37b5 14279md_pcrel_from (fixS *fixP)
252b5132
RH
14280{
14281 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
14282}
14283
14284#ifndef I386COFF
14285
14286static void
e3bb37b5 14287s_bss (int ignore ATTRIBUTE_UNUSED)
252b5132 14288{
29b0f896 14289 int temp;
252b5132 14290
8a75718c
JB
14291#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14292 if (IS_ELF)
14293 obj_elf_section_change_hook ();
14294#endif
252b5132
RH
14295 temp = get_absolute_expression ();
14296 subseg_set (bss_section, (subsegT) temp);
14297 demand_empty_rest_of_line ();
14298}
14299
14300#endif
14301
e379e5f3
L
14302/* Remember constant directive. */
14303
14304void
14305i386_cons_align (int ignore ATTRIBUTE_UNUSED)
14306{
14307 if (last_insn.kind != last_insn_directive
14308 && (bfd_section_flags (now_seg) & SEC_CODE))
14309 {
14310 last_insn.seg = now_seg;
14311 last_insn.kind = last_insn_directive;
14312 last_insn.name = "constant directive";
14313 last_insn.file = as_where (&last_insn.line);
ae531041
L
14314 if (lfence_before_ret != lfence_before_ret_none)
14315 {
14316 if (lfence_before_indirect_branch != lfence_branch_none)
14317 as_warn (_("constant directive skips -mlfence-before-ret "
14318 "and -mlfence-before-indirect-branch"));
14319 else
14320 as_warn (_("constant directive skips -mlfence-before-ret"));
14321 }
14322 else if (lfence_before_indirect_branch != lfence_branch_none)
14323 as_warn (_("constant directive skips -mlfence-before-indirect-branch"));
e379e5f3
L
14324 }
14325}
14326
3abbafc2 14327int
e3bb37b5 14328i386_validate_fix (fixS *fixp)
252b5132 14329{
e52a16f2
JB
14330 if (fixp->fx_addsy && S_GET_SEGMENT(fixp->fx_addsy) == reg_section)
14331 {
14332 reloc_howto_type *howto;
14333
14334 howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
14335 as_bad_where (fixp->fx_file, fixp->fx_line,
14336 _("invalid %s relocation against register"),
14337 howto ? howto->name : "<unknown>");
14338 return 0;
14339 }
14340
3abbafc2
JB
14341#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14342 if (fixp->fx_r_type == BFD_RELOC_SIZE32
14343 || fixp->fx_r_type == BFD_RELOC_SIZE64)
14344 return IS_ELF && fixp->fx_addsy
14345 && (!S_IS_DEFINED (fixp->fx_addsy)
14346 || S_IS_EXTERNAL (fixp->fx_addsy));
14347#endif
14348
02a86693 14349 if (fixp->fx_subsy)
252b5132 14350 {
02a86693 14351 if (fixp->fx_subsy == GOT_symbol)
23df1078 14352 {
02a86693
L
14353 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
14354 {
14355 if (!object_64bit)
14356 abort ();
14357#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14358 if (fixp->fx_tcbit2)
56ceb5b5
L
14359 fixp->fx_r_type = (fixp->fx_tcbit
14360 ? BFD_RELOC_X86_64_REX_GOTPCRELX
14361 : BFD_RELOC_X86_64_GOTPCRELX);
02a86693
L
14362 else
14363#endif
14364 fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
14365 }
d6ab8113 14366 else
02a86693
L
14367 {
14368 if (!object_64bit)
14369 fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
14370 else
14371 fixp->fx_r_type = BFD_RELOC_X86_64_GOTOFF64;
14372 }
14373 fixp->fx_subsy = 0;
23df1078 14374 }
252b5132 14375 }
02a86693 14376#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2585b7a5 14377 else
02a86693 14378 {
2585b7a5
L
14379 /* NB: Commit 292676c1 resolved PLT32 reloc aganst local symbol
14380 to section. Since PLT32 relocation must be against symbols,
14381 turn such PLT32 relocation into PC32 relocation. */
14382 if (fixp->fx_addsy
14383 && (fixp->fx_r_type == BFD_RELOC_386_PLT32
14384 || fixp->fx_r_type == BFD_RELOC_X86_64_PLT32)
14385 && symbol_section_p (fixp->fx_addsy))
14386 fixp->fx_r_type = BFD_RELOC_32_PCREL;
14387 if (!object_64bit)
14388 {
14389 if (fixp->fx_r_type == BFD_RELOC_386_GOT32
14390 && fixp->fx_tcbit2)
14391 fixp->fx_r_type = BFD_RELOC_386_GOT32X;
14392 }
02a86693
L
14393 }
14394#endif
3abbafc2
JB
14395
14396 return 1;
252b5132
RH
14397}
14398
252b5132 14399arelent *
7016a5d5 14400tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
14401{
14402 arelent *rel;
14403 bfd_reloc_code_real_type code;
14404
14405 switch (fixp->fx_r_type)
14406 {
8ce3d284 14407#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3abbafc2
JB
14408 symbolS *sym;
14409
8fd4256d
L
14410 case BFD_RELOC_SIZE32:
14411 case BFD_RELOC_SIZE64:
3abbafc2
JB
14412 if (fixp->fx_addsy
14413 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))
14414 && (!fixp->fx_subsy
14415 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))))
14416 sym = fixp->fx_addsy;
14417 else if (fixp->fx_subsy
14418 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))
14419 && (!fixp->fx_addsy
14420 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))))
14421 sym = fixp->fx_subsy;
14422 else
14423 sym = NULL;
14424 if (IS_ELF && sym && S_IS_DEFINED (sym) && !S_IS_EXTERNAL (sym))
8fd4256d
L
14425 {
14426 /* Resolve size relocation against local symbol to size of
14427 the symbol plus addend. */
3abbafc2 14428 valueT value = S_GET_SIZE (sym);
44f87162 14429
3abbafc2
JB
14430 if (symbol_get_bfdsym (sym)->flags & BSF_SECTION_SYM)
14431 value = bfd_section_size (S_GET_SEGMENT (sym));
14432 if (sym == fixp->fx_subsy)
14433 {
14434 value = -value;
14435 if (fixp->fx_addsy)
14436 value += S_GET_VALUE (fixp->fx_addsy);
14437 }
14438 else if (fixp->fx_subsy)
14439 value -= S_GET_VALUE (fixp->fx_subsy);
44f87162 14440 value += fixp->fx_offset;
8fd4256d 14441 if (fixp->fx_r_type == BFD_RELOC_SIZE32
d965814f 14442 && object_64bit
8fd4256d
L
14443 && !fits_in_unsigned_long (value))
14444 as_bad_where (fixp->fx_file, fixp->fx_line,
14445 _("symbol size computation overflow"));
14446 fixp->fx_addsy = NULL;
14447 fixp->fx_subsy = NULL;
14448 md_apply_fix (fixp, (valueT *) &value, NULL);
14449 return NULL;
14450 }
3abbafc2
JB
14451 if (!fixp->fx_addsy || fixp->fx_subsy)
14452 {
14453 as_bad_where (fixp->fx_file, fixp->fx_line,
14454 "unsupported expression involving @size");
14455 return NULL;
14456 }
8ce3d284 14457#endif
1a0670f3 14458 /* Fall through. */
8fd4256d 14459
3e73aa7c
JH
14460 case BFD_RELOC_X86_64_PLT32:
14461 case BFD_RELOC_X86_64_GOT32:
14462 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
14463 case BFD_RELOC_X86_64_GOTPCRELX:
14464 case BFD_RELOC_X86_64_REX_GOTPCRELX:
252b5132
RH
14465 case BFD_RELOC_386_PLT32:
14466 case BFD_RELOC_386_GOT32:
02a86693 14467 case BFD_RELOC_386_GOT32X:
252b5132
RH
14468 case BFD_RELOC_386_GOTOFF:
14469 case BFD_RELOC_386_GOTPC:
13ae64f3
JJ
14470 case BFD_RELOC_386_TLS_GD:
14471 case BFD_RELOC_386_TLS_LDM:
14472 case BFD_RELOC_386_TLS_LDO_32:
14473 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
14474 case BFD_RELOC_386_TLS_IE:
14475 case BFD_RELOC_386_TLS_GOTIE:
13ae64f3
JJ
14476 case BFD_RELOC_386_TLS_LE_32:
14477 case BFD_RELOC_386_TLS_LE:
67a4f2b7
AO
14478 case BFD_RELOC_386_TLS_GOTDESC:
14479 case BFD_RELOC_386_TLS_DESC_CALL:
bffbf940
JJ
14480 case BFD_RELOC_X86_64_TLSGD:
14481 case BFD_RELOC_X86_64_TLSLD:
14482 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 14483 case BFD_RELOC_X86_64_DTPOFF64:
bffbf940
JJ
14484 case BFD_RELOC_X86_64_GOTTPOFF:
14485 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113
JB
14486 case BFD_RELOC_X86_64_TPOFF64:
14487 case BFD_RELOC_X86_64_GOTOFF64:
14488 case BFD_RELOC_X86_64_GOTPC32:
7b81dfbb
AJ
14489 case BFD_RELOC_X86_64_GOT64:
14490 case BFD_RELOC_X86_64_GOTPCREL64:
14491 case BFD_RELOC_X86_64_GOTPC64:
14492 case BFD_RELOC_X86_64_GOTPLT64:
14493 case BFD_RELOC_X86_64_PLTOFF64:
67a4f2b7
AO
14494 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14495 case BFD_RELOC_X86_64_TLSDESC_CALL:
252b5132
RH
14496 case BFD_RELOC_RVA:
14497 case BFD_RELOC_VTABLE_ENTRY:
14498 case BFD_RELOC_VTABLE_INHERIT:
6482c264
NC
14499#ifdef TE_PE
14500 case BFD_RELOC_32_SECREL:
145667f8 14501 case BFD_RELOC_16_SECIDX:
6482c264 14502#endif
252b5132
RH
14503 code = fixp->fx_r_type;
14504 break;
dbbaec26
L
14505 case BFD_RELOC_X86_64_32S:
14506 if (!fixp->fx_pcrel)
14507 {
14508 /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32. */
14509 code = fixp->fx_r_type;
14510 break;
14511 }
1a0670f3 14512 /* Fall through. */
252b5132 14513 default:
93382f6d 14514 if (fixp->fx_pcrel)
252b5132 14515 {
93382f6d
AM
14516 switch (fixp->fx_size)
14517 {
14518 default:
b091f402
AM
14519 as_bad_where (fixp->fx_file, fixp->fx_line,
14520 _("can not do %d byte pc-relative relocation"),
14521 fixp->fx_size);
93382f6d
AM
14522 code = BFD_RELOC_32_PCREL;
14523 break;
14524 case 1: code = BFD_RELOC_8_PCREL; break;
14525 case 2: code = BFD_RELOC_16_PCREL; break;
d258b828 14526 case 4: code = BFD_RELOC_32_PCREL; break;
d6ab8113
JB
14527#ifdef BFD64
14528 case 8: code = BFD_RELOC_64_PCREL; break;
14529#endif
93382f6d
AM
14530 }
14531 }
14532 else
14533 {
14534 switch (fixp->fx_size)
14535 {
14536 default:
b091f402
AM
14537 as_bad_where (fixp->fx_file, fixp->fx_line,
14538 _("can not do %d byte relocation"),
14539 fixp->fx_size);
93382f6d
AM
14540 code = BFD_RELOC_32;
14541 break;
14542 case 1: code = BFD_RELOC_8; break;
14543 case 2: code = BFD_RELOC_16; break;
14544 case 4: code = BFD_RELOC_32; break;
937149dd 14545#ifdef BFD64
3e73aa7c 14546 case 8: code = BFD_RELOC_64; break;
937149dd 14547#endif
93382f6d 14548 }
252b5132
RH
14549 }
14550 break;
14551 }
252b5132 14552
d182319b
JB
14553 if ((code == BFD_RELOC_32
14554 || code == BFD_RELOC_32_PCREL
14555 || code == BFD_RELOC_X86_64_32S)
252b5132
RH
14556 && GOT_symbol
14557 && fixp->fx_addsy == GOT_symbol)
3e73aa7c 14558 {
4fa24527 14559 if (!object_64bit)
d6ab8113
JB
14560 code = BFD_RELOC_386_GOTPC;
14561 else
14562 code = BFD_RELOC_X86_64_GOTPC32;
3e73aa7c 14563 }
7b81dfbb
AJ
14564 if ((code == BFD_RELOC_64 || code == BFD_RELOC_64_PCREL)
14565 && GOT_symbol
14566 && fixp->fx_addsy == GOT_symbol)
14567 {
14568 code = BFD_RELOC_X86_64_GOTPC64;
14569 }
252b5132 14570
add39d23
TS
14571 rel = XNEW (arelent);
14572 rel->sym_ptr_ptr = XNEW (asymbol *);
49309057 14573 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
14574
14575 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
c87db184 14576
3e73aa7c
JH
14577 if (!use_rela_relocations)
14578 {
14579 /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
14580 vtable entry to be used in the relocation's section offset. */
14581 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
14582 rel->address = fixp->fx_offset;
fbeb56a4
DK
14583#if defined (OBJ_COFF) && defined (TE_PE)
14584 else if (fixp->fx_addsy && S_IS_WEAK (fixp->fx_addsy))
14585 rel->addend = fixp->fx_addnumber - (S_GET_VALUE (fixp->fx_addsy) * 2);
14586 else
14587#endif
c6682705 14588 rel->addend = 0;
3e73aa7c
JH
14589 }
14590 /* Use the rela in 64bit mode. */
252b5132 14591 else
3e73aa7c 14592 {
862be3fb
L
14593 if (disallow_64bit_reloc)
14594 switch (code)
14595 {
862be3fb
L
14596 case BFD_RELOC_X86_64_DTPOFF64:
14597 case BFD_RELOC_X86_64_TPOFF64:
14598 case BFD_RELOC_64_PCREL:
14599 case BFD_RELOC_X86_64_GOTOFF64:
14600 case BFD_RELOC_X86_64_GOT64:
14601 case BFD_RELOC_X86_64_GOTPCREL64:
14602 case BFD_RELOC_X86_64_GOTPC64:
14603 case BFD_RELOC_X86_64_GOTPLT64:
14604 case BFD_RELOC_X86_64_PLTOFF64:
14605 as_bad_where (fixp->fx_file, fixp->fx_line,
14606 _("cannot represent relocation type %s in x32 mode"),
14607 bfd_get_reloc_code_name (code));
14608 break;
14609 default:
14610 break;
14611 }
14612
062cd5e7
AS
14613 if (!fixp->fx_pcrel)
14614 rel->addend = fixp->fx_offset;
14615 else
14616 switch (code)
14617 {
14618 case BFD_RELOC_X86_64_PLT32:
14619 case BFD_RELOC_X86_64_GOT32:
14620 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
14621 case BFD_RELOC_X86_64_GOTPCRELX:
14622 case BFD_RELOC_X86_64_REX_GOTPCRELX:
bffbf940
JJ
14623 case BFD_RELOC_X86_64_TLSGD:
14624 case BFD_RELOC_X86_64_TLSLD:
14625 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7
AO
14626 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14627 case BFD_RELOC_X86_64_TLSDESC_CALL:
062cd5e7
AS
14628 rel->addend = fixp->fx_offset - fixp->fx_size;
14629 break;
14630 default:
14631 rel->addend = (section->vma
14632 - fixp->fx_size
14633 + fixp->fx_addnumber
14634 + md_pcrel_from (fixp));
14635 break;
14636 }
3e73aa7c
JH
14637 }
14638
252b5132
RH
14639 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
14640 if (rel->howto == NULL)
14641 {
14642 as_bad_where (fixp->fx_file, fixp->fx_line,
d0b47220 14643 _("cannot represent relocation type %s"),
252b5132
RH
14644 bfd_get_reloc_code_name (code));
14645 /* Set howto to a garbage value so that we can keep going. */
14646 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
9c2799c2 14647 gas_assert (rel->howto != NULL);
252b5132
RH
14648 }
14649
14650 return rel;
14651}
14652
ee86248c 14653#include "tc-i386-intel.c"
54cfded0 14654
a60de03c
JB
14655void
14656tc_x86_parse_to_dw2regnum (expressionS *exp)
54cfded0 14657{
a60de03c
JB
14658 int saved_naked_reg;
14659 char saved_register_dot;
54cfded0 14660
a60de03c
JB
14661 saved_naked_reg = allow_naked_reg;
14662 allow_naked_reg = 1;
14663 saved_register_dot = register_chars['.'];
14664 register_chars['.'] = '.';
14665 allow_pseudo_reg = 1;
14666 expression_and_evaluate (exp);
14667 allow_pseudo_reg = 0;
14668 register_chars['.'] = saved_register_dot;
14669 allow_naked_reg = saved_naked_reg;
14670
e96d56a1 14671 if (exp->X_op == O_register && exp->X_add_number >= 0)
54cfded0 14672 {
a60de03c
JB
14673 if ((addressT) exp->X_add_number < i386_regtab_size)
14674 {
14675 exp->X_op = O_constant;
14676 exp->X_add_number = i386_regtab[exp->X_add_number]
14677 .dw2_regnum[flag_code >> 1];
14678 }
14679 else
14680 exp->X_op = O_illegal;
54cfded0 14681 }
54cfded0
AM
14682}
14683
14684void
14685tc_x86_frame_initial_instructions (void)
14686{
a60de03c
JB
14687 static unsigned int sp_regno[2];
14688
14689 if (!sp_regno[flag_code >> 1])
14690 {
14691 char *saved_input = input_line_pointer;
14692 char sp[][4] = {"esp", "rsp"};
14693 expressionS exp;
a4447b93 14694
a60de03c
JB
14695 input_line_pointer = sp[flag_code >> 1];
14696 tc_x86_parse_to_dw2regnum (&exp);
9c2799c2 14697 gas_assert (exp.X_op == O_constant);
a60de03c
JB
14698 sp_regno[flag_code >> 1] = exp.X_add_number;
14699 input_line_pointer = saved_input;
14700 }
a4447b93 14701
61ff971f
L
14702 cfi_add_CFA_def_cfa (sp_regno[flag_code >> 1], -x86_cie_data_alignment);
14703 cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
54cfded0 14704}
d2b2c203 14705
d7921315
L
14706int
14707x86_dwarf2_addr_size (void)
14708{
14709#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
14710 if (x86_elf_abi == X86_64_X32_ABI)
14711 return 4;
14712#endif
14713 return bfd_arch_bits_per_address (stdoutput) / 8;
14714}
14715
d2b2c203
DJ
14716int
14717i386_elf_section_type (const char *str, size_t len)
14718{
14719 if (flag_code == CODE_64BIT
14720 && len == sizeof ("unwind") - 1
d34049e8 14721 && startswith (str, "unwind"))
d2b2c203
DJ
14722 return SHT_X86_64_UNWIND;
14723
14724 return -1;
14725}
bb41ade5 14726
ad5fec3b
EB
14727#ifdef TE_SOLARIS
14728void
14729i386_solaris_fix_up_eh_frame (segT sec)
14730{
14731 if (flag_code == CODE_64BIT)
14732 elf_section_type (sec) = SHT_X86_64_UNWIND;
14733}
14734#endif
14735
bb41ade5
AM
14736#ifdef TE_PE
14737void
14738tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
14739{
91d6fa6a 14740 expressionS exp;
bb41ade5 14741
91d6fa6a
NC
14742 exp.X_op = O_secrel;
14743 exp.X_add_symbol = symbol;
14744 exp.X_add_number = 0;
14745 emit_expr (&exp, size);
bb41ade5
AM
14746}
14747#endif
3b22753a
L
14748
14749#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14750/* For ELF on x86-64, add support for SHF_X86_64_LARGE. */
14751
01e1a5bc 14752bfd_vma
6d4af3c2 14753x86_64_section_letter (int letter, const char **ptr_msg)
3b22753a
L
14754{
14755 if (flag_code == CODE_64BIT)
14756 {
14757 if (letter == 'l')
14758 return SHF_X86_64_LARGE;
14759
8f3bae45 14760 *ptr_msg = _("bad .section directive: want a,l,w,x,M,S,G,T in string");
64e74474 14761 }
3b22753a 14762 else
8f3bae45 14763 *ptr_msg = _("bad .section directive: want a,w,x,M,S,G,T in string");
3b22753a
L
14764 return -1;
14765}
14766
01e1a5bc 14767bfd_vma
3b22753a
L
14768x86_64_section_word (char *str, size_t len)
14769{
08dedd66 14770 if (len == 5 && flag_code == CODE_64BIT && startswith (str, "large"))
3b22753a
L
14771 return SHF_X86_64_LARGE;
14772
14773 return -1;
14774}
14775
14776static void
14777handle_large_common (int small ATTRIBUTE_UNUSED)
14778{
14779 if (flag_code != CODE_64BIT)
14780 {
14781 s_comm_internal (0, elf_common_parse);
14782 as_warn (_(".largecomm supported only in 64bit mode, producing .comm"));
14783 }
14784 else
14785 {
14786 static segT lbss_section;
14787 asection *saved_com_section_ptr = elf_com_section_ptr;
14788 asection *saved_bss_section = bss_section;
14789
14790 if (lbss_section == NULL)
14791 {
14792 flagword applicable;
14793 segT seg = now_seg;
14794 subsegT subseg = now_subseg;
14795
14796 /* The .lbss section is for local .largecomm symbols. */
14797 lbss_section = subseg_new (".lbss", 0);
14798 applicable = bfd_applicable_section_flags (stdoutput);
fd361982 14799 bfd_set_section_flags (lbss_section, applicable & SEC_ALLOC);
3b22753a
L
14800 seg_info (lbss_section)->bss = 1;
14801
14802 subseg_set (seg, subseg);
14803 }
14804
14805 elf_com_section_ptr = &_bfd_elf_large_com_section;
14806 bss_section = lbss_section;
14807
14808 s_comm_internal (0, elf_common_parse);
14809
14810 elf_com_section_ptr = saved_com_section_ptr;
14811 bss_section = saved_bss_section;
14812 }
14813}
14814#endif /* OBJ_ELF || OBJ_MAYBE_ELF */