]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/config/tc-i386.c
x86: introduce .insn directive
[thirdparty/binutils-gdb.git] / gas / config / tc-i386.c
CommitLineData
b534c6d3 1/* tc-i386.c -- Assemble code for the Intel 80386
d87bef3a 2 Copyright (C) 1989-2023 Free Software Foundation, Inc.
252b5132
RH
3
4 This file is part of GAS, the GNU Assembler.
5
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
ec2655a6 8 the Free Software Foundation; either version 3, or (at your option)
252b5132
RH
9 any later version.
10
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to the Free
4b4da160
NC
18 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
19 02110-1301, USA. */
252b5132 20
47926f60
KH
21/* Intel 80386 machine specific gas.
22 Written by Eliot Dresselhaus (eliot@mgm.mit.edu).
3e73aa7c 23 x86_64 support by Jan Hubicka (jh@suse.cz)
0f10071e 24 VIA PadLock support by Michal Ludvig (mludvig@suse.cz)
47926f60
KH
25 Bugs & suggestions are completely welcome. This is free software.
26 Please help us make it better. */
252b5132 27
252b5132 28#include "as.h"
3882b010 29#include "safe-ctype.h"
252b5132 30#include "subsegs.h"
316e2c05 31#include "dwarf2dbg.h"
54cfded0 32#include "dw2gencfi.h"
b52c4ee4
IB
33#include "gen-sframe.h"
34#include "sframe.h"
d2b2c203 35#include "elf/x86-64.h"
40fb9820 36#include "opcodes/i386-init.h"
5c139202 37#include "opcodes/i386-mnem.h"
41fd2579 38#include <limits.h>
41fd2579 39
c3332e24 40#ifndef INFER_ADDR_PREFIX
eecb386c 41#define INFER_ADDR_PREFIX 1
c3332e24
AM
42#endif
43
29b0f896
AM
44#ifndef DEFAULT_ARCH
45#define DEFAULT_ARCH "i386"
246fcdee 46#endif
252b5132 47
edde18a5
AM
48#ifndef INLINE
49#if __GNUC__ >= 2
50#define INLINE __inline__
51#else
52#define INLINE
53#endif
54#endif
55
6305a203
L
56/* Prefixes will be emitted in the order defined below.
57 WAIT_PREFIX must be the first prefix since FWAIT is really is an
58 instruction, and so must come before any prefixes.
59 The preferred prefix order is SEG_PREFIX, ADDR_PREFIX, DATA_PREFIX,
42164a71 60 REP_PREFIX/HLE_PREFIX, LOCK_PREFIX. */
6305a203
L
61#define WAIT_PREFIX 0
62#define SEG_PREFIX 1
63#define ADDR_PREFIX 2
64#define DATA_PREFIX 3
c32fa91d 65#define REP_PREFIX 4
42164a71 66#define HLE_PREFIX REP_PREFIX
7e8b059b 67#define BND_PREFIX REP_PREFIX
c32fa91d 68#define LOCK_PREFIX 5
4e9ac44a
L
69#define REX_PREFIX 6 /* must come last. */
70#define MAX_PREFIXES 7 /* max prefixes per opcode */
6305a203
L
71
72/* we define the syntax here (modulo base,index,scale syntax) */
73#define REGISTER_PREFIX '%'
74#define IMMEDIATE_PREFIX '$'
75#define ABSOLUTE_PREFIX '*'
76
77/* these are the instruction mnemonic suffixes in AT&T syntax or
78 memory operand size in Intel syntax. */
79#define WORD_MNEM_SUFFIX 'w'
80#define BYTE_MNEM_SUFFIX 'b'
81#define SHORT_MNEM_SUFFIX 's'
82#define LONG_MNEM_SUFFIX 'l'
83#define QWORD_MNEM_SUFFIX 'q'
6305a203
L
84
85#define END_OF_INSN '\0'
86
05909f23
JB
87#define OPERAND_TYPE_NONE { .bitfield = { .class = ClassNone } }
88
79dec6b7
JB
89/* This matches the C -> StaticRounding alias in the opcode table. */
90#define commutative staticrounding
91
6305a203
L
92/*
93 'templates' is for grouping together 'template' structures for opcodes
94 of the same name. This is only used for storing the insns in the grand
95 ole hash table of insns.
96 The templates themselves start at START and range up to (but not including)
97 END.
98 */
99typedef struct
100{
d3ce72d0
NC
101 const insn_template *start;
102 const insn_template *end;
6305a203
L
103}
104templates;
105
106/* 386 operand encoding bytes: see 386 book for details of this. */
107typedef struct
108{
109 unsigned int regmem; /* codes register or memory operand */
110 unsigned int reg; /* codes register operand (or extended opcode) */
111 unsigned int mode; /* how to interpret regmem & reg */
112}
113modrm_byte;
114
115/* x86-64 extension prefix. */
116typedef int rex_byte;
117
6305a203
L
118/* 386 opcode byte to code indirect addressing. */
119typedef struct
120{
121 unsigned base;
122 unsigned index;
123 unsigned scale;
124}
125sib_byte;
126
6305a203
L
127/* x86 arch names, types and features */
128typedef struct
129{
130 const char *name; /* arch name */
6ceeed25
JB
131 unsigned int len:8; /* arch string length */
132 bool skip:1; /* show_arch should skip this. */
6305a203 133 enum processor_type type; /* arch type */
ae89daec
JB
134 i386_cpu_flags enable; /* cpu feature enable flags */
135 i386_cpu_flags disable; /* cpu feature disable flags */
6305a203
L
136}
137arch_entry;
138
78f12dd3 139static void update_code_flag (int, int);
edd67638 140static void s_insn (int);
e3bb37b5
L
141static void set_code_flag (int);
142static void set_16bit_gcc_code_flag (int);
143static void set_intel_syntax (int);
1efbbeb4 144static void set_intel_mnemonic (int);
db51cc60 145static void set_allow_index_reg (int);
7bab8ab5 146static void set_check (int);
e3bb37b5 147static void set_cpu_arch (int);
6482c264 148#ifdef TE_PE
e3bb37b5 149static void pe_directive_secrel (int);
145667f8 150static void pe_directive_secidx (int);
6482c264 151#endif
e3bb37b5
L
152static void signed_cons (int);
153static char *output_invalid (int c);
ee86248c
JB
154static int i386_finalize_immediate (segT, expressionS *, i386_operand_type,
155 const char *);
156static int i386_finalize_displacement (segT, expressionS *, i386_operand_type,
157 const char *);
a7619375 158static int i386_att_operand (char *);
e3bb37b5 159static int i386_intel_operand (char *, int);
ee86248c
JB
160static int i386_intel_simplify (expressionS *);
161static int i386_intel_parse_name (const char *, expressionS *);
e3bb37b5 162static const reg_entry *parse_register (char *, char **);
edd67638 163static const char *parse_insn (const char *, char *, bool);
e3bb37b5
L
164static char *parse_operands (char *, const char *);
165static void swap_operands (void);
783c187b 166static void swap_2_operands (unsigned int, unsigned int);
48bcea9f 167static enum flag_code i386_addressing_mode (void);
e3bb37b5 168static void optimize_imm (void);
0de704b9 169static bool optimize_disp (const insn_template *t);
83b16ac6 170static const insn_template *match_template (char);
e3bb37b5
L
171static int check_string (void);
172static int process_suffix (void);
173static int check_byte_reg (void);
174static int check_long_reg (void);
175static int check_qword_reg (void);
176static int check_word_reg (void);
177static int finalize_imm (void);
178static int process_operands (void);
5e042380 179static const reg_entry *build_modrm_byte (void);
e3bb37b5
L
180static void output_insn (void);
181static void output_imm (fragS *, offsetT);
182static void output_disp (fragS *, offsetT);
29b0f896 183#ifndef I386COFF
e3bb37b5 184static void s_bss (int);
252b5132 185#endif
17d4e2a2
L
186#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
187static void handle_large_common (int small ATTRIBUTE_UNUSED);
b4a3a7b4
L
188
189/* GNU_PROPERTY_X86_ISA_1_USED. */
190static unsigned int x86_isa_1_used;
191/* GNU_PROPERTY_X86_FEATURE_2_USED. */
192static unsigned int x86_feature_2_used;
193/* Generate x86 used ISA and feature properties. */
194static unsigned int x86_used_note = DEFAULT_X86_USED_NOTE;
17d4e2a2 195#endif
252b5132 196
a847613f 197static const char *default_arch = DEFAULT_ARCH;
3e73aa7c 198
8a6fb3f9
JB
199/* parse_register() returns this when a register alias cannot be used. */
200static const reg_entry bad_reg = { "<bad>", OPERAND_TYPE_NONE, 0, 0,
201 { Dw2Inval, Dw2Inval } };
202
34684862 203static const reg_entry *reg_eax;
5e042380
JB
204static const reg_entry *reg_ds;
205static const reg_entry *reg_es;
206static const reg_entry *reg_ss;
6288d05f 207static const reg_entry *reg_st0;
6225c532
JB
208static const reg_entry *reg_k0;
209
c0f3af97
L
210/* VEX prefix. */
211typedef struct
212{
43234a1e
L
213 /* VEX prefix is either 2 byte or 3 byte. EVEX is 4 byte. */
214 unsigned char bytes[4];
c0f3af97
L
215 unsigned int length;
216 /* Destination or source register specifier. */
217 const reg_entry *register_specifier;
218} vex_prefix;
219
252b5132 220/* 'md_assemble ()' gathers together information and puts it into a
47926f60 221 i386_insn. */
252b5132 222
520dc8e8
AM
223union i386_op
224 {
225 expressionS *disps;
226 expressionS *imms;
227 const reg_entry *regs;
228 };
229
a65babc9
L
230enum i386_error
231 {
b4d65f2d 232 no_error, /* Must be first. */
86e026a4 233 operand_size_mismatch,
a65babc9
L
234 operand_type_mismatch,
235 register_type_mismatch,
236 number_of_operands_mismatch,
237 invalid_instruction_suffix,
238 bad_imm4,
a65babc9
L
239 unsupported_with_intel_mnemonic,
240 unsupported_syntax,
6c30d220 241 unsupported,
9db83a32
JB
242 unsupported_on_arch,
243 unsupported_64bit,
260cd341 244 invalid_sib_address,
6c30d220 245 invalid_vsib_address,
7bab8ab5 246 invalid_vector_register_set,
260cd341 247 invalid_tmm_register_set,
0cc78721 248 invalid_dest_and_src_register_set,
43234a1e
L
249 unsupported_vector_index_register,
250 unsupported_broadcast,
43234a1e
L
251 broadcast_needed,
252 unsupported_masking,
253 mask_not_on_destination,
254 no_default_mask,
255 unsupported_rc_sae,
43234a1e 256 invalid_register_operand,
a65babc9
L
257 };
258
252b5132
RH
259struct _i386_insn
260 {
47926f60 261 /* TM holds the template for the insn were currently assembling. */
d3ce72d0 262 insn_template tm;
252b5132 263
7d5e4556
L
264 /* SUFFIX holds the instruction size suffix for byte, word, dword
265 or qword, if given. */
252b5132
RH
266 char suffix;
267
9a182d04
JB
268 /* OPCODE_LENGTH holds the number of base opcode bytes. */
269 unsigned char opcode_length;
270
47926f60 271 /* OPERANDS gives the number of given operands. */
252b5132
RH
272 unsigned int operands;
273
274 /* REG_OPERANDS, DISP_OPERANDS, MEM_OPERANDS, IMM_OPERANDS give the number
275 of given register, displacement, memory operands and immediate
47926f60 276 operands. */
252b5132
RH
277 unsigned int reg_operands, disp_operands, mem_operands, imm_operands;
278
279 /* TYPES [i] is the type (see above #defines) which tells us how to
520dc8e8 280 use OP[i] for the corresponding operand. */
40fb9820 281 i386_operand_type types[MAX_OPERANDS];
252b5132 282
520dc8e8
AM
283 /* Displacement expression, immediate expression, or register for each
284 operand. */
285 union i386_op op[MAX_OPERANDS];
252b5132 286
3e73aa7c
JH
287 /* Flags for operands. */
288 unsigned int flags[MAX_OPERANDS];
289#define Operand_PCrel 1
c48dadc9 290#define Operand_Mem 2
3e73aa7c 291
252b5132 292 /* Relocation type for operand */
f86103b7 293 enum bfd_reloc_code_real reloc[MAX_OPERANDS];
252b5132 294
252b5132
RH
295 /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
296 the base index byte below. */
297 const reg_entry *base_reg;
298 const reg_entry *index_reg;
299 unsigned int log2_scale_factor;
300
301 /* SEG gives the seg_entries of this insn. They are zero unless
47926f60 302 explicit segment overrides are given. */
5e042380 303 const reg_entry *seg[2];
252b5132
RH
304
305 /* PREFIX holds all the given prefix opcodes (usually null).
306 PREFIXES is the number of prefix opcodes. */
307 unsigned int prefixes;
308 unsigned char prefix[MAX_PREFIXES];
309
50128d0c 310 /* Register is in low 3 bits of opcode. */
5b7c81bd 311 bool short_form;
50128d0c 312
6f2f06be 313 /* The operand to a branch insn indicates an absolute branch. */
5b7c81bd 314 bool jumpabsolute;
6f2f06be 315
a4d3acd2
JB
316 /* The operand to a branch insn indicates a far branch. */
317 bool far_branch;
318
9373f275
L
319 /* There is a memory operand of (%dx) which should be only used
320 with input/output instructions. */
321 bool input_output_operand;
322
921eafea
L
323 /* Extended states. */
324 enum
325 {
326 /* Use MMX state. */
327 xstate_mmx = 1 << 0,
328 /* Use XMM state. */
329 xstate_xmm = 1 << 1,
330 /* Use YMM state. */
331 xstate_ymm = 1 << 2 | xstate_xmm,
332 /* Use ZMM state. */
333 xstate_zmm = 1 << 3 | xstate_ymm,
334 /* Use TMM state. */
32930e4e
L
335 xstate_tmm = 1 << 4,
336 /* Use MASK state. */
337 xstate_mask = 1 << 5
921eafea 338 } xstate;
260cd341 339
e379e5f3 340 /* Has GOTPC or TLS relocation. */
5b7c81bd 341 bool has_gotpc_tls_reloc;
e379e5f3 342
252b5132 343 /* RM and SIB are the modrm byte and the sib byte where the
c1e679ec 344 addressing modes of this insn are encoded. */
252b5132 345 modrm_byte rm;
3e73aa7c 346 rex_byte rex;
43234a1e 347 rex_byte vrex;
252b5132 348 sib_byte sib;
c0f3af97 349 vex_prefix vex;
b6169b20 350
6225c532
JB
351 /* Masking attributes.
352
353 The struct describes masking, applied to OPERAND in the instruction.
354 REG is a pointer to the corresponding mask register. ZEROING tells
355 whether merging or zeroing mask is used. */
356 struct Mask_Operation
357 {
358 const reg_entry *reg;
359 unsigned int zeroing;
360 /* The operand where this operation is associated. */
361 unsigned int operand;
362 } mask;
43234a1e
L
363
364 /* Rounding control and SAE attributes. */
ca5312a2
JB
365 struct RC_Operation
366 {
367 enum rc_type
368 {
369 rc_none = -1,
370 rne,
371 rd,
372 ru,
373 rz,
374 saeonly
375 } type;
7063667e
JB
376 /* In Intel syntax the operand modifier form is supposed to be used, but
377 we continue to accept the immediate forms as well. */
378 bool modifier;
ca5312a2 379 } rounding;
43234a1e 380
5273a3cd
JB
381 /* Broadcasting attributes.
382
383 The struct describes broadcasting, applied to OPERAND. TYPE is
384 expresses the broadcast factor. */
385 struct Broadcast_Operation
386 {
0cc78721 387 /* Type of broadcast: {1to2}, {1to4}, {1to8}, {1to16} or {1to32}. */
5273a3cd
JB
388 unsigned int type;
389
390 /* Index of broadcasted operand. */
391 unsigned int operand;
392
393 /* Number of bytes to broadcast. */
394 unsigned int bytes;
395 } broadcast;
43234a1e
L
396
397 /* Compressed disp8*N attribute. */
398 unsigned int memshift;
399
86fa6981
L
400 /* Prefer load or store in encoding. */
401 enum
402 {
403 dir_encoding_default = 0,
404 dir_encoding_load,
64c49ab3
JB
405 dir_encoding_store,
406 dir_encoding_swap
86fa6981 407 } dir_encoding;
891edac4 408
41eb8e88 409 /* Prefer 8bit, 16bit, 32bit displacement in encoding. */
a501d77e
L
410 enum
411 {
412 disp_encoding_default = 0,
413 disp_encoding_8bit,
41eb8e88 414 disp_encoding_16bit,
a501d77e
L
415 disp_encoding_32bit
416 } disp_encoding;
f8a5c266 417
6b6b6807 418 /* Prefer the REX byte in encoding. */
5b7c81bd 419 bool rex_encoding;
6b6b6807 420
b6f8c7c4 421 /* Disable instruction size optimization. */
5b7c81bd 422 bool no_optimize;
b6f8c7c4 423
86fa6981
L
424 /* How to encode vector instructions. */
425 enum
426 {
427 vex_encoding_default = 0,
42e04b36 428 vex_encoding_vex,
86fa6981 429 vex_encoding_vex3,
da4977e0
JB
430 vex_encoding_evex,
431 vex_encoding_error
86fa6981
L
432 } vec_encoding;
433
d5de92cf
L
434 /* REP prefix. */
435 const char *rep_prefix;
436
165de32a
L
437 /* HLE prefix. */
438 const char *hle_prefix;
42164a71 439
7e8b059b
L
440 /* Have BND prefix. */
441 const char *bnd_prefix;
442
04ef582a
L
443 /* Have NOTRACK prefix. */
444 const char *notrack_prefix;
445
891edac4 446 /* Error message. */
a65babc9 447 enum i386_error error;
252b5132
RH
448 };
449
450typedef struct _i386_insn i386_insn;
451
43234a1e
L
452/* Link RC type with corresponding string, that'll be looked for in
453 asm. */
454struct RC_name
455{
456 enum rc_type type;
457 const char *name;
458 unsigned int len;
459};
460
461static const struct RC_name RC_NamesTable[] =
462{
463 { rne, STRING_COMMA_LEN ("rn-sae") },
464 { rd, STRING_COMMA_LEN ("rd-sae") },
465 { ru, STRING_COMMA_LEN ("ru-sae") },
466 { rz, STRING_COMMA_LEN ("rz-sae") },
467 { saeonly, STRING_COMMA_LEN ("sae") },
468};
469
3bfea8ba
L
470/* To be indexed by segment register number. */
471static const unsigned char i386_seg_prefixes[] = {
472 ES_PREFIX_OPCODE,
473 CS_PREFIX_OPCODE,
474 SS_PREFIX_OPCODE,
475 DS_PREFIX_OPCODE,
476 FS_PREFIX_OPCODE,
477 GS_PREFIX_OPCODE
478};
479
252b5132
RH
480/* List of chars besides those in app.c:symbol_chars that can start an
481 operand. Used to prevent the scrubber eating vital white-space. */
86fa6981 482const char extra_symbol_chars[] = "*%-([{}"
252b5132 483#ifdef LEX_AT
32137342
NC
484 "@"
485#endif
486#ifdef LEX_QM
487 "?"
252b5132 488#endif
32137342 489 ;
252b5132 490
b3983e5f
JB
491#if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
492 && !defined (TE_GNU) \
493 && !defined (TE_LINUX) \
d85e70a3 494 && !defined (TE_Haiku) \
b3983e5f
JB
495 && !defined (TE_FreeBSD) \
496 && !defined (TE_DragonFly) \
497 && !defined (TE_NetBSD))
252b5132 498/* This array holds the chars that always start a comment. If the
b3b91714
AM
499 pre-processor is disabled, these aren't very useful. The option
500 --divide will remove '/' from this list. */
501const char *i386_comment_chars = "#/";
502#define SVR4_COMMENT_CHARS 1
252b5132 503#define PREFIX_SEPARATOR '\\'
252b5132 504
b3b91714
AM
505#else
506const char *i386_comment_chars = "#";
507#define PREFIX_SEPARATOR '/'
508#endif
509
252b5132
RH
510/* This array holds the chars that only start a comment at the beginning of
511 a line. If the line seems to have the form '# 123 filename'
ce8a8b2f
AM
512 .line and .file directives will appear in the pre-processed output.
513 Note that input_file.c hand checks for '#' at the beginning of the
252b5132 514 first line of the input file. This is because the compiler outputs
ce8a8b2f
AM
515 #NO_APP at the beginning of its output.
516 Also note that comments started like this one will always work if
252b5132 517 '/' isn't otherwise defined. */
b3b91714 518const char line_comment_chars[] = "#/";
252b5132 519
63a0b638 520const char line_separator_chars[] = ";";
252b5132 521
ce8a8b2f
AM
522/* Chars that can be used to separate mant from exp in floating point
523 nums. */
252b5132
RH
524const char EXP_CHARS[] = "eE";
525
ce8a8b2f
AM
526/* Chars that mean this number is a floating point constant
527 As in 0f12.456
528 or 0d1.2345e12. */
de133cf9 529const char FLT_CHARS[] = "fFdDxXhHbB";
252b5132 530
ce8a8b2f 531/* Tables for lexical analysis. */
252b5132
RH
532static char mnemonic_chars[256];
533static char register_chars[256];
534static char operand_chars[256];
252b5132 535
ce8a8b2f 536/* Lexical macros. */
252b5132
RH
537#define is_mnemonic_char(x) (mnemonic_chars[(unsigned char) x])
538#define is_operand_char(x) (operand_chars[(unsigned char) x])
539#define is_register_char(x) (register_chars[(unsigned char) x])
540#define is_space_char(x) ((x) == ' ')
252b5132 541
0234cb7c 542/* All non-digit non-letter characters that may occur in an operand. */
252b5132
RH
543static char operand_special_chars[] = "%$-+(,)*._~/<>|&^!:[@]";
544
545/* md_assemble() always leaves the strings it's passed unaltered. To
546 effect this we maintain a stack of saved characters that we've smashed
547 with '\0's (indicating end of strings for various sub-fields of the
47926f60 548 assembler instruction). */
252b5132 549static char save_stack[32];
ce8a8b2f 550static char *save_stack_p;
252b5132
RH
551#define END_STRING_AND_SAVE(s) \
552 do { *save_stack_p++ = *(s); *(s) = '\0'; } while (0)
553#define RESTORE_END_STRING(s) \
554 do { *(s) = *--save_stack_p; } while (0)
555
47926f60 556/* The instruction we're assembling. */
252b5132
RH
557static i386_insn i;
558
559/* Possible templates for current insn. */
560static const templates *current_templates;
561
31b2323c
L
562/* Per instruction expressionS buffers: max displacements & immediates. */
563static expressionS disp_expressions[MAX_MEMORY_OPERANDS];
564static expressionS im_expressions[MAX_IMMEDIATE_OPERANDS];
252b5132 565
47926f60 566/* Current operand we are working on. */
ee86248c 567static int this_operand = -1;
252b5132 568
3e73aa7c
JH
569/* We support four different modes. FLAG_CODE variable is used to distinguish
570 these. */
571
572enum flag_code {
573 CODE_32BIT,
574 CODE_16BIT,
575 CODE_64BIT };
576
577static enum flag_code flag_code;
4fa24527 578static unsigned int object_64bit;
862be3fb 579static unsigned int disallow_64bit_reloc;
3e73aa7c 580static int use_rela_relocations = 0;
e379e5f3
L
581/* __tls_get_addr/___tls_get_addr symbol for TLS. */
582static const char *tls_get_addr;
3e73aa7c 583
7af8ed2d
NC
584#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
585 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
586 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
587
351f65ca
L
588/* The ELF ABI to use. */
589enum x86_elf_abi
590{
591 I386_ABI,
7f56bc95
L
592 X86_64_ABI,
593 X86_64_X32_ABI
351f65ca
L
594};
595
596static enum x86_elf_abi x86_elf_abi = I386_ABI;
7af8ed2d 597#endif
351f65ca 598
167ad85b
TG
599#if defined (TE_PE) || defined (TE_PEP)
600/* Use big object file format. */
601static int use_big_obj = 0;
602#endif
603
8dcea932
L
604#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
605/* 1 if generating code for a shared library. */
606static int shared = 0;
b52c4ee4
IB
607
608unsigned int x86_sframe_cfa_sp_reg;
3e3e792a 609/* The other CFA base register for SFrame stack trace info. */
b52c4ee4
IB
610unsigned int x86_sframe_cfa_fp_reg;
611unsigned int x86_sframe_cfa_ra_reg;
612
8dcea932
L
613#endif
614
47926f60
KH
615/* 1 for intel syntax,
616 0 if att syntax. */
617static int intel_syntax = 0;
252b5132 618
4b5aaf5f
L
619static enum x86_64_isa
620{
621 amd64 = 1, /* AMD64 ISA. */
622 intel64 /* Intel64 ISA. */
623} isa64;
e89c5eaa 624
1efbbeb4
L
625/* 1 for intel mnemonic,
626 0 if att mnemonic. */
627static int intel_mnemonic = !SYSV386_COMPAT;
628
a60de03c
JB
629/* 1 if pseudo registers are permitted. */
630static int allow_pseudo_reg = 0;
631
47926f60
KH
632/* 1 if register prefix % not required. */
633static int allow_naked_reg = 0;
252b5132 634
33eaf5de 635/* 1 if the assembler should add BND prefix for all control-transferring
7e8b059b
L
636 instructions supporting it, even if this prefix wasn't specified
637 explicitly. */
638static int add_bnd_prefix = 0;
639
ba104c83 640/* 1 if pseudo index register, eiz/riz, is allowed . */
db51cc60
L
641static int allow_index_reg = 0;
642
d022bddd
IT
643/* 1 if the assembler should ignore LOCK prefix, even if it was
644 specified explicitly. */
645static int omit_lock_prefix = 0;
646
e4e00185
AS
647/* 1 if the assembler should encode lfence, mfence, and sfence as
648 "lock addl $0, (%{re}sp)". */
649static int avoid_fence = 0;
650
ae531041
L
651/* 1 if lfence should be inserted after every load. */
652static int lfence_after_load = 0;
653
654/* Non-zero if lfence should be inserted before indirect branch. */
655static enum lfence_before_indirect_branch_kind
656 {
657 lfence_branch_none = 0,
658 lfence_branch_register,
659 lfence_branch_memory,
660 lfence_branch_all
661 }
662lfence_before_indirect_branch;
663
664/* Non-zero if lfence should be inserted before ret. */
665static enum lfence_before_ret_kind
666 {
667 lfence_before_ret_none = 0,
668 lfence_before_ret_not,
a09f656b 669 lfence_before_ret_or,
670 lfence_before_ret_shl
ae531041
L
671 }
672lfence_before_ret;
673
674/* Types of previous instruction is .byte or prefix. */
e379e5f3
L
675static struct
676 {
677 segT seg;
678 const char *file;
679 const char *name;
680 unsigned int line;
681 enum last_insn_kind
682 {
683 last_insn_other = 0,
684 last_insn_directive,
685 last_insn_prefix
686 } kind;
687 } last_insn;
688
0cb4071e
L
689/* 1 if the assembler should generate relax relocations. */
690
691static int generate_relax_relocations
692 = DEFAULT_GENERATE_X86_RELAX_RELOCATIONS;
693
7bab8ab5 694static enum check_kind
daf50ae7 695 {
7bab8ab5
JB
696 check_none = 0,
697 check_warning,
698 check_error
daf50ae7 699 }
7bab8ab5 700sse_check, operand_check = check_warning;
daf50ae7 701
e379e5f3
L
702/* Non-zero if branches should be aligned within power of 2 boundary. */
703static int align_branch_power = 0;
704
705/* Types of branches to align. */
706enum align_branch_kind
707 {
708 align_branch_none = 0,
709 align_branch_jcc = 1,
710 align_branch_fused = 2,
711 align_branch_jmp = 3,
712 align_branch_call = 4,
713 align_branch_indirect = 5,
714 align_branch_ret = 6
715 };
716
717/* Type bits of branches to align. */
718enum align_branch_bit
719 {
720 align_branch_jcc_bit = 1 << align_branch_jcc,
721 align_branch_fused_bit = 1 << align_branch_fused,
722 align_branch_jmp_bit = 1 << align_branch_jmp,
723 align_branch_call_bit = 1 << align_branch_call,
724 align_branch_indirect_bit = 1 << align_branch_indirect,
725 align_branch_ret_bit = 1 << align_branch_ret
726 };
727
728static unsigned int align_branch = (align_branch_jcc_bit
729 | align_branch_fused_bit
730 | align_branch_jmp_bit);
731
79d72f45
HL
732/* Types of condition jump used by macro-fusion. */
733enum mf_jcc_kind
734 {
735 mf_jcc_jo = 0, /* base opcode 0x70 */
736 mf_jcc_jc, /* base opcode 0x72 */
737 mf_jcc_je, /* base opcode 0x74 */
738 mf_jcc_jna, /* base opcode 0x76 */
739 mf_jcc_js, /* base opcode 0x78 */
740 mf_jcc_jp, /* base opcode 0x7a */
741 mf_jcc_jl, /* base opcode 0x7c */
742 mf_jcc_jle, /* base opcode 0x7e */
743 };
744
745/* Types of compare flag-modifying insntructions used by macro-fusion. */
746enum mf_cmp_kind
747 {
748 mf_cmp_test_and, /* test/cmp */
749 mf_cmp_alu_cmp, /* add/sub/cmp */
750 mf_cmp_incdec /* inc/dec */
751 };
752
e379e5f3
L
753/* The maximum padding size for fused jcc. CMP like instruction can
754 be 9 bytes and jcc can be 6 bytes. Leave room just in case for
755 prefixes. */
756#define MAX_FUSED_JCC_PADDING_SIZE 20
757
758/* The maximum number of prefixes added for an instruction. */
759static unsigned int align_branch_prefix_size = 5;
760
b6f8c7c4
L
761/* Optimization:
762 1. Clear the REX_W bit with register operand if possible.
763 2. Above plus use 128bit vector instruction to clear the full vector
764 register.
765 */
766static int optimize = 0;
767
768/* Optimization:
769 1. Clear the REX_W bit with register operand if possible.
770 2. Above plus use 128bit vector instruction to clear the full vector
771 register.
772 3. Above plus optimize "test{q,l,w} $imm8,%r{64,32,16}" to
773 "testb $imm7,%r8".
774 */
775static int optimize_for_space = 0;
776
2ca3ace5
L
777/* Register prefix used for error message. */
778static const char *register_prefix = "%";
779
47926f60
KH
780/* Used in 16 bit gcc mode to add an l suffix to call, ret, enter,
781 leave, push, and pop instructions so that gcc has the same stack
782 frame as in 32 bit mode. */
783static char stackop_size = '\0';
eecb386c 784
12b55ccc
L
785/* Non-zero to optimize code alignment. */
786int optimize_align_code = 1;
787
47926f60
KH
788/* Non-zero to quieten some warnings. */
789static int quiet_warnings = 0;
a38cf1db 790
d59a54c2
JB
791/* Guard to avoid repeated warnings about non-16-bit code on 16-bit CPUs. */
792static bool pre_386_16bit_warned;
793
47926f60
KH
794/* CPU name. */
795static const char *cpu_arch_name = NULL;
6305a203 796static char *cpu_sub_arch_name = NULL;
a38cf1db 797
47926f60 798/* CPU feature flags. */
40fb9820
L
799static i386_cpu_flags cpu_arch_flags = CPU_UNKNOWN_FLAGS;
800
ccc9c027
L
801/* If we have selected a cpu we are generating instructions for. */
802static int cpu_arch_tune_set = 0;
803
9103f4f4 804/* Cpu we are generating instructions for. */
fbf3f584 805enum processor_type cpu_arch_tune = PROCESSOR_UNKNOWN;
9103f4f4
L
806
807/* CPU feature flags of cpu we are generating instructions for. */
40fb9820 808static i386_cpu_flags cpu_arch_tune_flags;
9103f4f4 809
ccc9c027 810/* CPU instruction set architecture used. */
fbf3f584 811enum processor_type cpu_arch_isa = PROCESSOR_UNKNOWN;
ccc9c027 812
9103f4f4 813/* CPU feature flags of instruction set architecture used. */
fbf3f584 814i386_cpu_flags cpu_arch_isa_flags;
9103f4f4 815
fddf5b5b
AM
816/* If set, conditional jumps are not automatically promoted to handle
817 larger than a byte offset. */
f68697e8 818static bool no_cond_jump_promotion = false;
fddf5b5b 819
c0f3af97
L
820/* Encode SSE instructions with VEX prefix. */
821static unsigned int sse2avx;
822
c8480b58
L
823/* Encode aligned vector move as unaligned vector move. */
824static unsigned int use_unaligned_vector_move;
825
539f890d
L
826/* Encode scalar AVX instructions with specific vector length. */
827static enum
828 {
829 vex128 = 0,
830 vex256
831 } avxscalar;
832
03751133
L
833/* Encode VEX WIG instructions with specific vex.w. */
834static enum
835 {
836 vexw0 = 0,
837 vexw1
838 } vexwig;
839
43234a1e
L
840/* Encode scalar EVEX LIG instructions with specific vector length. */
841static enum
842 {
843 evexl128 = 0,
844 evexl256,
845 evexl512
846 } evexlig;
847
848/* Encode EVEX WIG instructions with specific evex.w. */
849static enum
850 {
851 evexw0 = 0,
852 evexw1
853 } evexwig;
854
d3d3c6db
IT
855/* Value to encode in EVEX RC bits, for SAE-only instructions. */
856static enum rc_type evexrcig = rne;
857
29b0f896 858/* Pre-defined "_GLOBAL_OFFSET_TABLE_". */
87c245cc 859static symbolS *GOT_symbol;
29b0f896 860
a4447b93
RH
861/* The dwarf2 return column, adjusted for 32 or 64 bit. */
862unsigned int x86_dwarf2_return_column;
863
864/* The dwarf2 data alignment, adjusted for 32 or 64 bit. */
865int x86_cie_data_alignment;
866
252b5132 867/* Interface to relax_segment.
fddf5b5b
AM
868 There are 3 major relax states for 386 jump insns because the
869 different types of jumps add different sizes to frags when we're
e379e5f3
L
870 figuring out what sort of jump to choose to reach a given label.
871
872 BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING are used to align
873 branches which are handled by md_estimate_size_before_relax() and
874 i386_generic_table_relax_frag(). */
252b5132 875
47926f60 876/* Types. */
93c2a809
AM
877#define UNCOND_JUMP 0
878#define COND_JUMP 1
879#define COND_JUMP86 2
e379e5f3
L
880#define BRANCH_PADDING 3
881#define BRANCH_PREFIX 4
882#define FUSED_JCC_PADDING 5
fddf5b5b 883
47926f60 884/* Sizes. */
252b5132
RH
885#define CODE16 1
886#define SMALL 0
29b0f896 887#define SMALL16 (SMALL | CODE16)
252b5132 888#define BIG 2
29b0f896 889#define BIG16 (BIG | CODE16)
252b5132
RH
890
891#ifndef INLINE
892#ifdef __GNUC__
893#define INLINE __inline__
894#else
895#define INLINE
896#endif
897#endif
898
fddf5b5b
AM
899#define ENCODE_RELAX_STATE(type, size) \
900 ((relax_substateT) (((type) << 2) | (size)))
901#define TYPE_FROM_RELAX_STATE(s) \
902 ((s) >> 2)
903#define DISP_SIZE_FROM_RELAX_STATE(s) \
904 ((((s) & 3) == BIG ? 4 : (((s) & 3) == BIG16 ? 2 : 1)))
252b5132
RH
905
906/* This table is used by relax_frag to promote short jumps to long
907 ones where necessary. SMALL (short) jumps may be promoted to BIG
908 (32 bit long) ones, and SMALL16 jumps to BIG16 (16 bit long). We
909 don't allow a short jump in a 32 bit code segment to be promoted to
910 a 16 bit offset jump because it's slower (requires data size
911 prefix), and doesn't work, unless the destination is in the bottom
912 64k of the code segment (The top 16 bits of eip are zeroed). */
913
914const relax_typeS md_relax_table[] =
915{
24eab124
AM
916 /* The fields are:
917 1) most positive reach of this state,
918 2) most negative reach of this state,
93c2a809 919 3) how many bytes this mode will have in the variable part of the frag
ce8a8b2f 920 4) which index into the table to try if we can't fit into this one. */
252b5132 921
fddf5b5b 922 /* UNCOND_JUMP states. */
93c2a809
AM
923 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG)},
924 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16)},
925 /* dword jmp adds 4 bytes to frag:
926 0 extra opcode bytes, 4 displacement bytes. */
252b5132 927 {0, 0, 4, 0},
93c2a809
AM
928 /* word jmp adds 2 byte2 to frag:
929 0 extra opcode bytes, 2 displacement bytes. */
252b5132
RH
930 {0, 0, 2, 0},
931
93c2a809
AM
932 /* COND_JUMP states. */
933 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG)},
934 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG16)},
935 /* dword conditionals adds 5 bytes to frag:
936 1 extra opcode byte, 4 displacement bytes. */
937 {0, 0, 5, 0},
fddf5b5b 938 /* word conditionals add 3 bytes to frag:
93c2a809
AM
939 1 extra opcode byte, 2 displacement bytes. */
940 {0, 0, 3, 0},
941
942 /* COND_JUMP86 states. */
943 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG)},
944 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG16)},
945 /* dword conditionals adds 5 bytes to frag:
946 1 extra opcode byte, 4 displacement bytes. */
947 {0, 0, 5, 0},
948 /* word conditionals add 4 bytes to frag:
949 1 displacement byte and a 3 byte long branch insn. */
950 {0, 0, 4, 0}
252b5132
RH
951};
952
6ceeed25 953#define ARCH(n, t, f, s) \
ae89daec
JB
954 { STRING_COMMA_LEN (#n), s, PROCESSOR_ ## t, CPU_ ## f ## _FLAGS, \
955 CPU_NONE_FLAGS }
956#define SUBARCH(n, e, d, s) \
957 { STRING_COMMA_LEN (#n), s, PROCESSOR_NONE, CPU_ ## e ## _FLAGS, \
958 CPU_ ## d ## _FLAGS }
6ceeed25 959
9103f4f4
L
960static const arch_entry cpu_arch[] =
961{
3ce2ebcf
JB
962 /* Do not replace the first two entries - i386_target_format() and
963 set_cpu_arch() rely on them being there in this order. */
6ceeed25
JB
964 ARCH (generic32, GENERIC32, GENERIC32, false),
965 ARCH (generic64, GENERIC64, GENERIC64, false),
966 ARCH (i8086, UNKNOWN, NONE, false),
4d97c5c8
JB
967 ARCH (i186, UNKNOWN, 186, false),
968 ARCH (i286, UNKNOWN, 286, false),
969 ARCH (i386, I386, 386, false),
970 ARCH (i486, I486, 486, false),
971 ARCH (i586, PENTIUM, 586, false),
972 ARCH (i686, PENTIUMPRO, 686, false),
973 ARCH (pentium, PENTIUM, 586, false),
6ceeed25
JB
974 ARCH (pentiumpro, PENTIUMPRO, PENTIUMPRO, false),
975 ARCH (pentiumii, PENTIUMPRO, P2, false),
976 ARCH (pentiumiii, PENTIUMPRO, P3, false),
977 ARCH (pentium4, PENTIUM4, P4, false),
978 ARCH (prescott, NOCONA, CORE, false),
979 ARCH (nocona, NOCONA, NOCONA, false),
980 ARCH (yonah, CORE, CORE, true),
981 ARCH (core, CORE, CORE, false),
982 ARCH (merom, CORE2, CORE2, true),
983 ARCH (core2, CORE2, CORE2, false),
984 ARCH (corei7, COREI7, COREI7, false),
985 ARCH (iamcu, IAMCU, IAMCU, false),
986 ARCH (k6, K6, K6, false),
987 ARCH (k6_2, K6, K6_2, false),
988 ARCH (athlon, ATHLON, ATHLON, false),
989 ARCH (sledgehammer, K8, K8, true),
990 ARCH (opteron, K8, K8, false),
991 ARCH (k8, K8, K8, false),
992 ARCH (amdfam10, AMDFAM10, AMDFAM10, false),
993 ARCH (bdver1, BD, BDVER1, false),
994 ARCH (bdver2, BD, BDVER2, false),
995 ARCH (bdver3, BD, BDVER3, false),
996 ARCH (bdver4, BD, BDVER4, false),
997 ARCH (znver1, ZNVER, ZNVER1, false),
998 ARCH (znver2, ZNVER, ZNVER2, false),
999 ARCH (znver3, ZNVER, ZNVER3, false),
b0e8fa7f 1000 ARCH (znver4, ZNVER, ZNVER4, false),
6ceeed25
JB
1001 ARCH (btver1, BT, BTVER1, false),
1002 ARCH (btver2, BT, BTVER2, false),
1003
4d97c5c8
JB
1004 SUBARCH (8087, 8087, ANY_8087, false),
1005 SUBARCH (87, NONE, ANY_8087, false), /* Disable only! */
ae89daec
JB
1006 SUBARCH (287, 287, ANY_287, false),
1007 SUBARCH (387, 387, ANY_387, false),
1008 SUBARCH (687, 687, ANY_687, false),
4d97c5c8 1009 SUBARCH (cmov, CMOV, CMOV, false),
88bd2203 1010 SUBARCH (fxsr, FXSR, ANY_FXSR, false),
ae89daec
JB
1011 SUBARCH (mmx, MMX, ANY_MMX, false),
1012 SUBARCH (sse, SSE, ANY_SSE, false),
1013 SUBARCH (sse2, SSE2, ANY_SSE2, false),
1014 SUBARCH (sse3, SSE3, ANY_SSE3, false),
1015 SUBARCH (sse4a, SSE4A, ANY_SSE4A, false),
1016 SUBARCH (ssse3, SSSE3, ANY_SSSE3, false),
1017 SUBARCH (sse4.1, SSE4_1, ANY_SSE4_1, false),
1018 SUBARCH (sse4.2, SSE4_2, ANY_SSE4_2, false),
1019 SUBARCH (sse4, SSE4_2, ANY_SSE4_1, false),
1020 SUBARCH (avx, AVX, ANY_AVX, false),
1021 SUBARCH (avx2, AVX2, ANY_AVX2, false),
1022 SUBARCH (avx512f, AVX512F, ANY_AVX512F, false),
1023 SUBARCH (avx512cd, AVX512CD, ANY_AVX512CD, false),
1024 SUBARCH (avx512er, AVX512ER, ANY_AVX512ER, false),
1025 SUBARCH (avx512pf, AVX512PF, ANY_AVX512PF, false),
1026 SUBARCH (avx512dq, AVX512DQ, ANY_AVX512DQ, false),
1027 SUBARCH (avx512bw, AVX512BW, ANY_AVX512BW, false),
1028 SUBARCH (avx512vl, AVX512VL, ANY_AVX512VL, false),
cafa5ef7 1029 SUBARCH (monitor, MONITOR, MONITOR, false),
25626f79
JB
1030 SUBARCH (vmx, VMX, ANY_VMX, false),
1031 SUBARCH (vmfunc, VMFUNC, ANY_VMFUNC, false),
ae89daec 1032 SUBARCH (smx, SMX, SMX, false),
5091b9ee
JB
1033 SUBARCH (xsave, XSAVE, ANY_XSAVE, false),
1034 SUBARCH (xsaveopt, XSAVEOPT, ANY_XSAVEOPT, false),
1035 SUBARCH (xsavec, XSAVEC, ANY_XSAVEC, false),
1036 SUBARCH (xsaves, XSAVES, ANY_XSAVES, false),
1037 SUBARCH (aes, AES, ANY_AES, false),
1038 SUBARCH (pclmul, PCLMUL, ANY_PCLMUL, false),
1039 SUBARCH (clmul, PCLMUL, ANY_PCLMUL, true),
ae89daec
JB
1040 SUBARCH (fsgsbase, FSGSBASE, FSGSBASE, false),
1041 SUBARCH (rdrnd, RDRND, RDRND, false),
5091b9ee 1042 SUBARCH (f16c, F16C, ANY_F16C, false),
ae89daec 1043 SUBARCH (bmi2, BMI2, BMI2, false),
5091b9ee
JB
1044 SUBARCH (fma, FMA, ANY_FMA, false),
1045 SUBARCH (fma4, FMA4, ANY_FMA4, false),
1046 SUBARCH (xop, XOP, ANY_XOP, false),
1047 SUBARCH (lwp, LWP, ANY_LWP, false),
ae89daec
JB
1048 SUBARCH (movbe, MOVBE, MOVBE, false),
1049 SUBARCH (cx16, CX16, CX16, false),
c3bb24f5 1050 SUBARCH (lahf_sahf, LAHF_SAHF, LAHF_SAHF, false),
25626f79 1051 SUBARCH (ept, EPT, ANY_EPT, false),
ae89daec
JB
1052 SUBARCH (lzcnt, LZCNT, LZCNT, false),
1053 SUBARCH (popcnt, POPCNT, POPCNT, false),
1054 SUBARCH (hle, HLE, HLE, false),
760ab3d0
JB
1055 SUBARCH (rtm, RTM, ANY_RTM, false),
1056 SUBARCH (tsx, TSX, TSX, false),
ae89daec
JB
1057 SUBARCH (invpcid, INVPCID, INVPCID, false),
1058 SUBARCH (clflush, CLFLUSH, CLFLUSH, false),
1059 SUBARCH (nop, NOP, NOP, false),
1060 SUBARCH (syscall, SYSCALL, SYSCALL, false),
1061 SUBARCH (rdtscp, RDTSCP, RDTSCP, false),
5091b9ee
JB
1062 SUBARCH (3dnow, 3DNOW, ANY_3DNOW, false),
1063 SUBARCH (3dnowa, 3DNOWA, ANY_3DNOWA, false),
ae89daec 1064 SUBARCH (padlock, PADLOCK, PADLOCK, false),
0919e770
JB
1065 SUBARCH (pacifica, SVME, ANY_SVME, true),
1066 SUBARCH (svme, SVME, ANY_SVME, false),
ae89daec
JB
1067 SUBARCH (abm, ABM, ABM, false),
1068 SUBARCH (bmi, BMI, BMI, false),
1069 SUBARCH (tbm, TBM, TBM, false),
1070 SUBARCH (adx, ADX, ADX, false),
1071 SUBARCH (rdseed, RDSEED, RDSEED, false),
1072 SUBARCH (prfchw, PRFCHW, PRFCHW, false),
1073 SUBARCH (smap, SMAP, SMAP, false),
5091b9ee
JB
1074 SUBARCH (mpx, MPX, ANY_MPX, false),
1075 SUBARCH (sha, SHA, ANY_SHA, false),
ae89daec
JB
1076 SUBARCH (clflushopt, CLFLUSHOPT, CLFLUSHOPT, false),
1077 SUBARCH (prefetchwt1, PREFETCHWT1, PREFETCHWT1, false),
1078 SUBARCH (se1, SE1, SE1, false),
1079 SUBARCH (clwb, CLWB, CLWB, false),
1080 SUBARCH (avx512ifma, AVX512IFMA, ANY_AVX512IFMA, false),
1081 SUBARCH (avx512vbmi, AVX512VBMI, ANY_AVX512VBMI, false),
1082 SUBARCH (avx512_4fmaps, AVX512_4FMAPS, ANY_AVX512_4FMAPS, false),
1083 SUBARCH (avx512_4vnniw, AVX512_4VNNIW, ANY_AVX512_4VNNIW, false),
1084 SUBARCH (avx512_vpopcntdq, AVX512_VPOPCNTDQ, ANY_AVX512_VPOPCNTDQ, false),
1085 SUBARCH (avx512_vbmi2, AVX512_VBMI2, ANY_AVX512_VBMI2, false),
1086 SUBARCH (avx512_vnni, AVX512_VNNI, ANY_AVX512_VNNI, false),
1087 SUBARCH (avx512_bitalg, AVX512_BITALG, ANY_AVX512_BITALG, false),
1088 SUBARCH (avx_vnni, AVX_VNNI, ANY_AVX_VNNI, false),
1089 SUBARCH (clzero, CLZERO, CLZERO, false),
1090 SUBARCH (mwaitx, MWAITX, MWAITX, false),
5091b9ee 1091 SUBARCH (ospke, OSPKE, ANY_OSPKE, false),
ae89daec
JB
1092 SUBARCH (rdpid, RDPID, RDPID, false),
1093 SUBARCH (ptwrite, PTWRITE, PTWRITE, false),
4d97c5c8
JB
1094 SUBARCH (ibt, IBT, IBT, false),
1095 SUBARCH (shstk, SHSTK, SHSTK, false),
88bd2203 1096 SUBARCH (gfni, GFNI, ANY_GFNI, false),
b20f4261
JB
1097 SUBARCH (vaes, VAES, ANY_VAES, false),
1098 SUBARCH (vpclmulqdq, VPCLMULQDQ, ANY_VPCLMULQDQ, false),
ae89daec
JB
1099 SUBARCH (wbnoinvd, WBNOINVD, WBNOINVD, false),
1100 SUBARCH (pconfig, PCONFIG, PCONFIG, false),
1101 SUBARCH (waitpkg, WAITPKG, WAITPKG, false),
1102 SUBARCH (cldemote, CLDEMOTE, CLDEMOTE, false),
1103 SUBARCH (amx_int8, AMX_INT8, ANY_AMX_INT8, false),
1104 SUBARCH (amx_bf16, AMX_BF16, ANY_AMX_BF16, false),
5091b9ee 1105 SUBARCH (amx_fp16, AMX_FP16, ANY_AMX_FP16, false),
ae89daec 1106 SUBARCH (amx_tile, AMX_TILE, ANY_AMX_TILE, false),
4d97c5c8
JB
1107 SUBARCH (movdiri, MOVDIRI, MOVDIRI, false),
1108 SUBARCH (movdir64b, MOVDIR64B, MOVDIR64B, false),
ae89daec
JB
1109 SUBARCH (avx512_bf16, AVX512_BF16, ANY_AVX512_BF16, false),
1110 SUBARCH (avx512_vp2intersect, AVX512_VP2INTERSECT,
1111 ANY_AVX512_VP2INTERSECT, false),
4d97c5c8
JB
1112 SUBARCH (tdx, TDX, TDX, false),
1113 SUBARCH (enqcmd, ENQCMD, ENQCMD, false),
1114 SUBARCH (serialize, SERIALIZE, SERIALIZE, false),
ae89daec
JB
1115 SUBARCH (rdpru, RDPRU, RDPRU, false),
1116 SUBARCH (mcommit, MCOMMIT, MCOMMIT, false),
0919e770 1117 SUBARCH (sev_es, SEV_ES, ANY_SEV_ES, false),
760ab3d0 1118 SUBARCH (tsxldtrk, TSXLDTRK, ANY_TSXLDTRK, false),
88bd2203
JB
1119 SUBARCH (kl, KL, ANY_KL, false),
1120 SUBARCH (widekl, WIDEKL, ANY_WIDEKL, false),
4d97c5c8
JB
1121 SUBARCH (uintr, UINTR, UINTR, false),
1122 SUBARCH (hreset, HRESET, HRESET, false),
ae89daec 1123 SUBARCH (avx512_fp16, AVX512_FP16, ANY_AVX512_FP16, false),
ef07be45 1124 SUBARCH (prefetchi, PREFETCHI, PREFETCHI, false),
4321af3e 1125 SUBARCH (avx_ifma, AVX_IFMA, ANY_AVX_IFMA, false),
23ae61ad 1126 SUBARCH (avx_vnni_int8, AVX_VNNI_INT8, ANY_AVX_VNNI_INT8, false),
4d97c5c8
JB
1127 SUBARCH (cmpccxadd, CMPCCXADD, CMPCCXADD, false),
1128 SUBARCH (wrmsrns, WRMSRNS, WRMSRNS, false),
1129 SUBARCH (msrlist, MSRLIST, MSRLIST, false),
01d8ce74 1130 SUBARCH (avx_ne_convert, AVX_NE_CONVERT, ANY_AVX_NE_CONVERT, false),
4d97c5c8 1131 SUBARCH (rao_int, RAO_INT, RAO_INT, false),
0919e770 1132 SUBARCH (rmpquery, RMPQUERY, ANY_RMPQUERY, false),
293f5f65
L
1133};
1134
6ceeed25
JB
1135#undef SUBARCH
1136#undef ARCH
1137
704209c0 1138#ifdef I386COFF
a6c24e68
NC
1139/* Like s_lcomm_internal in gas/read.c but the alignment string
1140 is allowed to be optional. */
1141
1142static symbolS *
1143pe_lcomm_internal (int needs_align, symbolS *symbolP, addressT size)
1144{
1145 addressT align = 0;
1146
1147 SKIP_WHITESPACE ();
1148
7ab9ffdd 1149 if (needs_align
a6c24e68
NC
1150 && *input_line_pointer == ',')
1151 {
1152 align = parse_align (needs_align - 1);
7ab9ffdd 1153
a6c24e68
NC
1154 if (align == (addressT) -1)
1155 return NULL;
1156 }
1157 else
1158 {
1159 if (size >= 8)
1160 align = 3;
1161 else if (size >= 4)
1162 align = 2;
1163 else if (size >= 2)
1164 align = 1;
1165 else
1166 align = 0;
1167 }
1168
1169 bss_alloc (symbolP, size, align);
1170 return symbolP;
1171}
1172
704209c0 1173static void
a6c24e68
NC
1174pe_lcomm (int needs_align)
1175{
1176 s_comm_internal (needs_align * 2, pe_lcomm_internal);
1177}
704209c0 1178#endif
a6c24e68 1179
29b0f896
AM
1180const pseudo_typeS md_pseudo_table[] =
1181{
1182#if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
1183 {"align", s_align_bytes, 0},
1184#else
1185 {"align", s_align_ptwo, 0},
1186#endif
1187 {"arch", set_cpu_arch, 0},
1188#ifndef I386COFF
1189 {"bss", s_bss, 0},
a6c24e68
NC
1190#else
1191 {"lcomm", pe_lcomm, 1},
29b0f896
AM
1192#endif
1193 {"ffloat", float_cons, 'f'},
1194 {"dfloat", float_cons, 'd'},
1195 {"tfloat", float_cons, 'x'},
7d19d096 1196 {"hfloat", float_cons, 'h'},
de133cf9 1197 {"bfloat16", float_cons, 'b'},
29b0f896 1198 {"value", cons, 2},
d182319b 1199 {"slong", signed_cons, 4},
edd67638 1200 {"insn", s_insn, 0},
29b0f896
AM
1201 {"noopt", s_ignore, 0},
1202 {"optim", s_ignore, 0},
1203 {"code16gcc", set_16bit_gcc_code_flag, CODE_16BIT},
1204 {"code16", set_code_flag, CODE_16BIT},
1205 {"code32", set_code_flag, CODE_32BIT},
da5f19a2 1206#ifdef BFD64
29b0f896 1207 {"code64", set_code_flag, CODE_64BIT},
da5f19a2 1208#endif
29b0f896
AM
1209 {"intel_syntax", set_intel_syntax, 1},
1210 {"att_syntax", set_intel_syntax, 0},
1efbbeb4
L
1211 {"intel_mnemonic", set_intel_mnemonic, 1},
1212 {"att_mnemonic", set_intel_mnemonic, 0},
db51cc60
L
1213 {"allow_index_reg", set_allow_index_reg, 1},
1214 {"disallow_index_reg", set_allow_index_reg, 0},
7bab8ab5
JB
1215 {"sse_check", set_check, 0},
1216 {"operand_check", set_check, 1},
3b22753a
L
1217#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1218 {"largecomm", handle_large_common, 0},
07a53e5c 1219#else
68d20676 1220 {"file", dwarf2_directive_file, 0},
07a53e5c
RH
1221 {"loc", dwarf2_directive_loc, 0},
1222 {"loc_mark_labels", dwarf2_directive_loc_mark_labels, 0},
3b22753a 1223#endif
6482c264
NC
1224#ifdef TE_PE
1225 {"secrel32", pe_directive_secrel, 0},
145667f8 1226 {"secidx", pe_directive_secidx, 0},
6482c264 1227#endif
29b0f896
AM
1228 {0, 0, 0}
1229};
1230
1231/* For interface with expression (). */
1232extern char *input_line_pointer;
1233
1234/* Hash table for instruction mnemonic lookup. */
629310ab 1235static htab_t op_hash;
29b0f896
AM
1236
1237/* Hash table for register lookup. */
629310ab 1238static htab_t reg_hash;
29b0f896 1239\f
ce8a8b2f
AM
1240 /* Various efficient no-op patterns for aligning code labels.
1241 Note: Don't try to assemble the instructions in the comments.
1242 0L and 0w are not legal. */
62a02d25
L
1243static const unsigned char f32_1[] =
1244 {0x90}; /* nop */
1245static const unsigned char f32_2[] =
1246 {0x66,0x90}; /* xchg %ax,%ax */
1247static const unsigned char f32_3[] =
1248 {0x8d,0x76,0x00}; /* leal 0(%esi),%esi */
1249static const unsigned char f32_4[] =
1250 {0x8d,0x74,0x26,0x00}; /* leal 0(%esi,1),%esi */
62a02d25
L
1251static const unsigned char f32_6[] =
1252 {0x8d,0xb6,0x00,0x00,0x00,0x00}; /* leal 0L(%esi),%esi */
1253static const unsigned char f32_7[] =
1254 {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal 0L(%esi,1),%esi */
62a02d25 1255static const unsigned char f16_3[] =
3ae729d5 1256 {0x8d,0x74,0x00}; /* lea 0(%si),%si */
62a02d25 1257static const unsigned char f16_4[] =
3ae729d5
L
1258 {0x8d,0xb4,0x00,0x00}; /* lea 0W(%si),%si */
1259static const unsigned char jump_disp8[] =
1260 {0xeb}; /* jmp disp8 */
1261static const unsigned char jump32_disp32[] =
1262 {0xe9}; /* jmp disp32 */
1263static const unsigned char jump16_disp32[] =
1264 {0x66,0xe9}; /* jmp disp32 */
62a02d25
L
1265/* 32-bit NOPs patterns. */
1266static const unsigned char *const f32_patt[] = {
3ae729d5 1267 f32_1, f32_2, f32_3, f32_4, NULL, f32_6, f32_7
62a02d25
L
1268};
1269/* 16-bit NOPs patterns. */
1270static const unsigned char *const f16_patt[] = {
3ae729d5 1271 f32_1, f32_2, f16_3, f16_4
62a02d25
L
1272};
1273/* nopl (%[re]ax) */
1274static const unsigned char alt_3[] =
1275 {0x0f,0x1f,0x00};
1276/* nopl 0(%[re]ax) */
1277static const unsigned char alt_4[] =
1278 {0x0f,0x1f,0x40,0x00};
1279/* nopl 0(%[re]ax,%[re]ax,1) */
1280static const unsigned char alt_5[] =
1281 {0x0f,0x1f,0x44,0x00,0x00};
1282/* nopw 0(%[re]ax,%[re]ax,1) */
1283static const unsigned char alt_6[] =
1284 {0x66,0x0f,0x1f,0x44,0x00,0x00};
1285/* nopl 0L(%[re]ax) */
1286static const unsigned char alt_7[] =
1287 {0x0f,0x1f,0x80,0x00,0x00,0x00,0x00};
1288/* nopl 0L(%[re]ax,%[re]ax,1) */
1289static const unsigned char alt_8[] =
1290 {0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1291/* nopw 0L(%[re]ax,%[re]ax,1) */
1292static const unsigned char alt_9[] =
1293 {0x66,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1294/* nopw %cs:0L(%[re]ax,%[re]ax,1) */
1295static const unsigned char alt_10[] =
1296 {0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
3ae729d5
L
1297/* data16 nopw %cs:0L(%eax,%eax,1) */
1298static const unsigned char alt_11[] =
1299 {0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
62a02d25
L
1300/* 32-bit and 64-bit NOPs patterns. */
1301static const unsigned char *const alt_patt[] = {
1302 f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
3ae729d5 1303 alt_9, alt_10, alt_11
62a02d25
L
1304};
1305
1306/* Genenerate COUNT bytes of NOPs to WHERE from PATT with the maximum
1307 size of a single NOP instruction MAX_SINGLE_NOP_SIZE. */
1308
1309static void
1310i386_output_nops (char *where, const unsigned char *const *patt,
1311 int count, int max_single_nop_size)
1312
1313{
3ae729d5
L
1314 /* Place the longer NOP first. */
1315 int last;
1316 int offset;
3076e594
NC
1317 const unsigned char *nops;
1318
1319 if (max_single_nop_size < 1)
1320 {
1321 as_fatal (_("i386_output_nops called to generate nops of at most %d bytes!"),
1322 max_single_nop_size);
1323 return;
1324 }
1325
1326 nops = patt[max_single_nop_size - 1];
3ae729d5
L
1327
1328 /* Use the smaller one if the requsted one isn't available. */
1329 if (nops == NULL)
62a02d25 1330 {
3ae729d5
L
1331 max_single_nop_size--;
1332 nops = patt[max_single_nop_size - 1];
62a02d25
L
1333 }
1334
3ae729d5
L
1335 last = count % max_single_nop_size;
1336
1337 count -= last;
1338 for (offset = 0; offset < count; offset += max_single_nop_size)
1339 memcpy (where + offset, nops, max_single_nop_size);
1340
1341 if (last)
1342 {
1343 nops = patt[last - 1];
1344 if (nops == NULL)
1345 {
1346 /* Use the smaller one plus one-byte NOP if the needed one
1347 isn't available. */
1348 last--;
1349 nops = patt[last - 1];
1350 memcpy (where + offset, nops, last);
1351 where[offset + last] = *patt[0];
1352 }
1353 else
1354 memcpy (where + offset, nops, last);
1355 }
62a02d25
L
1356}
1357
3ae729d5
L
1358static INLINE int
1359fits_in_imm7 (offsetT num)
1360{
1361 return (num & 0x7f) == num;
1362}
1363
1364static INLINE int
1365fits_in_imm31 (offsetT num)
1366{
1367 return (num & 0x7fffffff) == num;
1368}
62a02d25
L
1369
1370/* Genenerate COUNT bytes of NOPs to WHERE with the maximum size of a
1371 single NOP instruction LIMIT. */
1372
1373void
3ae729d5 1374i386_generate_nops (fragS *fragP, char *where, offsetT count, int limit)
62a02d25 1375{
3ae729d5 1376 const unsigned char *const *patt = NULL;
62a02d25 1377 int max_single_nop_size;
3ae729d5
L
1378 /* Maximum number of NOPs before switching to jump over NOPs. */
1379 int max_number_of_nops;
62a02d25 1380
3ae729d5 1381 switch (fragP->fr_type)
62a02d25 1382 {
3ae729d5
L
1383 case rs_fill_nop:
1384 case rs_align_code:
1385 break;
e379e5f3
L
1386 case rs_machine_dependent:
1387 /* Allow NOP padding for jumps and calls. */
1388 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
1389 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
1390 break;
1391 /* Fall through. */
3ae729d5 1392 default:
62a02d25
L
1393 return;
1394 }
1395
ccc9c027
L
1396 /* We need to decide which NOP sequence to use for 32bit and
1397 64bit. When -mtune= is used:
4eed87de 1398
76bc74dc
L
1399 1. For PROCESSOR_I386, PROCESSOR_I486, PROCESSOR_PENTIUM and
1400 PROCESSOR_GENERIC32, f32_patt will be used.
80b8656c
L
1401 2. For the rest, alt_patt will be used.
1402
1403 When -mtune= isn't used, alt_patt will be used if
22109423 1404 cpu_arch_isa_flags has CpuNop. Otherwise, f32_patt will
76bc74dc 1405 be used.
ccc9c027
L
1406
1407 When -march= or .arch is used, we can't use anything beyond
1408 cpu_arch_isa_flags. */
1409
1410 if (flag_code == CODE_16BIT)
1411 {
3ae729d5
L
1412 patt = f16_patt;
1413 max_single_nop_size = sizeof (f16_patt) / sizeof (f16_patt[0]);
1414 /* Limit number of NOPs to 2 in 16-bit mode. */
1415 max_number_of_nops = 2;
252b5132 1416 }
33fef721 1417 else
ccc9c027 1418 {
fbf3f584 1419 if (fragP->tc_frag_data.isa == PROCESSOR_UNKNOWN)
ccc9c027
L
1420 {
1421 /* PROCESSOR_UNKNOWN means that all ISAs may be used. */
1422 switch (cpu_arch_tune)
1423 {
1424 case PROCESSOR_UNKNOWN:
1425 /* We use cpu_arch_isa_flags to check if we SHOULD
22109423
L
1426 optimize with nops. */
1427 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1428 patt = alt_patt;
ccc9c027
L
1429 else
1430 patt = f32_patt;
1431 break;
ccc9c027
L
1432 case PROCESSOR_PENTIUM4:
1433 case PROCESSOR_NOCONA:
ef05d495 1434 case PROCESSOR_CORE:
76bc74dc 1435 case PROCESSOR_CORE2:
bd5295b2 1436 case PROCESSOR_COREI7:
76bc74dc 1437 case PROCESSOR_GENERIC64:
ccc9c027
L
1438 case PROCESSOR_K6:
1439 case PROCESSOR_ATHLON:
1440 case PROCESSOR_K8:
4eed87de 1441 case PROCESSOR_AMDFAM10:
8aedb9fe 1442 case PROCESSOR_BD:
029f3522 1443 case PROCESSOR_ZNVER:
7b458c12 1444 case PROCESSOR_BT:
80b8656c 1445 patt = alt_patt;
ccc9c027 1446 break;
76bc74dc 1447 case PROCESSOR_I386:
ccc9c027
L
1448 case PROCESSOR_I486:
1449 case PROCESSOR_PENTIUM:
2dde1948 1450 case PROCESSOR_PENTIUMPRO:
81486035 1451 case PROCESSOR_IAMCU:
ccc9c027
L
1452 case PROCESSOR_GENERIC32:
1453 patt = f32_patt;
1454 break;
c368d2a8
JB
1455 case PROCESSOR_NONE:
1456 abort ();
4eed87de 1457 }
ccc9c027
L
1458 }
1459 else
1460 {
fbf3f584 1461 switch (fragP->tc_frag_data.tune)
ccc9c027
L
1462 {
1463 case PROCESSOR_UNKNOWN:
e6a14101 1464 /* When cpu_arch_isa is set, cpu_arch_tune shouldn't be
ccc9c027
L
1465 PROCESSOR_UNKNOWN. */
1466 abort ();
1467 break;
1468
76bc74dc 1469 case PROCESSOR_I386:
ccc9c027
L
1470 case PROCESSOR_I486:
1471 case PROCESSOR_PENTIUM:
81486035 1472 case PROCESSOR_IAMCU:
ccc9c027
L
1473 case PROCESSOR_K6:
1474 case PROCESSOR_ATHLON:
1475 case PROCESSOR_K8:
4eed87de 1476 case PROCESSOR_AMDFAM10:
8aedb9fe 1477 case PROCESSOR_BD:
029f3522 1478 case PROCESSOR_ZNVER:
7b458c12 1479 case PROCESSOR_BT:
ccc9c027
L
1480 case PROCESSOR_GENERIC32:
1481 /* We use cpu_arch_isa_flags to check if we CAN optimize
22109423
L
1482 with nops. */
1483 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1484 patt = alt_patt;
ccc9c027
L
1485 else
1486 patt = f32_patt;
1487 break;
76bc74dc
L
1488 case PROCESSOR_PENTIUMPRO:
1489 case PROCESSOR_PENTIUM4:
1490 case PROCESSOR_NOCONA:
1491 case PROCESSOR_CORE:
ef05d495 1492 case PROCESSOR_CORE2:
bd5295b2 1493 case PROCESSOR_COREI7:
22109423 1494 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1495 patt = alt_patt;
ccc9c027
L
1496 else
1497 patt = f32_patt;
1498 break;
1499 case PROCESSOR_GENERIC64:
80b8656c 1500 patt = alt_patt;
ccc9c027 1501 break;
c368d2a8
JB
1502 case PROCESSOR_NONE:
1503 abort ();
4eed87de 1504 }
ccc9c027
L
1505 }
1506
76bc74dc
L
1507 if (patt == f32_patt)
1508 {
3ae729d5
L
1509 max_single_nop_size = sizeof (f32_patt) / sizeof (f32_patt[0]);
1510 /* Limit number of NOPs to 2 for older processors. */
1511 max_number_of_nops = 2;
76bc74dc
L
1512 }
1513 else
1514 {
3ae729d5
L
1515 max_single_nop_size = sizeof (alt_patt) / sizeof (alt_patt[0]);
1516 /* Limit number of NOPs to 7 for newer processors. */
1517 max_number_of_nops = 7;
1518 }
1519 }
1520
1521 if (limit == 0)
1522 limit = max_single_nop_size;
1523
1524 if (fragP->fr_type == rs_fill_nop)
1525 {
1526 /* Output NOPs for .nop directive. */
1527 if (limit > max_single_nop_size)
1528 {
1529 as_bad_where (fragP->fr_file, fragP->fr_line,
1530 _("invalid single nop size: %d "
1531 "(expect within [0, %d])"),
1532 limit, max_single_nop_size);
1533 return;
1534 }
1535 }
e379e5f3 1536 else if (fragP->fr_type != rs_machine_dependent)
3ae729d5
L
1537 fragP->fr_var = count;
1538
1539 if ((count / max_single_nop_size) > max_number_of_nops)
1540 {
1541 /* Generate jump over NOPs. */
1542 offsetT disp = count - 2;
1543 if (fits_in_imm7 (disp))
1544 {
1545 /* Use "jmp disp8" if possible. */
1546 count = disp;
1547 where[0] = jump_disp8[0];
1548 where[1] = count;
1549 where += 2;
1550 }
1551 else
1552 {
1553 unsigned int size_of_jump;
1554
1555 if (flag_code == CODE_16BIT)
1556 {
1557 where[0] = jump16_disp32[0];
1558 where[1] = jump16_disp32[1];
1559 size_of_jump = 2;
1560 }
1561 else
1562 {
1563 where[0] = jump32_disp32[0];
1564 size_of_jump = 1;
1565 }
1566
1567 count -= size_of_jump + 4;
1568 if (!fits_in_imm31 (count))
1569 {
1570 as_bad_where (fragP->fr_file, fragP->fr_line,
1571 _("jump over nop padding out of range"));
1572 return;
1573 }
1574
1575 md_number_to_chars (where + size_of_jump, count, 4);
1576 where += size_of_jump + 4;
76bc74dc 1577 }
ccc9c027 1578 }
3ae729d5
L
1579
1580 /* Generate multiple NOPs. */
1581 i386_output_nops (where, patt, count, limit);
252b5132
RH
1582}
1583
c6fb90c8 1584static INLINE int
0dfbf9d7 1585operand_type_all_zero (const union i386_operand_type *x)
40fb9820 1586{
0dfbf9d7 1587 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1588 {
1589 case 3:
0dfbf9d7 1590 if (x->array[2])
c6fb90c8 1591 return 0;
1a0670f3 1592 /* Fall through. */
c6fb90c8 1593 case 2:
0dfbf9d7 1594 if (x->array[1])
c6fb90c8 1595 return 0;
1a0670f3 1596 /* Fall through. */
c6fb90c8 1597 case 1:
0dfbf9d7 1598 return !x->array[0];
c6fb90c8
L
1599 default:
1600 abort ();
1601 }
40fb9820
L
1602}
1603
c6fb90c8 1604static INLINE void
0dfbf9d7 1605operand_type_set (union i386_operand_type *x, unsigned int v)
40fb9820 1606{
0dfbf9d7 1607 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1608 {
1609 case 3:
0dfbf9d7 1610 x->array[2] = v;
1a0670f3 1611 /* Fall through. */
c6fb90c8 1612 case 2:
0dfbf9d7 1613 x->array[1] = v;
1a0670f3 1614 /* Fall through. */
c6fb90c8 1615 case 1:
0dfbf9d7 1616 x->array[0] = v;
1a0670f3 1617 /* Fall through. */
c6fb90c8
L
1618 break;
1619 default:
1620 abort ();
1621 }
bab6aec1
JB
1622
1623 x->bitfield.class = ClassNone;
75e5731b 1624 x->bitfield.instance = InstanceNone;
c6fb90c8 1625}
40fb9820 1626
c6fb90c8 1627static INLINE int
0dfbf9d7
L
1628operand_type_equal (const union i386_operand_type *x,
1629 const union i386_operand_type *y)
c6fb90c8 1630{
0dfbf9d7 1631 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1632 {
1633 case 3:
0dfbf9d7 1634 if (x->array[2] != y->array[2])
c6fb90c8 1635 return 0;
1a0670f3 1636 /* Fall through. */
c6fb90c8 1637 case 2:
0dfbf9d7 1638 if (x->array[1] != y->array[1])
c6fb90c8 1639 return 0;
1a0670f3 1640 /* Fall through. */
c6fb90c8 1641 case 1:
0dfbf9d7 1642 return x->array[0] == y->array[0];
c6fb90c8
L
1643 break;
1644 default:
1645 abort ();
1646 }
1647}
40fb9820 1648
0dfbf9d7
L
1649static INLINE int
1650cpu_flags_all_zero (const union i386_cpu_flags *x)
1651{
1652 switch (ARRAY_SIZE(x->array))
1653 {
75f8266a
KL
1654 case 5:
1655 if (x->array[4])
1656 return 0;
1657 /* Fall through. */
53467f57
IT
1658 case 4:
1659 if (x->array[3])
1660 return 0;
1661 /* Fall through. */
0dfbf9d7
L
1662 case 3:
1663 if (x->array[2])
1664 return 0;
1a0670f3 1665 /* Fall through. */
0dfbf9d7
L
1666 case 2:
1667 if (x->array[1])
1668 return 0;
1a0670f3 1669 /* Fall through. */
0dfbf9d7
L
1670 case 1:
1671 return !x->array[0];
1672 default:
1673 abort ();
1674 }
1675}
1676
0dfbf9d7
L
1677static INLINE int
1678cpu_flags_equal (const union i386_cpu_flags *x,
1679 const union i386_cpu_flags *y)
1680{
1681 switch (ARRAY_SIZE(x->array))
1682 {
75f8266a
KL
1683 case 5:
1684 if (x->array[4] != y->array[4])
1685 return 0;
1686 /* Fall through. */
53467f57
IT
1687 case 4:
1688 if (x->array[3] != y->array[3])
1689 return 0;
1690 /* Fall through. */
0dfbf9d7
L
1691 case 3:
1692 if (x->array[2] != y->array[2])
1693 return 0;
1a0670f3 1694 /* Fall through. */
0dfbf9d7
L
1695 case 2:
1696 if (x->array[1] != y->array[1])
1697 return 0;
1a0670f3 1698 /* Fall through. */
0dfbf9d7
L
1699 case 1:
1700 return x->array[0] == y->array[0];
1701 break;
1702 default:
1703 abort ();
1704 }
1705}
c6fb90c8
L
1706
1707static INLINE int
1708cpu_flags_check_cpu64 (i386_cpu_flags f)
1709{
1710 return !((flag_code == CODE_64BIT && f.bitfield.cpuno64)
1711 || (flag_code != CODE_64BIT && f.bitfield.cpu64));
40fb9820
L
1712}
1713
c6fb90c8
L
1714static INLINE i386_cpu_flags
1715cpu_flags_and (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1716{
c6fb90c8
L
1717 switch (ARRAY_SIZE (x.array))
1718 {
75f8266a
KL
1719 case 5:
1720 x.array [4] &= y.array [4];
1721 /* Fall through. */
53467f57
IT
1722 case 4:
1723 x.array [3] &= y.array [3];
1724 /* Fall through. */
c6fb90c8
L
1725 case 3:
1726 x.array [2] &= y.array [2];
1a0670f3 1727 /* Fall through. */
c6fb90c8
L
1728 case 2:
1729 x.array [1] &= y.array [1];
1a0670f3 1730 /* Fall through. */
c6fb90c8
L
1731 case 1:
1732 x.array [0] &= y.array [0];
1733 break;
1734 default:
1735 abort ();
1736 }
1737 return x;
1738}
40fb9820 1739
c6fb90c8
L
1740static INLINE i386_cpu_flags
1741cpu_flags_or (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1742{
c6fb90c8 1743 switch (ARRAY_SIZE (x.array))
40fb9820 1744 {
75f8266a
KL
1745 case 5:
1746 x.array [4] |= y.array [4];
1747 /* Fall through. */
53467f57
IT
1748 case 4:
1749 x.array [3] |= y.array [3];
1750 /* Fall through. */
c6fb90c8
L
1751 case 3:
1752 x.array [2] |= y.array [2];
1a0670f3 1753 /* Fall through. */
c6fb90c8
L
1754 case 2:
1755 x.array [1] |= y.array [1];
1a0670f3 1756 /* Fall through. */
c6fb90c8
L
1757 case 1:
1758 x.array [0] |= y.array [0];
40fb9820
L
1759 break;
1760 default:
1761 abort ();
1762 }
40fb9820
L
1763 return x;
1764}
1765
309d3373
JB
1766static INLINE i386_cpu_flags
1767cpu_flags_and_not (i386_cpu_flags x, i386_cpu_flags y)
1768{
1769 switch (ARRAY_SIZE (x.array))
1770 {
75f8266a
KL
1771 case 5:
1772 x.array [4] &= ~y.array [4];
1773 /* Fall through. */
53467f57
IT
1774 case 4:
1775 x.array [3] &= ~y.array [3];
1776 /* Fall through. */
309d3373
JB
1777 case 3:
1778 x.array [2] &= ~y.array [2];
1a0670f3 1779 /* Fall through. */
309d3373
JB
1780 case 2:
1781 x.array [1] &= ~y.array [1];
1a0670f3 1782 /* Fall through. */
309d3373
JB
1783 case 1:
1784 x.array [0] &= ~y.array [0];
1785 break;
1786 default:
1787 abort ();
1788 }
1789 return x;
1790}
1791
6c0946d0
JB
1792static const i386_cpu_flags avx512 = CPU_ANY_AVX512F_FLAGS;
1793
c0f3af97
L
1794#define CPU_FLAGS_ARCH_MATCH 0x1
1795#define CPU_FLAGS_64BIT_MATCH 0x2
1796
c0f3af97 1797#define CPU_FLAGS_PERFECT_MATCH \
db12e14e 1798 (CPU_FLAGS_ARCH_MATCH | CPU_FLAGS_64BIT_MATCH)
c0f3af97
L
1799
1800/* Return CPU flags match bits. */
3629bb00 1801
40fb9820 1802static int
d3ce72d0 1803cpu_flags_match (const insn_template *t)
40fb9820 1804{
c0f3af97
L
1805 i386_cpu_flags x = t->cpu_flags;
1806 int match = cpu_flags_check_cpu64 (x) ? CPU_FLAGS_64BIT_MATCH : 0;
40fb9820
L
1807
1808 x.bitfield.cpu64 = 0;
1809 x.bitfield.cpuno64 = 0;
1810
0dfbf9d7 1811 if (cpu_flags_all_zero (&x))
c0f3af97
L
1812 {
1813 /* This instruction is available on all archs. */
db12e14e 1814 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 1815 }
3629bb00
L
1816 else
1817 {
c0f3af97 1818 /* This instruction is available only on some archs. */
3629bb00
L
1819 i386_cpu_flags cpu = cpu_arch_flags;
1820
ab592e75
JB
1821 /* AVX512VL is no standalone feature - match it and then strip it. */
1822 if (x.bitfield.cpuavx512vl && !cpu.bitfield.cpuavx512vl)
1823 return match;
1824 x.bitfield.cpuavx512vl = 0;
1825
22c36940
JB
1826 /* AVX and AVX2 present at the same time express an operand size
1827 dependency - strip AVX2 for the purposes here. The operand size
1828 dependent check occurs in check_vecOperands(). */
1829 if (x.bitfield.cpuavx && x.bitfield.cpuavx2)
1830 x.bitfield.cpuavx2 = 0;
1831
3629bb00 1832 cpu = cpu_flags_and (x, cpu);
c0f3af97
L
1833 if (!cpu_flags_all_zero (&cpu))
1834 {
57392598 1835 if (x.bitfield.cpuavx)
a5ff0eb2 1836 {
929f69fa 1837 /* We need to check a few extra flags with AVX. */
b9d49817 1838 if (cpu.bitfield.cpuavx
40d231b4
JB
1839 && (!t->opcode_modifier.sse2avx
1840 || (sse2avx && !i.prefix[DATA_PREFIX]))
b9d49817 1841 && (!x.bitfield.cpuaes || cpu.bitfield.cpuaes)
929f69fa 1842 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
b9d49817
JB
1843 && (!x.bitfield.cpupclmul || cpu.bitfield.cpupclmul))
1844 match |= CPU_FLAGS_ARCH_MATCH;
a5ff0eb2 1845 }
929f69fa
JB
1846 else if (x.bitfield.cpuavx512f)
1847 {
1848 /* We need to check a few extra flags with AVX512F. */
1849 if (cpu.bitfield.cpuavx512f
1850 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
1851 && (!x.bitfield.cpuvaes || cpu.bitfield.cpuvaes)
1852 && (!x.bitfield.cpuvpclmulqdq || cpu.bitfield.cpuvpclmulqdq))
1853 match |= CPU_FLAGS_ARCH_MATCH;
1854 }
a5ff0eb2 1855 else
db12e14e 1856 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 1857 }
3629bb00 1858 }
c0f3af97 1859 return match;
40fb9820
L
1860}
1861
c6fb90c8
L
1862static INLINE i386_operand_type
1863operand_type_and (i386_operand_type x, i386_operand_type y)
40fb9820 1864{
bab6aec1
JB
1865 if (x.bitfield.class != y.bitfield.class)
1866 x.bitfield.class = ClassNone;
75e5731b
JB
1867 if (x.bitfield.instance != y.bitfield.instance)
1868 x.bitfield.instance = InstanceNone;
bab6aec1 1869
c6fb90c8
L
1870 switch (ARRAY_SIZE (x.array))
1871 {
1872 case 3:
1873 x.array [2] &= y.array [2];
1a0670f3 1874 /* Fall through. */
c6fb90c8
L
1875 case 2:
1876 x.array [1] &= y.array [1];
1a0670f3 1877 /* Fall through. */
c6fb90c8
L
1878 case 1:
1879 x.array [0] &= y.array [0];
1880 break;
1881 default:
1882 abort ();
1883 }
1884 return x;
40fb9820
L
1885}
1886
73053c1f
JB
1887static INLINE i386_operand_type
1888operand_type_and_not (i386_operand_type x, i386_operand_type y)
1889{
bab6aec1 1890 gas_assert (y.bitfield.class == ClassNone);
75e5731b 1891 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 1892
73053c1f
JB
1893 switch (ARRAY_SIZE (x.array))
1894 {
1895 case 3:
1896 x.array [2] &= ~y.array [2];
1897 /* Fall through. */
1898 case 2:
1899 x.array [1] &= ~y.array [1];
1900 /* Fall through. */
1901 case 1:
1902 x.array [0] &= ~y.array [0];
1903 break;
1904 default:
1905 abort ();
1906 }
1907 return x;
1908}
1909
c6fb90c8
L
1910static INLINE i386_operand_type
1911operand_type_or (i386_operand_type x, i386_operand_type y)
40fb9820 1912{
bab6aec1
JB
1913 gas_assert (x.bitfield.class == ClassNone ||
1914 y.bitfield.class == ClassNone ||
1915 x.bitfield.class == y.bitfield.class);
75e5731b
JB
1916 gas_assert (x.bitfield.instance == InstanceNone ||
1917 y.bitfield.instance == InstanceNone ||
1918 x.bitfield.instance == y.bitfield.instance);
bab6aec1 1919
c6fb90c8 1920 switch (ARRAY_SIZE (x.array))
40fb9820 1921 {
c6fb90c8
L
1922 case 3:
1923 x.array [2] |= y.array [2];
1a0670f3 1924 /* Fall through. */
c6fb90c8
L
1925 case 2:
1926 x.array [1] |= y.array [1];
1a0670f3 1927 /* Fall through. */
c6fb90c8
L
1928 case 1:
1929 x.array [0] |= y.array [0];
40fb9820
L
1930 break;
1931 default:
1932 abort ();
1933 }
c6fb90c8
L
1934 return x;
1935}
40fb9820 1936
c6fb90c8
L
1937static INLINE i386_operand_type
1938operand_type_xor (i386_operand_type x, i386_operand_type y)
1939{
bab6aec1 1940 gas_assert (y.bitfield.class == ClassNone);
75e5731b 1941 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 1942
c6fb90c8
L
1943 switch (ARRAY_SIZE (x.array))
1944 {
1945 case 3:
1946 x.array [2] ^= y.array [2];
1a0670f3 1947 /* Fall through. */
c6fb90c8
L
1948 case 2:
1949 x.array [1] ^= y.array [1];
1a0670f3 1950 /* Fall through. */
c6fb90c8
L
1951 case 1:
1952 x.array [0] ^= y.array [0];
1953 break;
1954 default:
1955 abort ();
1956 }
40fb9820
L
1957 return x;
1958}
1959
05909f23
JB
1960static const i386_operand_type anydisp = {
1961 .bitfield = { .disp8 = 1, .disp16 = 1, .disp32 = 1, .disp64 = 1 }
1962};
40fb9820
L
1963
1964enum operand_type
1965{
1966 reg,
40fb9820
L
1967 imm,
1968 disp,
1969 anymem
1970};
1971
c6fb90c8 1972static INLINE int
40fb9820
L
1973operand_type_check (i386_operand_type t, enum operand_type c)
1974{
1975 switch (c)
1976 {
1977 case reg:
bab6aec1 1978 return t.bitfield.class == Reg;
40fb9820 1979
40fb9820
L
1980 case imm:
1981 return (t.bitfield.imm8
1982 || t.bitfield.imm8s
1983 || t.bitfield.imm16
1984 || t.bitfield.imm32
1985 || t.bitfield.imm32s
1986 || t.bitfield.imm64);
1987
1988 case disp:
1989 return (t.bitfield.disp8
1990 || t.bitfield.disp16
1991 || t.bitfield.disp32
40fb9820
L
1992 || t.bitfield.disp64);
1993
1994 case anymem:
1995 return (t.bitfield.disp8
1996 || t.bitfield.disp16
1997 || t.bitfield.disp32
40fb9820
L
1998 || t.bitfield.disp64
1999 || t.bitfield.baseindex);
2000
2001 default:
2002 abort ();
2003 }
2cfe26b6
AM
2004
2005 return 0;
40fb9820
L
2006}
2007
7a54636a
L
2008/* Return 1 if there is no conflict in 8bit/16bit/32bit/64bit/80bit size
2009 between operand GIVEN and opeand WANTED for instruction template T. */
5c07affc
L
2010
2011static INLINE int
7a54636a
L
2012match_operand_size (const insn_template *t, unsigned int wanted,
2013 unsigned int given)
5c07affc 2014{
3ac21baa
JB
2015 return !((i.types[given].bitfield.byte
2016 && !t->operand_types[wanted].bitfield.byte)
2017 || (i.types[given].bitfield.word
2018 && !t->operand_types[wanted].bitfield.word)
2019 || (i.types[given].bitfield.dword
2020 && !t->operand_types[wanted].bitfield.dword)
2021 || (i.types[given].bitfield.qword
9db83a32
JB
2022 && (!t->operand_types[wanted].bitfield.qword
2023 /* Don't allow 64-bit (memory) operands outside of 64-bit
2024 mode, when they're used where a 64-bit GPR could also
2025 be used. Checking is needed for Intel Syntax only. */
2026 || (intel_syntax
2027 && flag_code != CODE_64BIT
2028 && (t->operand_types[wanted].bitfield.class == Reg
2029 || t->operand_types[wanted].bitfield.class == Accum
2030 || t->opcode_modifier.isstring))))
3ac21baa
JB
2031 || (i.types[given].bitfield.tbyte
2032 && !t->operand_types[wanted].bitfield.tbyte));
5c07affc
L
2033}
2034
dd40ce22
L
2035/* Return 1 if there is no conflict in SIMD register between operand
2036 GIVEN and opeand WANTED for instruction template T. */
1b54b8d7
JB
2037
2038static INLINE int
dd40ce22
L
2039match_simd_size (const insn_template *t, unsigned int wanted,
2040 unsigned int given)
1b54b8d7 2041{
3ac21baa
JB
2042 return !((i.types[given].bitfield.xmmword
2043 && !t->operand_types[wanted].bitfield.xmmword)
2044 || (i.types[given].bitfield.ymmword
2045 && !t->operand_types[wanted].bitfield.ymmword)
2046 || (i.types[given].bitfield.zmmword
260cd341
LC
2047 && !t->operand_types[wanted].bitfield.zmmword)
2048 || (i.types[given].bitfield.tmmword
2049 && !t->operand_types[wanted].bitfield.tmmword));
1b54b8d7
JB
2050}
2051
7a54636a
L
2052/* Return 1 if there is no conflict in any size between operand GIVEN
2053 and opeand WANTED for instruction template T. */
5c07affc
L
2054
2055static INLINE int
dd40ce22
L
2056match_mem_size (const insn_template *t, unsigned int wanted,
2057 unsigned int given)
5c07affc 2058{
7a54636a 2059 return (match_operand_size (t, wanted, given)
3ac21baa 2060 && !((i.types[given].bitfield.unspecified
5273a3cd 2061 && !i.broadcast.type
a5748e0d 2062 && !i.broadcast.bytes
3ac21baa
JB
2063 && !t->operand_types[wanted].bitfield.unspecified)
2064 || (i.types[given].bitfield.fword
2065 && !t->operand_types[wanted].bitfield.fword)
1b54b8d7
JB
2066 /* For scalar opcode templates to allow register and memory
2067 operands at the same time, some special casing is needed
d6793fa1
JB
2068 here. Also for v{,p}broadcast*, {,v}pmov{s,z}*, and
2069 down-conversion vpmov*. */
3528c362 2070 || ((t->operand_types[wanted].bitfield.class == RegSIMD
bc49bfd8
JB
2071 && t->operand_types[wanted].bitfield.byte
2072 + t->operand_types[wanted].bitfield.word
2073 + t->operand_types[wanted].bitfield.dword
2074 + t->operand_types[wanted].bitfield.qword
2075 > !!t->opcode_modifier.broadcast)
3ac21baa
JB
2076 ? (i.types[given].bitfield.xmmword
2077 || i.types[given].bitfield.ymmword
2078 || i.types[given].bitfield.zmmword)
2079 : !match_simd_size(t, wanted, given))));
5c07affc
L
2080}
2081
3ac21baa
JB
2082/* Return value has MATCH_STRAIGHT set if there is no size conflict on any
2083 operands for instruction template T, and it has MATCH_REVERSE set if there
2084 is no size conflict on any operands for the template with operands reversed
2085 (and the template allows for reversing in the first place). */
5c07affc 2086
3ac21baa
JB
2087#define MATCH_STRAIGHT 1
2088#define MATCH_REVERSE 2
2089
2090static INLINE unsigned int
d3ce72d0 2091operand_size_match (const insn_template *t)
5c07affc 2092{
3ac21baa 2093 unsigned int j, match = MATCH_STRAIGHT;
5c07affc 2094
0cfa3eb3 2095 /* Don't check non-absolute jump instructions. */
5c07affc 2096 if (t->opcode_modifier.jump
0cfa3eb3 2097 && t->opcode_modifier.jump != JUMP_ABSOLUTE)
5c07affc
L
2098 return match;
2099
2100 /* Check memory and accumulator operand size. */
2101 for (j = 0; j < i.operands; j++)
2102 {
3528c362
JB
2103 if (i.types[j].bitfield.class != Reg
2104 && i.types[j].bitfield.class != RegSIMD
255571cd 2105 && t->opcode_modifier.operandconstraint == ANY_SIZE)
5c07affc
L
2106 continue;
2107
bab6aec1 2108 if (t->operand_types[j].bitfield.class == Reg
7a54636a 2109 && !match_operand_size (t, j, j))
5c07affc
L
2110 {
2111 match = 0;
2112 break;
2113 }
2114
3528c362 2115 if (t->operand_types[j].bitfield.class == RegSIMD
3ac21baa 2116 && !match_simd_size (t, j, j))
1b54b8d7
JB
2117 {
2118 match = 0;
2119 break;
2120 }
2121
75e5731b 2122 if (t->operand_types[j].bitfield.instance == Accum
7a54636a 2123 && (!match_operand_size (t, j, j) || !match_simd_size (t, j, j)))
1b54b8d7
JB
2124 {
2125 match = 0;
2126 break;
2127 }
2128
c48dadc9 2129 if ((i.flags[j] & Operand_Mem) && !match_mem_size (t, j, j))
5c07affc
L
2130 {
2131 match = 0;
2132 break;
2133 }
2134 }
2135
3ac21baa 2136 if (!t->opcode_modifier.d)
7b94647a 2137 return match;
5c07affc
L
2138
2139 /* Check reverse. */
aa180741 2140 gas_assert (i.operands >= 2);
5c07affc 2141
f5eb1d70 2142 for (j = 0; j < i.operands; j++)
5c07affc 2143 {
f5eb1d70
JB
2144 unsigned int given = i.operands - j - 1;
2145
aa180741 2146 /* For FMA4 and XOP insns VEX.W controls just the first two
8bd915b7 2147 register operands. */
aa180741 2148 if (t->cpu_flags.bitfield.cpufma4 || t->cpu_flags.bitfield.cpuxop)
8bd915b7
JB
2149 given = j < 2 ? 1 - j : j;
2150
bab6aec1 2151 if (t->operand_types[j].bitfield.class == Reg
f5eb1d70 2152 && !match_operand_size (t, j, given))
7b94647a 2153 return match;
5c07affc 2154
3528c362 2155 if (t->operand_types[j].bitfield.class == RegSIMD
f5eb1d70 2156 && !match_simd_size (t, j, given))
7b94647a 2157 return match;
dbbc8b7e 2158
75e5731b 2159 if (t->operand_types[j].bitfield.instance == Accum
f5eb1d70
JB
2160 && (!match_operand_size (t, j, given)
2161 || !match_simd_size (t, j, given)))
7b94647a 2162 return match;
dbbc8b7e 2163
f5eb1d70 2164 if ((i.flags[given] & Operand_Mem) && !match_mem_size (t, j, given))
7b94647a 2165 return match;
5c07affc
L
2166 }
2167
3ac21baa 2168 return match | MATCH_REVERSE;
5c07affc
L
2169}
2170
c6fb90c8 2171static INLINE int
40fb9820
L
2172operand_type_match (i386_operand_type overlap,
2173 i386_operand_type given)
2174{
2175 i386_operand_type temp = overlap;
2176
7d5e4556 2177 temp.bitfield.unspecified = 0;
5c07affc
L
2178 temp.bitfield.byte = 0;
2179 temp.bitfield.word = 0;
2180 temp.bitfield.dword = 0;
2181 temp.bitfield.fword = 0;
2182 temp.bitfield.qword = 0;
2183 temp.bitfield.tbyte = 0;
2184 temp.bitfield.xmmword = 0;
c0f3af97 2185 temp.bitfield.ymmword = 0;
43234a1e 2186 temp.bitfield.zmmword = 0;
260cd341 2187 temp.bitfield.tmmword = 0;
0dfbf9d7 2188 if (operand_type_all_zero (&temp))
891edac4 2189 goto mismatch;
40fb9820 2190
6f2f06be 2191 if (given.bitfield.baseindex == overlap.bitfield.baseindex)
891edac4
L
2192 return 1;
2193
dc1e8a47 2194 mismatch:
a65babc9 2195 i.error = operand_type_mismatch;
891edac4 2196 return 0;
40fb9820
L
2197}
2198
7d5e4556 2199/* If given types g0 and g1 are registers they must be of the same type
10c17abd 2200 unless the expected operand type register overlap is null.
8ee52bcf 2201 Intel syntax sized memory operands are also checked here. */
40fb9820 2202
c6fb90c8 2203static INLINE int
dc821c5f 2204operand_type_register_match (i386_operand_type g0,
40fb9820 2205 i386_operand_type t0,
40fb9820
L
2206 i386_operand_type g1,
2207 i386_operand_type t1)
2208{
bab6aec1 2209 if (g0.bitfield.class != Reg
3528c362 2210 && g0.bitfield.class != RegSIMD
8ee52bcf
JB
2211 && (g0.bitfield.unspecified
2212 || !operand_type_check (g0, anymem)))
40fb9820
L
2213 return 1;
2214
bab6aec1 2215 if (g1.bitfield.class != Reg
3528c362 2216 && g1.bitfield.class != RegSIMD
8ee52bcf
JB
2217 && (g1.bitfield.unspecified
2218 || !operand_type_check (g1, anymem)))
40fb9820
L
2219 return 1;
2220
dc821c5f
JB
2221 if (g0.bitfield.byte == g1.bitfield.byte
2222 && g0.bitfield.word == g1.bitfield.word
2223 && g0.bitfield.dword == g1.bitfield.dword
10c17abd
JB
2224 && g0.bitfield.qword == g1.bitfield.qword
2225 && g0.bitfield.xmmword == g1.bitfield.xmmword
2226 && g0.bitfield.ymmword == g1.bitfield.ymmword
2227 && g0.bitfield.zmmword == g1.bitfield.zmmword)
40fb9820
L
2228 return 1;
2229
c4d09633
JB
2230 /* If expectations overlap in no more than a single size, all is fine. */
2231 g0 = operand_type_and (t0, t1);
2232 if (g0.bitfield.byte
2233 + g0.bitfield.word
2234 + g0.bitfield.dword
2235 + g0.bitfield.qword
2236 + g0.bitfield.xmmword
2237 + g0.bitfield.ymmword
2238 + g0.bitfield.zmmword <= 1)
891edac4
L
2239 return 1;
2240
a65babc9 2241 i.error = register_type_mismatch;
891edac4
L
2242
2243 return 0;
40fb9820
L
2244}
2245
4c692bc7
JB
2246static INLINE unsigned int
2247register_number (const reg_entry *r)
2248{
2249 unsigned int nr = r->reg_num;
2250
2251 if (r->reg_flags & RegRex)
2252 nr += 8;
2253
200cbe0f
L
2254 if (r->reg_flags & RegVRex)
2255 nr += 16;
2256
4c692bc7
JB
2257 return nr;
2258}
2259
252b5132 2260static INLINE unsigned int
40fb9820 2261mode_from_disp_size (i386_operand_type t)
252b5132 2262{
b5014f7a 2263 if (t.bitfield.disp8)
40fb9820
L
2264 return 1;
2265 else if (t.bitfield.disp16
a775efc8 2266 || t.bitfield.disp32)
40fb9820
L
2267 return 2;
2268 else
2269 return 0;
252b5132
RH
2270}
2271
2272static INLINE int
65879393 2273fits_in_signed_byte (addressT num)
252b5132 2274{
65879393 2275 return num + 0x80 <= 0xff;
47926f60 2276}
252b5132
RH
2277
2278static INLINE int
65879393 2279fits_in_unsigned_byte (addressT num)
252b5132 2280{
65879393 2281 return num <= 0xff;
47926f60 2282}
252b5132
RH
2283
2284static INLINE int
65879393 2285fits_in_unsigned_word (addressT num)
252b5132 2286{
65879393 2287 return num <= 0xffff;
47926f60 2288}
252b5132
RH
2289
2290static INLINE int
65879393 2291fits_in_signed_word (addressT num)
252b5132 2292{
65879393 2293 return num + 0x8000 <= 0xffff;
47926f60 2294}
2a962e6d 2295
3e73aa7c 2296static INLINE int
65879393 2297fits_in_signed_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2298{
2299#ifndef BFD64
2300 return 1;
2301#else
65879393 2302 return num + 0x80000000 <= 0xffffffff;
3e73aa7c
JH
2303#endif
2304} /* fits_in_signed_long() */
2a962e6d 2305
3e73aa7c 2306static INLINE int
65879393 2307fits_in_unsigned_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2308{
2309#ifndef BFD64
2310 return 1;
2311#else
65879393 2312 return num <= 0xffffffff;
3e73aa7c
JH
2313#endif
2314} /* fits_in_unsigned_long() */
252b5132 2315
a442cac5
JB
2316static INLINE valueT extend_to_32bit_address (addressT num)
2317{
2318#ifdef BFD64
2319 if (fits_in_unsigned_long(num))
2320 return (num ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
2321
2322 if (!fits_in_signed_long (num))
2323 return num & 0xffffffff;
2324#endif
2325
2326 return num;
2327}
2328
43234a1e 2329static INLINE int
b5014f7a 2330fits_in_disp8 (offsetT num)
43234a1e
L
2331{
2332 int shift = i.memshift;
2333 unsigned int mask;
2334
2335 if (shift == -1)
2336 abort ();
2337
2338 mask = (1 << shift) - 1;
2339
2340 /* Return 0 if NUM isn't properly aligned. */
2341 if ((num & mask))
2342 return 0;
2343
2344 /* Check if NUM will fit in 8bit after shift. */
2345 return fits_in_signed_byte (num >> shift);
2346}
2347
a683cc34
SP
2348static INLINE int
2349fits_in_imm4 (offsetT num)
2350{
2351 return (num & 0xf) == num;
2352}
2353
40fb9820 2354static i386_operand_type
e3bb37b5 2355smallest_imm_type (offsetT num)
252b5132 2356{
40fb9820 2357 i386_operand_type t;
7ab9ffdd 2358
0dfbf9d7 2359 operand_type_set (&t, 0);
40fb9820
L
2360 t.bitfield.imm64 = 1;
2361
2362 if (cpu_arch_tune != PROCESSOR_I486 && num == 1)
e413e4e9
AM
2363 {
2364 /* This code is disabled on the 486 because all the Imm1 forms
2365 in the opcode table are slower on the i486. They're the
2366 versions with the implicitly specified single-position
2367 displacement, which has another syntax if you really want to
2368 use that form. */
40fb9820
L
2369 t.bitfield.imm1 = 1;
2370 t.bitfield.imm8 = 1;
2371 t.bitfield.imm8s = 1;
2372 t.bitfield.imm16 = 1;
2373 t.bitfield.imm32 = 1;
2374 t.bitfield.imm32s = 1;
2375 }
2376 else if (fits_in_signed_byte (num))
2377 {
c34d1cc9
JB
2378 if (fits_in_unsigned_byte (num))
2379 t.bitfield.imm8 = 1;
40fb9820
L
2380 t.bitfield.imm8s = 1;
2381 t.bitfield.imm16 = 1;
2382 t.bitfield.imm32 = 1;
2383 t.bitfield.imm32s = 1;
2384 }
2385 else if (fits_in_unsigned_byte (num))
2386 {
2387 t.bitfield.imm8 = 1;
2388 t.bitfield.imm16 = 1;
2389 t.bitfield.imm32 = 1;
2390 t.bitfield.imm32s = 1;
2391 }
2392 else if (fits_in_signed_word (num) || fits_in_unsigned_word (num))
2393 {
2394 t.bitfield.imm16 = 1;
2395 t.bitfield.imm32 = 1;
2396 t.bitfield.imm32s = 1;
2397 }
2398 else if (fits_in_signed_long (num))
2399 {
2400 t.bitfield.imm32 = 1;
2401 t.bitfield.imm32s = 1;
2402 }
2403 else if (fits_in_unsigned_long (num))
2404 t.bitfield.imm32 = 1;
2405
2406 return t;
47926f60 2407}
252b5132 2408
847f7ad4 2409static offsetT
e3bb37b5 2410offset_in_range (offsetT val, int size)
847f7ad4 2411{
508866be 2412 addressT mask;
ba2adb93 2413
847f7ad4
AM
2414 switch (size)
2415 {
508866be
L
2416 case 1: mask = ((addressT) 1 << 8) - 1; break;
2417 case 2: mask = ((addressT) 1 << 16) - 1; break;
3e73aa7c 2418#ifdef BFD64
64965897 2419 case 4: mask = ((addressT) 1 << 32) - 1; break;
3e73aa7c 2420#endif
64965897 2421 case sizeof (val): return val;
47926f60 2422 default: abort ();
847f7ad4
AM
2423 }
2424
4fe51f7d 2425 if ((val & ~mask) != 0 && (-val & ~mask) != 0)
f493c217
AM
2426 as_warn (_("0x%" PRIx64 " shortened to 0x%" PRIx64),
2427 (uint64_t) val, (uint64_t) (val & mask));
847f7ad4 2428
847f7ad4
AM
2429 return val & mask;
2430}
2431
76d3f746
JB
2432static INLINE const char *insn_name (const insn_template *t)
2433{
5c139202 2434 return &i386_mnemonics[t->mnem_off];
76d3f746
JB
2435}
2436
c32fa91d
L
2437enum PREFIX_GROUP
2438{
2439 PREFIX_EXIST = 0,
2440 PREFIX_LOCK,
2441 PREFIX_REP,
04ef582a 2442 PREFIX_DS,
c32fa91d
L
2443 PREFIX_OTHER
2444};
2445
2446/* Returns
2447 a. PREFIX_EXIST if attempting to add a prefix where one from the
2448 same class already exists.
2449 b. PREFIX_LOCK if lock prefix is added.
2450 c. PREFIX_REP if rep/repne prefix is added.
04ef582a
L
2451 d. PREFIX_DS if ds prefix is added.
2452 e. PREFIX_OTHER if other prefix is added.
c32fa91d
L
2453 */
2454
2455static enum PREFIX_GROUP
e3bb37b5 2456add_prefix (unsigned int prefix)
252b5132 2457{
c32fa91d 2458 enum PREFIX_GROUP ret = PREFIX_OTHER;
b1905489 2459 unsigned int q;
252b5132 2460
29b0f896
AM
2461 if (prefix >= REX_OPCODE && prefix < REX_OPCODE + 16
2462 && flag_code == CODE_64BIT)
b1905489 2463 {
161a04f6 2464 if ((i.prefix[REX_PREFIX] & prefix & REX_W)
44846f29
JB
2465 || (i.prefix[REX_PREFIX] & prefix & REX_R)
2466 || (i.prefix[REX_PREFIX] & prefix & REX_X)
2467 || (i.prefix[REX_PREFIX] & prefix & REX_B))
c32fa91d 2468 ret = PREFIX_EXIST;
b1905489
JB
2469 q = REX_PREFIX;
2470 }
3e73aa7c 2471 else
b1905489
JB
2472 {
2473 switch (prefix)
2474 {
2475 default:
2476 abort ();
2477
b1905489 2478 case DS_PREFIX_OPCODE:
04ef582a
L
2479 ret = PREFIX_DS;
2480 /* Fall through. */
2481 case CS_PREFIX_OPCODE:
b1905489
JB
2482 case ES_PREFIX_OPCODE:
2483 case FS_PREFIX_OPCODE:
2484 case GS_PREFIX_OPCODE:
2485 case SS_PREFIX_OPCODE:
2486 q = SEG_PREFIX;
2487 break;
2488
2489 case REPNE_PREFIX_OPCODE:
2490 case REPE_PREFIX_OPCODE:
c32fa91d
L
2491 q = REP_PREFIX;
2492 ret = PREFIX_REP;
2493 break;
2494
b1905489 2495 case LOCK_PREFIX_OPCODE:
c32fa91d
L
2496 q = LOCK_PREFIX;
2497 ret = PREFIX_LOCK;
b1905489
JB
2498 break;
2499
2500 case FWAIT_OPCODE:
2501 q = WAIT_PREFIX;
2502 break;
2503
2504 case ADDR_PREFIX_OPCODE:
2505 q = ADDR_PREFIX;
2506 break;
2507
2508 case DATA_PREFIX_OPCODE:
2509 q = DATA_PREFIX;
2510 break;
2511 }
2512 if (i.prefix[q] != 0)
c32fa91d 2513 ret = PREFIX_EXIST;
b1905489 2514 }
252b5132 2515
b1905489 2516 if (ret)
252b5132 2517 {
b1905489
JB
2518 if (!i.prefix[q])
2519 ++i.prefixes;
2520 i.prefix[q] |= prefix;
252b5132 2521 }
b1905489
JB
2522 else
2523 as_bad (_("same type of prefix used twice"));
252b5132 2524
252b5132
RH
2525 return ret;
2526}
2527
2528static void
78f12dd3 2529update_code_flag (int value, int check)
eecb386c 2530{
78f12dd3
L
2531 PRINTF_LIKE ((*as_error));
2532
1e9cc1c2 2533 flag_code = (enum flag_code) value;
40fb9820
L
2534 if (flag_code == CODE_64BIT)
2535 {
2536 cpu_arch_flags.bitfield.cpu64 = 1;
2537 cpu_arch_flags.bitfield.cpuno64 = 0;
40fb9820
L
2538 }
2539 else
2540 {
2541 cpu_arch_flags.bitfield.cpu64 = 0;
2542 cpu_arch_flags.bitfield.cpuno64 = 1;
40fb9820
L
2543 }
2544 if (value == CODE_64BIT && !cpu_arch_flags.bitfield.cpulm )
3e73aa7c 2545 {
78f12dd3
L
2546 if (check)
2547 as_error = as_fatal;
2548 else
2549 as_error = as_bad;
2550 (*as_error) (_("64bit mode not supported on `%s'."),
2551 cpu_arch_name ? cpu_arch_name : default_arch);
3e73aa7c 2552 }
40fb9820 2553 if (value == CODE_32BIT && !cpu_arch_flags.bitfield.cpui386)
3e73aa7c 2554 {
78f12dd3
L
2555 if (check)
2556 as_error = as_fatal;
2557 else
2558 as_error = as_bad;
2559 (*as_error) (_("32bit mode not supported on `%s'."),
2560 cpu_arch_name ? cpu_arch_name : default_arch);
3e73aa7c 2561 }
eecb386c
AM
2562 stackop_size = '\0';
2563}
2564
78f12dd3
L
2565static void
2566set_code_flag (int value)
2567{
2568 update_code_flag (value, 0);
2569}
2570
eecb386c 2571static void
e3bb37b5 2572set_16bit_gcc_code_flag (int new_code_flag)
252b5132 2573{
1e9cc1c2 2574 flag_code = (enum flag_code) new_code_flag;
40fb9820
L
2575 if (flag_code != CODE_16BIT)
2576 abort ();
2577 cpu_arch_flags.bitfield.cpu64 = 0;
2578 cpu_arch_flags.bitfield.cpuno64 = 1;
9306ca4a 2579 stackop_size = LONG_MNEM_SUFFIX;
252b5132
RH
2580}
2581
2582static void
e3bb37b5 2583set_intel_syntax (int syntax_flag)
252b5132
RH
2584{
2585 /* Find out if register prefixing is specified. */
2586 int ask_naked_reg = 0;
2587
2588 SKIP_WHITESPACE ();
29b0f896 2589 if (!is_end_of_line[(unsigned char) *input_line_pointer])
252b5132 2590 {
d02603dc
NC
2591 char *string;
2592 int e = get_symbol_name (&string);
252b5132 2593
47926f60 2594 if (strcmp (string, "prefix") == 0)
252b5132 2595 ask_naked_reg = 1;
47926f60 2596 else if (strcmp (string, "noprefix") == 0)
252b5132
RH
2597 ask_naked_reg = -1;
2598 else
d0b47220 2599 as_bad (_("bad argument to syntax directive."));
d02603dc 2600 (void) restore_line_pointer (e);
252b5132
RH
2601 }
2602 demand_empty_rest_of_line ();
c3332e24 2603
252b5132
RH
2604 intel_syntax = syntax_flag;
2605
2606 if (ask_naked_reg == 0)
f86103b7
AM
2607 allow_naked_reg = (intel_syntax
2608 && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
252b5132
RH
2609 else
2610 allow_naked_reg = (ask_naked_reg < 0);
9306ca4a 2611
ee86248c 2612 expr_set_rank (O_full_ptr, syntax_flag ? 10 : 0);
7ab9ffdd 2613
e4a3b5a4 2614 register_prefix = allow_naked_reg ? "" : "%";
252b5132
RH
2615}
2616
1efbbeb4
L
2617static void
2618set_intel_mnemonic (int mnemonic_flag)
2619{
e1d4d893 2620 intel_mnemonic = mnemonic_flag;
1efbbeb4
L
2621}
2622
db51cc60
L
2623static void
2624set_allow_index_reg (int flag)
2625{
2626 allow_index_reg = flag;
2627}
2628
cb19c032 2629static void
7bab8ab5 2630set_check (int what)
cb19c032 2631{
7bab8ab5
JB
2632 enum check_kind *kind;
2633 const char *str;
2634
2635 if (what)
2636 {
2637 kind = &operand_check;
2638 str = "operand";
2639 }
2640 else
2641 {
2642 kind = &sse_check;
2643 str = "sse";
2644 }
2645
cb19c032
L
2646 SKIP_WHITESPACE ();
2647
2648 if (!is_end_of_line[(unsigned char) *input_line_pointer])
2649 {
d02603dc
NC
2650 char *string;
2651 int e = get_symbol_name (&string);
cb19c032
L
2652
2653 if (strcmp (string, "none") == 0)
7bab8ab5 2654 *kind = check_none;
cb19c032 2655 else if (strcmp (string, "warning") == 0)
7bab8ab5 2656 *kind = check_warning;
cb19c032 2657 else if (strcmp (string, "error") == 0)
7bab8ab5 2658 *kind = check_error;
cb19c032 2659 else
7bab8ab5 2660 as_bad (_("bad argument to %s_check directive."), str);
d02603dc 2661 (void) restore_line_pointer (e);
cb19c032
L
2662 }
2663 else
7bab8ab5 2664 as_bad (_("missing argument for %s_check directive"), str);
cb19c032
L
2665
2666 demand_empty_rest_of_line ();
2667}
2668
8a9036a4
L
2669static void
2670check_cpu_arch_compatible (const char *name ATTRIBUTE_UNUSED,
1e9cc1c2 2671 i386_cpu_flags new_flag ATTRIBUTE_UNUSED)
8a9036a4
L
2672{
2673#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2674 static const char *arch;
2675
c085ab00 2676 /* Intel MCU is only supported on ELF. */
8a9036a4
L
2677 if (!IS_ELF)
2678 return;
2679
2680 if (!arch)
2681 {
2682 /* Use cpu_arch_name if it is set in md_parse_option. Otherwise
2683 use default_arch. */
2684 arch = cpu_arch_name;
2685 if (!arch)
2686 arch = default_arch;
2687 }
2688
81486035 2689 /* If we are targeting Intel MCU, we must enable it. */
648d04db
JB
2690 if ((get_elf_backend_data (stdoutput)->elf_machine_code == EM_IAMCU)
2691 == new_flag.bitfield.cpuiamcu)
81486035
L
2692 return;
2693
8a9036a4
L
2694 as_bad (_("`%s' is not supported on `%s'"), name, arch);
2695#endif
2696}
2697
8180707f
JB
2698static void
2699extend_cpu_sub_arch_name (const char *name)
2700{
2701 if (cpu_sub_arch_name)
2702 cpu_sub_arch_name = reconcat (cpu_sub_arch_name, cpu_sub_arch_name,
ae89daec 2703 ".", name, (const char *) NULL);
8180707f 2704 else
ae89daec 2705 cpu_sub_arch_name = concat (".", name, (const char *) NULL);
8180707f
JB
2706}
2707
e413e4e9 2708static void
e3bb37b5 2709set_cpu_arch (int dummy ATTRIBUTE_UNUSED)
e413e4e9 2710{
f68697e8
JB
2711 typedef struct arch_stack_entry
2712 {
2713 const struct arch_stack_entry *prev;
2714 const char *name;
2715 char *sub_name;
2716 i386_cpu_flags flags;
2717 i386_cpu_flags isa_flags;
2718 enum processor_type isa;
2719 enum flag_code flag_code;
2720 char stackop_size;
2721 bool no_cond_jump_promotion;
2722 } arch_stack_entry;
2723 static const arch_stack_entry *arch_stack_top;
2724
47926f60 2725 SKIP_WHITESPACE ();
e413e4e9 2726
29b0f896 2727 if (!is_end_of_line[(unsigned char) *input_line_pointer])
e413e4e9 2728 {
3ce2ebcf
JB
2729 char *s;
2730 int e = get_symbol_name (&s);
2731 const char *string = s;
2732 unsigned int j = 0;
40fb9820 2733 i386_cpu_flags flags;
e413e4e9 2734
3ce2ebcf
JB
2735 if (strcmp (string, "default") == 0)
2736 {
2737 if (strcmp (default_arch, "iamcu") == 0)
2738 string = default_arch;
2739 else
2740 {
2741 static const i386_cpu_flags cpu_unknown_flags = CPU_UNKNOWN_FLAGS;
2742
2743 cpu_arch_name = NULL;
2744 free (cpu_sub_arch_name);
2745 cpu_sub_arch_name = NULL;
2746 cpu_arch_flags = cpu_unknown_flags;
2747 if (flag_code == CODE_64BIT)
2748 {
2749 cpu_arch_flags.bitfield.cpu64 = 1;
2750 cpu_arch_flags.bitfield.cpuno64 = 0;
2751 }
2752 else
2753 {
2754 cpu_arch_flags.bitfield.cpu64 = 0;
2755 cpu_arch_flags.bitfield.cpuno64 = 1;
2756 }
2757 cpu_arch_isa = PROCESSOR_UNKNOWN;
ae89daec 2758 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
3ce2ebcf
JB
2759 if (!cpu_arch_tune_set)
2760 {
2761 cpu_arch_tune = cpu_arch_isa;
2762 cpu_arch_tune_flags = cpu_arch_isa_flags;
2763 }
2764
2765 j = ARRAY_SIZE (cpu_arch) + 1;
2766 }
2767 }
f68697e8
JB
2768 else if (strcmp (string, "push") == 0)
2769 {
2770 arch_stack_entry *top = XNEW (arch_stack_entry);
2771
2772 top->name = cpu_arch_name;
2773 if (cpu_sub_arch_name)
2774 top->sub_name = xstrdup (cpu_sub_arch_name);
2775 else
2776 top->sub_name = NULL;
2777 top->flags = cpu_arch_flags;
2778 top->isa = cpu_arch_isa;
2779 top->isa_flags = cpu_arch_isa_flags;
2780 top->flag_code = flag_code;
2781 top->stackop_size = stackop_size;
2782 top->no_cond_jump_promotion = no_cond_jump_promotion;
2783
2784 top->prev = arch_stack_top;
2785 arch_stack_top = top;
2786
2787 (void) restore_line_pointer (e);
2788 demand_empty_rest_of_line ();
2789 return;
2790 }
2791 else if (strcmp (string, "pop") == 0)
2792 {
2793 const arch_stack_entry *top = arch_stack_top;
2794
2795 if (!top)
2796 as_bad (_(".arch stack is empty"));
2797 else if (top->flag_code != flag_code
2798 || top->stackop_size != stackop_size)
2799 {
2800 static const unsigned int bits[] = {
2801 [CODE_16BIT] = 16,
2802 [CODE_32BIT] = 32,
2803 [CODE_64BIT] = 64,
2804 };
2805
2806 as_bad (_("this `.arch pop' requires `.code%u%s' to be in effect"),
2807 bits[top->flag_code],
2808 top->stackop_size == LONG_MNEM_SUFFIX ? "gcc" : "");
2809 }
2810 else
2811 {
2812 arch_stack_top = top->prev;
2813
2814 cpu_arch_name = top->name;
2815 free (cpu_sub_arch_name);
2816 cpu_sub_arch_name = top->sub_name;
2817 cpu_arch_flags = top->flags;
2818 cpu_arch_isa = top->isa;
2819 cpu_arch_isa_flags = top->isa_flags;
2820 no_cond_jump_promotion = top->no_cond_jump_promotion;
2821
2822 XDELETE (top);
2823 }
2824
2825 (void) restore_line_pointer (e);
2826 demand_empty_rest_of_line ();
2827 return;
2828 }
3ce2ebcf
JB
2829
2830 for (; j < ARRAY_SIZE (cpu_arch); j++)
e413e4e9 2831 {
ae89daec
JB
2832 if (strcmp (string + (*string == '.'), cpu_arch[j].name) == 0
2833 && (*string == '.') == (cpu_arch[j].type == PROCESSOR_NONE))
e413e4e9 2834 {
5c6af06e
JB
2835 if (*string != '.')
2836 {
ae89daec 2837 check_cpu_arch_compatible (string, cpu_arch[j].enable);
648d04db 2838
91d6fa6a 2839 cpu_arch_name = cpu_arch[j].name;
d92c7521 2840 free (cpu_sub_arch_name);
5c6af06e 2841 cpu_sub_arch_name = NULL;
ae89daec 2842 cpu_arch_flags = cpu_arch[j].enable;
40fb9820
L
2843 if (flag_code == CODE_64BIT)
2844 {
2845 cpu_arch_flags.bitfield.cpu64 = 1;
2846 cpu_arch_flags.bitfield.cpuno64 = 0;
2847 }
2848 else
2849 {
2850 cpu_arch_flags.bitfield.cpu64 = 0;
2851 cpu_arch_flags.bitfield.cpuno64 = 1;
2852 }
91d6fa6a 2853 cpu_arch_isa = cpu_arch[j].type;
ae89daec 2854 cpu_arch_isa_flags = cpu_arch[j].enable;
ccc9c027
L
2855 if (!cpu_arch_tune_set)
2856 {
2857 cpu_arch_tune = cpu_arch_isa;
2858 cpu_arch_tune_flags = cpu_arch_isa_flags;
2859 }
d59a54c2 2860 pre_386_16bit_warned = false;
5c6af06e
JB
2861 break;
2862 }
40fb9820 2863
ae89daec
JB
2864 if (cpu_flags_all_zero (&cpu_arch[j].enable))
2865 continue;
2866
293f5f65 2867 flags = cpu_flags_or (cpu_arch_flags,
ae89daec 2868 cpu_arch[j].enable);
81486035 2869
5b64d091 2870 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
5c6af06e 2871 {
ae89daec 2872 extend_cpu_sub_arch_name (string + 1);
40fb9820 2873 cpu_arch_flags = flags;
a586129e 2874 cpu_arch_isa_flags = flags;
5c6af06e 2875 }
0089dace
L
2876 else
2877 cpu_arch_isa_flags
2878 = cpu_flags_or (cpu_arch_isa_flags,
ae89daec 2879 cpu_arch[j].enable);
d02603dc 2880 (void) restore_line_pointer (e);
5c6af06e
JB
2881 demand_empty_rest_of_line ();
2882 return;
e413e4e9
AM
2883 }
2884 }
293f5f65 2885
ae89daec 2886 if (startswith (string, ".no") && j >= ARRAY_SIZE (cpu_arch))
293f5f65 2887 {
33eaf5de 2888 /* Disable an ISA extension. */
ae89daec
JB
2889 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
2890 if (cpu_arch[j].type == PROCESSOR_NONE
2891 && strcmp (string + 3, cpu_arch[j].name) == 0)
293f5f65
L
2892 {
2893 flags = cpu_flags_and_not (cpu_arch_flags,
ae89daec 2894 cpu_arch[j].disable);
293f5f65
L
2895 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
2896 {
ae89daec 2897 extend_cpu_sub_arch_name (string + 1);
293f5f65
L
2898 cpu_arch_flags = flags;
2899 cpu_arch_isa_flags = flags;
2900 }
2901 (void) restore_line_pointer (e);
2902 demand_empty_rest_of_line ();
2903 return;
2904 }
293f5f65
L
2905 }
2906
3ce2ebcf 2907 if (j == ARRAY_SIZE (cpu_arch))
e413e4e9
AM
2908 as_bad (_("no such architecture: `%s'"), string);
2909
2910 *input_line_pointer = e;
2911 }
2912 else
2913 as_bad (_("missing cpu architecture"));
2914
fddf5b5b
AM
2915 no_cond_jump_promotion = 0;
2916 if (*input_line_pointer == ','
29b0f896 2917 && !is_end_of_line[(unsigned char) input_line_pointer[1]])
fddf5b5b 2918 {
d02603dc
NC
2919 char *string;
2920 char e;
2921
2922 ++input_line_pointer;
2923 e = get_symbol_name (&string);
fddf5b5b
AM
2924
2925 if (strcmp (string, "nojumps") == 0)
2926 no_cond_jump_promotion = 1;
2927 else if (strcmp (string, "jumps") == 0)
2928 ;
2929 else
2930 as_bad (_("no such architecture modifier: `%s'"), string);
2931
d02603dc 2932 (void) restore_line_pointer (e);
fddf5b5b
AM
2933 }
2934
e413e4e9
AM
2935 demand_empty_rest_of_line ();
2936}
2937
8a9036a4
L
2938enum bfd_architecture
2939i386_arch (void)
2940{
c085ab00 2941 if (cpu_arch_isa == PROCESSOR_IAMCU)
81486035
L
2942 {
2943 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2944 || flag_code == CODE_64BIT)
2945 as_fatal (_("Intel MCU is 32bit ELF only"));
2946 return bfd_arch_iamcu;
2947 }
8a9036a4
L
2948 else
2949 return bfd_arch_i386;
2950}
2951
b9d79e03 2952unsigned long
7016a5d5 2953i386_mach (void)
b9d79e03 2954{
d34049e8 2955 if (startswith (default_arch, "x86_64"))
8a9036a4 2956 {
c085ab00 2957 if (default_arch[6] == '\0')
8a9036a4 2958 return bfd_mach_x86_64;
351f65ca
L
2959 else
2960 return bfd_mach_x64_32;
8a9036a4 2961 }
5197d474
L
2962 else if (!strcmp (default_arch, "i386")
2963 || !strcmp (default_arch, "iamcu"))
81486035
L
2964 {
2965 if (cpu_arch_isa == PROCESSOR_IAMCU)
2966 {
2967 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
2968 as_fatal (_("Intel MCU is 32bit ELF only"));
2969 return bfd_mach_i386_iamcu;
2970 }
2971 else
2972 return bfd_mach_i386_i386;
2973 }
b9d79e03 2974 else
2b5d6a91 2975 as_fatal (_("unknown architecture"));
b9d79e03 2976}
b9d79e03 2977\f
99f0fb12
JB
2978#include "opcodes/i386-tbl.h"
2979
252b5132 2980void
7016a5d5 2981md_begin (void)
252b5132 2982{
86fa6981
L
2983 /* Support pseudo prefixes like {disp32}. */
2984 lex_type ['{'] = LEX_BEGIN_NAME;
2985
47926f60 2986 /* Initialize op_hash hash table. */
629310ab 2987 op_hash = str_htab_create ();
252b5132
RH
2988
2989 {
65f440c8
JB
2990 const insn_template *const *sets = i386_op_sets;
2991 const insn_template *const *end = sets + ARRAY_SIZE (i386_op_sets) - 1;
2992
2993 /* Type checks to compensate for the conversion through void * which
2994 occurs during hash table insertion / lookup. */
eb993861
JB
2995 (void) sizeof (sets == &current_templates->start);
2996 (void) sizeof (end == &current_templates->end);
65f440c8 2997 for (; sets < end; ++sets)
76d3f746
JB
2998 if (str_hash_insert (op_hash, insn_name (*sets), sets, 0))
2999 as_fatal (_("duplicate %s"), insn_name (*sets));
252b5132
RH
3000 }
3001
47926f60 3002 /* Initialize reg_hash hash table. */
629310ab 3003 reg_hash = str_htab_create ();
252b5132 3004 {
29b0f896 3005 const reg_entry *regtab;
c3fe08fa 3006 unsigned int regtab_size = i386_regtab_size;
252b5132 3007
c3fe08fa 3008 for (regtab = i386_regtab; regtab_size--; regtab++)
6225c532 3009 {
6288d05f
JB
3010 switch (regtab->reg_type.bitfield.class)
3011 {
3012 case Reg:
34684862
JB
3013 if (regtab->reg_type.bitfield.dword)
3014 {
3015 if (regtab->reg_type.bitfield.instance == Accum)
3016 reg_eax = regtab;
3017 }
3018 else if (regtab->reg_type.bitfield.tbyte)
6288d05f
JB
3019 {
3020 /* There's no point inserting st(<N>) in the hash table, as
3021 parentheses aren't included in register_chars[] anyway. */
3022 if (regtab->reg_type.bitfield.instance != Accum)
3023 continue;
3024 reg_st0 = regtab;
3025 }
3026 break;
3027
5e042380
JB
3028 case SReg:
3029 switch (regtab->reg_num)
3030 {
3031 case 0: reg_es = regtab; break;
3032 case 2: reg_ss = regtab; break;
3033 case 3: reg_ds = regtab; break;
3034 }
3035 break;
3036
6288d05f
JB
3037 case RegMask:
3038 if (!regtab->reg_num)
3039 reg_k0 = regtab;
3040 break;
3041 }
3042
6225c532
JB
3043 if (str_hash_insert (reg_hash, regtab->reg_name, regtab, 0) != NULL)
3044 as_fatal (_("duplicate %s"), regtab->reg_name);
6225c532 3045 }
252b5132
RH
3046 }
3047
47926f60 3048 /* Fill in lexical tables: mnemonic_chars, operand_chars. */
252b5132 3049 {
29b0f896
AM
3050 int c;
3051 char *p;
252b5132
RH
3052
3053 for (c = 0; c < 256; c++)
3054 {
014fbcda 3055 if (ISDIGIT (c) || ISLOWER (c))
252b5132
RH
3056 {
3057 mnemonic_chars[c] = c;
3058 register_chars[c] = c;
3059 operand_chars[c] = c;
3060 }
3882b010 3061 else if (ISUPPER (c))
252b5132 3062 {
3882b010 3063 mnemonic_chars[c] = TOLOWER (c);
252b5132
RH
3064 register_chars[c] = mnemonic_chars[c];
3065 operand_chars[c] = c;
3066 }
43234a1e 3067 else if (c == '{' || c == '}')
86fa6981
L
3068 {
3069 mnemonic_chars[c] = c;
3070 operand_chars[c] = c;
3071 }
b3983e5f
JB
3072#ifdef SVR4_COMMENT_CHARS
3073 else if (c == '\\' && strchr (i386_comment_chars, '/'))
3074 operand_chars[c] = c;
3075#endif
252b5132 3076
4795cd4a
JB
3077 if (c >= 128)
3078 operand_chars[c] = c;
252b5132
RH
3079 }
3080
32137342 3081#ifdef LEX_QM
32137342 3082 operand_chars['?'] = '?';
252b5132 3083#endif
c0f3af97 3084 mnemonic_chars['_'] = '_';
791fe849 3085 mnemonic_chars['-'] = '-';
0003779b 3086 mnemonic_chars['.'] = '.';
252b5132
RH
3087
3088 for (p = operand_special_chars; *p != '\0'; p++)
3089 operand_chars[(unsigned char) *p] = *p;
3090 }
3091
a4447b93
RH
3092 if (flag_code == CODE_64BIT)
3093 {
ca19b261
KT
3094#if defined (OBJ_COFF) && defined (TE_PE)
3095 x86_dwarf2_return_column = (OUTPUT_FLAVOR == bfd_target_coff_flavour
3096 ? 32 : 16);
3097#else
a4447b93 3098 x86_dwarf2_return_column = 16;
ca19b261 3099#endif
61ff971f 3100 x86_cie_data_alignment = -8;
b52c4ee4
IB
3101#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3102 x86_sframe_cfa_sp_reg = 7;
3103 x86_sframe_cfa_fp_reg = 6;
3104#endif
a4447b93
RH
3105 }
3106 else
3107 {
3108 x86_dwarf2_return_column = 8;
3109 x86_cie_data_alignment = -4;
3110 }
e379e5f3
L
3111
3112 /* NB: FUSED_JCC_PADDING frag must have sufficient room so that it
3113 can be turned into BRANCH_PREFIX frag. */
3114 if (align_branch_prefix_size > MAX_FUSED_JCC_PADDING_SIZE)
3115 abort ();
252b5132
RH
3116}
3117
3118void
e3bb37b5 3119i386_print_statistics (FILE *file)
252b5132 3120{
629310ab
ML
3121 htab_print_statistics (file, "i386 opcode", op_hash);
3122 htab_print_statistics (file, "i386 register", reg_hash);
252b5132 3123}
654d6f31
AM
3124
3125void
3126i386_md_end (void)
3127{
3128 htab_delete (op_hash);
3129 htab_delete (reg_hash);
3130}
252b5132 3131\f
252b5132
RH
3132#ifdef DEBUG386
3133
ce8a8b2f 3134/* Debugging routines for md_assemble. */
d3ce72d0 3135static void pte (insn_template *);
40fb9820 3136static void pt (i386_operand_type);
e3bb37b5
L
3137static void pe (expressionS *);
3138static void ps (symbolS *);
252b5132
RH
3139
3140static void
2c703856 3141pi (const char *line, i386_insn *x)
252b5132 3142{
09137c09 3143 unsigned int j;
252b5132
RH
3144
3145 fprintf (stdout, "%s: template ", line);
3146 pte (&x->tm);
09f131f2
JH
3147 fprintf (stdout, " address: base %s index %s scale %x\n",
3148 x->base_reg ? x->base_reg->reg_name : "none",
3149 x->index_reg ? x->index_reg->reg_name : "none",
3150 x->log2_scale_factor);
3151 fprintf (stdout, " modrm: mode %x reg %x reg/mem %x\n",
252b5132 3152 x->rm.mode, x->rm.reg, x->rm.regmem);
09f131f2
JH
3153 fprintf (stdout, " sib: base %x index %x scale %x\n",
3154 x->sib.base, x->sib.index, x->sib.scale);
3155 fprintf (stdout, " rex: 64bit %x extX %x extY %x extZ %x\n",
161a04f6
L
3156 (x->rex & REX_W) != 0,
3157 (x->rex & REX_R) != 0,
3158 (x->rex & REX_X) != 0,
3159 (x->rex & REX_B) != 0);
09137c09 3160 for (j = 0; j < x->operands; j++)
252b5132 3161 {
09137c09
SP
3162 fprintf (stdout, " #%d: ", j + 1);
3163 pt (x->types[j]);
252b5132 3164 fprintf (stdout, "\n");
bab6aec1 3165 if (x->types[j].bitfield.class == Reg
3528c362
JB
3166 || x->types[j].bitfield.class == RegMMX
3167 || x->types[j].bitfield.class == RegSIMD
dd6b8a0b 3168 || x->types[j].bitfield.class == RegMask
00cee14f 3169 || x->types[j].bitfield.class == SReg
4a5c67ed
JB
3170 || x->types[j].bitfield.class == RegCR
3171 || x->types[j].bitfield.class == RegDR
dd6b8a0b
JB
3172 || x->types[j].bitfield.class == RegTR
3173 || x->types[j].bitfield.class == RegBND)
09137c09
SP
3174 fprintf (stdout, "%s\n", x->op[j].regs->reg_name);
3175 if (operand_type_check (x->types[j], imm))
3176 pe (x->op[j].imms);
3177 if (operand_type_check (x->types[j], disp))
3178 pe (x->op[j].disps);
252b5132
RH
3179 }
3180}
3181
3182static void
d3ce72d0 3183pte (insn_template *t)
252b5132 3184{
b933fa4b 3185 static const unsigned char opc_pfx[] = { 0, 0x66, 0xf3, 0xf2 };
441f6aca 3186 static const char *const opc_spc[] = {
0cc78721 3187 NULL, "0f", "0f38", "0f3a", NULL, "evexmap5", "evexmap6", NULL,
441f6aca
JB
3188 "XOP08", "XOP09", "XOP0A",
3189 };
09137c09 3190 unsigned int j;
441f6aca 3191
252b5132 3192 fprintf (stdout, " %d operands ", t->operands);
441f6aca
JB
3193 if (opc_pfx[t->opcode_modifier.opcodeprefix])
3194 fprintf (stdout, "pfx %x ", opc_pfx[t->opcode_modifier.opcodeprefix]);
ddb62495
JB
3195 if (opc_spc[t->opcode_space])
3196 fprintf (stdout, "space %s ", opc_spc[t->opcode_space]);
47926f60 3197 fprintf (stdout, "opcode %x ", t->base_opcode);
252b5132
RH
3198 if (t->extension_opcode != None)
3199 fprintf (stdout, "ext %x ", t->extension_opcode);
40fb9820 3200 if (t->opcode_modifier.d)
252b5132 3201 fprintf (stdout, "D");
40fb9820 3202 if (t->opcode_modifier.w)
252b5132
RH
3203 fprintf (stdout, "W");
3204 fprintf (stdout, "\n");
09137c09 3205 for (j = 0; j < t->operands; j++)
252b5132 3206 {
09137c09
SP
3207 fprintf (stdout, " #%d type ", j + 1);
3208 pt (t->operand_types[j]);
252b5132
RH
3209 fprintf (stdout, "\n");
3210 }
3211}
3212
3213static void
e3bb37b5 3214pe (expressionS *e)
252b5132 3215{
24eab124 3216 fprintf (stdout, " operation %d\n", e->X_op);
b8281767
AM
3217 fprintf (stdout, " add_number %" PRId64 " (%" PRIx64 ")\n",
3218 (int64_t) e->X_add_number, (uint64_t) (valueT) e->X_add_number);
252b5132
RH
3219 if (e->X_add_symbol)
3220 {
3221 fprintf (stdout, " add_symbol ");
3222 ps (e->X_add_symbol);
3223 fprintf (stdout, "\n");
3224 }
3225 if (e->X_op_symbol)
3226 {
3227 fprintf (stdout, " op_symbol ");
3228 ps (e->X_op_symbol);
3229 fprintf (stdout, "\n");
3230 }
3231}
3232
3233static void
e3bb37b5 3234ps (symbolS *s)
252b5132
RH
3235{
3236 fprintf (stdout, "%s type %s%s",
3237 S_GET_NAME (s),
3238 S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
3239 segment_name (S_GET_SEGMENT (s)));
3240}
3241
7b81dfbb 3242static struct type_name
252b5132 3243 {
40fb9820
L
3244 i386_operand_type mask;
3245 const char *name;
252b5132 3246 }
7b81dfbb 3247const type_names[] =
252b5132 3248{
05909f23
JB
3249 { { .bitfield = { .class = Reg, .byte = 1 } }, "r8" },
3250 { { .bitfield = { .class = Reg, .word = 1 } }, "r16" },
3251 { { .bitfield = { .class = Reg, .dword = 1 } }, "r32" },
3252 { { .bitfield = { .class = Reg, .qword = 1 } }, "r64" },
3253 { { .bitfield = { .instance = Accum, .byte = 1 } }, "acc8" },
3254 { { .bitfield = { .instance = Accum, .word = 1 } }, "acc16" },
3255 { { .bitfield = { .instance = Accum, .dword = 1 } }, "acc32" },
3256 { { .bitfield = { .instance = Accum, .qword = 1 } }, "acc64" },
3257 { { .bitfield = { .imm8 = 1 } }, "i8" },
3258 { { .bitfield = { .imm8s = 1 } }, "i8s" },
3259 { { .bitfield = { .imm16 = 1 } }, "i16" },
3260 { { .bitfield = { .imm32 = 1 } }, "i32" },
3261 { { .bitfield = { .imm32s = 1 } }, "i32s" },
3262 { { .bitfield = { .imm64 = 1 } }, "i64" },
3263 { { .bitfield = { .imm1 = 1 } }, "i1" },
3264 { { .bitfield = { .baseindex = 1 } }, "BaseIndex" },
3265 { { .bitfield = { .disp8 = 1 } }, "d8" },
3266 { { .bitfield = { .disp16 = 1 } }, "d16" },
3267 { { .bitfield = { .disp32 = 1 } }, "d32" },
3268 { { .bitfield = { .disp64 = 1 } }, "d64" },
3269 { { .bitfield = { .instance = RegD, .word = 1 } }, "InOutPortReg" },
3270 { { .bitfield = { .instance = RegC, .byte = 1 } }, "ShiftCount" },
3271 { { .bitfield = { .class = RegCR } }, "control reg" },
3272 { { .bitfield = { .class = RegTR } }, "test reg" },
3273 { { .bitfield = { .class = RegDR } }, "debug reg" },
3274 { { .bitfield = { .class = Reg, .tbyte = 1 } }, "FReg" },
3275 { { .bitfield = { .instance = Accum, .tbyte = 1 } }, "FAcc" },
3276 { { .bitfield = { .class = SReg } }, "SReg" },
3277 { { .bitfield = { .class = RegMMX } }, "rMMX" },
3278 { { .bitfield = { .class = RegSIMD, .xmmword = 1 } }, "rXMM" },
3279 { { .bitfield = { .class = RegSIMD, .ymmword = 1 } }, "rYMM" },
3280 { { .bitfield = { .class = RegSIMD, .zmmword = 1 } }, "rZMM" },
3281 { { .bitfield = { .class = RegSIMD, .tmmword = 1 } }, "rTMM" },
3282 { { .bitfield = { .class = RegMask } }, "Mask reg" },
252b5132
RH
3283};
3284
3285static void
40fb9820 3286pt (i386_operand_type t)
252b5132 3287{
40fb9820 3288 unsigned int j;
c6fb90c8 3289 i386_operand_type a;
252b5132 3290
40fb9820 3291 for (j = 0; j < ARRAY_SIZE (type_names); j++)
c6fb90c8
L
3292 {
3293 a = operand_type_and (t, type_names[j].mask);
2c703856 3294 if (operand_type_equal (&a, &type_names[j].mask))
c6fb90c8
L
3295 fprintf (stdout, "%s, ", type_names[j].name);
3296 }
252b5132
RH
3297 fflush (stdout);
3298}
3299
3300#endif /* DEBUG386 */
3301\f
252b5132 3302static bfd_reloc_code_real_type
3956db08 3303reloc (unsigned int size,
64e74474
AM
3304 int pcrel,
3305 int sign,
3306 bfd_reloc_code_real_type other)
252b5132 3307{
47926f60 3308 if (other != NO_RELOC)
3956db08 3309 {
91d6fa6a 3310 reloc_howto_type *rel;
3956db08
JB
3311
3312 if (size == 8)
3313 switch (other)
3314 {
64e74474
AM
3315 case BFD_RELOC_X86_64_GOT32:
3316 return BFD_RELOC_X86_64_GOT64;
3317 break;
553d1284
L
3318 case BFD_RELOC_X86_64_GOTPLT64:
3319 return BFD_RELOC_X86_64_GOTPLT64;
3320 break;
64e74474
AM
3321 case BFD_RELOC_X86_64_PLTOFF64:
3322 return BFD_RELOC_X86_64_PLTOFF64;
3323 break;
3324 case BFD_RELOC_X86_64_GOTPC32:
3325 other = BFD_RELOC_X86_64_GOTPC64;
3326 break;
3327 case BFD_RELOC_X86_64_GOTPCREL:
3328 other = BFD_RELOC_X86_64_GOTPCREL64;
3329 break;
3330 case BFD_RELOC_X86_64_TPOFF32:
3331 other = BFD_RELOC_X86_64_TPOFF64;
3332 break;
3333 case BFD_RELOC_X86_64_DTPOFF32:
3334 other = BFD_RELOC_X86_64_DTPOFF64;
3335 break;
3336 default:
3337 break;
3956db08 3338 }
e05278af 3339
8ce3d284 3340#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
3341 if (other == BFD_RELOC_SIZE32)
3342 {
3343 if (size == 8)
1ab668bf 3344 other = BFD_RELOC_SIZE64;
8fd4256d 3345 if (pcrel)
1ab668bf
AM
3346 {
3347 as_bad (_("there are no pc-relative size relocations"));
3348 return NO_RELOC;
3349 }
8fd4256d 3350 }
8ce3d284 3351#endif
8fd4256d 3352
e05278af 3353 /* Sign-checking 4-byte relocations in 16-/32-bit code is pointless. */
f2d8a97c 3354 if (size == 4 && (flag_code != CODE_64BIT || disallow_64bit_reloc))
e05278af
JB
3355 sign = -1;
3356
91d6fa6a
NC
3357 rel = bfd_reloc_type_lookup (stdoutput, other);
3358 if (!rel)
3956db08 3359 as_bad (_("unknown relocation (%u)"), other);
91d6fa6a 3360 else if (size != bfd_get_reloc_size (rel))
3956db08 3361 as_bad (_("%u-byte relocation cannot be applied to %u-byte field"),
91d6fa6a 3362 bfd_get_reloc_size (rel),
3956db08 3363 size);
91d6fa6a 3364 else if (pcrel && !rel->pc_relative)
3956db08 3365 as_bad (_("non-pc-relative relocation for pc-relative field"));
91d6fa6a 3366 else if ((rel->complain_on_overflow == complain_overflow_signed
3956db08 3367 && !sign)
91d6fa6a 3368 || (rel->complain_on_overflow == complain_overflow_unsigned
64e74474 3369 && sign > 0))
3956db08
JB
3370 as_bad (_("relocated field and relocation type differ in signedness"));
3371 else
3372 return other;
3373 return NO_RELOC;
3374 }
252b5132
RH
3375
3376 if (pcrel)
3377 {
3e73aa7c 3378 if (!sign)
3956db08 3379 as_bad (_("there are no unsigned pc-relative relocations"));
252b5132
RH
3380 switch (size)
3381 {
3382 case 1: return BFD_RELOC_8_PCREL;
3383 case 2: return BFD_RELOC_16_PCREL;
d258b828 3384 case 4: return BFD_RELOC_32_PCREL;
d6ab8113 3385 case 8: return BFD_RELOC_64_PCREL;
252b5132 3386 }
3956db08 3387 as_bad (_("cannot do %u byte pc-relative relocation"), size);
252b5132
RH
3388 }
3389 else
3390 {
3956db08 3391 if (sign > 0)
e5cb08ac 3392 switch (size)
3e73aa7c
JH
3393 {
3394 case 4: return BFD_RELOC_X86_64_32S;
3395 }
3396 else
3397 switch (size)
3398 {
3399 case 1: return BFD_RELOC_8;
3400 case 2: return BFD_RELOC_16;
3401 case 4: return BFD_RELOC_32;
3402 case 8: return BFD_RELOC_64;
3403 }
3956db08
JB
3404 as_bad (_("cannot do %s %u byte relocation"),
3405 sign > 0 ? "signed" : "unsigned", size);
252b5132
RH
3406 }
3407
0cc9e1d3 3408 return NO_RELOC;
252b5132
RH
3409}
3410
47926f60
KH
3411/* Here we decide which fixups can be adjusted to make them relative to
3412 the beginning of the section instead of the symbol. Basically we need
3413 to make sure that the dynamic relocations are done correctly, so in
3414 some cases we force the original symbol to be used. */
3415
252b5132 3416int
e3bb37b5 3417tc_i386_fix_adjustable (fixS *fixP ATTRIBUTE_UNUSED)
252b5132 3418{
6d249963 3419#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 3420 if (!IS_ELF)
31312f95
AM
3421 return 1;
3422
a161fe53
AM
3423 /* Don't adjust pc-relative references to merge sections in 64-bit
3424 mode. */
3425 if (use_rela_relocations
3426 && (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0
3427 && fixP->fx_pcrel)
252b5132 3428 return 0;
31312f95 3429
8d01d9a9
AJ
3430 /* The x86_64 GOTPCREL are represented as 32bit PCrel relocations
3431 and changed later by validate_fix. */
3432 if (GOT_symbol && fixP->fx_subsy == GOT_symbol
3433 && fixP->fx_r_type == BFD_RELOC_32_PCREL)
3434 return 0;
3435
8fd4256d
L
3436 /* Adjust_reloc_syms doesn't know about the GOT. Need to keep symbol
3437 for size relocations. */
3438 if (fixP->fx_r_type == BFD_RELOC_SIZE32
3439 || fixP->fx_r_type == BFD_RELOC_SIZE64
3440 || fixP->fx_r_type == BFD_RELOC_386_GOTOFF
252b5132 3441 || fixP->fx_r_type == BFD_RELOC_386_GOT32
02a86693 3442 || fixP->fx_r_type == BFD_RELOC_386_GOT32X
13ae64f3
JJ
3443 || fixP->fx_r_type == BFD_RELOC_386_TLS_GD
3444 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDM
3445 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDO_32
3446 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE_32
37e55690
JJ
3447 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE
3448 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTIE
13ae64f3
JJ
3449 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE_32
3450 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE
67a4f2b7
AO
3451 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTDESC
3452 || fixP->fx_r_type == BFD_RELOC_386_TLS_DESC_CALL
3e73aa7c 3453 || fixP->fx_r_type == BFD_RELOC_X86_64_GOT32
80b3ee89 3454 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCREL
56ceb5b5
L
3455 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCRELX
3456 || fixP->fx_r_type == BFD_RELOC_X86_64_REX_GOTPCRELX
bffbf940
JJ
3457 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSGD
3458 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSLD
3459 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF32
d6ab8113 3460 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF64
bffbf940
JJ
3461 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF
3462 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF32
d6ab8113
JB
3463 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF64
3464 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTOFF64
67a4f2b7
AO
3465 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
3466 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSDESC_CALL
252b5132
RH
3467 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3468 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3469 return 0;
31312f95 3470#endif
252b5132
RH
3471 return 1;
3472}
252b5132 3473
a9aabc23
JB
3474static INLINE bool
3475want_disp32 (const insn_template *t)
3476{
3477 return flag_code != CODE_64BIT
3478 || i.prefix[ADDR_PREFIX]
7fc69528 3479 || (t->mnem_off == MN_lea
fe134c65
JB
3480 && (!i.types[1].bitfield.qword
3481 || t->opcode_modifier.size == SIZE32));
a9aabc23
JB
3482}
3483
b4cac588 3484static int
e3bb37b5 3485intel_float_operand (const char *mnemonic)
252b5132 3486{
9306ca4a
JB
3487 /* Note that the value returned is meaningful only for opcodes with (memory)
3488 operands, hence the code here is free to improperly handle opcodes that
3489 have no operands (for better performance and smaller code). */
3490
3491 if (mnemonic[0] != 'f')
3492 return 0; /* non-math */
3493
3494 switch (mnemonic[1])
3495 {
3496 /* fclex, fdecstp, fdisi, femms, feni, fincstp, finit, fsetpm, and
3497 the fs segment override prefix not currently handled because no
3498 call path can make opcodes without operands get here */
3499 case 'i':
3500 return 2 /* integer op */;
3501 case 'l':
3502 if (mnemonic[2] == 'd' && (mnemonic[3] == 'c' || mnemonic[3] == 'e'))
3503 return 3; /* fldcw/fldenv */
3504 break;
3505 case 'n':
3506 if (mnemonic[2] != 'o' /* fnop */)
3507 return 3; /* non-waiting control op */
3508 break;
3509 case 'r':
3510 if (mnemonic[2] == 's')
3511 return 3; /* frstor/frstpm */
3512 break;
3513 case 's':
3514 if (mnemonic[2] == 'a')
3515 return 3; /* fsave */
3516 if (mnemonic[2] == 't')
3517 {
3518 switch (mnemonic[3])
3519 {
3520 case 'c': /* fstcw */
3521 case 'd': /* fstdw */
3522 case 'e': /* fstenv */
3523 case 's': /* fsts[gw] */
3524 return 3;
3525 }
3526 }
3527 break;
3528 case 'x':
3529 if (mnemonic[2] == 'r' || mnemonic[2] == 's')
3530 return 0; /* fxsave/fxrstor are not really math ops */
3531 break;
3532 }
252b5132 3533
9306ca4a 3534 return 1;
252b5132
RH
3535}
3536
9a182d04
JB
3537static INLINE void
3538install_template (const insn_template *t)
3539{
3540 unsigned int l;
3541
3542 i.tm = *t;
3543
3544 /* Note that for pseudo prefixes this produces a length of 1. But for them
3545 the length isn't interesting at all. */
3546 for (l = 1; l < 4; ++l)
3547 if (!(t->base_opcode >> (8 * l)))
3548 break;
3549
3550 i.opcode_length = l;
3551}
3552
c0f3af97
L
3553/* Build the VEX prefix. */
3554
3555static void
d3ce72d0 3556build_vex_prefix (const insn_template *t)
c0f3af97
L
3557{
3558 unsigned int register_specifier;
c0f3af97 3559 unsigned int vector_length;
03751133 3560 unsigned int w;
c0f3af97
L
3561
3562 /* Check register specifier. */
3563 if (i.vex.register_specifier)
43234a1e
L
3564 {
3565 register_specifier =
3566 ~register_number (i.vex.register_specifier) & 0xf;
3567 gas_assert ((i.vex.register_specifier->reg_flags & RegVRex) == 0);
3568 }
c0f3af97
L
3569 else
3570 register_specifier = 0xf;
3571
79f0fa25
L
3572 /* Use 2-byte VEX prefix by swapping destination and source operand
3573 if there are more than 1 register operand. */
3574 if (i.reg_operands > 1
3575 && i.vec_encoding != vex_encoding_vex3
86fa6981 3576 && i.dir_encoding == dir_encoding_default
fa99fab2 3577 && i.operands == i.reg_operands
dbbc8b7e 3578 && operand_type_equal (&i.types[0], &i.types[i.operands - 1])
ddb62495 3579 && i.tm.opcode_space == SPACE_0F
dbbc8b7e 3580 && (i.tm.opcode_modifier.load || i.tm.opcode_modifier.d)
fa99fab2
L
3581 && i.rex == REX_B)
3582 {
67748abf 3583 unsigned int xchg;
fa99fab2 3584
67748abf 3585 swap_2_operands (0, i.operands - 1);
fa99fab2 3586
9c2799c2 3587 gas_assert (i.rm.mode == 3);
fa99fab2
L
3588
3589 i.rex = REX_R;
3590 xchg = i.rm.regmem;
3591 i.rm.regmem = i.rm.reg;
3592 i.rm.reg = xchg;
3593
dbbc8b7e
JB
3594 if (i.tm.opcode_modifier.d)
3595 i.tm.base_opcode ^= (i.tm.base_opcode & 0xee) != 0x6e
2c735193 3596 ? Opcode_ExtD : Opcode_SIMD_IntD;
dbbc8b7e 3597 else /* Use the next insn. */
9a182d04 3598 install_template (&t[1]);
fa99fab2
L
3599 }
3600
79dec6b7
JB
3601 /* Use 2-byte VEX prefix by swapping commutative source operands if there
3602 are no memory operands and at least 3 register ones. */
3603 if (i.reg_operands >= 3
3604 && i.vec_encoding != vex_encoding_vex3
3605 && i.reg_operands == i.operands - i.imm_operands
3606 && i.tm.opcode_modifier.vex
3607 && i.tm.opcode_modifier.commutative
33740f21
JB
3608 && (i.tm.opcode_modifier.sse2avx
3609 || (optimize > 1 && !i.no_optimize))
79dec6b7
JB
3610 && i.rex == REX_B
3611 && i.vex.register_specifier
3612 && !(i.vex.register_specifier->reg_flags & RegRex))
3613 {
3614 unsigned int xchg = i.operands - i.reg_operands;
79dec6b7 3615
ddb62495 3616 gas_assert (i.tm.opcode_space == SPACE_0F);
79dec6b7
JB
3617 gas_assert (!i.tm.opcode_modifier.sae);
3618 gas_assert (operand_type_equal (&i.types[i.operands - 2],
3619 &i.types[i.operands - 3]));
3620 gas_assert (i.rm.mode == 3);
3621
67748abf 3622 swap_2_operands (xchg, xchg + 1);
79dec6b7
JB
3623
3624 i.rex = 0;
3625 xchg = i.rm.regmem | 8;
3626 i.rm.regmem = ~register_specifier & 0xf;
3627 gas_assert (!(i.rm.regmem & 8));
3628 i.vex.register_specifier += xchg - i.rm.regmem;
3629 register_specifier = ~xchg & 0xf;
3630 }
3631
539f890d
L
3632 if (i.tm.opcode_modifier.vex == VEXScalar)
3633 vector_length = avxscalar;
10c17abd
JB
3634 else if (i.tm.opcode_modifier.vex == VEX256)
3635 vector_length = 1;
539f890d 3636 else
10c17abd 3637 {
56522fc5 3638 unsigned int op;
10c17abd 3639
c7213af9
L
3640 /* Determine vector length from the last multi-length vector
3641 operand. */
10c17abd 3642 vector_length = 0;
56522fc5 3643 for (op = t->operands; op--;)
10c17abd
JB
3644 if (t->operand_types[op].bitfield.xmmword
3645 && t->operand_types[op].bitfield.ymmword
3646 && i.types[op].bitfield.ymmword)
3647 {
3648 vector_length = 1;
3649 break;
3650 }
3651 }
c0f3af97 3652
03751133
L
3653 /* Check the REX.W bit and VEXW. */
3654 if (i.tm.opcode_modifier.vexw == VEXWIG)
3655 w = (vexwig == vexw1 || (i.rex & REX_W)) ? 1 : 0;
3656 else if (i.tm.opcode_modifier.vexw)
3657 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3658 else
931d03b7 3659 w = (flag_code == CODE_64BIT ? i.rex & REX_W : vexwig == vexw1) ? 1 : 0;
03751133 3660
c0f3af97 3661 /* Use 2-byte VEX prefix if possible. */
03751133
L
3662 if (w == 0
3663 && i.vec_encoding != vex_encoding_vex3
ddb62495 3664 && i.tm.opcode_space == SPACE_0F
c0f3af97
L
3665 && (i.rex & (REX_W | REX_X | REX_B)) == 0)
3666 {
3667 /* 2-byte VEX prefix. */
3668 unsigned int r;
3669
3670 i.vex.length = 2;
3671 i.vex.bytes[0] = 0xc5;
3672
3673 /* Check the REX.R bit. */
3674 r = (i.rex & REX_R) ? 0 : 1;
3675 i.vex.bytes[1] = (r << 7
3676 | register_specifier << 3
3677 | vector_length << 2
35648716 3678 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3679 }
3680 else
3681 {
3682 /* 3-byte VEX prefix. */
f88c9eb0 3683 i.vex.length = 3;
f88c9eb0 3684
ddb62495 3685 switch (i.tm.opcode_space)
5dd85c99 3686 {
441f6aca
JB
3687 case SPACE_0F:
3688 case SPACE_0F38:
3689 case SPACE_0F3A:
80de6e00 3690 i.vex.bytes[0] = 0xc4;
7f399153 3691 break;
441f6aca
JB
3692 case SPACE_XOP08:
3693 case SPACE_XOP09:
3694 case SPACE_XOP0A:
f88c9eb0 3695 i.vex.bytes[0] = 0x8f;
7f399153
L
3696 break;
3697 default:
3698 abort ();
f88c9eb0 3699 }
c0f3af97 3700
c0f3af97
L
3701 /* The high 3 bits of the second VEX byte are 1's compliment
3702 of RXB bits from REX. */
ddb62495 3703 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | i.tm.opcode_space;
c0f3af97 3704
c0f3af97
L
3705 i.vex.bytes[2] = (w << 7
3706 | register_specifier << 3
3707 | vector_length << 2
35648716 3708 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3709 }
3710}
3711
5b7c81bd 3712static INLINE bool
e771e7c9
JB
3713is_evex_encoding (const insn_template *t)
3714{
7091c612 3715 return t->opcode_modifier.evex || t->opcode_modifier.disp8memshift
e771e7c9 3716 || t->opcode_modifier.broadcast || t->opcode_modifier.masking
a80195f1 3717 || t->opcode_modifier.sae;
e771e7c9
JB
3718}
3719
5b7c81bd 3720static INLINE bool
7a8655d2
JB
3721is_any_vex_encoding (const insn_template *t)
3722{
7b47a312 3723 return t->opcode_modifier.vex || is_evex_encoding (t);
7a8655d2
JB
3724}
3725
a5748e0d
JB
3726static unsigned int
3727get_broadcast_bytes (const insn_template *t, bool diag)
3728{
3729 unsigned int op, bytes;
3730 const i386_operand_type *types;
3731
3732 if (i.broadcast.type)
9b345ce8 3733 return (1 << (t->opcode_modifier.broadcast - 1)) * i.broadcast.type;
a5748e0d
JB
3734
3735 gas_assert (intel_syntax);
3736
3737 for (op = 0; op < t->operands; ++op)
3738 if (t->operand_types[op].bitfield.baseindex)
3739 break;
3740
3741 gas_assert (op < t->operands);
3742
3743 if (t->opcode_modifier.evex
3744 && t->opcode_modifier.evex != EVEXDYN)
3745 switch (i.broadcast.bytes)
3746 {
3747 case 1:
3748 if (t->operand_types[op].bitfield.word)
3749 return 2;
3750 /* Fall through. */
3751 case 2:
3752 if (t->operand_types[op].bitfield.dword)
3753 return 4;
3754 /* Fall through. */
3755 case 4:
3756 if (t->operand_types[op].bitfield.qword)
3757 return 8;
3758 /* Fall through. */
3759 case 8:
3760 if (t->operand_types[op].bitfield.xmmword)
3761 return 16;
3762 if (t->operand_types[op].bitfield.ymmword)
3763 return 32;
3764 if (t->operand_types[op].bitfield.zmmword)
3765 return 64;
3766 /* Fall through. */
3767 default:
3768 abort ();
3769 }
3770
3771 gas_assert (op + 1 < t->operands);
3772
3773 if (t->operand_types[op + 1].bitfield.xmmword
3774 + t->operand_types[op + 1].bitfield.ymmword
3775 + t->operand_types[op + 1].bitfield.zmmword > 1)
3776 {
3777 types = &i.types[op + 1];
3778 diag = false;
3779 }
3780 else /* Ambiguous - guess with a preference to non-AVX512VL forms. */
3781 types = &t->operand_types[op];
3782
3783 if (types->bitfield.zmmword)
3784 bytes = 64;
3785 else if (types->bitfield.ymmword)
3786 bytes = 32;
3787 else
3788 bytes = 16;
3789
3790 if (diag)
3791 as_warn (_("ambiguous broadcast for `%s', using %u-bit form"),
76d3f746 3792 insn_name (t), bytes * 8);
a5748e0d
JB
3793
3794 return bytes;
3795}
3796
43234a1e
L
3797/* Build the EVEX prefix. */
3798
3799static void
3800build_evex_prefix (void)
3801{
35648716 3802 unsigned int register_specifier, w;
43234a1e
L
3803 rex_byte vrex_used = 0;
3804
3805 /* Check register specifier. */
3806 if (i.vex.register_specifier)
3807 {
3808 gas_assert ((i.vrex & REX_X) == 0);
3809
3810 register_specifier = i.vex.register_specifier->reg_num;
3811 if ((i.vex.register_specifier->reg_flags & RegRex))
3812 register_specifier += 8;
3813 /* The upper 16 registers are encoded in the fourth byte of the
3814 EVEX prefix. */
3815 if (!(i.vex.register_specifier->reg_flags & RegVRex))
3816 i.vex.bytes[3] = 0x8;
3817 register_specifier = ~register_specifier & 0xf;
3818 }
3819 else
3820 {
3821 register_specifier = 0xf;
3822
3823 /* Encode upper 16 vector index register in the fourth byte of
3824 the EVEX prefix. */
3825 if (!(i.vrex & REX_X))
3826 i.vex.bytes[3] = 0x8;
3827 else
3828 vrex_used |= REX_X;
3829 }
3830
43234a1e
L
3831 /* 4 byte EVEX prefix. */
3832 i.vex.length = 4;
3833 i.vex.bytes[0] = 0x62;
3834
43234a1e
L
3835 /* The high 3 bits of the second EVEX byte are 1's compliment of RXB
3836 bits from REX. */
ddb62495
JB
3837 gas_assert (i.tm.opcode_space >= SPACE_0F);
3838 gas_assert (i.tm.opcode_space <= SPACE_EVEXMAP6);
3839 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | i.tm.opcode_space;
43234a1e
L
3840
3841 /* The fifth bit of the second EVEX byte is 1's compliment of the
3842 REX_R bit in VREX. */
3843 if (!(i.vrex & REX_R))
3844 i.vex.bytes[1] |= 0x10;
3845 else
3846 vrex_used |= REX_R;
3847
3848 if ((i.reg_operands + i.imm_operands) == i.operands)
3849 {
3850 /* When all operands are registers, the REX_X bit in REX is not
3851 used. We reuse it to encode the upper 16 registers, which is
3852 indicated by the REX_B bit in VREX. The REX_X bit is encoded
3853 as 1's compliment. */
3854 if ((i.vrex & REX_B))
3855 {
3856 vrex_used |= REX_B;
3857 i.vex.bytes[1] &= ~0x40;
3858 }
3859 }
3860
3861 /* EVEX instructions shouldn't need the REX prefix. */
3862 i.vrex &= ~vrex_used;
3863 gas_assert (i.vrex == 0);
3864
6865c043
L
3865 /* Check the REX.W bit and VEXW. */
3866 if (i.tm.opcode_modifier.vexw == VEXWIG)
3867 w = (evexwig == evexw1 || (i.rex & REX_W)) ? 1 : 0;
3868 else if (i.tm.opcode_modifier.vexw)
3869 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3870 else
931d03b7 3871 w = (flag_code == CODE_64BIT ? i.rex & REX_W : evexwig == evexw1) ? 1 : 0;
43234a1e 3872
43234a1e 3873 /* The third byte of the EVEX prefix. */
35648716
JB
3874 i.vex.bytes[2] = ((w << 7)
3875 | (register_specifier << 3)
3876 | 4 /* Encode the U bit. */
3877 | i.tm.opcode_modifier.opcodeprefix);
43234a1e
L
3878
3879 /* The fourth byte of the EVEX prefix. */
3880 /* The zeroing-masking bit. */
6225c532 3881 if (i.mask.reg && i.mask.zeroing)
43234a1e
L
3882 i.vex.bytes[3] |= 0x80;
3883
3884 /* Don't always set the broadcast bit if there is no RC. */
ca5312a2 3885 if (i.rounding.type == rc_none)
43234a1e
L
3886 {
3887 /* Encode the vector length. */
3888 unsigned int vec_length;
3889
e771e7c9
JB
3890 if (!i.tm.opcode_modifier.evex
3891 || i.tm.opcode_modifier.evex == EVEXDYN)
3892 {
56522fc5 3893 unsigned int op;
e771e7c9 3894
c7213af9
L
3895 /* Determine vector length from the last multi-length vector
3896 operand. */
56522fc5 3897 for (op = i.operands; op--;)
e771e7c9
JB
3898 if (i.tm.operand_types[op].bitfield.xmmword
3899 + i.tm.operand_types[op].bitfield.ymmword
3900 + i.tm.operand_types[op].bitfield.zmmword > 1)
3901 {
3902 if (i.types[op].bitfield.zmmword)
c7213af9
L
3903 {
3904 i.tm.opcode_modifier.evex = EVEX512;
3905 break;
3906 }
e771e7c9 3907 else if (i.types[op].bitfield.ymmword)
c7213af9
L
3908 {
3909 i.tm.opcode_modifier.evex = EVEX256;
3910 break;
3911 }
e771e7c9 3912 else if (i.types[op].bitfield.xmmword)
c7213af9
L
3913 {
3914 i.tm.opcode_modifier.evex = EVEX128;
3915 break;
3916 }
9b345ce8
JB
3917 else if ((i.broadcast.type || i.broadcast.bytes)
3918 && op == i.broadcast.operand)
625cbd7a 3919 {
a5748e0d 3920 switch (get_broadcast_bytes (&i.tm, true))
625cbd7a
JB
3921 {
3922 case 64:
3923 i.tm.opcode_modifier.evex = EVEX512;
3924 break;
3925 case 32:
3926 i.tm.opcode_modifier.evex = EVEX256;
3927 break;
3928 case 16:
3929 i.tm.opcode_modifier.evex = EVEX128;
3930 break;
3931 default:
c7213af9 3932 abort ();
625cbd7a 3933 }
c7213af9 3934 break;
625cbd7a 3935 }
e771e7c9 3936 }
c7213af9 3937
56522fc5 3938 if (op >= MAX_OPERANDS)
c7213af9 3939 abort ();
e771e7c9
JB
3940 }
3941
43234a1e
L
3942 switch (i.tm.opcode_modifier.evex)
3943 {
3944 case EVEXLIG: /* LL' is ignored */
3945 vec_length = evexlig << 5;
3946 break;
3947 case EVEX128:
3948 vec_length = 0 << 5;
3949 break;
3950 case EVEX256:
3951 vec_length = 1 << 5;
3952 break;
3953 case EVEX512:
3954 vec_length = 2 << 5;
3955 break;
3956 default:
3957 abort ();
3958 break;
3959 }
3960 i.vex.bytes[3] |= vec_length;
3961 /* Encode the broadcast bit. */
9b345ce8 3962 if (i.broadcast.type || i.broadcast.bytes)
43234a1e
L
3963 i.vex.bytes[3] |= 0x10;
3964 }
ca5312a2
JB
3965 else if (i.rounding.type != saeonly)
3966 i.vex.bytes[3] |= 0x10 | (i.rounding.type << 5);
43234a1e 3967 else
ca5312a2 3968 i.vex.bytes[3] |= 0x10 | (evexrcig << 5);
43234a1e 3969
6225c532
JB
3970 if (i.mask.reg)
3971 i.vex.bytes[3] |= i.mask.reg->reg_num;
43234a1e
L
3972}
3973
65da13b5
L
3974static void
3975process_immext (void)
3976{
3977 expressionS *exp;
3978
c0f3af97 3979 /* These AMD 3DNow! and SSE2 instructions have an opcode suffix
65da13b5
L
3980 which is coded in the same place as an 8-bit immediate field
3981 would be. Here we fake an 8-bit immediate operand from the
3982 opcode suffix stored in tm.extension_opcode.
3983
c1e679ec 3984 AVX instructions also use this encoding, for some of
c0f3af97 3985 3 argument instructions. */
65da13b5 3986
43234a1e 3987 gas_assert (i.imm_operands <= 1
7ab9ffdd 3988 && (i.operands <= 2
7a8655d2 3989 || (is_any_vex_encoding (&i.tm)
7ab9ffdd 3990 && i.operands <= 4)));
65da13b5
L
3991
3992 exp = &im_expressions[i.imm_operands++];
3993 i.op[i.operands].imms = exp;
be1643ff 3994 i.types[i.operands].bitfield.imm8 = 1;
65da13b5
L
3995 i.operands++;
3996 exp->X_op = O_constant;
3997 exp->X_add_number = i.tm.extension_opcode;
3998 i.tm.extension_opcode = None;
3999}
4000
42164a71
L
4001
4002static int
4003check_hle (void)
4004{
742732c7 4005 switch (i.tm.opcode_modifier.prefixok)
42164a71
L
4006 {
4007 default:
4008 abort ();
742732c7
JB
4009 case PrefixLock:
4010 case PrefixNone:
4011 case PrefixNoTrack:
4012 case PrefixRep:
165de32a 4013 as_bad (_("invalid instruction `%s' after `%s'"),
76d3f746 4014 insn_name (&i.tm), i.hle_prefix);
42164a71 4015 return 0;
742732c7 4016 case PrefixHLELock:
42164a71
L
4017 if (i.prefix[LOCK_PREFIX])
4018 return 1;
165de32a 4019 as_bad (_("missing `lock' with `%s'"), i.hle_prefix);
42164a71 4020 return 0;
742732c7 4021 case PrefixHLEAny:
42164a71 4022 return 1;
742732c7 4023 case PrefixHLERelease:
42164a71
L
4024 if (i.prefix[HLE_PREFIX] != XRELEASE_PREFIX_OPCODE)
4025 {
4026 as_bad (_("instruction `%s' after `xacquire' not allowed"),
76d3f746 4027 insn_name (&i.tm));
42164a71
L
4028 return 0;
4029 }
8dc0818e 4030 if (i.mem_operands == 0 || !(i.flags[i.operands - 1] & Operand_Mem))
42164a71
L
4031 {
4032 as_bad (_("memory destination needed for instruction `%s'"
76d3f746 4033 " after `xrelease'"), insn_name (&i.tm));
42164a71
L
4034 return 0;
4035 }
4036 return 1;
4037 }
4038}
4039
c8480b58
L
4040/* Encode aligned vector move as unaligned vector move. */
4041
4042static void
4043encode_with_unaligned_vector_move (void)
4044{
4045 switch (i.tm.base_opcode)
4046 {
b3a9fe6f
L
4047 case 0x28: /* Load instructions. */
4048 case 0x29: /* Store instructions. */
c8480b58 4049 /* movaps/movapd/vmovaps/vmovapd. */
ddb62495 4050 if (i.tm.opcode_space == SPACE_0F
c8480b58 4051 && i.tm.opcode_modifier.opcodeprefix <= PREFIX_0X66)
b3a9fe6f 4052 i.tm.base_opcode = 0x10 | (i.tm.base_opcode & 1);
c8480b58 4053 break;
b3a9fe6f
L
4054 case 0x6f: /* Load instructions. */
4055 case 0x7f: /* Store instructions. */
c8480b58 4056 /* movdqa/vmovdqa/vmovdqa64/vmovdqa32. */
ddb62495 4057 if (i.tm.opcode_space == SPACE_0F
c8480b58
L
4058 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0X66)
4059 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
4060 break;
4061 default:
4062 break;
4063 }
4064}
4065
b6f8c7c4
L
4066/* Try the shortest encoding by shortening operand size. */
4067
4068static void
4069optimize_encoding (void)
4070{
a0a1771e 4071 unsigned int j;
b6f8c7c4 4072
7fc69528 4073 if (i.tm.mnem_off == MN_lea)
fe134c65
JB
4074 {
4075 /* Optimize: -O:
4076 lea symbol, %rN -> mov $symbol, %rN
4077 lea (%rM), %rN -> mov %rM, %rN
4078 lea (,%rM,1), %rN -> mov %rM, %rN
4079
4080 and in 32-bit mode for 16-bit addressing
4081
4082 lea (%rM), %rN -> movzx %rM, %rN
4083
4084 and in 64-bit mode zap 32-bit addressing in favor of using a
4085 32-bit (or less) destination.
4086 */
4087 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
4088 {
4089 if (!i.op[1].regs->reg_type.bitfield.word)
4090 i.tm.opcode_modifier.size = SIZE32;
4091 i.prefix[ADDR_PREFIX] = 0;
4092 }
4093
4094 if (!i.index_reg && !i.base_reg)
4095 {
4096 /* Handle:
4097 lea symbol, %rN -> mov $symbol, %rN
4098 */
4099 if (flag_code == CODE_64BIT)
4100 {
4101 /* Don't transform a relocation to a 16-bit one. */
4102 if (i.op[0].disps
4103 && i.op[0].disps->X_op != O_constant
4104 && i.op[1].regs->reg_type.bitfield.word)
4105 return;
4106
4107 if (!i.op[1].regs->reg_type.bitfield.qword
4108 || i.tm.opcode_modifier.size == SIZE32)
4109 {
4110 i.tm.base_opcode = 0xb8;
4111 i.tm.opcode_modifier.modrm = 0;
4112 if (!i.op[1].regs->reg_type.bitfield.word)
4113 i.types[0].bitfield.imm32 = 1;
4114 else
4115 {
4116 i.tm.opcode_modifier.size = SIZE16;
4117 i.types[0].bitfield.imm16 = 1;
4118 }
4119 }
4120 else
4121 {
4122 /* Subject to further optimization below. */
4123 i.tm.base_opcode = 0xc7;
4124 i.tm.extension_opcode = 0;
4125 i.types[0].bitfield.imm32s = 1;
4126 i.types[0].bitfield.baseindex = 0;
4127 }
4128 }
4129 /* Outside of 64-bit mode address and operand sizes have to match if
4130 a relocation is involved, as otherwise we wouldn't (currently) or
4131 even couldn't express the relocation correctly. */
4132 else if (i.op[0].disps
4133 && i.op[0].disps->X_op != O_constant
4134 && ((!i.prefix[ADDR_PREFIX])
4135 != (flag_code == CODE_32BIT
4136 ? i.op[1].regs->reg_type.bitfield.dword
4137 : i.op[1].regs->reg_type.bitfield.word)))
4138 return;
7772f168
JB
4139 /* In 16-bit mode converting LEA with 16-bit addressing and a 32-bit
4140 destination is going to grow encoding size. */
4141 else if (flag_code == CODE_16BIT
4142 && (optimize <= 1 || optimize_for_space)
4143 && !i.prefix[ADDR_PREFIX]
4144 && i.op[1].regs->reg_type.bitfield.dword)
4145 return;
fe134c65
JB
4146 else
4147 {
4148 i.tm.base_opcode = 0xb8;
4149 i.tm.opcode_modifier.modrm = 0;
4150 if (i.op[1].regs->reg_type.bitfield.dword)
4151 i.types[0].bitfield.imm32 = 1;
4152 else
4153 i.types[0].bitfield.imm16 = 1;
4154
4155 if (i.op[0].disps
4156 && i.op[0].disps->X_op == O_constant
4157 && i.op[1].regs->reg_type.bitfield.dword
60cfa10c
L
4158 /* NB: Add () to !i.prefix[ADDR_PREFIX] to silence
4159 GCC 5. */
4160 && (!i.prefix[ADDR_PREFIX]) != (flag_code == CODE_32BIT))
fe134c65
JB
4161 i.op[0].disps->X_add_number &= 0xffff;
4162 }
4163
4164 i.tm.operand_types[0] = i.types[0];
4165 i.imm_operands = 1;
4166 if (!i.op[0].imms)
4167 {
4168 i.op[0].imms = &im_expressions[0];
4169 i.op[0].imms->X_op = O_absent;
4170 }
4171 }
4172 else if (i.op[0].disps
4173 && (i.op[0].disps->X_op != O_constant
4174 || i.op[0].disps->X_add_number))
4175 return;
4176 else
4177 {
4178 /* Handle:
4179 lea (%rM), %rN -> mov %rM, %rN
4180 lea (,%rM,1), %rN -> mov %rM, %rN
4181 lea (%rM), %rN -> movzx %rM, %rN
4182 */
4183 const reg_entry *addr_reg;
4184
4185 if (!i.index_reg && i.base_reg->reg_num != RegIP)
4186 addr_reg = i.base_reg;
4187 else if (!i.base_reg
4188 && i.index_reg->reg_num != RegIZ
4189 && !i.log2_scale_factor)
4190 addr_reg = i.index_reg;
4191 else
4192 return;
4193
4194 if (addr_reg->reg_type.bitfield.word
4195 && i.op[1].regs->reg_type.bitfield.dword)
4196 {
4197 if (flag_code != CODE_32BIT)
4198 return;
ddb62495 4199 i.tm.opcode_space = SPACE_0F;
fe134c65
JB
4200 i.tm.base_opcode = 0xb7;
4201 }
4202 else
4203 i.tm.base_opcode = 0x8b;
4204
4205 if (addr_reg->reg_type.bitfield.dword
4206 && i.op[1].regs->reg_type.bitfield.qword)
4207 i.tm.opcode_modifier.size = SIZE32;
4208
4209 i.op[0].regs = addr_reg;
4210 i.reg_operands = 2;
4211 }
4212
4213 i.mem_operands = 0;
4214 i.disp_operands = 0;
4215 i.prefix[ADDR_PREFIX] = 0;
4216 i.prefix[SEG_PREFIX] = 0;
4217 i.seg[0] = NULL;
4218 }
4219
b6f8c7c4 4220 if (optimize_for_space
7fc69528 4221 && i.tm.mnem_off == MN_test
b6f8c7c4
L
4222 && i.reg_operands == 1
4223 && i.imm_operands == 1
4224 && !i.types[1].bitfield.byte
4225 && i.op[0].imms->X_op == O_constant
7fc69528 4226 && fits_in_imm7 (i.op[0].imms->X_add_number))
b6f8c7c4
L
4227 {
4228 /* Optimize: -Os:
4229 test $imm7, %r64/%r32/%r16 -> test $imm7, %r8
4230 */
4231 unsigned int base_regnum = i.op[1].regs->reg_num;
4232 if (flag_code == CODE_64BIT || base_regnum < 4)
4233 {
4234 i.types[1].bitfield.byte = 1;
4235 /* Ignore the suffix. */
4236 i.suffix = 0;
7697afb6
JB
4237 /* Convert to byte registers. */
4238 if (i.types[1].bitfield.word)
4239 j = 16;
4240 else if (i.types[1].bitfield.dword)
4241 j = 32;
4242 else
4243 j = 48;
4244 if (!(i.op[1].regs->reg_flags & RegRex) && base_regnum < 4)
4245 j += 8;
4246 i.op[1].regs -= j;
b6f8c7c4
L
4247 }
4248 }
4249 else if (flag_code == CODE_64BIT
ddb62495 4250 && i.tm.opcode_space == SPACE_BASE
d3d50934
L
4251 && ((i.types[1].bitfield.qword
4252 && i.reg_operands == 1
b6f8c7c4
L
4253 && i.imm_operands == 1
4254 && i.op[0].imms->X_op == O_constant
507916b8 4255 && ((i.tm.base_opcode == 0xb8
b6f8c7c4
L
4256 && i.tm.extension_opcode == None
4257 && fits_in_unsigned_long (i.op[0].imms->X_add_number))
4258 || (fits_in_imm31 (i.op[0].imms->X_add_number)
7fc69528 4259 && (i.tm.base_opcode == 0x24
b6f8c7c4
L
4260 || (i.tm.base_opcode == 0x80
4261 && i.tm.extension_opcode == 0x4)
7fc69528
JB
4262 || i.tm.mnem_off == MN_test
4263 || ((i.tm.base_opcode | 1) == 0xc7
b8364fa7
JB
4264 && i.tm.extension_opcode == 0x0)))
4265 || (fits_in_imm7 (i.op[0].imms->X_add_number)
4266 && i.tm.base_opcode == 0x83
4267 && i.tm.extension_opcode == 0x4)))
d3d50934
L
4268 || (i.types[0].bitfield.qword
4269 && ((i.reg_operands == 2
4270 && i.op[0].regs == i.op[1].regs
7fc69528
JB
4271 && (i.tm.mnem_off == MN_xor
4272 || i.tm.mnem_off == MN_sub))
4273 || i.tm.mnem_off == MN_clr))))
b6f8c7c4
L
4274 {
4275 /* Optimize: -O:
4276 andq $imm31, %r64 -> andl $imm31, %r32
b8364fa7 4277 andq $imm7, %r64 -> andl $imm7, %r32
b6f8c7c4
L
4278 testq $imm31, %r64 -> testl $imm31, %r32
4279 xorq %r64, %r64 -> xorl %r32, %r32
4280 subq %r64, %r64 -> subl %r32, %r32
4281 movq $imm31, %r64 -> movl $imm31, %r32
4282 movq $imm32, %r64 -> movl $imm32, %r32
4283 */
04784e33
JB
4284 i.tm.opcode_modifier.size = SIZE32;
4285 if (i.imm_operands)
4286 {
4287 i.types[0].bitfield.imm32 = 1;
4288 i.types[0].bitfield.imm32s = 0;
4289 i.types[0].bitfield.imm64 = 0;
4290 }
4291 else
4292 {
4293 i.types[0].bitfield.dword = 1;
4294 i.types[0].bitfield.qword = 0;
4295 }
4296 i.types[1].bitfield.dword = 1;
4297 i.types[1].bitfield.qword = 0;
7fc69528 4298 if (i.tm.mnem_off == MN_mov || i.tm.mnem_off == MN_lea)
b6f8c7c4
L
4299 {
4300 /* Handle
4301 movq $imm31, %r64 -> movl $imm31, %r32
4302 movq $imm32, %r64 -> movl $imm32, %r32
4303 */
4304 i.tm.operand_types[0].bitfield.imm32 = 1;
4305 i.tm.operand_types[0].bitfield.imm32s = 0;
4306 i.tm.operand_types[0].bitfield.imm64 = 0;
507916b8 4307 if ((i.tm.base_opcode | 1) == 0xc7)
b6f8c7c4
L
4308 {
4309 /* Handle
4310 movq $imm31, %r64 -> movl $imm31, %r32
4311 */
507916b8 4312 i.tm.base_opcode = 0xb8;
b6f8c7c4 4313 i.tm.extension_opcode = None;
507916b8 4314 i.tm.opcode_modifier.w = 0;
b6f8c7c4
L
4315 i.tm.opcode_modifier.modrm = 0;
4316 }
4317 }
4318 }
5641ec01
JB
4319 else if (optimize > 1
4320 && !optimize_for_space
4321 && i.reg_operands == 2
4322 && i.op[0].regs == i.op[1].regs
7fc69528 4323 && (i.tm.mnem_off == MN_and || i.tm.mnem_off == MN_or)
5641ec01
JB
4324 && (flag_code != CODE_64BIT || !i.types[0].bitfield.dword))
4325 {
4326 /* Optimize: -O2:
4327 andb %rN, %rN -> testb %rN, %rN
4328 andw %rN, %rN -> testw %rN, %rN
4329 andq %rN, %rN -> testq %rN, %rN
4330 orb %rN, %rN -> testb %rN, %rN
4331 orw %rN, %rN -> testw %rN, %rN
4332 orq %rN, %rN -> testq %rN, %rN
4333
4334 and outside of 64-bit mode
4335
4336 andl %rN, %rN -> testl %rN, %rN
4337 orl %rN, %rN -> testl %rN, %rN
4338 */
4339 i.tm.base_opcode = 0x84 | (i.tm.base_opcode & 1);
4340 }
ad2f4436
JB
4341 else if (i.tm.base_opcode == 0xba
4342 && i.tm.opcode_space == SPACE_0F
4343 && i.reg_operands == 1
4344 && i.op[0].imms->X_op == O_constant
4345 && i.op[0].imms->X_add_number >= 0)
4346 {
4347 /* Optimize: -O:
4348 btw $n, %rN -> btl $n, %rN (outside of 16-bit mode, n < 16)
4349 btq $n, %rN -> btl $n, %rN (in 64-bit mode, n < 32, N < 8)
4350 btl $n, %rN -> btw $n, %rN (in 16-bit mode, n < 16)
4351
4352 With <BT> one of bts, btr, and bts also:
4353 <BT>w $n, %rN -> btl $n, %rN (in 32-bit mode, n < 16)
4354 <BT>l $n, %rN -> btw $n, %rN (in 16-bit mode, n < 16)
4355 */
4356 switch (flag_code)
4357 {
4358 case CODE_64BIT:
4359 if (i.tm.extension_opcode != 4)
4360 break;
4361 if (i.types[1].bitfield.qword
4362 && i.op[0].imms->X_add_number < 32
4363 && !(i.op[1].regs->reg_flags & RegRex))
4364 i.tm.opcode_modifier.size = SIZE32;
4365 /* Fall through. */
4366 case CODE_32BIT:
4367 if (i.types[1].bitfield.word
4368 && i.op[0].imms->X_add_number < 16)
4369 i.tm.opcode_modifier.size = SIZE32;
4370 break;
4371 case CODE_16BIT:
4372 if (i.op[0].imms->X_add_number < 16)
4373 i.tm.opcode_modifier.size = SIZE16;
4374 break;
4375 }
4376 }
99112332 4377 else if (i.reg_operands == 3
b6f8c7c4
L
4378 && i.op[0].regs == i.op[1].regs
4379 && !i.types[2].bitfield.xmmword
4380 && (i.tm.opcode_modifier.vex
6225c532 4381 || ((!i.mask.reg || i.mask.zeroing)
e771e7c9 4382 && is_evex_encoding (&i.tm)
80c34c38 4383 && (i.vec_encoding != vex_encoding_evex
dd22218c 4384 || cpu_arch_isa_flags.bitfield.cpuavx512vl
80c34c38 4385 || i.tm.cpu_flags.bitfield.cpuavx512vl
7091c612 4386 || (i.tm.operand_types[2].bitfield.zmmword
dd22218c 4387 && i.types[2].bitfield.ymmword))))
ddb62495 4388 && i.tm.opcode_space == SPACE_0F
5844ccaa
JB
4389 && ((i.tm.base_opcode | 2) == 0x57
4390 || i.tm.base_opcode == 0xdf
4391 || i.tm.base_opcode == 0xef
4392 || (i.tm.base_opcode | 3) == 0xfb
4393 || i.tm.base_opcode == 0x42
4394 || i.tm.base_opcode == 0x47))
b6f8c7c4 4395 {
99112332 4396 /* Optimize: -O1:
8305403a
L
4397 VOP, one of vandnps, vandnpd, vxorps, vxorpd, vpsubb, vpsubd,
4398 vpsubq and vpsubw:
b6f8c7c4
L
4399 EVEX VOP %zmmM, %zmmM, %zmmN
4400 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4401 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4402 EVEX VOP %ymmM, %ymmM, %ymmN
4403 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4404 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4405 VEX VOP %ymmM, %ymmM, %ymmN
4406 -> VEX VOP %xmmM, %xmmM, %xmmN
4407 VOP, one of vpandn and vpxor:
4408 VEX VOP %ymmM, %ymmM, %ymmN
4409 -> VEX VOP %xmmM, %xmmM, %xmmN
4410 VOP, one of vpandnd and vpandnq:
4411 EVEX VOP %zmmM, %zmmM, %zmmN
4412 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4413 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4414 EVEX VOP %ymmM, %ymmM, %ymmN
4415 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4416 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4417 VOP, one of vpxord and vpxorq:
4418 EVEX VOP %zmmM, %zmmM, %zmmN
4419 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4420 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4421 EVEX VOP %ymmM, %ymmM, %ymmN
4422 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4423 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
1424ad86
JB
4424 VOP, one of kxord and kxorq:
4425 VEX VOP %kM, %kM, %kN
4426 -> VEX kxorw %kM, %kM, %kN
4427 VOP, one of kandnd and kandnq:
4428 VEX VOP %kM, %kM, %kN
4429 -> VEX kandnw %kM, %kM, %kN
b6f8c7c4 4430 */
e771e7c9 4431 if (is_evex_encoding (&i.tm))
b6f8c7c4 4432 {
7b1d7ca1 4433 if (i.vec_encoding != vex_encoding_evex)
b6f8c7c4
L
4434 {
4435 i.tm.opcode_modifier.vex = VEX128;
4436 i.tm.opcode_modifier.vexw = VEXW0;
4437 i.tm.opcode_modifier.evex = 0;
4438 }
7b1d7ca1 4439 else if (optimize > 1)
dd22218c
L
4440 i.tm.opcode_modifier.evex = EVEX128;
4441 else
4442 return;
b6f8c7c4 4443 }
f74a6307 4444 else if (i.tm.operand_types[0].bitfield.class == RegMask)
1424ad86 4445 {
35648716 4446 i.tm.opcode_modifier.opcodeprefix = PREFIX_NONE;
1424ad86
JB
4447 i.tm.opcode_modifier.vexw = VEXW0;
4448 }
b6f8c7c4
L
4449 else
4450 i.tm.opcode_modifier.vex = VEX128;
4451
4452 if (i.tm.opcode_modifier.vex)
4453 for (j = 0; j < 3; j++)
4454 {
4455 i.types[j].bitfield.xmmword = 1;
4456 i.types[j].bitfield.ymmword = 0;
4457 }
4458 }
392a5972 4459 else if (i.vec_encoding != vex_encoding_evex
97ed31ae 4460 && !i.types[0].bitfield.zmmword
392a5972 4461 && !i.types[1].bitfield.zmmword
6225c532 4462 && !i.mask.reg
9b345ce8 4463 && !i.broadcast.type
a5748e0d 4464 && !i.broadcast.bytes
97ed31ae 4465 && is_evex_encoding (&i.tm)
35648716
JB
4466 && ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4467 || (i.tm.base_opcode & ~4) == 0xdb
4468 || (i.tm.base_opcode & ~4) == 0xeb)
97ed31ae
L
4469 && i.tm.extension_opcode == None)
4470 {
4471 /* Optimize: -O1:
4472 VOP, one of vmovdqa32, vmovdqa64, vmovdqu8, vmovdqu16,
4473 vmovdqu32 and vmovdqu64:
4474 EVEX VOP %xmmM, %xmmN
4475 -> VEX vmovdqa|vmovdqu %xmmM, %xmmN (M and N < 16)
4476 EVEX VOP %ymmM, %ymmN
4477 -> VEX vmovdqa|vmovdqu %ymmM, %ymmN (M and N < 16)
4478 EVEX VOP %xmmM, mem
4479 -> VEX vmovdqa|vmovdqu %xmmM, mem (M < 16)
4480 EVEX VOP %ymmM, mem
4481 -> VEX vmovdqa|vmovdqu %ymmM, mem (M < 16)
4482 EVEX VOP mem, %xmmN
4483 -> VEX mvmovdqa|vmovdquem, %xmmN (N < 16)
4484 EVEX VOP mem, %ymmN
4485 -> VEX vmovdqa|vmovdqu mem, %ymmN (N < 16)
a0a1771e
JB
4486 VOP, one of vpand, vpandn, vpor, vpxor:
4487 EVEX VOP{d,q} %xmmL, %xmmM, %xmmN
4488 -> VEX VOP %xmmL, %xmmM, %xmmN (L, M, and N < 16)
4489 EVEX VOP{d,q} %ymmL, %ymmM, %ymmN
4490 -> VEX VOP %ymmL, %ymmM, %ymmN (L, M, and N < 16)
4491 EVEX VOP{d,q} mem, %xmmM, %xmmN
4492 -> VEX VOP mem, %xmmM, %xmmN (M and N < 16)
4493 EVEX VOP{d,q} mem, %ymmM, %ymmN
4494 -> VEX VOP mem, %ymmM, %ymmN (M and N < 16)
97ed31ae 4495 */
a0a1771e 4496 for (j = 0; j < i.operands; j++)
392a5972
L
4497 if (operand_type_check (i.types[j], disp)
4498 && i.op[j].disps->X_op == O_constant)
4499 {
4500 /* Since the VEX prefix has 2 or 3 bytes, the EVEX prefix
4501 has 4 bytes, EVEX Disp8 has 1 byte and VEX Disp32 has 4
4502 bytes, we choose EVEX Disp8 over VEX Disp32. */
4503 int evex_disp8, vex_disp8;
4504 unsigned int memshift = i.memshift;
4505 offsetT n = i.op[j].disps->X_add_number;
4506
4507 evex_disp8 = fits_in_disp8 (n);
4508 i.memshift = 0;
4509 vex_disp8 = fits_in_disp8 (n);
4510 if (evex_disp8 != vex_disp8)
4511 {
4512 i.memshift = memshift;
4513 return;
4514 }
4515
4516 i.types[j].bitfield.disp8 = vex_disp8;
4517 break;
4518 }
35648716
JB
4519 if ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4520 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2)
4521 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
97ed31ae
L
4522 i.tm.opcode_modifier.vex
4523 = i.types[0].bitfield.ymmword ? VEX256 : VEX128;
4524 i.tm.opcode_modifier.vexw = VEXW0;
79dec6b7 4525 /* VPAND, VPOR, and VPXOR are commutative. */
35648716 4526 if (i.reg_operands == 3 && i.tm.base_opcode != 0xdf)
79dec6b7 4527 i.tm.opcode_modifier.commutative = 1;
97ed31ae
L
4528 i.tm.opcode_modifier.evex = 0;
4529 i.tm.opcode_modifier.masking = 0;
a0a1771e 4530 i.tm.opcode_modifier.broadcast = 0;
97ed31ae
L
4531 i.tm.opcode_modifier.disp8memshift = 0;
4532 i.memshift = 0;
a0a1771e
JB
4533 if (j < i.operands)
4534 i.types[j].bitfield.disp8
4535 = fits_in_disp8 (i.op[j].disps->X_add_number);
97ed31ae 4536 }
b6f8c7c4
L
4537}
4538
ae531041
L
4539/* Return non-zero for load instruction. */
4540
4541static int
4542load_insn_p (void)
4543{
4544 unsigned int dest;
4545 int any_vex_p = is_any_vex_encoding (&i.tm);
4546 unsigned int base_opcode = i.tm.base_opcode | 1;
4547
4548 if (!any_vex_p)
4549 {
ef07be45
CL
4550 /* Anysize insns: lea, invlpg, clflush, prefetch*, bndmk, bndcl, bndcu,
4551 bndcn, bndstx, bndldx, clflushopt, clwb, cldemote. */
255571cd 4552 if (i.tm.opcode_modifier.operandconstraint == ANY_SIZE)
ae531041
L
4553 return 0;
4554
389d00a5 4555 /* pop. */
6d86a545 4556 if (i.tm.mnem_off == MN_pop)
389d00a5
JB
4557 return 1;
4558 }
4559
ddb62495 4560 if (i.tm.opcode_space == SPACE_BASE)
389d00a5
JB
4561 {
4562 /* popf, popa. */
4563 if (i.tm.base_opcode == 0x9d
a09f656b 4564 || i.tm.base_opcode == 0x61)
ae531041
L
4565 return 1;
4566
4567 /* movs, cmps, lods, scas. */
4568 if ((i.tm.base_opcode | 0xb) == 0xaf)
4569 return 1;
4570
a09f656b 4571 /* outs, xlatb. */
4572 if (base_opcode == 0x6f
4573 || i.tm.base_opcode == 0xd7)
ae531041 4574 return 1;
a09f656b 4575 /* NB: For AMD-specific insns with implicit memory operands,
4576 they're intentionally not covered. */
ae531041
L
4577 }
4578
4579 /* No memory operand. */
4580 if (!i.mem_operands)
4581 return 0;
4582
4583 if (any_vex_p)
4584 {
7fc69528 4585 if (i.tm.mnem_off == MN_vldmxcsr)
ae531041
L
4586 return 1;
4587 }
ddb62495 4588 else if (i.tm.opcode_space == SPACE_BASE)
ae531041
L
4589 {
4590 /* test, not, neg, mul, imul, div, idiv. */
aa4c197d 4591 if (base_opcode == 0xf7 && i.tm.extension_opcode != 1)
ae531041
L
4592 return 1;
4593
4594 /* inc, dec. */
4595 if (base_opcode == 0xff && i.tm.extension_opcode <= 1)
4596 return 1;
4597
4598 /* add, or, adc, sbb, and, sub, xor, cmp. */
4599 if (i.tm.base_opcode >= 0x80 && i.tm.base_opcode <= 0x83)
4600 return 1;
4601
ae531041 4602 /* rol, ror, rcl, rcr, shl/sal, shr, sar. */
aa4c197d 4603 if ((base_opcode == 0xc1 || (base_opcode | 2) == 0xd3)
ae531041
L
4604 && i.tm.extension_opcode != 6)
4605 return 1;
4606
ae531041 4607 /* Check for x87 instructions. */
aa4c197d 4608 if ((base_opcode | 6) == 0xdf)
ae531041
L
4609 {
4610 /* Skip fst, fstp, fstenv, fstcw. */
4611 if (i.tm.base_opcode == 0xd9
4612 && (i.tm.extension_opcode == 2
4613 || i.tm.extension_opcode == 3
4614 || i.tm.extension_opcode == 6
4615 || i.tm.extension_opcode == 7))
4616 return 0;
4617
4618 /* Skip fisttp, fist, fistp, fstp. */
4619 if (i.tm.base_opcode == 0xdb
4620 && (i.tm.extension_opcode == 1
4621 || i.tm.extension_opcode == 2
4622 || i.tm.extension_opcode == 3
4623 || i.tm.extension_opcode == 7))
4624 return 0;
4625
4626 /* Skip fisttp, fst, fstp, fsave, fstsw. */
4627 if (i.tm.base_opcode == 0xdd
4628 && (i.tm.extension_opcode == 1
4629 || i.tm.extension_opcode == 2
4630 || i.tm.extension_opcode == 3
4631 || i.tm.extension_opcode == 6
4632 || i.tm.extension_opcode == 7))
4633 return 0;
4634
4635 /* Skip fisttp, fist, fistp, fbstp, fistp. */
4636 if (i.tm.base_opcode == 0xdf
4637 && (i.tm.extension_opcode == 1
4638 || i.tm.extension_opcode == 2
4639 || i.tm.extension_opcode == 3
4640 || i.tm.extension_opcode == 6
4641 || i.tm.extension_opcode == 7))
4642 return 0;
4643
4644 return 1;
4645 }
4646 }
ddb62495 4647 else if (i.tm.opcode_space == SPACE_0F)
389d00a5
JB
4648 {
4649 /* bt, bts, btr, btc. */
4650 if (i.tm.base_opcode == 0xba
aa4c197d 4651 && (i.tm.extension_opcode | 3) == 7)
389d00a5
JB
4652 return 1;
4653
4654 /* cmpxchg8b, cmpxchg16b, xrstors, vmptrld. */
4655 if (i.tm.base_opcode == 0xc7
4656 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
4657 && (i.tm.extension_opcode == 1 || i.tm.extension_opcode == 3
4658 || i.tm.extension_opcode == 6))
4659 return 1;
4660
4661 /* fxrstor, ldmxcsr, xrstor. */
4662 if (i.tm.base_opcode == 0xae
4663 && (i.tm.extension_opcode == 1
4664 || i.tm.extension_opcode == 2
4665 || i.tm.extension_opcode == 5))
4666 return 1;
4667
4668 /* lgdt, lidt, lmsw. */
4669 if (i.tm.base_opcode == 0x01
4670 && (i.tm.extension_opcode == 2
4671 || i.tm.extension_opcode == 3
4672 || i.tm.extension_opcode == 6))
4673 return 1;
4674 }
ae531041
L
4675
4676 dest = i.operands - 1;
4677
4678 /* Check fake imm8 operand and 3 source operands. */
4679 if ((i.tm.opcode_modifier.immext
aa180741 4680 || i.reg_operands + i.mem_operands == 4)
ae531041
L
4681 && i.types[dest].bitfield.imm8)
4682 dest--;
4683
389d00a5 4684 /* add, or, adc, sbb, and, sub, xor, cmp, test, xchg. */
ddb62495 4685 if (i.tm.opcode_space == SPACE_BASE
aa4c197d 4686 && ((base_opcode | 0x38) == 0x39
389d00a5
JB
4687 || (base_opcode | 2) == 0x87))
4688 return 1;
4689
7fc69528 4690 if (i.tm.mnem_off == MN_xadd)
ae531041
L
4691 return 1;
4692
4693 /* Check for load instruction. */
4694 return (i.types[dest].bitfield.class != ClassNone
4695 || i.types[dest].bitfield.instance == Accum);
4696}
4697
4698/* Output lfence, 0xfaee8, after instruction. */
4699
4700static void
4701insert_lfence_after (void)
4702{
4703 if (lfence_after_load && load_insn_p ())
4704 {
a09f656b 4705 /* There are also two REP string instructions that require
4706 special treatment. Specifically, the compare string (CMPS)
4707 and scan string (SCAS) instructions set EFLAGS in a manner
4708 that depends on the data being compared/scanned. When used
4709 with a REP prefix, the number of iterations may therefore
4710 vary depending on this data. If the data is a program secret
4711 chosen by the adversary using an LVI method,
4712 then this data-dependent behavior may leak some aspect
4713 of the secret. */
aa4c197d 4714 if (((i.tm.base_opcode | 0x9) == 0xaf)
a09f656b 4715 && i.prefix[REP_PREFIX])
4716 {
4717 as_warn (_("`%s` changes flags which would affect control flow behavior"),
76d3f746 4718 insn_name (&i.tm));
a09f656b 4719 }
ae531041
L
4720 char *p = frag_more (3);
4721 *p++ = 0xf;
4722 *p++ = 0xae;
4723 *p = 0xe8;
4724 }
4725}
4726
4727/* Output lfence, 0xfaee8, before instruction. */
4728
4729static void
4730insert_lfence_before (void)
4731{
4732 char *p;
4733
ddb62495 4734 if (i.tm.opcode_space != SPACE_BASE)
ae531041
L
4735 return;
4736
4737 if (i.tm.base_opcode == 0xff
4738 && (i.tm.extension_opcode == 2 || i.tm.extension_opcode == 4))
4739 {
4740 /* Insert lfence before indirect branch if needed. */
4741
4742 if (lfence_before_indirect_branch == lfence_branch_none)
4743 return;
4744
4745 if (i.operands != 1)
4746 abort ();
4747
4748 if (i.reg_operands == 1)
4749 {
4750 /* Indirect branch via register. Don't insert lfence with
4751 -mlfence-after-load=yes. */
4752 if (lfence_after_load
4753 || lfence_before_indirect_branch == lfence_branch_memory)
4754 return;
4755 }
4756 else if (i.mem_operands == 1
4757 && lfence_before_indirect_branch != lfence_branch_register)
4758 {
4759 as_warn (_("indirect `%s` with memory operand should be avoided"),
76d3f746 4760 insn_name (&i.tm));
ae531041
L
4761 return;
4762 }
4763 else
4764 return;
4765
4766 if (last_insn.kind != last_insn_other
4767 && last_insn.seg == now_seg)
4768 {
4769 as_warn_where (last_insn.file, last_insn.line,
4770 _("`%s` skips -mlfence-before-indirect-branch on `%s`"),
76d3f746 4771 last_insn.name, insn_name (&i.tm));
ae531041
L
4772 return;
4773 }
4774
4775 p = frag_more (3);
4776 *p++ = 0xf;
4777 *p++ = 0xae;
4778 *p = 0xe8;
4779 return;
4780 }
4781
503648e4 4782 /* Output or/not/shl and lfence before near ret. */
ae531041 4783 if (lfence_before_ret != lfence_before_ret_none
aa4c197d 4784 && (i.tm.base_opcode | 1) == 0xc3)
ae531041
L
4785 {
4786 if (last_insn.kind != last_insn_other
4787 && last_insn.seg == now_seg)
4788 {
4789 as_warn_where (last_insn.file, last_insn.line,
4790 _("`%s` skips -mlfence-before-ret on `%s`"),
76d3f746 4791 last_insn.name, insn_name (&i.tm));
ae531041
L
4792 return;
4793 }
a09f656b 4794
a09f656b 4795 /* Near ret ingore operand size override under CPU64. */
503648e4 4796 char prefix = flag_code == CODE_64BIT
4797 ? 0x48
4798 : i.prefix[DATA_PREFIX] ? 0x66 : 0x0;
a09f656b 4799
4800 if (lfence_before_ret == lfence_before_ret_not)
4801 {
4802 /* not: 0xf71424, may add prefix
4803 for operand size override or 64-bit code. */
4804 p = frag_more ((prefix ? 2 : 0) + 6 + 3);
4805 if (prefix)
4806 *p++ = prefix;
ae531041
L
4807 *p++ = 0xf7;
4808 *p++ = 0x14;
4809 *p++ = 0x24;
a09f656b 4810 if (prefix)
4811 *p++ = prefix;
ae531041
L
4812 *p++ = 0xf7;
4813 *p++ = 0x14;
4814 *p++ = 0x24;
4815 }
a09f656b 4816 else
4817 {
4818 p = frag_more ((prefix ? 1 : 0) + 4 + 3);
4819 if (prefix)
4820 *p++ = prefix;
4821 if (lfence_before_ret == lfence_before_ret_or)
4822 {
4823 /* or: 0x830c2400, may add prefix
4824 for operand size override or 64-bit code. */
4825 *p++ = 0x83;
4826 *p++ = 0x0c;
4827 }
4828 else
4829 {
4830 /* shl: 0xc1242400, may add prefix
4831 for operand size override or 64-bit code. */
4832 *p++ = 0xc1;
4833 *p++ = 0x24;
4834 }
4835
4836 *p++ = 0x24;
4837 *p++ = 0x0;
4838 }
4839
ae531041
L
4840 *p++ = 0xf;
4841 *p++ = 0xae;
4842 *p = 0xe8;
4843 }
4844}
4845
edd67638
JB
4846/* Shared helper for md_assemble() and s_insn(). */
4847static void init_globals (void)
4848{
4849 unsigned int j;
4850
4851 memset (&i, '\0', sizeof (i));
4852 i.rounding.type = rc_none;
4853 for (j = 0; j < MAX_OPERANDS; j++)
4854 i.reloc[j] = NO_RELOC;
4855 memset (disp_expressions, '\0', sizeof (disp_expressions));
4856 memset (im_expressions, '\0', sizeof (im_expressions));
4857 save_stack_p = save_stack;
4858}
4859
04784e33
JB
4860/* Helper for md_assemble() to decide whether to prepare for a possible 2nd
4861 parsing pass. Instead of introducing a rarely use new insn attribute this
4862 utilizes a common pattern between affected templates. It is deemed
4863 acceptable that this will lead to unnecessary pass 2 preparations in a
4864 limited set of cases. */
4865static INLINE bool may_need_pass2 (const insn_template *t)
4866{
4867 return t->opcode_modifier.sse2avx
4868 /* Note that all SSE2AVX templates have at least one operand. */
a28fedbc 4869 ? t->operand_types[t->operands - 1].bitfield.class == RegSIMD
ddb62495 4870 : (t->opcode_space == SPACE_0F
a28fedbc 4871 && (t->base_opcode | 1) == 0xbf)
ddb62495 4872 || (t->opcode_space == SPACE_BASE
a28fedbc 4873 && t->base_opcode == 0x63);
04784e33
JB
4874}
4875
252b5132
RH
4876/* This is the guts of the machine-dependent assembler. LINE points to a
4877 machine dependent instruction. This function is supposed to emit
4878 the frags/bytes it assembles to. */
4879
4880void
65da13b5 4881md_assemble (char *line)
252b5132 4882{
40fb9820 4883 unsigned int j;
9db83a32 4884 char mnemonic[MAX_MNEM_SIZE], mnem_suffix = 0, *copy = NULL;
04784e33
JB
4885 const char *end, *pass1_mnem = NULL;
4886 enum i386_error pass1_err = 0;
d3ce72d0 4887 const insn_template *t;
252b5132 4888
47926f60 4889 /* Initialize globals. */
04784e33
JB
4890 current_templates = NULL;
4891 retry:
edd67638 4892 init_globals ();
252b5132
RH
4893
4894 /* First parse an instruction mnemonic & call i386_operand for the operands.
4895 We assume that the scrubber has arranged it so that line[0] is the valid
47926f60 4896 start of a (possibly prefixed) mnemonic. */
252b5132 4897
edd67638 4898 end = parse_insn (line, mnemonic, false);
5317ad2c 4899 if (end == NULL)
04784e33
JB
4900 {
4901 if (pass1_mnem != NULL)
4902 goto match_error;
9db83a32
JB
4903 if (i.error != no_error)
4904 {
4905 gas_assert (current_templates != NULL);
4906 if (may_need_pass2 (current_templates->start) && !i.suffix)
4907 goto no_match;
4908 /* No point in trying a 2nd pass - it'll only find the same suffix
4909 again. */
4910 mnem_suffix = i.suffix;
4911 goto match_error;
4912 }
04784e33
JB
4913 return;
4914 }
6d86a545
JB
4915 t = current_templates->start;
4916 if (may_need_pass2 (t))
04784e33
JB
4917 {
4918 /* Make a copy of the full line in case we need to retry. */
4919 copy = xstrdup (line);
4920 }
5317ad2c 4921 line += end - line;
83b16ac6 4922 mnem_suffix = i.suffix;
252b5132 4923
29b0f896 4924 line = parse_operands (line, mnemonic);
ee86248c 4925 this_operand = -1;
29b0f896 4926 if (line == NULL)
04784e33
JB
4927 {
4928 free (copy);
4929 return;
4930 }
252b5132 4931
29b0f896
AM
4932 /* Now we've parsed the mnemonic into a set of templates, and have the
4933 operands at hand. */
4934
b630c145 4935 /* All Intel opcodes have reversed operands except for "bound", "enter",
c0e54661 4936 "invlpg*", "monitor*", "mwait*", "tpause", "umwait", "pvalidate",
b0e8fa7f
TJ
4937 "rmpadjust", "rmpupdate", and "rmpquery". We also don't reverse
4938 intersegment "jmp" and "call" instructions with 2 immediate operands so
4939 that the immediate segment precedes the offset consistently in Intel and
4940 AT&T modes. */
4d456e3d
L
4941 if (intel_syntax
4942 && i.operands > 1
6d86a545
JB
4943 && (t->mnem_off != MN_bound)
4944 && !startswith (mnemonic, "invlpg")
d34049e8
ML
4945 && !startswith (mnemonic, "monitor")
4946 && !startswith (mnemonic, "mwait")
6d86a545 4947 && (t->mnem_off != MN_pvalidate)
d34049e8 4948 && !startswith (mnemonic, "rmp")
6d86a545
JB
4949 && (t->mnem_off != MN_tpause)
4950 && (t->mnem_off != MN_umwait)
47c0279b
JB
4951 && !(i.operands == 2
4952 && operand_type_check (i.types[0], imm)
40fb9820 4953 && operand_type_check (i.types[1], imm)))
29b0f896
AM
4954 swap_operands ();
4955
ec56d5c0
JB
4956 /* The order of the immediates should be reversed
4957 for 2 immediates extrq and insertq instructions */
4958 if (i.imm_operands == 2
6d86a545 4959 && (t->mnem_off == MN_extrq || t->mnem_off == MN_insertq))
ec56d5c0
JB
4960 swap_2_operands (0, 1);
4961
29b0f896
AM
4962 if (i.imm_operands)
4963 optimize_imm ();
4964
0de704b9
JB
4965 if (i.disp_operands && !optimize_disp (t))
4966 return;
29b0f896
AM
4967
4968 /* Next, we find a template that matches the given insn,
4969 making sure the overlap of the given operands types is consistent
4970 with the template operand types. */
252b5132 4971
83b16ac6 4972 if (!(t = match_template (mnem_suffix)))
04784e33
JB
4973 {
4974 const char *err_msg;
4975
4976 if (copy && !mnem_suffix)
4977 {
4978 line = copy;
4979 copy = NULL;
9db83a32 4980 no_match:
04784e33 4981 pass1_err = i.error;
76d3f746 4982 pass1_mnem = insn_name (current_templates->start);
04784e33
JB
4983 goto retry;
4984 }
9db83a32
JB
4985
4986 /* If a non-/only-64bit template (group) was found in pass 1, and if
4987 _some_ template (group) was found in pass 2, squash pass 1's
4988 error. */
4989 if (pass1_err == unsupported_64bit)
4990 pass1_mnem = NULL;
4991
04784e33 4992 match_error:
9db83a32
JB
4993 free (copy);
4994
04784e33
JB
4995 switch (pass1_mnem ? pass1_err : i.error)
4996 {
4997 default:
4998 abort ();
4999 case operand_size_mismatch:
5000 err_msg = _("operand size mismatch");
5001 break;
5002 case operand_type_mismatch:
5003 err_msg = _("operand type mismatch");
5004 break;
5005 case register_type_mismatch:
5006 err_msg = _("register type mismatch");
5007 break;
5008 case number_of_operands_mismatch:
5009 err_msg = _("number of operands mismatch");
5010 break;
5011 case invalid_instruction_suffix:
5012 err_msg = _("invalid instruction suffix");
5013 break;
5014 case bad_imm4:
5015 err_msg = _("constant doesn't fit in 4 bits");
5016 break;
5017 case unsupported_with_intel_mnemonic:
5018 err_msg = _("unsupported with Intel mnemonic");
5019 break;
5020 case unsupported_syntax:
5021 err_msg = _("unsupported syntax");
5022 break;
5023 case unsupported:
5024 as_bad (_("unsupported instruction `%s'"),
76d3f746 5025 pass1_mnem ? pass1_mnem : insn_name (current_templates->start));
04784e33 5026 return;
9db83a32
JB
5027 case unsupported_on_arch:
5028 as_bad (_("`%s' is not supported on `%s%s'"),
76d3f746 5029 pass1_mnem ? pass1_mnem : insn_name (current_templates->start),
9db83a32
JB
5030 cpu_arch_name ? cpu_arch_name : default_arch,
5031 cpu_sub_arch_name ? cpu_sub_arch_name : "");
5032 return;
5033 case unsupported_64bit:
5034 if (ISLOWER (mnem_suffix))
e8b4b7b2
NC
5035 {
5036 if (flag_code == CODE_64BIT)
5037 as_bad (_("`%s%c' is not supported in 64-bit mode"),
76d3f746 5038 pass1_mnem ? pass1_mnem : insn_name (current_templates->start),
e8b4b7b2
NC
5039 mnem_suffix);
5040 else
5041 as_bad (_("`%s%c' is only supported in 64-bit mode"),
76d3f746 5042 pass1_mnem ? pass1_mnem : insn_name (current_templates->start),
e8b4b7b2
NC
5043 mnem_suffix);
5044 }
9db83a32 5045 else
e8b4b7b2
NC
5046 {
5047 if (flag_code == CODE_64BIT)
5048 as_bad (_("`%s' is not supported in 64-bit mode"),
76d3f746 5049 pass1_mnem ? pass1_mnem : insn_name (current_templates->start));
e8b4b7b2
NC
5050 else
5051 as_bad (_("`%s' is only supported in 64-bit mode"),
76d3f746 5052 pass1_mnem ? pass1_mnem : insn_name (current_templates->start));
e8b4b7b2 5053 }
9db83a32 5054 return;
04784e33
JB
5055 case invalid_sib_address:
5056 err_msg = _("invalid SIB address");
5057 break;
5058 case invalid_vsib_address:
5059 err_msg = _("invalid VSIB address");
5060 break;
5061 case invalid_vector_register_set:
5062 err_msg = _("mask, index, and destination registers must be distinct");
5063 break;
5064 case invalid_tmm_register_set:
5065 err_msg = _("all tmm registers must be distinct");
5066 break;
5067 case invalid_dest_and_src_register_set:
5068 err_msg = _("destination and source registers must be distinct");
5069 break;
5070 case unsupported_vector_index_register:
5071 err_msg = _("unsupported vector index register");
5072 break;
5073 case unsupported_broadcast:
5074 err_msg = _("unsupported broadcast");
5075 break;
5076 case broadcast_needed:
5077 err_msg = _("broadcast is needed for operand of such type");
5078 break;
5079 case unsupported_masking:
5080 err_msg = _("unsupported masking");
5081 break;
5082 case mask_not_on_destination:
5083 err_msg = _("mask not on destination operand");
5084 break;
5085 case no_default_mask:
5086 err_msg = _("default mask isn't allowed");
5087 break;
5088 case unsupported_rc_sae:
5089 err_msg = _("unsupported static rounding/sae");
5090 break;
5091 case invalid_register_operand:
5092 err_msg = _("invalid register operand");
5093 break;
5094 }
5095 as_bad (_("%s for `%s'"), err_msg,
76d3f746 5096 pass1_mnem ? pass1_mnem : insn_name (current_templates->start));
04784e33
JB
5097 return;
5098 }
5099
5100 free (copy);
252b5132 5101
7bab8ab5 5102 if (sse_check != check_none
ffb86450
JB
5103 /* The opcode space check isn't strictly needed; it's there only to
5104 bypass the logic below when easily possible. */
ddb62495
JB
5105 && t->opcode_space >= SPACE_0F
5106 && t->opcode_space <= SPACE_0F3A
ffb86450
JB
5107 && !i.tm.cpu_flags.bitfield.cpusse4a
5108 && !is_any_vex_encoding (t))
daf50ae7 5109 {
ffb86450
JB
5110 bool simd = false;
5111
5112 for (j = 0; j < t->operands; ++j)
5113 {
5114 if (t->operand_types[j].bitfield.class == RegMMX)
5115 break;
5116 if (t->operand_types[j].bitfield.class == RegSIMD)
5117 simd = true;
5118 }
5119
5120 if (j >= t->operands && simd)
5121 (sse_check == check_warning
5122 ? as_warn
76d3f746 5123 : as_bad) (_("SSE instruction `%s' is used"), insn_name (&i.tm));
daf50ae7
L
5124 }
5125
40fb9820 5126 if (i.tm.opcode_modifier.fwait)
29b0f896
AM
5127 if (!add_prefix (FWAIT_OPCODE))
5128 return;
252b5132 5129
d5de92cf 5130 /* Check if REP prefix is OK. */
742732c7 5131 if (i.rep_prefix && i.tm.opcode_modifier.prefixok != PrefixRep)
d5de92cf
L
5132 {
5133 as_bad (_("invalid instruction `%s' after `%s'"),
76d3f746 5134 insn_name (&i.tm), i.rep_prefix);
d5de92cf
L
5135 return;
5136 }
5137
c1ba0266
L
5138 /* Check for lock without a lockable instruction. Destination operand
5139 must be memory unless it is xchg (0x86). */
9a4a4499
JB
5140 if (i.prefix[LOCK_PREFIX])
5141 {
5142 if (i.tm.opcode_modifier.prefixok < PrefixLock
c1ba0266
L
5143 || i.mem_operands == 0
5144 || (i.tm.base_opcode != 0x86
9a4a4499
JB
5145 && !(i.flags[i.operands - 1] & Operand_Mem)))
5146 {
5147 as_bad (_("expecting lockable instruction after `lock'"));
5148 return;
5149 }
5150
5151 /* Zap the redundant prefix from XCHG when optimizing. */
5152 if (i.tm.base_opcode == 0x86 && optimize && !i.no_optimize)
5153 i.prefix[LOCK_PREFIX] = 0;
c32fa91d
L
5154 }
5155
e3669c7f
JB
5156 if (is_any_vex_encoding (&i.tm)
5157 || i.tm.operand_types[i.imm_operands].bitfield.class >= RegMMX
5158 || i.tm.operand_types[i.imm_operands + 1].bitfield.class >= RegMMX)
7a8655d2 5159 {
e3669c7f
JB
5160 /* Check for data size prefix on VEX/XOP/EVEX encoded and SIMD insns. */
5161 if (i.prefix[DATA_PREFIX])
5162 {
76d3f746 5163 as_bad (_("data size prefix invalid with `%s'"), insn_name (&i.tm));
e3669c7f
JB
5164 return;
5165 }
5166
5167 /* Don't allow e.g. KMOV in TLS code sequences. */
5168 for (j = i.imm_operands; j < i.operands; ++j)
5169 switch (i.reloc[j])
5170 {
5171 case BFD_RELOC_386_TLS_GOTIE:
5172 case BFD_RELOC_386_TLS_LE_32:
5173 case BFD_RELOC_X86_64_GOTTPOFF:
5174 case BFD_RELOC_X86_64_TLSLD:
76d3f746 5175 as_bad (_("TLS relocation cannot be used with `%s'"), insn_name (&i.tm));
e3669c7f
JB
5176 return;
5177 default:
5178 break;
5179 }
7a8655d2
JB
5180 }
5181
42164a71 5182 /* Check if HLE prefix is OK. */
165de32a 5183 if (i.hle_prefix && !check_hle ())
42164a71
L
5184 return;
5185
7e8b059b
L
5186 /* Check BND prefix. */
5187 if (i.bnd_prefix && !i.tm.opcode_modifier.bndprefixok)
5188 as_bad (_("expecting valid branch instruction after `bnd'"));
5189
04ef582a 5190 /* Check NOTRACK prefix. */
742732c7 5191 if (i.notrack_prefix && i.tm.opcode_modifier.prefixok != PrefixNoTrack)
9fef80d6 5192 as_bad (_("expecting indirect branch instruction after `notrack'"));
04ef582a 5193
327e8c42
JB
5194 if (i.tm.cpu_flags.bitfield.cpumpx)
5195 {
5196 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
5197 as_bad (_("32-bit address isn't allowed in 64-bit MPX instructions."));
5198 else if (flag_code != CODE_16BIT
5199 ? i.prefix[ADDR_PREFIX]
5200 : i.mem_operands && !i.prefix[ADDR_PREFIX])
5201 as_bad (_("16-bit address isn't allowed in MPX instructions"));
5202 }
7e8b059b
L
5203
5204 /* Insert BND prefix. */
76d3a78a
JB
5205 if (add_bnd_prefix && i.tm.opcode_modifier.bndprefixok)
5206 {
5207 if (!i.prefix[BND_PREFIX])
5208 add_prefix (BND_PREFIX_OPCODE);
5209 else if (i.prefix[BND_PREFIX] != BND_PREFIX_OPCODE)
5210 {
5211 as_warn (_("replacing `rep'/`repe' prefix by `bnd'"));
5212 i.prefix[BND_PREFIX] = BND_PREFIX_OPCODE;
5213 }
5214 }
7e8b059b 5215
29b0f896 5216 /* Check string instruction segment overrides. */
51c8edf6 5217 if (i.tm.opcode_modifier.isstring >= IS_STRING_ES_OP0)
29b0f896 5218 {
51c8edf6 5219 gas_assert (i.mem_operands);
29b0f896 5220 if (!check_string ())
5dd0794d 5221 return;
fc0763e6 5222 i.disp_operands = 0;
29b0f896 5223 }
5dd0794d 5224
9373f275
L
5225 /* The memory operand of (%dx) should be only used with input/output
5226 instructions (base opcodes: 0x6c, 0x6e, 0xec, 0xee). */
5227 if (i.input_output_operand
5228 && ((i.tm.base_opcode | 0x82) != 0xee
ddb62495 5229 || i.tm.opcode_space != SPACE_BASE))
9373f275
L
5230 {
5231 as_bad (_("input/output port address isn't allowed with `%s'"),
76d3f746 5232 insn_name (&i.tm));
9373f275
L
5233 return;
5234 }
5235
b6f8c7c4
L
5236 if (optimize && !i.no_optimize && i.tm.opcode_modifier.optimize)
5237 optimize_encoding ();
5238
c8480b58
L
5239 if (use_unaligned_vector_move)
5240 encode_with_unaligned_vector_move ();
5241
29b0f896
AM
5242 if (!process_suffix ())
5243 return;
e413e4e9 5244
ef07be45
CL
5245 /* Check if IP-relative addressing requirements can be satisfied. */
5246 if (i.tm.cpu_flags.bitfield.cpuprefetchi
5247 && !(i.base_reg && i.base_reg->reg_num == RegIP))
76d3f746 5248 as_warn (_("'%s' only supports RIP-relative address"), insn_name (&i.tm));
ef07be45 5249
921eafea 5250 /* Update operand types and check extended states. */
bc0844ae 5251 for (j = 0; j < i.operands; j++)
921eafea
L
5252 {
5253 i.types[j] = operand_type_and (i.types[j], i.tm.operand_types[j]);
3d70986f 5254 switch (i.tm.operand_types[j].bitfield.class)
921eafea
L
5255 {
5256 default:
5257 break;
5258 case RegMMX:
5259 i.xstate |= xstate_mmx;
5260 break;
5261 case RegMask:
32930e4e 5262 i.xstate |= xstate_mask;
921eafea
L
5263 break;
5264 case RegSIMD:
3d70986f 5265 if (i.tm.operand_types[j].bitfield.tmmword)
921eafea 5266 i.xstate |= xstate_tmm;
3d70986f 5267 else if (i.tm.operand_types[j].bitfield.zmmword)
921eafea 5268 i.xstate |= xstate_zmm;
3d70986f 5269 else if (i.tm.operand_types[j].bitfield.ymmword)
921eafea 5270 i.xstate |= xstate_ymm;
3d70986f 5271 else if (i.tm.operand_types[j].bitfield.xmmword)
921eafea
L
5272 i.xstate |= xstate_xmm;
5273 break;
5274 }
5275 }
bc0844ae 5276
29b0f896
AM
5277 /* Make still unresolved immediate matches conform to size of immediate
5278 given in i.suffix. */
5279 if (!finalize_imm ())
5280 return;
252b5132 5281
40fb9820 5282 if (i.types[0].bitfield.imm1)
29b0f896 5283 i.imm_operands = 0; /* kludge for shift insns. */
252b5132 5284
29b0f896
AM
5285 /* For insns with operands there are more diddles to do to the opcode. */
5286 if (i.operands)
5287 {
5288 if (!process_operands ())
5289 return;
5290 }
255571cd 5291 else if (!quiet_warnings && i.tm.opcode_modifier.operandconstraint == UGH)
29b0f896
AM
5292 {
5293 /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc. */
76d3f746 5294 as_warn (_("translating to `%sp'"), insn_name (&i.tm));
29b0f896 5295 }
252b5132 5296
7a8655d2 5297 if (is_any_vex_encoding (&i.tm))
9e5e5283 5298 {
c1dc7af5 5299 if (!cpu_arch_flags.bitfield.cpui286)
9e5e5283 5300 {
c1dc7af5 5301 as_bad (_("instruction `%s' isn't supported outside of protected mode."),
76d3f746 5302 insn_name (&i.tm));
9e5e5283
L
5303 return;
5304 }
c0f3af97 5305
0b9404fd
JB
5306 /* Check for explicit REX prefix. */
5307 if (i.prefix[REX_PREFIX] || i.rex_encoding)
5308 {
76d3f746 5309 as_bad (_("REX prefix invalid with `%s'"), insn_name (&i.tm));
0b9404fd
JB
5310 return;
5311 }
5312
9e5e5283
L
5313 if (i.tm.opcode_modifier.vex)
5314 build_vex_prefix (t);
5315 else
5316 build_evex_prefix ();
0b9404fd
JB
5317
5318 /* The individual REX.RXBW bits got consumed. */
5319 i.rex &= REX_OPCODE;
9e5e5283 5320 }
43234a1e 5321
7fc69528
JB
5322 /* Handle conversion of 'int $3' --> special int3 insn. */
5323 if (i.tm.mnem_off == MN_int
a6461c02 5324 && i.op[0].imms->X_add_number == 3)
29b0f896
AM
5325 {
5326 i.tm.base_opcode = INT3_OPCODE;
5327 i.imm_operands = 0;
5328 }
252b5132 5329
0cfa3eb3
JB
5330 if ((i.tm.opcode_modifier.jump == JUMP
5331 || i.tm.opcode_modifier.jump == JUMP_BYTE
5332 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896
AM
5333 && i.op[0].disps->X_op == O_constant)
5334 {
5335 /* Convert "jmp constant" (and "call constant") to a jump (call) to
5336 the absolute address given by the constant. Since ix86 jumps and
5337 calls are pc relative, we need to generate a reloc. */
5338 i.op[0].disps->X_add_symbol = &abs_symbol;
5339 i.op[0].disps->X_op = O_symbol;
5340 }
252b5132 5341
29b0f896
AM
5342 /* For 8 bit registers we need an empty rex prefix. Also if the
5343 instruction already has a prefix, we need to convert old
5344 registers to new ones. */
773f551c 5345
bab6aec1 5346 if ((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte
29b0f896 5347 && (i.op[0].regs->reg_flags & RegRex64) != 0)
bab6aec1 5348 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte
29b0f896 5349 && (i.op[1].regs->reg_flags & RegRex64) != 0)
bab6aec1
JB
5350 || (((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte)
5351 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte))
29b0f896
AM
5352 && i.rex != 0))
5353 {
5354 int x;
726c5dcd 5355
29b0f896
AM
5356 i.rex |= REX_OPCODE;
5357 for (x = 0; x < 2; x++)
5358 {
5359 /* Look for 8 bit operand that uses old registers. */
bab6aec1 5360 if (i.types[x].bitfield.class == Reg && i.types[x].bitfield.byte
29b0f896 5361 && (i.op[x].regs->reg_flags & RegRex64) == 0)
773f551c 5362 {
3f93af61 5363 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
29b0f896
AM
5364 /* In case it is "hi" register, give up. */
5365 if (i.op[x].regs->reg_num > 3)
a540244d 5366 as_bad (_("can't encode register '%s%s' in an "
4eed87de 5367 "instruction requiring REX prefix."),
a540244d 5368 register_prefix, i.op[x].regs->reg_name);
773f551c 5369
29b0f896
AM
5370 /* Otherwise it is equivalent to the extended register.
5371 Since the encoding doesn't change this is merely
5372 cosmetic cleanup for debug output. */
5373
5374 i.op[x].regs = i.op[x].regs + 8;
773f551c 5375 }
29b0f896
AM
5376 }
5377 }
773f551c 5378
6b6b6807
L
5379 if (i.rex == 0 && i.rex_encoding)
5380 {
5381 /* Check if we can add a REX_OPCODE byte. Look for 8 bit operand
3f93af61 5382 that uses legacy register. If it is "hi" register, don't add
6b6b6807
L
5383 the REX_OPCODE byte. */
5384 int x;
5385 for (x = 0; x < 2; x++)
bab6aec1 5386 if (i.types[x].bitfield.class == Reg
6b6b6807
L
5387 && i.types[x].bitfield.byte
5388 && (i.op[x].regs->reg_flags & RegRex64) == 0
5389 && i.op[x].regs->reg_num > 3)
5390 {
3f93af61 5391 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
5b7c81bd 5392 i.rex_encoding = false;
6b6b6807
L
5393 break;
5394 }
5395
5396 if (i.rex_encoding)
5397 i.rex = REX_OPCODE;
5398 }
5399
7ab9ffdd 5400 if (i.rex != 0)
29b0f896
AM
5401 add_prefix (REX_OPCODE | i.rex);
5402
ae531041
L
5403 insert_lfence_before ();
5404
29b0f896
AM
5405 /* We are ready to output the insn. */
5406 output_insn ();
e379e5f3 5407
ae531041
L
5408 insert_lfence_after ();
5409
e379e5f3
L
5410 last_insn.seg = now_seg;
5411
5412 if (i.tm.opcode_modifier.isprefix)
5413 {
5414 last_insn.kind = last_insn_prefix;
76d3f746 5415 last_insn.name = insn_name (&i.tm);
e379e5f3
L
5416 last_insn.file = as_where (&last_insn.line);
5417 }
5418 else
5419 last_insn.kind = last_insn_other;
29b0f896
AM
5420}
5421
9db83a32
JB
5422/* The Q suffix is generally valid only in 64-bit mode, with very few
5423 exceptions: fild, fistp, fisttp, and cmpxchg8b. Note that for fild
5424 and fisttp only one of their two templates is matched below: That's
5425 sufficient since other relevant attributes are the same between both
5426 respective templates. */
5427static INLINE bool q_suffix_allowed(const insn_template *t)
5428{
5429 return flag_code == CODE_64BIT
ddb62495 5430 || (t->opcode_space == SPACE_BASE
9db83a32
JB
5431 && t->base_opcode == 0xdf
5432 && (t->extension_opcode & 1)) /* fild / fistp / fisttp */
7fc69528 5433 || t->mnem_off == MN_cmpxchg8b;
9db83a32
JB
5434}
5435
5317ad2c 5436static const char *
edd67638 5437parse_insn (const char *line, char *mnemonic, bool prefix_only)
29b0f896 5438{
5317ad2c 5439 const char *l = line, *token_start = l;
29b0f896 5440 char *mnem_p;
04784e33 5441 bool pass1 = !current_templates;
5c6af06e 5442 int supported;
d3ce72d0 5443 const insn_template *t;
b6169b20 5444 char *dot_p = NULL;
29b0f896 5445
29b0f896
AM
5446 while (1)
5447 {
5448 mnem_p = mnemonic;
5449 while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
5450 {
b6169b20
L
5451 if (*mnem_p == '.')
5452 dot_p = mnem_p;
29b0f896
AM
5453 mnem_p++;
5454 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
45288df1 5455 {
29b0f896
AM
5456 as_bad (_("no such instruction: `%s'"), token_start);
5457 return NULL;
5458 }
5459 l++;
5460 }
5461 if (!is_space_char (*l)
5462 && *l != END_OF_INSN
e44823cf
JB
5463 && (intel_syntax
5464 || (*l != PREFIX_SEPARATOR
5465 && *l != ',')))
29b0f896 5466 {
edd67638
JB
5467 if (prefix_only)
5468 break;
29b0f896
AM
5469 as_bad (_("invalid character %s in mnemonic"),
5470 output_invalid (*l));
5471 return NULL;
5472 }
5473 if (token_start == l)
5474 {
e44823cf 5475 if (!intel_syntax && *l == PREFIX_SEPARATOR)
29b0f896
AM
5476 as_bad (_("expecting prefix; got nothing"));
5477 else
5478 as_bad (_("expecting mnemonic; got nothing"));
5479 return NULL;
5480 }
45288df1 5481
29b0f896 5482 /* Look up instruction (or prefix) via hash table. */
629310ab 5483 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
47926f60 5484
29b0f896
AM
5485 if (*l != END_OF_INSN
5486 && (!is_space_char (*l) || l[1] != END_OF_INSN)
5487 && current_templates
40fb9820 5488 && current_templates->start->opcode_modifier.isprefix)
29b0f896 5489 {
c6fb90c8 5490 if (!cpu_flags_check_cpu64 (current_templates->start->cpu_flags))
2dd88dca
JB
5491 {
5492 as_bad ((flag_code != CODE_64BIT
5493 ? _("`%s' is only supported in 64-bit mode")
5494 : _("`%s' is not supported in 64-bit mode")),
76d3f746 5495 insn_name (current_templates->start));
2dd88dca
JB
5496 return NULL;
5497 }
29b0f896
AM
5498 /* If we are in 16-bit mode, do not allow addr16 or data16.
5499 Similarly, in 32-bit mode, do not allow addr32 or data32. */
673fe0f0
JB
5500 if ((current_templates->start->opcode_modifier.size == SIZE16
5501 || current_templates->start->opcode_modifier.size == SIZE32)
29b0f896 5502 && flag_code != CODE_64BIT
673fe0f0 5503 && ((current_templates->start->opcode_modifier.size == SIZE32)
29b0f896
AM
5504 ^ (flag_code == CODE_16BIT)))
5505 {
5506 as_bad (_("redundant %s prefix"),
76d3f746 5507 insn_name (current_templates->start));
29b0f896 5508 return NULL;
45288df1 5509 }
31184569
JB
5510
5511 if (current_templates->start->base_opcode == PSEUDO_PREFIX)
29b0f896 5512 {
86fa6981 5513 /* Handle pseudo prefixes. */
31184569 5514 switch (current_templates->start->extension_opcode)
86fa6981 5515 {
41eb8e88 5516 case Prefix_Disp8:
86fa6981
L
5517 /* {disp8} */
5518 i.disp_encoding = disp_encoding_8bit;
5519 break;
41eb8e88
L
5520 case Prefix_Disp16:
5521 /* {disp16} */
5522 i.disp_encoding = disp_encoding_16bit;
5523 break;
5524 case Prefix_Disp32:
86fa6981
L
5525 /* {disp32} */
5526 i.disp_encoding = disp_encoding_32bit;
5527 break;
41eb8e88 5528 case Prefix_Load:
86fa6981
L
5529 /* {load} */
5530 i.dir_encoding = dir_encoding_load;
5531 break;
41eb8e88 5532 case Prefix_Store:
86fa6981
L
5533 /* {store} */
5534 i.dir_encoding = dir_encoding_store;
5535 break;
41eb8e88 5536 case Prefix_VEX:
42e04b36
L
5537 /* {vex} */
5538 i.vec_encoding = vex_encoding_vex;
86fa6981 5539 break;
41eb8e88 5540 case Prefix_VEX3:
86fa6981
L
5541 /* {vex3} */
5542 i.vec_encoding = vex_encoding_vex3;
5543 break;
41eb8e88 5544 case Prefix_EVEX:
86fa6981
L
5545 /* {evex} */
5546 i.vec_encoding = vex_encoding_evex;
5547 break;
41eb8e88 5548 case Prefix_REX:
6b6b6807 5549 /* {rex} */
5b7c81bd 5550 i.rex_encoding = true;
6b6b6807 5551 break;
41eb8e88 5552 case Prefix_NoOptimize:
b6f8c7c4 5553 /* {nooptimize} */
5b7c81bd 5554 i.no_optimize = true;
b6f8c7c4 5555 break;
86fa6981
L
5556 default:
5557 abort ();
5558 }
5559 }
5560 else
5561 {
5562 /* Add prefix, checking for repeated prefixes. */
4e9ac44a 5563 switch (add_prefix (current_templates->start->base_opcode))
86fa6981 5564 {
4e9ac44a
L
5565 case PREFIX_EXIST:
5566 return NULL;
5567 case PREFIX_DS:
d777820b 5568 if (current_templates->start->cpu_flags.bitfield.cpuibt)
76d3f746 5569 i.notrack_prefix = insn_name (current_templates->start);
4e9ac44a
L
5570 break;
5571 case PREFIX_REP:
5572 if (current_templates->start->cpu_flags.bitfield.cpuhle)
76d3f746 5573 i.hle_prefix = insn_name (current_templates->start);
4e9ac44a 5574 else if (current_templates->start->cpu_flags.bitfield.cpumpx)
76d3f746 5575 i.bnd_prefix = insn_name (current_templates->start);
4e9ac44a 5576 else
76d3f746 5577 i.rep_prefix = insn_name (current_templates->start);
4e9ac44a
L
5578 break;
5579 default:
5580 break;
86fa6981 5581 }
29b0f896
AM
5582 }
5583 /* Skip past PREFIX_SEPARATOR and reset token_start. */
5584 token_start = ++l;
5585 }
5586 else
5587 break;
5588 }
45288df1 5589
edd67638
JB
5590 if (prefix_only)
5591 return token_start;
5592
30a55f88 5593 if (!current_templates)
b6169b20 5594 {
07d5e953
JB
5595 /* Deprecated functionality (new code should use pseudo-prefixes instead):
5596 Check if we should swap operand or force 32bit displacement in
f8a5c266 5597 encoding. */
30a55f88 5598 if (mnem_p - 2 == dot_p && dot_p[1] == 's')
64c49ab3 5599 i.dir_encoding = dir_encoding_swap;
8d63c93e 5600 else if (mnem_p - 3 == dot_p
a501d77e
L
5601 && dot_p[1] == 'd'
5602 && dot_p[2] == '8')
5603 i.disp_encoding = disp_encoding_8bit;
8d63c93e 5604 else if (mnem_p - 4 == dot_p
f8a5c266
L
5605 && dot_p[1] == 'd'
5606 && dot_p[2] == '3'
5607 && dot_p[3] == '2')
a501d77e 5608 i.disp_encoding = disp_encoding_32bit;
30a55f88
L
5609 else
5610 goto check_suffix;
5611 mnem_p = dot_p;
5612 *dot_p = '\0';
629310ab 5613 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
b6169b20
L
5614 }
5615
04784e33 5616 if (!current_templates || !pass1)
29b0f896 5617 {
04784e33
JB
5618 current_templates = NULL;
5619
dc1e8a47 5620 check_suffix:
1c529385 5621 if (mnem_p > mnemonic)
29b0f896 5622 {
1c529385
LH
5623 /* See if we can get a match by trimming off a suffix. */
5624 switch (mnem_p[-1])
29b0f896 5625 {
1c529385
LH
5626 case WORD_MNEM_SUFFIX:
5627 if (intel_syntax && (intel_float_operand (mnemonic) & 2))
29b0f896
AM
5628 i.suffix = SHORT_MNEM_SUFFIX;
5629 else
1c529385
LH
5630 /* Fall through. */
5631 case BYTE_MNEM_SUFFIX:
5632 case QWORD_MNEM_SUFFIX:
5633 i.suffix = mnem_p[-1];
29b0f896 5634 mnem_p[-1] = '\0';
fe0e921f
AM
5635 current_templates
5636 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5637 break;
5638 case SHORT_MNEM_SUFFIX:
5639 case LONG_MNEM_SUFFIX:
5640 if (!intel_syntax)
5641 {
5642 i.suffix = mnem_p[-1];
5643 mnem_p[-1] = '\0';
fe0e921f
AM
5644 current_templates
5645 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5646 }
5647 break;
5648
5649 /* Intel Syntax. */
5650 case 'd':
5651 if (intel_syntax)
5652 {
5653 if (intel_float_operand (mnemonic) == 1)
5654 i.suffix = SHORT_MNEM_SUFFIX;
5655 else
5656 i.suffix = LONG_MNEM_SUFFIX;
5657 mnem_p[-1] = '\0';
fe0e921f
AM
5658 current_templates
5659 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385 5660 }
04784e33
JB
5661 /* For compatibility reasons accept MOVSD and CMPSD without
5662 operands even in AT&T mode. */
5663 else if (*l == END_OF_INSN
5664 || (is_space_char (*l) && l[1] == END_OF_INSN))
5665 {
5666 mnem_p[-1] = '\0';
5667 current_templates
5668 = (const templates *) str_hash_find (op_hash, mnemonic);
5669 if (current_templates != NULL
5670 /* MOVS or CMPS */
5671 && (current_templates->start->base_opcode | 2) == 0xa6
ddb62495 5672 && current_templates->start->opcode_space
04784e33
JB
5673 == SPACE_BASE
5674 && mnem_p[-2] == 's')
5675 {
5676 as_warn (_("found `%sd'; assuming `%sl' was meant"),
5677 mnemonic, mnemonic);
5678 i.suffix = LONG_MNEM_SUFFIX;
5679 }
5680 else
5681 {
5682 current_templates = NULL;
5683 mnem_p[-1] = 'd';
5684 }
5685 }
1c529385 5686 break;
29b0f896 5687 }
29b0f896 5688 }
1c529385 5689
29b0f896
AM
5690 if (!current_templates)
5691 {
04784e33
JB
5692 if (pass1)
5693 as_bad (_("no such instruction: `%s'"), token_start);
29b0f896
AM
5694 return NULL;
5695 }
5696 }
252b5132 5697
0cfa3eb3
JB
5698 if (current_templates->start->opcode_modifier.jump == JUMP
5699 || current_templates->start->opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
5700 {
5701 /* Check for a branch hint. We allow ",pt" and ",pn" for
5702 predict taken and predict not taken respectively.
5703 I'm not sure that branch hints actually do anything on loop
5704 and jcxz insns (JumpByte) for current Pentium4 chips. They
5705 may work in the future and it doesn't hurt to accept them
5706 now. */
5707 if (l[0] == ',' && l[1] == 'p')
5708 {
5709 if (l[2] == 't')
5710 {
5711 if (!add_prefix (DS_PREFIX_OPCODE))
5712 return NULL;
5713 l += 3;
5714 }
5715 else if (l[2] == 'n')
5716 {
5717 if (!add_prefix (CS_PREFIX_OPCODE))
5718 return NULL;
5719 l += 3;
5720 }
5721 }
5722 }
5723 /* Any other comma loses. */
5724 if (*l == ',')
5725 {
5726 as_bad (_("invalid character %s in mnemonic"),
5727 output_invalid (*l));
5728 return NULL;
5729 }
252b5132 5730
29b0f896 5731 /* Check if instruction is supported on specified architecture. */
5c6af06e
JB
5732 supported = 0;
5733 for (t = current_templates->start; t < current_templates->end; ++t)
5734 {
c0f3af97 5735 supported |= cpu_flags_match (t);
9db83a32
JB
5736
5737 if (i.suffix == QWORD_MNEM_SUFFIX && !q_suffix_allowed (t))
5738 supported &= ~CPU_FLAGS_64BIT_MATCH;
5739
c0f3af97 5740 if (supported == CPU_FLAGS_PERFECT_MATCH)
d59a54c2 5741 return l;
29b0f896 5742 }
3629bb00 5743
9db83a32
JB
5744 if (pass1)
5745 {
5746 if (supported & CPU_FLAGS_64BIT_MATCH)
5747 i.error = unsupported_on_arch;
5748 else
5749 i.error = unsupported_64bit;
5750 }
252b5132 5751
548d0ee6 5752 return NULL;
29b0f896 5753}
252b5132 5754
29b0f896 5755static char *
e3bb37b5 5756parse_operands (char *l, const char *mnemonic)
29b0f896
AM
5757{
5758 char *token_start;
3138f287 5759
29b0f896
AM
5760 /* 1 if operand is pending after ','. */
5761 unsigned int expecting_operand = 0;
252b5132 5762
29b0f896
AM
5763 while (*l != END_OF_INSN)
5764 {
e68c3d59
JB
5765 /* Non-zero if operand parens not balanced. */
5766 unsigned int paren_not_balanced = 0;
5767 /* True if inside double quotes. */
5768 bool in_quotes = false;
5769
29b0f896
AM
5770 /* Skip optional white space before operand. */
5771 if (is_space_char (*l))
5772 ++l;
d02603dc 5773 if (!is_operand_char (*l) && *l != END_OF_INSN && *l != '"')
29b0f896
AM
5774 {
5775 as_bad (_("invalid character %s before operand %d"),
5776 output_invalid (*l),
5777 i.operands + 1);
5778 return NULL;
5779 }
d02603dc 5780 token_start = l; /* After white space. */
e68c3d59 5781 while (in_quotes || paren_not_balanced || *l != ',')
29b0f896
AM
5782 {
5783 if (*l == END_OF_INSN)
5784 {
e68c3d59
JB
5785 if (in_quotes)
5786 {
5787 as_bad (_("unbalanced double quotes in operand %d."),
5788 i.operands + 1);
5789 return NULL;
5790 }
29b0f896
AM
5791 if (paren_not_balanced)
5792 {
98ff9f1c
JB
5793 know (!intel_syntax);
5794 as_bad (_("unbalanced parenthesis in operand %d."),
5795 i.operands + 1);
29b0f896
AM
5796 return NULL;
5797 }
5798 else
5799 break; /* we are done */
5800 }
e68c3d59
JB
5801 else if (*l == '\\' && l[1] == '"')
5802 ++l;
5803 else if (*l == '"')
5804 in_quotes = !in_quotes;
5805 else if (!in_quotes && !is_operand_char (*l) && !is_space_char (*l))
29b0f896
AM
5806 {
5807 as_bad (_("invalid character %s in operand %d"),
5808 output_invalid (*l),
5809 i.operands + 1);
5810 return NULL;
5811 }
e68c3d59 5812 if (!intel_syntax && !in_quotes)
29b0f896
AM
5813 {
5814 if (*l == '(')
5815 ++paren_not_balanced;
5816 if (*l == ')')
5817 --paren_not_balanced;
5818 }
29b0f896
AM
5819 l++;
5820 }
5821 if (l != token_start)
5822 { /* Yes, we've read in another operand. */
5823 unsigned int operand_ok;
5824 this_operand = i.operands++;
5825 if (i.operands > MAX_OPERANDS)
5826 {
5827 as_bad (_("spurious operands; (%d operands/instruction max)"),
5828 MAX_OPERANDS);
5829 return NULL;
5830 }
9d46ce34 5831 i.types[this_operand].bitfield.unspecified = 1;
29b0f896
AM
5832 /* Now parse operand adding info to 'i' as we go along. */
5833 END_STRING_AND_SAVE (l);
5834
1286ab78
L
5835 if (i.mem_operands > 1)
5836 {
5837 as_bad (_("too many memory references for `%s'"),
5838 mnemonic);
5839 return 0;
5840 }
5841
29b0f896
AM
5842 if (intel_syntax)
5843 operand_ok =
5844 i386_intel_operand (token_start,
5845 intel_float_operand (mnemonic));
5846 else
a7619375 5847 operand_ok = i386_att_operand (token_start);
29b0f896
AM
5848
5849 RESTORE_END_STRING (l);
5850 if (!operand_ok)
5851 return NULL;
5852 }
5853 else
5854 {
5855 if (expecting_operand)
5856 {
5857 expecting_operand_after_comma:
5858 as_bad (_("expecting operand after ','; got nothing"));
5859 return NULL;
5860 }
5861 if (*l == ',')
5862 {
5863 as_bad (_("expecting operand before ','; got nothing"));
5864 return NULL;
5865 }
5866 }
7f3f1ea2 5867
29b0f896
AM
5868 /* Now *l must be either ',' or END_OF_INSN. */
5869 if (*l == ',')
5870 {
5871 if (*++l == END_OF_INSN)
5872 {
5873 /* Just skip it, if it's \n complain. */
5874 goto expecting_operand_after_comma;
5875 }
5876 expecting_operand = 1;
5877 }
5878 }
5879 return l;
5880}
7f3f1ea2 5881
050dfa73 5882static void
783c187b 5883swap_2_operands (unsigned int xchg1, unsigned int xchg2)
050dfa73
MM
5884{
5885 union i386_op temp_op;
40fb9820 5886 i386_operand_type temp_type;
c48dadc9 5887 unsigned int temp_flags;
050dfa73 5888 enum bfd_reloc_code_real temp_reloc;
4eed87de 5889
050dfa73
MM
5890 temp_type = i.types[xchg2];
5891 i.types[xchg2] = i.types[xchg1];
5892 i.types[xchg1] = temp_type;
c48dadc9
JB
5893
5894 temp_flags = i.flags[xchg2];
5895 i.flags[xchg2] = i.flags[xchg1];
5896 i.flags[xchg1] = temp_flags;
5897
050dfa73
MM
5898 temp_op = i.op[xchg2];
5899 i.op[xchg2] = i.op[xchg1];
5900 i.op[xchg1] = temp_op;
c48dadc9 5901
050dfa73
MM
5902 temp_reloc = i.reloc[xchg2];
5903 i.reloc[xchg2] = i.reloc[xchg1];
5904 i.reloc[xchg1] = temp_reloc;
43234a1e 5905
6225c532 5906 if (i.mask.reg)
43234a1e 5907 {
6225c532
JB
5908 if (i.mask.operand == xchg1)
5909 i.mask.operand = xchg2;
5910 else if (i.mask.operand == xchg2)
5911 i.mask.operand = xchg1;
43234a1e 5912 }
a5748e0d 5913 if (i.broadcast.type || i.broadcast.bytes)
43234a1e 5914 {
5273a3cd
JB
5915 if (i.broadcast.operand == xchg1)
5916 i.broadcast.operand = xchg2;
5917 else if (i.broadcast.operand == xchg2)
5918 i.broadcast.operand = xchg1;
43234a1e 5919 }
050dfa73
MM
5920}
5921
29b0f896 5922static void
e3bb37b5 5923swap_operands (void)
29b0f896 5924{
b7c61d9a 5925 switch (i.operands)
050dfa73 5926 {
c0f3af97 5927 case 5:
b7c61d9a 5928 case 4:
4d456e3d 5929 swap_2_operands (1, i.operands - 2);
1a0670f3 5930 /* Fall through. */
b7c61d9a
L
5931 case 3:
5932 case 2:
4d456e3d 5933 swap_2_operands (0, i.operands - 1);
b7c61d9a
L
5934 break;
5935 default:
5936 abort ();
29b0f896 5937 }
29b0f896
AM
5938
5939 if (i.mem_operands == 2)
5940 {
5e042380 5941 const reg_entry *temp_seg;
29b0f896
AM
5942 temp_seg = i.seg[0];
5943 i.seg[0] = i.seg[1];
5944 i.seg[1] = temp_seg;
5945 }
5946}
252b5132 5947
29b0f896
AM
5948/* Try to ensure constant immediates are represented in the smallest
5949 opcode possible. */
5950static void
e3bb37b5 5951optimize_imm (void)
29b0f896
AM
5952{
5953 char guess_suffix = 0;
5954 int op;
252b5132 5955
29b0f896
AM
5956 if (i.suffix)
5957 guess_suffix = i.suffix;
5958 else if (i.reg_operands)
5959 {
5960 /* Figure out a suffix from the last register operand specified.
75e5731b
JB
5961 We can't do this properly yet, i.e. excluding special register
5962 instances, but the following works for instructions with
5963 immediates. In any case, we can't set i.suffix yet. */
29b0f896 5964 for (op = i.operands; --op >= 0;)
bab6aec1
JB
5965 if (i.types[op].bitfield.class != Reg)
5966 continue;
5967 else if (i.types[op].bitfield.byte)
7ab9ffdd 5968 {
40fb9820
L
5969 guess_suffix = BYTE_MNEM_SUFFIX;
5970 break;
5971 }
bab6aec1 5972 else if (i.types[op].bitfield.word)
252b5132 5973 {
40fb9820
L
5974 guess_suffix = WORD_MNEM_SUFFIX;
5975 break;
5976 }
bab6aec1 5977 else if (i.types[op].bitfield.dword)
40fb9820
L
5978 {
5979 guess_suffix = LONG_MNEM_SUFFIX;
5980 break;
5981 }
bab6aec1 5982 else if (i.types[op].bitfield.qword)
40fb9820
L
5983 {
5984 guess_suffix = QWORD_MNEM_SUFFIX;
29b0f896 5985 break;
252b5132 5986 }
29b0f896
AM
5987 }
5988 else if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
5989 guess_suffix = WORD_MNEM_SUFFIX;
5990
5991 for (op = i.operands; --op >= 0;)
40fb9820 5992 if (operand_type_check (i.types[op], imm))
29b0f896
AM
5993 {
5994 switch (i.op[op].imms->X_op)
252b5132 5995 {
29b0f896
AM
5996 case O_constant:
5997 /* If a suffix is given, this operand may be shortened. */
5998 switch (guess_suffix)
252b5132 5999 {
29b0f896 6000 case LONG_MNEM_SUFFIX:
40fb9820
L
6001 i.types[op].bitfield.imm32 = 1;
6002 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
6003 break;
6004 case WORD_MNEM_SUFFIX:
40fb9820
L
6005 i.types[op].bitfield.imm16 = 1;
6006 i.types[op].bitfield.imm32 = 1;
6007 i.types[op].bitfield.imm32s = 1;
6008 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
6009 break;
6010 case BYTE_MNEM_SUFFIX:
40fb9820
L
6011 i.types[op].bitfield.imm8 = 1;
6012 i.types[op].bitfield.imm8s = 1;
6013 i.types[op].bitfield.imm16 = 1;
6014 i.types[op].bitfield.imm32 = 1;
6015 i.types[op].bitfield.imm32s = 1;
6016 i.types[op].bitfield.imm64 = 1;
29b0f896 6017 break;
252b5132 6018 }
252b5132 6019
29b0f896
AM
6020 /* If this operand is at most 16 bits, convert it
6021 to a signed 16 bit number before trying to see
6022 whether it will fit in an even smaller size.
6023 This allows a 16-bit operand such as $0xffe0 to
6024 be recognised as within Imm8S range. */
40fb9820 6025 if ((i.types[op].bitfield.imm16)
7e96fb68 6026 && fits_in_unsigned_word (i.op[op].imms->X_add_number))
252b5132 6027 {
87ed972d
JB
6028 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
6029 ^ 0x8000) - 0x8000);
29b0f896 6030 }
a28def75
L
6031#ifdef BFD64
6032 /* Store 32-bit immediate in 64-bit for 64-bit BFD. */
40fb9820 6033 if ((i.types[op].bitfield.imm32)
7e96fb68 6034 && fits_in_unsigned_long (i.op[op].imms->X_add_number))
29b0f896
AM
6035 {
6036 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
6037 ^ ((offsetT) 1 << 31))
6038 - ((offsetT) 1 << 31));
6039 }
a28def75 6040#endif
40fb9820 6041 i.types[op]
c6fb90c8
L
6042 = operand_type_or (i.types[op],
6043 smallest_imm_type (i.op[op].imms->X_add_number));
252b5132 6044
29b0f896
AM
6045 /* We must avoid matching of Imm32 templates when 64bit
6046 only immediate is available. */
6047 if (guess_suffix == QWORD_MNEM_SUFFIX)
40fb9820 6048 i.types[op].bitfield.imm32 = 0;
29b0f896 6049 break;
252b5132 6050
29b0f896
AM
6051 case O_absent:
6052 case O_register:
6053 abort ();
6054
6055 /* Symbols and expressions. */
6056 default:
9cd96992
JB
6057 /* Convert symbolic operand to proper sizes for matching, but don't
6058 prevent matching a set of insns that only supports sizes other
6059 than those matching the insn suffix. */
6060 {
40fb9820 6061 i386_operand_type mask, allowed;
87ed972d 6062 const insn_template *t = current_templates->start;
9cd96992 6063
0dfbf9d7 6064 operand_type_set (&mask, 0);
9cd96992
JB
6065 switch (guess_suffix)
6066 {
6067 case QWORD_MNEM_SUFFIX:
40fb9820
L
6068 mask.bitfield.imm64 = 1;
6069 mask.bitfield.imm32s = 1;
9cd96992
JB
6070 break;
6071 case LONG_MNEM_SUFFIX:
40fb9820 6072 mask.bitfield.imm32 = 1;
9cd96992
JB
6073 break;
6074 case WORD_MNEM_SUFFIX:
40fb9820 6075 mask.bitfield.imm16 = 1;
9cd96992
JB
6076 break;
6077 case BYTE_MNEM_SUFFIX:
40fb9820 6078 mask.bitfield.imm8 = 1;
9cd96992
JB
6079 break;
6080 default:
9cd96992
JB
6081 break;
6082 }
8f0212ac
JB
6083
6084 allowed = operand_type_and (t->operand_types[op], mask);
6085 while (++t < current_templates->end)
6086 {
6087 allowed = operand_type_or (allowed, t->operand_types[op]);
6088 allowed = operand_type_and (allowed, mask);
6089 }
6090
0dfbf9d7 6091 if (!operand_type_all_zero (&allowed))
c6fb90c8 6092 i.types[op] = operand_type_and (i.types[op], mask);
9cd96992 6093 }
29b0f896 6094 break;
252b5132 6095 }
29b0f896
AM
6096 }
6097}
47926f60 6098
29b0f896 6099/* Try to use the smallest displacement type too. */
0de704b9
JB
6100static bool
6101optimize_disp (const insn_template *t)
29b0f896 6102{
0de704b9 6103 unsigned int op;
3e73aa7c 6104
0de704b9
JB
6105 if (!want_disp32 (t)
6106 && (!t->opcode_modifier.jump
6107 || i.jumpabsolute || i.types[0].bitfield.baseindex))
6108 {
6109 for (op = 0; op < i.operands; ++op)
6110 {
6111 const expressionS *exp = i.op[op].disps;
6112
6113 if (!operand_type_check (i.types[op], disp))
6114 continue;
6115
6116 if (exp->X_op != O_constant)
6117 continue;
6118
6119 /* Since displacement is signed extended to 64bit, don't allow
6120 disp32 if it is out of range. */
6121 if (fits_in_signed_long (exp->X_add_number))
6122 continue;
6123
6124 i.types[op].bitfield.disp32 = 0;
6125 if (i.types[op].bitfield.baseindex)
6126 {
6127 as_bad (_("0x%" PRIx64 " out of range of signed 32bit displacement"),
6128 (uint64_t) exp->X_add_number);
6129 return false;
6130 }
6131 }
6132 }
6133
6134 /* Don't optimize displacement for movabs since it only takes 64bit
6135 displacement. */
6136 if (i.disp_encoding > disp_encoding_8bit
6137 || (flag_code == CODE_64BIT && t->mnem_off == MN_movabs))
6138 return true;
6139
6140 for (op = i.operands; op-- > 0;)
40fb9820 6141 if (operand_type_check (i.types[op], disp))
252b5132 6142 {
b300c311 6143 if (i.op[op].disps->X_op == O_constant)
252b5132 6144 {
91d6fa6a 6145 offsetT op_disp = i.op[op].disps->X_add_number;
29b0f896 6146
91d6fa6a 6147 if (!op_disp && i.types[op].bitfield.baseindex)
b300c311 6148 {
2f2be86b
JB
6149 i.types[op] = operand_type_and_not (i.types[op], anydisp);
6150 i.op[op].disps = NULL;
b300c311 6151 i.disp_operands--;
f185acdd
JB
6152 continue;
6153 }
6154
6155 if (i.types[op].bitfield.disp16
cd613c1f 6156 && fits_in_unsigned_word (op_disp))
f185acdd
JB
6157 {
6158 /* If this operand is at most 16 bits, convert
6159 to a signed 16 bit number and don't use 64bit
6160 displacement. */
6161 op_disp = ((op_disp ^ 0x8000) - 0x8000);
6162 i.types[op].bitfield.disp64 = 0;
b300c311 6163 }
f185acdd 6164
28a167a4 6165#ifdef BFD64
a50187b2 6166 /* Optimize 64-bit displacement to 32-bit for 64-bit BFD. */
a775efc8
JB
6167 if ((flag_code != CODE_64BIT
6168 ? i.types[op].bitfield.disp32
0de704b9
JB
6169 : want_disp32 (t)
6170 && (!t->opcode_modifier.jump
a775efc8 6171 || i.jumpabsolute || i.types[op].bitfield.baseindex))
a50187b2 6172 && fits_in_unsigned_long (op_disp))
b300c311 6173 {
a50187b2
JB
6174 /* If this operand is at most 32 bits, convert
6175 to a signed 32 bit number and don't use 64bit
6176 displacement. */
6177 op_disp = (op_disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
6178 i.types[op].bitfield.disp64 = 0;
6179 i.types[op].bitfield.disp32 = 1;
6180 }
28a167a4 6181
a50187b2
JB
6182 if (flag_code == CODE_64BIT && fits_in_signed_long (op_disp))
6183 {
6184 i.types[op].bitfield.disp64 = 0;
a775efc8 6185 i.types[op].bitfield.disp32 = 1;
b300c311 6186 }
28a167a4 6187#endif
40fb9820 6188 if ((i.types[op].bitfield.disp32
40fb9820 6189 || i.types[op].bitfield.disp16)
b5014f7a 6190 && fits_in_disp8 (op_disp))
40fb9820 6191 i.types[op].bitfield.disp8 = 1;
77c59789
JB
6192
6193 i.op[op].disps->X_add_number = op_disp;
252b5132 6194 }
67a4f2b7
AO
6195 else if (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
6196 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL)
6197 {
6198 fix_new_exp (frag_now, frag_more (0) - frag_now->fr_literal, 0,
6199 i.op[op].disps, 0, i.reloc[op]);
2f2be86b 6200 i.types[op] = operand_type_and_not (i.types[op], anydisp);
67a4f2b7
AO
6201 }
6202 else
b300c311 6203 /* We only support 64bit displacement on constants. */
40fb9820 6204 i.types[op].bitfield.disp64 = 0;
252b5132 6205 }
0de704b9
JB
6206
6207 return true;
29b0f896
AM
6208}
6209
4a1b91ea
L
6210/* Return 1 if there is a match in broadcast bytes between operand
6211 GIVEN and instruction template T. */
6212
6213static INLINE int
6214match_broadcast_size (const insn_template *t, unsigned int given)
6215{
6216 return ((t->opcode_modifier.broadcast == BYTE_BROADCAST
6217 && i.types[given].bitfield.byte)
6218 || (t->opcode_modifier.broadcast == WORD_BROADCAST
6219 && i.types[given].bitfield.word)
6220 || (t->opcode_modifier.broadcast == DWORD_BROADCAST
6221 && i.types[given].bitfield.dword)
6222 || (t->opcode_modifier.broadcast == QWORD_BROADCAST
6223 && i.types[given].bitfield.qword));
6224}
6225
6c30d220
L
6226/* Check if operands are valid for the instruction. */
6227
6228static int
6229check_VecOperands (const insn_template *t)
6230{
43234a1e 6231 unsigned int op;
e2195274 6232 i386_cpu_flags cpu;
e2195274
JB
6233
6234 /* Templates allowing for ZMMword as well as YMMword and/or XMMword for
6235 any one operand are implicity requiring AVX512VL support if the actual
6236 operand size is YMMword or XMMword. Since this function runs after
6237 template matching, there's no need to check for YMMword/XMMword in
6238 the template. */
6239 cpu = cpu_flags_and (t->cpu_flags, avx512);
6240 if (!cpu_flags_all_zero (&cpu)
6241 && !t->cpu_flags.bitfield.cpuavx512vl
6242 && !cpu_arch_flags.bitfield.cpuavx512vl)
6243 {
6244 for (op = 0; op < t->operands; ++op)
6245 {
6246 if (t->operand_types[op].bitfield.zmmword
6247 && (i.types[op].bitfield.ymmword
6248 || i.types[op].bitfield.xmmword))
6249 {
6250 i.error = unsupported;
6251 return 1;
6252 }
6253 }
6254 }
43234a1e 6255
22c36940
JB
6256 /* Somewhat similarly, templates specifying both AVX and AVX2 are
6257 requiring AVX2 support if the actual operand size is YMMword. */
6258 if (t->cpu_flags.bitfield.cpuavx
6259 && t->cpu_flags.bitfield.cpuavx2
6260 && !cpu_arch_flags.bitfield.cpuavx2)
6261 {
6262 for (op = 0; op < t->operands; ++op)
6263 {
6264 if (t->operand_types[op].bitfield.xmmword
6265 && i.types[op].bitfield.ymmword)
6266 {
6267 i.error = unsupported;
6268 return 1;
6269 }
6270 }
6271 }
6272
6c30d220 6273 /* Without VSIB byte, we can't have a vector register for index. */
63112cd6 6274 if (!t->opcode_modifier.sib
6c30d220 6275 && i.index_reg
1b54b8d7
JB
6276 && (i.index_reg->reg_type.bitfield.xmmword
6277 || i.index_reg->reg_type.bitfield.ymmword
6278 || i.index_reg->reg_type.bitfield.zmmword))
6c30d220
L
6279 {
6280 i.error = unsupported_vector_index_register;
6281 return 1;
6282 }
6283
ad8ecc81 6284 /* Check if default mask is allowed. */
255571cd 6285 if (t->opcode_modifier.operandconstraint == NO_DEFAULT_MASK
6225c532 6286 && (!i.mask.reg || i.mask.reg->reg_num == 0))
ad8ecc81
MZ
6287 {
6288 i.error = no_default_mask;
6289 return 1;
6290 }
6291
7bab8ab5
JB
6292 /* For VSIB byte, we need a vector register for index, and all vector
6293 registers must be distinct. */
260cd341 6294 if (t->opcode_modifier.sib && t->opcode_modifier.sib != SIBMEM)
7bab8ab5
JB
6295 {
6296 if (!i.index_reg
63112cd6 6297 || !((t->opcode_modifier.sib == VECSIB128
1b54b8d7 6298 && i.index_reg->reg_type.bitfield.xmmword)
63112cd6 6299 || (t->opcode_modifier.sib == VECSIB256
1b54b8d7 6300 && i.index_reg->reg_type.bitfield.ymmword)
63112cd6 6301 || (t->opcode_modifier.sib == VECSIB512
1b54b8d7 6302 && i.index_reg->reg_type.bitfield.zmmword)))
7bab8ab5
JB
6303 {
6304 i.error = invalid_vsib_address;
6305 return 1;
6306 }
6307
6225c532
JB
6308 gas_assert (i.reg_operands == 2 || i.mask.reg);
6309 if (i.reg_operands == 2 && !i.mask.reg)
43234a1e 6310 {
3528c362 6311 gas_assert (i.types[0].bitfield.class == RegSIMD);
1b54b8d7
JB
6312 gas_assert (i.types[0].bitfield.xmmword
6313 || i.types[0].bitfield.ymmword);
3528c362 6314 gas_assert (i.types[2].bitfield.class == RegSIMD);
1b54b8d7
JB
6315 gas_assert (i.types[2].bitfield.xmmword
6316 || i.types[2].bitfield.ymmword);
43234a1e
L
6317 if (operand_check == check_none)
6318 return 0;
6319 if (register_number (i.op[0].regs)
6320 != register_number (i.index_reg)
6321 && register_number (i.op[2].regs)
6322 != register_number (i.index_reg)
6323 && register_number (i.op[0].regs)
6324 != register_number (i.op[2].regs))
6325 return 0;
6326 if (operand_check == check_error)
6327 {
6328 i.error = invalid_vector_register_set;
6329 return 1;
6330 }
6331 as_warn (_("mask, index, and destination registers should be distinct"));
6332 }
6225c532 6333 else if (i.reg_operands == 1 && i.mask.reg)
8444f82a 6334 {
3528c362 6335 if (i.types[1].bitfield.class == RegSIMD
1b54b8d7
JB
6336 && (i.types[1].bitfield.xmmword
6337 || i.types[1].bitfield.ymmword
6338 || i.types[1].bitfield.zmmword)
8444f82a
MZ
6339 && (register_number (i.op[1].regs)
6340 == register_number (i.index_reg)))
6341 {
6342 if (operand_check == check_error)
6343 {
6344 i.error = invalid_vector_register_set;
6345 return 1;
6346 }
6347 if (operand_check != check_none)
6348 as_warn (_("index and destination registers should be distinct"));
6349 }
6350 }
43234a1e 6351 }
7bab8ab5 6352
fc141319
L
6353 /* For AMX instructions with 3 TMM register operands, all operands
6354 must be distinct. */
6355 if (i.reg_operands == 3
6356 && t->operand_types[0].bitfield.tmmword
6357 && (i.op[0].regs == i.op[1].regs
6358 || i.op[0].regs == i.op[2].regs
6359 || i.op[1].regs == i.op[2].regs))
6360 {
6361 i.error = invalid_tmm_register_set;
6362 return 1;
260cd341
LC
6363 }
6364
0cc78721
CL
6365 /* For some special instructions require that destination must be distinct
6366 from source registers. */
255571cd 6367 if (t->opcode_modifier.operandconstraint == DISTINCT_DEST)
0cc78721
CL
6368 {
6369 unsigned int dest_reg = i.operands - 1;
6370
6371 know (i.operands >= 3);
6372
6373 /* #UD if dest_reg == src1_reg or dest_reg == src2_reg. */
6374 if (i.op[dest_reg - 1].regs == i.op[dest_reg].regs
6375 || (i.reg_operands > 2
6376 && i.op[dest_reg - 2].regs == i.op[dest_reg].regs))
6377 {
6378 i.error = invalid_dest_and_src_register_set;
6379 return 1;
6380 }
6381 }
6382
43234a1e
L
6383 /* Check if broadcast is supported by the instruction and is applied
6384 to the memory operand. */
a5748e0d 6385 if (i.broadcast.type || i.broadcast.bytes)
43234a1e 6386 {
8e6e0792 6387 i386_operand_type type, overlap;
43234a1e
L
6388
6389 /* Check if specified broadcast is supported in this instruction,
4a1b91ea 6390 and its broadcast bytes match the memory operand. */
5273a3cd 6391 op = i.broadcast.operand;
8e6e0792 6392 if (!t->opcode_modifier.broadcast
c48dadc9 6393 || !(i.flags[op] & Operand_Mem)
c39e5b26 6394 || (!i.types[op].bitfield.unspecified
4a1b91ea 6395 && !match_broadcast_size (t, op)))
43234a1e
L
6396 {
6397 bad_broadcast:
6398 i.error = unsupported_broadcast;
6399 return 1;
6400 }
8e6e0792
JB
6401
6402 operand_type_set (&type, 0);
a5748e0d 6403 switch (get_broadcast_bytes (t, false))
8e6e0792 6404 {
4a1b91ea
L
6405 case 2:
6406 type.bitfield.word = 1;
6407 break;
6408 case 4:
6409 type.bitfield.dword = 1;
6410 break;
8e6e0792
JB
6411 case 8:
6412 type.bitfield.qword = 1;
6413 break;
6414 case 16:
6415 type.bitfield.xmmword = 1;
6416 break;
6417 case 32:
6418 type.bitfield.ymmword = 1;
6419 break;
6420 case 64:
6421 type.bitfield.zmmword = 1;
6422 break;
6423 default:
6424 goto bad_broadcast;
6425 }
6426
6427 overlap = operand_type_and (type, t->operand_types[op]);
bc49bfd8
JB
6428 if (t->operand_types[op].bitfield.class == RegSIMD
6429 && t->operand_types[op].bitfield.byte
6430 + t->operand_types[op].bitfield.word
6431 + t->operand_types[op].bitfield.dword
6432 + t->operand_types[op].bitfield.qword > 1)
6433 {
6434 overlap.bitfield.xmmword = 0;
6435 overlap.bitfield.ymmword = 0;
6436 overlap.bitfield.zmmword = 0;
6437 }
8e6e0792
JB
6438 if (operand_type_all_zero (&overlap))
6439 goto bad_broadcast;
6440
9c19e9ec 6441 if (t->opcode_modifier.checkoperandsize)
8e6e0792
JB
6442 {
6443 unsigned int j;
6444
e2195274 6445 type.bitfield.baseindex = 1;
8e6e0792
JB
6446 for (j = 0; j < i.operands; ++j)
6447 {
6448 if (j != op
6449 && !operand_type_register_match(i.types[j],
6450 t->operand_types[j],
6451 type,
6452 t->operand_types[op]))
6453 goto bad_broadcast;
6454 }
6455 }
43234a1e
L
6456 }
6457 /* If broadcast is supported in this instruction, we need to check if
6458 operand of one-element size isn't specified without broadcast. */
6459 else if (t->opcode_modifier.broadcast && i.mem_operands)
6460 {
6461 /* Find memory operand. */
6462 for (op = 0; op < i.operands; op++)
8dc0818e 6463 if (i.flags[op] & Operand_Mem)
43234a1e
L
6464 break;
6465 gas_assert (op < i.operands);
6466 /* Check size of the memory operand. */
4a1b91ea 6467 if (match_broadcast_size (t, op))
43234a1e
L
6468 {
6469 i.error = broadcast_needed;
6470 return 1;
6471 }
6472 }
c39e5b26
JB
6473 else
6474 op = MAX_OPERANDS - 1; /* Avoid uninitialized variable warning. */
43234a1e
L
6475
6476 /* Check if requested masking is supported. */
6225c532 6477 if (i.mask.reg)
43234a1e 6478 {
ae2387fe
JB
6479 switch (t->opcode_modifier.masking)
6480 {
6481 case BOTH_MASKING:
6482 break;
6483 case MERGING_MASKING:
6225c532 6484 if (i.mask.zeroing)
ae2387fe
JB
6485 {
6486 case 0:
6487 i.error = unsupported_masking;
6488 return 1;
6489 }
6490 break;
6491 case DYNAMIC_MASKING:
6492 /* Memory destinations allow only merging masking. */
6225c532 6493 if (i.mask.zeroing && i.mem_operands)
ae2387fe
JB
6494 {
6495 /* Find memory operand. */
6496 for (op = 0; op < i.operands; op++)
c48dadc9 6497 if (i.flags[op] & Operand_Mem)
ae2387fe
JB
6498 break;
6499 gas_assert (op < i.operands);
6500 if (op == i.operands - 1)
6501 {
6502 i.error = unsupported_masking;
6503 return 1;
6504 }
6505 }
6506 break;
6507 default:
6508 abort ();
6509 }
43234a1e
L
6510 }
6511
6512 /* Check if masking is applied to dest operand. */
6225c532 6513 if (i.mask.reg && (i.mask.operand != i.operands - 1))
43234a1e
L
6514 {
6515 i.error = mask_not_on_destination;
6516 return 1;
6517 }
6518
43234a1e 6519 /* Check RC/SAE. */
ca5312a2 6520 if (i.rounding.type != rc_none)
43234a1e 6521 {
a80195f1 6522 if (!t->opcode_modifier.sae
cf665fee
JB
6523 || ((i.rounding.type != saeonly) != t->opcode_modifier.staticrounding)
6524 || i.mem_operands)
43234a1e
L
6525 {
6526 i.error = unsupported_rc_sae;
6527 return 1;
6528 }
cf665fee
JB
6529
6530 /* Non-EVEX.LIG forms need to have a ZMM register as at least one
6531 operand. */
6532 if (t->opcode_modifier.evex != EVEXLIG)
7bab8ab5 6533 {
cf665fee
JB
6534 for (op = 0; op < t->operands; ++op)
6535 if (i.types[op].bitfield.zmmword)
6536 break;
6537 if (op >= t->operands)
6538 {
6539 i.error = operand_size_mismatch;
6540 return 1;
6541 }
7bab8ab5 6542 }
6c30d220
L
6543 }
6544
da4977e0
JB
6545 /* Check the special Imm4 cases; must be the first operand. */
6546 if (t->cpu_flags.bitfield.cpuxop && t->operands == 5)
6547 {
6548 if (i.op[0].imms->X_op != O_constant
6549 || !fits_in_imm4 (i.op[0].imms->X_add_number))
6550 {
6551 i.error = bad_imm4;
6552 return 1;
6553 }
6554
6555 /* Turn off Imm<N> so that update_imm won't complain. */
6556 operand_type_set (&i.types[0], 0);
6557 }
6558
43234a1e 6559 /* Check vector Disp8 operand. */
b5014f7a 6560 if (t->opcode_modifier.disp8memshift
1a42a9fe 6561 && i.disp_encoding <= disp_encoding_8bit)
43234a1e 6562 {
9b345ce8 6563 if (i.broadcast.type || i.broadcast.bytes)
4a1b91ea 6564 i.memshift = t->opcode_modifier.broadcast - 1;
7091c612 6565 else if (t->opcode_modifier.disp8memshift != DISP8_SHIFT_VL)
43234a1e 6566 i.memshift = t->opcode_modifier.disp8memshift;
7091c612
JB
6567 else
6568 {
125ff819 6569 const i386_operand_type *type = NULL, *fallback = NULL;
7091c612
JB
6570
6571 i.memshift = 0;
6572 for (op = 0; op < i.operands; op++)
8dc0818e 6573 if (i.flags[op] & Operand_Mem)
7091c612 6574 {
4174bfff
JB
6575 if (t->opcode_modifier.evex == EVEXLIG)
6576 i.memshift = 2 + (i.suffix == QWORD_MNEM_SUFFIX);
6577 else if (t->operand_types[op].bitfield.xmmword
6578 + t->operand_types[op].bitfield.ymmword
6579 + t->operand_types[op].bitfield.zmmword <= 1)
7091c612
JB
6580 type = &t->operand_types[op];
6581 else if (!i.types[op].bitfield.unspecified)
6582 type = &i.types[op];
125ff819
JB
6583 else /* Ambiguities get resolved elsewhere. */
6584 fallback = &t->operand_types[op];
7091c612 6585 }
3528c362 6586 else if (i.types[op].bitfield.class == RegSIMD
4174bfff 6587 && t->opcode_modifier.evex != EVEXLIG)
7091c612
JB
6588 {
6589 if (i.types[op].bitfield.zmmword)
6590 i.memshift = 6;
6591 else if (i.types[op].bitfield.ymmword && i.memshift < 5)
6592 i.memshift = 5;
6593 else if (i.types[op].bitfield.xmmword && i.memshift < 4)
6594 i.memshift = 4;
6595 }
6596
125ff819
JB
6597 if (!type && !i.memshift)
6598 type = fallback;
7091c612
JB
6599 if (type)
6600 {
6601 if (type->bitfield.zmmword)
6602 i.memshift = 6;
6603 else if (type->bitfield.ymmword)
6604 i.memshift = 5;
6605 else if (type->bitfield.xmmword)
6606 i.memshift = 4;
6607 }
6608
6609 /* For the check in fits_in_disp8(). */
6610 if (i.memshift == 0)
6611 i.memshift = -1;
6612 }
43234a1e
L
6613
6614 for (op = 0; op < i.operands; op++)
6615 if (operand_type_check (i.types[op], disp)
6616 && i.op[op].disps->X_op == O_constant)
6617 {
b5014f7a 6618 if (fits_in_disp8 (i.op[op].disps->X_add_number))
43234a1e 6619 {
b5014f7a
JB
6620 i.types[op].bitfield.disp8 = 1;
6621 return 0;
43234a1e 6622 }
b5014f7a 6623 i.types[op].bitfield.disp8 = 0;
43234a1e
L
6624 }
6625 }
b5014f7a
JB
6626
6627 i.memshift = 0;
43234a1e 6628
6c30d220
L
6629 return 0;
6630}
6631
da4977e0 6632/* Check if encoding requirements are met by the instruction. */
a683cc34
SP
6633
6634static int
da4977e0 6635VEX_check_encoding (const insn_template *t)
a683cc34 6636{
da4977e0
JB
6637 if (i.vec_encoding == vex_encoding_error)
6638 {
6639 i.error = unsupported;
6640 return 1;
6641 }
6642
86fa6981 6643 if (i.vec_encoding == vex_encoding_evex)
43234a1e 6644 {
86fa6981 6645 /* This instruction must be encoded with EVEX prefix. */
e771e7c9 6646 if (!is_evex_encoding (t))
86fa6981
L
6647 {
6648 i.error = unsupported;
6649 return 1;
6650 }
6651 return 0;
43234a1e
L
6652 }
6653
a683cc34 6654 if (!t->opcode_modifier.vex)
86fa6981
L
6655 {
6656 /* This instruction template doesn't have VEX prefix. */
6657 if (i.vec_encoding != vex_encoding_default)
6658 {
6659 i.error = unsupported;
6660 return 1;
6661 }
6662 return 0;
6663 }
a683cc34 6664
a683cc34
SP
6665 return 0;
6666}
6667
7b94647a
JB
6668/* Helper function for the progress() macro in match_template(). */
6669static INLINE enum i386_error progress (enum i386_error new,
6670 enum i386_error last,
6671 unsigned int line, unsigned int *line_p)
6672{
6673 if (line <= *line_p)
6674 return last;
6675 *line_p = line;
6676 return new;
6677}
6678
d3ce72d0 6679static const insn_template *
83b16ac6 6680match_template (char mnem_suffix)
29b0f896
AM
6681{
6682 /* Points to template once we've found it. */
d3ce72d0 6683 const insn_template *t;
40fb9820 6684 i386_operand_type overlap0, overlap1, overlap2, overlap3;
c0f3af97 6685 i386_operand_type overlap4;
29b0f896 6686 unsigned int found_reverse_match;
40fb9820 6687 i386_operand_type operand_types [MAX_OPERANDS];
539e75ad 6688 int addr_prefix_disp;
7b94647a
JB
6689 unsigned int j, size_match, check_register, errline = __LINE__;
6690 enum i386_error specific_error = number_of_operands_mismatch;
6691#define progress(err) progress (err, specific_error, __LINE__, &errline)
29b0f896 6692
c0f3af97
L
6693#if MAX_OPERANDS != 5
6694# error "MAX_OPERANDS must be 5."
f48ff2ae
L
6695#endif
6696
29b0f896 6697 found_reverse_match = 0;
539e75ad 6698 addr_prefix_disp = -1;
40fb9820 6699
45aa61fe 6700 for (t = current_templates->start; t < current_templates->end; t++)
29b0f896 6701 {
539e75ad 6702 addr_prefix_disp = -1;
dbbc8b7e 6703 found_reverse_match = 0;
539e75ad 6704
7b94647a 6705 /* Must have right number of operands. */
29b0f896
AM
6706 if (i.operands != t->operands)
6707 continue;
6708
50aecf8c 6709 /* Check processor support. */
7b94647a 6710 specific_error = progress (unsupported);
45a4bb20 6711 if (cpu_flags_match (t) != CPU_FLAGS_PERFECT_MATCH)
50aecf8c
L
6712 continue;
6713
e1d4d893 6714 /* Check AT&T mnemonic. */
7b94647a 6715 specific_error = progress (unsupported_with_intel_mnemonic);
e1d4d893 6716 if (intel_mnemonic && t->opcode_modifier.attmnemonic)
1efbbeb4
L
6717 continue;
6718
4b5aaf5f 6719 /* Check AT&T/Intel syntax. */
7b94647a 6720 specific_error = progress (unsupported_syntax);
5c07affc 6721 if ((intel_syntax && t->opcode_modifier.attsyntax)
4b5aaf5f 6722 || (!intel_syntax && t->opcode_modifier.intelsyntax))
1efbbeb4
L
6723 continue;
6724
4b5aaf5f
L
6725 /* Check Intel64/AMD64 ISA. */
6726 switch (isa64)
6727 {
6728 default:
6729 /* Default: Don't accept Intel64. */
6730 if (t->opcode_modifier.isa64 == INTEL64)
6731 continue;
6732 break;
6733 case amd64:
6734 /* -mamd64: Don't accept Intel64 and Intel64 only. */
6735 if (t->opcode_modifier.isa64 >= INTEL64)
6736 continue;
6737 break;
6738 case intel64:
6739 /* -mintel64: Don't accept AMD64. */
5990e377 6740 if (t->opcode_modifier.isa64 == AMD64 && flag_code == CODE_64BIT)
4b5aaf5f
L
6741 continue;
6742 break;
6743 }
6744
dc2be329 6745 /* Check the suffix. */
7b94647a 6746 specific_error = progress (invalid_instruction_suffix);
7505bb03
JB
6747 if ((t->opcode_modifier.no_bsuf && mnem_suffix == BYTE_MNEM_SUFFIX)
6748 || (t->opcode_modifier.no_wsuf && mnem_suffix == WORD_MNEM_SUFFIX)
6749 || (t->opcode_modifier.no_lsuf && mnem_suffix == LONG_MNEM_SUFFIX)
6750 || (t->opcode_modifier.no_ssuf && mnem_suffix == SHORT_MNEM_SUFFIX)
6751 || (t->opcode_modifier.no_qsuf && mnem_suffix == QWORD_MNEM_SUFFIX))
83b16ac6 6752 continue;
29b0f896 6753
7b94647a 6754 specific_error = progress (operand_size_mismatch);
3ac21baa
JB
6755 size_match = operand_size_match (t);
6756 if (!size_match)
7d5e4556 6757 continue;
539e75ad 6758
6f2f06be
JB
6759 /* This is intentionally not
6760
0cfa3eb3 6761 if (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE))
6f2f06be
JB
6762
6763 as the case of a missing * on the operand is accepted (perhaps with
6764 a warning, issued further down). */
7b94647a 6765 specific_error = progress (operand_type_mismatch);
0cfa3eb3 6766 if (i.jumpabsolute && t->opcode_modifier.jump != JUMP_ABSOLUTE)
7b94647a 6767 continue;
6f2f06be 6768
a4d3acd2
JB
6769 /* In Intel syntax, normally we can check for memory operand size when
6770 there is no mnemonic suffix. But jmp and call have 2 different
6771 encodings with Dword memory operand size. Skip the "near" one
6772 (permitting a register operand) when "far" was requested. */
6773 if (i.far_branch
6774 && t->opcode_modifier.jump == JUMP_ABSOLUTE
6775 && t->operand_types[0].bitfield.class == Reg)
6776 continue;
6777
5c07affc
L
6778 for (j = 0; j < MAX_OPERANDS; j++)
6779 operand_types[j] = t->operand_types[j];
6780
9db83a32 6781 /* In general, don't allow 32-bit operands on pre-386. */
7b94647a
JB
6782 specific_error = progress (mnem_suffix ? invalid_instruction_suffix
6783 : operand_size_mismatch);
4873e243 6784 j = i.imm_operands + (t->operands > i.imm_operands + 1);
9db83a32
JB
6785 if (i.suffix == LONG_MNEM_SUFFIX
6786 && !cpu_arch_flags.bitfield.cpui386
45aa61fe 6787 && (intel_syntax
3cd7f3e3 6788 ? (t->opcode_modifier.mnemonicsize != IGNORESIZE
76d3f746
JB
6789 && !intel_float_operand (insn_name (t)))
6790 : intel_float_operand (insn_name (t)) != 2)
4873e243
JB
6791 && (t->operands == i.imm_operands
6792 || (operand_types[i.imm_operands].bitfield.class != RegMMX
6793 && operand_types[i.imm_operands].bitfield.class != RegSIMD
6794 && operand_types[i.imm_operands].bitfield.class != RegMask)
6795 || (operand_types[j].bitfield.class != RegMMX
6796 && operand_types[j].bitfield.class != RegSIMD
6797 && operand_types[j].bitfield.class != RegMask))
63112cd6 6798 && !t->opcode_modifier.sib)
192dc9c6
JB
6799 continue;
6800
29b0f896 6801 /* Do not verify operands when there are none. */
e365e234 6802 if (!t->operands)
da4977e0
JB
6803 {
6804 if (VEX_check_encoding (t))
6805 {
7b94647a 6806 specific_error = progress (i.error);
da4977e0
JB
6807 continue;
6808 }
6809
6810 /* We've found a match; break out of loop. */
6811 break;
6812 }
252b5132 6813
48bcea9f
JB
6814 if (!t->opcode_modifier.jump
6815 || t->opcode_modifier.jump == JUMP_ABSOLUTE)
6816 {
6817 /* There should be only one Disp operand. */
6818 for (j = 0; j < MAX_OPERANDS; j++)
6819 if (operand_type_check (operand_types[j], disp))
539e75ad 6820 break;
48bcea9f
JB
6821 if (j < MAX_OPERANDS)
6822 {
5b7c81bd 6823 bool override = (i.prefix[ADDR_PREFIX] != 0);
48bcea9f
JB
6824
6825 addr_prefix_disp = j;
6826
a775efc8
JB
6827 /* Address size prefix will turn Disp64 operand into Disp32 and
6828 Disp32/Disp16 one into Disp16/Disp32 respectively. */
48bcea9f 6829 switch (flag_code)
40fb9820 6830 {
48bcea9f
JB
6831 case CODE_16BIT:
6832 override = !override;
6833 /* Fall through. */
6834 case CODE_32BIT:
6835 if (operand_types[j].bitfield.disp32
6836 && operand_types[j].bitfield.disp16)
40fb9820 6837 {
48bcea9f
JB
6838 operand_types[j].bitfield.disp16 = override;
6839 operand_types[j].bitfield.disp32 = !override;
40fb9820 6840 }
a775efc8 6841 gas_assert (!operand_types[j].bitfield.disp64);
48bcea9f
JB
6842 break;
6843
6844 case CODE_64BIT:
a775efc8 6845 if (operand_types[j].bitfield.disp64)
40fb9820 6846 {
a775efc8 6847 gas_assert (!operand_types[j].bitfield.disp32);
48bcea9f 6848 operand_types[j].bitfield.disp32 = override;
a775efc8 6849 operand_types[j].bitfield.disp64 = !override;
40fb9820 6850 }
48bcea9f
JB
6851 operand_types[j].bitfield.disp16 = 0;
6852 break;
40fb9820 6853 }
539e75ad 6854 }
48bcea9f 6855 }
539e75ad 6856
56ffb741 6857 /* We check register size if needed. */
9c19e9ec 6858 if (t->opcode_modifier.checkoperandsize)
e2195274
JB
6859 {
6860 check_register = (1 << t->operands) - 1;
a5748e0d 6861 if (i.broadcast.type || i.broadcast.bytes)
5273a3cd 6862 check_register &= ~(1 << i.broadcast.operand);
e2195274
JB
6863 }
6864 else
6865 check_register = 0;
6866
c6fb90c8 6867 overlap0 = operand_type_and (i.types[0], operand_types[0]);
29b0f896
AM
6868 switch (t->operands)
6869 {
6870 case 1:
40fb9820 6871 if (!operand_type_match (overlap0, i.types[0]))
29b0f896 6872 continue;
ae9a0a51
JB
6873
6874 /* Allow the ModR/M encoding to be requested by using the {load} or
6875 {store} pseudo prefix on an applicable insn. */
6876 if (!t->opcode_modifier.modrm
6877 && i.reg_operands == 1
6878 && ((i.dir_encoding == dir_encoding_load
6879 && t->mnem_off != MN_pop)
6880 || (i.dir_encoding == dir_encoding_store
6881 && t->mnem_off != MN_push))
6882 /* Avoid BSWAP. */
6883 && t->mnem_off != MN_bswap)
6884 continue;
29b0f896 6885 break;
ae9a0a51 6886
29b0f896 6887 case 2:
33eaf5de 6888 /* xchg %eax, %eax is a special case. It is an alias for nop
8b38ad71
L
6889 only in 32bit mode and we can use opcode 0x90. In 64bit
6890 mode, we can't use 0x90 for xchg %eax, %eax since it should
6891 zero-extend %eax to %rax. */
ae9a0a51
JB
6892 if (t->base_opcode == 0x90
6893 && t->opcode_space == SPACE_BASE)
6894 {
6895 if (flag_code == CODE_64BIT
6896 && i.types[0].bitfield.instance == Accum
6897 && i.types[0].bitfield.dword
6898 && i.types[1].bitfield.instance == Accum)
6899 continue;
6900
6901 /* Allow the ModR/M encoding to be requested by using the
6902 {load} or {store} pseudo prefix. */
6903 if (i.dir_encoding == dir_encoding_load
6904 || i.dir_encoding == dir_encoding_store)
6905 continue;
6906 }
e3669c7f
JB
6907
6908 if (t->base_opcode == MOV_AX_DISP32
ddb62495 6909 && t->opcode_space == SPACE_BASE
69196391 6910 && t->mnem_off != MN_movabs)
e3669c7f
JB
6911 {
6912 /* Force 0x8b encoding for "mov foo@GOT, %eax". */
6913 if (i.reloc[0] == BFD_RELOC_386_GOT32)
6914 continue;
6915
6916 /* xrelease mov %eax, <disp> is another special case. It must not
6917 match the accumulator-only encoding of mov. */
6918 if (i.hle_prefix)
6919 continue;
ae9a0a51
JB
6920
6921 /* Allow the ModR/M encoding to be requested by using a suitable
6922 {load} or {store} pseudo prefix. */
6923 if (i.dir_encoding == (i.types[0].bitfield.instance == Accum
6924 ? dir_encoding_store
6925 : dir_encoding_load)
6926 && !i.types[0].bitfield.disp64
6927 && !i.types[1].bitfield.disp64)
6928 continue;
6929 }
6930
6931 /* Allow the ModR/M encoding to be requested by using the {load} or
6932 {store} pseudo prefix on an applicable insn. */
6933 if (!t->opcode_modifier.modrm
6934 && i.reg_operands == 1
6935 && i.imm_operands == 1
6936 && (i.dir_encoding == dir_encoding_load
6937 || i.dir_encoding == dir_encoding_store)
6938 && t->opcode_space == SPACE_BASE)
6939 {
6940 if (t->base_opcode == 0xb0 /* mov $imm, %reg */
6941 && i.dir_encoding == dir_encoding_store)
6942 continue;
6943
6944 if ((t->base_opcode | 0x38) == 0x3c /* <alu> $imm, %acc */
6945 && (t->base_opcode != 0x3c /* cmp $imm, %acc */
6946 || i.dir_encoding == dir_encoding_load))
6947 continue;
6948
6949 if (t->base_opcode == 0xa8 /* test $imm, %acc */
6950 && i.dir_encoding == dir_encoding_load)
6951 continue;
e3669c7f 6952 }
f5eb1d70
JB
6953 /* Fall through. */
6954
6955 case 3:
3ac21baa
JB
6956 if (!(size_match & MATCH_STRAIGHT))
6957 goto check_reverse;
64c49ab3
JB
6958 /* Reverse direction of operands if swapping is possible in the first
6959 place (operands need to be symmetric) and
6960 - the load form is requested, and the template is a store form,
6961 - the store form is requested, and the template is a load form,
6962 - the non-default (swapped) form is requested. */
6963 overlap1 = operand_type_and (operand_types[0], operand_types[1]);
f5eb1d70 6964 if (t->opcode_modifier.d && i.reg_operands == i.operands
64c49ab3
JB
6965 && !operand_type_all_zero (&overlap1))
6966 switch (i.dir_encoding)
6967 {
6968 case dir_encoding_load:
6969 if (operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 6970 || t->opcode_modifier.regmem)
64c49ab3
JB
6971 goto check_reverse;
6972 break;
6973
6974 case dir_encoding_store:
6975 if (!operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 6976 && !t->opcode_modifier.regmem)
64c49ab3
JB
6977 goto check_reverse;
6978 break;
6979
6980 case dir_encoding_swap:
6981 goto check_reverse;
6982
6983 case dir_encoding_default:
6984 break;
6985 }
86fa6981 6986 /* If we want store form, we skip the current load. */
64c49ab3
JB
6987 if ((i.dir_encoding == dir_encoding_store
6988 || i.dir_encoding == dir_encoding_swap)
86fa6981
L
6989 && i.mem_operands == 0
6990 && t->opcode_modifier.load)
fa99fab2 6991 continue;
1a0670f3 6992 /* Fall through. */
f48ff2ae 6993 case 4:
c0f3af97 6994 case 5:
c6fb90c8 6995 overlap1 = operand_type_and (i.types[1], operand_types[1]);
40fb9820
L
6996 if (!operand_type_match (overlap0, i.types[0])
6997 || !operand_type_match (overlap1, i.types[1])
e2195274 6998 || ((check_register & 3) == 3
dc821c5f 6999 && !operand_type_register_match (i.types[0],
40fb9820 7000 operand_types[0],
dc821c5f 7001 i.types[1],
40fb9820 7002 operand_types[1])))
29b0f896 7003 {
7b94647a
JB
7004 specific_error = progress (i.error);
7005
29b0f896 7006 /* Check if other direction is valid ... */
38e314eb 7007 if (!t->opcode_modifier.d)
29b0f896
AM
7008 continue;
7009
dc1e8a47 7010 check_reverse:
3ac21baa
JB
7011 if (!(size_match & MATCH_REVERSE))
7012 continue;
29b0f896 7013 /* Try reversing direction of operands. */
aa180741 7014 j = t->cpu_flags.bitfield.cpufma4
5dab1799 7015 || t->cpu_flags.bitfield.cpuxop ? 1 : i.operands - 1;
8bd915b7
JB
7016 overlap0 = operand_type_and (i.types[0], operand_types[j]);
7017 overlap1 = operand_type_and (i.types[j], operand_types[0]);
c975cec5
JB
7018 overlap2 = operand_type_and (i.types[1], operand_types[1]);
7019 gas_assert (t->operands != 3 || !check_register);
40fb9820 7020 if (!operand_type_match (overlap0, i.types[0])
8bd915b7 7021 || !operand_type_match (overlap1, i.types[j])
c975cec5
JB
7022 || (t->operands == 3
7023 && !operand_type_match (overlap2, i.types[1]))
45664ddb 7024 || (check_register
dc821c5f 7025 && !operand_type_register_match (i.types[0],
8bd915b7
JB
7026 operand_types[j],
7027 i.types[j],
45664ddb 7028 operand_types[0])))
29b0f896
AM
7029 {
7030 /* Does not match either direction. */
7b94647a 7031 specific_error = progress (i.error);
29b0f896
AM
7032 continue;
7033 }
ac9226cf 7034 /* found_reverse_match holds which variant of D
29b0f896 7035 we've found. */
38e314eb
JB
7036 if (!t->opcode_modifier.d)
7037 found_reverse_match = 0;
7038 else if (operand_types[0].bitfield.tbyte)
ac9226cf 7039 {
4943d587
JB
7040 if (t->opcode_modifier.operandconstraint != UGH)
7041 found_reverse_match = Opcode_FloatD;
bd782808
JB
7042 else
7043 found_reverse_match = ~0;
ac9226cf 7044 /* FSUB{,R} and FDIV{,R} may need a 2nd bit flipped. */
bd782808 7045 if ((t->extension_opcode & 4)
ac9226cf
JB
7046 && (intel_syntax || intel_mnemonic))
7047 found_reverse_match |= Opcode_FloatR;
7048 }
aa180741 7049 else if (t->cpu_flags.bitfield.cpufma4
5dab1799 7050 || t->cpu_flags.bitfield.cpuxop)
8bd915b7
JB
7051 {
7052 found_reverse_match = Opcode_VexW;
7053 goto check_operands_345;
7054 }
ddb62495
JB
7055 else if (t->opcode_space != SPACE_BASE
7056 && (t->opcode_space != SPACE_0F
2c735193
JB
7057 /* MOV to/from CR/DR/TR, as an exception, follow
7058 the base opcode space encoding model. */
7059 || (t->base_opcode | 7) != 0x27))
dbbc8b7e 7060 found_reverse_match = (t->base_opcode & 0xee) != 0x6e
2c735193 7061 ? Opcode_ExtD : Opcode_SIMD_IntD;
a33ef3c2 7062 else if (!t->opcode_modifier.commutative)
38e314eb 7063 found_reverse_match = Opcode_D;
a33ef3c2
JB
7064 else
7065 found_reverse_match = ~0;
29b0f896 7066 }
f48ff2ae 7067 else
29b0f896 7068 {
f48ff2ae 7069 /* Found a forward 2 operand match here. */
8bd915b7 7070 check_operands_345:
d1cbb4db
L
7071 switch (t->operands)
7072 {
c0f3af97 7073 case 5:
3d0738af 7074 overlap4 = operand_type_and (i.types[4], operand_types[4]);
c0f3af97 7075 if (!operand_type_match (overlap4, i.types[4])
dc821c5f 7076 || !operand_type_register_match (i.types[3],
c0f3af97 7077 operand_types[3],
c0f3af97
L
7078 i.types[4],
7079 operand_types[4]))
7b94647a
JB
7080 {
7081 specific_error = progress (i.error);
7082 continue;
7083 }
1a0670f3 7084 /* Fall through. */
f48ff2ae 7085 case 4:
3d0738af 7086 overlap3 = operand_type_and (i.types[3], operand_types[3]);
40fb9820 7087 if (!operand_type_match (overlap3, i.types[3])
e2195274
JB
7088 || ((check_register & 0xa) == 0xa
7089 && !operand_type_register_match (i.types[1],
f7768225
JB
7090 operand_types[1],
7091 i.types[3],
e2195274
JB
7092 operand_types[3]))
7093 || ((check_register & 0xc) == 0xc
7094 && !operand_type_register_match (i.types[2],
7095 operand_types[2],
7096 i.types[3],
7097 operand_types[3])))
7b94647a
JB
7098 {
7099 specific_error = progress (i.error);
7100 continue;
7101 }
1a0670f3 7102 /* Fall through. */
f48ff2ae 7103 case 3:
3d0738af 7104 overlap2 = operand_type_and (i.types[2], operand_types[2]);
40fb9820 7105 if (!operand_type_match (overlap2, i.types[2])
e2195274
JB
7106 || ((check_register & 5) == 5
7107 && !operand_type_register_match (i.types[0],
23e42951
JB
7108 operand_types[0],
7109 i.types[2],
e2195274
JB
7110 operand_types[2]))
7111 || ((check_register & 6) == 6
7112 && !operand_type_register_match (i.types[1],
7113 operand_types[1],
7114 i.types[2],
7115 operand_types[2])))
7b94647a
JB
7116 {
7117 specific_error = progress (i.error);
7118 continue;
7119 }
f48ff2ae
L
7120 break;
7121 }
29b0f896 7122 }
f48ff2ae 7123 /* Found either forward/reverse 2, 3 or 4 operand match here:
29b0f896
AM
7124 slip through to break. */
7125 }
c0f3af97 7126
9bb4d860
L
7127 /* Check if VEX/EVEX encoding requirements can be satisfied. */
7128 if (VEX_check_encoding (t))
da4977e0 7129 {
7b94647a 7130 specific_error = progress (i.error);
da4977e0
JB
7131 continue;
7132 }
7133
9bb4d860
L
7134 /* Check if vector operands are valid. */
7135 if (check_VecOperands (t))
5614d22c 7136 {
7b94647a 7137 specific_error = progress (i.error);
5614d22c
JB
7138 continue;
7139 }
a683cc34 7140
29b0f896
AM
7141 /* We've found a match; break out of loop. */
7142 break;
7143 }
7144
7b94647a
JB
7145#undef progress
7146
29b0f896
AM
7147 if (t == current_templates->end)
7148 {
7149 /* We found no match. */
04784e33 7150 i.error = specific_error;
fa99fab2 7151 return NULL;
29b0f896 7152 }
252b5132 7153
29b0f896
AM
7154 if (!quiet_warnings)
7155 {
7156 if (!intel_syntax
0cfa3eb3 7157 && (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE)))
76d3f746 7158 as_warn (_("indirect %s without `*'"), insn_name (t));
29b0f896 7159
40fb9820 7160 if (t->opcode_modifier.isprefix
3cd7f3e3 7161 && t->opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
7162 {
7163 /* Warn them that a data or address size prefix doesn't
7164 affect assembly of the next line of code. */
76d3f746 7165 as_warn (_("stand-alone `%s' prefix"), insn_name (t));
29b0f896
AM
7166 }
7167 }
7168
7169 /* Copy the template we found. */
9a182d04 7170 install_template (t);
539e75ad
L
7171
7172 if (addr_prefix_disp != -1)
7173 i.tm.operand_types[addr_prefix_disp]
7174 = operand_types[addr_prefix_disp];
7175
8bd915b7 7176 switch (found_reverse_match)
29b0f896 7177 {
8bd915b7
JB
7178 case 0:
7179 break;
7180
bd782808
JB
7181 case Opcode_FloatR:
7182 case Opcode_FloatR | Opcode_FloatD:
7183 i.tm.extension_opcode ^= Opcode_FloatR >> 3;
7184 found_reverse_match &= Opcode_FloatD;
7185
7186 /* Fall through. */
8bd915b7 7187 default:
dfd69174
JB
7188 /* If we found a reverse match we must alter the opcode direction
7189 bit and clear/flip the regmem modifier one. found_reverse_match
7190 holds bits to change (different for int & float insns). */
29b0f896
AM
7191
7192 i.tm.base_opcode ^= found_reverse_match;
7193
dfd69174
JB
7194 /* Certain SIMD insns have their load forms specified in the opcode
7195 table, and hence we need to _set_ RegMem instead of clearing it.
7196 We need to avoid setting the bit though on insns like KMOVW. */
7197 i.tm.opcode_modifier.regmem
7198 = i.tm.opcode_modifier.modrm && i.tm.opcode_modifier.d
7199 && i.tm.operands > 2U - i.tm.opcode_modifier.sse2avx
7200 && !i.tm.opcode_modifier.regmem;
a33ef3c2
JB
7201
7202 /* Fall through. */
7203 case ~0:
7204 i.tm.operand_types[0] = operand_types[i.operands - 1];
7205 i.tm.operand_types[i.operands - 1] = operand_types[0];
8bd915b7
JB
7206 break;
7207
7208 case Opcode_VexW:
7209 /* Only the first two register operands need reversing, alongside
7210 flipping VEX.W. */
7211 i.tm.opcode_modifier.vexw ^= VEXW0 ^ VEXW1;
7212
7213 j = i.tm.operand_types[0].bitfield.imm8;
7214 i.tm.operand_types[j] = operand_types[j + 1];
7215 i.tm.operand_types[j + 1] = operand_types[j];
7216 break;
29b0f896
AM
7217 }
7218
fa99fab2 7219 return t;
29b0f896
AM
7220}
7221
7222static int
e3bb37b5 7223check_string (void)
29b0f896 7224{
51c8edf6
JB
7225 unsigned int es_op = i.tm.opcode_modifier.isstring - IS_STRING_ES_OP0;
7226 unsigned int op = i.tm.operand_types[0].bitfield.baseindex ? es_op : 0;
8dc0818e 7227
5e042380 7228 if (i.seg[op] != NULL && i.seg[op] != reg_es)
29b0f896 7229 {
51c8edf6 7230 as_bad (_("`%s' operand %u must use `%ses' segment"),
76d3f746 7231 insn_name (&i.tm),
51c8edf6
JB
7232 intel_syntax ? i.tm.operands - es_op : es_op + 1,
7233 register_prefix);
7234 return 0;
29b0f896 7235 }
51c8edf6
JB
7236
7237 /* There's only ever one segment override allowed per instruction.
7238 This instruction possibly has a legal segment override on the
7239 second operand, so copy the segment to where non-string
7240 instructions store it, allowing common code. */
7241 i.seg[op] = i.seg[1];
7242
29b0f896
AM
7243 return 1;
7244}
7245
7246static int
543613e9 7247process_suffix (void)
29b0f896 7248{
7fc69528 7249 bool is_movx = false;
8b65b895 7250
29b0f896
AM
7251 /* If matched instruction specifies an explicit instruction mnemonic
7252 suffix, use it. */
673fe0f0 7253 if (i.tm.opcode_modifier.size == SIZE16)
40fb9820 7254 i.suffix = WORD_MNEM_SUFFIX;
673fe0f0 7255 else if (i.tm.opcode_modifier.size == SIZE32)
40fb9820 7256 i.suffix = LONG_MNEM_SUFFIX;
673fe0f0 7257 else if (i.tm.opcode_modifier.size == SIZE64)
40fb9820 7258 i.suffix = QWORD_MNEM_SUFFIX;
13e600d0 7259 else if (i.reg_operands
c8f8eebc 7260 && (i.operands > 1 || i.types[0].bitfield.class == Reg)
255571cd 7261 && i.tm.opcode_modifier.operandconstraint != ADDR_PREFIX_OP_REG)
29b0f896 7262 {
65fca059 7263 unsigned int numop = i.operands;
389d00a5
JB
7264
7265 /* MOVSX/MOVZX */
ddb62495 7266 is_movx = (i.tm.opcode_space == SPACE_0F
389d00a5 7267 && (i.tm.base_opcode | 8) == 0xbe)
ddb62495 7268 || (i.tm.opcode_space == SPACE_BASE
389d00a5
JB
7269 && i.tm.base_opcode == 0x63
7270 && i.tm.cpu_flags.bitfield.cpu64);
7271
65fca059
JB
7272 /* movsx/movzx want only their source operand considered here, for the
7273 ambiguity checking below. The suffix will be replaced afterwards
7274 to represent the destination (register). */
389d00a5 7275 if (is_movx && (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63))
65fca059
JB
7276 --i.operands;
7277
643bb870 7278 /* crc32 needs REX.W set regardless of suffix / source operand size. */
7fc69528 7279 if (i.tm.mnem_off == MN_crc32 && i.tm.operand_types[1].bitfield.qword)
643bb870
JB
7280 i.rex |= REX_W;
7281
29b0f896 7282 /* If there's no instruction mnemonic suffix we try to invent one
13e600d0 7283 based on GPR operands. */
29b0f896
AM
7284 if (!i.suffix)
7285 {
7286 /* We take i.suffix from the last register operand specified,
7287 Destination register type is more significant than source
381d071f
L
7288 register type. crc32 in SSE4.2 prefers source register
7289 type. */
7fc69528 7290 unsigned int op = i.tm.mnem_off == MN_crc32 ? 1 : i.operands;
20592a94 7291
1a035124
JB
7292 while (op--)
7293 if (i.tm.operand_types[op].bitfield.instance == InstanceNone
7294 || i.tm.operand_types[op].bitfield.instance == Accum)
7295 {
7296 if (i.types[op].bitfield.class != Reg)
7297 continue;
7298 if (i.types[op].bitfield.byte)
7299 i.suffix = BYTE_MNEM_SUFFIX;
7300 else if (i.types[op].bitfield.word)
7301 i.suffix = WORD_MNEM_SUFFIX;
7302 else if (i.types[op].bitfield.dword)
7303 i.suffix = LONG_MNEM_SUFFIX;
7304 else if (i.types[op].bitfield.qword)
7305 i.suffix = QWORD_MNEM_SUFFIX;
7306 else
7307 continue;
7308 break;
7309 }
65fca059
JB
7310
7311 /* As an exception, movsx/movzx silently default to a byte source
7312 in AT&T mode. */
389d00a5 7313 if (is_movx && i.tm.opcode_modifier.w && !i.suffix && !intel_syntax)
65fca059 7314 i.suffix = BYTE_MNEM_SUFFIX;
29b0f896
AM
7315 }
7316 else if (i.suffix == BYTE_MNEM_SUFFIX)
7317 {
1cb0ab18 7318 if (!check_byte_reg ())
29b0f896
AM
7319 return 0;
7320 }
7321 else if (i.suffix == LONG_MNEM_SUFFIX)
7322 {
1cb0ab18 7323 if (!check_long_reg ())
29b0f896
AM
7324 return 0;
7325 }
7326 else if (i.suffix == QWORD_MNEM_SUFFIX)
7327 {
1cb0ab18 7328 if (!check_qword_reg ())
29b0f896
AM
7329 return 0;
7330 }
7331 else if (i.suffix == WORD_MNEM_SUFFIX)
7332 {
1cb0ab18 7333 if (!check_word_reg ())
29b0f896
AM
7334 return 0;
7335 }
3cd7f3e3
L
7336 else if (intel_syntax
7337 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
7338 /* Do nothing if the instruction is going to ignore the prefix. */
7339 ;
7340 else
7341 abort ();
65fca059
JB
7342
7343 /* Undo the movsx/movzx change done above. */
7344 i.operands = numop;
29b0f896 7345 }
3cd7f3e3
L
7346 else if (i.tm.opcode_modifier.mnemonicsize == DEFAULTSIZE
7347 && !i.suffix)
29b0f896 7348 {
13e600d0
JB
7349 i.suffix = stackop_size;
7350 if (stackop_size == LONG_MNEM_SUFFIX)
06f74c5c
L
7351 {
7352 /* stackop_size is set to LONG_MNEM_SUFFIX for the
7353 .code16gcc directive to support 16-bit mode with
7354 32-bit address. For IRET without a suffix, generate
7355 16-bit IRET (opcode 0xcf) to return from an interrupt
7356 handler. */
13e600d0
JB
7357 if (i.tm.base_opcode == 0xcf)
7358 {
7359 i.suffix = WORD_MNEM_SUFFIX;
7360 as_warn (_("generating 16-bit `iret' for .code16gcc directive"));
7361 }
7362 /* Warn about changed behavior for segment register push/pop. */
7363 else if ((i.tm.base_opcode | 1) == 0x07)
7364 as_warn (_("generating 32-bit `%s', unlike earlier gas versions"),
76d3f746 7365 insn_name (&i.tm));
06f74c5c 7366 }
29b0f896 7367 }
c006a730 7368 else if (!i.suffix
0cfa3eb3
JB
7369 && (i.tm.opcode_modifier.jump == JUMP_ABSOLUTE
7370 || i.tm.opcode_modifier.jump == JUMP_BYTE
7371 || i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT
ddb62495 7372 || (i.tm.opcode_space == SPACE_0F
389d00a5 7373 && i.tm.base_opcode == 0x01 /* [ls][gi]dt */
64e74474 7374 && i.tm.extension_opcode <= 3)))
9306ca4a
JB
7375 {
7376 switch (flag_code)
7377 {
7378 case CODE_64BIT:
40fb9820 7379 if (!i.tm.opcode_modifier.no_qsuf)
9306ca4a 7380 {
828c2a25
JB
7381 if (i.tm.opcode_modifier.jump == JUMP_BYTE
7382 || i.tm.opcode_modifier.no_lsuf)
7383 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a
JB
7384 break;
7385 }
1a0670f3 7386 /* Fall through. */
9306ca4a 7387 case CODE_32BIT:
40fb9820 7388 if (!i.tm.opcode_modifier.no_lsuf)
9306ca4a
JB
7389 i.suffix = LONG_MNEM_SUFFIX;
7390 break;
7391 case CODE_16BIT:
40fb9820 7392 if (!i.tm.opcode_modifier.no_wsuf)
9306ca4a
JB
7393 i.suffix = WORD_MNEM_SUFFIX;
7394 break;
7395 }
7396 }
252b5132 7397
c006a730 7398 if (!i.suffix
3cd7f3e3 7399 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8
JB
7400 /* Also cover lret/retf/iret in 64-bit mode. */
7401 || (flag_code == CODE_64BIT
7402 && !i.tm.opcode_modifier.no_lsuf
7403 && !i.tm.opcode_modifier.no_qsuf))
3cd7f3e3 7404 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
8bbb3ad8
JB
7405 /* Explicit sizing prefixes are assumed to disambiguate insns. */
7406 && !i.prefix[DATA_PREFIX] && !(i.prefix[REX_PREFIX] & REX_W)
62b3f548
JB
7407 /* Accept FLDENV et al without suffix. */
7408 && (i.tm.opcode_modifier.no_ssuf || i.tm.opcode_modifier.floatmf))
29b0f896 7409 {
6c0946d0 7410 unsigned int suffixes, evex = 0;
c006a730
JB
7411
7412 suffixes = !i.tm.opcode_modifier.no_bsuf;
7413 if (!i.tm.opcode_modifier.no_wsuf)
7414 suffixes |= 1 << 1;
7415 if (!i.tm.opcode_modifier.no_lsuf)
7416 suffixes |= 1 << 2;
c006a730
JB
7417 if (!i.tm.opcode_modifier.no_ssuf)
7418 suffixes |= 1 << 4;
7419 if (flag_code == CODE_64BIT && !i.tm.opcode_modifier.no_qsuf)
7420 suffixes |= 1 << 5;
7421
6c0946d0
JB
7422 /* For [XYZ]MMWORD operands inspect operand sizes. While generally
7423 also suitable for AT&T syntax mode, it was requested that this be
7424 restricted to just Intel syntax. */
a5748e0d
JB
7425 if (intel_syntax && is_any_vex_encoding (&i.tm)
7426 && !i.broadcast.type && !i.broadcast.bytes)
6c0946d0 7427 {
b9915cbc 7428 unsigned int op;
6c0946d0 7429
b9915cbc 7430 for (op = 0; op < i.tm.operands; ++op)
6c0946d0 7431 {
b9915cbc
JB
7432 if (is_evex_encoding (&i.tm)
7433 && !cpu_arch_flags.bitfield.cpuavx512vl)
6c0946d0 7434 {
b9915cbc
JB
7435 if (i.tm.operand_types[op].bitfield.ymmword)
7436 i.tm.operand_types[op].bitfield.xmmword = 0;
7437 if (i.tm.operand_types[op].bitfield.zmmword)
7438 i.tm.operand_types[op].bitfield.ymmword = 0;
7439 if (!i.tm.opcode_modifier.evex
7440 || i.tm.opcode_modifier.evex == EVEXDYN)
7441 i.tm.opcode_modifier.evex = EVEX512;
7442 }
6c0946d0 7443
b9915cbc
JB
7444 if (i.tm.operand_types[op].bitfield.xmmword
7445 + i.tm.operand_types[op].bitfield.ymmword
7446 + i.tm.operand_types[op].bitfield.zmmword < 2)
7447 continue;
6c0946d0 7448
b9915cbc
JB
7449 /* Any properly sized operand disambiguates the insn. */
7450 if (i.types[op].bitfield.xmmword
7451 || i.types[op].bitfield.ymmword
7452 || i.types[op].bitfield.zmmword)
7453 {
7454 suffixes &= ~(7 << 6);
7455 evex = 0;
7456 break;
7457 }
6c0946d0 7458
b9915cbc
JB
7459 if ((i.flags[op] & Operand_Mem)
7460 && i.tm.operand_types[op].bitfield.unspecified)
7461 {
7462 if (i.tm.operand_types[op].bitfield.xmmword)
7463 suffixes |= 1 << 6;
7464 if (i.tm.operand_types[op].bitfield.ymmword)
7465 suffixes |= 1 << 7;
7466 if (i.tm.operand_types[op].bitfield.zmmword)
7467 suffixes |= 1 << 8;
7468 if (is_evex_encoding (&i.tm))
7469 evex = EVEX512;
6c0946d0
JB
7470 }
7471 }
7472 }
7473
7474 /* Are multiple suffixes / operand sizes allowed? */
c006a730 7475 if (suffixes & (suffixes - 1))
9306ca4a 7476 {
873494c8 7477 if (intel_syntax
3cd7f3e3 7478 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8 7479 || operand_check == check_error))
9306ca4a 7480 {
76d3f746 7481 as_bad (_("ambiguous operand size for `%s'"), insn_name (&i.tm));
9306ca4a
JB
7482 return 0;
7483 }
c006a730 7484 if (operand_check == check_error)
9306ca4a 7485 {
c006a730 7486 as_bad (_("no instruction mnemonic suffix given and "
76d3f746 7487 "no register operands; can't size `%s'"), insn_name (&i.tm));
9306ca4a
JB
7488 return 0;
7489 }
c006a730 7490 if (operand_check == check_warning)
873494c8
JB
7491 as_warn (_("%s; using default for `%s'"),
7492 intel_syntax
7493 ? _("ambiguous operand size")
7494 : _("no instruction mnemonic suffix given and "
7495 "no register operands"),
76d3f746 7496 insn_name (&i.tm));
c006a730
JB
7497
7498 if (i.tm.opcode_modifier.floatmf)
7499 i.suffix = SHORT_MNEM_SUFFIX;
389d00a5 7500 else if (is_movx)
65fca059 7501 /* handled below */;
6c0946d0
JB
7502 else if (evex)
7503 i.tm.opcode_modifier.evex = evex;
c006a730
JB
7504 else if (flag_code == CODE_16BIT)
7505 i.suffix = WORD_MNEM_SUFFIX;
1a035124 7506 else if (!i.tm.opcode_modifier.no_lsuf)
c006a730 7507 i.suffix = LONG_MNEM_SUFFIX;
1a035124
JB
7508 else
7509 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a 7510 }
29b0f896 7511 }
252b5132 7512
389d00a5 7513 if (is_movx)
65fca059
JB
7514 {
7515 /* In Intel syntax, movsx/movzx must have a "suffix" (checked above).
7516 In AT&T syntax, if there is no suffix (warned about above), the default
7517 will be byte extension. */
7518 if (i.tm.opcode_modifier.w && i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
7519 i.tm.base_opcode |= 1;
7520
7521 /* For further processing, the suffix should represent the destination
7522 (register). This is already the case when one was used with
7523 mov[sz][bw]*, but we need to replace it for mov[sz]x, or if there was
7524 no suffix to begin with. */
7525 if (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63 || !i.suffix)
7526 {
7527 if (i.types[1].bitfield.word)
7528 i.suffix = WORD_MNEM_SUFFIX;
7529 else if (i.types[1].bitfield.qword)
7530 i.suffix = QWORD_MNEM_SUFFIX;
7531 else
7532 i.suffix = LONG_MNEM_SUFFIX;
7533
7534 i.tm.opcode_modifier.w = 0;
7535 }
7536 }
7537
50128d0c
JB
7538 if (!i.tm.opcode_modifier.modrm && i.reg_operands && i.tm.operands < 3)
7539 i.short_form = (i.tm.operand_types[0].bitfield.class == Reg)
7540 != (i.tm.operand_types[1].bitfield.class == Reg);
7541
d2224064
JB
7542 /* Change the opcode based on the operand size given by i.suffix. */
7543 switch (i.suffix)
29b0f896 7544 {
d2224064
JB
7545 /* Size floating point instruction. */
7546 case LONG_MNEM_SUFFIX:
7547 if (i.tm.opcode_modifier.floatmf)
7548 {
7549 i.tm.base_opcode ^= 4;
7550 break;
7551 }
7552 /* fall through */
7553 case WORD_MNEM_SUFFIX:
7554 case QWORD_MNEM_SUFFIX:
29b0f896 7555 /* It's not a byte, select word/dword operation. */
40fb9820 7556 if (i.tm.opcode_modifier.w)
29b0f896 7557 {
50128d0c 7558 if (i.short_form)
29b0f896
AM
7559 i.tm.base_opcode |= 8;
7560 else
7561 i.tm.base_opcode |= 1;
7562 }
d2224064
JB
7563 /* fall through */
7564 case SHORT_MNEM_SUFFIX:
29b0f896
AM
7565 /* Now select between word & dword operations via the operand
7566 size prefix, except for instructions that will ignore this
7567 prefix anyway. */
c8f8eebc 7568 if (i.suffix != QWORD_MNEM_SUFFIX
3cd7f3e3 7569 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
c8f8eebc
JB
7570 && !i.tm.opcode_modifier.floatmf
7571 && !is_any_vex_encoding (&i.tm)
7572 && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
7573 || (flag_code == CODE_64BIT
7574 && i.tm.opcode_modifier.jump == JUMP_BYTE)))
24eab124
AM
7575 {
7576 unsigned int prefix = DATA_PREFIX_OPCODE;
543613e9 7577
0cfa3eb3 7578 if (i.tm.opcode_modifier.jump == JUMP_BYTE) /* jcxz, loop */
29b0f896 7579 prefix = ADDR_PREFIX_OPCODE;
252b5132 7580
29b0f896
AM
7581 if (!add_prefix (prefix))
7582 return 0;
24eab124 7583 }
252b5132 7584
29b0f896
AM
7585 /* Set mode64 for an operand. */
7586 if (i.suffix == QWORD_MNEM_SUFFIX
9146926a 7587 && flag_code == CODE_64BIT
d2224064 7588 && !i.tm.opcode_modifier.norex64
4ed21b58 7589 && !i.tm.opcode_modifier.vexw
46e883c5 7590 /* Special case for xchg %rax,%rax. It is NOP and doesn't
d2224064
JB
7591 need rex64. */
7592 && ! (i.operands == 2
7593 && i.tm.base_opcode == 0x90
ddb62495 7594 && i.tm.opcode_space == SPACE_BASE
75e5731b
JB
7595 && i.types[0].bitfield.instance == Accum
7596 && i.types[0].bitfield.qword
44732014 7597 && i.types[1].bitfield.instance == Accum))
d2224064 7598 i.rex |= REX_W;
3e73aa7c 7599
d2224064 7600 break;
8bbb3ad8
JB
7601
7602 case 0:
f9a6a8f0 7603 /* Select word/dword/qword operation with explicit data sizing prefix
8bbb3ad8
JB
7604 when there are no suitable register operands. */
7605 if (i.tm.opcode_modifier.w
7606 && (i.prefix[DATA_PREFIX] || (i.prefix[REX_PREFIX] & REX_W))
7607 && (!i.reg_operands
7608 || (i.reg_operands == 1
7609 /* ShiftCount */
7610 && (i.tm.operand_types[0].bitfield.instance == RegC
7611 /* InOutPortReg */
7612 || i.tm.operand_types[0].bitfield.instance == RegD
7613 || i.tm.operand_types[1].bitfield.instance == RegD
7fc69528 7614 || i.tm.mnem_off == MN_crc32))))
8bbb3ad8
JB
7615 i.tm.base_opcode |= 1;
7616 break;
29b0f896 7617 }
7ecd2f8b 7618
255571cd 7619 if (i.tm.opcode_modifier.operandconstraint == ADDR_PREFIX_OP_REG)
c0a30a9f 7620 {
c8f8eebc
JB
7621 gas_assert (!i.suffix);
7622 gas_assert (i.reg_operands);
c0a30a9f 7623
c8f8eebc
JB
7624 if (i.tm.operand_types[0].bitfield.instance == Accum
7625 || i.operands == 1)
7626 {
7627 /* The address size override prefix changes the size of the
7628 first operand. */
7629 if (flag_code == CODE_64BIT
7630 && i.op[0].regs->reg_type.bitfield.word)
7631 {
7632 as_bad (_("16-bit addressing unavailable for `%s'"),
76d3f746 7633 insn_name (&i.tm));
c8f8eebc
JB
7634 return 0;
7635 }
7636
7637 if ((flag_code == CODE_32BIT
7638 ? i.op[0].regs->reg_type.bitfield.word
7639 : i.op[0].regs->reg_type.bitfield.dword)
7640 && !add_prefix (ADDR_PREFIX_OPCODE))
7641 return 0;
7642 }
c0a30a9f
L
7643 else
7644 {
c8f8eebc
JB
7645 /* Check invalid register operand when the address size override
7646 prefix changes the size of register operands. */
7647 unsigned int op;
7648 enum { need_word, need_dword, need_qword } need;
7649
27f13469 7650 /* Check the register operand for the address size prefix if
b3a3496f 7651 the memory operand has no real registers, like symbol, DISP
829f3fe1 7652 or bogus (x32-only) symbol(%rip) when symbol(%eip) is meant. */
27f13469
L
7653 if (i.mem_operands == 1
7654 && i.reg_operands == 1
7655 && i.operands == 2
27f13469 7656 && i.types[1].bitfield.class == Reg
b3a3496f
L
7657 && (flag_code == CODE_32BIT
7658 ? i.op[1].regs->reg_type.bitfield.word
7659 : i.op[1].regs->reg_type.bitfield.dword)
7660 && ((i.base_reg == NULL && i.index_reg == NULL)
829f3fe1
JB
7661#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
7662 || (x86_elf_abi == X86_64_X32_ABI
7663 && i.base_reg
b3a3496f
L
7664 && i.base_reg->reg_num == RegIP
7665 && i.base_reg->reg_type.bitfield.qword))
829f3fe1
JB
7666#else
7667 || 0)
7668#endif
27f13469
L
7669 && !add_prefix (ADDR_PREFIX_OPCODE))
7670 return 0;
7671
c8f8eebc
JB
7672 if (flag_code == CODE_32BIT)
7673 need = i.prefix[ADDR_PREFIX] ? need_word : need_dword;
7674 else if (i.prefix[ADDR_PREFIX])
c0a30a9f
L
7675 need = need_dword;
7676 else
7677 need = flag_code == CODE_64BIT ? need_qword : need_word;
c0a30a9f 7678
c8f8eebc
JB
7679 for (op = 0; op < i.operands; op++)
7680 {
7681 if (i.types[op].bitfield.class != Reg)
7682 continue;
7683
7684 switch (need)
7685 {
7686 case need_word:
7687 if (i.op[op].regs->reg_type.bitfield.word)
7688 continue;
7689 break;
7690 case need_dword:
7691 if (i.op[op].regs->reg_type.bitfield.dword)
7692 continue;
7693 break;
7694 case need_qword:
7695 if (i.op[op].regs->reg_type.bitfield.qword)
7696 continue;
7697 break;
7698 }
7699
7700 as_bad (_("invalid register operand size for `%s'"),
76d3f746 7701 insn_name (&i.tm));
c8f8eebc
JB
7702 return 0;
7703 }
7704 }
c0a30a9f
L
7705 }
7706
29b0f896
AM
7707 return 1;
7708}
3e73aa7c 7709
29b0f896 7710static int
543613e9 7711check_byte_reg (void)
29b0f896
AM
7712{
7713 int op;
543613e9 7714
29b0f896
AM
7715 for (op = i.operands; --op >= 0;)
7716 {
dc821c5f 7717 /* Skip non-register operands. */
bab6aec1 7718 if (i.types[op].bitfield.class != Reg)
dc821c5f
JB
7719 continue;
7720
29b0f896
AM
7721 /* If this is an eight bit register, it's OK. If it's the 16 or
7722 32 bit version of an eight bit register, we will just use the
7723 low portion, and that's OK too. */
dc821c5f 7724 if (i.types[op].bitfield.byte)
29b0f896
AM
7725 continue;
7726
5a819eb9 7727 /* I/O port address operands are OK too. */
75e5731b
JB
7728 if (i.tm.operand_types[op].bitfield.instance == RegD
7729 && i.tm.operand_types[op].bitfield.word)
5a819eb9
JB
7730 continue;
7731
9706160a 7732 /* crc32 only wants its source operand checked here. */
7fc69528 7733 if (i.tm.mnem_off == MN_crc32 && op != 0)
9344ff29
L
7734 continue;
7735
29b0f896 7736 /* Any other register is bad. */
73c76375
JB
7737 as_bad (_("`%s%s' not allowed with `%s%c'"),
7738 register_prefix, i.op[op].regs->reg_name,
76d3f746 7739 insn_name (&i.tm), i.suffix);
73c76375 7740 return 0;
29b0f896
AM
7741 }
7742 return 1;
7743}
7744
7745static int
e3bb37b5 7746check_long_reg (void)
29b0f896
AM
7747{
7748 int op;
7749
7750 for (op = i.operands; --op >= 0;)
dc821c5f 7751 /* Skip non-register operands. */
bab6aec1 7752 if (i.types[op].bitfield.class != Reg)
dc821c5f 7753 continue;
29b0f896
AM
7754 /* Reject eight bit registers, except where the template requires
7755 them. (eg. movzb) */
dc821c5f 7756 else if (i.types[op].bitfield.byte
bab6aec1 7757 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7758 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7759 && (i.tm.operand_types[op].bitfield.word
7760 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7761 {
a540244d
L
7762 as_bad (_("`%s%s' not allowed with `%s%c'"),
7763 register_prefix,
29b0f896 7764 i.op[op].regs->reg_name,
76d3f746 7765 insn_name (&i.tm),
29b0f896
AM
7766 i.suffix);
7767 return 0;
7768 }
be4c5e58
L
7769 /* Error if the e prefix on a general reg is missing. */
7770 else if (i.types[op].bitfield.word
bab6aec1 7771 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7772 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7773 && i.tm.operand_types[op].bitfield.dword)
29b0f896 7774 {
be4c5e58
L
7775 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7776 register_prefix, i.op[op].regs->reg_name,
7777 i.suffix);
7778 return 0;
252b5132 7779 }
e4630f71 7780 /* Warn if the r prefix on a general reg is present. */
dc821c5f 7781 else if (i.types[op].bitfield.qword
bab6aec1 7782 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7783 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7784 && i.tm.operand_types[op].bitfield.dword)
252b5132 7785 {
1cb0ab18
JB
7786 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7787 register_prefix, i.op[op].regs->reg_name, i.suffix);
7788 return 0;
29b0f896
AM
7789 }
7790 return 1;
7791}
252b5132 7792
29b0f896 7793static int
e3bb37b5 7794check_qword_reg (void)
29b0f896
AM
7795{
7796 int op;
252b5132 7797
29b0f896 7798 for (op = i.operands; --op >= 0; )
dc821c5f 7799 /* Skip non-register operands. */
bab6aec1 7800 if (i.types[op].bitfield.class != Reg)
dc821c5f 7801 continue;
29b0f896
AM
7802 /* Reject eight bit registers, except where the template requires
7803 them. (eg. movzb) */
dc821c5f 7804 else if (i.types[op].bitfield.byte
bab6aec1 7805 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7806 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7807 && (i.tm.operand_types[op].bitfield.word
7808 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7809 {
a540244d
L
7810 as_bad (_("`%s%s' not allowed with `%s%c'"),
7811 register_prefix,
29b0f896 7812 i.op[op].regs->reg_name,
76d3f746 7813 insn_name (&i.tm),
29b0f896
AM
7814 i.suffix);
7815 return 0;
7816 }
e4630f71 7817 /* Warn if the r prefix on a general reg is missing. */
dc821c5f
JB
7818 else if ((i.types[op].bitfield.word
7819 || i.types[op].bitfield.dword)
bab6aec1 7820 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7821 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7822 && i.tm.operand_types[op].bitfield.qword)
29b0f896
AM
7823 {
7824 /* Prohibit these changes in the 64bit mode, since the
7825 lowering is more complicated. */
1cb0ab18
JB
7826 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7827 register_prefix, i.op[op].regs->reg_name, i.suffix);
7828 return 0;
252b5132 7829 }
29b0f896
AM
7830 return 1;
7831}
252b5132 7832
29b0f896 7833static int
e3bb37b5 7834check_word_reg (void)
29b0f896
AM
7835{
7836 int op;
7837 for (op = i.operands; --op >= 0;)
dc821c5f 7838 /* Skip non-register operands. */
bab6aec1 7839 if (i.types[op].bitfield.class != Reg)
dc821c5f 7840 continue;
29b0f896
AM
7841 /* Reject eight bit registers, except where the template requires
7842 them. (eg. movzb) */
dc821c5f 7843 else if (i.types[op].bitfield.byte
bab6aec1 7844 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7845 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7846 && (i.tm.operand_types[op].bitfield.word
7847 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7848 {
a540244d
L
7849 as_bad (_("`%s%s' not allowed with `%s%c'"),
7850 register_prefix,
29b0f896 7851 i.op[op].regs->reg_name,
76d3f746 7852 insn_name (&i.tm),
29b0f896
AM
7853 i.suffix);
7854 return 0;
7855 }
9706160a
JB
7856 /* Error if the e or r prefix on a general reg is present. */
7857 else if ((i.types[op].bitfield.dword
dc821c5f 7858 || i.types[op].bitfield.qword)
bab6aec1 7859 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7860 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7861 && i.tm.operand_types[op].bitfield.word)
252b5132 7862 {
9706160a
JB
7863 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7864 register_prefix, i.op[op].regs->reg_name,
7865 i.suffix);
7866 return 0;
29b0f896
AM
7867 }
7868 return 1;
7869}
252b5132 7870
29b0f896 7871static int
40fb9820 7872update_imm (unsigned int j)
29b0f896 7873{
bc0844ae 7874 i386_operand_type overlap = i.types[j];
c34d1cc9
JB
7875
7876 if (i.tm.operand_types[j].bitfield.imm8
7877 && i.tm.operand_types[j].bitfield.imm8s
7878 && overlap.bitfield.imm8 && overlap.bitfield.imm8s)
7879 {
7880 /* This combination is used on 8-bit immediates where e.g. $~0 is
7881 desirable to permit. We're past operand type matching, so simply
7882 put things back in the shape they were before introducing the
7883 distinction between Imm8, Imm8S, and Imm8|Imm8S. */
7884 overlap.bitfield.imm8s = 0;
7885 }
7886
be1643ff
JB
7887 if (overlap.bitfield.imm8
7888 + overlap.bitfield.imm8s
7889 + overlap.bitfield.imm16
7890 + overlap.bitfield.imm32
7891 + overlap.bitfield.imm32s
7892 + overlap.bitfield.imm64 > 1)
29b0f896 7893 {
05909f23
JB
7894 static const i386_operand_type imm16 = { .bitfield = { .imm16 = 1 } };
7895 static const i386_operand_type imm32 = { .bitfield = { .imm32 = 1 } };
7896 static const i386_operand_type imm32s = { .bitfield = { .imm32s = 1 } };
7897 static const i386_operand_type imm16_32 = { .bitfield =
7898 { .imm16 = 1, .imm32 = 1 }
7899 };
7900 static const i386_operand_type imm16_32s = { .bitfield =
7901 { .imm16 = 1, .imm32s = 1 }
7902 };
7903 static const i386_operand_type imm16_32_32s = { .bitfield =
7904 { .imm16 = 1, .imm32 = 1, .imm32s = 1 }
7905 };
7906
29b0f896
AM
7907 if (i.suffix)
7908 {
40fb9820
L
7909 i386_operand_type temp;
7910
0dfbf9d7 7911 operand_type_set (&temp, 0);
7ab9ffdd 7912 if (i.suffix == BYTE_MNEM_SUFFIX)
40fb9820
L
7913 {
7914 temp.bitfield.imm8 = overlap.bitfield.imm8;
7915 temp.bitfield.imm8s = overlap.bitfield.imm8s;
7916 }
7917 else if (i.suffix == WORD_MNEM_SUFFIX)
7918 temp.bitfield.imm16 = overlap.bitfield.imm16;
7919 else if (i.suffix == QWORD_MNEM_SUFFIX)
7920 {
7921 temp.bitfield.imm64 = overlap.bitfield.imm64;
7922 temp.bitfield.imm32s = overlap.bitfield.imm32s;
7923 }
7924 else
7925 temp.bitfield.imm32 = overlap.bitfield.imm32;
7926 overlap = temp;
29b0f896 7927 }
0dfbf9d7
L
7928 else if (operand_type_equal (&overlap, &imm16_32_32s)
7929 || operand_type_equal (&overlap, &imm16_32)
7930 || operand_type_equal (&overlap, &imm16_32s))
29b0f896 7931 {
40fb9820 7932 if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
65da13b5 7933 overlap = imm16;
40fb9820 7934 else
65da13b5 7935 overlap = imm32s;
29b0f896 7936 }
8bbb3ad8
JB
7937 else if (i.prefix[REX_PREFIX] & REX_W)
7938 overlap = operand_type_and (overlap, imm32s);
7939 else if (i.prefix[DATA_PREFIX])
7940 overlap = operand_type_and (overlap,
7941 flag_code != CODE_16BIT ? imm16 : imm32);
be1643ff
JB
7942 if (overlap.bitfield.imm8
7943 + overlap.bitfield.imm8s
7944 + overlap.bitfield.imm16
7945 + overlap.bitfield.imm32
7946 + overlap.bitfield.imm32s
7947 + overlap.bitfield.imm64 != 1)
29b0f896 7948 {
4eed87de
AM
7949 as_bad (_("no instruction mnemonic suffix given; "
7950 "can't determine immediate size"));
29b0f896
AM
7951 return 0;
7952 }
7953 }
40fb9820 7954 i.types[j] = overlap;
29b0f896 7955
40fb9820
L
7956 return 1;
7957}
7958
7959static int
7960finalize_imm (void)
7961{
bc0844ae 7962 unsigned int j, n;
29b0f896 7963
bc0844ae
L
7964 /* Update the first 2 immediate operands. */
7965 n = i.operands > 2 ? 2 : i.operands;
7966 if (n)
7967 {
7968 for (j = 0; j < n; j++)
7969 if (update_imm (j) == 0)
7970 return 0;
40fb9820 7971
bc0844ae
L
7972 /* The 3rd operand can't be immediate operand. */
7973 gas_assert (operand_type_check (i.types[2], imm) == 0);
7974 }
29b0f896
AM
7975
7976 return 1;
7977}
7978
0a3eba42
JB
7979static INLINE void set_rex_vrex (const reg_entry *r, unsigned int rex_bit,
7980 bool do_sse2avx)
7981{
7982 if (r->reg_flags & RegRex)
7983 {
7984 if (i.rex & rex_bit)
7985 as_bad (_("same type of prefix used twice"));
7986 i.rex |= rex_bit;
7987 }
7988 else if (do_sse2avx && (i.rex & rex_bit) && i.vex.register_specifier)
7989 {
7990 gas_assert (i.vex.register_specifier == r);
7991 i.vex.register_specifier += 8;
7992 }
7993
7994 if (r->reg_flags & RegVRex)
7995 i.vrex |= rex_bit;
7996}
7997
29b0f896 7998static int
e3bb37b5 7999process_operands (void)
29b0f896
AM
8000{
8001 /* Default segment register this instruction will use for memory
8002 accesses. 0 means unknown. This is only for optimizing out
8003 unnecessary segment overrides. */
5e042380 8004 const reg_entry *default_seg = NULL;
29b0f896 8005
31b4cda3
JB
8006 /* We only need to check those implicit registers for instructions
8007 with 3 operands or less. */
8008 if (i.operands <= 3)
8009 for (unsigned int j = 0; j < i.operands; j++)
8010 if (i.types[j].bitfield.instance != InstanceNone)
8011 i.reg_operands--;
8012
a5aeccd9
JB
8013 if (i.tm.opcode_modifier.sse2avx)
8014 {
8015 /* Legacy encoded insns allow explicit REX prefixes, so these prefixes
8016 need converting. */
8017 i.rex |= i.prefix[REX_PREFIX] & (REX_W | REX_R | REX_X | REX_B);
8018 i.prefix[REX_PREFIX] = 0;
8019 i.rex_encoding = 0;
8020 }
c423d21a
JB
8021 /* ImmExt should be processed after SSE2AVX. */
8022 else if (i.tm.opcode_modifier.immext)
8023 process_immext ();
a5aeccd9 8024
ecb96e55
JB
8025 /* TILEZERO is unusual in that it has a single operand encoded in ModR/M.reg,
8026 not ModR/M.rm. To avoid special casing this in build_modrm_byte(), fake a
8027 new destination operand here, while converting the source one to register
8028 number 0. */
8029 if (i.tm.mnem_off == MN_tilezero)
8030 {
8031 i.op[1].regs = i.op[0].regs;
8032 i.op[0].regs -= i.op[0].regs->reg_num;
8033 i.types[1] = i.types[0];
8034 i.tm.operand_types[1] = i.tm.operand_types[0];
8035 i.flags[1] = i.flags[0];
8036 i.operands++;
8037 i.reg_operands++;
8038 i.tm.operands++;
8039 }
8040
2426c15f 8041 if (i.tm.opcode_modifier.sse2avx && i.tm.opcode_modifier.vexvvvv)
29b0f896 8042 {
05909f23
JB
8043 static const i386_operand_type regxmm = {
8044 .bitfield = { .class = RegSIMD, .xmmword = 1 }
8045 };
91d6fa6a
NC
8046 unsigned int dupl = i.operands;
8047 unsigned int dest = dupl - 1;
9fcfb3d7
L
8048 unsigned int j;
8049
c0f3af97 8050 /* The destination must be an xmm register. */
9c2799c2 8051 gas_assert (i.reg_operands
91d6fa6a 8052 && MAX_OPERANDS > dupl
7ab9ffdd 8053 && operand_type_equal (&i.types[dest], &regxmm));
c0f3af97 8054
75e5731b 8055 if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 8056 && i.tm.operand_types[0].bitfield.xmmword)
e2ec9d29 8057 {
95dfdd85
JB
8058 /* Keep xmm0 for instructions with VEX prefix and 3
8059 sources. */
8060 i.tm.operand_types[0].bitfield.instance = InstanceNone;
8061 i.tm.operand_types[0].bitfield.class = RegSIMD;
31b4cda3 8062 i.reg_operands++;
95dfdd85 8063 goto duplicate;
c0f3af97 8064 }
95dfdd85
JB
8065
8066 if (i.tm.opcode_modifier.operandconstraint == IMPLICIT_1ST_XMM0)
7ab9ffdd 8067 {
aa180741 8068 gas_assert ((MAX_OPERANDS - 1) > dupl);
c0f3af97
L
8069
8070 /* Add the implicit xmm0 for instructions with VEX prefix
8071 and 3 sources. */
8072 for (j = i.operands; j > 0; j--)
8073 {
8074 i.op[j] = i.op[j - 1];
8075 i.types[j] = i.types[j - 1];
8076 i.tm.operand_types[j] = i.tm.operand_types[j - 1];
8dc0818e 8077 i.flags[j] = i.flags[j - 1];
c0f3af97
L
8078 }
8079 i.op[0].regs
629310ab 8080 = (const reg_entry *) str_hash_find (reg_hash, "xmm0");
7ab9ffdd 8081 i.types[0] = regxmm;
c0f3af97
L
8082 i.tm.operand_types[0] = regxmm;
8083
8084 i.operands += 2;
8085 i.reg_operands += 2;
8086 i.tm.operands += 2;
8087
91d6fa6a 8088 dupl++;
c0f3af97 8089 dest++;
91d6fa6a
NC
8090 i.op[dupl] = i.op[dest];
8091 i.types[dupl] = i.types[dest];
8092 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 8093 i.flags[dupl] = i.flags[dest];
e2ec9d29 8094 }
c0f3af97
L
8095 else
8096 {
dc1e8a47 8097 duplicate:
c0f3af97
L
8098 i.operands++;
8099 i.reg_operands++;
8100 i.tm.operands++;
8101
91d6fa6a
NC
8102 i.op[dupl] = i.op[dest];
8103 i.types[dupl] = i.types[dest];
8104 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 8105 i.flags[dupl] = i.flags[dest];
c0f3af97
L
8106 }
8107
8108 if (i.tm.opcode_modifier.immext)
8109 process_immext ();
8110 }
75e5731b 8111 else if (i.tm.operand_types[0].bitfield.instance == Accum
bd782808 8112 && i.tm.opcode_modifier.modrm)
c0f3af97
L
8113 {
8114 unsigned int j;
8115
9fcfb3d7
L
8116 for (j = 1; j < i.operands; j++)
8117 {
8118 i.op[j - 1] = i.op[j];
8119 i.types[j - 1] = i.types[j];
8120
8121 /* We need to adjust fields in i.tm since they are used by
8122 build_modrm_byte. */
8123 i.tm.operand_types [j - 1] = i.tm.operand_types [j];
8dc0818e
JB
8124
8125 i.flags[j - 1] = i.flags[j];
9fcfb3d7
L
8126 }
8127
31b4cda3
JB
8128 /* No adjustment to i.reg_operands: This was already done at the top
8129 of the function. */
e2ec9d29 8130 i.operands--;
e2ec9d29
L
8131 i.tm.operands--;
8132 }
255571cd 8133 else if (i.tm.opcode_modifier.operandconstraint == IMPLICIT_QUAD_GROUP)
920d2ddc 8134 {
a477a8c4
JB
8135 unsigned int regnum, first_reg_in_group, last_reg_in_group;
8136
920d2ddc 8137 /* The second operand must be {x,y,z}mmN, where N is a multiple of 4. */
3528c362 8138 gas_assert (i.operands >= 2 && i.types[1].bitfield.class == RegSIMD);
a477a8c4
JB
8139 regnum = register_number (i.op[1].regs);
8140 first_reg_in_group = regnum & ~3;
8141 last_reg_in_group = first_reg_in_group + 3;
8142 if (regnum != first_reg_in_group)
8143 as_warn (_("source register `%s%s' implicitly denotes"
8144 " `%s%.3s%u' to `%s%.3s%u' source group in `%s'"),
8145 register_prefix, i.op[1].regs->reg_name,
8146 register_prefix, i.op[1].regs->reg_name, first_reg_in_group,
8147 register_prefix, i.op[1].regs->reg_name, last_reg_in_group,
76d3f746 8148 insn_name (&i.tm));
a477a8c4 8149 }
255571cd 8150 else if (i.tm.opcode_modifier.operandconstraint == REG_KLUDGE)
e2ec9d29
L
8151 {
8152 /* The imul $imm, %reg instruction is converted into
8153 imul $imm, %reg, %reg, and the clr %reg instruction
8154 is converted into xor %reg, %reg. */
8155
8156 unsigned int first_reg_op;
8157
8158 if (operand_type_check (i.types[0], reg))
8159 first_reg_op = 0;
8160 else
8161 first_reg_op = 1;
8162 /* Pretend we saw the extra register operand. */
9c2799c2 8163 gas_assert (i.reg_operands == 1
7ab9ffdd 8164 && i.op[first_reg_op + 1].regs == 0);
e2ec9d29
L
8165 i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
8166 i.types[first_reg_op + 1] = i.types[first_reg_op];
8167 i.operands++;
8168 i.reg_operands++;
29b0f896
AM
8169 }
8170
85b80b0f 8171 if (i.tm.opcode_modifier.modrm)
29b0f896
AM
8172 {
8173 /* The opcode is completed (modulo i.tm.extension_opcode which
52271982
AM
8174 must be put into the modrm byte). Now, we make the modrm and
8175 index base bytes based on all the info we've collected. */
29b0f896
AM
8176
8177 default_seg = build_modrm_byte ();
bd782808
JB
8178
8179 if (!quiet_warnings && i.tm.opcode_modifier.operandconstraint == UGH)
8180 {
8181 /* Warn about some common errors, but press on regardless. */
8182 if (i.operands == 2)
8183 {
8184 /* Reversed arguments on faddp or fmulp. */
8185 as_warn (_("translating to `%s %s%s,%s%s'"), insn_name (&i.tm),
8186 register_prefix, i.op[!intel_syntax].regs->reg_name,
8187 register_prefix, i.op[intel_syntax].regs->reg_name);
8188 }
8189 else if (i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
8190 {
8191 /* Extraneous `l' suffix on fp insn. */
8192 as_warn (_("translating to `%s %s%s'"), insn_name (&i.tm),
8193 register_prefix, i.op[0].regs->reg_name);
8194 }
8195 }
29b0f896 8196 }
00cee14f 8197 else if (i.types[0].bitfield.class == SReg)
85b80b0f
JB
8198 {
8199 if (flag_code != CODE_64BIT
8200 ? i.tm.base_opcode == POP_SEG_SHORT
8201 && i.op[0].regs->reg_num == 1
389d00a5 8202 : (i.tm.base_opcode | 1) == (POP_SEG386_SHORT & 0xff)
85b80b0f
JB
8203 && i.op[0].regs->reg_num < 4)
8204 {
8205 as_bad (_("you can't `%s %s%s'"),
76d3f746 8206 insn_name (&i.tm), register_prefix, i.op[0].regs->reg_name);
85b80b0f
JB
8207 return 0;
8208 }
389d00a5 8209 if (i.op[0].regs->reg_num > 3
ddb62495 8210 && i.tm.opcode_space == SPACE_BASE )
85b80b0f 8211 {
389d00a5 8212 i.tm.base_opcode ^= (POP_SEG_SHORT ^ POP_SEG386_SHORT) & 0xff;
ddb62495 8213 i.tm.opcode_space = SPACE_0F;
85b80b0f
JB
8214 }
8215 i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
8216 }
ddb62495 8217 else if (i.tm.opcode_space == SPACE_BASE
389d00a5 8218 && (i.tm.base_opcode & ~3) == MOV_AX_DISP32)
29b0f896 8219 {
5e042380 8220 default_seg = reg_ds;
29b0f896 8221 }
40fb9820 8222 else if (i.tm.opcode_modifier.isstring)
29b0f896
AM
8223 {
8224 /* For the string instructions that allow a segment override
8225 on one of their operands, the default segment is ds. */
5e042380 8226 default_seg = reg_ds;
29b0f896 8227 }
50128d0c 8228 else if (i.short_form)
85b80b0f 8229 {
bd782808 8230 /* The register operand is in operand 0 or 1. */
4943d587 8231 const reg_entry *r = i.op[0].regs;
85b80b0f 8232
4943d587
JB
8233 if (i.imm_operands
8234 || (r->reg_type.bitfield.instance == Accum && i.op[1].regs))
8235 r = i.op[1].regs;
85b80b0f 8236 /* Register goes in low 3 bits of opcode. */
4943d587 8237 i.tm.base_opcode |= r->reg_num;
0a3eba42 8238 set_rex_vrex (r, REX_B, false);
85b80b0f 8239 }
29b0f896 8240
514a8bb0 8241 if ((i.seg[0] || i.prefix[SEG_PREFIX])
7fc69528 8242 && i.tm.mnem_off == MN_lea)
92334ad2
JB
8243 {
8244 if (!quiet_warnings)
76d3f746 8245 as_warn (_("segment override on `%s' is ineffectual"), insn_name (&i.tm));
739d7649 8246 if (optimize && !i.no_optimize)
92334ad2
JB
8247 {
8248 i.seg[0] = NULL;
8249 i.prefix[SEG_PREFIX] = 0;
8250 }
8251 }
52271982
AM
8252
8253 /* If a segment was explicitly specified, and the specified segment
b6773884
JB
8254 is neither the default nor the one already recorded from a prefix,
8255 use an opcode prefix to select it. If we never figured out what
8256 the default segment is, then default_seg will be zero at this
8257 point, and the specified segment prefix will always be used. */
8258 if (i.seg[0]
8259 && i.seg[0] != default_seg
5e042380 8260 && i386_seg_prefixes[i.seg[0]->reg_num] != i.prefix[SEG_PREFIX])
29b0f896 8261 {
5e042380 8262 if (!add_prefix (i386_seg_prefixes[i.seg[0]->reg_num]))
29b0f896
AM
8263 return 0;
8264 }
8265 return 1;
8266}
8267
5e042380 8268static const reg_entry *
e3bb37b5 8269build_modrm_byte (void)
29b0f896 8270{
5e042380 8271 const reg_entry *default_seg = NULL;
ecb96e55
JB
8272 unsigned int source = i.imm_operands - i.tm.opcode_modifier.immext
8273 /* Compensate for kludge in md_assemble(). */
8274 + i.tm.operand_types[0].bitfield.imm1;
8275 unsigned int dest = i.operands - 1 - i.tm.opcode_modifier.immext;
8276 unsigned int v, op, reg_slot = ~0;
8277
8278 /* Accumulator (in particular %st), shift count (%cl), and alike need
8279 to be skipped just like immediate operands do. */
8280 if (i.tm.operand_types[source].bitfield.instance)
8281 ++source;
8282 while (i.tm.operand_types[dest].bitfield.instance)
8283 --dest;
8284
8285 for (op = source; op < i.operands; ++op)
8286 if (i.tm.operand_types[op].bitfield.baseindex)
8287 break;
c0f3af97 8288
ecb96e55 8289 if (i.reg_operands + i.mem_operands + (i.tm.extension_opcode != None) == 4)
c0f3af97 8290 {
4c2c6516 8291 expressionS *exp;
c0f3af97 8292
a683cc34 8293 /* There are 2 kinds of instructions:
bed3d976 8294 1. 5 operands: 4 register operands or 3 register operands
9d3bf266 8295 plus 1 memory operand plus one Imm4 operand, VexXDS, and
bed3d976 8296 VexW0 or VexW1. The destination must be either XMM, YMM or
43234a1e 8297 ZMM register.
bed3d976 8298 2. 4 operands: 4 register operands or 3 register operands
2f1bada2 8299 plus 1 memory operand, with VexXDS. */
eea96d3f 8300 gas_assert (i.tm.opcode_modifier.vexvvvv
dcd7e323 8301 && i.tm.opcode_modifier.vexw
3528c362 8302 && i.tm.operand_types[dest].bitfield.class == RegSIMD);
a683cc34 8303
ecb96e55
JB
8304 /* Of the first two non-immediate operands the one with the template
8305 not allowing for a memory one is encoded in the immediate operand. */
8306 if (source == op)
8307 reg_slot = source + 1;
48db9223 8308 else
ecb96e55 8309 reg_slot = source++;
48db9223 8310
a683cc34 8311 if (i.imm_operands == 0)
bed3d976
JB
8312 {
8313 /* When there is no immediate operand, generate an 8bit
8314 immediate operand to encode the first operand. */
8315 exp = &im_expressions[i.imm_operands++];
8316 i.op[i.operands].imms = exp;
be1643ff 8317 i.types[i.operands].bitfield.imm8 = 1;
bed3d976
JB
8318 i.operands++;
8319
3528c362 8320 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
bed3d976
JB
8321 exp->X_op = O_constant;
8322 exp->X_add_number = register_number (i.op[reg_slot].regs) << 4;
43234a1e
L
8323 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
8324 }
922d8de8 8325 else
bed3d976 8326 {
9d3bf266
JB
8327 gas_assert (i.imm_operands == 1);
8328 gas_assert (fits_in_imm4 (i.op[0].imms->X_add_number));
8329 gas_assert (!i.tm.opcode_modifier.immext);
a683cc34 8330
9d3bf266
JB
8331 /* Turn on Imm8 again so that output_imm will generate it. */
8332 i.types[0].bitfield.imm8 = 1;
bed3d976 8333
3528c362 8334 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
9d3bf266 8335 i.op[0].imms->X_add_number
bed3d976 8336 |= register_number (i.op[reg_slot].regs) << 4;
43234a1e 8337 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
bed3d976 8338 }
c0f3af97 8339 }
f12dc422 8340
ecb96e55
JB
8341 for (v = source + 1; v < dest; ++v)
8342 if (v != reg_slot)
8343 break;
8344 if (v >= dest)
8345 v = ~0;
8346 if (i.tm.extension_opcode != None)
8347 {
8348 if (dest != source)
8349 v = dest;
8350 dest = ~0;
8351 }
8352 gas_assert (source < dest);
8353 if (i.tm.opcode_modifier.operandconstraint == SWAP_SOURCES
8354 && source != op)
8355 {
8356 unsigned int tmp = source;
f12dc422 8357
ecb96e55
JB
8358 source = v;
8359 v = tmp;
8360 }
29b0f896 8361
ecb96e55
JB
8362 if (v < MAX_OPERANDS)
8363 {
8364 gas_assert (i.tm.opcode_modifier.vexvvvv);
8365 i.vex.register_specifier = i.op[v].regs;
29b0f896 8366 }
c0f3af97 8367
ecb96e55
JB
8368 if (op < i.operands)
8369 {
29b0f896
AM
8370 if (i.mem_operands)
8371 {
8372 unsigned int fake_zero_displacement = 0;
4eed87de 8373
ecb96e55 8374 gas_assert (i.flags[op] & Operand_Mem);
29b0f896 8375
63112cd6 8376 if (i.tm.opcode_modifier.sib)
6c30d220 8377 {
260cd341
LC
8378 /* The index register of VSIB shouldn't be RegIZ. */
8379 if (i.tm.opcode_modifier.sib != SIBMEM
8380 && i.index_reg->reg_num == RegIZ)
6c30d220
L
8381 abort ();
8382
8383 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8384 if (!i.base_reg)
8385 {
8386 i.sib.base = NO_BASE_REGISTER;
8387 i.sib.scale = i.log2_scale_factor;
2f2be86b 8388 i.types[op] = operand_type_and_not (i.types[op], anydisp);
a775efc8 8389 i.types[op].bitfield.disp32 = 1;
6c30d220 8390 }
260cd341
LC
8391
8392 /* Since the mandatory SIB always has index register, so
8393 the code logic remains unchanged. The non-mandatory SIB
8394 without index register is allowed and will be handled
8395 later. */
8396 if (i.index_reg)
8397 {
8398 if (i.index_reg->reg_num == RegIZ)
8399 i.sib.index = NO_INDEX_REGISTER;
8400 else
8401 i.sib.index = i.index_reg->reg_num;
5b7c81bd 8402 set_rex_vrex (i.index_reg, REX_X, false);
260cd341 8403 }
6c30d220
L
8404 }
8405
5e042380 8406 default_seg = reg_ds;
29b0f896
AM
8407
8408 if (i.base_reg == 0)
8409 {
8410 i.rm.mode = 0;
8411 if (!i.disp_operands)
9bb129e8 8412 fake_zero_displacement = 1;
29b0f896
AM
8413 if (i.index_reg == 0)
8414 {
260cd341
LC
8415 /* Both check for VSIB and mandatory non-vector SIB. */
8416 gas_assert (!i.tm.opcode_modifier.sib
8417 || i.tm.opcode_modifier.sib == SIBMEM);
29b0f896 8418 /* Operand is just <disp> */
2f2be86b 8419 i.types[op] = operand_type_and_not (i.types[op], anydisp);
20f0a1fc 8420 if (flag_code == CODE_64BIT)
29b0f896
AM
8421 {
8422 /* 64bit mode overwrites the 32bit absolute
8423 addressing by RIP relative addressing and
8424 absolute addressing is encoded by one of the
8425 redundant SIB forms. */
8426 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8427 i.sib.base = NO_BASE_REGISTER;
8428 i.sib.index = NO_INDEX_REGISTER;
a775efc8 8429 i.types[op].bitfield.disp32 = 1;
20f0a1fc 8430 }
fc225355
L
8431 else if ((flag_code == CODE_16BIT)
8432 ^ (i.prefix[ADDR_PREFIX] != 0))
20f0a1fc
NC
8433 {
8434 i.rm.regmem = NO_BASE_REGISTER_16;
2f2be86b 8435 i.types[op].bitfield.disp16 = 1;
20f0a1fc
NC
8436 }
8437 else
8438 {
8439 i.rm.regmem = NO_BASE_REGISTER;
2f2be86b 8440 i.types[op].bitfield.disp32 = 1;
29b0f896
AM
8441 }
8442 }
63112cd6 8443 else if (!i.tm.opcode_modifier.sib)
29b0f896 8444 {
6c30d220 8445 /* !i.base_reg && i.index_reg */
e968fc9b 8446 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8447 i.sib.index = NO_INDEX_REGISTER;
8448 else
8449 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8450 i.sib.base = NO_BASE_REGISTER;
8451 i.sib.scale = i.log2_scale_factor;
8452 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
2f2be86b 8453 i.types[op] = operand_type_and_not (i.types[op], anydisp);
a775efc8 8454 i.types[op].bitfield.disp32 = 1;
29b0f896 8455 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8456 i.rex |= REX_X;
29b0f896
AM
8457 }
8458 }
8459 /* RIP addressing for 64bit mode. */
e968fc9b 8460 else if (i.base_reg->reg_num == RegIP)
29b0f896 8461 {
63112cd6 8462 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896 8463 i.rm.regmem = NO_BASE_REGISTER;
40fb9820
L
8464 i.types[op].bitfield.disp8 = 0;
8465 i.types[op].bitfield.disp16 = 0;
a775efc8 8466 i.types[op].bitfield.disp32 = 1;
40fb9820 8467 i.types[op].bitfield.disp64 = 0;
71903a11 8468 i.flags[op] |= Operand_PCrel;
20f0a1fc
NC
8469 if (! i.disp_operands)
8470 fake_zero_displacement = 1;
29b0f896 8471 }
dc821c5f 8472 else if (i.base_reg->reg_type.bitfield.word)
29b0f896 8473 {
63112cd6 8474 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896
AM
8475 switch (i.base_reg->reg_num)
8476 {
8477 case 3: /* (%bx) */
8478 if (i.index_reg == 0)
8479 i.rm.regmem = 7;
8480 else /* (%bx,%si) -> 0, or (%bx,%di) -> 1 */
8481 i.rm.regmem = i.index_reg->reg_num - 6;
8482 break;
8483 case 5: /* (%bp) */
5e042380 8484 default_seg = reg_ss;
29b0f896
AM
8485 if (i.index_reg == 0)
8486 {
8487 i.rm.regmem = 6;
40fb9820 8488 if (operand_type_check (i.types[op], disp) == 0)
29b0f896
AM
8489 {
8490 /* fake (%bp) into 0(%bp) */
41eb8e88 8491 if (i.disp_encoding == disp_encoding_16bit)
1a02d6b0
L
8492 i.types[op].bitfield.disp16 = 1;
8493 else
8494 i.types[op].bitfield.disp8 = 1;
252b5132 8495 fake_zero_displacement = 1;
29b0f896
AM
8496 }
8497 }
8498 else /* (%bp,%si) -> 2, or (%bp,%di) -> 3 */
8499 i.rm.regmem = i.index_reg->reg_num - 6 + 2;
8500 break;
8501 default: /* (%si) -> 4 or (%di) -> 5 */
8502 i.rm.regmem = i.base_reg->reg_num - 6 + 4;
8503 }
41eb8e88
L
8504 if (!fake_zero_displacement
8505 && !i.disp_operands
8506 && i.disp_encoding)
8507 {
8508 fake_zero_displacement = 1;
8509 if (i.disp_encoding == disp_encoding_8bit)
8510 i.types[op].bitfield.disp8 = 1;
8511 else
8512 i.types[op].bitfield.disp16 = 1;
8513 }
29b0f896
AM
8514 i.rm.mode = mode_from_disp_size (i.types[op]);
8515 }
8516 else /* i.base_reg and 32/64 bit mode */
8517 {
a9aabc23 8518 if (operand_type_check (i.types[op], disp))
40fb9820 8519 {
73053c1f
JB
8520 i.types[op].bitfield.disp16 = 0;
8521 i.types[op].bitfield.disp64 = 0;
a775efc8 8522 i.types[op].bitfield.disp32 = 1;
40fb9820 8523 }
20f0a1fc 8524
63112cd6 8525 if (!i.tm.opcode_modifier.sib)
6c30d220 8526 i.rm.regmem = i.base_reg->reg_num;
29b0f896 8527 if ((i.base_reg->reg_flags & RegRex) != 0)
161a04f6 8528 i.rex |= REX_B;
29b0f896
AM
8529 i.sib.base = i.base_reg->reg_num;
8530 /* x86-64 ignores REX prefix bit here to avoid decoder
8531 complications. */
848930b2
JB
8532 if (!(i.base_reg->reg_flags & RegRex)
8533 && (i.base_reg->reg_num == EBP_REG_NUM
8534 || i.base_reg->reg_num == ESP_REG_NUM))
5e042380 8535 default_seg = reg_ss;
848930b2 8536 if (i.base_reg->reg_num == 5 && i.disp_operands == 0)
29b0f896 8537 {
848930b2 8538 fake_zero_displacement = 1;
1a02d6b0
L
8539 if (i.disp_encoding == disp_encoding_32bit)
8540 i.types[op].bitfield.disp32 = 1;
8541 else
8542 i.types[op].bitfield.disp8 = 1;
29b0f896
AM
8543 }
8544 i.sib.scale = i.log2_scale_factor;
8545 if (i.index_reg == 0)
8546 {
260cd341
LC
8547 /* Only check for VSIB. */
8548 gas_assert (i.tm.opcode_modifier.sib != VECSIB128
8549 && i.tm.opcode_modifier.sib != VECSIB256
8550 && i.tm.opcode_modifier.sib != VECSIB512);
8551
29b0f896
AM
8552 /* <disp>(%esp) becomes two byte modrm with no index
8553 register. We've already stored the code for esp
8554 in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
8555 Any base register besides %esp will not use the
8556 extra modrm byte. */
8557 i.sib.index = NO_INDEX_REGISTER;
29b0f896 8558 }
63112cd6 8559 else if (!i.tm.opcode_modifier.sib)
29b0f896 8560 {
e968fc9b 8561 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8562 i.sib.index = NO_INDEX_REGISTER;
8563 else
8564 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8565 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8566 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8567 i.rex |= REX_X;
29b0f896 8568 }
67a4f2b7
AO
8569
8570 if (i.disp_operands
8571 && (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
8572 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL))
8573 i.rm.mode = 0;
8574 else
a501d77e
L
8575 {
8576 if (!fake_zero_displacement
8577 && !i.disp_operands
8578 && i.disp_encoding)
8579 {
8580 fake_zero_displacement = 1;
8581 if (i.disp_encoding == disp_encoding_8bit)
8582 i.types[op].bitfield.disp8 = 1;
8583 else
8584 i.types[op].bitfield.disp32 = 1;
8585 }
8586 i.rm.mode = mode_from_disp_size (i.types[op]);
8587 }
29b0f896 8588 }
252b5132 8589
29b0f896
AM
8590 if (fake_zero_displacement)
8591 {
8592 /* Fakes a zero displacement assuming that i.types[op]
8593 holds the correct displacement size. */
8594 expressionS *exp;
8595
9c2799c2 8596 gas_assert (i.op[op].disps == 0);
29b0f896
AM
8597 exp = &disp_expressions[i.disp_operands++];
8598 i.op[op].disps = exp;
8599 exp->X_op = O_constant;
8600 exp->X_add_number = 0;
8601 exp->X_add_symbol = (symbolS *) 0;
8602 exp->X_op_symbol = (symbolS *) 0;
8603 }
8604 }
ecb96e55
JB
8605 else
8606 {
8607 i.rm.mode = 3;
8608 i.rm.regmem = i.op[op].regs->reg_num;
8609 set_rex_vrex (i.op[op].regs, REX_B, false);
8610 }
252b5132 8611
ecb96e55
JB
8612 if (op == dest)
8613 dest = ~0;
8614 if (op == source)
8615 source = ~0;
8616 }
8617 else
8618 {
8619 i.rm.mode = 3;
8620 if (!i.tm.opcode_modifier.regmem)
f88c9eb0 8621 {
ecb96e55
JB
8622 gas_assert (source < MAX_OPERANDS);
8623 i.rm.regmem = i.op[source].regs->reg_num;
8624 set_rex_vrex (i.op[source].regs, REX_B,
8625 dest >= MAX_OPERANDS && i.tm.opcode_modifier.sse2avx);
8626 source = ~0;
f88c9eb0 8627 }
ecb96e55 8628 else
29b0f896 8629 {
ecb96e55
JB
8630 gas_assert (dest < MAX_OPERANDS);
8631 i.rm.regmem = i.op[dest].regs->reg_num;
8632 set_rex_vrex (i.op[dest].regs, REX_B, i.tm.opcode_modifier.sse2avx);
8633 dest = ~0;
29b0f896 8634 }
ecb96e55 8635 }
252b5132 8636
ecb96e55
JB
8637 /* Fill in i.rm.reg field with extension opcode (if any) or the
8638 appropriate register. */
8639 if (i.tm.extension_opcode != None)
8640 i.rm.reg = i.tm.extension_opcode;
8641 else if (!i.tm.opcode_modifier.regmem && dest < MAX_OPERANDS)
8642 {
8643 i.rm.reg = i.op[dest].regs->reg_num;
8644 set_rex_vrex (i.op[dest].regs, REX_R, i.tm.opcode_modifier.sse2avx);
8645 }
8646 else
8647 {
8648 gas_assert (source < MAX_OPERANDS);
8649 i.rm.reg = i.op[source].regs->reg_num;
8650 set_rex_vrex (i.op[source].regs, REX_R, false);
29b0f896 8651 }
ecb96e55
JB
8652
8653 if (flag_code != CODE_64BIT && (i.rex & REX_R))
8654 {
8655 gas_assert (i.types[!i.tm.opcode_modifier.regmem].bitfield.class == RegCR);
8656 i.rex &= ~REX_R;
8657 add_prefix (LOCK_PREFIX_OPCODE);
8658 }
8659
29b0f896
AM
8660 return default_seg;
8661}
252b5132 8662
48ef937e
JB
8663static INLINE void
8664frag_opcode_byte (unsigned char byte)
8665{
8666 if (now_seg != absolute_section)
8667 FRAG_APPEND_1_CHAR (byte);
8668 else
8669 ++abs_section_offset;
8670}
8671
376cd056
JB
8672static unsigned int
8673flip_code16 (unsigned int code16)
8674{
8675 gas_assert (i.tm.operands == 1);
8676
8677 return !(i.prefix[REX_PREFIX] & REX_W)
8678 && (code16 ? i.tm.operand_types[0].bitfield.disp32
376cd056
JB
8679 : i.tm.operand_types[0].bitfield.disp16)
8680 ? CODE16 : 0;
8681}
8682
29b0f896 8683static void
e3bb37b5 8684output_branch (void)
29b0f896
AM
8685{
8686 char *p;
f8a5c266 8687 int size;
29b0f896
AM
8688 int code16;
8689 int prefix;
8690 relax_substateT subtype;
8691 symbolS *sym;
8692 offsetT off;
8693
48ef937e
JB
8694 if (now_seg == absolute_section)
8695 {
8696 as_bad (_("relaxable branches not supported in absolute section"));
8697 return;
8698 }
8699
f8a5c266 8700 code16 = flag_code == CODE_16BIT ? CODE16 : 0;
1a42a9fe 8701 size = i.disp_encoding > disp_encoding_8bit ? BIG : SMALL;
29b0f896
AM
8702
8703 prefix = 0;
8704 if (i.prefix[DATA_PREFIX] != 0)
252b5132 8705 {
29b0f896
AM
8706 prefix = 1;
8707 i.prefixes -= 1;
376cd056 8708 code16 ^= flip_code16(code16);
252b5132 8709 }
29b0f896
AM
8710 /* Pentium4 branch hints. */
8711 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8712 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
2f66722d 8713 {
29b0f896
AM
8714 prefix++;
8715 i.prefixes--;
8716 }
8717 if (i.prefix[REX_PREFIX] != 0)
8718 {
8719 prefix++;
8720 i.prefixes--;
2f66722d
AM
8721 }
8722
7e8b059b
L
8723 /* BND prefixed jump. */
8724 if (i.prefix[BND_PREFIX] != 0)
8725 {
6cb0a70e
JB
8726 prefix++;
8727 i.prefixes--;
7e8b059b
L
8728 }
8729
f2810fe0 8730 if (i.prefixes != 0)
76d3f746 8731 as_warn (_("skipping prefixes on `%s'"), insn_name (&i.tm));
29b0f896
AM
8732
8733 /* It's always a symbol; End frag & setup for relax.
8734 Make sure there is enough room in this frag for the largest
8735 instruction we may generate in md_convert_frag. This is 2
8736 bytes for the opcode and room for the prefix and largest
8737 displacement. */
8738 frag_grow (prefix + 2 + 4);
8739 /* Prefix and 1 opcode byte go in fr_fix. */
8740 p = frag_more (prefix + 1);
8741 if (i.prefix[DATA_PREFIX] != 0)
8742 *p++ = DATA_PREFIX_OPCODE;
8743 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
8744 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
8745 *p++ = i.prefix[SEG_PREFIX];
6cb0a70e
JB
8746 if (i.prefix[BND_PREFIX] != 0)
8747 *p++ = BND_PREFIX_OPCODE;
29b0f896
AM
8748 if (i.prefix[REX_PREFIX] != 0)
8749 *p++ = i.prefix[REX_PREFIX];
8750 *p = i.tm.base_opcode;
8751
8752 if ((unsigned char) *p == JUMP_PC_RELATIVE)
f8a5c266 8753 subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, size);
40fb9820 8754 else if (cpu_arch_flags.bitfield.cpui386)
f8a5c266 8755 subtype = ENCODE_RELAX_STATE (COND_JUMP, size);
29b0f896 8756 else
f8a5c266 8757 subtype = ENCODE_RELAX_STATE (COND_JUMP86, size);
29b0f896 8758 subtype |= code16;
3e73aa7c 8759
29b0f896
AM
8760 sym = i.op[0].disps->X_add_symbol;
8761 off = i.op[0].disps->X_add_number;
3e73aa7c 8762
29b0f896
AM
8763 if (i.op[0].disps->X_op != O_constant
8764 && i.op[0].disps->X_op != O_symbol)
3e73aa7c 8765 {
29b0f896
AM
8766 /* Handle complex expressions. */
8767 sym = make_expr_symbol (i.op[0].disps);
8768 off = 0;
8769 }
3e73aa7c 8770
1ef3994a
JB
8771 frag_now->tc_frag_data.code64 = flag_code == CODE_64BIT;
8772
29b0f896
AM
8773 /* 1 possible extra opcode + 4 byte displacement go in var part.
8774 Pass reloc in fr_var. */
d258b828 8775 frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
29b0f896 8776}
3e73aa7c 8777
bd7ab16b
L
8778#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8779/* Return TRUE iff PLT32 relocation should be used for branching to
8780 symbol S. */
8781
5b7c81bd 8782static bool
bd7ab16b
L
8783need_plt32_p (symbolS *s)
8784{
8785 /* PLT32 relocation is ELF only. */
8786 if (!IS_ELF)
5b7c81bd 8787 return false;
bd7ab16b 8788
a5def729
RO
8789#ifdef TE_SOLARIS
8790 /* Don't emit PLT32 relocation on Solaris: neither native linker nor
8791 krtld support it. */
5b7c81bd 8792 return false;
a5def729
RO
8793#endif
8794
bd7ab16b
L
8795 /* Since there is no need to prepare for PLT branch on x86-64, we
8796 can generate R_X86_64_PLT32, instead of R_X86_64_PC32, which can
8797 be used as a marker for 32-bit PC-relative branches. */
8798 if (!object_64bit)
5b7c81bd 8799 return false;
bd7ab16b 8800
44365e88 8801 if (s == NULL)
5b7c81bd 8802 return false;
44365e88 8803
bd7ab16b
L
8804 /* Weak or undefined symbol need PLT32 relocation. */
8805 if (S_IS_WEAK (s) || !S_IS_DEFINED (s))
5b7c81bd 8806 return true;
bd7ab16b
L
8807
8808 /* Non-global symbol doesn't need PLT32 relocation. */
8809 if (! S_IS_EXTERNAL (s))
5b7c81bd 8810 return false;
bd7ab16b
L
8811
8812 /* Other global symbols need PLT32 relocation. NB: Symbol with
8813 non-default visibilities are treated as normal global symbol
8814 so that PLT32 relocation can be used as a marker for 32-bit
8815 PC-relative branches. It is useful for linker relaxation. */
5b7c81bd 8816 return true;
bd7ab16b
L
8817}
8818#endif
8819
29b0f896 8820static void
e3bb37b5 8821output_jump (void)
29b0f896
AM
8822{
8823 char *p;
8824 int size;
3e02c1cc 8825 fixS *fixP;
bd7ab16b 8826 bfd_reloc_code_real_type jump_reloc = i.reloc[0];
29b0f896 8827
0cfa3eb3 8828 if (i.tm.opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
8829 {
8830 /* This is a loop or jecxz type instruction. */
8831 size = 1;
8832 if (i.prefix[ADDR_PREFIX] != 0)
8833 {
48ef937e 8834 frag_opcode_byte (ADDR_PREFIX_OPCODE);
29b0f896
AM
8835 i.prefixes -= 1;
8836 }
8837 /* Pentium4 branch hints. */
8838 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8839 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
8840 {
48ef937e 8841 frag_opcode_byte (i.prefix[SEG_PREFIX]);
29b0f896 8842 i.prefixes--;
3e73aa7c
JH
8843 }
8844 }
29b0f896
AM
8845 else
8846 {
8847 int code16;
3e73aa7c 8848
29b0f896
AM
8849 code16 = 0;
8850 if (flag_code == CODE_16BIT)
8851 code16 = CODE16;
3e73aa7c 8852
29b0f896
AM
8853 if (i.prefix[DATA_PREFIX] != 0)
8854 {
48ef937e 8855 frag_opcode_byte (DATA_PREFIX_OPCODE);
29b0f896 8856 i.prefixes -= 1;
376cd056 8857 code16 ^= flip_code16(code16);
29b0f896 8858 }
252b5132 8859
29b0f896
AM
8860 size = 4;
8861 if (code16)
8862 size = 2;
8863 }
9fcc94b6 8864
6cb0a70e
JB
8865 /* BND prefixed jump. */
8866 if (i.prefix[BND_PREFIX] != 0)
29b0f896 8867 {
48ef937e 8868 frag_opcode_byte (i.prefix[BND_PREFIX]);
29b0f896
AM
8869 i.prefixes -= 1;
8870 }
252b5132 8871
6cb0a70e 8872 if (i.prefix[REX_PREFIX] != 0)
7e8b059b 8873 {
48ef937e 8874 frag_opcode_byte (i.prefix[REX_PREFIX]);
7e8b059b
L
8875 i.prefixes -= 1;
8876 }
8877
f2810fe0 8878 if (i.prefixes != 0)
76d3f746 8879 as_warn (_("skipping prefixes on `%s'"), insn_name (&i.tm));
e0890092 8880
48ef937e
JB
8881 if (now_seg == absolute_section)
8882 {
9a182d04 8883 abs_section_offset += i.opcode_length + size;
48ef937e
JB
8884 return;
8885 }
8886
9a182d04
JB
8887 p = frag_more (i.opcode_length + size);
8888 switch (i.opcode_length)
42164a71
L
8889 {
8890 case 2:
8891 *p++ = i.tm.base_opcode >> 8;
1a0670f3 8892 /* Fall through. */
42164a71
L
8893 case 1:
8894 *p++ = i.tm.base_opcode;
8895 break;
8896 default:
8897 abort ();
8898 }
e0890092 8899
bd7ab16b 8900#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1ef3994a
JB
8901 if (flag_code == CODE_64BIT && size == 4
8902 && jump_reloc == NO_RELOC && i.op[0].disps->X_add_number == 0
bd7ab16b
L
8903 && need_plt32_p (i.op[0].disps->X_add_symbol))
8904 jump_reloc = BFD_RELOC_X86_64_PLT32;
8905#endif
8906
8907 jump_reloc = reloc (size, 1, 1, jump_reloc);
8908
3e02c1cc 8909 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
bd7ab16b 8910 i.op[0].disps, 1, jump_reloc);
3e02c1cc 8911
eb19308f
JB
8912 /* All jumps handled here are signed, but don't unconditionally use a
8913 signed limit check for 32 and 16 bit jumps as we want to allow wrap
8914 around at 4G (outside of 64-bit mode) and 64k (except for XBEGIN)
8915 respectively. */
8916 switch (size)
8917 {
8918 case 1:
8919 fixP->fx_signed = 1;
8920 break;
8921
8922 case 2:
7fc69528 8923 if (i.tm.mnem_off == MN_xbegin)
eb19308f
JB
8924 fixP->fx_signed = 1;
8925 break;
8926
8927 case 4:
8928 if (flag_code == CODE_64BIT)
8929 fixP->fx_signed = 1;
8930 break;
8931 }
29b0f896 8932}
e0890092 8933
29b0f896 8934static void
e3bb37b5 8935output_interseg_jump (void)
29b0f896
AM
8936{
8937 char *p;
8938 int size;
8939 int prefix;
8940 int code16;
252b5132 8941
29b0f896
AM
8942 code16 = 0;
8943 if (flag_code == CODE_16BIT)
8944 code16 = CODE16;
a217f122 8945
29b0f896
AM
8946 prefix = 0;
8947 if (i.prefix[DATA_PREFIX] != 0)
8948 {
8949 prefix = 1;
8950 i.prefixes -= 1;
8951 code16 ^= CODE16;
8952 }
6cb0a70e
JB
8953
8954 gas_assert (!i.prefix[REX_PREFIX]);
252b5132 8955
29b0f896
AM
8956 size = 4;
8957 if (code16)
8958 size = 2;
252b5132 8959
f2810fe0 8960 if (i.prefixes != 0)
76d3f746 8961 as_warn (_("skipping prefixes on `%s'"), insn_name (&i.tm));
252b5132 8962
48ef937e
JB
8963 if (now_seg == absolute_section)
8964 {
8965 abs_section_offset += prefix + 1 + 2 + size;
8966 return;
8967 }
8968
29b0f896
AM
8969 /* 1 opcode; 2 segment; offset */
8970 p = frag_more (prefix + 1 + 2 + size);
3e73aa7c 8971
29b0f896
AM
8972 if (i.prefix[DATA_PREFIX] != 0)
8973 *p++ = DATA_PREFIX_OPCODE;
252b5132 8974
29b0f896
AM
8975 if (i.prefix[REX_PREFIX] != 0)
8976 *p++ = i.prefix[REX_PREFIX];
252b5132 8977
29b0f896
AM
8978 *p++ = i.tm.base_opcode;
8979 if (i.op[1].imms->X_op == O_constant)
8980 {
8981 offsetT n = i.op[1].imms->X_add_number;
252b5132 8982
29b0f896
AM
8983 if (size == 2
8984 && !fits_in_unsigned_word (n)
8985 && !fits_in_signed_word (n))
8986 {
8987 as_bad (_("16-bit jump out of range"));
8988 return;
8989 }
8990 md_number_to_chars (p, n, size);
8991 }
8992 else
8993 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
d258b828 8994 i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
6d96a594
C
8995
8996 p += size;
8997 if (i.op[0].imms->X_op == O_constant)
8998 md_number_to_chars (p, (valueT) i.op[0].imms->X_add_number, 2);
8999 else
9000 fix_new_exp (frag_now, p - frag_now->fr_literal, 2,
9001 i.op[0].imms, 0, reloc (2, 0, 0, i.reloc[0]));
29b0f896 9002}
a217f122 9003
b4a3a7b4
L
9004#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9005void
9006x86_cleanup (void)
9007{
9008 char *p;
9009 asection *seg = now_seg;
9010 subsegT subseg = now_subseg;
9011 asection *sec;
9012 unsigned int alignment, align_size_1;
9013 unsigned int isa_1_descsz, feature_2_descsz, descsz;
9014 unsigned int isa_1_descsz_raw, feature_2_descsz_raw;
9015 unsigned int padding;
9016
1273b2f8 9017 if (!IS_ELF || !x86_used_note)
b4a3a7b4
L
9018 return;
9019
b4a3a7b4
L
9020 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X86;
9021
9022 /* The .note.gnu.property section layout:
9023
9024 Field Length Contents
9025 ---- ---- ----
9026 n_namsz 4 4
9027 n_descsz 4 The note descriptor size
9028 n_type 4 NT_GNU_PROPERTY_TYPE_0
9029 n_name 4 "GNU"
9030 n_desc n_descsz The program property array
9031 .... .... ....
9032 */
9033
9034 /* Create the .note.gnu.property section. */
9035 sec = subseg_new (NOTE_GNU_PROPERTY_SECTION_NAME, 0);
fd361982 9036 bfd_set_section_flags (sec,
b4a3a7b4
L
9037 (SEC_ALLOC
9038 | SEC_LOAD
9039 | SEC_DATA
9040 | SEC_HAS_CONTENTS
9041 | SEC_READONLY));
9042
9043 if (get_elf_backend_data (stdoutput)->s->elfclass == ELFCLASS64)
9044 {
9045 align_size_1 = 7;
9046 alignment = 3;
9047 }
9048 else
9049 {
9050 align_size_1 = 3;
9051 alignment = 2;
9052 }
9053
fd361982 9054 bfd_set_section_alignment (sec, alignment);
b4a3a7b4
L
9055 elf_section_type (sec) = SHT_NOTE;
9056
1273b2f8
L
9057 /* GNU_PROPERTY_X86_ISA_1_USED: 4-byte type + 4-byte data size
9058 + 4-byte data */
9059 isa_1_descsz_raw = 4 + 4 + 4;
9060 /* Align GNU_PROPERTY_X86_ISA_1_USED. */
9061 isa_1_descsz = (isa_1_descsz_raw + align_size_1) & ~align_size_1;
b4a3a7b4
L
9062
9063 feature_2_descsz_raw = isa_1_descsz;
9064 /* GNU_PROPERTY_X86_FEATURE_2_USED: 4-byte type + 4-byte data size
1273b2f8 9065 + 4-byte data */
b4a3a7b4
L
9066 feature_2_descsz_raw += 4 + 4 + 4;
9067 /* Align GNU_PROPERTY_X86_FEATURE_2_USED. */
9068 feature_2_descsz = ((feature_2_descsz_raw + align_size_1)
9069 & ~align_size_1);
9070
9071 descsz = feature_2_descsz;
9072 /* Section size: n_namsz + n_descsz + n_type + n_name + n_descsz. */
9073 p = frag_more (4 + 4 + 4 + 4 + descsz);
9074
9075 /* Write n_namsz. */
9076 md_number_to_chars (p, (valueT) 4, 4);
9077
9078 /* Write n_descsz. */
9079 md_number_to_chars (p + 4, (valueT) descsz, 4);
9080
9081 /* Write n_type. */
9082 md_number_to_chars (p + 4 * 2, (valueT) NT_GNU_PROPERTY_TYPE_0, 4);
9083
9084 /* Write n_name. */
9085 memcpy (p + 4 * 3, "GNU", 4);
9086
1273b2f8
L
9087 /* Write 4-byte type. */
9088 md_number_to_chars (p + 4 * 4,
9089 (valueT) GNU_PROPERTY_X86_ISA_1_USED, 4);
b4a3a7b4 9090
1273b2f8
L
9091 /* Write 4-byte data size. */
9092 md_number_to_chars (p + 4 * 5, (valueT) 4, 4);
b4a3a7b4 9093
1273b2f8
L
9094 /* Write 4-byte data. */
9095 md_number_to_chars (p + 4 * 6, (valueT) x86_isa_1_used, 4);
b4a3a7b4 9096
1273b2f8
L
9097 /* Zero out paddings. */
9098 padding = isa_1_descsz - isa_1_descsz_raw;
9099 if (padding)
9100 memset (p + 4 * 7, 0, padding);
b4a3a7b4
L
9101
9102 /* Write 4-byte type. */
9103 md_number_to_chars (p + isa_1_descsz + 4 * 4,
9104 (valueT) GNU_PROPERTY_X86_FEATURE_2_USED, 4);
9105
9106 /* Write 4-byte data size. */
9107 md_number_to_chars (p + isa_1_descsz + 4 * 5, (valueT) 4, 4);
9108
9109 /* Write 4-byte data. */
9110 md_number_to_chars (p + isa_1_descsz + 4 * 6,
9111 (valueT) x86_feature_2_used, 4);
9112
9113 /* Zero out paddings. */
9114 padding = feature_2_descsz - feature_2_descsz_raw;
9115 if (padding)
9116 memset (p + isa_1_descsz + 4 * 7, 0, padding);
9117
9118 /* We probably can't restore the current segment, for there likely
9119 isn't one yet... */
9120 if (seg && subseg)
9121 subseg_set (seg, subseg);
9122}
b52c4ee4
IB
9123
9124bool
9125x86_support_sframe_p (void)
9126{
3e3e792a 9127 /* At this time, SFrame stack trace is supported for AMD64 ABI only. */
b52c4ee4
IB
9128 return (x86_elf_abi == X86_64_ABI);
9129}
9130
9131bool
9132x86_sframe_ra_tracking_p (void)
9133{
9134 /* In AMD64, return address is always stored on the stack at a fixed offset
9135 from the CFA (provided via x86_sframe_cfa_ra_offset ()).
9136 Do not track explicitly via an SFrame Frame Row Entry. */
9137 return false;
9138}
9139
9140offsetT
9141x86_sframe_cfa_ra_offset (void)
9142{
9143 gas_assert (x86_elf_abi == X86_64_ABI);
9144 return (offsetT) -8;
9145}
9146
9147unsigned char
9148x86_sframe_get_abi_arch (void)
9149{
9150 unsigned char sframe_abi_arch = 0;
9151
9152 if (x86_support_sframe_p ())
9153 {
9154 gas_assert (!target_big_endian);
9155 sframe_abi_arch = SFRAME_ABI_AMD64_ENDIAN_LITTLE;
9156 }
9157
9158 return sframe_abi_arch;
9159}
9160
b4a3a7b4
L
9161#endif
9162
9c33702b
JB
9163static unsigned int
9164encoding_length (const fragS *start_frag, offsetT start_off,
9165 const char *frag_now_ptr)
9166{
9167 unsigned int len = 0;
9168
9169 if (start_frag != frag_now)
9170 {
9171 const fragS *fr = start_frag;
9172
9173 do {
9174 len += fr->fr_fix;
9175 fr = fr->fr_next;
9176 } while (fr && fr != frag_now);
9177 }
9178
9179 return len - start_off + (frag_now_ptr - frag_now->fr_literal);
9180}
9181
e379e5f3 9182/* Return 1 for test, and, cmp, add, sub, inc and dec which may
79d72f45
HL
9183 be macro-fused with conditional jumps.
9184 NB: If TEST/AND/CMP/ADD/SUB/INC/DEC is of RIP relative address,
9185 or is one of the following format:
9186
9187 cmp m, imm
9188 add m, imm
9189 sub m, imm
9190 test m, imm
9191 and m, imm
9192 inc m
9193 dec m
9194
9195 it is unfusible. */
e379e5f3
L
9196
9197static int
79d72f45 9198maybe_fused_with_jcc_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
9199{
9200 /* No RIP address. */
9201 if (i.base_reg && i.base_reg->reg_num == RegIP)
9202 return 0;
9203
389d00a5 9204 /* No opcodes outside of base encoding space. */
ddb62495 9205 if (i.tm.opcode_space != SPACE_BASE)
e379e5f3
L
9206 return 0;
9207
79d72f45
HL
9208 /* add, sub without add/sub m, imm. */
9209 if (i.tm.base_opcode <= 5
e379e5f3
L
9210 || (i.tm.base_opcode >= 0x28 && i.tm.base_opcode <= 0x2d)
9211 || ((i.tm.base_opcode | 3) == 0x83
79d72f45 9212 && (i.tm.extension_opcode == 0x5
e379e5f3 9213 || i.tm.extension_opcode == 0x0)))
79d72f45
HL
9214 {
9215 *mf_cmp_p = mf_cmp_alu_cmp;
9216 return !(i.mem_operands && i.imm_operands);
9217 }
e379e5f3 9218
79d72f45
HL
9219 /* and without and m, imm. */
9220 if ((i.tm.base_opcode >= 0x20 && i.tm.base_opcode <= 0x25)
9221 || ((i.tm.base_opcode | 3) == 0x83
9222 && i.tm.extension_opcode == 0x4))
9223 {
9224 *mf_cmp_p = mf_cmp_test_and;
9225 return !(i.mem_operands && i.imm_operands);
9226 }
9227
9228 /* test without test m imm. */
e379e5f3
L
9229 if ((i.tm.base_opcode | 1) == 0x85
9230 || (i.tm.base_opcode | 1) == 0xa9
9231 || ((i.tm.base_opcode | 1) == 0xf7
79d72f45
HL
9232 && i.tm.extension_opcode == 0))
9233 {
9234 *mf_cmp_p = mf_cmp_test_and;
9235 return !(i.mem_operands && i.imm_operands);
9236 }
9237
9238 /* cmp without cmp m, imm. */
9239 if ((i.tm.base_opcode >= 0x38 && i.tm.base_opcode <= 0x3d)
e379e5f3
L
9240 || ((i.tm.base_opcode | 3) == 0x83
9241 && (i.tm.extension_opcode == 0x7)))
79d72f45
HL
9242 {
9243 *mf_cmp_p = mf_cmp_alu_cmp;
9244 return !(i.mem_operands && i.imm_operands);
9245 }
e379e5f3 9246
79d72f45 9247 /* inc, dec without inc/dec m. */
e379e5f3
L
9248 if ((i.tm.cpu_flags.bitfield.cpuno64
9249 && (i.tm.base_opcode | 0xf) == 0x4f)
9250 || ((i.tm.base_opcode | 1) == 0xff
9251 && i.tm.extension_opcode <= 0x1))
79d72f45
HL
9252 {
9253 *mf_cmp_p = mf_cmp_incdec;
9254 return !i.mem_operands;
9255 }
e379e5f3
L
9256
9257 return 0;
9258}
9259
9260/* Return 1 if a FUSED_JCC_PADDING frag should be generated. */
9261
9262static int
79d72f45 9263add_fused_jcc_padding_frag_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
9264{
9265 /* NB: Don't work with COND_JUMP86 without i386. */
9266 if (!align_branch_power
9267 || now_seg == absolute_section
9268 || !cpu_arch_flags.bitfield.cpui386
9269 || !(align_branch & align_branch_fused_bit))
9270 return 0;
9271
79d72f45 9272 if (maybe_fused_with_jcc_p (mf_cmp_p))
e379e5f3
L
9273 {
9274 if (last_insn.kind == last_insn_other
9275 || last_insn.seg != now_seg)
9276 return 1;
9277 if (flag_debug)
9278 as_warn_where (last_insn.file, last_insn.line,
9279 _("`%s` skips -malign-branch-boundary on `%s`"),
76d3f746 9280 last_insn.name, insn_name (&i.tm));
e379e5f3
L
9281 }
9282
9283 return 0;
9284}
9285
9286/* Return 1 if a BRANCH_PREFIX frag should be generated. */
9287
9288static int
9289add_branch_prefix_frag_p (void)
9290{
9291 /* NB: Don't work with COND_JUMP86 without i386. Don't add prefix
9292 to PadLock instructions since they include prefixes in opcode. */
9293 if (!align_branch_power
9294 || !align_branch_prefix_size
9295 || now_seg == absolute_section
9296 || i.tm.cpu_flags.bitfield.cpupadlock
9297 || !cpu_arch_flags.bitfield.cpui386)
9298 return 0;
9299
9300 /* Don't add prefix if it is a prefix or there is no operand in case
9301 that segment prefix is special. */
9302 if (!i.operands || i.tm.opcode_modifier.isprefix)
9303 return 0;
9304
9305 if (last_insn.kind == last_insn_other
9306 || last_insn.seg != now_seg)
9307 return 1;
9308
9309 if (flag_debug)
9310 as_warn_where (last_insn.file, last_insn.line,
9311 _("`%s` skips -malign-branch-boundary on `%s`"),
76d3f746 9312 last_insn.name, insn_name (&i.tm));
e379e5f3
L
9313
9314 return 0;
9315}
9316
9317/* Return 1 if a BRANCH_PADDING frag should be generated. */
9318
9319static int
79d72f45
HL
9320add_branch_padding_frag_p (enum align_branch_kind *branch_p,
9321 enum mf_jcc_kind *mf_jcc_p)
e379e5f3
L
9322{
9323 int add_padding;
9324
9325 /* NB: Don't work with COND_JUMP86 without i386. */
9326 if (!align_branch_power
9327 || now_seg == absolute_section
389d00a5 9328 || !cpu_arch_flags.bitfield.cpui386
ddb62495 9329 || i.tm.opcode_space != SPACE_BASE)
e379e5f3
L
9330 return 0;
9331
9332 add_padding = 0;
9333
9334 /* Check for jcc and direct jmp. */
9335 if (i.tm.opcode_modifier.jump == JUMP)
9336 {
9337 if (i.tm.base_opcode == JUMP_PC_RELATIVE)
9338 {
9339 *branch_p = align_branch_jmp;
9340 add_padding = align_branch & align_branch_jmp_bit;
9341 }
9342 else
9343 {
79d72f45
HL
9344 /* Because J<cc> and JN<cc> share same group in macro-fusible table,
9345 igore the lowest bit. */
9346 *mf_jcc_p = (i.tm.base_opcode & 0x0e) >> 1;
e379e5f3
L
9347 *branch_p = align_branch_jcc;
9348 if ((align_branch & align_branch_jcc_bit))
9349 add_padding = 1;
9350 }
9351 }
e379e5f3
L
9352 else if ((i.tm.base_opcode | 1) == 0xc3)
9353 {
9354 /* Near ret. */
9355 *branch_p = align_branch_ret;
9356 if ((align_branch & align_branch_ret_bit))
9357 add_padding = 1;
9358 }
9359 else
9360 {
9361 /* Check for indirect jmp, direct and indirect calls. */
9362 if (i.tm.base_opcode == 0xe8)
9363 {
9364 /* Direct call. */
9365 *branch_p = align_branch_call;
9366 if ((align_branch & align_branch_call_bit))
9367 add_padding = 1;
9368 }
9369 else if (i.tm.base_opcode == 0xff
9370 && (i.tm.extension_opcode == 2
9371 || i.tm.extension_opcode == 4))
9372 {
9373 /* Indirect call and jmp. */
9374 *branch_p = align_branch_indirect;
9375 if ((align_branch & align_branch_indirect_bit))
9376 add_padding = 1;
9377 }
9378
9379 if (add_padding
9380 && i.disp_operands
9381 && tls_get_addr
9382 && (i.op[0].disps->X_op == O_symbol
9383 || (i.op[0].disps->X_op == O_subtract
9384 && i.op[0].disps->X_op_symbol == GOT_symbol)))
9385 {
9386 symbolS *s = i.op[0].disps->X_add_symbol;
9387 /* No padding to call to global or undefined tls_get_addr. */
9388 if ((S_IS_EXTERNAL (s) || !S_IS_DEFINED (s))
9389 && strcmp (S_GET_NAME (s), tls_get_addr) == 0)
9390 return 0;
9391 }
9392 }
9393
9394 if (add_padding
9395 && last_insn.kind != last_insn_other
9396 && last_insn.seg == now_seg)
9397 {
9398 if (flag_debug)
9399 as_warn_where (last_insn.file, last_insn.line,
9400 _("`%s` skips -malign-branch-boundary on `%s`"),
76d3f746 9401 last_insn.name, insn_name (&i.tm));
e379e5f3
L
9402 return 0;
9403 }
9404
9405 return add_padding;
9406}
9407
29b0f896 9408static void
e3bb37b5 9409output_insn (void)
29b0f896 9410{
2bbd9c25
JJ
9411 fragS *insn_start_frag;
9412 offsetT insn_start_off;
e379e5f3
L
9413 fragS *fragP = NULL;
9414 enum align_branch_kind branch = align_branch_none;
79d72f45
HL
9415 /* The initializer is arbitrary just to avoid uninitialized error.
9416 it's actually either assigned in add_branch_padding_frag_p
9417 or never be used. */
9418 enum mf_jcc_kind mf_jcc = mf_jcc_jo;
2bbd9c25 9419
b4a3a7b4 9420#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
48ef937e 9421 if (IS_ELF && x86_used_note && now_seg != absolute_section)
b4a3a7b4 9422 {
32930e4e
L
9423 if ((i.xstate & xstate_tmm) == xstate_tmm
9424 || i.tm.cpu_flags.bitfield.cpuamx_tile)
9425 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_TMM;
9426
b4a3a7b4
L
9427 if (i.tm.cpu_flags.bitfield.cpu8087
9428 || i.tm.cpu_flags.bitfield.cpu287
9429 || i.tm.cpu_flags.bitfield.cpu387
9430 || i.tm.cpu_flags.bitfield.cpu687
9431 || i.tm.cpu_flags.bitfield.cpufisttp)
9432 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X87;
014d61ea 9433
921eafea 9434 if ((i.xstate & xstate_mmx)
7fc69528
JB
9435 || i.tm.mnem_off == MN_emms
9436 || i.tm.mnem_off == MN_femms)
b4a3a7b4 9437 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MMX;
014d61ea 9438
32930e4e
L
9439 if (i.index_reg)
9440 {
9441 if (i.index_reg->reg_type.bitfield.zmmword)
9442 i.xstate |= xstate_zmm;
9443 else if (i.index_reg->reg_type.bitfield.ymmword)
9444 i.xstate |= xstate_ymm;
9445 else if (i.index_reg->reg_type.bitfield.xmmword)
9446 i.xstate |= xstate_xmm;
9447 }
014d61ea
JB
9448
9449 /* vzeroall / vzeroupper */
9450 if (i.tm.base_opcode == 0x77 && i.tm.cpu_flags.bitfield.cpuavx)
9451 i.xstate |= xstate_ymm;
9452
c4694f17 9453 if ((i.xstate & xstate_xmm)
389d00a5
JB
9454 /* ldmxcsr / stmxcsr / vldmxcsr / vstmxcsr */
9455 || (i.tm.base_opcode == 0xae
9456 && (i.tm.cpu_flags.bitfield.cpusse
9457 || i.tm.cpu_flags.bitfield.cpuavx))
c4694f17
TG
9458 || i.tm.cpu_flags.bitfield.cpuwidekl
9459 || i.tm.cpu_flags.bitfield.cpukl)
b4a3a7b4 9460 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XMM;
014d61ea 9461
921eafea 9462 if ((i.xstate & xstate_ymm) == xstate_ymm)
b4a3a7b4 9463 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_YMM;
921eafea 9464 if ((i.xstate & xstate_zmm) == xstate_zmm)
b4a3a7b4 9465 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_ZMM;
6225c532 9466 if (i.mask.reg || (i.xstate & xstate_mask) == xstate_mask)
32930e4e 9467 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MASK;
b4a3a7b4
L
9468 if (i.tm.cpu_flags.bitfield.cpufxsr)
9469 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_FXSR;
9470 if (i.tm.cpu_flags.bitfield.cpuxsave)
9471 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVE;
9472 if (i.tm.cpu_flags.bitfield.cpuxsaveopt)
9473 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT;
9474 if (i.tm.cpu_flags.bitfield.cpuxsavec)
9475 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEC;
b0ab0693
L
9476
9477 if (x86_feature_2_used
9478 || i.tm.cpu_flags.bitfield.cpucmov
9479 || i.tm.cpu_flags.bitfield.cpusyscall
7fc69528 9480 || i.tm.mnem_off == MN_cmpxchg8b)
b0ab0693
L
9481 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_BASELINE;
9482 if (i.tm.cpu_flags.bitfield.cpusse3
9483 || i.tm.cpu_flags.bitfield.cpussse3
9484 || i.tm.cpu_flags.bitfield.cpusse4_1
9485 || i.tm.cpu_flags.bitfield.cpusse4_2
9486 || i.tm.cpu_flags.bitfield.cpucx16
9487 || i.tm.cpu_flags.bitfield.cpupopcnt
9488 /* LAHF-SAHF insns in 64-bit mode. */
9489 || (flag_code == CODE_64BIT
35648716 9490 && (i.tm.base_opcode | 1) == 0x9f
ddb62495 9491 && i.tm.opcode_space == SPACE_BASE))
b0ab0693
L
9492 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V2;
9493 if (i.tm.cpu_flags.bitfield.cpuavx
9494 || i.tm.cpu_flags.bitfield.cpuavx2
a9860005
JB
9495 /* Any VEX encoded insns execpt for AVX512F, AVX512BW, AVX512DQ,
9496 XOP, FMA4, LPW, TBM, and AMX. */
b0ab0693
L
9497 || (i.tm.opcode_modifier.vex
9498 && !i.tm.cpu_flags.bitfield.cpuavx512f
9499 && !i.tm.cpu_flags.bitfield.cpuavx512bw
9500 && !i.tm.cpu_flags.bitfield.cpuavx512dq
a9860005
JB
9501 && !i.tm.cpu_flags.bitfield.cpuxop
9502 && !i.tm.cpu_flags.bitfield.cpufma4
b0ab0693
L
9503 && !i.tm.cpu_flags.bitfield.cpulwp
9504 && !i.tm.cpu_flags.bitfield.cputbm
9505 && !(x86_feature_2_used & GNU_PROPERTY_X86_FEATURE_2_TMM))
9506 || i.tm.cpu_flags.bitfield.cpuf16c
9507 || i.tm.cpu_flags.bitfield.cpufma
9508 || i.tm.cpu_flags.bitfield.cpulzcnt
9509 || i.tm.cpu_flags.bitfield.cpumovbe
9510 || i.tm.cpu_flags.bitfield.cpuxsaves
9511 || (x86_feature_2_used
9512 & (GNU_PROPERTY_X86_FEATURE_2_XSAVE
9513 | GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT
9514 | GNU_PROPERTY_X86_FEATURE_2_XSAVEC)) != 0)
9515 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V3;
9516 if (i.tm.cpu_flags.bitfield.cpuavx512f
9517 || i.tm.cpu_flags.bitfield.cpuavx512bw
9518 || i.tm.cpu_flags.bitfield.cpuavx512dq
9519 || i.tm.cpu_flags.bitfield.cpuavx512vl
a9860005
JB
9520 /* Any EVEX encoded insns except for AVX512ER, AVX512PF,
9521 AVX512-4FMAPS, and AVX512-4VNNIW. */
b0ab0693
L
9522 || (i.tm.opcode_modifier.evex
9523 && !i.tm.cpu_flags.bitfield.cpuavx512er
9524 && !i.tm.cpu_flags.bitfield.cpuavx512pf
a9860005 9525 && !i.tm.cpu_flags.bitfield.cpuavx512_4fmaps
b0ab0693
L
9526 && !i.tm.cpu_flags.bitfield.cpuavx512_4vnniw))
9527 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V4;
b4a3a7b4
L
9528 }
9529#endif
9530
29b0f896
AM
9531 /* Tie dwarf2 debug info to the address at the start of the insn.
9532 We can't do this after the insn has been output as the current
9533 frag may have been closed off. eg. by frag_var. */
9534 dwarf2_emit_insn (0);
9535
2bbd9c25
JJ
9536 insn_start_frag = frag_now;
9537 insn_start_off = frag_now_fix ();
9538
79d72f45 9539 if (add_branch_padding_frag_p (&branch, &mf_jcc))
e379e5f3
L
9540 {
9541 char *p;
9542 /* Branch can be 8 bytes. Leave some room for prefixes. */
9543 unsigned int max_branch_padding_size = 14;
9544
9545 /* Align section to boundary. */
9546 record_alignment (now_seg, align_branch_power);
9547
9548 /* Make room for padding. */
9549 frag_grow (max_branch_padding_size);
9550
9551 /* Start of the padding. */
9552 p = frag_more (0);
9553
9554 fragP = frag_now;
9555
9556 frag_var (rs_machine_dependent, max_branch_padding_size, 0,
9557 ENCODE_RELAX_STATE (BRANCH_PADDING, 0),
9558 NULL, 0, p);
9559
79d72f45 9560 fragP->tc_frag_data.mf_type = mf_jcc;
e379e5f3
L
9561 fragP->tc_frag_data.branch_type = branch;
9562 fragP->tc_frag_data.max_bytes = max_branch_padding_size;
9563 }
9564
d59a54c2
JB
9565 if (!cpu_arch_flags.bitfield.cpui386 && (flag_code != CODE_16BIT)
9566 && !pre_386_16bit_warned)
9567 {
9568 as_warn (_("use .code16 to ensure correct addressing mode"));
9569 pre_386_16bit_warned = true;
9570 }
9571
29b0f896 9572 /* Output jumps. */
0cfa3eb3 9573 if (i.tm.opcode_modifier.jump == JUMP)
29b0f896 9574 output_branch ();
0cfa3eb3
JB
9575 else if (i.tm.opcode_modifier.jump == JUMP_BYTE
9576 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896 9577 output_jump ();
0cfa3eb3 9578 else if (i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT)
29b0f896
AM
9579 output_interseg_jump ();
9580 else
9581 {
9582 /* Output normal instructions here. */
9583 char *p;
9584 unsigned char *q;
47465058 9585 unsigned int j;
79d72f45 9586 enum mf_cmp_kind mf_cmp;
4dffcebc 9587
e4e00185 9588 if (avoid_fence
389d00a5
JB
9589 && (i.tm.base_opcode == 0xaee8
9590 || i.tm.base_opcode == 0xaef0
9591 || i.tm.base_opcode == 0xaef8))
48ef937e
JB
9592 {
9593 /* Encode lfence, mfence, and sfence as
9594 f0 83 04 24 00 lock addl $0x0, (%{re}sp). */
47f4115a 9595 if (flag_code == CODE_16BIT)
76d3f746 9596 as_bad (_("Cannot convert `%s' in 16-bit mode"), insn_name (&i.tm));
47f4115a
JB
9597 else if (omit_lock_prefix)
9598 as_bad (_("Cannot convert `%s' with `-momit-lock-prefix=yes' in effect"),
76d3f746 9599 insn_name (&i.tm));
47f4115a 9600 else if (now_seg != absolute_section)
48ef937e
JB
9601 {
9602 offsetT val = 0x240483f0ULL;
9603
9604 p = frag_more (5);
9605 md_number_to_chars (p, val, 5);
9606 }
9607 else
9608 abs_section_offset += 5;
9609 return;
9610 }
e4e00185 9611
d022bddd
IT
9612 /* Some processors fail on LOCK prefix. This options makes
9613 assembler ignore LOCK prefix and serves as a workaround. */
9614 if (omit_lock_prefix)
9615 {
35648716
JB
9616 if (i.tm.base_opcode == LOCK_PREFIX_OPCODE
9617 && i.tm.opcode_modifier.isprefix)
d022bddd
IT
9618 return;
9619 i.prefix[LOCK_PREFIX] = 0;
9620 }
9621
e379e5f3
L
9622 if (branch)
9623 /* Skip if this is a branch. */
9624 ;
79d72f45 9625 else if (add_fused_jcc_padding_frag_p (&mf_cmp))
e379e5f3
L
9626 {
9627 /* Make room for padding. */
9628 frag_grow (MAX_FUSED_JCC_PADDING_SIZE);
9629 p = frag_more (0);
9630
9631 fragP = frag_now;
9632
9633 frag_var (rs_machine_dependent, MAX_FUSED_JCC_PADDING_SIZE, 0,
9634 ENCODE_RELAX_STATE (FUSED_JCC_PADDING, 0),
9635 NULL, 0, p);
9636
79d72f45 9637 fragP->tc_frag_data.mf_type = mf_cmp;
e379e5f3
L
9638 fragP->tc_frag_data.branch_type = align_branch_fused;
9639 fragP->tc_frag_data.max_bytes = MAX_FUSED_JCC_PADDING_SIZE;
9640 }
9641 else if (add_branch_prefix_frag_p ())
9642 {
9643 unsigned int max_prefix_size = align_branch_prefix_size;
9644
9645 /* Make room for padding. */
9646 frag_grow (max_prefix_size);
9647 p = frag_more (0);
9648
9649 fragP = frag_now;
9650
9651 frag_var (rs_machine_dependent, max_prefix_size, 0,
9652 ENCODE_RELAX_STATE (BRANCH_PREFIX, 0),
9653 NULL, 0, p);
9654
9655 fragP->tc_frag_data.max_bytes = max_prefix_size;
9656 }
9657
43234a1e
L
9658 /* Since the VEX/EVEX prefix contains the implicit prefix, we
9659 don't need the explicit prefix. */
cf665fee 9660 if (!is_any_vex_encoding (&i.tm))
bc4bd9ab 9661 {
7b47a312 9662 switch (i.tm.opcode_modifier.opcodeprefix)
bc4bd9ab 9663 {
7b47a312
L
9664 case PREFIX_0X66:
9665 add_prefix (0x66);
9666 break;
9667 case PREFIX_0XF2:
9668 add_prefix (0xf2);
9669 break;
9670 case PREFIX_0XF3:
8b65b895
L
9671 if (!i.tm.cpu_flags.bitfield.cpupadlock
9672 || (i.prefix[REP_PREFIX] != 0xf3))
9673 add_prefix (0xf3);
c0f3af97 9674 break;
7b47a312 9675 case PREFIX_NONE:
9a182d04 9676 switch (i.opcode_length)
c0f3af97 9677 {
7b47a312 9678 case 2:
7b47a312 9679 break;
9a182d04 9680 case 1:
7b47a312 9681 /* Check for pseudo prefixes. */
9a182d04
JB
9682 if (!i.tm.opcode_modifier.isprefix || i.tm.base_opcode)
9683 break;
7b47a312
L
9684 as_bad_where (insn_start_frag->fr_file,
9685 insn_start_frag->fr_line,
9686 _("pseudo prefix without instruction"));
9687 return;
9688 default:
9689 abort ();
4dffcebc 9690 }
c0f3af97 9691 break;
c0f3af97
L
9692 default:
9693 abort ();
bc4bd9ab 9694 }
c0f3af97 9695
6d19a37a 9696#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
cf61b747
L
9697 /* For x32, add a dummy REX_OPCODE prefix for mov/add with
9698 R_X86_64_GOTTPOFF relocation so that linker can safely
14470f07
L
9699 perform IE->LE optimization. A dummy REX_OPCODE prefix
9700 is also needed for lea with R_X86_64_GOTPC32_TLSDESC
9701 relocation for GDesc -> IE/LE optimization. */
cf61b747
L
9702 if (x86_elf_abi == X86_64_X32_ABI
9703 && i.operands == 2
14470f07
L
9704 && (i.reloc[0] == BFD_RELOC_X86_64_GOTTPOFF
9705 || i.reloc[0] == BFD_RELOC_X86_64_GOTPC32_TLSDESC)
cf61b747
L
9706 && i.prefix[REX_PREFIX] == 0)
9707 add_prefix (REX_OPCODE);
6d19a37a 9708#endif
cf61b747 9709
c0f3af97
L
9710 /* The prefix bytes. */
9711 for (j = ARRAY_SIZE (i.prefix), q = i.prefix; j > 0; j--, q++)
9712 if (*q)
48ef937e 9713 frag_opcode_byte (*q);
0f10071e 9714 }
ae5c1c7b 9715 else
c0f3af97
L
9716 {
9717 for (j = 0, q = i.prefix; j < ARRAY_SIZE (i.prefix); j++, q++)
9718 if (*q)
9719 switch (j)
9720 {
c0f3af97
L
9721 case SEG_PREFIX:
9722 case ADDR_PREFIX:
48ef937e 9723 frag_opcode_byte (*q);
c0f3af97
L
9724 break;
9725 default:
9726 /* There should be no other prefixes for instructions
9727 with VEX prefix. */
9728 abort ();
9729 }
9730
43234a1e
L
9731 /* For EVEX instructions i.vrex should become 0 after
9732 build_evex_prefix. For VEX instructions upper 16 registers
9733 aren't available, so VREX should be 0. */
9734 if (i.vrex)
9735 abort ();
c0f3af97 9736 /* Now the VEX prefix. */
48ef937e
JB
9737 if (now_seg != absolute_section)
9738 {
9739 p = frag_more (i.vex.length);
9740 for (j = 0; j < i.vex.length; j++)
9741 p[j] = i.vex.bytes[j];
9742 }
9743 else
9744 abs_section_offset += i.vex.length;
c0f3af97 9745 }
252b5132 9746
29b0f896 9747 /* Now the opcode; be careful about word order here! */
389d00a5
JB
9748 j = i.opcode_length;
9749 if (!i.vex.length)
ddb62495 9750 switch (i.tm.opcode_space)
389d00a5
JB
9751 {
9752 case SPACE_BASE:
9753 break;
9754 case SPACE_0F:
9755 ++j;
9756 break;
9757 case SPACE_0F38:
9758 case SPACE_0F3A:
9759 j += 2;
9760 break;
9761 default:
9762 abort ();
9763 }
9764
48ef937e 9765 if (now_seg == absolute_section)
389d00a5
JB
9766 abs_section_offset += j;
9767 else if (j == 1)
29b0f896
AM
9768 {
9769 FRAG_APPEND_1_CHAR (i.tm.base_opcode);
9770 }
9771 else
9772 {
389d00a5
JB
9773 p = frag_more (j);
9774 if (!i.vex.length
ddb62495 9775 && i.tm.opcode_space != SPACE_BASE)
389d00a5
JB
9776 {
9777 *p++ = 0x0f;
ddb62495
JB
9778 if (i.tm.opcode_space != SPACE_0F)
9779 *p++ = i.tm.opcode_space == SPACE_0F38
389d00a5
JB
9780 ? 0x38 : 0x3a;
9781 }
9782
9a182d04 9783 switch (i.opcode_length)
331d2d0d 9784 {
4dffcebc 9785 case 2:
389d00a5
JB
9786 /* Put out high byte first: can't use md_number_to_chars! */
9787 *p++ = (i.tm.base_opcode >> 8) & 0xff;
9788 /* Fall through. */
9789 case 1:
9790 *p = i.tm.base_opcode & 0xff;
4dffcebc
L
9791 break;
9792 default:
9793 abort ();
9794 break;
331d2d0d 9795 }
0f10071e 9796
29b0f896 9797 }
3e73aa7c 9798
29b0f896 9799 /* Now the modrm byte and sib byte (if present). */
40fb9820 9800 if (i.tm.opcode_modifier.modrm)
29b0f896 9801 {
48ef937e
JB
9802 frag_opcode_byte ((i.rm.regmem << 0)
9803 | (i.rm.reg << 3)
9804 | (i.rm.mode << 6));
29b0f896
AM
9805 /* If i.rm.regmem == ESP (4)
9806 && i.rm.mode != (Register mode)
9807 && not 16 bit
9808 ==> need second modrm byte. */
9809 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
9810 && i.rm.mode != 3
dc821c5f 9811 && !(i.base_reg && i.base_reg->reg_type.bitfield.word))
48ef937e
JB
9812 frag_opcode_byte ((i.sib.base << 0)
9813 | (i.sib.index << 3)
9814 | (i.sib.scale << 6));
29b0f896 9815 }
3e73aa7c 9816
29b0f896 9817 if (i.disp_operands)
2bbd9c25 9818 output_disp (insn_start_frag, insn_start_off);
3e73aa7c 9819
29b0f896 9820 if (i.imm_operands)
2bbd9c25 9821 output_imm (insn_start_frag, insn_start_off);
9c33702b
JB
9822
9823 /*
9824 * frag_now_fix () returning plain abs_section_offset when we're in the
9825 * absolute section, and abs_section_offset not getting updated as data
9826 * gets added to the frag breaks the logic below.
9827 */
9828 if (now_seg != absolute_section)
9829 {
9830 j = encoding_length (insn_start_frag, insn_start_off, frag_more (0));
9831 if (j > 15)
9832 as_warn (_("instruction length of %u bytes exceeds the limit of 15"),
9833 j);
e379e5f3
L
9834 else if (fragP)
9835 {
9836 /* NB: Don't add prefix with GOTPC relocation since
9837 output_disp() above depends on the fixed encoding
9838 length. Can't add prefix with TLS relocation since
9839 it breaks TLS linker optimization. */
9840 unsigned int max = i.has_gotpc_tls_reloc ? 0 : 15 - j;
9841 /* Prefix count on the current instruction. */
9842 unsigned int count = i.vex.length;
9843 unsigned int k;
9844 for (k = 0; k < ARRAY_SIZE (i.prefix); k++)
9845 /* REX byte is encoded in VEX/EVEX prefix. */
9846 if (i.prefix[k] && (k != REX_PREFIX || !i.vex.length))
9847 count++;
9848
9849 /* Count prefixes for extended opcode maps. */
9850 if (!i.vex.length)
ddb62495 9851 switch (i.tm.opcode_space)
e379e5f3 9852 {
389d00a5 9853 case SPACE_BASE:
e379e5f3 9854 break;
389d00a5
JB
9855 case SPACE_0F:
9856 count++;
e379e5f3 9857 break;
389d00a5
JB
9858 case SPACE_0F38:
9859 case SPACE_0F3A:
9860 count += 2;
e379e5f3
L
9861 break;
9862 default:
9863 abort ();
9864 }
9865
9866 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
9867 == BRANCH_PREFIX)
9868 {
9869 /* Set the maximum prefix size in BRANCH_PREFIX
9870 frag. */
9871 if (fragP->tc_frag_data.max_bytes > max)
9872 fragP->tc_frag_data.max_bytes = max;
9873 if (fragP->tc_frag_data.max_bytes > count)
9874 fragP->tc_frag_data.max_bytes -= count;
9875 else
9876 fragP->tc_frag_data.max_bytes = 0;
9877 }
9878 else
9879 {
9880 /* Remember the maximum prefix size in FUSED_JCC_PADDING
9881 frag. */
9882 unsigned int max_prefix_size;
9883 if (align_branch_prefix_size > max)
9884 max_prefix_size = max;
9885 else
9886 max_prefix_size = align_branch_prefix_size;
9887 if (max_prefix_size > count)
9888 fragP->tc_frag_data.max_prefix_length
9889 = max_prefix_size - count;
9890 }
9891
9892 /* Use existing segment prefix if possible. Use CS
9893 segment prefix in 64-bit mode. In 32-bit mode, use SS
9894 segment prefix with ESP/EBP base register and use DS
9895 segment prefix without ESP/EBP base register. */
9896 if (i.prefix[SEG_PREFIX])
9897 fragP->tc_frag_data.default_prefix = i.prefix[SEG_PREFIX];
9898 else if (flag_code == CODE_64BIT)
9899 fragP->tc_frag_data.default_prefix = CS_PREFIX_OPCODE;
9900 else if (i.base_reg
9901 && (i.base_reg->reg_num == 4
9902 || i.base_reg->reg_num == 5))
9903 fragP->tc_frag_data.default_prefix = SS_PREFIX_OPCODE;
9904 else
9905 fragP->tc_frag_data.default_prefix = DS_PREFIX_OPCODE;
9906 }
9c33702b 9907 }
29b0f896 9908 }
252b5132 9909
e379e5f3
L
9910 /* NB: Don't work with COND_JUMP86 without i386. */
9911 if (align_branch_power
9912 && now_seg != absolute_section
9913 && cpu_arch_flags.bitfield.cpui386)
9914 {
9915 /* Terminate each frag so that we can add prefix and check for
9916 fused jcc. */
9917 frag_wane (frag_now);
9918 frag_new (0);
9919 }
9920
29b0f896
AM
9921#ifdef DEBUG386
9922 if (flag_debug)
9923 {
7b81dfbb 9924 pi ("" /*line*/, &i);
29b0f896
AM
9925 }
9926#endif /* DEBUG386 */
9927}
252b5132 9928
e205caa7
L
9929/* Return the size of the displacement operand N. */
9930
9931static int
9932disp_size (unsigned int n)
9933{
9934 int size = 4;
43234a1e 9935
b5014f7a 9936 if (i.types[n].bitfield.disp64)
40fb9820
L
9937 size = 8;
9938 else if (i.types[n].bitfield.disp8)
9939 size = 1;
9940 else if (i.types[n].bitfield.disp16)
9941 size = 2;
e205caa7
L
9942 return size;
9943}
9944
9945/* Return the size of the immediate operand N. */
9946
9947static int
9948imm_size (unsigned int n)
9949{
9950 int size = 4;
40fb9820
L
9951 if (i.types[n].bitfield.imm64)
9952 size = 8;
9953 else if (i.types[n].bitfield.imm8 || i.types[n].bitfield.imm8s)
9954 size = 1;
9955 else if (i.types[n].bitfield.imm16)
9956 size = 2;
e205caa7
L
9957 return size;
9958}
9959
29b0f896 9960static void
64e74474 9961output_disp (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
9962{
9963 char *p;
9964 unsigned int n;
252b5132 9965
29b0f896
AM
9966 for (n = 0; n < i.operands; n++)
9967 {
b5014f7a 9968 if (operand_type_check (i.types[n], disp))
29b0f896 9969 {
48ef937e
JB
9970 int size = disp_size (n);
9971
9972 if (now_seg == absolute_section)
9973 abs_section_offset += size;
9974 else if (i.op[n].disps->X_op == O_constant)
29b0f896 9975 {
43234a1e 9976 offsetT val = i.op[n].disps->X_add_number;
252b5132 9977
629cfaf1
JB
9978 val = offset_in_range (val >> (size == 1 ? i.memshift : 0),
9979 size);
29b0f896
AM
9980 p = frag_more (size);
9981 md_number_to_chars (p, val, size);
9982 }
9983 else
9984 {
f86103b7 9985 enum bfd_reloc_code_real reloc_type;
a775efc8
JB
9986 bool pcrel = (i.flags[n] & Operand_PCrel) != 0;
9987 bool sign = (flag_code == CODE_64BIT && size == 4
9988 && (!want_disp32 (&i.tm)
9989 || (i.tm.opcode_modifier.jump && !i.jumpabsolute
9990 && !i.types[n].bitfield.baseindex)))
9991 || pcrel;
02a86693 9992 fixS *fixP;
29b0f896 9993
e205caa7 9994 /* We can't have 8 bit displacement here. */
9c2799c2 9995 gas_assert (!i.types[n].bitfield.disp8);
e205caa7 9996
29b0f896
AM
9997 /* The PC relative address is computed relative
9998 to the instruction boundary, so in case immediate
9999 fields follows, we need to adjust the value. */
10000 if (pcrel && i.imm_operands)
10001 {
29b0f896 10002 unsigned int n1;
e205caa7 10003 int sz = 0;
252b5132 10004
29b0f896 10005 for (n1 = 0; n1 < i.operands; n1++)
40fb9820 10006 if (operand_type_check (i.types[n1], imm))
252b5132 10007 {
e205caa7
L
10008 /* Only one immediate is allowed for PC
10009 relative address. */
9c2799c2 10010 gas_assert (sz == 0);
e205caa7
L
10011 sz = imm_size (n1);
10012 i.op[n].disps->X_add_number -= sz;
252b5132 10013 }
29b0f896 10014 /* We should find the immediate. */
9c2799c2 10015 gas_assert (sz != 0);
29b0f896 10016 }
520dc8e8 10017
29b0f896 10018 p = frag_more (size);
d258b828 10019 reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
d6ab8113 10020 if (GOT_symbol
2bbd9c25 10021 && GOT_symbol == i.op[n].disps->X_add_symbol
d6ab8113 10022 && (((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
10023 || reloc_type == BFD_RELOC_X86_64_32S
10024 || (reloc_type == BFD_RELOC_64
10025 && object_64bit))
d6ab8113
JB
10026 && (i.op[n].disps->X_op == O_symbol
10027 || (i.op[n].disps->X_op == O_add
10028 && ((symbol_get_value_expression
10029 (i.op[n].disps->X_op_symbol)->X_op)
10030 == O_subtract))))
10031 || reloc_type == BFD_RELOC_32_PCREL))
2bbd9c25 10032 {
4fa24527 10033 if (!object_64bit)
7b81dfbb
AJ
10034 {
10035 reloc_type = BFD_RELOC_386_GOTPC;
5b7c81bd 10036 i.has_gotpc_tls_reloc = true;
98da05bf 10037 i.op[n].disps->X_add_number +=
d583596c 10038 encoding_length (insn_start_frag, insn_start_off, p);
7b81dfbb
AJ
10039 }
10040 else if (reloc_type == BFD_RELOC_64)
10041 reloc_type = BFD_RELOC_X86_64_GOTPC64;
d6ab8113 10042 else
7b81dfbb
AJ
10043 /* Don't do the adjustment for x86-64, as there
10044 the pcrel addressing is relative to the _next_
10045 insn, and that is taken care of in other code. */
d6ab8113 10046 reloc_type = BFD_RELOC_X86_64_GOTPC32;
2bbd9c25 10047 }
e379e5f3
L
10048 else if (align_branch_power)
10049 {
10050 switch (reloc_type)
10051 {
10052 case BFD_RELOC_386_TLS_GD:
10053 case BFD_RELOC_386_TLS_LDM:
10054 case BFD_RELOC_386_TLS_IE:
10055 case BFD_RELOC_386_TLS_IE_32:
10056 case BFD_RELOC_386_TLS_GOTIE:
10057 case BFD_RELOC_386_TLS_GOTDESC:
10058 case BFD_RELOC_386_TLS_DESC_CALL:
10059 case BFD_RELOC_X86_64_TLSGD:
10060 case BFD_RELOC_X86_64_TLSLD:
10061 case BFD_RELOC_X86_64_GOTTPOFF:
10062 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
10063 case BFD_RELOC_X86_64_TLSDESC_CALL:
5b7c81bd 10064 i.has_gotpc_tls_reloc = true;
e379e5f3
L
10065 default:
10066 break;
10067 }
10068 }
02a86693
L
10069 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal,
10070 size, i.op[n].disps, pcrel,
10071 reloc_type);
eb19308f
JB
10072
10073 if (flag_code == CODE_64BIT && size == 4 && pcrel
10074 && !i.prefix[ADDR_PREFIX])
10075 fixP->fx_signed = 1;
10076
02a86693
L
10077 /* Check for "call/jmp *mem", "mov mem, %reg",
10078 "test %reg, mem" and "binop mem, %reg" where binop
10079 is one of adc, add, and, cmp, or, sbb, sub, xor
e60f4d3b
L
10080 instructions without data prefix. Always generate
10081 R_386_GOT32X for "sym*GOT" operand in 32-bit mode. */
10082 if (i.prefix[DATA_PREFIX] == 0
10083 && (generate_relax_relocations
10084 || (!object_64bit
10085 && i.rm.mode == 0
10086 && i.rm.regmem == 5))
0cb4071e
L
10087 && (i.rm.mode == 2
10088 || (i.rm.mode == 0 && i.rm.regmem == 5))
ddb62495 10089 && i.tm.opcode_space == SPACE_BASE
02a86693
L
10090 && ((i.operands == 1
10091 && i.tm.base_opcode == 0xff
10092 && (i.rm.reg == 2 || i.rm.reg == 4))
10093 || (i.operands == 2
10094 && (i.tm.base_opcode == 0x8b
10095 || i.tm.base_opcode == 0x85
2ae4c703 10096 || (i.tm.base_opcode & ~0x38) == 0x03))))
02a86693
L
10097 {
10098 if (object_64bit)
10099 {
10100 fixP->fx_tcbit = i.rex != 0;
10101 if (i.base_reg
e968fc9b 10102 && (i.base_reg->reg_num == RegIP))
02a86693
L
10103 fixP->fx_tcbit2 = 1;
10104 }
10105 else
10106 fixP->fx_tcbit2 = 1;
10107 }
29b0f896
AM
10108 }
10109 }
10110 }
10111}
252b5132 10112
29b0f896 10113static void
64e74474 10114output_imm (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
10115{
10116 char *p;
10117 unsigned int n;
252b5132 10118
29b0f896
AM
10119 for (n = 0; n < i.operands; n++)
10120 {
40fb9820 10121 if (operand_type_check (i.types[n], imm))
29b0f896 10122 {
48ef937e
JB
10123 int size = imm_size (n);
10124
10125 if (now_seg == absolute_section)
10126 abs_section_offset += size;
10127 else if (i.op[n].imms->X_op == O_constant)
29b0f896 10128 {
29b0f896 10129 offsetT val;
b4cac588 10130
29b0f896
AM
10131 val = offset_in_range (i.op[n].imms->X_add_number,
10132 size);
10133 p = frag_more (size);
10134 md_number_to_chars (p, val, size);
10135 }
10136 else
10137 {
10138 /* Not absolute_section.
10139 Need a 32-bit fixup (don't support 8bit
10140 non-absolute imms). Try to support other
10141 sizes ... */
f86103b7 10142 enum bfd_reloc_code_real reloc_type;
e205caa7 10143 int sign;
29b0f896 10144
40fb9820 10145 if (i.types[n].bitfield.imm32s
a7d61044 10146 && (i.suffix == QWORD_MNEM_SUFFIX
40fb9820 10147 || (!i.suffix && i.tm.opcode_modifier.no_lsuf)))
29b0f896 10148 sign = 1;
e205caa7
L
10149 else
10150 sign = 0;
520dc8e8 10151
29b0f896 10152 p = frag_more (size);
d258b828 10153 reloc_type = reloc (size, 0, sign, i.reloc[n]);
f86103b7 10154
2bbd9c25
JJ
10155 /* This is tough to explain. We end up with this one if we
10156 * have operands that look like
10157 * "_GLOBAL_OFFSET_TABLE_+[.-.L284]". The goal here is to
10158 * obtain the absolute address of the GOT, and it is strongly
10159 * preferable from a performance point of view to avoid using
10160 * a runtime relocation for this. The actual sequence of
10161 * instructions often look something like:
10162 *
10163 * call .L66
10164 * .L66:
10165 * popl %ebx
10166 * addl $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
10167 *
10168 * The call and pop essentially return the absolute address
10169 * of the label .L66 and store it in %ebx. The linker itself
10170 * will ultimately change the first operand of the addl so
10171 * that %ebx points to the GOT, but to keep things simple, the
10172 * .o file must have this operand set so that it generates not
10173 * the absolute address of .L66, but the absolute address of
10174 * itself. This allows the linker itself simply treat a GOTPC
10175 * relocation as asking for a pcrel offset to the GOT to be
10176 * added in, and the addend of the relocation is stored in the
10177 * operand field for the instruction itself.
10178 *
10179 * Our job here is to fix the operand so that it would add
10180 * the correct offset so that %ebx would point to itself. The
10181 * thing that is tricky is that .-.L66 will point to the
10182 * beginning of the instruction, so we need to further modify
10183 * the operand so that it will point to itself. There are
10184 * other cases where you have something like:
10185 *
10186 * .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
10187 *
10188 * and here no correction would be required. Internally in
10189 * the assembler we treat operands of this form as not being
10190 * pcrel since the '.' is explicitly mentioned, and I wonder
10191 * whether it would simplify matters to do it this way. Who
10192 * knows. In earlier versions of the PIC patches, the
10193 * pcrel_adjust field was used to store the correction, but
10194 * since the expression is not pcrel, I felt it would be
10195 * confusing to do it this way. */
10196
d6ab8113 10197 if ((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
10198 || reloc_type == BFD_RELOC_X86_64_32S
10199 || reloc_type == BFD_RELOC_64)
29b0f896
AM
10200 && GOT_symbol
10201 && GOT_symbol == i.op[n].imms->X_add_symbol
10202 && (i.op[n].imms->X_op == O_symbol
10203 || (i.op[n].imms->X_op == O_add
10204 && ((symbol_get_value_expression
10205 (i.op[n].imms->X_op_symbol)->X_op)
10206 == O_subtract))))
10207 {
4fa24527 10208 if (!object_64bit)
d6ab8113 10209 reloc_type = BFD_RELOC_386_GOTPC;
7b81dfbb 10210 else if (size == 4)
d6ab8113 10211 reloc_type = BFD_RELOC_X86_64_GOTPC32;
7b81dfbb
AJ
10212 else if (size == 8)
10213 reloc_type = BFD_RELOC_X86_64_GOTPC64;
5b7c81bd 10214 i.has_gotpc_tls_reloc = true;
d583596c
JB
10215 i.op[n].imms->X_add_number +=
10216 encoding_length (insn_start_frag, insn_start_off, p);
29b0f896 10217 }
29b0f896
AM
10218 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
10219 i.op[n].imms, 0, reloc_type);
10220 }
10221 }
10222 }
252b5132
RH
10223}
10224\f
d182319b
JB
10225/* x86_cons_fix_new is called via the expression parsing code when a
10226 reloc is needed. We use this hook to get the correct .got reloc. */
d182319b
JB
10227static int cons_sign = -1;
10228
10229void
e3bb37b5 10230x86_cons_fix_new (fragS *frag, unsigned int off, unsigned int len,
62ebcb5c 10231 expressionS *exp, bfd_reloc_code_real_type r)
d182319b 10232{
d258b828 10233 r = reloc (len, 0, cons_sign, r);
d182319b
JB
10234
10235#ifdef TE_PE
10236 if (exp->X_op == O_secrel)
10237 {
10238 exp->X_op = O_symbol;
10239 r = BFD_RELOC_32_SECREL;
10240 }
145667f8
MH
10241 else if (exp->X_op == O_secidx)
10242 r = BFD_RELOC_16_SECIDX;
d182319b
JB
10243#endif
10244
10245 fix_new_exp (frag, off, len, exp, 0, r);
10246}
10247
357d1bd8
L
10248/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
10249 purpose of the `.dc.a' internal pseudo-op. */
10250
10251int
10252x86_address_bytes (void)
10253{
10254 if ((stdoutput->arch_info->mach & bfd_mach_x64_32))
10255 return 4;
10256 return stdoutput->arch_info->bits_per_address / 8;
10257}
10258
deea4973
JB
10259#if (!(defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined (OBJ_MACH_O)) \
10260 || defined (LEX_AT)) && !defined (TE_PE)
d258b828 10261# define lex_got(reloc, adjust, types) NULL
718ddfc0 10262#else
f3c180ae
AM
10263/* Parse operands of the form
10264 <symbol>@GOTOFF+<nnn>
10265 and similar .plt or .got references.
10266
10267 If we find one, set up the correct relocation in RELOC and copy the
10268 input string, minus the `@GOTOFF' into a malloc'd buffer for
10269 parsing by the calling routine. Return this buffer, and if ADJUST
10270 is non-null set it to the length of the string we removed from the
10271 input line. Otherwise return NULL. */
10272static char *
91d6fa6a 10273lex_got (enum bfd_reloc_code_real *rel,
64e74474 10274 int *adjust,
d258b828 10275 i386_operand_type *types)
f3c180ae 10276{
7b81dfbb
AJ
10277 /* Some of the relocations depend on the size of what field is to
10278 be relocated. But in our callers i386_immediate and i386_displacement
10279 we don't yet know the operand size (this will be set by insn
10280 matching). Hence we record the word32 relocation here,
10281 and adjust the reloc according to the real size in reloc(). */
145667f8
MH
10282 static const struct
10283 {
f3c180ae 10284 const char *str;
cff8d58a 10285 int len;
4fa24527 10286 const enum bfd_reloc_code_real rel[2];
40fb9820 10287 const i386_operand_type types64;
5b7c81bd 10288 bool need_GOT_symbol;
145667f8
MH
10289 }
10290 gotrel[] =
10291 {
05909f23
JB
10292
10293#define OPERAND_TYPE_IMM32_32S_DISP32 { .bitfield = \
10294 { .imm32 = 1, .imm32s = 1, .disp32 = 1 } }
10295#define OPERAND_TYPE_IMM32_32S_64_DISP32 { .bitfield = \
10296 { .imm32 = 1, .imm32s = 1, .imm64 = 1, .disp32 = 1 } }
10297#define OPERAND_TYPE_IMM32_32S_64_DISP32_64 { .bitfield = \
10298 { .imm32 = 1, .imm32s = 1, .imm64 = 1, .disp32 = 1, .disp64 = 1 } }
10299#define OPERAND_TYPE_IMM64_DISP64 { .bitfield = \
10300 { .imm64 = 1, .disp64 = 1 } }
10301
deea4973 10302#ifndef TE_PE
8ce3d284 10303#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
10304 { STRING_COMMA_LEN ("SIZE"), { BFD_RELOC_SIZE32,
10305 BFD_RELOC_SIZE32 },
05909f23 10306 { .bitfield = { .imm32 = 1, .imm64 = 1 } }, false },
8ce3d284 10307#endif
cff8d58a
L
10308 { STRING_COMMA_LEN ("PLTOFF"), { _dummy_first_bfd_reloc_code_real,
10309 BFD_RELOC_X86_64_PLTOFF64 },
05909f23 10310 { .bitfield = { .imm64 = 1 } }, true },
cff8d58a
L
10311 { STRING_COMMA_LEN ("PLT"), { BFD_RELOC_386_PLT32,
10312 BFD_RELOC_X86_64_PLT32 },
a775efc8 10313 OPERAND_TYPE_IMM32_32S_DISP32, false },
cff8d58a
L
10314 { STRING_COMMA_LEN ("GOTPLT"), { _dummy_first_bfd_reloc_code_real,
10315 BFD_RELOC_X86_64_GOTPLT64 },
5b7c81bd 10316 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
10317 { STRING_COMMA_LEN ("GOTOFF"), { BFD_RELOC_386_GOTOFF,
10318 BFD_RELOC_X86_64_GOTOFF64 },
5b7c81bd 10319 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
10320 { STRING_COMMA_LEN ("GOTPCREL"), { _dummy_first_bfd_reloc_code_real,
10321 BFD_RELOC_X86_64_GOTPCREL },
a775efc8 10322 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10323 { STRING_COMMA_LEN ("TLSGD"), { BFD_RELOC_386_TLS_GD,
10324 BFD_RELOC_X86_64_TLSGD },
a775efc8 10325 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10326 { STRING_COMMA_LEN ("TLSLDM"), { BFD_RELOC_386_TLS_LDM,
10327 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10328 OPERAND_TYPE_NONE, true },
cff8d58a
L
10329 { STRING_COMMA_LEN ("TLSLD"), { _dummy_first_bfd_reloc_code_real,
10330 BFD_RELOC_X86_64_TLSLD },
a775efc8 10331 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10332 { STRING_COMMA_LEN ("GOTTPOFF"), { BFD_RELOC_386_TLS_IE_32,
10333 BFD_RELOC_X86_64_GOTTPOFF },
a775efc8 10334 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10335 { STRING_COMMA_LEN ("TPOFF"), { BFD_RELOC_386_TLS_LE_32,
10336 BFD_RELOC_X86_64_TPOFF32 },
a775efc8 10337 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
10338 { STRING_COMMA_LEN ("NTPOFF"), { BFD_RELOC_386_TLS_LE,
10339 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10340 OPERAND_TYPE_NONE, true },
cff8d58a
L
10341 { STRING_COMMA_LEN ("DTPOFF"), { BFD_RELOC_386_TLS_LDO_32,
10342 BFD_RELOC_X86_64_DTPOFF32 },
a775efc8 10343 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
10344 { STRING_COMMA_LEN ("GOTNTPOFF"),{ BFD_RELOC_386_TLS_GOTIE,
10345 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10346 OPERAND_TYPE_NONE, true },
cff8d58a
L
10347 { STRING_COMMA_LEN ("INDNTPOFF"),{ BFD_RELOC_386_TLS_IE,
10348 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10349 OPERAND_TYPE_NONE, true },
cff8d58a
L
10350 { STRING_COMMA_LEN ("GOT"), { BFD_RELOC_386_GOT32,
10351 BFD_RELOC_X86_64_GOT32 },
a775efc8 10352 OPERAND_TYPE_IMM32_32S_64_DISP32, true },
cff8d58a
L
10353 { STRING_COMMA_LEN ("TLSDESC"), { BFD_RELOC_386_TLS_GOTDESC,
10354 BFD_RELOC_X86_64_GOTPC32_TLSDESC },
a775efc8 10355 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10356 { STRING_COMMA_LEN ("TLSCALL"), { BFD_RELOC_386_TLS_DESC_CALL,
10357 BFD_RELOC_X86_64_TLSDESC_CALL },
a775efc8 10358 OPERAND_TYPE_IMM32_32S_DISP32, true },
deea4973
JB
10359#else /* TE_PE */
10360 { STRING_COMMA_LEN ("SECREL32"), { BFD_RELOC_32_SECREL,
10361 BFD_RELOC_32_SECREL },
a775efc8 10362 OPERAND_TYPE_IMM32_32S_64_DISP32_64, false },
deea4973 10363#endif
05909f23
JB
10364
10365#undef OPERAND_TYPE_IMM32_32S_DISP32
10366#undef OPERAND_TYPE_IMM32_32S_64_DISP32
10367#undef OPERAND_TYPE_IMM32_32S_64_DISP32_64
10368#undef OPERAND_TYPE_IMM64_DISP64
10369
f3c180ae
AM
10370 };
10371 char *cp;
10372 unsigned int j;
10373
deea4973 10374#if defined (OBJ_MAYBE_ELF) && !defined (TE_PE)
718ddfc0
JB
10375 if (!IS_ELF)
10376 return NULL;
d382c579 10377#endif
718ddfc0 10378
f3c180ae 10379 for (cp = input_line_pointer; *cp != '@'; cp++)
67c11a9b 10380 if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
f3c180ae
AM
10381 return NULL;
10382
47465058 10383 for (j = 0; j < ARRAY_SIZE (gotrel); j++)
f3c180ae 10384 {
cff8d58a 10385 int len = gotrel[j].len;
28f81592 10386 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
f3c180ae 10387 {
4fa24527 10388 if (gotrel[j].rel[object_64bit] != 0)
f3c180ae 10389 {
28f81592
AM
10390 int first, second;
10391 char *tmpbuf, *past_reloc;
f3c180ae 10392
91d6fa6a 10393 *rel = gotrel[j].rel[object_64bit];
f3c180ae 10394
3956db08
JB
10395 if (types)
10396 {
10397 if (flag_code != CODE_64BIT)
40fb9820
L
10398 {
10399 types->bitfield.imm32 = 1;
10400 types->bitfield.disp32 = 1;
10401 }
3956db08
JB
10402 else
10403 *types = gotrel[j].types64;
10404 }
10405
844bf810 10406 if (gotrel[j].need_GOT_symbol && GOT_symbol == NULL)
f3c180ae
AM
10407 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
10408
28f81592 10409 /* The length of the first part of our input line. */
f3c180ae 10410 first = cp - input_line_pointer;
28f81592
AM
10411
10412 /* The second part goes from after the reloc token until
67c11a9b 10413 (and including) an end_of_line char or comma. */
28f81592 10414 past_reloc = cp + 1 + len;
67c11a9b
AM
10415 cp = past_reloc;
10416 while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
10417 ++cp;
10418 second = cp + 1 - past_reloc;
28f81592
AM
10419
10420 /* Allocate and copy string. The trailing NUL shouldn't
10421 be necessary, but be safe. */
add39d23 10422 tmpbuf = XNEWVEC (char, first + second + 2);
f3c180ae 10423 memcpy (tmpbuf, input_line_pointer, first);
0787a12d
AM
10424 if (second != 0 && *past_reloc != ' ')
10425 /* Replace the relocation token with ' ', so that
10426 errors like foo@GOTOFF1 will be detected. */
10427 tmpbuf[first++] = ' ';
af89796a
L
10428 else
10429 /* Increment length by 1 if the relocation token is
10430 removed. */
10431 len++;
10432 if (adjust)
10433 *adjust = len;
0787a12d
AM
10434 memcpy (tmpbuf + first, past_reloc, second);
10435 tmpbuf[first + second] = '\0';
f3c180ae
AM
10436 return tmpbuf;
10437 }
10438
4fa24527
JB
10439 as_bad (_("@%s reloc is not supported with %d-bit output format"),
10440 gotrel[j].str, 1 << (5 + object_64bit));
f3c180ae
AM
10441 return NULL;
10442 }
10443 }
10444
10445 /* Might be a symbol version string. Don't as_bad here. */
10446 return NULL;
10447}
4e4f7c87 10448#endif
f3c180ae 10449
62ebcb5c 10450bfd_reloc_code_real_type
e3bb37b5 10451x86_cons (expressionS *exp, int size)
f3c180ae 10452{
62ebcb5c
AM
10453 bfd_reloc_code_real_type got_reloc = NO_RELOC;
10454
2748c1b1
L
10455#if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
10456 && !defined (LEX_AT)) \
10457 || defined (TE_PE)
ee86248c
JB
10458 intel_syntax = -intel_syntax;
10459
3c7b9c2c 10460 exp->X_md = 0;
4fa24527 10461 if (size == 4 || (object_64bit && size == 8))
f3c180ae
AM
10462 {
10463 /* Handle @GOTOFF and the like in an expression. */
10464 char *save;
10465 char *gotfree_input_line;
4a57f2cf 10466 int adjust = 0;
f3c180ae
AM
10467
10468 save = input_line_pointer;
d258b828 10469 gotfree_input_line = lex_got (&got_reloc, &adjust, NULL);
f3c180ae
AM
10470 if (gotfree_input_line)
10471 input_line_pointer = gotfree_input_line;
10472
10473 expression (exp);
10474
10475 if (gotfree_input_line)
10476 {
10477 /* expression () has merrily parsed up to the end of line,
10478 or a comma - in the wrong buffer. Transfer how far
10479 input_line_pointer has moved to the right buffer. */
10480 input_line_pointer = (save
10481 + (input_line_pointer - gotfree_input_line)
10482 + adjust);
10483 free (gotfree_input_line);
3992d3b7
AM
10484 if (exp->X_op == O_constant
10485 || exp->X_op == O_absent
10486 || exp->X_op == O_illegal
0398aac5 10487 || exp->X_op == O_register
3992d3b7
AM
10488 || exp->X_op == O_big)
10489 {
10490 char c = *input_line_pointer;
10491 *input_line_pointer = 0;
10492 as_bad (_("missing or invalid expression `%s'"), save);
10493 *input_line_pointer = c;
10494 }
b9519cfe
L
10495 else if ((got_reloc == BFD_RELOC_386_PLT32
10496 || got_reloc == BFD_RELOC_X86_64_PLT32)
10497 && exp->X_op != O_symbol)
10498 {
10499 char c = *input_line_pointer;
10500 *input_line_pointer = 0;
10501 as_bad (_("invalid PLT expression `%s'"), save);
10502 *input_line_pointer = c;
10503 }
f3c180ae
AM
10504 }
10505 }
10506 else
10507 expression (exp);
ee86248c
JB
10508
10509 intel_syntax = -intel_syntax;
10510
10511 if (intel_syntax)
10512 i386_intel_simplify (exp);
2748c1b1
L
10513#else
10514 expression (exp);
10515#endif
62ebcb5c 10516
a442cac5
JB
10517 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
10518 if (size == 4 && exp->X_op == O_constant && !object_64bit)
10519 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
10520
62ebcb5c 10521 return got_reloc;
f3c180ae 10522}
f3c180ae 10523
9f32dd5b
L
10524static void
10525signed_cons (int size)
6482c264 10526{
a442cac5 10527 if (object_64bit)
d182319b
JB
10528 cons_sign = 1;
10529 cons (size);
10530 cons_sign = -1;
6482c264
NC
10531}
10532
edd67638
JB
10533static void
10534s_insn (int dummy ATTRIBUTE_UNUSED)
10535{
10536 char mnemonic[MAX_MNEM_SIZE], *line = input_line_pointer;
10537 char *saved_ilp = find_end_of_line (line, false), saved_char;
10538 const char *end;
10539 unsigned int j;
10540 valueT val;
10541 bool vex = false, xop = false, evex = false;
10542 static const templates tt = { &i.tm, &i.tm + 1 };
10543
10544 init_globals ();
10545
10546 saved_char = *saved_ilp;
10547 *saved_ilp = 0;
10548
10549 end = parse_insn (line, mnemonic, true);
10550 if (end == NULL)
10551 {
10552 bad:
10553 *saved_ilp = saved_char;
10554 ignore_rest_of_line ();
10555 return;
10556 }
10557 line += end - line;
10558
10559 current_templates = &tt;
10560 i.tm.mnem_off = MN__insn;
10561
10562 if (startswith (line, "VEX")
10563 && (line[3] == '.' || is_space_char (line[3])))
10564 {
10565 vex = true;
10566 line += 3;
10567 }
10568 else if (startswith (line, "XOP") && ISDIGIT (line[3]))
10569 {
10570 char *e;
10571 unsigned long n = strtoul (line + 3, &e, 16);
10572
10573 if (e == line + 5 && n >= 0x08 && n <= 0x1f
10574 && (*e == '.' || is_space_char (*e)))
10575 {
10576 xop = true;
10577 line = e;
10578 }
10579 }
10580 else if (startswith (line, "EVEX")
10581 && (line[4] == '.' || is_space_char (line[4])))
10582 {
10583 evex = true;
10584 line += 4;
10585 }
10586
10587 if (vex || xop
10588 ? i.vec_encoding == vex_encoding_evex
10589 : evex
10590 ? i.vec_encoding == vex_encoding_vex
10591 || i.vec_encoding == vex_encoding_vex3
10592 : i.vec_encoding != vex_encoding_default)
10593 {
10594 as_bad (_("pseudo-prefix conflicts with encoding specifier"));
10595 goto bad;
10596 }
10597
10598 if (line > end && *line == '.')
10599 {
10600 }
10601
10602 input_line_pointer = line;
10603 val = get_absolute_expression ();
10604 line = input_line_pointer;
10605
10606 for (j = 1; j < sizeof(val); ++j)
10607 if (!(val >> (j * 8)))
10608 break;
10609
10610 /* Trim off a prefix if present. */
10611 if (j > 1 && !vex && !xop && !evex)
10612 {
10613 uint8_t byte = val >> ((j - 1) * 8);
10614
10615 switch (byte)
10616 {
10617 case DATA_PREFIX_OPCODE:
10618 case REPE_PREFIX_OPCODE:
10619 case REPNE_PREFIX_OPCODE:
10620 if (!add_prefix (byte))
10621 goto bad;
10622 val &= ((uint64_t)1 << (--j * 8)) - 1;
10623 break;
10624 }
10625 }
10626
10627 /* Trim off encoding space. */
10628 if (j > 1 && !i.tm.opcode_space && (val >> ((j - 1) * 8)) == 0x0f)
10629 {
10630 uint8_t byte = val >> ((--j - 1) * 8);
10631
10632 i.tm.opcode_space = SPACE_0F;
10633 switch (byte & -(j > 1))
10634 {
10635 case 0x38:
10636 i.tm.opcode_space = SPACE_0F38;
10637 --j;
10638 break;
10639 case 0x3a:
10640 i.tm.opcode_space = SPACE_0F3A;
10641 --j;
10642 break;
10643 }
10644 val &= ((uint64_t)1 << (j * 8)) - 1;
10645 }
10646
10647 if (j > 2)
10648 {
10649 as_bad (_("opcode residual (%#"PRIx64") too wide"), (uint64_t) val);
10650 goto bad;
10651 }
10652 i.opcode_length = j;
10653 i.tm.base_opcode = val;
10654
10655 output_insn ();
10656
10657 *saved_ilp = saved_char;
10658 input_line_pointer = line;
10659
10660 demand_empty_rest_of_line ();
10661}
10662
d182319b 10663#ifdef TE_PE
6482c264 10664static void
7016a5d5 10665pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
6482c264
NC
10666{
10667 expressionS exp;
10668
10669 do
10670 {
10671 expression (&exp);
10672 if (exp.X_op == O_symbol)
10673 exp.X_op = O_secrel;
10674
10675 emit_expr (&exp, 4);
10676 }
10677 while (*input_line_pointer++ == ',');
10678
10679 input_line_pointer--;
10680 demand_empty_rest_of_line ();
10681}
145667f8
MH
10682
10683static void
10684pe_directive_secidx (int dummy ATTRIBUTE_UNUSED)
10685{
10686 expressionS exp;
10687
10688 do
10689 {
10690 expression (&exp);
10691 if (exp.X_op == O_symbol)
10692 exp.X_op = O_secidx;
10693
10694 emit_expr (&exp, 2);
10695 }
10696 while (*input_line_pointer++ == ',');
10697
10698 input_line_pointer--;
10699 demand_empty_rest_of_line ();
10700}
6482c264
NC
10701#endif
10702
7063667e
JB
10703/* Handle Rounding Control / SAE specifiers. */
10704
10705static char *
10706RC_SAE_specifier (const char *pstr)
10707{
10708 unsigned int j;
10709
10710 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); j++)
10711 {
10712 if (!strncmp (pstr, RC_NamesTable[j].name, RC_NamesTable[j].len))
10713 {
10714 if (i.rounding.type != rc_none)
10715 {
10716 as_bad (_("duplicated `{%s}'"), RC_NamesTable[j].name);
10717 return NULL;
10718 }
10719
10720 i.rounding.type = RC_NamesTable[j].type;
10721
10722 return (char *)(pstr + RC_NamesTable[j].len);
10723 }
10724 }
10725
10726 return NULL;
10727}
10728
43234a1e
L
10729/* Handle Vector operations. */
10730
10731static char *
f70c6814 10732check_VecOperations (char *op_string)
43234a1e
L
10733{
10734 const reg_entry *mask;
10735 const char *saved;
10736 char *end_op;
10737
f70c6814 10738 while (*op_string)
43234a1e
L
10739 {
10740 saved = op_string;
10741 if (*op_string == '{')
10742 {
10743 op_string++;
10744
10745 /* Check broadcasts. */
d34049e8 10746 if (startswith (op_string, "1to"))
43234a1e 10747 {
5273a3cd 10748 unsigned int bcst_type;
43234a1e 10749
5273a3cd 10750 if (i.broadcast.type)
43234a1e
L
10751 goto duplicated_vec_op;
10752
10753 op_string += 3;
10754 if (*op_string == '8')
8e6e0792 10755 bcst_type = 8;
b28d1bda 10756 else if (*op_string == '4')
8e6e0792 10757 bcst_type = 4;
b28d1bda 10758 else if (*op_string == '2')
8e6e0792 10759 bcst_type = 2;
43234a1e
L
10760 else if (*op_string == '1'
10761 && *(op_string+1) == '6')
10762 {
8e6e0792 10763 bcst_type = 16;
43234a1e
L
10764 op_string++;
10765 }
0cc78721
CL
10766 else if (*op_string == '3'
10767 && *(op_string+1) == '2')
10768 {
10769 bcst_type = 32;
10770 op_string++;
10771 }
43234a1e
L
10772 else
10773 {
10774 as_bad (_("Unsupported broadcast: `%s'"), saved);
10775 return NULL;
10776 }
10777 op_string++;
10778
5273a3cd
JB
10779 i.broadcast.type = bcst_type;
10780 i.broadcast.operand = this_operand;
43234a1e
L
10781 }
10782 /* Check masking operation. */
10783 else if ((mask = parse_register (op_string, &end_op)) != NULL)
10784 {
8a6fb3f9
JB
10785 if (mask == &bad_reg)
10786 return NULL;
10787
43234a1e 10788 /* k0 can't be used for write mask. */
f74a6307 10789 if (mask->reg_type.bitfield.class != RegMask || !mask->reg_num)
43234a1e 10790 {
6d2cd6b2
JB
10791 as_bad (_("`%s%s' can't be used for write mask"),
10792 register_prefix, mask->reg_name);
43234a1e
L
10793 return NULL;
10794 }
10795
6225c532 10796 if (!i.mask.reg)
43234a1e 10797 {
6225c532
JB
10798 i.mask.reg = mask;
10799 i.mask.operand = this_operand;
43234a1e 10800 }
6225c532
JB
10801 else if (i.mask.reg->reg_num)
10802 goto duplicated_vec_op;
43234a1e
L
10803 else
10804 {
6225c532 10805 i.mask.reg = mask;
43234a1e
L
10806
10807 /* Only "{z}" is allowed here. No need to check
10808 zeroing mask explicitly. */
6225c532 10809 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
10810 {
10811 as_bad (_("invalid write mask `%s'"), saved);
10812 return NULL;
10813 }
10814 }
10815
10816 op_string = end_op;
10817 }
10818 /* Check zeroing-flag for masking operation. */
10819 else if (*op_string == 'z')
10820 {
6225c532 10821 if (!i.mask.reg)
43234a1e 10822 {
6225c532
JB
10823 i.mask.reg = reg_k0;
10824 i.mask.zeroing = 1;
10825 i.mask.operand = this_operand;
43234a1e
L
10826 }
10827 else
10828 {
6225c532 10829 if (i.mask.zeroing)
43234a1e
L
10830 {
10831 duplicated_vec_op:
10832 as_bad (_("duplicated `%s'"), saved);
10833 return NULL;
10834 }
10835
6225c532 10836 i.mask.zeroing = 1;
43234a1e
L
10837
10838 /* Only "{%k}" is allowed here. No need to check mask
10839 register explicitly. */
6225c532 10840 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
10841 {
10842 as_bad (_("invalid zeroing-masking `%s'"),
10843 saved);
10844 return NULL;
10845 }
10846 }
10847
10848 op_string++;
10849 }
7063667e
JB
10850 else if (intel_syntax
10851 && (op_string = RC_SAE_specifier (op_string)) != NULL)
10852 i.rounding.modifier = true;
43234a1e
L
10853 else
10854 goto unknown_vec_op;
10855
10856 if (*op_string != '}')
10857 {
10858 as_bad (_("missing `}' in `%s'"), saved);
10859 return NULL;
10860 }
10861 op_string++;
0ba3a731
L
10862
10863 /* Strip whitespace since the addition of pseudo prefixes
10864 changed how the scrubber treats '{'. */
10865 if (is_space_char (*op_string))
10866 ++op_string;
10867
43234a1e
L
10868 continue;
10869 }
10870 unknown_vec_op:
10871 /* We don't know this one. */
10872 as_bad (_("unknown vector operation: `%s'"), saved);
10873 return NULL;
10874 }
10875
6225c532 10876 if (i.mask.reg && i.mask.zeroing && !i.mask.reg->reg_num)
6d2cd6b2
JB
10877 {
10878 as_bad (_("zeroing-masking only allowed with write mask"));
10879 return NULL;
10880 }
10881
43234a1e
L
10882 return op_string;
10883}
10884
252b5132 10885static int
70e41ade 10886i386_immediate (char *imm_start)
252b5132
RH
10887{
10888 char *save_input_line_pointer;
f3c180ae 10889 char *gotfree_input_line;
252b5132 10890 segT exp_seg = 0;
47926f60 10891 expressionS *exp;
40fb9820
L
10892 i386_operand_type types;
10893
0dfbf9d7 10894 operand_type_set (&types, ~0);
252b5132
RH
10895
10896 if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
10897 {
31b2323c
L
10898 as_bad (_("at most %d immediate operands are allowed"),
10899 MAX_IMMEDIATE_OPERANDS);
252b5132
RH
10900 return 0;
10901 }
10902
10903 exp = &im_expressions[i.imm_operands++];
520dc8e8 10904 i.op[this_operand].imms = exp;
252b5132
RH
10905
10906 if (is_space_char (*imm_start))
10907 ++imm_start;
10908
10909 save_input_line_pointer = input_line_pointer;
10910 input_line_pointer = imm_start;
10911
d258b828 10912 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
10913 if (gotfree_input_line)
10914 input_line_pointer = gotfree_input_line;
252b5132
RH
10915
10916 exp_seg = expression (exp);
10917
83183c0c 10918 SKIP_WHITESPACE ();
252b5132 10919 if (*input_line_pointer)
f3c180ae 10920 as_bad (_("junk `%s' after expression"), input_line_pointer);
252b5132
RH
10921
10922 input_line_pointer = save_input_line_pointer;
f3c180ae 10923 if (gotfree_input_line)
ee86248c
JB
10924 {
10925 free (gotfree_input_line);
10926
9aac24b1 10927 if (exp->X_op == O_constant)
ee86248c
JB
10928 exp->X_op = O_illegal;
10929 }
10930
9aac24b1
JB
10931 if (exp_seg == reg_section)
10932 {
10933 as_bad (_("illegal immediate register operand %s"), imm_start);
10934 return 0;
10935 }
10936
ee86248c
JB
10937 return i386_finalize_immediate (exp_seg, exp, types, imm_start);
10938}
252b5132 10939
ee86248c
JB
10940static int
10941i386_finalize_immediate (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
10942 i386_operand_type types, const char *imm_start)
10943{
10944 if (exp->X_op == O_absent || exp->X_op == O_illegal || exp->X_op == O_big)
252b5132 10945 {
313c53d1
L
10946 if (imm_start)
10947 as_bad (_("missing or invalid immediate expression `%s'"),
10948 imm_start);
3992d3b7 10949 return 0;
252b5132 10950 }
3e73aa7c 10951 else if (exp->X_op == O_constant)
252b5132 10952 {
47926f60 10953 /* Size it properly later. */
40fb9820 10954 i.types[this_operand].bitfield.imm64 = 1;
a442cac5
JB
10955
10956 /* If not 64bit, sign/zero extend val, to account for wraparound
10957 when !BFD64. */
10958 if (flag_code != CODE_64BIT)
10959 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
252b5132 10960 }
4c63da97 10961#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
f86103b7 10962 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
31312f95 10963 && exp_seg != absolute_section
47926f60 10964 && exp_seg != text_section
24eab124
AM
10965 && exp_seg != data_section
10966 && exp_seg != bss_section
10967 && exp_seg != undefined_section
f86103b7 10968 && !bfd_is_com_section (exp_seg))
252b5132 10969 {
d0b47220 10970 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
252b5132
RH
10971 return 0;
10972 }
10973#endif
10974 else
10975 {
10976 /* This is an address. The size of the address will be
24eab124 10977 determined later, depending on destination register,
3e73aa7c 10978 suffix, or the default for the section. */
40fb9820
L
10979 i.types[this_operand].bitfield.imm8 = 1;
10980 i.types[this_operand].bitfield.imm16 = 1;
10981 i.types[this_operand].bitfield.imm32 = 1;
10982 i.types[this_operand].bitfield.imm32s = 1;
10983 i.types[this_operand].bitfield.imm64 = 1;
c6fb90c8
L
10984 i.types[this_operand] = operand_type_and (i.types[this_operand],
10985 types);
252b5132
RH
10986 }
10987
10988 return 1;
10989}
10990
551c1ca1 10991static char *
e3bb37b5 10992i386_scale (char *scale)
252b5132 10993{
551c1ca1
AM
10994 offsetT val;
10995 char *save = input_line_pointer;
252b5132 10996
551c1ca1
AM
10997 input_line_pointer = scale;
10998 val = get_absolute_expression ();
10999
11000 switch (val)
252b5132 11001 {
551c1ca1 11002 case 1:
252b5132
RH
11003 i.log2_scale_factor = 0;
11004 break;
551c1ca1 11005 case 2:
252b5132
RH
11006 i.log2_scale_factor = 1;
11007 break;
551c1ca1 11008 case 4:
252b5132
RH
11009 i.log2_scale_factor = 2;
11010 break;
551c1ca1 11011 case 8:
252b5132
RH
11012 i.log2_scale_factor = 3;
11013 break;
11014 default:
a724f0f4
JB
11015 {
11016 char sep = *input_line_pointer;
11017
11018 *input_line_pointer = '\0';
11019 as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
11020 scale);
11021 *input_line_pointer = sep;
11022 input_line_pointer = save;
11023 return NULL;
11024 }
252b5132 11025 }
29b0f896 11026 if (i.log2_scale_factor != 0 && i.index_reg == 0)
252b5132
RH
11027 {
11028 as_warn (_("scale factor of %d without an index register"),
24eab124 11029 1 << i.log2_scale_factor);
252b5132 11030 i.log2_scale_factor = 0;
252b5132 11031 }
551c1ca1
AM
11032 scale = input_line_pointer;
11033 input_line_pointer = save;
11034 return scale;
252b5132
RH
11035}
11036
252b5132 11037static int
e3bb37b5 11038i386_displacement (char *disp_start, char *disp_end)
252b5132 11039{
29b0f896 11040 expressionS *exp;
252b5132
RH
11041 segT exp_seg = 0;
11042 char *save_input_line_pointer;
f3c180ae 11043 char *gotfree_input_line;
40fb9820
L
11044 int override;
11045 i386_operand_type bigdisp, types = anydisp;
3992d3b7 11046 int ret;
252b5132 11047
31b2323c
L
11048 if (i.disp_operands == MAX_MEMORY_OPERANDS)
11049 {
11050 as_bad (_("at most %d displacement operands are allowed"),
11051 MAX_MEMORY_OPERANDS);
11052 return 0;
11053 }
11054
0dfbf9d7 11055 operand_type_set (&bigdisp, 0);
6f2f06be 11056 if (i.jumpabsolute
48bcea9f 11057 || i.types[this_operand].bitfield.baseindex
0cfa3eb3
JB
11058 || (current_templates->start->opcode_modifier.jump != JUMP
11059 && current_templates->start->opcode_modifier.jump != JUMP_DWORD))
e05278af 11060 {
48bcea9f 11061 i386_addressing_mode ();
e05278af 11062 override = (i.prefix[ADDR_PREFIX] != 0);
40fb9820
L
11063 if (flag_code == CODE_64BIT)
11064 {
a775efc8 11065 bigdisp.bitfield.disp32 = 1;
40fb9820 11066 if (!override)
a775efc8 11067 bigdisp.bitfield.disp64 = 1;
40fb9820
L
11068 }
11069 else if ((flag_code == CODE_16BIT) ^ override)
40fb9820 11070 bigdisp.bitfield.disp16 = 1;
48bcea9f
JB
11071 else
11072 bigdisp.bitfield.disp32 = 1;
e05278af
JB
11073 }
11074 else
11075 {
376cd056
JB
11076 /* For PC-relative branches, the width of the displacement may be
11077 dependent upon data size, but is never dependent upon address size.
11078 Also make sure to not unintentionally match against a non-PC-relative
11079 branch template. */
11080 static templates aux_templates;
11081 const insn_template *t = current_templates->start;
5b7c81bd 11082 bool has_intel64 = false;
376cd056
JB
11083
11084 aux_templates.start = t;
11085 while (++t < current_templates->end)
11086 {
11087 if (t->opcode_modifier.jump
11088 != current_templates->start->opcode_modifier.jump)
11089 break;
4b5aaf5f 11090 if ((t->opcode_modifier.isa64 >= INTEL64))
5b7c81bd 11091 has_intel64 = true;
376cd056
JB
11092 }
11093 if (t < current_templates->end)
11094 {
11095 aux_templates.end = t;
11096 current_templates = &aux_templates;
11097 }
11098
e05278af 11099 override = (i.prefix[DATA_PREFIX] != 0);
40fb9820
L
11100 if (flag_code == CODE_64BIT)
11101 {
376cd056
JB
11102 if ((override || i.suffix == WORD_MNEM_SUFFIX)
11103 && (!intel64 || !has_intel64))
40fb9820
L
11104 bigdisp.bitfield.disp16 = 1;
11105 else
a775efc8 11106 bigdisp.bitfield.disp32 = 1;
40fb9820
L
11107 }
11108 else
e05278af
JB
11109 {
11110 if (!override)
11111 override = (i.suffix == (flag_code != CODE_16BIT
11112 ? WORD_MNEM_SUFFIX
11113 : LONG_MNEM_SUFFIX));
40fb9820
L
11114 bigdisp.bitfield.disp32 = 1;
11115 if ((flag_code == CODE_16BIT) ^ override)
11116 {
11117 bigdisp.bitfield.disp32 = 0;
11118 bigdisp.bitfield.disp16 = 1;
11119 }
e05278af 11120 }
e05278af 11121 }
c6fb90c8
L
11122 i.types[this_operand] = operand_type_or (i.types[this_operand],
11123 bigdisp);
252b5132
RH
11124
11125 exp = &disp_expressions[i.disp_operands];
520dc8e8 11126 i.op[this_operand].disps = exp;
252b5132
RH
11127 i.disp_operands++;
11128 save_input_line_pointer = input_line_pointer;
11129 input_line_pointer = disp_start;
11130 END_STRING_AND_SAVE (disp_end);
11131
11132#ifndef GCC_ASM_O_HACK
11133#define GCC_ASM_O_HACK 0
11134#endif
11135#if GCC_ASM_O_HACK
11136 END_STRING_AND_SAVE (disp_end + 1);
40fb9820 11137 if (i.types[this_operand].bitfield.baseIndex
24eab124 11138 && displacement_string_end[-1] == '+')
252b5132
RH
11139 {
11140 /* This hack is to avoid a warning when using the "o"
24eab124
AM
11141 constraint within gcc asm statements.
11142 For instance:
11143
11144 #define _set_tssldt_desc(n,addr,limit,type) \
11145 __asm__ __volatile__ ( \
11146 "movw %w2,%0\n\t" \
11147 "movw %w1,2+%0\n\t" \
11148 "rorl $16,%1\n\t" \
11149 "movb %b1,4+%0\n\t" \
11150 "movb %4,5+%0\n\t" \
11151 "movb $0,6+%0\n\t" \
11152 "movb %h1,7+%0\n\t" \
11153 "rorl $16,%1" \
11154 : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
11155
11156 This works great except that the output assembler ends
11157 up looking a bit weird if it turns out that there is
11158 no offset. You end up producing code that looks like:
11159
11160 #APP
11161 movw $235,(%eax)
11162 movw %dx,2+(%eax)
11163 rorl $16,%edx
11164 movb %dl,4+(%eax)
11165 movb $137,5+(%eax)
11166 movb $0,6+(%eax)
11167 movb %dh,7+(%eax)
11168 rorl $16,%edx
11169 #NO_APP
11170
47926f60 11171 So here we provide the missing zero. */
24eab124
AM
11172
11173 *displacement_string_end = '0';
252b5132
RH
11174 }
11175#endif
d258b828 11176 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
11177 if (gotfree_input_line)
11178 input_line_pointer = gotfree_input_line;
252b5132 11179
24eab124 11180 exp_seg = expression (exp);
252b5132 11181
636c26b0
AM
11182 SKIP_WHITESPACE ();
11183 if (*input_line_pointer)
11184 as_bad (_("junk `%s' after expression"), input_line_pointer);
11185#if GCC_ASM_O_HACK
11186 RESTORE_END_STRING (disp_end + 1);
11187#endif
636c26b0 11188 input_line_pointer = save_input_line_pointer;
636c26b0 11189 if (gotfree_input_line)
ee86248c
JB
11190 {
11191 free (gotfree_input_line);
11192
11193 if (exp->X_op == O_constant || exp->X_op == O_register)
11194 exp->X_op = O_illegal;
11195 }
11196
11197 ret = i386_finalize_displacement (exp_seg, exp, types, disp_start);
11198
11199 RESTORE_END_STRING (disp_end);
11200
11201 return ret;
11202}
11203
11204static int
11205i386_finalize_displacement (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
11206 i386_operand_type types, const char *disp_start)
11207{
ee86248c 11208 int ret = 1;
636c26b0 11209
24eab124
AM
11210 /* We do this to make sure that the section symbol is in
11211 the symbol table. We will ultimately change the relocation
47926f60 11212 to be relative to the beginning of the section. */
1ae12ab7 11213 if (i.reloc[this_operand] == BFD_RELOC_386_GOTOFF
d6ab8113
JB
11214 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
11215 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
24eab124 11216 {
636c26b0 11217 if (exp->X_op != O_symbol)
3992d3b7 11218 goto inv_disp;
636c26b0 11219
e5cb08ac 11220 if (S_IS_LOCAL (exp->X_add_symbol)
c64efb4b
L
11221 && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section
11222 && S_GET_SEGMENT (exp->X_add_symbol) != expr_section)
24eab124 11223 section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
24eab124
AM
11224 exp->X_op = O_subtract;
11225 exp->X_op_symbol = GOT_symbol;
1ae12ab7 11226 if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
29b0f896 11227 i.reloc[this_operand] = BFD_RELOC_32_PCREL;
d6ab8113
JB
11228 else if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
11229 i.reloc[this_operand] = BFD_RELOC_64;
23df1078 11230 else
29b0f896 11231 i.reloc[this_operand] = BFD_RELOC_32;
24eab124 11232 }
252b5132 11233
3992d3b7
AM
11234 else if (exp->X_op == O_absent
11235 || exp->X_op == O_illegal
ee86248c 11236 || exp->X_op == O_big)
2daf4fd8 11237 {
3992d3b7
AM
11238 inv_disp:
11239 as_bad (_("missing or invalid displacement expression `%s'"),
2daf4fd8 11240 disp_start);
3992d3b7 11241 ret = 0;
2daf4fd8
AM
11242 }
11243
a50187b2
JB
11244 else if (exp->X_op == O_constant)
11245 {
11246 /* Sizing gets taken care of by optimize_disp().
11247
11248 If not 64bit, sign/zero extend val, to account for wraparound
11249 when !BFD64. */
11250 if (flag_code != CODE_64BIT)
11251 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
11252 }
11253
4c63da97 11254#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
a50187b2 11255 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
3992d3b7
AM
11256 && exp_seg != absolute_section
11257 && exp_seg != text_section
11258 && exp_seg != data_section
11259 && exp_seg != bss_section
11260 && exp_seg != undefined_section
11261 && !bfd_is_com_section (exp_seg))
24eab124 11262 {
d0b47220 11263 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
3992d3b7 11264 ret = 0;
24eab124 11265 }
252b5132 11266#endif
3956db08 11267
a50187b2 11268 else if (current_templates->start->opcode_modifier.jump == JUMP_BYTE)
48bcea9f
JB
11269 i.types[this_operand].bitfield.disp8 = 1;
11270
40fb9820 11271 /* Check if this is a displacement only operand. */
02b83698
JB
11272 if (!i.types[this_operand].bitfield.baseindex)
11273 i.types[this_operand] =
11274 operand_type_or (operand_type_and_not (i.types[this_operand], anydisp),
11275 operand_type_and (i.types[this_operand], types));
3956db08 11276
3992d3b7 11277 return ret;
252b5132
RH
11278}
11279
2abc2bec
JB
11280/* Return the active addressing mode, taking address override and
11281 registers forming the address into consideration. Update the
11282 address override prefix if necessary. */
47926f60 11283
2abc2bec
JB
11284static enum flag_code
11285i386_addressing_mode (void)
252b5132 11286{
be05d201
L
11287 enum flag_code addr_mode;
11288
11289 if (i.prefix[ADDR_PREFIX])
11290 addr_mode = flag_code == CODE_32BIT ? CODE_16BIT : CODE_32BIT;
a23b33b3
JB
11291 else if (flag_code == CODE_16BIT
11292 && current_templates->start->cpu_flags.bitfield.cpumpx
11293 /* Avoid replacing the "16-bit addressing not allowed" diagnostic
11294 from md_assemble() by "is not a valid base/index expression"
11295 when there is a base and/or index. */
11296 && !i.types[this_operand].bitfield.baseindex)
11297 {
11298 /* MPX insn memory operands with neither base nor index must be forced
11299 to use 32-bit addressing in 16-bit mode. */
11300 addr_mode = CODE_32BIT;
11301 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
11302 ++i.prefixes;
11303 gas_assert (!i.types[this_operand].bitfield.disp16);
11304 gas_assert (!i.types[this_operand].bitfield.disp32);
11305 }
be05d201
L
11306 else
11307 {
11308 addr_mode = flag_code;
11309
24eab124 11310#if INFER_ADDR_PREFIX
be05d201
L
11311 if (i.mem_operands == 0)
11312 {
11313 /* Infer address prefix from the first memory operand. */
11314 const reg_entry *addr_reg = i.base_reg;
11315
11316 if (addr_reg == NULL)
11317 addr_reg = i.index_reg;
eecb386c 11318
be05d201
L
11319 if (addr_reg)
11320 {
e968fc9b 11321 if (addr_reg->reg_type.bitfield.dword)
be05d201
L
11322 addr_mode = CODE_32BIT;
11323 else if (flag_code != CODE_64BIT
dc821c5f 11324 && addr_reg->reg_type.bitfield.word)
be05d201
L
11325 addr_mode = CODE_16BIT;
11326
11327 if (addr_mode != flag_code)
11328 {
11329 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
11330 i.prefixes += 1;
11331 /* Change the size of any displacement too. At most one
11332 of Disp16 or Disp32 is set.
11333 FIXME. There doesn't seem to be any real need for
11334 separate Disp16 and Disp32 flags. The same goes for
11335 Imm16 and Imm32. Removing them would probably clean
11336 up the code quite a lot. */
11337 if (flag_code != CODE_64BIT
11338 && (i.types[this_operand].bitfield.disp16
11339 || i.types[this_operand].bitfield.disp32))
05909f23
JB
11340 {
11341 static const i386_operand_type disp16_32 = {
11342 .bitfield = { .disp16 = 1, .disp32 = 1 }
11343 };
11344
11345 i.types[this_operand]
11346 = operand_type_xor (i.types[this_operand], disp16_32);
11347 }
be05d201
L
11348 }
11349 }
11350 }
24eab124 11351#endif
be05d201
L
11352 }
11353
2abc2bec
JB
11354 return addr_mode;
11355}
11356
11357/* Make sure the memory operand we've been dealt is valid.
11358 Return 1 on success, 0 on a failure. */
11359
11360static int
11361i386_index_check (const char *operand_string)
11362{
11363 const char *kind = "base/index";
11364 enum flag_code addr_mode = i386_addressing_mode ();
77a19f0e 11365 const insn_template *t = current_templates->end - 1;
2abc2bec 11366
77a19f0e 11367 if (t->opcode_modifier.isstring)
fc0763e6
JB
11368 {
11369 /* Memory operands of string insns are special in that they only allow
11370 a single register (rDI, rSI, or rBX) as their memory address. */
be05d201
L
11371 const reg_entry *expected_reg;
11372 static const char *di_si[][2] =
11373 {
11374 { "esi", "edi" },
11375 { "si", "di" },
11376 { "rsi", "rdi" }
11377 };
11378 static const char *bx[] = { "ebx", "bx", "rbx" };
fc0763e6
JB
11379
11380 kind = "string address";
11381
a152332d 11382 if (t->opcode_modifier.prefixok == PrefixRep)
fc0763e6 11383 {
77a19f0e 11384 int es_op = t->opcode_modifier.isstring - IS_STRING_ES_OP0;
51c8edf6 11385 int op = 0;
fc0763e6 11386
77a19f0e 11387 if (!t->operand_types[0].bitfield.baseindex
fc0763e6 11388 || ((!i.mem_operands != !intel_syntax)
77a19f0e 11389 && t->operand_types[1].bitfield.baseindex))
51c8edf6 11390 op = 1;
fe0e921f
AM
11391 expected_reg
11392 = (const reg_entry *) str_hash_find (reg_hash,
11393 di_si[addr_mode][op == es_op]);
fc0763e6
JB
11394 }
11395 else
fe0e921f
AM
11396 expected_reg
11397 = (const reg_entry *)str_hash_find (reg_hash, bx[addr_mode]);
fc0763e6 11398
be05d201
L
11399 if (i.base_reg != expected_reg
11400 || i.index_reg
fc0763e6 11401 || operand_type_check (i.types[this_operand], disp))
fc0763e6 11402 {
be05d201
L
11403 /* The second memory operand must have the same size as
11404 the first one. */
11405 if (i.mem_operands
11406 && i.base_reg
11407 && !((addr_mode == CODE_64BIT
dc821c5f 11408 && i.base_reg->reg_type.bitfield.qword)
be05d201 11409 || (addr_mode == CODE_32BIT
dc821c5f
JB
11410 ? i.base_reg->reg_type.bitfield.dword
11411 : i.base_reg->reg_type.bitfield.word)))
be05d201
L
11412 goto bad_address;
11413
fc0763e6
JB
11414 as_warn (_("`%s' is not valid here (expected `%c%s%s%c')"),
11415 operand_string,
11416 intel_syntax ? '[' : '(',
11417 register_prefix,
be05d201 11418 expected_reg->reg_name,
fc0763e6 11419 intel_syntax ? ']' : ')');
be05d201 11420 return 1;
fc0763e6 11421 }
be05d201
L
11422 else
11423 return 1;
11424
dc1e8a47 11425 bad_address:
be05d201
L
11426 as_bad (_("`%s' is not a valid %s expression"),
11427 operand_string, kind);
11428 return 0;
3e73aa7c
JH
11429 }
11430 else
11431 {
77a19f0e
JB
11432 t = current_templates->start;
11433
be05d201
L
11434 if (addr_mode != CODE_16BIT)
11435 {
11436 /* 32-bit/64-bit checks. */
41eb8e88
L
11437 if (i.disp_encoding == disp_encoding_16bit)
11438 {
11439 bad_disp:
11440 as_bad (_("invalid `%s' prefix"),
11441 addr_mode == CODE_16BIT ? "{disp32}" : "{disp16}");
11442 return 0;
11443 }
11444
be05d201 11445 if ((i.base_reg
e968fc9b
JB
11446 && ((addr_mode == CODE_64BIT
11447 ? !i.base_reg->reg_type.bitfield.qword
11448 : !i.base_reg->reg_type.bitfield.dword)
11449 || (i.index_reg && i.base_reg->reg_num == RegIP)
11450 || i.base_reg->reg_num == RegIZ))
be05d201 11451 || (i.index_reg
1b54b8d7
JB
11452 && !i.index_reg->reg_type.bitfield.xmmword
11453 && !i.index_reg->reg_type.bitfield.ymmword
11454 && !i.index_reg->reg_type.bitfield.zmmword
be05d201 11455 && ((addr_mode == CODE_64BIT
e968fc9b
JB
11456 ? !i.index_reg->reg_type.bitfield.qword
11457 : !i.index_reg->reg_type.bitfield.dword)
be05d201
L
11458 || !i.index_reg->reg_type.bitfield.baseindex)))
11459 goto bad_address;
8178be5b 11460
260cd341 11461 /* bndmk, bndldx, bndstx and mandatory non-vector SIB have special restrictions. */
7fc69528
JB
11462 if (t->mnem_off == MN_bndmk
11463 || t->mnem_off == MN_bndldx
11464 || t->mnem_off == MN_bndstx
a152332d 11465 || t->opcode_modifier.sib == SIBMEM)
8178be5b
JB
11466 {
11467 /* They cannot use RIP-relative addressing. */
e968fc9b 11468 if (i.base_reg && i.base_reg->reg_num == RegIP)
8178be5b
JB
11469 {
11470 as_bad (_("`%s' cannot be used here"), operand_string);
11471 return 0;
11472 }
11473
11474 /* bndldx and bndstx ignore their scale factor. */
7fc69528 11475 if ((t->mnem_off == MN_bndldx || t->mnem_off == MN_bndstx)
8178be5b
JB
11476 && i.log2_scale_factor)
11477 as_warn (_("register scaling is being ignored here"));
11478 }
be05d201
L
11479 }
11480 else
3e73aa7c 11481 {
be05d201 11482 /* 16-bit checks. */
41eb8e88
L
11483 if (i.disp_encoding == disp_encoding_32bit)
11484 goto bad_disp;
11485
3e73aa7c 11486 if ((i.base_reg
dc821c5f 11487 && (!i.base_reg->reg_type.bitfield.word
40fb9820 11488 || !i.base_reg->reg_type.bitfield.baseindex))
3e73aa7c 11489 || (i.index_reg
dc821c5f 11490 && (!i.index_reg->reg_type.bitfield.word
40fb9820 11491 || !i.index_reg->reg_type.bitfield.baseindex
29b0f896
AM
11492 || !(i.base_reg
11493 && i.base_reg->reg_num < 6
11494 && i.index_reg->reg_num >= 6
11495 && i.log2_scale_factor == 0))))
be05d201 11496 goto bad_address;
3e73aa7c
JH
11497 }
11498 }
be05d201 11499 return 1;
24eab124 11500}
252b5132 11501
43234a1e
L
11502/* Handle vector immediates. */
11503
11504static int
11505RC_SAE_immediate (const char *imm_start)
11506{
43234a1e 11507 const char *pstr = imm_start;
43234a1e
L
11508
11509 if (*pstr != '{')
11510 return 0;
11511
7063667e
JB
11512 pstr = RC_SAE_specifier (pstr + 1);
11513 if (pstr == NULL)
43234a1e
L
11514 return 0;
11515
11516 if (*pstr++ != '}')
11517 {
11518 as_bad (_("Missing '}': '%s'"), imm_start);
11519 return 0;
11520 }
11521 /* RC/SAE immediate string should contain nothing more. */;
11522 if (*pstr != 0)
11523 {
11524 as_bad (_("Junk after '}': '%s'"), imm_start);
11525 return 0;
11526 }
11527
cf665fee
JB
11528 /* Internally this doesn't count as an operand. */
11529 --i.operands;
43234a1e 11530
43234a1e
L
11531 return 1;
11532}
11533
9d299bea
JB
11534static INLINE bool starts_memory_operand (char c)
11535{
014fbcda 11536 return ISDIGIT (c)
4795cd4a 11537 || is_name_beginner (c)
014fbcda 11538 || strchr ("([\"+-!~", c);
9d299bea
JB
11539}
11540
fc0763e6 11541/* Parse OPERAND_STRING into the i386_insn structure I. Returns zero
47926f60 11542 on error. */
252b5132 11543
252b5132 11544static int
a7619375 11545i386_att_operand (char *operand_string)
252b5132 11546{
af6bdddf
AM
11547 const reg_entry *r;
11548 char *end_op;
24eab124 11549 char *op_string = operand_string;
252b5132 11550
24eab124 11551 if (is_space_char (*op_string))
252b5132
RH
11552 ++op_string;
11553
24eab124 11554 /* We check for an absolute prefix (differentiating,
47926f60 11555 for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
ccb05c9c
JB
11556 if (*op_string == ABSOLUTE_PREFIX
11557 && current_templates->start->opcode_modifier.jump)
24eab124
AM
11558 {
11559 ++op_string;
11560 if (is_space_char (*op_string))
11561 ++op_string;
5b7c81bd 11562 i.jumpabsolute = true;
24eab124 11563 }
252b5132 11564
47926f60 11565 /* Check if operand is a register. */
4d1bb795 11566 if ((r = parse_register (op_string, &end_op)) != NULL)
24eab124 11567 {
40fb9820
L
11568 i386_operand_type temp;
11569
8a6fb3f9
JB
11570 if (r == &bad_reg)
11571 return 0;
11572
24eab124
AM
11573 /* Check for a segment override by searching for ':' after a
11574 segment register. */
11575 op_string = end_op;
11576 if (is_space_char (*op_string))
11577 ++op_string;
00cee14f 11578 if (*op_string == ':' && r->reg_type.bitfield.class == SReg)
24eab124 11579 {
5e042380 11580 i.seg[i.mem_operands] = r;
252b5132 11581
24eab124 11582 /* Skip the ':' and whitespace. */
252b5132
RH
11583 ++op_string;
11584 if (is_space_char (*op_string))
24eab124 11585 ++op_string;
252b5132 11586
47926f60 11587 /* Handle case of %es:*foo. */
ccb05c9c
JB
11588 if (!i.jumpabsolute && *op_string == ABSOLUTE_PREFIX
11589 && current_templates->start->opcode_modifier.jump)
24eab124
AM
11590 {
11591 ++op_string;
11592 if (is_space_char (*op_string))
11593 ++op_string;
5b7c81bd 11594 i.jumpabsolute = true;
24eab124 11595 }
c8d541e2 11596
9d299bea 11597 if (!starts_memory_operand (*op_string))
c8d541e2
JB
11598 {
11599 as_bad (_("bad memory operand `%s'"), op_string);
11600 return 0;
11601 }
24eab124
AM
11602 goto do_memory_reference;
11603 }
43234a1e
L
11604
11605 /* Handle vector operations. */
11606 if (*op_string == '{')
11607 {
f70c6814 11608 op_string = check_VecOperations (op_string);
43234a1e
L
11609 if (op_string == NULL)
11610 return 0;
11611 }
11612
24eab124
AM
11613 if (*op_string)
11614 {
d0b47220 11615 as_bad (_("junk `%s' after register"), op_string);
24eab124
AM
11616 return 0;
11617 }
40fb9820
L
11618 temp = r->reg_type;
11619 temp.bitfield.baseindex = 0;
c6fb90c8
L
11620 i.types[this_operand] = operand_type_or (i.types[this_operand],
11621 temp);
7d5e4556 11622 i.types[this_operand].bitfield.unspecified = 0;
520dc8e8 11623 i.op[this_operand].regs = r;
24eab124 11624 i.reg_operands++;
cf665fee
JB
11625
11626 /* A GPR may follow an RC or SAE immediate only if a (vector) register
11627 operand was also present earlier on. */
11628 if (i.rounding.type != rc_none && temp.bitfield.class == Reg
11629 && i.reg_operands == 1)
11630 {
11631 unsigned int j;
11632
11633 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); ++j)
11634 if (i.rounding.type == RC_NamesTable[j].type)
11635 break;
11636 as_bad (_("`%s': misplaced `{%s}'"),
76d3f746 11637 insn_name (current_templates->start), RC_NamesTable[j].name);
cf665fee
JB
11638 return 0;
11639 }
24eab124 11640 }
af6bdddf
AM
11641 else if (*op_string == REGISTER_PREFIX)
11642 {
11643 as_bad (_("bad register name `%s'"), op_string);
11644 return 0;
11645 }
24eab124 11646 else if (*op_string == IMMEDIATE_PREFIX)
ce8a8b2f 11647 {
24eab124 11648 ++op_string;
6f2f06be 11649 if (i.jumpabsolute)
24eab124 11650 {
d0b47220 11651 as_bad (_("immediate operand illegal with absolute jump"));
24eab124
AM
11652 return 0;
11653 }
11654 if (!i386_immediate (op_string))
11655 return 0;
cf665fee
JB
11656 if (i.rounding.type != rc_none)
11657 {
11658 as_bad (_("`%s': RC/SAE operand must follow immediate operands"),
76d3f746 11659 insn_name (current_templates->start));
cf665fee
JB
11660 return 0;
11661 }
24eab124 11662 }
43234a1e
L
11663 else if (RC_SAE_immediate (operand_string))
11664 {
cf665fee
JB
11665 /* If it is a RC or SAE immediate, do the necessary placement check:
11666 Only another immediate or a GPR may precede it. */
11667 if (i.mem_operands || i.reg_operands + i.imm_operands > 1
11668 || (i.reg_operands == 1
11669 && i.op[0].regs->reg_type.bitfield.class != Reg))
11670 {
11671 as_bad (_("`%s': misplaced `%s'"),
76d3f746 11672 insn_name (current_templates->start), operand_string);
cf665fee
JB
11673 return 0;
11674 }
43234a1e 11675 }
9d299bea 11676 else if (starts_memory_operand (*op_string))
24eab124 11677 {
47926f60 11678 /* This is a memory reference of some sort. */
af6bdddf 11679 char *base_string;
252b5132 11680
47926f60 11681 /* Start and end of displacement string expression (if found). */
eecb386c
AM
11682 char *displacement_string_start;
11683 char *displacement_string_end;
252b5132 11684
24eab124 11685 do_memory_reference:
24eab124
AM
11686 /* Check for base index form. We detect the base index form by
11687 looking for an ')' at the end of the operand, searching
11688 for the '(' matching it, and finding a REGISTER_PREFIX or ','
11689 after the '('. */
af6bdddf 11690 base_string = op_string + strlen (op_string);
c3332e24 11691
43234a1e 11692 /* Handle vector operations. */
6b5ba0d4
JB
11693 --base_string;
11694 if (is_space_char (*base_string))
11695 --base_string;
11696
11697 if (*base_string == '}')
43234a1e 11698 {
6b5ba0d4
JB
11699 char *vop_start = NULL;
11700
11701 while (base_string-- > op_string)
11702 {
11703 if (*base_string == '"')
11704 break;
11705 if (*base_string != '{')
11706 continue;
11707
11708 vop_start = base_string;
11709
11710 --base_string;
11711 if (is_space_char (*base_string))
11712 --base_string;
11713
11714 if (*base_string != '}')
11715 break;
11716
11717 vop_start = NULL;
11718 }
11719
11720 if (!vop_start)
11721 {
11722 as_bad (_("unbalanced figure braces"));
11723 return 0;
11724 }
11725
f70c6814 11726 if (check_VecOperations (vop_start) == NULL)
43234a1e 11727 return 0;
43234a1e
L
11728 }
11729
47926f60 11730 /* If we only have a displacement, set-up for it to be parsed later. */
af6bdddf
AM
11731 displacement_string_start = op_string;
11732 displacement_string_end = base_string + 1;
252b5132 11733
24eab124
AM
11734 if (*base_string == ')')
11735 {
af6bdddf 11736 char *temp_string;
e87fb6a6
JB
11737 unsigned int parens_not_balanced = 0;
11738 bool in_quotes = false;
e68c3d59 11739
24eab124 11740 /* We've already checked that the number of left & right ()'s are
e87fb6a6
JB
11741 equal, and that there's a matching set of double quotes. */
11742 end_op = base_string;
11743 for (temp_string = op_string; temp_string < end_op; temp_string++)
24eab124 11744 {
e87fb6a6
JB
11745 if (*temp_string == '\\' && temp_string[1] == '"')
11746 ++temp_string;
11747 else if (*temp_string == '"')
11748 in_quotes = !in_quotes;
11749 else if (!in_quotes)
11750 {
11751 if (*temp_string == '(' && !parens_not_balanced++)
11752 base_string = temp_string;
11753 if (*temp_string == ')')
11754 --parens_not_balanced;
11755 }
24eab124 11756 }
c3332e24 11757
af6bdddf 11758 temp_string = base_string;
c3332e24 11759
24eab124 11760 /* Skip past '(' and whitespace. */
100f993c
L
11761 if (*base_string != '(')
11762 {
11763 as_bad (_("unbalanced braces"));
11764 return 0;
11765 }
e87fb6a6 11766 ++base_string;
252b5132 11767 if (is_space_char (*base_string))
24eab124 11768 ++base_string;
252b5132 11769
af6bdddf 11770 if (*base_string == ','
4eed87de
AM
11771 || ((i.base_reg = parse_register (base_string, &end_op))
11772 != NULL))
252b5132 11773 {
af6bdddf 11774 displacement_string_end = temp_string;
252b5132 11775
40fb9820 11776 i.types[this_operand].bitfield.baseindex = 1;
252b5132 11777
af6bdddf 11778 if (i.base_reg)
24eab124 11779 {
8a6fb3f9
JB
11780 if (i.base_reg == &bad_reg)
11781 return 0;
24eab124
AM
11782 base_string = end_op;
11783 if (is_space_char (*base_string))
11784 ++base_string;
af6bdddf
AM
11785 }
11786
11787 /* There may be an index reg or scale factor here. */
11788 if (*base_string == ',')
11789 {
11790 ++base_string;
11791 if (is_space_char (*base_string))
11792 ++base_string;
11793
4eed87de
AM
11794 if ((i.index_reg = parse_register (base_string, &end_op))
11795 != NULL)
24eab124 11796 {
8a6fb3f9
JB
11797 if (i.index_reg == &bad_reg)
11798 return 0;
af6bdddf 11799 base_string = end_op;
24eab124
AM
11800 if (is_space_char (*base_string))
11801 ++base_string;
af6bdddf
AM
11802 if (*base_string == ',')
11803 {
11804 ++base_string;
11805 if (is_space_char (*base_string))
11806 ++base_string;
11807 }
e5cb08ac 11808 else if (*base_string != ')')
af6bdddf 11809 {
4eed87de
AM
11810 as_bad (_("expecting `,' or `)' "
11811 "after index register in `%s'"),
af6bdddf
AM
11812 operand_string);
11813 return 0;
11814 }
24eab124 11815 }
af6bdddf 11816 else if (*base_string == REGISTER_PREFIX)
24eab124 11817 {
f76bf5e0
L
11818 end_op = strchr (base_string, ',');
11819 if (end_op)
11820 *end_op = '\0';
af6bdddf 11821 as_bad (_("bad register name `%s'"), base_string);
24eab124
AM
11822 return 0;
11823 }
252b5132 11824
47926f60 11825 /* Check for scale factor. */
551c1ca1 11826 if (*base_string != ')')
af6bdddf 11827 {
551c1ca1
AM
11828 char *end_scale = i386_scale (base_string);
11829
11830 if (!end_scale)
af6bdddf 11831 return 0;
24eab124 11832
551c1ca1 11833 base_string = end_scale;
af6bdddf
AM
11834 if (is_space_char (*base_string))
11835 ++base_string;
11836 if (*base_string != ')')
11837 {
4eed87de
AM
11838 as_bad (_("expecting `)' "
11839 "after scale factor in `%s'"),
af6bdddf
AM
11840 operand_string);
11841 return 0;
11842 }
11843 }
11844 else if (!i.index_reg)
24eab124 11845 {
4eed87de
AM
11846 as_bad (_("expecting index register or scale factor "
11847 "after `,'; got '%c'"),
af6bdddf 11848 *base_string);
24eab124
AM
11849 return 0;
11850 }
11851 }
af6bdddf 11852 else if (*base_string != ')')
24eab124 11853 {
4eed87de
AM
11854 as_bad (_("expecting `,' or `)' "
11855 "after base register in `%s'"),
af6bdddf 11856 operand_string);
24eab124
AM
11857 return 0;
11858 }
c3332e24 11859 }
af6bdddf 11860 else if (*base_string == REGISTER_PREFIX)
c3332e24 11861 {
f76bf5e0
L
11862 end_op = strchr (base_string, ',');
11863 if (end_op)
11864 *end_op = '\0';
af6bdddf 11865 as_bad (_("bad register name `%s'"), base_string);
24eab124 11866 return 0;
c3332e24 11867 }
24eab124
AM
11868 }
11869
11870 /* If there's an expression beginning the operand, parse it,
11871 assuming displacement_string_start and
11872 displacement_string_end are meaningful. */
11873 if (displacement_string_start != displacement_string_end)
11874 {
11875 if (!i386_displacement (displacement_string_start,
11876 displacement_string_end))
11877 return 0;
11878 }
11879
11880 /* Special case for (%dx) while doing input/output op. */
11881 if (i.base_reg
75e5731b
JB
11882 && i.base_reg->reg_type.bitfield.instance == RegD
11883 && i.base_reg->reg_type.bitfield.word
24eab124
AM
11884 && i.index_reg == 0
11885 && i.log2_scale_factor == 0
11886 && i.seg[i.mem_operands] == 0
40fb9820 11887 && !operand_type_check (i.types[this_operand], disp))
24eab124 11888 {
2fb5be8d 11889 i.types[this_operand] = i.base_reg->reg_type;
9373f275 11890 i.input_output_operand = true;
24eab124
AM
11891 return 1;
11892 }
11893
eecb386c
AM
11894 if (i386_index_check (operand_string) == 0)
11895 return 0;
c48dadc9 11896 i.flags[this_operand] |= Operand_Mem;
24eab124
AM
11897 i.mem_operands++;
11898 }
11899 else
ce8a8b2f
AM
11900 {
11901 /* It's not a memory operand; argh! */
24eab124
AM
11902 as_bad (_("invalid char %s beginning operand %d `%s'"),
11903 output_invalid (*op_string),
11904 this_operand + 1,
11905 op_string);
11906 return 0;
11907 }
47926f60 11908 return 1; /* Normal return. */
252b5132
RH
11909}
11910\f
fa94de6b
RM
11911/* Calculate the maximum variable size (i.e., excluding fr_fix)
11912 that an rs_machine_dependent frag may reach. */
11913
11914unsigned int
11915i386_frag_max_var (fragS *frag)
11916{
11917 /* The only relaxable frags are for jumps.
11918 Unconditional jumps can grow by 4 bytes and others by 5 bytes. */
11919 gas_assert (frag->fr_type == rs_machine_dependent);
11920 return TYPE_FROM_RELAX_STATE (frag->fr_subtype) == UNCOND_JUMP ? 4 : 5;
11921}
11922
b084df0b
L
11923#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
11924static int
8dcea932 11925elf_symbol_resolved_in_segment_p (symbolS *fr_symbol, offsetT fr_var)
b084df0b
L
11926{
11927 /* STT_GNU_IFUNC symbol must go through PLT. */
11928 if ((symbol_get_bfdsym (fr_symbol)->flags
11929 & BSF_GNU_INDIRECT_FUNCTION) != 0)
11930 return 0;
11931
11932 if (!S_IS_EXTERNAL (fr_symbol))
11933 /* Symbol may be weak or local. */
11934 return !S_IS_WEAK (fr_symbol);
11935
8dcea932
L
11936 /* Global symbols with non-default visibility can't be preempted. */
11937 if (ELF_ST_VISIBILITY (S_GET_OTHER (fr_symbol)) != STV_DEFAULT)
11938 return 1;
11939
11940 if (fr_var != NO_RELOC)
11941 switch ((enum bfd_reloc_code_real) fr_var)
11942 {
11943 case BFD_RELOC_386_PLT32:
11944 case BFD_RELOC_X86_64_PLT32:
33eaf5de 11945 /* Symbol with PLT relocation may be preempted. */
8dcea932
L
11946 return 0;
11947 default:
11948 abort ();
11949 }
11950
b084df0b
L
11951 /* Global symbols with default visibility in a shared library may be
11952 preempted by another definition. */
8dcea932 11953 return !shared;
b084df0b
L
11954}
11955#endif
11956
79d72f45
HL
11957/* Table 3-2. Macro-Fusible Instructions in Haswell Microarchitecture
11958 Note also work for Skylake and Cascadelake.
11959---------------------------------------------------------------------
11960| JCC | ADD/SUB/CMP | INC/DEC | TEST/AND |
11961| ------ | ----------- | ------- | -------- |
11962| Jo | N | N | Y |
11963| Jno | N | N | Y |
11964| Jc/Jb | Y | N | Y |
11965| Jae/Jnb | Y | N | Y |
11966| Je/Jz | Y | Y | Y |
11967| Jne/Jnz | Y | Y | Y |
11968| Jna/Jbe | Y | N | Y |
11969| Ja/Jnbe | Y | N | Y |
11970| Js | N | N | Y |
11971| Jns | N | N | Y |
11972| Jp/Jpe | N | N | Y |
11973| Jnp/Jpo | N | N | Y |
11974| Jl/Jnge | Y | Y | Y |
11975| Jge/Jnl | Y | Y | Y |
11976| Jle/Jng | Y | Y | Y |
11977| Jg/Jnle | Y | Y | Y |
11978--------------------------------------------------------------------- */
11979static int
11980i386_macro_fusible_p (enum mf_cmp_kind mf_cmp, enum mf_jcc_kind mf_jcc)
11981{
11982 if (mf_cmp == mf_cmp_alu_cmp)
11983 return ((mf_jcc >= mf_jcc_jc && mf_jcc <= mf_jcc_jna)
11984 || mf_jcc == mf_jcc_jl || mf_jcc == mf_jcc_jle);
11985 if (mf_cmp == mf_cmp_incdec)
11986 return (mf_jcc == mf_jcc_je || mf_jcc == mf_jcc_jl
11987 || mf_jcc == mf_jcc_jle);
11988 if (mf_cmp == mf_cmp_test_and)
11989 return 1;
11990 return 0;
11991}
11992
e379e5f3
L
11993/* Return the next non-empty frag. */
11994
11995static fragS *
11996i386_next_non_empty_frag (fragS *fragP)
11997{
11998 /* There may be a frag with a ".fill 0" when there is no room in
11999 the current frag for frag_grow in output_insn. */
12000 for (fragP = fragP->fr_next;
12001 (fragP != NULL
12002 && fragP->fr_type == rs_fill
12003 && fragP->fr_fix == 0);
12004 fragP = fragP->fr_next)
12005 ;
12006 return fragP;
12007}
12008
12009/* Return the next jcc frag after BRANCH_PADDING. */
12010
12011static fragS *
79d72f45 12012i386_next_fusible_jcc_frag (fragS *maybe_cmp_fragP, fragS *pad_fragP)
e379e5f3 12013{
79d72f45
HL
12014 fragS *branch_fragP;
12015 if (!pad_fragP)
e379e5f3
L
12016 return NULL;
12017
79d72f45
HL
12018 if (pad_fragP->fr_type == rs_machine_dependent
12019 && (TYPE_FROM_RELAX_STATE (pad_fragP->fr_subtype)
e379e5f3
L
12020 == BRANCH_PADDING))
12021 {
79d72f45
HL
12022 branch_fragP = i386_next_non_empty_frag (pad_fragP);
12023 if (branch_fragP->fr_type != rs_machine_dependent)
e379e5f3 12024 return NULL;
79d72f45
HL
12025 if (TYPE_FROM_RELAX_STATE (branch_fragP->fr_subtype) == COND_JUMP
12026 && i386_macro_fusible_p (maybe_cmp_fragP->tc_frag_data.mf_type,
12027 pad_fragP->tc_frag_data.mf_type))
12028 return branch_fragP;
e379e5f3
L
12029 }
12030
12031 return NULL;
12032}
12033
12034/* Classify BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags. */
12035
12036static void
12037i386_classify_machine_dependent_frag (fragS *fragP)
12038{
12039 fragS *cmp_fragP;
12040 fragS *pad_fragP;
12041 fragS *branch_fragP;
12042 fragS *next_fragP;
12043 unsigned int max_prefix_length;
12044
12045 if (fragP->tc_frag_data.classified)
12046 return;
12047
12048 /* First scan for BRANCH_PADDING and FUSED_JCC_PADDING. Convert
12049 FUSED_JCC_PADDING and merge BRANCH_PADDING. */
12050 for (next_fragP = fragP;
12051 next_fragP != NULL;
12052 next_fragP = next_fragP->fr_next)
12053 {
12054 next_fragP->tc_frag_data.classified = 1;
12055 if (next_fragP->fr_type == rs_machine_dependent)
12056 switch (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype))
12057 {
12058 case BRANCH_PADDING:
12059 /* The BRANCH_PADDING frag must be followed by a branch
12060 frag. */
12061 branch_fragP = i386_next_non_empty_frag (next_fragP);
12062 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
12063 break;
12064 case FUSED_JCC_PADDING:
12065 /* Check if this is a fused jcc:
12066 FUSED_JCC_PADDING
12067 CMP like instruction
12068 BRANCH_PADDING
12069 COND_JUMP
12070 */
12071 cmp_fragP = i386_next_non_empty_frag (next_fragP);
12072 pad_fragP = i386_next_non_empty_frag (cmp_fragP);
79d72f45 12073 branch_fragP = i386_next_fusible_jcc_frag (next_fragP, pad_fragP);
e379e5f3
L
12074 if (branch_fragP)
12075 {
12076 /* The BRANCH_PADDING frag is merged with the
12077 FUSED_JCC_PADDING frag. */
12078 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
12079 /* CMP like instruction size. */
12080 next_fragP->tc_frag_data.cmp_size = cmp_fragP->fr_fix;
12081 frag_wane (pad_fragP);
12082 /* Skip to branch_fragP. */
12083 next_fragP = branch_fragP;
12084 }
12085 else if (next_fragP->tc_frag_data.max_prefix_length)
12086 {
12087 /* Turn FUSED_JCC_PADDING into BRANCH_PREFIX if it isn't
12088 a fused jcc. */
12089 next_fragP->fr_subtype
12090 = ENCODE_RELAX_STATE (BRANCH_PREFIX, 0);
12091 next_fragP->tc_frag_data.max_bytes
12092 = next_fragP->tc_frag_data.max_prefix_length;
12093 /* This will be updated in the BRANCH_PREFIX scan. */
12094 next_fragP->tc_frag_data.max_prefix_length = 0;
12095 }
12096 else
12097 frag_wane (next_fragP);
12098 break;
12099 }
12100 }
12101
12102 /* Stop if there is no BRANCH_PREFIX. */
12103 if (!align_branch_prefix_size)
12104 return;
12105
12106 /* Scan for BRANCH_PREFIX. */
12107 for (; fragP != NULL; fragP = fragP->fr_next)
12108 {
12109 if (fragP->fr_type != rs_machine_dependent
12110 || (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
12111 != BRANCH_PREFIX))
12112 continue;
12113
12114 /* Count all BRANCH_PREFIX frags before BRANCH_PADDING and
12115 COND_JUMP_PREFIX. */
12116 max_prefix_length = 0;
12117 for (next_fragP = fragP;
12118 next_fragP != NULL;
12119 next_fragP = next_fragP->fr_next)
12120 {
12121 if (next_fragP->fr_type == rs_fill)
12122 /* Skip rs_fill frags. */
12123 continue;
12124 else if (next_fragP->fr_type != rs_machine_dependent)
12125 /* Stop for all other frags. */
12126 break;
12127
12128 /* rs_machine_dependent frags. */
12129 if (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12130 == BRANCH_PREFIX)
12131 {
12132 /* Count BRANCH_PREFIX frags. */
12133 if (max_prefix_length >= MAX_FUSED_JCC_PADDING_SIZE)
12134 {
12135 max_prefix_length = MAX_FUSED_JCC_PADDING_SIZE;
12136 frag_wane (next_fragP);
12137 }
12138 else
12139 max_prefix_length
12140 += next_fragP->tc_frag_data.max_bytes;
12141 }
12142 else if ((TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12143 == BRANCH_PADDING)
12144 || (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12145 == FUSED_JCC_PADDING))
12146 {
12147 /* Stop at BRANCH_PADDING and FUSED_JCC_PADDING. */
12148 fragP->tc_frag_data.u.padding_fragP = next_fragP;
12149 break;
12150 }
12151 else
12152 /* Stop for other rs_machine_dependent frags. */
12153 break;
12154 }
12155
12156 fragP->tc_frag_data.max_prefix_length = max_prefix_length;
12157
12158 /* Skip to the next frag. */
12159 fragP = next_fragP;
12160 }
12161}
12162
12163/* Compute padding size for
12164
12165 FUSED_JCC_PADDING
12166 CMP like instruction
12167 BRANCH_PADDING
12168 COND_JUMP/UNCOND_JUMP
12169
12170 or
12171
12172 BRANCH_PADDING
12173 COND_JUMP/UNCOND_JUMP
12174 */
12175
12176static int
12177i386_branch_padding_size (fragS *fragP, offsetT address)
12178{
12179 unsigned int offset, size, padding_size;
12180 fragS *branch_fragP = fragP->tc_frag_data.u.branch_fragP;
12181
12182 /* The start address of the BRANCH_PADDING or FUSED_JCC_PADDING frag. */
12183 if (!address)
12184 address = fragP->fr_address;
12185 address += fragP->fr_fix;
12186
12187 /* CMP like instrunction size. */
12188 size = fragP->tc_frag_data.cmp_size;
12189
12190 /* The base size of the branch frag. */
12191 size += branch_fragP->fr_fix;
12192
12193 /* Add opcode and displacement bytes for the rs_machine_dependent
12194 branch frag. */
12195 if (branch_fragP->fr_type == rs_machine_dependent)
12196 size += md_relax_table[branch_fragP->fr_subtype].rlx_length;
12197
12198 /* Check if branch is within boundary and doesn't end at the last
12199 byte. */
12200 offset = address & ((1U << align_branch_power) - 1);
12201 if ((offset + size) >= (1U << align_branch_power))
12202 /* Padding needed to avoid crossing boundary. */
12203 padding_size = (1U << align_branch_power) - offset;
12204 else
12205 /* No padding needed. */
12206 padding_size = 0;
12207
12208 /* The return value may be saved in tc_frag_data.length which is
12209 unsigned byte. */
12210 if (!fits_in_unsigned_byte (padding_size))
12211 abort ();
12212
12213 return padding_size;
12214}
12215
12216/* i386_generic_table_relax_frag()
12217
12218 Handle BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags to
12219 grow/shrink padding to align branch frags. Hand others to
12220 relax_frag(). */
12221
12222long
12223i386_generic_table_relax_frag (segT segment, fragS *fragP, long stretch)
12224{
12225 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12226 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
12227 {
12228 long padding_size = i386_branch_padding_size (fragP, 0);
12229 long grow = padding_size - fragP->tc_frag_data.length;
12230
12231 /* When the BRANCH_PREFIX frag is used, the computed address
12232 must match the actual address and there should be no padding. */
12233 if (fragP->tc_frag_data.padding_address
12234 && (fragP->tc_frag_data.padding_address != fragP->fr_address
12235 || padding_size))
12236 abort ();
12237
12238 /* Update the padding size. */
12239 if (grow)
12240 fragP->tc_frag_data.length = padding_size;
12241
12242 return grow;
12243 }
12244 else if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12245 {
12246 fragS *padding_fragP, *next_fragP;
12247 long padding_size, left_size, last_size;
12248
12249 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
12250 if (!padding_fragP)
12251 /* Use the padding set by the leading BRANCH_PREFIX frag. */
12252 return (fragP->tc_frag_data.length
12253 - fragP->tc_frag_data.last_length);
12254
12255 /* Compute the relative address of the padding frag in the very
12256 first time where the BRANCH_PREFIX frag sizes are zero. */
12257 if (!fragP->tc_frag_data.padding_address)
12258 fragP->tc_frag_data.padding_address
12259 = padding_fragP->fr_address - (fragP->fr_address - stretch);
12260
12261 /* First update the last length from the previous interation. */
12262 left_size = fragP->tc_frag_data.prefix_length;
12263 for (next_fragP = fragP;
12264 next_fragP != padding_fragP;
12265 next_fragP = next_fragP->fr_next)
12266 if (next_fragP->fr_type == rs_machine_dependent
12267 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12268 == BRANCH_PREFIX))
12269 {
12270 if (left_size)
12271 {
12272 int max = next_fragP->tc_frag_data.max_bytes;
12273 if (max)
12274 {
12275 int size;
12276 if (max > left_size)
12277 size = left_size;
12278 else
12279 size = max;
12280 left_size -= size;
12281 next_fragP->tc_frag_data.last_length = size;
12282 }
12283 }
12284 else
12285 next_fragP->tc_frag_data.last_length = 0;
12286 }
12287
12288 /* Check the padding size for the padding frag. */
12289 padding_size = i386_branch_padding_size
12290 (padding_fragP, (fragP->fr_address
12291 + fragP->tc_frag_data.padding_address));
12292
12293 last_size = fragP->tc_frag_data.prefix_length;
12294 /* Check if there is change from the last interation. */
12295 if (padding_size == last_size)
12296 {
12297 /* Update the expected address of the padding frag. */
12298 padding_fragP->tc_frag_data.padding_address
12299 = (fragP->fr_address + padding_size
12300 + fragP->tc_frag_data.padding_address);
12301 return 0;
12302 }
12303
12304 if (padding_size > fragP->tc_frag_data.max_prefix_length)
12305 {
12306 /* No padding if there is no sufficient room. Clear the
12307 expected address of the padding frag. */
12308 padding_fragP->tc_frag_data.padding_address = 0;
12309 padding_size = 0;
12310 }
12311 else
12312 /* Store the expected address of the padding frag. */
12313 padding_fragP->tc_frag_data.padding_address
12314 = (fragP->fr_address + padding_size
12315 + fragP->tc_frag_data.padding_address);
12316
12317 fragP->tc_frag_data.prefix_length = padding_size;
12318
12319 /* Update the length for the current interation. */
12320 left_size = padding_size;
12321 for (next_fragP = fragP;
12322 next_fragP != padding_fragP;
12323 next_fragP = next_fragP->fr_next)
12324 if (next_fragP->fr_type == rs_machine_dependent
12325 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12326 == BRANCH_PREFIX))
12327 {
12328 if (left_size)
12329 {
12330 int max = next_fragP->tc_frag_data.max_bytes;
12331 if (max)
12332 {
12333 int size;
12334 if (max > left_size)
12335 size = left_size;
12336 else
12337 size = max;
12338 left_size -= size;
12339 next_fragP->tc_frag_data.length = size;
12340 }
12341 }
12342 else
12343 next_fragP->tc_frag_data.length = 0;
12344 }
12345
12346 return (fragP->tc_frag_data.length
12347 - fragP->tc_frag_data.last_length);
12348 }
12349 return relax_frag (segment, fragP, stretch);
12350}
12351
ee7fcc42
AM
12352/* md_estimate_size_before_relax()
12353
12354 Called just before relax() for rs_machine_dependent frags. The x86
12355 assembler uses these frags to handle variable size jump
12356 instructions.
12357
12358 Any symbol that is now undefined will not become defined.
12359 Return the correct fr_subtype in the frag.
12360 Return the initial "guess for variable size of frag" to caller.
12361 The guess is actually the growth beyond the fixed part. Whatever
12362 we do to grow the fixed or variable part contributes to our
12363 returned value. */
12364
252b5132 12365int
7016a5d5 12366md_estimate_size_before_relax (fragS *fragP, segT segment)
252b5132 12367{
e379e5f3
L
12368 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12369 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX
12370 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
12371 {
12372 i386_classify_machine_dependent_frag (fragP);
12373 return fragP->tc_frag_data.length;
12374 }
12375
252b5132 12376 /* We've already got fragP->fr_subtype right; all we have to do is
b98ef147
AM
12377 check for un-relaxable symbols. On an ELF system, we can't relax
12378 an externally visible symbol, because it may be overridden by a
12379 shared library. */
12380 if (S_GET_SEGMENT (fragP->fr_symbol) != segment
6d249963 12381#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12382 || (IS_ELF
8dcea932
L
12383 && !elf_symbol_resolved_in_segment_p (fragP->fr_symbol,
12384 fragP->fr_var))
fbeb56a4
DK
12385#endif
12386#if defined (OBJ_COFF) && defined (TE_PE)
7ab9ffdd 12387 || (OUTPUT_FLAVOR == bfd_target_coff_flavour
fbeb56a4 12388 && S_IS_WEAK (fragP->fr_symbol))
b98ef147
AM
12389#endif
12390 )
252b5132 12391 {
b98ef147
AM
12392 /* Symbol is undefined in this segment, or we need to keep a
12393 reloc so that weak symbols can be overridden. */
12394 int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
f86103b7 12395 enum bfd_reloc_code_real reloc_type;
ee7fcc42
AM
12396 unsigned char *opcode;
12397 int old_fr_fix;
eb19308f 12398 fixS *fixP = NULL;
f6af82bd 12399
ee7fcc42 12400 if (fragP->fr_var != NO_RELOC)
1e9cc1c2 12401 reloc_type = (enum bfd_reloc_code_real) fragP->fr_var;
b98ef147 12402 else if (size == 2)
f6af82bd 12403 reloc_type = BFD_RELOC_16_PCREL;
bd7ab16b 12404#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1ef3994a
JB
12405 else if (fragP->tc_frag_data.code64 && fragP->fr_offset == 0
12406 && need_plt32_p (fragP->fr_symbol))
bd7ab16b
L
12407 reloc_type = BFD_RELOC_X86_64_PLT32;
12408#endif
f6af82bd
AM
12409 else
12410 reloc_type = BFD_RELOC_32_PCREL;
252b5132 12411
ee7fcc42
AM
12412 old_fr_fix = fragP->fr_fix;
12413 opcode = (unsigned char *) fragP->fr_opcode;
12414
fddf5b5b 12415 switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
252b5132 12416 {
fddf5b5b
AM
12417 case UNCOND_JUMP:
12418 /* Make jmp (0xeb) a (d)word displacement jump. */
47926f60 12419 opcode[0] = 0xe9;
252b5132 12420 fragP->fr_fix += size;
eb19308f
JB
12421 fixP = fix_new (fragP, old_fr_fix, size,
12422 fragP->fr_symbol,
12423 fragP->fr_offset, 1,
12424 reloc_type);
252b5132
RH
12425 break;
12426
fddf5b5b 12427 case COND_JUMP86:
412167cb
AM
12428 if (size == 2
12429 && (!no_cond_jump_promotion || fragP->fr_var != NO_RELOC))
fddf5b5b
AM
12430 {
12431 /* Negate the condition, and branch past an
12432 unconditional jump. */
12433 opcode[0] ^= 1;
12434 opcode[1] = 3;
12435 /* Insert an unconditional jump. */
12436 opcode[2] = 0xe9;
12437 /* We added two extra opcode bytes, and have a two byte
12438 offset. */
12439 fragP->fr_fix += 2 + 2;
062cd5e7
AS
12440 fix_new (fragP, old_fr_fix + 2, 2,
12441 fragP->fr_symbol,
12442 fragP->fr_offset, 1,
12443 reloc_type);
fddf5b5b
AM
12444 break;
12445 }
12446 /* Fall through. */
12447
12448 case COND_JUMP:
412167cb
AM
12449 if (no_cond_jump_promotion && fragP->fr_var == NO_RELOC)
12450 {
12451 fragP->fr_fix += 1;
3e02c1cc
AM
12452 fixP = fix_new (fragP, old_fr_fix, 1,
12453 fragP->fr_symbol,
12454 fragP->fr_offset, 1,
12455 BFD_RELOC_8_PCREL);
12456 fixP->fx_signed = 1;
412167cb
AM
12457 break;
12458 }
93c2a809 12459
24eab124 12460 /* This changes the byte-displacement jump 0x7N
fddf5b5b 12461 to the (d)word-displacement jump 0x0f,0x8N. */
252b5132 12462 opcode[1] = opcode[0] + 0x10;
f6af82bd 12463 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
47926f60
KH
12464 /* We've added an opcode byte. */
12465 fragP->fr_fix += 1 + size;
eb19308f
JB
12466 fixP = fix_new (fragP, old_fr_fix + 1, size,
12467 fragP->fr_symbol,
12468 fragP->fr_offset, 1,
12469 reloc_type);
252b5132 12470 break;
fddf5b5b
AM
12471
12472 default:
12473 BAD_CASE (fragP->fr_subtype);
12474 break;
252b5132 12475 }
eb19308f
JB
12476
12477 /* All jumps handled here are signed, but don't unconditionally use a
12478 signed limit check for 32 and 16 bit jumps as we want to allow wrap
12479 around at 4G (outside of 64-bit mode) and 64k. */
12480 if (size == 4 && flag_code == CODE_64BIT)
12481 fixP->fx_signed = 1;
12482
252b5132 12483 frag_wane (fragP);
ee7fcc42 12484 return fragP->fr_fix - old_fr_fix;
252b5132 12485 }
93c2a809 12486
93c2a809
AM
12487 /* Guess size depending on current relax state. Initially the relax
12488 state will correspond to a short jump and we return 1, because
12489 the variable part of the frag (the branch offset) is one byte
12490 long. However, we can relax a section more than once and in that
12491 case we must either set fr_subtype back to the unrelaxed state,
12492 or return the value for the appropriate branch. */
12493 return md_relax_table[fragP->fr_subtype].rlx_length;
ee7fcc42
AM
12494}
12495
47926f60
KH
12496/* Called after relax() is finished.
12497
12498 In: Address of frag.
12499 fr_type == rs_machine_dependent.
12500 fr_subtype is what the address relaxed to.
12501
12502 Out: Any fixSs and constants are set up.
12503 Caller will turn frag into a ".space 0". */
12504
252b5132 12505void
7016a5d5
TG
12506md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec ATTRIBUTE_UNUSED,
12507 fragS *fragP)
252b5132 12508{
29b0f896 12509 unsigned char *opcode;
252b5132 12510 unsigned char *where_to_put_displacement = NULL;
847f7ad4
AM
12511 offsetT target_address;
12512 offsetT opcode_address;
252b5132 12513 unsigned int extension = 0;
847f7ad4 12514 offsetT displacement_from_opcode_start;
252b5132 12515
e379e5f3
L
12516 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12517 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING
12518 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12519 {
12520 /* Generate nop padding. */
12521 unsigned int size = fragP->tc_frag_data.length;
12522 if (size)
12523 {
12524 if (size > fragP->tc_frag_data.max_bytes)
12525 abort ();
12526
12527 if (flag_debug)
12528 {
12529 const char *msg;
12530 const char *branch = "branch";
12531 const char *prefix = "";
12532 fragS *padding_fragP;
12533 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
12534 == BRANCH_PREFIX)
12535 {
12536 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
12537 switch (fragP->tc_frag_data.default_prefix)
12538 {
12539 default:
12540 abort ();
12541 break;
12542 case CS_PREFIX_OPCODE:
12543 prefix = " cs";
12544 break;
12545 case DS_PREFIX_OPCODE:
12546 prefix = " ds";
12547 break;
12548 case ES_PREFIX_OPCODE:
12549 prefix = " es";
12550 break;
12551 case FS_PREFIX_OPCODE:
12552 prefix = " fs";
12553 break;
12554 case GS_PREFIX_OPCODE:
12555 prefix = " gs";
12556 break;
12557 case SS_PREFIX_OPCODE:
12558 prefix = " ss";
12559 break;
12560 }
12561 if (padding_fragP)
12562 msg = _("%s:%u: add %d%s at 0x%llx to align "
12563 "%s within %d-byte boundary\n");
12564 else
12565 msg = _("%s:%u: add additional %d%s at 0x%llx to "
12566 "align %s within %d-byte boundary\n");
12567 }
12568 else
12569 {
12570 padding_fragP = fragP;
12571 msg = _("%s:%u: add %d%s-byte nop at 0x%llx to align "
12572 "%s within %d-byte boundary\n");
12573 }
12574
12575 if (padding_fragP)
12576 switch (padding_fragP->tc_frag_data.branch_type)
12577 {
12578 case align_branch_jcc:
12579 branch = "jcc";
12580 break;
12581 case align_branch_fused:
12582 branch = "fused jcc";
12583 break;
12584 case align_branch_jmp:
12585 branch = "jmp";
12586 break;
12587 case align_branch_call:
12588 branch = "call";
12589 break;
12590 case align_branch_indirect:
12591 branch = "indiret branch";
12592 break;
12593 case align_branch_ret:
12594 branch = "ret";
12595 break;
12596 default:
12597 break;
12598 }
12599
12600 fprintf (stdout, msg,
12601 fragP->fr_file, fragP->fr_line, size, prefix,
12602 (long long) fragP->fr_address, branch,
12603 1 << align_branch_power);
12604 }
12605 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12606 memset (fragP->fr_opcode,
12607 fragP->tc_frag_data.default_prefix, size);
12608 else
12609 i386_generate_nops (fragP, (char *) fragP->fr_opcode,
12610 size, 0);
12611 fragP->fr_fix += size;
12612 }
12613 return;
12614 }
12615
252b5132
RH
12616 opcode = (unsigned char *) fragP->fr_opcode;
12617
47926f60 12618 /* Address we want to reach in file space. */
252b5132 12619 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
252b5132 12620
47926f60 12621 /* Address opcode resides at in file space. */
252b5132
RH
12622 opcode_address = fragP->fr_address + fragP->fr_fix;
12623
47926f60 12624 /* Displacement from opcode start to fill into instruction. */
252b5132
RH
12625 displacement_from_opcode_start = target_address - opcode_address;
12626
fddf5b5b 12627 if ((fragP->fr_subtype & BIG) == 0)
252b5132 12628 {
47926f60
KH
12629 /* Don't have to change opcode. */
12630 extension = 1; /* 1 opcode + 1 displacement */
252b5132 12631 where_to_put_displacement = &opcode[1];
fddf5b5b
AM
12632 }
12633 else
12634 {
12635 if (no_cond_jump_promotion
12636 && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
4eed87de
AM
12637 as_warn_where (fragP->fr_file, fragP->fr_line,
12638 _("long jump required"));
252b5132 12639
fddf5b5b
AM
12640 switch (fragP->fr_subtype)
12641 {
12642 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
12643 extension = 4; /* 1 opcode + 4 displacement */
12644 opcode[0] = 0xe9;
12645 where_to_put_displacement = &opcode[1];
12646 break;
252b5132 12647
fddf5b5b
AM
12648 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
12649 extension = 2; /* 1 opcode + 2 displacement */
12650 opcode[0] = 0xe9;
12651 where_to_put_displacement = &opcode[1];
12652 break;
252b5132 12653
fddf5b5b
AM
12654 case ENCODE_RELAX_STATE (COND_JUMP, BIG):
12655 case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
12656 extension = 5; /* 2 opcode + 4 displacement */
12657 opcode[1] = opcode[0] + 0x10;
12658 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12659 where_to_put_displacement = &opcode[2];
12660 break;
252b5132 12661
fddf5b5b
AM
12662 case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
12663 extension = 3; /* 2 opcode + 2 displacement */
12664 opcode[1] = opcode[0] + 0x10;
12665 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12666 where_to_put_displacement = &opcode[2];
12667 break;
252b5132 12668
fddf5b5b
AM
12669 case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
12670 extension = 4;
12671 opcode[0] ^= 1;
12672 opcode[1] = 3;
12673 opcode[2] = 0xe9;
12674 where_to_put_displacement = &opcode[3];
12675 break;
12676
12677 default:
12678 BAD_CASE (fragP->fr_subtype);
12679 break;
12680 }
252b5132 12681 }
fddf5b5b 12682
7b81dfbb
AJ
12683 /* If size if less then four we are sure that the operand fits,
12684 but if it's 4, then it could be that the displacement is larger
12685 then -/+ 2GB. */
12686 if (DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype) == 4
12687 && object_64bit
12688 && ((addressT) (displacement_from_opcode_start - extension
4eed87de
AM
12689 + ((addressT) 1 << 31))
12690 > (((addressT) 2 << 31) - 1)))
7b81dfbb
AJ
12691 {
12692 as_bad_where (fragP->fr_file, fragP->fr_line,
12693 _("jump target out of range"));
12694 /* Make us emit 0. */
12695 displacement_from_opcode_start = extension;
12696 }
47926f60 12697 /* Now put displacement after opcode. */
252b5132
RH
12698 md_number_to_chars ((char *) where_to_put_displacement,
12699 (valueT) (displacement_from_opcode_start - extension),
fddf5b5b 12700 DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
252b5132
RH
12701 fragP->fr_fix += extension;
12702}
12703\f
7016a5d5 12704/* Apply a fixup (fixP) to segment data, once it has been determined
252b5132
RH
12705 by our caller that we have all the info we need to fix it up.
12706
7016a5d5
TG
12707 Parameter valP is the pointer to the value of the bits.
12708
252b5132
RH
12709 On the 386, immediates, displacements, and data pointers are all in
12710 the same (little-endian) format, so we don't need to care about which
12711 we are handling. */
12712
94f592af 12713void
7016a5d5 12714md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 12715{
94f592af 12716 char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
c6682705 12717 valueT value = *valP;
252b5132 12718
f86103b7 12719#if !defined (TE_Mach)
93382f6d
AM
12720 if (fixP->fx_pcrel)
12721 {
12722 switch (fixP->fx_r_type)
12723 {
5865bb77
ILT
12724 default:
12725 break;
12726
d6ab8113
JB
12727 case BFD_RELOC_64:
12728 fixP->fx_r_type = BFD_RELOC_64_PCREL;
12729 break;
93382f6d 12730 case BFD_RELOC_32:
ae8887b5 12731 case BFD_RELOC_X86_64_32S:
93382f6d
AM
12732 fixP->fx_r_type = BFD_RELOC_32_PCREL;
12733 break;
12734 case BFD_RELOC_16:
12735 fixP->fx_r_type = BFD_RELOC_16_PCREL;
12736 break;
12737 case BFD_RELOC_8:
12738 fixP->fx_r_type = BFD_RELOC_8_PCREL;
12739 break;
12740 }
12741 }
252b5132 12742
a161fe53 12743 if (fixP->fx_addsy != NULL
31312f95 12744 && (fixP->fx_r_type == BFD_RELOC_32_PCREL
d6ab8113 12745 || fixP->fx_r_type == BFD_RELOC_64_PCREL
31312f95 12746 || fixP->fx_r_type == BFD_RELOC_16_PCREL
d258b828 12747 || fixP->fx_r_type == BFD_RELOC_8_PCREL)
31312f95 12748 && !use_rela_relocations)
252b5132 12749 {
31312f95
AM
12750 /* This is a hack. There should be a better way to handle this.
12751 This covers for the fact that bfd_install_relocation will
12752 subtract the current location (for partial_inplace, PC relative
12753 relocations); see more below. */
252b5132 12754#ifndef OBJ_AOUT
718ddfc0 12755 if (IS_ELF
252b5132
RH
12756#ifdef TE_PE
12757 || OUTPUT_FLAVOR == bfd_target_coff_flavour
12758#endif
12759 )
12760 value += fixP->fx_where + fixP->fx_frag->fr_address;
12761#endif
12762#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12763 if (IS_ELF)
252b5132 12764 {
6539b54b 12765 segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
2f66722d 12766
6539b54b 12767 if ((sym_seg == seg
2f66722d 12768 || (symbol_section_p (fixP->fx_addsy)
6539b54b 12769 && sym_seg != absolute_section))
af65af87 12770 && !generic_force_reloc (fixP))
2f66722d
AM
12771 {
12772 /* Yes, we add the values in twice. This is because
6539b54b
AM
12773 bfd_install_relocation subtracts them out again. I think
12774 bfd_install_relocation is broken, but I don't dare change
2f66722d
AM
12775 it. FIXME. */
12776 value += fixP->fx_where + fixP->fx_frag->fr_address;
12777 }
252b5132
RH
12778 }
12779#endif
12780#if defined (OBJ_COFF) && defined (TE_PE)
977cdf5a
NC
12781 /* For some reason, the PE format does not store a
12782 section address offset for a PC relative symbol. */
12783 if (S_GET_SEGMENT (fixP->fx_addsy) != seg
7be1c489 12784 || S_IS_WEAK (fixP->fx_addsy))
252b5132
RH
12785 value += md_pcrel_from (fixP);
12786#endif
12787 }
fbeb56a4 12788#if defined (OBJ_COFF) && defined (TE_PE)
f01c1a09
NC
12789 if (fixP->fx_addsy != NULL
12790 && S_IS_WEAK (fixP->fx_addsy)
12791 /* PR 16858: Do not modify weak function references. */
12792 && ! fixP->fx_pcrel)
fbeb56a4 12793 {
296a8689
NC
12794#if !defined (TE_PEP)
12795 /* For x86 PE weak function symbols are neither PC-relative
12796 nor do they set S_IS_FUNCTION. So the only reliable way
12797 to detect them is to check the flags of their containing
12798 section. */
12799 if (S_GET_SEGMENT (fixP->fx_addsy) != NULL
12800 && S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_CODE)
12801 ;
12802 else
12803#endif
fbeb56a4
DK
12804 value -= S_GET_VALUE (fixP->fx_addsy);
12805 }
12806#endif
252b5132
RH
12807
12808 /* Fix a few things - the dynamic linker expects certain values here,
0234cb7c 12809 and we must not disappoint it. */
252b5132 12810#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12811 if (IS_ELF && fixP->fx_addsy)
47926f60
KH
12812 switch (fixP->fx_r_type)
12813 {
12814 case BFD_RELOC_386_PLT32:
3e73aa7c 12815 case BFD_RELOC_X86_64_PLT32:
b9519cfe
L
12816 /* Make the jump instruction point to the address of the operand.
12817 At runtime we merely add the offset to the actual PLT entry.
12818 NB: Subtract the offset size only for jump instructions. */
12819 if (fixP->fx_pcrel)
12820 value = -4;
47926f60 12821 break;
31312f95 12822
13ae64f3
JJ
12823 case BFD_RELOC_386_TLS_GD:
12824 case BFD_RELOC_386_TLS_LDM:
13ae64f3 12825 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
12826 case BFD_RELOC_386_TLS_IE:
12827 case BFD_RELOC_386_TLS_GOTIE:
67a4f2b7 12828 case BFD_RELOC_386_TLS_GOTDESC:
bffbf940
JJ
12829 case BFD_RELOC_X86_64_TLSGD:
12830 case BFD_RELOC_X86_64_TLSLD:
12831 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7 12832 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
00f7efb6
JJ
12833 value = 0; /* Fully resolved at runtime. No addend. */
12834 /* Fallthrough */
12835 case BFD_RELOC_386_TLS_LE:
12836 case BFD_RELOC_386_TLS_LDO_32:
12837 case BFD_RELOC_386_TLS_LE_32:
12838 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 12839 case BFD_RELOC_X86_64_DTPOFF64:
00f7efb6 12840 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113 12841 case BFD_RELOC_X86_64_TPOFF64:
00f7efb6
JJ
12842 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12843 break;
12844
67a4f2b7
AO
12845 case BFD_RELOC_386_TLS_DESC_CALL:
12846 case BFD_RELOC_X86_64_TLSDESC_CALL:
12847 value = 0; /* Fully resolved at runtime. No addend. */
12848 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12849 fixP->fx_done = 0;
12850 return;
12851
47926f60
KH
12852 case BFD_RELOC_VTABLE_INHERIT:
12853 case BFD_RELOC_VTABLE_ENTRY:
12854 fixP->fx_done = 0;
94f592af 12855 return;
47926f60
KH
12856
12857 default:
12858 break;
12859 }
12860#endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) */
a442cac5
JB
12861
12862 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
12863 if (!object_64bit)
12864 value = extend_to_32bit_address (value);
12865
c6682705 12866 *valP = value;
f86103b7 12867#endif /* !defined (TE_Mach) */
3e73aa7c 12868
3e73aa7c 12869 /* Are we finished with this relocation now? */
c6682705 12870 if (fixP->fx_addsy == NULL)
b8188555
JB
12871 {
12872 fixP->fx_done = 1;
12873 switch (fixP->fx_r_type)
12874 {
12875 case BFD_RELOC_X86_64_32S:
12876 fixP->fx_signed = 1;
12877 break;
12878
12879 default:
12880 break;
12881 }
12882 }
fbeb56a4
DK
12883#if defined (OBJ_COFF) && defined (TE_PE)
12884 else if (fixP->fx_addsy != NULL && S_IS_WEAK (fixP->fx_addsy))
12885 {
12886 fixP->fx_done = 0;
12887 /* Remember value for tc_gen_reloc. */
12888 fixP->fx_addnumber = value;
12889 /* Clear out the frag for now. */
12890 value = 0;
12891 }
12892#endif
3e73aa7c
JH
12893 else if (use_rela_relocations)
12894 {
46fb6d5a
JB
12895 if (!disallow_64bit_reloc || fixP->fx_r_type == NO_RELOC)
12896 fixP->fx_no_overflow = 1;
062cd5e7
AS
12897 /* Remember value for tc_gen_reloc. */
12898 fixP->fx_addnumber = value;
3e73aa7c
JH
12899 value = 0;
12900 }
f86103b7 12901
94f592af 12902 md_number_to_chars (p, value, fixP->fx_size);
252b5132 12903}
252b5132 12904\f
6d4af3c2 12905const char *
499ac353 12906md_atof (int type, char *litP, int *sizeP)
252b5132 12907{
499ac353
NC
12908 /* This outputs the LITTLENUMs in REVERSE order;
12909 in accord with the bigendian 386. */
5b7c81bd 12910 return ieee_md_atof (type, litP, sizeP, false);
252b5132
RH
12911}
12912\f
2d545b82 12913static char output_invalid_buf[sizeof (unsigned char) * 2 + 6];
252b5132 12914
252b5132 12915static char *
e3bb37b5 12916output_invalid (int c)
252b5132 12917{
3882b010 12918 if (ISPRINT (c))
f9f21a03
L
12919 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
12920 "'%c'", c);
252b5132 12921 else
f9f21a03 12922 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
2d545b82 12923 "(0x%x)", (unsigned char) c);
252b5132
RH
12924 return output_invalid_buf;
12925}
12926
8a6fb3f9
JB
12927/* Verify that @r can be used in the current context. */
12928
5b7c81bd 12929static bool check_register (const reg_entry *r)
8a6fb3f9
JB
12930{
12931 if (allow_pseudo_reg)
5b7c81bd 12932 return true;
8a6fb3f9
JB
12933
12934 if (operand_type_all_zero (&r->reg_type))
5b7c81bd 12935 return false;
8a6fb3f9
JB
12936
12937 if ((r->reg_type.bitfield.dword
12938 || (r->reg_type.bitfield.class == SReg && r->reg_num > 3)
12939 || r->reg_type.bitfield.class == RegCR
22e00a3f 12940 || r->reg_type.bitfield.class == RegDR)
8a6fb3f9 12941 && !cpu_arch_flags.bitfield.cpui386)
5b7c81bd 12942 return false;
8a6fb3f9 12943
22e00a3f
JB
12944 if (r->reg_type.bitfield.class == RegTR
12945 && (flag_code == CODE_64BIT
12946 || !cpu_arch_flags.bitfield.cpui386
12947 || cpu_arch_isa_flags.bitfield.cpui586
12948 || cpu_arch_isa_flags.bitfield.cpui686))
5b7c81bd 12949 return false;
22e00a3f 12950
8a6fb3f9 12951 if (r->reg_type.bitfield.class == RegMMX && !cpu_arch_flags.bitfield.cpummx)
5b7c81bd 12952 return false;
8a6fb3f9
JB
12953
12954 if (!cpu_arch_flags.bitfield.cpuavx512f)
12955 {
12956 if (r->reg_type.bitfield.zmmword
12957 || r->reg_type.bitfield.class == RegMask)
5b7c81bd 12958 return false;
8a6fb3f9
JB
12959
12960 if (!cpu_arch_flags.bitfield.cpuavx)
12961 {
12962 if (r->reg_type.bitfield.ymmword)
5b7c81bd 12963 return false;
8a6fb3f9
JB
12964
12965 if (!cpu_arch_flags.bitfield.cpusse && r->reg_type.bitfield.xmmword)
5b7c81bd 12966 return false;
8a6fb3f9
JB
12967 }
12968 }
12969
260cd341
LC
12970 if (r->reg_type.bitfield.tmmword
12971 && (!cpu_arch_flags.bitfield.cpuamx_tile
12972 || flag_code != CODE_64BIT))
5b7c81bd 12973 return false;
260cd341 12974
8a6fb3f9 12975 if (r->reg_type.bitfield.class == RegBND && !cpu_arch_flags.bitfield.cpumpx)
5b7c81bd 12976 return false;
8a6fb3f9
JB
12977
12978 /* Don't allow fake index register unless allow_index_reg isn't 0. */
12979 if (!allow_index_reg && r->reg_num == RegIZ)
5b7c81bd 12980 return false;
8a6fb3f9
JB
12981
12982 /* Upper 16 vector registers are only available with VREX in 64bit
12983 mode, and require EVEX encoding. */
12984 if (r->reg_flags & RegVRex)
12985 {
12986 if (!cpu_arch_flags.bitfield.cpuavx512f
12987 || flag_code != CODE_64BIT)
5b7c81bd 12988 return false;
8a6fb3f9 12989
da4977e0
JB
12990 if (i.vec_encoding == vex_encoding_default)
12991 i.vec_encoding = vex_encoding_evex;
12992 else if (i.vec_encoding != vex_encoding_evex)
12993 i.vec_encoding = vex_encoding_error;
8a6fb3f9
JB
12994 }
12995
12996 if (((r->reg_flags & (RegRex64 | RegRex)) || r->reg_type.bitfield.qword)
12997 && (!cpu_arch_flags.bitfield.cpulm || r->reg_type.bitfield.class != RegCR)
12998 && flag_code != CODE_64BIT)
5b7c81bd 12999 return false;
8a6fb3f9
JB
13000
13001 if (r->reg_type.bitfield.class == SReg && r->reg_num == RegFlat
13002 && !intel_syntax)
5b7c81bd 13003 return false;
8a6fb3f9 13004
5b7c81bd 13005 return true;
8a6fb3f9
JB
13006}
13007
af6bdddf 13008/* REG_STRING starts *before* REGISTER_PREFIX. */
252b5132
RH
13009
13010static const reg_entry *
4d1bb795 13011parse_real_register (char *reg_string, char **end_op)
252b5132 13012{
af6bdddf
AM
13013 char *s = reg_string;
13014 char *p;
252b5132
RH
13015 char reg_name_given[MAX_REG_NAME_SIZE + 1];
13016 const reg_entry *r;
13017
13018 /* Skip possible REGISTER_PREFIX and possible whitespace. */
13019 if (*s == REGISTER_PREFIX)
13020 ++s;
13021
13022 if (is_space_char (*s))
13023 ++s;
13024
13025 p = reg_name_given;
af6bdddf 13026 while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
252b5132
RH
13027 {
13028 if (p >= reg_name_given + MAX_REG_NAME_SIZE)
af6bdddf
AM
13029 return (const reg_entry *) NULL;
13030 s++;
252b5132
RH
13031 }
13032
4795cd4a 13033 if (is_part_of_name (*s))
6588847e
DN
13034 return (const reg_entry *) NULL;
13035
af6bdddf 13036 *end_op = s;
252b5132 13037
629310ab 13038 r = (const reg_entry *) str_hash_find (reg_hash, reg_name_given);
252b5132 13039
5f47d35b 13040 /* Handle floating point regs, allowing spaces in the (i) part. */
6288d05f 13041 if (r == reg_st0)
5f47d35b 13042 {
0e0eea78
JB
13043 if (!cpu_arch_flags.bitfield.cpu8087
13044 && !cpu_arch_flags.bitfield.cpu287
af32b722
JB
13045 && !cpu_arch_flags.bitfield.cpu387
13046 && !allow_pseudo_reg)
0e0eea78
JB
13047 return (const reg_entry *) NULL;
13048
5f47d35b
AM
13049 if (is_space_char (*s))
13050 ++s;
13051 if (*s == '(')
13052 {
af6bdddf 13053 ++s;
5f47d35b
AM
13054 if (is_space_char (*s))
13055 ++s;
13056 if (*s >= '0' && *s <= '7')
13057 {
db557034 13058 int fpr = *s - '0';
af6bdddf 13059 ++s;
5f47d35b
AM
13060 if (is_space_char (*s))
13061 ++s;
13062 if (*s == ')')
13063 {
13064 *end_op = s + 1;
6288d05f 13065 know (r[fpr].reg_num == fpr);
db557034 13066 return r + fpr;
5f47d35b 13067 }
5f47d35b 13068 }
47926f60 13069 /* We have "%st(" then garbage. */
5f47d35b
AM
13070 return (const reg_entry *) NULL;
13071 }
13072 }
13073
8a6fb3f9 13074 return r && check_register (r) ? r : NULL;
252b5132 13075}
4d1bb795
JB
13076
13077/* REG_STRING starts *before* REGISTER_PREFIX. */
13078
13079static const reg_entry *
13080parse_register (char *reg_string, char **end_op)
13081{
13082 const reg_entry *r;
13083
13084 if (*reg_string == REGISTER_PREFIX || allow_naked_reg)
13085 r = parse_real_register (reg_string, end_op);
13086 else
13087 r = NULL;
13088 if (!r)
13089 {
13090 char *save = input_line_pointer;
13091 char c;
13092 symbolS *symbolP;
13093
13094 input_line_pointer = reg_string;
d02603dc 13095 c = get_symbol_name (&reg_string);
4d1bb795 13096 symbolP = symbol_find (reg_string);
64d23078
JB
13097 while (symbolP && S_GET_SEGMENT (symbolP) != reg_section)
13098 {
13099 const expressionS *e = symbol_get_value_expression(symbolP);
13100
13101 if (e->X_op != O_symbol || e->X_add_number)
13102 break;
13103 symbolP = e->X_add_symbol;
13104 }
4d1bb795
JB
13105 if (symbolP && S_GET_SEGMENT (symbolP) == reg_section)
13106 {
13107 const expressionS *e = symbol_get_value_expression (symbolP);
13108
0ccade1a
JB
13109 if (e->X_op == O_register)
13110 {
13111 know (e->X_add_number >= 0
13112 && (valueT) e->X_add_number < i386_regtab_size);
13113 r = i386_regtab + e->X_add_number;
13114 *end_op = input_line_pointer;
13115 }
13116 if (r && !check_register (r))
8a6fb3f9 13117 {
3b55a1d0
JB
13118 as_bad (_("register '%s%s' cannot be used here"),
13119 register_prefix, r->reg_name);
13120 r = &bad_reg;
8a6fb3f9 13121 }
4d1bb795
JB
13122 }
13123 *input_line_pointer = c;
13124 input_line_pointer = save;
13125 }
13126 return r;
13127}
13128
13129int
13130i386_parse_name (char *name, expressionS *e, char *nextcharP)
13131{
4faaa10f 13132 const reg_entry *r = NULL;
4d1bb795
JB
13133 char *end = input_line_pointer;
13134
13135 *end = *nextcharP;
4faaa10f
JB
13136 if (*name == REGISTER_PREFIX || allow_naked_reg)
13137 r = parse_real_register (name, &input_line_pointer);
4d1bb795
JB
13138 if (r && end <= input_line_pointer)
13139 {
13140 *nextcharP = *input_line_pointer;
13141 *input_line_pointer = 0;
8a6fb3f9
JB
13142 if (r != &bad_reg)
13143 {
13144 e->X_op = O_register;
13145 e->X_add_number = r - i386_regtab;
13146 }
13147 else
13148 e->X_op = O_illegal;
4d1bb795
JB
13149 return 1;
13150 }
13151 input_line_pointer = end;
13152 *end = 0;
ee86248c 13153 return intel_syntax ? i386_intel_parse_name (name, e) : 0;
4d1bb795
JB
13154}
13155
13156void
13157md_operand (expressionS *e)
13158{
ee86248c
JB
13159 char *end;
13160 const reg_entry *r;
4d1bb795 13161
ee86248c
JB
13162 switch (*input_line_pointer)
13163 {
13164 case REGISTER_PREFIX:
13165 r = parse_real_register (input_line_pointer, &end);
4d1bb795
JB
13166 if (r)
13167 {
13168 e->X_op = O_register;
13169 e->X_add_number = r - i386_regtab;
13170 input_line_pointer = end;
13171 }
ee86248c
JB
13172 break;
13173
13174 case '[':
9c2799c2 13175 gas_assert (intel_syntax);
ee86248c
JB
13176 end = input_line_pointer++;
13177 expression (e);
13178 if (*input_line_pointer == ']')
13179 {
13180 ++input_line_pointer;
13181 e->X_op_symbol = make_expr_symbol (e);
13182 e->X_add_symbol = NULL;
13183 e->X_add_number = 0;
13184 e->X_op = O_index;
13185 }
13186 else
13187 {
13188 e->X_op = O_absent;
13189 input_line_pointer = end;
13190 }
13191 break;
4d1bb795
JB
13192 }
13193}
13194
252b5132 13195\f
4cc782b5 13196#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
b6f8c7c4 13197const char *md_shortopts = "kVQ:sqnO::";
252b5132 13198#else
b6f8c7c4 13199const char *md_shortopts = "qnO::";
252b5132 13200#endif
6e0b89ee 13201
3e73aa7c 13202#define OPTION_32 (OPTION_MD_BASE + 0)
b3b91714
AM
13203#define OPTION_64 (OPTION_MD_BASE + 1)
13204#define OPTION_DIVIDE (OPTION_MD_BASE + 2)
9103f4f4
L
13205#define OPTION_MARCH (OPTION_MD_BASE + 3)
13206#define OPTION_MTUNE (OPTION_MD_BASE + 4)
1efbbeb4
L
13207#define OPTION_MMNEMONIC (OPTION_MD_BASE + 5)
13208#define OPTION_MSYNTAX (OPTION_MD_BASE + 6)
13209#define OPTION_MINDEX_REG (OPTION_MD_BASE + 7)
13210#define OPTION_MNAKED_REG (OPTION_MD_BASE + 8)
bd5dea88 13211#define OPTION_MRELAX_RELOCATIONS (OPTION_MD_BASE + 9)
c0f3af97 13212#define OPTION_MSSE2AVX (OPTION_MD_BASE + 10)
daf50ae7 13213#define OPTION_MSSE_CHECK (OPTION_MD_BASE + 11)
7bab8ab5
JB
13214#define OPTION_MOPERAND_CHECK (OPTION_MD_BASE + 12)
13215#define OPTION_MAVXSCALAR (OPTION_MD_BASE + 13)
13216#define OPTION_X32 (OPTION_MD_BASE + 14)
7e8b059b 13217#define OPTION_MADD_BND_PREFIX (OPTION_MD_BASE + 15)
43234a1e
L
13218#define OPTION_MEVEXLIG (OPTION_MD_BASE + 16)
13219#define OPTION_MEVEXWIG (OPTION_MD_BASE + 17)
167ad85b 13220#define OPTION_MBIG_OBJ (OPTION_MD_BASE + 18)
d1982f93 13221#define OPTION_MOMIT_LOCK_PREFIX (OPTION_MD_BASE + 19)
d3d3c6db 13222#define OPTION_MEVEXRCIG (OPTION_MD_BASE + 20)
8dcea932 13223#define OPTION_MSHARED (OPTION_MD_BASE + 21)
5db04b09
L
13224#define OPTION_MAMD64 (OPTION_MD_BASE + 22)
13225#define OPTION_MINTEL64 (OPTION_MD_BASE + 23)
e4e00185 13226#define OPTION_MFENCE_AS_LOCK_ADD (OPTION_MD_BASE + 24)
b4a3a7b4 13227#define OPTION_X86_USED_NOTE (OPTION_MD_BASE + 25)
03751133 13228#define OPTION_MVEXWIG (OPTION_MD_BASE + 26)
e379e5f3
L
13229#define OPTION_MALIGN_BRANCH_BOUNDARY (OPTION_MD_BASE + 27)
13230#define OPTION_MALIGN_BRANCH_PREFIX_SIZE (OPTION_MD_BASE + 28)
13231#define OPTION_MALIGN_BRANCH (OPTION_MD_BASE + 29)
76cf450b 13232#define OPTION_MBRANCHES_WITH_32B_BOUNDARIES (OPTION_MD_BASE + 30)
ae531041
L
13233#define OPTION_MLFENCE_AFTER_LOAD (OPTION_MD_BASE + 31)
13234#define OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH (OPTION_MD_BASE + 32)
13235#define OPTION_MLFENCE_BEFORE_RET (OPTION_MD_BASE + 33)
c8480b58 13236#define OPTION_MUSE_UNALIGNED_VECTOR_MOVE (OPTION_MD_BASE + 34)
b3b91714 13237
99ad8390
NC
13238struct option md_longopts[] =
13239{
3e73aa7c 13240 {"32", no_argument, NULL, OPTION_32},
321098a5 13241#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 13242 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c 13243 {"64", no_argument, NULL, OPTION_64},
351f65ca
L
13244#endif
13245#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 13246 {"x32", no_argument, NULL, OPTION_X32},
8dcea932 13247 {"mshared", no_argument, NULL, OPTION_MSHARED},
b4a3a7b4 13248 {"mx86-used-note", required_argument, NULL, OPTION_X86_USED_NOTE},
6e0b89ee 13249#endif
b3b91714 13250 {"divide", no_argument, NULL, OPTION_DIVIDE},
9103f4f4
L
13251 {"march", required_argument, NULL, OPTION_MARCH},
13252 {"mtune", required_argument, NULL, OPTION_MTUNE},
1efbbeb4
L
13253 {"mmnemonic", required_argument, NULL, OPTION_MMNEMONIC},
13254 {"msyntax", required_argument, NULL, OPTION_MSYNTAX},
13255 {"mindex-reg", no_argument, NULL, OPTION_MINDEX_REG},
13256 {"mnaked-reg", no_argument, NULL, OPTION_MNAKED_REG},
c0f3af97 13257 {"msse2avx", no_argument, NULL, OPTION_MSSE2AVX},
c8480b58 13258 {"muse-unaligned-vector-move", no_argument, NULL, OPTION_MUSE_UNALIGNED_VECTOR_MOVE},
daf50ae7 13259 {"msse-check", required_argument, NULL, OPTION_MSSE_CHECK},
7bab8ab5 13260 {"moperand-check", required_argument, NULL, OPTION_MOPERAND_CHECK},
539f890d 13261 {"mavxscalar", required_argument, NULL, OPTION_MAVXSCALAR},
03751133 13262 {"mvexwig", required_argument, NULL, OPTION_MVEXWIG},
7e8b059b 13263 {"madd-bnd-prefix", no_argument, NULL, OPTION_MADD_BND_PREFIX},
43234a1e
L
13264 {"mevexlig", required_argument, NULL, OPTION_MEVEXLIG},
13265 {"mevexwig", required_argument, NULL, OPTION_MEVEXWIG},
167ad85b
TG
13266# if defined (TE_PE) || defined (TE_PEP)
13267 {"mbig-obj", no_argument, NULL, OPTION_MBIG_OBJ},
13268#endif
d1982f93 13269 {"momit-lock-prefix", required_argument, NULL, OPTION_MOMIT_LOCK_PREFIX},
e4e00185 13270 {"mfence-as-lock-add", required_argument, NULL, OPTION_MFENCE_AS_LOCK_ADD},
0cb4071e 13271 {"mrelax-relocations", required_argument, NULL, OPTION_MRELAX_RELOCATIONS},
d3d3c6db 13272 {"mevexrcig", required_argument, NULL, OPTION_MEVEXRCIG},
e379e5f3
L
13273 {"malign-branch-boundary", required_argument, NULL, OPTION_MALIGN_BRANCH_BOUNDARY},
13274 {"malign-branch-prefix-size", required_argument, NULL, OPTION_MALIGN_BRANCH_PREFIX_SIZE},
13275 {"malign-branch", required_argument, NULL, OPTION_MALIGN_BRANCH},
76cf450b 13276 {"mbranches-within-32B-boundaries", no_argument, NULL, OPTION_MBRANCHES_WITH_32B_BOUNDARIES},
ae531041
L
13277 {"mlfence-after-load", required_argument, NULL, OPTION_MLFENCE_AFTER_LOAD},
13278 {"mlfence-before-indirect-branch", required_argument, NULL,
13279 OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH},
13280 {"mlfence-before-ret", required_argument, NULL, OPTION_MLFENCE_BEFORE_RET},
5db04b09
L
13281 {"mamd64", no_argument, NULL, OPTION_MAMD64},
13282 {"mintel64", no_argument, NULL, OPTION_MINTEL64},
252b5132
RH
13283 {NULL, no_argument, NULL, 0}
13284};
13285size_t md_longopts_size = sizeof (md_longopts);
13286
13287int
17b9d67d 13288md_parse_option (int c, const char *arg)
252b5132 13289{
91d6fa6a 13290 unsigned int j;
e379e5f3 13291 char *arch, *next, *saved, *type;
9103f4f4 13292
252b5132
RH
13293 switch (c)
13294 {
12b55ccc
L
13295 case 'n':
13296 optimize_align_code = 0;
13297 break;
13298
a38cf1db
AM
13299 case 'q':
13300 quiet_warnings = 1;
252b5132
RH
13301 break;
13302
13303#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
a38cf1db
AM
13304 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
13305 should be emitted or not. FIXME: Not implemented. */
13306 case 'Q':
d4693039
JB
13307 if ((arg[0] != 'y' && arg[0] != 'n') || arg[1])
13308 return 0;
252b5132
RH
13309 break;
13310
13311 /* -V: SVR4 argument to print version ID. */
13312 case 'V':
13313 print_version_id ();
13314 break;
13315
a38cf1db
AM
13316 /* -k: Ignore for FreeBSD compatibility. */
13317 case 'k':
252b5132 13318 break;
4cc782b5
ILT
13319
13320 case 's':
13321 /* -s: On i386 Solaris, this tells the native assembler to use
29b0f896 13322 .stab instead of .stab.excl. We always use .stab anyhow. */
4cc782b5 13323 break;
8dcea932
L
13324
13325 case OPTION_MSHARED:
13326 shared = 1;
13327 break;
b4a3a7b4
L
13328
13329 case OPTION_X86_USED_NOTE:
13330 if (strcasecmp (arg, "yes") == 0)
13331 x86_used_note = 1;
13332 else if (strcasecmp (arg, "no") == 0)
13333 x86_used_note = 0;
13334 else
13335 as_fatal (_("invalid -mx86-used-note= option: `%s'"), arg);
13336 break;
13337
13338
99ad8390 13339#endif
321098a5 13340#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 13341 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c
JH
13342 case OPTION_64:
13343 {
13344 const char **list, **l;
13345
3e73aa7c
JH
13346 list = bfd_target_list ();
13347 for (l = list; *l != NULL; l++)
08dedd66 13348 if (startswith (*l, "elf64-x86-64")
99ad8390
NC
13349 || strcmp (*l, "coff-x86-64") == 0
13350 || strcmp (*l, "pe-x86-64") == 0
d382c579
TG
13351 || strcmp (*l, "pei-x86-64") == 0
13352 || strcmp (*l, "mach-o-x86-64") == 0)
6e0b89ee
AM
13353 {
13354 default_arch = "x86_64";
13355 break;
13356 }
3e73aa7c 13357 if (*l == NULL)
2b5d6a91 13358 as_fatal (_("no compiled in support for x86_64"));
3e73aa7c
JH
13359 free (list);
13360 }
13361 break;
13362#endif
252b5132 13363
351f65ca 13364#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 13365 case OPTION_X32:
351f65ca
L
13366 if (IS_ELF)
13367 {
13368 const char **list, **l;
13369
13370 list = bfd_target_list ();
13371 for (l = list; *l != NULL; l++)
08dedd66 13372 if (startswith (*l, "elf32-x86-64"))
351f65ca
L
13373 {
13374 default_arch = "x86_64:32";
13375 break;
13376 }
13377 if (*l == NULL)
2b5d6a91 13378 as_fatal (_("no compiled in support for 32bit x86_64"));
351f65ca
L
13379 free (list);
13380 }
13381 else
13382 as_fatal (_("32bit x86_64 is only supported for ELF"));
13383 break;
13384#endif
13385
6e0b89ee
AM
13386 case OPTION_32:
13387 default_arch = "i386";
13388 break;
13389
b3b91714
AM
13390 case OPTION_DIVIDE:
13391#ifdef SVR4_COMMENT_CHARS
13392 {
13393 char *n, *t;
13394 const char *s;
13395
add39d23 13396 n = XNEWVEC (char, strlen (i386_comment_chars) + 1);
b3b91714
AM
13397 t = n;
13398 for (s = i386_comment_chars; *s != '\0'; s++)
13399 if (*s != '/')
13400 *t++ = *s;
13401 *t = '\0';
13402 i386_comment_chars = n;
13403 }
13404#endif
13405 break;
13406
9103f4f4 13407 case OPTION_MARCH:
293f5f65
L
13408 saved = xstrdup (arg);
13409 arch = saved;
13410 /* Allow -march=+nosse. */
13411 if (*arch == '+')
13412 arch++;
6305a203 13413 do
9103f4f4 13414 {
6305a203 13415 if (*arch == '.')
2b5d6a91 13416 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
13417 next = strchr (arch, '+');
13418 if (next)
13419 *next++ = '\0';
91d6fa6a 13420 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 13421 {
ae89daec
JB
13422 if (arch == saved && cpu_arch[j].type != PROCESSOR_NONE
13423 && strcmp (arch, cpu_arch[j].name) == 0)
ccc9c027 13424 {
6305a203 13425 /* Processor. */
ae89daec 13426 if (! cpu_arch[j].enable.bitfield.cpui386)
1ded5609
JB
13427 continue;
13428
91d6fa6a 13429 cpu_arch_name = cpu_arch[j].name;
d92c7521 13430 free (cpu_sub_arch_name);
6305a203 13431 cpu_sub_arch_name = NULL;
ae89daec 13432 cpu_arch_flags = cpu_arch[j].enable;
91d6fa6a 13433 cpu_arch_isa = cpu_arch[j].type;
ae89daec 13434 cpu_arch_isa_flags = cpu_arch[j].enable;
6305a203
L
13435 if (!cpu_arch_tune_set)
13436 {
13437 cpu_arch_tune = cpu_arch_isa;
13438 cpu_arch_tune_flags = cpu_arch_isa_flags;
13439 }
13440 break;
13441 }
ae89daec
JB
13442 else if (cpu_arch[j].type == PROCESSOR_NONE
13443 && strcmp (arch, cpu_arch[j].name) == 0
13444 && !cpu_flags_all_zero (&cpu_arch[j].enable))
6305a203 13445 {
33eaf5de 13446 /* ISA extension. */
6305a203 13447 i386_cpu_flags flags;
309d3373 13448
293f5f65 13449 flags = cpu_flags_or (cpu_arch_flags,
ae89daec 13450 cpu_arch[j].enable);
81486035 13451
5b64d091 13452 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
6305a203 13453 {
ae89daec 13454 extend_cpu_sub_arch_name (arch);
6305a203 13455 cpu_arch_flags = flags;
a586129e 13456 cpu_arch_isa_flags = flags;
6305a203 13457 }
0089dace
L
13458 else
13459 cpu_arch_isa_flags
13460 = cpu_flags_or (cpu_arch_isa_flags,
ae89daec 13461 cpu_arch[j].enable);
6305a203 13462 break;
ccc9c027 13463 }
9103f4f4 13464 }
6305a203 13465
ae89daec 13466 if (j >= ARRAY_SIZE (cpu_arch) && startswith (arch, "no"))
293f5f65 13467 {
33eaf5de 13468 /* Disable an ISA extension. */
ae89daec
JB
13469 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13470 if (cpu_arch[j].type == PROCESSOR_NONE
13471 && strcmp (arch + 2, cpu_arch[j].name) == 0)
293f5f65
L
13472 {
13473 i386_cpu_flags flags;
13474
13475 flags = cpu_flags_and_not (cpu_arch_flags,
ae89daec 13476 cpu_arch[j].disable);
293f5f65
L
13477 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
13478 {
8180707f 13479 extend_cpu_sub_arch_name (arch);
293f5f65
L
13480 cpu_arch_flags = flags;
13481 cpu_arch_isa_flags = flags;
13482 }
13483 break;
13484 }
293f5f65
L
13485 }
13486
91d6fa6a 13487 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 13488 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
13489
13490 arch = next;
9103f4f4 13491 }
293f5f65
L
13492 while (next != NULL);
13493 free (saved);
9103f4f4
L
13494 break;
13495
13496 case OPTION_MTUNE:
13497 if (*arg == '.')
2b5d6a91 13498 as_fatal (_("invalid -mtune= option: `%s'"), arg);
91d6fa6a 13499 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 13500 {
ae89daec
JB
13501 if (cpu_arch[j].type != PROCESSOR_NONE
13502 && strcmp (arg, cpu_arch[j].name) == 0)
9103f4f4 13503 {
ccc9c027 13504 cpu_arch_tune_set = 1;
91d6fa6a 13505 cpu_arch_tune = cpu_arch [j].type;
ae89daec 13506 cpu_arch_tune_flags = cpu_arch[j].enable;
9103f4f4
L
13507 break;
13508 }
13509 }
91d6fa6a 13510 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 13511 as_fatal (_("invalid -mtune= option: `%s'"), arg);
9103f4f4
L
13512 break;
13513
1efbbeb4
L
13514 case OPTION_MMNEMONIC:
13515 if (strcasecmp (arg, "att") == 0)
13516 intel_mnemonic = 0;
13517 else if (strcasecmp (arg, "intel") == 0)
13518 intel_mnemonic = 1;
13519 else
2b5d6a91 13520 as_fatal (_("invalid -mmnemonic= option: `%s'"), arg);
1efbbeb4
L
13521 break;
13522
13523 case OPTION_MSYNTAX:
13524 if (strcasecmp (arg, "att") == 0)
13525 intel_syntax = 0;
13526 else if (strcasecmp (arg, "intel") == 0)
13527 intel_syntax = 1;
13528 else
2b5d6a91 13529 as_fatal (_("invalid -msyntax= option: `%s'"), arg);
1efbbeb4
L
13530 break;
13531
13532 case OPTION_MINDEX_REG:
13533 allow_index_reg = 1;
13534 break;
13535
13536 case OPTION_MNAKED_REG:
13537 allow_naked_reg = 1;
13538 break;
13539
c0f3af97
L
13540 case OPTION_MSSE2AVX:
13541 sse2avx = 1;
13542 break;
13543
c8480b58
L
13544 case OPTION_MUSE_UNALIGNED_VECTOR_MOVE:
13545 use_unaligned_vector_move = 1;
13546 break;
13547
daf50ae7
L
13548 case OPTION_MSSE_CHECK:
13549 if (strcasecmp (arg, "error") == 0)
7bab8ab5 13550 sse_check = check_error;
daf50ae7 13551 else if (strcasecmp (arg, "warning") == 0)
7bab8ab5 13552 sse_check = check_warning;
daf50ae7 13553 else if (strcasecmp (arg, "none") == 0)
7bab8ab5 13554 sse_check = check_none;
daf50ae7 13555 else
2b5d6a91 13556 as_fatal (_("invalid -msse-check= option: `%s'"), arg);
daf50ae7
L
13557 break;
13558
7bab8ab5
JB
13559 case OPTION_MOPERAND_CHECK:
13560 if (strcasecmp (arg, "error") == 0)
13561 operand_check = check_error;
13562 else if (strcasecmp (arg, "warning") == 0)
13563 operand_check = check_warning;
13564 else if (strcasecmp (arg, "none") == 0)
13565 operand_check = check_none;
13566 else
13567 as_fatal (_("invalid -moperand-check= option: `%s'"), arg);
13568 break;
13569
539f890d
L
13570 case OPTION_MAVXSCALAR:
13571 if (strcasecmp (arg, "128") == 0)
13572 avxscalar = vex128;
13573 else if (strcasecmp (arg, "256") == 0)
13574 avxscalar = vex256;
13575 else
2b5d6a91 13576 as_fatal (_("invalid -mavxscalar= option: `%s'"), arg);
539f890d
L
13577 break;
13578
03751133
L
13579 case OPTION_MVEXWIG:
13580 if (strcmp (arg, "0") == 0)
40c9c8de 13581 vexwig = vexw0;
03751133 13582 else if (strcmp (arg, "1") == 0)
40c9c8de 13583 vexwig = vexw1;
03751133
L
13584 else
13585 as_fatal (_("invalid -mvexwig= option: `%s'"), arg);
13586 break;
13587
7e8b059b
L
13588 case OPTION_MADD_BND_PREFIX:
13589 add_bnd_prefix = 1;
13590 break;
13591
43234a1e
L
13592 case OPTION_MEVEXLIG:
13593 if (strcmp (arg, "128") == 0)
13594 evexlig = evexl128;
13595 else if (strcmp (arg, "256") == 0)
13596 evexlig = evexl256;
13597 else if (strcmp (arg, "512") == 0)
13598 evexlig = evexl512;
13599 else
13600 as_fatal (_("invalid -mevexlig= option: `%s'"), arg);
13601 break;
13602
d3d3c6db
IT
13603 case OPTION_MEVEXRCIG:
13604 if (strcmp (arg, "rne") == 0)
13605 evexrcig = rne;
13606 else if (strcmp (arg, "rd") == 0)
13607 evexrcig = rd;
13608 else if (strcmp (arg, "ru") == 0)
13609 evexrcig = ru;
13610 else if (strcmp (arg, "rz") == 0)
13611 evexrcig = rz;
13612 else
13613 as_fatal (_("invalid -mevexrcig= option: `%s'"), arg);
13614 break;
13615
43234a1e
L
13616 case OPTION_MEVEXWIG:
13617 if (strcmp (arg, "0") == 0)
13618 evexwig = evexw0;
13619 else if (strcmp (arg, "1") == 0)
13620 evexwig = evexw1;
13621 else
13622 as_fatal (_("invalid -mevexwig= option: `%s'"), arg);
13623 break;
13624
167ad85b
TG
13625# if defined (TE_PE) || defined (TE_PEP)
13626 case OPTION_MBIG_OBJ:
13627 use_big_obj = 1;
13628 break;
13629#endif
13630
d1982f93 13631 case OPTION_MOMIT_LOCK_PREFIX:
d022bddd
IT
13632 if (strcasecmp (arg, "yes") == 0)
13633 omit_lock_prefix = 1;
13634 else if (strcasecmp (arg, "no") == 0)
13635 omit_lock_prefix = 0;
13636 else
13637 as_fatal (_("invalid -momit-lock-prefix= option: `%s'"), arg);
13638 break;
13639
e4e00185
AS
13640 case OPTION_MFENCE_AS_LOCK_ADD:
13641 if (strcasecmp (arg, "yes") == 0)
13642 avoid_fence = 1;
13643 else if (strcasecmp (arg, "no") == 0)
13644 avoid_fence = 0;
13645 else
13646 as_fatal (_("invalid -mfence-as-lock-add= option: `%s'"), arg);
13647 break;
13648
ae531041
L
13649 case OPTION_MLFENCE_AFTER_LOAD:
13650 if (strcasecmp (arg, "yes") == 0)
13651 lfence_after_load = 1;
13652 else if (strcasecmp (arg, "no") == 0)
13653 lfence_after_load = 0;
13654 else
13655 as_fatal (_("invalid -mlfence-after-load= option: `%s'"), arg);
13656 break;
13657
13658 case OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH:
13659 if (strcasecmp (arg, "all") == 0)
a09f656b 13660 {
13661 lfence_before_indirect_branch = lfence_branch_all;
13662 if (lfence_before_ret == lfence_before_ret_none)
13663 lfence_before_ret = lfence_before_ret_shl;
13664 }
ae531041
L
13665 else if (strcasecmp (arg, "memory") == 0)
13666 lfence_before_indirect_branch = lfence_branch_memory;
13667 else if (strcasecmp (arg, "register") == 0)
13668 lfence_before_indirect_branch = lfence_branch_register;
13669 else if (strcasecmp (arg, "none") == 0)
13670 lfence_before_indirect_branch = lfence_branch_none;
13671 else
13672 as_fatal (_("invalid -mlfence-before-indirect-branch= option: `%s'"),
13673 arg);
13674 break;
13675
13676 case OPTION_MLFENCE_BEFORE_RET:
13677 if (strcasecmp (arg, "or") == 0)
13678 lfence_before_ret = lfence_before_ret_or;
13679 else if (strcasecmp (arg, "not") == 0)
13680 lfence_before_ret = lfence_before_ret_not;
a09f656b 13681 else if (strcasecmp (arg, "shl") == 0 || strcasecmp (arg, "yes") == 0)
13682 lfence_before_ret = lfence_before_ret_shl;
ae531041
L
13683 else if (strcasecmp (arg, "none") == 0)
13684 lfence_before_ret = lfence_before_ret_none;
13685 else
13686 as_fatal (_("invalid -mlfence-before-ret= option: `%s'"),
13687 arg);
13688 break;
13689
0cb4071e
L
13690 case OPTION_MRELAX_RELOCATIONS:
13691 if (strcasecmp (arg, "yes") == 0)
13692 generate_relax_relocations = 1;
13693 else if (strcasecmp (arg, "no") == 0)
13694 generate_relax_relocations = 0;
13695 else
13696 as_fatal (_("invalid -mrelax-relocations= option: `%s'"), arg);
13697 break;
13698
e379e5f3
L
13699 case OPTION_MALIGN_BRANCH_BOUNDARY:
13700 {
13701 char *end;
13702 long int align = strtoul (arg, &end, 0);
13703 if (*end == '\0')
13704 {
13705 if (align == 0)
13706 {
13707 align_branch_power = 0;
13708 break;
13709 }
13710 else if (align >= 16)
13711 {
13712 int align_power;
13713 for (align_power = 0;
13714 (align & 1) == 0;
13715 align >>= 1, align_power++)
13716 continue;
13717 /* Limit alignment power to 31. */
13718 if (align == 1 && align_power < 32)
13719 {
13720 align_branch_power = align_power;
13721 break;
13722 }
13723 }
13724 }
13725 as_fatal (_("invalid -malign-branch-boundary= value: %s"), arg);
13726 }
13727 break;
13728
13729 case OPTION_MALIGN_BRANCH_PREFIX_SIZE:
13730 {
13731 char *end;
13732 int align = strtoul (arg, &end, 0);
13733 /* Some processors only support 5 prefixes. */
13734 if (*end == '\0' && align >= 0 && align < 6)
13735 {
13736 align_branch_prefix_size = align;
13737 break;
13738 }
13739 as_fatal (_("invalid -malign-branch-prefix-size= value: %s"),
13740 arg);
13741 }
13742 break;
13743
13744 case OPTION_MALIGN_BRANCH:
13745 align_branch = 0;
13746 saved = xstrdup (arg);
13747 type = saved;
13748 do
13749 {
13750 next = strchr (type, '+');
13751 if (next)
13752 *next++ = '\0';
13753 if (strcasecmp (type, "jcc") == 0)
13754 align_branch |= align_branch_jcc_bit;
13755 else if (strcasecmp (type, "fused") == 0)
13756 align_branch |= align_branch_fused_bit;
13757 else if (strcasecmp (type, "jmp") == 0)
13758 align_branch |= align_branch_jmp_bit;
13759 else if (strcasecmp (type, "call") == 0)
13760 align_branch |= align_branch_call_bit;
13761 else if (strcasecmp (type, "ret") == 0)
13762 align_branch |= align_branch_ret_bit;
13763 else if (strcasecmp (type, "indirect") == 0)
13764 align_branch |= align_branch_indirect_bit;
13765 else
13766 as_fatal (_("invalid -malign-branch= option: `%s'"), arg);
13767 type = next;
13768 }
13769 while (next != NULL);
13770 free (saved);
13771 break;
13772
76cf450b
L
13773 case OPTION_MBRANCHES_WITH_32B_BOUNDARIES:
13774 align_branch_power = 5;
13775 align_branch_prefix_size = 5;
13776 align_branch = (align_branch_jcc_bit
13777 | align_branch_fused_bit
13778 | align_branch_jmp_bit);
13779 break;
13780
5db04b09 13781 case OPTION_MAMD64:
4b5aaf5f 13782 isa64 = amd64;
5db04b09
L
13783 break;
13784
13785 case OPTION_MINTEL64:
4b5aaf5f 13786 isa64 = intel64;
5db04b09
L
13787 break;
13788
b6f8c7c4
L
13789 case 'O':
13790 if (arg == NULL)
13791 {
13792 optimize = 1;
13793 /* Turn off -Os. */
13794 optimize_for_space = 0;
13795 }
13796 else if (*arg == 's')
13797 {
13798 optimize_for_space = 1;
13799 /* Turn on all encoding optimizations. */
41fd2579 13800 optimize = INT_MAX;
b6f8c7c4
L
13801 }
13802 else
13803 {
13804 optimize = atoi (arg);
13805 /* Turn off -Os. */
13806 optimize_for_space = 0;
13807 }
13808 break;
13809
252b5132
RH
13810 default:
13811 return 0;
13812 }
13813 return 1;
13814}
13815
8a2c8fef
L
13816#define MESSAGE_TEMPLATE \
13817" "
13818
293f5f65
L
13819static char *
13820output_message (FILE *stream, char *p, char *message, char *start,
13821 int *left_p, const char *name, int len)
13822{
13823 int size = sizeof (MESSAGE_TEMPLATE);
13824 int left = *left_p;
13825
13826 /* Reserve 2 spaces for ", " or ",\0" */
13827 left -= len + 2;
13828
13829 /* Check if there is any room. */
13830 if (left >= 0)
13831 {
13832 if (p != start)
13833 {
13834 *p++ = ',';
13835 *p++ = ' ';
13836 }
13837 p = mempcpy (p, name, len);
13838 }
13839 else
13840 {
13841 /* Output the current message now and start a new one. */
13842 *p++ = ',';
13843 *p = '\0';
13844 fprintf (stream, "%s\n", message);
13845 p = start;
13846 left = size - (start - message) - len - 2;
13847
13848 gas_assert (left >= 0);
13849
13850 p = mempcpy (p, name, len);
13851 }
13852
13853 *left_p = left;
13854 return p;
13855}
13856
8a2c8fef 13857static void
1ded5609 13858show_arch (FILE *stream, int ext, int check)
8a2c8fef
L
13859{
13860 static char message[] = MESSAGE_TEMPLATE;
13861 char *start = message + 27;
13862 char *p;
13863 int size = sizeof (MESSAGE_TEMPLATE);
13864 int left;
13865 const char *name;
13866 int len;
13867 unsigned int j;
13868
13869 p = start;
13870 left = size - (start - message);
3ce2ebcf
JB
13871
13872 if (!ext && check)
13873 {
13874 p = output_message (stream, p, message, start, &left,
13875 STRING_COMMA_LEN ("default"));
f68697e8
JB
13876 p = output_message (stream, p, message, start, &left,
13877 STRING_COMMA_LEN ("push"));
13878 p = output_message (stream, p, message, start, &left,
13879 STRING_COMMA_LEN ("pop"));
3ce2ebcf
JB
13880 }
13881
8a2c8fef
L
13882 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13883 {
13884 /* Should it be skipped? */
13885 if (cpu_arch [j].skip)
13886 continue;
13887
13888 name = cpu_arch [j].name;
13889 len = cpu_arch [j].len;
ae89daec 13890 if (cpu_arch[j].type == PROCESSOR_NONE)
8a2c8fef
L
13891 {
13892 /* It is an extension. Skip if we aren't asked to show it. */
ae89daec 13893 if (!ext || cpu_flags_all_zero (&cpu_arch[j].enable))
8a2c8fef
L
13894 continue;
13895 }
13896 else if (ext)
13897 {
13898 /* It is an processor. Skip if we show only extension. */
13899 continue;
13900 }
ae89daec 13901 else if (check && ! cpu_arch[j].enable.bitfield.cpui386)
1ded5609
JB
13902 {
13903 /* It is an impossible processor - skip. */
13904 continue;
13905 }
8a2c8fef 13906
293f5f65 13907 p = output_message (stream, p, message, start, &left, name, len);
8a2c8fef
L
13908 }
13909
293f5f65
L
13910 /* Display disabled extensions. */
13911 if (ext)
ae89daec 13912 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
293f5f65 13913 {
ae89daec
JB
13914 char *str;
13915
13916 if (cpu_arch[j].type != PROCESSOR_NONE
13917 || !cpu_flags_all_zero (&cpu_arch[j].enable))
13918 continue;
13919 str = xasprintf ("no%s", cpu_arch[j].name);
13920 p = output_message (stream, p, message, start, &left, str,
13921 strlen (str));
13922 free (str);
293f5f65
L
13923 }
13924
8a2c8fef
L
13925 *p = '\0';
13926 fprintf (stream, "%s\n", message);
13927}
13928
252b5132 13929void
8a2c8fef 13930md_show_usage (FILE *stream)
252b5132 13931{
4cc782b5
ILT
13932#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13933 fprintf (stream, _("\
d4693039 13934 -Qy, -Qn ignored\n\
a38cf1db 13935 -V print assembler version number\n\
b3b91714
AM
13936 -k ignored\n"));
13937#endif
13938 fprintf (stream, _("\
7ebd68d1
NC
13939 -n do not optimize code alignment\n\
13940 -O{012s} attempt some code optimizations\n\
b3b91714
AM
13941 -q quieten some warnings\n"));
13942#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13943 fprintf (stream, _("\
a38cf1db 13944 -s ignored\n"));
b3b91714 13945#endif
b00af7c8
JB
13946#ifdef BFD64
13947# if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13948 fprintf (stream, _("\
13949 --32/--64/--x32 generate 32bit/64bit/x32 object\n"));
13950# elif defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O)
751d281c 13951 fprintf (stream, _("\
b00af7c8
JB
13952 --32/--64 generate 32bit/64bit object\n"));
13953# endif
751d281c 13954#endif
b3b91714
AM
13955#ifdef SVR4_COMMENT_CHARS
13956 fprintf (stream, _("\
13957 --divide do not treat `/' as a comment character\n"));
a38cf1db
AM
13958#else
13959 fprintf (stream, _("\
b3b91714 13960 --divide ignored\n"));
4cc782b5 13961#endif
9103f4f4 13962 fprintf (stream, _("\
6305a203 13963 -march=CPU[,+EXTENSION...]\n\
8a2c8fef 13964 generate code for CPU and EXTENSION, CPU is one of:\n"));
1ded5609 13965 show_arch (stream, 0, 1);
8a2c8fef 13966 fprintf (stream, _("\
ae89daec 13967 EXTENSION is combination of (possibly \"no\"-prefixed):\n"));
1ded5609 13968 show_arch (stream, 1, 0);
6305a203 13969 fprintf (stream, _("\
8a2c8fef 13970 -mtune=CPU optimize for CPU, CPU is one of:\n"));
1ded5609 13971 show_arch (stream, 0, 0);
ba104c83 13972 fprintf (stream, _("\
c0f3af97
L
13973 -msse2avx encode SSE instructions with VEX prefix\n"));
13974 fprintf (stream, _("\
c8480b58
L
13975 -muse-unaligned-vector-move\n\
13976 encode aligned vector move as unaligned vector move\n"));
13977 fprintf (stream, _("\
7c5c05ef 13978 -msse-check=[none|error|warning] (default: warning)\n\
daf50ae7
L
13979 check SSE instructions\n"));
13980 fprintf (stream, _("\
7c5c05ef 13981 -moperand-check=[none|error|warning] (default: warning)\n\
7bab8ab5
JB
13982 check operand combinations for validity\n"));
13983 fprintf (stream, _("\
7c5c05ef
L
13984 -mavxscalar=[128|256] (default: 128)\n\
13985 encode scalar AVX instructions with specific vector\n\
539f890d
L
13986 length\n"));
13987 fprintf (stream, _("\
03751133
L
13988 -mvexwig=[0|1] (default: 0)\n\
13989 encode VEX instructions with specific VEX.W value\n\
13990 for VEX.W bit ignored instructions\n"));
13991 fprintf (stream, _("\
7c5c05ef
L
13992 -mevexlig=[128|256|512] (default: 128)\n\
13993 encode scalar EVEX instructions with specific vector\n\
43234a1e
L
13994 length\n"));
13995 fprintf (stream, _("\
7c5c05ef
L
13996 -mevexwig=[0|1] (default: 0)\n\
13997 encode EVEX instructions with specific EVEX.W value\n\
43234a1e
L
13998 for EVEX.W bit ignored instructions\n"));
13999 fprintf (stream, _("\
7c5c05ef 14000 -mevexrcig=[rne|rd|ru|rz] (default: rne)\n\
d3d3c6db
IT
14001 encode EVEX instructions with specific EVEX.RC value\n\
14002 for SAE-only ignored instructions\n"));
14003 fprintf (stream, _("\
7c5c05ef
L
14004 -mmnemonic=[att|intel] "));
14005 if (SYSV386_COMPAT)
14006 fprintf (stream, _("(default: att)\n"));
14007 else
14008 fprintf (stream, _("(default: intel)\n"));
14009 fprintf (stream, _("\
14010 use AT&T/Intel mnemonic\n"));
ba104c83 14011 fprintf (stream, _("\
7c5c05ef
L
14012 -msyntax=[att|intel] (default: att)\n\
14013 use AT&T/Intel syntax\n"));
ba104c83
L
14014 fprintf (stream, _("\
14015 -mindex-reg support pseudo index registers\n"));
14016 fprintf (stream, _("\
14017 -mnaked-reg don't require `%%' prefix for registers\n"));
14018 fprintf (stream, _("\
7e8b059b 14019 -madd-bnd-prefix add BND prefix for all valid branches\n"));
b4a3a7b4 14020#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8dcea932
L
14021 fprintf (stream, _("\
14022 -mshared disable branch optimization for shared code\n"));
b4a3a7b4
L
14023 fprintf (stream, _("\
14024 -mx86-used-note=[no|yes] "));
14025 if (DEFAULT_X86_USED_NOTE)
14026 fprintf (stream, _("(default: yes)\n"));
14027 else
14028 fprintf (stream, _("(default: no)\n"));
14029 fprintf (stream, _("\
14030 generate x86 used ISA and feature properties\n"));
14031#endif
14032#if defined (TE_PE) || defined (TE_PEP)
167ad85b
TG
14033 fprintf (stream, _("\
14034 -mbig-obj generate big object files\n"));
14035#endif
d022bddd 14036 fprintf (stream, _("\
7c5c05ef 14037 -momit-lock-prefix=[no|yes] (default: no)\n\
d022bddd 14038 strip all lock prefixes\n"));
5db04b09 14039 fprintf (stream, _("\
7c5c05ef 14040 -mfence-as-lock-add=[no|yes] (default: no)\n\
e4e00185
AS
14041 encode lfence, mfence and sfence as\n\
14042 lock addl $0x0, (%%{re}sp)\n"));
14043 fprintf (stream, _("\
7c5c05ef
L
14044 -mrelax-relocations=[no|yes] "));
14045 if (DEFAULT_GENERATE_X86_RELAX_RELOCATIONS)
14046 fprintf (stream, _("(default: yes)\n"));
14047 else
14048 fprintf (stream, _("(default: no)\n"));
14049 fprintf (stream, _("\
0cb4071e
L
14050 generate relax relocations\n"));
14051 fprintf (stream, _("\
e379e5f3
L
14052 -malign-branch-boundary=NUM (default: 0)\n\
14053 align branches within NUM byte boundary\n"));
14054 fprintf (stream, _("\
14055 -malign-branch=TYPE[+TYPE...] (default: jcc+fused+jmp)\n\
14056 TYPE is combination of jcc, fused, jmp, call, ret,\n\
14057 indirect\n\
14058 specify types of branches to align\n"));
14059 fprintf (stream, _("\
14060 -malign-branch-prefix-size=NUM (default: 5)\n\
14061 align branches with NUM prefixes per instruction\n"));
14062 fprintf (stream, _("\
76cf450b
L
14063 -mbranches-within-32B-boundaries\n\
14064 align branches within 32 byte boundary\n"));
14065 fprintf (stream, _("\
ae531041
L
14066 -mlfence-after-load=[no|yes] (default: no)\n\
14067 generate lfence after load\n"));
14068 fprintf (stream, _("\
14069 -mlfence-before-indirect-branch=[none|all|register|memory] (default: none)\n\
14070 generate lfence before indirect near branch\n"));
14071 fprintf (stream, _("\
a09f656b 14072 -mlfence-before-ret=[none|or|not|shl|yes] (default: none)\n\
ae531041
L
14073 generate lfence before ret\n"));
14074 fprintf (stream, _("\
7c5c05ef 14075 -mamd64 accept only AMD64 ISA [default]\n"));
5db04b09
L
14076 fprintf (stream, _("\
14077 -mintel64 accept only Intel64 ISA\n"));
252b5132
RH
14078}
14079
3e73aa7c 14080#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
321098a5 14081 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
e57f8c65 14082 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
252b5132
RH
14083
14084/* Pick the target format to use. */
14085
47926f60 14086const char *
e3bb37b5 14087i386_target_format (void)
252b5132 14088{
d34049e8 14089 if (startswith (default_arch, "x86_64"))
351f65ca
L
14090 {
14091 update_code_flag (CODE_64BIT, 1);
14092 if (default_arch[6] == '\0')
7f56bc95 14093 x86_elf_abi = X86_64_ABI;
351f65ca 14094 else
7f56bc95 14095 x86_elf_abi = X86_64_X32_ABI;
351f65ca 14096 }
3e73aa7c 14097 else if (!strcmp (default_arch, "i386"))
78f12dd3 14098 update_code_flag (CODE_32BIT, 1);
5197d474
L
14099 else if (!strcmp (default_arch, "iamcu"))
14100 {
14101 update_code_flag (CODE_32BIT, 1);
14102 if (cpu_arch_isa == PROCESSOR_UNKNOWN)
14103 {
14104 static const i386_cpu_flags iamcu_flags = CPU_IAMCU_FLAGS;
14105 cpu_arch_name = "iamcu";
d92c7521 14106 free (cpu_sub_arch_name);
5197d474
L
14107 cpu_sub_arch_name = NULL;
14108 cpu_arch_flags = iamcu_flags;
14109 cpu_arch_isa = PROCESSOR_IAMCU;
14110 cpu_arch_isa_flags = iamcu_flags;
14111 if (!cpu_arch_tune_set)
14112 {
14113 cpu_arch_tune = cpu_arch_isa;
14114 cpu_arch_tune_flags = cpu_arch_isa_flags;
14115 }
14116 }
8d471ec1 14117 else if (cpu_arch_isa != PROCESSOR_IAMCU)
5197d474
L
14118 as_fatal (_("Intel MCU doesn't support `%s' architecture"),
14119 cpu_arch_name);
14120 }
3e73aa7c 14121 else
2b5d6a91 14122 as_fatal (_("unknown architecture"));
89507696
JB
14123
14124 if (cpu_flags_all_zero (&cpu_arch_isa_flags))
ae89daec 14125 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
89507696 14126 if (cpu_flags_all_zero (&cpu_arch_tune_flags))
ae89daec 14127 cpu_arch_tune_flags = cpu_arch[flag_code == CODE_64BIT].enable;
89507696 14128
252b5132
RH
14129 switch (OUTPUT_FLAVOR)
14130 {
9384f2ff 14131#if defined (OBJ_MAYBE_AOUT) || defined (OBJ_AOUT)
4c63da97 14132 case bfd_target_aout_flavour:
47926f60 14133 return AOUT_TARGET_FORMAT;
4c63da97 14134#endif
9384f2ff
AM
14135#if defined (OBJ_MAYBE_COFF) || defined (OBJ_COFF)
14136# if defined (TE_PE) || defined (TE_PEP)
14137 case bfd_target_coff_flavour:
167ad85b 14138 if (flag_code == CODE_64BIT)
eb19308f
JB
14139 {
14140 object_64bit = 1;
14141 return use_big_obj ? "pe-bigobj-x86-64" : "pe-x86-64";
14142 }
14143 return use_big_obj ? "pe-bigobj-i386" : "pe-i386";
9384f2ff 14144# elif defined (TE_GO32)
0561d57c
JK
14145 case bfd_target_coff_flavour:
14146 return "coff-go32";
9384f2ff 14147# else
252b5132
RH
14148 case bfd_target_coff_flavour:
14149 return "coff-i386";
9384f2ff 14150# endif
4c63da97 14151#endif
3e73aa7c 14152#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
252b5132 14153 case bfd_target_elf_flavour:
3e73aa7c 14154 {
351f65ca
L
14155 const char *format;
14156
14157 switch (x86_elf_abi)
4fa24527 14158 {
351f65ca
L
14159 default:
14160 format = ELF_TARGET_FORMAT;
e379e5f3
L
14161#ifndef TE_SOLARIS
14162 tls_get_addr = "___tls_get_addr";
14163#endif
351f65ca 14164 break;
7f56bc95 14165 case X86_64_ABI:
351f65ca 14166 use_rela_relocations = 1;
4fa24527 14167 object_64bit = 1;
e379e5f3
L
14168#ifndef TE_SOLARIS
14169 tls_get_addr = "__tls_get_addr";
14170#endif
351f65ca
L
14171 format = ELF_TARGET_FORMAT64;
14172 break;
7f56bc95 14173 case X86_64_X32_ABI:
4fa24527 14174 use_rela_relocations = 1;
351f65ca 14175 object_64bit = 1;
e379e5f3
L
14176#ifndef TE_SOLARIS
14177 tls_get_addr = "__tls_get_addr";
14178#endif
862be3fb 14179 disallow_64bit_reloc = 1;
351f65ca
L
14180 format = ELF_TARGET_FORMAT32;
14181 break;
4fa24527 14182 }
c085ab00 14183 if (cpu_arch_isa == PROCESSOR_IAMCU)
81486035
L
14184 {
14185 if (x86_elf_abi != I386_ABI)
14186 as_fatal (_("Intel MCU is 32bit only"));
14187 return ELF_TARGET_IAMCU_FORMAT;
14188 }
8a9036a4 14189 else
351f65ca 14190 return format;
3e73aa7c 14191 }
e57f8c65
TG
14192#endif
14193#if defined (OBJ_MACH_O)
14194 case bfd_target_mach_o_flavour:
d382c579
TG
14195 if (flag_code == CODE_64BIT)
14196 {
14197 use_rela_relocations = 1;
14198 object_64bit = 1;
14199 return "mach-o-x86-64";
14200 }
14201 else
14202 return "mach-o-i386";
4c63da97 14203#endif
252b5132
RH
14204 default:
14205 abort ();
14206 return NULL;
14207 }
14208}
14209
47926f60 14210#endif /* OBJ_MAYBE_ more than one */
252b5132 14211\f
252b5132 14212symbolS *
7016a5d5 14213md_undefined_symbol (char *name)
252b5132 14214{
18dc2407
ILT
14215 if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
14216 && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
14217 && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
14218 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
24eab124
AM
14219 {
14220 if (!GOT_symbol)
14221 {
14222 if (symbol_find (name))
14223 as_bad (_("GOT already in symbol table"));
14224 GOT_symbol = symbol_new (name, undefined_section,
e01e1cee 14225 &zero_address_frag, 0);
24eab124
AM
14226 };
14227 return GOT_symbol;
14228 }
252b5132
RH
14229 return 0;
14230}
14231
14232/* Round up a section size to the appropriate boundary. */
47926f60 14233
252b5132 14234valueT
7016a5d5 14235md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
252b5132 14236{
4c63da97
AM
14237#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
14238 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
14239 {
14240 /* For a.out, force the section size to be aligned. If we don't do
14241 this, BFD will align it for us, but it will not write out the
14242 final bytes of the section. This may be a bug in BFD, but it is
14243 easier to fix it here since that is how the other a.out targets
14244 work. */
14245 int align;
14246
fd361982 14247 align = bfd_section_alignment (segment);
8d3842cd 14248 size = ((size + (1 << align) - 1) & (-((valueT) 1 << align)));
4c63da97 14249 }
252b5132
RH
14250#endif
14251
14252 return size;
14253}
14254
14255/* On the i386, PC-relative offsets are relative to the start of the
14256 next instruction. That is, the address of the offset, plus its
14257 size, since the offset is always the last part of the insn. */
14258
14259long
e3bb37b5 14260md_pcrel_from (fixS *fixP)
252b5132
RH
14261{
14262 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
14263}
14264
14265#ifndef I386COFF
14266
14267static void
e3bb37b5 14268s_bss (int ignore ATTRIBUTE_UNUSED)
252b5132 14269{
29b0f896 14270 int temp;
252b5132 14271
8a75718c
JB
14272#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14273 if (IS_ELF)
14274 obj_elf_section_change_hook ();
14275#endif
252b5132
RH
14276 temp = get_absolute_expression ();
14277 subseg_set (bss_section, (subsegT) temp);
14278 demand_empty_rest_of_line ();
14279}
14280
14281#endif
14282
e379e5f3
L
14283/* Remember constant directive. */
14284
14285void
14286i386_cons_align (int ignore ATTRIBUTE_UNUSED)
14287{
14288 if (last_insn.kind != last_insn_directive
14289 && (bfd_section_flags (now_seg) & SEC_CODE))
14290 {
14291 last_insn.seg = now_seg;
14292 last_insn.kind = last_insn_directive;
14293 last_insn.name = "constant directive";
14294 last_insn.file = as_where (&last_insn.line);
ae531041
L
14295 if (lfence_before_ret != lfence_before_ret_none)
14296 {
14297 if (lfence_before_indirect_branch != lfence_branch_none)
14298 as_warn (_("constant directive skips -mlfence-before-ret "
14299 "and -mlfence-before-indirect-branch"));
14300 else
14301 as_warn (_("constant directive skips -mlfence-before-ret"));
14302 }
14303 else if (lfence_before_indirect_branch != lfence_branch_none)
14304 as_warn (_("constant directive skips -mlfence-before-indirect-branch"));
e379e5f3
L
14305 }
14306}
14307
3abbafc2 14308int
e3bb37b5 14309i386_validate_fix (fixS *fixp)
252b5132 14310{
e52a16f2
JB
14311 if (fixp->fx_addsy && S_GET_SEGMENT(fixp->fx_addsy) == reg_section)
14312 {
14313 reloc_howto_type *howto;
14314
14315 howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
14316 as_bad_where (fixp->fx_file, fixp->fx_line,
14317 _("invalid %s relocation against register"),
14318 howto ? howto->name : "<unknown>");
14319 return 0;
14320 }
14321
3abbafc2
JB
14322#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14323 if (fixp->fx_r_type == BFD_RELOC_SIZE32
14324 || fixp->fx_r_type == BFD_RELOC_SIZE64)
14325 return IS_ELF && fixp->fx_addsy
14326 && (!S_IS_DEFINED (fixp->fx_addsy)
14327 || S_IS_EXTERNAL (fixp->fx_addsy));
14328#endif
14329
02a86693 14330 if (fixp->fx_subsy)
252b5132 14331 {
02a86693 14332 if (fixp->fx_subsy == GOT_symbol)
23df1078 14333 {
02a86693
L
14334 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
14335 {
14336 if (!object_64bit)
14337 abort ();
14338#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14339 if (fixp->fx_tcbit2)
56ceb5b5
L
14340 fixp->fx_r_type = (fixp->fx_tcbit
14341 ? BFD_RELOC_X86_64_REX_GOTPCRELX
14342 : BFD_RELOC_X86_64_GOTPCRELX);
02a86693
L
14343 else
14344#endif
14345 fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
14346 }
d6ab8113 14347 else
02a86693
L
14348 {
14349 if (!object_64bit)
14350 fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
14351 else
14352 fixp->fx_r_type = BFD_RELOC_X86_64_GOTOFF64;
14353 }
14354 fixp->fx_subsy = 0;
23df1078 14355 }
252b5132 14356 }
02a86693 14357#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2585b7a5 14358 else
02a86693 14359 {
2585b7a5
L
14360 /* NB: Commit 292676c1 resolved PLT32 reloc aganst local symbol
14361 to section. Since PLT32 relocation must be against symbols,
14362 turn such PLT32 relocation into PC32 relocation. */
14363 if (fixp->fx_addsy
14364 && (fixp->fx_r_type == BFD_RELOC_386_PLT32
14365 || fixp->fx_r_type == BFD_RELOC_X86_64_PLT32)
14366 && symbol_section_p (fixp->fx_addsy))
14367 fixp->fx_r_type = BFD_RELOC_32_PCREL;
14368 if (!object_64bit)
14369 {
14370 if (fixp->fx_r_type == BFD_RELOC_386_GOT32
14371 && fixp->fx_tcbit2)
14372 fixp->fx_r_type = BFD_RELOC_386_GOT32X;
14373 }
02a86693
L
14374 }
14375#endif
3abbafc2
JB
14376
14377 return 1;
252b5132
RH
14378}
14379
252b5132 14380arelent *
7016a5d5 14381tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
14382{
14383 arelent *rel;
14384 bfd_reloc_code_real_type code;
14385
14386 switch (fixp->fx_r_type)
14387 {
8ce3d284 14388#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3abbafc2
JB
14389 symbolS *sym;
14390
8fd4256d
L
14391 case BFD_RELOC_SIZE32:
14392 case BFD_RELOC_SIZE64:
3abbafc2
JB
14393 if (fixp->fx_addsy
14394 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))
14395 && (!fixp->fx_subsy
14396 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))))
14397 sym = fixp->fx_addsy;
14398 else if (fixp->fx_subsy
14399 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))
14400 && (!fixp->fx_addsy
14401 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))))
14402 sym = fixp->fx_subsy;
14403 else
14404 sym = NULL;
14405 if (IS_ELF && sym && S_IS_DEFINED (sym) && !S_IS_EXTERNAL (sym))
8fd4256d
L
14406 {
14407 /* Resolve size relocation against local symbol to size of
14408 the symbol plus addend. */
3abbafc2 14409 valueT value = S_GET_SIZE (sym);
44f87162 14410
3abbafc2
JB
14411 if (symbol_get_bfdsym (sym)->flags & BSF_SECTION_SYM)
14412 value = bfd_section_size (S_GET_SEGMENT (sym));
14413 if (sym == fixp->fx_subsy)
14414 {
14415 value = -value;
14416 if (fixp->fx_addsy)
14417 value += S_GET_VALUE (fixp->fx_addsy);
14418 }
14419 else if (fixp->fx_subsy)
14420 value -= S_GET_VALUE (fixp->fx_subsy);
44f87162 14421 value += fixp->fx_offset;
8fd4256d 14422 if (fixp->fx_r_type == BFD_RELOC_SIZE32
d965814f 14423 && object_64bit
8fd4256d
L
14424 && !fits_in_unsigned_long (value))
14425 as_bad_where (fixp->fx_file, fixp->fx_line,
14426 _("symbol size computation overflow"));
14427 fixp->fx_addsy = NULL;
14428 fixp->fx_subsy = NULL;
14429 md_apply_fix (fixp, (valueT *) &value, NULL);
14430 return NULL;
14431 }
3abbafc2
JB
14432 if (!fixp->fx_addsy || fixp->fx_subsy)
14433 {
14434 as_bad_where (fixp->fx_file, fixp->fx_line,
14435 "unsupported expression involving @size");
14436 return NULL;
14437 }
8ce3d284 14438#endif
1a0670f3 14439 /* Fall through. */
8fd4256d 14440
3e73aa7c
JH
14441 case BFD_RELOC_X86_64_PLT32:
14442 case BFD_RELOC_X86_64_GOT32:
14443 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
14444 case BFD_RELOC_X86_64_GOTPCRELX:
14445 case BFD_RELOC_X86_64_REX_GOTPCRELX:
252b5132
RH
14446 case BFD_RELOC_386_PLT32:
14447 case BFD_RELOC_386_GOT32:
02a86693 14448 case BFD_RELOC_386_GOT32X:
252b5132
RH
14449 case BFD_RELOC_386_GOTOFF:
14450 case BFD_RELOC_386_GOTPC:
13ae64f3
JJ
14451 case BFD_RELOC_386_TLS_GD:
14452 case BFD_RELOC_386_TLS_LDM:
14453 case BFD_RELOC_386_TLS_LDO_32:
14454 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
14455 case BFD_RELOC_386_TLS_IE:
14456 case BFD_RELOC_386_TLS_GOTIE:
13ae64f3
JJ
14457 case BFD_RELOC_386_TLS_LE_32:
14458 case BFD_RELOC_386_TLS_LE:
67a4f2b7
AO
14459 case BFD_RELOC_386_TLS_GOTDESC:
14460 case BFD_RELOC_386_TLS_DESC_CALL:
bffbf940
JJ
14461 case BFD_RELOC_X86_64_TLSGD:
14462 case BFD_RELOC_X86_64_TLSLD:
14463 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 14464 case BFD_RELOC_X86_64_DTPOFF64:
bffbf940
JJ
14465 case BFD_RELOC_X86_64_GOTTPOFF:
14466 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113
JB
14467 case BFD_RELOC_X86_64_TPOFF64:
14468 case BFD_RELOC_X86_64_GOTOFF64:
14469 case BFD_RELOC_X86_64_GOTPC32:
7b81dfbb
AJ
14470 case BFD_RELOC_X86_64_GOT64:
14471 case BFD_RELOC_X86_64_GOTPCREL64:
14472 case BFD_RELOC_X86_64_GOTPC64:
14473 case BFD_RELOC_X86_64_GOTPLT64:
14474 case BFD_RELOC_X86_64_PLTOFF64:
67a4f2b7
AO
14475 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14476 case BFD_RELOC_X86_64_TLSDESC_CALL:
252b5132
RH
14477 case BFD_RELOC_RVA:
14478 case BFD_RELOC_VTABLE_ENTRY:
14479 case BFD_RELOC_VTABLE_INHERIT:
6482c264
NC
14480#ifdef TE_PE
14481 case BFD_RELOC_32_SECREL:
145667f8 14482 case BFD_RELOC_16_SECIDX:
6482c264 14483#endif
252b5132
RH
14484 code = fixp->fx_r_type;
14485 break;
dbbaec26
L
14486 case BFD_RELOC_X86_64_32S:
14487 if (!fixp->fx_pcrel)
14488 {
14489 /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32. */
14490 code = fixp->fx_r_type;
14491 break;
14492 }
1a0670f3 14493 /* Fall through. */
252b5132 14494 default:
93382f6d 14495 if (fixp->fx_pcrel)
252b5132 14496 {
93382f6d
AM
14497 switch (fixp->fx_size)
14498 {
14499 default:
b091f402
AM
14500 as_bad_where (fixp->fx_file, fixp->fx_line,
14501 _("can not do %d byte pc-relative relocation"),
14502 fixp->fx_size);
93382f6d
AM
14503 code = BFD_RELOC_32_PCREL;
14504 break;
14505 case 1: code = BFD_RELOC_8_PCREL; break;
14506 case 2: code = BFD_RELOC_16_PCREL; break;
d258b828 14507 case 4: code = BFD_RELOC_32_PCREL; break;
d6ab8113
JB
14508#ifdef BFD64
14509 case 8: code = BFD_RELOC_64_PCREL; break;
14510#endif
93382f6d
AM
14511 }
14512 }
14513 else
14514 {
14515 switch (fixp->fx_size)
14516 {
14517 default:
b091f402
AM
14518 as_bad_where (fixp->fx_file, fixp->fx_line,
14519 _("can not do %d byte relocation"),
14520 fixp->fx_size);
93382f6d
AM
14521 code = BFD_RELOC_32;
14522 break;
14523 case 1: code = BFD_RELOC_8; break;
14524 case 2: code = BFD_RELOC_16; break;
14525 case 4: code = BFD_RELOC_32; break;
937149dd 14526#ifdef BFD64
3e73aa7c 14527 case 8: code = BFD_RELOC_64; break;
937149dd 14528#endif
93382f6d 14529 }
252b5132
RH
14530 }
14531 break;
14532 }
252b5132 14533
d182319b
JB
14534 if ((code == BFD_RELOC_32
14535 || code == BFD_RELOC_32_PCREL
14536 || code == BFD_RELOC_X86_64_32S)
252b5132
RH
14537 && GOT_symbol
14538 && fixp->fx_addsy == GOT_symbol)
3e73aa7c 14539 {
4fa24527 14540 if (!object_64bit)
d6ab8113
JB
14541 code = BFD_RELOC_386_GOTPC;
14542 else
14543 code = BFD_RELOC_X86_64_GOTPC32;
3e73aa7c 14544 }
7b81dfbb
AJ
14545 if ((code == BFD_RELOC_64 || code == BFD_RELOC_64_PCREL)
14546 && GOT_symbol
14547 && fixp->fx_addsy == GOT_symbol)
14548 {
14549 code = BFD_RELOC_X86_64_GOTPC64;
14550 }
252b5132 14551
add39d23
TS
14552 rel = XNEW (arelent);
14553 rel->sym_ptr_ptr = XNEW (asymbol *);
49309057 14554 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
14555
14556 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
c87db184 14557
3e73aa7c
JH
14558 if (!use_rela_relocations)
14559 {
14560 /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
14561 vtable entry to be used in the relocation's section offset. */
14562 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
14563 rel->address = fixp->fx_offset;
fbeb56a4
DK
14564#if defined (OBJ_COFF) && defined (TE_PE)
14565 else if (fixp->fx_addsy && S_IS_WEAK (fixp->fx_addsy))
14566 rel->addend = fixp->fx_addnumber - (S_GET_VALUE (fixp->fx_addsy) * 2);
14567 else
14568#endif
c6682705 14569 rel->addend = 0;
3e73aa7c
JH
14570 }
14571 /* Use the rela in 64bit mode. */
252b5132 14572 else
3e73aa7c 14573 {
862be3fb
L
14574 if (disallow_64bit_reloc)
14575 switch (code)
14576 {
862be3fb
L
14577 case BFD_RELOC_X86_64_DTPOFF64:
14578 case BFD_RELOC_X86_64_TPOFF64:
14579 case BFD_RELOC_64_PCREL:
14580 case BFD_RELOC_X86_64_GOTOFF64:
14581 case BFD_RELOC_X86_64_GOT64:
14582 case BFD_RELOC_X86_64_GOTPCREL64:
14583 case BFD_RELOC_X86_64_GOTPC64:
14584 case BFD_RELOC_X86_64_GOTPLT64:
14585 case BFD_RELOC_X86_64_PLTOFF64:
14586 as_bad_where (fixp->fx_file, fixp->fx_line,
14587 _("cannot represent relocation type %s in x32 mode"),
14588 bfd_get_reloc_code_name (code));
14589 break;
14590 default:
14591 break;
14592 }
14593
062cd5e7
AS
14594 if (!fixp->fx_pcrel)
14595 rel->addend = fixp->fx_offset;
14596 else
14597 switch (code)
14598 {
14599 case BFD_RELOC_X86_64_PLT32:
14600 case BFD_RELOC_X86_64_GOT32:
14601 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
14602 case BFD_RELOC_X86_64_GOTPCRELX:
14603 case BFD_RELOC_X86_64_REX_GOTPCRELX:
bffbf940
JJ
14604 case BFD_RELOC_X86_64_TLSGD:
14605 case BFD_RELOC_X86_64_TLSLD:
14606 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7
AO
14607 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14608 case BFD_RELOC_X86_64_TLSDESC_CALL:
062cd5e7
AS
14609 rel->addend = fixp->fx_offset - fixp->fx_size;
14610 break;
14611 default:
14612 rel->addend = (section->vma
14613 - fixp->fx_size
14614 + fixp->fx_addnumber
14615 + md_pcrel_from (fixp));
14616 break;
14617 }
3e73aa7c
JH
14618 }
14619
252b5132
RH
14620 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
14621 if (rel->howto == NULL)
14622 {
14623 as_bad_where (fixp->fx_file, fixp->fx_line,
d0b47220 14624 _("cannot represent relocation type %s"),
252b5132
RH
14625 bfd_get_reloc_code_name (code));
14626 /* Set howto to a garbage value so that we can keep going. */
14627 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
9c2799c2 14628 gas_assert (rel->howto != NULL);
252b5132
RH
14629 }
14630
14631 return rel;
14632}
14633
ee86248c 14634#include "tc-i386-intel.c"
54cfded0 14635
a60de03c
JB
14636void
14637tc_x86_parse_to_dw2regnum (expressionS *exp)
54cfded0 14638{
a60de03c
JB
14639 int saved_naked_reg;
14640 char saved_register_dot;
54cfded0 14641
a60de03c
JB
14642 saved_naked_reg = allow_naked_reg;
14643 allow_naked_reg = 1;
14644 saved_register_dot = register_chars['.'];
14645 register_chars['.'] = '.';
14646 allow_pseudo_reg = 1;
14647 expression_and_evaluate (exp);
14648 allow_pseudo_reg = 0;
14649 register_chars['.'] = saved_register_dot;
14650 allow_naked_reg = saved_naked_reg;
14651
e96d56a1 14652 if (exp->X_op == O_register && exp->X_add_number >= 0)
54cfded0 14653 {
a60de03c
JB
14654 if ((addressT) exp->X_add_number < i386_regtab_size)
14655 {
14656 exp->X_op = O_constant;
14657 exp->X_add_number = i386_regtab[exp->X_add_number]
14658 .dw2_regnum[flag_code >> 1];
14659 }
14660 else
14661 exp->X_op = O_illegal;
54cfded0 14662 }
54cfded0
AM
14663}
14664
14665void
14666tc_x86_frame_initial_instructions (void)
14667{
a60de03c
JB
14668 static unsigned int sp_regno[2];
14669
14670 if (!sp_regno[flag_code >> 1])
14671 {
14672 char *saved_input = input_line_pointer;
14673 char sp[][4] = {"esp", "rsp"};
14674 expressionS exp;
a4447b93 14675
a60de03c
JB
14676 input_line_pointer = sp[flag_code >> 1];
14677 tc_x86_parse_to_dw2regnum (&exp);
9c2799c2 14678 gas_assert (exp.X_op == O_constant);
a60de03c
JB
14679 sp_regno[flag_code >> 1] = exp.X_add_number;
14680 input_line_pointer = saved_input;
14681 }
a4447b93 14682
61ff971f
L
14683 cfi_add_CFA_def_cfa (sp_regno[flag_code >> 1], -x86_cie_data_alignment);
14684 cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
54cfded0 14685}
d2b2c203 14686
d7921315
L
14687int
14688x86_dwarf2_addr_size (void)
14689{
14690#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
14691 if (x86_elf_abi == X86_64_X32_ABI)
14692 return 4;
14693#endif
14694 return bfd_arch_bits_per_address (stdoutput) / 8;
14695}
14696
d2b2c203
DJ
14697int
14698i386_elf_section_type (const char *str, size_t len)
14699{
14700 if (flag_code == CODE_64BIT
14701 && len == sizeof ("unwind") - 1
d34049e8 14702 && startswith (str, "unwind"))
d2b2c203
DJ
14703 return SHT_X86_64_UNWIND;
14704
14705 return -1;
14706}
bb41ade5 14707
ad5fec3b
EB
14708#ifdef TE_SOLARIS
14709void
14710i386_solaris_fix_up_eh_frame (segT sec)
14711{
14712 if (flag_code == CODE_64BIT)
14713 elf_section_type (sec) = SHT_X86_64_UNWIND;
14714}
14715#endif
14716
bb41ade5
AM
14717#ifdef TE_PE
14718void
14719tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
14720{
91d6fa6a 14721 expressionS exp;
bb41ade5 14722
91d6fa6a
NC
14723 exp.X_op = O_secrel;
14724 exp.X_add_symbol = symbol;
14725 exp.X_add_number = 0;
14726 emit_expr (&exp, size);
bb41ade5
AM
14727}
14728#endif
3b22753a
L
14729
14730#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14731/* For ELF on x86-64, add support for SHF_X86_64_LARGE. */
14732
01e1a5bc 14733bfd_vma
6d4af3c2 14734x86_64_section_letter (int letter, const char **ptr_msg)
3b22753a
L
14735{
14736 if (flag_code == CODE_64BIT)
14737 {
14738 if (letter == 'l')
14739 return SHF_X86_64_LARGE;
14740
8f3bae45 14741 *ptr_msg = _("bad .section directive: want a,l,w,x,M,S,G,T in string");
64e74474 14742 }
3b22753a 14743 else
8f3bae45 14744 *ptr_msg = _("bad .section directive: want a,w,x,M,S,G,T in string");
3b22753a
L
14745 return -1;
14746}
14747
01e1a5bc 14748bfd_vma
3b22753a
L
14749x86_64_section_word (char *str, size_t len)
14750{
08dedd66 14751 if (len == 5 && flag_code == CODE_64BIT && startswith (str, "large"))
3b22753a
L
14752 return SHF_X86_64_LARGE;
14753
14754 return -1;
14755}
14756
14757static void
14758handle_large_common (int small ATTRIBUTE_UNUSED)
14759{
14760 if (flag_code != CODE_64BIT)
14761 {
14762 s_comm_internal (0, elf_common_parse);
14763 as_warn (_(".largecomm supported only in 64bit mode, producing .comm"));
14764 }
14765 else
14766 {
14767 static segT lbss_section;
14768 asection *saved_com_section_ptr = elf_com_section_ptr;
14769 asection *saved_bss_section = bss_section;
14770
14771 if (lbss_section == NULL)
14772 {
14773 flagword applicable;
14774 segT seg = now_seg;
14775 subsegT subseg = now_subseg;
14776
14777 /* The .lbss section is for local .largecomm symbols. */
14778 lbss_section = subseg_new (".lbss", 0);
14779 applicable = bfd_applicable_section_flags (stdoutput);
fd361982 14780 bfd_set_section_flags (lbss_section, applicable & SEC_ALLOC);
3b22753a
L
14781 seg_info (lbss_section)->bss = 1;
14782
14783 subseg_set (seg, subseg);
14784 }
14785
14786 elf_com_section_ptr = &_bfd_elf_large_com_section;
14787 bss_section = lbss_section;
14788
14789 s_comm_internal (0, elf_common_parse);
14790
14791 elf_com_section_ptr = saved_com_section_ptr;
14792 bss_section = saved_bss_section;
14793 }
14794}
14795#endif /* OBJ_ELF || OBJ_MAYBE_ELF */