]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/config/tc-i386.c
X86: Remove "i.rex" to eliminate extra conditional branch
[thirdparty/binutils-gdb.git] / gas / config / tc-i386.c
CommitLineData
b534c6d3 1/* tc-i386.c -- Assemble code for the Intel 80386
fd67aa11 2 Copyright (C) 1989-2024 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"
c7defc53 33#include "scfi.h"
b52c4ee4
IB
34#include "gen-sframe.h"
35#include "sframe.h"
d2b2c203 36#include "elf/x86-64.h"
40fb9820 37#include "opcodes/i386-init.h"
5c139202 38#include "opcodes/i386-mnem.h"
41fd2579 39#include <limits.h>
41fd2579 40
c3332e24 41#ifndef INFER_ADDR_PREFIX
eecb386c 42#define INFER_ADDR_PREFIX 1
c3332e24
AM
43#endif
44
29b0f896
AM
45#ifndef DEFAULT_ARCH
46#define DEFAULT_ARCH "i386"
246fcdee 47#endif
252b5132 48
edde18a5
AM
49#ifndef INLINE
50#if __GNUC__ >= 2
51#define INLINE __inline__
52#else
53#define INLINE
54#endif
55#endif
56
6305a203
L
57/* Prefixes will be emitted in the order defined below.
58 WAIT_PREFIX must be the first prefix since FWAIT is really is an
59 instruction, and so must come before any prefixes.
60 The preferred prefix order is SEG_PREFIX, ADDR_PREFIX, DATA_PREFIX,
42164a71 61 REP_PREFIX/HLE_PREFIX, LOCK_PREFIX. */
6305a203
L
62#define WAIT_PREFIX 0
63#define SEG_PREFIX 1
64#define ADDR_PREFIX 2
65#define DATA_PREFIX 3
c32fa91d 66#define REP_PREFIX 4
42164a71 67#define HLE_PREFIX REP_PREFIX
7e8b059b 68#define BND_PREFIX REP_PREFIX
c32fa91d 69#define LOCK_PREFIX 5
4e9ac44a
L
70#define REX_PREFIX 6 /* must come last. */
71#define MAX_PREFIXES 7 /* max prefixes per opcode */
6305a203
L
72
73/* we define the syntax here (modulo base,index,scale syntax) */
74#define REGISTER_PREFIX '%'
75#define IMMEDIATE_PREFIX '$'
76#define ABSOLUTE_PREFIX '*'
77
78/* these are the instruction mnemonic suffixes in AT&T syntax or
79 memory operand size in Intel syntax. */
80#define WORD_MNEM_SUFFIX 'w'
81#define BYTE_MNEM_SUFFIX 'b'
82#define SHORT_MNEM_SUFFIX 's'
83#define LONG_MNEM_SUFFIX 'l'
84#define QWORD_MNEM_SUFFIX 'q'
6305a203
L
85
86#define END_OF_INSN '\0'
87
05909f23
JB
88#define OPERAND_TYPE_NONE { .bitfield = { .class = ClassNone } }
89
79dec6b7
JB
90/* This matches the C -> StaticRounding alias in the opcode table. */
91#define commutative staticrounding
92
6305a203
L
93/*
94 'templates' is for grouping together 'template' structures for opcodes
95 of the same name. This is only used for storing the insns in the grand
96 ole hash table of insns.
97 The templates themselves start at START and range up to (but not including)
98 END.
99 */
100typedef struct
101{
d3ce72d0
NC
102 const insn_template *start;
103 const insn_template *end;
6305a203
L
104}
105templates;
106
107/* 386 operand encoding bytes: see 386 book for details of this. */
108typedef struct
109{
110 unsigned int regmem; /* codes register or memory operand */
111 unsigned int reg; /* codes register operand (or extended opcode) */
112 unsigned int mode; /* how to interpret regmem & reg */
113}
114modrm_byte;
115
116/* x86-64 extension prefix. */
117typedef int rex_byte;
118
6305a203
L
119/* 386 opcode byte to code indirect addressing. */
120typedef struct
121{
122 unsigned base;
123 unsigned index;
124 unsigned scale;
125}
126sib_byte;
127
6305a203
L
128/* x86 arch names, types and features */
129typedef struct
130{
131 const char *name; /* arch name */
6ceeed25
JB
132 unsigned int len:8; /* arch string length */
133 bool skip:1; /* show_arch should skip this. */
6305a203 134 enum processor_type type; /* arch type */
4fc85f37 135 enum { vsz_none, vsz_set, vsz_reset } vsz; /* vector size control */
ae89daec
JB
136 i386_cpu_flags enable; /* cpu feature enable flags */
137 i386_cpu_flags disable; /* cpu feature disable flags */
6305a203
L
138}
139arch_entry;
140
78f12dd3 141static void update_code_flag (int, int);
edd67638 142static void s_insn (int);
1e7dd4a0 143static void s_noopt (int);
e3bb37b5
L
144static void set_code_flag (int);
145static void set_16bit_gcc_code_flag (int);
146static void set_intel_syntax (int);
1efbbeb4 147static void set_intel_mnemonic (int);
db51cc60 148static void set_allow_index_reg (int);
7bab8ab5 149static void set_check (int);
e3bb37b5 150static void set_cpu_arch (int);
6482c264 151#ifdef TE_PE
e3bb37b5 152static void pe_directive_secrel (int);
145667f8 153static void pe_directive_secidx (int);
6482c264 154#endif
e3bb37b5
L
155static void signed_cons (int);
156static char *output_invalid (int c);
ee86248c
JB
157static int i386_finalize_immediate (segT, expressionS *, i386_operand_type,
158 const char *);
159static int i386_finalize_displacement (segT, expressionS *, i386_operand_type,
160 const char *);
a7619375 161static int i386_att_operand (char *);
e3bb37b5 162static int i386_intel_operand (char *, int);
ee86248c
JB
163static int i386_intel_simplify (expressionS *);
164static int i386_intel_parse_name (const char *, expressionS *);
4f081312 165static const reg_entry *parse_register (const char *, char **);
edd67638 166static const char *parse_insn (const char *, char *, bool);
e3bb37b5
L
167static char *parse_operands (char *, const char *);
168static void swap_operands (void);
783c187b 169static void swap_2_operands (unsigned int, unsigned int);
ed719294 170static enum i386_flag_code i386_addressing_mode (void);
e3bb37b5 171static void optimize_imm (void);
0de704b9 172static bool optimize_disp (const insn_template *t);
83b16ac6 173static const insn_template *match_template (char);
e3bb37b5
L
174static int check_string (void);
175static int process_suffix (void);
176static int check_byte_reg (void);
177static int check_long_reg (void);
178static int check_qword_reg (void);
179static int check_word_reg (void);
180static int finalize_imm (void);
181static int process_operands (void);
5e042380 182static const reg_entry *build_modrm_byte (void);
b5482fe5 183static void output_insn (const struct last_insn *);
e3bb37b5
L
184static void output_imm (fragS *, offsetT);
185static void output_disp (fragS *, offsetT);
d4064aad 186#ifdef OBJ_AOUT
e3bb37b5 187static void s_bss (int);
252b5132 188#endif
17d4e2a2
L
189#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
190static void handle_large_common (int small ATTRIBUTE_UNUSED);
b4a3a7b4
L
191
192/* GNU_PROPERTY_X86_ISA_1_USED. */
193static unsigned int x86_isa_1_used;
194/* GNU_PROPERTY_X86_FEATURE_2_USED. */
195static unsigned int x86_feature_2_used;
196/* Generate x86 used ISA and feature properties. */
197static unsigned int x86_used_note = DEFAULT_X86_USED_NOTE;
17d4e2a2 198#endif
252b5132 199
a847613f 200static const char *default_arch = DEFAULT_ARCH;
3e73aa7c 201
8a6fb3f9
JB
202/* parse_register() returns this when a register alias cannot be used. */
203static const reg_entry bad_reg = { "<bad>", OPERAND_TYPE_NONE, 0, 0,
204 { Dw2Inval, Dw2Inval } };
205
34684862 206static const reg_entry *reg_eax;
5e042380
JB
207static const reg_entry *reg_ds;
208static const reg_entry *reg_es;
209static const reg_entry *reg_ss;
6288d05f 210static const reg_entry *reg_st0;
6225c532
JB
211static const reg_entry *reg_k0;
212
c0f3af97
L
213/* VEX prefix. */
214typedef struct
215{
43234a1e
L
216 /* VEX prefix is either 2 byte or 3 byte. EVEX is 4 byte. */
217 unsigned char bytes[4];
c0f3af97
L
218 unsigned int length;
219 /* Destination or source register specifier. */
220 const reg_entry *register_specifier;
221} vex_prefix;
222
252b5132 223/* 'md_assemble ()' gathers together information and puts it into a
47926f60 224 i386_insn. */
252b5132 225
520dc8e8
AM
226union i386_op
227 {
228 expressionS *disps;
229 expressionS *imms;
230 const reg_entry *regs;
231 };
232
a65babc9
L
233enum i386_error
234 {
b4d65f2d 235 no_error, /* Must be first. */
86e026a4 236 operand_size_mismatch,
a65babc9
L
237 operand_type_mismatch,
238 register_type_mismatch,
239 number_of_operands_mismatch,
240 invalid_instruction_suffix,
241 bad_imm4,
a65babc9
L
242 unsupported_with_intel_mnemonic,
243 unsupported_syntax,
80d61d8d 244 unsupported_EGPR_for_addressing,
dd74a603 245 unsupported_nf,
6c30d220 246 unsupported,
9db83a32
JB
247 unsupported_on_arch,
248 unsupported_64bit,
54294d73
JB
249 no_vex_encoding,
250 no_evex_encoding,
260cd341 251 invalid_sib_address,
6c30d220 252 invalid_vsib_address,
7bab8ab5 253 invalid_vector_register_set,
260cd341 254 invalid_tmm_register_set,
0cc78721 255 invalid_dest_and_src_register_set,
08a98d4c 256 invalid_dest_register_set,
80d61d8d 257 invalid_pseudo_prefix,
43234a1e
L
258 unsupported_vector_index_register,
259 unsupported_broadcast,
43234a1e
L
260 broadcast_needed,
261 unsupported_masking,
262 mask_not_on_destination,
263 no_default_mask,
264 unsupported_rc_sae,
54294d73 265 unsupported_vector_size,
08a98d4c 266 unsupported_rsp_register,
58bceb18 267 internal_error,
a65babc9
L
268 };
269
252b5132
RH
270struct _i386_insn
271 {
47926f60 272 /* TM holds the template for the insn were currently assembling. */
d3ce72d0 273 insn_template tm;
252b5132 274
7d5e4556
L
275 /* SUFFIX holds the instruction size suffix for byte, word, dword
276 or qword, if given. */
252b5132
RH
277 char suffix;
278
9a182d04
JB
279 /* OPCODE_LENGTH holds the number of base opcode bytes. */
280 unsigned char opcode_length;
281
47926f60 282 /* OPERANDS gives the number of given operands. */
252b5132
RH
283 unsigned int operands;
284
285 /* REG_OPERANDS, DISP_OPERANDS, MEM_OPERANDS, IMM_OPERANDS give the number
286 of given register, displacement, memory operands and immediate
47926f60 287 operands. */
252b5132
RH
288 unsigned int reg_operands, disp_operands, mem_operands, imm_operands;
289
290 /* TYPES [i] is the type (see above #defines) which tells us how to
520dc8e8 291 use OP[i] for the corresponding operand. */
40fb9820 292 i386_operand_type types[MAX_OPERANDS];
252b5132 293
520dc8e8
AM
294 /* Displacement expression, immediate expression, or register for each
295 operand. */
296 union i386_op op[MAX_OPERANDS];
252b5132 297
3e73aa7c
JH
298 /* Flags for operands. */
299 unsigned int flags[MAX_OPERANDS];
300#define Operand_PCrel 1
c48dadc9 301#define Operand_Mem 2
c032bc4f 302#define Operand_Signed 4 /* .insn only */
3e73aa7c 303
252b5132 304 /* Relocation type for operand */
f86103b7 305 enum bfd_reloc_code_real reloc[MAX_OPERANDS];
252b5132 306
252b5132
RH
307 /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
308 the base index byte below. */
309 const reg_entry *base_reg;
310 const reg_entry *index_reg;
311 unsigned int log2_scale_factor;
312
313 /* SEG gives the seg_entries of this insn. They are zero unless
47926f60 314 explicit segment overrides are given. */
5e042380 315 const reg_entry *seg[2];
252b5132
RH
316
317 /* PREFIX holds all the given prefix opcodes (usually null).
318 PREFIXES is the number of prefix opcodes. */
319 unsigned int prefixes;
320 unsigned char prefix[MAX_PREFIXES];
321
d0c2e3ec
JB
322 /* .insn allows for reserved opcode spaces. */
323 unsigned char insn_opcode_space;
324
c032bc4f
JB
325 /* .insn also allows (requires) specifying immediate size. */
326 unsigned char imm_bits[MAX_OPERANDS];
327
50128d0c 328 /* Register is in low 3 bits of opcode. */
5b7c81bd 329 bool short_form;
50128d0c 330
6f2f06be 331 /* The operand to a branch insn indicates an absolute branch. */
5b7c81bd 332 bool jumpabsolute;
6f2f06be 333
a4d3acd2
JB
334 /* The operand to a branch insn indicates a far branch. */
335 bool far_branch;
336
9373f275
L
337 /* There is a memory operand of (%dx) which should be only used
338 with input/output instructions. */
339 bool input_output_operand;
340
921eafea
L
341 /* Extended states. */
342 enum
343 {
344 /* Use MMX state. */
345 xstate_mmx = 1 << 0,
346 /* Use XMM state. */
347 xstate_xmm = 1 << 1,
348 /* Use YMM state. */
349 xstate_ymm = 1 << 2 | xstate_xmm,
350 /* Use ZMM state. */
351 xstate_zmm = 1 << 3 | xstate_ymm,
352 /* Use TMM state. */
32930e4e
L
353 xstate_tmm = 1 << 4,
354 /* Use MASK state. */
355 xstate_mask = 1 << 5
921eafea 356 } xstate;
260cd341 357
e379e5f3 358 /* Has GOTPC or TLS relocation. */
5b7c81bd 359 bool has_gotpc_tls_reloc;
e379e5f3 360
252b5132 361 /* RM and SIB are the modrm byte and the sib byte where the
c1e679ec 362 addressing modes of this insn are encoded. */
252b5132 363 modrm_byte rm;
3e73aa7c 364 rex_byte rex;
43234a1e 365 rex_byte vrex;
80d61d8d 366 rex_byte rex2;
252b5132 367 sib_byte sib;
c0f3af97 368 vex_prefix vex;
b6169b20 369
6225c532
JB
370 /* Masking attributes.
371
372 The struct describes masking, applied to OPERAND in the instruction.
373 REG is a pointer to the corresponding mask register. ZEROING tells
374 whether merging or zeroing mask is used. */
375 struct Mask_Operation
376 {
377 const reg_entry *reg;
378 unsigned int zeroing;
379 /* The operand where this operation is associated. */
380 unsigned int operand;
381 } mask;
43234a1e
L
382
383 /* Rounding control and SAE attributes. */
ca5312a2
JB
384 struct RC_Operation
385 {
386 enum rc_type
387 {
388 rc_none = -1,
389 rne,
390 rd,
391 ru,
392 rz,
393 saeonly
394 } type;
7063667e
JB
395 /* In Intel syntax the operand modifier form is supposed to be used, but
396 we continue to accept the immediate forms as well. */
397 bool modifier;
ca5312a2 398 } rounding;
43234a1e 399
5273a3cd
JB
400 /* Broadcasting attributes.
401
402 The struct describes broadcasting, applied to OPERAND. TYPE is
403 expresses the broadcast factor. */
404 struct Broadcast_Operation
405 {
0cc78721 406 /* Type of broadcast: {1to2}, {1to4}, {1to8}, {1to16} or {1to32}. */
5273a3cd
JB
407 unsigned int type;
408
409 /* Index of broadcasted operand. */
410 unsigned int operand;
411
412 /* Number of bytes to broadcast. */
413 unsigned int bytes;
414 } broadcast;
43234a1e
L
415
416 /* Compressed disp8*N attribute. */
417 unsigned int memshift;
418
86fa6981
L
419 /* Prefer load or store in encoding. */
420 enum
421 {
422 dir_encoding_default = 0,
423 dir_encoding_load,
64c49ab3
JB
424 dir_encoding_store,
425 dir_encoding_swap
86fa6981 426 } dir_encoding;
891edac4 427
41eb8e88 428 /* Prefer 8bit, 16bit, 32bit displacement in encoding. */
a501d77e
L
429 enum
430 {
431 disp_encoding_default = 0,
432 disp_encoding_8bit,
41eb8e88 433 disp_encoding_16bit,
a501d77e
L
434 disp_encoding_32bit
435 } disp_encoding;
f8a5c266 436
6b6b6807 437 /* Prefer the REX byte in encoding. */
5b7c81bd 438 bool rex_encoding;
6b6b6807 439
80d61d8d
CL
440 /* Prefer the REX2 prefix in encoding. */
441 bool rex2_encoding;
442
dd74a603
CL
443 /* No CSPAZO flags update. */
444 bool has_nf;
445
b6f8c7c4 446 /* Disable instruction size optimization. */
5b7c81bd 447 bool no_optimize;
b6f8c7c4 448
e346d50a 449 /* How to encode instructions. */
86fa6981
L
450 enum
451 {
e346d50a
JB
452 encoding_default = 0,
453 encoding_vex,
454 encoding_vex3,
eb3f3841 455 encoding_egpr, /* REX2 or EVEX. */
e346d50a
JB
456 encoding_evex,
457 encoding_evex512,
458 encoding_error
459 } encoding;
86fa6981 460
d5de92cf
L
461 /* REP prefix. */
462 const char *rep_prefix;
463
165de32a
L
464 /* HLE prefix. */
465 const char *hle_prefix;
42164a71 466
7e8b059b
L
467 /* Have BND prefix. */
468 const char *bnd_prefix;
469
04ef582a
L
470 /* Have NOTRACK prefix. */
471 const char *notrack_prefix;
472
891edac4 473 /* Error message. */
a65babc9 474 enum i386_error error;
252b5132
RH
475 };
476
477typedef struct _i386_insn i386_insn;
478
43234a1e
L
479/* Link RC type with corresponding string, that'll be looked for in
480 asm. */
481struct RC_name
482{
483 enum rc_type type;
484 const char *name;
485 unsigned int len;
486};
487
488static const struct RC_name RC_NamesTable[] =
489{
490 { rne, STRING_COMMA_LEN ("rn-sae") },
491 { rd, STRING_COMMA_LEN ("rd-sae") },
492 { ru, STRING_COMMA_LEN ("ru-sae") },
493 { rz, STRING_COMMA_LEN ("rz-sae") },
494 { saeonly, STRING_COMMA_LEN ("sae") },
495};
496
3bfea8ba
L
497/* To be indexed by segment register number. */
498static const unsigned char i386_seg_prefixes[] = {
499 ES_PREFIX_OPCODE,
500 CS_PREFIX_OPCODE,
501 SS_PREFIX_OPCODE,
502 DS_PREFIX_OPCODE,
503 FS_PREFIX_OPCODE,
504 GS_PREFIX_OPCODE
505};
506
252b5132
RH
507/* List of chars besides those in app.c:symbol_chars that can start an
508 operand. Used to prevent the scrubber eating vital white-space. */
86fa6981 509const char extra_symbol_chars[] = "*%-([{}"
252b5132 510#ifdef LEX_AT
32137342
NC
511 "@"
512#endif
513#ifdef LEX_QM
514 "?"
252b5132 515#endif
32137342 516 ;
252b5132 517
b3983e5f
JB
518#if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
519 && !defined (TE_GNU) \
520 && !defined (TE_LINUX) \
d85e70a3 521 && !defined (TE_Haiku) \
b3983e5f
JB
522 && !defined (TE_FreeBSD) \
523 && !defined (TE_DragonFly) \
524 && !defined (TE_NetBSD))
252b5132 525/* This array holds the chars that always start a comment. If the
b3b91714
AM
526 pre-processor is disabled, these aren't very useful. The option
527 --divide will remove '/' from this list. */
528const char *i386_comment_chars = "#/";
529#define SVR4_COMMENT_CHARS 1
252b5132 530#define PREFIX_SEPARATOR '\\'
252b5132 531
b3b91714
AM
532#else
533const char *i386_comment_chars = "#";
534#define PREFIX_SEPARATOR '/'
535#endif
536
252b5132
RH
537/* This array holds the chars that only start a comment at the beginning of
538 a line. If the line seems to have the form '# 123 filename'
ce8a8b2f
AM
539 .line and .file directives will appear in the pre-processed output.
540 Note that input_file.c hand checks for '#' at the beginning of the
252b5132 541 first line of the input file. This is because the compiler outputs
ce8a8b2f
AM
542 #NO_APP at the beginning of its output.
543 Also note that comments started like this one will always work if
252b5132 544 '/' isn't otherwise defined. */
b3b91714 545const char line_comment_chars[] = "#/";
252b5132 546
63a0b638 547const char line_separator_chars[] = ";";
252b5132 548
ce8a8b2f
AM
549/* Chars that can be used to separate mant from exp in floating point
550 nums. */
252b5132
RH
551const char EXP_CHARS[] = "eE";
552
ce8a8b2f
AM
553/* Chars that mean this number is a floating point constant
554 As in 0f12.456
555 or 0d1.2345e12. */
de133cf9 556const char FLT_CHARS[] = "fFdDxXhHbB";
252b5132 557
ce8a8b2f 558/* Tables for lexical analysis. */
252b5132
RH
559static char mnemonic_chars[256];
560static char register_chars[256];
561static char operand_chars[256];
252b5132 562
ce8a8b2f 563/* Lexical macros. */
252b5132
RH
564#define is_operand_char(x) (operand_chars[(unsigned char) x])
565#define is_register_char(x) (register_chars[(unsigned char) x])
566#define is_space_char(x) ((x) == ' ')
252b5132 567
d2b1a14d
JB
568/* All non-digit non-letter characters that may occur in an operand and
569 which aren't already in extra_symbol_chars[]. */
570static const char operand_special_chars[] = "$+,)._~/<>|&^!=:@]";
252b5132
RH
571
572/* md_assemble() always leaves the strings it's passed unaltered. To
573 effect this we maintain a stack of saved characters that we've smashed
574 with '\0's (indicating end of strings for various sub-fields of the
47926f60 575 assembler instruction). */
252b5132 576static char save_stack[32];
ce8a8b2f 577static char *save_stack_p;
252b5132
RH
578#define END_STRING_AND_SAVE(s) \
579 do { *save_stack_p++ = *(s); *(s) = '\0'; } while (0)
580#define RESTORE_END_STRING(s) \
581 do { *(s) = *--save_stack_p; } while (0)
582
47926f60 583/* The instruction we're assembling. */
252b5132
RH
584static i386_insn i;
585
586/* Possible templates for current insn. */
d3b01414 587static templates current_templates;
252b5132 588
31b2323c
L
589/* Per instruction expressionS buffers: max displacements & immediates. */
590static expressionS disp_expressions[MAX_MEMORY_OPERANDS];
591static expressionS im_expressions[MAX_IMMEDIATE_OPERANDS];
252b5132 592
47926f60 593/* Current operand we are working on. */
ee86248c 594static int this_operand = -1;
252b5132 595
d0c2e3ec
JB
596/* Are we processing a .insn directive? */
597#define dot_insn() (i.tm.mnem_off == MN__insn)
598
ed719294
JB
599enum i386_flag_code i386_flag_code;
600#define flag_code i386_flag_code /* Permit to continue using original name. */
4fa24527 601static unsigned int object_64bit;
862be3fb 602static unsigned int disallow_64bit_reloc;
3e73aa7c 603static int use_rela_relocations = 0;
e379e5f3
L
604/* __tls_get_addr/___tls_get_addr symbol for TLS. */
605static const char *tls_get_addr;
3e73aa7c 606
071c5d81 607#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
7af8ed2d 608
351f65ca
L
609/* The ELF ABI to use. */
610enum x86_elf_abi
611{
612 I386_ABI,
7f56bc95
L
613 X86_64_ABI,
614 X86_64_X32_ABI
351f65ca
L
615};
616
617static enum x86_elf_abi x86_elf_abi = I386_ABI;
7af8ed2d 618#endif
351f65ca 619
167ad85b
TG
620#if defined (TE_PE) || defined (TE_PEP)
621/* Use big object file format. */
622static int use_big_obj = 0;
623#endif
624
8dcea932
L
625#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
626/* 1 if generating code for a shared library. */
627static int shared = 0;
b52c4ee4
IB
628
629unsigned int x86_sframe_cfa_sp_reg;
3e3e792a 630/* The other CFA base register for SFrame stack trace info. */
b52c4ee4
IB
631unsigned int x86_sframe_cfa_fp_reg;
632unsigned int x86_sframe_cfa_ra_reg;
633
8dcea932
L
634#endif
635
47926f60
KH
636/* 1 for intel syntax,
637 0 if att syntax. */
638static int intel_syntax = 0;
252b5132 639
4b5aaf5f
L
640static enum x86_64_isa
641{
642 amd64 = 1, /* AMD64 ISA. */
643 intel64 /* Intel64 ISA. */
644} isa64;
e89c5eaa 645
1efbbeb4
L
646/* 1 for intel mnemonic,
647 0 if att mnemonic. */
648static int intel_mnemonic = !SYSV386_COMPAT;
649
a60de03c
JB
650/* 1 if pseudo registers are permitted. */
651static int allow_pseudo_reg = 0;
652
47926f60
KH
653/* 1 if register prefix % not required. */
654static int allow_naked_reg = 0;
252b5132 655
33eaf5de 656/* 1 if the assembler should add BND prefix for all control-transferring
7e8b059b
L
657 instructions supporting it, even if this prefix wasn't specified
658 explicitly. */
659static int add_bnd_prefix = 0;
660
ba104c83 661/* 1 if pseudo index register, eiz/riz, is allowed . */
db51cc60
L
662static int allow_index_reg = 0;
663
d022bddd
IT
664/* 1 if the assembler should ignore LOCK prefix, even if it was
665 specified explicitly. */
666static int omit_lock_prefix = 0;
667
e4e00185
AS
668/* 1 if the assembler should encode lfence, mfence, and sfence as
669 "lock addl $0, (%{re}sp)". */
670static int avoid_fence = 0;
671
ae531041
L
672/* 1 if lfence should be inserted after every load. */
673static int lfence_after_load = 0;
674
675/* Non-zero if lfence should be inserted before indirect branch. */
676static enum lfence_before_indirect_branch_kind
677 {
678 lfence_branch_none = 0,
679 lfence_branch_register,
680 lfence_branch_memory,
681 lfence_branch_all
682 }
683lfence_before_indirect_branch;
684
685/* Non-zero if lfence should be inserted before ret. */
686static enum lfence_before_ret_kind
687 {
688 lfence_before_ret_none = 0,
689 lfence_before_ret_not,
a09f656b 690 lfence_before_ret_or,
691 lfence_before_ret_shl
ae531041
L
692 }
693lfence_before_ret;
694
0cb4071e
L
695/* 1 if the assembler should generate relax relocations. */
696
697static int generate_relax_relocations
698 = DEFAULT_GENERATE_X86_RELAX_RELOCATIONS;
699
7bab8ab5 700static enum check_kind
daf50ae7 701 {
7bab8ab5
JB
702 check_none = 0,
703 check_warning,
704 check_error
daf50ae7 705 }
7bab8ab5 706sse_check, operand_check = check_warning;
daf50ae7 707
e379e5f3
L
708/* Non-zero if branches should be aligned within power of 2 boundary. */
709static int align_branch_power = 0;
710
711/* Types of branches to align. */
712enum align_branch_kind
713 {
714 align_branch_none = 0,
715 align_branch_jcc = 1,
716 align_branch_fused = 2,
717 align_branch_jmp = 3,
718 align_branch_call = 4,
719 align_branch_indirect = 5,
720 align_branch_ret = 6
721 };
722
723/* Type bits of branches to align. */
724enum align_branch_bit
725 {
726 align_branch_jcc_bit = 1 << align_branch_jcc,
727 align_branch_fused_bit = 1 << align_branch_fused,
728 align_branch_jmp_bit = 1 << align_branch_jmp,
729 align_branch_call_bit = 1 << align_branch_call,
730 align_branch_indirect_bit = 1 << align_branch_indirect,
731 align_branch_ret_bit = 1 << align_branch_ret
732 };
733
734static unsigned int align_branch = (align_branch_jcc_bit
735 | align_branch_fused_bit
736 | align_branch_jmp_bit);
737
79d72f45
HL
738/* Types of condition jump used by macro-fusion. */
739enum mf_jcc_kind
740 {
741 mf_jcc_jo = 0, /* base opcode 0x70 */
742 mf_jcc_jc, /* base opcode 0x72 */
743 mf_jcc_je, /* base opcode 0x74 */
744 mf_jcc_jna, /* base opcode 0x76 */
745 mf_jcc_js, /* base opcode 0x78 */
746 mf_jcc_jp, /* base opcode 0x7a */
747 mf_jcc_jl, /* base opcode 0x7c */
748 mf_jcc_jle, /* base opcode 0x7e */
749 };
750
751/* Types of compare flag-modifying insntructions used by macro-fusion. */
752enum mf_cmp_kind
753 {
754 mf_cmp_test_and, /* test/cmp */
755 mf_cmp_alu_cmp, /* add/sub/cmp */
756 mf_cmp_incdec /* inc/dec */
757 };
758
e379e5f3
L
759/* The maximum padding size for fused jcc. CMP like instruction can
760 be 9 bytes and jcc can be 6 bytes. Leave room just in case for
761 prefixes. */
762#define MAX_FUSED_JCC_PADDING_SIZE 20
763
764/* The maximum number of prefixes added for an instruction. */
765static unsigned int align_branch_prefix_size = 5;
766
b6f8c7c4
L
767/* Optimization:
768 1. Clear the REX_W bit with register operand if possible.
769 2. Above plus use 128bit vector instruction to clear the full vector
770 register.
771 */
772static int optimize = 0;
773
774/* Optimization:
775 1. Clear the REX_W bit with register operand if possible.
776 2. Above plus use 128bit vector instruction to clear the full vector
777 register.
778 3. Above plus optimize "test{q,l,w} $imm8,%r{64,32,16}" to
779 "testb $imm7,%r8".
780 */
781static int optimize_for_space = 0;
782
2ca3ace5
L
783/* Register prefix used for error message. */
784static const char *register_prefix = "%";
785
47926f60
KH
786/* Used in 16 bit gcc mode to add an l suffix to call, ret, enter,
787 leave, push, and pop instructions so that gcc has the same stack
788 frame as in 32 bit mode. */
789static char stackop_size = '\0';
eecb386c 790
12b55ccc
L
791/* Non-zero to optimize code alignment. */
792int optimize_align_code = 1;
793
47926f60
KH
794/* Non-zero to quieten some warnings. */
795static int quiet_warnings = 0;
a38cf1db 796
d59a54c2
JB
797/* Guard to avoid repeated warnings about non-16-bit code on 16-bit CPUs. */
798static bool pre_386_16bit_warned;
799
47926f60
KH
800/* CPU name. */
801static const char *cpu_arch_name = NULL;
6305a203 802static char *cpu_sub_arch_name = NULL;
a38cf1db 803
47926f60 804/* CPU feature flags. */
cd75cd85 805i386_cpu_flags cpu_arch_flags = CPU_UNKNOWN_FLAGS;
40fb9820 806
3e624fa4
JB
807/* ISA extensions available in 64-bit mode only. */
808static const i386_cpu_flags cpu_64_flags = CPU_ANY_64_FLAGS;
809
ccc9c027
L
810/* If we have selected a cpu we are generating instructions for. */
811static int cpu_arch_tune_set = 0;
812
9103f4f4 813/* Cpu we are generating instructions for. */
fbf3f584 814enum processor_type cpu_arch_tune = PROCESSOR_UNKNOWN;
9103f4f4 815
ccc9c027 816/* CPU instruction set architecture used. */
fbf3f584 817enum processor_type cpu_arch_isa = PROCESSOR_UNKNOWN;
ccc9c027 818
9103f4f4 819/* CPU feature flags of instruction set architecture used. */
fbf3f584 820i386_cpu_flags cpu_arch_isa_flags;
9103f4f4 821
fddf5b5b
AM
822/* If set, conditional jumps are not automatically promoted to handle
823 larger than a byte offset. */
f68697e8 824static bool no_cond_jump_promotion = false;
fddf5b5b 825
5cc00775
JB
826/* This will be set from an expression parser hook if there's any
827 applicable operator involved in an expression. */
828static enum {
829 expr_operator_none,
830 expr_operator_present,
831 expr_large_value,
832} expr_mode;
833
c0f3af97
L
834/* Encode SSE instructions with VEX prefix. */
835static unsigned int sse2avx;
836
c8480b58
L
837/* Encode aligned vector move as unaligned vector move. */
838static unsigned int use_unaligned_vector_move;
839
4fc85f37 840/* Maximum permitted vector size. */
fa88a361
HJ
841#define VSZ128 0
842#define VSZ256 1
843#define VSZ512 2
4fc85f37
JB
844#define VSZ_DEFAULT VSZ512
845static unsigned int vector_size = VSZ_DEFAULT;
846
539f890d
L
847/* Encode scalar AVX instructions with specific vector length. */
848static enum
849 {
850 vex128 = 0,
851 vex256
852 } avxscalar;
853
03751133
L
854/* Encode VEX WIG instructions with specific vex.w. */
855static enum
856 {
857 vexw0 = 0,
858 vexw1
859 } vexwig;
860
43234a1e
L
861/* Encode scalar EVEX LIG instructions with specific vector length. */
862static enum
863 {
864 evexl128 = 0,
865 evexl256,
866 evexl512
867 } evexlig;
868
869/* Encode EVEX WIG instructions with specific evex.w. */
870static enum
871 {
872 evexw0 = 0,
873 evexw1
874 } evexwig;
875
d3d3c6db
IT
876/* Value to encode in EVEX RC bits, for SAE-only instructions. */
877static enum rc_type evexrcig = rne;
878
29b0f896 879/* Pre-defined "_GLOBAL_OFFSET_TABLE_". */
87c245cc 880static symbolS *GOT_symbol;
29b0f896 881
a4447b93
RH
882/* The dwarf2 return column, adjusted for 32 or 64 bit. */
883unsigned int x86_dwarf2_return_column;
884
885/* The dwarf2 data alignment, adjusted for 32 or 64 bit. */
886int x86_cie_data_alignment;
887
252b5132 888/* Interface to relax_segment.
fddf5b5b
AM
889 There are 3 major relax states for 386 jump insns because the
890 different types of jumps add different sizes to frags when we're
e379e5f3
L
891 figuring out what sort of jump to choose to reach a given label.
892
893 BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING are used to align
894 branches which are handled by md_estimate_size_before_relax() and
895 i386_generic_table_relax_frag(). */
252b5132 896
47926f60 897/* Types. */
93c2a809
AM
898#define UNCOND_JUMP 0
899#define COND_JUMP 1
900#define COND_JUMP86 2
e379e5f3
L
901#define BRANCH_PADDING 3
902#define BRANCH_PREFIX 4
903#define FUSED_JCC_PADDING 5
fddf5b5b 904
47926f60 905/* Sizes. */
252b5132
RH
906#define CODE16 1
907#define SMALL 0
29b0f896 908#define SMALL16 (SMALL | CODE16)
252b5132 909#define BIG 2
29b0f896 910#define BIG16 (BIG | CODE16)
252b5132
RH
911
912#ifndef INLINE
913#ifdef __GNUC__
914#define INLINE __inline__
915#else
916#define INLINE
917#endif
918#endif
919
fddf5b5b
AM
920#define ENCODE_RELAX_STATE(type, size) \
921 ((relax_substateT) (((type) << 2) | (size)))
922#define TYPE_FROM_RELAX_STATE(s) \
923 ((s) >> 2)
924#define DISP_SIZE_FROM_RELAX_STATE(s) \
925 ((((s) & 3) == BIG ? 4 : (((s) & 3) == BIG16 ? 2 : 1)))
252b5132
RH
926
927/* This table is used by relax_frag to promote short jumps to long
928 ones where necessary. SMALL (short) jumps may be promoted to BIG
929 (32 bit long) ones, and SMALL16 jumps to BIG16 (16 bit long). We
930 don't allow a short jump in a 32 bit code segment to be promoted to
931 a 16 bit offset jump because it's slower (requires data size
932 prefix), and doesn't work, unless the destination is in the bottom
933 64k of the code segment (The top 16 bits of eip are zeroed). */
934
935const relax_typeS md_relax_table[] =
936{
24eab124
AM
937 /* The fields are:
938 1) most positive reach of this state,
939 2) most negative reach of this state,
93c2a809 940 3) how many bytes this mode will have in the variable part of the frag
ce8a8b2f 941 4) which index into the table to try if we can't fit into this one. */
252b5132 942
fddf5b5b 943 /* UNCOND_JUMP states. */
93c2a809
AM
944 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG)},
945 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16)},
946 /* dword jmp adds 4 bytes to frag:
947 0 extra opcode bytes, 4 displacement bytes. */
252b5132 948 {0, 0, 4, 0},
93c2a809
AM
949 /* word jmp adds 2 byte2 to frag:
950 0 extra opcode bytes, 2 displacement bytes. */
252b5132
RH
951 {0, 0, 2, 0},
952
93c2a809
AM
953 /* COND_JUMP states. */
954 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG)},
955 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG16)},
956 /* dword conditionals adds 5 bytes to frag:
957 1 extra opcode byte, 4 displacement bytes. */
958 {0, 0, 5, 0},
fddf5b5b 959 /* word conditionals add 3 bytes to frag:
93c2a809
AM
960 1 extra opcode byte, 2 displacement bytes. */
961 {0, 0, 3, 0},
962
963 /* COND_JUMP86 states. */
964 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG)},
965 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG16)},
966 /* dword conditionals adds 5 bytes to frag:
967 1 extra opcode byte, 4 displacement bytes. */
968 {0, 0, 5, 0},
969 /* word conditionals add 4 bytes to frag:
970 1 displacement byte and a 3 byte long branch insn. */
971 {0, 0, 4, 0}
252b5132
RH
972};
973
6ceeed25 974#define ARCH(n, t, f, s) \
4fc85f37 975 { STRING_COMMA_LEN (#n), s, PROCESSOR_ ## t, vsz_none, CPU_ ## f ## _FLAGS, \
ae89daec
JB
976 CPU_NONE_FLAGS }
977#define SUBARCH(n, e, d, s) \
4fc85f37 978 { STRING_COMMA_LEN (#n), s, PROCESSOR_NONE, vsz_none, CPU_ ## e ## _FLAGS, \
ae89daec 979 CPU_ ## d ## _FLAGS }
4fc85f37
JB
980#define VECARCH(n, e, d, v) \
981 { STRING_COMMA_LEN (#n), false, PROCESSOR_NONE, vsz_ ## v, \
982 CPU_ ## e ## _FLAGS, CPU_ ## d ## _FLAGS }
6ceeed25 983
9103f4f4
L
984static const arch_entry cpu_arch[] =
985{
3ce2ebcf
JB
986 /* Do not replace the first two entries - i386_target_format() and
987 set_cpu_arch() rely on them being there in this order. */
6ceeed25
JB
988 ARCH (generic32, GENERIC32, GENERIC32, false),
989 ARCH (generic64, GENERIC64, GENERIC64, false),
990 ARCH (i8086, UNKNOWN, NONE, false),
4d97c5c8
JB
991 ARCH (i186, UNKNOWN, 186, false),
992 ARCH (i286, UNKNOWN, 286, false),
993 ARCH (i386, I386, 386, false),
994 ARCH (i486, I486, 486, false),
995 ARCH (i586, PENTIUM, 586, false),
4d97c5c8 996 ARCH (pentium, PENTIUM, 586, false),
c8be4b6f 997 ARCH (i686, I686, 686, false),
6ceeed25
JB
998 ARCH (pentiumpro, PENTIUMPRO, PENTIUMPRO, false),
999 ARCH (pentiumii, PENTIUMPRO, P2, false),
1000 ARCH (pentiumiii, PENTIUMPRO, P3, false),
1001 ARCH (pentium4, PENTIUM4, P4, false),
1002 ARCH (prescott, NOCONA, CORE, false),
1003 ARCH (nocona, NOCONA, NOCONA, false),
1004 ARCH (yonah, CORE, CORE, true),
1005 ARCH (core, CORE, CORE, false),
1006 ARCH (merom, CORE2, CORE2, true),
1007 ARCH (core2, CORE2, CORE2, false),
1008 ARCH (corei7, COREI7, COREI7, false),
1009 ARCH (iamcu, IAMCU, IAMCU, false),
1010 ARCH (k6, K6, K6, false),
1011 ARCH (k6_2, K6, K6_2, false),
1012 ARCH (athlon, ATHLON, ATHLON, false),
1013 ARCH (sledgehammer, K8, K8, true),
1014 ARCH (opteron, K8, K8, false),
1015 ARCH (k8, K8, K8, false),
1016 ARCH (amdfam10, AMDFAM10, AMDFAM10, false),
1017 ARCH (bdver1, BD, BDVER1, false),
1018 ARCH (bdver2, BD, BDVER2, false),
1019 ARCH (bdver3, BD, BDVER3, false),
1020 ARCH (bdver4, BD, BDVER4, false),
1021 ARCH (znver1, ZNVER, ZNVER1, false),
1022 ARCH (znver2, ZNVER, ZNVER2, false),
1023 ARCH (znver3, ZNVER, ZNVER3, false),
b0e8fa7f 1024 ARCH (znver4, ZNVER, ZNVER4, false),
b143c979 1025 ARCH (znver5, ZNVER, ZNVER5, false),
6ceeed25
JB
1026 ARCH (btver1, BT, BTVER1, false),
1027 ARCH (btver2, BT, BTVER2, false),
1028
4d97c5c8
JB
1029 SUBARCH (8087, 8087, ANY_8087, false),
1030 SUBARCH (87, NONE, ANY_8087, false), /* Disable only! */
ae89daec
JB
1031 SUBARCH (287, 287, ANY_287, false),
1032 SUBARCH (387, 387, ANY_387, false),
1033 SUBARCH (687, 687, ANY_687, false),
4d97c5c8 1034 SUBARCH (cmov, CMOV, CMOV, false),
88bd2203 1035 SUBARCH (fxsr, FXSR, ANY_FXSR, false),
ae89daec
JB
1036 SUBARCH (mmx, MMX, ANY_MMX, false),
1037 SUBARCH (sse, SSE, ANY_SSE, false),
1038 SUBARCH (sse2, SSE2, ANY_SSE2, false),
1039 SUBARCH (sse3, SSE3, ANY_SSE3, false),
1040 SUBARCH (sse4a, SSE4A, ANY_SSE4A, false),
1041 SUBARCH (ssse3, SSSE3, ANY_SSSE3, false),
1042 SUBARCH (sse4.1, SSE4_1, ANY_SSE4_1, false),
1043 SUBARCH (sse4.2, SSE4_2, ANY_SSE4_2, false),
1044 SUBARCH (sse4, SSE4_2, ANY_SSE4_1, false),
4fc85f37
JB
1045 VECARCH (avx, AVX, ANY_AVX, reset),
1046 VECARCH (avx2, AVX2, ANY_AVX2, reset),
1047 VECARCH (avx512f, AVX512F, ANY_AVX512F, reset),
1048 VECARCH (avx512cd, AVX512CD, ANY_AVX512CD, reset),
1049 VECARCH (avx512er, AVX512ER, ANY_AVX512ER, reset),
1050 VECARCH (avx512pf, AVX512PF, ANY_AVX512PF, reset),
1051 VECARCH (avx512dq, AVX512DQ, ANY_AVX512DQ, reset),
1052 VECARCH (avx512bw, AVX512BW, ANY_AVX512BW, reset),
1053 VECARCH (avx512vl, AVX512VL, ANY_AVX512VL, reset),
cafa5ef7 1054 SUBARCH (monitor, MONITOR, MONITOR, false),
25626f79
JB
1055 SUBARCH (vmx, VMX, ANY_VMX, false),
1056 SUBARCH (vmfunc, VMFUNC, ANY_VMFUNC, false),
ae89daec 1057 SUBARCH (smx, SMX, SMX, false),
5091b9ee
JB
1058 SUBARCH (xsave, XSAVE, ANY_XSAVE, false),
1059 SUBARCH (xsaveopt, XSAVEOPT, ANY_XSAVEOPT, false),
1060 SUBARCH (xsavec, XSAVEC, ANY_XSAVEC, false),
1061 SUBARCH (xsaves, XSAVES, ANY_XSAVES, false),
1062 SUBARCH (aes, AES, ANY_AES, false),
d54678eb
JB
1063 SUBARCH (pclmul, PCLMULQDQ, ANY_PCLMULQDQ, false),
1064 SUBARCH (clmul, PCLMULQDQ, ANY_PCLMULQDQ, true),
ae89daec
JB
1065 SUBARCH (fsgsbase, FSGSBASE, FSGSBASE, false),
1066 SUBARCH (rdrnd, RDRND, RDRND, false),
5091b9ee 1067 SUBARCH (f16c, F16C, ANY_F16C, false),
ae89daec 1068 SUBARCH (bmi2, BMI2, BMI2, false),
5091b9ee
JB
1069 SUBARCH (fma, FMA, ANY_FMA, false),
1070 SUBARCH (fma4, FMA4, ANY_FMA4, false),
1071 SUBARCH (xop, XOP, ANY_XOP, false),
1072 SUBARCH (lwp, LWP, ANY_LWP, false),
ae89daec
JB
1073 SUBARCH (movbe, MOVBE, MOVBE, false),
1074 SUBARCH (cx16, CX16, CX16, false),
c3bb24f5 1075 SUBARCH (lahf_sahf, LAHF_SAHF, LAHF_SAHF, false),
25626f79 1076 SUBARCH (ept, EPT, ANY_EPT, false),
ae89daec
JB
1077 SUBARCH (lzcnt, LZCNT, LZCNT, false),
1078 SUBARCH (popcnt, POPCNT, POPCNT, false),
1079 SUBARCH (hle, HLE, HLE, false),
760ab3d0
JB
1080 SUBARCH (rtm, RTM, ANY_RTM, false),
1081 SUBARCH (tsx, TSX, TSX, false),
ae89daec
JB
1082 SUBARCH (invpcid, INVPCID, INVPCID, false),
1083 SUBARCH (clflush, CLFLUSH, CLFLUSH, false),
1084 SUBARCH (nop, NOP, NOP, false),
1085 SUBARCH (syscall, SYSCALL, SYSCALL, false),
1086 SUBARCH (rdtscp, RDTSCP, RDTSCP, false),
5091b9ee
JB
1087 SUBARCH (3dnow, 3DNOW, ANY_3DNOW, false),
1088 SUBARCH (3dnowa, 3DNOWA, ANY_3DNOWA, false),
ae89daec 1089 SUBARCH (padlock, PADLOCK, PADLOCK, false),
0919e770
JB
1090 SUBARCH (pacifica, SVME, ANY_SVME, true),
1091 SUBARCH (svme, SVME, ANY_SVME, false),
ae89daec
JB
1092 SUBARCH (abm, ABM, ABM, false),
1093 SUBARCH (bmi, BMI, BMI, false),
1094 SUBARCH (tbm, TBM, TBM, false),
1095 SUBARCH (adx, ADX, ADX, false),
1096 SUBARCH (rdseed, RDSEED, RDSEED, false),
1097 SUBARCH (prfchw, PRFCHW, PRFCHW, false),
1098 SUBARCH (smap, SMAP, SMAP, false),
5091b9ee
JB
1099 SUBARCH (mpx, MPX, ANY_MPX, false),
1100 SUBARCH (sha, SHA, ANY_SHA, false),
ae89daec
JB
1101 SUBARCH (clflushopt, CLFLUSHOPT, CLFLUSHOPT, false),
1102 SUBARCH (prefetchwt1, PREFETCHWT1, PREFETCHWT1, false),
1103 SUBARCH (se1, SE1, SE1, false),
1104 SUBARCH (clwb, CLWB, CLWB, false),
4fc85f37
JB
1105 VECARCH (avx512ifma, AVX512IFMA, ANY_AVX512IFMA, reset),
1106 VECARCH (avx512vbmi, AVX512VBMI, ANY_AVX512VBMI, reset),
1107 VECARCH (avx512_4fmaps, AVX512_4FMAPS, ANY_AVX512_4FMAPS, reset),
1108 VECARCH (avx512_4vnniw, AVX512_4VNNIW, ANY_AVX512_4VNNIW, reset),
1109 VECARCH (avx512_vpopcntdq, AVX512_VPOPCNTDQ, ANY_AVX512_VPOPCNTDQ, reset),
1110 VECARCH (avx512_vbmi2, AVX512_VBMI2, ANY_AVX512_VBMI2, reset),
1111 VECARCH (avx512_vnni, AVX512_VNNI, ANY_AVX512_VNNI, reset),
1112 VECARCH (avx512_bitalg, AVX512_BITALG, ANY_AVX512_BITALG, reset),
1113 VECARCH (avx_vnni, AVX_VNNI, ANY_AVX_VNNI, reset),
ae89daec
JB
1114 SUBARCH (clzero, CLZERO, CLZERO, false),
1115 SUBARCH (mwaitx, MWAITX, MWAITX, false),
5091b9ee 1116 SUBARCH (ospke, OSPKE, ANY_OSPKE, false),
ae89daec
JB
1117 SUBARCH (rdpid, RDPID, RDPID, false),
1118 SUBARCH (ptwrite, PTWRITE, PTWRITE, false),
4d97c5c8
JB
1119 SUBARCH (ibt, IBT, IBT, false),
1120 SUBARCH (shstk, SHSTK, SHSTK, false),
88bd2203 1121 SUBARCH (gfni, GFNI, ANY_GFNI, false),
4fc85f37
JB
1122 VECARCH (vaes, VAES, ANY_VAES, reset),
1123 VECARCH (vpclmulqdq, VPCLMULQDQ, ANY_VPCLMULQDQ, reset),
ae89daec
JB
1124 SUBARCH (wbnoinvd, WBNOINVD, WBNOINVD, false),
1125 SUBARCH (pconfig, PCONFIG, PCONFIG, false),
1126 SUBARCH (waitpkg, WAITPKG, WAITPKG, false),
1127 SUBARCH (cldemote, CLDEMOTE, CLDEMOTE, false),
1128 SUBARCH (amx_int8, AMX_INT8, ANY_AMX_INT8, false),
1129 SUBARCH (amx_bf16, AMX_BF16, ANY_AMX_BF16, false),
5091b9ee 1130 SUBARCH (amx_fp16, AMX_FP16, ANY_AMX_FP16, false),
d100d8c1 1131 SUBARCH (amx_complex, AMX_COMPLEX, ANY_AMX_COMPLEX, false),
ae89daec 1132 SUBARCH (amx_tile, AMX_TILE, ANY_AMX_TILE, false),
4d97c5c8
JB
1133 SUBARCH (movdiri, MOVDIRI, MOVDIRI, false),
1134 SUBARCH (movdir64b, MOVDIR64B, MOVDIR64B, false),
4fc85f37
JB
1135 VECARCH (avx512_bf16, AVX512_BF16, ANY_AVX512_BF16, reset),
1136 VECARCH (avx512_vp2intersect, AVX512_VP2INTERSECT,
1137 ANY_AVX512_VP2INTERSECT, reset),
4d97c5c8
JB
1138 SUBARCH (tdx, TDX, TDX, false),
1139 SUBARCH (enqcmd, ENQCMD, ENQCMD, false),
1140 SUBARCH (serialize, SERIALIZE, SERIALIZE, false),
ae89daec
JB
1141 SUBARCH (rdpru, RDPRU, RDPRU, false),
1142 SUBARCH (mcommit, MCOMMIT, MCOMMIT, false),
0919e770 1143 SUBARCH (sev_es, SEV_ES, ANY_SEV_ES, false),
760ab3d0 1144 SUBARCH (tsxldtrk, TSXLDTRK, ANY_TSXLDTRK, false),
88bd2203
JB
1145 SUBARCH (kl, KL, ANY_KL, false),
1146 SUBARCH (widekl, WIDEKL, ANY_WIDEKL, false),
4d97c5c8
JB
1147 SUBARCH (uintr, UINTR, UINTR, false),
1148 SUBARCH (hreset, HRESET, HRESET, false),
4fc85f37 1149 VECARCH (avx512_fp16, AVX512_FP16, ANY_AVX512_FP16, reset),
ef07be45 1150 SUBARCH (prefetchi, PREFETCHI, PREFETCHI, false),
4fc85f37
JB
1151 VECARCH (avx_ifma, AVX_IFMA, ANY_AVX_IFMA, reset),
1152 VECARCH (avx_vnni_int8, AVX_VNNI_INT8, ANY_AVX_VNNI_INT8, reset),
4d97c5c8
JB
1153 SUBARCH (cmpccxadd, CMPCCXADD, CMPCCXADD, false),
1154 SUBARCH (wrmsrns, WRMSRNS, WRMSRNS, false),
1155 SUBARCH (msrlist, MSRLIST, MSRLIST, false),
4fc85f37 1156 VECARCH (avx_ne_convert, AVX_NE_CONVERT, ANY_AVX_NE_CONVERT, reset),
4d97c5c8 1157 SUBARCH (rao_int, RAO_INT, RAO_INT, false),
0919e770 1158 SUBARCH (rmpquery, RMPQUERY, ANY_RMPQUERY, false),
c88ed92f
ZJ
1159 SUBARCH (fred, FRED, ANY_FRED, false),
1160 SUBARCH (lkgs, LKGS, ANY_LKGS, false),
4fc85f37
JB
1161 VECARCH (avx_vnni_int16, AVX_VNNI_INT16, ANY_AVX_VNNI_INT16, reset),
1162 VECARCH (sha512, SHA512, ANY_SHA512, reset),
1163 VECARCH (sm3, SM3, ANY_SM3, reset),
1164 VECARCH (sm4, SM4, ANY_SM4, reset),
b5c37946 1165 SUBARCH (pbndkb, PBNDKB, PBNDKB, false),
4fc85f37 1166 VECARCH (avx10.1, AVX10_1, ANY_AVX512F, set),
8170af78 1167 SUBARCH (user_msr, USER_MSR, USER_MSR, false),
80d61d8d 1168 SUBARCH (apx_f, APX_F, APX_F, false),
293f5f65
L
1169};
1170
6ceeed25
JB
1171#undef SUBARCH
1172#undef ARCH
1173
704209c0 1174#ifdef I386COFF
a6c24e68
NC
1175/* Like s_lcomm_internal in gas/read.c but the alignment string
1176 is allowed to be optional. */
1177
1178static symbolS *
1179pe_lcomm_internal (int needs_align, symbolS *symbolP, addressT size)
1180{
1181 addressT align = 0;
1182
1183 SKIP_WHITESPACE ();
1184
7ab9ffdd 1185 if (needs_align
a6c24e68
NC
1186 && *input_line_pointer == ',')
1187 {
1188 align = parse_align (needs_align - 1);
7ab9ffdd 1189
a6c24e68
NC
1190 if (align == (addressT) -1)
1191 return NULL;
1192 }
1193 else
1194 {
1195 if (size >= 8)
1196 align = 3;
1197 else if (size >= 4)
1198 align = 2;
1199 else if (size >= 2)
1200 align = 1;
1201 else
1202 align = 0;
1203 }
1204
1205 bss_alloc (symbolP, size, align);
1206 return symbolP;
1207}
1208
704209c0 1209static void
a6c24e68
NC
1210pe_lcomm (int needs_align)
1211{
1212 s_comm_internal (needs_align * 2, pe_lcomm_internal);
1213}
704209c0 1214#endif
a6c24e68 1215
29b0f896
AM
1216const pseudo_typeS md_pseudo_table[] =
1217{
1218#if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
1219 {"align", s_align_bytes, 0},
1220#else
1221 {"align", s_align_ptwo, 0},
1222#endif
1223 {"arch", set_cpu_arch, 0},
d4064aad 1224#ifdef OBJ_AOUT
29b0f896 1225 {"bss", s_bss, 0},
d4064aad
JB
1226#endif
1227#ifdef I386COFF
a6c24e68 1228 {"lcomm", pe_lcomm, 1},
29b0f896
AM
1229#endif
1230 {"ffloat", float_cons, 'f'},
1231 {"dfloat", float_cons, 'd'},
1232 {"tfloat", float_cons, 'x'},
7d19d096 1233 {"hfloat", float_cons, 'h'},
de133cf9 1234 {"bfloat16", float_cons, 'b'},
29b0f896 1235 {"value", cons, 2},
d182319b 1236 {"slong", signed_cons, 4},
edd67638 1237 {"insn", s_insn, 0},
1e7dd4a0 1238 {"noopt", s_noopt, 0},
29b0f896
AM
1239 {"optim", s_ignore, 0},
1240 {"code16gcc", set_16bit_gcc_code_flag, CODE_16BIT},
1241 {"code16", set_code_flag, CODE_16BIT},
1242 {"code32", set_code_flag, CODE_32BIT},
da5f19a2 1243#ifdef BFD64
29b0f896 1244 {"code64", set_code_flag, CODE_64BIT},
da5f19a2 1245#endif
29b0f896
AM
1246 {"intel_syntax", set_intel_syntax, 1},
1247 {"att_syntax", set_intel_syntax, 0},
1efbbeb4
L
1248 {"intel_mnemonic", set_intel_mnemonic, 1},
1249 {"att_mnemonic", set_intel_mnemonic, 0},
db51cc60
L
1250 {"allow_index_reg", set_allow_index_reg, 1},
1251 {"disallow_index_reg", set_allow_index_reg, 0},
7bab8ab5
JB
1252 {"sse_check", set_check, 0},
1253 {"operand_check", set_check, 1},
3b22753a
L
1254#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1255 {"largecomm", handle_large_common, 0},
07a53e5c 1256#else
68d20676 1257 {"file", dwarf2_directive_file, 0},
07a53e5c
RH
1258 {"loc", dwarf2_directive_loc, 0},
1259 {"loc_mark_labels", dwarf2_directive_loc_mark_labels, 0},
3b22753a 1260#endif
6482c264
NC
1261#ifdef TE_PE
1262 {"secrel32", pe_directive_secrel, 0},
145667f8 1263 {"secidx", pe_directive_secidx, 0},
6482c264 1264#endif
29b0f896
AM
1265 {0, 0, 0}
1266};
1267
1268/* For interface with expression (). */
1269extern char *input_line_pointer;
1270
1271/* Hash table for instruction mnemonic lookup. */
629310ab 1272static htab_t op_hash;
29b0f896
AM
1273
1274/* Hash table for register lookup. */
629310ab 1275static htab_t reg_hash;
29b0f896 1276\f
ce8a8b2f
AM
1277 /* Various efficient no-op patterns for aligning code labels.
1278 Note: Don't try to assemble the instructions in the comments.
1279 0L and 0w are not legal. */
62a02d25
L
1280static const unsigned char f32_1[] =
1281 {0x90}; /* nop */
1282static const unsigned char f32_2[] =
1283 {0x66,0x90}; /* xchg %ax,%ax */
1284static const unsigned char f32_3[] =
1285 {0x8d,0x76,0x00}; /* leal 0(%esi),%esi */
ad9f3230
JB
1286#define f32_4 (f32_5 + 1) /* leal 0(%esi,%eiz),%esi */
1287static const unsigned char f32_5[] =
1288 {0x2e,0x8d,0x74,0x26,0x00}; /* leal %cs:0(%esi,%eiz),%esi */
62a02d25
L
1289static const unsigned char f32_6[] =
1290 {0x8d,0xb6,0x00,0x00,0x00,0x00}; /* leal 0L(%esi),%esi */
ad9f3230
JB
1291#define f32_7 (f32_8 + 1) /* leal 0L(%esi,%eiz),%esi */
1292static const unsigned char f32_8[] =
1293 {0x2e,0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal %cs:0L(%esi,%eiz),%esi */
d164359d
JB
1294static const unsigned char f64_3[] =
1295 {0x48,0x89,0xf6}; /* mov %rsi,%rsi */
1296static const unsigned char f64_4[] =
1297 {0x48,0x8d,0x76,0x00}; /* lea 0(%rsi),%rsi */
1298#define f64_5 (f64_6 + 1) /* lea 0(%rsi,%riz),%rsi */
1299static const unsigned char f64_6[] =
1300 {0x2e,0x48,0x8d,0x74,0x26,0x00}; /* lea %cs:0(%rsi,%riz),%rsi */
1301static const unsigned char f64_7[] =
1302 {0x48,0x8d,0xb6,0x00,0x00,0x00,0x00}; /* lea 0L(%rsi),%rsi */
1303#define f64_8 (f64_9 + 1) /* lea 0L(%rsi,%riz),%rsi */
1304static const unsigned char f64_9[] =
1305 {0x2e,0x48,0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* lea %cs:0L(%rsi,%riz),%rsi */
5e0729b6 1306#define f16_2 (f64_3 + 1) /* mov %si,%si */
62a02d25 1307static const unsigned char f16_3[] =
3ae729d5 1308 {0x8d,0x74,0x00}; /* lea 0(%si),%si */
ad9f3230
JB
1309#define f16_4 (f16_5 + 1) /* lea 0W(%si),%si */
1310static const unsigned char f16_5[] =
1311 {0x2e,0x8d,0xb4,0x00,0x00}; /* lea %cs:0W(%si),%si */
3ae729d5
L
1312static const unsigned char jump_disp8[] =
1313 {0xeb}; /* jmp disp8 */
1314static const unsigned char jump32_disp32[] =
1315 {0xe9}; /* jmp disp32 */
1316static const unsigned char jump16_disp32[] =
1317 {0x66,0xe9}; /* jmp disp32 */
62a02d25
L
1318/* 32-bit NOPs patterns. */
1319static const unsigned char *const f32_patt[] = {
ad9f3230 1320 f32_1, f32_2, f32_3, f32_4, f32_5, f32_6, f32_7, f32_8
62a02d25 1321};
d164359d
JB
1322/* 64-bit NOPs patterns. */
1323static const unsigned char *const f64_patt[] = {
1324 f32_1, f32_2, f64_3, f64_4, f64_5, f64_6, f64_7, f64_8, f64_9
1325};
62a02d25
L
1326/* 16-bit NOPs patterns. */
1327static const unsigned char *const f16_patt[] = {
ad9f3230 1328 f32_1, f16_2, f16_3, f16_4, f16_5
62a02d25
L
1329};
1330/* nopl (%[re]ax) */
1331static const unsigned char alt_3[] =
1332 {0x0f,0x1f,0x00};
1333/* nopl 0(%[re]ax) */
1334static const unsigned char alt_4[] =
1335 {0x0f,0x1f,0x40,0x00};
1336/* nopl 0(%[re]ax,%[re]ax,1) */
ae7067fb 1337#define alt_5 (alt_6 + 1)
62a02d25
L
1338/* nopw 0(%[re]ax,%[re]ax,1) */
1339static const unsigned char alt_6[] =
1340 {0x66,0x0f,0x1f,0x44,0x00,0x00};
1341/* nopl 0L(%[re]ax) */
1342static const unsigned char alt_7[] =
1343 {0x0f,0x1f,0x80,0x00,0x00,0x00,0x00};
1344/* nopl 0L(%[re]ax,%[re]ax,1) */
ae7067fb 1345#define alt_8 (alt_9 + 1)
62a02d25
L
1346/* nopw 0L(%[re]ax,%[re]ax,1) */
1347static const unsigned char alt_9[] =
1348 {0x66,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1349/* nopw %cs:0L(%[re]ax,%[re]ax,1) */
ae7067fb 1350#define alt_10 (alt_11 + 1)
3ae729d5
L
1351/* data16 nopw %cs:0L(%eax,%eax,1) */
1352static const unsigned char alt_11[] =
1353 {0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
62a02d25
L
1354/* 32-bit and 64-bit NOPs patterns. */
1355static const unsigned char *const alt_patt[] = {
1356 f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
3ae729d5 1357 alt_9, alt_10, alt_11
62a02d25 1358};
bf649e72
L
1359#define alt64_9 (alt64_15 + 6) /* nopq 0L(%rax,%rax,1) */
1360#define alt64_10 (alt64_15 + 5) /* cs nopq 0L(%rax,%rax,1) */
1361/* data16 cs nopq 0L(%rax,%rax,1) */
1362#define alt64_11 (alt64_15 + 4)
1363/* data16 data16 cs nopq 0L(%rax,%rax,1) */
1364#define alt64_12 (alt64_15 + 3)
1365/* data16 data16 data16 cs nopq 0L(%rax,%rax,1) */
1366#define alt64_13 (alt64_15 + 2)
1367/* data16 data16 data16 data16 cs nopq 0L(%rax,%rax,1) */
1368#define alt64_14 (alt64_15 + 1)
1369/* data16 data16 data16 data16 data16 cs nopq 0L(%rax,%rax,1) */
1370static const unsigned char alt64_15[] =
1371 {0x66,0x66,0x66,0x66,0x66,0x2e,0x48,
1372 0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1373/* Long 64-bit NOPs patterns. */
1374static const unsigned char *const alt64_patt[] = {
1375 f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
1376 alt64_9, alt64_10, alt64_11,alt64_12, alt64_13, alt64_14, alt64_15
1377};
62a02d25
L
1378
1379/* Genenerate COUNT bytes of NOPs to WHERE from PATT with the maximum
1380 size of a single NOP instruction MAX_SINGLE_NOP_SIZE. */
1381
1382static void
1383i386_output_nops (char *where, const unsigned char *const *patt,
1384 int count, int max_single_nop_size)
1385
1386{
3ae729d5
L
1387 /* Place the longer NOP first. */
1388 int last;
1389 int offset;
3076e594
NC
1390 const unsigned char *nops;
1391
1392 if (max_single_nop_size < 1)
1393 {
1394 as_fatal (_("i386_output_nops called to generate nops of at most %d bytes!"),
1395 max_single_nop_size);
1396 return;
1397 }
1398
1399 nops = patt[max_single_nop_size - 1];
3ae729d5
L
1400 last = count % max_single_nop_size;
1401
1402 count -= last;
1403 for (offset = 0; offset < count; offset += max_single_nop_size)
1404 memcpy (where + offset, nops, max_single_nop_size);
1405
1406 if (last)
1407 {
1408 nops = patt[last - 1];
ad9f3230 1409 memcpy (where + offset, nops, last);
3ae729d5 1410 }
62a02d25
L
1411}
1412
3ae729d5
L
1413static INLINE int
1414fits_in_imm7 (offsetT num)
1415{
1416 return (num & 0x7f) == num;
1417}
1418
1419static INLINE int
1420fits_in_imm31 (offsetT num)
1421{
1422 return (num & 0x7fffffff) == num;
1423}
62a02d25
L
1424
1425/* Genenerate COUNT bytes of NOPs to WHERE with the maximum size of a
1426 single NOP instruction LIMIT. */
1427
1428void
3ae729d5 1429i386_generate_nops (fragS *fragP, char *where, offsetT count, int limit)
62a02d25 1430{
3ae729d5 1431 const unsigned char *const *patt = NULL;
62a02d25 1432 int max_single_nop_size;
3ae729d5
L
1433 /* Maximum number of NOPs before switching to jump over NOPs. */
1434 int max_number_of_nops;
62a02d25 1435
3ae729d5 1436 switch (fragP->fr_type)
62a02d25 1437 {
3ae729d5
L
1438 case rs_fill_nop:
1439 case rs_align_code:
1440 break;
e379e5f3
L
1441 case rs_machine_dependent:
1442 /* Allow NOP padding for jumps and calls. */
1443 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
1444 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
1445 break;
1446 /* Fall through. */
3ae729d5 1447 default:
62a02d25
L
1448 return;
1449 }
1450
ccc9c027
L
1451 /* We need to decide which NOP sequence to use for 32bit and
1452 64bit. When -mtune= is used:
4eed87de 1453
c8be4b6f 1454 1. For PROCESSOR_I?86, PROCESSOR_PENTIUM, PROCESSOR_IAMCU, and
76bc74dc 1455 PROCESSOR_GENERIC32, f32_patt will be used.
80b8656c
L
1456 2. For the rest, alt_patt will be used.
1457
1458 When -mtune= isn't used, alt_patt will be used if
d164359d 1459 cpu_arch_isa_flags has CpuNop. Otherwise, f32_patt/f64_patt will
76bc74dc 1460 be used.
ccc9c027
L
1461
1462 When -march= or .arch is used, we can't use anything beyond
1463 cpu_arch_isa_flags. */
1464
d12c7ab8 1465 if (fragP->tc_frag_data.code == CODE_16BIT)
ccc9c027 1466 {
3ae729d5
L
1467 patt = f16_patt;
1468 max_single_nop_size = sizeof (f16_patt) / sizeof (f16_patt[0]);
1469 /* Limit number of NOPs to 2 in 16-bit mode. */
1470 max_number_of_nops = 2;
252b5132 1471 }
33fef721 1472 else
ccc9c027 1473 {
d164359d 1474 patt = fragP->tc_frag_data.code == CODE_64BIT ? f64_patt : f32_patt;
fbf3f584 1475 if (fragP->tc_frag_data.isa == PROCESSOR_UNKNOWN)
ccc9c027 1476 {
cd75cd85
JB
1477 /* PROCESSOR_UNKNOWN means that all ISAs may be used, unless
1478 explicitly disabled. */
d12c7ab8 1479 switch (fragP->tc_frag_data.tune)
ccc9c027
L
1480 {
1481 case PROCESSOR_UNKNOWN:
1482 /* We use cpu_arch_isa_flags to check if we SHOULD
22109423 1483 optimize with nops. */
9f314ead 1484 if (fragP->tc_frag_data.isanop)
80b8656c 1485 patt = alt_patt;
ccc9c027 1486 break;
c8be4b6f 1487
ef05d495 1488 case PROCESSOR_CORE:
76bc74dc 1489 case PROCESSOR_CORE2:
bd5295b2 1490 case PROCESSOR_COREI7:
bf649e72
L
1491 if (fragP->tc_frag_data.cpunop)
1492 {
1493 if (fragP->tc_frag_data.code == CODE_64BIT)
1494 patt = alt64_patt;
1495 else
1496 patt = alt_patt;
1497 }
1498 break;
1499
1500 case PROCESSOR_PENTIUMPRO:
1501 case PROCESSOR_PENTIUM4:
1502 case PROCESSOR_NOCONA:
76bc74dc 1503 case PROCESSOR_GENERIC64:
ccc9c027
L
1504 case PROCESSOR_K6:
1505 case PROCESSOR_ATHLON:
1506 case PROCESSOR_K8:
4eed87de 1507 case PROCESSOR_AMDFAM10:
8aedb9fe 1508 case PROCESSOR_BD:
029f3522 1509 case PROCESSOR_ZNVER:
7b458c12 1510 case PROCESSOR_BT:
cd75cd85
JB
1511 if (fragP->tc_frag_data.cpunop)
1512 patt = alt_patt;
ccc9c027 1513 break;
cd75cd85 1514
76bc74dc 1515 case PROCESSOR_I386:
ccc9c027
L
1516 case PROCESSOR_I486:
1517 case PROCESSOR_PENTIUM:
c8be4b6f 1518 case PROCESSOR_I686:
81486035 1519 case PROCESSOR_IAMCU:
ccc9c027 1520 case PROCESSOR_GENERIC32:
ccc9c027 1521 break;
c368d2a8
JB
1522 case PROCESSOR_NONE:
1523 abort ();
4eed87de 1524 }
ccc9c027
L
1525 }
1526 else
1527 {
fbf3f584 1528 switch (fragP->tc_frag_data.tune)
ccc9c027
L
1529 {
1530 case PROCESSOR_UNKNOWN:
e6a14101 1531 /* When cpu_arch_isa is set, cpu_arch_tune shouldn't be
ccc9c027
L
1532 PROCESSOR_UNKNOWN. */
1533 abort ();
1534 break;
1535
cd75cd85 1536 default:
ccc9c027 1537 /* We use cpu_arch_isa_flags to check if we CAN optimize
22109423 1538 with nops. */
9f314ead 1539 if (fragP->tc_frag_data.isanop)
80b8656c 1540 patt = alt_patt;
ccc9c027 1541 break;
cd75cd85 1542
c368d2a8
JB
1543 case PROCESSOR_NONE:
1544 abort ();
4eed87de 1545 }
ccc9c027
L
1546 }
1547
bf649e72 1548 if (patt != alt_patt && patt != alt64_patt)
76bc74dc 1549 {
d164359d
JB
1550 max_single_nop_size = patt == f32_patt ? ARRAY_SIZE (f32_patt)
1551 : ARRAY_SIZE (f64_patt);
3ae729d5
L
1552 /* Limit number of NOPs to 2 for older processors. */
1553 max_number_of_nops = 2;
76bc74dc
L
1554 }
1555 else
1556 {
bf649e72
L
1557 max_single_nop_size = patt == alt_patt
1558 ? ARRAY_SIZE (alt_patt)
1559 : ARRAY_SIZE (alt64_patt);
3ae729d5
L
1560 /* Limit number of NOPs to 7 for newer processors. */
1561 max_number_of_nops = 7;
1562 }
1563 }
1564
1565 if (limit == 0)
1566 limit = max_single_nop_size;
1567
1568 if (fragP->fr_type == rs_fill_nop)
1569 {
1570 /* Output NOPs for .nop directive. */
1571 if (limit > max_single_nop_size)
1572 {
1573 as_bad_where (fragP->fr_file, fragP->fr_line,
1574 _("invalid single nop size: %d "
1575 "(expect within [0, %d])"),
1576 limit, max_single_nop_size);
1577 return;
1578 }
1579 }
e379e5f3 1580 else if (fragP->fr_type != rs_machine_dependent)
3ae729d5
L
1581 fragP->fr_var = count;
1582
0aa5d0c9
JB
1583 /* Emit a plain NOP first when the last thing we saw may not have been
1584 a proper instruction (e.g. a stand-alone prefix or .byte). */
1585 if (!fragP->tc_frag_data.last_insn_normal)
1586 {
1587 *where++ = 0x90;
1588 --count;
1589 }
1590
3ae729d5
L
1591 if ((count / max_single_nop_size) > max_number_of_nops)
1592 {
1593 /* Generate jump over NOPs. */
1594 offsetT disp = count - 2;
1595 if (fits_in_imm7 (disp))
1596 {
1597 /* Use "jmp disp8" if possible. */
1598 count = disp;
1599 where[0] = jump_disp8[0];
1600 where[1] = count;
1601 where += 2;
1602 }
1603 else
1604 {
1605 unsigned int size_of_jump;
1606
1607 if (flag_code == CODE_16BIT)
1608 {
1609 where[0] = jump16_disp32[0];
1610 where[1] = jump16_disp32[1];
1611 size_of_jump = 2;
1612 }
1613 else
1614 {
1615 where[0] = jump32_disp32[0];
1616 size_of_jump = 1;
1617 }
1618
1619 count -= size_of_jump + 4;
1620 if (!fits_in_imm31 (count))
1621 {
1622 as_bad_where (fragP->fr_file, fragP->fr_line,
1623 _("jump over nop padding out of range"));
1624 return;
1625 }
1626
1627 md_number_to_chars (where + size_of_jump, count, 4);
1628 where += size_of_jump + 4;
76bc74dc 1629 }
ccc9c027 1630 }
3ae729d5
L
1631
1632 /* Generate multiple NOPs. */
1633 i386_output_nops (where, patt, count, limit);
252b5132
RH
1634}
1635
c6fb90c8 1636static INLINE int
0dfbf9d7 1637operand_type_all_zero (const union i386_operand_type *x)
40fb9820 1638{
0dfbf9d7 1639 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1640 {
1641 case 3:
0dfbf9d7 1642 if (x->array[2])
c6fb90c8 1643 return 0;
1a0670f3 1644 /* Fall through. */
c6fb90c8 1645 case 2:
0dfbf9d7 1646 if (x->array[1])
c6fb90c8 1647 return 0;
1a0670f3 1648 /* Fall through. */
c6fb90c8 1649 case 1:
0dfbf9d7 1650 return !x->array[0];
c6fb90c8
L
1651 default:
1652 abort ();
1653 }
40fb9820
L
1654}
1655
c6fb90c8 1656static INLINE void
0dfbf9d7 1657operand_type_set (union i386_operand_type *x, unsigned int v)
40fb9820 1658{
0dfbf9d7 1659 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1660 {
1661 case 3:
0dfbf9d7 1662 x->array[2] = v;
1a0670f3 1663 /* Fall through. */
c6fb90c8 1664 case 2:
0dfbf9d7 1665 x->array[1] = v;
1a0670f3 1666 /* Fall through. */
c6fb90c8 1667 case 1:
0dfbf9d7 1668 x->array[0] = v;
1a0670f3 1669 /* Fall through. */
c6fb90c8
L
1670 break;
1671 default:
1672 abort ();
1673 }
bab6aec1
JB
1674
1675 x->bitfield.class = ClassNone;
75e5731b 1676 x->bitfield.instance = InstanceNone;
c6fb90c8 1677}
40fb9820 1678
c6fb90c8 1679static INLINE int
0dfbf9d7
L
1680operand_type_equal (const union i386_operand_type *x,
1681 const union i386_operand_type *y)
c6fb90c8 1682{
0dfbf9d7 1683 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1684 {
1685 case 3:
0dfbf9d7 1686 if (x->array[2] != y->array[2])
c6fb90c8 1687 return 0;
1a0670f3 1688 /* Fall through. */
c6fb90c8 1689 case 2:
0dfbf9d7 1690 if (x->array[1] != y->array[1])
c6fb90c8 1691 return 0;
1a0670f3 1692 /* Fall through. */
c6fb90c8 1693 case 1:
0dfbf9d7 1694 return x->array[0] == y->array[0];
c6fb90c8
L
1695 break;
1696 default:
1697 abort ();
1698 }
1699}
40fb9820 1700
734dfd1c 1701static INLINE bool
a5e91879 1702_is_cpu (const i386_cpu_attr *a, enum i386_cpu cpu)
734dfd1c
JB
1703{
1704 switch (cpu)
1705 {
a5e91879
JB
1706 case Cpu287: return a->bitfield.cpu287;
1707 case Cpu387: return a->bitfield.cpu387;
1708 case Cpu3dnow: return a->bitfield.cpu3dnow;
1709 case Cpu3dnowA: return a->bitfield.cpu3dnowa;
1710 case CpuAVX: return a->bitfield.cpuavx;
1711 case CpuHLE: return a->bitfield.cpuhle;
1712 case CpuAVX512F: return a->bitfield.cpuavx512f;
1713 case CpuAVX512VL: return a->bitfield.cpuavx512vl;
80d61d8d 1714 case CpuAPX_F: return a->bitfield.cpuapx_f;
a5e91879
JB
1715 case Cpu64: return a->bitfield.cpu64;
1716 case CpuNo64: return a->bitfield.cpuno64;
734dfd1c
JB
1717 default:
1718 gas_assert (cpu < CpuAttrEnums);
1719 }
a5e91879
JB
1720 return a->bitfield.isa == cpu + 1u;
1721}
1722
1723static INLINE bool
1724is_cpu (const insn_template *t, enum i386_cpu cpu)
1725{
1726 return _is_cpu(&t->cpu, cpu);
1727}
1728
1729static INLINE bool
1730maybe_cpu (const insn_template *t, enum i386_cpu cpu)
1731{
1732 return _is_cpu(&t->cpu_any, cpu);
734dfd1c
JB
1733}
1734
1735static i386_cpu_flags cpu_flags_from_attr (i386_cpu_attr a)
1736{
1737 const unsigned int bps = sizeof (a.array[0]) * CHAR_BIT;
1738 i386_cpu_flags f = { .array[0] = 0 };
1739
a12915cc 1740 switch (ARRAY_SIZE (a.array))
734dfd1c
JB
1741 {
1742 case 1:
1743 f.array[CpuAttrEnums / bps]
a12915cc
JB
1744#ifndef WORDS_BIGENDIAN
1745 |= (a.array[0] >> CpuIsaBits) << (CpuAttrEnums % bps);
1746#else
1747 |= (a.array[0] << CpuIsaBits) >> (CpuAttrEnums % bps);
1748#endif
1749 if (CpuMax / bps > CpuAttrEnums / bps)
734dfd1c 1750 f.array[CpuAttrEnums / bps + 1]
a12915cc 1751#ifndef WORDS_BIGENDIAN
734dfd1c 1752 = (a.array[0] >> CpuIsaBits) >> (bps - CpuAttrEnums % bps);
a12915cc
JB
1753#else
1754 = (a.array[0] << CpuIsaBits) << (bps - CpuAttrEnums % bps);
1755#endif
734dfd1c 1756 break;
a12915cc 1757
734dfd1c
JB
1758 default:
1759 abort ();
1760 }
1761
1762 if (a.bitfield.isa)
a12915cc 1763#ifndef WORDS_BIGENDIAN
734dfd1c 1764 f.array[(a.bitfield.isa - 1) / bps] |= 1u << ((a.bitfield.isa - 1) % bps);
a12915cc
JB
1765#else
1766 f.array[(a.bitfield.isa - 1) / bps] |= 1u << (~(a.bitfield.isa - 1) % bps);
1767#endif
734dfd1c
JB
1768
1769 return f;
1770}
1771
0dfbf9d7
L
1772static INLINE int
1773cpu_flags_all_zero (const union i386_cpu_flags *x)
1774{
1775 switch (ARRAY_SIZE(x->array))
1776 {
75f8266a
KL
1777 case 5:
1778 if (x->array[4])
1779 return 0;
1780 /* Fall through. */
53467f57
IT
1781 case 4:
1782 if (x->array[3])
1783 return 0;
1784 /* Fall through. */
0dfbf9d7
L
1785 case 3:
1786 if (x->array[2])
1787 return 0;
1a0670f3 1788 /* Fall through. */
0dfbf9d7
L
1789 case 2:
1790 if (x->array[1])
1791 return 0;
1a0670f3 1792 /* Fall through. */
0dfbf9d7
L
1793 case 1:
1794 return !x->array[0];
1795 default:
1796 abort ();
1797 }
1798}
1799
0dfbf9d7
L
1800static INLINE int
1801cpu_flags_equal (const union i386_cpu_flags *x,
1802 const union i386_cpu_flags *y)
1803{
1804 switch (ARRAY_SIZE(x->array))
1805 {
75f8266a
KL
1806 case 5:
1807 if (x->array[4] != y->array[4])
1808 return 0;
1809 /* Fall through. */
53467f57
IT
1810 case 4:
1811 if (x->array[3] != y->array[3])
1812 return 0;
1813 /* Fall through. */
0dfbf9d7
L
1814 case 3:
1815 if (x->array[2] != y->array[2])
1816 return 0;
1a0670f3 1817 /* Fall through. */
0dfbf9d7
L
1818 case 2:
1819 if (x->array[1] != y->array[1])
1820 return 0;
1a0670f3 1821 /* Fall through. */
0dfbf9d7
L
1822 case 1:
1823 return x->array[0] == y->array[0];
1824 break;
1825 default:
1826 abort ();
1827 }
1828}
c6fb90c8
L
1829
1830static INLINE int
c0260ac6 1831cpu_flags_check_cpu64 (const insn_template *t)
c6fb90c8 1832{
c0260ac6
JB
1833 return flag_code == CODE_64BIT
1834 ? !t->cpu.bitfield.cpuno64
1835 : !t->cpu.bitfield.cpu64;
40fb9820
L
1836}
1837
c6fb90c8
L
1838static INLINE i386_cpu_flags
1839cpu_flags_and (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1840{
c6fb90c8
L
1841 switch (ARRAY_SIZE (x.array))
1842 {
75f8266a
KL
1843 case 5:
1844 x.array [4] &= y.array [4];
1845 /* Fall through. */
53467f57
IT
1846 case 4:
1847 x.array [3] &= y.array [3];
1848 /* Fall through. */
c6fb90c8
L
1849 case 3:
1850 x.array [2] &= y.array [2];
1a0670f3 1851 /* Fall through. */
c6fb90c8
L
1852 case 2:
1853 x.array [1] &= y.array [1];
1a0670f3 1854 /* Fall through. */
c6fb90c8
L
1855 case 1:
1856 x.array [0] &= y.array [0];
1857 break;
1858 default:
1859 abort ();
1860 }
1861 return x;
1862}
40fb9820 1863
c6fb90c8
L
1864static INLINE i386_cpu_flags
1865cpu_flags_or (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1866{
c6fb90c8 1867 switch (ARRAY_SIZE (x.array))
40fb9820 1868 {
75f8266a
KL
1869 case 5:
1870 x.array [4] |= y.array [4];
1871 /* Fall through. */
53467f57
IT
1872 case 4:
1873 x.array [3] |= y.array [3];
1874 /* Fall through. */
c6fb90c8
L
1875 case 3:
1876 x.array [2] |= y.array [2];
1a0670f3 1877 /* Fall through. */
c6fb90c8
L
1878 case 2:
1879 x.array [1] |= y.array [1];
1a0670f3 1880 /* Fall through. */
c6fb90c8
L
1881 case 1:
1882 x.array [0] |= y.array [0];
40fb9820
L
1883 break;
1884 default:
1885 abort ();
1886 }
40fb9820
L
1887 return x;
1888}
1889
309d3373
JB
1890static INLINE i386_cpu_flags
1891cpu_flags_and_not (i386_cpu_flags x, i386_cpu_flags y)
1892{
1893 switch (ARRAY_SIZE (x.array))
1894 {
75f8266a
KL
1895 case 5:
1896 x.array [4] &= ~y.array [4];
1897 /* Fall through. */
53467f57
IT
1898 case 4:
1899 x.array [3] &= ~y.array [3];
1900 /* Fall through. */
309d3373
JB
1901 case 3:
1902 x.array [2] &= ~y.array [2];
1a0670f3 1903 /* Fall through. */
309d3373
JB
1904 case 2:
1905 x.array [1] &= ~y.array [1];
1a0670f3 1906 /* Fall through. */
309d3373
JB
1907 case 1:
1908 x.array [0] &= ~y.array [0];
1909 break;
1910 default:
1911 abort ();
1912 }
1913 return x;
1914}
1915
6c0946d0
JB
1916static const i386_cpu_flags avx512 = CPU_ANY_AVX512F_FLAGS;
1917
6177c84d 1918static INLINE bool need_evex_encoding (const insn_template *t)
a6f3add0 1919{
e346d50a
JB
1920 return i.encoding == encoding_evex
1921 || i.encoding == encoding_evex512
eb3f3841 1922 || (t->opcode_modifier.vex && i.encoding == encoding_egpr)
a6f3add0
JB
1923 || i.mask.reg;
1924}
1925
c0f3af97
L
1926#define CPU_FLAGS_ARCH_MATCH 0x1
1927#define CPU_FLAGS_64BIT_MATCH 0x2
1928
c0f3af97 1929#define CPU_FLAGS_PERFECT_MATCH \
db12e14e 1930 (CPU_FLAGS_ARCH_MATCH | CPU_FLAGS_64BIT_MATCH)
c0f3af97
L
1931
1932/* Return CPU flags match bits. */
3629bb00 1933
40fb9820 1934static int
d3ce72d0 1935cpu_flags_match (const insn_template *t)
40fb9820 1936{
a5e91879
JB
1937 i386_cpu_flags cpu, active, all = cpu_flags_from_attr (t->cpu);
1938 i386_cpu_flags any = cpu_flags_from_attr (t->cpu_any);
c0260ac6 1939 int match = cpu_flags_check_cpu64 (t) ? CPU_FLAGS_64BIT_MATCH : 0;
40fb9820 1940
a5e91879
JB
1941 all.bitfield.cpu64 = 0;
1942 all.bitfield.cpuno64 = 0;
1943 gas_assert (!any.bitfield.cpu64);
1944 gas_assert (!any.bitfield.cpuno64);
40fb9820 1945
a5e91879 1946 if (cpu_flags_all_zero (&all) && cpu_flags_all_zero (&any))
c0f3af97
L
1947 {
1948 /* This instruction is available on all archs. */
a5e91879 1949 return match | CPU_FLAGS_ARCH_MATCH;
c0f3af97 1950 }
3e624fa4 1951
a5e91879 1952 /* This instruction is available only on some archs. */
3629bb00 1953
a5e91879
JB
1954 /* Dual VEX/EVEX templates may need stripping of one of the flags. */
1955 if (t->opcode_modifier.vex && t->opcode_modifier.evex)
1956 {
1957 /* Dual AVX/AVX512 templates need to retain AVX512* only if we already
1958 know that EVEX encoding will be needed. */
1959 if ((any.bitfield.cpuavx || any.bitfield.cpuavx2 || any.bitfield.cpufma)
1960 && (any.bitfield.cpuavx512f || any.bitfield.cpuavx512vl))
a6f3add0 1961 {
6177c84d 1962 if (need_evex_encoding (t))
a6f3add0 1963 {
a5e91879
JB
1964 any.bitfield.cpuavx = 0;
1965 any.bitfield.cpuavx2 = 0;
1966 any.bitfield.cpufma = 0;
1967 }
6177c84d 1968 /* need_evex_encoding(t) isn't reliable before operands were
a5e91879
JB
1969 parsed. */
1970 else if (i.operands)
1971 {
1972 any.bitfield.cpuavx512f = 0;
1973 any.bitfield.cpuavx512vl = 0;
a6f3add0
JB
1974 }
1975 }
7c3df3c6
JB
1976
1977 /* Dual non-APX/APX templates need massaging from what APX_F() in the
1978 opcode table has produced. While the direct transformation of the
1979 incoming cpuid&(cpuid|APX_F) would be to cpuid&(cpuid) / cpuid&(APX_F)
1980 respectively, it's cheaper to move to just cpuid / cpuid&APX_F
1981 instead. */
1982 if (any.bitfield.cpuapx_f
1983 && (any.bitfield.cpubmi || any.bitfield.cpubmi2
1984 || any.bitfield.cpuavx512f || any.bitfield.cpuavx512bw
1985 || any.bitfield.cpuavx512dq || any.bitfield.cpuamx_tile
5190fa38 1986 || any.bitfield.cpucmpccxadd || any.bitfield.cpuuser_msr))
7c3df3c6
JB
1987 {
1988 /* These checks (verifying that APX_F() was properly used in the
1989 opcode table entry) make sure there's no need for an "else" to
1990 the "if()" below. */
1991 gas_assert (!cpu_flags_all_zero (&all));
1992 cpu = cpu_flags_and (all, any);
1993 gas_assert (cpu_flags_equal (&cpu, &all));
1994
1995 if (need_evex_encoding (t))
1996 all = any;
1997
1998 memset (&any, 0, sizeof (any));
1999 }
a5e91879 2000 }
a6f3add0 2001
a5e91879
JB
2002 if (flag_code != CODE_64BIT)
2003 active = cpu_flags_and_not (cpu_arch_flags, cpu_64_flags);
2004 else
2005 active = cpu_arch_flags;
2006 cpu = cpu_flags_and (all, active);
2007 if (cpu_flags_equal (&cpu, &all))
2008 {
22c36940
JB
2009 /* AVX and AVX2 present at the same time express an operand size
2010 dependency - strip AVX2 for the purposes here. The operand size
2011 dependent check occurs in check_vecOperands(). */
a5e91879
JB
2012 if (any.bitfield.cpuavx && any.bitfield.cpuavx2)
2013 any.bitfield.cpuavx2 = 0;
22c36940 2014
a5e91879
JB
2015 cpu = cpu_flags_and (any, active);
2016 if (cpu_flags_all_zero (&any) || !cpu_flags_all_zero (&cpu))
b58829cd 2017 match |= CPU_FLAGS_ARCH_MATCH;
3629bb00 2018 }
c0f3af97 2019 return match;
40fb9820
L
2020}
2021
c6fb90c8
L
2022static INLINE i386_operand_type
2023operand_type_and (i386_operand_type x, i386_operand_type y)
40fb9820 2024{
bab6aec1
JB
2025 if (x.bitfield.class != y.bitfield.class)
2026 x.bitfield.class = ClassNone;
75e5731b
JB
2027 if (x.bitfield.instance != y.bitfield.instance)
2028 x.bitfield.instance = InstanceNone;
bab6aec1 2029
c6fb90c8
L
2030 switch (ARRAY_SIZE (x.array))
2031 {
2032 case 3:
2033 x.array [2] &= y.array [2];
1a0670f3 2034 /* Fall through. */
c6fb90c8
L
2035 case 2:
2036 x.array [1] &= y.array [1];
1a0670f3 2037 /* Fall through. */
c6fb90c8
L
2038 case 1:
2039 x.array [0] &= y.array [0];
2040 break;
2041 default:
2042 abort ();
2043 }
2044 return x;
40fb9820
L
2045}
2046
73053c1f
JB
2047static INLINE i386_operand_type
2048operand_type_and_not (i386_operand_type x, i386_operand_type y)
2049{
bab6aec1 2050 gas_assert (y.bitfield.class == ClassNone);
75e5731b 2051 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 2052
73053c1f
JB
2053 switch (ARRAY_SIZE (x.array))
2054 {
2055 case 3:
2056 x.array [2] &= ~y.array [2];
2057 /* Fall through. */
2058 case 2:
2059 x.array [1] &= ~y.array [1];
2060 /* Fall through. */
2061 case 1:
2062 x.array [0] &= ~y.array [0];
2063 break;
2064 default:
2065 abort ();
2066 }
2067 return x;
2068}
2069
c6fb90c8
L
2070static INLINE i386_operand_type
2071operand_type_or (i386_operand_type x, i386_operand_type y)
40fb9820 2072{
bab6aec1
JB
2073 gas_assert (x.bitfield.class == ClassNone ||
2074 y.bitfield.class == ClassNone ||
2075 x.bitfield.class == y.bitfield.class);
75e5731b
JB
2076 gas_assert (x.bitfield.instance == InstanceNone ||
2077 y.bitfield.instance == InstanceNone ||
2078 x.bitfield.instance == y.bitfield.instance);
bab6aec1 2079
c6fb90c8 2080 switch (ARRAY_SIZE (x.array))
40fb9820 2081 {
c6fb90c8
L
2082 case 3:
2083 x.array [2] |= y.array [2];
1a0670f3 2084 /* Fall through. */
c6fb90c8
L
2085 case 2:
2086 x.array [1] |= y.array [1];
1a0670f3 2087 /* Fall through. */
c6fb90c8
L
2088 case 1:
2089 x.array [0] |= y.array [0];
40fb9820
L
2090 break;
2091 default:
2092 abort ();
2093 }
c6fb90c8
L
2094 return x;
2095}
40fb9820 2096
c6fb90c8
L
2097static INLINE i386_operand_type
2098operand_type_xor (i386_operand_type x, i386_operand_type y)
2099{
bab6aec1 2100 gas_assert (y.bitfield.class == ClassNone);
75e5731b 2101 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 2102
c6fb90c8
L
2103 switch (ARRAY_SIZE (x.array))
2104 {
2105 case 3:
2106 x.array [2] ^= y.array [2];
1a0670f3 2107 /* Fall through. */
c6fb90c8
L
2108 case 2:
2109 x.array [1] ^= y.array [1];
1a0670f3 2110 /* Fall through. */
c6fb90c8
L
2111 case 1:
2112 x.array [0] ^= y.array [0];
2113 break;
2114 default:
2115 abort ();
2116 }
40fb9820
L
2117 return x;
2118}
2119
05909f23
JB
2120static const i386_operand_type anydisp = {
2121 .bitfield = { .disp8 = 1, .disp16 = 1, .disp32 = 1, .disp64 = 1 }
2122};
40fb9820
L
2123
2124enum operand_type
2125{
2126 reg,
40fb9820
L
2127 imm,
2128 disp,
2129 anymem
2130};
2131
c6fb90c8 2132static INLINE int
40fb9820
L
2133operand_type_check (i386_operand_type t, enum operand_type c)
2134{
2135 switch (c)
2136 {
2137 case reg:
bab6aec1 2138 return t.bitfield.class == Reg;
40fb9820 2139
40fb9820
L
2140 case imm:
2141 return (t.bitfield.imm8
2142 || t.bitfield.imm8s
2143 || t.bitfield.imm16
2144 || t.bitfield.imm32
2145 || t.bitfield.imm32s
2146 || t.bitfield.imm64);
2147
2148 case disp:
2149 return (t.bitfield.disp8
2150 || t.bitfield.disp16
2151 || t.bitfield.disp32
40fb9820
L
2152 || t.bitfield.disp64);
2153
2154 case anymem:
2155 return (t.bitfield.disp8
2156 || t.bitfield.disp16
2157 || t.bitfield.disp32
40fb9820
L
2158 || t.bitfield.disp64
2159 || t.bitfield.baseindex);
2160
2161 default:
2162 abort ();
2163 }
2cfe26b6
AM
2164
2165 return 0;
40fb9820
L
2166}
2167
7a54636a
L
2168/* Return 1 if there is no conflict in 8bit/16bit/32bit/64bit/80bit size
2169 between operand GIVEN and opeand WANTED for instruction template T. */
5c07affc
L
2170
2171static INLINE int
7a54636a
L
2172match_operand_size (const insn_template *t, unsigned int wanted,
2173 unsigned int given)
5c07affc 2174{
3ac21baa
JB
2175 return !((i.types[given].bitfield.byte
2176 && !t->operand_types[wanted].bitfield.byte)
2177 || (i.types[given].bitfield.word
2178 && !t->operand_types[wanted].bitfield.word)
2179 || (i.types[given].bitfield.dword
2180 && !t->operand_types[wanted].bitfield.dword)
2181 || (i.types[given].bitfield.qword
9db83a32
JB
2182 && (!t->operand_types[wanted].bitfield.qword
2183 /* Don't allow 64-bit (memory) operands outside of 64-bit
2184 mode, when they're used where a 64-bit GPR could also
2185 be used. Checking is needed for Intel Syntax only. */
2186 || (intel_syntax
2187 && flag_code != CODE_64BIT
2188 && (t->operand_types[wanted].bitfield.class == Reg
2189 || t->operand_types[wanted].bitfield.class == Accum
2190 || t->opcode_modifier.isstring))))
3ac21baa
JB
2191 || (i.types[given].bitfield.tbyte
2192 && !t->operand_types[wanted].bitfield.tbyte));
5c07affc
L
2193}
2194
dd40ce22
L
2195/* Return 1 if there is no conflict in SIMD register between operand
2196 GIVEN and opeand WANTED for instruction template T. */
1b54b8d7
JB
2197
2198static INLINE int
dd40ce22
L
2199match_simd_size (const insn_template *t, unsigned int wanted,
2200 unsigned int given)
1b54b8d7 2201{
3ac21baa
JB
2202 return !((i.types[given].bitfield.xmmword
2203 && !t->operand_types[wanted].bitfield.xmmword)
2204 || (i.types[given].bitfield.ymmword
2205 && !t->operand_types[wanted].bitfield.ymmword)
2206 || (i.types[given].bitfield.zmmword
260cd341
LC
2207 && !t->operand_types[wanted].bitfield.zmmword)
2208 || (i.types[given].bitfield.tmmword
2209 && !t->operand_types[wanted].bitfield.tmmword));
1b54b8d7
JB
2210}
2211
7a54636a
L
2212/* Return 1 if there is no conflict in any size between operand GIVEN
2213 and opeand WANTED for instruction template T. */
5c07affc
L
2214
2215static INLINE int
dd40ce22
L
2216match_mem_size (const insn_template *t, unsigned int wanted,
2217 unsigned int given)
5c07affc 2218{
7a54636a 2219 return (match_operand_size (t, wanted, given)
3ac21baa 2220 && !((i.types[given].bitfield.unspecified
5273a3cd 2221 && !i.broadcast.type
a5748e0d 2222 && !i.broadcast.bytes
3ac21baa
JB
2223 && !t->operand_types[wanted].bitfield.unspecified)
2224 || (i.types[given].bitfield.fword
2225 && !t->operand_types[wanted].bitfield.fword)
1b54b8d7
JB
2226 /* For scalar opcode templates to allow register and memory
2227 operands at the same time, some special casing is needed
d6793fa1
JB
2228 here. Also for v{,p}broadcast*, {,v}pmov{s,z}*, and
2229 down-conversion vpmov*. */
3528c362 2230 || ((t->operand_types[wanted].bitfield.class == RegSIMD
bc49bfd8
JB
2231 && t->operand_types[wanted].bitfield.byte
2232 + t->operand_types[wanted].bitfield.word
2233 + t->operand_types[wanted].bitfield.dword
2234 + t->operand_types[wanted].bitfield.qword
2235 > !!t->opcode_modifier.broadcast)
3ac21baa
JB
2236 ? (i.types[given].bitfield.xmmword
2237 || i.types[given].bitfield.ymmword
2238 || i.types[given].bitfield.zmmword)
2239 : !match_simd_size(t, wanted, given))));
5c07affc
L
2240}
2241
3ac21baa
JB
2242/* Return value has MATCH_STRAIGHT set if there is no size conflict on any
2243 operands for instruction template T, and it has MATCH_REVERSE set if there
2244 is no size conflict on any operands for the template with operands reversed
2245 (and the template allows for reversing in the first place). */
5c07affc 2246
3ac21baa
JB
2247#define MATCH_STRAIGHT 1
2248#define MATCH_REVERSE 2
2249
2250static INLINE unsigned int
d3ce72d0 2251operand_size_match (const insn_template *t)
5c07affc 2252{
3ac21baa 2253 unsigned int j, match = MATCH_STRAIGHT;
5c07affc 2254
0cfa3eb3 2255 /* Don't check non-absolute jump instructions. */
5c07affc 2256 if (t->opcode_modifier.jump
0cfa3eb3 2257 && t->opcode_modifier.jump != JUMP_ABSOLUTE)
5c07affc
L
2258 return match;
2259
2260 /* Check memory and accumulator operand size. */
2261 for (j = 0; j < i.operands; j++)
2262 {
3528c362
JB
2263 if (i.types[j].bitfield.class != Reg
2264 && i.types[j].bitfield.class != RegSIMD
255571cd 2265 && t->opcode_modifier.operandconstraint == ANY_SIZE)
5c07affc
L
2266 continue;
2267
bab6aec1 2268 if (t->operand_types[j].bitfield.class == Reg
7a54636a 2269 && !match_operand_size (t, j, j))
5c07affc
L
2270 {
2271 match = 0;
2272 break;
2273 }
2274
3528c362 2275 if (t->operand_types[j].bitfield.class == RegSIMD
3ac21baa 2276 && !match_simd_size (t, j, j))
1b54b8d7
JB
2277 {
2278 match = 0;
2279 break;
2280 }
2281
75e5731b 2282 if (t->operand_types[j].bitfield.instance == Accum
7a54636a 2283 && (!match_operand_size (t, j, j) || !match_simd_size (t, j, j)))
1b54b8d7
JB
2284 {
2285 match = 0;
2286 break;
2287 }
2288
c48dadc9 2289 if ((i.flags[j] & Operand_Mem) && !match_mem_size (t, j, j))
5c07affc
L
2290 {
2291 match = 0;
2292 break;
2293 }
2294 }
2295
3ac21baa 2296 if (!t->opcode_modifier.d)
7b94647a 2297 return match;
5c07affc
L
2298
2299 /* Check reverse. */
aa180741 2300 gas_assert (i.operands >= 2);
5c07affc 2301
f5eb1d70 2302 for (j = 0; j < i.operands; j++)
5c07affc 2303 {
f5eb1d70
JB
2304 unsigned int given = i.operands - j - 1;
2305
aa180741 2306 /* For FMA4 and XOP insns VEX.W controls just the first two
3083f376 2307 register operands. And APX_F insns just swap the two source operands,
2308 with the 3rd one being the destination. */
2309 if (is_cpu (t, CpuFMA4) || is_cpu (t, CpuXOP)
2310 || is_cpu (t, CpuAPX_F))
8bd915b7
JB
2311 given = j < 2 ? 1 - j : j;
2312
bab6aec1 2313 if (t->operand_types[j].bitfield.class == Reg
f5eb1d70 2314 && !match_operand_size (t, j, given))
7b94647a 2315 return match;
5c07affc 2316
3528c362 2317 if (t->operand_types[j].bitfield.class == RegSIMD
f5eb1d70 2318 && !match_simd_size (t, j, given))
7b94647a 2319 return match;
dbbc8b7e 2320
75e5731b 2321 if (t->operand_types[j].bitfield.instance == Accum
f5eb1d70
JB
2322 && (!match_operand_size (t, j, given)
2323 || !match_simd_size (t, j, given)))
7b94647a 2324 return match;
dbbc8b7e 2325
f5eb1d70 2326 if ((i.flags[given] & Operand_Mem) && !match_mem_size (t, j, given))
7b94647a 2327 return match;
5c07affc
L
2328 }
2329
3ac21baa 2330 return match | MATCH_REVERSE;
5c07affc
L
2331}
2332
c6fb90c8 2333static INLINE int
40fb9820
L
2334operand_type_match (i386_operand_type overlap,
2335 i386_operand_type given)
2336{
2337 i386_operand_type temp = overlap;
2338
7d5e4556 2339 temp.bitfield.unspecified = 0;
5c07affc
L
2340 temp.bitfield.byte = 0;
2341 temp.bitfield.word = 0;
2342 temp.bitfield.dword = 0;
2343 temp.bitfield.fword = 0;
2344 temp.bitfield.qword = 0;
2345 temp.bitfield.tbyte = 0;
2346 temp.bitfield.xmmword = 0;
c0f3af97 2347 temp.bitfield.ymmword = 0;
43234a1e 2348 temp.bitfield.zmmword = 0;
260cd341 2349 temp.bitfield.tmmword = 0;
0dfbf9d7 2350 if (operand_type_all_zero (&temp))
891edac4 2351 goto mismatch;
40fb9820 2352
6f2f06be 2353 if (given.bitfield.baseindex == overlap.bitfield.baseindex)
891edac4
L
2354 return 1;
2355
dc1e8a47 2356 mismatch:
a65babc9 2357 i.error = operand_type_mismatch;
891edac4 2358 return 0;
40fb9820
L
2359}
2360
7d5e4556 2361/* If given types g0 and g1 are registers they must be of the same type
10c17abd 2362 unless the expected operand type register overlap is null.
8ee52bcf 2363 Intel syntax sized memory operands are also checked here. */
40fb9820 2364
c6fb90c8 2365static INLINE int
dc821c5f 2366operand_type_register_match (i386_operand_type g0,
40fb9820 2367 i386_operand_type t0,
40fb9820
L
2368 i386_operand_type g1,
2369 i386_operand_type t1)
2370{
bab6aec1 2371 if (g0.bitfield.class != Reg
3528c362 2372 && g0.bitfield.class != RegSIMD
8ee52bcf
JB
2373 && (g0.bitfield.unspecified
2374 || !operand_type_check (g0, anymem)))
40fb9820
L
2375 return 1;
2376
bab6aec1 2377 if (g1.bitfield.class != Reg
3528c362 2378 && g1.bitfield.class != RegSIMD
8ee52bcf
JB
2379 && (g1.bitfield.unspecified
2380 || !operand_type_check (g1, anymem)))
40fb9820
L
2381 return 1;
2382
dc821c5f
JB
2383 if (g0.bitfield.byte == g1.bitfield.byte
2384 && g0.bitfield.word == g1.bitfield.word
2385 && g0.bitfield.dword == g1.bitfield.dword
10c17abd
JB
2386 && g0.bitfield.qword == g1.bitfield.qword
2387 && g0.bitfield.xmmword == g1.bitfield.xmmword
2388 && g0.bitfield.ymmword == g1.bitfield.ymmword
2389 && g0.bitfield.zmmword == g1.bitfield.zmmword)
40fb9820
L
2390 return 1;
2391
c4d09633
JB
2392 /* If expectations overlap in no more than a single size, all is fine. */
2393 g0 = operand_type_and (t0, t1);
2394 if (g0.bitfield.byte
2395 + g0.bitfield.word
2396 + g0.bitfield.dword
2397 + g0.bitfield.qword
2398 + g0.bitfield.xmmword
2399 + g0.bitfield.ymmword
2400 + g0.bitfield.zmmword <= 1)
891edac4
L
2401 return 1;
2402
a65babc9 2403 i.error = register_type_mismatch;
891edac4
L
2404
2405 return 0;
40fb9820
L
2406}
2407
4c692bc7
JB
2408static INLINE unsigned int
2409register_number (const reg_entry *r)
2410{
2411 unsigned int nr = r->reg_num;
2412
2413 if (r->reg_flags & RegRex)
2414 nr += 8;
2415
80d61d8d 2416 if (r->reg_flags & (RegVRex | RegRex2))
200cbe0f
L
2417 nr += 16;
2418
4c692bc7
JB
2419 return nr;
2420}
2421
252b5132 2422static INLINE unsigned int
40fb9820 2423mode_from_disp_size (i386_operand_type t)
252b5132 2424{
b5014f7a 2425 if (t.bitfield.disp8)
40fb9820
L
2426 return 1;
2427 else if (t.bitfield.disp16
a775efc8 2428 || t.bitfield.disp32)
40fb9820
L
2429 return 2;
2430 else
2431 return 0;
252b5132
RH
2432}
2433
2434static INLINE int
65879393 2435fits_in_signed_byte (addressT num)
252b5132 2436{
65879393 2437 return num + 0x80 <= 0xff;
47926f60 2438}
252b5132
RH
2439
2440static INLINE int
65879393 2441fits_in_unsigned_byte (addressT num)
252b5132 2442{
65879393 2443 return num <= 0xff;
47926f60 2444}
252b5132
RH
2445
2446static INLINE int
65879393 2447fits_in_unsigned_word (addressT num)
252b5132 2448{
65879393 2449 return num <= 0xffff;
47926f60 2450}
252b5132
RH
2451
2452static INLINE int
65879393 2453fits_in_signed_word (addressT num)
252b5132 2454{
65879393 2455 return num + 0x8000 <= 0xffff;
47926f60 2456}
2a962e6d 2457
3e73aa7c 2458static INLINE int
65879393 2459fits_in_signed_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2460{
2461#ifndef BFD64
2462 return 1;
2463#else
65879393 2464 return num + 0x80000000 <= 0xffffffff;
3e73aa7c
JH
2465#endif
2466} /* fits_in_signed_long() */
2a962e6d 2467
3e73aa7c 2468static INLINE int
65879393 2469fits_in_unsigned_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2470{
2471#ifndef BFD64
2472 return 1;
2473#else
65879393 2474 return num <= 0xffffffff;
3e73aa7c
JH
2475#endif
2476} /* fits_in_unsigned_long() */
252b5132 2477
a442cac5
JB
2478static INLINE valueT extend_to_32bit_address (addressT num)
2479{
2480#ifdef BFD64
2481 if (fits_in_unsigned_long(num))
2482 return (num ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
2483
2484 if (!fits_in_signed_long (num))
2485 return num & 0xffffffff;
2486#endif
2487
2488 return num;
2489}
2490
43234a1e 2491static INLINE int
b5014f7a 2492fits_in_disp8 (offsetT num)
43234a1e
L
2493{
2494 int shift = i.memshift;
2495 unsigned int mask;
2496
2497 if (shift == -1)
2498 abort ();
2499
2500 mask = (1 << shift) - 1;
2501
2502 /* Return 0 if NUM isn't properly aligned. */
2503 if ((num & mask))
2504 return 0;
2505
2506 /* Check if NUM will fit in 8bit after shift. */
2507 return fits_in_signed_byte (num >> shift);
2508}
2509
a683cc34
SP
2510static INLINE int
2511fits_in_imm4 (offsetT num)
2512{
0ff3b7d0 2513 /* Despite the name, check for imm3 if we're dealing with EVEX. */
eb3f3841
JB
2514 return (num & (i.encoding != encoding_evex
2515 && i.encoding != encoding_egpr ? 0xf : 7)) == num;
a683cc34
SP
2516}
2517
40fb9820 2518static i386_operand_type
e3bb37b5 2519smallest_imm_type (offsetT num)
252b5132 2520{
40fb9820 2521 i386_operand_type t;
7ab9ffdd 2522
0dfbf9d7 2523 operand_type_set (&t, 0);
40fb9820
L
2524 t.bitfield.imm64 = 1;
2525
2526 if (cpu_arch_tune != PROCESSOR_I486 && num == 1)
e413e4e9
AM
2527 {
2528 /* This code is disabled on the 486 because all the Imm1 forms
2529 in the opcode table are slower on the i486. They're the
2530 versions with the implicitly specified single-position
2531 displacement, which has another syntax if you really want to
2532 use that form. */
40fb9820
L
2533 t.bitfield.imm1 = 1;
2534 t.bitfield.imm8 = 1;
2535 t.bitfield.imm8s = 1;
2536 t.bitfield.imm16 = 1;
2537 t.bitfield.imm32 = 1;
2538 t.bitfield.imm32s = 1;
2539 }
2540 else if (fits_in_signed_byte (num))
2541 {
c34d1cc9
JB
2542 if (fits_in_unsigned_byte (num))
2543 t.bitfield.imm8 = 1;
40fb9820
L
2544 t.bitfield.imm8s = 1;
2545 t.bitfield.imm16 = 1;
8170af78
HL
2546 if (flag_code != CODE_64BIT || fits_in_unsigned_long (num))
2547 t.bitfield.imm32 = 1;
40fb9820
L
2548 t.bitfield.imm32s = 1;
2549 }
2550 else if (fits_in_unsigned_byte (num))
2551 {
2552 t.bitfield.imm8 = 1;
2553 t.bitfield.imm16 = 1;
2554 t.bitfield.imm32 = 1;
2555 t.bitfield.imm32s = 1;
2556 }
2557 else if (fits_in_signed_word (num) || fits_in_unsigned_word (num))
2558 {
2559 t.bitfield.imm16 = 1;
8170af78
HL
2560 if (flag_code != CODE_64BIT || fits_in_unsigned_long (num))
2561 t.bitfield.imm32 = 1;
40fb9820
L
2562 t.bitfield.imm32s = 1;
2563 }
2564 else if (fits_in_signed_long (num))
2565 {
8170af78
HL
2566 if (flag_code != CODE_64BIT || fits_in_unsigned_long (num))
2567 t.bitfield.imm32 = 1;
40fb9820
L
2568 t.bitfield.imm32s = 1;
2569 }
2570 else if (fits_in_unsigned_long (num))
2571 t.bitfield.imm32 = 1;
2572
2573 return t;
47926f60 2574}
252b5132 2575
847f7ad4 2576static offsetT
e3bb37b5 2577offset_in_range (offsetT val, int size)
847f7ad4 2578{
508866be 2579 addressT mask;
ba2adb93 2580
847f7ad4
AM
2581 switch (size)
2582 {
508866be
L
2583 case 1: mask = ((addressT) 1 << 8) - 1; break;
2584 case 2: mask = ((addressT) 1 << 16) - 1; break;
3e73aa7c 2585#ifdef BFD64
64965897 2586 case 4: mask = ((addressT) 1 << 32) - 1; break;
3e73aa7c 2587#endif
64965897 2588 case sizeof (val): return val;
47926f60 2589 default: abort ();
847f7ad4
AM
2590 }
2591
4fe51f7d 2592 if ((val & ~mask) != 0 && (-val & ~mask) != 0)
f493c217
AM
2593 as_warn (_("0x%" PRIx64 " shortened to 0x%" PRIx64),
2594 (uint64_t) val, (uint64_t) (val & mask));
847f7ad4 2595
847f7ad4
AM
2596 return val & mask;
2597}
2598
76d3f746
JB
2599static INLINE const char *insn_name (const insn_template *t)
2600{
5c139202 2601 return &i386_mnemonics[t->mnem_off];
76d3f746
JB
2602}
2603
c32fa91d
L
2604enum PREFIX_GROUP
2605{
2606 PREFIX_EXIST = 0,
2607 PREFIX_LOCK,
2608 PREFIX_REP,
04ef582a 2609 PREFIX_DS,
c32fa91d
L
2610 PREFIX_OTHER
2611};
2612
2613/* Returns
2614 a. PREFIX_EXIST if attempting to add a prefix where one from the
2615 same class already exists.
2616 b. PREFIX_LOCK if lock prefix is added.
2617 c. PREFIX_REP if rep/repne prefix is added.
04ef582a
L
2618 d. PREFIX_DS if ds prefix is added.
2619 e. PREFIX_OTHER if other prefix is added.
c32fa91d
L
2620 */
2621
2622static enum PREFIX_GROUP
e3bb37b5 2623add_prefix (unsigned int prefix)
252b5132 2624{
c32fa91d 2625 enum PREFIX_GROUP ret = PREFIX_OTHER;
b1905489 2626 unsigned int q;
252b5132 2627
29b0f896
AM
2628 if (prefix >= REX_OPCODE && prefix < REX_OPCODE + 16
2629 && flag_code == CODE_64BIT)
b1905489 2630 {
161a04f6 2631 if ((i.prefix[REX_PREFIX] & prefix & REX_W)
44846f29
JB
2632 || (i.prefix[REX_PREFIX] & prefix & REX_R)
2633 || (i.prefix[REX_PREFIX] & prefix & REX_X)
2634 || (i.prefix[REX_PREFIX] & prefix & REX_B))
c32fa91d 2635 ret = PREFIX_EXIST;
b1905489
JB
2636 q = REX_PREFIX;
2637 }
3e73aa7c 2638 else
b1905489
JB
2639 {
2640 switch (prefix)
2641 {
2642 default:
2643 abort ();
2644
b1905489 2645 case DS_PREFIX_OPCODE:
04ef582a
L
2646 ret = PREFIX_DS;
2647 /* Fall through. */
2648 case CS_PREFIX_OPCODE:
b1905489
JB
2649 case ES_PREFIX_OPCODE:
2650 case FS_PREFIX_OPCODE:
2651 case GS_PREFIX_OPCODE:
2652 case SS_PREFIX_OPCODE:
2653 q = SEG_PREFIX;
2654 break;
2655
2656 case REPNE_PREFIX_OPCODE:
2657 case REPE_PREFIX_OPCODE:
c32fa91d
L
2658 q = REP_PREFIX;
2659 ret = PREFIX_REP;
2660 break;
2661
b1905489 2662 case LOCK_PREFIX_OPCODE:
c32fa91d
L
2663 q = LOCK_PREFIX;
2664 ret = PREFIX_LOCK;
b1905489
JB
2665 break;
2666
2667 case FWAIT_OPCODE:
2668 q = WAIT_PREFIX;
2669 break;
2670
2671 case ADDR_PREFIX_OPCODE:
2672 q = ADDR_PREFIX;
2673 break;
2674
2675 case DATA_PREFIX_OPCODE:
2676 q = DATA_PREFIX;
2677 break;
2678 }
2679 if (i.prefix[q] != 0)
c32fa91d 2680 ret = PREFIX_EXIST;
b1905489 2681 }
252b5132 2682
b1905489 2683 if (ret)
252b5132 2684 {
b1905489
JB
2685 if (!i.prefix[q])
2686 ++i.prefixes;
2687 i.prefix[q] |= prefix;
252b5132 2688 }
b1905489
JB
2689 else
2690 as_bad (_("same type of prefix used twice"));
252b5132 2691
252b5132
RH
2692 return ret;
2693}
2694
2695static void
78f12dd3 2696update_code_flag (int value, int check)
eecb386c 2697{
b44fef84
JB
2698 PRINTF_LIKE ((*as_error)) = check ? as_fatal : as_bad;
2699
da5f9eb4 2700 if (value == CODE_64BIT && !cpu_arch_flags.bitfield.cpu64 )
b44fef84
JB
2701 {
2702 as_error (_("64bit mode not supported on `%s'."),
2703 cpu_arch_name ? cpu_arch_name : default_arch);
2704 return;
2705 }
2706
2707 if (value == CODE_32BIT && !cpu_arch_flags.bitfield.cpui386)
2708 {
2709 as_error (_("32bit mode not supported on `%s'."),
2710 cpu_arch_name ? cpu_arch_name : default_arch);
2711 return;
2712 }
78f12dd3 2713
1e9cc1c2 2714 flag_code = (enum flag_code) value;
b44fef84 2715
eecb386c
AM
2716 stackop_size = '\0';
2717}
2718
78f12dd3
L
2719static void
2720set_code_flag (int value)
2721{
2722 update_code_flag (value, 0);
2723}
2724
eecb386c 2725static void
e3bb37b5 2726set_16bit_gcc_code_flag (int new_code_flag)
252b5132 2727{
1e9cc1c2 2728 flag_code = (enum flag_code) new_code_flag;
40fb9820
L
2729 if (flag_code != CODE_16BIT)
2730 abort ();
9306ca4a 2731 stackop_size = LONG_MNEM_SUFFIX;
252b5132
RH
2732}
2733
213f15cf
JB
2734static void
2735_set_intel_syntax (int syntax_flag)
2736{
2737 intel_syntax = syntax_flag;
2738
2739 expr_set_rank (O_full_ptr, syntax_flag ? 10 : 0);
2740
2741 register_prefix = allow_naked_reg ? "" : "%";
2742}
2743
252b5132 2744static void
e3bb37b5 2745set_intel_syntax (int syntax_flag)
252b5132
RH
2746{
2747 /* Find out if register prefixing is specified. */
2748 int ask_naked_reg = 0;
2749
2750 SKIP_WHITESPACE ();
29b0f896 2751 if (!is_end_of_line[(unsigned char) *input_line_pointer])
252b5132 2752 {
d02603dc
NC
2753 char *string;
2754 int e = get_symbol_name (&string);
252b5132 2755
47926f60 2756 if (strcmp (string, "prefix") == 0)
252b5132 2757 ask_naked_reg = 1;
47926f60 2758 else if (strcmp (string, "noprefix") == 0)
252b5132
RH
2759 ask_naked_reg = -1;
2760 else
d0b47220 2761 as_bad (_("bad argument to syntax directive."));
d02603dc 2762 (void) restore_line_pointer (e);
252b5132
RH
2763 }
2764 demand_empty_rest_of_line ();
c3332e24 2765
252b5132 2766 if (ask_naked_reg == 0)
213f15cf 2767 allow_naked_reg = (syntax_flag
f86103b7 2768 && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
252b5132
RH
2769 else
2770 allow_naked_reg = (ask_naked_reg < 0);
9306ca4a 2771
213f15cf 2772 _set_intel_syntax (syntax_flag);
252b5132
RH
2773}
2774
1efbbeb4
L
2775static void
2776set_intel_mnemonic (int mnemonic_flag)
2777{
e1d4d893 2778 intel_mnemonic = mnemonic_flag;
1efbbeb4
L
2779}
2780
db51cc60
L
2781static void
2782set_allow_index_reg (int flag)
2783{
2784 allow_index_reg = flag;
2785}
2786
cb19c032 2787static void
7bab8ab5 2788set_check (int what)
cb19c032 2789{
7bab8ab5
JB
2790 enum check_kind *kind;
2791 const char *str;
2792
2793 if (what)
2794 {
2795 kind = &operand_check;
2796 str = "operand";
2797 }
2798 else
2799 {
2800 kind = &sse_check;
2801 str = "sse";
2802 }
2803
cb19c032
L
2804 SKIP_WHITESPACE ();
2805
2806 if (!is_end_of_line[(unsigned char) *input_line_pointer])
2807 {
d02603dc
NC
2808 char *string;
2809 int e = get_symbol_name (&string);
cb19c032
L
2810
2811 if (strcmp (string, "none") == 0)
7bab8ab5 2812 *kind = check_none;
cb19c032 2813 else if (strcmp (string, "warning") == 0)
7bab8ab5 2814 *kind = check_warning;
cb19c032 2815 else if (strcmp (string, "error") == 0)
7bab8ab5 2816 *kind = check_error;
cb19c032 2817 else
7bab8ab5 2818 as_bad (_("bad argument to %s_check directive."), str);
d02603dc 2819 (void) restore_line_pointer (e);
cb19c032
L
2820 }
2821 else
7bab8ab5 2822 as_bad (_("missing argument for %s_check directive"), str);
cb19c032
L
2823
2824 demand_empty_rest_of_line ();
2825}
2826
8a9036a4
L
2827static void
2828check_cpu_arch_compatible (const char *name ATTRIBUTE_UNUSED,
1e9cc1c2 2829 i386_cpu_flags new_flag ATTRIBUTE_UNUSED)
8a9036a4
L
2830{
2831#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2832 static const char *arch;
2833
c085ab00 2834 /* Intel MCU is only supported on ELF. */
8a9036a4
L
2835 if (!IS_ELF)
2836 return;
2837
2838 if (!arch)
2839 {
2840 /* Use cpu_arch_name if it is set in md_parse_option. Otherwise
2841 use default_arch. */
2842 arch = cpu_arch_name;
2843 if (!arch)
2844 arch = default_arch;
2845 }
2846
81486035 2847 /* If we are targeting Intel MCU, we must enable it. */
648d04db
JB
2848 if ((get_elf_backend_data (stdoutput)->elf_machine_code == EM_IAMCU)
2849 == new_flag.bitfield.cpuiamcu)
81486035
L
2850 return;
2851
8a9036a4
L
2852 as_bad (_("`%s' is not supported on `%s'"), name, arch);
2853#endif
2854}
2855
8180707f 2856static void
bd483d21 2857extend_cpu_sub_arch_name (const char *pfx, const char *name)
8180707f
JB
2858{
2859 if (cpu_sub_arch_name)
2860 cpu_sub_arch_name = reconcat (cpu_sub_arch_name, cpu_sub_arch_name,
bd483d21 2861 pfx, name, (const char *) NULL);
8180707f 2862 else
bd483d21
JB
2863 cpu_sub_arch_name = concat (pfx, name, (const char *) NULL);
2864}
2865
2866static void isa_enable (unsigned int idx)
2867{
2868 i386_cpu_flags flags = cpu_flags_or (cpu_arch_flags, cpu_arch[idx].enable);
2869
2870 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
2871 {
2872 extend_cpu_sub_arch_name (".", cpu_arch[idx].name);
2873 cpu_arch_flags = flags;
2874 }
2875
2876 cpu_arch_isa_flags = cpu_flags_or (cpu_arch_isa_flags, cpu_arch[idx].enable);
2877}
2878
2879static void isa_disable (unsigned int idx)
2880{
2881 i386_cpu_flags flags
2882 = cpu_flags_and_not (cpu_arch_flags, cpu_arch[idx].disable);
2883
2884 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
2885 {
2886 extend_cpu_sub_arch_name (".no", cpu_arch[idx].name);
2887 cpu_arch_flags = flags;
2888 }
2889
2890 cpu_arch_isa_flags
2891 = cpu_flags_and_not (cpu_arch_isa_flags, cpu_arch[idx].disable);
8180707f
JB
2892}
2893
e413e4e9 2894static void
e3bb37b5 2895set_cpu_arch (int dummy ATTRIBUTE_UNUSED)
e413e4e9 2896{
f68697e8
JB
2897 typedef struct arch_stack_entry
2898 {
2899 const struct arch_stack_entry *prev;
2900 const char *name;
2901 char *sub_name;
2902 i386_cpu_flags flags;
2903 i386_cpu_flags isa_flags;
2904 enum processor_type isa;
2905 enum flag_code flag_code;
4fc85f37 2906 unsigned int vector_size;
f68697e8
JB
2907 char stackop_size;
2908 bool no_cond_jump_promotion;
2909 } arch_stack_entry;
2910 static const arch_stack_entry *arch_stack_top;
dfab07b9
JB
2911 char *s;
2912 int e;
2913 const char *string;
2914 unsigned int j = 0;
f68697e8 2915
47926f60 2916 SKIP_WHITESPACE ();
e413e4e9 2917
dfab07b9
JB
2918 if (is_end_of_line[(unsigned char) *input_line_pointer])
2919 {
2920 as_bad (_("missing cpu architecture"));
2921 input_line_pointer++;
2922 return;
2923 }
2924
2925 e = get_symbol_name (&s);
2926 string = s;
2927
2928 if (strcmp (string, "push") == 0)
e413e4e9 2929 {
dfab07b9 2930 arch_stack_entry *top = XNEW (arch_stack_entry);
e413e4e9 2931
dfab07b9
JB
2932 top->name = cpu_arch_name;
2933 if (cpu_sub_arch_name)
2934 top->sub_name = xstrdup (cpu_sub_arch_name);
2935 else
2936 top->sub_name = NULL;
2937 top->flags = cpu_arch_flags;
2938 top->isa = cpu_arch_isa;
2939 top->isa_flags = cpu_arch_isa_flags;
2940 top->flag_code = flag_code;
4fc85f37 2941 top->vector_size = vector_size;
dfab07b9
JB
2942 top->stackop_size = stackop_size;
2943 top->no_cond_jump_promotion = no_cond_jump_promotion;
2944
2945 top->prev = arch_stack_top;
2946 arch_stack_top = top;
2947
2948 (void) restore_line_pointer (e);
2949 demand_empty_rest_of_line ();
2950 return;
2951 }
2952
2953 if (strcmp (string, "pop") == 0)
2954 {
2955 const arch_stack_entry *top = arch_stack_top;
2956
2957 if (!top)
2958 as_bad (_(".arch stack is empty"));
2959 else if (top->flag_code != flag_code
2960 || top->stackop_size != stackop_size)
2961 {
2962 static const unsigned int bits[] = {
2963 [CODE_16BIT] = 16,
2964 [CODE_32BIT] = 32,
2965 [CODE_64BIT] = 64,
2966 };
2967
2968 as_bad (_("this `.arch pop' requires `.code%u%s' to be in effect"),
2969 bits[top->flag_code],
2970 top->stackop_size == LONG_MNEM_SUFFIX ? "gcc" : "");
2971 }
2972 else
3ce2ebcf 2973 {
dfab07b9
JB
2974 arch_stack_top = top->prev;
2975
2976 cpu_arch_name = top->name;
2977 free (cpu_sub_arch_name);
2978 cpu_sub_arch_name = top->sub_name;
2979 cpu_arch_flags = top->flags;
2980 cpu_arch_isa = top->isa;
2981 cpu_arch_isa_flags = top->isa_flags;
4fc85f37 2982 vector_size = top->vector_size;
dfab07b9
JB
2983 no_cond_jump_promotion = top->no_cond_jump_promotion;
2984
2985 XDELETE (top);
2986 }
2987
2988 (void) restore_line_pointer (e);
2989 demand_empty_rest_of_line ();
2990 return;
2991 }
2992
2993 if (strcmp (string, "default") == 0)
2994 {
2995 if (strcmp (default_arch, "iamcu") == 0)
2996 string = default_arch;
2997 else
2998 {
2999 static const i386_cpu_flags cpu_unknown_flags = CPU_UNKNOWN_FLAGS;
3000
3001 cpu_arch_name = NULL;
3002 free (cpu_sub_arch_name);
3003 cpu_sub_arch_name = NULL;
3004 cpu_arch_flags = cpu_unknown_flags;
dfab07b9
JB
3005 cpu_arch_isa = PROCESSOR_UNKNOWN;
3006 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
3007 if (!cpu_arch_tune_set)
fb263707 3008 cpu_arch_tune = PROCESSOR_UNKNOWN;
dfab07b9 3009
4fc85f37
JB
3010 vector_size = VSZ_DEFAULT;
3011
dfab07b9
JB
3012 j = ARRAY_SIZE (cpu_arch) + 1;
3013 }
3014 }
3015
3016 for (; j < ARRAY_SIZE (cpu_arch); j++)
3017 {
3018 if (strcmp (string + (*string == '.'), cpu_arch[j].name) == 0
3019 && (*string == '.') == (cpu_arch[j].type == PROCESSOR_NONE))
3020 {
3021 if (*string != '.')
3022 {
3023 check_cpu_arch_compatible (string, cpu_arch[j].enable);
3ce2ebcf 3024
da5f9eb4 3025 if (flag_code == CODE_64BIT && !cpu_arch[j].enable.bitfield.cpu64 )
1d07cfb4
JB
3026 {
3027 as_bad (_("64bit mode not supported on `%s'."),
3028 cpu_arch[j].name);
3029 (void) restore_line_pointer (e);
3030 ignore_rest_of_line ();
3031 return;
3032 }
3033
3034 if (flag_code == CODE_32BIT && !cpu_arch[j].enable.bitfield.cpui386)
3035 {
3036 as_bad (_("32bit mode not supported on `%s'."),
3037 cpu_arch[j].name);
3038 (void) restore_line_pointer (e);
3039 ignore_rest_of_line ();
3040 return;
3041 }
3042
dfab07b9 3043 cpu_arch_name = cpu_arch[j].name;
3ce2ebcf
JB
3044 free (cpu_sub_arch_name);
3045 cpu_sub_arch_name = NULL;
dfab07b9 3046 cpu_arch_flags = cpu_arch[j].enable;
dfab07b9
JB
3047 cpu_arch_isa = cpu_arch[j].type;
3048 cpu_arch_isa_flags = cpu_arch[j].enable;
3ce2ebcf 3049 if (!cpu_arch_tune_set)
fb263707 3050 cpu_arch_tune = cpu_arch_isa;
4fc85f37
JB
3051
3052 vector_size = VSZ_DEFAULT;
3053
dfab07b9
JB
3054 pre_386_16bit_warned = false;
3055 break;
3ce2ebcf 3056 }
f68697e8 3057
dfab07b9
JB
3058 if (cpu_flags_all_zero (&cpu_arch[j].enable))
3059 continue;
f68697e8 3060
bd483d21 3061 isa_enable (j);
f68697e8
JB
3062
3063 (void) restore_line_pointer (e);
4fc85f37
JB
3064
3065 switch (cpu_arch[j].vsz)
3066 {
3067 default:
3068 break;
3069
3070 case vsz_set:
3071#ifdef SVR4_COMMENT_CHARS
3072 if (*input_line_pointer == ':' || *input_line_pointer == '/')
3073#else
3074 if (*input_line_pointer == '/')
3075#endif
3076 {
3077 ++input_line_pointer;
3078 switch (get_absolute_expression ())
3079 {
3080 case 512: vector_size = VSZ512; break;
3081 case 256: vector_size = VSZ256; break;
3082 case 128: vector_size = VSZ128; break;
3083 default:
3084 as_bad (_("Unrecognized vector size specifier"));
3085 ignore_rest_of_line ();
3086 return;
3087 }
3088 break;
3089 }
3090 /* Fall through. */
3091 case vsz_reset:
3092 vector_size = VSZ_DEFAULT;
3093 break;
3094 }
3095
f68697e8
JB
3096 demand_empty_rest_of_line ();
3097 return;
3098 }
dfab07b9 3099 }
3ce2ebcf 3100
dfab07b9
JB
3101 if (startswith (string, ".no") && j >= ARRAY_SIZE (cpu_arch))
3102 {
3103 /* Disable an ISA extension. */
3104 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
3105 if (cpu_arch[j].type == PROCESSOR_NONE
3106 && strcmp (string + 3, cpu_arch[j].name) == 0)
3107 {
bd483d21 3108 isa_disable (j);
e413e4e9 3109
4fc85f37
JB
3110 if (cpu_arch[j].vsz == vsz_set)
3111 vector_size = VSZ_DEFAULT;
3112
dfab07b9
JB
3113 (void) restore_line_pointer (e);
3114 demand_empty_rest_of_line ();
3115 return;
3116 }
e413e4e9 3117 }
dfab07b9
JB
3118
3119 if (j == ARRAY_SIZE (cpu_arch))
3120 as_bad (_("no such architecture: `%s'"), string);
3121
3122 *input_line_pointer = e;
e413e4e9 3123
fddf5b5b
AM
3124 no_cond_jump_promotion = 0;
3125 if (*input_line_pointer == ','
29b0f896 3126 && !is_end_of_line[(unsigned char) input_line_pointer[1]])
fddf5b5b 3127 {
d02603dc 3128 ++input_line_pointer;
dfab07b9
JB
3129 e = get_symbol_name (&s);
3130 string = s;
fddf5b5b
AM
3131
3132 if (strcmp (string, "nojumps") == 0)
3133 no_cond_jump_promotion = 1;
3134 else if (strcmp (string, "jumps") == 0)
3135 ;
3136 else
3137 as_bad (_("no such architecture modifier: `%s'"), string);
3138
d02603dc 3139 (void) restore_line_pointer (e);
fddf5b5b
AM
3140 }
3141
e413e4e9
AM
3142 demand_empty_rest_of_line ();
3143}
3144
8a9036a4
L
3145enum bfd_architecture
3146i386_arch (void)
3147{
c085ab00 3148 if (cpu_arch_isa == PROCESSOR_IAMCU)
81486035 3149 {
ed049bd6 3150 if (!IS_ELF || flag_code == CODE_64BIT)
81486035
L
3151 as_fatal (_("Intel MCU is 32bit ELF only"));
3152 return bfd_arch_iamcu;
3153 }
8a9036a4
L
3154 else
3155 return bfd_arch_i386;
3156}
3157
b9d79e03 3158unsigned long
7016a5d5 3159i386_mach (void)
b9d79e03 3160{
d34049e8 3161 if (startswith (default_arch, "x86_64"))
8a9036a4 3162 {
c085ab00 3163 if (default_arch[6] == '\0')
8a9036a4 3164 return bfd_mach_x86_64;
351f65ca
L
3165 else
3166 return bfd_mach_x64_32;
8a9036a4 3167 }
5197d474
L
3168 else if (!strcmp (default_arch, "i386")
3169 || !strcmp (default_arch, "iamcu"))
81486035
L
3170 {
3171 if (cpu_arch_isa == PROCESSOR_IAMCU)
3172 {
ed049bd6 3173 if (!IS_ELF)
81486035
L
3174 as_fatal (_("Intel MCU is 32bit ELF only"));
3175 return bfd_mach_i386_iamcu;
3176 }
3177 else
3178 return bfd_mach_i386_i386;
3179 }
b9d79e03 3180 else
2b5d6a91 3181 as_fatal (_("unknown architecture"));
b9d79e03 3182}
b9d79e03 3183\f
99f0fb12
JB
3184#include "opcodes/i386-tbl.h"
3185
d3b01414
JB
3186static void
3187op_lookup (const char *mnemonic)
3188{
3189 i386_op_off_t *pos = str_hash_find (op_hash, mnemonic);
3190
3191 if (pos != NULL)
3192 {
3193 current_templates.start = &i386_optab[pos[0]];
3194 current_templates.end = &i386_optab[pos[1]];
3195 }
3196 else
3197 current_templates.end = current_templates.start = NULL;
3198}
3199
252b5132 3200void
7016a5d5 3201md_begin (void)
252b5132 3202{
86fa6981
L
3203 /* Support pseudo prefixes like {disp32}. */
3204 lex_type ['{'] = LEX_BEGIN_NAME;
3205
47926f60 3206 /* Initialize op_hash hash table. */
629310ab 3207 op_hash = str_htab_create ();
252b5132
RH
3208
3209 {
d3b01414
JB
3210 const i386_op_off_t *cur = i386_op_sets;
3211 const i386_op_off_t *end = cur + ARRAY_SIZE (i386_op_sets) - 1;
3212
3213 for (; cur < end; ++cur)
3214 if (str_hash_insert (op_hash, insn_name (&i386_optab[*cur]), cur, 0))
3215 as_fatal (_("duplicate %s"), insn_name (&i386_optab[*cur]));
252b5132
RH
3216 }
3217
47926f60 3218 /* Initialize reg_hash hash table. */
629310ab 3219 reg_hash = str_htab_create ();
252b5132 3220 {
29b0f896 3221 const reg_entry *regtab;
c3fe08fa 3222 unsigned int regtab_size = i386_regtab_size;
252b5132 3223
c3fe08fa 3224 for (regtab = i386_regtab; regtab_size--; regtab++)
6225c532 3225 {
6288d05f
JB
3226 switch (regtab->reg_type.bitfield.class)
3227 {
3228 case Reg:
34684862
JB
3229 if (regtab->reg_type.bitfield.dword)
3230 {
3231 if (regtab->reg_type.bitfield.instance == Accum)
3232 reg_eax = regtab;
3233 }
3234 else if (regtab->reg_type.bitfield.tbyte)
6288d05f
JB
3235 {
3236 /* There's no point inserting st(<N>) in the hash table, as
3237 parentheses aren't included in register_chars[] anyway. */
3238 if (regtab->reg_type.bitfield.instance != Accum)
3239 continue;
3240 reg_st0 = regtab;
3241 }
3242 break;
3243
5e042380
JB
3244 case SReg:
3245 switch (regtab->reg_num)
3246 {
3247 case 0: reg_es = regtab; break;
3248 case 2: reg_ss = regtab; break;
3249 case 3: reg_ds = regtab; break;
3250 }
3251 break;
3252
6288d05f
JB
3253 case RegMask:
3254 if (!regtab->reg_num)
3255 reg_k0 = regtab;
3256 break;
3257 }
3258
6225c532
JB
3259 if (str_hash_insert (reg_hash, regtab->reg_name, regtab, 0) != NULL)
3260 as_fatal (_("duplicate %s"), regtab->reg_name);
6225c532 3261 }
252b5132
RH
3262 }
3263
47926f60 3264 /* Fill in lexical tables: mnemonic_chars, operand_chars. */
252b5132 3265 {
29b0f896 3266 int c;
d2b1a14d 3267 const char *p;
252b5132
RH
3268
3269 for (c = 0; c < 256; c++)
3270 {
014fbcda 3271 if (ISDIGIT (c) || ISLOWER (c))
252b5132
RH
3272 {
3273 mnemonic_chars[c] = c;
3274 register_chars[c] = c;
3275 operand_chars[c] = c;
3276 }
3882b010 3277 else if (ISUPPER (c))
252b5132 3278 {
3882b010 3279 mnemonic_chars[c] = TOLOWER (c);
252b5132
RH
3280 register_chars[c] = mnemonic_chars[c];
3281 operand_chars[c] = c;
3282 }
b3983e5f
JB
3283#ifdef SVR4_COMMENT_CHARS
3284 else if (c == '\\' && strchr (i386_comment_chars, '/'))
3285 operand_chars[c] = c;
3286#endif
252b5132 3287
4795cd4a
JB
3288 if (c >= 128)
3289 operand_chars[c] = c;
252b5132
RH
3290 }
3291
c0f3af97 3292 mnemonic_chars['_'] = '_';
791fe849 3293 mnemonic_chars['-'] = '-';
0003779b 3294 mnemonic_chars['.'] = '.';
252b5132 3295
d2b1a14d
JB
3296 for (p = extra_symbol_chars; *p != '\0'; p++)
3297 operand_chars[(unsigned char) *p] = *p;
252b5132
RH
3298 for (p = operand_special_chars; *p != '\0'; p++)
3299 operand_chars[(unsigned char) *p] = *p;
3300 }
3301
fabb73d1 3302 if (object_64bit)
a4447b93 3303 {
ca19b261
KT
3304#if defined (OBJ_COFF) && defined (TE_PE)
3305 x86_dwarf2_return_column = (OUTPUT_FLAVOR == bfd_target_coff_flavour
3306 ? 32 : 16);
3307#else
a4447b93 3308 x86_dwarf2_return_column = 16;
ca19b261 3309#endif
61ff971f 3310 x86_cie_data_alignment = -8;
b52c4ee4 3311#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
788b11d9
JB
3312 x86_sframe_cfa_sp_reg = REG_SP;
3313 x86_sframe_cfa_fp_reg = REG_FP;
b52c4ee4 3314#endif
a4447b93
RH
3315 }
3316 else
3317 {
3318 x86_dwarf2_return_column = 8;
3319 x86_cie_data_alignment = -4;
3320 }
e379e5f3
L
3321
3322 /* NB: FUSED_JCC_PADDING frag must have sufficient room so that it
3323 can be turned into BRANCH_PREFIX frag. */
3324 if (align_branch_prefix_size > MAX_FUSED_JCC_PADDING_SIZE)
3325 abort ();
252b5132
RH
3326}
3327
3328void
e3bb37b5 3329i386_print_statistics (FILE *file)
252b5132 3330{
629310ab
ML
3331 htab_print_statistics (file, "i386 opcode", op_hash);
3332 htab_print_statistics (file, "i386 register", reg_hash);
252b5132 3333}
654d6f31
AM
3334
3335void
3336i386_md_end (void)
3337{
3338 htab_delete (op_hash);
3339 htab_delete (reg_hash);
3340}
252b5132 3341\f
252b5132
RH
3342#ifdef DEBUG386
3343
ce8a8b2f 3344/* Debugging routines for md_assemble. */
d3ce72d0 3345static void pte (insn_template *);
40fb9820 3346static void pt (i386_operand_type);
e3bb37b5
L
3347static void pe (expressionS *);
3348static void ps (symbolS *);
252b5132
RH
3349
3350static void
2c703856 3351pi (const char *line, i386_insn *x)
252b5132 3352{
09137c09 3353 unsigned int j;
252b5132
RH
3354
3355 fprintf (stdout, "%s: template ", line);
3356 pte (&x->tm);
09f131f2
JH
3357 fprintf (stdout, " address: base %s index %s scale %x\n",
3358 x->base_reg ? x->base_reg->reg_name : "none",
3359 x->index_reg ? x->index_reg->reg_name : "none",
3360 x->log2_scale_factor);
3361 fprintf (stdout, " modrm: mode %x reg %x reg/mem %x\n",
252b5132 3362 x->rm.mode, x->rm.reg, x->rm.regmem);
09f131f2
JH
3363 fprintf (stdout, " sib: base %x index %x scale %x\n",
3364 x->sib.base, x->sib.index, x->sib.scale);
3365 fprintf (stdout, " rex: 64bit %x extX %x extY %x extZ %x\n",
161a04f6
L
3366 (x->rex & REX_W) != 0,
3367 (x->rex & REX_R) != 0,
3368 (x->rex & REX_X) != 0,
3369 (x->rex & REX_B) != 0);
09137c09 3370 for (j = 0; j < x->operands; j++)
252b5132 3371 {
09137c09
SP
3372 fprintf (stdout, " #%d: ", j + 1);
3373 pt (x->types[j]);
252b5132 3374 fprintf (stdout, "\n");
bab6aec1 3375 if (x->types[j].bitfield.class == Reg
3528c362
JB
3376 || x->types[j].bitfield.class == RegMMX
3377 || x->types[j].bitfield.class == RegSIMD
dd6b8a0b 3378 || x->types[j].bitfield.class == RegMask
00cee14f 3379 || x->types[j].bitfield.class == SReg
4a5c67ed
JB
3380 || x->types[j].bitfield.class == RegCR
3381 || x->types[j].bitfield.class == RegDR
dd6b8a0b
JB
3382 || x->types[j].bitfield.class == RegTR
3383 || x->types[j].bitfield.class == RegBND)
09137c09
SP
3384 fprintf (stdout, "%s\n", x->op[j].regs->reg_name);
3385 if (operand_type_check (x->types[j], imm))
3386 pe (x->op[j].imms);
3387 if (operand_type_check (x->types[j], disp))
3388 pe (x->op[j].disps);
252b5132
RH
3389 }
3390}
3391
3392static void
d3ce72d0 3393pte (insn_template *t)
252b5132 3394{
b933fa4b 3395 static const unsigned char opc_pfx[] = { 0, 0x66, 0xf3, 0xf2 };
441f6aca 3396 static const char *const opc_spc[] = {
0cc78721 3397 NULL, "0f", "0f38", "0f3a", NULL, "evexmap5", "evexmap6", NULL,
441f6aca
JB
3398 "XOP08", "XOP09", "XOP0A",
3399 };
09137c09 3400 unsigned int j;
441f6aca 3401
252b5132 3402 fprintf (stdout, " %d operands ", t->operands);
441f6aca
JB
3403 if (opc_pfx[t->opcode_modifier.opcodeprefix])
3404 fprintf (stdout, "pfx %x ", opc_pfx[t->opcode_modifier.opcodeprefix]);
ddb62495
JB
3405 if (opc_spc[t->opcode_space])
3406 fprintf (stdout, "space %s ", opc_spc[t->opcode_space]);
47926f60 3407 fprintf (stdout, "opcode %x ", t->base_opcode);
252b5132
RH
3408 if (t->extension_opcode != None)
3409 fprintf (stdout, "ext %x ", t->extension_opcode);
40fb9820 3410 if (t->opcode_modifier.d)
252b5132 3411 fprintf (stdout, "D");
40fb9820 3412 if (t->opcode_modifier.w)
252b5132
RH
3413 fprintf (stdout, "W");
3414 fprintf (stdout, "\n");
09137c09 3415 for (j = 0; j < t->operands; j++)
252b5132 3416 {
09137c09
SP
3417 fprintf (stdout, " #%d type ", j + 1);
3418 pt (t->operand_types[j]);
252b5132
RH
3419 fprintf (stdout, "\n");
3420 }
3421}
3422
3423static void
e3bb37b5 3424pe (expressionS *e)
252b5132 3425{
24eab124 3426 fprintf (stdout, " operation %d\n", e->X_op);
b8281767
AM
3427 fprintf (stdout, " add_number %" PRId64 " (%" PRIx64 ")\n",
3428 (int64_t) e->X_add_number, (uint64_t) (valueT) e->X_add_number);
252b5132
RH
3429 if (e->X_add_symbol)
3430 {
3431 fprintf (stdout, " add_symbol ");
3432 ps (e->X_add_symbol);
3433 fprintf (stdout, "\n");
3434 }
3435 if (e->X_op_symbol)
3436 {
3437 fprintf (stdout, " op_symbol ");
3438 ps (e->X_op_symbol);
3439 fprintf (stdout, "\n");
3440 }
3441}
3442
3443static void
e3bb37b5 3444ps (symbolS *s)
252b5132
RH
3445{
3446 fprintf (stdout, "%s type %s%s",
3447 S_GET_NAME (s),
3448 S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
3449 segment_name (S_GET_SEGMENT (s)));
3450}
3451
7b81dfbb 3452static struct type_name
252b5132 3453 {
40fb9820
L
3454 i386_operand_type mask;
3455 const char *name;
252b5132 3456 }
7b81dfbb 3457const type_names[] =
252b5132 3458{
05909f23
JB
3459 { { .bitfield = { .class = Reg, .byte = 1 } }, "r8" },
3460 { { .bitfield = { .class = Reg, .word = 1 } }, "r16" },
3461 { { .bitfield = { .class = Reg, .dword = 1 } }, "r32" },
3462 { { .bitfield = { .class = Reg, .qword = 1 } }, "r64" },
3463 { { .bitfield = { .instance = Accum, .byte = 1 } }, "acc8" },
3464 { { .bitfield = { .instance = Accum, .word = 1 } }, "acc16" },
3465 { { .bitfield = { .instance = Accum, .dword = 1 } }, "acc32" },
3466 { { .bitfield = { .instance = Accum, .qword = 1 } }, "acc64" },
3467 { { .bitfield = { .imm8 = 1 } }, "i8" },
3468 { { .bitfield = { .imm8s = 1 } }, "i8s" },
3469 { { .bitfield = { .imm16 = 1 } }, "i16" },
3470 { { .bitfield = { .imm32 = 1 } }, "i32" },
3471 { { .bitfield = { .imm32s = 1 } }, "i32s" },
3472 { { .bitfield = { .imm64 = 1 } }, "i64" },
3473 { { .bitfield = { .imm1 = 1 } }, "i1" },
3474 { { .bitfield = { .baseindex = 1 } }, "BaseIndex" },
3475 { { .bitfield = { .disp8 = 1 } }, "d8" },
3476 { { .bitfield = { .disp16 = 1 } }, "d16" },
3477 { { .bitfield = { .disp32 = 1 } }, "d32" },
3478 { { .bitfield = { .disp64 = 1 } }, "d64" },
3479 { { .bitfield = { .instance = RegD, .word = 1 } }, "InOutPortReg" },
3480 { { .bitfield = { .instance = RegC, .byte = 1 } }, "ShiftCount" },
3481 { { .bitfield = { .class = RegCR } }, "control reg" },
3482 { { .bitfield = { .class = RegTR } }, "test reg" },
3483 { { .bitfield = { .class = RegDR } }, "debug reg" },
3484 { { .bitfield = { .class = Reg, .tbyte = 1 } }, "FReg" },
3485 { { .bitfield = { .instance = Accum, .tbyte = 1 } }, "FAcc" },
3486 { { .bitfield = { .class = SReg } }, "SReg" },
3487 { { .bitfield = { .class = RegMMX } }, "rMMX" },
3488 { { .bitfield = { .class = RegSIMD, .xmmword = 1 } }, "rXMM" },
3489 { { .bitfield = { .class = RegSIMD, .ymmword = 1 } }, "rYMM" },
3490 { { .bitfield = { .class = RegSIMD, .zmmword = 1 } }, "rZMM" },
3491 { { .bitfield = { .class = RegSIMD, .tmmword = 1 } }, "rTMM" },
3492 { { .bitfield = { .class = RegMask } }, "Mask reg" },
252b5132
RH
3493};
3494
3495static void
40fb9820 3496pt (i386_operand_type t)
252b5132 3497{
40fb9820 3498 unsigned int j;
c6fb90c8 3499 i386_operand_type a;
252b5132 3500
40fb9820 3501 for (j = 0; j < ARRAY_SIZE (type_names); j++)
c6fb90c8
L
3502 {
3503 a = operand_type_and (t, type_names[j].mask);
2c703856 3504 if (operand_type_equal (&a, &type_names[j].mask))
c6fb90c8
L
3505 fprintf (stdout, "%s, ", type_names[j].name);
3506 }
252b5132
RH
3507 fflush (stdout);
3508}
3509
3510#endif /* DEBUG386 */
3511\f
252b5132 3512static bfd_reloc_code_real_type
3956db08 3513reloc (unsigned int size,
64e74474
AM
3514 int pcrel,
3515 int sign,
3516 bfd_reloc_code_real_type other)
252b5132 3517{
47926f60 3518 if (other != NO_RELOC)
3956db08 3519 {
91d6fa6a 3520 reloc_howto_type *rel;
3956db08
JB
3521
3522 if (size == 8)
3523 switch (other)
3524 {
64e74474
AM
3525 case BFD_RELOC_X86_64_GOT32:
3526 return BFD_RELOC_X86_64_GOT64;
3527 break;
553d1284
L
3528 case BFD_RELOC_X86_64_GOTPLT64:
3529 return BFD_RELOC_X86_64_GOTPLT64;
3530 break;
64e74474
AM
3531 case BFD_RELOC_X86_64_PLTOFF64:
3532 return BFD_RELOC_X86_64_PLTOFF64;
3533 break;
3534 case BFD_RELOC_X86_64_GOTPC32:
3535 other = BFD_RELOC_X86_64_GOTPC64;
3536 break;
3537 case BFD_RELOC_X86_64_GOTPCREL:
3538 other = BFD_RELOC_X86_64_GOTPCREL64;
3539 break;
3540 case BFD_RELOC_X86_64_TPOFF32:
3541 other = BFD_RELOC_X86_64_TPOFF64;
3542 break;
3543 case BFD_RELOC_X86_64_DTPOFF32:
3544 other = BFD_RELOC_X86_64_DTPOFF64;
3545 break;
3546 default:
3547 break;
3956db08 3548 }
e05278af 3549
8ce3d284 3550#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
3551 if (other == BFD_RELOC_SIZE32)
3552 {
3553 if (size == 8)
1ab668bf 3554 other = BFD_RELOC_SIZE64;
8fd4256d 3555 if (pcrel)
1ab668bf
AM
3556 {
3557 as_bad (_("there are no pc-relative size relocations"));
3558 return NO_RELOC;
3559 }
8fd4256d 3560 }
8ce3d284 3561#endif
8fd4256d 3562
e05278af 3563 /* Sign-checking 4-byte relocations in 16-/32-bit code is pointless. */
f2d8a97c 3564 if (size == 4 && (flag_code != CODE_64BIT || disallow_64bit_reloc))
e05278af
JB
3565 sign = -1;
3566
91d6fa6a
NC
3567 rel = bfd_reloc_type_lookup (stdoutput, other);
3568 if (!rel)
3956db08 3569 as_bad (_("unknown relocation (%u)"), other);
91d6fa6a 3570 else if (size != bfd_get_reloc_size (rel))
3956db08 3571 as_bad (_("%u-byte relocation cannot be applied to %u-byte field"),
91d6fa6a 3572 bfd_get_reloc_size (rel),
3956db08 3573 size);
91d6fa6a 3574 else if (pcrel && !rel->pc_relative)
3956db08 3575 as_bad (_("non-pc-relative relocation for pc-relative field"));
91d6fa6a 3576 else if ((rel->complain_on_overflow == complain_overflow_signed
3956db08 3577 && !sign)
91d6fa6a 3578 || (rel->complain_on_overflow == complain_overflow_unsigned
64e74474 3579 && sign > 0))
3956db08
JB
3580 as_bad (_("relocated field and relocation type differ in signedness"));
3581 else
3582 return other;
3583 return NO_RELOC;
3584 }
252b5132
RH
3585
3586 if (pcrel)
3587 {
3e73aa7c 3588 if (!sign)
3956db08 3589 as_bad (_("there are no unsigned pc-relative relocations"));
252b5132
RH
3590 switch (size)
3591 {
3592 case 1: return BFD_RELOC_8_PCREL;
3593 case 2: return BFD_RELOC_16_PCREL;
d258b828 3594 case 4: return BFD_RELOC_32_PCREL;
d6ab8113 3595 case 8: return BFD_RELOC_64_PCREL;
252b5132 3596 }
3956db08 3597 as_bad (_("cannot do %u byte pc-relative relocation"), size);
252b5132
RH
3598 }
3599 else
3600 {
3956db08 3601 if (sign > 0)
e5cb08ac 3602 switch (size)
3e73aa7c
JH
3603 {
3604 case 4: return BFD_RELOC_X86_64_32S;
3605 }
3606 else
3607 switch (size)
3608 {
3609 case 1: return BFD_RELOC_8;
3610 case 2: return BFD_RELOC_16;
3611 case 4: return BFD_RELOC_32;
3612 case 8: return BFD_RELOC_64;
3613 }
3956db08
JB
3614 as_bad (_("cannot do %s %u byte relocation"),
3615 sign > 0 ? "signed" : "unsigned", size);
252b5132
RH
3616 }
3617
0cc9e1d3 3618 return NO_RELOC;
252b5132
RH
3619}
3620
071c5d81 3621#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
47926f60
KH
3622/* Here we decide which fixups can be adjusted to make them relative to
3623 the beginning of the section instead of the symbol. Basically we need
3624 to make sure that the dynamic relocations are done correctly, so in
3625 some cases we force the original symbol to be used. */
3626
252b5132 3627int
071c5d81 3628tc_i386_fix_adjustable (fixS *fixP)
252b5132 3629{
718ddfc0 3630 if (!IS_ELF)
31312f95
AM
3631 return 1;
3632
a161fe53
AM
3633 /* Don't adjust pc-relative references to merge sections in 64-bit
3634 mode. */
3635 if (use_rela_relocations
3636 && (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0
3637 && fixP->fx_pcrel)
252b5132 3638 return 0;
31312f95 3639
8d01d9a9
AJ
3640 /* The x86_64 GOTPCREL are represented as 32bit PCrel relocations
3641 and changed later by validate_fix. */
3642 if (GOT_symbol && fixP->fx_subsy == GOT_symbol
3643 && fixP->fx_r_type == BFD_RELOC_32_PCREL)
3644 return 0;
3645
8fd4256d
L
3646 /* Adjust_reloc_syms doesn't know about the GOT. Need to keep symbol
3647 for size relocations. */
3648 if (fixP->fx_r_type == BFD_RELOC_SIZE32
3649 || fixP->fx_r_type == BFD_RELOC_SIZE64
3650 || fixP->fx_r_type == BFD_RELOC_386_GOTOFF
252b5132 3651 || fixP->fx_r_type == BFD_RELOC_386_GOT32
02a86693 3652 || fixP->fx_r_type == BFD_RELOC_386_GOT32X
13ae64f3
JJ
3653 || fixP->fx_r_type == BFD_RELOC_386_TLS_GD
3654 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDM
3655 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDO_32
3656 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE_32
37e55690
JJ
3657 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE
3658 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTIE
13ae64f3
JJ
3659 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE_32
3660 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE
67a4f2b7
AO
3661 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTDESC
3662 || fixP->fx_r_type == BFD_RELOC_386_TLS_DESC_CALL
3e73aa7c 3663 || fixP->fx_r_type == BFD_RELOC_X86_64_GOT32
80b3ee89 3664 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCREL
56ceb5b5
L
3665 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCRELX
3666 || fixP->fx_r_type == BFD_RELOC_X86_64_REX_GOTPCRELX
3d5a60de 3667 || fixP->fx_r_type == BFD_RELOC_X86_64_CODE_4_GOTPCRELX
bffbf940
JJ
3668 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSGD
3669 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSLD
3670 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF32
d6ab8113 3671 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF64
bffbf940 3672 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF
a533c8df 3673 || fixP->fx_r_type == BFD_RELOC_X86_64_CODE_4_GOTTPOFF
5bc71c2a 3674 || fixP->fx_r_type == BFD_RELOC_X86_64_CODE_6_GOTTPOFF
bffbf940 3675 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF32
d6ab8113
JB
3676 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF64
3677 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTOFF64
67a4f2b7 3678 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
a533c8df 3679 || fixP->fx_r_type == BFD_RELOC_X86_64_CODE_4_GOTPC32_TLSDESC
67a4f2b7 3680 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSDESC_CALL
252b5132
RH
3681 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3682 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3683 return 0;
3684 return 1;
3685}
071c5d81 3686#endif
252b5132 3687
a9aabc23
JB
3688static INLINE bool
3689want_disp32 (const insn_template *t)
3690{
3691 return flag_code != CODE_64BIT
3692 || i.prefix[ADDR_PREFIX]
7fc69528 3693 || (t->mnem_off == MN_lea
fe134c65
JB
3694 && (!i.types[1].bitfield.qword
3695 || t->opcode_modifier.size == SIZE32));
a9aabc23
JB
3696}
3697
b4cac588 3698static int
e3bb37b5 3699intel_float_operand (const char *mnemonic)
252b5132 3700{
9306ca4a
JB
3701 /* Note that the value returned is meaningful only for opcodes with (memory)
3702 operands, hence the code here is free to improperly handle opcodes that
3703 have no operands (for better performance and smaller code). */
3704
3705 if (mnemonic[0] != 'f')
3706 return 0; /* non-math */
3707
3708 switch (mnemonic[1])
3709 {
3710 /* fclex, fdecstp, fdisi, femms, feni, fincstp, finit, fsetpm, and
3711 the fs segment override prefix not currently handled because no
3712 call path can make opcodes without operands get here */
3713 case 'i':
3714 return 2 /* integer op */;
3715 case 'l':
3716 if (mnemonic[2] == 'd' && (mnemonic[3] == 'c' || mnemonic[3] == 'e'))
3717 return 3; /* fldcw/fldenv */
3718 break;
3719 case 'n':
3720 if (mnemonic[2] != 'o' /* fnop */)
3721 return 3; /* non-waiting control op */
3722 break;
3723 case 'r':
3724 if (mnemonic[2] == 's')
3725 return 3; /* frstor/frstpm */
3726 break;
3727 case 's':
3728 if (mnemonic[2] == 'a')
3729 return 3; /* fsave */
3730 if (mnemonic[2] == 't')
3731 {
3732 switch (mnemonic[3])
3733 {
3734 case 'c': /* fstcw */
3735 case 'd': /* fstdw */
3736 case 'e': /* fstenv */
3737 case 's': /* fsts[gw] */
3738 return 3;
3739 }
3740 }
3741 break;
3742 case 'x':
3743 if (mnemonic[2] == 'r' || mnemonic[2] == 's')
3744 return 0; /* fxsave/fxrstor are not really math ops */
3745 break;
3746 }
252b5132 3747
9306ca4a 3748 return 1;
252b5132
RH
3749}
3750
9a182d04
JB
3751static INLINE void
3752install_template (const insn_template *t)
3753{
3754 unsigned int l;
3755
3756 i.tm = *t;
3757
a6f3add0
JB
3758 /* Dual VEX/EVEX templates need stripping one of the possible variants. */
3759 if (t->opcode_modifier.vex && t->opcode_modifier.evex)
6177c84d 3760 {
a5e91879
JB
3761 if ((maybe_cpu (t, CpuAVX) || maybe_cpu (t, CpuAVX2)
3762 || maybe_cpu (t, CpuFMA))
3763 && (maybe_cpu (t, CpuAVX512F) || maybe_cpu (t, CpuAVX512VL)))
a6f3add0 3764 {
6177c84d 3765 if (need_evex_encoding (t))
a6f3add0
JB
3766 {
3767 i.tm.opcode_modifier.vex = 0;
a5e91879
JB
3768 i.tm.cpu.bitfield.cpuavx512f = i.tm.cpu_any.bitfield.cpuavx512f;
3769 i.tm.cpu.bitfield.cpuavx512vl = i.tm.cpu_any.bitfield.cpuavx512vl;
a6f3add0
JB
3770 }
3771 else
3772 {
3773 i.tm.opcode_modifier.evex = 0;
a5e91879
JB
3774 if (i.tm.cpu_any.bitfield.cpuavx)
3775 i.tm.cpu.bitfield.cpuavx = 1;
3776 else if (!i.tm.cpu.bitfield.isa)
3777 i.tm.cpu.bitfield.isa = i.tm.cpu_any.bitfield.isa;
3778 else
3779 gas_assert (i.tm.cpu.bitfield.isa == i.tm.cpu_any.bitfield.isa);
a6f3add0
JB
3780 }
3781 }
6177c84d
CL
3782
3783 if ((maybe_cpu (t, CpuCMPCCXADD) || maybe_cpu (t, CpuAMX_TILE)
3784 || maybe_cpu (t, CpuAVX512F) || maybe_cpu (t, CpuAVX512DQ)
3785 || maybe_cpu (t, CpuAVX512BW) || maybe_cpu (t, CpuBMI)
5190fa38 3786 || maybe_cpu (t, CpuBMI2) || maybe_cpu (t, CpuUSER_MSR))
6177c84d
CL
3787 && maybe_cpu (t, CpuAPX_F))
3788 {
3789 if (need_evex_encoding (t))
3790 i.tm.opcode_modifier.vex = 0;
3791 else
3792 i.tm.opcode_modifier.evex = 0;
3793 }
3794 }
a6f3add0 3795
9a182d04
JB
3796 /* Note that for pseudo prefixes this produces a length of 1. But for them
3797 the length isn't interesting at all. */
3798 for (l = 1; l < 4; ++l)
3799 if (!(t->base_opcode >> (8 * l)))
3800 break;
3801
3802 i.opcode_length = l;
3803}
3804
c0f3af97
L
3805/* Build the VEX prefix. */
3806
3807static void
d3ce72d0 3808build_vex_prefix (const insn_template *t)
c0f3af97
L
3809{
3810 unsigned int register_specifier;
c0f3af97 3811 unsigned int vector_length;
03751133 3812 unsigned int w;
c0f3af97
L
3813
3814 /* Check register specifier. */
3815 if (i.vex.register_specifier)
43234a1e
L
3816 {
3817 register_specifier =
3818 ~register_number (i.vex.register_specifier) & 0xf;
3819 gas_assert ((i.vex.register_specifier->reg_flags & RegVRex) == 0);
3820 }
c0f3af97
L
3821 else
3822 register_specifier = 0xf;
3823
79f0fa25
L
3824 /* Use 2-byte VEX prefix by swapping destination and source operand
3825 if there are more than 1 register operand. */
3826 if (i.reg_operands > 1
e346d50a 3827 && i.encoding != encoding_vex3
86fa6981 3828 && i.dir_encoding == dir_encoding_default
fa99fab2 3829 && i.operands == i.reg_operands
dbbc8b7e 3830 && operand_type_equal (&i.types[0], &i.types[i.operands - 1])
ddb62495 3831 && i.tm.opcode_space == SPACE_0F
dbbc8b7e 3832 && (i.tm.opcode_modifier.load || i.tm.opcode_modifier.d)
fa99fab2
L
3833 && i.rex == REX_B)
3834 {
67748abf 3835 unsigned int xchg;
fa99fab2 3836
67748abf 3837 swap_2_operands (0, i.operands - 1);
fa99fab2 3838
9c2799c2 3839 gas_assert (i.rm.mode == 3);
fa99fab2
L
3840
3841 i.rex = REX_R;
3842 xchg = i.rm.regmem;
3843 i.rm.regmem = i.rm.reg;
3844 i.rm.reg = xchg;
3845
dbbc8b7e
JB
3846 if (i.tm.opcode_modifier.d)
3847 i.tm.base_opcode ^= (i.tm.base_opcode & 0xee) != 0x6e
2c735193 3848 ? Opcode_ExtD : Opcode_SIMD_IntD;
dbbc8b7e 3849 else /* Use the next insn. */
9a182d04 3850 install_template (&t[1]);
fa99fab2
L
3851 }
3852
79dec6b7
JB
3853 /* Use 2-byte VEX prefix by swapping commutative source operands if there
3854 are no memory operands and at least 3 register ones. */
3855 if (i.reg_operands >= 3
e346d50a 3856 && i.encoding != encoding_vex3
79dec6b7
JB
3857 && i.reg_operands == i.operands - i.imm_operands
3858 && i.tm.opcode_modifier.vex
3859 && i.tm.opcode_modifier.commutative
7440781b
JB
3860 /* .commutative aliases .staticrounding; disambiguate. */
3861 && !i.tm.opcode_modifier.sae
33740f21
JB
3862 && (i.tm.opcode_modifier.sse2avx
3863 || (optimize > 1 && !i.no_optimize))
79dec6b7
JB
3864 && i.rex == REX_B
3865 && i.vex.register_specifier
3866 && !(i.vex.register_specifier->reg_flags & RegRex))
3867 {
3868 unsigned int xchg = i.operands - i.reg_operands;
79dec6b7 3869
ddb62495 3870 gas_assert (i.tm.opcode_space == SPACE_0F);
79dec6b7
JB
3871 gas_assert (!i.tm.opcode_modifier.sae);
3872 gas_assert (operand_type_equal (&i.types[i.operands - 2],
3873 &i.types[i.operands - 3]));
3874 gas_assert (i.rm.mode == 3);
3875
67748abf 3876 swap_2_operands (xchg, xchg + 1);
79dec6b7
JB
3877
3878 i.rex = 0;
3879 xchg = i.rm.regmem | 8;
3880 i.rm.regmem = ~register_specifier & 0xf;
3881 gas_assert (!(i.rm.regmem & 8));
3882 i.vex.register_specifier += xchg - i.rm.regmem;
3883 register_specifier = ~xchg & 0xf;
3884 }
3885
539f890d
L
3886 if (i.tm.opcode_modifier.vex == VEXScalar)
3887 vector_length = avxscalar;
10c17abd
JB
3888 else if (i.tm.opcode_modifier.vex == VEX256)
3889 vector_length = 1;
d0c2e3ec
JB
3890 else if (dot_insn () && i.tm.opcode_modifier.vex == VEX128)
3891 vector_length = 0;
539f890d 3892 else
10c17abd 3893 {
56522fc5 3894 unsigned int op;
10c17abd 3895
c7213af9
L
3896 /* Determine vector length from the last multi-length vector
3897 operand. */
10c17abd 3898 vector_length = 0;
56522fc5 3899 for (op = t->operands; op--;)
10c17abd
JB
3900 if (t->operand_types[op].bitfield.xmmword
3901 && t->operand_types[op].bitfield.ymmword
3902 && i.types[op].bitfield.ymmword)
3903 {
3904 vector_length = 1;
3905 break;
3906 }
3907 }
c0f3af97 3908
03751133
L
3909 /* Check the REX.W bit and VEXW. */
3910 if (i.tm.opcode_modifier.vexw == VEXWIG)
3911 w = (vexwig == vexw1 || (i.rex & REX_W)) ? 1 : 0;
ebe82bfd 3912 else if (i.tm.opcode_modifier.vexw && !(i.rex & REX_W))
03751133
L
3913 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3914 else
931d03b7 3915 w = (flag_code == CODE_64BIT ? i.rex & REX_W : vexwig == vexw1) ? 1 : 0;
03751133 3916
c0f3af97 3917 /* Use 2-byte VEX prefix if possible. */
03751133 3918 if (w == 0
e346d50a 3919 && i.encoding != encoding_vex3
ddb62495 3920 && i.tm.opcode_space == SPACE_0F
c0f3af97
L
3921 && (i.rex & (REX_W | REX_X | REX_B)) == 0)
3922 {
3923 /* 2-byte VEX prefix. */
3924 unsigned int r;
3925
3926 i.vex.length = 2;
3927 i.vex.bytes[0] = 0xc5;
3928
3929 /* Check the REX.R bit. */
3930 r = (i.rex & REX_R) ? 0 : 1;
3931 i.vex.bytes[1] = (r << 7
3932 | register_specifier << 3
3933 | vector_length << 2
35648716 3934 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3935 }
3936 else
3937 {
3938 /* 3-byte VEX prefix. */
f88c9eb0 3939 i.vex.length = 3;
f88c9eb0 3940
ddb62495 3941 switch (i.tm.opcode_space)
5dd85c99 3942 {
441f6aca
JB
3943 case SPACE_0F:
3944 case SPACE_0F38:
3945 case SPACE_0F3A:
8170af78 3946 case SPACE_VEXMAP7:
80de6e00 3947 i.vex.bytes[0] = 0xc4;
7f399153 3948 break;
441f6aca
JB
3949 case SPACE_XOP08:
3950 case SPACE_XOP09:
3951 case SPACE_XOP0A:
f88c9eb0 3952 i.vex.bytes[0] = 0x8f;
7f399153
L
3953 break;
3954 default:
3955 abort ();
f88c9eb0 3956 }
c0f3af97 3957
c0f3af97
L
3958 /* The high 3 bits of the second VEX byte are 1's compliment
3959 of RXB bits from REX. */
d0c2e3ec
JB
3960 i.vex.bytes[1] = ((~i.rex & 7) << 5)
3961 | (!dot_insn () ? i.tm.opcode_space
3962 : i.insn_opcode_space);
c0f3af97 3963
c0f3af97
L
3964 i.vex.bytes[2] = (w << 7
3965 | register_specifier << 3
3966 | vector_length << 2
35648716 3967 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3968 }
3969}
3970
5b7c81bd 3971static INLINE bool
7a8655d2
JB
3972is_any_vex_encoding (const insn_template *t)
3973{
706ce984 3974 return t->opcode_modifier.vex || t->opcode_modifier.evex;
7a8655d2
JB
3975}
3976
6177c84d
CL
3977/* We can use this function only when the current encoding is evex. */
3978static INLINE bool
3979is_apx_evex_encoding (void)
3980{
dd74a603 3981 return i.rex2 || i.tm.opcode_space == SPACE_EVEXMAP4 || i.has_nf
6177c84d
CL
3982 || (i.vex.register_specifier
3983 && (i.vex.register_specifier->reg_flags & RegRex2));
3984}
3985
80d61d8d
CL
3986static INLINE bool
3987is_apx_rex2_encoding (void)
3988{
ac32c879 3989 return i.rex2 || i.rex2_encoding
3037cefe 3990 || i.tm.opcode_modifier.rex2;
80d61d8d
CL
3991}
3992
a5748e0d
JB
3993static unsigned int
3994get_broadcast_bytes (const insn_template *t, bool diag)
3995{
3996 unsigned int op, bytes;
3997 const i386_operand_type *types;
3998
3999 if (i.broadcast.type)
9b345ce8 4000 return (1 << (t->opcode_modifier.broadcast - 1)) * i.broadcast.type;
a5748e0d
JB
4001
4002 gas_assert (intel_syntax);
4003
4004 for (op = 0; op < t->operands; ++op)
4005 if (t->operand_types[op].bitfield.baseindex)
4006 break;
4007
4008 gas_assert (op < t->operands);
4009
706ce984 4010 if (t->opcode_modifier.evex != EVEXDYN)
a5748e0d
JB
4011 switch (i.broadcast.bytes)
4012 {
4013 case 1:
4014 if (t->operand_types[op].bitfield.word)
4015 return 2;
4016 /* Fall through. */
4017 case 2:
4018 if (t->operand_types[op].bitfield.dword)
4019 return 4;
4020 /* Fall through. */
4021 case 4:
4022 if (t->operand_types[op].bitfield.qword)
4023 return 8;
4024 /* Fall through. */
4025 case 8:
4026 if (t->operand_types[op].bitfield.xmmword)
4027 return 16;
4028 if (t->operand_types[op].bitfield.ymmword)
4029 return 32;
4030 if (t->operand_types[op].bitfield.zmmword)
4031 return 64;
4032 /* Fall through. */
4033 default:
4034 abort ();
4035 }
4036
4037 gas_assert (op + 1 < t->operands);
4038
4039 if (t->operand_types[op + 1].bitfield.xmmword
4040 + t->operand_types[op + 1].bitfield.ymmword
4041 + t->operand_types[op + 1].bitfield.zmmword > 1)
4042 {
4043 types = &i.types[op + 1];
4044 diag = false;
4045 }
4046 else /* Ambiguous - guess with a preference to non-AVX512VL forms. */
4047 types = &t->operand_types[op];
4048
4049 if (types->bitfield.zmmword)
4050 bytes = 64;
4051 else if (types->bitfield.ymmword)
4052 bytes = 32;
4053 else
4054 bytes = 16;
4055
4056 if (diag)
4057 as_warn (_("ambiguous broadcast for `%s', using %u-bit form"),
76d3f746 4058 insn_name (t), bytes * 8);
a5748e0d
JB
4059
4060 return bytes;
4061}
4062
43234a1e
L
4063/* Build the EVEX prefix. */
4064
4065static void
4066build_evex_prefix (void)
4067{
35648716 4068 unsigned int register_specifier, w;
43234a1e
L
4069 rex_byte vrex_used = 0;
4070
4071 /* Check register specifier. */
4072 if (i.vex.register_specifier)
4073 {
4074 gas_assert ((i.vrex & REX_X) == 0);
4075
4076 register_specifier = i.vex.register_specifier->reg_num;
4077 if ((i.vex.register_specifier->reg_flags & RegRex))
4078 register_specifier += 8;
4079 /* The upper 16 registers are encoded in the fourth byte of the
4080 EVEX prefix. */
4081 if (!(i.vex.register_specifier->reg_flags & RegVRex))
4082 i.vex.bytes[3] = 0x8;
4083 register_specifier = ~register_specifier & 0xf;
4084 }
4085 else
4086 {
4087 register_specifier = 0xf;
4088
4089 /* Encode upper 16 vector index register in the fourth byte of
4090 the EVEX prefix. */
4091 if (!(i.vrex & REX_X))
4092 i.vex.bytes[3] = 0x8;
4093 else
4094 vrex_used |= REX_X;
4095 }
4096
43234a1e
L
4097 /* 4 byte EVEX prefix. */
4098 i.vex.length = 4;
4099 i.vex.bytes[0] = 0x62;
4100
43234a1e
L
4101 /* The high 3 bits of the second EVEX byte are 1's compliment of RXB
4102 bits from REX. */
ddb62495 4103 gas_assert (i.tm.opcode_space >= SPACE_0F);
5190fa38 4104 gas_assert (i.tm.opcode_space <= SPACE_VEXMAP7);
d0c2e3ec
JB
4105 i.vex.bytes[1] = ((~i.rex & 7) << 5)
4106 | (!dot_insn () ? i.tm.opcode_space
4107 : i.insn_opcode_space);
43234a1e
L
4108
4109 /* The fifth bit of the second EVEX byte is 1's compliment of the
4110 REX_R bit in VREX. */
4111 if (!(i.vrex & REX_R))
4112 i.vex.bytes[1] |= 0x10;
4113 else
4114 vrex_used |= REX_R;
4115
4116 if ((i.reg_operands + i.imm_operands) == i.operands)
4117 {
4118 /* When all operands are registers, the REX_X bit in REX is not
4119 used. We reuse it to encode the upper 16 registers, which is
4120 indicated by the REX_B bit in VREX. The REX_X bit is encoded
4121 as 1's compliment. */
4122 if ((i.vrex & REX_B))
4123 {
4124 vrex_used |= REX_B;
4125 i.vex.bytes[1] &= ~0x40;
4126 }
4127 }
4128
4129 /* EVEX instructions shouldn't need the REX prefix. */
4130 i.vrex &= ~vrex_used;
4131 gas_assert (i.vrex == 0);
4132
6865c043
L
4133 /* Check the REX.W bit and VEXW. */
4134 if (i.tm.opcode_modifier.vexw == VEXWIG)
4135 w = (evexwig == evexw1 || (i.rex & REX_W)) ? 1 : 0;
24187fb9 4136 else if (i.tm.opcode_modifier.vexw && !(i.rex & REX_W))
6865c043
L
4137 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
4138 else
931d03b7 4139 w = (flag_code == CODE_64BIT ? i.rex & REX_W : evexwig == evexw1) ? 1 : 0;
43234a1e 4140
43234a1e 4141 /* The third byte of the EVEX prefix. */
35648716
JB
4142 i.vex.bytes[2] = ((w << 7)
4143 | (register_specifier << 3)
4144 | 4 /* Encode the U bit. */
4145 | i.tm.opcode_modifier.opcodeprefix);
43234a1e
L
4146
4147 /* The fourth byte of the EVEX prefix. */
4148 /* The zeroing-masking bit. */
6225c532 4149 if (i.mask.reg && i.mask.zeroing)
43234a1e
L
4150 i.vex.bytes[3] |= 0x80;
4151
4152 /* Don't always set the broadcast bit if there is no RC. */
ca5312a2 4153 if (i.rounding.type == rc_none)
43234a1e
L
4154 {
4155 /* Encode the vector length. */
4156 unsigned int vec_length;
4157
706ce984 4158 if (i.tm.opcode_modifier.evex == EVEXDYN)
e771e7c9 4159 {
56522fc5 4160 unsigned int op;
e771e7c9 4161
c7213af9
L
4162 /* Determine vector length from the last multi-length vector
4163 operand. */
56522fc5 4164 for (op = i.operands; op--;)
e771e7c9
JB
4165 if (i.tm.operand_types[op].bitfield.xmmword
4166 + i.tm.operand_types[op].bitfield.ymmword
4167 + i.tm.operand_types[op].bitfield.zmmword > 1)
4168 {
4169 if (i.types[op].bitfield.zmmword)
c7213af9
L
4170 {
4171 i.tm.opcode_modifier.evex = EVEX512;
4172 break;
4173 }
e771e7c9 4174 else if (i.types[op].bitfield.ymmword)
c7213af9
L
4175 {
4176 i.tm.opcode_modifier.evex = EVEX256;
4177 break;
4178 }
e771e7c9 4179 else if (i.types[op].bitfield.xmmword)
c7213af9
L
4180 {
4181 i.tm.opcode_modifier.evex = EVEX128;
4182 break;
4183 }
9b345ce8
JB
4184 else if ((i.broadcast.type || i.broadcast.bytes)
4185 && op == i.broadcast.operand)
625cbd7a 4186 {
a5748e0d 4187 switch (get_broadcast_bytes (&i.tm, true))
625cbd7a
JB
4188 {
4189 case 64:
4190 i.tm.opcode_modifier.evex = EVEX512;
4191 break;
4192 case 32:
4193 i.tm.opcode_modifier.evex = EVEX256;
4194 break;
4195 case 16:
4196 i.tm.opcode_modifier.evex = EVEX128;
4197 break;
4198 default:
c7213af9 4199 abort ();
625cbd7a 4200 }
c7213af9 4201 break;
625cbd7a 4202 }
e771e7c9 4203 }
c7213af9 4204
56522fc5 4205 if (op >= MAX_OPERANDS)
c7213af9 4206 abort ();
e771e7c9
JB
4207 }
4208
43234a1e
L
4209 switch (i.tm.opcode_modifier.evex)
4210 {
4211 case EVEXLIG: /* LL' is ignored */
4212 vec_length = evexlig << 5;
4213 break;
4214 case EVEX128:
4215 vec_length = 0 << 5;
4216 break;
4217 case EVEX256:
4218 vec_length = 1 << 5;
4219 break;
4220 case EVEX512:
4221 vec_length = 2 << 5;
4222 break;
d0c2e3ec
JB
4223 case EVEX_L3:
4224 if (dot_insn ())
4225 {
4226 vec_length = 3 << 5;
4227 break;
4228 }
4229 /* Fall through. */
43234a1e
L
4230 default:
4231 abort ();
4232 break;
4233 }
4234 i.vex.bytes[3] |= vec_length;
4235 /* Encode the broadcast bit. */
9b345ce8 4236 if (i.broadcast.type || i.broadcast.bytes)
43234a1e
L
4237 i.vex.bytes[3] |= 0x10;
4238 }
ca5312a2
JB
4239 else if (i.rounding.type != saeonly)
4240 i.vex.bytes[3] |= 0x10 | (i.rounding.type << 5);
43234a1e 4241 else
ca5312a2 4242 i.vex.bytes[3] |= 0x10 | (evexrcig << 5);
43234a1e 4243
6225c532
JB
4244 if (i.mask.reg)
4245 i.vex.bytes[3] |= i.mask.reg->reg_num;
43234a1e
L
4246}
4247
80d61d8d
CL
4248/* Build (2 bytes) rex2 prefix.
4249 | D5h |
4250 | m | R4 X4 B4 | W R X B |
4251
4252 Rex2 reuses i.vex as they both encode i.tm.opcode_space in their prefixes.
4253 */
4254static void
4255build_rex2_prefix (void)
4256{
4257 i.vex.length = 2;
4258 i.vex.bytes[0] = 0xd5;
4259 /* For the W R X B bits, the variables of rex prefix will be reused. */
4260 i.vex.bytes[1] = ((i.tm.opcode_space << 7)
4261 | (i.rex2 << 4) | i.rex);
4262}
4263
6177c84d
CL
4264/* Build the EVEX prefix (4-byte) for evex insn
4265 | 62h |
4266 | `R`X`B`R' | B'mmm |
4267 | W | v`v`v`v | `x' | pp |
4268 | z| L'L | b | `v | aaa |
4269*/
4270static void
4271build_apx_evex_prefix (void)
4272{
4273 build_evex_prefix ();
4274 if (i.rex2 & REX_R)
4275 i.vex.bytes[1] &= ~0x10;
4276 if (i.rex2 & REX_B)
4277 i.vex.bytes[1] |= 0x08;
4278 if (i.rex2 & REX_X)
b5247082
CL
4279 {
4280 gas_assert (i.rm.mode != 3);
4281 i.vex.bytes[2] &= ~0x04;
4282 }
6177c84d
CL
4283 if (i.vex.register_specifier
4284 && i.vex.register_specifier->reg_flags & RegRex2)
4285 i.vex.bytes[3] &= ~0x08;
3083f376 4286
4287 /* Encode the NDD bit of the instruction promoted from the legacy
4288 space. */
4289 if (i.vex.register_specifier && i.tm.opcode_space == SPACE_EVEXMAP4)
4290 i.vex.bytes[3] |= 0x10;
dd74a603
CL
4291
4292 /* Encode the NF bit. */
4293 if (i.has_nf)
4294 i.vex.bytes[3] |= 0x04;
6177c84d
CL
4295}
4296
ce705688
JB
4297static void establish_rex (void)
4298{
4299 /* Note that legacy encodings have at most 2 non-immediate operands. */
4300 unsigned int first = i.imm_operands;
4301 unsigned int last = i.operands > first ? i.operands - first - 1 : first;
4302
4303 /* Respect a user-specified REX prefix. */
4304 i.rex |= i.prefix[REX_PREFIX] & REX_OPCODE;
4305
3a8ecbda
CL
4306 /* For 8 bit RegRex64 registers without a prefix, we need an empty rex prefix. */
4307 if (((i.types[first].bitfield.class == Reg && i.types[first].bitfield.byte
4308 && (i.op[first].regs->reg_flags & RegRex64) != 0)
4309 || (i.types[last].bitfield.class == Reg && i.types[last].bitfield.byte
4310 && (i.op[last].regs->reg_flags & RegRex64) != 0))
b757e3c1 4311 && !is_apx_rex2_encoding () && !is_any_vex_encoding (&i.tm))
3a8ecbda
CL
4312 i.rex |= REX_OPCODE;
4313
4314 /* For REX/REX2/EVEX prefix instructions, we need to convert old registers
4315 (AL, CL, DL and BL) to new ones (AXL, CXL, DXL and BXL) and reject AH,
4316 CH, DH and BH. */
9f8b42c8 4317 if (i.rex || i.rex2 || i.tm.opcode_modifier.evex)
ce705688 4318 {
3a8ecbda 4319 for (unsigned int x = first; x <= last; x++)
ce705688
JB
4320 {
4321 /* Look for 8 bit operand that uses old registers. */
4322 if (i.types[x].bitfield.class == Reg && i.types[x].bitfield.byte
4323 && (i.op[x].regs->reg_flags & RegRex64) == 0)
4324 {
4325 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
4326 /* In case it is "hi" register, give up. */
4327 if (i.op[x].regs->reg_num > 3)
4328 as_bad (_("can't encode register '%s%s' in an "
9f8b42c8
CL
4329 "instruction requiring %s prefix"),
4330 register_prefix, i.op[x].regs->reg_name,
4331 i.tm.opcode_modifier.evex ? "EVEX" : "REX/REX2");
ce705688
JB
4332
4333 /* Otherwise it is equivalent to the extended register.
4334 Since the encoding doesn't change this is merely
4335 cosmetic cleanup for debug output. */
4336 i.op[x].regs += 8;
4337 }
4338 }
4339 }
4340
192781a3 4341 if (i.rex == 0 && i.rex2 == 0 && (i.rex_encoding || i.rex2_encoding))
ce705688
JB
4342 {
4343 /* Check if we can add a REX_OPCODE byte. Look for 8 bit operand
4344 that uses legacy register. If it is "hi" register, don't add
80d61d8d 4345 rex and rex2 prefix. */
ce705688
JB
4346 unsigned int x;
4347
4348 for (x = first; x <= last; x++)
4349 if (i.types[x].bitfield.class == Reg
4350 && i.types[x].bitfield.byte
4351 && (i.op[x].regs->reg_flags & RegRex64) == 0
4352 && i.op[x].regs->reg_num > 3)
4353 {
4354 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
4355 i.rex_encoding = false;
80d61d8d 4356 i.rex2_encoding = false;
ce705688
JB
4357 break;
4358 }
4359
4360 if (i.rex_encoding)
4361 i.rex = REX_OPCODE;
4362 }
4363
192781a3
CL
4364 if (is_apx_rex2_encoding ())
4365 {
4366 build_rex2_prefix ();
4367 /* The individual REX.RXBW bits got consumed. */
4368 i.rex &= REX_OPCODE;
4369 }
4370 else if (i.rex != 0)
4371 add_prefix (REX_OPCODE | i.rex);
ce705688
JB
4372}
4373
65da13b5
L
4374static void
4375process_immext (void)
4376{
4377 expressionS *exp;
4378
c0f3af97 4379 /* These AMD 3DNow! and SSE2 instructions have an opcode suffix
65da13b5
L
4380 which is coded in the same place as an 8-bit immediate field
4381 would be. Here we fake an 8-bit immediate operand from the
4382 opcode suffix stored in tm.extension_opcode.
4383
c1e679ec 4384 AVX instructions also use this encoding, for some of
c0f3af97 4385 3 argument instructions. */
65da13b5 4386
43234a1e 4387 gas_assert (i.imm_operands <= 1
7ab9ffdd 4388 && (i.operands <= 2
7a8655d2 4389 || (is_any_vex_encoding (&i.tm)
7ab9ffdd 4390 && i.operands <= 4)));
65da13b5
L
4391
4392 exp = &im_expressions[i.imm_operands++];
4393 i.op[i.operands].imms = exp;
be1643ff 4394 i.types[i.operands].bitfield.imm8 = 1;
65da13b5
L
4395 i.operands++;
4396 exp->X_op = O_constant;
4397 exp->X_add_number = i.tm.extension_opcode;
4398 i.tm.extension_opcode = None;
4399}
4400
42164a71
L
4401
4402static int
4403check_hle (void)
4404{
742732c7 4405 switch (i.tm.opcode_modifier.prefixok)
42164a71
L
4406 {
4407 default:
4408 abort ();
742732c7
JB
4409 case PrefixLock:
4410 case PrefixNone:
4411 case PrefixNoTrack:
4412 case PrefixRep:
165de32a 4413 as_bad (_("invalid instruction `%s' after `%s'"),
76d3f746 4414 insn_name (&i.tm), i.hle_prefix);
42164a71 4415 return 0;
742732c7 4416 case PrefixHLELock:
42164a71
L
4417 if (i.prefix[LOCK_PREFIX])
4418 return 1;
165de32a 4419 as_bad (_("missing `lock' with `%s'"), i.hle_prefix);
42164a71 4420 return 0;
742732c7 4421 case PrefixHLEAny:
42164a71 4422 return 1;
742732c7 4423 case PrefixHLERelease:
42164a71
L
4424 if (i.prefix[HLE_PREFIX] != XRELEASE_PREFIX_OPCODE)
4425 {
4426 as_bad (_("instruction `%s' after `xacquire' not allowed"),
76d3f746 4427 insn_name (&i.tm));
42164a71
L
4428 return 0;
4429 }
8dc0818e 4430 if (i.mem_operands == 0 || !(i.flags[i.operands - 1] & Operand_Mem))
42164a71
L
4431 {
4432 as_bad (_("memory destination needed for instruction `%s'"
76d3f746 4433 " after `xrelease'"), insn_name (&i.tm));
42164a71
L
4434 return 0;
4435 }
4436 return 1;
4437 }
4438}
4439
c8480b58
L
4440/* Encode aligned vector move as unaligned vector move. */
4441
4442static void
4443encode_with_unaligned_vector_move (void)
4444{
4445 switch (i.tm.base_opcode)
4446 {
b3a9fe6f
L
4447 case 0x28: /* Load instructions. */
4448 case 0x29: /* Store instructions. */
c8480b58 4449 /* movaps/movapd/vmovaps/vmovapd. */
ddb62495 4450 if (i.tm.opcode_space == SPACE_0F
c8480b58 4451 && i.tm.opcode_modifier.opcodeprefix <= PREFIX_0X66)
b3a9fe6f 4452 i.tm.base_opcode = 0x10 | (i.tm.base_opcode & 1);
c8480b58 4453 break;
b3a9fe6f
L
4454 case 0x6f: /* Load instructions. */
4455 case 0x7f: /* Store instructions. */
c8480b58 4456 /* movdqa/vmovdqa/vmovdqa64/vmovdqa32. */
ddb62495 4457 if (i.tm.opcode_space == SPACE_0F
c8480b58
L
4458 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0X66)
4459 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
4460 break;
4461 default:
4462 break;
4463 }
4464}
4465
b6f8c7c4
L
4466/* Try the shortest encoding by shortening operand size. */
4467
4468static void
4469optimize_encoding (void)
4470{
a0a1771e 4471 unsigned int j;
b6f8c7c4 4472
7fc69528 4473 if (i.tm.mnem_off == MN_lea)
fe134c65
JB
4474 {
4475 /* Optimize: -O:
4476 lea symbol, %rN -> mov $symbol, %rN
4477 lea (%rM), %rN -> mov %rM, %rN
4478 lea (,%rM,1), %rN -> mov %rM, %rN
4479
4480 and in 32-bit mode for 16-bit addressing
4481
4482 lea (%rM), %rN -> movzx %rM, %rN
4483
4484 and in 64-bit mode zap 32-bit addressing in favor of using a
4485 32-bit (or less) destination.
4486 */
4487 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
4488 {
4489 if (!i.op[1].regs->reg_type.bitfield.word)
4490 i.tm.opcode_modifier.size = SIZE32;
4491 i.prefix[ADDR_PREFIX] = 0;
4492 }
4493
4494 if (!i.index_reg && !i.base_reg)
4495 {
4496 /* Handle:
4497 lea symbol, %rN -> mov $symbol, %rN
4498 */
4499 if (flag_code == CODE_64BIT)
4500 {
4501 /* Don't transform a relocation to a 16-bit one. */
4502 if (i.op[0].disps
4503 && i.op[0].disps->X_op != O_constant
4504 && i.op[1].regs->reg_type.bitfield.word)
4505 return;
4506
4507 if (!i.op[1].regs->reg_type.bitfield.qword
4508 || i.tm.opcode_modifier.size == SIZE32)
4509 {
4510 i.tm.base_opcode = 0xb8;
4511 i.tm.opcode_modifier.modrm = 0;
4512 if (!i.op[1].regs->reg_type.bitfield.word)
4513 i.types[0].bitfield.imm32 = 1;
4514 else
4515 {
4516 i.tm.opcode_modifier.size = SIZE16;
4517 i.types[0].bitfield.imm16 = 1;
4518 }
4519 }
4520 else
4521 {
4522 /* Subject to further optimization below. */
4523 i.tm.base_opcode = 0xc7;
4524 i.tm.extension_opcode = 0;
4525 i.types[0].bitfield.imm32s = 1;
4526 i.types[0].bitfield.baseindex = 0;
4527 }
4528 }
4529 /* Outside of 64-bit mode address and operand sizes have to match if
4530 a relocation is involved, as otherwise we wouldn't (currently) or
4531 even couldn't express the relocation correctly. */
4532 else if (i.op[0].disps
4533 && i.op[0].disps->X_op != O_constant
4534 && ((!i.prefix[ADDR_PREFIX])
4535 != (flag_code == CODE_32BIT
4536 ? i.op[1].regs->reg_type.bitfield.dword
4537 : i.op[1].regs->reg_type.bitfield.word)))
4538 return;
7772f168
JB
4539 /* In 16-bit mode converting LEA with 16-bit addressing and a 32-bit
4540 destination is going to grow encoding size. */
4541 else if (flag_code == CODE_16BIT
4542 && (optimize <= 1 || optimize_for_space)
4543 && !i.prefix[ADDR_PREFIX]
4544 && i.op[1].regs->reg_type.bitfield.dword)
4545 return;
fe134c65
JB
4546 else
4547 {
4548 i.tm.base_opcode = 0xb8;
4549 i.tm.opcode_modifier.modrm = 0;
4550 if (i.op[1].regs->reg_type.bitfield.dword)
4551 i.types[0].bitfield.imm32 = 1;
4552 else
4553 i.types[0].bitfield.imm16 = 1;
4554
4555 if (i.op[0].disps
4556 && i.op[0].disps->X_op == O_constant
4557 && i.op[1].regs->reg_type.bitfield.dword
60cfa10c
L
4558 /* NB: Add () to !i.prefix[ADDR_PREFIX] to silence
4559 GCC 5. */
4560 && (!i.prefix[ADDR_PREFIX]) != (flag_code == CODE_32BIT))
fe134c65
JB
4561 i.op[0].disps->X_add_number &= 0xffff;
4562 }
4563
4564 i.tm.operand_types[0] = i.types[0];
4565 i.imm_operands = 1;
4566 if (!i.op[0].imms)
4567 {
4568 i.op[0].imms = &im_expressions[0];
4569 i.op[0].imms->X_op = O_absent;
4570 }
4571 }
4572 else if (i.op[0].disps
4573 && (i.op[0].disps->X_op != O_constant
4574 || i.op[0].disps->X_add_number))
4575 return;
4576 else
4577 {
4578 /* Handle:
4579 lea (%rM), %rN -> mov %rM, %rN
4580 lea (,%rM,1), %rN -> mov %rM, %rN
4581 lea (%rM), %rN -> movzx %rM, %rN
4582 */
4583 const reg_entry *addr_reg;
4584
4585 if (!i.index_reg && i.base_reg->reg_num != RegIP)
4586 addr_reg = i.base_reg;
4587 else if (!i.base_reg
4588 && i.index_reg->reg_num != RegIZ
4589 && !i.log2_scale_factor)
4590 addr_reg = i.index_reg;
4591 else
4592 return;
4593
4594 if (addr_reg->reg_type.bitfield.word
4595 && i.op[1].regs->reg_type.bitfield.dword)
4596 {
4597 if (flag_code != CODE_32BIT)
4598 return;
ddb62495 4599 i.tm.opcode_space = SPACE_0F;
fe134c65
JB
4600 i.tm.base_opcode = 0xb7;
4601 }
4602 else
4603 i.tm.base_opcode = 0x8b;
4604
4605 if (addr_reg->reg_type.bitfield.dword
4606 && i.op[1].regs->reg_type.bitfield.qword)
4607 i.tm.opcode_modifier.size = SIZE32;
4608
4609 i.op[0].regs = addr_reg;
4610 i.reg_operands = 2;
4611 }
4612
4613 i.mem_operands = 0;
4614 i.disp_operands = 0;
4615 i.prefix[ADDR_PREFIX] = 0;
4616 i.prefix[SEG_PREFIX] = 0;
4617 i.seg[0] = NULL;
4618 }
4619
b6f8c7c4 4620 if (optimize_for_space
7fc69528 4621 && i.tm.mnem_off == MN_test
b6f8c7c4
L
4622 && i.reg_operands == 1
4623 && i.imm_operands == 1
4624 && !i.types[1].bitfield.byte
4625 && i.op[0].imms->X_op == O_constant
7fc69528 4626 && fits_in_imm7 (i.op[0].imms->X_add_number))
b6f8c7c4
L
4627 {
4628 /* Optimize: -Os:
4629 test $imm7, %r64/%r32/%r16 -> test $imm7, %r8
4630 */
4631 unsigned int base_regnum = i.op[1].regs->reg_num;
4632 if (flag_code == CODE_64BIT || base_regnum < 4)
4633 {
4634 i.types[1].bitfield.byte = 1;
4635 /* Ignore the suffix. */
4636 i.suffix = 0;
80d61d8d
CL
4637 /* Convert to byte registers. 8-bit registers are special,
4638 RegRex64 and non-RegRex64 each have 8 registers. */
7697afb6 4639 if (i.types[1].bitfield.word)
80d61d8d 4640 /* 32 (or 40) 8-bit registers. */
7697afb6 4641 j = 32;
80d61d8d
CL
4642 else if (i.types[1].bitfield.dword)
4643 /* 32 (or 40) 8-bit registers + 32 16-bit registers. */
4644 j = 64;
7697afb6 4645 else
80d61d8d
CL
4646 /* 32 (or 40) 8-bit registers + 32 16-bit registers
4647 + 32 32-bit registers. */
4648 j = 96;
4649
4650 /* In 64-bit mode, the following byte registers cannot be accessed
4651 if using the Rex and Rex2 prefix: AH, BH, CH, DH */
4652 if (!(i.op[1].regs->reg_flags & (RegRex | RegRex2)) && base_regnum < 4)
7697afb6
JB
4653 j += 8;
4654 i.op[1].regs -= j;
b6f8c7c4
L
4655 }
4656 }
4657 else if (flag_code == CODE_64BIT
ddb62495 4658 && i.tm.opcode_space == SPACE_BASE
d3d50934
L
4659 && ((i.types[1].bitfield.qword
4660 && i.reg_operands == 1
b6f8c7c4
L
4661 && i.imm_operands == 1
4662 && i.op[0].imms->X_op == O_constant
507916b8 4663 && ((i.tm.base_opcode == 0xb8
b6f8c7c4
L
4664 && i.tm.extension_opcode == None
4665 && fits_in_unsigned_long (i.op[0].imms->X_add_number))
4666 || (fits_in_imm31 (i.op[0].imms->X_add_number)
7fc69528 4667 && (i.tm.base_opcode == 0x24
b6f8c7c4
L
4668 || (i.tm.base_opcode == 0x80
4669 && i.tm.extension_opcode == 0x4)
7fc69528
JB
4670 || i.tm.mnem_off == MN_test
4671 || ((i.tm.base_opcode | 1) == 0xc7
b8364fa7
JB
4672 && i.tm.extension_opcode == 0x0)))
4673 || (fits_in_imm7 (i.op[0].imms->X_add_number)
4674 && i.tm.base_opcode == 0x83
4675 && i.tm.extension_opcode == 0x4)))
d3d50934
L
4676 || (i.types[0].bitfield.qword
4677 && ((i.reg_operands == 2
4678 && i.op[0].regs == i.op[1].regs
7fc69528
JB
4679 && (i.tm.mnem_off == MN_xor
4680 || i.tm.mnem_off == MN_sub))
4681 || i.tm.mnem_off == MN_clr))))
b6f8c7c4
L
4682 {
4683 /* Optimize: -O:
4684 andq $imm31, %r64 -> andl $imm31, %r32
b8364fa7 4685 andq $imm7, %r64 -> andl $imm7, %r32
b6f8c7c4
L
4686 testq $imm31, %r64 -> testl $imm31, %r32
4687 xorq %r64, %r64 -> xorl %r32, %r32
4688 subq %r64, %r64 -> subl %r32, %r32
4689 movq $imm31, %r64 -> movl $imm31, %r32
4690 movq $imm32, %r64 -> movl $imm32, %r32
4691 */
04784e33
JB
4692 i.tm.opcode_modifier.size = SIZE32;
4693 if (i.imm_operands)
4694 {
4695 i.types[0].bitfield.imm32 = 1;
4696 i.types[0].bitfield.imm32s = 0;
4697 i.types[0].bitfield.imm64 = 0;
4698 }
4699 else
4700 {
4701 i.types[0].bitfield.dword = 1;
4702 i.types[0].bitfield.qword = 0;
4703 }
4704 i.types[1].bitfield.dword = 1;
4705 i.types[1].bitfield.qword = 0;
7fc69528 4706 if (i.tm.mnem_off == MN_mov || i.tm.mnem_off == MN_lea)
b6f8c7c4
L
4707 {
4708 /* Handle
4709 movq $imm31, %r64 -> movl $imm31, %r32
4710 movq $imm32, %r64 -> movl $imm32, %r32
4711 */
4712 i.tm.operand_types[0].bitfield.imm32 = 1;
4713 i.tm.operand_types[0].bitfield.imm32s = 0;
4714 i.tm.operand_types[0].bitfield.imm64 = 0;
507916b8 4715 if ((i.tm.base_opcode | 1) == 0xc7)
b6f8c7c4
L
4716 {
4717 /* Handle
4718 movq $imm31, %r64 -> movl $imm31, %r32
4719 */
507916b8 4720 i.tm.base_opcode = 0xb8;
b6f8c7c4 4721 i.tm.extension_opcode = None;
507916b8 4722 i.tm.opcode_modifier.w = 0;
b6f8c7c4
L
4723 i.tm.opcode_modifier.modrm = 0;
4724 }
4725 }
4726 }
c73a37b2
JB
4727 else if (i.reg_operands == 3
4728 && i.op[0].regs == i.op[1].regs
4729 && i.encoding != encoding_evex
4730 && (i.tm.mnem_off == MN_xor
4731 || i.tm.mnem_off == MN_sub))
4732 {
4733 /* Optimize: -O:
4734 xorb %rNb, %rNb, %rMb -> xorl %rMd, %rMd
4735 xorw %rNw, %rNw, %rMw -> xorl %rMd, %rMd
4736 xorl %rNd, %rNd, %rMd -> xorl %rMd, %rMd
4737 xorq %rN, %rN, %rM -> xorl %rMd, %rMd
4738 subb %rNb, %rNb, %rMb -> subl %rMd, %rMd
4739 subw %rNw, %rNw, %rMw -> subl %rMd, %rMd
4740 subl %rNd, %rNd, %rMd -> subl %rMd, %rMd
4741 subq %rN, %rN, %rM -> subl %rMd, %rMd
4742 */
4743 i.tm.opcode_space = SPACE_BASE;
4744 i.tm.opcode_modifier.evex = 0;
4745 i.tm.opcode_modifier.size = SIZE32;
4746 i.types[0].bitfield.byte = 0;
4747 i.types[0].bitfield.word = 0;
4748 i.types[0].bitfield.dword = 1;
4749 i.types[0].bitfield.qword = 0;
4750 i.op[0].regs = i.op[2].regs;
4751 i.types[1] = i.types[0];
4752 i.op[1].regs = i.op[2].regs;
4753 i.reg_operands = 2;
4754 }
5641ec01
JB
4755 else if (optimize > 1
4756 && !optimize_for_space
4757 && i.reg_operands == 2
4758 && i.op[0].regs == i.op[1].regs
7fc69528 4759 && (i.tm.mnem_off == MN_and || i.tm.mnem_off == MN_or)
5641ec01
JB
4760 && (flag_code != CODE_64BIT || !i.types[0].bitfield.dword))
4761 {
4762 /* Optimize: -O2:
4763 andb %rN, %rN -> testb %rN, %rN
4764 andw %rN, %rN -> testw %rN, %rN
4765 andq %rN, %rN -> testq %rN, %rN
4766 orb %rN, %rN -> testb %rN, %rN
4767 orw %rN, %rN -> testw %rN, %rN
4768 orq %rN, %rN -> testq %rN, %rN
4769
4770 and outside of 64-bit mode
4771
4772 andl %rN, %rN -> testl %rN, %rN
4773 orl %rN, %rN -> testl %rN, %rN
4774 */
4775 i.tm.base_opcode = 0x84 | (i.tm.base_opcode & 1);
4776 }
ad2f4436
JB
4777 else if (i.tm.base_opcode == 0xba
4778 && i.tm.opcode_space == SPACE_0F
4779 && i.reg_operands == 1
4780 && i.op[0].imms->X_op == O_constant
4781 && i.op[0].imms->X_add_number >= 0)
4782 {
4783 /* Optimize: -O:
4784 btw $n, %rN -> btl $n, %rN (outside of 16-bit mode, n < 16)
4785 btq $n, %rN -> btl $n, %rN (in 64-bit mode, n < 32, N < 8)
4786 btl $n, %rN -> btw $n, %rN (in 16-bit mode, n < 16)
4787
4788 With <BT> one of bts, btr, and bts also:
4789 <BT>w $n, %rN -> btl $n, %rN (in 32-bit mode, n < 16)
4790 <BT>l $n, %rN -> btw $n, %rN (in 16-bit mode, n < 16)
4791 */
4792 switch (flag_code)
4793 {
4794 case CODE_64BIT:
4795 if (i.tm.extension_opcode != 4)
4796 break;
4797 if (i.types[1].bitfield.qword
4798 && i.op[0].imms->X_add_number < 32
4799 && !(i.op[1].regs->reg_flags & RegRex))
4800 i.tm.opcode_modifier.size = SIZE32;
4801 /* Fall through. */
4802 case CODE_32BIT:
4803 if (i.types[1].bitfield.word
4804 && i.op[0].imms->X_add_number < 16)
4805 i.tm.opcode_modifier.size = SIZE32;
4806 break;
4807 case CODE_16BIT:
4808 if (i.op[0].imms->X_add_number < 16)
4809 i.tm.opcode_modifier.size = SIZE16;
4810 break;
4811 }
4812 }
99112332 4813 else if (i.reg_operands == 3
b6f8c7c4
L
4814 && i.op[0].regs == i.op[1].regs
4815 && !i.types[2].bitfield.xmmword
4816 && (i.tm.opcode_modifier.vex
6225c532 4817 || ((!i.mask.reg || i.mask.zeroing)
706ce984 4818 && i.tm.opcode_modifier.evex
e346d50a 4819 && (i.encoding != encoding_evex
dd22218c 4820 || cpu_arch_isa_flags.bitfield.cpuavx512vl
734dfd1c 4821 || is_cpu (&i.tm, CpuAVX512VL)
7091c612 4822 || (i.tm.operand_types[2].bitfield.zmmword
dd22218c 4823 && i.types[2].bitfield.ymmword))))
ddb62495 4824 && i.tm.opcode_space == SPACE_0F
5844ccaa
JB
4825 && ((i.tm.base_opcode | 2) == 0x57
4826 || i.tm.base_opcode == 0xdf
4827 || i.tm.base_opcode == 0xef
4828 || (i.tm.base_opcode | 3) == 0xfb
4829 || i.tm.base_opcode == 0x42
4830 || i.tm.base_opcode == 0x47))
b6f8c7c4 4831 {
99112332 4832 /* Optimize: -O1:
8305403a
L
4833 VOP, one of vandnps, vandnpd, vxorps, vxorpd, vpsubb, vpsubd,
4834 vpsubq and vpsubw:
b6f8c7c4
L
4835 EVEX VOP %zmmM, %zmmM, %zmmN
4836 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4837 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4838 EVEX VOP %ymmM, %ymmM, %ymmN
4839 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4840 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4841 VEX VOP %ymmM, %ymmM, %ymmN
4842 -> VEX VOP %xmmM, %xmmM, %xmmN
4843 VOP, one of vpandn and vpxor:
4844 VEX VOP %ymmM, %ymmM, %ymmN
4845 -> VEX VOP %xmmM, %xmmM, %xmmN
4846 VOP, one of vpandnd and vpandnq:
4847 EVEX VOP %zmmM, %zmmM, %zmmN
4848 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4849 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4850 EVEX VOP %ymmM, %ymmM, %ymmN
4851 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4852 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4853 VOP, one of vpxord and vpxorq:
4854 EVEX VOP %zmmM, %zmmM, %zmmN
4855 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4856 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4857 EVEX VOP %ymmM, %ymmM, %ymmN
4858 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4859 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
1424ad86
JB
4860 VOP, one of kxord and kxorq:
4861 VEX VOP %kM, %kM, %kN
4862 -> VEX kxorw %kM, %kM, %kN
4863 VOP, one of kandnd and kandnq:
4864 VEX VOP %kM, %kM, %kN
4865 -> VEX kandnw %kM, %kM, %kN
b6f8c7c4 4866 */
706ce984 4867 if (i.tm.opcode_modifier.evex)
b6f8c7c4 4868 {
e346d50a 4869 if (i.encoding != encoding_evex)
b6f8c7c4
L
4870 {
4871 i.tm.opcode_modifier.vex = VEX128;
4872 i.tm.opcode_modifier.vexw = VEXW0;
4873 i.tm.opcode_modifier.evex = 0;
e346d50a 4874 i.encoding = encoding_vex;
a6f3add0 4875 i.mask.reg = NULL;
b6f8c7c4 4876 }
7b1d7ca1 4877 else if (optimize > 1)
dd22218c
L
4878 i.tm.opcode_modifier.evex = EVEX128;
4879 else
4880 return;
b6f8c7c4 4881 }
f74a6307 4882 else if (i.tm.operand_types[0].bitfield.class == RegMask)
1424ad86 4883 {
35648716 4884 i.tm.opcode_modifier.opcodeprefix = PREFIX_NONE;
1424ad86
JB
4885 i.tm.opcode_modifier.vexw = VEXW0;
4886 }
b6f8c7c4
L
4887 else
4888 i.tm.opcode_modifier.vex = VEX128;
4889
4890 if (i.tm.opcode_modifier.vex)
4891 for (j = 0; j < 3; j++)
4892 {
4893 i.types[j].bitfield.xmmword = 1;
4894 i.types[j].bitfield.ymmword = 0;
4895 }
4896 }
e346d50a 4897 else if (i.encoding != encoding_evex
eb3f3841 4898 && i.encoding != encoding_egpr
97ed31ae 4899 && !i.types[0].bitfield.zmmword
392a5972 4900 && !i.types[1].bitfield.zmmword
6225c532 4901 && !i.mask.reg
9b345ce8 4902 && !i.broadcast.type
a5748e0d 4903 && !i.broadcast.bytes
706ce984 4904 && i.tm.opcode_modifier.evex
35648716
JB
4905 && ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4906 || (i.tm.base_opcode & ~4) == 0xdb
4907 || (i.tm.base_opcode & ~4) == 0xeb)
97ed31ae
L
4908 && i.tm.extension_opcode == None)
4909 {
4910 /* Optimize: -O1:
4911 VOP, one of vmovdqa32, vmovdqa64, vmovdqu8, vmovdqu16,
4912 vmovdqu32 and vmovdqu64:
4913 EVEX VOP %xmmM, %xmmN
4914 -> VEX vmovdqa|vmovdqu %xmmM, %xmmN (M and N < 16)
4915 EVEX VOP %ymmM, %ymmN
4916 -> VEX vmovdqa|vmovdqu %ymmM, %ymmN (M and N < 16)
4917 EVEX VOP %xmmM, mem
4918 -> VEX vmovdqa|vmovdqu %xmmM, mem (M < 16)
4919 EVEX VOP %ymmM, mem
4920 -> VEX vmovdqa|vmovdqu %ymmM, mem (M < 16)
4921 EVEX VOP mem, %xmmN
4922 -> VEX mvmovdqa|vmovdquem, %xmmN (N < 16)
4923 EVEX VOP mem, %ymmN
4924 -> VEX vmovdqa|vmovdqu mem, %ymmN (N < 16)
a0a1771e
JB
4925 VOP, one of vpand, vpandn, vpor, vpxor:
4926 EVEX VOP{d,q} %xmmL, %xmmM, %xmmN
4927 -> VEX VOP %xmmL, %xmmM, %xmmN (L, M, and N < 16)
4928 EVEX VOP{d,q} %ymmL, %ymmM, %ymmN
4929 -> VEX VOP %ymmL, %ymmM, %ymmN (L, M, and N < 16)
4930 EVEX VOP{d,q} mem, %xmmM, %xmmN
4931 -> VEX VOP mem, %xmmM, %xmmN (M and N < 16)
4932 EVEX VOP{d,q} mem, %ymmM, %ymmN
4933 -> VEX VOP mem, %ymmM, %ymmN (M and N < 16)
97ed31ae 4934 */
a0a1771e 4935 for (j = 0; j < i.operands; j++)
392a5972
L
4936 if (operand_type_check (i.types[j], disp)
4937 && i.op[j].disps->X_op == O_constant)
4938 {
4939 /* Since the VEX prefix has 2 or 3 bytes, the EVEX prefix
4940 has 4 bytes, EVEX Disp8 has 1 byte and VEX Disp32 has 4
4941 bytes, we choose EVEX Disp8 over VEX Disp32. */
4942 int evex_disp8, vex_disp8;
4943 unsigned int memshift = i.memshift;
4944 offsetT n = i.op[j].disps->X_add_number;
4945
4946 evex_disp8 = fits_in_disp8 (n);
4947 i.memshift = 0;
4948 vex_disp8 = fits_in_disp8 (n);
4949 if (evex_disp8 != vex_disp8)
4950 {
4951 i.memshift = memshift;
4952 return;
4953 }
4954
4955 i.types[j].bitfield.disp8 = vex_disp8;
4956 break;
4957 }
35648716
JB
4958 if ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4959 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2)
4960 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
97ed31ae
L
4961 i.tm.opcode_modifier.vex
4962 = i.types[0].bitfield.ymmword ? VEX256 : VEX128;
4963 i.tm.opcode_modifier.vexw = VEXW0;
79dec6b7 4964 /* VPAND, VPOR, and VPXOR are commutative. */
35648716 4965 if (i.reg_operands == 3 && i.tm.base_opcode != 0xdf)
79dec6b7 4966 i.tm.opcode_modifier.commutative = 1;
97ed31ae
L
4967 i.tm.opcode_modifier.evex = 0;
4968 i.tm.opcode_modifier.masking = 0;
a0a1771e 4969 i.tm.opcode_modifier.broadcast = 0;
97ed31ae
L
4970 i.tm.opcode_modifier.disp8memshift = 0;
4971 i.memshift = 0;
a0a1771e
JB
4972 if (j < i.operands)
4973 i.types[j].bitfield.disp8
4974 = fits_in_disp8 (i.op[j].disps->X_add_number);
97ed31ae 4975 }
b5c37946
SJ
4976 else if (optimize_for_space
4977 && i.tm.base_opcode == 0x29
4978 && i.tm.opcode_space == SPACE_0F38
4979 && i.operands == i.reg_operands
4980 && i.op[0].regs == i.op[1].regs
4981 && (!i.tm.opcode_modifier.vex
4982 || !(i.op[0].regs->reg_flags & RegRex))
706ce984 4983 && !i.tm.opcode_modifier.evex)
b5c37946
SJ
4984 {
4985 /* Optimize: -Os:
4986 pcmpeqq %xmmN, %xmmN -> pcmpeqd %xmmN, %xmmN
4987 vpcmpeqq %xmmN, %xmmN, %xmmM -> vpcmpeqd %xmmN, %xmmN, %xmmM (N < 8)
4988 vpcmpeqq %ymmN, %ymmN, %ymmM -> vpcmpeqd %ymmN, %ymmN, %ymmM (N < 8)
4989 */
4990 i.tm.opcode_space = SPACE_0F;
4991 i.tm.base_opcode = 0x76;
4992 }
4993 else if (((i.tm.base_opcode >= 0x64
4994 && i.tm.base_opcode <= 0x66
4995 && i.tm.opcode_space == SPACE_0F)
4996 || (i.tm.base_opcode == 0x37
4997 && i.tm.opcode_space == SPACE_0F38))
4998 && i.operands == i.reg_operands
4999 && i.op[0].regs == i.op[1].regs
706ce984 5000 && !i.tm.opcode_modifier.evex)
b5c37946
SJ
5001 {
5002 /* Optimize: -O:
5003 pcmpgt[bwd] %mmN, %mmN -> pxor %mmN, %mmN
5004 pcmpgt[bwdq] %xmmN, %xmmN -> pxor %xmmN, %xmmN
5005 vpcmpgt[bwdq] %xmmN, %xmmN, %xmmM -> vpxor %xmmN, %xmmN, %xmmM (N < 8)
5006 vpcmpgt[bwdq] %xmmN, %xmmN, %xmmM -> vpxor %xmm0, %xmm0, %xmmM (N > 7)
5007 vpcmpgt[bwdq] %ymmN, %ymmN, %ymmM -> vpxor %ymmN, %ymmN, %ymmM (N < 8)
5008 vpcmpgt[bwdq] %ymmN, %ymmN, %ymmM -> vpxor %ymm0, %ymm0, %ymmM (N > 7)
5009 */
5010 i.tm.opcode_space = SPACE_0F;
5011 i.tm.base_opcode = 0xef;
5012 if (i.tm.opcode_modifier.vex && (i.op[0].regs->reg_flags & RegRex))
5013 {
5014 if (i.operands == 2)
5015 {
5016 gas_assert (i.tm.opcode_modifier.sse2avx);
5017
5018 i.operands = 3;
5019 i.reg_operands = 3;
5020 i.tm.operands = 3;
5021
5022 i.op[2].regs = i.op[0].regs;
5023 i.types[2] = i.types[0];
5024 i.flags[2] = i.flags[0];
5025 i.tm.operand_types[2] = i.tm.operand_types[0];
5026
5027 i.tm.opcode_modifier.sse2avx = 0;
5028 }
5029 i.op[0].regs -= i.op[0].regs->reg_num + 8;
5030 i.op[1].regs = i.op[0].regs;
5031 }
5032 }
5033 else if (optimize_for_space
5034 && i.tm.base_opcode == 0x59
5035 && i.tm.opcode_space == SPACE_0F38
5036 && i.operands == i.reg_operands
5037 && i.tm.opcode_modifier.vex
5038 && !(i.op[0].regs->reg_flags & RegRex)
5039 && i.op[0].regs->reg_type.bitfield.xmmword
e346d50a 5040 && i.encoding != encoding_vex3)
b5c37946
SJ
5041 {
5042 /* Optimize: -Os:
5043 vpbroadcastq %xmmN, %xmmM -> vpunpcklqdq %xmmN, %xmmN, %xmmM (N < 8)
5044 */
5045 i.tm.opcode_space = SPACE_0F;
5046 i.tm.base_opcode = 0x6c;
f2a3a881 5047 i.tm.opcode_modifier.vexvvvv = VexVVVV_SRC1;
b5c37946
SJ
5048
5049 ++i.operands;
5050 ++i.reg_operands;
5051 ++i.tm.operands;
5052
5053 i.op[2].regs = i.op[0].regs;
5054 i.types[2] = i.types[0];
5055 i.flags[2] = i.flags[0];
5056 i.tm.operand_types[2] = i.tm.operand_types[0];
5057
5058 swap_2_operands (1, 2);
5059 }
b6f8c7c4
L
5060}
5061
1e7dd4a0
JB
5062static void
5063s_noopt (int dummy ATTRIBUTE_UNUSED)
5064{
5065 if (!is_it_end_of_statement ())
5066 as_warn (_("`.noopt' arguments ignored"));
5067
5068 optimize = 0;
5069 optimize_for_space = 0;
5070
5071 ignore_rest_of_line ();
5072}
5073
ae531041
L
5074/* Return non-zero for load instruction. */
5075
5076static int
5077load_insn_p (void)
5078{
5079 unsigned int dest;
5080 int any_vex_p = is_any_vex_encoding (&i.tm);
5081 unsigned int base_opcode = i.tm.base_opcode | 1;
5082
5083 if (!any_vex_p)
5084 {
ef07be45
CL
5085 /* Anysize insns: lea, invlpg, clflush, prefetch*, bndmk, bndcl, bndcu,
5086 bndcn, bndstx, bndldx, clflushopt, clwb, cldemote. */
255571cd 5087 if (i.tm.opcode_modifier.operandconstraint == ANY_SIZE)
ae531041
L
5088 return 0;
5089
389d00a5 5090 /* pop. */
6d86a545 5091 if (i.tm.mnem_off == MN_pop)
389d00a5
JB
5092 return 1;
5093 }
5094
ddb62495 5095 if (i.tm.opcode_space == SPACE_BASE)
389d00a5
JB
5096 {
5097 /* popf, popa. */
5098 if (i.tm.base_opcode == 0x9d
a09f656b 5099 || i.tm.base_opcode == 0x61)
ae531041
L
5100 return 1;
5101
5102 /* movs, cmps, lods, scas. */
5103 if ((i.tm.base_opcode | 0xb) == 0xaf)
5104 return 1;
5105
a09f656b 5106 /* outs, xlatb. */
5107 if (base_opcode == 0x6f
5108 || i.tm.base_opcode == 0xd7)
ae531041 5109 return 1;
a09f656b 5110 /* NB: For AMD-specific insns with implicit memory operands,
5111 they're intentionally not covered. */
ae531041
L
5112 }
5113
5114 /* No memory operand. */
5115 if (!i.mem_operands)
5116 return 0;
5117
5118 if (any_vex_p)
5119 {
7fc69528 5120 if (i.tm.mnem_off == MN_vldmxcsr)
ae531041
L
5121 return 1;
5122 }
ddb62495 5123 else if (i.tm.opcode_space == SPACE_BASE)
ae531041
L
5124 {
5125 /* test, not, neg, mul, imul, div, idiv. */
aa4c197d 5126 if (base_opcode == 0xf7 && i.tm.extension_opcode != 1)
ae531041
L
5127 return 1;
5128
5129 /* inc, dec. */
5130 if (base_opcode == 0xff && i.tm.extension_opcode <= 1)
5131 return 1;
5132
5133 /* add, or, adc, sbb, and, sub, xor, cmp. */
5134 if (i.tm.base_opcode >= 0x80 && i.tm.base_opcode <= 0x83)
5135 return 1;
5136
ae531041 5137 /* rol, ror, rcl, rcr, shl/sal, shr, sar. */
aa4c197d 5138 if ((base_opcode == 0xc1 || (base_opcode | 2) == 0xd3)
ae531041
L
5139 && i.tm.extension_opcode != 6)
5140 return 1;
5141
ae531041 5142 /* Check for x87 instructions. */
aa4c197d 5143 if ((base_opcode | 6) == 0xdf)
ae531041
L
5144 {
5145 /* Skip fst, fstp, fstenv, fstcw. */
5146 if (i.tm.base_opcode == 0xd9
5147 && (i.tm.extension_opcode == 2
5148 || i.tm.extension_opcode == 3
5149 || i.tm.extension_opcode == 6
5150 || i.tm.extension_opcode == 7))
5151 return 0;
5152
5153 /* Skip fisttp, fist, fistp, fstp. */
5154 if (i.tm.base_opcode == 0xdb
5155 && (i.tm.extension_opcode == 1
5156 || i.tm.extension_opcode == 2
5157 || i.tm.extension_opcode == 3
5158 || i.tm.extension_opcode == 7))
5159 return 0;
5160
5161 /* Skip fisttp, fst, fstp, fsave, fstsw. */
5162 if (i.tm.base_opcode == 0xdd
5163 && (i.tm.extension_opcode == 1
5164 || i.tm.extension_opcode == 2
5165 || i.tm.extension_opcode == 3
5166 || i.tm.extension_opcode == 6
5167 || i.tm.extension_opcode == 7))
5168 return 0;
5169
5170 /* Skip fisttp, fist, fistp, fbstp, fistp. */
5171 if (i.tm.base_opcode == 0xdf
5172 && (i.tm.extension_opcode == 1
5173 || i.tm.extension_opcode == 2
5174 || i.tm.extension_opcode == 3
5175 || i.tm.extension_opcode == 6
5176 || i.tm.extension_opcode == 7))
5177 return 0;
5178
5179 return 1;
5180 }
5181 }
ddb62495 5182 else if (i.tm.opcode_space == SPACE_0F)
389d00a5
JB
5183 {
5184 /* bt, bts, btr, btc. */
5185 if (i.tm.base_opcode == 0xba
aa4c197d 5186 && (i.tm.extension_opcode | 3) == 7)
389d00a5
JB
5187 return 1;
5188
5189 /* cmpxchg8b, cmpxchg16b, xrstors, vmptrld. */
5190 if (i.tm.base_opcode == 0xc7
5191 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
5192 && (i.tm.extension_opcode == 1 || i.tm.extension_opcode == 3
5193 || i.tm.extension_opcode == 6))
5194 return 1;
5195
5196 /* fxrstor, ldmxcsr, xrstor. */
5197 if (i.tm.base_opcode == 0xae
5198 && (i.tm.extension_opcode == 1
5199 || i.tm.extension_opcode == 2
5200 || i.tm.extension_opcode == 5))
5201 return 1;
5202
5203 /* lgdt, lidt, lmsw. */
5204 if (i.tm.base_opcode == 0x01
5205 && (i.tm.extension_opcode == 2
5206 || i.tm.extension_opcode == 3
5207 || i.tm.extension_opcode == 6))
5208 return 1;
5209 }
ae531041
L
5210
5211 dest = i.operands - 1;
5212
5213 /* Check fake imm8 operand and 3 source operands. */
5214 if ((i.tm.opcode_modifier.immext
aa180741 5215 || i.reg_operands + i.mem_operands == 4)
ae531041
L
5216 && i.types[dest].bitfield.imm8)
5217 dest--;
5218
389d00a5 5219 /* add, or, adc, sbb, and, sub, xor, cmp, test, xchg. */
ddb62495 5220 if (i.tm.opcode_space == SPACE_BASE
aa4c197d 5221 && ((base_opcode | 0x38) == 0x39
389d00a5
JB
5222 || (base_opcode | 2) == 0x87))
5223 return 1;
5224
7fc69528 5225 if (i.tm.mnem_off == MN_xadd)
ae531041
L
5226 return 1;
5227
5228 /* Check for load instruction. */
5229 return (i.types[dest].bitfield.class != ClassNone
5230 || i.types[dest].bitfield.instance == Accum);
5231}
5232
5233/* Output lfence, 0xfaee8, after instruction. */
5234
5235static void
5236insert_lfence_after (void)
5237{
5238 if (lfence_after_load && load_insn_p ())
5239 {
a09f656b 5240 /* There are also two REP string instructions that require
5241 special treatment. Specifically, the compare string (CMPS)
5242 and scan string (SCAS) instructions set EFLAGS in a manner
5243 that depends on the data being compared/scanned. When used
5244 with a REP prefix, the number of iterations may therefore
5245 vary depending on this data. If the data is a program secret
5246 chosen by the adversary using an LVI method,
5247 then this data-dependent behavior may leak some aspect
5248 of the secret. */
aa4c197d 5249 if (((i.tm.base_opcode | 0x9) == 0xaf)
a09f656b 5250 && i.prefix[REP_PREFIX])
5251 {
5252 as_warn (_("`%s` changes flags which would affect control flow behavior"),
76d3f746 5253 insn_name (&i.tm));
a09f656b 5254 }
ae531041
L
5255 char *p = frag_more (3);
5256 *p++ = 0xf;
5257 *p++ = 0xae;
5258 *p = 0xe8;
5259 }
5260}
5261
5262/* Output lfence, 0xfaee8, before instruction. */
5263
5264static void
b5482fe5 5265insert_lfence_before (const struct last_insn *last_insn)
ae531041
L
5266{
5267 char *p;
5268
ddb62495 5269 if (i.tm.opcode_space != SPACE_BASE)
ae531041
L
5270 return;
5271
5272 if (i.tm.base_opcode == 0xff
5273 && (i.tm.extension_opcode == 2 || i.tm.extension_opcode == 4))
5274 {
5275 /* Insert lfence before indirect branch if needed. */
5276
5277 if (lfence_before_indirect_branch == lfence_branch_none)
5278 return;
5279
5280 if (i.operands != 1)
5281 abort ();
5282
5283 if (i.reg_operands == 1)
5284 {
5285 /* Indirect branch via register. Don't insert lfence with
5286 -mlfence-after-load=yes. */
5287 if (lfence_after_load
5288 || lfence_before_indirect_branch == lfence_branch_memory)
5289 return;
5290 }
5291 else if (i.mem_operands == 1
5292 && lfence_before_indirect_branch != lfence_branch_register)
5293 {
5294 as_warn (_("indirect `%s` with memory operand should be avoided"),
76d3f746 5295 insn_name (&i.tm));
ae531041
L
5296 return;
5297 }
5298 else
5299 return;
5300
b5482fe5 5301 if (last_insn->kind != last_insn_other)
ae531041 5302 {
b5482fe5 5303 as_warn_where (last_insn->file, last_insn->line,
ae531041 5304 _("`%s` skips -mlfence-before-indirect-branch on `%s`"),
b5482fe5 5305 last_insn->name, insn_name (&i.tm));
ae531041
L
5306 return;
5307 }
5308
5309 p = frag_more (3);
5310 *p++ = 0xf;
5311 *p++ = 0xae;
5312 *p = 0xe8;
5313 return;
5314 }
5315
503648e4 5316 /* Output or/not/shl and lfence before near ret. */
ae531041 5317 if (lfence_before_ret != lfence_before_ret_none
aa4c197d 5318 && (i.tm.base_opcode | 1) == 0xc3)
ae531041 5319 {
b5482fe5 5320 if (last_insn->kind != last_insn_other)
ae531041 5321 {
b5482fe5 5322 as_warn_where (last_insn->file, last_insn->line,
ae531041 5323 _("`%s` skips -mlfence-before-ret on `%s`"),
b5482fe5 5324 last_insn->name, insn_name (&i.tm));
ae531041
L
5325 return;
5326 }
a09f656b 5327
a09f656b 5328 /* Near ret ingore operand size override under CPU64. */
503648e4 5329 char prefix = flag_code == CODE_64BIT
5330 ? 0x48
5331 : i.prefix[DATA_PREFIX] ? 0x66 : 0x0;
a09f656b 5332
5333 if (lfence_before_ret == lfence_before_ret_not)
5334 {
5335 /* not: 0xf71424, may add prefix
5336 for operand size override or 64-bit code. */
5337 p = frag_more ((prefix ? 2 : 0) + 6 + 3);
5338 if (prefix)
5339 *p++ = prefix;
ae531041
L
5340 *p++ = 0xf7;
5341 *p++ = 0x14;
5342 *p++ = 0x24;
a09f656b 5343 if (prefix)
5344 *p++ = prefix;
ae531041
L
5345 *p++ = 0xf7;
5346 *p++ = 0x14;
5347 *p++ = 0x24;
5348 }
a09f656b 5349 else
5350 {
5351 p = frag_more ((prefix ? 1 : 0) + 4 + 3);
5352 if (prefix)
5353 *p++ = prefix;
5354 if (lfence_before_ret == lfence_before_ret_or)
5355 {
5356 /* or: 0x830c2400, may add prefix
5357 for operand size override or 64-bit code. */
5358 *p++ = 0x83;
5359 *p++ = 0x0c;
5360 }
5361 else
5362 {
5363 /* shl: 0xc1242400, may add prefix
5364 for operand size override or 64-bit code. */
5365 *p++ = 0xc1;
5366 *p++ = 0x24;
5367 }
5368
5369 *p++ = 0x24;
5370 *p++ = 0x0;
5371 }
5372
ae531041
L
5373 *p++ = 0xf;
5374 *p++ = 0xae;
5375 *p = 0xe8;
5376 }
5377}
5378
edd67638
JB
5379/* Shared helper for md_assemble() and s_insn(). */
5380static void init_globals (void)
5381{
5382 unsigned int j;
5383
5384 memset (&i, '\0', sizeof (i));
5385 i.rounding.type = rc_none;
5386 for (j = 0; j < MAX_OPERANDS; j++)
5387 i.reloc[j] = NO_RELOC;
5388 memset (disp_expressions, '\0', sizeof (disp_expressions));
5389 memset (im_expressions, '\0', sizeof (im_expressions));
5390 save_stack_p = save_stack;
5391}
5392
04784e33
JB
5393/* Helper for md_assemble() to decide whether to prepare for a possible 2nd
5394 parsing pass. Instead of introducing a rarely use new insn attribute this
5395 utilizes a common pattern between affected templates. It is deemed
5396 acceptable that this will lead to unnecessary pass 2 preparations in a
5397 limited set of cases. */
5398static INLINE bool may_need_pass2 (const insn_template *t)
5399{
5400 return t->opcode_modifier.sse2avx
5401 /* Note that all SSE2AVX templates have at least one operand. */
a28fedbc 5402 ? t->operand_types[t->operands - 1].bitfield.class == RegSIMD
ddb62495 5403 : (t->opcode_space == SPACE_0F
a28fedbc 5404 && (t->base_opcode | 1) == 0xbf)
ddb62495 5405 || (t->opcode_space == SPACE_BASE
a28fedbc 5406 && t->base_opcode == 0x63);
04784e33
JB
5407}
5408
c7defc53
IB
5409#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
5410
5411/* DWARF register number for EFLAGS. Used for pushf/popf insns. */
5412#define GINSN_DW2_REGNUM_EFLAGS 49
5413/* DWARF register number for RSI. Used as dummy value when RegIP/RegIZ. */
5414#define GINSN_DW2_REGNUM_RSI_DUMMY 4
5415
5416/* Identify the callee-saved registers in System V AMD64 ABI. */
5417
5418bool
5419x86_scfi_callee_saved_p (unsigned int dw2reg_num)
5420{
5421 if (dw2reg_num == 3 /* rbx. */
5422 || dw2reg_num == REG_FP /* rbp. */
5423 || dw2reg_num == REG_SP /* rsp. */
5424 || (dw2reg_num >= 12 && dw2reg_num <= 15) /* r12 - r15. */)
5425 return true;
5426
5427 return false;
5428}
5429
5430/* Check whether an instruction prefix which affects operation size
5431 accompanies. For insns in the legacy space, setting REX.W takes precedence
5432 over the operand-size prefix (66H) when both are used.
5433
5434 The current users of this API are in the handlers for PUSH, POP or other
5435 instructions which affect the stack pointer implicitly: the operation size
5436 (16, 32, or 64 bits) determines the amount by which the stack pointer is
5437 incremented / decremented (2, 4 or 8). */
5438
5439static bool
5440ginsn_opsize_prefix_p (void)
5441{
5442 return (!(i.prefix[REX_PREFIX] & REX_W) && i.prefix[DATA_PREFIX]);
5443}
5444
5445/* Get the DWARF register number for the given register entry.
5446 For specific byte/word/dword register accesses like al, cl, ah, ch, r8d,
5447 r20w etc., we need to identify the DWARF register number for the
5448 corresponding 8-byte GPR.
5449
5450 This function is a hack - it relies on relative ordering of reg entries in
5451 the i386_regtab. FIXME - it will be good to allow a more direct way to get
5452 this information. */
5453
5454static unsigned int
5455ginsn_dw2_regnum (const reg_entry *ireg)
5456{
c7defc53 5457 const reg_entry *temp = ireg;
066673f6 5458 unsigned int dwarf_reg = Dw2Inval, idx = 0;
c7defc53
IB
5459
5460 /* ginsn creation is available for AMD64 abi only ATM. Other flag_code
5461 are not expected. */
5462 gas_assert (ireg && flag_code == CODE_64BIT);
5463
5464 /* Watch out for RegIP, RegIZ. These are expected to appear only with
5465 base/index addressing modes. Although creating inaccurate data
5466 dependencies, using a dummy value (lets say volatile register rsi) will
5467 not hurt SCFI. TBD_GINSN_GEN_NOT_SCFI. */
5468 if (ireg->reg_num == RegIP || ireg->reg_num == RegIZ)
5469 return GINSN_DW2_REGNUM_RSI_DUMMY;
5470
fabb73d1 5471 dwarf_reg = ireg->dw2_regnum[object_64bit];
c7defc53
IB
5472
5473 if (dwarf_reg == Dw2Inval)
5474 {
5475 if (ireg <= &i386_regtab[3])
5476 /* For al, cl, dl, bl, bump over to axl, cxl, dxl, bxl respectively by
5477 adding 8. */
5478 temp = ireg + 8;
5479 else if (ireg <= &i386_regtab[7])
5480 /* For ah, ch, dh, bh, bump over to axl, cxl, dxl, bxl respectively by
5481 adding 4. */
5482 temp = ireg + 4;
5483 else
5484 {
5485 /* The code relies on the relative ordering of the reg entries in
5486 i386_regtab. There are 32 register entries between axl-r31b,
5487 ax-r31w etc. The assertions here ensures the code does not
5488 recurse indefinitely. */
5489 gas_assert ((temp - &i386_regtab[0]) >= 0);
5490 idx = temp - &i386_regtab[0];
5491 gas_assert (idx + 32 < i386_regtab_size - 1);
5492
5493 temp = temp + 32;
5494 }
5495
5496 dwarf_reg = ginsn_dw2_regnum (temp);
5497 }
5498
5499 /* Sanity check - failure may indicate state corruption, bad ginsn or
5500 perhaps the i386-reg table and the current function got out of sync. */
066673f6 5501 gas_assert (dwarf_reg < Dw2Inval);
c7defc53 5502
066673f6 5503 return dwarf_reg;
c7defc53
IB
5504}
5505
5506static ginsnS *
5507x86_ginsn_addsub_reg_mem (const symbolS *insn_end_sym)
5508{
5509 unsigned int dw2_regnum;
5510 unsigned int src1_dw2_regnum;
5511 ginsnS *ginsn = NULL;
5512 ginsnS * (*ginsn_func) (const symbolS *, bool,
5513 enum ginsn_src_type, unsigned int, offsetT,
5514 enum ginsn_src_type, unsigned int, offsetT,
5515 enum ginsn_dst_type, unsigned int, offsetT);
5516 uint16_t opcode = i.tm.base_opcode;
5517
5518 gas_assert (i.tm.opcode_space == SPACE_BASE
5519 && (opcode == 0x1 || opcode == 0x29));
5520 ginsn_func = (opcode == 0x1) ? ginsn_new_add : ginsn_new_sub;
5521
5522 /* op %reg, symbol or even other cases where destination involves indirect
5523 access are unnecessary for SCFI correctness. TBD_GINSN_GEN_NOT_SCFI. */
5524 if (i.mem_operands)
5525 return ginsn;
5526
f8c50ae2
IB
5527 /* Skip detection of 8/16/32-bit op size; 'add/sub reg, reg/mem' ops always
5528 make the dest reg untraceable for SCFI. */
5529
c7defc53
IB
5530 /* op reg, reg/mem. */
5531 src1_dw2_regnum = ginsn_dw2_regnum (i.op[0].regs);
5532 /* Of interest only when second opnd is not memory. */
5533 if (i.reg_operands == 2)
5534 {
5535 dw2_regnum = ginsn_dw2_regnum (i.op[1].regs);
5536 ginsn = ginsn_func (insn_end_sym, true,
5537 GINSN_SRC_REG, src1_dw2_regnum, 0,
5538 GINSN_SRC_REG, dw2_regnum, 0,
5539 GINSN_DST_REG, dw2_regnum, 0);
5540 ginsn_set_where (ginsn);
5541 }
5542
5543 return ginsn;
5544}
5545
5546static ginsnS *
5547x86_ginsn_addsub_mem_reg (const symbolS *insn_end_sym)
5548{
5549 unsigned int dw2_regnum;
5550 unsigned int src1_dw2_regnum;
5551 const reg_entry *mem_reg;
5552 int32_t gdisp = 0;
5553 ginsnS *ginsn = NULL;
5554 ginsnS * (*ginsn_func) (const symbolS *, bool,
5555 enum ginsn_src_type, unsigned int, offsetT,
5556 enum ginsn_src_type, unsigned int, offsetT,
5557 enum ginsn_dst_type, unsigned int, offsetT);
5558 uint16_t opcode = i.tm.base_opcode;
5559
5560 gas_assert (i.tm.opcode_space == SPACE_BASE
5561 && (opcode == 0x3 || opcode == 0x2b));
5562 ginsn_func = (opcode == 0x3) ? ginsn_new_add : ginsn_new_sub;
5563
5564 /* op symbol, %reg. */
5565 if (i.mem_operands && !i.base_reg && !i.index_reg)
5566 return ginsn;
5567
f8c50ae2
IB
5568 /* Skip detection of 8/16/32-bit op size; 'add/sub reg/mem, reg' ops always
5569 make the dest reg untraceable for SCFI. */
5570
c7defc53
IB
5571 /* op reg/mem, %reg. */
5572 dw2_regnum = ginsn_dw2_regnum (i.op[1].regs);
5573
5574 if (i.reg_operands == 2)
5575 {
5576 src1_dw2_regnum = ginsn_dw2_regnum (i.op[0].regs);
5577 ginsn = ginsn_func (insn_end_sym, true,
5578 GINSN_SRC_REG, src1_dw2_regnum, 0,
5579 GINSN_SRC_REG, dw2_regnum, 0,
5580 GINSN_DST_REG, dw2_regnum, 0);
5581 ginsn_set_where (ginsn);
5582 }
5583 else if (i.mem_operands)
5584 {
5585 mem_reg = (i.base_reg) ? i.base_reg : i.index_reg;
5586 src1_dw2_regnum = ginsn_dw2_regnum (mem_reg);
5587 if (i.disp_operands == 1)
5588 gdisp = i.op[0].disps->X_add_number;
5589 ginsn = ginsn_func (insn_end_sym, true,
5590 GINSN_SRC_INDIRECT, src1_dw2_regnum, gdisp,
5591 GINSN_SRC_REG, dw2_regnum, 0,
5592 GINSN_DST_REG, dw2_regnum, 0);
5593 ginsn_set_where (ginsn);
5594 }
5595
5596 return ginsn;
5597}
5598
5599static ginsnS *
5600x86_ginsn_alu_imm (const symbolS *insn_end_sym)
5601{
5602 offsetT src_imm;
5603 unsigned int dw2_regnum;
5604 ginsnS *ginsn = NULL;
5605 enum ginsn_src_type src_type = GINSN_SRC_REG;
5606 enum ginsn_dst_type dst_type = GINSN_DST_REG;
5607
5608 ginsnS * (*ginsn_func) (const symbolS *, bool,
5609 enum ginsn_src_type, unsigned int, offsetT,
5610 enum ginsn_src_type, unsigned int, offsetT,
5611 enum ginsn_dst_type, unsigned int, offsetT);
5612
5613 /* FIXME - create ginsn where dest is REG_SP / REG_FP only ? */
5614 /* Map for insn.tm.extension_opcode
5615 000 ADD 100 AND
5616 001 OR 101 SUB
5617 010 ADC 110 XOR
5618 011 SBB 111 CMP */
5619
5620 /* add/sub/and imm, %reg only at this time for SCFI.
5621 Although all three ('and', 'or' , 'xor') make the destination reg
5622 untraceable, 'and' op is handled but not 'or' / 'xor' because we will look
5623 into supporting the DRAP pattern at some point. Other opcodes ('adc',
5624 'sbb' and 'cmp') are not generated here either. The ginsn representation
5625 does not have support for the latter three opcodes; GINSN_TYPE_OTHER may
5626 be added for these after x86_ginsn_unhandled () invocation if the
5627 destination register is REG_SP or REG_FP. */
5628 if (i.tm.extension_opcode == 5)
5629 ginsn_func = ginsn_new_sub;
5630 else if (i.tm.extension_opcode == 4)
5631 ginsn_func = ginsn_new_and;
5632 else if (i.tm.extension_opcode == 0)
5633 ginsn_func = ginsn_new_add;
5634 else
5635 return ginsn;
5636
5637 /* TBD_GINSN_REPRESENTATION_LIMIT: There is no representation for when a
5638 symbol is used as an operand, like so:
5639 addq $simd_cmp_op+8, %rdx
5640 Skip generating any ginsn for this. */
5641 if (i.imm_operands == 1
5642 && i.op[0].imms->X_op != O_constant)
5643 return ginsn;
5644
5645 /* addq $1, symbol
5646 addq $1, -16(%rbp)
5647 These are not of interest for SCFI. Also, TBD_GINSN_GEN_NOT_SCFI. */
5648 if (i.mem_operands == 1)
5649 return ginsn;
5650
f8c50ae2
IB
5651 /* 8/16/32-bit op size makes the destination reg untraceable for SCFI.
5652 Deal with this via the x86_ginsn_unhandled () code path. */
5653 if (i.suffix != QWORD_MNEM_SUFFIX)
5654 return ginsn;
5655
c7defc53
IB
5656 gas_assert (i.imm_operands == 1);
5657 src_imm = i.op[0].imms->X_add_number;
5658 /* The second operand may be a register or indirect access. For SCFI, only
5659 the case when the second opnd is a register is interesting. Revisit this
5660 if generating ginsns for a different gen mode TBD_GINSN_GEN_NOT_SCFI. */
5661 if (i.reg_operands == 1)
5662 {
5663 dw2_regnum = ginsn_dw2_regnum (i.op[1].regs);
5664 /* For ginsn, keep the imm as second src operand. */
5665 ginsn = ginsn_func (insn_end_sym, true,
5666 src_type, dw2_regnum, 0,
5667 GINSN_SRC_IMM, 0, src_imm,
5668 dst_type, dw2_regnum, 0);
5669
5670 ginsn_set_where (ginsn);
5671 }
5672
5673 return ginsn;
5674}
5675
5676/* Create ginsn(s) for MOV operations.
5677
5678 The generated ginsns corresponding to mov with indirect access to memory
5679 (src or dest) suffer with loss of information: when both index and base
5680 registers are at play, only base register gets conveyed in ginsn. Note
5681 this TBD_GINSN_GEN_NOT_SCFI. */
5682
5683static ginsnS *
5684x86_ginsn_move (const symbolS *insn_end_sym)
5685{
5686 ginsnS *ginsn = NULL;
5687 unsigned int dst_reg;
5688 unsigned int src_reg;
5689 offsetT src_disp = 0;
5690 offsetT dst_disp = 0;
5691 const reg_entry *dst = NULL;
5692 const reg_entry *src = NULL;
5693 uint16_t opcode = i.tm.base_opcode;
5694 enum ginsn_src_type src_type = GINSN_SRC_REG;
5695 enum ginsn_dst_type dst_type = GINSN_DST_REG;
5696
5697 /* mov %reg, symbol or mov symbol, %reg.
5698 Not of interest for SCFI. Also, TBD_GINSN_GEN_NOT_SCFI. */
5699 if (i.mem_operands == 1 && !i.base_reg && !i.index_reg)
5700 return ginsn;
5701
f8c50ae2
IB
5702 /* 8/16/32-bit op size makes the destination reg untraceable for SCFI.
5703 Handle mov reg, reg only. mov to or from a memory operand will make
5704 dest reg, when present, untraceable, irrespective of the op size. */
5705 if (i.reg_operands == 2 && i.suffix != QWORD_MNEM_SUFFIX)
5706 return ginsn;
5707
c7defc53
IB
5708 gas_assert (i.tm.opcode_space == SPACE_BASE);
5709 if (opcode == 0x8b || opcode == 0x8a)
5710 {
5711 /* mov disp(%reg), %reg. */
5712 if (i.mem_operands)
5713 {
5714 src = (i.base_reg) ? i.base_reg : i.index_reg;
5715 if (i.disp_operands == 1)
5716 src_disp = i.op[0].disps->X_add_number;
5717 src_type = GINSN_SRC_INDIRECT;
5718 }
5719 else
5720 src = i.op[0].regs;
5721
5722 dst = i.op[1].regs;
5723 }
5724 else if (opcode == 0x89 || opcode == 0x88)
5725 {
5726 /* mov %reg, disp(%reg). */
5727 src = i.op[0].regs;
5728 if (i.mem_operands)
5729 {
5730 dst = (i.base_reg) ? i.base_reg : i.index_reg;
5731 if (i.disp_operands == 1)
5732 dst_disp = i.op[1].disps->X_add_number;
5733 dst_type = GINSN_DST_INDIRECT;
5734 }
5735 else
5736 dst = i.op[1].regs;
5737 }
5738
5739 src_reg = ginsn_dw2_regnum (src);
5740 dst_reg = ginsn_dw2_regnum (dst);
5741
5742 ginsn = ginsn_new_mov (insn_end_sym, true,
5743 src_type, src_reg, src_disp,
5744 dst_type, dst_reg, dst_disp);
5745 ginsn_set_where (ginsn);
5746
5747 return ginsn;
5748}
5749
5750/* Generate appropriate ginsn for lea.
09812f08
IB
5751
5752 Unhandled sub-cases (marked with TBD_GINSN_GEN_NOT_SCFI) also suffer with
5753 some loss of information in the final ginsn chosen eventually (type
5754 GINSN_TYPE_OTHER). But this is fine for now for GINSN_GEN_SCFI generation
c7defc53
IB
5755 mode. */
5756
5757static ginsnS *
5758x86_ginsn_lea (const symbolS *insn_end_sym)
5759{
5760 offsetT src_disp = 0;
5761 ginsnS *ginsn = NULL;
09812f08
IB
5762 unsigned int src1_reg;
5763 const reg_entry *src1;
c7defc53
IB
5764 offsetT index_scale;
5765 unsigned int dst_reg;
09812f08 5766 bool index_regiz_p;
c7defc53 5767
03fa0c63 5768 if ((!i.base_reg) != (!i.index_reg || i.index_reg->reg_num == RegIZ))
c7defc53 5769 {
09812f08
IB
5770 /* lea disp(%base), %dst or lea disp(,%index,imm), %dst.
5771 Either index_reg or base_reg exists, but not both. Further, as per
5772 above, the case when just %index exists but is equal to RegIZ is
5773 excluded. If not excluded, a GINSN_TYPE_MOV of %rsi
5774 (GINSN_DW2_REGNUM_RSI_DUMMY) to %dst will be generated by this block.
5775 Such a mov ginsn is imprecise; so, exclude now and generate
5776 GINSN_TYPE_OTHER instead later via the x86_ginsn_unhandled ().
5777 Excluding other cases is required due to
5778 TBD_GINSN_REPRESENTATION_LIMIT. */
c7defc53 5779
c7defc53 5780 index_scale = i.log2_scale_factor;
09812f08
IB
5781 index_regiz_p = i.index_reg && i.index_reg->reg_num == RegIZ;
5782 src1 = i.base_reg ? i.base_reg : i.index_reg;
5783 src1_reg = ginsn_dw2_regnum (src1);
c7defc53 5784 dst_reg = ginsn_dw2_regnum (i.op[1].regs);
09812f08
IB
5785 /* It makes sense to represent a scale factor of 1 precisely here
5786 (i.e., not using GINSN_TYPE_OTHER, but rather similar to the
5787 base-without-index case). A non-zero scale factor is still OK if
5788 the index reg is zero reg.
5789 However, skip from here the case when disp has a symbol instead.
5790 TBD_GINSN_REPRESENTATION_LIMIT. */
5791 if ((!index_scale || index_regiz_p)
5792 && (!i.disp_operands || i.op[0].disps->X_op == O_constant))
5793 {
5794 if (i.disp_operands)
5795 src_disp = i.op[0].disps->X_add_number;
c7defc53 5796
09812f08
IB
5797 if (src_disp)
5798 /* Generate an ADD ginsn. */
5799 ginsn = ginsn_new_add (insn_end_sym, true,
5800 GINSN_SRC_REG, src1_reg, 0,
5801 GINSN_SRC_IMM, 0, src_disp,
5802 GINSN_DST_REG, dst_reg, 0);
5803 else
5804 /* Generate a MOV ginsn. */
5805 ginsn = ginsn_new_mov (insn_end_sym, true,
5806 GINSN_SRC_REG, src1_reg, 0,
5807 GINSN_DST_REG, dst_reg, 0);
5808
5809 ginsn_set_where (ginsn);
5810 }
5811 }
5812 /* Skip handling other cases here,
5813 - when (i.index_reg && i.base_reg) is true,
5814 e.g., lea disp(%base,%index,imm), %dst
5815 We do not have a ginsn representation for multiply.
5816 - or, when (!i.index_reg && !i.base_reg) is true,
5817 e.g., lea symbol, %dst
5818 Not a frequent pattern. If %dst is a register of interest, the user is
5819 likely to use a MOV op anyway.
5820 Deal with these via the x86_ginsn_unhandled () code path to generate
5821 GINSN_TYPE_OTHER when necessary. TBD_GINSN_GEN_NOT_SCFI. */
c7defc53
IB
5822
5823 return ginsn;
5824}
5825
5826static ginsnS *
5827x86_ginsn_jump (const symbolS *insn_end_sym, bool cond_p)
5828{
5829 ginsnS *ginsn = NULL;
5830 const symbolS *src_symbol;
5831 ginsnS * (*ginsn_func) (const symbolS *sym, bool real_p,
5832 enum ginsn_src_type src_type, unsigned int src_reg,
5833 const symbolS *src_ginsn_sym);
5834
5835 gas_assert (i.disp_operands == 1);
5836
5837 ginsn_func = cond_p ? ginsn_new_jump_cond : ginsn_new_jump;
5838 if (i.op[0].disps->X_op == O_symbol && !i.op[0].disps->X_add_number)
5839 {
5840 src_symbol = i.op[0].disps->X_add_symbol;
5841 ginsn = ginsn_func (insn_end_sym, true,
5842 GINSN_SRC_SYMBOL, 0, src_symbol);
5843
5844 ginsn_set_where (ginsn);
5845 }
5846 else
5847 {
5848 /* A non-zero addend in jump/JCC target makes control-flow tracking
5849 difficult. Skip SCFI for now. */
5850 as_bad (_("SCFI: `%s' insn with non-zero addend to sym not supported"),
5851 cond_p ? "JCC" : "jmp");
5852 return ginsn;
5853 }
5854
5855 return ginsn;
5856}
5857
5858static ginsnS *
5859x86_ginsn_enter (const symbolS *insn_end_sym)
5860{
5861 ginsnS *ginsn = NULL;
5862 ginsnS *ginsn_next = NULL;
5863 ginsnS *ginsn_last = NULL;
5864 /* In 64-bit mode, the default stack update size is 8 bytes. */
5865 int stack_opnd_size = 8;
5866
5867 gas_assert (i.imm_operands == 2);
5868
5869 /* For non-zero size operands, bail out as untraceable for SCFI. */
5870 if (i.op[0].imms->X_op != O_constant || i.op[0].imms->X_add_symbol != 0
5871 || i.op[1].imms->X_op != O_constant || i.op[1].imms->X_add_symbol != 0)
5872 {
5873 as_bad ("SCFI: enter insn with non-zero operand not supported");
5874 return ginsn;
5875 }
5876
5877 /* Check if this is a 16-bit op. */
5878 if (ginsn_opsize_prefix_p ())
5879 stack_opnd_size = 2;
5880
5881 /* If the nesting level is 0, the processor pushes the frame pointer from
5882 the BP/EBP/RBP register onto the stack, copies the current stack
5883 pointer from the SP/ESP/RSP register into the BP/EBP/RBP register, and
5884 loads the SP/ESP/RSP register with the current stack-pointer value
5885 minus the value in the size operand. */
5886 ginsn = ginsn_new_sub (insn_end_sym, false,
5887 GINSN_SRC_REG, REG_SP, 0,
5888 GINSN_SRC_IMM, 0, stack_opnd_size,
5889 GINSN_DST_REG, REG_SP, 0);
5890 ginsn_set_where (ginsn);
5891 ginsn_next = ginsn_new_store (insn_end_sym, false,
5892 GINSN_SRC_REG, REG_FP,
5893 GINSN_DST_INDIRECT, REG_SP, 0);
5894 ginsn_set_where (ginsn_next);
5895 gas_assert (!ginsn_link_next (ginsn, ginsn_next));
5896 ginsn_last = ginsn_new_mov (insn_end_sym, false,
5897 GINSN_SRC_REG, REG_SP, 0,
5898 GINSN_DST_REG, REG_FP, 0);
5899 ginsn_set_where (ginsn_last);
5900 gas_assert (!ginsn_link_next (ginsn_next, ginsn_last));
5901
5902 return ginsn;
5903}
5904
5905static ginsnS *
5906x86_ginsn_leave (const symbolS *insn_end_sym)
5907{
5908 ginsnS *ginsn = NULL;
5909 ginsnS *ginsn_next = NULL;
5910 ginsnS *ginsn_last = NULL;
5911 /* In 64-bit mode, the default stack update size is 8 bytes. */
5912 int stack_opnd_size = 8;
5913
5914 /* Check if this is a 16-bit op. */
5915 if (ginsn_opsize_prefix_p ())
5916 stack_opnd_size = 2;
5917
5918 /* The 'leave' instruction copies the contents of the RBP register
5919 into the RSP register to release all stack space allocated to the
5920 procedure. */
5921 ginsn = ginsn_new_mov (insn_end_sym, false,
5922 GINSN_SRC_REG, REG_FP, 0,
5923 GINSN_DST_REG, REG_SP, 0);
5924 ginsn_set_where (ginsn);
5925 /* Then it restores the old value of the RBP register from the stack. */
5926 ginsn_next = ginsn_new_load (insn_end_sym, false,
5927 GINSN_SRC_INDIRECT, REG_SP, 0,
5928 GINSN_DST_REG, REG_FP);
5929 ginsn_set_where (ginsn_next);
5930 gas_assert (!ginsn_link_next (ginsn, ginsn_next));
5931 ginsn_last = ginsn_new_add (insn_end_sym, false,
5932 GINSN_SRC_REG, REG_SP, 0,
5933 GINSN_SRC_IMM, 0, stack_opnd_size,
5934 GINSN_DST_REG, REG_SP, 0);
5935 ginsn_set_where (ginsn_next);
5936 gas_assert (!ginsn_link_next (ginsn_next, ginsn_last));
5937
5938 return ginsn;
5939}
5940
5941/* Check if an instruction is whitelisted.
5942
5943 Some instructions may appear with REG_SP or REG_FP as destination, because
5944 which they are deemed 'interesting' for SCFI. Whitelist them here if they
5945 do not affect SCFI correctness. */
5946
5947static bool
5948x86_ginsn_safe_to_skip_p (void)
5949{
5950 bool skip_p = false;
5951 uint16_t opcode = i.tm.base_opcode;
5952
5953 switch (opcode)
5954 {
5955 case 0x80:
5956 case 0x81:
5957 case 0x83:
5958 if (i.tm.opcode_space != SPACE_BASE)
5959 break;
5960 /* cmp imm, reg/rem. */
5961 if (i.tm.extension_opcode == 7)
5962 skip_p = true;
5963 break;
5964
5965 case 0x38:
5966 case 0x39:
5967 case 0x3a:
5968 case 0x3b:
5969 if (i.tm.opcode_space != SPACE_BASE)
5970 break;
5971 /* cmp imm/reg/mem, reg/rem. */
5972 skip_p = true;
5973 break;
5974
5975 case 0xf6:
5976 case 0xf7:
5977 case 0x84:
5978 case 0x85:
5979 /* test imm/reg/mem, reg/mem. */
5980 if (i.tm.opcode_space != SPACE_BASE)
5981 break;
5982 skip_p = true;
5983 break;
5984
5985 default:
5986 break;
5987 }
5988
5989 return skip_p;
5990}
5991
5992#define X86_GINSN_UNHANDLED_NONE 0
5993#define X86_GINSN_UNHANDLED_DEST_REG 1
5994#define X86_GINSN_UNHANDLED_CFG 2
5995#define X86_GINSN_UNHANDLED_STACKOP 3
5996#define X86_GINSN_UNHANDLED_UNEXPECTED 4
5997
5998/* Check the input insn for its impact on the correctness of the synthesized
5999 CFI. Returns an error code to the caller. */
6000
6001static int
6002x86_ginsn_unhandled (void)
6003{
6004 int err = X86_GINSN_UNHANDLED_NONE;
6005 const reg_entry *reg_op;
6006 unsigned int dw2_regnum;
6007
6008 /* Keep an eye out for instructions affecting control flow. */
6009 if (i.tm.opcode_modifier.jump)
6010 err = X86_GINSN_UNHANDLED_CFG;
6011 /* Also, for any instructions involving an implicit update to the stack
6012 pointer. */
6013 else if (i.tm.opcode_modifier.operandconstraint == IMPLICIT_STACK_OP)
6014 err = X86_GINSN_UNHANDLED_STACKOP;
6015 /* Finally, also check if the missed instructions are affecting REG_SP or
6016 REG_FP. The destination operand is the last at all stages of assembly
6017 (due to following AT&T syntax layout in the internal representation). In
6018 case of Intel syntax input, this still remains true as swap_operands ()
6019 is done by now.
6020 PS: These checks do not involve index / base reg, as indirect memory
6021 accesses via REG_SP or REG_FP do not affect SCFI correctness.
6022 (Also note these instructions are candidates for other ginsn generation
6023 modes in future. TBD_GINSN_GEN_NOT_SCFI.) */
6024 else if (i.operands && i.reg_operands
6025 && !(i.flags[i.operands - 1] & Operand_Mem))
6026 {
6027 reg_op = i.op[i.operands - 1].regs;
6028 if (reg_op)
6029 {
6030 dw2_regnum = ginsn_dw2_regnum (reg_op);
6031 if (dw2_regnum == REG_SP || dw2_regnum == REG_FP)
6032 err = X86_GINSN_UNHANDLED_DEST_REG;
6033 }
6034 else
6035 /* Something unexpected. Indicate to caller. */
6036 err = X86_GINSN_UNHANDLED_UNEXPECTED;
6037 }
6038
6039 return err;
6040}
6041
6042/* Generate one or more generic GAS instructions, a.k.a, ginsns for the current
6043 machine instruction.
6044
6045 Returns the head of linked list of ginsn(s) added, if success; Returns NULL
6046 if failure.
6047
6048 The input ginsn_gen_mode GMODE determines the set of minimal necessary
6049 ginsns necessary for correctness of any passes applicable for that mode.
6050 For supporting the GINSN_GEN_SCFI generation mode, following is the list of
6051 machine instructions that must be translated into the corresponding ginsns
6052 to ensure correctness of SCFI:
6053 - All instructions affecting the two registers that could potentially
6054 be used as the base register for CFA tracking. For SCFI, the base
6055 register for CFA tracking is limited to REG_SP and REG_FP only for
6056 now.
6057 - All change of flow instructions: conditional and unconditional branches,
6058 call and return from functions.
6059 - All instructions that can potentially be a register save / restore
6060 operation.
6061 - All instructions that perform stack manipulation implicitly: the CALL,
6062 RET, PUSH, POP, ENTER, and LEAVE instructions.
6063
6064 The function currently supports GINSN_GEN_SCFI ginsn generation mode only.
6065 To support other generation modes will require work on this target-specific
6066 process of creation of ginsns:
6067 - Some of such places are tagged with TBD_GINSN_GEN_NOT_SCFI to serve as
6068 possible starting points.
6069 - Also note that ginsn representation may need enhancements. Specifically,
6070 note some TBD_GINSN_INFO_LOSS and TBD_GINSN_REPRESENTATION_LIMIT markers.
6071 */
6072
6073static ginsnS *
6074x86_ginsn_new (const symbolS *insn_end_sym, enum ginsn_gen_mode gmode)
6075{
6076 int err = 0;
6077 uint16_t opcode;
6078 unsigned int dw2_regnum;
6079 const reg_entry *mem_reg;
6080 ginsnS *ginsn = NULL;
6081 ginsnS *ginsn_next = NULL;
6082 /* In 64-bit mode, the default stack update size is 8 bytes. */
6083 int stack_opnd_size = 8;
6084
6085 /* Currently supports generation of selected ginsns, sufficient for
6086 the use-case of SCFI only. */
6087 if (gmode != GINSN_GEN_SCFI)
6088 return ginsn;
6089
6090 opcode = i.tm.base_opcode;
6091
6092 /* Until it is clear how to handle APX NDD and other new opcodes, disallow
6093 them from SCFI. */
6094 if (is_apx_rex2_encoding ()
6095 || (i.tm.opcode_modifier.evex && is_apx_evex_encoding ()))
6096 {
6097 as_bad (_("SCFI: unsupported APX op %#x may cause incorrect CFI"),
6098 opcode);
6099 return ginsn;
6100 }
6101
6102 switch (opcode)
6103 {
f8c50ae2
IB
6104
6105 /* Add opcodes 0x0/0x2 and sub opcodes 0x28/0x2a (with opcode_space
6106 SPACE_BASE) are 8-bit ops. While they are relevant for SCFI
6107 correctness, skip handling them here and use the x86_ginsn_unhandled
6108 code path to generate GINSN_TYPE_OTHER when necessary. */
6109
c7defc53
IB
6110 case 0x1: /* add reg, reg/mem. */
6111 case 0x29: /* sub reg, reg/mem. */
6112 if (i.tm.opcode_space != SPACE_BASE)
6113 break;
6114 ginsn = x86_ginsn_addsub_reg_mem (insn_end_sym);
6115 break;
6116
6117 case 0x3: /* add reg/mem, reg. */
6118 case 0x2b: /* sub reg/mem, reg. */
6119 if (i.tm.opcode_space != SPACE_BASE)
6120 break;
6121 ginsn = x86_ginsn_addsub_mem_reg (insn_end_sym);
6122 break;
6123
6124 case 0xa0: /* push fs. */
6125 case 0xa8: /* push gs. */
6126 /* push fs / push gs have opcode_space == SPACE_0F. */
6127 if (i.tm.opcode_space != SPACE_0F)
6128 break;
6129 dw2_regnum = ginsn_dw2_regnum (i.op[0].regs);
6130 /* Check if operation size is 16-bit. */
6131 if (ginsn_opsize_prefix_p ())
6132 stack_opnd_size = 2;
6133 ginsn = ginsn_new_sub (insn_end_sym, false,
6134 GINSN_SRC_REG, REG_SP, 0,
6135 GINSN_SRC_IMM, 0, stack_opnd_size,
6136 GINSN_DST_REG, REG_SP, 0);
6137 ginsn_set_where (ginsn);
6138 ginsn_next = ginsn_new_store (insn_end_sym, false,
6139 GINSN_SRC_REG, dw2_regnum,
6140 GINSN_DST_INDIRECT, REG_SP, 0);
6141 ginsn_set_where (ginsn_next);
6142 gas_assert (!ginsn_link_next (ginsn, ginsn_next));
6143 break;
6144
6145 case 0xa1: /* pop fs. */
6146 case 0xa9: /* pop gs. */
6147 /* pop fs / pop gs have opcode_space == SPACE_0F. */
6148 if (i.tm.opcode_space != SPACE_0F)
6149 break;
6150 dw2_regnum = ginsn_dw2_regnum (i.op[0].regs);
6151 /* Check if operation size is 16-bit. */
6152 if (ginsn_opsize_prefix_p ())
6153 stack_opnd_size = 2;
6154 ginsn = ginsn_new_load (insn_end_sym, false,
6155 GINSN_SRC_INDIRECT, REG_SP, 0,
6156 GINSN_DST_REG, dw2_regnum);
6157 ginsn_set_where (ginsn);
6158 ginsn_next = ginsn_new_add (insn_end_sym, false,
6159 GINSN_SRC_REG, REG_SP, 0,
6160 GINSN_SRC_IMM, 0, stack_opnd_size,
6161 GINSN_DST_REG, REG_SP, 0);
6162 ginsn_set_where (ginsn_next);
6163 gas_assert (!ginsn_link_next (ginsn, ginsn_next));
6164 break;
6165
6166 case 0x50 ... 0x57:
6167 if (i.tm.opcode_space != SPACE_BASE)
6168 break;
6169 /* push reg. */
6170 dw2_regnum = ginsn_dw2_regnum (i.op[0].regs);
6171 /* Check if operation size is 16-bit. */
6172 if (ginsn_opsize_prefix_p ())
6173 stack_opnd_size = 2;
6174 ginsn = ginsn_new_sub (insn_end_sym, false,
6175 GINSN_SRC_REG, REG_SP, 0,
6176 GINSN_SRC_IMM, 0, stack_opnd_size,
6177 GINSN_DST_REG, REG_SP, 0);
6178 ginsn_set_where (ginsn);
6179 ginsn_next = ginsn_new_store (insn_end_sym, false,
6180 GINSN_SRC_REG, dw2_regnum,
6181 GINSN_DST_INDIRECT, REG_SP, 0);
6182 ginsn_set_where (ginsn_next);
6183 gas_assert (!ginsn_link_next (ginsn, ginsn_next));
6184 break;
6185
6186 case 0x58 ... 0x5f:
6187 if (i.tm.opcode_space != SPACE_BASE)
6188 break;
6189 /* pop reg. */
6190 dw2_regnum = ginsn_dw2_regnum (i.op[0].regs);
6191 ginsn = ginsn_new_load (insn_end_sym, false,
6192 GINSN_SRC_INDIRECT, REG_SP, 0,
6193 GINSN_DST_REG, dw2_regnum);
6194 ginsn_set_where (ginsn);
6195 /* Check if operation size is 16-bit. */
6196 if (ginsn_opsize_prefix_p ())
6197 stack_opnd_size = 2;
6198 ginsn_next = ginsn_new_add (insn_end_sym, false,
6199 GINSN_SRC_REG, REG_SP, 0,
6200 GINSN_SRC_IMM, 0, stack_opnd_size,
6201 GINSN_DST_REG, REG_SP, 0);
6202 ginsn_set_where (ginsn_next);
6203 gas_assert (!ginsn_link_next (ginsn, ginsn_next));
6204 break;
6205
6206 case 0x6a: /* push imm8. */
6207 case 0x68: /* push imm16/imm32. */
6208 if (i.tm.opcode_space != SPACE_BASE)
6209 break;
6210 /* Check if operation size is 16-bit. */
6211 if (ginsn_opsize_prefix_p ())
6212 stack_opnd_size = 2;
6213 /* Skip getting the value of imm from machine instruction
6214 because this is not important for SCFI. */
6215 ginsn = ginsn_new_sub (insn_end_sym, false,
6216 GINSN_SRC_REG, REG_SP, 0,
6217 GINSN_SRC_IMM, 0, stack_opnd_size,
6218 GINSN_DST_REG, REG_SP, 0);
6219 ginsn_set_where (ginsn);
6220 ginsn_next = ginsn_new_store (insn_end_sym, false,
6221 GINSN_SRC_IMM, 0,
6222 GINSN_DST_INDIRECT, REG_SP, 0);
6223 ginsn_set_where (ginsn_next);
6224 gas_assert (!ginsn_link_next (ginsn, ginsn_next));
6225 break;
6226
6227 /* PS: Opcodes 0x80 ... 0x8f with opcode_space SPACE_0F are present
6228 only after relaxation. They do not need to be handled for ginsn
6229 creation. */
6230 case 0x70 ... 0x7f:
6231 if (i.tm.opcode_space != SPACE_BASE)
6232 break;
6233 ginsn = x86_ginsn_jump (insn_end_sym, true);
6234 break;
6235
6236 case 0x80:
6237 case 0x81:
6238 case 0x83:
6239 if (i.tm.opcode_space != SPACE_BASE)
6240 break;
6241 ginsn = x86_ginsn_alu_imm (insn_end_sym);
6242 break;
6243
6244 case 0x8a: /* mov r/m8, r8. */
6245 case 0x8b: /* mov r/m(16/32/64), r(16/32/64). */
6246 case 0x88: /* mov r8, r/m8. */
6247 case 0x89: /* mov r(16/32/64), r/m(16/32/64). */
6248 if (i.tm.opcode_space != SPACE_BASE)
6249 break;
6250 ginsn = x86_ginsn_move (insn_end_sym);
6251 break;
6252
6253 case 0x8d:
6254 if (i.tm.opcode_space != SPACE_BASE)
6255 break;
09812f08 6256 /* lea disp(%base,%index,imm), %dst. */
c7defc53
IB
6257 ginsn = x86_ginsn_lea (insn_end_sym);
6258 break;
6259
6260 case 0x8f:
6261 if (i.tm.opcode_space != SPACE_BASE)
6262 break;
6263 /* pop to reg/mem. */
6264 if (i.mem_operands)
6265 {
6266 mem_reg = (i.base_reg) ? i.base_reg : i.index_reg;
6267 /* Use dummy register if no base or index. Unlike other opcodes,
6268 ginsns must be generated as this affect stack pointer. */
6269 dw2_regnum = (mem_reg
6270 ? ginsn_dw2_regnum (mem_reg)
6271 : GINSN_DW2_REGNUM_RSI_DUMMY);
6272 }
6273 else
6274 dw2_regnum = ginsn_dw2_regnum (i.op[0].regs);
6275 ginsn = ginsn_new_load (insn_end_sym, false,
6276 GINSN_SRC_INDIRECT, REG_SP, 0,
6277 GINSN_DST_INDIRECT, dw2_regnum);
6278 ginsn_set_where (ginsn);
6279 /* Check if operation size is 16-bit. */
6280 if (ginsn_opsize_prefix_p ())
6281 stack_opnd_size = 2;
6282 ginsn_next = ginsn_new_add (insn_end_sym, false,
6283 GINSN_SRC_REG, REG_SP, 0,
6284 GINSN_SRC_IMM, 0, stack_opnd_size,
6285 GINSN_DST_REG, REG_SP, 0);
6286 ginsn_set_where (ginsn_next);
6287 gas_assert (!ginsn_link_next (ginsn, ginsn_next));
6288 break;
6289
6290 case 0x9c:
6291 if (i.tm.opcode_space != SPACE_BASE)
6292 break;
6293 /* pushf / pushfq. */
6294 /* Check if operation size is 16-bit. */
6295 if (ginsn_opsize_prefix_p ())
6296 stack_opnd_size = 2;
6297 ginsn = ginsn_new_sub (insn_end_sym, false,
6298 GINSN_SRC_REG, REG_SP, 0,
6299 GINSN_SRC_IMM, 0, stack_opnd_size,
6300 GINSN_DST_REG, REG_SP, 0);
6301 ginsn_set_where (ginsn);
6302 /* FIXME - hardcode the actual DWARF reg number value. As for SCFI
6303 correctness, although this behaves simply a placeholder value; its
6304 just clearer if the value is correct. */
6305 dw2_regnum = GINSN_DW2_REGNUM_EFLAGS;
6306 ginsn_next = ginsn_new_store (insn_end_sym, false,
6307 GINSN_SRC_REG, dw2_regnum,
6308 GINSN_DST_INDIRECT, REG_SP, 0);
6309 ginsn_set_where (ginsn_next);
6310 gas_assert (!ginsn_link_next (ginsn, ginsn_next));
6311 break;
6312
6313 case 0x9d:
6314 if (i.tm.opcode_space != SPACE_BASE)
6315 break;
6316 /* popf / popfq. */
6317 /* Check if operation size is 16-bit. */
6318 if (ginsn_opsize_prefix_p ())
6319 stack_opnd_size = 2;
6320 /* FIXME - hardcode the actual DWARF reg number value. As for SCFI
6321 correctness, although this behaves simply a placeholder value; its
6322 just clearer if the value is correct. */
6323 dw2_regnum = GINSN_DW2_REGNUM_EFLAGS;
6324 ginsn = ginsn_new_load (insn_end_sym, false,
6325 GINSN_SRC_INDIRECT, REG_SP, 0,
6326 GINSN_DST_REG, dw2_regnum);
6327 ginsn_set_where (ginsn);
6328 ginsn_next = ginsn_new_add (insn_end_sym, false,
6329 GINSN_SRC_REG, REG_SP, 0,
6330 GINSN_SRC_IMM, 0, stack_opnd_size,
6331 GINSN_DST_REG, REG_SP, 0);
6332 ginsn_set_where (ginsn_next);
6333 gas_assert (!ginsn_link_next (ginsn, ginsn_next));
6334 break;
6335
6336 case 0xff:
6337 if (i.tm.opcode_space != SPACE_BASE)
6338 break;
6339 /* push from reg/mem. */
6340 if (i.tm.extension_opcode == 6)
6341 {
6342 /* Check if operation size is 16-bit. */
6343 if (ginsn_opsize_prefix_p ())
6344 stack_opnd_size = 2;
6345 ginsn = ginsn_new_sub (insn_end_sym, false,
6346 GINSN_SRC_REG, REG_SP, 0,
6347 GINSN_SRC_IMM, 0, stack_opnd_size,
6348 GINSN_DST_REG, REG_SP, 0);
6349 ginsn_set_where (ginsn);
6350 if (i.mem_operands)
6351 {
6352 mem_reg = (i.base_reg) ? i.base_reg : i.index_reg;
6353 /* Use dummy register if no base or index. Unlike other opcodes,
6354 ginsns must be generated as this affect stack pointer. */
6355 dw2_regnum = (mem_reg
6356 ? ginsn_dw2_regnum (mem_reg)
6357 : GINSN_DW2_REGNUM_RSI_DUMMY);
6358 }
6359 else
6360 dw2_regnum = ginsn_dw2_regnum (i.op[0].regs);
6361 ginsn_next = ginsn_new_store (insn_end_sym, false,
6362 GINSN_SRC_INDIRECT, dw2_regnum,
6363 GINSN_DST_INDIRECT, REG_SP, 0);
6364 ginsn_set_where (ginsn_next);
6365 gas_assert (!ginsn_link_next (ginsn, ginsn_next));
6366 }
6367 else if (i.tm.extension_opcode == 4)
6368 {
6369 /* jmp r/m. E.g., notrack jmp *%rax. */
6370 if (i.reg_operands)
6371 {
6372 dw2_regnum = ginsn_dw2_regnum (i.op[0].regs);
6373 ginsn = ginsn_new_jump (insn_end_sym, true,
6374 GINSN_SRC_REG, dw2_regnum, NULL);
6375 ginsn_set_where (ginsn);
6376 }
6377 else if (i.mem_operands && i.index_reg)
6378 {
6379 /* jmp *0x0(,%rax,8). */
6380 dw2_regnum = ginsn_dw2_regnum (i.index_reg);
6381 ginsn = ginsn_new_jump (insn_end_sym, true,
6382 GINSN_SRC_REG, dw2_regnum, NULL);
6383 ginsn_set_where (ginsn);
6384 }
6385 else if (i.mem_operands && i.base_reg)
6386 {
6387 dw2_regnum = ginsn_dw2_regnum (i.base_reg);
6388 ginsn = ginsn_new_jump (insn_end_sym, true,
6389 GINSN_SRC_REG, dw2_regnum, NULL);
6390 ginsn_set_where (ginsn);
6391 }
6392 }
6393 else if (i.tm.extension_opcode == 2)
6394 {
6395 /* 0xFF /2 (call). */
6396 if (i.reg_operands)
6397 {
6398 dw2_regnum = ginsn_dw2_regnum (i.op[0].regs);
6399 ginsn = ginsn_new_call (insn_end_sym, true,
6400 GINSN_SRC_REG, dw2_regnum, NULL);
6401 ginsn_set_where (ginsn);
6402 }
6403 else if (i.mem_operands && i.base_reg)
6404 {
6405 dw2_regnum = ginsn_dw2_regnum (i.base_reg);
6406 ginsn = ginsn_new_call (insn_end_sym, true,
6407 GINSN_SRC_REG, dw2_regnum, NULL);
6408 ginsn_set_where (ginsn);
6409 }
6410 }
6411 break;
6412
6413 case 0xc2: /* ret imm16. */
6414 case 0xc3: /* ret. */
6415 if (i.tm.opcode_space != SPACE_BASE)
6416 break;
6417 /* Near ret. */
6418 ginsn = ginsn_new_return (insn_end_sym, true);
6419 ginsn_set_where (ginsn);
6420 break;
6421
6422 case 0xc8:
6423 if (i.tm.opcode_space != SPACE_BASE)
6424 break;
6425 /* enter. */
6426 ginsn = x86_ginsn_enter (insn_end_sym);
6427 break;
6428
6429 case 0xc9:
6430 if (i.tm.opcode_space != SPACE_BASE)
6431 break;
6432 /* leave. */
6433 ginsn = x86_ginsn_leave (insn_end_sym);
6434 break;
6435
6436 case 0xe0 ... 0xe2: /* loop / loope / loopne. */
6437 case 0xe3: /* jecxz / jrcxz. */
6438 if (i.tm.opcode_space != SPACE_BASE)
6439 break;
6440 ginsn = x86_ginsn_jump (insn_end_sym, true);
6441 ginsn_set_where (ginsn);
6442 break;
6443
6444 case 0xe8:
6445 if (i.tm.opcode_space != SPACE_BASE)
6446 break;
6447 /* PS: SCFI machinery does not care about which func is being
6448 called. OK to skip that info. */
6449 ginsn = ginsn_new_call (insn_end_sym, true,
6450 GINSN_SRC_SYMBOL, 0, NULL);
6451 ginsn_set_where (ginsn);
6452 break;
6453
6454 /* PS: opcode 0xe9 appears only after relaxation. Skip here. */
6455 case 0xeb:
6456 /* If opcode_space != SPACE_BASE, this is not a jmp insn. Skip it
6457 for GINSN_GEN_SCFI. */
6458 if (i.tm.opcode_space != SPACE_BASE)
6459 break;
6460 /* Unconditional jmp. */
6461 ginsn = x86_ginsn_jump (insn_end_sym, false);
6462 ginsn_set_where (ginsn);
6463 break;
6464
6465 default:
6466 /* TBD_GINSN_GEN_NOT_SCFI: Skip all other opcodes uninteresting for
6467 GINSN_GEN_SCFI mode. */
6468 break;
6469 }
6470
6471 if (!ginsn && !x86_ginsn_safe_to_skip_p ())
6472 {
6473 /* For all unhandled insns that are not whitelisted, check that they do
6474 not impact SCFI correctness. */
6475 err = x86_ginsn_unhandled ();
6476 switch (err)
6477 {
6478 case X86_GINSN_UNHANDLED_NONE:
6479 break;
6480 case X86_GINSN_UNHANDLED_DEST_REG:
6481 /* Not all writes to REG_FP are harmful in context of SCFI. Simply
6482 generate a GINSN_TYPE_OTHER with destination set to the
6483 appropriate register. The SCFI machinery will bail out if this
6484 ginsn affects SCFI correctness. */
6485 dw2_regnum = ginsn_dw2_regnum (i.op[i.operands - 1].regs);
6486 ginsn = ginsn_new_other (insn_end_sym, true,
6487 GINSN_SRC_IMM, 0,
6488 GINSN_SRC_IMM, 0,
6489 GINSN_DST_REG, dw2_regnum);
6490 ginsn_set_where (ginsn);
6491 break;
6492 case X86_GINSN_UNHANDLED_CFG:
6493 case X86_GINSN_UNHANDLED_STACKOP:
6494 as_bad (_("SCFI: unhandled op %#x may cause incorrect CFI"), opcode);
6495 break;
6496 case X86_GINSN_UNHANDLED_UNEXPECTED:
6497 as_bad (_("SCFI: unexpected op %#x may cause incorrect CFI"),
6498 opcode);
6499 break;
6500 default:
6501 abort ();
6502 break;
6503 }
6504 }
6505
6506 return ginsn;
6507}
6508
6509#endif
6510
252b5132
RH
6511/* This is the guts of the machine-dependent assembler. LINE points to a
6512 machine dependent instruction. This function is supposed to emit
6513 the frags/bytes it assembles to. */
6514
6515void
65da13b5 6516md_assemble (char *line)
252b5132 6517{
40fb9820 6518 unsigned int j;
9db83a32 6519 char mnemonic[MAX_MNEM_SIZE], mnem_suffix = 0, *copy = NULL;
2d4c39a8 6520 char *xstrdup_copy = NULL;
04784e33
JB
6521 const char *end, *pass1_mnem = NULL;
6522 enum i386_error pass1_err = 0;
d3ce72d0 6523 const insn_template *t;
b5482fe5
JB
6524 struct last_insn *last_insn
6525 = &seg_info(now_seg)->tc_segment_info_data.last_insn;
252b5132 6526
47926f60 6527 /* Initialize globals. */
d3b01414 6528 current_templates.end = current_templates.start = NULL;
04784e33 6529 retry:
edd67638 6530 init_globals ();
252b5132 6531
175ce60f
JB
6532 /* Suppress optimization when the last thing we saw may not have been
6533 a proper instruction (e.g. a stand-alone prefix or .byte). */
6534 if (last_insn->kind != last_insn_other)
6535 i.no_optimize = true;
6536
252b5132
RH
6537 /* First parse an instruction mnemonic & call i386_operand for the operands.
6538 We assume that the scrubber has arranged it so that line[0] is the valid
47926f60 6539 start of a (possibly prefixed) mnemonic. */
252b5132 6540
edd67638 6541 end = parse_insn (line, mnemonic, false);
5317ad2c 6542 if (end == NULL)
04784e33
JB
6543 {
6544 if (pass1_mnem != NULL)
6545 goto match_error;
9db83a32
JB
6546 if (i.error != no_error)
6547 {
d3b01414
JB
6548 gas_assert (current_templates.start != NULL);
6549 if (may_need_pass2 (current_templates.start) && !i.suffix)
9db83a32
JB
6550 goto no_match;
6551 /* No point in trying a 2nd pass - it'll only find the same suffix
6552 again. */
6553 mnem_suffix = i.suffix;
6554 goto match_error;
6555 }
04784e33
JB
6556 return;
6557 }
d3b01414 6558 t = current_templates.start;
2d4c39a8
L
6559 /* NB: LINE may be change to be the same as XSTRDUP_COPY. */
6560 if (xstrdup_copy != line && may_need_pass2 (t))
04784e33
JB
6561 {
6562 /* Make a copy of the full line in case we need to retry. */
2d4c39a8
L
6563 xstrdup_copy = xstrdup (line);
6564 copy = xstrdup_copy;
04784e33 6565 }
5317ad2c 6566 line += end - line;
83b16ac6 6567 mnem_suffix = i.suffix;
252b5132 6568
29b0f896 6569 line = parse_operands (line, mnemonic);
ee86248c 6570 this_operand = -1;
29b0f896 6571 if (line == NULL)
04784e33 6572 {
2d4c39a8 6573 free (xstrdup_copy);
04784e33
JB
6574 return;
6575 }
252b5132 6576
29b0f896
AM
6577 /* Now we've parsed the mnemonic into a set of templates, and have the
6578 operands at hand. */
6579
b630c145 6580 /* All Intel opcodes have reversed operands except for "bound", "enter",
c0e54661 6581 "invlpg*", "monitor*", "mwait*", "tpause", "umwait", "pvalidate",
b0e8fa7f
TJ
6582 "rmpadjust", "rmpupdate", and "rmpquery". We also don't reverse
6583 intersegment "jmp" and "call" instructions with 2 immediate operands so
6584 that the immediate segment precedes the offset consistently in Intel and
6585 AT&T modes. */
4d456e3d
L
6586 if (intel_syntax
6587 && i.operands > 1
6d86a545
JB
6588 && (t->mnem_off != MN_bound)
6589 && !startswith (mnemonic, "invlpg")
d34049e8
ML
6590 && !startswith (mnemonic, "monitor")
6591 && !startswith (mnemonic, "mwait")
6d86a545 6592 && (t->mnem_off != MN_pvalidate)
d34049e8 6593 && !startswith (mnemonic, "rmp")
6d86a545
JB
6594 && (t->mnem_off != MN_tpause)
6595 && (t->mnem_off != MN_umwait)
47c0279b
JB
6596 && !(i.operands == 2
6597 && operand_type_check (i.types[0], imm)
40fb9820 6598 && operand_type_check (i.types[1], imm)))
29b0f896
AM
6599 swap_operands ();
6600
e7d74879
JB
6601 /* The order of the immediates should be reversed for 2-immediates EXTRQ
6602 and INSERTQ instructions. Also UWRMSR wants its immediate to be in the
6603 "canonical" place (first), despite it appearing last (in AT&T syntax, or
6604 because of the swapping above) in the incoming set of operands. */
6605 if ((i.imm_operands == 2
6606 && (t->mnem_off == MN_extrq || t->mnem_off == MN_insertq))
6607 || (t->mnem_off == MN_uwrmsr && i.imm_operands
6608 && i.operands > i.imm_operands))
ec56d5c0
JB
6609 swap_2_operands (0, 1);
6610
29b0f896 6611 if (i.imm_operands)
8170af78
HL
6612 {
6613 /* For USER_MSR instructions, imm32 stands for the name of an model specific
6614 register (MSR). That's an unsigned quantity, whereas all other insns with
6615 32-bit immediate and 64-bit operand size use sign-extended
6616 immediates (imm32s). Therefore these insns are special-cased, bypassing
6617 the normal handling of immediates here. */
d3b01414 6618 if (is_cpu(current_templates.start, CpuUSER_MSR))
8170af78
HL
6619 {
6620 for (j = 0; j < i.operands; j++)
6621 {
6622 if (operand_type_check(i.types[j], imm))
6623 i.types[j] = smallest_imm_type (i.op[j].imms->X_add_number);
6624 }
6625 }
6626 else
6627 optimize_imm ();
6628 }
29b0f896 6629
0de704b9
JB
6630 if (i.disp_operands && !optimize_disp (t))
6631 return;
29b0f896
AM
6632
6633 /* Next, we find a template that matches the given insn,
6634 making sure the overlap of the given operands types is consistent
6635 with the template operand types. */
252b5132 6636
83b16ac6 6637 if (!(t = match_template (mnem_suffix)))
04784e33
JB
6638 {
6639 const char *err_msg;
6640
6641 if (copy && !mnem_suffix)
6642 {
6643 line = copy;
6644 copy = NULL;
9db83a32 6645 no_match:
04784e33 6646 pass1_err = i.error;
d3b01414 6647 pass1_mnem = insn_name (current_templates.start);
04784e33
JB
6648 goto retry;
6649 }
9db83a32
JB
6650
6651 /* If a non-/only-64bit template (group) was found in pass 1, and if
6652 _some_ template (group) was found in pass 2, squash pass 1's
6653 error. */
6654 if (pass1_err == unsupported_64bit)
6655 pass1_mnem = NULL;
6656
04784e33 6657 match_error:
2d4c39a8 6658 free (xstrdup_copy);
9db83a32 6659
04784e33
JB
6660 switch (pass1_mnem ? pass1_err : i.error)
6661 {
6662 default:
6663 abort ();
6664 case operand_size_mismatch:
6665 err_msg = _("operand size mismatch");
6666 break;
6667 case operand_type_mismatch:
6668 err_msg = _("operand type mismatch");
6669 break;
6670 case register_type_mismatch:
6671 err_msg = _("register type mismatch");
6672 break;
6673 case number_of_operands_mismatch:
6674 err_msg = _("number of operands mismatch");
6675 break;
6676 case invalid_instruction_suffix:
6677 err_msg = _("invalid instruction suffix");
6678 break;
6679 case bad_imm4:
6680 err_msg = _("constant doesn't fit in 4 bits");
6681 break;
6682 case unsupported_with_intel_mnemonic:
6683 err_msg = _("unsupported with Intel mnemonic");
6684 break;
6685 case unsupported_syntax:
6686 err_msg = _("unsupported syntax");
6687 break;
80d61d8d
CL
6688 case unsupported_EGPR_for_addressing:
6689 err_msg = _("extended GPR cannot be used as base/index");
6690 break;
dd74a603
CL
6691 case unsupported_nf:
6692 err_msg = _("{nf} unsupported");
6693 break;
04784e33
JB
6694 case unsupported:
6695 as_bad (_("unsupported instruction `%s'"),
d3b01414 6696 pass1_mnem ? pass1_mnem : insn_name (current_templates.start));
04784e33 6697 return;
9db83a32
JB
6698 case unsupported_on_arch:
6699 as_bad (_("`%s' is not supported on `%s%s'"),
d3b01414 6700 pass1_mnem ? pass1_mnem : insn_name (current_templates.start),
9db83a32
JB
6701 cpu_arch_name ? cpu_arch_name : default_arch,
6702 cpu_sub_arch_name ? cpu_sub_arch_name : "");
6703 return;
6704 case unsupported_64bit:
6705 if (ISLOWER (mnem_suffix))
e8b4b7b2
NC
6706 {
6707 if (flag_code == CODE_64BIT)
6708 as_bad (_("`%s%c' is not supported in 64-bit mode"),
d3b01414 6709 pass1_mnem ? pass1_mnem : insn_name (current_templates.start),
e8b4b7b2
NC
6710 mnem_suffix);
6711 else
6712 as_bad (_("`%s%c' is only supported in 64-bit mode"),
d3b01414 6713 pass1_mnem ? pass1_mnem : insn_name (current_templates.start),
e8b4b7b2
NC
6714 mnem_suffix);
6715 }
9db83a32 6716 else
e8b4b7b2
NC
6717 {
6718 if (flag_code == CODE_64BIT)
6719 as_bad (_("`%s' is not supported in 64-bit mode"),
d3b01414 6720 pass1_mnem ? pass1_mnem : insn_name (current_templates.start));
e8b4b7b2
NC
6721 else
6722 as_bad (_("`%s' is only supported in 64-bit mode"),
d3b01414 6723 pass1_mnem ? pass1_mnem : insn_name (current_templates.start));
e8b4b7b2 6724 }
9db83a32 6725 return;
54294d73
JB
6726 case no_vex_encoding:
6727 err_msg = _("no VEX/XOP encoding");
6728 break;
6729 case no_evex_encoding:
6730 err_msg = _("no EVEX encoding");
6731 break;
04784e33
JB
6732 case invalid_sib_address:
6733 err_msg = _("invalid SIB address");
6734 break;
6735 case invalid_vsib_address:
6736 err_msg = _("invalid VSIB address");
6737 break;
6738 case invalid_vector_register_set:
6739 err_msg = _("mask, index, and destination registers must be distinct");
6740 break;
6741 case invalid_tmm_register_set:
6742 err_msg = _("all tmm registers must be distinct");
6743 break;
6744 case invalid_dest_and_src_register_set:
6745 err_msg = _("destination and source registers must be distinct");
6746 break;
08a98d4c
MZ
6747 case invalid_dest_register_set:
6748 err_msg = _("two dest registers must be distinct");
6749 break;
80d61d8d
CL
6750 case invalid_pseudo_prefix:
6751 err_msg = _("rex2 pseudo prefix cannot be used");
6752 break;
04784e33
JB
6753 case unsupported_vector_index_register:
6754 err_msg = _("unsupported vector index register");
6755 break;
6756 case unsupported_broadcast:
6757 err_msg = _("unsupported broadcast");
6758 break;
6759 case broadcast_needed:
6760 err_msg = _("broadcast is needed for operand of such type");
6761 break;
6762 case unsupported_masking:
6763 err_msg = _("unsupported masking");
6764 break;
6765 case mask_not_on_destination:
6766 err_msg = _("mask not on destination operand");
6767 break;
6768 case no_default_mask:
6769 err_msg = _("default mask isn't allowed");
6770 break;
6771 case unsupported_rc_sae:
6772 err_msg = _("unsupported static rounding/sae");
6773 break;
54294d73
JB
6774 case unsupported_vector_size:
6775 as_bad (_("vector size above %u required for `%s'"), 128u << vector_size,
d3b01414 6776 pass1_mnem ? pass1_mnem : insn_name (current_templates.start));
54294d73 6777 return;
08a98d4c
MZ
6778 case unsupported_rsp_register:
6779 err_msg = _("'rsp' register cannot be used");
6780 break;
58bceb18
JB
6781 case internal_error:
6782 err_msg = _("internal error");
6783 break;
04784e33
JB
6784 }
6785 as_bad (_("%s for `%s'"), err_msg,
d3b01414 6786 pass1_mnem ? pass1_mnem : insn_name (current_templates.start));
04784e33
JB
6787 return;
6788 }
6789
2d4c39a8 6790 free (xstrdup_copy);
252b5132 6791
7bab8ab5 6792 if (sse_check != check_none
ffb86450
JB
6793 /* The opcode space check isn't strictly needed; it's there only to
6794 bypass the logic below when easily possible. */
ddb62495
JB
6795 && t->opcode_space >= SPACE_0F
6796 && t->opcode_space <= SPACE_0F3A
734dfd1c 6797 && !is_cpu (&i.tm, CpuSSE4a)
ffb86450 6798 && !is_any_vex_encoding (t))
daf50ae7 6799 {
06360a5c
JB
6800 /* Some KL and all WideKL insns have only implicit %xmm operands. */
6801 bool simd = is_cpu (t, CpuKL) || is_cpu (t, CpuWideKL);
ffb86450
JB
6802
6803 for (j = 0; j < t->operands; ++j)
6804 {
6805 if (t->operand_types[j].bitfield.class == RegMMX)
6806 break;
6807 if (t->operand_types[j].bitfield.class == RegSIMD)
6808 simd = true;
6809 }
6810
6811 if (j >= t->operands && simd)
6812 (sse_check == check_warning
6813 ? as_warn
76d3f746 6814 : as_bad) (_("SSE instruction `%s' is used"), insn_name (&i.tm));
daf50ae7
L
6815 }
6816
40fb9820 6817 if (i.tm.opcode_modifier.fwait)
29b0f896
AM
6818 if (!add_prefix (FWAIT_OPCODE))
6819 return;
252b5132 6820
d5de92cf 6821 /* Check if REP prefix is OK. */
742732c7 6822 if (i.rep_prefix && i.tm.opcode_modifier.prefixok != PrefixRep)
d5de92cf
L
6823 {
6824 as_bad (_("invalid instruction `%s' after `%s'"),
76d3f746 6825 insn_name (&i.tm), i.rep_prefix);
d5de92cf
L
6826 return;
6827 }
6828
c1ba0266
L
6829 /* Check for lock without a lockable instruction. Destination operand
6830 must be memory unless it is xchg (0x86). */
9a4a4499
JB
6831 if (i.prefix[LOCK_PREFIX])
6832 {
6833 if (i.tm.opcode_modifier.prefixok < PrefixLock
c1ba0266
L
6834 || i.mem_operands == 0
6835 || (i.tm.base_opcode != 0x86
9a4a4499
JB
6836 && !(i.flags[i.operands - 1] & Operand_Mem)))
6837 {
6838 as_bad (_("expecting lockable instruction after `lock'"));
6839 return;
6840 }
6841
6842 /* Zap the redundant prefix from XCHG when optimizing. */
6843 if (i.tm.base_opcode == 0x86 && optimize && !i.no_optimize)
6844 i.prefix[LOCK_PREFIX] = 0;
c32fa91d
L
6845 }
6846
e3669c7f
JB
6847 if (is_any_vex_encoding (&i.tm)
6848 || i.tm.operand_types[i.imm_operands].bitfield.class >= RegMMX
6849 || i.tm.operand_types[i.imm_operands + 1].bitfield.class >= RegMMX)
7a8655d2 6850 {
e3669c7f
JB
6851 /* Check for data size prefix on VEX/XOP/EVEX encoded and SIMD insns. */
6852 if (i.prefix[DATA_PREFIX])
6853 {
76d3f746 6854 as_bad (_("data size prefix invalid with `%s'"), insn_name (&i.tm));
e3669c7f
JB
6855 return;
6856 }
6857
6858 /* Don't allow e.g. KMOV in TLS code sequences. */
6859 for (j = i.imm_operands; j < i.operands; ++j)
6860 switch (i.reloc[j])
6861 {
5bc71c2a
L
6862 case BFD_RELOC_X86_64_GOTTPOFF:
6863 if (i.tm.mnem_off == MN_add
6864 && i.tm.opcode_space == SPACE_EVEXMAP4
6865 && i.mem_operands == 1
6866 && i.base_reg
6867 && i.base_reg->reg_num == RegIP
6868 && i.tm.operand_types[0].bitfield.class == Reg
6869 && i.tm.operand_types[2].bitfield.class == Reg)
6870 /* Allow APX: add %reg1, foo@gottpoff(%rip), %reg2. */
6871 break;
6872 /* Fall through. */
e3669c7f
JB
6873 case BFD_RELOC_386_TLS_GOTIE:
6874 case BFD_RELOC_386_TLS_LE_32:
e3669c7f 6875 case BFD_RELOC_X86_64_TLSLD:
76d3f746 6876 as_bad (_("TLS relocation cannot be used with `%s'"), insn_name (&i.tm));
e3669c7f
JB
6877 return;
6878 default:
6879 break;
6880 }
7a8655d2
JB
6881 }
6882
42164a71 6883 /* Check if HLE prefix is OK. */
165de32a 6884 if (i.hle_prefix && !check_hle ())
42164a71
L
6885 return;
6886
7e8b059b
L
6887 /* Check BND prefix. */
6888 if (i.bnd_prefix && !i.tm.opcode_modifier.bndprefixok)
6889 as_bad (_("expecting valid branch instruction after `bnd'"));
6890
04ef582a 6891 /* Check NOTRACK prefix. */
742732c7 6892 if (i.notrack_prefix && i.tm.opcode_modifier.prefixok != PrefixNoTrack)
9fef80d6 6893 as_bad (_("expecting indirect branch instruction after `notrack'"));
04ef582a 6894
734dfd1c 6895 if (is_cpu (&i.tm, CpuMPX))
327e8c42
JB
6896 {
6897 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
6898 as_bad (_("32-bit address isn't allowed in 64-bit MPX instructions."));
6899 else if (flag_code != CODE_16BIT
6900 ? i.prefix[ADDR_PREFIX]
6901 : i.mem_operands && !i.prefix[ADDR_PREFIX])
6902 as_bad (_("16-bit address isn't allowed in MPX instructions"));
6903 }
7e8b059b
L
6904
6905 /* Insert BND prefix. */
76d3a78a
JB
6906 if (add_bnd_prefix && i.tm.opcode_modifier.bndprefixok)
6907 {
6908 if (!i.prefix[BND_PREFIX])
6909 add_prefix (BND_PREFIX_OPCODE);
6910 else if (i.prefix[BND_PREFIX] != BND_PREFIX_OPCODE)
6911 {
6912 as_warn (_("replacing `rep'/`repe' prefix by `bnd'"));
6913 i.prefix[BND_PREFIX] = BND_PREFIX_OPCODE;
6914 }
6915 }
7e8b059b 6916
29b0f896 6917 /* Check string instruction segment overrides. */
51c8edf6 6918 if (i.tm.opcode_modifier.isstring >= IS_STRING_ES_OP0)
29b0f896 6919 {
51c8edf6 6920 gas_assert (i.mem_operands);
29b0f896 6921 if (!check_string ())
5dd0794d 6922 return;
fc0763e6 6923 i.disp_operands = 0;
29b0f896 6924 }
5dd0794d 6925
9373f275
L
6926 /* The memory operand of (%dx) should be only used with input/output
6927 instructions (base opcodes: 0x6c, 0x6e, 0xec, 0xee). */
6928 if (i.input_output_operand
6929 && ((i.tm.base_opcode | 0x82) != 0xee
ddb62495 6930 || i.tm.opcode_space != SPACE_BASE))
9373f275
L
6931 {
6932 as_bad (_("input/output port address isn't allowed with `%s'"),
76d3f746 6933 insn_name (&i.tm));
9373f275
L
6934 return;
6935 }
6936
b6f8c7c4
L
6937 if (optimize && !i.no_optimize && i.tm.opcode_modifier.optimize)
6938 optimize_encoding ();
6939
eb3f3841
JB
6940 /* Past optimization there's no need to distinguish encoding_evex,
6941 encoding_evex512, and encoding_egpr anymore. */
e346d50a
JB
6942 if (i.encoding == encoding_evex512)
6943 i.encoding = encoding_evex;
eb3f3841
JB
6944 else if (i.encoding == encoding_egpr)
6945 i.encoding = is_any_vex_encoding (&i.tm) ? encoding_evex
6946 : encoding_default;
a6f3add0 6947
c8480b58
L
6948 if (use_unaligned_vector_move)
6949 encode_with_unaligned_vector_move ();
6950
29b0f896
AM
6951 if (!process_suffix ())
6952 return;
e413e4e9 6953
ef07be45 6954 /* Check if IP-relative addressing requirements can be satisfied. */
734dfd1c 6955 if (is_cpu (&i.tm, CpuPREFETCHI)
ef07be45 6956 && !(i.base_reg && i.base_reg->reg_num == RegIP))
76d3f746 6957 as_warn (_("'%s' only supports RIP-relative address"), insn_name (&i.tm));
ef07be45 6958
921eafea 6959 /* Update operand types and check extended states. */
bc0844ae 6960 for (j = 0; j < i.operands; j++)
921eafea
L
6961 {
6962 i.types[j] = operand_type_and (i.types[j], i.tm.operand_types[j]);
3d70986f 6963 switch (i.tm.operand_types[j].bitfield.class)
921eafea
L
6964 {
6965 default:
6966 break;
6967 case RegMMX:
6968 i.xstate |= xstate_mmx;
6969 break;
6970 case RegMask:
32930e4e 6971 i.xstate |= xstate_mask;
921eafea
L
6972 break;
6973 case RegSIMD:
3d70986f 6974 if (i.tm.operand_types[j].bitfield.tmmword)
921eafea 6975 i.xstate |= xstate_tmm;
4fc85f37 6976 else if (i.tm.operand_types[j].bitfield.zmmword
a6f3add0 6977 && !i.tm.opcode_modifier.vex
4fc85f37 6978 && vector_size >= VSZ512)
921eafea 6979 i.xstate |= xstate_zmm;
4fc85f37
JB
6980 else if (i.tm.operand_types[j].bitfield.ymmword
6981 && vector_size >= VSZ256)
921eafea 6982 i.xstate |= xstate_ymm;
3d70986f 6983 else if (i.tm.operand_types[j].bitfield.xmmword)
921eafea
L
6984 i.xstate |= xstate_xmm;
6985 break;
6986 }
6987 }
bc0844ae 6988
29b0f896
AM
6989 /* Make still unresolved immediate matches conform to size of immediate
6990 given in i.suffix. */
6991 if (!finalize_imm ())
6992 return;
252b5132 6993
40fb9820 6994 if (i.types[0].bitfield.imm1)
29b0f896 6995 i.imm_operands = 0; /* kludge for shift insns. */
252b5132 6996
29b0f896
AM
6997 /* For insns with operands there are more diddles to do to the opcode. */
6998 if (i.operands)
6999 {
7000 if (!process_operands ())
7001 return;
7002 }
255571cd 7003 else if (!quiet_warnings && i.tm.opcode_modifier.operandconstraint == UGH)
29b0f896
AM
7004 {
7005 /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc. */
76d3f746 7006 as_warn (_("translating to `%sp'"), insn_name (&i.tm));
29b0f896 7007 }
252b5132 7008
7a8655d2 7009 if (is_any_vex_encoding (&i.tm))
9e5e5283 7010 {
c1dc7af5 7011 if (!cpu_arch_flags.bitfield.cpui286)
9e5e5283 7012 {
c1dc7af5 7013 as_bad (_("instruction `%s' isn't supported outside of protected mode."),
76d3f746 7014 insn_name (&i.tm));
9e5e5283
L
7015 return;
7016 }
c0f3af97 7017
0b9404fd
JB
7018 /* Check for explicit REX prefix. */
7019 if (i.prefix[REX_PREFIX] || i.rex_encoding)
7020 {
76d3f746 7021 as_bad (_("REX prefix invalid with `%s'"), insn_name (&i.tm));
0b9404fd
JB
7022 return;
7023 }
7024
80d61d8d
CL
7025 /* Check for explicit REX2 prefix. */
7026 if (i.rex2_encoding)
7027 {
7028 as_bad (_("{rex2} prefix invalid with `%s'"), insn_name (&i.tm));
7029 return;
7030 }
7031
6177c84d
CL
7032 if (is_apx_evex_encoding ())
7033 build_apx_evex_prefix ();
7034 else if (i.tm.opcode_modifier.vex)
9e5e5283
L
7035 build_vex_prefix (t);
7036 else
7037 build_evex_prefix ();
0b9404fd
JB
7038
7039 /* The individual REX.RXBW bits got consumed. */
7040 i.rex &= REX_OPCODE;
6177c84d
CL
7041
7042 /* The rex2 bits got consumed. */
7043 i.rex2 = 0;
9e5e5283 7044 }
43234a1e 7045
7fc69528
JB
7046 /* Handle conversion of 'int $3' --> special int3 insn. */
7047 if (i.tm.mnem_off == MN_int
a6461c02 7048 && i.op[0].imms->X_add_number == 3)
29b0f896
AM
7049 {
7050 i.tm.base_opcode = INT3_OPCODE;
7051 i.imm_operands = 0;
7052 }
252b5132 7053
0cfa3eb3
JB
7054 if ((i.tm.opcode_modifier.jump == JUMP
7055 || i.tm.opcode_modifier.jump == JUMP_BYTE
7056 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896
AM
7057 && i.op[0].disps->X_op == O_constant)
7058 {
7059 /* Convert "jmp constant" (and "call constant") to a jump (call) to
7060 the absolute address given by the constant. Since ix86 jumps and
7061 calls are pc relative, we need to generate a reloc. */
7062 i.op[0].disps->X_add_symbol = &abs_symbol;
7063 i.op[0].disps->X_op = O_symbol;
7064 }
252b5132 7065
ce705688 7066 establish_rex ();
29b0f896 7067
b5482fe5 7068 insert_lfence_before (last_insn);
ae531041 7069
29b0f896 7070 /* We are ready to output the insn. */
b5482fe5 7071 output_insn (last_insn);
e379e5f3 7072
c7defc53
IB
7073#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
7074 /* PS: SCFI is enabled only for System V AMD64 ABI. The ABI check has been
7075 performed in i386_target_format. */
7076 if (IS_ELF && flag_synth_cfi)
7077 {
7078 ginsnS *ginsn;
7079 ginsn = x86_ginsn_new (symbol_temp_new_now (), frch_ginsn_gen_mode ());
7080 frch_ginsn_data_append (ginsn);
7081 }
7082#endif
7083
ae531041
L
7084 insert_lfence_after ();
7085
e379e5f3
L
7086 if (i.tm.opcode_modifier.isprefix)
7087 {
b5482fe5
JB
7088 last_insn->kind = last_insn_prefix;
7089 last_insn->name = insn_name (&i.tm);
7090 last_insn->file = as_where (&last_insn->line);
e379e5f3
L
7091 }
7092 else
b5482fe5 7093 last_insn->kind = last_insn_other;
29b0f896
AM
7094}
7095
9db83a32
JB
7096/* The Q suffix is generally valid only in 64-bit mode, with very few
7097 exceptions: fild, fistp, fisttp, and cmpxchg8b. Note that for fild
7098 and fisttp only one of their two templates is matched below: That's
7099 sufficient since other relevant attributes are the same between both
7100 respective templates. */
7101static INLINE bool q_suffix_allowed(const insn_template *t)
7102{
7103 return flag_code == CODE_64BIT
ddb62495 7104 || (t->opcode_space == SPACE_BASE
9db83a32
JB
7105 && t->base_opcode == 0xdf
7106 && (t->extension_opcode & 1)) /* fild / fistp / fisttp */
7fc69528 7107 || t->mnem_off == MN_cmpxchg8b;
9db83a32
JB
7108}
7109
5317ad2c 7110static const char *
edd67638 7111parse_insn (const char *line, char *mnemonic, bool prefix_only)
29b0f896 7112{
5317ad2c 7113 const char *l = line, *token_start = l;
29b0f896 7114 char *mnem_p;
d3b01414 7115 bool pass1 = !current_templates.start;
5c6af06e 7116 int supported;
d3ce72d0 7117 const insn_template *t;
b6169b20 7118 char *dot_p = NULL;
29b0f896 7119
29b0f896
AM
7120 while (1)
7121 {
7122 mnem_p = mnemonic;
778415f5
JB
7123 /* Pseudo-prefixes start with an opening figure brace. */
7124 if ((*mnem_p = *l) == '{')
7125 {
7126 ++mnem_p;
7127 ++l;
7128 }
29b0f896
AM
7129 while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
7130 {
b6169b20
L
7131 if (*mnem_p == '.')
7132 dot_p = mnem_p;
29b0f896
AM
7133 mnem_p++;
7134 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
45288df1 7135 {
778415f5 7136 too_long:
29b0f896
AM
7137 as_bad (_("no such instruction: `%s'"), token_start);
7138 return NULL;
7139 }
7140 l++;
7141 }
778415f5
JB
7142 /* Pseudo-prefixes end with a closing figure brace. */
7143 if (*mnemonic == '{' && *l == '}')
7144 {
7145 *mnem_p++ = *l++;
7146 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
7147 goto too_long;
7148 *mnem_p = '\0';
7149
7150 /* Point l at the closing brace if there's no other separator. */
7151 if (*l != END_OF_INSN && !is_space_char (*l)
7152 && *l != PREFIX_SEPARATOR)
7153 --l;
7154 }
7155 else if (!is_space_char (*l)
7156 && *l != END_OF_INSN
7157 && (intel_syntax
7158 || (*l != PREFIX_SEPARATOR && *l != ',')))
29b0f896 7159 {
edd67638
JB
7160 if (prefix_only)
7161 break;
29b0f896
AM
7162 as_bad (_("invalid character %s in mnemonic"),
7163 output_invalid (*l));
7164 return NULL;
7165 }
7166 if (token_start == l)
7167 {
e44823cf 7168 if (!intel_syntax && *l == PREFIX_SEPARATOR)
29b0f896
AM
7169 as_bad (_("expecting prefix; got nothing"));
7170 else
7171 as_bad (_("expecting mnemonic; got nothing"));
7172 return NULL;
7173 }
45288df1 7174
29b0f896 7175 /* Look up instruction (or prefix) via hash table. */
d3b01414 7176 op_lookup (mnemonic);
47926f60 7177
29b0f896
AM
7178 if (*l != END_OF_INSN
7179 && (!is_space_char (*l) || l[1] != END_OF_INSN)
d3b01414
JB
7180 && current_templates.start
7181 && current_templates.start->opcode_modifier.isprefix)
29b0f896 7182 {
d3b01414 7183 supported = cpu_flags_match (current_templates.start);
3086ed9a 7184 if (!(supported & CPU_FLAGS_64BIT_MATCH))
2dd88dca
JB
7185 {
7186 as_bad ((flag_code != CODE_64BIT
7187 ? _("`%s' is only supported in 64-bit mode")
7188 : _("`%s' is not supported in 64-bit mode")),
d3b01414 7189 insn_name (current_templates.start));
2dd88dca
JB
7190 return NULL;
7191 }
3086ed9a
JB
7192 if (supported != CPU_FLAGS_PERFECT_MATCH)
7193 {
7194 as_bad (_("`%s' is not supported on `%s%s'"),
d3b01414 7195 insn_name (current_templates.start),
3086ed9a
JB
7196 cpu_arch_name ? cpu_arch_name : default_arch,
7197 cpu_sub_arch_name ? cpu_sub_arch_name : "");
7198 return NULL;
7199 }
29b0f896
AM
7200 /* If we are in 16-bit mode, do not allow addr16 or data16.
7201 Similarly, in 32-bit mode, do not allow addr32 or data32. */
d3b01414
JB
7202 if ((current_templates.start->opcode_modifier.size == SIZE16
7203 || current_templates.start->opcode_modifier.size == SIZE32)
29b0f896 7204 && flag_code != CODE_64BIT
d3b01414 7205 && ((current_templates.start->opcode_modifier.size == SIZE32)
29b0f896
AM
7206 ^ (flag_code == CODE_16BIT)))
7207 {
7208 as_bad (_("redundant %s prefix"),
d3b01414 7209 insn_name (current_templates.start));
29b0f896 7210 return NULL;
45288df1 7211 }
31184569 7212
d3b01414 7213 if (current_templates.start->base_opcode == PSEUDO_PREFIX)
29b0f896 7214 {
86fa6981 7215 /* Handle pseudo prefixes. */
d3b01414 7216 switch (current_templates.start->extension_opcode)
86fa6981 7217 {
41eb8e88 7218 case Prefix_Disp8:
86fa6981
L
7219 /* {disp8} */
7220 i.disp_encoding = disp_encoding_8bit;
7221 break;
41eb8e88
L
7222 case Prefix_Disp16:
7223 /* {disp16} */
7224 i.disp_encoding = disp_encoding_16bit;
7225 break;
7226 case Prefix_Disp32:
86fa6981
L
7227 /* {disp32} */
7228 i.disp_encoding = disp_encoding_32bit;
7229 break;
41eb8e88 7230 case Prefix_Load:
86fa6981
L
7231 /* {load} */
7232 i.dir_encoding = dir_encoding_load;
7233 break;
41eb8e88 7234 case Prefix_Store:
86fa6981
L
7235 /* {store} */
7236 i.dir_encoding = dir_encoding_store;
7237 break;
41eb8e88 7238 case Prefix_VEX:
42e04b36 7239 /* {vex} */
e346d50a 7240 i.encoding = encoding_vex;
86fa6981 7241 break;
41eb8e88 7242 case Prefix_VEX3:
86fa6981 7243 /* {vex3} */
e346d50a 7244 i.encoding = encoding_vex3;
86fa6981 7245 break;
41eb8e88 7246 case Prefix_EVEX:
86fa6981 7247 /* {evex} */
e346d50a 7248 i.encoding = encoding_evex;
86fa6981 7249 break;
41eb8e88 7250 case Prefix_REX:
6b6b6807 7251 /* {rex} */
5b7c81bd 7252 i.rex_encoding = true;
6b6b6807 7253 break;
80d61d8d
CL
7254 case Prefix_REX2:
7255 /* {rex2} */
7256 i.rex2_encoding = true;
7257 break;
dd74a603
CL
7258 case Prefix_NF:
7259 /* {nf} */
7260 i.has_nf = true;
7261 if (i.encoding == encoding_default)
7262 i.encoding = encoding_evex;
7263 break;
41eb8e88 7264 case Prefix_NoOptimize:
b6f8c7c4 7265 /* {nooptimize} */
5b7c81bd 7266 i.no_optimize = true;
b6f8c7c4 7267 break;
86fa6981
L
7268 default:
7269 abort ();
7270 }
dd74a603
CL
7271 if (i.has_nf && i.encoding != encoding_evex)
7272 {
7273 as_bad (_("{nf} cannot be combined with {vex}/{vex3}"));
7274 return NULL;
7275 }
86fa6981
L
7276 }
7277 else
7278 {
7279 /* Add prefix, checking for repeated prefixes. */
d3b01414 7280 switch (add_prefix (current_templates.start->base_opcode))
86fa6981 7281 {
4e9ac44a
L
7282 case PREFIX_EXIST:
7283 return NULL;
7284 case PREFIX_DS:
d3b01414
JB
7285 if (is_cpu (current_templates.start, CpuIBT))
7286 i.notrack_prefix = insn_name (current_templates.start);
4e9ac44a
L
7287 break;
7288 case PREFIX_REP:
d3b01414
JB
7289 if (is_cpu (current_templates.start, CpuHLE))
7290 i.hle_prefix = insn_name (current_templates.start);
7291 else if (is_cpu (current_templates.start, CpuMPX))
7292 i.bnd_prefix = insn_name (current_templates.start);
4e9ac44a 7293 else
d3b01414 7294 i.rep_prefix = insn_name (current_templates.start);
4e9ac44a
L
7295 break;
7296 default:
7297 break;
86fa6981 7298 }
29b0f896
AM
7299 }
7300 /* Skip past PREFIX_SEPARATOR and reset token_start. */
7301 token_start = ++l;
7302 }
7303 else
7304 break;
7305 }
45288df1 7306
edd67638
JB
7307 if (prefix_only)
7308 return token_start;
7309
d3b01414 7310 if (!current_templates.start)
b6169b20 7311 {
07d5e953
JB
7312 /* Deprecated functionality (new code should use pseudo-prefixes instead):
7313 Check if we should swap operand or force 32bit displacement in
f8a5c266 7314 encoding. */
30a55f88 7315 if (mnem_p - 2 == dot_p && dot_p[1] == 's')
97f31cb4
JB
7316 {
7317 if (i.dir_encoding == dir_encoding_default)
7318 i.dir_encoding = dir_encoding_swap;
7319 else
7320 as_warn (_("ignoring `.s' suffix due to earlier `{%s}'"),
7321 i.dir_encoding == dir_encoding_load ? "load" : "store");
7322 }
8d63c93e 7323 else if (mnem_p - 3 == dot_p
a501d77e
L
7324 && dot_p[1] == 'd'
7325 && dot_p[2] == '8')
97f31cb4
JB
7326 {
7327 if (i.disp_encoding == disp_encoding_default)
7328 i.disp_encoding = disp_encoding_8bit;
7329 else if (i.disp_encoding != disp_encoding_8bit)
7330 as_warn (_("ignoring `.d8' suffix due to earlier `{disp<N>}'"));
7331 }
8d63c93e 7332 else if (mnem_p - 4 == dot_p
f8a5c266
L
7333 && dot_p[1] == 'd'
7334 && dot_p[2] == '3'
7335 && dot_p[3] == '2')
97f31cb4
JB
7336 {
7337 if (i.disp_encoding == disp_encoding_default)
7338 i.disp_encoding = disp_encoding_32bit;
7339 else if (i.disp_encoding != disp_encoding_32bit)
7340 as_warn (_("ignoring `.d32' suffix due to earlier `{disp<N>}'"));
7341 }
30a55f88
L
7342 else
7343 goto check_suffix;
7344 mnem_p = dot_p;
7345 *dot_p = '\0';
d3b01414 7346 op_lookup (mnemonic);
b6169b20
L
7347 }
7348
d3b01414 7349 if (!current_templates.start || !pass1)
29b0f896 7350 {
d3b01414 7351 current_templates.start = NULL;
04784e33 7352
dc1e8a47 7353 check_suffix:
1c529385 7354 if (mnem_p > mnemonic)
29b0f896 7355 {
1c529385
LH
7356 /* See if we can get a match by trimming off a suffix. */
7357 switch (mnem_p[-1])
29b0f896 7358 {
1c529385
LH
7359 case WORD_MNEM_SUFFIX:
7360 if (intel_syntax && (intel_float_operand (mnemonic) & 2))
29b0f896
AM
7361 i.suffix = SHORT_MNEM_SUFFIX;
7362 else
1c529385
LH
7363 /* Fall through. */
7364 case BYTE_MNEM_SUFFIX:
7365 case QWORD_MNEM_SUFFIX:
7366 i.suffix = mnem_p[-1];
29b0f896 7367 mnem_p[-1] = '\0';
d3b01414 7368 op_lookup (mnemonic);
1c529385
LH
7369 break;
7370 case SHORT_MNEM_SUFFIX:
7371 case LONG_MNEM_SUFFIX:
7372 if (!intel_syntax)
7373 {
7374 i.suffix = mnem_p[-1];
7375 mnem_p[-1] = '\0';
d3b01414 7376 op_lookup (mnemonic);
1c529385
LH
7377 }
7378 break;
7379
7380 /* Intel Syntax. */
7381 case 'd':
7382 if (intel_syntax)
7383 {
7384 if (intel_float_operand (mnemonic) == 1)
7385 i.suffix = SHORT_MNEM_SUFFIX;
7386 else
7387 i.suffix = LONG_MNEM_SUFFIX;
7388 mnem_p[-1] = '\0';
d3b01414 7389 op_lookup (mnemonic);
1c529385 7390 }
04784e33
JB
7391 /* For compatibility reasons accept MOVSD and CMPSD without
7392 operands even in AT&T mode. */
7393 else if (*l == END_OF_INSN
7394 || (is_space_char (*l) && l[1] == END_OF_INSN))
7395 {
7396 mnem_p[-1] = '\0';
d3b01414
JB
7397 op_lookup (mnemonic);
7398 if (current_templates.start != NULL
04784e33 7399 /* MOVS or CMPS */
d3b01414
JB
7400 && (current_templates.start->base_opcode | 2) == 0xa6
7401 && current_templates.start->opcode_space
04784e33
JB
7402 == SPACE_BASE
7403 && mnem_p[-2] == 's')
7404 {
7405 as_warn (_("found `%sd'; assuming `%sl' was meant"),
7406 mnemonic, mnemonic);
7407 i.suffix = LONG_MNEM_SUFFIX;
7408 }
7409 else
7410 {
d3b01414 7411 current_templates.start = NULL;
04784e33
JB
7412 mnem_p[-1] = 'd';
7413 }
7414 }
1c529385 7415 break;
29b0f896 7416 }
29b0f896 7417 }
1c529385 7418
d3b01414 7419 if (!current_templates.start)
29b0f896 7420 {
04784e33
JB
7421 if (pass1)
7422 as_bad (_("no such instruction: `%s'"), token_start);
29b0f896
AM
7423 return NULL;
7424 }
7425 }
252b5132 7426
d3b01414
JB
7427 if (current_templates.start->opcode_modifier.jump == JUMP
7428 || current_templates.start->opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
7429 {
7430 /* Check for a branch hint. We allow ",pt" and ",pn" for
7431 predict taken and predict not taken respectively.
7432 I'm not sure that branch hints actually do anything on loop
7433 and jcxz insns (JumpByte) for current Pentium4 chips. They
7434 may work in the future and it doesn't hurt to accept them
7435 now. */
7436 if (l[0] == ',' && l[1] == 'p')
7437 {
7438 if (l[2] == 't')
7439 {
7440 if (!add_prefix (DS_PREFIX_OPCODE))
7441 return NULL;
7442 l += 3;
7443 }
7444 else if (l[2] == 'n')
7445 {
7446 if (!add_prefix (CS_PREFIX_OPCODE))
7447 return NULL;
7448 l += 3;
7449 }
7450 }
7451 }
7452 /* Any other comma loses. */
7453 if (*l == ',')
7454 {
7455 as_bad (_("invalid character %s in mnemonic"),
7456 output_invalid (*l));
7457 return NULL;
7458 }
252b5132 7459
29b0f896 7460 /* Check if instruction is supported on specified architecture. */
5c6af06e 7461 supported = 0;
d3b01414 7462 for (t = current_templates.start; t < current_templates.end; ++t)
5c6af06e 7463 {
c0f3af97 7464 supported |= cpu_flags_match (t);
9db83a32
JB
7465
7466 if (i.suffix == QWORD_MNEM_SUFFIX && !q_suffix_allowed (t))
7467 supported &= ~CPU_FLAGS_64BIT_MATCH;
7468
c0f3af97 7469 if (supported == CPU_FLAGS_PERFECT_MATCH)
d59a54c2 7470 return l;
29b0f896 7471 }
3629bb00 7472
9db83a32
JB
7473 if (pass1)
7474 {
7475 if (supported & CPU_FLAGS_64BIT_MATCH)
7476 i.error = unsupported_on_arch;
7477 else
7478 i.error = unsupported_64bit;
7479 }
252b5132 7480
548d0ee6 7481 return NULL;
29b0f896 7482}
252b5132 7483
29b0f896 7484static char *
e3bb37b5 7485parse_operands (char *l, const char *mnemonic)
29b0f896
AM
7486{
7487 char *token_start;
3138f287 7488
29b0f896
AM
7489 /* 1 if operand is pending after ','. */
7490 unsigned int expecting_operand = 0;
252b5132 7491
29b0f896
AM
7492 while (*l != END_OF_INSN)
7493 {
e68c3d59
JB
7494 /* Non-zero if operand parens not balanced. */
7495 unsigned int paren_not_balanced = 0;
7496 /* True if inside double quotes. */
7497 bool in_quotes = false;
7498
29b0f896
AM
7499 /* Skip optional white space before operand. */
7500 if (is_space_char (*l))
7501 ++l;
d02603dc 7502 if (!is_operand_char (*l) && *l != END_OF_INSN && *l != '"')
29b0f896
AM
7503 {
7504 as_bad (_("invalid character %s before operand %d"),
7505 output_invalid (*l),
7506 i.operands + 1);
7507 return NULL;
7508 }
d02603dc 7509 token_start = l; /* After white space. */
e68c3d59 7510 while (in_quotes || paren_not_balanced || *l != ',')
29b0f896
AM
7511 {
7512 if (*l == END_OF_INSN)
7513 {
e68c3d59
JB
7514 if (in_quotes)
7515 {
7516 as_bad (_("unbalanced double quotes in operand %d."),
7517 i.operands + 1);
7518 return NULL;
7519 }
29b0f896
AM
7520 if (paren_not_balanced)
7521 {
98ff9f1c
JB
7522 know (!intel_syntax);
7523 as_bad (_("unbalanced parenthesis in operand %d."),
7524 i.operands + 1);
29b0f896
AM
7525 return NULL;
7526 }
7527 else
7528 break; /* we are done */
7529 }
e68c3d59
JB
7530 else if (*l == '\\' && l[1] == '"')
7531 ++l;
7532 else if (*l == '"')
7533 in_quotes = !in_quotes;
7534 else if (!in_quotes && !is_operand_char (*l) && !is_space_char (*l))
29b0f896
AM
7535 {
7536 as_bad (_("invalid character %s in operand %d"),
7537 output_invalid (*l),
7538 i.operands + 1);
7539 return NULL;
7540 }
e68c3d59 7541 if (!intel_syntax && !in_quotes)
29b0f896
AM
7542 {
7543 if (*l == '(')
7544 ++paren_not_balanced;
7545 if (*l == ')')
7546 --paren_not_balanced;
7547 }
29b0f896
AM
7548 l++;
7549 }
7550 if (l != token_start)
7551 { /* Yes, we've read in another operand. */
7552 unsigned int operand_ok;
7553 this_operand = i.operands++;
7554 if (i.operands > MAX_OPERANDS)
7555 {
7556 as_bad (_("spurious operands; (%d operands/instruction max)"),
7557 MAX_OPERANDS);
7558 return NULL;
7559 }
9d46ce34 7560 i.types[this_operand].bitfield.unspecified = 1;
29b0f896
AM
7561 /* Now parse operand adding info to 'i' as we go along. */
7562 END_STRING_AND_SAVE (l);
7563
1286ab78
L
7564 if (i.mem_operands > 1)
7565 {
7566 as_bad (_("too many memory references for `%s'"),
7567 mnemonic);
7568 return 0;
7569 }
7570
29b0f896
AM
7571 if (intel_syntax)
7572 operand_ok =
7573 i386_intel_operand (token_start,
7574 intel_float_operand (mnemonic));
7575 else
a7619375 7576 operand_ok = i386_att_operand (token_start);
29b0f896
AM
7577
7578 RESTORE_END_STRING (l);
7579 if (!operand_ok)
7580 return NULL;
7581 }
7582 else
7583 {
7584 if (expecting_operand)
7585 {
7586 expecting_operand_after_comma:
7587 as_bad (_("expecting operand after ','; got nothing"));
7588 return NULL;
7589 }
7590 if (*l == ',')
7591 {
7592 as_bad (_("expecting operand before ','; got nothing"));
7593 return NULL;
7594 }
7595 }
7f3f1ea2 7596
29b0f896
AM
7597 /* Now *l must be either ',' or END_OF_INSN. */
7598 if (*l == ',')
7599 {
7600 if (*++l == END_OF_INSN)
7601 {
7602 /* Just skip it, if it's \n complain. */
7603 goto expecting_operand_after_comma;
7604 }
7605 expecting_operand = 1;
7606 }
7607 }
7608 return l;
7609}
7f3f1ea2 7610
050dfa73 7611static void
783c187b 7612swap_2_operands (unsigned int xchg1, unsigned int xchg2)
050dfa73
MM
7613{
7614 union i386_op temp_op;
40fb9820 7615 i386_operand_type temp_type;
c48dadc9 7616 unsigned int temp_flags;
050dfa73 7617 enum bfd_reloc_code_real temp_reloc;
4eed87de 7618
050dfa73
MM
7619 temp_type = i.types[xchg2];
7620 i.types[xchg2] = i.types[xchg1];
7621 i.types[xchg1] = temp_type;
c48dadc9
JB
7622
7623 temp_flags = i.flags[xchg2];
7624 i.flags[xchg2] = i.flags[xchg1];
7625 i.flags[xchg1] = temp_flags;
7626
050dfa73
MM
7627 temp_op = i.op[xchg2];
7628 i.op[xchg2] = i.op[xchg1];
7629 i.op[xchg1] = temp_op;
c48dadc9 7630
050dfa73
MM
7631 temp_reloc = i.reloc[xchg2];
7632 i.reloc[xchg2] = i.reloc[xchg1];
7633 i.reloc[xchg1] = temp_reloc;
43234a1e 7634
c032bc4f
JB
7635 temp_flags = i.imm_bits[xchg2];
7636 i.imm_bits[xchg2] = i.imm_bits[xchg1];
7637 i.imm_bits[xchg1] = temp_flags;
7638
6225c532 7639 if (i.mask.reg)
43234a1e 7640 {
6225c532
JB
7641 if (i.mask.operand == xchg1)
7642 i.mask.operand = xchg2;
7643 else if (i.mask.operand == xchg2)
7644 i.mask.operand = xchg1;
43234a1e 7645 }
a5748e0d 7646 if (i.broadcast.type || i.broadcast.bytes)
43234a1e 7647 {
5273a3cd
JB
7648 if (i.broadcast.operand == xchg1)
7649 i.broadcast.operand = xchg2;
7650 else if (i.broadcast.operand == xchg2)
7651 i.broadcast.operand = xchg1;
43234a1e 7652 }
050dfa73
MM
7653}
7654
29b0f896 7655static void
e3bb37b5 7656swap_operands (void)
29b0f896 7657{
b7c61d9a 7658 switch (i.operands)
050dfa73 7659 {
c0f3af97 7660 case 5:
b7c61d9a 7661 case 4:
4d456e3d 7662 swap_2_operands (1, i.operands - 2);
1a0670f3 7663 /* Fall through. */
b7c61d9a
L
7664 case 3:
7665 case 2:
4d456e3d 7666 swap_2_operands (0, i.operands - 1);
b7c61d9a
L
7667 break;
7668 default:
7669 abort ();
29b0f896 7670 }
29b0f896
AM
7671
7672 if (i.mem_operands == 2)
7673 {
5e042380 7674 const reg_entry *temp_seg;
29b0f896
AM
7675 temp_seg = i.seg[0];
7676 i.seg[0] = i.seg[1];
7677 i.seg[1] = temp_seg;
7678 }
7679}
252b5132 7680
29b0f896
AM
7681/* Try to ensure constant immediates are represented in the smallest
7682 opcode possible. */
7683static void
e3bb37b5 7684optimize_imm (void)
29b0f896
AM
7685{
7686 char guess_suffix = 0;
7687 int op;
252b5132 7688
29b0f896
AM
7689 if (i.suffix)
7690 guess_suffix = i.suffix;
7691 else if (i.reg_operands)
7692 {
7693 /* Figure out a suffix from the last register operand specified.
75e5731b
JB
7694 We can't do this properly yet, i.e. excluding special register
7695 instances, but the following works for instructions with
7696 immediates. In any case, we can't set i.suffix yet. */
29b0f896 7697 for (op = i.operands; --op >= 0;)
bab6aec1
JB
7698 if (i.types[op].bitfield.class != Reg)
7699 continue;
7700 else if (i.types[op].bitfield.byte)
7ab9ffdd 7701 {
40fb9820
L
7702 guess_suffix = BYTE_MNEM_SUFFIX;
7703 break;
7704 }
bab6aec1 7705 else if (i.types[op].bitfield.word)
252b5132 7706 {
40fb9820
L
7707 guess_suffix = WORD_MNEM_SUFFIX;
7708 break;
7709 }
bab6aec1 7710 else if (i.types[op].bitfield.dword)
40fb9820
L
7711 {
7712 guess_suffix = LONG_MNEM_SUFFIX;
7713 break;
7714 }
bab6aec1 7715 else if (i.types[op].bitfield.qword)
40fb9820
L
7716 {
7717 guess_suffix = QWORD_MNEM_SUFFIX;
29b0f896 7718 break;
252b5132 7719 }
29b0f896 7720 }
f79d55e1
JB
7721 else if ((flag_code == CODE_16BIT)
7722 ^ (i.prefix[DATA_PREFIX] != 0 && !(i.prefix[REX_PREFIX] & REX_W)))
29b0f896 7723 guess_suffix = WORD_MNEM_SUFFIX;
fb1c1058
JB
7724 else if (flag_code != CODE_64BIT
7725 || (!(i.prefix[REX_PREFIX] & REX_W)
7726 /* A more generic (but also more involved) way of dealing
7727 with the special case(s) would be to go look for
7728 DefaultSize attributes on any of the templates. */
d3b01414 7729 && current_templates.start->mnem_off != MN_push))
5cc00775 7730 guess_suffix = LONG_MNEM_SUFFIX;
29b0f896
AM
7731
7732 for (op = i.operands; --op >= 0;)
40fb9820 7733 if (operand_type_check (i.types[op], imm))
29b0f896
AM
7734 {
7735 switch (i.op[op].imms->X_op)
252b5132 7736 {
29b0f896
AM
7737 case O_constant:
7738 /* If a suffix is given, this operand may be shortened. */
7739 switch (guess_suffix)
252b5132 7740 {
29b0f896 7741 case LONG_MNEM_SUFFIX:
40fb9820
L
7742 i.types[op].bitfield.imm32 = 1;
7743 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
7744 break;
7745 case WORD_MNEM_SUFFIX:
40fb9820
L
7746 i.types[op].bitfield.imm16 = 1;
7747 i.types[op].bitfield.imm32 = 1;
7748 i.types[op].bitfield.imm32s = 1;
7749 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
7750 break;
7751 case BYTE_MNEM_SUFFIX:
40fb9820
L
7752 i.types[op].bitfield.imm8 = 1;
7753 i.types[op].bitfield.imm8s = 1;
7754 i.types[op].bitfield.imm16 = 1;
7755 i.types[op].bitfield.imm32 = 1;
7756 i.types[op].bitfield.imm32s = 1;
7757 i.types[op].bitfield.imm64 = 1;
29b0f896 7758 break;
252b5132 7759 }
252b5132 7760
29b0f896
AM
7761 /* If this operand is at most 16 bits, convert it
7762 to a signed 16 bit number before trying to see
7763 whether it will fit in an even smaller size.
7764 This allows a 16-bit operand such as $0xffe0 to
7765 be recognised as within Imm8S range. */
40fb9820 7766 if ((i.types[op].bitfield.imm16)
7e96fb68 7767 && fits_in_unsigned_word (i.op[op].imms->X_add_number))
252b5132 7768 {
87ed972d
JB
7769 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
7770 ^ 0x8000) - 0x8000);
29b0f896 7771 }
a28def75
L
7772#ifdef BFD64
7773 /* Store 32-bit immediate in 64-bit for 64-bit BFD. */
40fb9820 7774 if ((i.types[op].bitfield.imm32)
7e96fb68 7775 && fits_in_unsigned_long (i.op[op].imms->X_add_number))
29b0f896
AM
7776 {
7777 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
7778 ^ ((offsetT) 1 << 31))
7779 - ((offsetT) 1 << 31));
7780 }
a28def75 7781#endif
40fb9820 7782 i.types[op]
c6fb90c8
L
7783 = operand_type_or (i.types[op],
7784 smallest_imm_type (i.op[op].imms->X_add_number));
252b5132 7785
29b0f896
AM
7786 /* We must avoid matching of Imm32 templates when 64bit
7787 only immediate is available. */
7788 if (guess_suffix == QWORD_MNEM_SUFFIX)
40fb9820 7789 i.types[op].bitfield.imm32 = 0;
29b0f896 7790 break;
252b5132 7791
29b0f896
AM
7792 case O_absent:
7793 case O_register:
7794 abort ();
7795
7796 /* Symbols and expressions. */
7797 default:
9cd96992
JB
7798 /* Convert symbolic operand to proper sizes for matching, but don't
7799 prevent matching a set of insns that only supports sizes other
7800 than those matching the insn suffix. */
7801 {
40fb9820 7802 i386_operand_type mask, allowed;
d3b01414 7803 const insn_template *t = current_templates.start;
9cd96992 7804
0dfbf9d7 7805 operand_type_set (&mask, 0);
9cd96992
JB
7806 switch (guess_suffix)
7807 {
7808 case QWORD_MNEM_SUFFIX:
40fb9820
L
7809 mask.bitfield.imm64 = 1;
7810 mask.bitfield.imm32s = 1;
9cd96992
JB
7811 break;
7812 case LONG_MNEM_SUFFIX:
40fb9820 7813 mask.bitfield.imm32 = 1;
9cd96992
JB
7814 break;
7815 case WORD_MNEM_SUFFIX:
40fb9820 7816 mask.bitfield.imm16 = 1;
9cd96992
JB
7817 break;
7818 case BYTE_MNEM_SUFFIX:
40fb9820 7819 mask.bitfield.imm8 = 1;
9cd96992
JB
7820 break;
7821 default:
9cd96992
JB
7822 break;
7823 }
8f0212ac
JB
7824
7825 allowed = operand_type_and (t->operand_types[op], mask);
d3b01414 7826 while (++t < current_templates.end)
8f0212ac
JB
7827 {
7828 allowed = operand_type_or (allowed, t->operand_types[op]);
7829 allowed = operand_type_and (allowed, mask);
7830 }
7831
0dfbf9d7 7832 if (!operand_type_all_zero (&allowed))
c6fb90c8 7833 i.types[op] = operand_type_and (i.types[op], mask);
9cd96992 7834 }
29b0f896 7835 break;
252b5132 7836 }
29b0f896
AM
7837 }
7838}
47926f60 7839
29b0f896 7840/* Try to use the smallest displacement type too. */
0de704b9
JB
7841static bool
7842optimize_disp (const insn_template *t)
29b0f896 7843{
0de704b9 7844 unsigned int op;
3e73aa7c 7845
0de704b9
JB
7846 if (!want_disp32 (t)
7847 && (!t->opcode_modifier.jump
7848 || i.jumpabsolute || i.types[0].bitfield.baseindex))
7849 {
7850 for (op = 0; op < i.operands; ++op)
7851 {
7852 const expressionS *exp = i.op[op].disps;
7853
7854 if (!operand_type_check (i.types[op], disp))
7855 continue;
7856
7857 if (exp->X_op != O_constant)
7858 continue;
7859
7860 /* Since displacement is signed extended to 64bit, don't allow
7861 disp32 if it is out of range. */
7862 if (fits_in_signed_long (exp->X_add_number))
7863 continue;
7864
7865 i.types[op].bitfield.disp32 = 0;
7866 if (i.types[op].bitfield.baseindex)
7867 {
7868 as_bad (_("0x%" PRIx64 " out of range of signed 32bit displacement"),
7869 (uint64_t) exp->X_add_number);
7870 return false;
7871 }
7872 }
7873 }
7874
7875 /* Don't optimize displacement for movabs since it only takes 64bit
7876 displacement. */
7877 if (i.disp_encoding > disp_encoding_8bit
7878 || (flag_code == CODE_64BIT && t->mnem_off == MN_movabs))
7879 return true;
7880
7881 for (op = i.operands; op-- > 0;)
40fb9820 7882 if (operand_type_check (i.types[op], disp))
252b5132 7883 {
b300c311 7884 if (i.op[op].disps->X_op == O_constant)
252b5132 7885 {
91d6fa6a 7886 offsetT op_disp = i.op[op].disps->X_add_number;
29b0f896 7887
91d6fa6a 7888 if (!op_disp && i.types[op].bitfield.baseindex)
b300c311 7889 {
2f2be86b
JB
7890 i.types[op] = operand_type_and_not (i.types[op], anydisp);
7891 i.op[op].disps = NULL;
b300c311 7892 i.disp_operands--;
f185acdd
JB
7893 continue;
7894 }
7895
7896 if (i.types[op].bitfield.disp16
cd613c1f 7897 && fits_in_unsigned_word (op_disp))
f185acdd
JB
7898 {
7899 /* If this operand is at most 16 bits, convert
7900 to a signed 16 bit number and don't use 64bit
7901 displacement. */
7902 op_disp = ((op_disp ^ 0x8000) - 0x8000);
7903 i.types[op].bitfield.disp64 = 0;
b300c311 7904 }
f185acdd 7905
28a167a4 7906#ifdef BFD64
a50187b2 7907 /* Optimize 64-bit displacement to 32-bit for 64-bit BFD. */
a775efc8
JB
7908 if ((flag_code != CODE_64BIT
7909 ? i.types[op].bitfield.disp32
0de704b9
JB
7910 : want_disp32 (t)
7911 && (!t->opcode_modifier.jump
a775efc8 7912 || i.jumpabsolute || i.types[op].bitfield.baseindex))
a50187b2 7913 && fits_in_unsigned_long (op_disp))
b300c311 7914 {
a50187b2
JB
7915 /* If this operand is at most 32 bits, convert
7916 to a signed 32 bit number and don't use 64bit
7917 displacement. */
7918 op_disp = (op_disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
7919 i.types[op].bitfield.disp64 = 0;
7920 i.types[op].bitfield.disp32 = 1;
7921 }
28a167a4 7922
a50187b2
JB
7923 if (flag_code == CODE_64BIT && fits_in_signed_long (op_disp))
7924 {
7925 i.types[op].bitfield.disp64 = 0;
a775efc8 7926 i.types[op].bitfield.disp32 = 1;
b300c311 7927 }
28a167a4 7928#endif
40fb9820 7929 if ((i.types[op].bitfield.disp32
40fb9820 7930 || i.types[op].bitfield.disp16)
b5014f7a 7931 && fits_in_disp8 (op_disp))
40fb9820 7932 i.types[op].bitfield.disp8 = 1;
77c59789
JB
7933
7934 i.op[op].disps->X_add_number = op_disp;
252b5132 7935 }
67a4f2b7
AO
7936 else if (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
7937 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL)
7938 {
7939 fix_new_exp (frag_now, frag_more (0) - frag_now->fr_literal, 0,
7940 i.op[op].disps, 0, i.reloc[op]);
2f2be86b 7941 i.types[op] = operand_type_and_not (i.types[op], anydisp);
67a4f2b7
AO
7942 }
7943 else
b300c311 7944 /* We only support 64bit displacement on constants. */
40fb9820 7945 i.types[op].bitfield.disp64 = 0;
252b5132 7946 }
0de704b9
JB
7947
7948 return true;
29b0f896
AM
7949}
7950
4a1b91ea
L
7951/* Return 1 if there is a match in broadcast bytes between operand
7952 GIVEN and instruction template T. */
7953
7954static INLINE int
7955match_broadcast_size (const insn_template *t, unsigned int given)
7956{
7957 return ((t->opcode_modifier.broadcast == BYTE_BROADCAST
7958 && i.types[given].bitfield.byte)
7959 || (t->opcode_modifier.broadcast == WORD_BROADCAST
7960 && i.types[given].bitfield.word)
7961 || (t->opcode_modifier.broadcast == DWORD_BROADCAST
7962 && i.types[given].bitfield.dword)
7963 || (t->opcode_modifier.broadcast == QWORD_BROADCAST
7964 && i.types[given].bitfield.qword));
7965}
7966
6c30d220
L
7967/* Check if operands are valid for the instruction. */
7968
7969static int
7970check_VecOperands (const insn_template *t)
7971{
43234a1e 7972 unsigned int op;
e2195274 7973 i386_cpu_flags cpu;
e2195274
JB
7974
7975 /* Templates allowing for ZMMword as well as YMMword and/or XMMword for
7976 any one operand are implicity requiring AVX512VL support if the actual
7977 operand size is YMMword or XMMword. Since this function runs after
a61cb9db
L
7978 template matching, there's no need to check for YMMword/XMMword in
7979 the template. */
734dfd1c 7980 cpu = cpu_flags_and (cpu_flags_from_attr (t->cpu), avx512);
e2195274 7981 if (!cpu_flags_all_zero (&cpu)
734dfd1c 7982 && !is_cpu (t, CpuAVX512VL)
a6f3add0 7983 && !cpu_arch_flags.bitfield.cpuavx512vl
6177c84d 7984 && (!t->opcode_modifier.vex || need_evex_encoding (t)))
e2195274
JB
7985 {
7986 for (op = 0; op < t->operands; ++op)
7987 {
7988 if (t->operand_types[op].bitfield.zmmword
7989 && (i.types[op].bitfield.ymmword
7990 || i.types[op].bitfield.xmmword))
7991 {
54294d73 7992 i.error = operand_size_mismatch;
e2195274
JB
7993 return 1;
7994 }
7995 }
7996 }
43234a1e 7997
22c36940
JB
7998 /* Somewhat similarly, templates specifying both AVX and AVX2 are
7999 requiring AVX2 support if the actual operand size is YMMword. */
a5e91879 8000 if (maybe_cpu (t, CpuAVX) && maybe_cpu (t, CpuAVX2)
22c36940
JB
8001 && !cpu_arch_flags.bitfield.cpuavx2)
8002 {
8003 for (op = 0; op < t->operands; ++op)
8004 {
8005 if (t->operand_types[op].bitfield.xmmword
8006 && i.types[op].bitfield.ymmword)
8007 {
54294d73 8008 i.error = operand_size_mismatch;
22c36940
JB
8009 return 1;
8010 }
8011 }
8012 }
8013
6c30d220 8014 /* Without VSIB byte, we can't have a vector register for index. */
63112cd6 8015 if (!t->opcode_modifier.sib
6c30d220 8016 && i.index_reg
1b54b8d7
JB
8017 && (i.index_reg->reg_type.bitfield.xmmword
8018 || i.index_reg->reg_type.bitfield.ymmword
8019 || i.index_reg->reg_type.bitfield.zmmword))
6c30d220
L
8020 {
8021 i.error = unsupported_vector_index_register;
8022 return 1;
8023 }
8024
ad8ecc81 8025 /* Check if default mask is allowed. */
255571cd 8026 if (t->opcode_modifier.operandconstraint == NO_DEFAULT_MASK
6225c532 8027 && (!i.mask.reg || i.mask.reg->reg_num == 0))
ad8ecc81
MZ
8028 {
8029 i.error = no_default_mask;
8030 return 1;
8031 }
8032
7bab8ab5
JB
8033 /* For VSIB byte, we need a vector register for index, and all vector
8034 registers must be distinct. */
260cd341 8035 if (t->opcode_modifier.sib && t->opcode_modifier.sib != SIBMEM)
7bab8ab5
JB
8036 {
8037 if (!i.index_reg
63112cd6 8038 || !((t->opcode_modifier.sib == VECSIB128
1b54b8d7 8039 && i.index_reg->reg_type.bitfield.xmmword)
63112cd6 8040 || (t->opcode_modifier.sib == VECSIB256
1b54b8d7 8041 && i.index_reg->reg_type.bitfield.ymmword)
63112cd6 8042 || (t->opcode_modifier.sib == VECSIB512
1b54b8d7 8043 && i.index_reg->reg_type.bitfield.zmmword)))
7bab8ab5
JB
8044 {
8045 i.error = invalid_vsib_address;
8046 return 1;
8047 }
8048
6225c532
JB
8049 gas_assert (i.reg_operands == 2 || i.mask.reg);
8050 if (i.reg_operands == 2 && !i.mask.reg)
43234a1e 8051 {
3528c362 8052 gas_assert (i.types[0].bitfield.class == RegSIMD);
1b54b8d7
JB
8053 gas_assert (i.types[0].bitfield.xmmword
8054 || i.types[0].bitfield.ymmword);
3528c362 8055 gas_assert (i.types[2].bitfield.class == RegSIMD);
1b54b8d7
JB
8056 gas_assert (i.types[2].bitfield.xmmword
8057 || i.types[2].bitfield.ymmword);
43234a1e
L
8058 if (operand_check == check_none)
8059 return 0;
8060 if (register_number (i.op[0].regs)
8061 != register_number (i.index_reg)
8062 && register_number (i.op[2].regs)
8063 != register_number (i.index_reg)
8064 && register_number (i.op[0].regs)
8065 != register_number (i.op[2].regs))
8066 return 0;
8067 if (operand_check == check_error)
8068 {
8069 i.error = invalid_vector_register_set;
8070 return 1;
8071 }
8072 as_warn (_("mask, index, and destination registers should be distinct"));
8073 }
6225c532 8074 else if (i.reg_operands == 1 && i.mask.reg)
8444f82a 8075 {
3528c362 8076 if (i.types[1].bitfield.class == RegSIMD
1b54b8d7
JB
8077 && (i.types[1].bitfield.xmmword
8078 || i.types[1].bitfield.ymmword
8079 || i.types[1].bitfield.zmmword)
8444f82a
MZ
8080 && (register_number (i.op[1].regs)
8081 == register_number (i.index_reg)))
8082 {
8083 if (operand_check == check_error)
8084 {
8085 i.error = invalid_vector_register_set;
8086 return 1;
8087 }
8088 if (operand_check != check_none)
8089 as_warn (_("index and destination registers should be distinct"));
8090 }
8091 }
43234a1e 8092 }
7bab8ab5 8093
fc141319
L
8094 /* For AMX instructions with 3 TMM register operands, all operands
8095 must be distinct. */
8096 if (i.reg_operands == 3
8097 && t->operand_types[0].bitfield.tmmword
8098 && (i.op[0].regs == i.op[1].regs
8099 || i.op[0].regs == i.op[2].regs
8100 || i.op[1].regs == i.op[2].regs))
8101 {
8102 i.error = invalid_tmm_register_set;
8103 return 1;
260cd341
LC
8104 }
8105
0cc78721
CL
8106 /* For some special instructions require that destination must be distinct
8107 from source registers. */
255571cd 8108 if (t->opcode_modifier.operandconstraint == DISTINCT_DEST)
0cc78721
CL
8109 {
8110 unsigned int dest_reg = i.operands - 1;
8111
8112 know (i.operands >= 3);
8113
8114 /* #UD if dest_reg == src1_reg or dest_reg == src2_reg. */
8115 if (i.op[dest_reg - 1].regs == i.op[dest_reg].regs
8116 || (i.reg_operands > 2
8117 && i.op[dest_reg - 2].regs == i.op[dest_reg].regs))
8118 {
8119 i.error = invalid_dest_and_src_register_set;
8120 return 1;
8121 }
8122 }
8123
43234a1e
L
8124 /* Check if broadcast is supported by the instruction and is applied
8125 to the memory operand. */
a5748e0d 8126 if (i.broadcast.type || i.broadcast.bytes)
43234a1e 8127 {
8e6e0792 8128 i386_operand_type type, overlap;
43234a1e
L
8129
8130 /* Check if specified broadcast is supported in this instruction,
4a1b91ea 8131 and its broadcast bytes match the memory operand. */
5273a3cd 8132 op = i.broadcast.operand;
8e6e0792 8133 if (!t->opcode_modifier.broadcast
c48dadc9 8134 || !(i.flags[op] & Operand_Mem)
c39e5b26 8135 || (!i.types[op].bitfield.unspecified
4a1b91ea 8136 && !match_broadcast_size (t, op)))
43234a1e
L
8137 {
8138 bad_broadcast:
8139 i.error = unsupported_broadcast;
8140 return 1;
8141 }
8e6e0792
JB
8142
8143 operand_type_set (&type, 0);
a5748e0d 8144 switch (get_broadcast_bytes (t, false))
8e6e0792 8145 {
4a1b91ea
L
8146 case 2:
8147 type.bitfield.word = 1;
8148 break;
8149 case 4:
8150 type.bitfield.dword = 1;
8151 break;
8e6e0792
JB
8152 case 8:
8153 type.bitfield.qword = 1;
8154 break;
8155 case 16:
8156 type.bitfield.xmmword = 1;
8157 break;
8158 case 32:
4fc85f37
JB
8159 if (vector_size < VSZ256)
8160 goto bad_broadcast;
8e6e0792
JB
8161 type.bitfield.ymmword = 1;
8162 break;
8163 case 64:
4fc85f37
JB
8164 if (vector_size < VSZ512)
8165 goto bad_broadcast;
8e6e0792
JB
8166 type.bitfield.zmmword = 1;
8167 break;
8168 default:
8169 goto bad_broadcast;
8170 }
8171
8172 overlap = operand_type_and (type, t->operand_types[op]);
bc49bfd8
JB
8173 if (t->operand_types[op].bitfield.class == RegSIMD
8174 && t->operand_types[op].bitfield.byte
8175 + t->operand_types[op].bitfield.word
8176 + t->operand_types[op].bitfield.dword
8177 + t->operand_types[op].bitfield.qword > 1)
8178 {
8179 overlap.bitfield.xmmword = 0;
8180 overlap.bitfield.ymmword = 0;
8181 overlap.bitfield.zmmword = 0;
8182 }
8e6e0792
JB
8183 if (operand_type_all_zero (&overlap))
8184 goto bad_broadcast;
8185
9c19e9ec 8186 if (t->opcode_modifier.checkoperandsize)
8e6e0792
JB
8187 {
8188 unsigned int j;
8189
e2195274 8190 type.bitfield.baseindex = 1;
8e6e0792
JB
8191 for (j = 0; j < i.operands; ++j)
8192 {
8193 if (j != op
8194 && !operand_type_register_match(i.types[j],
8195 t->operand_types[j],
8196 type,
8197 t->operand_types[op]))
8198 goto bad_broadcast;
8199 }
8200 }
43234a1e
L
8201 }
8202 /* If broadcast is supported in this instruction, we need to check if
8203 operand of one-element size isn't specified without broadcast. */
8204 else if (t->opcode_modifier.broadcast && i.mem_operands)
8205 {
8206 /* Find memory operand. */
8207 for (op = 0; op < i.operands; op++)
8dc0818e 8208 if (i.flags[op] & Operand_Mem)
43234a1e
L
8209 break;
8210 gas_assert (op < i.operands);
8211 /* Check size of the memory operand. */
4a1b91ea 8212 if (match_broadcast_size (t, op))
43234a1e
L
8213 {
8214 i.error = broadcast_needed;
8215 return 1;
8216 }
8217 }
c39e5b26
JB
8218 else
8219 op = MAX_OPERANDS - 1; /* Avoid uninitialized variable warning. */
43234a1e
L
8220
8221 /* Check if requested masking is supported. */
6225c532 8222 if (i.mask.reg)
43234a1e 8223 {
b1c79256 8224 if (!t->opcode_modifier.masking)
ae2387fe 8225 {
b1c79256
JB
8226 i.error = unsupported_masking;
8227 return 1;
8228 }
8229
8230 /* Common rules for masking:
8231 - mask register destinations permit only zeroing-masking, without
8232 that actually being expressed by a {z} operand suffix or EVEX.z,
8233 - memory destinations allow only merging-masking,
8234 - scatter/gather insns (i.e. ones using vSIB) only allow merging-
8235 masking. */
8236 if (i.mask.zeroing
8237 && (t->operand_types[t->operands - 1].bitfield.class == RegMask
8238 || (i.flags[t->operands - 1] & Operand_Mem)
8239 || t->opcode_modifier.sib))
8240 {
8241 i.error = unsupported_masking;
8242 return 1;
ae2387fe 8243 }
43234a1e
L
8244 }
8245
8246 /* Check if masking is applied to dest operand. */
6225c532 8247 if (i.mask.reg && (i.mask.operand != i.operands - 1))
43234a1e
L
8248 {
8249 i.error = mask_not_on_destination;
8250 return 1;
8251 }
8252
43234a1e 8253 /* Check RC/SAE. */
ca5312a2 8254 if (i.rounding.type != rc_none)
43234a1e 8255 {
a80195f1 8256 if (!t->opcode_modifier.sae
cf665fee
JB
8257 || ((i.rounding.type != saeonly) != t->opcode_modifier.staticrounding)
8258 || i.mem_operands)
43234a1e
L
8259 {
8260 i.error = unsupported_rc_sae;
8261 return 1;
8262 }
cf665fee
JB
8263
8264 /* Non-EVEX.LIG forms need to have a ZMM register as at least one
8265 operand. */
8266 if (t->opcode_modifier.evex != EVEXLIG)
7bab8ab5 8267 {
cf665fee
JB
8268 for (op = 0; op < t->operands; ++op)
8269 if (i.types[op].bitfield.zmmword)
8270 break;
8271 if (op >= t->operands)
8272 {
8273 i.error = operand_size_mismatch;
8274 return 1;
8275 }
7bab8ab5 8276 }
6c30d220
L
8277 }
8278
da4977e0 8279 /* Check the special Imm4 cases; must be the first operand. */
eea43579 8280 if ((is_cpu (t, CpuXOP) && t->operands == 5)
ec6b11e7
JB
8281 || (t->opcode_space == SPACE_0F3A
8282 && (t->base_opcode | 3) == 0x0b
24187fb9
JB
8283 && (is_cpu (t, CpuAPX_F)
8284 || (t->opcode_modifier.sse2avx && t->opcode_modifier.evex
8285 && (!t->opcode_modifier.vex
8286 || (i.encoding != encoding_default
8287 && i.encoding != encoding_vex
8288 && i.encoding != encoding_vex3))))))
da4977e0
JB
8289 {
8290 if (i.op[0].imms->X_op != O_constant
8291 || !fits_in_imm4 (i.op[0].imms->X_add_number))
8292 {
8293 i.error = bad_imm4;
8294 return 1;
8295 }
8296
8297 /* Turn off Imm<N> so that update_imm won't complain. */
eea43579
JB
8298 if (t->operands == 5)
8299 operand_type_set (&i.types[0], 0);
da4977e0
JB
8300 }
8301
43234a1e 8302 /* Check vector Disp8 operand. */
b5014f7a 8303 if (t->opcode_modifier.disp8memshift
a6f3add0 8304 && (!t->opcode_modifier.vex
6177c84d 8305 || need_evex_encoding (t))
1a42a9fe 8306 && i.disp_encoding <= disp_encoding_8bit)
43234a1e 8307 {
9b345ce8 8308 if (i.broadcast.type || i.broadcast.bytes)
4a1b91ea 8309 i.memshift = t->opcode_modifier.broadcast - 1;
7091c612 8310 else if (t->opcode_modifier.disp8memshift != DISP8_SHIFT_VL)
43234a1e 8311 i.memshift = t->opcode_modifier.disp8memshift;
7091c612
JB
8312 else
8313 {
125ff819 8314 const i386_operand_type *type = NULL, *fallback = NULL;
7091c612
JB
8315
8316 i.memshift = 0;
8317 for (op = 0; op < i.operands; op++)
8dc0818e 8318 if (i.flags[op] & Operand_Mem)
7091c612 8319 {
4174bfff
JB
8320 if (t->opcode_modifier.evex == EVEXLIG)
8321 i.memshift = 2 + (i.suffix == QWORD_MNEM_SUFFIX);
8322 else if (t->operand_types[op].bitfield.xmmword
8323 + t->operand_types[op].bitfield.ymmword
8324 + t->operand_types[op].bitfield.zmmword <= 1)
7091c612
JB
8325 type = &t->operand_types[op];
8326 else if (!i.types[op].bitfield.unspecified)
8327 type = &i.types[op];
125ff819
JB
8328 else /* Ambiguities get resolved elsewhere. */
8329 fallback = &t->operand_types[op];
7091c612 8330 }
3528c362 8331 else if (i.types[op].bitfield.class == RegSIMD
4174bfff 8332 && t->opcode_modifier.evex != EVEXLIG)
7091c612
JB
8333 {
8334 if (i.types[op].bitfield.zmmword)
8335 i.memshift = 6;
8336 else if (i.types[op].bitfield.ymmword && i.memshift < 5)
8337 i.memshift = 5;
8338 else if (i.types[op].bitfield.xmmword && i.memshift < 4)
8339 i.memshift = 4;
8340 }
8341
125ff819
JB
8342 if (!type && !i.memshift)
8343 type = fallback;
7091c612
JB
8344 if (type)
8345 {
8346 if (type->bitfield.zmmword)
8347 i.memshift = 6;
8348 else if (type->bitfield.ymmword)
8349 i.memshift = 5;
8350 else if (type->bitfield.xmmword)
8351 i.memshift = 4;
8352 }
8353
8354 /* For the check in fits_in_disp8(). */
8355 if (i.memshift == 0)
8356 i.memshift = -1;
8357 }
43234a1e
L
8358
8359 for (op = 0; op < i.operands; op++)
8360 if (operand_type_check (i.types[op], disp)
8361 && i.op[op].disps->X_op == O_constant)
8362 {
b5014f7a 8363 if (fits_in_disp8 (i.op[op].disps->X_add_number))
43234a1e 8364 {
b5014f7a
JB
8365 i.types[op].bitfield.disp8 = 1;
8366 return 0;
43234a1e 8367 }
b5014f7a 8368 i.types[op].bitfield.disp8 = 0;
43234a1e
L
8369 }
8370 }
b5014f7a
JB
8371
8372 i.memshift = 0;
43234a1e 8373
6c30d220
L
8374 return 0;
8375}
8376
da4977e0 8377/* Check if encoding requirements are met by the instruction. */
a683cc34
SP
8378
8379static int
da4977e0 8380VEX_check_encoding (const insn_template *t)
a683cc34 8381{
e346d50a 8382 if (i.encoding == encoding_error)
da4977e0
JB
8383 {
8384 i.error = unsupported;
8385 return 1;
8386 }
8387
4fc85f37
JB
8388 /* Vector size restrictions. */
8389 if ((vector_size < VSZ512
fa88a361 8390 && t->opcode_modifier.evex == EVEX512)
4fc85f37
JB
8391 || (vector_size < VSZ256
8392 && (t->opcode_modifier.evex == EVEX256
fa88a361 8393 || t->opcode_modifier.vex == VEX256)))
4fc85f37 8394 {
54294d73 8395 i.error = unsupported_vector_size;
4fc85f37
JB
8396 return 1;
8397 }
8398
eb3f3841 8399 switch (i.encoding)
43234a1e 8400 {
eb3f3841
JB
8401 case encoding_default:
8402 break;
8403
8404 case encoding_vex:
8405 case encoding_vex3:
8406 /* This instruction must be encoded with VEX prefix. */
8407 if (!t->opcode_modifier.vex)
8408 {
8409 i.error = no_vex_encoding;
8410 return 1;
8411 }
8412 break;
8413
8414 case encoding_evex:
8415 case encoding_evex512:
86fa6981 8416 /* This instruction must be encoded with EVEX prefix. */
706ce984 8417 if (!t->opcode_modifier.evex)
86fa6981 8418 {
54294d73 8419 i.error = no_evex_encoding;
86fa6981
L
8420 return 1;
8421 }
eb3f3841 8422 break;
43234a1e 8423
eb3f3841
JB
8424 case encoding_egpr:
8425 /* This instruction must be encoded with REX2 or EVEX prefix. */
8426 if (t->opcode_modifier.vex && !t->opcode_modifier.evex)
86fa6981 8427 {
eb3f3841 8428 i.error = no_evex_encoding;
86fa6981
L
8429 return 1;
8430 }
eb3f3841
JB
8431 break;
8432
8433 default:
8434 abort ();
86fa6981 8435 }
a683cc34 8436
a683cc34
SP
8437 return 0;
8438}
8439
80d61d8d
CL
8440/* Check if Egprs operands are valid for the instruction. */
8441
8442static bool
8443check_EgprOperands (const insn_template *t)
8444{
8445 if (!t->opcode_modifier.noegpr)
192781a3 8446 return false;
80d61d8d
CL
8447
8448 for (unsigned int op = 0; op < i.operands; op++)
8449 {
8450 if (i.types[op].bitfield.class != Reg)
8451 continue;
8452
8453 if (i.op[op].regs->reg_flags & RegRex2)
8454 {
8455 i.error = register_type_mismatch;
192781a3 8456 return true;
80d61d8d
CL
8457 }
8458 }
8459
8460 if ((i.index_reg && (i.index_reg->reg_flags & RegRex2))
8461 || (i.base_reg && (i.base_reg->reg_flags & RegRex2)))
8462 {
8463 i.error = unsupported_EGPR_for_addressing;
192781a3 8464 return true;
80d61d8d
CL
8465 }
8466
8467 /* Check if pseudo prefix {rex2} is valid. */
ebe82bfd 8468 if (i.rex2_encoding && !t->opcode_modifier.sse2avx)
80d61d8d
CL
8469 {
8470 i.error = invalid_pseudo_prefix;
192781a3 8471 return true;
80d61d8d
CL
8472 }
8473
192781a3 8474 return false;
80d61d8d
CL
8475}
8476
08a98d4c
MZ
8477/* Check if APX operands are valid for the instruction. */
8478static bool
8479check_APX_operands (const insn_template *t)
8480{
8481 /* Push2* and Pop2* cannot use RSP and Pop2* cannot pop two same registers.
8482 */
8483 switch (t->mnem_off)
8484 {
8485 case MN_pop2:
8486 case MN_pop2p:
8487 if (register_number (i.op[0].regs) == register_number (i.op[1].regs))
8488 {
8489 i.error = invalid_dest_register_set;
8490 return 1;
8491 }
8492 /* fall through */
8493 case MN_push2:
8494 case MN_push2p:
8495 if (register_number (i.op[0].regs) == 4
8496 || register_number (i.op[1].regs) == 4)
8497 {
8498 i.error = unsupported_rsp_register;
8499 return 1;
8500 }
8501 break;
8502 }
8503 return 0;
8504}
8505
6967f19d
HL
8506/* Check if the instruction use the REX registers or REX prefix. */
8507static bool
8508check_Rex_required (void)
8509{
8510 for (unsigned int op = 0; op < i.operands; op++)
8511 {
8512 if (i.types[op].bitfield.class != Reg)
8513 continue;
8514
8515 if (i.op[op].regs->reg_flags & (RegRex | RegRex64))
8516 return true;
8517 }
8518
8519 if ((i.index_reg && (i.index_reg->reg_flags & (RegRex | RegRex64)))
8520 || (i.base_reg && (i.base_reg->reg_flags & (RegRex | RegRex64))))
8521 return true;
8522
8523 /* Check pseudo prefix {rex} are valid. */
8524 return i.rex_encoding;
8525}
8526
8527/* Optimize APX NDD insns to legacy insns. */
8528static unsigned int
8529can_convert_NDD_to_legacy (const insn_template *t)
8530{
8531 unsigned int match_dest_op = ~0;
8532
dd74a603 8533 if (!i.has_nf && i.reg_operands >= 2)
6967f19d
HL
8534 {
8535 unsigned int dest = i.operands - 1;
8536 unsigned int src1 = i.operands - 2;
8537 unsigned int src2 = (i.operands > 3) ? i.operands - 3 : 0;
8538
8539 if (i.types[src1].bitfield.class == Reg
8540 && i.op[src1].regs == i.op[dest].regs)
8541 match_dest_op = src1;
8542 /* If the first operand is the same as the third operand,
8543 these instructions need to support the ability to commutative
8544 the first two operands and still not change the semantics in order
8545 to be optimized. */
8546 else if (optimize > 1
8547 && t->opcode_modifier.commutative
8548 && i.types[src2].bitfield.class == Reg
8549 && i.op[src2].regs == i.op[dest].regs)
8550 match_dest_op = src2;
8551 }
8552 return match_dest_op;
8553}
8554
7b94647a
JB
8555/* Helper function for the progress() macro in match_template(). */
8556static INLINE enum i386_error progress (enum i386_error new,
8557 enum i386_error last,
8558 unsigned int line, unsigned int *line_p)
8559{
8560 if (line <= *line_p)
8561 return last;
8562 *line_p = line;
8563 return new;
8564}
8565
d3ce72d0 8566static const insn_template *
83b16ac6 8567match_template (char mnem_suffix)
29b0f896
AM
8568{
8569 /* Points to template once we've found it. */
d3ce72d0 8570 const insn_template *t;
40fb9820 8571 i386_operand_type overlap0, overlap1, overlap2, overlap3;
c0f3af97 8572 i386_operand_type overlap4;
29b0f896 8573 unsigned int found_reverse_match;
40fb9820 8574 i386_operand_type operand_types [MAX_OPERANDS];
539e75ad 8575 int addr_prefix_disp;
7b94647a
JB
8576 unsigned int j, size_match, check_register, errline = __LINE__;
8577 enum i386_error specific_error = number_of_operands_mismatch;
8578#define progress(err) progress (err, specific_error, __LINE__, &errline)
29b0f896 8579
c0f3af97
L
8580#if MAX_OPERANDS != 5
8581# error "MAX_OPERANDS must be 5."
f48ff2ae
L
8582#endif
8583
29b0f896 8584 found_reverse_match = 0;
539e75ad 8585 addr_prefix_disp = -1;
40fb9820 8586
d3b01414 8587 for (t = current_templates.start; t < current_templates.end; t++)
29b0f896 8588 {
539e75ad 8589 addr_prefix_disp = -1;
dbbc8b7e 8590 found_reverse_match = 0;
539e75ad 8591
7b94647a 8592 /* Must have right number of operands. */
29b0f896
AM
8593 if (i.operands != t->operands)
8594 continue;
8595
b58829cd
JB
8596 /* Skip SSE2AVX templates when inapplicable. */
8597 if (t->opcode_modifier.sse2avx
8598 && (!sse2avx || i.prefix[DATA_PREFIX]))
8599 {
8600 /* Another non-SSE2AVX template has to follow. */
8601 gas_assert (t + 1 < current_templates.end);
8602 continue;
8603 }
8604
50aecf8c 8605 /* Check processor support. */
7b94647a 8606 specific_error = progress (unsupported);
45a4bb20 8607 if (cpu_flags_match (t) != CPU_FLAGS_PERFECT_MATCH)
50aecf8c
L
8608 continue;
8609
e1d4d893 8610 /* Check AT&T mnemonic. */
7b94647a 8611 specific_error = progress (unsupported_with_intel_mnemonic);
35266cb1
JB
8612 if (!intel_syntax && intel_mnemonic
8613 && t->opcode_modifier.dialect == ATT_MNEMONIC)
1efbbeb4
L
8614 continue;
8615
4b5aaf5f 8616 /* Check AT&T/Intel syntax. */
7b94647a 8617 specific_error = progress (unsupported_syntax);
7d3182d6 8618 if (intel_syntax
35266cb1
JB
8619 ? t->opcode_modifier.dialect >= ATT_SYNTAX
8620 : t->opcode_modifier.dialect == INTEL_SYNTAX)
1efbbeb4
L
8621 continue;
8622
dd74a603
CL
8623 /* Check NF support. */
8624 specific_error = progress (unsupported_nf);
8625 if (i.has_nf && !t->opcode_modifier.nf)
8626 continue;
8627
4b5aaf5f
L
8628 /* Check Intel64/AMD64 ISA. */
8629 switch (isa64)
8630 {
8631 default:
8632 /* Default: Don't accept Intel64. */
8633 if (t->opcode_modifier.isa64 == INTEL64)
8634 continue;
8635 break;
8636 case amd64:
8637 /* -mamd64: Don't accept Intel64 and Intel64 only. */
8638 if (t->opcode_modifier.isa64 >= INTEL64)
8639 continue;
8640 break;
8641 case intel64:
8642 /* -mintel64: Don't accept AMD64. */
5990e377 8643 if (t->opcode_modifier.isa64 == AMD64 && flag_code == CODE_64BIT)
4b5aaf5f
L
8644 continue;
8645 break;
8646 }
8647
dc2be329 8648 /* Check the suffix. */
7b94647a 8649 specific_error = progress (invalid_instruction_suffix);
7505bb03
JB
8650 if ((t->opcode_modifier.no_bsuf && mnem_suffix == BYTE_MNEM_SUFFIX)
8651 || (t->opcode_modifier.no_wsuf && mnem_suffix == WORD_MNEM_SUFFIX)
8652 || (t->opcode_modifier.no_lsuf && mnem_suffix == LONG_MNEM_SUFFIX)
8653 || (t->opcode_modifier.no_ssuf && mnem_suffix == SHORT_MNEM_SUFFIX)
8654 || (t->opcode_modifier.no_qsuf && mnem_suffix == QWORD_MNEM_SUFFIX))
83b16ac6 8655 continue;
29b0f896 8656
7b94647a 8657 specific_error = progress (operand_size_mismatch);
3ac21baa
JB
8658 size_match = operand_size_match (t);
8659 if (!size_match)
7d5e4556 8660 continue;
539e75ad 8661
6f2f06be
JB
8662 /* This is intentionally not
8663
0cfa3eb3 8664 if (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE))
6f2f06be
JB
8665
8666 as the case of a missing * on the operand is accepted (perhaps with
8667 a warning, issued further down). */
7b94647a 8668 specific_error = progress (operand_type_mismatch);
0cfa3eb3 8669 if (i.jumpabsolute && t->opcode_modifier.jump != JUMP_ABSOLUTE)
7b94647a 8670 continue;
6f2f06be 8671
a4d3acd2
JB
8672 /* In Intel syntax, normally we can check for memory operand size when
8673 there is no mnemonic suffix. But jmp and call have 2 different
8674 encodings with Dword memory operand size. Skip the "near" one
8675 (permitting a register operand) when "far" was requested. */
8676 if (i.far_branch
8677 && t->opcode_modifier.jump == JUMP_ABSOLUTE
8678 && t->operand_types[0].bitfield.class == Reg)
8679 continue;
8680
5c07affc
L
8681 for (j = 0; j < MAX_OPERANDS; j++)
8682 operand_types[j] = t->operand_types[j];
8683
9db83a32 8684 /* In general, don't allow 32-bit operands on pre-386. */
7b94647a
JB
8685 specific_error = progress (mnem_suffix ? invalid_instruction_suffix
8686 : operand_size_mismatch);
4873e243 8687 j = i.imm_operands + (t->operands > i.imm_operands + 1);
9db83a32
JB
8688 if (i.suffix == LONG_MNEM_SUFFIX
8689 && !cpu_arch_flags.bitfield.cpui386
45aa61fe 8690 && (intel_syntax
3cd7f3e3 8691 ? (t->opcode_modifier.mnemonicsize != IGNORESIZE
76d3f746
JB
8692 && !intel_float_operand (insn_name (t)))
8693 : intel_float_operand (insn_name (t)) != 2)
4873e243
JB
8694 && (t->operands == i.imm_operands
8695 || (operand_types[i.imm_operands].bitfield.class != RegMMX
8696 && operand_types[i.imm_operands].bitfield.class != RegSIMD
8697 && operand_types[i.imm_operands].bitfield.class != RegMask)
8698 || (operand_types[j].bitfield.class != RegMMX
8699 && operand_types[j].bitfield.class != RegSIMD
8700 && operand_types[j].bitfield.class != RegMask))
63112cd6 8701 && !t->opcode_modifier.sib)
192dc9c6
JB
8702 continue;
8703
29b0f896 8704 /* Do not verify operands when there are none. */
e365e234 8705 if (!t->operands)
da4977e0
JB
8706 {
8707 if (VEX_check_encoding (t))
8708 {
7b94647a 8709 specific_error = progress (i.error);
da4977e0
JB
8710 continue;
8711 }
8712
80d61d8d
CL
8713 /* Check if pseudo prefix {rex2} is valid. */
8714 if (t->opcode_modifier.noegpr && i.rex2_encoding)
8715 {
8716 specific_error = progress (invalid_pseudo_prefix);
8717 continue;
8718 }
8719
da4977e0
JB
8720 /* We've found a match; break out of loop. */
8721 break;
8722 }
252b5132 8723
48bcea9f
JB
8724 if (!t->opcode_modifier.jump
8725 || t->opcode_modifier.jump == JUMP_ABSOLUTE)
8726 {
8727 /* There should be only one Disp operand. */
8728 for (j = 0; j < MAX_OPERANDS; j++)
8729 if (operand_type_check (operand_types[j], disp))
539e75ad 8730 break;
48bcea9f
JB
8731 if (j < MAX_OPERANDS)
8732 {
5b7c81bd 8733 bool override = (i.prefix[ADDR_PREFIX] != 0);
48bcea9f
JB
8734
8735 addr_prefix_disp = j;
8736
a775efc8
JB
8737 /* Address size prefix will turn Disp64 operand into Disp32 and
8738 Disp32/Disp16 one into Disp16/Disp32 respectively. */
48bcea9f 8739 switch (flag_code)
40fb9820 8740 {
48bcea9f
JB
8741 case CODE_16BIT:
8742 override = !override;
8743 /* Fall through. */
8744 case CODE_32BIT:
8745 if (operand_types[j].bitfield.disp32
8746 && operand_types[j].bitfield.disp16)
40fb9820 8747 {
48bcea9f
JB
8748 operand_types[j].bitfield.disp16 = override;
8749 operand_types[j].bitfield.disp32 = !override;
40fb9820 8750 }
a775efc8 8751 gas_assert (!operand_types[j].bitfield.disp64);
48bcea9f
JB
8752 break;
8753
8754 case CODE_64BIT:
a775efc8 8755 if (operand_types[j].bitfield.disp64)
40fb9820 8756 {
a775efc8 8757 gas_assert (!operand_types[j].bitfield.disp32);
48bcea9f 8758 operand_types[j].bitfield.disp32 = override;
a775efc8 8759 operand_types[j].bitfield.disp64 = !override;
40fb9820 8760 }
48bcea9f
JB
8761 operand_types[j].bitfield.disp16 = 0;
8762 break;
40fb9820 8763 }
539e75ad 8764 }
48bcea9f 8765 }
539e75ad 8766
56ffb741 8767 /* We check register size if needed. */
9c19e9ec 8768 if (t->opcode_modifier.checkoperandsize)
e2195274
JB
8769 {
8770 check_register = (1 << t->operands) - 1;
a5748e0d 8771 if (i.broadcast.type || i.broadcast.bytes)
5273a3cd 8772 check_register &= ~(1 << i.broadcast.operand);
e2195274
JB
8773 }
8774 else
8775 check_register = 0;
8776
c6fb90c8 8777 overlap0 = operand_type_and (i.types[0], operand_types[0]);
29b0f896
AM
8778 switch (t->operands)
8779 {
8780 case 1:
40fb9820 8781 if (!operand_type_match (overlap0, i.types[0]))
29b0f896 8782 continue;
ae9a0a51
JB
8783
8784 /* Allow the ModR/M encoding to be requested by using the {load} or
8785 {store} pseudo prefix on an applicable insn. */
8786 if (!t->opcode_modifier.modrm
8787 && i.reg_operands == 1
8788 && ((i.dir_encoding == dir_encoding_load
8789 && t->mnem_off != MN_pop)
8790 || (i.dir_encoding == dir_encoding_store
8791 && t->mnem_off != MN_push))
8792 /* Avoid BSWAP. */
8793 && t->mnem_off != MN_bswap)
8794 continue;
29b0f896 8795 break;
ae9a0a51 8796
29b0f896 8797 case 2:
33eaf5de 8798 /* xchg %eax, %eax is a special case. It is an alias for nop
8b38ad71
L
8799 only in 32bit mode and we can use opcode 0x90. In 64bit
8800 mode, we can't use 0x90 for xchg %eax, %eax since it should
8801 zero-extend %eax to %rax. */
ae9a0a51
JB
8802 if (t->base_opcode == 0x90
8803 && t->opcode_space == SPACE_BASE)
8804 {
8805 if (flag_code == CODE_64BIT
8806 && i.types[0].bitfield.instance == Accum
8807 && i.types[0].bitfield.dword
8808 && i.types[1].bitfield.instance == Accum)
8809 continue;
8810
8811 /* Allow the ModR/M encoding to be requested by using the
8812 {load} or {store} pseudo prefix. */
8813 if (i.dir_encoding == dir_encoding_load
8814 || i.dir_encoding == dir_encoding_store)
8815 continue;
8816 }
e3669c7f
JB
8817
8818 if (t->base_opcode == MOV_AX_DISP32
ddb62495 8819 && t->opcode_space == SPACE_BASE
69196391 8820 && t->mnem_off != MN_movabs)
e3669c7f
JB
8821 {
8822 /* Force 0x8b encoding for "mov foo@GOT, %eax". */
8823 if (i.reloc[0] == BFD_RELOC_386_GOT32)
8824 continue;
8825
8826 /* xrelease mov %eax, <disp> is another special case. It must not
8827 match the accumulator-only encoding of mov. */
8828 if (i.hle_prefix)
8829 continue;
ae9a0a51
JB
8830
8831 /* Allow the ModR/M encoding to be requested by using a suitable
8832 {load} or {store} pseudo prefix. */
8833 if (i.dir_encoding == (i.types[0].bitfield.instance == Accum
8834 ? dir_encoding_store
8835 : dir_encoding_load)
8836 && !i.types[0].bitfield.disp64
8837 && !i.types[1].bitfield.disp64)
8838 continue;
8839 }
8840
8841 /* Allow the ModR/M encoding to be requested by using the {load} or
8842 {store} pseudo prefix on an applicable insn. */
8843 if (!t->opcode_modifier.modrm
8844 && i.reg_operands == 1
8845 && i.imm_operands == 1
8846 && (i.dir_encoding == dir_encoding_load
8847 || i.dir_encoding == dir_encoding_store)
8848 && t->opcode_space == SPACE_BASE)
8849 {
8850 if (t->base_opcode == 0xb0 /* mov $imm, %reg */
8851 && i.dir_encoding == dir_encoding_store)
8852 continue;
8853
8854 if ((t->base_opcode | 0x38) == 0x3c /* <alu> $imm, %acc */
8855 && (t->base_opcode != 0x3c /* cmp $imm, %acc */
8856 || i.dir_encoding == dir_encoding_load))
8857 continue;
8858
8859 if (t->base_opcode == 0xa8 /* test $imm, %acc */
8860 && i.dir_encoding == dir_encoding_load)
8861 continue;
e3669c7f 8862 }
f5eb1d70
JB
8863 /* Fall through. */
8864
8865 case 3:
3ac21baa
JB
8866 if (!(size_match & MATCH_STRAIGHT))
8867 goto check_reverse;
64c49ab3
JB
8868 /* Reverse direction of operands if swapping is possible in the first
8869 place (operands need to be symmetric) and
8870 - the load form is requested, and the template is a store form,
8871 - the store form is requested, and the template is a load form,
8872 - the non-default (swapped) form is requested. */
8873 overlap1 = operand_type_and (operand_types[0], operand_types[1]);
3083f376 8874
8875 j = i.operands - 1 - (t->opcode_space == SPACE_EVEXMAP4
8876 && t->opcode_modifier.vexvvvv);
8877
f5eb1d70 8878 if (t->opcode_modifier.d && i.reg_operands == i.operands
64c49ab3
JB
8879 && !operand_type_all_zero (&overlap1))
8880 switch (i.dir_encoding)
8881 {
8882 case dir_encoding_load:
3083f376 8883 if (operand_type_check (operand_types[j], anymem)
dfd69174 8884 || t->opcode_modifier.regmem)
64c49ab3
JB
8885 goto check_reverse;
8886 break;
8887
8888 case dir_encoding_store:
3083f376 8889 if (!operand_type_check (operand_types[j], anymem)
dfd69174 8890 && !t->opcode_modifier.regmem)
64c49ab3
JB
8891 goto check_reverse;
8892 break;
8893
8894 case dir_encoding_swap:
8895 goto check_reverse;
8896
8897 case dir_encoding_default:
8898 break;
8899 }
3083f376 8900
86fa6981 8901 /* If we want store form, we skip the current load. */
64c49ab3
JB
8902 if ((i.dir_encoding == dir_encoding_store
8903 || i.dir_encoding == dir_encoding_swap)
86fa6981
L
8904 && i.mem_operands == 0
8905 && t->opcode_modifier.load)
fa99fab2 8906 continue;
1a0670f3 8907 /* Fall through. */
f48ff2ae 8908 case 4:
c0f3af97 8909 case 5:
c6fb90c8 8910 overlap1 = operand_type_and (i.types[1], operand_types[1]);
40fb9820
L
8911 if (!operand_type_match (overlap0, i.types[0])
8912 || !operand_type_match (overlap1, i.types[1])
e2195274 8913 || ((check_register & 3) == 3
dc821c5f 8914 && !operand_type_register_match (i.types[0],
40fb9820 8915 operand_types[0],
dc821c5f 8916 i.types[1],
40fb9820 8917 operand_types[1])))
29b0f896 8918 {
7b94647a
JB
8919 specific_error = progress (i.error);
8920
29b0f896 8921 /* Check if other direction is valid ... */
38e314eb 8922 if (!t->opcode_modifier.d)
29b0f896
AM
8923 continue;
8924
dc1e8a47 8925 check_reverse:
3ac21baa
JB
8926 if (!(size_match & MATCH_REVERSE))
8927 continue;
29b0f896 8928 /* Try reversing direction of operands. */
734dfd1c 8929 j = is_cpu (t, CpuFMA4)
3083f376 8930 || is_cpu (t, CpuXOP)
8931 || is_cpu (t, CpuAPX_F) ? 1 : i.operands - 1;
8bd915b7
JB
8932 overlap0 = operand_type_and (i.types[0], operand_types[j]);
8933 overlap1 = operand_type_and (i.types[j], operand_types[0]);
c975cec5 8934 overlap2 = operand_type_and (i.types[1], operand_types[1]);
3083f376 8935 gas_assert (t->operands != 3 || !check_register
8936 || is_cpu (t, CpuAPX_F));
40fb9820 8937 if (!operand_type_match (overlap0, i.types[0])
8bd915b7 8938 || !operand_type_match (overlap1, i.types[j])
c975cec5
JB
8939 || (t->operands == 3
8940 && !operand_type_match (overlap2, i.types[1]))
45664ddb 8941 || (check_register
dc821c5f 8942 && !operand_type_register_match (i.types[0],
8bd915b7
JB
8943 operand_types[j],
8944 i.types[j],
45664ddb 8945 operand_types[0])))
29b0f896
AM
8946 {
8947 /* Does not match either direction. */
7b94647a 8948 specific_error = progress (i.error);
29b0f896
AM
8949 continue;
8950 }
ac9226cf 8951 /* found_reverse_match holds which variant of D
29b0f896 8952 we've found. */
38e314eb
JB
8953 if (!t->opcode_modifier.d)
8954 found_reverse_match = 0;
8955 else if (operand_types[0].bitfield.tbyte)
ac9226cf 8956 {
4943d587
JB
8957 if (t->opcode_modifier.operandconstraint != UGH)
8958 found_reverse_match = Opcode_FloatD;
bd782808
JB
8959 else
8960 found_reverse_match = ~0;
ac9226cf 8961 /* FSUB{,R} and FDIV{,R} may need a 2nd bit flipped. */
bd782808 8962 if ((t->extension_opcode & 4)
ac9226cf
JB
8963 && (intel_syntax || intel_mnemonic))
8964 found_reverse_match |= Opcode_FloatR;
8965 }
734dfd1c 8966 else if (is_cpu (t, CpuFMA4) || is_cpu (t, CpuXOP))
8bd915b7
JB
8967 {
8968 found_reverse_match = Opcode_VexW;
8969 goto check_operands_345;
8970 }
dd74a603
CL
8971 else if (t->opcode_space == SPACE_EVEXMAP4
8972 && t->opcode_modifier.w)
3083f376 8973 {
8974 found_reverse_match = Opcode_D;
8975 goto check_operands_345;
8976 }
ddb62495
JB
8977 else if (t->opcode_space != SPACE_BASE
8978 && (t->opcode_space != SPACE_0F
2c735193
JB
8979 /* MOV to/from CR/DR/TR, as an exception, follow
8980 the base opcode space encoding model. */
8981 || (t->base_opcode | 7) != 0x27))
dbbc8b7e 8982 found_reverse_match = (t->base_opcode & 0xee) != 0x6e
2c735193 8983 ? Opcode_ExtD : Opcode_SIMD_IntD;
a33ef3c2 8984 else if (!t->opcode_modifier.commutative)
38e314eb 8985 found_reverse_match = Opcode_D;
a33ef3c2
JB
8986 else
8987 found_reverse_match = ~0;
29b0f896 8988 }
f48ff2ae 8989 else
29b0f896 8990 {
f48ff2ae 8991 /* Found a forward 2 operand match here. */
8bd915b7 8992 check_operands_345:
d1cbb4db
L
8993 switch (t->operands)
8994 {
c0f3af97 8995 case 5:
3d0738af 8996 overlap4 = operand_type_and (i.types[4], operand_types[4]);
c0f3af97 8997 if (!operand_type_match (overlap4, i.types[4])
dc821c5f 8998 || !operand_type_register_match (i.types[3],
c0f3af97 8999 operand_types[3],
c0f3af97
L
9000 i.types[4],
9001 operand_types[4]))
7b94647a
JB
9002 {
9003 specific_error = progress (i.error);
9004 continue;
9005 }
1a0670f3 9006 /* Fall through. */
f48ff2ae 9007 case 4:
3d0738af 9008 overlap3 = operand_type_and (i.types[3], operand_types[3]);
40fb9820 9009 if (!operand_type_match (overlap3, i.types[3])
e2195274
JB
9010 || ((check_register & 0xa) == 0xa
9011 && !operand_type_register_match (i.types[1],
f7768225
JB
9012 operand_types[1],
9013 i.types[3],
e2195274
JB
9014 operand_types[3]))
9015 || ((check_register & 0xc) == 0xc
9016 && !operand_type_register_match (i.types[2],
9017 operand_types[2],
9018 i.types[3],
9019 operand_types[3])))
7b94647a
JB
9020 {
9021 specific_error = progress (i.error);
9022 continue;
9023 }
1a0670f3 9024 /* Fall through. */
f48ff2ae 9025 case 3:
3d0738af 9026 overlap2 = operand_type_and (i.types[2], operand_types[2]);
40fb9820 9027 if (!operand_type_match (overlap2, i.types[2])
e2195274
JB
9028 || ((check_register & 5) == 5
9029 && !operand_type_register_match (i.types[0],
23e42951
JB
9030 operand_types[0],
9031 i.types[2],
e2195274
JB
9032 operand_types[2]))
9033 || ((check_register & 6) == 6
9034 && !operand_type_register_match (i.types[1],
9035 operand_types[1],
9036 i.types[2],
9037 operand_types[2])))
7b94647a
JB
9038 {
9039 specific_error = progress (i.error);
9040 continue;
9041 }
f48ff2ae
L
9042 break;
9043 }
29b0f896 9044 }
f48ff2ae 9045 /* Found either forward/reverse 2, 3 or 4 operand match here:
29b0f896
AM
9046 slip through to break. */
9047 }
c0f3af97 9048
9bb4d860
L
9049 /* Check if VEX/EVEX encoding requirements can be satisfied. */
9050 if (VEX_check_encoding (t))
da4977e0 9051 {
7b94647a 9052 specific_error = progress (i.error);
da4977e0
JB
9053 continue;
9054 }
9055
80d61d8d
CL
9056 /* Check if EGPR operands(r16-r31) are valid. */
9057 if (check_EgprOperands (t))
9058 {
9059 specific_error = progress (i.error);
9060 continue;
9061 }
9062
9bb4d860
L
9063 /* Check if vector operands are valid. */
9064 if (check_VecOperands (t))
5614d22c 9065 {
7b94647a 9066 specific_error = progress (i.error);
5614d22c
JB
9067 continue;
9068 }
a683cc34 9069
08a98d4c
MZ
9070 /* Check if APX operands are valid. */
9071 if (check_APX_operands (t))
9072 {
9073 specific_error = progress (i.error);
9074 continue;
9075 }
9076
58bceb18 9077 /* Check whether to use the shorter VEX encoding for certain insns where
39bb3ade
JB
9078 the EVEX encoding comes first in the table. This requires the respective
9079 AVX-* feature to be explicitly enabled.
9080
9081 Most of the respective insns have just a single EVEX and a single VEX
9082 template. The one that's presently different is generated using the
9083 Vxy / Exy constructs: There are 3 suffix-less EVEX forms, the latter
9084 two of which may fall back to their two corresponding VEX forms. */
9085 j = t->mnem_off != MN_vcvtneps2bf16 ? 1 : 2;
d3b01414 9086 if ((t == current_templates.start || j > 1)
58bceb18
JB
9087 && t->opcode_modifier.disp8memshift
9088 && !t->opcode_modifier.vex
6177c84d 9089 && !need_evex_encoding (t)
d3b01414 9090 && t + j < current_templates.end
39bb3ade 9091 && t[j].opcode_modifier.vex)
58bceb18
JB
9092 {
9093 i386_cpu_flags cpu;
9094 unsigned int memshift = i.memshift;
9095
9096 i.memshift = 0;
39bb3ade
JB
9097 cpu = cpu_flags_and (cpu_flags_from_attr (t[j].cpu),
9098 cpu_arch_isa_flags);
58bceb18
JB
9099 if (!cpu_flags_all_zero (&cpu)
9100 && (!i.types[0].bitfield.disp8
9101 || !operand_type_check (i.types[0], disp)
9102 || i.op[0].disps->X_op != O_constant
9103 || fits_in_disp8 (i.op[0].disps->X_add_number)))
9104 {
9105 specific_error = progress (internal_error);
39bb3ade 9106 t += j - 1;
58bceb18
JB
9107 continue;
9108 }
9109 i.memshift = memshift;
9110 }
9111
6967f19d
HL
9112 /* If we can optimize a NDD insn to legacy insn, like
9113 add %r16, %r8, %r8 -> add %r16, %r8,
9114 add %r8, %r16, %r8 -> add %r16, %r8, then rematch template.
9115 Note that the semantics have not been changed. */
9116 if (optimize
9117 && !i.no_optimize
e346d50a 9118 && i.encoding != encoding_evex
0ebcbb1b
JB
9119 && ((t + 1 < current_templates.end
9120 && !t[1].opcode_modifier.evex
9121 && t[1].opcode_space <= SPACE_0F38
9122 && t->opcode_modifier.vexvvvv == VexVVVV_DST)
9123 || t->mnem_off == MN_movbe)
6967f19d
HL
9124 && (i.types[i.operands - 1].bitfield.dword
9125 || i.types[i.operands - 1].bitfield.qword))
9126 {
9127 unsigned int match_dest_op = can_convert_NDD_to_legacy (t);
9128
9129 if (match_dest_op != (unsigned int) ~0)
9130 {
9131 size_match = true;
9132 /* We ensure that the next template has the same input
9133 operands as the original matching template by the first
9134 opernd (ATT). To avoid someone support new NDD insns and
9135 put it in the wrong position. */
9136 overlap0 = operand_type_and (i.types[0],
9137 t[1].operand_types[0]);
9138 if (t->opcode_modifier.d)
9139 overlap1 = operand_type_and (i.types[0],
9140 t[1].operand_types[1]);
9141 if (!operand_type_match (overlap0, i.types[0])
9142 && (!t->opcode_modifier.d
9143 || !operand_type_match (overlap1, i.types[0])))
9144 size_match = false;
9145
9146 if (size_match
9147 && (t[1].opcode_space <= SPACE_0F
9148 /* Some non-legacy-map0/1 insns can be shorter when
9149 legacy-encoded and when no REX prefix is required. */
9150 || (!check_EgprOperands (t + 1)
9151 && !check_Rex_required ()
9152 && !i.op[i.operands - 1].regs->reg_type.bitfield.qword)))
9153 {
9154 if (i.operands > 2 && match_dest_op == i.operands - 3)
9155 swap_2_operands (match_dest_op, i.operands - 2);
9156
9157 --i.operands;
9158 --i.reg_operands;
9159
0ebcbb1b
JB
9160 if (t->mnem_off == MN_movbe)
9161 {
9162 gas_assert (t[1].mnem_off == MN_bswap);
9163 ++current_templates.end;
9164 }
9165
6967f19d
HL
9166 specific_error = progress (internal_error);
9167 continue;
9168 }
9169
9170 }
9171 }
9172
29b0f896
AM
9173 /* We've found a match; break out of loop. */
9174 break;
9175 }
9176
7b94647a
JB
9177#undef progress
9178
d3b01414 9179 if (t == current_templates.end)
29b0f896
AM
9180 {
9181 /* We found no match. */
04784e33 9182 i.error = specific_error;
fa99fab2 9183 return NULL;
29b0f896 9184 }
252b5132 9185
29b0f896
AM
9186 if (!quiet_warnings)
9187 {
9188 if (!intel_syntax
0cfa3eb3 9189 && (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE)))
76d3f746 9190 as_warn (_("indirect %s without `*'"), insn_name (t));
29b0f896 9191
40fb9820 9192 if (t->opcode_modifier.isprefix
3cd7f3e3 9193 && t->opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
9194 {
9195 /* Warn them that a data or address size prefix doesn't
9196 affect assembly of the next line of code. */
76d3f746 9197 as_warn (_("stand-alone `%s' prefix"), insn_name (t));
29b0f896
AM
9198 }
9199 }
9200
9201 /* Copy the template we found. */
9a182d04 9202 install_template (t);
539e75ad
L
9203
9204 if (addr_prefix_disp != -1)
9205 i.tm.operand_types[addr_prefix_disp]
9206 = operand_types[addr_prefix_disp];
9207
09de03fc
JB
9208 /* APX insns acting on byte operands are WIG, yet that can't be expressed
9209 in the templates (they're also covering word/dword/qword operands). */
9210 if (t->opcode_space == SPACE_EVEXMAP4 && !t->opcode_modifier.vexw &&
9211 i.types[i.operands - 1].bitfield.byte)
9212 {
9213 gas_assert (t->opcode_modifier.w);
9214 i.tm.opcode_modifier.vexw = VEXWIG;
9215 }
9216
8bd915b7 9217 switch (found_reverse_match)
29b0f896 9218 {
8bd915b7
JB
9219 case 0:
9220 break;
9221
bd782808
JB
9222 case Opcode_FloatR:
9223 case Opcode_FloatR | Opcode_FloatD:
9224 i.tm.extension_opcode ^= Opcode_FloatR >> 3;
9225 found_reverse_match &= Opcode_FloatD;
9226
9227 /* Fall through. */
8bd915b7 9228 default:
dfd69174
JB
9229 /* If we found a reverse match we must alter the opcode direction
9230 bit and clear/flip the regmem modifier one. found_reverse_match
9231 holds bits to change (different for int & float insns). */
29b0f896
AM
9232
9233 i.tm.base_opcode ^= found_reverse_match;
9234
3083f376 9235 if (i.tm.opcode_space == SPACE_EVEXMAP4)
9236 goto swap_first_2;
9237
dfd69174
JB
9238 /* Certain SIMD insns have their load forms specified in the opcode
9239 table, and hence we need to _set_ RegMem instead of clearing it.
9240 We need to avoid setting the bit though on insns like KMOVW. */
9241 i.tm.opcode_modifier.regmem
9242 = i.tm.opcode_modifier.modrm && i.tm.opcode_modifier.d
9243 && i.tm.operands > 2U - i.tm.opcode_modifier.sse2avx
9244 && !i.tm.opcode_modifier.regmem;
a33ef3c2
JB
9245
9246 /* Fall through. */
9247 case ~0:
9248 i.tm.operand_types[0] = operand_types[i.operands - 1];
9249 i.tm.operand_types[i.operands - 1] = operand_types[0];
8bd915b7
JB
9250 break;
9251
9252 case Opcode_VexW:
9253 /* Only the first two register operands need reversing, alongside
9254 flipping VEX.W. */
9255 i.tm.opcode_modifier.vexw ^= VEXW0 ^ VEXW1;
9256
3083f376 9257 swap_first_2:
8bd915b7
JB
9258 j = i.tm.operand_types[0].bitfield.imm8;
9259 i.tm.operand_types[j] = operand_types[j + 1];
9260 i.tm.operand_types[j + 1] = operand_types[j];
9261 break;
29b0f896
AM
9262 }
9263
fa99fab2 9264 return t;
29b0f896
AM
9265}
9266
9267static int
e3bb37b5 9268check_string (void)
29b0f896 9269{
51c8edf6
JB
9270 unsigned int es_op = i.tm.opcode_modifier.isstring - IS_STRING_ES_OP0;
9271 unsigned int op = i.tm.operand_types[0].bitfield.baseindex ? es_op : 0;
8dc0818e 9272
5e042380 9273 if (i.seg[op] != NULL && i.seg[op] != reg_es)
29b0f896 9274 {
51c8edf6 9275 as_bad (_("`%s' operand %u must use `%ses' segment"),
76d3f746 9276 insn_name (&i.tm),
51c8edf6
JB
9277 intel_syntax ? i.tm.operands - es_op : es_op + 1,
9278 register_prefix);
9279 return 0;
29b0f896 9280 }
51c8edf6
JB
9281
9282 /* There's only ever one segment override allowed per instruction.
9283 This instruction possibly has a legal segment override on the
9284 second operand, so copy the segment to where non-string
9285 instructions store it, allowing common code. */
9286 i.seg[op] = i.seg[1];
9287
29b0f896
AM
9288 return 1;
9289}
9290
9291static int
543613e9 9292process_suffix (void)
29b0f896 9293{
7fc69528 9294 bool is_movx = false;
8b65b895 9295
29b0f896
AM
9296 /* If matched instruction specifies an explicit instruction mnemonic
9297 suffix, use it. */
673fe0f0 9298 if (i.tm.opcode_modifier.size == SIZE16)
40fb9820 9299 i.suffix = WORD_MNEM_SUFFIX;
673fe0f0 9300 else if (i.tm.opcode_modifier.size == SIZE32)
40fb9820 9301 i.suffix = LONG_MNEM_SUFFIX;
673fe0f0 9302 else if (i.tm.opcode_modifier.size == SIZE64)
40fb9820 9303 i.suffix = QWORD_MNEM_SUFFIX;
13e600d0 9304 else if (i.reg_operands
c8f8eebc 9305 && (i.operands > 1 || i.types[0].bitfield.class == Reg)
255571cd 9306 && i.tm.opcode_modifier.operandconstraint != ADDR_PREFIX_OP_REG)
29b0f896 9307 {
65fca059 9308 unsigned int numop = i.operands;
389d00a5
JB
9309
9310 /* MOVSX/MOVZX */
ddb62495 9311 is_movx = (i.tm.opcode_space == SPACE_0F
389d00a5 9312 && (i.tm.base_opcode | 8) == 0xbe)
ddb62495 9313 || (i.tm.opcode_space == SPACE_BASE
389d00a5 9314 && i.tm.base_opcode == 0x63
734dfd1c 9315 && is_cpu (&i.tm, Cpu64));
389d00a5 9316
65fca059
JB
9317 /* movsx/movzx want only their source operand considered here, for the
9318 ambiguity checking below. The suffix will be replaced afterwards
9319 to represent the destination (register). */
389d00a5 9320 if (is_movx && (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63))
65fca059
JB
9321 --i.operands;
9322
643bb870 9323 /* crc32 needs REX.W set regardless of suffix / source operand size. */
7fc69528 9324 if (i.tm.mnem_off == MN_crc32 && i.tm.operand_types[1].bitfield.qword)
643bb870
JB
9325 i.rex |= REX_W;
9326
29b0f896 9327 /* If there's no instruction mnemonic suffix we try to invent one
13e600d0 9328 based on GPR operands. */
29b0f896
AM
9329 if (!i.suffix)
9330 {
9331 /* We take i.suffix from the last register operand specified,
9332 Destination register type is more significant than source
381d071f
L
9333 register type. crc32 in SSE4.2 prefers source register
9334 type. */
7fc69528 9335 unsigned int op = i.tm.mnem_off == MN_crc32 ? 1 : i.operands;
20592a94 9336
1a035124
JB
9337 while (op--)
9338 if (i.tm.operand_types[op].bitfield.instance == InstanceNone
9339 || i.tm.operand_types[op].bitfield.instance == Accum)
9340 {
9341 if (i.types[op].bitfield.class != Reg)
9342 continue;
9343 if (i.types[op].bitfield.byte)
9344 i.suffix = BYTE_MNEM_SUFFIX;
9345 else if (i.types[op].bitfield.word)
9346 i.suffix = WORD_MNEM_SUFFIX;
9347 else if (i.types[op].bitfield.dword)
9348 i.suffix = LONG_MNEM_SUFFIX;
9349 else if (i.types[op].bitfield.qword)
9350 i.suffix = QWORD_MNEM_SUFFIX;
9351 else
9352 continue;
9353 break;
9354 }
65fca059
JB
9355
9356 /* As an exception, movsx/movzx silently default to a byte source
9357 in AT&T mode. */
389d00a5 9358 if (is_movx && i.tm.opcode_modifier.w && !i.suffix && !intel_syntax)
65fca059 9359 i.suffix = BYTE_MNEM_SUFFIX;
29b0f896
AM
9360 }
9361 else if (i.suffix == BYTE_MNEM_SUFFIX)
9362 {
1cb0ab18 9363 if (!check_byte_reg ())
29b0f896
AM
9364 return 0;
9365 }
9366 else if (i.suffix == LONG_MNEM_SUFFIX)
9367 {
1cb0ab18 9368 if (!check_long_reg ())
29b0f896
AM
9369 return 0;
9370 }
9371 else if (i.suffix == QWORD_MNEM_SUFFIX)
9372 {
1cb0ab18 9373 if (!check_qword_reg ())
29b0f896
AM
9374 return 0;
9375 }
9376 else if (i.suffix == WORD_MNEM_SUFFIX)
9377 {
1cb0ab18 9378 if (!check_word_reg ())
29b0f896
AM
9379 return 0;
9380 }
3cd7f3e3
L
9381 else if (intel_syntax
9382 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
9383 /* Do nothing if the instruction is going to ignore the prefix. */
9384 ;
9385 else
9386 abort ();
65fca059
JB
9387
9388 /* Undo the movsx/movzx change done above. */
9389 i.operands = numop;
29b0f896 9390 }
3cd7f3e3
L
9391 else if (i.tm.opcode_modifier.mnemonicsize == DEFAULTSIZE
9392 && !i.suffix)
29b0f896 9393 {
13e600d0
JB
9394 i.suffix = stackop_size;
9395 if (stackop_size == LONG_MNEM_SUFFIX)
06f74c5c
L
9396 {
9397 /* stackop_size is set to LONG_MNEM_SUFFIX for the
9398 .code16gcc directive to support 16-bit mode with
9399 32-bit address. For IRET without a suffix, generate
9400 16-bit IRET (opcode 0xcf) to return from an interrupt
9401 handler. */
13e600d0
JB
9402 if (i.tm.base_opcode == 0xcf)
9403 {
9404 i.suffix = WORD_MNEM_SUFFIX;
9405 as_warn (_("generating 16-bit `iret' for .code16gcc directive"));
9406 }
9407 /* Warn about changed behavior for segment register push/pop. */
9408 else if ((i.tm.base_opcode | 1) == 0x07)
9409 as_warn (_("generating 32-bit `%s', unlike earlier gas versions"),
76d3f746 9410 insn_name (&i.tm));
06f74c5c 9411 }
29b0f896 9412 }
c006a730 9413 else if (!i.suffix
0cfa3eb3
JB
9414 && (i.tm.opcode_modifier.jump == JUMP_ABSOLUTE
9415 || i.tm.opcode_modifier.jump == JUMP_BYTE
9416 || i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT
ddb62495 9417 || (i.tm.opcode_space == SPACE_0F
389d00a5 9418 && i.tm.base_opcode == 0x01 /* [ls][gi]dt */
64e74474 9419 && i.tm.extension_opcode <= 3)))
9306ca4a
JB
9420 {
9421 switch (flag_code)
9422 {
9423 case CODE_64BIT:
40fb9820 9424 if (!i.tm.opcode_modifier.no_qsuf)
9306ca4a 9425 {
828c2a25
JB
9426 if (i.tm.opcode_modifier.jump == JUMP_BYTE
9427 || i.tm.opcode_modifier.no_lsuf)
9428 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a
JB
9429 break;
9430 }
1a0670f3 9431 /* Fall through. */
9306ca4a 9432 case CODE_32BIT:
40fb9820 9433 if (!i.tm.opcode_modifier.no_lsuf)
9306ca4a
JB
9434 i.suffix = LONG_MNEM_SUFFIX;
9435 break;
9436 case CODE_16BIT:
40fb9820 9437 if (!i.tm.opcode_modifier.no_wsuf)
9306ca4a
JB
9438 i.suffix = WORD_MNEM_SUFFIX;
9439 break;
9440 }
9441 }
252b5132 9442
c006a730 9443 if (!i.suffix
3cd7f3e3 9444 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8
JB
9445 /* Also cover lret/retf/iret in 64-bit mode. */
9446 || (flag_code == CODE_64BIT
9447 && !i.tm.opcode_modifier.no_lsuf
9448 && !i.tm.opcode_modifier.no_qsuf))
3cd7f3e3 9449 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
8bbb3ad8
JB
9450 /* Explicit sizing prefixes are assumed to disambiguate insns. */
9451 && !i.prefix[DATA_PREFIX] && !(i.prefix[REX_PREFIX] & REX_W)
62b3f548
JB
9452 /* Accept FLDENV et al without suffix. */
9453 && (i.tm.opcode_modifier.no_ssuf || i.tm.opcode_modifier.floatmf))
29b0f896 9454 {
6c0946d0 9455 unsigned int suffixes, evex = 0;
c006a730
JB
9456
9457 suffixes = !i.tm.opcode_modifier.no_bsuf;
9458 if (!i.tm.opcode_modifier.no_wsuf)
9459 suffixes |= 1 << 1;
9460 if (!i.tm.opcode_modifier.no_lsuf)
9461 suffixes |= 1 << 2;
c006a730
JB
9462 if (!i.tm.opcode_modifier.no_ssuf)
9463 suffixes |= 1 << 4;
9464 if (flag_code == CODE_64BIT && !i.tm.opcode_modifier.no_qsuf)
9465 suffixes |= 1 << 5;
9466
6c0946d0
JB
9467 /* For [XYZ]MMWORD operands inspect operand sizes. While generally
9468 also suitable for AT&T syntax mode, it was requested that this be
9469 restricted to just Intel syntax. */
a5748e0d
JB
9470 if (intel_syntax && is_any_vex_encoding (&i.tm)
9471 && !i.broadcast.type && !i.broadcast.bytes)
6c0946d0 9472 {
b9915cbc 9473 unsigned int op;
6c0946d0 9474
b9915cbc 9475 for (op = 0; op < i.tm.operands; ++op)
6c0946d0 9476 {
4fc85f37
JB
9477 if (vector_size < VSZ512)
9478 {
9479 i.tm.operand_types[op].bitfield.zmmword = 0;
9480 if (vector_size < VSZ256)
9481 {
9482 i.tm.operand_types[op].bitfield.ymmword = 0;
9483 if (i.tm.operand_types[op].bitfield.xmmword
706ce984 9484 && i.tm.opcode_modifier.evex == EVEXDYN)
4fc85f37
JB
9485 i.tm.opcode_modifier.evex = EVEX128;
9486 }
9487 else if (i.tm.operand_types[op].bitfield.ymmword
9488 && !i.tm.operand_types[op].bitfield.xmmword
706ce984 9489 && i.tm.opcode_modifier.evex == EVEXDYN)
4fc85f37
JB
9490 i.tm.opcode_modifier.evex = EVEX256;
9491 }
706ce984 9492 else if (i.tm.opcode_modifier.evex
4fc85f37 9493 && !cpu_arch_flags.bitfield.cpuavx512vl)
6c0946d0 9494 {
b9915cbc
JB
9495 if (i.tm.operand_types[op].bitfield.ymmword)
9496 i.tm.operand_types[op].bitfield.xmmword = 0;
9497 if (i.tm.operand_types[op].bitfield.zmmword)
9498 i.tm.operand_types[op].bitfield.ymmword = 0;
706ce984 9499 if (i.tm.opcode_modifier.evex == EVEXDYN)
b9915cbc
JB
9500 i.tm.opcode_modifier.evex = EVEX512;
9501 }
6c0946d0 9502
b9915cbc
JB
9503 if (i.tm.operand_types[op].bitfield.xmmword
9504 + i.tm.operand_types[op].bitfield.ymmword
9505 + i.tm.operand_types[op].bitfield.zmmword < 2)
9506 continue;
6c0946d0 9507
b9915cbc
JB
9508 /* Any properly sized operand disambiguates the insn. */
9509 if (i.types[op].bitfield.xmmword
9510 || i.types[op].bitfield.ymmword
9511 || i.types[op].bitfield.zmmword)
9512 {
9513 suffixes &= ~(7 << 6);
9514 evex = 0;
9515 break;
9516 }
6c0946d0 9517
b9915cbc
JB
9518 if ((i.flags[op] & Operand_Mem)
9519 && i.tm.operand_types[op].bitfield.unspecified)
9520 {
9521 if (i.tm.operand_types[op].bitfield.xmmword)
9522 suffixes |= 1 << 6;
9523 if (i.tm.operand_types[op].bitfield.ymmword)
9524 suffixes |= 1 << 7;
9525 if (i.tm.operand_types[op].bitfield.zmmword)
9526 suffixes |= 1 << 8;
706ce984 9527 if (i.tm.opcode_modifier.evex)
b9915cbc 9528 evex = EVEX512;
6c0946d0
JB
9529 }
9530 }
9531 }
9532
9533 /* Are multiple suffixes / operand sizes allowed? */
c006a730 9534 if (suffixes & (suffixes - 1))
9306ca4a 9535 {
873494c8 9536 if (intel_syntax
3cd7f3e3 9537 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8 9538 || operand_check == check_error))
9306ca4a 9539 {
76d3f746 9540 as_bad (_("ambiguous operand size for `%s'"), insn_name (&i.tm));
9306ca4a
JB
9541 return 0;
9542 }
c006a730 9543 if (operand_check == check_error)
9306ca4a 9544 {
c006a730 9545 as_bad (_("no instruction mnemonic suffix given and "
76d3f746 9546 "no register operands; can't size `%s'"), insn_name (&i.tm));
9306ca4a
JB
9547 return 0;
9548 }
c006a730 9549 if (operand_check == check_warning)
873494c8
JB
9550 as_warn (_("%s; using default for `%s'"),
9551 intel_syntax
9552 ? _("ambiguous operand size")
9553 : _("no instruction mnemonic suffix given and "
9554 "no register operands"),
76d3f746 9555 insn_name (&i.tm));
c006a730
JB
9556
9557 if (i.tm.opcode_modifier.floatmf)
9558 i.suffix = SHORT_MNEM_SUFFIX;
389d00a5 9559 else if (is_movx)
65fca059 9560 /* handled below */;
6c0946d0
JB
9561 else if (evex)
9562 i.tm.opcode_modifier.evex = evex;
c006a730
JB
9563 else if (flag_code == CODE_16BIT)
9564 i.suffix = WORD_MNEM_SUFFIX;
1a035124 9565 else if (!i.tm.opcode_modifier.no_lsuf)
c006a730 9566 i.suffix = LONG_MNEM_SUFFIX;
1a035124
JB
9567 else
9568 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a 9569 }
29b0f896 9570 }
252b5132 9571
389d00a5 9572 if (is_movx)
65fca059
JB
9573 {
9574 /* In Intel syntax, movsx/movzx must have a "suffix" (checked above).
9575 In AT&T syntax, if there is no suffix (warned about above), the default
9576 will be byte extension. */
9577 if (i.tm.opcode_modifier.w && i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
9578 i.tm.base_opcode |= 1;
9579
9580 /* For further processing, the suffix should represent the destination
9581 (register). This is already the case when one was used with
9582 mov[sz][bw]*, but we need to replace it for mov[sz]x, or if there was
9583 no suffix to begin with. */
9584 if (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63 || !i.suffix)
9585 {
9586 if (i.types[1].bitfield.word)
9587 i.suffix = WORD_MNEM_SUFFIX;
9588 else if (i.types[1].bitfield.qword)
9589 i.suffix = QWORD_MNEM_SUFFIX;
9590 else
9591 i.suffix = LONG_MNEM_SUFFIX;
9592
9593 i.tm.opcode_modifier.w = 0;
9594 }
9595 }
9596
50128d0c
JB
9597 if (!i.tm.opcode_modifier.modrm && i.reg_operands && i.tm.operands < 3)
9598 i.short_form = (i.tm.operand_types[0].bitfield.class == Reg)
9599 != (i.tm.operand_types[1].bitfield.class == Reg);
9600
d2224064
JB
9601 /* Change the opcode based on the operand size given by i.suffix. */
9602 switch (i.suffix)
29b0f896 9603 {
d2224064
JB
9604 /* Size floating point instruction. */
9605 case LONG_MNEM_SUFFIX:
9606 if (i.tm.opcode_modifier.floatmf)
9607 {
9608 i.tm.base_opcode ^= 4;
9609 break;
9610 }
9611 /* fall through */
9612 case WORD_MNEM_SUFFIX:
9613 case QWORD_MNEM_SUFFIX:
29b0f896 9614 /* It's not a byte, select word/dword operation. */
40fb9820 9615 if (i.tm.opcode_modifier.w)
29b0f896 9616 {
50128d0c 9617 if (i.short_form)
29b0f896
AM
9618 i.tm.base_opcode |= 8;
9619 else
9620 i.tm.base_opcode |= 1;
9621 }
4e3be89f
JB
9622
9623 /* Set mode64 for an operand. */
9624 if (i.suffix == QWORD_MNEM_SUFFIX)
9625 {
9626 if (flag_code == CODE_64BIT
9627 && !i.tm.opcode_modifier.norex64
9628 && !i.tm.opcode_modifier.vexw
9629 /* Special case for xchg %rax,%rax. It is NOP and doesn't
9630 need rex64. */
9631 && ! (i.operands == 2
9632 && i.tm.base_opcode == 0x90
9633 && i.tm.opcode_space == SPACE_BASE
9634 && i.types[0].bitfield.instance == Accum
9635 && i.types[1].bitfield.instance == Accum))
9636 i.rex |= REX_W;
9637
9638 break;
9639 }
9640
d2224064
JB
9641 /* fall through */
9642 case SHORT_MNEM_SUFFIX:
29b0f896
AM
9643 /* Now select between word & dword operations via the operand
9644 size prefix, except for instructions that will ignore this
9645 prefix anyway. */
4e3be89f 9646 if (i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
c8f8eebc 9647 && !i.tm.opcode_modifier.floatmf
6177c84d
CL
9648 && (!is_any_vex_encoding (&i.tm)
9649 || i.tm.opcode_space == SPACE_EVEXMAP4)
c8f8eebc
JB
9650 && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
9651 || (flag_code == CODE_64BIT
9652 && i.tm.opcode_modifier.jump == JUMP_BYTE)))
24eab124
AM
9653 {
9654 unsigned int prefix = DATA_PREFIX_OPCODE;
543613e9 9655
0cfa3eb3 9656 if (i.tm.opcode_modifier.jump == JUMP_BYTE) /* jcxz, loop */
29b0f896 9657 prefix = ADDR_PREFIX_OPCODE;
252b5132 9658
6177c84d
CL
9659 /* The DATA PREFIX of EVEX promoted from legacy APX instructions
9660 needs to be adjusted. */
9661 if (i.tm.opcode_space == SPACE_EVEXMAP4)
9662 {
9663 gas_assert (!i.tm.opcode_modifier.opcodeprefix);
9664 i.tm.opcode_modifier.opcodeprefix = PREFIX_0X66;
9665 }
9666 else if (!add_prefix (prefix))
29b0f896 9667 return 0;
24eab124 9668 }
252b5132 9669
d2224064 9670 break;
8bbb3ad8
JB
9671
9672 case 0:
f9a6a8f0 9673 /* Select word/dword/qword operation with explicit data sizing prefix
8bbb3ad8
JB
9674 when there are no suitable register operands. */
9675 if (i.tm.opcode_modifier.w
9676 && (i.prefix[DATA_PREFIX] || (i.prefix[REX_PREFIX] & REX_W))
9677 && (!i.reg_operands
9678 || (i.reg_operands == 1
9679 /* ShiftCount */
9680 && (i.tm.operand_types[0].bitfield.instance == RegC
9681 /* InOutPortReg */
9682 || i.tm.operand_types[0].bitfield.instance == RegD
9683 || i.tm.operand_types[1].bitfield.instance == RegD
7fc69528 9684 || i.tm.mnem_off == MN_crc32))))
8bbb3ad8
JB
9685 i.tm.base_opcode |= 1;
9686 break;
29b0f896 9687 }
7ecd2f8b 9688
255571cd 9689 if (i.tm.opcode_modifier.operandconstraint == ADDR_PREFIX_OP_REG)
c0a30a9f 9690 {
c8f8eebc
JB
9691 gas_assert (!i.suffix);
9692 gas_assert (i.reg_operands);
c0a30a9f 9693
c8f8eebc
JB
9694 if (i.tm.operand_types[0].bitfield.instance == Accum
9695 || i.operands == 1)
9696 {
9697 /* The address size override prefix changes the size of the
9698 first operand. */
9699 if (flag_code == CODE_64BIT
9700 && i.op[0].regs->reg_type.bitfield.word)
9701 {
9702 as_bad (_("16-bit addressing unavailable for `%s'"),
76d3f746 9703 insn_name (&i.tm));
c8f8eebc
JB
9704 return 0;
9705 }
9706
9707 if ((flag_code == CODE_32BIT
9708 ? i.op[0].regs->reg_type.bitfield.word
9709 : i.op[0].regs->reg_type.bitfield.dword)
9710 && !add_prefix (ADDR_PREFIX_OPCODE))
9711 return 0;
9712 }
c0a30a9f
L
9713 else
9714 {
c8f8eebc
JB
9715 /* Check invalid register operand when the address size override
9716 prefix changes the size of register operands. */
9717 unsigned int op;
9718 enum { need_word, need_dword, need_qword } need;
9719
27f13469 9720 /* Check the register operand for the address size prefix if
b3a3496f 9721 the memory operand has no real registers, like symbol, DISP
829f3fe1 9722 or bogus (x32-only) symbol(%rip) when symbol(%eip) is meant. */
27f13469
L
9723 if (i.mem_operands == 1
9724 && i.reg_operands == 1
9725 && i.operands == 2
27f13469 9726 && i.types[1].bitfield.class == Reg
b3a3496f
L
9727 && (flag_code == CODE_32BIT
9728 ? i.op[1].regs->reg_type.bitfield.word
9729 : i.op[1].regs->reg_type.bitfield.dword)
9730 && ((i.base_reg == NULL && i.index_reg == NULL)
829f3fe1
JB
9731#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
9732 || (x86_elf_abi == X86_64_X32_ABI
9733 && i.base_reg
b3a3496f
L
9734 && i.base_reg->reg_num == RegIP
9735 && i.base_reg->reg_type.bitfield.qword))
829f3fe1
JB
9736#else
9737 || 0)
9738#endif
27f13469
L
9739 && !add_prefix (ADDR_PREFIX_OPCODE))
9740 return 0;
9741
c8f8eebc
JB
9742 if (flag_code == CODE_32BIT)
9743 need = i.prefix[ADDR_PREFIX] ? need_word : need_dword;
9744 else if (i.prefix[ADDR_PREFIX])
c0a30a9f
L
9745 need = need_dword;
9746 else
9747 need = flag_code == CODE_64BIT ? need_qword : need_word;
c0a30a9f 9748
c8f8eebc
JB
9749 for (op = 0; op < i.operands; op++)
9750 {
9751 if (i.types[op].bitfield.class != Reg)
9752 continue;
9753
9754 switch (need)
9755 {
9756 case need_word:
9757 if (i.op[op].regs->reg_type.bitfield.word)
9758 continue;
9759 break;
9760 case need_dword:
9761 if (i.op[op].regs->reg_type.bitfield.dword)
9762 continue;
9763 break;
9764 case need_qword:
9765 if (i.op[op].regs->reg_type.bitfield.qword)
9766 continue;
9767 break;
9768 }
9769
9770 as_bad (_("invalid register operand size for `%s'"),
76d3f746 9771 insn_name (&i.tm));
c8f8eebc
JB
9772 return 0;
9773 }
9774 }
c0a30a9f
L
9775 }
9776
29b0f896
AM
9777 return 1;
9778}
3e73aa7c 9779
29b0f896 9780static int
543613e9 9781check_byte_reg (void)
29b0f896
AM
9782{
9783 int op;
543613e9 9784
29b0f896
AM
9785 for (op = i.operands; --op >= 0;)
9786 {
dc821c5f 9787 /* Skip non-register operands. */
bab6aec1 9788 if (i.types[op].bitfield.class != Reg)
dc821c5f
JB
9789 continue;
9790
29b0f896
AM
9791 /* If this is an eight bit register, it's OK. If it's the 16 or
9792 32 bit version of an eight bit register, we will just use the
9793 low portion, and that's OK too. */
dc821c5f 9794 if (i.types[op].bitfield.byte)
29b0f896
AM
9795 continue;
9796
5a819eb9 9797 /* I/O port address operands are OK too. */
75e5731b
JB
9798 if (i.tm.operand_types[op].bitfield.instance == RegD
9799 && i.tm.operand_types[op].bitfield.word)
5a819eb9
JB
9800 continue;
9801
9706160a 9802 /* crc32 only wants its source operand checked here. */
7fc69528 9803 if (i.tm.mnem_off == MN_crc32 && op != 0)
9344ff29
L
9804 continue;
9805
29b0f896 9806 /* Any other register is bad. */
73c76375
JB
9807 as_bad (_("`%s%s' not allowed with `%s%c'"),
9808 register_prefix, i.op[op].regs->reg_name,
76d3f746 9809 insn_name (&i.tm), i.suffix);
73c76375 9810 return 0;
29b0f896
AM
9811 }
9812 return 1;
9813}
9814
9815static int
e3bb37b5 9816check_long_reg (void)
29b0f896
AM
9817{
9818 int op;
9819
9820 for (op = i.operands; --op >= 0;)
dc821c5f 9821 /* Skip non-register operands. */
bab6aec1 9822 if (i.types[op].bitfield.class != Reg)
dc821c5f 9823 continue;
29b0f896
AM
9824 /* Reject eight bit registers, except where the template requires
9825 them. (eg. movzb) */
dc821c5f 9826 else if (i.types[op].bitfield.byte
bab6aec1 9827 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 9828 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
9829 && (i.tm.operand_types[op].bitfield.word
9830 || i.tm.operand_types[op].bitfield.dword))
29b0f896 9831 {
a540244d
L
9832 as_bad (_("`%s%s' not allowed with `%s%c'"),
9833 register_prefix,
29b0f896 9834 i.op[op].regs->reg_name,
76d3f746 9835 insn_name (&i.tm),
29b0f896
AM
9836 i.suffix);
9837 return 0;
9838 }
d9a1b711
JB
9839 /* Error if the e prefix on a general reg is missing, or if the r
9840 prefix on a general reg is present. */
9841 else if ((i.types[op].bitfield.word
9842 || i.types[op].bitfield.qword)
bab6aec1 9843 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 9844 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 9845 && i.tm.operand_types[op].bitfield.dword)
29b0f896 9846 {
be4c5e58
L
9847 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
9848 register_prefix, i.op[op].regs->reg_name,
9849 i.suffix);
9850 return 0;
252b5132 9851 }
29b0f896
AM
9852 return 1;
9853}
252b5132 9854
29b0f896 9855static int
e3bb37b5 9856check_qword_reg (void)
29b0f896
AM
9857{
9858 int op;
252b5132 9859
29b0f896 9860 for (op = i.operands; --op >= 0; )
dc821c5f 9861 /* Skip non-register operands. */
bab6aec1 9862 if (i.types[op].bitfield.class != Reg)
dc821c5f 9863 continue;
29b0f896
AM
9864 /* Reject eight bit registers, except where the template requires
9865 them. (eg. movzb) */
dc821c5f 9866 else if (i.types[op].bitfield.byte
bab6aec1 9867 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 9868 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 9869 && (i.tm.operand_types[op].bitfield.word
742b55c7
JB
9870 || i.tm.operand_types[op].bitfield.dword
9871 || i.tm.operand_types[op].bitfield.qword))
29b0f896 9872 {
a540244d
L
9873 as_bad (_("`%s%s' not allowed with `%s%c'"),
9874 register_prefix,
29b0f896 9875 i.op[op].regs->reg_name,
76d3f746 9876 insn_name (&i.tm),
29b0f896
AM
9877 i.suffix);
9878 return 0;
9879 }
d9a1b711 9880 /* Error if the r prefix on a general reg is missing. */
dc821c5f
JB
9881 else if ((i.types[op].bitfield.word
9882 || i.types[op].bitfield.dword)
bab6aec1 9883 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 9884 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 9885 && i.tm.operand_types[op].bitfield.qword)
29b0f896
AM
9886 {
9887 /* Prohibit these changes in the 64bit mode, since the
9888 lowering is more complicated. */
1cb0ab18
JB
9889 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
9890 register_prefix, i.op[op].regs->reg_name, i.suffix);
9891 return 0;
252b5132 9892 }
29b0f896
AM
9893 return 1;
9894}
252b5132 9895
29b0f896 9896static int
e3bb37b5 9897check_word_reg (void)
29b0f896
AM
9898{
9899 int op;
9900 for (op = i.operands; --op >= 0;)
dc821c5f 9901 /* Skip non-register operands. */
bab6aec1 9902 if (i.types[op].bitfield.class != Reg)
dc821c5f 9903 continue;
29b0f896
AM
9904 /* Reject eight bit registers, except where the template requires
9905 them. (eg. movzb) */
dc821c5f 9906 else if (i.types[op].bitfield.byte
bab6aec1 9907 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 9908 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
9909 && (i.tm.operand_types[op].bitfield.word
9910 || i.tm.operand_types[op].bitfield.dword))
29b0f896 9911 {
a540244d
L
9912 as_bad (_("`%s%s' not allowed with `%s%c'"),
9913 register_prefix,
29b0f896 9914 i.op[op].regs->reg_name,
76d3f746 9915 insn_name (&i.tm),
29b0f896
AM
9916 i.suffix);
9917 return 0;
9918 }
9706160a
JB
9919 /* Error if the e or r prefix on a general reg is present. */
9920 else if ((i.types[op].bitfield.dword
dc821c5f 9921 || i.types[op].bitfield.qword)
bab6aec1 9922 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 9923 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 9924 && i.tm.operand_types[op].bitfield.word)
252b5132 9925 {
9706160a
JB
9926 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
9927 register_prefix, i.op[op].regs->reg_name,
9928 i.suffix);
9929 return 0;
29b0f896
AM
9930 }
9931 return 1;
9932}
252b5132 9933
29b0f896 9934static int
40fb9820 9935update_imm (unsigned int j)
29b0f896 9936{
bc0844ae 9937 i386_operand_type overlap = i.types[j];
c34d1cc9
JB
9938
9939 if (i.tm.operand_types[j].bitfield.imm8
9940 && i.tm.operand_types[j].bitfield.imm8s
9941 && overlap.bitfield.imm8 && overlap.bitfield.imm8s)
9942 {
9943 /* This combination is used on 8-bit immediates where e.g. $~0 is
9944 desirable to permit. We're past operand type matching, so simply
9945 put things back in the shape they were before introducing the
9946 distinction between Imm8, Imm8S, and Imm8|Imm8S. */
9947 overlap.bitfield.imm8s = 0;
9948 }
9949
be1643ff
JB
9950 if (overlap.bitfield.imm8
9951 + overlap.bitfield.imm8s
9952 + overlap.bitfield.imm16
9953 + overlap.bitfield.imm32
9954 + overlap.bitfield.imm32s
9955 + overlap.bitfield.imm64 > 1)
29b0f896 9956 {
05909f23
JB
9957 static const i386_operand_type imm16 = { .bitfield = { .imm16 = 1 } };
9958 static const i386_operand_type imm32 = { .bitfield = { .imm32 = 1 } };
9959 static const i386_operand_type imm32s = { .bitfield = { .imm32s = 1 } };
9960 static const i386_operand_type imm16_32 = { .bitfield =
9961 { .imm16 = 1, .imm32 = 1 }
9962 };
9963 static const i386_operand_type imm16_32s = { .bitfield =
9964 { .imm16 = 1, .imm32s = 1 }
9965 };
9966 static const i386_operand_type imm16_32_32s = { .bitfield =
9967 { .imm16 = 1, .imm32 = 1, .imm32s = 1 }
9968 };
9969
29b0f896
AM
9970 if (i.suffix)
9971 {
40fb9820
L
9972 i386_operand_type temp;
9973
0dfbf9d7 9974 operand_type_set (&temp, 0);
7ab9ffdd 9975 if (i.suffix == BYTE_MNEM_SUFFIX)
40fb9820
L
9976 {
9977 temp.bitfield.imm8 = overlap.bitfield.imm8;
9978 temp.bitfield.imm8s = overlap.bitfield.imm8s;
9979 }
9980 else if (i.suffix == WORD_MNEM_SUFFIX)
9981 temp.bitfield.imm16 = overlap.bitfield.imm16;
9982 else if (i.suffix == QWORD_MNEM_SUFFIX)
9983 {
9984 temp.bitfield.imm64 = overlap.bitfield.imm64;
9985 temp.bitfield.imm32s = overlap.bitfield.imm32s;
9986 }
9987 else
9988 temp.bitfield.imm32 = overlap.bitfield.imm32;
9989 overlap = temp;
29b0f896 9990 }
0dfbf9d7
L
9991 else if (operand_type_equal (&overlap, &imm16_32_32s)
9992 || operand_type_equal (&overlap, &imm16_32)
9993 || operand_type_equal (&overlap, &imm16_32s))
29b0f896 9994 {
f79d55e1
JB
9995 if ((flag_code == CODE_16BIT)
9996 ^ (i.prefix[DATA_PREFIX] != 0 && !(i.prefix[REX_PREFIX] & REX_W)))
65da13b5 9997 overlap = imm16;
40fb9820 9998 else
65da13b5 9999 overlap = imm32s;
29b0f896 10000 }
8bbb3ad8
JB
10001 else if (i.prefix[REX_PREFIX] & REX_W)
10002 overlap = operand_type_and (overlap, imm32s);
10003 else if (i.prefix[DATA_PREFIX])
10004 overlap = operand_type_and (overlap,
10005 flag_code != CODE_16BIT ? imm16 : imm32);
be1643ff
JB
10006 if (overlap.bitfield.imm8
10007 + overlap.bitfield.imm8s
10008 + overlap.bitfield.imm16
10009 + overlap.bitfield.imm32
10010 + overlap.bitfield.imm32s
10011 + overlap.bitfield.imm64 != 1)
29b0f896 10012 {
4eed87de
AM
10013 as_bad (_("no instruction mnemonic suffix given; "
10014 "can't determine immediate size"));
29b0f896
AM
10015 return 0;
10016 }
10017 }
40fb9820 10018 i.types[j] = overlap;
29b0f896 10019
40fb9820
L
10020 return 1;
10021}
10022
10023static int
10024finalize_imm (void)
10025{
bc0844ae 10026 unsigned int j, n;
29b0f896 10027
bc0844ae
L
10028 /* Update the first 2 immediate operands. */
10029 n = i.operands > 2 ? 2 : i.operands;
10030 if (n)
10031 {
10032 for (j = 0; j < n; j++)
10033 if (update_imm (j) == 0)
10034 return 0;
40fb9820 10035
bc0844ae
L
10036 /* The 3rd operand can't be immediate operand. */
10037 gas_assert (operand_type_check (i.types[2], imm) == 0);
10038 }
29b0f896
AM
10039
10040 return 1;
10041}
10042
0a3eba42
JB
10043static INLINE void set_rex_vrex (const reg_entry *r, unsigned int rex_bit,
10044 bool do_sse2avx)
10045{
10046 if (r->reg_flags & RegRex)
10047 {
10048 if (i.rex & rex_bit)
10049 as_bad (_("same type of prefix used twice"));
10050 i.rex |= rex_bit;
10051 }
10052 else if (do_sse2avx && (i.rex & rex_bit) && i.vex.register_specifier)
10053 {
10054 gas_assert (i.vex.register_specifier == r);
10055 i.vex.register_specifier += 8;
10056 }
10057
10058 if (r->reg_flags & RegVRex)
10059 i.vrex |= rex_bit;
80d61d8d
CL
10060
10061 if (r->reg_flags & RegRex2)
10062 i.rex2 |= rex_bit;
10063}
10064
10065static INLINE void
10066set_rex_rex2 (const reg_entry *r, unsigned int rex_bit)
10067{
10068 if ((r->reg_flags & RegRex) != 0)
10069 i.rex |= rex_bit;
10070 if ((r->reg_flags & RegRex2) != 0)
10071 i.rex2 |= rex_bit;
0a3eba42
JB
10072}
10073
29b0f896 10074static int
e3bb37b5 10075process_operands (void)
29b0f896
AM
10076{
10077 /* Default segment register this instruction will use for memory
10078 accesses. 0 means unknown. This is only for optimizing out
10079 unnecessary segment overrides. */
5e042380 10080 const reg_entry *default_seg = NULL;
29b0f896 10081
3083f376 10082 for (unsigned int j = 0; j < i.operands; j++)
10083 if (i.types[j].bitfield.instance != InstanceNone)
10084 i.reg_operands--;
31b4cda3 10085
a5aeccd9
JB
10086 if (i.tm.opcode_modifier.sse2avx)
10087 {
10088 /* Legacy encoded insns allow explicit REX prefixes, so these prefixes
10089 need converting. */
10090 i.rex |= i.prefix[REX_PREFIX] & (REX_W | REX_R | REX_X | REX_B);
10091 i.prefix[REX_PREFIX] = 0;
10092 i.rex_encoding = 0;
ebe82bfd 10093 i.rex2_encoding = 0;
a5aeccd9 10094 }
c423d21a
JB
10095 /* ImmExt should be processed after SSE2AVX. */
10096 else if (i.tm.opcode_modifier.immext)
10097 process_immext ();
a5aeccd9 10098
ecb96e55
JB
10099 /* TILEZERO is unusual in that it has a single operand encoded in ModR/M.reg,
10100 not ModR/M.rm. To avoid special casing this in build_modrm_byte(), fake a
10101 new destination operand here, while converting the source one to register
10102 number 0. */
10103 if (i.tm.mnem_off == MN_tilezero)
10104 {
10105 i.op[1].regs = i.op[0].regs;
10106 i.op[0].regs -= i.op[0].regs->reg_num;
10107 i.types[1] = i.types[0];
10108 i.tm.operand_types[1] = i.tm.operand_types[0];
10109 i.flags[1] = i.flags[0];
10110 i.operands++;
10111 i.reg_operands++;
10112 i.tm.operands++;
10113 }
10114
2426c15f 10115 if (i.tm.opcode_modifier.sse2avx && i.tm.opcode_modifier.vexvvvv)
29b0f896 10116 {
05909f23
JB
10117 static const i386_operand_type regxmm = {
10118 .bitfield = { .class = RegSIMD, .xmmword = 1 }
10119 };
91d6fa6a
NC
10120 unsigned int dupl = i.operands;
10121 unsigned int dest = dupl - 1;
9fcfb3d7
L
10122 unsigned int j;
10123
c0f3af97 10124 /* The destination must be an xmm register. */
9c2799c2 10125 gas_assert (i.reg_operands
91d6fa6a 10126 && MAX_OPERANDS > dupl
7ab9ffdd 10127 && operand_type_equal (&i.types[dest], &regxmm));
c0f3af97 10128
75e5731b 10129 if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 10130 && i.tm.operand_types[0].bitfield.xmmword)
e2ec9d29 10131 {
95dfdd85
JB
10132 /* Keep xmm0 for instructions with VEX prefix and 3
10133 sources. */
10134 i.tm.operand_types[0].bitfield.instance = InstanceNone;
10135 i.tm.operand_types[0].bitfield.class = RegSIMD;
31b4cda3 10136 i.reg_operands++;
95dfdd85 10137 goto duplicate;
c0f3af97 10138 }
95dfdd85
JB
10139
10140 if (i.tm.opcode_modifier.operandconstraint == IMPLICIT_1ST_XMM0)
7ab9ffdd 10141 {
aa180741 10142 gas_assert ((MAX_OPERANDS - 1) > dupl);
c0f3af97
L
10143
10144 /* Add the implicit xmm0 for instructions with VEX prefix
10145 and 3 sources. */
10146 for (j = i.operands; j > 0; j--)
10147 {
10148 i.op[j] = i.op[j - 1];
10149 i.types[j] = i.types[j - 1];
10150 i.tm.operand_types[j] = i.tm.operand_types[j - 1];
8dc0818e 10151 i.flags[j] = i.flags[j - 1];
c0f3af97
L
10152 }
10153 i.op[0].regs
629310ab 10154 = (const reg_entry *) str_hash_find (reg_hash, "xmm0");
7ab9ffdd 10155 i.types[0] = regxmm;
c0f3af97
L
10156 i.tm.operand_types[0] = regxmm;
10157
10158 i.operands += 2;
10159 i.reg_operands += 2;
10160 i.tm.operands += 2;
10161
91d6fa6a 10162 dupl++;
c0f3af97 10163 dest++;
91d6fa6a
NC
10164 i.op[dupl] = i.op[dest];
10165 i.types[dupl] = i.types[dest];
10166 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 10167 i.flags[dupl] = i.flags[dest];
e2ec9d29 10168 }
c0f3af97
L
10169 else
10170 {
dc1e8a47 10171 duplicate:
c0f3af97
L
10172 i.operands++;
10173 i.reg_operands++;
10174 i.tm.operands++;
10175
91d6fa6a
NC
10176 i.op[dupl] = i.op[dest];
10177 i.types[dupl] = i.types[dest];
10178 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 10179 i.flags[dupl] = i.flags[dest];
c0f3af97
L
10180 }
10181
10182 if (i.tm.opcode_modifier.immext)
10183 process_immext ();
10184 }
75e5731b 10185 else if (i.tm.operand_types[0].bitfield.instance == Accum
bd782808 10186 && i.tm.opcode_modifier.modrm)
c0f3af97
L
10187 {
10188 unsigned int j;
10189
9fcfb3d7
L
10190 for (j = 1; j < i.operands; j++)
10191 {
10192 i.op[j - 1] = i.op[j];
10193 i.types[j - 1] = i.types[j];
10194
10195 /* We need to adjust fields in i.tm since they are used by
10196 build_modrm_byte. */
10197 i.tm.operand_types [j - 1] = i.tm.operand_types [j];
8dc0818e
JB
10198
10199 i.flags[j - 1] = i.flags[j];
9fcfb3d7
L
10200 }
10201
31b4cda3
JB
10202 /* No adjustment to i.reg_operands: This was already done at the top
10203 of the function. */
e2ec9d29 10204 i.operands--;
e2ec9d29
L
10205 i.tm.operands--;
10206 }
255571cd 10207 else if (i.tm.opcode_modifier.operandconstraint == IMPLICIT_QUAD_GROUP)
920d2ddc 10208 {
a477a8c4
JB
10209 unsigned int regnum, first_reg_in_group, last_reg_in_group;
10210
920d2ddc 10211 /* The second operand must be {x,y,z}mmN, where N is a multiple of 4. */
3528c362 10212 gas_assert (i.operands >= 2 && i.types[1].bitfield.class == RegSIMD);
a477a8c4
JB
10213 regnum = register_number (i.op[1].regs);
10214 first_reg_in_group = regnum & ~3;
10215 last_reg_in_group = first_reg_in_group + 3;
10216 if (regnum != first_reg_in_group)
10217 as_warn (_("source register `%s%s' implicitly denotes"
10218 " `%s%.3s%u' to `%s%.3s%u' source group in `%s'"),
10219 register_prefix, i.op[1].regs->reg_name,
10220 register_prefix, i.op[1].regs->reg_name, first_reg_in_group,
10221 register_prefix, i.op[1].regs->reg_name, last_reg_in_group,
76d3f746 10222 insn_name (&i.tm));
a477a8c4 10223 }
255571cd 10224 else if (i.tm.opcode_modifier.operandconstraint == REG_KLUDGE)
e2ec9d29
L
10225 {
10226 /* The imul $imm, %reg instruction is converted into
10227 imul $imm, %reg, %reg, and the clr %reg instruction
10228 is converted into xor %reg, %reg. */
10229
10230 unsigned int first_reg_op;
10231
10232 if (operand_type_check (i.types[0], reg))
10233 first_reg_op = 0;
10234 else
10235 first_reg_op = 1;
10236 /* Pretend we saw the extra register operand. */
9c2799c2 10237 gas_assert (i.reg_operands == 1
7ab9ffdd 10238 && i.op[first_reg_op + 1].regs == 0);
e2ec9d29
L
10239 i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
10240 i.types[first_reg_op + 1] = i.types[first_reg_op];
10241 i.operands++;
10242 i.reg_operands++;
29b0f896
AM
10243 }
10244
85b80b0f 10245 if (i.tm.opcode_modifier.modrm)
29b0f896
AM
10246 {
10247 /* The opcode is completed (modulo i.tm.extension_opcode which
52271982
AM
10248 must be put into the modrm byte). Now, we make the modrm and
10249 index base bytes based on all the info we've collected. */
29b0f896
AM
10250
10251 default_seg = build_modrm_byte ();
bd782808
JB
10252
10253 if (!quiet_warnings && i.tm.opcode_modifier.operandconstraint == UGH)
10254 {
10255 /* Warn about some common errors, but press on regardless. */
10256 if (i.operands == 2)
10257 {
10258 /* Reversed arguments on faddp or fmulp. */
10259 as_warn (_("translating to `%s %s%s,%s%s'"), insn_name (&i.tm),
10260 register_prefix, i.op[!intel_syntax].regs->reg_name,
10261 register_prefix, i.op[intel_syntax].regs->reg_name);
10262 }
10263 else if (i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
10264 {
10265 /* Extraneous `l' suffix on fp insn. */
10266 as_warn (_("translating to `%s %s%s'"), insn_name (&i.tm),
10267 register_prefix, i.op[0].regs->reg_name);
10268 }
10269 }
29b0f896 10270 }
0ff3b7d0 10271 else if (i.types[0].bitfield.class == SReg && !dot_insn ())
85b80b0f
JB
10272 {
10273 if (flag_code != CODE_64BIT
10274 ? i.tm.base_opcode == POP_SEG_SHORT
10275 && i.op[0].regs->reg_num == 1
389d00a5 10276 : (i.tm.base_opcode | 1) == (POP_SEG386_SHORT & 0xff)
85b80b0f
JB
10277 && i.op[0].regs->reg_num < 4)
10278 {
10279 as_bad (_("you can't `%s %s%s'"),
76d3f746 10280 insn_name (&i.tm), register_prefix, i.op[0].regs->reg_name);
85b80b0f
JB
10281 return 0;
10282 }
389d00a5 10283 if (i.op[0].regs->reg_num > 3
ddb62495 10284 && i.tm.opcode_space == SPACE_BASE )
85b80b0f 10285 {
389d00a5 10286 i.tm.base_opcode ^= (POP_SEG_SHORT ^ POP_SEG386_SHORT) & 0xff;
ddb62495 10287 i.tm.opcode_space = SPACE_0F;
85b80b0f
JB
10288 }
10289 i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
10290 }
ddb62495 10291 else if (i.tm.opcode_space == SPACE_BASE
389d00a5 10292 && (i.tm.base_opcode & ~3) == MOV_AX_DISP32)
29b0f896 10293 {
5e042380 10294 default_seg = reg_ds;
29b0f896 10295 }
40fb9820 10296 else if (i.tm.opcode_modifier.isstring)
29b0f896
AM
10297 {
10298 /* For the string instructions that allow a segment override
10299 on one of their operands, the default segment is ds. */
5e042380 10300 default_seg = reg_ds;
29b0f896 10301 }
50128d0c 10302 else if (i.short_form)
85b80b0f 10303 {
0ff3b7d0
JB
10304 /* The register operand is in the 1st or 2nd non-immediate operand. */
10305 const reg_entry *r = i.op[i.imm_operands].regs;
85b80b0f 10306
0ff3b7d0
JB
10307 if (!dot_insn ()
10308 && r->reg_type.bitfield.instance == Accum
10309 && i.op[i.imm_operands + 1].regs)
10310 r = i.op[i.imm_operands + 1].regs;
85b80b0f 10311 /* Register goes in low 3 bits of opcode. */
4943d587 10312 i.tm.base_opcode |= r->reg_num;
0a3eba42 10313 set_rex_vrex (r, REX_B, false);
0ff3b7d0
JB
10314
10315 if (dot_insn () && i.reg_operands == 2)
10316 {
10317 gas_assert (is_any_vex_encoding (&i.tm)
e346d50a 10318 || i.encoding != encoding_default);
0ff3b7d0
JB
10319 i.vex.register_specifier = i.op[i.operands - 1].regs;
10320 }
10321 }
10322 else if (i.reg_operands == 1
10323 && !i.flags[i.operands - 1]
10324 && i.tm.operand_types[i.operands - 1].bitfield.instance
10325 == InstanceNone)
10326 {
10327 gas_assert (is_any_vex_encoding (&i.tm)
e346d50a 10328 || i.encoding != encoding_default);
0ff3b7d0 10329 i.vex.register_specifier = i.op[i.operands - 1].regs;
85b80b0f 10330 }
29b0f896 10331
514a8bb0 10332 if ((i.seg[0] || i.prefix[SEG_PREFIX])
7fc69528 10333 && i.tm.mnem_off == MN_lea)
92334ad2
JB
10334 {
10335 if (!quiet_warnings)
76d3f746 10336 as_warn (_("segment override on `%s' is ineffectual"), insn_name (&i.tm));
739d7649 10337 if (optimize && !i.no_optimize)
92334ad2
JB
10338 {
10339 i.seg[0] = NULL;
10340 i.prefix[SEG_PREFIX] = 0;
10341 }
10342 }
52271982
AM
10343
10344 /* If a segment was explicitly specified, and the specified segment
b6773884
JB
10345 is neither the default nor the one already recorded from a prefix,
10346 use an opcode prefix to select it. If we never figured out what
10347 the default segment is, then default_seg will be zero at this
10348 point, and the specified segment prefix will always be used. */
10349 if (i.seg[0]
10350 && i.seg[0] != default_seg
5e042380 10351 && i386_seg_prefixes[i.seg[0]->reg_num] != i.prefix[SEG_PREFIX])
29b0f896 10352 {
5e042380 10353 if (!add_prefix (i386_seg_prefixes[i.seg[0]->reg_num]))
29b0f896
AM
10354 return 0;
10355 }
10356 return 1;
10357}
10358
5e042380 10359static const reg_entry *
e3bb37b5 10360build_modrm_byte (void)
29b0f896 10361{
5e042380 10362 const reg_entry *default_seg = NULL;
ecb96e55
JB
10363 unsigned int source = i.imm_operands - i.tm.opcode_modifier.immext
10364 /* Compensate for kludge in md_assemble(). */
10365 + i.tm.operand_types[0].bitfield.imm1;
10366 unsigned int dest = i.operands - 1 - i.tm.opcode_modifier.immext;
f2a3a881 10367 unsigned int v, op, reg_slot;
ecb96e55
JB
10368
10369 /* Accumulator (in particular %st), shift count (%cl), and alike need
10370 to be skipped just like immediate operands do. */
10371 if (i.tm.operand_types[source].bitfield.instance)
10372 ++source;
10373 while (i.tm.operand_types[dest].bitfield.instance)
10374 --dest;
10375
10376 for (op = source; op < i.operands; ++op)
10377 if (i.tm.operand_types[op].bitfield.baseindex)
10378 break;
c0f3af97 10379
ecb96e55 10380 if (i.reg_operands + i.mem_operands + (i.tm.extension_opcode != None) == 4)
c0f3af97 10381 {
4c2c6516 10382 expressionS *exp;
c0f3af97 10383
a683cc34 10384 /* There are 2 kinds of instructions:
bed3d976 10385 1. 5 operands: 4 register operands or 3 register operands
9d3bf266 10386 plus 1 memory operand plus one Imm4 operand, VexXDS, and
bed3d976 10387 VexW0 or VexW1. The destination must be either XMM, YMM or
43234a1e 10388 ZMM register.
bed3d976 10389 2. 4 operands: 4 register operands or 3 register operands
0ff3b7d0
JB
10390 plus 1 memory operand, with VexXDS.
10391 3. Other equivalent combinations when coming from s_insn(). */
eea96d3f 10392 gas_assert (i.tm.opcode_modifier.vexvvvv
0ff3b7d0
JB
10393 && i.tm.opcode_modifier.vexw);
10394 gas_assert (dot_insn ()
10395 || i.tm.operand_types[dest].bitfield.class == RegSIMD);
a683cc34 10396
ecb96e55
JB
10397 /* Of the first two non-immediate operands the one with the template
10398 not allowing for a memory one is encoded in the immediate operand. */
10399 if (source == op)
10400 reg_slot = source + 1;
48db9223 10401 else
ecb96e55 10402 reg_slot = source++;
48db9223 10403
0ff3b7d0
JB
10404 if (!dot_insn ())
10405 {
10406 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
10407 gas_assert (!(i.op[reg_slot].regs->reg_flags & RegVRex));
10408 }
10409 else
10410 gas_assert (i.tm.operand_types[reg_slot].bitfield.class != ClassNone);
10411
a683cc34 10412 if (i.imm_operands == 0)
bed3d976
JB
10413 {
10414 /* When there is no immediate operand, generate an 8bit
10415 immediate operand to encode the first operand. */
10416 exp = &im_expressions[i.imm_operands++];
10417 i.op[i.operands].imms = exp;
be1643ff 10418 i.types[i.operands].bitfield.imm8 = 1;
bed3d976
JB
10419 i.operands++;
10420
bed3d976 10421 exp->X_op = O_constant;
43234a1e 10422 }
922d8de8 10423 else
bed3d976 10424 {
9d3bf266
JB
10425 gas_assert (i.imm_operands == 1);
10426 gas_assert (fits_in_imm4 (i.op[0].imms->X_add_number));
10427 gas_assert (!i.tm.opcode_modifier.immext);
a683cc34 10428
9d3bf266
JB
10429 /* Turn on Imm8 again so that output_imm will generate it. */
10430 i.types[0].bitfield.imm8 = 1;
bed3d976 10431
0ff3b7d0 10432 exp = i.op[0].imms;
bed3d976 10433 }
0ff3b7d0 10434 exp->X_add_number |= register_number (i.op[reg_slot].regs)
706ce984 10435 << (3 + !(i.tm.opcode_modifier.evex
e346d50a 10436 || i.encoding == encoding_evex));
c0f3af97 10437 }
f12dc422 10438
f2a3a881 10439 switch (i.tm.opcode_modifier.vexvvvv)
3083f376 10440 {
0820c9f5
CL
10441 /* VEX.vvvv encodes the last source register operand. */
10442 case VexVVVV_SRC2:
10443 if (source != op)
10444 {
10445 v = source++;
10446 break;
10447 }
10448 /* For vprot*, vpshl*, and vpsha*, XOP.W controls the swapping of src1
10449 and src2, and it requires fall through when the operands are swapped.
10450 */
10451 /* Fall through. */
f2a3a881
CL
10452 /* VEX.vvvv encodes the first source register operand. */
10453 case VexVVVV_SRC1:
10454 v = dest - 1;
10455 break;
10456 /* VEX.vvvv encodes the destination register operand. */
10457 case VexVVVV_DST:
10458 v = dest--;
10459 break;
10460 default:
10461 v = ~0;
10462 break;
10463 }
10464
c8866e3e
CL
10465 if (dest == source)
10466 dest = ~0;
10467
ecb96e55 10468 gas_assert (source < dest);
29b0f896 10469
ecb96e55
JB
10470 if (v < MAX_OPERANDS)
10471 {
10472 gas_assert (i.tm.opcode_modifier.vexvvvv);
10473 i.vex.register_specifier = i.op[v].regs;
29b0f896 10474 }
c0f3af97 10475
ecb96e55
JB
10476 if (op < i.operands)
10477 {
29b0f896
AM
10478 if (i.mem_operands)
10479 {
10480 unsigned int fake_zero_displacement = 0;
4eed87de 10481
ecb96e55 10482 gas_assert (i.flags[op] & Operand_Mem);
29b0f896 10483
63112cd6 10484 if (i.tm.opcode_modifier.sib)
6c30d220 10485 {
260cd341
LC
10486 /* The index register of VSIB shouldn't be RegIZ. */
10487 if (i.tm.opcode_modifier.sib != SIBMEM
10488 && i.index_reg->reg_num == RegIZ)
6c30d220
L
10489 abort ();
10490
10491 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
10492 if (!i.base_reg)
10493 {
10494 i.sib.base = NO_BASE_REGISTER;
10495 i.sib.scale = i.log2_scale_factor;
2f2be86b 10496 i.types[op] = operand_type_and_not (i.types[op], anydisp);
a775efc8 10497 i.types[op].bitfield.disp32 = 1;
6c30d220 10498 }
260cd341
LC
10499
10500 /* Since the mandatory SIB always has index register, so
10501 the code logic remains unchanged. The non-mandatory SIB
10502 without index register is allowed and will be handled
10503 later. */
10504 if (i.index_reg)
10505 {
10506 if (i.index_reg->reg_num == RegIZ)
10507 i.sib.index = NO_INDEX_REGISTER;
10508 else
10509 i.sib.index = i.index_reg->reg_num;
5b7c81bd 10510 set_rex_vrex (i.index_reg, REX_X, false);
260cd341 10511 }
6c30d220
L
10512 }
10513
5e042380 10514 default_seg = reg_ds;
29b0f896
AM
10515
10516 if (i.base_reg == 0)
10517 {
10518 i.rm.mode = 0;
10519 if (!i.disp_operands)
9bb129e8 10520 fake_zero_displacement = 1;
29b0f896
AM
10521 if (i.index_reg == 0)
10522 {
260cd341
LC
10523 /* Both check for VSIB and mandatory non-vector SIB. */
10524 gas_assert (!i.tm.opcode_modifier.sib
10525 || i.tm.opcode_modifier.sib == SIBMEM);
29b0f896 10526 /* Operand is just <disp> */
2f2be86b 10527 i.types[op] = operand_type_and_not (i.types[op], anydisp);
20f0a1fc 10528 if (flag_code == CODE_64BIT)
29b0f896
AM
10529 {
10530 /* 64bit mode overwrites the 32bit absolute
10531 addressing by RIP relative addressing and
10532 absolute addressing is encoded by one of the
10533 redundant SIB forms. */
10534 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
10535 i.sib.base = NO_BASE_REGISTER;
10536 i.sib.index = NO_INDEX_REGISTER;
a775efc8 10537 i.types[op].bitfield.disp32 = 1;
20f0a1fc 10538 }
fc225355
L
10539 else if ((flag_code == CODE_16BIT)
10540 ^ (i.prefix[ADDR_PREFIX] != 0))
20f0a1fc
NC
10541 {
10542 i.rm.regmem = NO_BASE_REGISTER_16;
2f2be86b 10543 i.types[op].bitfield.disp16 = 1;
20f0a1fc
NC
10544 }
10545 else
10546 {
10547 i.rm.regmem = NO_BASE_REGISTER;
2f2be86b 10548 i.types[op].bitfield.disp32 = 1;
29b0f896
AM
10549 }
10550 }
63112cd6 10551 else if (!i.tm.opcode_modifier.sib)
29b0f896 10552 {
6c30d220 10553 /* !i.base_reg && i.index_reg */
e968fc9b 10554 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
10555 i.sib.index = NO_INDEX_REGISTER;
10556 else
10557 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
10558 i.sib.base = NO_BASE_REGISTER;
10559 i.sib.scale = i.log2_scale_factor;
10560 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
2f2be86b 10561 i.types[op] = operand_type_and_not (i.types[op], anydisp);
a775efc8 10562 i.types[op].bitfield.disp32 = 1;
80d61d8d 10563 set_rex_rex2 (i.index_reg, REX_X);
29b0f896
AM
10564 }
10565 }
10566 /* RIP addressing for 64bit mode. */
e968fc9b 10567 else if (i.base_reg->reg_num == RegIP)
29b0f896 10568 {
63112cd6 10569 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896 10570 i.rm.regmem = NO_BASE_REGISTER;
40fb9820
L
10571 i.types[op].bitfield.disp8 = 0;
10572 i.types[op].bitfield.disp16 = 0;
a775efc8 10573 i.types[op].bitfield.disp32 = 1;
40fb9820 10574 i.types[op].bitfield.disp64 = 0;
71903a11 10575 i.flags[op] |= Operand_PCrel;
20f0a1fc
NC
10576 if (! i.disp_operands)
10577 fake_zero_displacement = 1;
29b0f896 10578 }
dc821c5f 10579 else if (i.base_reg->reg_type.bitfield.word)
29b0f896 10580 {
63112cd6 10581 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896
AM
10582 switch (i.base_reg->reg_num)
10583 {
10584 case 3: /* (%bx) */
10585 if (i.index_reg == 0)
10586 i.rm.regmem = 7;
10587 else /* (%bx,%si) -> 0, or (%bx,%di) -> 1 */
10588 i.rm.regmem = i.index_reg->reg_num - 6;
10589 break;
10590 case 5: /* (%bp) */
5e042380 10591 default_seg = reg_ss;
29b0f896
AM
10592 if (i.index_reg == 0)
10593 {
10594 i.rm.regmem = 6;
40fb9820 10595 if (operand_type_check (i.types[op], disp) == 0)
29b0f896
AM
10596 {
10597 /* fake (%bp) into 0(%bp) */
41eb8e88 10598 if (i.disp_encoding == disp_encoding_16bit)
1a02d6b0
L
10599 i.types[op].bitfield.disp16 = 1;
10600 else
10601 i.types[op].bitfield.disp8 = 1;
252b5132 10602 fake_zero_displacement = 1;
29b0f896
AM
10603 }
10604 }
10605 else /* (%bp,%si) -> 2, or (%bp,%di) -> 3 */
10606 i.rm.regmem = i.index_reg->reg_num - 6 + 2;
10607 break;
10608 default: /* (%si) -> 4 or (%di) -> 5 */
10609 i.rm.regmem = i.base_reg->reg_num - 6 + 4;
10610 }
41eb8e88
L
10611 if (!fake_zero_displacement
10612 && !i.disp_operands
10613 && i.disp_encoding)
10614 {
10615 fake_zero_displacement = 1;
10616 if (i.disp_encoding == disp_encoding_8bit)
10617 i.types[op].bitfield.disp8 = 1;
10618 else
10619 i.types[op].bitfield.disp16 = 1;
10620 }
29b0f896
AM
10621 i.rm.mode = mode_from_disp_size (i.types[op]);
10622 }
10623 else /* i.base_reg and 32/64 bit mode */
10624 {
a9aabc23 10625 if (operand_type_check (i.types[op], disp))
40fb9820 10626 {
73053c1f
JB
10627 i.types[op].bitfield.disp16 = 0;
10628 i.types[op].bitfield.disp64 = 0;
a775efc8 10629 i.types[op].bitfield.disp32 = 1;
40fb9820 10630 }
20f0a1fc 10631
63112cd6 10632 if (!i.tm.opcode_modifier.sib)
6c30d220 10633 i.rm.regmem = i.base_reg->reg_num;
80d61d8d 10634 set_rex_rex2 (i.base_reg, REX_B);
29b0f896
AM
10635 i.sib.base = i.base_reg->reg_num;
10636 /* x86-64 ignores REX prefix bit here to avoid decoder
10637 complications. */
848930b2
JB
10638 if (!(i.base_reg->reg_flags & RegRex)
10639 && (i.base_reg->reg_num == EBP_REG_NUM
10640 || i.base_reg->reg_num == ESP_REG_NUM))
5e042380 10641 default_seg = reg_ss;
848930b2 10642 if (i.base_reg->reg_num == 5 && i.disp_operands == 0)
29b0f896 10643 {
848930b2 10644 fake_zero_displacement = 1;
1a02d6b0
L
10645 if (i.disp_encoding == disp_encoding_32bit)
10646 i.types[op].bitfield.disp32 = 1;
10647 else
10648 i.types[op].bitfield.disp8 = 1;
29b0f896
AM
10649 }
10650 i.sib.scale = i.log2_scale_factor;
10651 if (i.index_reg == 0)
10652 {
260cd341
LC
10653 /* Only check for VSIB. */
10654 gas_assert (i.tm.opcode_modifier.sib != VECSIB128
10655 && i.tm.opcode_modifier.sib != VECSIB256
10656 && i.tm.opcode_modifier.sib != VECSIB512);
10657
29b0f896
AM
10658 /* <disp>(%esp) becomes two byte modrm with no index
10659 register. We've already stored the code for esp
10660 in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
10661 Any base register besides %esp will not use the
10662 extra modrm byte. */
10663 i.sib.index = NO_INDEX_REGISTER;
29b0f896 10664 }
63112cd6 10665 else if (!i.tm.opcode_modifier.sib)
29b0f896 10666 {
e968fc9b 10667 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
10668 i.sib.index = NO_INDEX_REGISTER;
10669 else
10670 i.sib.index = i.index_reg->reg_num;
29b0f896 10671 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
80d61d8d 10672 set_rex_rex2 (i.index_reg, REX_X);
29b0f896 10673 }
67a4f2b7
AO
10674
10675 if (i.disp_operands
10676 && (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
10677 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL))
10678 i.rm.mode = 0;
10679 else
a501d77e
L
10680 {
10681 if (!fake_zero_displacement
10682 && !i.disp_operands
10683 && i.disp_encoding)
10684 {
10685 fake_zero_displacement = 1;
10686 if (i.disp_encoding == disp_encoding_8bit)
10687 i.types[op].bitfield.disp8 = 1;
10688 else
10689 i.types[op].bitfield.disp32 = 1;
10690 }
10691 i.rm.mode = mode_from_disp_size (i.types[op]);
10692 }
29b0f896 10693 }
252b5132 10694
29b0f896
AM
10695 if (fake_zero_displacement)
10696 {
10697 /* Fakes a zero displacement assuming that i.types[op]
10698 holds the correct displacement size. */
10699 expressionS *exp;
10700
9c2799c2 10701 gas_assert (i.op[op].disps == 0);
29b0f896
AM
10702 exp = &disp_expressions[i.disp_operands++];
10703 i.op[op].disps = exp;
10704 exp->X_op = O_constant;
10705 exp->X_add_number = 0;
10706 exp->X_add_symbol = (symbolS *) 0;
10707 exp->X_op_symbol = (symbolS *) 0;
10708 }
10709 }
ecb96e55
JB
10710 else
10711 {
10712 i.rm.mode = 3;
10713 i.rm.regmem = i.op[op].regs->reg_num;
10714 set_rex_vrex (i.op[op].regs, REX_B, false);
10715 }
252b5132 10716
ecb96e55
JB
10717 if (op == dest)
10718 dest = ~0;
10719 if (op == source)
10720 source = ~0;
10721 }
10722 else
10723 {
10724 i.rm.mode = 3;
10725 if (!i.tm.opcode_modifier.regmem)
f88c9eb0 10726 {
ecb96e55
JB
10727 gas_assert (source < MAX_OPERANDS);
10728 i.rm.regmem = i.op[source].regs->reg_num;
10729 set_rex_vrex (i.op[source].regs, REX_B,
10730 dest >= MAX_OPERANDS && i.tm.opcode_modifier.sse2avx);
10731 source = ~0;
f88c9eb0 10732 }
ecb96e55 10733 else
29b0f896 10734 {
ecb96e55
JB
10735 gas_assert (dest < MAX_OPERANDS);
10736 i.rm.regmem = i.op[dest].regs->reg_num;
10737 set_rex_vrex (i.op[dest].regs, REX_B, i.tm.opcode_modifier.sse2avx);
10738 dest = ~0;
29b0f896 10739 }
ecb96e55 10740 }
252b5132 10741
ecb96e55
JB
10742 /* Fill in i.rm.reg field with extension opcode (if any) or the
10743 appropriate register. */
10744 if (i.tm.extension_opcode != None)
10745 i.rm.reg = i.tm.extension_opcode;
10746 else if (!i.tm.opcode_modifier.regmem && dest < MAX_OPERANDS)
10747 {
10748 i.rm.reg = i.op[dest].regs->reg_num;
10749 set_rex_vrex (i.op[dest].regs, REX_R, i.tm.opcode_modifier.sse2avx);
10750 }
10751 else
10752 {
10753 gas_assert (source < MAX_OPERANDS);
10754 i.rm.reg = i.op[source].regs->reg_num;
10755 set_rex_vrex (i.op[source].regs, REX_R, false);
29b0f896 10756 }
ecb96e55
JB
10757
10758 if (flag_code != CODE_64BIT && (i.rex & REX_R))
10759 {
10760 gas_assert (i.types[!i.tm.opcode_modifier.regmem].bitfield.class == RegCR);
10761 i.rex &= ~REX_R;
10762 add_prefix (LOCK_PREFIX_OPCODE);
10763 }
10764
29b0f896
AM
10765 return default_seg;
10766}
252b5132 10767
48ef937e
JB
10768static INLINE void
10769frag_opcode_byte (unsigned char byte)
10770{
10771 if (now_seg != absolute_section)
10772 FRAG_APPEND_1_CHAR (byte);
10773 else
10774 ++abs_section_offset;
10775}
10776
376cd056
JB
10777static unsigned int
10778flip_code16 (unsigned int code16)
10779{
10780 gas_assert (i.tm.operands == 1);
10781
10782 return !(i.prefix[REX_PREFIX] & REX_W)
10783 && (code16 ? i.tm.operand_types[0].bitfield.disp32
376cd056
JB
10784 : i.tm.operand_types[0].bitfield.disp16)
10785 ? CODE16 : 0;
10786}
10787
29b0f896 10788static void
e3bb37b5 10789output_branch (void)
29b0f896
AM
10790{
10791 char *p;
f8a5c266 10792 int size;
29b0f896
AM
10793 int code16;
10794 int prefix;
10795 relax_substateT subtype;
10796 symbolS *sym;
10797 offsetT off;
10798
48ef937e
JB
10799 if (now_seg == absolute_section)
10800 {
10801 as_bad (_("relaxable branches not supported in absolute section"));
10802 return;
10803 }
10804
f8a5c266 10805 code16 = flag_code == CODE_16BIT ? CODE16 : 0;
1a42a9fe 10806 size = i.disp_encoding > disp_encoding_8bit ? BIG : SMALL;
29b0f896
AM
10807
10808 prefix = 0;
10809 if (i.prefix[DATA_PREFIX] != 0)
252b5132 10810 {
29b0f896
AM
10811 prefix = 1;
10812 i.prefixes -= 1;
376cd056 10813 code16 ^= flip_code16(code16);
252b5132 10814 }
29b0f896
AM
10815 /* Pentium4 branch hints. */
10816 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
10817 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
2f66722d 10818 {
29b0f896
AM
10819 prefix++;
10820 i.prefixes--;
10821 }
10822 if (i.prefix[REX_PREFIX] != 0)
10823 {
10824 prefix++;
10825 i.prefixes--;
2f66722d
AM
10826 }
10827
7e8b059b
L
10828 /* BND prefixed jump. */
10829 if (i.prefix[BND_PREFIX] != 0)
10830 {
6cb0a70e
JB
10831 prefix++;
10832 i.prefixes--;
7e8b059b
L
10833 }
10834
f2810fe0 10835 if (i.prefixes != 0)
76d3f746 10836 as_warn (_("skipping prefixes on `%s'"), insn_name (&i.tm));
29b0f896
AM
10837
10838 /* It's always a symbol; End frag & setup for relax.
10839 Make sure there is enough room in this frag for the largest
10840 instruction we may generate in md_convert_frag. This is 2
10841 bytes for the opcode and room for the prefix and largest
10842 displacement. */
10843 frag_grow (prefix + 2 + 4);
10844 /* Prefix and 1 opcode byte go in fr_fix. */
10845 p = frag_more (prefix + 1);
10846 if (i.prefix[DATA_PREFIX] != 0)
10847 *p++ = DATA_PREFIX_OPCODE;
10848 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
10849 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
10850 *p++ = i.prefix[SEG_PREFIX];
6cb0a70e
JB
10851 if (i.prefix[BND_PREFIX] != 0)
10852 *p++ = BND_PREFIX_OPCODE;
29b0f896
AM
10853 if (i.prefix[REX_PREFIX] != 0)
10854 *p++ = i.prefix[REX_PREFIX];
10855 *p = i.tm.base_opcode;
10856
10857 if ((unsigned char) *p == JUMP_PC_RELATIVE)
f8a5c266 10858 subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, size);
40fb9820 10859 else if (cpu_arch_flags.bitfield.cpui386)
f8a5c266 10860 subtype = ENCODE_RELAX_STATE (COND_JUMP, size);
29b0f896 10861 else
f8a5c266 10862 subtype = ENCODE_RELAX_STATE (COND_JUMP86, size);
29b0f896 10863 subtype |= code16;
3e73aa7c 10864
29b0f896
AM
10865 sym = i.op[0].disps->X_add_symbol;
10866 off = i.op[0].disps->X_add_number;
3e73aa7c 10867
29b0f896
AM
10868 if (i.op[0].disps->X_op != O_constant
10869 && i.op[0].disps->X_op != O_symbol)
3e73aa7c 10870 {
29b0f896
AM
10871 /* Handle complex expressions. */
10872 sym = make_expr_symbol (i.op[0].disps);
10873 off = 0;
10874 }
3e73aa7c 10875
29b0f896
AM
10876 /* 1 possible extra opcode + 4 byte displacement go in var part.
10877 Pass reloc in fr_var. */
d258b828 10878 frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
29b0f896 10879}
3e73aa7c 10880
bd7ab16b
L
10881#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
10882/* Return TRUE iff PLT32 relocation should be used for branching to
10883 symbol S. */
10884
5b7c81bd 10885static bool
bd7ab16b
L
10886need_plt32_p (symbolS *s)
10887{
10888 /* PLT32 relocation is ELF only. */
10889 if (!IS_ELF)
5b7c81bd 10890 return false;
bd7ab16b 10891
a5def729
RO
10892#ifdef TE_SOLARIS
10893 /* Don't emit PLT32 relocation on Solaris: neither native linker nor
10894 krtld support it. */
5b7c81bd 10895 return false;
a5def729
RO
10896#endif
10897
bd7ab16b
L
10898 /* Since there is no need to prepare for PLT branch on x86-64, we
10899 can generate R_X86_64_PLT32, instead of R_X86_64_PC32, which can
10900 be used as a marker for 32-bit PC-relative branches. */
10901 if (!object_64bit)
5b7c81bd 10902 return false;
bd7ab16b 10903
44365e88 10904 if (s == NULL)
5b7c81bd 10905 return false;
44365e88 10906
bd7ab16b
L
10907 /* Weak or undefined symbol need PLT32 relocation. */
10908 if (S_IS_WEAK (s) || !S_IS_DEFINED (s))
5b7c81bd 10909 return true;
bd7ab16b
L
10910
10911 /* Non-global symbol doesn't need PLT32 relocation. */
10912 if (! S_IS_EXTERNAL (s))
5b7c81bd 10913 return false;
bd7ab16b
L
10914
10915 /* Other global symbols need PLT32 relocation. NB: Symbol with
10916 non-default visibilities are treated as normal global symbol
10917 so that PLT32 relocation can be used as a marker for 32-bit
10918 PC-relative branches. It is useful for linker relaxation. */
5b7c81bd 10919 return true;
bd7ab16b
L
10920}
10921#endif
10922
29b0f896 10923static void
e3bb37b5 10924output_jump (void)
29b0f896
AM
10925{
10926 char *p;
10927 int size;
3e02c1cc 10928 fixS *fixP;
bd7ab16b 10929 bfd_reloc_code_real_type jump_reloc = i.reloc[0];
29b0f896 10930
0cfa3eb3 10931 if (i.tm.opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
10932 {
10933 /* This is a loop or jecxz type instruction. */
10934 size = 1;
10935 if (i.prefix[ADDR_PREFIX] != 0)
10936 {
48ef937e 10937 frag_opcode_byte (ADDR_PREFIX_OPCODE);
29b0f896
AM
10938 i.prefixes -= 1;
10939 }
10940 /* Pentium4 branch hints. */
10941 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
10942 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
10943 {
48ef937e 10944 frag_opcode_byte (i.prefix[SEG_PREFIX]);
29b0f896 10945 i.prefixes--;
3e73aa7c
JH
10946 }
10947 }
29b0f896
AM
10948 else
10949 {
10950 int code16;
3e73aa7c 10951
29b0f896
AM
10952 code16 = 0;
10953 if (flag_code == CODE_16BIT)
10954 code16 = CODE16;
3e73aa7c 10955
29b0f896
AM
10956 if (i.prefix[DATA_PREFIX] != 0)
10957 {
48ef937e 10958 frag_opcode_byte (DATA_PREFIX_OPCODE);
29b0f896 10959 i.prefixes -= 1;
376cd056 10960 code16 ^= flip_code16(code16);
29b0f896 10961 }
252b5132 10962
29b0f896
AM
10963 size = 4;
10964 if (code16)
10965 size = 2;
10966 }
9fcc94b6 10967
6cb0a70e
JB
10968 /* BND prefixed jump. */
10969 if (i.prefix[BND_PREFIX] != 0)
29b0f896 10970 {
48ef937e 10971 frag_opcode_byte (i.prefix[BND_PREFIX]);
29b0f896
AM
10972 i.prefixes -= 1;
10973 }
252b5132 10974
6cb0a70e 10975 if (i.prefix[REX_PREFIX] != 0)
7e8b059b 10976 {
48ef937e 10977 frag_opcode_byte (i.prefix[REX_PREFIX]);
7e8b059b
L
10978 i.prefixes -= 1;
10979 }
10980
f2810fe0 10981 if (i.prefixes != 0)
76d3f746 10982 as_warn (_("skipping prefixes on `%s'"), insn_name (&i.tm));
e0890092 10983
48ef937e
JB
10984 if (now_seg == absolute_section)
10985 {
9a182d04 10986 abs_section_offset += i.opcode_length + size;
48ef937e
JB
10987 return;
10988 }
10989
9a182d04
JB
10990 p = frag_more (i.opcode_length + size);
10991 switch (i.opcode_length)
42164a71
L
10992 {
10993 case 2:
10994 *p++ = i.tm.base_opcode >> 8;
1a0670f3 10995 /* Fall through. */
42164a71
L
10996 case 1:
10997 *p++ = i.tm.base_opcode;
10998 break;
10999 default:
11000 abort ();
11001 }
e0890092 11002
bd7ab16b 11003#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1ef3994a
JB
11004 if (flag_code == CODE_64BIT && size == 4
11005 && jump_reloc == NO_RELOC && i.op[0].disps->X_add_number == 0
bd7ab16b
L
11006 && need_plt32_p (i.op[0].disps->X_add_symbol))
11007 jump_reloc = BFD_RELOC_X86_64_PLT32;
11008#endif
11009
11010 jump_reloc = reloc (size, 1, 1, jump_reloc);
11011
3e02c1cc 11012 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
bd7ab16b 11013 i.op[0].disps, 1, jump_reloc);
3e02c1cc 11014
eb19308f
JB
11015 /* All jumps handled here are signed, but don't unconditionally use a
11016 signed limit check for 32 and 16 bit jumps as we want to allow wrap
11017 around at 4G (outside of 64-bit mode) and 64k (except for XBEGIN)
11018 respectively. */
11019 switch (size)
11020 {
11021 case 1:
11022 fixP->fx_signed = 1;
11023 break;
11024
11025 case 2:
7fc69528 11026 if (i.tm.mnem_off == MN_xbegin)
eb19308f
JB
11027 fixP->fx_signed = 1;
11028 break;
11029
11030 case 4:
11031 if (flag_code == CODE_64BIT)
11032 fixP->fx_signed = 1;
11033 break;
11034 }
29b0f896 11035}
e0890092 11036
29b0f896 11037static void
e3bb37b5 11038output_interseg_jump (void)
29b0f896
AM
11039{
11040 char *p;
11041 int size;
11042 int prefix;
11043 int code16;
252b5132 11044
29b0f896
AM
11045 code16 = 0;
11046 if (flag_code == CODE_16BIT)
11047 code16 = CODE16;
a217f122 11048
29b0f896
AM
11049 prefix = 0;
11050 if (i.prefix[DATA_PREFIX] != 0)
11051 {
11052 prefix = 1;
11053 i.prefixes -= 1;
11054 code16 ^= CODE16;
11055 }
6cb0a70e
JB
11056
11057 gas_assert (!i.prefix[REX_PREFIX]);
252b5132 11058
29b0f896
AM
11059 size = 4;
11060 if (code16)
11061 size = 2;
252b5132 11062
f2810fe0 11063 if (i.prefixes != 0)
76d3f746 11064 as_warn (_("skipping prefixes on `%s'"), insn_name (&i.tm));
252b5132 11065
48ef937e
JB
11066 if (now_seg == absolute_section)
11067 {
11068 abs_section_offset += prefix + 1 + 2 + size;
11069 return;
11070 }
11071
29b0f896
AM
11072 /* 1 opcode; 2 segment; offset */
11073 p = frag_more (prefix + 1 + 2 + size);
3e73aa7c 11074
29b0f896
AM
11075 if (i.prefix[DATA_PREFIX] != 0)
11076 *p++ = DATA_PREFIX_OPCODE;
252b5132 11077
29b0f896
AM
11078 if (i.prefix[REX_PREFIX] != 0)
11079 *p++ = i.prefix[REX_PREFIX];
252b5132 11080
29b0f896
AM
11081 *p++ = i.tm.base_opcode;
11082 if (i.op[1].imms->X_op == O_constant)
11083 {
11084 offsetT n = i.op[1].imms->X_add_number;
252b5132 11085
29b0f896
AM
11086 if (size == 2
11087 && !fits_in_unsigned_word (n)
11088 && !fits_in_signed_word (n))
11089 {
11090 as_bad (_("16-bit jump out of range"));
11091 return;
11092 }
11093 md_number_to_chars (p, n, size);
11094 }
11095 else
11096 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
d258b828 11097 i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
6d96a594
C
11098
11099 p += size;
11100 if (i.op[0].imms->X_op == O_constant)
11101 md_number_to_chars (p, (valueT) i.op[0].imms->X_add_number, 2);
11102 else
11103 fix_new_exp (frag_now, p - frag_now->fr_literal, 2,
11104 i.op[0].imms, 0, reloc (2, 0, 0, i.reloc[0]));
29b0f896 11105}
a217f122 11106
b4a3a7b4
L
11107#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
11108void
11109x86_cleanup (void)
11110{
11111 char *p;
11112 asection *seg = now_seg;
11113 subsegT subseg = now_subseg;
11114 asection *sec;
11115 unsigned int alignment, align_size_1;
11116 unsigned int isa_1_descsz, feature_2_descsz, descsz;
11117 unsigned int isa_1_descsz_raw, feature_2_descsz_raw;
11118 unsigned int padding;
11119
1273b2f8 11120 if (!IS_ELF || !x86_used_note)
b4a3a7b4
L
11121 return;
11122
b4a3a7b4
L
11123 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X86;
11124
11125 /* The .note.gnu.property section layout:
11126
11127 Field Length Contents
11128 ---- ---- ----
11129 n_namsz 4 4
11130 n_descsz 4 The note descriptor size
11131 n_type 4 NT_GNU_PROPERTY_TYPE_0
11132 n_name 4 "GNU"
11133 n_desc n_descsz The program property array
11134 .... .... ....
11135 */
11136
11137 /* Create the .note.gnu.property section. */
11138 sec = subseg_new (NOTE_GNU_PROPERTY_SECTION_NAME, 0);
fd361982 11139 bfd_set_section_flags (sec,
b4a3a7b4
L
11140 (SEC_ALLOC
11141 | SEC_LOAD
11142 | SEC_DATA
11143 | SEC_HAS_CONTENTS
11144 | SEC_READONLY));
11145
11146 if (get_elf_backend_data (stdoutput)->s->elfclass == ELFCLASS64)
11147 {
11148 align_size_1 = 7;
11149 alignment = 3;
11150 }
11151 else
11152 {
11153 align_size_1 = 3;
11154 alignment = 2;
11155 }
11156
fd361982 11157 bfd_set_section_alignment (sec, alignment);
b4a3a7b4
L
11158 elf_section_type (sec) = SHT_NOTE;
11159
1273b2f8
L
11160 /* GNU_PROPERTY_X86_ISA_1_USED: 4-byte type + 4-byte data size
11161 + 4-byte data */
11162 isa_1_descsz_raw = 4 + 4 + 4;
11163 /* Align GNU_PROPERTY_X86_ISA_1_USED. */
11164 isa_1_descsz = (isa_1_descsz_raw + align_size_1) & ~align_size_1;
b4a3a7b4
L
11165
11166 feature_2_descsz_raw = isa_1_descsz;
11167 /* GNU_PROPERTY_X86_FEATURE_2_USED: 4-byte type + 4-byte data size
1273b2f8 11168 + 4-byte data */
b4a3a7b4
L
11169 feature_2_descsz_raw += 4 + 4 + 4;
11170 /* Align GNU_PROPERTY_X86_FEATURE_2_USED. */
11171 feature_2_descsz = ((feature_2_descsz_raw + align_size_1)
11172 & ~align_size_1);
11173
11174 descsz = feature_2_descsz;
11175 /* Section size: n_namsz + n_descsz + n_type + n_name + n_descsz. */
11176 p = frag_more (4 + 4 + 4 + 4 + descsz);
11177
11178 /* Write n_namsz. */
11179 md_number_to_chars (p, (valueT) 4, 4);
11180
11181 /* Write n_descsz. */
11182 md_number_to_chars (p + 4, (valueT) descsz, 4);
11183
11184 /* Write n_type. */
11185 md_number_to_chars (p + 4 * 2, (valueT) NT_GNU_PROPERTY_TYPE_0, 4);
11186
11187 /* Write n_name. */
11188 memcpy (p + 4 * 3, "GNU", 4);
11189
1273b2f8
L
11190 /* Write 4-byte type. */
11191 md_number_to_chars (p + 4 * 4,
11192 (valueT) GNU_PROPERTY_X86_ISA_1_USED, 4);
b4a3a7b4 11193
1273b2f8
L
11194 /* Write 4-byte data size. */
11195 md_number_to_chars (p + 4 * 5, (valueT) 4, 4);
b4a3a7b4 11196
1273b2f8
L
11197 /* Write 4-byte data. */
11198 md_number_to_chars (p + 4 * 6, (valueT) x86_isa_1_used, 4);
b4a3a7b4 11199
1273b2f8
L
11200 /* Zero out paddings. */
11201 padding = isa_1_descsz - isa_1_descsz_raw;
11202 if (padding)
11203 memset (p + 4 * 7, 0, padding);
b4a3a7b4
L
11204
11205 /* Write 4-byte type. */
11206 md_number_to_chars (p + isa_1_descsz + 4 * 4,
11207 (valueT) GNU_PROPERTY_X86_FEATURE_2_USED, 4);
11208
11209 /* Write 4-byte data size. */
11210 md_number_to_chars (p + isa_1_descsz + 4 * 5, (valueT) 4, 4);
11211
11212 /* Write 4-byte data. */
11213 md_number_to_chars (p + isa_1_descsz + 4 * 6,
11214 (valueT) x86_feature_2_used, 4);
11215
11216 /* Zero out paddings. */
11217 padding = feature_2_descsz - feature_2_descsz_raw;
11218 if (padding)
11219 memset (p + isa_1_descsz + 4 * 7, 0, padding);
11220
11221 /* We probably can't restore the current segment, for there likely
11222 isn't one yet... */
11223 if (seg && subseg)
11224 subseg_set (seg, subseg);
11225}
b52c4ee4
IB
11226
11227bool
11228x86_support_sframe_p (void)
11229{
3e3e792a 11230 /* At this time, SFrame stack trace is supported for AMD64 ABI only. */
b52c4ee4
IB
11231 return (x86_elf_abi == X86_64_ABI);
11232}
11233
11234bool
11235x86_sframe_ra_tracking_p (void)
11236{
11237 /* In AMD64, return address is always stored on the stack at a fixed offset
11238 from the CFA (provided via x86_sframe_cfa_ra_offset ()).
11239 Do not track explicitly via an SFrame Frame Row Entry. */
11240 return false;
11241}
11242
11243offsetT
11244x86_sframe_cfa_ra_offset (void)
11245{
11246 gas_assert (x86_elf_abi == X86_64_ABI);
11247 return (offsetT) -8;
11248}
11249
11250unsigned char
11251x86_sframe_get_abi_arch (void)
11252{
11253 unsigned char sframe_abi_arch = 0;
11254
11255 if (x86_support_sframe_p ())
11256 {
11257 gas_assert (!target_big_endian);
11258 sframe_abi_arch = SFRAME_ABI_AMD64_ENDIAN_LITTLE;
11259 }
11260
11261 return sframe_abi_arch;
11262}
11263
b4a3a7b4
L
11264#endif
11265
9c33702b
JB
11266static unsigned int
11267encoding_length (const fragS *start_frag, offsetT start_off,
11268 const char *frag_now_ptr)
11269{
11270 unsigned int len = 0;
11271
11272 if (start_frag != frag_now)
11273 {
11274 const fragS *fr = start_frag;
11275
11276 do {
11277 len += fr->fr_fix;
11278 fr = fr->fr_next;
11279 } while (fr && fr != frag_now);
11280 }
11281
11282 return len - start_off + (frag_now_ptr - frag_now->fr_literal);
11283}
11284
e379e5f3 11285/* Return 1 for test, and, cmp, add, sub, inc and dec which may
79d72f45
HL
11286 be macro-fused with conditional jumps.
11287 NB: If TEST/AND/CMP/ADD/SUB/INC/DEC is of RIP relative address,
11288 or is one of the following format:
11289
11290 cmp m, imm
11291 add m, imm
11292 sub m, imm
11293 test m, imm
11294 and m, imm
11295 inc m
11296 dec m
11297
11298 it is unfusible. */
e379e5f3
L
11299
11300static int
79d72f45 11301maybe_fused_with_jcc_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
11302{
11303 /* No RIP address. */
11304 if (i.base_reg && i.base_reg->reg_num == RegIP)
11305 return 0;
11306
389d00a5 11307 /* No opcodes outside of base encoding space. */
ddb62495 11308 if (i.tm.opcode_space != SPACE_BASE)
e379e5f3
L
11309 return 0;
11310
79d72f45
HL
11311 /* add, sub without add/sub m, imm. */
11312 if (i.tm.base_opcode <= 5
e379e5f3
L
11313 || (i.tm.base_opcode >= 0x28 && i.tm.base_opcode <= 0x2d)
11314 || ((i.tm.base_opcode | 3) == 0x83
79d72f45 11315 && (i.tm.extension_opcode == 0x5
e379e5f3 11316 || i.tm.extension_opcode == 0x0)))
79d72f45
HL
11317 {
11318 *mf_cmp_p = mf_cmp_alu_cmp;
11319 return !(i.mem_operands && i.imm_operands);
11320 }
e379e5f3 11321
79d72f45
HL
11322 /* and without and m, imm. */
11323 if ((i.tm.base_opcode >= 0x20 && i.tm.base_opcode <= 0x25)
11324 || ((i.tm.base_opcode | 3) == 0x83
11325 && i.tm.extension_opcode == 0x4))
11326 {
11327 *mf_cmp_p = mf_cmp_test_and;
11328 return !(i.mem_operands && i.imm_operands);
11329 }
11330
11331 /* test without test m imm. */
e379e5f3
L
11332 if ((i.tm.base_opcode | 1) == 0x85
11333 || (i.tm.base_opcode | 1) == 0xa9
11334 || ((i.tm.base_opcode | 1) == 0xf7
79d72f45
HL
11335 && i.tm.extension_opcode == 0))
11336 {
11337 *mf_cmp_p = mf_cmp_test_and;
11338 return !(i.mem_operands && i.imm_operands);
11339 }
11340
11341 /* cmp without cmp m, imm. */
11342 if ((i.tm.base_opcode >= 0x38 && i.tm.base_opcode <= 0x3d)
e379e5f3
L
11343 || ((i.tm.base_opcode | 3) == 0x83
11344 && (i.tm.extension_opcode == 0x7)))
79d72f45
HL
11345 {
11346 *mf_cmp_p = mf_cmp_alu_cmp;
11347 return !(i.mem_operands && i.imm_operands);
11348 }
e379e5f3 11349
79d72f45 11350 /* inc, dec without inc/dec m. */
734dfd1c 11351 if ((is_cpu (&i.tm, CpuNo64)
e379e5f3
L
11352 && (i.tm.base_opcode | 0xf) == 0x4f)
11353 || ((i.tm.base_opcode | 1) == 0xff
11354 && i.tm.extension_opcode <= 0x1))
79d72f45
HL
11355 {
11356 *mf_cmp_p = mf_cmp_incdec;
11357 return !i.mem_operands;
11358 }
e379e5f3
L
11359
11360 return 0;
11361}
11362
11363/* Return 1 if a FUSED_JCC_PADDING frag should be generated. */
11364
11365static int
b5482fe5
JB
11366add_fused_jcc_padding_frag_p (enum mf_cmp_kind *mf_cmp_p,
11367 const struct last_insn *last_insn)
e379e5f3
L
11368{
11369 /* NB: Don't work with COND_JUMP86 without i386. */
11370 if (!align_branch_power
11371 || now_seg == absolute_section
11372 || !cpu_arch_flags.bitfield.cpui386
11373 || !(align_branch & align_branch_fused_bit))
11374 return 0;
11375
79d72f45 11376 if (maybe_fused_with_jcc_p (mf_cmp_p))
e379e5f3 11377 {
b5482fe5 11378 if (last_insn->kind == last_insn_other)
e379e5f3
L
11379 return 1;
11380 if (flag_debug)
b5482fe5 11381 as_warn_where (last_insn->file, last_insn->line,
e379e5f3 11382 _("`%s` skips -malign-branch-boundary on `%s`"),
b5482fe5 11383 last_insn->name, insn_name (&i.tm));
e379e5f3
L
11384 }
11385
11386 return 0;
11387}
11388
11389/* Return 1 if a BRANCH_PREFIX frag should be generated. */
11390
11391static int
b5482fe5 11392add_branch_prefix_frag_p (const struct last_insn *last_insn)
e379e5f3
L
11393{
11394 /* NB: Don't work with COND_JUMP86 without i386. Don't add prefix
11395 to PadLock instructions since they include prefixes in opcode. */
11396 if (!align_branch_power
11397 || !align_branch_prefix_size
11398 || now_seg == absolute_section
734dfd1c 11399 || is_cpu (&i.tm, CpuPadLock)
e379e5f3
L
11400 || !cpu_arch_flags.bitfield.cpui386)
11401 return 0;
11402
11403 /* Don't add prefix if it is a prefix or there is no operand in case
11404 that segment prefix is special. */
11405 if (!i.operands || i.tm.opcode_modifier.isprefix)
11406 return 0;
11407
b5482fe5 11408 if (last_insn->kind == last_insn_other)
e379e5f3
L
11409 return 1;
11410
11411 if (flag_debug)
b5482fe5 11412 as_warn_where (last_insn->file, last_insn->line,
e379e5f3 11413 _("`%s` skips -malign-branch-boundary on `%s`"),
b5482fe5 11414 last_insn->name, insn_name (&i.tm));
e379e5f3
L
11415
11416 return 0;
11417}
11418
11419/* Return 1 if a BRANCH_PADDING frag should be generated. */
11420
11421static int
79d72f45 11422add_branch_padding_frag_p (enum align_branch_kind *branch_p,
b5482fe5
JB
11423 enum mf_jcc_kind *mf_jcc_p,
11424 const struct last_insn *last_insn)
e379e5f3
L
11425{
11426 int add_padding;
11427
11428 /* NB: Don't work with COND_JUMP86 without i386. */
11429 if (!align_branch_power
11430 || now_seg == absolute_section
389d00a5 11431 || !cpu_arch_flags.bitfield.cpui386
ddb62495 11432 || i.tm.opcode_space != SPACE_BASE)
e379e5f3
L
11433 return 0;
11434
11435 add_padding = 0;
11436
11437 /* Check for jcc and direct jmp. */
11438 if (i.tm.opcode_modifier.jump == JUMP)
11439 {
11440 if (i.tm.base_opcode == JUMP_PC_RELATIVE)
11441 {
11442 *branch_p = align_branch_jmp;
11443 add_padding = align_branch & align_branch_jmp_bit;
11444 }
11445 else
11446 {
79d72f45
HL
11447 /* Because J<cc> and JN<cc> share same group in macro-fusible table,
11448 igore the lowest bit. */
11449 *mf_jcc_p = (i.tm.base_opcode & 0x0e) >> 1;
e379e5f3
L
11450 *branch_p = align_branch_jcc;
11451 if ((align_branch & align_branch_jcc_bit))
11452 add_padding = 1;
11453 }
11454 }
e379e5f3
L
11455 else if ((i.tm.base_opcode | 1) == 0xc3)
11456 {
11457 /* Near ret. */
11458 *branch_p = align_branch_ret;
11459 if ((align_branch & align_branch_ret_bit))
11460 add_padding = 1;
11461 }
11462 else
11463 {
11464 /* Check for indirect jmp, direct and indirect calls. */
11465 if (i.tm.base_opcode == 0xe8)
11466 {
11467 /* Direct call. */
11468 *branch_p = align_branch_call;
11469 if ((align_branch & align_branch_call_bit))
11470 add_padding = 1;
11471 }
11472 else if (i.tm.base_opcode == 0xff
11473 && (i.tm.extension_opcode == 2
11474 || i.tm.extension_opcode == 4))
11475 {
11476 /* Indirect call and jmp. */
11477 *branch_p = align_branch_indirect;
11478 if ((align_branch & align_branch_indirect_bit))
11479 add_padding = 1;
11480 }
11481
11482 if (add_padding
11483 && i.disp_operands
11484 && tls_get_addr
11485 && (i.op[0].disps->X_op == O_symbol
11486 || (i.op[0].disps->X_op == O_subtract
11487 && i.op[0].disps->X_op_symbol == GOT_symbol)))
11488 {
11489 symbolS *s = i.op[0].disps->X_add_symbol;
11490 /* No padding to call to global or undefined tls_get_addr. */
11491 if ((S_IS_EXTERNAL (s) || !S_IS_DEFINED (s))
11492 && strcmp (S_GET_NAME (s), tls_get_addr) == 0)
11493 return 0;
11494 }
11495 }
11496
11497 if (add_padding
b5482fe5 11498 && last_insn->kind != last_insn_other)
e379e5f3
L
11499 {
11500 if (flag_debug)
b5482fe5 11501 as_warn_where (last_insn->file, last_insn->line,
e379e5f3 11502 _("`%s` skips -malign-branch-boundary on `%s`"),
b5482fe5 11503 last_insn->name, insn_name (&i.tm));
e379e5f3
L
11504 return 0;
11505 }
11506
11507 return add_padding;
11508}
11509
29b0f896 11510static void
b5482fe5 11511output_insn (const struct last_insn *last_insn)
29b0f896 11512{
2bbd9c25
JJ
11513 fragS *insn_start_frag;
11514 offsetT insn_start_off;
e379e5f3
L
11515 fragS *fragP = NULL;
11516 enum align_branch_kind branch = align_branch_none;
79d72f45
HL
11517 /* The initializer is arbitrary just to avoid uninitialized error.
11518 it's actually either assigned in add_branch_padding_frag_p
11519 or never be used. */
11520 enum mf_jcc_kind mf_jcc = mf_jcc_jo;
2bbd9c25 11521
b4a3a7b4 11522#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
48ef937e 11523 if (IS_ELF && x86_used_note && now_seg != absolute_section)
b4a3a7b4 11524 {
32930e4e 11525 if ((i.xstate & xstate_tmm) == xstate_tmm
734dfd1c 11526 || is_cpu (&i.tm, CpuAMX_TILE))
32930e4e
L
11527 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_TMM;
11528
734dfd1c
JB
11529 if (is_cpu (&i.tm, Cpu8087)
11530 || is_cpu (&i.tm, Cpu287)
11531 || is_cpu (&i.tm, Cpu387)
11532 || is_cpu (&i.tm, Cpu687)
11533 || is_cpu (&i.tm, CpuFISTTP))
b4a3a7b4 11534 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X87;
014d61ea 11535
921eafea 11536 if ((i.xstate & xstate_mmx)
7fc69528
JB
11537 || i.tm.mnem_off == MN_emms
11538 || i.tm.mnem_off == MN_femms)
b4a3a7b4 11539 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MMX;
014d61ea 11540
32930e4e
L
11541 if (i.index_reg)
11542 {
11543 if (i.index_reg->reg_type.bitfield.zmmword)
11544 i.xstate |= xstate_zmm;
11545 else if (i.index_reg->reg_type.bitfield.ymmword)
11546 i.xstate |= xstate_ymm;
11547 else if (i.index_reg->reg_type.bitfield.xmmword)
11548 i.xstate |= xstate_xmm;
11549 }
014d61ea
JB
11550
11551 /* vzeroall / vzeroupper */
734dfd1c 11552 if (i.tm.base_opcode == 0x77 && is_cpu (&i.tm, CpuAVX))
014d61ea
JB
11553 i.xstate |= xstate_ymm;
11554
c4694f17 11555 if ((i.xstate & xstate_xmm)
389d00a5
JB
11556 /* ldmxcsr / stmxcsr / vldmxcsr / vstmxcsr */
11557 || (i.tm.base_opcode == 0xae
734dfd1c
JB
11558 && (is_cpu (&i.tm, CpuSSE)
11559 || is_cpu (&i.tm, CpuAVX)))
11560 || is_cpu (&i.tm, CpuWideKL)
11561 || is_cpu (&i.tm, CpuKL))
b4a3a7b4 11562 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XMM;
014d61ea 11563
921eafea 11564 if ((i.xstate & xstate_ymm) == xstate_ymm)
b4a3a7b4 11565 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_YMM;
921eafea 11566 if ((i.xstate & xstate_zmm) == xstate_zmm)
b4a3a7b4 11567 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_ZMM;
6225c532 11568 if (i.mask.reg || (i.xstate & xstate_mask) == xstate_mask)
32930e4e 11569 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MASK;
734dfd1c 11570 if (is_cpu (&i.tm, CpuFXSR))
b4a3a7b4 11571 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_FXSR;
734dfd1c 11572 if (is_cpu (&i.tm, CpuXsave))
b4a3a7b4 11573 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVE;
734dfd1c 11574 if (is_cpu (&i.tm, CpuXsaveopt))
b4a3a7b4 11575 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT;
734dfd1c 11576 if (is_cpu (&i.tm, CpuXSAVEC))
b4a3a7b4 11577 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEC;
b0ab0693
L
11578
11579 if (x86_feature_2_used
734dfd1c
JB
11580 || is_cpu (&i.tm, CpuCMOV)
11581 || is_cpu (&i.tm, CpuSYSCALL)
7fc69528 11582 || i.tm.mnem_off == MN_cmpxchg8b)
b0ab0693 11583 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_BASELINE;
734dfd1c
JB
11584 if (is_cpu (&i.tm, CpuSSE3)
11585 || is_cpu (&i.tm, CpuSSSE3)
11586 || is_cpu (&i.tm, CpuSSE4_1)
11587 || is_cpu (&i.tm, CpuSSE4_2)
11588 || is_cpu (&i.tm, CpuCX16)
11589 || is_cpu (&i.tm, CpuPOPCNT)
b0ab0693
L
11590 /* LAHF-SAHF insns in 64-bit mode. */
11591 || (flag_code == CODE_64BIT
35648716 11592 && (i.tm.base_opcode | 1) == 0x9f
ddb62495 11593 && i.tm.opcode_space == SPACE_BASE))
b0ab0693 11594 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V2;
734dfd1c
JB
11595 if (is_cpu (&i.tm, CpuAVX)
11596 || is_cpu (&i.tm, CpuAVX2)
a9860005
JB
11597 /* Any VEX encoded insns execpt for AVX512F, AVX512BW, AVX512DQ,
11598 XOP, FMA4, LPW, TBM, and AMX. */
b0ab0693 11599 || (i.tm.opcode_modifier.vex
734dfd1c
JB
11600 && !is_cpu (&i.tm, CpuAVX512F)
11601 && !is_cpu (&i.tm, CpuAVX512BW)
11602 && !is_cpu (&i.tm, CpuAVX512DQ)
11603 && !is_cpu (&i.tm, CpuXOP)
11604 && !is_cpu (&i.tm, CpuFMA4)
11605 && !is_cpu (&i.tm, CpuLWP)
11606 && !is_cpu (&i.tm, CpuTBM)
b0ab0693 11607 && !(x86_feature_2_used & GNU_PROPERTY_X86_FEATURE_2_TMM))
734dfd1c
JB
11608 || is_cpu (&i.tm, CpuF16C)
11609 || is_cpu (&i.tm, CpuFMA)
11610 || is_cpu (&i.tm, CpuLZCNT)
11611 || is_cpu (&i.tm, CpuMovbe)
11612 || is_cpu (&i.tm, CpuXSAVES)
b0ab0693
L
11613 || (x86_feature_2_used
11614 & (GNU_PROPERTY_X86_FEATURE_2_XSAVE
11615 | GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT
11616 | GNU_PROPERTY_X86_FEATURE_2_XSAVEC)) != 0)
11617 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V3;
734dfd1c
JB
11618 if (is_cpu (&i.tm, CpuAVX512F)
11619 || is_cpu (&i.tm, CpuAVX512BW)
11620 || is_cpu (&i.tm, CpuAVX512DQ)
11621 || is_cpu (&i.tm, CpuAVX512VL)
a9860005
JB
11622 /* Any EVEX encoded insns except for AVX512ER, AVX512PF,
11623 AVX512-4FMAPS, and AVX512-4VNNIW. */
b0ab0693 11624 || (i.tm.opcode_modifier.evex
734dfd1c
JB
11625 && !is_cpu (&i.tm, CpuAVX512ER)
11626 && !is_cpu (&i.tm, CpuAVX512PF)
11627 && !is_cpu (&i.tm, CpuAVX512_4FMAPS)
11628 && !is_cpu (&i.tm, CpuAVX512_4VNNIW)))
b0ab0693 11629 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V4;
b4a3a7b4
L
11630 }
11631#endif
11632
29b0f896
AM
11633 /* Tie dwarf2 debug info to the address at the start of the insn.
11634 We can't do this after the insn has been output as the current
11635 frag may have been closed off. eg. by frag_var. */
11636 dwarf2_emit_insn (0);
11637
2bbd9c25
JJ
11638 insn_start_frag = frag_now;
11639 insn_start_off = frag_now_fix ();
11640
b5482fe5 11641 if (add_branch_padding_frag_p (&branch, &mf_jcc, last_insn))
e379e5f3
L
11642 {
11643 char *p;
11644 /* Branch can be 8 bytes. Leave some room for prefixes. */
11645 unsigned int max_branch_padding_size = 14;
11646
11647 /* Align section to boundary. */
11648 record_alignment (now_seg, align_branch_power);
11649
11650 /* Make room for padding. */
11651 frag_grow (max_branch_padding_size);
11652
11653 /* Start of the padding. */
11654 p = frag_more (0);
11655
11656 fragP = frag_now;
11657
11658 frag_var (rs_machine_dependent, max_branch_padding_size, 0,
11659 ENCODE_RELAX_STATE (BRANCH_PADDING, 0),
11660 NULL, 0, p);
11661
79d72f45 11662 fragP->tc_frag_data.mf_type = mf_jcc;
e379e5f3
L
11663 fragP->tc_frag_data.branch_type = branch;
11664 fragP->tc_frag_data.max_bytes = max_branch_padding_size;
11665 }
11666
d59a54c2
JB
11667 if (!cpu_arch_flags.bitfield.cpui386 && (flag_code != CODE_16BIT)
11668 && !pre_386_16bit_warned)
11669 {
11670 as_warn (_("use .code16 to ensure correct addressing mode"));
11671 pre_386_16bit_warned = true;
11672 }
11673
29b0f896 11674 /* Output jumps. */
0cfa3eb3 11675 if (i.tm.opcode_modifier.jump == JUMP)
29b0f896 11676 output_branch ();
0cfa3eb3
JB
11677 else if (i.tm.opcode_modifier.jump == JUMP_BYTE
11678 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896 11679 output_jump ();
0cfa3eb3 11680 else if (i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT)
29b0f896
AM
11681 output_interseg_jump ();
11682 else
11683 {
11684 /* Output normal instructions here. */
11685 char *p;
11686 unsigned char *q;
47465058 11687 unsigned int j;
79d72f45 11688 enum mf_cmp_kind mf_cmp;
4dffcebc 11689
e4e00185 11690 if (avoid_fence
389d00a5
JB
11691 && (i.tm.base_opcode == 0xaee8
11692 || i.tm.base_opcode == 0xaef0
11693 || i.tm.base_opcode == 0xaef8))
48ef937e
JB
11694 {
11695 /* Encode lfence, mfence, and sfence as
11696 f0 83 04 24 00 lock addl $0x0, (%{re}sp). */
47f4115a 11697 if (flag_code == CODE_16BIT)
76d3f746 11698 as_bad (_("Cannot convert `%s' in 16-bit mode"), insn_name (&i.tm));
47f4115a
JB
11699 else if (omit_lock_prefix)
11700 as_bad (_("Cannot convert `%s' with `-momit-lock-prefix=yes' in effect"),
76d3f746 11701 insn_name (&i.tm));
47f4115a 11702 else if (now_seg != absolute_section)
48ef937e
JB
11703 {
11704 offsetT val = 0x240483f0ULL;
11705
11706 p = frag_more (5);
11707 md_number_to_chars (p, val, 5);
11708 }
11709 else
11710 abs_section_offset += 5;
11711 return;
11712 }
e4e00185 11713
d022bddd
IT
11714 /* Some processors fail on LOCK prefix. This options makes
11715 assembler ignore LOCK prefix and serves as a workaround. */
11716 if (omit_lock_prefix)
11717 {
35648716
JB
11718 if (i.tm.base_opcode == LOCK_PREFIX_OPCODE
11719 && i.tm.opcode_modifier.isprefix)
d022bddd
IT
11720 return;
11721 i.prefix[LOCK_PREFIX] = 0;
11722 }
11723
e379e5f3
L
11724 if (branch)
11725 /* Skip if this is a branch. */
11726 ;
b5482fe5 11727 else if (add_fused_jcc_padding_frag_p (&mf_cmp, last_insn))
e379e5f3
L
11728 {
11729 /* Make room for padding. */
11730 frag_grow (MAX_FUSED_JCC_PADDING_SIZE);
11731 p = frag_more (0);
11732
11733 fragP = frag_now;
11734
11735 frag_var (rs_machine_dependent, MAX_FUSED_JCC_PADDING_SIZE, 0,
11736 ENCODE_RELAX_STATE (FUSED_JCC_PADDING, 0),
11737 NULL, 0, p);
11738
79d72f45 11739 fragP->tc_frag_data.mf_type = mf_cmp;
e379e5f3
L
11740 fragP->tc_frag_data.branch_type = align_branch_fused;
11741 fragP->tc_frag_data.max_bytes = MAX_FUSED_JCC_PADDING_SIZE;
11742 }
b5482fe5 11743 else if (add_branch_prefix_frag_p (last_insn))
e379e5f3
L
11744 {
11745 unsigned int max_prefix_size = align_branch_prefix_size;
11746
11747 /* Make room for padding. */
11748 frag_grow (max_prefix_size);
11749 p = frag_more (0);
11750
11751 fragP = frag_now;
11752
11753 frag_var (rs_machine_dependent, max_prefix_size, 0,
11754 ENCODE_RELAX_STATE (BRANCH_PREFIX, 0),
11755 NULL, 0, p);
11756
11757 fragP->tc_frag_data.max_bytes = max_prefix_size;
11758 }
11759
43234a1e
L
11760 /* Since the VEX/EVEX prefix contains the implicit prefix, we
11761 don't need the explicit prefix. */
cf665fee 11762 if (!is_any_vex_encoding (&i.tm))
bc4bd9ab 11763 {
7b47a312 11764 switch (i.tm.opcode_modifier.opcodeprefix)
bc4bd9ab 11765 {
7b47a312
L
11766 case PREFIX_0X66:
11767 add_prefix (0x66);
11768 break;
11769 case PREFIX_0XF2:
11770 add_prefix (0xf2);
11771 break;
11772 case PREFIX_0XF3:
734dfd1c 11773 if (!is_cpu (&i.tm, CpuPadLock)
8b65b895
L
11774 || (i.prefix[REP_PREFIX] != 0xf3))
11775 add_prefix (0xf3);
c0f3af97 11776 break;
7b47a312 11777 case PREFIX_NONE:
9a182d04 11778 switch (i.opcode_length)
c0f3af97 11779 {
7b47a312 11780 case 2:
7b47a312 11781 break;
9a182d04 11782 case 1:
7b47a312 11783 /* Check for pseudo prefixes. */
9a182d04
JB
11784 if (!i.tm.opcode_modifier.isprefix || i.tm.base_opcode)
11785 break;
7b47a312
L
11786 as_bad_where (insn_start_frag->fr_file,
11787 insn_start_frag->fr_line,
11788 _("pseudo prefix without instruction"));
11789 return;
11790 default:
11791 abort ();
4dffcebc 11792 }
c0f3af97 11793 break;
c0f3af97
L
11794 default:
11795 abort ();
bc4bd9ab 11796 }
c0f3af97 11797
6d19a37a 11798#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
cf61b747
L
11799 /* For x32, add a dummy REX_OPCODE prefix for mov/add with
11800 R_X86_64_GOTTPOFF relocation so that linker can safely
14470f07
L
11801 perform IE->LE optimization. A dummy REX_OPCODE prefix
11802 is also needed for lea with R_X86_64_GOTPC32_TLSDESC
11803 relocation for GDesc -> IE/LE optimization. */
cf61b747 11804 if (x86_elf_abi == X86_64_X32_ABI
a533c8df 11805 && !is_apx_rex2_encoding ()
cf61b747 11806 && i.operands == 2
14470f07
L
11807 && (i.reloc[0] == BFD_RELOC_X86_64_GOTTPOFF
11808 || i.reloc[0] == BFD_RELOC_X86_64_GOTPC32_TLSDESC)
cf61b747
L
11809 && i.prefix[REX_PREFIX] == 0)
11810 add_prefix (REX_OPCODE);
6d19a37a 11811#endif
cf61b747 11812
c0f3af97
L
11813 /* The prefix bytes. */
11814 for (j = ARRAY_SIZE (i.prefix), q = i.prefix; j > 0; j--, q++)
11815 if (*q)
48ef937e 11816 frag_opcode_byte (*q);
80d61d8d
CL
11817
11818 if (is_apx_rex2_encoding ())
11819 {
11820 frag_opcode_byte (i.vex.bytes[0]);
11821 frag_opcode_byte (i.vex.bytes[1]);
11822 }
0f10071e 11823 }
ae5c1c7b 11824 else
c0f3af97
L
11825 {
11826 for (j = 0, q = i.prefix; j < ARRAY_SIZE (i.prefix); j++, q++)
11827 if (*q)
11828 switch (j)
11829 {
c0f3af97
L
11830 case SEG_PREFIX:
11831 case ADDR_PREFIX:
48ef937e 11832 frag_opcode_byte (*q);
c0f3af97
L
11833 break;
11834 default:
11835 /* There should be no other prefixes for instructions
11836 with VEX prefix. */
11837 abort ();
11838 }
11839
43234a1e
L
11840 /* For EVEX instructions i.vrex should become 0 after
11841 build_evex_prefix. For VEX instructions upper 16 registers
11842 aren't available, so VREX should be 0. */
11843 if (i.vrex)
11844 abort ();
c0f3af97 11845 /* Now the VEX prefix. */
48ef937e
JB
11846 if (now_seg != absolute_section)
11847 {
11848 p = frag_more (i.vex.length);
11849 for (j = 0; j < i.vex.length; j++)
11850 p[j] = i.vex.bytes[j];
11851 }
11852 else
11853 abs_section_offset += i.vex.length;
c0f3af97 11854 }
252b5132 11855
29b0f896 11856 /* Now the opcode; be careful about word order here! */
389d00a5
JB
11857 j = i.opcode_length;
11858 if (!i.vex.length)
ddb62495 11859 switch (i.tm.opcode_space)
389d00a5
JB
11860 {
11861 case SPACE_BASE:
11862 break;
11863 case SPACE_0F:
11864 ++j;
11865 break;
11866 case SPACE_0F38:
11867 case SPACE_0F3A:
11868 j += 2;
11869 break;
11870 default:
11871 abort ();
11872 }
11873
48ef937e 11874 if (now_seg == absolute_section)
389d00a5
JB
11875 abs_section_offset += j;
11876 else if (j == 1)
29b0f896
AM
11877 {
11878 FRAG_APPEND_1_CHAR (i.tm.base_opcode);
11879 }
11880 else
11881 {
389d00a5
JB
11882 p = frag_more (j);
11883 if (!i.vex.length
ddb62495 11884 && i.tm.opcode_space != SPACE_BASE)
389d00a5
JB
11885 {
11886 *p++ = 0x0f;
ddb62495
JB
11887 if (i.tm.opcode_space != SPACE_0F)
11888 *p++ = i.tm.opcode_space == SPACE_0F38
389d00a5
JB
11889 ? 0x38 : 0x3a;
11890 }
11891
9a182d04 11892 switch (i.opcode_length)
331d2d0d 11893 {
4dffcebc 11894 case 2:
389d00a5
JB
11895 /* Put out high byte first: can't use md_number_to_chars! */
11896 *p++ = (i.tm.base_opcode >> 8) & 0xff;
11897 /* Fall through. */
11898 case 1:
11899 *p = i.tm.base_opcode & 0xff;
4dffcebc
L
11900 break;
11901 default:
11902 abort ();
11903 break;
331d2d0d 11904 }
0f10071e 11905
29b0f896 11906 }
3e73aa7c 11907
29b0f896 11908 /* Now the modrm byte and sib byte (if present). */
40fb9820 11909 if (i.tm.opcode_modifier.modrm)
29b0f896 11910 {
48ef937e
JB
11911 frag_opcode_byte ((i.rm.regmem << 0)
11912 | (i.rm.reg << 3)
11913 | (i.rm.mode << 6));
29b0f896
AM
11914 /* If i.rm.regmem == ESP (4)
11915 && i.rm.mode != (Register mode)
11916 && not 16 bit
11917 ==> need second modrm byte. */
11918 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
11919 && i.rm.mode != 3
dc821c5f 11920 && !(i.base_reg && i.base_reg->reg_type.bitfield.word))
48ef937e
JB
11921 frag_opcode_byte ((i.sib.base << 0)
11922 | (i.sib.index << 3)
11923 | (i.sib.scale << 6));
29b0f896 11924 }
3e73aa7c 11925
29b0f896 11926 if (i.disp_operands)
2bbd9c25 11927 output_disp (insn_start_frag, insn_start_off);
3e73aa7c 11928
29b0f896 11929 if (i.imm_operands)
2bbd9c25 11930 output_imm (insn_start_frag, insn_start_off);
9c33702b
JB
11931
11932 /*
11933 * frag_now_fix () returning plain abs_section_offset when we're in the
11934 * absolute section, and abs_section_offset not getting updated as data
11935 * gets added to the frag breaks the logic below.
11936 */
11937 if (now_seg != absolute_section)
11938 {
11939 j = encoding_length (insn_start_frag, insn_start_off, frag_more (0));
11940 if (j > 15)
0afc614c
L
11941 {
11942 if (dot_insn ())
11943 as_warn (_("instruction length of %u bytes exceeds the limit of 15"),
11944 j);
11945 else
11946 as_bad (_("instruction length of %u bytes exceeds the limit of 15"),
11947 j);
11948 }
e379e5f3
L
11949 else if (fragP)
11950 {
11951 /* NB: Don't add prefix with GOTPC relocation since
11952 output_disp() above depends on the fixed encoding
11953 length. Can't add prefix with TLS relocation since
11954 it breaks TLS linker optimization. */
11955 unsigned int max = i.has_gotpc_tls_reloc ? 0 : 15 - j;
11956 /* Prefix count on the current instruction. */
11957 unsigned int count = i.vex.length;
11958 unsigned int k;
11959 for (k = 0; k < ARRAY_SIZE (i.prefix); k++)
11960 /* REX byte is encoded in VEX/EVEX prefix. */
11961 if (i.prefix[k] && (k != REX_PREFIX || !i.vex.length))
11962 count++;
11963
11964 /* Count prefixes for extended opcode maps. */
11965 if (!i.vex.length)
ddb62495 11966 switch (i.tm.opcode_space)
e379e5f3 11967 {
389d00a5 11968 case SPACE_BASE:
e379e5f3 11969 break;
389d00a5
JB
11970 case SPACE_0F:
11971 count++;
e379e5f3 11972 break;
389d00a5
JB
11973 case SPACE_0F38:
11974 case SPACE_0F3A:
11975 count += 2;
e379e5f3
L
11976 break;
11977 default:
11978 abort ();
11979 }
11980
11981 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
11982 == BRANCH_PREFIX)
11983 {
11984 /* Set the maximum prefix size in BRANCH_PREFIX
11985 frag. */
11986 if (fragP->tc_frag_data.max_bytes > max)
11987 fragP->tc_frag_data.max_bytes = max;
11988 if (fragP->tc_frag_data.max_bytes > count)
11989 fragP->tc_frag_data.max_bytes -= count;
11990 else
11991 fragP->tc_frag_data.max_bytes = 0;
11992 }
11993 else
11994 {
11995 /* Remember the maximum prefix size in FUSED_JCC_PADDING
11996 frag. */
11997 unsigned int max_prefix_size;
11998 if (align_branch_prefix_size > max)
11999 max_prefix_size = max;
12000 else
12001 max_prefix_size = align_branch_prefix_size;
12002 if (max_prefix_size > count)
12003 fragP->tc_frag_data.max_prefix_length
12004 = max_prefix_size - count;
12005 }
12006
12007 /* Use existing segment prefix if possible. Use CS
12008 segment prefix in 64-bit mode. In 32-bit mode, use SS
12009 segment prefix with ESP/EBP base register and use DS
12010 segment prefix without ESP/EBP base register. */
12011 if (i.prefix[SEG_PREFIX])
12012 fragP->tc_frag_data.default_prefix = i.prefix[SEG_PREFIX];
12013 else if (flag_code == CODE_64BIT)
12014 fragP->tc_frag_data.default_prefix = CS_PREFIX_OPCODE;
12015 else if (i.base_reg
12016 && (i.base_reg->reg_num == 4
12017 || i.base_reg->reg_num == 5))
12018 fragP->tc_frag_data.default_prefix = SS_PREFIX_OPCODE;
12019 else
12020 fragP->tc_frag_data.default_prefix = DS_PREFIX_OPCODE;
12021 }
9c33702b 12022 }
29b0f896 12023 }
252b5132 12024
e379e5f3
L
12025 /* NB: Don't work with COND_JUMP86 without i386. */
12026 if (align_branch_power
12027 && now_seg != absolute_section
12028 && cpu_arch_flags.bitfield.cpui386)
12029 {
12030 /* Terminate each frag so that we can add prefix and check for
12031 fused jcc. */
12032 frag_wane (frag_now);
12033 frag_new (0);
12034 }
12035
29b0f896
AM
12036#ifdef DEBUG386
12037 if (flag_debug)
12038 {
7b81dfbb 12039 pi ("" /*line*/, &i);
29b0f896
AM
12040 }
12041#endif /* DEBUG386 */
12042}
252b5132 12043
e205caa7
L
12044/* Return the size of the displacement operand N. */
12045
12046static int
12047disp_size (unsigned int n)
12048{
12049 int size = 4;
43234a1e 12050
b5014f7a 12051 if (i.types[n].bitfield.disp64)
40fb9820
L
12052 size = 8;
12053 else if (i.types[n].bitfield.disp8)
12054 size = 1;
12055 else if (i.types[n].bitfield.disp16)
12056 size = 2;
e205caa7
L
12057 return size;
12058}
12059
12060/* Return the size of the immediate operand N. */
12061
12062static int
12063imm_size (unsigned int n)
12064{
12065 int size = 4;
40fb9820
L
12066 if (i.types[n].bitfield.imm64)
12067 size = 8;
12068 else if (i.types[n].bitfield.imm8 || i.types[n].bitfield.imm8s)
12069 size = 1;
12070 else if (i.types[n].bitfield.imm16)
12071 size = 2;
e205caa7
L
12072 return size;
12073}
12074
29b0f896 12075static void
64e74474 12076output_disp (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
12077{
12078 char *p;
12079 unsigned int n;
252b5132 12080
29b0f896
AM
12081 for (n = 0; n < i.operands; n++)
12082 {
b5014f7a 12083 if (operand_type_check (i.types[n], disp))
29b0f896 12084 {
48ef937e
JB
12085 int size = disp_size (n);
12086
12087 if (now_seg == absolute_section)
12088 abs_section_offset += size;
12089 else if (i.op[n].disps->X_op == O_constant)
29b0f896 12090 {
43234a1e 12091 offsetT val = i.op[n].disps->X_add_number;
252b5132 12092
629cfaf1
JB
12093 val = offset_in_range (val >> (size == 1 ? i.memshift : 0),
12094 size);
29b0f896
AM
12095 p = frag_more (size);
12096 md_number_to_chars (p, val, size);
12097 }
12098 else
12099 {
f86103b7 12100 enum bfd_reloc_code_real reloc_type;
a775efc8
JB
12101 bool pcrel = (i.flags[n] & Operand_PCrel) != 0;
12102 bool sign = (flag_code == CODE_64BIT && size == 4
12103 && (!want_disp32 (&i.tm)
12104 || (i.tm.opcode_modifier.jump && !i.jumpabsolute
12105 && !i.types[n].bitfield.baseindex)))
12106 || pcrel;
02a86693 12107 fixS *fixP;
29b0f896 12108
e205caa7 12109 /* We can't have 8 bit displacement here. */
9c2799c2 12110 gas_assert (!i.types[n].bitfield.disp8);
e205caa7 12111
29b0f896
AM
12112 /* The PC relative address is computed relative
12113 to the instruction boundary, so in case immediate
12114 fields follows, we need to adjust the value. */
12115 if (pcrel && i.imm_operands)
12116 {
29b0f896 12117 unsigned int n1;
e205caa7 12118 int sz = 0;
252b5132 12119
29b0f896 12120 for (n1 = 0; n1 < i.operands; n1++)
40fb9820 12121 if (operand_type_check (i.types[n1], imm))
252b5132 12122 {
e205caa7 12123 /* Only one immediate is allowed for PC
e3bf0aad
JB
12124 relative address, except with .insn. */
12125 gas_assert (sz == 0 || dot_insn ());
12126 sz += imm_size (n1);
252b5132 12127 }
e3bf0aad 12128 /* We should find at least one immediate. */
9c2799c2 12129 gas_assert (sz != 0);
e3bf0aad 12130 i.op[n].disps->X_add_number -= sz;
29b0f896 12131 }
520dc8e8 12132
29b0f896 12133 p = frag_more (size);
d258b828 12134 reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
d6ab8113 12135 if (GOT_symbol
2bbd9c25 12136 && GOT_symbol == i.op[n].disps->X_add_symbol
d6ab8113 12137 && (((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
12138 || reloc_type == BFD_RELOC_X86_64_32S
12139 || (reloc_type == BFD_RELOC_64
12140 && object_64bit))
d6ab8113
JB
12141 && (i.op[n].disps->X_op == O_symbol
12142 || (i.op[n].disps->X_op == O_add
12143 && ((symbol_get_value_expression
12144 (i.op[n].disps->X_op_symbol)->X_op)
12145 == O_subtract))))
12146 || reloc_type == BFD_RELOC_32_PCREL))
2bbd9c25 12147 {
4fa24527 12148 if (!object_64bit)
7b81dfbb
AJ
12149 {
12150 reloc_type = BFD_RELOC_386_GOTPC;
5b7c81bd 12151 i.has_gotpc_tls_reloc = true;
98da05bf 12152 i.op[n].disps->X_add_number +=
d583596c 12153 encoding_length (insn_start_frag, insn_start_off, p);
7b81dfbb
AJ
12154 }
12155 else if (reloc_type == BFD_RELOC_64)
12156 reloc_type = BFD_RELOC_X86_64_GOTPC64;
d6ab8113 12157 else
7b81dfbb
AJ
12158 /* Don't do the adjustment for x86-64, as there
12159 the pcrel addressing is relative to the _next_
12160 insn, and that is taken care of in other code. */
d6ab8113 12161 reloc_type = BFD_RELOC_X86_64_GOTPC32;
2bbd9c25 12162 }
e379e5f3
L
12163 else if (align_branch_power)
12164 {
12165 switch (reloc_type)
12166 {
12167 case BFD_RELOC_386_TLS_GD:
12168 case BFD_RELOC_386_TLS_LDM:
12169 case BFD_RELOC_386_TLS_IE:
12170 case BFD_RELOC_386_TLS_IE_32:
12171 case BFD_RELOC_386_TLS_GOTIE:
12172 case BFD_RELOC_386_TLS_GOTDESC:
12173 case BFD_RELOC_386_TLS_DESC_CALL:
12174 case BFD_RELOC_X86_64_TLSGD:
12175 case BFD_RELOC_X86_64_TLSLD:
12176 case BFD_RELOC_X86_64_GOTTPOFF:
a533c8df 12177 case BFD_RELOC_X86_64_CODE_4_GOTTPOFF:
5bc71c2a 12178 case BFD_RELOC_X86_64_CODE_6_GOTTPOFF:
e379e5f3 12179 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
a533c8df 12180 case BFD_RELOC_X86_64_CODE_4_GOTPC32_TLSDESC:
e379e5f3 12181 case BFD_RELOC_X86_64_TLSDESC_CALL:
5b7c81bd 12182 i.has_gotpc_tls_reloc = true;
e379e5f3
L
12183 default:
12184 break;
12185 }
12186 }
02a86693
L
12187 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal,
12188 size, i.op[n].disps, pcrel,
12189 reloc_type);
eb19308f
JB
12190
12191 if (flag_code == CODE_64BIT && size == 4 && pcrel
12192 && !i.prefix[ADDR_PREFIX])
12193 fixP->fx_signed = 1;
12194
5bc71c2a
L
12195 if (reloc_type == BFD_RELOC_X86_64_GOTTPOFF
12196 && i.tm.opcode_space == SPACE_EVEXMAP4)
12197 {
12198 /* Only "add %reg1, foo@gottpoff(%rip), %reg2" is
12199 allowed in md_assemble. Set fx_tcbit2 for EVEX
12200 prefix. */
12201 fixP->fx_tcbit2 = 1;
12202 continue;
12203 }
12204
12205 if (i.base_reg && i.base_reg->reg_num == RegIP)
12206 {
12207 if (reloc_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC)
12208 {
12209 /* Set fx_tcbit for REX2 prefix. */
12210 if (is_apx_rex2_encoding ())
12211 fixP->fx_tcbit = 1;
12212 continue;
12213 }
12214 }
12215 /* In 64-bit, i386_validate_fix updates only (%rip)
12216 relocations. */
12217 else if (object_64bit)
12218 continue;
3d5a60de 12219
02a86693
L
12220 /* Check for "call/jmp *mem", "mov mem, %reg",
12221 "test %reg, mem" and "binop mem, %reg" where binop
12222 is one of adc, add, and, cmp, or, sbb, sub, xor
e60f4d3b
L
12223 instructions without data prefix. Always generate
12224 R_386_GOT32X for "sym*GOT" operand in 32-bit mode. */
12225 if (i.prefix[DATA_PREFIX] == 0
0cb4071e
L
12226 && (i.rm.mode == 2
12227 || (i.rm.mode == 0 && i.rm.regmem == 5))
ddb62495 12228 && i.tm.opcode_space == SPACE_BASE
02a86693
L
12229 && ((i.operands == 1
12230 && i.tm.base_opcode == 0xff
12231 && (i.rm.reg == 2 || i.rm.reg == 4))
12232 || (i.operands == 2
12233 && (i.tm.base_opcode == 0x8b
12234 || i.tm.base_opcode == 0x85
2ae4c703 12235 || (i.tm.base_opcode & ~0x38) == 0x03))))
02a86693
L
12236 {
12237 if (object_64bit)
12238 {
5bc71c2a
L
12239 if (reloc_type == BFD_RELOC_X86_64_GOTTPOFF)
12240 {
12241 /* Set fx_tcbit for REX2 prefix. */
12242 if (is_apx_rex2_encoding ())
12243 fixP->fx_tcbit = 1;
12244 }
820a7755 12245 else if (generate_relax_relocations)
5bc71c2a
L
12246 {
12247 /* Set fx_tcbit3 for REX2 prefix. */
12248 if (is_apx_rex2_encoding ())
12249 fixP->fx_tcbit3 = 1;
12250 else if (i.rex)
12251 fixP->fx_tcbit2 = 1;
12252 else
12253 fixP->fx_tcbit = 1;
12254 }
02a86693 12255 }
820a7755
JB
12256 else if (generate_relax_relocations
12257 || (i.rm.mode == 0 && i.rm.regmem == 5))
02a86693
L
12258 fixP->fx_tcbit2 = 1;
12259 }
29b0f896
AM
12260 }
12261 }
12262 }
12263}
252b5132 12264
29b0f896 12265static void
64e74474 12266output_imm (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
12267{
12268 char *p;
12269 unsigned int n;
252b5132 12270
29b0f896
AM
12271 for (n = 0; n < i.operands; n++)
12272 {
40fb9820 12273 if (operand_type_check (i.types[n], imm))
29b0f896 12274 {
48ef937e
JB
12275 int size = imm_size (n);
12276
12277 if (now_seg == absolute_section)
12278 abs_section_offset += size;
12279 else if (i.op[n].imms->X_op == O_constant)
29b0f896 12280 {
29b0f896 12281 offsetT val;
b4cac588 12282
29b0f896
AM
12283 val = offset_in_range (i.op[n].imms->X_add_number,
12284 size);
12285 p = frag_more (size);
12286 md_number_to_chars (p, val, size);
12287 }
12288 else
12289 {
12290 /* Not absolute_section.
12291 Need a 32-bit fixup (don't support 8bit
12292 non-absolute imms). Try to support other
12293 sizes ... */
f86103b7 12294 enum bfd_reloc_code_real reloc_type;
e205caa7 12295 int sign;
29b0f896 12296
40fb9820 12297 if (i.types[n].bitfield.imm32s
a7d61044 12298 && (i.suffix == QWORD_MNEM_SUFFIX
c032bc4f 12299 || (!i.suffix && i.tm.opcode_modifier.no_lsuf)
f79d55e1 12300 || (i.prefix[REX_PREFIX] & REX_W)
c032bc4f 12301 || dot_insn ()))
29b0f896 12302 sign = 1;
e205caa7
L
12303 else
12304 sign = 0;
520dc8e8 12305
29b0f896 12306 p = frag_more (size);
d258b828 12307 reloc_type = reloc (size, 0, sign, i.reloc[n]);
f86103b7 12308
2bbd9c25
JJ
12309 /* This is tough to explain. We end up with this one if we
12310 * have operands that look like
12311 * "_GLOBAL_OFFSET_TABLE_+[.-.L284]". The goal here is to
12312 * obtain the absolute address of the GOT, and it is strongly
12313 * preferable from a performance point of view to avoid using
12314 * a runtime relocation for this. The actual sequence of
12315 * instructions often look something like:
12316 *
12317 * call .L66
12318 * .L66:
12319 * popl %ebx
12320 * addl $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
12321 *
12322 * The call and pop essentially return the absolute address
12323 * of the label .L66 and store it in %ebx. The linker itself
12324 * will ultimately change the first operand of the addl so
12325 * that %ebx points to the GOT, but to keep things simple, the
12326 * .o file must have this operand set so that it generates not
12327 * the absolute address of .L66, but the absolute address of
12328 * itself. This allows the linker itself simply treat a GOTPC
12329 * relocation as asking for a pcrel offset to the GOT to be
12330 * added in, and the addend of the relocation is stored in the
12331 * operand field for the instruction itself.
12332 *
12333 * Our job here is to fix the operand so that it would add
12334 * the correct offset so that %ebx would point to itself. The
12335 * thing that is tricky is that .-.L66 will point to the
12336 * beginning of the instruction, so we need to further modify
12337 * the operand so that it will point to itself. There are
12338 * other cases where you have something like:
12339 *
12340 * .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
12341 *
12342 * and here no correction would be required. Internally in
12343 * the assembler we treat operands of this form as not being
12344 * pcrel since the '.' is explicitly mentioned, and I wonder
12345 * whether it would simplify matters to do it this way. Who
12346 * knows. In earlier versions of the PIC patches, the
12347 * pcrel_adjust field was used to store the correction, but
12348 * since the expression is not pcrel, I felt it would be
12349 * confusing to do it this way. */
12350
d6ab8113 12351 if ((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
12352 || reloc_type == BFD_RELOC_X86_64_32S
12353 || reloc_type == BFD_RELOC_64)
29b0f896
AM
12354 && GOT_symbol
12355 && GOT_symbol == i.op[n].imms->X_add_symbol
12356 && (i.op[n].imms->X_op == O_symbol
12357 || (i.op[n].imms->X_op == O_add
12358 && ((symbol_get_value_expression
12359 (i.op[n].imms->X_op_symbol)->X_op)
12360 == O_subtract))))
12361 {
4fa24527 12362 if (!object_64bit)
d6ab8113 12363 reloc_type = BFD_RELOC_386_GOTPC;
7b81dfbb 12364 else if (size == 4)
d6ab8113 12365 reloc_type = BFD_RELOC_X86_64_GOTPC32;
7b81dfbb
AJ
12366 else if (size == 8)
12367 reloc_type = BFD_RELOC_X86_64_GOTPC64;
5b7c81bd 12368 i.has_gotpc_tls_reloc = true;
d583596c
JB
12369 i.op[n].imms->X_add_number +=
12370 encoding_length (insn_start_frag, insn_start_off, p);
29b0f896 12371 }
29b0f896
AM
12372 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
12373 i.op[n].imms, 0, reloc_type);
12374 }
12375 }
12376 }
252b5132
RH
12377}
12378\f
d182319b
JB
12379/* x86_cons_fix_new is called via the expression parsing code when a
12380 reloc is needed. We use this hook to get the correct .got reloc. */
d182319b
JB
12381static int cons_sign = -1;
12382
12383void
e3bb37b5 12384x86_cons_fix_new (fragS *frag, unsigned int off, unsigned int len,
62ebcb5c 12385 expressionS *exp, bfd_reloc_code_real_type r)
d182319b 12386{
d258b828 12387 r = reloc (len, 0, cons_sign, r);
d182319b
JB
12388
12389#ifdef TE_PE
12390 if (exp->X_op == O_secrel)
12391 {
12392 exp->X_op = O_symbol;
12393 r = BFD_RELOC_32_SECREL;
12394 }
145667f8
MH
12395 else if (exp->X_op == O_secidx)
12396 r = BFD_RELOC_16_SECIDX;
d182319b
JB
12397#endif
12398
12399 fix_new_exp (frag, off, len, exp, 0, r);
12400}
12401
357d1bd8
L
12402/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
12403 purpose of the `.dc.a' internal pseudo-op. */
12404
12405int
12406x86_address_bytes (void)
12407{
12408 if ((stdoutput->arch_info->mach & bfd_mach_x64_32))
12409 return 4;
12410 return stdoutput->arch_info->bits_per_address / 8;
12411}
12412
deea4973
JB
12413#if (!(defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined (OBJ_MACH_O)) \
12414 || defined (LEX_AT)) && !defined (TE_PE)
d258b828 12415# define lex_got(reloc, adjust, types) NULL
718ddfc0 12416#else
f3c180ae
AM
12417/* Parse operands of the form
12418 <symbol>@GOTOFF+<nnn>
12419 and similar .plt or .got references.
12420
12421 If we find one, set up the correct relocation in RELOC and copy the
12422 input string, minus the `@GOTOFF' into a malloc'd buffer for
12423 parsing by the calling routine. Return this buffer, and if ADJUST
12424 is non-null set it to the length of the string we removed from the
12425 input line. Otherwise return NULL. */
12426static char *
91d6fa6a 12427lex_got (enum bfd_reloc_code_real *rel,
64e74474 12428 int *adjust,
d258b828 12429 i386_operand_type *types)
f3c180ae 12430{
7b81dfbb
AJ
12431 /* Some of the relocations depend on the size of what field is to
12432 be relocated. But in our callers i386_immediate and i386_displacement
12433 we don't yet know the operand size (this will be set by insn
12434 matching). Hence we record the word32 relocation here,
12435 and adjust the reloc according to the real size in reloc(). */
145667f8
MH
12436 static const struct
12437 {
f3c180ae 12438 const char *str;
cff8d58a 12439 int len;
4fa24527 12440 const enum bfd_reloc_code_real rel[2];
40fb9820 12441 const i386_operand_type types64;
5b7c81bd 12442 bool need_GOT_symbol;
145667f8
MH
12443 }
12444 gotrel[] =
12445 {
05909f23
JB
12446
12447#define OPERAND_TYPE_IMM32_32S_DISP32 { .bitfield = \
12448 { .imm32 = 1, .imm32s = 1, .disp32 = 1 } }
12449#define OPERAND_TYPE_IMM32_32S_64_DISP32 { .bitfield = \
12450 { .imm32 = 1, .imm32s = 1, .imm64 = 1, .disp32 = 1 } }
12451#define OPERAND_TYPE_IMM32_32S_64_DISP32_64 { .bitfield = \
12452 { .imm32 = 1, .imm32s = 1, .imm64 = 1, .disp32 = 1, .disp64 = 1 } }
12453#define OPERAND_TYPE_IMM64_DISP64 { .bitfield = \
12454 { .imm64 = 1, .disp64 = 1 } }
12455
deea4973 12456#ifndef TE_PE
8ce3d284 12457#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
12458 { STRING_COMMA_LEN ("SIZE"), { BFD_RELOC_SIZE32,
12459 BFD_RELOC_SIZE32 },
05909f23 12460 { .bitfield = { .imm32 = 1, .imm64 = 1 } }, false },
8ce3d284 12461#endif
cff8d58a
L
12462 { STRING_COMMA_LEN ("PLTOFF"), { _dummy_first_bfd_reloc_code_real,
12463 BFD_RELOC_X86_64_PLTOFF64 },
05909f23 12464 { .bitfield = { .imm64 = 1 } }, true },
cff8d58a
L
12465 { STRING_COMMA_LEN ("PLT"), { BFD_RELOC_386_PLT32,
12466 BFD_RELOC_X86_64_PLT32 },
a775efc8 12467 OPERAND_TYPE_IMM32_32S_DISP32, false },
cff8d58a
L
12468 { STRING_COMMA_LEN ("GOTPLT"), { _dummy_first_bfd_reloc_code_real,
12469 BFD_RELOC_X86_64_GOTPLT64 },
5b7c81bd 12470 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
12471 { STRING_COMMA_LEN ("GOTOFF"), { BFD_RELOC_386_GOTOFF,
12472 BFD_RELOC_X86_64_GOTOFF64 },
5b7c81bd 12473 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
12474 { STRING_COMMA_LEN ("GOTPCREL"), { _dummy_first_bfd_reloc_code_real,
12475 BFD_RELOC_X86_64_GOTPCREL },
a775efc8 12476 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
12477 { STRING_COMMA_LEN ("TLSGD"), { BFD_RELOC_386_TLS_GD,
12478 BFD_RELOC_X86_64_TLSGD },
a775efc8 12479 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
12480 { STRING_COMMA_LEN ("TLSLDM"), { BFD_RELOC_386_TLS_LDM,
12481 _dummy_first_bfd_reloc_code_real },
5b7c81bd 12482 OPERAND_TYPE_NONE, true },
cff8d58a
L
12483 { STRING_COMMA_LEN ("TLSLD"), { _dummy_first_bfd_reloc_code_real,
12484 BFD_RELOC_X86_64_TLSLD },
a775efc8 12485 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
12486 { STRING_COMMA_LEN ("GOTTPOFF"), { BFD_RELOC_386_TLS_IE_32,
12487 BFD_RELOC_X86_64_GOTTPOFF },
a775efc8 12488 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
12489 { STRING_COMMA_LEN ("TPOFF"), { BFD_RELOC_386_TLS_LE_32,
12490 BFD_RELOC_X86_64_TPOFF32 },
a775efc8 12491 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
12492 { STRING_COMMA_LEN ("NTPOFF"), { BFD_RELOC_386_TLS_LE,
12493 _dummy_first_bfd_reloc_code_real },
5b7c81bd 12494 OPERAND_TYPE_NONE, true },
cff8d58a
L
12495 { STRING_COMMA_LEN ("DTPOFF"), { BFD_RELOC_386_TLS_LDO_32,
12496 BFD_RELOC_X86_64_DTPOFF32 },
a775efc8 12497 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
12498 { STRING_COMMA_LEN ("GOTNTPOFF"),{ BFD_RELOC_386_TLS_GOTIE,
12499 _dummy_first_bfd_reloc_code_real },
5b7c81bd 12500 OPERAND_TYPE_NONE, true },
cff8d58a
L
12501 { STRING_COMMA_LEN ("INDNTPOFF"),{ BFD_RELOC_386_TLS_IE,
12502 _dummy_first_bfd_reloc_code_real },
5b7c81bd 12503 OPERAND_TYPE_NONE, true },
cff8d58a
L
12504 { STRING_COMMA_LEN ("GOT"), { BFD_RELOC_386_GOT32,
12505 BFD_RELOC_X86_64_GOT32 },
a775efc8 12506 OPERAND_TYPE_IMM32_32S_64_DISP32, true },
cff8d58a
L
12507 { STRING_COMMA_LEN ("TLSDESC"), { BFD_RELOC_386_TLS_GOTDESC,
12508 BFD_RELOC_X86_64_GOTPC32_TLSDESC },
a775efc8 12509 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
12510 { STRING_COMMA_LEN ("TLSCALL"), { BFD_RELOC_386_TLS_DESC_CALL,
12511 BFD_RELOC_X86_64_TLSDESC_CALL },
a775efc8 12512 OPERAND_TYPE_IMM32_32S_DISP32, true },
deea4973
JB
12513#else /* TE_PE */
12514 { STRING_COMMA_LEN ("SECREL32"), { BFD_RELOC_32_SECREL,
12515 BFD_RELOC_32_SECREL },
a775efc8 12516 OPERAND_TYPE_IMM32_32S_64_DISP32_64, false },
deea4973 12517#endif
05909f23
JB
12518
12519#undef OPERAND_TYPE_IMM32_32S_DISP32
12520#undef OPERAND_TYPE_IMM32_32S_64_DISP32
12521#undef OPERAND_TYPE_IMM32_32S_64_DISP32_64
12522#undef OPERAND_TYPE_IMM64_DISP64
12523
f3c180ae
AM
12524 };
12525 char *cp;
12526 unsigned int j;
12527
deea4973 12528#if defined (OBJ_MAYBE_ELF) && !defined (TE_PE)
718ddfc0
JB
12529 if (!IS_ELF)
12530 return NULL;
d382c579 12531#endif
718ddfc0 12532
f3c180ae 12533 for (cp = input_line_pointer; *cp != '@'; cp++)
67c11a9b 12534 if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
f3c180ae
AM
12535 return NULL;
12536
47465058 12537 for (j = 0; j < ARRAY_SIZE (gotrel); j++)
f3c180ae 12538 {
cff8d58a 12539 int len = gotrel[j].len;
28f81592 12540 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
f3c180ae 12541 {
4fa24527 12542 if (gotrel[j].rel[object_64bit] != 0)
f3c180ae 12543 {
28f81592
AM
12544 int first, second;
12545 char *tmpbuf, *past_reloc;
f3c180ae 12546
91d6fa6a 12547 *rel = gotrel[j].rel[object_64bit];
f3c180ae 12548
3956db08
JB
12549 if (types)
12550 {
12551 if (flag_code != CODE_64BIT)
40fb9820
L
12552 {
12553 types->bitfield.imm32 = 1;
12554 types->bitfield.disp32 = 1;
12555 }
3956db08
JB
12556 else
12557 *types = gotrel[j].types64;
12558 }
12559
844bf810 12560 if (gotrel[j].need_GOT_symbol && GOT_symbol == NULL)
f3c180ae
AM
12561 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
12562
28f81592 12563 /* The length of the first part of our input line. */
f3c180ae 12564 first = cp - input_line_pointer;
28f81592
AM
12565
12566 /* The second part goes from after the reloc token until
67c11a9b 12567 (and including) an end_of_line char or comma. */
28f81592 12568 past_reloc = cp + 1 + len;
67c11a9b
AM
12569 cp = past_reloc;
12570 while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
12571 ++cp;
12572 second = cp + 1 - past_reloc;
28f81592
AM
12573
12574 /* Allocate and copy string. The trailing NUL shouldn't
12575 be necessary, but be safe. */
add39d23 12576 tmpbuf = XNEWVEC (char, first + second + 2);
f3c180ae 12577 memcpy (tmpbuf, input_line_pointer, first);
0787a12d
AM
12578 if (second != 0 && *past_reloc != ' ')
12579 /* Replace the relocation token with ' ', so that
12580 errors like foo@GOTOFF1 will be detected. */
12581 tmpbuf[first++] = ' ';
af89796a
L
12582 else
12583 /* Increment length by 1 if the relocation token is
12584 removed. */
12585 len++;
12586 if (adjust)
12587 *adjust = len;
0787a12d
AM
12588 memcpy (tmpbuf + first, past_reloc, second);
12589 tmpbuf[first + second] = '\0';
f3c180ae
AM
12590 return tmpbuf;
12591 }
12592
4fa24527
JB
12593 as_bad (_("@%s reloc is not supported with %d-bit output format"),
12594 gotrel[j].str, 1 << (5 + object_64bit));
f3c180ae
AM
12595 return NULL;
12596 }
12597 }
12598
12599 /* Might be a symbol version string. Don't as_bad here. */
12600 return NULL;
12601}
4e4f7c87 12602#endif
f3c180ae 12603
62ebcb5c 12604bfd_reloc_code_real_type
e3bb37b5 12605x86_cons (expressionS *exp, int size)
f3c180ae 12606{
62ebcb5c
AM
12607 bfd_reloc_code_real_type got_reloc = NO_RELOC;
12608
6b50f5f4
JB
12609 intel_syntax = -intel_syntax;
12610 exp->X_md = 0;
5cc00775 12611 expr_mode = expr_operator_none;
6b50f5f4 12612
2748c1b1
L
12613#if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
12614 && !defined (LEX_AT)) \
12615 || defined (TE_PE)
4fa24527 12616 if (size == 4 || (object_64bit && size == 8))
f3c180ae
AM
12617 {
12618 /* Handle @GOTOFF and the like in an expression. */
12619 char *save;
12620 char *gotfree_input_line;
4a57f2cf 12621 int adjust = 0;
f3c180ae
AM
12622
12623 save = input_line_pointer;
d258b828 12624 gotfree_input_line = lex_got (&got_reloc, &adjust, NULL);
f3c180ae
AM
12625 if (gotfree_input_line)
12626 input_line_pointer = gotfree_input_line;
12627
12628 expression (exp);
12629
12630 if (gotfree_input_line)
12631 {
12632 /* expression () has merrily parsed up to the end of line,
12633 or a comma - in the wrong buffer. Transfer how far
12634 input_line_pointer has moved to the right buffer. */
12635 input_line_pointer = (save
12636 + (input_line_pointer - gotfree_input_line)
12637 + adjust);
12638 free (gotfree_input_line);
3992d3b7
AM
12639 if (exp->X_op == O_constant
12640 || exp->X_op == O_absent
12641 || exp->X_op == O_illegal
0398aac5 12642 || exp->X_op == O_register
3992d3b7
AM
12643 || exp->X_op == O_big)
12644 {
12645 char c = *input_line_pointer;
12646 *input_line_pointer = 0;
12647 as_bad (_("missing or invalid expression `%s'"), save);
12648 *input_line_pointer = c;
12649 }
b9519cfe
L
12650 else if ((got_reloc == BFD_RELOC_386_PLT32
12651 || got_reloc == BFD_RELOC_X86_64_PLT32)
12652 && exp->X_op != O_symbol)
12653 {
12654 char c = *input_line_pointer;
12655 *input_line_pointer = 0;
12656 as_bad (_("invalid PLT expression `%s'"), save);
12657 *input_line_pointer = c;
12658 }
f3c180ae
AM
12659 }
12660 }
12661 else
6b50f5f4 12662#endif
f3c180ae 12663 expression (exp);
ee86248c
JB
12664
12665 intel_syntax = -intel_syntax;
12666
12667 if (intel_syntax)
12668 i386_intel_simplify (exp);
62ebcb5c 12669
a442cac5 12670 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
5cc00775
JB
12671 if (size <= 4 && expr_mode == expr_operator_present
12672 && exp->X_op == O_constant && !object_64bit)
a442cac5
JB
12673 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
12674
62ebcb5c 12675 return got_reloc;
f3c180ae 12676}
f3c180ae 12677
9f32dd5b
L
12678static void
12679signed_cons (int size)
6482c264 12680{
a442cac5 12681 if (object_64bit)
d182319b
JB
12682 cons_sign = 1;
12683 cons (size);
12684 cons_sign = -1;
6482c264
NC
12685}
12686
edd67638
JB
12687static void
12688s_insn (int dummy ATTRIBUTE_UNUSED)
12689{
393fbe8d 12690 char mnemonic[MAX_MNEM_SIZE], *line = input_line_pointer, *ptr;
edd67638
JB
12691 char *saved_ilp = find_end_of_line (line, false), saved_char;
12692 const char *end;
12693 unsigned int j;
12694 valueT val;
12695 bool vex = false, xop = false, evex = false;
b5482fe5 12696 struct last_insn *last_insn;
edd67638
JB
12697
12698 init_globals ();
12699
12700 saved_char = *saved_ilp;
12701 *saved_ilp = 0;
12702
12703 end = parse_insn (line, mnemonic, true);
12704 if (end == NULL)
12705 {
12706 bad:
12707 *saved_ilp = saved_char;
12708 ignore_rest_of_line ();
d0c2e3ec 12709 i.tm.mnem_off = 0;
edd67638
JB
12710 return;
12711 }
12712 line += end - line;
12713
d3b01414
JB
12714 current_templates.start = &i.tm;
12715 current_templates.end = &i.tm + 1;
edd67638 12716 i.tm.mnem_off = MN__insn;
393fbe8d 12717 i.tm.extension_opcode = None;
edd67638
JB
12718
12719 if (startswith (line, "VEX")
12720 && (line[3] == '.' || is_space_char (line[3])))
12721 {
12722 vex = true;
12723 line += 3;
12724 }
12725 else if (startswith (line, "XOP") && ISDIGIT (line[3]))
12726 {
12727 char *e;
12728 unsigned long n = strtoul (line + 3, &e, 16);
12729
12730 if (e == line + 5 && n >= 0x08 && n <= 0x1f
12731 && (*e == '.' || is_space_char (*e)))
12732 {
12733 xop = true;
d0c2e3ec
JB
12734 /* Arrange for build_vex_prefix() to emit 0x8f. */
12735 i.tm.opcode_space = SPACE_XOP08;
12736 i.insn_opcode_space = n;
edd67638
JB
12737 line = e;
12738 }
12739 }
12740 else if (startswith (line, "EVEX")
12741 && (line[4] == '.' || is_space_char (line[4])))
12742 {
12743 evex = true;
12744 line += 4;
12745 }
12746
12747 if (vex || xop
e346d50a 12748 ? i.encoding == encoding_evex
edd67638 12749 : evex
e346d50a
JB
12750 ? i.encoding == encoding_vex
12751 || i.encoding == encoding_vex3
12752 : i.encoding != encoding_default)
edd67638
JB
12753 {
12754 as_bad (_("pseudo-prefix conflicts with encoding specifier"));
12755 goto bad;
12756 }
12757
e346d50a
JB
12758 if (line > end && i.encoding == encoding_default)
12759 i.encoding = evex ? encoding_evex : encoding_vex;
0ff3b7d0 12760
e346d50a 12761 if (i.encoding != encoding_default)
1adecddd
JB
12762 {
12763 /* Only address size and segment override prefixes are permitted with
12764 VEX/XOP/EVEX encodings. */
12765 const unsigned char *p = i.prefix;
12766
12767 for (j = 0; j < ARRAY_SIZE (i.prefix); ++j, ++p)
12768 {
12769 if (!*p)
12770 continue;
12771
12772 switch (j)
12773 {
12774 case SEG_PREFIX:
12775 case ADDR_PREFIX:
12776 break;
12777 default:
12778 as_bad (_("illegal prefix used with VEX/XOP/EVEX"));
12779 goto bad;
12780 }
12781 }
12782 }
12783
edd67638
JB
12784 if (line > end && *line == '.')
12785 {
d0c2e3ec
JB
12786 /* Length specifier (VEX.L, XOP.L, EVEX.L'L). */
12787 switch (line[1])
12788 {
12789 case 'L':
12790 switch (line[2])
12791 {
12792 case '0':
12793 if (evex)
12794 i.tm.opcode_modifier.evex = EVEX128;
12795 else
12796 i.tm.opcode_modifier.vex = VEX128;
12797 break;
12798
12799 case '1':
12800 if (evex)
12801 i.tm.opcode_modifier.evex = EVEX256;
12802 else
12803 i.tm.opcode_modifier.vex = VEX256;
12804 break;
12805
12806 case '2':
12807 if (evex)
12808 i.tm.opcode_modifier.evex = EVEX512;
12809 break;
12810
12811 case '3':
12812 if (evex)
12813 i.tm.opcode_modifier.evex = EVEX_L3;
12814 break;
12815
12816 case 'I':
12817 if (line[3] == 'G')
12818 {
12819 if (evex)
12820 i.tm.opcode_modifier.evex = EVEXLIG;
12821 else
12822 i.tm.opcode_modifier.vex = VEXScalar; /* LIG */
12823 ++line;
12824 }
12825 break;
12826 }
12827
12828 if (i.tm.opcode_modifier.vex || i.tm.opcode_modifier.evex)
12829 line += 3;
12830 break;
12831
12832 case '1':
12833 if (line[2] == '2' && line[3] == '8')
12834 {
12835 if (evex)
12836 i.tm.opcode_modifier.evex = EVEX128;
12837 else
12838 i.tm.opcode_modifier.vex = VEX128;
12839 line += 4;
12840 }
12841 break;
12842
12843 case '2':
12844 if (line[2] == '5' && line[3] == '6')
12845 {
12846 if (evex)
12847 i.tm.opcode_modifier.evex = EVEX256;
12848 else
12849 i.tm.opcode_modifier.vex = VEX256;
12850 line += 4;
12851 }
12852 break;
12853
12854 case '5':
12855 if (evex && line[2] == '1' && line[3] == '2')
12856 {
12857 i.tm.opcode_modifier.evex = EVEX512;
12858 line += 4;
12859 }
12860 break;
12861 }
12862 }
12863
12864 if (line > end && *line == '.')
12865 {
12866 /* embedded prefix (VEX.pp, XOP.pp, EVEX.pp). */
12867 switch (line[1])
12868 {
12869 case 'N':
12870 if (line[2] == 'P')
12871 line += 3;
12872 break;
12873
12874 case '6':
12875 if (line[2] == '6')
12876 {
12877 i.tm.opcode_modifier.opcodeprefix = PREFIX_0X66;
12878 line += 3;
12879 }
12880 break;
12881
12882 case 'F': case 'f':
12883 if (line[2] == '3')
12884 {
12885 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
12886 line += 3;
12887 }
12888 else if (line[2] == '2')
12889 {
12890 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF2;
12891 line += 3;
12892 }
12893 break;
12894 }
12895 }
12896
12897 if (line > end && !xop && *line == '.')
12898 {
12899 /* Encoding space (VEX.mmmmm, EVEX.mmmm). */
12900 switch (line[1])
12901 {
12902 case '0':
12903 if (TOUPPER (line[2]) != 'F')
12904 break;
12905 if (line[3] == '.' || is_space_char (line[3]))
12906 {
12907 i.insn_opcode_space = SPACE_0F;
12908 line += 3;
12909 }
12910 else if (line[3] == '3'
12911 && (line[4] == '8' || TOUPPER (line[4]) == 'A')
12912 && (line[5] == '.' || is_space_char (line[5])))
12913 {
12914 i.insn_opcode_space = line[4] == '8' ? SPACE_0F38 : SPACE_0F3A;
12915 line += 5;
12916 }
12917 break;
12918
12919 case 'M':
12920 if (ISDIGIT (line[2]) && line[2] != '0')
12921 {
12922 char *e;
12923 unsigned long n = strtoul (line + 2, &e, 10);
12924
12925 if (n <= (evex ? 15 : 31)
12926 && (*e == '.' || is_space_char (*e)))
12927 {
12928 i.insn_opcode_space = n;
12929 line = e;
12930 }
12931 }
12932 break;
12933 }
12934 }
12935
12936 if (line > end && *line == '.' && line[1] == 'W')
12937 {
12938 /* VEX.W, XOP.W, EVEX.W */
12939 switch (line[2])
12940 {
12941 case '0':
12942 i.tm.opcode_modifier.vexw = VEXW0;
12943 break;
12944
12945 case '1':
12946 i.tm.opcode_modifier.vexw = VEXW1;
12947 break;
12948
12949 case 'I':
12950 if (line[3] == 'G')
12951 {
12952 i.tm.opcode_modifier.vexw = VEXWIG;
12953 ++line;
12954 }
12955 break;
12956 }
12957
12958 if (i.tm.opcode_modifier.vexw)
12959 line += 3;
12960 }
12961
12962 if (line > end && *line && !is_space_char (*line))
12963 {
12964 /* Improve diagnostic a little. */
12965 if (*line == '.' && line[1] && !is_space_char (line[1]))
12966 ++line;
12967 goto done;
edd67638
JB
12968 }
12969
393fbe8d
JB
12970 /* Before processing the opcode expression, find trailing "+r" or
12971 "/<digit>" specifiers. */
12972 for (ptr = line; ; ++ptr)
12973 {
12974 unsigned long n;
12975 char *e;
12976
12977 ptr = strpbrk (ptr, "+/,");
12978 if (ptr == NULL || *ptr == ',')
12979 break;
12980
12981 if (*ptr == '+' && ptr[1] == 'r'
12982 && (ptr[2] == ',' || (is_space_char (ptr[2]) && ptr[3] == ',')))
12983 {
12984 *ptr = ' ';
12985 ptr[1] = ' ';
12986 i.short_form = true;
12987 break;
12988 }
12989
12990 if (*ptr == '/' && ISDIGIT (ptr[1])
12991 && (n = strtoul (ptr + 1, &e, 8)) < 8
12992 && e == ptr + 2
12993 && (ptr[2] == ',' || (is_space_char (ptr[2]) && ptr[3] == ',')))
12994 {
12995 *ptr = ' ';
12996 ptr[1] = ' ';
12997 i.tm.extension_opcode = n;
12998 i.tm.opcode_modifier.modrm = 1;
12999 break;
13000 }
13001 }
13002
edd67638
JB
13003 input_line_pointer = line;
13004 val = get_absolute_expression ();
13005 line = input_line_pointer;
13006
393fbe8d
JB
13007 if (i.short_form && (val & 7))
13008 as_warn ("`+r' assumes low three opcode bits to be clear");
13009
edd67638
JB
13010 for (j = 1; j < sizeof(val); ++j)
13011 if (!(val >> (j * 8)))
13012 break;
13013
13014 /* Trim off a prefix if present. */
13015 if (j > 1 && !vex && !xop && !evex)
13016 {
13017 uint8_t byte = val >> ((j - 1) * 8);
13018
13019 switch (byte)
13020 {
13021 case DATA_PREFIX_OPCODE:
13022 case REPE_PREFIX_OPCODE:
13023 case REPNE_PREFIX_OPCODE:
13024 if (!add_prefix (byte))
13025 goto bad;
13026 val &= ((uint64_t)1 << (--j * 8)) - 1;
13027 break;
13028 }
13029 }
13030
6804f42c
JB
13031 /* Parse operands, if any, before evaluating encoding space. */
13032 if (*line == ',')
13033 {
13034 i.memshift = -1;
13035
13036 ptr = parse_operands (line + 1, &i386_mnemonics[MN__insn]);
13037 this_operand = -1;
13038 if (!ptr)
13039 goto bad;
13040 line = ptr;
13041
13042 if (!i.operands)
13043 {
13044 as_bad (_("expecting operand after ','; got nothing"));
13045 goto done;
13046 }
13047
13048 if (i.mem_operands > 1)
13049 {
13050 as_bad (_("too many memory references for `%s'"),
13051 &i386_mnemonics[MN__insn]);
13052 goto done;
13053 }
13054
13055 /* No need to distinguish encoding_evex and encoding_evex512. */
13056 if (i.encoding == encoding_evex512)
13057 i.encoding = encoding_evex;
13058 }
13059
edd67638 13060 /* Trim off encoding space. */
d0c2e3ec 13061 if (j > 1 && !i.insn_opcode_space && (val >> ((j - 1) * 8)) == 0x0f)
edd67638
JB
13062 {
13063 uint8_t byte = val >> ((--j - 1) * 8);
13064
d0c2e3ec 13065 i.insn_opcode_space = SPACE_0F;
6804f42c
JB
13066 switch (byte & -(j > 1 && !i.rex2_encoding
13067 && (i.encoding != encoding_egpr || evex)))
edd67638
JB
13068 {
13069 case 0x38:
d0c2e3ec 13070 i.insn_opcode_space = SPACE_0F38;
edd67638
JB
13071 --j;
13072 break;
13073 case 0x3a:
d0c2e3ec 13074 i.insn_opcode_space = SPACE_0F3A;
edd67638
JB
13075 --j;
13076 break;
13077 }
d0c2e3ec 13078 i.tm.opcode_space = i.insn_opcode_space;
edd67638
JB
13079 val &= ((uint64_t)1 << (j * 8)) - 1;
13080 }
d0c2e3ec
JB
13081 if (!i.tm.opcode_space && (vex || evex))
13082 /* Arrange for build_vex_prefix() to properly emit 0xC4/0xC5.
13083 Also avoid hitting abort() there or in build_evex_prefix(). */
13084 i.tm.opcode_space = i.insn_opcode_space == SPACE_0F ? SPACE_0F
13085 : SPACE_0F38;
edd67638
JB
13086
13087 if (j > 2)
13088 {
13089 as_bad (_("opcode residual (%#"PRIx64") too wide"), (uint64_t) val);
6804f42c 13090 goto done;
edd67638
JB
13091 }
13092 i.opcode_length = j;
0ff3b7d0
JB
13093
13094 /* Handle operands, if any. */
6804f42c 13095 if (i.operands)
0ff3b7d0
JB
13096 {
13097 i386_operand_type combined;
f7377a91 13098 expressionS *disp_exp = NULL;
0ff3b7d0
JB
13099 bool changed;
13100
eb3f3841
JB
13101 if (i.encoding == encoding_egpr)
13102 {
13103 if (vex || xop)
13104 {
13105 as_bad (_("eGPR use conflicts with encoding specifier"));
13106 goto done;
13107 }
13108 if (evex)
13109 i.encoding = encoding_evex;
13110 else
13111 i.encoding = encoding_default;
13112 }
13113
0ff3b7d0
JB
13114 /* Are we to emit ModR/M encoding? */
13115 if (!i.short_form
13116 && (i.mem_operands
e346d50a 13117 || i.reg_operands > (i.encoding != encoding_default)
0ff3b7d0
JB
13118 || i.tm.extension_opcode != None))
13119 i.tm.opcode_modifier.modrm = 1;
13120
13121 if (!i.tm.opcode_modifier.modrm
13122 && (i.reg_operands
e346d50a 13123 > i.short_form + 0U + (i.encoding != encoding_default)
0ff3b7d0
JB
13124 || i.mem_operands))
13125 {
13126 as_bad (_("too many register/memory operands"));
13127 goto done;
13128 }
13129
13130 /* Enforce certain constraints on operands. */
13131 switch (i.reg_operands + i.mem_operands
13132 + (i.tm.extension_opcode != None))
13133 {
13134 case 0:
13135 if (i.short_form)
13136 {
13137 as_bad (_("too few register/memory operands"));
13138 goto done;
13139 }
13140 /* Fall through. */
13141 case 1:
13142 if (i.tm.opcode_modifier.modrm)
13143 {
13144 as_bad (_("too few register/memory operands"));
13145 goto done;
13146 }
13147 break;
13148
13149 case 2:
13150 break;
13151
13152 case 4:
13153 if (i.imm_operands
13154 && (i.op[0].imms->X_op != O_constant
13155 || !fits_in_imm4 (i.op[0].imms->X_add_number)))
13156 {
13157 as_bad (_("constant doesn't fit in %d bits"), evex ? 3 : 4);
13158 goto done;
13159 }
13160 /* Fall through. */
13161 case 3:
e346d50a 13162 if (i.encoding != encoding_default)
0ff3b7d0 13163 {
f2a3a881 13164 i.tm.opcode_modifier.vexvvvv = VexVVVV_SRC1;
0ff3b7d0
JB
13165 break;
13166 }
13167 /* Fall through. */
13168 default:
13169 as_bad (_("too many register/memory operands"));
13170 goto done;
13171 }
13172
13173 /* Bring operands into canonical order (imm, mem, reg). */
13174 do
13175 {
13176 changed = false;
13177
13178 for (j = 1; j < i.operands; ++j)
13179 {
13180 if ((!operand_type_check (i.types[j - 1], imm)
13181 && operand_type_check (i.types[j], imm))
13182 || (i.types[j - 1].bitfield.class != ClassNone
13183 && i.types[j].bitfield.class == ClassNone))
13184 {
13185 swap_2_operands (j - 1, j);
13186 changed = true;
13187 }
13188 }
13189 }
13190 while (changed);
13191
13192 /* For Intel syntax swap the order of register operands. */
13193 if (intel_syntax)
13194 switch (i.reg_operands)
13195 {
13196 case 0:
13197 case 1:
13198 break;
13199
13200 case 4:
13201 swap_2_operands (i.imm_operands + i.mem_operands + 1, i.operands - 2);
13202 /* Fall through. */
13203 case 3:
13204 case 2:
13205 swap_2_operands (i.imm_operands + i.mem_operands, i.operands - 1);
13206 break;
13207
13208 default:
13209 abort ();
13210 }
13211
13212 /* Enforce constraints when using VSIB. */
13213 if (i.index_reg
13214 && (i.index_reg->reg_type.bitfield.xmmword
13215 || i.index_reg->reg_type.bitfield.ymmword
13216 || i.index_reg->reg_type.bitfield.zmmword))
13217 {
e346d50a 13218 if (i.encoding == encoding_default)
0ff3b7d0
JB
13219 {
13220 as_bad (_("VSIB unavailable with legacy encoding"));
13221 goto done;
13222 }
13223
e346d50a 13224 if (i.encoding == encoding_evex
0ff3b7d0
JB
13225 && i.reg_operands > 1)
13226 {
13227 /* We could allow two register operands, encoding the 2nd one in
13228 an 8-bit immediate like for 4-register-operand insns, but that
13229 would require ugly fiddling with process_operands() and/or
13230 build_modrm_byte(). */
13231 as_bad (_("too many register operands with VSIB"));
13232 goto done;
13233 }
13234
13235 i.tm.opcode_modifier.sib = 1;
13236 }
13237
13238 /* Establish operand size encoding. */
13239 operand_type_set (&combined, 0);
f7377a91 13240
0ff3b7d0
JB
13241 for (j = i.imm_operands; j < i.operands; ++j)
13242 {
3e4a511b 13243 /* Look for 8-bit operands that use old registers. */
e346d50a 13244 if (i.encoding != encoding_default
3e4a511b
JB
13245 && flag_code == CODE_64BIT
13246 && i.types[j].bitfield.class == Reg
13247 && i.types[j].bitfield.byte
13248 && !(i.op[j].regs->reg_flags & RegRex64)
13249 && i.op[j].regs->reg_num > 3)
13250 as_bad (_("can't encode register '%s%s' with VEX/XOP/EVEX"),
13251 register_prefix, i.op[j].regs->reg_name);
13252
0ff3b7d0
JB
13253 i.types[j].bitfield.instance = InstanceNone;
13254
13255 if (operand_type_check (i.types[j], disp))
f7377a91
JB
13256 {
13257 i.types[j].bitfield.baseindex = 1;
13258 disp_exp = i.op[j].disps;
13259 }
13260
13261 if (evex && i.types[j].bitfield.baseindex)
13262 {
13263 unsigned int n = i.memshift;
13264
13265 if (i.types[j].bitfield.byte)
13266 n = 0;
13267 else if (i.types[j].bitfield.word)
13268 n = 1;
13269 else if (i.types[j].bitfield.dword)
13270 n = 2;
13271 else if (i.types[j].bitfield.qword)
13272 n = 3;
13273 else if (i.types[j].bitfield.xmmword)
13274 n = 4;
13275 else if (i.types[j].bitfield.ymmword)
13276 n = 5;
13277 else if (i.types[j].bitfield.zmmword)
13278 n = 6;
13279
13280 if (i.memshift < 32 && n != i.memshift)
13281 as_warn ("conflicting memory operand size specifiers");
13282 i.memshift = n;
13283 }
0ff3b7d0
JB
13284
13285 if ((i.broadcast.type || i.broadcast.bytes)
13286 && j == i.broadcast.operand)
13287 continue;
13288
13289 combined = operand_type_or (combined, i.types[j]);
13290 combined.bitfield.class = ClassNone;
13291 }
13292
f7377a91
JB
13293 switch ((i.broadcast.type ? i.broadcast.type : 1)
13294 << (i.memshift < 32 ? i.memshift : 0))
13295 {
13296 case 64: combined.bitfield.zmmword = 1; break;
13297 case 32: combined.bitfield.ymmword = 1; break;
13298 case 16: combined.bitfield.xmmword = 1; break;
13299 case 8: combined.bitfield.qword = 1; break;
13300 case 4: combined.bitfield.dword = 1; break;
13301 }
13302
e346d50a 13303 if (i.encoding == encoding_default)
0ff3b7d0
JB
13304 {
13305 if (flag_code == CODE_64BIT && combined.bitfield.qword)
13306 i.rex |= REX_W;
13307 else if ((flag_code == CODE_16BIT ? combined.bitfield.dword
13308 : combined.bitfield.word)
13309 && !add_prefix (DATA_PREFIX_OPCODE))
13310 goto done;
13311 }
13312 else if (!i.tm.opcode_modifier.vexw)
13313 {
13314 if (flag_code == CODE_64BIT)
13315 {
13316 if (combined.bitfield.qword)
13317 i.tm.opcode_modifier.vexw = VEXW1;
13318 else if (combined.bitfield.dword)
13319 i.tm.opcode_modifier.vexw = VEXW0;
13320 }
13321
13322 if (!i.tm.opcode_modifier.vexw)
13323 i.tm.opcode_modifier.vexw = VEXWIG;
13324 }
13325
13326 if (vex || xop)
13327 {
13328 if (!i.tm.opcode_modifier.vex)
13329 {
13330 if (combined.bitfield.ymmword)
13331 i.tm.opcode_modifier.vex = VEX256;
13332 else if (combined.bitfield.xmmword)
13333 i.tm.opcode_modifier.vex = VEX128;
13334 }
13335 }
13336 else if (evex)
13337 {
13338 if (!i.tm.opcode_modifier.evex)
13339 {
13340 /* Do _not_ consider AVX512VL here. */
13341 if (i.rounding.type != rc_none || combined.bitfield.zmmword)
13342 i.tm.opcode_modifier.evex = EVEX512;
13343 else if (combined.bitfield.ymmword)
13344 i.tm.opcode_modifier.evex = EVEX256;
13345 else if (combined.bitfield.xmmword)
13346 i.tm.opcode_modifier.evex = EVEX128;
13347 }
f7377a91
JB
13348
13349 if (i.memshift >= 32)
13350 {
13351 unsigned int n = 0;
13352
13353 switch (i.tm.opcode_modifier.evex)
13354 {
13355 case EVEX512: n = 64; break;
13356 case EVEX256: n = 32; break;
13357 case EVEX128: n = 16; break;
13358 }
13359
13360 if (i.broadcast.type)
13361 n /= i.broadcast.type;
13362
13363 if (n > 0)
13364 for (i.memshift = 0; !(n & 1); n >>= 1)
13365 ++i.memshift;
13366 else if (disp_exp != NULL && disp_exp->X_op == O_constant
13367 && disp_exp->X_add_number != 0
13368 && i.disp_encoding != disp_encoding_32bit)
13369 {
13370 if (!quiet_warnings)
13371 as_warn ("cannot determine memory operand size");
13372 i.disp_encoding = disp_encoding_32bit;
13373 }
13374 }
0ff3b7d0
JB
13375 }
13376
f7377a91
JB
13377 if (i.memshift >= 32)
13378 i.memshift = 0;
13379 else if (!evex)
e346d50a 13380 i.encoding = encoding_error;
f7377a91 13381
0ff3b7d0
JB
13382 if (i.disp_operands && !optimize_disp (&i.tm))
13383 goto done;
13384
c032bc4f
JB
13385 /* Establish size for immediate operands. */
13386 for (j = 0; j < i.imm_operands; ++j)
13387 {
13388 expressionS *expP = i.op[j].imms;
13389
13390 gas_assert (operand_type_check (i.types[j], imm));
13391 operand_type_set (&i.types[j], 0);
13392
13393 if (i.imm_bits[j] > 32)
13394 i.types[j].bitfield.imm64 = 1;
13395 else if (i.imm_bits[j] > 16)
13396 {
13397 if (flag_code == CODE_64BIT && (i.flags[j] & Operand_Signed))
13398 i.types[j].bitfield.imm32s = 1;
13399 else
13400 i.types[j].bitfield.imm32 = 1;
13401 }
13402 else if (i.imm_bits[j] > 8)
13403 i.types[j].bitfield.imm16 = 1;
13404 else if (i.imm_bits[j] > 0)
13405 {
13406 if (i.flags[j] & Operand_Signed)
13407 i.types[j].bitfield.imm8s = 1;
13408 else
13409 i.types[j].bitfield.imm8 = 1;
13410 }
13411 else if (expP->X_op == O_constant)
13412 {
13413 i.types[j] = smallest_imm_type (expP->X_add_number);
13414 i.types[j].bitfield.imm1 = 0;
13415 /* Oddly enough imm_size() checks imm64 first, so the bit needs
13416 zapping since smallest_imm_type() sets it unconditionally. */
13417 if (flag_code != CODE_64BIT)
13418 {
13419 i.types[j].bitfield.imm64 = 0;
13420 i.types[j].bitfield.imm32s = 0;
13421 i.types[j].bitfield.imm32 = 1;
13422 }
13423 else if (i.types[j].bitfield.imm32 || i.types[j].bitfield.imm32s)
13424 i.types[j].bitfield.imm64 = 0;
13425 }
13426 else
13427 /* Non-constant expressions are sized heuristically. */
13428 switch (flag_code)
13429 {
13430 case CODE_64BIT: i.types[j].bitfield.imm32s = 1; break;
13431 case CODE_32BIT: i.types[j].bitfield.imm32 = 1; break;
13432 case CODE_16BIT: i.types[j].bitfield.imm16 = 1; break;
13433 }
13434 }
13435
0ff3b7d0
JB
13436 for (j = 0; j < i.operands; ++j)
13437 i.tm.operand_types[j] = i.types[j];
13438
13439 process_operands ();
13440 }
13441
13442 /* Don't set opcode until after processing operands, to avoid any
13443 potential special casing there. */
13444 i.tm.base_opcode |= val;
13445
e346d50a
JB
13446 if (i.encoding == encoding_error
13447 || (i.encoding != encoding_evex
0ff3b7d0
JB
13448 ? i.broadcast.type || i.broadcast.bytes
13449 || i.rounding.type != rc_none
13450 || i.mask.reg
f586e340
JB
13451 : (i.mem_operands && i.rounding.type != rc_none)
13452 || ((i.broadcast.type || i.broadcast.bytes)
13453 && !(i.flags[i.broadcast.operand] & Operand_Mem))))
0ff3b7d0
JB
13454 {
13455 as_bad (_("conflicting .insn operands"));
13456 goto done;
13457 }
edd67638 13458
d0c2e3ec
JB
13459 if (vex || xop)
13460 {
13461 if (!i.tm.opcode_modifier.vex)
13462 i.tm.opcode_modifier.vex = VEXScalar; /* LIG */
13463
13464 build_vex_prefix (NULL);
13465 i.rex &= REX_OPCODE;
13466 }
13467 else if (evex)
13468 {
13469 if (!i.tm.opcode_modifier.evex)
13470 i.tm.opcode_modifier.evex = EVEXLIG;
13471
13472 build_evex_prefix ();
13473 i.rex &= REX_OPCODE;
13474 }
ce705688
JB
13475 else
13476 establish_rex ();
d0c2e3ec 13477
b5482fe5
JB
13478 last_insn = &seg_info(now_seg)->tc_segment_info_data.last_insn;
13479 output_insn (last_insn);
13480 last_insn->kind = last_insn_directive;
13481 last_insn->name = ".insn directive";
13482 last_insn->file = as_where (&last_insn->line);
edd67638 13483
c7defc53
IB
13484#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
13485 /* PS: SCFI is enabled only for System V AMD64 ABI. The ABI check has been
13486 performed in i386_target_format. */
13487 if (IS_ELF && flag_synth_cfi)
13488 as_bad (_("SCFI: hand-crafting instructions not supported"));
13489#endif
13490
d0c2e3ec 13491 done:
edd67638
JB
13492 *saved_ilp = saved_char;
13493 input_line_pointer = line;
13494
13495 demand_empty_rest_of_line ();
d0c2e3ec
JB
13496
13497 /* Make sure dot_insn() won't yield "true" anymore. */
13498 i.tm.mnem_off = 0;
edd67638
JB
13499}
13500
d182319b 13501#ifdef TE_PE
6482c264 13502static void
7016a5d5 13503pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
6482c264
NC
13504{
13505 expressionS exp;
13506
13507 do
13508 {
13509 expression (&exp);
13510 if (exp.X_op == O_symbol)
13511 exp.X_op = O_secrel;
13512
13513 emit_expr (&exp, 4);
13514 }
13515 while (*input_line_pointer++ == ',');
13516
13517 input_line_pointer--;
13518 demand_empty_rest_of_line ();
13519}
145667f8
MH
13520
13521static void
13522pe_directive_secidx (int dummy ATTRIBUTE_UNUSED)
13523{
13524 expressionS exp;
13525
13526 do
13527 {
13528 expression (&exp);
13529 if (exp.X_op == O_symbol)
13530 exp.X_op = O_secidx;
13531
13532 emit_expr (&exp, 2);
13533 }
13534 while (*input_line_pointer++ == ',');
13535
13536 input_line_pointer--;
13537 demand_empty_rest_of_line ();
13538}
6482c264
NC
13539#endif
13540
7063667e
JB
13541/* Handle Rounding Control / SAE specifiers. */
13542
13543static char *
13544RC_SAE_specifier (const char *pstr)
13545{
13546 unsigned int j;
13547
13548 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); j++)
13549 {
13550 if (!strncmp (pstr, RC_NamesTable[j].name, RC_NamesTable[j].len))
13551 {
13552 if (i.rounding.type != rc_none)
13553 {
13554 as_bad (_("duplicated `{%s}'"), RC_NamesTable[j].name);
13555 return NULL;
13556 }
13557
eb3f3841
JB
13558 switch (i.encoding)
13559 {
13560 case encoding_default:
13561 case encoding_egpr:
13562 i.encoding = encoding_evex512;
13563 break;
13564 case encoding_evex:
13565 case encoding_evex512:
13566 break;
13567 default:
13568 return NULL;
13569 }
a6f3add0 13570
7063667e
JB
13571 i.rounding.type = RC_NamesTable[j].type;
13572
13573 return (char *)(pstr + RC_NamesTable[j].len);
13574 }
13575 }
13576
13577 return NULL;
13578}
13579
43234a1e
L
13580/* Handle Vector operations. */
13581
13582static char *
f70c6814 13583check_VecOperations (char *op_string)
43234a1e
L
13584{
13585 const reg_entry *mask;
13586 const char *saved;
13587 char *end_op;
13588
f70c6814 13589 while (*op_string)
43234a1e
L
13590 {
13591 saved = op_string;
13592 if (*op_string == '{')
13593 {
13594 op_string++;
13595
13596 /* Check broadcasts. */
d34049e8 13597 if (startswith (op_string, "1to"))
43234a1e 13598 {
5273a3cd 13599 unsigned int bcst_type;
43234a1e 13600
5273a3cd 13601 if (i.broadcast.type)
43234a1e
L
13602 goto duplicated_vec_op;
13603
13604 op_string += 3;
13605 if (*op_string == '8')
8e6e0792 13606 bcst_type = 8;
b28d1bda 13607 else if (*op_string == '4')
8e6e0792 13608 bcst_type = 4;
b28d1bda 13609 else if (*op_string == '2')
8e6e0792 13610 bcst_type = 2;
43234a1e
L
13611 else if (*op_string == '1'
13612 && *(op_string+1) == '6')
13613 {
8e6e0792 13614 bcst_type = 16;
43234a1e
L
13615 op_string++;
13616 }
0cc78721
CL
13617 else if (*op_string == '3'
13618 && *(op_string+1) == '2')
13619 {
13620 bcst_type = 32;
13621 op_string++;
13622 }
43234a1e
L
13623 else
13624 {
13625 as_bad (_("Unsupported broadcast: `%s'"), saved);
13626 return NULL;
13627 }
13628 op_string++;
13629
eb3f3841
JB
13630 switch (i.encoding)
13631 {
13632 case encoding_default:
13633 case encoding_egpr:
13634 i.encoding = encoding_evex;
13635 break;
13636 case encoding_evex:
13637 case encoding_evex512:
13638 break;
13639 default:
13640 goto unknown_vec_op;
13641 }
a6f3add0 13642
5273a3cd
JB
13643 i.broadcast.type = bcst_type;
13644 i.broadcast.operand = this_operand;
f7377a91
JB
13645
13646 /* For .insn a data size specifier may be appended. */
13647 if (dot_insn () && *op_string == ':')
13648 goto dot_insn_modifier;
13649 }
13650 /* Check .insn special cases. */
13651 else if (dot_insn () && *op_string == ':')
13652 {
13653 dot_insn_modifier:
c032bc4f 13654 switch (op_string[1])
f7377a91
JB
13655 {
13656 unsigned long n;
13657
c032bc4f 13658 case 'd':
f7377a91
JB
13659 if (i.memshift < 32)
13660 goto duplicated_vec_op;
13661
13662 n = strtoul (op_string + 2, &end_op, 0);
13663 if (n)
13664 for (i.memshift = 0; !(n & 1); n >>= 1)
13665 ++i.memshift;
13666 if (i.memshift < 32 && n == 1)
13667 op_string = end_op;
c032bc4f
JB
13668 break;
13669
13670 case 's': case 'u':
13671 /* This isn't really a "vector" operation, but a sign/size
13672 specifier for immediate operands of .insn. Note that AT&T
13673 syntax handles the same in i386_immediate(). */
13674 if (!intel_syntax)
13675 break;
13676
13677 if (i.imm_bits[this_operand])
13678 goto duplicated_vec_op;
13679
13680 n = strtoul (op_string + 2, &end_op, 0);
13681 if (n && n <= (flag_code == CODE_64BIT ? 64 : 32))
13682 {
13683 i.imm_bits[this_operand] = n;
13684 if (op_string[1] == 's')
13685 i.flags[this_operand] |= Operand_Signed;
13686 op_string = end_op;
13687 }
13688 break;
f7377a91 13689 }
43234a1e
L
13690 }
13691 /* Check masking operation. */
13692 else if ((mask = parse_register (op_string, &end_op)) != NULL)
13693 {
8a6fb3f9
JB
13694 if (mask == &bad_reg)
13695 return NULL;
13696
43234a1e 13697 /* k0 can't be used for write mask. */
f74a6307 13698 if (mask->reg_type.bitfield.class != RegMask || !mask->reg_num)
43234a1e 13699 {
6d2cd6b2
JB
13700 as_bad (_("`%s%s' can't be used for write mask"),
13701 register_prefix, mask->reg_name);
43234a1e
L
13702 return NULL;
13703 }
13704
6225c532 13705 if (!i.mask.reg)
43234a1e 13706 {
6225c532
JB
13707 i.mask.reg = mask;
13708 i.mask.operand = this_operand;
43234a1e 13709 }
6225c532
JB
13710 else if (i.mask.reg->reg_num)
13711 goto duplicated_vec_op;
43234a1e
L
13712 else
13713 {
6225c532 13714 i.mask.reg = mask;
43234a1e
L
13715
13716 /* Only "{z}" is allowed here. No need to check
13717 zeroing mask explicitly. */
6225c532 13718 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
13719 {
13720 as_bad (_("invalid write mask `%s'"), saved);
13721 return NULL;
13722 }
13723 }
13724
13725 op_string = end_op;
13726 }
13727 /* Check zeroing-flag for masking operation. */
13728 else if (*op_string == 'z')
13729 {
6225c532 13730 if (!i.mask.reg)
43234a1e 13731 {
6225c532
JB
13732 i.mask.reg = reg_k0;
13733 i.mask.zeroing = 1;
13734 i.mask.operand = this_operand;
43234a1e
L
13735 }
13736 else
13737 {
6225c532 13738 if (i.mask.zeroing)
43234a1e
L
13739 {
13740 duplicated_vec_op:
13741 as_bad (_("duplicated `%s'"), saved);
13742 return NULL;
13743 }
13744
6225c532 13745 i.mask.zeroing = 1;
43234a1e
L
13746
13747 /* Only "{%k}" is allowed here. No need to check mask
13748 register explicitly. */
6225c532 13749 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
13750 {
13751 as_bad (_("invalid zeroing-masking `%s'"),
13752 saved);
13753 return NULL;
13754 }
13755 }
13756
13757 op_string++;
13758 }
7063667e
JB
13759 else if (intel_syntax
13760 && (op_string = RC_SAE_specifier (op_string)) != NULL)
13761 i.rounding.modifier = true;
43234a1e
L
13762 else
13763 goto unknown_vec_op;
13764
13765 if (*op_string != '}')
13766 {
13767 as_bad (_("missing `}' in `%s'"), saved);
13768 return NULL;
13769 }
13770 op_string++;
0ba3a731
L
13771
13772 /* Strip whitespace since the addition of pseudo prefixes
13773 changed how the scrubber treats '{'. */
13774 if (is_space_char (*op_string))
13775 ++op_string;
13776
43234a1e
L
13777 continue;
13778 }
13779 unknown_vec_op:
13780 /* We don't know this one. */
13781 as_bad (_("unknown vector operation: `%s'"), saved);
13782 return NULL;
13783 }
13784
6225c532 13785 if (i.mask.reg && i.mask.zeroing && !i.mask.reg->reg_num)
6d2cd6b2
JB
13786 {
13787 as_bad (_("zeroing-masking only allowed with write mask"));
13788 return NULL;
13789 }
13790
43234a1e
L
13791 return op_string;
13792}
13793
252b5132 13794static int
70e41ade 13795i386_immediate (char *imm_start)
252b5132
RH
13796{
13797 char *save_input_line_pointer;
f3c180ae 13798 char *gotfree_input_line;
252b5132 13799 segT exp_seg = 0;
47926f60 13800 expressionS *exp;
40fb9820
L
13801 i386_operand_type types;
13802
0dfbf9d7 13803 operand_type_set (&types, ~0);
252b5132
RH
13804
13805 if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
13806 {
31b2323c
L
13807 as_bad (_("at most %d immediate operands are allowed"),
13808 MAX_IMMEDIATE_OPERANDS);
252b5132
RH
13809 return 0;
13810 }
13811
13812 exp = &im_expressions[i.imm_operands++];
520dc8e8 13813 i.op[this_operand].imms = exp;
252b5132
RH
13814
13815 if (is_space_char (*imm_start))
13816 ++imm_start;
13817
13818 save_input_line_pointer = input_line_pointer;
13819 input_line_pointer = imm_start;
13820
d258b828 13821 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
13822 if (gotfree_input_line)
13823 input_line_pointer = gotfree_input_line;
252b5132 13824
5cc00775 13825 expr_mode = expr_operator_none;
252b5132
RH
13826 exp_seg = expression (exp);
13827
c032bc4f
JB
13828 /* For .insn immediates there may be a size specifier. */
13829 if (dot_insn () && *input_line_pointer == '{' && input_line_pointer[1] == ':'
13830 && (input_line_pointer[2] == 's' || input_line_pointer[2] == 'u'))
13831 {
13832 char *e;
13833 unsigned long n = strtoul (input_line_pointer + 3, &e, 0);
13834
13835 if (*e == '}' && n && n <= (flag_code == CODE_64BIT ? 64 : 32))
13836 {
13837 i.imm_bits[this_operand] = n;
13838 if (input_line_pointer[2] == 's')
13839 i.flags[this_operand] |= Operand_Signed;
13840 input_line_pointer = e + 1;
13841 }
13842 }
13843
83183c0c 13844 SKIP_WHITESPACE ();
252b5132 13845 if (*input_line_pointer)
f3c180ae 13846 as_bad (_("junk `%s' after expression"), input_line_pointer);
252b5132
RH
13847
13848 input_line_pointer = save_input_line_pointer;
f3c180ae 13849 if (gotfree_input_line)
ee86248c
JB
13850 {
13851 free (gotfree_input_line);
13852
9aac24b1 13853 if (exp->X_op == O_constant)
ee86248c
JB
13854 exp->X_op = O_illegal;
13855 }
13856
9aac24b1
JB
13857 if (exp_seg == reg_section)
13858 {
13859 as_bad (_("illegal immediate register operand %s"), imm_start);
13860 return 0;
13861 }
13862
ee86248c
JB
13863 return i386_finalize_immediate (exp_seg, exp, types, imm_start);
13864}
252b5132 13865
ee86248c
JB
13866static int
13867i386_finalize_immediate (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
13868 i386_operand_type types, const char *imm_start)
13869{
13870 if (exp->X_op == O_absent || exp->X_op == O_illegal || exp->X_op == O_big)
252b5132 13871 {
313c53d1
L
13872 if (imm_start)
13873 as_bad (_("missing or invalid immediate expression `%s'"),
13874 imm_start);
3992d3b7 13875 return 0;
252b5132 13876 }
3e73aa7c 13877 else if (exp->X_op == O_constant)
252b5132 13878 {
47926f60 13879 /* Size it properly later. */
40fb9820 13880 i.types[this_operand].bitfield.imm64 = 1;
a442cac5
JB
13881
13882 /* If not 64bit, sign/zero extend val, to account for wraparound
13883 when !BFD64. */
5cc00775
JB
13884 if (expr_mode == expr_operator_present
13885 && flag_code != CODE_64BIT && !object_64bit)
a442cac5 13886 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
252b5132 13887 }
4c63da97 13888#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
f86103b7 13889 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
31312f95 13890 && exp_seg != absolute_section
47926f60 13891 && exp_seg != text_section
24eab124
AM
13892 && exp_seg != data_section
13893 && exp_seg != bss_section
13894 && exp_seg != undefined_section
f86103b7 13895 && !bfd_is_com_section (exp_seg))
252b5132 13896 {
d0b47220 13897 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
252b5132
RH
13898 return 0;
13899 }
13900#endif
13901 else
13902 {
13903 /* This is an address. The size of the address will be
24eab124 13904 determined later, depending on destination register,
3e73aa7c 13905 suffix, or the default for the section. */
40fb9820
L
13906 i.types[this_operand].bitfield.imm8 = 1;
13907 i.types[this_operand].bitfield.imm16 = 1;
13908 i.types[this_operand].bitfield.imm32 = 1;
13909 i.types[this_operand].bitfield.imm32s = 1;
13910 i.types[this_operand].bitfield.imm64 = 1;
c6fb90c8
L
13911 i.types[this_operand] = operand_type_and (i.types[this_operand],
13912 types);
252b5132
RH
13913 }
13914
13915 return 1;
13916}
13917
551c1ca1 13918static char *
e3bb37b5 13919i386_scale (char *scale)
252b5132 13920{
551c1ca1
AM
13921 offsetT val;
13922 char *save = input_line_pointer;
252b5132 13923
551c1ca1
AM
13924 input_line_pointer = scale;
13925 val = get_absolute_expression ();
13926
13927 switch (val)
252b5132 13928 {
551c1ca1 13929 case 1:
252b5132
RH
13930 i.log2_scale_factor = 0;
13931 break;
551c1ca1 13932 case 2:
252b5132
RH
13933 i.log2_scale_factor = 1;
13934 break;
551c1ca1 13935 case 4:
252b5132
RH
13936 i.log2_scale_factor = 2;
13937 break;
551c1ca1 13938 case 8:
252b5132
RH
13939 i.log2_scale_factor = 3;
13940 break;
13941 default:
a724f0f4
JB
13942 {
13943 char sep = *input_line_pointer;
13944
13945 *input_line_pointer = '\0';
13946 as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
13947 scale);
13948 *input_line_pointer = sep;
13949 input_line_pointer = save;
13950 return NULL;
13951 }
252b5132 13952 }
29b0f896 13953 if (i.log2_scale_factor != 0 && i.index_reg == 0)
252b5132
RH
13954 {
13955 as_warn (_("scale factor of %d without an index register"),
24eab124 13956 1 << i.log2_scale_factor);
252b5132 13957 i.log2_scale_factor = 0;
252b5132 13958 }
551c1ca1
AM
13959 scale = input_line_pointer;
13960 input_line_pointer = save;
13961 return scale;
252b5132
RH
13962}
13963
252b5132 13964static int
e3bb37b5 13965i386_displacement (char *disp_start, char *disp_end)
252b5132 13966{
29b0f896 13967 expressionS *exp;
252b5132
RH
13968 segT exp_seg = 0;
13969 char *save_input_line_pointer;
f3c180ae 13970 char *gotfree_input_line;
40fb9820
L
13971 int override;
13972 i386_operand_type bigdisp, types = anydisp;
3992d3b7 13973 int ret;
252b5132 13974
31b2323c
L
13975 if (i.disp_operands == MAX_MEMORY_OPERANDS)
13976 {
13977 as_bad (_("at most %d displacement operands are allowed"),
13978 MAX_MEMORY_OPERANDS);
13979 return 0;
13980 }
13981
0dfbf9d7 13982 operand_type_set (&bigdisp, 0);
6f2f06be 13983 if (i.jumpabsolute
48bcea9f 13984 || i.types[this_operand].bitfield.baseindex
d3b01414
JB
13985 || (current_templates.start->opcode_modifier.jump != JUMP
13986 && current_templates.start->opcode_modifier.jump != JUMP_DWORD))
e05278af 13987 {
48bcea9f 13988 i386_addressing_mode ();
e05278af 13989 override = (i.prefix[ADDR_PREFIX] != 0);
40fb9820
L
13990 if (flag_code == CODE_64BIT)
13991 {
a775efc8 13992 bigdisp.bitfield.disp32 = 1;
40fb9820 13993 if (!override)
a775efc8 13994 bigdisp.bitfield.disp64 = 1;
40fb9820
L
13995 }
13996 else if ((flag_code == CODE_16BIT) ^ override)
40fb9820 13997 bigdisp.bitfield.disp16 = 1;
48bcea9f
JB
13998 else
13999 bigdisp.bitfield.disp32 = 1;
e05278af
JB
14000 }
14001 else
14002 {
376cd056
JB
14003 /* For PC-relative branches, the width of the displacement may be
14004 dependent upon data size, but is never dependent upon address size.
14005 Also make sure to not unintentionally match against a non-PC-relative
14006 branch template. */
d3b01414 14007 const insn_template *t = current_templates.start;
5b7c81bd 14008 bool has_intel64 = false;
376cd056 14009
d3b01414 14010 while (++t < current_templates.end)
376cd056
JB
14011 {
14012 if (t->opcode_modifier.jump
d3b01414 14013 != current_templates.start->opcode_modifier.jump)
376cd056 14014 break;
4b5aaf5f 14015 if ((t->opcode_modifier.isa64 >= INTEL64))
5b7c81bd 14016 has_intel64 = true;
376cd056 14017 }
d3b01414 14018 current_templates.end = t;
376cd056 14019
e05278af 14020 override = (i.prefix[DATA_PREFIX] != 0);
40fb9820
L
14021 if (flag_code == CODE_64BIT)
14022 {
376cd056
JB
14023 if ((override || i.suffix == WORD_MNEM_SUFFIX)
14024 && (!intel64 || !has_intel64))
40fb9820
L
14025 bigdisp.bitfield.disp16 = 1;
14026 else
a775efc8 14027 bigdisp.bitfield.disp32 = 1;
40fb9820
L
14028 }
14029 else
e05278af
JB
14030 {
14031 if (!override)
14032 override = (i.suffix == (flag_code != CODE_16BIT
14033 ? WORD_MNEM_SUFFIX
14034 : LONG_MNEM_SUFFIX));
40fb9820
L
14035 bigdisp.bitfield.disp32 = 1;
14036 if ((flag_code == CODE_16BIT) ^ override)
14037 {
14038 bigdisp.bitfield.disp32 = 0;
14039 bigdisp.bitfield.disp16 = 1;
14040 }
e05278af 14041 }
e05278af 14042 }
c6fb90c8
L
14043 i.types[this_operand] = operand_type_or (i.types[this_operand],
14044 bigdisp);
252b5132
RH
14045
14046 exp = &disp_expressions[i.disp_operands];
520dc8e8 14047 i.op[this_operand].disps = exp;
252b5132
RH
14048 i.disp_operands++;
14049 save_input_line_pointer = input_line_pointer;
14050 input_line_pointer = disp_start;
14051 END_STRING_AND_SAVE (disp_end);
14052
14053#ifndef GCC_ASM_O_HACK
14054#define GCC_ASM_O_HACK 0
14055#endif
14056#if GCC_ASM_O_HACK
14057 END_STRING_AND_SAVE (disp_end + 1);
40fb9820 14058 if (i.types[this_operand].bitfield.baseIndex
24eab124 14059 && displacement_string_end[-1] == '+')
252b5132
RH
14060 {
14061 /* This hack is to avoid a warning when using the "o"
24eab124
AM
14062 constraint within gcc asm statements.
14063 For instance:
14064
14065 #define _set_tssldt_desc(n,addr,limit,type) \
14066 __asm__ __volatile__ ( \
14067 "movw %w2,%0\n\t" \
14068 "movw %w1,2+%0\n\t" \
14069 "rorl $16,%1\n\t" \
14070 "movb %b1,4+%0\n\t" \
14071 "movb %4,5+%0\n\t" \
14072 "movb $0,6+%0\n\t" \
14073 "movb %h1,7+%0\n\t" \
14074 "rorl $16,%1" \
14075 : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
14076
14077 This works great except that the output assembler ends
14078 up looking a bit weird if it turns out that there is
14079 no offset. You end up producing code that looks like:
14080
14081 #APP
14082 movw $235,(%eax)
14083 movw %dx,2+(%eax)
14084 rorl $16,%edx
14085 movb %dl,4+(%eax)
14086 movb $137,5+(%eax)
14087 movb $0,6+(%eax)
14088 movb %dh,7+(%eax)
14089 rorl $16,%edx
14090 #NO_APP
14091
47926f60 14092 So here we provide the missing zero. */
24eab124
AM
14093
14094 *displacement_string_end = '0';
252b5132
RH
14095 }
14096#endif
d258b828 14097 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
14098 if (gotfree_input_line)
14099 input_line_pointer = gotfree_input_line;
252b5132 14100
5cc00775 14101 expr_mode = expr_operator_none;
24eab124 14102 exp_seg = expression (exp);
252b5132 14103
636c26b0
AM
14104 SKIP_WHITESPACE ();
14105 if (*input_line_pointer)
14106 as_bad (_("junk `%s' after expression"), input_line_pointer);
14107#if GCC_ASM_O_HACK
14108 RESTORE_END_STRING (disp_end + 1);
14109#endif
636c26b0 14110 input_line_pointer = save_input_line_pointer;
636c26b0 14111 if (gotfree_input_line)
ee86248c
JB
14112 {
14113 free (gotfree_input_line);
14114
14115 if (exp->X_op == O_constant || exp->X_op == O_register)
14116 exp->X_op = O_illegal;
14117 }
14118
14119 ret = i386_finalize_displacement (exp_seg, exp, types, disp_start);
14120
14121 RESTORE_END_STRING (disp_end);
14122
14123 return ret;
14124}
14125
14126static int
14127i386_finalize_displacement (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
14128 i386_operand_type types, const char *disp_start)
14129{
ee86248c 14130 int ret = 1;
636c26b0 14131
24eab124
AM
14132 /* We do this to make sure that the section symbol is in
14133 the symbol table. We will ultimately change the relocation
47926f60 14134 to be relative to the beginning of the section. */
1ae12ab7 14135 if (i.reloc[this_operand] == BFD_RELOC_386_GOTOFF
d6ab8113
JB
14136 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
14137 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
24eab124 14138 {
636c26b0 14139 if (exp->X_op != O_symbol)
3992d3b7 14140 goto inv_disp;
636c26b0 14141
e5cb08ac 14142 if (S_IS_LOCAL (exp->X_add_symbol)
c64efb4b
L
14143 && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section
14144 && S_GET_SEGMENT (exp->X_add_symbol) != expr_section)
24eab124 14145 section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
24eab124
AM
14146 exp->X_op = O_subtract;
14147 exp->X_op_symbol = GOT_symbol;
1ae12ab7 14148 if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
29b0f896 14149 i.reloc[this_operand] = BFD_RELOC_32_PCREL;
d6ab8113
JB
14150 else if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
14151 i.reloc[this_operand] = BFD_RELOC_64;
23df1078 14152 else
29b0f896 14153 i.reloc[this_operand] = BFD_RELOC_32;
24eab124 14154 }
252b5132 14155
3992d3b7
AM
14156 else if (exp->X_op == O_absent
14157 || exp->X_op == O_illegal
ee86248c 14158 || exp->X_op == O_big)
2daf4fd8 14159 {
3992d3b7
AM
14160 inv_disp:
14161 as_bad (_("missing or invalid displacement expression `%s'"),
2daf4fd8 14162 disp_start);
3992d3b7 14163 ret = 0;
2daf4fd8
AM
14164 }
14165
a50187b2
JB
14166 else if (exp->X_op == O_constant)
14167 {
14168 /* Sizing gets taken care of by optimize_disp().
14169
14170 If not 64bit, sign/zero extend val, to account for wraparound
14171 when !BFD64. */
5cc00775
JB
14172 if (expr_mode == expr_operator_present
14173 && flag_code != CODE_64BIT && !object_64bit)
a50187b2
JB
14174 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
14175 }
14176
4c63da97 14177#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
a50187b2 14178 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
3992d3b7
AM
14179 && exp_seg != absolute_section
14180 && exp_seg != text_section
14181 && exp_seg != data_section
14182 && exp_seg != bss_section
14183 && exp_seg != undefined_section
14184 && !bfd_is_com_section (exp_seg))
24eab124 14185 {
d0b47220 14186 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
3992d3b7 14187 ret = 0;
24eab124 14188 }
252b5132 14189#endif
3956db08 14190
d3b01414 14191 else if (current_templates.start->opcode_modifier.jump == JUMP_BYTE)
48bcea9f
JB
14192 i.types[this_operand].bitfield.disp8 = 1;
14193
40fb9820 14194 /* Check if this is a displacement only operand. */
02b83698
JB
14195 if (!i.types[this_operand].bitfield.baseindex)
14196 i.types[this_operand] =
14197 operand_type_or (operand_type_and_not (i.types[this_operand], anydisp),
14198 operand_type_and (i.types[this_operand], types));
3956db08 14199
3992d3b7 14200 return ret;
252b5132
RH
14201}
14202
2abc2bec
JB
14203/* Return the active addressing mode, taking address override and
14204 registers forming the address into consideration. Update the
14205 address override prefix if necessary. */
47926f60 14206
2abc2bec
JB
14207static enum flag_code
14208i386_addressing_mode (void)
252b5132 14209{
be05d201
L
14210 enum flag_code addr_mode;
14211
14212 if (i.prefix[ADDR_PREFIX])
14213 addr_mode = flag_code == CODE_32BIT ? CODE_16BIT : CODE_32BIT;
a23b33b3 14214 else if (flag_code == CODE_16BIT
d3b01414 14215 && is_cpu (current_templates.start, CpuMPX)
a23b33b3
JB
14216 /* Avoid replacing the "16-bit addressing not allowed" diagnostic
14217 from md_assemble() by "is not a valid base/index expression"
14218 when there is a base and/or index. */
14219 && !i.types[this_operand].bitfield.baseindex)
14220 {
14221 /* MPX insn memory operands with neither base nor index must be forced
14222 to use 32-bit addressing in 16-bit mode. */
14223 addr_mode = CODE_32BIT;
14224 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
14225 ++i.prefixes;
14226 gas_assert (!i.types[this_operand].bitfield.disp16);
14227 gas_assert (!i.types[this_operand].bitfield.disp32);
14228 }
be05d201
L
14229 else
14230 {
14231 addr_mode = flag_code;
14232
24eab124 14233#if INFER_ADDR_PREFIX
be05d201
L
14234 if (i.mem_operands == 0)
14235 {
14236 /* Infer address prefix from the first memory operand. */
14237 const reg_entry *addr_reg = i.base_reg;
14238
14239 if (addr_reg == NULL)
14240 addr_reg = i.index_reg;
eecb386c 14241
be05d201
L
14242 if (addr_reg)
14243 {
e968fc9b 14244 if (addr_reg->reg_type.bitfield.dword)
be05d201
L
14245 addr_mode = CODE_32BIT;
14246 else if (flag_code != CODE_64BIT
dc821c5f 14247 && addr_reg->reg_type.bitfield.word)
be05d201
L
14248 addr_mode = CODE_16BIT;
14249
14250 if (addr_mode != flag_code)
14251 {
14252 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
14253 i.prefixes += 1;
14254 /* Change the size of any displacement too. At most one
14255 of Disp16 or Disp32 is set.
14256 FIXME. There doesn't seem to be any real need for
14257 separate Disp16 and Disp32 flags. The same goes for
14258 Imm16 and Imm32. Removing them would probably clean
14259 up the code quite a lot. */
14260 if (flag_code != CODE_64BIT
14261 && (i.types[this_operand].bitfield.disp16
14262 || i.types[this_operand].bitfield.disp32))
05909f23
JB
14263 {
14264 static const i386_operand_type disp16_32 = {
14265 .bitfield = { .disp16 = 1, .disp32 = 1 }
14266 };
14267
14268 i.types[this_operand]
14269 = operand_type_xor (i.types[this_operand], disp16_32);
14270 }
be05d201
L
14271 }
14272 }
14273 }
24eab124 14274#endif
be05d201
L
14275 }
14276
2abc2bec
JB
14277 return addr_mode;
14278}
14279
14280/* Make sure the memory operand we've been dealt is valid.
14281 Return 1 on success, 0 on a failure. */
14282
14283static int
14284i386_index_check (const char *operand_string)
14285{
14286 const char *kind = "base/index";
14287 enum flag_code addr_mode = i386_addressing_mode ();
d3b01414 14288 const insn_template *t = current_templates.end - 1;
2abc2bec 14289
77a19f0e 14290 if (t->opcode_modifier.isstring)
fc0763e6
JB
14291 {
14292 /* Memory operands of string insns are special in that they only allow
14293 a single register (rDI, rSI, or rBX) as their memory address. */
be05d201 14294 const reg_entry *expected_reg;
069a1561 14295 static const char di_si[][2][4] =
be05d201
L
14296 {
14297 { "esi", "edi" },
14298 { "si", "di" },
14299 { "rsi", "rdi" }
14300 };
069a1561 14301 static const char bx[][4] = { "ebx", "bx", "rbx" };
fc0763e6
JB
14302
14303 kind = "string address";
14304
a152332d 14305 if (t->opcode_modifier.prefixok == PrefixRep)
fc0763e6 14306 {
77a19f0e 14307 int es_op = t->opcode_modifier.isstring - IS_STRING_ES_OP0;
51c8edf6 14308 int op = 0;
fc0763e6 14309
77a19f0e 14310 if (!t->operand_types[0].bitfield.baseindex
fc0763e6 14311 || ((!i.mem_operands != !intel_syntax)
77a19f0e 14312 && t->operand_types[1].bitfield.baseindex))
51c8edf6 14313 op = 1;
fe0e921f
AM
14314 expected_reg
14315 = (const reg_entry *) str_hash_find (reg_hash,
14316 di_si[addr_mode][op == es_op]);
fc0763e6
JB
14317 }
14318 else
fe0e921f
AM
14319 expected_reg
14320 = (const reg_entry *)str_hash_find (reg_hash, bx[addr_mode]);
fc0763e6 14321
be05d201
L
14322 if (i.base_reg != expected_reg
14323 || i.index_reg
fc0763e6 14324 || operand_type_check (i.types[this_operand], disp))
fc0763e6 14325 {
be05d201
L
14326 /* The second memory operand must have the same size as
14327 the first one. */
14328 if (i.mem_operands
14329 && i.base_reg
14330 && !((addr_mode == CODE_64BIT
dc821c5f 14331 && i.base_reg->reg_type.bitfield.qword)
be05d201 14332 || (addr_mode == CODE_32BIT
dc821c5f
JB
14333 ? i.base_reg->reg_type.bitfield.dword
14334 : i.base_reg->reg_type.bitfield.word)))
be05d201
L
14335 goto bad_address;
14336
fc0763e6
JB
14337 as_warn (_("`%s' is not valid here (expected `%c%s%s%c')"),
14338 operand_string,
14339 intel_syntax ? '[' : '(',
14340 register_prefix,
be05d201 14341 expected_reg->reg_name,
fc0763e6 14342 intel_syntax ? ']' : ')');
be05d201 14343 return 1;
fc0763e6 14344 }
be05d201
L
14345 else
14346 return 1;
14347
dc1e8a47 14348 bad_address:
be05d201
L
14349 as_bad (_("`%s' is not a valid %s expression"),
14350 operand_string, kind);
14351 return 0;
3e73aa7c
JH
14352 }
14353 else
14354 {
d3b01414 14355 t = current_templates.start;
77a19f0e 14356
be05d201
L
14357 if (addr_mode != CODE_16BIT)
14358 {
14359 /* 32-bit/64-bit checks. */
41eb8e88
L
14360 if (i.disp_encoding == disp_encoding_16bit)
14361 {
14362 bad_disp:
14363 as_bad (_("invalid `%s' prefix"),
14364 addr_mode == CODE_16BIT ? "{disp32}" : "{disp16}");
14365 return 0;
14366 }
14367
be05d201 14368 if ((i.base_reg
e968fc9b
JB
14369 && ((addr_mode == CODE_64BIT
14370 ? !i.base_reg->reg_type.bitfield.qword
14371 : !i.base_reg->reg_type.bitfield.dword)
14372 || (i.index_reg && i.base_reg->reg_num == RegIP)
14373 || i.base_reg->reg_num == RegIZ))
be05d201 14374 || (i.index_reg
1b54b8d7
JB
14375 && !i.index_reg->reg_type.bitfield.xmmword
14376 && !i.index_reg->reg_type.bitfield.ymmword
14377 && !i.index_reg->reg_type.bitfield.zmmword
be05d201 14378 && ((addr_mode == CODE_64BIT
e968fc9b
JB
14379 ? !i.index_reg->reg_type.bitfield.qword
14380 : !i.index_reg->reg_type.bitfield.dword)
be05d201
L
14381 || !i.index_reg->reg_type.bitfield.baseindex)))
14382 goto bad_address;
8178be5b 14383
260cd341 14384 /* bndmk, bndldx, bndstx and mandatory non-vector SIB have special restrictions. */
7fc69528
JB
14385 if (t->mnem_off == MN_bndmk
14386 || t->mnem_off == MN_bndldx
14387 || t->mnem_off == MN_bndstx
a152332d 14388 || t->opcode_modifier.sib == SIBMEM)
8178be5b
JB
14389 {
14390 /* They cannot use RIP-relative addressing. */
e968fc9b 14391 if (i.base_reg && i.base_reg->reg_num == RegIP)
8178be5b
JB
14392 {
14393 as_bad (_("`%s' cannot be used here"), operand_string);
14394 return 0;
14395 }
14396
14397 /* bndldx and bndstx ignore their scale factor. */
7fc69528 14398 if ((t->mnem_off == MN_bndldx || t->mnem_off == MN_bndstx)
8178be5b
JB
14399 && i.log2_scale_factor)
14400 as_warn (_("register scaling is being ignored here"));
14401 }
be05d201
L
14402 }
14403 else
3e73aa7c 14404 {
be05d201 14405 /* 16-bit checks. */
41eb8e88
L
14406 if (i.disp_encoding == disp_encoding_32bit)
14407 goto bad_disp;
14408
3e73aa7c 14409 if ((i.base_reg
dc821c5f 14410 && (!i.base_reg->reg_type.bitfield.word
40fb9820 14411 || !i.base_reg->reg_type.bitfield.baseindex))
3e73aa7c 14412 || (i.index_reg
dc821c5f 14413 && (!i.index_reg->reg_type.bitfield.word
40fb9820 14414 || !i.index_reg->reg_type.bitfield.baseindex
29b0f896
AM
14415 || !(i.base_reg
14416 && i.base_reg->reg_num < 6
14417 && i.index_reg->reg_num >= 6
14418 && i.log2_scale_factor == 0))))
be05d201 14419 goto bad_address;
3e73aa7c
JH
14420 }
14421 }
be05d201 14422 return 1;
24eab124 14423}
252b5132 14424
43234a1e
L
14425/* Handle vector immediates. */
14426
14427static int
14428RC_SAE_immediate (const char *imm_start)
14429{
43234a1e 14430 const char *pstr = imm_start;
43234a1e
L
14431
14432 if (*pstr != '{')
14433 return 0;
14434
7063667e
JB
14435 pstr = RC_SAE_specifier (pstr + 1);
14436 if (pstr == NULL)
43234a1e
L
14437 return 0;
14438
14439 if (*pstr++ != '}')
14440 {
14441 as_bad (_("Missing '}': '%s'"), imm_start);
14442 return 0;
14443 }
14444 /* RC/SAE immediate string should contain nothing more. */;
14445 if (*pstr != 0)
14446 {
14447 as_bad (_("Junk after '}': '%s'"), imm_start);
14448 return 0;
14449 }
14450
cf665fee
JB
14451 /* Internally this doesn't count as an operand. */
14452 --i.operands;
43234a1e 14453
43234a1e
L
14454 return 1;
14455}
14456
9d299bea
JB
14457static INLINE bool starts_memory_operand (char c)
14458{
014fbcda 14459 return ISDIGIT (c)
4795cd4a 14460 || is_name_beginner (c)
014fbcda 14461 || strchr ("([\"+-!~", c);
9d299bea
JB
14462}
14463
fc0763e6 14464/* Parse OPERAND_STRING into the i386_insn structure I. Returns zero
47926f60 14465 on error. */
252b5132 14466
252b5132 14467static int
a7619375 14468i386_att_operand (char *operand_string)
252b5132 14469{
af6bdddf
AM
14470 const reg_entry *r;
14471 char *end_op;
24eab124 14472 char *op_string = operand_string;
252b5132 14473
24eab124 14474 if (is_space_char (*op_string))
252b5132
RH
14475 ++op_string;
14476
24eab124 14477 /* We check for an absolute prefix (differentiating,
47926f60 14478 for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
ccb05c9c 14479 if (*op_string == ABSOLUTE_PREFIX
d3b01414 14480 && current_templates.start->opcode_modifier.jump)
24eab124
AM
14481 {
14482 ++op_string;
14483 if (is_space_char (*op_string))
14484 ++op_string;
5b7c81bd 14485 i.jumpabsolute = true;
24eab124 14486 }
252b5132 14487
47926f60 14488 /* Check if operand is a register. */
4d1bb795 14489 if ((r = parse_register (op_string, &end_op)) != NULL)
24eab124 14490 {
40fb9820
L
14491 i386_operand_type temp;
14492
8a6fb3f9
JB
14493 if (r == &bad_reg)
14494 return 0;
14495
24eab124
AM
14496 /* Check for a segment override by searching for ':' after a
14497 segment register. */
14498 op_string = end_op;
14499 if (is_space_char (*op_string))
14500 ++op_string;
00cee14f 14501 if (*op_string == ':' && r->reg_type.bitfield.class == SReg)
24eab124 14502 {
5e042380 14503 i.seg[i.mem_operands] = r;
252b5132 14504
24eab124 14505 /* Skip the ':' and whitespace. */
252b5132
RH
14506 ++op_string;
14507 if (is_space_char (*op_string))
24eab124 14508 ++op_string;
252b5132 14509
47926f60 14510 /* Handle case of %es:*foo. */
ccb05c9c 14511 if (!i.jumpabsolute && *op_string == ABSOLUTE_PREFIX
d3b01414 14512 && current_templates.start->opcode_modifier.jump)
24eab124
AM
14513 {
14514 ++op_string;
14515 if (is_space_char (*op_string))
14516 ++op_string;
5b7c81bd 14517 i.jumpabsolute = true;
24eab124 14518 }
c8d541e2 14519
9d299bea 14520 if (!starts_memory_operand (*op_string))
c8d541e2
JB
14521 {
14522 as_bad (_("bad memory operand `%s'"), op_string);
14523 return 0;
14524 }
24eab124
AM
14525 goto do_memory_reference;
14526 }
43234a1e
L
14527
14528 /* Handle vector operations. */
14529 if (*op_string == '{')
14530 {
f70c6814 14531 op_string = check_VecOperations (op_string);
43234a1e
L
14532 if (op_string == NULL)
14533 return 0;
14534 }
14535
24eab124
AM
14536 if (*op_string)
14537 {
d0b47220 14538 as_bad (_("junk `%s' after register"), op_string);
24eab124
AM
14539 return 0;
14540 }
0ff3b7d0
JB
14541
14542 /* Reject pseudo registers for .insn. */
14543 if (dot_insn () && r->reg_type.bitfield.class == ClassNone)
14544 {
14545 as_bad (_("`%s%s' cannot be used here"),
14546 register_prefix, r->reg_name);
14547 return 0;
14548 }
14549
40fb9820
L
14550 temp = r->reg_type;
14551 temp.bitfield.baseindex = 0;
c6fb90c8
L
14552 i.types[this_operand] = operand_type_or (i.types[this_operand],
14553 temp);
7d5e4556 14554 i.types[this_operand].bitfield.unspecified = 0;
520dc8e8 14555 i.op[this_operand].regs = r;
24eab124 14556 i.reg_operands++;
cf665fee
JB
14557
14558 /* A GPR may follow an RC or SAE immediate only if a (vector) register
14559 operand was also present earlier on. */
14560 if (i.rounding.type != rc_none && temp.bitfield.class == Reg
14561 && i.reg_operands == 1)
14562 {
14563 unsigned int j;
14564
14565 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); ++j)
14566 if (i.rounding.type == RC_NamesTable[j].type)
14567 break;
14568 as_bad (_("`%s': misplaced `{%s}'"),
d3b01414 14569 insn_name (current_templates.start), RC_NamesTable[j].name);
cf665fee
JB
14570 return 0;
14571 }
24eab124 14572 }
af6bdddf
AM
14573 else if (*op_string == REGISTER_PREFIX)
14574 {
14575 as_bad (_("bad register name `%s'"), op_string);
14576 return 0;
14577 }
24eab124 14578 else if (*op_string == IMMEDIATE_PREFIX)
ce8a8b2f 14579 {
24eab124 14580 ++op_string;
6f2f06be 14581 if (i.jumpabsolute)
24eab124 14582 {
d0b47220 14583 as_bad (_("immediate operand illegal with absolute jump"));
24eab124
AM
14584 return 0;
14585 }
14586 if (!i386_immediate (op_string))
14587 return 0;
cf665fee
JB
14588 if (i.rounding.type != rc_none)
14589 {
14590 as_bad (_("`%s': RC/SAE operand must follow immediate operands"),
d3b01414 14591 insn_name (current_templates.start));
cf665fee
JB
14592 return 0;
14593 }
24eab124 14594 }
43234a1e
L
14595 else if (RC_SAE_immediate (operand_string))
14596 {
cf665fee
JB
14597 /* If it is a RC or SAE immediate, do the necessary placement check:
14598 Only another immediate or a GPR may precede it. */
14599 if (i.mem_operands || i.reg_operands + i.imm_operands > 1
14600 || (i.reg_operands == 1
14601 && i.op[0].regs->reg_type.bitfield.class != Reg))
14602 {
14603 as_bad (_("`%s': misplaced `%s'"),
d3b01414 14604 insn_name (current_templates.start), operand_string);
cf665fee
JB
14605 return 0;
14606 }
43234a1e 14607 }
9d299bea 14608 else if (starts_memory_operand (*op_string))
24eab124 14609 {
47926f60 14610 /* This is a memory reference of some sort. */
af6bdddf 14611 char *base_string;
252b5132 14612
47926f60 14613 /* Start and end of displacement string expression (if found). */
eecb386c
AM
14614 char *displacement_string_start;
14615 char *displacement_string_end;
252b5132 14616
24eab124 14617 do_memory_reference:
24eab124
AM
14618 /* Check for base index form. We detect the base index form by
14619 looking for an ')' at the end of the operand, searching
14620 for the '(' matching it, and finding a REGISTER_PREFIX or ','
14621 after the '('. */
af6bdddf 14622 base_string = op_string + strlen (op_string);
c3332e24 14623
43234a1e 14624 /* Handle vector operations. */
6b5ba0d4
JB
14625 --base_string;
14626 if (is_space_char (*base_string))
14627 --base_string;
14628
14629 if (*base_string == '}')
43234a1e 14630 {
6b5ba0d4
JB
14631 char *vop_start = NULL;
14632
14633 while (base_string-- > op_string)
14634 {
14635 if (*base_string == '"')
14636 break;
14637 if (*base_string != '{')
14638 continue;
14639
14640 vop_start = base_string;
14641
14642 --base_string;
14643 if (is_space_char (*base_string))
14644 --base_string;
14645
14646 if (*base_string != '}')
14647 break;
14648
14649 vop_start = NULL;
14650 }
14651
14652 if (!vop_start)
14653 {
14654 as_bad (_("unbalanced figure braces"));
14655 return 0;
14656 }
14657
f70c6814 14658 if (check_VecOperations (vop_start) == NULL)
43234a1e 14659 return 0;
43234a1e
L
14660 }
14661
47926f60 14662 /* If we only have a displacement, set-up for it to be parsed later. */
af6bdddf
AM
14663 displacement_string_start = op_string;
14664 displacement_string_end = base_string + 1;
252b5132 14665
24eab124
AM
14666 if (*base_string == ')')
14667 {
af6bdddf 14668 char *temp_string;
e87fb6a6
JB
14669 unsigned int parens_not_balanced = 0;
14670 bool in_quotes = false;
e68c3d59 14671
24eab124 14672 /* We've already checked that the number of left & right ()'s are
e87fb6a6
JB
14673 equal, and that there's a matching set of double quotes. */
14674 end_op = base_string;
14675 for (temp_string = op_string; temp_string < end_op; temp_string++)
24eab124 14676 {
e87fb6a6
JB
14677 if (*temp_string == '\\' && temp_string[1] == '"')
14678 ++temp_string;
14679 else if (*temp_string == '"')
14680 in_quotes = !in_quotes;
14681 else if (!in_quotes)
14682 {
14683 if (*temp_string == '(' && !parens_not_balanced++)
14684 base_string = temp_string;
14685 if (*temp_string == ')')
14686 --parens_not_balanced;
14687 }
24eab124 14688 }
c3332e24 14689
af6bdddf 14690 temp_string = base_string;
c3332e24 14691
24eab124 14692 /* Skip past '(' and whitespace. */
4f081312 14693 gas_assert (*base_string == '(');
e87fb6a6 14694 ++base_string;
252b5132 14695 if (is_space_char (*base_string))
24eab124 14696 ++base_string;
252b5132 14697
af6bdddf 14698 if (*base_string == ','
4eed87de
AM
14699 || ((i.base_reg = parse_register (base_string, &end_op))
14700 != NULL))
252b5132 14701 {
af6bdddf 14702 displacement_string_end = temp_string;
252b5132 14703
40fb9820 14704 i.types[this_operand].bitfield.baseindex = 1;
252b5132 14705
af6bdddf 14706 if (i.base_reg)
24eab124 14707 {
8a6fb3f9
JB
14708 if (i.base_reg == &bad_reg)
14709 return 0;
24eab124
AM
14710 base_string = end_op;
14711 if (is_space_char (*base_string))
14712 ++base_string;
af6bdddf
AM
14713 }
14714
14715 /* There may be an index reg or scale factor here. */
14716 if (*base_string == ',')
14717 {
14718 ++base_string;
14719 if (is_space_char (*base_string))
14720 ++base_string;
14721
4eed87de
AM
14722 if ((i.index_reg = parse_register (base_string, &end_op))
14723 != NULL)
24eab124 14724 {
8a6fb3f9
JB
14725 if (i.index_reg == &bad_reg)
14726 return 0;
af6bdddf 14727 base_string = end_op;
24eab124
AM
14728 if (is_space_char (*base_string))
14729 ++base_string;
af6bdddf
AM
14730 if (*base_string == ',')
14731 {
14732 ++base_string;
14733 if (is_space_char (*base_string))
14734 ++base_string;
14735 }
e5cb08ac 14736 else if (*base_string != ')')
af6bdddf 14737 {
4eed87de
AM
14738 as_bad (_("expecting `,' or `)' "
14739 "after index register in `%s'"),
af6bdddf
AM
14740 operand_string);
14741 return 0;
14742 }
24eab124 14743 }
af6bdddf 14744 else if (*base_string == REGISTER_PREFIX)
24eab124 14745 {
f76bf5e0
L
14746 end_op = strchr (base_string, ',');
14747 if (end_op)
14748 *end_op = '\0';
af6bdddf 14749 as_bad (_("bad register name `%s'"), base_string);
24eab124
AM
14750 return 0;
14751 }
252b5132 14752
47926f60 14753 /* Check for scale factor. */
551c1ca1 14754 if (*base_string != ')')
af6bdddf 14755 {
551c1ca1
AM
14756 char *end_scale = i386_scale (base_string);
14757
14758 if (!end_scale)
af6bdddf 14759 return 0;
24eab124 14760
551c1ca1 14761 base_string = end_scale;
af6bdddf
AM
14762 if (is_space_char (*base_string))
14763 ++base_string;
14764 if (*base_string != ')')
14765 {
4eed87de
AM
14766 as_bad (_("expecting `)' "
14767 "after scale factor in `%s'"),
af6bdddf
AM
14768 operand_string);
14769 return 0;
14770 }
14771 }
14772 else if (!i.index_reg)
24eab124 14773 {
4eed87de
AM
14774 as_bad (_("expecting index register or scale factor "
14775 "after `,'; got '%c'"),
af6bdddf 14776 *base_string);
24eab124
AM
14777 return 0;
14778 }
14779 }
af6bdddf 14780 else if (*base_string != ')')
24eab124 14781 {
4eed87de
AM
14782 as_bad (_("expecting `,' or `)' "
14783 "after base register in `%s'"),
af6bdddf 14784 operand_string);
24eab124
AM
14785 return 0;
14786 }
c3332e24 14787 }
af6bdddf 14788 else if (*base_string == REGISTER_PREFIX)
c3332e24 14789 {
f76bf5e0
L
14790 end_op = strchr (base_string, ',');
14791 if (end_op)
14792 *end_op = '\0';
af6bdddf 14793 as_bad (_("bad register name `%s'"), base_string);
24eab124 14794 return 0;
c3332e24 14795 }
24eab124
AM
14796 }
14797
14798 /* If there's an expression beginning the operand, parse it,
14799 assuming displacement_string_start and
14800 displacement_string_end are meaningful. */
14801 if (displacement_string_start != displacement_string_end)
14802 {
14803 if (!i386_displacement (displacement_string_start,
14804 displacement_string_end))
14805 return 0;
14806 }
14807
14808 /* Special case for (%dx) while doing input/output op. */
14809 if (i.base_reg
75e5731b
JB
14810 && i.base_reg->reg_type.bitfield.instance == RegD
14811 && i.base_reg->reg_type.bitfield.word
24eab124
AM
14812 && i.index_reg == 0
14813 && i.log2_scale_factor == 0
14814 && i.seg[i.mem_operands] == 0
40fb9820 14815 && !operand_type_check (i.types[this_operand], disp))
24eab124 14816 {
2fb5be8d 14817 i.types[this_operand] = i.base_reg->reg_type;
f0a4cb15
CL
14818 i.op[this_operand].regs = i.base_reg;
14819 i.base_reg = NULL;
9373f275 14820 i.input_output_operand = true;
24eab124
AM
14821 return 1;
14822 }
14823
eecb386c
AM
14824 if (i386_index_check (operand_string) == 0)
14825 return 0;
c48dadc9 14826 i.flags[this_operand] |= Operand_Mem;
24eab124
AM
14827 i.mem_operands++;
14828 }
14829 else
ce8a8b2f
AM
14830 {
14831 /* It's not a memory operand; argh! */
24eab124
AM
14832 as_bad (_("invalid char %s beginning operand %d `%s'"),
14833 output_invalid (*op_string),
14834 this_operand + 1,
14835 op_string);
14836 return 0;
14837 }
47926f60 14838 return 1; /* Normal return. */
252b5132
RH
14839}
14840\f
fa94de6b
RM
14841/* Calculate the maximum variable size (i.e., excluding fr_fix)
14842 that an rs_machine_dependent frag may reach. */
14843
14844unsigned int
14845i386_frag_max_var (fragS *frag)
14846{
14847 /* The only relaxable frags are for jumps.
14848 Unconditional jumps can grow by 4 bytes and others by 5 bytes. */
14849 gas_assert (frag->fr_type == rs_machine_dependent);
14850 return TYPE_FROM_RELAX_STATE (frag->fr_subtype) == UNCOND_JUMP ? 4 : 5;
14851}
14852
b084df0b
L
14853#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14854static int
8dcea932 14855elf_symbol_resolved_in_segment_p (symbolS *fr_symbol, offsetT fr_var)
b084df0b
L
14856{
14857 /* STT_GNU_IFUNC symbol must go through PLT. */
14858 if ((symbol_get_bfdsym (fr_symbol)->flags
14859 & BSF_GNU_INDIRECT_FUNCTION) != 0)
14860 return 0;
14861
14862 if (!S_IS_EXTERNAL (fr_symbol))
14863 /* Symbol may be weak or local. */
14864 return !S_IS_WEAK (fr_symbol);
14865
8dcea932
L
14866 /* Global symbols with non-default visibility can't be preempted. */
14867 if (ELF_ST_VISIBILITY (S_GET_OTHER (fr_symbol)) != STV_DEFAULT)
14868 return 1;
14869
14870 if (fr_var != NO_RELOC)
14871 switch ((enum bfd_reloc_code_real) fr_var)
14872 {
14873 case BFD_RELOC_386_PLT32:
14874 case BFD_RELOC_X86_64_PLT32:
33eaf5de 14875 /* Symbol with PLT relocation may be preempted. */
8dcea932
L
14876 return 0;
14877 default:
14878 abort ();
14879 }
14880
b084df0b
L
14881 /* Global symbols with default visibility in a shared library may be
14882 preempted by another definition. */
8dcea932 14883 return !shared;
b084df0b
L
14884}
14885#endif
14886
79d72f45
HL
14887/* Table 3-2. Macro-Fusible Instructions in Haswell Microarchitecture
14888 Note also work for Skylake and Cascadelake.
14889---------------------------------------------------------------------
14890| JCC | ADD/SUB/CMP | INC/DEC | TEST/AND |
14891| ------ | ----------- | ------- | -------- |
14892| Jo | N | N | Y |
14893| Jno | N | N | Y |
14894| Jc/Jb | Y | N | Y |
14895| Jae/Jnb | Y | N | Y |
14896| Je/Jz | Y | Y | Y |
14897| Jne/Jnz | Y | Y | Y |
14898| Jna/Jbe | Y | N | Y |
14899| Ja/Jnbe | Y | N | Y |
14900| Js | N | N | Y |
14901| Jns | N | N | Y |
14902| Jp/Jpe | N | N | Y |
14903| Jnp/Jpo | N | N | Y |
14904| Jl/Jnge | Y | Y | Y |
14905| Jge/Jnl | Y | Y | Y |
14906| Jle/Jng | Y | Y | Y |
14907| Jg/Jnle | Y | Y | Y |
14908--------------------------------------------------------------------- */
14909static int
14910i386_macro_fusible_p (enum mf_cmp_kind mf_cmp, enum mf_jcc_kind mf_jcc)
14911{
14912 if (mf_cmp == mf_cmp_alu_cmp)
14913 return ((mf_jcc >= mf_jcc_jc && mf_jcc <= mf_jcc_jna)
14914 || mf_jcc == mf_jcc_jl || mf_jcc == mf_jcc_jle);
14915 if (mf_cmp == mf_cmp_incdec)
14916 return (mf_jcc == mf_jcc_je || mf_jcc == mf_jcc_jl
14917 || mf_jcc == mf_jcc_jle);
14918 if (mf_cmp == mf_cmp_test_and)
14919 return 1;
14920 return 0;
14921}
14922
e379e5f3
L
14923/* Return the next non-empty frag. */
14924
14925static fragS *
14926i386_next_non_empty_frag (fragS *fragP)
14927{
14928 /* There may be a frag with a ".fill 0" when there is no room in
14929 the current frag for frag_grow in output_insn. */
14930 for (fragP = fragP->fr_next;
14931 (fragP != NULL
14932 && fragP->fr_type == rs_fill
14933 && fragP->fr_fix == 0);
14934 fragP = fragP->fr_next)
14935 ;
14936 return fragP;
14937}
14938
14939/* Return the next jcc frag after BRANCH_PADDING. */
14940
14941static fragS *
79d72f45 14942i386_next_fusible_jcc_frag (fragS *maybe_cmp_fragP, fragS *pad_fragP)
e379e5f3 14943{
79d72f45
HL
14944 fragS *branch_fragP;
14945 if (!pad_fragP)
e379e5f3
L
14946 return NULL;
14947
79d72f45
HL
14948 if (pad_fragP->fr_type == rs_machine_dependent
14949 && (TYPE_FROM_RELAX_STATE (pad_fragP->fr_subtype)
e379e5f3
L
14950 == BRANCH_PADDING))
14951 {
79d72f45
HL
14952 branch_fragP = i386_next_non_empty_frag (pad_fragP);
14953 if (branch_fragP->fr_type != rs_machine_dependent)
e379e5f3 14954 return NULL;
79d72f45
HL
14955 if (TYPE_FROM_RELAX_STATE (branch_fragP->fr_subtype) == COND_JUMP
14956 && i386_macro_fusible_p (maybe_cmp_fragP->tc_frag_data.mf_type,
14957 pad_fragP->tc_frag_data.mf_type))
14958 return branch_fragP;
e379e5f3
L
14959 }
14960
14961 return NULL;
14962}
14963
14964/* Classify BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags. */
14965
14966static void
14967i386_classify_machine_dependent_frag (fragS *fragP)
14968{
14969 fragS *cmp_fragP;
14970 fragS *pad_fragP;
14971 fragS *branch_fragP;
14972 fragS *next_fragP;
14973 unsigned int max_prefix_length;
14974
14975 if (fragP->tc_frag_data.classified)
14976 return;
14977
14978 /* First scan for BRANCH_PADDING and FUSED_JCC_PADDING. Convert
14979 FUSED_JCC_PADDING and merge BRANCH_PADDING. */
14980 for (next_fragP = fragP;
14981 next_fragP != NULL;
14982 next_fragP = next_fragP->fr_next)
14983 {
14984 next_fragP->tc_frag_data.classified = 1;
14985 if (next_fragP->fr_type == rs_machine_dependent)
14986 switch (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype))
14987 {
14988 case BRANCH_PADDING:
14989 /* The BRANCH_PADDING frag must be followed by a branch
14990 frag. */
14991 branch_fragP = i386_next_non_empty_frag (next_fragP);
14992 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
14993 break;
14994 case FUSED_JCC_PADDING:
14995 /* Check if this is a fused jcc:
14996 FUSED_JCC_PADDING
14997 CMP like instruction
14998 BRANCH_PADDING
14999 COND_JUMP
15000 */
15001 cmp_fragP = i386_next_non_empty_frag (next_fragP);
15002 pad_fragP = i386_next_non_empty_frag (cmp_fragP);
79d72f45 15003 branch_fragP = i386_next_fusible_jcc_frag (next_fragP, pad_fragP);
e379e5f3
L
15004 if (branch_fragP)
15005 {
15006 /* The BRANCH_PADDING frag is merged with the
15007 FUSED_JCC_PADDING frag. */
15008 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
15009 /* CMP like instruction size. */
15010 next_fragP->tc_frag_data.cmp_size = cmp_fragP->fr_fix;
15011 frag_wane (pad_fragP);
15012 /* Skip to branch_fragP. */
15013 next_fragP = branch_fragP;
15014 }
15015 else if (next_fragP->tc_frag_data.max_prefix_length)
15016 {
15017 /* Turn FUSED_JCC_PADDING into BRANCH_PREFIX if it isn't
15018 a fused jcc. */
15019 next_fragP->fr_subtype
15020 = ENCODE_RELAX_STATE (BRANCH_PREFIX, 0);
15021 next_fragP->tc_frag_data.max_bytes
15022 = next_fragP->tc_frag_data.max_prefix_length;
15023 /* This will be updated in the BRANCH_PREFIX scan. */
15024 next_fragP->tc_frag_data.max_prefix_length = 0;
15025 }
15026 else
15027 frag_wane (next_fragP);
15028 break;
15029 }
15030 }
15031
15032 /* Stop if there is no BRANCH_PREFIX. */
15033 if (!align_branch_prefix_size)
15034 return;
15035
15036 /* Scan for BRANCH_PREFIX. */
15037 for (; fragP != NULL; fragP = fragP->fr_next)
15038 {
15039 if (fragP->fr_type != rs_machine_dependent
15040 || (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
15041 != BRANCH_PREFIX))
15042 continue;
15043
15044 /* Count all BRANCH_PREFIX frags before BRANCH_PADDING and
15045 COND_JUMP_PREFIX. */
15046 max_prefix_length = 0;
15047 for (next_fragP = fragP;
15048 next_fragP != NULL;
15049 next_fragP = next_fragP->fr_next)
15050 {
15051 if (next_fragP->fr_type == rs_fill)
15052 /* Skip rs_fill frags. */
15053 continue;
15054 else if (next_fragP->fr_type != rs_machine_dependent)
15055 /* Stop for all other frags. */
15056 break;
15057
15058 /* rs_machine_dependent frags. */
15059 if (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
15060 == BRANCH_PREFIX)
15061 {
15062 /* Count BRANCH_PREFIX frags. */
15063 if (max_prefix_length >= MAX_FUSED_JCC_PADDING_SIZE)
15064 {
15065 max_prefix_length = MAX_FUSED_JCC_PADDING_SIZE;
15066 frag_wane (next_fragP);
15067 }
15068 else
15069 max_prefix_length
15070 += next_fragP->tc_frag_data.max_bytes;
15071 }
15072 else if ((TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
15073 == BRANCH_PADDING)
15074 || (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
15075 == FUSED_JCC_PADDING))
15076 {
15077 /* Stop at BRANCH_PADDING and FUSED_JCC_PADDING. */
15078 fragP->tc_frag_data.u.padding_fragP = next_fragP;
15079 break;
15080 }
15081 else
15082 /* Stop for other rs_machine_dependent frags. */
15083 break;
15084 }
15085
15086 fragP->tc_frag_data.max_prefix_length = max_prefix_length;
15087
15088 /* Skip to the next frag. */
15089 fragP = next_fragP;
15090 }
15091}
15092
15093/* Compute padding size for
15094
15095 FUSED_JCC_PADDING
15096 CMP like instruction
15097 BRANCH_PADDING
15098 COND_JUMP/UNCOND_JUMP
15099
15100 or
15101
15102 BRANCH_PADDING
15103 COND_JUMP/UNCOND_JUMP
15104 */
15105
15106static int
15107i386_branch_padding_size (fragS *fragP, offsetT address)
15108{
15109 unsigned int offset, size, padding_size;
15110 fragS *branch_fragP = fragP->tc_frag_data.u.branch_fragP;
15111
15112 /* The start address of the BRANCH_PADDING or FUSED_JCC_PADDING frag. */
15113 if (!address)
15114 address = fragP->fr_address;
15115 address += fragP->fr_fix;
15116
15117 /* CMP like instrunction size. */
15118 size = fragP->tc_frag_data.cmp_size;
15119
15120 /* The base size of the branch frag. */
15121 size += branch_fragP->fr_fix;
15122
15123 /* Add opcode and displacement bytes for the rs_machine_dependent
15124 branch frag. */
15125 if (branch_fragP->fr_type == rs_machine_dependent)
15126 size += md_relax_table[branch_fragP->fr_subtype].rlx_length;
15127
15128 /* Check if branch is within boundary and doesn't end at the last
15129 byte. */
15130 offset = address & ((1U << align_branch_power) - 1);
15131 if ((offset + size) >= (1U << align_branch_power))
15132 /* Padding needed to avoid crossing boundary. */
15133 padding_size = (1U << align_branch_power) - offset;
15134 else
15135 /* No padding needed. */
15136 padding_size = 0;
15137
15138 /* The return value may be saved in tc_frag_data.length which is
15139 unsigned byte. */
15140 if (!fits_in_unsigned_byte (padding_size))
15141 abort ();
15142
15143 return padding_size;
15144}
15145
15146/* i386_generic_table_relax_frag()
15147
15148 Handle BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags to
15149 grow/shrink padding to align branch frags. Hand others to
15150 relax_frag(). */
15151
15152long
15153i386_generic_table_relax_frag (segT segment, fragS *fragP, long stretch)
15154{
15155 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
15156 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
15157 {
15158 long padding_size = i386_branch_padding_size (fragP, 0);
15159 long grow = padding_size - fragP->tc_frag_data.length;
15160
15161 /* When the BRANCH_PREFIX frag is used, the computed address
15162 must match the actual address and there should be no padding. */
15163 if (fragP->tc_frag_data.padding_address
15164 && (fragP->tc_frag_data.padding_address != fragP->fr_address
15165 || padding_size))
15166 abort ();
15167
15168 /* Update the padding size. */
15169 if (grow)
15170 fragP->tc_frag_data.length = padding_size;
15171
15172 return grow;
15173 }
15174 else if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
15175 {
15176 fragS *padding_fragP, *next_fragP;
15177 long padding_size, left_size, last_size;
15178
15179 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
15180 if (!padding_fragP)
15181 /* Use the padding set by the leading BRANCH_PREFIX frag. */
15182 return (fragP->tc_frag_data.length
15183 - fragP->tc_frag_data.last_length);
15184
15185 /* Compute the relative address of the padding frag in the very
15186 first time where the BRANCH_PREFIX frag sizes are zero. */
15187 if (!fragP->tc_frag_data.padding_address)
15188 fragP->tc_frag_data.padding_address
15189 = padding_fragP->fr_address - (fragP->fr_address - stretch);
15190
15191 /* First update the last length from the previous interation. */
15192 left_size = fragP->tc_frag_data.prefix_length;
15193 for (next_fragP = fragP;
15194 next_fragP != padding_fragP;
15195 next_fragP = next_fragP->fr_next)
15196 if (next_fragP->fr_type == rs_machine_dependent
15197 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
15198 == BRANCH_PREFIX))
15199 {
15200 if (left_size)
15201 {
15202 int max = next_fragP->tc_frag_data.max_bytes;
15203 if (max)
15204 {
15205 int size;
15206 if (max > left_size)
15207 size = left_size;
15208 else
15209 size = max;
15210 left_size -= size;
15211 next_fragP->tc_frag_data.last_length = size;
15212 }
15213 }
15214 else
15215 next_fragP->tc_frag_data.last_length = 0;
15216 }
15217
15218 /* Check the padding size for the padding frag. */
15219 padding_size = i386_branch_padding_size
15220 (padding_fragP, (fragP->fr_address
15221 + fragP->tc_frag_data.padding_address));
15222
15223 last_size = fragP->tc_frag_data.prefix_length;
15224 /* Check if there is change from the last interation. */
15225 if (padding_size == last_size)
15226 {
15227 /* Update the expected address of the padding frag. */
15228 padding_fragP->tc_frag_data.padding_address
15229 = (fragP->fr_address + padding_size
15230 + fragP->tc_frag_data.padding_address);
15231 return 0;
15232 }
15233
15234 if (padding_size > fragP->tc_frag_data.max_prefix_length)
15235 {
15236 /* No padding if there is no sufficient room. Clear the
15237 expected address of the padding frag. */
15238 padding_fragP->tc_frag_data.padding_address = 0;
15239 padding_size = 0;
15240 }
15241 else
15242 /* Store the expected address of the padding frag. */
15243 padding_fragP->tc_frag_data.padding_address
15244 = (fragP->fr_address + padding_size
15245 + fragP->tc_frag_data.padding_address);
15246
15247 fragP->tc_frag_data.prefix_length = padding_size;
15248
15249 /* Update the length for the current interation. */
15250 left_size = padding_size;
15251 for (next_fragP = fragP;
15252 next_fragP != padding_fragP;
15253 next_fragP = next_fragP->fr_next)
15254 if (next_fragP->fr_type == rs_machine_dependent
15255 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
15256 == BRANCH_PREFIX))
15257 {
15258 if (left_size)
15259 {
15260 int max = next_fragP->tc_frag_data.max_bytes;
15261 if (max)
15262 {
15263 int size;
15264 if (max > left_size)
15265 size = left_size;
15266 else
15267 size = max;
15268 left_size -= size;
15269 next_fragP->tc_frag_data.length = size;
15270 }
15271 }
15272 else
15273 next_fragP->tc_frag_data.length = 0;
15274 }
15275
15276 return (fragP->tc_frag_data.length
15277 - fragP->tc_frag_data.last_length);
15278 }
15279 return relax_frag (segment, fragP, stretch);
15280}
15281
ee7fcc42
AM
15282/* md_estimate_size_before_relax()
15283
15284 Called just before relax() for rs_machine_dependent frags. The x86
15285 assembler uses these frags to handle variable size jump
15286 instructions.
15287
15288 Any symbol that is now undefined will not become defined.
15289 Return the correct fr_subtype in the frag.
15290 Return the initial "guess for variable size of frag" to caller.
15291 The guess is actually the growth beyond the fixed part. Whatever
15292 we do to grow the fixed or variable part contributes to our
15293 returned value. */
15294
252b5132 15295int
7016a5d5 15296md_estimate_size_before_relax (fragS *fragP, segT segment)
252b5132 15297{
e379e5f3
L
15298 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
15299 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX
15300 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
15301 {
15302 i386_classify_machine_dependent_frag (fragP);
15303 return fragP->tc_frag_data.length;
15304 }
15305
252b5132 15306 /* We've already got fragP->fr_subtype right; all we have to do is
b98ef147
AM
15307 check for un-relaxable symbols. On an ELF system, we can't relax
15308 an externally visible symbol, because it may be overridden by a
15309 shared library. */
15310 if (S_GET_SEGMENT (fragP->fr_symbol) != segment
6d249963 15311#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 15312 || (IS_ELF
8dcea932
L
15313 && !elf_symbol_resolved_in_segment_p (fragP->fr_symbol,
15314 fragP->fr_var))
fbeb56a4
DK
15315#endif
15316#if defined (OBJ_COFF) && defined (TE_PE)
7ab9ffdd 15317 || (OUTPUT_FLAVOR == bfd_target_coff_flavour
fbeb56a4 15318 && S_IS_WEAK (fragP->fr_symbol))
b98ef147
AM
15319#endif
15320 )
252b5132 15321 {
b98ef147
AM
15322 /* Symbol is undefined in this segment, or we need to keep a
15323 reloc so that weak symbols can be overridden. */
15324 int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
f86103b7 15325 enum bfd_reloc_code_real reloc_type;
ee7fcc42
AM
15326 unsigned char *opcode;
15327 int old_fr_fix;
eb19308f 15328 fixS *fixP = NULL;
f6af82bd 15329
ee7fcc42 15330 if (fragP->fr_var != NO_RELOC)
1e9cc1c2 15331 reloc_type = (enum bfd_reloc_code_real) fragP->fr_var;
b98ef147 15332 else if (size == 2)
f6af82bd 15333 reloc_type = BFD_RELOC_16_PCREL;
bd7ab16b 15334#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
ed719294
JB
15335 else if (fragP->tc_frag_data.code == CODE_64BIT
15336 && fragP->fr_offset == 0
1ef3994a 15337 && need_plt32_p (fragP->fr_symbol))
bd7ab16b
L
15338 reloc_type = BFD_RELOC_X86_64_PLT32;
15339#endif
f6af82bd
AM
15340 else
15341 reloc_type = BFD_RELOC_32_PCREL;
252b5132 15342
ee7fcc42
AM
15343 old_fr_fix = fragP->fr_fix;
15344 opcode = (unsigned char *) fragP->fr_opcode;
15345
fddf5b5b 15346 switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
252b5132 15347 {
fddf5b5b
AM
15348 case UNCOND_JUMP:
15349 /* Make jmp (0xeb) a (d)word displacement jump. */
47926f60 15350 opcode[0] = 0xe9;
252b5132 15351 fragP->fr_fix += size;
eb19308f
JB
15352 fixP = fix_new (fragP, old_fr_fix, size,
15353 fragP->fr_symbol,
15354 fragP->fr_offset, 1,
15355 reloc_type);
252b5132
RH
15356 break;
15357
fddf5b5b 15358 case COND_JUMP86:
412167cb
AM
15359 if (size == 2
15360 && (!no_cond_jump_promotion || fragP->fr_var != NO_RELOC))
fddf5b5b
AM
15361 {
15362 /* Negate the condition, and branch past an
15363 unconditional jump. */
15364 opcode[0] ^= 1;
15365 opcode[1] = 3;
15366 /* Insert an unconditional jump. */
15367 opcode[2] = 0xe9;
15368 /* We added two extra opcode bytes, and have a two byte
15369 offset. */
15370 fragP->fr_fix += 2 + 2;
062cd5e7
AS
15371 fix_new (fragP, old_fr_fix + 2, 2,
15372 fragP->fr_symbol,
15373 fragP->fr_offset, 1,
15374 reloc_type);
fddf5b5b
AM
15375 break;
15376 }
15377 /* Fall through. */
15378
15379 case COND_JUMP:
412167cb
AM
15380 if (no_cond_jump_promotion && fragP->fr_var == NO_RELOC)
15381 {
15382 fragP->fr_fix += 1;
3e02c1cc
AM
15383 fixP = fix_new (fragP, old_fr_fix, 1,
15384 fragP->fr_symbol,
15385 fragP->fr_offset, 1,
15386 BFD_RELOC_8_PCREL);
15387 fixP->fx_signed = 1;
412167cb
AM
15388 break;
15389 }
93c2a809 15390
24eab124 15391 /* This changes the byte-displacement jump 0x7N
fddf5b5b 15392 to the (d)word-displacement jump 0x0f,0x8N. */
252b5132 15393 opcode[1] = opcode[0] + 0x10;
f6af82bd 15394 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
47926f60
KH
15395 /* We've added an opcode byte. */
15396 fragP->fr_fix += 1 + size;
eb19308f
JB
15397 fixP = fix_new (fragP, old_fr_fix + 1, size,
15398 fragP->fr_symbol,
15399 fragP->fr_offset, 1,
15400 reloc_type);
252b5132 15401 break;
fddf5b5b
AM
15402
15403 default:
15404 BAD_CASE (fragP->fr_subtype);
15405 break;
252b5132 15406 }
eb19308f
JB
15407
15408 /* All jumps handled here are signed, but don't unconditionally use a
15409 signed limit check for 32 and 16 bit jumps as we want to allow wrap
15410 around at 4G (outside of 64-bit mode) and 64k. */
15411 if (size == 4 && flag_code == CODE_64BIT)
15412 fixP->fx_signed = 1;
15413
252b5132 15414 frag_wane (fragP);
ee7fcc42 15415 return fragP->fr_fix - old_fr_fix;
252b5132 15416 }
93c2a809 15417
93c2a809
AM
15418 /* Guess size depending on current relax state. Initially the relax
15419 state will correspond to a short jump and we return 1, because
15420 the variable part of the frag (the branch offset) is one byte
15421 long. However, we can relax a section more than once and in that
15422 case we must either set fr_subtype back to the unrelaxed state,
15423 or return the value for the appropriate branch. */
15424 return md_relax_table[fragP->fr_subtype].rlx_length;
ee7fcc42
AM
15425}
15426
47926f60
KH
15427/* Called after relax() is finished.
15428
15429 In: Address of frag.
15430 fr_type == rs_machine_dependent.
15431 fr_subtype is what the address relaxed to.
15432
15433 Out: Any fixSs and constants are set up.
15434 Caller will turn frag into a ".space 0". */
15435
252b5132 15436void
7016a5d5
TG
15437md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec ATTRIBUTE_UNUSED,
15438 fragS *fragP)
252b5132 15439{
29b0f896 15440 unsigned char *opcode;
252b5132 15441 unsigned char *where_to_put_displacement = NULL;
847f7ad4
AM
15442 offsetT target_address;
15443 offsetT opcode_address;
252b5132 15444 unsigned int extension = 0;
847f7ad4 15445 offsetT displacement_from_opcode_start;
252b5132 15446
e379e5f3
L
15447 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
15448 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING
15449 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
15450 {
15451 /* Generate nop padding. */
15452 unsigned int size = fragP->tc_frag_data.length;
15453 if (size)
15454 {
15455 if (size > fragP->tc_frag_data.max_bytes)
15456 abort ();
15457
15458 if (flag_debug)
15459 {
15460 const char *msg;
15461 const char *branch = "branch";
15462 const char *prefix = "";
15463 fragS *padding_fragP;
15464 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
15465 == BRANCH_PREFIX)
15466 {
15467 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
15468 switch (fragP->tc_frag_data.default_prefix)
15469 {
15470 default:
15471 abort ();
15472 break;
15473 case CS_PREFIX_OPCODE:
15474 prefix = " cs";
15475 break;
15476 case DS_PREFIX_OPCODE:
15477 prefix = " ds";
15478 break;
15479 case ES_PREFIX_OPCODE:
15480 prefix = " es";
15481 break;
15482 case FS_PREFIX_OPCODE:
15483 prefix = " fs";
15484 break;
15485 case GS_PREFIX_OPCODE:
15486 prefix = " gs";
15487 break;
15488 case SS_PREFIX_OPCODE:
15489 prefix = " ss";
15490 break;
15491 }
15492 if (padding_fragP)
15493 msg = _("%s:%u: add %d%s at 0x%llx to align "
15494 "%s within %d-byte boundary\n");
15495 else
15496 msg = _("%s:%u: add additional %d%s at 0x%llx to "
15497 "align %s within %d-byte boundary\n");
15498 }
15499 else
15500 {
15501 padding_fragP = fragP;
15502 msg = _("%s:%u: add %d%s-byte nop at 0x%llx to align "
15503 "%s within %d-byte boundary\n");
15504 }
15505
15506 if (padding_fragP)
15507 switch (padding_fragP->tc_frag_data.branch_type)
15508 {
15509 case align_branch_jcc:
15510 branch = "jcc";
15511 break;
15512 case align_branch_fused:
15513 branch = "fused jcc";
15514 break;
15515 case align_branch_jmp:
15516 branch = "jmp";
15517 break;
15518 case align_branch_call:
15519 branch = "call";
15520 break;
15521 case align_branch_indirect:
15522 branch = "indiret branch";
15523 break;
15524 case align_branch_ret:
15525 branch = "ret";
15526 break;
15527 default:
15528 break;
15529 }
15530
15531 fprintf (stdout, msg,
15532 fragP->fr_file, fragP->fr_line, size, prefix,
15533 (long long) fragP->fr_address, branch,
15534 1 << align_branch_power);
15535 }
15536 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
15537 memset (fragP->fr_opcode,
15538 fragP->tc_frag_data.default_prefix, size);
15539 else
15540 i386_generate_nops (fragP, (char *) fragP->fr_opcode,
15541 size, 0);
15542 fragP->fr_fix += size;
15543 }
15544 return;
15545 }
15546
252b5132
RH
15547 opcode = (unsigned char *) fragP->fr_opcode;
15548
47926f60 15549 /* Address we want to reach in file space. */
252b5132 15550 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
252b5132 15551
47926f60 15552 /* Address opcode resides at in file space. */
252b5132
RH
15553 opcode_address = fragP->fr_address + fragP->fr_fix;
15554
47926f60 15555 /* Displacement from opcode start to fill into instruction. */
252b5132
RH
15556 displacement_from_opcode_start = target_address - opcode_address;
15557
fddf5b5b 15558 if ((fragP->fr_subtype & BIG) == 0)
252b5132 15559 {
47926f60
KH
15560 /* Don't have to change opcode. */
15561 extension = 1; /* 1 opcode + 1 displacement */
252b5132 15562 where_to_put_displacement = &opcode[1];
fddf5b5b
AM
15563 }
15564 else
15565 {
15566 if (no_cond_jump_promotion
15567 && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
4eed87de
AM
15568 as_warn_where (fragP->fr_file, fragP->fr_line,
15569 _("long jump required"));
252b5132 15570
fddf5b5b
AM
15571 switch (fragP->fr_subtype)
15572 {
15573 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
15574 extension = 4; /* 1 opcode + 4 displacement */
15575 opcode[0] = 0xe9;
15576 where_to_put_displacement = &opcode[1];
15577 break;
252b5132 15578
fddf5b5b
AM
15579 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
15580 extension = 2; /* 1 opcode + 2 displacement */
15581 opcode[0] = 0xe9;
15582 where_to_put_displacement = &opcode[1];
15583 break;
252b5132 15584
fddf5b5b
AM
15585 case ENCODE_RELAX_STATE (COND_JUMP, BIG):
15586 case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
15587 extension = 5; /* 2 opcode + 4 displacement */
15588 opcode[1] = opcode[0] + 0x10;
15589 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
15590 where_to_put_displacement = &opcode[2];
15591 break;
252b5132 15592
fddf5b5b
AM
15593 case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
15594 extension = 3; /* 2 opcode + 2 displacement */
15595 opcode[1] = opcode[0] + 0x10;
15596 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
15597 where_to_put_displacement = &opcode[2];
15598 break;
252b5132 15599
fddf5b5b
AM
15600 case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
15601 extension = 4;
15602 opcode[0] ^= 1;
15603 opcode[1] = 3;
15604 opcode[2] = 0xe9;
15605 where_to_put_displacement = &opcode[3];
15606 break;
15607
15608 default:
15609 BAD_CASE (fragP->fr_subtype);
15610 break;
15611 }
252b5132 15612 }
fddf5b5b 15613
7b81dfbb
AJ
15614 /* If size if less then four we are sure that the operand fits,
15615 but if it's 4, then it could be that the displacement is larger
15616 then -/+ 2GB. */
15617 if (DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype) == 4
15618 && object_64bit
15619 && ((addressT) (displacement_from_opcode_start - extension
4eed87de
AM
15620 + ((addressT) 1 << 31))
15621 > (((addressT) 2 << 31) - 1)))
7b81dfbb
AJ
15622 {
15623 as_bad_where (fragP->fr_file, fragP->fr_line,
15624 _("jump target out of range"));
15625 /* Make us emit 0. */
15626 displacement_from_opcode_start = extension;
15627 }
47926f60 15628 /* Now put displacement after opcode. */
252b5132
RH
15629 md_number_to_chars ((char *) where_to_put_displacement,
15630 (valueT) (displacement_from_opcode_start - extension),
fddf5b5b 15631 DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
252b5132
RH
15632 fragP->fr_fix += extension;
15633}
15634\f
7016a5d5 15635/* Apply a fixup (fixP) to segment data, once it has been determined
252b5132
RH
15636 by our caller that we have all the info we need to fix it up.
15637
7016a5d5
TG
15638 Parameter valP is the pointer to the value of the bits.
15639
252b5132
RH
15640 On the 386, immediates, displacements, and data pointers are all in
15641 the same (little-endian) format, so we don't need to care about which
15642 we are handling. */
15643
94f592af 15644void
7016a5d5 15645md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 15646{
94f592af 15647 char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
c6682705 15648 valueT value = *valP;
252b5132 15649
f86103b7 15650#if !defined (TE_Mach)
93382f6d
AM
15651 if (fixP->fx_pcrel)
15652 {
15653 switch (fixP->fx_r_type)
15654 {
5865bb77
ILT
15655 default:
15656 break;
15657
d6ab8113
JB
15658 case BFD_RELOC_64:
15659 fixP->fx_r_type = BFD_RELOC_64_PCREL;
15660 break;
93382f6d 15661 case BFD_RELOC_32:
ae8887b5 15662 case BFD_RELOC_X86_64_32S:
93382f6d
AM
15663 fixP->fx_r_type = BFD_RELOC_32_PCREL;
15664 break;
15665 case BFD_RELOC_16:
15666 fixP->fx_r_type = BFD_RELOC_16_PCREL;
15667 break;
15668 case BFD_RELOC_8:
15669 fixP->fx_r_type = BFD_RELOC_8_PCREL;
15670 break;
15671 }
15672 }
252b5132 15673
a161fe53 15674 if (fixP->fx_addsy != NULL
31312f95 15675 && (fixP->fx_r_type == BFD_RELOC_32_PCREL
d6ab8113 15676 || fixP->fx_r_type == BFD_RELOC_64_PCREL
31312f95 15677 || fixP->fx_r_type == BFD_RELOC_16_PCREL
d258b828 15678 || fixP->fx_r_type == BFD_RELOC_8_PCREL)
31312f95 15679 && !use_rela_relocations)
252b5132 15680 {
31312f95
AM
15681 /* This is a hack. There should be a better way to handle this.
15682 This covers for the fact that bfd_install_relocation will
15683 subtract the current location (for partial_inplace, PC relative
15684 relocations); see more below. */
252b5132 15685#ifndef OBJ_AOUT
718ddfc0 15686 if (IS_ELF
252b5132
RH
15687#ifdef TE_PE
15688 || OUTPUT_FLAVOR == bfd_target_coff_flavour
15689#endif
15690 )
15691 value += fixP->fx_where + fixP->fx_frag->fr_address;
15692#endif
15693#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 15694 if (IS_ELF)
252b5132 15695 {
6539b54b 15696 segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
2f66722d 15697
6539b54b 15698 if ((sym_seg == seg
2f66722d 15699 || (symbol_section_p (fixP->fx_addsy)
6539b54b 15700 && sym_seg != absolute_section))
af65af87 15701 && !generic_force_reloc (fixP))
2f66722d
AM
15702 {
15703 /* Yes, we add the values in twice. This is because
6539b54b
AM
15704 bfd_install_relocation subtracts them out again. I think
15705 bfd_install_relocation is broken, but I don't dare change
2f66722d
AM
15706 it. FIXME. */
15707 value += fixP->fx_where + fixP->fx_frag->fr_address;
15708 }
252b5132
RH
15709 }
15710#endif
15711#if defined (OBJ_COFF) && defined (TE_PE)
977cdf5a
NC
15712 /* For some reason, the PE format does not store a
15713 section address offset for a PC relative symbol. */
15714 if (S_GET_SEGMENT (fixP->fx_addsy) != seg
7be1c489 15715 || S_IS_WEAK (fixP->fx_addsy))
252b5132
RH
15716 value += md_pcrel_from (fixP);
15717#endif
15718 }
fbeb56a4 15719#if defined (OBJ_COFF) && defined (TE_PE)
f01c1a09
NC
15720 if (fixP->fx_addsy != NULL
15721 && S_IS_WEAK (fixP->fx_addsy)
15722 /* PR 16858: Do not modify weak function references. */
15723 && ! fixP->fx_pcrel)
fbeb56a4 15724 {
296a8689
NC
15725#if !defined (TE_PEP)
15726 /* For x86 PE weak function symbols are neither PC-relative
15727 nor do they set S_IS_FUNCTION. So the only reliable way
15728 to detect them is to check the flags of their containing
15729 section. */
15730 if (S_GET_SEGMENT (fixP->fx_addsy) != NULL
15731 && S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_CODE)
15732 ;
15733 else
15734#endif
fbeb56a4
DK
15735 value -= S_GET_VALUE (fixP->fx_addsy);
15736 }
15737#endif
252b5132
RH
15738
15739 /* Fix a few things - the dynamic linker expects certain values here,
0234cb7c 15740 and we must not disappoint it. */
252b5132 15741#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 15742 if (IS_ELF && fixP->fx_addsy)
47926f60
KH
15743 switch (fixP->fx_r_type)
15744 {
15745 case BFD_RELOC_386_PLT32:
3e73aa7c 15746 case BFD_RELOC_X86_64_PLT32:
b9519cfe
L
15747 /* Make the jump instruction point to the address of the operand.
15748 At runtime we merely add the offset to the actual PLT entry.
15749 NB: Subtract the offset size only for jump instructions. */
15750 if (fixP->fx_pcrel)
15751 value = -4;
47926f60 15752 break;
31312f95 15753
13ae64f3
JJ
15754 case BFD_RELOC_386_TLS_GD:
15755 case BFD_RELOC_386_TLS_LDM:
13ae64f3 15756 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
15757 case BFD_RELOC_386_TLS_IE:
15758 case BFD_RELOC_386_TLS_GOTIE:
67a4f2b7 15759 case BFD_RELOC_386_TLS_GOTDESC:
bffbf940
JJ
15760 case BFD_RELOC_X86_64_TLSGD:
15761 case BFD_RELOC_X86_64_TLSLD:
15762 case BFD_RELOC_X86_64_GOTTPOFF:
a533c8df 15763 case BFD_RELOC_X86_64_CODE_4_GOTTPOFF:
5bc71c2a 15764 case BFD_RELOC_X86_64_CODE_6_GOTTPOFF:
67a4f2b7 15765 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
a533c8df 15766 case BFD_RELOC_X86_64_CODE_4_GOTPC32_TLSDESC:
00f7efb6
JJ
15767 value = 0; /* Fully resolved at runtime. No addend. */
15768 /* Fallthrough */
15769 case BFD_RELOC_386_TLS_LE:
15770 case BFD_RELOC_386_TLS_LDO_32:
15771 case BFD_RELOC_386_TLS_LE_32:
15772 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 15773 case BFD_RELOC_X86_64_DTPOFF64:
00f7efb6 15774 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113 15775 case BFD_RELOC_X86_64_TPOFF64:
00f7efb6
JJ
15776 S_SET_THREAD_LOCAL (fixP->fx_addsy);
15777 break;
15778
67a4f2b7
AO
15779 case BFD_RELOC_386_TLS_DESC_CALL:
15780 case BFD_RELOC_X86_64_TLSDESC_CALL:
15781 value = 0; /* Fully resolved at runtime. No addend. */
15782 S_SET_THREAD_LOCAL (fixP->fx_addsy);
15783 fixP->fx_done = 0;
15784 return;
15785
47926f60
KH
15786 case BFD_RELOC_VTABLE_INHERIT:
15787 case BFD_RELOC_VTABLE_ENTRY:
15788 fixP->fx_done = 0;
94f592af 15789 return;
47926f60
KH
15790
15791 default:
15792 break;
15793 }
15794#endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) */
a442cac5
JB
15795
15796 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
15797 if (!object_64bit)
15798 value = extend_to_32bit_address (value);
15799
c6682705 15800 *valP = value;
f86103b7 15801#endif /* !defined (TE_Mach) */
3e73aa7c 15802
3e73aa7c 15803 /* Are we finished with this relocation now? */
c6682705 15804 if (fixP->fx_addsy == NULL)
b8188555
JB
15805 {
15806 fixP->fx_done = 1;
15807 switch (fixP->fx_r_type)
15808 {
15809 case BFD_RELOC_X86_64_32S:
15810 fixP->fx_signed = 1;
15811 break;
15812
15813 default:
15814 break;
15815 }
15816 }
fbeb56a4
DK
15817#if defined (OBJ_COFF) && defined (TE_PE)
15818 else if (fixP->fx_addsy != NULL && S_IS_WEAK (fixP->fx_addsy))
15819 {
15820 fixP->fx_done = 0;
15821 /* Remember value for tc_gen_reloc. */
15822 fixP->fx_addnumber = value;
15823 /* Clear out the frag for now. */
15824 value = 0;
15825 }
15826#endif
3e73aa7c
JH
15827 else if (use_rela_relocations)
15828 {
46fb6d5a
JB
15829 if (!disallow_64bit_reloc || fixP->fx_r_type == NO_RELOC)
15830 fixP->fx_no_overflow = 1;
062cd5e7
AS
15831 /* Remember value for tc_gen_reloc. */
15832 fixP->fx_addnumber = value;
3e73aa7c
JH
15833 value = 0;
15834 }
f86103b7 15835
94f592af 15836 md_number_to_chars (p, value, fixP->fx_size);
252b5132 15837}
252b5132 15838\f
6d4af3c2 15839const char *
499ac353 15840md_atof (int type, char *litP, int *sizeP)
252b5132 15841{
499ac353
NC
15842 /* This outputs the LITTLENUMs in REVERSE order;
15843 in accord with the bigendian 386. */
5b7c81bd 15844 return ieee_md_atof (type, litP, sizeP, false);
252b5132
RH
15845}
15846\f
2d545b82 15847static char output_invalid_buf[sizeof (unsigned char) * 2 + 6];
252b5132 15848
252b5132 15849static char *
e3bb37b5 15850output_invalid (int c)
252b5132 15851{
3882b010 15852 if (ISPRINT (c))
f9f21a03
L
15853 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
15854 "'%c'", c);
252b5132 15855 else
f9f21a03 15856 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
2d545b82 15857 "(0x%x)", (unsigned char) c);
252b5132
RH
15858 return output_invalid_buf;
15859}
15860
8a6fb3f9
JB
15861/* Verify that @r can be used in the current context. */
15862
5b7c81bd 15863static bool check_register (const reg_entry *r)
8a6fb3f9
JB
15864{
15865 if (allow_pseudo_reg)
5b7c81bd 15866 return true;
8a6fb3f9
JB
15867
15868 if (operand_type_all_zero (&r->reg_type))
5b7c81bd 15869 return false;
8a6fb3f9
JB
15870
15871 if ((r->reg_type.bitfield.dword
15872 || (r->reg_type.bitfield.class == SReg && r->reg_num > 3)
15873 || r->reg_type.bitfield.class == RegCR
22e00a3f 15874 || r->reg_type.bitfield.class == RegDR)
8a6fb3f9 15875 && !cpu_arch_flags.bitfield.cpui386)
5b7c81bd 15876 return false;
8a6fb3f9 15877
22e00a3f
JB
15878 if (r->reg_type.bitfield.class == RegTR
15879 && (flag_code == CODE_64BIT
15880 || !cpu_arch_flags.bitfield.cpui386
15881 || cpu_arch_isa_flags.bitfield.cpui586
15882 || cpu_arch_isa_flags.bitfield.cpui686))
5b7c81bd 15883 return false;
22e00a3f 15884
8a6fb3f9 15885 if (r->reg_type.bitfield.class == RegMMX && !cpu_arch_flags.bitfield.cpummx)
5b7c81bd 15886 return false;
8a6fb3f9
JB
15887
15888 if (!cpu_arch_flags.bitfield.cpuavx512f)
15889 {
15890 if (r->reg_type.bitfield.zmmword
15891 || r->reg_type.bitfield.class == RegMask)
5b7c81bd 15892 return false;
8a6fb3f9
JB
15893
15894 if (!cpu_arch_flags.bitfield.cpuavx)
15895 {
15896 if (r->reg_type.bitfield.ymmword)
5b7c81bd 15897 return false;
8a6fb3f9
JB
15898
15899 if (!cpu_arch_flags.bitfield.cpusse && r->reg_type.bitfield.xmmword)
5b7c81bd 15900 return false;
8a6fb3f9
JB
15901 }
15902 }
15903
a6f3add0
JB
15904 if (r->reg_type.bitfield.zmmword)
15905 {
15906 if (vector_size < VSZ512)
15907 return false;
15908
eb3f3841
JB
15909 switch (i.encoding)
15910 {
15911 case encoding_default:
15912 case encoding_egpr:
15913 i.encoding = encoding_evex512;
15914 break;
15915 case encoding_evex:
15916 case encoding_evex512:
15917 break;
15918 default:
15919 i.encoding = encoding_error;
15920 break;
15921 }
a6f3add0 15922 }
4fc85f37
JB
15923
15924 if (vector_size < VSZ256 && r->reg_type.bitfield.ymmword)
15925 return false;
15926
260cd341
LC
15927 if (r->reg_type.bitfield.tmmword
15928 && (!cpu_arch_flags.bitfield.cpuamx_tile
15929 || flag_code != CODE_64BIT))
5b7c81bd 15930 return false;
260cd341 15931
8a6fb3f9 15932 if (r->reg_type.bitfield.class == RegBND && !cpu_arch_flags.bitfield.cpumpx)
5b7c81bd 15933 return false;
8a6fb3f9
JB
15934
15935 /* Don't allow fake index register unless allow_index_reg isn't 0. */
15936 if (!allow_index_reg && r->reg_num == RegIZ)
5b7c81bd 15937 return false;
8a6fb3f9
JB
15938
15939 /* Upper 16 vector registers are only available with VREX in 64bit
15940 mode, and require EVEX encoding. */
15941 if (r->reg_flags & RegVRex)
15942 {
15943 if (!cpu_arch_flags.bitfield.cpuavx512f
15944 || flag_code != CODE_64BIT)
5b7c81bd 15945 return false;
8a6fb3f9 15946
eb3f3841
JB
15947 switch (i.encoding)
15948 {
15949 case encoding_default:
15950 case encoding_egpr:
15951 case encoding_evex512:
15952 i.encoding = encoding_evex;
15953 break;
15954 case encoding_evex:
15955 break;
15956 default:
15957 i.encoding = encoding_error;
15958 break;
15959 }
8a6fb3f9
JB
15960 }
15961
80d61d8d
CL
15962 if (r->reg_flags & RegRex2)
15963 {
15964 if (!cpu_arch_flags.bitfield.cpuapx_f
15965 || flag_code != CODE_64BIT)
15966 return false;
6177c84d 15967
eb3f3841
JB
15968 switch (i.encoding)
15969 {
15970 case encoding_default:
15971 i.encoding = encoding_egpr;
15972 break;
15973 case encoding_egpr:
15974 case encoding_evex:
15975 case encoding_evex512:
15976 break;
15977 default:
15978 i.encoding = encoding_error;
15979 break;
15980 }
80d61d8d
CL
15981 }
15982
8a6fb3f9 15983 if (((r->reg_flags & (RegRex64 | RegRex)) || r->reg_type.bitfield.qword)
da5f9eb4 15984 && (!cpu_arch_flags.bitfield.cpu64
0ff3b7d0
JB
15985 || r->reg_type.bitfield.class != RegCR
15986 || dot_insn ())
8a6fb3f9 15987 && flag_code != CODE_64BIT)
5b7c81bd 15988 return false;
8a6fb3f9
JB
15989
15990 if (r->reg_type.bitfield.class == SReg && r->reg_num == RegFlat
15991 && !intel_syntax)
5b7c81bd 15992 return false;
8a6fb3f9 15993
5b7c81bd 15994 return true;
8a6fb3f9
JB
15995}
15996
af6bdddf 15997/* REG_STRING starts *before* REGISTER_PREFIX. */
252b5132
RH
15998
15999static const reg_entry *
74e05e01 16000parse_real_register (const char *reg_string, char **end_op)
252b5132 16001{
74e05e01 16002 const char *s = reg_string;
af6bdddf 16003 char *p;
252b5132
RH
16004 char reg_name_given[MAX_REG_NAME_SIZE + 1];
16005 const reg_entry *r;
16006
16007 /* Skip possible REGISTER_PREFIX and possible whitespace. */
16008 if (*s == REGISTER_PREFIX)
16009 ++s;
16010
16011 if (is_space_char (*s))
16012 ++s;
16013
16014 p = reg_name_given;
af6bdddf 16015 while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
252b5132
RH
16016 {
16017 if (p >= reg_name_given + MAX_REG_NAME_SIZE)
af6bdddf
AM
16018 return (const reg_entry *) NULL;
16019 s++;
252b5132
RH
16020 }
16021
4795cd4a 16022 if (is_part_of_name (*s))
6588847e
DN
16023 return (const reg_entry *) NULL;
16024
74e05e01 16025 *end_op = (char *) s;
252b5132 16026
629310ab 16027 r = (const reg_entry *) str_hash_find (reg_hash, reg_name_given);
252b5132 16028
5f47d35b 16029 /* Handle floating point regs, allowing spaces in the (i) part. */
6288d05f 16030 if (r == reg_st0)
5f47d35b 16031 {
0e0eea78
JB
16032 if (!cpu_arch_flags.bitfield.cpu8087
16033 && !cpu_arch_flags.bitfield.cpu287
af32b722
JB
16034 && !cpu_arch_flags.bitfield.cpu387
16035 && !allow_pseudo_reg)
0e0eea78
JB
16036 return (const reg_entry *) NULL;
16037
5f47d35b
AM
16038 if (is_space_char (*s))
16039 ++s;
16040 if (*s == '(')
16041 {
af6bdddf 16042 ++s;
5f47d35b
AM
16043 if (is_space_char (*s))
16044 ++s;
16045 if (*s >= '0' && *s <= '7')
16046 {
db557034 16047 int fpr = *s - '0';
af6bdddf 16048 ++s;
5f47d35b
AM
16049 if (is_space_char (*s))
16050 ++s;
16051 if (*s == ')')
16052 {
74e05e01 16053 *end_op = (char *) s + 1;
6288d05f 16054 know (r[fpr].reg_num == fpr);
db557034 16055 return r + fpr;
5f47d35b 16056 }
5f47d35b 16057 }
47926f60 16058 /* We have "%st(" then garbage. */
5f47d35b
AM
16059 return (const reg_entry *) NULL;
16060 }
16061 }
16062
8a6fb3f9 16063 return r && check_register (r) ? r : NULL;
252b5132 16064}
4d1bb795
JB
16065
16066/* REG_STRING starts *before* REGISTER_PREFIX. */
16067
16068static const reg_entry *
4f081312 16069parse_register (const char *reg_string, char **end_op)
4d1bb795
JB
16070{
16071 const reg_entry *r;
16072
16073 if (*reg_string == REGISTER_PREFIX || allow_naked_reg)
16074 r = parse_real_register (reg_string, end_op);
16075 else
16076 r = NULL;
16077 if (!r)
16078 {
16079 char *save = input_line_pointer;
4f081312 16080 char *buf = xstrdup (reg_string), *name;
4d1bb795
JB
16081 symbolS *symbolP;
16082
4f081312
JB
16083 input_line_pointer = buf;
16084 get_symbol_name (&name);
16085 symbolP = symbol_find (name);
d50c498a 16086 while (symbolP && symbol_equated_p (symbolP))
64d23078
JB
16087 {
16088 const expressionS *e = symbol_get_value_expression(symbolP);
16089
d50c498a 16090 if (e->X_add_number)
64d23078
JB
16091 break;
16092 symbolP = e->X_add_symbol;
16093 }
4d1bb795
JB
16094 if (symbolP && S_GET_SEGMENT (symbolP) == reg_section)
16095 {
16096 const expressionS *e = symbol_get_value_expression (symbolP);
16097
0ccade1a
JB
16098 if (e->X_op == O_register)
16099 {
16100 know (e->X_add_number >= 0
16101 && (valueT) e->X_add_number < i386_regtab_size);
16102 r = i386_regtab + e->X_add_number;
4f081312 16103 *end_op = (char *) reg_string + (input_line_pointer - buf);
0ccade1a
JB
16104 }
16105 if (r && !check_register (r))
8a6fb3f9 16106 {
3b55a1d0
JB
16107 as_bad (_("register '%s%s' cannot be used here"),
16108 register_prefix, r->reg_name);
16109 r = &bad_reg;
8a6fb3f9 16110 }
4d1bb795 16111 }
4d1bb795 16112 input_line_pointer = save;
4f081312 16113 free (buf);
4d1bb795
JB
16114 }
16115 return r;
16116}
16117
16118int
16119i386_parse_name (char *name, expressionS *e, char *nextcharP)
16120{
4faaa10f 16121 const reg_entry *r = NULL;
4d1bb795
JB
16122 char *end = input_line_pointer;
16123
6acf9130
JB
16124 /* We only know the terminating character here. It being double quote could
16125 be the closing one of a quoted symbol name, or an opening one from a
16126 following string (or another quoted symbol name). Since the latter can't
16127 be valid syntax for anything, bailing in either case is good enough. */
16128 if (*nextcharP == '"')
16129 return 0;
16130
4d1bb795 16131 *end = *nextcharP;
4faaa10f
JB
16132 if (*name == REGISTER_PREFIX || allow_naked_reg)
16133 r = parse_real_register (name, &input_line_pointer);
4d1bb795
JB
16134 if (r && end <= input_line_pointer)
16135 {
16136 *nextcharP = *input_line_pointer;
16137 *input_line_pointer = 0;
f847749a
JB
16138 e->X_op = O_register;
16139 e->X_add_number = r - i386_regtab;
4d1bb795
JB
16140 return 1;
16141 }
16142 input_line_pointer = end;
16143 *end = 0;
ee86248c 16144 return intel_syntax ? i386_intel_parse_name (name, e) : 0;
4d1bb795
JB
16145}
16146
16147void
16148md_operand (expressionS *e)
16149{
ee86248c
JB
16150 char *end;
16151 const reg_entry *r;
4d1bb795 16152
ee86248c
JB
16153 switch (*input_line_pointer)
16154 {
16155 case REGISTER_PREFIX:
16156 r = parse_real_register (input_line_pointer, &end);
4d1bb795
JB
16157 if (r)
16158 {
16159 e->X_op = O_register;
16160 e->X_add_number = r - i386_regtab;
16161 input_line_pointer = end;
16162 }
ee86248c
JB
16163 break;
16164
16165 case '[':
9c2799c2 16166 gas_assert (intel_syntax);
ee86248c
JB
16167 end = input_line_pointer++;
16168 expression (e);
16169 if (*input_line_pointer == ']')
16170 {
16171 ++input_line_pointer;
16172 e->X_op_symbol = make_expr_symbol (e);
16173 e->X_add_symbol = NULL;
16174 e->X_add_number = 0;
16175 e->X_op = O_index;
16176 }
16177 else
16178 {
16179 e->X_op = O_absent;
16180 input_line_pointer = end;
16181 }
16182 break;
4d1bb795
JB
16183 }
16184}
16185
5cc00775
JB
16186#ifdef BFD64
16187/* To maintain consistency with !BFD64 builds of gas record, whether any
16188 (binary) operator was involved in an expression. As expressions are
16189 evaluated in only 32 bits when !BFD64, we use this to decide whether to
16190 truncate results. */
16191bool i386_record_operator (operatorT op,
16192 const expressionS *left,
16193 const expressionS *right)
16194{
16195 if (op == O_absent)
16196 return false;
16197
16198 if (!left)
16199 {
16200 /* Since the expression parser applies unary operators fine to bignum
16201 operands, we don't need to be concerned of respective operands not
16202 fitting in 32 bits. */
16203 if (right->X_op == O_constant && right->X_unsigned
16204 && !fits_in_unsigned_long (right->X_add_number))
16205 return false;
16206 }
16207 /* This isn't entirely right: The pattern can also result when constant
16208 expressions are folded (e.g. 0xffffffff + 1). */
16209 else if ((left->X_op == O_constant && left->X_unsigned
16210 && !fits_in_unsigned_long (left->X_add_number))
16211 || (right->X_op == O_constant && right->X_unsigned
16212 && !fits_in_unsigned_long (right->X_add_number)))
16213 expr_mode = expr_large_value;
16214
16215 if (expr_mode != expr_large_value)
16216 expr_mode = expr_operator_present;
16217
16218 return false;
16219}
16220#endif
252b5132 16221\f
4cc782b5 16222#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
b6f8c7c4 16223const char *md_shortopts = "kVQ:sqnO::";
252b5132 16224#else
b6f8c7c4 16225const char *md_shortopts = "qnO::";
252b5132 16226#endif
6e0b89ee 16227
3e73aa7c 16228#define OPTION_32 (OPTION_MD_BASE + 0)
b3b91714
AM
16229#define OPTION_64 (OPTION_MD_BASE + 1)
16230#define OPTION_DIVIDE (OPTION_MD_BASE + 2)
9103f4f4
L
16231#define OPTION_MARCH (OPTION_MD_BASE + 3)
16232#define OPTION_MTUNE (OPTION_MD_BASE + 4)
1efbbeb4
L
16233#define OPTION_MMNEMONIC (OPTION_MD_BASE + 5)
16234#define OPTION_MSYNTAX (OPTION_MD_BASE + 6)
16235#define OPTION_MINDEX_REG (OPTION_MD_BASE + 7)
16236#define OPTION_MNAKED_REG (OPTION_MD_BASE + 8)
bd5dea88 16237#define OPTION_MRELAX_RELOCATIONS (OPTION_MD_BASE + 9)
c0f3af97 16238#define OPTION_MSSE2AVX (OPTION_MD_BASE + 10)
daf50ae7 16239#define OPTION_MSSE_CHECK (OPTION_MD_BASE + 11)
7bab8ab5
JB
16240#define OPTION_MOPERAND_CHECK (OPTION_MD_BASE + 12)
16241#define OPTION_MAVXSCALAR (OPTION_MD_BASE + 13)
16242#define OPTION_X32 (OPTION_MD_BASE + 14)
7e8b059b 16243#define OPTION_MADD_BND_PREFIX (OPTION_MD_BASE + 15)
43234a1e
L
16244#define OPTION_MEVEXLIG (OPTION_MD_BASE + 16)
16245#define OPTION_MEVEXWIG (OPTION_MD_BASE + 17)
167ad85b 16246#define OPTION_MBIG_OBJ (OPTION_MD_BASE + 18)
d1982f93 16247#define OPTION_MOMIT_LOCK_PREFIX (OPTION_MD_BASE + 19)
d3d3c6db 16248#define OPTION_MEVEXRCIG (OPTION_MD_BASE + 20)
8dcea932 16249#define OPTION_MSHARED (OPTION_MD_BASE + 21)
5db04b09
L
16250#define OPTION_MAMD64 (OPTION_MD_BASE + 22)
16251#define OPTION_MINTEL64 (OPTION_MD_BASE + 23)
e4e00185 16252#define OPTION_MFENCE_AS_LOCK_ADD (OPTION_MD_BASE + 24)
b4a3a7b4 16253#define OPTION_X86_USED_NOTE (OPTION_MD_BASE + 25)
03751133 16254#define OPTION_MVEXWIG (OPTION_MD_BASE + 26)
e379e5f3
L
16255#define OPTION_MALIGN_BRANCH_BOUNDARY (OPTION_MD_BASE + 27)
16256#define OPTION_MALIGN_BRANCH_PREFIX_SIZE (OPTION_MD_BASE + 28)
16257#define OPTION_MALIGN_BRANCH (OPTION_MD_BASE + 29)
76cf450b 16258#define OPTION_MBRANCHES_WITH_32B_BOUNDARIES (OPTION_MD_BASE + 30)
ae531041
L
16259#define OPTION_MLFENCE_AFTER_LOAD (OPTION_MD_BASE + 31)
16260#define OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH (OPTION_MD_BASE + 32)
16261#define OPTION_MLFENCE_BEFORE_RET (OPTION_MD_BASE + 33)
c8480b58 16262#define OPTION_MUSE_UNALIGNED_VECTOR_MOVE (OPTION_MD_BASE + 34)
b3b91714 16263
99ad8390
NC
16264struct option md_longopts[] =
16265{
3e73aa7c 16266 {"32", no_argument, NULL, OPTION_32},
321098a5 16267#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 16268 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c 16269 {"64", no_argument, NULL, OPTION_64},
351f65ca
L
16270#endif
16271#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 16272 {"x32", no_argument, NULL, OPTION_X32},
8dcea932 16273 {"mshared", no_argument, NULL, OPTION_MSHARED},
b4a3a7b4 16274 {"mx86-used-note", required_argument, NULL, OPTION_X86_USED_NOTE},
6e0b89ee 16275#endif
b3b91714 16276 {"divide", no_argument, NULL, OPTION_DIVIDE},
9103f4f4
L
16277 {"march", required_argument, NULL, OPTION_MARCH},
16278 {"mtune", required_argument, NULL, OPTION_MTUNE},
1efbbeb4
L
16279 {"mmnemonic", required_argument, NULL, OPTION_MMNEMONIC},
16280 {"msyntax", required_argument, NULL, OPTION_MSYNTAX},
16281 {"mindex-reg", no_argument, NULL, OPTION_MINDEX_REG},
16282 {"mnaked-reg", no_argument, NULL, OPTION_MNAKED_REG},
c0f3af97 16283 {"msse2avx", no_argument, NULL, OPTION_MSSE2AVX},
c8480b58 16284 {"muse-unaligned-vector-move", no_argument, NULL, OPTION_MUSE_UNALIGNED_VECTOR_MOVE},
daf50ae7 16285 {"msse-check", required_argument, NULL, OPTION_MSSE_CHECK},
7bab8ab5 16286 {"moperand-check", required_argument, NULL, OPTION_MOPERAND_CHECK},
539f890d 16287 {"mavxscalar", required_argument, NULL, OPTION_MAVXSCALAR},
03751133 16288 {"mvexwig", required_argument, NULL, OPTION_MVEXWIG},
7e8b059b 16289 {"madd-bnd-prefix", no_argument, NULL, OPTION_MADD_BND_PREFIX},
43234a1e
L
16290 {"mevexlig", required_argument, NULL, OPTION_MEVEXLIG},
16291 {"mevexwig", required_argument, NULL, OPTION_MEVEXWIG},
167ad85b
TG
16292# if defined (TE_PE) || defined (TE_PEP)
16293 {"mbig-obj", no_argument, NULL, OPTION_MBIG_OBJ},
16294#endif
d1982f93 16295 {"momit-lock-prefix", required_argument, NULL, OPTION_MOMIT_LOCK_PREFIX},
e4e00185 16296 {"mfence-as-lock-add", required_argument, NULL, OPTION_MFENCE_AS_LOCK_ADD},
0cb4071e 16297 {"mrelax-relocations", required_argument, NULL, OPTION_MRELAX_RELOCATIONS},
d3d3c6db 16298 {"mevexrcig", required_argument, NULL, OPTION_MEVEXRCIG},
e379e5f3
L
16299 {"malign-branch-boundary", required_argument, NULL, OPTION_MALIGN_BRANCH_BOUNDARY},
16300 {"malign-branch-prefix-size", required_argument, NULL, OPTION_MALIGN_BRANCH_PREFIX_SIZE},
16301 {"malign-branch", required_argument, NULL, OPTION_MALIGN_BRANCH},
76cf450b 16302 {"mbranches-within-32B-boundaries", no_argument, NULL, OPTION_MBRANCHES_WITH_32B_BOUNDARIES},
ae531041
L
16303 {"mlfence-after-load", required_argument, NULL, OPTION_MLFENCE_AFTER_LOAD},
16304 {"mlfence-before-indirect-branch", required_argument, NULL,
16305 OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH},
16306 {"mlfence-before-ret", required_argument, NULL, OPTION_MLFENCE_BEFORE_RET},
5db04b09
L
16307 {"mamd64", no_argument, NULL, OPTION_MAMD64},
16308 {"mintel64", no_argument, NULL, OPTION_MINTEL64},
252b5132
RH
16309 {NULL, no_argument, NULL, 0}
16310};
16311size_t md_longopts_size = sizeof (md_longopts);
16312
16313int
17b9d67d 16314md_parse_option (int c, const char *arg)
252b5132 16315{
91d6fa6a 16316 unsigned int j;
e379e5f3 16317 char *arch, *next, *saved, *type;
9103f4f4 16318
252b5132
RH
16319 switch (c)
16320 {
12b55ccc
L
16321 case 'n':
16322 optimize_align_code = 0;
16323 break;
16324
a38cf1db
AM
16325 case 'q':
16326 quiet_warnings = 1;
252b5132
RH
16327 break;
16328
16329#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
a38cf1db
AM
16330 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
16331 should be emitted or not. FIXME: Not implemented. */
16332 case 'Q':
d4693039
JB
16333 if ((arg[0] != 'y' && arg[0] != 'n') || arg[1])
16334 return 0;
252b5132
RH
16335 break;
16336
16337 /* -V: SVR4 argument to print version ID. */
16338 case 'V':
16339 print_version_id ();
16340 break;
16341
a38cf1db
AM
16342 /* -k: Ignore for FreeBSD compatibility. */
16343 case 'k':
252b5132 16344 break;
4cc782b5
ILT
16345
16346 case 's':
16347 /* -s: On i386 Solaris, this tells the native assembler to use
29b0f896 16348 .stab instead of .stab.excl. We always use .stab anyhow. */
4cc782b5 16349 break;
8dcea932
L
16350
16351 case OPTION_MSHARED:
16352 shared = 1;
16353 break;
b4a3a7b4
L
16354
16355 case OPTION_X86_USED_NOTE:
16356 if (strcasecmp (arg, "yes") == 0)
16357 x86_used_note = 1;
16358 else if (strcasecmp (arg, "no") == 0)
16359 x86_used_note = 0;
16360 else
16361 as_fatal (_("invalid -mx86-used-note= option: `%s'"), arg);
16362 break;
16363
16364
99ad8390 16365#endif
321098a5 16366#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 16367 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c
JH
16368 case OPTION_64:
16369 {
16370 const char **list, **l;
16371
3e73aa7c
JH
16372 list = bfd_target_list ();
16373 for (l = list; *l != NULL; l++)
08dedd66 16374 if (startswith (*l, "elf64-x86-64")
99ad8390
NC
16375 || strcmp (*l, "coff-x86-64") == 0
16376 || strcmp (*l, "pe-x86-64") == 0
d382c579
TG
16377 || strcmp (*l, "pei-x86-64") == 0
16378 || strcmp (*l, "mach-o-x86-64") == 0)
6e0b89ee
AM
16379 {
16380 default_arch = "x86_64";
16381 break;
16382 }
3e73aa7c 16383 if (*l == NULL)
2b5d6a91 16384 as_fatal (_("no compiled in support for x86_64"));
3e73aa7c
JH
16385 free (list);
16386 }
16387 break;
16388#endif
252b5132 16389
351f65ca 16390#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 16391 case OPTION_X32:
351f65ca
L
16392 if (IS_ELF)
16393 {
16394 const char **list, **l;
16395
16396 list = bfd_target_list ();
16397 for (l = list; *l != NULL; l++)
08dedd66 16398 if (startswith (*l, "elf32-x86-64"))
351f65ca
L
16399 {
16400 default_arch = "x86_64:32";
16401 break;
16402 }
16403 if (*l == NULL)
2b5d6a91 16404 as_fatal (_("no compiled in support for 32bit x86_64"));
351f65ca
L
16405 free (list);
16406 }
16407 else
16408 as_fatal (_("32bit x86_64 is only supported for ELF"));
16409 break;
16410#endif
16411
6e0b89ee 16412 case OPTION_32:
590a0fed
JB
16413 {
16414 const char **list, **l;
16415
16416 list = bfd_target_list ();
16417 for (l = list; *l != NULL; l++)
16418 if (strstr (*l, "-i386")
16419 || strstr (*l, "-go32"))
16420 {
16421 default_arch = "i386";
16422 break;
16423 }
16424 if (*l == NULL)
16425 as_fatal (_("no compiled in support for ix86"));
16426 free (list);
16427 }
6e0b89ee
AM
16428 break;
16429
b3b91714
AM
16430 case OPTION_DIVIDE:
16431#ifdef SVR4_COMMENT_CHARS
16432 {
16433 char *n, *t;
16434 const char *s;
16435
add39d23 16436 n = XNEWVEC (char, strlen (i386_comment_chars) + 1);
b3b91714
AM
16437 t = n;
16438 for (s = i386_comment_chars; *s != '\0'; s++)
16439 if (*s != '/')
16440 *t++ = *s;
16441 *t = '\0';
16442 i386_comment_chars = n;
16443 }
16444#endif
16445 break;
16446
9103f4f4 16447 case OPTION_MARCH:
293f5f65
L
16448 saved = xstrdup (arg);
16449 arch = saved;
16450 /* Allow -march=+nosse. */
16451 if (*arch == '+')
16452 arch++;
6305a203 16453 do
9103f4f4 16454 {
4fc85f37
JB
16455 char *vsz;
16456
6305a203 16457 if (*arch == '.')
2b5d6a91 16458 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
16459 next = strchr (arch, '+');
16460 if (next)
16461 *next++ = '\0';
4fc85f37
JB
16462 vsz = strchr (arch, '/');
16463 if (vsz)
16464 *vsz++ = '\0';
91d6fa6a 16465 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 16466 {
4fc85f37
JB
16467 if (vsz && cpu_arch[j].vsz != vsz_set)
16468 continue;
16469
ae89daec
JB
16470 if (arch == saved && cpu_arch[j].type != PROCESSOR_NONE
16471 && strcmp (arch, cpu_arch[j].name) == 0)
ccc9c027 16472 {
6305a203 16473 /* Processor. */
ae89daec 16474 if (! cpu_arch[j].enable.bitfield.cpui386)
1ded5609
JB
16475 continue;
16476
91d6fa6a 16477 cpu_arch_name = cpu_arch[j].name;
d92c7521 16478 free (cpu_sub_arch_name);
6305a203 16479 cpu_sub_arch_name = NULL;
ae89daec 16480 cpu_arch_flags = cpu_arch[j].enable;
91d6fa6a 16481 cpu_arch_isa = cpu_arch[j].type;
ae89daec 16482 cpu_arch_isa_flags = cpu_arch[j].enable;
6305a203 16483 if (!cpu_arch_tune_set)
fb263707 16484 cpu_arch_tune = cpu_arch_isa;
4fc85f37 16485 vector_size = VSZ_DEFAULT;
6305a203
L
16486 break;
16487 }
ae89daec
JB
16488 else if (cpu_arch[j].type == PROCESSOR_NONE
16489 && strcmp (arch, cpu_arch[j].name) == 0
16490 && !cpu_flags_all_zero (&cpu_arch[j].enable))
6305a203 16491 {
33eaf5de 16492 /* ISA extension. */
bd483d21 16493 isa_enable (j);
4fc85f37
JB
16494
16495 switch (cpu_arch[j].vsz)
16496 {
16497 default:
16498 break;
16499
16500 case vsz_set:
16501 if (vsz)
16502 {
16503 char *end;
16504 unsigned long val = strtoul (vsz, &end, 0);
16505
16506 if (*end)
16507 val = 0;
16508 switch (val)
16509 {
16510 case 512: vector_size = VSZ512; break;
16511 case 256: vector_size = VSZ256; break;
16512 case 128: vector_size = VSZ128; break;
16513 default:
16514 as_warn (_("Unrecognized vector size specifier ignored"));
16515 break;
16516 }
16517 break;
16518 }
16519 /* Fall through. */
16520 case vsz_reset:
16521 vector_size = VSZ_DEFAULT;
16522 break;
16523 }
16524
6305a203 16525 break;
ccc9c027 16526 }
9103f4f4 16527 }
6305a203 16528
ae89daec 16529 if (j >= ARRAY_SIZE (cpu_arch) && startswith (arch, "no"))
293f5f65 16530 {
33eaf5de 16531 /* Disable an ISA extension. */
ae89daec
JB
16532 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
16533 if (cpu_arch[j].type == PROCESSOR_NONE
16534 && strcmp (arch + 2, cpu_arch[j].name) == 0)
293f5f65 16535 {
bd483d21 16536 isa_disable (j);
4fc85f37
JB
16537 if (cpu_arch[j].vsz == vsz_set)
16538 vector_size = VSZ_DEFAULT;
293f5f65
L
16539 break;
16540 }
293f5f65
L
16541 }
16542
91d6fa6a 16543 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 16544 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
16545
16546 arch = next;
9103f4f4 16547 }
293f5f65
L
16548 while (next != NULL);
16549 free (saved);
9103f4f4
L
16550 break;
16551
16552 case OPTION_MTUNE:
16553 if (*arg == '.')
2b5d6a91 16554 as_fatal (_("invalid -mtune= option: `%s'"), arg);
91d6fa6a 16555 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 16556 {
ae89daec
JB
16557 if (cpu_arch[j].type != PROCESSOR_NONE
16558 && strcmp (arg, cpu_arch[j].name) == 0)
9103f4f4 16559 {
ccc9c027 16560 cpu_arch_tune_set = 1;
91d6fa6a 16561 cpu_arch_tune = cpu_arch [j].type;
9103f4f4
L
16562 break;
16563 }
16564 }
91d6fa6a 16565 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 16566 as_fatal (_("invalid -mtune= option: `%s'"), arg);
9103f4f4
L
16567 break;
16568
1efbbeb4
L
16569 case OPTION_MMNEMONIC:
16570 if (strcasecmp (arg, "att") == 0)
16571 intel_mnemonic = 0;
16572 else if (strcasecmp (arg, "intel") == 0)
16573 intel_mnemonic = 1;
16574 else
2b5d6a91 16575 as_fatal (_("invalid -mmnemonic= option: `%s'"), arg);
1efbbeb4
L
16576 break;
16577
16578 case OPTION_MSYNTAX:
16579 if (strcasecmp (arg, "att") == 0)
213f15cf 16580 _set_intel_syntax (0);
1efbbeb4 16581 else if (strcasecmp (arg, "intel") == 0)
213f15cf 16582 _set_intel_syntax (1);
1efbbeb4 16583 else
2b5d6a91 16584 as_fatal (_("invalid -msyntax= option: `%s'"), arg);
1efbbeb4
L
16585 break;
16586
16587 case OPTION_MINDEX_REG:
16588 allow_index_reg = 1;
16589 break;
16590
16591 case OPTION_MNAKED_REG:
16592 allow_naked_reg = 1;
213f15cf 16593 register_prefix = "";
1efbbeb4
L
16594 break;
16595
c0f3af97
L
16596 case OPTION_MSSE2AVX:
16597 sse2avx = 1;
16598 break;
16599
c8480b58
L
16600 case OPTION_MUSE_UNALIGNED_VECTOR_MOVE:
16601 use_unaligned_vector_move = 1;
16602 break;
16603
daf50ae7
L
16604 case OPTION_MSSE_CHECK:
16605 if (strcasecmp (arg, "error") == 0)
7bab8ab5 16606 sse_check = check_error;
daf50ae7 16607 else if (strcasecmp (arg, "warning") == 0)
7bab8ab5 16608 sse_check = check_warning;
daf50ae7 16609 else if (strcasecmp (arg, "none") == 0)
7bab8ab5 16610 sse_check = check_none;
daf50ae7 16611 else
2b5d6a91 16612 as_fatal (_("invalid -msse-check= option: `%s'"), arg);
daf50ae7
L
16613 break;
16614
7bab8ab5
JB
16615 case OPTION_MOPERAND_CHECK:
16616 if (strcasecmp (arg, "error") == 0)
16617 operand_check = check_error;
16618 else if (strcasecmp (arg, "warning") == 0)
16619 operand_check = check_warning;
16620 else if (strcasecmp (arg, "none") == 0)
16621 operand_check = check_none;
16622 else
16623 as_fatal (_("invalid -moperand-check= option: `%s'"), arg);
16624 break;
16625
539f890d
L
16626 case OPTION_MAVXSCALAR:
16627 if (strcasecmp (arg, "128") == 0)
16628 avxscalar = vex128;
16629 else if (strcasecmp (arg, "256") == 0)
16630 avxscalar = vex256;
16631 else
2b5d6a91 16632 as_fatal (_("invalid -mavxscalar= option: `%s'"), arg);
539f890d
L
16633 break;
16634
03751133
L
16635 case OPTION_MVEXWIG:
16636 if (strcmp (arg, "0") == 0)
40c9c8de 16637 vexwig = vexw0;
03751133 16638 else if (strcmp (arg, "1") == 0)
40c9c8de 16639 vexwig = vexw1;
03751133
L
16640 else
16641 as_fatal (_("invalid -mvexwig= option: `%s'"), arg);
16642 break;
16643
7e8b059b
L
16644 case OPTION_MADD_BND_PREFIX:
16645 add_bnd_prefix = 1;
16646 break;
16647
43234a1e
L
16648 case OPTION_MEVEXLIG:
16649 if (strcmp (arg, "128") == 0)
16650 evexlig = evexl128;
16651 else if (strcmp (arg, "256") == 0)
16652 evexlig = evexl256;
16653 else if (strcmp (arg, "512") == 0)
16654 evexlig = evexl512;
16655 else
16656 as_fatal (_("invalid -mevexlig= option: `%s'"), arg);
16657 break;
16658
d3d3c6db
IT
16659 case OPTION_MEVEXRCIG:
16660 if (strcmp (arg, "rne") == 0)
16661 evexrcig = rne;
16662 else if (strcmp (arg, "rd") == 0)
16663 evexrcig = rd;
16664 else if (strcmp (arg, "ru") == 0)
16665 evexrcig = ru;
16666 else if (strcmp (arg, "rz") == 0)
16667 evexrcig = rz;
16668 else
16669 as_fatal (_("invalid -mevexrcig= option: `%s'"), arg);
16670 break;
16671
43234a1e
L
16672 case OPTION_MEVEXWIG:
16673 if (strcmp (arg, "0") == 0)
16674 evexwig = evexw0;
16675 else if (strcmp (arg, "1") == 0)
16676 evexwig = evexw1;
16677 else
16678 as_fatal (_("invalid -mevexwig= option: `%s'"), arg);
16679 break;
16680
167ad85b
TG
16681# if defined (TE_PE) || defined (TE_PEP)
16682 case OPTION_MBIG_OBJ:
16683 use_big_obj = 1;
16684 break;
16685#endif
16686
d1982f93 16687 case OPTION_MOMIT_LOCK_PREFIX:
d022bddd
IT
16688 if (strcasecmp (arg, "yes") == 0)
16689 omit_lock_prefix = 1;
16690 else if (strcasecmp (arg, "no") == 0)
16691 omit_lock_prefix = 0;
16692 else
16693 as_fatal (_("invalid -momit-lock-prefix= option: `%s'"), arg);
16694 break;
16695
e4e00185
AS
16696 case OPTION_MFENCE_AS_LOCK_ADD:
16697 if (strcasecmp (arg, "yes") == 0)
16698 avoid_fence = 1;
16699 else if (strcasecmp (arg, "no") == 0)
16700 avoid_fence = 0;
16701 else
16702 as_fatal (_("invalid -mfence-as-lock-add= option: `%s'"), arg);
16703 break;
16704
ae531041
L
16705 case OPTION_MLFENCE_AFTER_LOAD:
16706 if (strcasecmp (arg, "yes") == 0)
16707 lfence_after_load = 1;
16708 else if (strcasecmp (arg, "no") == 0)
16709 lfence_after_load = 0;
16710 else
16711 as_fatal (_("invalid -mlfence-after-load= option: `%s'"), arg);
16712 break;
16713
16714 case OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH:
16715 if (strcasecmp (arg, "all") == 0)
a09f656b 16716 {
16717 lfence_before_indirect_branch = lfence_branch_all;
16718 if (lfence_before_ret == lfence_before_ret_none)
16719 lfence_before_ret = lfence_before_ret_shl;
16720 }
ae531041
L
16721 else if (strcasecmp (arg, "memory") == 0)
16722 lfence_before_indirect_branch = lfence_branch_memory;
16723 else if (strcasecmp (arg, "register") == 0)
16724 lfence_before_indirect_branch = lfence_branch_register;
16725 else if (strcasecmp (arg, "none") == 0)
16726 lfence_before_indirect_branch = lfence_branch_none;
16727 else
16728 as_fatal (_("invalid -mlfence-before-indirect-branch= option: `%s'"),
16729 arg);
16730 break;
16731
16732 case OPTION_MLFENCE_BEFORE_RET:
16733 if (strcasecmp (arg, "or") == 0)
16734 lfence_before_ret = lfence_before_ret_or;
16735 else if (strcasecmp (arg, "not") == 0)
16736 lfence_before_ret = lfence_before_ret_not;
a09f656b 16737 else if (strcasecmp (arg, "shl") == 0 || strcasecmp (arg, "yes") == 0)
16738 lfence_before_ret = lfence_before_ret_shl;
ae531041
L
16739 else if (strcasecmp (arg, "none") == 0)
16740 lfence_before_ret = lfence_before_ret_none;
16741 else
16742 as_fatal (_("invalid -mlfence-before-ret= option: `%s'"),
16743 arg);
16744 break;
16745
0cb4071e
L
16746 case OPTION_MRELAX_RELOCATIONS:
16747 if (strcasecmp (arg, "yes") == 0)
16748 generate_relax_relocations = 1;
16749 else if (strcasecmp (arg, "no") == 0)
16750 generate_relax_relocations = 0;
16751 else
16752 as_fatal (_("invalid -mrelax-relocations= option: `%s'"), arg);
16753 break;
16754
e379e5f3
L
16755 case OPTION_MALIGN_BRANCH_BOUNDARY:
16756 {
16757 char *end;
16758 long int align = strtoul (arg, &end, 0);
16759 if (*end == '\0')
16760 {
16761 if (align == 0)
16762 {
16763 align_branch_power = 0;
16764 break;
16765 }
16766 else if (align >= 16)
16767 {
16768 int align_power;
16769 for (align_power = 0;
16770 (align & 1) == 0;
16771 align >>= 1, align_power++)
16772 continue;
16773 /* Limit alignment power to 31. */
16774 if (align == 1 && align_power < 32)
16775 {
16776 align_branch_power = align_power;
16777 break;
16778 }
16779 }
16780 }
16781 as_fatal (_("invalid -malign-branch-boundary= value: %s"), arg);
16782 }
16783 break;
16784
16785 case OPTION_MALIGN_BRANCH_PREFIX_SIZE:
16786 {
16787 char *end;
16788 int align = strtoul (arg, &end, 0);
16789 /* Some processors only support 5 prefixes. */
16790 if (*end == '\0' && align >= 0 && align < 6)
16791 {
16792 align_branch_prefix_size = align;
16793 break;
16794 }
16795 as_fatal (_("invalid -malign-branch-prefix-size= value: %s"),
16796 arg);
16797 }
16798 break;
16799
16800 case OPTION_MALIGN_BRANCH:
16801 align_branch = 0;
16802 saved = xstrdup (arg);
16803 type = saved;
16804 do
16805 {
16806 next = strchr (type, '+');
16807 if (next)
16808 *next++ = '\0';
16809 if (strcasecmp (type, "jcc") == 0)
16810 align_branch |= align_branch_jcc_bit;
16811 else if (strcasecmp (type, "fused") == 0)
16812 align_branch |= align_branch_fused_bit;
16813 else if (strcasecmp (type, "jmp") == 0)
16814 align_branch |= align_branch_jmp_bit;
16815 else if (strcasecmp (type, "call") == 0)
16816 align_branch |= align_branch_call_bit;
16817 else if (strcasecmp (type, "ret") == 0)
16818 align_branch |= align_branch_ret_bit;
16819 else if (strcasecmp (type, "indirect") == 0)
16820 align_branch |= align_branch_indirect_bit;
16821 else
16822 as_fatal (_("invalid -malign-branch= option: `%s'"), arg);
16823 type = next;
16824 }
16825 while (next != NULL);
16826 free (saved);
16827 break;
16828
76cf450b
L
16829 case OPTION_MBRANCHES_WITH_32B_BOUNDARIES:
16830 align_branch_power = 5;
16831 align_branch_prefix_size = 5;
16832 align_branch = (align_branch_jcc_bit
16833 | align_branch_fused_bit
16834 | align_branch_jmp_bit);
16835 break;
16836
5db04b09 16837 case OPTION_MAMD64:
4b5aaf5f 16838 isa64 = amd64;
5db04b09
L
16839 break;
16840
16841 case OPTION_MINTEL64:
4b5aaf5f 16842 isa64 = intel64;
5db04b09
L
16843 break;
16844
b6f8c7c4
L
16845 case 'O':
16846 if (arg == NULL)
16847 {
16848 optimize = 1;
16849 /* Turn off -Os. */
16850 optimize_for_space = 0;
16851 }
16852 else if (*arg == 's')
16853 {
16854 optimize_for_space = 1;
16855 /* Turn on all encoding optimizations. */
41fd2579 16856 optimize = INT_MAX;
b6f8c7c4
L
16857 }
16858 else
16859 {
16860 optimize = atoi (arg);
16861 /* Turn off -Os. */
16862 optimize_for_space = 0;
16863 }
16864 break;
16865
252b5132
RH
16866 default:
16867 return 0;
16868 }
16869 return 1;
16870}
16871
8a2c8fef
L
16872#define MESSAGE_TEMPLATE \
16873" "
16874
293f5f65
L
16875static char *
16876output_message (FILE *stream, char *p, char *message, char *start,
16877 int *left_p, const char *name, int len)
16878{
16879 int size = sizeof (MESSAGE_TEMPLATE);
16880 int left = *left_p;
16881
16882 /* Reserve 2 spaces for ", " or ",\0" */
16883 left -= len + 2;
16884
16885 /* Check if there is any room. */
16886 if (left >= 0)
16887 {
16888 if (p != start)
16889 {
16890 *p++ = ',';
16891 *p++ = ' ';
16892 }
16893 p = mempcpy (p, name, len);
16894 }
16895 else
16896 {
16897 /* Output the current message now and start a new one. */
16898 *p++ = ',';
16899 *p = '\0';
16900 fprintf (stream, "%s\n", message);
16901 p = start;
16902 left = size - (start - message) - len - 2;
16903
16904 gas_assert (left >= 0);
16905
16906 p = mempcpy (p, name, len);
16907 }
16908
16909 *left_p = left;
16910 return p;
16911}
16912
8a2c8fef 16913static void
1ded5609 16914show_arch (FILE *stream, int ext, int check)
8a2c8fef
L
16915{
16916 static char message[] = MESSAGE_TEMPLATE;
16917 char *start = message + 27;
16918 char *p;
16919 int size = sizeof (MESSAGE_TEMPLATE);
16920 int left;
16921 const char *name;
16922 int len;
16923 unsigned int j;
16924
16925 p = start;
16926 left = size - (start - message);
3ce2ebcf
JB
16927
16928 if (!ext && check)
16929 {
16930 p = output_message (stream, p, message, start, &left,
16931 STRING_COMMA_LEN ("default"));
f68697e8
JB
16932 p = output_message (stream, p, message, start, &left,
16933 STRING_COMMA_LEN ("push"));
16934 p = output_message (stream, p, message, start, &left,
16935 STRING_COMMA_LEN ("pop"));
3ce2ebcf
JB
16936 }
16937
8a2c8fef
L
16938 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
16939 {
16940 /* Should it be skipped? */
16941 if (cpu_arch [j].skip)
16942 continue;
16943
16944 name = cpu_arch [j].name;
16945 len = cpu_arch [j].len;
ae89daec 16946 if (cpu_arch[j].type == PROCESSOR_NONE)
8a2c8fef
L
16947 {
16948 /* It is an extension. Skip if we aren't asked to show it. */
ae89daec 16949 if (!ext || cpu_flags_all_zero (&cpu_arch[j].enable))
8a2c8fef
L
16950 continue;
16951 }
16952 else if (ext)
16953 {
16954 /* It is an processor. Skip if we show only extension. */
16955 continue;
16956 }
ae89daec 16957 else if (check && ! cpu_arch[j].enable.bitfield.cpui386)
1ded5609
JB
16958 {
16959 /* It is an impossible processor - skip. */
16960 continue;
16961 }
8a2c8fef 16962
293f5f65 16963 p = output_message (stream, p, message, start, &left, name, len);
8a2c8fef
L
16964 }
16965
293f5f65
L
16966 /* Display disabled extensions. */
16967 if (ext)
ae89daec 16968 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
293f5f65 16969 {
ae89daec
JB
16970 char *str;
16971
16972 if (cpu_arch[j].type != PROCESSOR_NONE
16973 || !cpu_flags_all_zero (&cpu_arch[j].enable))
16974 continue;
16975 str = xasprintf ("no%s", cpu_arch[j].name);
16976 p = output_message (stream, p, message, start, &left, str,
16977 strlen (str));
16978 free (str);
293f5f65
L
16979 }
16980
8a2c8fef
L
16981 *p = '\0';
16982 fprintf (stream, "%s\n", message);
16983}
16984
252b5132 16985void
8a2c8fef 16986md_show_usage (FILE *stream)
252b5132 16987{
4cc782b5
ILT
16988#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
16989 fprintf (stream, _("\
d4693039 16990 -Qy, -Qn ignored\n\
a38cf1db 16991 -V print assembler version number\n\
b3b91714
AM
16992 -k ignored\n"));
16993#endif
16994 fprintf (stream, _("\
7ebd68d1
NC
16995 -n do not optimize code alignment\n\
16996 -O{012s} attempt some code optimizations\n\
b3b91714
AM
16997 -q quieten some warnings\n"));
16998#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
16999 fprintf (stream, _("\
a38cf1db 17000 -s ignored\n"));
b3b91714 17001#endif
b00af7c8
JB
17002#ifdef BFD64
17003# if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
17004 fprintf (stream, _("\
17005 --32/--64/--x32 generate 32bit/64bit/x32 object\n"));
17006# elif defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O)
751d281c 17007 fprintf (stream, _("\
b00af7c8
JB
17008 --32/--64 generate 32bit/64bit object\n"));
17009# endif
751d281c 17010#endif
b3b91714
AM
17011#ifdef SVR4_COMMENT_CHARS
17012 fprintf (stream, _("\
17013 --divide do not treat `/' as a comment character\n"));
a38cf1db
AM
17014#else
17015 fprintf (stream, _("\
b3b91714 17016 --divide ignored\n"));
4cc782b5 17017#endif
9103f4f4 17018 fprintf (stream, _("\
6305a203 17019 -march=CPU[,+EXTENSION...]\n\
8a2c8fef 17020 generate code for CPU and EXTENSION, CPU is one of:\n"));
1ded5609 17021 show_arch (stream, 0, 1);
8a2c8fef 17022 fprintf (stream, _("\
ae89daec 17023 EXTENSION is combination of (possibly \"no\"-prefixed):\n"));
1ded5609 17024 show_arch (stream, 1, 0);
6305a203 17025 fprintf (stream, _("\
8a2c8fef 17026 -mtune=CPU optimize for CPU, CPU is one of:\n"));
1ded5609 17027 show_arch (stream, 0, 0);
ba104c83 17028 fprintf (stream, _("\
c0f3af97
L
17029 -msse2avx encode SSE instructions with VEX prefix\n"));
17030 fprintf (stream, _("\
c8480b58
L
17031 -muse-unaligned-vector-move\n\
17032 encode aligned vector move as unaligned vector move\n"));
17033 fprintf (stream, _("\
7a6a03c4 17034 -msse-check=[none|error|warning] (default: none)\n\
daf50ae7
L
17035 check SSE instructions\n"));
17036 fprintf (stream, _("\
7c5c05ef 17037 -moperand-check=[none|error|warning] (default: warning)\n\
7bab8ab5
JB
17038 check operand combinations for validity\n"));
17039 fprintf (stream, _("\
7c5c05ef
L
17040 -mavxscalar=[128|256] (default: 128)\n\
17041 encode scalar AVX instructions with specific vector\n\
539f890d
L
17042 length\n"));
17043 fprintf (stream, _("\
03751133
L
17044 -mvexwig=[0|1] (default: 0)\n\
17045 encode VEX instructions with specific VEX.W value\n\
17046 for VEX.W bit ignored instructions\n"));
17047 fprintf (stream, _("\
7c5c05ef
L
17048 -mevexlig=[128|256|512] (default: 128)\n\
17049 encode scalar EVEX instructions with specific vector\n\
43234a1e
L
17050 length\n"));
17051 fprintf (stream, _("\
7c5c05ef
L
17052 -mevexwig=[0|1] (default: 0)\n\
17053 encode EVEX instructions with specific EVEX.W value\n\
43234a1e
L
17054 for EVEX.W bit ignored instructions\n"));
17055 fprintf (stream, _("\
7c5c05ef 17056 -mevexrcig=[rne|rd|ru|rz] (default: rne)\n\
d3d3c6db
IT
17057 encode EVEX instructions with specific EVEX.RC value\n\
17058 for SAE-only ignored instructions\n"));
17059 fprintf (stream, _("\
7c5c05ef
L
17060 -mmnemonic=[att|intel] "));
17061 if (SYSV386_COMPAT)
17062 fprintf (stream, _("(default: att)\n"));
17063 else
17064 fprintf (stream, _("(default: intel)\n"));
17065 fprintf (stream, _("\
7d3182d6 17066 use AT&T/Intel mnemonic (AT&T syntax only)\n"));
ba104c83 17067 fprintf (stream, _("\
7c5c05ef
L
17068 -msyntax=[att|intel] (default: att)\n\
17069 use AT&T/Intel syntax\n"));
ba104c83
L
17070 fprintf (stream, _("\
17071 -mindex-reg support pseudo index registers\n"));
17072 fprintf (stream, _("\
17073 -mnaked-reg don't require `%%' prefix for registers\n"));
17074 fprintf (stream, _("\
7e8b059b 17075 -madd-bnd-prefix add BND prefix for all valid branches\n"));
b4a3a7b4 17076#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8dcea932
L
17077 fprintf (stream, _("\
17078 -mshared disable branch optimization for shared code\n"));
b4a3a7b4
L
17079 fprintf (stream, _("\
17080 -mx86-used-note=[no|yes] "));
17081 if (DEFAULT_X86_USED_NOTE)
17082 fprintf (stream, _("(default: yes)\n"));
17083 else
17084 fprintf (stream, _("(default: no)\n"));
17085 fprintf (stream, _("\
17086 generate x86 used ISA and feature properties\n"));
17087#endif
17088#if defined (TE_PE) || defined (TE_PEP)
167ad85b
TG
17089 fprintf (stream, _("\
17090 -mbig-obj generate big object files\n"));
17091#endif
d022bddd 17092 fprintf (stream, _("\
7c5c05ef 17093 -momit-lock-prefix=[no|yes] (default: no)\n\
d022bddd 17094 strip all lock prefixes\n"));
5db04b09 17095 fprintf (stream, _("\
7c5c05ef 17096 -mfence-as-lock-add=[no|yes] (default: no)\n\
e4e00185
AS
17097 encode lfence, mfence and sfence as\n\
17098 lock addl $0x0, (%%{re}sp)\n"));
17099 fprintf (stream, _("\
7c5c05ef
L
17100 -mrelax-relocations=[no|yes] "));
17101 if (DEFAULT_GENERATE_X86_RELAX_RELOCATIONS)
17102 fprintf (stream, _("(default: yes)\n"));
17103 else
17104 fprintf (stream, _("(default: no)\n"));
17105 fprintf (stream, _("\
0cb4071e
L
17106 generate relax relocations\n"));
17107 fprintf (stream, _("\
e379e5f3
L
17108 -malign-branch-boundary=NUM (default: 0)\n\
17109 align branches within NUM byte boundary\n"));
17110 fprintf (stream, _("\
17111 -malign-branch=TYPE[+TYPE...] (default: jcc+fused+jmp)\n\
17112 TYPE is combination of jcc, fused, jmp, call, ret,\n\
17113 indirect\n\
17114 specify types of branches to align\n"));
17115 fprintf (stream, _("\
17116 -malign-branch-prefix-size=NUM (default: 5)\n\
17117 align branches with NUM prefixes per instruction\n"));
17118 fprintf (stream, _("\
76cf450b
L
17119 -mbranches-within-32B-boundaries\n\
17120 align branches within 32 byte boundary\n"));
17121 fprintf (stream, _("\
ae531041
L
17122 -mlfence-after-load=[no|yes] (default: no)\n\
17123 generate lfence after load\n"));
17124 fprintf (stream, _("\
17125 -mlfence-before-indirect-branch=[none|all|register|memory] (default: none)\n\
17126 generate lfence before indirect near branch\n"));
17127 fprintf (stream, _("\
a09f656b 17128 -mlfence-before-ret=[none|or|not|shl|yes] (default: none)\n\
ae531041
L
17129 generate lfence before ret\n"));
17130 fprintf (stream, _("\
7c5c05ef 17131 -mamd64 accept only AMD64 ISA [default]\n"));
5db04b09
L
17132 fprintf (stream, _("\
17133 -mintel64 accept only Intel64 ISA\n"));
252b5132
RH
17134}
17135
3e73aa7c 17136#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
321098a5 17137 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
e57f8c65 17138 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
252b5132
RH
17139
17140/* Pick the target format to use. */
17141
47926f60 17142const char *
e3bb37b5 17143i386_target_format (void)
252b5132 17144{
d34049e8 17145 if (startswith (default_arch, "x86_64"))
351f65ca
L
17146 {
17147 update_code_flag (CODE_64BIT, 1);
071c5d81 17148#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
351f65ca 17149 if (default_arch[6] == '\0')
7f56bc95 17150 x86_elf_abi = X86_64_ABI;
351f65ca 17151 else
7f56bc95 17152 x86_elf_abi = X86_64_X32_ABI;
071c5d81 17153#endif
351f65ca 17154 }
3e73aa7c 17155 else if (!strcmp (default_arch, "i386"))
78f12dd3 17156 update_code_flag (CODE_32BIT, 1);
5197d474
L
17157 else if (!strcmp (default_arch, "iamcu"))
17158 {
17159 update_code_flag (CODE_32BIT, 1);
17160 if (cpu_arch_isa == PROCESSOR_UNKNOWN)
17161 {
17162 static const i386_cpu_flags iamcu_flags = CPU_IAMCU_FLAGS;
17163 cpu_arch_name = "iamcu";
d92c7521 17164 free (cpu_sub_arch_name);
5197d474
L
17165 cpu_sub_arch_name = NULL;
17166 cpu_arch_flags = iamcu_flags;
17167 cpu_arch_isa = PROCESSOR_IAMCU;
17168 cpu_arch_isa_flags = iamcu_flags;
17169 if (!cpu_arch_tune_set)
fb263707 17170 cpu_arch_tune = PROCESSOR_IAMCU;
5197d474 17171 }
8d471ec1 17172 else if (cpu_arch_isa != PROCESSOR_IAMCU)
5197d474
L
17173 as_fatal (_("Intel MCU doesn't support `%s' architecture"),
17174 cpu_arch_name);
17175 }
3e73aa7c 17176 else
2b5d6a91 17177 as_fatal (_("unknown architecture"));
89507696 17178
c7defc53
IB
17179#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
17180 if (IS_ELF && flag_synth_cfi && x86_elf_abi != X86_64_ABI)
17181 as_fatal (_("SCFI is not supported for this ABI"));
17182#endif
17183
89507696 17184 if (cpu_flags_all_zero (&cpu_arch_isa_flags))
ae89daec 17185 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
89507696 17186
252b5132
RH
17187 switch (OUTPUT_FLAVOR)
17188 {
9384f2ff 17189#if defined (OBJ_MAYBE_AOUT) || defined (OBJ_AOUT)
4c63da97 17190 case bfd_target_aout_flavour:
47926f60 17191 return AOUT_TARGET_FORMAT;
4c63da97 17192#endif
9384f2ff
AM
17193#if defined (OBJ_MAYBE_COFF) || defined (OBJ_COFF)
17194# if defined (TE_PE) || defined (TE_PEP)
17195 case bfd_target_coff_flavour:
167ad85b 17196 if (flag_code == CODE_64BIT)
eb19308f
JB
17197 {
17198 object_64bit = 1;
17199 return use_big_obj ? "pe-bigobj-x86-64" : "pe-x86-64";
17200 }
17201 return use_big_obj ? "pe-bigobj-i386" : "pe-i386";
9384f2ff 17202# elif defined (TE_GO32)
0561d57c
JK
17203 case bfd_target_coff_flavour:
17204 return "coff-go32";
9384f2ff 17205# else
252b5132
RH
17206 case bfd_target_coff_flavour:
17207 return "coff-i386";
9384f2ff 17208# endif
4c63da97 17209#endif
3e73aa7c 17210#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
252b5132 17211 case bfd_target_elf_flavour:
3e73aa7c 17212 {
351f65ca
L
17213 const char *format;
17214
17215 switch (x86_elf_abi)
4fa24527 17216 {
351f65ca
L
17217 default:
17218 format = ELF_TARGET_FORMAT;
e379e5f3
L
17219#ifndef TE_SOLARIS
17220 tls_get_addr = "___tls_get_addr";
17221#endif
351f65ca 17222 break;
7f56bc95 17223 case X86_64_ABI:
351f65ca 17224 use_rela_relocations = 1;
4fa24527 17225 object_64bit = 1;
e379e5f3
L
17226#ifndef TE_SOLARIS
17227 tls_get_addr = "__tls_get_addr";
17228#endif
351f65ca
L
17229 format = ELF_TARGET_FORMAT64;
17230 break;
7f56bc95 17231 case X86_64_X32_ABI:
4fa24527 17232 use_rela_relocations = 1;
351f65ca 17233 object_64bit = 1;
e379e5f3
L
17234#ifndef TE_SOLARIS
17235 tls_get_addr = "__tls_get_addr";
17236#endif
862be3fb 17237 disallow_64bit_reloc = 1;
351f65ca
L
17238 format = ELF_TARGET_FORMAT32;
17239 break;
4fa24527 17240 }
c085ab00 17241 if (cpu_arch_isa == PROCESSOR_IAMCU)
81486035
L
17242 {
17243 if (x86_elf_abi != I386_ABI)
17244 as_fatal (_("Intel MCU is 32bit only"));
17245 return ELF_TARGET_IAMCU_FORMAT;
17246 }
8a9036a4 17247 else
351f65ca 17248 return format;
3e73aa7c 17249 }
e57f8c65
TG
17250#endif
17251#if defined (OBJ_MACH_O)
17252 case bfd_target_mach_o_flavour:
d382c579
TG
17253 if (flag_code == CODE_64BIT)
17254 {
17255 use_rela_relocations = 1;
17256 object_64bit = 1;
17257 return "mach-o-x86-64";
17258 }
17259 else
17260 return "mach-o-i386";
4c63da97 17261#endif
252b5132
RH
17262 default:
17263 abort ();
17264 return NULL;
17265 }
17266}
17267
47926f60 17268#endif /* OBJ_MAYBE_ more than one */
252b5132 17269\f
252b5132 17270symbolS *
7016a5d5 17271md_undefined_symbol (char *name)
252b5132 17272{
18dc2407
ILT
17273 if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
17274 && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
17275 && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
17276 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
24eab124
AM
17277 {
17278 if (!GOT_symbol)
17279 {
17280 if (symbol_find (name))
17281 as_bad (_("GOT already in symbol table"));
17282 GOT_symbol = symbol_new (name, undefined_section,
e01e1cee 17283 &zero_address_frag, 0);
24eab124
AM
17284 };
17285 return GOT_symbol;
17286 }
252b5132
RH
17287 return 0;
17288}
17289
071c5d81 17290#if defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT)
252b5132 17291/* Round up a section size to the appropriate boundary. */
47926f60 17292
252b5132 17293valueT
071c5d81 17294md_section_align (segT segment, valueT size)
252b5132 17295{
4c63da97
AM
17296 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
17297 {
17298 /* For a.out, force the section size to be aligned. If we don't do
17299 this, BFD will align it for us, but it will not write out the
17300 final bytes of the section. This may be a bug in BFD, but it is
17301 easier to fix it here since that is how the other a.out targets
17302 work. */
17303 int align;
17304
fd361982 17305 align = bfd_section_alignment (segment);
8d3842cd 17306 size = ((size + (1 << align) - 1) & (-((valueT) 1 << align)));
4c63da97 17307 }
252b5132
RH
17308
17309 return size;
17310}
071c5d81 17311#endif
252b5132
RH
17312
17313/* On the i386, PC-relative offsets are relative to the start of the
17314 next instruction. That is, the address of the offset, plus its
17315 size, since the offset is always the last part of the insn. */
17316
17317long
e3bb37b5 17318md_pcrel_from (fixS *fixP)
252b5132
RH
17319{
17320 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
17321}
17322
d4064aad 17323#ifdef OBJ_AOUT
252b5132
RH
17324
17325static void
e3bb37b5 17326s_bss (int ignore ATTRIBUTE_UNUSED)
252b5132 17327{
29b0f896 17328 int temp;
252b5132
RH
17329
17330 temp = get_absolute_expression ();
17331 subseg_set (bss_section, (subsegT) temp);
17332 demand_empty_rest_of_line ();
17333}
17334
17335#endif
17336
e379e5f3
L
17337/* Remember constant directive. */
17338
17339void
17340i386_cons_align (int ignore ATTRIBUTE_UNUSED)
17341{
b5482fe5
JB
17342 struct last_insn *last_insn
17343 = &seg_info(now_seg)->tc_segment_info_data.last_insn;
17344
ce69d081 17345 if (bfd_section_flags (now_seg) & SEC_CODE)
e379e5f3 17346 {
b5482fe5
JB
17347 last_insn->kind = last_insn_directive;
17348 last_insn->name = "constant directive";
17349 last_insn->file = as_where (&last_insn->line);
e379e5f3
L
17350 }
17351}
17352
3abbafc2 17353int
e3bb37b5 17354i386_validate_fix (fixS *fixp)
252b5132 17355{
e52a16f2
JB
17356 if (fixp->fx_addsy && S_GET_SEGMENT(fixp->fx_addsy) == reg_section)
17357 {
17358 reloc_howto_type *howto;
17359
17360 howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
17361 as_bad_where (fixp->fx_file, fixp->fx_line,
17362 _("invalid %s relocation against register"),
17363 howto ? howto->name : "<unknown>");
17364 return 0;
17365 }
17366
3abbafc2
JB
17367#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
17368 if (fixp->fx_r_type == BFD_RELOC_SIZE32
17369 || fixp->fx_r_type == BFD_RELOC_SIZE64)
17370 return IS_ELF && fixp->fx_addsy
17371 && (!S_IS_DEFINED (fixp->fx_addsy)
17372 || S_IS_EXTERNAL (fixp->fx_addsy));
a533c8df 17373
5bc71c2a
L
17374 /* BFD_RELOC_X86_64_GOTTPOFF:
17375 1. fx_tcbit -> BFD_RELOC_X86_64_CODE_4_GOTTPOFF
17376 2. fx_tcbit2 -> BFD_RELOC_X86_64_CODE_6_GOTTPOFF
17377 BFD_RELOC_X86_64_GOTPC32_TLSDESC:
17378 1. fx_tcbit -> BFD_RELOC_X86_64_CODE_4_GOTPC32_TLSDESC
17379 BFD_RELOC_32_PCREL:
17380 1. fx_tcbit -> BFD_RELOC_X86_64_GOTPCRELX
17381 2. fx_tcbit2 -> BFD_RELOC_X86_64_REX_GOTPCRELX
17382 3. fx_tcbit3 -> BFD_RELOC_X86_64_CODE_4_GOTPCRELX
17383 4. else -> BFD_RELOC_X86_64_GOTPCREL
17384 */
17385 if (fixp->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF)
a533c8df 17386 {
5bc71c2a 17387 if (fixp->fx_tcbit)
a533c8df 17388 fixp->fx_r_type = BFD_RELOC_X86_64_CODE_4_GOTTPOFF;
5bc71c2a
L
17389 else if (fixp->fx_tcbit2)
17390 fixp->fx_r_type = BFD_RELOC_X86_64_CODE_6_GOTTPOFF;
a533c8df 17391 }
5bc71c2a
L
17392 else if (fixp->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
17393 && fixp->fx_tcbit)
17394 fixp->fx_r_type = BFD_RELOC_X86_64_CODE_4_GOTPC32_TLSDESC;
3abbafc2
JB
17395#endif
17396
02a86693 17397 if (fixp->fx_subsy)
252b5132 17398 {
02a86693 17399 if (fixp->fx_subsy == GOT_symbol)
23df1078 17400 {
02a86693
L
17401 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
17402 {
17403 if (!object_64bit)
17404 abort ();
17405#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
5bc71c2a
L
17406 if (fixp->fx_tcbit)
17407 fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCRELX;
17408 else if (fixp->fx_tcbit2)
17409 fixp->fx_r_type = BFD_RELOC_X86_64_REX_GOTPCRELX;
17410 else if (fixp->fx_tcbit3)
17411 fixp->fx_r_type = BFD_RELOC_X86_64_CODE_4_GOTPCRELX;
02a86693
L
17412 else
17413#endif
17414 fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
17415 }
d6ab8113 17416 else
02a86693
L
17417 {
17418 if (!object_64bit)
17419 fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
17420 else
17421 fixp->fx_r_type = BFD_RELOC_X86_64_GOTOFF64;
17422 }
17423 fixp->fx_subsy = 0;
23df1078 17424 }
252b5132 17425 }
02a86693 17426#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2585b7a5 17427 else
02a86693 17428 {
2585b7a5
L
17429 /* NB: Commit 292676c1 resolved PLT32 reloc aganst local symbol
17430 to section. Since PLT32 relocation must be against symbols,
17431 turn such PLT32 relocation into PC32 relocation. */
17432 if (fixp->fx_addsy
17433 && (fixp->fx_r_type == BFD_RELOC_386_PLT32
17434 || fixp->fx_r_type == BFD_RELOC_X86_64_PLT32)
17435 && symbol_section_p (fixp->fx_addsy))
17436 fixp->fx_r_type = BFD_RELOC_32_PCREL;
17437 if (!object_64bit)
17438 {
17439 if (fixp->fx_r_type == BFD_RELOC_386_GOT32
17440 && fixp->fx_tcbit2)
17441 fixp->fx_r_type = BFD_RELOC_386_GOT32X;
17442 }
02a86693
L
17443 }
17444#endif
3abbafc2
JB
17445
17446 return 1;
252b5132
RH
17447}
17448
252b5132 17449arelent *
7016a5d5 17450tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
17451{
17452 arelent *rel;
17453 bfd_reloc_code_real_type code;
17454
17455 switch (fixp->fx_r_type)
17456 {
8ce3d284 17457#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3abbafc2
JB
17458 symbolS *sym;
17459
8fd4256d
L
17460 case BFD_RELOC_SIZE32:
17461 case BFD_RELOC_SIZE64:
3abbafc2
JB
17462 if (fixp->fx_addsy
17463 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))
17464 && (!fixp->fx_subsy
17465 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))))
17466 sym = fixp->fx_addsy;
17467 else if (fixp->fx_subsy
17468 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))
17469 && (!fixp->fx_addsy
17470 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))))
17471 sym = fixp->fx_subsy;
17472 else
17473 sym = NULL;
17474 if (IS_ELF && sym && S_IS_DEFINED (sym) && !S_IS_EXTERNAL (sym))
8fd4256d
L
17475 {
17476 /* Resolve size relocation against local symbol to size of
17477 the symbol plus addend. */
3abbafc2 17478 valueT value = S_GET_SIZE (sym);
44f87162 17479
3abbafc2
JB
17480 if (symbol_get_bfdsym (sym)->flags & BSF_SECTION_SYM)
17481 value = bfd_section_size (S_GET_SEGMENT (sym));
17482 if (sym == fixp->fx_subsy)
17483 {
17484 value = -value;
17485 if (fixp->fx_addsy)
17486 value += S_GET_VALUE (fixp->fx_addsy);
17487 }
17488 else if (fixp->fx_subsy)
17489 value -= S_GET_VALUE (fixp->fx_subsy);
44f87162 17490 value += fixp->fx_offset;
8fd4256d 17491 if (fixp->fx_r_type == BFD_RELOC_SIZE32
d965814f 17492 && object_64bit
8fd4256d
L
17493 && !fits_in_unsigned_long (value))
17494 as_bad_where (fixp->fx_file, fixp->fx_line,
17495 _("symbol size computation overflow"));
17496 fixp->fx_addsy = NULL;
17497 fixp->fx_subsy = NULL;
17498 md_apply_fix (fixp, (valueT *) &value, NULL);
17499 return NULL;
17500 }
3abbafc2
JB
17501 if (!fixp->fx_addsy || fixp->fx_subsy)
17502 {
17503 as_bad_where (fixp->fx_file, fixp->fx_line,
17504 "unsupported expression involving @size");
17505 return NULL;
17506 }
8ce3d284 17507#endif
1a0670f3 17508 /* Fall through. */
8fd4256d 17509
3e73aa7c
JH
17510 case BFD_RELOC_X86_64_PLT32:
17511 case BFD_RELOC_X86_64_GOT32:
17512 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
17513 case BFD_RELOC_X86_64_GOTPCRELX:
17514 case BFD_RELOC_X86_64_REX_GOTPCRELX:
3d5a60de 17515 case BFD_RELOC_X86_64_CODE_4_GOTPCRELX:
252b5132
RH
17516 case BFD_RELOC_386_PLT32:
17517 case BFD_RELOC_386_GOT32:
02a86693 17518 case BFD_RELOC_386_GOT32X:
252b5132
RH
17519 case BFD_RELOC_386_GOTOFF:
17520 case BFD_RELOC_386_GOTPC:
13ae64f3
JJ
17521 case BFD_RELOC_386_TLS_GD:
17522 case BFD_RELOC_386_TLS_LDM:
17523 case BFD_RELOC_386_TLS_LDO_32:
17524 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
17525 case BFD_RELOC_386_TLS_IE:
17526 case BFD_RELOC_386_TLS_GOTIE:
13ae64f3
JJ
17527 case BFD_RELOC_386_TLS_LE_32:
17528 case BFD_RELOC_386_TLS_LE:
67a4f2b7
AO
17529 case BFD_RELOC_386_TLS_GOTDESC:
17530 case BFD_RELOC_386_TLS_DESC_CALL:
bffbf940
JJ
17531 case BFD_RELOC_X86_64_TLSGD:
17532 case BFD_RELOC_X86_64_TLSLD:
17533 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 17534 case BFD_RELOC_X86_64_DTPOFF64:
bffbf940 17535 case BFD_RELOC_X86_64_GOTTPOFF:
a533c8df 17536 case BFD_RELOC_X86_64_CODE_4_GOTTPOFF:
5bc71c2a 17537 case BFD_RELOC_X86_64_CODE_6_GOTTPOFF:
bffbf940 17538 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113
JB
17539 case BFD_RELOC_X86_64_TPOFF64:
17540 case BFD_RELOC_X86_64_GOTOFF64:
17541 case BFD_RELOC_X86_64_GOTPC32:
7b81dfbb
AJ
17542 case BFD_RELOC_X86_64_GOT64:
17543 case BFD_RELOC_X86_64_GOTPCREL64:
17544 case BFD_RELOC_X86_64_GOTPC64:
17545 case BFD_RELOC_X86_64_GOTPLT64:
17546 case BFD_RELOC_X86_64_PLTOFF64:
67a4f2b7 17547 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
a533c8df 17548 case BFD_RELOC_X86_64_CODE_4_GOTPC32_TLSDESC:
67a4f2b7 17549 case BFD_RELOC_X86_64_TLSDESC_CALL:
252b5132
RH
17550 case BFD_RELOC_RVA:
17551 case BFD_RELOC_VTABLE_ENTRY:
17552 case BFD_RELOC_VTABLE_INHERIT:
6482c264
NC
17553#ifdef TE_PE
17554 case BFD_RELOC_32_SECREL:
145667f8 17555 case BFD_RELOC_16_SECIDX:
6482c264 17556#endif
252b5132
RH
17557 code = fixp->fx_r_type;
17558 break;
dbbaec26
L
17559 case BFD_RELOC_X86_64_32S:
17560 if (!fixp->fx_pcrel)
17561 {
17562 /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32. */
17563 code = fixp->fx_r_type;
17564 break;
17565 }
1a0670f3 17566 /* Fall through. */
252b5132 17567 default:
93382f6d 17568 if (fixp->fx_pcrel)
252b5132 17569 {
93382f6d
AM
17570 switch (fixp->fx_size)
17571 {
17572 default:
b091f402
AM
17573 as_bad_where (fixp->fx_file, fixp->fx_line,
17574 _("can not do %d byte pc-relative relocation"),
17575 fixp->fx_size);
93382f6d
AM
17576 code = BFD_RELOC_32_PCREL;
17577 break;
17578 case 1: code = BFD_RELOC_8_PCREL; break;
17579 case 2: code = BFD_RELOC_16_PCREL; break;
d258b828 17580 case 4: code = BFD_RELOC_32_PCREL; break;
d6ab8113
JB
17581#ifdef BFD64
17582 case 8: code = BFD_RELOC_64_PCREL; break;
17583#endif
93382f6d
AM
17584 }
17585 }
17586 else
17587 {
17588 switch (fixp->fx_size)
17589 {
17590 default:
b091f402
AM
17591 as_bad_where (fixp->fx_file, fixp->fx_line,
17592 _("can not do %d byte relocation"),
17593 fixp->fx_size);
93382f6d
AM
17594 code = BFD_RELOC_32;
17595 break;
17596 case 1: code = BFD_RELOC_8; break;
17597 case 2: code = BFD_RELOC_16; break;
17598 case 4: code = BFD_RELOC_32; break;
937149dd 17599#ifdef BFD64
3e73aa7c 17600 case 8: code = BFD_RELOC_64; break;
937149dd 17601#endif
93382f6d 17602 }
252b5132
RH
17603 }
17604 break;
17605 }
252b5132 17606
d182319b
JB
17607 if ((code == BFD_RELOC_32
17608 || code == BFD_RELOC_32_PCREL
17609 || code == BFD_RELOC_X86_64_32S)
252b5132
RH
17610 && GOT_symbol
17611 && fixp->fx_addsy == GOT_symbol)
3e73aa7c 17612 {
4fa24527 17613 if (!object_64bit)
d6ab8113
JB
17614 code = BFD_RELOC_386_GOTPC;
17615 else
17616 code = BFD_RELOC_X86_64_GOTPC32;
3e73aa7c 17617 }
7b81dfbb
AJ
17618 if ((code == BFD_RELOC_64 || code == BFD_RELOC_64_PCREL)
17619 && GOT_symbol
17620 && fixp->fx_addsy == GOT_symbol)
17621 {
17622 code = BFD_RELOC_X86_64_GOTPC64;
17623 }
252b5132 17624
add39d23
TS
17625 rel = XNEW (arelent);
17626 rel->sym_ptr_ptr = XNEW (asymbol *);
49309057 17627 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
17628
17629 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
c87db184 17630
3e73aa7c
JH
17631 if (!use_rela_relocations)
17632 {
17633 /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
17634 vtable entry to be used in the relocation's section offset. */
17635 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
17636 rel->address = fixp->fx_offset;
fbeb56a4
DK
17637#if defined (OBJ_COFF) && defined (TE_PE)
17638 else if (fixp->fx_addsy && S_IS_WEAK (fixp->fx_addsy))
17639 rel->addend = fixp->fx_addnumber - (S_GET_VALUE (fixp->fx_addsy) * 2);
17640 else
17641#endif
c6682705 17642 rel->addend = 0;
3e73aa7c
JH
17643 }
17644 /* Use the rela in 64bit mode. */
252b5132 17645 else
3e73aa7c 17646 {
862be3fb
L
17647 if (disallow_64bit_reloc)
17648 switch (code)
17649 {
862be3fb
L
17650 case BFD_RELOC_X86_64_DTPOFF64:
17651 case BFD_RELOC_X86_64_TPOFF64:
17652 case BFD_RELOC_64_PCREL:
17653 case BFD_RELOC_X86_64_GOTOFF64:
17654 case BFD_RELOC_X86_64_GOT64:
17655 case BFD_RELOC_X86_64_GOTPCREL64:
17656 case BFD_RELOC_X86_64_GOTPC64:
17657 case BFD_RELOC_X86_64_GOTPLT64:
17658 case BFD_RELOC_X86_64_PLTOFF64:
17659 as_bad_where (fixp->fx_file, fixp->fx_line,
17660 _("cannot represent relocation type %s in x32 mode"),
17661 bfd_get_reloc_code_name (code));
17662 break;
17663 default:
17664 break;
17665 }
17666
062cd5e7
AS
17667 if (!fixp->fx_pcrel)
17668 rel->addend = fixp->fx_offset;
17669 else
17670 switch (code)
17671 {
17672 case BFD_RELOC_X86_64_PLT32:
17673 case BFD_RELOC_X86_64_GOT32:
17674 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
17675 case BFD_RELOC_X86_64_GOTPCRELX:
17676 case BFD_RELOC_X86_64_REX_GOTPCRELX:
3d5a60de 17677 case BFD_RELOC_X86_64_CODE_4_GOTPCRELX:
bffbf940
JJ
17678 case BFD_RELOC_X86_64_TLSGD:
17679 case BFD_RELOC_X86_64_TLSLD:
17680 case BFD_RELOC_X86_64_GOTTPOFF:
a533c8df 17681 case BFD_RELOC_X86_64_CODE_4_GOTTPOFF:
5bc71c2a 17682 case BFD_RELOC_X86_64_CODE_6_GOTTPOFF:
67a4f2b7 17683 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
a533c8df 17684 case BFD_RELOC_X86_64_CODE_4_GOTPC32_TLSDESC:
67a4f2b7 17685 case BFD_RELOC_X86_64_TLSDESC_CALL:
062cd5e7
AS
17686 rel->addend = fixp->fx_offset - fixp->fx_size;
17687 break;
17688 default:
17689 rel->addend = (section->vma
17690 - fixp->fx_size
17691 + fixp->fx_addnumber
17692 + md_pcrel_from (fixp));
17693 break;
17694 }
3e73aa7c
JH
17695 }
17696
252b5132
RH
17697 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
17698 if (rel->howto == NULL)
17699 {
17700 as_bad_where (fixp->fx_file, fixp->fx_line,
d0b47220 17701 _("cannot represent relocation type %s"),
252b5132
RH
17702 bfd_get_reloc_code_name (code));
17703 /* Set howto to a garbage value so that we can keep going. */
17704 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
9c2799c2 17705 gas_assert (rel->howto != NULL);
252b5132
RH
17706 }
17707
17708 return rel;
17709}
17710
ee86248c 17711#include "tc-i386-intel.c"
54cfded0 17712
a60de03c
JB
17713void
17714tc_x86_parse_to_dw2regnum (expressionS *exp)
54cfded0 17715{
a60de03c
JB
17716 int saved_naked_reg;
17717 char saved_register_dot;
54cfded0 17718
a60de03c
JB
17719 saved_naked_reg = allow_naked_reg;
17720 allow_naked_reg = 1;
17721 saved_register_dot = register_chars['.'];
17722 register_chars['.'] = '.';
17723 allow_pseudo_reg = 1;
17724 expression_and_evaluate (exp);
17725 allow_pseudo_reg = 0;
17726 register_chars['.'] = saved_register_dot;
17727 allow_naked_reg = saved_naked_reg;
17728
e96d56a1 17729 if (exp->X_op == O_register && exp->X_add_number >= 0)
54cfded0 17730 {
066673f6 17731 exp->X_op = O_illegal;
a60de03c
JB
17732 if ((addressT) exp->X_add_number < i386_regtab_size)
17733 {
a60de03c 17734 exp->X_add_number = i386_regtab[exp->X_add_number]
fabb73d1 17735 .dw2_regnum[object_64bit];
066673f6
JB
17736 if (exp->X_add_number != Dw2Inval)
17737 exp->X_op = O_constant;
a60de03c 17738 }
54cfded0 17739 }
54cfded0
AM
17740}
17741
17742void
17743tc_x86_frame_initial_instructions (void)
17744{
fabb73d1 17745 cfi_add_CFA_def_cfa (object_64bit ? REG_SP : 4, -x86_cie_data_alignment);
61ff971f 17746 cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
54cfded0 17747}
d2b2c203 17748
d7921315
L
17749int
17750x86_dwarf2_addr_size (void)
17751{
17752#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
17753 if (x86_elf_abi == X86_64_X32_ABI)
17754 return 4;
17755#endif
17756 return bfd_arch_bits_per_address (stdoutput) / 8;
17757}
17758
071c5d81
JB
17759#ifdef TE_PE
17760void
17761tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
17762{
17763 expressionS exp;
17764
17765 exp.X_op = O_secrel;
17766 exp.X_add_symbol = symbol;
17767 exp.X_add_number = 0;
17768 emit_expr (&exp, size);
17769}
17770#endif
17771
17772#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
d2b2c203
DJ
17773int
17774i386_elf_section_type (const char *str, size_t len)
17775{
17776 if (flag_code == CODE_64BIT
17777 && len == sizeof ("unwind") - 1
d34049e8 17778 && startswith (str, "unwind"))
d2b2c203
DJ
17779 return SHT_X86_64_UNWIND;
17780
17781 return -1;
17782}
bb41ade5 17783
da374e94
JB
17784void
17785i386_elf_section_change_hook (void)
17786{
17787 struct i386_segment_info *info = &seg_info(now_seg)->tc_segment_info_data;
17788 struct i386_segment_info *curr, *prev;
17789
17790 if (info->subseg == now_subseg)
17791 return;
17792
17793 /* Find the (or make a) list entry to save state into. */
17794 for (prev = info; (curr = prev->next) != NULL; prev = curr)
17795 if (curr->subseg == info->subseg)
17796 break;
17797 if (!curr)
17798 {
fddee222 17799 curr = notes_alloc (sizeof (*curr));
da374e94
JB
17800 curr->subseg = info->subseg;
17801 curr->next = NULL;
17802 prev->next = curr;
17803 }
17804 curr->last_insn = info->last_insn;
17805
17806 /* Find the list entry to load state from. */
17807 for (curr = info->next; curr; curr = curr->next)
17808 if (curr->subseg == now_subseg)
17809 break;
17810 if (curr)
17811 info->last_insn = curr->last_insn;
17812 else
17813 memset (&info->last_insn, 0, sizeof (info->last_insn));
17814 info->subseg = now_subseg;
17815}
17816
ad5fec3b
EB
17817#ifdef TE_SOLARIS
17818void
17819i386_solaris_fix_up_eh_frame (segT sec)
17820{
17821 if (flag_code == CODE_64BIT)
17822 elf_section_type (sec) = SHT_X86_64_UNWIND;
17823}
17824#endif
17825
3b22753a
L
17826/* For ELF on x86-64, add support for SHF_X86_64_LARGE. */
17827
01e1a5bc 17828bfd_vma
6d4af3c2 17829x86_64_section_letter (int letter, const char **ptr_msg)
3b22753a
L
17830{
17831 if (flag_code == CODE_64BIT)
17832 {
17833 if (letter == 'l')
17834 return SHF_X86_64_LARGE;
17835
8f3bae45 17836 *ptr_msg = _("bad .section directive: want a,l,w,x,M,S,G,T in string");
64e74474 17837 }
3b22753a 17838 else
8f3bae45 17839 *ptr_msg = _("bad .section directive: want a,w,x,M,S,G,T in string");
3b22753a
L
17840 return -1;
17841}
17842
3b22753a
L
17843static void
17844handle_large_common (int small ATTRIBUTE_UNUSED)
17845{
17846 if (flag_code != CODE_64BIT)
17847 {
17848 s_comm_internal (0, elf_common_parse);
17849 as_warn (_(".largecomm supported only in 64bit mode, producing .comm"));
17850 }
17851 else
17852 {
17853 static segT lbss_section;
17854 asection *saved_com_section_ptr = elf_com_section_ptr;
17855 asection *saved_bss_section = bss_section;
17856
17857 if (lbss_section == NULL)
17858 {
17859 flagword applicable;
17860 segT seg = now_seg;
17861 subsegT subseg = now_subseg;
17862
17863 /* The .lbss section is for local .largecomm symbols. */
17864 lbss_section = subseg_new (".lbss", 0);
17865 applicable = bfd_applicable_section_flags (stdoutput);
fd361982 17866 bfd_set_section_flags (lbss_section, applicable & SEC_ALLOC);
3b22753a
L
17867 seg_info (lbss_section)->bss = 1;
17868
17869 subseg_set (seg, subseg);
17870 }
17871
17872 elf_com_section_ptr = &_bfd_elf_large_com_section;
17873 bss_section = lbss_section;
17874
17875 s_comm_internal (0, elf_common_parse);
17876
17877 elf_com_section_ptr = saved_com_section_ptr;
17878 bss_section = saved_bss_section;
17879 }
17880}
17881#endif /* OBJ_ELF || OBJ_MAYBE_ELF */