]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/config/tc-i386.c
x86: parse VEX and alike specifiers for .insn
[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
d0c2e3ec
JB
310 /* .insn allows for reserved opcode spaces. */
311 unsigned char insn_opcode_space;
312
50128d0c 313 /* Register is in low 3 bits of opcode. */
5b7c81bd 314 bool short_form;
50128d0c 315
6f2f06be 316 /* The operand to a branch insn indicates an absolute branch. */
5b7c81bd 317 bool jumpabsolute;
6f2f06be 318
a4d3acd2
JB
319 /* The operand to a branch insn indicates a far branch. */
320 bool far_branch;
321
9373f275
L
322 /* There is a memory operand of (%dx) which should be only used
323 with input/output instructions. */
324 bool input_output_operand;
325
921eafea
L
326 /* Extended states. */
327 enum
328 {
329 /* Use MMX state. */
330 xstate_mmx = 1 << 0,
331 /* Use XMM state. */
332 xstate_xmm = 1 << 1,
333 /* Use YMM state. */
334 xstate_ymm = 1 << 2 | xstate_xmm,
335 /* Use ZMM state. */
336 xstate_zmm = 1 << 3 | xstate_ymm,
337 /* Use TMM state. */
32930e4e
L
338 xstate_tmm = 1 << 4,
339 /* Use MASK state. */
340 xstate_mask = 1 << 5
921eafea 341 } xstate;
260cd341 342
e379e5f3 343 /* Has GOTPC or TLS relocation. */
5b7c81bd 344 bool has_gotpc_tls_reloc;
e379e5f3 345
252b5132 346 /* RM and SIB are the modrm byte and the sib byte where the
c1e679ec 347 addressing modes of this insn are encoded. */
252b5132 348 modrm_byte rm;
3e73aa7c 349 rex_byte rex;
43234a1e 350 rex_byte vrex;
252b5132 351 sib_byte sib;
c0f3af97 352 vex_prefix vex;
b6169b20 353
6225c532
JB
354 /* Masking attributes.
355
356 The struct describes masking, applied to OPERAND in the instruction.
357 REG is a pointer to the corresponding mask register. ZEROING tells
358 whether merging or zeroing mask is used. */
359 struct Mask_Operation
360 {
361 const reg_entry *reg;
362 unsigned int zeroing;
363 /* The operand where this operation is associated. */
364 unsigned int operand;
365 } mask;
43234a1e
L
366
367 /* Rounding control and SAE attributes. */
ca5312a2
JB
368 struct RC_Operation
369 {
370 enum rc_type
371 {
372 rc_none = -1,
373 rne,
374 rd,
375 ru,
376 rz,
377 saeonly
378 } type;
7063667e
JB
379 /* In Intel syntax the operand modifier form is supposed to be used, but
380 we continue to accept the immediate forms as well. */
381 bool modifier;
ca5312a2 382 } rounding;
43234a1e 383
5273a3cd
JB
384 /* Broadcasting attributes.
385
386 The struct describes broadcasting, applied to OPERAND. TYPE is
387 expresses the broadcast factor. */
388 struct Broadcast_Operation
389 {
0cc78721 390 /* Type of broadcast: {1to2}, {1to4}, {1to8}, {1to16} or {1to32}. */
5273a3cd
JB
391 unsigned int type;
392
393 /* Index of broadcasted operand. */
394 unsigned int operand;
395
396 /* Number of bytes to broadcast. */
397 unsigned int bytes;
398 } broadcast;
43234a1e
L
399
400 /* Compressed disp8*N attribute. */
401 unsigned int memshift;
402
86fa6981
L
403 /* Prefer load or store in encoding. */
404 enum
405 {
406 dir_encoding_default = 0,
407 dir_encoding_load,
64c49ab3
JB
408 dir_encoding_store,
409 dir_encoding_swap
86fa6981 410 } dir_encoding;
891edac4 411
41eb8e88 412 /* Prefer 8bit, 16bit, 32bit displacement in encoding. */
a501d77e
L
413 enum
414 {
415 disp_encoding_default = 0,
416 disp_encoding_8bit,
41eb8e88 417 disp_encoding_16bit,
a501d77e
L
418 disp_encoding_32bit
419 } disp_encoding;
f8a5c266 420
6b6b6807 421 /* Prefer the REX byte in encoding. */
5b7c81bd 422 bool rex_encoding;
6b6b6807 423
b6f8c7c4 424 /* Disable instruction size optimization. */
5b7c81bd 425 bool no_optimize;
b6f8c7c4 426
86fa6981
L
427 /* How to encode vector instructions. */
428 enum
429 {
430 vex_encoding_default = 0,
42e04b36 431 vex_encoding_vex,
86fa6981 432 vex_encoding_vex3,
da4977e0
JB
433 vex_encoding_evex,
434 vex_encoding_error
86fa6981
L
435 } vec_encoding;
436
d5de92cf
L
437 /* REP prefix. */
438 const char *rep_prefix;
439
165de32a
L
440 /* HLE prefix. */
441 const char *hle_prefix;
42164a71 442
7e8b059b
L
443 /* Have BND prefix. */
444 const char *bnd_prefix;
445
04ef582a
L
446 /* Have NOTRACK prefix. */
447 const char *notrack_prefix;
448
891edac4 449 /* Error message. */
a65babc9 450 enum i386_error error;
252b5132
RH
451 };
452
453typedef struct _i386_insn i386_insn;
454
43234a1e
L
455/* Link RC type with corresponding string, that'll be looked for in
456 asm. */
457struct RC_name
458{
459 enum rc_type type;
460 const char *name;
461 unsigned int len;
462};
463
464static const struct RC_name RC_NamesTable[] =
465{
466 { rne, STRING_COMMA_LEN ("rn-sae") },
467 { rd, STRING_COMMA_LEN ("rd-sae") },
468 { ru, STRING_COMMA_LEN ("ru-sae") },
469 { rz, STRING_COMMA_LEN ("rz-sae") },
470 { saeonly, STRING_COMMA_LEN ("sae") },
471};
472
3bfea8ba
L
473/* To be indexed by segment register number. */
474static const unsigned char i386_seg_prefixes[] = {
475 ES_PREFIX_OPCODE,
476 CS_PREFIX_OPCODE,
477 SS_PREFIX_OPCODE,
478 DS_PREFIX_OPCODE,
479 FS_PREFIX_OPCODE,
480 GS_PREFIX_OPCODE
481};
482
252b5132
RH
483/* List of chars besides those in app.c:symbol_chars that can start an
484 operand. Used to prevent the scrubber eating vital white-space. */
86fa6981 485const char extra_symbol_chars[] = "*%-([{}"
252b5132 486#ifdef LEX_AT
32137342
NC
487 "@"
488#endif
489#ifdef LEX_QM
490 "?"
252b5132 491#endif
32137342 492 ;
252b5132 493
b3983e5f
JB
494#if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
495 && !defined (TE_GNU) \
496 && !defined (TE_LINUX) \
d85e70a3 497 && !defined (TE_Haiku) \
b3983e5f
JB
498 && !defined (TE_FreeBSD) \
499 && !defined (TE_DragonFly) \
500 && !defined (TE_NetBSD))
252b5132 501/* This array holds the chars that always start a comment. If the
b3b91714
AM
502 pre-processor is disabled, these aren't very useful. The option
503 --divide will remove '/' from this list. */
504const char *i386_comment_chars = "#/";
505#define SVR4_COMMENT_CHARS 1
252b5132 506#define PREFIX_SEPARATOR '\\'
252b5132 507
b3b91714
AM
508#else
509const char *i386_comment_chars = "#";
510#define PREFIX_SEPARATOR '/'
511#endif
512
252b5132
RH
513/* This array holds the chars that only start a comment at the beginning of
514 a line. If the line seems to have the form '# 123 filename'
ce8a8b2f
AM
515 .line and .file directives will appear in the pre-processed output.
516 Note that input_file.c hand checks for '#' at the beginning of the
252b5132 517 first line of the input file. This is because the compiler outputs
ce8a8b2f
AM
518 #NO_APP at the beginning of its output.
519 Also note that comments started like this one will always work if
252b5132 520 '/' isn't otherwise defined. */
b3b91714 521const char line_comment_chars[] = "#/";
252b5132 522
63a0b638 523const char line_separator_chars[] = ";";
252b5132 524
ce8a8b2f
AM
525/* Chars that can be used to separate mant from exp in floating point
526 nums. */
252b5132
RH
527const char EXP_CHARS[] = "eE";
528
ce8a8b2f
AM
529/* Chars that mean this number is a floating point constant
530 As in 0f12.456
531 or 0d1.2345e12. */
de133cf9 532const char FLT_CHARS[] = "fFdDxXhHbB";
252b5132 533
ce8a8b2f 534/* Tables for lexical analysis. */
252b5132
RH
535static char mnemonic_chars[256];
536static char register_chars[256];
537static char operand_chars[256];
252b5132 538
ce8a8b2f 539/* Lexical macros. */
252b5132
RH
540#define is_mnemonic_char(x) (mnemonic_chars[(unsigned char) x])
541#define is_operand_char(x) (operand_chars[(unsigned char) x])
542#define is_register_char(x) (register_chars[(unsigned char) x])
543#define is_space_char(x) ((x) == ' ')
252b5132 544
0234cb7c 545/* All non-digit non-letter characters that may occur in an operand. */
252b5132
RH
546static char operand_special_chars[] = "%$-+(,)*._~/<>|&^!:[@]";
547
548/* md_assemble() always leaves the strings it's passed unaltered. To
549 effect this we maintain a stack of saved characters that we've smashed
550 with '\0's (indicating end of strings for various sub-fields of the
47926f60 551 assembler instruction). */
252b5132 552static char save_stack[32];
ce8a8b2f 553static char *save_stack_p;
252b5132
RH
554#define END_STRING_AND_SAVE(s) \
555 do { *save_stack_p++ = *(s); *(s) = '\0'; } while (0)
556#define RESTORE_END_STRING(s) \
557 do { *(s) = *--save_stack_p; } while (0)
558
47926f60 559/* The instruction we're assembling. */
252b5132
RH
560static i386_insn i;
561
562/* Possible templates for current insn. */
563static const templates *current_templates;
564
31b2323c
L
565/* Per instruction expressionS buffers: max displacements & immediates. */
566static expressionS disp_expressions[MAX_MEMORY_OPERANDS];
567static expressionS im_expressions[MAX_IMMEDIATE_OPERANDS];
252b5132 568
47926f60 569/* Current operand we are working on. */
ee86248c 570static int this_operand = -1;
252b5132 571
d0c2e3ec
JB
572/* Are we processing a .insn directive? */
573#define dot_insn() (i.tm.mnem_off == MN__insn)
574
3e73aa7c
JH
575/* We support four different modes. FLAG_CODE variable is used to distinguish
576 these. */
577
578enum flag_code {
579 CODE_32BIT,
580 CODE_16BIT,
581 CODE_64BIT };
582
583static enum flag_code flag_code;
4fa24527 584static unsigned int object_64bit;
862be3fb 585static unsigned int disallow_64bit_reloc;
3e73aa7c 586static int use_rela_relocations = 0;
e379e5f3
L
587/* __tls_get_addr/___tls_get_addr symbol for TLS. */
588static const char *tls_get_addr;
3e73aa7c 589
7af8ed2d
NC
590#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
591 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
592 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
593
351f65ca
L
594/* The ELF ABI to use. */
595enum x86_elf_abi
596{
597 I386_ABI,
7f56bc95
L
598 X86_64_ABI,
599 X86_64_X32_ABI
351f65ca
L
600};
601
602static enum x86_elf_abi x86_elf_abi = I386_ABI;
7af8ed2d 603#endif
351f65ca 604
167ad85b
TG
605#if defined (TE_PE) || defined (TE_PEP)
606/* Use big object file format. */
607static int use_big_obj = 0;
608#endif
609
8dcea932
L
610#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
611/* 1 if generating code for a shared library. */
612static int shared = 0;
b52c4ee4
IB
613
614unsigned int x86_sframe_cfa_sp_reg;
3e3e792a 615/* The other CFA base register for SFrame stack trace info. */
b52c4ee4
IB
616unsigned int x86_sframe_cfa_fp_reg;
617unsigned int x86_sframe_cfa_ra_reg;
618
8dcea932
L
619#endif
620
47926f60
KH
621/* 1 for intel syntax,
622 0 if att syntax. */
623static int intel_syntax = 0;
252b5132 624
4b5aaf5f
L
625static enum x86_64_isa
626{
627 amd64 = 1, /* AMD64 ISA. */
628 intel64 /* Intel64 ISA. */
629} isa64;
e89c5eaa 630
1efbbeb4
L
631/* 1 for intel mnemonic,
632 0 if att mnemonic. */
633static int intel_mnemonic = !SYSV386_COMPAT;
634
a60de03c
JB
635/* 1 if pseudo registers are permitted. */
636static int allow_pseudo_reg = 0;
637
47926f60
KH
638/* 1 if register prefix % not required. */
639static int allow_naked_reg = 0;
252b5132 640
33eaf5de 641/* 1 if the assembler should add BND prefix for all control-transferring
7e8b059b
L
642 instructions supporting it, even if this prefix wasn't specified
643 explicitly. */
644static int add_bnd_prefix = 0;
645
ba104c83 646/* 1 if pseudo index register, eiz/riz, is allowed . */
db51cc60
L
647static int allow_index_reg = 0;
648
d022bddd
IT
649/* 1 if the assembler should ignore LOCK prefix, even if it was
650 specified explicitly. */
651static int omit_lock_prefix = 0;
652
e4e00185
AS
653/* 1 if the assembler should encode lfence, mfence, and sfence as
654 "lock addl $0, (%{re}sp)". */
655static int avoid_fence = 0;
656
ae531041
L
657/* 1 if lfence should be inserted after every load. */
658static int lfence_after_load = 0;
659
660/* Non-zero if lfence should be inserted before indirect branch. */
661static enum lfence_before_indirect_branch_kind
662 {
663 lfence_branch_none = 0,
664 lfence_branch_register,
665 lfence_branch_memory,
666 lfence_branch_all
667 }
668lfence_before_indirect_branch;
669
670/* Non-zero if lfence should be inserted before ret. */
671static enum lfence_before_ret_kind
672 {
673 lfence_before_ret_none = 0,
674 lfence_before_ret_not,
a09f656b 675 lfence_before_ret_or,
676 lfence_before_ret_shl
ae531041
L
677 }
678lfence_before_ret;
679
680/* Types of previous instruction is .byte or prefix. */
e379e5f3
L
681static struct
682 {
683 segT seg;
684 const char *file;
685 const char *name;
686 unsigned int line;
687 enum last_insn_kind
688 {
689 last_insn_other = 0,
690 last_insn_directive,
691 last_insn_prefix
692 } kind;
693 } last_insn;
694
0cb4071e
L
695/* 1 if the assembler should generate relax relocations. */
696
697static int generate_relax_relocations
698 = DEFAULT_GENERATE_X86_RELAX_RELOCATIONS;
699
7bab8ab5 700static enum check_kind
daf50ae7 701 {
7bab8ab5
JB
702 check_none = 0,
703 check_warning,
704 check_error
daf50ae7 705 }
7bab8ab5 706sse_check, operand_check = check_warning;
daf50ae7 707
e379e5f3
L
708/* Non-zero if branches should be aligned within power of 2 boundary. */
709static int align_branch_power = 0;
710
711/* Types of branches to align. */
712enum align_branch_kind
713 {
714 align_branch_none = 0,
715 align_branch_jcc = 1,
716 align_branch_fused = 2,
717 align_branch_jmp = 3,
718 align_branch_call = 4,
719 align_branch_indirect = 5,
720 align_branch_ret = 6
721 };
722
723/* Type bits of branches to align. */
724enum align_branch_bit
725 {
726 align_branch_jcc_bit = 1 << align_branch_jcc,
727 align_branch_fused_bit = 1 << align_branch_fused,
728 align_branch_jmp_bit = 1 << align_branch_jmp,
729 align_branch_call_bit = 1 << align_branch_call,
730 align_branch_indirect_bit = 1 << align_branch_indirect,
731 align_branch_ret_bit = 1 << align_branch_ret
732 };
733
734static unsigned int align_branch = (align_branch_jcc_bit
735 | align_branch_fused_bit
736 | align_branch_jmp_bit);
737
79d72f45
HL
738/* Types of condition jump used by macro-fusion. */
739enum mf_jcc_kind
740 {
741 mf_jcc_jo = 0, /* base opcode 0x70 */
742 mf_jcc_jc, /* base opcode 0x72 */
743 mf_jcc_je, /* base opcode 0x74 */
744 mf_jcc_jna, /* base opcode 0x76 */
745 mf_jcc_js, /* base opcode 0x78 */
746 mf_jcc_jp, /* base opcode 0x7a */
747 mf_jcc_jl, /* base opcode 0x7c */
748 mf_jcc_jle, /* base opcode 0x7e */
749 };
750
751/* Types of compare flag-modifying insntructions used by macro-fusion. */
752enum mf_cmp_kind
753 {
754 mf_cmp_test_and, /* test/cmp */
755 mf_cmp_alu_cmp, /* add/sub/cmp */
756 mf_cmp_incdec /* inc/dec */
757 };
758
e379e5f3
L
759/* The maximum padding size for fused jcc. CMP like instruction can
760 be 9 bytes and jcc can be 6 bytes. Leave room just in case for
761 prefixes. */
762#define MAX_FUSED_JCC_PADDING_SIZE 20
763
764/* The maximum number of prefixes added for an instruction. */
765static unsigned int align_branch_prefix_size = 5;
766
b6f8c7c4
L
767/* Optimization:
768 1. Clear the REX_W bit with register operand if possible.
769 2. Above plus use 128bit vector instruction to clear the full vector
770 register.
771 */
772static int optimize = 0;
773
774/* Optimization:
775 1. Clear the REX_W bit with register operand if possible.
776 2. Above plus use 128bit vector instruction to clear the full vector
777 register.
778 3. Above plus optimize "test{q,l,w} $imm8,%r{64,32,16}" to
779 "testb $imm7,%r8".
780 */
781static int optimize_for_space = 0;
782
2ca3ace5
L
783/* Register prefix used for error message. */
784static const char *register_prefix = "%";
785
47926f60
KH
786/* Used in 16 bit gcc mode to add an l suffix to call, ret, enter,
787 leave, push, and pop instructions so that gcc has the same stack
788 frame as in 32 bit mode. */
789static char stackop_size = '\0';
eecb386c 790
12b55ccc
L
791/* Non-zero to optimize code alignment. */
792int optimize_align_code = 1;
793
47926f60
KH
794/* Non-zero to quieten some warnings. */
795static int quiet_warnings = 0;
a38cf1db 796
d59a54c2
JB
797/* Guard to avoid repeated warnings about non-16-bit code on 16-bit CPUs. */
798static bool pre_386_16bit_warned;
799
47926f60
KH
800/* CPU name. */
801static const char *cpu_arch_name = NULL;
6305a203 802static char *cpu_sub_arch_name = NULL;
a38cf1db 803
47926f60 804/* CPU feature flags. */
40fb9820
L
805static i386_cpu_flags cpu_arch_flags = CPU_UNKNOWN_FLAGS;
806
ccc9c027
L
807/* If we have selected a cpu we are generating instructions for. */
808static int cpu_arch_tune_set = 0;
809
9103f4f4 810/* Cpu we are generating instructions for. */
fbf3f584 811enum processor_type cpu_arch_tune = PROCESSOR_UNKNOWN;
9103f4f4
L
812
813/* CPU feature flags of cpu we are generating instructions for. */
40fb9820 814static i386_cpu_flags cpu_arch_tune_flags;
9103f4f4 815
ccc9c027 816/* CPU instruction set architecture used. */
fbf3f584 817enum processor_type cpu_arch_isa = PROCESSOR_UNKNOWN;
ccc9c027 818
9103f4f4 819/* CPU feature flags of instruction set architecture used. */
fbf3f584 820i386_cpu_flags cpu_arch_isa_flags;
9103f4f4 821
fddf5b5b
AM
822/* If set, conditional jumps are not automatically promoted to handle
823 larger than a byte offset. */
f68697e8 824static bool no_cond_jump_promotion = false;
fddf5b5b 825
c0f3af97
L
826/* Encode SSE instructions with VEX prefix. */
827static unsigned int sse2avx;
828
c8480b58
L
829/* Encode aligned vector move as unaligned vector move. */
830static unsigned int use_unaligned_vector_move;
831
539f890d
L
832/* Encode scalar AVX instructions with specific vector length. */
833static enum
834 {
835 vex128 = 0,
836 vex256
837 } avxscalar;
838
03751133
L
839/* Encode VEX WIG instructions with specific vex.w. */
840static enum
841 {
842 vexw0 = 0,
843 vexw1
844 } vexwig;
845
43234a1e
L
846/* Encode scalar EVEX LIG instructions with specific vector length. */
847static enum
848 {
849 evexl128 = 0,
850 evexl256,
851 evexl512
852 } evexlig;
853
854/* Encode EVEX WIG instructions with specific evex.w. */
855static enum
856 {
857 evexw0 = 0,
858 evexw1
859 } evexwig;
860
d3d3c6db
IT
861/* Value to encode in EVEX RC bits, for SAE-only instructions. */
862static enum rc_type evexrcig = rne;
863
29b0f896 864/* Pre-defined "_GLOBAL_OFFSET_TABLE_". */
87c245cc 865static symbolS *GOT_symbol;
29b0f896 866
a4447b93
RH
867/* The dwarf2 return column, adjusted for 32 or 64 bit. */
868unsigned int x86_dwarf2_return_column;
869
870/* The dwarf2 data alignment, adjusted for 32 or 64 bit. */
871int x86_cie_data_alignment;
872
252b5132 873/* Interface to relax_segment.
fddf5b5b
AM
874 There are 3 major relax states for 386 jump insns because the
875 different types of jumps add different sizes to frags when we're
e379e5f3
L
876 figuring out what sort of jump to choose to reach a given label.
877
878 BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING are used to align
879 branches which are handled by md_estimate_size_before_relax() and
880 i386_generic_table_relax_frag(). */
252b5132 881
47926f60 882/* Types. */
93c2a809
AM
883#define UNCOND_JUMP 0
884#define COND_JUMP 1
885#define COND_JUMP86 2
e379e5f3
L
886#define BRANCH_PADDING 3
887#define BRANCH_PREFIX 4
888#define FUSED_JCC_PADDING 5
fddf5b5b 889
47926f60 890/* Sizes. */
252b5132
RH
891#define CODE16 1
892#define SMALL 0
29b0f896 893#define SMALL16 (SMALL | CODE16)
252b5132 894#define BIG 2
29b0f896 895#define BIG16 (BIG | CODE16)
252b5132
RH
896
897#ifndef INLINE
898#ifdef __GNUC__
899#define INLINE __inline__
900#else
901#define INLINE
902#endif
903#endif
904
fddf5b5b
AM
905#define ENCODE_RELAX_STATE(type, size) \
906 ((relax_substateT) (((type) << 2) | (size)))
907#define TYPE_FROM_RELAX_STATE(s) \
908 ((s) >> 2)
909#define DISP_SIZE_FROM_RELAX_STATE(s) \
910 ((((s) & 3) == BIG ? 4 : (((s) & 3) == BIG16 ? 2 : 1)))
252b5132
RH
911
912/* This table is used by relax_frag to promote short jumps to long
913 ones where necessary. SMALL (short) jumps may be promoted to BIG
914 (32 bit long) ones, and SMALL16 jumps to BIG16 (16 bit long). We
915 don't allow a short jump in a 32 bit code segment to be promoted to
916 a 16 bit offset jump because it's slower (requires data size
917 prefix), and doesn't work, unless the destination is in the bottom
918 64k of the code segment (The top 16 bits of eip are zeroed). */
919
920const relax_typeS md_relax_table[] =
921{
24eab124
AM
922 /* The fields are:
923 1) most positive reach of this state,
924 2) most negative reach of this state,
93c2a809 925 3) how many bytes this mode will have in the variable part of the frag
ce8a8b2f 926 4) which index into the table to try if we can't fit into this one. */
252b5132 927
fddf5b5b 928 /* UNCOND_JUMP states. */
93c2a809
AM
929 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG)},
930 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16)},
931 /* dword jmp adds 4 bytes to frag:
932 0 extra opcode bytes, 4 displacement bytes. */
252b5132 933 {0, 0, 4, 0},
93c2a809
AM
934 /* word jmp adds 2 byte2 to frag:
935 0 extra opcode bytes, 2 displacement bytes. */
252b5132
RH
936 {0, 0, 2, 0},
937
93c2a809
AM
938 /* COND_JUMP states. */
939 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG)},
940 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG16)},
941 /* dword conditionals adds 5 bytes to frag:
942 1 extra opcode byte, 4 displacement bytes. */
943 {0, 0, 5, 0},
fddf5b5b 944 /* word conditionals add 3 bytes to frag:
93c2a809
AM
945 1 extra opcode byte, 2 displacement bytes. */
946 {0, 0, 3, 0},
947
948 /* COND_JUMP86 states. */
949 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG)},
950 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG16)},
951 /* dword conditionals adds 5 bytes to frag:
952 1 extra opcode byte, 4 displacement bytes. */
953 {0, 0, 5, 0},
954 /* word conditionals add 4 bytes to frag:
955 1 displacement byte and a 3 byte long branch insn. */
956 {0, 0, 4, 0}
252b5132
RH
957};
958
6ceeed25 959#define ARCH(n, t, f, s) \
ae89daec
JB
960 { STRING_COMMA_LEN (#n), s, PROCESSOR_ ## t, CPU_ ## f ## _FLAGS, \
961 CPU_NONE_FLAGS }
962#define SUBARCH(n, e, d, s) \
963 { STRING_COMMA_LEN (#n), s, PROCESSOR_NONE, CPU_ ## e ## _FLAGS, \
964 CPU_ ## d ## _FLAGS }
6ceeed25 965
9103f4f4
L
966static const arch_entry cpu_arch[] =
967{
3ce2ebcf
JB
968 /* Do not replace the first two entries - i386_target_format() and
969 set_cpu_arch() rely on them being there in this order. */
6ceeed25
JB
970 ARCH (generic32, GENERIC32, GENERIC32, false),
971 ARCH (generic64, GENERIC64, GENERIC64, false),
972 ARCH (i8086, UNKNOWN, NONE, false),
4d97c5c8
JB
973 ARCH (i186, UNKNOWN, 186, false),
974 ARCH (i286, UNKNOWN, 286, false),
975 ARCH (i386, I386, 386, false),
976 ARCH (i486, I486, 486, false),
977 ARCH (i586, PENTIUM, 586, false),
978 ARCH (i686, PENTIUMPRO, 686, false),
979 ARCH (pentium, PENTIUM, 586, false),
6ceeed25
JB
980 ARCH (pentiumpro, PENTIUMPRO, PENTIUMPRO, false),
981 ARCH (pentiumii, PENTIUMPRO, P2, false),
982 ARCH (pentiumiii, PENTIUMPRO, P3, false),
983 ARCH (pentium4, PENTIUM4, P4, false),
984 ARCH (prescott, NOCONA, CORE, false),
985 ARCH (nocona, NOCONA, NOCONA, false),
986 ARCH (yonah, CORE, CORE, true),
987 ARCH (core, CORE, CORE, false),
988 ARCH (merom, CORE2, CORE2, true),
989 ARCH (core2, CORE2, CORE2, false),
990 ARCH (corei7, COREI7, COREI7, false),
991 ARCH (iamcu, IAMCU, IAMCU, false),
992 ARCH (k6, K6, K6, false),
993 ARCH (k6_2, K6, K6_2, false),
994 ARCH (athlon, ATHLON, ATHLON, false),
995 ARCH (sledgehammer, K8, K8, true),
996 ARCH (opteron, K8, K8, false),
997 ARCH (k8, K8, K8, false),
998 ARCH (amdfam10, AMDFAM10, AMDFAM10, false),
999 ARCH (bdver1, BD, BDVER1, false),
1000 ARCH (bdver2, BD, BDVER2, false),
1001 ARCH (bdver3, BD, BDVER3, false),
1002 ARCH (bdver4, BD, BDVER4, false),
1003 ARCH (znver1, ZNVER, ZNVER1, false),
1004 ARCH (znver2, ZNVER, ZNVER2, false),
1005 ARCH (znver3, ZNVER, ZNVER3, false),
b0e8fa7f 1006 ARCH (znver4, ZNVER, ZNVER4, false),
6ceeed25
JB
1007 ARCH (btver1, BT, BTVER1, false),
1008 ARCH (btver2, BT, BTVER2, false),
1009
4d97c5c8
JB
1010 SUBARCH (8087, 8087, ANY_8087, false),
1011 SUBARCH (87, NONE, ANY_8087, false), /* Disable only! */
ae89daec
JB
1012 SUBARCH (287, 287, ANY_287, false),
1013 SUBARCH (387, 387, ANY_387, false),
1014 SUBARCH (687, 687, ANY_687, false),
4d97c5c8 1015 SUBARCH (cmov, CMOV, CMOV, false),
88bd2203 1016 SUBARCH (fxsr, FXSR, ANY_FXSR, false),
ae89daec
JB
1017 SUBARCH (mmx, MMX, ANY_MMX, false),
1018 SUBARCH (sse, SSE, ANY_SSE, false),
1019 SUBARCH (sse2, SSE2, ANY_SSE2, false),
1020 SUBARCH (sse3, SSE3, ANY_SSE3, false),
1021 SUBARCH (sse4a, SSE4A, ANY_SSE4A, false),
1022 SUBARCH (ssse3, SSSE3, ANY_SSSE3, false),
1023 SUBARCH (sse4.1, SSE4_1, ANY_SSE4_1, false),
1024 SUBARCH (sse4.2, SSE4_2, ANY_SSE4_2, false),
1025 SUBARCH (sse4, SSE4_2, ANY_SSE4_1, false),
1026 SUBARCH (avx, AVX, ANY_AVX, false),
1027 SUBARCH (avx2, AVX2, ANY_AVX2, false),
1028 SUBARCH (avx512f, AVX512F, ANY_AVX512F, false),
1029 SUBARCH (avx512cd, AVX512CD, ANY_AVX512CD, false),
1030 SUBARCH (avx512er, AVX512ER, ANY_AVX512ER, false),
1031 SUBARCH (avx512pf, AVX512PF, ANY_AVX512PF, false),
1032 SUBARCH (avx512dq, AVX512DQ, ANY_AVX512DQ, false),
1033 SUBARCH (avx512bw, AVX512BW, ANY_AVX512BW, false),
1034 SUBARCH (avx512vl, AVX512VL, ANY_AVX512VL, false),
cafa5ef7 1035 SUBARCH (monitor, MONITOR, MONITOR, false),
25626f79
JB
1036 SUBARCH (vmx, VMX, ANY_VMX, false),
1037 SUBARCH (vmfunc, VMFUNC, ANY_VMFUNC, false),
ae89daec 1038 SUBARCH (smx, SMX, SMX, false),
5091b9ee
JB
1039 SUBARCH (xsave, XSAVE, ANY_XSAVE, false),
1040 SUBARCH (xsaveopt, XSAVEOPT, ANY_XSAVEOPT, false),
1041 SUBARCH (xsavec, XSAVEC, ANY_XSAVEC, false),
1042 SUBARCH (xsaves, XSAVES, ANY_XSAVES, false),
1043 SUBARCH (aes, AES, ANY_AES, false),
1044 SUBARCH (pclmul, PCLMUL, ANY_PCLMUL, false),
1045 SUBARCH (clmul, PCLMUL, ANY_PCLMUL, true),
ae89daec
JB
1046 SUBARCH (fsgsbase, FSGSBASE, FSGSBASE, false),
1047 SUBARCH (rdrnd, RDRND, RDRND, false),
5091b9ee 1048 SUBARCH (f16c, F16C, ANY_F16C, false),
ae89daec 1049 SUBARCH (bmi2, BMI2, BMI2, false),
5091b9ee
JB
1050 SUBARCH (fma, FMA, ANY_FMA, false),
1051 SUBARCH (fma4, FMA4, ANY_FMA4, false),
1052 SUBARCH (xop, XOP, ANY_XOP, false),
1053 SUBARCH (lwp, LWP, ANY_LWP, false),
ae89daec
JB
1054 SUBARCH (movbe, MOVBE, MOVBE, false),
1055 SUBARCH (cx16, CX16, CX16, false),
c3bb24f5 1056 SUBARCH (lahf_sahf, LAHF_SAHF, LAHF_SAHF, false),
25626f79 1057 SUBARCH (ept, EPT, ANY_EPT, false),
ae89daec
JB
1058 SUBARCH (lzcnt, LZCNT, LZCNT, false),
1059 SUBARCH (popcnt, POPCNT, POPCNT, false),
1060 SUBARCH (hle, HLE, HLE, false),
760ab3d0
JB
1061 SUBARCH (rtm, RTM, ANY_RTM, false),
1062 SUBARCH (tsx, TSX, TSX, false),
ae89daec
JB
1063 SUBARCH (invpcid, INVPCID, INVPCID, false),
1064 SUBARCH (clflush, CLFLUSH, CLFLUSH, false),
1065 SUBARCH (nop, NOP, NOP, false),
1066 SUBARCH (syscall, SYSCALL, SYSCALL, false),
1067 SUBARCH (rdtscp, RDTSCP, RDTSCP, false),
5091b9ee
JB
1068 SUBARCH (3dnow, 3DNOW, ANY_3DNOW, false),
1069 SUBARCH (3dnowa, 3DNOWA, ANY_3DNOWA, false),
ae89daec 1070 SUBARCH (padlock, PADLOCK, PADLOCK, false),
0919e770
JB
1071 SUBARCH (pacifica, SVME, ANY_SVME, true),
1072 SUBARCH (svme, SVME, ANY_SVME, false),
ae89daec
JB
1073 SUBARCH (abm, ABM, ABM, false),
1074 SUBARCH (bmi, BMI, BMI, false),
1075 SUBARCH (tbm, TBM, TBM, false),
1076 SUBARCH (adx, ADX, ADX, false),
1077 SUBARCH (rdseed, RDSEED, RDSEED, false),
1078 SUBARCH (prfchw, PRFCHW, PRFCHW, false),
1079 SUBARCH (smap, SMAP, SMAP, false),
5091b9ee
JB
1080 SUBARCH (mpx, MPX, ANY_MPX, false),
1081 SUBARCH (sha, SHA, ANY_SHA, false),
ae89daec
JB
1082 SUBARCH (clflushopt, CLFLUSHOPT, CLFLUSHOPT, false),
1083 SUBARCH (prefetchwt1, PREFETCHWT1, PREFETCHWT1, false),
1084 SUBARCH (se1, SE1, SE1, false),
1085 SUBARCH (clwb, CLWB, CLWB, false),
1086 SUBARCH (avx512ifma, AVX512IFMA, ANY_AVX512IFMA, false),
1087 SUBARCH (avx512vbmi, AVX512VBMI, ANY_AVX512VBMI, false),
1088 SUBARCH (avx512_4fmaps, AVX512_4FMAPS, ANY_AVX512_4FMAPS, false),
1089 SUBARCH (avx512_4vnniw, AVX512_4VNNIW, ANY_AVX512_4VNNIW, false),
1090 SUBARCH (avx512_vpopcntdq, AVX512_VPOPCNTDQ, ANY_AVX512_VPOPCNTDQ, false),
1091 SUBARCH (avx512_vbmi2, AVX512_VBMI2, ANY_AVX512_VBMI2, false),
1092 SUBARCH (avx512_vnni, AVX512_VNNI, ANY_AVX512_VNNI, false),
1093 SUBARCH (avx512_bitalg, AVX512_BITALG, ANY_AVX512_BITALG, false),
1094 SUBARCH (avx_vnni, AVX_VNNI, ANY_AVX_VNNI, false),
1095 SUBARCH (clzero, CLZERO, CLZERO, false),
1096 SUBARCH (mwaitx, MWAITX, MWAITX, false),
5091b9ee 1097 SUBARCH (ospke, OSPKE, ANY_OSPKE, false),
ae89daec
JB
1098 SUBARCH (rdpid, RDPID, RDPID, false),
1099 SUBARCH (ptwrite, PTWRITE, PTWRITE, false),
4d97c5c8
JB
1100 SUBARCH (ibt, IBT, IBT, false),
1101 SUBARCH (shstk, SHSTK, SHSTK, false),
88bd2203 1102 SUBARCH (gfni, GFNI, ANY_GFNI, false),
b20f4261
JB
1103 SUBARCH (vaes, VAES, ANY_VAES, false),
1104 SUBARCH (vpclmulqdq, VPCLMULQDQ, ANY_VPCLMULQDQ, false),
ae89daec
JB
1105 SUBARCH (wbnoinvd, WBNOINVD, WBNOINVD, false),
1106 SUBARCH (pconfig, PCONFIG, PCONFIG, false),
1107 SUBARCH (waitpkg, WAITPKG, WAITPKG, false),
1108 SUBARCH (cldemote, CLDEMOTE, CLDEMOTE, false),
1109 SUBARCH (amx_int8, AMX_INT8, ANY_AMX_INT8, false),
1110 SUBARCH (amx_bf16, AMX_BF16, ANY_AMX_BF16, false),
5091b9ee 1111 SUBARCH (amx_fp16, AMX_FP16, ANY_AMX_FP16, false),
ae89daec 1112 SUBARCH (amx_tile, AMX_TILE, ANY_AMX_TILE, false),
4d97c5c8
JB
1113 SUBARCH (movdiri, MOVDIRI, MOVDIRI, false),
1114 SUBARCH (movdir64b, MOVDIR64B, MOVDIR64B, false),
ae89daec
JB
1115 SUBARCH (avx512_bf16, AVX512_BF16, ANY_AVX512_BF16, false),
1116 SUBARCH (avx512_vp2intersect, AVX512_VP2INTERSECT,
1117 ANY_AVX512_VP2INTERSECT, false),
4d97c5c8
JB
1118 SUBARCH (tdx, TDX, TDX, false),
1119 SUBARCH (enqcmd, ENQCMD, ENQCMD, false),
1120 SUBARCH (serialize, SERIALIZE, SERIALIZE, false),
ae89daec
JB
1121 SUBARCH (rdpru, RDPRU, RDPRU, false),
1122 SUBARCH (mcommit, MCOMMIT, MCOMMIT, false),
0919e770 1123 SUBARCH (sev_es, SEV_ES, ANY_SEV_ES, false),
760ab3d0 1124 SUBARCH (tsxldtrk, TSXLDTRK, ANY_TSXLDTRK, false),
88bd2203
JB
1125 SUBARCH (kl, KL, ANY_KL, false),
1126 SUBARCH (widekl, WIDEKL, ANY_WIDEKL, false),
4d97c5c8
JB
1127 SUBARCH (uintr, UINTR, UINTR, false),
1128 SUBARCH (hreset, HRESET, HRESET, false),
ae89daec 1129 SUBARCH (avx512_fp16, AVX512_FP16, ANY_AVX512_FP16, false),
ef07be45 1130 SUBARCH (prefetchi, PREFETCHI, PREFETCHI, false),
4321af3e 1131 SUBARCH (avx_ifma, AVX_IFMA, ANY_AVX_IFMA, false),
23ae61ad 1132 SUBARCH (avx_vnni_int8, AVX_VNNI_INT8, ANY_AVX_VNNI_INT8, false),
4d97c5c8
JB
1133 SUBARCH (cmpccxadd, CMPCCXADD, CMPCCXADD, false),
1134 SUBARCH (wrmsrns, WRMSRNS, WRMSRNS, false),
1135 SUBARCH (msrlist, MSRLIST, MSRLIST, false),
01d8ce74 1136 SUBARCH (avx_ne_convert, AVX_NE_CONVERT, ANY_AVX_NE_CONVERT, false),
4d97c5c8 1137 SUBARCH (rao_int, RAO_INT, RAO_INT, false),
0919e770 1138 SUBARCH (rmpquery, RMPQUERY, ANY_RMPQUERY, false),
293f5f65
L
1139};
1140
6ceeed25
JB
1141#undef SUBARCH
1142#undef ARCH
1143
704209c0 1144#ifdef I386COFF
a6c24e68
NC
1145/* Like s_lcomm_internal in gas/read.c but the alignment string
1146 is allowed to be optional. */
1147
1148static symbolS *
1149pe_lcomm_internal (int needs_align, symbolS *symbolP, addressT size)
1150{
1151 addressT align = 0;
1152
1153 SKIP_WHITESPACE ();
1154
7ab9ffdd 1155 if (needs_align
a6c24e68
NC
1156 && *input_line_pointer == ',')
1157 {
1158 align = parse_align (needs_align - 1);
7ab9ffdd 1159
a6c24e68
NC
1160 if (align == (addressT) -1)
1161 return NULL;
1162 }
1163 else
1164 {
1165 if (size >= 8)
1166 align = 3;
1167 else if (size >= 4)
1168 align = 2;
1169 else if (size >= 2)
1170 align = 1;
1171 else
1172 align = 0;
1173 }
1174
1175 bss_alloc (symbolP, size, align);
1176 return symbolP;
1177}
1178
704209c0 1179static void
a6c24e68
NC
1180pe_lcomm (int needs_align)
1181{
1182 s_comm_internal (needs_align * 2, pe_lcomm_internal);
1183}
704209c0 1184#endif
a6c24e68 1185
29b0f896
AM
1186const pseudo_typeS md_pseudo_table[] =
1187{
1188#if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
1189 {"align", s_align_bytes, 0},
1190#else
1191 {"align", s_align_ptwo, 0},
1192#endif
1193 {"arch", set_cpu_arch, 0},
1194#ifndef I386COFF
1195 {"bss", s_bss, 0},
a6c24e68
NC
1196#else
1197 {"lcomm", pe_lcomm, 1},
29b0f896
AM
1198#endif
1199 {"ffloat", float_cons, 'f'},
1200 {"dfloat", float_cons, 'd'},
1201 {"tfloat", float_cons, 'x'},
7d19d096 1202 {"hfloat", float_cons, 'h'},
de133cf9 1203 {"bfloat16", float_cons, 'b'},
29b0f896 1204 {"value", cons, 2},
d182319b 1205 {"slong", signed_cons, 4},
edd67638 1206 {"insn", s_insn, 0},
29b0f896
AM
1207 {"noopt", s_ignore, 0},
1208 {"optim", s_ignore, 0},
1209 {"code16gcc", set_16bit_gcc_code_flag, CODE_16BIT},
1210 {"code16", set_code_flag, CODE_16BIT},
1211 {"code32", set_code_flag, CODE_32BIT},
da5f19a2 1212#ifdef BFD64
29b0f896 1213 {"code64", set_code_flag, CODE_64BIT},
da5f19a2 1214#endif
29b0f896
AM
1215 {"intel_syntax", set_intel_syntax, 1},
1216 {"att_syntax", set_intel_syntax, 0},
1efbbeb4
L
1217 {"intel_mnemonic", set_intel_mnemonic, 1},
1218 {"att_mnemonic", set_intel_mnemonic, 0},
db51cc60
L
1219 {"allow_index_reg", set_allow_index_reg, 1},
1220 {"disallow_index_reg", set_allow_index_reg, 0},
7bab8ab5
JB
1221 {"sse_check", set_check, 0},
1222 {"operand_check", set_check, 1},
3b22753a
L
1223#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1224 {"largecomm", handle_large_common, 0},
07a53e5c 1225#else
68d20676 1226 {"file", dwarf2_directive_file, 0},
07a53e5c
RH
1227 {"loc", dwarf2_directive_loc, 0},
1228 {"loc_mark_labels", dwarf2_directive_loc_mark_labels, 0},
3b22753a 1229#endif
6482c264
NC
1230#ifdef TE_PE
1231 {"secrel32", pe_directive_secrel, 0},
145667f8 1232 {"secidx", pe_directive_secidx, 0},
6482c264 1233#endif
29b0f896
AM
1234 {0, 0, 0}
1235};
1236
1237/* For interface with expression (). */
1238extern char *input_line_pointer;
1239
1240/* Hash table for instruction mnemonic lookup. */
629310ab 1241static htab_t op_hash;
29b0f896
AM
1242
1243/* Hash table for register lookup. */
629310ab 1244static htab_t reg_hash;
29b0f896 1245\f
ce8a8b2f
AM
1246 /* Various efficient no-op patterns for aligning code labels.
1247 Note: Don't try to assemble the instructions in the comments.
1248 0L and 0w are not legal. */
62a02d25
L
1249static const unsigned char f32_1[] =
1250 {0x90}; /* nop */
1251static const unsigned char f32_2[] =
1252 {0x66,0x90}; /* xchg %ax,%ax */
1253static const unsigned char f32_3[] =
1254 {0x8d,0x76,0x00}; /* leal 0(%esi),%esi */
1255static const unsigned char f32_4[] =
1256 {0x8d,0x74,0x26,0x00}; /* leal 0(%esi,1),%esi */
62a02d25
L
1257static const unsigned char f32_6[] =
1258 {0x8d,0xb6,0x00,0x00,0x00,0x00}; /* leal 0L(%esi),%esi */
1259static const unsigned char f32_7[] =
1260 {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal 0L(%esi,1),%esi */
62a02d25 1261static const unsigned char f16_3[] =
3ae729d5 1262 {0x8d,0x74,0x00}; /* lea 0(%si),%si */
62a02d25 1263static const unsigned char f16_4[] =
3ae729d5
L
1264 {0x8d,0xb4,0x00,0x00}; /* lea 0W(%si),%si */
1265static const unsigned char jump_disp8[] =
1266 {0xeb}; /* jmp disp8 */
1267static const unsigned char jump32_disp32[] =
1268 {0xe9}; /* jmp disp32 */
1269static const unsigned char jump16_disp32[] =
1270 {0x66,0xe9}; /* jmp disp32 */
62a02d25
L
1271/* 32-bit NOPs patterns. */
1272static const unsigned char *const f32_patt[] = {
3ae729d5 1273 f32_1, f32_2, f32_3, f32_4, NULL, f32_6, f32_7
62a02d25
L
1274};
1275/* 16-bit NOPs patterns. */
1276static const unsigned char *const f16_patt[] = {
3ae729d5 1277 f32_1, f32_2, f16_3, f16_4
62a02d25
L
1278};
1279/* nopl (%[re]ax) */
1280static const unsigned char alt_3[] =
1281 {0x0f,0x1f,0x00};
1282/* nopl 0(%[re]ax) */
1283static const unsigned char alt_4[] =
1284 {0x0f,0x1f,0x40,0x00};
1285/* nopl 0(%[re]ax,%[re]ax,1) */
1286static const unsigned char alt_5[] =
1287 {0x0f,0x1f,0x44,0x00,0x00};
1288/* nopw 0(%[re]ax,%[re]ax,1) */
1289static const unsigned char alt_6[] =
1290 {0x66,0x0f,0x1f,0x44,0x00,0x00};
1291/* nopl 0L(%[re]ax) */
1292static const unsigned char alt_7[] =
1293 {0x0f,0x1f,0x80,0x00,0x00,0x00,0x00};
1294/* nopl 0L(%[re]ax,%[re]ax,1) */
1295static const unsigned char alt_8[] =
1296 {0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1297/* nopw 0L(%[re]ax,%[re]ax,1) */
1298static const unsigned char alt_9[] =
1299 {0x66,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1300/* nopw %cs:0L(%[re]ax,%[re]ax,1) */
1301static const unsigned char alt_10[] =
1302 {0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
3ae729d5
L
1303/* data16 nopw %cs:0L(%eax,%eax,1) */
1304static const unsigned char alt_11[] =
1305 {0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
62a02d25
L
1306/* 32-bit and 64-bit NOPs patterns. */
1307static const unsigned char *const alt_patt[] = {
1308 f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
3ae729d5 1309 alt_9, alt_10, alt_11
62a02d25
L
1310};
1311
1312/* Genenerate COUNT bytes of NOPs to WHERE from PATT with the maximum
1313 size of a single NOP instruction MAX_SINGLE_NOP_SIZE. */
1314
1315static void
1316i386_output_nops (char *where, const unsigned char *const *patt,
1317 int count, int max_single_nop_size)
1318
1319{
3ae729d5
L
1320 /* Place the longer NOP first. */
1321 int last;
1322 int offset;
3076e594
NC
1323 const unsigned char *nops;
1324
1325 if (max_single_nop_size < 1)
1326 {
1327 as_fatal (_("i386_output_nops called to generate nops of at most %d bytes!"),
1328 max_single_nop_size);
1329 return;
1330 }
1331
1332 nops = patt[max_single_nop_size - 1];
3ae729d5
L
1333
1334 /* Use the smaller one if the requsted one isn't available. */
1335 if (nops == NULL)
62a02d25 1336 {
3ae729d5
L
1337 max_single_nop_size--;
1338 nops = patt[max_single_nop_size - 1];
62a02d25
L
1339 }
1340
3ae729d5
L
1341 last = count % max_single_nop_size;
1342
1343 count -= last;
1344 for (offset = 0; offset < count; offset += max_single_nop_size)
1345 memcpy (where + offset, nops, max_single_nop_size);
1346
1347 if (last)
1348 {
1349 nops = patt[last - 1];
1350 if (nops == NULL)
1351 {
1352 /* Use the smaller one plus one-byte NOP if the needed one
1353 isn't available. */
1354 last--;
1355 nops = patt[last - 1];
1356 memcpy (where + offset, nops, last);
1357 where[offset + last] = *patt[0];
1358 }
1359 else
1360 memcpy (where + offset, nops, last);
1361 }
62a02d25
L
1362}
1363
3ae729d5
L
1364static INLINE int
1365fits_in_imm7 (offsetT num)
1366{
1367 return (num & 0x7f) == num;
1368}
1369
1370static INLINE int
1371fits_in_imm31 (offsetT num)
1372{
1373 return (num & 0x7fffffff) == num;
1374}
62a02d25
L
1375
1376/* Genenerate COUNT bytes of NOPs to WHERE with the maximum size of a
1377 single NOP instruction LIMIT. */
1378
1379void
3ae729d5 1380i386_generate_nops (fragS *fragP, char *where, offsetT count, int limit)
62a02d25 1381{
3ae729d5 1382 const unsigned char *const *patt = NULL;
62a02d25 1383 int max_single_nop_size;
3ae729d5
L
1384 /* Maximum number of NOPs before switching to jump over NOPs. */
1385 int max_number_of_nops;
62a02d25 1386
3ae729d5 1387 switch (fragP->fr_type)
62a02d25 1388 {
3ae729d5
L
1389 case rs_fill_nop:
1390 case rs_align_code:
1391 break;
e379e5f3
L
1392 case rs_machine_dependent:
1393 /* Allow NOP padding for jumps and calls. */
1394 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
1395 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
1396 break;
1397 /* Fall through. */
3ae729d5 1398 default:
62a02d25
L
1399 return;
1400 }
1401
ccc9c027
L
1402 /* We need to decide which NOP sequence to use for 32bit and
1403 64bit. When -mtune= is used:
4eed87de 1404
76bc74dc
L
1405 1. For PROCESSOR_I386, PROCESSOR_I486, PROCESSOR_PENTIUM and
1406 PROCESSOR_GENERIC32, f32_patt will be used.
80b8656c
L
1407 2. For the rest, alt_patt will be used.
1408
1409 When -mtune= isn't used, alt_patt will be used if
22109423 1410 cpu_arch_isa_flags has CpuNop. Otherwise, f32_patt will
76bc74dc 1411 be used.
ccc9c027
L
1412
1413 When -march= or .arch is used, we can't use anything beyond
1414 cpu_arch_isa_flags. */
1415
1416 if (flag_code == CODE_16BIT)
1417 {
3ae729d5
L
1418 patt = f16_patt;
1419 max_single_nop_size = sizeof (f16_patt) / sizeof (f16_patt[0]);
1420 /* Limit number of NOPs to 2 in 16-bit mode. */
1421 max_number_of_nops = 2;
252b5132 1422 }
33fef721 1423 else
ccc9c027 1424 {
fbf3f584 1425 if (fragP->tc_frag_data.isa == PROCESSOR_UNKNOWN)
ccc9c027
L
1426 {
1427 /* PROCESSOR_UNKNOWN means that all ISAs may be used. */
1428 switch (cpu_arch_tune)
1429 {
1430 case PROCESSOR_UNKNOWN:
1431 /* We use cpu_arch_isa_flags to check if we SHOULD
22109423
L
1432 optimize with nops. */
1433 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1434 patt = alt_patt;
ccc9c027
L
1435 else
1436 patt = f32_patt;
1437 break;
ccc9c027
L
1438 case PROCESSOR_PENTIUM4:
1439 case PROCESSOR_NOCONA:
ef05d495 1440 case PROCESSOR_CORE:
76bc74dc 1441 case PROCESSOR_CORE2:
bd5295b2 1442 case PROCESSOR_COREI7:
76bc74dc 1443 case PROCESSOR_GENERIC64:
ccc9c027
L
1444 case PROCESSOR_K6:
1445 case PROCESSOR_ATHLON:
1446 case PROCESSOR_K8:
4eed87de 1447 case PROCESSOR_AMDFAM10:
8aedb9fe 1448 case PROCESSOR_BD:
029f3522 1449 case PROCESSOR_ZNVER:
7b458c12 1450 case PROCESSOR_BT:
80b8656c 1451 patt = alt_patt;
ccc9c027 1452 break;
76bc74dc 1453 case PROCESSOR_I386:
ccc9c027
L
1454 case PROCESSOR_I486:
1455 case PROCESSOR_PENTIUM:
2dde1948 1456 case PROCESSOR_PENTIUMPRO:
81486035 1457 case PROCESSOR_IAMCU:
ccc9c027
L
1458 case PROCESSOR_GENERIC32:
1459 patt = f32_patt;
1460 break;
c368d2a8
JB
1461 case PROCESSOR_NONE:
1462 abort ();
4eed87de 1463 }
ccc9c027
L
1464 }
1465 else
1466 {
fbf3f584 1467 switch (fragP->tc_frag_data.tune)
ccc9c027
L
1468 {
1469 case PROCESSOR_UNKNOWN:
e6a14101 1470 /* When cpu_arch_isa is set, cpu_arch_tune shouldn't be
ccc9c027
L
1471 PROCESSOR_UNKNOWN. */
1472 abort ();
1473 break;
1474
76bc74dc 1475 case PROCESSOR_I386:
ccc9c027
L
1476 case PROCESSOR_I486:
1477 case PROCESSOR_PENTIUM:
81486035 1478 case PROCESSOR_IAMCU:
ccc9c027
L
1479 case PROCESSOR_K6:
1480 case PROCESSOR_ATHLON:
1481 case PROCESSOR_K8:
4eed87de 1482 case PROCESSOR_AMDFAM10:
8aedb9fe 1483 case PROCESSOR_BD:
029f3522 1484 case PROCESSOR_ZNVER:
7b458c12 1485 case PROCESSOR_BT:
ccc9c027
L
1486 case PROCESSOR_GENERIC32:
1487 /* We use cpu_arch_isa_flags to check if we CAN optimize
22109423
L
1488 with nops. */
1489 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1490 patt = alt_patt;
ccc9c027
L
1491 else
1492 patt = f32_patt;
1493 break;
76bc74dc
L
1494 case PROCESSOR_PENTIUMPRO:
1495 case PROCESSOR_PENTIUM4:
1496 case PROCESSOR_NOCONA:
1497 case PROCESSOR_CORE:
ef05d495 1498 case PROCESSOR_CORE2:
bd5295b2 1499 case PROCESSOR_COREI7:
22109423 1500 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1501 patt = alt_patt;
ccc9c027
L
1502 else
1503 patt = f32_patt;
1504 break;
1505 case PROCESSOR_GENERIC64:
80b8656c 1506 patt = alt_patt;
ccc9c027 1507 break;
c368d2a8
JB
1508 case PROCESSOR_NONE:
1509 abort ();
4eed87de 1510 }
ccc9c027
L
1511 }
1512
76bc74dc
L
1513 if (patt == f32_patt)
1514 {
3ae729d5
L
1515 max_single_nop_size = sizeof (f32_patt) / sizeof (f32_patt[0]);
1516 /* Limit number of NOPs to 2 for older processors. */
1517 max_number_of_nops = 2;
76bc74dc
L
1518 }
1519 else
1520 {
3ae729d5
L
1521 max_single_nop_size = sizeof (alt_patt) / sizeof (alt_patt[0]);
1522 /* Limit number of NOPs to 7 for newer processors. */
1523 max_number_of_nops = 7;
1524 }
1525 }
1526
1527 if (limit == 0)
1528 limit = max_single_nop_size;
1529
1530 if (fragP->fr_type == rs_fill_nop)
1531 {
1532 /* Output NOPs for .nop directive. */
1533 if (limit > max_single_nop_size)
1534 {
1535 as_bad_where (fragP->fr_file, fragP->fr_line,
1536 _("invalid single nop size: %d "
1537 "(expect within [0, %d])"),
1538 limit, max_single_nop_size);
1539 return;
1540 }
1541 }
e379e5f3 1542 else if (fragP->fr_type != rs_machine_dependent)
3ae729d5
L
1543 fragP->fr_var = count;
1544
1545 if ((count / max_single_nop_size) > max_number_of_nops)
1546 {
1547 /* Generate jump over NOPs. */
1548 offsetT disp = count - 2;
1549 if (fits_in_imm7 (disp))
1550 {
1551 /* Use "jmp disp8" if possible. */
1552 count = disp;
1553 where[0] = jump_disp8[0];
1554 where[1] = count;
1555 where += 2;
1556 }
1557 else
1558 {
1559 unsigned int size_of_jump;
1560
1561 if (flag_code == CODE_16BIT)
1562 {
1563 where[0] = jump16_disp32[0];
1564 where[1] = jump16_disp32[1];
1565 size_of_jump = 2;
1566 }
1567 else
1568 {
1569 where[0] = jump32_disp32[0];
1570 size_of_jump = 1;
1571 }
1572
1573 count -= size_of_jump + 4;
1574 if (!fits_in_imm31 (count))
1575 {
1576 as_bad_where (fragP->fr_file, fragP->fr_line,
1577 _("jump over nop padding out of range"));
1578 return;
1579 }
1580
1581 md_number_to_chars (where + size_of_jump, count, 4);
1582 where += size_of_jump + 4;
76bc74dc 1583 }
ccc9c027 1584 }
3ae729d5
L
1585
1586 /* Generate multiple NOPs. */
1587 i386_output_nops (where, patt, count, limit);
252b5132
RH
1588}
1589
c6fb90c8 1590static INLINE int
0dfbf9d7 1591operand_type_all_zero (const union i386_operand_type *x)
40fb9820 1592{
0dfbf9d7 1593 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1594 {
1595 case 3:
0dfbf9d7 1596 if (x->array[2])
c6fb90c8 1597 return 0;
1a0670f3 1598 /* Fall through. */
c6fb90c8 1599 case 2:
0dfbf9d7 1600 if (x->array[1])
c6fb90c8 1601 return 0;
1a0670f3 1602 /* Fall through. */
c6fb90c8 1603 case 1:
0dfbf9d7 1604 return !x->array[0];
c6fb90c8
L
1605 default:
1606 abort ();
1607 }
40fb9820
L
1608}
1609
c6fb90c8 1610static INLINE void
0dfbf9d7 1611operand_type_set (union i386_operand_type *x, unsigned int v)
40fb9820 1612{
0dfbf9d7 1613 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1614 {
1615 case 3:
0dfbf9d7 1616 x->array[2] = v;
1a0670f3 1617 /* Fall through. */
c6fb90c8 1618 case 2:
0dfbf9d7 1619 x->array[1] = v;
1a0670f3 1620 /* Fall through. */
c6fb90c8 1621 case 1:
0dfbf9d7 1622 x->array[0] = v;
1a0670f3 1623 /* Fall through. */
c6fb90c8
L
1624 break;
1625 default:
1626 abort ();
1627 }
bab6aec1
JB
1628
1629 x->bitfield.class = ClassNone;
75e5731b 1630 x->bitfield.instance = InstanceNone;
c6fb90c8 1631}
40fb9820 1632
c6fb90c8 1633static INLINE int
0dfbf9d7
L
1634operand_type_equal (const union i386_operand_type *x,
1635 const union i386_operand_type *y)
c6fb90c8 1636{
0dfbf9d7 1637 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1638 {
1639 case 3:
0dfbf9d7 1640 if (x->array[2] != y->array[2])
c6fb90c8 1641 return 0;
1a0670f3 1642 /* Fall through. */
c6fb90c8 1643 case 2:
0dfbf9d7 1644 if (x->array[1] != y->array[1])
c6fb90c8 1645 return 0;
1a0670f3 1646 /* Fall through. */
c6fb90c8 1647 case 1:
0dfbf9d7 1648 return x->array[0] == y->array[0];
c6fb90c8
L
1649 break;
1650 default:
1651 abort ();
1652 }
1653}
40fb9820 1654
0dfbf9d7
L
1655static INLINE int
1656cpu_flags_all_zero (const union i386_cpu_flags *x)
1657{
1658 switch (ARRAY_SIZE(x->array))
1659 {
75f8266a
KL
1660 case 5:
1661 if (x->array[4])
1662 return 0;
1663 /* Fall through. */
53467f57
IT
1664 case 4:
1665 if (x->array[3])
1666 return 0;
1667 /* Fall through. */
0dfbf9d7
L
1668 case 3:
1669 if (x->array[2])
1670 return 0;
1a0670f3 1671 /* Fall through. */
0dfbf9d7
L
1672 case 2:
1673 if (x->array[1])
1674 return 0;
1a0670f3 1675 /* Fall through. */
0dfbf9d7
L
1676 case 1:
1677 return !x->array[0];
1678 default:
1679 abort ();
1680 }
1681}
1682
0dfbf9d7
L
1683static INLINE int
1684cpu_flags_equal (const union i386_cpu_flags *x,
1685 const union i386_cpu_flags *y)
1686{
1687 switch (ARRAY_SIZE(x->array))
1688 {
75f8266a
KL
1689 case 5:
1690 if (x->array[4] != y->array[4])
1691 return 0;
1692 /* Fall through. */
53467f57
IT
1693 case 4:
1694 if (x->array[3] != y->array[3])
1695 return 0;
1696 /* Fall through. */
0dfbf9d7
L
1697 case 3:
1698 if (x->array[2] != y->array[2])
1699 return 0;
1a0670f3 1700 /* Fall through. */
0dfbf9d7
L
1701 case 2:
1702 if (x->array[1] != y->array[1])
1703 return 0;
1a0670f3 1704 /* Fall through. */
0dfbf9d7
L
1705 case 1:
1706 return x->array[0] == y->array[0];
1707 break;
1708 default:
1709 abort ();
1710 }
1711}
c6fb90c8
L
1712
1713static INLINE int
1714cpu_flags_check_cpu64 (i386_cpu_flags f)
1715{
1716 return !((flag_code == CODE_64BIT && f.bitfield.cpuno64)
1717 || (flag_code != CODE_64BIT && f.bitfield.cpu64));
40fb9820
L
1718}
1719
c6fb90c8
L
1720static INLINE i386_cpu_flags
1721cpu_flags_and (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1722{
c6fb90c8
L
1723 switch (ARRAY_SIZE (x.array))
1724 {
75f8266a
KL
1725 case 5:
1726 x.array [4] &= y.array [4];
1727 /* Fall through. */
53467f57
IT
1728 case 4:
1729 x.array [3] &= y.array [3];
1730 /* Fall through. */
c6fb90c8
L
1731 case 3:
1732 x.array [2] &= y.array [2];
1a0670f3 1733 /* Fall through. */
c6fb90c8
L
1734 case 2:
1735 x.array [1] &= y.array [1];
1a0670f3 1736 /* Fall through. */
c6fb90c8
L
1737 case 1:
1738 x.array [0] &= y.array [0];
1739 break;
1740 default:
1741 abort ();
1742 }
1743 return x;
1744}
40fb9820 1745
c6fb90c8
L
1746static INLINE i386_cpu_flags
1747cpu_flags_or (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1748{
c6fb90c8 1749 switch (ARRAY_SIZE (x.array))
40fb9820 1750 {
75f8266a
KL
1751 case 5:
1752 x.array [4] |= y.array [4];
1753 /* Fall through. */
53467f57
IT
1754 case 4:
1755 x.array [3] |= y.array [3];
1756 /* Fall through. */
c6fb90c8
L
1757 case 3:
1758 x.array [2] |= y.array [2];
1a0670f3 1759 /* Fall through. */
c6fb90c8
L
1760 case 2:
1761 x.array [1] |= y.array [1];
1a0670f3 1762 /* Fall through. */
c6fb90c8
L
1763 case 1:
1764 x.array [0] |= y.array [0];
40fb9820
L
1765 break;
1766 default:
1767 abort ();
1768 }
40fb9820
L
1769 return x;
1770}
1771
309d3373
JB
1772static INLINE i386_cpu_flags
1773cpu_flags_and_not (i386_cpu_flags x, i386_cpu_flags y)
1774{
1775 switch (ARRAY_SIZE (x.array))
1776 {
75f8266a
KL
1777 case 5:
1778 x.array [4] &= ~y.array [4];
1779 /* Fall through. */
53467f57
IT
1780 case 4:
1781 x.array [3] &= ~y.array [3];
1782 /* Fall through. */
309d3373
JB
1783 case 3:
1784 x.array [2] &= ~y.array [2];
1a0670f3 1785 /* Fall through. */
309d3373
JB
1786 case 2:
1787 x.array [1] &= ~y.array [1];
1a0670f3 1788 /* Fall through. */
309d3373
JB
1789 case 1:
1790 x.array [0] &= ~y.array [0];
1791 break;
1792 default:
1793 abort ();
1794 }
1795 return x;
1796}
1797
6c0946d0
JB
1798static const i386_cpu_flags avx512 = CPU_ANY_AVX512F_FLAGS;
1799
c0f3af97
L
1800#define CPU_FLAGS_ARCH_MATCH 0x1
1801#define CPU_FLAGS_64BIT_MATCH 0x2
1802
c0f3af97 1803#define CPU_FLAGS_PERFECT_MATCH \
db12e14e 1804 (CPU_FLAGS_ARCH_MATCH | CPU_FLAGS_64BIT_MATCH)
c0f3af97
L
1805
1806/* Return CPU flags match bits. */
3629bb00 1807
40fb9820 1808static int
d3ce72d0 1809cpu_flags_match (const insn_template *t)
40fb9820 1810{
c0f3af97
L
1811 i386_cpu_flags x = t->cpu_flags;
1812 int match = cpu_flags_check_cpu64 (x) ? CPU_FLAGS_64BIT_MATCH : 0;
40fb9820
L
1813
1814 x.bitfield.cpu64 = 0;
1815 x.bitfield.cpuno64 = 0;
1816
0dfbf9d7 1817 if (cpu_flags_all_zero (&x))
c0f3af97
L
1818 {
1819 /* This instruction is available on all archs. */
db12e14e 1820 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 1821 }
3629bb00
L
1822 else
1823 {
c0f3af97 1824 /* This instruction is available only on some archs. */
3629bb00
L
1825 i386_cpu_flags cpu = cpu_arch_flags;
1826
ab592e75
JB
1827 /* AVX512VL is no standalone feature - match it and then strip it. */
1828 if (x.bitfield.cpuavx512vl && !cpu.bitfield.cpuavx512vl)
1829 return match;
1830 x.bitfield.cpuavx512vl = 0;
1831
22c36940
JB
1832 /* AVX and AVX2 present at the same time express an operand size
1833 dependency - strip AVX2 for the purposes here. The operand size
1834 dependent check occurs in check_vecOperands(). */
1835 if (x.bitfield.cpuavx && x.bitfield.cpuavx2)
1836 x.bitfield.cpuavx2 = 0;
1837
3629bb00 1838 cpu = cpu_flags_and (x, cpu);
c0f3af97
L
1839 if (!cpu_flags_all_zero (&cpu))
1840 {
57392598 1841 if (x.bitfield.cpuavx)
a5ff0eb2 1842 {
929f69fa 1843 /* We need to check a few extra flags with AVX. */
b9d49817 1844 if (cpu.bitfield.cpuavx
40d231b4
JB
1845 && (!t->opcode_modifier.sse2avx
1846 || (sse2avx && !i.prefix[DATA_PREFIX]))
b9d49817 1847 && (!x.bitfield.cpuaes || cpu.bitfield.cpuaes)
929f69fa 1848 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
b9d49817
JB
1849 && (!x.bitfield.cpupclmul || cpu.bitfield.cpupclmul))
1850 match |= CPU_FLAGS_ARCH_MATCH;
a5ff0eb2 1851 }
929f69fa
JB
1852 else if (x.bitfield.cpuavx512f)
1853 {
1854 /* We need to check a few extra flags with AVX512F. */
1855 if (cpu.bitfield.cpuavx512f
1856 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
1857 && (!x.bitfield.cpuvaes || cpu.bitfield.cpuvaes)
1858 && (!x.bitfield.cpuvpclmulqdq || cpu.bitfield.cpuvpclmulqdq))
1859 match |= CPU_FLAGS_ARCH_MATCH;
1860 }
a5ff0eb2 1861 else
db12e14e 1862 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 1863 }
3629bb00 1864 }
c0f3af97 1865 return match;
40fb9820
L
1866}
1867
c6fb90c8
L
1868static INLINE i386_operand_type
1869operand_type_and (i386_operand_type x, i386_operand_type y)
40fb9820 1870{
bab6aec1
JB
1871 if (x.bitfield.class != y.bitfield.class)
1872 x.bitfield.class = ClassNone;
75e5731b
JB
1873 if (x.bitfield.instance != y.bitfield.instance)
1874 x.bitfield.instance = InstanceNone;
bab6aec1 1875
c6fb90c8
L
1876 switch (ARRAY_SIZE (x.array))
1877 {
1878 case 3:
1879 x.array [2] &= y.array [2];
1a0670f3 1880 /* Fall through. */
c6fb90c8
L
1881 case 2:
1882 x.array [1] &= y.array [1];
1a0670f3 1883 /* Fall through. */
c6fb90c8
L
1884 case 1:
1885 x.array [0] &= y.array [0];
1886 break;
1887 default:
1888 abort ();
1889 }
1890 return x;
40fb9820
L
1891}
1892
73053c1f
JB
1893static INLINE i386_operand_type
1894operand_type_and_not (i386_operand_type x, i386_operand_type y)
1895{
bab6aec1 1896 gas_assert (y.bitfield.class == ClassNone);
75e5731b 1897 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 1898
73053c1f
JB
1899 switch (ARRAY_SIZE (x.array))
1900 {
1901 case 3:
1902 x.array [2] &= ~y.array [2];
1903 /* Fall through. */
1904 case 2:
1905 x.array [1] &= ~y.array [1];
1906 /* Fall through. */
1907 case 1:
1908 x.array [0] &= ~y.array [0];
1909 break;
1910 default:
1911 abort ();
1912 }
1913 return x;
1914}
1915
c6fb90c8
L
1916static INLINE i386_operand_type
1917operand_type_or (i386_operand_type x, i386_operand_type y)
40fb9820 1918{
bab6aec1
JB
1919 gas_assert (x.bitfield.class == ClassNone ||
1920 y.bitfield.class == ClassNone ||
1921 x.bitfield.class == y.bitfield.class);
75e5731b
JB
1922 gas_assert (x.bitfield.instance == InstanceNone ||
1923 y.bitfield.instance == InstanceNone ||
1924 x.bitfield.instance == y.bitfield.instance);
bab6aec1 1925
c6fb90c8 1926 switch (ARRAY_SIZE (x.array))
40fb9820 1927 {
c6fb90c8
L
1928 case 3:
1929 x.array [2] |= y.array [2];
1a0670f3 1930 /* Fall through. */
c6fb90c8
L
1931 case 2:
1932 x.array [1] |= y.array [1];
1a0670f3 1933 /* Fall through. */
c6fb90c8
L
1934 case 1:
1935 x.array [0] |= y.array [0];
40fb9820
L
1936 break;
1937 default:
1938 abort ();
1939 }
c6fb90c8
L
1940 return x;
1941}
40fb9820 1942
c6fb90c8
L
1943static INLINE i386_operand_type
1944operand_type_xor (i386_operand_type x, i386_operand_type y)
1945{
bab6aec1 1946 gas_assert (y.bitfield.class == ClassNone);
75e5731b 1947 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 1948
c6fb90c8
L
1949 switch (ARRAY_SIZE (x.array))
1950 {
1951 case 3:
1952 x.array [2] ^= y.array [2];
1a0670f3 1953 /* Fall through. */
c6fb90c8
L
1954 case 2:
1955 x.array [1] ^= y.array [1];
1a0670f3 1956 /* Fall through. */
c6fb90c8
L
1957 case 1:
1958 x.array [0] ^= y.array [0];
1959 break;
1960 default:
1961 abort ();
1962 }
40fb9820
L
1963 return x;
1964}
1965
05909f23
JB
1966static const i386_operand_type anydisp = {
1967 .bitfield = { .disp8 = 1, .disp16 = 1, .disp32 = 1, .disp64 = 1 }
1968};
40fb9820
L
1969
1970enum operand_type
1971{
1972 reg,
40fb9820
L
1973 imm,
1974 disp,
1975 anymem
1976};
1977
c6fb90c8 1978static INLINE int
40fb9820
L
1979operand_type_check (i386_operand_type t, enum operand_type c)
1980{
1981 switch (c)
1982 {
1983 case reg:
bab6aec1 1984 return t.bitfield.class == Reg;
40fb9820 1985
40fb9820
L
1986 case imm:
1987 return (t.bitfield.imm8
1988 || t.bitfield.imm8s
1989 || t.bitfield.imm16
1990 || t.bitfield.imm32
1991 || t.bitfield.imm32s
1992 || t.bitfield.imm64);
1993
1994 case disp:
1995 return (t.bitfield.disp8
1996 || t.bitfield.disp16
1997 || t.bitfield.disp32
40fb9820
L
1998 || t.bitfield.disp64);
1999
2000 case anymem:
2001 return (t.bitfield.disp8
2002 || t.bitfield.disp16
2003 || t.bitfield.disp32
40fb9820
L
2004 || t.bitfield.disp64
2005 || t.bitfield.baseindex);
2006
2007 default:
2008 abort ();
2009 }
2cfe26b6
AM
2010
2011 return 0;
40fb9820
L
2012}
2013
7a54636a
L
2014/* Return 1 if there is no conflict in 8bit/16bit/32bit/64bit/80bit size
2015 between operand GIVEN and opeand WANTED for instruction template T. */
5c07affc
L
2016
2017static INLINE int
7a54636a
L
2018match_operand_size (const insn_template *t, unsigned int wanted,
2019 unsigned int given)
5c07affc 2020{
3ac21baa
JB
2021 return !((i.types[given].bitfield.byte
2022 && !t->operand_types[wanted].bitfield.byte)
2023 || (i.types[given].bitfield.word
2024 && !t->operand_types[wanted].bitfield.word)
2025 || (i.types[given].bitfield.dword
2026 && !t->operand_types[wanted].bitfield.dword)
2027 || (i.types[given].bitfield.qword
9db83a32
JB
2028 && (!t->operand_types[wanted].bitfield.qword
2029 /* Don't allow 64-bit (memory) operands outside of 64-bit
2030 mode, when they're used where a 64-bit GPR could also
2031 be used. Checking is needed for Intel Syntax only. */
2032 || (intel_syntax
2033 && flag_code != CODE_64BIT
2034 && (t->operand_types[wanted].bitfield.class == Reg
2035 || t->operand_types[wanted].bitfield.class == Accum
2036 || t->opcode_modifier.isstring))))
3ac21baa
JB
2037 || (i.types[given].bitfield.tbyte
2038 && !t->operand_types[wanted].bitfield.tbyte));
5c07affc
L
2039}
2040
dd40ce22
L
2041/* Return 1 if there is no conflict in SIMD register between operand
2042 GIVEN and opeand WANTED for instruction template T. */
1b54b8d7
JB
2043
2044static INLINE int
dd40ce22
L
2045match_simd_size (const insn_template *t, unsigned int wanted,
2046 unsigned int given)
1b54b8d7 2047{
3ac21baa
JB
2048 return !((i.types[given].bitfield.xmmword
2049 && !t->operand_types[wanted].bitfield.xmmword)
2050 || (i.types[given].bitfield.ymmword
2051 && !t->operand_types[wanted].bitfield.ymmword)
2052 || (i.types[given].bitfield.zmmword
260cd341
LC
2053 && !t->operand_types[wanted].bitfield.zmmword)
2054 || (i.types[given].bitfield.tmmword
2055 && !t->operand_types[wanted].bitfield.tmmword));
1b54b8d7
JB
2056}
2057
7a54636a
L
2058/* Return 1 if there is no conflict in any size between operand GIVEN
2059 and opeand WANTED for instruction template T. */
5c07affc
L
2060
2061static INLINE int
dd40ce22
L
2062match_mem_size (const insn_template *t, unsigned int wanted,
2063 unsigned int given)
5c07affc 2064{
7a54636a 2065 return (match_operand_size (t, wanted, given)
3ac21baa 2066 && !((i.types[given].bitfield.unspecified
5273a3cd 2067 && !i.broadcast.type
a5748e0d 2068 && !i.broadcast.bytes
3ac21baa
JB
2069 && !t->operand_types[wanted].bitfield.unspecified)
2070 || (i.types[given].bitfield.fword
2071 && !t->operand_types[wanted].bitfield.fword)
1b54b8d7
JB
2072 /* For scalar opcode templates to allow register and memory
2073 operands at the same time, some special casing is needed
d6793fa1
JB
2074 here. Also for v{,p}broadcast*, {,v}pmov{s,z}*, and
2075 down-conversion vpmov*. */
3528c362 2076 || ((t->operand_types[wanted].bitfield.class == RegSIMD
bc49bfd8
JB
2077 && t->operand_types[wanted].bitfield.byte
2078 + t->operand_types[wanted].bitfield.word
2079 + t->operand_types[wanted].bitfield.dword
2080 + t->operand_types[wanted].bitfield.qword
2081 > !!t->opcode_modifier.broadcast)
3ac21baa
JB
2082 ? (i.types[given].bitfield.xmmword
2083 || i.types[given].bitfield.ymmword
2084 || i.types[given].bitfield.zmmword)
2085 : !match_simd_size(t, wanted, given))));
5c07affc
L
2086}
2087
3ac21baa
JB
2088/* Return value has MATCH_STRAIGHT set if there is no size conflict on any
2089 operands for instruction template T, and it has MATCH_REVERSE set if there
2090 is no size conflict on any operands for the template with operands reversed
2091 (and the template allows for reversing in the first place). */
5c07affc 2092
3ac21baa
JB
2093#define MATCH_STRAIGHT 1
2094#define MATCH_REVERSE 2
2095
2096static INLINE unsigned int
d3ce72d0 2097operand_size_match (const insn_template *t)
5c07affc 2098{
3ac21baa 2099 unsigned int j, match = MATCH_STRAIGHT;
5c07affc 2100
0cfa3eb3 2101 /* Don't check non-absolute jump instructions. */
5c07affc 2102 if (t->opcode_modifier.jump
0cfa3eb3 2103 && t->opcode_modifier.jump != JUMP_ABSOLUTE)
5c07affc
L
2104 return match;
2105
2106 /* Check memory and accumulator operand size. */
2107 for (j = 0; j < i.operands; j++)
2108 {
3528c362
JB
2109 if (i.types[j].bitfield.class != Reg
2110 && i.types[j].bitfield.class != RegSIMD
255571cd 2111 && t->opcode_modifier.operandconstraint == ANY_SIZE)
5c07affc
L
2112 continue;
2113
bab6aec1 2114 if (t->operand_types[j].bitfield.class == Reg
7a54636a 2115 && !match_operand_size (t, j, j))
5c07affc
L
2116 {
2117 match = 0;
2118 break;
2119 }
2120
3528c362 2121 if (t->operand_types[j].bitfield.class == RegSIMD
3ac21baa 2122 && !match_simd_size (t, j, j))
1b54b8d7
JB
2123 {
2124 match = 0;
2125 break;
2126 }
2127
75e5731b 2128 if (t->operand_types[j].bitfield.instance == Accum
7a54636a 2129 && (!match_operand_size (t, j, j) || !match_simd_size (t, j, j)))
1b54b8d7
JB
2130 {
2131 match = 0;
2132 break;
2133 }
2134
c48dadc9 2135 if ((i.flags[j] & Operand_Mem) && !match_mem_size (t, j, j))
5c07affc
L
2136 {
2137 match = 0;
2138 break;
2139 }
2140 }
2141
3ac21baa 2142 if (!t->opcode_modifier.d)
7b94647a 2143 return match;
5c07affc
L
2144
2145 /* Check reverse. */
aa180741 2146 gas_assert (i.operands >= 2);
5c07affc 2147
f5eb1d70 2148 for (j = 0; j < i.operands; j++)
5c07affc 2149 {
f5eb1d70
JB
2150 unsigned int given = i.operands - j - 1;
2151
aa180741 2152 /* For FMA4 and XOP insns VEX.W controls just the first two
8bd915b7 2153 register operands. */
aa180741 2154 if (t->cpu_flags.bitfield.cpufma4 || t->cpu_flags.bitfield.cpuxop)
8bd915b7
JB
2155 given = j < 2 ? 1 - j : j;
2156
bab6aec1 2157 if (t->operand_types[j].bitfield.class == Reg
f5eb1d70 2158 && !match_operand_size (t, j, given))
7b94647a 2159 return match;
5c07affc 2160
3528c362 2161 if (t->operand_types[j].bitfield.class == RegSIMD
f5eb1d70 2162 && !match_simd_size (t, j, given))
7b94647a 2163 return match;
dbbc8b7e 2164
75e5731b 2165 if (t->operand_types[j].bitfield.instance == Accum
f5eb1d70
JB
2166 && (!match_operand_size (t, j, given)
2167 || !match_simd_size (t, j, given)))
7b94647a 2168 return match;
dbbc8b7e 2169
f5eb1d70 2170 if ((i.flags[given] & Operand_Mem) && !match_mem_size (t, j, given))
7b94647a 2171 return match;
5c07affc
L
2172 }
2173
3ac21baa 2174 return match | MATCH_REVERSE;
5c07affc
L
2175}
2176
c6fb90c8 2177static INLINE int
40fb9820
L
2178operand_type_match (i386_operand_type overlap,
2179 i386_operand_type given)
2180{
2181 i386_operand_type temp = overlap;
2182
7d5e4556 2183 temp.bitfield.unspecified = 0;
5c07affc
L
2184 temp.bitfield.byte = 0;
2185 temp.bitfield.word = 0;
2186 temp.bitfield.dword = 0;
2187 temp.bitfield.fword = 0;
2188 temp.bitfield.qword = 0;
2189 temp.bitfield.tbyte = 0;
2190 temp.bitfield.xmmword = 0;
c0f3af97 2191 temp.bitfield.ymmword = 0;
43234a1e 2192 temp.bitfield.zmmword = 0;
260cd341 2193 temp.bitfield.tmmword = 0;
0dfbf9d7 2194 if (operand_type_all_zero (&temp))
891edac4 2195 goto mismatch;
40fb9820 2196
6f2f06be 2197 if (given.bitfield.baseindex == overlap.bitfield.baseindex)
891edac4
L
2198 return 1;
2199
dc1e8a47 2200 mismatch:
a65babc9 2201 i.error = operand_type_mismatch;
891edac4 2202 return 0;
40fb9820
L
2203}
2204
7d5e4556 2205/* If given types g0 and g1 are registers they must be of the same type
10c17abd 2206 unless the expected operand type register overlap is null.
8ee52bcf 2207 Intel syntax sized memory operands are also checked here. */
40fb9820 2208
c6fb90c8 2209static INLINE int
dc821c5f 2210operand_type_register_match (i386_operand_type g0,
40fb9820 2211 i386_operand_type t0,
40fb9820
L
2212 i386_operand_type g1,
2213 i386_operand_type t1)
2214{
bab6aec1 2215 if (g0.bitfield.class != Reg
3528c362 2216 && g0.bitfield.class != RegSIMD
8ee52bcf
JB
2217 && (g0.bitfield.unspecified
2218 || !operand_type_check (g0, anymem)))
40fb9820
L
2219 return 1;
2220
bab6aec1 2221 if (g1.bitfield.class != Reg
3528c362 2222 && g1.bitfield.class != RegSIMD
8ee52bcf
JB
2223 && (g1.bitfield.unspecified
2224 || !operand_type_check (g1, anymem)))
40fb9820
L
2225 return 1;
2226
dc821c5f
JB
2227 if (g0.bitfield.byte == g1.bitfield.byte
2228 && g0.bitfield.word == g1.bitfield.word
2229 && g0.bitfield.dword == g1.bitfield.dword
10c17abd
JB
2230 && g0.bitfield.qword == g1.bitfield.qword
2231 && g0.bitfield.xmmword == g1.bitfield.xmmword
2232 && g0.bitfield.ymmword == g1.bitfield.ymmword
2233 && g0.bitfield.zmmword == g1.bitfield.zmmword)
40fb9820
L
2234 return 1;
2235
c4d09633
JB
2236 /* If expectations overlap in no more than a single size, all is fine. */
2237 g0 = operand_type_and (t0, t1);
2238 if (g0.bitfield.byte
2239 + g0.bitfield.word
2240 + g0.bitfield.dword
2241 + g0.bitfield.qword
2242 + g0.bitfield.xmmword
2243 + g0.bitfield.ymmword
2244 + g0.bitfield.zmmword <= 1)
891edac4
L
2245 return 1;
2246
a65babc9 2247 i.error = register_type_mismatch;
891edac4
L
2248
2249 return 0;
40fb9820
L
2250}
2251
4c692bc7
JB
2252static INLINE unsigned int
2253register_number (const reg_entry *r)
2254{
2255 unsigned int nr = r->reg_num;
2256
2257 if (r->reg_flags & RegRex)
2258 nr += 8;
2259
200cbe0f
L
2260 if (r->reg_flags & RegVRex)
2261 nr += 16;
2262
4c692bc7
JB
2263 return nr;
2264}
2265
252b5132 2266static INLINE unsigned int
40fb9820 2267mode_from_disp_size (i386_operand_type t)
252b5132 2268{
b5014f7a 2269 if (t.bitfield.disp8)
40fb9820
L
2270 return 1;
2271 else if (t.bitfield.disp16
a775efc8 2272 || t.bitfield.disp32)
40fb9820
L
2273 return 2;
2274 else
2275 return 0;
252b5132
RH
2276}
2277
2278static INLINE int
65879393 2279fits_in_signed_byte (addressT num)
252b5132 2280{
65879393 2281 return num + 0x80 <= 0xff;
47926f60 2282}
252b5132
RH
2283
2284static INLINE int
65879393 2285fits_in_unsigned_byte (addressT num)
252b5132 2286{
65879393 2287 return num <= 0xff;
47926f60 2288}
252b5132
RH
2289
2290static INLINE int
65879393 2291fits_in_unsigned_word (addressT num)
252b5132 2292{
65879393 2293 return num <= 0xffff;
47926f60 2294}
252b5132
RH
2295
2296static INLINE int
65879393 2297fits_in_signed_word (addressT num)
252b5132 2298{
65879393 2299 return num + 0x8000 <= 0xffff;
47926f60 2300}
2a962e6d 2301
3e73aa7c 2302static INLINE int
65879393 2303fits_in_signed_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2304{
2305#ifndef BFD64
2306 return 1;
2307#else
65879393 2308 return num + 0x80000000 <= 0xffffffff;
3e73aa7c
JH
2309#endif
2310} /* fits_in_signed_long() */
2a962e6d 2311
3e73aa7c 2312static INLINE int
65879393 2313fits_in_unsigned_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2314{
2315#ifndef BFD64
2316 return 1;
2317#else
65879393 2318 return num <= 0xffffffff;
3e73aa7c
JH
2319#endif
2320} /* fits_in_unsigned_long() */
252b5132 2321
a442cac5
JB
2322static INLINE valueT extend_to_32bit_address (addressT num)
2323{
2324#ifdef BFD64
2325 if (fits_in_unsigned_long(num))
2326 return (num ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
2327
2328 if (!fits_in_signed_long (num))
2329 return num & 0xffffffff;
2330#endif
2331
2332 return num;
2333}
2334
43234a1e 2335static INLINE int
b5014f7a 2336fits_in_disp8 (offsetT num)
43234a1e
L
2337{
2338 int shift = i.memshift;
2339 unsigned int mask;
2340
2341 if (shift == -1)
2342 abort ();
2343
2344 mask = (1 << shift) - 1;
2345
2346 /* Return 0 if NUM isn't properly aligned. */
2347 if ((num & mask))
2348 return 0;
2349
2350 /* Check if NUM will fit in 8bit after shift. */
2351 return fits_in_signed_byte (num >> shift);
2352}
2353
a683cc34
SP
2354static INLINE int
2355fits_in_imm4 (offsetT num)
2356{
2357 return (num & 0xf) == num;
2358}
2359
40fb9820 2360static i386_operand_type
e3bb37b5 2361smallest_imm_type (offsetT num)
252b5132 2362{
40fb9820 2363 i386_operand_type t;
7ab9ffdd 2364
0dfbf9d7 2365 operand_type_set (&t, 0);
40fb9820
L
2366 t.bitfield.imm64 = 1;
2367
2368 if (cpu_arch_tune != PROCESSOR_I486 && num == 1)
e413e4e9
AM
2369 {
2370 /* This code is disabled on the 486 because all the Imm1 forms
2371 in the opcode table are slower on the i486. They're the
2372 versions with the implicitly specified single-position
2373 displacement, which has another syntax if you really want to
2374 use that form. */
40fb9820
L
2375 t.bitfield.imm1 = 1;
2376 t.bitfield.imm8 = 1;
2377 t.bitfield.imm8s = 1;
2378 t.bitfield.imm16 = 1;
2379 t.bitfield.imm32 = 1;
2380 t.bitfield.imm32s = 1;
2381 }
2382 else if (fits_in_signed_byte (num))
2383 {
c34d1cc9
JB
2384 if (fits_in_unsigned_byte (num))
2385 t.bitfield.imm8 = 1;
40fb9820
L
2386 t.bitfield.imm8s = 1;
2387 t.bitfield.imm16 = 1;
2388 t.bitfield.imm32 = 1;
2389 t.bitfield.imm32s = 1;
2390 }
2391 else if (fits_in_unsigned_byte (num))
2392 {
2393 t.bitfield.imm8 = 1;
2394 t.bitfield.imm16 = 1;
2395 t.bitfield.imm32 = 1;
2396 t.bitfield.imm32s = 1;
2397 }
2398 else if (fits_in_signed_word (num) || fits_in_unsigned_word (num))
2399 {
2400 t.bitfield.imm16 = 1;
2401 t.bitfield.imm32 = 1;
2402 t.bitfield.imm32s = 1;
2403 }
2404 else if (fits_in_signed_long (num))
2405 {
2406 t.bitfield.imm32 = 1;
2407 t.bitfield.imm32s = 1;
2408 }
2409 else if (fits_in_unsigned_long (num))
2410 t.bitfield.imm32 = 1;
2411
2412 return t;
47926f60 2413}
252b5132 2414
847f7ad4 2415static offsetT
e3bb37b5 2416offset_in_range (offsetT val, int size)
847f7ad4 2417{
508866be 2418 addressT mask;
ba2adb93 2419
847f7ad4
AM
2420 switch (size)
2421 {
508866be
L
2422 case 1: mask = ((addressT) 1 << 8) - 1; break;
2423 case 2: mask = ((addressT) 1 << 16) - 1; break;
3e73aa7c 2424#ifdef BFD64
64965897 2425 case 4: mask = ((addressT) 1 << 32) - 1; break;
3e73aa7c 2426#endif
64965897 2427 case sizeof (val): return val;
47926f60 2428 default: abort ();
847f7ad4
AM
2429 }
2430
4fe51f7d 2431 if ((val & ~mask) != 0 && (-val & ~mask) != 0)
f493c217
AM
2432 as_warn (_("0x%" PRIx64 " shortened to 0x%" PRIx64),
2433 (uint64_t) val, (uint64_t) (val & mask));
847f7ad4 2434
847f7ad4
AM
2435 return val & mask;
2436}
2437
76d3f746
JB
2438static INLINE const char *insn_name (const insn_template *t)
2439{
5c139202 2440 return &i386_mnemonics[t->mnem_off];
76d3f746
JB
2441}
2442
c32fa91d
L
2443enum PREFIX_GROUP
2444{
2445 PREFIX_EXIST = 0,
2446 PREFIX_LOCK,
2447 PREFIX_REP,
04ef582a 2448 PREFIX_DS,
c32fa91d
L
2449 PREFIX_OTHER
2450};
2451
2452/* Returns
2453 a. PREFIX_EXIST if attempting to add a prefix where one from the
2454 same class already exists.
2455 b. PREFIX_LOCK if lock prefix is added.
2456 c. PREFIX_REP if rep/repne prefix is added.
04ef582a
L
2457 d. PREFIX_DS if ds prefix is added.
2458 e. PREFIX_OTHER if other prefix is added.
c32fa91d
L
2459 */
2460
2461static enum PREFIX_GROUP
e3bb37b5 2462add_prefix (unsigned int prefix)
252b5132 2463{
c32fa91d 2464 enum PREFIX_GROUP ret = PREFIX_OTHER;
b1905489 2465 unsigned int q;
252b5132 2466
29b0f896
AM
2467 if (prefix >= REX_OPCODE && prefix < REX_OPCODE + 16
2468 && flag_code == CODE_64BIT)
b1905489 2469 {
161a04f6 2470 if ((i.prefix[REX_PREFIX] & prefix & REX_W)
44846f29
JB
2471 || (i.prefix[REX_PREFIX] & prefix & REX_R)
2472 || (i.prefix[REX_PREFIX] & prefix & REX_X)
2473 || (i.prefix[REX_PREFIX] & prefix & REX_B))
c32fa91d 2474 ret = PREFIX_EXIST;
b1905489
JB
2475 q = REX_PREFIX;
2476 }
3e73aa7c 2477 else
b1905489
JB
2478 {
2479 switch (prefix)
2480 {
2481 default:
2482 abort ();
2483
b1905489 2484 case DS_PREFIX_OPCODE:
04ef582a
L
2485 ret = PREFIX_DS;
2486 /* Fall through. */
2487 case CS_PREFIX_OPCODE:
b1905489
JB
2488 case ES_PREFIX_OPCODE:
2489 case FS_PREFIX_OPCODE:
2490 case GS_PREFIX_OPCODE:
2491 case SS_PREFIX_OPCODE:
2492 q = SEG_PREFIX;
2493 break;
2494
2495 case REPNE_PREFIX_OPCODE:
2496 case REPE_PREFIX_OPCODE:
c32fa91d
L
2497 q = REP_PREFIX;
2498 ret = PREFIX_REP;
2499 break;
2500
b1905489 2501 case LOCK_PREFIX_OPCODE:
c32fa91d
L
2502 q = LOCK_PREFIX;
2503 ret = PREFIX_LOCK;
b1905489
JB
2504 break;
2505
2506 case FWAIT_OPCODE:
2507 q = WAIT_PREFIX;
2508 break;
2509
2510 case ADDR_PREFIX_OPCODE:
2511 q = ADDR_PREFIX;
2512 break;
2513
2514 case DATA_PREFIX_OPCODE:
2515 q = DATA_PREFIX;
2516 break;
2517 }
2518 if (i.prefix[q] != 0)
c32fa91d 2519 ret = PREFIX_EXIST;
b1905489 2520 }
252b5132 2521
b1905489 2522 if (ret)
252b5132 2523 {
b1905489
JB
2524 if (!i.prefix[q])
2525 ++i.prefixes;
2526 i.prefix[q] |= prefix;
252b5132 2527 }
b1905489
JB
2528 else
2529 as_bad (_("same type of prefix used twice"));
252b5132 2530
252b5132
RH
2531 return ret;
2532}
2533
2534static void
78f12dd3 2535update_code_flag (int value, int check)
eecb386c 2536{
78f12dd3
L
2537 PRINTF_LIKE ((*as_error));
2538
1e9cc1c2 2539 flag_code = (enum flag_code) value;
40fb9820
L
2540 if (flag_code == CODE_64BIT)
2541 {
2542 cpu_arch_flags.bitfield.cpu64 = 1;
2543 cpu_arch_flags.bitfield.cpuno64 = 0;
40fb9820
L
2544 }
2545 else
2546 {
2547 cpu_arch_flags.bitfield.cpu64 = 0;
2548 cpu_arch_flags.bitfield.cpuno64 = 1;
40fb9820
L
2549 }
2550 if (value == CODE_64BIT && !cpu_arch_flags.bitfield.cpulm )
3e73aa7c 2551 {
78f12dd3
L
2552 if (check)
2553 as_error = as_fatal;
2554 else
2555 as_error = as_bad;
2556 (*as_error) (_("64bit mode not supported on `%s'."),
2557 cpu_arch_name ? cpu_arch_name : default_arch);
3e73aa7c 2558 }
40fb9820 2559 if (value == CODE_32BIT && !cpu_arch_flags.bitfield.cpui386)
3e73aa7c 2560 {
78f12dd3
L
2561 if (check)
2562 as_error = as_fatal;
2563 else
2564 as_error = as_bad;
2565 (*as_error) (_("32bit mode not supported on `%s'."),
2566 cpu_arch_name ? cpu_arch_name : default_arch);
3e73aa7c 2567 }
eecb386c
AM
2568 stackop_size = '\0';
2569}
2570
78f12dd3
L
2571static void
2572set_code_flag (int value)
2573{
2574 update_code_flag (value, 0);
2575}
2576
eecb386c 2577static void
e3bb37b5 2578set_16bit_gcc_code_flag (int new_code_flag)
252b5132 2579{
1e9cc1c2 2580 flag_code = (enum flag_code) new_code_flag;
40fb9820
L
2581 if (flag_code != CODE_16BIT)
2582 abort ();
2583 cpu_arch_flags.bitfield.cpu64 = 0;
2584 cpu_arch_flags.bitfield.cpuno64 = 1;
9306ca4a 2585 stackop_size = LONG_MNEM_SUFFIX;
252b5132
RH
2586}
2587
2588static void
e3bb37b5 2589set_intel_syntax (int syntax_flag)
252b5132
RH
2590{
2591 /* Find out if register prefixing is specified. */
2592 int ask_naked_reg = 0;
2593
2594 SKIP_WHITESPACE ();
29b0f896 2595 if (!is_end_of_line[(unsigned char) *input_line_pointer])
252b5132 2596 {
d02603dc
NC
2597 char *string;
2598 int e = get_symbol_name (&string);
252b5132 2599
47926f60 2600 if (strcmp (string, "prefix") == 0)
252b5132 2601 ask_naked_reg = 1;
47926f60 2602 else if (strcmp (string, "noprefix") == 0)
252b5132
RH
2603 ask_naked_reg = -1;
2604 else
d0b47220 2605 as_bad (_("bad argument to syntax directive."));
d02603dc 2606 (void) restore_line_pointer (e);
252b5132
RH
2607 }
2608 demand_empty_rest_of_line ();
c3332e24 2609
252b5132
RH
2610 intel_syntax = syntax_flag;
2611
2612 if (ask_naked_reg == 0)
f86103b7
AM
2613 allow_naked_reg = (intel_syntax
2614 && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
252b5132
RH
2615 else
2616 allow_naked_reg = (ask_naked_reg < 0);
9306ca4a 2617
ee86248c 2618 expr_set_rank (O_full_ptr, syntax_flag ? 10 : 0);
7ab9ffdd 2619
e4a3b5a4 2620 register_prefix = allow_naked_reg ? "" : "%";
252b5132
RH
2621}
2622
1efbbeb4
L
2623static void
2624set_intel_mnemonic (int mnemonic_flag)
2625{
e1d4d893 2626 intel_mnemonic = mnemonic_flag;
1efbbeb4
L
2627}
2628
db51cc60
L
2629static void
2630set_allow_index_reg (int flag)
2631{
2632 allow_index_reg = flag;
2633}
2634
cb19c032 2635static void
7bab8ab5 2636set_check (int what)
cb19c032 2637{
7bab8ab5
JB
2638 enum check_kind *kind;
2639 const char *str;
2640
2641 if (what)
2642 {
2643 kind = &operand_check;
2644 str = "operand";
2645 }
2646 else
2647 {
2648 kind = &sse_check;
2649 str = "sse";
2650 }
2651
cb19c032
L
2652 SKIP_WHITESPACE ();
2653
2654 if (!is_end_of_line[(unsigned char) *input_line_pointer])
2655 {
d02603dc
NC
2656 char *string;
2657 int e = get_symbol_name (&string);
cb19c032
L
2658
2659 if (strcmp (string, "none") == 0)
7bab8ab5 2660 *kind = check_none;
cb19c032 2661 else if (strcmp (string, "warning") == 0)
7bab8ab5 2662 *kind = check_warning;
cb19c032 2663 else if (strcmp (string, "error") == 0)
7bab8ab5 2664 *kind = check_error;
cb19c032 2665 else
7bab8ab5 2666 as_bad (_("bad argument to %s_check directive."), str);
d02603dc 2667 (void) restore_line_pointer (e);
cb19c032
L
2668 }
2669 else
7bab8ab5 2670 as_bad (_("missing argument for %s_check directive"), str);
cb19c032
L
2671
2672 demand_empty_rest_of_line ();
2673}
2674
8a9036a4
L
2675static void
2676check_cpu_arch_compatible (const char *name ATTRIBUTE_UNUSED,
1e9cc1c2 2677 i386_cpu_flags new_flag ATTRIBUTE_UNUSED)
8a9036a4
L
2678{
2679#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2680 static const char *arch;
2681
c085ab00 2682 /* Intel MCU is only supported on ELF. */
8a9036a4
L
2683 if (!IS_ELF)
2684 return;
2685
2686 if (!arch)
2687 {
2688 /* Use cpu_arch_name if it is set in md_parse_option. Otherwise
2689 use default_arch. */
2690 arch = cpu_arch_name;
2691 if (!arch)
2692 arch = default_arch;
2693 }
2694
81486035 2695 /* If we are targeting Intel MCU, we must enable it. */
648d04db
JB
2696 if ((get_elf_backend_data (stdoutput)->elf_machine_code == EM_IAMCU)
2697 == new_flag.bitfield.cpuiamcu)
81486035
L
2698 return;
2699
8a9036a4
L
2700 as_bad (_("`%s' is not supported on `%s'"), name, arch);
2701#endif
2702}
2703
8180707f
JB
2704static void
2705extend_cpu_sub_arch_name (const char *name)
2706{
2707 if (cpu_sub_arch_name)
2708 cpu_sub_arch_name = reconcat (cpu_sub_arch_name, cpu_sub_arch_name,
ae89daec 2709 ".", name, (const char *) NULL);
8180707f 2710 else
ae89daec 2711 cpu_sub_arch_name = concat (".", name, (const char *) NULL);
8180707f
JB
2712}
2713
e413e4e9 2714static void
e3bb37b5 2715set_cpu_arch (int dummy ATTRIBUTE_UNUSED)
e413e4e9 2716{
f68697e8
JB
2717 typedef struct arch_stack_entry
2718 {
2719 const struct arch_stack_entry *prev;
2720 const char *name;
2721 char *sub_name;
2722 i386_cpu_flags flags;
2723 i386_cpu_flags isa_flags;
2724 enum processor_type isa;
2725 enum flag_code flag_code;
2726 char stackop_size;
2727 bool no_cond_jump_promotion;
2728 } arch_stack_entry;
2729 static const arch_stack_entry *arch_stack_top;
2730
47926f60 2731 SKIP_WHITESPACE ();
e413e4e9 2732
29b0f896 2733 if (!is_end_of_line[(unsigned char) *input_line_pointer])
e413e4e9 2734 {
3ce2ebcf
JB
2735 char *s;
2736 int e = get_symbol_name (&s);
2737 const char *string = s;
2738 unsigned int j = 0;
40fb9820 2739 i386_cpu_flags flags;
e413e4e9 2740
3ce2ebcf
JB
2741 if (strcmp (string, "default") == 0)
2742 {
2743 if (strcmp (default_arch, "iamcu") == 0)
2744 string = default_arch;
2745 else
2746 {
2747 static const i386_cpu_flags cpu_unknown_flags = CPU_UNKNOWN_FLAGS;
2748
2749 cpu_arch_name = NULL;
2750 free (cpu_sub_arch_name);
2751 cpu_sub_arch_name = NULL;
2752 cpu_arch_flags = cpu_unknown_flags;
2753 if (flag_code == CODE_64BIT)
2754 {
2755 cpu_arch_flags.bitfield.cpu64 = 1;
2756 cpu_arch_flags.bitfield.cpuno64 = 0;
2757 }
2758 else
2759 {
2760 cpu_arch_flags.bitfield.cpu64 = 0;
2761 cpu_arch_flags.bitfield.cpuno64 = 1;
2762 }
2763 cpu_arch_isa = PROCESSOR_UNKNOWN;
ae89daec 2764 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
3ce2ebcf
JB
2765 if (!cpu_arch_tune_set)
2766 {
2767 cpu_arch_tune = cpu_arch_isa;
2768 cpu_arch_tune_flags = cpu_arch_isa_flags;
2769 }
2770
2771 j = ARRAY_SIZE (cpu_arch) + 1;
2772 }
2773 }
f68697e8
JB
2774 else if (strcmp (string, "push") == 0)
2775 {
2776 arch_stack_entry *top = XNEW (arch_stack_entry);
2777
2778 top->name = cpu_arch_name;
2779 if (cpu_sub_arch_name)
2780 top->sub_name = xstrdup (cpu_sub_arch_name);
2781 else
2782 top->sub_name = NULL;
2783 top->flags = cpu_arch_flags;
2784 top->isa = cpu_arch_isa;
2785 top->isa_flags = cpu_arch_isa_flags;
2786 top->flag_code = flag_code;
2787 top->stackop_size = stackop_size;
2788 top->no_cond_jump_promotion = no_cond_jump_promotion;
2789
2790 top->prev = arch_stack_top;
2791 arch_stack_top = top;
2792
2793 (void) restore_line_pointer (e);
2794 demand_empty_rest_of_line ();
2795 return;
2796 }
2797 else if (strcmp (string, "pop") == 0)
2798 {
2799 const arch_stack_entry *top = arch_stack_top;
2800
2801 if (!top)
2802 as_bad (_(".arch stack is empty"));
2803 else if (top->flag_code != flag_code
2804 || top->stackop_size != stackop_size)
2805 {
2806 static const unsigned int bits[] = {
2807 [CODE_16BIT] = 16,
2808 [CODE_32BIT] = 32,
2809 [CODE_64BIT] = 64,
2810 };
2811
2812 as_bad (_("this `.arch pop' requires `.code%u%s' to be in effect"),
2813 bits[top->flag_code],
2814 top->stackop_size == LONG_MNEM_SUFFIX ? "gcc" : "");
2815 }
2816 else
2817 {
2818 arch_stack_top = top->prev;
2819
2820 cpu_arch_name = top->name;
2821 free (cpu_sub_arch_name);
2822 cpu_sub_arch_name = top->sub_name;
2823 cpu_arch_flags = top->flags;
2824 cpu_arch_isa = top->isa;
2825 cpu_arch_isa_flags = top->isa_flags;
2826 no_cond_jump_promotion = top->no_cond_jump_promotion;
2827
2828 XDELETE (top);
2829 }
2830
2831 (void) restore_line_pointer (e);
2832 demand_empty_rest_of_line ();
2833 return;
2834 }
3ce2ebcf
JB
2835
2836 for (; j < ARRAY_SIZE (cpu_arch); j++)
e413e4e9 2837 {
ae89daec
JB
2838 if (strcmp (string + (*string == '.'), cpu_arch[j].name) == 0
2839 && (*string == '.') == (cpu_arch[j].type == PROCESSOR_NONE))
e413e4e9 2840 {
5c6af06e
JB
2841 if (*string != '.')
2842 {
ae89daec 2843 check_cpu_arch_compatible (string, cpu_arch[j].enable);
648d04db 2844
91d6fa6a 2845 cpu_arch_name = cpu_arch[j].name;
d92c7521 2846 free (cpu_sub_arch_name);
5c6af06e 2847 cpu_sub_arch_name = NULL;
ae89daec 2848 cpu_arch_flags = cpu_arch[j].enable;
40fb9820
L
2849 if (flag_code == CODE_64BIT)
2850 {
2851 cpu_arch_flags.bitfield.cpu64 = 1;
2852 cpu_arch_flags.bitfield.cpuno64 = 0;
2853 }
2854 else
2855 {
2856 cpu_arch_flags.bitfield.cpu64 = 0;
2857 cpu_arch_flags.bitfield.cpuno64 = 1;
2858 }
91d6fa6a 2859 cpu_arch_isa = cpu_arch[j].type;
ae89daec 2860 cpu_arch_isa_flags = cpu_arch[j].enable;
ccc9c027
L
2861 if (!cpu_arch_tune_set)
2862 {
2863 cpu_arch_tune = cpu_arch_isa;
2864 cpu_arch_tune_flags = cpu_arch_isa_flags;
2865 }
d59a54c2 2866 pre_386_16bit_warned = false;
5c6af06e
JB
2867 break;
2868 }
40fb9820 2869
ae89daec
JB
2870 if (cpu_flags_all_zero (&cpu_arch[j].enable))
2871 continue;
2872
293f5f65 2873 flags = cpu_flags_or (cpu_arch_flags,
ae89daec 2874 cpu_arch[j].enable);
81486035 2875
5b64d091 2876 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
5c6af06e 2877 {
ae89daec 2878 extend_cpu_sub_arch_name (string + 1);
40fb9820 2879 cpu_arch_flags = flags;
a586129e 2880 cpu_arch_isa_flags = flags;
5c6af06e 2881 }
0089dace
L
2882 else
2883 cpu_arch_isa_flags
2884 = cpu_flags_or (cpu_arch_isa_flags,
ae89daec 2885 cpu_arch[j].enable);
d02603dc 2886 (void) restore_line_pointer (e);
5c6af06e
JB
2887 demand_empty_rest_of_line ();
2888 return;
e413e4e9
AM
2889 }
2890 }
293f5f65 2891
ae89daec 2892 if (startswith (string, ".no") && j >= ARRAY_SIZE (cpu_arch))
293f5f65 2893 {
33eaf5de 2894 /* Disable an ISA extension. */
ae89daec
JB
2895 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
2896 if (cpu_arch[j].type == PROCESSOR_NONE
2897 && strcmp (string + 3, cpu_arch[j].name) == 0)
293f5f65
L
2898 {
2899 flags = cpu_flags_and_not (cpu_arch_flags,
ae89daec 2900 cpu_arch[j].disable);
293f5f65
L
2901 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
2902 {
ae89daec 2903 extend_cpu_sub_arch_name (string + 1);
293f5f65
L
2904 cpu_arch_flags = flags;
2905 cpu_arch_isa_flags = flags;
2906 }
2907 (void) restore_line_pointer (e);
2908 demand_empty_rest_of_line ();
2909 return;
2910 }
293f5f65
L
2911 }
2912
3ce2ebcf 2913 if (j == ARRAY_SIZE (cpu_arch))
e413e4e9
AM
2914 as_bad (_("no such architecture: `%s'"), string);
2915
2916 *input_line_pointer = e;
2917 }
2918 else
2919 as_bad (_("missing cpu architecture"));
2920
fddf5b5b
AM
2921 no_cond_jump_promotion = 0;
2922 if (*input_line_pointer == ','
29b0f896 2923 && !is_end_of_line[(unsigned char) input_line_pointer[1]])
fddf5b5b 2924 {
d02603dc
NC
2925 char *string;
2926 char e;
2927
2928 ++input_line_pointer;
2929 e = get_symbol_name (&string);
fddf5b5b
AM
2930
2931 if (strcmp (string, "nojumps") == 0)
2932 no_cond_jump_promotion = 1;
2933 else if (strcmp (string, "jumps") == 0)
2934 ;
2935 else
2936 as_bad (_("no such architecture modifier: `%s'"), string);
2937
d02603dc 2938 (void) restore_line_pointer (e);
fddf5b5b
AM
2939 }
2940
e413e4e9
AM
2941 demand_empty_rest_of_line ();
2942}
2943
8a9036a4
L
2944enum bfd_architecture
2945i386_arch (void)
2946{
c085ab00 2947 if (cpu_arch_isa == PROCESSOR_IAMCU)
81486035
L
2948 {
2949 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2950 || flag_code == CODE_64BIT)
2951 as_fatal (_("Intel MCU is 32bit ELF only"));
2952 return bfd_arch_iamcu;
2953 }
8a9036a4
L
2954 else
2955 return bfd_arch_i386;
2956}
2957
b9d79e03 2958unsigned long
7016a5d5 2959i386_mach (void)
b9d79e03 2960{
d34049e8 2961 if (startswith (default_arch, "x86_64"))
8a9036a4 2962 {
c085ab00 2963 if (default_arch[6] == '\0')
8a9036a4 2964 return bfd_mach_x86_64;
351f65ca
L
2965 else
2966 return bfd_mach_x64_32;
8a9036a4 2967 }
5197d474
L
2968 else if (!strcmp (default_arch, "i386")
2969 || !strcmp (default_arch, "iamcu"))
81486035
L
2970 {
2971 if (cpu_arch_isa == PROCESSOR_IAMCU)
2972 {
2973 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
2974 as_fatal (_("Intel MCU is 32bit ELF only"));
2975 return bfd_mach_i386_iamcu;
2976 }
2977 else
2978 return bfd_mach_i386_i386;
2979 }
b9d79e03 2980 else
2b5d6a91 2981 as_fatal (_("unknown architecture"));
b9d79e03 2982}
b9d79e03 2983\f
99f0fb12
JB
2984#include "opcodes/i386-tbl.h"
2985
252b5132 2986void
7016a5d5 2987md_begin (void)
252b5132 2988{
86fa6981
L
2989 /* Support pseudo prefixes like {disp32}. */
2990 lex_type ['{'] = LEX_BEGIN_NAME;
2991
47926f60 2992 /* Initialize op_hash hash table. */
629310ab 2993 op_hash = str_htab_create ();
252b5132
RH
2994
2995 {
65f440c8
JB
2996 const insn_template *const *sets = i386_op_sets;
2997 const insn_template *const *end = sets + ARRAY_SIZE (i386_op_sets) - 1;
2998
2999 /* Type checks to compensate for the conversion through void * which
3000 occurs during hash table insertion / lookup. */
eb993861
JB
3001 (void) sizeof (sets == &current_templates->start);
3002 (void) sizeof (end == &current_templates->end);
65f440c8 3003 for (; sets < end; ++sets)
76d3f746
JB
3004 if (str_hash_insert (op_hash, insn_name (*sets), sets, 0))
3005 as_fatal (_("duplicate %s"), insn_name (*sets));
252b5132
RH
3006 }
3007
47926f60 3008 /* Initialize reg_hash hash table. */
629310ab 3009 reg_hash = str_htab_create ();
252b5132 3010 {
29b0f896 3011 const reg_entry *regtab;
c3fe08fa 3012 unsigned int regtab_size = i386_regtab_size;
252b5132 3013
c3fe08fa 3014 for (regtab = i386_regtab; regtab_size--; regtab++)
6225c532 3015 {
6288d05f
JB
3016 switch (regtab->reg_type.bitfield.class)
3017 {
3018 case Reg:
34684862
JB
3019 if (regtab->reg_type.bitfield.dword)
3020 {
3021 if (regtab->reg_type.bitfield.instance == Accum)
3022 reg_eax = regtab;
3023 }
3024 else if (regtab->reg_type.bitfield.tbyte)
6288d05f
JB
3025 {
3026 /* There's no point inserting st(<N>) in the hash table, as
3027 parentheses aren't included in register_chars[] anyway. */
3028 if (regtab->reg_type.bitfield.instance != Accum)
3029 continue;
3030 reg_st0 = regtab;
3031 }
3032 break;
3033
5e042380
JB
3034 case SReg:
3035 switch (regtab->reg_num)
3036 {
3037 case 0: reg_es = regtab; break;
3038 case 2: reg_ss = regtab; break;
3039 case 3: reg_ds = regtab; break;
3040 }
3041 break;
3042
6288d05f
JB
3043 case RegMask:
3044 if (!regtab->reg_num)
3045 reg_k0 = regtab;
3046 break;
3047 }
3048
6225c532
JB
3049 if (str_hash_insert (reg_hash, regtab->reg_name, regtab, 0) != NULL)
3050 as_fatal (_("duplicate %s"), regtab->reg_name);
6225c532 3051 }
252b5132
RH
3052 }
3053
47926f60 3054 /* Fill in lexical tables: mnemonic_chars, operand_chars. */
252b5132 3055 {
29b0f896
AM
3056 int c;
3057 char *p;
252b5132
RH
3058
3059 for (c = 0; c < 256; c++)
3060 {
014fbcda 3061 if (ISDIGIT (c) || ISLOWER (c))
252b5132
RH
3062 {
3063 mnemonic_chars[c] = c;
3064 register_chars[c] = c;
3065 operand_chars[c] = c;
3066 }
3882b010 3067 else if (ISUPPER (c))
252b5132 3068 {
3882b010 3069 mnemonic_chars[c] = TOLOWER (c);
252b5132
RH
3070 register_chars[c] = mnemonic_chars[c];
3071 operand_chars[c] = c;
3072 }
43234a1e 3073 else if (c == '{' || c == '}')
86fa6981
L
3074 {
3075 mnemonic_chars[c] = c;
3076 operand_chars[c] = c;
3077 }
b3983e5f
JB
3078#ifdef SVR4_COMMENT_CHARS
3079 else if (c == '\\' && strchr (i386_comment_chars, '/'))
3080 operand_chars[c] = c;
3081#endif
252b5132 3082
4795cd4a
JB
3083 if (c >= 128)
3084 operand_chars[c] = c;
252b5132
RH
3085 }
3086
32137342 3087#ifdef LEX_QM
32137342 3088 operand_chars['?'] = '?';
252b5132 3089#endif
c0f3af97 3090 mnemonic_chars['_'] = '_';
791fe849 3091 mnemonic_chars['-'] = '-';
0003779b 3092 mnemonic_chars['.'] = '.';
252b5132
RH
3093
3094 for (p = operand_special_chars; *p != '\0'; p++)
3095 operand_chars[(unsigned char) *p] = *p;
3096 }
3097
a4447b93
RH
3098 if (flag_code == CODE_64BIT)
3099 {
ca19b261
KT
3100#if defined (OBJ_COFF) && defined (TE_PE)
3101 x86_dwarf2_return_column = (OUTPUT_FLAVOR == bfd_target_coff_flavour
3102 ? 32 : 16);
3103#else
a4447b93 3104 x86_dwarf2_return_column = 16;
ca19b261 3105#endif
61ff971f 3106 x86_cie_data_alignment = -8;
b52c4ee4
IB
3107#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3108 x86_sframe_cfa_sp_reg = 7;
3109 x86_sframe_cfa_fp_reg = 6;
3110#endif
a4447b93
RH
3111 }
3112 else
3113 {
3114 x86_dwarf2_return_column = 8;
3115 x86_cie_data_alignment = -4;
3116 }
e379e5f3
L
3117
3118 /* NB: FUSED_JCC_PADDING frag must have sufficient room so that it
3119 can be turned into BRANCH_PREFIX frag. */
3120 if (align_branch_prefix_size > MAX_FUSED_JCC_PADDING_SIZE)
3121 abort ();
252b5132
RH
3122}
3123
3124void
e3bb37b5 3125i386_print_statistics (FILE *file)
252b5132 3126{
629310ab
ML
3127 htab_print_statistics (file, "i386 opcode", op_hash);
3128 htab_print_statistics (file, "i386 register", reg_hash);
252b5132 3129}
654d6f31
AM
3130
3131void
3132i386_md_end (void)
3133{
3134 htab_delete (op_hash);
3135 htab_delete (reg_hash);
3136}
252b5132 3137\f
252b5132
RH
3138#ifdef DEBUG386
3139
ce8a8b2f 3140/* Debugging routines for md_assemble. */
d3ce72d0 3141static void pte (insn_template *);
40fb9820 3142static void pt (i386_operand_type);
e3bb37b5
L
3143static void pe (expressionS *);
3144static void ps (symbolS *);
252b5132
RH
3145
3146static void
2c703856 3147pi (const char *line, i386_insn *x)
252b5132 3148{
09137c09 3149 unsigned int j;
252b5132
RH
3150
3151 fprintf (stdout, "%s: template ", line);
3152 pte (&x->tm);
09f131f2
JH
3153 fprintf (stdout, " address: base %s index %s scale %x\n",
3154 x->base_reg ? x->base_reg->reg_name : "none",
3155 x->index_reg ? x->index_reg->reg_name : "none",
3156 x->log2_scale_factor);
3157 fprintf (stdout, " modrm: mode %x reg %x reg/mem %x\n",
252b5132 3158 x->rm.mode, x->rm.reg, x->rm.regmem);
09f131f2
JH
3159 fprintf (stdout, " sib: base %x index %x scale %x\n",
3160 x->sib.base, x->sib.index, x->sib.scale);
3161 fprintf (stdout, " rex: 64bit %x extX %x extY %x extZ %x\n",
161a04f6
L
3162 (x->rex & REX_W) != 0,
3163 (x->rex & REX_R) != 0,
3164 (x->rex & REX_X) != 0,
3165 (x->rex & REX_B) != 0);
09137c09 3166 for (j = 0; j < x->operands; j++)
252b5132 3167 {
09137c09
SP
3168 fprintf (stdout, " #%d: ", j + 1);
3169 pt (x->types[j]);
252b5132 3170 fprintf (stdout, "\n");
bab6aec1 3171 if (x->types[j].bitfield.class == Reg
3528c362
JB
3172 || x->types[j].bitfield.class == RegMMX
3173 || x->types[j].bitfield.class == RegSIMD
dd6b8a0b 3174 || x->types[j].bitfield.class == RegMask
00cee14f 3175 || x->types[j].bitfield.class == SReg
4a5c67ed
JB
3176 || x->types[j].bitfield.class == RegCR
3177 || x->types[j].bitfield.class == RegDR
dd6b8a0b
JB
3178 || x->types[j].bitfield.class == RegTR
3179 || x->types[j].bitfield.class == RegBND)
09137c09
SP
3180 fprintf (stdout, "%s\n", x->op[j].regs->reg_name);
3181 if (operand_type_check (x->types[j], imm))
3182 pe (x->op[j].imms);
3183 if (operand_type_check (x->types[j], disp))
3184 pe (x->op[j].disps);
252b5132
RH
3185 }
3186}
3187
3188static void
d3ce72d0 3189pte (insn_template *t)
252b5132 3190{
b933fa4b 3191 static const unsigned char opc_pfx[] = { 0, 0x66, 0xf3, 0xf2 };
441f6aca 3192 static const char *const opc_spc[] = {
0cc78721 3193 NULL, "0f", "0f38", "0f3a", NULL, "evexmap5", "evexmap6", NULL,
441f6aca
JB
3194 "XOP08", "XOP09", "XOP0A",
3195 };
09137c09 3196 unsigned int j;
441f6aca 3197
252b5132 3198 fprintf (stdout, " %d operands ", t->operands);
441f6aca
JB
3199 if (opc_pfx[t->opcode_modifier.opcodeprefix])
3200 fprintf (stdout, "pfx %x ", opc_pfx[t->opcode_modifier.opcodeprefix]);
ddb62495
JB
3201 if (opc_spc[t->opcode_space])
3202 fprintf (stdout, "space %s ", opc_spc[t->opcode_space]);
47926f60 3203 fprintf (stdout, "opcode %x ", t->base_opcode);
252b5132
RH
3204 if (t->extension_opcode != None)
3205 fprintf (stdout, "ext %x ", t->extension_opcode);
40fb9820 3206 if (t->opcode_modifier.d)
252b5132 3207 fprintf (stdout, "D");
40fb9820 3208 if (t->opcode_modifier.w)
252b5132
RH
3209 fprintf (stdout, "W");
3210 fprintf (stdout, "\n");
09137c09 3211 for (j = 0; j < t->operands; j++)
252b5132 3212 {
09137c09
SP
3213 fprintf (stdout, " #%d type ", j + 1);
3214 pt (t->operand_types[j]);
252b5132
RH
3215 fprintf (stdout, "\n");
3216 }
3217}
3218
3219static void
e3bb37b5 3220pe (expressionS *e)
252b5132 3221{
24eab124 3222 fprintf (stdout, " operation %d\n", e->X_op);
b8281767
AM
3223 fprintf (stdout, " add_number %" PRId64 " (%" PRIx64 ")\n",
3224 (int64_t) e->X_add_number, (uint64_t) (valueT) e->X_add_number);
252b5132
RH
3225 if (e->X_add_symbol)
3226 {
3227 fprintf (stdout, " add_symbol ");
3228 ps (e->X_add_symbol);
3229 fprintf (stdout, "\n");
3230 }
3231 if (e->X_op_symbol)
3232 {
3233 fprintf (stdout, " op_symbol ");
3234 ps (e->X_op_symbol);
3235 fprintf (stdout, "\n");
3236 }
3237}
3238
3239static void
e3bb37b5 3240ps (symbolS *s)
252b5132
RH
3241{
3242 fprintf (stdout, "%s type %s%s",
3243 S_GET_NAME (s),
3244 S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
3245 segment_name (S_GET_SEGMENT (s)));
3246}
3247
7b81dfbb 3248static struct type_name
252b5132 3249 {
40fb9820
L
3250 i386_operand_type mask;
3251 const char *name;
252b5132 3252 }
7b81dfbb 3253const type_names[] =
252b5132 3254{
05909f23
JB
3255 { { .bitfield = { .class = Reg, .byte = 1 } }, "r8" },
3256 { { .bitfield = { .class = Reg, .word = 1 } }, "r16" },
3257 { { .bitfield = { .class = Reg, .dword = 1 } }, "r32" },
3258 { { .bitfield = { .class = Reg, .qword = 1 } }, "r64" },
3259 { { .bitfield = { .instance = Accum, .byte = 1 } }, "acc8" },
3260 { { .bitfield = { .instance = Accum, .word = 1 } }, "acc16" },
3261 { { .bitfield = { .instance = Accum, .dword = 1 } }, "acc32" },
3262 { { .bitfield = { .instance = Accum, .qword = 1 } }, "acc64" },
3263 { { .bitfield = { .imm8 = 1 } }, "i8" },
3264 { { .bitfield = { .imm8s = 1 } }, "i8s" },
3265 { { .bitfield = { .imm16 = 1 } }, "i16" },
3266 { { .bitfield = { .imm32 = 1 } }, "i32" },
3267 { { .bitfield = { .imm32s = 1 } }, "i32s" },
3268 { { .bitfield = { .imm64 = 1 } }, "i64" },
3269 { { .bitfield = { .imm1 = 1 } }, "i1" },
3270 { { .bitfield = { .baseindex = 1 } }, "BaseIndex" },
3271 { { .bitfield = { .disp8 = 1 } }, "d8" },
3272 { { .bitfield = { .disp16 = 1 } }, "d16" },
3273 { { .bitfield = { .disp32 = 1 } }, "d32" },
3274 { { .bitfield = { .disp64 = 1 } }, "d64" },
3275 { { .bitfield = { .instance = RegD, .word = 1 } }, "InOutPortReg" },
3276 { { .bitfield = { .instance = RegC, .byte = 1 } }, "ShiftCount" },
3277 { { .bitfield = { .class = RegCR } }, "control reg" },
3278 { { .bitfield = { .class = RegTR } }, "test reg" },
3279 { { .bitfield = { .class = RegDR } }, "debug reg" },
3280 { { .bitfield = { .class = Reg, .tbyte = 1 } }, "FReg" },
3281 { { .bitfield = { .instance = Accum, .tbyte = 1 } }, "FAcc" },
3282 { { .bitfield = { .class = SReg } }, "SReg" },
3283 { { .bitfield = { .class = RegMMX } }, "rMMX" },
3284 { { .bitfield = { .class = RegSIMD, .xmmword = 1 } }, "rXMM" },
3285 { { .bitfield = { .class = RegSIMD, .ymmword = 1 } }, "rYMM" },
3286 { { .bitfield = { .class = RegSIMD, .zmmword = 1 } }, "rZMM" },
3287 { { .bitfield = { .class = RegSIMD, .tmmword = 1 } }, "rTMM" },
3288 { { .bitfield = { .class = RegMask } }, "Mask reg" },
252b5132
RH
3289};
3290
3291static void
40fb9820 3292pt (i386_operand_type t)
252b5132 3293{
40fb9820 3294 unsigned int j;
c6fb90c8 3295 i386_operand_type a;
252b5132 3296
40fb9820 3297 for (j = 0; j < ARRAY_SIZE (type_names); j++)
c6fb90c8
L
3298 {
3299 a = operand_type_and (t, type_names[j].mask);
2c703856 3300 if (operand_type_equal (&a, &type_names[j].mask))
c6fb90c8
L
3301 fprintf (stdout, "%s, ", type_names[j].name);
3302 }
252b5132
RH
3303 fflush (stdout);
3304}
3305
3306#endif /* DEBUG386 */
3307\f
252b5132 3308static bfd_reloc_code_real_type
3956db08 3309reloc (unsigned int size,
64e74474
AM
3310 int pcrel,
3311 int sign,
3312 bfd_reloc_code_real_type other)
252b5132 3313{
47926f60 3314 if (other != NO_RELOC)
3956db08 3315 {
91d6fa6a 3316 reloc_howto_type *rel;
3956db08
JB
3317
3318 if (size == 8)
3319 switch (other)
3320 {
64e74474
AM
3321 case BFD_RELOC_X86_64_GOT32:
3322 return BFD_RELOC_X86_64_GOT64;
3323 break;
553d1284
L
3324 case BFD_RELOC_X86_64_GOTPLT64:
3325 return BFD_RELOC_X86_64_GOTPLT64;
3326 break;
64e74474
AM
3327 case BFD_RELOC_X86_64_PLTOFF64:
3328 return BFD_RELOC_X86_64_PLTOFF64;
3329 break;
3330 case BFD_RELOC_X86_64_GOTPC32:
3331 other = BFD_RELOC_X86_64_GOTPC64;
3332 break;
3333 case BFD_RELOC_X86_64_GOTPCREL:
3334 other = BFD_RELOC_X86_64_GOTPCREL64;
3335 break;
3336 case BFD_RELOC_X86_64_TPOFF32:
3337 other = BFD_RELOC_X86_64_TPOFF64;
3338 break;
3339 case BFD_RELOC_X86_64_DTPOFF32:
3340 other = BFD_RELOC_X86_64_DTPOFF64;
3341 break;
3342 default:
3343 break;
3956db08 3344 }
e05278af 3345
8ce3d284 3346#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
3347 if (other == BFD_RELOC_SIZE32)
3348 {
3349 if (size == 8)
1ab668bf 3350 other = BFD_RELOC_SIZE64;
8fd4256d 3351 if (pcrel)
1ab668bf
AM
3352 {
3353 as_bad (_("there are no pc-relative size relocations"));
3354 return NO_RELOC;
3355 }
8fd4256d 3356 }
8ce3d284 3357#endif
8fd4256d 3358
e05278af 3359 /* Sign-checking 4-byte relocations in 16-/32-bit code is pointless. */
f2d8a97c 3360 if (size == 4 && (flag_code != CODE_64BIT || disallow_64bit_reloc))
e05278af
JB
3361 sign = -1;
3362
91d6fa6a
NC
3363 rel = bfd_reloc_type_lookup (stdoutput, other);
3364 if (!rel)
3956db08 3365 as_bad (_("unknown relocation (%u)"), other);
91d6fa6a 3366 else if (size != bfd_get_reloc_size (rel))
3956db08 3367 as_bad (_("%u-byte relocation cannot be applied to %u-byte field"),
91d6fa6a 3368 bfd_get_reloc_size (rel),
3956db08 3369 size);
91d6fa6a 3370 else if (pcrel && !rel->pc_relative)
3956db08 3371 as_bad (_("non-pc-relative relocation for pc-relative field"));
91d6fa6a 3372 else if ((rel->complain_on_overflow == complain_overflow_signed
3956db08 3373 && !sign)
91d6fa6a 3374 || (rel->complain_on_overflow == complain_overflow_unsigned
64e74474 3375 && sign > 0))
3956db08
JB
3376 as_bad (_("relocated field and relocation type differ in signedness"));
3377 else
3378 return other;
3379 return NO_RELOC;
3380 }
252b5132
RH
3381
3382 if (pcrel)
3383 {
3e73aa7c 3384 if (!sign)
3956db08 3385 as_bad (_("there are no unsigned pc-relative relocations"));
252b5132
RH
3386 switch (size)
3387 {
3388 case 1: return BFD_RELOC_8_PCREL;
3389 case 2: return BFD_RELOC_16_PCREL;
d258b828 3390 case 4: return BFD_RELOC_32_PCREL;
d6ab8113 3391 case 8: return BFD_RELOC_64_PCREL;
252b5132 3392 }
3956db08 3393 as_bad (_("cannot do %u byte pc-relative relocation"), size);
252b5132
RH
3394 }
3395 else
3396 {
3956db08 3397 if (sign > 0)
e5cb08ac 3398 switch (size)
3e73aa7c
JH
3399 {
3400 case 4: return BFD_RELOC_X86_64_32S;
3401 }
3402 else
3403 switch (size)
3404 {
3405 case 1: return BFD_RELOC_8;
3406 case 2: return BFD_RELOC_16;
3407 case 4: return BFD_RELOC_32;
3408 case 8: return BFD_RELOC_64;
3409 }
3956db08
JB
3410 as_bad (_("cannot do %s %u byte relocation"),
3411 sign > 0 ? "signed" : "unsigned", size);
252b5132
RH
3412 }
3413
0cc9e1d3 3414 return NO_RELOC;
252b5132
RH
3415}
3416
47926f60
KH
3417/* Here we decide which fixups can be adjusted to make them relative to
3418 the beginning of the section instead of the symbol. Basically we need
3419 to make sure that the dynamic relocations are done correctly, so in
3420 some cases we force the original symbol to be used. */
3421
252b5132 3422int
e3bb37b5 3423tc_i386_fix_adjustable (fixS *fixP ATTRIBUTE_UNUSED)
252b5132 3424{
6d249963 3425#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 3426 if (!IS_ELF)
31312f95
AM
3427 return 1;
3428
a161fe53
AM
3429 /* Don't adjust pc-relative references to merge sections in 64-bit
3430 mode. */
3431 if (use_rela_relocations
3432 && (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0
3433 && fixP->fx_pcrel)
252b5132 3434 return 0;
31312f95 3435
8d01d9a9
AJ
3436 /* The x86_64 GOTPCREL are represented as 32bit PCrel relocations
3437 and changed later by validate_fix. */
3438 if (GOT_symbol && fixP->fx_subsy == GOT_symbol
3439 && fixP->fx_r_type == BFD_RELOC_32_PCREL)
3440 return 0;
3441
8fd4256d
L
3442 /* Adjust_reloc_syms doesn't know about the GOT. Need to keep symbol
3443 for size relocations. */
3444 if (fixP->fx_r_type == BFD_RELOC_SIZE32
3445 || fixP->fx_r_type == BFD_RELOC_SIZE64
3446 || fixP->fx_r_type == BFD_RELOC_386_GOTOFF
252b5132 3447 || fixP->fx_r_type == BFD_RELOC_386_GOT32
02a86693 3448 || fixP->fx_r_type == BFD_RELOC_386_GOT32X
13ae64f3
JJ
3449 || fixP->fx_r_type == BFD_RELOC_386_TLS_GD
3450 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDM
3451 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDO_32
3452 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE_32
37e55690
JJ
3453 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE
3454 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTIE
13ae64f3
JJ
3455 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE_32
3456 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE
67a4f2b7
AO
3457 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTDESC
3458 || fixP->fx_r_type == BFD_RELOC_386_TLS_DESC_CALL
3e73aa7c 3459 || fixP->fx_r_type == BFD_RELOC_X86_64_GOT32
80b3ee89 3460 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCREL
56ceb5b5
L
3461 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCRELX
3462 || fixP->fx_r_type == BFD_RELOC_X86_64_REX_GOTPCRELX
bffbf940
JJ
3463 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSGD
3464 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSLD
3465 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF32
d6ab8113 3466 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF64
bffbf940
JJ
3467 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF
3468 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF32
d6ab8113
JB
3469 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF64
3470 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTOFF64
67a4f2b7
AO
3471 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
3472 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSDESC_CALL
252b5132
RH
3473 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3474 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3475 return 0;
31312f95 3476#endif
252b5132
RH
3477 return 1;
3478}
252b5132 3479
a9aabc23
JB
3480static INLINE bool
3481want_disp32 (const insn_template *t)
3482{
3483 return flag_code != CODE_64BIT
3484 || i.prefix[ADDR_PREFIX]
7fc69528 3485 || (t->mnem_off == MN_lea
fe134c65
JB
3486 && (!i.types[1].bitfield.qword
3487 || t->opcode_modifier.size == SIZE32));
a9aabc23
JB
3488}
3489
b4cac588 3490static int
e3bb37b5 3491intel_float_operand (const char *mnemonic)
252b5132 3492{
9306ca4a
JB
3493 /* Note that the value returned is meaningful only for opcodes with (memory)
3494 operands, hence the code here is free to improperly handle opcodes that
3495 have no operands (for better performance and smaller code). */
3496
3497 if (mnemonic[0] != 'f')
3498 return 0; /* non-math */
3499
3500 switch (mnemonic[1])
3501 {
3502 /* fclex, fdecstp, fdisi, femms, feni, fincstp, finit, fsetpm, and
3503 the fs segment override prefix not currently handled because no
3504 call path can make opcodes without operands get here */
3505 case 'i':
3506 return 2 /* integer op */;
3507 case 'l':
3508 if (mnemonic[2] == 'd' && (mnemonic[3] == 'c' || mnemonic[3] == 'e'))
3509 return 3; /* fldcw/fldenv */
3510 break;
3511 case 'n':
3512 if (mnemonic[2] != 'o' /* fnop */)
3513 return 3; /* non-waiting control op */
3514 break;
3515 case 'r':
3516 if (mnemonic[2] == 's')
3517 return 3; /* frstor/frstpm */
3518 break;
3519 case 's':
3520 if (mnemonic[2] == 'a')
3521 return 3; /* fsave */
3522 if (mnemonic[2] == 't')
3523 {
3524 switch (mnemonic[3])
3525 {
3526 case 'c': /* fstcw */
3527 case 'd': /* fstdw */
3528 case 'e': /* fstenv */
3529 case 's': /* fsts[gw] */
3530 return 3;
3531 }
3532 }
3533 break;
3534 case 'x':
3535 if (mnemonic[2] == 'r' || mnemonic[2] == 's')
3536 return 0; /* fxsave/fxrstor are not really math ops */
3537 break;
3538 }
252b5132 3539
9306ca4a 3540 return 1;
252b5132
RH
3541}
3542
9a182d04
JB
3543static INLINE void
3544install_template (const insn_template *t)
3545{
3546 unsigned int l;
3547
3548 i.tm = *t;
3549
3550 /* Note that for pseudo prefixes this produces a length of 1. But for them
3551 the length isn't interesting at all. */
3552 for (l = 1; l < 4; ++l)
3553 if (!(t->base_opcode >> (8 * l)))
3554 break;
3555
3556 i.opcode_length = l;
3557}
3558
c0f3af97
L
3559/* Build the VEX prefix. */
3560
3561static void
d3ce72d0 3562build_vex_prefix (const insn_template *t)
c0f3af97
L
3563{
3564 unsigned int register_specifier;
c0f3af97 3565 unsigned int vector_length;
03751133 3566 unsigned int w;
c0f3af97
L
3567
3568 /* Check register specifier. */
3569 if (i.vex.register_specifier)
43234a1e
L
3570 {
3571 register_specifier =
3572 ~register_number (i.vex.register_specifier) & 0xf;
3573 gas_assert ((i.vex.register_specifier->reg_flags & RegVRex) == 0);
3574 }
c0f3af97
L
3575 else
3576 register_specifier = 0xf;
3577
79f0fa25
L
3578 /* Use 2-byte VEX prefix by swapping destination and source operand
3579 if there are more than 1 register operand. */
3580 if (i.reg_operands > 1
3581 && i.vec_encoding != vex_encoding_vex3
86fa6981 3582 && i.dir_encoding == dir_encoding_default
fa99fab2 3583 && i.operands == i.reg_operands
dbbc8b7e 3584 && operand_type_equal (&i.types[0], &i.types[i.operands - 1])
ddb62495 3585 && i.tm.opcode_space == SPACE_0F
dbbc8b7e 3586 && (i.tm.opcode_modifier.load || i.tm.opcode_modifier.d)
fa99fab2
L
3587 && i.rex == REX_B)
3588 {
67748abf 3589 unsigned int xchg;
fa99fab2 3590
67748abf 3591 swap_2_operands (0, i.operands - 1);
fa99fab2 3592
9c2799c2 3593 gas_assert (i.rm.mode == 3);
fa99fab2
L
3594
3595 i.rex = REX_R;
3596 xchg = i.rm.regmem;
3597 i.rm.regmem = i.rm.reg;
3598 i.rm.reg = xchg;
3599
dbbc8b7e
JB
3600 if (i.tm.opcode_modifier.d)
3601 i.tm.base_opcode ^= (i.tm.base_opcode & 0xee) != 0x6e
2c735193 3602 ? Opcode_ExtD : Opcode_SIMD_IntD;
dbbc8b7e 3603 else /* Use the next insn. */
9a182d04 3604 install_template (&t[1]);
fa99fab2
L
3605 }
3606
79dec6b7
JB
3607 /* Use 2-byte VEX prefix by swapping commutative source operands if there
3608 are no memory operands and at least 3 register ones. */
3609 if (i.reg_operands >= 3
3610 && i.vec_encoding != vex_encoding_vex3
3611 && i.reg_operands == i.operands - i.imm_operands
3612 && i.tm.opcode_modifier.vex
3613 && i.tm.opcode_modifier.commutative
33740f21
JB
3614 && (i.tm.opcode_modifier.sse2avx
3615 || (optimize > 1 && !i.no_optimize))
79dec6b7
JB
3616 && i.rex == REX_B
3617 && i.vex.register_specifier
3618 && !(i.vex.register_specifier->reg_flags & RegRex))
3619 {
3620 unsigned int xchg = i.operands - i.reg_operands;
79dec6b7 3621
ddb62495 3622 gas_assert (i.tm.opcode_space == SPACE_0F);
79dec6b7
JB
3623 gas_assert (!i.tm.opcode_modifier.sae);
3624 gas_assert (operand_type_equal (&i.types[i.operands - 2],
3625 &i.types[i.operands - 3]));
3626 gas_assert (i.rm.mode == 3);
3627
67748abf 3628 swap_2_operands (xchg, xchg + 1);
79dec6b7
JB
3629
3630 i.rex = 0;
3631 xchg = i.rm.regmem | 8;
3632 i.rm.regmem = ~register_specifier & 0xf;
3633 gas_assert (!(i.rm.regmem & 8));
3634 i.vex.register_specifier += xchg - i.rm.regmem;
3635 register_specifier = ~xchg & 0xf;
3636 }
3637
539f890d
L
3638 if (i.tm.opcode_modifier.vex == VEXScalar)
3639 vector_length = avxscalar;
10c17abd
JB
3640 else if (i.tm.opcode_modifier.vex == VEX256)
3641 vector_length = 1;
d0c2e3ec
JB
3642 else if (dot_insn () && i.tm.opcode_modifier.vex == VEX128)
3643 vector_length = 0;
539f890d 3644 else
10c17abd 3645 {
56522fc5 3646 unsigned int op;
10c17abd 3647
c7213af9
L
3648 /* Determine vector length from the last multi-length vector
3649 operand. */
10c17abd 3650 vector_length = 0;
56522fc5 3651 for (op = t->operands; op--;)
10c17abd
JB
3652 if (t->operand_types[op].bitfield.xmmword
3653 && t->operand_types[op].bitfield.ymmword
3654 && i.types[op].bitfield.ymmword)
3655 {
3656 vector_length = 1;
3657 break;
3658 }
3659 }
c0f3af97 3660
03751133
L
3661 /* Check the REX.W bit and VEXW. */
3662 if (i.tm.opcode_modifier.vexw == VEXWIG)
3663 w = (vexwig == vexw1 || (i.rex & REX_W)) ? 1 : 0;
3664 else if (i.tm.opcode_modifier.vexw)
3665 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3666 else
931d03b7 3667 w = (flag_code == CODE_64BIT ? i.rex & REX_W : vexwig == vexw1) ? 1 : 0;
03751133 3668
c0f3af97 3669 /* Use 2-byte VEX prefix if possible. */
03751133
L
3670 if (w == 0
3671 && i.vec_encoding != vex_encoding_vex3
ddb62495 3672 && i.tm.opcode_space == SPACE_0F
c0f3af97
L
3673 && (i.rex & (REX_W | REX_X | REX_B)) == 0)
3674 {
3675 /* 2-byte VEX prefix. */
3676 unsigned int r;
3677
3678 i.vex.length = 2;
3679 i.vex.bytes[0] = 0xc5;
3680
3681 /* Check the REX.R bit. */
3682 r = (i.rex & REX_R) ? 0 : 1;
3683 i.vex.bytes[1] = (r << 7
3684 | register_specifier << 3
3685 | vector_length << 2
35648716 3686 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3687 }
3688 else
3689 {
3690 /* 3-byte VEX prefix. */
f88c9eb0 3691 i.vex.length = 3;
f88c9eb0 3692
ddb62495 3693 switch (i.tm.opcode_space)
5dd85c99 3694 {
441f6aca
JB
3695 case SPACE_0F:
3696 case SPACE_0F38:
3697 case SPACE_0F3A:
80de6e00 3698 i.vex.bytes[0] = 0xc4;
7f399153 3699 break;
441f6aca
JB
3700 case SPACE_XOP08:
3701 case SPACE_XOP09:
3702 case SPACE_XOP0A:
f88c9eb0 3703 i.vex.bytes[0] = 0x8f;
7f399153
L
3704 break;
3705 default:
3706 abort ();
f88c9eb0 3707 }
c0f3af97 3708
c0f3af97
L
3709 /* The high 3 bits of the second VEX byte are 1's compliment
3710 of RXB bits from REX. */
d0c2e3ec
JB
3711 i.vex.bytes[1] = ((~i.rex & 7) << 5)
3712 | (!dot_insn () ? i.tm.opcode_space
3713 : i.insn_opcode_space);
c0f3af97 3714
c0f3af97
L
3715 i.vex.bytes[2] = (w << 7
3716 | register_specifier << 3
3717 | vector_length << 2
35648716 3718 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3719 }
3720}
3721
5b7c81bd 3722static INLINE bool
e771e7c9
JB
3723is_evex_encoding (const insn_template *t)
3724{
7091c612 3725 return t->opcode_modifier.evex || t->opcode_modifier.disp8memshift
e771e7c9 3726 || t->opcode_modifier.broadcast || t->opcode_modifier.masking
a80195f1 3727 || t->opcode_modifier.sae;
e771e7c9
JB
3728}
3729
5b7c81bd 3730static INLINE bool
7a8655d2
JB
3731is_any_vex_encoding (const insn_template *t)
3732{
7b47a312 3733 return t->opcode_modifier.vex || is_evex_encoding (t);
7a8655d2
JB
3734}
3735
a5748e0d
JB
3736static unsigned int
3737get_broadcast_bytes (const insn_template *t, bool diag)
3738{
3739 unsigned int op, bytes;
3740 const i386_operand_type *types;
3741
3742 if (i.broadcast.type)
9b345ce8 3743 return (1 << (t->opcode_modifier.broadcast - 1)) * i.broadcast.type;
a5748e0d
JB
3744
3745 gas_assert (intel_syntax);
3746
3747 for (op = 0; op < t->operands; ++op)
3748 if (t->operand_types[op].bitfield.baseindex)
3749 break;
3750
3751 gas_assert (op < t->operands);
3752
3753 if (t->opcode_modifier.evex
3754 && t->opcode_modifier.evex != EVEXDYN)
3755 switch (i.broadcast.bytes)
3756 {
3757 case 1:
3758 if (t->operand_types[op].bitfield.word)
3759 return 2;
3760 /* Fall through. */
3761 case 2:
3762 if (t->operand_types[op].bitfield.dword)
3763 return 4;
3764 /* Fall through. */
3765 case 4:
3766 if (t->operand_types[op].bitfield.qword)
3767 return 8;
3768 /* Fall through. */
3769 case 8:
3770 if (t->operand_types[op].bitfield.xmmword)
3771 return 16;
3772 if (t->operand_types[op].bitfield.ymmword)
3773 return 32;
3774 if (t->operand_types[op].bitfield.zmmword)
3775 return 64;
3776 /* Fall through. */
3777 default:
3778 abort ();
3779 }
3780
3781 gas_assert (op + 1 < t->operands);
3782
3783 if (t->operand_types[op + 1].bitfield.xmmword
3784 + t->operand_types[op + 1].bitfield.ymmword
3785 + t->operand_types[op + 1].bitfield.zmmword > 1)
3786 {
3787 types = &i.types[op + 1];
3788 diag = false;
3789 }
3790 else /* Ambiguous - guess with a preference to non-AVX512VL forms. */
3791 types = &t->operand_types[op];
3792
3793 if (types->bitfield.zmmword)
3794 bytes = 64;
3795 else if (types->bitfield.ymmword)
3796 bytes = 32;
3797 else
3798 bytes = 16;
3799
3800 if (diag)
3801 as_warn (_("ambiguous broadcast for `%s', using %u-bit form"),
76d3f746 3802 insn_name (t), bytes * 8);
a5748e0d
JB
3803
3804 return bytes;
3805}
3806
43234a1e
L
3807/* Build the EVEX prefix. */
3808
3809static void
3810build_evex_prefix (void)
3811{
35648716 3812 unsigned int register_specifier, w;
43234a1e
L
3813 rex_byte vrex_used = 0;
3814
3815 /* Check register specifier. */
3816 if (i.vex.register_specifier)
3817 {
3818 gas_assert ((i.vrex & REX_X) == 0);
3819
3820 register_specifier = i.vex.register_specifier->reg_num;
3821 if ((i.vex.register_specifier->reg_flags & RegRex))
3822 register_specifier += 8;
3823 /* The upper 16 registers are encoded in the fourth byte of the
3824 EVEX prefix. */
3825 if (!(i.vex.register_specifier->reg_flags & RegVRex))
3826 i.vex.bytes[3] = 0x8;
3827 register_specifier = ~register_specifier & 0xf;
3828 }
3829 else
3830 {
3831 register_specifier = 0xf;
3832
3833 /* Encode upper 16 vector index register in the fourth byte of
3834 the EVEX prefix. */
3835 if (!(i.vrex & REX_X))
3836 i.vex.bytes[3] = 0x8;
3837 else
3838 vrex_used |= REX_X;
3839 }
3840
43234a1e
L
3841 /* 4 byte EVEX prefix. */
3842 i.vex.length = 4;
3843 i.vex.bytes[0] = 0x62;
3844
43234a1e
L
3845 /* The high 3 bits of the second EVEX byte are 1's compliment of RXB
3846 bits from REX. */
ddb62495
JB
3847 gas_assert (i.tm.opcode_space >= SPACE_0F);
3848 gas_assert (i.tm.opcode_space <= SPACE_EVEXMAP6);
d0c2e3ec
JB
3849 i.vex.bytes[1] = ((~i.rex & 7) << 5)
3850 | (!dot_insn () ? i.tm.opcode_space
3851 : i.insn_opcode_space);
43234a1e
L
3852
3853 /* The fifth bit of the second EVEX byte is 1's compliment of the
3854 REX_R bit in VREX. */
3855 if (!(i.vrex & REX_R))
3856 i.vex.bytes[1] |= 0x10;
3857 else
3858 vrex_used |= REX_R;
3859
3860 if ((i.reg_operands + i.imm_operands) == i.operands)
3861 {
3862 /* When all operands are registers, the REX_X bit in REX is not
3863 used. We reuse it to encode the upper 16 registers, which is
3864 indicated by the REX_B bit in VREX. The REX_X bit is encoded
3865 as 1's compliment. */
3866 if ((i.vrex & REX_B))
3867 {
3868 vrex_used |= REX_B;
3869 i.vex.bytes[1] &= ~0x40;
3870 }
3871 }
3872
3873 /* EVEX instructions shouldn't need the REX prefix. */
3874 i.vrex &= ~vrex_used;
3875 gas_assert (i.vrex == 0);
3876
6865c043
L
3877 /* Check the REX.W bit and VEXW. */
3878 if (i.tm.opcode_modifier.vexw == VEXWIG)
3879 w = (evexwig == evexw1 || (i.rex & REX_W)) ? 1 : 0;
3880 else if (i.tm.opcode_modifier.vexw)
3881 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3882 else
931d03b7 3883 w = (flag_code == CODE_64BIT ? i.rex & REX_W : evexwig == evexw1) ? 1 : 0;
43234a1e 3884
43234a1e 3885 /* The third byte of the EVEX prefix. */
35648716
JB
3886 i.vex.bytes[2] = ((w << 7)
3887 | (register_specifier << 3)
3888 | 4 /* Encode the U bit. */
3889 | i.tm.opcode_modifier.opcodeprefix);
43234a1e
L
3890
3891 /* The fourth byte of the EVEX prefix. */
3892 /* The zeroing-masking bit. */
6225c532 3893 if (i.mask.reg && i.mask.zeroing)
43234a1e
L
3894 i.vex.bytes[3] |= 0x80;
3895
3896 /* Don't always set the broadcast bit if there is no RC. */
ca5312a2 3897 if (i.rounding.type == rc_none)
43234a1e
L
3898 {
3899 /* Encode the vector length. */
3900 unsigned int vec_length;
3901
e771e7c9
JB
3902 if (!i.tm.opcode_modifier.evex
3903 || i.tm.opcode_modifier.evex == EVEXDYN)
3904 {
56522fc5 3905 unsigned int op;
e771e7c9 3906
c7213af9
L
3907 /* Determine vector length from the last multi-length vector
3908 operand. */
56522fc5 3909 for (op = i.operands; op--;)
e771e7c9
JB
3910 if (i.tm.operand_types[op].bitfield.xmmword
3911 + i.tm.operand_types[op].bitfield.ymmword
3912 + i.tm.operand_types[op].bitfield.zmmword > 1)
3913 {
3914 if (i.types[op].bitfield.zmmword)
c7213af9
L
3915 {
3916 i.tm.opcode_modifier.evex = EVEX512;
3917 break;
3918 }
e771e7c9 3919 else if (i.types[op].bitfield.ymmword)
c7213af9
L
3920 {
3921 i.tm.opcode_modifier.evex = EVEX256;
3922 break;
3923 }
e771e7c9 3924 else if (i.types[op].bitfield.xmmword)
c7213af9
L
3925 {
3926 i.tm.opcode_modifier.evex = EVEX128;
3927 break;
3928 }
9b345ce8
JB
3929 else if ((i.broadcast.type || i.broadcast.bytes)
3930 && op == i.broadcast.operand)
625cbd7a 3931 {
a5748e0d 3932 switch (get_broadcast_bytes (&i.tm, true))
625cbd7a
JB
3933 {
3934 case 64:
3935 i.tm.opcode_modifier.evex = EVEX512;
3936 break;
3937 case 32:
3938 i.tm.opcode_modifier.evex = EVEX256;
3939 break;
3940 case 16:
3941 i.tm.opcode_modifier.evex = EVEX128;
3942 break;
3943 default:
c7213af9 3944 abort ();
625cbd7a 3945 }
c7213af9 3946 break;
625cbd7a 3947 }
e771e7c9 3948 }
c7213af9 3949
56522fc5 3950 if (op >= MAX_OPERANDS)
c7213af9 3951 abort ();
e771e7c9
JB
3952 }
3953
43234a1e
L
3954 switch (i.tm.opcode_modifier.evex)
3955 {
3956 case EVEXLIG: /* LL' is ignored */
3957 vec_length = evexlig << 5;
3958 break;
3959 case EVEX128:
3960 vec_length = 0 << 5;
3961 break;
3962 case EVEX256:
3963 vec_length = 1 << 5;
3964 break;
3965 case EVEX512:
3966 vec_length = 2 << 5;
3967 break;
d0c2e3ec
JB
3968 case EVEX_L3:
3969 if (dot_insn ())
3970 {
3971 vec_length = 3 << 5;
3972 break;
3973 }
3974 /* Fall through. */
43234a1e
L
3975 default:
3976 abort ();
3977 break;
3978 }
3979 i.vex.bytes[3] |= vec_length;
3980 /* Encode the broadcast bit. */
9b345ce8 3981 if (i.broadcast.type || i.broadcast.bytes)
43234a1e
L
3982 i.vex.bytes[3] |= 0x10;
3983 }
ca5312a2
JB
3984 else if (i.rounding.type != saeonly)
3985 i.vex.bytes[3] |= 0x10 | (i.rounding.type << 5);
43234a1e 3986 else
ca5312a2 3987 i.vex.bytes[3] |= 0x10 | (evexrcig << 5);
43234a1e 3988
6225c532
JB
3989 if (i.mask.reg)
3990 i.vex.bytes[3] |= i.mask.reg->reg_num;
43234a1e
L
3991}
3992
65da13b5
L
3993static void
3994process_immext (void)
3995{
3996 expressionS *exp;
3997
c0f3af97 3998 /* These AMD 3DNow! and SSE2 instructions have an opcode suffix
65da13b5
L
3999 which is coded in the same place as an 8-bit immediate field
4000 would be. Here we fake an 8-bit immediate operand from the
4001 opcode suffix stored in tm.extension_opcode.
4002
c1e679ec 4003 AVX instructions also use this encoding, for some of
c0f3af97 4004 3 argument instructions. */
65da13b5 4005
43234a1e 4006 gas_assert (i.imm_operands <= 1
7ab9ffdd 4007 && (i.operands <= 2
7a8655d2 4008 || (is_any_vex_encoding (&i.tm)
7ab9ffdd 4009 && i.operands <= 4)));
65da13b5
L
4010
4011 exp = &im_expressions[i.imm_operands++];
4012 i.op[i.operands].imms = exp;
be1643ff 4013 i.types[i.operands].bitfield.imm8 = 1;
65da13b5
L
4014 i.operands++;
4015 exp->X_op = O_constant;
4016 exp->X_add_number = i.tm.extension_opcode;
4017 i.tm.extension_opcode = None;
4018}
4019
42164a71
L
4020
4021static int
4022check_hle (void)
4023{
742732c7 4024 switch (i.tm.opcode_modifier.prefixok)
42164a71
L
4025 {
4026 default:
4027 abort ();
742732c7
JB
4028 case PrefixLock:
4029 case PrefixNone:
4030 case PrefixNoTrack:
4031 case PrefixRep:
165de32a 4032 as_bad (_("invalid instruction `%s' after `%s'"),
76d3f746 4033 insn_name (&i.tm), i.hle_prefix);
42164a71 4034 return 0;
742732c7 4035 case PrefixHLELock:
42164a71
L
4036 if (i.prefix[LOCK_PREFIX])
4037 return 1;
165de32a 4038 as_bad (_("missing `lock' with `%s'"), i.hle_prefix);
42164a71 4039 return 0;
742732c7 4040 case PrefixHLEAny:
42164a71 4041 return 1;
742732c7 4042 case PrefixHLERelease:
42164a71
L
4043 if (i.prefix[HLE_PREFIX] != XRELEASE_PREFIX_OPCODE)
4044 {
4045 as_bad (_("instruction `%s' after `xacquire' not allowed"),
76d3f746 4046 insn_name (&i.tm));
42164a71
L
4047 return 0;
4048 }
8dc0818e 4049 if (i.mem_operands == 0 || !(i.flags[i.operands - 1] & Operand_Mem))
42164a71
L
4050 {
4051 as_bad (_("memory destination needed for instruction `%s'"
76d3f746 4052 " after `xrelease'"), insn_name (&i.tm));
42164a71
L
4053 return 0;
4054 }
4055 return 1;
4056 }
4057}
4058
c8480b58
L
4059/* Encode aligned vector move as unaligned vector move. */
4060
4061static void
4062encode_with_unaligned_vector_move (void)
4063{
4064 switch (i.tm.base_opcode)
4065 {
b3a9fe6f
L
4066 case 0x28: /* Load instructions. */
4067 case 0x29: /* Store instructions. */
c8480b58 4068 /* movaps/movapd/vmovaps/vmovapd. */
ddb62495 4069 if (i.tm.opcode_space == SPACE_0F
c8480b58 4070 && i.tm.opcode_modifier.opcodeprefix <= PREFIX_0X66)
b3a9fe6f 4071 i.tm.base_opcode = 0x10 | (i.tm.base_opcode & 1);
c8480b58 4072 break;
b3a9fe6f
L
4073 case 0x6f: /* Load instructions. */
4074 case 0x7f: /* Store instructions. */
c8480b58 4075 /* movdqa/vmovdqa/vmovdqa64/vmovdqa32. */
ddb62495 4076 if (i.tm.opcode_space == SPACE_0F
c8480b58
L
4077 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0X66)
4078 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
4079 break;
4080 default:
4081 break;
4082 }
4083}
4084
b6f8c7c4
L
4085/* Try the shortest encoding by shortening operand size. */
4086
4087static void
4088optimize_encoding (void)
4089{
a0a1771e 4090 unsigned int j;
b6f8c7c4 4091
7fc69528 4092 if (i.tm.mnem_off == MN_lea)
fe134c65
JB
4093 {
4094 /* Optimize: -O:
4095 lea symbol, %rN -> mov $symbol, %rN
4096 lea (%rM), %rN -> mov %rM, %rN
4097 lea (,%rM,1), %rN -> mov %rM, %rN
4098
4099 and in 32-bit mode for 16-bit addressing
4100
4101 lea (%rM), %rN -> movzx %rM, %rN
4102
4103 and in 64-bit mode zap 32-bit addressing in favor of using a
4104 32-bit (or less) destination.
4105 */
4106 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
4107 {
4108 if (!i.op[1].regs->reg_type.bitfield.word)
4109 i.tm.opcode_modifier.size = SIZE32;
4110 i.prefix[ADDR_PREFIX] = 0;
4111 }
4112
4113 if (!i.index_reg && !i.base_reg)
4114 {
4115 /* Handle:
4116 lea symbol, %rN -> mov $symbol, %rN
4117 */
4118 if (flag_code == CODE_64BIT)
4119 {
4120 /* Don't transform a relocation to a 16-bit one. */
4121 if (i.op[0].disps
4122 && i.op[0].disps->X_op != O_constant
4123 && i.op[1].regs->reg_type.bitfield.word)
4124 return;
4125
4126 if (!i.op[1].regs->reg_type.bitfield.qword
4127 || i.tm.opcode_modifier.size == SIZE32)
4128 {
4129 i.tm.base_opcode = 0xb8;
4130 i.tm.opcode_modifier.modrm = 0;
4131 if (!i.op[1].regs->reg_type.bitfield.word)
4132 i.types[0].bitfield.imm32 = 1;
4133 else
4134 {
4135 i.tm.opcode_modifier.size = SIZE16;
4136 i.types[0].bitfield.imm16 = 1;
4137 }
4138 }
4139 else
4140 {
4141 /* Subject to further optimization below. */
4142 i.tm.base_opcode = 0xc7;
4143 i.tm.extension_opcode = 0;
4144 i.types[0].bitfield.imm32s = 1;
4145 i.types[0].bitfield.baseindex = 0;
4146 }
4147 }
4148 /* Outside of 64-bit mode address and operand sizes have to match if
4149 a relocation is involved, as otherwise we wouldn't (currently) or
4150 even couldn't express the relocation correctly. */
4151 else if (i.op[0].disps
4152 && i.op[0].disps->X_op != O_constant
4153 && ((!i.prefix[ADDR_PREFIX])
4154 != (flag_code == CODE_32BIT
4155 ? i.op[1].regs->reg_type.bitfield.dword
4156 : i.op[1].regs->reg_type.bitfield.word)))
4157 return;
7772f168
JB
4158 /* In 16-bit mode converting LEA with 16-bit addressing and a 32-bit
4159 destination is going to grow encoding size. */
4160 else if (flag_code == CODE_16BIT
4161 && (optimize <= 1 || optimize_for_space)
4162 && !i.prefix[ADDR_PREFIX]
4163 && i.op[1].regs->reg_type.bitfield.dword)
4164 return;
fe134c65
JB
4165 else
4166 {
4167 i.tm.base_opcode = 0xb8;
4168 i.tm.opcode_modifier.modrm = 0;
4169 if (i.op[1].regs->reg_type.bitfield.dword)
4170 i.types[0].bitfield.imm32 = 1;
4171 else
4172 i.types[0].bitfield.imm16 = 1;
4173
4174 if (i.op[0].disps
4175 && i.op[0].disps->X_op == O_constant
4176 && i.op[1].regs->reg_type.bitfield.dword
60cfa10c
L
4177 /* NB: Add () to !i.prefix[ADDR_PREFIX] to silence
4178 GCC 5. */
4179 && (!i.prefix[ADDR_PREFIX]) != (flag_code == CODE_32BIT))
fe134c65
JB
4180 i.op[0].disps->X_add_number &= 0xffff;
4181 }
4182
4183 i.tm.operand_types[0] = i.types[0];
4184 i.imm_operands = 1;
4185 if (!i.op[0].imms)
4186 {
4187 i.op[0].imms = &im_expressions[0];
4188 i.op[0].imms->X_op = O_absent;
4189 }
4190 }
4191 else if (i.op[0].disps
4192 && (i.op[0].disps->X_op != O_constant
4193 || i.op[0].disps->X_add_number))
4194 return;
4195 else
4196 {
4197 /* Handle:
4198 lea (%rM), %rN -> mov %rM, %rN
4199 lea (,%rM,1), %rN -> mov %rM, %rN
4200 lea (%rM), %rN -> movzx %rM, %rN
4201 */
4202 const reg_entry *addr_reg;
4203
4204 if (!i.index_reg && i.base_reg->reg_num != RegIP)
4205 addr_reg = i.base_reg;
4206 else if (!i.base_reg
4207 && i.index_reg->reg_num != RegIZ
4208 && !i.log2_scale_factor)
4209 addr_reg = i.index_reg;
4210 else
4211 return;
4212
4213 if (addr_reg->reg_type.bitfield.word
4214 && i.op[1].regs->reg_type.bitfield.dword)
4215 {
4216 if (flag_code != CODE_32BIT)
4217 return;
ddb62495 4218 i.tm.opcode_space = SPACE_0F;
fe134c65
JB
4219 i.tm.base_opcode = 0xb7;
4220 }
4221 else
4222 i.tm.base_opcode = 0x8b;
4223
4224 if (addr_reg->reg_type.bitfield.dword
4225 && i.op[1].regs->reg_type.bitfield.qword)
4226 i.tm.opcode_modifier.size = SIZE32;
4227
4228 i.op[0].regs = addr_reg;
4229 i.reg_operands = 2;
4230 }
4231
4232 i.mem_operands = 0;
4233 i.disp_operands = 0;
4234 i.prefix[ADDR_PREFIX] = 0;
4235 i.prefix[SEG_PREFIX] = 0;
4236 i.seg[0] = NULL;
4237 }
4238
b6f8c7c4 4239 if (optimize_for_space
7fc69528 4240 && i.tm.mnem_off == MN_test
b6f8c7c4
L
4241 && i.reg_operands == 1
4242 && i.imm_operands == 1
4243 && !i.types[1].bitfield.byte
4244 && i.op[0].imms->X_op == O_constant
7fc69528 4245 && fits_in_imm7 (i.op[0].imms->X_add_number))
b6f8c7c4
L
4246 {
4247 /* Optimize: -Os:
4248 test $imm7, %r64/%r32/%r16 -> test $imm7, %r8
4249 */
4250 unsigned int base_regnum = i.op[1].regs->reg_num;
4251 if (flag_code == CODE_64BIT || base_regnum < 4)
4252 {
4253 i.types[1].bitfield.byte = 1;
4254 /* Ignore the suffix. */
4255 i.suffix = 0;
7697afb6
JB
4256 /* Convert to byte registers. */
4257 if (i.types[1].bitfield.word)
4258 j = 16;
4259 else if (i.types[1].bitfield.dword)
4260 j = 32;
4261 else
4262 j = 48;
4263 if (!(i.op[1].regs->reg_flags & RegRex) && base_regnum < 4)
4264 j += 8;
4265 i.op[1].regs -= j;
b6f8c7c4
L
4266 }
4267 }
4268 else if (flag_code == CODE_64BIT
ddb62495 4269 && i.tm.opcode_space == SPACE_BASE
d3d50934
L
4270 && ((i.types[1].bitfield.qword
4271 && i.reg_operands == 1
b6f8c7c4
L
4272 && i.imm_operands == 1
4273 && i.op[0].imms->X_op == O_constant
507916b8 4274 && ((i.tm.base_opcode == 0xb8
b6f8c7c4
L
4275 && i.tm.extension_opcode == None
4276 && fits_in_unsigned_long (i.op[0].imms->X_add_number))
4277 || (fits_in_imm31 (i.op[0].imms->X_add_number)
7fc69528 4278 && (i.tm.base_opcode == 0x24
b6f8c7c4
L
4279 || (i.tm.base_opcode == 0x80
4280 && i.tm.extension_opcode == 0x4)
7fc69528
JB
4281 || i.tm.mnem_off == MN_test
4282 || ((i.tm.base_opcode | 1) == 0xc7
b8364fa7
JB
4283 && i.tm.extension_opcode == 0x0)))
4284 || (fits_in_imm7 (i.op[0].imms->X_add_number)
4285 && i.tm.base_opcode == 0x83
4286 && i.tm.extension_opcode == 0x4)))
d3d50934
L
4287 || (i.types[0].bitfield.qword
4288 && ((i.reg_operands == 2
4289 && i.op[0].regs == i.op[1].regs
7fc69528
JB
4290 && (i.tm.mnem_off == MN_xor
4291 || i.tm.mnem_off == MN_sub))
4292 || i.tm.mnem_off == MN_clr))))
b6f8c7c4
L
4293 {
4294 /* Optimize: -O:
4295 andq $imm31, %r64 -> andl $imm31, %r32
b8364fa7 4296 andq $imm7, %r64 -> andl $imm7, %r32
b6f8c7c4
L
4297 testq $imm31, %r64 -> testl $imm31, %r32
4298 xorq %r64, %r64 -> xorl %r32, %r32
4299 subq %r64, %r64 -> subl %r32, %r32
4300 movq $imm31, %r64 -> movl $imm31, %r32
4301 movq $imm32, %r64 -> movl $imm32, %r32
4302 */
04784e33
JB
4303 i.tm.opcode_modifier.size = SIZE32;
4304 if (i.imm_operands)
4305 {
4306 i.types[0].bitfield.imm32 = 1;
4307 i.types[0].bitfield.imm32s = 0;
4308 i.types[0].bitfield.imm64 = 0;
4309 }
4310 else
4311 {
4312 i.types[0].bitfield.dword = 1;
4313 i.types[0].bitfield.qword = 0;
4314 }
4315 i.types[1].bitfield.dword = 1;
4316 i.types[1].bitfield.qword = 0;
7fc69528 4317 if (i.tm.mnem_off == MN_mov || i.tm.mnem_off == MN_lea)
b6f8c7c4
L
4318 {
4319 /* Handle
4320 movq $imm31, %r64 -> movl $imm31, %r32
4321 movq $imm32, %r64 -> movl $imm32, %r32
4322 */
4323 i.tm.operand_types[0].bitfield.imm32 = 1;
4324 i.tm.operand_types[0].bitfield.imm32s = 0;
4325 i.tm.operand_types[0].bitfield.imm64 = 0;
507916b8 4326 if ((i.tm.base_opcode | 1) == 0xc7)
b6f8c7c4
L
4327 {
4328 /* Handle
4329 movq $imm31, %r64 -> movl $imm31, %r32
4330 */
507916b8 4331 i.tm.base_opcode = 0xb8;
b6f8c7c4 4332 i.tm.extension_opcode = None;
507916b8 4333 i.tm.opcode_modifier.w = 0;
b6f8c7c4
L
4334 i.tm.opcode_modifier.modrm = 0;
4335 }
4336 }
4337 }
5641ec01
JB
4338 else if (optimize > 1
4339 && !optimize_for_space
4340 && i.reg_operands == 2
4341 && i.op[0].regs == i.op[1].regs
7fc69528 4342 && (i.tm.mnem_off == MN_and || i.tm.mnem_off == MN_or)
5641ec01
JB
4343 && (flag_code != CODE_64BIT || !i.types[0].bitfield.dword))
4344 {
4345 /* Optimize: -O2:
4346 andb %rN, %rN -> testb %rN, %rN
4347 andw %rN, %rN -> testw %rN, %rN
4348 andq %rN, %rN -> testq %rN, %rN
4349 orb %rN, %rN -> testb %rN, %rN
4350 orw %rN, %rN -> testw %rN, %rN
4351 orq %rN, %rN -> testq %rN, %rN
4352
4353 and outside of 64-bit mode
4354
4355 andl %rN, %rN -> testl %rN, %rN
4356 orl %rN, %rN -> testl %rN, %rN
4357 */
4358 i.tm.base_opcode = 0x84 | (i.tm.base_opcode & 1);
4359 }
ad2f4436
JB
4360 else if (i.tm.base_opcode == 0xba
4361 && i.tm.opcode_space == SPACE_0F
4362 && i.reg_operands == 1
4363 && i.op[0].imms->X_op == O_constant
4364 && i.op[0].imms->X_add_number >= 0)
4365 {
4366 /* Optimize: -O:
4367 btw $n, %rN -> btl $n, %rN (outside of 16-bit mode, n < 16)
4368 btq $n, %rN -> btl $n, %rN (in 64-bit mode, n < 32, N < 8)
4369 btl $n, %rN -> btw $n, %rN (in 16-bit mode, n < 16)
4370
4371 With <BT> one of bts, btr, and bts also:
4372 <BT>w $n, %rN -> btl $n, %rN (in 32-bit mode, n < 16)
4373 <BT>l $n, %rN -> btw $n, %rN (in 16-bit mode, n < 16)
4374 */
4375 switch (flag_code)
4376 {
4377 case CODE_64BIT:
4378 if (i.tm.extension_opcode != 4)
4379 break;
4380 if (i.types[1].bitfield.qword
4381 && i.op[0].imms->X_add_number < 32
4382 && !(i.op[1].regs->reg_flags & RegRex))
4383 i.tm.opcode_modifier.size = SIZE32;
4384 /* Fall through. */
4385 case CODE_32BIT:
4386 if (i.types[1].bitfield.word
4387 && i.op[0].imms->X_add_number < 16)
4388 i.tm.opcode_modifier.size = SIZE32;
4389 break;
4390 case CODE_16BIT:
4391 if (i.op[0].imms->X_add_number < 16)
4392 i.tm.opcode_modifier.size = SIZE16;
4393 break;
4394 }
4395 }
99112332 4396 else if (i.reg_operands == 3
b6f8c7c4
L
4397 && i.op[0].regs == i.op[1].regs
4398 && !i.types[2].bitfield.xmmword
4399 && (i.tm.opcode_modifier.vex
6225c532 4400 || ((!i.mask.reg || i.mask.zeroing)
e771e7c9 4401 && is_evex_encoding (&i.tm)
80c34c38 4402 && (i.vec_encoding != vex_encoding_evex
dd22218c 4403 || cpu_arch_isa_flags.bitfield.cpuavx512vl
80c34c38 4404 || i.tm.cpu_flags.bitfield.cpuavx512vl
7091c612 4405 || (i.tm.operand_types[2].bitfield.zmmword
dd22218c 4406 && i.types[2].bitfield.ymmword))))
ddb62495 4407 && i.tm.opcode_space == SPACE_0F
5844ccaa
JB
4408 && ((i.tm.base_opcode | 2) == 0x57
4409 || i.tm.base_opcode == 0xdf
4410 || i.tm.base_opcode == 0xef
4411 || (i.tm.base_opcode | 3) == 0xfb
4412 || i.tm.base_opcode == 0x42
4413 || i.tm.base_opcode == 0x47))
b6f8c7c4 4414 {
99112332 4415 /* Optimize: -O1:
8305403a
L
4416 VOP, one of vandnps, vandnpd, vxorps, vxorpd, vpsubb, vpsubd,
4417 vpsubq and vpsubw:
b6f8c7c4
L
4418 EVEX VOP %zmmM, %zmmM, %zmmN
4419 -> VEX VOP %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 VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4423 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4424 VEX VOP %ymmM, %ymmM, %ymmN
4425 -> VEX VOP %xmmM, %xmmM, %xmmN
4426 VOP, one of vpandn and vpxor:
4427 VEX VOP %ymmM, %ymmM, %ymmN
4428 -> VEX VOP %xmmM, %xmmM, %xmmN
4429 VOP, one of vpandnd and vpandnq:
4430 EVEX VOP %zmmM, %zmmM, %zmmN
4431 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4432 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4433 EVEX VOP %ymmM, %ymmM, %ymmN
4434 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4435 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4436 VOP, one of vpxord and vpxorq:
4437 EVEX VOP %zmmM, %zmmM, %zmmN
4438 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4439 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4440 EVEX VOP %ymmM, %ymmM, %ymmN
4441 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4442 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
1424ad86
JB
4443 VOP, one of kxord and kxorq:
4444 VEX VOP %kM, %kM, %kN
4445 -> VEX kxorw %kM, %kM, %kN
4446 VOP, one of kandnd and kandnq:
4447 VEX VOP %kM, %kM, %kN
4448 -> VEX kandnw %kM, %kM, %kN
b6f8c7c4 4449 */
e771e7c9 4450 if (is_evex_encoding (&i.tm))
b6f8c7c4 4451 {
7b1d7ca1 4452 if (i.vec_encoding != vex_encoding_evex)
b6f8c7c4
L
4453 {
4454 i.tm.opcode_modifier.vex = VEX128;
4455 i.tm.opcode_modifier.vexw = VEXW0;
4456 i.tm.opcode_modifier.evex = 0;
4457 }
7b1d7ca1 4458 else if (optimize > 1)
dd22218c
L
4459 i.tm.opcode_modifier.evex = EVEX128;
4460 else
4461 return;
b6f8c7c4 4462 }
f74a6307 4463 else if (i.tm.operand_types[0].bitfield.class == RegMask)
1424ad86 4464 {
35648716 4465 i.tm.opcode_modifier.opcodeprefix = PREFIX_NONE;
1424ad86
JB
4466 i.tm.opcode_modifier.vexw = VEXW0;
4467 }
b6f8c7c4
L
4468 else
4469 i.tm.opcode_modifier.vex = VEX128;
4470
4471 if (i.tm.opcode_modifier.vex)
4472 for (j = 0; j < 3; j++)
4473 {
4474 i.types[j].bitfield.xmmword = 1;
4475 i.types[j].bitfield.ymmword = 0;
4476 }
4477 }
392a5972 4478 else if (i.vec_encoding != vex_encoding_evex
97ed31ae 4479 && !i.types[0].bitfield.zmmword
392a5972 4480 && !i.types[1].bitfield.zmmword
6225c532 4481 && !i.mask.reg
9b345ce8 4482 && !i.broadcast.type
a5748e0d 4483 && !i.broadcast.bytes
97ed31ae 4484 && is_evex_encoding (&i.tm)
35648716
JB
4485 && ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4486 || (i.tm.base_opcode & ~4) == 0xdb
4487 || (i.tm.base_opcode & ~4) == 0xeb)
97ed31ae
L
4488 && i.tm.extension_opcode == None)
4489 {
4490 /* Optimize: -O1:
4491 VOP, one of vmovdqa32, vmovdqa64, vmovdqu8, vmovdqu16,
4492 vmovdqu32 and vmovdqu64:
4493 EVEX VOP %xmmM, %xmmN
4494 -> VEX vmovdqa|vmovdqu %xmmM, %xmmN (M and N < 16)
4495 EVEX VOP %ymmM, %ymmN
4496 -> VEX vmovdqa|vmovdqu %ymmM, %ymmN (M and N < 16)
4497 EVEX VOP %xmmM, mem
4498 -> VEX vmovdqa|vmovdqu %xmmM, mem (M < 16)
4499 EVEX VOP %ymmM, mem
4500 -> VEX vmovdqa|vmovdqu %ymmM, mem (M < 16)
4501 EVEX VOP mem, %xmmN
4502 -> VEX mvmovdqa|vmovdquem, %xmmN (N < 16)
4503 EVEX VOP mem, %ymmN
4504 -> VEX vmovdqa|vmovdqu mem, %ymmN (N < 16)
a0a1771e
JB
4505 VOP, one of vpand, vpandn, vpor, vpxor:
4506 EVEX VOP{d,q} %xmmL, %xmmM, %xmmN
4507 -> VEX VOP %xmmL, %xmmM, %xmmN (L, M, and N < 16)
4508 EVEX VOP{d,q} %ymmL, %ymmM, %ymmN
4509 -> VEX VOP %ymmL, %ymmM, %ymmN (L, M, and N < 16)
4510 EVEX VOP{d,q} mem, %xmmM, %xmmN
4511 -> VEX VOP mem, %xmmM, %xmmN (M and N < 16)
4512 EVEX VOP{d,q} mem, %ymmM, %ymmN
4513 -> VEX VOP mem, %ymmM, %ymmN (M and N < 16)
97ed31ae 4514 */
a0a1771e 4515 for (j = 0; j < i.operands; j++)
392a5972
L
4516 if (operand_type_check (i.types[j], disp)
4517 && i.op[j].disps->X_op == O_constant)
4518 {
4519 /* Since the VEX prefix has 2 or 3 bytes, the EVEX prefix
4520 has 4 bytes, EVEX Disp8 has 1 byte and VEX Disp32 has 4
4521 bytes, we choose EVEX Disp8 over VEX Disp32. */
4522 int evex_disp8, vex_disp8;
4523 unsigned int memshift = i.memshift;
4524 offsetT n = i.op[j].disps->X_add_number;
4525
4526 evex_disp8 = fits_in_disp8 (n);
4527 i.memshift = 0;
4528 vex_disp8 = fits_in_disp8 (n);
4529 if (evex_disp8 != vex_disp8)
4530 {
4531 i.memshift = memshift;
4532 return;
4533 }
4534
4535 i.types[j].bitfield.disp8 = vex_disp8;
4536 break;
4537 }
35648716
JB
4538 if ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4539 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2)
4540 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
97ed31ae
L
4541 i.tm.opcode_modifier.vex
4542 = i.types[0].bitfield.ymmword ? VEX256 : VEX128;
4543 i.tm.opcode_modifier.vexw = VEXW0;
79dec6b7 4544 /* VPAND, VPOR, and VPXOR are commutative. */
35648716 4545 if (i.reg_operands == 3 && i.tm.base_opcode != 0xdf)
79dec6b7 4546 i.tm.opcode_modifier.commutative = 1;
97ed31ae
L
4547 i.tm.opcode_modifier.evex = 0;
4548 i.tm.opcode_modifier.masking = 0;
a0a1771e 4549 i.tm.opcode_modifier.broadcast = 0;
97ed31ae
L
4550 i.tm.opcode_modifier.disp8memshift = 0;
4551 i.memshift = 0;
a0a1771e
JB
4552 if (j < i.operands)
4553 i.types[j].bitfield.disp8
4554 = fits_in_disp8 (i.op[j].disps->X_add_number);
97ed31ae 4555 }
b6f8c7c4
L
4556}
4557
ae531041
L
4558/* Return non-zero for load instruction. */
4559
4560static int
4561load_insn_p (void)
4562{
4563 unsigned int dest;
4564 int any_vex_p = is_any_vex_encoding (&i.tm);
4565 unsigned int base_opcode = i.tm.base_opcode | 1;
4566
4567 if (!any_vex_p)
4568 {
ef07be45
CL
4569 /* Anysize insns: lea, invlpg, clflush, prefetch*, bndmk, bndcl, bndcu,
4570 bndcn, bndstx, bndldx, clflushopt, clwb, cldemote. */
255571cd 4571 if (i.tm.opcode_modifier.operandconstraint == ANY_SIZE)
ae531041
L
4572 return 0;
4573
389d00a5 4574 /* pop. */
6d86a545 4575 if (i.tm.mnem_off == MN_pop)
389d00a5
JB
4576 return 1;
4577 }
4578
ddb62495 4579 if (i.tm.opcode_space == SPACE_BASE)
389d00a5
JB
4580 {
4581 /* popf, popa. */
4582 if (i.tm.base_opcode == 0x9d
a09f656b 4583 || i.tm.base_opcode == 0x61)
ae531041
L
4584 return 1;
4585
4586 /* movs, cmps, lods, scas. */
4587 if ((i.tm.base_opcode | 0xb) == 0xaf)
4588 return 1;
4589
a09f656b 4590 /* outs, xlatb. */
4591 if (base_opcode == 0x6f
4592 || i.tm.base_opcode == 0xd7)
ae531041 4593 return 1;
a09f656b 4594 /* NB: For AMD-specific insns with implicit memory operands,
4595 they're intentionally not covered. */
ae531041
L
4596 }
4597
4598 /* No memory operand. */
4599 if (!i.mem_operands)
4600 return 0;
4601
4602 if (any_vex_p)
4603 {
7fc69528 4604 if (i.tm.mnem_off == MN_vldmxcsr)
ae531041
L
4605 return 1;
4606 }
ddb62495 4607 else if (i.tm.opcode_space == SPACE_BASE)
ae531041
L
4608 {
4609 /* test, not, neg, mul, imul, div, idiv. */
aa4c197d 4610 if (base_opcode == 0xf7 && i.tm.extension_opcode != 1)
ae531041
L
4611 return 1;
4612
4613 /* inc, dec. */
4614 if (base_opcode == 0xff && i.tm.extension_opcode <= 1)
4615 return 1;
4616
4617 /* add, or, adc, sbb, and, sub, xor, cmp. */
4618 if (i.tm.base_opcode >= 0x80 && i.tm.base_opcode <= 0x83)
4619 return 1;
4620
ae531041 4621 /* rol, ror, rcl, rcr, shl/sal, shr, sar. */
aa4c197d 4622 if ((base_opcode == 0xc1 || (base_opcode | 2) == 0xd3)
ae531041
L
4623 && i.tm.extension_opcode != 6)
4624 return 1;
4625
ae531041 4626 /* Check for x87 instructions. */
aa4c197d 4627 if ((base_opcode | 6) == 0xdf)
ae531041
L
4628 {
4629 /* Skip fst, fstp, fstenv, fstcw. */
4630 if (i.tm.base_opcode == 0xd9
4631 && (i.tm.extension_opcode == 2
4632 || i.tm.extension_opcode == 3
4633 || i.tm.extension_opcode == 6
4634 || i.tm.extension_opcode == 7))
4635 return 0;
4636
4637 /* Skip fisttp, fist, fistp, fstp. */
4638 if (i.tm.base_opcode == 0xdb
4639 && (i.tm.extension_opcode == 1
4640 || i.tm.extension_opcode == 2
4641 || i.tm.extension_opcode == 3
4642 || i.tm.extension_opcode == 7))
4643 return 0;
4644
4645 /* Skip fisttp, fst, fstp, fsave, fstsw. */
4646 if (i.tm.base_opcode == 0xdd
4647 && (i.tm.extension_opcode == 1
4648 || i.tm.extension_opcode == 2
4649 || i.tm.extension_opcode == 3
4650 || i.tm.extension_opcode == 6
4651 || i.tm.extension_opcode == 7))
4652 return 0;
4653
4654 /* Skip fisttp, fist, fistp, fbstp, fistp. */
4655 if (i.tm.base_opcode == 0xdf
4656 && (i.tm.extension_opcode == 1
4657 || i.tm.extension_opcode == 2
4658 || i.tm.extension_opcode == 3
4659 || i.tm.extension_opcode == 6
4660 || i.tm.extension_opcode == 7))
4661 return 0;
4662
4663 return 1;
4664 }
4665 }
ddb62495 4666 else if (i.tm.opcode_space == SPACE_0F)
389d00a5
JB
4667 {
4668 /* bt, bts, btr, btc. */
4669 if (i.tm.base_opcode == 0xba
aa4c197d 4670 && (i.tm.extension_opcode | 3) == 7)
389d00a5
JB
4671 return 1;
4672
4673 /* cmpxchg8b, cmpxchg16b, xrstors, vmptrld. */
4674 if (i.tm.base_opcode == 0xc7
4675 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
4676 && (i.tm.extension_opcode == 1 || i.tm.extension_opcode == 3
4677 || i.tm.extension_opcode == 6))
4678 return 1;
4679
4680 /* fxrstor, ldmxcsr, xrstor. */
4681 if (i.tm.base_opcode == 0xae
4682 && (i.tm.extension_opcode == 1
4683 || i.tm.extension_opcode == 2
4684 || i.tm.extension_opcode == 5))
4685 return 1;
4686
4687 /* lgdt, lidt, lmsw. */
4688 if (i.tm.base_opcode == 0x01
4689 && (i.tm.extension_opcode == 2
4690 || i.tm.extension_opcode == 3
4691 || i.tm.extension_opcode == 6))
4692 return 1;
4693 }
ae531041
L
4694
4695 dest = i.operands - 1;
4696
4697 /* Check fake imm8 operand and 3 source operands. */
4698 if ((i.tm.opcode_modifier.immext
aa180741 4699 || i.reg_operands + i.mem_operands == 4)
ae531041
L
4700 && i.types[dest].bitfield.imm8)
4701 dest--;
4702
389d00a5 4703 /* add, or, adc, sbb, and, sub, xor, cmp, test, xchg. */
ddb62495 4704 if (i.tm.opcode_space == SPACE_BASE
aa4c197d 4705 && ((base_opcode | 0x38) == 0x39
389d00a5
JB
4706 || (base_opcode | 2) == 0x87))
4707 return 1;
4708
7fc69528 4709 if (i.tm.mnem_off == MN_xadd)
ae531041
L
4710 return 1;
4711
4712 /* Check for load instruction. */
4713 return (i.types[dest].bitfield.class != ClassNone
4714 || i.types[dest].bitfield.instance == Accum);
4715}
4716
4717/* Output lfence, 0xfaee8, after instruction. */
4718
4719static void
4720insert_lfence_after (void)
4721{
4722 if (lfence_after_load && load_insn_p ())
4723 {
a09f656b 4724 /* There are also two REP string instructions that require
4725 special treatment. Specifically, the compare string (CMPS)
4726 and scan string (SCAS) instructions set EFLAGS in a manner
4727 that depends on the data being compared/scanned. When used
4728 with a REP prefix, the number of iterations may therefore
4729 vary depending on this data. If the data is a program secret
4730 chosen by the adversary using an LVI method,
4731 then this data-dependent behavior may leak some aspect
4732 of the secret. */
aa4c197d 4733 if (((i.tm.base_opcode | 0x9) == 0xaf)
a09f656b 4734 && i.prefix[REP_PREFIX])
4735 {
4736 as_warn (_("`%s` changes flags which would affect control flow behavior"),
76d3f746 4737 insn_name (&i.tm));
a09f656b 4738 }
ae531041
L
4739 char *p = frag_more (3);
4740 *p++ = 0xf;
4741 *p++ = 0xae;
4742 *p = 0xe8;
4743 }
4744}
4745
4746/* Output lfence, 0xfaee8, before instruction. */
4747
4748static void
4749insert_lfence_before (void)
4750{
4751 char *p;
4752
ddb62495 4753 if (i.tm.opcode_space != SPACE_BASE)
ae531041
L
4754 return;
4755
4756 if (i.tm.base_opcode == 0xff
4757 && (i.tm.extension_opcode == 2 || i.tm.extension_opcode == 4))
4758 {
4759 /* Insert lfence before indirect branch if needed. */
4760
4761 if (lfence_before_indirect_branch == lfence_branch_none)
4762 return;
4763
4764 if (i.operands != 1)
4765 abort ();
4766
4767 if (i.reg_operands == 1)
4768 {
4769 /* Indirect branch via register. Don't insert lfence with
4770 -mlfence-after-load=yes. */
4771 if (lfence_after_load
4772 || lfence_before_indirect_branch == lfence_branch_memory)
4773 return;
4774 }
4775 else if (i.mem_operands == 1
4776 && lfence_before_indirect_branch != lfence_branch_register)
4777 {
4778 as_warn (_("indirect `%s` with memory operand should be avoided"),
76d3f746 4779 insn_name (&i.tm));
ae531041
L
4780 return;
4781 }
4782 else
4783 return;
4784
4785 if (last_insn.kind != last_insn_other
4786 && last_insn.seg == now_seg)
4787 {
4788 as_warn_where (last_insn.file, last_insn.line,
4789 _("`%s` skips -mlfence-before-indirect-branch on `%s`"),
76d3f746 4790 last_insn.name, insn_name (&i.tm));
ae531041
L
4791 return;
4792 }
4793
4794 p = frag_more (3);
4795 *p++ = 0xf;
4796 *p++ = 0xae;
4797 *p = 0xe8;
4798 return;
4799 }
4800
503648e4 4801 /* Output or/not/shl and lfence before near ret. */
ae531041 4802 if (lfence_before_ret != lfence_before_ret_none
aa4c197d 4803 && (i.tm.base_opcode | 1) == 0xc3)
ae531041
L
4804 {
4805 if (last_insn.kind != last_insn_other
4806 && last_insn.seg == now_seg)
4807 {
4808 as_warn_where (last_insn.file, last_insn.line,
4809 _("`%s` skips -mlfence-before-ret on `%s`"),
76d3f746 4810 last_insn.name, insn_name (&i.tm));
ae531041
L
4811 return;
4812 }
a09f656b 4813
a09f656b 4814 /* Near ret ingore operand size override under CPU64. */
503648e4 4815 char prefix = flag_code == CODE_64BIT
4816 ? 0x48
4817 : i.prefix[DATA_PREFIX] ? 0x66 : 0x0;
a09f656b 4818
4819 if (lfence_before_ret == lfence_before_ret_not)
4820 {
4821 /* not: 0xf71424, may add prefix
4822 for operand size override or 64-bit code. */
4823 p = frag_more ((prefix ? 2 : 0) + 6 + 3);
4824 if (prefix)
4825 *p++ = prefix;
ae531041
L
4826 *p++ = 0xf7;
4827 *p++ = 0x14;
4828 *p++ = 0x24;
a09f656b 4829 if (prefix)
4830 *p++ = prefix;
ae531041
L
4831 *p++ = 0xf7;
4832 *p++ = 0x14;
4833 *p++ = 0x24;
4834 }
a09f656b 4835 else
4836 {
4837 p = frag_more ((prefix ? 1 : 0) + 4 + 3);
4838 if (prefix)
4839 *p++ = prefix;
4840 if (lfence_before_ret == lfence_before_ret_or)
4841 {
4842 /* or: 0x830c2400, may add prefix
4843 for operand size override or 64-bit code. */
4844 *p++ = 0x83;
4845 *p++ = 0x0c;
4846 }
4847 else
4848 {
4849 /* shl: 0xc1242400, may add prefix
4850 for operand size override or 64-bit code. */
4851 *p++ = 0xc1;
4852 *p++ = 0x24;
4853 }
4854
4855 *p++ = 0x24;
4856 *p++ = 0x0;
4857 }
4858
ae531041
L
4859 *p++ = 0xf;
4860 *p++ = 0xae;
4861 *p = 0xe8;
4862 }
4863}
4864
edd67638
JB
4865/* Shared helper for md_assemble() and s_insn(). */
4866static void init_globals (void)
4867{
4868 unsigned int j;
4869
4870 memset (&i, '\0', sizeof (i));
4871 i.rounding.type = rc_none;
4872 for (j = 0; j < MAX_OPERANDS; j++)
4873 i.reloc[j] = NO_RELOC;
4874 memset (disp_expressions, '\0', sizeof (disp_expressions));
4875 memset (im_expressions, '\0', sizeof (im_expressions));
4876 save_stack_p = save_stack;
4877}
4878
04784e33
JB
4879/* Helper for md_assemble() to decide whether to prepare for a possible 2nd
4880 parsing pass. Instead of introducing a rarely use new insn attribute this
4881 utilizes a common pattern between affected templates. It is deemed
4882 acceptable that this will lead to unnecessary pass 2 preparations in a
4883 limited set of cases. */
4884static INLINE bool may_need_pass2 (const insn_template *t)
4885{
4886 return t->opcode_modifier.sse2avx
4887 /* Note that all SSE2AVX templates have at least one operand. */
a28fedbc 4888 ? t->operand_types[t->operands - 1].bitfield.class == RegSIMD
ddb62495 4889 : (t->opcode_space == SPACE_0F
a28fedbc 4890 && (t->base_opcode | 1) == 0xbf)
ddb62495 4891 || (t->opcode_space == SPACE_BASE
a28fedbc 4892 && t->base_opcode == 0x63);
04784e33
JB
4893}
4894
252b5132
RH
4895/* This is the guts of the machine-dependent assembler. LINE points to a
4896 machine dependent instruction. This function is supposed to emit
4897 the frags/bytes it assembles to. */
4898
4899void
65da13b5 4900md_assemble (char *line)
252b5132 4901{
40fb9820 4902 unsigned int j;
9db83a32 4903 char mnemonic[MAX_MNEM_SIZE], mnem_suffix = 0, *copy = NULL;
04784e33
JB
4904 const char *end, *pass1_mnem = NULL;
4905 enum i386_error pass1_err = 0;
d3ce72d0 4906 const insn_template *t;
252b5132 4907
47926f60 4908 /* Initialize globals. */
04784e33
JB
4909 current_templates = NULL;
4910 retry:
edd67638 4911 init_globals ();
252b5132
RH
4912
4913 /* First parse an instruction mnemonic & call i386_operand for the operands.
4914 We assume that the scrubber has arranged it so that line[0] is the valid
47926f60 4915 start of a (possibly prefixed) mnemonic. */
252b5132 4916
edd67638 4917 end = parse_insn (line, mnemonic, false);
5317ad2c 4918 if (end == NULL)
04784e33
JB
4919 {
4920 if (pass1_mnem != NULL)
4921 goto match_error;
9db83a32
JB
4922 if (i.error != no_error)
4923 {
4924 gas_assert (current_templates != NULL);
4925 if (may_need_pass2 (current_templates->start) && !i.suffix)
4926 goto no_match;
4927 /* No point in trying a 2nd pass - it'll only find the same suffix
4928 again. */
4929 mnem_suffix = i.suffix;
4930 goto match_error;
4931 }
04784e33
JB
4932 return;
4933 }
6d86a545
JB
4934 t = current_templates->start;
4935 if (may_need_pass2 (t))
04784e33
JB
4936 {
4937 /* Make a copy of the full line in case we need to retry. */
4938 copy = xstrdup (line);
4939 }
5317ad2c 4940 line += end - line;
83b16ac6 4941 mnem_suffix = i.suffix;
252b5132 4942
29b0f896 4943 line = parse_operands (line, mnemonic);
ee86248c 4944 this_operand = -1;
29b0f896 4945 if (line == NULL)
04784e33
JB
4946 {
4947 free (copy);
4948 return;
4949 }
252b5132 4950
29b0f896
AM
4951 /* Now we've parsed the mnemonic into a set of templates, and have the
4952 operands at hand. */
4953
b630c145 4954 /* All Intel opcodes have reversed operands except for "bound", "enter",
c0e54661 4955 "invlpg*", "monitor*", "mwait*", "tpause", "umwait", "pvalidate",
b0e8fa7f
TJ
4956 "rmpadjust", "rmpupdate", and "rmpquery". We also don't reverse
4957 intersegment "jmp" and "call" instructions with 2 immediate operands so
4958 that the immediate segment precedes the offset consistently in Intel and
4959 AT&T modes. */
4d456e3d
L
4960 if (intel_syntax
4961 && i.operands > 1
6d86a545
JB
4962 && (t->mnem_off != MN_bound)
4963 && !startswith (mnemonic, "invlpg")
d34049e8
ML
4964 && !startswith (mnemonic, "monitor")
4965 && !startswith (mnemonic, "mwait")
6d86a545 4966 && (t->mnem_off != MN_pvalidate)
d34049e8 4967 && !startswith (mnemonic, "rmp")
6d86a545
JB
4968 && (t->mnem_off != MN_tpause)
4969 && (t->mnem_off != MN_umwait)
47c0279b
JB
4970 && !(i.operands == 2
4971 && operand_type_check (i.types[0], imm)
40fb9820 4972 && operand_type_check (i.types[1], imm)))
29b0f896
AM
4973 swap_operands ();
4974
ec56d5c0
JB
4975 /* The order of the immediates should be reversed
4976 for 2 immediates extrq and insertq instructions */
4977 if (i.imm_operands == 2
6d86a545 4978 && (t->mnem_off == MN_extrq || t->mnem_off == MN_insertq))
ec56d5c0
JB
4979 swap_2_operands (0, 1);
4980
29b0f896
AM
4981 if (i.imm_operands)
4982 optimize_imm ();
4983
0de704b9
JB
4984 if (i.disp_operands && !optimize_disp (t))
4985 return;
29b0f896
AM
4986
4987 /* Next, we find a template that matches the given insn,
4988 making sure the overlap of the given operands types is consistent
4989 with the template operand types. */
252b5132 4990
83b16ac6 4991 if (!(t = match_template (mnem_suffix)))
04784e33
JB
4992 {
4993 const char *err_msg;
4994
4995 if (copy && !mnem_suffix)
4996 {
4997 line = copy;
4998 copy = NULL;
9db83a32 4999 no_match:
04784e33 5000 pass1_err = i.error;
76d3f746 5001 pass1_mnem = insn_name (current_templates->start);
04784e33
JB
5002 goto retry;
5003 }
9db83a32
JB
5004
5005 /* If a non-/only-64bit template (group) was found in pass 1, and if
5006 _some_ template (group) was found in pass 2, squash pass 1's
5007 error. */
5008 if (pass1_err == unsupported_64bit)
5009 pass1_mnem = NULL;
5010
04784e33 5011 match_error:
9db83a32
JB
5012 free (copy);
5013
04784e33
JB
5014 switch (pass1_mnem ? pass1_err : i.error)
5015 {
5016 default:
5017 abort ();
5018 case operand_size_mismatch:
5019 err_msg = _("operand size mismatch");
5020 break;
5021 case operand_type_mismatch:
5022 err_msg = _("operand type mismatch");
5023 break;
5024 case register_type_mismatch:
5025 err_msg = _("register type mismatch");
5026 break;
5027 case number_of_operands_mismatch:
5028 err_msg = _("number of operands mismatch");
5029 break;
5030 case invalid_instruction_suffix:
5031 err_msg = _("invalid instruction suffix");
5032 break;
5033 case bad_imm4:
5034 err_msg = _("constant doesn't fit in 4 bits");
5035 break;
5036 case unsupported_with_intel_mnemonic:
5037 err_msg = _("unsupported with Intel mnemonic");
5038 break;
5039 case unsupported_syntax:
5040 err_msg = _("unsupported syntax");
5041 break;
5042 case unsupported:
5043 as_bad (_("unsupported instruction `%s'"),
76d3f746 5044 pass1_mnem ? pass1_mnem : insn_name (current_templates->start));
04784e33 5045 return;
9db83a32
JB
5046 case unsupported_on_arch:
5047 as_bad (_("`%s' is not supported on `%s%s'"),
76d3f746 5048 pass1_mnem ? pass1_mnem : insn_name (current_templates->start),
9db83a32
JB
5049 cpu_arch_name ? cpu_arch_name : default_arch,
5050 cpu_sub_arch_name ? cpu_sub_arch_name : "");
5051 return;
5052 case unsupported_64bit:
5053 if (ISLOWER (mnem_suffix))
e8b4b7b2
NC
5054 {
5055 if (flag_code == CODE_64BIT)
5056 as_bad (_("`%s%c' is not supported in 64-bit mode"),
76d3f746 5057 pass1_mnem ? pass1_mnem : insn_name (current_templates->start),
e8b4b7b2
NC
5058 mnem_suffix);
5059 else
5060 as_bad (_("`%s%c' is only supported in 64-bit mode"),
76d3f746 5061 pass1_mnem ? pass1_mnem : insn_name (current_templates->start),
e8b4b7b2
NC
5062 mnem_suffix);
5063 }
9db83a32 5064 else
e8b4b7b2
NC
5065 {
5066 if (flag_code == CODE_64BIT)
5067 as_bad (_("`%s' is not supported in 64-bit mode"),
76d3f746 5068 pass1_mnem ? pass1_mnem : insn_name (current_templates->start));
e8b4b7b2
NC
5069 else
5070 as_bad (_("`%s' is only supported in 64-bit mode"),
76d3f746 5071 pass1_mnem ? pass1_mnem : insn_name (current_templates->start));
e8b4b7b2 5072 }
9db83a32 5073 return;
04784e33
JB
5074 case invalid_sib_address:
5075 err_msg = _("invalid SIB address");
5076 break;
5077 case invalid_vsib_address:
5078 err_msg = _("invalid VSIB address");
5079 break;
5080 case invalid_vector_register_set:
5081 err_msg = _("mask, index, and destination registers must be distinct");
5082 break;
5083 case invalid_tmm_register_set:
5084 err_msg = _("all tmm registers must be distinct");
5085 break;
5086 case invalid_dest_and_src_register_set:
5087 err_msg = _("destination and source registers must be distinct");
5088 break;
5089 case unsupported_vector_index_register:
5090 err_msg = _("unsupported vector index register");
5091 break;
5092 case unsupported_broadcast:
5093 err_msg = _("unsupported broadcast");
5094 break;
5095 case broadcast_needed:
5096 err_msg = _("broadcast is needed for operand of such type");
5097 break;
5098 case unsupported_masking:
5099 err_msg = _("unsupported masking");
5100 break;
5101 case mask_not_on_destination:
5102 err_msg = _("mask not on destination operand");
5103 break;
5104 case no_default_mask:
5105 err_msg = _("default mask isn't allowed");
5106 break;
5107 case unsupported_rc_sae:
5108 err_msg = _("unsupported static rounding/sae");
5109 break;
5110 case invalid_register_operand:
5111 err_msg = _("invalid register operand");
5112 break;
5113 }
5114 as_bad (_("%s for `%s'"), err_msg,
76d3f746 5115 pass1_mnem ? pass1_mnem : insn_name (current_templates->start));
04784e33
JB
5116 return;
5117 }
5118
5119 free (copy);
252b5132 5120
7bab8ab5 5121 if (sse_check != check_none
ffb86450
JB
5122 /* The opcode space check isn't strictly needed; it's there only to
5123 bypass the logic below when easily possible. */
ddb62495
JB
5124 && t->opcode_space >= SPACE_0F
5125 && t->opcode_space <= SPACE_0F3A
ffb86450
JB
5126 && !i.tm.cpu_flags.bitfield.cpusse4a
5127 && !is_any_vex_encoding (t))
daf50ae7 5128 {
ffb86450
JB
5129 bool simd = false;
5130
5131 for (j = 0; j < t->operands; ++j)
5132 {
5133 if (t->operand_types[j].bitfield.class == RegMMX)
5134 break;
5135 if (t->operand_types[j].bitfield.class == RegSIMD)
5136 simd = true;
5137 }
5138
5139 if (j >= t->operands && simd)
5140 (sse_check == check_warning
5141 ? as_warn
76d3f746 5142 : as_bad) (_("SSE instruction `%s' is used"), insn_name (&i.tm));
daf50ae7
L
5143 }
5144
40fb9820 5145 if (i.tm.opcode_modifier.fwait)
29b0f896
AM
5146 if (!add_prefix (FWAIT_OPCODE))
5147 return;
252b5132 5148
d5de92cf 5149 /* Check if REP prefix is OK. */
742732c7 5150 if (i.rep_prefix && i.tm.opcode_modifier.prefixok != PrefixRep)
d5de92cf
L
5151 {
5152 as_bad (_("invalid instruction `%s' after `%s'"),
76d3f746 5153 insn_name (&i.tm), i.rep_prefix);
d5de92cf
L
5154 return;
5155 }
5156
c1ba0266
L
5157 /* Check for lock without a lockable instruction. Destination operand
5158 must be memory unless it is xchg (0x86). */
9a4a4499
JB
5159 if (i.prefix[LOCK_PREFIX])
5160 {
5161 if (i.tm.opcode_modifier.prefixok < PrefixLock
c1ba0266
L
5162 || i.mem_operands == 0
5163 || (i.tm.base_opcode != 0x86
9a4a4499
JB
5164 && !(i.flags[i.operands - 1] & Operand_Mem)))
5165 {
5166 as_bad (_("expecting lockable instruction after `lock'"));
5167 return;
5168 }
5169
5170 /* Zap the redundant prefix from XCHG when optimizing. */
5171 if (i.tm.base_opcode == 0x86 && optimize && !i.no_optimize)
5172 i.prefix[LOCK_PREFIX] = 0;
c32fa91d
L
5173 }
5174
e3669c7f
JB
5175 if (is_any_vex_encoding (&i.tm)
5176 || i.tm.operand_types[i.imm_operands].bitfield.class >= RegMMX
5177 || i.tm.operand_types[i.imm_operands + 1].bitfield.class >= RegMMX)
7a8655d2 5178 {
e3669c7f
JB
5179 /* Check for data size prefix on VEX/XOP/EVEX encoded and SIMD insns. */
5180 if (i.prefix[DATA_PREFIX])
5181 {
76d3f746 5182 as_bad (_("data size prefix invalid with `%s'"), insn_name (&i.tm));
e3669c7f
JB
5183 return;
5184 }
5185
5186 /* Don't allow e.g. KMOV in TLS code sequences. */
5187 for (j = i.imm_operands; j < i.operands; ++j)
5188 switch (i.reloc[j])
5189 {
5190 case BFD_RELOC_386_TLS_GOTIE:
5191 case BFD_RELOC_386_TLS_LE_32:
5192 case BFD_RELOC_X86_64_GOTTPOFF:
5193 case BFD_RELOC_X86_64_TLSLD:
76d3f746 5194 as_bad (_("TLS relocation cannot be used with `%s'"), insn_name (&i.tm));
e3669c7f
JB
5195 return;
5196 default:
5197 break;
5198 }
7a8655d2
JB
5199 }
5200
42164a71 5201 /* Check if HLE prefix is OK. */
165de32a 5202 if (i.hle_prefix && !check_hle ())
42164a71
L
5203 return;
5204
7e8b059b
L
5205 /* Check BND prefix. */
5206 if (i.bnd_prefix && !i.tm.opcode_modifier.bndprefixok)
5207 as_bad (_("expecting valid branch instruction after `bnd'"));
5208
04ef582a 5209 /* Check NOTRACK prefix. */
742732c7 5210 if (i.notrack_prefix && i.tm.opcode_modifier.prefixok != PrefixNoTrack)
9fef80d6 5211 as_bad (_("expecting indirect branch instruction after `notrack'"));
04ef582a 5212
327e8c42
JB
5213 if (i.tm.cpu_flags.bitfield.cpumpx)
5214 {
5215 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
5216 as_bad (_("32-bit address isn't allowed in 64-bit MPX instructions."));
5217 else if (flag_code != CODE_16BIT
5218 ? i.prefix[ADDR_PREFIX]
5219 : i.mem_operands && !i.prefix[ADDR_PREFIX])
5220 as_bad (_("16-bit address isn't allowed in MPX instructions"));
5221 }
7e8b059b
L
5222
5223 /* Insert BND prefix. */
76d3a78a
JB
5224 if (add_bnd_prefix && i.tm.opcode_modifier.bndprefixok)
5225 {
5226 if (!i.prefix[BND_PREFIX])
5227 add_prefix (BND_PREFIX_OPCODE);
5228 else if (i.prefix[BND_PREFIX] != BND_PREFIX_OPCODE)
5229 {
5230 as_warn (_("replacing `rep'/`repe' prefix by `bnd'"));
5231 i.prefix[BND_PREFIX] = BND_PREFIX_OPCODE;
5232 }
5233 }
7e8b059b 5234
29b0f896 5235 /* Check string instruction segment overrides. */
51c8edf6 5236 if (i.tm.opcode_modifier.isstring >= IS_STRING_ES_OP0)
29b0f896 5237 {
51c8edf6 5238 gas_assert (i.mem_operands);
29b0f896 5239 if (!check_string ())
5dd0794d 5240 return;
fc0763e6 5241 i.disp_operands = 0;
29b0f896 5242 }
5dd0794d 5243
9373f275
L
5244 /* The memory operand of (%dx) should be only used with input/output
5245 instructions (base opcodes: 0x6c, 0x6e, 0xec, 0xee). */
5246 if (i.input_output_operand
5247 && ((i.tm.base_opcode | 0x82) != 0xee
ddb62495 5248 || i.tm.opcode_space != SPACE_BASE))
9373f275
L
5249 {
5250 as_bad (_("input/output port address isn't allowed with `%s'"),
76d3f746 5251 insn_name (&i.tm));
9373f275
L
5252 return;
5253 }
5254
b6f8c7c4
L
5255 if (optimize && !i.no_optimize && i.tm.opcode_modifier.optimize)
5256 optimize_encoding ();
5257
c8480b58
L
5258 if (use_unaligned_vector_move)
5259 encode_with_unaligned_vector_move ();
5260
29b0f896
AM
5261 if (!process_suffix ())
5262 return;
e413e4e9 5263
ef07be45
CL
5264 /* Check if IP-relative addressing requirements can be satisfied. */
5265 if (i.tm.cpu_flags.bitfield.cpuprefetchi
5266 && !(i.base_reg && i.base_reg->reg_num == RegIP))
76d3f746 5267 as_warn (_("'%s' only supports RIP-relative address"), insn_name (&i.tm));
ef07be45 5268
921eafea 5269 /* Update operand types and check extended states. */
bc0844ae 5270 for (j = 0; j < i.operands; j++)
921eafea
L
5271 {
5272 i.types[j] = operand_type_and (i.types[j], i.tm.operand_types[j]);
3d70986f 5273 switch (i.tm.operand_types[j].bitfield.class)
921eafea
L
5274 {
5275 default:
5276 break;
5277 case RegMMX:
5278 i.xstate |= xstate_mmx;
5279 break;
5280 case RegMask:
32930e4e 5281 i.xstate |= xstate_mask;
921eafea
L
5282 break;
5283 case RegSIMD:
3d70986f 5284 if (i.tm.operand_types[j].bitfield.tmmword)
921eafea 5285 i.xstate |= xstate_tmm;
3d70986f 5286 else if (i.tm.operand_types[j].bitfield.zmmword)
921eafea 5287 i.xstate |= xstate_zmm;
3d70986f 5288 else if (i.tm.operand_types[j].bitfield.ymmword)
921eafea 5289 i.xstate |= xstate_ymm;
3d70986f 5290 else if (i.tm.operand_types[j].bitfield.xmmword)
921eafea
L
5291 i.xstate |= xstate_xmm;
5292 break;
5293 }
5294 }
bc0844ae 5295
29b0f896
AM
5296 /* Make still unresolved immediate matches conform to size of immediate
5297 given in i.suffix. */
5298 if (!finalize_imm ())
5299 return;
252b5132 5300
40fb9820 5301 if (i.types[0].bitfield.imm1)
29b0f896 5302 i.imm_operands = 0; /* kludge for shift insns. */
252b5132 5303
29b0f896
AM
5304 /* For insns with operands there are more diddles to do to the opcode. */
5305 if (i.operands)
5306 {
5307 if (!process_operands ())
5308 return;
5309 }
255571cd 5310 else if (!quiet_warnings && i.tm.opcode_modifier.operandconstraint == UGH)
29b0f896
AM
5311 {
5312 /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc. */
76d3f746 5313 as_warn (_("translating to `%sp'"), insn_name (&i.tm));
29b0f896 5314 }
252b5132 5315
7a8655d2 5316 if (is_any_vex_encoding (&i.tm))
9e5e5283 5317 {
c1dc7af5 5318 if (!cpu_arch_flags.bitfield.cpui286)
9e5e5283 5319 {
c1dc7af5 5320 as_bad (_("instruction `%s' isn't supported outside of protected mode."),
76d3f746 5321 insn_name (&i.tm));
9e5e5283
L
5322 return;
5323 }
c0f3af97 5324
0b9404fd
JB
5325 /* Check for explicit REX prefix. */
5326 if (i.prefix[REX_PREFIX] || i.rex_encoding)
5327 {
76d3f746 5328 as_bad (_("REX prefix invalid with `%s'"), insn_name (&i.tm));
0b9404fd
JB
5329 return;
5330 }
5331
9e5e5283
L
5332 if (i.tm.opcode_modifier.vex)
5333 build_vex_prefix (t);
5334 else
5335 build_evex_prefix ();
0b9404fd
JB
5336
5337 /* The individual REX.RXBW bits got consumed. */
5338 i.rex &= REX_OPCODE;
9e5e5283 5339 }
43234a1e 5340
7fc69528
JB
5341 /* Handle conversion of 'int $3' --> special int3 insn. */
5342 if (i.tm.mnem_off == MN_int
a6461c02 5343 && i.op[0].imms->X_add_number == 3)
29b0f896
AM
5344 {
5345 i.tm.base_opcode = INT3_OPCODE;
5346 i.imm_operands = 0;
5347 }
252b5132 5348
0cfa3eb3
JB
5349 if ((i.tm.opcode_modifier.jump == JUMP
5350 || i.tm.opcode_modifier.jump == JUMP_BYTE
5351 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896
AM
5352 && i.op[0].disps->X_op == O_constant)
5353 {
5354 /* Convert "jmp constant" (and "call constant") to a jump (call) to
5355 the absolute address given by the constant. Since ix86 jumps and
5356 calls are pc relative, we need to generate a reloc. */
5357 i.op[0].disps->X_add_symbol = &abs_symbol;
5358 i.op[0].disps->X_op = O_symbol;
5359 }
252b5132 5360
29b0f896
AM
5361 /* For 8 bit registers we need an empty rex prefix. Also if the
5362 instruction already has a prefix, we need to convert old
5363 registers to new ones. */
773f551c 5364
bab6aec1 5365 if ((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte
29b0f896 5366 && (i.op[0].regs->reg_flags & RegRex64) != 0)
bab6aec1 5367 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte
29b0f896 5368 && (i.op[1].regs->reg_flags & RegRex64) != 0)
bab6aec1
JB
5369 || (((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte)
5370 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte))
29b0f896
AM
5371 && i.rex != 0))
5372 {
5373 int x;
726c5dcd 5374
29b0f896
AM
5375 i.rex |= REX_OPCODE;
5376 for (x = 0; x < 2; x++)
5377 {
5378 /* Look for 8 bit operand that uses old registers. */
bab6aec1 5379 if (i.types[x].bitfield.class == Reg && i.types[x].bitfield.byte
29b0f896 5380 && (i.op[x].regs->reg_flags & RegRex64) == 0)
773f551c 5381 {
3f93af61 5382 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
29b0f896
AM
5383 /* In case it is "hi" register, give up. */
5384 if (i.op[x].regs->reg_num > 3)
a540244d 5385 as_bad (_("can't encode register '%s%s' in an "
4eed87de 5386 "instruction requiring REX prefix."),
a540244d 5387 register_prefix, i.op[x].regs->reg_name);
773f551c 5388
29b0f896
AM
5389 /* Otherwise it is equivalent to the extended register.
5390 Since the encoding doesn't change this is merely
5391 cosmetic cleanup for debug output. */
5392
5393 i.op[x].regs = i.op[x].regs + 8;
773f551c 5394 }
29b0f896
AM
5395 }
5396 }
773f551c 5397
6b6b6807
L
5398 if (i.rex == 0 && i.rex_encoding)
5399 {
5400 /* Check if we can add a REX_OPCODE byte. Look for 8 bit operand
3f93af61 5401 that uses legacy register. If it is "hi" register, don't add
6b6b6807
L
5402 the REX_OPCODE byte. */
5403 int x;
5404 for (x = 0; x < 2; x++)
bab6aec1 5405 if (i.types[x].bitfield.class == Reg
6b6b6807
L
5406 && i.types[x].bitfield.byte
5407 && (i.op[x].regs->reg_flags & RegRex64) == 0
5408 && i.op[x].regs->reg_num > 3)
5409 {
3f93af61 5410 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
5b7c81bd 5411 i.rex_encoding = false;
6b6b6807
L
5412 break;
5413 }
5414
5415 if (i.rex_encoding)
5416 i.rex = REX_OPCODE;
5417 }
5418
7ab9ffdd 5419 if (i.rex != 0)
29b0f896
AM
5420 add_prefix (REX_OPCODE | i.rex);
5421
ae531041
L
5422 insert_lfence_before ();
5423
29b0f896
AM
5424 /* We are ready to output the insn. */
5425 output_insn ();
e379e5f3 5426
ae531041
L
5427 insert_lfence_after ();
5428
e379e5f3
L
5429 last_insn.seg = now_seg;
5430
5431 if (i.tm.opcode_modifier.isprefix)
5432 {
5433 last_insn.kind = last_insn_prefix;
76d3f746 5434 last_insn.name = insn_name (&i.tm);
e379e5f3
L
5435 last_insn.file = as_where (&last_insn.line);
5436 }
5437 else
5438 last_insn.kind = last_insn_other;
29b0f896
AM
5439}
5440
9db83a32
JB
5441/* The Q suffix is generally valid only in 64-bit mode, with very few
5442 exceptions: fild, fistp, fisttp, and cmpxchg8b. Note that for fild
5443 and fisttp only one of their two templates is matched below: That's
5444 sufficient since other relevant attributes are the same between both
5445 respective templates. */
5446static INLINE bool q_suffix_allowed(const insn_template *t)
5447{
5448 return flag_code == CODE_64BIT
ddb62495 5449 || (t->opcode_space == SPACE_BASE
9db83a32
JB
5450 && t->base_opcode == 0xdf
5451 && (t->extension_opcode & 1)) /* fild / fistp / fisttp */
7fc69528 5452 || t->mnem_off == MN_cmpxchg8b;
9db83a32
JB
5453}
5454
5317ad2c 5455static const char *
edd67638 5456parse_insn (const char *line, char *mnemonic, bool prefix_only)
29b0f896 5457{
5317ad2c 5458 const char *l = line, *token_start = l;
29b0f896 5459 char *mnem_p;
04784e33 5460 bool pass1 = !current_templates;
5c6af06e 5461 int supported;
d3ce72d0 5462 const insn_template *t;
b6169b20 5463 char *dot_p = NULL;
29b0f896 5464
29b0f896
AM
5465 while (1)
5466 {
5467 mnem_p = mnemonic;
5468 while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
5469 {
b6169b20
L
5470 if (*mnem_p == '.')
5471 dot_p = mnem_p;
29b0f896
AM
5472 mnem_p++;
5473 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
45288df1 5474 {
29b0f896
AM
5475 as_bad (_("no such instruction: `%s'"), token_start);
5476 return NULL;
5477 }
5478 l++;
5479 }
5480 if (!is_space_char (*l)
5481 && *l != END_OF_INSN
e44823cf
JB
5482 && (intel_syntax
5483 || (*l != PREFIX_SEPARATOR
5484 && *l != ',')))
29b0f896 5485 {
edd67638
JB
5486 if (prefix_only)
5487 break;
29b0f896
AM
5488 as_bad (_("invalid character %s in mnemonic"),
5489 output_invalid (*l));
5490 return NULL;
5491 }
5492 if (token_start == l)
5493 {
e44823cf 5494 if (!intel_syntax && *l == PREFIX_SEPARATOR)
29b0f896
AM
5495 as_bad (_("expecting prefix; got nothing"));
5496 else
5497 as_bad (_("expecting mnemonic; got nothing"));
5498 return NULL;
5499 }
45288df1 5500
29b0f896 5501 /* Look up instruction (or prefix) via hash table. */
629310ab 5502 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
47926f60 5503
29b0f896
AM
5504 if (*l != END_OF_INSN
5505 && (!is_space_char (*l) || l[1] != END_OF_INSN)
5506 && current_templates
40fb9820 5507 && current_templates->start->opcode_modifier.isprefix)
29b0f896 5508 {
c6fb90c8 5509 if (!cpu_flags_check_cpu64 (current_templates->start->cpu_flags))
2dd88dca
JB
5510 {
5511 as_bad ((flag_code != CODE_64BIT
5512 ? _("`%s' is only supported in 64-bit mode")
5513 : _("`%s' is not supported in 64-bit mode")),
76d3f746 5514 insn_name (current_templates->start));
2dd88dca
JB
5515 return NULL;
5516 }
29b0f896
AM
5517 /* If we are in 16-bit mode, do not allow addr16 or data16.
5518 Similarly, in 32-bit mode, do not allow addr32 or data32. */
673fe0f0
JB
5519 if ((current_templates->start->opcode_modifier.size == SIZE16
5520 || current_templates->start->opcode_modifier.size == SIZE32)
29b0f896 5521 && flag_code != CODE_64BIT
673fe0f0 5522 && ((current_templates->start->opcode_modifier.size == SIZE32)
29b0f896
AM
5523 ^ (flag_code == CODE_16BIT)))
5524 {
5525 as_bad (_("redundant %s prefix"),
76d3f746 5526 insn_name (current_templates->start));
29b0f896 5527 return NULL;
45288df1 5528 }
31184569
JB
5529
5530 if (current_templates->start->base_opcode == PSEUDO_PREFIX)
29b0f896 5531 {
86fa6981 5532 /* Handle pseudo prefixes. */
31184569 5533 switch (current_templates->start->extension_opcode)
86fa6981 5534 {
41eb8e88 5535 case Prefix_Disp8:
86fa6981
L
5536 /* {disp8} */
5537 i.disp_encoding = disp_encoding_8bit;
5538 break;
41eb8e88
L
5539 case Prefix_Disp16:
5540 /* {disp16} */
5541 i.disp_encoding = disp_encoding_16bit;
5542 break;
5543 case Prefix_Disp32:
86fa6981
L
5544 /* {disp32} */
5545 i.disp_encoding = disp_encoding_32bit;
5546 break;
41eb8e88 5547 case Prefix_Load:
86fa6981
L
5548 /* {load} */
5549 i.dir_encoding = dir_encoding_load;
5550 break;
41eb8e88 5551 case Prefix_Store:
86fa6981
L
5552 /* {store} */
5553 i.dir_encoding = dir_encoding_store;
5554 break;
41eb8e88 5555 case Prefix_VEX:
42e04b36
L
5556 /* {vex} */
5557 i.vec_encoding = vex_encoding_vex;
86fa6981 5558 break;
41eb8e88 5559 case Prefix_VEX3:
86fa6981
L
5560 /* {vex3} */
5561 i.vec_encoding = vex_encoding_vex3;
5562 break;
41eb8e88 5563 case Prefix_EVEX:
86fa6981
L
5564 /* {evex} */
5565 i.vec_encoding = vex_encoding_evex;
5566 break;
41eb8e88 5567 case Prefix_REX:
6b6b6807 5568 /* {rex} */
5b7c81bd 5569 i.rex_encoding = true;
6b6b6807 5570 break;
41eb8e88 5571 case Prefix_NoOptimize:
b6f8c7c4 5572 /* {nooptimize} */
5b7c81bd 5573 i.no_optimize = true;
b6f8c7c4 5574 break;
86fa6981
L
5575 default:
5576 abort ();
5577 }
5578 }
5579 else
5580 {
5581 /* Add prefix, checking for repeated prefixes. */
4e9ac44a 5582 switch (add_prefix (current_templates->start->base_opcode))
86fa6981 5583 {
4e9ac44a
L
5584 case PREFIX_EXIST:
5585 return NULL;
5586 case PREFIX_DS:
d777820b 5587 if (current_templates->start->cpu_flags.bitfield.cpuibt)
76d3f746 5588 i.notrack_prefix = insn_name (current_templates->start);
4e9ac44a
L
5589 break;
5590 case PREFIX_REP:
5591 if (current_templates->start->cpu_flags.bitfield.cpuhle)
76d3f746 5592 i.hle_prefix = insn_name (current_templates->start);
4e9ac44a 5593 else if (current_templates->start->cpu_flags.bitfield.cpumpx)
76d3f746 5594 i.bnd_prefix = insn_name (current_templates->start);
4e9ac44a 5595 else
76d3f746 5596 i.rep_prefix = insn_name (current_templates->start);
4e9ac44a
L
5597 break;
5598 default:
5599 break;
86fa6981 5600 }
29b0f896
AM
5601 }
5602 /* Skip past PREFIX_SEPARATOR and reset token_start. */
5603 token_start = ++l;
5604 }
5605 else
5606 break;
5607 }
45288df1 5608
edd67638
JB
5609 if (prefix_only)
5610 return token_start;
5611
30a55f88 5612 if (!current_templates)
b6169b20 5613 {
07d5e953
JB
5614 /* Deprecated functionality (new code should use pseudo-prefixes instead):
5615 Check if we should swap operand or force 32bit displacement in
f8a5c266 5616 encoding. */
30a55f88 5617 if (mnem_p - 2 == dot_p && dot_p[1] == 's')
64c49ab3 5618 i.dir_encoding = dir_encoding_swap;
8d63c93e 5619 else if (mnem_p - 3 == dot_p
a501d77e
L
5620 && dot_p[1] == 'd'
5621 && dot_p[2] == '8')
5622 i.disp_encoding = disp_encoding_8bit;
8d63c93e 5623 else if (mnem_p - 4 == dot_p
f8a5c266
L
5624 && dot_p[1] == 'd'
5625 && dot_p[2] == '3'
5626 && dot_p[3] == '2')
a501d77e 5627 i.disp_encoding = disp_encoding_32bit;
30a55f88
L
5628 else
5629 goto check_suffix;
5630 mnem_p = dot_p;
5631 *dot_p = '\0';
629310ab 5632 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
b6169b20
L
5633 }
5634
04784e33 5635 if (!current_templates || !pass1)
29b0f896 5636 {
04784e33
JB
5637 current_templates = NULL;
5638
dc1e8a47 5639 check_suffix:
1c529385 5640 if (mnem_p > mnemonic)
29b0f896 5641 {
1c529385
LH
5642 /* See if we can get a match by trimming off a suffix. */
5643 switch (mnem_p[-1])
29b0f896 5644 {
1c529385
LH
5645 case WORD_MNEM_SUFFIX:
5646 if (intel_syntax && (intel_float_operand (mnemonic) & 2))
29b0f896
AM
5647 i.suffix = SHORT_MNEM_SUFFIX;
5648 else
1c529385
LH
5649 /* Fall through. */
5650 case BYTE_MNEM_SUFFIX:
5651 case QWORD_MNEM_SUFFIX:
5652 i.suffix = mnem_p[-1];
29b0f896 5653 mnem_p[-1] = '\0';
fe0e921f
AM
5654 current_templates
5655 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5656 break;
5657 case SHORT_MNEM_SUFFIX:
5658 case LONG_MNEM_SUFFIX:
5659 if (!intel_syntax)
5660 {
5661 i.suffix = mnem_p[-1];
5662 mnem_p[-1] = '\0';
fe0e921f
AM
5663 current_templates
5664 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5665 }
5666 break;
5667
5668 /* Intel Syntax. */
5669 case 'd':
5670 if (intel_syntax)
5671 {
5672 if (intel_float_operand (mnemonic) == 1)
5673 i.suffix = SHORT_MNEM_SUFFIX;
5674 else
5675 i.suffix = LONG_MNEM_SUFFIX;
5676 mnem_p[-1] = '\0';
fe0e921f
AM
5677 current_templates
5678 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385 5679 }
04784e33
JB
5680 /* For compatibility reasons accept MOVSD and CMPSD without
5681 operands even in AT&T mode. */
5682 else if (*l == END_OF_INSN
5683 || (is_space_char (*l) && l[1] == END_OF_INSN))
5684 {
5685 mnem_p[-1] = '\0';
5686 current_templates
5687 = (const templates *) str_hash_find (op_hash, mnemonic);
5688 if (current_templates != NULL
5689 /* MOVS or CMPS */
5690 && (current_templates->start->base_opcode | 2) == 0xa6
ddb62495 5691 && current_templates->start->opcode_space
04784e33
JB
5692 == SPACE_BASE
5693 && mnem_p[-2] == 's')
5694 {
5695 as_warn (_("found `%sd'; assuming `%sl' was meant"),
5696 mnemonic, mnemonic);
5697 i.suffix = LONG_MNEM_SUFFIX;
5698 }
5699 else
5700 {
5701 current_templates = NULL;
5702 mnem_p[-1] = 'd';
5703 }
5704 }
1c529385 5705 break;
29b0f896 5706 }
29b0f896 5707 }
1c529385 5708
29b0f896
AM
5709 if (!current_templates)
5710 {
04784e33
JB
5711 if (pass1)
5712 as_bad (_("no such instruction: `%s'"), token_start);
29b0f896
AM
5713 return NULL;
5714 }
5715 }
252b5132 5716
0cfa3eb3
JB
5717 if (current_templates->start->opcode_modifier.jump == JUMP
5718 || current_templates->start->opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
5719 {
5720 /* Check for a branch hint. We allow ",pt" and ",pn" for
5721 predict taken and predict not taken respectively.
5722 I'm not sure that branch hints actually do anything on loop
5723 and jcxz insns (JumpByte) for current Pentium4 chips. They
5724 may work in the future and it doesn't hurt to accept them
5725 now. */
5726 if (l[0] == ',' && l[1] == 'p')
5727 {
5728 if (l[2] == 't')
5729 {
5730 if (!add_prefix (DS_PREFIX_OPCODE))
5731 return NULL;
5732 l += 3;
5733 }
5734 else if (l[2] == 'n')
5735 {
5736 if (!add_prefix (CS_PREFIX_OPCODE))
5737 return NULL;
5738 l += 3;
5739 }
5740 }
5741 }
5742 /* Any other comma loses. */
5743 if (*l == ',')
5744 {
5745 as_bad (_("invalid character %s in mnemonic"),
5746 output_invalid (*l));
5747 return NULL;
5748 }
252b5132 5749
29b0f896 5750 /* Check if instruction is supported on specified architecture. */
5c6af06e
JB
5751 supported = 0;
5752 for (t = current_templates->start; t < current_templates->end; ++t)
5753 {
c0f3af97 5754 supported |= cpu_flags_match (t);
9db83a32
JB
5755
5756 if (i.suffix == QWORD_MNEM_SUFFIX && !q_suffix_allowed (t))
5757 supported &= ~CPU_FLAGS_64BIT_MATCH;
5758
c0f3af97 5759 if (supported == CPU_FLAGS_PERFECT_MATCH)
d59a54c2 5760 return l;
29b0f896 5761 }
3629bb00 5762
9db83a32
JB
5763 if (pass1)
5764 {
5765 if (supported & CPU_FLAGS_64BIT_MATCH)
5766 i.error = unsupported_on_arch;
5767 else
5768 i.error = unsupported_64bit;
5769 }
252b5132 5770
548d0ee6 5771 return NULL;
29b0f896 5772}
252b5132 5773
29b0f896 5774static char *
e3bb37b5 5775parse_operands (char *l, const char *mnemonic)
29b0f896
AM
5776{
5777 char *token_start;
3138f287 5778
29b0f896
AM
5779 /* 1 if operand is pending after ','. */
5780 unsigned int expecting_operand = 0;
252b5132 5781
29b0f896
AM
5782 while (*l != END_OF_INSN)
5783 {
e68c3d59
JB
5784 /* Non-zero if operand parens not balanced. */
5785 unsigned int paren_not_balanced = 0;
5786 /* True if inside double quotes. */
5787 bool in_quotes = false;
5788
29b0f896
AM
5789 /* Skip optional white space before operand. */
5790 if (is_space_char (*l))
5791 ++l;
d02603dc 5792 if (!is_operand_char (*l) && *l != END_OF_INSN && *l != '"')
29b0f896
AM
5793 {
5794 as_bad (_("invalid character %s before operand %d"),
5795 output_invalid (*l),
5796 i.operands + 1);
5797 return NULL;
5798 }
d02603dc 5799 token_start = l; /* After white space. */
e68c3d59 5800 while (in_quotes || paren_not_balanced || *l != ',')
29b0f896
AM
5801 {
5802 if (*l == END_OF_INSN)
5803 {
e68c3d59
JB
5804 if (in_quotes)
5805 {
5806 as_bad (_("unbalanced double quotes in operand %d."),
5807 i.operands + 1);
5808 return NULL;
5809 }
29b0f896
AM
5810 if (paren_not_balanced)
5811 {
98ff9f1c
JB
5812 know (!intel_syntax);
5813 as_bad (_("unbalanced parenthesis in operand %d."),
5814 i.operands + 1);
29b0f896
AM
5815 return NULL;
5816 }
5817 else
5818 break; /* we are done */
5819 }
e68c3d59
JB
5820 else if (*l == '\\' && l[1] == '"')
5821 ++l;
5822 else if (*l == '"')
5823 in_quotes = !in_quotes;
5824 else if (!in_quotes && !is_operand_char (*l) && !is_space_char (*l))
29b0f896
AM
5825 {
5826 as_bad (_("invalid character %s in operand %d"),
5827 output_invalid (*l),
5828 i.operands + 1);
5829 return NULL;
5830 }
e68c3d59 5831 if (!intel_syntax && !in_quotes)
29b0f896
AM
5832 {
5833 if (*l == '(')
5834 ++paren_not_balanced;
5835 if (*l == ')')
5836 --paren_not_balanced;
5837 }
29b0f896
AM
5838 l++;
5839 }
5840 if (l != token_start)
5841 { /* Yes, we've read in another operand. */
5842 unsigned int operand_ok;
5843 this_operand = i.operands++;
5844 if (i.operands > MAX_OPERANDS)
5845 {
5846 as_bad (_("spurious operands; (%d operands/instruction max)"),
5847 MAX_OPERANDS);
5848 return NULL;
5849 }
9d46ce34 5850 i.types[this_operand].bitfield.unspecified = 1;
29b0f896
AM
5851 /* Now parse operand adding info to 'i' as we go along. */
5852 END_STRING_AND_SAVE (l);
5853
1286ab78
L
5854 if (i.mem_operands > 1)
5855 {
5856 as_bad (_("too many memory references for `%s'"),
5857 mnemonic);
5858 return 0;
5859 }
5860
29b0f896
AM
5861 if (intel_syntax)
5862 operand_ok =
5863 i386_intel_operand (token_start,
5864 intel_float_operand (mnemonic));
5865 else
a7619375 5866 operand_ok = i386_att_operand (token_start);
29b0f896
AM
5867
5868 RESTORE_END_STRING (l);
5869 if (!operand_ok)
5870 return NULL;
5871 }
5872 else
5873 {
5874 if (expecting_operand)
5875 {
5876 expecting_operand_after_comma:
5877 as_bad (_("expecting operand after ','; got nothing"));
5878 return NULL;
5879 }
5880 if (*l == ',')
5881 {
5882 as_bad (_("expecting operand before ','; got nothing"));
5883 return NULL;
5884 }
5885 }
7f3f1ea2 5886
29b0f896
AM
5887 /* Now *l must be either ',' or END_OF_INSN. */
5888 if (*l == ',')
5889 {
5890 if (*++l == END_OF_INSN)
5891 {
5892 /* Just skip it, if it's \n complain. */
5893 goto expecting_operand_after_comma;
5894 }
5895 expecting_operand = 1;
5896 }
5897 }
5898 return l;
5899}
7f3f1ea2 5900
050dfa73 5901static void
783c187b 5902swap_2_operands (unsigned int xchg1, unsigned int xchg2)
050dfa73
MM
5903{
5904 union i386_op temp_op;
40fb9820 5905 i386_operand_type temp_type;
c48dadc9 5906 unsigned int temp_flags;
050dfa73 5907 enum bfd_reloc_code_real temp_reloc;
4eed87de 5908
050dfa73
MM
5909 temp_type = i.types[xchg2];
5910 i.types[xchg2] = i.types[xchg1];
5911 i.types[xchg1] = temp_type;
c48dadc9
JB
5912
5913 temp_flags = i.flags[xchg2];
5914 i.flags[xchg2] = i.flags[xchg1];
5915 i.flags[xchg1] = temp_flags;
5916
050dfa73
MM
5917 temp_op = i.op[xchg2];
5918 i.op[xchg2] = i.op[xchg1];
5919 i.op[xchg1] = temp_op;
c48dadc9 5920
050dfa73
MM
5921 temp_reloc = i.reloc[xchg2];
5922 i.reloc[xchg2] = i.reloc[xchg1];
5923 i.reloc[xchg1] = temp_reloc;
43234a1e 5924
6225c532 5925 if (i.mask.reg)
43234a1e 5926 {
6225c532
JB
5927 if (i.mask.operand == xchg1)
5928 i.mask.operand = xchg2;
5929 else if (i.mask.operand == xchg2)
5930 i.mask.operand = xchg1;
43234a1e 5931 }
a5748e0d 5932 if (i.broadcast.type || i.broadcast.bytes)
43234a1e 5933 {
5273a3cd
JB
5934 if (i.broadcast.operand == xchg1)
5935 i.broadcast.operand = xchg2;
5936 else if (i.broadcast.operand == xchg2)
5937 i.broadcast.operand = xchg1;
43234a1e 5938 }
050dfa73
MM
5939}
5940
29b0f896 5941static void
e3bb37b5 5942swap_operands (void)
29b0f896 5943{
b7c61d9a 5944 switch (i.operands)
050dfa73 5945 {
c0f3af97 5946 case 5:
b7c61d9a 5947 case 4:
4d456e3d 5948 swap_2_operands (1, i.operands - 2);
1a0670f3 5949 /* Fall through. */
b7c61d9a
L
5950 case 3:
5951 case 2:
4d456e3d 5952 swap_2_operands (0, i.operands - 1);
b7c61d9a
L
5953 break;
5954 default:
5955 abort ();
29b0f896 5956 }
29b0f896
AM
5957
5958 if (i.mem_operands == 2)
5959 {
5e042380 5960 const reg_entry *temp_seg;
29b0f896
AM
5961 temp_seg = i.seg[0];
5962 i.seg[0] = i.seg[1];
5963 i.seg[1] = temp_seg;
5964 }
5965}
252b5132 5966
29b0f896
AM
5967/* Try to ensure constant immediates are represented in the smallest
5968 opcode possible. */
5969static void
e3bb37b5 5970optimize_imm (void)
29b0f896
AM
5971{
5972 char guess_suffix = 0;
5973 int op;
252b5132 5974
29b0f896
AM
5975 if (i.suffix)
5976 guess_suffix = i.suffix;
5977 else if (i.reg_operands)
5978 {
5979 /* Figure out a suffix from the last register operand specified.
75e5731b
JB
5980 We can't do this properly yet, i.e. excluding special register
5981 instances, but the following works for instructions with
5982 immediates. In any case, we can't set i.suffix yet. */
29b0f896 5983 for (op = i.operands; --op >= 0;)
bab6aec1
JB
5984 if (i.types[op].bitfield.class != Reg)
5985 continue;
5986 else if (i.types[op].bitfield.byte)
7ab9ffdd 5987 {
40fb9820
L
5988 guess_suffix = BYTE_MNEM_SUFFIX;
5989 break;
5990 }
bab6aec1 5991 else if (i.types[op].bitfield.word)
252b5132 5992 {
40fb9820
L
5993 guess_suffix = WORD_MNEM_SUFFIX;
5994 break;
5995 }
bab6aec1 5996 else if (i.types[op].bitfield.dword)
40fb9820
L
5997 {
5998 guess_suffix = LONG_MNEM_SUFFIX;
5999 break;
6000 }
bab6aec1 6001 else if (i.types[op].bitfield.qword)
40fb9820
L
6002 {
6003 guess_suffix = QWORD_MNEM_SUFFIX;
29b0f896 6004 break;
252b5132 6005 }
29b0f896
AM
6006 }
6007 else if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
6008 guess_suffix = WORD_MNEM_SUFFIX;
6009
6010 for (op = i.operands; --op >= 0;)
40fb9820 6011 if (operand_type_check (i.types[op], imm))
29b0f896
AM
6012 {
6013 switch (i.op[op].imms->X_op)
252b5132 6014 {
29b0f896
AM
6015 case O_constant:
6016 /* If a suffix is given, this operand may be shortened. */
6017 switch (guess_suffix)
252b5132 6018 {
29b0f896 6019 case LONG_MNEM_SUFFIX:
40fb9820
L
6020 i.types[op].bitfield.imm32 = 1;
6021 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
6022 break;
6023 case WORD_MNEM_SUFFIX:
40fb9820
L
6024 i.types[op].bitfield.imm16 = 1;
6025 i.types[op].bitfield.imm32 = 1;
6026 i.types[op].bitfield.imm32s = 1;
6027 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
6028 break;
6029 case BYTE_MNEM_SUFFIX:
40fb9820
L
6030 i.types[op].bitfield.imm8 = 1;
6031 i.types[op].bitfield.imm8s = 1;
6032 i.types[op].bitfield.imm16 = 1;
6033 i.types[op].bitfield.imm32 = 1;
6034 i.types[op].bitfield.imm32s = 1;
6035 i.types[op].bitfield.imm64 = 1;
29b0f896 6036 break;
252b5132 6037 }
252b5132 6038
29b0f896
AM
6039 /* If this operand is at most 16 bits, convert it
6040 to a signed 16 bit number before trying to see
6041 whether it will fit in an even smaller size.
6042 This allows a 16-bit operand such as $0xffe0 to
6043 be recognised as within Imm8S range. */
40fb9820 6044 if ((i.types[op].bitfield.imm16)
7e96fb68 6045 && fits_in_unsigned_word (i.op[op].imms->X_add_number))
252b5132 6046 {
87ed972d
JB
6047 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
6048 ^ 0x8000) - 0x8000);
29b0f896 6049 }
a28def75
L
6050#ifdef BFD64
6051 /* Store 32-bit immediate in 64-bit for 64-bit BFD. */
40fb9820 6052 if ((i.types[op].bitfield.imm32)
7e96fb68 6053 && fits_in_unsigned_long (i.op[op].imms->X_add_number))
29b0f896
AM
6054 {
6055 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
6056 ^ ((offsetT) 1 << 31))
6057 - ((offsetT) 1 << 31));
6058 }
a28def75 6059#endif
40fb9820 6060 i.types[op]
c6fb90c8
L
6061 = operand_type_or (i.types[op],
6062 smallest_imm_type (i.op[op].imms->X_add_number));
252b5132 6063
29b0f896
AM
6064 /* We must avoid matching of Imm32 templates when 64bit
6065 only immediate is available. */
6066 if (guess_suffix == QWORD_MNEM_SUFFIX)
40fb9820 6067 i.types[op].bitfield.imm32 = 0;
29b0f896 6068 break;
252b5132 6069
29b0f896
AM
6070 case O_absent:
6071 case O_register:
6072 abort ();
6073
6074 /* Symbols and expressions. */
6075 default:
9cd96992
JB
6076 /* Convert symbolic operand to proper sizes for matching, but don't
6077 prevent matching a set of insns that only supports sizes other
6078 than those matching the insn suffix. */
6079 {
40fb9820 6080 i386_operand_type mask, allowed;
87ed972d 6081 const insn_template *t = current_templates->start;
9cd96992 6082
0dfbf9d7 6083 operand_type_set (&mask, 0);
9cd96992
JB
6084 switch (guess_suffix)
6085 {
6086 case QWORD_MNEM_SUFFIX:
40fb9820
L
6087 mask.bitfield.imm64 = 1;
6088 mask.bitfield.imm32s = 1;
9cd96992
JB
6089 break;
6090 case LONG_MNEM_SUFFIX:
40fb9820 6091 mask.bitfield.imm32 = 1;
9cd96992
JB
6092 break;
6093 case WORD_MNEM_SUFFIX:
40fb9820 6094 mask.bitfield.imm16 = 1;
9cd96992
JB
6095 break;
6096 case BYTE_MNEM_SUFFIX:
40fb9820 6097 mask.bitfield.imm8 = 1;
9cd96992
JB
6098 break;
6099 default:
9cd96992
JB
6100 break;
6101 }
8f0212ac
JB
6102
6103 allowed = operand_type_and (t->operand_types[op], mask);
6104 while (++t < current_templates->end)
6105 {
6106 allowed = operand_type_or (allowed, t->operand_types[op]);
6107 allowed = operand_type_and (allowed, mask);
6108 }
6109
0dfbf9d7 6110 if (!operand_type_all_zero (&allowed))
c6fb90c8 6111 i.types[op] = operand_type_and (i.types[op], mask);
9cd96992 6112 }
29b0f896 6113 break;
252b5132 6114 }
29b0f896
AM
6115 }
6116}
47926f60 6117
29b0f896 6118/* Try to use the smallest displacement type too. */
0de704b9
JB
6119static bool
6120optimize_disp (const insn_template *t)
29b0f896 6121{
0de704b9 6122 unsigned int op;
3e73aa7c 6123
0de704b9
JB
6124 if (!want_disp32 (t)
6125 && (!t->opcode_modifier.jump
6126 || i.jumpabsolute || i.types[0].bitfield.baseindex))
6127 {
6128 for (op = 0; op < i.operands; ++op)
6129 {
6130 const expressionS *exp = i.op[op].disps;
6131
6132 if (!operand_type_check (i.types[op], disp))
6133 continue;
6134
6135 if (exp->X_op != O_constant)
6136 continue;
6137
6138 /* Since displacement is signed extended to 64bit, don't allow
6139 disp32 if it is out of range. */
6140 if (fits_in_signed_long (exp->X_add_number))
6141 continue;
6142
6143 i.types[op].bitfield.disp32 = 0;
6144 if (i.types[op].bitfield.baseindex)
6145 {
6146 as_bad (_("0x%" PRIx64 " out of range of signed 32bit displacement"),
6147 (uint64_t) exp->X_add_number);
6148 return false;
6149 }
6150 }
6151 }
6152
6153 /* Don't optimize displacement for movabs since it only takes 64bit
6154 displacement. */
6155 if (i.disp_encoding > disp_encoding_8bit
6156 || (flag_code == CODE_64BIT && t->mnem_off == MN_movabs))
6157 return true;
6158
6159 for (op = i.operands; op-- > 0;)
40fb9820 6160 if (operand_type_check (i.types[op], disp))
252b5132 6161 {
b300c311 6162 if (i.op[op].disps->X_op == O_constant)
252b5132 6163 {
91d6fa6a 6164 offsetT op_disp = i.op[op].disps->X_add_number;
29b0f896 6165
91d6fa6a 6166 if (!op_disp && i.types[op].bitfield.baseindex)
b300c311 6167 {
2f2be86b
JB
6168 i.types[op] = operand_type_and_not (i.types[op], anydisp);
6169 i.op[op].disps = NULL;
b300c311 6170 i.disp_operands--;
f185acdd
JB
6171 continue;
6172 }
6173
6174 if (i.types[op].bitfield.disp16
cd613c1f 6175 && fits_in_unsigned_word (op_disp))
f185acdd
JB
6176 {
6177 /* If this operand is at most 16 bits, convert
6178 to a signed 16 bit number and don't use 64bit
6179 displacement. */
6180 op_disp = ((op_disp ^ 0x8000) - 0x8000);
6181 i.types[op].bitfield.disp64 = 0;
b300c311 6182 }
f185acdd 6183
28a167a4 6184#ifdef BFD64
a50187b2 6185 /* Optimize 64-bit displacement to 32-bit for 64-bit BFD. */
a775efc8
JB
6186 if ((flag_code != CODE_64BIT
6187 ? i.types[op].bitfield.disp32
0de704b9
JB
6188 : want_disp32 (t)
6189 && (!t->opcode_modifier.jump
a775efc8 6190 || i.jumpabsolute || i.types[op].bitfield.baseindex))
a50187b2 6191 && fits_in_unsigned_long (op_disp))
b300c311 6192 {
a50187b2
JB
6193 /* If this operand is at most 32 bits, convert
6194 to a signed 32 bit number and don't use 64bit
6195 displacement. */
6196 op_disp = (op_disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
6197 i.types[op].bitfield.disp64 = 0;
6198 i.types[op].bitfield.disp32 = 1;
6199 }
28a167a4 6200
a50187b2
JB
6201 if (flag_code == CODE_64BIT && fits_in_signed_long (op_disp))
6202 {
6203 i.types[op].bitfield.disp64 = 0;
a775efc8 6204 i.types[op].bitfield.disp32 = 1;
b300c311 6205 }
28a167a4 6206#endif
40fb9820 6207 if ((i.types[op].bitfield.disp32
40fb9820 6208 || i.types[op].bitfield.disp16)
b5014f7a 6209 && fits_in_disp8 (op_disp))
40fb9820 6210 i.types[op].bitfield.disp8 = 1;
77c59789
JB
6211
6212 i.op[op].disps->X_add_number = op_disp;
252b5132 6213 }
67a4f2b7
AO
6214 else if (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
6215 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL)
6216 {
6217 fix_new_exp (frag_now, frag_more (0) - frag_now->fr_literal, 0,
6218 i.op[op].disps, 0, i.reloc[op]);
2f2be86b 6219 i.types[op] = operand_type_and_not (i.types[op], anydisp);
67a4f2b7
AO
6220 }
6221 else
b300c311 6222 /* We only support 64bit displacement on constants. */
40fb9820 6223 i.types[op].bitfield.disp64 = 0;
252b5132 6224 }
0de704b9
JB
6225
6226 return true;
29b0f896
AM
6227}
6228
4a1b91ea
L
6229/* Return 1 if there is a match in broadcast bytes between operand
6230 GIVEN and instruction template T. */
6231
6232static INLINE int
6233match_broadcast_size (const insn_template *t, unsigned int given)
6234{
6235 return ((t->opcode_modifier.broadcast == BYTE_BROADCAST
6236 && i.types[given].bitfield.byte)
6237 || (t->opcode_modifier.broadcast == WORD_BROADCAST
6238 && i.types[given].bitfield.word)
6239 || (t->opcode_modifier.broadcast == DWORD_BROADCAST
6240 && i.types[given].bitfield.dword)
6241 || (t->opcode_modifier.broadcast == QWORD_BROADCAST
6242 && i.types[given].bitfield.qword));
6243}
6244
6c30d220
L
6245/* Check if operands are valid for the instruction. */
6246
6247static int
6248check_VecOperands (const insn_template *t)
6249{
43234a1e 6250 unsigned int op;
e2195274 6251 i386_cpu_flags cpu;
e2195274
JB
6252
6253 /* Templates allowing for ZMMword as well as YMMword and/or XMMword for
6254 any one operand are implicity requiring AVX512VL support if the actual
6255 operand size is YMMword or XMMword. Since this function runs after
6256 template matching, there's no need to check for YMMword/XMMword in
6257 the template. */
6258 cpu = cpu_flags_and (t->cpu_flags, avx512);
6259 if (!cpu_flags_all_zero (&cpu)
6260 && !t->cpu_flags.bitfield.cpuavx512vl
6261 && !cpu_arch_flags.bitfield.cpuavx512vl)
6262 {
6263 for (op = 0; op < t->operands; ++op)
6264 {
6265 if (t->operand_types[op].bitfield.zmmword
6266 && (i.types[op].bitfield.ymmword
6267 || i.types[op].bitfield.xmmword))
6268 {
6269 i.error = unsupported;
6270 return 1;
6271 }
6272 }
6273 }
43234a1e 6274
22c36940
JB
6275 /* Somewhat similarly, templates specifying both AVX and AVX2 are
6276 requiring AVX2 support if the actual operand size is YMMword. */
6277 if (t->cpu_flags.bitfield.cpuavx
6278 && t->cpu_flags.bitfield.cpuavx2
6279 && !cpu_arch_flags.bitfield.cpuavx2)
6280 {
6281 for (op = 0; op < t->operands; ++op)
6282 {
6283 if (t->operand_types[op].bitfield.xmmword
6284 && i.types[op].bitfield.ymmword)
6285 {
6286 i.error = unsupported;
6287 return 1;
6288 }
6289 }
6290 }
6291
6c30d220 6292 /* Without VSIB byte, we can't have a vector register for index. */
63112cd6 6293 if (!t->opcode_modifier.sib
6c30d220 6294 && i.index_reg
1b54b8d7
JB
6295 && (i.index_reg->reg_type.bitfield.xmmword
6296 || i.index_reg->reg_type.bitfield.ymmword
6297 || i.index_reg->reg_type.bitfield.zmmword))
6c30d220
L
6298 {
6299 i.error = unsupported_vector_index_register;
6300 return 1;
6301 }
6302
ad8ecc81 6303 /* Check if default mask is allowed. */
255571cd 6304 if (t->opcode_modifier.operandconstraint == NO_DEFAULT_MASK
6225c532 6305 && (!i.mask.reg || i.mask.reg->reg_num == 0))
ad8ecc81
MZ
6306 {
6307 i.error = no_default_mask;
6308 return 1;
6309 }
6310
7bab8ab5
JB
6311 /* For VSIB byte, we need a vector register for index, and all vector
6312 registers must be distinct. */
260cd341 6313 if (t->opcode_modifier.sib && t->opcode_modifier.sib != SIBMEM)
7bab8ab5
JB
6314 {
6315 if (!i.index_reg
63112cd6 6316 || !((t->opcode_modifier.sib == VECSIB128
1b54b8d7 6317 && i.index_reg->reg_type.bitfield.xmmword)
63112cd6 6318 || (t->opcode_modifier.sib == VECSIB256
1b54b8d7 6319 && i.index_reg->reg_type.bitfield.ymmword)
63112cd6 6320 || (t->opcode_modifier.sib == VECSIB512
1b54b8d7 6321 && i.index_reg->reg_type.bitfield.zmmword)))
7bab8ab5
JB
6322 {
6323 i.error = invalid_vsib_address;
6324 return 1;
6325 }
6326
6225c532
JB
6327 gas_assert (i.reg_operands == 2 || i.mask.reg);
6328 if (i.reg_operands == 2 && !i.mask.reg)
43234a1e 6329 {
3528c362 6330 gas_assert (i.types[0].bitfield.class == RegSIMD);
1b54b8d7
JB
6331 gas_assert (i.types[0].bitfield.xmmword
6332 || i.types[0].bitfield.ymmword);
3528c362 6333 gas_assert (i.types[2].bitfield.class == RegSIMD);
1b54b8d7
JB
6334 gas_assert (i.types[2].bitfield.xmmword
6335 || i.types[2].bitfield.ymmword);
43234a1e
L
6336 if (operand_check == check_none)
6337 return 0;
6338 if (register_number (i.op[0].regs)
6339 != register_number (i.index_reg)
6340 && register_number (i.op[2].regs)
6341 != register_number (i.index_reg)
6342 && register_number (i.op[0].regs)
6343 != register_number (i.op[2].regs))
6344 return 0;
6345 if (operand_check == check_error)
6346 {
6347 i.error = invalid_vector_register_set;
6348 return 1;
6349 }
6350 as_warn (_("mask, index, and destination registers should be distinct"));
6351 }
6225c532 6352 else if (i.reg_operands == 1 && i.mask.reg)
8444f82a 6353 {
3528c362 6354 if (i.types[1].bitfield.class == RegSIMD
1b54b8d7
JB
6355 && (i.types[1].bitfield.xmmword
6356 || i.types[1].bitfield.ymmword
6357 || i.types[1].bitfield.zmmword)
8444f82a
MZ
6358 && (register_number (i.op[1].regs)
6359 == register_number (i.index_reg)))
6360 {
6361 if (operand_check == check_error)
6362 {
6363 i.error = invalid_vector_register_set;
6364 return 1;
6365 }
6366 if (operand_check != check_none)
6367 as_warn (_("index and destination registers should be distinct"));
6368 }
6369 }
43234a1e 6370 }
7bab8ab5 6371
fc141319
L
6372 /* For AMX instructions with 3 TMM register operands, all operands
6373 must be distinct. */
6374 if (i.reg_operands == 3
6375 && t->operand_types[0].bitfield.tmmword
6376 && (i.op[0].regs == i.op[1].regs
6377 || i.op[0].regs == i.op[2].regs
6378 || i.op[1].regs == i.op[2].regs))
6379 {
6380 i.error = invalid_tmm_register_set;
6381 return 1;
260cd341
LC
6382 }
6383
0cc78721
CL
6384 /* For some special instructions require that destination must be distinct
6385 from source registers. */
255571cd 6386 if (t->opcode_modifier.operandconstraint == DISTINCT_DEST)
0cc78721
CL
6387 {
6388 unsigned int dest_reg = i.operands - 1;
6389
6390 know (i.operands >= 3);
6391
6392 /* #UD if dest_reg == src1_reg or dest_reg == src2_reg. */
6393 if (i.op[dest_reg - 1].regs == i.op[dest_reg].regs
6394 || (i.reg_operands > 2
6395 && i.op[dest_reg - 2].regs == i.op[dest_reg].regs))
6396 {
6397 i.error = invalid_dest_and_src_register_set;
6398 return 1;
6399 }
6400 }
6401
43234a1e
L
6402 /* Check if broadcast is supported by the instruction and is applied
6403 to the memory operand. */
a5748e0d 6404 if (i.broadcast.type || i.broadcast.bytes)
43234a1e 6405 {
8e6e0792 6406 i386_operand_type type, overlap;
43234a1e
L
6407
6408 /* Check if specified broadcast is supported in this instruction,
4a1b91ea 6409 and its broadcast bytes match the memory operand. */
5273a3cd 6410 op = i.broadcast.operand;
8e6e0792 6411 if (!t->opcode_modifier.broadcast
c48dadc9 6412 || !(i.flags[op] & Operand_Mem)
c39e5b26 6413 || (!i.types[op].bitfield.unspecified
4a1b91ea 6414 && !match_broadcast_size (t, op)))
43234a1e
L
6415 {
6416 bad_broadcast:
6417 i.error = unsupported_broadcast;
6418 return 1;
6419 }
8e6e0792
JB
6420
6421 operand_type_set (&type, 0);
a5748e0d 6422 switch (get_broadcast_bytes (t, false))
8e6e0792 6423 {
4a1b91ea
L
6424 case 2:
6425 type.bitfield.word = 1;
6426 break;
6427 case 4:
6428 type.bitfield.dword = 1;
6429 break;
8e6e0792
JB
6430 case 8:
6431 type.bitfield.qword = 1;
6432 break;
6433 case 16:
6434 type.bitfield.xmmword = 1;
6435 break;
6436 case 32:
6437 type.bitfield.ymmword = 1;
6438 break;
6439 case 64:
6440 type.bitfield.zmmword = 1;
6441 break;
6442 default:
6443 goto bad_broadcast;
6444 }
6445
6446 overlap = operand_type_and (type, t->operand_types[op]);
bc49bfd8
JB
6447 if (t->operand_types[op].bitfield.class == RegSIMD
6448 && t->operand_types[op].bitfield.byte
6449 + t->operand_types[op].bitfield.word
6450 + t->operand_types[op].bitfield.dword
6451 + t->operand_types[op].bitfield.qword > 1)
6452 {
6453 overlap.bitfield.xmmword = 0;
6454 overlap.bitfield.ymmword = 0;
6455 overlap.bitfield.zmmword = 0;
6456 }
8e6e0792
JB
6457 if (operand_type_all_zero (&overlap))
6458 goto bad_broadcast;
6459
9c19e9ec 6460 if (t->opcode_modifier.checkoperandsize)
8e6e0792
JB
6461 {
6462 unsigned int j;
6463
e2195274 6464 type.bitfield.baseindex = 1;
8e6e0792
JB
6465 for (j = 0; j < i.operands; ++j)
6466 {
6467 if (j != op
6468 && !operand_type_register_match(i.types[j],
6469 t->operand_types[j],
6470 type,
6471 t->operand_types[op]))
6472 goto bad_broadcast;
6473 }
6474 }
43234a1e
L
6475 }
6476 /* If broadcast is supported in this instruction, we need to check if
6477 operand of one-element size isn't specified without broadcast. */
6478 else if (t->opcode_modifier.broadcast && i.mem_operands)
6479 {
6480 /* Find memory operand. */
6481 for (op = 0; op < i.operands; op++)
8dc0818e 6482 if (i.flags[op] & Operand_Mem)
43234a1e
L
6483 break;
6484 gas_assert (op < i.operands);
6485 /* Check size of the memory operand. */
4a1b91ea 6486 if (match_broadcast_size (t, op))
43234a1e
L
6487 {
6488 i.error = broadcast_needed;
6489 return 1;
6490 }
6491 }
c39e5b26
JB
6492 else
6493 op = MAX_OPERANDS - 1; /* Avoid uninitialized variable warning. */
43234a1e
L
6494
6495 /* Check if requested masking is supported. */
6225c532 6496 if (i.mask.reg)
43234a1e 6497 {
ae2387fe
JB
6498 switch (t->opcode_modifier.masking)
6499 {
6500 case BOTH_MASKING:
6501 break;
6502 case MERGING_MASKING:
6225c532 6503 if (i.mask.zeroing)
ae2387fe
JB
6504 {
6505 case 0:
6506 i.error = unsupported_masking;
6507 return 1;
6508 }
6509 break;
6510 case DYNAMIC_MASKING:
6511 /* Memory destinations allow only merging masking. */
6225c532 6512 if (i.mask.zeroing && i.mem_operands)
ae2387fe
JB
6513 {
6514 /* Find memory operand. */
6515 for (op = 0; op < i.operands; op++)
c48dadc9 6516 if (i.flags[op] & Operand_Mem)
ae2387fe
JB
6517 break;
6518 gas_assert (op < i.operands);
6519 if (op == i.operands - 1)
6520 {
6521 i.error = unsupported_masking;
6522 return 1;
6523 }
6524 }
6525 break;
6526 default:
6527 abort ();
6528 }
43234a1e
L
6529 }
6530
6531 /* Check if masking is applied to dest operand. */
6225c532 6532 if (i.mask.reg && (i.mask.operand != i.operands - 1))
43234a1e
L
6533 {
6534 i.error = mask_not_on_destination;
6535 return 1;
6536 }
6537
43234a1e 6538 /* Check RC/SAE. */
ca5312a2 6539 if (i.rounding.type != rc_none)
43234a1e 6540 {
a80195f1 6541 if (!t->opcode_modifier.sae
cf665fee
JB
6542 || ((i.rounding.type != saeonly) != t->opcode_modifier.staticrounding)
6543 || i.mem_operands)
43234a1e
L
6544 {
6545 i.error = unsupported_rc_sae;
6546 return 1;
6547 }
cf665fee
JB
6548
6549 /* Non-EVEX.LIG forms need to have a ZMM register as at least one
6550 operand. */
6551 if (t->opcode_modifier.evex != EVEXLIG)
7bab8ab5 6552 {
cf665fee
JB
6553 for (op = 0; op < t->operands; ++op)
6554 if (i.types[op].bitfield.zmmword)
6555 break;
6556 if (op >= t->operands)
6557 {
6558 i.error = operand_size_mismatch;
6559 return 1;
6560 }
7bab8ab5 6561 }
6c30d220
L
6562 }
6563
da4977e0
JB
6564 /* Check the special Imm4 cases; must be the first operand. */
6565 if (t->cpu_flags.bitfield.cpuxop && t->operands == 5)
6566 {
6567 if (i.op[0].imms->X_op != O_constant
6568 || !fits_in_imm4 (i.op[0].imms->X_add_number))
6569 {
6570 i.error = bad_imm4;
6571 return 1;
6572 }
6573
6574 /* Turn off Imm<N> so that update_imm won't complain. */
6575 operand_type_set (&i.types[0], 0);
6576 }
6577
43234a1e 6578 /* Check vector Disp8 operand. */
b5014f7a 6579 if (t->opcode_modifier.disp8memshift
1a42a9fe 6580 && i.disp_encoding <= disp_encoding_8bit)
43234a1e 6581 {
9b345ce8 6582 if (i.broadcast.type || i.broadcast.bytes)
4a1b91ea 6583 i.memshift = t->opcode_modifier.broadcast - 1;
7091c612 6584 else if (t->opcode_modifier.disp8memshift != DISP8_SHIFT_VL)
43234a1e 6585 i.memshift = t->opcode_modifier.disp8memshift;
7091c612
JB
6586 else
6587 {
125ff819 6588 const i386_operand_type *type = NULL, *fallback = NULL;
7091c612
JB
6589
6590 i.memshift = 0;
6591 for (op = 0; op < i.operands; op++)
8dc0818e 6592 if (i.flags[op] & Operand_Mem)
7091c612 6593 {
4174bfff
JB
6594 if (t->opcode_modifier.evex == EVEXLIG)
6595 i.memshift = 2 + (i.suffix == QWORD_MNEM_SUFFIX);
6596 else if (t->operand_types[op].bitfield.xmmword
6597 + t->operand_types[op].bitfield.ymmword
6598 + t->operand_types[op].bitfield.zmmword <= 1)
7091c612
JB
6599 type = &t->operand_types[op];
6600 else if (!i.types[op].bitfield.unspecified)
6601 type = &i.types[op];
125ff819
JB
6602 else /* Ambiguities get resolved elsewhere. */
6603 fallback = &t->operand_types[op];
7091c612 6604 }
3528c362 6605 else if (i.types[op].bitfield.class == RegSIMD
4174bfff 6606 && t->opcode_modifier.evex != EVEXLIG)
7091c612
JB
6607 {
6608 if (i.types[op].bitfield.zmmword)
6609 i.memshift = 6;
6610 else if (i.types[op].bitfield.ymmword && i.memshift < 5)
6611 i.memshift = 5;
6612 else if (i.types[op].bitfield.xmmword && i.memshift < 4)
6613 i.memshift = 4;
6614 }
6615
125ff819
JB
6616 if (!type && !i.memshift)
6617 type = fallback;
7091c612
JB
6618 if (type)
6619 {
6620 if (type->bitfield.zmmword)
6621 i.memshift = 6;
6622 else if (type->bitfield.ymmword)
6623 i.memshift = 5;
6624 else if (type->bitfield.xmmword)
6625 i.memshift = 4;
6626 }
6627
6628 /* For the check in fits_in_disp8(). */
6629 if (i.memshift == 0)
6630 i.memshift = -1;
6631 }
43234a1e
L
6632
6633 for (op = 0; op < i.operands; op++)
6634 if (operand_type_check (i.types[op], disp)
6635 && i.op[op].disps->X_op == O_constant)
6636 {
b5014f7a 6637 if (fits_in_disp8 (i.op[op].disps->X_add_number))
43234a1e 6638 {
b5014f7a
JB
6639 i.types[op].bitfield.disp8 = 1;
6640 return 0;
43234a1e 6641 }
b5014f7a 6642 i.types[op].bitfield.disp8 = 0;
43234a1e
L
6643 }
6644 }
b5014f7a
JB
6645
6646 i.memshift = 0;
43234a1e 6647
6c30d220
L
6648 return 0;
6649}
6650
da4977e0 6651/* Check if encoding requirements are met by the instruction. */
a683cc34
SP
6652
6653static int
da4977e0 6654VEX_check_encoding (const insn_template *t)
a683cc34 6655{
da4977e0
JB
6656 if (i.vec_encoding == vex_encoding_error)
6657 {
6658 i.error = unsupported;
6659 return 1;
6660 }
6661
86fa6981 6662 if (i.vec_encoding == vex_encoding_evex)
43234a1e 6663 {
86fa6981 6664 /* This instruction must be encoded with EVEX prefix. */
e771e7c9 6665 if (!is_evex_encoding (t))
86fa6981
L
6666 {
6667 i.error = unsupported;
6668 return 1;
6669 }
6670 return 0;
43234a1e
L
6671 }
6672
a683cc34 6673 if (!t->opcode_modifier.vex)
86fa6981
L
6674 {
6675 /* This instruction template doesn't have VEX prefix. */
6676 if (i.vec_encoding != vex_encoding_default)
6677 {
6678 i.error = unsupported;
6679 return 1;
6680 }
6681 return 0;
6682 }
a683cc34 6683
a683cc34
SP
6684 return 0;
6685}
6686
7b94647a
JB
6687/* Helper function for the progress() macro in match_template(). */
6688static INLINE enum i386_error progress (enum i386_error new,
6689 enum i386_error last,
6690 unsigned int line, unsigned int *line_p)
6691{
6692 if (line <= *line_p)
6693 return last;
6694 *line_p = line;
6695 return new;
6696}
6697
d3ce72d0 6698static const insn_template *
83b16ac6 6699match_template (char mnem_suffix)
29b0f896
AM
6700{
6701 /* Points to template once we've found it. */
d3ce72d0 6702 const insn_template *t;
40fb9820 6703 i386_operand_type overlap0, overlap1, overlap2, overlap3;
c0f3af97 6704 i386_operand_type overlap4;
29b0f896 6705 unsigned int found_reverse_match;
40fb9820 6706 i386_operand_type operand_types [MAX_OPERANDS];
539e75ad 6707 int addr_prefix_disp;
7b94647a
JB
6708 unsigned int j, size_match, check_register, errline = __LINE__;
6709 enum i386_error specific_error = number_of_operands_mismatch;
6710#define progress(err) progress (err, specific_error, __LINE__, &errline)
29b0f896 6711
c0f3af97
L
6712#if MAX_OPERANDS != 5
6713# error "MAX_OPERANDS must be 5."
f48ff2ae
L
6714#endif
6715
29b0f896 6716 found_reverse_match = 0;
539e75ad 6717 addr_prefix_disp = -1;
40fb9820 6718
45aa61fe 6719 for (t = current_templates->start; t < current_templates->end; t++)
29b0f896 6720 {
539e75ad 6721 addr_prefix_disp = -1;
dbbc8b7e 6722 found_reverse_match = 0;
539e75ad 6723
7b94647a 6724 /* Must have right number of operands. */
29b0f896
AM
6725 if (i.operands != t->operands)
6726 continue;
6727
50aecf8c 6728 /* Check processor support. */
7b94647a 6729 specific_error = progress (unsupported);
45a4bb20 6730 if (cpu_flags_match (t) != CPU_FLAGS_PERFECT_MATCH)
50aecf8c
L
6731 continue;
6732
e1d4d893 6733 /* Check AT&T mnemonic. */
7b94647a 6734 specific_error = progress (unsupported_with_intel_mnemonic);
e1d4d893 6735 if (intel_mnemonic && t->opcode_modifier.attmnemonic)
1efbbeb4
L
6736 continue;
6737
4b5aaf5f 6738 /* Check AT&T/Intel syntax. */
7b94647a 6739 specific_error = progress (unsupported_syntax);
5c07affc 6740 if ((intel_syntax && t->opcode_modifier.attsyntax)
4b5aaf5f 6741 || (!intel_syntax && t->opcode_modifier.intelsyntax))
1efbbeb4
L
6742 continue;
6743
4b5aaf5f
L
6744 /* Check Intel64/AMD64 ISA. */
6745 switch (isa64)
6746 {
6747 default:
6748 /* Default: Don't accept Intel64. */
6749 if (t->opcode_modifier.isa64 == INTEL64)
6750 continue;
6751 break;
6752 case amd64:
6753 /* -mamd64: Don't accept Intel64 and Intel64 only. */
6754 if (t->opcode_modifier.isa64 >= INTEL64)
6755 continue;
6756 break;
6757 case intel64:
6758 /* -mintel64: Don't accept AMD64. */
5990e377 6759 if (t->opcode_modifier.isa64 == AMD64 && flag_code == CODE_64BIT)
4b5aaf5f
L
6760 continue;
6761 break;
6762 }
6763
dc2be329 6764 /* Check the suffix. */
7b94647a 6765 specific_error = progress (invalid_instruction_suffix);
7505bb03
JB
6766 if ((t->opcode_modifier.no_bsuf && mnem_suffix == BYTE_MNEM_SUFFIX)
6767 || (t->opcode_modifier.no_wsuf && mnem_suffix == WORD_MNEM_SUFFIX)
6768 || (t->opcode_modifier.no_lsuf && mnem_suffix == LONG_MNEM_SUFFIX)
6769 || (t->opcode_modifier.no_ssuf && mnem_suffix == SHORT_MNEM_SUFFIX)
6770 || (t->opcode_modifier.no_qsuf && mnem_suffix == QWORD_MNEM_SUFFIX))
83b16ac6 6771 continue;
29b0f896 6772
7b94647a 6773 specific_error = progress (operand_size_mismatch);
3ac21baa
JB
6774 size_match = operand_size_match (t);
6775 if (!size_match)
7d5e4556 6776 continue;
539e75ad 6777
6f2f06be
JB
6778 /* This is intentionally not
6779
0cfa3eb3 6780 if (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE))
6f2f06be
JB
6781
6782 as the case of a missing * on the operand is accepted (perhaps with
6783 a warning, issued further down). */
7b94647a 6784 specific_error = progress (operand_type_mismatch);
0cfa3eb3 6785 if (i.jumpabsolute && t->opcode_modifier.jump != JUMP_ABSOLUTE)
7b94647a 6786 continue;
6f2f06be 6787
a4d3acd2
JB
6788 /* In Intel syntax, normally we can check for memory operand size when
6789 there is no mnemonic suffix. But jmp and call have 2 different
6790 encodings with Dword memory operand size. Skip the "near" one
6791 (permitting a register operand) when "far" was requested. */
6792 if (i.far_branch
6793 && t->opcode_modifier.jump == JUMP_ABSOLUTE
6794 && t->operand_types[0].bitfield.class == Reg)
6795 continue;
6796
5c07affc
L
6797 for (j = 0; j < MAX_OPERANDS; j++)
6798 operand_types[j] = t->operand_types[j];
6799
9db83a32 6800 /* In general, don't allow 32-bit operands on pre-386. */
7b94647a
JB
6801 specific_error = progress (mnem_suffix ? invalid_instruction_suffix
6802 : operand_size_mismatch);
4873e243 6803 j = i.imm_operands + (t->operands > i.imm_operands + 1);
9db83a32
JB
6804 if (i.suffix == LONG_MNEM_SUFFIX
6805 && !cpu_arch_flags.bitfield.cpui386
45aa61fe 6806 && (intel_syntax
3cd7f3e3 6807 ? (t->opcode_modifier.mnemonicsize != IGNORESIZE
76d3f746
JB
6808 && !intel_float_operand (insn_name (t)))
6809 : intel_float_operand (insn_name (t)) != 2)
4873e243
JB
6810 && (t->operands == i.imm_operands
6811 || (operand_types[i.imm_operands].bitfield.class != RegMMX
6812 && operand_types[i.imm_operands].bitfield.class != RegSIMD
6813 && operand_types[i.imm_operands].bitfield.class != RegMask)
6814 || (operand_types[j].bitfield.class != RegMMX
6815 && operand_types[j].bitfield.class != RegSIMD
6816 && operand_types[j].bitfield.class != RegMask))
63112cd6 6817 && !t->opcode_modifier.sib)
192dc9c6
JB
6818 continue;
6819
29b0f896 6820 /* Do not verify operands when there are none. */
e365e234 6821 if (!t->operands)
da4977e0
JB
6822 {
6823 if (VEX_check_encoding (t))
6824 {
7b94647a 6825 specific_error = progress (i.error);
da4977e0
JB
6826 continue;
6827 }
6828
6829 /* We've found a match; break out of loop. */
6830 break;
6831 }
252b5132 6832
48bcea9f
JB
6833 if (!t->opcode_modifier.jump
6834 || t->opcode_modifier.jump == JUMP_ABSOLUTE)
6835 {
6836 /* There should be only one Disp operand. */
6837 for (j = 0; j < MAX_OPERANDS; j++)
6838 if (operand_type_check (operand_types[j], disp))
539e75ad 6839 break;
48bcea9f
JB
6840 if (j < MAX_OPERANDS)
6841 {
5b7c81bd 6842 bool override = (i.prefix[ADDR_PREFIX] != 0);
48bcea9f
JB
6843
6844 addr_prefix_disp = j;
6845
a775efc8
JB
6846 /* Address size prefix will turn Disp64 operand into Disp32 and
6847 Disp32/Disp16 one into Disp16/Disp32 respectively. */
48bcea9f 6848 switch (flag_code)
40fb9820 6849 {
48bcea9f
JB
6850 case CODE_16BIT:
6851 override = !override;
6852 /* Fall through. */
6853 case CODE_32BIT:
6854 if (operand_types[j].bitfield.disp32
6855 && operand_types[j].bitfield.disp16)
40fb9820 6856 {
48bcea9f
JB
6857 operand_types[j].bitfield.disp16 = override;
6858 operand_types[j].bitfield.disp32 = !override;
40fb9820 6859 }
a775efc8 6860 gas_assert (!operand_types[j].bitfield.disp64);
48bcea9f
JB
6861 break;
6862
6863 case CODE_64BIT:
a775efc8 6864 if (operand_types[j].bitfield.disp64)
40fb9820 6865 {
a775efc8 6866 gas_assert (!operand_types[j].bitfield.disp32);
48bcea9f 6867 operand_types[j].bitfield.disp32 = override;
a775efc8 6868 operand_types[j].bitfield.disp64 = !override;
40fb9820 6869 }
48bcea9f
JB
6870 operand_types[j].bitfield.disp16 = 0;
6871 break;
40fb9820 6872 }
539e75ad 6873 }
48bcea9f 6874 }
539e75ad 6875
56ffb741 6876 /* We check register size if needed. */
9c19e9ec 6877 if (t->opcode_modifier.checkoperandsize)
e2195274
JB
6878 {
6879 check_register = (1 << t->operands) - 1;
a5748e0d 6880 if (i.broadcast.type || i.broadcast.bytes)
5273a3cd 6881 check_register &= ~(1 << i.broadcast.operand);
e2195274
JB
6882 }
6883 else
6884 check_register = 0;
6885
c6fb90c8 6886 overlap0 = operand_type_and (i.types[0], operand_types[0]);
29b0f896
AM
6887 switch (t->operands)
6888 {
6889 case 1:
40fb9820 6890 if (!operand_type_match (overlap0, i.types[0]))
29b0f896 6891 continue;
ae9a0a51
JB
6892
6893 /* Allow the ModR/M encoding to be requested by using the {load} or
6894 {store} pseudo prefix on an applicable insn. */
6895 if (!t->opcode_modifier.modrm
6896 && i.reg_operands == 1
6897 && ((i.dir_encoding == dir_encoding_load
6898 && t->mnem_off != MN_pop)
6899 || (i.dir_encoding == dir_encoding_store
6900 && t->mnem_off != MN_push))
6901 /* Avoid BSWAP. */
6902 && t->mnem_off != MN_bswap)
6903 continue;
29b0f896 6904 break;
ae9a0a51 6905
29b0f896 6906 case 2:
33eaf5de 6907 /* xchg %eax, %eax is a special case. It is an alias for nop
8b38ad71
L
6908 only in 32bit mode and we can use opcode 0x90. In 64bit
6909 mode, we can't use 0x90 for xchg %eax, %eax since it should
6910 zero-extend %eax to %rax. */
ae9a0a51
JB
6911 if (t->base_opcode == 0x90
6912 && t->opcode_space == SPACE_BASE)
6913 {
6914 if (flag_code == CODE_64BIT
6915 && i.types[0].bitfield.instance == Accum
6916 && i.types[0].bitfield.dword
6917 && i.types[1].bitfield.instance == Accum)
6918 continue;
6919
6920 /* Allow the ModR/M encoding to be requested by using the
6921 {load} or {store} pseudo prefix. */
6922 if (i.dir_encoding == dir_encoding_load
6923 || i.dir_encoding == dir_encoding_store)
6924 continue;
6925 }
e3669c7f
JB
6926
6927 if (t->base_opcode == MOV_AX_DISP32
ddb62495 6928 && t->opcode_space == SPACE_BASE
69196391 6929 && t->mnem_off != MN_movabs)
e3669c7f
JB
6930 {
6931 /* Force 0x8b encoding for "mov foo@GOT, %eax". */
6932 if (i.reloc[0] == BFD_RELOC_386_GOT32)
6933 continue;
6934
6935 /* xrelease mov %eax, <disp> is another special case. It must not
6936 match the accumulator-only encoding of mov. */
6937 if (i.hle_prefix)
6938 continue;
ae9a0a51
JB
6939
6940 /* Allow the ModR/M encoding to be requested by using a suitable
6941 {load} or {store} pseudo prefix. */
6942 if (i.dir_encoding == (i.types[0].bitfield.instance == Accum
6943 ? dir_encoding_store
6944 : dir_encoding_load)
6945 && !i.types[0].bitfield.disp64
6946 && !i.types[1].bitfield.disp64)
6947 continue;
6948 }
6949
6950 /* Allow the ModR/M encoding to be requested by using the {load} or
6951 {store} pseudo prefix on an applicable insn. */
6952 if (!t->opcode_modifier.modrm
6953 && i.reg_operands == 1
6954 && i.imm_operands == 1
6955 && (i.dir_encoding == dir_encoding_load
6956 || i.dir_encoding == dir_encoding_store)
6957 && t->opcode_space == SPACE_BASE)
6958 {
6959 if (t->base_opcode == 0xb0 /* mov $imm, %reg */
6960 && i.dir_encoding == dir_encoding_store)
6961 continue;
6962
6963 if ((t->base_opcode | 0x38) == 0x3c /* <alu> $imm, %acc */
6964 && (t->base_opcode != 0x3c /* cmp $imm, %acc */
6965 || i.dir_encoding == dir_encoding_load))
6966 continue;
6967
6968 if (t->base_opcode == 0xa8 /* test $imm, %acc */
6969 && i.dir_encoding == dir_encoding_load)
6970 continue;
e3669c7f 6971 }
f5eb1d70
JB
6972 /* Fall through. */
6973
6974 case 3:
3ac21baa
JB
6975 if (!(size_match & MATCH_STRAIGHT))
6976 goto check_reverse;
64c49ab3
JB
6977 /* Reverse direction of operands if swapping is possible in the first
6978 place (operands need to be symmetric) and
6979 - the load form is requested, and the template is a store form,
6980 - the store form is requested, and the template is a load form,
6981 - the non-default (swapped) form is requested. */
6982 overlap1 = operand_type_and (operand_types[0], operand_types[1]);
f5eb1d70 6983 if (t->opcode_modifier.d && i.reg_operands == i.operands
64c49ab3
JB
6984 && !operand_type_all_zero (&overlap1))
6985 switch (i.dir_encoding)
6986 {
6987 case dir_encoding_load:
6988 if (operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 6989 || t->opcode_modifier.regmem)
64c49ab3
JB
6990 goto check_reverse;
6991 break;
6992
6993 case dir_encoding_store:
6994 if (!operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 6995 && !t->opcode_modifier.regmem)
64c49ab3
JB
6996 goto check_reverse;
6997 break;
6998
6999 case dir_encoding_swap:
7000 goto check_reverse;
7001
7002 case dir_encoding_default:
7003 break;
7004 }
86fa6981 7005 /* If we want store form, we skip the current load. */
64c49ab3
JB
7006 if ((i.dir_encoding == dir_encoding_store
7007 || i.dir_encoding == dir_encoding_swap)
86fa6981
L
7008 && i.mem_operands == 0
7009 && t->opcode_modifier.load)
fa99fab2 7010 continue;
1a0670f3 7011 /* Fall through. */
f48ff2ae 7012 case 4:
c0f3af97 7013 case 5:
c6fb90c8 7014 overlap1 = operand_type_and (i.types[1], operand_types[1]);
40fb9820
L
7015 if (!operand_type_match (overlap0, i.types[0])
7016 || !operand_type_match (overlap1, i.types[1])
e2195274 7017 || ((check_register & 3) == 3
dc821c5f 7018 && !operand_type_register_match (i.types[0],
40fb9820 7019 operand_types[0],
dc821c5f 7020 i.types[1],
40fb9820 7021 operand_types[1])))
29b0f896 7022 {
7b94647a
JB
7023 specific_error = progress (i.error);
7024
29b0f896 7025 /* Check if other direction is valid ... */
38e314eb 7026 if (!t->opcode_modifier.d)
29b0f896
AM
7027 continue;
7028
dc1e8a47 7029 check_reverse:
3ac21baa
JB
7030 if (!(size_match & MATCH_REVERSE))
7031 continue;
29b0f896 7032 /* Try reversing direction of operands. */
aa180741 7033 j = t->cpu_flags.bitfield.cpufma4
5dab1799 7034 || t->cpu_flags.bitfield.cpuxop ? 1 : i.operands - 1;
8bd915b7
JB
7035 overlap0 = operand_type_and (i.types[0], operand_types[j]);
7036 overlap1 = operand_type_and (i.types[j], operand_types[0]);
c975cec5
JB
7037 overlap2 = operand_type_and (i.types[1], operand_types[1]);
7038 gas_assert (t->operands != 3 || !check_register);
40fb9820 7039 if (!operand_type_match (overlap0, i.types[0])
8bd915b7 7040 || !operand_type_match (overlap1, i.types[j])
c975cec5
JB
7041 || (t->operands == 3
7042 && !operand_type_match (overlap2, i.types[1]))
45664ddb 7043 || (check_register
dc821c5f 7044 && !operand_type_register_match (i.types[0],
8bd915b7
JB
7045 operand_types[j],
7046 i.types[j],
45664ddb 7047 operand_types[0])))
29b0f896
AM
7048 {
7049 /* Does not match either direction. */
7b94647a 7050 specific_error = progress (i.error);
29b0f896
AM
7051 continue;
7052 }
ac9226cf 7053 /* found_reverse_match holds which variant of D
29b0f896 7054 we've found. */
38e314eb
JB
7055 if (!t->opcode_modifier.d)
7056 found_reverse_match = 0;
7057 else if (operand_types[0].bitfield.tbyte)
ac9226cf 7058 {
4943d587
JB
7059 if (t->opcode_modifier.operandconstraint != UGH)
7060 found_reverse_match = Opcode_FloatD;
bd782808
JB
7061 else
7062 found_reverse_match = ~0;
ac9226cf 7063 /* FSUB{,R} and FDIV{,R} may need a 2nd bit flipped. */
bd782808 7064 if ((t->extension_opcode & 4)
ac9226cf
JB
7065 && (intel_syntax || intel_mnemonic))
7066 found_reverse_match |= Opcode_FloatR;
7067 }
aa180741 7068 else if (t->cpu_flags.bitfield.cpufma4
5dab1799 7069 || t->cpu_flags.bitfield.cpuxop)
8bd915b7
JB
7070 {
7071 found_reverse_match = Opcode_VexW;
7072 goto check_operands_345;
7073 }
ddb62495
JB
7074 else if (t->opcode_space != SPACE_BASE
7075 && (t->opcode_space != SPACE_0F
2c735193
JB
7076 /* MOV to/from CR/DR/TR, as an exception, follow
7077 the base opcode space encoding model. */
7078 || (t->base_opcode | 7) != 0x27))
dbbc8b7e 7079 found_reverse_match = (t->base_opcode & 0xee) != 0x6e
2c735193 7080 ? Opcode_ExtD : Opcode_SIMD_IntD;
a33ef3c2 7081 else if (!t->opcode_modifier.commutative)
38e314eb 7082 found_reverse_match = Opcode_D;
a33ef3c2
JB
7083 else
7084 found_reverse_match = ~0;
29b0f896 7085 }
f48ff2ae 7086 else
29b0f896 7087 {
f48ff2ae 7088 /* Found a forward 2 operand match here. */
8bd915b7 7089 check_operands_345:
d1cbb4db
L
7090 switch (t->operands)
7091 {
c0f3af97 7092 case 5:
3d0738af 7093 overlap4 = operand_type_and (i.types[4], operand_types[4]);
c0f3af97 7094 if (!operand_type_match (overlap4, i.types[4])
dc821c5f 7095 || !operand_type_register_match (i.types[3],
c0f3af97 7096 operand_types[3],
c0f3af97
L
7097 i.types[4],
7098 operand_types[4]))
7b94647a
JB
7099 {
7100 specific_error = progress (i.error);
7101 continue;
7102 }
1a0670f3 7103 /* Fall through. */
f48ff2ae 7104 case 4:
3d0738af 7105 overlap3 = operand_type_and (i.types[3], operand_types[3]);
40fb9820 7106 if (!operand_type_match (overlap3, i.types[3])
e2195274
JB
7107 || ((check_register & 0xa) == 0xa
7108 && !operand_type_register_match (i.types[1],
f7768225
JB
7109 operand_types[1],
7110 i.types[3],
e2195274
JB
7111 operand_types[3]))
7112 || ((check_register & 0xc) == 0xc
7113 && !operand_type_register_match (i.types[2],
7114 operand_types[2],
7115 i.types[3],
7116 operand_types[3])))
7b94647a
JB
7117 {
7118 specific_error = progress (i.error);
7119 continue;
7120 }
1a0670f3 7121 /* Fall through. */
f48ff2ae 7122 case 3:
3d0738af 7123 overlap2 = operand_type_and (i.types[2], operand_types[2]);
40fb9820 7124 if (!operand_type_match (overlap2, i.types[2])
e2195274
JB
7125 || ((check_register & 5) == 5
7126 && !operand_type_register_match (i.types[0],
23e42951
JB
7127 operand_types[0],
7128 i.types[2],
e2195274
JB
7129 operand_types[2]))
7130 || ((check_register & 6) == 6
7131 && !operand_type_register_match (i.types[1],
7132 operand_types[1],
7133 i.types[2],
7134 operand_types[2])))
7b94647a
JB
7135 {
7136 specific_error = progress (i.error);
7137 continue;
7138 }
f48ff2ae
L
7139 break;
7140 }
29b0f896 7141 }
f48ff2ae 7142 /* Found either forward/reverse 2, 3 or 4 operand match here:
29b0f896
AM
7143 slip through to break. */
7144 }
c0f3af97 7145
9bb4d860
L
7146 /* Check if VEX/EVEX encoding requirements can be satisfied. */
7147 if (VEX_check_encoding (t))
da4977e0 7148 {
7b94647a 7149 specific_error = progress (i.error);
da4977e0
JB
7150 continue;
7151 }
7152
9bb4d860
L
7153 /* Check if vector operands are valid. */
7154 if (check_VecOperands (t))
5614d22c 7155 {
7b94647a 7156 specific_error = progress (i.error);
5614d22c
JB
7157 continue;
7158 }
a683cc34 7159
29b0f896
AM
7160 /* We've found a match; break out of loop. */
7161 break;
7162 }
7163
7b94647a
JB
7164#undef progress
7165
29b0f896
AM
7166 if (t == current_templates->end)
7167 {
7168 /* We found no match. */
04784e33 7169 i.error = specific_error;
fa99fab2 7170 return NULL;
29b0f896 7171 }
252b5132 7172
29b0f896
AM
7173 if (!quiet_warnings)
7174 {
7175 if (!intel_syntax
0cfa3eb3 7176 && (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE)))
76d3f746 7177 as_warn (_("indirect %s without `*'"), insn_name (t));
29b0f896 7178
40fb9820 7179 if (t->opcode_modifier.isprefix
3cd7f3e3 7180 && t->opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
7181 {
7182 /* Warn them that a data or address size prefix doesn't
7183 affect assembly of the next line of code. */
76d3f746 7184 as_warn (_("stand-alone `%s' prefix"), insn_name (t));
29b0f896
AM
7185 }
7186 }
7187
7188 /* Copy the template we found. */
9a182d04 7189 install_template (t);
539e75ad
L
7190
7191 if (addr_prefix_disp != -1)
7192 i.tm.operand_types[addr_prefix_disp]
7193 = operand_types[addr_prefix_disp];
7194
8bd915b7 7195 switch (found_reverse_match)
29b0f896 7196 {
8bd915b7
JB
7197 case 0:
7198 break;
7199
bd782808
JB
7200 case Opcode_FloatR:
7201 case Opcode_FloatR | Opcode_FloatD:
7202 i.tm.extension_opcode ^= Opcode_FloatR >> 3;
7203 found_reverse_match &= Opcode_FloatD;
7204
7205 /* Fall through. */
8bd915b7 7206 default:
dfd69174
JB
7207 /* If we found a reverse match we must alter the opcode direction
7208 bit and clear/flip the regmem modifier one. found_reverse_match
7209 holds bits to change (different for int & float insns). */
29b0f896
AM
7210
7211 i.tm.base_opcode ^= found_reverse_match;
7212
dfd69174
JB
7213 /* Certain SIMD insns have their load forms specified in the opcode
7214 table, and hence we need to _set_ RegMem instead of clearing it.
7215 We need to avoid setting the bit though on insns like KMOVW. */
7216 i.tm.opcode_modifier.regmem
7217 = i.tm.opcode_modifier.modrm && i.tm.opcode_modifier.d
7218 && i.tm.operands > 2U - i.tm.opcode_modifier.sse2avx
7219 && !i.tm.opcode_modifier.regmem;
a33ef3c2
JB
7220
7221 /* Fall through. */
7222 case ~0:
7223 i.tm.operand_types[0] = operand_types[i.operands - 1];
7224 i.tm.operand_types[i.operands - 1] = operand_types[0];
8bd915b7
JB
7225 break;
7226
7227 case Opcode_VexW:
7228 /* Only the first two register operands need reversing, alongside
7229 flipping VEX.W. */
7230 i.tm.opcode_modifier.vexw ^= VEXW0 ^ VEXW1;
7231
7232 j = i.tm.operand_types[0].bitfield.imm8;
7233 i.tm.operand_types[j] = operand_types[j + 1];
7234 i.tm.operand_types[j + 1] = operand_types[j];
7235 break;
29b0f896
AM
7236 }
7237
fa99fab2 7238 return t;
29b0f896
AM
7239}
7240
7241static int
e3bb37b5 7242check_string (void)
29b0f896 7243{
51c8edf6
JB
7244 unsigned int es_op = i.tm.opcode_modifier.isstring - IS_STRING_ES_OP0;
7245 unsigned int op = i.tm.operand_types[0].bitfield.baseindex ? es_op : 0;
8dc0818e 7246
5e042380 7247 if (i.seg[op] != NULL && i.seg[op] != reg_es)
29b0f896 7248 {
51c8edf6 7249 as_bad (_("`%s' operand %u must use `%ses' segment"),
76d3f746 7250 insn_name (&i.tm),
51c8edf6
JB
7251 intel_syntax ? i.tm.operands - es_op : es_op + 1,
7252 register_prefix);
7253 return 0;
29b0f896 7254 }
51c8edf6
JB
7255
7256 /* There's only ever one segment override allowed per instruction.
7257 This instruction possibly has a legal segment override on the
7258 second operand, so copy the segment to where non-string
7259 instructions store it, allowing common code. */
7260 i.seg[op] = i.seg[1];
7261
29b0f896
AM
7262 return 1;
7263}
7264
7265static int
543613e9 7266process_suffix (void)
29b0f896 7267{
7fc69528 7268 bool is_movx = false;
8b65b895 7269
29b0f896
AM
7270 /* If matched instruction specifies an explicit instruction mnemonic
7271 suffix, use it. */
673fe0f0 7272 if (i.tm.opcode_modifier.size == SIZE16)
40fb9820 7273 i.suffix = WORD_MNEM_SUFFIX;
673fe0f0 7274 else if (i.tm.opcode_modifier.size == SIZE32)
40fb9820 7275 i.suffix = LONG_MNEM_SUFFIX;
673fe0f0 7276 else if (i.tm.opcode_modifier.size == SIZE64)
40fb9820 7277 i.suffix = QWORD_MNEM_SUFFIX;
13e600d0 7278 else if (i.reg_operands
c8f8eebc 7279 && (i.operands > 1 || i.types[0].bitfield.class == Reg)
255571cd 7280 && i.tm.opcode_modifier.operandconstraint != ADDR_PREFIX_OP_REG)
29b0f896 7281 {
65fca059 7282 unsigned int numop = i.operands;
389d00a5
JB
7283
7284 /* MOVSX/MOVZX */
ddb62495 7285 is_movx = (i.tm.opcode_space == SPACE_0F
389d00a5 7286 && (i.tm.base_opcode | 8) == 0xbe)
ddb62495 7287 || (i.tm.opcode_space == SPACE_BASE
389d00a5
JB
7288 && i.tm.base_opcode == 0x63
7289 && i.tm.cpu_flags.bitfield.cpu64);
7290
65fca059
JB
7291 /* movsx/movzx want only their source operand considered here, for the
7292 ambiguity checking below. The suffix will be replaced afterwards
7293 to represent the destination (register). */
389d00a5 7294 if (is_movx && (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63))
65fca059
JB
7295 --i.operands;
7296
643bb870 7297 /* crc32 needs REX.W set regardless of suffix / source operand size. */
7fc69528 7298 if (i.tm.mnem_off == MN_crc32 && i.tm.operand_types[1].bitfield.qword)
643bb870
JB
7299 i.rex |= REX_W;
7300
29b0f896 7301 /* If there's no instruction mnemonic suffix we try to invent one
13e600d0 7302 based on GPR operands. */
29b0f896
AM
7303 if (!i.suffix)
7304 {
7305 /* We take i.suffix from the last register operand specified,
7306 Destination register type is more significant than source
381d071f
L
7307 register type. crc32 in SSE4.2 prefers source register
7308 type. */
7fc69528 7309 unsigned int op = i.tm.mnem_off == MN_crc32 ? 1 : i.operands;
20592a94 7310
1a035124
JB
7311 while (op--)
7312 if (i.tm.operand_types[op].bitfield.instance == InstanceNone
7313 || i.tm.operand_types[op].bitfield.instance == Accum)
7314 {
7315 if (i.types[op].bitfield.class != Reg)
7316 continue;
7317 if (i.types[op].bitfield.byte)
7318 i.suffix = BYTE_MNEM_SUFFIX;
7319 else if (i.types[op].bitfield.word)
7320 i.suffix = WORD_MNEM_SUFFIX;
7321 else if (i.types[op].bitfield.dword)
7322 i.suffix = LONG_MNEM_SUFFIX;
7323 else if (i.types[op].bitfield.qword)
7324 i.suffix = QWORD_MNEM_SUFFIX;
7325 else
7326 continue;
7327 break;
7328 }
65fca059
JB
7329
7330 /* As an exception, movsx/movzx silently default to a byte source
7331 in AT&T mode. */
389d00a5 7332 if (is_movx && i.tm.opcode_modifier.w && !i.suffix && !intel_syntax)
65fca059 7333 i.suffix = BYTE_MNEM_SUFFIX;
29b0f896
AM
7334 }
7335 else if (i.suffix == BYTE_MNEM_SUFFIX)
7336 {
1cb0ab18 7337 if (!check_byte_reg ())
29b0f896
AM
7338 return 0;
7339 }
7340 else if (i.suffix == LONG_MNEM_SUFFIX)
7341 {
1cb0ab18 7342 if (!check_long_reg ())
29b0f896
AM
7343 return 0;
7344 }
7345 else if (i.suffix == QWORD_MNEM_SUFFIX)
7346 {
1cb0ab18 7347 if (!check_qword_reg ())
29b0f896
AM
7348 return 0;
7349 }
7350 else if (i.suffix == WORD_MNEM_SUFFIX)
7351 {
1cb0ab18 7352 if (!check_word_reg ())
29b0f896
AM
7353 return 0;
7354 }
3cd7f3e3
L
7355 else if (intel_syntax
7356 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
7357 /* Do nothing if the instruction is going to ignore the prefix. */
7358 ;
7359 else
7360 abort ();
65fca059
JB
7361
7362 /* Undo the movsx/movzx change done above. */
7363 i.operands = numop;
29b0f896 7364 }
3cd7f3e3
L
7365 else if (i.tm.opcode_modifier.mnemonicsize == DEFAULTSIZE
7366 && !i.suffix)
29b0f896 7367 {
13e600d0
JB
7368 i.suffix = stackop_size;
7369 if (stackop_size == LONG_MNEM_SUFFIX)
06f74c5c
L
7370 {
7371 /* stackop_size is set to LONG_MNEM_SUFFIX for the
7372 .code16gcc directive to support 16-bit mode with
7373 32-bit address. For IRET without a suffix, generate
7374 16-bit IRET (opcode 0xcf) to return from an interrupt
7375 handler. */
13e600d0
JB
7376 if (i.tm.base_opcode == 0xcf)
7377 {
7378 i.suffix = WORD_MNEM_SUFFIX;
7379 as_warn (_("generating 16-bit `iret' for .code16gcc directive"));
7380 }
7381 /* Warn about changed behavior for segment register push/pop. */
7382 else if ((i.tm.base_opcode | 1) == 0x07)
7383 as_warn (_("generating 32-bit `%s', unlike earlier gas versions"),
76d3f746 7384 insn_name (&i.tm));
06f74c5c 7385 }
29b0f896 7386 }
c006a730 7387 else if (!i.suffix
0cfa3eb3
JB
7388 && (i.tm.opcode_modifier.jump == JUMP_ABSOLUTE
7389 || i.tm.opcode_modifier.jump == JUMP_BYTE
7390 || i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT
ddb62495 7391 || (i.tm.opcode_space == SPACE_0F
389d00a5 7392 && i.tm.base_opcode == 0x01 /* [ls][gi]dt */
64e74474 7393 && i.tm.extension_opcode <= 3)))
9306ca4a
JB
7394 {
7395 switch (flag_code)
7396 {
7397 case CODE_64BIT:
40fb9820 7398 if (!i.tm.opcode_modifier.no_qsuf)
9306ca4a 7399 {
828c2a25
JB
7400 if (i.tm.opcode_modifier.jump == JUMP_BYTE
7401 || i.tm.opcode_modifier.no_lsuf)
7402 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a
JB
7403 break;
7404 }
1a0670f3 7405 /* Fall through. */
9306ca4a 7406 case CODE_32BIT:
40fb9820 7407 if (!i.tm.opcode_modifier.no_lsuf)
9306ca4a
JB
7408 i.suffix = LONG_MNEM_SUFFIX;
7409 break;
7410 case CODE_16BIT:
40fb9820 7411 if (!i.tm.opcode_modifier.no_wsuf)
9306ca4a
JB
7412 i.suffix = WORD_MNEM_SUFFIX;
7413 break;
7414 }
7415 }
252b5132 7416
c006a730 7417 if (!i.suffix
3cd7f3e3 7418 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8
JB
7419 /* Also cover lret/retf/iret in 64-bit mode. */
7420 || (flag_code == CODE_64BIT
7421 && !i.tm.opcode_modifier.no_lsuf
7422 && !i.tm.opcode_modifier.no_qsuf))
3cd7f3e3 7423 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
8bbb3ad8
JB
7424 /* Explicit sizing prefixes are assumed to disambiguate insns. */
7425 && !i.prefix[DATA_PREFIX] && !(i.prefix[REX_PREFIX] & REX_W)
62b3f548
JB
7426 /* Accept FLDENV et al without suffix. */
7427 && (i.tm.opcode_modifier.no_ssuf || i.tm.opcode_modifier.floatmf))
29b0f896 7428 {
6c0946d0 7429 unsigned int suffixes, evex = 0;
c006a730
JB
7430
7431 suffixes = !i.tm.opcode_modifier.no_bsuf;
7432 if (!i.tm.opcode_modifier.no_wsuf)
7433 suffixes |= 1 << 1;
7434 if (!i.tm.opcode_modifier.no_lsuf)
7435 suffixes |= 1 << 2;
c006a730
JB
7436 if (!i.tm.opcode_modifier.no_ssuf)
7437 suffixes |= 1 << 4;
7438 if (flag_code == CODE_64BIT && !i.tm.opcode_modifier.no_qsuf)
7439 suffixes |= 1 << 5;
7440
6c0946d0
JB
7441 /* For [XYZ]MMWORD operands inspect operand sizes. While generally
7442 also suitable for AT&T syntax mode, it was requested that this be
7443 restricted to just Intel syntax. */
a5748e0d
JB
7444 if (intel_syntax && is_any_vex_encoding (&i.tm)
7445 && !i.broadcast.type && !i.broadcast.bytes)
6c0946d0 7446 {
b9915cbc 7447 unsigned int op;
6c0946d0 7448
b9915cbc 7449 for (op = 0; op < i.tm.operands; ++op)
6c0946d0 7450 {
b9915cbc
JB
7451 if (is_evex_encoding (&i.tm)
7452 && !cpu_arch_flags.bitfield.cpuavx512vl)
6c0946d0 7453 {
b9915cbc
JB
7454 if (i.tm.operand_types[op].bitfield.ymmword)
7455 i.tm.operand_types[op].bitfield.xmmword = 0;
7456 if (i.tm.operand_types[op].bitfield.zmmword)
7457 i.tm.operand_types[op].bitfield.ymmword = 0;
7458 if (!i.tm.opcode_modifier.evex
7459 || i.tm.opcode_modifier.evex == EVEXDYN)
7460 i.tm.opcode_modifier.evex = EVEX512;
7461 }
6c0946d0 7462
b9915cbc
JB
7463 if (i.tm.operand_types[op].bitfield.xmmword
7464 + i.tm.operand_types[op].bitfield.ymmword
7465 + i.tm.operand_types[op].bitfield.zmmword < 2)
7466 continue;
6c0946d0 7467
b9915cbc
JB
7468 /* Any properly sized operand disambiguates the insn. */
7469 if (i.types[op].bitfield.xmmword
7470 || i.types[op].bitfield.ymmword
7471 || i.types[op].bitfield.zmmword)
7472 {
7473 suffixes &= ~(7 << 6);
7474 evex = 0;
7475 break;
7476 }
6c0946d0 7477
b9915cbc
JB
7478 if ((i.flags[op] & Operand_Mem)
7479 && i.tm.operand_types[op].bitfield.unspecified)
7480 {
7481 if (i.tm.operand_types[op].bitfield.xmmword)
7482 suffixes |= 1 << 6;
7483 if (i.tm.operand_types[op].bitfield.ymmword)
7484 suffixes |= 1 << 7;
7485 if (i.tm.operand_types[op].bitfield.zmmword)
7486 suffixes |= 1 << 8;
7487 if (is_evex_encoding (&i.tm))
7488 evex = EVEX512;
6c0946d0
JB
7489 }
7490 }
7491 }
7492
7493 /* Are multiple suffixes / operand sizes allowed? */
c006a730 7494 if (suffixes & (suffixes - 1))
9306ca4a 7495 {
873494c8 7496 if (intel_syntax
3cd7f3e3 7497 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8 7498 || operand_check == check_error))
9306ca4a 7499 {
76d3f746 7500 as_bad (_("ambiguous operand size for `%s'"), insn_name (&i.tm));
9306ca4a
JB
7501 return 0;
7502 }
c006a730 7503 if (operand_check == check_error)
9306ca4a 7504 {
c006a730 7505 as_bad (_("no instruction mnemonic suffix given and "
76d3f746 7506 "no register operands; can't size `%s'"), insn_name (&i.tm));
9306ca4a
JB
7507 return 0;
7508 }
c006a730 7509 if (operand_check == check_warning)
873494c8
JB
7510 as_warn (_("%s; using default for `%s'"),
7511 intel_syntax
7512 ? _("ambiguous operand size")
7513 : _("no instruction mnemonic suffix given and "
7514 "no register operands"),
76d3f746 7515 insn_name (&i.tm));
c006a730
JB
7516
7517 if (i.tm.opcode_modifier.floatmf)
7518 i.suffix = SHORT_MNEM_SUFFIX;
389d00a5 7519 else if (is_movx)
65fca059 7520 /* handled below */;
6c0946d0
JB
7521 else if (evex)
7522 i.tm.opcode_modifier.evex = evex;
c006a730
JB
7523 else if (flag_code == CODE_16BIT)
7524 i.suffix = WORD_MNEM_SUFFIX;
1a035124 7525 else if (!i.tm.opcode_modifier.no_lsuf)
c006a730 7526 i.suffix = LONG_MNEM_SUFFIX;
1a035124
JB
7527 else
7528 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a 7529 }
29b0f896 7530 }
252b5132 7531
389d00a5 7532 if (is_movx)
65fca059
JB
7533 {
7534 /* In Intel syntax, movsx/movzx must have a "suffix" (checked above).
7535 In AT&T syntax, if there is no suffix (warned about above), the default
7536 will be byte extension. */
7537 if (i.tm.opcode_modifier.w && i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
7538 i.tm.base_opcode |= 1;
7539
7540 /* For further processing, the suffix should represent the destination
7541 (register). This is already the case when one was used with
7542 mov[sz][bw]*, but we need to replace it for mov[sz]x, or if there was
7543 no suffix to begin with. */
7544 if (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63 || !i.suffix)
7545 {
7546 if (i.types[1].bitfield.word)
7547 i.suffix = WORD_MNEM_SUFFIX;
7548 else if (i.types[1].bitfield.qword)
7549 i.suffix = QWORD_MNEM_SUFFIX;
7550 else
7551 i.suffix = LONG_MNEM_SUFFIX;
7552
7553 i.tm.opcode_modifier.w = 0;
7554 }
7555 }
7556
50128d0c
JB
7557 if (!i.tm.opcode_modifier.modrm && i.reg_operands && i.tm.operands < 3)
7558 i.short_form = (i.tm.operand_types[0].bitfield.class == Reg)
7559 != (i.tm.operand_types[1].bitfield.class == Reg);
7560
d2224064
JB
7561 /* Change the opcode based on the operand size given by i.suffix. */
7562 switch (i.suffix)
29b0f896 7563 {
d2224064
JB
7564 /* Size floating point instruction. */
7565 case LONG_MNEM_SUFFIX:
7566 if (i.tm.opcode_modifier.floatmf)
7567 {
7568 i.tm.base_opcode ^= 4;
7569 break;
7570 }
7571 /* fall through */
7572 case WORD_MNEM_SUFFIX:
7573 case QWORD_MNEM_SUFFIX:
29b0f896 7574 /* It's not a byte, select word/dword operation. */
40fb9820 7575 if (i.tm.opcode_modifier.w)
29b0f896 7576 {
50128d0c 7577 if (i.short_form)
29b0f896
AM
7578 i.tm.base_opcode |= 8;
7579 else
7580 i.tm.base_opcode |= 1;
7581 }
d2224064
JB
7582 /* fall through */
7583 case SHORT_MNEM_SUFFIX:
29b0f896
AM
7584 /* Now select between word & dword operations via the operand
7585 size prefix, except for instructions that will ignore this
7586 prefix anyway. */
c8f8eebc 7587 if (i.suffix != QWORD_MNEM_SUFFIX
3cd7f3e3 7588 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
c8f8eebc
JB
7589 && !i.tm.opcode_modifier.floatmf
7590 && !is_any_vex_encoding (&i.tm)
7591 && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
7592 || (flag_code == CODE_64BIT
7593 && i.tm.opcode_modifier.jump == JUMP_BYTE)))
24eab124
AM
7594 {
7595 unsigned int prefix = DATA_PREFIX_OPCODE;
543613e9 7596
0cfa3eb3 7597 if (i.tm.opcode_modifier.jump == JUMP_BYTE) /* jcxz, loop */
29b0f896 7598 prefix = ADDR_PREFIX_OPCODE;
252b5132 7599
29b0f896
AM
7600 if (!add_prefix (prefix))
7601 return 0;
24eab124 7602 }
252b5132 7603
29b0f896
AM
7604 /* Set mode64 for an operand. */
7605 if (i.suffix == QWORD_MNEM_SUFFIX
9146926a 7606 && flag_code == CODE_64BIT
d2224064 7607 && !i.tm.opcode_modifier.norex64
4ed21b58 7608 && !i.tm.opcode_modifier.vexw
46e883c5 7609 /* Special case for xchg %rax,%rax. It is NOP and doesn't
d2224064
JB
7610 need rex64. */
7611 && ! (i.operands == 2
7612 && i.tm.base_opcode == 0x90
ddb62495 7613 && i.tm.opcode_space == SPACE_BASE
75e5731b
JB
7614 && i.types[0].bitfield.instance == Accum
7615 && i.types[0].bitfield.qword
44732014 7616 && i.types[1].bitfield.instance == Accum))
d2224064 7617 i.rex |= REX_W;
3e73aa7c 7618
d2224064 7619 break;
8bbb3ad8
JB
7620
7621 case 0:
f9a6a8f0 7622 /* Select word/dword/qword operation with explicit data sizing prefix
8bbb3ad8
JB
7623 when there are no suitable register operands. */
7624 if (i.tm.opcode_modifier.w
7625 && (i.prefix[DATA_PREFIX] || (i.prefix[REX_PREFIX] & REX_W))
7626 && (!i.reg_operands
7627 || (i.reg_operands == 1
7628 /* ShiftCount */
7629 && (i.tm.operand_types[0].bitfield.instance == RegC
7630 /* InOutPortReg */
7631 || i.tm.operand_types[0].bitfield.instance == RegD
7632 || i.tm.operand_types[1].bitfield.instance == RegD
7fc69528 7633 || i.tm.mnem_off == MN_crc32))))
8bbb3ad8
JB
7634 i.tm.base_opcode |= 1;
7635 break;
29b0f896 7636 }
7ecd2f8b 7637
255571cd 7638 if (i.tm.opcode_modifier.operandconstraint == ADDR_PREFIX_OP_REG)
c0a30a9f 7639 {
c8f8eebc
JB
7640 gas_assert (!i.suffix);
7641 gas_assert (i.reg_operands);
c0a30a9f 7642
c8f8eebc
JB
7643 if (i.tm.operand_types[0].bitfield.instance == Accum
7644 || i.operands == 1)
7645 {
7646 /* The address size override prefix changes the size of the
7647 first operand. */
7648 if (flag_code == CODE_64BIT
7649 && i.op[0].regs->reg_type.bitfield.word)
7650 {
7651 as_bad (_("16-bit addressing unavailable for `%s'"),
76d3f746 7652 insn_name (&i.tm));
c8f8eebc
JB
7653 return 0;
7654 }
7655
7656 if ((flag_code == CODE_32BIT
7657 ? i.op[0].regs->reg_type.bitfield.word
7658 : i.op[0].regs->reg_type.bitfield.dword)
7659 && !add_prefix (ADDR_PREFIX_OPCODE))
7660 return 0;
7661 }
c0a30a9f
L
7662 else
7663 {
c8f8eebc
JB
7664 /* Check invalid register operand when the address size override
7665 prefix changes the size of register operands. */
7666 unsigned int op;
7667 enum { need_word, need_dword, need_qword } need;
7668
27f13469 7669 /* Check the register operand for the address size prefix if
b3a3496f 7670 the memory operand has no real registers, like symbol, DISP
829f3fe1 7671 or bogus (x32-only) symbol(%rip) when symbol(%eip) is meant. */
27f13469
L
7672 if (i.mem_operands == 1
7673 && i.reg_operands == 1
7674 && i.operands == 2
27f13469 7675 && i.types[1].bitfield.class == Reg
b3a3496f
L
7676 && (flag_code == CODE_32BIT
7677 ? i.op[1].regs->reg_type.bitfield.word
7678 : i.op[1].regs->reg_type.bitfield.dword)
7679 && ((i.base_reg == NULL && i.index_reg == NULL)
829f3fe1
JB
7680#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
7681 || (x86_elf_abi == X86_64_X32_ABI
7682 && i.base_reg
b3a3496f
L
7683 && i.base_reg->reg_num == RegIP
7684 && i.base_reg->reg_type.bitfield.qword))
829f3fe1
JB
7685#else
7686 || 0)
7687#endif
27f13469
L
7688 && !add_prefix (ADDR_PREFIX_OPCODE))
7689 return 0;
7690
c8f8eebc
JB
7691 if (flag_code == CODE_32BIT)
7692 need = i.prefix[ADDR_PREFIX] ? need_word : need_dword;
7693 else if (i.prefix[ADDR_PREFIX])
c0a30a9f
L
7694 need = need_dword;
7695 else
7696 need = flag_code == CODE_64BIT ? need_qword : need_word;
c0a30a9f 7697
c8f8eebc
JB
7698 for (op = 0; op < i.operands; op++)
7699 {
7700 if (i.types[op].bitfield.class != Reg)
7701 continue;
7702
7703 switch (need)
7704 {
7705 case need_word:
7706 if (i.op[op].regs->reg_type.bitfield.word)
7707 continue;
7708 break;
7709 case need_dword:
7710 if (i.op[op].regs->reg_type.bitfield.dword)
7711 continue;
7712 break;
7713 case need_qword:
7714 if (i.op[op].regs->reg_type.bitfield.qword)
7715 continue;
7716 break;
7717 }
7718
7719 as_bad (_("invalid register operand size for `%s'"),
76d3f746 7720 insn_name (&i.tm));
c8f8eebc
JB
7721 return 0;
7722 }
7723 }
c0a30a9f
L
7724 }
7725
29b0f896
AM
7726 return 1;
7727}
3e73aa7c 7728
29b0f896 7729static int
543613e9 7730check_byte_reg (void)
29b0f896
AM
7731{
7732 int op;
543613e9 7733
29b0f896
AM
7734 for (op = i.operands; --op >= 0;)
7735 {
dc821c5f 7736 /* Skip non-register operands. */
bab6aec1 7737 if (i.types[op].bitfield.class != Reg)
dc821c5f
JB
7738 continue;
7739
29b0f896
AM
7740 /* If this is an eight bit register, it's OK. If it's the 16 or
7741 32 bit version of an eight bit register, we will just use the
7742 low portion, and that's OK too. */
dc821c5f 7743 if (i.types[op].bitfield.byte)
29b0f896
AM
7744 continue;
7745
5a819eb9 7746 /* I/O port address operands are OK too. */
75e5731b
JB
7747 if (i.tm.operand_types[op].bitfield.instance == RegD
7748 && i.tm.operand_types[op].bitfield.word)
5a819eb9
JB
7749 continue;
7750
9706160a 7751 /* crc32 only wants its source operand checked here. */
7fc69528 7752 if (i.tm.mnem_off == MN_crc32 && op != 0)
9344ff29
L
7753 continue;
7754
29b0f896 7755 /* Any other register is bad. */
73c76375
JB
7756 as_bad (_("`%s%s' not allowed with `%s%c'"),
7757 register_prefix, i.op[op].regs->reg_name,
76d3f746 7758 insn_name (&i.tm), i.suffix);
73c76375 7759 return 0;
29b0f896
AM
7760 }
7761 return 1;
7762}
7763
7764static int
e3bb37b5 7765check_long_reg (void)
29b0f896
AM
7766{
7767 int op;
7768
7769 for (op = i.operands; --op >= 0;)
dc821c5f 7770 /* Skip non-register operands. */
bab6aec1 7771 if (i.types[op].bitfield.class != Reg)
dc821c5f 7772 continue;
29b0f896
AM
7773 /* Reject eight bit registers, except where the template requires
7774 them. (eg. movzb) */
dc821c5f 7775 else if (i.types[op].bitfield.byte
bab6aec1 7776 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7777 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7778 && (i.tm.operand_types[op].bitfield.word
7779 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7780 {
a540244d
L
7781 as_bad (_("`%s%s' not allowed with `%s%c'"),
7782 register_prefix,
29b0f896 7783 i.op[op].regs->reg_name,
76d3f746 7784 insn_name (&i.tm),
29b0f896
AM
7785 i.suffix);
7786 return 0;
7787 }
be4c5e58
L
7788 /* Error if the e prefix on a general reg is missing. */
7789 else if (i.types[op].bitfield.word
bab6aec1 7790 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7791 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7792 && i.tm.operand_types[op].bitfield.dword)
29b0f896 7793 {
be4c5e58
L
7794 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7795 register_prefix, i.op[op].regs->reg_name,
7796 i.suffix);
7797 return 0;
252b5132 7798 }
e4630f71 7799 /* Warn if the r prefix on a general reg is present. */
dc821c5f 7800 else if (i.types[op].bitfield.qword
bab6aec1 7801 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7802 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7803 && i.tm.operand_types[op].bitfield.dword)
252b5132 7804 {
1cb0ab18
JB
7805 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7806 register_prefix, i.op[op].regs->reg_name, i.suffix);
7807 return 0;
29b0f896
AM
7808 }
7809 return 1;
7810}
252b5132 7811
29b0f896 7812static int
e3bb37b5 7813check_qword_reg (void)
29b0f896
AM
7814{
7815 int op;
252b5132 7816
29b0f896 7817 for (op = i.operands; --op >= 0; )
dc821c5f 7818 /* Skip non-register operands. */
bab6aec1 7819 if (i.types[op].bitfield.class != Reg)
dc821c5f 7820 continue;
29b0f896
AM
7821 /* Reject eight bit registers, except where the template requires
7822 them. (eg. movzb) */
dc821c5f 7823 else if (i.types[op].bitfield.byte
bab6aec1 7824 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7825 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7826 && (i.tm.operand_types[op].bitfield.word
7827 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7828 {
a540244d
L
7829 as_bad (_("`%s%s' not allowed with `%s%c'"),
7830 register_prefix,
29b0f896 7831 i.op[op].regs->reg_name,
76d3f746 7832 insn_name (&i.tm),
29b0f896
AM
7833 i.suffix);
7834 return 0;
7835 }
e4630f71 7836 /* Warn if the r prefix on a general reg is missing. */
dc821c5f
JB
7837 else if ((i.types[op].bitfield.word
7838 || i.types[op].bitfield.dword)
bab6aec1 7839 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7840 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7841 && i.tm.operand_types[op].bitfield.qword)
29b0f896
AM
7842 {
7843 /* Prohibit these changes in the 64bit mode, since the
7844 lowering is more complicated. */
1cb0ab18
JB
7845 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7846 register_prefix, i.op[op].regs->reg_name, i.suffix);
7847 return 0;
252b5132 7848 }
29b0f896
AM
7849 return 1;
7850}
252b5132 7851
29b0f896 7852static int
e3bb37b5 7853check_word_reg (void)
29b0f896
AM
7854{
7855 int op;
7856 for (op = i.operands; --op >= 0;)
dc821c5f 7857 /* Skip non-register operands. */
bab6aec1 7858 if (i.types[op].bitfield.class != Reg)
dc821c5f 7859 continue;
29b0f896
AM
7860 /* Reject eight bit registers, except where the template requires
7861 them. (eg. movzb) */
dc821c5f 7862 else if (i.types[op].bitfield.byte
bab6aec1 7863 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7864 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7865 && (i.tm.operand_types[op].bitfield.word
7866 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7867 {
a540244d
L
7868 as_bad (_("`%s%s' not allowed with `%s%c'"),
7869 register_prefix,
29b0f896 7870 i.op[op].regs->reg_name,
76d3f746 7871 insn_name (&i.tm),
29b0f896
AM
7872 i.suffix);
7873 return 0;
7874 }
9706160a
JB
7875 /* Error if the e or r prefix on a general reg is present. */
7876 else if ((i.types[op].bitfield.dword
dc821c5f 7877 || i.types[op].bitfield.qword)
bab6aec1 7878 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7879 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7880 && i.tm.operand_types[op].bitfield.word)
252b5132 7881 {
9706160a
JB
7882 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7883 register_prefix, i.op[op].regs->reg_name,
7884 i.suffix);
7885 return 0;
29b0f896
AM
7886 }
7887 return 1;
7888}
252b5132 7889
29b0f896 7890static int
40fb9820 7891update_imm (unsigned int j)
29b0f896 7892{
bc0844ae 7893 i386_operand_type overlap = i.types[j];
c34d1cc9
JB
7894
7895 if (i.tm.operand_types[j].bitfield.imm8
7896 && i.tm.operand_types[j].bitfield.imm8s
7897 && overlap.bitfield.imm8 && overlap.bitfield.imm8s)
7898 {
7899 /* This combination is used on 8-bit immediates where e.g. $~0 is
7900 desirable to permit. We're past operand type matching, so simply
7901 put things back in the shape they were before introducing the
7902 distinction between Imm8, Imm8S, and Imm8|Imm8S. */
7903 overlap.bitfield.imm8s = 0;
7904 }
7905
be1643ff
JB
7906 if (overlap.bitfield.imm8
7907 + overlap.bitfield.imm8s
7908 + overlap.bitfield.imm16
7909 + overlap.bitfield.imm32
7910 + overlap.bitfield.imm32s
7911 + overlap.bitfield.imm64 > 1)
29b0f896 7912 {
05909f23
JB
7913 static const i386_operand_type imm16 = { .bitfield = { .imm16 = 1 } };
7914 static const i386_operand_type imm32 = { .bitfield = { .imm32 = 1 } };
7915 static const i386_operand_type imm32s = { .bitfield = { .imm32s = 1 } };
7916 static const i386_operand_type imm16_32 = { .bitfield =
7917 { .imm16 = 1, .imm32 = 1 }
7918 };
7919 static const i386_operand_type imm16_32s = { .bitfield =
7920 { .imm16 = 1, .imm32s = 1 }
7921 };
7922 static const i386_operand_type imm16_32_32s = { .bitfield =
7923 { .imm16 = 1, .imm32 = 1, .imm32s = 1 }
7924 };
7925
29b0f896
AM
7926 if (i.suffix)
7927 {
40fb9820
L
7928 i386_operand_type temp;
7929
0dfbf9d7 7930 operand_type_set (&temp, 0);
7ab9ffdd 7931 if (i.suffix == BYTE_MNEM_SUFFIX)
40fb9820
L
7932 {
7933 temp.bitfield.imm8 = overlap.bitfield.imm8;
7934 temp.bitfield.imm8s = overlap.bitfield.imm8s;
7935 }
7936 else if (i.suffix == WORD_MNEM_SUFFIX)
7937 temp.bitfield.imm16 = overlap.bitfield.imm16;
7938 else if (i.suffix == QWORD_MNEM_SUFFIX)
7939 {
7940 temp.bitfield.imm64 = overlap.bitfield.imm64;
7941 temp.bitfield.imm32s = overlap.bitfield.imm32s;
7942 }
7943 else
7944 temp.bitfield.imm32 = overlap.bitfield.imm32;
7945 overlap = temp;
29b0f896 7946 }
0dfbf9d7
L
7947 else if (operand_type_equal (&overlap, &imm16_32_32s)
7948 || operand_type_equal (&overlap, &imm16_32)
7949 || operand_type_equal (&overlap, &imm16_32s))
29b0f896 7950 {
40fb9820 7951 if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
65da13b5 7952 overlap = imm16;
40fb9820 7953 else
65da13b5 7954 overlap = imm32s;
29b0f896 7955 }
8bbb3ad8
JB
7956 else if (i.prefix[REX_PREFIX] & REX_W)
7957 overlap = operand_type_and (overlap, imm32s);
7958 else if (i.prefix[DATA_PREFIX])
7959 overlap = operand_type_and (overlap,
7960 flag_code != CODE_16BIT ? imm16 : imm32);
be1643ff
JB
7961 if (overlap.bitfield.imm8
7962 + overlap.bitfield.imm8s
7963 + overlap.bitfield.imm16
7964 + overlap.bitfield.imm32
7965 + overlap.bitfield.imm32s
7966 + overlap.bitfield.imm64 != 1)
29b0f896 7967 {
4eed87de
AM
7968 as_bad (_("no instruction mnemonic suffix given; "
7969 "can't determine immediate size"));
29b0f896
AM
7970 return 0;
7971 }
7972 }
40fb9820 7973 i.types[j] = overlap;
29b0f896 7974
40fb9820
L
7975 return 1;
7976}
7977
7978static int
7979finalize_imm (void)
7980{
bc0844ae 7981 unsigned int j, n;
29b0f896 7982
bc0844ae
L
7983 /* Update the first 2 immediate operands. */
7984 n = i.operands > 2 ? 2 : i.operands;
7985 if (n)
7986 {
7987 for (j = 0; j < n; j++)
7988 if (update_imm (j) == 0)
7989 return 0;
40fb9820 7990
bc0844ae
L
7991 /* The 3rd operand can't be immediate operand. */
7992 gas_assert (operand_type_check (i.types[2], imm) == 0);
7993 }
29b0f896
AM
7994
7995 return 1;
7996}
7997
0a3eba42
JB
7998static INLINE void set_rex_vrex (const reg_entry *r, unsigned int rex_bit,
7999 bool do_sse2avx)
8000{
8001 if (r->reg_flags & RegRex)
8002 {
8003 if (i.rex & rex_bit)
8004 as_bad (_("same type of prefix used twice"));
8005 i.rex |= rex_bit;
8006 }
8007 else if (do_sse2avx && (i.rex & rex_bit) && i.vex.register_specifier)
8008 {
8009 gas_assert (i.vex.register_specifier == r);
8010 i.vex.register_specifier += 8;
8011 }
8012
8013 if (r->reg_flags & RegVRex)
8014 i.vrex |= rex_bit;
8015}
8016
29b0f896 8017static int
e3bb37b5 8018process_operands (void)
29b0f896
AM
8019{
8020 /* Default segment register this instruction will use for memory
8021 accesses. 0 means unknown. This is only for optimizing out
8022 unnecessary segment overrides. */
5e042380 8023 const reg_entry *default_seg = NULL;
29b0f896 8024
31b4cda3
JB
8025 /* We only need to check those implicit registers for instructions
8026 with 3 operands or less. */
8027 if (i.operands <= 3)
8028 for (unsigned int j = 0; j < i.operands; j++)
8029 if (i.types[j].bitfield.instance != InstanceNone)
8030 i.reg_operands--;
8031
a5aeccd9
JB
8032 if (i.tm.opcode_modifier.sse2avx)
8033 {
8034 /* Legacy encoded insns allow explicit REX prefixes, so these prefixes
8035 need converting. */
8036 i.rex |= i.prefix[REX_PREFIX] & (REX_W | REX_R | REX_X | REX_B);
8037 i.prefix[REX_PREFIX] = 0;
8038 i.rex_encoding = 0;
8039 }
c423d21a
JB
8040 /* ImmExt should be processed after SSE2AVX. */
8041 else if (i.tm.opcode_modifier.immext)
8042 process_immext ();
a5aeccd9 8043
ecb96e55
JB
8044 /* TILEZERO is unusual in that it has a single operand encoded in ModR/M.reg,
8045 not ModR/M.rm. To avoid special casing this in build_modrm_byte(), fake a
8046 new destination operand here, while converting the source one to register
8047 number 0. */
8048 if (i.tm.mnem_off == MN_tilezero)
8049 {
8050 i.op[1].regs = i.op[0].regs;
8051 i.op[0].regs -= i.op[0].regs->reg_num;
8052 i.types[1] = i.types[0];
8053 i.tm.operand_types[1] = i.tm.operand_types[0];
8054 i.flags[1] = i.flags[0];
8055 i.operands++;
8056 i.reg_operands++;
8057 i.tm.operands++;
8058 }
8059
2426c15f 8060 if (i.tm.opcode_modifier.sse2avx && i.tm.opcode_modifier.vexvvvv)
29b0f896 8061 {
05909f23
JB
8062 static const i386_operand_type regxmm = {
8063 .bitfield = { .class = RegSIMD, .xmmword = 1 }
8064 };
91d6fa6a
NC
8065 unsigned int dupl = i.operands;
8066 unsigned int dest = dupl - 1;
9fcfb3d7
L
8067 unsigned int j;
8068
c0f3af97 8069 /* The destination must be an xmm register. */
9c2799c2 8070 gas_assert (i.reg_operands
91d6fa6a 8071 && MAX_OPERANDS > dupl
7ab9ffdd 8072 && operand_type_equal (&i.types[dest], &regxmm));
c0f3af97 8073
75e5731b 8074 if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 8075 && i.tm.operand_types[0].bitfield.xmmword)
e2ec9d29 8076 {
95dfdd85
JB
8077 /* Keep xmm0 for instructions with VEX prefix and 3
8078 sources. */
8079 i.tm.operand_types[0].bitfield.instance = InstanceNone;
8080 i.tm.operand_types[0].bitfield.class = RegSIMD;
31b4cda3 8081 i.reg_operands++;
95dfdd85 8082 goto duplicate;
c0f3af97 8083 }
95dfdd85
JB
8084
8085 if (i.tm.opcode_modifier.operandconstraint == IMPLICIT_1ST_XMM0)
7ab9ffdd 8086 {
aa180741 8087 gas_assert ((MAX_OPERANDS - 1) > dupl);
c0f3af97
L
8088
8089 /* Add the implicit xmm0 for instructions with VEX prefix
8090 and 3 sources. */
8091 for (j = i.operands; j > 0; j--)
8092 {
8093 i.op[j] = i.op[j - 1];
8094 i.types[j] = i.types[j - 1];
8095 i.tm.operand_types[j] = i.tm.operand_types[j - 1];
8dc0818e 8096 i.flags[j] = i.flags[j - 1];
c0f3af97
L
8097 }
8098 i.op[0].regs
629310ab 8099 = (const reg_entry *) str_hash_find (reg_hash, "xmm0");
7ab9ffdd 8100 i.types[0] = regxmm;
c0f3af97
L
8101 i.tm.operand_types[0] = regxmm;
8102
8103 i.operands += 2;
8104 i.reg_operands += 2;
8105 i.tm.operands += 2;
8106
91d6fa6a 8107 dupl++;
c0f3af97 8108 dest++;
91d6fa6a
NC
8109 i.op[dupl] = i.op[dest];
8110 i.types[dupl] = i.types[dest];
8111 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 8112 i.flags[dupl] = i.flags[dest];
e2ec9d29 8113 }
c0f3af97
L
8114 else
8115 {
dc1e8a47 8116 duplicate:
c0f3af97
L
8117 i.operands++;
8118 i.reg_operands++;
8119 i.tm.operands++;
8120
91d6fa6a
NC
8121 i.op[dupl] = i.op[dest];
8122 i.types[dupl] = i.types[dest];
8123 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 8124 i.flags[dupl] = i.flags[dest];
c0f3af97
L
8125 }
8126
8127 if (i.tm.opcode_modifier.immext)
8128 process_immext ();
8129 }
75e5731b 8130 else if (i.tm.operand_types[0].bitfield.instance == Accum
bd782808 8131 && i.tm.opcode_modifier.modrm)
c0f3af97
L
8132 {
8133 unsigned int j;
8134
9fcfb3d7
L
8135 for (j = 1; j < i.operands; j++)
8136 {
8137 i.op[j - 1] = i.op[j];
8138 i.types[j - 1] = i.types[j];
8139
8140 /* We need to adjust fields in i.tm since they are used by
8141 build_modrm_byte. */
8142 i.tm.operand_types [j - 1] = i.tm.operand_types [j];
8dc0818e
JB
8143
8144 i.flags[j - 1] = i.flags[j];
9fcfb3d7
L
8145 }
8146
31b4cda3
JB
8147 /* No adjustment to i.reg_operands: This was already done at the top
8148 of the function. */
e2ec9d29 8149 i.operands--;
e2ec9d29
L
8150 i.tm.operands--;
8151 }
255571cd 8152 else if (i.tm.opcode_modifier.operandconstraint == IMPLICIT_QUAD_GROUP)
920d2ddc 8153 {
a477a8c4
JB
8154 unsigned int regnum, first_reg_in_group, last_reg_in_group;
8155
920d2ddc 8156 /* The second operand must be {x,y,z}mmN, where N is a multiple of 4. */
3528c362 8157 gas_assert (i.operands >= 2 && i.types[1].bitfield.class == RegSIMD);
a477a8c4
JB
8158 regnum = register_number (i.op[1].regs);
8159 first_reg_in_group = regnum & ~3;
8160 last_reg_in_group = first_reg_in_group + 3;
8161 if (regnum != first_reg_in_group)
8162 as_warn (_("source register `%s%s' implicitly denotes"
8163 " `%s%.3s%u' to `%s%.3s%u' source group in `%s'"),
8164 register_prefix, i.op[1].regs->reg_name,
8165 register_prefix, i.op[1].regs->reg_name, first_reg_in_group,
8166 register_prefix, i.op[1].regs->reg_name, last_reg_in_group,
76d3f746 8167 insn_name (&i.tm));
a477a8c4 8168 }
255571cd 8169 else if (i.tm.opcode_modifier.operandconstraint == REG_KLUDGE)
e2ec9d29
L
8170 {
8171 /* The imul $imm, %reg instruction is converted into
8172 imul $imm, %reg, %reg, and the clr %reg instruction
8173 is converted into xor %reg, %reg. */
8174
8175 unsigned int first_reg_op;
8176
8177 if (operand_type_check (i.types[0], reg))
8178 first_reg_op = 0;
8179 else
8180 first_reg_op = 1;
8181 /* Pretend we saw the extra register operand. */
9c2799c2 8182 gas_assert (i.reg_operands == 1
7ab9ffdd 8183 && i.op[first_reg_op + 1].regs == 0);
e2ec9d29
L
8184 i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
8185 i.types[first_reg_op + 1] = i.types[first_reg_op];
8186 i.operands++;
8187 i.reg_operands++;
29b0f896
AM
8188 }
8189
85b80b0f 8190 if (i.tm.opcode_modifier.modrm)
29b0f896
AM
8191 {
8192 /* The opcode is completed (modulo i.tm.extension_opcode which
52271982
AM
8193 must be put into the modrm byte). Now, we make the modrm and
8194 index base bytes based on all the info we've collected. */
29b0f896
AM
8195
8196 default_seg = build_modrm_byte ();
bd782808
JB
8197
8198 if (!quiet_warnings && i.tm.opcode_modifier.operandconstraint == UGH)
8199 {
8200 /* Warn about some common errors, but press on regardless. */
8201 if (i.operands == 2)
8202 {
8203 /* Reversed arguments on faddp or fmulp. */
8204 as_warn (_("translating to `%s %s%s,%s%s'"), insn_name (&i.tm),
8205 register_prefix, i.op[!intel_syntax].regs->reg_name,
8206 register_prefix, i.op[intel_syntax].regs->reg_name);
8207 }
8208 else if (i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
8209 {
8210 /* Extraneous `l' suffix on fp insn. */
8211 as_warn (_("translating to `%s %s%s'"), insn_name (&i.tm),
8212 register_prefix, i.op[0].regs->reg_name);
8213 }
8214 }
29b0f896 8215 }
00cee14f 8216 else if (i.types[0].bitfield.class == SReg)
85b80b0f
JB
8217 {
8218 if (flag_code != CODE_64BIT
8219 ? i.tm.base_opcode == POP_SEG_SHORT
8220 && i.op[0].regs->reg_num == 1
389d00a5 8221 : (i.tm.base_opcode | 1) == (POP_SEG386_SHORT & 0xff)
85b80b0f
JB
8222 && i.op[0].regs->reg_num < 4)
8223 {
8224 as_bad (_("you can't `%s %s%s'"),
76d3f746 8225 insn_name (&i.tm), register_prefix, i.op[0].regs->reg_name);
85b80b0f
JB
8226 return 0;
8227 }
389d00a5 8228 if (i.op[0].regs->reg_num > 3
ddb62495 8229 && i.tm.opcode_space == SPACE_BASE )
85b80b0f 8230 {
389d00a5 8231 i.tm.base_opcode ^= (POP_SEG_SHORT ^ POP_SEG386_SHORT) & 0xff;
ddb62495 8232 i.tm.opcode_space = SPACE_0F;
85b80b0f
JB
8233 }
8234 i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
8235 }
ddb62495 8236 else if (i.tm.opcode_space == SPACE_BASE
389d00a5 8237 && (i.tm.base_opcode & ~3) == MOV_AX_DISP32)
29b0f896 8238 {
5e042380 8239 default_seg = reg_ds;
29b0f896 8240 }
40fb9820 8241 else if (i.tm.opcode_modifier.isstring)
29b0f896
AM
8242 {
8243 /* For the string instructions that allow a segment override
8244 on one of their operands, the default segment is ds. */
5e042380 8245 default_seg = reg_ds;
29b0f896 8246 }
50128d0c 8247 else if (i.short_form)
85b80b0f 8248 {
bd782808 8249 /* The register operand is in operand 0 or 1. */
4943d587 8250 const reg_entry *r = i.op[0].regs;
85b80b0f 8251
4943d587
JB
8252 if (i.imm_operands
8253 || (r->reg_type.bitfield.instance == Accum && i.op[1].regs))
8254 r = i.op[1].regs;
85b80b0f 8255 /* Register goes in low 3 bits of opcode. */
4943d587 8256 i.tm.base_opcode |= r->reg_num;
0a3eba42 8257 set_rex_vrex (r, REX_B, false);
85b80b0f 8258 }
29b0f896 8259
514a8bb0 8260 if ((i.seg[0] || i.prefix[SEG_PREFIX])
7fc69528 8261 && i.tm.mnem_off == MN_lea)
92334ad2
JB
8262 {
8263 if (!quiet_warnings)
76d3f746 8264 as_warn (_("segment override on `%s' is ineffectual"), insn_name (&i.tm));
739d7649 8265 if (optimize && !i.no_optimize)
92334ad2
JB
8266 {
8267 i.seg[0] = NULL;
8268 i.prefix[SEG_PREFIX] = 0;
8269 }
8270 }
52271982
AM
8271
8272 /* If a segment was explicitly specified, and the specified segment
b6773884
JB
8273 is neither the default nor the one already recorded from a prefix,
8274 use an opcode prefix to select it. If we never figured out what
8275 the default segment is, then default_seg will be zero at this
8276 point, and the specified segment prefix will always be used. */
8277 if (i.seg[0]
8278 && i.seg[0] != default_seg
5e042380 8279 && i386_seg_prefixes[i.seg[0]->reg_num] != i.prefix[SEG_PREFIX])
29b0f896 8280 {
5e042380 8281 if (!add_prefix (i386_seg_prefixes[i.seg[0]->reg_num]))
29b0f896
AM
8282 return 0;
8283 }
8284 return 1;
8285}
8286
5e042380 8287static const reg_entry *
e3bb37b5 8288build_modrm_byte (void)
29b0f896 8289{
5e042380 8290 const reg_entry *default_seg = NULL;
ecb96e55
JB
8291 unsigned int source = i.imm_operands - i.tm.opcode_modifier.immext
8292 /* Compensate for kludge in md_assemble(). */
8293 + i.tm.operand_types[0].bitfield.imm1;
8294 unsigned int dest = i.operands - 1 - i.tm.opcode_modifier.immext;
8295 unsigned int v, op, reg_slot = ~0;
8296
8297 /* Accumulator (in particular %st), shift count (%cl), and alike need
8298 to be skipped just like immediate operands do. */
8299 if (i.tm.operand_types[source].bitfield.instance)
8300 ++source;
8301 while (i.tm.operand_types[dest].bitfield.instance)
8302 --dest;
8303
8304 for (op = source; op < i.operands; ++op)
8305 if (i.tm.operand_types[op].bitfield.baseindex)
8306 break;
c0f3af97 8307
ecb96e55 8308 if (i.reg_operands + i.mem_operands + (i.tm.extension_opcode != None) == 4)
c0f3af97 8309 {
4c2c6516 8310 expressionS *exp;
c0f3af97 8311
a683cc34 8312 /* There are 2 kinds of instructions:
bed3d976 8313 1. 5 operands: 4 register operands or 3 register operands
9d3bf266 8314 plus 1 memory operand plus one Imm4 operand, VexXDS, and
bed3d976 8315 VexW0 or VexW1. The destination must be either XMM, YMM or
43234a1e 8316 ZMM register.
bed3d976 8317 2. 4 operands: 4 register operands or 3 register operands
2f1bada2 8318 plus 1 memory operand, with VexXDS. */
eea96d3f 8319 gas_assert (i.tm.opcode_modifier.vexvvvv
dcd7e323 8320 && i.tm.opcode_modifier.vexw
3528c362 8321 && i.tm.operand_types[dest].bitfield.class == RegSIMD);
a683cc34 8322
ecb96e55
JB
8323 /* Of the first two non-immediate operands the one with the template
8324 not allowing for a memory one is encoded in the immediate operand. */
8325 if (source == op)
8326 reg_slot = source + 1;
48db9223 8327 else
ecb96e55 8328 reg_slot = source++;
48db9223 8329
a683cc34 8330 if (i.imm_operands == 0)
bed3d976
JB
8331 {
8332 /* When there is no immediate operand, generate an 8bit
8333 immediate operand to encode the first operand. */
8334 exp = &im_expressions[i.imm_operands++];
8335 i.op[i.operands].imms = exp;
be1643ff 8336 i.types[i.operands].bitfield.imm8 = 1;
bed3d976
JB
8337 i.operands++;
8338
3528c362 8339 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
bed3d976
JB
8340 exp->X_op = O_constant;
8341 exp->X_add_number = register_number (i.op[reg_slot].regs) << 4;
43234a1e
L
8342 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
8343 }
922d8de8 8344 else
bed3d976 8345 {
9d3bf266
JB
8346 gas_assert (i.imm_operands == 1);
8347 gas_assert (fits_in_imm4 (i.op[0].imms->X_add_number));
8348 gas_assert (!i.tm.opcode_modifier.immext);
a683cc34 8349
9d3bf266
JB
8350 /* Turn on Imm8 again so that output_imm will generate it. */
8351 i.types[0].bitfield.imm8 = 1;
bed3d976 8352
3528c362 8353 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
9d3bf266 8354 i.op[0].imms->X_add_number
bed3d976 8355 |= register_number (i.op[reg_slot].regs) << 4;
43234a1e 8356 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
bed3d976 8357 }
c0f3af97 8358 }
f12dc422 8359
ecb96e55
JB
8360 for (v = source + 1; v < dest; ++v)
8361 if (v != reg_slot)
8362 break;
8363 if (v >= dest)
8364 v = ~0;
8365 if (i.tm.extension_opcode != None)
8366 {
8367 if (dest != source)
8368 v = dest;
8369 dest = ~0;
8370 }
8371 gas_assert (source < dest);
8372 if (i.tm.opcode_modifier.operandconstraint == SWAP_SOURCES
8373 && source != op)
8374 {
8375 unsigned int tmp = source;
f12dc422 8376
ecb96e55
JB
8377 source = v;
8378 v = tmp;
8379 }
29b0f896 8380
ecb96e55
JB
8381 if (v < MAX_OPERANDS)
8382 {
8383 gas_assert (i.tm.opcode_modifier.vexvvvv);
8384 i.vex.register_specifier = i.op[v].regs;
29b0f896 8385 }
c0f3af97 8386
ecb96e55
JB
8387 if (op < i.operands)
8388 {
29b0f896
AM
8389 if (i.mem_operands)
8390 {
8391 unsigned int fake_zero_displacement = 0;
4eed87de 8392
ecb96e55 8393 gas_assert (i.flags[op] & Operand_Mem);
29b0f896 8394
63112cd6 8395 if (i.tm.opcode_modifier.sib)
6c30d220 8396 {
260cd341
LC
8397 /* The index register of VSIB shouldn't be RegIZ. */
8398 if (i.tm.opcode_modifier.sib != SIBMEM
8399 && i.index_reg->reg_num == RegIZ)
6c30d220
L
8400 abort ();
8401
8402 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8403 if (!i.base_reg)
8404 {
8405 i.sib.base = NO_BASE_REGISTER;
8406 i.sib.scale = i.log2_scale_factor;
2f2be86b 8407 i.types[op] = operand_type_and_not (i.types[op], anydisp);
a775efc8 8408 i.types[op].bitfield.disp32 = 1;
6c30d220 8409 }
260cd341
LC
8410
8411 /* Since the mandatory SIB always has index register, so
8412 the code logic remains unchanged. The non-mandatory SIB
8413 without index register is allowed and will be handled
8414 later. */
8415 if (i.index_reg)
8416 {
8417 if (i.index_reg->reg_num == RegIZ)
8418 i.sib.index = NO_INDEX_REGISTER;
8419 else
8420 i.sib.index = i.index_reg->reg_num;
5b7c81bd 8421 set_rex_vrex (i.index_reg, REX_X, false);
260cd341 8422 }
6c30d220
L
8423 }
8424
5e042380 8425 default_seg = reg_ds;
29b0f896
AM
8426
8427 if (i.base_reg == 0)
8428 {
8429 i.rm.mode = 0;
8430 if (!i.disp_operands)
9bb129e8 8431 fake_zero_displacement = 1;
29b0f896
AM
8432 if (i.index_reg == 0)
8433 {
260cd341
LC
8434 /* Both check for VSIB and mandatory non-vector SIB. */
8435 gas_assert (!i.tm.opcode_modifier.sib
8436 || i.tm.opcode_modifier.sib == SIBMEM);
29b0f896 8437 /* Operand is just <disp> */
2f2be86b 8438 i.types[op] = operand_type_and_not (i.types[op], anydisp);
20f0a1fc 8439 if (flag_code == CODE_64BIT)
29b0f896
AM
8440 {
8441 /* 64bit mode overwrites the 32bit absolute
8442 addressing by RIP relative addressing and
8443 absolute addressing is encoded by one of the
8444 redundant SIB forms. */
8445 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8446 i.sib.base = NO_BASE_REGISTER;
8447 i.sib.index = NO_INDEX_REGISTER;
a775efc8 8448 i.types[op].bitfield.disp32 = 1;
20f0a1fc 8449 }
fc225355
L
8450 else if ((flag_code == CODE_16BIT)
8451 ^ (i.prefix[ADDR_PREFIX] != 0))
20f0a1fc
NC
8452 {
8453 i.rm.regmem = NO_BASE_REGISTER_16;
2f2be86b 8454 i.types[op].bitfield.disp16 = 1;
20f0a1fc
NC
8455 }
8456 else
8457 {
8458 i.rm.regmem = NO_BASE_REGISTER;
2f2be86b 8459 i.types[op].bitfield.disp32 = 1;
29b0f896
AM
8460 }
8461 }
63112cd6 8462 else if (!i.tm.opcode_modifier.sib)
29b0f896 8463 {
6c30d220 8464 /* !i.base_reg && i.index_reg */
e968fc9b 8465 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8466 i.sib.index = NO_INDEX_REGISTER;
8467 else
8468 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8469 i.sib.base = NO_BASE_REGISTER;
8470 i.sib.scale = i.log2_scale_factor;
8471 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
2f2be86b 8472 i.types[op] = operand_type_and_not (i.types[op], anydisp);
a775efc8 8473 i.types[op].bitfield.disp32 = 1;
29b0f896 8474 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8475 i.rex |= REX_X;
29b0f896
AM
8476 }
8477 }
8478 /* RIP addressing for 64bit mode. */
e968fc9b 8479 else if (i.base_reg->reg_num == RegIP)
29b0f896 8480 {
63112cd6 8481 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896 8482 i.rm.regmem = NO_BASE_REGISTER;
40fb9820
L
8483 i.types[op].bitfield.disp8 = 0;
8484 i.types[op].bitfield.disp16 = 0;
a775efc8 8485 i.types[op].bitfield.disp32 = 1;
40fb9820 8486 i.types[op].bitfield.disp64 = 0;
71903a11 8487 i.flags[op] |= Operand_PCrel;
20f0a1fc
NC
8488 if (! i.disp_operands)
8489 fake_zero_displacement = 1;
29b0f896 8490 }
dc821c5f 8491 else if (i.base_reg->reg_type.bitfield.word)
29b0f896 8492 {
63112cd6 8493 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896
AM
8494 switch (i.base_reg->reg_num)
8495 {
8496 case 3: /* (%bx) */
8497 if (i.index_reg == 0)
8498 i.rm.regmem = 7;
8499 else /* (%bx,%si) -> 0, or (%bx,%di) -> 1 */
8500 i.rm.regmem = i.index_reg->reg_num - 6;
8501 break;
8502 case 5: /* (%bp) */
5e042380 8503 default_seg = reg_ss;
29b0f896
AM
8504 if (i.index_reg == 0)
8505 {
8506 i.rm.regmem = 6;
40fb9820 8507 if (operand_type_check (i.types[op], disp) == 0)
29b0f896
AM
8508 {
8509 /* fake (%bp) into 0(%bp) */
41eb8e88 8510 if (i.disp_encoding == disp_encoding_16bit)
1a02d6b0
L
8511 i.types[op].bitfield.disp16 = 1;
8512 else
8513 i.types[op].bitfield.disp8 = 1;
252b5132 8514 fake_zero_displacement = 1;
29b0f896
AM
8515 }
8516 }
8517 else /* (%bp,%si) -> 2, or (%bp,%di) -> 3 */
8518 i.rm.regmem = i.index_reg->reg_num - 6 + 2;
8519 break;
8520 default: /* (%si) -> 4 or (%di) -> 5 */
8521 i.rm.regmem = i.base_reg->reg_num - 6 + 4;
8522 }
41eb8e88
L
8523 if (!fake_zero_displacement
8524 && !i.disp_operands
8525 && i.disp_encoding)
8526 {
8527 fake_zero_displacement = 1;
8528 if (i.disp_encoding == disp_encoding_8bit)
8529 i.types[op].bitfield.disp8 = 1;
8530 else
8531 i.types[op].bitfield.disp16 = 1;
8532 }
29b0f896
AM
8533 i.rm.mode = mode_from_disp_size (i.types[op]);
8534 }
8535 else /* i.base_reg and 32/64 bit mode */
8536 {
a9aabc23 8537 if (operand_type_check (i.types[op], disp))
40fb9820 8538 {
73053c1f
JB
8539 i.types[op].bitfield.disp16 = 0;
8540 i.types[op].bitfield.disp64 = 0;
a775efc8 8541 i.types[op].bitfield.disp32 = 1;
40fb9820 8542 }
20f0a1fc 8543
63112cd6 8544 if (!i.tm.opcode_modifier.sib)
6c30d220 8545 i.rm.regmem = i.base_reg->reg_num;
29b0f896 8546 if ((i.base_reg->reg_flags & RegRex) != 0)
161a04f6 8547 i.rex |= REX_B;
29b0f896
AM
8548 i.sib.base = i.base_reg->reg_num;
8549 /* x86-64 ignores REX prefix bit here to avoid decoder
8550 complications. */
848930b2
JB
8551 if (!(i.base_reg->reg_flags & RegRex)
8552 && (i.base_reg->reg_num == EBP_REG_NUM
8553 || i.base_reg->reg_num == ESP_REG_NUM))
5e042380 8554 default_seg = reg_ss;
848930b2 8555 if (i.base_reg->reg_num == 5 && i.disp_operands == 0)
29b0f896 8556 {
848930b2 8557 fake_zero_displacement = 1;
1a02d6b0
L
8558 if (i.disp_encoding == disp_encoding_32bit)
8559 i.types[op].bitfield.disp32 = 1;
8560 else
8561 i.types[op].bitfield.disp8 = 1;
29b0f896
AM
8562 }
8563 i.sib.scale = i.log2_scale_factor;
8564 if (i.index_reg == 0)
8565 {
260cd341
LC
8566 /* Only check for VSIB. */
8567 gas_assert (i.tm.opcode_modifier.sib != VECSIB128
8568 && i.tm.opcode_modifier.sib != VECSIB256
8569 && i.tm.opcode_modifier.sib != VECSIB512);
8570
29b0f896
AM
8571 /* <disp>(%esp) becomes two byte modrm with no index
8572 register. We've already stored the code for esp
8573 in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
8574 Any base register besides %esp will not use the
8575 extra modrm byte. */
8576 i.sib.index = NO_INDEX_REGISTER;
29b0f896 8577 }
63112cd6 8578 else if (!i.tm.opcode_modifier.sib)
29b0f896 8579 {
e968fc9b 8580 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8581 i.sib.index = NO_INDEX_REGISTER;
8582 else
8583 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8584 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8585 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8586 i.rex |= REX_X;
29b0f896 8587 }
67a4f2b7
AO
8588
8589 if (i.disp_operands
8590 && (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
8591 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL))
8592 i.rm.mode = 0;
8593 else
a501d77e
L
8594 {
8595 if (!fake_zero_displacement
8596 && !i.disp_operands
8597 && i.disp_encoding)
8598 {
8599 fake_zero_displacement = 1;
8600 if (i.disp_encoding == disp_encoding_8bit)
8601 i.types[op].bitfield.disp8 = 1;
8602 else
8603 i.types[op].bitfield.disp32 = 1;
8604 }
8605 i.rm.mode = mode_from_disp_size (i.types[op]);
8606 }
29b0f896 8607 }
252b5132 8608
29b0f896
AM
8609 if (fake_zero_displacement)
8610 {
8611 /* Fakes a zero displacement assuming that i.types[op]
8612 holds the correct displacement size. */
8613 expressionS *exp;
8614
9c2799c2 8615 gas_assert (i.op[op].disps == 0);
29b0f896
AM
8616 exp = &disp_expressions[i.disp_operands++];
8617 i.op[op].disps = exp;
8618 exp->X_op = O_constant;
8619 exp->X_add_number = 0;
8620 exp->X_add_symbol = (symbolS *) 0;
8621 exp->X_op_symbol = (symbolS *) 0;
8622 }
8623 }
ecb96e55
JB
8624 else
8625 {
8626 i.rm.mode = 3;
8627 i.rm.regmem = i.op[op].regs->reg_num;
8628 set_rex_vrex (i.op[op].regs, REX_B, false);
8629 }
252b5132 8630
ecb96e55
JB
8631 if (op == dest)
8632 dest = ~0;
8633 if (op == source)
8634 source = ~0;
8635 }
8636 else
8637 {
8638 i.rm.mode = 3;
8639 if (!i.tm.opcode_modifier.regmem)
f88c9eb0 8640 {
ecb96e55
JB
8641 gas_assert (source < MAX_OPERANDS);
8642 i.rm.regmem = i.op[source].regs->reg_num;
8643 set_rex_vrex (i.op[source].regs, REX_B,
8644 dest >= MAX_OPERANDS && i.tm.opcode_modifier.sse2avx);
8645 source = ~0;
f88c9eb0 8646 }
ecb96e55 8647 else
29b0f896 8648 {
ecb96e55
JB
8649 gas_assert (dest < MAX_OPERANDS);
8650 i.rm.regmem = i.op[dest].regs->reg_num;
8651 set_rex_vrex (i.op[dest].regs, REX_B, i.tm.opcode_modifier.sse2avx);
8652 dest = ~0;
29b0f896 8653 }
ecb96e55 8654 }
252b5132 8655
ecb96e55
JB
8656 /* Fill in i.rm.reg field with extension opcode (if any) or the
8657 appropriate register. */
8658 if (i.tm.extension_opcode != None)
8659 i.rm.reg = i.tm.extension_opcode;
8660 else if (!i.tm.opcode_modifier.regmem && dest < MAX_OPERANDS)
8661 {
8662 i.rm.reg = i.op[dest].regs->reg_num;
8663 set_rex_vrex (i.op[dest].regs, REX_R, i.tm.opcode_modifier.sse2avx);
8664 }
8665 else
8666 {
8667 gas_assert (source < MAX_OPERANDS);
8668 i.rm.reg = i.op[source].regs->reg_num;
8669 set_rex_vrex (i.op[source].regs, REX_R, false);
29b0f896 8670 }
ecb96e55
JB
8671
8672 if (flag_code != CODE_64BIT && (i.rex & REX_R))
8673 {
8674 gas_assert (i.types[!i.tm.opcode_modifier.regmem].bitfield.class == RegCR);
8675 i.rex &= ~REX_R;
8676 add_prefix (LOCK_PREFIX_OPCODE);
8677 }
8678
29b0f896
AM
8679 return default_seg;
8680}
252b5132 8681
48ef937e
JB
8682static INLINE void
8683frag_opcode_byte (unsigned char byte)
8684{
8685 if (now_seg != absolute_section)
8686 FRAG_APPEND_1_CHAR (byte);
8687 else
8688 ++abs_section_offset;
8689}
8690
376cd056
JB
8691static unsigned int
8692flip_code16 (unsigned int code16)
8693{
8694 gas_assert (i.tm.operands == 1);
8695
8696 return !(i.prefix[REX_PREFIX] & REX_W)
8697 && (code16 ? i.tm.operand_types[0].bitfield.disp32
376cd056
JB
8698 : i.tm.operand_types[0].bitfield.disp16)
8699 ? CODE16 : 0;
8700}
8701
29b0f896 8702static void
e3bb37b5 8703output_branch (void)
29b0f896
AM
8704{
8705 char *p;
f8a5c266 8706 int size;
29b0f896
AM
8707 int code16;
8708 int prefix;
8709 relax_substateT subtype;
8710 symbolS *sym;
8711 offsetT off;
8712
48ef937e
JB
8713 if (now_seg == absolute_section)
8714 {
8715 as_bad (_("relaxable branches not supported in absolute section"));
8716 return;
8717 }
8718
f8a5c266 8719 code16 = flag_code == CODE_16BIT ? CODE16 : 0;
1a42a9fe 8720 size = i.disp_encoding > disp_encoding_8bit ? BIG : SMALL;
29b0f896
AM
8721
8722 prefix = 0;
8723 if (i.prefix[DATA_PREFIX] != 0)
252b5132 8724 {
29b0f896
AM
8725 prefix = 1;
8726 i.prefixes -= 1;
376cd056 8727 code16 ^= flip_code16(code16);
252b5132 8728 }
29b0f896
AM
8729 /* Pentium4 branch hints. */
8730 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8731 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
2f66722d 8732 {
29b0f896
AM
8733 prefix++;
8734 i.prefixes--;
8735 }
8736 if (i.prefix[REX_PREFIX] != 0)
8737 {
8738 prefix++;
8739 i.prefixes--;
2f66722d
AM
8740 }
8741
7e8b059b
L
8742 /* BND prefixed jump. */
8743 if (i.prefix[BND_PREFIX] != 0)
8744 {
6cb0a70e
JB
8745 prefix++;
8746 i.prefixes--;
7e8b059b
L
8747 }
8748
f2810fe0 8749 if (i.prefixes != 0)
76d3f746 8750 as_warn (_("skipping prefixes on `%s'"), insn_name (&i.tm));
29b0f896
AM
8751
8752 /* It's always a symbol; End frag & setup for relax.
8753 Make sure there is enough room in this frag for the largest
8754 instruction we may generate in md_convert_frag. This is 2
8755 bytes for the opcode and room for the prefix and largest
8756 displacement. */
8757 frag_grow (prefix + 2 + 4);
8758 /* Prefix and 1 opcode byte go in fr_fix. */
8759 p = frag_more (prefix + 1);
8760 if (i.prefix[DATA_PREFIX] != 0)
8761 *p++ = DATA_PREFIX_OPCODE;
8762 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
8763 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
8764 *p++ = i.prefix[SEG_PREFIX];
6cb0a70e
JB
8765 if (i.prefix[BND_PREFIX] != 0)
8766 *p++ = BND_PREFIX_OPCODE;
29b0f896
AM
8767 if (i.prefix[REX_PREFIX] != 0)
8768 *p++ = i.prefix[REX_PREFIX];
8769 *p = i.tm.base_opcode;
8770
8771 if ((unsigned char) *p == JUMP_PC_RELATIVE)
f8a5c266 8772 subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, size);
40fb9820 8773 else if (cpu_arch_flags.bitfield.cpui386)
f8a5c266 8774 subtype = ENCODE_RELAX_STATE (COND_JUMP, size);
29b0f896 8775 else
f8a5c266 8776 subtype = ENCODE_RELAX_STATE (COND_JUMP86, size);
29b0f896 8777 subtype |= code16;
3e73aa7c 8778
29b0f896
AM
8779 sym = i.op[0].disps->X_add_symbol;
8780 off = i.op[0].disps->X_add_number;
3e73aa7c 8781
29b0f896
AM
8782 if (i.op[0].disps->X_op != O_constant
8783 && i.op[0].disps->X_op != O_symbol)
3e73aa7c 8784 {
29b0f896
AM
8785 /* Handle complex expressions. */
8786 sym = make_expr_symbol (i.op[0].disps);
8787 off = 0;
8788 }
3e73aa7c 8789
1ef3994a
JB
8790 frag_now->tc_frag_data.code64 = flag_code == CODE_64BIT;
8791
29b0f896
AM
8792 /* 1 possible extra opcode + 4 byte displacement go in var part.
8793 Pass reloc in fr_var. */
d258b828 8794 frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
29b0f896 8795}
3e73aa7c 8796
bd7ab16b
L
8797#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8798/* Return TRUE iff PLT32 relocation should be used for branching to
8799 symbol S. */
8800
5b7c81bd 8801static bool
bd7ab16b
L
8802need_plt32_p (symbolS *s)
8803{
8804 /* PLT32 relocation is ELF only. */
8805 if (!IS_ELF)
5b7c81bd 8806 return false;
bd7ab16b 8807
a5def729
RO
8808#ifdef TE_SOLARIS
8809 /* Don't emit PLT32 relocation on Solaris: neither native linker nor
8810 krtld support it. */
5b7c81bd 8811 return false;
a5def729
RO
8812#endif
8813
bd7ab16b
L
8814 /* Since there is no need to prepare for PLT branch on x86-64, we
8815 can generate R_X86_64_PLT32, instead of R_X86_64_PC32, which can
8816 be used as a marker for 32-bit PC-relative branches. */
8817 if (!object_64bit)
5b7c81bd 8818 return false;
bd7ab16b 8819
44365e88 8820 if (s == NULL)
5b7c81bd 8821 return false;
44365e88 8822
bd7ab16b
L
8823 /* Weak or undefined symbol need PLT32 relocation. */
8824 if (S_IS_WEAK (s) || !S_IS_DEFINED (s))
5b7c81bd 8825 return true;
bd7ab16b
L
8826
8827 /* Non-global symbol doesn't need PLT32 relocation. */
8828 if (! S_IS_EXTERNAL (s))
5b7c81bd 8829 return false;
bd7ab16b
L
8830
8831 /* Other global symbols need PLT32 relocation. NB: Symbol with
8832 non-default visibilities are treated as normal global symbol
8833 so that PLT32 relocation can be used as a marker for 32-bit
8834 PC-relative branches. It is useful for linker relaxation. */
5b7c81bd 8835 return true;
bd7ab16b
L
8836}
8837#endif
8838
29b0f896 8839static void
e3bb37b5 8840output_jump (void)
29b0f896
AM
8841{
8842 char *p;
8843 int size;
3e02c1cc 8844 fixS *fixP;
bd7ab16b 8845 bfd_reloc_code_real_type jump_reloc = i.reloc[0];
29b0f896 8846
0cfa3eb3 8847 if (i.tm.opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
8848 {
8849 /* This is a loop or jecxz type instruction. */
8850 size = 1;
8851 if (i.prefix[ADDR_PREFIX] != 0)
8852 {
48ef937e 8853 frag_opcode_byte (ADDR_PREFIX_OPCODE);
29b0f896
AM
8854 i.prefixes -= 1;
8855 }
8856 /* Pentium4 branch hints. */
8857 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8858 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
8859 {
48ef937e 8860 frag_opcode_byte (i.prefix[SEG_PREFIX]);
29b0f896 8861 i.prefixes--;
3e73aa7c
JH
8862 }
8863 }
29b0f896
AM
8864 else
8865 {
8866 int code16;
3e73aa7c 8867
29b0f896
AM
8868 code16 = 0;
8869 if (flag_code == CODE_16BIT)
8870 code16 = CODE16;
3e73aa7c 8871
29b0f896
AM
8872 if (i.prefix[DATA_PREFIX] != 0)
8873 {
48ef937e 8874 frag_opcode_byte (DATA_PREFIX_OPCODE);
29b0f896 8875 i.prefixes -= 1;
376cd056 8876 code16 ^= flip_code16(code16);
29b0f896 8877 }
252b5132 8878
29b0f896
AM
8879 size = 4;
8880 if (code16)
8881 size = 2;
8882 }
9fcc94b6 8883
6cb0a70e
JB
8884 /* BND prefixed jump. */
8885 if (i.prefix[BND_PREFIX] != 0)
29b0f896 8886 {
48ef937e 8887 frag_opcode_byte (i.prefix[BND_PREFIX]);
29b0f896
AM
8888 i.prefixes -= 1;
8889 }
252b5132 8890
6cb0a70e 8891 if (i.prefix[REX_PREFIX] != 0)
7e8b059b 8892 {
48ef937e 8893 frag_opcode_byte (i.prefix[REX_PREFIX]);
7e8b059b
L
8894 i.prefixes -= 1;
8895 }
8896
f2810fe0 8897 if (i.prefixes != 0)
76d3f746 8898 as_warn (_("skipping prefixes on `%s'"), insn_name (&i.tm));
e0890092 8899
48ef937e
JB
8900 if (now_seg == absolute_section)
8901 {
9a182d04 8902 abs_section_offset += i.opcode_length + size;
48ef937e
JB
8903 return;
8904 }
8905
9a182d04
JB
8906 p = frag_more (i.opcode_length + size);
8907 switch (i.opcode_length)
42164a71
L
8908 {
8909 case 2:
8910 *p++ = i.tm.base_opcode >> 8;
1a0670f3 8911 /* Fall through. */
42164a71
L
8912 case 1:
8913 *p++ = i.tm.base_opcode;
8914 break;
8915 default:
8916 abort ();
8917 }
e0890092 8918
bd7ab16b 8919#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1ef3994a
JB
8920 if (flag_code == CODE_64BIT && size == 4
8921 && jump_reloc == NO_RELOC && i.op[0].disps->X_add_number == 0
bd7ab16b
L
8922 && need_plt32_p (i.op[0].disps->X_add_symbol))
8923 jump_reloc = BFD_RELOC_X86_64_PLT32;
8924#endif
8925
8926 jump_reloc = reloc (size, 1, 1, jump_reloc);
8927
3e02c1cc 8928 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
bd7ab16b 8929 i.op[0].disps, 1, jump_reloc);
3e02c1cc 8930
eb19308f
JB
8931 /* All jumps handled here are signed, but don't unconditionally use a
8932 signed limit check for 32 and 16 bit jumps as we want to allow wrap
8933 around at 4G (outside of 64-bit mode) and 64k (except for XBEGIN)
8934 respectively. */
8935 switch (size)
8936 {
8937 case 1:
8938 fixP->fx_signed = 1;
8939 break;
8940
8941 case 2:
7fc69528 8942 if (i.tm.mnem_off == MN_xbegin)
eb19308f
JB
8943 fixP->fx_signed = 1;
8944 break;
8945
8946 case 4:
8947 if (flag_code == CODE_64BIT)
8948 fixP->fx_signed = 1;
8949 break;
8950 }
29b0f896 8951}
e0890092 8952
29b0f896 8953static void
e3bb37b5 8954output_interseg_jump (void)
29b0f896
AM
8955{
8956 char *p;
8957 int size;
8958 int prefix;
8959 int code16;
252b5132 8960
29b0f896
AM
8961 code16 = 0;
8962 if (flag_code == CODE_16BIT)
8963 code16 = CODE16;
a217f122 8964
29b0f896
AM
8965 prefix = 0;
8966 if (i.prefix[DATA_PREFIX] != 0)
8967 {
8968 prefix = 1;
8969 i.prefixes -= 1;
8970 code16 ^= CODE16;
8971 }
6cb0a70e
JB
8972
8973 gas_assert (!i.prefix[REX_PREFIX]);
252b5132 8974
29b0f896
AM
8975 size = 4;
8976 if (code16)
8977 size = 2;
252b5132 8978
f2810fe0 8979 if (i.prefixes != 0)
76d3f746 8980 as_warn (_("skipping prefixes on `%s'"), insn_name (&i.tm));
252b5132 8981
48ef937e
JB
8982 if (now_seg == absolute_section)
8983 {
8984 abs_section_offset += prefix + 1 + 2 + size;
8985 return;
8986 }
8987
29b0f896
AM
8988 /* 1 opcode; 2 segment; offset */
8989 p = frag_more (prefix + 1 + 2 + size);
3e73aa7c 8990
29b0f896
AM
8991 if (i.prefix[DATA_PREFIX] != 0)
8992 *p++ = DATA_PREFIX_OPCODE;
252b5132 8993
29b0f896
AM
8994 if (i.prefix[REX_PREFIX] != 0)
8995 *p++ = i.prefix[REX_PREFIX];
252b5132 8996
29b0f896
AM
8997 *p++ = i.tm.base_opcode;
8998 if (i.op[1].imms->X_op == O_constant)
8999 {
9000 offsetT n = i.op[1].imms->X_add_number;
252b5132 9001
29b0f896
AM
9002 if (size == 2
9003 && !fits_in_unsigned_word (n)
9004 && !fits_in_signed_word (n))
9005 {
9006 as_bad (_("16-bit jump out of range"));
9007 return;
9008 }
9009 md_number_to_chars (p, n, size);
9010 }
9011 else
9012 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
d258b828 9013 i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
6d96a594
C
9014
9015 p += size;
9016 if (i.op[0].imms->X_op == O_constant)
9017 md_number_to_chars (p, (valueT) i.op[0].imms->X_add_number, 2);
9018 else
9019 fix_new_exp (frag_now, p - frag_now->fr_literal, 2,
9020 i.op[0].imms, 0, reloc (2, 0, 0, i.reloc[0]));
29b0f896 9021}
a217f122 9022
b4a3a7b4
L
9023#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9024void
9025x86_cleanup (void)
9026{
9027 char *p;
9028 asection *seg = now_seg;
9029 subsegT subseg = now_subseg;
9030 asection *sec;
9031 unsigned int alignment, align_size_1;
9032 unsigned int isa_1_descsz, feature_2_descsz, descsz;
9033 unsigned int isa_1_descsz_raw, feature_2_descsz_raw;
9034 unsigned int padding;
9035
1273b2f8 9036 if (!IS_ELF || !x86_used_note)
b4a3a7b4
L
9037 return;
9038
b4a3a7b4
L
9039 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X86;
9040
9041 /* The .note.gnu.property section layout:
9042
9043 Field Length Contents
9044 ---- ---- ----
9045 n_namsz 4 4
9046 n_descsz 4 The note descriptor size
9047 n_type 4 NT_GNU_PROPERTY_TYPE_0
9048 n_name 4 "GNU"
9049 n_desc n_descsz The program property array
9050 .... .... ....
9051 */
9052
9053 /* Create the .note.gnu.property section. */
9054 sec = subseg_new (NOTE_GNU_PROPERTY_SECTION_NAME, 0);
fd361982 9055 bfd_set_section_flags (sec,
b4a3a7b4
L
9056 (SEC_ALLOC
9057 | SEC_LOAD
9058 | SEC_DATA
9059 | SEC_HAS_CONTENTS
9060 | SEC_READONLY));
9061
9062 if (get_elf_backend_data (stdoutput)->s->elfclass == ELFCLASS64)
9063 {
9064 align_size_1 = 7;
9065 alignment = 3;
9066 }
9067 else
9068 {
9069 align_size_1 = 3;
9070 alignment = 2;
9071 }
9072
fd361982 9073 bfd_set_section_alignment (sec, alignment);
b4a3a7b4
L
9074 elf_section_type (sec) = SHT_NOTE;
9075
1273b2f8
L
9076 /* GNU_PROPERTY_X86_ISA_1_USED: 4-byte type + 4-byte data size
9077 + 4-byte data */
9078 isa_1_descsz_raw = 4 + 4 + 4;
9079 /* Align GNU_PROPERTY_X86_ISA_1_USED. */
9080 isa_1_descsz = (isa_1_descsz_raw + align_size_1) & ~align_size_1;
b4a3a7b4
L
9081
9082 feature_2_descsz_raw = isa_1_descsz;
9083 /* GNU_PROPERTY_X86_FEATURE_2_USED: 4-byte type + 4-byte data size
1273b2f8 9084 + 4-byte data */
b4a3a7b4
L
9085 feature_2_descsz_raw += 4 + 4 + 4;
9086 /* Align GNU_PROPERTY_X86_FEATURE_2_USED. */
9087 feature_2_descsz = ((feature_2_descsz_raw + align_size_1)
9088 & ~align_size_1);
9089
9090 descsz = feature_2_descsz;
9091 /* Section size: n_namsz + n_descsz + n_type + n_name + n_descsz. */
9092 p = frag_more (4 + 4 + 4 + 4 + descsz);
9093
9094 /* Write n_namsz. */
9095 md_number_to_chars (p, (valueT) 4, 4);
9096
9097 /* Write n_descsz. */
9098 md_number_to_chars (p + 4, (valueT) descsz, 4);
9099
9100 /* Write n_type. */
9101 md_number_to_chars (p + 4 * 2, (valueT) NT_GNU_PROPERTY_TYPE_0, 4);
9102
9103 /* Write n_name. */
9104 memcpy (p + 4 * 3, "GNU", 4);
9105
1273b2f8
L
9106 /* Write 4-byte type. */
9107 md_number_to_chars (p + 4 * 4,
9108 (valueT) GNU_PROPERTY_X86_ISA_1_USED, 4);
b4a3a7b4 9109
1273b2f8
L
9110 /* Write 4-byte data size. */
9111 md_number_to_chars (p + 4 * 5, (valueT) 4, 4);
b4a3a7b4 9112
1273b2f8
L
9113 /* Write 4-byte data. */
9114 md_number_to_chars (p + 4 * 6, (valueT) x86_isa_1_used, 4);
b4a3a7b4 9115
1273b2f8
L
9116 /* Zero out paddings. */
9117 padding = isa_1_descsz - isa_1_descsz_raw;
9118 if (padding)
9119 memset (p + 4 * 7, 0, padding);
b4a3a7b4
L
9120
9121 /* Write 4-byte type. */
9122 md_number_to_chars (p + isa_1_descsz + 4 * 4,
9123 (valueT) GNU_PROPERTY_X86_FEATURE_2_USED, 4);
9124
9125 /* Write 4-byte data size. */
9126 md_number_to_chars (p + isa_1_descsz + 4 * 5, (valueT) 4, 4);
9127
9128 /* Write 4-byte data. */
9129 md_number_to_chars (p + isa_1_descsz + 4 * 6,
9130 (valueT) x86_feature_2_used, 4);
9131
9132 /* Zero out paddings. */
9133 padding = feature_2_descsz - feature_2_descsz_raw;
9134 if (padding)
9135 memset (p + isa_1_descsz + 4 * 7, 0, padding);
9136
9137 /* We probably can't restore the current segment, for there likely
9138 isn't one yet... */
9139 if (seg && subseg)
9140 subseg_set (seg, subseg);
9141}
b52c4ee4
IB
9142
9143bool
9144x86_support_sframe_p (void)
9145{
3e3e792a 9146 /* At this time, SFrame stack trace is supported for AMD64 ABI only. */
b52c4ee4
IB
9147 return (x86_elf_abi == X86_64_ABI);
9148}
9149
9150bool
9151x86_sframe_ra_tracking_p (void)
9152{
9153 /* In AMD64, return address is always stored on the stack at a fixed offset
9154 from the CFA (provided via x86_sframe_cfa_ra_offset ()).
9155 Do not track explicitly via an SFrame Frame Row Entry. */
9156 return false;
9157}
9158
9159offsetT
9160x86_sframe_cfa_ra_offset (void)
9161{
9162 gas_assert (x86_elf_abi == X86_64_ABI);
9163 return (offsetT) -8;
9164}
9165
9166unsigned char
9167x86_sframe_get_abi_arch (void)
9168{
9169 unsigned char sframe_abi_arch = 0;
9170
9171 if (x86_support_sframe_p ())
9172 {
9173 gas_assert (!target_big_endian);
9174 sframe_abi_arch = SFRAME_ABI_AMD64_ENDIAN_LITTLE;
9175 }
9176
9177 return sframe_abi_arch;
9178}
9179
b4a3a7b4
L
9180#endif
9181
9c33702b
JB
9182static unsigned int
9183encoding_length (const fragS *start_frag, offsetT start_off,
9184 const char *frag_now_ptr)
9185{
9186 unsigned int len = 0;
9187
9188 if (start_frag != frag_now)
9189 {
9190 const fragS *fr = start_frag;
9191
9192 do {
9193 len += fr->fr_fix;
9194 fr = fr->fr_next;
9195 } while (fr && fr != frag_now);
9196 }
9197
9198 return len - start_off + (frag_now_ptr - frag_now->fr_literal);
9199}
9200
e379e5f3 9201/* Return 1 for test, and, cmp, add, sub, inc and dec which may
79d72f45
HL
9202 be macro-fused with conditional jumps.
9203 NB: If TEST/AND/CMP/ADD/SUB/INC/DEC is of RIP relative address,
9204 or is one of the following format:
9205
9206 cmp m, imm
9207 add m, imm
9208 sub m, imm
9209 test m, imm
9210 and m, imm
9211 inc m
9212 dec m
9213
9214 it is unfusible. */
e379e5f3
L
9215
9216static int
79d72f45 9217maybe_fused_with_jcc_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
9218{
9219 /* No RIP address. */
9220 if (i.base_reg && i.base_reg->reg_num == RegIP)
9221 return 0;
9222
389d00a5 9223 /* No opcodes outside of base encoding space. */
ddb62495 9224 if (i.tm.opcode_space != SPACE_BASE)
e379e5f3
L
9225 return 0;
9226
79d72f45
HL
9227 /* add, sub without add/sub m, imm. */
9228 if (i.tm.base_opcode <= 5
e379e5f3
L
9229 || (i.tm.base_opcode >= 0x28 && i.tm.base_opcode <= 0x2d)
9230 || ((i.tm.base_opcode | 3) == 0x83
79d72f45 9231 && (i.tm.extension_opcode == 0x5
e379e5f3 9232 || i.tm.extension_opcode == 0x0)))
79d72f45
HL
9233 {
9234 *mf_cmp_p = mf_cmp_alu_cmp;
9235 return !(i.mem_operands && i.imm_operands);
9236 }
e379e5f3 9237
79d72f45
HL
9238 /* and without and m, imm. */
9239 if ((i.tm.base_opcode >= 0x20 && i.tm.base_opcode <= 0x25)
9240 || ((i.tm.base_opcode | 3) == 0x83
9241 && i.tm.extension_opcode == 0x4))
9242 {
9243 *mf_cmp_p = mf_cmp_test_and;
9244 return !(i.mem_operands && i.imm_operands);
9245 }
9246
9247 /* test without test m imm. */
e379e5f3
L
9248 if ((i.tm.base_opcode | 1) == 0x85
9249 || (i.tm.base_opcode | 1) == 0xa9
9250 || ((i.tm.base_opcode | 1) == 0xf7
79d72f45
HL
9251 && i.tm.extension_opcode == 0))
9252 {
9253 *mf_cmp_p = mf_cmp_test_and;
9254 return !(i.mem_operands && i.imm_operands);
9255 }
9256
9257 /* cmp without cmp m, imm. */
9258 if ((i.tm.base_opcode >= 0x38 && i.tm.base_opcode <= 0x3d)
e379e5f3
L
9259 || ((i.tm.base_opcode | 3) == 0x83
9260 && (i.tm.extension_opcode == 0x7)))
79d72f45
HL
9261 {
9262 *mf_cmp_p = mf_cmp_alu_cmp;
9263 return !(i.mem_operands && i.imm_operands);
9264 }
e379e5f3 9265
79d72f45 9266 /* inc, dec without inc/dec m. */
e379e5f3
L
9267 if ((i.tm.cpu_flags.bitfield.cpuno64
9268 && (i.tm.base_opcode | 0xf) == 0x4f)
9269 || ((i.tm.base_opcode | 1) == 0xff
9270 && i.tm.extension_opcode <= 0x1))
79d72f45
HL
9271 {
9272 *mf_cmp_p = mf_cmp_incdec;
9273 return !i.mem_operands;
9274 }
e379e5f3
L
9275
9276 return 0;
9277}
9278
9279/* Return 1 if a FUSED_JCC_PADDING frag should be generated. */
9280
9281static int
79d72f45 9282add_fused_jcc_padding_frag_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
9283{
9284 /* NB: Don't work with COND_JUMP86 without i386. */
9285 if (!align_branch_power
9286 || now_seg == absolute_section
9287 || !cpu_arch_flags.bitfield.cpui386
9288 || !(align_branch & align_branch_fused_bit))
9289 return 0;
9290
79d72f45 9291 if (maybe_fused_with_jcc_p (mf_cmp_p))
e379e5f3
L
9292 {
9293 if (last_insn.kind == last_insn_other
9294 || last_insn.seg != now_seg)
9295 return 1;
9296 if (flag_debug)
9297 as_warn_where (last_insn.file, last_insn.line,
9298 _("`%s` skips -malign-branch-boundary on `%s`"),
76d3f746 9299 last_insn.name, insn_name (&i.tm));
e379e5f3
L
9300 }
9301
9302 return 0;
9303}
9304
9305/* Return 1 if a BRANCH_PREFIX frag should be generated. */
9306
9307static int
9308add_branch_prefix_frag_p (void)
9309{
9310 /* NB: Don't work with COND_JUMP86 without i386. Don't add prefix
9311 to PadLock instructions since they include prefixes in opcode. */
9312 if (!align_branch_power
9313 || !align_branch_prefix_size
9314 || now_seg == absolute_section
9315 || i.tm.cpu_flags.bitfield.cpupadlock
9316 || !cpu_arch_flags.bitfield.cpui386)
9317 return 0;
9318
9319 /* Don't add prefix if it is a prefix or there is no operand in case
9320 that segment prefix is special. */
9321 if (!i.operands || i.tm.opcode_modifier.isprefix)
9322 return 0;
9323
9324 if (last_insn.kind == last_insn_other
9325 || last_insn.seg != now_seg)
9326 return 1;
9327
9328 if (flag_debug)
9329 as_warn_where (last_insn.file, last_insn.line,
9330 _("`%s` skips -malign-branch-boundary on `%s`"),
76d3f746 9331 last_insn.name, insn_name (&i.tm));
e379e5f3
L
9332
9333 return 0;
9334}
9335
9336/* Return 1 if a BRANCH_PADDING frag should be generated. */
9337
9338static int
79d72f45
HL
9339add_branch_padding_frag_p (enum align_branch_kind *branch_p,
9340 enum mf_jcc_kind *mf_jcc_p)
e379e5f3
L
9341{
9342 int add_padding;
9343
9344 /* NB: Don't work with COND_JUMP86 without i386. */
9345 if (!align_branch_power
9346 || now_seg == absolute_section
389d00a5 9347 || !cpu_arch_flags.bitfield.cpui386
ddb62495 9348 || i.tm.opcode_space != SPACE_BASE)
e379e5f3
L
9349 return 0;
9350
9351 add_padding = 0;
9352
9353 /* Check for jcc and direct jmp. */
9354 if (i.tm.opcode_modifier.jump == JUMP)
9355 {
9356 if (i.tm.base_opcode == JUMP_PC_RELATIVE)
9357 {
9358 *branch_p = align_branch_jmp;
9359 add_padding = align_branch & align_branch_jmp_bit;
9360 }
9361 else
9362 {
79d72f45
HL
9363 /* Because J<cc> and JN<cc> share same group in macro-fusible table,
9364 igore the lowest bit. */
9365 *mf_jcc_p = (i.tm.base_opcode & 0x0e) >> 1;
e379e5f3
L
9366 *branch_p = align_branch_jcc;
9367 if ((align_branch & align_branch_jcc_bit))
9368 add_padding = 1;
9369 }
9370 }
e379e5f3
L
9371 else if ((i.tm.base_opcode | 1) == 0xc3)
9372 {
9373 /* Near ret. */
9374 *branch_p = align_branch_ret;
9375 if ((align_branch & align_branch_ret_bit))
9376 add_padding = 1;
9377 }
9378 else
9379 {
9380 /* Check for indirect jmp, direct and indirect calls. */
9381 if (i.tm.base_opcode == 0xe8)
9382 {
9383 /* Direct call. */
9384 *branch_p = align_branch_call;
9385 if ((align_branch & align_branch_call_bit))
9386 add_padding = 1;
9387 }
9388 else if (i.tm.base_opcode == 0xff
9389 && (i.tm.extension_opcode == 2
9390 || i.tm.extension_opcode == 4))
9391 {
9392 /* Indirect call and jmp. */
9393 *branch_p = align_branch_indirect;
9394 if ((align_branch & align_branch_indirect_bit))
9395 add_padding = 1;
9396 }
9397
9398 if (add_padding
9399 && i.disp_operands
9400 && tls_get_addr
9401 && (i.op[0].disps->X_op == O_symbol
9402 || (i.op[0].disps->X_op == O_subtract
9403 && i.op[0].disps->X_op_symbol == GOT_symbol)))
9404 {
9405 symbolS *s = i.op[0].disps->X_add_symbol;
9406 /* No padding to call to global or undefined tls_get_addr. */
9407 if ((S_IS_EXTERNAL (s) || !S_IS_DEFINED (s))
9408 && strcmp (S_GET_NAME (s), tls_get_addr) == 0)
9409 return 0;
9410 }
9411 }
9412
9413 if (add_padding
9414 && last_insn.kind != last_insn_other
9415 && last_insn.seg == now_seg)
9416 {
9417 if (flag_debug)
9418 as_warn_where (last_insn.file, last_insn.line,
9419 _("`%s` skips -malign-branch-boundary on `%s`"),
76d3f746 9420 last_insn.name, insn_name (&i.tm));
e379e5f3
L
9421 return 0;
9422 }
9423
9424 return add_padding;
9425}
9426
29b0f896 9427static void
e3bb37b5 9428output_insn (void)
29b0f896 9429{
2bbd9c25
JJ
9430 fragS *insn_start_frag;
9431 offsetT insn_start_off;
e379e5f3
L
9432 fragS *fragP = NULL;
9433 enum align_branch_kind branch = align_branch_none;
79d72f45
HL
9434 /* The initializer is arbitrary just to avoid uninitialized error.
9435 it's actually either assigned in add_branch_padding_frag_p
9436 or never be used. */
9437 enum mf_jcc_kind mf_jcc = mf_jcc_jo;
2bbd9c25 9438
b4a3a7b4 9439#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
48ef937e 9440 if (IS_ELF && x86_used_note && now_seg != absolute_section)
b4a3a7b4 9441 {
32930e4e
L
9442 if ((i.xstate & xstate_tmm) == xstate_tmm
9443 || i.tm.cpu_flags.bitfield.cpuamx_tile)
9444 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_TMM;
9445
b4a3a7b4
L
9446 if (i.tm.cpu_flags.bitfield.cpu8087
9447 || i.tm.cpu_flags.bitfield.cpu287
9448 || i.tm.cpu_flags.bitfield.cpu387
9449 || i.tm.cpu_flags.bitfield.cpu687
9450 || i.tm.cpu_flags.bitfield.cpufisttp)
9451 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X87;
014d61ea 9452
921eafea 9453 if ((i.xstate & xstate_mmx)
7fc69528
JB
9454 || i.tm.mnem_off == MN_emms
9455 || i.tm.mnem_off == MN_femms)
b4a3a7b4 9456 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MMX;
014d61ea 9457
32930e4e
L
9458 if (i.index_reg)
9459 {
9460 if (i.index_reg->reg_type.bitfield.zmmword)
9461 i.xstate |= xstate_zmm;
9462 else if (i.index_reg->reg_type.bitfield.ymmword)
9463 i.xstate |= xstate_ymm;
9464 else if (i.index_reg->reg_type.bitfield.xmmword)
9465 i.xstate |= xstate_xmm;
9466 }
014d61ea
JB
9467
9468 /* vzeroall / vzeroupper */
9469 if (i.tm.base_opcode == 0x77 && i.tm.cpu_flags.bitfield.cpuavx)
9470 i.xstate |= xstate_ymm;
9471
c4694f17 9472 if ((i.xstate & xstate_xmm)
389d00a5
JB
9473 /* ldmxcsr / stmxcsr / vldmxcsr / vstmxcsr */
9474 || (i.tm.base_opcode == 0xae
9475 && (i.tm.cpu_flags.bitfield.cpusse
9476 || i.tm.cpu_flags.bitfield.cpuavx))
c4694f17
TG
9477 || i.tm.cpu_flags.bitfield.cpuwidekl
9478 || i.tm.cpu_flags.bitfield.cpukl)
b4a3a7b4 9479 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XMM;
014d61ea 9480
921eafea 9481 if ((i.xstate & xstate_ymm) == xstate_ymm)
b4a3a7b4 9482 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_YMM;
921eafea 9483 if ((i.xstate & xstate_zmm) == xstate_zmm)
b4a3a7b4 9484 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_ZMM;
6225c532 9485 if (i.mask.reg || (i.xstate & xstate_mask) == xstate_mask)
32930e4e 9486 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MASK;
b4a3a7b4
L
9487 if (i.tm.cpu_flags.bitfield.cpufxsr)
9488 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_FXSR;
9489 if (i.tm.cpu_flags.bitfield.cpuxsave)
9490 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVE;
9491 if (i.tm.cpu_flags.bitfield.cpuxsaveopt)
9492 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT;
9493 if (i.tm.cpu_flags.bitfield.cpuxsavec)
9494 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEC;
b0ab0693
L
9495
9496 if (x86_feature_2_used
9497 || i.tm.cpu_flags.bitfield.cpucmov
9498 || i.tm.cpu_flags.bitfield.cpusyscall
7fc69528 9499 || i.tm.mnem_off == MN_cmpxchg8b)
b0ab0693
L
9500 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_BASELINE;
9501 if (i.tm.cpu_flags.bitfield.cpusse3
9502 || i.tm.cpu_flags.bitfield.cpussse3
9503 || i.tm.cpu_flags.bitfield.cpusse4_1
9504 || i.tm.cpu_flags.bitfield.cpusse4_2
9505 || i.tm.cpu_flags.bitfield.cpucx16
9506 || i.tm.cpu_flags.bitfield.cpupopcnt
9507 /* LAHF-SAHF insns in 64-bit mode. */
9508 || (flag_code == CODE_64BIT
35648716 9509 && (i.tm.base_opcode | 1) == 0x9f
ddb62495 9510 && i.tm.opcode_space == SPACE_BASE))
b0ab0693
L
9511 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V2;
9512 if (i.tm.cpu_flags.bitfield.cpuavx
9513 || i.tm.cpu_flags.bitfield.cpuavx2
a9860005
JB
9514 /* Any VEX encoded insns execpt for AVX512F, AVX512BW, AVX512DQ,
9515 XOP, FMA4, LPW, TBM, and AMX. */
b0ab0693
L
9516 || (i.tm.opcode_modifier.vex
9517 && !i.tm.cpu_flags.bitfield.cpuavx512f
9518 && !i.tm.cpu_flags.bitfield.cpuavx512bw
9519 && !i.tm.cpu_flags.bitfield.cpuavx512dq
a9860005
JB
9520 && !i.tm.cpu_flags.bitfield.cpuxop
9521 && !i.tm.cpu_flags.bitfield.cpufma4
b0ab0693
L
9522 && !i.tm.cpu_flags.bitfield.cpulwp
9523 && !i.tm.cpu_flags.bitfield.cputbm
9524 && !(x86_feature_2_used & GNU_PROPERTY_X86_FEATURE_2_TMM))
9525 || i.tm.cpu_flags.bitfield.cpuf16c
9526 || i.tm.cpu_flags.bitfield.cpufma
9527 || i.tm.cpu_flags.bitfield.cpulzcnt
9528 || i.tm.cpu_flags.bitfield.cpumovbe
9529 || i.tm.cpu_flags.bitfield.cpuxsaves
9530 || (x86_feature_2_used
9531 & (GNU_PROPERTY_X86_FEATURE_2_XSAVE
9532 | GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT
9533 | GNU_PROPERTY_X86_FEATURE_2_XSAVEC)) != 0)
9534 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V3;
9535 if (i.tm.cpu_flags.bitfield.cpuavx512f
9536 || i.tm.cpu_flags.bitfield.cpuavx512bw
9537 || i.tm.cpu_flags.bitfield.cpuavx512dq
9538 || i.tm.cpu_flags.bitfield.cpuavx512vl
a9860005
JB
9539 /* Any EVEX encoded insns except for AVX512ER, AVX512PF,
9540 AVX512-4FMAPS, and AVX512-4VNNIW. */
b0ab0693
L
9541 || (i.tm.opcode_modifier.evex
9542 && !i.tm.cpu_flags.bitfield.cpuavx512er
9543 && !i.tm.cpu_flags.bitfield.cpuavx512pf
a9860005 9544 && !i.tm.cpu_flags.bitfield.cpuavx512_4fmaps
b0ab0693
L
9545 && !i.tm.cpu_flags.bitfield.cpuavx512_4vnniw))
9546 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V4;
b4a3a7b4
L
9547 }
9548#endif
9549
29b0f896
AM
9550 /* Tie dwarf2 debug info to the address at the start of the insn.
9551 We can't do this after the insn has been output as the current
9552 frag may have been closed off. eg. by frag_var. */
9553 dwarf2_emit_insn (0);
9554
2bbd9c25
JJ
9555 insn_start_frag = frag_now;
9556 insn_start_off = frag_now_fix ();
9557
79d72f45 9558 if (add_branch_padding_frag_p (&branch, &mf_jcc))
e379e5f3
L
9559 {
9560 char *p;
9561 /* Branch can be 8 bytes. Leave some room for prefixes. */
9562 unsigned int max_branch_padding_size = 14;
9563
9564 /* Align section to boundary. */
9565 record_alignment (now_seg, align_branch_power);
9566
9567 /* Make room for padding. */
9568 frag_grow (max_branch_padding_size);
9569
9570 /* Start of the padding. */
9571 p = frag_more (0);
9572
9573 fragP = frag_now;
9574
9575 frag_var (rs_machine_dependent, max_branch_padding_size, 0,
9576 ENCODE_RELAX_STATE (BRANCH_PADDING, 0),
9577 NULL, 0, p);
9578
79d72f45 9579 fragP->tc_frag_data.mf_type = mf_jcc;
e379e5f3
L
9580 fragP->tc_frag_data.branch_type = branch;
9581 fragP->tc_frag_data.max_bytes = max_branch_padding_size;
9582 }
9583
d59a54c2
JB
9584 if (!cpu_arch_flags.bitfield.cpui386 && (flag_code != CODE_16BIT)
9585 && !pre_386_16bit_warned)
9586 {
9587 as_warn (_("use .code16 to ensure correct addressing mode"));
9588 pre_386_16bit_warned = true;
9589 }
9590
29b0f896 9591 /* Output jumps. */
0cfa3eb3 9592 if (i.tm.opcode_modifier.jump == JUMP)
29b0f896 9593 output_branch ();
0cfa3eb3
JB
9594 else if (i.tm.opcode_modifier.jump == JUMP_BYTE
9595 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896 9596 output_jump ();
0cfa3eb3 9597 else if (i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT)
29b0f896
AM
9598 output_interseg_jump ();
9599 else
9600 {
9601 /* Output normal instructions here. */
9602 char *p;
9603 unsigned char *q;
47465058 9604 unsigned int j;
79d72f45 9605 enum mf_cmp_kind mf_cmp;
4dffcebc 9606
e4e00185 9607 if (avoid_fence
389d00a5
JB
9608 && (i.tm.base_opcode == 0xaee8
9609 || i.tm.base_opcode == 0xaef0
9610 || i.tm.base_opcode == 0xaef8))
48ef937e
JB
9611 {
9612 /* Encode lfence, mfence, and sfence as
9613 f0 83 04 24 00 lock addl $0x0, (%{re}sp). */
47f4115a 9614 if (flag_code == CODE_16BIT)
76d3f746 9615 as_bad (_("Cannot convert `%s' in 16-bit mode"), insn_name (&i.tm));
47f4115a
JB
9616 else if (omit_lock_prefix)
9617 as_bad (_("Cannot convert `%s' with `-momit-lock-prefix=yes' in effect"),
76d3f746 9618 insn_name (&i.tm));
47f4115a 9619 else if (now_seg != absolute_section)
48ef937e
JB
9620 {
9621 offsetT val = 0x240483f0ULL;
9622
9623 p = frag_more (5);
9624 md_number_to_chars (p, val, 5);
9625 }
9626 else
9627 abs_section_offset += 5;
9628 return;
9629 }
e4e00185 9630
d022bddd
IT
9631 /* Some processors fail on LOCK prefix. This options makes
9632 assembler ignore LOCK prefix and serves as a workaround. */
9633 if (omit_lock_prefix)
9634 {
35648716
JB
9635 if (i.tm.base_opcode == LOCK_PREFIX_OPCODE
9636 && i.tm.opcode_modifier.isprefix)
d022bddd
IT
9637 return;
9638 i.prefix[LOCK_PREFIX] = 0;
9639 }
9640
e379e5f3
L
9641 if (branch)
9642 /* Skip if this is a branch. */
9643 ;
79d72f45 9644 else if (add_fused_jcc_padding_frag_p (&mf_cmp))
e379e5f3
L
9645 {
9646 /* Make room for padding. */
9647 frag_grow (MAX_FUSED_JCC_PADDING_SIZE);
9648 p = frag_more (0);
9649
9650 fragP = frag_now;
9651
9652 frag_var (rs_machine_dependent, MAX_FUSED_JCC_PADDING_SIZE, 0,
9653 ENCODE_RELAX_STATE (FUSED_JCC_PADDING, 0),
9654 NULL, 0, p);
9655
79d72f45 9656 fragP->tc_frag_data.mf_type = mf_cmp;
e379e5f3
L
9657 fragP->tc_frag_data.branch_type = align_branch_fused;
9658 fragP->tc_frag_data.max_bytes = MAX_FUSED_JCC_PADDING_SIZE;
9659 }
9660 else if (add_branch_prefix_frag_p ())
9661 {
9662 unsigned int max_prefix_size = align_branch_prefix_size;
9663
9664 /* Make room for padding. */
9665 frag_grow (max_prefix_size);
9666 p = frag_more (0);
9667
9668 fragP = frag_now;
9669
9670 frag_var (rs_machine_dependent, max_prefix_size, 0,
9671 ENCODE_RELAX_STATE (BRANCH_PREFIX, 0),
9672 NULL, 0, p);
9673
9674 fragP->tc_frag_data.max_bytes = max_prefix_size;
9675 }
9676
43234a1e
L
9677 /* Since the VEX/EVEX prefix contains the implicit prefix, we
9678 don't need the explicit prefix. */
cf665fee 9679 if (!is_any_vex_encoding (&i.tm))
bc4bd9ab 9680 {
7b47a312 9681 switch (i.tm.opcode_modifier.opcodeprefix)
bc4bd9ab 9682 {
7b47a312
L
9683 case PREFIX_0X66:
9684 add_prefix (0x66);
9685 break;
9686 case PREFIX_0XF2:
9687 add_prefix (0xf2);
9688 break;
9689 case PREFIX_0XF3:
8b65b895
L
9690 if (!i.tm.cpu_flags.bitfield.cpupadlock
9691 || (i.prefix[REP_PREFIX] != 0xf3))
9692 add_prefix (0xf3);
c0f3af97 9693 break;
7b47a312 9694 case PREFIX_NONE:
9a182d04 9695 switch (i.opcode_length)
c0f3af97 9696 {
7b47a312 9697 case 2:
7b47a312 9698 break;
9a182d04 9699 case 1:
7b47a312 9700 /* Check for pseudo prefixes. */
9a182d04
JB
9701 if (!i.tm.opcode_modifier.isprefix || i.tm.base_opcode)
9702 break;
7b47a312
L
9703 as_bad_where (insn_start_frag->fr_file,
9704 insn_start_frag->fr_line,
9705 _("pseudo prefix without instruction"));
9706 return;
9707 default:
9708 abort ();
4dffcebc 9709 }
c0f3af97 9710 break;
c0f3af97
L
9711 default:
9712 abort ();
bc4bd9ab 9713 }
c0f3af97 9714
6d19a37a 9715#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
cf61b747
L
9716 /* For x32, add a dummy REX_OPCODE prefix for mov/add with
9717 R_X86_64_GOTTPOFF relocation so that linker can safely
14470f07
L
9718 perform IE->LE optimization. A dummy REX_OPCODE prefix
9719 is also needed for lea with R_X86_64_GOTPC32_TLSDESC
9720 relocation for GDesc -> IE/LE optimization. */
cf61b747
L
9721 if (x86_elf_abi == X86_64_X32_ABI
9722 && i.operands == 2
14470f07
L
9723 && (i.reloc[0] == BFD_RELOC_X86_64_GOTTPOFF
9724 || i.reloc[0] == BFD_RELOC_X86_64_GOTPC32_TLSDESC)
cf61b747
L
9725 && i.prefix[REX_PREFIX] == 0)
9726 add_prefix (REX_OPCODE);
6d19a37a 9727#endif
cf61b747 9728
c0f3af97
L
9729 /* The prefix bytes. */
9730 for (j = ARRAY_SIZE (i.prefix), q = i.prefix; j > 0; j--, q++)
9731 if (*q)
48ef937e 9732 frag_opcode_byte (*q);
0f10071e 9733 }
ae5c1c7b 9734 else
c0f3af97
L
9735 {
9736 for (j = 0, q = i.prefix; j < ARRAY_SIZE (i.prefix); j++, q++)
9737 if (*q)
9738 switch (j)
9739 {
c0f3af97
L
9740 case SEG_PREFIX:
9741 case ADDR_PREFIX:
48ef937e 9742 frag_opcode_byte (*q);
c0f3af97
L
9743 break;
9744 default:
9745 /* There should be no other prefixes for instructions
9746 with VEX prefix. */
9747 abort ();
9748 }
9749
43234a1e
L
9750 /* For EVEX instructions i.vrex should become 0 after
9751 build_evex_prefix. For VEX instructions upper 16 registers
9752 aren't available, so VREX should be 0. */
9753 if (i.vrex)
9754 abort ();
c0f3af97 9755 /* Now the VEX prefix. */
48ef937e
JB
9756 if (now_seg != absolute_section)
9757 {
9758 p = frag_more (i.vex.length);
9759 for (j = 0; j < i.vex.length; j++)
9760 p[j] = i.vex.bytes[j];
9761 }
9762 else
9763 abs_section_offset += i.vex.length;
c0f3af97 9764 }
252b5132 9765
29b0f896 9766 /* Now the opcode; be careful about word order here! */
389d00a5
JB
9767 j = i.opcode_length;
9768 if (!i.vex.length)
ddb62495 9769 switch (i.tm.opcode_space)
389d00a5
JB
9770 {
9771 case SPACE_BASE:
9772 break;
9773 case SPACE_0F:
9774 ++j;
9775 break;
9776 case SPACE_0F38:
9777 case SPACE_0F3A:
9778 j += 2;
9779 break;
9780 default:
9781 abort ();
9782 }
9783
48ef937e 9784 if (now_seg == absolute_section)
389d00a5
JB
9785 abs_section_offset += j;
9786 else if (j == 1)
29b0f896
AM
9787 {
9788 FRAG_APPEND_1_CHAR (i.tm.base_opcode);
9789 }
9790 else
9791 {
389d00a5
JB
9792 p = frag_more (j);
9793 if (!i.vex.length
ddb62495 9794 && i.tm.opcode_space != SPACE_BASE)
389d00a5
JB
9795 {
9796 *p++ = 0x0f;
ddb62495
JB
9797 if (i.tm.opcode_space != SPACE_0F)
9798 *p++ = i.tm.opcode_space == SPACE_0F38
389d00a5
JB
9799 ? 0x38 : 0x3a;
9800 }
9801
9a182d04 9802 switch (i.opcode_length)
331d2d0d 9803 {
4dffcebc 9804 case 2:
389d00a5
JB
9805 /* Put out high byte first: can't use md_number_to_chars! */
9806 *p++ = (i.tm.base_opcode >> 8) & 0xff;
9807 /* Fall through. */
9808 case 1:
9809 *p = i.tm.base_opcode & 0xff;
4dffcebc
L
9810 break;
9811 default:
9812 abort ();
9813 break;
331d2d0d 9814 }
0f10071e 9815
29b0f896 9816 }
3e73aa7c 9817
29b0f896 9818 /* Now the modrm byte and sib byte (if present). */
40fb9820 9819 if (i.tm.opcode_modifier.modrm)
29b0f896 9820 {
48ef937e
JB
9821 frag_opcode_byte ((i.rm.regmem << 0)
9822 | (i.rm.reg << 3)
9823 | (i.rm.mode << 6));
29b0f896
AM
9824 /* If i.rm.regmem == ESP (4)
9825 && i.rm.mode != (Register mode)
9826 && not 16 bit
9827 ==> need second modrm byte. */
9828 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
9829 && i.rm.mode != 3
dc821c5f 9830 && !(i.base_reg && i.base_reg->reg_type.bitfield.word))
48ef937e
JB
9831 frag_opcode_byte ((i.sib.base << 0)
9832 | (i.sib.index << 3)
9833 | (i.sib.scale << 6));
29b0f896 9834 }
3e73aa7c 9835
29b0f896 9836 if (i.disp_operands)
2bbd9c25 9837 output_disp (insn_start_frag, insn_start_off);
3e73aa7c 9838
29b0f896 9839 if (i.imm_operands)
2bbd9c25 9840 output_imm (insn_start_frag, insn_start_off);
9c33702b
JB
9841
9842 /*
9843 * frag_now_fix () returning plain abs_section_offset when we're in the
9844 * absolute section, and abs_section_offset not getting updated as data
9845 * gets added to the frag breaks the logic below.
9846 */
9847 if (now_seg != absolute_section)
9848 {
9849 j = encoding_length (insn_start_frag, insn_start_off, frag_more (0));
9850 if (j > 15)
9851 as_warn (_("instruction length of %u bytes exceeds the limit of 15"),
9852 j);
e379e5f3
L
9853 else if (fragP)
9854 {
9855 /* NB: Don't add prefix with GOTPC relocation since
9856 output_disp() above depends on the fixed encoding
9857 length. Can't add prefix with TLS relocation since
9858 it breaks TLS linker optimization. */
9859 unsigned int max = i.has_gotpc_tls_reloc ? 0 : 15 - j;
9860 /* Prefix count on the current instruction. */
9861 unsigned int count = i.vex.length;
9862 unsigned int k;
9863 for (k = 0; k < ARRAY_SIZE (i.prefix); k++)
9864 /* REX byte is encoded in VEX/EVEX prefix. */
9865 if (i.prefix[k] && (k != REX_PREFIX || !i.vex.length))
9866 count++;
9867
9868 /* Count prefixes for extended opcode maps. */
9869 if (!i.vex.length)
ddb62495 9870 switch (i.tm.opcode_space)
e379e5f3 9871 {
389d00a5 9872 case SPACE_BASE:
e379e5f3 9873 break;
389d00a5
JB
9874 case SPACE_0F:
9875 count++;
e379e5f3 9876 break;
389d00a5
JB
9877 case SPACE_0F38:
9878 case SPACE_0F3A:
9879 count += 2;
e379e5f3
L
9880 break;
9881 default:
9882 abort ();
9883 }
9884
9885 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
9886 == BRANCH_PREFIX)
9887 {
9888 /* Set the maximum prefix size in BRANCH_PREFIX
9889 frag. */
9890 if (fragP->tc_frag_data.max_bytes > max)
9891 fragP->tc_frag_data.max_bytes = max;
9892 if (fragP->tc_frag_data.max_bytes > count)
9893 fragP->tc_frag_data.max_bytes -= count;
9894 else
9895 fragP->tc_frag_data.max_bytes = 0;
9896 }
9897 else
9898 {
9899 /* Remember the maximum prefix size in FUSED_JCC_PADDING
9900 frag. */
9901 unsigned int max_prefix_size;
9902 if (align_branch_prefix_size > max)
9903 max_prefix_size = max;
9904 else
9905 max_prefix_size = align_branch_prefix_size;
9906 if (max_prefix_size > count)
9907 fragP->tc_frag_data.max_prefix_length
9908 = max_prefix_size - count;
9909 }
9910
9911 /* Use existing segment prefix if possible. Use CS
9912 segment prefix in 64-bit mode. In 32-bit mode, use SS
9913 segment prefix with ESP/EBP base register and use DS
9914 segment prefix without ESP/EBP base register. */
9915 if (i.prefix[SEG_PREFIX])
9916 fragP->tc_frag_data.default_prefix = i.prefix[SEG_PREFIX];
9917 else if (flag_code == CODE_64BIT)
9918 fragP->tc_frag_data.default_prefix = CS_PREFIX_OPCODE;
9919 else if (i.base_reg
9920 && (i.base_reg->reg_num == 4
9921 || i.base_reg->reg_num == 5))
9922 fragP->tc_frag_data.default_prefix = SS_PREFIX_OPCODE;
9923 else
9924 fragP->tc_frag_data.default_prefix = DS_PREFIX_OPCODE;
9925 }
9c33702b 9926 }
29b0f896 9927 }
252b5132 9928
e379e5f3
L
9929 /* NB: Don't work with COND_JUMP86 without i386. */
9930 if (align_branch_power
9931 && now_seg != absolute_section
9932 && cpu_arch_flags.bitfield.cpui386)
9933 {
9934 /* Terminate each frag so that we can add prefix and check for
9935 fused jcc. */
9936 frag_wane (frag_now);
9937 frag_new (0);
9938 }
9939
29b0f896
AM
9940#ifdef DEBUG386
9941 if (flag_debug)
9942 {
7b81dfbb 9943 pi ("" /*line*/, &i);
29b0f896
AM
9944 }
9945#endif /* DEBUG386 */
9946}
252b5132 9947
e205caa7
L
9948/* Return the size of the displacement operand N. */
9949
9950static int
9951disp_size (unsigned int n)
9952{
9953 int size = 4;
43234a1e 9954
b5014f7a 9955 if (i.types[n].bitfield.disp64)
40fb9820
L
9956 size = 8;
9957 else if (i.types[n].bitfield.disp8)
9958 size = 1;
9959 else if (i.types[n].bitfield.disp16)
9960 size = 2;
e205caa7
L
9961 return size;
9962}
9963
9964/* Return the size of the immediate operand N. */
9965
9966static int
9967imm_size (unsigned int n)
9968{
9969 int size = 4;
40fb9820
L
9970 if (i.types[n].bitfield.imm64)
9971 size = 8;
9972 else if (i.types[n].bitfield.imm8 || i.types[n].bitfield.imm8s)
9973 size = 1;
9974 else if (i.types[n].bitfield.imm16)
9975 size = 2;
e205caa7
L
9976 return size;
9977}
9978
29b0f896 9979static void
64e74474 9980output_disp (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
9981{
9982 char *p;
9983 unsigned int n;
252b5132 9984
29b0f896
AM
9985 for (n = 0; n < i.operands; n++)
9986 {
b5014f7a 9987 if (operand_type_check (i.types[n], disp))
29b0f896 9988 {
48ef937e
JB
9989 int size = disp_size (n);
9990
9991 if (now_seg == absolute_section)
9992 abs_section_offset += size;
9993 else if (i.op[n].disps->X_op == O_constant)
29b0f896 9994 {
43234a1e 9995 offsetT val = i.op[n].disps->X_add_number;
252b5132 9996
629cfaf1
JB
9997 val = offset_in_range (val >> (size == 1 ? i.memshift : 0),
9998 size);
29b0f896
AM
9999 p = frag_more (size);
10000 md_number_to_chars (p, val, size);
10001 }
10002 else
10003 {
f86103b7 10004 enum bfd_reloc_code_real reloc_type;
a775efc8
JB
10005 bool pcrel = (i.flags[n] & Operand_PCrel) != 0;
10006 bool sign = (flag_code == CODE_64BIT && size == 4
10007 && (!want_disp32 (&i.tm)
10008 || (i.tm.opcode_modifier.jump && !i.jumpabsolute
10009 && !i.types[n].bitfield.baseindex)))
10010 || pcrel;
02a86693 10011 fixS *fixP;
29b0f896 10012
e205caa7 10013 /* We can't have 8 bit displacement here. */
9c2799c2 10014 gas_assert (!i.types[n].bitfield.disp8);
e205caa7 10015
29b0f896
AM
10016 /* The PC relative address is computed relative
10017 to the instruction boundary, so in case immediate
10018 fields follows, we need to adjust the value. */
10019 if (pcrel && i.imm_operands)
10020 {
29b0f896 10021 unsigned int n1;
e205caa7 10022 int sz = 0;
252b5132 10023
29b0f896 10024 for (n1 = 0; n1 < i.operands; n1++)
40fb9820 10025 if (operand_type_check (i.types[n1], imm))
252b5132 10026 {
e205caa7
L
10027 /* Only one immediate is allowed for PC
10028 relative address. */
9c2799c2 10029 gas_assert (sz == 0);
e205caa7
L
10030 sz = imm_size (n1);
10031 i.op[n].disps->X_add_number -= sz;
252b5132 10032 }
29b0f896 10033 /* We should find the immediate. */
9c2799c2 10034 gas_assert (sz != 0);
29b0f896 10035 }
520dc8e8 10036
29b0f896 10037 p = frag_more (size);
d258b828 10038 reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
d6ab8113 10039 if (GOT_symbol
2bbd9c25 10040 && GOT_symbol == i.op[n].disps->X_add_symbol
d6ab8113 10041 && (((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
10042 || reloc_type == BFD_RELOC_X86_64_32S
10043 || (reloc_type == BFD_RELOC_64
10044 && object_64bit))
d6ab8113
JB
10045 && (i.op[n].disps->X_op == O_symbol
10046 || (i.op[n].disps->X_op == O_add
10047 && ((symbol_get_value_expression
10048 (i.op[n].disps->X_op_symbol)->X_op)
10049 == O_subtract))))
10050 || reloc_type == BFD_RELOC_32_PCREL))
2bbd9c25 10051 {
4fa24527 10052 if (!object_64bit)
7b81dfbb
AJ
10053 {
10054 reloc_type = BFD_RELOC_386_GOTPC;
5b7c81bd 10055 i.has_gotpc_tls_reloc = true;
98da05bf 10056 i.op[n].disps->X_add_number +=
d583596c 10057 encoding_length (insn_start_frag, insn_start_off, p);
7b81dfbb
AJ
10058 }
10059 else if (reloc_type == BFD_RELOC_64)
10060 reloc_type = BFD_RELOC_X86_64_GOTPC64;
d6ab8113 10061 else
7b81dfbb
AJ
10062 /* Don't do the adjustment for x86-64, as there
10063 the pcrel addressing is relative to the _next_
10064 insn, and that is taken care of in other code. */
d6ab8113 10065 reloc_type = BFD_RELOC_X86_64_GOTPC32;
2bbd9c25 10066 }
e379e5f3
L
10067 else if (align_branch_power)
10068 {
10069 switch (reloc_type)
10070 {
10071 case BFD_RELOC_386_TLS_GD:
10072 case BFD_RELOC_386_TLS_LDM:
10073 case BFD_RELOC_386_TLS_IE:
10074 case BFD_RELOC_386_TLS_IE_32:
10075 case BFD_RELOC_386_TLS_GOTIE:
10076 case BFD_RELOC_386_TLS_GOTDESC:
10077 case BFD_RELOC_386_TLS_DESC_CALL:
10078 case BFD_RELOC_X86_64_TLSGD:
10079 case BFD_RELOC_X86_64_TLSLD:
10080 case BFD_RELOC_X86_64_GOTTPOFF:
10081 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
10082 case BFD_RELOC_X86_64_TLSDESC_CALL:
5b7c81bd 10083 i.has_gotpc_tls_reloc = true;
e379e5f3
L
10084 default:
10085 break;
10086 }
10087 }
02a86693
L
10088 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal,
10089 size, i.op[n].disps, pcrel,
10090 reloc_type);
eb19308f
JB
10091
10092 if (flag_code == CODE_64BIT && size == 4 && pcrel
10093 && !i.prefix[ADDR_PREFIX])
10094 fixP->fx_signed = 1;
10095
02a86693
L
10096 /* Check for "call/jmp *mem", "mov mem, %reg",
10097 "test %reg, mem" and "binop mem, %reg" where binop
10098 is one of adc, add, and, cmp, or, sbb, sub, xor
e60f4d3b
L
10099 instructions without data prefix. Always generate
10100 R_386_GOT32X for "sym*GOT" operand in 32-bit mode. */
10101 if (i.prefix[DATA_PREFIX] == 0
10102 && (generate_relax_relocations
10103 || (!object_64bit
10104 && i.rm.mode == 0
10105 && i.rm.regmem == 5))
0cb4071e
L
10106 && (i.rm.mode == 2
10107 || (i.rm.mode == 0 && i.rm.regmem == 5))
ddb62495 10108 && i.tm.opcode_space == SPACE_BASE
02a86693
L
10109 && ((i.operands == 1
10110 && i.tm.base_opcode == 0xff
10111 && (i.rm.reg == 2 || i.rm.reg == 4))
10112 || (i.operands == 2
10113 && (i.tm.base_opcode == 0x8b
10114 || i.tm.base_opcode == 0x85
2ae4c703 10115 || (i.tm.base_opcode & ~0x38) == 0x03))))
02a86693
L
10116 {
10117 if (object_64bit)
10118 {
10119 fixP->fx_tcbit = i.rex != 0;
10120 if (i.base_reg
e968fc9b 10121 && (i.base_reg->reg_num == RegIP))
02a86693
L
10122 fixP->fx_tcbit2 = 1;
10123 }
10124 else
10125 fixP->fx_tcbit2 = 1;
10126 }
29b0f896
AM
10127 }
10128 }
10129 }
10130}
252b5132 10131
29b0f896 10132static void
64e74474 10133output_imm (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
10134{
10135 char *p;
10136 unsigned int n;
252b5132 10137
29b0f896
AM
10138 for (n = 0; n < i.operands; n++)
10139 {
40fb9820 10140 if (operand_type_check (i.types[n], imm))
29b0f896 10141 {
48ef937e
JB
10142 int size = imm_size (n);
10143
10144 if (now_seg == absolute_section)
10145 abs_section_offset += size;
10146 else if (i.op[n].imms->X_op == O_constant)
29b0f896 10147 {
29b0f896 10148 offsetT val;
b4cac588 10149
29b0f896
AM
10150 val = offset_in_range (i.op[n].imms->X_add_number,
10151 size);
10152 p = frag_more (size);
10153 md_number_to_chars (p, val, size);
10154 }
10155 else
10156 {
10157 /* Not absolute_section.
10158 Need a 32-bit fixup (don't support 8bit
10159 non-absolute imms). Try to support other
10160 sizes ... */
f86103b7 10161 enum bfd_reloc_code_real reloc_type;
e205caa7 10162 int sign;
29b0f896 10163
40fb9820 10164 if (i.types[n].bitfield.imm32s
a7d61044 10165 && (i.suffix == QWORD_MNEM_SUFFIX
40fb9820 10166 || (!i.suffix && i.tm.opcode_modifier.no_lsuf)))
29b0f896 10167 sign = 1;
e205caa7
L
10168 else
10169 sign = 0;
520dc8e8 10170
29b0f896 10171 p = frag_more (size);
d258b828 10172 reloc_type = reloc (size, 0, sign, i.reloc[n]);
f86103b7 10173
2bbd9c25
JJ
10174 /* This is tough to explain. We end up with this one if we
10175 * have operands that look like
10176 * "_GLOBAL_OFFSET_TABLE_+[.-.L284]". The goal here is to
10177 * obtain the absolute address of the GOT, and it is strongly
10178 * preferable from a performance point of view to avoid using
10179 * a runtime relocation for this. The actual sequence of
10180 * instructions often look something like:
10181 *
10182 * call .L66
10183 * .L66:
10184 * popl %ebx
10185 * addl $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
10186 *
10187 * The call and pop essentially return the absolute address
10188 * of the label .L66 and store it in %ebx. The linker itself
10189 * will ultimately change the first operand of the addl so
10190 * that %ebx points to the GOT, but to keep things simple, the
10191 * .o file must have this operand set so that it generates not
10192 * the absolute address of .L66, but the absolute address of
10193 * itself. This allows the linker itself simply treat a GOTPC
10194 * relocation as asking for a pcrel offset to the GOT to be
10195 * added in, and the addend of the relocation is stored in the
10196 * operand field for the instruction itself.
10197 *
10198 * Our job here is to fix the operand so that it would add
10199 * the correct offset so that %ebx would point to itself. The
10200 * thing that is tricky is that .-.L66 will point to the
10201 * beginning of the instruction, so we need to further modify
10202 * the operand so that it will point to itself. There are
10203 * other cases where you have something like:
10204 *
10205 * .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
10206 *
10207 * and here no correction would be required. Internally in
10208 * the assembler we treat operands of this form as not being
10209 * pcrel since the '.' is explicitly mentioned, and I wonder
10210 * whether it would simplify matters to do it this way. Who
10211 * knows. In earlier versions of the PIC patches, the
10212 * pcrel_adjust field was used to store the correction, but
10213 * since the expression is not pcrel, I felt it would be
10214 * confusing to do it this way. */
10215
d6ab8113 10216 if ((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
10217 || reloc_type == BFD_RELOC_X86_64_32S
10218 || reloc_type == BFD_RELOC_64)
29b0f896
AM
10219 && GOT_symbol
10220 && GOT_symbol == i.op[n].imms->X_add_symbol
10221 && (i.op[n].imms->X_op == O_symbol
10222 || (i.op[n].imms->X_op == O_add
10223 && ((symbol_get_value_expression
10224 (i.op[n].imms->X_op_symbol)->X_op)
10225 == O_subtract))))
10226 {
4fa24527 10227 if (!object_64bit)
d6ab8113 10228 reloc_type = BFD_RELOC_386_GOTPC;
7b81dfbb 10229 else if (size == 4)
d6ab8113 10230 reloc_type = BFD_RELOC_X86_64_GOTPC32;
7b81dfbb
AJ
10231 else if (size == 8)
10232 reloc_type = BFD_RELOC_X86_64_GOTPC64;
5b7c81bd 10233 i.has_gotpc_tls_reloc = true;
d583596c
JB
10234 i.op[n].imms->X_add_number +=
10235 encoding_length (insn_start_frag, insn_start_off, p);
29b0f896 10236 }
29b0f896
AM
10237 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
10238 i.op[n].imms, 0, reloc_type);
10239 }
10240 }
10241 }
252b5132
RH
10242}
10243\f
d182319b
JB
10244/* x86_cons_fix_new is called via the expression parsing code when a
10245 reloc is needed. We use this hook to get the correct .got reloc. */
d182319b
JB
10246static int cons_sign = -1;
10247
10248void
e3bb37b5 10249x86_cons_fix_new (fragS *frag, unsigned int off, unsigned int len,
62ebcb5c 10250 expressionS *exp, bfd_reloc_code_real_type r)
d182319b 10251{
d258b828 10252 r = reloc (len, 0, cons_sign, r);
d182319b
JB
10253
10254#ifdef TE_PE
10255 if (exp->X_op == O_secrel)
10256 {
10257 exp->X_op = O_symbol;
10258 r = BFD_RELOC_32_SECREL;
10259 }
145667f8
MH
10260 else if (exp->X_op == O_secidx)
10261 r = BFD_RELOC_16_SECIDX;
d182319b
JB
10262#endif
10263
10264 fix_new_exp (frag, off, len, exp, 0, r);
10265}
10266
357d1bd8
L
10267/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
10268 purpose of the `.dc.a' internal pseudo-op. */
10269
10270int
10271x86_address_bytes (void)
10272{
10273 if ((stdoutput->arch_info->mach & bfd_mach_x64_32))
10274 return 4;
10275 return stdoutput->arch_info->bits_per_address / 8;
10276}
10277
deea4973
JB
10278#if (!(defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined (OBJ_MACH_O)) \
10279 || defined (LEX_AT)) && !defined (TE_PE)
d258b828 10280# define lex_got(reloc, adjust, types) NULL
718ddfc0 10281#else
f3c180ae
AM
10282/* Parse operands of the form
10283 <symbol>@GOTOFF+<nnn>
10284 and similar .plt or .got references.
10285
10286 If we find one, set up the correct relocation in RELOC and copy the
10287 input string, minus the `@GOTOFF' into a malloc'd buffer for
10288 parsing by the calling routine. Return this buffer, and if ADJUST
10289 is non-null set it to the length of the string we removed from the
10290 input line. Otherwise return NULL. */
10291static char *
91d6fa6a 10292lex_got (enum bfd_reloc_code_real *rel,
64e74474 10293 int *adjust,
d258b828 10294 i386_operand_type *types)
f3c180ae 10295{
7b81dfbb
AJ
10296 /* Some of the relocations depend on the size of what field is to
10297 be relocated. But in our callers i386_immediate and i386_displacement
10298 we don't yet know the operand size (this will be set by insn
10299 matching). Hence we record the word32 relocation here,
10300 and adjust the reloc according to the real size in reloc(). */
145667f8
MH
10301 static const struct
10302 {
f3c180ae 10303 const char *str;
cff8d58a 10304 int len;
4fa24527 10305 const enum bfd_reloc_code_real rel[2];
40fb9820 10306 const i386_operand_type types64;
5b7c81bd 10307 bool need_GOT_symbol;
145667f8
MH
10308 }
10309 gotrel[] =
10310 {
05909f23
JB
10311
10312#define OPERAND_TYPE_IMM32_32S_DISP32 { .bitfield = \
10313 { .imm32 = 1, .imm32s = 1, .disp32 = 1 } }
10314#define OPERAND_TYPE_IMM32_32S_64_DISP32 { .bitfield = \
10315 { .imm32 = 1, .imm32s = 1, .imm64 = 1, .disp32 = 1 } }
10316#define OPERAND_TYPE_IMM32_32S_64_DISP32_64 { .bitfield = \
10317 { .imm32 = 1, .imm32s = 1, .imm64 = 1, .disp32 = 1, .disp64 = 1 } }
10318#define OPERAND_TYPE_IMM64_DISP64 { .bitfield = \
10319 { .imm64 = 1, .disp64 = 1 } }
10320
deea4973 10321#ifndef TE_PE
8ce3d284 10322#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
10323 { STRING_COMMA_LEN ("SIZE"), { BFD_RELOC_SIZE32,
10324 BFD_RELOC_SIZE32 },
05909f23 10325 { .bitfield = { .imm32 = 1, .imm64 = 1 } }, false },
8ce3d284 10326#endif
cff8d58a
L
10327 { STRING_COMMA_LEN ("PLTOFF"), { _dummy_first_bfd_reloc_code_real,
10328 BFD_RELOC_X86_64_PLTOFF64 },
05909f23 10329 { .bitfield = { .imm64 = 1 } }, true },
cff8d58a
L
10330 { STRING_COMMA_LEN ("PLT"), { BFD_RELOC_386_PLT32,
10331 BFD_RELOC_X86_64_PLT32 },
a775efc8 10332 OPERAND_TYPE_IMM32_32S_DISP32, false },
cff8d58a
L
10333 { STRING_COMMA_LEN ("GOTPLT"), { _dummy_first_bfd_reloc_code_real,
10334 BFD_RELOC_X86_64_GOTPLT64 },
5b7c81bd 10335 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
10336 { STRING_COMMA_LEN ("GOTOFF"), { BFD_RELOC_386_GOTOFF,
10337 BFD_RELOC_X86_64_GOTOFF64 },
5b7c81bd 10338 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
10339 { STRING_COMMA_LEN ("GOTPCREL"), { _dummy_first_bfd_reloc_code_real,
10340 BFD_RELOC_X86_64_GOTPCREL },
a775efc8 10341 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10342 { STRING_COMMA_LEN ("TLSGD"), { BFD_RELOC_386_TLS_GD,
10343 BFD_RELOC_X86_64_TLSGD },
a775efc8 10344 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10345 { STRING_COMMA_LEN ("TLSLDM"), { BFD_RELOC_386_TLS_LDM,
10346 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10347 OPERAND_TYPE_NONE, true },
cff8d58a
L
10348 { STRING_COMMA_LEN ("TLSLD"), { _dummy_first_bfd_reloc_code_real,
10349 BFD_RELOC_X86_64_TLSLD },
a775efc8 10350 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10351 { STRING_COMMA_LEN ("GOTTPOFF"), { BFD_RELOC_386_TLS_IE_32,
10352 BFD_RELOC_X86_64_GOTTPOFF },
a775efc8 10353 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10354 { STRING_COMMA_LEN ("TPOFF"), { BFD_RELOC_386_TLS_LE_32,
10355 BFD_RELOC_X86_64_TPOFF32 },
a775efc8 10356 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
10357 { STRING_COMMA_LEN ("NTPOFF"), { BFD_RELOC_386_TLS_LE,
10358 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10359 OPERAND_TYPE_NONE, true },
cff8d58a
L
10360 { STRING_COMMA_LEN ("DTPOFF"), { BFD_RELOC_386_TLS_LDO_32,
10361 BFD_RELOC_X86_64_DTPOFF32 },
a775efc8 10362 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
10363 { STRING_COMMA_LEN ("GOTNTPOFF"),{ BFD_RELOC_386_TLS_GOTIE,
10364 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10365 OPERAND_TYPE_NONE, true },
cff8d58a
L
10366 { STRING_COMMA_LEN ("INDNTPOFF"),{ BFD_RELOC_386_TLS_IE,
10367 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10368 OPERAND_TYPE_NONE, true },
cff8d58a
L
10369 { STRING_COMMA_LEN ("GOT"), { BFD_RELOC_386_GOT32,
10370 BFD_RELOC_X86_64_GOT32 },
a775efc8 10371 OPERAND_TYPE_IMM32_32S_64_DISP32, true },
cff8d58a
L
10372 { STRING_COMMA_LEN ("TLSDESC"), { BFD_RELOC_386_TLS_GOTDESC,
10373 BFD_RELOC_X86_64_GOTPC32_TLSDESC },
a775efc8 10374 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10375 { STRING_COMMA_LEN ("TLSCALL"), { BFD_RELOC_386_TLS_DESC_CALL,
10376 BFD_RELOC_X86_64_TLSDESC_CALL },
a775efc8 10377 OPERAND_TYPE_IMM32_32S_DISP32, true },
deea4973
JB
10378#else /* TE_PE */
10379 { STRING_COMMA_LEN ("SECREL32"), { BFD_RELOC_32_SECREL,
10380 BFD_RELOC_32_SECREL },
a775efc8 10381 OPERAND_TYPE_IMM32_32S_64_DISP32_64, false },
deea4973 10382#endif
05909f23
JB
10383
10384#undef OPERAND_TYPE_IMM32_32S_DISP32
10385#undef OPERAND_TYPE_IMM32_32S_64_DISP32
10386#undef OPERAND_TYPE_IMM32_32S_64_DISP32_64
10387#undef OPERAND_TYPE_IMM64_DISP64
10388
f3c180ae
AM
10389 };
10390 char *cp;
10391 unsigned int j;
10392
deea4973 10393#if defined (OBJ_MAYBE_ELF) && !defined (TE_PE)
718ddfc0
JB
10394 if (!IS_ELF)
10395 return NULL;
d382c579 10396#endif
718ddfc0 10397
f3c180ae 10398 for (cp = input_line_pointer; *cp != '@'; cp++)
67c11a9b 10399 if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
f3c180ae
AM
10400 return NULL;
10401
47465058 10402 for (j = 0; j < ARRAY_SIZE (gotrel); j++)
f3c180ae 10403 {
cff8d58a 10404 int len = gotrel[j].len;
28f81592 10405 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
f3c180ae 10406 {
4fa24527 10407 if (gotrel[j].rel[object_64bit] != 0)
f3c180ae 10408 {
28f81592
AM
10409 int first, second;
10410 char *tmpbuf, *past_reloc;
f3c180ae 10411
91d6fa6a 10412 *rel = gotrel[j].rel[object_64bit];
f3c180ae 10413
3956db08
JB
10414 if (types)
10415 {
10416 if (flag_code != CODE_64BIT)
40fb9820
L
10417 {
10418 types->bitfield.imm32 = 1;
10419 types->bitfield.disp32 = 1;
10420 }
3956db08
JB
10421 else
10422 *types = gotrel[j].types64;
10423 }
10424
844bf810 10425 if (gotrel[j].need_GOT_symbol && GOT_symbol == NULL)
f3c180ae
AM
10426 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
10427
28f81592 10428 /* The length of the first part of our input line. */
f3c180ae 10429 first = cp - input_line_pointer;
28f81592
AM
10430
10431 /* The second part goes from after the reloc token until
67c11a9b 10432 (and including) an end_of_line char or comma. */
28f81592 10433 past_reloc = cp + 1 + len;
67c11a9b
AM
10434 cp = past_reloc;
10435 while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
10436 ++cp;
10437 second = cp + 1 - past_reloc;
28f81592
AM
10438
10439 /* Allocate and copy string. The trailing NUL shouldn't
10440 be necessary, but be safe. */
add39d23 10441 tmpbuf = XNEWVEC (char, first + second + 2);
f3c180ae 10442 memcpy (tmpbuf, input_line_pointer, first);
0787a12d
AM
10443 if (second != 0 && *past_reloc != ' ')
10444 /* Replace the relocation token with ' ', so that
10445 errors like foo@GOTOFF1 will be detected. */
10446 tmpbuf[first++] = ' ';
af89796a
L
10447 else
10448 /* Increment length by 1 if the relocation token is
10449 removed. */
10450 len++;
10451 if (adjust)
10452 *adjust = len;
0787a12d
AM
10453 memcpy (tmpbuf + first, past_reloc, second);
10454 tmpbuf[first + second] = '\0';
f3c180ae
AM
10455 return tmpbuf;
10456 }
10457
4fa24527
JB
10458 as_bad (_("@%s reloc is not supported with %d-bit output format"),
10459 gotrel[j].str, 1 << (5 + object_64bit));
f3c180ae
AM
10460 return NULL;
10461 }
10462 }
10463
10464 /* Might be a symbol version string. Don't as_bad here. */
10465 return NULL;
10466}
4e4f7c87 10467#endif
f3c180ae 10468
62ebcb5c 10469bfd_reloc_code_real_type
e3bb37b5 10470x86_cons (expressionS *exp, int size)
f3c180ae 10471{
62ebcb5c
AM
10472 bfd_reloc_code_real_type got_reloc = NO_RELOC;
10473
2748c1b1
L
10474#if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
10475 && !defined (LEX_AT)) \
10476 || defined (TE_PE)
ee86248c
JB
10477 intel_syntax = -intel_syntax;
10478
3c7b9c2c 10479 exp->X_md = 0;
4fa24527 10480 if (size == 4 || (object_64bit && size == 8))
f3c180ae
AM
10481 {
10482 /* Handle @GOTOFF and the like in an expression. */
10483 char *save;
10484 char *gotfree_input_line;
4a57f2cf 10485 int adjust = 0;
f3c180ae
AM
10486
10487 save = input_line_pointer;
d258b828 10488 gotfree_input_line = lex_got (&got_reloc, &adjust, NULL);
f3c180ae
AM
10489 if (gotfree_input_line)
10490 input_line_pointer = gotfree_input_line;
10491
10492 expression (exp);
10493
10494 if (gotfree_input_line)
10495 {
10496 /* expression () has merrily parsed up to the end of line,
10497 or a comma - in the wrong buffer. Transfer how far
10498 input_line_pointer has moved to the right buffer. */
10499 input_line_pointer = (save
10500 + (input_line_pointer - gotfree_input_line)
10501 + adjust);
10502 free (gotfree_input_line);
3992d3b7
AM
10503 if (exp->X_op == O_constant
10504 || exp->X_op == O_absent
10505 || exp->X_op == O_illegal
0398aac5 10506 || exp->X_op == O_register
3992d3b7
AM
10507 || exp->X_op == O_big)
10508 {
10509 char c = *input_line_pointer;
10510 *input_line_pointer = 0;
10511 as_bad (_("missing or invalid expression `%s'"), save);
10512 *input_line_pointer = c;
10513 }
b9519cfe
L
10514 else if ((got_reloc == BFD_RELOC_386_PLT32
10515 || got_reloc == BFD_RELOC_X86_64_PLT32)
10516 && exp->X_op != O_symbol)
10517 {
10518 char c = *input_line_pointer;
10519 *input_line_pointer = 0;
10520 as_bad (_("invalid PLT expression `%s'"), save);
10521 *input_line_pointer = c;
10522 }
f3c180ae
AM
10523 }
10524 }
10525 else
10526 expression (exp);
ee86248c
JB
10527
10528 intel_syntax = -intel_syntax;
10529
10530 if (intel_syntax)
10531 i386_intel_simplify (exp);
2748c1b1
L
10532#else
10533 expression (exp);
10534#endif
62ebcb5c 10535
a442cac5
JB
10536 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
10537 if (size == 4 && exp->X_op == O_constant && !object_64bit)
10538 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
10539
62ebcb5c 10540 return got_reloc;
f3c180ae 10541}
f3c180ae 10542
9f32dd5b
L
10543static void
10544signed_cons (int size)
6482c264 10545{
a442cac5 10546 if (object_64bit)
d182319b
JB
10547 cons_sign = 1;
10548 cons (size);
10549 cons_sign = -1;
6482c264
NC
10550}
10551
edd67638
JB
10552static void
10553s_insn (int dummy ATTRIBUTE_UNUSED)
10554{
10555 char mnemonic[MAX_MNEM_SIZE], *line = input_line_pointer;
10556 char *saved_ilp = find_end_of_line (line, false), saved_char;
10557 const char *end;
10558 unsigned int j;
10559 valueT val;
10560 bool vex = false, xop = false, evex = false;
10561 static const templates tt = { &i.tm, &i.tm + 1 };
10562
10563 init_globals ();
10564
10565 saved_char = *saved_ilp;
10566 *saved_ilp = 0;
10567
10568 end = parse_insn (line, mnemonic, true);
10569 if (end == NULL)
10570 {
10571 bad:
10572 *saved_ilp = saved_char;
10573 ignore_rest_of_line ();
d0c2e3ec 10574 i.tm.mnem_off = 0;
edd67638
JB
10575 return;
10576 }
10577 line += end - line;
10578
10579 current_templates = &tt;
10580 i.tm.mnem_off = MN__insn;
10581
10582 if (startswith (line, "VEX")
10583 && (line[3] == '.' || is_space_char (line[3])))
10584 {
10585 vex = true;
10586 line += 3;
10587 }
10588 else if (startswith (line, "XOP") && ISDIGIT (line[3]))
10589 {
10590 char *e;
10591 unsigned long n = strtoul (line + 3, &e, 16);
10592
10593 if (e == line + 5 && n >= 0x08 && n <= 0x1f
10594 && (*e == '.' || is_space_char (*e)))
10595 {
10596 xop = true;
d0c2e3ec
JB
10597 /* Arrange for build_vex_prefix() to emit 0x8f. */
10598 i.tm.opcode_space = SPACE_XOP08;
10599 i.insn_opcode_space = n;
edd67638
JB
10600 line = e;
10601 }
10602 }
10603 else if (startswith (line, "EVEX")
10604 && (line[4] == '.' || is_space_char (line[4])))
10605 {
10606 evex = true;
10607 line += 4;
10608 }
10609
10610 if (vex || xop
10611 ? i.vec_encoding == vex_encoding_evex
10612 : evex
10613 ? i.vec_encoding == vex_encoding_vex
10614 || i.vec_encoding == vex_encoding_vex3
10615 : i.vec_encoding != vex_encoding_default)
10616 {
10617 as_bad (_("pseudo-prefix conflicts with encoding specifier"));
10618 goto bad;
10619 }
10620
10621 if (line > end && *line == '.')
10622 {
d0c2e3ec
JB
10623 /* Length specifier (VEX.L, XOP.L, EVEX.L'L). */
10624 switch (line[1])
10625 {
10626 case 'L':
10627 switch (line[2])
10628 {
10629 case '0':
10630 if (evex)
10631 i.tm.opcode_modifier.evex = EVEX128;
10632 else
10633 i.tm.opcode_modifier.vex = VEX128;
10634 break;
10635
10636 case '1':
10637 if (evex)
10638 i.tm.opcode_modifier.evex = EVEX256;
10639 else
10640 i.tm.opcode_modifier.vex = VEX256;
10641 break;
10642
10643 case '2':
10644 if (evex)
10645 i.tm.opcode_modifier.evex = EVEX512;
10646 break;
10647
10648 case '3':
10649 if (evex)
10650 i.tm.opcode_modifier.evex = EVEX_L3;
10651 break;
10652
10653 case 'I':
10654 if (line[3] == 'G')
10655 {
10656 if (evex)
10657 i.tm.opcode_modifier.evex = EVEXLIG;
10658 else
10659 i.tm.opcode_modifier.vex = VEXScalar; /* LIG */
10660 ++line;
10661 }
10662 break;
10663 }
10664
10665 if (i.tm.opcode_modifier.vex || i.tm.opcode_modifier.evex)
10666 line += 3;
10667 break;
10668
10669 case '1':
10670 if (line[2] == '2' && line[3] == '8')
10671 {
10672 if (evex)
10673 i.tm.opcode_modifier.evex = EVEX128;
10674 else
10675 i.tm.opcode_modifier.vex = VEX128;
10676 line += 4;
10677 }
10678 break;
10679
10680 case '2':
10681 if (line[2] == '5' && line[3] == '6')
10682 {
10683 if (evex)
10684 i.tm.opcode_modifier.evex = EVEX256;
10685 else
10686 i.tm.opcode_modifier.vex = VEX256;
10687 line += 4;
10688 }
10689 break;
10690
10691 case '5':
10692 if (evex && line[2] == '1' && line[3] == '2')
10693 {
10694 i.tm.opcode_modifier.evex = EVEX512;
10695 line += 4;
10696 }
10697 break;
10698 }
10699 }
10700
10701 if (line > end && *line == '.')
10702 {
10703 /* embedded prefix (VEX.pp, XOP.pp, EVEX.pp). */
10704 switch (line[1])
10705 {
10706 case 'N':
10707 if (line[2] == 'P')
10708 line += 3;
10709 break;
10710
10711 case '6':
10712 if (line[2] == '6')
10713 {
10714 i.tm.opcode_modifier.opcodeprefix = PREFIX_0X66;
10715 line += 3;
10716 }
10717 break;
10718
10719 case 'F': case 'f':
10720 if (line[2] == '3')
10721 {
10722 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
10723 line += 3;
10724 }
10725 else if (line[2] == '2')
10726 {
10727 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF2;
10728 line += 3;
10729 }
10730 break;
10731 }
10732 }
10733
10734 if (line > end && !xop && *line == '.')
10735 {
10736 /* Encoding space (VEX.mmmmm, EVEX.mmmm). */
10737 switch (line[1])
10738 {
10739 case '0':
10740 if (TOUPPER (line[2]) != 'F')
10741 break;
10742 if (line[3] == '.' || is_space_char (line[3]))
10743 {
10744 i.insn_opcode_space = SPACE_0F;
10745 line += 3;
10746 }
10747 else if (line[3] == '3'
10748 && (line[4] == '8' || TOUPPER (line[4]) == 'A')
10749 && (line[5] == '.' || is_space_char (line[5])))
10750 {
10751 i.insn_opcode_space = line[4] == '8' ? SPACE_0F38 : SPACE_0F3A;
10752 line += 5;
10753 }
10754 break;
10755
10756 case 'M':
10757 if (ISDIGIT (line[2]) && line[2] != '0')
10758 {
10759 char *e;
10760 unsigned long n = strtoul (line + 2, &e, 10);
10761
10762 if (n <= (evex ? 15 : 31)
10763 && (*e == '.' || is_space_char (*e)))
10764 {
10765 i.insn_opcode_space = n;
10766 line = e;
10767 }
10768 }
10769 break;
10770 }
10771 }
10772
10773 if (line > end && *line == '.' && line[1] == 'W')
10774 {
10775 /* VEX.W, XOP.W, EVEX.W */
10776 switch (line[2])
10777 {
10778 case '0':
10779 i.tm.opcode_modifier.vexw = VEXW0;
10780 break;
10781
10782 case '1':
10783 i.tm.opcode_modifier.vexw = VEXW1;
10784 break;
10785
10786 case 'I':
10787 if (line[3] == 'G')
10788 {
10789 i.tm.opcode_modifier.vexw = VEXWIG;
10790 ++line;
10791 }
10792 break;
10793 }
10794
10795 if (i.tm.opcode_modifier.vexw)
10796 line += 3;
10797 }
10798
10799 if (line > end && *line && !is_space_char (*line))
10800 {
10801 /* Improve diagnostic a little. */
10802 if (*line == '.' && line[1] && !is_space_char (line[1]))
10803 ++line;
10804 goto done;
edd67638
JB
10805 }
10806
10807 input_line_pointer = line;
10808 val = get_absolute_expression ();
10809 line = input_line_pointer;
10810
10811 for (j = 1; j < sizeof(val); ++j)
10812 if (!(val >> (j * 8)))
10813 break;
10814
10815 /* Trim off a prefix if present. */
10816 if (j > 1 && !vex && !xop && !evex)
10817 {
10818 uint8_t byte = val >> ((j - 1) * 8);
10819
10820 switch (byte)
10821 {
10822 case DATA_PREFIX_OPCODE:
10823 case REPE_PREFIX_OPCODE:
10824 case REPNE_PREFIX_OPCODE:
10825 if (!add_prefix (byte))
10826 goto bad;
10827 val &= ((uint64_t)1 << (--j * 8)) - 1;
10828 break;
10829 }
10830 }
10831
10832 /* Trim off encoding space. */
d0c2e3ec 10833 if (j > 1 && !i.insn_opcode_space && (val >> ((j - 1) * 8)) == 0x0f)
edd67638
JB
10834 {
10835 uint8_t byte = val >> ((--j - 1) * 8);
10836
d0c2e3ec 10837 i.insn_opcode_space = SPACE_0F;
edd67638
JB
10838 switch (byte & -(j > 1))
10839 {
10840 case 0x38:
d0c2e3ec 10841 i.insn_opcode_space = SPACE_0F38;
edd67638
JB
10842 --j;
10843 break;
10844 case 0x3a:
d0c2e3ec 10845 i.insn_opcode_space = SPACE_0F3A;
edd67638
JB
10846 --j;
10847 break;
10848 }
d0c2e3ec 10849 i.tm.opcode_space = i.insn_opcode_space;
edd67638
JB
10850 val &= ((uint64_t)1 << (j * 8)) - 1;
10851 }
d0c2e3ec
JB
10852 if (!i.tm.opcode_space && (vex || evex))
10853 /* Arrange for build_vex_prefix() to properly emit 0xC4/0xC5.
10854 Also avoid hitting abort() there or in build_evex_prefix(). */
10855 i.tm.opcode_space = i.insn_opcode_space == SPACE_0F ? SPACE_0F
10856 : SPACE_0F38;
edd67638
JB
10857
10858 if (j > 2)
10859 {
10860 as_bad (_("opcode residual (%#"PRIx64") too wide"), (uint64_t) val);
10861 goto bad;
10862 }
10863 i.opcode_length = j;
10864 i.tm.base_opcode = val;
10865
d0c2e3ec
JB
10866 if (vex || xop)
10867 {
10868 if (!i.tm.opcode_modifier.vex)
10869 i.tm.opcode_modifier.vex = VEXScalar; /* LIG */
10870
10871 build_vex_prefix (NULL);
10872 i.rex &= REX_OPCODE;
10873 }
10874 else if (evex)
10875 {
10876 if (!i.tm.opcode_modifier.evex)
10877 i.tm.opcode_modifier.evex = EVEXLIG;
10878
10879 build_evex_prefix ();
10880 i.rex &= REX_OPCODE;
10881 }
10882
edd67638
JB
10883 output_insn ();
10884
d0c2e3ec 10885 done:
edd67638
JB
10886 *saved_ilp = saved_char;
10887 input_line_pointer = line;
10888
10889 demand_empty_rest_of_line ();
d0c2e3ec
JB
10890
10891 /* Make sure dot_insn() won't yield "true" anymore. */
10892 i.tm.mnem_off = 0;
edd67638
JB
10893}
10894
d182319b 10895#ifdef TE_PE
6482c264 10896static void
7016a5d5 10897pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
6482c264
NC
10898{
10899 expressionS exp;
10900
10901 do
10902 {
10903 expression (&exp);
10904 if (exp.X_op == O_symbol)
10905 exp.X_op = O_secrel;
10906
10907 emit_expr (&exp, 4);
10908 }
10909 while (*input_line_pointer++ == ',');
10910
10911 input_line_pointer--;
10912 demand_empty_rest_of_line ();
10913}
145667f8
MH
10914
10915static void
10916pe_directive_secidx (int dummy ATTRIBUTE_UNUSED)
10917{
10918 expressionS exp;
10919
10920 do
10921 {
10922 expression (&exp);
10923 if (exp.X_op == O_symbol)
10924 exp.X_op = O_secidx;
10925
10926 emit_expr (&exp, 2);
10927 }
10928 while (*input_line_pointer++ == ',');
10929
10930 input_line_pointer--;
10931 demand_empty_rest_of_line ();
10932}
6482c264
NC
10933#endif
10934
7063667e
JB
10935/* Handle Rounding Control / SAE specifiers. */
10936
10937static char *
10938RC_SAE_specifier (const char *pstr)
10939{
10940 unsigned int j;
10941
10942 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); j++)
10943 {
10944 if (!strncmp (pstr, RC_NamesTable[j].name, RC_NamesTable[j].len))
10945 {
10946 if (i.rounding.type != rc_none)
10947 {
10948 as_bad (_("duplicated `{%s}'"), RC_NamesTable[j].name);
10949 return NULL;
10950 }
10951
10952 i.rounding.type = RC_NamesTable[j].type;
10953
10954 return (char *)(pstr + RC_NamesTable[j].len);
10955 }
10956 }
10957
10958 return NULL;
10959}
10960
43234a1e
L
10961/* Handle Vector operations. */
10962
10963static char *
f70c6814 10964check_VecOperations (char *op_string)
43234a1e
L
10965{
10966 const reg_entry *mask;
10967 const char *saved;
10968 char *end_op;
10969
f70c6814 10970 while (*op_string)
43234a1e
L
10971 {
10972 saved = op_string;
10973 if (*op_string == '{')
10974 {
10975 op_string++;
10976
10977 /* Check broadcasts. */
d34049e8 10978 if (startswith (op_string, "1to"))
43234a1e 10979 {
5273a3cd 10980 unsigned int bcst_type;
43234a1e 10981
5273a3cd 10982 if (i.broadcast.type)
43234a1e
L
10983 goto duplicated_vec_op;
10984
10985 op_string += 3;
10986 if (*op_string == '8')
8e6e0792 10987 bcst_type = 8;
b28d1bda 10988 else if (*op_string == '4')
8e6e0792 10989 bcst_type = 4;
b28d1bda 10990 else if (*op_string == '2')
8e6e0792 10991 bcst_type = 2;
43234a1e
L
10992 else if (*op_string == '1'
10993 && *(op_string+1) == '6')
10994 {
8e6e0792 10995 bcst_type = 16;
43234a1e
L
10996 op_string++;
10997 }
0cc78721
CL
10998 else if (*op_string == '3'
10999 && *(op_string+1) == '2')
11000 {
11001 bcst_type = 32;
11002 op_string++;
11003 }
43234a1e
L
11004 else
11005 {
11006 as_bad (_("Unsupported broadcast: `%s'"), saved);
11007 return NULL;
11008 }
11009 op_string++;
11010
5273a3cd
JB
11011 i.broadcast.type = bcst_type;
11012 i.broadcast.operand = this_operand;
43234a1e
L
11013 }
11014 /* Check masking operation. */
11015 else if ((mask = parse_register (op_string, &end_op)) != NULL)
11016 {
8a6fb3f9
JB
11017 if (mask == &bad_reg)
11018 return NULL;
11019
43234a1e 11020 /* k0 can't be used for write mask. */
f74a6307 11021 if (mask->reg_type.bitfield.class != RegMask || !mask->reg_num)
43234a1e 11022 {
6d2cd6b2
JB
11023 as_bad (_("`%s%s' can't be used for write mask"),
11024 register_prefix, mask->reg_name);
43234a1e
L
11025 return NULL;
11026 }
11027
6225c532 11028 if (!i.mask.reg)
43234a1e 11029 {
6225c532
JB
11030 i.mask.reg = mask;
11031 i.mask.operand = this_operand;
43234a1e 11032 }
6225c532
JB
11033 else if (i.mask.reg->reg_num)
11034 goto duplicated_vec_op;
43234a1e
L
11035 else
11036 {
6225c532 11037 i.mask.reg = mask;
43234a1e
L
11038
11039 /* Only "{z}" is allowed here. No need to check
11040 zeroing mask explicitly. */
6225c532 11041 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
11042 {
11043 as_bad (_("invalid write mask `%s'"), saved);
11044 return NULL;
11045 }
11046 }
11047
11048 op_string = end_op;
11049 }
11050 /* Check zeroing-flag for masking operation. */
11051 else if (*op_string == 'z')
11052 {
6225c532 11053 if (!i.mask.reg)
43234a1e 11054 {
6225c532
JB
11055 i.mask.reg = reg_k0;
11056 i.mask.zeroing = 1;
11057 i.mask.operand = this_operand;
43234a1e
L
11058 }
11059 else
11060 {
6225c532 11061 if (i.mask.zeroing)
43234a1e
L
11062 {
11063 duplicated_vec_op:
11064 as_bad (_("duplicated `%s'"), saved);
11065 return NULL;
11066 }
11067
6225c532 11068 i.mask.zeroing = 1;
43234a1e
L
11069
11070 /* Only "{%k}" is allowed here. No need to check mask
11071 register explicitly. */
6225c532 11072 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
11073 {
11074 as_bad (_("invalid zeroing-masking `%s'"),
11075 saved);
11076 return NULL;
11077 }
11078 }
11079
11080 op_string++;
11081 }
7063667e
JB
11082 else if (intel_syntax
11083 && (op_string = RC_SAE_specifier (op_string)) != NULL)
11084 i.rounding.modifier = true;
43234a1e
L
11085 else
11086 goto unknown_vec_op;
11087
11088 if (*op_string != '}')
11089 {
11090 as_bad (_("missing `}' in `%s'"), saved);
11091 return NULL;
11092 }
11093 op_string++;
0ba3a731
L
11094
11095 /* Strip whitespace since the addition of pseudo prefixes
11096 changed how the scrubber treats '{'. */
11097 if (is_space_char (*op_string))
11098 ++op_string;
11099
43234a1e
L
11100 continue;
11101 }
11102 unknown_vec_op:
11103 /* We don't know this one. */
11104 as_bad (_("unknown vector operation: `%s'"), saved);
11105 return NULL;
11106 }
11107
6225c532 11108 if (i.mask.reg && i.mask.zeroing && !i.mask.reg->reg_num)
6d2cd6b2
JB
11109 {
11110 as_bad (_("zeroing-masking only allowed with write mask"));
11111 return NULL;
11112 }
11113
43234a1e
L
11114 return op_string;
11115}
11116
252b5132 11117static int
70e41ade 11118i386_immediate (char *imm_start)
252b5132
RH
11119{
11120 char *save_input_line_pointer;
f3c180ae 11121 char *gotfree_input_line;
252b5132 11122 segT exp_seg = 0;
47926f60 11123 expressionS *exp;
40fb9820
L
11124 i386_operand_type types;
11125
0dfbf9d7 11126 operand_type_set (&types, ~0);
252b5132
RH
11127
11128 if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
11129 {
31b2323c
L
11130 as_bad (_("at most %d immediate operands are allowed"),
11131 MAX_IMMEDIATE_OPERANDS);
252b5132
RH
11132 return 0;
11133 }
11134
11135 exp = &im_expressions[i.imm_operands++];
520dc8e8 11136 i.op[this_operand].imms = exp;
252b5132
RH
11137
11138 if (is_space_char (*imm_start))
11139 ++imm_start;
11140
11141 save_input_line_pointer = input_line_pointer;
11142 input_line_pointer = imm_start;
11143
d258b828 11144 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
11145 if (gotfree_input_line)
11146 input_line_pointer = gotfree_input_line;
252b5132
RH
11147
11148 exp_seg = expression (exp);
11149
83183c0c 11150 SKIP_WHITESPACE ();
252b5132 11151 if (*input_line_pointer)
f3c180ae 11152 as_bad (_("junk `%s' after expression"), input_line_pointer);
252b5132
RH
11153
11154 input_line_pointer = save_input_line_pointer;
f3c180ae 11155 if (gotfree_input_line)
ee86248c
JB
11156 {
11157 free (gotfree_input_line);
11158
9aac24b1 11159 if (exp->X_op == O_constant)
ee86248c
JB
11160 exp->X_op = O_illegal;
11161 }
11162
9aac24b1
JB
11163 if (exp_seg == reg_section)
11164 {
11165 as_bad (_("illegal immediate register operand %s"), imm_start);
11166 return 0;
11167 }
11168
ee86248c
JB
11169 return i386_finalize_immediate (exp_seg, exp, types, imm_start);
11170}
252b5132 11171
ee86248c
JB
11172static int
11173i386_finalize_immediate (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
11174 i386_operand_type types, const char *imm_start)
11175{
11176 if (exp->X_op == O_absent || exp->X_op == O_illegal || exp->X_op == O_big)
252b5132 11177 {
313c53d1
L
11178 if (imm_start)
11179 as_bad (_("missing or invalid immediate expression `%s'"),
11180 imm_start);
3992d3b7 11181 return 0;
252b5132 11182 }
3e73aa7c 11183 else if (exp->X_op == O_constant)
252b5132 11184 {
47926f60 11185 /* Size it properly later. */
40fb9820 11186 i.types[this_operand].bitfield.imm64 = 1;
a442cac5
JB
11187
11188 /* If not 64bit, sign/zero extend val, to account for wraparound
11189 when !BFD64. */
11190 if (flag_code != CODE_64BIT)
11191 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
252b5132 11192 }
4c63da97 11193#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
f86103b7 11194 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
31312f95 11195 && exp_seg != absolute_section
47926f60 11196 && exp_seg != text_section
24eab124
AM
11197 && exp_seg != data_section
11198 && exp_seg != bss_section
11199 && exp_seg != undefined_section
f86103b7 11200 && !bfd_is_com_section (exp_seg))
252b5132 11201 {
d0b47220 11202 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
252b5132
RH
11203 return 0;
11204 }
11205#endif
11206 else
11207 {
11208 /* This is an address. The size of the address will be
24eab124 11209 determined later, depending on destination register,
3e73aa7c 11210 suffix, or the default for the section. */
40fb9820
L
11211 i.types[this_operand].bitfield.imm8 = 1;
11212 i.types[this_operand].bitfield.imm16 = 1;
11213 i.types[this_operand].bitfield.imm32 = 1;
11214 i.types[this_operand].bitfield.imm32s = 1;
11215 i.types[this_operand].bitfield.imm64 = 1;
c6fb90c8
L
11216 i.types[this_operand] = operand_type_and (i.types[this_operand],
11217 types);
252b5132
RH
11218 }
11219
11220 return 1;
11221}
11222
551c1ca1 11223static char *
e3bb37b5 11224i386_scale (char *scale)
252b5132 11225{
551c1ca1
AM
11226 offsetT val;
11227 char *save = input_line_pointer;
252b5132 11228
551c1ca1
AM
11229 input_line_pointer = scale;
11230 val = get_absolute_expression ();
11231
11232 switch (val)
252b5132 11233 {
551c1ca1 11234 case 1:
252b5132
RH
11235 i.log2_scale_factor = 0;
11236 break;
551c1ca1 11237 case 2:
252b5132
RH
11238 i.log2_scale_factor = 1;
11239 break;
551c1ca1 11240 case 4:
252b5132
RH
11241 i.log2_scale_factor = 2;
11242 break;
551c1ca1 11243 case 8:
252b5132
RH
11244 i.log2_scale_factor = 3;
11245 break;
11246 default:
a724f0f4
JB
11247 {
11248 char sep = *input_line_pointer;
11249
11250 *input_line_pointer = '\0';
11251 as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
11252 scale);
11253 *input_line_pointer = sep;
11254 input_line_pointer = save;
11255 return NULL;
11256 }
252b5132 11257 }
29b0f896 11258 if (i.log2_scale_factor != 0 && i.index_reg == 0)
252b5132
RH
11259 {
11260 as_warn (_("scale factor of %d without an index register"),
24eab124 11261 1 << i.log2_scale_factor);
252b5132 11262 i.log2_scale_factor = 0;
252b5132 11263 }
551c1ca1
AM
11264 scale = input_line_pointer;
11265 input_line_pointer = save;
11266 return scale;
252b5132
RH
11267}
11268
252b5132 11269static int
e3bb37b5 11270i386_displacement (char *disp_start, char *disp_end)
252b5132 11271{
29b0f896 11272 expressionS *exp;
252b5132
RH
11273 segT exp_seg = 0;
11274 char *save_input_line_pointer;
f3c180ae 11275 char *gotfree_input_line;
40fb9820
L
11276 int override;
11277 i386_operand_type bigdisp, types = anydisp;
3992d3b7 11278 int ret;
252b5132 11279
31b2323c
L
11280 if (i.disp_operands == MAX_MEMORY_OPERANDS)
11281 {
11282 as_bad (_("at most %d displacement operands are allowed"),
11283 MAX_MEMORY_OPERANDS);
11284 return 0;
11285 }
11286
0dfbf9d7 11287 operand_type_set (&bigdisp, 0);
6f2f06be 11288 if (i.jumpabsolute
48bcea9f 11289 || i.types[this_operand].bitfield.baseindex
0cfa3eb3
JB
11290 || (current_templates->start->opcode_modifier.jump != JUMP
11291 && current_templates->start->opcode_modifier.jump != JUMP_DWORD))
e05278af 11292 {
48bcea9f 11293 i386_addressing_mode ();
e05278af 11294 override = (i.prefix[ADDR_PREFIX] != 0);
40fb9820
L
11295 if (flag_code == CODE_64BIT)
11296 {
a775efc8 11297 bigdisp.bitfield.disp32 = 1;
40fb9820 11298 if (!override)
a775efc8 11299 bigdisp.bitfield.disp64 = 1;
40fb9820
L
11300 }
11301 else if ((flag_code == CODE_16BIT) ^ override)
40fb9820 11302 bigdisp.bitfield.disp16 = 1;
48bcea9f
JB
11303 else
11304 bigdisp.bitfield.disp32 = 1;
e05278af
JB
11305 }
11306 else
11307 {
376cd056
JB
11308 /* For PC-relative branches, the width of the displacement may be
11309 dependent upon data size, but is never dependent upon address size.
11310 Also make sure to not unintentionally match against a non-PC-relative
11311 branch template. */
11312 static templates aux_templates;
11313 const insn_template *t = current_templates->start;
5b7c81bd 11314 bool has_intel64 = false;
376cd056
JB
11315
11316 aux_templates.start = t;
11317 while (++t < current_templates->end)
11318 {
11319 if (t->opcode_modifier.jump
11320 != current_templates->start->opcode_modifier.jump)
11321 break;
4b5aaf5f 11322 if ((t->opcode_modifier.isa64 >= INTEL64))
5b7c81bd 11323 has_intel64 = true;
376cd056
JB
11324 }
11325 if (t < current_templates->end)
11326 {
11327 aux_templates.end = t;
11328 current_templates = &aux_templates;
11329 }
11330
e05278af 11331 override = (i.prefix[DATA_PREFIX] != 0);
40fb9820
L
11332 if (flag_code == CODE_64BIT)
11333 {
376cd056
JB
11334 if ((override || i.suffix == WORD_MNEM_SUFFIX)
11335 && (!intel64 || !has_intel64))
40fb9820
L
11336 bigdisp.bitfield.disp16 = 1;
11337 else
a775efc8 11338 bigdisp.bitfield.disp32 = 1;
40fb9820
L
11339 }
11340 else
e05278af
JB
11341 {
11342 if (!override)
11343 override = (i.suffix == (flag_code != CODE_16BIT
11344 ? WORD_MNEM_SUFFIX
11345 : LONG_MNEM_SUFFIX));
40fb9820
L
11346 bigdisp.bitfield.disp32 = 1;
11347 if ((flag_code == CODE_16BIT) ^ override)
11348 {
11349 bigdisp.bitfield.disp32 = 0;
11350 bigdisp.bitfield.disp16 = 1;
11351 }
e05278af 11352 }
e05278af 11353 }
c6fb90c8
L
11354 i.types[this_operand] = operand_type_or (i.types[this_operand],
11355 bigdisp);
252b5132
RH
11356
11357 exp = &disp_expressions[i.disp_operands];
520dc8e8 11358 i.op[this_operand].disps = exp;
252b5132
RH
11359 i.disp_operands++;
11360 save_input_line_pointer = input_line_pointer;
11361 input_line_pointer = disp_start;
11362 END_STRING_AND_SAVE (disp_end);
11363
11364#ifndef GCC_ASM_O_HACK
11365#define GCC_ASM_O_HACK 0
11366#endif
11367#if GCC_ASM_O_HACK
11368 END_STRING_AND_SAVE (disp_end + 1);
40fb9820 11369 if (i.types[this_operand].bitfield.baseIndex
24eab124 11370 && displacement_string_end[-1] == '+')
252b5132
RH
11371 {
11372 /* This hack is to avoid a warning when using the "o"
24eab124
AM
11373 constraint within gcc asm statements.
11374 For instance:
11375
11376 #define _set_tssldt_desc(n,addr,limit,type) \
11377 __asm__ __volatile__ ( \
11378 "movw %w2,%0\n\t" \
11379 "movw %w1,2+%0\n\t" \
11380 "rorl $16,%1\n\t" \
11381 "movb %b1,4+%0\n\t" \
11382 "movb %4,5+%0\n\t" \
11383 "movb $0,6+%0\n\t" \
11384 "movb %h1,7+%0\n\t" \
11385 "rorl $16,%1" \
11386 : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
11387
11388 This works great except that the output assembler ends
11389 up looking a bit weird if it turns out that there is
11390 no offset. You end up producing code that looks like:
11391
11392 #APP
11393 movw $235,(%eax)
11394 movw %dx,2+(%eax)
11395 rorl $16,%edx
11396 movb %dl,4+(%eax)
11397 movb $137,5+(%eax)
11398 movb $0,6+(%eax)
11399 movb %dh,7+(%eax)
11400 rorl $16,%edx
11401 #NO_APP
11402
47926f60 11403 So here we provide the missing zero. */
24eab124
AM
11404
11405 *displacement_string_end = '0';
252b5132
RH
11406 }
11407#endif
d258b828 11408 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
11409 if (gotfree_input_line)
11410 input_line_pointer = gotfree_input_line;
252b5132 11411
24eab124 11412 exp_seg = expression (exp);
252b5132 11413
636c26b0
AM
11414 SKIP_WHITESPACE ();
11415 if (*input_line_pointer)
11416 as_bad (_("junk `%s' after expression"), input_line_pointer);
11417#if GCC_ASM_O_HACK
11418 RESTORE_END_STRING (disp_end + 1);
11419#endif
636c26b0 11420 input_line_pointer = save_input_line_pointer;
636c26b0 11421 if (gotfree_input_line)
ee86248c
JB
11422 {
11423 free (gotfree_input_line);
11424
11425 if (exp->X_op == O_constant || exp->X_op == O_register)
11426 exp->X_op = O_illegal;
11427 }
11428
11429 ret = i386_finalize_displacement (exp_seg, exp, types, disp_start);
11430
11431 RESTORE_END_STRING (disp_end);
11432
11433 return ret;
11434}
11435
11436static int
11437i386_finalize_displacement (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
11438 i386_operand_type types, const char *disp_start)
11439{
ee86248c 11440 int ret = 1;
636c26b0 11441
24eab124
AM
11442 /* We do this to make sure that the section symbol is in
11443 the symbol table. We will ultimately change the relocation
47926f60 11444 to be relative to the beginning of the section. */
1ae12ab7 11445 if (i.reloc[this_operand] == BFD_RELOC_386_GOTOFF
d6ab8113
JB
11446 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
11447 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
24eab124 11448 {
636c26b0 11449 if (exp->X_op != O_symbol)
3992d3b7 11450 goto inv_disp;
636c26b0 11451
e5cb08ac 11452 if (S_IS_LOCAL (exp->X_add_symbol)
c64efb4b
L
11453 && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section
11454 && S_GET_SEGMENT (exp->X_add_symbol) != expr_section)
24eab124 11455 section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
24eab124
AM
11456 exp->X_op = O_subtract;
11457 exp->X_op_symbol = GOT_symbol;
1ae12ab7 11458 if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
29b0f896 11459 i.reloc[this_operand] = BFD_RELOC_32_PCREL;
d6ab8113
JB
11460 else if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
11461 i.reloc[this_operand] = BFD_RELOC_64;
23df1078 11462 else
29b0f896 11463 i.reloc[this_operand] = BFD_RELOC_32;
24eab124 11464 }
252b5132 11465
3992d3b7
AM
11466 else if (exp->X_op == O_absent
11467 || exp->X_op == O_illegal
ee86248c 11468 || exp->X_op == O_big)
2daf4fd8 11469 {
3992d3b7
AM
11470 inv_disp:
11471 as_bad (_("missing or invalid displacement expression `%s'"),
2daf4fd8 11472 disp_start);
3992d3b7 11473 ret = 0;
2daf4fd8
AM
11474 }
11475
a50187b2
JB
11476 else if (exp->X_op == O_constant)
11477 {
11478 /* Sizing gets taken care of by optimize_disp().
11479
11480 If not 64bit, sign/zero extend val, to account for wraparound
11481 when !BFD64. */
11482 if (flag_code != CODE_64BIT)
11483 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
11484 }
11485
4c63da97 11486#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
a50187b2 11487 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
3992d3b7
AM
11488 && exp_seg != absolute_section
11489 && exp_seg != text_section
11490 && exp_seg != data_section
11491 && exp_seg != bss_section
11492 && exp_seg != undefined_section
11493 && !bfd_is_com_section (exp_seg))
24eab124 11494 {
d0b47220 11495 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
3992d3b7 11496 ret = 0;
24eab124 11497 }
252b5132 11498#endif
3956db08 11499
a50187b2 11500 else if (current_templates->start->opcode_modifier.jump == JUMP_BYTE)
48bcea9f
JB
11501 i.types[this_operand].bitfield.disp8 = 1;
11502
40fb9820 11503 /* Check if this is a displacement only operand. */
02b83698
JB
11504 if (!i.types[this_operand].bitfield.baseindex)
11505 i.types[this_operand] =
11506 operand_type_or (operand_type_and_not (i.types[this_operand], anydisp),
11507 operand_type_and (i.types[this_operand], types));
3956db08 11508
3992d3b7 11509 return ret;
252b5132
RH
11510}
11511
2abc2bec
JB
11512/* Return the active addressing mode, taking address override and
11513 registers forming the address into consideration. Update the
11514 address override prefix if necessary. */
47926f60 11515
2abc2bec
JB
11516static enum flag_code
11517i386_addressing_mode (void)
252b5132 11518{
be05d201
L
11519 enum flag_code addr_mode;
11520
11521 if (i.prefix[ADDR_PREFIX])
11522 addr_mode = flag_code == CODE_32BIT ? CODE_16BIT : CODE_32BIT;
a23b33b3
JB
11523 else if (flag_code == CODE_16BIT
11524 && current_templates->start->cpu_flags.bitfield.cpumpx
11525 /* Avoid replacing the "16-bit addressing not allowed" diagnostic
11526 from md_assemble() by "is not a valid base/index expression"
11527 when there is a base and/or index. */
11528 && !i.types[this_operand].bitfield.baseindex)
11529 {
11530 /* MPX insn memory operands with neither base nor index must be forced
11531 to use 32-bit addressing in 16-bit mode. */
11532 addr_mode = CODE_32BIT;
11533 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
11534 ++i.prefixes;
11535 gas_assert (!i.types[this_operand].bitfield.disp16);
11536 gas_assert (!i.types[this_operand].bitfield.disp32);
11537 }
be05d201
L
11538 else
11539 {
11540 addr_mode = flag_code;
11541
24eab124 11542#if INFER_ADDR_PREFIX
be05d201
L
11543 if (i.mem_operands == 0)
11544 {
11545 /* Infer address prefix from the first memory operand. */
11546 const reg_entry *addr_reg = i.base_reg;
11547
11548 if (addr_reg == NULL)
11549 addr_reg = i.index_reg;
eecb386c 11550
be05d201
L
11551 if (addr_reg)
11552 {
e968fc9b 11553 if (addr_reg->reg_type.bitfield.dword)
be05d201
L
11554 addr_mode = CODE_32BIT;
11555 else if (flag_code != CODE_64BIT
dc821c5f 11556 && addr_reg->reg_type.bitfield.word)
be05d201
L
11557 addr_mode = CODE_16BIT;
11558
11559 if (addr_mode != flag_code)
11560 {
11561 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
11562 i.prefixes += 1;
11563 /* Change the size of any displacement too. At most one
11564 of Disp16 or Disp32 is set.
11565 FIXME. There doesn't seem to be any real need for
11566 separate Disp16 and Disp32 flags. The same goes for
11567 Imm16 and Imm32. Removing them would probably clean
11568 up the code quite a lot. */
11569 if (flag_code != CODE_64BIT
11570 && (i.types[this_operand].bitfield.disp16
11571 || i.types[this_operand].bitfield.disp32))
05909f23
JB
11572 {
11573 static const i386_operand_type disp16_32 = {
11574 .bitfield = { .disp16 = 1, .disp32 = 1 }
11575 };
11576
11577 i.types[this_operand]
11578 = operand_type_xor (i.types[this_operand], disp16_32);
11579 }
be05d201
L
11580 }
11581 }
11582 }
24eab124 11583#endif
be05d201
L
11584 }
11585
2abc2bec
JB
11586 return addr_mode;
11587}
11588
11589/* Make sure the memory operand we've been dealt is valid.
11590 Return 1 on success, 0 on a failure. */
11591
11592static int
11593i386_index_check (const char *operand_string)
11594{
11595 const char *kind = "base/index";
11596 enum flag_code addr_mode = i386_addressing_mode ();
77a19f0e 11597 const insn_template *t = current_templates->end - 1;
2abc2bec 11598
77a19f0e 11599 if (t->opcode_modifier.isstring)
fc0763e6
JB
11600 {
11601 /* Memory operands of string insns are special in that they only allow
11602 a single register (rDI, rSI, or rBX) as their memory address. */
be05d201
L
11603 const reg_entry *expected_reg;
11604 static const char *di_si[][2] =
11605 {
11606 { "esi", "edi" },
11607 { "si", "di" },
11608 { "rsi", "rdi" }
11609 };
11610 static const char *bx[] = { "ebx", "bx", "rbx" };
fc0763e6
JB
11611
11612 kind = "string address";
11613
a152332d 11614 if (t->opcode_modifier.prefixok == PrefixRep)
fc0763e6 11615 {
77a19f0e 11616 int es_op = t->opcode_modifier.isstring - IS_STRING_ES_OP0;
51c8edf6 11617 int op = 0;
fc0763e6 11618
77a19f0e 11619 if (!t->operand_types[0].bitfield.baseindex
fc0763e6 11620 || ((!i.mem_operands != !intel_syntax)
77a19f0e 11621 && t->operand_types[1].bitfield.baseindex))
51c8edf6 11622 op = 1;
fe0e921f
AM
11623 expected_reg
11624 = (const reg_entry *) str_hash_find (reg_hash,
11625 di_si[addr_mode][op == es_op]);
fc0763e6
JB
11626 }
11627 else
fe0e921f
AM
11628 expected_reg
11629 = (const reg_entry *)str_hash_find (reg_hash, bx[addr_mode]);
fc0763e6 11630
be05d201
L
11631 if (i.base_reg != expected_reg
11632 || i.index_reg
fc0763e6 11633 || operand_type_check (i.types[this_operand], disp))
fc0763e6 11634 {
be05d201
L
11635 /* The second memory operand must have the same size as
11636 the first one. */
11637 if (i.mem_operands
11638 && i.base_reg
11639 && !((addr_mode == CODE_64BIT
dc821c5f 11640 && i.base_reg->reg_type.bitfield.qword)
be05d201 11641 || (addr_mode == CODE_32BIT
dc821c5f
JB
11642 ? i.base_reg->reg_type.bitfield.dword
11643 : i.base_reg->reg_type.bitfield.word)))
be05d201
L
11644 goto bad_address;
11645
fc0763e6
JB
11646 as_warn (_("`%s' is not valid here (expected `%c%s%s%c')"),
11647 operand_string,
11648 intel_syntax ? '[' : '(',
11649 register_prefix,
be05d201 11650 expected_reg->reg_name,
fc0763e6 11651 intel_syntax ? ']' : ')');
be05d201 11652 return 1;
fc0763e6 11653 }
be05d201
L
11654 else
11655 return 1;
11656
dc1e8a47 11657 bad_address:
be05d201
L
11658 as_bad (_("`%s' is not a valid %s expression"),
11659 operand_string, kind);
11660 return 0;
3e73aa7c
JH
11661 }
11662 else
11663 {
77a19f0e
JB
11664 t = current_templates->start;
11665
be05d201
L
11666 if (addr_mode != CODE_16BIT)
11667 {
11668 /* 32-bit/64-bit checks. */
41eb8e88
L
11669 if (i.disp_encoding == disp_encoding_16bit)
11670 {
11671 bad_disp:
11672 as_bad (_("invalid `%s' prefix"),
11673 addr_mode == CODE_16BIT ? "{disp32}" : "{disp16}");
11674 return 0;
11675 }
11676
be05d201 11677 if ((i.base_reg
e968fc9b
JB
11678 && ((addr_mode == CODE_64BIT
11679 ? !i.base_reg->reg_type.bitfield.qword
11680 : !i.base_reg->reg_type.bitfield.dword)
11681 || (i.index_reg && i.base_reg->reg_num == RegIP)
11682 || i.base_reg->reg_num == RegIZ))
be05d201 11683 || (i.index_reg
1b54b8d7
JB
11684 && !i.index_reg->reg_type.bitfield.xmmword
11685 && !i.index_reg->reg_type.bitfield.ymmword
11686 && !i.index_reg->reg_type.bitfield.zmmword
be05d201 11687 && ((addr_mode == CODE_64BIT
e968fc9b
JB
11688 ? !i.index_reg->reg_type.bitfield.qword
11689 : !i.index_reg->reg_type.bitfield.dword)
be05d201
L
11690 || !i.index_reg->reg_type.bitfield.baseindex)))
11691 goto bad_address;
8178be5b 11692
260cd341 11693 /* bndmk, bndldx, bndstx and mandatory non-vector SIB have special restrictions. */
7fc69528
JB
11694 if (t->mnem_off == MN_bndmk
11695 || t->mnem_off == MN_bndldx
11696 || t->mnem_off == MN_bndstx
a152332d 11697 || t->opcode_modifier.sib == SIBMEM)
8178be5b
JB
11698 {
11699 /* They cannot use RIP-relative addressing. */
e968fc9b 11700 if (i.base_reg && i.base_reg->reg_num == RegIP)
8178be5b
JB
11701 {
11702 as_bad (_("`%s' cannot be used here"), operand_string);
11703 return 0;
11704 }
11705
11706 /* bndldx and bndstx ignore their scale factor. */
7fc69528 11707 if ((t->mnem_off == MN_bndldx || t->mnem_off == MN_bndstx)
8178be5b
JB
11708 && i.log2_scale_factor)
11709 as_warn (_("register scaling is being ignored here"));
11710 }
be05d201
L
11711 }
11712 else
3e73aa7c 11713 {
be05d201 11714 /* 16-bit checks. */
41eb8e88
L
11715 if (i.disp_encoding == disp_encoding_32bit)
11716 goto bad_disp;
11717
3e73aa7c 11718 if ((i.base_reg
dc821c5f 11719 && (!i.base_reg->reg_type.bitfield.word
40fb9820 11720 || !i.base_reg->reg_type.bitfield.baseindex))
3e73aa7c 11721 || (i.index_reg
dc821c5f 11722 && (!i.index_reg->reg_type.bitfield.word
40fb9820 11723 || !i.index_reg->reg_type.bitfield.baseindex
29b0f896
AM
11724 || !(i.base_reg
11725 && i.base_reg->reg_num < 6
11726 && i.index_reg->reg_num >= 6
11727 && i.log2_scale_factor == 0))))
be05d201 11728 goto bad_address;
3e73aa7c
JH
11729 }
11730 }
be05d201 11731 return 1;
24eab124 11732}
252b5132 11733
43234a1e
L
11734/* Handle vector immediates. */
11735
11736static int
11737RC_SAE_immediate (const char *imm_start)
11738{
43234a1e 11739 const char *pstr = imm_start;
43234a1e
L
11740
11741 if (*pstr != '{')
11742 return 0;
11743
7063667e
JB
11744 pstr = RC_SAE_specifier (pstr + 1);
11745 if (pstr == NULL)
43234a1e
L
11746 return 0;
11747
11748 if (*pstr++ != '}')
11749 {
11750 as_bad (_("Missing '}': '%s'"), imm_start);
11751 return 0;
11752 }
11753 /* RC/SAE immediate string should contain nothing more. */;
11754 if (*pstr != 0)
11755 {
11756 as_bad (_("Junk after '}': '%s'"), imm_start);
11757 return 0;
11758 }
11759
cf665fee
JB
11760 /* Internally this doesn't count as an operand. */
11761 --i.operands;
43234a1e 11762
43234a1e
L
11763 return 1;
11764}
11765
9d299bea
JB
11766static INLINE bool starts_memory_operand (char c)
11767{
014fbcda 11768 return ISDIGIT (c)
4795cd4a 11769 || is_name_beginner (c)
014fbcda 11770 || strchr ("([\"+-!~", c);
9d299bea
JB
11771}
11772
fc0763e6 11773/* Parse OPERAND_STRING into the i386_insn structure I. Returns zero
47926f60 11774 on error. */
252b5132 11775
252b5132 11776static int
a7619375 11777i386_att_operand (char *operand_string)
252b5132 11778{
af6bdddf
AM
11779 const reg_entry *r;
11780 char *end_op;
24eab124 11781 char *op_string = operand_string;
252b5132 11782
24eab124 11783 if (is_space_char (*op_string))
252b5132
RH
11784 ++op_string;
11785
24eab124 11786 /* We check for an absolute prefix (differentiating,
47926f60 11787 for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
ccb05c9c
JB
11788 if (*op_string == ABSOLUTE_PREFIX
11789 && current_templates->start->opcode_modifier.jump)
24eab124
AM
11790 {
11791 ++op_string;
11792 if (is_space_char (*op_string))
11793 ++op_string;
5b7c81bd 11794 i.jumpabsolute = true;
24eab124 11795 }
252b5132 11796
47926f60 11797 /* Check if operand is a register. */
4d1bb795 11798 if ((r = parse_register (op_string, &end_op)) != NULL)
24eab124 11799 {
40fb9820
L
11800 i386_operand_type temp;
11801
8a6fb3f9
JB
11802 if (r == &bad_reg)
11803 return 0;
11804
24eab124
AM
11805 /* Check for a segment override by searching for ':' after a
11806 segment register. */
11807 op_string = end_op;
11808 if (is_space_char (*op_string))
11809 ++op_string;
00cee14f 11810 if (*op_string == ':' && r->reg_type.bitfield.class == SReg)
24eab124 11811 {
5e042380 11812 i.seg[i.mem_operands] = r;
252b5132 11813
24eab124 11814 /* Skip the ':' and whitespace. */
252b5132
RH
11815 ++op_string;
11816 if (is_space_char (*op_string))
24eab124 11817 ++op_string;
252b5132 11818
47926f60 11819 /* Handle case of %es:*foo. */
ccb05c9c
JB
11820 if (!i.jumpabsolute && *op_string == ABSOLUTE_PREFIX
11821 && current_templates->start->opcode_modifier.jump)
24eab124
AM
11822 {
11823 ++op_string;
11824 if (is_space_char (*op_string))
11825 ++op_string;
5b7c81bd 11826 i.jumpabsolute = true;
24eab124 11827 }
c8d541e2 11828
9d299bea 11829 if (!starts_memory_operand (*op_string))
c8d541e2
JB
11830 {
11831 as_bad (_("bad memory operand `%s'"), op_string);
11832 return 0;
11833 }
24eab124
AM
11834 goto do_memory_reference;
11835 }
43234a1e
L
11836
11837 /* Handle vector operations. */
11838 if (*op_string == '{')
11839 {
f70c6814 11840 op_string = check_VecOperations (op_string);
43234a1e
L
11841 if (op_string == NULL)
11842 return 0;
11843 }
11844
24eab124
AM
11845 if (*op_string)
11846 {
d0b47220 11847 as_bad (_("junk `%s' after register"), op_string);
24eab124
AM
11848 return 0;
11849 }
40fb9820
L
11850 temp = r->reg_type;
11851 temp.bitfield.baseindex = 0;
c6fb90c8
L
11852 i.types[this_operand] = operand_type_or (i.types[this_operand],
11853 temp);
7d5e4556 11854 i.types[this_operand].bitfield.unspecified = 0;
520dc8e8 11855 i.op[this_operand].regs = r;
24eab124 11856 i.reg_operands++;
cf665fee
JB
11857
11858 /* A GPR may follow an RC or SAE immediate only if a (vector) register
11859 operand was also present earlier on. */
11860 if (i.rounding.type != rc_none && temp.bitfield.class == Reg
11861 && i.reg_operands == 1)
11862 {
11863 unsigned int j;
11864
11865 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); ++j)
11866 if (i.rounding.type == RC_NamesTable[j].type)
11867 break;
11868 as_bad (_("`%s': misplaced `{%s}'"),
76d3f746 11869 insn_name (current_templates->start), RC_NamesTable[j].name);
cf665fee
JB
11870 return 0;
11871 }
24eab124 11872 }
af6bdddf
AM
11873 else if (*op_string == REGISTER_PREFIX)
11874 {
11875 as_bad (_("bad register name `%s'"), op_string);
11876 return 0;
11877 }
24eab124 11878 else if (*op_string == IMMEDIATE_PREFIX)
ce8a8b2f 11879 {
24eab124 11880 ++op_string;
6f2f06be 11881 if (i.jumpabsolute)
24eab124 11882 {
d0b47220 11883 as_bad (_("immediate operand illegal with absolute jump"));
24eab124
AM
11884 return 0;
11885 }
11886 if (!i386_immediate (op_string))
11887 return 0;
cf665fee
JB
11888 if (i.rounding.type != rc_none)
11889 {
11890 as_bad (_("`%s': RC/SAE operand must follow immediate operands"),
76d3f746 11891 insn_name (current_templates->start));
cf665fee
JB
11892 return 0;
11893 }
24eab124 11894 }
43234a1e
L
11895 else if (RC_SAE_immediate (operand_string))
11896 {
cf665fee
JB
11897 /* If it is a RC or SAE immediate, do the necessary placement check:
11898 Only another immediate or a GPR may precede it. */
11899 if (i.mem_operands || i.reg_operands + i.imm_operands > 1
11900 || (i.reg_operands == 1
11901 && i.op[0].regs->reg_type.bitfield.class != Reg))
11902 {
11903 as_bad (_("`%s': misplaced `%s'"),
76d3f746 11904 insn_name (current_templates->start), operand_string);
cf665fee
JB
11905 return 0;
11906 }
43234a1e 11907 }
9d299bea 11908 else if (starts_memory_operand (*op_string))
24eab124 11909 {
47926f60 11910 /* This is a memory reference of some sort. */
af6bdddf 11911 char *base_string;
252b5132 11912
47926f60 11913 /* Start and end of displacement string expression (if found). */
eecb386c
AM
11914 char *displacement_string_start;
11915 char *displacement_string_end;
252b5132 11916
24eab124 11917 do_memory_reference:
24eab124
AM
11918 /* Check for base index form. We detect the base index form by
11919 looking for an ')' at the end of the operand, searching
11920 for the '(' matching it, and finding a REGISTER_PREFIX or ','
11921 after the '('. */
af6bdddf 11922 base_string = op_string + strlen (op_string);
c3332e24 11923
43234a1e 11924 /* Handle vector operations. */
6b5ba0d4
JB
11925 --base_string;
11926 if (is_space_char (*base_string))
11927 --base_string;
11928
11929 if (*base_string == '}')
43234a1e 11930 {
6b5ba0d4
JB
11931 char *vop_start = NULL;
11932
11933 while (base_string-- > op_string)
11934 {
11935 if (*base_string == '"')
11936 break;
11937 if (*base_string != '{')
11938 continue;
11939
11940 vop_start = base_string;
11941
11942 --base_string;
11943 if (is_space_char (*base_string))
11944 --base_string;
11945
11946 if (*base_string != '}')
11947 break;
11948
11949 vop_start = NULL;
11950 }
11951
11952 if (!vop_start)
11953 {
11954 as_bad (_("unbalanced figure braces"));
11955 return 0;
11956 }
11957
f70c6814 11958 if (check_VecOperations (vop_start) == NULL)
43234a1e 11959 return 0;
43234a1e
L
11960 }
11961
47926f60 11962 /* If we only have a displacement, set-up for it to be parsed later. */
af6bdddf
AM
11963 displacement_string_start = op_string;
11964 displacement_string_end = base_string + 1;
252b5132 11965
24eab124
AM
11966 if (*base_string == ')')
11967 {
af6bdddf 11968 char *temp_string;
e87fb6a6
JB
11969 unsigned int parens_not_balanced = 0;
11970 bool in_quotes = false;
e68c3d59 11971
24eab124 11972 /* We've already checked that the number of left & right ()'s are
e87fb6a6
JB
11973 equal, and that there's a matching set of double quotes. */
11974 end_op = base_string;
11975 for (temp_string = op_string; temp_string < end_op; temp_string++)
24eab124 11976 {
e87fb6a6
JB
11977 if (*temp_string == '\\' && temp_string[1] == '"')
11978 ++temp_string;
11979 else if (*temp_string == '"')
11980 in_quotes = !in_quotes;
11981 else if (!in_quotes)
11982 {
11983 if (*temp_string == '(' && !parens_not_balanced++)
11984 base_string = temp_string;
11985 if (*temp_string == ')')
11986 --parens_not_balanced;
11987 }
24eab124 11988 }
c3332e24 11989
af6bdddf 11990 temp_string = base_string;
c3332e24 11991
24eab124 11992 /* Skip past '(' and whitespace. */
100f993c
L
11993 if (*base_string != '(')
11994 {
11995 as_bad (_("unbalanced braces"));
11996 return 0;
11997 }
e87fb6a6 11998 ++base_string;
252b5132 11999 if (is_space_char (*base_string))
24eab124 12000 ++base_string;
252b5132 12001
af6bdddf 12002 if (*base_string == ','
4eed87de
AM
12003 || ((i.base_reg = parse_register (base_string, &end_op))
12004 != NULL))
252b5132 12005 {
af6bdddf 12006 displacement_string_end = temp_string;
252b5132 12007
40fb9820 12008 i.types[this_operand].bitfield.baseindex = 1;
252b5132 12009
af6bdddf 12010 if (i.base_reg)
24eab124 12011 {
8a6fb3f9
JB
12012 if (i.base_reg == &bad_reg)
12013 return 0;
24eab124
AM
12014 base_string = end_op;
12015 if (is_space_char (*base_string))
12016 ++base_string;
af6bdddf
AM
12017 }
12018
12019 /* There may be an index reg or scale factor here. */
12020 if (*base_string == ',')
12021 {
12022 ++base_string;
12023 if (is_space_char (*base_string))
12024 ++base_string;
12025
4eed87de
AM
12026 if ((i.index_reg = parse_register (base_string, &end_op))
12027 != NULL)
24eab124 12028 {
8a6fb3f9
JB
12029 if (i.index_reg == &bad_reg)
12030 return 0;
af6bdddf 12031 base_string = end_op;
24eab124
AM
12032 if (is_space_char (*base_string))
12033 ++base_string;
af6bdddf
AM
12034 if (*base_string == ',')
12035 {
12036 ++base_string;
12037 if (is_space_char (*base_string))
12038 ++base_string;
12039 }
e5cb08ac 12040 else if (*base_string != ')')
af6bdddf 12041 {
4eed87de
AM
12042 as_bad (_("expecting `,' or `)' "
12043 "after index register in `%s'"),
af6bdddf
AM
12044 operand_string);
12045 return 0;
12046 }
24eab124 12047 }
af6bdddf 12048 else if (*base_string == REGISTER_PREFIX)
24eab124 12049 {
f76bf5e0
L
12050 end_op = strchr (base_string, ',');
12051 if (end_op)
12052 *end_op = '\0';
af6bdddf 12053 as_bad (_("bad register name `%s'"), base_string);
24eab124
AM
12054 return 0;
12055 }
252b5132 12056
47926f60 12057 /* Check for scale factor. */
551c1ca1 12058 if (*base_string != ')')
af6bdddf 12059 {
551c1ca1
AM
12060 char *end_scale = i386_scale (base_string);
12061
12062 if (!end_scale)
af6bdddf 12063 return 0;
24eab124 12064
551c1ca1 12065 base_string = end_scale;
af6bdddf
AM
12066 if (is_space_char (*base_string))
12067 ++base_string;
12068 if (*base_string != ')')
12069 {
4eed87de
AM
12070 as_bad (_("expecting `)' "
12071 "after scale factor in `%s'"),
af6bdddf
AM
12072 operand_string);
12073 return 0;
12074 }
12075 }
12076 else if (!i.index_reg)
24eab124 12077 {
4eed87de
AM
12078 as_bad (_("expecting index register or scale factor "
12079 "after `,'; got '%c'"),
af6bdddf 12080 *base_string);
24eab124
AM
12081 return 0;
12082 }
12083 }
af6bdddf 12084 else if (*base_string != ')')
24eab124 12085 {
4eed87de
AM
12086 as_bad (_("expecting `,' or `)' "
12087 "after base register in `%s'"),
af6bdddf 12088 operand_string);
24eab124
AM
12089 return 0;
12090 }
c3332e24 12091 }
af6bdddf 12092 else if (*base_string == REGISTER_PREFIX)
c3332e24 12093 {
f76bf5e0
L
12094 end_op = strchr (base_string, ',');
12095 if (end_op)
12096 *end_op = '\0';
af6bdddf 12097 as_bad (_("bad register name `%s'"), base_string);
24eab124 12098 return 0;
c3332e24 12099 }
24eab124
AM
12100 }
12101
12102 /* If there's an expression beginning the operand, parse it,
12103 assuming displacement_string_start and
12104 displacement_string_end are meaningful. */
12105 if (displacement_string_start != displacement_string_end)
12106 {
12107 if (!i386_displacement (displacement_string_start,
12108 displacement_string_end))
12109 return 0;
12110 }
12111
12112 /* Special case for (%dx) while doing input/output op. */
12113 if (i.base_reg
75e5731b
JB
12114 && i.base_reg->reg_type.bitfield.instance == RegD
12115 && i.base_reg->reg_type.bitfield.word
24eab124
AM
12116 && i.index_reg == 0
12117 && i.log2_scale_factor == 0
12118 && i.seg[i.mem_operands] == 0
40fb9820 12119 && !operand_type_check (i.types[this_operand], disp))
24eab124 12120 {
2fb5be8d 12121 i.types[this_operand] = i.base_reg->reg_type;
9373f275 12122 i.input_output_operand = true;
24eab124
AM
12123 return 1;
12124 }
12125
eecb386c
AM
12126 if (i386_index_check (operand_string) == 0)
12127 return 0;
c48dadc9 12128 i.flags[this_operand] |= Operand_Mem;
24eab124
AM
12129 i.mem_operands++;
12130 }
12131 else
ce8a8b2f
AM
12132 {
12133 /* It's not a memory operand; argh! */
24eab124
AM
12134 as_bad (_("invalid char %s beginning operand %d `%s'"),
12135 output_invalid (*op_string),
12136 this_operand + 1,
12137 op_string);
12138 return 0;
12139 }
47926f60 12140 return 1; /* Normal return. */
252b5132
RH
12141}
12142\f
fa94de6b
RM
12143/* Calculate the maximum variable size (i.e., excluding fr_fix)
12144 that an rs_machine_dependent frag may reach. */
12145
12146unsigned int
12147i386_frag_max_var (fragS *frag)
12148{
12149 /* The only relaxable frags are for jumps.
12150 Unconditional jumps can grow by 4 bytes and others by 5 bytes. */
12151 gas_assert (frag->fr_type == rs_machine_dependent);
12152 return TYPE_FROM_RELAX_STATE (frag->fr_subtype) == UNCOND_JUMP ? 4 : 5;
12153}
12154
b084df0b
L
12155#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
12156static int
8dcea932 12157elf_symbol_resolved_in_segment_p (symbolS *fr_symbol, offsetT fr_var)
b084df0b
L
12158{
12159 /* STT_GNU_IFUNC symbol must go through PLT. */
12160 if ((symbol_get_bfdsym (fr_symbol)->flags
12161 & BSF_GNU_INDIRECT_FUNCTION) != 0)
12162 return 0;
12163
12164 if (!S_IS_EXTERNAL (fr_symbol))
12165 /* Symbol may be weak or local. */
12166 return !S_IS_WEAK (fr_symbol);
12167
8dcea932
L
12168 /* Global symbols with non-default visibility can't be preempted. */
12169 if (ELF_ST_VISIBILITY (S_GET_OTHER (fr_symbol)) != STV_DEFAULT)
12170 return 1;
12171
12172 if (fr_var != NO_RELOC)
12173 switch ((enum bfd_reloc_code_real) fr_var)
12174 {
12175 case BFD_RELOC_386_PLT32:
12176 case BFD_RELOC_X86_64_PLT32:
33eaf5de 12177 /* Symbol with PLT relocation may be preempted. */
8dcea932
L
12178 return 0;
12179 default:
12180 abort ();
12181 }
12182
b084df0b
L
12183 /* Global symbols with default visibility in a shared library may be
12184 preempted by another definition. */
8dcea932 12185 return !shared;
b084df0b
L
12186}
12187#endif
12188
79d72f45
HL
12189/* Table 3-2. Macro-Fusible Instructions in Haswell Microarchitecture
12190 Note also work for Skylake and Cascadelake.
12191---------------------------------------------------------------------
12192| JCC | ADD/SUB/CMP | INC/DEC | TEST/AND |
12193| ------ | ----------- | ------- | -------- |
12194| Jo | N | N | Y |
12195| Jno | N | N | Y |
12196| Jc/Jb | Y | N | Y |
12197| Jae/Jnb | Y | N | Y |
12198| Je/Jz | Y | Y | Y |
12199| Jne/Jnz | Y | Y | Y |
12200| Jna/Jbe | Y | N | Y |
12201| Ja/Jnbe | Y | N | Y |
12202| Js | N | N | Y |
12203| Jns | N | N | Y |
12204| Jp/Jpe | N | N | Y |
12205| Jnp/Jpo | N | N | Y |
12206| Jl/Jnge | Y | Y | Y |
12207| Jge/Jnl | Y | Y | Y |
12208| Jle/Jng | Y | Y | Y |
12209| Jg/Jnle | Y | Y | Y |
12210--------------------------------------------------------------------- */
12211static int
12212i386_macro_fusible_p (enum mf_cmp_kind mf_cmp, enum mf_jcc_kind mf_jcc)
12213{
12214 if (mf_cmp == mf_cmp_alu_cmp)
12215 return ((mf_jcc >= mf_jcc_jc && mf_jcc <= mf_jcc_jna)
12216 || mf_jcc == mf_jcc_jl || mf_jcc == mf_jcc_jle);
12217 if (mf_cmp == mf_cmp_incdec)
12218 return (mf_jcc == mf_jcc_je || mf_jcc == mf_jcc_jl
12219 || mf_jcc == mf_jcc_jle);
12220 if (mf_cmp == mf_cmp_test_and)
12221 return 1;
12222 return 0;
12223}
12224
e379e5f3
L
12225/* Return the next non-empty frag. */
12226
12227static fragS *
12228i386_next_non_empty_frag (fragS *fragP)
12229{
12230 /* There may be a frag with a ".fill 0" when there is no room in
12231 the current frag for frag_grow in output_insn. */
12232 for (fragP = fragP->fr_next;
12233 (fragP != NULL
12234 && fragP->fr_type == rs_fill
12235 && fragP->fr_fix == 0);
12236 fragP = fragP->fr_next)
12237 ;
12238 return fragP;
12239}
12240
12241/* Return the next jcc frag after BRANCH_PADDING. */
12242
12243static fragS *
79d72f45 12244i386_next_fusible_jcc_frag (fragS *maybe_cmp_fragP, fragS *pad_fragP)
e379e5f3 12245{
79d72f45
HL
12246 fragS *branch_fragP;
12247 if (!pad_fragP)
e379e5f3
L
12248 return NULL;
12249
79d72f45
HL
12250 if (pad_fragP->fr_type == rs_machine_dependent
12251 && (TYPE_FROM_RELAX_STATE (pad_fragP->fr_subtype)
e379e5f3
L
12252 == BRANCH_PADDING))
12253 {
79d72f45
HL
12254 branch_fragP = i386_next_non_empty_frag (pad_fragP);
12255 if (branch_fragP->fr_type != rs_machine_dependent)
e379e5f3 12256 return NULL;
79d72f45
HL
12257 if (TYPE_FROM_RELAX_STATE (branch_fragP->fr_subtype) == COND_JUMP
12258 && i386_macro_fusible_p (maybe_cmp_fragP->tc_frag_data.mf_type,
12259 pad_fragP->tc_frag_data.mf_type))
12260 return branch_fragP;
e379e5f3
L
12261 }
12262
12263 return NULL;
12264}
12265
12266/* Classify BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags. */
12267
12268static void
12269i386_classify_machine_dependent_frag (fragS *fragP)
12270{
12271 fragS *cmp_fragP;
12272 fragS *pad_fragP;
12273 fragS *branch_fragP;
12274 fragS *next_fragP;
12275 unsigned int max_prefix_length;
12276
12277 if (fragP->tc_frag_data.classified)
12278 return;
12279
12280 /* First scan for BRANCH_PADDING and FUSED_JCC_PADDING. Convert
12281 FUSED_JCC_PADDING and merge BRANCH_PADDING. */
12282 for (next_fragP = fragP;
12283 next_fragP != NULL;
12284 next_fragP = next_fragP->fr_next)
12285 {
12286 next_fragP->tc_frag_data.classified = 1;
12287 if (next_fragP->fr_type == rs_machine_dependent)
12288 switch (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype))
12289 {
12290 case BRANCH_PADDING:
12291 /* The BRANCH_PADDING frag must be followed by a branch
12292 frag. */
12293 branch_fragP = i386_next_non_empty_frag (next_fragP);
12294 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
12295 break;
12296 case FUSED_JCC_PADDING:
12297 /* Check if this is a fused jcc:
12298 FUSED_JCC_PADDING
12299 CMP like instruction
12300 BRANCH_PADDING
12301 COND_JUMP
12302 */
12303 cmp_fragP = i386_next_non_empty_frag (next_fragP);
12304 pad_fragP = i386_next_non_empty_frag (cmp_fragP);
79d72f45 12305 branch_fragP = i386_next_fusible_jcc_frag (next_fragP, pad_fragP);
e379e5f3
L
12306 if (branch_fragP)
12307 {
12308 /* The BRANCH_PADDING frag is merged with the
12309 FUSED_JCC_PADDING frag. */
12310 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
12311 /* CMP like instruction size. */
12312 next_fragP->tc_frag_data.cmp_size = cmp_fragP->fr_fix;
12313 frag_wane (pad_fragP);
12314 /* Skip to branch_fragP. */
12315 next_fragP = branch_fragP;
12316 }
12317 else if (next_fragP->tc_frag_data.max_prefix_length)
12318 {
12319 /* Turn FUSED_JCC_PADDING into BRANCH_PREFIX if it isn't
12320 a fused jcc. */
12321 next_fragP->fr_subtype
12322 = ENCODE_RELAX_STATE (BRANCH_PREFIX, 0);
12323 next_fragP->tc_frag_data.max_bytes
12324 = next_fragP->tc_frag_data.max_prefix_length;
12325 /* This will be updated in the BRANCH_PREFIX scan. */
12326 next_fragP->tc_frag_data.max_prefix_length = 0;
12327 }
12328 else
12329 frag_wane (next_fragP);
12330 break;
12331 }
12332 }
12333
12334 /* Stop if there is no BRANCH_PREFIX. */
12335 if (!align_branch_prefix_size)
12336 return;
12337
12338 /* Scan for BRANCH_PREFIX. */
12339 for (; fragP != NULL; fragP = fragP->fr_next)
12340 {
12341 if (fragP->fr_type != rs_machine_dependent
12342 || (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
12343 != BRANCH_PREFIX))
12344 continue;
12345
12346 /* Count all BRANCH_PREFIX frags before BRANCH_PADDING and
12347 COND_JUMP_PREFIX. */
12348 max_prefix_length = 0;
12349 for (next_fragP = fragP;
12350 next_fragP != NULL;
12351 next_fragP = next_fragP->fr_next)
12352 {
12353 if (next_fragP->fr_type == rs_fill)
12354 /* Skip rs_fill frags. */
12355 continue;
12356 else if (next_fragP->fr_type != rs_machine_dependent)
12357 /* Stop for all other frags. */
12358 break;
12359
12360 /* rs_machine_dependent frags. */
12361 if (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12362 == BRANCH_PREFIX)
12363 {
12364 /* Count BRANCH_PREFIX frags. */
12365 if (max_prefix_length >= MAX_FUSED_JCC_PADDING_SIZE)
12366 {
12367 max_prefix_length = MAX_FUSED_JCC_PADDING_SIZE;
12368 frag_wane (next_fragP);
12369 }
12370 else
12371 max_prefix_length
12372 += next_fragP->tc_frag_data.max_bytes;
12373 }
12374 else if ((TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12375 == BRANCH_PADDING)
12376 || (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12377 == FUSED_JCC_PADDING))
12378 {
12379 /* Stop at BRANCH_PADDING and FUSED_JCC_PADDING. */
12380 fragP->tc_frag_data.u.padding_fragP = next_fragP;
12381 break;
12382 }
12383 else
12384 /* Stop for other rs_machine_dependent frags. */
12385 break;
12386 }
12387
12388 fragP->tc_frag_data.max_prefix_length = max_prefix_length;
12389
12390 /* Skip to the next frag. */
12391 fragP = next_fragP;
12392 }
12393}
12394
12395/* Compute padding size for
12396
12397 FUSED_JCC_PADDING
12398 CMP like instruction
12399 BRANCH_PADDING
12400 COND_JUMP/UNCOND_JUMP
12401
12402 or
12403
12404 BRANCH_PADDING
12405 COND_JUMP/UNCOND_JUMP
12406 */
12407
12408static int
12409i386_branch_padding_size (fragS *fragP, offsetT address)
12410{
12411 unsigned int offset, size, padding_size;
12412 fragS *branch_fragP = fragP->tc_frag_data.u.branch_fragP;
12413
12414 /* The start address of the BRANCH_PADDING or FUSED_JCC_PADDING frag. */
12415 if (!address)
12416 address = fragP->fr_address;
12417 address += fragP->fr_fix;
12418
12419 /* CMP like instrunction size. */
12420 size = fragP->tc_frag_data.cmp_size;
12421
12422 /* The base size of the branch frag. */
12423 size += branch_fragP->fr_fix;
12424
12425 /* Add opcode and displacement bytes for the rs_machine_dependent
12426 branch frag. */
12427 if (branch_fragP->fr_type == rs_machine_dependent)
12428 size += md_relax_table[branch_fragP->fr_subtype].rlx_length;
12429
12430 /* Check if branch is within boundary and doesn't end at the last
12431 byte. */
12432 offset = address & ((1U << align_branch_power) - 1);
12433 if ((offset + size) >= (1U << align_branch_power))
12434 /* Padding needed to avoid crossing boundary. */
12435 padding_size = (1U << align_branch_power) - offset;
12436 else
12437 /* No padding needed. */
12438 padding_size = 0;
12439
12440 /* The return value may be saved in tc_frag_data.length which is
12441 unsigned byte. */
12442 if (!fits_in_unsigned_byte (padding_size))
12443 abort ();
12444
12445 return padding_size;
12446}
12447
12448/* i386_generic_table_relax_frag()
12449
12450 Handle BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags to
12451 grow/shrink padding to align branch frags. Hand others to
12452 relax_frag(). */
12453
12454long
12455i386_generic_table_relax_frag (segT segment, fragS *fragP, long stretch)
12456{
12457 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12458 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
12459 {
12460 long padding_size = i386_branch_padding_size (fragP, 0);
12461 long grow = padding_size - fragP->tc_frag_data.length;
12462
12463 /* When the BRANCH_PREFIX frag is used, the computed address
12464 must match the actual address and there should be no padding. */
12465 if (fragP->tc_frag_data.padding_address
12466 && (fragP->tc_frag_data.padding_address != fragP->fr_address
12467 || padding_size))
12468 abort ();
12469
12470 /* Update the padding size. */
12471 if (grow)
12472 fragP->tc_frag_data.length = padding_size;
12473
12474 return grow;
12475 }
12476 else if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12477 {
12478 fragS *padding_fragP, *next_fragP;
12479 long padding_size, left_size, last_size;
12480
12481 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
12482 if (!padding_fragP)
12483 /* Use the padding set by the leading BRANCH_PREFIX frag. */
12484 return (fragP->tc_frag_data.length
12485 - fragP->tc_frag_data.last_length);
12486
12487 /* Compute the relative address of the padding frag in the very
12488 first time where the BRANCH_PREFIX frag sizes are zero. */
12489 if (!fragP->tc_frag_data.padding_address)
12490 fragP->tc_frag_data.padding_address
12491 = padding_fragP->fr_address - (fragP->fr_address - stretch);
12492
12493 /* First update the last length from the previous interation. */
12494 left_size = fragP->tc_frag_data.prefix_length;
12495 for (next_fragP = fragP;
12496 next_fragP != padding_fragP;
12497 next_fragP = next_fragP->fr_next)
12498 if (next_fragP->fr_type == rs_machine_dependent
12499 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12500 == BRANCH_PREFIX))
12501 {
12502 if (left_size)
12503 {
12504 int max = next_fragP->tc_frag_data.max_bytes;
12505 if (max)
12506 {
12507 int size;
12508 if (max > left_size)
12509 size = left_size;
12510 else
12511 size = max;
12512 left_size -= size;
12513 next_fragP->tc_frag_data.last_length = size;
12514 }
12515 }
12516 else
12517 next_fragP->tc_frag_data.last_length = 0;
12518 }
12519
12520 /* Check the padding size for the padding frag. */
12521 padding_size = i386_branch_padding_size
12522 (padding_fragP, (fragP->fr_address
12523 + fragP->tc_frag_data.padding_address));
12524
12525 last_size = fragP->tc_frag_data.prefix_length;
12526 /* Check if there is change from the last interation. */
12527 if (padding_size == last_size)
12528 {
12529 /* Update the expected address of the padding frag. */
12530 padding_fragP->tc_frag_data.padding_address
12531 = (fragP->fr_address + padding_size
12532 + fragP->tc_frag_data.padding_address);
12533 return 0;
12534 }
12535
12536 if (padding_size > fragP->tc_frag_data.max_prefix_length)
12537 {
12538 /* No padding if there is no sufficient room. Clear the
12539 expected address of the padding frag. */
12540 padding_fragP->tc_frag_data.padding_address = 0;
12541 padding_size = 0;
12542 }
12543 else
12544 /* Store the expected address of the padding frag. */
12545 padding_fragP->tc_frag_data.padding_address
12546 = (fragP->fr_address + padding_size
12547 + fragP->tc_frag_data.padding_address);
12548
12549 fragP->tc_frag_data.prefix_length = padding_size;
12550
12551 /* Update the length for the current interation. */
12552 left_size = padding_size;
12553 for (next_fragP = fragP;
12554 next_fragP != padding_fragP;
12555 next_fragP = next_fragP->fr_next)
12556 if (next_fragP->fr_type == rs_machine_dependent
12557 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12558 == BRANCH_PREFIX))
12559 {
12560 if (left_size)
12561 {
12562 int max = next_fragP->tc_frag_data.max_bytes;
12563 if (max)
12564 {
12565 int size;
12566 if (max > left_size)
12567 size = left_size;
12568 else
12569 size = max;
12570 left_size -= size;
12571 next_fragP->tc_frag_data.length = size;
12572 }
12573 }
12574 else
12575 next_fragP->tc_frag_data.length = 0;
12576 }
12577
12578 return (fragP->tc_frag_data.length
12579 - fragP->tc_frag_data.last_length);
12580 }
12581 return relax_frag (segment, fragP, stretch);
12582}
12583
ee7fcc42
AM
12584/* md_estimate_size_before_relax()
12585
12586 Called just before relax() for rs_machine_dependent frags. The x86
12587 assembler uses these frags to handle variable size jump
12588 instructions.
12589
12590 Any symbol that is now undefined will not become defined.
12591 Return the correct fr_subtype in the frag.
12592 Return the initial "guess for variable size of frag" to caller.
12593 The guess is actually the growth beyond the fixed part. Whatever
12594 we do to grow the fixed or variable part contributes to our
12595 returned value. */
12596
252b5132 12597int
7016a5d5 12598md_estimate_size_before_relax (fragS *fragP, segT segment)
252b5132 12599{
e379e5f3
L
12600 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12601 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX
12602 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
12603 {
12604 i386_classify_machine_dependent_frag (fragP);
12605 return fragP->tc_frag_data.length;
12606 }
12607
252b5132 12608 /* We've already got fragP->fr_subtype right; all we have to do is
b98ef147
AM
12609 check for un-relaxable symbols. On an ELF system, we can't relax
12610 an externally visible symbol, because it may be overridden by a
12611 shared library. */
12612 if (S_GET_SEGMENT (fragP->fr_symbol) != segment
6d249963 12613#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12614 || (IS_ELF
8dcea932
L
12615 && !elf_symbol_resolved_in_segment_p (fragP->fr_symbol,
12616 fragP->fr_var))
fbeb56a4
DK
12617#endif
12618#if defined (OBJ_COFF) && defined (TE_PE)
7ab9ffdd 12619 || (OUTPUT_FLAVOR == bfd_target_coff_flavour
fbeb56a4 12620 && S_IS_WEAK (fragP->fr_symbol))
b98ef147
AM
12621#endif
12622 )
252b5132 12623 {
b98ef147
AM
12624 /* Symbol is undefined in this segment, or we need to keep a
12625 reloc so that weak symbols can be overridden. */
12626 int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
f86103b7 12627 enum bfd_reloc_code_real reloc_type;
ee7fcc42
AM
12628 unsigned char *opcode;
12629 int old_fr_fix;
eb19308f 12630 fixS *fixP = NULL;
f6af82bd 12631
ee7fcc42 12632 if (fragP->fr_var != NO_RELOC)
1e9cc1c2 12633 reloc_type = (enum bfd_reloc_code_real) fragP->fr_var;
b98ef147 12634 else if (size == 2)
f6af82bd 12635 reloc_type = BFD_RELOC_16_PCREL;
bd7ab16b 12636#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1ef3994a
JB
12637 else if (fragP->tc_frag_data.code64 && fragP->fr_offset == 0
12638 && need_plt32_p (fragP->fr_symbol))
bd7ab16b
L
12639 reloc_type = BFD_RELOC_X86_64_PLT32;
12640#endif
f6af82bd
AM
12641 else
12642 reloc_type = BFD_RELOC_32_PCREL;
252b5132 12643
ee7fcc42
AM
12644 old_fr_fix = fragP->fr_fix;
12645 opcode = (unsigned char *) fragP->fr_opcode;
12646
fddf5b5b 12647 switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
252b5132 12648 {
fddf5b5b
AM
12649 case UNCOND_JUMP:
12650 /* Make jmp (0xeb) a (d)word displacement jump. */
47926f60 12651 opcode[0] = 0xe9;
252b5132 12652 fragP->fr_fix += size;
eb19308f
JB
12653 fixP = fix_new (fragP, old_fr_fix, size,
12654 fragP->fr_symbol,
12655 fragP->fr_offset, 1,
12656 reloc_type);
252b5132
RH
12657 break;
12658
fddf5b5b 12659 case COND_JUMP86:
412167cb
AM
12660 if (size == 2
12661 && (!no_cond_jump_promotion || fragP->fr_var != NO_RELOC))
fddf5b5b
AM
12662 {
12663 /* Negate the condition, and branch past an
12664 unconditional jump. */
12665 opcode[0] ^= 1;
12666 opcode[1] = 3;
12667 /* Insert an unconditional jump. */
12668 opcode[2] = 0xe9;
12669 /* We added two extra opcode bytes, and have a two byte
12670 offset. */
12671 fragP->fr_fix += 2 + 2;
062cd5e7
AS
12672 fix_new (fragP, old_fr_fix + 2, 2,
12673 fragP->fr_symbol,
12674 fragP->fr_offset, 1,
12675 reloc_type);
fddf5b5b
AM
12676 break;
12677 }
12678 /* Fall through. */
12679
12680 case COND_JUMP:
412167cb
AM
12681 if (no_cond_jump_promotion && fragP->fr_var == NO_RELOC)
12682 {
12683 fragP->fr_fix += 1;
3e02c1cc
AM
12684 fixP = fix_new (fragP, old_fr_fix, 1,
12685 fragP->fr_symbol,
12686 fragP->fr_offset, 1,
12687 BFD_RELOC_8_PCREL);
12688 fixP->fx_signed = 1;
412167cb
AM
12689 break;
12690 }
93c2a809 12691
24eab124 12692 /* This changes the byte-displacement jump 0x7N
fddf5b5b 12693 to the (d)word-displacement jump 0x0f,0x8N. */
252b5132 12694 opcode[1] = opcode[0] + 0x10;
f6af82bd 12695 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
47926f60
KH
12696 /* We've added an opcode byte. */
12697 fragP->fr_fix += 1 + size;
eb19308f
JB
12698 fixP = fix_new (fragP, old_fr_fix + 1, size,
12699 fragP->fr_symbol,
12700 fragP->fr_offset, 1,
12701 reloc_type);
252b5132 12702 break;
fddf5b5b
AM
12703
12704 default:
12705 BAD_CASE (fragP->fr_subtype);
12706 break;
252b5132 12707 }
eb19308f
JB
12708
12709 /* All jumps handled here are signed, but don't unconditionally use a
12710 signed limit check for 32 and 16 bit jumps as we want to allow wrap
12711 around at 4G (outside of 64-bit mode) and 64k. */
12712 if (size == 4 && flag_code == CODE_64BIT)
12713 fixP->fx_signed = 1;
12714
252b5132 12715 frag_wane (fragP);
ee7fcc42 12716 return fragP->fr_fix - old_fr_fix;
252b5132 12717 }
93c2a809 12718
93c2a809
AM
12719 /* Guess size depending on current relax state. Initially the relax
12720 state will correspond to a short jump and we return 1, because
12721 the variable part of the frag (the branch offset) is one byte
12722 long. However, we can relax a section more than once and in that
12723 case we must either set fr_subtype back to the unrelaxed state,
12724 or return the value for the appropriate branch. */
12725 return md_relax_table[fragP->fr_subtype].rlx_length;
ee7fcc42
AM
12726}
12727
47926f60
KH
12728/* Called after relax() is finished.
12729
12730 In: Address of frag.
12731 fr_type == rs_machine_dependent.
12732 fr_subtype is what the address relaxed to.
12733
12734 Out: Any fixSs and constants are set up.
12735 Caller will turn frag into a ".space 0". */
12736
252b5132 12737void
7016a5d5
TG
12738md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec ATTRIBUTE_UNUSED,
12739 fragS *fragP)
252b5132 12740{
29b0f896 12741 unsigned char *opcode;
252b5132 12742 unsigned char *where_to_put_displacement = NULL;
847f7ad4
AM
12743 offsetT target_address;
12744 offsetT opcode_address;
252b5132 12745 unsigned int extension = 0;
847f7ad4 12746 offsetT displacement_from_opcode_start;
252b5132 12747
e379e5f3
L
12748 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12749 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING
12750 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12751 {
12752 /* Generate nop padding. */
12753 unsigned int size = fragP->tc_frag_data.length;
12754 if (size)
12755 {
12756 if (size > fragP->tc_frag_data.max_bytes)
12757 abort ();
12758
12759 if (flag_debug)
12760 {
12761 const char *msg;
12762 const char *branch = "branch";
12763 const char *prefix = "";
12764 fragS *padding_fragP;
12765 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
12766 == BRANCH_PREFIX)
12767 {
12768 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
12769 switch (fragP->tc_frag_data.default_prefix)
12770 {
12771 default:
12772 abort ();
12773 break;
12774 case CS_PREFIX_OPCODE:
12775 prefix = " cs";
12776 break;
12777 case DS_PREFIX_OPCODE:
12778 prefix = " ds";
12779 break;
12780 case ES_PREFIX_OPCODE:
12781 prefix = " es";
12782 break;
12783 case FS_PREFIX_OPCODE:
12784 prefix = " fs";
12785 break;
12786 case GS_PREFIX_OPCODE:
12787 prefix = " gs";
12788 break;
12789 case SS_PREFIX_OPCODE:
12790 prefix = " ss";
12791 break;
12792 }
12793 if (padding_fragP)
12794 msg = _("%s:%u: add %d%s at 0x%llx to align "
12795 "%s within %d-byte boundary\n");
12796 else
12797 msg = _("%s:%u: add additional %d%s at 0x%llx to "
12798 "align %s within %d-byte boundary\n");
12799 }
12800 else
12801 {
12802 padding_fragP = fragP;
12803 msg = _("%s:%u: add %d%s-byte nop at 0x%llx to align "
12804 "%s within %d-byte boundary\n");
12805 }
12806
12807 if (padding_fragP)
12808 switch (padding_fragP->tc_frag_data.branch_type)
12809 {
12810 case align_branch_jcc:
12811 branch = "jcc";
12812 break;
12813 case align_branch_fused:
12814 branch = "fused jcc";
12815 break;
12816 case align_branch_jmp:
12817 branch = "jmp";
12818 break;
12819 case align_branch_call:
12820 branch = "call";
12821 break;
12822 case align_branch_indirect:
12823 branch = "indiret branch";
12824 break;
12825 case align_branch_ret:
12826 branch = "ret";
12827 break;
12828 default:
12829 break;
12830 }
12831
12832 fprintf (stdout, msg,
12833 fragP->fr_file, fragP->fr_line, size, prefix,
12834 (long long) fragP->fr_address, branch,
12835 1 << align_branch_power);
12836 }
12837 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12838 memset (fragP->fr_opcode,
12839 fragP->tc_frag_data.default_prefix, size);
12840 else
12841 i386_generate_nops (fragP, (char *) fragP->fr_opcode,
12842 size, 0);
12843 fragP->fr_fix += size;
12844 }
12845 return;
12846 }
12847
252b5132
RH
12848 opcode = (unsigned char *) fragP->fr_opcode;
12849
47926f60 12850 /* Address we want to reach in file space. */
252b5132 12851 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
252b5132 12852
47926f60 12853 /* Address opcode resides at in file space. */
252b5132
RH
12854 opcode_address = fragP->fr_address + fragP->fr_fix;
12855
47926f60 12856 /* Displacement from opcode start to fill into instruction. */
252b5132
RH
12857 displacement_from_opcode_start = target_address - opcode_address;
12858
fddf5b5b 12859 if ((fragP->fr_subtype & BIG) == 0)
252b5132 12860 {
47926f60
KH
12861 /* Don't have to change opcode. */
12862 extension = 1; /* 1 opcode + 1 displacement */
252b5132 12863 where_to_put_displacement = &opcode[1];
fddf5b5b
AM
12864 }
12865 else
12866 {
12867 if (no_cond_jump_promotion
12868 && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
4eed87de
AM
12869 as_warn_where (fragP->fr_file, fragP->fr_line,
12870 _("long jump required"));
252b5132 12871
fddf5b5b
AM
12872 switch (fragP->fr_subtype)
12873 {
12874 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
12875 extension = 4; /* 1 opcode + 4 displacement */
12876 opcode[0] = 0xe9;
12877 where_to_put_displacement = &opcode[1];
12878 break;
252b5132 12879
fddf5b5b
AM
12880 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
12881 extension = 2; /* 1 opcode + 2 displacement */
12882 opcode[0] = 0xe9;
12883 where_to_put_displacement = &opcode[1];
12884 break;
252b5132 12885
fddf5b5b
AM
12886 case ENCODE_RELAX_STATE (COND_JUMP, BIG):
12887 case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
12888 extension = 5; /* 2 opcode + 4 displacement */
12889 opcode[1] = opcode[0] + 0x10;
12890 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12891 where_to_put_displacement = &opcode[2];
12892 break;
252b5132 12893
fddf5b5b
AM
12894 case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
12895 extension = 3; /* 2 opcode + 2 displacement */
12896 opcode[1] = opcode[0] + 0x10;
12897 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12898 where_to_put_displacement = &opcode[2];
12899 break;
252b5132 12900
fddf5b5b
AM
12901 case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
12902 extension = 4;
12903 opcode[0] ^= 1;
12904 opcode[1] = 3;
12905 opcode[2] = 0xe9;
12906 where_to_put_displacement = &opcode[3];
12907 break;
12908
12909 default:
12910 BAD_CASE (fragP->fr_subtype);
12911 break;
12912 }
252b5132 12913 }
fddf5b5b 12914
7b81dfbb
AJ
12915 /* If size if less then four we are sure that the operand fits,
12916 but if it's 4, then it could be that the displacement is larger
12917 then -/+ 2GB. */
12918 if (DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype) == 4
12919 && object_64bit
12920 && ((addressT) (displacement_from_opcode_start - extension
4eed87de
AM
12921 + ((addressT) 1 << 31))
12922 > (((addressT) 2 << 31) - 1)))
7b81dfbb
AJ
12923 {
12924 as_bad_where (fragP->fr_file, fragP->fr_line,
12925 _("jump target out of range"));
12926 /* Make us emit 0. */
12927 displacement_from_opcode_start = extension;
12928 }
47926f60 12929 /* Now put displacement after opcode. */
252b5132
RH
12930 md_number_to_chars ((char *) where_to_put_displacement,
12931 (valueT) (displacement_from_opcode_start - extension),
fddf5b5b 12932 DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
252b5132
RH
12933 fragP->fr_fix += extension;
12934}
12935\f
7016a5d5 12936/* Apply a fixup (fixP) to segment data, once it has been determined
252b5132
RH
12937 by our caller that we have all the info we need to fix it up.
12938
7016a5d5
TG
12939 Parameter valP is the pointer to the value of the bits.
12940
252b5132
RH
12941 On the 386, immediates, displacements, and data pointers are all in
12942 the same (little-endian) format, so we don't need to care about which
12943 we are handling. */
12944
94f592af 12945void
7016a5d5 12946md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 12947{
94f592af 12948 char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
c6682705 12949 valueT value = *valP;
252b5132 12950
f86103b7 12951#if !defined (TE_Mach)
93382f6d
AM
12952 if (fixP->fx_pcrel)
12953 {
12954 switch (fixP->fx_r_type)
12955 {
5865bb77
ILT
12956 default:
12957 break;
12958
d6ab8113
JB
12959 case BFD_RELOC_64:
12960 fixP->fx_r_type = BFD_RELOC_64_PCREL;
12961 break;
93382f6d 12962 case BFD_RELOC_32:
ae8887b5 12963 case BFD_RELOC_X86_64_32S:
93382f6d
AM
12964 fixP->fx_r_type = BFD_RELOC_32_PCREL;
12965 break;
12966 case BFD_RELOC_16:
12967 fixP->fx_r_type = BFD_RELOC_16_PCREL;
12968 break;
12969 case BFD_RELOC_8:
12970 fixP->fx_r_type = BFD_RELOC_8_PCREL;
12971 break;
12972 }
12973 }
252b5132 12974
a161fe53 12975 if (fixP->fx_addsy != NULL
31312f95 12976 && (fixP->fx_r_type == BFD_RELOC_32_PCREL
d6ab8113 12977 || fixP->fx_r_type == BFD_RELOC_64_PCREL
31312f95 12978 || fixP->fx_r_type == BFD_RELOC_16_PCREL
d258b828 12979 || fixP->fx_r_type == BFD_RELOC_8_PCREL)
31312f95 12980 && !use_rela_relocations)
252b5132 12981 {
31312f95
AM
12982 /* This is a hack. There should be a better way to handle this.
12983 This covers for the fact that bfd_install_relocation will
12984 subtract the current location (for partial_inplace, PC relative
12985 relocations); see more below. */
252b5132 12986#ifndef OBJ_AOUT
718ddfc0 12987 if (IS_ELF
252b5132
RH
12988#ifdef TE_PE
12989 || OUTPUT_FLAVOR == bfd_target_coff_flavour
12990#endif
12991 )
12992 value += fixP->fx_where + fixP->fx_frag->fr_address;
12993#endif
12994#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12995 if (IS_ELF)
252b5132 12996 {
6539b54b 12997 segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
2f66722d 12998
6539b54b 12999 if ((sym_seg == seg
2f66722d 13000 || (symbol_section_p (fixP->fx_addsy)
6539b54b 13001 && sym_seg != absolute_section))
af65af87 13002 && !generic_force_reloc (fixP))
2f66722d
AM
13003 {
13004 /* Yes, we add the values in twice. This is because
6539b54b
AM
13005 bfd_install_relocation subtracts them out again. I think
13006 bfd_install_relocation is broken, but I don't dare change
2f66722d
AM
13007 it. FIXME. */
13008 value += fixP->fx_where + fixP->fx_frag->fr_address;
13009 }
252b5132
RH
13010 }
13011#endif
13012#if defined (OBJ_COFF) && defined (TE_PE)
977cdf5a
NC
13013 /* For some reason, the PE format does not store a
13014 section address offset for a PC relative symbol. */
13015 if (S_GET_SEGMENT (fixP->fx_addsy) != seg
7be1c489 13016 || S_IS_WEAK (fixP->fx_addsy))
252b5132
RH
13017 value += md_pcrel_from (fixP);
13018#endif
13019 }
fbeb56a4 13020#if defined (OBJ_COFF) && defined (TE_PE)
f01c1a09
NC
13021 if (fixP->fx_addsy != NULL
13022 && S_IS_WEAK (fixP->fx_addsy)
13023 /* PR 16858: Do not modify weak function references. */
13024 && ! fixP->fx_pcrel)
fbeb56a4 13025 {
296a8689
NC
13026#if !defined (TE_PEP)
13027 /* For x86 PE weak function symbols are neither PC-relative
13028 nor do they set S_IS_FUNCTION. So the only reliable way
13029 to detect them is to check the flags of their containing
13030 section. */
13031 if (S_GET_SEGMENT (fixP->fx_addsy) != NULL
13032 && S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_CODE)
13033 ;
13034 else
13035#endif
fbeb56a4
DK
13036 value -= S_GET_VALUE (fixP->fx_addsy);
13037 }
13038#endif
252b5132
RH
13039
13040 /* Fix a few things - the dynamic linker expects certain values here,
0234cb7c 13041 and we must not disappoint it. */
252b5132 13042#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 13043 if (IS_ELF && fixP->fx_addsy)
47926f60
KH
13044 switch (fixP->fx_r_type)
13045 {
13046 case BFD_RELOC_386_PLT32:
3e73aa7c 13047 case BFD_RELOC_X86_64_PLT32:
b9519cfe
L
13048 /* Make the jump instruction point to the address of the operand.
13049 At runtime we merely add the offset to the actual PLT entry.
13050 NB: Subtract the offset size only for jump instructions. */
13051 if (fixP->fx_pcrel)
13052 value = -4;
47926f60 13053 break;
31312f95 13054
13ae64f3
JJ
13055 case BFD_RELOC_386_TLS_GD:
13056 case BFD_RELOC_386_TLS_LDM:
13ae64f3 13057 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
13058 case BFD_RELOC_386_TLS_IE:
13059 case BFD_RELOC_386_TLS_GOTIE:
67a4f2b7 13060 case BFD_RELOC_386_TLS_GOTDESC:
bffbf940
JJ
13061 case BFD_RELOC_X86_64_TLSGD:
13062 case BFD_RELOC_X86_64_TLSLD:
13063 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7 13064 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
00f7efb6
JJ
13065 value = 0; /* Fully resolved at runtime. No addend. */
13066 /* Fallthrough */
13067 case BFD_RELOC_386_TLS_LE:
13068 case BFD_RELOC_386_TLS_LDO_32:
13069 case BFD_RELOC_386_TLS_LE_32:
13070 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 13071 case BFD_RELOC_X86_64_DTPOFF64:
00f7efb6 13072 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113 13073 case BFD_RELOC_X86_64_TPOFF64:
00f7efb6
JJ
13074 S_SET_THREAD_LOCAL (fixP->fx_addsy);
13075 break;
13076
67a4f2b7
AO
13077 case BFD_RELOC_386_TLS_DESC_CALL:
13078 case BFD_RELOC_X86_64_TLSDESC_CALL:
13079 value = 0; /* Fully resolved at runtime. No addend. */
13080 S_SET_THREAD_LOCAL (fixP->fx_addsy);
13081 fixP->fx_done = 0;
13082 return;
13083
47926f60
KH
13084 case BFD_RELOC_VTABLE_INHERIT:
13085 case BFD_RELOC_VTABLE_ENTRY:
13086 fixP->fx_done = 0;
94f592af 13087 return;
47926f60
KH
13088
13089 default:
13090 break;
13091 }
13092#endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) */
a442cac5
JB
13093
13094 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
13095 if (!object_64bit)
13096 value = extend_to_32bit_address (value);
13097
c6682705 13098 *valP = value;
f86103b7 13099#endif /* !defined (TE_Mach) */
3e73aa7c 13100
3e73aa7c 13101 /* Are we finished with this relocation now? */
c6682705 13102 if (fixP->fx_addsy == NULL)
b8188555
JB
13103 {
13104 fixP->fx_done = 1;
13105 switch (fixP->fx_r_type)
13106 {
13107 case BFD_RELOC_X86_64_32S:
13108 fixP->fx_signed = 1;
13109 break;
13110
13111 default:
13112 break;
13113 }
13114 }
fbeb56a4
DK
13115#if defined (OBJ_COFF) && defined (TE_PE)
13116 else if (fixP->fx_addsy != NULL && S_IS_WEAK (fixP->fx_addsy))
13117 {
13118 fixP->fx_done = 0;
13119 /* Remember value for tc_gen_reloc. */
13120 fixP->fx_addnumber = value;
13121 /* Clear out the frag for now. */
13122 value = 0;
13123 }
13124#endif
3e73aa7c
JH
13125 else if (use_rela_relocations)
13126 {
46fb6d5a
JB
13127 if (!disallow_64bit_reloc || fixP->fx_r_type == NO_RELOC)
13128 fixP->fx_no_overflow = 1;
062cd5e7
AS
13129 /* Remember value for tc_gen_reloc. */
13130 fixP->fx_addnumber = value;
3e73aa7c
JH
13131 value = 0;
13132 }
f86103b7 13133
94f592af 13134 md_number_to_chars (p, value, fixP->fx_size);
252b5132 13135}
252b5132 13136\f
6d4af3c2 13137const char *
499ac353 13138md_atof (int type, char *litP, int *sizeP)
252b5132 13139{
499ac353
NC
13140 /* This outputs the LITTLENUMs in REVERSE order;
13141 in accord with the bigendian 386. */
5b7c81bd 13142 return ieee_md_atof (type, litP, sizeP, false);
252b5132
RH
13143}
13144\f
2d545b82 13145static char output_invalid_buf[sizeof (unsigned char) * 2 + 6];
252b5132 13146
252b5132 13147static char *
e3bb37b5 13148output_invalid (int c)
252b5132 13149{
3882b010 13150 if (ISPRINT (c))
f9f21a03
L
13151 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
13152 "'%c'", c);
252b5132 13153 else
f9f21a03 13154 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
2d545b82 13155 "(0x%x)", (unsigned char) c);
252b5132
RH
13156 return output_invalid_buf;
13157}
13158
8a6fb3f9
JB
13159/* Verify that @r can be used in the current context. */
13160
5b7c81bd 13161static bool check_register (const reg_entry *r)
8a6fb3f9
JB
13162{
13163 if (allow_pseudo_reg)
5b7c81bd 13164 return true;
8a6fb3f9
JB
13165
13166 if (operand_type_all_zero (&r->reg_type))
5b7c81bd 13167 return false;
8a6fb3f9
JB
13168
13169 if ((r->reg_type.bitfield.dword
13170 || (r->reg_type.bitfield.class == SReg && r->reg_num > 3)
13171 || r->reg_type.bitfield.class == RegCR
22e00a3f 13172 || r->reg_type.bitfield.class == RegDR)
8a6fb3f9 13173 && !cpu_arch_flags.bitfield.cpui386)
5b7c81bd 13174 return false;
8a6fb3f9 13175
22e00a3f
JB
13176 if (r->reg_type.bitfield.class == RegTR
13177 && (flag_code == CODE_64BIT
13178 || !cpu_arch_flags.bitfield.cpui386
13179 || cpu_arch_isa_flags.bitfield.cpui586
13180 || cpu_arch_isa_flags.bitfield.cpui686))
5b7c81bd 13181 return false;
22e00a3f 13182
8a6fb3f9 13183 if (r->reg_type.bitfield.class == RegMMX && !cpu_arch_flags.bitfield.cpummx)
5b7c81bd 13184 return false;
8a6fb3f9
JB
13185
13186 if (!cpu_arch_flags.bitfield.cpuavx512f)
13187 {
13188 if (r->reg_type.bitfield.zmmword
13189 || r->reg_type.bitfield.class == RegMask)
5b7c81bd 13190 return false;
8a6fb3f9
JB
13191
13192 if (!cpu_arch_flags.bitfield.cpuavx)
13193 {
13194 if (r->reg_type.bitfield.ymmword)
5b7c81bd 13195 return false;
8a6fb3f9
JB
13196
13197 if (!cpu_arch_flags.bitfield.cpusse && r->reg_type.bitfield.xmmword)
5b7c81bd 13198 return false;
8a6fb3f9
JB
13199 }
13200 }
13201
260cd341
LC
13202 if (r->reg_type.bitfield.tmmword
13203 && (!cpu_arch_flags.bitfield.cpuamx_tile
13204 || flag_code != CODE_64BIT))
5b7c81bd 13205 return false;
260cd341 13206
8a6fb3f9 13207 if (r->reg_type.bitfield.class == RegBND && !cpu_arch_flags.bitfield.cpumpx)
5b7c81bd 13208 return false;
8a6fb3f9
JB
13209
13210 /* Don't allow fake index register unless allow_index_reg isn't 0. */
13211 if (!allow_index_reg && r->reg_num == RegIZ)
5b7c81bd 13212 return false;
8a6fb3f9
JB
13213
13214 /* Upper 16 vector registers are only available with VREX in 64bit
13215 mode, and require EVEX encoding. */
13216 if (r->reg_flags & RegVRex)
13217 {
13218 if (!cpu_arch_flags.bitfield.cpuavx512f
13219 || flag_code != CODE_64BIT)
5b7c81bd 13220 return false;
8a6fb3f9 13221
da4977e0
JB
13222 if (i.vec_encoding == vex_encoding_default)
13223 i.vec_encoding = vex_encoding_evex;
13224 else if (i.vec_encoding != vex_encoding_evex)
13225 i.vec_encoding = vex_encoding_error;
8a6fb3f9
JB
13226 }
13227
13228 if (((r->reg_flags & (RegRex64 | RegRex)) || r->reg_type.bitfield.qword)
13229 && (!cpu_arch_flags.bitfield.cpulm || r->reg_type.bitfield.class != RegCR)
13230 && flag_code != CODE_64BIT)
5b7c81bd 13231 return false;
8a6fb3f9
JB
13232
13233 if (r->reg_type.bitfield.class == SReg && r->reg_num == RegFlat
13234 && !intel_syntax)
5b7c81bd 13235 return false;
8a6fb3f9 13236
5b7c81bd 13237 return true;
8a6fb3f9
JB
13238}
13239
af6bdddf 13240/* REG_STRING starts *before* REGISTER_PREFIX. */
252b5132
RH
13241
13242static const reg_entry *
4d1bb795 13243parse_real_register (char *reg_string, char **end_op)
252b5132 13244{
af6bdddf
AM
13245 char *s = reg_string;
13246 char *p;
252b5132
RH
13247 char reg_name_given[MAX_REG_NAME_SIZE + 1];
13248 const reg_entry *r;
13249
13250 /* Skip possible REGISTER_PREFIX and possible whitespace. */
13251 if (*s == REGISTER_PREFIX)
13252 ++s;
13253
13254 if (is_space_char (*s))
13255 ++s;
13256
13257 p = reg_name_given;
af6bdddf 13258 while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
252b5132
RH
13259 {
13260 if (p >= reg_name_given + MAX_REG_NAME_SIZE)
af6bdddf
AM
13261 return (const reg_entry *) NULL;
13262 s++;
252b5132
RH
13263 }
13264
4795cd4a 13265 if (is_part_of_name (*s))
6588847e
DN
13266 return (const reg_entry *) NULL;
13267
af6bdddf 13268 *end_op = s;
252b5132 13269
629310ab 13270 r = (const reg_entry *) str_hash_find (reg_hash, reg_name_given);
252b5132 13271
5f47d35b 13272 /* Handle floating point regs, allowing spaces in the (i) part. */
6288d05f 13273 if (r == reg_st0)
5f47d35b 13274 {
0e0eea78
JB
13275 if (!cpu_arch_flags.bitfield.cpu8087
13276 && !cpu_arch_flags.bitfield.cpu287
af32b722
JB
13277 && !cpu_arch_flags.bitfield.cpu387
13278 && !allow_pseudo_reg)
0e0eea78
JB
13279 return (const reg_entry *) NULL;
13280
5f47d35b
AM
13281 if (is_space_char (*s))
13282 ++s;
13283 if (*s == '(')
13284 {
af6bdddf 13285 ++s;
5f47d35b
AM
13286 if (is_space_char (*s))
13287 ++s;
13288 if (*s >= '0' && *s <= '7')
13289 {
db557034 13290 int fpr = *s - '0';
af6bdddf 13291 ++s;
5f47d35b
AM
13292 if (is_space_char (*s))
13293 ++s;
13294 if (*s == ')')
13295 {
13296 *end_op = s + 1;
6288d05f 13297 know (r[fpr].reg_num == fpr);
db557034 13298 return r + fpr;
5f47d35b 13299 }
5f47d35b 13300 }
47926f60 13301 /* We have "%st(" then garbage. */
5f47d35b
AM
13302 return (const reg_entry *) NULL;
13303 }
13304 }
13305
8a6fb3f9 13306 return r && check_register (r) ? r : NULL;
252b5132 13307}
4d1bb795
JB
13308
13309/* REG_STRING starts *before* REGISTER_PREFIX. */
13310
13311static const reg_entry *
13312parse_register (char *reg_string, char **end_op)
13313{
13314 const reg_entry *r;
13315
13316 if (*reg_string == REGISTER_PREFIX || allow_naked_reg)
13317 r = parse_real_register (reg_string, end_op);
13318 else
13319 r = NULL;
13320 if (!r)
13321 {
13322 char *save = input_line_pointer;
13323 char c;
13324 symbolS *symbolP;
13325
13326 input_line_pointer = reg_string;
d02603dc 13327 c = get_symbol_name (&reg_string);
4d1bb795 13328 symbolP = symbol_find (reg_string);
64d23078
JB
13329 while (symbolP && S_GET_SEGMENT (symbolP) != reg_section)
13330 {
13331 const expressionS *e = symbol_get_value_expression(symbolP);
13332
13333 if (e->X_op != O_symbol || e->X_add_number)
13334 break;
13335 symbolP = e->X_add_symbol;
13336 }
4d1bb795
JB
13337 if (symbolP && S_GET_SEGMENT (symbolP) == reg_section)
13338 {
13339 const expressionS *e = symbol_get_value_expression (symbolP);
13340
0ccade1a
JB
13341 if (e->X_op == O_register)
13342 {
13343 know (e->X_add_number >= 0
13344 && (valueT) e->X_add_number < i386_regtab_size);
13345 r = i386_regtab + e->X_add_number;
13346 *end_op = input_line_pointer;
13347 }
13348 if (r && !check_register (r))
8a6fb3f9 13349 {
3b55a1d0
JB
13350 as_bad (_("register '%s%s' cannot be used here"),
13351 register_prefix, r->reg_name);
13352 r = &bad_reg;
8a6fb3f9 13353 }
4d1bb795
JB
13354 }
13355 *input_line_pointer = c;
13356 input_line_pointer = save;
13357 }
13358 return r;
13359}
13360
13361int
13362i386_parse_name (char *name, expressionS *e, char *nextcharP)
13363{
4faaa10f 13364 const reg_entry *r = NULL;
4d1bb795
JB
13365 char *end = input_line_pointer;
13366
13367 *end = *nextcharP;
4faaa10f
JB
13368 if (*name == REGISTER_PREFIX || allow_naked_reg)
13369 r = parse_real_register (name, &input_line_pointer);
4d1bb795
JB
13370 if (r && end <= input_line_pointer)
13371 {
13372 *nextcharP = *input_line_pointer;
13373 *input_line_pointer = 0;
8a6fb3f9
JB
13374 if (r != &bad_reg)
13375 {
13376 e->X_op = O_register;
13377 e->X_add_number = r - i386_regtab;
13378 }
13379 else
13380 e->X_op = O_illegal;
4d1bb795
JB
13381 return 1;
13382 }
13383 input_line_pointer = end;
13384 *end = 0;
ee86248c 13385 return intel_syntax ? i386_intel_parse_name (name, e) : 0;
4d1bb795
JB
13386}
13387
13388void
13389md_operand (expressionS *e)
13390{
ee86248c
JB
13391 char *end;
13392 const reg_entry *r;
4d1bb795 13393
ee86248c
JB
13394 switch (*input_line_pointer)
13395 {
13396 case REGISTER_PREFIX:
13397 r = parse_real_register (input_line_pointer, &end);
4d1bb795
JB
13398 if (r)
13399 {
13400 e->X_op = O_register;
13401 e->X_add_number = r - i386_regtab;
13402 input_line_pointer = end;
13403 }
ee86248c
JB
13404 break;
13405
13406 case '[':
9c2799c2 13407 gas_assert (intel_syntax);
ee86248c
JB
13408 end = input_line_pointer++;
13409 expression (e);
13410 if (*input_line_pointer == ']')
13411 {
13412 ++input_line_pointer;
13413 e->X_op_symbol = make_expr_symbol (e);
13414 e->X_add_symbol = NULL;
13415 e->X_add_number = 0;
13416 e->X_op = O_index;
13417 }
13418 else
13419 {
13420 e->X_op = O_absent;
13421 input_line_pointer = end;
13422 }
13423 break;
4d1bb795
JB
13424 }
13425}
13426
252b5132 13427\f
4cc782b5 13428#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
b6f8c7c4 13429const char *md_shortopts = "kVQ:sqnO::";
252b5132 13430#else
b6f8c7c4 13431const char *md_shortopts = "qnO::";
252b5132 13432#endif
6e0b89ee 13433
3e73aa7c 13434#define OPTION_32 (OPTION_MD_BASE + 0)
b3b91714
AM
13435#define OPTION_64 (OPTION_MD_BASE + 1)
13436#define OPTION_DIVIDE (OPTION_MD_BASE + 2)
9103f4f4
L
13437#define OPTION_MARCH (OPTION_MD_BASE + 3)
13438#define OPTION_MTUNE (OPTION_MD_BASE + 4)
1efbbeb4
L
13439#define OPTION_MMNEMONIC (OPTION_MD_BASE + 5)
13440#define OPTION_MSYNTAX (OPTION_MD_BASE + 6)
13441#define OPTION_MINDEX_REG (OPTION_MD_BASE + 7)
13442#define OPTION_MNAKED_REG (OPTION_MD_BASE + 8)
bd5dea88 13443#define OPTION_MRELAX_RELOCATIONS (OPTION_MD_BASE + 9)
c0f3af97 13444#define OPTION_MSSE2AVX (OPTION_MD_BASE + 10)
daf50ae7 13445#define OPTION_MSSE_CHECK (OPTION_MD_BASE + 11)
7bab8ab5
JB
13446#define OPTION_MOPERAND_CHECK (OPTION_MD_BASE + 12)
13447#define OPTION_MAVXSCALAR (OPTION_MD_BASE + 13)
13448#define OPTION_X32 (OPTION_MD_BASE + 14)
7e8b059b 13449#define OPTION_MADD_BND_PREFIX (OPTION_MD_BASE + 15)
43234a1e
L
13450#define OPTION_MEVEXLIG (OPTION_MD_BASE + 16)
13451#define OPTION_MEVEXWIG (OPTION_MD_BASE + 17)
167ad85b 13452#define OPTION_MBIG_OBJ (OPTION_MD_BASE + 18)
d1982f93 13453#define OPTION_MOMIT_LOCK_PREFIX (OPTION_MD_BASE + 19)
d3d3c6db 13454#define OPTION_MEVEXRCIG (OPTION_MD_BASE + 20)
8dcea932 13455#define OPTION_MSHARED (OPTION_MD_BASE + 21)
5db04b09
L
13456#define OPTION_MAMD64 (OPTION_MD_BASE + 22)
13457#define OPTION_MINTEL64 (OPTION_MD_BASE + 23)
e4e00185 13458#define OPTION_MFENCE_AS_LOCK_ADD (OPTION_MD_BASE + 24)
b4a3a7b4 13459#define OPTION_X86_USED_NOTE (OPTION_MD_BASE + 25)
03751133 13460#define OPTION_MVEXWIG (OPTION_MD_BASE + 26)
e379e5f3
L
13461#define OPTION_MALIGN_BRANCH_BOUNDARY (OPTION_MD_BASE + 27)
13462#define OPTION_MALIGN_BRANCH_PREFIX_SIZE (OPTION_MD_BASE + 28)
13463#define OPTION_MALIGN_BRANCH (OPTION_MD_BASE + 29)
76cf450b 13464#define OPTION_MBRANCHES_WITH_32B_BOUNDARIES (OPTION_MD_BASE + 30)
ae531041
L
13465#define OPTION_MLFENCE_AFTER_LOAD (OPTION_MD_BASE + 31)
13466#define OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH (OPTION_MD_BASE + 32)
13467#define OPTION_MLFENCE_BEFORE_RET (OPTION_MD_BASE + 33)
c8480b58 13468#define OPTION_MUSE_UNALIGNED_VECTOR_MOVE (OPTION_MD_BASE + 34)
b3b91714 13469
99ad8390
NC
13470struct option md_longopts[] =
13471{
3e73aa7c 13472 {"32", no_argument, NULL, OPTION_32},
321098a5 13473#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 13474 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c 13475 {"64", no_argument, NULL, OPTION_64},
351f65ca
L
13476#endif
13477#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 13478 {"x32", no_argument, NULL, OPTION_X32},
8dcea932 13479 {"mshared", no_argument, NULL, OPTION_MSHARED},
b4a3a7b4 13480 {"mx86-used-note", required_argument, NULL, OPTION_X86_USED_NOTE},
6e0b89ee 13481#endif
b3b91714 13482 {"divide", no_argument, NULL, OPTION_DIVIDE},
9103f4f4
L
13483 {"march", required_argument, NULL, OPTION_MARCH},
13484 {"mtune", required_argument, NULL, OPTION_MTUNE},
1efbbeb4
L
13485 {"mmnemonic", required_argument, NULL, OPTION_MMNEMONIC},
13486 {"msyntax", required_argument, NULL, OPTION_MSYNTAX},
13487 {"mindex-reg", no_argument, NULL, OPTION_MINDEX_REG},
13488 {"mnaked-reg", no_argument, NULL, OPTION_MNAKED_REG},
c0f3af97 13489 {"msse2avx", no_argument, NULL, OPTION_MSSE2AVX},
c8480b58 13490 {"muse-unaligned-vector-move", no_argument, NULL, OPTION_MUSE_UNALIGNED_VECTOR_MOVE},
daf50ae7 13491 {"msse-check", required_argument, NULL, OPTION_MSSE_CHECK},
7bab8ab5 13492 {"moperand-check", required_argument, NULL, OPTION_MOPERAND_CHECK},
539f890d 13493 {"mavxscalar", required_argument, NULL, OPTION_MAVXSCALAR},
03751133 13494 {"mvexwig", required_argument, NULL, OPTION_MVEXWIG},
7e8b059b 13495 {"madd-bnd-prefix", no_argument, NULL, OPTION_MADD_BND_PREFIX},
43234a1e
L
13496 {"mevexlig", required_argument, NULL, OPTION_MEVEXLIG},
13497 {"mevexwig", required_argument, NULL, OPTION_MEVEXWIG},
167ad85b
TG
13498# if defined (TE_PE) || defined (TE_PEP)
13499 {"mbig-obj", no_argument, NULL, OPTION_MBIG_OBJ},
13500#endif
d1982f93 13501 {"momit-lock-prefix", required_argument, NULL, OPTION_MOMIT_LOCK_PREFIX},
e4e00185 13502 {"mfence-as-lock-add", required_argument, NULL, OPTION_MFENCE_AS_LOCK_ADD},
0cb4071e 13503 {"mrelax-relocations", required_argument, NULL, OPTION_MRELAX_RELOCATIONS},
d3d3c6db 13504 {"mevexrcig", required_argument, NULL, OPTION_MEVEXRCIG},
e379e5f3
L
13505 {"malign-branch-boundary", required_argument, NULL, OPTION_MALIGN_BRANCH_BOUNDARY},
13506 {"malign-branch-prefix-size", required_argument, NULL, OPTION_MALIGN_BRANCH_PREFIX_SIZE},
13507 {"malign-branch", required_argument, NULL, OPTION_MALIGN_BRANCH},
76cf450b 13508 {"mbranches-within-32B-boundaries", no_argument, NULL, OPTION_MBRANCHES_WITH_32B_BOUNDARIES},
ae531041
L
13509 {"mlfence-after-load", required_argument, NULL, OPTION_MLFENCE_AFTER_LOAD},
13510 {"mlfence-before-indirect-branch", required_argument, NULL,
13511 OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH},
13512 {"mlfence-before-ret", required_argument, NULL, OPTION_MLFENCE_BEFORE_RET},
5db04b09
L
13513 {"mamd64", no_argument, NULL, OPTION_MAMD64},
13514 {"mintel64", no_argument, NULL, OPTION_MINTEL64},
252b5132
RH
13515 {NULL, no_argument, NULL, 0}
13516};
13517size_t md_longopts_size = sizeof (md_longopts);
13518
13519int
17b9d67d 13520md_parse_option (int c, const char *arg)
252b5132 13521{
91d6fa6a 13522 unsigned int j;
e379e5f3 13523 char *arch, *next, *saved, *type;
9103f4f4 13524
252b5132
RH
13525 switch (c)
13526 {
12b55ccc
L
13527 case 'n':
13528 optimize_align_code = 0;
13529 break;
13530
a38cf1db
AM
13531 case 'q':
13532 quiet_warnings = 1;
252b5132
RH
13533 break;
13534
13535#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
a38cf1db
AM
13536 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
13537 should be emitted or not. FIXME: Not implemented. */
13538 case 'Q':
d4693039
JB
13539 if ((arg[0] != 'y' && arg[0] != 'n') || arg[1])
13540 return 0;
252b5132
RH
13541 break;
13542
13543 /* -V: SVR4 argument to print version ID. */
13544 case 'V':
13545 print_version_id ();
13546 break;
13547
a38cf1db
AM
13548 /* -k: Ignore for FreeBSD compatibility. */
13549 case 'k':
252b5132 13550 break;
4cc782b5
ILT
13551
13552 case 's':
13553 /* -s: On i386 Solaris, this tells the native assembler to use
29b0f896 13554 .stab instead of .stab.excl. We always use .stab anyhow. */
4cc782b5 13555 break;
8dcea932
L
13556
13557 case OPTION_MSHARED:
13558 shared = 1;
13559 break;
b4a3a7b4
L
13560
13561 case OPTION_X86_USED_NOTE:
13562 if (strcasecmp (arg, "yes") == 0)
13563 x86_used_note = 1;
13564 else if (strcasecmp (arg, "no") == 0)
13565 x86_used_note = 0;
13566 else
13567 as_fatal (_("invalid -mx86-used-note= option: `%s'"), arg);
13568 break;
13569
13570
99ad8390 13571#endif
321098a5 13572#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 13573 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c
JH
13574 case OPTION_64:
13575 {
13576 const char **list, **l;
13577
3e73aa7c
JH
13578 list = bfd_target_list ();
13579 for (l = list; *l != NULL; l++)
08dedd66 13580 if (startswith (*l, "elf64-x86-64")
99ad8390
NC
13581 || strcmp (*l, "coff-x86-64") == 0
13582 || strcmp (*l, "pe-x86-64") == 0
d382c579
TG
13583 || strcmp (*l, "pei-x86-64") == 0
13584 || strcmp (*l, "mach-o-x86-64") == 0)
6e0b89ee
AM
13585 {
13586 default_arch = "x86_64";
13587 break;
13588 }
3e73aa7c 13589 if (*l == NULL)
2b5d6a91 13590 as_fatal (_("no compiled in support for x86_64"));
3e73aa7c
JH
13591 free (list);
13592 }
13593 break;
13594#endif
252b5132 13595
351f65ca 13596#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 13597 case OPTION_X32:
351f65ca
L
13598 if (IS_ELF)
13599 {
13600 const char **list, **l;
13601
13602 list = bfd_target_list ();
13603 for (l = list; *l != NULL; l++)
08dedd66 13604 if (startswith (*l, "elf32-x86-64"))
351f65ca
L
13605 {
13606 default_arch = "x86_64:32";
13607 break;
13608 }
13609 if (*l == NULL)
2b5d6a91 13610 as_fatal (_("no compiled in support for 32bit x86_64"));
351f65ca
L
13611 free (list);
13612 }
13613 else
13614 as_fatal (_("32bit x86_64 is only supported for ELF"));
13615 break;
13616#endif
13617
6e0b89ee
AM
13618 case OPTION_32:
13619 default_arch = "i386";
13620 break;
13621
b3b91714
AM
13622 case OPTION_DIVIDE:
13623#ifdef SVR4_COMMENT_CHARS
13624 {
13625 char *n, *t;
13626 const char *s;
13627
add39d23 13628 n = XNEWVEC (char, strlen (i386_comment_chars) + 1);
b3b91714
AM
13629 t = n;
13630 for (s = i386_comment_chars; *s != '\0'; s++)
13631 if (*s != '/')
13632 *t++ = *s;
13633 *t = '\0';
13634 i386_comment_chars = n;
13635 }
13636#endif
13637 break;
13638
9103f4f4 13639 case OPTION_MARCH:
293f5f65
L
13640 saved = xstrdup (arg);
13641 arch = saved;
13642 /* Allow -march=+nosse. */
13643 if (*arch == '+')
13644 arch++;
6305a203 13645 do
9103f4f4 13646 {
6305a203 13647 if (*arch == '.')
2b5d6a91 13648 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
13649 next = strchr (arch, '+');
13650 if (next)
13651 *next++ = '\0';
91d6fa6a 13652 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 13653 {
ae89daec
JB
13654 if (arch == saved && cpu_arch[j].type != PROCESSOR_NONE
13655 && strcmp (arch, cpu_arch[j].name) == 0)
ccc9c027 13656 {
6305a203 13657 /* Processor. */
ae89daec 13658 if (! cpu_arch[j].enable.bitfield.cpui386)
1ded5609
JB
13659 continue;
13660
91d6fa6a 13661 cpu_arch_name = cpu_arch[j].name;
d92c7521 13662 free (cpu_sub_arch_name);
6305a203 13663 cpu_sub_arch_name = NULL;
ae89daec 13664 cpu_arch_flags = cpu_arch[j].enable;
91d6fa6a 13665 cpu_arch_isa = cpu_arch[j].type;
ae89daec 13666 cpu_arch_isa_flags = cpu_arch[j].enable;
6305a203
L
13667 if (!cpu_arch_tune_set)
13668 {
13669 cpu_arch_tune = cpu_arch_isa;
13670 cpu_arch_tune_flags = cpu_arch_isa_flags;
13671 }
13672 break;
13673 }
ae89daec
JB
13674 else if (cpu_arch[j].type == PROCESSOR_NONE
13675 && strcmp (arch, cpu_arch[j].name) == 0
13676 && !cpu_flags_all_zero (&cpu_arch[j].enable))
6305a203 13677 {
33eaf5de 13678 /* ISA extension. */
6305a203 13679 i386_cpu_flags flags;
309d3373 13680
293f5f65 13681 flags = cpu_flags_or (cpu_arch_flags,
ae89daec 13682 cpu_arch[j].enable);
81486035 13683
5b64d091 13684 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
6305a203 13685 {
ae89daec 13686 extend_cpu_sub_arch_name (arch);
6305a203 13687 cpu_arch_flags = flags;
a586129e 13688 cpu_arch_isa_flags = flags;
6305a203 13689 }
0089dace
L
13690 else
13691 cpu_arch_isa_flags
13692 = cpu_flags_or (cpu_arch_isa_flags,
ae89daec 13693 cpu_arch[j].enable);
6305a203 13694 break;
ccc9c027 13695 }
9103f4f4 13696 }
6305a203 13697
ae89daec 13698 if (j >= ARRAY_SIZE (cpu_arch) && startswith (arch, "no"))
293f5f65 13699 {
33eaf5de 13700 /* Disable an ISA extension. */
ae89daec
JB
13701 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13702 if (cpu_arch[j].type == PROCESSOR_NONE
13703 && strcmp (arch + 2, cpu_arch[j].name) == 0)
293f5f65
L
13704 {
13705 i386_cpu_flags flags;
13706
13707 flags = cpu_flags_and_not (cpu_arch_flags,
ae89daec 13708 cpu_arch[j].disable);
293f5f65
L
13709 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
13710 {
8180707f 13711 extend_cpu_sub_arch_name (arch);
293f5f65
L
13712 cpu_arch_flags = flags;
13713 cpu_arch_isa_flags = flags;
13714 }
13715 break;
13716 }
293f5f65
L
13717 }
13718
91d6fa6a 13719 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 13720 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
13721
13722 arch = next;
9103f4f4 13723 }
293f5f65
L
13724 while (next != NULL);
13725 free (saved);
9103f4f4
L
13726 break;
13727
13728 case OPTION_MTUNE:
13729 if (*arg == '.')
2b5d6a91 13730 as_fatal (_("invalid -mtune= option: `%s'"), arg);
91d6fa6a 13731 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 13732 {
ae89daec
JB
13733 if (cpu_arch[j].type != PROCESSOR_NONE
13734 && strcmp (arg, cpu_arch[j].name) == 0)
9103f4f4 13735 {
ccc9c027 13736 cpu_arch_tune_set = 1;
91d6fa6a 13737 cpu_arch_tune = cpu_arch [j].type;
ae89daec 13738 cpu_arch_tune_flags = cpu_arch[j].enable;
9103f4f4
L
13739 break;
13740 }
13741 }
91d6fa6a 13742 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 13743 as_fatal (_("invalid -mtune= option: `%s'"), arg);
9103f4f4
L
13744 break;
13745
1efbbeb4
L
13746 case OPTION_MMNEMONIC:
13747 if (strcasecmp (arg, "att") == 0)
13748 intel_mnemonic = 0;
13749 else if (strcasecmp (arg, "intel") == 0)
13750 intel_mnemonic = 1;
13751 else
2b5d6a91 13752 as_fatal (_("invalid -mmnemonic= option: `%s'"), arg);
1efbbeb4
L
13753 break;
13754
13755 case OPTION_MSYNTAX:
13756 if (strcasecmp (arg, "att") == 0)
13757 intel_syntax = 0;
13758 else if (strcasecmp (arg, "intel") == 0)
13759 intel_syntax = 1;
13760 else
2b5d6a91 13761 as_fatal (_("invalid -msyntax= option: `%s'"), arg);
1efbbeb4
L
13762 break;
13763
13764 case OPTION_MINDEX_REG:
13765 allow_index_reg = 1;
13766 break;
13767
13768 case OPTION_MNAKED_REG:
13769 allow_naked_reg = 1;
13770 break;
13771
c0f3af97
L
13772 case OPTION_MSSE2AVX:
13773 sse2avx = 1;
13774 break;
13775
c8480b58
L
13776 case OPTION_MUSE_UNALIGNED_VECTOR_MOVE:
13777 use_unaligned_vector_move = 1;
13778 break;
13779
daf50ae7
L
13780 case OPTION_MSSE_CHECK:
13781 if (strcasecmp (arg, "error") == 0)
7bab8ab5 13782 sse_check = check_error;
daf50ae7 13783 else if (strcasecmp (arg, "warning") == 0)
7bab8ab5 13784 sse_check = check_warning;
daf50ae7 13785 else if (strcasecmp (arg, "none") == 0)
7bab8ab5 13786 sse_check = check_none;
daf50ae7 13787 else
2b5d6a91 13788 as_fatal (_("invalid -msse-check= option: `%s'"), arg);
daf50ae7
L
13789 break;
13790
7bab8ab5
JB
13791 case OPTION_MOPERAND_CHECK:
13792 if (strcasecmp (arg, "error") == 0)
13793 operand_check = check_error;
13794 else if (strcasecmp (arg, "warning") == 0)
13795 operand_check = check_warning;
13796 else if (strcasecmp (arg, "none") == 0)
13797 operand_check = check_none;
13798 else
13799 as_fatal (_("invalid -moperand-check= option: `%s'"), arg);
13800 break;
13801
539f890d
L
13802 case OPTION_MAVXSCALAR:
13803 if (strcasecmp (arg, "128") == 0)
13804 avxscalar = vex128;
13805 else if (strcasecmp (arg, "256") == 0)
13806 avxscalar = vex256;
13807 else
2b5d6a91 13808 as_fatal (_("invalid -mavxscalar= option: `%s'"), arg);
539f890d
L
13809 break;
13810
03751133
L
13811 case OPTION_MVEXWIG:
13812 if (strcmp (arg, "0") == 0)
40c9c8de 13813 vexwig = vexw0;
03751133 13814 else if (strcmp (arg, "1") == 0)
40c9c8de 13815 vexwig = vexw1;
03751133
L
13816 else
13817 as_fatal (_("invalid -mvexwig= option: `%s'"), arg);
13818 break;
13819
7e8b059b
L
13820 case OPTION_MADD_BND_PREFIX:
13821 add_bnd_prefix = 1;
13822 break;
13823
43234a1e
L
13824 case OPTION_MEVEXLIG:
13825 if (strcmp (arg, "128") == 0)
13826 evexlig = evexl128;
13827 else if (strcmp (arg, "256") == 0)
13828 evexlig = evexl256;
13829 else if (strcmp (arg, "512") == 0)
13830 evexlig = evexl512;
13831 else
13832 as_fatal (_("invalid -mevexlig= option: `%s'"), arg);
13833 break;
13834
d3d3c6db
IT
13835 case OPTION_MEVEXRCIG:
13836 if (strcmp (arg, "rne") == 0)
13837 evexrcig = rne;
13838 else if (strcmp (arg, "rd") == 0)
13839 evexrcig = rd;
13840 else if (strcmp (arg, "ru") == 0)
13841 evexrcig = ru;
13842 else if (strcmp (arg, "rz") == 0)
13843 evexrcig = rz;
13844 else
13845 as_fatal (_("invalid -mevexrcig= option: `%s'"), arg);
13846 break;
13847
43234a1e
L
13848 case OPTION_MEVEXWIG:
13849 if (strcmp (arg, "0") == 0)
13850 evexwig = evexw0;
13851 else if (strcmp (arg, "1") == 0)
13852 evexwig = evexw1;
13853 else
13854 as_fatal (_("invalid -mevexwig= option: `%s'"), arg);
13855 break;
13856
167ad85b
TG
13857# if defined (TE_PE) || defined (TE_PEP)
13858 case OPTION_MBIG_OBJ:
13859 use_big_obj = 1;
13860 break;
13861#endif
13862
d1982f93 13863 case OPTION_MOMIT_LOCK_PREFIX:
d022bddd
IT
13864 if (strcasecmp (arg, "yes") == 0)
13865 omit_lock_prefix = 1;
13866 else if (strcasecmp (arg, "no") == 0)
13867 omit_lock_prefix = 0;
13868 else
13869 as_fatal (_("invalid -momit-lock-prefix= option: `%s'"), arg);
13870 break;
13871
e4e00185
AS
13872 case OPTION_MFENCE_AS_LOCK_ADD:
13873 if (strcasecmp (arg, "yes") == 0)
13874 avoid_fence = 1;
13875 else if (strcasecmp (arg, "no") == 0)
13876 avoid_fence = 0;
13877 else
13878 as_fatal (_("invalid -mfence-as-lock-add= option: `%s'"), arg);
13879 break;
13880
ae531041
L
13881 case OPTION_MLFENCE_AFTER_LOAD:
13882 if (strcasecmp (arg, "yes") == 0)
13883 lfence_after_load = 1;
13884 else if (strcasecmp (arg, "no") == 0)
13885 lfence_after_load = 0;
13886 else
13887 as_fatal (_("invalid -mlfence-after-load= option: `%s'"), arg);
13888 break;
13889
13890 case OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH:
13891 if (strcasecmp (arg, "all") == 0)
a09f656b 13892 {
13893 lfence_before_indirect_branch = lfence_branch_all;
13894 if (lfence_before_ret == lfence_before_ret_none)
13895 lfence_before_ret = lfence_before_ret_shl;
13896 }
ae531041
L
13897 else if (strcasecmp (arg, "memory") == 0)
13898 lfence_before_indirect_branch = lfence_branch_memory;
13899 else if (strcasecmp (arg, "register") == 0)
13900 lfence_before_indirect_branch = lfence_branch_register;
13901 else if (strcasecmp (arg, "none") == 0)
13902 lfence_before_indirect_branch = lfence_branch_none;
13903 else
13904 as_fatal (_("invalid -mlfence-before-indirect-branch= option: `%s'"),
13905 arg);
13906 break;
13907
13908 case OPTION_MLFENCE_BEFORE_RET:
13909 if (strcasecmp (arg, "or") == 0)
13910 lfence_before_ret = lfence_before_ret_or;
13911 else if (strcasecmp (arg, "not") == 0)
13912 lfence_before_ret = lfence_before_ret_not;
a09f656b 13913 else if (strcasecmp (arg, "shl") == 0 || strcasecmp (arg, "yes") == 0)
13914 lfence_before_ret = lfence_before_ret_shl;
ae531041
L
13915 else if (strcasecmp (arg, "none") == 0)
13916 lfence_before_ret = lfence_before_ret_none;
13917 else
13918 as_fatal (_("invalid -mlfence-before-ret= option: `%s'"),
13919 arg);
13920 break;
13921
0cb4071e
L
13922 case OPTION_MRELAX_RELOCATIONS:
13923 if (strcasecmp (arg, "yes") == 0)
13924 generate_relax_relocations = 1;
13925 else if (strcasecmp (arg, "no") == 0)
13926 generate_relax_relocations = 0;
13927 else
13928 as_fatal (_("invalid -mrelax-relocations= option: `%s'"), arg);
13929 break;
13930
e379e5f3
L
13931 case OPTION_MALIGN_BRANCH_BOUNDARY:
13932 {
13933 char *end;
13934 long int align = strtoul (arg, &end, 0);
13935 if (*end == '\0')
13936 {
13937 if (align == 0)
13938 {
13939 align_branch_power = 0;
13940 break;
13941 }
13942 else if (align >= 16)
13943 {
13944 int align_power;
13945 for (align_power = 0;
13946 (align & 1) == 0;
13947 align >>= 1, align_power++)
13948 continue;
13949 /* Limit alignment power to 31. */
13950 if (align == 1 && align_power < 32)
13951 {
13952 align_branch_power = align_power;
13953 break;
13954 }
13955 }
13956 }
13957 as_fatal (_("invalid -malign-branch-boundary= value: %s"), arg);
13958 }
13959 break;
13960
13961 case OPTION_MALIGN_BRANCH_PREFIX_SIZE:
13962 {
13963 char *end;
13964 int align = strtoul (arg, &end, 0);
13965 /* Some processors only support 5 prefixes. */
13966 if (*end == '\0' && align >= 0 && align < 6)
13967 {
13968 align_branch_prefix_size = align;
13969 break;
13970 }
13971 as_fatal (_("invalid -malign-branch-prefix-size= value: %s"),
13972 arg);
13973 }
13974 break;
13975
13976 case OPTION_MALIGN_BRANCH:
13977 align_branch = 0;
13978 saved = xstrdup (arg);
13979 type = saved;
13980 do
13981 {
13982 next = strchr (type, '+');
13983 if (next)
13984 *next++ = '\0';
13985 if (strcasecmp (type, "jcc") == 0)
13986 align_branch |= align_branch_jcc_bit;
13987 else if (strcasecmp (type, "fused") == 0)
13988 align_branch |= align_branch_fused_bit;
13989 else if (strcasecmp (type, "jmp") == 0)
13990 align_branch |= align_branch_jmp_bit;
13991 else if (strcasecmp (type, "call") == 0)
13992 align_branch |= align_branch_call_bit;
13993 else if (strcasecmp (type, "ret") == 0)
13994 align_branch |= align_branch_ret_bit;
13995 else if (strcasecmp (type, "indirect") == 0)
13996 align_branch |= align_branch_indirect_bit;
13997 else
13998 as_fatal (_("invalid -malign-branch= option: `%s'"), arg);
13999 type = next;
14000 }
14001 while (next != NULL);
14002 free (saved);
14003 break;
14004
76cf450b
L
14005 case OPTION_MBRANCHES_WITH_32B_BOUNDARIES:
14006 align_branch_power = 5;
14007 align_branch_prefix_size = 5;
14008 align_branch = (align_branch_jcc_bit
14009 | align_branch_fused_bit
14010 | align_branch_jmp_bit);
14011 break;
14012
5db04b09 14013 case OPTION_MAMD64:
4b5aaf5f 14014 isa64 = amd64;
5db04b09
L
14015 break;
14016
14017 case OPTION_MINTEL64:
4b5aaf5f 14018 isa64 = intel64;
5db04b09
L
14019 break;
14020
b6f8c7c4
L
14021 case 'O':
14022 if (arg == NULL)
14023 {
14024 optimize = 1;
14025 /* Turn off -Os. */
14026 optimize_for_space = 0;
14027 }
14028 else if (*arg == 's')
14029 {
14030 optimize_for_space = 1;
14031 /* Turn on all encoding optimizations. */
41fd2579 14032 optimize = INT_MAX;
b6f8c7c4
L
14033 }
14034 else
14035 {
14036 optimize = atoi (arg);
14037 /* Turn off -Os. */
14038 optimize_for_space = 0;
14039 }
14040 break;
14041
252b5132
RH
14042 default:
14043 return 0;
14044 }
14045 return 1;
14046}
14047
8a2c8fef
L
14048#define MESSAGE_TEMPLATE \
14049" "
14050
293f5f65
L
14051static char *
14052output_message (FILE *stream, char *p, char *message, char *start,
14053 int *left_p, const char *name, int len)
14054{
14055 int size = sizeof (MESSAGE_TEMPLATE);
14056 int left = *left_p;
14057
14058 /* Reserve 2 spaces for ", " or ",\0" */
14059 left -= len + 2;
14060
14061 /* Check if there is any room. */
14062 if (left >= 0)
14063 {
14064 if (p != start)
14065 {
14066 *p++ = ',';
14067 *p++ = ' ';
14068 }
14069 p = mempcpy (p, name, len);
14070 }
14071 else
14072 {
14073 /* Output the current message now and start a new one. */
14074 *p++ = ',';
14075 *p = '\0';
14076 fprintf (stream, "%s\n", message);
14077 p = start;
14078 left = size - (start - message) - len - 2;
14079
14080 gas_assert (left >= 0);
14081
14082 p = mempcpy (p, name, len);
14083 }
14084
14085 *left_p = left;
14086 return p;
14087}
14088
8a2c8fef 14089static void
1ded5609 14090show_arch (FILE *stream, int ext, int check)
8a2c8fef
L
14091{
14092 static char message[] = MESSAGE_TEMPLATE;
14093 char *start = message + 27;
14094 char *p;
14095 int size = sizeof (MESSAGE_TEMPLATE);
14096 int left;
14097 const char *name;
14098 int len;
14099 unsigned int j;
14100
14101 p = start;
14102 left = size - (start - message);
3ce2ebcf
JB
14103
14104 if (!ext && check)
14105 {
14106 p = output_message (stream, p, message, start, &left,
14107 STRING_COMMA_LEN ("default"));
f68697e8
JB
14108 p = output_message (stream, p, message, start, &left,
14109 STRING_COMMA_LEN ("push"));
14110 p = output_message (stream, p, message, start, &left,
14111 STRING_COMMA_LEN ("pop"));
3ce2ebcf
JB
14112 }
14113
8a2c8fef
L
14114 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
14115 {
14116 /* Should it be skipped? */
14117 if (cpu_arch [j].skip)
14118 continue;
14119
14120 name = cpu_arch [j].name;
14121 len = cpu_arch [j].len;
ae89daec 14122 if (cpu_arch[j].type == PROCESSOR_NONE)
8a2c8fef
L
14123 {
14124 /* It is an extension. Skip if we aren't asked to show it. */
ae89daec 14125 if (!ext || cpu_flags_all_zero (&cpu_arch[j].enable))
8a2c8fef
L
14126 continue;
14127 }
14128 else if (ext)
14129 {
14130 /* It is an processor. Skip if we show only extension. */
14131 continue;
14132 }
ae89daec 14133 else if (check && ! cpu_arch[j].enable.bitfield.cpui386)
1ded5609
JB
14134 {
14135 /* It is an impossible processor - skip. */
14136 continue;
14137 }
8a2c8fef 14138
293f5f65 14139 p = output_message (stream, p, message, start, &left, name, len);
8a2c8fef
L
14140 }
14141
293f5f65
L
14142 /* Display disabled extensions. */
14143 if (ext)
ae89daec 14144 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
293f5f65 14145 {
ae89daec
JB
14146 char *str;
14147
14148 if (cpu_arch[j].type != PROCESSOR_NONE
14149 || !cpu_flags_all_zero (&cpu_arch[j].enable))
14150 continue;
14151 str = xasprintf ("no%s", cpu_arch[j].name);
14152 p = output_message (stream, p, message, start, &left, str,
14153 strlen (str));
14154 free (str);
293f5f65
L
14155 }
14156
8a2c8fef
L
14157 *p = '\0';
14158 fprintf (stream, "%s\n", message);
14159}
14160
252b5132 14161void
8a2c8fef 14162md_show_usage (FILE *stream)
252b5132 14163{
4cc782b5
ILT
14164#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14165 fprintf (stream, _("\
d4693039 14166 -Qy, -Qn ignored\n\
a38cf1db 14167 -V print assembler version number\n\
b3b91714
AM
14168 -k ignored\n"));
14169#endif
14170 fprintf (stream, _("\
7ebd68d1
NC
14171 -n do not optimize code alignment\n\
14172 -O{012s} attempt some code optimizations\n\
b3b91714
AM
14173 -q quieten some warnings\n"));
14174#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14175 fprintf (stream, _("\
a38cf1db 14176 -s ignored\n"));
b3b91714 14177#endif
b00af7c8
JB
14178#ifdef BFD64
14179# if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14180 fprintf (stream, _("\
14181 --32/--64/--x32 generate 32bit/64bit/x32 object\n"));
14182# elif defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O)
751d281c 14183 fprintf (stream, _("\
b00af7c8
JB
14184 --32/--64 generate 32bit/64bit object\n"));
14185# endif
751d281c 14186#endif
b3b91714
AM
14187#ifdef SVR4_COMMENT_CHARS
14188 fprintf (stream, _("\
14189 --divide do not treat `/' as a comment character\n"));
a38cf1db
AM
14190#else
14191 fprintf (stream, _("\
b3b91714 14192 --divide ignored\n"));
4cc782b5 14193#endif
9103f4f4 14194 fprintf (stream, _("\
6305a203 14195 -march=CPU[,+EXTENSION...]\n\
8a2c8fef 14196 generate code for CPU and EXTENSION, CPU is one of:\n"));
1ded5609 14197 show_arch (stream, 0, 1);
8a2c8fef 14198 fprintf (stream, _("\
ae89daec 14199 EXTENSION is combination of (possibly \"no\"-prefixed):\n"));
1ded5609 14200 show_arch (stream, 1, 0);
6305a203 14201 fprintf (stream, _("\
8a2c8fef 14202 -mtune=CPU optimize for CPU, CPU is one of:\n"));
1ded5609 14203 show_arch (stream, 0, 0);
ba104c83 14204 fprintf (stream, _("\
c0f3af97
L
14205 -msse2avx encode SSE instructions with VEX prefix\n"));
14206 fprintf (stream, _("\
c8480b58
L
14207 -muse-unaligned-vector-move\n\
14208 encode aligned vector move as unaligned vector move\n"));
14209 fprintf (stream, _("\
7c5c05ef 14210 -msse-check=[none|error|warning] (default: warning)\n\
daf50ae7
L
14211 check SSE instructions\n"));
14212 fprintf (stream, _("\
7c5c05ef 14213 -moperand-check=[none|error|warning] (default: warning)\n\
7bab8ab5
JB
14214 check operand combinations for validity\n"));
14215 fprintf (stream, _("\
7c5c05ef
L
14216 -mavxscalar=[128|256] (default: 128)\n\
14217 encode scalar AVX instructions with specific vector\n\
539f890d
L
14218 length\n"));
14219 fprintf (stream, _("\
03751133
L
14220 -mvexwig=[0|1] (default: 0)\n\
14221 encode VEX instructions with specific VEX.W value\n\
14222 for VEX.W bit ignored instructions\n"));
14223 fprintf (stream, _("\
7c5c05ef
L
14224 -mevexlig=[128|256|512] (default: 128)\n\
14225 encode scalar EVEX instructions with specific vector\n\
43234a1e
L
14226 length\n"));
14227 fprintf (stream, _("\
7c5c05ef
L
14228 -mevexwig=[0|1] (default: 0)\n\
14229 encode EVEX instructions with specific EVEX.W value\n\
43234a1e
L
14230 for EVEX.W bit ignored instructions\n"));
14231 fprintf (stream, _("\
7c5c05ef 14232 -mevexrcig=[rne|rd|ru|rz] (default: rne)\n\
d3d3c6db
IT
14233 encode EVEX instructions with specific EVEX.RC value\n\
14234 for SAE-only ignored instructions\n"));
14235 fprintf (stream, _("\
7c5c05ef
L
14236 -mmnemonic=[att|intel] "));
14237 if (SYSV386_COMPAT)
14238 fprintf (stream, _("(default: att)\n"));
14239 else
14240 fprintf (stream, _("(default: intel)\n"));
14241 fprintf (stream, _("\
14242 use AT&T/Intel mnemonic\n"));
ba104c83 14243 fprintf (stream, _("\
7c5c05ef
L
14244 -msyntax=[att|intel] (default: att)\n\
14245 use AT&T/Intel syntax\n"));
ba104c83
L
14246 fprintf (stream, _("\
14247 -mindex-reg support pseudo index registers\n"));
14248 fprintf (stream, _("\
14249 -mnaked-reg don't require `%%' prefix for registers\n"));
14250 fprintf (stream, _("\
7e8b059b 14251 -madd-bnd-prefix add BND prefix for all valid branches\n"));
b4a3a7b4 14252#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8dcea932
L
14253 fprintf (stream, _("\
14254 -mshared disable branch optimization for shared code\n"));
b4a3a7b4
L
14255 fprintf (stream, _("\
14256 -mx86-used-note=[no|yes] "));
14257 if (DEFAULT_X86_USED_NOTE)
14258 fprintf (stream, _("(default: yes)\n"));
14259 else
14260 fprintf (stream, _("(default: no)\n"));
14261 fprintf (stream, _("\
14262 generate x86 used ISA and feature properties\n"));
14263#endif
14264#if defined (TE_PE) || defined (TE_PEP)
167ad85b
TG
14265 fprintf (stream, _("\
14266 -mbig-obj generate big object files\n"));
14267#endif
d022bddd 14268 fprintf (stream, _("\
7c5c05ef 14269 -momit-lock-prefix=[no|yes] (default: no)\n\
d022bddd 14270 strip all lock prefixes\n"));
5db04b09 14271 fprintf (stream, _("\
7c5c05ef 14272 -mfence-as-lock-add=[no|yes] (default: no)\n\
e4e00185
AS
14273 encode lfence, mfence and sfence as\n\
14274 lock addl $0x0, (%%{re}sp)\n"));
14275 fprintf (stream, _("\
7c5c05ef
L
14276 -mrelax-relocations=[no|yes] "));
14277 if (DEFAULT_GENERATE_X86_RELAX_RELOCATIONS)
14278 fprintf (stream, _("(default: yes)\n"));
14279 else
14280 fprintf (stream, _("(default: no)\n"));
14281 fprintf (stream, _("\
0cb4071e
L
14282 generate relax relocations\n"));
14283 fprintf (stream, _("\
e379e5f3
L
14284 -malign-branch-boundary=NUM (default: 0)\n\
14285 align branches within NUM byte boundary\n"));
14286 fprintf (stream, _("\
14287 -malign-branch=TYPE[+TYPE...] (default: jcc+fused+jmp)\n\
14288 TYPE is combination of jcc, fused, jmp, call, ret,\n\
14289 indirect\n\
14290 specify types of branches to align\n"));
14291 fprintf (stream, _("\
14292 -malign-branch-prefix-size=NUM (default: 5)\n\
14293 align branches with NUM prefixes per instruction\n"));
14294 fprintf (stream, _("\
76cf450b
L
14295 -mbranches-within-32B-boundaries\n\
14296 align branches within 32 byte boundary\n"));
14297 fprintf (stream, _("\
ae531041
L
14298 -mlfence-after-load=[no|yes] (default: no)\n\
14299 generate lfence after load\n"));
14300 fprintf (stream, _("\
14301 -mlfence-before-indirect-branch=[none|all|register|memory] (default: none)\n\
14302 generate lfence before indirect near branch\n"));
14303 fprintf (stream, _("\
a09f656b 14304 -mlfence-before-ret=[none|or|not|shl|yes] (default: none)\n\
ae531041
L
14305 generate lfence before ret\n"));
14306 fprintf (stream, _("\
7c5c05ef 14307 -mamd64 accept only AMD64 ISA [default]\n"));
5db04b09
L
14308 fprintf (stream, _("\
14309 -mintel64 accept only Intel64 ISA\n"));
252b5132
RH
14310}
14311
3e73aa7c 14312#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
321098a5 14313 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
e57f8c65 14314 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
252b5132
RH
14315
14316/* Pick the target format to use. */
14317
47926f60 14318const char *
e3bb37b5 14319i386_target_format (void)
252b5132 14320{
d34049e8 14321 if (startswith (default_arch, "x86_64"))
351f65ca
L
14322 {
14323 update_code_flag (CODE_64BIT, 1);
14324 if (default_arch[6] == '\0')
7f56bc95 14325 x86_elf_abi = X86_64_ABI;
351f65ca 14326 else
7f56bc95 14327 x86_elf_abi = X86_64_X32_ABI;
351f65ca 14328 }
3e73aa7c 14329 else if (!strcmp (default_arch, "i386"))
78f12dd3 14330 update_code_flag (CODE_32BIT, 1);
5197d474
L
14331 else if (!strcmp (default_arch, "iamcu"))
14332 {
14333 update_code_flag (CODE_32BIT, 1);
14334 if (cpu_arch_isa == PROCESSOR_UNKNOWN)
14335 {
14336 static const i386_cpu_flags iamcu_flags = CPU_IAMCU_FLAGS;
14337 cpu_arch_name = "iamcu";
d92c7521 14338 free (cpu_sub_arch_name);
5197d474
L
14339 cpu_sub_arch_name = NULL;
14340 cpu_arch_flags = iamcu_flags;
14341 cpu_arch_isa = PROCESSOR_IAMCU;
14342 cpu_arch_isa_flags = iamcu_flags;
14343 if (!cpu_arch_tune_set)
14344 {
14345 cpu_arch_tune = cpu_arch_isa;
14346 cpu_arch_tune_flags = cpu_arch_isa_flags;
14347 }
14348 }
8d471ec1 14349 else if (cpu_arch_isa != PROCESSOR_IAMCU)
5197d474
L
14350 as_fatal (_("Intel MCU doesn't support `%s' architecture"),
14351 cpu_arch_name);
14352 }
3e73aa7c 14353 else
2b5d6a91 14354 as_fatal (_("unknown architecture"));
89507696
JB
14355
14356 if (cpu_flags_all_zero (&cpu_arch_isa_flags))
ae89daec 14357 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
89507696 14358 if (cpu_flags_all_zero (&cpu_arch_tune_flags))
ae89daec 14359 cpu_arch_tune_flags = cpu_arch[flag_code == CODE_64BIT].enable;
89507696 14360
252b5132
RH
14361 switch (OUTPUT_FLAVOR)
14362 {
9384f2ff 14363#if defined (OBJ_MAYBE_AOUT) || defined (OBJ_AOUT)
4c63da97 14364 case bfd_target_aout_flavour:
47926f60 14365 return AOUT_TARGET_FORMAT;
4c63da97 14366#endif
9384f2ff
AM
14367#if defined (OBJ_MAYBE_COFF) || defined (OBJ_COFF)
14368# if defined (TE_PE) || defined (TE_PEP)
14369 case bfd_target_coff_flavour:
167ad85b 14370 if (flag_code == CODE_64BIT)
eb19308f
JB
14371 {
14372 object_64bit = 1;
14373 return use_big_obj ? "pe-bigobj-x86-64" : "pe-x86-64";
14374 }
14375 return use_big_obj ? "pe-bigobj-i386" : "pe-i386";
9384f2ff 14376# elif defined (TE_GO32)
0561d57c
JK
14377 case bfd_target_coff_flavour:
14378 return "coff-go32";
9384f2ff 14379# else
252b5132
RH
14380 case bfd_target_coff_flavour:
14381 return "coff-i386";
9384f2ff 14382# endif
4c63da97 14383#endif
3e73aa7c 14384#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
252b5132 14385 case bfd_target_elf_flavour:
3e73aa7c 14386 {
351f65ca
L
14387 const char *format;
14388
14389 switch (x86_elf_abi)
4fa24527 14390 {
351f65ca
L
14391 default:
14392 format = ELF_TARGET_FORMAT;
e379e5f3
L
14393#ifndef TE_SOLARIS
14394 tls_get_addr = "___tls_get_addr";
14395#endif
351f65ca 14396 break;
7f56bc95 14397 case X86_64_ABI:
351f65ca 14398 use_rela_relocations = 1;
4fa24527 14399 object_64bit = 1;
e379e5f3
L
14400#ifndef TE_SOLARIS
14401 tls_get_addr = "__tls_get_addr";
14402#endif
351f65ca
L
14403 format = ELF_TARGET_FORMAT64;
14404 break;
7f56bc95 14405 case X86_64_X32_ABI:
4fa24527 14406 use_rela_relocations = 1;
351f65ca 14407 object_64bit = 1;
e379e5f3
L
14408#ifndef TE_SOLARIS
14409 tls_get_addr = "__tls_get_addr";
14410#endif
862be3fb 14411 disallow_64bit_reloc = 1;
351f65ca
L
14412 format = ELF_TARGET_FORMAT32;
14413 break;
4fa24527 14414 }
c085ab00 14415 if (cpu_arch_isa == PROCESSOR_IAMCU)
81486035
L
14416 {
14417 if (x86_elf_abi != I386_ABI)
14418 as_fatal (_("Intel MCU is 32bit only"));
14419 return ELF_TARGET_IAMCU_FORMAT;
14420 }
8a9036a4 14421 else
351f65ca 14422 return format;
3e73aa7c 14423 }
e57f8c65
TG
14424#endif
14425#if defined (OBJ_MACH_O)
14426 case bfd_target_mach_o_flavour:
d382c579
TG
14427 if (flag_code == CODE_64BIT)
14428 {
14429 use_rela_relocations = 1;
14430 object_64bit = 1;
14431 return "mach-o-x86-64";
14432 }
14433 else
14434 return "mach-o-i386";
4c63da97 14435#endif
252b5132
RH
14436 default:
14437 abort ();
14438 return NULL;
14439 }
14440}
14441
47926f60 14442#endif /* OBJ_MAYBE_ more than one */
252b5132 14443\f
252b5132 14444symbolS *
7016a5d5 14445md_undefined_symbol (char *name)
252b5132 14446{
18dc2407
ILT
14447 if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
14448 && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
14449 && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
14450 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
24eab124
AM
14451 {
14452 if (!GOT_symbol)
14453 {
14454 if (symbol_find (name))
14455 as_bad (_("GOT already in symbol table"));
14456 GOT_symbol = symbol_new (name, undefined_section,
e01e1cee 14457 &zero_address_frag, 0);
24eab124
AM
14458 };
14459 return GOT_symbol;
14460 }
252b5132
RH
14461 return 0;
14462}
14463
14464/* Round up a section size to the appropriate boundary. */
47926f60 14465
252b5132 14466valueT
7016a5d5 14467md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
252b5132 14468{
4c63da97
AM
14469#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
14470 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
14471 {
14472 /* For a.out, force the section size to be aligned. If we don't do
14473 this, BFD will align it for us, but it will not write out the
14474 final bytes of the section. This may be a bug in BFD, but it is
14475 easier to fix it here since that is how the other a.out targets
14476 work. */
14477 int align;
14478
fd361982 14479 align = bfd_section_alignment (segment);
8d3842cd 14480 size = ((size + (1 << align) - 1) & (-((valueT) 1 << align)));
4c63da97 14481 }
252b5132
RH
14482#endif
14483
14484 return size;
14485}
14486
14487/* On the i386, PC-relative offsets are relative to the start of the
14488 next instruction. That is, the address of the offset, plus its
14489 size, since the offset is always the last part of the insn. */
14490
14491long
e3bb37b5 14492md_pcrel_from (fixS *fixP)
252b5132
RH
14493{
14494 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
14495}
14496
14497#ifndef I386COFF
14498
14499static void
e3bb37b5 14500s_bss (int ignore ATTRIBUTE_UNUSED)
252b5132 14501{
29b0f896 14502 int temp;
252b5132 14503
8a75718c
JB
14504#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14505 if (IS_ELF)
14506 obj_elf_section_change_hook ();
14507#endif
252b5132
RH
14508 temp = get_absolute_expression ();
14509 subseg_set (bss_section, (subsegT) temp);
14510 demand_empty_rest_of_line ();
14511}
14512
14513#endif
14514
e379e5f3
L
14515/* Remember constant directive. */
14516
14517void
14518i386_cons_align (int ignore ATTRIBUTE_UNUSED)
14519{
14520 if (last_insn.kind != last_insn_directive
14521 && (bfd_section_flags (now_seg) & SEC_CODE))
14522 {
14523 last_insn.seg = now_seg;
14524 last_insn.kind = last_insn_directive;
14525 last_insn.name = "constant directive";
14526 last_insn.file = as_where (&last_insn.line);
ae531041
L
14527 if (lfence_before_ret != lfence_before_ret_none)
14528 {
14529 if (lfence_before_indirect_branch != lfence_branch_none)
14530 as_warn (_("constant directive skips -mlfence-before-ret "
14531 "and -mlfence-before-indirect-branch"));
14532 else
14533 as_warn (_("constant directive skips -mlfence-before-ret"));
14534 }
14535 else if (lfence_before_indirect_branch != lfence_branch_none)
14536 as_warn (_("constant directive skips -mlfence-before-indirect-branch"));
e379e5f3
L
14537 }
14538}
14539
3abbafc2 14540int
e3bb37b5 14541i386_validate_fix (fixS *fixp)
252b5132 14542{
e52a16f2
JB
14543 if (fixp->fx_addsy && S_GET_SEGMENT(fixp->fx_addsy) == reg_section)
14544 {
14545 reloc_howto_type *howto;
14546
14547 howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
14548 as_bad_where (fixp->fx_file, fixp->fx_line,
14549 _("invalid %s relocation against register"),
14550 howto ? howto->name : "<unknown>");
14551 return 0;
14552 }
14553
3abbafc2
JB
14554#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14555 if (fixp->fx_r_type == BFD_RELOC_SIZE32
14556 || fixp->fx_r_type == BFD_RELOC_SIZE64)
14557 return IS_ELF && fixp->fx_addsy
14558 && (!S_IS_DEFINED (fixp->fx_addsy)
14559 || S_IS_EXTERNAL (fixp->fx_addsy));
14560#endif
14561
02a86693 14562 if (fixp->fx_subsy)
252b5132 14563 {
02a86693 14564 if (fixp->fx_subsy == GOT_symbol)
23df1078 14565 {
02a86693
L
14566 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
14567 {
14568 if (!object_64bit)
14569 abort ();
14570#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14571 if (fixp->fx_tcbit2)
56ceb5b5
L
14572 fixp->fx_r_type = (fixp->fx_tcbit
14573 ? BFD_RELOC_X86_64_REX_GOTPCRELX
14574 : BFD_RELOC_X86_64_GOTPCRELX);
02a86693
L
14575 else
14576#endif
14577 fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
14578 }
d6ab8113 14579 else
02a86693
L
14580 {
14581 if (!object_64bit)
14582 fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
14583 else
14584 fixp->fx_r_type = BFD_RELOC_X86_64_GOTOFF64;
14585 }
14586 fixp->fx_subsy = 0;
23df1078 14587 }
252b5132 14588 }
02a86693 14589#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2585b7a5 14590 else
02a86693 14591 {
2585b7a5
L
14592 /* NB: Commit 292676c1 resolved PLT32 reloc aganst local symbol
14593 to section. Since PLT32 relocation must be against symbols,
14594 turn such PLT32 relocation into PC32 relocation. */
14595 if (fixp->fx_addsy
14596 && (fixp->fx_r_type == BFD_RELOC_386_PLT32
14597 || fixp->fx_r_type == BFD_RELOC_X86_64_PLT32)
14598 && symbol_section_p (fixp->fx_addsy))
14599 fixp->fx_r_type = BFD_RELOC_32_PCREL;
14600 if (!object_64bit)
14601 {
14602 if (fixp->fx_r_type == BFD_RELOC_386_GOT32
14603 && fixp->fx_tcbit2)
14604 fixp->fx_r_type = BFD_RELOC_386_GOT32X;
14605 }
02a86693
L
14606 }
14607#endif
3abbafc2
JB
14608
14609 return 1;
252b5132
RH
14610}
14611
252b5132 14612arelent *
7016a5d5 14613tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
14614{
14615 arelent *rel;
14616 bfd_reloc_code_real_type code;
14617
14618 switch (fixp->fx_r_type)
14619 {
8ce3d284 14620#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3abbafc2
JB
14621 symbolS *sym;
14622
8fd4256d
L
14623 case BFD_RELOC_SIZE32:
14624 case BFD_RELOC_SIZE64:
3abbafc2
JB
14625 if (fixp->fx_addsy
14626 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))
14627 && (!fixp->fx_subsy
14628 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))))
14629 sym = fixp->fx_addsy;
14630 else if (fixp->fx_subsy
14631 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))
14632 && (!fixp->fx_addsy
14633 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))))
14634 sym = fixp->fx_subsy;
14635 else
14636 sym = NULL;
14637 if (IS_ELF && sym && S_IS_DEFINED (sym) && !S_IS_EXTERNAL (sym))
8fd4256d
L
14638 {
14639 /* Resolve size relocation against local symbol to size of
14640 the symbol plus addend. */
3abbafc2 14641 valueT value = S_GET_SIZE (sym);
44f87162 14642
3abbafc2
JB
14643 if (symbol_get_bfdsym (sym)->flags & BSF_SECTION_SYM)
14644 value = bfd_section_size (S_GET_SEGMENT (sym));
14645 if (sym == fixp->fx_subsy)
14646 {
14647 value = -value;
14648 if (fixp->fx_addsy)
14649 value += S_GET_VALUE (fixp->fx_addsy);
14650 }
14651 else if (fixp->fx_subsy)
14652 value -= S_GET_VALUE (fixp->fx_subsy);
44f87162 14653 value += fixp->fx_offset;
8fd4256d 14654 if (fixp->fx_r_type == BFD_RELOC_SIZE32
d965814f 14655 && object_64bit
8fd4256d
L
14656 && !fits_in_unsigned_long (value))
14657 as_bad_where (fixp->fx_file, fixp->fx_line,
14658 _("symbol size computation overflow"));
14659 fixp->fx_addsy = NULL;
14660 fixp->fx_subsy = NULL;
14661 md_apply_fix (fixp, (valueT *) &value, NULL);
14662 return NULL;
14663 }
3abbafc2
JB
14664 if (!fixp->fx_addsy || fixp->fx_subsy)
14665 {
14666 as_bad_where (fixp->fx_file, fixp->fx_line,
14667 "unsupported expression involving @size");
14668 return NULL;
14669 }
8ce3d284 14670#endif
1a0670f3 14671 /* Fall through. */
8fd4256d 14672
3e73aa7c
JH
14673 case BFD_RELOC_X86_64_PLT32:
14674 case BFD_RELOC_X86_64_GOT32:
14675 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
14676 case BFD_RELOC_X86_64_GOTPCRELX:
14677 case BFD_RELOC_X86_64_REX_GOTPCRELX:
252b5132
RH
14678 case BFD_RELOC_386_PLT32:
14679 case BFD_RELOC_386_GOT32:
02a86693 14680 case BFD_RELOC_386_GOT32X:
252b5132
RH
14681 case BFD_RELOC_386_GOTOFF:
14682 case BFD_RELOC_386_GOTPC:
13ae64f3
JJ
14683 case BFD_RELOC_386_TLS_GD:
14684 case BFD_RELOC_386_TLS_LDM:
14685 case BFD_RELOC_386_TLS_LDO_32:
14686 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
14687 case BFD_RELOC_386_TLS_IE:
14688 case BFD_RELOC_386_TLS_GOTIE:
13ae64f3
JJ
14689 case BFD_RELOC_386_TLS_LE_32:
14690 case BFD_RELOC_386_TLS_LE:
67a4f2b7
AO
14691 case BFD_RELOC_386_TLS_GOTDESC:
14692 case BFD_RELOC_386_TLS_DESC_CALL:
bffbf940
JJ
14693 case BFD_RELOC_X86_64_TLSGD:
14694 case BFD_RELOC_X86_64_TLSLD:
14695 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 14696 case BFD_RELOC_X86_64_DTPOFF64:
bffbf940
JJ
14697 case BFD_RELOC_X86_64_GOTTPOFF:
14698 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113
JB
14699 case BFD_RELOC_X86_64_TPOFF64:
14700 case BFD_RELOC_X86_64_GOTOFF64:
14701 case BFD_RELOC_X86_64_GOTPC32:
7b81dfbb
AJ
14702 case BFD_RELOC_X86_64_GOT64:
14703 case BFD_RELOC_X86_64_GOTPCREL64:
14704 case BFD_RELOC_X86_64_GOTPC64:
14705 case BFD_RELOC_X86_64_GOTPLT64:
14706 case BFD_RELOC_X86_64_PLTOFF64:
67a4f2b7
AO
14707 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14708 case BFD_RELOC_X86_64_TLSDESC_CALL:
252b5132
RH
14709 case BFD_RELOC_RVA:
14710 case BFD_RELOC_VTABLE_ENTRY:
14711 case BFD_RELOC_VTABLE_INHERIT:
6482c264
NC
14712#ifdef TE_PE
14713 case BFD_RELOC_32_SECREL:
145667f8 14714 case BFD_RELOC_16_SECIDX:
6482c264 14715#endif
252b5132
RH
14716 code = fixp->fx_r_type;
14717 break;
dbbaec26
L
14718 case BFD_RELOC_X86_64_32S:
14719 if (!fixp->fx_pcrel)
14720 {
14721 /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32. */
14722 code = fixp->fx_r_type;
14723 break;
14724 }
1a0670f3 14725 /* Fall through. */
252b5132 14726 default:
93382f6d 14727 if (fixp->fx_pcrel)
252b5132 14728 {
93382f6d
AM
14729 switch (fixp->fx_size)
14730 {
14731 default:
b091f402
AM
14732 as_bad_where (fixp->fx_file, fixp->fx_line,
14733 _("can not do %d byte pc-relative relocation"),
14734 fixp->fx_size);
93382f6d
AM
14735 code = BFD_RELOC_32_PCREL;
14736 break;
14737 case 1: code = BFD_RELOC_8_PCREL; break;
14738 case 2: code = BFD_RELOC_16_PCREL; break;
d258b828 14739 case 4: code = BFD_RELOC_32_PCREL; break;
d6ab8113
JB
14740#ifdef BFD64
14741 case 8: code = BFD_RELOC_64_PCREL; break;
14742#endif
93382f6d
AM
14743 }
14744 }
14745 else
14746 {
14747 switch (fixp->fx_size)
14748 {
14749 default:
b091f402
AM
14750 as_bad_where (fixp->fx_file, fixp->fx_line,
14751 _("can not do %d byte relocation"),
14752 fixp->fx_size);
93382f6d
AM
14753 code = BFD_RELOC_32;
14754 break;
14755 case 1: code = BFD_RELOC_8; break;
14756 case 2: code = BFD_RELOC_16; break;
14757 case 4: code = BFD_RELOC_32; break;
937149dd 14758#ifdef BFD64
3e73aa7c 14759 case 8: code = BFD_RELOC_64; break;
937149dd 14760#endif
93382f6d 14761 }
252b5132
RH
14762 }
14763 break;
14764 }
252b5132 14765
d182319b
JB
14766 if ((code == BFD_RELOC_32
14767 || code == BFD_RELOC_32_PCREL
14768 || code == BFD_RELOC_X86_64_32S)
252b5132
RH
14769 && GOT_symbol
14770 && fixp->fx_addsy == GOT_symbol)
3e73aa7c 14771 {
4fa24527 14772 if (!object_64bit)
d6ab8113
JB
14773 code = BFD_RELOC_386_GOTPC;
14774 else
14775 code = BFD_RELOC_X86_64_GOTPC32;
3e73aa7c 14776 }
7b81dfbb
AJ
14777 if ((code == BFD_RELOC_64 || code == BFD_RELOC_64_PCREL)
14778 && GOT_symbol
14779 && fixp->fx_addsy == GOT_symbol)
14780 {
14781 code = BFD_RELOC_X86_64_GOTPC64;
14782 }
252b5132 14783
add39d23
TS
14784 rel = XNEW (arelent);
14785 rel->sym_ptr_ptr = XNEW (asymbol *);
49309057 14786 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
14787
14788 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
c87db184 14789
3e73aa7c
JH
14790 if (!use_rela_relocations)
14791 {
14792 /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
14793 vtable entry to be used in the relocation's section offset. */
14794 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
14795 rel->address = fixp->fx_offset;
fbeb56a4
DK
14796#if defined (OBJ_COFF) && defined (TE_PE)
14797 else if (fixp->fx_addsy && S_IS_WEAK (fixp->fx_addsy))
14798 rel->addend = fixp->fx_addnumber - (S_GET_VALUE (fixp->fx_addsy) * 2);
14799 else
14800#endif
c6682705 14801 rel->addend = 0;
3e73aa7c
JH
14802 }
14803 /* Use the rela in 64bit mode. */
252b5132 14804 else
3e73aa7c 14805 {
862be3fb
L
14806 if (disallow_64bit_reloc)
14807 switch (code)
14808 {
862be3fb
L
14809 case BFD_RELOC_X86_64_DTPOFF64:
14810 case BFD_RELOC_X86_64_TPOFF64:
14811 case BFD_RELOC_64_PCREL:
14812 case BFD_RELOC_X86_64_GOTOFF64:
14813 case BFD_RELOC_X86_64_GOT64:
14814 case BFD_RELOC_X86_64_GOTPCREL64:
14815 case BFD_RELOC_X86_64_GOTPC64:
14816 case BFD_RELOC_X86_64_GOTPLT64:
14817 case BFD_RELOC_X86_64_PLTOFF64:
14818 as_bad_where (fixp->fx_file, fixp->fx_line,
14819 _("cannot represent relocation type %s in x32 mode"),
14820 bfd_get_reloc_code_name (code));
14821 break;
14822 default:
14823 break;
14824 }
14825
062cd5e7
AS
14826 if (!fixp->fx_pcrel)
14827 rel->addend = fixp->fx_offset;
14828 else
14829 switch (code)
14830 {
14831 case BFD_RELOC_X86_64_PLT32:
14832 case BFD_RELOC_X86_64_GOT32:
14833 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
14834 case BFD_RELOC_X86_64_GOTPCRELX:
14835 case BFD_RELOC_X86_64_REX_GOTPCRELX:
bffbf940
JJ
14836 case BFD_RELOC_X86_64_TLSGD:
14837 case BFD_RELOC_X86_64_TLSLD:
14838 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7
AO
14839 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14840 case BFD_RELOC_X86_64_TLSDESC_CALL:
062cd5e7
AS
14841 rel->addend = fixp->fx_offset - fixp->fx_size;
14842 break;
14843 default:
14844 rel->addend = (section->vma
14845 - fixp->fx_size
14846 + fixp->fx_addnumber
14847 + md_pcrel_from (fixp));
14848 break;
14849 }
3e73aa7c
JH
14850 }
14851
252b5132
RH
14852 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
14853 if (rel->howto == NULL)
14854 {
14855 as_bad_where (fixp->fx_file, fixp->fx_line,
d0b47220 14856 _("cannot represent relocation type %s"),
252b5132
RH
14857 bfd_get_reloc_code_name (code));
14858 /* Set howto to a garbage value so that we can keep going. */
14859 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
9c2799c2 14860 gas_assert (rel->howto != NULL);
252b5132
RH
14861 }
14862
14863 return rel;
14864}
14865
ee86248c 14866#include "tc-i386-intel.c"
54cfded0 14867
a60de03c
JB
14868void
14869tc_x86_parse_to_dw2regnum (expressionS *exp)
54cfded0 14870{
a60de03c
JB
14871 int saved_naked_reg;
14872 char saved_register_dot;
54cfded0 14873
a60de03c
JB
14874 saved_naked_reg = allow_naked_reg;
14875 allow_naked_reg = 1;
14876 saved_register_dot = register_chars['.'];
14877 register_chars['.'] = '.';
14878 allow_pseudo_reg = 1;
14879 expression_and_evaluate (exp);
14880 allow_pseudo_reg = 0;
14881 register_chars['.'] = saved_register_dot;
14882 allow_naked_reg = saved_naked_reg;
14883
e96d56a1 14884 if (exp->X_op == O_register && exp->X_add_number >= 0)
54cfded0 14885 {
a60de03c
JB
14886 if ((addressT) exp->X_add_number < i386_regtab_size)
14887 {
14888 exp->X_op = O_constant;
14889 exp->X_add_number = i386_regtab[exp->X_add_number]
14890 .dw2_regnum[flag_code >> 1];
14891 }
14892 else
14893 exp->X_op = O_illegal;
54cfded0 14894 }
54cfded0
AM
14895}
14896
14897void
14898tc_x86_frame_initial_instructions (void)
14899{
a60de03c
JB
14900 static unsigned int sp_regno[2];
14901
14902 if (!sp_regno[flag_code >> 1])
14903 {
14904 char *saved_input = input_line_pointer;
14905 char sp[][4] = {"esp", "rsp"};
14906 expressionS exp;
a4447b93 14907
a60de03c
JB
14908 input_line_pointer = sp[flag_code >> 1];
14909 tc_x86_parse_to_dw2regnum (&exp);
9c2799c2 14910 gas_assert (exp.X_op == O_constant);
a60de03c
JB
14911 sp_regno[flag_code >> 1] = exp.X_add_number;
14912 input_line_pointer = saved_input;
14913 }
a4447b93 14914
61ff971f
L
14915 cfi_add_CFA_def_cfa (sp_regno[flag_code >> 1], -x86_cie_data_alignment);
14916 cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
54cfded0 14917}
d2b2c203 14918
d7921315
L
14919int
14920x86_dwarf2_addr_size (void)
14921{
14922#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
14923 if (x86_elf_abi == X86_64_X32_ABI)
14924 return 4;
14925#endif
14926 return bfd_arch_bits_per_address (stdoutput) / 8;
14927}
14928
d2b2c203
DJ
14929int
14930i386_elf_section_type (const char *str, size_t len)
14931{
14932 if (flag_code == CODE_64BIT
14933 && len == sizeof ("unwind") - 1
d34049e8 14934 && startswith (str, "unwind"))
d2b2c203
DJ
14935 return SHT_X86_64_UNWIND;
14936
14937 return -1;
14938}
bb41ade5 14939
ad5fec3b
EB
14940#ifdef TE_SOLARIS
14941void
14942i386_solaris_fix_up_eh_frame (segT sec)
14943{
14944 if (flag_code == CODE_64BIT)
14945 elf_section_type (sec) = SHT_X86_64_UNWIND;
14946}
14947#endif
14948
bb41ade5
AM
14949#ifdef TE_PE
14950void
14951tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
14952{
91d6fa6a 14953 expressionS exp;
bb41ade5 14954
91d6fa6a
NC
14955 exp.X_op = O_secrel;
14956 exp.X_add_symbol = symbol;
14957 exp.X_add_number = 0;
14958 emit_expr (&exp, size);
bb41ade5
AM
14959}
14960#endif
3b22753a
L
14961
14962#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14963/* For ELF on x86-64, add support for SHF_X86_64_LARGE. */
14964
01e1a5bc 14965bfd_vma
6d4af3c2 14966x86_64_section_letter (int letter, const char **ptr_msg)
3b22753a
L
14967{
14968 if (flag_code == CODE_64BIT)
14969 {
14970 if (letter == 'l')
14971 return SHF_X86_64_LARGE;
14972
8f3bae45 14973 *ptr_msg = _("bad .section directive: want a,l,w,x,M,S,G,T in string");
64e74474 14974 }
3b22753a 14975 else
8f3bae45 14976 *ptr_msg = _("bad .section directive: want a,w,x,M,S,G,T in string");
3b22753a
L
14977 return -1;
14978}
14979
01e1a5bc 14980bfd_vma
3b22753a
L
14981x86_64_section_word (char *str, size_t len)
14982{
08dedd66 14983 if (len == 5 && flag_code == CODE_64BIT && startswith (str, "large"))
3b22753a
L
14984 return SHF_X86_64_LARGE;
14985
14986 return -1;
14987}
14988
14989static void
14990handle_large_common (int small ATTRIBUTE_UNUSED)
14991{
14992 if (flag_code != CODE_64BIT)
14993 {
14994 s_comm_internal (0, elf_common_parse);
14995 as_warn (_(".largecomm supported only in 64bit mode, producing .comm"));
14996 }
14997 else
14998 {
14999 static segT lbss_section;
15000 asection *saved_com_section_ptr = elf_com_section_ptr;
15001 asection *saved_bss_section = bss_section;
15002
15003 if (lbss_section == NULL)
15004 {
15005 flagword applicable;
15006 segT seg = now_seg;
15007 subsegT subseg = now_subseg;
15008
15009 /* The .lbss section is for local .largecomm symbols. */
15010 lbss_section = subseg_new (".lbss", 0);
15011 applicable = bfd_applicable_section_flags (stdoutput);
fd361982 15012 bfd_set_section_flags (lbss_section, applicable & SEC_ALLOC);
3b22753a
L
15013 seg_info (lbss_section)->bss = 1;
15014
15015 subseg_set (seg, subseg);
15016 }
15017
15018 elf_com_section_ptr = &_bfd_elf_large_com_section;
15019 bss_section = lbss_section;
15020
15021 s_comm_internal (0, elf_common_parse);
15022
15023 elf_com_section_ptr = saved_com_section_ptr;
15024 bss_section = saved_bss_section;
15025 }
15026}
15027#endif /* OBJ_ELF || OBJ_MAYBE_ELF */