]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/config/tc-i386.c
x86: fix Solaris testsuite failures
[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,
6c30d220 245 unsupported,
9db83a32
JB
246 unsupported_on_arch,
247 unsupported_64bit,
54294d73
JB
248 no_vex_encoding,
249 no_evex_encoding,
260cd341 250 invalid_sib_address,
6c30d220 251 invalid_vsib_address,
7bab8ab5 252 invalid_vector_register_set,
260cd341 253 invalid_tmm_register_set,
0cc78721 254 invalid_dest_and_src_register_set,
08a98d4c 255 invalid_dest_register_set,
80d61d8d 256 invalid_pseudo_prefix,
43234a1e
L
257 unsupported_vector_index_register,
258 unsupported_broadcast,
43234a1e
L
259 broadcast_needed,
260 unsupported_masking,
261 mask_not_on_destination,
262 no_default_mask,
263 unsupported_rc_sae,
54294d73 264 unsupported_vector_size,
08a98d4c 265 unsupported_rsp_register,
58bceb18 266 internal_error,
a65babc9
L
267 };
268
252b5132
RH
269struct _i386_insn
270 {
47926f60 271 /* TM holds the template for the insn were currently assembling. */
d3ce72d0 272 insn_template tm;
252b5132 273
7d5e4556
L
274 /* SUFFIX holds the instruction size suffix for byte, word, dword
275 or qword, if given. */
252b5132
RH
276 char suffix;
277
9a182d04
JB
278 /* OPCODE_LENGTH holds the number of base opcode bytes. */
279 unsigned char opcode_length;
280
47926f60 281 /* OPERANDS gives the number of given operands. */
252b5132
RH
282 unsigned int operands;
283
284 /* REG_OPERANDS, DISP_OPERANDS, MEM_OPERANDS, IMM_OPERANDS give the number
285 of given register, displacement, memory operands and immediate
47926f60 286 operands. */
252b5132
RH
287 unsigned int reg_operands, disp_operands, mem_operands, imm_operands;
288
289 /* TYPES [i] is the type (see above #defines) which tells us how to
520dc8e8 290 use OP[i] for the corresponding operand. */
40fb9820 291 i386_operand_type types[MAX_OPERANDS];
252b5132 292
520dc8e8
AM
293 /* Displacement expression, immediate expression, or register for each
294 operand. */
295 union i386_op op[MAX_OPERANDS];
252b5132 296
3e73aa7c
JH
297 /* Flags for operands. */
298 unsigned int flags[MAX_OPERANDS];
299#define Operand_PCrel 1
c48dadc9 300#define Operand_Mem 2
c032bc4f 301#define Operand_Signed 4 /* .insn only */
3e73aa7c 302
252b5132 303 /* Relocation type for operand */
f86103b7 304 enum bfd_reloc_code_real reloc[MAX_OPERANDS];
252b5132 305
252b5132
RH
306 /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
307 the base index byte below. */
308 const reg_entry *base_reg;
309 const reg_entry *index_reg;
310 unsigned int log2_scale_factor;
311
312 /* SEG gives the seg_entries of this insn. They are zero unless
47926f60 313 explicit segment overrides are given. */
5e042380 314 const reg_entry *seg[2];
252b5132
RH
315
316 /* PREFIX holds all the given prefix opcodes (usually null).
317 PREFIXES is the number of prefix opcodes. */
318 unsigned int prefixes;
319 unsigned char prefix[MAX_PREFIXES];
320
d0c2e3ec
JB
321 /* .insn allows for reserved opcode spaces. */
322 unsigned char insn_opcode_space;
323
c032bc4f
JB
324 /* .insn also allows (requires) specifying immediate size. */
325 unsigned char imm_bits[MAX_OPERANDS];
326
50128d0c 327 /* Register is in low 3 bits of opcode. */
5b7c81bd 328 bool short_form;
50128d0c 329
6f2f06be 330 /* The operand to a branch insn indicates an absolute branch. */
5b7c81bd 331 bool jumpabsolute;
6f2f06be 332
a4d3acd2
JB
333 /* The operand to a branch insn indicates a far branch. */
334 bool far_branch;
335
9373f275
L
336 /* There is a memory operand of (%dx) which should be only used
337 with input/output instructions. */
338 bool input_output_operand;
339
921eafea
L
340 /* Extended states. */
341 enum
342 {
343 /* Use MMX state. */
344 xstate_mmx = 1 << 0,
345 /* Use XMM state. */
346 xstate_xmm = 1 << 1,
347 /* Use YMM state. */
348 xstate_ymm = 1 << 2 | xstate_xmm,
349 /* Use ZMM state. */
350 xstate_zmm = 1 << 3 | xstate_ymm,
351 /* Use TMM state. */
32930e4e
L
352 xstate_tmm = 1 << 4,
353 /* Use MASK state. */
354 xstate_mask = 1 << 5
921eafea 355 } xstate;
260cd341 356
e379e5f3 357 /* Has GOTPC or TLS relocation. */
5b7c81bd 358 bool has_gotpc_tls_reloc;
e379e5f3 359
252b5132 360 /* RM and SIB are the modrm byte and the sib byte where the
c1e679ec 361 addressing modes of this insn are encoded. */
252b5132 362 modrm_byte rm;
3e73aa7c 363 rex_byte rex;
43234a1e 364 rex_byte vrex;
80d61d8d 365 rex_byte rex2;
252b5132 366 sib_byte sib;
c0f3af97 367 vex_prefix vex;
b6169b20 368
6225c532
JB
369 /* Masking attributes.
370
371 The struct describes masking, applied to OPERAND in the instruction.
372 REG is a pointer to the corresponding mask register. ZEROING tells
373 whether merging or zeroing mask is used. */
374 struct Mask_Operation
375 {
376 const reg_entry *reg;
377 unsigned int zeroing;
378 /* The operand where this operation is associated. */
379 unsigned int operand;
380 } mask;
43234a1e
L
381
382 /* Rounding control and SAE attributes. */
ca5312a2
JB
383 struct RC_Operation
384 {
385 enum rc_type
386 {
387 rc_none = -1,
388 rne,
389 rd,
390 ru,
391 rz,
392 saeonly
393 } type;
7063667e
JB
394 /* In Intel syntax the operand modifier form is supposed to be used, but
395 we continue to accept the immediate forms as well. */
396 bool modifier;
ca5312a2 397 } rounding;
43234a1e 398
5273a3cd
JB
399 /* Broadcasting attributes.
400
401 The struct describes broadcasting, applied to OPERAND. TYPE is
402 expresses the broadcast factor. */
403 struct Broadcast_Operation
404 {
0cc78721 405 /* Type of broadcast: {1to2}, {1to4}, {1to8}, {1to16} or {1to32}. */
5273a3cd
JB
406 unsigned int type;
407
408 /* Index of broadcasted operand. */
409 unsigned int operand;
410
411 /* Number of bytes to broadcast. */
412 unsigned int bytes;
413 } broadcast;
43234a1e
L
414
415 /* Compressed disp8*N attribute. */
416 unsigned int memshift;
417
86fa6981
L
418 /* Prefer load or store in encoding. */
419 enum
420 {
421 dir_encoding_default = 0,
422 dir_encoding_load,
64c49ab3
JB
423 dir_encoding_store,
424 dir_encoding_swap
86fa6981 425 } dir_encoding;
891edac4 426
41eb8e88 427 /* Prefer 8bit, 16bit, 32bit displacement in encoding. */
a501d77e
L
428 enum
429 {
430 disp_encoding_default = 0,
431 disp_encoding_8bit,
41eb8e88 432 disp_encoding_16bit,
a501d77e
L
433 disp_encoding_32bit
434 } disp_encoding;
f8a5c266 435
6b6b6807 436 /* Prefer the REX byte in encoding. */
5b7c81bd 437 bool rex_encoding;
6b6b6807 438
80d61d8d
CL
439 /* Prefer the REX2 prefix in encoding. */
440 bool rex2_encoding;
441
b6f8c7c4 442 /* Disable instruction size optimization. */
5b7c81bd 443 bool no_optimize;
b6f8c7c4 444
e346d50a 445 /* How to encode instructions. */
86fa6981
L
446 enum
447 {
e346d50a
JB
448 encoding_default = 0,
449 encoding_vex,
450 encoding_vex3,
eb3f3841 451 encoding_egpr, /* REX2 or EVEX. */
e346d50a
JB
452 encoding_evex,
453 encoding_evex512,
454 encoding_error
455 } encoding;
86fa6981 456
d5de92cf
L
457 /* REP prefix. */
458 const char *rep_prefix;
459
165de32a
L
460 /* HLE prefix. */
461 const char *hle_prefix;
42164a71 462
7e8b059b
L
463 /* Have BND prefix. */
464 const char *bnd_prefix;
465
04ef582a
L
466 /* Have NOTRACK prefix. */
467 const char *notrack_prefix;
468
891edac4 469 /* Error message. */
a65babc9 470 enum i386_error error;
252b5132
RH
471 };
472
473typedef struct _i386_insn i386_insn;
474
43234a1e
L
475/* Link RC type with corresponding string, that'll be looked for in
476 asm. */
477struct RC_name
478{
479 enum rc_type type;
480 const char *name;
481 unsigned int len;
482};
483
484static const struct RC_name RC_NamesTable[] =
485{
486 { rne, STRING_COMMA_LEN ("rn-sae") },
487 { rd, STRING_COMMA_LEN ("rd-sae") },
488 { ru, STRING_COMMA_LEN ("ru-sae") },
489 { rz, STRING_COMMA_LEN ("rz-sae") },
490 { saeonly, STRING_COMMA_LEN ("sae") },
491};
492
3bfea8ba
L
493/* To be indexed by segment register number. */
494static const unsigned char i386_seg_prefixes[] = {
495 ES_PREFIX_OPCODE,
496 CS_PREFIX_OPCODE,
497 SS_PREFIX_OPCODE,
498 DS_PREFIX_OPCODE,
499 FS_PREFIX_OPCODE,
500 GS_PREFIX_OPCODE
501};
502
252b5132
RH
503/* List of chars besides those in app.c:symbol_chars that can start an
504 operand. Used to prevent the scrubber eating vital white-space. */
86fa6981 505const char extra_symbol_chars[] = "*%-([{}"
252b5132 506#ifdef LEX_AT
32137342
NC
507 "@"
508#endif
509#ifdef LEX_QM
510 "?"
252b5132 511#endif
32137342 512 ;
252b5132 513
b3983e5f
JB
514#if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
515 && !defined (TE_GNU) \
516 && !defined (TE_LINUX) \
d85e70a3 517 && !defined (TE_Haiku) \
b3983e5f
JB
518 && !defined (TE_FreeBSD) \
519 && !defined (TE_DragonFly) \
520 && !defined (TE_NetBSD))
252b5132 521/* This array holds the chars that always start a comment. If the
b3b91714
AM
522 pre-processor is disabled, these aren't very useful. The option
523 --divide will remove '/' from this list. */
524const char *i386_comment_chars = "#/";
525#define SVR4_COMMENT_CHARS 1
252b5132 526#define PREFIX_SEPARATOR '\\'
252b5132 527
b3b91714
AM
528#else
529const char *i386_comment_chars = "#";
530#define PREFIX_SEPARATOR '/'
531#endif
532
252b5132
RH
533/* This array holds the chars that only start a comment at the beginning of
534 a line. If the line seems to have the form '# 123 filename'
ce8a8b2f
AM
535 .line and .file directives will appear in the pre-processed output.
536 Note that input_file.c hand checks for '#' at the beginning of the
252b5132 537 first line of the input file. This is because the compiler outputs
ce8a8b2f
AM
538 #NO_APP at the beginning of its output.
539 Also note that comments started like this one will always work if
252b5132 540 '/' isn't otherwise defined. */
b3b91714 541const char line_comment_chars[] = "#/";
252b5132 542
63a0b638 543const char line_separator_chars[] = ";";
252b5132 544
ce8a8b2f
AM
545/* Chars that can be used to separate mant from exp in floating point
546 nums. */
252b5132
RH
547const char EXP_CHARS[] = "eE";
548
ce8a8b2f
AM
549/* Chars that mean this number is a floating point constant
550 As in 0f12.456
551 or 0d1.2345e12. */
de133cf9 552const char FLT_CHARS[] = "fFdDxXhHbB";
252b5132 553
ce8a8b2f 554/* Tables for lexical analysis. */
252b5132
RH
555static char mnemonic_chars[256];
556static char register_chars[256];
557static char operand_chars[256];
252b5132 558
ce8a8b2f 559/* Lexical macros. */
252b5132
RH
560#define is_operand_char(x) (operand_chars[(unsigned char) x])
561#define is_register_char(x) (register_chars[(unsigned char) x])
562#define is_space_char(x) ((x) == ' ')
252b5132 563
d2b1a14d
JB
564/* All non-digit non-letter characters that may occur in an operand and
565 which aren't already in extra_symbol_chars[]. */
566static const char operand_special_chars[] = "$+,)._~/<>|&^!=:@]";
252b5132
RH
567
568/* md_assemble() always leaves the strings it's passed unaltered. To
569 effect this we maintain a stack of saved characters that we've smashed
570 with '\0's (indicating end of strings for various sub-fields of the
47926f60 571 assembler instruction). */
252b5132 572static char save_stack[32];
ce8a8b2f 573static char *save_stack_p;
252b5132
RH
574#define END_STRING_AND_SAVE(s) \
575 do { *save_stack_p++ = *(s); *(s) = '\0'; } while (0)
576#define RESTORE_END_STRING(s) \
577 do { *(s) = *--save_stack_p; } while (0)
578
47926f60 579/* The instruction we're assembling. */
252b5132
RH
580static i386_insn i;
581
582/* Possible templates for current insn. */
d3b01414 583static templates current_templates;
252b5132 584
31b2323c
L
585/* Per instruction expressionS buffers: max displacements & immediates. */
586static expressionS disp_expressions[MAX_MEMORY_OPERANDS];
587static expressionS im_expressions[MAX_IMMEDIATE_OPERANDS];
252b5132 588
47926f60 589/* Current operand we are working on. */
ee86248c 590static int this_operand = -1;
252b5132 591
d0c2e3ec
JB
592/* Are we processing a .insn directive? */
593#define dot_insn() (i.tm.mnem_off == MN__insn)
594
ed719294
JB
595enum i386_flag_code i386_flag_code;
596#define flag_code i386_flag_code /* Permit to continue using original name. */
4fa24527 597static unsigned int object_64bit;
862be3fb 598static unsigned int disallow_64bit_reloc;
3e73aa7c 599static int use_rela_relocations = 0;
e379e5f3
L
600/* __tls_get_addr/___tls_get_addr symbol for TLS. */
601static const char *tls_get_addr;
3e73aa7c 602
071c5d81 603#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
7af8ed2d 604
351f65ca
L
605/* The ELF ABI to use. */
606enum x86_elf_abi
607{
608 I386_ABI,
7f56bc95
L
609 X86_64_ABI,
610 X86_64_X32_ABI
351f65ca
L
611};
612
613static enum x86_elf_abi x86_elf_abi = I386_ABI;
7af8ed2d 614#endif
351f65ca 615
167ad85b
TG
616#if defined (TE_PE) || defined (TE_PEP)
617/* Use big object file format. */
618static int use_big_obj = 0;
619#endif
620
8dcea932
L
621#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
622/* 1 if generating code for a shared library. */
623static int shared = 0;
b52c4ee4
IB
624
625unsigned int x86_sframe_cfa_sp_reg;
3e3e792a 626/* The other CFA base register for SFrame stack trace info. */
b52c4ee4
IB
627unsigned int x86_sframe_cfa_fp_reg;
628unsigned int x86_sframe_cfa_ra_reg;
629
8dcea932
L
630#endif
631
47926f60
KH
632/* 1 for intel syntax,
633 0 if att syntax. */
634static int intel_syntax = 0;
252b5132 635
4b5aaf5f
L
636static enum x86_64_isa
637{
638 amd64 = 1, /* AMD64 ISA. */
639 intel64 /* Intel64 ISA. */
640} isa64;
e89c5eaa 641
1efbbeb4
L
642/* 1 for intel mnemonic,
643 0 if att mnemonic. */
644static int intel_mnemonic = !SYSV386_COMPAT;
645
a60de03c
JB
646/* 1 if pseudo registers are permitted. */
647static int allow_pseudo_reg = 0;
648
47926f60
KH
649/* 1 if register prefix % not required. */
650static int allow_naked_reg = 0;
252b5132 651
33eaf5de 652/* 1 if the assembler should add BND prefix for all control-transferring
7e8b059b
L
653 instructions supporting it, even if this prefix wasn't specified
654 explicitly. */
655static int add_bnd_prefix = 0;
656
ba104c83 657/* 1 if pseudo index register, eiz/riz, is allowed . */
db51cc60
L
658static int allow_index_reg = 0;
659
d022bddd
IT
660/* 1 if the assembler should ignore LOCK prefix, even if it was
661 specified explicitly. */
662static int omit_lock_prefix = 0;
663
e4e00185
AS
664/* 1 if the assembler should encode lfence, mfence, and sfence as
665 "lock addl $0, (%{re}sp)". */
666static int avoid_fence = 0;
667
ae531041
L
668/* 1 if lfence should be inserted after every load. */
669static int lfence_after_load = 0;
670
671/* Non-zero if lfence should be inserted before indirect branch. */
672static enum lfence_before_indirect_branch_kind
673 {
674 lfence_branch_none = 0,
675 lfence_branch_register,
676 lfence_branch_memory,
677 lfence_branch_all
678 }
679lfence_before_indirect_branch;
680
681/* Non-zero if lfence should be inserted before ret. */
682static enum lfence_before_ret_kind
683 {
684 lfence_before_ret_none = 0,
685 lfence_before_ret_not,
a09f656b 686 lfence_before_ret_or,
687 lfence_before_ret_shl
ae531041
L
688 }
689lfence_before_ret;
690
0cb4071e
L
691/* 1 if the assembler should generate relax relocations. */
692
693static int generate_relax_relocations
694 = DEFAULT_GENERATE_X86_RELAX_RELOCATIONS;
695
7bab8ab5 696static enum check_kind
daf50ae7 697 {
7bab8ab5
JB
698 check_none = 0,
699 check_warning,
700 check_error
daf50ae7 701 }
7bab8ab5 702sse_check, operand_check = check_warning;
daf50ae7 703
e379e5f3
L
704/* Non-zero if branches should be aligned within power of 2 boundary. */
705static int align_branch_power = 0;
706
707/* Types of branches to align. */
708enum align_branch_kind
709 {
710 align_branch_none = 0,
711 align_branch_jcc = 1,
712 align_branch_fused = 2,
713 align_branch_jmp = 3,
714 align_branch_call = 4,
715 align_branch_indirect = 5,
716 align_branch_ret = 6
717 };
718
719/* Type bits of branches to align. */
720enum align_branch_bit
721 {
722 align_branch_jcc_bit = 1 << align_branch_jcc,
723 align_branch_fused_bit = 1 << align_branch_fused,
724 align_branch_jmp_bit = 1 << align_branch_jmp,
725 align_branch_call_bit = 1 << align_branch_call,
726 align_branch_indirect_bit = 1 << align_branch_indirect,
727 align_branch_ret_bit = 1 << align_branch_ret
728 };
729
730static unsigned int align_branch = (align_branch_jcc_bit
731 | align_branch_fused_bit
732 | align_branch_jmp_bit);
733
79d72f45
HL
734/* Types of condition jump used by macro-fusion. */
735enum mf_jcc_kind
736 {
737 mf_jcc_jo = 0, /* base opcode 0x70 */
738 mf_jcc_jc, /* base opcode 0x72 */
739 mf_jcc_je, /* base opcode 0x74 */
740 mf_jcc_jna, /* base opcode 0x76 */
741 mf_jcc_js, /* base opcode 0x78 */
742 mf_jcc_jp, /* base opcode 0x7a */
743 mf_jcc_jl, /* base opcode 0x7c */
744 mf_jcc_jle, /* base opcode 0x7e */
745 };
746
747/* Types of compare flag-modifying insntructions used by macro-fusion. */
748enum mf_cmp_kind
749 {
750 mf_cmp_test_and, /* test/cmp */
751 mf_cmp_alu_cmp, /* add/sub/cmp */
752 mf_cmp_incdec /* inc/dec */
753 };
754
e379e5f3
L
755/* The maximum padding size for fused jcc. CMP like instruction can
756 be 9 bytes and jcc can be 6 bytes. Leave room just in case for
757 prefixes. */
758#define MAX_FUSED_JCC_PADDING_SIZE 20
759
760/* The maximum number of prefixes added for an instruction. */
761static unsigned int align_branch_prefix_size = 5;
762
b6f8c7c4
L
763/* Optimization:
764 1. Clear the REX_W bit with register operand if possible.
765 2. Above plus use 128bit vector instruction to clear the full vector
766 register.
767 */
768static int optimize = 0;
769
770/* Optimization:
771 1. Clear the REX_W bit with register operand if possible.
772 2. Above plus use 128bit vector instruction to clear the full vector
773 register.
774 3. Above plus optimize "test{q,l,w} $imm8,%r{64,32,16}" to
775 "testb $imm7,%r8".
776 */
777static int optimize_for_space = 0;
778
2ca3ace5
L
779/* Register prefix used for error message. */
780static const char *register_prefix = "%";
781
47926f60
KH
782/* Used in 16 bit gcc mode to add an l suffix to call, ret, enter,
783 leave, push, and pop instructions so that gcc has the same stack
784 frame as in 32 bit mode. */
785static char stackop_size = '\0';
eecb386c 786
12b55ccc
L
787/* Non-zero to optimize code alignment. */
788int optimize_align_code = 1;
789
47926f60
KH
790/* Non-zero to quieten some warnings. */
791static int quiet_warnings = 0;
a38cf1db 792
d59a54c2
JB
793/* Guard to avoid repeated warnings about non-16-bit code on 16-bit CPUs. */
794static bool pre_386_16bit_warned;
795
47926f60
KH
796/* CPU name. */
797static const char *cpu_arch_name = NULL;
6305a203 798static char *cpu_sub_arch_name = NULL;
a38cf1db 799
47926f60 800/* CPU feature flags. */
cd75cd85 801i386_cpu_flags cpu_arch_flags = CPU_UNKNOWN_FLAGS;
40fb9820 802
3e624fa4
JB
803/* ISA extensions available in 64-bit mode only. */
804static const i386_cpu_flags cpu_64_flags = CPU_ANY_64_FLAGS;
805
ccc9c027
L
806/* If we have selected a cpu we are generating instructions for. */
807static int cpu_arch_tune_set = 0;
808
9103f4f4 809/* Cpu we are generating instructions for. */
fbf3f584 810enum processor_type cpu_arch_tune = PROCESSOR_UNKNOWN;
9103f4f4 811
ccc9c027 812/* CPU instruction set architecture used. */
fbf3f584 813enum processor_type cpu_arch_isa = PROCESSOR_UNKNOWN;
ccc9c027 814
9103f4f4 815/* CPU feature flags of instruction set architecture used. */
fbf3f584 816i386_cpu_flags cpu_arch_isa_flags;
9103f4f4 817
fddf5b5b
AM
818/* If set, conditional jumps are not automatically promoted to handle
819 larger than a byte offset. */
f68697e8 820static bool no_cond_jump_promotion = false;
fddf5b5b 821
5cc00775
JB
822/* This will be set from an expression parser hook if there's any
823 applicable operator involved in an expression. */
824static enum {
825 expr_operator_none,
826 expr_operator_present,
827 expr_large_value,
828} expr_mode;
829
c0f3af97
L
830/* Encode SSE instructions with VEX prefix. */
831static unsigned int sse2avx;
832
c8480b58
L
833/* Encode aligned vector move as unaligned vector move. */
834static unsigned int use_unaligned_vector_move;
835
4fc85f37 836/* Maximum permitted vector size. */
fa88a361
HJ
837#define VSZ128 0
838#define VSZ256 1
839#define VSZ512 2
4fc85f37
JB
840#define VSZ_DEFAULT VSZ512
841static unsigned int vector_size = VSZ_DEFAULT;
842
539f890d
L
843/* Encode scalar AVX instructions with specific vector length. */
844static enum
845 {
846 vex128 = 0,
847 vex256
848 } avxscalar;
849
03751133
L
850/* Encode VEX WIG instructions with specific vex.w. */
851static enum
852 {
853 vexw0 = 0,
854 vexw1
855 } vexwig;
856
43234a1e
L
857/* Encode scalar EVEX LIG instructions with specific vector length. */
858static enum
859 {
860 evexl128 = 0,
861 evexl256,
862 evexl512
863 } evexlig;
864
865/* Encode EVEX WIG instructions with specific evex.w. */
866static enum
867 {
868 evexw0 = 0,
869 evexw1
870 } evexwig;
871
d3d3c6db
IT
872/* Value to encode in EVEX RC bits, for SAE-only instructions. */
873static enum rc_type evexrcig = rne;
874
29b0f896 875/* Pre-defined "_GLOBAL_OFFSET_TABLE_". */
87c245cc 876static symbolS *GOT_symbol;
29b0f896 877
a4447b93
RH
878/* The dwarf2 return column, adjusted for 32 or 64 bit. */
879unsigned int x86_dwarf2_return_column;
880
881/* The dwarf2 data alignment, adjusted for 32 or 64 bit. */
882int x86_cie_data_alignment;
883
252b5132 884/* Interface to relax_segment.
fddf5b5b
AM
885 There are 3 major relax states for 386 jump insns because the
886 different types of jumps add different sizes to frags when we're
e379e5f3
L
887 figuring out what sort of jump to choose to reach a given label.
888
889 BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING are used to align
890 branches which are handled by md_estimate_size_before_relax() and
891 i386_generic_table_relax_frag(). */
252b5132 892
47926f60 893/* Types. */
93c2a809
AM
894#define UNCOND_JUMP 0
895#define COND_JUMP 1
896#define COND_JUMP86 2
e379e5f3
L
897#define BRANCH_PADDING 3
898#define BRANCH_PREFIX 4
899#define FUSED_JCC_PADDING 5
fddf5b5b 900
47926f60 901/* Sizes. */
252b5132
RH
902#define CODE16 1
903#define SMALL 0
29b0f896 904#define SMALL16 (SMALL | CODE16)
252b5132 905#define BIG 2
29b0f896 906#define BIG16 (BIG | CODE16)
252b5132
RH
907
908#ifndef INLINE
909#ifdef __GNUC__
910#define INLINE __inline__
911#else
912#define INLINE
913#endif
914#endif
915
fddf5b5b
AM
916#define ENCODE_RELAX_STATE(type, size) \
917 ((relax_substateT) (((type) << 2) | (size)))
918#define TYPE_FROM_RELAX_STATE(s) \
919 ((s) >> 2)
920#define DISP_SIZE_FROM_RELAX_STATE(s) \
921 ((((s) & 3) == BIG ? 4 : (((s) & 3) == BIG16 ? 2 : 1)))
252b5132
RH
922
923/* This table is used by relax_frag to promote short jumps to long
924 ones where necessary. SMALL (short) jumps may be promoted to BIG
925 (32 bit long) ones, and SMALL16 jumps to BIG16 (16 bit long). We
926 don't allow a short jump in a 32 bit code segment to be promoted to
927 a 16 bit offset jump because it's slower (requires data size
928 prefix), and doesn't work, unless the destination is in the bottom
929 64k of the code segment (The top 16 bits of eip are zeroed). */
930
931const relax_typeS md_relax_table[] =
932{
24eab124
AM
933 /* The fields are:
934 1) most positive reach of this state,
935 2) most negative reach of this state,
93c2a809 936 3) how many bytes this mode will have in the variable part of the frag
ce8a8b2f 937 4) which index into the table to try if we can't fit into this one. */
252b5132 938
fddf5b5b 939 /* UNCOND_JUMP states. */
93c2a809
AM
940 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG)},
941 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16)},
942 /* dword jmp adds 4 bytes to frag:
943 0 extra opcode bytes, 4 displacement bytes. */
252b5132 944 {0, 0, 4, 0},
93c2a809
AM
945 /* word jmp adds 2 byte2 to frag:
946 0 extra opcode bytes, 2 displacement bytes. */
252b5132
RH
947 {0, 0, 2, 0},
948
93c2a809
AM
949 /* COND_JUMP states. */
950 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG)},
951 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG16)},
952 /* dword conditionals adds 5 bytes to frag:
953 1 extra opcode byte, 4 displacement bytes. */
954 {0, 0, 5, 0},
fddf5b5b 955 /* word conditionals add 3 bytes to frag:
93c2a809
AM
956 1 extra opcode byte, 2 displacement bytes. */
957 {0, 0, 3, 0},
958
959 /* COND_JUMP86 states. */
960 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG)},
961 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG16)},
962 /* dword conditionals adds 5 bytes to frag:
963 1 extra opcode byte, 4 displacement bytes. */
964 {0, 0, 5, 0},
965 /* word conditionals add 4 bytes to frag:
966 1 displacement byte and a 3 byte long branch insn. */
967 {0, 0, 4, 0}
252b5132
RH
968};
969
6ceeed25 970#define ARCH(n, t, f, s) \
4fc85f37 971 { STRING_COMMA_LEN (#n), s, PROCESSOR_ ## t, vsz_none, CPU_ ## f ## _FLAGS, \
ae89daec
JB
972 CPU_NONE_FLAGS }
973#define SUBARCH(n, e, d, s) \
4fc85f37 974 { STRING_COMMA_LEN (#n), s, PROCESSOR_NONE, vsz_none, CPU_ ## e ## _FLAGS, \
ae89daec 975 CPU_ ## d ## _FLAGS }
4fc85f37
JB
976#define VECARCH(n, e, d, v) \
977 { STRING_COMMA_LEN (#n), false, PROCESSOR_NONE, vsz_ ## v, \
978 CPU_ ## e ## _FLAGS, CPU_ ## d ## _FLAGS }
6ceeed25 979
9103f4f4
L
980static const arch_entry cpu_arch[] =
981{
3ce2ebcf
JB
982 /* Do not replace the first two entries - i386_target_format() and
983 set_cpu_arch() rely on them being there in this order. */
6ceeed25
JB
984 ARCH (generic32, GENERIC32, GENERIC32, false),
985 ARCH (generic64, GENERIC64, GENERIC64, false),
986 ARCH (i8086, UNKNOWN, NONE, false),
4d97c5c8
JB
987 ARCH (i186, UNKNOWN, 186, false),
988 ARCH (i286, UNKNOWN, 286, false),
989 ARCH (i386, I386, 386, false),
990 ARCH (i486, I486, 486, false),
991 ARCH (i586, PENTIUM, 586, false),
4d97c5c8 992 ARCH (pentium, PENTIUM, 586, false),
c8be4b6f 993 ARCH (i686, I686, 686, false),
6ceeed25
JB
994 ARCH (pentiumpro, PENTIUMPRO, PENTIUMPRO, false),
995 ARCH (pentiumii, PENTIUMPRO, P2, false),
996 ARCH (pentiumiii, PENTIUMPRO, P3, false),
997 ARCH (pentium4, PENTIUM4, P4, false),
998 ARCH (prescott, NOCONA, CORE, false),
999 ARCH (nocona, NOCONA, NOCONA, false),
1000 ARCH (yonah, CORE, CORE, true),
1001 ARCH (core, CORE, CORE, false),
1002 ARCH (merom, CORE2, CORE2, true),
1003 ARCH (core2, CORE2, CORE2, false),
1004 ARCH (corei7, COREI7, COREI7, false),
1005 ARCH (iamcu, IAMCU, IAMCU, false),
1006 ARCH (k6, K6, K6, false),
1007 ARCH (k6_2, K6, K6_2, false),
1008 ARCH (athlon, ATHLON, ATHLON, false),
1009 ARCH (sledgehammer, K8, K8, true),
1010 ARCH (opteron, K8, K8, false),
1011 ARCH (k8, K8, K8, false),
1012 ARCH (amdfam10, AMDFAM10, AMDFAM10, false),
1013 ARCH (bdver1, BD, BDVER1, false),
1014 ARCH (bdver2, BD, BDVER2, false),
1015 ARCH (bdver3, BD, BDVER3, false),
1016 ARCH (bdver4, BD, BDVER4, false),
1017 ARCH (znver1, ZNVER, ZNVER1, false),
1018 ARCH (znver2, ZNVER, ZNVER2, false),
1019 ARCH (znver3, ZNVER, ZNVER3, false),
b0e8fa7f 1020 ARCH (znver4, ZNVER, ZNVER4, false),
b143c979 1021 ARCH (znver5, ZNVER, ZNVER5, false),
6ceeed25
JB
1022 ARCH (btver1, BT, BTVER1, false),
1023 ARCH (btver2, BT, BTVER2, false),
1024
4d97c5c8
JB
1025 SUBARCH (8087, 8087, ANY_8087, false),
1026 SUBARCH (87, NONE, ANY_8087, false), /* Disable only! */
ae89daec
JB
1027 SUBARCH (287, 287, ANY_287, false),
1028 SUBARCH (387, 387, ANY_387, false),
1029 SUBARCH (687, 687, ANY_687, false),
4d97c5c8 1030 SUBARCH (cmov, CMOV, CMOV, false),
88bd2203 1031 SUBARCH (fxsr, FXSR, ANY_FXSR, false),
ae89daec
JB
1032 SUBARCH (mmx, MMX, ANY_MMX, false),
1033 SUBARCH (sse, SSE, ANY_SSE, false),
1034 SUBARCH (sse2, SSE2, ANY_SSE2, false),
1035 SUBARCH (sse3, SSE3, ANY_SSE3, false),
1036 SUBARCH (sse4a, SSE4A, ANY_SSE4A, false),
1037 SUBARCH (ssse3, SSSE3, ANY_SSSE3, false),
1038 SUBARCH (sse4.1, SSE4_1, ANY_SSE4_1, false),
1039 SUBARCH (sse4.2, SSE4_2, ANY_SSE4_2, false),
1040 SUBARCH (sse4, SSE4_2, ANY_SSE4_1, false),
4fc85f37
JB
1041 VECARCH (avx, AVX, ANY_AVX, reset),
1042 VECARCH (avx2, AVX2, ANY_AVX2, reset),
1043 VECARCH (avx512f, AVX512F, ANY_AVX512F, reset),
1044 VECARCH (avx512cd, AVX512CD, ANY_AVX512CD, reset),
1045 VECARCH (avx512er, AVX512ER, ANY_AVX512ER, reset),
1046 VECARCH (avx512pf, AVX512PF, ANY_AVX512PF, reset),
1047 VECARCH (avx512dq, AVX512DQ, ANY_AVX512DQ, reset),
1048 VECARCH (avx512bw, AVX512BW, ANY_AVX512BW, reset),
1049 VECARCH (avx512vl, AVX512VL, ANY_AVX512VL, reset),
cafa5ef7 1050 SUBARCH (monitor, MONITOR, MONITOR, false),
25626f79
JB
1051 SUBARCH (vmx, VMX, ANY_VMX, false),
1052 SUBARCH (vmfunc, VMFUNC, ANY_VMFUNC, false),
ae89daec 1053 SUBARCH (smx, SMX, SMX, false),
5091b9ee
JB
1054 SUBARCH (xsave, XSAVE, ANY_XSAVE, false),
1055 SUBARCH (xsaveopt, XSAVEOPT, ANY_XSAVEOPT, false),
1056 SUBARCH (xsavec, XSAVEC, ANY_XSAVEC, false),
1057 SUBARCH (xsaves, XSAVES, ANY_XSAVES, false),
1058 SUBARCH (aes, AES, ANY_AES, false),
d54678eb
JB
1059 SUBARCH (pclmul, PCLMULQDQ, ANY_PCLMULQDQ, false),
1060 SUBARCH (clmul, PCLMULQDQ, ANY_PCLMULQDQ, true),
ae89daec
JB
1061 SUBARCH (fsgsbase, FSGSBASE, FSGSBASE, false),
1062 SUBARCH (rdrnd, RDRND, RDRND, false),
5091b9ee 1063 SUBARCH (f16c, F16C, ANY_F16C, false),
ae89daec 1064 SUBARCH (bmi2, BMI2, BMI2, false),
5091b9ee
JB
1065 SUBARCH (fma, FMA, ANY_FMA, false),
1066 SUBARCH (fma4, FMA4, ANY_FMA4, false),
1067 SUBARCH (xop, XOP, ANY_XOP, false),
1068 SUBARCH (lwp, LWP, ANY_LWP, false),
ae89daec
JB
1069 SUBARCH (movbe, MOVBE, MOVBE, false),
1070 SUBARCH (cx16, CX16, CX16, false),
c3bb24f5 1071 SUBARCH (lahf_sahf, LAHF_SAHF, LAHF_SAHF, false),
25626f79 1072 SUBARCH (ept, EPT, ANY_EPT, false),
ae89daec
JB
1073 SUBARCH (lzcnt, LZCNT, LZCNT, false),
1074 SUBARCH (popcnt, POPCNT, POPCNT, false),
1075 SUBARCH (hle, HLE, HLE, false),
760ab3d0
JB
1076 SUBARCH (rtm, RTM, ANY_RTM, false),
1077 SUBARCH (tsx, TSX, TSX, false),
ae89daec
JB
1078 SUBARCH (invpcid, INVPCID, INVPCID, false),
1079 SUBARCH (clflush, CLFLUSH, CLFLUSH, false),
1080 SUBARCH (nop, NOP, NOP, false),
1081 SUBARCH (syscall, SYSCALL, SYSCALL, false),
1082 SUBARCH (rdtscp, RDTSCP, RDTSCP, false),
5091b9ee
JB
1083 SUBARCH (3dnow, 3DNOW, ANY_3DNOW, false),
1084 SUBARCH (3dnowa, 3DNOWA, ANY_3DNOWA, false),
ae89daec 1085 SUBARCH (padlock, PADLOCK, PADLOCK, false),
0919e770
JB
1086 SUBARCH (pacifica, SVME, ANY_SVME, true),
1087 SUBARCH (svme, SVME, ANY_SVME, false),
ae89daec
JB
1088 SUBARCH (abm, ABM, ABM, false),
1089 SUBARCH (bmi, BMI, BMI, false),
1090 SUBARCH (tbm, TBM, TBM, false),
1091 SUBARCH (adx, ADX, ADX, false),
1092 SUBARCH (rdseed, RDSEED, RDSEED, false),
1093 SUBARCH (prfchw, PRFCHW, PRFCHW, false),
1094 SUBARCH (smap, SMAP, SMAP, false),
5091b9ee
JB
1095 SUBARCH (mpx, MPX, ANY_MPX, false),
1096 SUBARCH (sha, SHA, ANY_SHA, false),
ae89daec
JB
1097 SUBARCH (clflushopt, CLFLUSHOPT, CLFLUSHOPT, false),
1098 SUBARCH (prefetchwt1, PREFETCHWT1, PREFETCHWT1, false),
1099 SUBARCH (se1, SE1, SE1, false),
1100 SUBARCH (clwb, CLWB, CLWB, false),
4fc85f37
JB
1101 VECARCH (avx512ifma, AVX512IFMA, ANY_AVX512IFMA, reset),
1102 VECARCH (avx512vbmi, AVX512VBMI, ANY_AVX512VBMI, reset),
1103 VECARCH (avx512_4fmaps, AVX512_4FMAPS, ANY_AVX512_4FMAPS, reset),
1104 VECARCH (avx512_4vnniw, AVX512_4VNNIW, ANY_AVX512_4VNNIW, reset),
1105 VECARCH (avx512_vpopcntdq, AVX512_VPOPCNTDQ, ANY_AVX512_VPOPCNTDQ, reset),
1106 VECARCH (avx512_vbmi2, AVX512_VBMI2, ANY_AVX512_VBMI2, reset),
1107 VECARCH (avx512_vnni, AVX512_VNNI, ANY_AVX512_VNNI, reset),
1108 VECARCH (avx512_bitalg, AVX512_BITALG, ANY_AVX512_BITALG, reset),
1109 VECARCH (avx_vnni, AVX_VNNI, ANY_AVX_VNNI, reset),
ae89daec
JB
1110 SUBARCH (clzero, CLZERO, CLZERO, false),
1111 SUBARCH (mwaitx, MWAITX, MWAITX, false),
5091b9ee 1112 SUBARCH (ospke, OSPKE, ANY_OSPKE, false),
ae89daec
JB
1113 SUBARCH (rdpid, RDPID, RDPID, false),
1114 SUBARCH (ptwrite, PTWRITE, PTWRITE, false),
4d97c5c8
JB
1115 SUBARCH (ibt, IBT, IBT, false),
1116 SUBARCH (shstk, SHSTK, SHSTK, false),
88bd2203 1117 SUBARCH (gfni, GFNI, ANY_GFNI, false),
4fc85f37
JB
1118 VECARCH (vaes, VAES, ANY_VAES, reset),
1119 VECARCH (vpclmulqdq, VPCLMULQDQ, ANY_VPCLMULQDQ, reset),
ae89daec
JB
1120 SUBARCH (wbnoinvd, WBNOINVD, WBNOINVD, false),
1121 SUBARCH (pconfig, PCONFIG, PCONFIG, false),
1122 SUBARCH (waitpkg, WAITPKG, WAITPKG, false),
1123 SUBARCH (cldemote, CLDEMOTE, CLDEMOTE, false),
1124 SUBARCH (amx_int8, AMX_INT8, ANY_AMX_INT8, false),
1125 SUBARCH (amx_bf16, AMX_BF16, ANY_AMX_BF16, false),
5091b9ee 1126 SUBARCH (amx_fp16, AMX_FP16, ANY_AMX_FP16, false),
d100d8c1 1127 SUBARCH (amx_complex, AMX_COMPLEX, ANY_AMX_COMPLEX, false),
ae89daec 1128 SUBARCH (amx_tile, AMX_TILE, ANY_AMX_TILE, false),
4d97c5c8
JB
1129 SUBARCH (movdiri, MOVDIRI, MOVDIRI, false),
1130 SUBARCH (movdir64b, MOVDIR64B, MOVDIR64B, false),
4fc85f37
JB
1131 VECARCH (avx512_bf16, AVX512_BF16, ANY_AVX512_BF16, reset),
1132 VECARCH (avx512_vp2intersect, AVX512_VP2INTERSECT,
1133 ANY_AVX512_VP2INTERSECT, reset),
4d97c5c8
JB
1134 SUBARCH (tdx, TDX, TDX, false),
1135 SUBARCH (enqcmd, ENQCMD, ENQCMD, false),
1136 SUBARCH (serialize, SERIALIZE, SERIALIZE, false),
ae89daec
JB
1137 SUBARCH (rdpru, RDPRU, RDPRU, false),
1138 SUBARCH (mcommit, MCOMMIT, MCOMMIT, false),
0919e770 1139 SUBARCH (sev_es, SEV_ES, ANY_SEV_ES, false),
760ab3d0 1140 SUBARCH (tsxldtrk, TSXLDTRK, ANY_TSXLDTRK, false),
88bd2203
JB
1141 SUBARCH (kl, KL, ANY_KL, false),
1142 SUBARCH (widekl, WIDEKL, ANY_WIDEKL, false),
4d97c5c8
JB
1143 SUBARCH (uintr, UINTR, UINTR, false),
1144 SUBARCH (hreset, HRESET, HRESET, false),
4fc85f37 1145 VECARCH (avx512_fp16, AVX512_FP16, ANY_AVX512_FP16, reset),
ef07be45 1146 SUBARCH (prefetchi, PREFETCHI, PREFETCHI, false),
4fc85f37
JB
1147 VECARCH (avx_ifma, AVX_IFMA, ANY_AVX_IFMA, reset),
1148 VECARCH (avx_vnni_int8, AVX_VNNI_INT8, ANY_AVX_VNNI_INT8, reset),
4d97c5c8
JB
1149 SUBARCH (cmpccxadd, CMPCCXADD, CMPCCXADD, false),
1150 SUBARCH (wrmsrns, WRMSRNS, WRMSRNS, false),
1151 SUBARCH (msrlist, MSRLIST, MSRLIST, false),
4fc85f37 1152 VECARCH (avx_ne_convert, AVX_NE_CONVERT, ANY_AVX_NE_CONVERT, reset),
4d97c5c8 1153 SUBARCH (rao_int, RAO_INT, RAO_INT, false),
0919e770 1154 SUBARCH (rmpquery, RMPQUERY, ANY_RMPQUERY, false),
c88ed92f
ZJ
1155 SUBARCH (fred, FRED, ANY_FRED, false),
1156 SUBARCH (lkgs, LKGS, ANY_LKGS, false),
4fc85f37
JB
1157 VECARCH (avx_vnni_int16, AVX_VNNI_INT16, ANY_AVX_VNNI_INT16, reset),
1158 VECARCH (sha512, SHA512, ANY_SHA512, reset),
1159 VECARCH (sm3, SM3, ANY_SM3, reset),
1160 VECARCH (sm4, SM4, ANY_SM4, reset),
b5c37946 1161 SUBARCH (pbndkb, PBNDKB, PBNDKB, false),
4fc85f37 1162 VECARCH (avx10.1, AVX10_1, ANY_AVX512F, set),
8170af78 1163 SUBARCH (user_msr, USER_MSR, USER_MSR, false),
80d61d8d 1164 SUBARCH (apx_f, APX_F, APX_F, false),
293f5f65
L
1165};
1166
6ceeed25
JB
1167#undef SUBARCH
1168#undef ARCH
1169
704209c0 1170#ifdef I386COFF
a6c24e68
NC
1171/* Like s_lcomm_internal in gas/read.c but the alignment string
1172 is allowed to be optional. */
1173
1174static symbolS *
1175pe_lcomm_internal (int needs_align, symbolS *symbolP, addressT size)
1176{
1177 addressT align = 0;
1178
1179 SKIP_WHITESPACE ();
1180
7ab9ffdd 1181 if (needs_align
a6c24e68
NC
1182 && *input_line_pointer == ',')
1183 {
1184 align = parse_align (needs_align - 1);
7ab9ffdd 1185
a6c24e68
NC
1186 if (align == (addressT) -1)
1187 return NULL;
1188 }
1189 else
1190 {
1191 if (size >= 8)
1192 align = 3;
1193 else if (size >= 4)
1194 align = 2;
1195 else if (size >= 2)
1196 align = 1;
1197 else
1198 align = 0;
1199 }
1200
1201 bss_alloc (symbolP, size, align);
1202 return symbolP;
1203}
1204
704209c0 1205static void
a6c24e68
NC
1206pe_lcomm (int needs_align)
1207{
1208 s_comm_internal (needs_align * 2, pe_lcomm_internal);
1209}
704209c0 1210#endif
a6c24e68 1211
29b0f896
AM
1212const pseudo_typeS md_pseudo_table[] =
1213{
1214#if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
1215 {"align", s_align_bytes, 0},
1216#else
1217 {"align", s_align_ptwo, 0},
1218#endif
1219 {"arch", set_cpu_arch, 0},
d4064aad 1220#ifdef OBJ_AOUT
29b0f896 1221 {"bss", s_bss, 0},
d4064aad
JB
1222#endif
1223#ifdef I386COFF
a6c24e68 1224 {"lcomm", pe_lcomm, 1},
29b0f896
AM
1225#endif
1226 {"ffloat", float_cons, 'f'},
1227 {"dfloat", float_cons, 'd'},
1228 {"tfloat", float_cons, 'x'},
7d19d096 1229 {"hfloat", float_cons, 'h'},
de133cf9 1230 {"bfloat16", float_cons, 'b'},
29b0f896 1231 {"value", cons, 2},
d182319b 1232 {"slong", signed_cons, 4},
edd67638 1233 {"insn", s_insn, 0},
1e7dd4a0 1234 {"noopt", s_noopt, 0},
29b0f896
AM
1235 {"optim", s_ignore, 0},
1236 {"code16gcc", set_16bit_gcc_code_flag, CODE_16BIT},
1237 {"code16", set_code_flag, CODE_16BIT},
1238 {"code32", set_code_flag, CODE_32BIT},
da5f19a2 1239#ifdef BFD64
29b0f896 1240 {"code64", set_code_flag, CODE_64BIT},
da5f19a2 1241#endif
29b0f896
AM
1242 {"intel_syntax", set_intel_syntax, 1},
1243 {"att_syntax", set_intel_syntax, 0},
1efbbeb4
L
1244 {"intel_mnemonic", set_intel_mnemonic, 1},
1245 {"att_mnemonic", set_intel_mnemonic, 0},
db51cc60
L
1246 {"allow_index_reg", set_allow_index_reg, 1},
1247 {"disallow_index_reg", set_allow_index_reg, 0},
7bab8ab5
JB
1248 {"sse_check", set_check, 0},
1249 {"operand_check", set_check, 1},
3b22753a
L
1250#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1251 {"largecomm", handle_large_common, 0},
07a53e5c 1252#else
68d20676 1253 {"file", dwarf2_directive_file, 0},
07a53e5c
RH
1254 {"loc", dwarf2_directive_loc, 0},
1255 {"loc_mark_labels", dwarf2_directive_loc_mark_labels, 0},
3b22753a 1256#endif
6482c264
NC
1257#ifdef TE_PE
1258 {"secrel32", pe_directive_secrel, 0},
145667f8 1259 {"secidx", pe_directive_secidx, 0},
6482c264 1260#endif
29b0f896
AM
1261 {0, 0, 0}
1262};
1263
1264/* For interface with expression (). */
1265extern char *input_line_pointer;
1266
1267/* Hash table for instruction mnemonic lookup. */
629310ab 1268static htab_t op_hash;
29b0f896
AM
1269
1270/* Hash table for register lookup. */
629310ab 1271static htab_t reg_hash;
29b0f896 1272\f
ce8a8b2f
AM
1273 /* Various efficient no-op patterns for aligning code labels.
1274 Note: Don't try to assemble the instructions in the comments.
1275 0L and 0w are not legal. */
62a02d25
L
1276static const unsigned char f32_1[] =
1277 {0x90}; /* nop */
1278static const unsigned char f32_2[] =
1279 {0x66,0x90}; /* xchg %ax,%ax */
1280static const unsigned char f32_3[] =
1281 {0x8d,0x76,0x00}; /* leal 0(%esi),%esi */
ad9f3230
JB
1282#define f32_4 (f32_5 + 1) /* leal 0(%esi,%eiz),%esi */
1283static const unsigned char f32_5[] =
1284 {0x2e,0x8d,0x74,0x26,0x00}; /* leal %cs:0(%esi,%eiz),%esi */
62a02d25
L
1285static const unsigned char f32_6[] =
1286 {0x8d,0xb6,0x00,0x00,0x00,0x00}; /* leal 0L(%esi),%esi */
ad9f3230
JB
1287#define f32_7 (f32_8 + 1) /* leal 0L(%esi,%eiz),%esi */
1288static const unsigned char f32_8[] =
1289 {0x2e,0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal %cs:0L(%esi,%eiz),%esi */
d164359d
JB
1290static const unsigned char f64_3[] =
1291 {0x48,0x89,0xf6}; /* mov %rsi,%rsi */
1292static const unsigned char f64_4[] =
1293 {0x48,0x8d,0x76,0x00}; /* lea 0(%rsi),%rsi */
1294#define f64_5 (f64_6 + 1) /* lea 0(%rsi,%riz),%rsi */
1295static const unsigned char f64_6[] =
1296 {0x2e,0x48,0x8d,0x74,0x26,0x00}; /* lea %cs:0(%rsi,%riz),%rsi */
1297static const unsigned char f64_7[] =
1298 {0x48,0x8d,0xb6,0x00,0x00,0x00,0x00}; /* lea 0L(%rsi),%rsi */
1299#define f64_8 (f64_9 + 1) /* lea 0L(%rsi,%riz),%rsi */
1300static const unsigned char f64_9[] =
1301 {0x2e,0x48,0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* lea %cs:0L(%rsi,%riz),%rsi */
5e0729b6 1302#define f16_2 (f64_3 + 1) /* mov %si,%si */
62a02d25 1303static const unsigned char f16_3[] =
3ae729d5 1304 {0x8d,0x74,0x00}; /* lea 0(%si),%si */
ad9f3230
JB
1305#define f16_4 (f16_5 + 1) /* lea 0W(%si),%si */
1306static const unsigned char f16_5[] =
1307 {0x2e,0x8d,0xb4,0x00,0x00}; /* lea %cs:0W(%si),%si */
3ae729d5
L
1308static const unsigned char jump_disp8[] =
1309 {0xeb}; /* jmp disp8 */
1310static const unsigned char jump32_disp32[] =
1311 {0xe9}; /* jmp disp32 */
1312static const unsigned char jump16_disp32[] =
1313 {0x66,0xe9}; /* jmp disp32 */
62a02d25
L
1314/* 32-bit NOPs patterns. */
1315static const unsigned char *const f32_patt[] = {
ad9f3230 1316 f32_1, f32_2, f32_3, f32_4, f32_5, f32_6, f32_7, f32_8
62a02d25 1317};
d164359d
JB
1318/* 64-bit NOPs patterns. */
1319static const unsigned char *const f64_patt[] = {
1320 f32_1, f32_2, f64_3, f64_4, f64_5, f64_6, f64_7, f64_8, f64_9
1321};
62a02d25
L
1322/* 16-bit NOPs patterns. */
1323static const unsigned char *const f16_patt[] = {
ad9f3230 1324 f32_1, f16_2, f16_3, f16_4, f16_5
62a02d25
L
1325};
1326/* nopl (%[re]ax) */
1327static const unsigned char alt_3[] =
1328 {0x0f,0x1f,0x00};
1329/* nopl 0(%[re]ax) */
1330static const unsigned char alt_4[] =
1331 {0x0f,0x1f,0x40,0x00};
1332/* nopl 0(%[re]ax,%[re]ax,1) */
ae7067fb 1333#define alt_5 (alt_6 + 1)
62a02d25
L
1334/* nopw 0(%[re]ax,%[re]ax,1) */
1335static const unsigned char alt_6[] =
1336 {0x66,0x0f,0x1f,0x44,0x00,0x00};
1337/* nopl 0L(%[re]ax) */
1338static const unsigned char alt_7[] =
1339 {0x0f,0x1f,0x80,0x00,0x00,0x00,0x00};
1340/* nopl 0L(%[re]ax,%[re]ax,1) */
ae7067fb 1341#define alt_8 (alt_9 + 1)
62a02d25
L
1342/* nopw 0L(%[re]ax,%[re]ax,1) */
1343static const unsigned char alt_9[] =
1344 {0x66,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1345/* nopw %cs:0L(%[re]ax,%[re]ax,1) */
ae7067fb 1346#define alt_10 (alt_11 + 1)
3ae729d5
L
1347/* data16 nopw %cs:0L(%eax,%eax,1) */
1348static const unsigned char alt_11[] =
1349 {0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
62a02d25
L
1350/* 32-bit and 64-bit NOPs patterns. */
1351static const unsigned char *const alt_patt[] = {
1352 f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
3ae729d5 1353 alt_9, alt_10, alt_11
62a02d25
L
1354};
1355
1356/* Genenerate COUNT bytes of NOPs to WHERE from PATT with the maximum
1357 size of a single NOP instruction MAX_SINGLE_NOP_SIZE. */
1358
1359static void
1360i386_output_nops (char *where, const unsigned char *const *patt,
1361 int count, int max_single_nop_size)
1362
1363{
3ae729d5
L
1364 /* Place the longer NOP first. */
1365 int last;
1366 int offset;
3076e594
NC
1367 const unsigned char *nops;
1368
1369 if (max_single_nop_size < 1)
1370 {
1371 as_fatal (_("i386_output_nops called to generate nops of at most %d bytes!"),
1372 max_single_nop_size);
1373 return;
1374 }
1375
1376 nops = patt[max_single_nop_size - 1];
3ae729d5
L
1377 last = count % max_single_nop_size;
1378
1379 count -= last;
1380 for (offset = 0; offset < count; offset += max_single_nop_size)
1381 memcpy (where + offset, nops, max_single_nop_size);
1382
1383 if (last)
1384 {
1385 nops = patt[last - 1];
ad9f3230 1386 memcpy (where + offset, nops, last);
3ae729d5 1387 }
62a02d25
L
1388}
1389
3ae729d5
L
1390static INLINE int
1391fits_in_imm7 (offsetT num)
1392{
1393 return (num & 0x7f) == num;
1394}
1395
1396static INLINE int
1397fits_in_imm31 (offsetT num)
1398{
1399 return (num & 0x7fffffff) == num;
1400}
62a02d25
L
1401
1402/* Genenerate COUNT bytes of NOPs to WHERE with the maximum size of a
1403 single NOP instruction LIMIT. */
1404
1405void
3ae729d5 1406i386_generate_nops (fragS *fragP, char *where, offsetT count, int limit)
62a02d25 1407{
3ae729d5 1408 const unsigned char *const *patt = NULL;
62a02d25 1409 int max_single_nop_size;
3ae729d5
L
1410 /* Maximum number of NOPs before switching to jump over NOPs. */
1411 int max_number_of_nops;
62a02d25 1412
3ae729d5 1413 switch (fragP->fr_type)
62a02d25 1414 {
3ae729d5
L
1415 case rs_fill_nop:
1416 case rs_align_code:
1417 break;
e379e5f3
L
1418 case rs_machine_dependent:
1419 /* Allow NOP padding for jumps and calls. */
1420 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
1421 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
1422 break;
1423 /* Fall through. */
3ae729d5 1424 default:
62a02d25
L
1425 return;
1426 }
1427
ccc9c027
L
1428 /* We need to decide which NOP sequence to use for 32bit and
1429 64bit. When -mtune= is used:
4eed87de 1430
c8be4b6f 1431 1. For PROCESSOR_I?86, PROCESSOR_PENTIUM, PROCESSOR_IAMCU, and
76bc74dc 1432 PROCESSOR_GENERIC32, f32_patt will be used.
80b8656c
L
1433 2. For the rest, alt_patt will be used.
1434
1435 When -mtune= isn't used, alt_patt will be used if
d164359d 1436 cpu_arch_isa_flags has CpuNop. Otherwise, f32_patt/f64_patt will
76bc74dc 1437 be used.
ccc9c027
L
1438
1439 When -march= or .arch is used, we can't use anything beyond
1440 cpu_arch_isa_flags. */
1441
d12c7ab8 1442 if (fragP->tc_frag_data.code == CODE_16BIT)
ccc9c027 1443 {
3ae729d5
L
1444 patt = f16_patt;
1445 max_single_nop_size = sizeof (f16_patt) / sizeof (f16_patt[0]);
1446 /* Limit number of NOPs to 2 in 16-bit mode. */
1447 max_number_of_nops = 2;
252b5132 1448 }
33fef721 1449 else
ccc9c027 1450 {
d164359d 1451 patt = fragP->tc_frag_data.code == CODE_64BIT ? f64_patt : f32_patt;
fbf3f584 1452 if (fragP->tc_frag_data.isa == PROCESSOR_UNKNOWN)
ccc9c027 1453 {
cd75cd85
JB
1454 /* PROCESSOR_UNKNOWN means that all ISAs may be used, unless
1455 explicitly disabled. */
d12c7ab8 1456 switch (fragP->tc_frag_data.tune)
ccc9c027
L
1457 {
1458 case PROCESSOR_UNKNOWN:
1459 /* We use cpu_arch_isa_flags to check if we SHOULD
22109423 1460 optimize with nops. */
9f314ead 1461 if (fragP->tc_frag_data.isanop)
80b8656c 1462 patt = alt_patt;
ccc9c027 1463 break;
c8be4b6f
JB
1464
1465 case PROCESSOR_PENTIUMPRO:
ccc9c027
L
1466 case PROCESSOR_PENTIUM4:
1467 case PROCESSOR_NOCONA:
ef05d495 1468 case PROCESSOR_CORE:
76bc74dc 1469 case PROCESSOR_CORE2:
bd5295b2 1470 case PROCESSOR_COREI7:
76bc74dc 1471 case PROCESSOR_GENERIC64:
ccc9c027
L
1472 case PROCESSOR_K6:
1473 case PROCESSOR_ATHLON:
1474 case PROCESSOR_K8:
4eed87de 1475 case PROCESSOR_AMDFAM10:
8aedb9fe 1476 case PROCESSOR_BD:
029f3522 1477 case PROCESSOR_ZNVER:
7b458c12 1478 case PROCESSOR_BT:
cd75cd85
JB
1479 if (fragP->tc_frag_data.cpunop)
1480 patt = alt_patt;
ccc9c027 1481 break;
cd75cd85 1482
76bc74dc 1483 case PROCESSOR_I386:
ccc9c027
L
1484 case PROCESSOR_I486:
1485 case PROCESSOR_PENTIUM:
c8be4b6f 1486 case PROCESSOR_I686:
81486035 1487 case PROCESSOR_IAMCU:
ccc9c027 1488 case PROCESSOR_GENERIC32:
ccc9c027 1489 break;
c368d2a8
JB
1490 case PROCESSOR_NONE:
1491 abort ();
4eed87de 1492 }
ccc9c027
L
1493 }
1494 else
1495 {
fbf3f584 1496 switch (fragP->tc_frag_data.tune)
ccc9c027
L
1497 {
1498 case PROCESSOR_UNKNOWN:
e6a14101 1499 /* When cpu_arch_isa is set, cpu_arch_tune shouldn't be
ccc9c027
L
1500 PROCESSOR_UNKNOWN. */
1501 abort ();
1502 break;
1503
cd75cd85 1504 default:
ccc9c027 1505 /* We use cpu_arch_isa_flags to check if we CAN optimize
22109423 1506 with nops. */
9f314ead 1507 if (fragP->tc_frag_data.isanop)
80b8656c 1508 patt = alt_patt;
ccc9c027 1509 break;
cd75cd85 1510
c368d2a8
JB
1511 case PROCESSOR_NONE:
1512 abort ();
4eed87de 1513 }
ccc9c027
L
1514 }
1515
d164359d 1516 if (patt != alt_patt)
76bc74dc 1517 {
d164359d
JB
1518 max_single_nop_size = patt == f32_patt ? ARRAY_SIZE (f32_patt)
1519 : ARRAY_SIZE (f64_patt);
3ae729d5
L
1520 /* Limit number of NOPs to 2 for older processors. */
1521 max_number_of_nops = 2;
76bc74dc
L
1522 }
1523 else
1524 {
3ae729d5
L
1525 max_single_nop_size = sizeof (alt_patt) / sizeof (alt_patt[0]);
1526 /* Limit number of NOPs to 7 for newer processors. */
1527 max_number_of_nops = 7;
1528 }
1529 }
1530
1531 if (limit == 0)
1532 limit = max_single_nop_size;
1533
1534 if (fragP->fr_type == rs_fill_nop)
1535 {
1536 /* Output NOPs for .nop directive. */
1537 if (limit > max_single_nop_size)
1538 {
1539 as_bad_where (fragP->fr_file, fragP->fr_line,
1540 _("invalid single nop size: %d "
1541 "(expect within [0, %d])"),
1542 limit, max_single_nop_size);
1543 return;
1544 }
1545 }
e379e5f3 1546 else if (fragP->fr_type != rs_machine_dependent)
3ae729d5
L
1547 fragP->fr_var = count;
1548
0aa5d0c9
JB
1549 /* Emit a plain NOP first when the last thing we saw may not have been
1550 a proper instruction (e.g. a stand-alone prefix or .byte). */
1551 if (!fragP->tc_frag_data.last_insn_normal)
1552 {
1553 *where++ = 0x90;
1554 --count;
1555 }
1556
3ae729d5
L
1557 if ((count / max_single_nop_size) > max_number_of_nops)
1558 {
1559 /* Generate jump over NOPs. */
1560 offsetT disp = count - 2;
1561 if (fits_in_imm7 (disp))
1562 {
1563 /* Use "jmp disp8" if possible. */
1564 count = disp;
1565 where[0] = jump_disp8[0];
1566 where[1] = count;
1567 where += 2;
1568 }
1569 else
1570 {
1571 unsigned int size_of_jump;
1572
1573 if (flag_code == CODE_16BIT)
1574 {
1575 where[0] = jump16_disp32[0];
1576 where[1] = jump16_disp32[1];
1577 size_of_jump = 2;
1578 }
1579 else
1580 {
1581 where[0] = jump32_disp32[0];
1582 size_of_jump = 1;
1583 }
1584
1585 count -= size_of_jump + 4;
1586 if (!fits_in_imm31 (count))
1587 {
1588 as_bad_where (fragP->fr_file, fragP->fr_line,
1589 _("jump over nop padding out of range"));
1590 return;
1591 }
1592
1593 md_number_to_chars (where + size_of_jump, count, 4);
1594 where += size_of_jump + 4;
76bc74dc 1595 }
ccc9c027 1596 }
3ae729d5
L
1597
1598 /* Generate multiple NOPs. */
1599 i386_output_nops (where, patt, count, limit);
252b5132
RH
1600}
1601
c6fb90c8 1602static INLINE int
0dfbf9d7 1603operand_type_all_zero (const union i386_operand_type *x)
40fb9820 1604{
0dfbf9d7 1605 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1606 {
1607 case 3:
0dfbf9d7 1608 if (x->array[2])
c6fb90c8 1609 return 0;
1a0670f3 1610 /* Fall through. */
c6fb90c8 1611 case 2:
0dfbf9d7 1612 if (x->array[1])
c6fb90c8 1613 return 0;
1a0670f3 1614 /* Fall through. */
c6fb90c8 1615 case 1:
0dfbf9d7 1616 return !x->array[0];
c6fb90c8
L
1617 default:
1618 abort ();
1619 }
40fb9820
L
1620}
1621
c6fb90c8 1622static INLINE void
0dfbf9d7 1623operand_type_set (union i386_operand_type *x, unsigned int v)
40fb9820 1624{
0dfbf9d7 1625 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1626 {
1627 case 3:
0dfbf9d7 1628 x->array[2] = v;
1a0670f3 1629 /* Fall through. */
c6fb90c8 1630 case 2:
0dfbf9d7 1631 x->array[1] = v;
1a0670f3 1632 /* Fall through. */
c6fb90c8 1633 case 1:
0dfbf9d7 1634 x->array[0] = v;
1a0670f3 1635 /* Fall through. */
c6fb90c8
L
1636 break;
1637 default:
1638 abort ();
1639 }
bab6aec1
JB
1640
1641 x->bitfield.class = ClassNone;
75e5731b 1642 x->bitfield.instance = InstanceNone;
c6fb90c8 1643}
40fb9820 1644
c6fb90c8 1645static INLINE int
0dfbf9d7
L
1646operand_type_equal (const union i386_operand_type *x,
1647 const union i386_operand_type *y)
c6fb90c8 1648{
0dfbf9d7 1649 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1650 {
1651 case 3:
0dfbf9d7 1652 if (x->array[2] != y->array[2])
c6fb90c8 1653 return 0;
1a0670f3 1654 /* Fall through. */
c6fb90c8 1655 case 2:
0dfbf9d7 1656 if (x->array[1] != y->array[1])
c6fb90c8 1657 return 0;
1a0670f3 1658 /* Fall through. */
c6fb90c8 1659 case 1:
0dfbf9d7 1660 return x->array[0] == y->array[0];
c6fb90c8
L
1661 break;
1662 default:
1663 abort ();
1664 }
1665}
40fb9820 1666
734dfd1c 1667static INLINE bool
a5e91879 1668_is_cpu (const i386_cpu_attr *a, enum i386_cpu cpu)
734dfd1c
JB
1669{
1670 switch (cpu)
1671 {
a5e91879
JB
1672 case Cpu287: return a->bitfield.cpu287;
1673 case Cpu387: return a->bitfield.cpu387;
1674 case Cpu3dnow: return a->bitfield.cpu3dnow;
1675 case Cpu3dnowA: return a->bitfield.cpu3dnowa;
1676 case CpuAVX: return a->bitfield.cpuavx;
1677 case CpuHLE: return a->bitfield.cpuhle;
1678 case CpuAVX512F: return a->bitfield.cpuavx512f;
1679 case CpuAVX512VL: return a->bitfield.cpuavx512vl;
80d61d8d 1680 case CpuAPX_F: return a->bitfield.cpuapx_f;
a5e91879
JB
1681 case Cpu64: return a->bitfield.cpu64;
1682 case CpuNo64: return a->bitfield.cpuno64;
734dfd1c
JB
1683 default:
1684 gas_assert (cpu < CpuAttrEnums);
1685 }
a5e91879
JB
1686 return a->bitfield.isa == cpu + 1u;
1687}
1688
1689static INLINE bool
1690is_cpu (const insn_template *t, enum i386_cpu cpu)
1691{
1692 return _is_cpu(&t->cpu, cpu);
1693}
1694
1695static INLINE bool
1696maybe_cpu (const insn_template *t, enum i386_cpu cpu)
1697{
1698 return _is_cpu(&t->cpu_any, cpu);
734dfd1c
JB
1699}
1700
1701static i386_cpu_flags cpu_flags_from_attr (i386_cpu_attr a)
1702{
1703 const unsigned int bps = sizeof (a.array[0]) * CHAR_BIT;
1704 i386_cpu_flags f = { .array[0] = 0 };
1705
a12915cc 1706 switch (ARRAY_SIZE (a.array))
734dfd1c
JB
1707 {
1708 case 1:
1709 f.array[CpuAttrEnums / bps]
a12915cc
JB
1710#ifndef WORDS_BIGENDIAN
1711 |= (a.array[0] >> CpuIsaBits) << (CpuAttrEnums % bps);
1712#else
1713 |= (a.array[0] << CpuIsaBits) >> (CpuAttrEnums % bps);
1714#endif
1715 if (CpuMax / bps > CpuAttrEnums / bps)
734dfd1c 1716 f.array[CpuAttrEnums / bps + 1]
a12915cc 1717#ifndef WORDS_BIGENDIAN
734dfd1c 1718 = (a.array[0] >> CpuIsaBits) >> (bps - CpuAttrEnums % bps);
a12915cc
JB
1719#else
1720 = (a.array[0] << CpuIsaBits) << (bps - CpuAttrEnums % bps);
1721#endif
734dfd1c 1722 break;
a12915cc 1723
734dfd1c
JB
1724 default:
1725 abort ();
1726 }
1727
1728 if (a.bitfield.isa)
a12915cc 1729#ifndef WORDS_BIGENDIAN
734dfd1c 1730 f.array[(a.bitfield.isa - 1) / bps] |= 1u << ((a.bitfield.isa - 1) % bps);
a12915cc
JB
1731#else
1732 f.array[(a.bitfield.isa - 1) / bps] |= 1u << (~(a.bitfield.isa - 1) % bps);
1733#endif
734dfd1c
JB
1734
1735 return f;
1736}
1737
0dfbf9d7
L
1738static INLINE int
1739cpu_flags_all_zero (const union i386_cpu_flags *x)
1740{
1741 switch (ARRAY_SIZE(x->array))
1742 {
75f8266a
KL
1743 case 5:
1744 if (x->array[4])
1745 return 0;
1746 /* Fall through. */
53467f57
IT
1747 case 4:
1748 if (x->array[3])
1749 return 0;
1750 /* Fall through. */
0dfbf9d7
L
1751 case 3:
1752 if (x->array[2])
1753 return 0;
1a0670f3 1754 /* Fall through. */
0dfbf9d7
L
1755 case 2:
1756 if (x->array[1])
1757 return 0;
1a0670f3 1758 /* Fall through. */
0dfbf9d7
L
1759 case 1:
1760 return !x->array[0];
1761 default:
1762 abort ();
1763 }
1764}
1765
0dfbf9d7
L
1766static INLINE int
1767cpu_flags_equal (const union i386_cpu_flags *x,
1768 const union i386_cpu_flags *y)
1769{
1770 switch (ARRAY_SIZE(x->array))
1771 {
75f8266a
KL
1772 case 5:
1773 if (x->array[4] != y->array[4])
1774 return 0;
1775 /* Fall through. */
53467f57
IT
1776 case 4:
1777 if (x->array[3] != y->array[3])
1778 return 0;
1779 /* Fall through. */
0dfbf9d7
L
1780 case 3:
1781 if (x->array[2] != y->array[2])
1782 return 0;
1a0670f3 1783 /* Fall through. */
0dfbf9d7
L
1784 case 2:
1785 if (x->array[1] != y->array[1])
1786 return 0;
1a0670f3 1787 /* Fall through. */
0dfbf9d7
L
1788 case 1:
1789 return x->array[0] == y->array[0];
1790 break;
1791 default:
1792 abort ();
1793 }
1794}
c6fb90c8
L
1795
1796static INLINE int
c0260ac6 1797cpu_flags_check_cpu64 (const insn_template *t)
c6fb90c8 1798{
c0260ac6
JB
1799 return flag_code == CODE_64BIT
1800 ? !t->cpu.bitfield.cpuno64
1801 : !t->cpu.bitfield.cpu64;
40fb9820
L
1802}
1803
c6fb90c8
L
1804static INLINE i386_cpu_flags
1805cpu_flags_and (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1806{
c6fb90c8
L
1807 switch (ARRAY_SIZE (x.array))
1808 {
75f8266a
KL
1809 case 5:
1810 x.array [4] &= y.array [4];
1811 /* Fall through. */
53467f57
IT
1812 case 4:
1813 x.array [3] &= y.array [3];
1814 /* Fall through. */
c6fb90c8
L
1815 case 3:
1816 x.array [2] &= y.array [2];
1a0670f3 1817 /* Fall through. */
c6fb90c8
L
1818 case 2:
1819 x.array [1] &= y.array [1];
1a0670f3 1820 /* Fall through. */
c6fb90c8
L
1821 case 1:
1822 x.array [0] &= y.array [0];
1823 break;
1824 default:
1825 abort ();
1826 }
1827 return x;
1828}
40fb9820 1829
c6fb90c8
L
1830static INLINE i386_cpu_flags
1831cpu_flags_or (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1832{
c6fb90c8 1833 switch (ARRAY_SIZE (x.array))
40fb9820 1834 {
75f8266a
KL
1835 case 5:
1836 x.array [4] |= y.array [4];
1837 /* Fall through. */
53467f57
IT
1838 case 4:
1839 x.array [3] |= y.array [3];
1840 /* Fall through. */
c6fb90c8
L
1841 case 3:
1842 x.array [2] |= y.array [2];
1a0670f3 1843 /* Fall through. */
c6fb90c8
L
1844 case 2:
1845 x.array [1] |= y.array [1];
1a0670f3 1846 /* Fall through. */
c6fb90c8
L
1847 case 1:
1848 x.array [0] |= y.array [0];
40fb9820
L
1849 break;
1850 default:
1851 abort ();
1852 }
40fb9820
L
1853 return x;
1854}
1855
309d3373
JB
1856static INLINE i386_cpu_flags
1857cpu_flags_and_not (i386_cpu_flags x, i386_cpu_flags y)
1858{
1859 switch (ARRAY_SIZE (x.array))
1860 {
75f8266a
KL
1861 case 5:
1862 x.array [4] &= ~y.array [4];
1863 /* Fall through. */
53467f57
IT
1864 case 4:
1865 x.array [3] &= ~y.array [3];
1866 /* Fall through. */
309d3373
JB
1867 case 3:
1868 x.array [2] &= ~y.array [2];
1a0670f3 1869 /* Fall through. */
309d3373
JB
1870 case 2:
1871 x.array [1] &= ~y.array [1];
1a0670f3 1872 /* Fall through. */
309d3373
JB
1873 case 1:
1874 x.array [0] &= ~y.array [0];
1875 break;
1876 default:
1877 abort ();
1878 }
1879 return x;
1880}
1881
6c0946d0
JB
1882static const i386_cpu_flags avx512 = CPU_ANY_AVX512F_FLAGS;
1883
6177c84d 1884static INLINE bool need_evex_encoding (const insn_template *t)
a6f3add0 1885{
e346d50a
JB
1886 return i.encoding == encoding_evex
1887 || i.encoding == encoding_evex512
eb3f3841 1888 || (t->opcode_modifier.vex && i.encoding == encoding_egpr)
a6f3add0
JB
1889 || i.mask.reg;
1890}
1891
c0f3af97
L
1892#define CPU_FLAGS_ARCH_MATCH 0x1
1893#define CPU_FLAGS_64BIT_MATCH 0x2
1894
c0f3af97 1895#define CPU_FLAGS_PERFECT_MATCH \
db12e14e 1896 (CPU_FLAGS_ARCH_MATCH | CPU_FLAGS_64BIT_MATCH)
c0f3af97
L
1897
1898/* Return CPU flags match bits. */
3629bb00 1899
40fb9820 1900static int
d3ce72d0 1901cpu_flags_match (const insn_template *t)
40fb9820 1902{
a5e91879
JB
1903 i386_cpu_flags cpu, active, all = cpu_flags_from_attr (t->cpu);
1904 i386_cpu_flags any = cpu_flags_from_attr (t->cpu_any);
c0260ac6 1905 int match = cpu_flags_check_cpu64 (t) ? CPU_FLAGS_64BIT_MATCH : 0;
40fb9820 1906
a5e91879
JB
1907 all.bitfield.cpu64 = 0;
1908 all.bitfield.cpuno64 = 0;
1909 gas_assert (!any.bitfield.cpu64);
1910 gas_assert (!any.bitfield.cpuno64);
40fb9820 1911
a5e91879 1912 if (cpu_flags_all_zero (&all) && cpu_flags_all_zero (&any))
c0f3af97
L
1913 {
1914 /* This instruction is available on all archs. */
a5e91879 1915 return match | CPU_FLAGS_ARCH_MATCH;
c0f3af97 1916 }
3e624fa4 1917
a5e91879 1918 /* This instruction is available only on some archs. */
3629bb00 1919
a5e91879
JB
1920 /* Dual VEX/EVEX templates may need stripping of one of the flags. */
1921 if (t->opcode_modifier.vex && t->opcode_modifier.evex)
1922 {
1923 /* Dual AVX/AVX512 templates need to retain AVX512* only if we already
1924 know that EVEX encoding will be needed. */
1925 if ((any.bitfield.cpuavx || any.bitfield.cpuavx2 || any.bitfield.cpufma)
1926 && (any.bitfield.cpuavx512f || any.bitfield.cpuavx512vl))
a6f3add0 1927 {
6177c84d 1928 if (need_evex_encoding (t))
a6f3add0 1929 {
a5e91879
JB
1930 any.bitfield.cpuavx = 0;
1931 any.bitfield.cpuavx2 = 0;
1932 any.bitfield.cpufma = 0;
1933 }
6177c84d 1934 /* need_evex_encoding(t) isn't reliable before operands were
a5e91879
JB
1935 parsed. */
1936 else if (i.operands)
1937 {
1938 any.bitfield.cpuavx512f = 0;
1939 any.bitfield.cpuavx512vl = 0;
a6f3add0
JB
1940 }
1941 }
7c3df3c6
JB
1942
1943 /* Dual non-APX/APX templates need massaging from what APX_F() in the
1944 opcode table has produced. While the direct transformation of the
1945 incoming cpuid&(cpuid|APX_F) would be to cpuid&(cpuid) / cpuid&(APX_F)
1946 respectively, it's cheaper to move to just cpuid / cpuid&APX_F
1947 instead. */
1948 if (any.bitfield.cpuapx_f
1949 && (any.bitfield.cpubmi || any.bitfield.cpubmi2
1950 || any.bitfield.cpuavx512f || any.bitfield.cpuavx512bw
1951 || any.bitfield.cpuavx512dq || any.bitfield.cpuamx_tile
5190fa38 1952 || any.bitfield.cpucmpccxadd || any.bitfield.cpuuser_msr))
7c3df3c6
JB
1953 {
1954 /* These checks (verifying that APX_F() was properly used in the
1955 opcode table entry) make sure there's no need for an "else" to
1956 the "if()" below. */
1957 gas_assert (!cpu_flags_all_zero (&all));
1958 cpu = cpu_flags_and (all, any);
1959 gas_assert (cpu_flags_equal (&cpu, &all));
1960
1961 if (need_evex_encoding (t))
1962 all = any;
1963
1964 memset (&any, 0, sizeof (any));
1965 }
a5e91879 1966 }
a6f3add0 1967
a5e91879
JB
1968 if (flag_code != CODE_64BIT)
1969 active = cpu_flags_and_not (cpu_arch_flags, cpu_64_flags);
1970 else
1971 active = cpu_arch_flags;
1972 cpu = cpu_flags_and (all, active);
1973 if (cpu_flags_equal (&cpu, &all))
1974 {
22c36940
JB
1975 /* AVX and AVX2 present at the same time express an operand size
1976 dependency - strip AVX2 for the purposes here. The operand size
1977 dependent check occurs in check_vecOperands(). */
a5e91879
JB
1978 if (any.bitfield.cpuavx && any.bitfield.cpuavx2)
1979 any.bitfield.cpuavx2 = 0;
22c36940 1980
a5e91879
JB
1981 cpu = cpu_flags_and (any, active);
1982 if (cpu_flags_all_zero (&any) || !cpu_flags_all_zero (&cpu))
c0f3af97 1983 {
a5e91879 1984 if (all.bitfield.cpuavx)
f94f390e 1985 {
a5e91879
JB
1986 /* We need to check SSE2AVX with AVX. */
1987 if (!t->opcode_modifier.sse2avx
1988 || (sse2avx && !i.prefix[DATA_PREFIX]))
929f69fa
JB
1989 match |= CPU_FLAGS_ARCH_MATCH;
1990 }
a5ff0eb2 1991 else
db12e14e 1992 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 1993 }
3629bb00 1994 }
c0f3af97 1995 return match;
40fb9820
L
1996}
1997
c6fb90c8
L
1998static INLINE i386_operand_type
1999operand_type_and (i386_operand_type x, i386_operand_type y)
40fb9820 2000{
bab6aec1
JB
2001 if (x.bitfield.class != y.bitfield.class)
2002 x.bitfield.class = ClassNone;
75e5731b
JB
2003 if (x.bitfield.instance != y.bitfield.instance)
2004 x.bitfield.instance = InstanceNone;
bab6aec1 2005
c6fb90c8
L
2006 switch (ARRAY_SIZE (x.array))
2007 {
2008 case 3:
2009 x.array [2] &= y.array [2];
1a0670f3 2010 /* Fall through. */
c6fb90c8
L
2011 case 2:
2012 x.array [1] &= y.array [1];
1a0670f3 2013 /* Fall through. */
c6fb90c8
L
2014 case 1:
2015 x.array [0] &= y.array [0];
2016 break;
2017 default:
2018 abort ();
2019 }
2020 return x;
40fb9820
L
2021}
2022
73053c1f
JB
2023static INLINE i386_operand_type
2024operand_type_and_not (i386_operand_type x, i386_operand_type y)
2025{
bab6aec1 2026 gas_assert (y.bitfield.class == ClassNone);
75e5731b 2027 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 2028
73053c1f
JB
2029 switch (ARRAY_SIZE (x.array))
2030 {
2031 case 3:
2032 x.array [2] &= ~y.array [2];
2033 /* Fall through. */
2034 case 2:
2035 x.array [1] &= ~y.array [1];
2036 /* Fall through. */
2037 case 1:
2038 x.array [0] &= ~y.array [0];
2039 break;
2040 default:
2041 abort ();
2042 }
2043 return x;
2044}
2045
c6fb90c8
L
2046static INLINE i386_operand_type
2047operand_type_or (i386_operand_type x, i386_operand_type y)
40fb9820 2048{
bab6aec1
JB
2049 gas_assert (x.bitfield.class == ClassNone ||
2050 y.bitfield.class == ClassNone ||
2051 x.bitfield.class == y.bitfield.class);
75e5731b
JB
2052 gas_assert (x.bitfield.instance == InstanceNone ||
2053 y.bitfield.instance == InstanceNone ||
2054 x.bitfield.instance == y.bitfield.instance);
bab6aec1 2055
c6fb90c8 2056 switch (ARRAY_SIZE (x.array))
40fb9820 2057 {
c6fb90c8
L
2058 case 3:
2059 x.array [2] |= y.array [2];
1a0670f3 2060 /* Fall through. */
c6fb90c8
L
2061 case 2:
2062 x.array [1] |= y.array [1];
1a0670f3 2063 /* Fall through. */
c6fb90c8
L
2064 case 1:
2065 x.array [0] |= y.array [0];
40fb9820
L
2066 break;
2067 default:
2068 abort ();
2069 }
c6fb90c8
L
2070 return x;
2071}
40fb9820 2072
c6fb90c8
L
2073static INLINE i386_operand_type
2074operand_type_xor (i386_operand_type x, i386_operand_type y)
2075{
bab6aec1 2076 gas_assert (y.bitfield.class == ClassNone);
75e5731b 2077 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 2078
c6fb90c8
L
2079 switch (ARRAY_SIZE (x.array))
2080 {
2081 case 3:
2082 x.array [2] ^= y.array [2];
1a0670f3 2083 /* Fall through. */
c6fb90c8
L
2084 case 2:
2085 x.array [1] ^= y.array [1];
1a0670f3 2086 /* Fall through. */
c6fb90c8
L
2087 case 1:
2088 x.array [0] ^= y.array [0];
2089 break;
2090 default:
2091 abort ();
2092 }
40fb9820
L
2093 return x;
2094}
2095
05909f23
JB
2096static const i386_operand_type anydisp = {
2097 .bitfield = { .disp8 = 1, .disp16 = 1, .disp32 = 1, .disp64 = 1 }
2098};
40fb9820
L
2099
2100enum operand_type
2101{
2102 reg,
40fb9820
L
2103 imm,
2104 disp,
2105 anymem
2106};
2107
c6fb90c8 2108static INLINE int
40fb9820
L
2109operand_type_check (i386_operand_type t, enum operand_type c)
2110{
2111 switch (c)
2112 {
2113 case reg:
bab6aec1 2114 return t.bitfield.class == Reg;
40fb9820 2115
40fb9820
L
2116 case imm:
2117 return (t.bitfield.imm8
2118 || t.bitfield.imm8s
2119 || t.bitfield.imm16
2120 || t.bitfield.imm32
2121 || t.bitfield.imm32s
2122 || t.bitfield.imm64);
2123
2124 case disp:
2125 return (t.bitfield.disp8
2126 || t.bitfield.disp16
2127 || t.bitfield.disp32
40fb9820
L
2128 || t.bitfield.disp64);
2129
2130 case anymem:
2131 return (t.bitfield.disp8
2132 || t.bitfield.disp16
2133 || t.bitfield.disp32
40fb9820
L
2134 || t.bitfield.disp64
2135 || t.bitfield.baseindex);
2136
2137 default:
2138 abort ();
2139 }
2cfe26b6
AM
2140
2141 return 0;
40fb9820
L
2142}
2143
7a54636a
L
2144/* Return 1 if there is no conflict in 8bit/16bit/32bit/64bit/80bit size
2145 between operand GIVEN and opeand WANTED for instruction template T. */
5c07affc
L
2146
2147static INLINE int
7a54636a
L
2148match_operand_size (const insn_template *t, unsigned int wanted,
2149 unsigned int given)
5c07affc 2150{
3ac21baa
JB
2151 return !((i.types[given].bitfield.byte
2152 && !t->operand_types[wanted].bitfield.byte)
2153 || (i.types[given].bitfield.word
2154 && !t->operand_types[wanted].bitfield.word)
2155 || (i.types[given].bitfield.dword
2156 && !t->operand_types[wanted].bitfield.dword)
2157 || (i.types[given].bitfield.qword
9db83a32
JB
2158 && (!t->operand_types[wanted].bitfield.qword
2159 /* Don't allow 64-bit (memory) operands outside of 64-bit
2160 mode, when they're used where a 64-bit GPR could also
2161 be used. Checking is needed for Intel Syntax only. */
2162 || (intel_syntax
2163 && flag_code != CODE_64BIT
2164 && (t->operand_types[wanted].bitfield.class == Reg
2165 || t->operand_types[wanted].bitfield.class == Accum
2166 || t->opcode_modifier.isstring))))
3ac21baa
JB
2167 || (i.types[given].bitfield.tbyte
2168 && !t->operand_types[wanted].bitfield.tbyte));
5c07affc
L
2169}
2170
dd40ce22
L
2171/* Return 1 if there is no conflict in SIMD register between operand
2172 GIVEN and opeand WANTED for instruction template T. */
1b54b8d7
JB
2173
2174static INLINE int
dd40ce22
L
2175match_simd_size (const insn_template *t, unsigned int wanted,
2176 unsigned int given)
1b54b8d7 2177{
3ac21baa
JB
2178 return !((i.types[given].bitfield.xmmword
2179 && !t->operand_types[wanted].bitfield.xmmword)
2180 || (i.types[given].bitfield.ymmword
2181 && !t->operand_types[wanted].bitfield.ymmword)
2182 || (i.types[given].bitfield.zmmword
260cd341
LC
2183 && !t->operand_types[wanted].bitfield.zmmword)
2184 || (i.types[given].bitfield.tmmword
2185 && !t->operand_types[wanted].bitfield.tmmword));
1b54b8d7
JB
2186}
2187
7a54636a
L
2188/* Return 1 if there is no conflict in any size between operand GIVEN
2189 and opeand WANTED for instruction template T. */
5c07affc
L
2190
2191static INLINE int
dd40ce22
L
2192match_mem_size (const insn_template *t, unsigned int wanted,
2193 unsigned int given)
5c07affc 2194{
7a54636a 2195 return (match_operand_size (t, wanted, given)
3ac21baa 2196 && !((i.types[given].bitfield.unspecified
5273a3cd 2197 && !i.broadcast.type
a5748e0d 2198 && !i.broadcast.bytes
3ac21baa
JB
2199 && !t->operand_types[wanted].bitfield.unspecified)
2200 || (i.types[given].bitfield.fword
2201 && !t->operand_types[wanted].bitfield.fword)
1b54b8d7
JB
2202 /* For scalar opcode templates to allow register and memory
2203 operands at the same time, some special casing is needed
d6793fa1
JB
2204 here. Also for v{,p}broadcast*, {,v}pmov{s,z}*, and
2205 down-conversion vpmov*. */
3528c362 2206 || ((t->operand_types[wanted].bitfield.class == RegSIMD
bc49bfd8
JB
2207 && t->operand_types[wanted].bitfield.byte
2208 + t->operand_types[wanted].bitfield.word
2209 + t->operand_types[wanted].bitfield.dword
2210 + t->operand_types[wanted].bitfield.qword
2211 > !!t->opcode_modifier.broadcast)
3ac21baa
JB
2212 ? (i.types[given].bitfield.xmmword
2213 || i.types[given].bitfield.ymmword
2214 || i.types[given].bitfield.zmmword)
2215 : !match_simd_size(t, wanted, given))));
5c07affc
L
2216}
2217
3ac21baa
JB
2218/* Return value has MATCH_STRAIGHT set if there is no size conflict on any
2219 operands for instruction template T, and it has MATCH_REVERSE set if there
2220 is no size conflict on any operands for the template with operands reversed
2221 (and the template allows for reversing in the first place). */
5c07affc 2222
3ac21baa
JB
2223#define MATCH_STRAIGHT 1
2224#define MATCH_REVERSE 2
2225
2226static INLINE unsigned int
d3ce72d0 2227operand_size_match (const insn_template *t)
5c07affc 2228{
3ac21baa 2229 unsigned int j, match = MATCH_STRAIGHT;
5c07affc 2230
0cfa3eb3 2231 /* Don't check non-absolute jump instructions. */
5c07affc 2232 if (t->opcode_modifier.jump
0cfa3eb3 2233 && t->opcode_modifier.jump != JUMP_ABSOLUTE)
5c07affc
L
2234 return match;
2235
2236 /* Check memory and accumulator operand size. */
2237 for (j = 0; j < i.operands; j++)
2238 {
3528c362
JB
2239 if (i.types[j].bitfield.class != Reg
2240 && i.types[j].bitfield.class != RegSIMD
255571cd 2241 && t->opcode_modifier.operandconstraint == ANY_SIZE)
5c07affc
L
2242 continue;
2243
bab6aec1 2244 if (t->operand_types[j].bitfield.class == Reg
7a54636a 2245 && !match_operand_size (t, j, j))
5c07affc
L
2246 {
2247 match = 0;
2248 break;
2249 }
2250
3528c362 2251 if (t->operand_types[j].bitfield.class == RegSIMD
3ac21baa 2252 && !match_simd_size (t, j, j))
1b54b8d7
JB
2253 {
2254 match = 0;
2255 break;
2256 }
2257
75e5731b 2258 if (t->operand_types[j].bitfield.instance == Accum
7a54636a 2259 && (!match_operand_size (t, j, j) || !match_simd_size (t, j, j)))
1b54b8d7
JB
2260 {
2261 match = 0;
2262 break;
2263 }
2264
c48dadc9 2265 if ((i.flags[j] & Operand_Mem) && !match_mem_size (t, j, j))
5c07affc
L
2266 {
2267 match = 0;
2268 break;
2269 }
2270 }
2271
3ac21baa 2272 if (!t->opcode_modifier.d)
7b94647a 2273 return match;
5c07affc
L
2274
2275 /* Check reverse. */
aa180741 2276 gas_assert (i.operands >= 2);
5c07affc 2277
f5eb1d70 2278 for (j = 0; j < i.operands; j++)
5c07affc 2279 {
f5eb1d70
JB
2280 unsigned int given = i.operands - j - 1;
2281
aa180741 2282 /* For FMA4 and XOP insns VEX.W controls just the first two
3083f376 2283 register operands. And APX_F insns just swap the two source operands,
2284 with the 3rd one being the destination. */
2285 if (is_cpu (t, CpuFMA4) || is_cpu (t, CpuXOP)
2286 || is_cpu (t, CpuAPX_F))
8bd915b7
JB
2287 given = j < 2 ? 1 - j : j;
2288
bab6aec1 2289 if (t->operand_types[j].bitfield.class == Reg
f5eb1d70 2290 && !match_operand_size (t, j, given))
7b94647a 2291 return match;
5c07affc 2292
3528c362 2293 if (t->operand_types[j].bitfield.class == RegSIMD
f5eb1d70 2294 && !match_simd_size (t, j, given))
7b94647a 2295 return match;
dbbc8b7e 2296
75e5731b 2297 if (t->operand_types[j].bitfield.instance == Accum
f5eb1d70
JB
2298 && (!match_operand_size (t, j, given)
2299 || !match_simd_size (t, j, given)))
7b94647a 2300 return match;
dbbc8b7e 2301
f5eb1d70 2302 if ((i.flags[given] & Operand_Mem) && !match_mem_size (t, j, given))
7b94647a 2303 return match;
5c07affc
L
2304 }
2305
3ac21baa 2306 return match | MATCH_REVERSE;
5c07affc
L
2307}
2308
c6fb90c8 2309static INLINE int
40fb9820
L
2310operand_type_match (i386_operand_type overlap,
2311 i386_operand_type given)
2312{
2313 i386_operand_type temp = overlap;
2314
7d5e4556 2315 temp.bitfield.unspecified = 0;
5c07affc
L
2316 temp.bitfield.byte = 0;
2317 temp.bitfield.word = 0;
2318 temp.bitfield.dword = 0;
2319 temp.bitfield.fword = 0;
2320 temp.bitfield.qword = 0;
2321 temp.bitfield.tbyte = 0;
2322 temp.bitfield.xmmword = 0;
c0f3af97 2323 temp.bitfield.ymmword = 0;
43234a1e 2324 temp.bitfield.zmmword = 0;
260cd341 2325 temp.bitfield.tmmword = 0;
0dfbf9d7 2326 if (operand_type_all_zero (&temp))
891edac4 2327 goto mismatch;
40fb9820 2328
6f2f06be 2329 if (given.bitfield.baseindex == overlap.bitfield.baseindex)
891edac4
L
2330 return 1;
2331
dc1e8a47 2332 mismatch:
a65babc9 2333 i.error = operand_type_mismatch;
891edac4 2334 return 0;
40fb9820
L
2335}
2336
7d5e4556 2337/* If given types g0 and g1 are registers they must be of the same type
10c17abd 2338 unless the expected operand type register overlap is null.
8ee52bcf 2339 Intel syntax sized memory operands are also checked here. */
40fb9820 2340
c6fb90c8 2341static INLINE int
dc821c5f 2342operand_type_register_match (i386_operand_type g0,
40fb9820 2343 i386_operand_type t0,
40fb9820
L
2344 i386_operand_type g1,
2345 i386_operand_type t1)
2346{
bab6aec1 2347 if (g0.bitfield.class != Reg
3528c362 2348 && g0.bitfield.class != RegSIMD
8ee52bcf
JB
2349 && (g0.bitfield.unspecified
2350 || !operand_type_check (g0, anymem)))
40fb9820
L
2351 return 1;
2352
bab6aec1 2353 if (g1.bitfield.class != Reg
3528c362 2354 && g1.bitfield.class != RegSIMD
8ee52bcf
JB
2355 && (g1.bitfield.unspecified
2356 || !operand_type_check (g1, anymem)))
40fb9820
L
2357 return 1;
2358
dc821c5f
JB
2359 if (g0.bitfield.byte == g1.bitfield.byte
2360 && g0.bitfield.word == g1.bitfield.word
2361 && g0.bitfield.dword == g1.bitfield.dword
10c17abd
JB
2362 && g0.bitfield.qword == g1.bitfield.qword
2363 && g0.bitfield.xmmword == g1.bitfield.xmmword
2364 && g0.bitfield.ymmword == g1.bitfield.ymmword
2365 && g0.bitfield.zmmword == g1.bitfield.zmmword)
40fb9820
L
2366 return 1;
2367
c4d09633
JB
2368 /* If expectations overlap in no more than a single size, all is fine. */
2369 g0 = operand_type_and (t0, t1);
2370 if (g0.bitfield.byte
2371 + g0.bitfield.word
2372 + g0.bitfield.dword
2373 + g0.bitfield.qword
2374 + g0.bitfield.xmmword
2375 + g0.bitfield.ymmword
2376 + g0.bitfield.zmmword <= 1)
891edac4
L
2377 return 1;
2378
a65babc9 2379 i.error = register_type_mismatch;
891edac4
L
2380
2381 return 0;
40fb9820
L
2382}
2383
4c692bc7
JB
2384static INLINE unsigned int
2385register_number (const reg_entry *r)
2386{
2387 unsigned int nr = r->reg_num;
2388
2389 if (r->reg_flags & RegRex)
2390 nr += 8;
2391
80d61d8d 2392 if (r->reg_flags & (RegVRex | RegRex2))
200cbe0f
L
2393 nr += 16;
2394
4c692bc7
JB
2395 return nr;
2396}
2397
252b5132 2398static INLINE unsigned int
40fb9820 2399mode_from_disp_size (i386_operand_type t)
252b5132 2400{
b5014f7a 2401 if (t.bitfield.disp8)
40fb9820
L
2402 return 1;
2403 else if (t.bitfield.disp16
a775efc8 2404 || t.bitfield.disp32)
40fb9820
L
2405 return 2;
2406 else
2407 return 0;
252b5132
RH
2408}
2409
2410static INLINE int
65879393 2411fits_in_signed_byte (addressT num)
252b5132 2412{
65879393 2413 return num + 0x80 <= 0xff;
47926f60 2414}
252b5132
RH
2415
2416static INLINE int
65879393 2417fits_in_unsigned_byte (addressT num)
252b5132 2418{
65879393 2419 return num <= 0xff;
47926f60 2420}
252b5132
RH
2421
2422static INLINE int
65879393 2423fits_in_unsigned_word (addressT num)
252b5132 2424{
65879393 2425 return num <= 0xffff;
47926f60 2426}
252b5132
RH
2427
2428static INLINE int
65879393 2429fits_in_signed_word (addressT num)
252b5132 2430{
65879393 2431 return num + 0x8000 <= 0xffff;
47926f60 2432}
2a962e6d 2433
3e73aa7c 2434static INLINE int
65879393 2435fits_in_signed_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2436{
2437#ifndef BFD64
2438 return 1;
2439#else
65879393 2440 return num + 0x80000000 <= 0xffffffff;
3e73aa7c
JH
2441#endif
2442} /* fits_in_signed_long() */
2a962e6d 2443
3e73aa7c 2444static INLINE int
65879393 2445fits_in_unsigned_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2446{
2447#ifndef BFD64
2448 return 1;
2449#else
65879393 2450 return num <= 0xffffffff;
3e73aa7c
JH
2451#endif
2452} /* fits_in_unsigned_long() */
252b5132 2453
a442cac5
JB
2454static INLINE valueT extend_to_32bit_address (addressT num)
2455{
2456#ifdef BFD64
2457 if (fits_in_unsigned_long(num))
2458 return (num ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
2459
2460 if (!fits_in_signed_long (num))
2461 return num & 0xffffffff;
2462#endif
2463
2464 return num;
2465}
2466
43234a1e 2467static INLINE int
b5014f7a 2468fits_in_disp8 (offsetT num)
43234a1e
L
2469{
2470 int shift = i.memshift;
2471 unsigned int mask;
2472
2473 if (shift == -1)
2474 abort ();
2475
2476 mask = (1 << shift) - 1;
2477
2478 /* Return 0 if NUM isn't properly aligned. */
2479 if ((num & mask))
2480 return 0;
2481
2482 /* Check if NUM will fit in 8bit after shift. */
2483 return fits_in_signed_byte (num >> shift);
2484}
2485
a683cc34
SP
2486static INLINE int
2487fits_in_imm4 (offsetT num)
2488{
0ff3b7d0 2489 /* Despite the name, check for imm3 if we're dealing with EVEX. */
eb3f3841
JB
2490 return (num & (i.encoding != encoding_evex
2491 && i.encoding != encoding_egpr ? 0xf : 7)) == num;
a683cc34
SP
2492}
2493
40fb9820 2494static i386_operand_type
e3bb37b5 2495smallest_imm_type (offsetT num)
252b5132 2496{
40fb9820 2497 i386_operand_type t;
7ab9ffdd 2498
0dfbf9d7 2499 operand_type_set (&t, 0);
40fb9820
L
2500 t.bitfield.imm64 = 1;
2501
2502 if (cpu_arch_tune != PROCESSOR_I486 && num == 1)
e413e4e9
AM
2503 {
2504 /* This code is disabled on the 486 because all the Imm1 forms
2505 in the opcode table are slower on the i486. They're the
2506 versions with the implicitly specified single-position
2507 displacement, which has another syntax if you really want to
2508 use that form. */
40fb9820
L
2509 t.bitfield.imm1 = 1;
2510 t.bitfield.imm8 = 1;
2511 t.bitfield.imm8s = 1;
2512 t.bitfield.imm16 = 1;
2513 t.bitfield.imm32 = 1;
2514 t.bitfield.imm32s = 1;
2515 }
2516 else if (fits_in_signed_byte (num))
2517 {
c34d1cc9
JB
2518 if (fits_in_unsigned_byte (num))
2519 t.bitfield.imm8 = 1;
40fb9820
L
2520 t.bitfield.imm8s = 1;
2521 t.bitfield.imm16 = 1;
8170af78
HL
2522 if (flag_code != CODE_64BIT || fits_in_unsigned_long (num))
2523 t.bitfield.imm32 = 1;
40fb9820
L
2524 t.bitfield.imm32s = 1;
2525 }
2526 else if (fits_in_unsigned_byte (num))
2527 {
2528 t.bitfield.imm8 = 1;
2529 t.bitfield.imm16 = 1;
2530 t.bitfield.imm32 = 1;
2531 t.bitfield.imm32s = 1;
2532 }
2533 else if (fits_in_signed_word (num) || fits_in_unsigned_word (num))
2534 {
2535 t.bitfield.imm16 = 1;
8170af78
HL
2536 if (flag_code != CODE_64BIT || fits_in_unsigned_long (num))
2537 t.bitfield.imm32 = 1;
40fb9820
L
2538 t.bitfield.imm32s = 1;
2539 }
2540 else if (fits_in_signed_long (num))
2541 {
8170af78
HL
2542 if (flag_code != CODE_64BIT || fits_in_unsigned_long (num))
2543 t.bitfield.imm32 = 1;
40fb9820
L
2544 t.bitfield.imm32s = 1;
2545 }
2546 else if (fits_in_unsigned_long (num))
2547 t.bitfield.imm32 = 1;
2548
2549 return t;
47926f60 2550}
252b5132 2551
847f7ad4 2552static offsetT
e3bb37b5 2553offset_in_range (offsetT val, int size)
847f7ad4 2554{
508866be 2555 addressT mask;
ba2adb93 2556
847f7ad4
AM
2557 switch (size)
2558 {
508866be
L
2559 case 1: mask = ((addressT) 1 << 8) - 1; break;
2560 case 2: mask = ((addressT) 1 << 16) - 1; break;
3e73aa7c 2561#ifdef BFD64
64965897 2562 case 4: mask = ((addressT) 1 << 32) - 1; break;
3e73aa7c 2563#endif
64965897 2564 case sizeof (val): return val;
47926f60 2565 default: abort ();
847f7ad4
AM
2566 }
2567
4fe51f7d 2568 if ((val & ~mask) != 0 && (-val & ~mask) != 0)
f493c217
AM
2569 as_warn (_("0x%" PRIx64 " shortened to 0x%" PRIx64),
2570 (uint64_t) val, (uint64_t) (val & mask));
847f7ad4 2571
847f7ad4
AM
2572 return val & mask;
2573}
2574
76d3f746
JB
2575static INLINE const char *insn_name (const insn_template *t)
2576{
5c139202 2577 return &i386_mnemonics[t->mnem_off];
76d3f746
JB
2578}
2579
c32fa91d
L
2580enum PREFIX_GROUP
2581{
2582 PREFIX_EXIST = 0,
2583 PREFIX_LOCK,
2584 PREFIX_REP,
04ef582a 2585 PREFIX_DS,
c32fa91d
L
2586 PREFIX_OTHER
2587};
2588
2589/* Returns
2590 a. PREFIX_EXIST if attempting to add a prefix where one from the
2591 same class already exists.
2592 b. PREFIX_LOCK if lock prefix is added.
2593 c. PREFIX_REP if rep/repne prefix is added.
04ef582a
L
2594 d. PREFIX_DS if ds prefix is added.
2595 e. PREFIX_OTHER if other prefix is added.
c32fa91d
L
2596 */
2597
2598static enum PREFIX_GROUP
e3bb37b5 2599add_prefix (unsigned int prefix)
252b5132 2600{
c32fa91d 2601 enum PREFIX_GROUP ret = PREFIX_OTHER;
b1905489 2602 unsigned int q;
252b5132 2603
29b0f896
AM
2604 if (prefix >= REX_OPCODE && prefix < REX_OPCODE + 16
2605 && flag_code == CODE_64BIT)
b1905489 2606 {
161a04f6 2607 if ((i.prefix[REX_PREFIX] & prefix & REX_W)
44846f29
JB
2608 || (i.prefix[REX_PREFIX] & prefix & REX_R)
2609 || (i.prefix[REX_PREFIX] & prefix & REX_X)
2610 || (i.prefix[REX_PREFIX] & prefix & REX_B))
c32fa91d 2611 ret = PREFIX_EXIST;
b1905489
JB
2612 q = REX_PREFIX;
2613 }
3e73aa7c 2614 else
b1905489
JB
2615 {
2616 switch (prefix)
2617 {
2618 default:
2619 abort ();
2620
b1905489 2621 case DS_PREFIX_OPCODE:
04ef582a
L
2622 ret = PREFIX_DS;
2623 /* Fall through. */
2624 case CS_PREFIX_OPCODE:
b1905489
JB
2625 case ES_PREFIX_OPCODE:
2626 case FS_PREFIX_OPCODE:
2627 case GS_PREFIX_OPCODE:
2628 case SS_PREFIX_OPCODE:
2629 q = SEG_PREFIX;
2630 break;
2631
2632 case REPNE_PREFIX_OPCODE:
2633 case REPE_PREFIX_OPCODE:
c32fa91d
L
2634 q = REP_PREFIX;
2635 ret = PREFIX_REP;
2636 break;
2637
b1905489 2638 case LOCK_PREFIX_OPCODE:
c32fa91d
L
2639 q = LOCK_PREFIX;
2640 ret = PREFIX_LOCK;
b1905489
JB
2641 break;
2642
2643 case FWAIT_OPCODE:
2644 q = WAIT_PREFIX;
2645 break;
2646
2647 case ADDR_PREFIX_OPCODE:
2648 q = ADDR_PREFIX;
2649 break;
2650
2651 case DATA_PREFIX_OPCODE:
2652 q = DATA_PREFIX;
2653 break;
2654 }
2655 if (i.prefix[q] != 0)
c32fa91d 2656 ret = PREFIX_EXIST;
b1905489 2657 }
252b5132 2658
b1905489 2659 if (ret)
252b5132 2660 {
b1905489
JB
2661 if (!i.prefix[q])
2662 ++i.prefixes;
2663 i.prefix[q] |= prefix;
252b5132 2664 }
b1905489
JB
2665 else
2666 as_bad (_("same type of prefix used twice"));
252b5132 2667
252b5132
RH
2668 return ret;
2669}
2670
2671static void
78f12dd3 2672update_code_flag (int value, int check)
eecb386c 2673{
b44fef84
JB
2674 PRINTF_LIKE ((*as_error)) = check ? as_fatal : as_bad;
2675
da5f9eb4 2676 if (value == CODE_64BIT && !cpu_arch_flags.bitfield.cpu64 )
b44fef84
JB
2677 {
2678 as_error (_("64bit mode not supported on `%s'."),
2679 cpu_arch_name ? cpu_arch_name : default_arch);
2680 return;
2681 }
2682
2683 if (value == CODE_32BIT && !cpu_arch_flags.bitfield.cpui386)
2684 {
2685 as_error (_("32bit mode not supported on `%s'."),
2686 cpu_arch_name ? cpu_arch_name : default_arch);
2687 return;
2688 }
78f12dd3 2689
1e9cc1c2 2690 flag_code = (enum flag_code) value;
b44fef84 2691
eecb386c
AM
2692 stackop_size = '\0';
2693}
2694
78f12dd3
L
2695static void
2696set_code_flag (int value)
2697{
2698 update_code_flag (value, 0);
2699}
2700
eecb386c 2701static void
e3bb37b5 2702set_16bit_gcc_code_flag (int new_code_flag)
252b5132 2703{
1e9cc1c2 2704 flag_code = (enum flag_code) new_code_flag;
40fb9820
L
2705 if (flag_code != CODE_16BIT)
2706 abort ();
9306ca4a 2707 stackop_size = LONG_MNEM_SUFFIX;
252b5132
RH
2708}
2709
213f15cf
JB
2710static void
2711_set_intel_syntax (int syntax_flag)
2712{
2713 intel_syntax = syntax_flag;
2714
2715 expr_set_rank (O_full_ptr, syntax_flag ? 10 : 0);
2716
2717 register_prefix = allow_naked_reg ? "" : "%";
2718}
2719
252b5132 2720static void
e3bb37b5 2721set_intel_syntax (int syntax_flag)
252b5132
RH
2722{
2723 /* Find out if register prefixing is specified. */
2724 int ask_naked_reg = 0;
2725
2726 SKIP_WHITESPACE ();
29b0f896 2727 if (!is_end_of_line[(unsigned char) *input_line_pointer])
252b5132 2728 {
d02603dc
NC
2729 char *string;
2730 int e = get_symbol_name (&string);
252b5132 2731
47926f60 2732 if (strcmp (string, "prefix") == 0)
252b5132 2733 ask_naked_reg = 1;
47926f60 2734 else if (strcmp (string, "noprefix") == 0)
252b5132
RH
2735 ask_naked_reg = -1;
2736 else
d0b47220 2737 as_bad (_("bad argument to syntax directive."));
d02603dc 2738 (void) restore_line_pointer (e);
252b5132
RH
2739 }
2740 demand_empty_rest_of_line ();
c3332e24 2741
252b5132 2742 if (ask_naked_reg == 0)
213f15cf 2743 allow_naked_reg = (syntax_flag
f86103b7 2744 && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
252b5132
RH
2745 else
2746 allow_naked_reg = (ask_naked_reg < 0);
9306ca4a 2747
213f15cf 2748 _set_intel_syntax (syntax_flag);
252b5132
RH
2749}
2750
1efbbeb4
L
2751static void
2752set_intel_mnemonic (int mnemonic_flag)
2753{
e1d4d893 2754 intel_mnemonic = mnemonic_flag;
1efbbeb4
L
2755}
2756
db51cc60
L
2757static void
2758set_allow_index_reg (int flag)
2759{
2760 allow_index_reg = flag;
2761}
2762
cb19c032 2763static void
7bab8ab5 2764set_check (int what)
cb19c032 2765{
7bab8ab5
JB
2766 enum check_kind *kind;
2767 const char *str;
2768
2769 if (what)
2770 {
2771 kind = &operand_check;
2772 str = "operand";
2773 }
2774 else
2775 {
2776 kind = &sse_check;
2777 str = "sse";
2778 }
2779
cb19c032
L
2780 SKIP_WHITESPACE ();
2781
2782 if (!is_end_of_line[(unsigned char) *input_line_pointer])
2783 {
d02603dc
NC
2784 char *string;
2785 int e = get_symbol_name (&string);
cb19c032
L
2786
2787 if (strcmp (string, "none") == 0)
7bab8ab5 2788 *kind = check_none;
cb19c032 2789 else if (strcmp (string, "warning") == 0)
7bab8ab5 2790 *kind = check_warning;
cb19c032 2791 else if (strcmp (string, "error") == 0)
7bab8ab5 2792 *kind = check_error;
cb19c032 2793 else
7bab8ab5 2794 as_bad (_("bad argument to %s_check directive."), str);
d02603dc 2795 (void) restore_line_pointer (e);
cb19c032
L
2796 }
2797 else
7bab8ab5 2798 as_bad (_("missing argument for %s_check directive"), str);
cb19c032
L
2799
2800 demand_empty_rest_of_line ();
2801}
2802
8a9036a4
L
2803static void
2804check_cpu_arch_compatible (const char *name ATTRIBUTE_UNUSED,
1e9cc1c2 2805 i386_cpu_flags new_flag ATTRIBUTE_UNUSED)
8a9036a4
L
2806{
2807#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2808 static const char *arch;
2809
c085ab00 2810 /* Intel MCU is only supported on ELF. */
8a9036a4
L
2811 if (!IS_ELF)
2812 return;
2813
2814 if (!arch)
2815 {
2816 /* Use cpu_arch_name if it is set in md_parse_option. Otherwise
2817 use default_arch. */
2818 arch = cpu_arch_name;
2819 if (!arch)
2820 arch = default_arch;
2821 }
2822
81486035 2823 /* If we are targeting Intel MCU, we must enable it. */
648d04db
JB
2824 if ((get_elf_backend_data (stdoutput)->elf_machine_code == EM_IAMCU)
2825 == new_flag.bitfield.cpuiamcu)
81486035
L
2826 return;
2827
8a9036a4
L
2828 as_bad (_("`%s' is not supported on `%s'"), name, arch);
2829#endif
2830}
2831
8180707f 2832static void
bd483d21 2833extend_cpu_sub_arch_name (const char *pfx, const char *name)
8180707f
JB
2834{
2835 if (cpu_sub_arch_name)
2836 cpu_sub_arch_name = reconcat (cpu_sub_arch_name, cpu_sub_arch_name,
bd483d21 2837 pfx, name, (const char *) NULL);
8180707f 2838 else
bd483d21
JB
2839 cpu_sub_arch_name = concat (pfx, name, (const char *) NULL);
2840}
2841
2842static void isa_enable (unsigned int idx)
2843{
2844 i386_cpu_flags flags = cpu_flags_or (cpu_arch_flags, cpu_arch[idx].enable);
2845
2846 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
2847 {
2848 extend_cpu_sub_arch_name (".", cpu_arch[idx].name);
2849 cpu_arch_flags = flags;
2850 }
2851
2852 cpu_arch_isa_flags = cpu_flags_or (cpu_arch_isa_flags, cpu_arch[idx].enable);
2853}
2854
2855static void isa_disable (unsigned int idx)
2856{
2857 i386_cpu_flags flags
2858 = cpu_flags_and_not (cpu_arch_flags, cpu_arch[idx].disable);
2859
2860 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
2861 {
2862 extend_cpu_sub_arch_name (".no", cpu_arch[idx].name);
2863 cpu_arch_flags = flags;
2864 }
2865
2866 cpu_arch_isa_flags
2867 = cpu_flags_and_not (cpu_arch_isa_flags, cpu_arch[idx].disable);
8180707f
JB
2868}
2869
e413e4e9 2870static void
e3bb37b5 2871set_cpu_arch (int dummy ATTRIBUTE_UNUSED)
e413e4e9 2872{
f68697e8
JB
2873 typedef struct arch_stack_entry
2874 {
2875 const struct arch_stack_entry *prev;
2876 const char *name;
2877 char *sub_name;
2878 i386_cpu_flags flags;
2879 i386_cpu_flags isa_flags;
2880 enum processor_type isa;
2881 enum flag_code flag_code;
4fc85f37 2882 unsigned int vector_size;
f68697e8
JB
2883 char stackop_size;
2884 bool no_cond_jump_promotion;
2885 } arch_stack_entry;
2886 static const arch_stack_entry *arch_stack_top;
dfab07b9
JB
2887 char *s;
2888 int e;
2889 const char *string;
2890 unsigned int j = 0;
f68697e8 2891
47926f60 2892 SKIP_WHITESPACE ();
e413e4e9 2893
dfab07b9
JB
2894 if (is_end_of_line[(unsigned char) *input_line_pointer])
2895 {
2896 as_bad (_("missing cpu architecture"));
2897 input_line_pointer++;
2898 return;
2899 }
2900
2901 e = get_symbol_name (&s);
2902 string = s;
2903
2904 if (strcmp (string, "push") == 0)
e413e4e9 2905 {
dfab07b9 2906 arch_stack_entry *top = XNEW (arch_stack_entry);
e413e4e9 2907
dfab07b9
JB
2908 top->name = cpu_arch_name;
2909 if (cpu_sub_arch_name)
2910 top->sub_name = xstrdup (cpu_sub_arch_name);
2911 else
2912 top->sub_name = NULL;
2913 top->flags = cpu_arch_flags;
2914 top->isa = cpu_arch_isa;
2915 top->isa_flags = cpu_arch_isa_flags;
2916 top->flag_code = flag_code;
4fc85f37 2917 top->vector_size = vector_size;
dfab07b9
JB
2918 top->stackop_size = stackop_size;
2919 top->no_cond_jump_promotion = no_cond_jump_promotion;
2920
2921 top->prev = arch_stack_top;
2922 arch_stack_top = top;
2923
2924 (void) restore_line_pointer (e);
2925 demand_empty_rest_of_line ();
2926 return;
2927 }
2928
2929 if (strcmp (string, "pop") == 0)
2930 {
2931 const arch_stack_entry *top = arch_stack_top;
2932
2933 if (!top)
2934 as_bad (_(".arch stack is empty"));
2935 else if (top->flag_code != flag_code
2936 || top->stackop_size != stackop_size)
2937 {
2938 static const unsigned int bits[] = {
2939 [CODE_16BIT] = 16,
2940 [CODE_32BIT] = 32,
2941 [CODE_64BIT] = 64,
2942 };
2943
2944 as_bad (_("this `.arch pop' requires `.code%u%s' to be in effect"),
2945 bits[top->flag_code],
2946 top->stackop_size == LONG_MNEM_SUFFIX ? "gcc" : "");
2947 }
2948 else
3ce2ebcf 2949 {
dfab07b9
JB
2950 arch_stack_top = top->prev;
2951
2952 cpu_arch_name = top->name;
2953 free (cpu_sub_arch_name);
2954 cpu_sub_arch_name = top->sub_name;
2955 cpu_arch_flags = top->flags;
2956 cpu_arch_isa = top->isa;
2957 cpu_arch_isa_flags = top->isa_flags;
4fc85f37 2958 vector_size = top->vector_size;
dfab07b9
JB
2959 no_cond_jump_promotion = top->no_cond_jump_promotion;
2960
2961 XDELETE (top);
2962 }
2963
2964 (void) restore_line_pointer (e);
2965 demand_empty_rest_of_line ();
2966 return;
2967 }
2968
2969 if (strcmp (string, "default") == 0)
2970 {
2971 if (strcmp (default_arch, "iamcu") == 0)
2972 string = default_arch;
2973 else
2974 {
2975 static const i386_cpu_flags cpu_unknown_flags = CPU_UNKNOWN_FLAGS;
2976
2977 cpu_arch_name = NULL;
2978 free (cpu_sub_arch_name);
2979 cpu_sub_arch_name = NULL;
2980 cpu_arch_flags = cpu_unknown_flags;
dfab07b9
JB
2981 cpu_arch_isa = PROCESSOR_UNKNOWN;
2982 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
2983 if (!cpu_arch_tune_set)
fb263707 2984 cpu_arch_tune = PROCESSOR_UNKNOWN;
dfab07b9 2985
4fc85f37
JB
2986 vector_size = VSZ_DEFAULT;
2987
dfab07b9
JB
2988 j = ARRAY_SIZE (cpu_arch) + 1;
2989 }
2990 }
2991
2992 for (; j < ARRAY_SIZE (cpu_arch); j++)
2993 {
2994 if (strcmp (string + (*string == '.'), cpu_arch[j].name) == 0
2995 && (*string == '.') == (cpu_arch[j].type == PROCESSOR_NONE))
2996 {
2997 if (*string != '.')
2998 {
2999 check_cpu_arch_compatible (string, cpu_arch[j].enable);
3ce2ebcf 3000
da5f9eb4 3001 if (flag_code == CODE_64BIT && !cpu_arch[j].enable.bitfield.cpu64 )
1d07cfb4
JB
3002 {
3003 as_bad (_("64bit mode not supported on `%s'."),
3004 cpu_arch[j].name);
3005 (void) restore_line_pointer (e);
3006 ignore_rest_of_line ();
3007 return;
3008 }
3009
3010 if (flag_code == CODE_32BIT && !cpu_arch[j].enable.bitfield.cpui386)
3011 {
3012 as_bad (_("32bit mode not supported on `%s'."),
3013 cpu_arch[j].name);
3014 (void) restore_line_pointer (e);
3015 ignore_rest_of_line ();
3016 return;
3017 }
3018
dfab07b9 3019 cpu_arch_name = cpu_arch[j].name;
3ce2ebcf
JB
3020 free (cpu_sub_arch_name);
3021 cpu_sub_arch_name = NULL;
dfab07b9 3022 cpu_arch_flags = cpu_arch[j].enable;
dfab07b9
JB
3023 cpu_arch_isa = cpu_arch[j].type;
3024 cpu_arch_isa_flags = cpu_arch[j].enable;
3ce2ebcf 3025 if (!cpu_arch_tune_set)
fb263707 3026 cpu_arch_tune = cpu_arch_isa;
4fc85f37
JB
3027
3028 vector_size = VSZ_DEFAULT;
3029
dfab07b9
JB
3030 pre_386_16bit_warned = false;
3031 break;
3ce2ebcf 3032 }
f68697e8 3033
dfab07b9
JB
3034 if (cpu_flags_all_zero (&cpu_arch[j].enable))
3035 continue;
f68697e8 3036
bd483d21 3037 isa_enable (j);
f68697e8
JB
3038
3039 (void) restore_line_pointer (e);
4fc85f37
JB
3040
3041 switch (cpu_arch[j].vsz)
3042 {
3043 default:
3044 break;
3045
3046 case vsz_set:
3047#ifdef SVR4_COMMENT_CHARS
3048 if (*input_line_pointer == ':' || *input_line_pointer == '/')
3049#else
3050 if (*input_line_pointer == '/')
3051#endif
3052 {
3053 ++input_line_pointer;
3054 switch (get_absolute_expression ())
3055 {
3056 case 512: vector_size = VSZ512; break;
3057 case 256: vector_size = VSZ256; break;
3058 case 128: vector_size = VSZ128; break;
3059 default:
3060 as_bad (_("Unrecognized vector size specifier"));
3061 ignore_rest_of_line ();
3062 return;
3063 }
3064 break;
3065 }
3066 /* Fall through. */
3067 case vsz_reset:
3068 vector_size = VSZ_DEFAULT;
3069 break;
3070 }
3071
f68697e8
JB
3072 demand_empty_rest_of_line ();
3073 return;
3074 }
dfab07b9 3075 }
3ce2ebcf 3076
dfab07b9
JB
3077 if (startswith (string, ".no") && j >= ARRAY_SIZE (cpu_arch))
3078 {
3079 /* Disable an ISA extension. */
3080 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
3081 if (cpu_arch[j].type == PROCESSOR_NONE
3082 && strcmp (string + 3, cpu_arch[j].name) == 0)
3083 {
bd483d21 3084 isa_disable (j);
e413e4e9 3085
4fc85f37
JB
3086 if (cpu_arch[j].vsz == vsz_set)
3087 vector_size = VSZ_DEFAULT;
3088
dfab07b9
JB
3089 (void) restore_line_pointer (e);
3090 demand_empty_rest_of_line ();
3091 return;
3092 }
e413e4e9 3093 }
dfab07b9
JB
3094
3095 if (j == ARRAY_SIZE (cpu_arch))
3096 as_bad (_("no such architecture: `%s'"), string);
3097
3098 *input_line_pointer = e;
e413e4e9 3099
fddf5b5b
AM
3100 no_cond_jump_promotion = 0;
3101 if (*input_line_pointer == ','
29b0f896 3102 && !is_end_of_line[(unsigned char) input_line_pointer[1]])
fddf5b5b 3103 {
d02603dc 3104 ++input_line_pointer;
dfab07b9
JB
3105 e = get_symbol_name (&s);
3106 string = s;
fddf5b5b
AM
3107
3108 if (strcmp (string, "nojumps") == 0)
3109 no_cond_jump_promotion = 1;
3110 else if (strcmp (string, "jumps") == 0)
3111 ;
3112 else
3113 as_bad (_("no such architecture modifier: `%s'"), string);
3114
d02603dc 3115 (void) restore_line_pointer (e);
fddf5b5b
AM
3116 }
3117
e413e4e9
AM
3118 demand_empty_rest_of_line ();
3119}
3120
8a9036a4
L
3121enum bfd_architecture
3122i386_arch (void)
3123{
c085ab00 3124 if (cpu_arch_isa == PROCESSOR_IAMCU)
81486035 3125 {
ed049bd6 3126 if (!IS_ELF || flag_code == CODE_64BIT)
81486035
L
3127 as_fatal (_("Intel MCU is 32bit ELF only"));
3128 return bfd_arch_iamcu;
3129 }
8a9036a4
L
3130 else
3131 return bfd_arch_i386;
3132}
3133
b9d79e03 3134unsigned long
7016a5d5 3135i386_mach (void)
b9d79e03 3136{
d34049e8 3137 if (startswith (default_arch, "x86_64"))
8a9036a4 3138 {
c085ab00 3139 if (default_arch[6] == '\0')
8a9036a4 3140 return bfd_mach_x86_64;
351f65ca
L
3141 else
3142 return bfd_mach_x64_32;
8a9036a4 3143 }
5197d474
L
3144 else if (!strcmp (default_arch, "i386")
3145 || !strcmp (default_arch, "iamcu"))
81486035
L
3146 {
3147 if (cpu_arch_isa == PROCESSOR_IAMCU)
3148 {
ed049bd6 3149 if (!IS_ELF)
81486035
L
3150 as_fatal (_("Intel MCU is 32bit ELF only"));
3151 return bfd_mach_i386_iamcu;
3152 }
3153 else
3154 return bfd_mach_i386_i386;
3155 }
b9d79e03 3156 else
2b5d6a91 3157 as_fatal (_("unknown architecture"));
b9d79e03 3158}
b9d79e03 3159\f
99f0fb12
JB
3160#include "opcodes/i386-tbl.h"
3161
d3b01414
JB
3162static void
3163op_lookup (const char *mnemonic)
3164{
3165 i386_op_off_t *pos = str_hash_find (op_hash, mnemonic);
3166
3167 if (pos != NULL)
3168 {
3169 current_templates.start = &i386_optab[pos[0]];
3170 current_templates.end = &i386_optab[pos[1]];
3171 }
3172 else
3173 current_templates.end = current_templates.start = NULL;
3174}
3175
252b5132 3176void
7016a5d5 3177md_begin (void)
252b5132 3178{
86fa6981
L
3179 /* Support pseudo prefixes like {disp32}. */
3180 lex_type ['{'] = LEX_BEGIN_NAME;
3181
47926f60 3182 /* Initialize op_hash hash table. */
629310ab 3183 op_hash = str_htab_create ();
252b5132
RH
3184
3185 {
d3b01414
JB
3186 const i386_op_off_t *cur = i386_op_sets;
3187 const i386_op_off_t *end = cur + ARRAY_SIZE (i386_op_sets) - 1;
3188
3189 for (; cur < end; ++cur)
3190 if (str_hash_insert (op_hash, insn_name (&i386_optab[*cur]), cur, 0))
3191 as_fatal (_("duplicate %s"), insn_name (&i386_optab[*cur]));
252b5132
RH
3192 }
3193
47926f60 3194 /* Initialize reg_hash hash table. */
629310ab 3195 reg_hash = str_htab_create ();
252b5132 3196 {
29b0f896 3197 const reg_entry *regtab;
c3fe08fa 3198 unsigned int regtab_size = i386_regtab_size;
252b5132 3199
c3fe08fa 3200 for (regtab = i386_regtab; regtab_size--; regtab++)
6225c532 3201 {
6288d05f
JB
3202 switch (regtab->reg_type.bitfield.class)
3203 {
3204 case Reg:
34684862
JB
3205 if (regtab->reg_type.bitfield.dword)
3206 {
3207 if (regtab->reg_type.bitfield.instance == Accum)
3208 reg_eax = regtab;
3209 }
3210 else if (regtab->reg_type.bitfield.tbyte)
6288d05f
JB
3211 {
3212 /* There's no point inserting st(<N>) in the hash table, as
3213 parentheses aren't included in register_chars[] anyway. */
3214 if (regtab->reg_type.bitfield.instance != Accum)
3215 continue;
3216 reg_st0 = regtab;
3217 }
3218 break;
3219
5e042380
JB
3220 case SReg:
3221 switch (regtab->reg_num)
3222 {
3223 case 0: reg_es = regtab; break;
3224 case 2: reg_ss = regtab; break;
3225 case 3: reg_ds = regtab; break;
3226 }
3227 break;
3228
6288d05f
JB
3229 case RegMask:
3230 if (!regtab->reg_num)
3231 reg_k0 = regtab;
3232 break;
3233 }
3234
6225c532
JB
3235 if (str_hash_insert (reg_hash, regtab->reg_name, regtab, 0) != NULL)
3236 as_fatal (_("duplicate %s"), regtab->reg_name);
6225c532 3237 }
252b5132
RH
3238 }
3239
47926f60 3240 /* Fill in lexical tables: mnemonic_chars, operand_chars. */
252b5132 3241 {
29b0f896 3242 int c;
d2b1a14d 3243 const char *p;
252b5132
RH
3244
3245 for (c = 0; c < 256; c++)
3246 {
014fbcda 3247 if (ISDIGIT (c) || ISLOWER (c))
252b5132
RH
3248 {
3249 mnemonic_chars[c] = c;
3250 register_chars[c] = c;
3251 operand_chars[c] = c;
3252 }
3882b010 3253 else if (ISUPPER (c))
252b5132 3254 {
3882b010 3255 mnemonic_chars[c] = TOLOWER (c);
252b5132
RH
3256 register_chars[c] = mnemonic_chars[c];
3257 operand_chars[c] = c;
3258 }
b3983e5f
JB
3259#ifdef SVR4_COMMENT_CHARS
3260 else if (c == '\\' && strchr (i386_comment_chars, '/'))
3261 operand_chars[c] = c;
3262#endif
252b5132 3263
4795cd4a
JB
3264 if (c >= 128)
3265 operand_chars[c] = c;
252b5132
RH
3266 }
3267
c0f3af97 3268 mnemonic_chars['_'] = '_';
791fe849 3269 mnemonic_chars['-'] = '-';
0003779b 3270 mnemonic_chars['.'] = '.';
252b5132 3271
d2b1a14d
JB
3272 for (p = extra_symbol_chars; *p != '\0'; p++)
3273 operand_chars[(unsigned char) *p] = *p;
252b5132
RH
3274 for (p = operand_special_chars; *p != '\0'; p++)
3275 operand_chars[(unsigned char) *p] = *p;
3276 }
3277
fabb73d1 3278 if (object_64bit)
a4447b93 3279 {
ca19b261
KT
3280#if defined (OBJ_COFF) && defined (TE_PE)
3281 x86_dwarf2_return_column = (OUTPUT_FLAVOR == bfd_target_coff_flavour
3282 ? 32 : 16);
3283#else
a4447b93 3284 x86_dwarf2_return_column = 16;
ca19b261 3285#endif
61ff971f 3286 x86_cie_data_alignment = -8;
b52c4ee4 3287#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
788b11d9
JB
3288 x86_sframe_cfa_sp_reg = REG_SP;
3289 x86_sframe_cfa_fp_reg = REG_FP;
b52c4ee4 3290#endif
a4447b93
RH
3291 }
3292 else
3293 {
3294 x86_dwarf2_return_column = 8;
3295 x86_cie_data_alignment = -4;
3296 }
e379e5f3
L
3297
3298 /* NB: FUSED_JCC_PADDING frag must have sufficient room so that it
3299 can be turned into BRANCH_PREFIX frag. */
3300 if (align_branch_prefix_size > MAX_FUSED_JCC_PADDING_SIZE)
3301 abort ();
252b5132
RH
3302}
3303
3304void
e3bb37b5 3305i386_print_statistics (FILE *file)
252b5132 3306{
629310ab
ML
3307 htab_print_statistics (file, "i386 opcode", op_hash);
3308 htab_print_statistics (file, "i386 register", reg_hash);
252b5132 3309}
654d6f31
AM
3310
3311void
3312i386_md_end (void)
3313{
3314 htab_delete (op_hash);
3315 htab_delete (reg_hash);
3316}
252b5132 3317\f
252b5132
RH
3318#ifdef DEBUG386
3319
ce8a8b2f 3320/* Debugging routines for md_assemble. */
d3ce72d0 3321static void pte (insn_template *);
40fb9820 3322static void pt (i386_operand_type);
e3bb37b5
L
3323static void pe (expressionS *);
3324static void ps (symbolS *);
252b5132
RH
3325
3326static void
2c703856 3327pi (const char *line, i386_insn *x)
252b5132 3328{
09137c09 3329 unsigned int j;
252b5132
RH
3330
3331 fprintf (stdout, "%s: template ", line);
3332 pte (&x->tm);
09f131f2
JH
3333 fprintf (stdout, " address: base %s index %s scale %x\n",
3334 x->base_reg ? x->base_reg->reg_name : "none",
3335 x->index_reg ? x->index_reg->reg_name : "none",
3336 x->log2_scale_factor);
3337 fprintf (stdout, " modrm: mode %x reg %x reg/mem %x\n",
252b5132 3338 x->rm.mode, x->rm.reg, x->rm.regmem);
09f131f2
JH
3339 fprintf (stdout, " sib: base %x index %x scale %x\n",
3340 x->sib.base, x->sib.index, x->sib.scale);
3341 fprintf (stdout, " rex: 64bit %x extX %x extY %x extZ %x\n",
161a04f6
L
3342 (x->rex & REX_W) != 0,
3343 (x->rex & REX_R) != 0,
3344 (x->rex & REX_X) != 0,
3345 (x->rex & REX_B) != 0);
09137c09 3346 for (j = 0; j < x->operands; j++)
252b5132 3347 {
09137c09
SP
3348 fprintf (stdout, " #%d: ", j + 1);
3349 pt (x->types[j]);
252b5132 3350 fprintf (stdout, "\n");
bab6aec1 3351 if (x->types[j].bitfield.class == Reg
3528c362
JB
3352 || x->types[j].bitfield.class == RegMMX
3353 || x->types[j].bitfield.class == RegSIMD
dd6b8a0b 3354 || x->types[j].bitfield.class == RegMask
00cee14f 3355 || x->types[j].bitfield.class == SReg
4a5c67ed
JB
3356 || x->types[j].bitfield.class == RegCR
3357 || x->types[j].bitfield.class == RegDR
dd6b8a0b
JB
3358 || x->types[j].bitfield.class == RegTR
3359 || x->types[j].bitfield.class == RegBND)
09137c09
SP
3360 fprintf (stdout, "%s\n", x->op[j].regs->reg_name);
3361 if (operand_type_check (x->types[j], imm))
3362 pe (x->op[j].imms);
3363 if (operand_type_check (x->types[j], disp))
3364 pe (x->op[j].disps);
252b5132
RH
3365 }
3366}
3367
3368static void
d3ce72d0 3369pte (insn_template *t)
252b5132 3370{
b933fa4b 3371 static const unsigned char opc_pfx[] = { 0, 0x66, 0xf3, 0xf2 };
441f6aca 3372 static const char *const opc_spc[] = {
0cc78721 3373 NULL, "0f", "0f38", "0f3a", NULL, "evexmap5", "evexmap6", NULL,
441f6aca
JB
3374 "XOP08", "XOP09", "XOP0A",
3375 };
09137c09 3376 unsigned int j;
441f6aca 3377
252b5132 3378 fprintf (stdout, " %d operands ", t->operands);
441f6aca
JB
3379 if (opc_pfx[t->opcode_modifier.opcodeprefix])
3380 fprintf (stdout, "pfx %x ", opc_pfx[t->opcode_modifier.opcodeprefix]);
ddb62495
JB
3381 if (opc_spc[t->opcode_space])
3382 fprintf (stdout, "space %s ", opc_spc[t->opcode_space]);
47926f60 3383 fprintf (stdout, "opcode %x ", t->base_opcode);
252b5132
RH
3384 if (t->extension_opcode != None)
3385 fprintf (stdout, "ext %x ", t->extension_opcode);
40fb9820 3386 if (t->opcode_modifier.d)
252b5132 3387 fprintf (stdout, "D");
40fb9820 3388 if (t->opcode_modifier.w)
252b5132
RH
3389 fprintf (stdout, "W");
3390 fprintf (stdout, "\n");
09137c09 3391 for (j = 0; j < t->operands; j++)
252b5132 3392 {
09137c09
SP
3393 fprintf (stdout, " #%d type ", j + 1);
3394 pt (t->operand_types[j]);
252b5132
RH
3395 fprintf (stdout, "\n");
3396 }
3397}
3398
3399static void
e3bb37b5 3400pe (expressionS *e)
252b5132 3401{
24eab124 3402 fprintf (stdout, " operation %d\n", e->X_op);
b8281767
AM
3403 fprintf (stdout, " add_number %" PRId64 " (%" PRIx64 ")\n",
3404 (int64_t) e->X_add_number, (uint64_t) (valueT) e->X_add_number);
252b5132
RH
3405 if (e->X_add_symbol)
3406 {
3407 fprintf (stdout, " add_symbol ");
3408 ps (e->X_add_symbol);
3409 fprintf (stdout, "\n");
3410 }
3411 if (e->X_op_symbol)
3412 {
3413 fprintf (stdout, " op_symbol ");
3414 ps (e->X_op_symbol);
3415 fprintf (stdout, "\n");
3416 }
3417}
3418
3419static void
e3bb37b5 3420ps (symbolS *s)
252b5132
RH
3421{
3422 fprintf (stdout, "%s type %s%s",
3423 S_GET_NAME (s),
3424 S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
3425 segment_name (S_GET_SEGMENT (s)));
3426}
3427
7b81dfbb 3428static struct type_name
252b5132 3429 {
40fb9820
L
3430 i386_operand_type mask;
3431 const char *name;
252b5132 3432 }
7b81dfbb 3433const type_names[] =
252b5132 3434{
05909f23
JB
3435 { { .bitfield = { .class = Reg, .byte = 1 } }, "r8" },
3436 { { .bitfield = { .class = Reg, .word = 1 } }, "r16" },
3437 { { .bitfield = { .class = Reg, .dword = 1 } }, "r32" },
3438 { { .bitfield = { .class = Reg, .qword = 1 } }, "r64" },
3439 { { .bitfield = { .instance = Accum, .byte = 1 } }, "acc8" },
3440 { { .bitfield = { .instance = Accum, .word = 1 } }, "acc16" },
3441 { { .bitfield = { .instance = Accum, .dword = 1 } }, "acc32" },
3442 { { .bitfield = { .instance = Accum, .qword = 1 } }, "acc64" },
3443 { { .bitfield = { .imm8 = 1 } }, "i8" },
3444 { { .bitfield = { .imm8s = 1 } }, "i8s" },
3445 { { .bitfield = { .imm16 = 1 } }, "i16" },
3446 { { .bitfield = { .imm32 = 1 } }, "i32" },
3447 { { .bitfield = { .imm32s = 1 } }, "i32s" },
3448 { { .bitfield = { .imm64 = 1 } }, "i64" },
3449 { { .bitfield = { .imm1 = 1 } }, "i1" },
3450 { { .bitfield = { .baseindex = 1 } }, "BaseIndex" },
3451 { { .bitfield = { .disp8 = 1 } }, "d8" },
3452 { { .bitfield = { .disp16 = 1 } }, "d16" },
3453 { { .bitfield = { .disp32 = 1 } }, "d32" },
3454 { { .bitfield = { .disp64 = 1 } }, "d64" },
3455 { { .bitfield = { .instance = RegD, .word = 1 } }, "InOutPortReg" },
3456 { { .bitfield = { .instance = RegC, .byte = 1 } }, "ShiftCount" },
3457 { { .bitfield = { .class = RegCR } }, "control reg" },
3458 { { .bitfield = { .class = RegTR } }, "test reg" },
3459 { { .bitfield = { .class = RegDR } }, "debug reg" },
3460 { { .bitfield = { .class = Reg, .tbyte = 1 } }, "FReg" },
3461 { { .bitfield = { .instance = Accum, .tbyte = 1 } }, "FAcc" },
3462 { { .bitfield = { .class = SReg } }, "SReg" },
3463 { { .bitfield = { .class = RegMMX } }, "rMMX" },
3464 { { .bitfield = { .class = RegSIMD, .xmmword = 1 } }, "rXMM" },
3465 { { .bitfield = { .class = RegSIMD, .ymmword = 1 } }, "rYMM" },
3466 { { .bitfield = { .class = RegSIMD, .zmmword = 1 } }, "rZMM" },
3467 { { .bitfield = { .class = RegSIMD, .tmmword = 1 } }, "rTMM" },
3468 { { .bitfield = { .class = RegMask } }, "Mask reg" },
252b5132
RH
3469};
3470
3471static void
40fb9820 3472pt (i386_operand_type t)
252b5132 3473{
40fb9820 3474 unsigned int j;
c6fb90c8 3475 i386_operand_type a;
252b5132 3476
40fb9820 3477 for (j = 0; j < ARRAY_SIZE (type_names); j++)
c6fb90c8
L
3478 {
3479 a = operand_type_and (t, type_names[j].mask);
2c703856 3480 if (operand_type_equal (&a, &type_names[j].mask))
c6fb90c8
L
3481 fprintf (stdout, "%s, ", type_names[j].name);
3482 }
252b5132
RH
3483 fflush (stdout);
3484}
3485
3486#endif /* DEBUG386 */
3487\f
252b5132 3488static bfd_reloc_code_real_type
3956db08 3489reloc (unsigned int size,
64e74474
AM
3490 int pcrel,
3491 int sign,
3492 bfd_reloc_code_real_type other)
252b5132 3493{
47926f60 3494 if (other != NO_RELOC)
3956db08 3495 {
91d6fa6a 3496 reloc_howto_type *rel;
3956db08
JB
3497
3498 if (size == 8)
3499 switch (other)
3500 {
64e74474
AM
3501 case BFD_RELOC_X86_64_GOT32:
3502 return BFD_RELOC_X86_64_GOT64;
3503 break;
553d1284
L
3504 case BFD_RELOC_X86_64_GOTPLT64:
3505 return BFD_RELOC_X86_64_GOTPLT64;
3506 break;
64e74474
AM
3507 case BFD_RELOC_X86_64_PLTOFF64:
3508 return BFD_RELOC_X86_64_PLTOFF64;
3509 break;
3510 case BFD_RELOC_X86_64_GOTPC32:
3511 other = BFD_RELOC_X86_64_GOTPC64;
3512 break;
3513 case BFD_RELOC_X86_64_GOTPCREL:
3514 other = BFD_RELOC_X86_64_GOTPCREL64;
3515 break;
3516 case BFD_RELOC_X86_64_TPOFF32:
3517 other = BFD_RELOC_X86_64_TPOFF64;
3518 break;
3519 case BFD_RELOC_X86_64_DTPOFF32:
3520 other = BFD_RELOC_X86_64_DTPOFF64;
3521 break;
3522 default:
3523 break;
3956db08 3524 }
e05278af 3525
8ce3d284 3526#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
3527 if (other == BFD_RELOC_SIZE32)
3528 {
3529 if (size == 8)
1ab668bf 3530 other = BFD_RELOC_SIZE64;
8fd4256d 3531 if (pcrel)
1ab668bf
AM
3532 {
3533 as_bad (_("there are no pc-relative size relocations"));
3534 return NO_RELOC;
3535 }
8fd4256d 3536 }
8ce3d284 3537#endif
8fd4256d 3538
e05278af 3539 /* Sign-checking 4-byte relocations in 16-/32-bit code is pointless. */
f2d8a97c 3540 if (size == 4 && (flag_code != CODE_64BIT || disallow_64bit_reloc))
e05278af
JB
3541 sign = -1;
3542
91d6fa6a
NC
3543 rel = bfd_reloc_type_lookup (stdoutput, other);
3544 if (!rel)
3956db08 3545 as_bad (_("unknown relocation (%u)"), other);
91d6fa6a 3546 else if (size != bfd_get_reloc_size (rel))
3956db08 3547 as_bad (_("%u-byte relocation cannot be applied to %u-byte field"),
91d6fa6a 3548 bfd_get_reloc_size (rel),
3956db08 3549 size);
91d6fa6a 3550 else if (pcrel && !rel->pc_relative)
3956db08 3551 as_bad (_("non-pc-relative relocation for pc-relative field"));
91d6fa6a 3552 else if ((rel->complain_on_overflow == complain_overflow_signed
3956db08 3553 && !sign)
91d6fa6a 3554 || (rel->complain_on_overflow == complain_overflow_unsigned
64e74474 3555 && sign > 0))
3956db08
JB
3556 as_bad (_("relocated field and relocation type differ in signedness"));
3557 else
3558 return other;
3559 return NO_RELOC;
3560 }
252b5132
RH
3561
3562 if (pcrel)
3563 {
3e73aa7c 3564 if (!sign)
3956db08 3565 as_bad (_("there are no unsigned pc-relative relocations"));
252b5132
RH
3566 switch (size)
3567 {
3568 case 1: return BFD_RELOC_8_PCREL;
3569 case 2: return BFD_RELOC_16_PCREL;
d258b828 3570 case 4: return BFD_RELOC_32_PCREL;
d6ab8113 3571 case 8: return BFD_RELOC_64_PCREL;
252b5132 3572 }
3956db08 3573 as_bad (_("cannot do %u byte pc-relative relocation"), size);
252b5132
RH
3574 }
3575 else
3576 {
3956db08 3577 if (sign > 0)
e5cb08ac 3578 switch (size)
3e73aa7c
JH
3579 {
3580 case 4: return BFD_RELOC_X86_64_32S;
3581 }
3582 else
3583 switch (size)
3584 {
3585 case 1: return BFD_RELOC_8;
3586 case 2: return BFD_RELOC_16;
3587 case 4: return BFD_RELOC_32;
3588 case 8: return BFD_RELOC_64;
3589 }
3956db08
JB
3590 as_bad (_("cannot do %s %u byte relocation"),
3591 sign > 0 ? "signed" : "unsigned", size);
252b5132
RH
3592 }
3593
0cc9e1d3 3594 return NO_RELOC;
252b5132
RH
3595}
3596
071c5d81 3597#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
47926f60
KH
3598/* Here we decide which fixups can be adjusted to make them relative to
3599 the beginning of the section instead of the symbol. Basically we need
3600 to make sure that the dynamic relocations are done correctly, so in
3601 some cases we force the original symbol to be used. */
3602
252b5132 3603int
071c5d81 3604tc_i386_fix_adjustable (fixS *fixP)
252b5132 3605{
718ddfc0 3606 if (!IS_ELF)
31312f95
AM
3607 return 1;
3608
a161fe53
AM
3609 /* Don't adjust pc-relative references to merge sections in 64-bit
3610 mode. */
3611 if (use_rela_relocations
3612 && (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0
3613 && fixP->fx_pcrel)
252b5132 3614 return 0;
31312f95 3615
8d01d9a9
AJ
3616 /* The x86_64 GOTPCREL are represented as 32bit PCrel relocations
3617 and changed later by validate_fix. */
3618 if (GOT_symbol && fixP->fx_subsy == GOT_symbol
3619 && fixP->fx_r_type == BFD_RELOC_32_PCREL)
3620 return 0;
3621
8fd4256d
L
3622 /* Adjust_reloc_syms doesn't know about the GOT. Need to keep symbol
3623 for size relocations. */
3624 if (fixP->fx_r_type == BFD_RELOC_SIZE32
3625 || fixP->fx_r_type == BFD_RELOC_SIZE64
3626 || fixP->fx_r_type == BFD_RELOC_386_GOTOFF
252b5132 3627 || fixP->fx_r_type == BFD_RELOC_386_GOT32
02a86693 3628 || fixP->fx_r_type == BFD_RELOC_386_GOT32X
13ae64f3
JJ
3629 || fixP->fx_r_type == BFD_RELOC_386_TLS_GD
3630 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDM
3631 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDO_32
3632 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE_32
37e55690
JJ
3633 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE
3634 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTIE
13ae64f3
JJ
3635 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE_32
3636 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE
67a4f2b7
AO
3637 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTDESC
3638 || fixP->fx_r_type == BFD_RELOC_386_TLS_DESC_CALL
3e73aa7c 3639 || fixP->fx_r_type == BFD_RELOC_X86_64_GOT32
80b3ee89 3640 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCREL
56ceb5b5
L
3641 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCRELX
3642 || fixP->fx_r_type == BFD_RELOC_X86_64_REX_GOTPCRELX
3d5a60de 3643 || fixP->fx_r_type == BFD_RELOC_X86_64_CODE_4_GOTPCRELX
bffbf940
JJ
3644 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSGD
3645 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSLD
3646 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF32
d6ab8113 3647 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF64
bffbf940 3648 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF
a533c8df 3649 || fixP->fx_r_type == BFD_RELOC_X86_64_CODE_4_GOTTPOFF
5bc71c2a 3650 || fixP->fx_r_type == BFD_RELOC_X86_64_CODE_6_GOTTPOFF
bffbf940 3651 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF32
d6ab8113
JB
3652 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF64
3653 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTOFF64
67a4f2b7 3654 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
a533c8df 3655 || fixP->fx_r_type == BFD_RELOC_X86_64_CODE_4_GOTPC32_TLSDESC
67a4f2b7 3656 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSDESC_CALL
252b5132
RH
3657 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3658 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3659 return 0;
3660 return 1;
3661}
071c5d81 3662#endif
252b5132 3663
a9aabc23
JB
3664static INLINE bool
3665want_disp32 (const insn_template *t)
3666{
3667 return flag_code != CODE_64BIT
3668 || i.prefix[ADDR_PREFIX]
7fc69528 3669 || (t->mnem_off == MN_lea
fe134c65
JB
3670 && (!i.types[1].bitfield.qword
3671 || t->opcode_modifier.size == SIZE32));
a9aabc23
JB
3672}
3673
b4cac588 3674static int
e3bb37b5 3675intel_float_operand (const char *mnemonic)
252b5132 3676{
9306ca4a
JB
3677 /* Note that the value returned is meaningful only for opcodes with (memory)
3678 operands, hence the code here is free to improperly handle opcodes that
3679 have no operands (for better performance and smaller code). */
3680
3681 if (mnemonic[0] != 'f')
3682 return 0; /* non-math */
3683
3684 switch (mnemonic[1])
3685 {
3686 /* fclex, fdecstp, fdisi, femms, feni, fincstp, finit, fsetpm, and
3687 the fs segment override prefix not currently handled because no
3688 call path can make opcodes without operands get here */
3689 case 'i':
3690 return 2 /* integer op */;
3691 case 'l':
3692 if (mnemonic[2] == 'd' && (mnemonic[3] == 'c' || mnemonic[3] == 'e'))
3693 return 3; /* fldcw/fldenv */
3694 break;
3695 case 'n':
3696 if (mnemonic[2] != 'o' /* fnop */)
3697 return 3; /* non-waiting control op */
3698 break;
3699 case 'r':
3700 if (mnemonic[2] == 's')
3701 return 3; /* frstor/frstpm */
3702 break;
3703 case 's':
3704 if (mnemonic[2] == 'a')
3705 return 3; /* fsave */
3706 if (mnemonic[2] == 't')
3707 {
3708 switch (mnemonic[3])
3709 {
3710 case 'c': /* fstcw */
3711 case 'd': /* fstdw */
3712 case 'e': /* fstenv */
3713 case 's': /* fsts[gw] */
3714 return 3;
3715 }
3716 }
3717 break;
3718 case 'x':
3719 if (mnemonic[2] == 'r' || mnemonic[2] == 's')
3720 return 0; /* fxsave/fxrstor are not really math ops */
3721 break;
3722 }
252b5132 3723
9306ca4a 3724 return 1;
252b5132
RH
3725}
3726
9a182d04
JB
3727static INLINE void
3728install_template (const insn_template *t)
3729{
3730 unsigned int l;
3731
3732 i.tm = *t;
3733
a6f3add0
JB
3734 /* Dual VEX/EVEX templates need stripping one of the possible variants. */
3735 if (t->opcode_modifier.vex && t->opcode_modifier.evex)
6177c84d 3736 {
a5e91879
JB
3737 if ((maybe_cpu (t, CpuAVX) || maybe_cpu (t, CpuAVX2)
3738 || maybe_cpu (t, CpuFMA))
3739 && (maybe_cpu (t, CpuAVX512F) || maybe_cpu (t, CpuAVX512VL)))
a6f3add0 3740 {
6177c84d 3741 if (need_evex_encoding (t))
a6f3add0
JB
3742 {
3743 i.tm.opcode_modifier.vex = 0;
a5e91879
JB
3744 i.tm.cpu.bitfield.cpuavx512f = i.tm.cpu_any.bitfield.cpuavx512f;
3745 i.tm.cpu.bitfield.cpuavx512vl = i.tm.cpu_any.bitfield.cpuavx512vl;
a6f3add0
JB
3746 }
3747 else
3748 {
3749 i.tm.opcode_modifier.evex = 0;
a5e91879
JB
3750 if (i.tm.cpu_any.bitfield.cpuavx)
3751 i.tm.cpu.bitfield.cpuavx = 1;
3752 else if (!i.tm.cpu.bitfield.isa)
3753 i.tm.cpu.bitfield.isa = i.tm.cpu_any.bitfield.isa;
3754 else
3755 gas_assert (i.tm.cpu.bitfield.isa == i.tm.cpu_any.bitfield.isa);
a6f3add0
JB
3756 }
3757 }
6177c84d
CL
3758
3759 if ((maybe_cpu (t, CpuCMPCCXADD) || maybe_cpu (t, CpuAMX_TILE)
3760 || maybe_cpu (t, CpuAVX512F) || maybe_cpu (t, CpuAVX512DQ)
3761 || maybe_cpu (t, CpuAVX512BW) || maybe_cpu (t, CpuBMI)
5190fa38 3762 || maybe_cpu (t, CpuBMI2) || maybe_cpu (t, CpuUSER_MSR))
6177c84d
CL
3763 && maybe_cpu (t, CpuAPX_F))
3764 {
3765 if (need_evex_encoding (t))
3766 i.tm.opcode_modifier.vex = 0;
3767 else
3768 i.tm.opcode_modifier.evex = 0;
3769 }
3770 }
a6f3add0 3771
9a182d04
JB
3772 /* Note that for pseudo prefixes this produces a length of 1. But for them
3773 the length isn't interesting at all. */
3774 for (l = 1; l < 4; ++l)
3775 if (!(t->base_opcode >> (8 * l)))
3776 break;
3777
3778 i.opcode_length = l;
3779}
3780
c0f3af97
L
3781/* Build the VEX prefix. */
3782
3783static void
d3ce72d0 3784build_vex_prefix (const insn_template *t)
c0f3af97
L
3785{
3786 unsigned int register_specifier;
c0f3af97 3787 unsigned int vector_length;
03751133 3788 unsigned int w;
c0f3af97
L
3789
3790 /* Check register specifier. */
3791 if (i.vex.register_specifier)
43234a1e
L
3792 {
3793 register_specifier =
3794 ~register_number (i.vex.register_specifier) & 0xf;
3795 gas_assert ((i.vex.register_specifier->reg_flags & RegVRex) == 0);
3796 }
c0f3af97
L
3797 else
3798 register_specifier = 0xf;
3799
79f0fa25
L
3800 /* Use 2-byte VEX prefix by swapping destination and source operand
3801 if there are more than 1 register operand. */
3802 if (i.reg_operands > 1
e346d50a 3803 && i.encoding != encoding_vex3
86fa6981 3804 && i.dir_encoding == dir_encoding_default
fa99fab2 3805 && i.operands == i.reg_operands
dbbc8b7e 3806 && operand_type_equal (&i.types[0], &i.types[i.operands - 1])
ddb62495 3807 && i.tm.opcode_space == SPACE_0F
dbbc8b7e 3808 && (i.tm.opcode_modifier.load || i.tm.opcode_modifier.d)
fa99fab2
L
3809 && i.rex == REX_B)
3810 {
67748abf 3811 unsigned int xchg;
fa99fab2 3812
67748abf 3813 swap_2_operands (0, i.operands - 1);
fa99fab2 3814
9c2799c2 3815 gas_assert (i.rm.mode == 3);
fa99fab2
L
3816
3817 i.rex = REX_R;
3818 xchg = i.rm.regmem;
3819 i.rm.regmem = i.rm.reg;
3820 i.rm.reg = xchg;
3821
dbbc8b7e
JB
3822 if (i.tm.opcode_modifier.d)
3823 i.tm.base_opcode ^= (i.tm.base_opcode & 0xee) != 0x6e
2c735193 3824 ? Opcode_ExtD : Opcode_SIMD_IntD;
dbbc8b7e 3825 else /* Use the next insn. */
9a182d04 3826 install_template (&t[1]);
fa99fab2
L
3827 }
3828
79dec6b7
JB
3829 /* Use 2-byte VEX prefix by swapping commutative source operands if there
3830 are no memory operands and at least 3 register ones. */
3831 if (i.reg_operands >= 3
e346d50a 3832 && i.encoding != encoding_vex3
79dec6b7
JB
3833 && i.reg_operands == i.operands - i.imm_operands
3834 && i.tm.opcode_modifier.vex
3835 && i.tm.opcode_modifier.commutative
7440781b
JB
3836 /* .commutative aliases .staticrounding; disambiguate. */
3837 && !i.tm.opcode_modifier.sae
33740f21
JB
3838 && (i.tm.opcode_modifier.sse2avx
3839 || (optimize > 1 && !i.no_optimize))
79dec6b7
JB
3840 && i.rex == REX_B
3841 && i.vex.register_specifier
3842 && !(i.vex.register_specifier->reg_flags & RegRex))
3843 {
3844 unsigned int xchg = i.operands - i.reg_operands;
79dec6b7 3845
ddb62495 3846 gas_assert (i.tm.opcode_space == SPACE_0F);
79dec6b7
JB
3847 gas_assert (!i.tm.opcode_modifier.sae);
3848 gas_assert (operand_type_equal (&i.types[i.operands - 2],
3849 &i.types[i.operands - 3]));
3850 gas_assert (i.rm.mode == 3);
3851
67748abf 3852 swap_2_operands (xchg, xchg + 1);
79dec6b7
JB
3853
3854 i.rex = 0;
3855 xchg = i.rm.regmem | 8;
3856 i.rm.regmem = ~register_specifier & 0xf;
3857 gas_assert (!(i.rm.regmem & 8));
3858 i.vex.register_specifier += xchg - i.rm.regmem;
3859 register_specifier = ~xchg & 0xf;
3860 }
3861
539f890d
L
3862 if (i.tm.opcode_modifier.vex == VEXScalar)
3863 vector_length = avxscalar;
10c17abd
JB
3864 else if (i.tm.opcode_modifier.vex == VEX256)
3865 vector_length = 1;
d0c2e3ec
JB
3866 else if (dot_insn () && i.tm.opcode_modifier.vex == VEX128)
3867 vector_length = 0;
539f890d 3868 else
10c17abd 3869 {
56522fc5 3870 unsigned int op;
10c17abd 3871
c7213af9
L
3872 /* Determine vector length from the last multi-length vector
3873 operand. */
10c17abd 3874 vector_length = 0;
56522fc5 3875 for (op = t->operands; op--;)
10c17abd
JB
3876 if (t->operand_types[op].bitfield.xmmword
3877 && t->operand_types[op].bitfield.ymmword
3878 && i.types[op].bitfield.ymmword)
3879 {
3880 vector_length = 1;
3881 break;
3882 }
3883 }
c0f3af97 3884
03751133
L
3885 /* Check the REX.W bit and VEXW. */
3886 if (i.tm.opcode_modifier.vexw == VEXWIG)
3887 w = (vexwig == vexw1 || (i.rex & REX_W)) ? 1 : 0;
3888 else if (i.tm.opcode_modifier.vexw)
3889 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3890 else
931d03b7 3891 w = (flag_code == CODE_64BIT ? i.rex & REX_W : vexwig == vexw1) ? 1 : 0;
03751133 3892
c0f3af97 3893 /* Use 2-byte VEX prefix if possible. */
03751133 3894 if (w == 0
e346d50a 3895 && i.encoding != encoding_vex3
ddb62495 3896 && i.tm.opcode_space == SPACE_0F
c0f3af97
L
3897 && (i.rex & (REX_W | REX_X | REX_B)) == 0)
3898 {
3899 /* 2-byte VEX prefix. */
3900 unsigned int r;
3901
3902 i.vex.length = 2;
3903 i.vex.bytes[0] = 0xc5;
3904
3905 /* Check the REX.R bit. */
3906 r = (i.rex & REX_R) ? 0 : 1;
3907 i.vex.bytes[1] = (r << 7
3908 | register_specifier << 3
3909 | vector_length << 2
35648716 3910 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3911 }
3912 else
3913 {
3914 /* 3-byte VEX prefix. */
f88c9eb0 3915 i.vex.length = 3;
f88c9eb0 3916
ddb62495 3917 switch (i.tm.opcode_space)
5dd85c99 3918 {
441f6aca
JB
3919 case SPACE_0F:
3920 case SPACE_0F38:
3921 case SPACE_0F3A:
8170af78 3922 case SPACE_VEXMAP7:
80de6e00 3923 i.vex.bytes[0] = 0xc4;
7f399153 3924 break;
441f6aca
JB
3925 case SPACE_XOP08:
3926 case SPACE_XOP09:
3927 case SPACE_XOP0A:
f88c9eb0 3928 i.vex.bytes[0] = 0x8f;
7f399153
L
3929 break;
3930 default:
3931 abort ();
f88c9eb0 3932 }
c0f3af97 3933
c0f3af97
L
3934 /* The high 3 bits of the second VEX byte are 1's compliment
3935 of RXB bits from REX. */
d0c2e3ec
JB
3936 i.vex.bytes[1] = ((~i.rex & 7) << 5)
3937 | (!dot_insn () ? i.tm.opcode_space
3938 : i.insn_opcode_space);
c0f3af97 3939
c0f3af97
L
3940 i.vex.bytes[2] = (w << 7
3941 | register_specifier << 3
3942 | vector_length << 2
35648716 3943 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3944 }
3945}
3946
5b7c81bd 3947static INLINE bool
7a8655d2
JB
3948is_any_vex_encoding (const insn_template *t)
3949{
706ce984 3950 return t->opcode_modifier.vex || t->opcode_modifier.evex;
7a8655d2
JB
3951}
3952
6177c84d
CL
3953/* We can use this function only when the current encoding is evex. */
3954static INLINE bool
3955is_apx_evex_encoding (void)
3956{
3957 return i.rex2 || i.tm.opcode_space == SPACE_EVEXMAP4
3958 || (i.vex.register_specifier
3959 && (i.vex.register_specifier->reg_flags & RegRex2));
3960}
3961
80d61d8d
CL
3962static INLINE bool
3963is_apx_rex2_encoding (void)
3964{
ac32c879 3965 return i.rex2 || i.rex2_encoding
3037cefe 3966 || i.tm.opcode_modifier.rex2;
80d61d8d
CL
3967}
3968
a5748e0d
JB
3969static unsigned int
3970get_broadcast_bytes (const insn_template *t, bool diag)
3971{
3972 unsigned int op, bytes;
3973 const i386_operand_type *types;
3974
3975 if (i.broadcast.type)
9b345ce8 3976 return (1 << (t->opcode_modifier.broadcast - 1)) * i.broadcast.type;
a5748e0d
JB
3977
3978 gas_assert (intel_syntax);
3979
3980 for (op = 0; op < t->operands; ++op)
3981 if (t->operand_types[op].bitfield.baseindex)
3982 break;
3983
3984 gas_assert (op < t->operands);
3985
706ce984 3986 if (t->opcode_modifier.evex != EVEXDYN)
a5748e0d
JB
3987 switch (i.broadcast.bytes)
3988 {
3989 case 1:
3990 if (t->operand_types[op].bitfield.word)
3991 return 2;
3992 /* Fall through. */
3993 case 2:
3994 if (t->operand_types[op].bitfield.dword)
3995 return 4;
3996 /* Fall through. */
3997 case 4:
3998 if (t->operand_types[op].bitfield.qword)
3999 return 8;
4000 /* Fall through. */
4001 case 8:
4002 if (t->operand_types[op].bitfield.xmmword)
4003 return 16;
4004 if (t->operand_types[op].bitfield.ymmword)
4005 return 32;
4006 if (t->operand_types[op].bitfield.zmmword)
4007 return 64;
4008 /* Fall through. */
4009 default:
4010 abort ();
4011 }
4012
4013 gas_assert (op + 1 < t->operands);
4014
4015 if (t->operand_types[op + 1].bitfield.xmmword
4016 + t->operand_types[op + 1].bitfield.ymmword
4017 + t->operand_types[op + 1].bitfield.zmmword > 1)
4018 {
4019 types = &i.types[op + 1];
4020 diag = false;
4021 }
4022 else /* Ambiguous - guess with a preference to non-AVX512VL forms. */
4023 types = &t->operand_types[op];
4024
4025 if (types->bitfield.zmmword)
4026 bytes = 64;
4027 else if (types->bitfield.ymmword)
4028 bytes = 32;
4029 else
4030 bytes = 16;
4031
4032 if (diag)
4033 as_warn (_("ambiguous broadcast for `%s', using %u-bit form"),
76d3f746 4034 insn_name (t), bytes * 8);
a5748e0d
JB
4035
4036 return bytes;
4037}
4038
43234a1e
L
4039/* Build the EVEX prefix. */
4040
4041static void
4042build_evex_prefix (void)
4043{
35648716 4044 unsigned int register_specifier, w;
43234a1e
L
4045 rex_byte vrex_used = 0;
4046
4047 /* Check register specifier. */
4048 if (i.vex.register_specifier)
4049 {
4050 gas_assert ((i.vrex & REX_X) == 0);
4051
4052 register_specifier = i.vex.register_specifier->reg_num;
4053 if ((i.vex.register_specifier->reg_flags & RegRex))
4054 register_specifier += 8;
4055 /* The upper 16 registers are encoded in the fourth byte of the
4056 EVEX prefix. */
4057 if (!(i.vex.register_specifier->reg_flags & RegVRex))
4058 i.vex.bytes[3] = 0x8;
4059 register_specifier = ~register_specifier & 0xf;
4060 }
4061 else
4062 {
4063 register_specifier = 0xf;
4064
4065 /* Encode upper 16 vector index register in the fourth byte of
4066 the EVEX prefix. */
4067 if (!(i.vrex & REX_X))
4068 i.vex.bytes[3] = 0x8;
4069 else
4070 vrex_used |= REX_X;
4071 }
4072
43234a1e
L
4073 /* 4 byte EVEX prefix. */
4074 i.vex.length = 4;
4075 i.vex.bytes[0] = 0x62;
4076
43234a1e
L
4077 /* The high 3 bits of the second EVEX byte are 1's compliment of RXB
4078 bits from REX. */
ddb62495 4079 gas_assert (i.tm.opcode_space >= SPACE_0F);
5190fa38 4080 gas_assert (i.tm.opcode_space <= SPACE_VEXMAP7);
d0c2e3ec
JB
4081 i.vex.bytes[1] = ((~i.rex & 7) << 5)
4082 | (!dot_insn () ? i.tm.opcode_space
4083 : i.insn_opcode_space);
43234a1e
L
4084
4085 /* The fifth bit of the second EVEX byte is 1's compliment of the
4086 REX_R bit in VREX. */
4087 if (!(i.vrex & REX_R))
4088 i.vex.bytes[1] |= 0x10;
4089 else
4090 vrex_used |= REX_R;
4091
4092 if ((i.reg_operands + i.imm_operands) == i.operands)
4093 {
4094 /* When all operands are registers, the REX_X bit in REX is not
4095 used. We reuse it to encode the upper 16 registers, which is
4096 indicated by the REX_B bit in VREX. The REX_X bit is encoded
4097 as 1's compliment. */
4098 if ((i.vrex & REX_B))
4099 {
4100 vrex_used |= REX_B;
4101 i.vex.bytes[1] &= ~0x40;
4102 }
4103 }
4104
4105 /* EVEX instructions shouldn't need the REX prefix. */
4106 i.vrex &= ~vrex_used;
4107 gas_assert (i.vrex == 0);
4108
6865c043
L
4109 /* Check the REX.W bit and VEXW. */
4110 if (i.tm.opcode_modifier.vexw == VEXWIG)
4111 w = (evexwig == evexw1 || (i.rex & REX_W)) ? 1 : 0;
4112 else if (i.tm.opcode_modifier.vexw)
4113 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
4114 else
931d03b7 4115 w = (flag_code == CODE_64BIT ? i.rex & REX_W : evexwig == evexw1) ? 1 : 0;
43234a1e 4116
43234a1e 4117 /* The third byte of the EVEX prefix. */
35648716
JB
4118 i.vex.bytes[2] = ((w << 7)
4119 | (register_specifier << 3)
4120 | 4 /* Encode the U bit. */
4121 | i.tm.opcode_modifier.opcodeprefix);
43234a1e
L
4122
4123 /* The fourth byte of the EVEX prefix. */
4124 /* The zeroing-masking bit. */
6225c532 4125 if (i.mask.reg && i.mask.zeroing)
43234a1e
L
4126 i.vex.bytes[3] |= 0x80;
4127
4128 /* Don't always set the broadcast bit if there is no RC. */
ca5312a2 4129 if (i.rounding.type == rc_none)
43234a1e
L
4130 {
4131 /* Encode the vector length. */
4132 unsigned int vec_length;
4133
706ce984 4134 if (i.tm.opcode_modifier.evex == EVEXDYN)
e771e7c9 4135 {
56522fc5 4136 unsigned int op;
e771e7c9 4137
c7213af9
L
4138 /* Determine vector length from the last multi-length vector
4139 operand. */
56522fc5 4140 for (op = i.operands; op--;)
e771e7c9
JB
4141 if (i.tm.operand_types[op].bitfield.xmmword
4142 + i.tm.operand_types[op].bitfield.ymmword
4143 + i.tm.operand_types[op].bitfield.zmmword > 1)
4144 {
4145 if (i.types[op].bitfield.zmmword)
c7213af9
L
4146 {
4147 i.tm.opcode_modifier.evex = EVEX512;
4148 break;
4149 }
e771e7c9 4150 else if (i.types[op].bitfield.ymmword)
c7213af9
L
4151 {
4152 i.tm.opcode_modifier.evex = EVEX256;
4153 break;
4154 }
e771e7c9 4155 else if (i.types[op].bitfield.xmmword)
c7213af9
L
4156 {
4157 i.tm.opcode_modifier.evex = EVEX128;
4158 break;
4159 }
9b345ce8
JB
4160 else if ((i.broadcast.type || i.broadcast.bytes)
4161 && op == i.broadcast.operand)
625cbd7a 4162 {
a5748e0d 4163 switch (get_broadcast_bytes (&i.tm, true))
625cbd7a
JB
4164 {
4165 case 64:
4166 i.tm.opcode_modifier.evex = EVEX512;
4167 break;
4168 case 32:
4169 i.tm.opcode_modifier.evex = EVEX256;
4170 break;
4171 case 16:
4172 i.tm.opcode_modifier.evex = EVEX128;
4173 break;
4174 default:
c7213af9 4175 abort ();
625cbd7a 4176 }
c7213af9 4177 break;
625cbd7a 4178 }
e771e7c9 4179 }
c7213af9 4180
56522fc5 4181 if (op >= MAX_OPERANDS)
c7213af9 4182 abort ();
e771e7c9
JB
4183 }
4184
43234a1e
L
4185 switch (i.tm.opcode_modifier.evex)
4186 {
4187 case EVEXLIG: /* LL' is ignored */
4188 vec_length = evexlig << 5;
4189 break;
4190 case EVEX128:
4191 vec_length = 0 << 5;
4192 break;
4193 case EVEX256:
4194 vec_length = 1 << 5;
4195 break;
4196 case EVEX512:
4197 vec_length = 2 << 5;
4198 break;
d0c2e3ec
JB
4199 case EVEX_L3:
4200 if (dot_insn ())
4201 {
4202 vec_length = 3 << 5;
4203 break;
4204 }
4205 /* Fall through. */
43234a1e
L
4206 default:
4207 abort ();
4208 break;
4209 }
4210 i.vex.bytes[3] |= vec_length;
4211 /* Encode the broadcast bit. */
9b345ce8 4212 if (i.broadcast.type || i.broadcast.bytes)
43234a1e
L
4213 i.vex.bytes[3] |= 0x10;
4214 }
ca5312a2
JB
4215 else if (i.rounding.type != saeonly)
4216 i.vex.bytes[3] |= 0x10 | (i.rounding.type << 5);
43234a1e 4217 else
ca5312a2 4218 i.vex.bytes[3] |= 0x10 | (evexrcig << 5);
43234a1e 4219
6225c532
JB
4220 if (i.mask.reg)
4221 i.vex.bytes[3] |= i.mask.reg->reg_num;
43234a1e
L
4222}
4223
80d61d8d
CL
4224/* Build (2 bytes) rex2 prefix.
4225 | D5h |
4226 | m | R4 X4 B4 | W R X B |
4227
4228 Rex2 reuses i.vex as they both encode i.tm.opcode_space in their prefixes.
4229 */
4230static void
4231build_rex2_prefix (void)
4232{
4233 i.vex.length = 2;
4234 i.vex.bytes[0] = 0xd5;
4235 /* For the W R X B bits, the variables of rex prefix will be reused. */
4236 i.vex.bytes[1] = ((i.tm.opcode_space << 7)
4237 | (i.rex2 << 4) | i.rex);
4238}
4239
6177c84d
CL
4240/* Build the EVEX prefix (4-byte) for evex insn
4241 | 62h |
4242 | `R`X`B`R' | B'mmm |
4243 | W | v`v`v`v | `x' | pp |
4244 | z| L'L | b | `v | aaa |
4245*/
4246static void
4247build_apx_evex_prefix (void)
4248{
4249 build_evex_prefix ();
4250 if (i.rex2 & REX_R)
4251 i.vex.bytes[1] &= ~0x10;
4252 if (i.rex2 & REX_B)
4253 i.vex.bytes[1] |= 0x08;
4254 if (i.rex2 & REX_X)
4255 i.vex.bytes[2] &= ~0x04;
4256 if (i.vex.register_specifier
4257 && i.vex.register_specifier->reg_flags & RegRex2)
4258 i.vex.bytes[3] &= ~0x08;
3083f376 4259
4260 /* Encode the NDD bit of the instruction promoted from the legacy
4261 space. */
4262 if (i.vex.register_specifier && i.tm.opcode_space == SPACE_EVEXMAP4)
4263 i.vex.bytes[3] |= 0x10;
6177c84d
CL
4264}
4265
ce705688
JB
4266static void establish_rex (void)
4267{
4268 /* Note that legacy encodings have at most 2 non-immediate operands. */
4269 unsigned int first = i.imm_operands;
4270 unsigned int last = i.operands > first ? i.operands - first - 1 : first;
4271
4272 /* Respect a user-specified REX prefix. */
4273 i.rex |= i.prefix[REX_PREFIX] & REX_OPCODE;
4274
4275 /* For 8 bit registers we need an empty rex prefix. Also if the
4276 instruction already has a prefix, we need to convert old
4277 registers to new ones. */
4278
4279 if ((i.types[first].bitfield.class == Reg && i.types[first].bitfield.byte
80d61d8d
CL
4280 && ((i.op[first].regs->reg_flags & RegRex64) != 0 || i.rex != 0
4281 || i.rex2 != 0))
ce705688 4282 || (i.types[last].bitfield.class == Reg && i.types[last].bitfield.byte
80d61d8d
CL
4283 && ((i.op[last].regs->reg_flags & RegRex64) != 0 || i.rex != 0
4284 || i.rex2 != 0)))
ce705688
JB
4285 {
4286 unsigned int x;
4287
80d61d8d
CL
4288 if (!is_apx_rex2_encoding () && !is_any_vex_encoding(&i.tm))
4289 i.rex |= REX_OPCODE;
ce705688
JB
4290 for (x = first; x <= last; x++)
4291 {
4292 /* Look for 8 bit operand that uses old registers. */
4293 if (i.types[x].bitfield.class == Reg && i.types[x].bitfield.byte
4294 && (i.op[x].regs->reg_flags & RegRex64) == 0)
4295 {
4296 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
4297 /* In case it is "hi" register, give up. */
4298 if (i.op[x].regs->reg_num > 3)
4299 as_bad (_("can't encode register '%s%s' in an "
80d61d8d 4300 "instruction requiring REX/REX2 prefix"),
ce705688
JB
4301 register_prefix, i.op[x].regs->reg_name);
4302
4303 /* Otherwise it is equivalent to the extended register.
4304 Since the encoding doesn't change this is merely
4305 cosmetic cleanup for debug output. */
4306 i.op[x].regs += 8;
4307 }
4308 }
4309 }
4310
192781a3 4311 if (i.rex == 0 && i.rex2 == 0 && (i.rex_encoding || i.rex2_encoding))
ce705688
JB
4312 {
4313 /* Check if we can add a REX_OPCODE byte. Look for 8 bit operand
4314 that uses legacy register. If it is "hi" register, don't add
80d61d8d 4315 rex and rex2 prefix. */
ce705688
JB
4316 unsigned int x;
4317
4318 for (x = first; x <= last; x++)
4319 if (i.types[x].bitfield.class == Reg
4320 && i.types[x].bitfield.byte
4321 && (i.op[x].regs->reg_flags & RegRex64) == 0
4322 && i.op[x].regs->reg_num > 3)
4323 {
4324 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
4325 i.rex_encoding = false;
80d61d8d 4326 i.rex2_encoding = false;
ce705688
JB
4327 break;
4328 }
4329
4330 if (i.rex_encoding)
4331 i.rex = REX_OPCODE;
4332 }
4333
192781a3
CL
4334 if (is_apx_rex2_encoding ())
4335 {
4336 build_rex2_prefix ();
4337 /* The individual REX.RXBW bits got consumed. */
4338 i.rex &= REX_OPCODE;
4339 }
4340 else if (i.rex != 0)
4341 add_prefix (REX_OPCODE | i.rex);
ce705688
JB
4342}
4343
65da13b5
L
4344static void
4345process_immext (void)
4346{
4347 expressionS *exp;
4348
c0f3af97 4349 /* These AMD 3DNow! and SSE2 instructions have an opcode suffix
65da13b5
L
4350 which is coded in the same place as an 8-bit immediate field
4351 would be. Here we fake an 8-bit immediate operand from the
4352 opcode suffix stored in tm.extension_opcode.
4353
c1e679ec 4354 AVX instructions also use this encoding, for some of
c0f3af97 4355 3 argument instructions. */
65da13b5 4356
43234a1e 4357 gas_assert (i.imm_operands <= 1
7ab9ffdd 4358 && (i.operands <= 2
7a8655d2 4359 || (is_any_vex_encoding (&i.tm)
7ab9ffdd 4360 && i.operands <= 4)));
65da13b5
L
4361
4362 exp = &im_expressions[i.imm_operands++];
4363 i.op[i.operands].imms = exp;
be1643ff 4364 i.types[i.operands].bitfield.imm8 = 1;
65da13b5
L
4365 i.operands++;
4366 exp->X_op = O_constant;
4367 exp->X_add_number = i.tm.extension_opcode;
4368 i.tm.extension_opcode = None;
4369}
4370
42164a71
L
4371
4372static int
4373check_hle (void)
4374{
742732c7 4375 switch (i.tm.opcode_modifier.prefixok)
42164a71
L
4376 {
4377 default:
4378 abort ();
742732c7
JB
4379 case PrefixLock:
4380 case PrefixNone:
4381 case PrefixNoTrack:
4382 case PrefixRep:
165de32a 4383 as_bad (_("invalid instruction `%s' after `%s'"),
76d3f746 4384 insn_name (&i.tm), i.hle_prefix);
42164a71 4385 return 0;
742732c7 4386 case PrefixHLELock:
42164a71
L
4387 if (i.prefix[LOCK_PREFIX])
4388 return 1;
165de32a 4389 as_bad (_("missing `lock' with `%s'"), i.hle_prefix);
42164a71 4390 return 0;
742732c7 4391 case PrefixHLEAny:
42164a71 4392 return 1;
742732c7 4393 case PrefixHLERelease:
42164a71
L
4394 if (i.prefix[HLE_PREFIX] != XRELEASE_PREFIX_OPCODE)
4395 {
4396 as_bad (_("instruction `%s' after `xacquire' not allowed"),
76d3f746 4397 insn_name (&i.tm));
42164a71
L
4398 return 0;
4399 }
8dc0818e 4400 if (i.mem_operands == 0 || !(i.flags[i.operands - 1] & Operand_Mem))
42164a71
L
4401 {
4402 as_bad (_("memory destination needed for instruction `%s'"
76d3f746 4403 " after `xrelease'"), insn_name (&i.tm));
42164a71
L
4404 return 0;
4405 }
4406 return 1;
4407 }
4408}
4409
c8480b58
L
4410/* Encode aligned vector move as unaligned vector move. */
4411
4412static void
4413encode_with_unaligned_vector_move (void)
4414{
4415 switch (i.tm.base_opcode)
4416 {
b3a9fe6f
L
4417 case 0x28: /* Load instructions. */
4418 case 0x29: /* Store instructions. */
c8480b58 4419 /* movaps/movapd/vmovaps/vmovapd. */
ddb62495 4420 if (i.tm.opcode_space == SPACE_0F
c8480b58 4421 && i.tm.opcode_modifier.opcodeprefix <= PREFIX_0X66)
b3a9fe6f 4422 i.tm.base_opcode = 0x10 | (i.tm.base_opcode & 1);
c8480b58 4423 break;
b3a9fe6f
L
4424 case 0x6f: /* Load instructions. */
4425 case 0x7f: /* Store instructions. */
c8480b58 4426 /* movdqa/vmovdqa/vmovdqa64/vmovdqa32. */
ddb62495 4427 if (i.tm.opcode_space == SPACE_0F
c8480b58
L
4428 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0X66)
4429 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
4430 break;
4431 default:
4432 break;
4433 }
4434}
4435
b6f8c7c4
L
4436/* Try the shortest encoding by shortening operand size. */
4437
4438static void
4439optimize_encoding (void)
4440{
a0a1771e 4441 unsigned int j;
b6f8c7c4 4442
7fc69528 4443 if (i.tm.mnem_off == MN_lea)
fe134c65
JB
4444 {
4445 /* Optimize: -O:
4446 lea symbol, %rN -> mov $symbol, %rN
4447 lea (%rM), %rN -> mov %rM, %rN
4448 lea (,%rM,1), %rN -> mov %rM, %rN
4449
4450 and in 32-bit mode for 16-bit addressing
4451
4452 lea (%rM), %rN -> movzx %rM, %rN
4453
4454 and in 64-bit mode zap 32-bit addressing in favor of using a
4455 32-bit (or less) destination.
4456 */
4457 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
4458 {
4459 if (!i.op[1].regs->reg_type.bitfield.word)
4460 i.tm.opcode_modifier.size = SIZE32;
4461 i.prefix[ADDR_PREFIX] = 0;
4462 }
4463
4464 if (!i.index_reg && !i.base_reg)
4465 {
4466 /* Handle:
4467 lea symbol, %rN -> mov $symbol, %rN
4468 */
4469 if (flag_code == CODE_64BIT)
4470 {
4471 /* Don't transform a relocation to a 16-bit one. */
4472 if (i.op[0].disps
4473 && i.op[0].disps->X_op != O_constant
4474 && i.op[1].regs->reg_type.bitfield.word)
4475 return;
4476
4477 if (!i.op[1].regs->reg_type.bitfield.qword
4478 || i.tm.opcode_modifier.size == SIZE32)
4479 {
4480 i.tm.base_opcode = 0xb8;
4481 i.tm.opcode_modifier.modrm = 0;
4482 if (!i.op[1].regs->reg_type.bitfield.word)
4483 i.types[0].bitfield.imm32 = 1;
4484 else
4485 {
4486 i.tm.opcode_modifier.size = SIZE16;
4487 i.types[0].bitfield.imm16 = 1;
4488 }
4489 }
4490 else
4491 {
4492 /* Subject to further optimization below. */
4493 i.tm.base_opcode = 0xc7;
4494 i.tm.extension_opcode = 0;
4495 i.types[0].bitfield.imm32s = 1;
4496 i.types[0].bitfield.baseindex = 0;
4497 }
4498 }
4499 /* Outside of 64-bit mode address and operand sizes have to match if
4500 a relocation is involved, as otherwise we wouldn't (currently) or
4501 even couldn't express the relocation correctly. */
4502 else if (i.op[0].disps
4503 && i.op[0].disps->X_op != O_constant
4504 && ((!i.prefix[ADDR_PREFIX])
4505 != (flag_code == CODE_32BIT
4506 ? i.op[1].regs->reg_type.bitfield.dword
4507 : i.op[1].regs->reg_type.bitfield.word)))
4508 return;
7772f168
JB
4509 /* In 16-bit mode converting LEA with 16-bit addressing and a 32-bit
4510 destination is going to grow encoding size. */
4511 else if (flag_code == CODE_16BIT
4512 && (optimize <= 1 || optimize_for_space)
4513 && !i.prefix[ADDR_PREFIX]
4514 && i.op[1].regs->reg_type.bitfield.dword)
4515 return;
fe134c65
JB
4516 else
4517 {
4518 i.tm.base_opcode = 0xb8;
4519 i.tm.opcode_modifier.modrm = 0;
4520 if (i.op[1].regs->reg_type.bitfield.dword)
4521 i.types[0].bitfield.imm32 = 1;
4522 else
4523 i.types[0].bitfield.imm16 = 1;
4524
4525 if (i.op[0].disps
4526 && i.op[0].disps->X_op == O_constant
4527 && i.op[1].regs->reg_type.bitfield.dword
60cfa10c
L
4528 /* NB: Add () to !i.prefix[ADDR_PREFIX] to silence
4529 GCC 5. */
4530 && (!i.prefix[ADDR_PREFIX]) != (flag_code == CODE_32BIT))
fe134c65
JB
4531 i.op[0].disps->X_add_number &= 0xffff;
4532 }
4533
4534 i.tm.operand_types[0] = i.types[0];
4535 i.imm_operands = 1;
4536 if (!i.op[0].imms)
4537 {
4538 i.op[0].imms = &im_expressions[0];
4539 i.op[0].imms->X_op = O_absent;
4540 }
4541 }
4542 else if (i.op[0].disps
4543 && (i.op[0].disps->X_op != O_constant
4544 || i.op[0].disps->X_add_number))
4545 return;
4546 else
4547 {
4548 /* Handle:
4549 lea (%rM), %rN -> mov %rM, %rN
4550 lea (,%rM,1), %rN -> mov %rM, %rN
4551 lea (%rM), %rN -> movzx %rM, %rN
4552 */
4553 const reg_entry *addr_reg;
4554
4555 if (!i.index_reg && i.base_reg->reg_num != RegIP)
4556 addr_reg = i.base_reg;
4557 else if (!i.base_reg
4558 && i.index_reg->reg_num != RegIZ
4559 && !i.log2_scale_factor)
4560 addr_reg = i.index_reg;
4561 else
4562 return;
4563
4564 if (addr_reg->reg_type.bitfield.word
4565 && i.op[1].regs->reg_type.bitfield.dword)
4566 {
4567 if (flag_code != CODE_32BIT)
4568 return;
ddb62495 4569 i.tm.opcode_space = SPACE_0F;
fe134c65
JB
4570 i.tm.base_opcode = 0xb7;
4571 }
4572 else
4573 i.tm.base_opcode = 0x8b;
4574
4575 if (addr_reg->reg_type.bitfield.dword
4576 && i.op[1].regs->reg_type.bitfield.qword)
4577 i.tm.opcode_modifier.size = SIZE32;
4578
4579 i.op[0].regs = addr_reg;
4580 i.reg_operands = 2;
4581 }
4582
4583 i.mem_operands = 0;
4584 i.disp_operands = 0;
4585 i.prefix[ADDR_PREFIX] = 0;
4586 i.prefix[SEG_PREFIX] = 0;
4587 i.seg[0] = NULL;
4588 }
4589
b6f8c7c4 4590 if (optimize_for_space
7fc69528 4591 && i.tm.mnem_off == MN_test
b6f8c7c4
L
4592 && i.reg_operands == 1
4593 && i.imm_operands == 1
4594 && !i.types[1].bitfield.byte
4595 && i.op[0].imms->X_op == O_constant
7fc69528 4596 && fits_in_imm7 (i.op[0].imms->X_add_number))
b6f8c7c4
L
4597 {
4598 /* Optimize: -Os:
4599 test $imm7, %r64/%r32/%r16 -> test $imm7, %r8
4600 */
4601 unsigned int base_regnum = i.op[1].regs->reg_num;
4602 if (flag_code == CODE_64BIT || base_regnum < 4)
4603 {
4604 i.types[1].bitfield.byte = 1;
4605 /* Ignore the suffix. */
4606 i.suffix = 0;
80d61d8d
CL
4607 /* Convert to byte registers. 8-bit registers are special,
4608 RegRex64 and non-RegRex64 each have 8 registers. */
7697afb6 4609 if (i.types[1].bitfield.word)
80d61d8d 4610 /* 32 (or 40) 8-bit registers. */
7697afb6 4611 j = 32;
80d61d8d
CL
4612 else if (i.types[1].bitfield.dword)
4613 /* 32 (or 40) 8-bit registers + 32 16-bit registers. */
4614 j = 64;
7697afb6 4615 else
80d61d8d
CL
4616 /* 32 (or 40) 8-bit registers + 32 16-bit registers
4617 + 32 32-bit registers. */
4618 j = 96;
4619
4620 /* In 64-bit mode, the following byte registers cannot be accessed
4621 if using the Rex and Rex2 prefix: AH, BH, CH, DH */
4622 if (!(i.op[1].regs->reg_flags & (RegRex | RegRex2)) && base_regnum < 4)
7697afb6
JB
4623 j += 8;
4624 i.op[1].regs -= j;
b6f8c7c4
L
4625 }
4626 }
4627 else if (flag_code == CODE_64BIT
ddb62495 4628 && i.tm.opcode_space == SPACE_BASE
d3d50934
L
4629 && ((i.types[1].bitfield.qword
4630 && i.reg_operands == 1
b6f8c7c4
L
4631 && i.imm_operands == 1
4632 && i.op[0].imms->X_op == O_constant
507916b8 4633 && ((i.tm.base_opcode == 0xb8
b6f8c7c4
L
4634 && i.tm.extension_opcode == None
4635 && fits_in_unsigned_long (i.op[0].imms->X_add_number))
4636 || (fits_in_imm31 (i.op[0].imms->X_add_number)
7fc69528 4637 && (i.tm.base_opcode == 0x24
b6f8c7c4
L
4638 || (i.tm.base_opcode == 0x80
4639 && i.tm.extension_opcode == 0x4)
7fc69528
JB
4640 || i.tm.mnem_off == MN_test
4641 || ((i.tm.base_opcode | 1) == 0xc7
b8364fa7
JB
4642 && i.tm.extension_opcode == 0x0)))
4643 || (fits_in_imm7 (i.op[0].imms->X_add_number)
4644 && i.tm.base_opcode == 0x83
4645 && i.tm.extension_opcode == 0x4)))
d3d50934
L
4646 || (i.types[0].bitfield.qword
4647 && ((i.reg_operands == 2
4648 && i.op[0].regs == i.op[1].regs
7fc69528
JB
4649 && (i.tm.mnem_off == MN_xor
4650 || i.tm.mnem_off == MN_sub))
4651 || i.tm.mnem_off == MN_clr))))
b6f8c7c4
L
4652 {
4653 /* Optimize: -O:
4654 andq $imm31, %r64 -> andl $imm31, %r32
b8364fa7 4655 andq $imm7, %r64 -> andl $imm7, %r32
b6f8c7c4
L
4656 testq $imm31, %r64 -> testl $imm31, %r32
4657 xorq %r64, %r64 -> xorl %r32, %r32
4658 subq %r64, %r64 -> subl %r32, %r32
4659 movq $imm31, %r64 -> movl $imm31, %r32
4660 movq $imm32, %r64 -> movl $imm32, %r32
4661 */
04784e33
JB
4662 i.tm.opcode_modifier.size = SIZE32;
4663 if (i.imm_operands)
4664 {
4665 i.types[0].bitfield.imm32 = 1;
4666 i.types[0].bitfield.imm32s = 0;
4667 i.types[0].bitfield.imm64 = 0;
4668 }
4669 else
4670 {
4671 i.types[0].bitfield.dword = 1;
4672 i.types[0].bitfield.qword = 0;
4673 }
4674 i.types[1].bitfield.dword = 1;
4675 i.types[1].bitfield.qword = 0;
7fc69528 4676 if (i.tm.mnem_off == MN_mov || i.tm.mnem_off == MN_lea)
b6f8c7c4
L
4677 {
4678 /* Handle
4679 movq $imm31, %r64 -> movl $imm31, %r32
4680 movq $imm32, %r64 -> movl $imm32, %r32
4681 */
4682 i.tm.operand_types[0].bitfield.imm32 = 1;
4683 i.tm.operand_types[0].bitfield.imm32s = 0;
4684 i.tm.operand_types[0].bitfield.imm64 = 0;
507916b8 4685 if ((i.tm.base_opcode | 1) == 0xc7)
b6f8c7c4
L
4686 {
4687 /* Handle
4688 movq $imm31, %r64 -> movl $imm31, %r32
4689 */
507916b8 4690 i.tm.base_opcode = 0xb8;
b6f8c7c4 4691 i.tm.extension_opcode = None;
507916b8 4692 i.tm.opcode_modifier.w = 0;
b6f8c7c4
L
4693 i.tm.opcode_modifier.modrm = 0;
4694 }
4695 }
4696 }
c73a37b2
JB
4697 else if (i.reg_operands == 3
4698 && i.op[0].regs == i.op[1].regs
4699 && i.encoding != encoding_evex
4700 && (i.tm.mnem_off == MN_xor
4701 || i.tm.mnem_off == MN_sub))
4702 {
4703 /* Optimize: -O:
4704 xorb %rNb, %rNb, %rMb -> xorl %rMd, %rMd
4705 xorw %rNw, %rNw, %rMw -> xorl %rMd, %rMd
4706 xorl %rNd, %rNd, %rMd -> xorl %rMd, %rMd
4707 xorq %rN, %rN, %rM -> xorl %rMd, %rMd
4708 subb %rNb, %rNb, %rMb -> subl %rMd, %rMd
4709 subw %rNw, %rNw, %rMw -> subl %rMd, %rMd
4710 subl %rNd, %rNd, %rMd -> subl %rMd, %rMd
4711 subq %rN, %rN, %rM -> subl %rMd, %rMd
4712 */
4713 i.tm.opcode_space = SPACE_BASE;
4714 i.tm.opcode_modifier.evex = 0;
4715 i.tm.opcode_modifier.size = SIZE32;
4716 i.types[0].bitfield.byte = 0;
4717 i.types[0].bitfield.word = 0;
4718 i.types[0].bitfield.dword = 1;
4719 i.types[0].bitfield.qword = 0;
4720 i.op[0].regs = i.op[2].regs;
4721 i.types[1] = i.types[0];
4722 i.op[1].regs = i.op[2].regs;
4723 i.reg_operands = 2;
4724 }
5641ec01
JB
4725 else if (optimize > 1
4726 && !optimize_for_space
4727 && i.reg_operands == 2
4728 && i.op[0].regs == i.op[1].regs
7fc69528 4729 && (i.tm.mnem_off == MN_and || i.tm.mnem_off == MN_or)
5641ec01
JB
4730 && (flag_code != CODE_64BIT || !i.types[0].bitfield.dword))
4731 {
4732 /* Optimize: -O2:
4733 andb %rN, %rN -> testb %rN, %rN
4734 andw %rN, %rN -> testw %rN, %rN
4735 andq %rN, %rN -> testq %rN, %rN
4736 orb %rN, %rN -> testb %rN, %rN
4737 orw %rN, %rN -> testw %rN, %rN
4738 orq %rN, %rN -> testq %rN, %rN
4739
4740 and outside of 64-bit mode
4741
4742 andl %rN, %rN -> testl %rN, %rN
4743 orl %rN, %rN -> testl %rN, %rN
4744 */
4745 i.tm.base_opcode = 0x84 | (i.tm.base_opcode & 1);
4746 }
ad2f4436
JB
4747 else if (i.tm.base_opcode == 0xba
4748 && i.tm.opcode_space == SPACE_0F
4749 && i.reg_operands == 1
4750 && i.op[0].imms->X_op == O_constant
4751 && i.op[0].imms->X_add_number >= 0)
4752 {
4753 /* Optimize: -O:
4754 btw $n, %rN -> btl $n, %rN (outside of 16-bit mode, n < 16)
4755 btq $n, %rN -> btl $n, %rN (in 64-bit mode, n < 32, N < 8)
4756 btl $n, %rN -> btw $n, %rN (in 16-bit mode, n < 16)
4757
4758 With <BT> one of bts, btr, and bts also:
4759 <BT>w $n, %rN -> btl $n, %rN (in 32-bit mode, n < 16)
4760 <BT>l $n, %rN -> btw $n, %rN (in 16-bit mode, n < 16)
4761 */
4762 switch (flag_code)
4763 {
4764 case CODE_64BIT:
4765 if (i.tm.extension_opcode != 4)
4766 break;
4767 if (i.types[1].bitfield.qword
4768 && i.op[0].imms->X_add_number < 32
4769 && !(i.op[1].regs->reg_flags & RegRex))
4770 i.tm.opcode_modifier.size = SIZE32;
4771 /* Fall through. */
4772 case CODE_32BIT:
4773 if (i.types[1].bitfield.word
4774 && i.op[0].imms->X_add_number < 16)
4775 i.tm.opcode_modifier.size = SIZE32;
4776 break;
4777 case CODE_16BIT:
4778 if (i.op[0].imms->X_add_number < 16)
4779 i.tm.opcode_modifier.size = SIZE16;
4780 break;
4781 }
4782 }
99112332 4783 else if (i.reg_operands == 3
b6f8c7c4
L
4784 && i.op[0].regs == i.op[1].regs
4785 && !i.types[2].bitfield.xmmword
4786 && (i.tm.opcode_modifier.vex
6225c532 4787 || ((!i.mask.reg || i.mask.zeroing)
706ce984 4788 && i.tm.opcode_modifier.evex
e346d50a 4789 && (i.encoding != encoding_evex
dd22218c 4790 || cpu_arch_isa_flags.bitfield.cpuavx512vl
734dfd1c 4791 || is_cpu (&i.tm, CpuAVX512VL)
7091c612 4792 || (i.tm.operand_types[2].bitfield.zmmword
dd22218c 4793 && i.types[2].bitfield.ymmword))))
ddb62495 4794 && i.tm.opcode_space == SPACE_0F
5844ccaa
JB
4795 && ((i.tm.base_opcode | 2) == 0x57
4796 || i.tm.base_opcode == 0xdf
4797 || i.tm.base_opcode == 0xef
4798 || (i.tm.base_opcode | 3) == 0xfb
4799 || i.tm.base_opcode == 0x42
4800 || i.tm.base_opcode == 0x47))
b6f8c7c4 4801 {
99112332 4802 /* Optimize: -O1:
8305403a
L
4803 VOP, one of vandnps, vandnpd, vxorps, vxorpd, vpsubb, vpsubd,
4804 vpsubq and vpsubw:
b6f8c7c4
L
4805 EVEX VOP %zmmM, %zmmM, %zmmN
4806 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4807 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4808 EVEX VOP %ymmM, %ymmM, %ymmN
4809 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4810 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4811 VEX VOP %ymmM, %ymmM, %ymmN
4812 -> VEX VOP %xmmM, %xmmM, %xmmN
4813 VOP, one of vpandn and vpxor:
4814 VEX VOP %ymmM, %ymmM, %ymmN
4815 -> VEX VOP %xmmM, %xmmM, %xmmN
4816 VOP, one of vpandnd and vpandnq:
4817 EVEX VOP %zmmM, %zmmM, %zmmN
4818 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4819 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4820 EVEX VOP %ymmM, %ymmM, %ymmN
4821 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4822 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4823 VOP, one of vpxord and vpxorq:
4824 EVEX VOP %zmmM, %zmmM, %zmmN
4825 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4826 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4827 EVEX VOP %ymmM, %ymmM, %ymmN
4828 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4829 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
1424ad86
JB
4830 VOP, one of kxord and kxorq:
4831 VEX VOP %kM, %kM, %kN
4832 -> VEX kxorw %kM, %kM, %kN
4833 VOP, one of kandnd and kandnq:
4834 VEX VOP %kM, %kM, %kN
4835 -> VEX kandnw %kM, %kM, %kN
b6f8c7c4 4836 */
706ce984 4837 if (i.tm.opcode_modifier.evex)
b6f8c7c4 4838 {
e346d50a 4839 if (i.encoding != encoding_evex)
b6f8c7c4
L
4840 {
4841 i.tm.opcode_modifier.vex = VEX128;
4842 i.tm.opcode_modifier.vexw = VEXW0;
4843 i.tm.opcode_modifier.evex = 0;
e346d50a 4844 i.encoding = encoding_vex;
a6f3add0 4845 i.mask.reg = NULL;
b6f8c7c4 4846 }
7b1d7ca1 4847 else if (optimize > 1)
dd22218c
L
4848 i.tm.opcode_modifier.evex = EVEX128;
4849 else
4850 return;
b6f8c7c4 4851 }
f74a6307 4852 else if (i.tm.operand_types[0].bitfield.class == RegMask)
1424ad86 4853 {
35648716 4854 i.tm.opcode_modifier.opcodeprefix = PREFIX_NONE;
1424ad86
JB
4855 i.tm.opcode_modifier.vexw = VEXW0;
4856 }
b6f8c7c4
L
4857 else
4858 i.tm.opcode_modifier.vex = VEX128;
4859
4860 if (i.tm.opcode_modifier.vex)
4861 for (j = 0; j < 3; j++)
4862 {
4863 i.types[j].bitfield.xmmword = 1;
4864 i.types[j].bitfield.ymmword = 0;
4865 }
4866 }
e346d50a 4867 else if (i.encoding != encoding_evex
eb3f3841 4868 && i.encoding != encoding_egpr
97ed31ae 4869 && !i.types[0].bitfield.zmmword
392a5972 4870 && !i.types[1].bitfield.zmmword
6225c532 4871 && !i.mask.reg
9b345ce8 4872 && !i.broadcast.type
a5748e0d 4873 && !i.broadcast.bytes
706ce984 4874 && i.tm.opcode_modifier.evex
35648716
JB
4875 && ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4876 || (i.tm.base_opcode & ~4) == 0xdb
4877 || (i.tm.base_opcode & ~4) == 0xeb)
97ed31ae
L
4878 && i.tm.extension_opcode == None)
4879 {
4880 /* Optimize: -O1:
4881 VOP, one of vmovdqa32, vmovdqa64, vmovdqu8, vmovdqu16,
4882 vmovdqu32 and vmovdqu64:
4883 EVEX VOP %xmmM, %xmmN
4884 -> VEX vmovdqa|vmovdqu %xmmM, %xmmN (M and N < 16)
4885 EVEX VOP %ymmM, %ymmN
4886 -> VEX vmovdqa|vmovdqu %ymmM, %ymmN (M and N < 16)
4887 EVEX VOP %xmmM, mem
4888 -> VEX vmovdqa|vmovdqu %xmmM, mem (M < 16)
4889 EVEX VOP %ymmM, mem
4890 -> VEX vmovdqa|vmovdqu %ymmM, mem (M < 16)
4891 EVEX VOP mem, %xmmN
4892 -> VEX mvmovdqa|vmovdquem, %xmmN (N < 16)
4893 EVEX VOP mem, %ymmN
4894 -> VEX vmovdqa|vmovdqu mem, %ymmN (N < 16)
a0a1771e
JB
4895 VOP, one of vpand, vpandn, vpor, vpxor:
4896 EVEX VOP{d,q} %xmmL, %xmmM, %xmmN
4897 -> VEX VOP %xmmL, %xmmM, %xmmN (L, M, and N < 16)
4898 EVEX VOP{d,q} %ymmL, %ymmM, %ymmN
4899 -> VEX VOP %ymmL, %ymmM, %ymmN (L, M, and N < 16)
4900 EVEX VOP{d,q} mem, %xmmM, %xmmN
4901 -> VEX VOP mem, %xmmM, %xmmN (M and N < 16)
4902 EVEX VOP{d,q} mem, %ymmM, %ymmN
4903 -> VEX VOP mem, %ymmM, %ymmN (M and N < 16)
97ed31ae 4904 */
a0a1771e 4905 for (j = 0; j < i.operands; j++)
392a5972
L
4906 if (operand_type_check (i.types[j], disp)
4907 && i.op[j].disps->X_op == O_constant)
4908 {
4909 /* Since the VEX prefix has 2 or 3 bytes, the EVEX prefix
4910 has 4 bytes, EVEX Disp8 has 1 byte and VEX Disp32 has 4
4911 bytes, we choose EVEX Disp8 over VEX Disp32. */
4912 int evex_disp8, vex_disp8;
4913 unsigned int memshift = i.memshift;
4914 offsetT n = i.op[j].disps->X_add_number;
4915
4916 evex_disp8 = fits_in_disp8 (n);
4917 i.memshift = 0;
4918 vex_disp8 = fits_in_disp8 (n);
4919 if (evex_disp8 != vex_disp8)
4920 {
4921 i.memshift = memshift;
4922 return;
4923 }
4924
4925 i.types[j].bitfield.disp8 = vex_disp8;
4926 break;
4927 }
35648716
JB
4928 if ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4929 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2)
4930 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
97ed31ae
L
4931 i.tm.opcode_modifier.vex
4932 = i.types[0].bitfield.ymmword ? VEX256 : VEX128;
4933 i.tm.opcode_modifier.vexw = VEXW0;
79dec6b7 4934 /* VPAND, VPOR, and VPXOR are commutative. */
35648716 4935 if (i.reg_operands == 3 && i.tm.base_opcode != 0xdf)
79dec6b7 4936 i.tm.opcode_modifier.commutative = 1;
97ed31ae
L
4937 i.tm.opcode_modifier.evex = 0;
4938 i.tm.opcode_modifier.masking = 0;
a0a1771e 4939 i.tm.opcode_modifier.broadcast = 0;
97ed31ae
L
4940 i.tm.opcode_modifier.disp8memshift = 0;
4941 i.memshift = 0;
a0a1771e
JB
4942 if (j < i.operands)
4943 i.types[j].bitfield.disp8
4944 = fits_in_disp8 (i.op[j].disps->X_add_number);
97ed31ae 4945 }
b5c37946
SJ
4946 else if (optimize_for_space
4947 && i.tm.base_opcode == 0x29
4948 && i.tm.opcode_space == SPACE_0F38
4949 && i.operands == i.reg_operands
4950 && i.op[0].regs == i.op[1].regs
4951 && (!i.tm.opcode_modifier.vex
4952 || !(i.op[0].regs->reg_flags & RegRex))
706ce984 4953 && !i.tm.opcode_modifier.evex)
b5c37946
SJ
4954 {
4955 /* Optimize: -Os:
4956 pcmpeqq %xmmN, %xmmN -> pcmpeqd %xmmN, %xmmN
4957 vpcmpeqq %xmmN, %xmmN, %xmmM -> vpcmpeqd %xmmN, %xmmN, %xmmM (N < 8)
4958 vpcmpeqq %ymmN, %ymmN, %ymmM -> vpcmpeqd %ymmN, %ymmN, %ymmM (N < 8)
4959 */
4960 i.tm.opcode_space = SPACE_0F;
4961 i.tm.base_opcode = 0x76;
4962 }
4963 else if (((i.tm.base_opcode >= 0x64
4964 && i.tm.base_opcode <= 0x66
4965 && i.tm.opcode_space == SPACE_0F)
4966 || (i.tm.base_opcode == 0x37
4967 && i.tm.opcode_space == SPACE_0F38))
4968 && i.operands == i.reg_operands
4969 && i.op[0].regs == i.op[1].regs
706ce984 4970 && !i.tm.opcode_modifier.evex)
b5c37946
SJ
4971 {
4972 /* Optimize: -O:
4973 pcmpgt[bwd] %mmN, %mmN -> pxor %mmN, %mmN
4974 pcmpgt[bwdq] %xmmN, %xmmN -> pxor %xmmN, %xmmN
4975 vpcmpgt[bwdq] %xmmN, %xmmN, %xmmM -> vpxor %xmmN, %xmmN, %xmmM (N < 8)
4976 vpcmpgt[bwdq] %xmmN, %xmmN, %xmmM -> vpxor %xmm0, %xmm0, %xmmM (N > 7)
4977 vpcmpgt[bwdq] %ymmN, %ymmN, %ymmM -> vpxor %ymmN, %ymmN, %ymmM (N < 8)
4978 vpcmpgt[bwdq] %ymmN, %ymmN, %ymmM -> vpxor %ymm0, %ymm0, %ymmM (N > 7)
4979 */
4980 i.tm.opcode_space = SPACE_0F;
4981 i.tm.base_opcode = 0xef;
4982 if (i.tm.opcode_modifier.vex && (i.op[0].regs->reg_flags & RegRex))
4983 {
4984 if (i.operands == 2)
4985 {
4986 gas_assert (i.tm.opcode_modifier.sse2avx);
4987
4988 i.operands = 3;
4989 i.reg_operands = 3;
4990 i.tm.operands = 3;
4991
4992 i.op[2].regs = i.op[0].regs;
4993 i.types[2] = i.types[0];
4994 i.flags[2] = i.flags[0];
4995 i.tm.operand_types[2] = i.tm.operand_types[0];
4996
4997 i.tm.opcode_modifier.sse2avx = 0;
4998 }
4999 i.op[0].regs -= i.op[0].regs->reg_num + 8;
5000 i.op[1].regs = i.op[0].regs;
5001 }
5002 }
5003 else if (optimize_for_space
5004 && i.tm.base_opcode == 0x59
5005 && i.tm.opcode_space == SPACE_0F38
5006 && i.operands == i.reg_operands
5007 && i.tm.opcode_modifier.vex
5008 && !(i.op[0].regs->reg_flags & RegRex)
5009 && i.op[0].regs->reg_type.bitfield.xmmword
e346d50a 5010 && i.encoding != encoding_vex3)
b5c37946
SJ
5011 {
5012 /* Optimize: -Os:
5013 vpbroadcastq %xmmN, %xmmM -> vpunpcklqdq %xmmN, %xmmN, %xmmM (N < 8)
5014 */
5015 i.tm.opcode_space = SPACE_0F;
5016 i.tm.base_opcode = 0x6c;
5017 i.tm.opcode_modifier.vexvvvv = 1;
5018
5019 ++i.operands;
5020 ++i.reg_operands;
5021 ++i.tm.operands;
5022
5023 i.op[2].regs = i.op[0].regs;
5024 i.types[2] = i.types[0];
5025 i.flags[2] = i.flags[0];
5026 i.tm.operand_types[2] = i.tm.operand_types[0];
5027
5028 swap_2_operands (1, 2);
5029 }
b6f8c7c4
L
5030}
5031
1e7dd4a0
JB
5032static void
5033s_noopt (int dummy ATTRIBUTE_UNUSED)
5034{
5035 if (!is_it_end_of_statement ())
5036 as_warn (_("`.noopt' arguments ignored"));
5037
5038 optimize = 0;
5039 optimize_for_space = 0;
5040
5041 ignore_rest_of_line ();
5042}
5043
ae531041
L
5044/* Return non-zero for load instruction. */
5045
5046static int
5047load_insn_p (void)
5048{
5049 unsigned int dest;
5050 int any_vex_p = is_any_vex_encoding (&i.tm);
5051 unsigned int base_opcode = i.tm.base_opcode | 1;
5052
5053 if (!any_vex_p)
5054 {
ef07be45
CL
5055 /* Anysize insns: lea, invlpg, clflush, prefetch*, bndmk, bndcl, bndcu,
5056 bndcn, bndstx, bndldx, clflushopt, clwb, cldemote. */
255571cd 5057 if (i.tm.opcode_modifier.operandconstraint == ANY_SIZE)
ae531041
L
5058 return 0;
5059
389d00a5 5060 /* pop. */
6d86a545 5061 if (i.tm.mnem_off == MN_pop)
389d00a5
JB
5062 return 1;
5063 }
5064
ddb62495 5065 if (i.tm.opcode_space == SPACE_BASE)
389d00a5
JB
5066 {
5067 /* popf, popa. */
5068 if (i.tm.base_opcode == 0x9d
a09f656b 5069 || i.tm.base_opcode == 0x61)
ae531041
L
5070 return 1;
5071
5072 /* movs, cmps, lods, scas. */
5073 if ((i.tm.base_opcode | 0xb) == 0xaf)
5074 return 1;
5075
a09f656b 5076 /* outs, xlatb. */
5077 if (base_opcode == 0x6f
5078 || i.tm.base_opcode == 0xd7)
ae531041 5079 return 1;
a09f656b 5080 /* NB: For AMD-specific insns with implicit memory operands,
5081 they're intentionally not covered. */
ae531041
L
5082 }
5083
5084 /* No memory operand. */
5085 if (!i.mem_operands)
5086 return 0;
5087
5088 if (any_vex_p)
5089 {
7fc69528 5090 if (i.tm.mnem_off == MN_vldmxcsr)
ae531041
L
5091 return 1;
5092 }
ddb62495 5093 else if (i.tm.opcode_space == SPACE_BASE)
ae531041
L
5094 {
5095 /* test, not, neg, mul, imul, div, idiv. */
aa4c197d 5096 if (base_opcode == 0xf7 && i.tm.extension_opcode != 1)
ae531041
L
5097 return 1;
5098
5099 /* inc, dec. */
5100 if (base_opcode == 0xff && i.tm.extension_opcode <= 1)
5101 return 1;
5102
5103 /* add, or, adc, sbb, and, sub, xor, cmp. */
5104 if (i.tm.base_opcode >= 0x80 && i.tm.base_opcode <= 0x83)
5105 return 1;
5106
ae531041 5107 /* rol, ror, rcl, rcr, shl/sal, shr, sar. */
aa4c197d 5108 if ((base_opcode == 0xc1 || (base_opcode | 2) == 0xd3)
ae531041
L
5109 && i.tm.extension_opcode != 6)
5110 return 1;
5111
ae531041 5112 /* Check for x87 instructions. */
aa4c197d 5113 if ((base_opcode | 6) == 0xdf)
ae531041
L
5114 {
5115 /* Skip fst, fstp, fstenv, fstcw. */
5116 if (i.tm.base_opcode == 0xd9
5117 && (i.tm.extension_opcode == 2
5118 || i.tm.extension_opcode == 3
5119 || i.tm.extension_opcode == 6
5120 || i.tm.extension_opcode == 7))
5121 return 0;
5122
5123 /* Skip fisttp, fist, fistp, fstp. */
5124 if (i.tm.base_opcode == 0xdb
5125 && (i.tm.extension_opcode == 1
5126 || i.tm.extension_opcode == 2
5127 || i.tm.extension_opcode == 3
5128 || i.tm.extension_opcode == 7))
5129 return 0;
5130
5131 /* Skip fisttp, fst, fstp, fsave, fstsw. */
5132 if (i.tm.base_opcode == 0xdd
5133 && (i.tm.extension_opcode == 1
5134 || i.tm.extension_opcode == 2
5135 || i.tm.extension_opcode == 3
5136 || i.tm.extension_opcode == 6
5137 || i.tm.extension_opcode == 7))
5138 return 0;
5139
5140 /* Skip fisttp, fist, fistp, fbstp, fistp. */
5141 if (i.tm.base_opcode == 0xdf
5142 && (i.tm.extension_opcode == 1
5143 || i.tm.extension_opcode == 2
5144 || i.tm.extension_opcode == 3
5145 || i.tm.extension_opcode == 6
5146 || i.tm.extension_opcode == 7))
5147 return 0;
5148
5149 return 1;
5150 }
5151 }
ddb62495 5152 else if (i.tm.opcode_space == SPACE_0F)
389d00a5
JB
5153 {
5154 /* bt, bts, btr, btc. */
5155 if (i.tm.base_opcode == 0xba
aa4c197d 5156 && (i.tm.extension_opcode | 3) == 7)
389d00a5
JB
5157 return 1;
5158
5159 /* cmpxchg8b, cmpxchg16b, xrstors, vmptrld. */
5160 if (i.tm.base_opcode == 0xc7
5161 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
5162 && (i.tm.extension_opcode == 1 || i.tm.extension_opcode == 3
5163 || i.tm.extension_opcode == 6))
5164 return 1;
5165
5166 /* fxrstor, ldmxcsr, xrstor. */
5167 if (i.tm.base_opcode == 0xae
5168 && (i.tm.extension_opcode == 1
5169 || i.tm.extension_opcode == 2
5170 || i.tm.extension_opcode == 5))
5171 return 1;
5172
5173 /* lgdt, lidt, lmsw. */
5174 if (i.tm.base_opcode == 0x01
5175 && (i.tm.extension_opcode == 2
5176 || i.tm.extension_opcode == 3
5177 || i.tm.extension_opcode == 6))
5178 return 1;
5179 }
ae531041
L
5180
5181 dest = i.operands - 1;
5182
5183 /* Check fake imm8 operand and 3 source operands. */
5184 if ((i.tm.opcode_modifier.immext
aa180741 5185 || i.reg_operands + i.mem_operands == 4)
ae531041
L
5186 && i.types[dest].bitfield.imm8)
5187 dest--;
5188
389d00a5 5189 /* add, or, adc, sbb, and, sub, xor, cmp, test, xchg. */
ddb62495 5190 if (i.tm.opcode_space == SPACE_BASE
aa4c197d 5191 && ((base_opcode | 0x38) == 0x39
389d00a5
JB
5192 || (base_opcode | 2) == 0x87))
5193 return 1;
5194
7fc69528 5195 if (i.tm.mnem_off == MN_xadd)
ae531041
L
5196 return 1;
5197
5198 /* Check for load instruction. */
5199 return (i.types[dest].bitfield.class != ClassNone
5200 || i.types[dest].bitfield.instance == Accum);
5201}
5202
5203/* Output lfence, 0xfaee8, after instruction. */
5204
5205static void
5206insert_lfence_after (void)
5207{
5208 if (lfence_after_load && load_insn_p ())
5209 {
a09f656b 5210 /* There are also two REP string instructions that require
5211 special treatment. Specifically, the compare string (CMPS)
5212 and scan string (SCAS) instructions set EFLAGS in a manner
5213 that depends on the data being compared/scanned. When used
5214 with a REP prefix, the number of iterations may therefore
5215 vary depending on this data. If the data is a program secret
5216 chosen by the adversary using an LVI method,
5217 then this data-dependent behavior may leak some aspect
5218 of the secret. */
aa4c197d 5219 if (((i.tm.base_opcode | 0x9) == 0xaf)
a09f656b 5220 && i.prefix[REP_PREFIX])
5221 {
5222 as_warn (_("`%s` changes flags which would affect control flow behavior"),
76d3f746 5223 insn_name (&i.tm));
a09f656b 5224 }
ae531041
L
5225 char *p = frag_more (3);
5226 *p++ = 0xf;
5227 *p++ = 0xae;
5228 *p = 0xe8;
5229 }
5230}
5231
5232/* Output lfence, 0xfaee8, before instruction. */
5233
5234static void
b5482fe5 5235insert_lfence_before (const struct last_insn *last_insn)
ae531041
L
5236{
5237 char *p;
5238
ddb62495 5239 if (i.tm.opcode_space != SPACE_BASE)
ae531041
L
5240 return;
5241
5242 if (i.tm.base_opcode == 0xff
5243 && (i.tm.extension_opcode == 2 || i.tm.extension_opcode == 4))
5244 {
5245 /* Insert lfence before indirect branch if needed. */
5246
5247 if (lfence_before_indirect_branch == lfence_branch_none)
5248 return;
5249
5250 if (i.operands != 1)
5251 abort ();
5252
5253 if (i.reg_operands == 1)
5254 {
5255 /* Indirect branch via register. Don't insert lfence with
5256 -mlfence-after-load=yes. */
5257 if (lfence_after_load
5258 || lfence_before_indirect_branch == lfence_branch_memory)
5259 return;
5260 }
5261 else if (i.mem_operands == 1
5262 && lfence_before_indirect_branch != lfence_branch_register)
5263 {
5264 as_warn (_("indirect `%s` with memory operand should be avoided"),
76d3f746 5265 insn_name (&i.tm));
ae531041
L
5266 return;
5267 }
5268 else
5269 return;
5270
b5482fe5 5271 if (last_insn->kind != last_insn_other)
ae531041 5272 {
b5482fe5 5273 as_warn_where (last_insn->file, last_insn->line,
ae531041 5274 _("`%s` skips -mlfence-before-indirect-branch on `%s`"),
b5482fe5 5275 last_insn->name, insn_name (&i.tm));
ae531041
L
5276 return;
5277 }
5278
5279 p = frag_more (3);
5280 *p++ = 0xf;
5281 *p++ = 0xae;
5282 *p = 0xe8;
5283 return;
5284 }
5285
503648e4 5286 /* Output or/not/shl and lfence before near ret. */
ae531041 5287 if (lfence_before_ret != lfence_before_ret_none
aa4c197d 5288 && (i.tm.base_opcode | 1) == 0xc3)
ae531041 5289 {
b5482fe5 5290 if (last_insn->kind != last_insn_other)
ae531041 5291 {
b5482fe5 5292 as_warn_where (last_insn->file, last_insn->line,
ae531041 5293 _("`%s` skips -mlfence-before-ret on `%s`"),
b5482fe5 5294 last_insn->name, insn_name (&i.tm));
ae531041
L
5295 return;
5296 }
a09f656b 5297
a09f656b 5298 /* Near ret ingore operand size override under CPU64. */
503648e4 5299 char prefix = flag_code == CODE_64BIT
5300 ? 0x48
5301 : i.prefix[DATA_PREFIX] ? 0x66 : 0x0;
a09f656b 5302
5303 if (lfence_before_ret == lfence_before_ret_not)
5304 {
5305 /* not: 0xf71424, may add prefix
5306 for operand size override or 64-bit code. */
5307 p = frag_more ((prefix ? 2 : 0) + 6 + 3);
5308 if (prefix)
5309 *p++ = prefix;
ae531041
L
5310 *p++ = 0xf7;
5311 *p++ = 0x14;
5312 *p++ = 0x24;
a09f656b 5313 if (prefix)
5314 *p++ = prefix;
ae531041
L
5315 *p++ = 0xf7;
5316 *p++ = 0x14;
5317 *p++ = 0x24;
5318 }
a09f656b 5319 else
5320 {
5321 p = frag_more ((prefix ? 1 : 0) + 4 + 3);
5322 if (prefix)
5323 *p++ = prefix;
5324 if (lfence_before_ret == lfence_before_ret_or)
5325 {
5326 /* or: 0x830c2400, may add prefix
5327 for operand size override or 64-bit code. */
5328 *p++ = 0x83;
5329 *p++ = 0x0c;
5330 }
5331 else
5332 {
5333 /* shl: 0xc1242400, may add prefix
5334 for operand size override or 64-bit code. */
5335 *p++ = 0xc1;
5336 *p++ = 0x24;
5337 }
5338
5339 *p++ = 0x24;
5340 *p++ = 0x0;
5341 }
5342
ae531041
L
5343 *p++ = 0xf;
5344 *p++ = 0xae;
5345 *p = 0xe8;
5346 }
5347}
5348
edd67638
JB
5349/* Shared helper for md_assemble() and s_insn(). */
5350static void init_globals (void)
5351{
5352 unsigned int j;
5353
5354 memset (&i, '\0', sizeof (i));
5355 i.rounding.type = rc_none;
5356 for (j = 0; j < MAX_OPERANDS; j++)
5357 i.reloc[j] = NO_RELOC;
5358 memset (disp_expressions, '\0', sizeof (disp_expressions));
5359 memset (im_expressions, '\0', sizeof (im_expressions));
5360 save_stack_p = save_stack;
5361}
5362
04784e33
JB
5363/* Helper for md_assemble() to decide whether to prepare for a possible 2nd
5364 parsing pass. Instead of introducing a rarely use new insn attribute this
5365 utilizes a common pattern between affected templates. It is deemed
5366 acceptable that this will lead to unnecessary pass 2 preparations in a
5367 limited set of cases. */
5368static INLINE bool may_need_pass2 (const insn_template *t)
5369{
5370 return t->opcode_modifier.sse2avx
5371 /* Note that all SSE2AVX templates have at least one operand. */
a28fedbc 5372 ? t->operand_types[t->operands - 1].bitfield.class == RegSIMD
ddb62495 5373 : (t->opcode_space == SPACE_0F
a28fedbc 5374 && (t->base_opcode | 1) == 0xbf)
ddb62495 5375 || (t->opcode_space == SPACE_BASE
a28fedbc 5376 && t->base_opcode == 0x63);
04784e33
JB
5377}
5378
c7defc53
IB
5379#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
5380
5381/* DWARF register number for EFLAGS. Used for pushf/popf insns. */
5382#define GINSN_DW2_REGNUM_EFLAGS 49
5383/* DWARF register number for RSI. Used as dummy value when RegIP/RegIZ. */
5384#define GINSN_DW2_REGNUM_RSI_DUMMY 4
5385
5386/* Identify the callee-saved registers in System V AMD64 ABI. */
5387
5388bool
5389x86_scfi_callee_saved_p (unsigned int dw2reg_num)
5390{
5391 if (dw2reg_num == 3 /* rbx. */
5392 || dw2reg_num == REG_FP /* rbp. */
5393 || dw2reg_num == REG_SP /* rsp. */
5394 || (dw2reg_num >= 12 && dw2reg_num <= 15) /* r12 - r15. */)
5395 return true;
5396
5397 return false;
5398}
5399
5400/* Check whether an instruction prefix which affects operation size
5401 accompanies. For insns in the legacy space, setting REX.W takes precedence
5402 over the operand-size prefix (66H) when both are used.
5403
5404 The current users of this API are in the handlers for PUSH, POP or other
5405 instructions which affect the stack pointer implicitly: the operation size
5406 (16, 32, or 64 bits) determines the amount by which the stack pointer is
5407 incremented / decremented (2, 4 or 8). */
5408
5409static bool
5410ginsn_opsize_prefix_p (void)
5411{
5412 return (!(i.prefix[REX_PREFIX] & REX_W) && i.prefix[DATA_PREFIX]);
5413}
5414
5415/* Get the DWARF register number for the given register entry.
5416 For specific byte/word/dword register accesses like al, cl, ah, ch, r8d,
5417 r20w etc., we need to identify the DWARF register number for the
5418 corresponding 8-byte GPR.
5419
5420 This function is a hack - it relies on relative ordering of reg entries in
5421 the i386_regtab. FIXME - it will be good to allow a more direct way to get
5422 this information. */
5423
5424static unsigned int
5425ginsn_dw2_regnum (const reg_entry *ireg)
5426{
c7defc53 5427 const reg_entry *temp = ireg;
066673f6 5428 unsigned int dwarf_reg = Dw2Inval, idx = 0;
c7defc53
IB
5429
5430 /* ginsn creation is available for AMD64 abi only ATM. Other flag_code
5431 are not expected. */
5432 gas_assert (ireg && flag_code == CODE_64BIT);
5433
5434 /* Watch out for RegIP, RegIZ. These are expected to appear only with
5435 base/index addressing modes. Although creating inaccurate data
5436 dependencies, using a dummy value (lets say volatile register rsi) will
5437 not hurt SCFI. TBD_GINSN_GEN_NOT_SCFI. */
5438 if (ireg->reg_num == RegIP || ireg->reg_num == RegIZ)
5439 return GINSN_DW2_REGNUM_RSI_DUMMY;
5440
fabb73d1 5441 dwarf_reg = ireg->dw2_regnum[object_64bit];
c7defc53
IB
5442
5443 if (dwarf_reg == Dw2Inval)
5444 {
5445 if (ireg <= &i386_regtab[3])
5446 /* For al, cl, dl, bl, bump over to axl, cxl, dxl, bxl respectively by
5447 adding 8. */
5448 temp = ireg + 8;
5449 else if (ireg <= &i386_regtab[7])
5450 /* For ah, ch, dh, bh, bump over to axl, cxl, dxl, bxl respectively by
5451 adding 4. */
5452 temp = ireg + 4;
5453 else
5454 {
5455 /* The code relies on the relative ordering of the reg entries in
5456 i386_regtab. There are 32 register entries between axl-r31b,
5457 ax-r31w etc. The assertions here ensures the code does not
5458 recurse indefinitely. */
5459 gas_assert ((temp - &i386_regtab[0]) >= 0);
5460 idx = temp - &i386_regtab[0];
5461 gas_assert (idx + 32 < i386_regtab_size - 1);
5462
5463 temp = temp + 32;
5464 }
5465
5466 dwarf_reg = ginsn_dw2_regnum (temp);
5467 }
5468
5469 /* Sanity check - failure may indicate state corruption, bad ginsn or
5470 perhaps the i386-reg table and the current function got out of sync. */
066673f6 5471 gas_assert (dwarf_reg < Dw2Inval);
c7defc53 5472
066673f6 5473 return dwarf_reg;
c7defc53
IB
5474}
5475
5476static ginsnS *
5477x86_ginsn_addsub_reg_mem (const symbolS *insn_end_sym)
5478{
5479 unsigned int dw2_regnum;
5480 unsigned int src1_dw2_regnum;
5481 ginsnS *ginsn = NULL;
5482 ginsnS * (*ginsn_func) (const symbolS *, bool,
5483 enum ginsn_src_type, unsigned int, offsetT,
5484 enum ginsn_src_type, unsigned int, offsetT,
5485 enum ginsn_dst_type, unsigned int, offsetT);
5486 uint16_t opcode = i.tm.base_opcode;
5487
5488 gas_assert (i.tm.opcode_space == SPACE_BASE
5489 && (opcode == 0x1 || opcode == 0x29));
5490 ginsn_func = (opcode == 0x1) ? ginsn_new_add : ginsn_new_sub;
5491
5492 /* op %reg, symbol or even other cases where destination involves indirect
5493 access are unnecessary for SCFI correctness. TBD_GINSN_GEN_NOT_SCFI. */
5494 if (i.mem_operands)
5495 return ginsn;
5496
f8c50ae2
IB
5497 /* Skip detection of 8/16/32-bit op size; 'add/sub reg, reg/mem' ops always
5498 make the dest reg untraceable for SCFI. */
5499
c7defc53
IB
5500 /* op reg, reg/mem. */
5501 src1_dw2_regnum = ginsn_dw2_regnum (i.op[0].regs);
5502 /* Of interest only when second opnd is not memory. */
5503 if (i.reg_operands == 2)
5504 {
5505 dw2_regnum = ginsn_dw2_regnum (i.op[1].regs);
5506 ginsn = ginsn_func (insn_end_sym, true,
5507 GINSN_SRC_REG, src1_dw2_regnum, 0,
5508 GINSN_SRC_REG, dw2_regnum, 0,
5509 GINSN_DST_REG, dw2_regnum, 0);
5510 ginsn_set_where (ginsn);
5511 }
5512
5513 return ginsn;
5514}
5515
5516static ginsnS *
5517x86_ginsn_addsub_mem_reg (const symbolS *insn_end_sym)
5518{
5519 unsigned int dw2_regnum;
5520 unsigned int src1_dw2_regnum;
5521 const reg_entry *mem_reg;
5522 int32_t gdisp = 0;
5523 ginsnS *ginsn = NULL;
5524 ginsnS * (*ginsn_func) (const symbolS *, bool,
5525 enum ginsn_src_type, unsigned int, offsetT,
5526 enum ginsn_src_type, unsigned int, offsetT,
5527 enum ginsn_dst_type, unsigned int, offsetT);
5528 uint16_t opcode = i.tm.base_opcode;
5529
5530 gas_assert (i.tm.opcode_space == SPACE_BASE
5531 && (opcode == 0x3 || opcode == 0x2b));
5532 ginsn_func = (opcode == 0x3) ? ginsn_new_add : ginsn_new_sub;
5533
5534 /* op symbol, %reg. */
5535 if (i.mem_operands && !i.base_reg && !i.index_reg)
5536 return ginsn;
5537
f8c50ae2
IB
5538 /* Skip detection of 8/16/32-bit op size; 'add/sub reg/mem, reg' ops always
5539 make the dest reg untraceable for SCFI. */
5540
c7defc53
IB
5541 /* op reg/mem, %reg. */
5542 dw2_regnum = ginsn_dw2_regnum (i.op[1].regs);
5543
5544 if (i.reg_operands == 2)
5545 {
5546 src1_dw2_regnum = ginsn_dw2_regnum (i.op[0].regs);
5547 ginsn = ginsn_func (insn_end_sym, true,
5548 GINSN_SRC_REG, src1_dw2_regnum, 0,
5549 GINSN_SRC_REG, dw2_regnum, 0,
5550 GINSN_DST_REG, dw2_regnum, 0);
5551 ginsn_set_where (ginsn);
5552 }
5553 else if (i.mem_operands)
5554 {
5555 mem_reg = (i.base_reg) ? i.base_reg : i.index_reg;
5556 src1_dw2_regnum = ginsn_dw2_regnum (mem_reg);
5557 if (i.disp_operands == 1)
5558 gdisp = i.op[0].disps->X_add_number;
5559 ginsn = ginsn_func (insn_end_sym, true,
5560 GINSN_SRC_INDIRECT, src1_dw2_regnum, gdisp,
5561 GINSN_SRC_REG, dw2_regnum, 0,
5562 GINSN_DST_REG, dw2_regnum, 0);
5563 ginsn_set_where (ginsn);
5564 }
5565
5566 return ginsn;
5567}
5568
5569static ginsnS *
5570x86_ginsn_alu_imm (const symbolS *insn_end_sym)
5571{
5572 offsetT src_imm;
5573 unsigned int dw2_regnum;
5574 ginsnS *ginsn = NULL;
5575 enum ginsn_src_type src_type = GINSN_SRC_REG;
5576 enum ginsn_dst_type dst_type = GINSN_DST_REG;
5577
5578 ginsnS * (*ginsn_func) (const symbolS *, bool,
5579 enum ginsn_src_type, unsigned int, offsetT,
5580 enum ginsn_src_type, unsigned int, offsetT,
5581 enum ginsn_dst_type, unsigned int, offsetT);
5582
5583 /* FIXME - create ginsn where dest is REG_SP / REG_FP only ? */
5584 /* Map for insn.tm.extension_opcode
5585 000 ADD 100 AND
5586 001 OR 101 SUB
5587 010 ADC 110 XOR
5588 011 SBB 111 CMP */
5589
5590 /* add/sub/and imm, %reg only at this time for SCFI.
5591 Although all three ('and', 'or' , 'xor') make the destination reg
5592 untraceable, 'and' op is handled but not 'or' / 'xor' because we will look
5593 into supporting the DRAP pattern at some point. Other opcodes ('adc',
5594 'sbb' and 'cmp') are not generated here either. The ginsn representation
5595 does not have support for the latter three opcodes; GINSN_TYPE_OTHER may
5596 be added for these after x86_ginsn_unhandled () invocation if the
5597 destination register is REG_SP or REG_FP. */
5598 if (i.tm.extension_opcode == 5)
5599 ginsn_func = ginsn_new_sub;
5600 else if (i.tm.extension_opcode == 4)
5601 ginsn_func = ginsn_new_and;
5602 else if (i.tm.extension_opcode == 0)
5603 ginsn_func = ginsn_new_add;
5604 else
5605 return ginsn;
5606
5607 /* TBD_GINSN_REPRESENTATION_LIMIT: There is no representation for when a
5608 symbol is used as an operand, like so:
5609 addq $simd_cmp_op+8, %rdx
5610 Skip generating any ginsn for this. */
5611 if (i.imm_operands == 1
5612 && i.op[0].imms->X_op != O_constant)
5613 return ginsn;
5614
5615 /* addq $1, symbol
5616 addq $1, -16(%rbp)
5617 These are not of interest for SCFI. Also, TBD_GINSN_GEN_NOT_SCFI. */
5618 if (i.mem_operands == 1)
5619 return ginsn;
5620
f8c50ae2
IB
5621 /* 8/16/32-bit op size makes the destination reg untraceable for SCFI.
5622 Deal with this via the x86_ginsn_unhandled () code path. */
5623 if (i.suffix != QWORD_MNEM_SUFFIX)
5624 return ginsn;
5625
c7defc53
IB
5626 gas_assert (i.imm_operands == 1);
5627 src_imm = i.op[0].imms->X_add_number;
5628 /* The second operand may be a register or indirect access. For SCFI, only
5629 the case when the second opnd is a register is interesting. Revisit this
5630 if generating ginsns for a different gen mode TBD_GINSN_GEN_NOT_SCFI. */
5631 if (i.reg_operands == 1)
5632 {
5633 dw2_regnum = ginsn_dw2_regnum (i.op[1].regs);
5634 /* For ginsn, keep the imm as second src operand. */
5635 ginsn = ginsn_func (insn_end_sym, true,
5636 src_type, dw2_regnum, 0,
5637 GINSN_SRC_IMM, 0, src_imm,
5638 dst_type, dw2_regnum, 0);
5639
5640 ginsn_set_where (ginsn);
5641 }
5642
5643 return ginsn;
5644}
5645
5646/* Create ginsn(s) for MOV operations.
5647
5648 The generated ginsns corresponding to mov with indirect access to memory
5649 (src or dest) suffer with loss of information: when both index and base
5650 registers are at play, only base register gets conveyed in ginsn. Note
5651 this TBD_GINSN_GEN_NOT_SCFI. */
5652
5653static ginsnS *
5654x86_ginsn_move (const symbolS *insn_end_sym)
5655{
5656 ginsnS *ginsn = NULL;
5657 unsigned int dst_reg;
5658 unsigned int src_reg;
5659 offsetT src_disp = 0;
5660 offsetT dst_disp = 0;
5661 const reg_entry *dst = NULL;
5662 const reg_entry *src = NULL;
5663 uint16_t opcode = i.tm.base_opcode;
5664 enum ginsn_src_type src_type = GINSN_SRC_REG;
5665 enum ginsn_dst_type dst_type = GINSN_DST_REG;
5666
5667 /* mov %reg, symbol or mov symbol, %reg.
5668 Not of interest for SCFI. Also, TBD_GINSN_GEN_NOT_SCFI. */
5669 if (i.mem_operands == 1 && !i.base_reg && !i.index_reg)
5670 return ginsn;
5671
f8c50ae2
IB
5672 /* 8/16/32-bit op size makes the destination reg untraceable for SCFI.
5673 Handle mov reg, reg only. mov to or from a memory operand will make
5674 dest reg, when present, untraceable, irrespective of the op size. */
5675 if (i.reg_operands == 2 && i.suffix != QWORD_MNEM_SUFFIX)
5676 return ginsn;
5677
c7defc53
IB
5678 gas_assert (i.tm.opcode_space == SPACE_BASE);
5679 if (opcode == 0x8b || opcode == 0x8a)
5680 {
5681 /* mov disp(%reg), %reg. */
5682 if (i.mem_operands)
5683 {
5684 src = (i.base_reg) ? i.base_reg : i.index_reg;
5685 if (i.disp_operands == 1)
5686 src_disp = i.op[0].disps->X_add_number;
5687 src_type = GINSN_SRC_INDIRECT;
5688 }
5689 else
5690 src = i.op[0].regs;
5691
5692 dst = i.op[1].regs;
5693 }
5694 else if (opcode == 0x89 || opcode == 0x88)
5695 {
5696 /* mov %reg, disp(%reg). */
5697 src = i.op[0].regs;
5698 if (i.mem_operands)
5699 {
5700 dst = (i.base_reg) ? i.base_reg : i.index_reg;
5701 if (i.disp_operands == 1)
5702 dst_disp = i.op[1].disps->X_add_number;
5703 dst_type = GINSN_DST_INDIRECT;
5704 }
5705 else
5706 dst = i.op[1].regs;
5707 }
5708
5709 src_reg = ginsn_dw2_regnum (src);
5710 dst_reg = ginsn_dw2_regnum (dst);
5711
5712 ginsn = ginsn_new_mov (insn_end_sym, true,
5713 src_type, src_reg, src_disp,
5714 dst_type, dst_reg, dst_disp);
5715 ginsn_set_where (ginsn);
5716
5717 return ginsn;
5718}
5719
5720/* Generate appropriate ginsn for lea.
09812f08
IB
5721
5722 Unhandled sub-cases (marked with TBD_GINSN_GEN_NOT_SCFI) also suffer with
5723 some loss of information in the final ginsn chosen eventually (type
5724 GINSN_TYPE_OTHER). But this is fine for now for GINSN_GEN_SCFI generation
c7defc53
IB
5725 mode. */
5726
5727static ginsnS *
5728x86_ginsn_lea (const symbolS *insn_end_sym)
5729{
5730 offsetT src_disp = 0;
5731 ginsnS *ginsn = NULL;
09812f08
IB
5732 unsigned int src1_reg;
5733 const reg_entry *src1;
c7defc53
IB
5734 offsetT index_scale;
5735 unsigned int dst_reg;
09812f08 5736 bool index_regiz_p;
c7defc53 5737
03fa0c63 5738 if ((!i.base_reg) != (!i.index_reg || i.index_reg->reg_num == RegIZ))
c7defc53 5739 {
09812f08
IB
5740 /* lea disp(%base), %dst or lea disp(,%index,imm), %dst.
5741 Either index_reg or base_reg exists, but not both. Further, as per
5742 above, the case when just %index exists but is equal to RegIZ is
5743 excluded. If not excluded, a GINSN_TYPE_MOV of %rsi
5744 (GINSN_DW2_REGNUM_RSI_DUMMY) to %dst will be generated by this block.
5745 Such a mov ginsn is imprecise; so, exclude now and generate
5746 GINSN_TYPE_OTHER instead later via the x86_ginsn_unhandled ().
5747 Excluding other cases is required due to
5748 TBD_GINSN_REPRESENTATION_LIMIT. */
c7defc53 5749
c7defc53 5750 index_scale = i.log2_scale_factor;
09812f08
IB
5751 index_regiz_p = i.index_reg && i.index_reg->reg_num == RegIZ;
5752 src1 = i.base_reg ? i.base_reg : i.index_reg;
5753 src1_reg = ginsn_dw2_regnum (src1);
c7defc53 5754 dst_reg = ginsn_dw2_regnum (i.op[1].regs);
09812f08
IB
5755 /* It makes sense to represent a scale factor of 1 precisely here
5756 (i.e., not using GINSN_TYPE_OTHER, but rather similar to the
5757 base-without-index case). A non-zero scale factor is still OK if
5758 the index reg is zero reg.
5759 However, skip from here the case when disp has a symbol instead.
5760 TBD_GINSN_REPRESENTATION_LIMIT. */
5761 if ((!index_scale || index_regiz_p)
5762 && (!i.disp_operands || i.op[0].disps->X_op == O_constant))
5763 {
5764 if (i.disp_operands)
5765 src_disp = i.op[0].disps->X_add_number;
c7defc53 5766
09812f08
IB
5767 if (src_disp)
5768 /* Generate an ADD ginsn. */
5769 ginsn = ginsn_new_add (insn_end_sym, true,
5770 GINSN_SRC_REG, src1_reg, 0,
5771 GINSN_SRC_IMM, 0, src_disp,
5772 GINSN_DST_REG, dst_reg, 0);
5773 else
5774 /* Generate a MOV ginsn. */
5775 ginsn = ginsn_new_mov (insn_end_sym, true,
5776 GINSN_SRC_REG, src1_reg, 0,
5777 GINSN_DST_REG, dst_reg, 0);
5778
5779 ginsn_set_where (ginsn);
5780 }
5781 }
5782 /* Skip handling other cases here,
5783 - when (i.index_reg && i.base_reg) is true,
5784 e.g., lea disp(%base,%index,imm), %dst
5785 We do not have a ginsn representation for multiply.
5786 - or, when (!i.index_reg && !i.base_reg) is true,
5787 e.g., lea symbol, %dst
5788 Not a frequent pattern. If %dst is a register of interest, the user is
5789 likely to use a MOV op anyway.
5790 Deal with these via the x86_ginsn_unhandled () code path to generate
5791 GINSN_TYPE_OTHER when necessary. TBD_GINSN_GEN_NOT_SCFI. */
c7defc53
IB
5792
5793 return ginsn;
5794}
5795
5796static ginsnS *
5797x86_ginsn_jump (const symbolS *insn_end_sym, bool cond_p)
5798{
5799 ginsnS *ginsn = NULL;
5800 const symbolS *src_symbol;
5801 ginsnS * (*ginsn_func) (const symbolS *sym, bool real_p,
5802 enum ginsn_src_type src_type, unsigned int src_reg,
5803 const symbolS *src_ginsn_sym);
5804
5805 gas_assert (i.disp_operands == 1);
5806
5807 ginsn_func = cond_p ? ginsn_new_jump_cond : ginsn_new_jump;
5808 if (i.op[0].disps->X_op == O_symbol && !i.op[0].disps->X_add_number)
5809 {
5810 src_symbol = i.op[0].disps->X_add_symbol;
5811 ginsn = ginsn_func (insn_end_sym, true,
5812 GINSN_SRC_SYMBOL, 0, src_symbol);
5813
5814 ginsn_set_where (ginsn);
5815 }
5816 else
5817 {
5818 /* A non-zero addend in jump/JCC target makes control-flow tracking
5819 difficult. Skip SCFI for now. */
5820 as_bad (_("SCFI: `%s' insn with non-zero addend to sym not supported"),
5821 cond_p ? "JCC" : "jmp");
5822 return ginsn;
5823 }
5824
5825 return ginsn;
5826}
5827
5828static ginsnS *
5829x86_ginsn_enter (const symbolS *insn_end_sym)
5830{
5831 ginsnS *ginsn = NULL;
5832 ginsnS *ginsn_next = NULL;
5833 ginsnS *ginsn_last = NULL;
5834 /* In 64-bit mode, the default stack update size is 8 bytes. */
5835 int stack_opnd_size = 8;
5836
5837 gas_assert (i.imm_operands == 2);
5838
5839 /* For non-zero size operands, bail out as untraceable for SCFI. */
5840 if (i.op[0].imms->X_op != O_constant || i.op[0].imms->X_add_symbol != 0
5841 || i.op[1].imms->X_op != O_constant || i.op[1].imms->X_add_symbol != 0)
5842 {
5843 as_bad ("SCFI: enter insn with non-zero operand not supported");
5844 return ginsn;
5845 }
5846
5847 /* Check if this is a 16-bit op. */
5848 if (ginsn_opsize_prefix_p ())
5849 stack_opnd_size = 2;
5850
5851 /* If the nesting level is 0, the processor pushes the frame pointer from
5852 the BP/EBP/RBP register onto the stack, copies the current stack
5853 pointer from the SP/ESP/RSP register into the BP/EBP/RBP register, and
5854 loads the SP/ESP/RSP register with the current stack-pointer value
5855 minus the value in the size operand. */
5856 ginsn = ginsn_new_sub (insn_end_sym, false,
5857 GINSN_SRC_REG, REG_SP, 0,
5858 GINSN_SRC_IMM, 0, stack_opnd_size,
5859 GINSN_DST_REG, REG_SP, 0);
5860 ginsn_set_where (ginsn);
5861 ginsn_next = ginsn_new_store (insn_end_sym, false,
5862 GINSN_SRC_REG, REG_FP,
5863 GINSN_DST_INDIRECT, REG_SP, 0);
5864 ginsn_set_where (ginsn_next);
5865 gas_assert (!ginsn_link_next (ginsn, ginsn_next));
5866 ginsn_last = ginsn_new_mov (insn_end_sym, false,
5867 GINSN_SRC_REG, REG_SP, 0,
5868 GINSN_DST_REG, REG_FP, 0);
5869 ginsn_set_where (ginsn_last);
5870 gas_assert (!ginsn_link_next (ginsn_next, ginsn_last));
5871
5872 return ginsn;
5873}
5874
5875static ginsnS *
5876x86_ginsn_leave (const symbolS *insn_end_sym)
5877{
5878 ginsnS *ginsn = NULL;
5879 ginsnS *ginsn_next = NULL;
5880 ginsnS *ginsn_last = NULL;
5881 /* In 64-bit mode, the default stack update size is 8 bytes. */
5882 int stack_opnd_size = 8;
5883
5884 /* Check if this is a 16-bit op. */
5885 if (ginsn_opsize_prefix_p ())
5886 stack_opnd_size = 2;
5887
5888 /* The 'leave' instruction copies the contents of the RBP register
5889 into the RSP register to release all stack space allocated to the
5890 procedure. */
5891 ginsn = ginsn_new_mov (insn_end_sym, false,
5892 GINSN_SRC_REG, REG_FP, 0,
5893 GINSN_DST_REG, REG_SP, 0);
5894 ginsn_set_where (ginsn);
5895 /* Then it restores the old value of the RBP register from the stack. */
5896 ginsn_next = ginsn_new_load (insn_end_sym, false,
5897 GINSN_SRC_INDIRECT, REG_SP, 0,
5898 GINSN_DST_REG, REG_FP);
5899 ginsn_set_where (ginsn_next);
5900 gas_assert (!ginsn_link_next (ginsn, ginsn_next));
5901 ginsn_last = ginsn_new_add (insn_end_sym, false,
5902 GINSN_SRC_REG, REG_SP, 0,
5903 GINSN_SRC_IMM, 0, stack_opnd_size,
5904 GINSN_DST_REG, REG_SP, 0);
5905 ginsn_set_where (ginsn_next);
5906 gas_assert (!ginsn_link_next (ginsn_next, ginsn_last));
5907
5908 return ginsn;
5909}
5910
5911/* Check if an instruction is whitelisted.
5912
5913 Some instructions may appear with REG_SP or REG_FP as destination, because
5914 which they are deemed 'interesting' for SCFI. Whitelist them here if they
5915 do not affect SCFI correctness. */
5916
5917static bool
5918x86_ginsn_safe_to_skip_p (void)
5919{
5920 bool skip_p = false;
5921 uint16_t opcode = i.tm.base_opcode;
5922
5923 switch (opcode)
5924 {
5925 case 0x80:
5926 case 0x81:
5927 case 0x83:
5928 if (i.tm.opcode_space != SPACE_BASE)
5929 break;
5930 /* cmp imm, reg/rem. */
5931 if (i.tm.extension_opcode == 7)
5932 skip_p = true;
5933 break;
5934
5935 case 0x38:
5936 case 0x39:
5937 case 0x3a:
5938 case 0x3b:
5939 if (i.tm.opcode_space != SPACE_BASE)
5940 break;
5941 /* cmp imm/reg/mem, reg/rem. */
5942 skip_p = true;
5943 break;
5944
5945 case 0xf6:
5946 case 0xf7:
5947 case 0x84:
5948 case 0x85:
5949 /* test imm/reg/mem, reg/mem. */
5950 if (i.tm.opcode_space != SPACE_BASE)
5951 break;
5952 skip_p = true;
5953 break;
5954
5955 default:
5956 break;
5957 }
5958
5959 return skip_p;
5960}
5961
5962#define X86_GINSN_UNHANDLED_NONE 0
5963#define X86_GINSN_UNHANDLED_DEST_REG 1
5964#define X86_GINSN_UNHANDLED_CFG 2
5965#define X86_GINSN_UNHANDLED_STACKOP 3
5966#define X86_GINSN_UNHANDLED_UNEXPECTED 4
5967
5968/* Check the input insn for its impact on the correctness of the synthesized
5969 CFI. Returns an error code to the caller. */
5970
5971static int
5972x86_ginsn_unhandled (void)
5973{
5974 int err = X86_GINSN_UNHANDLED_NONE;
5975 const reg_entry *reg_op;
5976 unsigned int dw2_regnum;
5977
5978 /* Keep an eye out for instructions affecting control flow. */
5979 if (i.tm.opcode_modifier.jump)
5980 err = X86_GINSN_UNHANDLED_CFG;
5981 /* Also, for any instructions involving an implicit update to the stack
5982 pointer. */
5983 else if (i.tm.opcode_modifier.operandconstraint == IMPLICIT_STACK_OP)
5984 err = X86_GINSN_UNHANDLED_STACKOP;
5985 /* Finally, also check if the missed instructions are affecting REG_SP or
5986 REG_FP. The destination operand is the last at all stages of assembly
5987 (due to following AT&T syntax layout in the internal representation). In
5988 case of Intel syntax input, this still remains true as swap_operands ()
5989 is done by now.
5990 PS: These checks do not involve index / base reg, as indirect memory
5991 accesses via REG_SP or REG_FP do not affect SCFI correctness.
5992 (Also note these instructions are candidates for other ginsn generation
5993 modes in future. TBD_GINSN_GEN_NOT_SCFI.) */
5994 else if (i.operands && i.reg_operands
5995 && !(i.flags[i.operands - 1] & Operand_Mem))
5996 {
5997 reg_op = i.op[i.operands - 1].regs;
5998 if (reg_op)
5999 {
6000 dw2_regnum = ginsn_dw2_regnum (reg_op);
6001 if (dw2_regnum == REG_SP || dw2_regnum == REG_FP)
6002 err = X86_GINSN_UNHANDLED_DEST_REG;
6003 }
6004 else
6005 /* Something unexpected. Indicate to caller. */
6006 err = X86_GINSN_UNHANDLED_UNEXPECTED;
6007 }
6008
6009 return err;
6010}
6011
6012/* Generate one or more generic GAS instructions, a.k.a, ginsns for the current
6013 machine instruction.
6014
6015 Returns the head of linked list of ginsn(s) added, if success; Returns NULL
6016 if failure.
6017
6018 The input ginsn_gen_mode GMODE determines the set of minimal necessary
6019 ginsns necessary for correctness of any passes applicable for that mode.
6020 For supporting the GINSN_GEN_SCFI generation mode, following is the list of
6021 machine instructions that must be translated into the corresponding ginsns
6022 to ensure correctness of SCFI:
6023 - All instructions affecting the two registers that could potentially
6024 be used as the base register for CFA tracking. For SCFI, the base
6025 register for CFA tracking is limited to REG_SP and REG_FP only for
6026 now.
6027 - All change of flow instructions: conditional and unconditional branches,
6028 call and return from functions.
6029 - All instructions that can potentially be a register save / restore
6030 operation.
6031 - All instructions that perform stack manipulation implicitly: the CALL,
6032 RET, PUSH, POP, ENTER, and LEAVE instructions.
6033
6034 The function currently supports GINSN_GEN_SCFI ginsn generation mode only.
6035 To support other generation modes will require work on this target-specific
6036 process of creation of ginsns:
6037 - Some of such places are tagged with TBD_GINSN_GEN_NOT_SCFI to serve as
6038 possible starting points.
6039 - Also note that ginsn representation may need enhancements. Specifically,
6040 note some TBD_GINSN_INFO_LOSS and TBD_GINSN_REPRESENTATION_LIMIT markers.
6041 */
6042
6043static ginsnS *
6044x86_ginsn_new (const symbolS *insn_end_sym, enum ginsn_gen_mode gmode)
6045{
6046 int err = 0;
6047 uint16_t opcode;
6048 unsigned int dw2_regnum;
6049 const reg_entry *mem_reg;
6050 ginsnS *ginsn = NULL;
6051 ginsnS *ginsn_next = NULL;
6052 /* In 64-bit mode, the default stack update size is 8 bytes. */
6053 int stack_opnd_size = 8;
6054
6055 /* Currently supports generation of selected ginsns, sufficient for
6056 the use-case of SCFI only. */
6057 if (gmode != GINSN_GEN_SCFI)
6058 return ginsn;
6059
6060 opcode = i.tm.base_opcode;
6061
6062 /* Until it is clear how to handle APX NDD and other new opcodes, disallow
6063 them from SCFI. */
6064 if (is_apx_rex2_encoding ()
6065 || (i.tm.opcode_modifier.evex && is_apx_evex_encoding ()))
6066 {
6067 as_bad (_("SCFI: unsupported APX op %#x may cause incorrect CFI"),
6068 opcode);
6069 return ginsn;
6070 }
6071
6072 switch (opcode)
6073 {
f8c50ae2
IB
6074
6075 /* Add opcodes 0x0/0x2 and sub opcodes 0x28/0x2a (with opcode_space
6076 SPACE_BASE) are 8-bit ops. While they are relevant for SCFI
6077 correctness, skip handling them here and use the x86_ginsn_unhandled
6078 code path to generate GINSN_TYPE_OTHER when necessary. */
6079
c7defc53
IB
6080 case 0x1: /* add reg, reg/mem. */
6081 case 0x29: /* sub reg, reg/mem. */
6082 if (i.tm.opcode_space != SPACE_BASE)
6083 break;
6084 ginsn = x86_ginsn_addsub_reg_mem (insn_end_sym);
6085 break;
6086
6087 case 0x3: /* add reg/mem, reg. */
6088 case 0x2b: /* sub reg/mem, reg. */
6089 if (i.tm.opcode_space != SPACE_BASE)
6090 break;
6091 ginsn = x86_ginsn_addsub_mem_reg (insn_end_sym);
6092 break;
6093
6094 case 0xa0: /* push fs. */
6095 case 0xa8: /* push gs. */
6096 /* push fs / push gs have opcode_space == SPACE_0F. */
6097 if (i.tm.opcode_space != SPACE_0F)
6098 break;
6099 dw2_regnum = ginsn_dw2_regnum (i.op[0].regs);
6100 /* Check if operation size is 16-bit. */
6101 if (ginsn_opsize_prefix_p ())
6102 stack_opnd_size = 2;
6103 ginsn = ginsn_new_sub (insn_end_sym, false,
6104 GINSN_SRC_REG, REG_SP, 0,
6105 GINSN_SRC_IMM, 0, stack_opnd_size,
6106 GINSN_DST_REG, REG_SP, 0);
6107 ginsn_set_where (ginsn);
6108 ginsn_next = ginsn_new_store (insn_end_sym, false,
6109 GINSN_SRC_REG, dw2_regnum,
6110 GINSN_DST_INDIRECT, REG_SP, 0);
6111 ginsn_set_where (ginsn_next);
6112 gas_assert (!ginsn_link_next (ginsn, ginsn_next));
6113 break;
6114
6115 case 0xa1: /* pop fs. */
6116 case 0xa9: /* pop gs. */
6117 /* pop fs / pop gs have opcode_space == SPACE_0F. */
6118 if (i.tm.opcode_space != SPACE_0F)
6119 break;
6120 dw2_regnum = ginsn_dw2_regnum (i.op[0].regs);
6121 /* Check if operation size is 16-bit. */
6122 if (ginsn_opsize_prefix_p ())
6123 stack_opnd_size = 2;
6124 ginsn = ginsn_new_load (insn_end_sym, false,
6125 GINSN_SRC_INDIRECT, REG_SP, 0,
6126 GINSN_DST_REG, dw2_regnum);
6127 ginsn_set_where (ginsn);
6128 ginsn_next = ginsn_new_add (insn_end_sym, false,
6129 GINSN_SRC_REG, REG_SP, 0,
6130 GINSN_SRC_IMM, 0, stack_opnd_size,
6131 GINSN_DST_REG, REG_SP, 0);
6132 ginsn_set_where (ginsn_next);
6133 gas_assert (!ginsn_link_next (ginsn, ginsn_next));
6134 break;
6135
6136 case 0x50 ... 0x57:
6137 if (i.tm.opcode_space != SPACE_BASE)
6138 break;
6139 /* push reg. */
6140 dw2_regnum = ginsn_dw2_regnum (i.op[0].regs);
6141 /* Check if operation size is 16-bit. */
6142 if (ginsn_opsize_prefix_p ())
6143 stack_opnd_size = 2;
6144 ginsn = ginsn_new_sub (insn_end_sym, false,
6145 GINSN_SRC_REG, REG_SP, 0,
6146 GINSN_SRC_IMM, 0, stack_opnd_size,
6147 GINSN_DST_REG, REG_SP, 0);
6148 ginsn_set_where (ginsn);
6149 ginsn_next = ginsn_new_store (insn_end_sym, false,
6150 GINSN_SRC_REG, dw2_regnum,
6151 GINSN_DST_INDIRECT, REG_SP, 0);
6152 ginsn_set_where (ginsn_next);
6153 gas_assert (!ginsn_link_next (ginsn, ginsn_next));
6154 break;
6155
6156 case 0x58 ... 0x5f:
6157 if (i.tm.opcode_space != SPACE_BASE)
6158 break;
6159 /* pop reg. */
6160 dw2_regnum = ginsn_dw2_regnum (i.op[0].regs);
6161 ginsn = ginsn_new_load (insn_end_sym, false,
6162 GINSN_SRC_INDIRECT, REG_SP, 0,
6163 GINSN_DST_REG, dw2_regnum);
6164 ginsn_set_where (ginsn);
6165 /* Check if operation size is 16-bit. */
6166 if (ginsn_opsize_prefix_p ())
6167 stack_opnd_size = 2;
6168 ginsn_next = ginsn_new_add (insn_end_sym, false,
6169 GINSN_SRC_REG, REG_SP, 0,
6170 GINSN_SRC_IMM, 0, stack_opnd_size,
6171 GINSN_DST_REG, REG_SP, 0);
6172 ginsn_set_where (ginsn_next);
6173 gas_assert (!ginsn_link_next (ginsn, ginsn_next));
6174 break;
6175
6176 case 0x6a: /* push imm8. */
6177 case 0x68: /* push imm16/imm32. */
6178 if (i.tm.opcode_space != SPACE_BASE)
6179 break;
6180 /* Check if operation size is 16-bit. */
6181 if (ginsn_opsize_prefix_p ())
6182 stack_opnd_size = 2;
6183 /* Skip getting the value of imm from machine instruction
6184 because this is not important for SCFI. */
6185 ginsn = ginsn_new_sub (insn_end_sym, false,
6186 GINSN_SRC_REG, REG_SP, 0,
6187 GINSN_SRC_IMM, 0, stack_opnd_size,
6188 GINSN_DST_REG, REG_SP, 0);
6189 ginsn_set_where (ginsn);
6190 ginsn_next = ginsn_new_store (insn_end_sym, false,
6191 GINSN_SRC_IMM, 0,
6192 GINSN_DST_INDIRECT, REG_SP, 0);
6193 ginsn_set_where (ginsn_next);
6194 gas_assert (!ginsn_link_next (ginsn, ginsn_next));
6195 break;
6196
6197 /* PS: Opcodes 0x80 ... 0x8f with opcode_space SPACE_0F are present
6198 only after relaxation. They do not need to be handled for ginsn
6199 creation. */
6200 case 0x70 ... 0x7f:
6201 if (i.tm.opcode_space != SPACE_BASE)
6202 break;
6203 ginsn = x86_ginsn_jump (insn_end_sym, true);
6204 break;
6205
6206 case 0x80:
6207 case 0x81:
6208 case 0x83:
6209 if (i.tm.opcode_space != SPACE_BASE)
6210 break;
6211 ginsn = x86_ginsn_alu_imm (insn_end_sym);
6212 break;
6213
6214 case 0x8a: /* mov r/m8, r8. */
6215 case 0x8b: /* mov r/m(16/32/64), r(16/32/64). */
6216 case 0x88: /* mov r8, r/m8. */
6217 case 0x89: /* mov r(16/32/64), r/m(16/32/64). */
6218 if (i.tm.opcode_space != SPACE_BASE)
6219 break;
6220 ginsn = x86_ginsn_move (insn_end_sym);
6221 break;
6222
6223 case 0x8d:
6224 if (i.tm.opcode_space != SPACE_BASE)
6225 break;
09812f08 6226 /* lea disp(%base,%index,imm), %dst. */
c7defc53
IB
6227 ginsn = x86_ginsn_lea (insn_end_sym);
6228 break;
6229
6230 case 0x8f:
6231 if (i.tm.opcode_space != SPACE_BASE)
6232 break;
6233 /* pop to reg/mem. */
6234 if (i.mem_operands)
6235 {
6236 mem_reg = (i.base_reg) ? i.base_reg : i.index_reg;
6237 /* Use dummy register if no base or index. Unlike other opcodes,
6238 ginsns must be generated as this affect stack pointer. */
6239 dw2_regnum = (mem_reg
6240 ? ginsn_dw2_regnum (mem_reg)
6241 : GINSN_DW2_REGNUM_RSI_DUMMY);
6242 }
6243 else
6244 dw2_regnum = ginsn_dw2_regnum (i.op[0].regs);
6245 ginsn = ginsn_new_load (insn_end_sym, false,
6246 GINSN_SRC_INDIRECT, REG_SP, 0,
6247 GINSN_DST_INDIRECT, dw2_regnum);
6248 ginsn_set_where (ginsn);
6249 /* Check if operation size is 16-bit. */
6250 if (ginsn_opsize_prefix_p ())
6251 stack_opnd_size = 2;
6252 ginsn_next = ginsn_new_add (insn_end_sym, false,
6253 GINSN_SRC_REG, REG_SP, 0,
6254 GINSN_SRC_IMM, 0, stack_opnd_size,
6255 GINSN_DST_REG, REG_SP, 0);
6256 ginsn_set_where (ginsn_next);
6257 gas_assert (!ginsn_link_next (ginsn, ginsn_next));
6258 break;
6259
6260 case 0x9c:
6261 if (i.tm.opcode_space != SPACE_BASE)
6262 break;
6263 /* pushf / pushfq. */
6264 /* Check if operation size is 16-bit. */
6265 if (ginsn_opsize_prefix_p ())
6266 stack_opnd_size = 2;
6267 ginsn = ginsn_new_sub (insn_end_sym, false,
6268 GINSN_SRC_REG, REG_SP, 0,
6269 GINSN_SRC_IMM, 0, stack_opnd_size,
6270 GINSN_DST_REG, REG_SP, 0);
6271 ginsn_set_where (ginsn);
6272 /* FIXME - hardcode the actual DWARF reg number value. As for SCFI
6273 correctness, although this behaves simply a placeholder value; its
6274 just clearer if the value is correct. */
6275 dw2_regnum = GINSN_DW2_REGNUM_EFLAGS;
6276 ginsn_next = ginsn_new_store (insn_end_sym, false,
6277 GINSN_SRC_REG, dw2_regnum,
6278 GINSN_DST_INDIRECT, REG_SP, 0);
6279 ginsn_set_where (ginsn_next);
6280 gas_assert (!ginsn_link_next (ginsn, ginsn_next));
6281 break;
6282
6283 case 0x9d:
6284 if (i.tm.opcode_space != SPACE_BASE)
6285 break;
6286 /* popf / popfq. */
6287 /* Check if operation size is 16-bit. */
6288 if (ginsn_opsize_prefix_p ())
6289 stack_opnd_size = 2;
6290 /* FIXME - hardcode the actual DWARF reg number value. As for SCFI
6291 correctness, although this behaves simply a placeholder value; its
6292 just clearer if the value is correct. */
6293 dw2_regnum = GINSN_DW2_REGNUM_EFLAGS;
6294 ginsn = ginsn_new_load (insn_end_sym, false,
6295 GINSN_SRC_INDIRECT, REG_SP, 0,
6296 GINSN_DST_REG, dw2_regnum);
6297 ginsn_set_where (ginsn);
6298 ginsn_next = ginsn_new_add (insn_end_sym, false,
6299 GINSN_SRC_REG, REG_SP, 0,
6300 GINSN_SRC_IMM, 0, stack_opnd_size,
6301 GINSN_DST_REG, REG_SP, 0);
6302 ginsn_set_where (ginsn_next);
6303 gas_assert (!ginsn_link_next (ginsn, ginsn_next));
6304 break;
6305
6306 case 0xff:
6307 if (i.tm.opcode_space != SPACE_BASE)
6308 break;
6309 /* push from reg/mem. */
6310 if (i.tm.extension_opcode == 6)
6311 {
6312 /* Check if operation size is 16-bit. */
6313 if (ginsn_opsize_prefix_p ())
6314 stack_opnd_size = 2;
6315 ginsn = ginsn_new_sub (insn_end_sym, false,
6316 GINSN_SRC_REG, REG_SP, 0,
6317 GINSN_SRC_IMM, 0, stack_opnd_size,
6318 GINSN_DST_REG, REG_SP, 0);
6319 ginsn_set_where (ginsn);
6320 if (i.mem_operands)
6321 {
6322 mem_reg = (i.base_reg) ? i.base_reg : i.index_reg;
6323 /* Use dummy register if no base or index. Unlike other opcodes,
6324 ginsns must be generated as this affect stack pointer. */
6325 dw2_regnum = (mem_reg
6326 ? ginsn_dw2_regnum (mem_reg)
6327 : GINSN_DW2_REGNUM_RSI_DUMMY);
6328 }
6329 else
6330 dw2_regnum = ginsn_dw2_regnum (i.op[0].regs);
6331 ginsn_next = ginsn_new_store (insn_end_sym, false,
6332 GINSN_SRC_INDIRECT, dw2_regnum,
6333 GINSN_DST_INDIRECT, REG_SP, 0);
6334 ginsn_set_where (ginsn_next);
6335 gas_assert (!ginsn_link_next (ginsn, ginsn_next));
6336 }
6337 else if (i.tm.extension_opcode == 4)
6338 {
6339 /* jmp r/m. E.g., notrack jmp *%rax. */
6340 if (i.reg_operands)
6341 {
6342 dw2_regnum = ginsn_dw2_regnum (i.op[0].regs);
6343 ginsn = ginsn_new_jump (insn_end_sym, true,
6344 GINSN_SRC_REG, dw2_regnum, NULL);
6345 ginsn_set_where (ginsn);
6346 }
6347 else if (i.mem_operands && i.index_reg)
6348 {
6349 /* jmp *0x0(,%rax,8). */
6350 dw2_regnum = ginsn_dw2_regnum (i.index_reg);
6351 ginsn = ginsn_new_jump (insn_end_sym, true,
6352 GINSN_SRC_REG, dw2_regnum, NULL);
6353 ginsn_set_where (ginsn);
6354 }
6355 else if (i.mem_operands && i.base_reg)
6356 {
6357 dw2_regnum = ginsn_dw2_regnum (i.base_reg);
6358 ginsn = ginsn_new_jump (insn_end_sym, true,
6359 GINSN_SRC_REG, dw2_regnum, NULL);
6360 ginsn_set_where (ginsn);
6361 }
6362 }
6363 else if (i.tm.extension_opcode == 2)
6364 {
6365 /* 0xFF /2 (call). */
6366 if (i.reg_operands)
6367 {
6368 dw2_regnum = ginsn_dw2_regnum (i.op[0].regs);
6369 ginsn = ginsn_new_call (insn_end_sym, true,
6370 GINSN_SRC_REG, dw2_regnum, NULL);
6371 ginsn_set_where (ginsn);
6372 }
6373 else if (i.mem_operands && i.base_reg)
6374 {
6375 dw2_regnum = ginsn_dw2_regnum (i.base_reg);
6376 ginsn = ginsn_new_call (insn_end_sym, true,
6377 GINSN_SRC_REG, dw2_regnum, NULL);
6378 ginsn_set_where (ginsn);
6379 }
6380 }
6381 break;
6382
6383 case 0xc2: /* ret imm16. */
6384 case 0xc3: /* ret. */
6385 if (i.tm.opcode_space != SPACE_BASE)
6386 break;
6387 /* Near ret. */
6388 ginsn = ginsn_new_return (insn_end_sym, true);
6389 ginsn_set_where (ginsn);
6390 break;
6391
6392 case 0xc8:
6393 if (i.tm.opcode_space != SPACE_BASE)
6394 break;
6395 /* enter. */
6396 ginsn = x86_ginsn_enter (insn_end_sym);
6397 break;
6398
6399 case 0xc9:
6400 if (i.tm.opcode_space != SPACE_BASE)
6401 break;
6402 /* leave. */
6403 ginsn = x86_ginsn_leave (insn_end_sym);
6404 break;
6405
6406 case 0xe0 ... 0xe2: /* loop / loope / loopne. */
6407 case 0xe3: /* jecxz / jrcxz. */
6408 if (i.tm.opcode_space != SPACE_BASE)
6409 break;
6410 ginsn = x86_ginsn_jump (insn_end_sym, true);
6411 ginsn_set_where (ginsn);
6412 break;
6413
6414 case 0xe8:
6415 if (i.tm.opcode_space != SPACE_BASE)
6416 break;
6417 /* PS: SCFI machinery does not care about which func is being
6418 called. OK to skip that info. */
6419 ginsn = ginsn_new_call (insn_end_sym, true,
6420 GINSN_SRC_SYMBOL, 0, NULL);
6421 ginsn_set_where (ginsn);
6422 break;
6423
6424 /* PS: opcode 0xe9 appears only after relaxation. Skip here. */
6425 case 0xeb:
6426 /* If opcode_space != SPACE_BASE, this is not a jmp insn. Skip it
6427 for GINSN_GEN_SCFI. */
6428 if (i.tm.opcode_space != SPACE_BASE)
6429 break;
6430 /* Unconditional jmp. */
6431 ginsn = x86_ginsn_jump (insn_end_sym, false);
6432 ginsn_set_where (ginsn);
6433 break;
6434
6435 default:
6436 /* TBD_GINSN_GEN_NOT_SCFI: Skip all other opcodes uninteresting for
6437 GINSN_GEN_SCFI mode. */
6438 break;
6439 }
6440
6441 if (!ginsn && !x86_ginsn_safe_to_skip_p ())
6442 {
6443 /* For all unhandled insns that are not whitelisted, check that they do
6444 not impact SCFI correctness. */
6445 err = x86_ginsn_unhandled ();
6446 switch (err)
6447 {
6448 case X86_GINSN_UNHANDLED_NONE:
6449 break;
6450 case X86_GINSN_UNHANDLED_DEST_REG:
6451 /* Not all writes to REG_FP are harmful in context of SCFI. Simply
6452 generate a GINSN_TYPE_OTHER with destination set to the
6453 appropriate register. The SCFI machinery will bail out if this
6454 ginsn affects SCFI correctness. */
6455 dw2_regnum = ginsn_dw2_regnum (i.op[i.operands - 1].regs);
6456 ginsn = ginsn_new_other (insn_end_sym, true,
6457 GINSN_SRC_IMM, 0,
6458 GINSN_SRC_IMM, 0,
6459 GINSN_DST_REG, dw2_regnum);
6460 ginsn_set_where (ginsn);
6461 break;
6462 case X86_GINSN_UNHANDLED_CFG:
6463 case X86_GINSN_UNHANDLED_STACKOP:
6464 as_bad (_("SCFI: unhandled op %#x may cause incorrect CFI"), opcode);
6465 break;
6466 case X86_GINSN_UNHANDLED_UNEXPECTED:
6467 as_bad (_("SCFI: unexpected op %#x may cause incorrect CFI"),
6468 opcode);
6469 break;
6470 default:
6471 abort ();
6472 break;
6473 }
6474 }
6475
6476 return ginsn;
6477}
6478
6479#endif
6480
252b5132
RH
6481/* This is the guts of the machine-dependent assembler. LINE points to a
6482 machine dependent instruction. This function is supposed to emit
6483 the frags/bytes it assembles to. */
6484
6485void
65da13b5 6486md_assemble (char *line)
252b5132 6487{
40fb9820 6488 unsigned int j;
9db83a32 6489 char mnemonic[MAX_MNEM_SIZE], mnem_suffix = 0, *copy = NULL;
04784e33
JB
6490 const char *end, *pass1_mnem = NULL;
6491 enum i386_error pass1_err = 0;
d3ce72d0 6492 const insn_template *t;
b5482fe5
JB
6493 struct last_insn *last_insn
6494 = &seg_info(now_seg)->tc_segment_info_data.last_insn;
252b5132 6495
47926f60 6496 /* Initialize globals. */
d3b01414 6497 current_templates.end = current_templates.start = NULL;
04784e33 6498 retry:
edd67638 6499 init_globals ();
252b5132 6500
175ce60f
JB
6501 /* Suppress optimization when the last thing we saw may not have been
6502 a proper instruction (e.g. a stand-alone prefix or .byte). */
6503 if (last_insn->kind != last_insn_other)
6504 i.no_optimize = true;
6505
252b5132
RH
6506 /* First parse an instruction mnemonic & call i386_operand for the operands.
6507 We assume that the scrubber has arranged it so that line[0] is the valid
47926f60 6508 start of a (possibly prefixed) mnemonic. */
252b5132 6509
edd67638 6510 end = parse_insn (line, mnemonic, false);
5317ad2c 6511 if (end == NULL)
04784e33
JB
6512 {
6513 if (pass1_mnem != NULL)
6514 goto match_error;
9db83a32
JB
6515 if (i.error != no_error)
6516 {
d3b01414
JB
6517 gas_assert (current_templates.start != NULL);
6518 if (may_need_pass2 (current_templates.start) && !i.suffix)
9db83a32
JB
6519 goto no_match;
6520 /* No point in trying a 2nd pass - it'll only find the same suffix
6521 again. */
6522 mnem_suffix = i.suffix;
6523 goto match_error;
6524 }
04784e33
JB
6525 return;
6526 }
d3b01414 6527 t = current_templates.start;
6d86a545 6528 if (may_need_pass2 (t))
04784e33
JB
6529 {
6530 /* Make a copy of the full line in case we need to retry. */
6531 copy = xstrdup (line);
6532 }
5317ad2c 6533 line += end - line;
83b16ac6 6534 mnem_suffix = i.suffix;
252b5132 6535
29b0f896 6536 line = parse_operands (line, mnemonic);
ee86248c 6537 this_operand = -1;
29b0f896 6538 if (line == NULL)
04784e33
JB
6539 {
6540 free (copy);
6541 return;
6542 }
252b5132 6543
29b0f896
AM
6544 /* Now we've parsed the mnemonic into a set of templates, and have the
6545 operands at hand. */
6546
b630c145 6547 /* All Intel opcodes have reversed operands except for "bound", "enter",
c0e54661 6548 "invlpg*", "monitor*", "mwait*", "tpause", "umwait", "pvalidate",
b0e8fa7f
TJ
6549 "rmpadjust", "rmpupdate", and "rmpquery". We also don't reverse
6550 intersegment "jmp" and "call" instructions with 2 immediate operands so
6551 that the immediate segment precedes the offset consistently in Intel and
6552 AT&T modes. */
4d456e3d
L
6553 if (intel_syntax
6554 && i.operands > 1
6d86a545
JB
6555 && (t->mnem_off != MN_bound)
6556 && !startswith (mnemonic, "invlpg")
d34049e8
ML
6557 && !startswith (mnemonic, "monitor")
6558 && !startswith (mnemonic, "mwait")
6d86a545 6559 && (t->mnem_off != MN_pvalidate)
d34049e8 6560 && !startswith (mnemonic, "rmp")
6d86a545
JB
6561 && (t->mnem_off != MN_tpause)
6562 && (t->mnem_off != MN_umwait)
47c0279b
JB
6563 && !(i.operands == 2
6564 && operand_type_check (i.types[0], imm)
40fb9820 6565 && operand_type_check (i.types[1], imm)))
29b0f896
AM
6566 swap_operands ();
6567
e7d74879
JB
6568 /* The order of the immediates should be reversed for 2-immediates EXTRQ
6569 and INSERTQ instructions. Also UWRMSR wants its immediate to be in the
6570 "canonical" place (first), despite it appearing last (in AT&T syntax, or
6571 because of the swapping above) in the incoming set of operands. */
6572 if ((i.imm_operands == 2
6573 && (t->mnem_off == MN_extrq || t->mnem_off == MN_insertq))
6574 || (t->mnem_off == MN_uwrmsr && i.imm_operands
6575 && i.operands > i.imm_operands))
ec56d5c0
JB
6576 swap_2_operands (0, 1);
6577
29b0f896 6578 if (i.imm_operands)
8170af78
HL
6579 {
6580 /* For USER_MSR instructions, imm32 stands for the name of an model specific
6581 register (MSR). That's an unsigned quantity, whereas all other insns with
6582 32-bit immediate and 64-bit operand size use sign-extended
6583 immediates (imm32s). Therefore these insns are special-cased, bypassing
6584 the normal handling of immediates here. */
d3b01414 6585 if (is_cpu(current_templates.start, CpuUSER_MSR))
8170af78
HL
6586 {
6587 for (j = 0; j < i.operands; j++)
6588 {
6589 if (operand_type_check(i.types[j], imm))
6590 i.types[j] = smallest_imm_type (i.op[j].imms->X_add_number);
6591 }
6592 }
6593 else
6594 optimize_imm ();
6595 }
29b0f896 6596
0de704b9
JB
6597 if (i.disp_operands && !optimize_disp (t))
6598 return;
29b0f896
AM
6599
6600 /* Next, we find a template that matches the given insn,
6601 making sure the overlap of the given operands types is consistent
6602 with the template operand types. */
252b5132 6603
83b16ac6 6604 if (!(t = match_template (mnem_suffix)))
04784e33
JB
6605 {
6606 const char *err_msg;
6607
6608 if (copy && !mnem_suffix)
6609 {
6610 line = copy;
6611 copy = NULL;
9db83a32 6612 no_match:
04784e33 6613 pass1_err = i.error;
d3b01414 6614 pass1_mnem = insn_name (current_templates.start);
04784e33
JB
6615 goto retry;
6616 }
9db83a32
JB
6617
6618 /* If a non-/only-64bit template (group) was found in pass 1, and if
6619 _some_ template (group) was found in pass 2, squash pass 1's
6620 error. */
6621 if (pass1_err == unsupported_64bit)
6622 pass1_mnem = NULL;
6623
04784e33 6624 match_error:
9db83a32
JB
6625 free (copy);
6626
04784e33
JB
6627 switch (pass1_mnem ? pass1_err : i.error)
6628 {
6629 default:
6630 abort ();
6631 case operand_size_mismatch:
6632 err_msg = _("operand size mismatch");
6633 break;
6634 case operand_type_mismatch:
6635 err_msg = _("operand type mismatch");
6636 break;
6637 case register_type_mismatch:
6638 err_msg = _("register type mismatch");
6639 break;
6640 case number_of_operands_mismatch:
6641 err_msg = _("number of operands mismatch");
6642 break;
6643 case invalid_instruction_suffix:
6644 err_msg = _("invalid instruction suffix");
6645 break;
6646 case bad_imm4:
6647 err_msg = _("constant doesn't fit in 4 bits");
6648 break;
6649 case unsupported_with_intel_mnemonic:
6650 err_msg = _("unsupported with Intel mnemonic");
6651 break;
6652 case unsupported_syntax:
6653 err_msg = _("unsupported syntax");
6654 break;
80d61d8d
CL
6655 case unsupported_EGPR_for_addressing:
6656 err_msg = _("extended GPR cannot be used as base/index");
6657 break;
04784e33
JB
6658 case unsupported:
6659 as_bad (_("unsupported instruction `%s'"),
d3b01414 6660 pass1_mnem ? pass1_mnem : insn_name (current_templates.start));
04784e33 6661 return;
9db83a32
JB
6662 case unsupported_on_arch:
6663 as_bad (_("`%s' is not supported on `%s%s'"),
d3b01414 6664 pass1_mnem ? pass1_mnem : insn_name (current_templates.start),
9db83a32
JB
6665 cpu_arch_name ? cpu_arch_name : default_arch,
6666 cpu_sub_arch_name ? cpu_sub_arch_name : "");
6667 return;
6668 case unsupported_64bit:
6669 if (ISLOWER (mnem_suffix))
e8b4b7b2
NC
6670 {
6671 if (flag_code == CODE_64BIT)
6672 as_bad (_("`%s%c' is not supported in 64-bit mode"),
d3b01414 6673 pass1_mnem ? pass1_mnem : insn_name (current_templates.start),
e8b4b7b2
NC
6674 mnem_suffix);
6675 else
6676 as_bad (_("`%s%c' is only supported in 64-bit mode"),
d3b01414 6677 pass1_mnem ? pass1_mnem : insn_name (current_templates.start),
e8b4b7b2
NC
6678 mnem_suffix);
6679 }
9db83a32 6680 else
e8b4b7b2
NC
6681 {
6682 if (flag_code == CODE_64BIT)
6683 as_bad (_("`%s' is not supported in 64-bit mode"),
d3b01414 6684 pass1_mnem ? pass1_mnem : insn_name (current_templates.start));
e8b4b7b2
NC
6685 else
6686 as_bad (_("`%s' is only supported in 64-bit mode"),
d3b01414 6687 pass1_mnem ? pass1_mnem : insn_name (current_templates.start));
e8b4b7b2 6688 }
9db83a32 6689 return;
54294d73
JB
6690 case no_vex_encoding:
6691 err_msg = _("no VEX/XOP encoding");
6692 break;
6693 case no_evex_encoding:
6694 err_msg = _("no EVEX encoding");
6695 break;
04784e33
JB
6696 case invalid_sib_address:
6697 err_msg = _("invalid SIB address");
6698 break;
6699 case invalid_vsib_address:
6700 err_msg = _("invalid VSIB address");
6701 break;
6702 case invalid_vector_register_set:
6703 err_msg = _("mask, index, and destination registers must be distinct");
6704 break;
6705 case invalid_tmm_register_set:
6706 err_msg = _("all tmm registers must be distinct");
6707 break;
6708 case invalid_dest_and_src_register_set:
6709 err_msg = _("destination and source registers must be distinct");
6710 break;
08a98d4c
MZ
6711 case invalid_dest_register_set:
6712 err_msg = _("two dest registers must be distinct");
6713 break;
80d61d8d
CL
6714 case invalid_pseudo_prefix:
6715 err_msg = _("rex2 pseudo prefix cannot be used");
6716 break;
04784e33
JB
6717 case unsupported_vector_index_register:
6718 err_msg = _("unsupported vector index register");
6719 break;
6720 case unsupported_broadcast:
6721 err_msg = _("unsupported broadcast");
6722 break;
6723 case broadcast_needed:
6724 err_msg = _("broadcast is needed for operand of such type");
6725 break;
6726 case unsupported_masking:
6727 err_msg = _("unsupported masking");
6728 break;
6729 case mask_not_on_destination:
6730 err_msg = _("mask not on destination operand");
6731 break;
6732 case no_default_mask:
6733 err_msg = _("default mask isn't allowed");
6734 break;
6735 case unsupported_rc_sae:
6736 err_msg = _("unsupported static rounding/sae");
6737 break;
54294d73
JB
6738 case unsupported_vector_size:
6739 as_bad (_("vector size above %u required for `%s'"), 128u << vector_size,
d3b01414 6740 pass1_mnem ? pass1_mnem : insn_name (current_templates.start));
54294d73 6741 return;
08a98d4c
MZ
6742 case unsupported_rsp_register:
6743 err_msg = _("'rsp' register cannot be used");
6744 break;
58bceb18
JB
6745 case internal_error:
6746 err_msg = _("internal error");
6747 break;
04784e33
JB
6748 }
6749 as_bad (_("%s for `%s'"), err_msg,
d3b01414 6750 pass1_mnem ? pass1_mnem : insn_name (current_templates.start));
04784e33
JB
6751 return;
6752 }
6753
6754 free (copy);
252b5132 6755
7bab8ab5 6756 if (sse_check != check_none
ffb86450
JB
6757 /* The opcode space check isn't strictly needed; it's there only to
6758 bypass the logic below when easily possible. */
ddb62495
JB
6759 && t->opcode_space >= SPACE_0F
6760 && t->opcode_space <= SPACE_0F3A
734dfd1c 6761 && !is_cpu (&i.tm, CpuSSE4a)
ffb86450 6762 && !is_any_vex_encoding (t))
daf50ae7 6763 {
06360a5c
JB
6764 /* Some KL and all WideKL insns have only implicit %xmm operands. */
6765 bool simd = is_cpu (t, CpuKL) || is_cpu (t, CpuWideKL);
ffb86450
JB
6766
6767 for (j = 0; j < t->operands; ++j)
6768 {
6769 if (t->operand_types[j].bitfield.class == RegMMX)
6770 break;
6771 if (t->operand_types[j].bitfield.class == RegSIMD)
6772 simd = true;
6773 }
6774
6775 if (j >= t->operands && simd)
6776 (sse_check == check_warning
6777 ? as_warn
76d3f746 6778 : as_bad) (_("SSE instruction `%s' is used"), insn_name (&i.tm));
daf50ae7
L
6779 }
6780
40fb9820 6781 if (i.tm.opcode_modifier.fwait)
29b0f896
AM
6782 if (!add_prefix (FWAIT_OPCODE))
6783 return;
252b5132 6784
d5de92cf 6785 /* Check if REP prefix is OK. */
742732c7 6786 if (i.rep_prefix && i.tm.opcode_modifier.prefixok != PrefixRep)
d5de92cf
L
6787 {
6788 as_bad (_("invalid instruction `%s' after `%s'"),
76d3f746 6789 insn_name (&i.tm), i.rep_prefix);
d5de92cf
L
6790 return;
6791 }
6792
c1ba0266
L
6793 /* Check for lock without a lockable instruction. Destination operand
6794 must be memory unless it is xchg (0x86). */
9a4a4499
JB
6795 if (i.prefix[LOCK_PREFIX])
6796 {
6797 if (i.tm.opcode_modifier.prefixok < PrefixLock
c1ba0266
L
6798 || i.mem_operands == 0
6799 || (i.tm.base_opcode != 0x86
9a4a4499
JB
6800 && !(i.flags[i.operands - 1] & Operand_Mem)))
6801 {
6802 as_bad (_("expecting lockable instruction after `lock'"));
6803 return;
6804 }
6805
6806 /* Zap the redundant prefix from XCHG when optimizing. */
6807 if (i.tm.base_opcode == 0x86 && optimize && !i.no_optimize)
6808 i.prefix[LOCK_PREFIX] = 0;
c32fa91d
L
6809 }
6810
e3669c7f
JB
6811 if (is_any_vex_encoding (&i.tm)
6812 || i.tm.operand_types[i.imm_operands].bitfield.class >= RegMMX
6813 || i.tm.operand_types[i.imm_operands + 1].bitfield.class >= RegMMX)
7a8655d2 6814 {
e3669c7f
JB
6815 /* Check for data size prefix on VEX/XOP/EVEX encoded and SIMD insns. */
6816 if (i.prefix[DATA_PREFIX])
6817 {
76d3f746 6818 as_bad (_("data size prefix invalid with `%s'"), insn_name (&i.tm));
e3669c7f
JB
6819 return;
6820 }
6821
6822 /* Don't allow e.g. KMOV in TLS code sequences. */
6823 for (j = i.imm_operands; j < i.operands; ++j)
6824 switch (i.reloc[j])
6825 {
5bc71c2a
L
6826 case BFD_RELOC_X86_64_GOTTPOFF:
6827 if (i.tm.mnem_off == MN_add
6828 && i.tm.opcode_space == SPACE_EVEXMAP4
6829 && i.mem_operands == 1
6830 && i.base_reg
6831 && i.base_reg->reg_num == RegIP
6832 && i.tm.operand_types[0].bitfield.class == Reg
6833 && i.tm.operand_types[2].bitfield.class == Reg)
6834 /* Allow APX: add %reg1, foo@gottpoff(%rip), %reg2. */
6835 break;
6836 /* Fall through. */
e3669c7f
JB
6837 case BFD_RELOC_386_TLS_GOTIE:
6838 case BFD_RELOC_386_TLS_LE_32:
e3669c7f 6839 case BFD_RELOC_X86_64_TLSLD:
76d3f746 6840 as_bad (_("TLS relocation cannot be used with `%s'"), insn_name (&i.tm));
e3669c7f
JB
6841 return;
6842 default:
6843 break;
6844 }
7a8655d2
JB
6845 }
6846
42164a71 6847 /* Check if HLE prefix is OK. */
165de32a 6848 if (i.hle_prefix && !check_hle ())
42164a71
L
6849 return;
6850
7e8b059b
L
6851 /* Check BND prefix. */
6852 if (i.bnd_prefix && !i.tm.opcode_modifier.bndprefixok)
6853 as_bad (_("expecting valid branch instruction after `bnd'"));
6854
04ef582a 6855 /* Check NOTRACK prefix. */
742732c7 6856 if (i.notrack_prefix && i.tm.opcode_modifier.prefixok != PrefixNoTrack)
9fef80d6 6857 as_bad (_("expecting indirect branch instruction after `notrack'"));
04ef582a 6858
734dfd1c 6859 if (is_cpu (&i.tm, CpuMPX))
327e8c42
JB
6860 {
6861 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
6862 as_bad (_("32-bit address isn't allowed in 64-bit MPX instructions."));
6863 else if (flag_code != CODE_16BIT
6864 ? i.prefix[ADDR_PREFIX]
6865 : i.mem_operands && !i.prefix[ADDR_PREFIX])
6866 as_bad (_("16-bit address isn't allowed in MPX instructions"));
6867 }
7e8b059b
L
6868
6869 /* Insert BND prefix. */
76d3a78a
JB
6870 if (add_bnd_prefix && i.tm.opcode_modifier.bndprefixok)
6871 {
6872 if (!i.prefix[BND_PREFIX])
6873 add_prefix (BND_PREFIX_OPCODE);
6874 else if (i.prefix[BND_PREFIX] != BND_PREFIX_OPCODE)
6875 {
6876 as_warn (_("replacing `rep'/`repe' prefix by `bnd'"));
6877 i.prefix[BND_PREFIX] = BND_PREFIX_OPCODE;
6878 }
6879 }
7e8b059b 6880
29b0f896 6881 /* Check string instruction segment overrides. */
51c8edf6 6882 if (i.tm.opcode_modifier.isstring >= IS_STRING_ES_OP0)
29b0f896 6883 {
51c8edf6 6884 gas_assert (i.mem_operands);
29b0f896 6885 if (!check_string ())
5dd0794d 6886 return;
fc0763e6 6887 i.disp_operands = 0;
29b0f896 6888 }
5dd0794d 6889
9373f275
L
6890 /* The memory operand of (%dx) should be only used with input/output
6891 instructions (base opcodes: 0x6c, 0x6e, 0xec, 0xee). */
6892 if (i.input_output_operand
6893 && ((i.tm.base_opcode | 0x82) != 0xee
ddb62495 6894 || i.tm.opcode_space != SPACE_BASE))
9373f275
L
6895 {
6896 as_bad (_("input/output port address isn't allowed with `%s'"),
76d3f746 6897 insn_name (&i.tm));
9373f275
L
6898 return;
6899 }
6900
b6f8c7c4
L
6901 if (optimize && !i.no_optimize && i.tm.opcode_modifier.optimize)
6902 optimize_encoding ();
6903
eb3f3841
JB
6904 /* Past optimization there's no need to distinguish encoding_evex,
6905 encoding_evex512, and encoding_egpr anymore. */
e346d50a
JB
6906 if (i.encoding == encoding_evex512)
6907 i.encoding = encoding_evex;
eb3f3841
JB
6908 else if (i.encoding == encoding_egpr)
6909 i.encoding = is_any_vex_encoding (&i.tm) ? encoding_evex
6910 : encoding_default;
a6f3add0 6911
c8480b58
L
6912 if (use_unaligned_vector_move)
6913 encode_with_unaligned_vector_move ();
6914
29b0f896
AM
6915 if (!process_suffix ())
6916 return;
e413e4e9 6917
ef07be45 6918 /* Check if IP-relative addressing requirements can be satisfied. */
734dfd1c 6919 if (is_cpu (&i.tm, CpuPREFETCHI)
ef07be45 6920 && !(i.base_reg && i.base_reg->reg_num == RegIP))
76d3f746 6921 as_warn (_("'%s' only supports RIP-relative address"), insn_name (&i.tm));
ef07be45 6922
921eafea 6923 /* Update operand types and check extended states. */
bc0844ae 6924 for (j = 0; j < i.operands; j++)
921eafea
L
6925 {
6926 i.types[j] = operand_type_and (i.types[j], i.tm.operand_types[j]);
3d70986f 6927 switch (i.tm.operand_types[j].bitfield.class)
921eafea
L
6928 {
6929 default:
6930 break;
6931 case RegMMX:
6932 i.xstate |= xstate_mmx;
6933 break;
6934 case RegMask:
32930e4e 6935 i.xstate |= xstate_mask;
921eafea
L
6936 break;
6937 case RegSIMD:
3d70986f 6938 if (i.tm.operand_types[j].bitfield.tmmword)
921eafea 6939 i.xstate |= xstate_tmm;
4fc85f37 6940 else if (i.tm.operand_types[j].bitfield.zmmword
a6f3add0 6941 && !i.tm.opcode_modifier.vex
4fc85f37 6942 && vector_size >= VSZ512)
921eafea 6943 i.xstate |= xstate_zmm;
4fc85f37
JB
6944 else if (i.tm.operand_types[j].bitfield.ymmword
6945 && vector_size >= VSZ256)
921eafea 6946 i.xstate |= xstate_ymm;
3d70986f 6947 else if (i.tm.operand_types[j].bitfield.xmmword)
921eafea
L
6948 i.xstate |= xstate_xmm;
6949 break;
6950 }
6951 }
bc0844ae 6952
29b0f896
AM
6953 /* Make still unresolved immediate matches conform to size of immediate
6954 given in i.suffix. */
6955 if (!finalize_imm ())
6956 return;
252b5132 6957
40fb9820 6958 if (i.types[0].bitfield.imm1)
29b0f896 6959 i.imm_operands = 0; /* kludge for shift insns. */
252b5132 6960
29b0f896
AM
6961 /* For insns with operands there are more diddles to do to the opcode. */
6962 if (i.operands)
6963 {
6964 if (!process_operands ())
6965 return;
6966 }
255571cd 6967 else if (!quiet_warnings && i.tm.opcode_modifier.operandconstraint == UGH)
29b0f896
AM
6968 {
6969 /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc. */
76d3f746 6970 as_warn (_("translating to `%sp'"), insn_name (&i.tm));
29b0f896 6971 }
252b5132 6972
7a8655d2 6973 if (is_any_vex_encoding (&i.tm))
9e5e5283 6974 {
c1dc7af5 6975 if (!cpu_arch_flags.bitfield.cpui286)
9e5e5283 6976 {
c1dc7af5 6977 as_bad (_("instruction `%s' isn't supported outside of protected mode."),
76d3f746 6978 insn_name (&i.tm));
9e5e5283
L
6979 return;
6980 }
c0f3af97 6981
0b9404fd
JB
6982 /* Check for explicit REX prefix. */
6983 if (i.prefix[REX_PREFIX] || i.rex_encoding)
6984 {
76d3f746 6985 as_bad (_("REX prefix invalid with `%s'"), insn_name (&i.tm));
0b9404fd
JB
6986 return;
6987 }
6988
80d61d8d
CL
6989 /* Check for explicit REX2 prefix. */
6990 if (i.rex2_encoding)
6991 {
6992 as_bad (_("{rex2} prefix invalid with `%s'"), insn_name (&i.tm));
6993 return;
6994 }
6995
6177c84d
CL
6996 if (is_apx_evex_encoding ())
6997 build_apx_evex_prefix ();
6998 else if (i.tm.opcode_modifier.vex)
9e5e5283
L
6999 build_vex_prefix (t);
7000 else
7001 build_evex_prefix ();
0b9404fd
JB
7002
7003 /* The individual REX.RXBW bits got consumed. */
7004 i.rex &= REX_OPCODE;
6177c84d
CL
7005
7006 /* The rex2 bits got consumed. */
7007 i.rex2 = 0;
9e5e5283 7008 }
43234a1e 7009
7fc69528
JB
7010 /* Handle conversion of 'int $3' --> special int3 insn. */
7011 if (i.tm.mnem_off == MN_int
a6461c02 7012 && i.op[0].imms->X_add_number == 3)
29b0f896
AM
7013 {
7014 i.tm.base_opcode = INT3_OPCODE;
7015 i.imm_operands = 0;
7016 }
252b5132 7017
0cfa3eb3
JB
7018 if ((i.tm.opcode_modifier.jump == JUMP
7019 || i.tm.opcode_modifier.jump == JUMP_BYTE
7020 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896
AM
7021 && i.op[0].disps->X_op == O_constant)
7022 {
7023 /* Convert "jmp constant" (and "call constant") to a jump (call) to
7024 the absolute address given by the constant. Since ix86 jumps and
7025 calls are pc relative, we need to generate a reloc. */
7026 i.op[0].disps->X_add_symbol = &abs_symbol;
7027 i.op[0].disps->X_op = O_symbol;
7028 }
252b5132 7029
ce705688 7030 establish_rex ();
29b0f896 7031
b5482fe5 7032 insert_lfence_before (last_insn);
ae531041 7033
29b0f896 7034 /* We are ready to output the insn. */
b5482fe5 7035 output_insn (last_insn);
e379e5f3 7036
c7defc53
IB
7037#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
7038 /* PS: SCFI is enabled only for System V AMD64 ABI. The ABI check has been
7039 performed in i386_target_format. */
7040 if (IS_ELF && flag_synth_cfi)
7041 {
7042 ginsnS *ginsn;
7043 ginsn = x86_ginsn_new (symbol_temp_new_now (), frch_ginsn_gen_mode ());
7044 frch_ginsn_data_append (ginsn);
7045 }
7046#endif
7047
ae531041
L
7048 insert_lfence_after ();
7049
e379e5f3
L
7050 if (i.tm.opcode_modifier.isprefix)
7051 {
b5482fe5
JB
7052 last_insn->kind = last_insn_prefix;
7053 last_insn->name = insn_name (&i.tm);
7054 last_insn->file = as_where (&last_insn->line);
e379e5f3
L
7055 }
7056 else
b5482fe5 7057 last_insn->kind = last_insn_other;
29b0f896
AM
7058}
7059
9db83a32
JB
7060/* The Q suffix is generally valid only in 64-bit mode, with very few
7061 exceptions: fild, fistp, fisttp, and cmpxchg8b. Note that for fild
7062 and fisttp only one of their two templates is matched below: That's
7063 sufficient since other relevant attributes are the same between both
7064 respective templates. */
7065static INLINE bool q_suffix_allowed(const insn_template *t)
7066{
7067 return flag_code == CODE_64BIT
ddb62495 7068 || (t->opcode_space == SPACE_BASE
9db83a32
JB
7069 && t->base_opcode == 0xdf
7070 && (t->extension_opcode & 1)) /* fild / fistp / fisttp */
7fc69528 7071 || t->mnem_off == MN_cmpxchg8b;
9db83a32
JB
7072}
7073
5317ad2c 7074static const char *
edd67638 7075parse_insn (const char *line, char *mnemonic, bool prefix_only)
29b0f896 7076{
5317ad2c 7077 const char *l = line, *token_start = l;
29b0f896 7078 char *mnem_p;
d3b01414 7079 bool pass1 = !current_templates.start;
5c6af06e 7080 int supported;
d3ce72d0 7081 const insn_template *t;
b6169b20 7082 char *dot_p = NULL;
29b0f896 7083
29b0f896
AM
7084 while (1)
7085 {
7086 mnem_p = mnemonic;
778415f5
JB
7087 /* Pseudo-prefixes start with an opening figure brace. */
7088 if ((*mnem_p = *l) == '{')
7089 {
7090 ++mnem_p;
7091 ++l;
7092 }
29b0f896
AM
7093 while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
7094 {
b6169b20
L
7095 if (*mnem_p == '.')
7096 dot_p = mnem_p;
29b0f896
AM
7097 mnem_p++;
7098 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
45288df1 7099 {
778415f5 7100 too_long:
29b0f896
AM
7101 as_bad (_("no such instruction: `%s'"), token_start);
7102 return NULL;
7103 }
7104 l++;
7105 }
778415f5
JB
7106 /* Pseudo-prefixes end with a closing figure brace. */
7107 if (*mnemonic == '{' && *l == '}')
7108 {
7109 *mnem_p++ = *l++;
7110 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
7111 goto too_long;
7112 *mnem_p = '\0';
7113
7114 /* Point l at the closing brace if there's no other separator. */
7115 if (*l != END_OF_INSN && !is_space_char (*l)
7116 && *l != PREFIX_SEPARATOR)
7117 --l;
7118 }
7119 else if (!is_space_char (*l)
7120 && *l != END_OF_INSN
7121 && (intel_syntax
7122 || (*l != PREFIX_SEPARATOR && *l != ',')))
29b0f896 7123 {
edd67638
JB
7124 if (prefix_only)
7125 break;
29b0f896
AM
7126 as_bad (_("invalid character %s in mnemonic"),
7127 output_invalid (*l));
7128 return NULL;
7129 }
7130 if (token_start == l)
7131 {
e44823cf 7132 if (!intel_syntax && *l == PREFIX_SEPARATOR)
29b0f896
AM
7133 as_bad (_("expecting prefix; got nothing"));
7134 else
7135 as_bad (_("expecting mnemonic; got nothing"));
7136 return NULL;
7137 }
45288df1 7138
29b0f896 7139 /* Look up instruction (or prefix) via hash table. */
d3b01414 7140 op_lookup (mnemonic);
47926f60 7141
29b0f896
AM
7142 if (*l != END_OF_INSN
7143 && (!is_space_char (*l) || l[1] != END_OF_INSN)
d3b01414
JB
7144 && current_templates.start
7145 && current_templates.start->opcode_modifier.isprefix)
29b0f896 7146 {
d3b01414 7147 supported = cpu_flags_match (current_templates.start);
3086ed9a 7148 if (!(supported & CPU_FLAGS_64BIT_MATCH))
2dd88dca
JB
7149 {
7150 as_bad ((flag_code != CODE_64BIT
7151 ? _("`%s' is only supported in 64-bit mode")
7152 : _("`%s' is not supported in 64-bit mode")),
d3b01414 7153 insn_name (current_templates.start));
2dd88dca
JB
7154 return NULL;
7155 }
3086ed9a
JB
7156 if (supported != CPU_FLAGS_PERFECT_MATCH)
7157 {
7158 as_bad (_("`%s' is not supported on `%s%s'"),
d3b01414 7159 insn_name (current_templates.start),
3086ed9a
JB
7160 cpu_arch_name ? cpu_arch_name : default_arch,
7161 cpu_sub_arch_name ? cpu_sub_arch_name : "");
7162 return NULL;
7163 }
29b0f896
AM
7164 /* If we are in 16-bit mode, do not allow addr16 or data16.
7165 Similarly, in 32-bit mode, do not allow addr32 or data32. */
d3b01414
JB
7166 if ((current_templates.start->opcode_modifier.size == SIZE16
7167 || current_templates.start->opcode_modifier.size == SIZE32)
29b0f896 7168 && flag_code != CODE_64BIT
d3b01414 7169 && ((current_templates.start->opcode_modifier.size == SIZE32)
29b0f896
AM
7170 ^ (flag_code == CODE_16BIT)))
7171 {
7172 as_bad (_("redundant %s prefix"),
d3b01414 7173 insn_name (current_templates.start));
29b0f896 7174 return NULL;
45288df1 7175 }
31184569 7176
d3b01414 7177 if (current_templates.start->base_opcode == PSEUDO_PREFIX)
29b0f896 7178 {
86fa6981 7179 /* Handle pseudo prefixes. */
d3b01414 7180 switch (current_templates.start->extension_opcode)
86fa6981 7181 {
41eb8e88 7182 case Prefix_Disp8:
86fa6981
L
7183 /* {disp8} */
7184 i.disp_encoding = disp_encoding_8bit;
7185 break;
41eb8e88
L
7186 case Prefix_Disp16:
7187 /* {disp16} */
7188 i.disp_encoding = disp_encoding_16bit;
7189 break;
7190 case Prefix_Disp32:
86fa6981
L
7191 /* {disp32} */
7192 i.disp_encoding = disp_encoding_32bit;
7193 break;
41eb8e88 7194 case Prefix_Load:
86fa6981
L
7195 /* {load} */
7196 i.dir_encoding = dir_encoding_load;
7197 break;
41eb8e88 7198 case Prefix_Store:
86fa6981
L
7199 /* {store} */
7200 i.dir_encoding = dir_encoding_store;
7201 break;
41eb8e88 7202 case Prefix_VEX:
42e04b36 7203 /* {vex} */
e346d50a 7204 i.encoding = encoding_vex;
86fa6981 7205 break;
41eb8e88 7206 case Prefix_VEX3:
86fa6981 7207 /* {vex3} */
e346d50a 7208 i.encoding = encoding_vex3;
86fa6981 7209 break;
41eb8e88 7210 case Prefix_EVEX:
86fa6981 7211 /* {evex} */
e346d50a 7212 i.encoding = encoding_evex;
86fa6981 7213 break;
41eb8e88 7214 case Prefix_REX:
6b6b6807 7215 /* {rex} */
5b7c81bd 7216 i.rex_encoding = true;
6b6b6807 7217 break;
80d61d8d
CL
7218 case Prefix_REX2:
7219 /* {rex2} */
7220 i.rex2_encoding = true;
7221 break;
41eb8e88 7222 case Prefix_NoOptimize:
b6f8c7c4 7223 /* {nooptimize} */
5b7c81bd 7224 i.no_optimize = true;
b6f8c7c4 7225 break;
86fa6981
L
7226 default:
7227 abort ();
7228 }
7229 }
7230 else
7231 {
7232 /* Add prefix, checking for repeated prefixes. */
d3b01414 7233 switch (add_prefix (current_templates.start->base_opcode))
86fa6981 7234 {
4e9ac44a
L
7235 case PREFIX_EXIST:
7236 return NULL;
7237 case PREFIX_DS:
d3b01414
JB
7238 if (is_cpu (current_templates.start, CpuIBT))
7239 i.notrack_prefix = insn_name (current_templates.start);
4e9ac44a
L
7240 break;
7241 case PREFIX_REP:
d3b01414
JB
7242 if (is_cpu (current_templates.start, CpuHLE))
7243 i.hle_prefix = insn_name (current_templates.start);
7244 else if (is_cpu (current_templates.start, CpuMPX))
7245 i.bnd_prefix = insn_name (current_templates.start);
4e9ac44a 7246 else
d3b01414 7247 i.rep_prefix = insn_name (current_templates.start);
4e9ac44a
L
7248 break;
7249 default:
7250 break;
86fa6981 7251 }
29b0f896
AM
7252 }
7253 /* Skip past PREFIX_SEPARATOR and reset token_start. */
7254 token_start = ++l;
7255 }
7256 else
7257 break;
7258 }
45288df1 7259
edd67638
JB
7260 if (prefix_only)
7261 return token_start;
7262
d3b01414 7263 if (!current_templates.start)
b6169b20 7264 {
07d5e953
JB
7265 /* Deprecated functionality (new code should use pseudo-prefixes instead):
7266 Check if we should swap operand or force 32bit displacement in
f8a5c266 7267 encoding. */
30a55f88 7268 if (mnem_p - 2 == dot_p && dot_p[1] == 's')
97f31cb4
JB
7269 {
7270 if (i.dir_encoding == dir_encoding_default)
7271 i.dir_encoding = dir_encoding_swap;
7272 else
7273 as_warn (_("ignoring `.s' suffix due to earlier `{%s}'"),
7274 i.dir_encoding == dir_encoding_load ? "load" : "store");
7275 }
8d63c93e 7276 else if (mnem_p - 3 == dot_p
a501d77e
L
7277 && dot_p[1] == 'd'
7278 && dot_p[2] == '8')
97f31cb4
JB
7279 {
7280 if (i.disp_encoding == disp_encoding_default)
7281 i.disp_encoding = disp_encoding_8bit;
7282 else if (i.disp_encoding != disp_encoding_8bit)
7283 as_warn (_("ignoring `.d8' suffix due to earlier `{disp<N>}'"));
7284 }
8d63c93e 7285 else if (mnem_p - 4 == dot_p
f8a5c266
L
7286 && dot_p[1] == 'd'
7287 && dot_p[2] == '3'
7288 && dot_p[3] == '2')
97f31cb4
JB
7289 {
7290 if (i.disp_encoding == disp_encoding_default)
7291 i.disp_encoding = disp_encoding_32bit;
7292 else if (i.disp_encoding != disp_encoding_32bit)
7293 as_warn (_("ignoring `.d32' suffix due to earlier `{disp<N>}'"));
7294 }
30a55f88
L
7295 else
7296 goto check_suffix;
7297 mnem_p = dot_p;
7298 *dot_p = '\0';
d3b01414 7299 op_lookup (mnemonic);
b6169b20
L
7300 }
7301
d3b01414 7302 if (!current_templates.start || !pass1)
29b0f896 7303 {
d3b01414 7304 current_templates.start = NULL;
04784e33 7305
dc1e8a47 7306 check_suffix:
1c529385 7307 if (mnem_p > mnemonic)
29b0f896 7308 {
1c529385
LH
7309 /* See if we can get a match by trimming off a suffix. */
7310 switch (mnem_p[-1])
29b0f896 7311 {
1c529385
LH
7312 case WORD_MNEM_SUFFIX:
7313 if (intel_syntax && (intel_float_operand (mnemonic) & 2))
29b0f896
AM
7314 i.suffix = SHORT_MNEM_SUFFIX;
7315 else
1c529385
LH
7316 /* Fall through. */
7317 case BYTE_MNEM_SUFFIX:
7318 case QWORD_MNEM_SUFFIX:
7319 i.suffix = mnem_p[-1];
29b0f896 7320 mnem_p[-1] = '\0';
d3b01414 7321 op_lookup (mnemonic);
1c529385
LH
7322 break;
7323 case SHORT_MNEM_SUFFIX:
7324 case LONG_MNEM_SUFFIX:
7325 if (!intel_syntax)
7326 {
7327 i.suffix = mnem_p[-1];
7328 mnem_p[-1] = '\0';
d3b01414 7329 op_lookup (mnemonic);
1c529385
LH
7330 }
7331 break;
7332
7333 /* Intel Syntax. */
7334 case 'd':
7335 if (intel_syntax)
7336 {
7337 if (intel_float_operand (mnemonic) == 1)
7338 i.suffix = SHORT_MNEM_SUFFIX;
7339 else
7340 i.suffix = LONG_MNEM_SUFFIX;
7341 mnem_p[-1] = '\0';
d3b01414 7342 op_lookup (mnemonic);
1c529385 7343 }
04784e33
JB
7344 /* For compatibility reasons accept MOVSD and CMPSD without
7345 operands even in AT&T mode. */
7346 else if (*l == END_OF_INSN
7347 || (is_space_char (*l) && l[1] == END_OF_INSN))
7348 {
7349 mnem_p[-1] = '\0';
d3b01414
JB
7350 op_lookup (mnemonic);
7351 if (current_templates.start != NULL
04784e33 7352 /* MOVS or CMPS */
d3b01414
JB
7353 && (current_templates.start->base_opcode | 2) == 0xa6
7354 && current_templates.start->opcode_space
04784e33
JB
7355 == SPACE_BASE
7356 && mnem_p[-2] == 's')
7357 {
7358 as_warn (_("found `%sd'; assuming `%sl' was meant"),
7359 mnemonic, mnemonic);
7360 i.suffix = LONG_MNEM_SUFFIX;
7361 }
7362 else
7363 {
d3b01414 7364 current_templates.start = NULL;
04784e33
JB
7365 mnem_p[-1] = 'd';
7366 }
7367 }
1c529385 7368 break;
29b0f896 7369 }
29b0f896 7370 }
1c529385 7371
d3b01414 7372 if (!current_templates.start)
29b0f896 7373 {
04784e33
JB
7374 if (pass1)
7375 as_bad (_("no such instruction: `%s'"), token_start);
29b0f896
AM
7376 return NULL;
7377 }
7378 }
252b5132 7379
d3b01414
JB
7380 if (current_templates.start->opcode_modifier.jump == JUMP
7381 || current_templates.start->opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
7382 {
7383 /* Check for a branch hint. We allow ",pt" and ",pn" for
7384 predict taken and predict not taken respectively.
7385 I'm not sure that branch hints actually do anything on loop
7386 and jcxz insns (JumpByte) for current Pentium4 chips. They
7387 may work in the future and it doesn't hurt to accept them
7388 now. */
7389 if (l[0] == ',' && l[1] == 'p')
7390 {
7391 if (l[2] == 't')
7392 {
7393 if (!add_prefix (DS_PREFIX_OPCODE))
7394 return NULL;
7395 l += 3;
7396 }
7397 else if (l[2] == 'n')
7398 {
7399 if (!add_prefix (CS_PREFIX_OPCODE))
7400 return NULL;
7401 l += 3;
7402 }
7403 }
7404 }
7405 /* Any other comma loses. */
7406 if (*l == ',')
7407 {
7408 as_bad (_("invalid character %s in mnemonic"),
7409 output_invalid (*l));
7410 return NULL;
7411 }
252b5132 7412
29b0f896 7413 /* Check if instruction is supported on specified architecture. */
5c6af06e 7414 supported = 0;
d3b01414 7415 for (t = current_templates.start; t < current_templates.end; ++t)
5c6af06e 7416 {
c0f3af97 7417 supported |= cpu_flags_match (t);
9db83a32
JB
7418
7419 if (i.suffix == QWORD_MNEM_SUFFIX && !q_suffix_allowed (t))
7420 supported &= ~CPU_FLAGS_64BIT_MATCH;
7421
c0f3af97 7422 if (supported == CPU_FLAGS_PERFECT_MATCH)
d59a54c2 7423 return l;
29b0f896 7424 }
3629bb00 7425
9db83a32
JB
7426 if (pass1)
7427 {
7428 if (supported & CPU_FLAGS_64BIT_MATCH)
7429 i.error = unsupported_on_arch;
7430 else
7431 i.error = unsupported_64bit;
7432 }
252b5132 7433
548d0ee6 7434 return NULL;
29b0f896 7435}
252b5132 7436
29b0f896 7437static char *
e3bb37b5 7438parse_operands (char *l, const char *mnemonic)
29b0f896
AM
7439{
7440 char *token_start;
3138f287 7441
29b0f896
AM
7442 /* 1 if operand is pending after ','. */
7443 unsigned int expecting_operand = 0;
252b5132 7444
29b0f896
AM
7445 while (*l != END_OF_INSN)
7446 {
e68c3d59
JB
7447 /* Non-zero if operand parens not balanced. */
7448 unsigned int paren_not_balanced = 0;
7449 /* True if inside double quotes. */
7450 bool in_quotes = false;
7451
29b0f896
AM
7452 /* Skip optional white space before operand. */
7453 if (is_space_char (*l))
7454 ++l;
d02603dc 7455 if (!is_operand_char (*l) && *l != END_OF_INSN && *l != '"')
29b0f896
AM
7456 {
7457 as_bad (_("invalid character %s before operand %d"),
7458 output_invalid (*l),
7459 i.operands + 1);
7460 return NULL;
7461 }
d02603dc 7462 token_start = l; /* After white space. */
e68c3d59 7463 while (in_quotes || paren_not_balanced || *l != ',')
29b0f896
AM
7464 {
7465 if (*l == END_OF_INSN)
7466 {
e68c3d59
JB
7467 if (in_quotes)
7468 {
7469 as_bad (_("unbalanced double quotes in operand %d."),
7470 i.operands + 1);
7471 return NULL;
7472 }
29b0f896
AM
7473 if (paren_not_balanced)
7474 {
98ff9f1c
JB
7475 know (!intel_syntax);
7476 as_bad (_("unbalanced parenthesis in operand %d."),
7477 i.operands + 1);
29b0f896
AM
7478 return NULL;
7479 }
7480 else
7481 break; /* we are done */
7482 }
e68c3d59
JB
7483 else if (*l == '\\' && l[1] == '"')
7484 ++l;
7485 else if (*l == '"')
7486 in_quotes = !in_quotes;
7487 else if (!in_quotes && !is_operand_char (*l) && !is_space_char (*l))
29b0f896
AM
7488 {
7489 as_bad (_("invalid character %s in operand %d"),
7490 output_invalid (*l),
7491 i.operands + 1);
7492 return NULL;
7493 }
e68c3d59 7494 if (!intel_syntax && !in_quotes)
29b0f896
AM
7495 {
7496 if (*l == '(')
7497 ++paren_not_balanced;
7498 if (*l == ')')
7499 --paren_not_balanced;
7500 }
29b0f896
AM
7501 l++;
7502 }
7503 if (l != token_start)
7504 { /* Yes, we've read in another operand. */
7505 unsigned int operand_ok;
7506 this_operand = i.operands++;
7507 if (i.operands > MAX_OPERANDS)
7508 {
7509 as_bad (_("spurious operands; (%d operands/instruction max)"),
7510 MAX_OPERANDS);
7511 return NULL;
7512 }
9d46ce34 7513 i.types[this_operand].bitfield.unspecified = 1;
29b0f896
AM
7514 /* Now parse operand adding info to 'i' as we go along. */
7515 END_STRING_AND_SAVE (l);
7516
1286ab78
L
7517 if (i.mem_operands > 1)
7518 {
7519 as_bad (_("too many memory references for `%s'"),
7520 mnemonic);
7521 return 0;
7522 }
7523
29b0f896
AM
7524 if (intel_syntax)
7525 operand_ok =
7526 i386_intel_operand (token_start,
7527 intel_float_operand (mnemonic));
7528 else
a7619375 7529 operand_ok = i386_att_operand (token_start);
29b0f896
AM
7530
7531 RESTORE_END_STRING (l);
7532 if (!operand_ok)
7533 return NULL;
7534 }
7535 else
7536 {
7537 if (expecting_operand)
7538 {
7539 expecting_operand_after_comma:
7540 as_bad (_("expecting operand after ','; got nothing"));
7541 return NULL;
7542 }
7543 if (*l == ',')
7544 {
7545 as_bad (_("expecting operand before ','; got nothing"));
7546 return NULL;
7547 }
7548 }
7f3f1ea2 7549
29b0f896
AM
7550 /* Now *l must be either ',' or END_OF_INSN. */
7551 if (*l == ',')
7552 {
7553 if (*++l == END_OF_INSN)
7554 {
7555 /* Just skip it, if it's \n complain. */
7556 goto expecting_operand_after_comma;
7557 }
7558 expecting_operand = 1;
7559 }
7560 }
7561 return l;
7562}
7f3f1ea2 7563
050dfa73 7564static void
783c187b 7565swap_2_operands (unsigned int xchg1, unsigned int xchg2)
050dfa73
MM
7566{
7567 union i386_op temp_op;
40fb9820 7568 i386_operand_type temp_type;
c48dadc9 7569 unsigned int temp_flags;
050dfa73 7570 enum bfd_reloc_code_real temp_reloc;
4eed87de 7571
050dfa73
MM
7572 temp_type = i.types[xchg2];
7573 i.types[xchg2] = i.types[xchg1];
7574 i.types[xchg1] = temp_type;
c48dadc9
JB
7575
7576 temp_flags = i.flags[xchg2];
7577 i.flags[xchg2] = i.flags[xchg1];
7578 i.flags[xchg1] = temp_flags;
7579
050dfa73
MM
7580 temp_op = i.op[xchg2];
7581 i.op[xchg2] = i.op[xchg1];
7582 i.op[xchg1] = temp_op;
c48dadc9 7583
050dfa73
MM
7584 temp_reloc = i.reloc[xchg2];
7585 i.reloc[xchg2] = i.reloc[xchg1];
7586 i.reloc[xchg1] = temp_reloc;
43234a1e 7587
c032bc4f
JB
7588 temp_flags = i.imm_bits[xchg2];
7589 i.imm_bits[xchg2] = i.imm_bits[xchg1];
7590 i.imm_bits[xchg1] = temp_flags;
7591
6225c532 7592 if (i.mask.reg)
43234a1e 7593 {
6225c532
JB
7594 if (i.mask.operand == xchg1)
7595 i.mask.operand = xchg2;
7596 else if (i.mask.operand == xchg2)
7597 i.mask.operand = xchg1;
43234a1e 7598 }
a5748e0d 7599 if (i.broadcast.type || i.broadcast.bytes)
43234a1e 7600 {
5273a3cd
JB
7601 if (i.broadcast.operand == xchg1)
7602 i.broadcast.operand = xchg2;
7603 else if (i.broadcast.operand == xchg2)
7604 i.broadcast.operand = xchg1;
43234a1e 7605 }
050dfa73
MM
7606}
7607
29b0f896 7608static void
e3bb37b5 7609swap_operands (void)
29b0f896 7610{
b7c61d9a 7611 switch (i.operands)
050dfa73 7612 {
c0f3af97 7613 case 5:
b7c61d9a 7614 case 4:
4d456e3d 7615 swap_2_operands (1, i.operands - 2);
1a0670f3 7616 /* Fall through. */
b7c61d9a
L
7617 case 3:
7618 case 2:
4d456e3d 7619 swap_2_operands (0, i.operands - 1);
b7c61d9a
L
7620 break;
7621 default:
7622 abort ();
29b0f896 7623 }
29b0f896
AM
7624
7625 if (i.mem_operands == 2)
7626 {
5e042380 7627 const reg_entry *temp_seg;
29b0f896
AM
7628 temp_seg = i.seg[0];
7629 i.seg[0] = i.seg[1];
7630 i.seg[1] = temp_seg;
7631 }
7632}
252b5132 7633
29b0f896
AM
7634/* Try to ensure constant immediates are represented in the smallest
7635 opcode possible. */
7636static void
e3bb37b5 7637optimize_imm (void)
29b0f896
AM
7638{
7639 char guess_suffix = 0;
7640 int op;
252b5132 7641
29b0f896
AM
7642 if (i.suffix)
7643 guess_suffix = i.suffix;
7644 else if (i.reg_operands)
7645 {
7646 /* Figure out a suffix from the last register operand specified.
75e5731b
JB
7647 We can't do this properly yet, i.e. excluding special register
7648 instances, but the following works for instructions with
7649 immediates. In any case, we can't set i.suffix yet. */
29b0f896 7650 for (op = i.operands; --op >= 0;)
bab6aec1
JB
7651 if (i.types[op].bitfield.class != Reg)
7652 continue;
7653 else if (i.types[op].bitfield.byte)
7ab9ffdd 7654 {
40fb9820
L
7655 guess_suffix = BYTE_MNEM_SUFFIX;
7656 break;
7657 }
bab6aec1 7658 else if (i.types[op].bitfield.word)
252b5132 7659 {
40fb9820
L
7660 guess_suffix = WORD_MNEM_SUFFIX;
7661 break;
7662 }
bab6aec1 7663 else if (i.types[op].bitfield.dword)
40fb9820
L
7664 {
7665 guess_suffix = LONG_MNEM_SUFFIX;
7666 break;
7667 }
bab6aec1 7668 else if (i.types[op].bitfield.qword)
40fb9820
L
7669 {
7670 guess_suffix = QWORD_MNEM_SUFFIX;
29b0f896 7671 break;
252b5132 7672 }
29b0f896 7673 }
f79d55e1
JB
7674 else if ((flag_code == CODE_16BIT)
7675 ^ (i.prefix[DATA_PREFIX] != 0 && !(i.prefix[REX_PREFIX] & REX_W)))
29b0f896 7676 guess_suffix = WORD_MNEM_SUFFIX;
fb1c1058
JB
7677 else if (flag_code != CODE_64BIT
7678 || (!(i.prefix[REX_PREFIX] & REX_W)
7679 /* A more generic (but also more involved) way of dealing
7680 with the special case(s) would be to go look for
7681 DefaultSize attributes on any of the templates. */
d3b01414 7682 && current_templates.start->mnem_off != MN_push))
5cc00775 7683 guess_suffix = LONG_MNEM_SUFFIX;
29b0f896
AM
7684
7685 for (op = i.operands; --op >= 0;)
40fb9820 7686 if (operand_type_check (i.types[op], imm))
29b0f896
AM
7687 {
7688 switch (i.op[op].imms->X_op)
252b5132 7689 {
29b0f896
AM
7690 case O_constant:
7691 /* If a suffix is given, this operand may be shortened. */
7692 switch (guess_suffix)
252b5132 7693 {
29b0f896 7694 case LONG_MNEM_SUFFIX:
40fb9820
L
7695 i.types[op].bitfield.imm32 = 1;
7696 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
7697 break;
7698 case WORD_MNEM_SUFFIX:
40fb9820
L
7699 i.types[op].bitfield.imm16 = 1;
7700 i.types[op].bitfield.imm32 = 1;
7701 i.types[op].bitfield.imm32s = 1;
7702 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
7703 break;
7704 case BYTE_MNEM_SUFFIX:
40fb9820
L
7705 i.types[op].bitfield.imm8 = 1;
7706 i.types[op].bitfield.imm8s = 1;
7707 i.types[op].bitfield.imm16 = 1;
7708 i.types[op].bitfield.imm32 = 1;
7709 i.types[op].bitfield.imm32s = 1;
7710 i.types[op].bitfield.imm64 = 1;
29b0f896 7711 break;
252b5132 7712 }
252b5132 7713
29b0f896
AM
7714 /* If this operand is at most 16 bits, convert it
7715 to a signed 16 bit number before trying to see
7716 whether it will fit in an even smaller size.
7717 This allows a 16-bit operand such as $0xffe0 to
7718 be recognised as within Imm8S range. */
40fb9820 7719 if ((i.types[op].bitfield.imm16)
7e96fb68 7720 && fits_in_unsigned_word (i.op[op].imms->X_add_number))
252b5132 7721 {
87ed972d
JB
7722 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
7723 ^ 0x8000) - 0x8000);
29b0f896 7724 }
a28def75
L
7725#ifdef BFD64
7726 /* Store 32-bit immediate in 64-bit for 64-bit BFD. */
40fb9820 7727 if ((i.types[op].bitfield.imm32)
7e96fb68 7728 && fits_in_unsigned_long (i.op[op].imms->X_add_number))
29b0f896
AM
7729 {
7730 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
7731 ^ ((offsetT) 1 << 31))
7732 - ((offsetT) 1 << 31));
7733 }
a28def75 7734#endif
40fb9820 7735 i.types[op]
c6fb90c8
L
7736 = operand_type_or (i.types[op],
7737 smallest_imm_type (i.op[op].imms->X_add_number));
252b5132 7738
29b0f896
AM
7739 /* We must avoid matching of Imm32 templates when 64bit
7740 only immediate is available. */
7741 if (guess_suffix == QWORD_MNEM_SUFFIX)
40fb9820 7742 i.types[op].bitfield.imm32 = 0;
29b0f896 7743 break;
252b5132 7744
29b0f896
AM
7745 case O_absent:
7746 case O_register:
7747 abort ();
7748
7749 /* Symbols and expressions. */
7750 default:
9cd96992
JB
7751 /* Convert symbolic operand to proper sizes for matching, but don't
7752 prevent matching a set of insns that only supports sizes other
7753 than those matching the insn suffix. */
7754 {
40fb9820 7755 i386_operand_type mask, allowed;
d3b01414 7756 const insn_template *t = current_templates.start;
9cd96992 7757
0dfbf9d7 7758 operand_type_set (&mask, 0);
9cd96992
JB
7759 switch (guess_suffix)
7760 {
7761 case QWORD_MNEM_SUFFIX:
40fb9820
L
7762 mask.bitfield.imm64 = 1;
7763 mask.bitfield.imm32s = 1;
9cd96992
JB
7764 break;
7765 case LONG_MNEM_SUFFIX:
40fb9820 7766 mask.bitfield.imm32 = 1;
9cd96992
JB
7767 break;
7768 case WORD_MNEM_SUFFIX:
40fb9820 7769 mask.bitfield.imm16 = 1;
9cd96992
JB
7770 break;
7771 case BYTE_MNEM_SUFFIX:
40fb9820 7772 mask.bitfield.imm8 = 1;
9cd96992
JB
7773 break;
7774 default:
9cd96992
JB
7775 break;
7776 }
8f0212ac
JB
7777
7778 allowed = operand_type_and (t->operand_types[op], mask);
d3b01414 7779 while (++t < current_templates.end)
8f0212ac
JB
7780 {
7781 allowed = operand_type_or (allowed, t->operand_types[op]);
7782 allowed = operand_type_and (allowed, mask);
7783 }
7784
0dfbf9d7 7785 if (!operand_type_all_zero (&allowed))
c6fb90c8 7786 i.types[op] = operand_type_and (i.types[op], mask);
9cd96992 7787 }
29b0f896 7788 break;
252b5132 7789 }
29b0f896
AM
7790 }
7791}
47926f60 7792
29b0f896 7793/* Try to use the smallest displacement type too. */
0de704b9
JB
7794static bool
7795optimize_disp (const insn_template *t)
29b0f896 7796{
0de704b9 7797 unsigned int op;
3e73aa7c 7798
0de704b9
JB
7799 if (!want_disp32 (t)
7800 && (!t->opcode_modifier.jump
7801 || i.jumpabsolute || i.types[0].bitfield.baseindex))
7802 {
7803 for (op = 0; op < i.operands; ++op)
7804 {
7805 const expressionS *exp = i.op[op].disps;
7806
7807 if (!operand_type_check (i.types[op], disp))
7808 continue;
7809
7810 if (exp->X_op != O_constant)
7811 continue;
7812
7813 /* Since displacement is signed extended to 64bit, don't allow
7814 disp32 if it is out of range. */
7815 if (fits_in_signed_long (exp->X_add_number))
7816 continue;
7817
7818 i.types[op].bitfield.disp32 = 0;
7819 if (i.types[op].bitfield.baseindex)
7820 {
7821 as_bad (_("0x%" PRIx64 " out of range of signed 32bit displacement"),
7822 (uint64_t) exp->X_add_number);
7823 return false;
7824 }
7825 }
7826 }
7827
7828 /* Don't optimize displacement for movabs since it only takes 64bit
7829 displacement. */
7830 if (i.disp_encoding > disp_encoding_8bit
7831 || (flag_code == CODE_64BIT && t->mnem_off == MN_movabs))
7832 return true;
7833
7834 for (op = i.operands; op-- > 0;)
40fb9820 7835 if (operand_type_check (i.types[op], disp))
252b5132 7836 {
b300c311 7837 if (i.op[op].disps->X_op == O_constant)
252b5132 7838 {
91d6fa6a 7839 offsetT op_disp = i.op[op].disps->X_add_number;
29b0f896 7840
91d6fa6a 7841 if (!op_disp && i.types[op].bitfield.baseindex)
b300c311 7842 {
2f2be86b
JB
7843 i.types[op] = operand_type_and_not (i.types[op], anydisp);
7844 i.op[op].disps = NULL;
b300c311 7845 i.disp_operands--;
f185acdd
JB
7846 continue;
7847 }
7848
7849 if (i.types[op].bitfield.disp16
cd613c1f 7850 && fits_in_unsigned_word (op_disp))
f185acdd
JB
7851 {
7852 /* If this operand is at most 16 bits, convert
7853 to a signed 16 bit number and don't use 64bit
7854 displacement. */
7855 op_disp = ((op_disp ^ 0x8000) - 0x8000);
7856 i.types[op].bitfield.disp64 = 0;
b300c311 7857 }
f185acdd 7858
28a167a4 7859#ifdef BFD64
a50187b2 7860 /* Optimize 64-bit displacement to 32-bit for 64-bit BFD. */
a775efc8
JB
7861 if ((flag_code != CODE_64BIT
7862 ? i.types[op].bitfield.disp32
0de704b9
JB
7863 : want_disp32 (t)
7864 && (!t->opcode_modifier.jump
a775efc8 7865 || i.jumpabsolute || i.types[op].bitfield.baseindex))
a50187b2 7866 && fits_in_unsigned_long (op_disp))
b300c311 7867 {
a50187b2
JB
7868 /* If this operand is at most 32 bits, convert
7869 to a signed 32 bit number and don't use 64bit
7870 displacement. */
7871 op_disp = (op_disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
7872 i.types[op].bitfield.disp64 = 0;
7873 i.types[op].bitfield.disp32 = 1;
7874 }
28a167a4 7875
a50187b2
JB
7876 if (flag_code == CODE_64BIT && fits_in_signed_long (op_disp))
7877 {
7878 i.types[op].bitfield.disp64 = 0;
a775efc8 7879 i.types[op].bitfield.disp32 = 1;
b300c311 7880 }
28a167a4 7881#endif
40fb9820 7882 if ((i.types[op].bitfield.disp32
40fb9820 7883 || i.types[op].bitfield.disp16)
b5014f7a 7884 && fits_in_disp8 (op_disp))
40fb9820 7885 i.types[op].bitfield.disp8 = 1;
77c59789
JB
7886
7887 i.op[op].disps->X_add_number = op_disp;
252b5132 7888 }
67a4f2b7
AO
7889 else if (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
7890 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL)
7891 {
7892 fix_new_exp (frag_now, frag_more (0) - frag_now->fr_literal, 0,
7893 i.op[op].disps, 0, i.reloc[op]);
2f2be86b 7894 i.types[op] = operand_type_and_not (i.types[op], anydisp);
67a4f2b7
AO
7895 }
7896 else
b300c311 7897 /* We only support 64bit displacement on constants. */
40fb9820 7898 i.types[op].bitfield.disp64 = 0;
252b5132 7899 }
0de704b9
JB
7900
7901 return true;
29b0f896
AM
7902}
7903
4a1b91ea
L
7904/* Return 1 if there is a match in broadcast bytes between operand
7905 GIVEN and instruction template T. */
7906
7907static INLINE int
7908match_broadcast_size (const insn_template *t, unsigned int given)
7909{
7910 return ((t->opcode_modifier.broadcast == BYTE_BROADCAST
7911 && i.types[given].bitfield.byte)
7912 || (t->opcode_modifier.broadcast == WORD_BROADCAST
7913 && i.types[given].bitfield.word)
7914 || (t->opcode_modifier.broadcast == DWORD_BROADCAST
7915 && i.types[given].bitfield.dword)
7916 || (t->opcode_modifier.broadcast == QWORD_BROADCAST
7917 && i.types[given].bitfield.qword));
7918}
7919
6c30d220
L
7920/* Check if operands are valid for the instruction. */
7921
7922static int
7923check_VecOperands (const insn_template *t)
7924{
43234a1e 7925 unsigned int op;
e2195274 7926 i386_cpu_flags cpu;
e2195274
JB
7927
7928 /* Templates allowing for ZMMword as well as YMMword and/or XMMword for
7929 any one operand are implicity requiring AVX512VL support if the actual
7930 operand size is YMMword or XMMword. Since this function runs after
a61cb9db
L
7931 template matching, there's no need to check for YMMword/XMMword in
7932 the template. */
734dfd1c 7933 cpu = cpu_flags_and (cpu_flags_from_attr (t->cpu), avx512);
e2195274 7934 if (!cpu_flags_all_zero (&cpu)
734dfd1c 7935 && !is_cpu (t, CpuAVX512VL)
a6f3add0 7936 && !cpu_arch_flags.bitfield.cpuavx512vl
6177c84d 7937 && (!t->opcode_modifier.vex || need_evex_encoding (t)))
e2195274
JB
7938 {
7939 for (op = 0; op < t->operands; ++op)
7940 {
7941 if (t->operand_types[op].bitfield.zmmword
7942 && (i.types[op].bitfield.ymmword
7943 || i.types[op].bitfield.xmmword))
7944 {
54294d73 7945 i.error = operand_size_mismatch;
e2195274
JB
7946 return 1;
7947 }
7948 }
7949 }
43234a1e 7950
22c36940
JB
7951 /* Somewhat similarly, templates specifying both AVX and AVX2 are
7952 requiring AVX2 support if the actual operand size is YMMword. */
a5e91879 7953 if (maybe_cpu (t, CpuAVX) && maybe_cpu (t, CpuAVX2)
22c36940
JB
7954 && !cpu_arch_flags.bitfield.cpuavx2)
7955 {
7956 for (op = 0; op < t->operands; ++op)
7957 {
7958 if (t->operand_types[op].bitfield.xmmword
7959 && i.types[op].bitfield.ymmword)
7960 {
54294d73 7961 i.error = operand_size_mismatch;
22c36940
JB
7962 return 1;
7963 }
7964 }
7965 }
7966
6c30d220 7967 /* Without VSIB byte, we can't have a vector register for index. */
63112cd6 7968 if (!t->opcode_modifier.sib
6c30d220 7969 && i.index_reg
1b54b8d7
JB
7970 && (i.index_reg->reg_type.bitfield.xmmword
7971 || i.index_reg->reg_type.bitfield.ymmword
7972 || i.index_reg->reg_type.bitfield.zmmword))
6c30d220
L
7973 {
7974 i.error = unsupported_vector_index_register;
7975 return 1;
7976 }
7977
ad8ecc81 7978 /* Check if default mask is allowed. */
255571cd 7979 if (t->opcode_modifier.operandconstraint == NO_DEFAULT_MASK
6225c532 7980 && (!i.mask.reg || i.mask.reg->reg_num == 0))
ad8ecc81
MZ
7981 {
7982 i.error = no_default_mask;
7983 return 1;
7984 }
7985
7bab8ab5
JB
7986 /* For VSIB byte, we need a vector register for index, and all vector
7987 registers must be distinct. */
260cd341 7988 if (t->opcode_modifier.sib && t->opcode_modifier.sib != SIBMEM)
7bab8ab5
JB
7989 {
7990 if (!i.index_reg
63112cd6 7991 || !((t->opcode_modifier.sib == VECSIB128
1b54b8d7 7992 && i.index_reg->reg_type.bitfield.xmmword)
63112cd6 7993 || (t->opcode_modifier.sib == VECSIB256
1b54b8d7 7994 && i.index_reg->reg_type.bitfield.ymmword)
63112cd6 7995 || (t->opcode_modifier.sib == VECSIB512
1b54b8d7 7996 && i.index_reg->reg_type.bitfield.zmmword)))
7bab8ab5
JB
7997 {
7998 i.error = invalid_vsib_address;
7999 return 1;
8000 }
8001
6225c532
JB
8002 gas_assert (i.reg_operands == 2 || i.mask.reg);
8003 if (i.reg_operands == 2 && !i.mask.reg)
43234a1e 8004 {
3528c362 8005 gas_assert (i.types[0].bitfield.class == RegSIMD);
1b54b8d7
JB
8006 gas_assert (i.types[0].bitfield.xmmword
8007 || i.types[0].bitfield.ymmword);
3528c362 8008 gas_assert (i.types[2].bitfield.class == RegSIMD);
1b54b8d7
JB
8009 gas_assert (i.types[2].bitfield.xmmword
8010 || i.types[2].bitfield.ymmword);
43234a1e
L
8011 if (operand_check == check_none)
8012 return 0;
8013 if (register_number (i.op[0].regs)
8014 != register_number (i.index_reg)
8015 && register_number (i.op[2].regs)
8016 != register_number (i.index_reg)
8017 && register_number (i.op[0].regs)
8018 != register_number (i.op[2].regs))
8019 return 0;
8020 if (operand_check == check_error)
8021 {
8022 i.error = invalid_vector_register_set;
8023 return 1;
8024 }
8025 as_warn (_("mask, index, and destination registers should be distinct"));
8026 }
6225c532 8027 else if (i.reg_operands == 1 && i.mask.reg)
8444f82a 8028 {
3528c362 8029 if (i.types[1].bitfield.class == RegSIMD
1b54b8d7
JB
8030 && (i.types[1].bitfield.xmmword
8031 || i.types[1].bitfield.ymmword
8032 || i.types[1].bitfield.zmmword)
8444f82a
MZ
8033 && (register_number (i.op[1].regs)
8034 == register_number (i.index_reg)))
8035 {
8036 if (operand_check == check_error)
8037 {
8038 i.error = invalid_vector_register_set;
8039 return 1;
8040 }
8041 if (operand_check != check_none)
8042 as_warn (_("index and destination registers should be distinct"));
8043 }
8044 }
43234a1e 8045 }
7bab8ab5 8046
fc141319
L
8047 /* For AMX instructions with 3 TMM register operands, all operands
8048 must be distinct. */
8049 if (i.reg_operands == 3
8050 && t->operand_types[0].bitfield.tmmword
8051 && (i.op[0].regs == i.op[1].regs
8052 || i.op[0].regs == i.op[2].regs
8053 || i.op[1].regs == i.op[2].regs))
8054 {
8055 i.error = invalid_tmm_register_set;
8056 return 1;
260cd341
LC
8057 }
8058
0cc78721
CL
8059 /* For some special instructions require that destination must be distinct
8060 from source registers. */
255571cd 8061 if (t->opcode_modifier.operandconstraint == DISTINCT_DEST)
0cc78721
CL
8062 {
8063 unsigned int dest_reg = i.operands - 1;
8064
8065 know (i.operands >= 3);
8066
8067 /* #UD if dest_reg == src1_reg or dest_reg == src2_reg. */
8068 if (i.op[dest_reg - 1].regs == i.op[dest_reg].regs
8069 || (i.reg_operands > 2
8070 && i.op[dest_reg - 2].regs == i.op[dest_reg].regs))
8071 {
8072 i.error = invalid_dest_and_src_register_set;
8073 return 1;
8074 }
8075 }
8076
43234a1e
L
8077 /* Check if broadcast is supported by the instruction and is applied
8078 to the memory operand. */
a5748e0d 8079 if (i.broadcast.type || i.broadcast.bytes)
43234a1e 8080 {
8e6e0792 8081 i386_operand_type type, overlap;
43234a1e
L
8082
8083 /* Check if specified broadcast is supported in this instruction,
4a1b91ea 8084 and its broadcast bytes match the memory operand. */
5273a3cd 8085 op = i.broadcast.operand;
8e6e0792 8086 if (!t->opcode_modifier.broadcast
c48dadc9 8087 || !(i.flags[op] & Operand_Mem)
c39e5b26 8088 || (!i.types[op].bitfield.unspecified
4a1b91ea 8089 && !match_broadcast_size (t, op)))
43234a1e
L
8090 {
8091 bad_broadcast:
8092 i.error = unsupported_broadcast;
8093 return 1;
8094 }
8e6e0792
JB
8095
8096 operand_type_set (&type, 0);
a5748e0d 8097 switch (get_broadcast_bytes (t, false))
8e6e0792 8098 {
4a1b91ea
L
8099 case 2:
8100 type.bitfield.word = 1;
8101 break;
8102 case 4:
8103 type.bitfield.dword = 1;
8104 break;
8e6e0792
JB
8105 case 8:
8106 type.bitfield.qword = 1;
8107 break;
8108 case 16:
8109 type.bitfield.xmmword = 1;
8110 break;
8111 case 32:
4fc85f37
JB
8112 if (vector_size < VSZ256)
8113 goto bad_broadcast;
8e6e0792
JB
8114 type.bitfield.ymmword = 1;
8115 break;
8116 case 64:
4fc85f37
JB
8117 if (vector_size < VSZ512)
8118 goto bad_broadcast;
8e6e0792
JB
8119 type.bitfield.zmmword = 1;
8120 break;
8121 default:
8122 goto bad_broadcast;
8123 }
8124
8125 overlap = operand_type_and (type, t->operand_types[op]);
bc49bfd8
JB
8126 if (t->operand_types[op].bitfield.class == RegSIMD
8127 && t->operand_types[op].bitfield.byte
8128 + t->operand_types[op].bitfield.word
8129 + t->operand_types[op].bitfield.dword
8130 + t->operand_types[op].bitfield.qword > 1)
8131 {
8132 overlap.bitfield.xmmword = 0;
8133 overlap.bitfield.ymmword = 0;
8134 overlap.bitfield.zmmword = 0;
8135 }
8e6e0792
JB
8136 if (operand_type_all_zero (&overlap))
8137 goto bad_broadcast;
8138
9c19e9ec 8139 if (t->opcode_modifier.checkoperandsize)
8e6e0792
JB
8140 {
8141 unsigned int j;
8142
e2195274 8143 type.bitfield.baseindex = 1;
8e6e0792
JB
8144 for (j = 0; j < i.operands; ++j)
8145 {
8146 if (j != op
8147 && !operand_type_register_match(i.types[j],
8148 t->operand_types[j],
8149 type,
8150 t->operand_types[op]))
8151 goto bad_broadcast;
8152 }
8153 }
43234a1e
L
8154 }
8155 /* If broadcast is supported in this instruction, we need to check if
8156 operand of one-element size isn't specified without broadcast. */
8157 else if (t->opcode_modifier.broadcast && i.mem_operands)
8158 {
8159 /* Find memory operand. */
8160 for (op = 0; op < i.operands; op++)
8dc0818e 8161 if (i.flags[op] & Operand_Mem)
43234a1e
L
8162 break;
8163 gas_assert (op < i.operands);
8164 /* Check size of the memory operand. */
4a1b91ea 8165 if (match_broadcast_size (t, op))
43234a1e
L
8166 {
8167 i.error = broadcast_needed;
8168 return 1;
8169 }
8170 }
c39e5b26
JB
8171 else
8172 op = MAX_OPERANDS - 1; /* Avoid uninitialized variable warning. */
43234a1e
L
8173
8174 /* Check if requested masking is supported. */
6225c532 8175 if (i.mask.reg)
43234a1e 8176 {
b1c79256 8177 if (!t->opcode_modifier.masking)
ae2387fe 8178 {
b1c79256
JB
8179 i.error = unsupported_masking;
8180 return 1;
8181 }
8182
8183 /* Common rules for masking:
8184 - mask register destinations permit only zeroing-masking, without
8185 that actually being expressed by a {z} operand suffix or EVEX.z,
8186 - memory destinations allow only merging-masking,
8187 - scatter/gather insns (i.e. ones using vSIB) only allow merging-
8188 masking. */
8189 if (i.mask.zeroing
8190 && (t->operand_types[t->operands - 1].bitfield.class == RegMask
8191 || (i.flags[t->operands - 1] & Operand_Mem)
8192 || t->opcode_modifier.sib))
8193 {
8194 i.error = unsupported_masking;
8195 return 1;
ae2387fe 8196 }
43234a1e
L
8197 }
8198
8199 /* Check if masking is applied to dest operand. */
6225c532 8200 if (i.mask.reg && (i.mask.operand != i.operands - 1))
43234a1e
L
8201 {
8202 i.error = mask_not_on_destination;
8203 return 1;
8204 }
8205
43234a1e 8206 /* Check RC/SAE. */
ca5312a2 8207 if (i.rounding.type != rc_none)
43234a1e 8208 {
a80195f1 8209 if (!t->opcode_modifier.sae
cf665fee
JB
8210 || ((i.rounding.type != saeonly) != t->opcode_modifier.staticrounding)
8211 || i.mem_operands)
43234a1e
L
8212 {
8213 i.error = unsupported_rc_sae;
8214 return 1;
8215 }
cf665fee
JB
8216
8217 /* Non-EVEX.LIG forms need to have a ZMM register as at least one
8218 operand. */
8219 if (t->opcode_modifier.evex != EVEXLIG)
7bab8ab5 8220 {
cf665fee
JB
8221 for (op = 0; op < t->operands; ++op)
8222 if (i.types[op].bitfield.zmmword)
8223 break;
8224 if (op >= t->operands)
8225 {
8226 i.error = operand_size_mismatch;
8227 return 1;
8228 }
7bab8ab5 8229 }
6c30d220
L
8230 }
8231
da4977e0 8232 /* Check the special Imm4 cases; must be the first operand. */
eea43579 8233 if ((is_cpu (t, CpuXOP) && t->operands == 5)
ec6b11e7
JB
8234 || (t->opcode_space == SPACE_0F3A
8235 && (t->base_opcode | 3) == 0x0b
8236 && is_cpu (t, CpuAPX_F)))
da4977e0
JB
8237 {
8238 if (i.op[0].imms->X_op != O_constant
8239 || !fits_in_imm4 (i.op[0].imms->X_add_number))
8240 {
8241 i.error = bad_imm4;
8242 return 1;
8243 }
8244
8245 /* Turn off Imm<N> so that update_imm won't complain. */
eea43579
JB
8246 if (t->operands == 5)
8247 operand_type_set (&i.types[0], 0);
da4977e0
JB
8248 }
8249
43234a1e 8250 /* Check vector Disp8 operand. */
b5014f7a 8251 if (t->opcode_modifier.disp8memshift
a6f3add0 8252 && (!t->opcode_modifier.vex
6177c84d 8253 || need_evex_encoding (t))
1a42a9fe 8254 && i.disp_encoding <= disp_encoding_8bit)
43234a1e 8255 {
9b345ce8 8256 if (i.broadcast.type || i.broadcast.bytes)
4a1b91ea 8257 i.memshift = t->opcode_modifier.broadcast - 1;
7091c612 8258 else if (t->opcode_modifier.disp8memshift != DISP8_SHIFT_VL)
43234a1e 8259 i.memshift = t->opcode_modifier.disp8memshift;
7091c612
JB
8260 else
8261 {
125ff819 8262 const i386_operand_type *type = NULL, *fallback = NULL;
7091c612
JB
8263
8264 i.memshift = 0;
8265 for (op = 0; op < i.operands; op++)
8dc0818e 8266 if (i.flags[op] & Operand_Mem)
7091c612 8267 {
4174bfff
JB
8268 if (t->opcode_modifier.evex == EVEXLIG)
8269 i.memshift = 2 + (i.suffix == QWORD_MNEM_SUFFIX);
8270 else if (t->operand_types[op].bitfield.xmmword
8271 + t->operand_types[op].bitfield.ymmword
8272 + t->operand_types[op].bitfield.zmmword <= 1)
7091c612
JB
8273 type = &t->operand_types[op];
8274 else if (!i.types[op].bitfield.unspecified)
8275 type = &i.types[op];
125ff819
JB
8276 else /* Ambiguities get resolved elsewhere. */
8277 fallback = &t->operand_types[op];
7091c612 8278 }
3528c362 8279 else if (i.types[op].bitfield.class == RegSIMD
4174bfff 8280 && t->opcode_modifier.evex != EVEXLIG)
7091c612
JB
8281 {
8282 if (i.types[op].bitfield.zmmword)
8283 i.memshift = 6;
8284 else if (i.types[op].bitfield.ymmword && i.memshift < 5)
8285 i.memshift = 5;
8286 else if (i.types[op].bitfield.xmmword && i.memshift < 4)
8287 i.memshift = 4;
8288 }
8289
125ff819
JB
8290 if (!type && !i.memshift)
8291 type = fallback;
7091c612
JB
8292 if (type)
8293 {
8294 if (type->bitfield.zmmword)
8295 i.memshift = 6;
8296 else if (type->bitfield.ymmword)
8297 i.memshift = 5;
8298 else if (type->bitfield.xmmword)
8299 i.memshift = 4;
8300 }
8301
8302 /* For the check in fits_in_disp8(). */
8303 if (i.memshift == 0)
8304 i.memshift = -1;
8305 }
43234a1e
L
8306
8307 for (op = 0; op < i.operands; op++)
8308 if (operand_type_check (i.types[op], disp)
8309 && i.op[op].disps->X_op == O_constant)
8310 {
b5014f7a 8311 if (fits_in_disp8 (i.op[op].disps->X_add_number))
43234a1e 8312 {
b5014f7a
JB
8313 i.types[op].bitfield.disp8 = 1;
8314 return 0;
43234a1e 8315 }
b5014f7a 8316 i.types[op].bitfield.disp8 = 0;
43234a1e
L
8317 }
8318 }
b5014f7a
JB
8319
8320 i.memshift = 0;
43234a1e 8321
6c30d220
L
8322 return 0;
8323}
8324
da4977e0 8325/* Check if encoding requirements are met by the instruction. */
a683cc34
SP
8326
8327static int
da4977e0 8328VEX_check_encoding (const insn_template *t)
a683cc34 8329{
e346d50a 8330 if (i.encoding == encoding_error)
da4977e0
JB
8331 {
8332 i.error = unsupported;
8333 return 1;
8334 }
8335
4fc85f37
JB
8336 /* Vector size restrictions. */
8337 if ((vector_size < VSZ512
fa88a361 8338 && t->opcode_modifier.evex == EVEX512)
4fc85f37
JB
8339 || (vector_size < VSZ256
8340 && (t->opcode_modifier.evex == EVEX256
fa88a361 8341 || t->opcode_modifier.vex == VEX256)))
4fc85f37 8342 {
54294d73 8343 i.error = unsupported_vector_size;
4fc85f37
JB
8344 return 1;
8345 }
8346
eb3f3841 8347 switch (i.encoding)
43234a1e 8348 {
eb3f3841
JB
8349 case encoding_default:
8350 break;
8351
8352 case encoding_vex:
8353 case encoding_vex3:
8354 /* This instruction must be encoded with VEX prefix. */
8355 if (!t->opcode_modifier.vex)
8356 {
8357 i.error = no_vex_encoding;
8358 return 1;
8359 }
8360 break;
8361
8362 case encoding_evex:
8363 case encoding_evex512:
86fa6981 8364 /* This instruction must be encoded with EVEX prefix. */
706ce984 8365 if (!t->opcode_modifier.evex)
86fa6981 8366 {
54294d73 8367 i.error = no_evex_encoding;
86fa6981
L
8368 return 1;
8369 }
eb3f3841 8370 break;
43234a1e 8371
eb3f3841
JB
8372 case encoding_egpr:
8373 /* This instruction must be encoded with REX2 or EVEX prefix. */
8374 if (t->opcode_modifier.vex && !t->opcode_modifier.evex)
86fa6981 8375 {
eb3f3841 8376 i.error = no_evex_encoding;
86fa6981
L
8377 return 1;
8378 }
eb3f3841
JB
8379 break;
8380
8381 default:
8382 abort ();
86fa6981 8383 }
a683cc34 8384
a683cc34
SP
8385 return 0;
8386}
8387
80d61d8d
CL
8388/* Check if Egprs operands are valid for the instruction. */
8389
8390static bool
8391check_EgprOperands (const insn_template *t)
8392{
8393 if (!t->opcode_modifier.noegpr)
192781a3 8394 return false;
80d61d8d
CL
8395
8396 for (unsigned int op = 0; op < i.operands; op++)
8397 {
8398 if (i.types[op].bitfield.class != Reg)
8399 continue;
8400
8401 if (i.op[op].regs->reg_flags & RegRex2)
8402 {
8403 i.error = register_type_mismatch;
192781a3 8404 return true;
80d61d8d
CL
8405 }
8406 }
8407
8408 if ((i.index_reg && (i.index_reg->reg_flags & RegRex2))
8409 || (i.base_reg && (i.base_reg->reg_flags & RegRex2)))
8410 {
8411 i.error = unsupported_EGPR_for_addressing;
192781a3 8412 return true;
80d61d8d
CL
8413 }
8414
8415 /* Check if pseudo prefix {rex2} is valid. */
8416 if (i.rex2_encoding)
8417 {
8418 i.error = invalid_pseudo_prefix;
192781a3 8419 return true;
80d61d8d
CL
8420 }
8421
192781a3 8422 return false;
80d61d8d
CL
8423}
8424
08a98d4c
MZ
8425/* Check if APX operands are valid for the instruction. */
8426static bool
8427check_APX_operands (const insn_template *t)
8428{
8429 /* Push2* and Pop2* cannot use RSP and Pop2* cannot pop two same registers.
8430 */
8431 switch (t->mnem_off)
8432 {
8433 case MN_pop2:
8434 case MN_pop2p:
8435 if (register_number (i.op[0].regs) == register_number (i.op[1].regs))
8436 {
8437 i.error = invalid_dest_register_set;
8438 return 1;
8439 }
8440 /* fall through */
8441 case MN_push2:
8442 case MN_push2p:
8443 if (register_number (i.op[0].regs) == 4
8444 || register_number (i.op[1].regs) == 4)
8445 {
8446 i.error = unsupported_rsp_register;
8447 return 1;
8448 }
8449 break;
8450 }
8451 return 0;
8452}
8453
6967f19d
HL
8454/* Check if the instruction use the REX registers or REX prefix. */
8455static bool
8456check_Rex_required (void)
8457{
8458 for (unsigned int op = 0; op < i.operands; op++)
8459 {
8460 if (i.types[op].bitfield.class != Reg)
8461 continue;
8462
8463 if (i.op[op].regs->reg_flags & (RegRex | RegRex64))
8464 return true;
8465 }
8466
8467 if ((i.index_reg && (i.index_reg->reg_flags & (RegRex | RegRex64)))
8468 || (i.base_reg && (i.base_reg->reg_flags & (RegRex | RegRex64))))
8469 return true;
8470
8471 /* Check pseudo prefix {rex} are valid. */
8472 return i.rex_encoding;
8473}
8474
8475/* Optimize APX NDD insns to legacy insns. */
8476static unsigned int
8477can_convert_NDD_to_legacy (const insn_template *t)
8478{
8479 unsigned int match_dest_op = ~0;
8480
8481 if (!i.tm.opcode_modifier.nf
8482 && i.reg_operands >= 2)
8483 {
8484 unsigned int dest = i.operands - 1;
8485 unsigned int src1 = i.operands - 2;
8486 unsigned int src2 = (i.operands > 3) ? i.operands - 3 : 0;
8487
8488 if (i.types[src1].bitfield.class == Reg
8489 && i.op[src1].regs == i.op[dest].regs)
8490 match_dest_op = src1;
8491 /* If the first operand is the same as the third operand,
8492 these instructions need to support the ability to commutative
8493 the first two operands and still not change the semantics in order
8494 to be optimized. */
8495 else if (optimize > 1
8496 && t->opcode_modifier.commutative
8497 && i.types[src2].bitfield.class == Reg
8498 && i.op[src2].regs == i.op[dest].regs)
8499 match_dest_op = src2;
8500 }
8501 return match_dest_op;
8502}
8503
7b94647a
JB
8504/* Helper function for the progress() macro in match_template(). */
8505static INLINE enum i386_error progress (enum i386_error new,
8506 enum i386_error last,
8507 unsigned int line, unsigned int *line_p)
8508{
8509 if (line <= *line_p)
8510 return last;
8511 *line_p = line;
8512 return new;
8513}
8514
d3ce72d0 8515static const insn_template *
83b16ac6 8516match_template (char mnem_suffix)
29b0f896
AM
8517{
8518 /* Points to template once we've found it. */
d3ce72d0 8519 const insn_template *t;
40fb9820 8520 i386_operand_type overlap0, overlap1, overlap2, overlap3;
c0f3af97 8521 i386_operand_type overlap4;
29b0f896 8522 unsigned int found_reverse_match;
40fb9820 8523 i386_operand_type operand_types [MAX_OPERANDS];
539e75ad 8524 int addr_prefix_disp;
7b94647a
JB
8525 unsigned int j, size_match, check_register, errline = __LINE__;
8526 enum i386_error specific_error = number_of_operands_mismatch;
8527#define progress(err) progress (err, specific_error, __LINE__, &errline)
29b0f896 8528
c0f3af97
L
8529#if MAX_OPERANDS != 5
8530# error "MAX_OPERANDS must be 5."
f48ff2ae
L
8531#endif
8532
29b0f896 8533 found_reverse_match = 0;
539e75ad 8534 addr_prefix_disp = -1;
40fb9820 8535
d3b01414 8536 for (t = current_templates.start; t < current_templates.end; t++)
29b0f896 8537 {
539e75ad 8538 addr_prefix_disp = -1;
dbbc8b7e 8539 found_reverse_match = 0;
539e75ad 8540
7b94647a 8541 /* Must have right number of operands. */
29b0f896
AM
8542 if (i.operands != t->operands)
8543 continue;
8544
50aecf8c 8545 /* Check processor support. */
7b94647a 8546 specific_error = progress (unsupported);
45a4bb20 8547 if (cpu_flags_match (t) != CPU_FLAGS_PERFECT_MATCH)
50aecf8c
L
8548 continue;
8549
e1d4d893 8550 /* Check AT&T mnemonic. */
7b94647a 8551 specific_error = progress (unsupported_with_intel_mnemonic);
35266cb1
JB
8552 if (!intel_syntax && intel_mnemonic
8553 && t->opcode_modifier.dialect == ATT_MNEMONIC)
1efbbeb4
L
8554 continue;
8555
4b5aaf5f 8556 /* Check AT&T/Intel syntax. */
7b94647a 8557 specific_error = progress (unsupported_syntax);
7d3182d6 8558 if (intel_syntax
35266cb1
JB
8559 ? t->opcode_modifier.dialect >= ATT_SYNTAX
8560 : t->opcode_modifier.dialect == INTEL_SYNTAX)
1efbbeb4
L
8561 continue;
8562
4b5aaf5f
L
8563 /* Check Intel64/AMD64 ISA. */
8564 switch (isa64)
8565 {
8566 default:
8567 /* Default: Don't accept Intel64. */
8568 if (t->opcode_modifier.isa64 == INTEL64)
8569 continue;
8570 break;
8571 case amd64:
8572 /* -mamd64: Don't accept Intel64 and Intel64 only. */
8573 if (t->opcode_modifier.isa64 >= INTEL64)
8574 continue;
8575 break;
8576 case intel64:
8577 /* -mintel64: Don't accept AMD64. */
5990e377 8578 if (t->opcode_modifier.isa64 == AMD64 && flag_code == CODE_64BIT)
4b5aaf5f
L
8579 continue;
8580 break;
8581 }
8582
dc2be329 8583 /* Check the suffix. */
7b94647a 8584 specific_error = progress (invalid_instruction_suffix);
7505bb03
JB
8585 if ((t->opcode_modifier.no_bsuf && mnem_suffix == BYTE_MNEM_SUFFIX)
8586 || (t->opcode_modifier.no_wsuf && mnem_suffix == WORD_MNEM_SUFFIX)
8587 || (t->opcode_modifier.no_lsuf && mnem_suffix == LONG_MNEM_SUFFIX)
8588 || (t->opcode_modifier.no_ssuf && mnem_suffix == SHORT_MNEM_SUFFIX)
8589 || (t->opcode_modifier.no_qsuf && mnem_suffix == QWORD_MNEM_SUFFIX))
83b16ac6 8590 continue;
29b0f896 8591
7b94647a 8592 specific_error = progress (operand_size_mismatch);
3ac21baa
JB
8593 size_match = operand_size_match (t);
8594 if (!size_match)
7d5e4556 8595 continue;
539e75ad 8596
6f2f06be
JB
8597 /* This is intentionally not
8598
0cfa3eb3 8599 if (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE))
6f2f06be
JB
8600
8601 as the case of a missing * on the operand is accepted (perhaps with
8602 a warning, issued further down). */
7b94647a 8603 specific_error = progress (operand_type_mismatch);
0cfa3eb3 8604 if (i.jumpabsolute && t->opcode_modifier.jump != JUMP_ABSOLUTE)
7b94647a 8605 continue;
6f2f06be 8606
a4d3acd2
JB
8607 /* In Intel syntax, normally we can check for memory operand size when
8608 there is no mnemonic suffix. But jmp and call have 2 different
8609 encodings with Dword memory operand size. Skip the "near" one
8610 (permitting a register operand) when "far" was requested. */
8611 if (i.far_branch
8612 && t->opcode_modifier.jump == JUMP_ABSOLUTE
8613 && t->operand_types[0].bitfield.class == Reg)
8614 continue;
8615
5c07affc
L
8616 for (j = 0; j < MAX_OPERANDS; j++)
8617 operand_types[j] = t->operand_types[j];
8618
9db83a32 8619 /* In general, don't allow 32-bit operands on pre-386. */
7b94647a
JB
8620 specific_error = progress (mnem_suffix ? invalid_instruction_suffix
8621 : operand_size_mismatch);
4873e243 8622 j = i.imm_operands + (t->operands > i.imm_operands + 1);
9db83a32
JB
8623 if (i.suffix == LONG_MNEM_SUFFIX
8624 && !cpu_arch_flags.bitfield.cpui386
45aa61fe 8625 && (intel_syntax
3cd7f3e3 8626 ? (t->opcode_modifier.mnemonicsize != IGNORESIZE
76d3f746
JB
8627 && !intel_float_operand (insn_name (t)))
8628 : intel_float_operand (insn_name (t)) != 2)
4873e243
JB
8629 && (t->operands == i.imm_operands
8630 || (operand_types[i.imm_operands].bitfield.class != RegMMX
8631 && operand_types[i.imm_operands].bitfield.class != RegSIMD
8632 && operand_types[i.imm_operands].bitfield.class != RegMask)
8633 || (operand_types[j].bitfield.class != RegMMX
8634 && operand_types[j].bitfield.class != RegSIMD
8635 && operand_types[j].bitfield.class != RegMask))
63112cd6 8636 && !t->opcode_modifier.sib)
192dc9c6
JB
8637 continue;
8638
29b0f896 8639 /* Do not verify operands when there are none. */
e365e234 8640 if (!t->operands)
da4977e0
JB
8641 {
8642 if (VEX_check_encoding (t))
8643 {
7b94647a 8644 specific_error = progress (i.error);
da4977e0
JB
8645 continue;
8646 }
8647
80d61d8d
CL
8648 /* Check if pseudo prefix {rex2} is valid. */
8649 if (t->opcode_modifier.noegpr && i.rex2_encoding)
8650 {
8651 specific_error = progress (invalid_pseudo_prefix);
8652 continue;
8653 }
8654
da4977e0
JB
8655 /* We've found a match; break out of loop. */
8656 break;
8657 }
252b5132 8658
48bcea9f
JB
8659 if (!t->opcode_modifier.jump
8660 || t->opcode_modifier.jump == JUMP_ABSOLUTE)
8661 {
8662 /* There should be only one Disp operand. */
8663 for (j = 0; j < MAX_OPERANDS; j++)
8664 if (operand_type_check (operand_types[j], disp))
539e75ad 8665 break;
48bcea9f
JB
8666 if (j < MAX_OPERANDS)
8667 {
5b7c81bd 8668 bool override = (i.prefix[ADDR_PREFIX] != 0);
48bcea9f
JB
8669
8670 addr_prefix_disp = j;
8671
a775efc8
JB
8672 /* Address size prefix will turn Disp64 operand into Disp32 and
8673 Disp32/Disp16 one into Disp16/Disp32 respectively. */
48bcea9f 8674 switch (flag_code)
40fb9820 8675 {
48bcea9f
JB
8676 case CODE_16BIT:
8677 override = !override;
8678 /* Fall through. */
8679 case CODE_32BIT:
8680 if (operand_types[j].bitfield.disp32
8681 && operand_types[j].bitfield.disp16)
40fb9820 8682 {
48bcea9f
JB
8683 operand_types[j].bitfield.disp16 = override;
8684 operand_types[j].bitfield.disp32 = !override;
40fb9820 8685 }
a775efc8 8686 gas_assert (!operand_types[j].bitfield.disp64);
48bcea9f
JB
8687 break;
8688
8689 case CODE_64BIT:
a775efc8 8690 if (operand_types[j].bitfield.disp64)
40fb9820 8691 {
a775efc8 8692 gas_assert (!operand_types[j].bitfield.disp32);
48bcea9f 8693 operand_types[j].bitfield.disp32 = override;
a775efc8 8694 operand_types[j].bitfield.disp64 = !override;
40fb9820 8695 }
48bcea9f
JB
8696 operand_types[j].bitfield.disp16 = 0;
8697 break;
40fb9820 8698 }
539e75ad 8699 }
48bcea9f 8700 }
539e75ad 8701
56ffb741 8702 /* We check register size if needed. */
9c19e9ec 8703 if (t->opcode_modifier.checkoperandsize)
e2195274
JB
8704 {
8705 check_register = (1 << t->operands) - 1;
a5748e0d 8706 if (i.broadcast.type || i.broadcast.bytes)
5273a3cd 8707 check_register &= ~(1 << i.broadcast.operand);
e2195274
JB
8708 }
8709 else
8710 check_register = 0;
8711
c6fb90c8 8712 overlap0 = operand_type_and (i.types[0], operand_types[0]);
29b0f896
AM
8713 switch (t->operands)
8714 {
8715 case 1:
40fb9820 8716 if (!operand_type_match (overlap0, i.types[0]))
29b0f896 8717 continue;
ae9a0a51
JB
8718
8719 /* Allow the ModR/M encoding to be requested by using the {load} or
8720 {store} pseudo prefix on an applicable insn. */
8721 if (!t->opcode_modifier.modrm
8722 && i.reg_operands == 1
8723 && ((i.dir_encoding == dir_encoding_load
8724 && t->mnem_off != MN_pop)
8725 || (i.dir_encoding == dir_encoding_store
8726 && t->mnem_off != MN_push))
8727 /* Avoid BSWAP. */
8728 && t->mnem_off != MN_bswap)
8729 continue;
29b0f896 8730 break;
ae9a0a51 8731
29b0f896 8732 case 2:
33eaf5de 8733 /* xchg %eax, %eax is a special case. It is an alias for nop
8b38ad71
L
8734 only in 32bit mode and we can use opcode 0x90. In 64bit
8735 mode, we can't use 0x90 for xchg %eax, %eax since it should
8736 zero-extend %eax to %rax. */
ae9a0a51
JB
8737 if (t->base_opcode == 0x90
8738 && t->opcode_space == SPACE_BASE)
8739 {
8740 if (flag_code == CODE_64BIT
8741 && i.types[0].bitfield.instance == Accum
8742 && i.types[0].bitfield.dword
8743 && i.types[1].bitfield.instance == Accum)
8744 continue;
8745
8746 /* Allow the ModR/M encoding to be requested by using the
8747 {load} or {store} pseudo prefix. */
8748 if (i.dir_encoding == dir_encoding_load
8749 || i.dir_encoding == dir_encoding_store)
8750 continue;
8751 }
e3669c7f
JB
8752
8753 if (t->base_opcode == MOV_AX_DISP32
ddb62495 8754 && t->opcode_space == SPACE_BASE
69196391 8755 && t->mnem_off != MN_movabs)
e3669c7f
JB
8756 {
8757 /* Force 0x8b encoding for "mov foo@GOT, %eax". */
8758 if (i.reloc[0] == BFD_RELOC_386_GOT32)
8759 continue;
8760
8761 /* xrelease mov %eax, <disp> is another special case. It must not
8762 match the accumulator-only encoding of mov. */
8763 if (i.hle_prefix)
8764 continue;
ae9a0a51
JB
8765
8766 /* Allow the ModR/M encoding to be requested by using a suitable
8767 {load} or {store} pseudo prefix. */
8768 if (i.dir_encoding == (i.types[0].bitfield.instance == Accum
8769 ? dir_encoding_store
8770 : dir_encoding_load)
8771 && !i.types[0].bitfield.disp64
8772 && !i.types[1].bitfield.disp64)
8773 continue;
8774 }
8775
8776 /* Allow the ModR/M encoding to be requested by using the {load} or
8777 {store} pseudo prefix on an applicable insn. */
8778 if (!t->opcode_modifier.modrm
8779 && i.reg_operands == 1
8780 && i.imm_operands == 1
8781 && (i.dir_encoding == dir_encoding_load
8782 || i.dir_encoding == dir_encoding_store)
8783 && t->opcode_space == SPACE_BASE)
8784 {
8785 if (t->base_opcode == 0xb0 /* mov $imm, %reg */
8786 && i.dir_encoding == dir_encoding_store)
8787 continue;
8788
8789 if ((t->base_opcode | 0x38) == 0x3c /* <alu> $imm, %acc */
8790 && (t->base_opcode != 0x3c /* cmp $imm, %acc */
8791 || i.dir_encoding == dir_encoding_load))
8792 continue;
8793
8794 if (t->base_opcode == 0xa8 /* test $imm, %acc */
8795 && i.dir_encoding == dir_encoding_load)
8796 continue;
e3669c7f 8797 }
f5eb1d70
JB
8798 /* Fall through. */
8799
8800 case 3:
3ac21baa
JB
8801 if (!(size_match & MATCH_STRAIGHT))
8802 goto check_reverse;
64c49ab3
JB
8803 /* Reverse direction of operands if swapping is possible in the first
8804 place (operands need to be symmetric) and
8805 - the load form is requested, and the template is a store form,
8806 - the store form is requested, and the template is a load form,
8807 - the non-default (swapped) form is requested. */
8808 overlap1 = operand_type_and (operand_types[0], operand_types[1]);
3083f376 8809
8810 j = i.operands - 1 - (t->opcode_space == SPACE_EVEXMAP4
8811 && t->opcode_modifier.vexvvvv);
8812
f5eb1d70 8813 if (t->opcode_modifier.d && i.reg_operands == i.operands
64c49ab3
JB
8814 && !operand_type_all_zero (&overlap1))
8815 switch (i.dir_encoding)
8816 {
8817 case dir_encoding_load:
3083f376 8818 if (operand_type_check (operand_types[j], anymem)
dfd69174 8819 || t->opcode_modifier.regmem)
64c49ab3
JB
8820 goto check_reverse;
8821 break;
8822
8823 case dir_encoding_store:
3083f376 8824 if (!operand_type_check (operand_types[j], anymem)
dfd69174 8825 && !t->opcode_modifier.regmem)
64c49ab3
JB
8826 goto check_reverse;
8827 break;
8828
8829 case dir_encoding_swap:
8830 goto check_reverse;
8831
8832 case dir_encoding_default:
8833 break;
8834 }
3083f376 8835
86fa6981 8836 /* If we want store form, we skip the current load. */
64c49ab3
JB
8837 if ((i.dir_encoding == dir_encoding_store
8838 || i.dir_encoding == dir_encoding_swap)
86fa6981
L
8839 && i.mem_operands == 0
8840 && t->opcode_modifier.load)
fa99fab2 8841 continue;
1a0670f3 8842 /* Fall through. */
f48ff2ae 8843 case 4:
c0f3af97 8844 case 5:
c6fb90c8 8845 overlap1 = operand_type_and (i.types[1], operand_types[1]);
40fb9820
L
8846 if (!operand_type_match (overlap0, i.types[0])
8847 || !operand_type_match (overlap1, i.types[1])
e2195274 8848 || ((check_register & 3) == 3
dc821c5f 8849 && !operand_type_register_match (i.types[0],
40fb9820 8850 operand_types[0],
dc821c5f 8851 i.types[1],
40fb9820 8852 operand_types[1])))
29b0f896 8853 {
7b94647a
JB
8854 specific_error = progress (i.error);
8855
29b0f896 8856 /* Check if other direction is valid ... */
38e314eb 8857 if (!t->opcode_modifier.d)
29b0f896
AM
8858 continue;
8859
dc1e8a47 8860 check_reverse:
3ac21baa
JB
8861 if (!(size_match & MATCH_REVERSE))
8862 continue;
29b0f896 8863 /* Try reversing direction of operands. */
734dfd1c 8864 j = is_cpu (t, CpuFMA4)
3083f376 8865 || is_cpu (t, CpuXOP)
8866 || is_cpu (t, CpuAPX_F) ? 1 : i.operands - 1;
8bd915b7
JB
8867 overlap0 = operand_type_and (i.types[0], operand_types[j]);
8868 overlap1 = operand_type_and (i.types[j], operand_types[0]);
c975cec5 8869 overlap2 = operand_type_and (i.types[1], operand_types[1]);
3083f376 8870 gas_assert (t->operands != 3 || !check_register
8871 || is_cpu (t, CpuAPX_F));
40fb9820 8872 if (!operand_type_match (overlap0, i.types[0])
8bd915b7 8873 || !operand_type_match (overlap1, i.types[j])
c975cec5
JB
8874 || (t->operands == 3
8875 && !operand_type_match (overlap2, i.types[1]))
45664ddb 8876 || (check_register
dc821c5f 8877 && !operand_type_register_match (i.types[0],
8bd915b7
JB
8878 operand_types[j],
8879 i.types[j],
45664ddb 8880 operand_types[0])))
29b0f896
AM
8881 {
8882 /* Does not match either direction. */
7b94647a 8883 specific_error = progress (i.error);
29b0f896
AM
8884 continue;
8885 }
ac9226cf 8886 /* found_reverse_match holds which variant of D
29b0f896 8887 we've found. */
38e314eb
JB
8888 if (!t->opcode_modifier.d)
8889 found_reverse_match = 0;
8890 else if (operand_types[0].bitfield.tbyte)
ac9226cf 8891 {
4943d587
JB
8892 if (t->opcode_modifier.operandconstraint != UGH)
8893 found_reverse_match = Opcode_FloatD;
bd782808
JB
8894 else
8895 found_reverse_match = ~0;
ac9226cf 8896 /* FSUB{,R} and FDIV{,R} may need a 2nd bit flipped. */
bd782808 8897 if ((t->extension_opcode & 4)
ac9226cf
JB
8898 && (intel_syntax || intel_mnemonic))
8899 found_reverse_match |= Opcode_FloatR;
8900 }
734dfd1c 8901 else if (is_cpu (t, CpuFMA4) || is_cpu (t, CpuXOP))
8bd915b7
JB
8902 {
8903 found_reverse_match = Opcode_VexW;
8904 goto check_operands_345;
8905 }
3083f376 8906 else if (is_cpu (t, CpuAPX_F) && i.operands == 3)
8907 {
8908 found_reverse_match = Opcode_D;
8909 goto check_operands_345;
8910 }
ddb62495
JB
8911 else if (t->opcode_space != SPACE_BASE
8912 && (t->opcode_space != SPACE_0F
2c735193
JB
8913 /* MOV to/from CR/DR/TR, as an exception, follow
8914 the base opcode space encoding model. */
8915 || (t->base_opcode | 7) != 0x27))
dbbc8b7e 8916 found_reverse_match = (t->base_opcode & 0xee) != 0x6e
2c735193 8917 ? Opcode_ExtD : Opcode_SIMD_IntD;
a33ef3c2 8918 else if (!t->opcode_modifier.commutative)
38e314eb 8919 found_reverse_match = Opcode_D;
a33ef3c2
JB
8920 else
8921 found_reverse_match = ~0;
29b0f896 8922 }
f48ff2ae 8923 else
29b0f896 8924 {
f48ff2ae 8925 /* Found a forward 2 operand match here. */
8bd915b7 8926 check_operands_345:
d1cbb4db
L
8927 switch (t->operands)
8928 {
c0f3af97 8929 case 5:
3d0738af 8930 overlap4 = operand_type_and (i.types[4], operand_types[4]);
c0f3af97 8931 if (!operand_type_match (overlap4, i.types[4])
dc821c5f 8932 || !operand_type_register_match (i.types[3],
c0f3af97 8933 operand_types[3],
c0f3af97
L
8934 i.types[4],
8935 operand_types[4]))
7b94647a
JB
8936 {
8937 specific_error = progress (i.error);
8938 continue;
8939 }
1a0670f3 8940 /* Fall through. */
f48ff2ae 8941 case 4:
3d0738af 8942 overlap3 = operand_type_and (i.types[3], operand_types[3]);
40fb9820 8943 if (!operand_type_match (overlap3, i.types[3])
e2195274
JB
8944 || ((check_register & 0xa) == 0xa
8945 && !operand_type_register_match (i.types[1],
f7768225
JB
8946 operand_types[1],
8947 i.types[3],
e2195274
JB
8948 operand_types[3]))
8949 || ((check_register & 0xc) == 0xc
8950 && !operand_type_register_match (i.types[2],
8951 operand_types[2],
8952 i.types[3],
8953 operand_types[3])))
7b94647a
JB
8954 {
8955 specific_error = progress (i.error);
8956 continue;
8957 }
1a0670f3 8958 /* Fall through. */
f48ff2ae 8959 case 3:
3d0738af 8960 overlap2 = operand_type_and (i.types[2], operand_types[2]);
40fb9820 8961 if (!operand_type_match (overlap2, i.types[2])
e2195274
JB
8962 || ((check_register & 5) == 5
8963 && !operand_type_register_match (i.types[0],
23e42951
JB
8964 operand_types[0],
8965 i.types[2],
e2195274
JB
8966 operand_types[2]))
8967 || ((check_register & 6) == 6
8968 && !operand_type_register_match (i.types[1],
8969 operand_types[1],
8970 i.types[2],
8971 operand_types[2])))
7b94647a
JB
8972 {
8973 specific_error = progress (i.error);
8974 continue;
8975 }
f48ff2ae
L
8976 break;
8977 }
29b0f896 8978 }
f48ff2ae 8979 /* Found either forward/reverse 2, 3 or 4 operand match here:
29b0f896
AM
8980 slip through to break. */
8981 }
c0f3af97 8982
9bb4d860
L
8983 /* Check if VEX/EVEX encoding requirements can be satisfied. */
8984 if (VEX_check_encoding (t))
da4977e0 8985 {
7b94647a 8986 specific_error = progress (i.error);
da4977e0
JB
8987 continue;
8988 }
8989
80d61d8d
CL
8990 /* Check if EGPR operands(r16-r31) are valid. */
8991 if (check_EgprOperands (t))
8992 {
8993 specific_error = progress (i.error);
8994 continue;
8995 }
8996
9bb4d860
L
8997 /* Check if vector operands are valid. */
8998 if (check_VecOperands (t))
5614d22c 8999 {
7b94647a 9000 specific_error = progress (i.error);
5614d22c
JB
9001 continue;
9002 }
a683cc34 9003
08a98d4c
MZ
9004 /* Check if APX operands are valid. */
9005 if (check_APX_operands (t))
9006 {
9007 specific_error = progress (i.error);
9008 continue;
9009 }
9010
58bceb18 9011 /* Check whether to use the shorter VEX encoding for certain insns where
39bb3ade
JB
9012 the EVEX encoding comes first in the table. This requires the respective
9013 AVX-* feature to be explicitly enabled.
9014
9015 Most of the respective insns have just a single EVEX and a single VEX
9016 template. The one that's presently different is generated using the
9017 Vxy / Exy constructs: There are 3 suffix-less EVEX forms, the latter
9018 two of which may fall back to their two corresponding VEX forms. */
9019 j = t->mnem_off != MN_vcvtneps2bf16 ? 1 : 2;
d3b01414 9020 if ((t == current_templates.start || j > 1)
58bceb18
JB
9021 && t->opcode_modifier.disp8memshift
9022 && !t->opcode_modifier.vex
6177c84d 9023 && !need_evex_encoding (t)
d3b01414 9024 && t + j < current_templates.end
39bb3ade 9025 && t[j].opcode_modifier.vex)
58bceb18
JB
9026 {
9027 i386_cpu_flags cpu;
9028 unsigned int memshift = i.memshift;
9029
9030 i.memshift = 0;
39bb3ade
JB
9031 cpu = cpu_flags_and (cpu_flags_from_attr (t[j].cpu),
9032 cpu_arch_isa_flags);
58bceb18
JB
9033 if (!cpu_flags_all_zero (&cpu)
9034 && (!i.types[0].bitfield.disp8
9035 || !operand_type_check (i.types[0], disp)
9036 || i.op[0].disps->X_op != O_constant
9037 || fits_in_disp8 (i.op[0].disps->X_add_number)))
9038 {
9039 specific_error = progress (internal_error);
39bb3ade 9040 t += j - 1;
58bceb18
JB
9041 continue;
9042 }
9043 i.memshift = memshift;
9044 }
9045
6967f19d
HL
9046 /* If we can optimize a NDD insn to legacy insn, like
9047 add %r16, %r8, %r8 -> add %r16, %r8,
9048 add %r8, %r16, %r8 -> add %r16, %r8, then rematch template.
9049 Note that the semantics have not been changed. */
9050 if (optimize
9051 && !i.no_optimize
e346d50a 9052 && i.encoding != encoding_evex
0ebcbb1b
JB
9053 && ((t + 1 < current_templates.end
9054 && !t[1].opcode_modifier.evex
9055 && t[1].opcode_space <= SPACE_0F38
9056 && t->opcode_modifier.vexvvvv == VexVVVV_DST)
9057 || t->mnem_off == MN_movbe)
6967f19d
HL
9058 && (i.types[i.operands - 1].bitfield.dword
9059 || i.types[i.operands - 1].bitfield.qword))
9060 {
9061 unsigned int match_dest_op = can_convert_NDD_to_legacy (t);
9062
9063 if (match_dest_op != (unsigned int) ~0)
9064 {
9065 size_match = true;
9066 /* We ensure that the next template has the same input
9067 operands as the original matching template by the first
9068 opernd (ATT). To avoid someone support new NDD insns and
9069 put it in the wrong position. */
9070 overlap0 = operand_type_and (i.types[0],
9071 t[1].operand_types[0]);
9072 if (t->opcode_modifier.d)
9073 overlap1 = operand_type_and (i.types[0],
9074 t[1].operand_types[1]);
9075 if (!operand_type_match (overlap0, i.types[0])
9076 && (!t->opcode_modifier.d
9077 || !operand_type_match (overlap1, i.types[0])))
9078 size_match = false;
9079
9080 if (size_match
9081 && (t[1].opcode_space <= SPACE_0F
9082 /* Some non-legacy-map0/1 insns can be shorter when
9083 legacy-encoded and when no REX prefix is required. */
9084 || (!check_EgprOperands (t + 1)
9085 && !check_Rex_required ()
9086 && !i.op[i.operands - 1].regs->reg_type.bitfield.qword)))
9087 {
9088 if (i.operands > 2 && match_dest_op == i.operands - 3)
9089 swap_2_operands (match_dest_op, i.operands - 2);
9090
9091 --i.operands;
9092 --i.reg_operands;
9093
0ebcbb1b
JB
9094 if (t->mnem_off == MN_movbe)
9095 {
9096 gas_assert (t[1].mnem_off == MN_bswap);
9097 ++current_templates.end;
9098 }
9099
6967f19d
HL
9100 specific_error = progress (internal_error);
9101 continue;
9102 }
9103
9104 }
9105 }
9106
29b0f896
AM
9107 /* We've found a match; break out of loop. */
9108 break;
9109 }
9110
7b94647a
JB
9111#undef progress
9112
d3b01414 9113 if (t == current_templates.end)
29b0f896
AM
9114 {
9115 /* We found no match. */
04784e33 9116 i.error = specific_error;
fa99fab2 9117 return NULL;
29b0f896 9118 }
252b5132 9119
29b0f896
AM
9120 if (!quiet_warnings)
9121 {
9122 if (!intel_syntax
0cfa3eb3 9123 && (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE)))
76d3f746 9124 as_warn (_("indirect %s without `*'"), insn_name (t));
29b0f896 9125
40fb9820 9126 if (t->opcode_modifier.isprefix
3cd7f3e3 9127 && t->opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
9128 {
9129 /* Warn them that a data or address size prefix doesn't
9130 affect assembly of the next line of code. */
76d3f746 9131 as_warn (_("stand-alone `%s' prefix"), insn_name (t));
29b0f896
AM
9132 }
9133 }
9134
9135 /* Copy the template we found. */
9a182d04 9136 install_template (t);
539e75ad
L
9137
9138 if (addr_prefix_disp != -1)
9139 i.tm.operand_types[addr_prefix_disp]
9140 = operand_types[addr_prefix_disp];
9141
09de03fc
JB
9142 /* APX insns acting on byte operands are WIG, yet that can't be expressed
9143 in the templates (they're also covering word/dword/qword operands). */
9144 if (t->opcode_space == SPACE_EVEXMAP4 && !t->opcode_modifier.vexw &&
9145 i.types[i.operands - 1].bitfield.byte)
9146 {
9147 gas_assert (t->opcode_modifier.w);
9148 i.tm.opcode_modifier.vexw = VEXWIG;
9149 }
9150
8bd915b7 9151 switch (found_reverse_match)
29b0f896 9152 {
8bd915b7
JB
9153 case 0:
9154 break;
9155
bd782808
JB
9156 case Opcode_FloatR:
9157 case Opcode_FloatR | Opcode_FloatD:
9158 i.tm.extension_opcode ^= Opcode_FloatR >> 3;
9159 found_reverse_match &= Opcode_FloatD;
9160
9161 /* Fall through. */
8bd915b7 9162 default:
dfd69174
JB
9163 /* If we found a reverse match we must alter the opcode direction
9164 bit and clear/flip the regmem modifier one. found_reverse_match
9165 holds bits to change (different for int & float insns). */
29b0f896
AM
9166
9167 i.tm.base_opcode ^= found_reverse_match;
9168
3083f376 9169 if (i.tm.opcode_space == SPACE_EVEXMAP4)
9170 goto swap_first_2;
9171
dfd69174
JB
9172 /* Certain SIMD insns have their load forms specified in the opcode
9173 table, and hence we need to _set_ RegMem instead of clearing it.
9174 We need to avoid setting the bit though on insns like KMOVW. */
9175 i.tm.opcode_modifier.regmem
9176 = i.tm.opcode_modifier.modrm && i.tm.opcode_modifier.d
9177 && i.tm.operands > 2U - i.tm.opcode_modifier.sse2avx
9178 && !i.tm.opcode_modifier.regmem;
a33ef3c2
JB
9179
9180 /* Fall through. */
9181 case ~0:
9182 i.tm.operand_types[0] = operand_types[i.operands - 1];
9183 i.tm.operand_types[i.operands - 1] = operand_types[0];
8bd915b7
JB
9184 break;
9185
9186 case Opcode_VexW:
9187 /* Only the first two register operands need reversing, alongside
9188 flipping VEX.W. */
9189 i.tm.opcode_modifier.vexw ^= VEXW0 ^ VEXW1;
9190
3083f376 9191 swap_first_2:
8bd915b7
JB
9192 j = i.tm.operand_types[0].bitfield.imm8;
9193 i.tm.operand_types[j] = operand_types[j + 1];
9194 i.tm.operand_types[j + 1] = operand_types[j];
9195 break;
29b0f896
AM
9196 }
9197
fa99fab2 9198 return t;
29b0f896
AM
9199}
9200
9201static int
e3bb37b5 9202check_string (void)
29b0f896 9203{
51c8edf6
JB
9204 unsigned int es_op = i.tm.opcode_modifier.isstring - IS_STRING_ES_OP0;
9205 unsigned int op = i.tm.operand_types[0].bitfield.baseindex ? es_op : 0;
8dc0818e 9206
5e042380 9207 if (i.seg[op] != NULL && i.seg[op] != reg_es)
29b0f896 9208 {
51c8edf6 9209 as_bad (_("`%s' operand %u must use `%ses' segment"),
76d3f746 9210 insn_name (&i.tm),
51c8edf6
JB
9211 intel_syntax ? i.tm.operands - es_op : es_op + 1,
9212 register_prefix);
9213 return 0;
29b0f896 9214 }
51c8edf6
JB
9215
9216 /* There's only ever one segment override allowed per instruction.
9217 This instruction possibly has a legal segment override on the
9218 second operand, so copy the segment to where non-string
9219 instructions store it, allowing common code. */
9220 i.seg[op] = i.seg[1];
9221
29b0f896
AM
9222 return 1;
9223}
9224
9225static int
543613e9 9226process_suffix (void)
29b0f896 9227{
7fc69528 9228 bool is_movx = false;
8b65b895 9229
29b0f896
AM
9230 /* If matched instruction specifies an explicit instruction mnemonic
9231 suffix, use it. */
673fe0f0 9232 if (i.tm.opcode_modifier.size == SIZE16)
40fb9820 9233 i.suffix = WORD_MNEM_SUFFIX;
673fe0f0 9234 else if (i.tm.opcode_modifier.size == SIZE32)
40fb9820 9235 i.suffix = LONG_MNEM_SUFFIX;
673fe0f0 9236 else if (i.tm.opcode_modifier.size == SIZE64)
40fb9820 9237 i.suffix = QWORD_MNEM_SUFFIX;
13e600d0 9238 else if (i.reg_operands
c8f8eebc 9239 && (i.operands > 1 || i.types[0].bitfield.class == Reg)
255571cd 9240 && i.tm.opcode_modifier.operandconstraint != ADDR_PREFIX_OP_REG)
29b0f896 9241 {
65fca059 9242 unsigned int numop = i.operands;
389d00a5
JB
9243
9244 /* MOVSX/MOVZX */
ddb62495 9245 is_movx = (i.tm.opcode_space == SPACE_0F
389d00a5 9246 && (i.tm.base_opcode | 8) == 0xbe)
ddb62495 9247 || (i.tm.opcode_space == SPACE_BASE
389d00a5 9248 && i.tm.base_opcode == 0x63
734dfd1c 9249 && is_cpu (&i.tm, Cpu64));
389d00a5 9250
65fca059
JB
9251 /* movsx/movzx want only their source operand considered here, for the
9252 ambiguity checking below. The suffix will be replaced afterwards
9253 to represent the destination (register). */
389d00a5 9254 if (is_movx && (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63))
65fca059
JB
9255 --i.operands;
9256
643bb870 9257 /* crc32 needs REX.W set regardless of suffix / source operand size. */
7fc69528 9258 if (i.tm.mnem_off == MN_crc32 && i.tm.operand_types[1].bitfield.qword)
643bb870
JB
9259 i.rex |= REX_W;
9260
29b0f896 9261 /* If there's no instruction mnemonic suffix we try to invent one
13e600d0 9262 based on GPR operands. */
29b0f896
AM
9263 if (!i.suffix)
9264 {
9265 /* We take i.suffix from the last register operand specified,
9266 Destination register type is more significant than source
381d071f
L
9267 register type. crc32 in SSE4.2 prefers source register
9268 type. */
7fc69528 9269 unsigned int op = i.tm.mnem_off == MN_crc32 ? 1 : i.operands;
20592a94 9270
1a035124
JB
9271 while (op--)
9272 if (i.tm.operand_types[op].bitfield.instance == InstanceNone
9273 || i.tm.operand_types[op].bitfield.instance == Accum)
9274 {
9275 if (i.types[op].bitfield.class != Reg)
9276 continue;
9277 if (i.types[op].bitfield.byte)
9278 i.suffix = BYTE_MNEM_SUFFIX;
9279 else if (i.types[op].bitfield.word)
9280 i.suffix = WORD_MNEM_SUFFIX;
9281 else if (i.types[op].bitfield.dword)
9282 i.suffix = LONG_MNEM_SUFFIX;
9283 else if (i.types[op].bitfield.qword)
9284 i.suffix = QWORD_MNEM_SUFFIX;
9285 else
9286 continue;
9287 break;
9288 }
65fca059
JB
9289
9290 /* As an exception, movsx/movzx silently default to a byte source
9291 in AT&T mode. */
389d00a5 9292 if (is_movx && i.tm.opcode_modifier.w && !i.suffix && !intel_syntax)
65fca059 9293 i.suffix = BYTE_MNEM_SUFFIX;
29b0f896
AM
9294 }
9295 else if (i.suffix == BYTE_MNEM_SUFFIX)
9296 {
1cb0ab18 9297 if (!check_byte_reg ())
29b0f896
AM
9298 return 0;
9299 }
9300 else if (i.suffix == LONG_MNEM_SUFFIX)
9301 {
1cb0ab18 9302 if (!check_long_reg ())
29b0f896
AM
9303 return 0;
9304 }
9305 else if (i.suffix == QWORD_MNEM_SUFFIX)
9306 {
1cb0ab18 9307 if (!check_qword_reg ())
29b0f896
AM
9308 return 0;
9309 }
9310 else if (i.suffix == WORD_MNEM_SUFFIX)
9311 {
1cb0ab18 9312 if (!check_word_reg ())
29b0f896
AM
9313 return 0;
9314 }
3cd7f3e3
L
9315 else if (intel_syntax
9316 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
9317 /* Do nothing if the instruction is going to ignore the prefix. */
9318 ;
9319 else
9320 abort ();
65fca059
JB
9321
9322 /* Undo the movsx/movzx change done above. */
9323 i.operands = numop;
29b0f896 9324 }
3cd7f3e3
L
9325 else if (i.tm.opcode_modifier.mnemonicsize == DEFAULTSIZE
9326 && !i.suffix)
29b0f896 9327 {
13e600d0
JB
9328 i.suffix = stackop_size;
9329 if (stackop_size == LONG_MNEM_SUFFIX)
06f74c5c
L
9330 {
9331 /* stackop_size is set to LONG_MNEM_SUFFIX for the
9332 .code16gcc directive to support 16-bit mode with
9333 32-bit address. For IRET without a suffix, generate
9334 16-bit IRET (opcode 0xcf) to return from an interrupt
9335 handler. */
13e600d0
JB
9336 if (i.tm.base_opcode == 0xcf)
9337 {
9338 i.suffix = WORD_MNEM_SUFFIX;
9339 as_warn (_("generating 16-bit `iret' for .code16gcc directive"));
9340 }
9341 /* Warn about changed behavior for segment register push/pop. */
9342 else if ((i.tm.base_opcode | 1) == 0x07)
9343 as_warn (_("generating 32-bit `%s', unlike earlier gas versions"),
76d3f746 9344 insn_name (&i.tm));
06f74c5c 9345 }
29b0f896 9346 }
c006a730 9347 else if (!i.suffix
0cfa3eb3
JB
9348 && (i.tm.opcode_modifier.jump == JUMP_ABSOLUTE
9349 || i.tm.opcode_modifier.jump == JUMP_BYTE
9350 || i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT
ddb62495 9351 || (i.tm.opcode_space == SPACE_0F
389d00a5 9352 && i.tm.base_opcode == 0x01 /* [ls][gi]dt */
64e74474 9353 && i.tm.extension_opcode <= 3)))
9306ca4a
JB
9354 {
9355 switch (flag_code)
9356 {
9357 case CODE_64BIT:
40fb9820 9358 if (!i.tm.opcode_modifier.no_qsuf)
9306ca4a 9359 {
828c2a25
JB
9360 if (i.tm.opcode_modifier.jump == JUMP_BYTE
9361 || i.tm.opcode_modifier.no_lsuf)
9362 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a
JB
9363 break;
9364 }
1a0670f3 9365 /* Fall through. */
9306ca4a 9366 case CODE_32BIT:
40fb9820 9367 if (!i.tm.opcode_modifier.no_lsuf)
9306ca4a
JB
9368 i.suffix = LONG_MNEM_SUFFIX;
9369 break;
9370 case CODE_16BIT:
40fb9820 9371 if (!i.tm.opcode_modifier.no_wsuf)
9306ca4a
JB
9372 i.suffix = WORD_MNEM_SUFFIX;
9373 break;
9374 }
9375 }
252b5132 9376
c006a730 9377 if (!i.suffix
3cd7f3e3 9378 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8
JB
9379 /* Also cover lret/retf/iret in 64-bit mode. */
9380 || (flag_code == CODE_64BIT
9381 && !i.tm.opcode_modifier.no_lsuf
9382 && !i.tm.opcode_modifier.no_qsuf))
3cd7f3e3 9383 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
8bbb3ad8
JB
9384 /* Explicit sizing prefixes are assumed to disambiguate insns. */
9385 && !i.prefix[DATA_PREFIX] && !(i.prefix[REX_PREFIX] & REX_W)
62b3f548
JB
9386 /* Accept FLDENV et al without suffix. */
9387 && (i.tm.opcode_modifier.no_ssuf || i.tm.opcode_modifier.floatmf))
29b0f896 9388 {
6c0946d0 9389 unsigned int suffixes, evex = 0;
c006a730
JB
9390
9391 suffixes = !i.tm.opcode_modifier.no_bsuf;
9392 if (!i.tm.opcode_modifier.no_wsuf)
9393 suffixes |= 1 << 1;
9394 if (!i.tm.opcode_modifier.no_lsuf)
9395 suffixes |= 1 << 2;
c006a730
JB
9396 if (!i.tm.opcode_modifier.no_ssuf)
9397 suffixes |= 1 << 4;
9398 if (flag_code == CODE_64BIT && !i.tm.opcode_modifier.no_qsuf)
9399 suffixes |= 1 << 5;
9400
6c0946d0
JB
9401 /* For [XYZ]MMWORD operands inspect operand sizes. While generally
9402 also suitable for AT&T syntax mode, it was requested that this be
9403 restricted to just Intel syntax. */
a5748e0d
JB
9404 if (intel_syntax && is_any_vex_encoding (&i.tm)
9405 && !i.broadcast.type && !i.broadcast.bytes)
6c0946d0 9406 {
b9915cbc 9407 unsigned int op;
6c0946d0 9408
b9915cbc 9409 for (op = 0; op < i.tm.operands; ++op)
6c0946d0 9410 {
4fc85f37
JB
9411 if (vector_size < VSZ512)
9412 {
9413 i.tm.operand_types[op].bitfield.zmmword = 0;
9414 if (vector_size < VSZ256)
9415 {
9416 i.tm.operand_types[op].bitfield.ymmword = 0;
9417 if (i.tm.operand_types[op].bitfield.xmmword
706ce984 9418 && i.tm.opcode_modifier.evex == EVEXDYN)
4fc85f37
JB
9419 i.tm.opcode_modifier.evex = EVEX128;
9420 }
9421 else if (i.tm.operand_types[op].bitfield.ymmword
9422 && !i.tm.operand_types[op].bitfield.xmmword
706ce984 9423 && i.tm.opcode_modifier.evex == EVEXDYN)
4fc85f37
JB
9424 i.tm.opcode_modifier.evex = EVEX256;
9425 }
706ce984 9426 else if (i.tm.opcode_modifier.evex
4fc85f37 9427 && !cpu_arch_flags.bitfield.cpuavx512vl)
6c0946d0 9428 {
b9915cbc
JB
9429 if (i.tm.operand_types[op].bitfield.ymmword)
9430 i.tm.operand_types[op].bitfield.xmmword = 0;
9431 if (i.tm.operand_types[op].bitfield.zmmword)
9432 i.tm.operand_types[op].bitfield.ymmword = 0;
706ce984 9433 if (i.tm.opcode_modifier.evex == EVEXDYN)
b9915cbc
JB
9434 i.tm.opcode_modifier.evex = EVEX512;
9435 }
6c0946d0 9436
b9915cbc
JB
9437 if (i.tm.operand_types[op].bitfield.xmmword
9438 + i.tm.operand_types[op].bitfield.ymmword
9439 + i.tm.operand_types[op].bitfield.zmmword < 2)
9440 continue;
6c0946d0 9441
b9915cbc
JB
9442 /* Any properly sized operand disambiguates the insn. */
9443 if (i.types[op].bitfield.xmmword
9444 || i.types[op].bitfield.ymmword
9445 || i.types[op].bitfield.zmmword)
9446 {
9447 suffixes &= ~(7 << 6);
9448 evex = 0;
9449 break;
9450 }
6c0946d0 9451
b9915cbc
JB
9452 if ((i.flags[op] & Operand_Mem)
9453 && i.tm.operand_types[op].bitfield.unspecified)
9454 {
9455 if (i.tm.operand_types[op].bitfield.xmmword)
9456 suffixes |= 1 << 6;
9457 if (i.tm.operand_types[op].bitfield.ymmword)
9458 suffixes |= 1 << 7;
9459 if (i.tm.operand_types[op].bitfield.zmmword)
9460 suffixes |= 1 << 8;
706ce984 9461 if (i.tm.opcode_modifier.evex)
b9915cbc 9462 evex = EVEX512;
6c0946d0
JB
9463 }
9464 }
9465 }
9466
9467 /* Are multiple suffixes / operand sizes allowed? */
c006a730 9468 if (suffixes & (suffixes - 1))
9306ca4a 9469 {
873494c8 9470 if (intel_syntax
3cd7f3e3 9471 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8 9472 || operand_check == check_error))
9306ca4a 9473 {
76d3f746 9474 as_bad (_("ambiguous operand size for `%s'"), insn_name (&i.tm));
9306ca4a
JB
9475 return 0;
9476 }
c006a730 9477 if (operand_check == check_error)
9306ca4a 9478 {
c006a730 9479 as_bad (_("no instruction mnemonic suffix given and "
76d3f746 9480 "no register operands; can't size `%s'"), insn_name (&i.tm));
9306ca4a
JB
9481 return 0;
9482 }
c006a730 9483 if (operand_check == check_warning)
873494c8
JB
9484 as_warn (_("%s; using default for `%s'"),
9485 intel_syntax
9486 ? _("ambiguous operand size")
9487 : _("no instruction mnemonic suffix given and "
9488 "no register operands"),
76d3f746 9489 insn_name (&i.tm));
c006a730
JB
9490
9491 if (i.tm.opcode_modifier.floatmf)
9492 i.suffix = SHORT_MNEM_SUFFIX;
389d00a5 9493 else if (is_movx)
65fca059 9494 /* handled below */;
6c0946d0
JB
9495 else if (evex)
9496 i.tm.opcode_modifier.evex = evex;
c006a730
JB
9497 else if (flag_code == CODE_16BIT)
9498 i.suffix = WORD_MNEM_SUFFIX;
1a035124 9499 else if (!i.tm.opcode_modifier.no_lsuf)
c006a730 9500 i.suffix = LONG_MNEM_SUFFIX;
1a035124
JB
9501 else
9502 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a 9503 }
29b0f896 9504 }
252b5132 9505
389d00a5 9506 if (is_movx)
65fca059
JB
9507 {
9508 /* In Intel syntax, movsx/movzx must have a "suffix" (checked above).
9509 In AT&T syntax, if there is no suffix (warned about above), the default
9510 will be byte extension. */
9511 if (i.tm.opcode_modifier.w && i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
9512 i.tm.base_opcode |= 1;
9513
9514 /* For further processing, the suffix should represent the destination
9515 (register). This is already the case when one was used with
9516 mov[sz][bw]*, but we need to replace it for mov[sz]x, or if there was
9517 no suffix to begin with. */
9518 if (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63 || !i.suffix)
9519 {
9520 if (i.types[1].bitfield.word)
9521 i.suffix = WORD_MNEM_SUFFIX;
9522 else if (i.types[1].bitfield.qword)
9523 i.suffix = QWORD_MNEM_SUFFIX;
9524 else
9525 i.suffix = LONG_MNEM_SUFFIX;
9526
9527 i.tm.opcode_modifier.w = 0;
9528 }
9529 }
9530
50128d0c
JB
9531 if (!i.tm.opcode_modifier.modrm && i.reg_operands && i.tm.operands < 3)
9532 i.short_form = (i.tm.operand_types[0].bitfield.class == Reg)
9533 != (i.tm.operand_types[1].bitfield.class == Reg);
9534
d2224064
JB
9535 /* Change the opcode based on the operand size given by i.suffix. */
9536 switch (i.suffix)
29b0f896 9537 {
d2224064
JB
9538 /* Size floating point instruction. */
9539 case LONG_MNEM_SUFFIX:
9540 if (i.tm.opcode_modifier.floatmf)
9541 {
9542 i.tm.base_opcode ^= 4;
9543 break;
9544 }
9545 /* fall through */
9546 case WORD_MNEM_SUFFIX:
9547 case QWORD_MNEM_SUFFIX:
29b0f896 9548 /* It's not a byte, select word/dword operation. */
40fb9820 9549 if (i.tm.opcode_modifier.w)
29b0f896 9550 {
50128d0c 9551 if (i.short_form)
29b0f896
AM
9552 i.tm.base_opcode |= 8;
9553 else
9554 i.tm.base_opcode |= 1;
9555 }
4e3be89f
JB
9556
9557 /* Set mode64 for an operand. */
9558 if (i.suffix == QWORD_MNEM_SUFFIX)
9559 {
9560 if (flag_code == CODE_64BIT
9561 && !i.tm.opcode_modifier.norex64
9562 && !i.tm.opcode_modifier.vexw
9563 /* Special case for xchg %rax,%rax. It is NOP and doesn't
9564 need rex64. */
9565 && ! (i.operands == 2
9566 && i.tm.base_opcode == 0x90
9567 && i.tm.opcode_space == SPACE_BASE
9568 && i.types[0].bitfield.instance == Accum
9569 && i.types[1].bitfield.instance == Accum))
9570 i.rex |= REX_W;
9571
9572 break;
9573 }
9574
d2224064
JB
9575 /* fall through */
9576 case SHORT_MNEM_SUFFIX:
29b0f896
AM
9577 /* Now select between word & dword operations via the operand
9578 size prefix, except for instructions that will ignore this
9579 prefix anyway. */
4e3be89f 9580 if (i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
c8f8eebc 9581 && !i.tm.opcode_modifier.floatmf
6177c84d
CL
9582 && (!is_any_vex_encoding (&i.tm)
9583 || i.tm.opcode_space == SPACE_EVEXMAP4)
c8f8eebc
JB
9584 && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
9585 || (flag_code == CODE_64BIT
9586 && i.tm.opcode_modifier.jump == JUMP_BYTE)))
24eab124
AM
9587 {
9588 unsigned int prefix = DATA_PREFIX_OPCODE;
543613e9 9589
0cfa3eb3 9590 if (i.tm.opcode_modifier.jump == JUMP_BYTE) /* jcxz, loop */
29b0f896 9591 prefix = ADDR_PREFIX_OPCODE;
252b5132 9592
6177c84d
CL
9593 /* The DATA PREFIX of EVEX promoted from legacy APX instructions
9594 needs to be adjusted. */
9595 if (i.tm.opcode_space == SPACE_EVEXMAP4)
9596 {
9597 gas_assert (!i.tm.opcode_modifier.opcodeprefix);
9598 i.tm.opcode_modifier.opcodeprefix = PREFIX_0X66;
9599 }
9600 else if (!add_prefix (prefix))
29b0f896 9601 return 0;
24eab124 9602 }
252b5132 9603
d2224064 9604 break;
8bbb3ad8
JB
9605
9606 case 0:
f9a6a8f0 9607 /* Select word/dword/qword operation with explicit data sizing prefix
8bbb3ad8
JB
9608 when there are no suitable register operands. */
9609 if (i.tm.opcode_modifier.w
9610 && (i.prefix[DATA_PREFIX] || (i.prefix[REX_PREFIX] & REX_W))
9611 && (!i.reg_operands
9612 || (i.reg_operands == 1
9613 /* ShiftCount */
9614 && (i.tm.operand_types[0].bitfield.instance == RegC
9615 /* InOutPortReg */
9616 || i.tm.operand_types[0].bitfield.instance == RegD
9617 || i.tm.operand_types[1].bitfield.instance == RegD
7fc69528 9618 || i.tm.mnem_off == MN_crc32))))
8bbb3ad8
JB
9619 i.tm.base_opcode |= 1;
9620 break;
29b0f896 9621 }
7ecd2f8b 9622
255571cd 9623 if (i.tm.opcode_modifier.operandconstraint == ADDR_PREFIX_OP_REG)
c0a30a9f 9624 {
c8f8eebc
JB
9625 gas_assert (!i.suffix);
9626 gas_assert (i.reg_operands);
c0a30a9f 9627
c8f8eebc
JB
9628 if (i.tm.operand_types[0].bitfield.instance == Accum
9629 || i.operands == 1)
9630 {
9631 /* The address size override prefix changes the size of the
9632 first operand. */
9633 if (flag_code == CODE_64BIT
9634 && i.op[0].regs->reg_type.bitfield.word)
9635 {
9636 as_bad (_("16-bit addressing unavailable for `%s'"),
76d3f746 9637 insn_name (&i.tm));
c8f8eebc
JB
9638 return 0;
9639 }
9640
9641 if ((flag_code == CODE_32BIT
9642 ? i.op[0].regs->reg_type.bitfield.word
9643 : i.op[0].regs->reg_type.bitfield.dword)
9644 && !add_prefix (ADDR_PREFIX_OPCODE))
9645 return 0;
9646 }
c0a30a9f
L
9647 else
9648 {
c8f8eebc
JB
9649 /* Check invalid register operand when the address size override
9650 prefix changes the size of register operands. */
9651 unsigned int op;
9652 enum { need_word, need_dword, need_qword } need;
9653
27f13469 9654 /* Check the register operand for the address size prefix if
b3a3496f 9655 the memory operand has no real registers, like symbol, DISP
829f3fe1 9656 or bogus (x32-only) symbol(%rip) when symbol(%eip) is meant. */
27f13469
L
9657 if (i.mem_operands == 1
9658 && i.reg_operands == 1
9659 && i.operands == 2
27f13469 9660 && i.types[1].bitfield.class == Reg
b3a3496f
L
9661 && (flag_code == CODE_32BIT
9662 ? i.op[1].regs->reg_type.bitfield.word
9663 : i.op[1].regs->reg_type.bitfield.dword)
9664 && ((i.base_reg == NULL && i.index_reg == NULL)
829f3fe1
JB
9665#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
9666 || (x86_elf_abi == X86_64_X32_ABI
9667 && i.base_reg
b3a3496f
L
9668 && i.base_reg->reg_num == RegIP
9669 && i.base_reg->reg_type.bitfield.qword))
829f3fe1
JB
9670#else
9671 || 0)
9672#endif
27f13469
L
9673 && !add_prefix (ADDR_PREFIX_OPCODE))
9674 return 0;
9675
c8f8eebc
JB
9676 if (flag_code == CODE_32BIT)
9677 need = i.prefix[ADDR_PREFIX] ? need_word : need_dword;
9678 else if (i.prefix[ADDR_PREFIX])
c0a30a9f
L
9679 need = need_dword;
9680 else
9681 need = flag_code == CODE_64BIT ? need_qword : need_word;
c0a30a9f 9682
c8f8eebc
JB
9683 for (op = 0; op < i.operands; op++)
9684 {
9685 if (i.types[op].bitfield.class != Reg)
9686 continue;
9687
9688 switch (need)
9689 {
9690 case need_word:
9691 if (i.op[op].regs->reg_type.bitfield.word)
9692 continue;
9693 break;
9694 case need_dword:
9695 if (i.op[op].regs->reg_type.bitfield.dword)
9696 continue;
9697 break;
9698 case need_qword:
9699 if (i.op[op].regs->reg_type.bitfield.qword)
9700 continue;
9701 break;
9702 }
9703
9704 as_bad (_("invalid register operand size for `%s'"),
76d3f746 9705 insn_name (&i.tm));
c8f8eebc
JB
9706 return 0;
9707 }
9708 }
c0a30a9f
L
9709 }
9710
29b0f896
AM
9711 return 1;
9712}
3e73aa7c 9713
29b0f896 9714static int
543613e9 9715check_byte_reg (void)
29b0f896
AM
9716{
9717 int op;
543613e9 9718
29b0f896
AM
9719 for (op = i.operands; --op >= 0;)
9720 {
dc821c5f 9721 /* Skip non-register operands. */
bab6aec1 9722 if (i.types[op].bitfield.class != Reg)
dc821c5f
JB
9723 continue;
9724
29b0f896
AM
9725 /* If this is an eight bit register, it's OK. If it's the 16 or
9726 32 bit version of an eight bit register, we will just use the
9727 low portion, and that's OK too. */
dc821c5f 9728 if (i.types[op].bitfield.byte)
29b0f896
AM
9729 continue;
9730
5a819eb9 9731 /* I/O port address operands are OK too. */
75e5731b
JB
9732 if (i.tm.operand_types[op].bitfield.instance == RegD
9733 && i.tm.operand_types[op].bitfield.word)
5a819eb9
JB
9734 continue;
9735
9706160a 9736 /* crc32 only wants its source operand checked here. */
7fc69528 9737 if (i.tm.mnem_off == MN_crc32 && op != 0)
9344ff29
L
9738 continue;
9739
29b0f896 9740 /* Any other register is bad. */
73c76375
JB
9741 as_bad (_("`%s%s' not allowed with `%s%c'"),
9742 register_prefix, i.op[op].regs->reg_name,
76d3f746 9743 insn_name (&i.tm), i.suffix);
73c76375 9744 return 0;
29b0f896
AM
9745 }
9746 return 1;
9747}
9748
9749static int
e3bb37b5 9750check_long_reg (void)
29b0f896
AM
9751{
9752 int op;
9753
9754 for (op = i.operands; --op >= 0;)
dc821c5f 9755 /* Skip non-register operands. */
bab6aec1 9756 if (i.types[op].bitfield.class != Reg)
dc821c5f 9757 continue;
29b0f896
AM
9758 /* Reject eight bit registers, except where the template requires
9759 them. (eg. movzb) */
dc821c5f 9760 else if (i.types[op].bitfield.byte
bab6aec1 9761 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 9762 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
9763 && (i.tm.operand_types[op].bitfield.word
9764 || i.tm.operand_types[op].bitfield.dword))
29b0f896 9765 {
a540244d
L
9766 as_bad (_("`%s%s' not allowed with `%s%c'"),
9767 register_prefix,
29b0f896 9768 i.op[op].regs->reg_name,
76d3f746 9769 insn_name (&i.tm),
29b0f896
AM
9770 i.suffix);
9771 return 0;
9772 }
d9a1b711
JB
9773 /* Error if the e prefix on a general reg is missing, or if the r
9774 prefix on a general reg is present. */
9775 else if ((i.types[op].bitfield.word
9776 || i.types[op].bitfield.qword)
bab6aec1 9777 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 9778 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 9779 && i.tm.operand_types[op].bitfield.dword)
29b0f896 9780 {
be4c5e58
L
9781 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
9782 register_prefix, i.op[op].regs->reg_name,
9783 i.suffix);
9784 return 0;
252b5132 9785 }
29b0f896
AM
9786 return 1;
9787}
252b5132 9788
29b0f896 9789static int
e3bb37b5 9790check_qword_reg (void)
29b0f896
AM
9791{
9792 int op;
252b5132 9793
29b0f896 9794 for (op = i.operands; --op >= 0; )
dc821c5f 9795 /* Skip non-register operands. */
bab6aec1 9796 if (i.types[op].bitfield.class != Reg)
dc821c5f 9797 continue;
29b0f896
AM
9798 /* Reject eight bit registers, except where the template requires
9799 them. (eg. movzb) */
dc821c5f 9800 else if (i.types[op].bitfield.byte
bab6aec1 9801 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 9802 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 9803 && (i.tm.operand_types[op].bitfield.word
742b55c7
JB
9804 || i.tm.operand_types[op].bitfield.dword
9805 || i.tm.operand_types[op].bitfield.qword))
29b0f896 9806 {
a540244d
L
9807 as_bad (_("`%s%s' not allowed with `%s%c'"),
9808 register_prefix,
29b0f896 9809 i.op[op].regs->reg_name,
76d3f746 9810 insn_name (&i.tm),
29b0f896
AM
9811 i.suffix);
9812 return 0;
9813 }
d9a1b711 9814 /* Error if the r prefix on a general reg is missing. */
dc821c5f
JB
9815 else if ((i.types[op].bitfield.word
9816 || i.types[op].bitfield.dword)
bab6aec1 9817 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 9818 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 9819 && i.tm.operand_types[op].bitfield.qword)
29b0f896
AM
9820 {
9821 /* Prohibit these changes in the 64bit mode, since the
9822 lowering is more complicated. */
1cb0ab18
JB
9823 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
9824 register_prefix, i.op[op].regs->reg_name, i.suffix);
9825 return 0;
252b5132 9826 }
29b0f896
AM
9827 return 1;
9828}
252b5132 9829
29b0f896 9830static int
e3bb37b5 9831check_word_reg (void)
29b0f896
AM
9832{
9833 int op;
9834 for (op = i.operands; --op >= 0;)
dc821c5f 9835 /* Skip non-register operands. */
bab6aec1 9836 if (i.types[op].bitfield.class != Reg)
dc821c5f 9837 continue;
29b0f896
AM
9838 /* Reject eight bit registers, except where the template requires
9839 them. (eg. movzb) */
dc821c5f 9840 else if (i.types[op].bitfield.byte
bab6aec1 9841 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 9842 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
9843 && (i.tm.operand_types[op].bitfield.word
9844 || i.tm.operand_types[op].bitfield.dword))
29b0f896 9845 {
a540244d
L
9846 as_bad (_("`%s%s' not allowed with `%s%c'"),
9847 register_prefix,
29b0f896 9848 i.op[op].regs->reg_name,
76d3f746 9849 insn_name (&i.tm),
29b0f896
AM
9850 i.suffix);
9851 return 0;
9852 }
9706160a
JB
9853 /* Error if the e or r prefix on a general reg is present. */
9854 else if ((i.types[op].bitfield.dword
dc821c5f 9855 || i.types[op].bitfield.qword)
bab6aec1 9856 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 9857 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 9858 && i.tm.operand_types[op].bitfield.word)
252b5132 9859 {
9706160a
JB
9860 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
9861 register_prefix, i.op[op].regs->reg_name,
9862 i.suffix);
9863 return 0;
29b0f896
AM
9864 }
9865 return 1;
9866}
252b5132 9867
29b0f896 9868static int
40fb9820 9869update_imm (unsigned int j)
29b0f896 9870{
bc0844ae 9871 i386_operand_type overlap = i.types[j];
c34d1cc9
JB
9872
9873 if (i.tm.operand_types[j].bitfield.imm8
9874 && i.tm.operand_types[j].bitfield.imm8s
9875 && overlap.bitfield.imm8 && overlap.bitfield.imm8s)
9876 {
9877 /* This combination is used on 8-bit immediates where e.g. $~0 is
9878 desirable to permit. We're past operand type matching, so simply
9879 put things back in the shape they were before introducing the
9880 distinction between Imm8, Imm8S, and Imm8|Imm8S. */
9881 overlap.bitfield.imm8s = 0;
9882 }
9883
be1643ff
JB
9884 if (overlap.bitfield.imm8
9885 + overlap.bitfield.imm8s
9886 + overlap.bitfield.imm16
9887 + overlap.bitfield.imm32
9888 + overlap.bitfield.imm32s
9889 + overlap.bitfield.imm64 > 1)
29b0f896 9890 {
05909f23
JB
9891 static const i386_operand_type imm16 = { .bitfield = { .imm16 = 1 } };
9892 static const i386_operand_type imm32 = { .bitfield = { .imm32 = 1 } };
9893 static const i386_operand_type imm32s = { .bitfield = { .imm32s = 1 } };
9894 static const i386_operand_type imm16_32 = { .bitfield =
9895 { .imm16 = 1, .imm32 = 1 }
9896 };
9897 static const i386_operand_type imm16_32s = { .bitfield =
9898 { .imm16 = 1, .imm32s = 1 }
9899 };
9900 static const i386_operand_type imm16_32_32s = { .bitfield =
9901 { .imm16 = 1, .imm32 = 1, .imm32s = 1 }
9902 };
9903
29b0f896
AM
9904 if (i.suffix)
9905 {
40fb9820
L
9906 i386_operand_type temp;
9907
0dfbf9d7 9908 operand_type_set (&temp, 0);
7ab9ffdd 9909 if (i.suffix == BYTE_MNEM_SUFFIX)
40fb9820
L
9910 {
9911 temp.bitfield.imm8 = overlap.bitfield.imm8;
9912 temp.bitfield.imm8s = overlap.bitfield.imm8s;
9913 }
9914 else if (i.suffix == WORD_MNEM_SUFFIX)
9915 temp.bitfield.imm16 = overlap.bitfield.imm16;
9916 else if (i.suffix == QWORD_MNEM_SUFFIX)
9917 {
9918 temp.bitfield.imm64 = overlap.bitfield.imm64;
9919 temp.bitfield.imm32s = overlap.bitfield.imm32s;
9920 }
9921 else
9922 temp.bitfield.imm32 = overlap.bitfield.imm32;
9923 overlap = temp;
29b0f896 9924 }
0dfbf9d7
L
9925 else if (operand_type_equal (&overlap, &imm16_32_32s)
9926 || operand_type_equal (&overlap, &imm16_32)
9927 || operand_type_equal (&overlap, &imm16_32s))
29b0f896 9928 {
f79d55e1
JB
9929 if ((flag_code == CODE_16BIT)
9930 ^ (i.prefix[DATA_PREFIX] != 0 && !(i.prefix[REX_PREFIX] & REX_W)))
65da13b5 9931 overlap = imm16;
40fb9820 9932 else
65da13b5 9933 overlap = imm32s;
29b0f896 9934 }
8bbb3ad8
JB
9935 else if (i.prefix[REX_PREFIX] & REX_W)
9936 overlap = operand_type_and (overlap, imm32s);
9937 else if (i.prefix[DATA_PREFIX])
9938 overlap = operand_type_and (overlap,
9939 flag_code != CODE_16BIT ? imm16 : imm32);
be1643ff
JB
9940 if (overlap.bitfield.imm8
9941 + overlap.bitfield.imm8s
9942 + overlap.bitfield.imm16
9943 + overlap.bitfield.imm32
9944 + overlap.bitfield.imm32s
9945 + overlap.bitfield.imm64 != 1)
29b0f896 9946 {
4eed87de
AM
9947 as_bad (_("no instruction mnemonic suffix given; "
9948 "can't determine immediate size"));
29b0f896
AM
9949 return 0;
9950 }
9951 }
40fb9820 9952 i.types[j] = overlap;
29b0f896 9953
40fb9820
L
9954 return 1;
9955}
9956
9957static int
9958finalize_imm (void)
9959{
bc0844ae 9960 unsigned int j, n;
29b0f896 9961
bc0844ae
L
9962 /* Update the first 2 immediate operands. */
9963 n = i.operands > 2 ? 2 : i.operands;
9964 if (n)
9965 {
9966 for (j = 0; j < n; j++)
9967 if (update_imm (j) == 0)
9968 return 0;
40fb9820 9969
bc0844ae
L
9970 /* The 3rd operand can't be immediate operand. */
9971 gas_assert (operand_type_check (i.types[2], imm) == 0);
9972 }
29b0f896
AM
9973
9974 return 1;
9975}
9976
0a3eba42
JB
9977static INLINE void set_rex_vrex (const reg_entry *r, unsigned int rex_bit,
9978 bool do_sse2avx)
9979{
9980 if (r->reg_flags & RegRex)
9981 {
9982 if (i.rex & rex_bit)
9983 as_bad (_("same type of prefix used twice"));
9984 i.rex |= rex_bit;
9985 }
9986 else if (do_sse2avx && (i.rex & rex_bit) && i.vex.register_specifier)
9987 {
9988 gas_assert (i.vex.register_specifier == r);
9989 i.vex.register_specifier += 8;
9990 }
9991
9992 if (r->reg_flags & RegVRex)
9993 i.vrex |= rex_bit;
80d61d8d
CL
9994
9995 if (r->reg_flags & RegRex2)
9996 i.rex2 |= rex_bit;
9997}
9998
9999static INLINE void
10000set_rex_rex2 (const reg_entry *r, unsigned int rex_bit)
10001{
10002 if ((r->reg_flags & RegRex) != 0)
10003 i.rex |= rex_bit;
10004 if ((r->reg_flags & RegRex2) != 0)
10005 i.rex2 |= rex_bit;
0a3eba42
JB
10006}
10007
29b0f896 10008static int
e3bb37b5 10009process_operands (void)
29b0f896
AM
10010{
10011 /* Default segment register this instruction will use for memory
10012 accesses. 0 means unknown. This is only for optimizing out
10013 unnecessary segment overrides. */
5e042380 10014 const reg_entry *default_seg = NULL;
29b0f896 10015
3083f376 10016 for (unsigned int j = 0; j < i.operands; j++)
10017 if (i.types[j].bitfield.instance != InstanceNone)
10018 i.reg_operands--;
9fe07b7f
JB
10019 else if (i.tm.opcode_space == SPACE_EVEXMAP4
10020 && i.types[j].bitfield.class == RegSIMD
10021 && (i.op[j].regs->reg_flags & RegVRex)
10022 && !dot_insn ())
10023 /* Just raise an error, but continue processing. */
10024 as_bad (_("`%s%s' cannot be used with `%s'"),
10025 register_prefix, i.op[j].regs->reg_name, insn_name (&i.tm));
31b4cda3 10026
a5aeccd9
JB
10027 if (i.tm.opcode_modifier.sse2avx)
10028 {
10029 /* Legacy encoded insns allow explicit REX prefixes, so these prefixes
10030 need converting. */
10031 i.rex |= i.prefix[REX_PREFIX] & (REX_W | REX_R | REX_X | REX_B);
10032 i.prefix[REX_PREFIX] = 0;
10033 i.rex_encoding = 0;
10034 }
c423d21a
JB
10035 /* ImmExt should be processed after SSE2AVX. */
10036 else if (i.tm.opcode_modifier.immext)
10037 process_immext ();
a5aeccd9 10038
ecb96e55
JB
10039 /* TILEZERO is unusual in that it has a single operand encoded in ModR/M.reg,
10040 not ModR/M.rm. To avoid special casing this in build_modrm_byte(), fake a
10041 new destination operand here, while converting the source one to register
10042 number 0. */
10043 if (i.tm.mnem_off == MN_tilezero)
10044 {
10045 i.op[1].regs = i.op[0].regs;
10046 i.op[0].regs -= i.op[0].regs->reg_num;
10047 i.types[1] = i.types[0];
10048 i.tm.operand_types[1] = i.tm.operand_types[0];
10049 i.flags[1] = i.flags[0];
10050 i.operands++;
10051 i.reg_operands++;
10052 i.tm.operands++;
10053 }
10054
2426c15f 10055 if (i.tm.opcode_modifier.sse2avx && i.tm.opcode_modifier.vexvvvv)
29b0f896 10056 {
05909f23
JB
10057 static const i386_operand_type regxmm = {
10058 .bitfield = { .class = RegSIMD, .xmmword = 1 }
10059 };
91d6fa6a
NC
10060 unsigned int dupl = i.operands;
10061 unsigned int dest = dupl - 1;
9fcfb3d7
L
10062 unsigned int j;
10063
c0f3af97 10064 /* The destination must be an xmm register. */
9c2799c2 10065 gas_assert (i.reg_operands
91d6fa6a 10066 && MAX_OPERANDS > dupl
7ab9ffdd 10067 && operand_type_equal (&i.types[dest], &regxmm));
c0f3af97 10068
75e5731b 10069 if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 10070 && i.tm.operand_types[0].bitfield.xmmword)
e2ec9d29 10071 {
95dfdd85
JB
10072 /* Keep xmm0 for instructions with VEX prefix and 3
10073 sources. */
10074 i.tm.operand_types[0].bitfield.instance = InstanceNone;
10075 i.tm.operand_types[0].bitfield.class = RegSIMD;
31b4cda3 10076 i.reg_operands++;
95dfdd85 10077 goto duplicate;
c0f3af97 10078 }
95dfdd85
JB
10079
10080 if (i.tm.opcode_modifier.operandconstraint == IMPLICIT_1ST_XMM0)
7ab9ffdd 10081 {
aa180741 10082 gas_assert ((MAX_OPERANDS - 1) > dupl);
c0f3af97
L
10083
10084 /* Add the implicit xmm0 for instructions with VEX prefix
10085 and 3 sources. */
10086 for (j = i.operands; j > 0; j--)
10087 {
10088 i.op[j] = i.op[j - 1];
10089 i.types[j] = i.types[j - 1];
10090 i.tm.operand_types[j] = i.tm.operand_types[j - 1];
8dc0818e 10091 i.flags[j] = i.flags[j - 1];
c0f3af97
L
10092 }
10093 i.op[0].regs
629310ab 10094 = (const reg_entry *) str_hash_find (reg_hash, "xmm0");
7ab9ffdd 10095 i.types[0] = regxmm;
c0f3af97
L
10096 i.tm.operand_types[0] = regxmm;
10097
10098 i.operands += 2;
10099 i.reg_operands += 2;
10100 i.tm.operands += 2;
10101
91d6fa6a 10102 dupl++;
c0f3af97 10103 dest++;
91d6fa6a
NC
10104 i.op[dupl] = i.op[dest];
10105 i.types[dupl] = i.types[dest];
10106 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 10107 i.flags[dupl] = i.flags[dest];
e2ec9d29 10108 }
c0f3af97
L
10109 else
10110 {
dc1e8a47 10111 duplicate:
c0f3af97
L
10112 i.operands++;
10113 i.reg_operands++;
10114 i.tm.operands++;
10115
91d6fa6a
NC
10116 i.op[dupl] = i.op[dest];
10117 i.types[dupl] = i.types[dest];
10118 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 10119 i.flags[dupl] = i.flags[dest];
c0f3af97
L
10120 }
10121
10122 if (i.tm.opcode_modifier.immext)
10123 process_immext ();
10124 }
75e5731b 10125 else if (i.tm.operand_types[0].bitfield.instance == Accum
bd782808 10126 && i.tm.opcode_modifier.modrm)
c0f3af97
L
10127 {
10128 unsigned int j;
10129
9fcfb3d7
L
10130 for (j = 1; j < i.operands; j++)
10131 {
10132 i.op[j - 1] = i.op[j];
10133 i.types[j - 1] = i.types[j];
10134
10135 /* We need to adjust fields in i.tm since they are used by
10136 build_modrm_byte. */
10137 i.tm.operand_types [j - 1] = i.tm.operand_types [j];
8dc0818e
JB
10138
10139 i.flags[j - 1] = i.flags[j];
9fcfb3d7
L
10140 }
10141
31b4cda3
JB
10142 /* No adjustment to i.reg_operands: This was already done at the top
10143 of the function. */
e2ec9d29 10144 i.operands--;
e2ec9d29
L
10145 i.tm.operands--;
10146 }
255571cd 10147 else if (i.tm.opcode_modifier.operandconstraint == IMPLICIT_QUAD_GROUP)
920d2ddc 10148 {
a477a8c4
JB
10149 unsigned int regnum, first_reg_in_group, last_reg_in_group;
10150
920d2ddc 10151 /* The second operand must be {x,y,z}mmN, where N is a multiple of 4. */
3528c362 10152 gas_assert (i.operands >= 2 && i.types[1].bitfield.class == RegSIMD);
a477a8c4
JB
10153 regnum = register_number (i.op[1].regs);
10154 first_reg_in_group = regnum & ~3;
10155 last_reg_in_group = first_reg_in_group + 3;
10156 if (regnum != first_reg_in_group)
10157 as_warn (_("source register `%s%s' implicitly denotes"
10158 " `%s%.3s%u' to `%s%.3s%u' source group in `%s'"),
10159 register_prefix, i.op[1].regs->reg_name,
10160 register_prefix, i.op[1].regs->reg_name, first_reg_in_group,
10161 register_prefix, i.op[1].regs->reg_name, last_reg_in_group,
76d3f746 10162 insn_name (&i.tm));
a477a8c4 10163 }
255571cd 10164 else if (i.tm.opcode_modifier.operandconstraint == REG_KLUDGE)
e2ec9d29
L
10165 {
10166 /* The imul $imm, %reg instruction is converted into
10167 imul $imm, %reg, %reg, and the clr %reg instruction
10168 is converted into xor %reg, %reg. */
10169
10170 unsigned int first_reg_op;
10171
10172 if (operand_type_check (i.types[0], reg))
10173 first_reg_op = 0;
10174 else
10175 first_reg_op = 1;
10176 /* Pretend we saw the extra register operand. */
9c2799c2 10177 gas_assert (i.reg_operands == 1
7ab9ffdd 10178 && i.op[first_reg_op + 1].regs == 0);
e2ec9d29
L
10179 i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
10180 i.types[first_reg_op + 1] = i.types[first_reg_op];
10181 i.operands++;
10182 i.reg_operands++;
29b0f896
AM
10183 }
10184
85b80b0f 10185 if (i.tm.opcode_modifier.modrm)
29b0f896
AM
10186 {
10187 /* The opcode is completed (modulo i.tm.extension_opcode which
52271982
AM
10188 must be put into the modrm byte). Now, we make the modrm and
10189 index base bytes based on all the info we've collected. */
29b0f896
AM
10190
10191 default_seg = build_modrm_byte ();
bd782808
JB
10192
10193 if (!quiet_warnings && i.tm.opcode_modifier.operandconstraint == UGH)
10194 {
10195 /* Warn about some common errors, but press on regardless. */
10196 if (i.operands == 2)
10197 {
10198 /* Reversed arguments on faddp or fmulp. */
10199 as_warn (_("translating to `%s %s%s,%s%s'"), insn_name (&i.tm),
10200 register_prefix, i.op[!intel_syntax].regs->reg_name,
10201 register_prefix, i.op[intel_syntax].regs->reg_name);
10202 }
10203 else if (i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
10204 {
10205 /* Extraneous `l' suffix on fp insn. */
10206 as_warn (_("translating to `%s %s%s'"), insn_name (&i.tm),
10207 register_prefix, i.op[0].regs->reg_name);
10208 }
10209 }
29b0f896 10210 }
0ff3b7d0 10211 else if (i.types[0].bitfield.class == SReg && !dot_insn ())
85b80b0f
JB
10212 {
10213 if (flag_code != CODE_64BIT
10214 ? i.tm.base_opcode == POP_SEG_SHORT
10215 && i.op[0].regs->reg_num == 1
389d00a5 10216 : (i.tm.base_opcode | 1) == (POP_SEG386_SHORT & 0xff)
85b80b0f
JB
10217 && i.op[0].regs->reg_num < 4)
10218 {
10219 as_bad (_("you can't `%s %s%s'"),
76d3f746 10220 insn_name (&i.tm), register_prefix, i.op[0].regs->reg_name);
85b80b0f
JB
10221 return 0;
10222 }
389d00a5 10223 if (i.op[0].regs->reg_num > 3
ddb62495 10224 && i.tm.opcode_space == SPACE_BASE )
85b80b0f 10225 {
389d00a5 10226 i.tm.base_opcode ^= (POP_SEG_SHORT ^ POP_SEG386_SHORT) & 0xff;
ddb62495 10227 i.tm.opcode_space = SPACE_0F;
85b80b0f
JB
10228 }
10229 i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
10230 }
ddb62495 10231 else if (i.tm.opcode_space == SPACE_BASE
389d00a5 10232 && (i.tm.base_opcode & ~3) == MOV_AX_DISP32)
29b0f896 10233 {
5e042380 10234 default_seg = reg_ds;
29b0f896 10235 }
40fb9820 10236 else if (i.tm.opcode_modifier.isstring)
29b0f896
AM
10237 {
10238 /* For the string instructions that allow a segment override
10239 on one of their operands, the default segment is ds. */
5e042380 10240 default_seg = reg_ds;
29b0f896 10241 }
50128d0c 10242 else if (i.short_form)
85b80b0f 10243 {
0ff3b7d0
JB
10244 /* The register operand is in the 1st or 2nd non-immediate operand. */
10245 const reg_entry *r = i.op[i.imm_operands].regs;
85b80b0f 10246
0ff3b7d0
JB
10247 if (!dot_insn ()
10248 && r->reg_type.bitfield.instance == Accum
10249 && i.op[i.imm_operands + 1].regs)
10250 r = i.op[i.imm_operands + 1].regs;
85b80b0f 10251 /* Register goes in low 3 bits of opcode. */
4943d587 10252 i.tm.base_opcode |= r->reg_num;
0a3eba42 10253 set_rex_vrex (r, REX_B, false);
0ff3b7d0
JB
10254
10255 if (dot_insn () && i.reg_operands == 2)
10256 {
10257 gas_assert (is_any_vex_encoding (&i.tm)
e346d50a 10258 || i.encoding != encoding_default);
0ff3b7d0
JB
10259 i.vex.register_specifier = i.op[i.operands - 1].regs;
10260 }
10261 }
10262 else if (i.reg_operands == 1
10263 && !i.flags[i.operands - 1]
10264 && i.tm.operand_types[i.operands - 1].bitfield.instance
10265 == InstanceNone)
10266 {
10267 gas_assert (is_any_vex_encoding (&i.tm)
e346d50a 10268 || i.encoding != encoding_default);
0ff3b7d0 10269 i.vex.register_specifier = i.op[i.operands - 1].regs;
85b80b0f 10270 }
29b0f896 10271
514a8bb0 10272 if ((i.seg[0] || i.prefix[SEG_PREFIX])
7fc69528 10273 && i.tm.mnem_off == MN_lea)
92334ad2
JB
10274 {
10275 if (!quiet_warnings)
76d3f746 10276 as_warn (_("segment override on `%s' is ineffectual"), insn_name (&i.tm));
739d7649 10277 if (optimize && !i.no_optimize)
92334ad2
JB
10278 {
10279 i.seg[0] = NULL;
10280 i.prefix[SEG_PREFIX] = 0;
10281 }
10282 }
52271982
AM
10283
10284 /* If a segment was explicitly specified, and the specified segment
b6773884
JB
10285 is neither the default nor the one already recorded from a prefix,
10286 use an opcode prefix to select it. If we never figured out what
10287 the default segment is, then default_seg will be zero at this
10288 point, and the specified segment prefix will always be used. */
10289 if (i.seg[0]
10290 && i.seg[0] != default_seg
5e042380 10291 && i386_seg_prefixes[i.seg[0]->reg_num] != i.prefix[SEG_PREFIX])
29b0f896 10292 {
5e042380 10293 if (!add_prefix (i386_seg_prefixes[i.seg[0]->reg_num]))
29b0f896
AM
10294 return 0;
10295 }
10296 return 1;
10297}
10298
5e042380 10299static const reg_entry *
e3bb37b5 10300build_modrm_byte (void)
29b0f896 10301{
5e042380 10302 const reg_entry *default_seg = NULL;
ecb96e55
JB
10303 unsigned int source = i.imm_operands - i.tm.opcode_modifier.immext
10304 /* Compensate for kludge in md_assemble(). */
10305 + i.tm.operand_types[0].bitfield.imm1;
10306 unsigned int dest = i.operands - 1 - i.tm.opcode_modifier.immext;
10307 unsigned int v, op, reg_slot = ~0;
10308
10309 /* Accumulator (in particular %st), shift count (%cl), and alike need
10310 to be skipped just like immediate operands do. */
10311 if (i.tm.operand_types[source].bitfield.instance)
10312 ++source;
10313 while (i.tm.operand_types[dest].bitfield.instance)
10314 --dest;
10315
10316 for (op = source; op < i.operands; ++op)
10317 if (i.tm.operand_types[op].bitfield.baseindex)
10318 break;
c0f3af97 10319
ecb96e55 10320 if (i.reg_operands + i.mem_operands + (i.tm.extension_opcode != None) == 4)
c0f3af97 10321 {
4c2c6516 10322 expressionS *exp;
c0f3af97 10323
a683cc34 10324 /* There are 2 kinds of instructions:
bed3d976 10325 1. 5 operands: 4 register operands or 3 register operands
9d3bf266 10326 plus 1 memory operand plus one Imm4 operand, VexXDS, and
bed3d976 10327 VexW0 or VexW1. The destination must be either XMM, YMM or
43234a1e 10328 ZMM register.
bed3d976 10329 2. 4 operands: 4 register operands or 3 register operands
0ff3b7d0
JB
10330 plus 1 memory operand, with VexXDS.
10331 3. Other equivalent combinations when coming from s_insn(). */
eea96d3f 10332 gas_assert (i.tm.opcode_modifier.vexvvvv
0ff3b7d0
JB
10333 && i.tm.opcode_modifier.vexw);
10334 gas_assert (dot_insn ()
10335 || i.tm.operand_types[dest].bitfield.class == RegSIMD);
a683cc34 10336
ecb96e55
JB
10337 /* Of the first two non-immediate operands the one with the template
10338 not allowing for a memory one is encoded in the immediate operand. */
10339 if (source == op)
10340 reg_slot = source + 1;
48db9223 10341 else
ecb96e55 10342 reg_slot = source++;
48db9223 10343
0ff3b7d0
JB
10344 if (!dot_insn ())
10345 {
10346 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
10347 gas_assert (!(i.op[reg_slot].regs->reg_flags & RegVRex));
10348 }
10349 else
10350 gas_assert (i.tm.operand_types[reg_slot].bitfield.class != ClassNone);
10351
a683cc34 10352 if (i.imm_operands == 0)
bed3d976
JB
10353 {
10354 /* When there is no immediate operand, generate an 8bit
10355 immediate operand to encode the first operand. */
10356 exp = &im_expressions[i.imm_operands++];
10357 i.op[i.operands].imms = exp;
be1643ff 10358 i.types[i.operands].bitfield.imm8 = 1;
bed3d976
JB
10359 i.operands++;
10360
bed3d976 10361 exp->X_op = O_constant;
43234a1e 10362 }
922d8de8 10363 else
bed3d976 10364 {
9d3bf266
JB
10365 gas_assert (i.imm_operands == 1);
10366 gas_assert (fits_in_imm4 (i.op[0].imms->X_add_number));
10367 gas_assert (!i.tm.opcode_modifier.immext);
a683cc34 10368
9d3bf266
JB
10369 /* Turn on Imm8 again so that output_imm will generate it. */
10370 i.types[0].bitfield.imm8 = 1;
bed3d976 10371
0ff3b7d0 10372 exp = i.op[0].imms;
bed3d976 10373 }
0ff3b7d0 10374 exp->X_add_number |= register_number (i.op[reg_slot].regs)
706ce984 10375 << (3 + !(i.tm.opcode_modifier.evex
e346d50a 10376 || i.encoding == encoding_evex));
c0f3af97 10377 }
f12dc422 10378
3083f376 10379 if (i.tm.opcode_modifier.vexvvvv == VexVVVV_DST)
10380 {
10381 v = dest;
10382 dest-- ;
10383 }
10384 else
10385 {
10386 for (v = source + 1; v < dest; ++v)
10387 if (v != reg_slot)
10388 break;
10389 if (v >= dest)
10390 v = ~0;
10391 }
ecb96e55
JB
10392 if (i.tm.extension_opcode != None)
10393 {
10394 if (dest != source)
10395 v = dest;
10396 dest = ~0;
10397 }
10398 gas_assert (source < dest);
10399 if (i.tm.opcode_modifier.operandconstraint == SWAP_SOURCES
10400 && source != op)
10401 {
10402 unsigned int tmp = source;
f12dc422 10403
ecb96e55
JB
10404 source = v;
10405 v = tmp;
10406 }
29b0f896 10407
ecb96e55
JB
10408 if (v < MAX_OPERANDS)
10409 {
10410 gas_assert (i.tm.opcode_modifier.vexvvvv);
10411 i.vex.register_specifier = i.op[v].regs;
29b0f896 10412 }
c0f3af97 10413
ecb96e55
JB
10414 if (op < i.operands)
10415 {
29b0f896
AM
10416 if (i.mem_operands)
10417 {
10418 unsigned int fake_zero_displacement = 0;
4eed87de 10419
ecb96e55 10420 gas_assert (i.flags[op] & Operand_Mem);
29b0f896 10421
63112cd6 10422 if (i.tm.opcode_modifier.sib)
6c30d220 10423 {
260cd341
LC
10424 /* The index register of VSIB shouldn't be RegIZ. */
10425 if (i.tm.opcode_modifier.sib != SIBMEM
10426 && i.index_reg->reg_num == RegIZ)
6c30d220
L
10427 abort ();
10428
10429 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
10430 if (!i.base_reg)
10431 {
10432 i.sib.base = NO_BASE_REGISTER;
10433 i.sib.scale = i.log2_scale_factor;
2f2be86b 10434 i.types[op] = operand_type_and_not (i.types[op], anydisp);
a775efc8 10435 i.types[op].bitfield.disp32 = 1;
6c30d220 10436 }
260cd341
LC
10437
10438 /* Since the mandatory SIB always has index register, so
10439 the code logic remains unchanged. The non-mandatory SIB
10440 without index register is allowed and will be handled
10441 later. */
10442 if (i.index_reg)
10443 {
10444 if (i.index_reg->reg_num == RegIZ)
10445 i.sib.index = NO_INDEX_REGISTER;
10446 else
10447 i.sib.index = i.index_reg->reg_num;
5b7c81bd 10448 set_rex_vrex (i.index_reg, REX_X, false);
260cd341 10449 }
6c30d220
L
10450 }
10451
5e042380 10452 default_seg = reg_ds;
29b0f896
AM
10453
10454 if (i.base_reg == 0)
10455 {
10456 i.rm.mode = 0;
10457 if (!i.disp_operands)
9bb129e8 10458 fake_zero_displacement = 1;
29b0f896
AM
10459 if (i.index_reg == 0)
10460 {
260cd341
LC
10461 /* Both check for VSIB and mandatory non-vector SIB. */
10462 gas_assert (!i.tm.opcode_modifier.sib
10463 || i.tm.opcode_modifier.sib == SIBMEM);
29b0f896 10464 /* Operand is just <disp> */
2f2be86b 10465 i.types[op] = operand_type_and_not (i.types[op], anydisp);
20f0a1fc 10466 if (flag_code == CODE_64BIT)
29b0f896
AM
10467 {
10468 /* 64bit mode overwrites the 32bit absolute
10469 addressing by RIP relative addressing and
10470 absolute addressing is encoded by one of the
10471 redundant SIB forms. */
10472 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
10473 i.sib.base = NO_BASE_REGISTER;
10474 i.sib.index = NO_INDEX_REGISTER;
a775efc8 10475 i.types[op].bitfield.disp32 = 1;
20f0a1fc 10476 }
fc225355
L
10477 else if ((flag_code == CODE_16BIT)
10478 ^ (i.prefix[ADDR_PREFIX] != 0))
20f0a1fc
NC
10479 {
10480 i.rm.regmem = NO_BASE_REGISTER_16;
2f2be86b 10481 i.types[op].bitfield.disp16 = 1;
20f0a1fc
NC
10482 }
10483 else
10484 {
10485 i.rm.regmem = NO_BASE_REGISTER;
2f2be86b 10486 i.types[op].bitfield.disp32 = 1;
29b0f896
AM
10487 }
10488 }
63112cd6 10489 else if (!i.tm.opcode_modifier.sib)
29b0f896 10490 {
6c30d220 10491 /* !i.base_reg && i.index_reg */
e968fc9b 10492 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
10493 i.sib.index = NO_INDEX_REGISTER;
10494 else
10495 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
10496 i.sib.base = NO_BASE_REGISTER;
10497 i.sib.scale = i.log2_scale_factor;
10498 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
2f2be86b 10499 i.types[op] = operand_type_and_not (i.types[op], anydisp);
a775efc8 10500 i.types[op].bitfield.disp32 = 1;
80d61d8d 10501 set_rex_rex2 (i.index_reg, REX_X);
29b0f896
AM
10502 }
10503 }
10504 /* RIP addressing for 64bit mode. */
e968fc9b 10505 else if (i.base_reg->reg_num == RegIP)
29b0f896 10506 {
63112cd6 10507 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896 10508 i.rm.regmem = NO_BASE_REGISTER;
40fb9820
L
10509 i.types[op].bitfield.disp8 = 0;
10510 i.types[op].bitfield.disp16 = 0;
a775efc8 10511 i.types[op].bitfield.disp32 = 1;
40fb9820 10512 i.types[op].bitfield.disp64 = 0;
71903a11 10513 i.flags[op] |= Operand_PCrel;
20f0a1fc
NC
10514 if (! i.disp_operands)
10515 fake_zero_displacement = 1;
29b0f896 10516 }
dc821c5f 10517 else if (i.base_reg->reg_type.bitfield.word)
29b0f896 10518 {
63112cd6 10519 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896
AM
10520 switch (i.base_reg->reg_num)
10521 {
10522 case 3: /* (%bx) */
10523 if (i.index_reg == 0)
10524 i.rm.regmem = 7;
10525 else /* (%bx,%si) -> 0, or (%bx,%di) -> 1 */
10526 i.rm.regmem = i.index_reg->reg_num - 6;
10527 break;
10528 case 5: /* (%bp) */
5e042380 10529 default_seg = reg_ss;
29b0f896
AM
10530 if (i.index_reg == 0)
10531 {
10532 i.rm.regmem = 6;
40fb9820 10533 if (operand_type_check (i.types[op], disp) == 0)
29b0f896
AM
10534 {
10535 /* fake (%bp) into 0(%bp) */
41eb8e88 10536 if (i.disp_encoding == disp_encoding_16bit)
1a02d6b0
L
10537 i.types[op].bitfield.disp16 = 1;
10538 else
10539 i.types[op].bitfield.disp8 = 1;
252b5132 10540 fake_zero_displacement = 1;
29b0f896
AM
10541 }
10542 }
10543 else /* (%bp,%si) -> 2, or (%bp,%di) -> 3 */
10544 i.rm.regmem = i.index_reg->reg_num - 6 + 2;
10545 break;
10546 default: /* (%si) -> 4 or (%di) -> 5 */
10547 i.rm.regmem = i.base_reg->reg_num - 6 + 4;
10548 }
41eb8e88
L
10549 if (!fake_zero_displacement
10550 && !i.disp_operands
10551 && i.disp_encoding)
10552 {
10553 fake_zero_displacement = 1;
10554 if (i.disp_encoding == disp_encoding_8bit)
10555 i.types[op].bitfield.disp8 = 1;
10556 else
10557 i.types[op].bitfield.disp16 = 1;
10558 }
29b0f896
AM
10559 i.rm.mode = mode_from_disp_size (i.types[op]);
10560 }
10561 else /* i.base_reg and 32/64 bit mode */
10562 {
a9aabc23 10563 if (operand_type_check (i.types[op], disp))
40fb9820 10564 {
73053c1f
JB
10565 i.types[op].bitfield.disp16 = 0;
10566 i.types[op].bitfield.disp64 = 0;
a775efc8 10567 i.types[op].bitfield.disp32 = 1;
40fb9820 10568 }
20f0a1fc 10569
63112cd6 10570 if (!i.tm.opcode_modifier.sib)
6c30d220 10571 i.rm.regmem = i.base_reg->reg_num;
80d61d8d 10572 set_rex_rex2 (i.base_reg, REX_B);
29b0f896
AM
10573 i.sib.base = i.base_reg->reg_num;
10574 /* x86-64 ignores REX prefix bit here to avoid decoder
10575 complications. */
848930b2
JB
10576 if (!(i.base_reg->reg_flags & RegRex)
10577 && (i.base_reg->reg_num == EBP_REG_NUM
10578 || i.base_reg->reg_num == ESP_REG_NUM))
5e042380 10579 default_seg = reg_ss;
848930b2 10580 if (i.base_reg->reg_num == 5 && i.disp_operands == 0)
29b0f896 10581 {
848930b2 10582 fake_zero_displacement = 1;
1a02d6b0
L
10583 if (i.disp_encoding == disp_encoding_32bit)
10584 i.types[op].bitfield.disp32 = 1;
10585 else
10586 i.types[op].bitfield.disp8 = 1;
29b0f896
AM
10587 }
10588 i.sib.scale = i.log2_scale_factor;
10589 if (i.index_reg == 0)
10590 {
260cd341
LC
10591 /* Only check for VSIB. */
10592 gas_assert (i.tm.opcode_modifier.sib != VECSIB128
10593 && i.tm.opcode_modifier.sib != VECSIB256
10594 && i.tm.opcode_modifier.sib != VECSIB512);
10595
29b0f896
AM
10596 /* <disp>(%esp) becomes two byte modrm with no index
10597 register. We've already stored the code for esp
10598 in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
10599 Any base register besides %esp will not use the
10600 extra modrm byte. */
10601 i.sib.index = NO_INDEX_REGISTER;
29b0f896 10602 }
63112cd6 10603 else if (!i.tm.opcode_modifier.sib)
29b0f896 10604 {
e968fc9b 10605 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
10606 i.sib.index = NO_INDEX_REGISTER;
10607 else
10608 i.sib.index = i.index_reg->reg_num;
29b0f896 10609 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
80d61d8d 10610 set_rex_rex2 (i.index_reg, REX_X);
29b0f896 10611 }
67a4f2b7
AO
10612
10613 if (i.disp_operands
10614 && (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
10615 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL))
10616 i.rm.mode = 0;
10617 else
a501d77e
L
10618 {
10619 if (!fake_zero_displacement
10620 && !i.disp_operands
10621 && i.disp_encoding)
10622 {
10623 fake_zero_displacement = 1;
10624 if (i.disp_encoding == disp_encoding_8bit)
10625 i.types[op].bitfield.disp8 = 1;
10626 else
10627 i.types[op].bitfield.disp32 = 1;
10628 }
10629 i.rm.mode = mode_from_disp_size (i.types[op]);
10630 }
29b0f896 10631 }
252b5132 10632
29b0f896
AM
10633 if (fake_zero_displacement)
10634 {
10635 /* Fakes a zero displacement assuming that i.types[op]
10636 holds the correct displacement size. */
10637 expressionS *exp;
10638
9c2799c2 10639 gas_assert (i.op[op].disps == 0);
29b0f896
AM
10640 exp = &disp_expressions[i.disp_operands++];
10641 i.op[op].disps = exp;
10642 exp->X_op = O_constant;
10643 exp->X_add_number = 0;
10644 exp->X_add_symbol = (symbolS *) 0;
10645 exp->X_op_symbol = (symbolS *) 0;
10646 }
10647 }
ecb96e55
JB
10648 else
10649 {
10650 i.rm.mode = 3;
10651 i.rm.regmem = i.op[op].regs->reg_num;
10652 set_rex_vrex (i.op[op].regs, REX_B, false);
10653 }
252b5132 10654
ecb96e55
JB
10655 if (op == dest)
10656 dest = ~0;
10657 if (op == source)
10658 source = ~0;
10659 }
10660 else
10661 {
10662 i.rm.mode = 3;
10663 if (!i.tm.opcode_modifier.regmem)
f88c9eb0 10664 {
ecb96e55
JB
10665 gas_assert (source < MAX_OPERANDS);
10666 i.rm.regmem = i.op[source].regs->reg_num;
10667 set_rex_vrex (i.op[source].regs, REX_B,
10668 dest >= MAX_OPERANDS && i.tm.opcode_modifier.sse2avx);
10669 source = ~0;
f88c9eb0 10670 }
ecb96e55 10671 else
29b0f896 10672 {
ecb96e55
JB
10673 gas_assert (dest < MAX_OPERANDS);
10674 i.rm.regmem = i.op[dest].regs->reg_num;
10675 set_rex_vrex (i.op[dest].regs, REX_B, i.tm.opcode_modifier.sse2avx);
10676 dest = ~0;
29b0f896 10677 }
ecb96e55 10678 }
252b5132 10679
ecb96e55
JB
10680 /* Fill in i.rm.reg field with extension opcode (if any) or the
10681 appropriate register. */
10682 if (i.tm.extension_opcode != None)
10683 i.rm.reg = i.tm.extension_opcode;
10684 else if (!i.tm.opcode_modifier.regmem && dest < MAX_OPERANDS)
10685 {
10686 i.rm.reg = i.op[dest].regs->reg_num;
10687 set_rex_vrex (i.op[dest].regs, REX_R, i.tm.opcode_modifier.sse2avx);
10688 }
10689 else
10690 {
10691 gas_assert (source < MAX_OPERANDS);
10692 i.rm.reg = i.op[source].regs->reg_num;
10693 set_rex_vrex (i.op[source].regs, REX_R, false);
29b0f896 10694 }
ecb96e55
JB
10695
10696 if (flag_code != CODE_64BIT && (i.rex & REX_R))
10697 {
10698 gas_assert (i.types[!i.tm.opcode_modifier.regmem].bitfield.class == RegCR);
10699 i.rex &= ~REX_R;
10700 add_prefix (LOCK_PREFIX_OPCODE);
10701 }
10702
29b0f896
AM
10703 return default_seg;
10704}
252b5132 10705
48ef937e
JB
10706static INLINE void
10707frag_opcode_byte (unsigned char byte)
10708{
10709 if (now_seg != absolute_section)
10710 FRAG_APPEND_1_CHAR (byte);
10711 else
10712 ++abs_section_offset;
10713}
10714
376cd056
JB
10715static unsigned int
10716flip_code16 (unsigned int code16)
10717{
10718 gas_assert (i.tm.operands == 1);
10719
10720 return !(i.prefix[REX_PREFIX] & REX_W)
10721 && (code16 ? i.tm.operand_types[0].bitfield.disp32
376cd056
JB
10722 : i.tm.operand_types[0].bitfield.disp16)
10723 ? CODE16 : 0;
10724}
10725
29b0f896 10726static void
e3bb37b5 10727output_branch (void)
29b0f896
AM
10728{
10729 char *p;
f8a5c266 10730 int size;
29b0f896
AM
10731 int code16;
10732 int prefix;
10733 relax_substateT subtype;
10734 symbolS *sym;
10735 offsetT off;
10736
48ef937e
JB
10737 if (now_seg == absolute_section)
10738 {
10739 as_bad (_("relaxable branches not supported in absolute section"));
10740 return;
10741 }
10742
f8a5c266 10743 code16 = flag_code == CODE_16BIT ? CODE16 : 0;
1a42a9fe 10744 size = i.disp_encoding > disp_encoding_8bit ? BIG : SMALL;
29b0f896
AM
10745
10746 prefix = 0;
10747 if (i.prefix[DATA_PREFIX] != 0)
252b5132 10748 {
29b0f896
AM
10749 prefix = 1;
10750 i.prefixes -= 1;
376cd056 10751 code16 ^= flip_code16(code16);
252b5132 10752 }
29b0f896
AM
10753 /* Pentium4 branch hints. */
10754 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
10755 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
2f66722d 10756 {
29b0f896
AM
10757 prefix++;
10758 i.prefixes--;
10759 }
10760 if (i.prefix[REX_PREFIX] != 0)
10761 {
10762 prefix++;
10763 i.prefixes--;
2f66722d
AM
10764 }
10765
7e8b059b
L
10766 /* BND prefixed jump. */
10767 if (i.prefix[BND_PREFIX] != 0)
10768 {
6cb0a70e
JB
10769 prefix++;
10770 i.prefixes--;
7e8b059b
L
10771 }
10772
f2810fe0 10773 if (i.prefixes != 0)
76d3f746 10774 as_warn (_("skipping prefixes on `%s'"), insn_name (&i.tm));
29b0f896
AM
10775
10776 /* It's always a symbol; End frag & setup for relax.
10777 Make sure there is enough room in this frag for the largest
10778 instruction we may generate in md_convert_frag. This is 2
10779 bytes for the opcode and room for the prefix and largest
10780 displacement. */
10781 frag_grow (prefix + 2 + 4);
10782 /* Prefix and 1 opcode byte go in fr_fix. */
10783 p = frag_more (prefix + 1);
10784 if (i.prefix[DATA_PREFIX] != 0)
10785 *p++ = DATA_PREFIX_OPCODE;
10786 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
10787 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
10788 *p++ = i.prefix[SEG_PREFIX];
6cb0a70e
JB
10789 if (i.prefix[BND_PREFIX] != 0)
10790 *p++ = BND_PREFIX_OPCODE;
29b0f896
AM
10791 if (i.prefix[REX_PREFIX] != 0)
10792 *p++ = i.prefix[REX_PREFIX];
10793 *p = i.tm.base_opcode;
10794
10795 if ((unsigned char) *p == JUMP_PC_RELATIVE)
f8a5c266 10796 subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, size);
40fb9820 10797 else if (cpu_arch_flags.bitfield.cpui386)
f8a5c266 10798 subtype = ENCODE_RELAX_STATE (COND_JUMP, size);
29b0f896 10799 else
f8a5c266 10800 subtype = ENCODE_RELAX_STATE (COND_JUMP86, size);
29b0f896 10801 subtype |= code16;
3e73aa7c 10802
29b0f896
AM
10803 sym = i.op[0].disps->X_add_symbol;
10804 off = i.op[0].disps->X_add_number;
3e73aa7c 10805
29b0f896
AM
10806 if (i.op[0].disps->X_op != O_constant
10807 && i.op[0].disps->X_op != O_symbol)
3e73aa7c 10808 {
29b0f896
AM
10809 /* Handle complex expressions. */
10810 sym = make_expr_symbol (i.op[0].disps);
10811 off = 0;
10812 }
3e73aa7c 10813
29b0f896
AM
10814 /* 1 possible extra opcode + 4 byte displacement go in var part.
10815 Pass reloc in fr_var. */
d258b828 10816 frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
29b0f896 10817}
3e73aa7c 10818
bd7ab16b
L
10819#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
10820/* Return TRUE iff PLT32 relocation should be used for branching to
10821 symbol S. */
10822
5b7c81bd 10823static bool
bd7ab16b
L
10824need_plt32_p (symbolS *s)
10825{
10826 /* PLT32 relocation is ELF only. */
10827 if (!IS_ELF)
5b7c81bd 10828 return false;
bd7ab16b 10829
a5def729
RO
10830#ifdef TE_SOLARIS
10831 /* Don't emit PLT32 relocation on Solaris: neither native linker nor
10832 krtld support it. */
5b7c81bd 10833 return false;
a5def729
RO
10834#endif
10835
bd7ab16b
L
10836 /* Since there is no need to prepare for PLT branch on x86-64, we
10837 can generate R_X86_64_PLT32, instead of R_X86_64_PC32, which can
10838 be used as a marker for 32-bit PC-relative branches. */
10839 if (!object_64bit)
5b7c81bd 10840 return false;
bd7ab16b 10841
44365e88 10842 if (s == NULL)
5b7c81bd 10843 return false;
44365e88 10844
bd7ab16b
L
10845 /* Weak or undefined symbol need PLT32 relocation. */
10846 if (S_IS_WEAK (s) || !S_IS_DEFINED (s))
5b7c81bd 10847 return true;
bd7ab16b
L
10848
10849 /* Non-global symbol doesn't need PLT32 relocation. */
10850 if (! S_IS_EXTERNAL (s))
5b7c81bd 10851 return false;
bd7ab16b
L
10852
10853 /* Other global symbols need PLT32 relocation. NB: Symbol with
10854 non-default visibilities are treated as normal global symbol
10855 so that PLT32 relocation can be used as a marker for 32-bit
10856 PC-relative branches. It is useful for linker relaxation. */
5b7c81bd 10857 return true;
bd7ab16b
L
10858}
10859#endif
10860
29b0f896 10861static void
e3bb37b5 10862output_jump (void)
29b0f896
AM
10863{
10864 char *p;
10865 int size;
3e02c1cc 10866 fixS *fixP;
bd7ab16b 10867 bfd_reloc_code_real_type jump_reloc = i.reloc[0];
29b0f896 10868
0cfa3eb3 10869 if (i.tm.opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
10870 {
10871 /* This is a loop or jecxz type instruction. */
10872 size = 1;
10873 if (i.prefix[ADDR_PREFIX] != 0)
10874 {
48ef937e 10875 frag_opcode_byte (ADDR_PREFIX_OPCODE);
29b0f896
AM
10876 i.prefixes -= 1;
10877 }
10878 /* Pentium4 branch hints. */
10879 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
10880 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
10881 {
48ef937e 10882 frag_opcode_byte (i.prefix[SEG_PREFIX]);
29b0f896 10883 i.prefixes--;
3e73aa7c
JH
10884 }
10885 }
29b0f896
AM
10886 else
10887 {
10888 int code16;
3e73aa7c 10889
29b0f896
AM
10890 code16 = 0;
10891 if (flag_code == CODE_16BIT)
10892 code16 = CODE16;
3e73aa7c 10893
29b0f896
AM
10894 if (i.prefix[DATA_PREFIX] != 0)
10895 {
48ef937e 10896 frag_opcode_byte (DATA_PREFIX_OPCODE);
29b0f896 10897 i.prefixes -= 1;
376cd056 10898 code16 ^= flip_code16(code16);
29b0f896 10899 }
252b5132 10900
29b0f896
AM
10901 size = 4;
10902 if (code16)
10903 size = 2;
10904 }
9fcc94b6 10905
6cb0a70e
JB
10906 /* BND prefixed jump. */
10907 if (i.prefix[BND_PREFIX] != 0)
29b0f896 10908 {
48ef937e 10909 frag_opcode_byte (i.prefix[BND_PREFIX]);
29b0f896
AM
10910 i.prefixes -= 1;
10911 }
252b5132 10912
6cb0a70e 10913 if (i.prefix[REX_PREFIX] != 0)
7e8b059b 10914 {
48ef937e 10915 frag_opcode_byte (i.prefix[REX_PREFIX]);
7e8b059b
L
10916 i.prefixes -= 1;
10917 }
10918
f2810fe0 10919 if (i.prefixes != 0)
76d3f746 10920 as_warn (_("skipping prefixes on `%s'"), insn_name (&i.tm));
e0890092 10921
48ef937e
JB
10922 if (now_seg == absolute_section)
10923 {
9a182d04 10924 abs_section_offset += i.opcode_length + size;
48ef937e
JB
10925 return;
10926 }
10927
9a182d04
JB
10928 p = frag_more (i.opcode_length + size);
10929 switch (i.opcode_length)
42164a71
L
10930 {
10931 case 2:
10932 *p++ = i.tm.base_opcode >> 8;
1a0670f3 10933 /* Fall through. */
42164a71
L
10934 case 1:
10935 *p++ = i.tm.base_opcode;
10936 break;
10937 default:
10938 abort ();
10939 }
e0890092 10940
bd7ab16b 10941#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1ef3994a
JB
10942 if (flag_code == CODE_64BIT && size == 4
10943 && jump_reloc == NO_RELOC && i.op[0].disps->X_add_number == 0
bd7ab16b
L
10944 && need_plt32_p (i.op[0].disps->X_add_symbol))
10945 jump_reloc = BFD_RELOC_X86_64_PLT32;
10946#endif
10947
10948 jump_reloc = reloc (size, 1, 1, jump_reloc);
10949
3e02c1cc 10950 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
bd7ab16b 10951 i.op[0].disps, 1, jump_reloc);
3e02c1cc 10952
eb19308f
JB
10953 /* All jumps handled here are signed, but don't unconditionally use a
10954 signed limit check for 32 and 16 bit jumps as we want to allow wrap
10955 around at 4G (outside of 64-bit mode) and 64k (except for XBEGIN)
10956 respectively. */
10957 switch (size)
10958 {
10959 case 1:
10960 fixP->fx_signed = 1;
10961 break;
10962
10963 case 2:
7fc69528 10964 if (i.tm.mnem_off == MN_xbegin)
eb19308f
JB
10965 fixP->fx_signed = 1;
10966 break;
10967
10968 case 4:
10969 if (flag_code == CODE_64BIT)
10970 fixP->fx_signed = 1;
10971 break;
10972 }
29b0f896 10973}
e0890092 10974
29b0f896 10975static void
e3bb37b5 10976output_interseg_jump (void)
29b0f896
AM
10977{
10978 char *p;
10979 int size;
10980 int prefix;
10981 int code16;
252b5132 10982
29b0f896
AM
10983 code16 = 0;
10984 if (flag_code == CODE_16BIT)
10985 code16 = CODE16;
a217f122 10986
29b0f896
AM
10987 prefix = 0;
10988 if (i.prefix[DATA_PREFIX] != 0)
10989 {
10990 prefix = 1;
10991 i.prefixes -= 1;
10992 code16 ^= CODE16;
10993 }
6cb0a70e
JB
10994
10995 gas_assert (!i.prefix[REX_PREFIX]);
252b5132 10996
29b0f896
AM
10997 size = 4;
10998 if (code16)
10999 size = 2;
252b5132 11000
f2810fe0 11001 if (i.prefixes != 0)
76d3f746 11002 as_warn (_("skipping prefixes on `%s'"), insn_name (&i.tm));
252b5132 11003
48ef937e
JB
11004 if (now_seg == absolute_section)
11005 {
11006 abs_section_offset += prefix + 1 + 2 + size;
11007 return;
11008 }
11009
29b0f896
AM
11010 /* 1 opcode; 2 segment; offset */
11011 p = frag_more (prefix + 1 + 2 + size);
3e73aa7c 11012
29b0f896
AM
11013 if (i.prefix[DATA_PREFIX] != 0)
11014 *p++ = DATA_PREFIX_OPCODE;
252b5132 11015
29b0f896
AM
11016 if (i.prefix[REX_PREFIX] != 0)
11017 *p++ = i.prefix[REX_PREFIX];
252b5132 11018
29b0f896
AM
11019 *p++ = i.tm.base_opcode;
11020 if (i.op[1].imms->X_op == O_constant)
11021 {
11022 offsetT n = i.op[1].imms->X_add_number;
252b5132 11023
29b0f896
AM
11024 if (size == 2
11025 && !fits_in_unsigned_word (n)
11026 && !fits_in_signed_word (n))
11027 {
11028 as_bad (_("16-bit jump out of range"));
11029 return;
11030 }
11031 md_number_to_chars (p, n, size);
11032 }
11033 else
11034 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
d258b828 11035 i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
6d96a594
C
11036
11037 p += size;
11038 if (i.op[0].imms->X_op == O_constant)
11039 md_number_to_chars (p, (valueT) i.op[0].imms->X_add_number, 2);
11040 else
11041 fix_new_exp (frag_now, p - frag_now->fr_literal, 2,
11042 i.op[0].imms, 0, reloc (2, 0, 0, i.reloc[0]));
29b0f896 11043}
a217f122 11044
b4a3a7b4
L
11045#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
11046void
11047x86_cleanup (void)
11048{
11049 char *p;
11050 asection *seg = now_seg;
11051 subsegT subseg = now_subseg;
11052 asection *sec;
11053 unsigned int alignment, align_size_1;
11054 unsigned int isa_1_descsz, feature_2_descsz, descsz;
11055 unsigned int isa_1_descsz_raw, feature_2_descsz_raw;
11056 unsigned int padding;
11057
1273b2f8 11058 if (!IS_ELF || !x86_used_note)
b4a3a7b4
L
11059 return;
11060
b4a3a7b4
L
11061 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X86;
11062
11063 /* The .note.gnu.property section layout:
11064
11065 Field Length Contents
11066 ---- ---- ----
11067 n_namsz 4 4
11068 n_descsz 4 The note descriptor size
11069 n_type 4 NT_GNU_PROPERTY_TYPE_0
11070 n_name 4 "GNU"
11071 n_desc n_descsz The program property array
11072 .... .... ....
11073 */
11074
11075 /* Create the .note.gnu.property section. */
11076 sec = subseg_new (NOTE_GNU_PROPERTY_SECTION_NAME, 0);
fd361982 11077 bfd_set_section_flags (sec,
b4a3a7b4
L
11078 (SEC_ALLOC
11079 | SEC_LOAD
11080 | SEC_DATA
11081 | SEC_HAS_CONTENTS
11082 | SEC_READONLY));
11083
11084 if (get_elf_backend_data (stdoutput)->s->elfclass == ELFCLASS64)
11085 {
11086 align_size_1 = 7;
11087 alignment = 3;
11088 }
11089 else
11090 {
11091 align_size_1 = 3;
11092 alignment = 2;
11093 }
11094
fd361982 11095 bfd_set_section_alignment (sec, alignment);
b4a3a7b4
L
11096 elf_section_type (sec) = SHT_NOTE;
11097
1273b2f8
L
11098 /* GNU_PROPERTY_X86_ISA_1_USED: 4-byte type + 4-byte data size
11099 + 4-byte data */
11100 isa_1_descsz_raw = 4 + 4 + 4;
11101 /* Align GNU_PROPERTY_X86_ISA_1_USED. */
11102 isa_1_descsz = (isa_1_descsz_raw + align_size_1) & ~align_size_1;
b4a3a7b4
L
11103
11104 feature_2_descsz_raw = isa_1_descsz;
11105 /* GNU_PROPERTY_X86_FEATURE_2_USED: 4-byte type + 4-byte data size
1273b2f8 11106 + 4-byte data */
b4a3a7b4
L
11107 feature_2_descsz_raw += 4 + 4 + 4;
11108 /* Align GNU_PROPERTY_X86_FEATURE_2_USED. */
11109 feature_2_descsz = ((feature_2_descsz_raw + align_size_1)
11110 & ~align_size_1);
11111
11112 descsz = feature_2_descsz;
11113 /* Section size: n_namsz + n_descsz + n_type + n_name + n_descsz. */
11114 p = frag_more (4 + 4 + 4 + 4 + descsz);
11115
11116 /* Write n_namsz. */
11117 md_number_to_chars (p, (valueT) 4, 4);
11118
11119 /* Write n_descsz. */
11120 md_number_to_chars (p + 4, (valueT) descsz, 4);
11121
11122 /* Write n_type. */
11123 md_number_to_chars (p + 4 * 2, (valueT) NT_GNU_PROPERTY_TYPE_0, 4);
11124
11125 /* Write n_name. */
11126 memcpy (p + 4 * 3, "GNU", 4);
11127
1273b2f8
L
11128 /* Write 4-byte type. */
11129 md_number_to_chars (p + 4 * 4,
11130 (valueT) GNU_PROPERTY_X86_ISA_1_USED, 4);
b4a3a7b4 11131
1273b2f8
L
11132 /* Write 4-byte data size. */
11133 md_number_to_chars (p + 4 * 5, (valueT) 4, 4);
b4a3a7b4 11134
1273b2f8
L
11135 /* Write 4-byte data. */
11136 md_number_to_chars (p + 4 * 6, (valueT) x86_isa_1_used, 4);
b4a3a7b4 11137
1273b2f8
L
11138 /* Zero out paddings. */
11139 padding = isa_1_descsz - isa_1_descsz_raw;
11140 if (padding)
11141 memset (p + 4 * 7, 0, padding);
b4a3a7b4
L
11142
11143 /* Write 4-byte type. */
11144 md_number_to_chars (p + isa_1_descsz + 4 * 4,
11145 (valueT) GNU_PROPERTY_X86_FEATURE_2_USED, 4);
11146
11147 /* Write 4-byte data size. */
11148 md_number_to_chars (p + isa_1_descsz + 4 * 5, (valueT) 4, 4);
11149
11150 /* Write 4-byte data. */
11151 md_number_to_chars (p + isa_1_descsz + 4 * 6,
11152 (valueT) x86_feature_2_used, 4);
11153
11154 /* Zero out paddings. */
11155 padding = feature_2_descsz - feature_2_descsz_raw;
11156 if (padding)
11157 memset (p + isa_1_descsz + 4 * 7, 0, padding);
11158
11159 /* We probably can't restore the current segment, for there likely
11160 isn't one yet... */
11161 if (seg && subseg)
11162 subseg_set (seg, subseg);
11163}
b52c4ee4
IB
11164
11165bool
11166x86_support_sframe_p (void)
11167{
3e3e792a 11168 /* At this time, SFrame stack trace is supported for AMD64 ABI only. */
b52c4ee4
IB
11169 return (x86_elf_abi == X86_64_ABI);
11170}
11171
11172bool
11173x86_sframe_ra_tracking_p (void)
11174{
11175 /* In AMD64, return address is always stored on the stack at a fixed offset
11176 from the CFA (provided via x86_sframe_cfa_ra_offset ()).
11177 Do not track explicitly via an SFrame Frame Row Entry. */
11178 return false;
11179}
11180
11181offsetT
11182x86_sframe_cfa_ra_offset (void)
11183{
11184 gas_assert (x86_elf_abi == X86_64_ABI);
11185 return (offsetT) -8;
11186}
11187
11188unsigned char
11189x86_sframe_get_abi_arch (void)
11190{
11191 unsigned char sframe_abi_arch = 0;
11192
11193 if (x86_support_sframe_p ())
11194 {
11195 gas_assert (!target_big_endian);
11196 sframe_abi_arch = SFRAME_ABI_AMD64_ENDIAN_LITTLE;
11197 }
11198
11199 return sframe_abi_arch;
11200}
11201
b4a3a7b4
L
11202#endif
11203
9c33702b
JB
11204static unsigned int
11205encoding_length (const fragS *start_frag, offsetT start_off,
11206 const char *frag_now_ptr)
11207{
11208 unsigned int len = 0;
11209
11210 if (start_frag != frag_now)
11211 {
11212 const fragS *fr = start_frag;
11213
11214 do {
11215 len += fr->fr_fix;
11216 fr = fr->fr_next;
11217 } while (fr && fr != frag_now);
11218 }
11219
11220 return len - start_off + (frag_now_ptr - frag_now->fr_literal);
11221}
11222
e379e5f3 11223/* Return 1 for test, and, cmp, add, sub, inc and dec which may
79d72f45
HL
11224 be macro-fused with conditional jumps.
11225 NB: If TEST/AND/CMP/ADD/SUB/INC/DEC is of RIP relative address,
11226 or is one of the following format:
11227
11228 cmp m, imm
11229 add m, imm
11230 sub m, imm
11231 test m, imm
11232 and m, imm
11233 inc m
11234 dec m
11235
11236 it is unfusible. */
e379e5f3
L
11237
11238static int
79d72f45 11239maybe_fused_with_jcc_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
11240{
11241 /* No RIP address. */
11242 if (i.base_reg && i.base_reg->reg_num == RegIP)
11243 return 0;
11244
389d00a5 11245 /* No opcodes outside of base encoding space. */
ddb62495 11246 if (i.tm.opcode_space != SPACE_BASE)
e379e5f3
L
11247 return 0;
11248
79d72f45
HL
11249 /* add, sub without add/sub m, imm. */
11250 if (i.tm.base_opcode <= 5
e379e5f3
L
11251 || (i.tm.base_opcode >= 0x28 && i.tm.base_opcode <= 0x2d)
11252 || ((i.tm.base_opcode | 3) == 0x83
79d72f45 11253 && (i.tm.extension_opcode == 0x5
e379e5f3 11254 || i.tm.extension_opcode == 0x0)))
79d72f45
HL
11255 {
11256 *mf_cmp_p = mf_cmp_alu_cmp;
11257 return !(i.mem_operands && i.imm_operands);
11258 }
e379e5f3 11259
79d72f45
HL
11260 /* and without and m, imm. */
11261 if ((i.tm.base_opcode >= 0x20 && i.tm.base_opcode <= 0x25)
11262 || ((i.tm.base_opcode | 3) == 0x83
11263 && i.tm.extension_opcode == 0x4))
11264 {
11265 *mf_cmp_p = mf_cmp_test_and;
11266 return !(i.mem_operands && i.imm_operands);
11267 }
11268
11269 /* test without test m imm. */
e379e5f3
L
11270 if ((i.tm.base_opcode | 1) == 0x85
11271 || (i.tm.base_opcode | 1) == 0xa9
11272 || ((i.tm.base_opcode | 1) == 0xf7
79d72f45
HL
11273 && i.tm.extension_opcode == 0))
11274 {
11275 *mf_cmp_p = mf_cmp_test_and;
11276 return !(i.mem_operands && i.imm_operands);
11277 }
11278
11279 /* cmp without cmp m, imm. */
11280 if ((i.tm.base_opcode >= 0x38 && i.tm.base_opcode <= 0x3d)
e379e5f3
L
11281 || ((i.tm.base_opcode | 3) == 0x83
11282 && (i.tm.extension_opcode == 0x7)))
79d72f45
HL
11283 {
11284 *mf_cmp_p = mf_cmp_alu_cmp;
11285 return !(i.mem_operands && i.imm_operands);
11286 }
e379e5f3 11287
79d72f45 11288 /* inc, dec without inc/dec m. */
734dfd1c 11289 if ((is_cpu (&i.tm, CpuNo64)
e379e5f3
L
11290 && (i.tm.base_opcode | 0xf) == 0x4f)
11291 || ((i.tm.base_opcode | 1) == 0xff
11292 && i.tm.extension_opcode <= 0x1))
79d72f45
HL
11293 {
11294 *mf_cmp_p = mf_cmp_incdec;
11295 return !i.mem_operands;
11296 }
e379e5f3
L
11297
11298 return 0;
11299}
11300
11301/* Return 1 if a FUSED_JCC_PADDING frag should be generated. */
11302
11303static int
b5482fe5
JB
11304add_fused_jcc_padding_frag_p (enum mf_cmp_kind *mf_cmp_p,
11305 const struct last_insn *last_insn)
e379e5f3
L
11306{
11307 /* NB: Don't work with COND_JUMP86 without i386. */
11308 if (!align_branch_power
11309 || now_seg == absolute_section
11310 || !cpu_arch_flags.bitfield.cpui386
11311 || !(align_branch & align_branch_fused_bit))
11312 return 0;
11313
79d72f45 11314 if (maybe_fused_with_jcc_p (mf_cmp_p))
e379e5f3 11315 {
b5482fe5 11316 if (last_insn->kind == last_insn_other)
e379e5f3
L
11317 return 1;
11318 if (flag_debug)
b5482fe5 11319 as_warn_where (last_insn->file, last_insn->line,
e379e5f3 11320 _("`%s` skips -malign-branch-boundary on `%s`"),
b5482fe5 11321 last_insn->name, insn_name (&i.tm));
e379e5f3
L
11322 }
11323
11324 return 0;
11325}
11326
11327/* Return 1 if a BRANCH_PREFIX frag should be generated. */
11328
11329static int
b5482fe5 11330add_branch_prefix_frag_p (const struct last_insn *last_insn)
e379e5f3
L
11331{
11332 /* NB: Don't work with COND_JUMP86 without i386. Don't add prefix
11333 to PadLock instructions since they include prefixes in opcode. */
11334 if (!align_branch_power
11335 || !align_branch_prefix_size
11336 || now_seg == absolute_section
734dfd1c 11337 || is_cpu (&i.tm, CpuPadLock)
e379e5f3
L
11338 || !cpu_arch_flags.bitfield.cpui386)
11339 return 0;
11340
11341 /* Don't add prefix if it is a prefix or there is no operand in case
11342 that segment prefix is special. */
11343 if (!i.operands || i.tm.opcode_modifier.isprefix)
11344 return 0;
11345
b5482fe5 11346 if (last_insn->kind == last_insn_other)
e379e5f3
L
11347 return 1;
11348
11349 if (flag_debug)
b5482fe5 11350 as_warn_where (last_insn->file, last_insn->line,
e379e5f3 11351 _("`%s` skips -malign-branch-boundary on `%s`"),
b5482fe5 11352 last_insn->name, insn_name (&i.tm));
e379e5f3
L
11353
11354 return 0;
11355}
11356
11357/* Return 1 if a BRANCH_PADDING frag should be generated. */
11358
11359static int
79d72f45 11360add_branch_padding_frag_p (enum align_branch_kind *branch_p,
b5482fe5
JB
11361 enum mf_jcc_kind *mf_jcc_p,
11362 const struct last_insn *last_insn)
e379e5f3
L
11363{
11364 int add_padding;
11365
11366 /* NB: Don't work with COND_JUMP86 without i386. */
11367 if (!align_branch_power
11368 || now_seg == absolute_section
389d00a5 11369 || !cpu_arch_flags.bitfield.cpui386
ddb62495 11370 || i.tm.opcode_space != SPACE_BASE)
e379e5f3
L
11371 return 0;
11372
11373 add_padding = 0;
11374
11375 /* Check for jcc and direct jmp. */
11376 if (i.tm.opcode_modifier.jump == JUMP)
11377 {
11378 if (i.tm.base_opcode == JUMP_PC_RELATIVE)
11379 {
11380 *branch_p = align_branch_jmp;
11381 add_padding = align_branch & align_branch_jmp_bit;
11382 }
11383 else
11384 {
79d72f45
HL
11385 /* Because J<cc> and JN<cc> share same group in macro-fusible table,
11386 igore the lowest bit. */
11387 *mf_jcc_p = (i.tm.base_opcode & 0x0e) >> 1;
e379e5f3
L
11388 *branch_p = align_branch_jcc;
11389 if ((align_branch & align_branch_jcc_bit))
11390 add_padding = 1;
11391 }
11392 }
e379e5f3
L
11393 else if ((i.tm.base_opcode | 1) == 0xc3)
11394 {
11395 /* Near ret. */
11396 *branch_p = align_branch_ret;
11397 if ((align_branch & align_branch_ret_bit))
11398 add_padding = 1;
11399 }
11400 else
11401 {
11402 /* Check for indirect jmp, direct and indirect calls. */
11403 if (i.tm.base_opcode == 0xe8)
11404 {
11405 /* Direct call. */
11406 *branch_p = align_branch_call;
11407 if ((align_branch & align_branch_call_bit))
11408 add_padding = 1;
11409 }
11410 else if (i.tm.base_opcode == 0xff
11411 && (i.tm.extension_opcode == 2
11412 || i.tm.extension_opcode == 4))
11413 {
11414 /* Indirect call and jmp. */
11415 *branch_p = align_branch_indirect;
11416 if ((align_branch & align_branch_indirect_bit))
11417 add_padding = 1;
11418 }
11419
11420 if (add_padding
11421 && i.disp_operands
11422 && tls_get_addr
11423 && (i.op[0].disps->X_op == O_symbol
11424 || (i.op[0].disps->X_op == O_subtract
11425 && i.op[0].disps->X_op_symbol == GOT_symbol)))
11426 {
11427 symbolS *s = i.op[0].disps->X_add_symbol;
11428 /* No padding to call to global or undefined tls_get_addr. */
11429 if ((S_IS_EXTERNAL (s) || !S_IS_DEFINED (s))
11430 && strcmp (S_GET_NAME (s), tls_get_addr) == 0)
11431 return 0;
11432 }
11433 }
11434
11435 if (add_padding
b5482fe5 11436 && last_insn->kind != last_insn_other)
e379e5f3
L
11437 {
11438 if (flag_debug)
b5482fe5 11439 as_warn_where (last_insn->file, last_insn->line,
e379e5f3 11440 _("`%s` skips -malign-branch-boundary on `%s`"),
b5482fe5 11441 last_insn->name, insn_name (&i.tm));
e379e5f3
L
11442 return 0;
11443 }
11444
11445 return add_padding;
11446}
11447
29b0f896 11448static void
b5482fe5 11449output_insn (const struct last_insn *last_insn)
29b0f896 11450{
2bbd9c25
JJ
11451 fragS *insn_start_frag;
11452 offsetT insn_start_off;
e379e5f3
L
11453 fragS *fragP = NULL;
11454 enum align_branch_kind branch = align_branch_none;
79d72f45
HL
11455 /* The initializer is arbitrary just to avoid uninitialized error.
11456 it's actually either assigned in add_branch_padding_frag_p
11457 or never be used. */
11458 enum mf_jcc_kind mf_jcc = mf_jcc_jo;
2bbd9c25 11459
b4a3a7b4 11460#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
48ef937e 11461 if (IS_ELF && x86_used_note && now_seg != absolute_section)
b4a3a7b4 11462 {
32930e4e 11463 if ((i.xstate & xstate_tmm) == xstate_tmm
734dfd1c 11464 || is_cpu (&i.tm, CpuAMX_TILE))
32930e4e
L
11465 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_TMM;
11466
734dfd1c
JB
11467 if (is_cpu (&i.tm, Cpu8087)
11468 || is_cpu (&i.tm, Cpu287)
11469 || is_cpu (&i.tm, Cpu387)
11470 || is_cpu (&i.tm, Cpu687)
11471 || is_cpu (&i.tm, CpuFISTTP))
b4a3a7b4 11472 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X87;
014d61ea 11473
921eafea 11474 if ((i.xstate & xstate_mmx)
7fc69528
JB
11475 || i.tm.mnem_off == MN_emms
11476 || i.tm.mnem_off == MN_femms)
b4a3a7b4 11477 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MMX;
014d61ea 11478
32930e4e
L
11479 if (i.index_reg)
11480 {
11481 if (i.index_reg->reg_type.bitfield.zmmword)
11482 i.xstate |= xstate_zmm;
11483 else if (i.index_reg->reg_type.bitfield.ymmword)
11484 i.xstate |= xstate_ymm;
11485 else if (i.index_reg->reg_type.bitfield.xmmword)
11486 i.xstate |= xstate_xmm;
11487 }
014d61ea
JB
11488
11489 /* vzeroall / vzeroupper */
734dfd1c 11490 if (i.tm.base_opcode == 0x77 && is_cpu (&i.tm, CpuAVX))
014d61ea
JB
11491 i.xstate |= xstate_ymm;
11492
c4694f17 11493 if ((i.xstate & xstate_xmm)
389d00a5
JB
11494 /* ldmxcsr / stmxcsr / vldmxcsr / vstmxcsr */
11495 || (i.tm.base_opcode == 0xae
734dfd1c
JB
11496 && (is_cpu (&i.tm, CpuSSE)
11497 || is_cpu (&i.tm, CpuAVX)))
11498 || is_cpu (&i.tm, CpuWideKL)
11499 || is_cpu (&i.tm, CpuKL))
b4a3a7b4 11500 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XMM;
014d61ea 11501
921eafea 11502 if ((i.xstate & xstate_ymm) == xstate_ymm)
b4a3a7b4 11503 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_YMM;
921eafea 11504 if ((i.xstate & xstate_zmm) == xstate_zmm)
b4a3a7b4 11505 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_ZMM;
6225c532 11506 if (i.mask.reg || (i.xstate & xstate_mask) == xstate_mask)
32930e4e 11507 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MASK;
734dfd1c 11508 if (is_cpu (&i.tm, CpuFXSR))
b4a3a7b4 11509 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_FXSR;
734dfd1c 11510 if (is_cpu (&i.tm, CpuXsave))
b4a3a7b4 11511 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVE;
734dfd1c 11512 if (is_cpu (&i.tm, CpuXsaveopt))
b4a3a7b4 11513 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT;
734dfd1c 11514 if (is_cpu (&i.tm, CpuXSAVEC))
b4a3a7b4 11515 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEC;
b0ab0693
L
11516
11517 if (x86_feature_2_used
734dfd1c
JB
11518 || is_cpu (&i.tm, CpuCMOV)
11519 || is_cpu (&i.tm, CpuSYSCALL)
7fc69528 11520 || i.tm.mnem_off == MN_cmpxchg8b)
b0ab0693 11521 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_BASELINE;
734dfd1c
JB
11522 if (is_cpu (&i.tm, CpuSSE3)
11523 || is_cpu (&i.tm, CpuSSSE3)
11524 || is_cpu (&i.tm, CpuSSE4_1)
11525 || is_cpu (&i.tm, CpuSSE4_2)
11526 || is_cpu (&i.tm, CpuCX16)
11527 || is_cpu (&i.tm, CpuPOPCNT)
b0ab0693
L
11528 /* LAHF-SAHF insns in 64-bit mode. */
11529 || (flag_code == CODE_64BIT
35648716 11530 && (i.tm.base_opcode | 1) == 0x9f
ddb62495 11531 && i.tm.opcode_space == SPACE_BASE))
b0ab0693 11532 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V2;
734dfd1c
JB
11533 if (is_cpu (&i.tm, CpuAVX)
11534 || is_cpu (&i.tm, CpuAVX2)
a9860005
JB
11535 /* Any VEX encoded insns execpt for AVX512F, AVX512BW, AVX512DQ,
11536 XOP, FMA4, LPW, TBM, and AMX. */
b0ab0693 11537 || (i.tm.opcode_modifier.vex
734dfd1c
JB
11538 && !is_cpu (&i.tm, CpuAVX512F)
11539 && !is_cpu (&i.tm, CpuAVX512BW)
11540 && !is_cpu (&i.tm, CpuAVX512DQ)
11541 && !is_cpu (&i.tm, CpuXOP)
11542 && !is_cpu (&i.tm, CpuFMA4)
11543 && !is_cpu (&i.tm, CpuLWP)
11544 && !is_cpu (&i.tm, CpuTBM)
b0ab0693 11545 && !(x86_feature_2_used & GNU_PROPERTY_X86_FEATURE_2_TMM))
734dfd1c
JB
11546 || is_cpu (&i.tm, CpuF16C)
11547 || is_cpu (&i.tm, CpuFMA)
11548 || is_cpu (&i.tm, CpuLZCNT)
11549 || is_cpu (&i.tm, CpuMovbe)
11550 || is_cpu (&i.tm, CpuXSAVES)
b0ab0693
L
11551 || (x86_feature_2_used
11552 & (GNU_PROPERTY_X86_FEATURE_2_XSAVE
11553 | GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT
11554 | GNU_PROPERTY_X86_FEATURE_2_XSAVEC)) != 0)
11555 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V3;
734dfd1c
JB
11556 if (is_cpu (&i.tm, CpuAVX512F)
11557 || is_cpu (&i.tm, CpuAVX512BW)
11558 || is_cpu (&i.tm, CpuAVX512DQ)
11559 || is_cpu (&i.tm, CpuAVX512VL)
a9860005
JB
11560 /* Any EVEX encoded insns except for AVX512ER, AVX512PF,
11561 AVX512-4FMAPS, and AVX512-4VNNIW. */
b0ab0693 11562 || (i.tm.opcode_modifier.evex
734dfd1c
JB
11563 && !is_cpu (&i.tm, CpuAVX512ER)
11564 && !is_cpu (&i.tm, CpuAVX512PF)
11565 && !is_cpu (&i.tm, CpuAVX512_4FMAPS)
11566 && !is_cpu (&i.tm, CpuAVX512_4VNNIW)))
b0ab0693 11567 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V4;
b4a3a7b4
L
11568 }
11569#endif
11570
29b0f896
AM
11571 /* Tie dwarf2 debug info to the address at the start of the insn.
11572 We can't do this after the insn has been output as the current
11573 frag may have been closed off. eg. by frag_var. */
11574 dwarf2_emit_insn (0);
11575
2bbd9c25
JJ
11576 insn_start_frag = frag_now;
11577 insn_start_off = frag_now_fix ();
11578
b5482fe5 11579 if (add_branch_padding_frag_p (&branch, &mf_jcc, last_insn))
e379e5f3
L
11580 {
11581 char *p;
11582 /* Branch can be 8 bytes. Leave some room for prefixes. */
11583 unsigned int max_branch_padding_size = 14;
11584
11585 /* Align section to boundary. */
11586 record_alignment (now_seg, align_branch_power);
11587
11588 /* Make room for padding. */
11589 frag_grow (max_branch_padding_size);
11590
11591 /* Start of the padding. */
11592 p = frag_more (0);
11593
11594 fragP = frag_now;
11595
11596 frag_var (rs_machine_dependent, max_branch_padding_size, 0,
11597 ENCODE_RELAX_STATE (BRANCH_PADDING, 0),
11598 NULL, 0, p);
11599
79d72f45 11600 fragP->tc_frag_data.mf_type = mf_jcc;
e379e5f3
L
11601 fragP->tc_frag_data.branch_type = branch;
11602 fragP->tc_frag_data.max_bytes = max_branch_padding_size;
11603 }
11604
d59a54c2
JB
11605 if (!cpu_arch_flags.bitfield.cpui386 && (flag_code != CODE_16BIT)
11606 && !pre_386_16bit_warned)
11607 {
11608 as_warn (_("use .code16 to ensure correct addressing mode"));
11609 pre_386_16bit_warned = true;
11610 }
11611
29b0f896 11612 /* Output jumps. */
0cfa3eb3 11613 if (i.tm.opcode_modifier.jump == JUMP)
29b0f896 11614 output_branch ();
0cfa3eb3
JB
11615 else if (i.tm.opcode_modifier.jump == JUMP_BYTE
11616 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896 11617 output_jump ();
0cfa3eb3 11618 else if (i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT)
29b0f896
AM
11619 output_interseg_jump ();
11620 else
11621 {
11622 /* Output normal instructions here. */
11623 char *p;
11624 unsigned char *q;
47465058 11625 unsigned int j;
79d72f45 11626 enum mf_cmp_kind mf_cmp;
4dffcebc 11627
e4e00185 11628 if (avoid_fence
389d00a5
JB
11629 && (i.tm.base_opcode == 0xaee8
11630 || i.tm.base_opcode == 0xaef0
11631 || i.tm.base_opcode == 0xaef8))
48ef937e
JB
11632 {
11633 /* Encode lfence, mfence, and sfence as
11634 f0 83 04 24 00 lock addl $0x0, (%{re}sp). */
47f4115a 11635 if (flag_code == CODE_16BIT)
76d3f746 11636 as_bad (_("Cannot convert `%s' in 16-bit mode"), insn_name (&i.tm));
47f4115a
JB
11637 else if (omit_lock_prefix)
11638 as_bad (_("Cannot convert `%s' with `-momit-lock-prefix=yes' in effect"),
76d3f746 11639 insn_name (&i.tm));
47f4115a 11640 else if (now_seg != absolute_section)
48ef937e
JB
11641 {
11642 offsetT val = 0x240483f0ULL;
11643
11644 p = frag_more (5);
11645 md_number_to_chars (p, val, 5);
11646 }
11647 else
11648 abs_section_offset += 5;
11649 return;
11650 }
e4e00185 11651
d022bddd
IT
11652 /* Some processors fail on LOCK prefix. This options makes
11653 assembler ignore LOCK prefix and serves as a workaround. */
11654 if (omit_lock_prefix)
11655 {
35648716
JB
11656 if (i.tm.base_opcode == LOCK_PREFIX_OPCODE
11657 && i.tm.opcode_modifier.isprefix)
d022bddd
IT
11658 return;
11659 i.prefix[LOCK_PREFIX] = 0;
11660 }
11661
e379e5f3
L
11662 if (branch)
11663 /* Skip if this is a branch. */
11664 ;
b5482fe5 11665 else if (add_fused_jcc_padding_frag_p (&mf_cmp, last_insn))
e379e5f3
L
11666 {
11667 /* Make room for padding. */
11668 frag_grow (MAX_FUSED_JCC_PADDING_SIZE);
11669 p = frag_more (0);
11670
11671 fragP = frag_now;
11672
11673 frag_var (rs_machine_dependent, MAX_FUSED_JCC_PADDING_SIZE, 0,
11674 ENCODE_RELAX_STATE (FUSED_JCC_PADDING, 0),
11675 NULL, 0, p);
11676
79d72f45 11677 fragP->tc_frag_data.mf_type = mf_cmp;
e379e5f3
L
11678 fragP->tc_frag_data.branch_type = align_branch_fused;
11679 fragP->tc_frag_data.max_bytes = MAX_FUSED_JCC_PADDING_SIZE;
11680 }
b5482fe5 11681 else if (add_branch_prefix_frag_p (last_insn))
e379e5f3
L
11682 {
11683 unsigned int max_prefix_size = align_branch_prefix_size;
11684
11685 /* Make room for padding. */
11686 frag_grow (max_prefix_size);
11687 p = frag_more (0);
11688
11689 fragP = frag_now;
11690
11691 frag_var (rs_machine_dependent, max_prefix_size, 0,
11692 ENCODE_RELAX_STATE (BRANCH_PREFIX, 0),
11693 NULL, 0, p);
11694
11695 fragP->tc_frag_data.max_bytes = max_prefix_size;
11696 }
11697
43234a1e
L
11698 /* Since the VEX/EVEX prefix contains the implicit prefix, we
11699 don't need the explicit prefix. */
cf665fee 11700 if (!is_any_vex_encoding (&i.tm))
bc4bd9ab 11701 {
7b47a312 11702 switch (i.tm.opcode_modifier.opcodeprefix)
bc4bd9ab 11703 {
7b47a312
L
11704 case PREFIX_0X66:
11705 add_prefix (0x66);
11706 break;
11707 case PREFIX_0XF2:
11708 add_prefix (0xf2);
11709 break;
11710 case PREFIX_0XF3:
734dfd1c 11711 if (!is_cpu (&i.tm, CpuPadLock)
8b65b895
L
11712 || (i.prefix[REP_PREFIX] != 0xf3))
11713 add_prefix (0xf3);
c0f3af97 11714 break;
7b47a312 11715 case PREFIX_NONE:
9a182d04 11716 switch (i.opcode_length)
c0f3af97 11717 {
7b47a312 11718 case 2:
7b47a312 11719 break;
9a182d04 11720 case 1:
7b47a312 11721 /* Check for pseudo prefixes. */
9a182d04
JB
11722 if (!i.tm.opcode_modifier.isprefix || i.tm.base_opcode)
11723 break;
7b47a312
L
11724 as_bad_where (insn_start_frag->fr_file,
11725 insn_start_frag->fr_line,
11726 _("pseudo prefix without instruction"));
11727 return;
11728 default:
11729 abort ();
4dffcebc 11730 }
c0f3af97 11731 break;
c0f3af97
L
11732 default:
11733 abort ();
bc4bd9ab 11734 }
c0f3af97 11735
6d19a37a 11736#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
cf61b747
L
11737 /* For x32, add a dummy REX_OPCODE prefix for mov/add with
11738 R_X86_64_GOTTPOFF relocation so that linker can safely
14470f07
L
11739 perform IE->LE optimization. A dummy REX_OPCODE prefix
11740 is also needed for lea with R_X86_64_GOTPC32_TLSDESC
11741 relocation for GDesc -> IE/LE optimization. */
cf61b747 11742 if (x86_elf_abi == X86_64_X32_ABI
a533c8df 11743 && !is_apx_rex2_encoding ()
cf61b747 11744 && i.operands == 2
14470f07
L
11745 && (i.reloc[0] == BFD_RELOC_X86_64_GOTTPOFF
11746 || i.reloc[0] == BFD_RELOC_X86_64_GOTPC32_TLSDESC)
cf61b747
L
11747 && i.prefix[REX_PREFIX] == 0)
11748 add_prefix (REX_OPCODE);
6d19a37a 11749#endif
cf61b747 11750
c0f3af97
L
11751 /* The prefix bytes. */
11752 for (j = ARRAY_SIZE (i.prefix), q = i.prefix; j > 0; j--, q++)
11753 if (*q)
48ef937e 11754 frag_opcode_byte (*q);
80d61d8d
CL
11755
11756 if (is_apx_rex2_encoding ())
11757 {
11758 frag_opcode_byte (i.vex.bytes[0]);
11759 frag_opcode_byte (i.vex.bytes[1]);
11760 }
0f10071e 11761 }
ae5c1c7b 11762 else
c0f3af97
L
11763 {
11764 for (j = 0, q = i.prefix; j < ARRAY_SIZE (i.prefix); j++, q++)
11765 if (*q)
11766 switch (j)
11767 {
c0f3af97
L
11768 case SEG_PREFIX:
11769 case ADDR_PREFIX:
48ef937e 11770 frag_opcode_byte (*q);
c0f3af97
L
11771 break;
11772 default:
11773 /* There should be no other prefixes for instructions
11774 with VEX prefix. */
11775 abort ();
11776 }
11777
43234a1e
L
11778 /* For EVEX instructions i.vrex should become 0 after
11779 build_evex_prefix. For VEX instructions upper 16 registers
11780 aren't available, so VREX should be 0. */
11781 if (i.vrex)
11782 abort ();
c0f3af97 11783 /* Now the VEX prefix. */
48ef937e
JB
11784 if (now_seg != absolute_section)
11785 {
11786 p = frag_more (i.vex.length);
11787 for (j = 0; j < i.vex.length; j++)
11788 p[j] = i.vex.bytes[j];
11789 }
11790 else
11791 abs_section_offset += i.vex.length;
c0f3af97 11792 }
252b5132 11793
29b0f896 11794 /* Now the opcode; be careful about word order here! */
389d00a5
JB
11795 j = i.opcode_length;
11796 if (!i.vex.length)
ddb62495 11797 switch (i.tm.opcode_space)
389d00a5
JB
11798 {
11799 case SPACE_BASE:
11800 break;
11801 case SPACE_0F:
11802 ++j;
11803 break;
11804 case SPACE_0F38:
11805 case SPACE_0F3A:
11806 j += 2;
11807 break;
11808 default:
11809 abort ();
11810 }
11811
48ef937e 11812 if (now_seg == absolute_section)
389d00a5
JB
11813 abs_section_offset += j;
11814 else if (j == 1)
29b0f896
AM
11815 {
11816 FRAG_APPEND_1_CHAR (i.tm.base_opcode);
11817 }
11818 else
11819 {
389d00a5
JB
11820 p = frag_more (j);
11821 if (!i.vex.length
ddb62495 11822 && i.tm.opcode_space != SPACE_BASE)
389d00a5
JB
11823 {
11824 *p++ = 0x0f;
ddb62495
JB
11825 if (i.tm.opcode_space != SPACE_0F)
11826 *p++ = i.tm.opcode_space == SPACE_0F38
389d00a5
JB
11827 ? 0x38 : 0x3a;
11828 }
11829
9a182d04 11830 switch (i.opcode_length)
331d2d0d 11831 {
4dffcebc 11832 case 2:
389d00a5
JB
11833 /* Put out high byte first: can't use md_number_to_chars! */
11834 *p++ = (i.tm.base_opcode >> 8) & 0xff;
11835 /* Fall through. */
11836 case 1:
11837 *p = i.tm.base_opcode & 0xff;
4dffcebc
L
11838 break;
11839 default:
11840 abort ();
11841 break;
331d2d0d 11842 }
0f10071e 11843
29b0f896 11844 }
3e73aa7c 11845
29b0f896 11846 /* Now the modrm byte and sib byte (if present). */
40fb9820 11847 if (i.tm.opcode_modifier.modrm)
29b0f896 11848 {
48ef937e
JB
11849 frag_opcode_byte ((i.rm.regmem << 0)
11850 | (i.rm.reg << 3)
11851 | (i.rm.mode << 6));
29b0f896
AM
11852 /* If i.rm.regmem == ESP (4)
11853 && i.rm.mode != (Register mode)
11854 && not 16 bit
11855 ==> need second modrm byte. */
11856 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
11857 && i.rm.mode != 3
dc821c5f 11858 && !(i.base_reg && i.base_reg->reg_type.bitfield.word))
48ef937e
JB
11859 frag_opcode_byte ((i.sib.base << 0)
11860 | (i.sib.index << 3)
11861 | (i.sib.scale << 6));
29b0f896 11862 }
3e73aa7c 11863
29b0f896 11864 if (i.disp_operands)
2bbd9c25 11865 output_disp (insn_start_frag, insn_start_off);
3e73aa7c 11866
29b0f896 11867 if (i.imm_operands)
2bbd9c25 11868 output_imm (insn_start_frag, insn_start_off);
9c33702b
JB
11869
11870 /*
11871 * frag_now_fix () returning plain abs_section_offset when we're in the
11872 * absolute section, and abs_section_offset not getting updated as data
11873 * gets added to the frag breaks the logic below.
11874 */
11875 if (now_seg != absolute_section)
11876 {
11877 j = encoding_length (insn_start_frag, insn_start_off, frag_more (0));
11878 if (j > 15)
0afc614c
L
11879 {
11880 if (dot_insn ())
11881 as_warn (_("instruction length of %u bytes exceeds the limit of 15"),
11882 j);
11883 else
11884 as_bad (_("instruction length of %u bytes exceeds the limit of 15"),
11885 j);
11886 }
e379e5f3
L
11887 else if (fragP)
11888 {
11889 /* NB: Don't add prefix with GOTPC relocation since
11890 output_disp() above depends on the fixed encoding
11891 length. Can't add prefix with TLS relocation since
11892 it breaks TLS linker optimization. */
11893 unsigned int max = i.has_gotpc_tls_reloc ? 0 : 15 - j;
11894 /* Prefix count on the current instruction. */
11895 unsigned int count = i.vex.length;
11896 unsigned int k;
11897 for (k = 0; k < ARRAY_SIZE (i.prefix); k++)
11898 /* REX byte is encoded in VEX/EVEX prefix. */
11899 if (i.prefix[k] && (k != REX_PREFIX || !i.vex.length))
11900 count++;
11901
11902 /* Count prefixes for extended opcode maps. */
11903 if (!i.vex.length)
ddb62495 11904 switch (i.tm.opcode_space)
e379e5f3 11905 {
389d00a5 11906 case SPACE_BASE:
e379e5f3 11907 break;
389d00a5
JB
11908 case SPACE_0F:
11909 count++;
e379e5f3 11910 break;
389d00a5
JB
11911 case SPACE_0F38:
11912 case SPACE_0F3A:
11913 count += 2;
e379e5f3
L
11914 break;
11915 default:
11916 abort ();
11917 }
11918
11919 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
11920 == BRANCH_PREFIX)
11921 {
11922 /* Set the maximum prefix size in BRANCH_PREFIX
11923 frag. */
11924 if (fragP->tc_frag_data.max_bytes > max)
11925 fragP->tc_frag_data.max_bytes = max;
11926 if (fragP->tc_frag_data.max_bytes > count)
11927 fragP->tc_frag_data.max_bytes -= count;
11928 else
11929 fragP->tc_frag_data.max_bytes = 0;
11930 }
11931 else
11932 {
11933 /* Remember the maximum prefix size in FUSED_JCC_PADDING
11934 frag. */
11935 unsigned int max_prefix_size;
11936 if (align_branch_prefix_size > max)
11937 max_prefix_size = max;
11938 else
11939 max_prefix_size = align_branch_prefix_size;
11940 if (max_prefix_size > count)
11941 fragP->tc_frag_data.max_prefix_length
11942 = max_prefix_size - count;
11943 }
11944
11945 /* Use existing segment prefix if possible. Use CS
11946 segment prefix in 64-bit mode. In 32-bit mode, use SS
11947 segment prefix with ESP/EBP base register and use DS
11948 segment prefix without ESP/EBP base register. */
11949 if (i.prefix[SEG_PREFIX])
11950 fragP->tc_frag_data.default_prefix = i.prefix[SEG_PREFIX];
11951 else if (flag_code == CODE_64BIT)
11952 fragP->tc_frag_data.default_prefix = CS_PREFIX_OPCODE;
11953 else if (i.base_reg
11954 && (i.base_reg->reg_num == 4
11955 || i.base_reg->reg_num == 5))
11956 fragP->tc_frag_data.default_prefix = SS_PREFIX_OPCODE;
11957 else
11958 fragP->tc_frag_data.default_prefix = DS_PREFIX_OPCODE;
11959 }
9c33702b 11960 }
29b0f896 11961 }
252b5132 11962
e379e5f3
L
11963 /* NB: Don't work with COND_JUMP86 without i386. */
11964 if (align_branch_power
11965 && now_seg != absolute_section
11966 && cpu_arch_flags.bitfield.cpui386)
11967 {
11968 /* Terminate each frag so that we can add prefix and check for
11969 fused jcc. */
11970 frag_wane (frag_now);
11971 frag_new (0);
11972 }
11973
29b0f896
AM
11974#ifdef DEBUG386
11975 if (flag_debug)
11976 {
7b81dfbb 11977 pi ("" /*line*/, &i);
29b0f896
AM
11978 }
11979#endif /* DEBUG386 */
11980}
252b5132 11981
e205caa7
L
11982/* Return the size of the displacement operand N. */
11983
11984static int
11985disp_size (unsigned int n)
11986{
11987 int size = 4;
43234a1e 11988
b5014f7a 11989 if (i.types[n].bitfield.disp64)
40fb9820
L
11990 size = 8;
11991 else if (i.types[n].bitfield.disp8)
11992 size = 1;
11993 else if (i.types[n].bitfield.disp16)
11994 size = 2;
e205caa7
L
11995 return size;
11996}
11997
11998/* Return the size of the immediate operand N. */
11999
12000static int
12001imm_size (unsigned int n)
12002{
12003 int size = 4;
40fb9820
L
12004 if (i.types[n].bitfield.imm64)
12005 size = 8;
12006 else if (i.types[n].bitfield.imm8 || i.types[n].bitfield.imm8s)
12007 size = 1;
12008 else if (i.types[n].bitfield.imm16)
12009 size = 2;
e205caa7
L
12010 return size;
12011}
12012
29b0f896 12013static void
64e74474 12014output_disp (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
12015{
12016 char *p;
12017 unsigned int n;
252b5132 12018
29b0f896
AM
12019 for (n = 0; n < i.operands; n++)
12020 {
b5014f7a 12021 if (operand_type_check (i.types[n], disp))
29b0f896 12022 {
48ef937e
JB
12023 int size = disp_size (n);
12024
12025 if (now_seg == absolute_section)
12026 abs_section_offset += size;
12027 else if (i.op[n].disps->X_op == O_constant)
29b0f896 12028 {
43234a1e 12029 offsetT val = i.op[n].disps->X_add_number;
252b5132 12030
629cfaf1
JB
12031 val = offset_in_range (val >> (size == 1 ? i.memshift : 0),
12032 size);
29b0f896
AM
12033 p = frag_more (size);
12034 md_number_to_chars (p, val, size);
12035 }
12036 else
12037 {
f86103b7 12038 enum bfd_reloc_code_real reloc_type;
a775efc8
JB
12039 bool pcrel = (i.flags[n] & Operand_PCrel) != 0;
12040 bool sign = (flag_code == CODE_64BIT && size == 4
12041 && (!want_disp32 (&i.tm)
12042 || (i.tm.opcode_modifier.jump && !i.jumpabsolute
12043 && !i.types[n].bitfield.baseindex)))
12044 || pcrel;
02a86693 12045 fixS *fixP;
29b0f896 12046
e205caa7 12047 /* We can't have 8 bit displacement here. */
9c2799c2 12048 gas_assert (!i.types[n].bitfield.disp8);
e205caa7 12049
29b0f896
AM
12050 /* The PC relative address is computed relative
12051 to the instruction boundary, so in case immediate
12052 fields follows, we need to adjust the value. */
12053 if (pcrel && i.imm_operands)
12054 {
29b0f896 12055 unsigned int n1;
e205caa7 12056 int sz = 0;
252b5132 12057
29b0f896 12058 for (n1 = 0; n1 < i.operands; n1++)
40fb9820 12059 if (operand_type_check (i.types[n1], imm))
252b5132 12060 {
e205caa7 12061 /* Only one immediate is allowed for PC
e3bf0aad
JB
12062 relative address, except with .insn. */
12063 gas_assert (sz == 0 || dot_insn ());
12064 sz += imm_size (n1);
252b5132 12065 }
e3bf0aad 12066 /* We should find at least one immediate. */
9c2799c2 12067 gas_assert (sz != 0);
e3bf0aad 12068 i.op[n].disps->X_add_number -= sz;
29b0f896 12069 }
520dc8e8 12070
29b0f896 12071 p = frag_more (size);
d258b828 12072 reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
d6ab8113 12073 if (GOT_symbol
2bbd9c25 12074 && GOT_symbol == i.op[n].disps->X_add_symbol
d6ab8113 12075 && (((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
12076 || reloc_type == BFD_RELOC_X86_64_32S
12077 || (reloc_type == BFD_RELOC_64
12078 && object_64bit))
d6ab8113
JB
12079 && (i.op[n].disps->X_op == O_symbol
12080 || (i.op[n].disps->X_op == O_add
12081 && ((symbol_get_value_expression
12082 (i.op[n].disps->X_op_symbol)->X_op)
12083 == O_subtract))))
12084 || reloc_type == BFD_RELOC_32_PCREL))
2bbd9c25 12085 {
4fa24527 12086 if (!object_64bit)
7b81dfbb
AJ
12087 {
12088 reloc_type = BFD_RELOC_386_GOTPC;
5b7c81bd 12089 i.has_gotpc_tls_reloc = true;
98da05bf 12090 i.op[n].disps->X_add_number +=
d583596c 12091 encoding_length (insn_start_frag, insn_start_off, p);
7b81dfbb
AJ
12092 }
12093 else if (reloc_type == BFD_RELOC_64)
12094 reloc_type = BFD_RELOC_X86_64_GOTPC64;
d6ab8113 12095 else
7b81dfbb
AJ
12096 /* Don't do the adjustment for x86-64, as there
12097 the pcrel addressing is relative to the _next_
12098 insn, and that is taken care of in other code. */
d6ab8113 12099 reloc_type = BFD_RELOC_X86_64_GOTPC32;
2bbd9c25 12100 }
e379e5f3
L
12101 else if (align_branch_power)
12102 {
12103 switch (reloc_type)
12104 {
12105 case BFD_RELOC_386_TLS_GD:
12106 case BFD_RELOC_386_TLS_LDM:
12107 case BFD_RELOC_386_TLS_IE:
12108 case BFD_RELOC_386_TLS_IE_32:
12109 case BFD_RELOC_386_TLS_GOTIE:
12110 case BFD_RELOC_386_TLS_GOTDESC:
12111 case BFD_RELOC_386_TLS_DESC_CALL:
12112 case BFD_RELOC_X86_64_TLSGD:
12113 case BFD_RELOC_X86_64_TLSLD:
12114 case BFD_RELOC_X86_64_GOTTPOFF:
a533c8df 12115 case BFD_RELOC_X86_64_CODE_4_GOTTPOFF:
5bc71c2a 12116 case BFD_RELOC_X86_64_CODE_6_GOTTPOFF:
e379e5f3 12117 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
a533c8df 12118 case BFD_RELOC_X86_64_CODE_4_GOTPC32_TLSDESC:
e379e5f3 12119 case BFD_RELOC_X86_64_TLSDESC_CALL:
5b7c81bd 12120 i.has_gotpc_tls_reloc = true;
e379e5f3
L
12121 default:
12122 break;
12123 }
12124 }
02a86693
L
12125 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal,
12126 size, i.op[n].disps, pcrel,
12127 reloc_type);
eb19308f
JB
12128
12129 if (flag_code == CODE_64BIT && size == 4 && pcrel
12130 && !i.prefix[ADDR_PREFIX])
12131 fixP->fx_signed = 1;
12132
5bc71c2a
L
12133 if (reloc_type == BFD_RELOC_X86_64_GOTTPOFF
12134 && i.tm.opcode_space == SPACE_EVEXMAP4)
12135 {
12136 /* Only "add %reg1, foo@gottpoff(%rip), %reg2" is
12137 allowed in md_assemble. Set fx_tcbit2 for EVEX
12138 prefix. */
12139 fixP->fx_tcbit2 = 1;
12140 continue;
12141 }
12142
12143 if (i.base_reg && i.base_reg->reg_num == RegIP)
12144 {
12145 if (reloc_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC)
12146 {
12147 /* Set fx_tcbit for REX2 prefix. */
12148 if (is_apx_rex2_encoding ())
12149 fixP->fx_tcbit = 1;
12150 continue;
12151 }
12152 }
12153 /* In 64-bit, i386_validate_fix updates only (%rip)
12154 relocations. */
12155 else if (object_64bit)
12156 continue;
3d5a60de 12157
02a86693
L
12158 /* Check for "call/jmp *mem", "mov mem, %reg",
12159 "test %reg, mem" and "binop mem, %reg" where binop
12160 is one of adc, add, and, cmp, or, sbb, sub, xor
e60f4d3b
L
12161 instructions without data prefix. Always generate
12162 R_386_GOT32X for "sym*GOT" operand in 32-bit mode. */
12163 if (i.prefix[DATA_PREFIX] == 0
0cb4071e
L
12164 && (i.rm.mode == 2
12165 || (i.rm.mode == 0 && i.rm.regmem == 5))
ddb62495 12166 && i.tm.opcode_space == SPACE_BASE
02a86693
L
12167 && ((i.operands == 1
12168 && i.tm.base_opcode == 0xff
12169 && (i.rm.reg == 2 || i.rm.reg == 4))
12170 || (i.operands == 2
12171 && (i.tm.base_opcode == 0x8b
12172 || i.tm.base_opcode == 0x85
2ae4c703 12173 || (i.tm.base_opcode & ~0x38) == 0x03))))
02a86693
L
12174 {
12175 if (object_64bit)
12176 {
5bc71c2a
L
12177 if (reloc_type == BFD_RELOC_X86_64_GOTTPOFF)
12178 {
12179 /* Set fx_tcbit for REX2 prefix. */
12180 if (is_apx_rex2_encoding ())
12181 fixP->fx_tcbit = 1;
12182 }
820a7755 12183 else if (generate_relax_relocations)
5bc71c2a
L
12184 {
12185 /* Set fx_tcbit3 for REX2 prefix. */
12186 if (is_apx_rex2_encoding ())
12187 fixP->fx_tcbit3 = 1;
12188 else if (i.rex)
12189 fixP->fx_tcbit2 = 1;
12190 else
12191 fixP->fx_tcbit = 1;
12192 }
02a86693 12193 }
820a7755
JB
12194 else if (generate_relax_relocations
12195 || (i.rm.mode == 0 && i.rm.regmem == 5))
02a86693
L
12196 fixP->fx_tcbit2 = 1;
12197 }
29b0f896
AM
12198 }
12199 }
12200 }
12201}
252b5132 12202
29b0f896 12203static void
64e74474 12204output_imm (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
12205{
12206 char *p;
12207 unsigned int n;
252b5132 12208
29b0f896
AM
12209 for (n = 0; n < i.operands; n++)
12210 {
40fb9820 12211 if (operand_type_check (i.types[n], imm))
29b0f896 12212 {
48ef937e
JB
12213 int size = imm_size (n);
12214
12215 if (now_seg == absolute_section)
12216 abs_section_offset += size;
12217 else if (i.op[n].imms->X_op == O_constant)
29b0f896 12218 {
29b0f896 12219 offsetT val;
b4cac588 12220
29b0f896
AM
12221 val = offset_in_range (i.op[n].imms->X_add_number,
12222 size);
12223 p = frag_more (size);
12224 md_number_to_chars (p, val, size);
12225 }
12226 else
12227 {
12228 /* Not absolute_section.
12229 Need a 32-bit fixup (don't support 8bit
12230 non-absolute imms). Try to support other
12231 sizes ... */
f86103b7 12232 enum bfd_reloc_code_real reloc_type;
e205caa7 12233 int sign;
29b0f896 12234
40fb9820 12235 if (i.types[n].bitfield.imm32s
a7d61044 12236 && (i.suffix == QWORD_MNEM_SUFFIX
c032bc4f 12237 || (!i.suffix && i.tm.opcode_modifier.no_lsuf)
f79d55e1 12238 || (i.prefix[REX_PREFIX] & REX_W)
c032bc4f 12239 || dot_insn ()))
29b0f896 12240 sign = 1;
e205caa7
L
12241 else
12242 sign = 0;
520dc8e8 12243
29b0f896 12244 p = frag_more (size);
d258b828 12245 reloc_type = reloc (size, 0, sign, i.reloc[n]);
f86103b7 12246
2bbd9c25
JJ
12247 /* This is tough to explain. We end up with this one if we
12248 * have operands that look like
12249 * "_GLOBAL_OFFSET_TABLE_+[.-.L284]". The goal here is to
12250 * obtain the absolute address of the GOT, and it is strongly
12251 * preferable from a performance point of view to avoid using
12252 * a runtime relocation for this. The actual sequence of
12253 * instructions often look something like:
12254 *
12255 * call .L66
12256 * .L66:
12257 * popl %ebx
12258 * addl $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
12259 *
12260 * The call and pop essentially return the absolute address
12261 * of the label .L66 and store it in %ebx. The linker itself
12262 * will ultimately change the first operand of the addl so
12263 * that %ebx points to the GOT, but to keep things simple, the
12264 * .o file must have this operand set so that it generates not
12265 * the absolute address of .L66, but the absolute address of
12266 * itself. This allows the linker itself simply treat a GOTPC
12267 * relocation as asking for a pcrel offset to the GOT to be
12268 * added in, and the addend of the relocation is stored in the
12269 * operand field for the instruction itself.
12270 *
12271 * Our job here is to fix the operand so that it would add
12272 * the correct offset so that %ebx would point to itself. The
12273 * thing that is tricky is that .-.L66 will point to the
12274 * beginning of the instruction, so we need to further modify
12275 * the operand so that it will point to itself. There are
12276 * other cases where you have something like:
12277 *
12278 * .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
12279 *
12280 * and here no correction would be required. Internally in
12281 * the assembler we treat operands of this form as not being
12282 * pcrel since the '.' is explicitly mentioned, and I wonder
12283 * whether it would simplify matters to do it this way. Who
12284 * knows. In earlier versions of the PIC patches, the
12285 * pcrel_adjust field was used to store the correction, but
12286 * since the expression is not pcrel, I felt it would be
12287 * confusing to do it this way. */
12288
d6ab8113 12289 if ((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
12290 || reloc_type == BFD_RELOC_X86_64_32S
12291 || reloc_type == BFD_RELOC_64)
29b0f896
AM
12292 && GOT_symbol
12293 && GOT_symbol == i.op[n].imms->X_add_symbol
12294 && (i.op[n].imms->X_op == O_symbol
12295 || (i.op[n].imms->X_op == O_add
12296 && ((symbol_get_value_expression
12297 (i.op[n].imms->X_op_symbol)->X_op)
12298 == O_subtract))))
12299 {
4fa24527 12300 if (!object_64bit)
d6ab8113 12301 reloc_type = BFD_RELOC_386_GOTPC;
7b81dfbb 12302 else if (size == 4)
d6ab8113 12303 reloc_type = BFD_RELOC_X86_64_GOTPC32;
7b81dfbb
AJ
12304 else if (size == 8)
12305 reloc_type = BFD_RELOC_X86_64_GOTPC64;
5b7c81bd 12306 i.has_gotpc_tls_reloc = true;
d583596c
JB
12307 i.op[n].imms->X_add_number +=
12308 encoding_length (insn_start_frag, insn_start_off, p);
29b0f896 12309 }
29b0f896
AM
12310 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
12311 i.op[n].imms, 0, reloc_type);
12312 }
12313 }
12314 }
252b5132
RH
12315}
12316\f
d182319b
JB
12317/* x86_cons_fix_new is called via the expression parsing code when a
12318 reloc is needed. We use this hook to get the correct .got reloc. */
d182319b
JB
12319static int cons_sign = -1;
12320
12321void
e3bb37b5 12322x86_cons_fix_new (fragS *frag, unsigned int off, unsigned int len,
62ebcb5c 12323 expressionS *exp, bfd_reloc_code_real_type r)
d182319b 12324{
d258b828 12325 r = reloc (len, 0, cons_sign, r);
d182319b
JB
12326
12327#ifdef TE_PE
12328 if (exp->X_op == O_secrel)
12329 {
12330 exp->X_op = O_symbol;
12331 r = BFD_RELOC_32_SECREL;
12332 }
145667f8
MH
12333 else if (exp->X_op == O_secidx)
12334 r = BFD_RELOC_16_SECIDX;
d182319b
JB
12335#endif
12336
12337 fix_new_exp (frag, off, len, exp, 0, r);
12338}
12339
357d1bd8
L
12340/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
12341 purpose of the `.dc.a' internal pseudo-op. */
12342
12343int
12344x86_address_bytes (void)
12345{
12346 if ((stdoutput->arch_info->mach & bfd_mach_x64_32))
12347 return 4;
12348 return stdoutput->arch_info->bits_per_address / 8;
12349}
12350
deea4973
JB
12351#if (!(defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined (OBJ_MACH_O)) \
12352 || defined (LEX_AT)) && !defined (TE_PE)
d258b828 12353# define lex_got(reloc, adjust, types) NULL
718ddfc0 12354#else
f3c180ae
AM
12355/* Parse operands of the form
12356 <symbol>@GOTOFF+<nnn>
12357 and similar .plt or .got references.
12358
12359 If we find one, set up the correct relocation in RELOC and copy the
12360 input string, minus the `@GOTOFF' into a malloc'd buffer for
12361 parsing by the calling routine. Return this buffer, and if ADJUST
12362 is non-null set it to the length of the string we removed from the
12363 input line. Otherwise return NULL. */
12364static char *
91d6fa6a 12365lex_got (enum bfd_reloc_code_real *rel,
64e74474 12366 int *adjust,
d258b828 12367 i386_operand_type *types)
f3c180ae 12368{
7b81dfbb
AJ
12369 /* Some of the relocations depend on the size of what field is to
12370 be relocated. But in our callers i386_immediate and i386_displacement
12371 we don't yet know the operand size (this will be set by insn
12372 matching). Hence we record the word32 relocation here,
12373 and adjust the reloc according to the real size in reloc(). */
145667f8
MH
12374 static const struct
12375 {
f3c180ae 12376 const char *str;
cff8d58a 12377 int len;
4fa24527 12378 const enum bfd_reloc_code_real rel[2];
40fb9820 12379 const i386_operand_type types64;
5b7c81bd 12380 bool need_GOT_symbol;
145667f8
MH
12381 }
12382 gotrel[] =
12383 {
05909f23
JB
12384
12385#define OPERAND_TYPE_IMM32_32S_DISP32 { .bitfield = \
12386 { .imm32 = 1, .imm32s = 1, .disp32 = 1 } }
12387#define OPERAND_TYPE_IMM32_32S_64_DISP32 { .bitfield = \
12388 { .imm32 = 1, .imm32s = 1, .imm64 = 1, .disp32 = 1 } }
12389#define OPERAND_TYPE_IMM32_32S_64_DISP32_64 { .bitfield = \
12390 { .imm32 = 1, .imm32s = 1, .imm64 = 1, .disp32 = 1, .disp64 = 1 } }
12391#define OPERAND_TYPE_IMM64_DISP64 { .bitfield = \
12392 { .imm64 = 1, .disp64 = 1 } }
12393
deea4973 12394#ifndef TE_PE
8ce3d284 12395#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
12396 { STRING_COMMA_LEN ("SIZE"), { BFD_RELOC_SIZE32,
12397 BFD_RELOC_SIZE32 },
05909f23 12398 { .bitfield = { .imm32 = 1, .imm64 = 1 } }, false },
8ce3d284 12399#endif
cff8d58a
L
12400 { STRING_COMMA_LEN ("PLTOFF"), { _dummy_first_bfd_reloc_code_real,
12401 BFD_RELOC_X86_64_PLTOFF64 },
05909f23 12402 { .bitfield = { .imm64 = 1 } }, true },
cff8d58a
L
12403 { STRING_COMMA_LEN ("PLT"), { BFD_RELOC_386_PLT32,
12404 BFD_RELOC_X86_64_PLT32 },
a775efc8 12405 OPERAND_TYPE_IMM32_32S_DISP32, false },
cff8d58a
L
12406 { STRING_COMMA_LEN ("GOTPLT"), { _dummy_first_bfd_reloc_code_real,
12407 BFD_RELOC_X86_64_GOTPLT64 },
5b7c81bd 12408 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
12409 { STRING_COMMA_LEN ("GOTOFF"), { BFD_RELOC_386_GOTOFF,
12410 BFD_RELOC_X86_64_GOTOFF64 },
5b7c81bd 12411 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
12412 { STRING_COMMA_LEN ("GOTPCREL"), { _dummy_first_bfd_reloc_code_real,
12413 BFD_RELOC_X86_64_GOTPCREL },
a775efc8 12414 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
12415 { STRING_COMMA_LEN ("TLSGD"), { BFD_RELOC_386_TLS_GD,
12416 BFD_RELOC_X86_64_TLSGD },
a775efc8 12417 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
12418 { STRING_COMMA_LEN ("TLSLDM"), { BFD_RELOC_386_TLS_LDM,
12419 _dummy_first_bfd_reloc_code_real },
5b7c81bd 12420 OPERAND_TYPE_NONE, true },
cff8d58a
L
12421 { STRING_COMMA_LEN ("TLSLD"), { _dummy_first_bfd_reloc_code_real,
12422 BFD_RELOC_X86_64_TLSLD },
a775efc8 12423 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
12424 { STRING_COMMA_LEN ("GOTTPOFF"), { BFD_RELOC_386_TLS_IE_32,
12425 BFD_RELOC_X86_64_GOTTPOFF },
a775efc8 12426 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
12427 { STRING_COMMA_LEN ("TPOFF"), { BFD_RELOC_386_TLS_LE_32,
12428 BFD_RELOC_X86_64_TPOFF32 },
a775efc8 12429 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
12430 { STRING_COMMA_LEN ("NTPOFF"), { BFD_RELOC_386_TLS_LE,
12431 _dummy_first_bfd_reloc_code_real },
5b7c81bd 12432 OPERAND_TYPE_NONE, true },
cff8d58a
L
12433 { STRING_COMMA_LEN ("DTPOFF"), { BFD_RELOC_386_TLS_LDO_32,
12434 BFD_RELOC_X86_64_DTPOFF32 },
a775efc8 12435 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
12436 { STRING_COMMA_LEN ("GOTNTPOFF"),{ BFD_RELOC_386_TLS_GOTIE,
12437 _dummy_first_bfd_reloc_code_real },
5b7c81bd 12438 OPERAND_TYPE_NONE, true },
cff8d58a
L
12439 { STRING_COMMA_LEN ("INDNTPOFF"),{ BFD_RELOC_386_TLS_IE,
12440 _dummy_first_bfd_reloc_code_real },
5b7c81bd 12441 OPERAND_TYPE_NONE, true },
cff8d58a
L
12442 { STRING_COMMA_LEN ("GOT"), { BFD_RELOC_386_GOT32,
12443 BFD_RELOC_X86_64_GOT32 },
a775efc8 12444 OPERAND_TYPE_IMM32_32S_64_DISP32, true },
cff8d58a
L
12445 { STRING_COMMA_LEN ("TLSDESC"), { BFD_RELOC_386_TLS_GOTDESC,
12446 BFD_RELOC_X86_64_GOTPC32_TLSDESC },
a775efc8 12447 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
12448 { STRING_COMMA_LEN ("TLSCALL"), { BFD_RELOC_386_TLS_DESC_CALL,
12449 BFD_RELOC_X86_64_TLSDESC_CALL },
a775efc8 12450 OPERAND_TYPE_IMM32_32S_DISP32, true },
deea4973
JB
12451#else /* TE_PE */
12452 { STRING_COMMA_LEN ("SECREL32"), { BFD_RELOC_32_SECREL,
12453 BFD_RELOC_32_SECREL },
a775efc8 12454 OPERAND_TYPE_IMM32_32S_64_DISP32_64, false },
deea4973 12455#endif
05909f23
JB
12456
12457#undef OPERAND_TYPE_IMM32_32S_DISP32
12458#undef OPERAND_TYPE_IMM32_32S_64_DISP32
12459#undef OPERAND_TYPE_IMM32_32S_64_DISP32_64
12460#undef OPERAND_TYPE_IMM64_DISP64
12461
f3c180ae
AM
12462 };
12463 char *cp;
12464 unsigned int j;
12465
deea4973 12466#if defined (OBJ_MAYBE_ELF) && !defined (TE_PE)
718ddfc0
JB
12467 if (!IS_ELF)
12468 return NULL;
d382c579 12469#endif
718ddfc0 12470
f3c180ae 12471 for (cp = input_line_pointer; *cp != '@'; cp++)
67c11a9b 12472 if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
f3c180ae
AM
12473 return NULL;
12474
47465058 12475 for (j = 0; j < ARRAY_SIZE (gotrel); j++)
f3c180ae 12476 {
cff8d58a 12477 int len = gotrel[j].len;
28f81592 12478 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
f3c180ae 12479 {
4fa24527 12480 if (gotrel[j].rel[object_64bit] != 0)
f3c180ae 12481 {
28f81592
AM
12482 int first, second;
12483 char *tmpbuf, *past_reloc;
f3c180ae 12484
91d6fa6a 12485 *rel = gotrel[j].rel[object_64bit];
f3c180ae 12486
3956db08
JB
12487 if (types)
12488 {
12489 if (flag_code != CODE_64BIT)
40fb9820
L
12490 {
12491 types->bitfield.imm32 = 1;
12492 types->bitfield.disp32 = 1;
12493 }
3956db08
JB
12494 else
12495 *types = gotrel[j].types64;
12496 }
12497
844bf810 12498 if (gotrel[j].need_GOT_symbol && GOT_symbol == NULL)
f3c180ae
AM
12499 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
12500
28f81592 12501 /* The length of the first part of our input line. */
f3c180ae 12502 first = cp - input_line_pointer;
28f81592
AM
12503
12504 /* The second part goes from after the reloc token until
67c11a9b 12505 (and including) an end_of_line char or comma. */
28f81592 12506 past_reloc = cp + 1 + len;
67c11a9b
AM
12507 cp = past_reloc;
12508 while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
12509 ++cp;
12510 second = cp + 1 - past_reloc;
28f81592
AM
12511
12512 /* Allocate and copy string. The trailing NUL shouldn't
12513 be necessary, but be safe. */
add39d23 12514 tmpbuf = XNEWVEC (char, first + second + 2);
f3c180ae 12515 memcpy (tmpbuf, input_line_pointer, first);
0787a12d
AM
12516 if (second != 0 && *past_reloc != ' ')
12517 /* Replace the relocation token with ' ', so that
12518 errors like foo@GOTOFF1 will be detected. */
12519 tmpbuf[first++] = ' ';
af89796a
L
12520 else
12521 /* Increment length by 1 if the relocation token is
12522 removed. */
12523 len++;
12524 if (adjust)
12525 *adjust = len;
0787a12d
AM
12526 memcpy (tmpbuf + first, past_reloc, second);
12527 tmpbuf[first + second] = '\0';
f3c180ae
AM
12528 return tmpbuf;
12529 }
12530
4fa24527
JB
12531 as_bad (_("@%s reloc is not supported with %d-bit output format"),
12532 gotrel[j].str, 1 << (5 + object_64bit));
f3c180ae
AM
12533 return NULL;
12534 }
12535 }
12536
12537 /* Might be a symbol version string. Don't as_bad here. */
12538 return NULL;
12539}
4e4f7c87 12540#endif
f3c180ae 12541
62ebcb5c 12542bfd_reloc_code_real_type
e3bb37b5 12543x86_cons (expressionS *exp, int size)
f3c180ae 12544{
62ebcb5c
AM
12545 bfd_reloc_code_real_type got_reloc = NO_RELOC;
12546
6b50f5f4
JB
12547 intel_syntax = -intel_syntax;
12548 exp->X_md = 0;
5cc00775 12549 expr_mode = expr_operator_none;
6b50f5f4 12550
2748c1b1
L
12551#if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
12552 && !defined (LEX_AT)) \
12553 || defined (TE_PE)
4fa24527 12554 if (size == 4 || (object_64bit && size == 8))
f3c180ae
AM
12555 {
12556 /* Handle @GOTOFF and the like in an expression. */
12557 char *save;
12558 char *gotfree_input_line;
4a57f2cf 12559 int adjust = 0;
f3c180ae
AM
12560
12561 save = input_line_pointer;
d258b828 12562 gotfree_input_line = lex_got (&got_reloc, &adjust, NULL);
f3c180ae
AM
12563 if (gotfree_input_line)
12564 input_line_pointer = gotfree_input_line;
12565
12566 expression (exp);
12567
12568 if (gotfree_input_line)
12569 {
12570 /* expression () has merrily parsed up to the end of line,
12571 or a comma - in the wrong buffer. Transfer how far
12572 input_line_pointer has moved to the right buffer. */
12573 input_line_pointer = (save
12574 + (input_line_pointer - gotfree_input_line)
12575 + adjust);
12576 free (gotfree_input_line);
3992d3b7
AM
12577 if (exp->X_op == O_constant
12578 || exp->X_op == O_absent
12579 || exp->X_op == O_illegal
0398aac5 12580 || exp->X_op == O_register
3992d3b7
AM
12581 || exp->X_op == O_big)
12582 {
12583 char c = *input_line_pointer;
12584 *input_line_pointer = 0;
12585 as_bad (_("missing or invalid expression `%s'"), save);
12586 *input_line_pointer = c;
12587 }
b9519cfe
L
12588 else if ((got_reloc == BFD_RELOC_386_PLT32
12589 || got_reloc == BFD_RELOC_X86_64_PLT32)
12590 && exp->X_op != O_symbol)
12591 {
12592 char c = *input_line_pointer;
12593 *input_line_pointer = 0;
12594 as_bad (_("invalid PLT expression `%s'"), save);
12595 *input_line_pointer = c;
12596 }
f3c180ae
AM
12597 }
12598 }
12599 else
6b50f5f4 12600#endif
f3c180ae 12601 expression (exp);
ee86248c
JB
12602
12603 intel_syntax = -intel_syntax;
12604
12605 if (intel_syntax)
12606 i386_intel_simplify (exp);
62ebcb5c 12607
a442cac5 12608 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
5cc00775
JB
12609 if (size <= 4 && expr_mode == expr_operator_present
12610 && exp->X_op == O_constant && !object_64bit)
a442cac5
JB
12611 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
12612
62ebcb5c 12613 return got_reloc;
f3c180ae 12614}
f3c180ae 12615
9f32dd5b
L
12616static void
12617signed_cons (int size)
6482c264 12618{
a442cac5 12619 if (object_64bit)
d182319b
JB
12620 cons_sign = 1;
12621 cons (size);
12622 cons_sign = -1;
6482c264
NC
12623}
12624
edd67638
JB
12625static void
12626s_insn (int dummy ATTRIBUTE_UNUSED)
12627{
393fbe8d 12628 char mnemonic[MAX_MNEM_SIZE], *line = input_line_pointer, *ptr;
edd67638
JB
12629 char *saved_ilp = find_end_of_line (line, false), saved_char;
12630 const char *end;
12631 unsigned int j;
12632 valueT val;
12633 bool vex = false, xop = false, evex = false;
b5482fe5 12634 struct last_insn *last_insn;
edd67638
JB
12635
12636 init_globals ();
12637
12638 saved_char = *saved_ilp;
12639 *saved_ilp = 0;
12640
12641 end = parse_insn (line, mnemonic, true);
12642 if (end == NULL)
12643 {
12644 bad:
12645 *saved_ilp = saved_char;
12646 ignore_rest_of_line ();
d0c2e3ec 12647 i.tm.mnem_off = 0;
edd67638
JB
12648 return;
12649 }
12650 line += end - line;
12651
d3b01414
JB
12652 current_templates.start = &i.tm;
12653 current_templates.end = &i.tm + 1;
edd67638 12654 i.tm.mnem_off = MN__insn;
393fbe8d 12655 i.tm.extension_opcode = None;
edd67638
JB
12656
12657 if (startswith (line, "VEX")
12658 && (line[3] == '.' || is_space_char (line[3])))
12659 {
12660 vex = true;
12661 line += 3;
12662 }
12663 else if (startswith (line, "XOP") && ISDIGIT (line[3]))
12664 {
12665 char *e;
12666 unsigned long n = strtoul (line + 3, &e, 16);
12667
12668 if (e == line + 5 && n >= 0x08 && n <= 0x1f
12669 && (*e == '.' || is_space_char (*e)))
12670 {
12671 xop = true;
d0c2e3ec
JB
12672 /* Arrange for build_vex_prefix() to emit 0x8f. */
12673 i.tm.opcode_space = SPACE_XOP08;
12674 i.insn_opcode_space = n;
edd67638
JB
12675 line = e;
12676 }
12677 }
12678 else if (startswith (line, "EVEX")
12679 && (line[4] == '.' || is_space_char (line[4])))
12680 {
12681 evex = true;
12682 line += 4;
12683 }
12684
12685 if (vex || xop
e346d50a 12686 ? i.encoding == encoding_evex
edd67638 12687 : evex
e346d50a
JB
12688 ? i.encoding == encoding_vex
12689 || i.encoding == encoding_vex3
12690 : i.encoding != encoding_default)
edd67638
JB
12691 {
12692 as_bad (_("pseudo-prefix conflicts with encoding specifier"));
12693 goto bad;
12694 }
12695
e346d50a
JB
12696 if (line > end && i.encoding == encoding_default)
12697 i.encoding = evex ? encoding_evex : encoding_vex;
0ff3b7d0 12698
e346d50a 12699 if (i.encoding != encoding_default)
1adecddd
JB
12700 {
12701 /* Only address size and segment override prefixes are permitted with
12702 VEX/XOP/EVEX encodings. */
12703 const unsigned char *p = i.prefix;
12704
12705 for (j = 0; j < ARRAY_SIZE (i.prefix); ++j, ++p)
12706 {
12707 if (!*p)
12708 continue;
12709
12710 switch (j)
12711 {
12712 case SEG_PREFIX:
12713 case ADDR_PREFIX:
12714 break;
12715 default:
12716 as_bad (_("illegal prefix used with VEX/XOP/EVEX"));
12717 goto bad;
12718 }
12719 }
12720 }
12721
edd67638
JB
12722 if (line > end && *line == '.')
12723 {
d0c2e3ec
JB
12724 /* Length specifier (VEX.L, XOP.L, EVEX.L'L). */
12725 switch (line[1])
12726 {
12727 case 'L':
12728 switch (line[2])
12729 {
12730 case '0':
12731 if (evex)
12732 i.tm.opcode_modifier.evex = EVEX128;
12733 else
12734 i.tm.opcode_modifier.vex = VEX128;
12735 break;
12736
12737 case '1':
12738 if (evex)
12739 i.tm.opcode_modifier.evex = EVEX256;
12740 else
12741 i.tm.opcode_modifier.vex = VEX256;
12742 break;
12743
12744 case '2':
12745 if (evex)
12746 i.tm.opcode_modifier.evex = EVEX512;
12747 break;
12748
12749 case '3':
12750 if (evex)
12751 i.tm.opcode_modifier.evex = EVEX_L3;
12752 break;
12753
12754 case 'I':
12755 if (line[3] == 'G')
12756 {
12757 if (evex)
12758 i.tm.opcode_modifier.evex = EVEXLIG;
12759 else
12760 i.tm.opcode_modifier.vex = VEXScalar; /* LIG */
12761 ++line;
12762 }
12763 break;
12764 }
12765
12766 if (i.tm.opcode_modifier.vex || i.tm.opcode_modifier.evex)
12767 line += 3;
12768 break;
12769
12770 case '1':
12771 if (line[2] == '2' && line[3] == '8')
12772 {
12773 if (evex)
12774 i.tm.opcode_modifier.evex = EVEX128;
12775 else
12776 i.tm.opcode_modifier.vex = VEX128;
12777 line += 4;
12778 }
12779 break;
12780
12781 case '2':
12782 if (line[2] == '5' && line[3] == '6')
12783 {
12784 if (evex)
12785 i.tm.opcode_modifier.evex = EVEX256;
12786 else
12787 i.tm.opcode_modifier.vex = VEX256;
12788 line += 4;
12789 }
12790 break;
12791
12792 case '5':
12793 if (evex && line[2] == '1' && line[3] == '2')
12794 {
12795 i.tm.opcode_modifier.evex = EVEX512;
12796 line += 4;
12797 }
12798 break;
12799 }
12800 }
12801
12802 if (line > end && *line == '.')
12803 {
12804 /* embedded prefix (VEX.pp, XOP.pp, EVEX.pp). */
12805 switch (line[1])
12806 {
12807 case 'N':
12808 if (line[2] == 'P')
12809 line += 3;
12810 break;
12811
12812 case '6':
12813 if (line[2] == '6')
12814 {
12815 i.tm.opcode_modifier.opcodeprefix = PREFIX_0X66;
12816 line += 3;
12817 }
12818 break;
12819
12820 case 'F': case 'f':
12821 if (line[2] == '3')
12822 {
12823 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
12824 line += 3;
12825 }
12826 else if (line[2] == '2')
12827 {
12828 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF2;
12829 line += 3;
12830 }
12831 break;
12832 }
12833 }
12834
12835 if (line > end && !xop && *line == '.')
12836 {
12837 /* Encoding space (VEX.mmmmm, EVEX.mmmm). */
12838 switch (line[1])
12839 {
12840 case '0':
12841 if (TOUPPER (line[2]) != 'F')
12842 break;
12843 if (line[3] == '.' || is_space_char (line[3]))
12844 {
12845 i.insn_opcode_space = SPACE_0F;
12846 line += 3;
12847 }
12848 else if (line[3] == '3'
12849 && (line[4] == '8' || TOUPPER (line[4]) == 'A')
12850 && (line[5] == '.' || is_space_char (line[5])))
12851 {
12852 i.insn_opcode_space = line[4] == '8' ? SPACE_0F38 : SPACE_0F3A;
12853 line += 5;
12854 }
12855 break;
12856
12857 case 'M':
12858 if (ISDIGIT (line[2]) && line[2] != '0')
12859 {
12860 char *e;
12861 unsigned long n = strtoul (line + 2, &e, 10);
12862
12863 if (n <= (evex ? 15 : 31)
12864 && (*e == '.' || is_space_char (*e)))
12865 {
12866 i.insn_opcode_space = n;
12867 line = e;
12868 }
12869 }
12870 break;
12871 }
12872 }
12873
12874 if (line > end && *line == '.' && line[1] == 'W')
12875 {
12876 /* VEX.W, XOP.W, EVEX.W */
12877 switch (line[2])
12878 {
12879 case '0':
12880 i.tm.opcode_modifier.vexw = VEXW0;
12881 break;
12882
12883 case '1':
12884 i.tm.opcode_modifier.vexw = VEXW1;
12885 break;
12886
12887 case 'I':
12888 if (line[3] == 'G')
12889 {
12890 i.tm.opcode_modifier.vexw = VEXWIG;
12891 ++line;
12892 }
12893 break;
12894 }
12895
12896 if (i.tm.opcode_modifier.vexw)
12897 line += 3;
12898 }
12899
12900 if (line > end && *line && !is_space_char (*line))
12901 {
12902 /* Improve diagnostic a little. */
12903 if (*line == '.' && line[1] && !is_space_char (line[1]))
12904 ++line;
12905 goto done;
edd67638
JB
12906 }
12907
393fbe8d
JB
12908 /* Before processing the opcode expression, find trailing "+r" or
12909 "/<digit>" specifiers. */
12910 for (ptr = line; ; ++ptr)
12911 {
12912 unsigned long n;
12913 char *e;
12914
12915 ptr = strpbrk (ptr, "+/,");
12916 if (ptr == NULL || *ptr == ',')
12917 break;
12918
12919 if (*ptr == '+' && ptr[1] == 'r'
12920 && (ptr[2] == ',' || (is_space_char (ptr[2]) && ptr[3] == ',')))
12921 {
12922 *ptr = ' ';
12923 ptr[1] = ' ';
12924 i.short_form = true;
12925 break;
12926 }
12927
12928 if (*ptr == '/' && ISDIGIT (ptr[1])
12929 && (n = strtoul (ptr + 1, &e, 8)) < 8
12930 && e == ptr + 2
12931 && (ptr[2] == ',' || (is_space_char (ptr[2]) && ptr[3] == ',')))
12932 {
12933 *ptr = ' ';
12934 ptr[1] = ' ';
12935 i.tm.extension_opcode = n;
12936 i.tm.opcode_modifier.modrm = 1;
12937 break;
12938 }
12939 }
12940
edd67638
JB
12941 input_line_pointer = line;
12942 val = get_absolute_expression ();
12943 line = input_line_pointer;
12944
393fbe8d
JB
12945 if (i.short_form && (val & 7))
12946 as_warn ("`+r' assumes low three opcode bits to be clear");
12947
edd67638
JB
12948 for (j = 1; j < sizeof(val); ++j)
12949 if (!(val >> (j * 8)))
12950 break;
12951
12952 /* Trim off a prefix if present. */
12953 if (j > 1 && !vex && !xop && !evex)
12954 {
12955 uint8_t byte = val >> ((j - 1) * 8);
12956
12957 switch (byte)
12958 {
12959 case DATA_PREFIX_OPCODE:
12960 case REPE_PREFIX_OPCODE:
12961 case REPNE_PREFIX_OPCODE:
12962 if (!add_prefix (byte))
12963 goto bad;
12964 val &= ((uint64_t)1 << (--j * 8)) - 1;
12965 break;
12966 }
12967 }
12968
6804f42c
JB
12969 /* Parse operands, if any, before evaluating encoding space. */
12970 if (*line == ',')
12971 {
12972 i.memshift = -1;
12973
12974 ptr = parse_operands (line + 1, &i386_mnemonics[MN__insn]);
12975 this_operand = -1;
12976 if (!ptr)
12977 goto bad;
12978 line = ptr;
12979
12980 if (!i.operands)
12981 {
12982 as_bad (_("expecting operand after ','; got nothing"));
12983 goto done;
12984 }
12985
12986 if (i.mem_operands > 1)
12987 {
12988 as_bad (_("too many memory references for `%s'"),
12989 &i386_mnemonics[MN__insn]);
12990 goto done;
12991 }
12992
12993 /* No need to distinguish encoding_evex and encoding_evex512. */
12994 if (i.encoding == encoding_evex512)
12995 i.encoding = encoding_evex;
12996 }
12997
edd67638 12998 /* Trim off encoding space. */
d0c2e3ec 12999 if (j > 1 && !i.insn_opcode_space && (val >> ((j - 1) * 8)) == 0x0f)
edd67638
JB
13000 {
13001 uint8_t byte = val >> ((--j - 1) * 8);
13002
d0c2e3ec 13003 i.insn_opcode_space = SPACE_0F;
6804f42c
JB
13004 switch (byte & -(j > 1 && !i.rex2_encoding
13005 && (i.encoding != encoding_egpr || evex)))
edd67638
JB
13006 {
13007 case 0x38:
d0c2e3ec 13008 i.insn_opcode_space = SPACE_0F38;
edd67638
JB
13009 --j;
13010 break;
13011 case 0x3a:
d0c2e3ec 13012 i.insn_opcode_space = SPACE_0F3A;
edd67638
JB
13013 --j;
13014 break;
13015 }
d0c2e3ec 13016 i.tm.opcode_space = i.insn_opcode_space;
edd67638
JB
13017 val &= ((uint64_t)1 << (j * 8)) - 1;
13018 }
d0c2e3ec
JB
13019 if (!i.tm.opcode_space && (vex || evex))
13020 /* Arrange for build_vex_prefix() to properly emit 0xC4/0xC5.
13021 Also avoid hitting abort() there or in build_evex_prefix(). */
13022 i.tm.opcode_space = i.insn_opcode_space == SPACE_0F ? SPACE_0F
13023 : SPACE_0F38;
edd67638
JB
13024
13025 if (j > 2)
13026 {
13027 as_bad (_("opcode residual (%#"PRIx64") too wide"), (uint64_t) val);
6804f42c 13028 goto done;
edd67638
JB
13029 }
13030 i.opcode_length = j;
0ff3b7d0
JB
13031
13032 /* Handle operands, if any. */
6804f42c 13033 if (i.operands)
0ff3b7d0
JB
13034 {
13035 i386_operand_type combined;
f7377a91 13036 expressionS *disp_exp = NULL;
0ff3b7d0
JB
13037 bool changed;
13038
eb3f3841
JB
13039 if (i.encoding == encoding_egpr)
13040 {
13041 if (vex || xop)
13042 {
13043 as_bad (_("eGPR use conflicts with encoding specifier"));
13044 goto done;
13045 }
13046 if (evex)
13047 i.encoding = encoding_evex;
13048 else
13049 i.encoding = encoding_default;
13050 }
13051
0ff3b7d0
JB
13052 /* Are we to emit ModR/M encoding? */
13053 if (!i.short_form
13054 && (i.mem_operands
e346d50a 13055 || i.reg_operands > (i.encoding != encoding_default)
0ff3b7d0
JB
13056 || i.tm.extension_opcode != None))
13057 i.tm.opcode_modifier.modrm = 1;
13058
13059 if (!i.tm.opcode_modifier.modrm
13060 && (i.reg_operands
e346d50a 13061 > i.short_form + 0U + (i.encoding != encoding_default)
0ff3b7d0
JB
13062 || i.mem_operands))
13063 {
13064 as_bad (_("too many register/memory operands"));
13065 goto done;
13066 }
13067
13068 /* Enforce certain constraints on operands. */
13069 switch (i.reg_operands + i.mem_operands
13070 + (i.tm.extension_opcode != None))
13071 {
13072 case 0:
13073 if (i.short_form)
13074 {
13075 as_bad (_("too few register/memory operands"));
13076 goto done;
13077 }
13078 /* Fall through. */
13079 case 1:
13080 if (i.tm.opcode_modifier.modrm)
13081 {
13082 as_bad (_("too few register/memory operands"));
13083 goto done;
13084 }
13085 break;
13086
13087 case 2:
13088 break;
13089
13090 case 4:
13091 if (i.imm_operands
13092 && (i.op[0].imms->X_op != O_constant
13093 || !fits_in_imm4 (i.op[0].imms->X_add_number)))
13094 {
13095 as_bad (_("constant doesn't fit in %d bits"), evex ? 3 : 4);
13096 goto done;
13097 }
13098 /* Fall through. */
13099 case 3:
e346d50a 13100 if (i.encoding != encoding_default)
0ff3b7d0
JB
13101 {
13102 i.tm.opcode_modifier.vexvvvv = 1;
13103 break;
13104 }
13105 /* Fall through. */
13106 default:
13107 as_bad (_("too many register/memory operands"));
13108 goto done;
13109 }
13110
13111 /* Bring operands into canonical order (imm, mem, reg). */
13112 do
13113 {
13114 changed = false;
13115
13116 for (j = 1; j < i.operands; ++j)
13117 {
13118 if ((!operand_type_check (i.types[j - 1], imm)
13119 && operand_type_check (i.types[j], imm))
13120 || (i.types[j - 1].bitfield.class != ClassNone
13121 && i.types[j].bitfield.class == ClassNone))
13122 {
13123 swap_2_operands (j - 1, j);
13124 changed = true;
13125 }
13126 }
13127 }
13128 while (changed);
13129
13130 /* For Intel syntax swap the order of register operands. */
13131 if (intel_syntax)
13132 switch (i.reg_operands)
13133 {
13134 case 0:
13135 case 1:
13136 break;
13137
13138 case 4:
13139 swap_2_operands (i.imm_operands + i.mem_operands + 1, i.operands - 2);
13140 /* Fall through. */
13141 case 3:
13142 case 2:
13143 swap_2_operands (i.imm_operands + i.mem_operands, i.operands - 1);
13144 break;
13145
13146 default:
13147 abort ();
13148 }
13149
13150 /* Enforce constraints when using VSIB. */
13151 if (i.index_reg
13152 && (i.index_reg->reg_type.bitfield.xmmword
13153 || i.index_reg->reg_type.bitfield.ymmword
13154 || i.index_reg->reg_type.bitfield.zmmword))
13155 {
e346d50a 13156 if (i.encoding == encoding_default)
0ff3b7d0
JB
13157 {
13158 as_bad (_("VSIB unavailable with legacy encoding"));
13159 goto done;
13160 }
13161
e346d50a 13162 if (i.encoding == encoding_evex
0ff3b7d0
JB
13163 && i.reg_operands > 1)
13164 {
13165 /* We could allow two register operands, encoding the 2nd one in
13166 an 8-bit immediate like for 4-register-operand insns, but that
13167 would require ugly fiddling with process_operands() and/or
13168 build_modrm_byte(). */
13169 as_bad (_("too many register operands with VSIB"));
13170 goto done;
13171 }
13172
13173 i.tm.opcode_modifier.sib = 1;
13174 }
13175
13176 /* Establish operand size encoding. */
13177 operand_type_set (&combined, 0);
f7377a91 13178
0ff3b7d0
JB
13179 for (j = i.imm_operands; j < i.operands; ++j)
13180 {
3e4a511b 13181 /* Look for 8-bit operands that use old registers. */
e346d50a 13182 if (i.encoding != encoding_default
3e4a511b
JB
13183 && flag_code == CODE_64BIT
13184 && i.types[j].bitfield.class == Reg
13185 && i.types[j].bitfield.byte
13186 && !(i.op[j].regs->reg_flags & RegRex64)
13187 && i.op[j].regs->reg_num > 3)
13188 as_bad (_("can't encode register '%s%s' with VEX/XOP/EVEX"),
13189 register_prefix, i.op[j].regs->reg_name);
13190
0ff3b7d0
JB
13191 i.types[j].bitfield.instance = InstanceNone;
13192
13193 if (operand_type_check (i.types[j], disp))
f7377a91
JB
13194 {
13195 i.types[j].bitfield.baseindex = 1;
13196 disp_exp = i.op[j].disps;
13197 }
13198
13199 if (evex && i.types[j].bitfield.baseindex)
13200 {
13201 unsigned int n = i.memshift;
13202
13203 if (i.types[j].bitfield.byte)
13204 n = 0;
13205 else if (i.types[j].bitfield.word)
13206 n = 1;
13207 else if (i.types[j].bitfield.dword)
13208 n = 2;
13209 else if (i.types[j].bitfield.qword)
13210 n = 3;
13211 else if (i.types[j].bitfield.xmmword)
13212 n = 4;
13213 else if (i.types[j].bitfield.ymmword)
13214 n = 5;
13215 else if (i.types[j].bitfield.zmmword)
13216 n = 6;
13217
13218 if (i.memshift < 32 && n != i.memshift)
13219 as_warn ("conflicting memory operand size specifiers");
13220 i.memshift = n;
13221 }
0ff3b7d0
JB
13222
13223 if ((i.broadcast.type || i.broadcast.bytes)
13224 && j == i.broadcast.operand)
13225 continue;
13226
13227 combined = operand_type_or (combined, i.types[j]);
13228 combined.bitfield.class = ClassNone;
13229 }
13230
f7377a91
JB
13231 switch ((i.broadcast.type ? i.broadcast.type : 1)
13232 << (i.memshift < 32 ? i.memshift : 0))
13233 {
13234 case 64: combined.bitfield.zmmword = 1; break;
13235 case 32: combined.bitfield.ymmword = 1; break;
13236 case 16: combined.bitfield.xmmword = 1; break;
13237 case 8: combined.bitfield.qword = 1; break;
13238 case 4: combined.bitfield.dword = 1; break;
13239 }
13240
e346d50a 13241 if (i.encoding == encoding_default)
0ff3b7d0
JB
13242 {
13243 if (flag_code == CODE_64BIT && combined.bitfield.qword)
13244 i.rex |= REX_W;
13245 else if ((flag_code == CODE_16BIT ? combined.bitfield.dword
13246 : combined.bitfield.word)
13247 && !add_prefix (DATA_PREFIX_OPCODE))
13248 goto done;
13249 }
13250 else if (!i.tm.opcode_modifier.vexw)
13251 {
13252 if (flag_code == CODE_64BIT)
13253 {
13254 if (combined.bitfield.qword)
13255 i.tm.opcode_modifier.vexw = VEXW1;
13256 else if (combined.bitfield.dword)
13257 i.tm.opcode_modifier.vexw = VEXW0;
13258 }
13259
13260 if (!i.tm.opcode_modifier.vexw)
13261 i.tm.opcode_modifier.vexw = VEXWIG;
13262 }
13263
13264 if (vex || xop)
13265 {
13266 if (!i.tm.opcode_modifier.vex)
13267 {
13268 if (combined.bitfield.ymmword)
13269 i.tm.opcode_modifier.vex = VEX256;
13270 else if (combined.bitfield.xmmword)
13271 i.tm.opcode_modifier.vex = VEX128;
13272 }
13273 }
13274 else if (evex)
13275 {
13276 if (!i.tm.opcode_modifier.evex)
13277 {
13278 /* Do _not_ consider AVX512VL here. */
13279 if (i.rounding.type != rc_none || combined.bitfield.zmmword)
13280 i.tm.opcode_modifier.evex = EVEX512;
13281 else if (combined.bitfield.ymmword)
13282 i.tm.opcode_modifier.evex = EVEX256;
13283 else if (combined.bitfield.xmmword)
13284 i.tm.opcode_modifier.evex = EVEX128;
13285 }
f7377a91
JB
13286
13287 if (i.memshift >= 32)
13288 {
13289 unsigned int n = 0;
13290
13291 switch (i.tm.opcode_modifier.evex)
13292 {
13293 case EVEX512: n = 64; break;
13294 case EVEX256: n = 32; break;
13295 case EVEX128: n = 16; break;
13296 }
13297
13298 if (i.broadcast.type)
13299 n /= i.broadcast.type;
13300
13301 if (n > 0)
13302 for (i.memshift = 0; !(n & 1); n >>= 1)
13303 ++i.memshift;
13304 else if (disp_exp != NULL && disp_exp->X_op == O_constant
13305 && disp_exp->X_add_number != 0
13306 && i.disp_encoding != disp_encoding_32bit)
13307 {
13308 if (!quiet_warnings)
13309 as_warn ("cannot determine memory operand size");
13310 i.disp_encoding = disp_encoding_32bit;
13311 }
13312 }
0ff3b7d0
JB
13313 }
13314
f7377a91
JB
13315 if (i.memshift >= 32)
13316 i.memshift = 0;
13317 else if (!evex)
e346d50a 13318 i.encoding = encoding_error;
f7377a91 13319
0ff3b7d0
JB
13320 if (i.disp_operands && !optimize_disp (&i.tm))
13321 goto done;
13322
c032bc4f
JB
13323 /* Establish size for immediate operands. */
13324 for (j = 0; j < i.imm_operands; ++j)
13325 {
13326 expressionS *expP = i.op[j].imms;
13327
13328 gas_assert (operand_type_check (i.types[j], imm));
13329 operand_type_set (&i.types[j], 0);
13330
13331 if (i.imm_bits[j] > 32)
13332 i.types[j].bitfield.imm64 = 1;
13333 else if (i.imm_bits[j] > 16)
13334 {
13335 if (flag_code == CODE_64BIT && (i.flags[j] & Operand_Signed))
13336 i.types[j].bitfield.imm32s = 1;
13337 else
13338 i.types[j].bitfield.imm32 = 1;
13339 }
13340 else if (i.imm_bits[j] > 8)
13341 i.types[j].bitfield.imm16 = 1;
13342 else if (i.imm_bits[j] > 0)
13343 {
13344 if (i.flags[j] & Operand_Signed)
13345 i.types[j].bitfield.imm8s = 1;
13346 else
13347 i.types[j].bitfield.imm8 = 1;
13348 }
13349 else if (expP->X_op == O_constant)
13350 {
13351 i.types[j] = smallest_imm_type (expP->X_add_number);
13352 i.types[j].bitfield.imm1 = 0;
13353 /* Oddly enough imm_size() checks imm64 first, so the bit needs
13354 zapping since smallest_imm_type() sets it unconditionally. */
13355 if (flag_code != CODE_64BIT)
13356 {
13357 i.types[j].bitfield.imm64 = 0;
13358 i.types[j].bitfield.imm32s = 0;
13359 i.types[j].bitfield.imm32 = 1;
13360 }
13361 else if (i.types[j].bitfield.imm32 || i.types[j].bitfield.imm32s)
13362 i.types[j].bitfield.imm64 = 0;
13363 }
13364 else
13365 /* Non-constant expressions are sized heuristically. */
13366 switch (flag_code)
13367 {
13368 case CODE_64BIT: i.types[j].bitfield.imm32s = 1; break;
13369 case CODE_32BIT: i.types[j].bitfield.imm32 = 1; break;
13370 case CODE_16BIT: i.types[j].bitfield.imm16 = 1; break;
13371 }
13372 }
13373
0ff3b7d0
JB
13374 for (j = 0; j < i.operands; ++j)
13375 i.tm.operand_types[j] = i.types[j];
13376
13377 process_operands ();
13378 }
13379
13380 /* Don't set opcode until after processing operands, to avoid any
13381 potential special casing there. */
13382 i.tm.base_opcode |= val;
13383
e346d50a
JB
13384 if (i.encoding == encoding_error
13385 || (i.encoding != encoding_evex
0ff3b7d0
JB
13386 ? i.broadcast.type || i.broadcast.bytes
13387 || i.rounding.type != rc_none
13388 || i.mask.reg
f586e340
JB
13389 : (i.mem_operands && i.rounding.type != rc_none)
13390 || ((i.broadcast.type || i.broadcast.bytes)
13391 && !(i.flags[i.broadcast.operand] & Operand_Mem))))
0ff3b7d0
JB
13392 {
13393 as_bad (_("conflicting .insn operands"));
13394 goto done;
13395 }
edd67638 13396
d0c2e3ec
JB
13397 if (vex || xop)
13398 {
13399 if (!i.tm.opcode_modifier.vex)
13400 i.tm.opcode_modifier.vex = VEXScalar; /* LIG */
13401
13402 build_vex_prefix (NULL);
13403 i.rex &= REX_OPCODE;
13404 }
13405 else if (evex)
13406 {
13407 if (!i.tm.opcode_modifier.evex)
13408 i.tm.opcode_modifier.evex = EVEXLIG;
13409
13410 build_evex_prefix ();
13411 i.rex &= REX_OPCODE;
13412 }
ce705688
JB
13413 else
13414 establish_rex ();
d0c2e3ec 13415
b5482fe5
JB
13416 last_insn = &seg_info(now_seg)->tc_segment_info_data.last_insn;
13417 output_insn (last_insn);
13418 last_insn->kind = last_insn_directive;
13419 last_insn->name = ".insn directive";
13420 last_insn->file = as_where (&last_insn->line);
edd67638 13421
c7defc53
IB
13422#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
13423 /* PS: SCFI is enabled only for System V AMD64 ABI. The ABI check has been
13424 performed in i386_target_format. */
13425 if (IS_ELF && flag_synth_cfi)
13426 as_bad (_("SCFI: hand-crafting instructions not supported"));
13427#endif
13428
d0c2e3ec 13429 done:
edd67638
JB
13430 *saved_ilp = saved_char;
13431 input_line_pointer = line;
13432
13433 demand_empty_rest_of_line ();
d0c2e3ec
JB
13434
13435 /* Make sure dot_insn() won't yield "true" anymore. */
13436 i.tm.mnem_off = 0;
edd67638
JB
13437}
13438
d182319b 13439#ifdef TE_PE
6482c264 13440static void
7016a5d5 13441pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
6482c264
NC
13442{
13443 expressionS exp;
13444
13445 do
13446 {
13447 expression (&exp);
13448 if (exp.X_op == O_symbol)
13449 exp.X_op = O_secrel;
13450
13451 emit_expr (&exp, 4);
13452 }
13453 while (*input_line_pointer++ == ',');
13454
13455 input_line_pointer--;
13456 demand_empty_rest_of_line ();
13457}
145667f8
MH
13458
13459static void
13460pe_directive_secidx (int dummy ATTRIBUTE_UNUSED)
13461{
13462 expressionS exp;
13463
13464 do
13465 {
13466 expression (&exp);
13467 if (exp.X_op == O_symbol)
13468 exp.X_op = O_secidx;
13469
13470 emit_expr (&exp, 2);
13471 }
13472 while (*input_line_pointer++ == ',');
13473
13474 input_line_pointer--;
13475 demand_empty_rest_of_line ();
13476}
6482c264
NC
13477#endif
13478
7063667e
JB
13479/* Handle Rounding Control / SAE specifiers. */
13480
13481static char *
13482RC_SAE_specifier (const char *pstr)
13483{
13484 unsigned int j;
13485
13486 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); j++)
13487 {
13488 if (!strncmp (pstr, RC_NamesTable[j].name, RC_NamesTable[j].len))
13489 {
13490 if (i.rounding.type != rc_none)
13491 {
13492 as_bad (_("duplicated `{%s}'"), RC_NamesTable[j].name);
13493 return NULL;
13494 }
13495
eb3f3841
JB
13496 switch (i.encoding)
13497 {
13498 case encoding_default:
13499 case encoding_egpr:
13500 i.encoding = encoding_evex512;
13501 break;
13502 case encoding_evex:
13503 case encoding_evex512:
13504 break;
13505 default:
13506 return NULL;
13507 }
a6f3add0 13508
7063667e
JB
13509 i.rounding.type = RC_NamesTable[j].type;
13510
13511 return (char *)(pstr + RC_NamesTable[j].len);
13512 }
13513 }
13514
13515 return NULL;
13516}
13517
43234a1e
L
13518/* Handle Vector operations. */
13519
13520static char *
f70c6814 13521check_VecOperations (char *op_string)
43234a1e
L
13522{
13523 const reg_entry *mask;
13524 const char *saved;
13525 char *end_op;
13526
f70c6814 13527 while (*op_string)
43234a1e
L
13528 {
13529 saved = op_string;
13530 if (*op_string == '{')
13531 {
13532 op_string++;
13533
13534 /* Check broadcasts. */
d34049e8 13535 if (startswith (op_string, "1to"))
43234a1e 13536 {
5273a3cd 13537 unsigned int bcst_type;
43234a1e 13538
5273a3cd 13539 if (i.broadcast.type)
43234a1e
L
13540 goto duplicated_vec_op;
13541
13542 op_string += 3;
13543 if (*op_string == '8')
8e6e0792 13544 bcst_type = 8;
b28d1bda 13545 else if (*op_string == '4')
8e6e0792 13546 bcst_type = 4;
b28d1bda 13547 else if (*op_string == '2')
8e6e0792 13548 bcst_type = 2;
43234a1e
L
13549 else if (*op_string == '1'
13550 && *(op_string+1) == '6')
13551 {
8e6e0792 13552 bcst_type = 16;
43234a1e
L
13553 op_string++;
13554 }
0cc78721
CL
13555 else if (*op_string == '3'
13556 && *(op_string+1) == '2')
13557 {
13558 bcst_type = 32;
13559 op_string++;
13560 }
43234a1e
L
13561 else
13562 {
13563 as_bad (_("Unsupported broadcast: `%s'"), saved);
13564 return NULL;
13565 }
13566 op_string++;
13567
eb3f3841
JB
13568 switch (i.encoding)
13569 {
13570 case encoding_default:
13571 case encoding_egpr:
13572 i.encoding = encoding_evex;
13573 break;
13574 case encoding_evex:
13575 case encoding_evex512:
13576 break;
13577 default:
13578 goto unknown_vec_op;
13579 }
a6f3add0 13580
5273a3cd
JB
13581 i.broadcast.type = bcst_type;
13582 i.broadcast.operand = this_operand;
f7377a91
JB
13583
13584 /* For .insn a data size specifier may be appended. */
13585 if (dot_insn () && *op_string == ':')
13586 goto dot_insn_modifier;
13587 }
13588 /* Check .insn special cases. */
13589 else if (dot_insn () && *op_string == ':')
13590 {
13591 dot_insn_modifier:
c032bc4f 13592 switch (op_string[1])
f7377a91
JB
13593 {
13594 unsigned long n;
13595
c032bc4f 13596 case 'd':
f7377a91
JB
13597 if (i.memshift < 32)
13598 goto duplicated_vec_op;
13599
13600 n = strtoul (op_string + 2, &end_op, 0);
13601 if (n)
13602 for (i.memshift = 0; !(n & 1); n >>= 1)
13603 ++i.memshift;
13604 if (i.memshift < 32 && n == 1)
13605 op_string = end_op;
c032bc4f
JB
13606 break;
13607
13608 case 's': case 'u':
13609 /* This isn't really a "vector" operation, but a sign/size
13610 specifier for immediate operands of .insn. Note that AT&T
13611 syntax handles the same in i386_immediate(). */
13612 if (!intel_syntax)
13613 break;
13614
13615 if (i.imm_bits[this_operand])
13616 goto duplicated_vec_op;
13617
13618 n = strtoul (op_string + 2, &end_op, 0);
13619 if (n && n <= (flag_code == CODE_64BIT ? 64 : 32))
13620 {
13621 i.imm_bits[this_operand] = n;
13622 if (op_string[1] == 's')
13623 i.flags[this_operand] |= Operand_Signed;
13624 op_string = end_op;
13625 }
13626 break;
f7377a91 13627 }
43234a1e
L
13628 }
13629 /* Check masking operation. */
13630 else if ((mask = parse_register (op_string, &end_op)) != NULL)
13631 {
8a6fb3f9
JB
13632 if (mask == &bad_reg)
13633 return NULL;
13634
43234a1e 13635 /* k0 can't be used for write mask. */
f74a6307 13636 if (mask->reg_type.bitfield.class != RegMask || !mask->reg_num)
43234a1e 13637 {
6d2cd6b2
JB
13638 as_bad (_("`%s%s' can't be used for write mask"),
13639 register_prefix, mask->reg_name);
43234a1e
L
13640 return NULL;
13641 }
13642
6225c532 13643 if (!i.mask.reg)
43234a1e 13644 {
6225c532
JB
13645 i.mask.reg = mask;
13646 i.mask.operand = this_operand;
43234a1e 13647 }
6225c532
JB
13648 else if (i.mask.reg->reg_num)
13649 goto duplicated_vec_op;
43234a1e
L
13650 else
13651 {
6225c532 13652 i.mask.reg = mask;
43234a1e
L
13653
13654 /* Only "{z}" is allowed here. No need to check
13655 zeroing mask explicitly. */
6225c532 13656 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
13657 {
13658 as_bad (_("invalid write mask `%s'"), saved);
13659 return NULL;
13660 }
13661 }
13662
13663 op_string = end_op;
13664 }
13665 /* Check zeroing-flag for masking operation. */
13666 else if (*op_string == 'z')
13667 {
6225c532 13668 if (!i.mask.reg)
43234a1e 13669 {
6225c532
JB
13670 i.mask.reg = reg_k0;
13671 i.mask.zeroing = 1;
13672 i.mask.operand = this_operand;
43234a1e
L
13673 }
13674 else
13675 {
6225c532 13676 if (i.mask.zeroing)
43234a1e
L
13677 {
13678 duplicated_vec_op:
13679 as_bad (_("duplicated `%s'"), saved);
13680 return NULL;
13681 }
13682
6225c532 13683 i.mask.zeroing = 1;
43234a1e
L
13684
13685 /* Only "{%k}" is allowed here. No need to check mask
13686 register explicitly. */
6225c532 13687 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
13688 {
13689 as_bad (_("invalid zeroing-masking `%s'"),
13690 saved);
13691 return NULL;
13692 }
13693 }
13694
13695 op_string++;
13696 }
7063667e
JB
13697 else if (intel_syntax
13698 && (op_string = RC_SAE_specifier (op_string)) != NULL)
13699 i.rounding.modifier = true;
43234a1e
L
13700 else
13701 goto unknown_vec_op;
13702
13703 if (*op_string != '}')
13704 {
13705 as_bad (_("missing `}' in `%s'"), saved);
13706 return NULL;
13707 }
13708 op_string++;
0ba3a731
L
13709
13710 /* Strip whitespace since the addition of pseudo prefixes
13711 changed how the scrubber treats '{'. */
13712 if (is_space_char (*op_string))
13713 ++op_string;
13714
43234a1e
L
13715 continue;
13716 }
13717 unknown_vec_op:
13718 /* We don't know this one. */
13719 as_bad (_("unknown vector operation: `%s'"), saved);
13720 return NULL;
13721 }
13722
6225c532 13723 if (i.mask.reg && i.mask.zeroing && !i.mask.reg->reg_num)
6d2cd6b2
JB
13724 {
13725 as_bad (_("zeroing-masking only allowed with write mask"));
13726 return NULL;
13727 }
13728
43234a1e
L
13729 return op_string;
13730}
13731
252b5132 13732static int
70e41ade 13733i386_immediate (char *imm_start)
252b5132
RH
13734{
13735 char *save_input_line_pointer;
f3c180ae 13736 char *gotfree_input_line;
252b5132 13737 segT exp_seg = 0;
47926f60 13738 expressionS *exp;
40fb9820
L
13739 i386_operand_type types;
13740
0dfbf9d7 13741 operand_type_set (&types, ~0);
252b5132
RH
13742
13743 if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
13744 {
31b2323c
L
13745 as_bad (_("at most %d immediate operands are allowed"),
13746 MAX_IMMEDIATE_OPERANDS);
252b5132
RH
13747 return 0;
13748 }
13749
13750 exp = &im_expressions[i.imm_operands++];
520dc8e8 13751 i.op[this_operand].imms = exp;
252b5132
RH
13752
13753 if (is_space_char (*imm_start))
13754 ++imm_start;
13755
13756 save_input_line_pointer = input_line_pointer;
13757 input_line_pointer = imm_start;
13758
d258b828 13759 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
13760 if (gotfree_input_line)
13761 input_line_pointer = gotfree_input_line;
252b5132 13762
5cc00775 13763 expr_mode = expr_operator_none;
252b5132
RH
13764 exp_seg = expression (exp);
13765
c032bc4f
JB
13766 /* For .insn immediates there may be a size specifier. */
13767 if (dot_insn () && *input_line_pointer == '{' && input_line_pointer[1] == ':'
13768 && (input_line_pointer[2] == 's' || input_line_pointer[2] == 'u'))
13769 {
13770 char *e;
13771 unsigned long n = strtoul (input_line_pointer + 3, &e, 0);
13772
13773 if (*e == '}' && n && n <= (flag_code == CODE_64BIT ? 64 : 32))
13774 {
13775 i.imm_bits[this_operand] = n;
13776 if (input_line_pointer[2] == 's')
13777 i.flags[this_operand] |= Operand_Signed;
13778 input_line_pointer = e + 1;
13779 }
13780 }
13781
83183c0c 13782 SKIP_WHITESPACE ();
252b5132 13783 if (*input_line_pointer)
f3c180ae 13784 as_bad (_("junk `%s' after expression"), input_line_pointer);
252b5132
RH
13785
13786 input_line_pointer = save_input_line_pointer;
f3c180ae 13787 if (gotfree_input_line)
ee86248c
JB
13788 {
13789 free (gotfree_input_line);
13790
9aac24b1 13791 if (exp->X_op == O_constant)
ee86248c
JB
13792 exp->X_op = O_illegal;
13793 }
13794
9aac24b1
JB
13795 if (exp_seg == reg_section)
13796 {
13797 as_bad (_("illegal immediate register operand %s"), imm_start);
13798 return 0;
13799 }
13800
ee86248c
JB
13801 return i386_finalize_immediate (exp_seg, exp, types, imm_start);
13802}
252b5132 13803
ee86248c
JB
13804static int
13805i386_finalize_immediate (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
13806 i386_operand_type types, const char *imm_start)
13807{
13808 if (exp->X_op == O_absent || exp->X_op == O_illegal || exp->X_op == O_big)
252b5132 13809 {
313c53d1
L
13810 if (imm_start)
13811 as_bad (_("missing or invalid immediate expression `%s'"),
13812 imm_start);
3992d3b7 13813 return 0;
252b5132 13814 }
3e73aa7c 13815 else if (exp->X_op == O_constant)
252b5132 13816 {
47926f60 13817 /* Size it properly later. */
40fb9820 13818 i.types[this_operand].bitfield.imm64 = 1;
a442cac5
JB
13819
13820 /* If not 64bit, sign/zero extend val, to account for wraparound
13821 when !BFD64. */
5cc00775
JB
13822 if (expr_mode == expr_operator_present
13823 && flag_code != CODE_64BIT && !object_64bit)
a442cac5 13824 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
252b5132 13825 }
4c63da97 13826#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
f86103b7 13827 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
31312f95 13828 && exp_seg != absolute_section
47926f60 13829 && exp_seg != text_section
24eab124
AM
13830 && exp_seg != data_section
13831 && exp_seg != bss_section
13832 && exp_seg != undefined_section
f86103b7 13833 && !bfd_is_com_section (exp_seg))
252b5132 13834 {
d0b47220 13835 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
252b5132
RH
13836 return 0;
13837 }
13838#endif
13839 else
13840 {
13841 /* This is an address. The size of the address will be
24eab124 13842 determined later, depending on destination register,
3e73aa7c 13843 suffix, or the default for the section. */
40fb9820
L
13844 i.types[this_operand].bitfield.imm8 = 1;
13845 i.types[this_operand].bitfield.imm16 = 1;
13846 i.types[this_operand].bitfield.imm32 = 1;
13847 i.types[this_operand].bitfield.imm32s = 1;
13848 i.types[this_operand].bitfield.imm64 = 1;
c6fb90c8
L
13849 i.types[this_operand] = operand_type_and (i.types[this_operand],
13850 types);
252b5132
RH
13851 }
13852
13853 return 1;
13854}
13855
551c1ca1 13856static char *
e3bb37b5 13857i386_scale (char *scale)
252b5132 13858{
551c1ca1
AM
13859 offsetT val;
13860 char *save = input_line_pointer;
252b5132 13861
551c1ca1
AM
13862 input_line_pointer = scale;
13863 val = get_absolute_expression ();
13864
13865 switch (val)
252b5132 13866 {
551c1ca1 13867 case 1:
252b5132
RH
13868 i.log2_scale_factor = 0;
13869 break;
551c1ca1 13870 case 2:
252b5132
RH
13871 i.log2_scale_factor = 1;
13872 break;
551c1ca1 13873 case 4:
252b5132
RH
13874 i.log2_scale_factor = 2;
13875 break;
551c1ca1 13876 case 8:
252b5132
RH
13877 i.log2_scale_factor = 3;
13878 break;
13879 default:
a724f0f4
JB
13880 {
13881 char sep = *input_line_pointer;
13882
13883 *input_line_pointer = '\0';
13884 as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
13885 scale);
13886 *input_line_pointer = sep;
13887 input_line_pointer = save;
13888 return NULL;
13889 }
252b5132 13890 }
29b0f896 13891 if (i.log2_scale_factor != 0 && i.index_reg == 0)
252b5132
RH
13892 {
13893 as_warn (_("scale factor of %d without an index register"),
24eab124 13894 1 << i.log2_scale_factor);
252b5132 13895 i.log2_scale_factor = 0;
252b5132 13896 }
551c1ca1
AM
13897 scale = input_line_pointer;
13898 input_line_pointer = save;
13899 return scale;
252b5132
RH
13900}
13901
252b5132 13902static int
e3bb37b5 13903i386_displacement (char *disp_start, char *disp_end)
252b5132 13904{
29b0f896 13905 expressionS *exp;
252b5132
RH
13906 segT exp_seg = 0;
13907 char *save_input_line_pointer;
f3c180ae 13908 char *gotfree_input_line;
40fb9820
L
13909 int override;
13910 i386_operand_type bigdisp, types = anydisp;
3992d3b7 13911 int ret;
252b5132 13912
31b2323c
L
13913 if (i.disp_operands == MAX_MEMORY_OPERANDS)
13914 {
13915 as_bad (_("at most %d displacement operands are allowed"),
13916 MAX_MEMORY_OPERANDS);
13917 return 0;
13918 }
13919
0dfbf9d7 13920 operand_type_set (&bigdisp, 0);
6f2f06be 13921 if (i.jumpabsolute
48bcea9f 13922 || i.types[this_operand].bitfield.baseindex
d3b01414
JB
13923 || (current_templates.start->opcode_modifier.jump != JUMP
13924 && current_templates.start->opcode_modifier.jump != JUMP_DWORD))
e05278af 13925 {
48bcea9f 13926 i386_addressing_mode ();
e05278af 13927 override = (i.prefix[ADDR_PREFIX] != 0);
40fb9820
L
13928 if (flag_code == CODE_64BIT)
13929 {
a775efc8 13930 bigdisp.bitfield.disp32 = 1;
40fb9820 13931 if (!override)
a775efc8 13932 bigdisp.bitfield.disp64 = 1;
40fb9820
L
13933 }
13934 else if ((flag_code == CODE_16BIT) ^ override)
40fb9820 13935 bigdisp.bitfield.disp16 = 1;
48bcea9f
JB
13936 else
13937 bigdisp.bitfield.disp32 = 1;
e05278af
JB
13938 }
13939 else
13940 {
376cd056
JB
13941 /* For PC-relative branches, the width of the displacement may be
13942 dependent upon data size, but is never dependent upon address size.
13943 Also make sure to not unintentionally match against a non-PC-relative
13944 branch template. */
d3b01414 13945 const insn_template *t = current_templates.start;
5b7c81bd 13946 bool has_intel64 = false;
376cd056 13947
d3b01414 13948 while (++t < current_templates.end)
376cd056
JB
13949 {
13950 if (t->opcode_modifier.jump
d3b01414 13951 != current_templates.start->opcode_modifier.jump)
376cd056 13952 break;
4b5aaf5f 13953 if ((t->opcode_modifier.isa64 >= INTEL64))
5b7c81bd 13954 has_intel64 = true;
376cd056 13955 }
d3b01414 13956 current_templates.end = t;
376cd056 13957
e05278af 13958 override = (i.prefix[DATA_PREFIX] != 0);
40fb9820
L
13959 if (flag_code == CODE_64BIT)
13960 {
376cd056
JB
13961 if ((override || i.suffix == WORD_MNEM_SUFFIX)
13962 && (!intel64 || !has_intel64))
40fb9820
L
13963 bigdisp.bitfield.disp16 = 1;
13964 else
a775efc8 13965 bigdisp.bitfield.disp32 = 1;
40fb9820
L
13966 }
13967 else
e05278af
JB
13968 {
13969 if (!override)
13970 override = (i.suffix == (flag_code != CODE_16BIT
13971 ? WORD_MNEM_SUFFIX
13972 : LONG_MNEM_SUFFIX));
40fb9820
L
13973 bigdisp.bitfield.disp32 = 1;
13974 if ((flag_code == CODE_16BIT) ^ override)
13975 {
13976 bigdisp.bitfield.disp32 = 0;
13977 bigdisp.bitfield.disp16 = 1;
13978 }
e05278af 13979 }
e05278af 13980 }
c6fb90c8
L
13981 i.types[this_operand] = operand_type_or (i.types[this_operand],
13982 bigdisp);
252b5132
RH
13983
13984 exp = &disp_expressions[i.disp_operands];
520dc8e8 13985 i.op[this_operand].disps = exp;
252b5132
RH
13986 i.disp_operands++;
13987 save_input_line_pointer = input_line_pointer;
13988 input_line_pointer = disp_start;
13989 END_STRING_AND_SAVE (disp_end);
13990
13991#ifndef GCC_ASM_O_HACK
13992#define GCC_ASM_O_HACK 0
13993#endif
13994#if GCC_ASM_O_HACK
13995 END_STRING_AND_SAVE (disp_end + 1);
40fb9820 13996 if (i.types[this_operand].bitfield.baseIndex
24eab124 13997 && displacement_string_end[-1] == '+')
252b5132
RH
13998 {
13999 /* This hack is to avoid a warning when using the "o"
24eab124
AM
14000 constraint within gcc asm statements.
14001 For instance:
14002
14003 #define _set_tssldt_desc(n,addr,limit,type) \
14004 __asm__ __volatile__ ( \
14005 "movw %w2,%0\n\t" \
14006 "movw %w1,2+%0\n\t" \
14007 "rorl $16,%1\n\t" \
14008 "movb %b1,4+%0\n\t" \
14009 "movb %4,5+%0\n\t" \
14010 "movb $0,6+%0\n\t" \
14011 "movb %h1,7+%0\n\t" \
14012 "rorl $16,%1" \
14013 : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
14014
14015 This works great except that the output assembler ends
14016 up looking a bit weird if it turns out that there is
14017 no offset. You end up producing code that looks like:
14018
14019 #APP
14020 movw $235,(%eax)
14021 movw %dx,2+(%eax)
14022 rorl $16,%edx
14023 movb %dl,4+(%eax)
14024 movb $137,5+(%eax)
14025 movb $0,6+(%eax)
14026 movb %dh,7+(%eax)
14027 rorl $16,%edx
14028 #NO_APP
14029
47926f60 14030 So here we provide the missing zero. */
24eab124
AM
14031
14032 *displacement_string_end = '0';
252b5132
RH
14033 }
14034#endif
d258b828 14035 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
14036 if (gotfree_input_line)
14037 input_line_pointer = gotfree_input_line;
252b5132 14038
5cc00775 14039 expr_mode = expr_operator_none;
24eab124 14040 exp_seg = expression (exp);
252b5132 14041
636c26b0
AM
14042 SKIP_WHITESPACE ();
14043 if (*input_line_pointer)
14044 as_bad (_("junk `%s' after expression"), input_line_pointer);
14045#if GCC_ASM_O_HACK
14046 RESTORE_END_STRING (disp_end + 1);
14047#endif
636c26b0 14048 input_line_pointer = save_input_line_pointer;
636c26b0 14049 if (gotfree_input_line)
ee86248c
JB
14050 {
14051 free (gotfree_input_line);
14052
14053 if (exp->X_op == O_constant || exp->X_op == O_register)
14054 exp->X_op = O_illegal;
14055 }
14056
14057 ret = i386_finalize_displacement (exp_seg, exp, types, disp_start);
14058
14059 RESTORE_END_STRING (disp_end);
14060
14061 return ret;
14062}
14063
14064static int
14065i386_finalize_displacement (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
14066 i386_operand_type types, const char *disp_start)
14067{
ee86248c 14068 int ret = 1;
636c26b0 14069
24eab124
AM
14070 /* We do this to make sure that the section symbol is in
14071 the symbol table. We will ultimately change the relocation
47926f60 14072 to be relative to the beginning of the section. */
1ae12ab7 14073 if (i.reloc[this_operand] == BFD_RELOC_386_GOTOFF
d6ab8113
JB
14074 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
14075 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
24eab124 14076 {
636c26b0 14077 if (exp->X_op != O_symbol)
3992d3b7 14078 goto inv_disp;
636c26b0 14079
e5cb08ac 14080 if (S_IS_LOCAL (exp->X_add_symbol)
c64efb4b
L
14081 && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section
14082 && S_GET_SEGMENT (exp->X_add_symbol) != expr_section)
24eab124 14083 section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
24eab124
AM
14084 exp->X_op = O_subtract;
14085 exp->X_op_symbol = GOT_symbol;
1ae12ab7 14086 if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
29b0f896 14087 i.reloc[this_operand] = BFD_RELOC_32_PCREL;
d6ab8113
JB
14088 else if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
14089 i.reloc[this_operand] = BFD_RELOC_64;
23df1078 14090 else
29b0f896 14091 i.reloc[this_operand] = BFD_RELOC_32;
24eab124 14092 }
252b5132 14093
3992d3b7
AM
14094 else if (exp->X_op == O_absent
14095 || exp->X_op == O_illegal
ee86248c 14096 || exp->X_op == O_big)
2daf4fd8 14097 {
3992d3b7
AM
14098 inv_disp:
14099 as_bad (_("missing or invalid displacement expression `%s'"),
2daf4fd8 14100 disp_start);
3992d3b7 14101 ret = 0;
2daf4fd8
AM
14102 }
14103
a50187b2
JB
14104 else if (exp->X_op == O_constant)
14105 {
14106 /* Sizing gets taken care of by optimize_disp().
14107
14108 If not 64bit, sign/zero extend val, to account for wraparound
14109 when !BFD64. */
5cc00775
JB
14110 if (expr_mode == expr_operator_present
14111 && flag_code != CODE_64BIT && !object_64bit)
a50187b2
JB
14112 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
14113 }
14114
4c63da97 14115#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
a50187b2 14116 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
3992d3b7
AM
14117 && exp_seg != absolute_section
14118 && exp_seg != text_section
14119 && exp_seg != data_section
14120 && exp_seg != bss_section
14121 && exp_seg != undefined_section
14122 && !bfd_is_com_section (exp_seg))
24eab124 14123 {
d0b47220 14124 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
3992d3b7 14125 ret = 0;
24eab124 14126 }
252b5132 14127#endif
3956db08 14128
d3b01414 14129 else if (current_templates.start->opcode_modifier.jump == JUMP_BYTE)
48bcea9f
JB
14130 i.types[this_operand].bitfield.disp8 = 1;
14131
40fb9820 14132 /* Check if this is a displacement only operand. */
02b83698
JB
14133 if (!i.types[this_operand].bitfield.baseindex)
14134 i.types[this_operand] =
14135 operand_type_or (operand_type_and_not (i.types[this_operand], anydisp),
14136 operand_type_and (i.types[this_operand], types));
3956db08 14137
3992d3b7 14138 return ret;
252b5132
RH
14139}
14140
2abc2bec
JB
14141/* Return the active addressing mode, taking address override and
14142 registers forming the address into consideration. Update the
14143 address override prefix if necessary. */
47926f60 14144
2abc2bec
JB
14145static enum flag_code
14146i386_addressing_mode (void)
252b5132 14147{
be05d201
L
14148 enum flag_code addr_mode;
14149
14150 if (i.prefix[ADDR_PREFIX])
14151 addr_mode = flag_code == CODE_32BIT ? CODE_16BIT : CODE_32BIT;
a23b33b3 14152 else if (flag_code == CODE_16BIT
d3b01414 14153 && is_cpu (current_templates.start, CpuMPX)
a23b33b3
JB
14154 /* Avoid replacing the "16-bit addressing not allowed" diagnostic
14155 from md_assemble() by "is not a valid base/index expression"
14156 when there is a base and/or index. */
14157 && !i.types[this_operand].bitfield.baseindex)
14158 {
14159 /* MPX insn memory operands with neither base nor index must be forced
14160 to use 32-bit addressing in 16-bit mode. */
14161 addr_mode = CODE_32BIT;
14162 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
14163 ++i.prefixes;
14164 gas_assert (!i.types[this_operand].bitfield.disp16);
14165 gas_assert (!i.types[this_operand].bitfield.disp32);
14166 }
be05d201
L
14167 else
14168 {
14169 addr_mode = flag_code;
14170
24eab124 14171#if INFER_ADDR_PREFIX
be05d201
L
14172 if (i.mem_operands == 0)
14173 {
14174 /* Infer address prefix from the first memory operand. */
14175 const reg_entry *addr_reg = i.base_reg;
14176
14177 if (addr_reg == NULL)
14178 addr_reg = i.index_reg;
eecb386c 14179
be05d201
L
14180 if (addr_reg)
14181 {
e968fc9b 14182 if (addr_reg->reg_type.bitfield.dword)
be05d201
L
14183 addr_mode = CODE_32BIT;
14184 else if (flag_code != CODE_64BIT
dc821c5f 14185 && addr_reg->reg_type.bitfield.word)
be05d201
L
14186 addr_mode = CODE_16BIT;
14187
14188 if (addr_mode != flag_code)
14189 {
14190 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
14191 i.prefixes += 1;
14192 /* Change the size of any displacement too. At most one
14193 of Disp16 or Disp32 is set.
14194 FIXME. There doesn't seem to be any real need for
14195 separate Disp16 and Disp32 flags. The same goes for
14196 Imm16 and Imm32. Removing them would probably clean
14197 up the code quite a lot. */
14198 if (flag_code != CODE_64BIT
14199 && (i.types[this_operand].bitfield.disp16
14200 || i.types[this_operand].bitfield.disp32))
05909f23
JB
14201 {
14202 static const i386_operand_type disp16_32 = {
14203 .bitfield = { .disp16 = 1, .disp32 = 1 }
14204 };
14205
14206 i.types[this_operand]
14207 = operand_type_xor (i.types[this_operand], disp16_32);
14208 }
be05d201
L
14209 }
14210 }
14211 }
24eab124 14212#endif
be05d201
L
14213 }
14214
2abc2bec
JB
14215 return addr_mode;
14216}
14217
14218/* Make sure the memory operand we've been dealt is valid.
14219 Return 1 on success, 0 on a failure. */
14220
14221static int
14222i386_index_check (const char *operand_string)
14223{
14224 const char *kind = "base/index";
14225 enum flag_code addr_mode = i386_addressing_mode ();
d3b01414 14226 const insn_template *t = current_templates.end - 1;
2abc2bec 14227
77a19f0e 14228 if (t->opcode_modifier.isstring)
fc0763e6
JB
14229 {
14230 /* Memory operands of string insns are special in that they only allow
14231 a single register (rDI, rSI, or rBX) as their memory address. */
be05d201 14232 const reg_entry *expected_reg;
069a1561 14233 static const char di_si[][2][4] =
be05d201
L
14234 {
14235 { "esi", "edi" },
14236 { "si", "di" },
14237 { "rsi", "rdi" }
14238 };
069a1561 14239 static const char bx[][4] = { "ebx", "bx", "rbx" };
fc0763e6
JB
14240
14241 kind = "string address";
14242
a152332d 14243 if (t->opcode_modifier.prefixok == PrefixRep)
fc0763e6 14244 {
77a19f0e 14245 int es_op = t->opcode_modifier.isstring - IS_STRING_ES_OP0;
51c8edf6 14246 int op = 0;
fc0763e6 14247
77a19f0e 14248 if (!t->operand_types[0].bitfield.baseindex
fc0763e6 14249 || ((!i.mem_operands != !intel_syntax)
77a19f0e 14250 && t->operand_types[1].bitfield.baseindex))
51c8edf6 14251 op = 1;
fe0e921f
AM
14252 expected_reg
14253 = (const reg_entry *) str_hash_find (reg_hash,
14254 di_si[addr_mode][op == es_op]);
fc0763e6
JB
14255 }
14256 else
fe0e921f
AM
14257 expected_reg
14258 = (const reg_entry *)str_hash_find (reg_hash, bx[addr_mode]);
fc0763e6 14259
be05d201
L
14260 if (i.base_reg != expected_reg
14261 || i.index_reg
fc0763e6 14262 || operand_type_check (i.types[this_operand], disp))
fc0763e6 14263 {
be05d201
L
14264 /* The second memory operand must have the same size as
14265 the first one. */
14266 if (i.mem_operands
14267 && i.base_reg
14268 && !((addr_mode == CODE_64BIT
dc821c5f 14269 && i.base_reg->reg_type.bitfield.qword)
be05d201 14270 || (addr_mode == CODE_32BIT
dc821c5f
JB
14271 ? i.base_reg->reg_type.bitfield.dword
14272 : i.base_reg->reg_type.bitfield.word)))
be05d201
L
14273 goto bad_address;
14274
fc0763e6
JB
14275 as_warn (_("`%s' is not valid here (expected `%c%s%s%c')"),
14276 operand_string,
14277 intel_syntax ? '[' : '(',
14278 register_prefix,
be05d201 14279 expected_reg->reg_name,
fc0763e6 14280 intel_syntax ? ']' : ')');
be05d201 14281 return 1;
fc0763e6 14282 }
be05d201
L
14283 else
14284 return 1;
14285
dc1e8a47 14286 bad_address:
be05d201
L
14287 as_bad (_("`%s' is not a valid %s expression"),
14288 operand_string, kind);
14289 return 0;
3e73aa7c
JH
14290 }
14291 else
14292 {
d3b01414 14293 t = current_templates.start;
77a19f0e 14294
be05d201
L
14295 if (addr_mode != CODE_16BIT)
14296 {
14297 /* 32-bit/64-bit checks. */
41eb8e88
L
14298 if (i.disp_encoding == disp_encoding_16bit)
14299 {
14300 bad_disp:
14301 as_bad (_("invalid `%s' prefix"),
14302 addr_mode == CODE_16BIT ? "{disp32}" : "{disp16}");
14303 return 0;
14304 }
14305
be05d201 14306 if ((i.base_reg
e968fc9b
JB
14307 && ((addr_mode == CODE_64BIT
14308 ? !i.base_reg->reg_type.bitfield.qword
14309 : !i.base_reg->reg_type.bitfield.dword)
14310 || (i.index_reg && i.base_reg->reg_num == RegIP)
14311 || i.base_reg->reg_num == RegIZ))
be05d201 14312 || (i.index_reg
1b54b8d7
JB
14313 && !i.index_reg->reg_type.bitfield.xmmword
14314 && !i.index_reg->reg_type.bitfield.ymmword
14315 && !i.index_reg->reg_type.bitfield.zmmword
be05d201 14316 && ((addr_mode == CODE_64BIT
e968fc9b
JB
14317 ? !i.index_reg->reg_type.bitfield.qword
14318 : !i.index_reg->reg_type.bitfield.dword)
be05d201
L
14319 || !i.index_reg->reg_type.bitfield.baseindex)))
14320 goto bad_address;
8178be5b 14321
260cd341 14322 /* bndmk, bndldx, bndstx and mandatory non-vector SIB have special restrictions. */
7fc69528
JB
14323 if (t->mnem_off == MN_bndmk
14324 || t->mnem_off == MN_bndldx
14325 || t->mnem_off == MN_bndstx
a152332d 14326 || t->opcode_modifier.sib == SIBMEM)
8178be5b
JB
14327 {
14328 /* They cannot use RIP-relative addressing. */
e968fc9b 14329 if (i.base_reg && i.base_reg->reg_num == RegIP)
8178be5b
JB
14330 {
14331 as_bad (_("`%s' cannot be used here"), operand_string);
14332 return 0;
14333 }
14334
14335 /* bndldx and bndstx ignore their scale factor. */
7fc69528 14336 if ((t->mnem_off == MN_bndldx || t->mnem_off == MN_bndstx)
8178be5b
JB
14337 && i.log2_scale_factor)
14338 as_warn (_("register scaling is being ignored here"));
14339 }
be05d201
L
14340 }
14341 else
3e73aa7c 14342 {
be05d201 14343 /* 16-bit checks. */
41eb8e88
L
14344 if (i.disp_encoding == disp_encoding_32bit)
14345 goto bad_disp;
14346
3e73aa7c 14347 if ((i.base_reg
dc821c5f 14348 && (!i.base_reg->reg_type.bitfield.word
40fb9820 14349 || !i.base_reg->reg_type.bitfield.baseindex))
3e73aa7c 14350 || (i.index_reg
dc821c5f 14351 && (!i.index_reg->reg_type.bitfield.word
40fb9820 14352 || !i.index_reg->reg_type.bitfield.baseindex
29b0f896
AM
14353 || !(i.base_reg
14354 && i.base_reg->reg_num < 6
14355 && i.index_reg->reg_num >= 6
14356 && i.log2_scale_factor == 0))))
be05d201 14357 goto bad_address;
3e73aa7c
JH
14358 }
14359 }
be05d201 14360 return 1;
24eab124 14361}
252b5132 14362
43234a1e
L
14363/* Handle vector immediates. */
14364
14365static int
14366RC_SAE_immediate (const char *imm_start)
14367{
43234a1e 14368 const char *pstr = imm_start;
43234a1e
L
14369
14370 if (*pstr != '{')
14371 return 0;
14372
7063667e
JB
14373 pstr = RC_SAE_specifier (pstr + 1);
14374 if (pstr == NULL)
43234a1e
L
14375 return 0;
14376
14377 if (*pstr++ != '}')
14378 {
14379 as_bad (_("Missing '}': '%s'"), imm_start);
14380 return 0;
14381 }
14382 /* RC/SAE immediate string should contain nothing more. */;
14383 if (*pstr != 0)
14384 {
14385 as_bad (_("Junk after '}': '%s'"), imm_start);
14386 return 0;
14387 }
14388
cf665fee
JB
14389 /* Internally this doesn't count as an operand. */
14390 --i.operands;
43234a1e 14391
43234a1e
L
14392 return 1;
14393}
14394
9d299bea
JB
14395static INLINE bool starts_memory_operand (char c)
14396{
014fbcda 14397 return ISDIGIT (c)
4795cd4a 14398 || is_name_beginner (c)
014fbcda 14399 || strchr ("([\"+-!~", c);
9d299bea
JB
14400}
14401
fc0763e6 14402/* Parse OPERAND_STRING into the i386_insn structure I. Returns zero
47926f60 14403 on error. */
252b5132 14404
252b5132 14405static int
a7619375 14406i386_att_operand (char *operand_string)
252b5132 14407{
af6bdddf
AM
14408 const reg_entry *r;
14409 char *end_op;
24eab124 14410 char *op_string = operand_string;
252b5132 14411
24eab124 14412 if (is_space_char (*op_string))
252b5132
RH
14413 ++op_string;
14414
24eab124 14415 /* We check for an absolute prefix (differentiating,
47926f60 14416 for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
ccb05c9c 14417 if (*op_string == ABSOLUTE_PREFIX
d3b01414 14418 && current_templates.start->opcode_modifier.jump)
24eab124
AM
14419 {
14420 ++op_string;
14421 if (is_space_char (*op_string))
14422 ++op_string;
5b7c81bd 14423 i.jumpabsolute = true;
24eab124 14424 }
252b5132 14425
47926f60 14426 /* Check if operand is a register. */
4d1bb795 14427 if ((r = parse_register (op_string, &end_op)) != NULL)
24eab124 14428 {
40fb9820
L
14429 i386_operand_type temp;
14430
8a6fb3f9
JB
14431 if (r == &bad_reg)
14432 return 0;
14433
24eab124
AM
14434 /* Check for a segment override by searching for ':' after a
14435 segment register. */
14436 op_string = end_op;
14437 if (is_space_char (*op_string))
14438 ++op_string;
00cee14f 14439 if (*op_string == ':' && r->reg_type.bitfield.class == SReg)
24eab124 14440 {
5e042380 14441 i.seg[i.mem_operands] = r;
252b5132 14442
24eab124 14443 /* Skip the ':' and whitespace. */
252b5132
RH
14444 ++op_string;
14445 if (is_space_char (*op_string))
24eab124 14446 ++op_string;
252b5132 14447
47926f60 14448 /* Handle case of %es:*foo. */
ccb05c9c 14449 if (!i.jumpabsolute && *op_string == ABSOLUTE_PREFIX
d3b01414 14450 && current_templates.start->opcode_modifier.jump)
24eab124
AM
14451 {
14452 ++op_string;
14453 if (is_space_char (*op_string))
14454 ++op_string;
5b7c81bd 14455 i.jumpabsolute = true;
24eab124 14456 }
c8d541e2 14457
9d299bea 14458 if (!starts_memory_operand (*op_string))
c8d541e2
JB
14459 {
14460 as_bad (_("bad memory operand `%s'"), op_string);
14461 return 0;
14462 }
24eab124
AM
14463 goto do_memory_reference;
14464 }
43234a1e
L
14465
14466 /* Handle vector operations. */
14467 if (*op_string == '{')
14468 {
f70c6814 14469 op_string = check_VecOperations (op_string);
43234a1e
L
14470 if (op_string == NULL)
14471 return 0;
14472 }
14473
24eab124
AM
14474 if (*op_string)
14475 {
d0b47220 14476 as_bad (_("junk `%s' after register"), op_string);
24eab124
AM
14477 return 0;
14478 }
0ff3b7d0
JB
14479
14480 /* Reject pseudo registers for .insn. */
14481 if (dot_insn () && r->reg_type.bitfield.class == ClassNone)
14482 {
14483 as_bad (_("`%s%s' cannot be used here"),
14484 register_prefix, r->reg_name);
14485 return 0;
14486 }
14487
40fb9820
L
14488 temp = r->reg_type;
14489 temp.bitfield.baseindex = 0;
c6fb90c8
L
14490 i.types[this_operand] = operand_type_or (i.types[this_operand],
14491 temp);
7d5e4556 14492 i.types[this_operand].bitfield.unspecified = 0;
520dc8e8 14493 i.op[this_operand].regs = r;
24eab124 14494 i.reg_operands++;
cf665fee
JB
14495
14496 /* A GPR may follow an RC or SAE immediate only if a (vector) register
14497 operand was also present earlier on. */
14498 if (i.rounding.type != rc_none && temp.bitfield.class == Reg
14499 && i.reg_operands == 1)
14500 {
14501 unsigned int j;
14502
14503 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); ++j)
14504 if (i.rounding.type == RC_NamesTable[j].type)
14505 break;
14506 as_bad (_("`%s': misplaced `{%s}'"),
d3b01414 14507 insn_name (current_templates.start), RC_NamesTable[j].name);
cf665fee
JB
14508 return 0;
14509 }
24eab124 14510 }
af6bdddf
AM
14511 else if (*op_string == REGISTER_PREFIX)
14512 {
14513 as_bad (_("bad register name `%s'"), op_string);
14514 return 0;
14515 }
24eab124 14516 else if (*op_string == IMMEDIATE_PREFIX)
ce8a8b2f 14517 {
24eab124 14518 ++op_string;
6f2f06be 14519 if (i.jumpabsolute)
24eab124 14520 {
d0b47220 14521 as_bad (_("immediate operand illegal with absolute jump"));
24eab124
AM
14522 return 0;
14523 }
14524 if (!i386_immediate (op_string))
14525 return 0;
cf665fee
JB
14526 if (i.rounding.type != rc_none)
14527 {
14528 as_bad (_("`%s': RC/SAE operand must follow immediate operands"),
d3b01414 14529 insn_name (current_templates.start));
cf665fee
JB
14530 return 0;
14531 }
24eab124 14532 }
43234a1e
L
14533 else if (RC_SAE_immediate (operand_string))
14534 {
cf665fee
JB
14535 /* If it is a RC or SAE immediate, do the necessary placement check:
14536 Only another immediate or a GPR may precede it. */
14537 if (i.mem_operands || i.reg_operands + i.imm_operands > 1
14538 || (i.reg_operands == 1
14539 && i.op[0].regs->reg_type.bitfield.class != Reg))
14540 {
14541 as_bad (_("`%s': misplaced `%s'"),
d3b01414 14542 insn_name (current_templates.start), operand_string);
cf665fee
JB
14543 return 0;
14544 }
43234a1e 14545 }
9d299bea 14546 else if (starts_memory_operand (*op_string))
24eab124 14547 {
47926f60 14548 /* This is a memory reference of some sort. */
af6bdddf 14549 char *base_string;
252b5132 14550
47926f60 14551 /* Start and end of displacement string expression (if found). */
eecb386c
AM
14552 char *displacement_string_start;
14553 char *displacement_string_end;
252b5132 14554
24eab124 14555 do_memory_reference:
24eab124
AM
14556 /* Check for base index form. We detect the base index form by
14557 looking for an ')' at the end of the operand, searching
14558 for the '(' matching it, and finding a REGISTER_PREFIX or ','
14559 after the '('. */
af6bdddf 14560 base_string = op_string + strlen (op_string);
c3332e24 14561
43234a1e 14562 /* Handle vector operations. */
6b5ba0d4
JB
14563 --base_string;
14564 if (is_space_char (*base_string))
14565 --base_string;
14566
14567 if (*base_string == '}')
43234a1e 14568 {
6b5ba0d4
JB
14569 char *vop_start = NULL;
14570
14571 while (base_string-- > op_string)
14572 {
14573 if (*base_string == '"')
14574 break;
14575 if (*base_string != '{')
14576 continue;
14577
14578 vop_start = base_string;
14579
14580 --base_string;
14581 if (is_space_char (*base_string))
14582 --base_string;
14583
14584 if (*base_string != '}')
14585 break;
14586
14587 vop_start = NULL;
14588 }
14589
14590 if (!vop_start)
14591 {
14592 as_bad (_("unbalanced figure braces"));
14593 return 0;
14594 }
14595
f70c6814 14596 if (check_VecOperations (vop_start) == NULL)
43234a1e 14597 return 0;
43234a1e
L
14598 }
14599
47926f60 14600 /* If we only have a displacement, set-up for it to be parsed later. */
af6bdddf
AM
14601 displacement_string_start = op_string;
14602 displacement_string_end = base_string + 1;
252b5132 14603
24eab124
AM
14604 if (*base_string == ')')
14605 {
af6bdddf 14606 char *temp_string;
e87fb6a6
JB
14607 unsigned int parens_not_balanced = 0;
14608 bool in_quotes = false;
e68c3d59 14609
24eab124 14610 /* We've already checked that the number of left & right ()'s are
e87fb6a6
JB
14611 equal, and that there's a matching set of double quotes. */
14612 end_op = base_string;
14613 for (temp_string = op_string; temp_string < end_op; temp_string++)
24eab124 14614 {
e87fb6a6
JB
14615 if (*temp_string == '\\' && temp_string[1] == '"')
14616 ++temp_string;
14617 else if (*temp_string == '"')
14618 in_quotes = !in_quotes;
14619 else if (!in_quotes)
14620 {
14621 if (*temp_string == '(' && !parens_not_balanced++)
14622 base_string = temp_string;
14623 if (*temp_string == ')')
14624 --parens_not_balanced;
14625 }
24eab124 14626 }
c3332e24 14627
af6bdddf 14628 temp_string = base_string;
c3332e24 14629
24eab124 14630 /* Skip past '(' and whitespace. */
4f081312 14631 gas_assert (*base_string == '(');
e87fb6a6 14632 ++base_string;
252b5132 14633 if (is_space_char (*base_string))
24eab124 14634 ++base_string;
252b5132 14635
af6bdddf 14636 if (*base_string == ','
4eed87de
AM
14637 || ((i.base_reg = parse_register (base_string, &end_op))
14638 != NULL))
252b5132 14639 {
af6bdddf 14640 displacement_string_end = temp_string;
252b5132 14641
40fb9820 14642 i.types[this_operand].bitfield.baseindex = 1;
252b5132 14643
af6bdddf 14644 if (i.base_reg)
24eab124 14645 {
8a6fb3f9
JB
14646 if (i.base_reg == &bad_reg)
14647 return 0;
24eab124
AM
14648 base_string = end_op;
14649 if (is_space_char (*base_string))
14650 ++base_string;
af6bdddf
AM
14651 }
14652
14653 /* There may be an index reg or scale factor here. */
14654 if (*base_string == ',')
14655 {
14656 ++base_string;
14657 if (is_space_char (*base_string))
14658 ++base_string;
14659
4eed87de
AM
14660 if ((i.index_reg = parse_register (base_string, &end_op))
14661 != NULL)
24eab124 14662 {
8a6fb3f9
JB
14663 if (i.index_reg == &bad_reg)
14664 return 0;
af6bdddf 14665 base_string = end_op;
24eab124
AM
14666 if (is_space_char (*base_string))
14667 ++base_string;
af6bdddf
AM
14668 if (*base_string == ',')
14669 {
14670 ++base_string;
14671 if (is_space_char (*base_string))
14672 ++base_string;
14673 }
e5cb08ac 14674 else if (*base_string != ')')
af6bdddf 14675 {
4eed87de
AM
14676 as_bad (_("expecting `,' or `)' "
14677 "after index register in `%s'"),
af6bdddf
AM
14678 operand_string);
14679 return 0;
14680 }
24eab124 14681 }
af6bdddf 14682 else if (*base_string == REGISTER_PREFIX)
24eab124 14683 {
f76bf5e0
L
14684 end_op = strchr (base_string, ',');
14685 if (end_op)
14686 *end_op = '\0';
af6bdddf 14687 as_bad (_("bad register name `%s'"), base_string);
24eab124
AM
14688 return 0;
14689 }
252b5132 14690
47926f60 14691 /* Check for scale factor. */
551c1ca1 14692 if (*base_string != ')')
af6bdddf 14693 {
551c1ca1
AM
14694 char *end_scale = i386_scale (base_string);
14695
14696 if (!end_scale)
af6bdddf 14697 return 0;
24eab124 14698
551c1ca1 14699 base_string = end_scale;
af6bdddf
AM
14700 if (is_space_char (*base_string))
14701 ++base_string;
14702 if (*base_string != ')')
14703 {
4eed87de
AM
14704 as_bad (_("expecting `)' "
14705 "after scale factor in `%s'"),
af6bdddf
AM
14706 operand_string);
14707 return 0;
14708 }
14709 }
14710 else if (!i.index_reg)
24eab124 14711 {
4eed87de
AM
14712 as_bad (_("expecting index register or scale factor "
14713 "after `,'; got '%c'"),
af6bdddf 14714 *base_string);
24eab124
AM
14715 return 0;
14716 }
14717 }
af6bdddf 14718 else if (*base_string != ')')
24eab124 14719 {
4eed87de
AM
14720 as_bad (_("expecting `,' or `)' "
14721 "after base register in `%s'"),
af6bdddf 14722 operand_string);
24eab124
AM
14723 return 0;
14724 }
c3332e24 14725 }
af6bdddf 14726 else if (*base_string == REGISTER_PREFIX)
c3332e24 14727 {
f76bf5e0
L
14728 end_op = strchr (base_string, ',');
14729 if (end_op)
14730 *end_op = '\0';
af6bdddf 14731 as_bad (_("bad register name `%s'"), base_string);
24eab124 14732 return 0;
c3332e24 14733 }
24eab124
AM
14734 }
14735
14736 /* If there's an expression beginning the operand, parse it,
14737 assuming displacement_string_start and
14738 displacement_string_end are meaningful. */
14739 if (displacement_string_start != displacement_string_end)
14740 {
14741 if (!i386_displacement (displacement_string_start,
14742 displacement_string_end))
14743 return 0;
14744 }
14745
14746 /* Special case for (%dx) while doing input/output op. */
14747 if (i.base_reg
75e5731b
JB
14748 && i.base_reg->reg_type.bitfield.instance == RegD
14749 && i.base_reg->reg_type.bitfield.word
24eab124
AM
14750 && i.index_reg == 0
14751 && i.log2_scale_factor == 0
14752 && i.seg[i.mem_operands] == 0
40fb9820 14753 && !operand_type_check (i.types[this_operand], disp))
24eab124 14754 {
2fb5be8d 14755 i.types[this_operand] = i.base_reg->reg_type;
f0a4cb15
CL
14756 i.op[this_operand].regs = i.base_reg;
14757 i.base_reg = NULL;
9373f275 14758 i.input_output_operand = true;
24eab124
AM
14759 return 1;
14760 }
14761
eecb386c
AM
14762 if (i386_index_check (operand_string) == 0)
14763 return 0;
c48dadc9 14764 i.flags[this_operand] |= Operand_Mem;
24eab124
AM
14765 i.mem_operands++;
14766 }
14767 else
ce8a8b2f
AM
14768 {
14769 /* It's not a memory operand; argh! */
24eab124
AM
14770 as_bad (_("invalid char %s beginning operand %d `%s'"),
14771 output_invalid (*op_string),
14772 this_operand + 1,
14773 op_string);
14774 return 0;
14775 }
47926f60 14776 return 1; /* Normal return. */
252b5132
RH
14777}
14778\f
fa94de6b
RM
14779/* Calculate the maximum variable size (i.e., excluding fr_fix)
14780 that an rs_machine_dependent frag may reach. */
14781
14782unsigned int
14783i386_frag_max_var (fragS *frag)
14784{
14785 /* The only relaxable frags are for jumps.
14786 Unconditional jumps can grow by 4 bytes and others by 5 bytes. */
14787 gas_assert (frag->fr_type == rs_machine_dependent);
14788 return TYPE_FROM_RELAX_STATE (frag->fr_subtype) == UNCOND_JUMP ? 4 : 5;
14789}
14790
b084df0b
L
14791#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14792static int
8dcea932 14793elf_symbol_resolved_in_segment_p (symbolS *fr_symbol, offsetT fr_var)
b084df0b
L
14794{
14795 /* STT_GNU_IFUNC symbol must go through PLT. */
14796 if ((symbol_get_bfdsym (fr_symbol)->flags
14797 & BSF_GNU_INDIRECT_FUNCTION) != 0)
14798 return 0;
14799
14800 if (!S_IS_EXTERNAL (fr_symbol))
14801 /* Symbol may be weak or local. */
14802 return !S_IS_WEAK (fr_symbol);
14803
8dcea932
L
14804 /* Global symbols with non-default visibility can't be preempted. */
14805 if (ELF_ST_VISIBILITY (S_GET_OTHER (fr_symbol)) != STV_DEFAULT)
14806 return 1;
14807
14808 if (fr_var != NO_RELOC)
14809 switch ((enum bfd_reloc_code_real) fr_var)
14810 {
14811 case BFD_RELOC_386_PLT32:
14812 case BFD_RELOC_X86_64_PLT32:
33eaf5de 14813 /* Symbol with PLT relocation may be preempted. */
8dcea932
L
14814 return 0;
14815 default:
14816 abort ();
14817 }
14818
b084df0b
L
14819 /* Global symbols with default visibility in a shared library may be
14820 preempted by another definition. */
8dcea932 14821 return !shared;
b084df0b
L
14822}
14823#endif
14824
79d72f45
HL
14825/* Table 3-2. Macro-Fusible Instructions in Haswell Microarchitecture
14826 Note also work for Skylake and Cascadelake.
14827---------------------------------------------------------------------
14828| JCC | ADD/SUB/CMP | INC/DEC | TEST/AND |
14829| ------ | ----------- | ------- | -------- |
14830| Jo | N | N | Y |
14831| Jno | N | N | Y |
14832| Jc/Jb | Y | N | Y |
14833| Jae/Jnb | Y | N | Y |
14834| Je/Jz | Y | Y | Y |
14835| Jne/Jnz | Y | Y | Y |
14836| Jna/Jbe | Y | N | Y |
14837| Ja/Jnbe | Y | N | Y |
14838| Js | N | N | Y |
14839| Jns | N | N | Y |
14840| Jp/Jpe | N | N | Y |
14841| Jnp/Jpo | N | N | Y |
14842| Jl/Jnge | Y | Y | Y |
14843| Jge/Jnl | Y | Y | Y |
14844| Jle/Jng | Y | Y | Y |
14845| Jg/Jnle | Y | Y | Y |
14846--------------------------------------------------------------------- */
14847static int
14848i386_macro_fusible_p (enum mf_cmp_kind mf_cmp, enum mf_jcc_kind mf_jcc)
14849{
14850 if (mf_cmp == mf_cmp_alu_cmp)
14851 return ((mf_jcc >= mf_jcc_jc && mf_jcc <= mf_jcc_jna)
14852 || mf_jcc == mf_jcc_jl || mf_jcc == mf_jcc_jle);
14853 if (mf_cmp == mf_cmp_incdec)
14854 return (mf_jcc == mf_jcc_je || mf_jcc == mf_jcc_jl
14855 || mf_jcc == mf_jcc_jle);
14856 if (mf_cmp == mf_cmp_test_and)
14857 return 1;
14858 return 0;
14859}
14860
e379e5f3
L
14861/* Return the next non-empty frag. */
14862
14863static fragS *
14864i386_next_non_empty_frag (fragS *fragP)
14865{
14866 /* There may be a frag with a ".fill 0" when there is no room in
14867 the current frag for frag_grow in output_insn. */
14868 for (fragP = fragP->fr_next;
14869 (fragP != NULL
14870 && fragP->fr_type == rs_fill
14871 && fragP->fr_fix == 0);
14872 fragP = fragP->fr_next)
14873 ;
14874 return fragP;
14875}
14876
14877/* Return the next jcc frag after BRANCH_PADDING. */
14878
14879static fragS *
79d72f45 14880i386_next_fusible_jcc_frag (fragS *maybe_cmp_fragP, fragS *pad_fragP)
e379e5f3 14881{
79d72f45
HL
14882 fragS *branch_fragP;
14883 if (!pad_fragP)
e379e5f3
L
14884 return NULL;
14885
79d72f45
HL
14886 if (pad_fragP->fr_type == rs_machine_dependent
14887 && (TYPE_FROM_RELAX_STATE (pad_fragP->fr_subtype)
e379e5f3
L
14888 == BRANCH_PADDING))
14889 {
79d72f45
HL
14890 branch_fragP = i386_next_non_empty_frag (pad_fragP);
14891 if (branch_fragP->fr_type != rs_machine_dependent)
e379e5f3 14892 return NULL;
79d72f45
HL
14893 if (TYPE_FROM_RELAX_STATE (branch_fragP->fr_subtype) == COND_JUMP
14894 && i386_macro_fusible_p (maybe_cmp_fragP->tc_frag_data.mf_type,
14895 pad_fragP->tc_frag_data.mf_type))
14896 return branch_fragP;
e379e5f3
L
14897 }
14898
14899 return NULL;
14900}
14901
14902/* Classify BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags. */
14903
14904static void
14905i386_classify_machine_dependent_frag (fragS *fragP)
14906{
14907 fragS *cmp_fragP;
14908 fragS *pad_fragP;
14909 fragS *branch_fragP;
14910 fragS *next_fragP;
14911 unsigned int max_prefix_length;
14912
14913 if (fragP->tc_frag_data.classified)
14914 return;
14915
14916 /* First scan for BRANCH_PADDING and FUSED_JCC_PADDING. Convert
14917 FUSED_JCC_PADDING and merge BRANCH_PADDING. */
14918 for (next_fragP = fragP;
14919 next_fragP != NULL;
14920 next_fragP = next_fragP->fr_next)
14921 {
14922 next_fragP->tc_frag_data.classified = 1;
14923 if (next_fragP->fr_type == rs_machine_dependent)
14924 switch (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype))
14925 {
14926 case BRANCH_PADDING:
14927 /* The BRANCH_PADDING frag must be followed by a branch
14928 frag. */
14929 branch_fragP = i386_next_non_empty_frag (next_fragP);
14930 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
14931 break;
14932 case FUSED_JCC_PADDING:
14933 /* Check if this is a fused jcc:
14934 FUSED_JCC_PADDING
14935 CMP like instruction
14936 BRANCH_PADDING
14937 COND_JUMP
14938 */
14939 cmp_fragP = i386_next_non_empty_frag (next_fragP);
14940 pad_fragP = i386_next_non_empty_frag (cmp_fragP);
79d72f45 14941 branch_fragP = i386_next_fusible_jcc_frag (next_fragP, pad_fragP);
e379e5f3
L
14942 if (branch_fragP)
14943 {
14944 /* The BRANCH_PADDING frag is merged with the
14945 FUSED_JCC_PADDING frag. */
14946 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
14947 /* CMP like instruction size. */
14948 next_fragP->tc_frag_data.cmp_size = cmp_fragP->fr_fix;
14949 frag_wane (pad_fragP);
14950 /* Skip to branch_fragP. */
14951 next_fragP = branch_fragP;
14952 }
14953 else if (next_fragP->tc_frag_data.max_prefix_length)
14954 {
14955 /* Turn FUSED_JCC_PADDING into BRANCH_PREFIX if it isn't
14956 a fused jcc. */
14957 next_fragP->fr_subtype
14958 = ENCODE_RELAX_STATE (BRANCH_PREFIX, 0);
14959 next_fragP->tc_frag_data.max_bytes
14960 = next_fragP->tc_frag_data.max_prefix_length;
14961 /* This will be updated in the BRANCH_PREFIX scan. */
14962 next_fragP->tc_frag_data.max_prefix_length = 0;
14963 }
14964 else
14965 frag_wane (next_fragP);
14966 break;
14967 }
14968 }
14969
14970 /* Stop if there is no BRANCH_PREFIX. */
14971 if (!align_branch_prefix_size)
14972 return;
14973
14974 /* Scan for BRANCH_PREFIX. */
14975 for (; fragP != NULL; fragP = fragP->fr_next)
14976 {
14977 if (fragP->fr_type != rs_machine_dependent
14978 || (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
14979 != BRANCH_PREFIX))
14980 continue;
14981
14982 /* Count all BRANCH_PREFIX frags before BRANCH_PADDING and
14983 COND_JUMP_PREFIX. */
14984 max_prefix_length = 0;
14985 for (next_fragP = fragP;
14986 next_fragP != NULL;
14987 next_fragP = next_fragP->fr_next)
14988 {
14989 if (next_fragP->fr_type == rs_fill)
14990 /* Skip rs_fill frags. */
14991 continue;
14992 else if (next_fragP->fr_type != rs_machine_dependent)
14993 /* Stop for all other frags. */
14994 break;
14995
14996 /* rs_machine_dependent frags. */
14997 if (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
14998 == BRANCH_PREFIX)
14999 {
15000 /* Count BRANCH_PREFIX frags. */
15001 if (max_prefix_length >= MAX_FUSED_JCC_PADDING_SIZE)
15002 {
15003 max_prefix_length = MAX_FUSED_JCC_PADDING_SIZE;
15004 frag_wane (next_fragP);
15005 }
15006 else
15007 max_prefix_length
15008 += next_fragP->tc_frag_data.max_bytes;
15009 }
15010 else if ((TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
15011 == BRANCH_PADDING)
15012 || (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
15013 == FUSED_JCC_PADDING))
15014 {
15015 /* Stop at BRANCH_PADDING and FUSED_JCC_PADDING. */
15016 fragP->tc_frag_data.u.padding_fragP = next_fragP;
15017 break;
15018 }
15019 else
15020 /* Stop for other rs_machine_dependent frags. */
15021 break;
15022 }
15023
15024 fragP->tc_frag_data.max_prefix_length = max_prefix_length;
15025
15026 /* Skip to the next frag. */
15027 fragP = next_fragP;
15028 }
15029}
15030
15031/* Compute padding size for
15032
15033 FUSED_JCC_PADDING
15034 CMP like instruction
15035 BRANCH_PADDING
15036 COND_JUMP/UNCOND_JUMP
15037
15038 or
15039
15040 BRANCH_PADDING
15041 COND_JUMP/UNCOND_JUMP
15042 */
15043
15044static int
15045i386_branch_padding_size (fragS *fragP, offsetT address)
15046{
15047 unsigned int offset, size, padding_size;
15048 fragS *branch_fragP = fragP->tc_frag_data.u.branch_fragP;
15049
15050 /* The start address of the BRANCH_PADDING or FUSED_JCC_PADDING frag. */
15051 if (!address)
15052 address = fragP->fr_address;
15053 address += fragP->fr_fix;
15054
15055 /* CMP like instrunction size. */
15056 size = fragP->tc_frag_data.cmp_size;
15057
15058 /* The base size of the branch frag. */
15059 size += branch_fragP->fr_fix;
15060
15061 /* Add opcode and displacement bytes for the rs_machine_dependent
15062 branch frag. */
15063 if (branch_fragP->fr_type == rs_machine_dependent)
15064 size += md_relax_table[branch_fragP->fr_subtype].rlx_length;
15065
15066 /* Check if branch is within boundary and doesn't end at the last
15067 byte. */
15068 offset = address & ((1U << align_branch_power) - 1);
15069 if ((offset + size) >= (1U << align_branch_power))
15070 /* Padding needed to avoid crossing boundary. */
15071 padding_size = (1U << align_branch_power) - offset;
15072 else
15073 /* No padding needed. */
15074 padding_size = 0;
15075
15076 /* The return value may be saved in tc_frag_data.length which is
15077 unsigned byte. */
15078 if (!fits_in_unsigned_byte (padding_size))
15079 abort ();
15080
15081 return padding_size;
15082}
15083
15084/* i386_generic_table_relax_frag()
15085
15086 Handle BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags to
15087 grow/shrink padding to align branch frags. Hand others to
15088 relax_frag(). */
15089
15090long
15091i386_generic_table_relax_frag (segT segment, fragS *fragP, long stretch)
15092{
15093 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
15094 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
15095 {
15096 long padding_size = i386_branch_padding_size (fragP, 0);
15097 long grow = padding_size - fragP->tc_frag_data.length;
15098
15099 /* When the BRANCH_PREFIX frag is used, the computed address
15100 must match the actual address and there should be no padding. */
15101 if (fragP->tc_frag_data.padding_address
15102 && (fragP->tc_frag_data.padding_address != fragP->fr_address
15103 || padding_size))
15104 abort ();
15105
15106 /* Update the padding size. */
15107 if (grow)
15108 fragP->tc_frag_data.length = padding_size;
15109
15110 return grow;
15111 }
15112 else if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
15113 {
15114 fragS *padding_fragP, *next_fragP;
15115 long padding_size, left_size, last_size;
15116
15117 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
15118 if (!padding_fragP)
15119 /* Use the padding set by the leading BRANCH_PREFIX frag. */
15120 return (fragP->tc_frag_data.length
15121 - fragP->tc_frag_data.last_length);
15122
15123 /* Compute the relative address of the padding frag in the very
15124 first time where the BRANCH_PREFIX frag sizes are zero. */
15125 if (!fragP->tc_frag_data.padding_address)
15126 fragP->tc_frag_data.padding_address
15127 = padding_fragP->fr_address - (fragP->fr_address - stretch);
15128
15129 /* First update the last length from the previous interation. */
15130 left_size = fragP->tc_frag_data.prefix_length;
15131 for (next_fragP = fragP;
15132 next_fragP != padding_fragP;
15133 next_fragP = next_fragP->fr_next)
15134 if (next_fragP->fr_type == rs_machine_dependent
15135 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
15136 == BRANCH_PREFIX))
15137 {
15138 if (left_size)
15139 {
15140 int max = next_fragP->tc_frag_data.max_bytes;
15141 if (max)
15142 {
15143 int size;
15144 if (max > left_size)
15145 size = left_size;
15146 else
15147 size = max;
15148 left_size -= size;
15149 next_fragP->tc_frag_data.last_length = size;
15150 }
15151 }
15152 else
15153 next_fragP->tc_frag_data.last_length = 0;
15154 }
15155
15156 /* Check the padding size for the padding frag. */
15157 padding_size = i386_branch_padding_size
15158 (padding_fragP, (fragP->fr_address
15159 + fragP->tc_frag_data.padding_address));
15160
15161 last_size = fragP->tc_frag_data.prefix_length;
15162 /* Check if there is change from the last interation. */
15163 if (padding_size == last_size)
15164 {
15165 /* Update the expected address of the padding frag. */
15166 padding_fragP->tc_frag_data.padding_address
15167 = (fragP->fr_address + padding_size
15168 + fragP->tc_frag_data.padding_address);
15169 return 0;
15170 }
15171
15172 if (padding_size > fragP->tc_frag_data.max_prefix_length)
15173 {
15174 /* No padding if there is no sufficient room. Clear the
15175 expected address of the padding frag. */
15176 padding_fragP->tc_frag_data.padding_address = 0;
15177 padding_size = 0;
15178 }
15179 else
15180 /* Store the expected address of the padding frag. */
15181 padding_fragP->tc_frag_data.padding_address
15182 = (fragP->fr_address + padding_size
15183 + fragP->tc_frag_data.padding_address);
15184
15185 fragP->tc_frag_data.prefix_length = padding_size;
15186
15187 /* Update the length for the current interation. */
15188 left_size = padding_size;
15189 for (next_fragP = fragP;
15190 next_fragP != padding_fragP;
15191 next_fragP = next_fragP->fr_next)
15192 if (next_fragP->fr_type == rs_machine_dependent
15193 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
15194 == BRANCH_PREFIX))
15195 {
15196 if (left_size)
15197 {
15198 int max = next_fragP->tc_frag_data.max_bytes;
15199 if (max)
15200 {
15201 int size;
15202 if (max > left_size)
15203 size = left_size;
15204 else
15205 size = max;
15206 left_size -= size;
15207 next_fragP->tc_frag_data.length = size;
15208 }
15209 }
15210 else
15211 next_fragP->tc_frag_data.length = 0;
15212 }
15213
15214 return (fragP->tc_frag_data.length
15215 - fragP->tc_frag_data.last_length);
15216 }
15217 return relax_frag (segment, fragP, stretch);
15218}
15219
ee7fcc42
AM
15220/* md_estimate_size_before_relax()
15221
15222 Called just before relax() for rs_machine_dependent frags. The x86
15223 assembler uses these frags to handle variable size jump
15224 instructions.
15225
15226 Any symbol that is now undefined will not become defined.
15227 Return the correct fr_subtype in the frag.
15228 Return the initial "guess for variable size of frag" to caller.
15229 The guess is actually the growth beyond the fixed part. Whatever
15230 we do to grow the fixed or variable part contributes to our
15231 returned value. */
15232
252b5132 15233int
7016a5d5 15234md_estimate_size_before_relax (fragS *fragP, segT segment)
252b5132 15235{
e379e5f3
L
15236 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
15237 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX
15238 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
15239 {
15240 i386_classify_machine_dependent_frag (fragP);
15241 return fragP->tc_frag_data.length;
15242 }
15243
252b5132 15244 /* We've already got fragP->fr_subtype right; all we have to do is
b98ef147
AM
15245 check for un-relaxable symbols. On an ELF system, we can't relax
15246 an externally visible symbol, because it may be overridden by a
15247 shared library. */
15248 if (S_GET_SEGMENT (fragP->fr_symbol) != segment
6d249963 15249#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 15250 || (IS_ELF
8dcea932
L
15251 && !elf_symbol_resolved_in_segment_p (fragP->fr_symbol,
15252 fragP->fr_var))
fbeb56a4
DK
15253#endif
15254#if defined (OBJ_COFF) && defined (TE_PE)
7ab9ffdd 15255 || (OUTPUT_FLAVOR == bfd_target_coff_flavour
fbeb56a4 15256 && S_IS_WEAK (fragP->fr_symbol))
b98ef147
AM
15257#endif
15258 )
252b5132 15259 {
b98ef147
AM
15260 /* Symbol is undefined in this segment, or we need to keep a
15261 reloc so that weak symbols can be overridden. */
15262 int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
f86103b7 15263 enum bfd_reloc_code_real reloc_type;
ee7fcc42
AM
15264 unsigned char *opcode;
15265 int old_fr_fix;
eb19308f 15266 fixS *fixP = NULL;
f6af82bd 15267
ee7fcc42 15268 if (fragP->fr_var != NO_RELOC)
1e9cc1c2 15269 reloc_type = (enum bfd_reloc_code_real) fragP->fr_var;
b98ef147 15270 else if (size == 2)
f6af82bd 15271 reloc_type = BFD_RELOC_16_PCREL;
bd7ab16b 15272#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
ed719294
JB
15273 else if (fragP->tc_frag_data.code == CODE_64BIT
15274 && fragP->fr_offset == 0
1ef3994a 15275 && need_plt32_p (fragP->fr_symbol))
bd7ab16b
L
15276 reloc_type = BFD_RELOC_X86_64_PLT32;
15277#endif
f6af82bd
AM
15278 else
15279 reloc_type = BFD_RELOC_32_PCREL;
252b5132 15280
ee7fcc42
AM
15281 old_fr_fix = fragP->fr_fix;
15282 opcode = (unsigned char *) fragP->fr_opcode;
15283
fddf5b5b 15284 switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
252b5132 15285 {
fddf5b5b
AM
15286 case UNCOND_JUMP:
15287 /* Make jmp (0xeb) a (d)word displacement jump. */
47926f60 15288 opcode[0] = 0xe9;
252b5132 15289 fragP->fr_fix += size;
eb19308f
JB
15290 fixP = fix_new (fragP, old_fr_fix, size,
15291 fragP->fr_symbol,
15292 fragP->fr_offset, 1,
15293 reloc_type);
252b5132
RH
15294 break;
15295
fddf5b5b 15296 case COND_JUMP86:
412167cb
AM
15297 if (size == 2
15298 && (!no_cond_jump_promotion || fragP->fr_var != NO_RELOC))
fddf5b5b
AM
15299 {
15300 /* Negate the condition, and branch past an
15301 unconditional jump. */
15302 opcode[0] ^= 1;
15303 opcode[1] = 3;
15304 /* Insert an unconditional jump. */
15305 opcode[2] = 0xe9;
15306 /* We added two extra opcode bytes, and have a two byte
15307 offset. */
15308 fragP->fr_fix += 2 + 2;
062cd5e7
AS
15309 fix_new (fragP, old_fr_fix + 2, 2,
15310 fragP->fr_symbol,
15311 fragP->fr_offset, 1,
15312 reloc_type);
fddf5b5b
AM
15313 break;
15314 }
15315 /* Fall through. */
15316
15317 case COND_JUMP:
412167cb
AM
15318 if (no_cond_jump_promotion && fragP->fr_var == NO_RELOC)
15319 {
15320 fragP->fr_fix += 1;
3e02c1cc
AM
15321 fixP = fix_new (fragP, old_fr_fix, 1,
15322 fragP->fr_symbol,
15323 fragP->fr_offset, 1,
15324 BFD_RELOC_8_PCREL);
15325 fixP->fx_signed = 1;
412167cb
AM
15326 break;
15327 }
93c2a809 15328
24eab124 15329 /* This changes the byte-displacement jump 0x7N
fddf5b5b 15330 to the (d)word-displacement jump 0x0f,0x8N. */
252b5132 15331 opcode[1] = opcode[0] + 0x10;
f6af82bd 15332 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
47926f60
KH
15333 /* We've added an opcode byte. */
15334 fragP->fr_fix += 1 + size;
eb19308f
JB
15335 fixP = fix_new (fragP, old_fr_fix + 1, size,
15336 fragP->fr_symbol,
15337 fragP->fr_offset, 1,
15338 reloc_type);
252b5132 15339 break;
fddf5b5b
AM
15340
15341 default:
15342 BAD_CASE (fragP->fr_subtype);
15343 break;
252b5132 15344 }
eb19308f
JB
15345
15346 /* All jumps handled here are signed, but don't unconditionally use a
15347 signed limit check for 32 and 16 bit jumps as we want to allow wrap
15348 around at 4G (outside of 64-bit mode) and 64k. */
15349 if (size == 4 && flag_code == CODE_64BIT)
15350 fixP->fx_signed = 1;
15351
252b5132 15352 frag_wane (fragP);
ee7fcc42 15353 return fragP->fr_fix - old_fr_fix;
252b5132 15354 }
93c2a809 15355
93c2a809
AM
15356 /* Guess size depending on current relax state. Initially the relax
15357 state will correspond to a short jump and we return 1, because
15358 the variable part of the frag (the branch offset) is one byte
15359 long. However, we can relax a section more than once and in that
15360 case we must either set fr_subtype back to the unrelaxed state,
15361 or return the value for the appropriate branch. */
15362 return md_relax_table[fragP->fr_subtype].rlx_length;
ee7fcc42
AM
15363}
15364
47926f60
KH
15365/* Called after relax() is finished.
15366
15367 In: Address of frag.
15368 fr_type == rs_machine_dependent.
15369 fr_subtype is what the address relaxed to.
15370
15371 Out: Any fixSs and constants are set up.
15372 Caller will turn frag into a ".space 0". */
15373
252b5132 15374void
7016a5d5
TG
15375md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec ATTRIBUTE_UNUSED,
15376 fragS *fragP)
252b5132 15377{
29b0f896 15378 unsigned char *opcode;
252b5132 15379 unsigned char *where_to_put_displacement = NULL;
847f7ad4
AM
15380 offsetT target_address;
15381 offsetT opcode_address;
252b5132 15382 unsigned int extension = 0;
847f7ad4 15383 offsetT displacement_from_opcode_start;
252b5132 15384
e379e5f3
L
15385 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
15386 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING
15387 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
15388 {
15389 /* Generate nop padding. */
15390 unsigned int size = fragP->tc_frag_data.length;
15391 if (size)
15392 {
15393 if (size > fragP->tc_frag_data.max_bytes)
15394 abort ();
15395
15396 if (flag_debug)
15397 {
15398 const char *msg;
15399 const char *branch = "branch";
15400 const char *prefix = "";
15401 fragS *padding_fragP;
15402 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
15403 == BRANCH_PREFIX)
15404 {
15405 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
15406 switch (fragP->tc_frag_data.default_prefix)
15407 {
15408 default:
15409 abort ();
15410 break;
15411 case CS_PREFIX_OPCODE:
15412 prefix = " cs";
15413 break;
15414 case DS_PREFIX_OPCODE:
15415 prefix = " ds";
15416 break;
15417 case ES_PREFIX_OPCODE:
15418 prefix = " es";
15419 break;
15420 case FS_PREFIX_OPCODE:
15421 prefix = " fs";
15422 break;
15423 case GS_PREFIX_OPCODE:
15424 prefix = " gs";
15425 break;
15426 case SS_PREFIX_OPCODE:
15427 prefix = " ss";
15428 break;
15429 }
15430 if (padding_fragP)
15431 msg = _("%s:%u: add %d%s at 0x%llx to align "
15432 "%s within %d-byte boundary\n");
15433 else
15434 msg = _("%s:%u: add additional %d%s at 0x%llx to "
15435 "align %s within %d-byte boundary\n");
15436 }
15437 else
15438 {
15439 padding_fragP = fragP;
15440 msg = _("%s:%u: add %d%s-byte nop at 0x%llx to align "
15441 "%s within %d-byte boundary\n");
15442 }
15443
15444 if (padding_fragP)
15445 switch (padding_fragP->tc_frag_data.branch_type)
15446 {
15447 case align_branch_jcc:
15448 branch = "jcc";
15449 break;
15450 case align_branch_fused:
15451 branch = "fused jcc";
15452 break;
15453 case align_branch_jmp:
15454 branch = "jmp";
15455 break;
15456 case align_branch_call:
15457 branch = "call";
15458 break;
15459 case align_branch_indirect:
15460 branch = "indiret branch";
15461 break;
15462 case align_branch_ret:
15463 branch = "ret";
15464 break;
15465 default:
15466 break;
15467 }
15468
15469 fprintf (stdout, msg,
15470 fragP->fr_file, fragP->fr_line, size, prefix,
15471 (long long) fragP->fr_address, branch,
15472 1 << align_branch_power);
15473 }
15474 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
15475 memset (fragP->fr_opcode,
15476 fragP->tc_frag_data.default_prefix, size);
15477 else
15478 i386_generate_nops (fragP, (char *) fragP->fr_opcode,
15479 size, 0);
15480 fragP->fr_fix += size;
15481 }
15482 return;
15483 }
15484
252b5132
RH
15485 opcode = (unsigned char *) fragP->fr_opcode;
15486
47926f60 15487 /* Address we want to reach in file space. */
252b5132 15488 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
252b5132 15489
47926f60 15490 /* Address opcode resides at in file space. */
252b5132
RH
15491 opcode_address = fragP->fr_address + fragP->fr_fix;
15492
47926f60 15493 /* Displacement from opcode start to fill into instruction. */
252b5132
RH
15494 displacement_from_opcode_start = target_address - opcode_address;
15495
fddf5b5b 15496 if ((fragP->fr_subtype & BIG) == 0)
252b5132 15497 {
47926f60
KH
15498 /* Don't have to change opcode. */
15499 extension = 1; /* 1 opcode + 1 displacement */
252b5132 15500 where_to_put_displacement = &opcode[1];
fddf5b5b
AM
15501 }
15502 else
15503 {
15504 if (no_cond_jump_promotion
15505 && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
4eed87de
AM
15506 as_warn_where (fragP->fr_file, fragP->fr_line,
15507 _("long jump required"));
252b5132 15508
fddf5b5b
AM
15509 switch (fragP->fr_subtype)
15510 {
15511 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
15512 extension = 4; /* 1 opcode + 4 displacement */
15513 opcode[0] = 0xe9;
15514 where_to_put_displacement = &opcode[1];
15515 break;
252b5132 15516
fddf5b5b
AM
15517 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
15518 extension = 2; /* 1 opcode + 2 displacement */
15519 opcode[0] = 0xe9;
15520 where_to_put_displacement = &opcode[1];
15521 break;
252b5132 15522
fddf5b5b
AM
15523 case ENCODE_RELAX_STATE (COND_JUMP, BIG):
15524 case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
15525 extension = 5; /* 2 opcode + 4 displacement */
15526 opcode[1] = opcode[0] + 0x10;
15527 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
15528 where_to_put_displacement = &opcode[2];
15529 break;
252b5132 15530
fddf5b5b
AM
15531 case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
15532 extension = 3; /* 2 opcode + 2 displacement */
15533 opcode[1] = opcode[0] + 0x10;
15534 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
15535 where_to_put_displacement = &opcode[2];
15536 break;
252b5132 15537
fddf5b5b
AM
15538 case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
15539 extension = 4;
15540 opcode[0] ^= 1;
15541 opcode[1] = 3;
15542 opcode[2] = 0xe9;
15543 where_to_put_displacement = &opcode[3];
15544 break;
15545
15546 default:
15547 BAD_CASE (fragP->fr_subtype);
15548 break;
15549 }
252b5132 15550 }
fddf5b5b 15551
7b81dfbb
AJ
15552 /* If size if less then four we are sure that the operand fits,
15553 but if it's 4, then it could be that the displacement is larger
15554 then -/+ 2GB. */
15555 if (DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype) == 4
15556 && object_64bit
15557 && ((addressT) (displacement_from_opcode_start - extension
4eed87de
AM
15558 + ((addressT) 1 << 31))
15559 > (((addressT) 2 << 31) - 1)))
7b81dfbb
AJ
15560 {
15561 as_bad_where (fragP->fr_file, fragP->fr_line,
15562 _("jump target out of range"));
15563 /* Make us emit 0. */
15564 displacement_from_opcode_start = extension;
15565 }
47926f60 15566 /* Now put displacement after opcode. */
252b5132
RH
15567 md_number_to_chars ((char *) where_to_put_displacement,
15568 (valueT) (displacement_from_opcode_start - extension),
fddf5b5b 15569 DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
252b5132
RH
15570 fragP->fr_fix += extension;
15571}
15572\f
7016a5d5 15573/* Apply a fixup (fixP) to segment data, once it has been determined
252b5132
RH
15574 by our caller that we have all the info we need to fix it up.
15575
7016a5d5
TG
15576 Parameter valP is the pointer to the value of the bits.
15577
252b5132
RH
15578 On the 386, immediates, displacements, and data pointers are all in
15579 the same (little-endian) format, so we don't need to care about which
15580 we are handling. */
15581
94f592af 15582void
7016a5d5 15583md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 15584{
94f592af 15585 char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
c6682705 15586 valueT value = *valP;
252b5132 15587
f86103b7 15588#if !defined (TE_Mach)
93382f6d
AM
15589 if (fixP->fx_pcrel)
15590 {
15591 switch (fixP->fx_r_type)
15592 {
5865bb77
ILT
15593 default:
15594 break;
15595
d6ab8113
JB
15596 case BFD_RELOC_64:
15597 fixP->fx_r_type = BFD_RELOC_64_PCREL;
15598 break;
93382f6d 15599 case BFD_RELOC_32:
ae8887b5 15600 case BFD_RELOC_X86_64_32S:
93382f6d
AM
15601 fixP->fx_r_type = BFD_RELOC_32_PCREL;
15602 break;
15603 case BFD_RELOC_16:
15604 fixP->fx_r_type = BFD_RELOC_16_PCREL;
15605 break;
15606 case BFD_RELOC_8:
15607 fixP->fx_r_type = BFD_RELOC_8_PCREL;
15608 break;
15609 }
15610 }
252b5132 15611
a161fe53 15612 if (fixP->fx_addsy != NULL
31312f95 15613 && (fixP->fx_r_type == BFD_RELOC_32_PCREL
d6ab8113 15614 || fixP->fx_r_type == BFD_RELOC_64_PCREL
31312f95 15615 || fixP->fx_r_type == BFD_RELOC_16_PCREL
d258b828 15616 || fixP->fx_r_type == BFD_RELOC_8_PCREL)
31312f95 15617 && !use_rela_relocations)
252b5132 15618 {
31312f95
AM
15619 /* This is a hack. There should be a better way to handle this.
15620 This covers for the fact that bfd_install_relocation will
15621 subtract the current location (for partial_inplace, PC relative
15622 relocations); see more below. */
252b5132 15623#ifndef OBJ_AOUT
718ddfc0 15624 if (IS_ELF
252b5132
RH
15625#ifdef TE_PE
15626 || OUTPUT_FLAVOR == bfd_target_coff_flavour
15627#endif
15628 )
15629 value += fixP->fx_where + fixP->fx_frag->fr_address;
15630#endif
15631#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 15632 if (IS_ELF)
252b5132 15633 {
6539b54b 15634 segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
2f66722d 15635
6539b54b 15636 if ((sym_seg == seg
2f66722d 15637 || (symbol_section_p (fixP->fx_addsy)
6539b54b 15638 && sym_seg != absolute_section))
af65af87 15639 && !generic_force_reloc (fixP))
2f66722d
AM
15640 {
15641 /* Yes, we add the values in twice. This is because
6539b54b
AM
15642 bfd_install_relocation subtracts them out again. I think
15643 bfd_install_relocation is broken, but I don't dare change
2f66722d
AM
15644 it. FIXME. */
15645 value += fixP->fx_where + fixP->fx_frag->fr_address;
15646 }
252b5132
RH
15647 }
15648#endif
15649#if defined (OBJ_COFF) && defined (TE_PE)
977cdf5a
NC
15650 /* For some reason, the PE format does not store a
15651 section address offset for a PC relative symbol. */
15652 if (S_GET_SEGMENT (fixP->fx_addsy) != seg
7be1c489 15653 || S_IS_WEAK (fixP->fx_addsy))
252b5132
RH
15654 value += md_pcrel_from (fixP);
15655#endif
15656 }
fbeb56a4 15657#if defined (OBJ_COFF) && defined (TE_PE)
f01c1a09
NC
15658 if (fixP->fx_addsy != NULL
15659 && S_IS_WEAK (fixP->fx_addsy)
15660 /* PR 16858: Do not modify weak function references. */
15661 && ! fixP->fx_pcrel)
fbeb56a4 15662 {
296a8689
NC
15663#if !defined (TE_PEP)
15664 /* For x86 PE weak function symbols are neither PC-relative
15665 nor do they set S_IS_FUNCTION. So the only reliable way
15666 to detect them is to check the flags of their containing
15667 section. */
15668 if (S_GET_SEGMENT (fixP->fx_addsy) != NULL
15669 && S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_CODE)
15670 ;
15671 else
15672#endif
fbeb56a4
DK
15673 value -= S_GET_VALUE (fixP->fx_addsy);
15674 }
15675#endif
252b5132
RH
15676
15677 /* Fix a few things - the dynamic linker expects certain values here,
0234cb7c 15678 and we must not disappoint it. */
252b5132 15679#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 15680 if (IS_ELF && fixP->fx_addsy)
47926f60
KH
15681 switch (fixP->fx_r_type)
15682 {
15683 case BFD_RELOC_386_PLT32:
3e73aa7c 15684 case BFD_RELOC_X86_64_PLT32:
b9519cfe
L
15685 /* Make the jump instruction point to the address of the operand.
15686 At runtime we merely add the offset to the actual PLT entry.
15687 NB: Subtract the offset size only for jump instructions. */
15688 if (fixP->fx_pcrel)
15689 value = -4;
47926f60 15690 break;
31312f95 15691
13ae64f3
JJ
15692 case BFD_RELOC_386_TLS_GD:
15693 case BFD_RELOC_386_TLS_LDM:
13ae64f3 15694 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
15695 case BFD_RELOC_386_TLS_IE:
15696 case BFD_RELOC_386_TLS_GOTIE:
67a4f2b7 15697 case BFD_RELOC_386_TLS_GOTDESC:
bffbf940
JJ
15698 case BFD_RELOC_X86_64_TLSGD:
15699 case BFD_RELOC_X86_64_TLSLD:
15700 case BFD_RELOC_X86_64_GOTTPOFF:
a533c8df 15701 case BFD_RELOC_X86_64_CODE_4_GOTTPOFF:
5bc71c2a 15702 case BFD_RELOC_X86_64_CODE_6_GOTTPOFF:
67a4f2b7 15703 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
a533c8df 15704 case BFD_RELOC_X86_64_CODE_4_GOTPC32_TLSDESC:
00f7efb6
JJ
15705 value = 0; /* Fully resolved at runtime. No addend. */
15706 /* Fallthrough */
15707 case BFD_RELOC_386_TLS_LE:
15708 case BFD_RELOC_386_TLS_LDO_32:
15709 case BFD_RELOC_386_TLS_LE_32:
15710 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 15711 case BFD_RELOC_X86_64_DTPOFF64:
00f7efb6 15712 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113 15713 case BFD_RELOC_X86_64_TPOFF64:
00f7efb6
JJ
15714 S_SET_THREAD_LOCAL (fixP->fx_addsy);
15715 break;
15716
67a4f2b7
AO
15717 case BFD_RELOC_386_TLS_DESC_CALL:
15718 case BFD_RELOC_X86_64_TLSDESC_CALL:
15719 value = 0; /* Fully resolved at runtime. No addend. */
15720 S_SET_THREAD_LOCAL (fixP->fx_addsy);
15721 fixP->fx_done = 0;
15722 return;
15723
47926f60
KH
15724 case BFD_RELOC_VTABLE_INHERIT:
15725 case BFD_RELOC_VTABLE_ENTRY:
15726 fixP->fx_done = 0;
94f592af 15727 return;
47926f60
KH
15728
15729 default:
15730 break;
15731 }
15732#endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) */
a442cac5
JB
15733
15734 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
15735 if (!object_64bit)
15736 value = extend_to_32bit_address (value);
15737
c6682705 15738 *valP = value;
f86103b7 15739#endif /* !defined (TE_Mach) */
3e73aa7c 15740
3e73aa7c 15741 /* Are we finished with this relocation now? */
c6682705 15742 if (fixP->fx_addsy == NULL)
b8188555
JB
15743 {
15744 fixP->fx_done = 1;
15745 switch (fixP->fx_r_type)
15746 {
15747 case BFD_RELOC_X86_64_32S:
15748 fixP->fx_signed = 1;
15749 break;
15750
15751 default:
15752 break;
15753 }
15754 }
fbeb56a4
DK
15755#if defined (OBJ_COFF) && defined (TE_PE)
15756 else if (fixP->fx_addsy != NULL && S_IS_WEAK (fixP->fx_addsy))
15757 {
15758 fixP->fx_done = 0;
15759 /* Remember value for tc_gen_reloc. */
15760 fixP->fx_addnumber = value;
15761 /* Clear out the frag for now. */
15762 value = 0;
15763 }
15764#endif
3e73aa7c
JH
15765 else if (use_rela_relocations)
15766 {
46fb6d5a
JB
15767 if (!disallow_64bit_reloc || fixP->fx_r_type == NO_RELOC)
15768 fixP->fx_no_overflow = 1;
062cd5e7
AS
15769 /* Remember value for tc_gen_reloc. */
15770 fixP->fx_addnumber = value;
3e73aa7c
JH
15771 value = 0;
15772 }
f86103b7 15773
94f592af 15774 md_number_to_chars (p, value, fixP->fx_size);
252b5132 15775}
252b5132 15776\f
6d4af3c2 15777const char *
499ac353 15778md_atof (int type, char *litP, int *sizeP)
252b5132 15779{
499ac353
NC
15780 /* This outputs the LITTLENUMs in REVERSE order;
15781 in accord with the bigendian 386. */
5b7c81bd 15782 return ieee_md_atof (type, litP, sizeP, false);
252b5132
RH
15783}
15784\f
2d545b82 15785static char output_invalid_buf[sizeof (unsigned char) * 2 + 6];
252b5132 15786
252b5132 15787static char *
e3bb37b5 15788output_invalid (int c)
252b5132 15789{
3882b010 15790 if (ISPRINT (c))
f9f21a03
L
15791 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
15792 "'%c'", c);
252b5132 15793 else
f9f21a03 15794 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
2d545b82 15795 "(0x%x)", (unsigned char) c);
252b5132
RH
15796 return output_invalid_buf;
15797}
15798
8a6fb3f9
JB
15799/* Verify that @r can be used in the current context. */
15800
5b7c81bd 15801static bool check_register (const reg_entry *r)
8a6fb3f9
JB
15802{
15803 if (allow_pseudo_reg)
5b7c81bd 15804 return true;
8a6fb3f9
JB
15805
15806 if (operand_type_all_zero (&r->reg_type))
5b7c81bd 15807 return false;
8a6fb3f9
JB
15808
15809 if ((r->reg_type.bitfield.dword
15810 || (r->reg_type.bitfield.class == SReg && r->reg_num > 3)
15811 || r->reg_type.bitfield.class == RegCR
22e00a3f 15812 || r->reg_type.bitfield.class == RegDR)
8a6fb3f9 15813 && !cpu_arch_flags.bitfield.cpui386)
5b7c81bd 15814 return false;
8a6fb3f9 15815
22e00a3f
JB
15816 if (r->reg_type.bitfield.class == RegTR
15817 && (flag_code == CODE_64BIT
15818 || !cpu_arch_flags.bitfield.cpui386
15819 || cpu_arch_isa_flags.bitfield.cpui586
15820 || cpu_arch_isa_flags.bitfield.cpui686))
5b7c81bd 15821 return false;
22e00a3f 15822
8a6fb3f9 15823 if (r->reg_type.bitfield.class == RegMMX && !cpu_arch_flags.bitfield.cpummx)
5b7c81bd 15824 return false;
8a6fb3f9
JB
15825
15826 if (!cpu_arch_flags.bitfield.cpuavx512f)
15827 {
15828 if (r->reg_type.bitfield.zmmword
15829 || r->reg_type.bitfield.class == RegMask)
5b7c81bd 15830 return false;
8a6fb3f9
JB
15831
15832 if (!cpu_arch_flags.bitfield.cpuavx)
15833 {
15834 if (r->reg_type.bitfield.ymmword)
5b7c81bd 15835 return false;
8a6fb3f9
JB
15836
15837 if (!cpu_arch_flags.bitfield.cpusse && r->reg_type.bitfield.xmmword)
5b7c81bd 15838 return false;
8a6fb3f9
JB
15839 }
15840 }
15841
a6f3add0
JB
15842 if (r->reg_type.bitfield.zmmword)
15843 {
15844 if (vector_size < VSZ512)
15845 return false;
15846
eb3f3841
JB
15847 switch (i.encoding)
15848 {
15849 case encoding_default:
15850 case encoding_egpr:
15851 i.encoding = encoding_evex512;
15852 break;
15853 case encoding_evex:
15854 case encoding_evex512:
15855 break;
15856 default:
15857 i.encoding = encoding_error;
15858 break;
15859 }
a6f3add0 15860 }
4fc85f37
JB
15861
15862 if (vector_size < VSZ256 && r->reg_type.bitfield.ymmword)
15863 return false;
15864
260cd341
LC
15865 if (r->reg_type.bitfield.tmmword
15866 && (!cpu_arch_flags.bitfield.cpuamx_tile
15867 || flag_code != CODE_64BIT))
5b7c81bd 15868 return false;
260cd341 15869
8a6fb3f9 15870 if (r->reg_type.bitfield.class == RegBND && !cpu_arch_flags.bitfield.cpumpx)
5b7c81bd 15871 return false;
8a6fb3f9
JB
15872
15873 /* Don't allow fake index register unless allow_index_reg isn't 0. */
15874 if (!allow_index_reg && r->reg_num == RegIZ)
5b7c81bd 15875 return false;
8a6fb3f9
JB
15876
15877 /* Upper 16 vector registers are only available with VREX in 64bit
15878 mode, and require EVEX encoding. */
15879 if (r->reg_flags & RegVRex)
15880 {
15881 if (!cpu_arch_flags.bitfield.cpuavx512f
15882 || flag_code != CODE_64BIT)
5b7c81bd 15883 return false;
8a6fb3f9 15884
eb3f3841
JB
15885 switch (i.encoding)
15886 {
15887 case encoding_default:
15888 case encoding_egpr:
15889 case encoding_evex512:
15890 i.encoding = encoding_evex;
15891 break;
15892 case encoding_evex:
15893 break;
15894 default:
15895 i.encoding = encoding_error;
15896 break;
15897 }
8a6fb3f9
JB
15898 }
15899
80d61d8d
CL
15900 if (r->reg_flags & RegRex2)
15901 {
15902 if (!cpu_arch_flags.bitfield.cpuapx_f
15903 || flag_code != CODE_64BIT)
15904 return false;
6177c84d 15905
eb3f3841
JB
15906 switch (i.encoding)
15907 {
15908 case encoding_default:
15909 i.encoding = encoding_egpr;
15910 break;
15911 case encoding_egpr:
15912 case encoding_evex:
15913 case encoding_evex512:
15914 break;
15915 default:
15916 i.encoding = encoding_error;
15917 break;
15918 }
80d61d8d
CL
15919 }
15920
8a6fb3f9 15921 if (((r->reg_flags & (RegRex64 | RegRex)) || r->reg_type.bitfield.qword)
da5f9eb4 15922 && (!cpu_arch_flags.bitfield.cpu64
0ff3b7d0
JB
15923 || r->reg_type.bitfield.class != RegCR
15924 || dot_insn ())
8a6fb3f9 15925 && flag_code != CODE_64BIT)
5b7c81bd 15926 return false;
8a6fb3f9
JB
15927
15928 if (r->reg_type.bitfield.class == SReg && r->reg_num == RegFlat
15929 && !intel_syntax)
5b7c81bd 15930 return false;
8a6fb3f9 15931
5b7c81bd 15932 return true;
8a6fb3f9
JB
15933}
15934
af6bdddf 15935/* REG_STRING starts *before* REGISTER_PREFIX. */
252b5132
RH
15936
15937static const reg_entry *
74e05e01 15938parse_real_register (const char *reg_string, char **end_op)
252b5132 15939{
74e05e01 15940 const char *s = reg_string;
af6bdddf 15941 char *p;
252b5132
RH
15942 char reg_name_given[MAX_REG_NAME_SIZE + 1];
15943 const reg_entry *r;
15944
15945 /* Skip possible REGISTER_PREFIX and possible whitespace. */
15946 if (*s == REGISTER_PREFIX)
15947 ++s;
15948
15949 if (is_space_char (*s))
15950 ++s;
15951
15952 p = reg_name_given;
af6bdddf 15953 while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
252b5132
RH
15954 {
15955 if (p >= reg_name_given + MAX_REG_NAME_SIZE)
af6bdddf
AM
15956 return (const reg_entry *) NULL;
15957 s++;
252b5132
RH
15958 }
15959
4795cd4a 15960 if (is_part_of_name (*s))
6588847e
DN
15961 return (const reg_entry *) NULL;
15962
74e05e01 15963 *end_op = (char *) s;
252b5132 15964
629310ab 15965 r = (const reg_entry *) str_hash_find (reg_hash, reg_name_given);
252b5132 15966
5f47d35b 15967 /* Handle floating point regs, allowing spaces in the (i) part. */
6288d05f 15968 if (r == reg_st0)
5f47d35b 15969 {
0e0eea78
JB
15970 if (!cpu_arch_flags.bitfield.cpu8087
15971 && !cpu_arch_flags.bitfield.cpu287
af32b722
JB
15972 && !cpu_arch_flags.bitfield.cpu387
15973 && !allow_pseudo_reg)
0e0eea78
JB
15974 return (const reg_entry *) NULL;
15975
5f47d35b
AM
15976 if (is_space_char (*s))
15977 ++s;
15978 if (*s == '(')
15979 {
af6bdddf 15980 ++s;
5f47d35b
AM
15981 if (is_space_char (*s))
15982 ++s;
15983 if (*s >= '0' && *s <= '7')
15984 {
db557034 15985 int fpr = *s - '0';
af6bdddf 15986 ++s;
5f47d35b
AM
15987 if (is_space_char (*s))
15988 ++s;
15989 if (*s == ')')
15990 {
74e05e01 15991 *end_op = (char *) s + 1;
6288d05f 15992 know (r[fpr].reg_num == fpr);
db557034 15993 return r + fpr;
5f47d35b 15994 }
5f47d35b 15995 }
47926f60 15996 /* We have "%st(" then garbage. */
5f47d35b
AM
15997 return (const reg_entry *) NULL;
15998 }
15999 }
16000
8a6fb3f9 16001 return r && check_register (r) ? r : NULL;
252b5132 16002}
4d1bb795
JB
16003
16004/* REG_STRING starts *before* REGISTER_PREFIX. */
16005
16006static const reg_entry *
4f081312 16007parse_register (const char *reg_string, char **end_op)
4d1bb795
JB
16008{
16009 const reg_entry *r;
16010
16011 if (*reg_string == REGISTER_PREFIX || allow_naked_reg)
16012 r = parse_real_register (reg_string, end_op);
16013 else
16014 r = NULL;
16015 if (!r)
16016 {
16017 char *save = input_line_pointer;
4f081312 16018 char *buf = xstrdup (reg_string), *name;
4d1bb795
JB
16019 symbolS *symbolP;
16020
4f081312
JB
16021 input_line_pointer = buf;
16022 get_symbol_name (&name);
16023 symbolP = symbol_find (name);
d50c498a 16024 while (symbolP && symbol_equated_p (symbolP))
64d23078
JB
16025 {
16026 const expressionS *e = symbol_get_value_expression(symbolP);
16027
d50c498a 16028 if (e->X_add_number)
64d23078
JB
16029 break;
16030 symbolP = e->X_add_symbol;
16031 }
4d1bb795
JB
16032 if (symbolP && S_GET_SEGMENT (symbolP) == reg_section)
16033 {
16034 const expressionS *e = symbol_get_value_expression (symbolP);
16035
0ccade1a
JB
16036 if (e->X_op == O_register)
16037 {
16038 know (e->X_add_number >= 0
16039 && (valueT) e->X_add_number < i386_regtab_size);
16040 r = i386_regtab + e->X_add_number;
4f081312 16041 *end_op = (char *) reg_string + (input_line_pointer - buf);
0ccade1a
JB
16042 }
16043 if (r && !check_register (r))
8a6fb3f9 16044 {
3b55a1d0
JB
16045 as_bad (_("register '%s%s' cannot be used here"),
16046 register_prefix, r->reg_name);
16047 r = &bad_reg;
8a6fb3f9 16048 }
4d1bb795 16049 }
4d1bb795 16050 input_line_pointer = save;
4f081312 16051 free (buf);
4d1bb795
JB
16052 }
16053 return r;
16054}
16055
16056int
16057i386_parse_name (char *name, expressionS *e, char *nextcharP)
16058{
4faaa10f 16059 const reg_entry *r = NULL;
4d1bb795
JB
16060 char *end = input_line_pointer;
16061
6acf9130
JB
16062 /* We only know the terminating character here. It being double quote could
16063 be the closing one of a quoted symbol name, or an opening one from a
16064 following string (or another quoted symbol name). Since the latter can't
16065 be valid syntax for anything, bailing in either case is good enough. */
16066 if (*nextcharP == '"')
16067 return 0;
16068
4d1bb795 16069 *end = *nextcharP;
4faaa10f
JB
16070 if (*name == REGISTER_PREFIX || allow_naked_reg)
16071 r = parse_real_register (name, &input_line_pointer);
4d1bb795
JB
16072 if (r && end <= input_line_pointer)
16073 {
16074 *nextcharP = *input_line_pointer;
16075 *input_line_pointer = 0;
f847749a
JB
16076 e->X_op = O_register;
16077 e->X_add_number = r - i386_regtab;
4d1bb795
JB
16078 return 1;
16079 }
16080 input_line_pointer = end;
16081 *end = 0;
ee86248c 16082 return intel_syntax ? i386_intel_parse_name (name, e) : 0;
4d1bb795
JB
16083}
16084
16085void
16086md_operand (expressionS *e)
16087{
ee86248c
JB
16088 char *end;
16089 const reg_entry *r;
4d1bb795 16090
ee86248c
JB
16091 switch (*input_line_pointer)
16092 {
16093 case REGISTER_PREFIX:
16094 r = parse_real_register (input_line_pointer, &end);
4d1bb795
JB
16095 if (r)
16096 {
16097 e->X_op = O_register;
16098 e->X_add_number = r - i386_regtab;
16099 input_line_pointer = end;
16100 }
ee86248c
JB
16101 break;
16102
16103 case '[':
9c2799c2 16104 gas_assert (intel_syntax);
ee86248c
JB
16105 end = input_line_pointer++;
16106 expression (e);
16107 if (*input_line_pointer == ']')
16108 {
16109 ++input_line_pointer;
16110 e->X_op_symbol = make_expr_symbol (e);
16111 e->X_add_symbol = NULL;
16112 e->X_add_number = 0;
16113 e->X_op = O_index;
16114 }
16115 else
16116 {
16117 e->X_op = O_absent;
16118 input_line_pointer = end;
16119 }
16120 break;
4d1bb795
JB
16121 }
16122}
16123
5cc00775
JB
16124#ifdef BFD64
16125/* To maintain consistency with !BFD64 builds of gas record, whether any
16126 (binary) operator was involved in an expression. As expressions are
16127 evaluated in only 32 bits when !BFD64, we use this to decide whether to
16128 truncate results. */
16129bool i386_record_operator (operatorT op,
16130 const expressionS *left,
16131 const expressionS *right)
16132{
16133 if (op == O_absent)
16134 return false;
16135
16136 if (!left)
16137 {
16138 /* Since the expression parser applies unary operators fine to bignum
16139 operands, we don't need to be concerned of respective operands not
16140 fitting in 32 bits. */
16141 if (right->X_op == O_constant && right->X_unsigned
16142 && !fits_in_unsigned_long (right->X_add_number))
16143 return false;
16144 }
16145 /* This isn't entirely right: The pattern can also result when constant
16146 expressions are folded (e.g. 0xffffffff + 1). */
16147 else if ((left->X_op == O_constant && left->X_unsigned
16148 && !fits_in_unsigned_long (left->X_add_number))
16149 || (right->X_op == O_constant && right->X_unsigned
16150 && !fits_in_unsigned_long (right->X_add_number)))
16151 expr_mode = expr_large_value;
16152
16153 if (expr_mode != expr_large_value)
16154 expr_mode = expr_operator_present;
16155
16156 return false;
16157}
16158#endif
252b5132 16159\f
4cc782b5 16160#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
b6f8c7c4 16161const char *md_shortopts = "kVQ:sqnO::";
252b5132 16162#else
b6f8c7c4 16163const char *md_shortopts = "qnO::";
252b5132 16164#endif
6e0b89ee 16165
3e73aa7c 16166#define OPTION_32 (OPTION_MD_BASE + 0)
b3b91714
AM
16167#define OPTION_64 (OPTION_MD_BASE + 1)
16168#define OPTION_DIVIDE (OPTION_MD_BASE + 2)
9103f4f4
L
16169#define OPTION_MARCH (OPTION_MD_BASE + 3)
16170#define OPTION_MTUNE (OPTION_MD_BASE + 4)
1efbbeb4
L
16171#define OPTION_MMNEMONIC (OPTION_MD_BASE + 5)
16172#define OPTION_MSYNTAX (OPTION_MD_BASE + 6)
16173#define OPTION_MINDEX_REG (OPTION_MD_BASE + 7)
16174#define OPTION_MNAKED_REG (OPTION_MD_BASE + 8)
bd5dea88 16175#define OPTION_MRELAX_RELOCATIONS (OPTION_MD_BASE + 9)
c0f3af97 16176#define OPTION_MSSE2AVX (OPTION_MD_BASE + 10)
daf50ae7 16177#define OPTION_MSSE_CHECK (OPTION_MD_BASE + 11)
7bab8ab5
JB
16178#define OPTION_MOPERAND_CHECK (OPTION_MD_BASE + 12)
16179#define OPTION_MAVXSCALAR (OPTION_MD_BASE + 13)
16180#define OPTION_X32 (OPTION_MD_BASE + 14)
7e8b059b 16181#define OPTION_MADD_BND_PREFIX (OPTION_MD_BASE + 15)
43234a1e
L
16182#define OPTION_MEVEXLIG (OPTION_MD_BASE + 16)
16183#define OPTION_MEVEXWIG (OPTION_MD_BASE + 17)
167ad85b 16184#define OPTION_MBIG_OBJ (OPTION_MD_BASE + 18)
d1982f93 16185#define OPTION_MOMIT_LOCK_PREFIX (OPTION_MD_BASE + 19)
d3d3c6db 16186#define OPTION_MEVEXRCIG (OPTION_MD_BASE + 20)
8dcea932 16187#define OPTION_MSHARED (OPTION_MD_BASE + 21)
5db04b09
L
16188#define OPTION_MAMD64 (OPTION_MD_BASE + 22)
16189#define OPTION_MINTEL64 (OPTION_MD_BASE + 23)
e4e00185 16190#define OPTION_MFENCE_AS_LOCK_ADD (OPTION_MD_BASE + 24)
b4a3a7b4 16191#define OPTION_X86_USED_NOTE (OPTION_MD_BASE + 25)
03751133 16192#define OPTION_MVEXWIG (OPTION_MD_BASE + 26)
e379e5f3
L
16193#define OPTION_MALIGN_BRANCH_BOUNDARY (OPTION_MD_BASE + 27)
16194#define OPTION_MALIGN_BRANCH_PREFIX_SIZE (OPTION_MD_BASE + 28)
16195#define OPTION_MALIGN_BRANCH (OPTION_MD_BASE + 29)
76cf450b 16196#define OPTION_MBRANCHES_WITH_32B_BOUNDARIES (OPTION_MD_BASE + 30)
ae531041
L
16197#define OPTION_MLFENCE_AFTER_LOAD (OPTION_MD_BASE + 31)
16198#define OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH (OPTION_MD_BASE + 32)
16199#define OPTION_MLFENCE_BEFORE_RET (OPTION_MD_BASE + 33)
c8480b58 16200#define OPTION_MUSE_UNALIGNED_VECTOR_MOVE (OPTION_MD_BASE + 34)
b3b91714 16201
99ad8390
NC
16202struct option md_longopts[] =
16203{
3e73aa7c 16204 {"32", no_argument, NULL, OPTION_32},
321098a5 16205#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 16206 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c 16207 {"64", no_argument, NULL, OPTION_64},
351f65ca
L
16208#endif
16209#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 16210 {"x32", no_argument, NULL, OPTION_X32},
8dcea932 16211 {"mshared", no_argument, NULL, OPTION_MSHARED},
b4a3a7b4 16212 {"mx86-used-note", required_argument, NULL, OPTION_X86_USED_NOTE},
6e0b89ee 16213#endif
b3b91714 16214 {"divide", no_argument, NULL, OPTION_DIVIDE},
9103f4f4
L
16215 {"march", required_argument, NULL, OPTION_MARCH},
16216 {"mtune", required_argument, NULL, OPTION_MTUNE},
1efbbeb4
L
16217 {"mmnemonic", required_argument, NULL, OPTION_MMNEMONIC},
16218 {"msyntax", required_argument, NULL, OPTION_MSYNTAX},
16219 {"mindex-reg", no_argument, NULL, OPTION_MINDEX_REG},
16220 {"mnaked-reg", no_argument, NULL, OPTION_MNAKED_REG},
c0f3af97 16221 {"msse2avx", no_argument, NULL, OPTION_MSSE2AVX},
c8480b58 16222 {"muse-unaligned-vector-move", no_argument, NULL, OPTION_MUSE_UNALIGNED_VECTOR_MOVE},
daf50ae7 16223 {"msse-check", required_argument, NULL, OPTION_MSSE_CHECK},
7bab8ab5 16224 {"moperand-check", required_argument, NULL, OPTION_MOPERAND_CHECK},
539f890d 16225 {"mavxscalar", required_argument, NULL, OPTION_MAVXSCALAR},
03751133 16226 {"mvexwig", required_argument, NULL, OPTION_MVEXWIG},
7e8b059b 16227 {"madd-bnd-prefix", no_argument, NULL, OPTION_MADD_BND_PREFIX},
43234a1e
L
16228 {"mevexlig", required_argument, NULL, OPTION_MEVEXLIG},
16229 {"mevexwig", required_argument, NULL, OPTION_MEVEXWIG},
167ad85b
TG
16230# if defined (TE_PE) || defined (TE_PEP)
16231 {"mbig-obj", no_argument, NULL, OPTION_MBIG_OBJ},
16232#endif
d1982f93 16233 {"momit-lock-prefix", required_argument, NULL, OPTION_MOMIT_LOCK_PREFIX},
e4e00185 16234 {"mfence-as-lock-add", required_argument, NULL, OPTION_MFENCE_AS_LOCK_ADD},
0cb4071e 16235 {"mrelax-relocations", required_argument, NULL, OPTION_MRELAX_RELOCATIONS},
d3d3c6db 16236 {"mevexrcig", required_argument, NULL, OPTION_MEVEXRCIG},
e379e5f3
L
16237 {"malign-branch-boundary", required_argument, NULL, OPTION_MALIGN_BRANCH_BOUNDARY},
16238 {"malign-branch-prefix-size", required_argument, NULL, OPTION_MALIGN_BRANCH_PREFIX_SIZE},
16239 {"malign-branch", required_argument, NULL, OPTION_MALIGN_BRANCH},
76cf450b 16240 {"mbranches-within-32B-boundaries", no_argument, NULL, OPTION_MBRANCHES_WITH_32B_BOUNDARIES},
ae531041
L
16241 {"mlfence-after-load", required_argument, NULL, OPTION_MLFENCE_AFTER_LOAD},
16242 {"mlfence-before-indirect-branch", required_argument, NULL,
16243 OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH},
16244 {"mlfence-before-ret", required_argument, NULL, OPTION_MLFENCE_BEFORE_RET},
5db04b09
L
16245 {"mamd64", no_argument, NULL, OPTION_MAMD64},
16246 {"mintel64", no_argument, NULL, OPTION_MINTEL64},
252b5132
RH
16247 {NULL, no_argument, NULL, 0}
16248};
16249size_t md_longopts_size = sizeof (md_longopts);
16250
16251int
17b9d67d 16252md_parse_option (int c, const char *arg)
252b5132 16253{
91d6fa6a 16254 unsigned int j;
e379e5f3 16255 char *arch, *next, *saved, *type;
9103f4f4 16256
252b5132
RH
16257 switch (c)
16258 {
12b55ccc
L
16259 case 'n':
16260 optimize_align_code = 0;
16261 break;
16262
a38cf1db
AM
16263 case 'q':
16264 quiet_warnings = 1;
252b5132
RH
16265 break;
16266
16267#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
a38cf1db
AM
16268 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
16269 should be emitted or not. FIXME: Not implemented. */
16270 case 'Q':
d4693039
JB
16271 if ((arg[0] != 'y' && arg[0] != 'n') || arg[1])
16272 return 0;
252b5132
RH
16273 break;
16274
16275 /* -V: SVR4 argument to print version ID. */
16276 case 'V':
16277 print_version_id ();
16278 break;
16279
a38cf1db
AM
16280 /* -k: Ignore for FreeBSD compatibility. */
16281 case 'k':
252b5132 16282 break;
4cc782b5
ILT
16283
16284 case 's':
16285 /* -s: On i386 Solaris, this tells the native assembler to use
29b0f896 16286 .stab instead of .stab.excl. We always use .stab anyhow. */
4cc782b5 16287 break;
8dcea932
L
16288
16289 case OPTION_MSHARED:
16290 shared = 1;
16291 break;
b4a3a7b4
L
16292
16293 case OPTION_X86_USED_NOTE:
16294 if (strcasecmp (arg, "yes") == 0)
16295 x86_used_note = 1;
16296 else if (strcasecmp (arg, "no") == 0)
16297 x86_used_note = 0;
16298 else
16299 as_fatal (_("invalid -mx86-used-note= option: `%s'"), arg);
16300 break;
16301
16302
99ad8390 16303#endif
321098a5 16304#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 16305 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c
JH
16306 case OPTION_64:
16307 {
16308 const char **list, **l;
16309
3e73aa7c
JH
16310 list = bfd_target_list ();
16311 for (l = list; *l != NULL; l++)
08dedd66 16312 if (startswith (*l, "elf64-x86-64")
99ad8390
NC
16313 || strcmp (*l, "coff-x86-64") == 0
16314 || strcmp (*l, "pe-x86-64") == 0
d382c579
TG
16315 || strcmp (*l, "pei-x86-64") == 0
16316 || strcmp (*l, "mach-o-x86-64") == 0)
6e0b89ee
AM
16317 {
16318 default_arch = "x86_64";
16319 break;
16320 }
3e73aa7c 16321 if (*l == NULL)
2b5d6a91 16322 as_fatal (_("no compiled in support for x86_64"));
3e73aa7c
JH
16323 free (list);
16324 }
16325 break;
16326#endif
252b5132 16327
351f65ca 16328#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 16329 case OPTION_X32:
351f65ca
L
16330 if (IS_ELF)
16331 {
16332 const char **list, **l;
16333
16334 list = bfd_target_list ();
16335 for (l = list; *l != NULL; l++)
08dedd66 16336 if (startswith (*l, "elf32-x86-64"))
351f65ca
L
16337 {
16338 default_arch = "x86_64:32";
16339 break;
16340 }
16341 if (*l == NULL)
2b5d6a91 16342 as_fatal (_("no compiled in support for 32bit x86_64"));
351f65ca
L
16343 free (list);
16344 }
16345 else
16346 as_fatal (_("32bit x86_64 is only supported for ELF"));
16347 break;
16348#endif
16349
6e0b89ee 16350 case OPTION_32:
590a0fed
JB
16351 {
16352 const char **list, **l;
16353
16354 list = bfd_target_list ();
16355 for (l = list; *l != NULL; l++)
16356 if (strstr (*l, "-i386")
16357 || strstr (*l, "-go32"))
16358 {
16359 default_arch = "i386";
16360 break;
16361 }
16362 if (*l == NULL)
16363 as_fatal (_("no compiled in support for ix86"));
16364 free (list);
16365 }
6e0b89ee
AM
16366 break;
16367
b3b91714
AM
16368 case OPTION_DIVIDE:
16369#ifdef SVR4_COMMENT_CHARS
16370 {
16371 char *n, *t;
16372 const char *s;
16373
add39d23 16374 n = XNEWVEC (char, strlen (i386_comment_chars) + 1);
b3b91714
AM
16375 t = n;
16376 for (s = i386_comment_chars; *s != '\0'; s++)
16377 if (*s != '/')
16378 *t++ = *s;
16379 *t = '\0';
16380 i386_comment_chars = n;
16381 }
16382#endif
16383 break;
16384
9103f4f4 16385 case OPTION_MARCH:
293f5f65
L
16386 saved = xstrdup (arg);
16387 arch = saved;
16388 /* Allow -march=+nosse. */
16389 if (*arch == '+')
16390 arch++;
6305a203 16391 do
9103f4f4 16392 {
4fc85f37
JB
16393 char *vsz;
16394
6305a203 16395 if (*arch == '.')
2b5d6a91 16396 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
16397 next = strchr (arch, '+');
16398 if (next)
16399 *next++ = '\0';
4fc85f37
JB
16400 vsz = strchr (arch, '/');
16401 if (vsz)
16402 *vsz++ = '\0';
91d6fa6a 16403 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 16404 {
4fc85f37
JB
16405 if (vsz && cpu_arch[j].vsz != vsz_set)
16406 continue;
16407
ae89daec
JB
16408 if (arch == saved && cpu_arch[j].type != PROCESSOR_NONE
16409 && strcmp (arch, cpu_arch[j].name) == 0)
ccc9c027 16410 {
6305a203 16411 /* Processor. */
ae89daec 16412 if (! cpu_arch[j].enable.bitfield.cpui386)
1ded5609
JB
16413 continue;
16414
91d6fa6a 16415 cpu_arch_name = cpu_arch[j].name;
d92c7521 16416 free (cpu_sub_arch_name);
6305a203 16417 cpu_sub_arch_name = NULL;
ae89daec 16418 cpu_arch_flags = cpu_arch[j].enable;
91d6fa6a 16419 cpu_arch_isa = cpu_arch[j].type;
ae89daec 16420 cpu_arch_isa_flags = cpu_arch[j].enable;
6305a203 16421 if (!cpu_arch_tune_set)
fb263707 16422 cpu_arch_tune = cpu_arch_isa;
4fc85f37 16423 vector_size = VSZ_DEFAULT;
6305a203
L
16424 break;
16425 }
ae89daec
JB
16426 else if (cpu_arch[j].type == PROCESSOR_NONE
16427 && strcmp (arch, cpu_arch[j].name) == 0
16428 && !cpu_flags_all_zero (&cpu_arch[j].enable))
6305a203 16429 {
33eaf5de 16430 /* ISA extension. */
bd483d21 16431 isa_enable (j);
4fc85f37
JB
16432
16433 switch (cpu_arch[j].vsz)
16434 {
16435 default:
16436 break;
16437
16438 case vsz_set:
16439 if (vsz)
16440 {
16441 char *end;
16442 unsigned long val = strtoul (vsz, &end, 0);
16443
16444 if (*end)
16445 val = 0;
16446 switch (val)
16447 {
16448 case 512: vector_size = VSZ512; break;
16449 case 256: vector_size = VSZ256; break;
16450 case 128: vector_size = VSZ128; break;
16451 default:
16452 as_warn (_("Unrecognized vector size specifier ignored"));
16453 break;
16454 }
16455 break;
16456 }
16457 /* Fall through. */
16458 case vsz_reset:
16459 vector_size = VSZ_DEFAULT;
16460 break;
16461 }
16462
6305a203 16463 break;
ccc9c027 16464 }
9103f4f4 16465 }
6305a203 16466
ae89daec 16467 if (j >= ARRAY_SIZE (cpu_arch) && startswith (arch, "no"))
293f5f65 16468 {
33eaf5de 16469 /* Disable an ISA extension. */
ae89daec
JB
16470 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
16471 if (cpu_arch[j].type == PROCESSOR_NONE
16472 && strcmp (arch + 2, cpu_arch[j].name) == 0)
293f5f65 16473 {
bd483d21 16474 isa_disable (j);
4fc85f37
JB
16475 if (cpu_arch[j].vsz == vsz_set)
16476 vector_size = VSZ_DEFAULT;
293f5f65
L
16477 break;
16478 }
293f5f65
L
16479 }
16480
91d6fa6a 16481 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 16482 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
16483
16484 arch = next;
9103f4f4 16485 }
293f5f65
L
16486 while (next != NULL);
16487 free (saved);
9103f4f4
L
16488 break;
16489
16490 case OPTION_MTUNE:
16491 if (*arg == '.')
2b5d6a91 16492 as_fatal (_("invalid -mtune= option: `%s'"), arg);
91d6fa6a 16493 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 16494 {
ae89daec
JB
16495 if (cpu_arch[j].type != PROCESSOR_NONE
16496 && strcmp (arg, cpu_arch[j].name) == 0)
9103f4f4 16497 {
ccc9c027 16498 cpu_arch_tune_set = 1;
91d6fa6a 16499 cpu_arch_tune = cpu_arch [j].type;
9103f4f4
L
16500 break;
16501 }
16502 }
91d6fa6a 16503 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 16504 as_fatal (_("invalid -mtune= option: `%s'"), arg);
9103f4f4
L
16505 break;
16506
1efbbeb4
L
16507 case OPTION_MMNEMONIC:
16508 if (strcasecmp (arg, "att") == 0)
16509 intel_mnemonic = 0;
16510 else if (strcasecmp (arg, "intel") == 0)
16511 intel_mnemonic = 1;
16512 else
2b5d6a91 16513 as_fatal (_("invalid -mmnemonic= option: `%s'"), arg);
1efbbeb4
L
16514 break;
16515
16516 case OPTION_MSYNTAX:
16517 if (strcasecmp (arg, "att") == 0)
213f15cf 16518 _set_intel_syntax (0);
1efbbeb4 16519 else if (strcasecmp (arg, "intel") == 0)
213f15cf 16520 _set_intel_syntax (1);
1efbbeb4 16521 else
2b5d6a91 16522 as_fatal (_("invalid -msyntax= option: `%s'"), arg);
1efbbeb4
L
16523 break;
16524
16525 case OPTION_MINDEX_REG:
16526 allow_index_reg = 1;
16527 break;
16528
16529 case OPTION_MNAKED_REG:
16530 allow_naked_reg = 1;
213f15cf 16531 register_prefix = "";
1efbbeb4
L
16532 break;
16533
c0f3af97
L
16534 case OPTION_MSSE2AVX:
16535 sse2avx = 1;
16536 break;
16537
c8480b58
L
16538 case OPTION_MUSE_UNALIGNED_VECTOR_MOVE:
16539 use_unaligned_vector_move = 1;
16540 break;
16541
daf50ae7
L
16542 case OPTION_MSSE_CHECK:
16543 if (strcasecmp (arg, "error") == 0)
7bab8ab5 16544 sse_check = check_error;
daf50ae7 16545 else if (strcasecmp (arg, "warning") == 0)
7bab8ab5 16546 sse_check = check_warning;
daf50ae7 16547 else if (strcasecmp (arg, "none") == 0)
7bab8ab5 16548 sse_check = check_none;
daf50ae7 16549 else
2b5d6a91 16550 as_fatal (_("invalid -msse-check= option: `%s'"), arg);
daf50ae7
L
16551 break;
16552
7bab8ab5
JB
16553 case OPTION_MOPERAND_CHECK:
16554 if (strcasecmp (arg, "error") == 0)
16555 operand_check = check_error;
16556 else if (strcasecmp (arg, "warning") == 0)
16557 operand_check = check_warning;
16558 else if (strcasecmp (arg, "none") == 0)
16559 operand_check = check_none;
16560 else
16561 as_fatal (_("invalid -moperand-check= option: `%s'"), arg);
16562 break;
16563
539f890d
L
16564 case OPTION_MAVXSCALAR:
16565 if (strcasecmp (arg, "128") == 0)
16566 avxscalar = vex128;
16567 else if (strcasecmp (arg, "256") == 0)
16568 avxscalar = vex256;
16569 else
2b5d6a91 16570 as_fatal (_("invalid -mavxscalar= option: `%s'"), arg);
539f890d
L
16571 break;
16572
03751133
L
16573 case OPTION_MVEXWIG:
16574 if (strcmp (arg, "0") == 0)
40c9c8de 16575 vexwig = vexw0;
03751133 16576 else if (strcmp (arg, "1") == 0)
40c9c8de 16577 vexwig = vexw1;
03751133
L
16578 else
16579 as_fatal (_("invalid -mvexwig= option: `%s'"), arg);
16580 break;
16581
7e8b059b
L
16582 case OPTION_MADD_BND_PREFIX:
16583 add_bnd_prefix = 1;
16584 break;
16585
43234a1e
L
16586 case OPTION_MEVEXLIG:
16587 if (strcmp (arg, "128") == 0)
16588 evexlig = evexl128;
16589 else if (strcmp (arg, "256") == 0)
16590 evexlig = evexl256;
16591 else if (strcmp (arg, "512") == 0)
16592 evexlig = evexl512;
16593 else
16594 as_fatal (_("invalid -mevexlig= option: `%s'"), arg);
16595 break;
16596
d3d3c6db
IT
16597 case OPTION_MEVEXRCIG:
16598 if (strcmp (arg, "rne") == 0)
16599 evexrcig = rne;
16600 else if (strcmp (arg, "rd") == 0)
16601 evexrcig = rd;
16602 else if (strcmp (arg, "ru") == 0)
16603 evexrcig = ru;
16604 else if (strcmp (arg, "rz") == 0)
16605 evexrcig = rz;
16606 else
16607 as_fatal (_("invalid -mevexrcig= option: `%s'"), arg);
16608 break;
16609
43234a1e
L
16610 case OPTION_MEVEXWIG:
16611 if (strcmp (arg, "0") == 0)
16612 evexwig = evexw0;
16613 else if (strcmp (arg, "1") == 0)
16614 evexwig = evexw1;
16615 else
16616 as_fatal (_("invalid -mevexwig= option: `%s'"), arg);
16617 break;
16618
167ad85b
TG
16619# if defined (TE_PE) || defined (TE_PEP)
16620 case OPTION_MBIG_OBJ:
16621 use_big_obj = 1;
16622 break;
16623#endif
16624
d1982f93 16625 case OPTION_MOMIT_LOCK_PREFIX:
d022bddd
IT
16626 if (strcasecmp (arg, "yes") == 0)
16627 omit_lock_prefix = 1;
16628 else if (strcasecmp (arg, "no") == 0)
16629 omit_lock_prefix = 0;
16630 else
16631 as_fatal (_("invalid -momit-lock-prefix= option: `%s'"), arg);
16632 break;
16633
e4e00185
AS
16634 case OPTION_MFENCE_AS_LOCK_ADD:
16635 if (strcasecmp (arg, "yes") == 0)
16636 avoid_fence = 1;
16637 else if (strcasecmp (arg, "no") == 0)
16638 avoid_fence = 0;
16639 else
16640 as_fatal (_("invalid -mfence-as-lock-add= option: `%s'"), arg);
16641 break;
16642
ae531041
L
16643 case OPTION_MLFENCE_AFTER_LOAD:
16644 if (strcasecmp (arg, "yes") == 0)
16645 lfence_after_load = 1;
16646 else if (strcasecmp (arg, "no") == 0)
16647 lfence_after_load = 0;
16648 else
16649 as_fatal (_("invalid -mlfence-after-load= option: `%s'"), arg);
16650 break;
16651
16652 case OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH:
16653 if (strcasecmp (arg, "all") == 0)
a09f656b 16654 {
16655 lfence_before_indirect_branch = lfence_branch_all;
16656 if (lfence_before_ret == lfence_before_ret_none)
16657 lfence_before_ret = lfence_before_ret_shl;
16658 }
ae531041
L
16659 else if (strcasecmp (arg, "memory") == 0)
16660 lfence_before_indirect_branch = lfence_branch_memory;
16661 else if (strcasecmp (arg, "register") == 0)
16662 lfence_before_indirect_branch = lfence_branch_register;
16663 else if (strcasecmp (arg, "none") == 0)
16664 lfence_before_indirect_branch = lfence_branch_none;
16665 else
16666 as_fatal (_("invalid -mlfence-before-indirect-branch= option: `%s'"),
16667 arg);
16668 break;
16669
16670 case OPTION_MLFENCE_BEFORE_RET:
16671 if (strcasecmp (arg, "or") == 0)
16672 lfence_before_ret = lfence_before_ret_or;
16673 else if (strcasecmp (arg, "not") == 0)
16674 lfence_before_ret = lfence_before_ret_not;
a09f656b 16675 else if (strcasecmp (arg, "shl") == 0 || strcasecmp (arg, "yes") == 0)
16676 lfence_before_ret = lfence_before_ret_shl;
ae531041
L
16677 else if (strcasecmp (arg, "none") == 0)
16678 lfence_before_ret = lfence_before_ret_none;
16679 else
16680 as_fatal (_("invalid -mlfence-before-ret= option: `%s'"),
16681 arg);
16682 break;
16683
0cb4071e
L
16684 case OPTION_MRELAX_RELOCATIONS:
16685 if (strcasecmp (arg, "yes") == 0)
16686 generate_relax_relocations = 1;
16687 else if (strcasecmp (arg, "no") == 0)
16688 generate_relax_relocations = 0;
16689 else
16690 as_fatal (_("invalid -mrelax-relocations= option: `%s'"), arg);
16691 break;
16692
e379e5f3
L
16693 case OPTION_MALIGN_BRANCH_BOUNDARY:
16694 {
16695 char *end;
16696 long int align = strtoul (arg, &end, 0);
16697 if (*end == '\0')
16698 {
16699 if (align == 0)
16700 {
16701 align_branch_power = 0;
16702 break;
16703 }
16704 else if (align >= 16)
16705 {
16706 int align_power;
16707 for (align_power = 0;
16708 (align & 1) == 0;
16709 align >>= 1, align_power++)
16710 continue;
16711 /* Limit alignment power to 31. */
16712 if (align == 1 && align_power < 32)
16713 {
16714 align_branch_power = align_power;
16715 break;
16716 }
16717 }
16718 }
16719 as_fatal (_("invalid -malign-branch-boundary= value: %s"), arg);
16720 }
16721 break;
16722
16723 case OPTION_MALIGN_BRANCH_PREFIX_SIZE:
16724 {
16725 char *end;
16726 int align = strtoul (arg, &end, 0);
16727 /* Some processors only support 5 prefixes. */
16728 if (*end == '\0' && align >= 0 && align < 6)
16729 {
16730 align_branch_prefix_size = align;
16731 break;
16732 }
16733 as_fatal (_("invalid -malign-branch-prefix-size= value: %s"),
16734 arg);
16735 }
16736 break;
16737
16738 case OPTION_MALIGN_BRANCH:
16739 align_branch = 0;
16740 saved = xstrdup (arg);
16741 type = saved;
16742 do
16743 {
16744 next = strchr (type, '+');
16745 if (next)
16746 *next++ = '\0';
16747 if (strcasecmp (type, "jcc") == 0)
16748 align_branch |= align_branch_jcc_bit;
16749 else if (strcasecmp (type, "fused") == 0)
16750 align_branch |= align_branch_fused_bit;
16751 else if (strcasecmp (type, "jmp") == 0)
16752 align_branch |= align_branch_jmp_bit;
16753 else if (strcasecmp (type, "call") == 0)
16754 align_branch |= align_branch_call_bit;
16755 else if (strcasecmp (type, "ret") == 0)
16756 align_branch |= align_branch_ret_bit;
16757 else if (strcasecmp (type, "indirect") == 0)
16758 align_branch |= align_branch_indirect_bit;
16759 else
16760 as_fatal (_("invalid -malign-branch= option: `%s'"), arg);
16761 type = next;
16762 }
16763 while (next != NULL);
16764 free (saved);
16765 break;
16766
76cf450b
L
16767 case OPTION_MBRANCHES_WITH_32B_BOUNDARIES:
16768 align_branch_power = 5;
16769 align_branch_prefix_size = 5;
16770 align_branch = (align_branch_jcc_bit
16771 | align_branch_fused_bit
16772 | align_branch_jmp_bit);
16773 break;
16774
5db04b09 16775 case OPTION_MAMD64:
4b5aaf5f 16776 isa64 = amd64;
5db04b09
L
16777 break;
16778
16779 case OPTION_MINTEL64:
4b5aaf5f 16780 isa64 = intel64;
5db04b09
L
16781 break;
16782
b6f8c7c4
L
16783 case 'O':
16784 if (arg == NULL)
16785 {
16786 optimize = 1;
16787 /* Turn off -Os. */
16788 optimize_for_space = 0;
16789 }
16790 else if (*arg == 's')
16791 {
16792 optimize_for_space = 1;
16793 /* Turn on all encoding optimizations. */
41fd2579 16794 optimize = INT_MAX;
b6f8c7c4
L
16795 }
16796 else
16797 {
16798 optimize = atoi (arg);
16799 /* Turn off -Os. */
16800 optimize_for_space = 0;
16801 }
16802 break;
16803
252b5132
RH
16804 default:
16805 return 0;
16806 }
16807 return 1;
16808}
16809
8a2c8fef
L
16810#define MESSAGE_TEMPLATE \
16811" "
16812
293f5f65
L
16813static char *
16814output_message (FILE *stream, char *p, char *message, char *start,
16815 int *left_p, const char *name, int len)
16816{
16817 int size = sizeof (MESSAGE_TEMPLATE);
16818 int left = *left_p;
16819
16820 /* Reserve 2 spaces for ", " or ",\0" */
16821 left -= len + 2;
16822
16823 /* Check if there is any room. */
16824 if (left >= 0)
16825 {
16826 if (p != start)
16827 {
16828 *p++ = ',';
16829 *p++ = ' ';
16830 }
16831 p = mempcpy (p, name, len);
16832 }
16833 else
16834 {
16835 /* Output the current message now and start a new one. */
16836 *p++ = ',';
16837 *p = '\0';
16838 fprintf (stream, "%s\n", message);
16839 p = start;
16840 left = size - (start - message) - len - 2;
16841
16842 gas_assert (left >= 0);
16843
16844 p = mempcpy (p, name, len);
16845 }
16846
16847 *left_p = left;
16848 return p;
16849}
16850
8a2c8fef 16851static void
1ded5609 16852show_arch (FILE *stream, int ext, int check)
8a2c8fef
L
16853{
16854 static char message[] = MESSAGE_TEMPLATE;
16855 char *start = message + 27;
16856 char *p;
16857 int size = sizeof (MESSAGE_TEMPLATE);
16858 int left;
16859 const char *name;
16860 int len;
16861 unsigned int j;
16862
16863 p = start;
16864 left = size - (start - message);
3ce2ebcf
JB
16865
16866 if (!ext && check)
16867 {
16868 p = output_message (stream, p, message, start, &left,
16869 STRING_COMMA_LEN ("default"));
f68697e8
JB
16870 p = output_message (stream, p, message, start, &left,
16871 STRING_COMMA_LEN ("push"));
16872 p = output_message (stream, p, message, start, &left,
16873 STRING_COMMA_LEN ("pop"));
3ce2ebcf
JB
16874 }
16875
8a2c8fef
L
16876 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
16877 {
16878 /* Should it be skipped? */
16879 if (cpu_arch [j].skip)
16880 continue;
16881
16882 name = cpu_arch [j].name;
16883 len = cpu_arch [j].len;
ae89daec 16884 if (cpu_arch[j].type == PROCESSOR_NONE)
8a2c8fef
L
16885 {
16886 /* It is an extension. Skip if we aren't asked to show it. */
ae89daec 16887 if (!ext || cpu_flags_all_zero (&cpu_arch[j].enable))
8a2c8fef
L
16888 continue;
16889 }
16890 else if (ext)
16891 {
16892 /* It is an processor. Skip if we show only extension. */
16893 continue;
16894 }
ae89daec 16895 else if (check && ! cpu_arch[j].enable.bitfield.cpui386)
1ded5609
JB
16896 {
16897 /* It is an impossible processor - skip. */
16898 continue;
16899 }
8a2c8fef 16900
293f5f65 16901 p = output_message (stream, p, message, start, &left, name, len);
8a2c8fef
L
16902 }
16903
293f5f65
L
16904 /* Display disabled extensions. */
16905 if (ext)
ae89daec 16906 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
293f5f65 16907 {
ae89daec
JB
16908 char *str;
16909
16910 if (cpu_arch[j].type != PROCESSOR_NONE
16911 || !cpu_flags_all_zero (&cpu_arch[j].enable))
16912 continue;
16913 str = xasprintf ("no%s", cpu_arch[j].name);
16914 p = output_message (stream, p, message, start, &left, str,
16915 strlen (str));
16916 free (str);
293f5f65
L
16917 }
16918
8a2c8fef
L
16919 *p = '\0';
16920 fprintf (stream, "%s\n", message);
16921}
16922
252b5132 16923void
8a2c8fef 16924md_show_usage (FILE *stream)
252b5132 16925{
4cc782b5
ILT
16926#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
16927 fprintf (stream, _("\
d4693039 16928 -Qy, -Qn ignored\n\
a38cf1db 16929 -V print assembler version number\n\
b3b91714
AM
16930 -k ignored\n"));
16931#endif
16932 fprintf (stream, _("\
7ebd68d1
NC
16933 -n do not optimize code alignment\n\
16934 -O{012s} attempt some code optimizations\n\
b3b91714
AM
16935 -q quieten some warnings\n"));
16936#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
16937 fprintf (stream, _("\
a38cf1db 16938 -s ignored\n"));
b3b91714 16939#endif
b00af7c8
JB
16940#ifdef BFD64
16941# if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
16942 fprintf (stream, _("\
16943 --32/--64/--x32 generate 32bit/64bit/x32 object\n"));
16944# elif defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O)
751d281c 16945 fprintf (stream, _("\
b00af7c8
JB
16946 --32/--64 generate 32bit/64bit object\n"));
16947# endif
751d281c 16948#endif
b3b91714
AM
16949#ifdef SVR4_COMMENT_CHARS
16950 fprintf (stream, _("\
16951 --divide do not treat `/' as a comment character\n"));
a38cf1db
AM
16952#else
16953 fprintf (stream, _("\
b3b91714 16954 --divide ignored\n"));
4cc782b5 16955#endif
9103f4f4 16956 fprintf (stream, _("\
6305a203 16957 -march=CPU[,+EXTENSION...]\n\
8a2c8fef 16958 generate code for CPU and EXTENSION, CPU is one of:\n"));
1ded5609 16959 show_arch (stream, 0, 1);
8a2c8fef 16960 fprintf (stream, _("\
ae89daec 16961 EXTENSION is combination of (possibly \"no\"-prefixed):\n"));
1ded5609 16962 show_arch (stream, 1, 0);
6305a203 16963 fprintf (stream, _("\
8a2c8fef 16964 -mtune=CPU optimize for CPU, CPU is one of:\n"));
1ded5609 16965 show_arch (stream, 0, 0);
ba104c83 16966 fprintf (stream, _("\
c0f3af97
L
16967 -msse2avx encode SSE instructions with VEX prefix\n"));
16968 fprintf (stream, _("\
c8480b58
L
16969 -muse-unaligned-vector-move\n\
16970 encode aligned vector move as unaligned vector move\n"));
16971 fprintf (stream, _("\
7a6a03c4 16972 -msse-check=[none|error|warning] (default: none)\n\
daf50ae7
L
16973 check SSE instructions\n"));
16974 fprintf (stream, _("\
7c5c05ef 16975 -moperand-check=[none|error|warning] (default: warning)\n\
7bab8ab5
JB
16976 check operand combinations for validity\n"));
16977 fprintf (stream, _("\
7c5c05ef
L
16978 -mavxscalar=[128|256] (default: 128)\n\
16979 encode scalar AVX instructions with specific vector\n\
539f890d
L
16980 length\n"));
16981 fprintf (stream, _("\
03751133
L
16982 -mvexwig=[0|1] (default: 0)\n\
16983 encode VEX instructions with specific VEX.W value\n\
16984 for VEX.W bit ignored instructions\n"));
16985 fprintf (stream, _("\
7c5c05ef
L
16986 -mevexlig=[128|256|512] (default: 128)\n\
16987 encode scalar EVEX instructions with specific vector\n\
43234a1e
L
16988 length\n"));
16989 fprintf (stream, _("\
7c5c05ef
L
16990 -mevexwig=[0|1] (default: 0)\n\
16991 encode EVEX instructions with specific EVEX.W value\n\
43234a1e
L
16992 for EVEX.W bit ignored instructions\n"));
16993 fprintf (stream, _("\
7c5c05ef 16994 -mevexrcig=[rne|rd|ru|rz] (default: rne)\n\
d3d3c6db
IT
16995 encode EVEX instructions with specific EVEX.RC value\n\
16996 for SAE-only ignored instructions\n"));
16997 fprintf (stream, _("\
7c5c05ef
L
16998 -mmnemonic=[att|intel] "));
16999 if (SYSV386_COMPAT)
17000 fprintf (stream, _("(default: att)\n"));
17001 else
17002 fprintf (stream, _("(default: intel)\n"));
17003 fprintf (stream, _("\
7d3182d6 17004 use AT&T/Intel mnemonic (AT&T syntax only)\n"));
ba104c83 17005 fprintf (stream, _("\
7c5c05ef
L
17006 -msyntax=[att|intel] (default: att)\n\
17007 use AT&T/Intel syntax\n"));
ba104c83
L
17008 fprintf (stream, _("\
17009 -mindex-reg support pseudo index registers\n"));
17010 fprintf (stream, _("\
17011 -mnaked-reg don't require `%%' prefix for registers\n"));
17012 fprintf (stream, _("\
7e8b059b 17013 -madd-bnd-prefix add BND prefix for all valid branches\n"));
b4a3a7b4 17014#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8dcea932
L
17015 fprintf (stream, _("\
17016 -mshared disable branch optimization for shared code\n"));
b4a3a7b4
L
17017 fprintf (stream, _("\
17018 -mx86-used-note=[no|yes] "));
17019 if (DEFAULT_X86_USED_NOTE)
17020 fprintf (stream, _("(default: yes)\n"));
17021 else
17022 fprintf (stream, _("(default: no)\n"));
17023 fprintf (stream, _("\
17024 generate x86 used ISA and feature properties\n"));
17025#endif
17026#if defined (TE_PE) || defined (TE_PEP)
167ad85b
TG
17027 fprintf (stream, _("\
17028 -mbig-obj generate big object files\n"));
17029#endif
d022bddd 17030 fprintf (stream, _("\
7c5c05ef 17031 -momit-lock-prefix=[no|yes] (default: no)\n\
d022bddd 17032 strip all lock prefixes\n"));
5db04b09 17033 fprintf (stream, _("\
7c5c05ef 17034 -mfence-as-lock-add=[no|yes] (default: no)\n\
e4e00185
AS
17035 encode lfence, mfence and sfence as\n\
17036 lock addl $0x0, (%%{re}sp)\n"));
17037 fprintf (stream, _("\
7c5c05ef
L
17038 -mrelax-relocations=[no|yes] "));
17039 if (DEFAULT_GENERATE_X86_RELAX_RELOCATIONS)
17040 fprintf (stream, _("(default: yes)\n"));
17041 else
17042 fprintf (stream, _("(default: no)\n"));
17043 fprintf (stream, _("\
0cb4071e
L
17044 generate relax relocations\n"));
17045 fprintf (stream, _("\
e379e5f3
L
17046 -malign-branch-boundary=NUM (default: 0)\n\
17047 align branches within NUM byte boundary\n"));
17048 fprintf (stream, _("\
17049 -malign-branch=TYPE[+TYPE...] (default: jcc+fused+jmp)\n\
17050 TYPE is combination of jcc, fused, jmp, call, ret,\n\
17051 indirect\n\
17052 specify types of branches to align\n"));
17053 fprintf (stream, _("\
17054 -malign-branch-prefix-size=NUM (default: 5)\n\
17055 align branches with NUM prefixes per instruction\n"));
17056 fprintf (stream, _("\
76cf450b
L
17057 -mbranches-within-32B-boundaries\n\
17058 align branches within 32 byte boundary\n"));
17059 fprintf (stream, _("\
ae531041
L
17060 -mlfence-after-load=[no|yes] (default: no)\n\
17061 generate lfence after load\n"));
17062 fprintf (stream, _("\
17063 -mlfence-before-indirect-branch=[none|all|register|memory] (default: none)\n\
17064 generate lfence before indirect near branch\n"));
17065 fprintf (stream, _("\
a09f656b 17066 -mlfence-before-ret=[none|or|not|shl|yes] (default: none)\n\
ae531041
L
17067 generate lfence before ret\n"));
17068 fprintf (stream, _("\
7c5c05ef 17069 -mamd64 accept only AMD64 ISA [default]\n"));
5db04b09
L
17070 fprintf (stream, _("\
17071 -mintel64 accept only Intel64 ISA\n"));
252b5132
RH
17072}
17073
3e73aa7c 17074#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
321098a5 17075 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
e57f8c65 17076 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
252b5132
RH
17077
17078/* Pick the target format to use. */
17079
47926f60 17080const char *
e3bb37b5 17081i386_target_format (void)
252b5132 17082{
d34049e8 17083 if (startswith (default_arch, "x86_64"))
351f65ca
L
17084 {
17085 update_code_flag (CODE_64BIT, 1);
071c5d81 17086#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
351f65ca 17087 if (default_arch[6] == '\0')
7f56bc95 17088 x86_elf_abi = X86_64_ABI;
351f65ca 17089 else
7f56bc95 17090 x86_elf_abi = X86_64_X32_ABI;
071c5d81 17091#endif
351f65ca 17092 }
3e73aa7c 17093 else if (!strcmp (default_arch, "i386"))
78f12dd3 17094 update_code_flag (CODE_32BIT, 1);
5197d474
L
17095 else if (!strcmp (default_arch, "iamcu"))
17096 {
17097 update_code_flag (CODE_32BIT, 1);
17098 if (cpu_arch_isa == PROCESSOR_UNKNOWN)
17099 {
17100 static const i386_cpu_flags iamcu_flags = CPU_IAMCU_FLAGS;
17101 cpu_arch_name = "iamcu";
d92c7521 17102 free (cpu_sub_arch_name);
5197d474
L
17103 cpu_sub_arch_name = NULL;
17104 cpu_arch_flags = iamcu_flags;
17105 cpu_arch_isa = PROCESSOR_IAMCU;
17106 cpu_arch_isa_flags = iamcu_flags;
17107 if (!cpu_arch_tune_set)
fb263707 17108 cpu_arch_tune = PROCESSOR_IAMCU;
5197d474 17109 }
8d471ec1 17110 else if (cpu_arch_isa != PROCESSOR_IAMCU)
5197d474
L
17111 as_fatal (_("Intel MCU doesn't support `%s' architecture"),
17112 cpu_arch_name);
17113 }
3e73aa7c 17114 else
2b5d6a91 17115 as_fatal (_("unknown architecture"));
89507696 17116
c7defc53
IB
17117#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
17118 if (IS_ELF && flag_synth_cfi && x86_elf_abi != X86_64_ABI)
17119 as_fatal (_("SCFI is not supported for this ABI"));
17120#endif
17121
89507696 17122 if (cpu_flags_all_zero (&cpu_arch_isa_flags))
ae89daec 17123 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
89507696 17124
252b5132
RH
17125 switch (OUTPUT_FLAVOR)
17126 {
9384f2ff 17127#if defined (OBJ_MAYBE_AOUT) || defined (OBJ_AOUT)
4c63da97 17128 case bfd_target_aout_flavour:
47926f60 17129 return AOUT_TARGET_FORMAT;
4c63da97 17130#endif
9384f2ff
AM
17131#if defined (OBJ_MAYBE_COFF) || defined (OBJ_COFF)
17132# if defined (TE_PE) || defined (TE_PEP)
17133 case bfd_target_coff_flavour:
167ad85b 17134 if (flag_code == CODE_64BIT)
eb19308f
JB
17135 {
17136 object_64bit = 1;
17137 return use_big_obj ? "pe-bigobj-x86-64" : "pe-x86-64";
17138 }
17139 return use_big_obj ? "pe-bigobj-i386" : "pe-i386";
9384f2ff 17140# elif defined (TE_GO32)
0561d57c
JK
17141 case bfd_target_coff_flavour:
17142 return "coff-go32";
9384f2ff 17143# else
252b5132
RH
17144 case bfd_target_coff_flavour:
17145 return "coff-i386";
9384f2ff 17146# endif
4c63da97 17147#endif
3e73aa7c 17148#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
252b5132 17149 case bfd_target_elf_flavour:
3e73aa7c 17150 {
351f65ca
L
17151 const char *format;
17152
17153 switch (x86_elf_abi)
4fa24527 17154 {
351f65ca
L
17155 default:
17156 format = ELF_TARGET_FORMAT;
e379e5f3
L
17157#ifndef TE_SOLARIS
17158 tls_get_addr = "___tls_get_addr";
17159#endif
351f65ca 17160 break;
7f56bc95 17161 case X86_64_ABI:
351f65ca 17162 use_rela_relocations = 1;
4fa24527 17163 object_64bit = 1;
e379e5f3
L
17164#ifndef TE_SOLARIS
17165 tls_get_addr = "__tls_get_addr";
17166#endif
351f65ca
L
17167 format = ELF_TARGET_FORMAT64;
17168 break;
7f56bc95 17169 case X86_64_X32_ABI:
4fa24527 17170 use_rela_relocations = 1;
351f65ca 17171 object_64bit = 1;
e379e5f3
L
17172#ifndef TE_SOLARIS
17173 tls_get_addr = "__tls_get_addr";
17174#endif
862be3fb 17175 disallow_64bit_reloc = 1;
351f65ca
L
17176 format = ELF_TARGET_FORMAT32;
17177 break;
4fa24527 17178 }
c085ab00 17179 if (cpu_arch_isa == PROCESSOR_IAMCU)
81486035
L
17180 {
17181 if (x86_elf_abi != I386_ABI)
17182 as_fatal (_("Intel MCU is 32bit only"));
17183 return ELF_TARGET_IAMCU_FORMAT;
17184 }
8a9036a4 17185 else
351f65ca 17186 return format;
3e73aa7c 17187 }
e57f8c65
TG
17188#endif
17189#if defined (OBJ_MACH_O)
17190 case bfd_target_mach_o_flavour:
d382c579
TG
17191 if (flag_code == CODE_64BIT)
17192 {
17193 use_rela_relocations = 1;
17194 object_64bit = 1;
17195 return "mach-o-x86-64";
17196 }
17197 else
17198 return "mach-o-i386";
4c63da97 17199#endif
252b5132
RH
17200 default:
17201 abort ();
17202 return NULL;
17203 }
17204}
17205
47926f60 17206#endif /* OBJ_MAYBE_ more than one */
252b5132 17207\f
252b5132 17208symbolS *
7016a5d5 17209md_undefined_symbol (char *name)
252b5132 17210{
18dc2407
ILT
17211 if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
17212 && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
17213 && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
17214 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
24eab124
AM
17215 {
17216 if (!GOT_symbol)
17217 {
17218 if (symbol_find (name))
17219 as_bad (_("GOT already in symbol table"));
17220 GOT_symbol = symbol_new (name, undefined_section,
e01e1cee 17221 &zero_address_frag, 0);
24eab124
AM
17222 };
17223 return GOT_symbol;
17224 }
252b5132
RH
17225 return 0;
17226}
17227
071c5d81 17228#if defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT)
252b5132 17229/* Round up a section size to the appropriate boundary. */
47926f60 17230
252b5132 17231valueT
071c5d81 17232md_section_align (segT segment, valueT size)
252b5132 17233{
4c63da97
AM
17234 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
17235 {
17236 /* For a.out, force the section size to be aligned. If we don't do
17237 this, BFD will align it for us, but it will not write out the
17238 final bytes of the section. This may be a bug in BFD, but it is
17239 easier to fix it here since that is how the other a.out targets
17240 work. */
17241 int align;
17242
fd361982 17243 align = bfd_section_alignment (segment);
8d3842cd 17244 size = ((size + (1 << align) - 1) & (-((valueT) 1 << align)));
4c63da97 17245 }
252b5132
RH
17246
17247 return size;
17248}
071c5d81 17249#endif
252b5132
RH
17250
17251/* On the i386, PC-relative offsets are relative to the start of the
17252 next instruction. That is, the address of the offset, plus its
17253 size, since the offset is always the last part of the insn. */
17254
17255long
e3bb37b5 17256md_pcrel_from (fixS *fixP)
252b5132
RH
17257{
17258 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
17259}
17260
d4064aad 17261#ifdef OBJ_AOUT
252b5132
RH
17262
17263static void
e3bb37b5 17264s_bss (int ignore ATTRIBUTE_UNUSED)
252b5132 17265{
29b0f896 17266 int temp;
252b5132
RH
17267
17268 temp = get_absolute_expression ();
17269 subseg_set (bss_section, (subsegT) temp);
17270 demand_empty_rest_of_line ();
17271}
17272
17273#endif
17274
e379e5f3
L
17275/* Remember constant directive. */
17276
17277void
17278i386_cons_align (int ignore ATTRIBUTE_UNUSED)
17279{
b5482fe5
JB
17280 struct last_insn *last_insn
17281 = &seg_info(now_seg)->tc_segment_info_data.last_insn;
17282
ce69d081 17283 if (bfd_section_flags (now_seg) & SEC_CODE)
e379e5f3 17284 {
b5482fe5
JB
17285 last_insn->kind = last_insn_directive;
17286 last_insn->name = "constant directive";
17287 last_insn->file = as_where (&last_insn->line);
e379e5f3
L
17288 }
17289}
17290
3abbafc2 17291int
e3bb37b5 17292i386_validate_fix (fixS *fixp)
252b5132 17293{
e52a16f2
JB
17294 if (fixp->fx_addsy && S_GET_SEGMENT(fixp->fx_addsy) == reg_section)
17295 {
17296 reloc_howto_type *howto;
17297
17298 howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
17299 as_bad_where (fixp->fx_file, fixp->fx_line,
17300 _("invalid %s relocation against register"),
17301 howto ? howto->name : "<unknown>");
17302 return 0;
17303 }
17304
3abbafc2
JB
17305#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
17306 if (fixp->fx_r_type == BFD_RELOC_SIZE32
17307 || fixp->fx_r_type == BFD_RELOC_SIZE64)
17308 return IS_ELF && fixp->fx_addsy
17309 && (!S_IS_DEFINED (fixp->fx_addsy)
17310 || S_IS_EXTERNAL (fixp->fx_addsy));
a533c8df 17311
5bc71c2a
L
17312 /* BFD_RELOC_X86_64_GOTTPOFF:
17313 1. fx_tcbit -> BFD_RELOC_X86_64_CODE_4_GOTTPOFF
17314 2. fx_tcbit2 -> BFD_RELOC_X86_64_CODE_6_GOTTPOFF
17315 BFD_RELOC_X86_64_GOTPC32_TLSDESC:
17316 1. fx_tcbit -> BFD_RELOC_X86_64_CODE_4_GOTPC32_TLSDESC
17317 BFD_RELOC_32_PCREL:
17318 1. fx_tcbit -> BFD_RELOC_X86_64_GOTPCRELX
17319 2. fx_tcbit2 -> BFD_RELOC_X86_64_REX_GOTPCRELX
17320 3. fx_tcbit3 -> BFD_RELOC_X86_64_CODE_4_GOTPCRELX
17321 4. else -> BFD_RELOC_X86_64_GOTPCREL
17322 */
17323 if (fixp->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF)
a533c8df 17324 {
5bc71c2a 17325 if (fixp->fx_tcbit)
a533c8df 17326 fixp->fx_r_type = BFD_RELOC_X86_64_CODE_4_GOTTPOFF;
5bc71c2a
L
17327 else if (fixp->fx_tcbit2)
17328 fixp->fx_r_type = BFD_RELOC_X86_64_CODE_6_GOTTPOFF;
a533c8df 17329 }
5bc71c2a
L
17330 else if (fixp->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
17331 && fixp->fx_tcbit)
17332 fixp->fx_r_type = BFD_RELOC_X86_64_CODE_4_GOTPC32_TLSDESC;
3abbafc2
JB
17333#endif
17334
02a86693 17335 if (fixp->fx_subsy)
252b5132 17336 {
02a86693 17337 if (fixp->fx_subsy == GOT_symbol)
23df1078 17338 {
02a86693
L
17339 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
17340 {
17341 if (!object_64bit)
17342 abort ();
17343#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
5bc71c2a
L
17344 if (fixp->fx_tcbit)
17345 fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCRELX;
17346 else if (fixp->fx_tcbit2)
17347 fixp->fx_r_type = BFD_RELOC_X86_64_REX_GOTPCRELX;
17348 else if (fixp->fx_tcbit3)
17349 fixp->fx_r_type = BFD_RELOC_X86_64_CODE_4_GOTPCRELX;
02a86693
L
17350 else
17351#endif
17352 fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
17353 }
d6ab8113 17354 else
02a86693
L
17355 {
17356 if (!object_64bit)
17357 fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
17358 else
17359 fixp->fx_r_type = BFD_RELOC_X86_64_GOTOFF64;
17360 }
17361 fixp->fx_subsy = 0;
23df1078 17362 }
252b5132 17363 }
02a86693 17364#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2585b7a5 17365 else
02a86693 17366 {
2585b7a5
L
17367 /* NB: Commit 292676c1 resolved PLT32 reloc aganst local symbol
17368 to section. Since PLT32 relocation must be against symbols,
17369 turn such PLT32 relocation into PC32 relocation. */
17370 if (fixp->fx_addsy
17371 && (fixp->fx_r_type == BFD_RELOC_386_PLT32
17372 || fixp->fx_r_type == BFD_RELOC_X86_64_PLT32)
17373 && symbol_section_p (fixp->fx_addsy))
17374 fixp->fx_r_type = BFD_RELOC_32_PCREL;
17375 if (!object_64bit)
17376 {
17377 if (fixp->fx_r_type == BFD_RELOC_386_GOT32
17378 && fixp->fx_tcbit2)
17379 fixp->fx_r_type = BFD_RELOC_386_GOT32X;
17380 }
02a86693
L
17381 }
17382#endif
3abbafc2
JB
17383
17384 return 1;
252b5132
RH
17385}
17386
252b5132 17387arelent *
7016a5d5 17388tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
17389{
17390 arelent *rel;
17391 bfd_reloc_code_real_type code;
17392
17393 switch (fixp->fx_r_type)
17394 {
8ce3d284 17395#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3abbafc2
JB
17396 symbolS *sym;
17397
8fd4256d
L
17398 case BFD_RELOC_SIZE32:
17399 case BFD_RELOC_SIZE64:
3abbafc2
JB
17400 if (fixp->fx_addsy
17401 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))
17402 && (!fixp->fx_subsy
17403 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))))
17404 sym = fixp->fx_addsy;
17405 else if (fixp->fx_subsy
17406 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))
17407 && (!fixp->fx_addsy
17408 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))))
17409 sym = fixp->fx_subsy;
17410 else
17411 sym = NULL;
17412 if (IS_ELF && sym && S_IS_DEFINED (sym) && !S_IS_EXTERNAL (sym))
8fd4256d
L
17413 {
17414 /* Resolve size relocation against local symbol to size of
17415 the symbol plus addend. */
3abbafc2 17416 valueT value = S_GET_SIZE (sym);
44f87162 17417
3abbafc2
JB
17418 if (symbol_get_bfdsym (sym)->flags & BSF_SECTION_SYM)
17419 value = bfd_section_size (S_GET_SEGMENT (sym));
17420 if (sym == fixp->fx_subsy)
17421 {
17422 value = -value;
17423 if (fixp->fx_addsy)
17424 value += S_GET_VALUE (fixp->fx_addsy);
17425 }
17426 else if (fixp->fx_subsy)
17427 value -= S_GET_VALUE (fixp->fx_subsy);
44f87162 17428 value += fixp->fx_offset;
8fd4256d 17429 if (fixp->fx_r_type == BFD_RELOC_SIZE32
d965814f 17430 && object_64bit
8fd4256d
L
17431 && !fits_in_unsigned_long (value))
17432 as_bad_where (fixp->fx_file, fixp->fx_line,
17433 _("symbol size computation overflow"));
17434 fixp->fx_addsy = NULL;
17435 fixp->fx_subsy = NULL;
17436 md_apply_fix (fixp, (valueT *) &value, NULL);
17437 return NULL;
17438 }
3abbafc2
JB
17439 if (!fixp->fx_addsy || fixp->fx_subsy)
17440 {
17441 as_bad_where (fixp->fx_file, fixp->fx_line,
17442 "unsupported expression involving @size");
17443 return NULL;
17444 }
8ce3d284 17445#endif
1a0670f3 17446 /* Fall through. */
8fd4256d 17447
3e73aa7c
JH
17448 case BFD_RELOC_X86_64_PLT32:
17449 case BFD_RELOC_X86_64_GOT32:
17450 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
17451 case BFD_RELOC_X86_64_GOTPCRELX:
17452 case BFD_RELOC_X86_64_REX_GOTPCRELX:
3d5a60de 17453 case BFD_RELOC_X86_64_CODE_4_GOTPCRELX:
252b5132
RH
17454 case BFD_RELOC_386_PLT32:
17455 case BFD_RELOC_386_GOT32:
02a86693 17456 case BFD_RELOC_386_GOT32X:
252b5132
RH
17457 case BFD_RELOC_386_GOTOFF:
17458 case BFD_RELOC_386_GOTPC:
13ae64f3
JJ
17459 case BFD_RELOC_386_TLS_GD:
17460 case BFD_RELOC_386_TLS_LDM:
17461 case BFD_RELOC_386_TLS_LDO_32:
17462 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
17463 case BFD_RELOC_386_TLS_IE:
17464 case BFD_RELOC_386_TLS_GOTIE:
13ae64f3
JJ
17465 case BFD_RELOC_386_TLS_LE_32:
17466 case BFD_RELOC_386_TLS_LE:
67a4f2b7
AO
17467 case BFD_RELOC_386_TLS_GOTDESC:
17468 case BFD_RELOC_386_TLS_DESC_CALL:
bffbf940
JJ
17469 case BFD_RELOC_X86_64_TLSGD:
17470 case BFD_RELOC_X86_64_TLSLD:
17471 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 17472 case BFD_RELOC_X86_64_DTPOFF64:
bffbf940 17473 case BFD_RELOC_X86_64_GOTTPOFF:
a533c8df 17474 case BFD_RELOC_X86_64_CODE_4_GOTTPOFF:
5bc71c2a 17475 case BFD_RELOC_X86_64_CODE_6_GOTTPOFF:
bffbf940 17476 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113
JB
17477 case BFD_RELOC_X86_64_TPOFF64:
17478 case BFD_RELOC_X86_64_GOTOFF64:
17479 case BFD_RELOC_X86_64_GOTPC32:
7b81dfbb
AJ
17480 case BFD_RELOC_X86_64_GOT64:
17481 case BFD_RELOC_X86_64_GOTPCREL64:
17482 case BFD_RELOC_X86_64_GOTPC64:
17483 case BFD_RELOC_X86_64_GOTPLT64:
17484 case BFD_RELOC_X86_64_PLTOFF64:
67a4f2b7 17485 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
a533c8df 17486 case BFD_RELOC_X86_64_CODE_4_GOTPC32_TLSDESC:
67a4f2b7 17487 case BFD_RELOC_X86_64_TLSDESC_CALL:
252b5132
RH
17488 case BFD_RELOC_RVA:
17489 case BFD_RELOC_VTABLE_ENTRY:
17490 case BFD_RELOC_VTABLE_INHERIT:
6482c264
NC
17491#ifdef TE_PE
17492 case BFD_RELOC_32_SECREL:
145667f8 17493 case BFD_RELOC_16_SECIDX:
6482c264 17494#endif
252b5132
RH
17495 code = fixp->fx_r_type;
17496 break;
dbbaec26
L
17497 case BFD_RELOC_X86_64_32S:
17498 if (!fixp->fx_pcrel)
17499 {
17500 /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32. */
17501 code = fixp->fx_r_type;
17502 break;
17503 }
1a0670f3 17504 /* Fall through. */
252b5132 17505 default:
93382f6d 17506 if (fixp->fx_pcrel)
252b5132 17507 {
93382f6d
AM
17508 switch (fixp->fx_size)
17509 {
17510 default:
b091f402
AM
17511 as_bad_where (fixp->fx_file, fixp->fx_line,
17512 _("can not do %d byte pc-relative relocation"),
17513 fixp->fx_size);
93382f6d
AM
17514 code = BFD_RELOC_32_PCREL;
17515 break;
17516 case 1: code = BFD_RELOC_8_PCREL; break;
17517 case 2: code = BFD_RELOC_16_PCREL; break;
d258b828 17518 case 4: code = BFD_RELOC_32_PCREL; break;
d6ab8113
JB
17519#ifdef BFD64
17520 case 8: code = BFD_RELOC_64_PCREL; break;
17521#endif
93382f6d
AM
17522 }
17523 }
17524 else
17525 {
17526 switch (fixp->fx_size)
17527 {
17528 default:
b091f402
AM
17529 as_bad_where (fixp->fx_file, fixp->fx_line,
17530 _("can not do %d byte relocation"),
17531 fixp->fx_size);
93382f6d
AM
17532 code = BFD_RELOC_32;
17533 break;
17534 case 1: code = BFD_RELOC_8; break;
17535 case 2: code = BFD_RELOC_16; break;
17536 case 4: code = BFD_RELOC_32; break;
937149dd 17537#ifdef BFD64
3e73aa7c 17538 case 8: code = BFD_RELOC_64; break;
937149dd 17539#endif
93382f6d 17540 }
252b5132
RH
17541 }
17542 break;
17543 }
252b5132 17544
d182319b
JB
17545 if ((code == BFD_RELOC_32
17546 || code == BFD_RELOC_32_PCREL
17547 || code == BFD_RELOC_X86_64_32S)
252b5132
RH
17548 && GOT_symbol
17549 && fixp->fx_addsy == GOT_symbol)
3e73aa7c 17550 {
4fa24527 17551 if (!object_64bit)
d6ab8113
JB
17552 code = BFD_RELOC_386_GOTPC;
17553 else
17554 code = BFD_RELOC_X86_64_GOTPC32;
3e73aa7c 17555 }
7b81dfbb
AJ
17556 if ((code == BFD_RELOC_64 || code == BFD_RELOC_64_PCREL)
17557 && GOT_symbol
17558 && fixp->fx_addsy == GOT_symbol)
17559 {
17560 code = BFD_RELOC_X86_64_GOTPC64;
17561 }
252b5132 17562
add39d23
TS
17563 rel = XNEW (arelent);
17564 rel->sym_ptr_ptr = XNEW (asymbol *);
49309057 17565 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
17566
17567 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
c87db184 17568
3e73aa7c
JH
17569 if (!use_rela_relocations)
17570 {
17571 /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
17572 vtable entry to be used in the relocation's section offset. */
17573 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
17574 rel->address = fixp->fx_offset;
fbeb56a4
DK
17575#if defined (OBJ_COFF) && defined (TE_PE)
17576 else if (fixp->fx_addsy && S_IS_WEAK (fixp->fx_addsy))
17577 rel->addend = fixp->fx_addnumber - (S_GET_VALUE (fixp->fx_addsy) * 2);
17578 else
17579#endif
c6682705 17580 rel->addend = 0;
3e73aa7c
JH
17581 }
17582 /* Use the rela in 64bit mode. */
252b5132 17583 else
3e73aa7c 17584 {
862be3fb
L
17585 if (disallow_64bit_reloc)
17586 switch (code)
17587 {
862be3fb
L
17588 case BFD_RELOC_X86_64_DTPOFF64:
17589 case BFD_RELOC_X86_64_TPOFF64:
17590 case BFD_RELOC_64_PCREL:
17591 case BFD_RELOC_X86_64_GOTOFF64:
17592 case BFD_RELOC_X86_64_GOT64:
17593 case BFD_RELOC_X86_64_GOTPCREL64:
17594 case BFD_RELOC_X86_64_GOTPC64:
17595 case BFD_RELOC_X86_64_GOTPLT64:
17596 case BFD_RELOC_X86_64_PLTOFF64:
17597 as_bad_where (fixp->fx_file, fixp->fx_line,
17598 _("cannot represent relocation type %s in x32 mode"),
17599 bfd_get_reloc_code_name (code));
17600 break;
17601 default:
17602 break;
17603 }
17604
062cd5e7
AS
17605 if (!fixp->fx_pcrel)
17606 rel->addend = fixp->fx_offset;
17607 else
17608 switch (code)
17609 {
17610 case BFD_RELOC_X86_64_PLT32:
17611 case BFD_RELOC_X86_64_GOT32:
17612 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
17613 case BFD_RELOC_X86_64_GOTPCRELX:
17614 case BFD_RELOC_X86_64_REX_GOTPCRELX:
3d5a60de 17615 case BFD_RELOC_X86_64_CODE_4_GOTPCRELX:
bffbf940
JJ
17616 case BFD_RELOC_X86_64_TLSGD:
17617 case BFD_RELOC_X86_64_TLSLD:
17618 case BFD_RELOC_X86_64_GOTTPOFF:
a533c8df 17619 case BFD_RELOC_X86_64_CODE_4_GOTTPOFF:
5bc71c2a 17620 case BFD_RELOC_X86_64_CODE_6_GOTTPOFF:
67a4f2b7 17621 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
a533c8df 17622 case BFD_RELOC_X86_64_CODE_4_GOTPC32_TLSDESC:
67a4f2b7 17623 case BFD_RELOC_X86_64_TLSDESC_CALL:
062cd5e7
AS
17624 rel->addend = fixp->fx_offset - fixp->fx_size;
17625 break;
17626 default:
17627 rel->addend = (section->vma
17628 - fixp->fx_size
17629 + fixp->fx_addnumber
17630 + md_pcrel_from (fixp));
17631 break;
17632 }
3e73aa7c
JH
17633 }
17634
252b5132
RH
17635 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
17636 if (rel->howto == NULL)
17637 {
17638 as_bad_where (fixp->fx_file, fixp->fx_line,
d0b47220 17639 _("cannot represent relocation type %s"),
252b5132
RH
17640 bfd_get_reloc_code_name (code));
17641 /* Set howto to a garbage value so that we can keep going. */
17642 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
9c2799c2 17643 gas_assert (rel->howto != NULL);
252b5132
RH
17644 }
17645
17646 return rel;
17647}
17648
ee86248c 17649#include "tc-i386-intel.c"
54cfded0 17650
a60de03c
JB
17651void
17652tc_x86_parse_to_dw2regnum (expressionS *exp)
54cfded0 17653{
a60de03c
JB
17654 int saved_naked_reg;
17655 char saved_register_dot;
54cfded0 17656
a60de03c
JB
17657 saved_naked_reg = allow_naked_reg;
17658 allow_naked_reg = 1;
17659 saved_register_dot = register_chars['.'];
17660 register_chars['.'] = '.';
17661 allow_pseudo_reg = 1;
17662 expression_and_evaluate (exp);
17663 allow_pseudo_reg = 0;
17664 register_chars['.'] = saved_register_dot;
17665 allow_naked_reg = saved_naked_reg;
17666
e96d56a1 17667 if (exp->X_op == O_register && exp->X_add_number >= 0)
54cfded0 17668 {
066673f6 17669 exp->X_op = O_illegal;
a60de03c
JB
17670 if ((addressT) exp->X_add_number < i386_regtab_size)
17671 {
a60de03c 17672 exp->X_add_number = i386_regtab[exp->X_add_number]
fabb73d1 17673 .dw2_regnum[object_64bit];
066673f6
JB
17674 if (exp->X_add_number != Dw2Inval)
17675 exp->X_op = O_constant;
a60de03c 17676 }
54cfded0 17677 }
54cfded0
AM
17678}
17679
17680void
17681tc_x86_frame_initial_instructions (void)
17682{
fabb73d1 17683 cfi_add_CFA_def_cfa (object_64bit ? REG_SP : 4, -x86_cie_data_alignment);
61ff971f 17684 cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
54cfded0 17685}
d2b2c203 17686
d7921315
L
17687int
17688x86_dwarf2_addr_size (void)
17689{
17690#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
17691 if (x86_elf_abi == X86_64_X32_ABI)
17692 return 4;
17693#endif
17694 return bfd_arch_bits_per_address (stdoutput) / 8;
17695}
17696
071c5d81
JB
17697#ifdef TE_PE
17698void
17699tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
17700{
17701 expressionS exp;
17702
17703 exp.X_op = O_secrel;
17704 exp.X_add_symbol = symbol;
17705 exp.X_add_number = 0;
17706 emit_expr (&exp, size);
17707}
17708#endif
17709
17710#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
d2b2c203
DJ
17711int
17712i386_elf_section_type (const char *str, size_t len)
17713{
17714 if (flag_code == CODE_64BIT
17715 && len == sizeof ("unwind") - 1
d34049e8 17716 && startswith (str, "unwind"))
d2b2c203
DJ
17717 return SHT_X86_64_UNWIND;
17718
17719 return -1;
17720}
bb41ade5 17721
da374e94
JB
17722void
17723i386_elf_section_change_hook (void)
17724{
17725 struct i386_segment_info *info = &seg_info(now_seg)->tc_segment_info_data;
17726 struct i386_segment_info *curr, *prev;
17727
17728 if (info->subseg == now_subseg)
17729 return;
17730
17731 /* Find the (or make a) list entry to save state into. */
17732 for (prev = info; (curr = prev->next) != NULL; prev = curr)
17733 if (curr->subseg == info->subseg)
17734 break;
17735 if (!curr)
17736 {
fddee222 17737 curr = notes_alloc (sizeof (*curr));
da374e94
JB
17738 curr->subseg = info->subseg;
17739 curr->next = NULL;
17740 prev->next = curr;
17741 }
17742 curr->last_insn = info->last_insn;
17743
17744 /* Find the list entry to load state from. */
17745 for (curr = info->next; curr; curr = curr->next)
17746 if (curr->subseg == now_subseg)
17747 break;
17748 if (curr)
17749 info->last_insn = curr->last_insn;
17750 else
17751 memset (&info->last_insn, 0, sizeof (info->last_insn));
17752 info->subseg = now_subseg;
17753}
17754
ad5fec3b
EB
17755#ifdef TE_SOLARIS
17756void
17757i386_solaris_fix_up_eh_frame (segT sec)
17758{
17759 if (flag_code == CODE_64BIT)
17760 elf_section_type (sec) = SHT_X86_64_UNWIND;
17761}
17762#endif
17763
3b22753a
L
17764/* For ELF on x86-64, add support for SHF_X86_64_LARGE. */
17765
01e1a5bc 17766bfd_vma
6d4af3c2 17767x86_64_section_letter (int letter, const char **ptr_msg)
3b22753a
L
17768{
17769 if (flag_code == CODE_64BIT)
17770 {
17771 if (letter == 'l')
17772 return SHF_X86_64_LARGE;
17773
8f3bae45 17774 *ptr_msg = _("bad .section directive: want a,l,w,x,M,S,G,T in string");
64e74474 17775 }
3b22753a 17776 else
8f3bae45 17777 *ptr_msg = _("bad .section directive: want a,w,x,M,S,G,T in string");
3b22753a
L
17778 return -1;
17779}
17780
3b22753a
L
17781static void
17782handle_large_common (int small ATTRIBUTE_UNUSED)
17783{
17784 if (flag_code != CODE_64BIT)
17785 {
17786 s_comm_internal (0, elf_common_parse);
17787 as_warn (_(".largecomm supported only in 64bit mode, producing .comm"));
17788 }
17789 else
17790 {
17791 static segT lbss_section;
17792 asection *saved_com_section_ptr = elf_com_section_ptr;
17793 asection *saved_bss_section = bss_section;
17794
17795 if (lbss_section == NULL)
17796 {
17797 flagword applicable;
17798 segT seg = now_seg;
17799 subsegT subseg = now_subseg;
17800
17801 /* The .lbss section is for local .largecomm symbols. */
17802 lbss_section = subseg_new (".lbss", 0);
17803 applicable = bfd_applicable_section_flags (stdoutput);
fd361982 17804 bfd_set_section_flags (lbss_section, applicable & SEC_ALLOC);
3b22753a
L
17805 seg_info (lbss_section)->bss = 1;
17806
17807 subseg_set (seg, subseg);
17808 }
17809
17810 elf_com_section_ptr = &_bfd_elf_large_com_section;
17811 bss_section = lbss_section;
17812
17813 s_comm_internal (0, elf_common_parse);
17814
17815 elf_com_section_ptr = saved_com_section_ptr;
17816 bss_section = saved_bss_section;
17817 }
17818}
17819#endif /* OBJ_ELF || OBJ_MAYBE_ELF */