]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/config/tc-i386.c
gas: generate .sframe from CFI directives
[thirdparty/binutils-gdb.git] / gas / config / tc-i386.c
CommitLineData
b534c6d3 1/* tc-i386.c -- Assemble code for the Intel 80386
a2c58332 2 Copyright (C) 1989-2022 Free Software Foundation, Inc.
252b5132
RH
3
4 This file is part of GAS, the GNU Assembler.
5
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
ec2655a6 8 the Free Software Foundation; either version 3, or (at your option)
252b5132
RH
9 any later version.
10
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to the Free
4b4da160
NC
18 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
19 02110-1301, USA. */
252b5132 20
47926f60
KH
21/* Intel 80386 machine specific gas.
22 Written by Eliot Dresselhaus (eliot@mgm.mit.edu).
3e73aa7c 23 x86_64 support by Jan Hubicka (jh@suse.cz)
0f10071e 24 VIA PadLock support by Michal Ludvig (mludvig@suse.cz)
47926f60
KH
25 Bugs & suggestions are completely welcome. This is free software.
26 Please help us make it better. */
252b5132 27
252b5132 28#include "as.h"
3882b010 29#include "safe-ctype.h"
252b5132 30#include "subsegs.h"
316e2c05 31#include "dwarf2dbg.h"
54cfded0 32#include "dw2gencfi.h"
b52c4ee4
IB
33#include "gen-sframe.h"
34#include "sframe.h"
d2b2c203 35#include "elf/x86-64.h"
40fb9820 36#include "opcodes/i386-init.h"
41fd2579 37#include <limits.h>
41fd2579 38
c3332e24 39#ifndef INFER_ADDR_PREFIX
eecb386c 40#define INFER_ADDR_PREFIX 1
c3332e24
AM
41#endif
42
29b0f896
AM
43#ifndef DEFAULT_ARCH
44#define DEFAULT_ARCH "i386"
246fcdee 45#endif
252b5132 46
edde18a5
AM
47#ifndef INLINE
48#if __GNUC__ >= 2
49#define INLINE __inline__
50#else
51#define INLINE
52#endif
53#endif
54
6305a203
L
55/* Prefixes will be emitted in the order defined below.
56 WAIT_PREFIX must be the first prefix since FWAIT is really is an
57 instruction, and so must come before any prefixes.
58 The preferred prefix order is SEG_PREFIX, ADDR_PREFIX, DATA_PREFIX,
42164a71 59 REP_PREFIX/HLE_PREFIX, LOCK_PREFIX. */
6305a203
L
60#define WAIT_PREFIX 0
61#define SEG_PREFIX 1
62#define ADDR_PREFIX 2
63#define DATA_PREFIX 3
c32fa91d 64#define REP_PREFIX 4
42164a71 65#define HLE_PREFIX REP_PREFIX
7e8b059b 66#define BND_PREFIX REP_PREFIX
c32fa91d 67#define LOCK_PREFIX 5
4e9ac44a
L
68#define REX_PREFIX 6 /* must come last. */
69#define MAX_PREFIXES 7 /* max prefixes per opcode */
6305a203
L
70
71/* we define the syntax here (modulo base,index,scale syntax) */
72#define REGISTER_PREFIX '%'
73#define IMMEDIATE_PREFIX '$'
74#define ABSOLUTE_PREFIX '*'
75
76/* these are the instruction mnemonic suffixes in AT&T syntax or
77 memory operand size in Intel syntax. */
78#define WORD_MNEM_SUFFIX 'w'
79#define BYTE_MNEM_SUFFIX 'b'
80#define SHORT_MNEM_SUFFIX 's'
81#define LONG_MNEM_SUFFIX 'l'
82#define QWORD_MNEM_SUFFIX 'q'
6305a203
L
83/* Intel Syntax. Use a non-ascii letter since since it never appears
84 in instructions. */
85#define LONG_DOUBLE_MNEM_SUFFIX '\1'
86
87#define END_OF_INSN '\0'
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);
e3bb37b5
L
140static void set_code_flag (int);
141static void set_16bit_gcc_code_flag (int);
142static void set_intel_syntax (int);
1efbbeb4 143static void set_intel_mnemonic (int);
db51cc60 144static void set_allow_index_reg (int);
7bab8ab5 145static void set_check (int);
e3bb37b5 146static void set_cpu_arch (int);
6482c264 147#ifdef TE_PE
e3bb37b5 148static void pe_directive_secrel (int);
145667f8 149static void pe_directive_secidx (int);
6482c264 150#endif
e3bb37b5
L
151static void signed_cons (int);
152static char *output_invalid (int c);
ee86248c
JB
153static int i386_finalize_immediate (segT, expressionS *, i386_operand_type,
154 const char *);
155static int i386_finalize_displacement (segT, expressionS *, i386_operand_type,
156 const char *);
a7619375 157static int i386_att_operand (char *);
e3bb37b5 158static int i386_intel_operand (char *, int);
ee86248c
JB
159static int i386_intel_simplify (expressionS *);
160static int i386_intel_parse_name (const char *, expressionS *);
e3bb37b5
L
161static const reg_entry *parse_register (char *, char **);
162static char *parse_insn (char *, char *);
163static char *parse_operands (char *, const char *);
164static void swap_operands (void);
783c187b 165static void swap_2_operands (unsigned int, unsigned int);
48bcea9f 166static enum flag_code i386_addressing_mode (void);
e3bb37b5
L
167static void optimize_imm (void);
168static void optimize_disp (void);
83b16ac6 169static const insn_template *match_template (char);
e3bb37b5
L
170static int check_string (void);
171static int process_suffix (void);
172static int check_byte_reg (void);
173static int check_long_reg (void);
174static int check_qword_reg (void);
175static int check_word_reg (void);
176static int finalize_imm (void);
177static int process_operands (void);
5e042380 178static const reg_entry *build_modrm_byte (void);
e3bb37b5
L
179static void output_insn (void);
180static void output_imm (fragS *, offsetT);
181static void output_disp (fragS *, offsetT);
29b0f896 182#ifndef I386COFF
e3bb37b5 183static void s_bss (int);
252b5132 184#endif
17d4e2a2
L
185#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
186static void handle_large_common (int small ATTRIBUTE_UNUSED);
b4a3a7b4
L
187
188/* GNU_PROPERTY_X86_ISA_1_USED. */
189static unsigned int x86_isa_1_used;
190/* GNU_PROPERTY_X86_FEATURE_2_USED. */
191static unsigned int x86_feature_2_used;
192/* Generate x86 used ISA and feature properties. */
193static unsigned int x86_used_note = DEFAULT_X86_USED_NOTE;
17d4e2a2 194#endif
252b5132 195
a847613f 196static const char *default_arch = DEFAULT_ARCH;
3e73aa7c 197
8a6fb3f9
JB
198/* parse_register() returns this when a register alias cannot be used. */
199static const reg_entry bad_reg = { "<bad>", OPERAND_TYPE_NONE, 0, 0,
200 { Dw2Inval, Dw2Inval } };
201
34684862 202static const reg_entry *reg_eax;
5e042380
JB
203static const reg_entry *reg_ds;
204static const reg_entry *reg_es;
205static const reg_entry *reg_ss;
6288d05f 206static const reg_entry *reg_st0;
6225c532
JB
207static const reg_entry *reg_k0;
208
c0f3af97
L
209/* VEX prefix. */
210typedef struct
211{
43234a1e
L
212 /* VEX prefix is either 2 byte or 3 byte. EVEX is 4 byte. */
213 unsigned char bytes[4];
c0f3af97
L
214 unsigned int length;
215 /* Destination or source register specifier. */
216 const reg_entry *register_specifier;
217} vex_prefix;
218
252b5132 219/* 'md_assemble ()' gathers together information and puts it into a
47926f60 220 i386_insn. */
252b5132 221
520dc8e8
AM
222union i386_op
223 {
224 expressionS *disps;
225 expressionS *imms;
226 const reg_entry *regs;
227 };
228
a65babc9
L
229enum i386_error
230 {
b4d65f2d 231 no_error, /* Must be first. */
86e026a4 232 operand_size_mismatch,
a65babc9
L
233 operand_type_mismatch,
234 register_type_mismatch,
235 number_of_operands_mismatch,
236 invalid_instruction_suffix,
237 bad_imm4,
a65babc9
L
238 unsupported_with_intel_mnemonic,
239 unsupported_syntax,
6c30d220 240 unsupported,
260cd341 241 invalid_sib_address,
6c30d220 242 invalid_vsib_address,
7bab8ab5 243 invalid_vector_register_set,
260cd341 244 invalid_tmm_register_set,
0cc78721 245 invalid_dest_and_src_register_set,
43234a1e
L
246 unsupported_vector_index_register,
247 unsupported_broadcast,
43234a1e
L
248 broadcast_needed,
249 unsupported_masking,
250 mask_not_on_destination,
251 no_default_mask,
252 unsupported_rc_sae,
43234a1e 253 invalid_register_operand,
a65babc9
L
254 };
255
252b5132
RH
256struct _i386_insn
257 {
47926f60 258 /* TM holds the template for the insn were currently assembling. */
d3ce72d0 259 insn_template tm;
252b5132 260
7d5e4556
L
261 /* SUFFIX holds the instruction size suffix for byte, word, dword
262 or qword, if given. */
252b5132
RH
263 char suffix;
264
9a182d04
JB
265 /* OPCODE_LENGTH holds the number of base opcode bytes. */
266 unsigned char opcode_length;
267
47926f60 268 /* OPERANDS gives the number of given operands. */
252b5132
RH
269 unsigned int operands;
270
271 /* REG_OPERANDS, DISP_OPERANDS, MEM_OPERANDS, IMM_OPERANDS give the number
272 of given register, displacement, memory operands and immediate
47926f60 273 operands. */
252b5132
RH
274 unsigned int reg_operands, disp_operands, mem_operands, imm_operands;
275
276 /* TYPES [i] is the type (see above #defines) which tells us how to
520dc8e8 277 use OP[i] for the corresponding operand. */
40fb9820 278 i386_operand_type types[MAX_OPERANDS];
252b5132 279
520dc8e8
AM
280 /* Displacement expression, immediate expression, or register for each
281 operand. */
282 union i386_op op[MAX_OPERANDS];
252b5132 283
3e73aa7c
JH
284 /* Flags for operands. */
285 unsigned int flags[MAX_OPERANDS];
286#define Operand_PCrel 1
c48dadc9 287#define Operand_Mem 2
3e73aa7c 288
252b5132 289 /* Relocation type for operand */
f86103b7 290 enum bfd_reloc_code_real reloc[MAX_OPERANDS];
252b5132 291
252b5132
RH
292 /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
293 the base index byte below. */
294 const reg_entry *base_reg;
295 const reg_entry *index_reg;
296 unsigned int log2_scale_factor;
297
298 /* SEG gives the seg_entries of this insn. They are zero unless
47926f60 299 explicit segment overrides are given. */
5e042380 300 const reg_entry *seg[2];
252b5132 301
8325cc63
JB
302 /* Copied first memory operand string, for re-checking. */
303 char *memop1_string;
304
252b5132
RH
305 /* PREFIX holds all the given prefix opcodes (usually null).
306 PREFIXES is the number of prefix opcodes. */
307 unsigned int prefixes;
308 unsigned char prefix[MAX_PREFIXES];
309
50128d0c 310 /* Register is in low 3 bits of opcode. */
5b7c81bd 311 bool short_form;
50128d0c 312
6f2f06be 313 /* The operand to a branch insn indicates an absolute branch. */
5b7c81bd 314 bool jumpabsolute;
6f2f06be 315
9373f275
L
316 /* There is a memory operand of (%dx) which should be only used
317 with input/output instructions. */
318 bool input_output_operand;
319
921eafea
L
320 /* Extended states. */
321 enum
322 {
323 /* Use MMX state. */
324 xstate_mmx = 1 << 0,
325 /* Use XMM state. */
326 xstate_xmm = 1 << 1,
327 /* Use YMM state. */
328 xstate_ymm = 1 << 2 | xstate_xmm,
329 /* Use ZMM state. */
330 xstate_zmm = 1 << 3 | xstate_ymm,
331 /* Use TMM state. */
32930e4e
L
332 xstate_tmm = 1 << 4,
333 /* Use MASK state. */
334 xstate_mask = 1 << 5
921eafea 335 } xstate;
260cd341 336
e379e5f3 337 /* Has GOTPC or TLS relocation. */
5b7c81bd 338 bool has_gotpc_tls_reloc;
e379e5f3 339
252b5132 340 /* RM and SIB are the modrm byte and the sib byte where the
c1e679ec 341 addressing modes of this insn are encoded. */
252b5132 342 modrm_byte rm;
3e73aa7c 343 rex_byte rex;
43234a1e 344 rex_byte vrex;
252b5132 345 sib_byte sib;
c0f3af97 346 vex_prefix vex;
b6169b20 347
6225c532
JB
348 /* Masking attributes.
349
350 The struct describes masking, applied to OPERAND in the instruction.
351 REG is a pointer to the corresponding mask register. ZEROING tells
352 whether merging or zeroing mask is used. */
353 struct Mask_Operation
354 {
355 const reg_entry *reg;
356 unsigned int zeroing;
357 /* The operand where this operation is associated. */
358 unsigned int operand;
359 } mask;
43234a1e
L
360
361 /* Rounding control and SAE attributes. */
ca5312a2
JB
362 struct RC_Operation
363 {
364 enum rc_type
365 {
366 rc_none = -1,
367 rne,
368 rd,
369 ru,
370 rz,
371 saeonly
372 } type;
7063667e
JB
373 /* In Intel syntax the operand modifier form is supposed to be used, but
374 we continue to accept the immediate forms as well. */
375 bool modifier;
ca5312a2 376 } rounding;
43234a1e 377
5273a3cd
JB
378 /* Broadcasting attributes.
379
380 The struct describes broadcasting, applied to OPERAND. TYPE is
381 expresses the broadcast factor. */
382 struct Broadcast_Operation
383 {
0cc78721 384 /* Type of broadcast: {1to2}, {1to4}, {1to8}, {1to16} or {1to32}. */
5273a3cd
JB
385 unsigned int type;
386
387 /* Index of broadcasted operand. */
388 unsigned int operand;
389
390 /* Number of bytes to broadcast. */
391 unsigned int bytes;
392 } broadcast;
43234a1e
L
393
394 /* Compressed disp8*N attribute. */
395 unsigned int memshift;
396
86fa6981
L
397 /* Prefer load or store in encoding. */
398 enum
399 {
400 dir_encoding_default = 0,
401 dir_encoding_load,
64c49ab3
JB
402 dir_encoding_store,
403 dir_encoding_swap
86fa6981 404 } dir_encoding;
891edac4 405
41eb8e88 406 /* Prefer 8bit, 16bit, 32bit displacement in encoding. */
a501d77e
L
407 enum
408 {
409 disp_encoding_default = 0,
410 disp_encoding_8bit,
41eb8e88 411 disp_encoding_16bit,
a501d77e
L
412 disp_encoding_32bit
413 } disp_encoding;
f8a5c266 414
6b6b6807 415 /* Prefer the REX byte in encoding. */
5b7c81bd 416 bool rex_encoding;
6b6b6807 417
b6f8c7c4 418 /* Disable instruction size optimization. */
5b7c81bd 419 bool no_optimize;
b6f8c7c4 420
86fa6981
L
421 /* How to encode vector instructions. */
422 enum
423 {
424 vex_encoding_default = 0,
42e04b36 425 vex_encoding_vex,
86fa6981 426 vex_encoding_vex3,
da4977e0
JB
427 vex_encoding_evex,
428 vex_encoding_error
86fa6981
L
429 } vec_encoding;
430
d5de92cf
L
431 /* REP prefix. */
432 const char *rep_prefix;
433
165de32a
L
434 /* HLE prefix. */
435 const char *hle_prefix;
42164a71 436
7e8b059b
L
437 /* Have BND prefix. */
438 const char *bnd_prefix;
439
04ef582a
L
440 /* Have NOTRACK prefix. */
441 const char *notrack_prefix;
442
891edac4 443 /* Error message. */
a65babc9 444 enum i386_error error;
252b5132
RH
445 };
446
447typedef struct _i386_insn i386_insn;
448
43234a1e
L
449/* Link RC type with corresponding string, that'll be looked for in
450 asm. */
451struct RC_name
452{
453 enum rc_type type;
454 const char *name;
455 unsigned int len;
456};
457
458static const struct RC_name RC_NamesTable[] =
459{
460 { rne, STRING_COMMA_LEN ("rn-sae") },
461 { rd, STRING_COMMA_LEN ("rd-sae") },
462 { ru, STRING_COMMA_LEN ("ru-sae") },
463 { rz, STRING_COMMA_LEN ("rz-sae") },
464 { saeonly, STRING_COMMA_LEN ("sae") },
465};
466
252b5132
RH
467/* List of chars besides those in app.c:symbol_chars that can start an
468 operand. Used to prevent the scrubber eating vital white-space. */
86fa6981 469const char extra_symbol_chars[] = "*%-([{}"
252b5132 470#ifdef LEX_AT
32137342
NC
471 "@"
472#endif
473#ifdef LEX_QM
474 "?"
252b5132 475#endif
32137342 476 ;
252b5132 477
b3983e5f
JB
478#if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
479 && !defined (TE_GNU) \
480 && !defined (TE_LINUX) \
d85e70a3 481 && !defined (TE_Haiku) \
b3983e5f
JB
482 && !defined (TE_FreeBSD) \
483 && !defined (TE_DragonFly) \
484 && !defined (TE_NetBSD))
252b5132 485/* This array holds the chars that always start a comment. If the
b3b91714
AM
486 pre-processor is disabled, these aren't very useful. The option
487 --divide will remove '/' from this list. */
488const char *i386_comment_chars = "#/";
489#define SVR4_COMMENT_CHARS 1
252b5132 490#define PREFIX_SEPARATOR '\\'
252b5132 491
b3b91714
AM
492#else
493const char *i386_comment_chars = "#";
494#define PREFIX_SEPARATOR '/'
495#endif
496
252b5132
RH
497/* This array holds the chars that only start a comment at the beginning of
498 a line. If the line seems to have the form '# 123 filename'
ce8a8b2f
AM
499 .line and .file directives will appear in the pre-processed output.
500 Note that input_file.c hand checks for '#' at the beginning of the
252b5132 501 first line of the input file. This is because the compiler outputs
ce8a8b2f
AM
502 #NO_APP at the beginning of its output.
503 Also note that comments started like this one will always work if
252b5132 504 '/' isn't otherwise defined. */
b3b91714 505const char line_comment_chars[] = "#/";
252b5132 506
63a0b638 507const char line_separator_chars[] = ";";
252b5132 508
ce8a8b2f
AM
509/* Chars that can be used to separate mant from exp in floating point
510 nums. */
252b5132
RH
511const char EXP_CHARS[] = "eE";
512
ce8a8b2f
AM
513/* Chars that mean this number is a floating point constant
514 As in 0f12.456
515 or 0d1.2345e12. */
de133cf9 516const char FLT_CHARS[] = "fFdDxXhHbB";
252b5132 517
ce8a8b2f 518/* Tables for lexical analysis. */
252b5132
RH
519static char mnemonic_chars[256];
520static char register_chars[256];
521static char operand_chars[256];
522static char identifier_chars[256];
252b5132 523
ce8a8b2f 524/* Lexical macros. */
252b5132
RH
525#define is_mnemonic_char(x) (mnemonic_chars[(unsigned char) x])
526#define is_operand_char(x) (operand_chars[(unsigned char) x])
527#define is_register_char(x) (register_chars[(unsigned char) x])
528#define is_space_char(x) ((x) == ' ')
529#define is_identifier_char(x) (identifier_chars[(unsigned char) x])
252b5132 530
0234cb7c 531/* All non-digit non-letter characters that may occur in an operand. */
252b5132
RH
532static char operand_special_chars[] = "%$-+(,)*._~/<>|&^!:[@]";
533
534/* md_assemble() always leaves the strings it's passed unaltered. To
535 effect this we maintain a stack of saved characters that we've smashed
536 with '\0's (indicating end of strings for various sub-fields of the
47926f60 537 assembler instruction). */
252b5132 538static char save_stack[32];
ce8a8b2f 539static char *save_stack_p;
252b5132
RH
540#define END_STRING_AND_SAVE(s) \
541 do { *save_stack_p++ = *(s); *(s) = '\0'; } while (0)
542#define RESTORE_END_STRING(s) \
543 do { *(s) = *--save_stack_p; } while (0)
544
47926f60 545/* The instruction we're assembling. */
252b5132
RH
546static i386_insn i;
547
548/* Possible templates for current insn. */
549static const templates *current_templates;
550
31b2323c
L
551/* Per instruction expressionS buffers: max displacements & immediates. */
552static expressionS disp_expressions[MAX_MEMORY_OPERANDS];
553static expressionS im_expressions[MAX_IMMEDIATE_OPERANDS];
252b5132 554
47926f60 555/* Current operand we are working on. */
ee86248c 556static int this_operand = -1;
252b5132 557
3e73aa7c
JH
558/* We support four different modes. FLAG_CODE variable is used to distinguish
559 these. */
560
561enum flag_code {
562 CODE_32BIT,
563 CODE_16BIT,
564 CODE_64BIT };
565
566static enum flag_code flag_code;
4fa24527 567static unsigned int object_64bit;
862be3fb 568static unsigned int disallow_64bit_reloc;
3e73aa7c 569static int use_rela_relocations = 0;
e379e5f3
L
570/* __tls_get_addr/___tls_get_addr symbol for TLS. */
571static const char *tls_get_addr;
3e73aa7c 572
7af8ed2d
NC
573#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
574 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
575 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
576
351f65ca
L
577/* The ELF ABI to use. */
578enum x86_elf_abi
579{
580 I386_ABI,
7f56bc95
L
581 X86_64_ABI,
582 X86_64_X32_ABI
351f65ca
L
583};
584
585static enum x86_elf_abi x86_elf_abi = I386_ABI;
7af8ed2d 586#endif
351f65ca 587
167ad85b
TG
588#if defined (TE_PE) || defined (TE_PEP)
589/* Use big object file format. */
590static int use_big_obj = 0;
591#endif
592
8dcea932
L
593#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
594/* 1 if generating code for a shared library. */
595static int shared = 0;
b52c4ee4
IB
596
597unsigned int x86_sframe_cfa_sp_reg;
598/* The other CFA base register for SFrame unwind info. */
599unsigned int x86_sframe_cfa_fp_reg;
600unsigned int x86_sframe_cfa_ra_reg;
601
8dcea932
L
602#endif
603
47926f60
KH
604/* 1 for intel syntax,
605 0 if att syntax. */
606static int intel_syntax = 0;
252b5132 607
4b5aaf5f
L
608static enum x86_64_isa
609{
610 amd64 = 1, /* AMD64 ISA. */
611 intel64 /* Intel64 ISA. */
612} isa64;
e89c5eaa 613
1efbbeb4
L
614/* 1 for intel mnemonic,
615 0 if att mnemonic. */
616static int intel_mnemonic = !SYSV386_COMPAT;
617
a60de03c
JB
618/* 1 if pseudo registers are permitted. */
619static int allow_pseudo_reg = 0;
620
47926f60
KH
621/* 1 if register prefix % not required. */
622static int allow_naked_reg = 0;
252b5132 623
33eaf5de 624/* 1 if the assembler should add BND prefix for all control-transferring
7e8b059b
L
625 instructions supporting it, even if this prefix wasn't specified
626 explicitly. */
627static int add_bnd_prefix = 0;
628
ba104c83 629/* 1 if pseudo index register, eiz/riz, is allowed . */
db51cc60
L
630static int allow_index_reg = 0;
631
d022bddd
IT
632/* 1 if the assembler should ignore LOCK prefix, even if it was
633 specified explicitly. */
634static int omit_lock_prefix = 0;
635
e4e00185
AS
636/* 1 if the assembler should encode lfence, mfence, and sfence as
637 "lock addl $0, (%{re}sp)". */
638static int avoid_fence = 0;
639
ae531041
L
640/* 1 if lfence should be inserted after every load. */
641static int lfence_after_load = 0;
642
643/* Non-zero if lfence should be inserted before indirect branch. */
644static enum lfence_before_indirect_branch_kind
645 {
646 lfence_branch_none = 0,
647 lfence_branch_register,
648 lfence_branch_memory,
649 lfence_branch_all
650 }
651lfence_before_indirect_branch;
652
653/* Non-zero if lfence should be inserted before ret. */
654static enum lfence_before_ret_kind
655 {
656 lfence_before_ret_none = 0,
657 lfence_before_ret_not,
a09f656b 658 lfence_before_ret_or,
659 lfence_before_ret_shl
ae531041
L
660 }
661lfence_before_ret;
662
663/* Types of previous instruction is .byte or prefix. */
e379e5f3
L
664static struct
665 {
666 segT seg;
667 const char *file;
668 const char *name;
669 unsigned int line;
670 enum last_insn_kind
671 {
672 last_insn_other = 0,
673 last_insn_directive,
674 last_insn_prefix
675 } kind;
676 } last_insn;
677
0cb4071e
L
678/* 1 if the assembler should generate relax relocations. */
679
680static int generate_relax_relocations
681 = DEFAULT_GENERATE_X86_RELAX_RELOCATIONS;
682
7bab8ab5 683static enum check_kind
daf50ae7 684 {
7bab8ab5
JB
685 check_none = 0,
686 check_warning,
687 check_error
daf50ae7 688 }
7bab8ab5 689sse_check, operand_check = check_warning;
daf50ae7 690
e379e5f3
L
691/* Non-zero if branches should be aligned within power of 2 boundary. */
692static int align_branch_power = 0;
693
694/* Types of branches to align. */
695enum align_branch_kind
696 {
697 align_branch_none = 0,
698 align_branch_jcc = 1,
699 align_branch_fused = 2,
700 align_branch_jmp = 3,
701 align_branch_call = 4,
702 align_branch_indirect = 5,
703 align_branch_ret = 6
704 };
705
706/* Type bits of branches to align. */
707enum align_branch_bit
708 {
709 align_branch_jcc_bit = 1 << align_branch_jcc,
710 align_branch_fused_bit = 1 << align_branch_fused,
711 align_branch_jmp_bit = 1 << align_branch_jmp,
712 align_branch_call_bit = 1 << align_branch_call,
713 align_branch_indirect_bit = 1 << align_branch_indirect,
714 align_branch_ret_bit = 1 << align_branch_ret
715 };
716
717static unsigned int align_branch = (align_branch_jcc_bit
718 | align_branch_fused_bit
719 | align_branch_jmp_bit);
720
79d72f45
HL
721/* Types of condition jump used by macro-fusion. */
722enum mf_jcc_kind
723 {
724 mf_jcc_jo = 0, /* base opcode 0x70 */
725 mf_jcc_jc, /* base opcode 0x72 */
726 mf_jcc_je, /* base opcode 0x74 */
727 mf_jcc_jna, /* base opcode 0x76 */
728 mf_jcc_js, /* base opcode 0x78 */
729 mf_jcc_jp, /* base opcode 0x7a */
730 mf_jcc_jl, /* base opcode 0x7c */
731 mf_jcc_jle, /* base opcode 0x7e */
732 };
733
734/* Types of compare flag-modifying insntructions used by macro-fusion. */
735enum mf_cmp_kind
736 {
737 mf_cmp_test_and, /* test/cmp */
738 mf_cmp_alu_cmp, /* add/sub/cmp */
739 mf_cmp_incdec /* inc/dec */
740 };
741
e379e5f3
L
742/* The maximum padding size for fused jcc. CMP like instruction can
743 be 9 bytes and jcc can be 6 bytes. Leave room just in case for
744 prefixes. */
745#define MAX_FUSED_JCC_PADDING_SIZE 20
746
747/* The maximum number of prefixes added for an instruction. */
748static unsigned int align_branch_prefix_size = 5;
749
b6f8c7c4
L
750/* Optimization:
751 1. Clear the REX_W bit with register operand if possible.
752 2. Above plus use 128bit vector instruction to clear the full vector
753 register.
754 */
755static int optimize = 0;
756
757/* Optimization:
758 1. Clear the REX_W bit with register operand if possible.
759 2. Above plus use 128bit vector instruction to clear the full vector
760 register.
761 3. Above plus optimize "test{q,l,w} $imm8,%r{64,32,16}" to
762 "testb $imm7,%r8".
763 */
764static int optimize_for_space = 0;
765
2ca3ace5
L
766/* Register prefix used for error message. */
767static const char *register_prefix = "%";
768
47926f60
KH
769/* Used in 16 bit gcc mode to add an l suffix to call, ret, enter,
770 leave, push, and pop instructions so that gcc has the same stack
771 frame as in 32 bit mode. */
772static char stackop_size = '\0';
eecb386c 773
12b55ccc
L
774/* Non-zero to optimize code alignment. */
775int optimize_align_code = 1;
776
47926f60
KH
777/* Non-zero to quieten some warnings. */
778static int quiet_warnings = 0;
a38cf1db 779
d59a54c2
JB
780/* Guard to avoid repeated warnings about non-16-bit code on 16-bit CPUs. */
781static bool pre_386_16bit_warned;
782
47926f60
KH
783/* CPU name. */
784static const char *cpu_arch_name = NULL;
6305a203 785static char *cpu_sub_arch_name = NULL;
a38cf1db 786
47926f60 787/* CPU feature flags. */
40fb9820
L
788static i386_cpu_flags cpu_arch_flags = CPU_UNKNOWN_FLAGS;
789
ccc9c027
L
790/* If we have selected a cpu we are generating instructions for. */
791static int cpu_arch_tune_set = 0;
792
9103f4f4 793/* Cpu we are generating instructions for. */
fbf3f584 794enum processor_type cpu_arch_tune = PROCESSOR_UNKNOWN;
9103f4f4
L
795
796/* CPU feature flags of cpu we are generating instructions for. */
40fb9820 797static i386_cpu_flags cpu_arch_tune_flags;
9103f4f4 798
ccc9c027 799/* CPU instruction set architecture used. */
fbf3f584 800enum processor_type cpu_arch_isa = PROCESSOR_UNKNOWN;
ccc9c027 801
9103f4f4 802/* CPU feature flags of instruction set architecture used. */
fbf3f584 803i386_cpu_flags cpu_arch_isa_flags;
9103f4f4 804
fddf5b5b
AM
805/* If set, conditional jumps are not automatically promoted to handle
806 larger than a byte offset. */
f68697e8 807static bool no_cond_jump_promotion = false;
fddf5b5b 808
c0f3af97
L
809/* Encode SSE instructions with VEX prefix. */
810static unsigned int sse2avx;
811
c8480b58
L
812/* Encode aligned vector move as unaligned vector move. */
813static unsigned int use_unaligned_vector_move;
814
539f890d
L
815/* Encode scalar AVX instructions with specific vector length. */
816static enum
817 {
818 vex128 = 0,
819 vex256
820 } avxscalar;
821
03751133
L
822/* Encode VEX WIG instructions with specific vex.w. */
823static enum
824 {
825 vexw0 = 0,
826 vexw1
827 } vexwig;
828
43234a1e
L
829/* Encode scalar EVEX LIG instructions with specific vector length. */
830static enum
831 {
832 evexl128 = 0,
833 evexl256,
834 evexl512
835 } evexlig;
836
837/* Encode EVEX WIG instructions with specific evex.w. */
838static enum
839 {
840 evexw0 = 0,
841 evexw1
842 } evexwig;
843
d3d3c6db
IT
844/* Value to encode in EVEX RC bits, for SAE-only instructions. */
845static enum rc_type evexrcig = rne;
846
29b0f896 847/* Pre-defined "_GLOBAL_OFFSET_TABLE_". */
87c245cc 848static symbolS *GOT_symbol;
29b0f896 849
a4447b93
RH
850/* The dwarf2 return column, adjusted for 32 or 64 bit. */
851unsigned int x86_dwarf2_return_column;
852
853/* The dwarf2 data alignment, adjusted for 32 or 64 bit. */
854int x86_cie_data_alignment;
855
252b5132 856/* Interface to relax_segment.
fddf5b5b
AM
857 There are 3 major relax states for 386 jump insns because the
858 different types of jumps add different sizes to frags when we're
e379e5f3
L
859 figuring out what sort of jump to choose to reach a given label.
860
861 BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING are used to align
862 branches which are handled by md_estimate_size_before_relax() and
863 i386_generic_table_relax_frag(). */
252b5132 864
47926f60 865/* Types. */
93c2a809
AM
866#define UNCOND_JUMP 0
867#define COND_JUMP 1
868#define COND_JUMP86 2
e379e5f3
L
869#define BRANCH_PADDING 3
870#define BRANCH_PREFIX 4
871#define FUSED_JCC_PADDING 5
fddf5b5b 872
47926f60 873/* Sizes. */
252b5132
RH
874#define CODE16 1
875#define SMALL 0
29b0f896 876#define SMALL16 (SMALL | CODE16)
252b5132 877#define BIG 2
29b0f896 878#define BIG16 (BIG | CODE16)
252b5132
RH
879
880#ifndef INLINE
881#ifdef __GNUC__
882#define INLINE __inline__
883#else
884#define INLINE
885#endif
886#endif
887
fddf5b5b
AM
888#define ENCODE_RELAX_STATE(type, size) \
889 ((relax_substateT) (((type) << 2) | (size)))
890#define TYPE_FROM_RELAX_STATE(s) \
891 ((s) >> 2)
892#define DISP_SIZE_FROM_RELAX_STATE(s) \
893 ((((s) & 3) == BIG ? 4 : (((s) & 3) == BIG16 ? 2 : 1)))
252b5132
RH
894
895/* This table is used by relax_frag to promote short jumps to long
896 ones where necessary. SMALL (short) jumps may be promoted to BIG
897 (32 bit long) ones, and SMALL16 jumps to BIG16 (16 bit long). We
898 don't allow a short jump in a 32 bit code segment to be promoted to
899 a 16 bit offset jump because it's slower (requires data size
900 prefix), and doesn't work, unless the destination is in the bottom
901 64k of the code segment (The top 16 bits of eip are zeroed). */
902
903const relax_typeS md_relax_table[] =
904{
24eab124
AM
905 /* The fields are:
906 1) most positive reach of this state,
907 2) most negative reach of this state,
93c2a809 908 3) how many bytes this mode will have in the variable part of the frag
ce8a8b2f 909 4) which index into the table to try if we can't fit into this one. */
252b5132 910
fddf5b5b 911 /* UNCOND_JUMP states. */
93c2a809
AM
912 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG)},
913 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16)},
914 /* dword jmp adds 4 bytes to frag:
915 0 extra opcode bytes, 4 displacement bytes. */
252b5132 916 {0, 0, 4, 0},
93c2a809
AM
917 /* word jmp adds 2 byte2 to frag:
918 0 extra opcode bytes, 2 displacement bytes. */
252b5132
RH
919 {0, 0, 2, 0},
920
93c2a809
AM
921 /* COND_JUMP states. */
922 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG)},
923 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG16)},
924 /* dword conditionals adds 5 bytes to frag:
925 1 extra opcode byte, 4 displacement bytes. */
926 {0, 0, 5, 0},
fddf5b5b 927 /* word conditionals add 3 bytes to frag:
93c2a809
AM
928 1 extra opcode byte, 2 displacement bytes. */
929 {0, 0, 3, 0},
930
931 /* COND_JUMP86 states. */
932 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG)},
933 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG16)},
934 /* dword conditionals adds 5 bytes to frag:
935 1 extra opcode byte, 4 displacement bytes. */
936 {0, 0, 5, 0},
937 /* word conditionals add 4 bytes to frag:
938 1 displacement byte and a 3 byte long branch insn. */
939 {0, 0, 4, 0}
252b5132
RH
940};
941
6ceeed25 942#define ARCH(n, t, f, s) \
ae89daec
JB
943 { STRING_COMMA_LEN (#n), s, PROCESSOR_ ## t, CPU_ ## f ## _FLAGS, \
944 CPU_NONE_FLAGS }
945#define SUBARCH(n, e, d, s) \
946 { STRING_COMMA_LEN (#n), s, PROCESSOR_NONE, CPU_ ## e ## _FLAGS, \
947 CPU_ ## d ## _FLAGS }
6ceeed25 948
9103f4f4
L
949static const arch_entry cpu_arch[] =
950{
3ce2ebcf
JB
951 /* Do not replace the first two entries - i386_target_format() and
952 set_cpu_arch() rely on them being there in this order. */
6ceeed25
JB
953 ARCH (generic32, GENERIC32, GENERIC32, false),
954 ARCH (generic64, GENERIC64, GENERIC64, false),
955 ARCH (i8086, UNKNOWN, NONE, false),
956 ARCH (i186, UNKNOWN, I186, false),
957 ARCH (i286, UNKNOWN, I286, false),
958 ARCH (i386, I386, I386, false),
959 ARCH (i486, I486, I486, false),
960 ARCH (i586, PENTIUM, I586, false),
961 ARCH (i686, PENTIUMPRO, I686, false),
962 ARCH (pentium, PENTIUM, I586, false),
963 ARCH (pentiumpro, PENTIUMPRO, PENTIUMPRO, false),
964 ARCH (pentiumii, PENTIUMPRO, P2, false),
965 ARCH (pentiumiii, PENTIUMPRO, P3, false),
966 ARCH (pentium4, PENTIUM4, P4, false),
967 ARCH (prescott, NOCONA, CORE, false),
968 ARCH (nocona, NOCONA, NOCONA, false),
969 ARCH (yonah, CORE, CORE, true),
970 ARCH (core, CORE, CORE, false),
971 ARCH (merom, CORE2, CORE2, true),
972 ARCH (core2, CORE2, CORE2, false),
973 ARCH (corei7, COREI7, COREI7, false),
974 ARCH (iamcu, IAMCU, IAMCU, false),
975 ARCH (k6, K6, K6, false),
976 ARCH (k6_2, K6, K6_2, false),
977 ARCH (athlon, ATHLON, ATHLON, false),
978 ARCH (sledgehammer, K8, K8, true),
979 ARCH (opteron, K8, K8, false),
980 ARCH (k8, K8, K8, false),
981 ARCH (amdfam10, AMDFAM10, AMDFAM10, false),
982 ARCH (bdver1, BD, BDVER1, false),
983 ARCH (bdver2, BD, BDVER2, false),
984 ARCH (bdver3, BD, BDVER3, false),
985 ARCH (bdver4, BD, BDVER4, false),
986 ARCH (znver1, ZNVER, ZNVER1, false),
987 ARCH (znver2, ZNVER, ZNVER2, false),
988 ARCH (znver3, ZNVER, ZNVER3, false),
b0e8fa7f 989 ARCH (znver4, ZNVER, ZNVER4, false),
6ceeed25
JB
990 ARCH (btver1, BT, BTVER1, false),
991 ARCH (btver2, BT, BTVER2, false),
992
ae89daec
JB
993 SUBARCH (8087, 8087, ANY_X87, false),
994 SUBARCH (87, NONE, ANY_X87, false), /* Disable only! */
995 SUBARCH (287, 287, ANY_287, false),
996 SUBARCH (387, 387, ANY_387, false),
997 SUBARCH (687, 687, ANY_687, false),
998 SUBARCH (cmov, CMOV, ANY_CMOV, false),
999 SUBARCH (fxsr, FXSR, ANY_FXSR, false),
1000 SUBARCH (mmx, MMX, ANY_MMX, false),
1001 SUBARCH (sse, SSE, ANY_SSE, false),
1002 SUBARCH (sse2, SSE2, ANY_SSE2, false),
1003 SUBARCH (sse3, SSE3, ANY_SSE3, false),
1004 SUBARCH (sse4a, SSE4A, ANY_SSE4A, false),
1005 SUBARCH (ssse3, SSSE3, ANY_SSSE3, false),
1006 SUBARCH (sse4.1, SSE4_1, ANY_SSE4_1, false),
1007 SUBARCH (sse4.2, SSE4_2, ANY_SSE4_2, false),
1008 SUBARCH (sse4, SSE4_2, ANY_SSE4_1, false),
1009 SUBARCH (avx, AVX, ANY_AVX, false),
1010 SUBARCH (avx2, AVX2, ANY_AVX2, false),
1011 SUBARCH (avx512f, AVX512F, ANY_AVX512F, false),
1012 SUBARCH (avx512cd, AVX512CD, ANY_AVX512CD, false),
1013 SUBARCH (avx512er, AVX512ER, ANY_AVX512ER, false),
1014 SUBARCH (avx512pf, AVX512PF, ANY_AVX512PF, false),
1015 SUBARCH (avx512dq, AVX512DQ, ANY_AVX512DQ, false),
1016 SUBARCH (avx512bw, AVX512BW, ANY_AVX512BW, false),
1017 SUBARCH (avx512vl, AVX512VL, ANY_AVX512VL, false),
1018 SUBARCH (vmx, VMX, VMX, false),
1019 SUBARCH (vmfunc, VMFUNC, VMFUNC, false),
1020 SUBARCH (smx, SMX, SMX, false),
1021 SUBARCH (xsave, XSAVE, XSAVE, false),
1022 SUBARCH (xsaveopt, XSAVEOPT, XSAVEOPT, false),
1023 SUBARCH (xsavec, XSAVEC, XSAVEC, false),
1024 SUBARCH (xsaves, XSAVES, XSAVES, false),
1025 SUBARCH (aes, AES, AES, false),
1026 SUBARCH (pclmul, PCLMUL, PCLMUL, false),
1027 SUBARCH (clmul, PCLMUL, PCLMUL, true),
1028 SUBARCH (fsgsbase, FSGSBASE, FSGSBASE, false),
1029 SUBARCH (rdrnd, RDRND, RDRND, false),
1030 SUBARCH (f16c, F16C, F16C, false),
1031 SUBARCH (bmi2, BMI2, BMI2, false),
1032 SUBARCH (fma, FMA, FMA, false),
1033 SUBARCH (fma4, FMA4, FMA4, false),
1034 SUBARCH (xop, XOP, XOP, false),
1035 SUBARCH (lwp, LWP, LWP, false),
1036 SUBARCH (movbe, MOVBE, MOVBE, false),
1037 SUBARCH (cx16, CX16, CX16, false),
1038 SUBARCH (ept, EPT, EPT, false),
1039 SUBARCH (lzcnt, LZCNT, LZCNT, false),
1040 SUBARCH (popcnt, POPCNT, POPCNT, false),
1041 SUBARCH (hle, HLE, HLE, false),
1042 SUBARCH (rtm, RTM, RTM, false),
1043 SUBARCH (invpcid, INVPCID, INVPCID, false),
1044 SUBARCH (clflush, CLFLUSH, CLFLUSH, false),
1045 SUBARCH (nop, NOP, NOP, false),
1046 SUBARCH (syscall, SYSCALL, SYSCALL, false),
1047 SUBARCH (rdtscp, RDTSCP, RDTSCP, false),
1048 SUBARCH (3dnow, 3DNOW, 3DNOW, false),
1049 SUBARCH (3dnowa, 3DNOWA, 3DNOWA, false),
1050 SUBARCH (padlock, PADLOCK, PADLOCK, false),
1051 SUBARCH (pacifica, SVME, SVME, true),
1052 SUBARCH (svme, SVME, SVME, false),
ae89daec
JB
1053 SUBARCH (abm, ABM, ABM, false),
1054 SUBARCH (bmi, BMI, BMI, false),
1055 SUBARCH (tbm, TBM, TBM, false),
1056 SUBARCH (adx, ADX, ADX, false),
1057 SUBARCH (rdseed, RDSEED, RDSEED, false),
1058 SUBARCH (prfchw, PRFCHW, PRFCHW, false),
1059 SUBARCH (smap, SMAP, SMAP, false),
1060 SUBARCH (mpx, MPX, MPX, false),
1061 SUBARCH (sha, SHA, SHA, false),
1062 SUBARCH (clflushopt, CLFLUSHOPT, CLFLUSHOPT, false),
1063 SUBARCH (prefetchwt1, PREFETCHWT1, PREFETCHWT1, false),
1064 SUBARCH (se1, SE1, SE1, false),
1065 SUBARCH (clwb, CLWB, CLWB, false),
1066 SUBARCH (avx512ifma, AVX512IFMA, ANY_AVX512IFMA, false),
1067 SUBARCH (avx512vbmi, AVX512VBMI, ANY_AVX512VBMI, false),
1068 SUBARCH (avx512_4fmaps, AVX512_4FMAPS, ANY_AVX512_4FMAPS, false),
1069 SUBARCH (avx512_4vnniw, AVX512_4VNNIW, ANY_AVX512_4VNNIW, false),
1070 SUBARCH (avx512_vpopcntdq, AVX512_VPOPCNTDQ, ANY_AVX512_VPOPCNTDQ, false),
1071 SUBARCH (avx512_vbmi2, AVX512_VBMI2, ANY_AVX512_VBMI2, false),
1072 SUBARCH (avx512_vnni, AVX512_VNNI, ANY_AVX512_VNNI, false),
1073 SUBARCH (avx512_bitalg, AVX512_BITALG, ANY_AVX512_BITALG, false),
1074 SUBARCH (avx_vnni, AVX_VNNI, ANY_AVX_VNNI, false),
1075 SUBARCH (clzero, CLZERO, CLZERO, false),
1076 SUBARCH (mwaitx, MWAITX, MWAITX, false),
1077 SUBARCH (ospke, OSPKE, OSPKE, false),
1078 SUBARCH (rdpid, RDPID, RDPID, false),
1079 SUBARCH (ptwrite, PTWRITE, PTWRITE, false),
1080 SUBARCH (ibt, IBT, ANY_IBT, false),
1081 SUBARCH (shstk, SHSTK, ANY_SHSTK, false),
1082 SUBARCH (gfni, GFNI, GFNI, false),
1083 SUBARCH (vaes, VAES, VAES, false),
1084 SUBARCH (vpclmulqdq, VPCLMULQDQ, VPCLMULQDQ, false),
1085 SUBARCH (wbnoinvd, WBNOINVD, WBNOINVD, false),
1086 SUBARCH (pconfig, PCONFIG, PCONFIG, false),
1087 SUBARCH (waitpkg, WAITPKG, WAITPKG, false),
1088 SUBARCH (cldemote, CLDEMOTE, CLDEMOTE, false),
1089 SUBARCH (amx_int8, AMX_INT8, ANY_AMX_INT8, false),
1090 SUBARCH (amx_bf16, AMX_BF16, ANY_AMX_BF16, false),
68830fba 1091 SUBARCH (amx_fp16, AMX_FP16, AMX_FP16, false),
ae89daec
JB
1092 SUBARCH (amx_tile, AMX_TILE, ANY_AMX_TILE, false),
1093 SUBARCH (movdiri, MOVDIRI, ANY_MOVDIRI, false),
1094 SUBARCH (movdir64b, MOVDIR64B, ANY_MOVDIR64B, false),
1095 SUBARCH (avx512_bf16, AVX512_BF16, ANY_AVX512_BF16, false),
1096 SUBARCH (avx512_vp2intersect, AVX512_VP2INTERSECT,
1097 ANY_AVX512_VP2INTERSECT, false),
1098 SUBARCH (tdx, TDX, ANY_TDX, false),
1099 SUBARCH (enqcmd, ENQCMD, ANY_ENQCMD, false),
1100 SUBARCH (serialize, SERIALIZE, ANY_SERIALIZE, false),
1101 SUBARCH (rdpru, RDPRU, RDPRU, false),
1102 SUBARCH (mcommit, MCOMMIT, MCOMMIT, false),
1103 SUBARCH (sev_es, SEV_ES, SEV_ES, false),
1104 SUBARCH (tsxldtrk, TSXLDTRK, ANY_TSXLDTRK, false),
1105 SUBARCH (kl, KL, ANY_KL, false),
1106 SUBARCH (widekl, WIDEKL, ANY_WIDEKL, false),
1107 SUBARCH (uintr, UINTR, ANY_UINTR, false),
1108 SUBARCH (hreset, HRESET, ANY_HRESET, false),
1109 SUBARCH (avx512_fp16, AVX512_FP16, ANY_AVX512_FP16, false),
ef07be45 1110 SUBARCH (prefetchi, PREFETCHI, PREFETCHI, false),
4321af3e 1111 SUBARCH (avx_ifma, AVX_IFMA, ANY_AVX_IFMA, false),
23ae61ad 1112 SUBARCH (avx_vnni_int8, AVX_VNNI_INT8, ANY_AVX_VNNI_INT8, false),
a93e3234 1113 SUBARCH (cmpccxadd, CMPCCXADD, ANY_CMPCCXADD, false),
941f0833 1114 SUBARCH (wrmsrns, WRMSRNS, ANY_WRMSRNS, false),
2188d6ea 1115 SUBARCH (msrlist, MSRLIST, ANY_MSRLIST, false),
01d8ce74 1116 SUBARCH (avx_ne_convert, AVX_NE_CONVERT, ANY_AVX_NE_CONVERT, false),
b06311ad 1117 SUBARCH (rao_int, RAO_INT, ANY_RAO_INT, false),
b0e8fa7f 1118 SUBARCH (rmpquery, RMPQUERY, RMPQUERY, false),
293f5f65
L
1119};
1120
6ceeed25
JB
1121#undef SUBARCH
1122#undef ARCH
1123
704209c0 1124#ifdef I386COFF
a6c24e68
NC
1125/* Like s_lcomm_internal in gas/read.c but the alignment string
1126 is allowed to be optional. */
1127
1128static symbolS *
1129pe_lcomm_internal (int needs_align, symbolS *symbolP, addressT size)
1130{
1131 addressT align = 0;
1132
1133 SKIP_WHITESPACE ();
1134
7ab9ffdd 1135 if (needs_align
a6c24e68
NC
1136 && *input_line_pointer == ',')
1137 {
1138 align = parse_align (needs_align - 1);
7ab9ffdd 1139
a6c24e68
NC
1140 if (align == (addressT) -1)
1141 return NULL;
1142 }
1143 else
1144 {
1145 if (size >= 8)
1146 align = 3;
1147 else if (size >= 4)
1148 align = 2;
1149 else if (size >= 2)
1150 align = 1;
1151 else
1152 align = 0;
1153 }
1154
1155 bss_alloc (symbolP, size, align);
1156 return symbolP;
1157}
1158
704209c0 1159static void
a6c24e68
NC
1160pe_lcomm (int needs_align)
1161{
1162 s_comm_internal (needs_align * 2, pe_lcomm_internal);
1163}
704209c0 1164#endif
a6c24e68 1165
29b0f896
AM
1166const pseudo_typeS md_pseudo_table[] =
1167{
1168#if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
1169 {"align", s_align_bytes, 0},
1170#else
1171 {"align", s_align_ptwo, 0},
1172#endif
1173 {"arch", set_cpu_arch, 0},
1174#ifndef I386COFF
1175 {"bss", s_bss, 0},
a6c24e68
NC
1176#else
1177 {"lcomm", pe_lcomm, 1},
29b0f896
AM
1178#endif
1179 {"ffloat", float_cons, 'f'},
1180 {"dfloat", float_cons, 'd'},
1181 {"tfloat", float_cons, 'x'},
7d19d096 1182 {"hfloat", float_cons, 'h'},
de133cf9 1183 {"bfloat16", float_cons, 'b'},
29b0f896 1184 {"value", cons, 2},
d182319b 1185 {"slong", signed_cons, 4},
29b0f896
AM
1186 {"noopt", s_ignore, 0},
1187 {"optim", s_ignore, 0},
1188 {"code16gcc", set_16bit_gcc_code_flag, CODE_16BIT},
1189 {"code16", set_code_flag, CODE_16BIT},
1190 {"code32", set_code_flag, CODE_32BIT},
da5f19a2 1191#ifdef BFD64
29b0f896 1192 {"code64", set_code_flag, CODE_64BIT},
da5f19a2 1193#endif
29b0f896
AM
1194 {"intel_syntax", set_intel_syntax, 1},
1195 {"att_syntax", set_intel_syntax, 0},
1efbbeb4
L
1196 {"intel_mnemonic", set_intel_mnemonic, 1},
1197 {"att_mnemonic", set_intel_mnemonic, 0},
db51cc60
L
1198 {"allow_index_reg", set_allow_index_reg, 1},
1199 {"disallow_index_reg", set_allow_index_reg, 0},
7bab8ab5
JB
1200 {"sse_check", set_check, 0},
1201 {"operand_check", set_check, 1},
3b22753a
L
1202#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1203 {"largecomm", handle_large_common, 0},
07a53e5c 1204#else
68d20676 1205 {"file", dwarf2_directive_file, 0},
07a53e5c
RH
1206 {"loc", dwarf2_directive_loc, 0},
1207 {"loc_mark_labels", dwarf2_directive_loc_mark_labels, 0},
3b22753a 1208#endif
6482c264
NC
1209#ifdef TE_PE
1210 {"secrel32", pe_directive_secrel, 0},
145667f8 1211 {"secidx", pe_directive_secidx, 0},
6482c264 1212#endif
29b0f896
AM
1213 {0, 0, 0}
1214};
1215
1216/* For interface with expression (). */
1217extern char *input_line_pointer;
1218
1219/* Hash table for instruction mnemonic lookup. */
629310ab 1220static htab_t op_hash;
29b0f896
AM
1221
1222/* Hash table for register lookup. */
629310ab 1223static htab_t reg_hash;
29b0f896 1224\f
ce8a8b2f
AM
1225 /* Various efficient no-op patterns for aligning code labels.
1226 Note: Don't try to assemble the instructions in the comments.
1227 0L and 0w are not legal. */
62a02d25
L
1228static const unsigned char f32_1[] =
1229 {0x90}; /* nop */
1230static const unsigned char f32_2[] =
1231 {0x66,0x90}; /* xchg %ax,%ax */
1232static const unsigned char f32_3[] =
1233 {0x8d,0x76,0x00}; /* leal 0(%esi),%esi */
1234static const unsigned char f32_4[] =
1235 {0x8d,0x74,0x26,0x00}; /* leal 0(%esi,1),%esi */
62a02d25
L
1236static const unsigned char f32_6[] =
1237 {0x8d,0xb6,0x00,0x00,0x00,0x00}; /* leal 0L(%esi),%esi */
1238static const unsigned char f32_7[] =
1239 {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal 0L(%esi,1),%esi */
62a02d25 1240static const unsigned char f16_3[] =
3ae729d5 1241 {0x8d,0x74,0x00}; /* lea 0(%si),%si */
62a02d25 1242static const unsigned char f16_4[] =
3ae729d5
L
1243 {0x8d,0xb4,0x00,0x00}; /* lea 0W(%si),%si */
1244static const unsigned char jump_disp8[] =
1245 {0xeb}; /* jmp disp8 */
1246static const unsigned char jump32_disp32[] =
1247 {0xe9}; /* jmp disp32 */
1248static const unsigned char jump16_disp32[] =
1249 {0x66,0xe9}; /* jmp disp32 */
62a02d25
L
1250/* 32-bit NOPs patterns. */
1251static const unsigned char *const f32_patt[] = {
3ae729d5 1252 f32_1, f32_2, f32_3, f32_4, NULL, f32_6, f32_7
62a02d25
L
1253};
1254/* 16-bit NOPs patterns. */
1255static const unsigned char *const f16_patt[] = {
3ae729d5 1256 f32_1, f32_2, f16_3, f16_4
62a02d25
L
1257};
1258/* nopl (%[re]ax) */
1259static const unsigned char alt_3[] =
1260 {0x0f,0x1f,0x00};
1261/* nopl 0(%[re]ax) */
1262static const unsigned char alt_4[] =
1263 {0x0f,0x1f,0x40,0x00};
1264/* nopl 0(%[re]ax,%[re]ax,1) */
1265static const unsigned char alt_5[] =
1266 {0x0f,0x1f,0x44,0x00,0x00};
1267/* nopw 0(%[re]ax,%[re]ax,1) */
1268static const unsigned char alt_6[] =
1269 {0x66,0x0f,0x1f,0x44,0x00,0x00};
1270/* nopl 0L(%[re]ax) */
1271static const unsigned char alt_7[] =
1272 {0x0f,0x1f,0x80,0x00,0x00,0x00,0x00};
1273/* nopl 0L(%[re]ax,%[re]ax,1) */
1274static const unsigned char alt_8[] =
1275 {0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1276/* nopw 0L(%[re]ax,%[re]ax,1) */
1277static const unsigned char alt_9[] =
1278 {0x66,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1279/* nopw %cs:0L(%[re]ax,%[re]ax,1) */
1280static const unsigned char alt_10[] =
1281 {0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
3ae729d5
L
1282/* data16 nopw %cs:0L(%eax,%eax,1) */
1283static const unsigned char alt_11[] =
1284 {0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
62a02d25
L
1285/* 32-bit and 64-bit NOPs patterns. */
1286static const unsigned char *const alt_patt[] = {
1287 f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
3ae729d5 1288 alt_9, alt_10, alt_11
62a02d25
L
1289};
1290
1291/* Genenerate COUNT bytes of NOPs to WHERE from PATT with the maximum
1292 size of a single NOP instruction MAX_SINGLE_NOP_SIZE. */
1293
1294static void
1295i386_output_nops (char *where, const unsigned char *const *patt,
1296 int count, int max_single_nop_size)
1297
1298{
3ae729d5
L
1299 /* Place the longer NOP first. */
1300 int last;
1301 int offset;
3076e594
NC
1302 const unsigned char *nops;
1303
1304 if (max_single_nop_size < 1)
1305 {
1306 as_fatal (_("i386_output_nops called to generate nops of at most %d bytes!"),
1307 max_single_nop_size);
1308 return;
1309 }
1310
1311 nops = patt[max_single_nop_size - 1];
3ae729d5
L
1312
1313 /* Use the smaller one if the requsted one isn't available. */
1314 if (nops == NULL)
62a02d25 1315 {
3ae729d5
L
1316 max_single_nop_size--;
1317 nops = patt[max_single_nop_size - 1];
62a02d25
L
1318 }
1319
3ae729d5
L
1320 last = count % max_single_nop_size;
1321
1322 count -= last;
1323 for (offset = 0; offset < count; offset += max_single_nop_size)
1324 memcpy (where + offset, nops, max_single_nop_size);
1325
1326 if (last)
1327 {
1328 nops = patt[last - 1];
1329 if (nops == NULL)
1330 {
1331 /* Use the smaller one plus one-byte NOP if the needed one
1332 isn't available. */
1333 last--;
1334 nops = patt[last - 1];
1335 memcpy (where + offset, nops, last);
1336 where[offset + last] = *patt[0];
1337 }
1338 else
1339 memcpy (where + offset, nops, last);
1340 }
62a02d25
L
1341}
1342
3ae729d5
L
1343static INLINE int
1344fits_in_imm7 (offsetT num)
1345{
1346 return (num & 0x7f) == num;
1347}
1348
1349static INLINE int
1350fits_in_imm31 (offsetT num)
1351{
1352 return (num & 0x7fffffff) == num;
1353}
62a02d25
L
1354
1355/* Genenerate COUNT bytes of NOPs to WHERE with the maximum size of a
1356 single NOP instruction LIMIT. */
1357
1358void
3ae729d5 1359i386_generate_nops (fragS *fragP, char *where, offsetT count, int limit)
62a02d25 1360{
3ae729d5 1361 const unsigned char *const *patt = NULL;
62a02d25 1362 int max_single_nop_size;
3ae729d5
L
1363 /* Maximum number of NOPs before switching to jump over NOPs. */
1364 int max_number_of_nops;
62a02d25 1365
3ae729d5 1366 switch (fragP->fr_type)
62a02d25 1367 {
3ae729d5
L
1368 case rs_fill_nop:
1369 case rs_align_code:
1370 break;
e379e5f3
L
1371 case rs_machine_dependent:
1372 /* Allow NOP padding for jumps and calls. */
1373 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
1374 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
1375 break;
1376 /* Fall through. */
3ae729d5 1377 default:
62a02d25
L
1378 return;
1379 }
1380
ccc9c027
L
1381 /* We need to decide which NOP sequence to use for 32bit and
1382 64bit. When -mtune= is used:
4eed87de 1383
76bc74dc
L
1384 1. For PROCESSOR_I386, PROCESSOR_I486, PROCESSOR_PENTIUM and
1385 PROCESSOR_GENERIC32, f32_patt will be used.
80b8656c
L
1386 2. For the rest, alt_patt will be used.
1387
1388 When -mtune= isn't used, alt_patt will be used if
22109423 1389 cpu_arch_isa_flags has CpuNop. Otherwise, f32_patt will
76bc74dc 1390 be used.
ccc9c027
L
1391
1392 When -march= or .arch is used, we can't use anything beyond
1393 cpu_arch_isa_flags. */
1394
1395 if (flag_code == CODE_16BIT)
1396 {
3ae729d5
L
1397 patt = f16_patt;
1398 max_single_nop_size = sizeof (f16_patt) / sizeof (f16_patt[0]);
1399 /* Limit number of NOPs to 2 in 16-bit mode. */
1400 max_number_of_nops = 2;
252b5132 1401 }
33fef721 1402 else
ccc9c027 1403 {
fbf3f584 1404 if (fragP->tc_frag_data.isa == PROCESSOR_UNKNOWN)
ccc9c027
L
1405 {
1406 /* PROCESSOR_UNKNOWN means that all ISAs may be used. */
1407 switch (cpu_arch_tune)
1408 {
1409 case PROCESSOR_UNKNOWN:
1410 /* We use cpu_arch_isa_flags to check if we SHOULD
22109423
L
1411 optimize with nops. */
1412 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1413 patt = alt_patt;
ccc9c027
L
1414 else
1415 patt = f32_patt;
1416 break;
ccc9c027
L
1417 case PROCESSOR_PENTIUM4:
1418 case PROCESSOR_NOCONA:
ef05d495 1419 case PROCESSOR_CORE:
76bc74dc 1420 case PROCESSOR_CORE2:
bd5295b2 1421 case PROCESSOR_COREI7:
76bc74dc 1422 case PROCESSOR_GENERIC64:
ccc9c027
L
1423 case PROCESSOR_K6:
1424 case PROCESSOR_ATHLON:
1425 case PROCESSOR_K8:
4eed87de 1426 case PROCESSOR_AMDFAM10:
8aedb9fe 1427 case PROCESSOR_BD:
029f3522 1428 case PROCESSOR_ZNVER:
7b458c12 1429 case PROCESSOR_BT:
80b8656c 1430 patt = alt_patt;
ccc9c027 1431 break;
76bc74dc 1432 case PROCESSOR_I386:
ccc9c027
L
1433 case PROCESSOR_I486:
1434 case PROCESSOR_PENTIUM:
2dde1948 1435 case PROCESSOR_PENTIUMPRO:
81486035 1436 case PROCESSOR_IAMCU:
ccc9c027
L
1437 case PROCESSOR_GENERIC32:
1438 patt = f32_patt;
1439 break;
c368d2a8
JB
1440 case PROCESSOR_NONE:
1441 abort ();
4eed87de 1442 }
ccc9c027
L
1443 }
1444 else
1445 {
fbf3f584 1446 switch (fragP->tc_frag_data.tune)
ccc9c027
L
1447 {
1448 case PROCESSOR_UNKNOWN:
e6a14101 1449 /* When cpu_arch_isa is set, cpu_arch_tune shouldn't be
ccc9c027
L
1450 PROCESSOR_UNKNOWN. */
1451 abort ();
1452 break;
1453
76bc74dc 1454 case PROCESSOR_I386:
ccc9c027
L
1455 case PROCESSOR_I486:
1456 case PROCESSOR_PENTIUM:
81486035 1457 case PROCESSOR_IAMCU:
ccc9c027
L
1458 case PROCESSOR_K6:
1459 case PROCESSOR_ATHLON:
1460 case PROCESSOR_K8:
4eed87de 1461 case PROCESSOR_AMDFAM10:
8aedb9fe 1462 case PROCESSOR_BD:
029f3522 1463 case PROCESSOR_ZNVER:
7b458c12 1464 case PROCESSOR_BT:
ccc9c027
L
1465 case PROCESSOR_GENERIC32:
1466 /* We use cpu_arch_isa_flags to check if we CAN optimize
22109423
L
1467 with nops. */
1468 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1469 patt = alt_patt;
ccc9c027
L
1470 else
1471 patt = f32_patt;
1472 break;
76bc74dc
L
1473 case PROCESSOR_PENTIUMPRO:
1474 case PROCESSOR_PENTIUM4:
1475 case PROCESSOR_NOCONA:
1476 case PROCESSOR_CORE:
ef05d495 1477 case PROCESSOR_CORE2:
bd5295b2 1478 case PROCESSOR_COREI7:
22109423 1479 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1480 patt = alt_patt;
ccc9c027
L
1481 else
1482 patt = f32_patt;
1483 break;
1484 case PROCESSOR_GENERIC64:
80b8656c 1485 patt = alt_patt;
ccc9c027 1486 break;
c368d2a8
JB
1487 case PROCESSOR_NONE:
1488 abort ();
4eed87de 1489 }
ccc9c027
L
1490 }
1491
76bc74dc
L
1492 if (patt == f32_patt)
1493 {
3ae729d5
L
1494 max_single_nop_size = sizeof (f32_patt) / sizeof (f32_patt[0]);
1495 /* Limit number of NOPs to 2 for older processors. */
1496 max_number_of_nops = 2;
76bc74dc
L
1497 }
1498 else
1499 {
3ae729d5
L
1500 max_single_nop_size = sizeof (alt_patt) / sizeof (alt_patt[0]);
1501 /* Limit number of NOPs to 7 for newer processors. */
1502 max_number_of_nops = 7;
1503 }
1504 }
1505
1506 if (limit == 0)
1507 limit = max_single_nop_size;
1508
1509 if (fragP->fr_type == rs_fill_nop)
1510 {
1511 /* Output NOPs for .nop directive. */
1512 if (limit > max_single_nop_size)
1513 {
1514 as_bad_where (fragP->fr_file, fragP->fr_line,
1515 _("invalid single nop size: %d "
1516 "(expect within [0, %d])"),
1517 limit, max_single_nop_size);
1518 return;
1519 }
1520 }
e379e5f3 1521 else if (fragP->fr_type != rs_machine_dependent)
3ae729d5
L
1522 fragP->fr_var = count;
1523
1524 if ((count / max_single_nop_size) > max_number_of_nops)
1525 {
1526 /* Generate jump over NOPs. */
1527 offsetT disp = count - 2;
1528 if (fits_in_imm7 (disp))
1529 {
1530 /* Use "jmp disp8" if possible. */
1531 count = disp;
1532 where[0] = jump_disp8[0];
1533 where[1] = count;
1534 where += 2;
1535 }
1536 else
1537 {
1538 unsigned int size_of_jump;
1539
1540 if (flag_code == CODE_16BIT)
1541 {
1542 where[0] = jump16_disp32[0];
1543 where[1] = jump16_disp32[1];
1544 size_of_jump = 2;
1545 }
1546 else
1547 {
1548 where[0] = jump32_disp32[0];
1549 size_of_jump = 1;
1550 }
1551
1552 count -= size_of_jump + 4;
1553 if (!fits_in_imm31 (count))
1554 {
1555 as_bad_where (fragP->fr_file, fragP->fr_line,
1556 _("jump over nop padding out of range"));
1557 return;
1558 }
1559
1560 md_number_to_chars (where + size_of_jump, count, 4);
1561 where += size_of_jump + 4;
76bc74dc 1562 }
ccc9c027 1563 }
3ae729d5
L
1564
1565 /* Generate multiple NOPs. */
1566 i386_output_nops (where, patt, count, limit);
252b5132
RH
1567}
1568
c6fb90c8 1569static INLINE int
0dfbf9d7 1570operand_type_all_zero (const union i386_operand_type *x)
40fb9820 1571{
0dfbf9d7 1572 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1573 {
1574 case 3:
0dfbf9d7 1575 if (x->array[2])
c6fb90c8 1576 return 0;
1a0670f3 1577 /* Fall through. */
c6fb90c8 1578 case 2:
0dfbf9d7 1579 if (x->array[1])
c6fb90c8 1580 return 0;
1a0670f3 1581 /* Fall through. */
c6fb90c8 1582 case 1:
0dfbf9d7 1583 return !x->array[0];
c6fb90c8
L
1584 default:
1585 abort ();
1586 }
40fb9820
L
1587}
1588
c6fb90c8 1589static INLINE void
0dfbf9d7 1590operand_type_set (union i386_operand_type *x, unsigned int v)
40fb9820 1591{
0dfbf9d7 1592 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1593 {
1594 case 3:
0dfbf9d7 1595 x->array[2] = v;
1a0670f3 1596 /* Fall through. */
c6fb90c8 1597 case 2:
0dfbf9d7 1598 x->array[1] = v;
1a0670f3 1599 /* Fall through. */
c6fb90c8 1600 case 1:
0dfbf9d7 1601 x->array[0] = v;
1a0670f3 1602 /* Fall through. */
c6fb90c8
L
1603 break;
1604 default:
1605 abort ();
1606 }
bab6aec1
JB
1607
1608 x->bitfield.class = ClassNone;
75e5731b 1609 x->bitfield.instance = InstanceNone;
c6fb90c8 1610}
40fb9820 1611
c6fb90c8 1612static INLINE int
0dfbf9d7
L
1613operand_type_equal (const union i386_operand_type *x,
1614 const union i386_operand_type *y)
c6fb90c8 1615{
0dfbf9d7 1616 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1617 {
1618 case 3:
0dfbf9d7 1619 if (x->array[2] != y->array[2])
c6fb90c8 1620 return 0;
1a0670f3 1621 /* Fall through. */
c6fb90c8 1622 case 2:
0dfbf9d7 1623 if (x->array[1] != y->array[1])
c6fb90c8 1624 return 0;
1a0670f3 1625 /* Fall through. */
c6fb90c8 1626 case 1:
0dfbf9d7 1627 return x->array[0] == y->array[0];
c6fb90c8
L
1628 break;
1629 default:
1630 abort ();
1631 }
1632}
40fb9820 1633
0dfbf9d7
L
1634static INLINE int
1635cpu_flags_all_zero (const union i386_cpu_flags *x)
1636{
1637 switch (ARRAY_SIZE(x->array))
1638 {
75f8266a
KL
1639 case 5:
1640 if (x->array[4])
1641 return 0;
1642 /* Fall through. */
53467f57
IT
1643 case 4:
1644 if (x->array[3])
1645 return 0;
1646 /* Fall through. */
0dfbf9d7
L
1647 case 3:
1648 if (x->array[2])
1649 return 0;
1a0670f3 1650 /* Fall through. */
0dfbf9d7
L
1651 case 2:
1652 if (x->array[1])
1653 return 0;
1a0670f3 1654 /* Fall through. */
0dfbf9d7
L
1655 case 1:
1656 return !x->array[0];
1657 default:
1658 abort ();
1659 }
1660}
1661
0dfbf9d7
L
1662static INLINE int
1663cpu_flags_equal (const union i386_cpu_flags *x,
1664 const union i386_cpu_flags *y)
1665{
1666 switch (ARRAY_SIZE(x->array))
1667 {
75f8266a
KL
1668 case 5:
1669 if (x->array[4] != y->array[4])
1670 return 0;
1671 /* Fall through. */
53467f57
IT
1672 case 4:
1673 if (x->array[3] != y->array[3])
1674 return 0;
1675 /* Fall through. */
0dfbf9d7
L
1676 case 3:
1677 if (x->array[2] != y->array[2])
1678 return 0;
1a0670f3 1679 /* Fall through. */
0dfbf9d7
L
1680 case 2:
1681 if (x->array[1] != y->array[1])
1682 return 0;
1a0670f3 1683 /* Fall through. */
0dfbf9d7
L
1684 case 1:
1685 return x->array[0] == y->array[0];
1686 break;
1687 default:
1688 abort ();
1689 }
1690}
c6fb90c8
L
1691
1692static INLINE int
1693cpu_flags_check_cpu64 (i386_cpu_flags f)
1694{
1695 return !((flag_code == CODE_64BIT && f.bitfield.cpuno64)
1696 || (flag_code != CODE_64BIT && f.bitfield.cpu64));
40fb9820
L
1697}
1698
c6fb90c8
L
1699static INLINE i386_cpu_flags
1700cpu_flags_and (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1701{
c6fb90c8
L
1702 switch (ARRAY_SIZE (x.array))
1703 {
75f8266a
KL
1704 case 5:
1705 x.array [4] &= y.array [4];
1706 /* Fall through. */
53467f57
IT
1707 case 4:
1708 x.array [3] &= y.array [3];
1709 /* Fall through. */
c6fb90c8
L
1710 case 3:
1711 x.array [2] &= y.array [2];
1a0670f3 1712 /* Fall through. */
c6fb90c8
L
1713 case 2:
1714 x.array [1] &= y.array [1];
1a0670f3 1715 /* Fall through. */
c6fb90c8
L
1716 case 1:
1717 x.array [0] &= y.array [0];
1718 break;
1719 default:
1720 abort ();
1721 }
1722 return x;
1723}
40fb9820 1724
c6fb90c8
L
1725static INLINE i386_cpu_flags
1726cpu_flags_or (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1727{
c6fb90c8 1728 switch (ARRAY_SIZE (x.array))
40fb9820 1729 {
75f8266a
KL
1730 case 5:
1731 x.array [4] |= y.array [4];
1732 /* Fall through. */
53467f57
IT
1733 case 4:
1734 x.array [3] |= y.array [3];
1735 /* Fall through. */
c6fb90c8
L
1736 case 3:
1737 x.array [2] |= y.array [2];
1a0670f3 1738 /* Fall through. */
c6fb90c8
L
1739 case 2:
1740 x.array [1] |= y.array [1];
1a0670f3 1741 /* Fall through. */
c6fb90c8
L
1742 case 1:
1743 x.array [0] |= y.array [0];
40fb9820
L
1744 break;
1745 default:
1746 abort ();
1747 }
40fb9820
L
1748 return x;
1749}
1750
309d3373
JB
1751static INLINE i386_cpu_flags
1752cpu_flags_and_not (i386_cpu_flags x, i386_cpu_flags y)
1753{
1754 switch (ARRAY_SIZE (x.array))
1755 {
75f8266a
KL
1756 case 5:
1757 x.array [4] &= ~y.array [4];
1758 /* Fall through. */
53467f57
IT
1759 case 4:
1760 x.array [3] &= ~y.array [3];
1761 /* Fall through. */
309d3373
JB
1762 case 3:
1763 x.array [2] &= ~y.array [2];
1a0670f3 1764 /* Fall through. */
309d3373
JB
1765 case 2:
1766 x.array [1] &= ~y.array [1];
1a0670f3 1767 /* Fall through. */
309d3373
JB
1768 case 1:
1769 x.array [0] &= ~y.array [0];
1770 break;
1771 default:
1772 abort ();
1773 }
1774 return x;
1775}
1776
6c0946d0
JB
1777static const i386_cpu_flags avx512 = CPU_ANY_AVX512F_FLAGS;
1778
c0f3af97
L
1779#define CPU_FLAGS_ARCH_MATCH 0x1
1780#define CPU_FLAGS_64BIT_MATCH 0x2
1781
c0f3af97 1782#define CPU_FLAGS_PERFECT_MATCH \
db12e14e 1783 (CPU_FLAGS_ARCH_MATCH | CPU_FLAGS_64BIT_MATCH)
c0f3af97
L
1784
1785/* Return CPU flags match bits. */
3629bb00 1786
40fb9820 1787static int
d3ce72d0 1788cpu_flags_match (const insn_template *t)
40fb9820 1789{
c0f3af97
L
1790 i386_cpu_flags x = t->cpu_flags;
1791 int match = cpu_flags_check_cpu64 (x) ? CPU_FLAGS_64BIT_MATCH : 0;
40fb9820
L
1792
1793 x.bitfield.cpu64 = 0;
1794 x.bitfield.cpuno64 = 0;
1795
0dfbf9d7 1796 if (cpu_flags_all_zero (&x))
c0f3af97
L
1797 {
1798 /* This instruction is available on all archs. */
db12e14e 1799 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 1800 }
3629bb00
L
1801 else
1802 {
c0f3af97 1803 /* This instruction is available only on some archs. */
3629bb00
L
1804 i386_cpu_flags cpu = cpu_arch_flags;
1805
ab592e75
JB
1806 /* AVX512VL is no standalone feature - match it and then strip it. */
1807 if (x.bitfield.cpuavx512vl && !cpu.bitfield.cpuavx512vl)
1808 return match;
1809 x.bitfield.cpuavx512vl = 0;
1810
22c36940
JB
1811 /* AVX and AVX2 present at the same time express an operand size
1812 dependency - strip AVX2 for the purposes here. The operand size
1813 dependent check occurs in check_vecOperands(). */
1814 if (x.bitfield.cpuavx && x.bitfield.cpuavx2)
1815 x.bitfield.cpuavx2 = 0;
1816
3629bb00 1817 cpu = cpu_flags_and (x, cpu);
c0f3af97
L
1818 if (!cpu_flags_all_zero (&cpu))
1819 {
57392598 1820 if (x.bitfield.cpuavx)
a5ff0eb2 1821 {
929f69fa 1822 /* We need to check a few extra flags with AVX. */
b9d49817 1823 if (cpu.bitfield.cpuavx
40d231b4
JB
1824 && (!t->opcode_modifier.sse2avx
1825 || (sse2avx && !i.prefix[DATA_PREFIX]))
b9d49817 1826 && (!x.bitfield.cpuaes || cpu.bitfield.cpuaes)
929f69fa 1827 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
b9d49817
JB
1828 && (!x.bitfield.cpupclmul || cpu.bitfield.cpupclmul))
1829 match |= CPU_FLAGS_ARCH_MATCH;
a5ff0eb2 1830 }
929f69fa
JB
1831 else if (x.bitfield.cpuavx512f)
1832 {
1833 /* We need to check a few extra flags with AVX512F. */
1834 if (cpu.bitfield.cpuavx512f
1835 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
1836 && (!x.bitfield.cpuvaes || cpu.bitfield.cpuvaes)
1837 && (!x.bitfield.cpuvpclmulqdq || cpu.bitfield.cpuvpclmulqdq))
1838 match |= CPU_FLAGS_ARCH_MATCH;
1839 }
a5ff0eb2 1840 else
db12e14e 1841 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 1842 }
3629bb00 1843 }
c0f3af97 1844 return match;
40fb9820
L
1845}
1846
c6fb90c8
L
1847static INLINE i386_operand_type
1848operand_type_and (i386_operand_type x, i386_operand_type y)
40fb9820 1849{
bab6aec1
JB
1850 if (x.bitfield.class != y.bitfield.class)
1851 x.bitfield.class = ClassNone;
75e5731b
JB
1852 if (x.bitfield.instance != y.bitfield.instance)
1853 x.bitfield.instance = InstanceNone;
bab6aec1 1854
c6fb90c8
L
1855 switch (ARRAY_SIZE (x.array))
1856 {
1857 case 3:
1858 x.array [2] &= y.array [2];
1a0670f3 1859 /* Fall through. */
c6fb90c8
L
1860 case 2:
1861 x.array [1] &= y.array [1];
1a0670f3 1862 /* Fall through. */
c6fb90c8
L
1863 case 1:
1864 x.array [0] &= y.array [0];
1865 break;
1866 default:
1867 abort ();
1868 }
1869 return x;
40fb9820
L
1870}
1871
73053c1f
JB
1872static INLINE i386_operand_type
1873operand_type_and_not (i386_operand_type x, i386_operand_type y)
1874{
bab6aec1 1875 gas_assert (y.bitfield.class == ClassNone);
75e5731b 1876 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 1877
73053c1f
JB
1878 switch (ARRAY_SIZE (x.array))
1879 {
1880 case 3:
1881 x.array [2] &= ~y.array [2];
1882 /* Fall through. */
1883 case 2:
1884 x.array [1] &= ~y.array [1];
1885 /* Fall through. */
1886 case 1:
1887 x.array [0] &= ~y.array [0];
1888 break;
1889 default:
1890 abort ();
1891 }
1892 return x;
1893}
1894
c6fb90c8
L
1895static INLINE i386_operand_type
1896operand_type_or (i386_operand_type x, i386_operand_type y)
40fb9820 1897{
bab6aec1
JB
1898 gas_assert (x.bitfield.class == ClassNone ||
1899 y.bitfield.class == ClassNone ||
1900 x.bitfield.class == y.bitfield.class);
75e5731b
JB
1901 gas_assert (x.bitfield.instance == InstanceNone ||
1902 y.bitfield.instance == InstanceNone ||
1903 x.bitfield.instance == y.bitfield.instance);
bab6aec1 1904
c6fb90c8 1905 switch (ARRAY_SIZE (x.array))
40fb9820 1906 {
c6fb90c8
L
1907 case 3:
1908 x.array [2] |= y.array [2];
1a0670f3 1909 /* Fall through. */
c6fb90c8
L
1910 case 2:
1911 x.array [1] |= y.array [1];
1a0670f3 1912 /* Fall through. */
c6fb90c8
L
1913 case 1:
1914 x.array [0] |= y.array [0];
40fb9820
L
1915 break;
1916 default:
1917 abort ();
1918 }
c6fb90c8
L
1919 return x;
1920}
40fb9820 1921
c6fb90c8
L
1922static INLINE i386_operand_type
1923operand_type_xor (i386_operand_type x, i386_operand_type y)
1924{
bab6aec1 1925 gas_assert (y.bitfield.class == ClassNone);
75e5731b 1926 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 1927
c6fb90c8
L
1928 switch (ARRAY_SIZE (x.array))
1929 {
1930 case 3:
1931 x.array [2] ^= y.array [2];
1a0670f3 1932 /* Fall through. */
c6fb90c8
L
1933 case 2:
1934 x.array [1] ^= y.array [1];
1a0670f3 1935 /* Fall through. */
c6fb90c8
L
1936 case 1:
1937 x.array [0] ^= y.array [0];
1938 break;
1939 default:
1940 abort ();
1941 }
40fb9820
L
1942 return x;
1943}
1944
40fb9820 1945static const i386_operand_type disp16_32 = OPERAND_TYPE_DISP16_32;
bab6aec1 1946static const i386_operand_type anydisp = OPERAND_TYPE_ANYDISP;
40fb9820 1947static const i386_operand_type regxmm = OPERAND_TYPE_REGXMM;
40fb9820
L
1948static const i386_operand_type imm16 = OPERAND_TYPE_IMM16;
1949static const i386_operand_type imm32 = OPERAND_TYPE_IMM32;
1950static const i386_operand_type imm32s = OPERAND_TYPE_IMM32S;
40fb9820
L
1951static const i386_operand_type imm16_32 = OPERAND_TYPE_IMM16_32;
1952static const i386_operand_type imm16_32s = OPERAND_TYPE_IMM16_32S;
1953static const i386_operand_type imm16_32_32s = OPERAND_TYPE_IMM16_32_32S;
1954
1955enum operand_type
1956{
1957 reg,
40fb9820
L
1958 imm,
1959 disp,
1960 anymem
1961};
1962
c6fb90c8 1963static INLINE int
40fb9820
L
1964operand_type_check (i386_operand_type t, enum operand_type c)
1965{
1966 switch (c)
1967 {
1968 case reg:
bab6aec1 1969 return t.bitfield.class == Reg;
40fb9820 1970
40fb9820
L
1971 case imm:
1972 return (t.bitfield.imm8
1973 || t.bitfield.imm8s
1974 || t.bitfield.imm16
1975 || t.bitfield.imm32
1976 || t.bitfield.imm32s
1977 || t.bitfield.imm64);
1978
1979 case disp:
1980 return (t.bitfield.disp8
1981 || t.bitfield.disp16
1982 || t.bitfield.disp32
40fb9820
L
1983 || t.bitfield.disp64);
1984
1985 case anymem:
1986 return (t.bitfield.disp8
1987 || t.bitfield.disp16
1988 || t.bitfield.disp32
40fb9820
L
1989 || t.bitfield.disp64
1990 || t.bitfield.baseindex);
1991
1992 default:
1993 abort ();
1994 }
2cfe26b6
AM
1995
1996 return 0;
40fb9820
L
1997}
1998
7a54636a
L
1999/* Return 1 if there is no conflict in 8bit/16bit/32bit/64bit/80bit size
2000 between operand GIVEN and opeand WANTED for instruction template T. */
5c07affc
L
2001
2002static INLINE int
7a54636a
L
2003match_operand_size (const insn_template *t, unsigned int wanted,
2004 unsigned int given)
5c07affc 2005{
3ac21baa
JB
2006 return !((i.types[given].bitfield.byte
2007 && !t->operand_types[wanted].bitfield.byte)
2008 || (i.types[given].bitfield.word
2009 && !t->operand_types[wanted].bitfield.word)
2010 || (i.types[given].bitfield.dword
2011 && !t->operand_types[wanted].bitfield.dword)
2012 || (i.types[given].bitfield.qword
2013 && !t->operand_types[wanted].bitfield.qword)
2014 || (i.types[given].bitfield.tbyte
2015 && !t->operand_types[wanted].bitfield.tbyte));
5c07affc
L
2016}
2017
dd40ce22
L
2018/* Return 1 if there is no conflict in SIMD register between operand
2019 GIVEN and opeand WANTED for instruction template T. */
1b54b8d7
JB
2020
2021static INLINE int
dd40ce22
L
2022match_simd_size (const insn_template *t, unsigned int wanted,
2023 unsigned int given)
1b54b8d7 2024{
3ac21baa
JB
2025 return !((i.types[given].bitfield.xmmword
2026 && !t->operand_types[wanted].bitfield.xmmword)
2027 || (i.types[given].bitfield.ymmword
2028 && !t->operand_types[wanted].bitfield.ymmword)
2029 || (i.types[given].bitfield.zmmword
260cd341
LC
2030 && !t->operand_types[wanted].bitfield.zmmword)
2031 || (i.types[given].bitfield.tmmword
2032 && !t->operand_types[wanted].bitfield.tmmword));
1b54b8d7
JB
2033}
2034
7a54636a
L
2035/* Return 1 if there is no conflict in any size between operand GIVEN
2036 and opeand WANTED for instruction template T. */
5c07affc
L
2037
2038static INLINE int
dd40ce22
L
2039match_mem_size (const insn_template *t, unsigned int wanted,
2040 unsigned int given)
5c07affc 2041{
7a54636a 2042 return (match_operand_size (t, wanted, given)
3ac21baa 2043 && !((i.types[given].bitfield.unspecified
5273a3cd 2044 && !i.broadcast.type
a5748e0d 2045 && !i.broadcast.bytes
3ac21baa
JB
2046 && !t->operand_types[wanted].bitfield.unspecified)
2047 || (i.types[given].bitfield.fword
2048 && !t->operand_types[wanted].bitfield.fword)
1b54b8d7
JB
2049 /* For scalar opcode templates to allow register and memory
2050 operands at the same time, some special casing is needed
d6793fa1
JB
2051 here. Also for v{,p}broadcast*, {,v}pmov{s,z}*, and
2052 down-conversion vpmov*. */
3528c362 2053 || ((t->operand_types[wanted].bitfield.class == RegSIMD
bc49bfd8
JB
2054 && t->operand_types[wanted].bitfield.byte
2055 + t->operand_types[wanted].bitfield.word
2056 + t->operand_types[wanted].bitfield.dword
2057 + t->operand_types[wanted].bitfield.qword
2058 > !!t->opcode_modifier.broadcast)
3ac21baa
JB
2059 ? (i.types[given].bitfield.xmmword
2060 || i.types[given].bitfield.ymmword
2061 || i.types[given].bitfield.zmmword)
2062 : !match_simd_size(t, wanted, given))));
5c07affc
L
2063}
2064
3ac21baa
JB
2065/* Return value has MATCH_STRAIGHT set if there is no size conflict on any
2066 operands for instruction template T, and it has MATCH_REVERSE set if there
2067 is no size conflict on any operands for the template with operands reversed
2068 (and the template allows for reversing in the first place). */
5c07affc 2069
3ac21baa
JB
2070#define MATCH_STRAIGHT 1
2071#define MATCH_REVERSE 2
2072
2073static INLINE unsigned int
d3ce72d0 2074operand_size_match (const insn_template *t)
5c07affc 2075{
3ac21baa 2076 unsigned int j, match = MATCH_STRAIGHT;
5c07affc 2077
0cfa3eb3 2078 /* Don't check non-absolute jump instructions. */
5c07affc 2079 if (t->opcode_modifier.jump
0cfa3eb3 2080 && t->opcode_modifier.jump != JUMP_ABSOLUTE)
5c07affc
L
2081 return match;
2082
2083 /* Check memory and accumulator operand size. */
2084 for (j = 0; j < i.operands; j++)
2085 {
3528c362
JB
2086 if (i.types[j].bitfield.class != Reg
2087 && i.types[j].bitfield.class != RegSIMD
255571cd 2088 && t->opcode_modifier.operandconstraint == ANY_SIZE)
5c07affc
L
2089 continue;
2090
bab6aec1 2091 if (t->operand_types[j].bitfield.class == Reg
7a54636a 2092 && !match_operand_size (t, j, j))
5c07affc
L
2093 {
2094 match = 0;
2095 break;
2096 }
2097
3528c362 2098 if (t->operand_types[j].bitfield.class == RegSIMD
3ac21baa 2099 && !match_simd_size (t, j, j))
1b54b8d7
JB
2100 {
2101 match = 0;
2102 break;
2103 }
2104
75e5731b 2105 if (t->operand_types[j].bitfield.instance == Accum
7a54636a 2106 && (!match_operand_size (t, j, j) || !match_simd_size (t, j, j)))
1b54b8d7
JB
2107 {
2108 match = 0;
2109 break;
2110 }
2111
c48dadc9 2112 if ((i.flags[j] & Operand_Mem) && !match_mem_size (t, j, j))
5c07affc
L
2113 {
2114 match = 0;
2115 break;
2116 }
2117 }
2118
3ac21baa 2119 if (!t->opcode_modifier.d)
7b94647a 2120 return match;
5c07affc
L
2121
2122 /* Check reverse. */
8bd915b7
JB
2123 gas_assert ((i.operands >= 2 && i.operands <= 3)
2124 || t->opcode_modifier.vexsources);
5c07affc 2125
f5eb1d70 2126 for (j = 0; j < i.operands; j++)
5c07affc 2127 {
f5eb1d70
JB
2128 unsigned int given = i.operands - j - 1;
2129
8bd915b7
JB
2130 /* For 4- and 5-operand insns VEX.W controls just the first two
2131 register operands. */
2132 if (t->opcode_modifier.vexsources)
2133 given = j < 2 ? 1 - j : j;
2134
bab6aec1 2135 if (t->operand_types[j].bitfield.class == Reg
f5eb1d70 2136 && !match_operand_size (t, j, given))
7b94647a 2137 return match;
5c07affc 2138
3528c362 2139 if (t->operand_types[j].bitfield.class == RegSIMD
f5eb1d70 2140 && !match_simd_size (t, j, given))
7b94647a 2141 return match;
dbbc8b7e 2142
75e5731b 2143 if (t->operand_types[j].bitfield.instance == Accum
f5eb1d70
JB
2144 && (!match_operand_size (t, j, given)
2145 || !match_simd_size (t, j, given)))
7b94647a 2146 return match;
dbbc8b7e 2147
f5eb1d70 2148 if ((i.flags[given] & Operand_Mem) && !match_mem_size (t, j, given))
7b94647a 2149 return match;
5c07affc
L
2150 }
2151
3ac21baa 2152 return match | MATCH_REVERSE;
5c07affc
L
2153}
2154
c6fb90c8 2155static INLINE int
40fb9820
L
2156operand_type_match (i386_operand_type overlap,
2157 i386_operand_type given)
2158{
2159 i386_operand_type temp = overlap;
2160
7d5e4556 2161 temp.bitfield.unspecified = 0;
5c07affc
L
2162 temp.bitfield.byte = 0;
2163 temp.bitfield.word = 0;
2164 temp.bitfield.dword = 0;
2165 temp.bitfield.fword = 0;
2166 temp.bitfield.qword = 0;
2167 temp.bitfield.tbyte = 0;
2168 temp.bitfield.xmmword = 0;
c0f3af97 2169 temp.bitfield.ymmword = 0;
43234a1e 2170 temp.bitfield.zmmword = 0;
260cd341 2171 temp.bitfield.tmmword = 0;
0dfbf9d7 2172 if (operand_type_all_zero (&temp))
891edac4 2173 goto mismatch;
40fb9820 2174
6f2f06be 2175 if (given.bitfield.baseindex == overlap.bitfield.baseindex)
891edac4
L
2176 return 1;
2177
dc1e8a47 2178 mismatch:
a65babc9 2179 i.error = operand_type_mismatch;
891edac4 2180 return 0;
40fb9820
L
2181}
2182
7d5e4556 2183/* If given types g0 and g1 are registers they must be of the same type
10c17abd 2184 unless the expected operand type register overlap is null.
5de4d9ef 2185 Some Intel syntax memory operand size checking also happens here. */
40fb9820 2186
c6fb90c8 2187static INLINE int
dc821c5f 2188operand_type_register_match (i386_operand_type g0,
40fb9820 2189 i386_operand_type t0,
40fb9820
L
2190 i386_operand_type g1,
2191 i386_operand_type t1)
2192{
bab6aec1 2193 if (g0.bitfield.class != Reg
3528c362 2194 && g0.bitfield.class != RegSIMD
10c17abd
JB
2195 && (!operand_type_check (g0, anymem)
2196 || g0.bitfield.unspecified
5de4d9ef
JB
2197 || (t0.bitfield.class != Reg
2198 && t0.bitfield.class != RegSIMD)))
40fb9820
L
2199 return 1;
2200
bab6aec1 2201 if (g1.bitfield.class != Reg
3528c362 2202 && g1.bitfield.class != RegSIMD
10c17abd
JB
2203 && (!operand_type_check (g1, anymem)
2204 || g1.bitfield.unspecified
5de4d9ef
JB
2205 || (t1.bitfield.class != Reg
2206 && t1.bitfield.class != RegSIMD)))
40fb9820
L
2207 return 1;
2208
dc821c5f
JB
2209 if (g0.bitfield.byte == g1.bitfield.byte
2210 && g0.bitfield.word == g1.bitfield.word
2211 && g0.bitfield.dword == g1.bitfield.dword
10c17abd
JB
2212 && g0.bitfield.qword == g1.bitfield.qword
2213 && g0.bitfield.xmmword == g1.bitfield.xmmword
2214 && g0.bitfield.ymmword == g1.bitfield.ymmword
2215 && g0.bitfield.zmmword == g1.bitfield.zmmword)
40fb9820
L
2216 return 1;
2217
c4d09633
JB
2218 /* If expectations overlap in no more than a single size, all is fine. */
2219 g0 = operand_type_and (t0, t1);
2220 if (g0.bitfield.byte
2221 + g0.bitfield.word
2222 + g0.bitfield.dword
2223 + g0.bitfield.qword
2224 + g0.bitfield.xmmword
2225 + g0.bitfield.ymmword
2226 + g0.bitfield.zmmword <= 1)
891edac4
L
2227 return 1;
2228
a65babc9 2229 i.error = register_type_mismatch;
891edac4
L
2230
2231 return 0;
40fb9820
L
2232}
2233
4c692bc7
JB
2234static INLINE unsigned int
2235register_number (const reg_entry *r)
2236{
2237 unsigned int nr = r->reg_num;
2238
2239 if (r->reg_flags & RegRex)
2240 nr += 8;
2241
200cbe0f
L
2242 if (r->reg_flags & RegVRex)
2243 nr += 16;
2244
4c692bc7
JB
2245 return nr;
2246}
2247
252b5132 2248static INLINE unsigned int
40fb9820 2249mode_from_disp_size (i386_operand_type t)
252b5132 2250{
b5014f7a 2251 if (t.bitfield.disp8)
40fb9820
L
2252 return 1;
2253 else if (t.bitfield.disp16
a775efc8 2254 || t.bitfield.disp32)
40fb9820
L
2255 return 2;
2256 else
2257 return 0;
252b5132
RH
2258}
2259
2260static INLINE int
65879393 2261fits_in_signed_byte (addressT num)
252b5132 2262{
65879393 2263 return num + 0x80 <= 0xff;
47926f60 2264}
252b5132
RH
2265
2266static INLINE int
65879393 2267fits_in_unsigned_byte (addressT num)
252b5132 2268{
65879393 2269 return num <= 0xff;
47926f60 2270}
252b5132
RH
2271
2272static INLINE int
65879393 2273fits_in_unsigned_word (addressT num)
252b5132 2274{
65879393 2275 return num <= 0xffff;
47926f60 2276}
252b5132
RH
2277
2278static INLINE int
65879393 2279fits_in_signed_word (addressT num)
252b5132 2280{
65879393 2281 return num + 0x8000 <= 0xffff;
47926f60 2282}
2a962e6d 2283
3e73aa7c 2284static INLINE int
65879393 2285fits_in_signed_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2286{
2287#ifndef BFD64
2288 return 1;
2289#else
65879393 2290 return num + 0x80000000 <= 0xffffffff;
3e73aa7c
JH
2291#endif
2292} /* fits_in_signed_long() */
2a962e6d 2293
3e73aa7c 2294static INLINE int
65879393 2295fits_in_unsigned_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2296{
2297#ifndef BFD64
2298 return 1;
2299#else
65879393 2300 return num <= 0xffffffff;
3e73aa7c
JH
2301#endif
2302} /* fits_in_unsigned_long() */
252b5132 2303
a442cac5
JB
2304static INLINE valueT extend_to_32bit_address (addressT num)
2305{
2306#ifdef BFD64
2307 if (fits_in_unsigned_long(num))
2308 return (num ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
2309
2310 if (!fits_in_signed_long (num))
2311 return num & 0xffffffff;
2312#endif
2313
2314 return num;
2315}
2316
43234a1e 2317static INLINE int
b5014f7a 2318fits_in_disp8 (offsetT num)
43234a1e
L
2319{
2320 int shift = i.memshift;
2321 unsigned int mask;
2322
2323 if (shift == -1)
2324 abort ();
2325
2326 mask = (1 << shift) - 1;
2327
2328 /* Return 0 if NUM isn't properly aligned. */
2329 if ((num & mask))
2330 return 0;
2331
2332 /* Check if NUM will fit in 8bit after shift. */
2333 return fits_in_signed_byte (num >> shift);
2334}
2335
a683cc34
SP
2336static INLINE int
2337fits_in_imm4 (offsetT num)
2338{
2339 return (num & 0xf) == num;
2340}
2341
40fb9820 2342static i386_operand_type
e3bb37b5 2343smallest_imm_type (offsetT num)
252b5132 2344{
40fb9820 2345 i386_operand_type t;
7ab9ffdd 2346
0dfbf9d7 2347 operand_type_set (&t, 0);
40fb9820
L
2348 t.bitfield.imm64 = 1;
2349
2350 if (cpu_arch_tune != PROCESSOR_I486 && num == 1)
e413e4e9
AM
2351 {
2352 /* This code is disabled on the 486 because all the Imm1 forms
2353 in the opcode table are slower on the i486. They're the
2354 versions with the implicitly specified single-position
2355 displacement, which has another syntax if you really want to
2356 use that form. */
40fb9820
L
2357 t.bitfield.imm1 = 1;
2358 t.bitfield.imm8 = 1;
2359 t.bitfield.imm8s = 1;
2360 t.bitfield.imm16 = 1;
2361 t.bitfield.imm32 = 1;
2362 t.bitfield.imm32s = 1;
2363 }
2364 else if (fits_in_signed_byte (num))
2365 {
2366 t.bitfield.imm8 = 1;
2367 t.bitfield.imm8s = 1;
2368 t.bitfield.imm16 = 1;
2369 t.bitfield.imm32 = 1;
2370 t.bitfield.imm32s = 1;
2371 }
2372 else if (fits_in_unsigned_byte (num))
2373 {
2374 t.bitfield.imm8 = 1;
2375 t.bitfield.imm16 = 1;
2376 t.bitfield.imm32 = 1;
2377 t.bitfield.imm32s = 1;
2378 }
2379 else if (fits_in_signed_word (num) || fits_in_unsigned_word (num))
2380 {
2381 t.bitfield.imm16 = 1;
2382 t.bitfield.imm32 = 1;
2383 t.bitfield.imm32s = 1;
2384 }
2385 else if (fits_in_signed_long (num))
2386 {
2387 t.bitfield.imm32 = 1;
2388 t.bitfield.imm32s = 1;
2389 }
2390 else if (fits_in_unsigned_long (num))
2391 t.bitfield.imm32 = 1;
2392
2393 return t;
47926f60 2394}
252b5132 2395
847f7ad4 2396static offsetT
e3bb37b5 2397offset_in_range (offsetT val, int size)
847f7ad4 2398{
508866be 2399 addressT mask;
ba2adb93 2400
847f7ad4
AM
2401 switch (size)
2402 {
508866be
L
2403 case 1: mask = ((addressT) 1 << 8) - 1; break;
2404 case 2: mask = ((addressT) 1 << 16) - 1; break;
3e73aa7c 2405#ifdef BFD64
64965897 2406 case 4: mask = ((addressT) 1 << 32) - 1; break;
3e73aa7c 2407#endif
64965897 2408 case sizeof (val): return val;
47926f60 2409 default: abort ();
847f7ad4
AM
2410 }
2411
4fe51f7d 2412 if ((val & ~mask) != 0 && (-val & ~mask) != 0)
f493c217
AM
2413 as_warn (_("0x%" PRIx64 " shortened to 0x%" PRIx64),
2414 (uint64_t) val, (uint64_t) (val & mask));
847f7ad4 2415
847f7ad4
AM
2416 return val & mask;
2417}
2418
c32fa91d
L
2419enum PREFIX_GROUP
2420{
2421 PREFIX_EXIST = 0,
2422 PREFIX_LOCK,
2423 PREFIX_REP,
04ef582a 2424 PREFIX_DS,
c32fa91d
L
2425 PREFIX_OTHER
2426};
2427
2428/* Returns
2429 a. PREFIX_EXIST if attempting to add a prefix where one from the
2430 same class already exists.
2431 b. PREFIX_LOCK if lock prefix is added.
2432 c. PREFIX_REP if rep/repne prefix is added.
04ef582a
L
2433 d. PREFIX_DS if ds prefix is added.
2434 e. PREFIX_OTHER if other prefix is added.
c32fa91d
L
2435 */
2436
2437static enum PREFIX_GROUP
e3bb37b5 2438add_prefix (unsigned int prefix)
252b5132 2439{
c32fa91d 2440 enum PREFIX_GROUP ret = PREFIX_OTHER;
b1905489 2441 unsigned int q;
252b5132 2442
29b0f896
AM
2443 if (prefix >= REX_OPCODE && prefix < REX_OPCODE + 16
2444 && flag_code == CODE_64BIT)
b1905489 2445 {
161a04f6 2446 if ((i.prefix[REX_PREFIX] & prefix & REX_W)
44846f29
JB
2447 || (i.prefix[REX_PREFIX] & prefix & REX_R)
2448 || (i.prefix[REX_PREFIX] & prefix & REX_X)
2449 || (i.prefix[REX_PREFIX] & prefix & REX_B))
c32fa91d 2450 ret = PREFIX_EXIST;
b1905489
JB
2451 q = REX_PREFIX;
2452 }
3e73aa7c 2453 else
b1905489
JB
2454 {
2455 switch (prefix)
2456 {
2457 default:
2458 abort ();
2459
b1905489 2460 case DS_PREFIX_OPCODE:
04ef582a
L
2461 ret = PREFIX_DS;
2462 /* Fall through. */
2463 case CS_PREFIX_OPCODE:
b1905489
JB
2464 case ES_PREFIX_OPCODE:
2465 case FS_PREFIX_OPCODE:
2466 case GS_PREFIX_OPCODE:
2467 case SS_PREFIX_OPCODE:
2468 q = SEG_PREFIX;
2469 break;
2470
2471 case REPNE_PREFIX_OPCODE:
2472 case REPE_PREFIX_OPCODE:
c32fa91d
L
2473 q = REP_PREFIX;
2474 ret = PREFIX_REP;
2475 break;
2476
b1905489 2477 case LOCK_PREFIX_OPCODE:
c32fa91d
L
2478 q = LOCK_PREFIX;
2479 ret = PREFIX_LOCK;
b1905489
JB
2480 break;
2481
2482 case FWAIT_OPCODE:
2483 q = WAIT_PREFIX;
2484 break;
2485
2486 case ADDR_PREFIX_OPCODE:
2487 q = ADDR_PREFIX;
2488 break;
2489
2490 case DATA_PREFIX_OPCODE:
2491 q = DATA_PREFIX;
2492 break;
2493 }
2494 if (i.prefix[q] != 0)
c32fa91d 2495 ret = PREFIX_EXIST;
b1905489 2496 }
252b5132 2497
b1905489 2498 if (ret)
252b5132 2499 {
b1905489
JB
2500 if (!i.prefix[q])
2501 ++i.prefixes;
2502 i.prefix[q] |= prefix;
252b5132 2503 }
b1905489
JB
2504 else
2505 as_bad (_("same type of prefix used twice"));
252b5132 2506
252b5132
RH
2507 return ret;
2508}
2509
2510static void
78f12dd3 2511update_code_flag (int value, int check)
eecb386c 2512{
78f12dd3
L
2513 PRINTF_LIKE ((*as_error));
2514
1e9cc1c2 2515 flag_code = (enum flag_code) value;
40fb9820
L
2516 if (flag_code == CODE_64BIT)
2517 {
2518 cpu_arch_flags.bitfield.cpu64 = 1;
2519 cpu_arch_flags.bitfield.cpuno64 = 0;
40fb9820
L
2520 }
2521 else
2522 {
2523 cpu_arch_flags.bitfield.cpu64 = 0;
2524 cpu_arch_flags.bitfield.cpuno64 = 1;
40fb9820
L
2525 }
2526 if (value == CODE_64BIT && !cpu_arch_flags.bitfield.cpulm )
3e73aa7c 2527 {
78f12dd3
L
2528 if (check)
2529 as_error = as_fatal;
2530 else
2531 as_error = as_bad;
2532 (*as_error) (_("64bit mode not supported on `%s'."),
2533 cpu_arch_name ? cpu_arch_name : default_arch);
3e73aa7c 2534 }
40fb9820 2535 if (value == CODE_32BIT && !cpu_arch_flags.bitfield.cpui386)
3e73aa7c 2536 {
78f12dd3
L
2537 if (check)
2538 as_error = as_fatal;
2539 else
2540 as_error = as_bad;
2541 (*as_error) (_("32bit mode not supported on `%s'."),
2542 cpu_arch_name ? cpu_arch_name : default_arch);
3e73aa7c 2543 }
eecb386c
AM
2544 stackop_size = '\0';
2545}
2546
78f12dd3
L
2547static void
2548set_code_flag (int value)
2549{
2550 update_code_flag (value, 0);
2551}
2552
eecb386c 2553static void
e3bb37b5 2554set_16bit_gcc_code_flag (int new_code_flag)
252b5132 2555{
1e9cc1c2 2556 flag_code = (enum flag_code) new_code_flag;
40fb9820
L
2557 if (flag_code != CODE_16BIT)
2558 abort ();
2559 cpu_arch_flags.bitfield.cpu64 = 0;
2560 cpu_arch_flags.bitfield.cpuno64 = 1;
9306ca4a 2561 stackop_size = LONG_MNEM_SUFFIX;
252b5132
RH
2562}
2563
2564static void
e3bb37b5 2565set_intel_syntax (int syntax_flag)
252b5132
RH
2566{
2567 /* Find out if register prefixing is specified. */
2568 int ask_naked_reg = 0;
2569
2570 SKIP_WHITESPACE ();
29b0f896 2571 if (!is_end_of_line[(unsigned char) *input_line_pointer])
252b5132 2572 {
d02603dc
NC
2573 char *string;
2574 int e = get_symbol_name (&string);
252b5132 2575
47926f60 2576 if (strcmp (string, "prefix") == 0)
252b5132 2577 ask_naked_reg = 1;
47926f60 2578 else if (strcmp (string, "noprefix") == 0)
252b5132
RH
2579 ask_naked_reg = -1;
2580 else
d0b47220 2581 as_bad (_("bad argument to syntax directive."));
d02603dc 2582 (void) restore_line_pointer (e);
252b5132
RH
2583 }
2584 demand_empty_rest_of_line ();
c3332e24 2585
252b5132
RH
2586 intel_syntax = syntax_flag;
2587
2588 if (ask_naked_reg == 0)
f86103b7
AM
2589 allow_naked_reg = (intel_syntax
2590 && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
252b5132
RH
2591 else
2592 allow_naked_reg = (ask_naked_reg < 0);
9306ca4a 2593
ee86248c 2594 expr_set_rank (O_full_ptr, syntax_flag ? 10 : 0);
7ab9ffdd 2595
e4a3b5a4 2596 identifier_chars['%'] = intel_syntax && allow_naked_reg ? '%' : 0;
9306ca4a 2597 identifier_chars['$'] = intel_syntax ? '$' : 0;
e4a3b5a4 2598 register_prefix = allow_naked_reg ? "" : "%";
252b5132
RH
2599}
2600
1efbbeb4
L
2601static void
2602set_intel_mnemonic (int mnemonic_flag)
2603{
e1d4d893 2604 intel_mnemonic = mnemonic_flag;
1efbbeb4
L
2605}
2606
db51cc60
L
2607static void
2608set_allow_index_reg (int flag)
2609{
2610 allow_index_reg = flag;
2611}
2612
cb19c032 2613static void
7bab8ab5 2614set_check (int what)
cb19c032 2615{
7bab8ab5
JB
2616 enum check_kind *kind;
2617 const char *str;
2618
2619 if (what)
2620 {
2621 kind = &operand_check;
2622 str = "operand";
2623 }
2624 else
2625 {
2626 kind = &sse_check;
2627 str = "sse";
2628 }
2629
cb19c032
L
2630 SKIP_WHITESPACE ();
2631
2632 if (!is_end_of_line[(unsigned char) *input_line_pointer])
2633 {
d02603dc
NC
2634 char *string;
2635 int e = get_symbol_name (&string);
cb19c032
L
2636
2637 if (strcmp (string, "none") == 0)
7bab8ab5 2638 *kind = check_none;
cb19c032 2639 else if (strcmp (string, "warning") == 0)
7bab8ab5 2640 *kind = check_warning;
cb19c032 2641 else if (strcmp (string, "error") == 0)
7bab8ab5 2642 *kind = check_error;
cb19c032 2643 else
7bab8ab5 2644 as_bad (_("bad argument to %s_check directive."), str);
d02603dc 2645 (void) restore_line_pointer (e);
cb19c032
L
2646 }
2647 else
7bab8ab5 2648 as_bad (_("missing argument for %s_check directive"), str);
cb19c032
L
2649
2650 demand_empty_rest_of_line ();
2651}
2652
8a9036a4
L
2653static void
2654check_cpu_arch_compatible (const char *name ATTRIBUTE_UNUSED,
1e9cc1c2 2655 i386_cpu_flags new_flag ATTRIBUTE_UNUSED)
8a9036a4
L
2656{
2657#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2658 static const char *arch;
2659
c085ab00 2660 /* Intel MCU is only supported on ELF. */
8a9036a4
L
2661 if (!IS_ELF)
2662 return;
2663
2664 if (!arch)
2665 {
2666 /* Use cpu_arch_name if it is set in md_parse_option. Otherwise
2667 use default_arch. */
2668 arch = cpu_arch_name;
2669 if (!arch)
2670 arch = default_arch;
2671 }
2672
81486035 2673 /* If we are targeting Intel MCU, we must enable it. */
648d04db
JB
2674 if ((get_elf_backend_data (stdoutput)->elf_machine_code == EM_IAMCU)
2675 == new_flag.bitfield.cpuiamcu)
81486035
L
2676 return;
2677
8a9036a4
L
2678 as_bad (_("`%s' is not supported on `%s'"), name, arch);
2679#endif
2680}
2681
8180707f
JB
2682static void
2683extend_cpu_sub_arch_name (const char *name)
2684{
2685 if (cpu_sub_arch_name)
2686 cpu_sub_arch_name = reconcat (cpu_sub_arch_name, cpu_sub_arch_name,
ae89daec 2687 ".", name, (const char *) NULL);
8180707f 2688 else
ae89daec 2689 cpu_sub_arch_name = concat (".", name, (const char *) NULL);
8180707f
JB
2690}
2691
e413e4e9 2692static void
e3bb37b5 2693set_cpu_arch (int dummy ATTRIBUTE_UNUSED)
e413e4e9 2694{
f68697e8
JB
2695 typedef struct arch_stack_entry
2696 {
2697 const struct arch_stack_entry *prev;
2698 const char *name;
2699 char *sub_name;
2700 i386_cpu_flags flags;
2701 i386_cpu_flags isa_flags;
2702 enum processor_type isa;
2703 enum flag_code flag_code;
2704 char stackop_size;
2705 bool no_cond_jump_promotion;
2706 } arch_stack_entry;
2707 static const arch_stack_entry *arch_stack_top;
2708
47926f60 2709 SKIP_WHITESPACE ();
e413e4e9 2710
29b0f896 2711 if (!is_end_of_line[(unsigned char) *input_line_pointer])
e413e4e9 2712 {
3ce2ebcf
JB
2713 char *s;
2714 int e = get_symbol_name (&s);
2715 const char *string = s;
2716 unsigned int j = 0;
40fb9820 2717 i386_cpu_flags flags;
e413e4e9 2718
3ce2ebcf
JB
2719 if (strcmp (string, "default") == 0)
2720 {
2721 if (strcmp (default_arch, "iamcu") == 0)
2722 string = default_arch;
2723 else
2724 {
2725 static const i386_cpu_flags cpu_unknown_flags = CPU_UNKNOWN_FLAGS;
2726
2727 cpu_arch_name = NULL;
2728 free (cpu_sub_arch_name);
2729 cpu_sub_arch_name = NULL;
2730 cpu_arch_flags = cpu_unknown_flags;
2731 if (flag_code == CODE_64BIT)
2732 {
2733 cpu_arch_flags.bitfield.cpu64 = 1;
2734 cpu_arch_flags.bitfield.cpuno64 = 0;
2735 }
2736 else
2737 {
2738 cpu_arch_flags.bitfield.cpu64 = 0;
2739 cpu_arch_flags.bitfield.cpuno64 = 1;
2740 }
2741 cpu_arch_isa = PROCESSOR_UNKNOWN;
ae89daec 2742 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
3ce2ebcf
JB
2743 if (!cpu_arch_tune_set)
2744 {
2745 cpu_arch_tune = cpu_arch_isa;
2746 cpu_arch_tune_flags = cpu_arch_isa_flags;
2747 }
2748
2749 j = ARRAY_SIZE (cpu_arch) + 1;
2750 }
2751 }
f68697e8
JB
2752 else if (strcmp (string, "push") == 0)
2753 {
2754 arch_stack_entry *top = XNEW (arch_stack_entry);
2755
2756 top->name = cpu_arch_name;
2757 if (cpu_sub_arch_name)
2758 top->sub_name = xstrdup (cpu_sub_arch_name);
2759 else
2760 top->sub_name = NULL;
2761 top->flags = cpu_arch_flags;
2762 top->isa = cpu_arch_isa;
2763 top->isa_flags = cpu_arch_isa_flags;
2764 top->flag_code = flag_code;
2765 top->stackop_size = stackop_size;
2766 top->no_cond_jump_promotion = no_cond_jump_promotion;
2767
2768 top->prev = arch_stack_top;
2769 arch_stack_top = top;
2770
2771 (void) restore_line_pointer (e);
2772 demand_empty_rest_of_line ();
2773 return;
2774 }
2775 else if (strcmp (string, "pop") == 0)
2776 {
2777 const arch_stack_entry *top = arch_stack_top;
2778
2779 if (!top)
2780 as_bad (_(".arch stack is empty"));
2781 else if (top->flag_code != flag_code
2782 || top->stackop_size != stackop_size)
2783 {
2784 static const unsigned int bits[] = {
2785 [CODE_16BIT] = 16,
2786 [CODE_32BIT] = 32,
2787 [CODE_64BIT] = 64,
2788 };
2789
2790 as_bad (_("this `.arch pop' requires `.code%u%s' to be in effect"),
2791 bits[top->flag_code],
2792 top->stackop_size == LONG_MNEM_SUFFIX ? "gcc" : "");
2793 }
2794 else
2795 {
2796 arch_stack_top = top->prev;
2797
2798 cpu_arch_name = top->name;
2799 free (cpu_sub_arch_name);
2800 cpu_sub_arch_name = top->sub_name;
2801 cpu_arch_flags = top->flags;
2802 cpu_arch_isa = top->isa;
2803 cpu_arch_isa_flags = top->isa_flags;
2804 no_cond_jump_promotion = top->no_cond_jump_promotion;
2805
2806 XDELETE (top);
2807 }
2808
2809 (void) restore_line_pointer (e);
2810 demand_empty_rest_of_line ();
2811 return;
2812 }
3ce2ebcf
JB
2813
2814 for (; j < ARRAY_SIZE (cpu_arch); j++)
e413e4e9 2815 {
ae89daec
JB
2816 if (strcmp (string + (*string == '.'), cpu_arch[j].name) == 0
2817 && (*string == '.') == (cpu_arch[j].type == PROCESSOR_NONE))
e413e4e9 2818 {
5c6af06e
JB
2819 if (*string != '.')
2820 {
ae89daec 2821 check_cpu_arch_compatible (string, cpu_arch[j].enable);
648d04db 2822
91d6fa6a 2823 cpu_arch_name = cpu_arch[j].name;
d92c7521 2824 free (cpu_sub_arch_name);
5c6af06e 2825 cpu_sub_arch_name = NULL;
ae89daec 2826 cpu_arch_flags = cpu_arch[j].enable;
40fb9820
L
2827 if (flag_code == CODE_64BIT)
2828 {
2829 cpu_arch_flags.bitfield.cpu64 = 1;
2830 cpu_arch_flags.bitfield.cpuno64 = 0;
2831 }
2832 else
2833 {
2834 cpu_arch_flags.bitfield.cpu64 = 0;
2835 cpu_arch_flags.bitfield.cpuno64 = 1;
2836 }
91d6fa6a 2837 cpu_arch_isa = cpu_arch[j].type;
ae89daec 2838 cpu_arch_isa_flags = cpu_arch[j].enable;
ccc9c027
L
2839 if (!cpu_arch_tune_set)
2840 {
2841 cpu_arch_tune = cpu_arch_isa;
2842 cpu_arch_tune_flags = cpu_arch_isa_flags;
2843 }
d59a54c2 2844 pre_386_16bit_warned = false;
5c6af06e
JB
2845 break;
2846 }
40fb9820 2847
ae89daec
JB
2848 if (cpu_flags_all_zero (&cpu_arch[j].enable))
2849 continue;
2850
293f5f65 2851 flags = cpu_flags_or (cpu_arch_flags,
ae89daec 2852 cpu_arch[j].enable);
81486035 2853
5b64d091 2854 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
5c6af06e 2855 {
ae89daec 2856 extend_cpu_sub_arch_name (string + 1);
40fb9820 2857 cpu_arch_flags = flags;
a586129e 2858 cpu_arch_isa_flags = flags;
5c6af06e 2859 }
0089dace
L
2860 else
2861 cpu_arch_isa_flags
2862 = cpu_flags_or (cpu_arch_isa_flags,
ae89daec 2863 cpu_arch[j].enable);
d02603dc 2864 (void) restore_line_pointer (e);
5c6af06e
JB
2865 demand_empty_rest_of_line ();
2866 return;
e413e4e9
AM
2867 }
2868 }
293f5f65 2869
ae89daec 2870 if (startswith (string, ".no") && j >= ARRAY_SIZE (cpu_arch))
293f5f65 2871 {
33eaf5de 2872 /* Disable an ISA extension. */
ae89daec
JB
2873 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
2874 if (cpu_arch[j].type == PROCESSOR_NONE
2875 && strcmp (string + 3, cpu_arch[j].name) == 0)
293f5f65
L
2876 {
2877 flags = cpu_flags_and_not (cpu_arch_flags,
ae89daec 2878 cpu_arch[j].disable);
293f5f65
L
2879 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
2880 {
ae89daec 2881 extend_cpu_sub_arch_name (string + 1);
293f5f65
L
2882 cpu_arch_flags = flags;
2883 cpu_arch_isa_flags = flags;
2884 }
2885 (void) restore_line_pointer (e);
2886 demand_empty_rest_of_line ();
2887 return;
2888 }
293f5f65
L
2889 }
2890
3ce2ebcf 2891 if (j == ARRAY_SIZE (cpu_arch))
e413e4e9
AM
2892 as_bad (_("no such architecture: `%s'"), string);
2893
2894 *input_line_pointer = e;
2895 }
2896 else
2897 as_bad (_("missing cpu architecture"));
2898
fddf5b5b
AM
2899 no_cond_jump_promotion = 0;
2900 if (*input_line_pointer == ','
29b0f896 2901 && !is_end_of_line[(unsigned char) input_line_pointer[1]])
fddf5b5b 2902 {
d02603dc
NC
2903 char *string;
2904 char e;
2905
2906 ++input_line_pointer;
2907 e = get_symbol_name (&string);
fddf5b5b
AM
2908
2909 if (strcmp (string, "nojumps") == 0)
2910 no_cond_jump_promotion = 1;
2911 else if (strcmp (string, "jumps") == 0)
2912 ;
2913 else
2914 as_bad (_("no such architecture modifier: `%s'"), string);
2915
d02603dc 2916 (void) restore_line_pointer (e);
fddf5b5b
AM
2917 }
2918
e413e4e9
AM
2919 demand_empty_rest_of_line ();
2920}
2921
8a9036a4
L
2922enum bfd_architecture
2923i386_arch (void)
2924{
c085ab00 2925 if (cpu_arch_isa == PROCESSOR_IAMCU)
81486035
L
2926 {
2927 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2928 || flag_code == CODE_64BIT)
2929 as_fatal (_("Intel MCU is 32bit ELF only"));
2930 return bfd_arch_iamcu;
2931 }
8a9036a4
L
2932 else
2933 return bfd_arch_i386;
2934}
2935
b9d79e03 2936unsigned long
7016a5d5 2937i386_mach (void)
b9d79e03 2938{
d34049e8 2939 if (startswith (default_arch, "x86_64"))
8a9036a4 2940 {
c085ab00 2941 if (default_arch[6] == '\0')
8a9036a4 2942 return bfd_mach_x86_64;
351f65ca
L
2943 else
2944 return bfd_mach_x64_32;
8a9036a4 2945 }
5197d474
L
2946 else if (!strcmp (default_arch, "i386")
2947 || !strcmp (default_arch, "iamcu"))
81486035
L
2948 {
2949 if (cpu_arch_isa == PROCESSOR_IAMCU)
2950 {
2951 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
2952 as_fatal (_("Intel MCU is 32bit ELF only"));
2953 return bfd_mach_i386_iamcu;
2954 }
2955 else
2956 return bfd_mach_i386_i386;
2957 }
b9d79e03 2958 else
2b5d6a91 2959 as_fatal (_("unknown architecture"));
b9d79e03 2960}
b9d79e03 2961\f
252b5132 2962void
7016a5d5 2963md_begin (void)
252b5132 2964{
86fa6981
L
2965 /* Support pseudo prefixes like {disp32}. */
2966 lex_type ['{'] = LEX_BEGIN_NAME;
2967
47926f60 2968 /* Initialize op_hash hash table. */
629310ab 2969 op_hash = str_htab_create ();
252b5132
RH
2970
2971 {
d3ce72d0 2972 const insn_template *optab;
29b0f896 2973 templates *core_optab;
252b5132 2974
47926f60
KH
2975 /* Setup for loop. */
2976 optab = i386_optab;
654d6f31 2977 core_optab = notes_alloc (sizeof (*core_optab));
252b5132
RH
2978 core_optab->start = optab;
2979
2980 while (1)
2981 {
2982 ++optab;
2983 if (optab->name == NULL
2984 || strcmp (optab->name, (optab - 1)->name) != 0)
2985 {
2986 /* different name --> ship out current template list;
47926f60 2987 add to hash table; & begin anew. */
252b5132 2988 core_optab->end = optab;
fe0e921f
AM
2989 if (str_hash_insert (op_hash, (optab - 1)->name, core_optab, 0))
2990 as_fatal (_("duplicate %s"), (optab - 1)->name);
2991
252b5132
RH
2992 if (optab->name == NULL)
2993 break;
654d6f31 2994 core_optab = notes_alloc (sizeof (*core_optab));
252b5132
RH
2995 core_optab->start = optab;
2996 }
2997 }
2998 }
2999
47926f60 3000 /* Initialize reg_hash hash table. */
629310ab 3001 reg_hash = str_htab_create ();
252b5132 3002 {
29b0f896 3003 const reg_entry *regtab;
c3fe08fa 3004 unsigned int regtab_size = i386_regtab_size;
252b5132 3005
c3fe08fa 3006 for (regtab = i386_regtab; regtab_size--; regtab++)
6225c532 3007 {
6288d05f
JB
3008 switch (regtab->reg_type.bitfield.class)
3009 {
3010 case Reg:
34684862
JB
3011 if (regtab->reg_type.bitfield.dword)
3012 {
3013 if (regtab->reg_type.bitfield.instance == Accum)
3014 reg_eax = regtab;
3015 }
3016 else if (regtab->reg_type.bitfield.tbyte)
6288d05f
JB
3017 {
3018 /* There's no point inserting st(<N>) in the hash table, as
3019 parentheses aren't included in register_chars[] anyway. */
3020 if (regtab->reg_type.bitfield.instance != Accum)
3021 continue;
3022 reg_st0 = regtab;
3023 }
3024 break;
3025
5e042380
JB
3026 case SReg:
3027 switch (regtab->reg_num)
3028 {
3029 case 0: reg_es = regtab; break;
3030 case 2: reg_ss = regtab; break;
3031 case 3: reg_ds = regtab; break;
3032 }
3033 break;
3034
6288d05f
JB
3035 case RegMask:
3036 if (!regtab->reg_num)
3037 reg_k0 = regtab;
3038 break;
3039 }
3040
6225c532
JB
3041 if (str_hash_insert (reg_hash, regtab->reg_name, regtab, 0) != NULL)
3042 as_fatal (_("duplicate %s"), regtab->reg_name);
6225c532 3043 }
252b5132
RH
3044 }
3045
47926f60 3046 /* Fill in lexical tables: mnemonic_chars, operand_chars. */
252b5132 3047 {
29b0f896
AM
3048 int c;
3049 char *p;
252b5132
RH
3050
3051 for (c = 0; c < 256; c++)
3052 {
014fbcda 3053 if (ISDIGIT (c) || ISLOWER (c))
252b5132
RH
3054 {
3055 mnemonic_chars[c] = c;
3056 register_chars[c] = c;
3057 operand_chars[c] = c;
3058 }
3882b010 3059 else if (ISUPPER (c))
252b5132 3060 {
3882b010 3061 mnemonic_chars[c] = TOLOWER (c);
252b5132
RH
3062 register_chars[c] = mnemonic_chars[c];
3063 operand_chars[c] = c;
3064 }
43234a1e 3065 else if (c == '{' || c == '}')
86fa6981
L
3066 {
3067 mnemonic_chars[c] = c;
3068 operand_chars[c] = c;
3069 }
b3983e5f
JB
3070#ifdef SVR4_COMMENT_CHARS
3071 else if (c == '\\' && strchr (i386_comment_chars, '/'))
3072 operand_chars[c] = c;
3073#endif
252b5132 3074
3882b010 3075 if (ISALPHA (c) || ISDIGIT (c))
252b5132
RH
3076 identifier_chars[c] = c;
3077 else if (c >= 128)
3078 {
3079 identifier_chars[c] = c;
3080 operand_chars[c] = c;
3081 }
3082 }
3083
3084#ifdef LEX_AT
3085 identifier_chars['@'] = '@';
32137342
NC
3086#endif
3087#ifdef LEX_QM
3088 identifier_chars['?'] = '?';
3089 operand_chars['?'] = '?';
252b5132 3090#endif
c0f3af97 3091 mnemonic_chars['_'] = '_';
791fe849 3092 mnemonic_chars['-'] = '-';
0003779b 3093 mnemonic_chars['.'] = '.';
252b5132
RH
3094 identifier_chars['_'] = '_';
3095 identifier_chars['.'] = '.';
3096
3097 for (p = operand_special_chars; *p != '\0'; p++)
3098 operand_chars[(unsigned char) *p] = *p;
3099 }
3100
a4447b93
RH
3101 if (flag_code == CODE_64BIT)
3102 {
ca19b261
KT
3103#if defined (OBJ_COFF) && defined (TE_PE)
3104 x86_dwarf2_return_column = (OUTPUT_FLAVOR == bfd_target_coff_flavour
3105 ? 32 : 16);
3106#else
a4447b93 3107 x86_dwarf2_return_column = 16;
ca19b261 3108#endif
61ff971f 3109 x86_cie_data_alignment = -8;
b52c4ee4
IB
3110#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3111 x86_sframe_cfa_sp_reg = 7;
3112 x86_sframe_cfa_fp_reg = 6;
3113#endif
a4447b93
RH
3114 }
3115 else
3116 {
3117 x86_dwarf2_return_column = 8;
3118 x86_cie_data_alignment = -4;
3119 }
e379e5f3
L
3120
3121 /* NB: FUSED_JCC_PADDING frag must have sufficient room so that it
3122 can be turned into BRANCH_PREFIX frag. */
3123 if (align_branch_prefix_size > MAX_FUSED_JCC_PADDING_SIZE)
3124 abort ();
252b5132
RH
3125}
3126
3127void
e3bb37b5 3128i386_print_statistics (FILE *file)
252b5132 3129{
629310ab
ML
3130 htab_print_statistics (file, "i386 opcode", op_hash);
3131 htab_print_statistics (file, "i386 register", reg_hash);
252b5132 3132}
654d6f31
AM
3133
3134void
3135i386_md_end (void)
3136{
3137 htab_delete (op_hash);
3138 htab_delete (reg_hash);
3139}
252b5132 3140\f
252b5132
RH
3141#ifdef DEBUG386
3142
ce8a8b2f 3143/* Debugging routines for md_assemble. */
d3ce72d0 3144static void pte (insn_template *);
40fb9820 3145static void pt (i386_operand_type);
e3bb37b5
L
3146static void pe (expressionS *);
3147static void ps (symbolS *);
252b5132
RH
3148
3149static void
2c703856 3150pi (const char *line, i386_insn *x)
252b5132 3151{
09137c09 3152 unsigned int j;
252b5132
RH
3153
3154 fprintf (stdout, "%s: template ", line);
3155 pte (&x->tm);
09f131f2
JH
3156 fprintf (stdout, " address: base %s index %s scale %x\n",
3157 x->base_reg ? x->base_reg->reg_name : "none",
3158 x->index_reg ? x->index_reg->reg_name : "none",
3159 x->log2_scale_factor);
3160 fprintf (stdout, " modrm: mode %x reg %x reg/mem %x\n",
252b5132 3161 x->rm.mode, x->rm.reg, x->rm.regmem);
09f131f2
JH
3162 fprintf (stdout, " sib: base %x index %x scale %x\n",
3163 x->sib.base, x->sib.index, x->sib.scale);
3164 fprintf (stdout, " rex: 64bit %x extX %x extY %x extZ %x\n",
161a04f6
L
3165 (x->rex & REX_W) != 0,
3166 (x->rex & REX_R) != 0,
3167 (x->rex & REX_X) != 0,
3168 (x->rex & REX_B) != 0);
09137c09 3169 for (j = 0; j < x->operands; j++)
252b5132 3170 {
09137c09
SP
3171 fprintf (stdout, " #%d: ", j + 1);
3172 pt (x->types[j]);
252b5132 3173 fprintf (stdout, "\n");
bab6aec1 3174 if (x->types[j].bitfield.class == Reg
3528c362
JB
3175 || x->types[j].bitfield.class == RegMMX
3176 || x->types[j].bitfield.class == RegSIMD
dd6b8a0b 3177 || x->types[j].bitfield.class == RegMask
00cee14f 3178 || x->types[j].bitfield.class == SReg
4a5c67ed
JB
3179 || x->types[j].bitfield.class == RegCR
3180 || x->types[j].bitfield.class == RegDR
dd6b8a0b
JB
3181 || x->types[j].bitfield.class == RegTR
3182 || x->types[j].bitfield.class == RegBND)
09137c09
SP
3183 fprintf (stdout, "%s\n", x->op[j].regs->reg_name);
3184 if (operand_type_check (x->types[j], imm))
3185 pe (x->op[j].imms);
3186 if (operand_type_check (x->types[j], disp))
3187 pe (x->op[j].disps);
252b5132
RH
3188 }
3189}
3190
3191static void
d3ce72d0 3192pte (insn_template *t)
252b5132 3193{
b933fa4b 3194 static const unsigned char opc_pfx[] = { 0, 0x66, 0xf3, 0xf2 };
441f6aca 3195 static const char *const opc_spc[] = {
0cc78721 3196 NULL, "0f", "0f38", "0f3a", NULL, "evexmap5", "evexmap6", NULL,
441f6aca
JB
3197 "XOP08", "XOP09", "XOP0A",
3198 };
09137c09 3199 unsigned int j;
441f6aca 3200
252b5132 3201 fprintf (stdout, " %d operands ", t->operands);
441f6aca
JB
3202 if (opc_pfx[t->opcode_modifier.opcodeprefix])
3203 fprintf (stdout, "pfx %x ", opc_pfx[t->opcode_modifier.opcodeprefix]);
3204 if (opc_spc[t->opcode_modifier.opcodespace])
3205 fprintf (stdout, "space %s ", opc_spc[t->opcode_modifier.opcodespace]);
47926f60 3206 fprintf (stdout, "opcode %x ", t->base_opcode);
252b5132
RH
3207 if (t->extension_opcode != None)
3208 fprintf (stdout, "ext %x ", t->extension_opcode);
40fb9820 3209 if (t->opcode_modifier.d)
252b5132 3210 fprintf (stdout, "D");
40fb9820 3211 if (t->opcode_modifier.w)
252b5132
RH
3212 fprintf (stdout, "W");
3213 fprintf (stdout, "\n");
09137c09 3214 for (j = 0; j < t->operands; j++)
252b5132 3215 {
09137c09
SP
3216 fprintf (stdout, " #%d type ", j + 1);
3217 pt (t->operand_types[j]);
252b5132
RH
3218 fprintf (stdout, "\n");
3219 }
3220}
3221
3222static void
e3bb37b5 3223pe (expressionS *e)
252b5132 3224{
24eab124 3225 fprintf (stdout, " operation %d\n", e->X_op);
b8281767
AM
3226 fprintf (stdout, " add_number %" PRId64 " (%" PRIx64 ")\n",
3227 (int64_t) e->X_add_number, (uint64_t) (valueT) e->X_add_number);
252b5132
RH
3228 if (e->X_add_symbol)
3229 {
3230 fprintf (stdout, " add_symbol ");
3231 ps (e->X_add_symbol);
3232 fprintf (stdout, "\n");
3233 }
3234 if (e->X_op_symbol)
3235 {
3236 fprintf (stdout, " op_symbol ");
3237 ps (e->X_op_symbol);
3238 fprintf (stdout, "\n");
3239 }
3240}
3241
3242static void
e3bb37b5 3243ps (symbolS *s)
252b5132
RH
3244{
3245 fprintf (stdout, "%s type %s%s",
3246 S_GET_NAME (s),
3247 S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
3248 segment_name (S_GET_SEGMENT (s)));
3249}
3250
7b81dfbb 3251static struct type_name
252b5132 3252 {
40fb9820
L
3253 i386_operand_type mask;
3254 const char *name;
252b5132 3255 }
7b81dfbb 3256const type_names[] =
252b5132 3257{
40fb9820
L
3258 { OPERAND_TYPE_REG8, "r8" },
3259 { OPERAND_TYPE_REG16, "r16" },
3260 { OPERAND_TYPE_REG32, "r32" },
3261 { OPERAND_TYPE_REG64, "r64" },
2c703856
JB
3262 { OPERAND_TYPE_ACC8, "acc8" },
3263 { OPERAND_TYPE_ACC16, "acc16" },
3264 { OPERAND_TYPE_ACC32, "acc32" },
3265 { OPERAND_TYPE_ACC64, "acc64" },
40fb9820
L
3266 { OPERAND_TYPE_IMM8, "i8" },
3267 { OPERAND_TYPE_IMM8, "i8s" },
3268 { OPERAND_TYPE_IMM16, "i16" },
3269 { OPERAND_TYPE_IMM32, "i32" },
3270 { OPERAND_TYPE_IMM32S, "i32s" },
3271 { OPERAND_TYPE_IMM64, "i64" },
3272 { OPERAND_TYPE_IMM1, "i1" },
3273 { OPERAND_TYPE_BASEINDEX, "BaseIndex" },
3274 { OPERAND_TYPE_DISP8, "d8" },
3275 { OPERAND_TYPE_DISP16, "d16" },
3276 { OPERAND_TYPE_DISP32, "d32" },
40fb9820
L
3277 { OPERAND_TYPE_DISP64, "d64" },
3278 { OPERAND_TYPE_INOUTPORTREG, "InOutPortReg" },
3279 { OPERAND_TYPE_SHIFTCOUNT, "ShiftCount" },
3280 { OPERAND_TYPE_CONTROL, "control reg" },
3281 { OPERAND_TYPE_TEST, "test reg" },
3282 { OPERAND_TYPE_DEBUG, "debug reg" },
3283 { OPERAND_TYPE_FLOATREG, "FReg" },
3284 { OPERAND_TYPE_FLOATACC, "FAcc" },
21df382b 3285 { OPERAND_TYPE_SREG, "SReg" },
40fb9820
L
3286 { OPERAND_TYPE_REGMMX, "rMMX" },
3287 { OPERAND_TYPE_REGXMM, "rXMM" },
0349dc08 3288 { OPERAND_TYPE_REGYMM, "rYMM" },
43234a1e 3289 { OPERAND_TYPE_REGZMM, "rZMM" },
260cd341 3290 { OPERAND_TYPE_REGTMM, "rTMM" },
43234a1e 3291 { OPERAND_TYPE_REGMASK, "Mask reg" },
252b5132
RH
3292};
3293
3294static void
40fb9820 3295pt (i386_operand_type t)
252b5132 3296{
40fb9820 3297 unsigned int j;
c6fb90c8 3298 i386_operand_type a;
252b5132 3299
40fb9820 3300 for (j = 0; j < ARRAY_SIZE (type_names); j++)
c6fb90c8
L
3301 {
3302 a = operand_type_and (t, type_names[j].mask);
2c703856 3303 if (operand_type_equal (&a, &type_names[j].mask))
c6fb90c8
L
3304 fprintf (stdout, "%s, ", type_names[j].name);
3305 }
252b5132
RH
3306 fflush (stdout);
3307}
3308
3309#endif /* DEBUG386 */
3310\f
252b5132 3311static bfd_reloc_code_real_type
3956db08 3312reloc (unsigned int size,
64e74474
AM
3313 int pcrel,
3314 int sign,
3315 bfd_reloc_code_real_type other)
252b5132 3316{
47926f60 3317 if (other != NO_RELOC)
3956db08 3318 {
91d6fa6a 3319 reloc_howto_type *rel;
3956db08
JB
3320
3321 if (size == 8)
3322 switch (other)
3323 {
64e74474
AM
3324 case BFD_RELOC_X86_64_GOT32:
3325 return BFD_RELOC_X86_64_GOT64;
3326 break;
553d1284
L
3327 case BFD_RELOC_X86_64_GOTPLT64:
3328 return BFD_RELOC_X86_64_GOTPLT64;
3329 break;
64e74474
AM
3330 case BFD_RELOC_X86_64_PLTOFF64:
3331 return BFD_RELOC_X86_64_PLTOFF64;
3332 break;
3333 case BFD_RELOC_X86_64_GOTPC32:
3334 other = BFD_RELOC_X86_64_GOTPC64;
3335 break;
3336 case BFD_RELOC_X86_64_GOTPCREL:
3337 other = BFD_RELOC_X86_64_GOTPCREL64;
3338 break;
3339 case BFD_RELOC_X86_64_TPOFF32:
3340 other = BFD_RELOC_X86_64_TPOFF64;
3341 break;
3342 case BFD_RELOC_X86_64_DTPOFF32:
3343 other = BFD_RELOC_X86_64_DTPOFF64;
3344 break;
3345 default:
3346 break;
3956db08 3347 }
e05278af 3348
8ce3d284 3349#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
3350 if (other == BFD_RELOC_SIZE32)
3351 {
3352 if (size == 8)
1ab668bf 3353 other = BFD_RELOC_SIZE64;
8fd4256d 3354 if (pcrel)
1ab668bf
AM
3355 {
3356 as_bad (_("there are no pc-relative size relocations"));
3357 return NO_RELOC;
3358 }
8fd4256d 3359 }
8ce3d284 3360#endif
8fd4256d 3361
e05278af 3362 /* Sign-checking 4-byte relocations in 16-/32-bit code is pointless. */
f2d8a97c 3363 if (size == 4 && (flag_code != CODE_64BIT || disallow_64bit_reloc))
e05278af
JB
3364 sign = -1;
3365
91d6fa6a
NC
3366 rel = bfd_reloc_type_lookup (stdoutput, other);
3367 if (!rel)
3956db08 3368 as_bad (_("unknown relocation (%u)"), other);
91d6fa6a 3369 else if (size != bfd_get_reloc_size (rel))
3956db08 3370 as_bad (_("%u-byte relocation cannot be applied to %u-byte field"),
91d6fa6a 3371 bfd_get_reloc_size (rel),
3956db08 3372 size);
91d6fa6a 3373 else if (pcrel && !rel->pc_relative)
3956db08 3374 as_bad (_("non-pc-relative relocation for pc-relative field"));
91d6fa6a 3375 else if ((rel->complain_on_overflow == complain_overflow_signed
3956db08 3376 && !sign)
91d6fa6a 3377 || (rel->complain_on_overflow == complain_overflow_unsigned
64e74474 3378 && sign > 0))
3956db08
JB
3379 as_bad (_("relocated field and relocation type differ in signedness"));
3380 else
3381 return other;
3382 return NO_RELOC;
3383 }
252b5132
RH
3384
3385 if (pcrel)
3386 {
3e73aa7c 3387 if (!sign)
3956db08 3388 as_bad (_("there are no unsigned pc-relative relocations"));
252b5132
RH
3389 switch (size)
3390 {
3391 case 1: return BFD_RELOC_8_PCREL;
3392 case 2: return BFD_RELOC_16_PCREL;
d258b828 3393 case 4: return BFD_RELOC_32_PCREL;
d6ab8113 3394 case 8: return BFD_RELOC_64_PCREL;
252b5132 3395 }
3956db08 3396 as_bad (_("cannot do %u byte pc-relative relocation"), size);
252b5132
RH
3397 }
3398 else
3399 {
3956db08 3400 if (sign > 0)
e5cb08ac 3401 switch (size)
3e73aa7c
JH
3402 {
3403 case 4: return BFD_RELOC_X86_64_32S;
3404 }
3405 else
3406 switch (size)
3407 {
3408 case 1: return BFD_RELOC_8;
3409 case 2: return BFD_RELOC_16;
3410 case 4: return BFD_RELOC_32;
3411 case 8: return BFD_RELOC_64;
3412 }
3956db08
JB
3413 as_bad (_("cannot do %s %u byte relocation"),
3414 sign > 0 ? "signed" : "unsigned", size);
252b5132
RH
3415 }
3416
0cc9e1d3 3417 return NO_RELOC;
252b5132
RH
3418}
3419
47926f60
KH
3420/* Here we decide which fixups can be adjusted to make them relative to
3421 the beginning of the section instead of the symbol. Basically we need
3422 to make sure that the dynamic relocations are done correctly, so in
3423 some cases we force the original symbol to be used. */
3424
252b5132 3425int
e3bb37b5 3426tc_i386_fix_adjustable (fixS *fixP ATTRIBUTE_UNUSED)
252b5132 3427{
6d249963 3428#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 3429 if (!IS_ELF)
31312f95
AM
3430 return 1;
3431
a161fe53
AM
3432 /* Don't adjust pc-relative references to merge sections in 64-bit
3433 mode. */
3434 if (use_rela_relocations
3435 && (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0
3436 && fixP->fx_pcrel)
252b5132 3437 return 0;
31312f95 3438
8d01d9a9
AJ
3439 /* The x86_64 GOTPCREL are represented as 32bit PCrel relocations
3440 and changed later by validate_fix. */
3441 if (GOT_symbol && fixP->fx_subsy == GOT_symbol
3442 && fixP->fx_r_type == BFD_RELOC_32_PCREL)
3443 return 0;
3444
8fd4256d
L
3445 /* Adjust_reloc_syms doesn't know about the GOT. Need to keep symbol
3446 for size relocations. */
3447 if (fixP->fx_r_type == BFD_RELOC_SIZE32
3448 || fixP->fx_r_type == BFD_RELOC_SIZE64
3449 || fixP->fx_r_type == BFD_RELOC_386_GOTOFF
252b5132 3450 || fixP->fx_r_type == BFD_RELOC_386_GOT32
02a86693 3451 || fixP->fx_r_type == BFD_RELOC_386_GOT32X
13ae64f3
JJ
3452 || fixP->fx_r_type == BFD_RELOC_386_TLS_GD
3453 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDM
3454 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDO_32
3455 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE_32
37e55690
JJ
3456 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE
3457 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTIE
13ae64f3
JJ
3458 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE_32
3459 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE
67a4f2b7
AO
3460 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTDESC
3461 || fixP->fx_r_type == BFD_RELOC_386_TLS_DESC_CALL
3e73aa7c 3462 || fixP->fx_r_type == BFD_RELOC_X86_64_GOT32
80b3ee89 3463 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCREL
56ceb5b5
L
3464 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCRELX
3465 || fixP->fx_r_type == BFD_RELOC_X86_64_REX_GOTPCRELX
bffbf940
JJ
3466 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSGD
3467 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSLD
3468 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF32
d6ab8113 3469 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF64
bffbf940
JJ
3470 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF
3471 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF32
d6ab8113
JB
3472 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF64
3473 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTOFF64
67a4f2b7
AO
3474 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
3475 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSDESC_CALL
252b5132
RH
3476 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3477 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3478 return 0;
31312f95 3479#endif
252b5132
RH
3480 return 1;
3481}
252b5132 3482
a9aabc23
JB
3483static INLINE bool
3484want_disp32 (const insn_template *t)
3485{
3486 return flag_code != CODE_64BIT
3487 || i.prefix[ADDR_PREFIX]
3488 || (t->base_opcode == 0x8d
3489 && t->opcode_modifier.opcodespace == SPACE_BASE
fe134c65
JB
3490 && (!i.types[1].bitfield.qword
3491 || t->opcode_modifier.size == SIZE32));
a9aabc23
JB
3492}
3493
b4cac588 3494static int
e3bb37b5 3495intel_float_operand (const char *mnemonic)
252b5132 3496{
9306ca4a
JB
3497 /* Note that the value returned is meaningful only for opcodes with (memory)
3498 operands, hence the code here is free to improperly handle opcodes that
3499 have no operands (for better performance and smaller code). */
3500
3501 if (mnemonic[0] != 'f')
3502 return 0; /* non-math */
3503
3504 switch (mnemonic[1])
3505 {
3506 /* fclex, fdecstp, fdisi, femms, feni, fincstp, finit, fsetpm, and
3507 the fs segment override prefix not currently handled because no
3508 call path can make opcodes without operands get here */
3509 case 'i':
3510 return 2 /* integer op */;
3511 case 'l':
3512 if (mnemonic[2] == 'd' && (mnemonic[3] == 'c' || mnemonic[3] == 'e'))
3513 return 3; /* fldcw/fldenv */
3514 break;
3515 case 'n':
3516 if (mnemonic[2] != 'o' /* fnop */)
3517 return 3; /* non-waiting control op */
3518 break;
3519 case 'r':
3520 if (mnemonic[2] == 's')
3521 return 3; /* frstor/frstpm */
3522 break;
3523 case 's':
3524 if (mnemonic[2] == 'a')
3525 return 3; /* fsave */
3526 if (mnemonic[2] == 't')
3527 {
3528 switch (mnemonic[3])
3529 {
3530 case 'c': /* fstcw */
3531 case 'd': /* fstdw */
3532 case 'e': /* fstenv */
3533 case 's': /* fsts[gw] */
3534 return 3;
3535 }
3536 }
3537 break;
3538 case 'x':
3539 if (mnemonic[2] == 'r' || mnemonic[2] == 's')
3540 return 0; /* fxsave/fxrstor are not really math ops */
3541 break;
3542 }
252b5132 3543
9306ca4a 3544 return 1;
252b5132
RH
3545}
3546
9a182d04
JB
3547static INLINE void
3548install_template (const insn_template *t)
3549{
3550 unsigned int l;
3551
3552 i.tm = *t;
3553
3554 /* Note that for pseudo prefixes this produces a length of 1. But for them
3555 the length isn't interesting at all. */
3556 for (l = 1; l < 4; ++l)
3557 if (!(t->base_opcode >> (8 * l)))
3558 break;
3559
3560 i.opcode_length = l;
3561}
3562
c0f3af97
L
3563/* Build the VEX prefix. */
3564
3565static void
d3ce72d0 3566build_vex_prefix (const insn_template *t)
c0f3af97
L
3567{
3568 unsigned int register_specifier;
c0f3af97 3569 unsigned int vector_length;
03751133 3570 unsigned int w;
c0f3af97
L
3571
3572 /* Check register specifier. */
3573 if (i.vex.register_specifier)
43234a1e
L
3574 {
3575 register_specifier =
3576 ~register_number (i.vex.register_specifier) & 0xf;
3577 gas_assert ((i.vex.register_specifier->reg_flags & RegVRex) == 0);
3578 }
c0f3af97
L
3579 else
3580 register_specifier = 0xf;
3581
79f0fa25
L
3582 /* Use 2-byte VEX prefix by swapping destination and source operand
3583 if there are more than 1 register operand. */
3584 if (i.reg_operands > 1
3585 && i.vec_encoding != vex_encoding_vex3
86fa6981 3586 && i.dir_encoding == dir_encoding_default
fa99fab2 3587 && i.operands == i.reg_operands
dbbc8b7e 3588 && operand_type_equal (&i.types[0], &i.types[i.operands - 1])
441f6aca 3589 && i.tm.opcode_modifier.opcodespace == SPACE_0F
dbbc8b7e 3590 && (i.tm.opcode_modifier.load || i.tm.opcode_modifier.d)
fa99fab2
L
3591 && i.rex == REX_B)
3592 {
3593 unsigned int xchg = i.operands - 1;
3594 union i386_op temp_op;
3595 i386_operand_type temp_type;
3596
3597 temp_type = i.types[xchg];
3598 i.types[xchg] = i.types[0];
3599 i.types[0] = temp_type;
3600 temp_op = i.op[xchg];
3601 i.op[xchg] = i.op[0];
3602 i.op[0] = temp_op;
3603
9c2799c2 3604 gas_assert (i.rm.mode == 3);
fa99fab2
L
3605
3606 i.rex = REX_R;
3607 xchg = i.rm.regmem;
3608 i.rm.regmem = i.rm.reg;
3609 i.rm.reg = xchg;
3610
dbbc8b7e
JB
3611 if (i.tm.opcode_modifier.d)
3612 i.tm.base_opcode ^= (i.tm.base_opcode & 0xee) != 0x6e
2c735193 3613 ? Opcode_ExtD : Opcode_SIMD_IntD;
dbbc8b7e 3614 else /* Use the next insn. */
9a182d04 3615 install_template (&t[1]);
fa99fab2
L
3616 }
3617
79dec6b7
JB
3618 /* Use 2-byte VEX prefix by swapping commutative source operands if there
3619 are no memory operands and at least 3 register ones. */
3620 if (i.reg_operands >= 3
3621 && i.vec_encoding != vex_encoding_vex3
3622 && i.reg_operands == i.operands - i.imm_operands
3623 && i.tm.opcode_modifier.vex
3624 && i.tm.opcode_modifier.commutative
3625 && (i.tm.opcode_modifier.sse2avx || optimize > 1)
3626 && i.rex == REX_B
3627 && i.vex.register_specifier
3628 && !(i.vex.register_specifier->reg_flags & RegRex))
3629 {
3630 unsigned int xchg = i.operands - i.reg_operands;
3631 union i386_op temp_op;
3632 i386_operand_type temp_type;
3633
441f6aca 3634 gas_assert (i.tm.opcode_modifier.opcodespace == SPACE_0F);
79dec6b7
JB
3635 gas_assert (!i.tm.opcode_modifier.sae);
3636 gas_assert (operand_type_equal (&i.types[i.operands - 2],
3637 &i.types[i.operands - 3]));
3638 gas_assert (i.rm.mode == 3);
3639
3640 temp_type = i.types[xchg];
3641 i.types[xchg] = i.types[xchg + 1];
3642 i.types[xchg + 1] = temp_type;
3643 temp_op = i.op[xchg];
3644 i.op[xchg] = i.op[xchg + 1];
3645 i.op[xchg + 1] = temp_op;
3646
3647 i.rex = 0;
3648 xchg = i.rm.regmem | 8;
3649 i.rm.regmem = ~register_specifier & 0xf;
3650 gas_assert (!(i.rm.regmem & 8));
3651 i.vex.register_specifier += xchg - i.rm.regmem;
3652 register_specifier = ~xchg & 0xf;
3653 }
3654
539f890d
L
3655 if (i.tm.opcode_modifier.vex == VEXScalar)
3656 vector_length = avxscalar;
10c17abd
JB
3657 else if (i.tm.opcode_modifier.vex == VEX256)
3658 vector_length = 1;
539f890d 3659 else
10c17abd 3660 {
56522fc5 3661 unsigned int op;
10c17abd 3662
c7213af9
L
3663 /* Determine vector length from the last multi-length vector
3664 operand. */
10c17abd 3665 vector_length = 0;
56522fc5 3666 for (op = t->operands; op--;)
10c17abd
JB
3667 if (t->operand_types[op].bitfield.xmmword
3668 && t->operand_types[op].bitfield.ymmword
3669 && i.types[op].bitfield.ymmword)
3670 {
3671 vector_length = 1;
3672 break;
3673 }
3674 }
c0f3af97 3675
03751133
L
3676 /* Check the REX.W bit and VEXW. */
3677 if (i.tm.opcode_modifier.vexw == VEXWIG)
3678 w = (vexwig == vexw1 || (i.rex & REX_W)) ? 1 : 0;
3679 else if (i.tm.opcode_modifier.vexw)
3680 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3681 else
931d03b7 3682 w = (flag_code == CODE_64BIT ? i.rex & REX_W : vexwig == vexw1) ? 1 : 0;
03751133 3683
c0f3af97 3684 /* Use 2-byte VEX prefix if possible. */
03751133
L
3685 if (w == 0
3686 && i.vec_encoding != vex_encoding_vex3
441f6aca 3687 && i.tm.opcode_modifier.opcodespace == SPACE_0F
c0f3af97
L
3688 && (i.rex & (REX_W | REX_X | REX_B)) == 0)
3689 {
3690 /* 2-byte VEX prefix. */
3691 unsigned int r;
3692
3693 i.vex.length = 2;
3694 i.vex.bytes[0] = 0xc5;
3695
3696 /* Check the REX.R bit. */
3697 r = (i.rex & REX_R) ? 0 : 1;
3698 i.vex.bytes[1] = (r << 7
3699 | register_specifier << 3
3700 | vector_length << 2
35648716 3701 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3702 }
3703 else
3704 {
3705 /* 3-byte VEX prefix. */
f88c9eb0 3706 i.vex.length = 3;
f88c9eb0 3707
441f6aca 3708 switch (i.tm.opcode_modifier.opcodespace)
5dd85c99 3709 {
441f6aca
JB
3710 case SPACE_0F:
3711 case SPACE_0F38:
3712 case SPACE_0F3A:
80de6e00 3713 i.vex.bytes[0] = 0xc4;
7f399153 3714 break;
441f6aca
JB
3715 case SPACE_XOP08:
3716 case SPACE_XOP09:
3717 case SPACE_XOP0A:
f88c9eb0 3718 i.vex.bytes[0] = 0x8f;
7f399153
L
3719 break;
3720 default:
3721 abort ();
f88c9eb0 3722 }
c0f3af97 3723
c0f3af97
L
3724 /* The high 3 bits of the second VEX byte are 1's compliment
3725 of RXB bits from REX. */
441f6aca 3726 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | i.tm.opcode_modifier.opcodespace;
c0f3af97 3727
c0f3af97
L
3728 i.vex.bytes[2] = (w << 7
3729 | register_specifier << 3
3730 | vector_length << 2
35648716 3731 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3732 }
3733}
3734
5b7c81bd 3735static INLINE bool
e771e7c9
JB
3736is_evex_encoding (const insn_template *t)
3737{
7091c612 3738 return t->opcode_modifier.evex || t->opcode_modifier.disp8memshift
e771e7c9 3739 || t->opcode_modifier.broadcast || t->opcode_modifier.masking
a80195f1 3740 || t->opcode_modifier.sae;
e771e7c9
JB
3741}
3742
5b7c81bd 3743static INLINE bool
7a8655d2
JB
3744is_any_vex_encoding (const insn_template *t)
3745{
7b47a312 3746 return t->opcode_modifier.vex || is_evex_encoding (t);
7a8655d2
JB
3747}
3748
a5748e0d
JB
3749static unsigned int
3750get_broadcast_bytes (const insn_template *t, bool diag)
3751{
3752 unsigned int op, bytes;
3753 const i386_operand_type *types;
3754
3755 if (i.broadcast.type)
3756 return i.broadcast.bytes = ((1 << (t->opcode_modifier.broadcast - 1))
3757 * i.broadcast.type);
3758
3759 gas_assert (intel_syntax);
3760
3761 for (op = 0; op < t->operands; ++op)
3762 if (t->operand_types[op].bitfield.baseindex)
3763 break;
3764
3765 gas_assert (op < t->operands);
3766
3767 if (t->opcode_modifier.evex
3768 && t->opcode_modifier.evex != EVEXDYN)
3769 switch (i.broadcast.bytes)
3770 {
3771 case 1:
3772 if (t->operand_types[op].bitfield.word)
3773 return 2;
3774 /* Fall through. */
3775 case 2:
3776 if (t->operand_types[op].bitfield.dword)
3777 return 4;
3778 /* Fall through. */
3779 case 4:
3780 if (t->operand_types[op].bitfield.qword)
3781 return 8;
3782 /* Fall through. */
3783 case 8:
3784 if (t->operand_types[op].bitfield.xmmword)
3785 return 16;
3786 if (t->operand_types[op].bitfield.ymmword)
3787 return 32;
3788 if (t->operand_types[op].bitfield.zmmword)
3789 return 64;
3790 /* Fall through. */
3791 default:
3792 abort ();
3793 }
3794
3795 gas_assert (op + 1 < t->operands);
3796
3797 if (t->operand_types[op + 1].bitfield.xmmword
3798 + t->operand_types[op + 1].bitfield.ymmword
3799 + t->operand_types[op + 1].bitfield.zmmword > 1)
3800 {
3801 types = &i.types[op + 1];
3802 diag = false;
3803 }
3804 else /* Ambiguous - guess with a preference to non-AVX512VL forms. */
3805 types = &t->operand_types[op];
3806
3807 if (types->bitfield.zmmword)
3808 bytes = 64;
3809 else if (types->bitfield.ymmword)
3810 bytes = 32;
3811 else
3812 bytes = 16;
3813
3814 if (diag)
3815 as_warn (_("ambiguous broadcast for `%s', using %u-bit form"),
3816 t->name, bytes * 8);
3817
3818 return bytes;
3819}
3820
43234a1e
L
3821/* Build the EVEX prefix. */
3822
3823static void
3824build_evex_prefix (void)
3825{
35648716 3826 unsigned int register_specifier, w;
43234a1e
L
3827 rex_byte vrex_used = 0;
3828
3829 /* Check register specifier. */
3830 if (i.vex.register_specifier)
3831 {
3832 gas_assert ((i.vrex & REX_X) == 0);
3833
3834 register_specifier = i.vex.register_specifier->reg_num;
3835 if ((i.vex.register_specifier->reg_flags & RegRex))
3836 register_specifier += 8;
3837 /* The upper 16 registers are encoded in the fourth byte of the
3838 EVEX prefix. */
3839 if (!(i.vex.register_specifier->reg_flags & RegVRex))
3840 i.vex.bytes[3] = 0x8;
3841 register_specifier = ~register_specifier & 0xf;
3842 }
3843 else
3844 {
3845 register_specifier = 0xf;
3846
3847 /* Encode upper 16 vector index register in the fourth byte of
3848 the EVEX prefix. */
3849 if (!(i.vrex & REX_X))
3850 i.vex.bytes[3] = 0x8;
3851 else
3852 vrex_used |= REX_X;
3853 }
3854
43234a1e
L
3855 /* 4 byte EVEX prefix. */
3856 i.vex.length = 4;
3857 i.vex.bytes[0] = 0x62;
3858
43234a1e
L
3859 /* The high 3 bits of the second EVEX byte are 1's compliment of RXB
3860 bits from REX. */
441f6aca 3861 gas_assert (i.tm.opcode_modifier.opcodespace >= SPACE_0F);
0cc78721 3862 gas_assert (i.tm.opcode_modifier.opcodespace <= SPACE_EVEXMAP6);
441f6aca 3863 i.vex.bytes[1] = (~i.rex & 0x7) << 5 | i.tm.opcode_modifier.opcodespace;
43234a1e
L
3864
3865 /* The fifth bit of the second EVEX byte is 1's compliment of the
3866 REX_R bit in VREX. */
3867 if (!(i.vrex & REX_R))
3868 i.vex.bytes[1] |= 0x10;
3869 else
3870 vrex_used |= REX_R;
3871
3872 if ((i.reg_operands + i.imm_operands) == i.operands)
3873 {
3874 /* When all operands are registers, the REX_X bit in REX is not
3875 used. We reuse it to encode the upper 16 registers, which is
3876 indicated by the REX_B bit in VREX. The REX_X bit is encoded
3877 as 1's compliment. */
3878 if ((i.vrex & REX_B))
3879 {
3880 vrex_used |= REX_B;
3881 i.vex.bytes[1] &= ~0x40;
3882 }
3883 }
3884
3885 /* EVEX instructions shouldn't need the REX prefix. */
3886 i.vrex &= ~vrex_used;
3887 gas_assert (i.vrex == 0);
3888
6865c043
L
3889 /* Check the REX.W bit and VEXW. */
3890 if (i.tm.opcode_modifier.vexw == VEXWIG)
3891 w = (evexwig == evexw1 || (i.rex & REX_W)) ? 1 : 0;
3892 else if (i.tm.opcode_modifier.vexw)
3893 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3894 else
931d03b7 3895 w = (flag_code == CODE_64BIT ? i.rex & REX_W : evexwig == evexw1) ? 1 : 0;
43234a1e 3896
43234a1e 3897 /* The third byte of the EVEX prefix. */
35648716
JB
3898 i.vex.bytes[2] = ((w << 7)
3899 | (register_specifier << 3)
3900 | 4 /* Encode the U bit. */
3901 | i.tm.opcode_modifier.opcodeprefix);
43234a1e
L
3902
3903 /* The fourth byte of the EVEX prefix. */
3904 /* The zeroing-masking bit. */
6225c532 3905 if (i.mask.reg && i.mask.zeroing)
43234a1e
L
3906 i.vex.bytes[3] |= 0x80;
3907
3908 /* Don't always set the broadcast bit if there is no RC. */
ca5312a2 3909 if (i.rounding.type == rc_none)
43234a1e
L
3910 {
3911 /* Encode the vector length. */
3912 unsigned int vec_length;
3913
e771e7c9
JB
3914 if (!i.tm.opcode_modifier.evex
3915 || i.tm.opcode_modifier.evex == EVEXDYN)
3916 {
56522fc5 3917 unsigned int op;
e771e7c9 3918
c7213af9
L
3919 /* Determine vector length from the last multi-length vector
3920 operand. */
56522fc5 3921 for (op = i.operands; op--;)
e771e7c9
JB
3922 if (i.tm.operand_types[op].bitfield.xmmword
3923 + i.tm.operand_types[op].bitfield.ymmword
3924 + i.tm.operand_types[op].bitfield.zmmword > 1)
3925 {
3926 if (i.types[op].bitfield.zmmword)
c7213af9
L
3927 {
3928 i.tm.opcode_modifier.evex = EVEX512;
3929 break;
3930 }
e771e7c9 3931 else if (i.types[op].bitfield.ymmword)
c7213af9
L
3932 {
3933 i.tm.opcode_modifier.evex = EVEX256;
3934 break;
3935 }
e771e7c9 3936 else if (i.types[op].bitfield.xmmword)
c7213af9
L
3937 {
3938 i.tm.opcode_modifier.evex = EVEX128;
3939 break;
3940 }
a5748e0d 3941 else if (i.broadcast.bytes && op == i.broadcast.operand)
625cbd7a 3942 {
a5748e0d 3943 switch (get_broadcast_bytes (&i.tm, true))
625cbd7a
JB
3944 {
3945 case 64:
3946 i.tm.opcode_modifier.evex = EVEX512;
3947 break;
3948 case 32:
3949 i.tm.opcode_modifier.evex = EVEX256;
3950 break;
3951 case 16:
3952 i.tm.opcode_modifier.evex = EVEX128;
3953 break;
3954 default:
c7213af9 3955 abort ();
625cbd7a 3956 }
c7213af9 3957 break;
625cbd7a 3958 }
e771e7c9 3959 }
c7213af9 3960
56522fc5 3961 if (op >= MAX_OPERANDS)
c7213af9 3962 abort ();
e771e7c9
JB
3963 }
3964
43234a1e
L
3965 switch (i.tm.opcode_modifier.evex)
3966 {
3967 case EVEXLIG: /* LL' is ignored */
3968 vec_length = evexlig << 5;
3969 break;
3970 case EVEX128:
3971 vec_length = 0 << 5;
3972 break;
3973 case EVEX256:
3974 vec_length = 1 << 5;
3975 break;
3976 case EVEX512:
3977 vec_length = 2 << 5;
3978 break;
3979 default:
3980 abort ();
3981 break;
3982 }
3983 i.vex.bytes[3] |= vec_length;
3984 /* Encode the broadcast bit. */
a5748e0d 3985 if (i.broadcast.bytes)
43234a1e
L
3986 i.vex.bytes[3] |= 0x10;
3987 }
ca5312a2
JB
3988 else if (i.rounding.type != saeonly)
3989 i.vex.bytes[3] |= 0x10 | (i.rounding.type << 5);
43234a1e 3990 else
ca5312a2 3991 i.vex.bytes[3] |= 0x10 | (evexrcig << 5);
43234a1e 3992
6225c532
JB
3993 if (i.mask.reg)
3994 i.vex.bytes[3] |= i.mask.reg->reg_num;
43234a1e
L
3995}
3996
65da13b5
L
3997static void
3998process_immext (void)
3999{
4000 expressionS *exp;
4001
c0f3af97 4002 /* These AMD 3DNow! and SSE2 instructions have an opcode suffix
65da13b5
L
4003 which is coded in the same place as an 8-bit immediate field
4004 would be. Here we fake an 8-bit immediate operand from the
4005 opcode suffix stored in tm.extension_opcode.
4006
c1e679ec 4007 AVX instructions also use this encoding, for some of
c0f3af97 4008 3 argument instructions. */
65da13b5 4009
43234a1e 4010 gas_assert (i.imm_operands <= 1
7ab9ffdd 4011 && (i.operands <= 2
7a8655d2 4012 || (is_any_vex_encoding (&i.tm)
7ab9ffdd 4013 && i.operands <= 4)));
65da13b5
L
4014
4015 exp = &im_expressions[i.imm_operands++];
4016 i.op[i.operands].imms = exp;
be1643ff 4017 i.types[i.operands].bitfield.imm8 = 1;
65da13b5
L
4018 i.operands++;
4019 exp->X_op = O_constant;
4020 exp->X_add_number = i.tm.extension_opcode;
4021 i.tm.extension_opcode = None;
4022}
4023
42164a71
L
4024
4025static int
4026check_hle (void)
4027{
742732c7 4028 switch (i.tm.opcode_modifier.prefixok)
42164a71
L
4029 {
4030 default:
4031 abort ();
742732c7
JB
4032 case PrefixLock:
4033 case PrefixNone:
4034 case PrefixNoTrack:
4035 case PrefixRep:
165de32a
L
4036 as_bad (_("invalid instruction `%s' after `%s'"),
4037 i.tm.name, i.hle_prefix);
42164a71 4038 return 0;
742732c7 4039 case PrefixHLELock:
42164a71
L
4040 if (i.prefix[LOCK_PREFIX])
4041 return 1;
165de32a 4042 as_bad (_("missing `lock' with `%s'"), i.hle_prefix);
42164a71 4043 return 0;
742732c7 4044 case PrefixHLEAny:
42164a71 4045 return 1;
742732c7 4046 case PrefixHLERelease:
42164a71
L
4047 if (i.prefix[HLE_PREFIX] != XRELEASE_PREFIX_OPCODE)
4048 {
4049 as_bad (_("instruction `%s' after `xacquire' not allowed"),
4050 i.tm.name);
4051 return 0;
4052 }
8dc0818e 4053 if (i.mem_operands == 0 || !(i.flags[i.operands - 1] & Operand_Mem))
42164a71
L
4054 {
4055 as_bad (_("memory destination needed for instruction `%s'"
4056 " after `xrelease'"), i.tm.name);
4057 return 0;
4058 }
4059 return 1;
4060 }
4061}
4062
c8480b58
L
4063/* Encode aligned vector move as unaligned vector move. */
4064
4065static void
4066encode_with_unaligned_vector_move (void)
4067{
4068 switch (i.tm.base_opcode)
4069 {
b3a9fe6f
L
4070 case 0x28: /* Load instructions. */
4071 case 0x29: /* Store instructions. */
c8480b58
L
4072 /* movaps/movapd/vmovaps/vmovapd. */
4073 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4074 && i.tm.opcode_modifier.opcodeprefix <= PREFIX_0X66)
b3a9fe6f 4075 i.tm.base_opcode = 0x10 | (i.tm.base_opcode & 1);
c8480b58 4076 break;
b3a9fe6f
L
4077 case 0x6f: /* Load instructions. */
4078 case 0x7f: /* Store instructions. */
c8480b58
L
4079 /* movdqa/vmovdqa/vmovdqa64/vmovdqa32. */
4080 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4081 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0X66)
4082 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
4083 break;
4084 default:
4085 break;
4086 }
4087}
4088
b6f8c7c4
L
4089/* Try the shortest encoding by shortening operand size. */
4090
4091static void
4092optimize_encoding (void)
4093{
a0a1771e 4094 unsigned int j;
b6f8c7c4 4095
fe134c65
JB
4096 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
4097 && i.tm.base_opcode == 0x8d)
4098 {
4099 /* Optimize: -O:
4100 lea symbol, %rN -> mov $symbol, %rN
4101 lea (%rM), %rN -> mov %rM, %rN
4102 lea (,%rM,1), %rN -> mov %rM, %rN
4103
4104 and in 32-bit mode for 16-bit addressing
4105
4106 lea (%rM), %rN -> movzx %rM, %rN
4107
4108 and in 64-bit mode zap 32-bit addressing in favor of using a
4109 32-bit (or less) destination.
4110 */
4111 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
4112 {
4113 if (!i.op[1].regs->reg_type.bitfield.word)
4114 i.tm.opcode_modifier.size = SIZE32;
4115 i.prefix[ADDR_PREFIX] = 0;
4116 }
4117
4118 if (!i.index_reg && !i.base_reg)
4119 {
4120 /* Handle:
4121 lea symbol, %rN -> mov $symbol, %rN
4122 */
4123 if (flag_code == CODE_64BIT)
4124 {
4125 /* Don't transform a relocation to a 16-bit one. */
4126 if (i.op[0].disps
4127 && i.op[0].disps->X_op != O_constant
4128 && i.op[1].regs->reg_type.bitfield.word)
4129 return;
4130
4131 if (!i.op[1].regs->reg_type.bitfield.qword
4132 || i.tm.opcode_modifier.size == SIZE32)
4133 {
4134 i.tm.base_opcode = 0xb8;
4135 i.tm.opcode_modifier.modrm = 0;
4136 if (!i.op[1].regs->reg_type.bitfield.word)
4137 i.types[0].bitfield.imm32 = 1;
4138 else
4139 {
4140 i.tm.opcode_modifier.size = SIZE16;
4141 i.types[0].bitfield.imm16 = 1;
4142 }
4143 }
4144 else
4145 {
4146 /* Subject to further optimization below. */
4147 i.tm.base_opcode = 0xc7;
4148 i.tm.extension_opcode = 0;
4149 i.types[0].bitfield.imm32s = 1;
4150 i.types[0].bitfield.baseindex = 0;
4151 }
4152 }
4153 /* Outside of 64-bit mode address and operand sizes have to match if
4154 a relocation is involved, as otherwise we wouldn't (currently) or
4155 even couldn't express the relocation correctly. */
4156 else if (i.op[0].disps
4157 && i.op[0].disps->X_op != O_constant
4158 && ((!i.prefix[ADDR_PREFIX])
4159 != (flag_code == CODE_32BIT
4160 ? i.op[1].regs->reg_type.bitfield.dword
4161 : i.op[1].regs->reg_type.bitfield.word)))
4162 return;
7772f168
JB
4163 /* In 16-bit mode converting LEA with 16-bit addressing and a 32-bit
4164 destination is going to grow encoding size. */
4165 else if (flag_code == CODE_16BIT
4166 && (optimize <= 1 || optimize_for_space)
4167 && !i.prefix[ADDR_PREFIX]
4168 && i.op[1].regs->reg_type.bitfield.dword)
4169 return;
fe134c65
JB
4170 else
4171 {
4172 i.tm.base_opcode = 0xb8;
4173 i.tm.opcode_modifier.modrm = 0;
4174 if (i.op[1].regs->reg_type.bitfield.dword)
4175 i.types[0].bitfield.imm32 = 1;
4176 else
4177 i.types[0].bitfield.imm16 = 1;
4178
4179 if (i.op[0].disps
4180 && i.op[0].disps->X_op == O_constant
4181 && i.op[1].regs->reg_type.bitfield.dword
60cfa10c
L
4182 /* NB: Add () to !i.prefix[ADDR_PREFIX] to silence
4183 GCC 5. */
4184 && (!i.prefix[ADDR_PREFIX]) != (flag_code == CODE_32BIT))
fe134c65
JB
4185 i.op[0].disps->X_add_number &= 0xffff;
4186 }
4187
4188 i.tm.operand_types[0] = i.types[0];
4189 i.imm_operands = 1;
4190 if (!i.op[0].imms)
4191 {
4192 i.op[0].imms = &im_expressions[0];
4193 i.op[0].imms->X_op = O_absent;
4194 }
4195 }
4196 else if (i.op[0].disps
4197 && (i.op[0].disps->X_op != O_constant
4198 || i.op[0].disps->X_add_number))
4199 return;
4200 else
4201 {
4202 /* Handle:
4203 lea (%rM), %rN -> mov %rM, %rN
4204 lea (,%rM,1), %rN -> mov %rM, %rN
4205 lea (%rM), %rN -> movzx %rM, %rN
4206 */
4207 const reg_entry *addr_reg;
4208
4209 if (!i.index_reg && i.base_reg->reg_num != RegIP)
4210 addr_reg = i.base_reg;
4211 else if (!i.base_reg
4212 && i.index_reg->reg_num != RegIZ
4213 && !i.log2_scale_factor)
4214 addr_reg = i.index_reg;
4215 else
4216 return;
4217
4218 if (addr_reg->reg_type.bitfield.word
4219 && i.op[1].regs->reg_type.bitfield.dword)
4220 {
4221 if (flag_code != CODE_32BIT)
4222 return;
4223 i.tm.opcode_modifier.opcodespace = SPACE_0F;
4224 i.tm.base_opcode = 0xb7;
4225 }
4226 else
4227 i.tm.base_opcode = 0x8b;
4228
4229 if (addr_reg->reg_type.bitfield.dword
4230 && i.op[1].regs->reg_type.bitfield.qword)
4231 i.tm.opcode_modifier.size = SIZE32;
4232
4233 i.op[0].regs = addr_reg;
4234 i.reg_operands = 2;
4235 }
4236
4237 i.mem_operands = 0;
4238 i.disp_operands = 0;
4239 i.prefix[ADDR_PREFIX] = 0;
4240 i.prefix[SEG_PREFIX] = 0;
4241 i.seg[0] = NULL;
4242 }
4243
b6f8c7c4 4244 if (optimize_for_space
389d00a5 4245 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
b6f8c7c4
L
4246 && i.reg_operands == 1
4247 && i.imm_operands == 1
4248 && !i.types[1].bitfield.byte
4249 && i.op[0].imms->X_op == O_constant
4250 && fits_in_imm7 (i.op[0].imms->X_add_number)
72aea328 4251 && (i.tm.base_opcode == 0xa8
b6f8c7c4
L
4252 || (i.tm.base_opcode == 0xf6
4253 && i.tm.extension_opcode == 0x0)))
4254 {
4255 /* Optimize: -Os:
4256 test $imm7, %r64/%r32/%r16 -> test $imm7, %r8
4257 */
4258 unsigned int base_regnum = i.op[1].regs->reg_num;
4259 if (flag_code == CODE_64BIT || base_regnum < 4)
4260 {
4261 i.types[1].bitfield.byte = 1;
4262 /* Ignore the suffix. */
4263 i.suffix = 0;
7697afb6
JB
4264 /* Convert to byte registers. */
4265 if (i.types[1].bitfield.word)
4266 j = 16;
4267 else if (i.types[1].bitfield.dword)
4268 j = 32;
4269 else
4270 j = 48;
4271 if (!(i.op[1].regs->reg_flags & RegRex) && base_regnum < 4)
4272 j += 8;
4273 i.op[1].regs -= j;
b6f8c7c4
L
4274 }
4275 }
4276 else if (flag_code == CODE_64BIT
389d00a5 4277 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
d3d50934
L
4278 && ((i.types[1].bitfield.qword
4279 && i.reg_operands == 1
b6f8c7c4
L
4280 && i.imm_operands == 1
4281 && i.op[0].imms->X_op == O_constant
507916b8 4282 && ((i.tm.base_opcode == 0xb8
b6f8c7c4
L
4283 && i.tm.extension_opcode == None
4284 && fits_in_unsigned_long (i.op[0].imms->X_add_number))
4285 || (fits_in_imm31 (i.op[0].imms->X_add_number)
72aea328
JB
4286 && ((i.tm.base_opcode == 0x24
4287 || i.tm.base_opcode == 0xa8)
b6f8c7c4
L
4288 || (i.tm.base_opcode == 0x80
4289 && i.tm.extension_opcode == 0x4)
4290 || ((i.tm.base_opcode == 0xf6
507916b8 4291 || (i.tm.base_opcode | 1) == 0xc7)
b8364fa7
JB
4292 && i.tm.extension_opcode == 0x0)))
4293 || (fits_in_imm7 (i.op[0].imms->X_add_number)
4294 && i.tm.base_opcode == 0x83
4295 && i.tm.extension_opcode == 0x4)))
d3d50934
L
4296 || (i.types[0].bitfield.qword
4297 && ((i.reg_operands == 2
4298 && i.op[0].regs == i.op[1].regs
72aea328
JB
4299 && (i.tm.base_opcode == 0x30
4300 || i.tm.base_opcode == 0x28))
d3d50934
L
4301 || (i.reg_operands == 1
4302 && i.operands == 1
72aea328 4303 && i.tm.base_opcode == 0x30)))))
b6f8c7c4
L
4304 {
4305 /* Optimize: -O:
4306 andq $imm31, %r64 -> andl $imm31, %r32
b8364fa7 4307 andq $imm7, %r64 -> andl $imm7, %r32
b6f8c7c4
L
4308 testq $imm31, %r64 -> testl $imm31, %r32
4309 xorq %r64, %r64 -> xorl %r32, %r32
4310 subq %r64, %r64 -> subl %r32, %r32
4311 movq $imm31, %r64 -> movl $imm31, %r32
4312 movq $imm32, %r64 -> movl $imm32, %r32
4313 */
4314 i.tm.opcode_modifier.norex64 = 1;
507916b8 4315 if (i.tm.base_opcode == 0xb8 || (i.tm.base_opcode | 1) == 0xc7)
b6f8c7c4
L
4316 {
4317 /* Handle
4318 movq $imm31, %r64 -> movl $imm31, %r32
4319 movq $imm32, %r64 -> movl $imm32, %r32
4320 */
4321 i.tm.operand_types[0].bitfield.imm32 = 1;
4322 i.tm.operand_types[0].bitfield.imm32s = 0;
4323 i.tm.operand_types[0].bitfield.imm64 = 0;
4324 i.types[0].bitfield.imm32 = 1;
4325 i.types[0].bitfield.imm32s = 0;
4326 i.types[0].bitfield.imm64 = 0;
4327 i.types[1].bitfield.dword = 1;
4328 i.types[1].bitfield.qword = 0;
507916b8 4329 if ((i.tm.base_opcode | 1) == 0xc7)
b6f8c7c4
L
4330 {
4331 /* Handle
4332 movq $imm31, %r64 -> movl $imm31, %r32
4333 */
507916b8 4334 i.tm.base_opcode = 0xb8;
b6f8c7c4 4335 i.tm.extension_opcode = None;
507916b8 4336 i.tm.opcode_modifier.w = 0;
b6f8c7c4
L
4337 i.tm.opcode_modifier.modrm = 0;
4338 }
4339 }
4340 }
5641ec01
JB
4341 else if (optimize > 1
4342 && !optimize_for_space
389d00a5 4343 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
5641ec01
JB
4344 && i.reg_operands == 2
4345 && i.op[0].regs == i.op[1].regs
4346 && ((i.tm.base_opcode & ~(Opcode_D | 1)) == 0x8
4347 || (i.tm.base_opcode & ~(Opcode_D | 1)) == 0x20)
4348 && (flag_code != CODE_64BIT || !i.types[0].bitfield.dword))
4349 {
4350 /* Optimize: -O2:
4351 andb %rN, %rN -> testb %rN, %rN
4352 andw %rN, %rN -> testw %rN, %rN
4353 andq %rN, %rN -> testq %rN, %rN
4354 orb %rN, %rN -> testb %rN, %rN
4355 orw %rN, %rN -> testw %rN, %rN
4356 orq %rN, %rN -> testq %rN, %rN
4357
4358 and outside of 64-bit mode
4359
4360 andl %rN, %rN -> testl %rN, %rN
4361 orl %rN, %rN -> testl %rN, %rN
4362 */
4363 i.tm.base_opcode = 0x84 | (i.tm.base_opcode & 1);
4364 }
99112332 4365 else if (i.reg_operands == 3
b6f8c7c4
L
4366 && i.op[0].regs == i.op[1].regs
4367 && !i.types[2].bitfield.xmmword
4368 && (i.tm.opcode_modifier.vex
6225c532 4369 || ((!i.mask.reg || i.mask.zeroing)
e771e7c9 4370 && is_evex_encoding (&i.tm)
80c34c38 4371 && (i.vec_encoding != vex_encoding_evex
dd22218c 4372 || cpu_arch_isa_flags.bitfield.cpuavx512vl
80c34c38 4373 || i.tm.cpu_flags.bitfield.cpuavx512vl
7091c612 4374 || (i.tm.operand_types[2].bitfield.zmmword
dd22218c 4375 && i.types[2].bitfield.ymmword))))
5844ccaa
JB
4376 && i.tm.opcode_modifier.opcodespace == SPACE_0F
4377 && ((i.tm.base_opcode | 2) == 0x57
4378 || i.tm.base_opcode == 0xdf
4379 || i.tm.base_opcode == 0xef
4380 || (i.tm.base_opcode | 3) == 0xfb
4381 || i.tm.base_opcode == 0x42
4382 || i.tm.base_opcode == 0x47))
b6f8c7c4 4383 {
99112332 4384 /* Optimize: -O1:
8305403a
L
4385 VOP, one of vandnps, vandnpd, vxorps, vxorpd, vpsubb, vpsubd,
4386 vpsubq and vpsubw:
b6f8c7c4
L
4387 EVEX VOP %zmmM, %zmmM, %zmmN
4388 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4389 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4390 EVEX VOP %ymmM, %ymmM, %ymmN
4391 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4392 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4393 VEX VOP %ymmM, %ymmM, %ymmN
4394 -> VEX VOP %xmmM, %xmmM, %xmmN
4395 VOP, one of vpandn and vpxor:
4396 VEX VOP %ymmM, %ymmM, %ymmN
4397 -> VEX VOP %xmmM, %xmmM, %xmmN
4398 VOP, one of vpandnd and vpandnq:
4399 EVEX VOP %zmmM, %zmmM, %zmmN
4400 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4401 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4402 EVEX VOP %ymmM, %ymmM, %ymmN
4403 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4404 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4405 VOP, one of vpxord and vpxorq:
4406 EVEX VOP %zmmM, %zmmM, %zmmN
4407 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4408 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4409 EVEX VOP %ymmM, %ymmM, %ymmN
4410 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4411 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
1424ad86
JB
4412 VOP, one of kxord and kxorq:
4413 VEX VOP %kM, %kM, %kN
4414 -> VEX kxorw %kM, %kM, %kN
4415 VOP, one of kandnd and kandnq:
4416 VEX VOP %kM, %kM, %kN
4417 -> VEX kandnw %kM, %kM, %kN
b6f8c7c4 4418 */
e771e7c9 4419 if (is_evex_encoding (&i.tm))
b6f8c7c4 4420 {
7b1d7ca1 4421 if (i.vec_encoding != vex_encoding_evex)
b6f8c7c4
L
4422 {
4423 i.tm.opcode_modifier.vex = VEX128;
4424 i.tm.opcode_modifier.vexw = VEXW0;
4425 i.tm.opcode_modifier.evex = 0;
4426 }
7b1d7ca1 4427 else if (optimize > 1)
dd22218c
L
4428 i.tm.opcode_modifier.evex = EVEX128;
4429 else
4430 return;
b6f8c7c4 4431 }
f74a6307 4432 else if (i.tm.operand_types[0].bitfield.class == RegMask)
1424ad86 4433 {
35648716 4434 i.tm.opcode_modifier.opcodeprefix = PREFIX_NONE;
1424ad86
JB
4435 i.tm.opcode_modifier.vexw = VEXW0;
4436 }
b6f8c7c4
L
4437 else
4438 i.tm.opcode_modifier.vex = VEX128;
4439
4440 if (i.tm.opcode_modifier.vex)
4441 for (j = 0; j < 3; j++)
4442 {
4443 i.types[j].bitfield.xmmword = 1;
4444 i.types[j].bitfield.ymmword = 0;
4445 }
4446 }
392a5972 4447 else if (i.vec_encoding != vex_encoding_evex
97ed31ae 4448 && !i.types[0].bitfield.zmmword
392a5972 4449 && !i.types[1].bitfield.zmmword
6225c532 4450 && !i.mask.reg
a5748e0d 4451 && !i.broadcast.bytes
97ed31ae 4452 && is_evex_encoding (&i.tm)
35648716
JB
4453 && ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4454 || (i.tm.base_opcode & ~4) == 0xdb
4455 || (i.tm.base_opcode & ~4) == 0xeb)
97ed31ae
L
4456 && i.tm.extension_opcode == None)
4457 {
4458 /* Optimize: -O1:
4459 VOP, one of vmovdqa32, vmovdqa64, vmovdqu8, vmovdqu16,
4460 vmovdqu32 and vmovdqu64:
4461 EVEX VOP %xmmM, %xmmN
4462 -> VEX vmovdqa|vmovdqu %xmmM, %xmmN (M and N < 16)
4463 EVEX VOP %ymmM, %ymmN
4464 -> VEX vmovdqa|vmovdqu %ymmM, %ymmN (M and N < 16)
4465 EVEX VOP %xmmM, mem
4466 -> VEX vmovdqa|vmovdqu %xmmM, mem (M < 16)
4467 EVEX VOP %ymmM, mem
4468 -> VEX vmovdqa|vmovdqu %ymmM, mem (M < 16)
4469 EVEX VOP mem, %xmmN
4470 -> VEX mvmovdqa|vmovdquem, %xmmN (N < 16)
4471 EVEX VOP mem, %ymmN
4472 -> VEX vmovdqa|vmovdqu mem, %ymmN (N < 16)
a0a1771e
JB
4473 VOP, one of vpand, vpandn, vpor, vpxor:
4474 EVEX VOP{d,q} %xmmL, %xmmM, %xmmN
4475 -> VEX VOP %xmmL, %xmmM, %xmmN (L, M, and N < 16)
4476 EVEX VOP{d,q} %ymmL, %ymmM, %ymmN
4477 -> VEX VOP %ymmL, %ymmM, %ymmN (L, M, and N < 16)
4478 EVEX VOP{d,q} mem, %xmmM, %xmmN
4479 -> VEX VOP mem, %xmmM, %xmmN (M and N < 16)
4480 EVEX VOP{d,q} mem, %ymmM, %ymmN
4481 -> VEX VOP mem, %ymmM, %ymmN (M and N < 16)
97ed31ae 4482 */
a0a1771e 4483 for (j = 0; j < i.operands; j++)
392a5972
L
4484 if (operand_type_check (i.types[j], disp)
4485 && i.op[j].disps->X_op == O_constant)
4486 {
4487 /* Since the VEX prefix has 2 or 3 bytes, the EVEX prefix
4488 has 4 bytes, EVEX Disp8 has 1 byte and VEX Disp32 has 4
4489 bytes, we choose EVEX Disp8 over VEX Disp32. */
4490 int evex_disp8, vex_disp8;
4491 unsigned int memshift = i.memshift;
4492 offsetT n = i.op[j].disps->X_add_number;
4493
4494 evex_disp8 = fits_in_disp8 (n);
4495 i.memshift = 0;
4496 vex_disp8 = fits_in_disp8 (n);
4497 if (evex_disp8 != vex_disp8)
4498 {
4499 i.memshift = memshift;
4500 return;
4501 }
4502
4503 i.types[j].bitfield.disp8 = vex_disp8;
4504 break;
4505 }
35648716
JB
4506 if ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4507 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2)
4508 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
97ed31ae
L
4509 i.tm.opcode_modifier.vex
4510 = i.types[0].bitfield.ymmword ? VEX256 : VEX128;
4511 i.tm.opcode_modifier.vexw = VEXW0;
79dec6b7 4512 /* VPAND, VPOR, and VPXOR are commutative. */
35648716 4513 if (i.reg_operands == 3 && i.tm.base_opcode != 0xdf)
79dec6b7 4514 i.tm.opcode_modifier.commutative = 1;
97ed31ae
L
4515 i.tm.opcode_modifier.evex = 0;
4516 i.tm.opcode_modifier.masking = 0;
a0a1771e 4517 i.tm.opcode_modifier.broadcast = 0;
97ed31ae
L
4518 i.tm.opcode_modifier.disp8memshift = 0;
4519 i.memshift = 0;
a0a1771e
JB
4520 if (j < i.operands)
4521 i.types[j].bitfield.disp8
4522 = fits_in_disp8 (i.op[j].disps->X_add_number);
97ed31ae 4523 }
b6f8c7c4
L
4524}
4525
ae531041
L
4526/* Return non-zero for load instruction. */
4527
4528static int
4529load_insn_p (void)
4530{
4531 unsigned int dest;
4532 int any_vex_p = is_any_vex_encoding (&i.tm);
4533 unsigned int base_opcode = i.tm.base_opcode | 1;
4534
4535 if (!any_vex_p)
4536 {
ef07be45
CL
4537 /* Anysize insns: lea, invlpg, clflush, prefetch*, bndmk, bndcl, bndcu,
4538 bndcn, bndstx, bndldx, clflushopt, clwb, cldemote. */
255571cd 4539 if (i.tm.opcode_modifier.operandconstraint == ANY_SIZE)
ae531041
L
4540 return 0;
4541
389d00a5
JB
4542 /* pop. */
4543 if (strcmp (i.tm.name, "pop") == 0)
4544 return 1;
4545 }
4546
4547 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE)
4548 {
4549 /* popf, popa. */
4550 if (i.tm.base_opcode == 0x9d
a09f656b 4551 || i.tm.base_opcode == 0x61)
ae531041
L
4552 return 1;
4553
4554 /* movs, cmps, lods, scas. */
4555 if ((i.tm.base_opcode | 0xb) == 0xaf)
4556 return 1;
4557
a09f656b 4558 /* outs, xlatb. */
4559 if (base_opcode == 0x6f
4560 || i.tm.base_opcode == 0xd7)
ae531041 4561 return 1;
a09f656b 4562 /* NB: For AMD-specific insns with implicit memory operands,
4563 they're intentionally not covered. */
ae531041
L
4564 }
4565
4566 /* No memory operand. */
4567 if (!i.mem_operands)
4568 return 0;
4569
4570 if (any_vex_p)
4571 {
4572 /* vldmxcsr. */
4573 if (i.tm.base_opcode == 0xae
4574 && i.tm.opcode_modifier.vex
441f6aca 4575 && i.tm.opcode_modifier.opcodespace == SPACE_0F
35648716 4576 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
ae531041
L
4577 && i.tm.extension_opcode == 2)
4578 return 1;
4579 }
389d00a5 4580 else if (i.tm.opcode_modifier.opcodespace == SPACE_BASE)
ae531041
L
4581 {
4582 /* test, not, neg, mul, imul, div, idiv. */
4583 if ((i.tm.base_opcode == 0xf6 || i.tm.base_opcode == 0xf7)
4584 && i.tm.extension_opcode != 1)
4585 return 1;
4586
4587 /* inc, dec. */
4588 if (base_opcode == 0xff && i.tm.extension_opcode <= 1)
4589 return 1;
4590
4591 /* add, or, adc, sbb, and, sub, xor, cmp. */
4592 if (i.tm.base_opcode >= 0x80 && i.tm.base_opcode <= 0x83)
4593 return 1;
4594
ae531041
L
4595 /* rol, ror, rcl, rcr, shl/sal, shr, sar. */
4596 if ((base_opcode == 0xc1
4597 || (i.tm.base_opcode >= 0xd0 && i.tm.base_opcode <= 0xd3))
4598 && i.tm.extension_opcode != 6)
4599 return 1;
4600
ae531041 4601 /* Check for x87 instructions. */
389d00a5 4602 if (base_opcode >= 0xd8 && base_opcode <= 0xdf)
ae531041
L
4603 {
4604 /* Skip fst, fstp, fstenv, fstcw. */
4605 if (i.tm.base_opcode == 0xd9
4606 && (i.tm.extension_opcode == 2
4607 || i.tm.extension_opcode == 3
4608 || i.tm.extension_opcode == 6
4609 || i.tm.extension_opcode == 7))
4610 return 0;
4611
4612 /* Skip fisttp, fist, fistp, fstp. */
4613 if (i.tm.base_opcode == 0xdb
4614 && (i.tm.extension_opcode == 1
4615 || i.tm.extension_opcode == 2
4616 || i.tm.extension_opcode == 3
4617 || i.tm.extension_opcode == 7))
4618 return 0;
4619
4620 /* Skip fisttp, fst, fstp, fsave, fstsw. */
4621 if (i.tm.base_opcode == 0xdd
4622 && (i.tm.extension_opcode == 1
4623 || i.tm.extension_opcode == 2
4624 || i.tm.extension_opcode == 3
4625 || i.tm.extension_opcode == 6
4626 || i.tm.extension_opcode == 7))
4627 return 0;
4628
4629 /* Skip fisttp, fist, fistp, fbstp, fistp. */
4630 if (i.tm.base_opcode == 0xdf
4631 && (i.tm.extension_opcode == 1
4632 || i.tm.extension_opcode == 2
4633 || i.tm.extension_opcode == 3
4634 || i.tm.extension_opcode == 6
4635 || i.tm.extension_opcode == 7))
4636 return 0;
4637
4638 return 1;
4639 }
4640 }
389d00a5
JB
4641 else if (i.tm.opcode_modifier.opcodespace == SPACE_0F)
4642 {
4643 /* bt, bts, btr, btc. */
4644 if (i.tm.base_opcode == 0xba
4645 && (i.tm.extension_opcode >= 4 && i.tm.extension_opcode <= 7))
4646 return 1;
4647
4648 /* cmpxchg8b, cmpxchg16b, xrstors, vmptrld. */
4649 if (i.tm.base_opcode == 0xc7
4650 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
4651 && (i.tm.extension_opcode == 1 || i.tm.extension_opcode == 3
4652 || i.tm.extension_opcode == 6))
4653 return 1;
4654
4655 /* fxrstor, ldmxcsr, xrstor. */
4656 if (i.tm.base_opcode == 0xae
4657 && (i.tm.extension_opcode == 1
4658 || i.tm.extension_opcode == 2
4659 || i.tm.extension_opcode == 5))
4660 return 1;
4661
4662 /* lgdt, lidt, lmsw. */
4663 if (i.tm.base_opcode == 0x01
4664 && (i.tm.extension_opcode == 2
4665 || i.tm.extension_opcode == 3
4666 || i.tm.extension_opcode == 6))
4667 return 1;
4668 }
ae531041
L
4669
4670 dest = i.operands - 1;
4671
4672 /* Check fake imm8 operand and 3 source operands. */
4673 if ((i.tm.opcode_modifier.immext
4674 || i.tm.opcode_modifier.vexsources == VEX3SOURCES)
4675 && i.types[dest].bitfield.imm8)
4676 dest--;
4677
389d00a5
JB
4678 /* add, or, adc, sbb, and, sub, xor, cmp, test, xchg. */
4679 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
ae531041
L
4680 && (base_opcode == 0x1
4681 || base_opcode == 0x9
4682 || base_opcode == 0x11
4683 || base_opcode == 0x19
4684 || base_opcode == 0x21
4685 || base_opcode == 0x29
4686 || base_opcode == 0x31
4687 || base_opcode == 0x39
389d00a5
JB
4688 || (base_opcode | 2) == 0x87))
4689 return 1;
4690
4691 /* xadd. */
4692 if (i.tm.opcode_modifier.opcodespace == SPACE_0F
4693 && base_opcode == 0xc1)
ae531041
L
4694 return 1;
4695
4696 /* Check for load instruction. */
4697 return (i.types[dest].bitfield.class != ClassNone
4698 || i.types[dest].bitfield.instance == Accum);
4699}
4700
4701/* Output lfence, 0xfaee8, after instruction. */
4702
4703static void
4704insert_lfence_after (void)
4705{
4706 if (lfence_after_load && load_insn_p ())
4707 {
a09f656b 4708 /* There are also two REP string instructions that require
4709 special treatment. Specifically, the compare string (CMPS)
4710 and scan string (SCAS) instructions set EFLAGS in a manner
4711 that depends on the data being compared/scanned. When used
4712 with a REP prefix, the number of iterations may therefore
4713 vary depending on this data. If the data is a program secret
4714 chosen by the adversary using an LVI method,
4715 then this data-dependent behavior may leak some aspect
4716 of the secret. */
4717 if (((i.tm.base_opcode | 0x1) == 0xa7
4718 || (i.tm.base_opcode | 0x1) == 0xaf)
4719 && i.prefix[REP_PREFIX])
4720 {
4721 as_warn (_("`%s` changes flags which would affect control flow behavior"),
4722 i.tm.name);
4723 }
ae531041
L
4724 char *p = frag_more (3);
4725 *p++ = 0xf;
4726 *p++ = 0xae;
4727 *p = 0xe8;
4728 }
4729}
4730
4731/* Output lfence, 0xfaee8, before instruction. */
4732
4733static void
4734insert_lfence_before (void)
4735{
4736 char *p;
4737
389d00a5 4738 if (i.tm.opcode_modifier.opcodespace != SPACE_BASE)
ae531041
L
4739 return;
4740
4741 if (i.tm.base_opcode == 0xff
4742 && (i.tm.extension_opcode == 2 || i.tm.extension_opcode == 4))
4743 {
4744 /* Insert lfence before indirect branch if needed. */
4745
4746 if (lfence_before_indirect_branch == lfence_branch_none)
4747 return;
4748
4749 if (i.operands != 1)
4750 abort ();
4751
4752 if (i.reg_operands == 1)
4753 {
4754 /* Indirect branch via register. Don't insert lfence with
4755 -mlfence-after-load=yes. */
4756 if (lfence_after_load
4757 || lfence_before_indirect_branch == lfence_branch_memory)
4758 return;
4759 }
4760 else if (i.mem_operands == 1
4761 && lfence_before_indirect_branch != lfence_branch_register)
4762 {
4763 as_warn (_("indirect `%s` with memory operand should be avoided"),
4764 i.tm.name);
4765 return;
4766 }
4767 else
4768 return;
4769
4770 if (last_insn.kind != last_insn_other
4771 && last_insn.seg == now_seg)
4772 {
4773 as_warn_where (last_insn.file, last_insn.line,
4774 _("`%s` skips -mlfence-before-indirect-branch on `%s`"),
4775 last_insn.name, i.tm.name);
4776 return;
4777 }
4778
4779 p = frag_more (3);
4780 *p++ = 0xf;
4781 *p++ = 0xae;
4782 *p = 0xe8;
4783 return;
4784 }
4785
503648e4 4786 /* Output or/not/shl and lfence before near ret. */
ae531041
L
4787 if (lfence_before_ret != lfence_before_ret_none
4788 && (i.tm.base_opcode == 0xc2
503648e4 4789 || i.tm.base_opcode == 0xc3))
ae531041
L
4790 {
4791 if (last_insn.kind != last_insn_other
4792 && last_insn.seg == now_seg)
4793 {
4794 as_warn_where (last_insn.file, last_insn.line,
4795 _("`%s` skips -mlfence-before-ret on `%s`"),
4796 last_insn.name, i.tm.name);
4797 return;
4798 }
a09f656b 4799
a09f656b 4800 /* Near ret ingore operand size override under CPU64. */
503648e4 4801 char prefix = flag_code == CODE_64BIT
4802 ? 0x48
4803 : i.prefix[DATA_PREFIX] ? 0x66 : 0x0;
a09f656b 4804
4805 if (lfence_before_ret == lfence_before_ret_not)
4806 {
4807 /* not: 0xf71424, may add prefix
4808 for operand size override or 64-bit code. */
4809 p = frag_more ((prefix ? 2 : 0) + 6 + 3);
4810 if (prefix)
4811 *p++ = prefix;
ae531041
L
4812 *p++ = 0xf7;
4813 *p++ = 0x14;
4814 *p++ = 0x24;
a09f656b 4815 if (prefix)
4816 *p++ = prefix;
ae531041
L
4817 *p++ = 0xf7;
4818 *p++ = 0x14;
4819 *p++ = 0x24;
4820 }
a09f656b 4821 else
4822 {
4823 p = frag_more ((prefix ? 1 : 0) + 4 + 3);
4824 if (prefix)
4825 *p++ = prefix;
4826 if (lfence_before_ret == lfence_before_ret_or)
4827 {
4828 /* or: 0x830c2400, may add prefix
4829 for operand size override or 64-bit code. */
4830 *p++ = 0x83;
4831 *p++ = 0x0c;
4832 }
4833 else
4834 {
4835 /* shl: 0xc1242400, may add prefix
4836 for operand size override or 64-bit code. */
4837 *p++ = 0xc1;
4838 *p++ = 0x24;
4839 }
4840
4841 *p++ = 0x24;
4842 *p++ = 0x0;
4843 }
4844
ae531041
L
4845 *p++ = 0xf;
4846 *p++ = 0xae;
4847 *p = 0xe8;
4848 }
4849}
4850
252b5132
RH
4851/* This is the guts of the machine-dependent assembler. LINE points to a
4852 machine dependent instruction. This function is supposed to emit
4853 the frags/bytes it assembles to. */
4854
4855void
65da13b5 4856md_assemble (char *line)
252b5132 4857{
40fb9820 4858 unsigned int j;
83b16ac6 4859 char mnemonic[MAX_MNEM_SIZE], mnem_suffix;
d3ce72d0 4860 const insn_template *t;
252b5132 4861
47926f60 4862 /* Initialize globals. */
252b5132 4863 memset (&i, '\0', sizeof (i));
ca5312a2 4864 i.rounding.type = rc_none;
252b5132 4865 for (j = 0; j < MAX_OPERANDS; j++)
1ae12ab7 4866 i.reloc[j] = NO_RELOC;
252b5132
RH
4867 memset (disp_expressions, '\0', sizeof (disp_expressions));
4868 memset (im_expressions, '\0', sizeof (im_expressions));
ce8a8b2f 4869 save_stack_p = save_stack;
252b5132
RH
4870
4871 /* First parse an instruction mnemonic & call i386_operand for the operands.
4872 We assume that the scrubber has arranged it so that line[0] is the valid
47926f60 4873 start of a (possibly prefixed) mnemonic. */
252b5132 4874
29b0f896
AM
4875 line = parse_insn (line, mnemonic);
4876 if (line == NULL)
4877 return;
83b16ac6 4878 mnem_suffix = i.suffix;
252b5132 4879
29b0f896 4880 line = parse_operands (line, mnemonic);
ee86248c 4881 this_operand = -1;
8325cc63
JB
4882 xfree (i.memop1_string);
4883 i.memop1_string = NULL;
29b0f896
AM
4884 if (line == NULL)
4885 return;
252b5132 4886
29b0f896
AM
4887 /* Now we've parsed the mnemonic into a set of templates, and have the
4888 operands at hand. */
4889
b630c145 4890 /* All Intel opcodes have reversed operands except for "bound", "enter",
c0e54661 4891 "invlpg*", "monitor*", "mwait*", "tpause", "umwait", "pvalidate",
b0e8fa7f
TJ
4892 "rmpadjust", "rmpupdate", and "rmpquery". We also don't reverse
4893 intersegment "jmp" and "call" instructions with 2 immediate operands so
4894 that the immediate segment precedes the offset consistently in Intel and
4895 AT&T modes. */
4d456e3d
L
4896 if (intel_syntax
4897 && i.operands > 1
29b0f896 4898 && (strcmp (mnemonic, "bound") != 0)
c0e54661 4899 && (strncmp (mnemonic, "invlpg", 6) != 0)
d34049e8
ML
4900 && !startswith (mnemonic, "monitor")
4901 && !startswith (mnemonic, "mwait")
c0e54661 4902 && (strcmp (mnemonic, "pvalidate") != 0)
d34049e8 4903 && !startswith (mnemonic, "rmp")
b630c145
JB
4904 && (strcmp (mnemonic, "tpause") != 0)
4905 && (strcmp (mnemonic, "umwait") != 0)
47c0279b
JB
4906 && !(i.operands == 2
4907 && operand_type_check (i.types[0], imm)
40fb9820 4908 && operand_type_check (i.types[1], imm)))
29b0f896
AM
4909 swap_operands ();
4910
ec56d5c0
JB
4911 /* The order of the immediates should be reversed
4912 for 2 immediates extrq and insertq instructions */
4913 if (i.imm_operands == 2
4914 && (strcmp (mnemonic, "extrq") == 0
4915 || strcmp (mnemonic, "insertq") == 0))
4916 swap_2_operands (0, 1);
4917
29b0f896
AM
4918 if (i.imm_operands)
4919 optimize_imm ();
4920
9386188e
JB
4921 if (i.disp_operands && !want_disp32 (current_templates->start)
4922 && (!current_templates->start->opcode_modifier.jump
4923 || i.jumpabsolute || i.types[0].bitfield.baseindex))
cce08655
JB
4924 {
4925 for (j = 0; j < i.operands; ++j)
4926 {
4927 const expressionS *exp = i.op[j].disps;
4928
4929 if (!operand_type_check (i.types[j], disp))
4930 continue;
4931
4932 if (exp->X_op != O_constant)
4933 continue;
4934
4935 /* Since displacement is signed extended to 64bit, don't allow
a775efc8 4936 disp32 if it is out of range. */
cce08655
JB
4937 if (fits_in_signed_long (exp->X_add_number))
4938 continue;
4939
a775efc8 4940 i.types[j].bitfield.disp32 = 0;
cce08655
JB
4941 if (i.types[j].bitfield.baseindex)
4942 {
f493c217
AM
4943 as_bad (_("0x%" PRIx64 " out of range of signed 32bit displacement"),
4944 (uint64_t) exp->X_add_number);
cce08655
JB
4945 return;
4946 }
4947 }
4948 }
4949
b300c311
L
4950 /* Don't optimize displacement for movabs since it only takes 64bit
4951 displacement. */
4952 if (i.disp_operands
1a42a9fe 4953 && i.disp_encoding <= disp_encoding_8bit
862be3fb
L
4954 && (flag_code != CODE_64BIT
4955 || strcmp (mnemonic, "movabs") != 0))
4956 optimize_disp ();
29b0f896
AM
4957
4958 /* Next, we find a template that matches the given insn,
4959 making sure the overlap of the given operands types is consistent
4960 with the template operand types. */
252b5132 4961
83b16ac6 4962 if (!(t = match_template (mnem_suffix)))
29b0f896 4963 return;
252b5132 4964
7bab8ab5 4965 if (sse_check != check_none
ffb86450
JB
4966 /* The opcode space check isn't strictly needed; it's there only to
4967 bypass the logic below when easily possible. */
4968 && t->opcode_modifier.opcodespace >= SPACE_0F
4969 && t->opcode_modifier.opcodespace <= SPACE_0F3A
4970 && !i.tm.cpu_flags.bitfield.cpusse4a
4971 && !is_any_vex_encoding (t))
daf50ae7 4972 {
ffb86450
JB
4973 bool simd = false;
4974
4975 for (j = 0; j < t->operands; ++j)
4976 {
4977 if (t->operand_types[j].bitfield.class == RegMMX)
4978 break;
4979 if (t->operand_types[j].bitfield.class == RegSIMD)
4980 simd = true;
4981 }
4982
4983 if (j >= t->operands && simd)
4984 (sse_check == check_warning
4985 ? as_warn
4986 : as_bad) (_("SSE instruction `%s' is used"), i.tm.name);
daf50ae7
L
4987 }
4988
40fb9820 4989 if (i.tm.opcode_modifier.fwait)
29b0f896
AM
4990 if (!add_prefix (FWAIT_OPCODE))
4991 return;
252b5132 4992
d5de92cf 4993 /* Check if REP prefix is OK. */
742732c7 4994 if (i.rep_prefix && i.tm.opcode_modifier.prefixok != PrefixRep)
d5de92cf
L
4995 {
4996 as_bad (_("invalid instruction `%s' after `%s'"),
4997 i.tm.name, i.rep_prefix);
4998 return;
4999 }
5000
c1ba0266
L
5001 /* Check for lock without a lockable instruction. Destination operand
5002 must be memory unless it is xchg (0x86). */
c32fa91d 5003 if (i.prefix[LOCK_PREFIX]
742732c7 5004 && (i.tm.opcode_modifier.prefixok < PrefixLock
c1ba0266
L
5005 || i.mem_operands == 0
5006 || (i.tm.base_opcode != 0x86
8dc0818e 5007 && !(i.flags[i.operands - 1] & Operand_Mem))))
c32fa91d
L
5008 {
5009 as_bad (_("expecting lockable instruction after `lock'"));
5010 return;
5011 }
5012
40d231b4
JB
5013 /* Check for data size prefix on VEX/XOP/EVEX encoded and SIMD insns. */
5014 if (i.prefix[DATA_PREFIX]
5015 && (is_any_vex_encoding (&i.tm)
5016 || i.tm.operand_types[i.imm_operands].bitfield.class >= RegMMX
5017 || i.tm.operand_types[i.imm_operands + 1].bitfield.class >= RegMMX))
7a8655d2
JB
5018 {
5019 as_bad (_("data size prefix invalid with `%s'"), i.tm.name);
5020 return;
5021 }
5022
42164a71 5023 /* Check if HLE prefix is OK. */
165de32a 5024 if (i.hle_prefix && !check_hle ())
42164a71
L
5025 return;
5026
7e8b059b
L
5027 /* Check BND prefix. */
5028 if (i.bnd_prefix && !i.tm.opcode_modifier.bndprefixok)
5029 as_bad (_("expecting valid branch instruction after `bnd'"));
5030
04ef582a 5031 /* Check NOTRACK prefix. */
742732c7 5032 if (i.notrack_prefix && i.tm.opcode_modifier.prefixok != PrefixNoTrack)
9fef80d6 5033 as_bad (_("expecting indirect branch instruction after `notrack'"));
04ef582a 5034
327e8c42
JB
5035 if (i.tm.cpu_flags.bitfield.cpumpx)
5036 {
5037 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
5038 as_bad (_("32-bit address isn't allowed in 64-bit MPX instructions."));
5039 else if (flag_code != CODE_16BIT
5040 ? i.prefix[ADDR_PREFIX]
5041 : i.mem_operands && !i.prefix[ADDR_PREFIX])
5042 as_bad (_("16-bit address isn't allowed in MPX instructions"));
5043 }
7e8b059b
L
5044
5045 /* Insert BND prefix. */
76d3a78a
JB
5046 if (add_bnd_prefix && i.tm.opcode_modifier.bndprefixok)
5047 {
5048 if (!i.prefix[BND_PREFIX])
5049 add_prefix (BND_PREFIX_OPCODE);
5050 else if (i.prefix[BND_PREFIX] != BND_PREFIX_OPCODE)
5051 {
5052 as_warn (_("replacing `rep'/`repe' prefix by `bnd'"));
5053 i.prefix[BND_PREFIX] = BND_PREFIX_OPCODE;
5054 }
5055 }
7e8b059b 5056
29b0f896 5057 /* Check string instruction segment overrides. */
51c8edf6 5058 if (i.tm.opcode_modifier.isstring >= IS_STRING_ES_OP0)
29b0f896 5059 {
51c8edf6 5060 gas_assert (i.mem_operands);
29b0f896 5061 if (!check_string ())
5dd0794d 5062 return;
fc0763e6 5063 i.disp_operands = 0;
29b0f896 5064 }
5dd0794d 5065
9373f275
L
5066 /* The memory operand of (%dx) should be only used with input/output
5067 instructions (base opcodes: 0x6c, 0x6e, 0xec, 0xee). */
5068 if (i.input_output_operand
5069 && ((i.tm.base_opcode | 0x82) != 0xee
5070 || i.tm.opcode_modifier.opcodespace != SPACE_BASE))
5071 {
5072 as_bad (_("input/output port address isn't allowed with `%s'"),
5073 i.tm.name);
5074 return;
5075 }
5076
b6f8c7c4
L
5077 if (optimize && !i.no_optimize && i.tm.opcode_modifier.optimize)
5078 optimize_encoding ();
5079
c8480b58
L
5080 if (use_unaligned_vector_move)
5081 encode_with_unaligned_vector_move ();
5082
29b0f896
AM
5083 if (!process_suffix ())
5084 return;
e413e4e9 5085
ef07be45
CL
5086 /* Check if IP-relative addressing requirements can be satisfied. */
5087 if (i.tm.cpu_flags.bitfield.cpuprefetchi
5088 && !(i.base_reg && i.base_reg->reg_num == RegIP))
f2462532 5089 as_warn (_("'%s' only supports RIP-relative address"), i.tm.name);
ef07be45 5090
921eafea 5091 /* Update operand types and check extended states. */
bc0844ae 5092 for (j = 0; j < i.operands; j++)
921eafea
L
5093 {
5094 i.types[j] = operand_type_and (i.types[j], i.tm.operand_types[j]);
3d70986f 5095 switch (i.tm.operand_types[j].bitfield.class)
921eafea
L
5096 {
5097 default:
5098 break;
5099 case RegMMX:
5100 i.xstate |= xstate_mmx;
5101 break;
5102 case RegMask:
32930e4e 5103 i.xstate |= xstate_mask;
921eafea
L
5104 break;
5105 case RegSIMD:
3d70986f 5106 if (i.tm.operand_types[j].bitfield.tmmword)
921eafea 5107 i.xstate |= xstate_tmm;
3d70986f 5108 else if (i.tm.operand_types[j].bitfield.zmmword)
921eafea 5109 i.xstate |= xstate_zmm;
3d70986f 5110 else if (i.tm.operand_types[j].bitfield.ymmword)
921eafea 5111 i.xstate |= xstate_ymm;
3d70986f 5112 else if (i.tm.operand_types[j].bitfield.xmmword)
921eafea
L
5113 i.xstate |= xstate_xmm;
5114 break;
5115 }
5116 }
bc0844ae 5117
29b0f896
AM
5118 /* Make still unresolved immediate matches conform to size of immediate
5119 given in i.suffix. */
5120 if (!finalize_imm ())
5121 return;
252b5132 5122
40fb9820 5123 if (i.types[0].bitfield.imm1)
29b0f896 5124 i.imm_operands = 0; /* kludge for shift insns. */
252b5132 5125
9afe6eb8
L
5126 /* We only need to check those implicit registers for instructions
5127 with 3 operands or less. */
5128 if (i.operands <= 3)
5129 for (j = 0; j < i.operands; j++)
75e5731b
JB
5130 if (i.types[j].bitfield.instance != InstanceNone
5131 && !i.types[j].bitfield.xmmword)
9afe6eb8 5132 i.reg_operands--;
40fb9820 5133
29b0f896
AM
5134 /* For insns with operands there are more diddles to do to the opcode. */
5135 if (i.operands)
5136 {
5137 if (!process_operands ())
5138 return;
5139 }
255571cd 5140 else if (!quiet_warnings && i.tm.opcode_modifier.operandconstraint == UGH)
29b0f896
AM
5141 {
5142 /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc. */
5143 as_warn (_("translating to `%sp'"), i.tm.name);
5144 }
252b5132 5145
7a8655d2 5146 if (is_any_vex_encoding (&i.tm))
9e5e5283 5147 {
c1dc7af5 5148 if (!cpu_arch_flags.bitfield.cpui286)
9e5e5283 5149 {
c1dc7af5 5150 as_bad (_("instruction `%s' isn't supported outside of protected mode."),
9e5e5283
L
5151 i.tm.name);
5152 return;
5153 }
c0f3af97 5154
0b9404fd
JB
5155 /* Check for explicit REX prefix. */
5156 if (i.prefix[REX_PREFIX] || i.rex_encoding)
5157 {
5158 as_bad (_("REX prefix invalid with `%s'"), i.tm.name);
5159 return;
5160 }
5161
9e5e5283
L
5162 if (i.tm.opcode_modifier.vex)
5163 build_vex_prefix (t);
5164 else
5165 build_evex_prefix ();
0b9404fd
JB
5166
5167 /* The individual REX.RXBW bits got consumed. */
5168 i.rex &= REX_OPCODE;
9e5e5283 5169 }
43234a1e 5170
5dd85c99
SP
5171 /* Handle conversion of 'int $3' --> special int3 insn. XOP or FMA4
5172 instructions may define INT_OPCODE as well, so avoid this corner
5173 case for those instructions that use MODRM. */
389d00a5
JB
5174 if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
5175 && i.tm.base_opcode == INT_OPCODE
a6461c02
SP
5176 && !i.tm.opcode_modifier.modrm
5177 && i.op[0].imms->X_add_number == 3)
29b0f896
AM
5178 {
5179 i.tm.base_opcode = INT3_OPCODE;
5180 i.imm_operands = 0;
5181 }
252b5132 5182
0cfa3eb3
JB
5183 if ((i.tm.opcode_modifier.jump == JUMP
5184 || i.tm.opcode_modifier.jump == JUMP_BYTE
5185 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896
AM
5186 && i.op[0].disps->X_op == O_constant)
5187 {
5188 /* Convert "jmp constant" (and "call constant") to a jump (call) to
5189 the absolute address given by the constant. Since ix86 jumps and
5190 calls are pc relative, we need to generate a reloc. */
5191 i.op[0].disps->X_add_symbol = &abs_symbol;
5192 i.op[0].disps->X_op = O_symbol;
5193 }
252b5132 5194
29b0f896
AM
5195 /* For 8 bit registers we need an empty rex prefix. Also if the
5196 instruction already has a prefix, we need to convert old
5197 registers to new ones. */
773f551c 5198
bab6aec1 5199 if ((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte
29b0f896 5200 && (i.op[0].regs->reg_flags & RegRex64) != 0)
bab6aec1 5201 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte
29b0f896 5202 && (i.op[1].regs->reg_flags & RegRex64) != 0)
bab6aec1
JB
5203 || (((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte)
5204 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte))
29b0f896
AM
5205 && i.rex != 0))
5206 {
5207 int x;
726c5dcd 5208
29b0f896
AM
5209 i.rex |= REX_OPCODE;
5210 for (x = 0; x < 2; x++)
5211 {
5212 /* Look for 8 bit operand that uses old registers. */
bab6aec1 5213 if (i.types[x].bitfield.class == Reg && i.types[x].bitfield.byte
29b0f896 5214 && (i.op[x].regs->reg_flags & RegRex64) == 0)
773f551c 5215 {
3f93af61 5216 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
29b0f896
AM
5217 /* In case it is "hi" register, give up. */
5218 if (i.op[x].regs->reg_num > 3)
a540244d 5219 as_bad (_("can't encode register '%s%s' in an "
4eed87de 5220 "instruction requiring REX prefix."),
a540244d 5221 register_prefix, i.op[x].regs->reg_name);
773f551c 5222
29b0f896
AM
5223 /* Otherwise it is equivalent to the extended register.
5224 Since the encoding doesn't change this is merely
5225 cosmetic cleanup for debug output. */
5226
5227 i.op[x].regs = i.op[x].regs + 8;
773f551c 5228 }
29b0f896
AM
5229 }
5230 }
773f551c 5231
6b6b6807
L
5232 if (i.rex == 0 && i.rex_encoding)
5233 {
5234 /* Check if we can add a REX_OPCODE byte. Look for 8 bit operand
3f93af61 5235 that uses legacy register. If it is "hi" register, don't add
6b6b6807
L
5236 the REX_OPCODE byte. */
5237 int x;
5238 for (x = 0; x < 2; x++)
bab6aec1 5239 if (i.types[x].bitfield.class == Reg
6b6b6807
L
5240 && i.types[x].bitfield.byte
5241 && (i.op[x].regs->reg_flags & RegRex64) == 0
5242 && i.op[x].regs->reg_num > 3)
5243 {
3f93af61 5244 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
5b7c81bd 5245 i.rex_encoding = false;
6b6b6807
L
5246 break;
5247 }
5248
5249 if (i.rex_encoding)
5250 i.rex = REX_OPCODE;
5251 }
5252
7ab9ffdd 5253 if (i.rex != 0)
29b0f896
AM
5254 add_prefix (REX_OPCODE | i.rex);
5255
ae531041
L
5256 insert_lfence_before ();
5257
29b0f896
AM
5258 /* We are ready to output the insn. */
5259 output_insn ();
e379e5f3 5260
ae531041
L
5261 insert_lfence_after ();
5262
e379e5f3
L
5263 last_insn.seg = now_seg;
5264
5265 if (i.tm.opcode_modifier.isprefix)
5266 {
5267 last_insn.kind = last_insn_prefix;
5268 last_insn.name = i.tm.name;
5269 last_insn.file = as_where (&last_insn.line);
5270 }
5271 else
5272 last_insn.kind = last_insn_other;
29b0f896
AM
5273}
5274
5275static char *
e3bb37b5 5276parse_insn (char *line, char *mnemonic)
29b0f896
AM
5277{
5278 char *l = line;
5279 char *token_start = l;
5280 char *mnem_p;
5c6af06e 5281 int supported;
d3ce72d0 5282 const insn_template *t;
b6169b20 5283 char *dot_p = NULL;
29b0f896 5284
29b0f896
AM
5285 while (1)
5286 {
5287 mnem_p = mnemonic;
5288 while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
5289 {
b6169b20
L
5290 if (*mnem_p == '.')
5291 dot_p = mnem_p;
29b0f896
AM
5292 mnem_p++;
5293 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
45288df1 5294 {
29b0f896
AM
5295 as_bad (_("no such instruction: `%s'"), token_start);
5296 return NULL;
5297 }
5298 l++;
5299 }
5300 if (!is_space_char (*l)
5301 && *l != END_OF_INSN
e44823cf
JB
5302 && (intel_syntax
5303 || (*l != PREFIX_SEPARATOR
5304 && *l != ',')))
29b0f896
AM
5305 {
5306 as_bad (_("invalid character %s in mnemonic"),
5307 output_invalid (*l));
5308 return NULL;
5309 }
5310 if (token_start == l)
5311 {
e44823cf 5312 if (!intel_syntax && *l == PREFIX_SEPARATOR)
29b0f896
AM
5313 as_bad (_("expecting prefix; got nothing"));
5314 else
5315 as_bad (_("expecting mnemonic; got nothing"));
5316 return NULL;
5317 }
45288df1 5318
29b0f896 5319 /* Look up instruction (or prefix) via hash table. */
629310ab 5320 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
47926f60 5321
29b0f896
AM
5322 if (*l != END_OF_INSN
5323 && (!is_space_char (*l) || l[1] != END_OF_INSN)
5324 && current_templates
40fb9820 5325 && current_templates->start->opcode_modifier.isprefix)
29b0f896 5326 {
c6fb90c8 5327 if (!cpu_flags_check_cpu64 (current_templates->start->cpu_flags))
2dd88dca
JB
5328 {
5329 as_bad ((flag_code != CODE_64BIT
5330 ? _("`%s' is only supported in 64-bit mode")
5331 : _("`%s' is not supported in 64-bit mode")),
5332 current_templates->start->name);
5333 return NULL;
5334 }
29b0f896
AM
5335 /* If we are in 16-bit mode, do not allow addr16 or data16.
5336 Similarly, in 32-bit mode, do not allow addr32 or data32. */
673fe0f0
JB
5337 if ((current_templates->start->opcode_modifier.size == SIZE16
5338 || current_templates->start->opcode_modifier.size == SIZE32)
29b0f896 5339 && flag_code != CODE_64BIT
673fe0f0 5340 && ((current_templates->start->opcode_modifier.size == SIZE32)
29b0f896
AM
5341 ^ (flag_code == CODE_16BIT)))
5342 {
5343 as_bad (_("redundant %s prefix"),
5344 current_templates->start->name);
5345 return NULL;
45288df1 5346 }
31184569
JB
5347
5348 if (current_templates->start->base_opcode == PSEUDO_PREFIX)
29b0f896 5349 {
86fa6981 5350 /* Handle pseudo prefixes. */
31184569 5351 switch (current_templates->start->extension_opcode)
86fa6981 5352 {
41eb8e88 5353 case Prefix_Disp8:
86fa6981
L
5354 /* {disp8} */
5355 i.disp_encoding = disp_encoding_8bit;
5356 break;
41eb8e88
L
5357 case Prefix_Disp16:
5358 /* {disp16} */
5359 i.disp_encoding = disp_encoding_16bit;
5360 break;
5361 case Prefix_Disp32:
86fa6981
L
5362 /* {disp32} */
5363 i.disp_encoding = disp_encoding_32bit;
5364 break;
41eb8e88 5365 case Prefix_Load:
86fa6981
L
5366 /* {load} */
5367 i.dir_encoding = dir_encoding_load;
5368 break;
41eb8e88 5369 case Prefix_Store:
86fa6981
L
5370 /* {store} */
5371 i.dir_encoding = dir_encoding_store;
5372 break;
41eb8e88 5373 case Prefix_VEX:
42e04b36
L
5374 /* {vex} */
5375 i.vec_encoding = vex_encoding_vex;
86fa6981 5376 break;
41eb8e88 5377 case Prefix_VEX3:
86fa6981
L
5378 /* {vex3} */
5379 i.vec_encoding = vex_encoding_vex3;
5380 break;
41eb8e88 5381 case Prefix_EVEX:
86fa6981
L
5382 /* {evex} */
5383 i.vec_encoding = vex_encoding_evex;
5384 break;
41eb8e88 5385 case Prefix_REX:
6b6b6807 5386 /* {rex} */
5b7c81bd 5387 i.rex_encoding = true;
6b6b6807 5388 break;
41eb8e88 5389 case Prefix_NoOptimize:
b6f8c7c4 5390 /* {nooptimize} */
5b7c81bd 5391 i.no_optimize = true;
b6f8c7c4 5392 break;
86fa6981
L
5393 default:
5394 abort ();
5395 }
5396 }
5397 else
5398 {
5399 /* Add prefix, checking for repeated prefixes. */
4e9ac44a 5400 switch (add_prefix (current_templates->start->base_opcode))
86fa6981 5401 {
4e9ac44a
L
5402 case PREFIX_EXIST:
5403 return NULL;
5404 case PREFIX_DS:
d777820b 5405 if (current_templates->start->cpu_flags.bitfield.cpuibt)
4e9ac44a
L
5406 i.notrack_prefix = current_templates->start->name;
5407 break;
5408 case PREFIX_REP:
5409 if (current_templates->start->cpu_flags.bitfield.cpuhle)
5410 i.hle_prefix = current_templates->start->name;
5411 else if (current_templates->start->cpu_flags.bitfield.cpumpx)
5412 i.bnd_prefix = current_templates->start->name;
5413 else
5414 i.rep_prefix = current_templates->start->name;
5415 break;
5416 default:
5417 break;
86fa6981 5418 }
29b0f896
AM
5419 }
5420 /* Skip past PREFIX_SEPARATOR and reset token_start. */
5421 token_start = ++l;
5422 }
5423 else
5424 break;
5425 }
45288df1 5426
30a55f88 5427 if (!current_templates)
b6169b20 5428 {
07d5e953
JB
5429 /* Deprecated functionality (new code should use pseudo-prefixes instead):
5430 Check if we should swap operand or force 32bit displacement in
f8a5c266 5431 encoding. */
30a55f88 5432 if (mnem_p - 2 == dot_p && dot_p[1] == 's')
64c49ab3 5433 i.dir_encoding = dir_encoding_swap;
8d63c93e 5434 else if (mnem_p - 3 == dot_p
a501d77e
L
5435 && dot_p[1] == 'd'
5436 && dot_p[2] == '8')
5437 i.disp_encoding = disp_encoding_8bit;
8d63c93e 5438 else if (mnem_p - 4 == dot_p
f8a5c266
L
5439 && dot_p[1] == 'd'
5440 && dot_p[2] == '3'
5441 && dot_p[3] == '2')
a501d77e 5442 i.disp_encoding = disp_encoding_32bit;
30a55f88
L
5443 else
5444 goto check_suffix;
5445 mnem_p = dot_p;
5446 *dot_p = '\0';
629310ab 5447 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
b6169b20
L
5448 }
5449
29b0f896
AM
5450 if (!current_templates)
5451 {
dc1e8a47 5452 check_suffix:
1c529385 5453 if (mnem_p > mnemonic)
29b0f896 5454 {
1c529385
LH
5455 /* See if we can get a match by trimming off a suffix. */
5456 switch (mnem_p[-1])
29b0f896 5457 {
1c529385
LH
5458 case WORD_MNEM_SUFFIX:
5459 if (intel_syntax && (intel_float_operand (mnemonic) & 2))
29b0f896
AM
5460 i.suffix = SHORT_MNEM_SUFFIX;
5461 else
1c529385
LH
5462 /* Fall through. */
5463 case BYTE_MNEM_SUFFIX:
5464 case QWORD_MNEM_SUFFIX:
5465 i.suffix = mnem_p[-1];
29b0f896 5466 mnem_p[-1] = '\0';
fe0e921f
AM
5467 current_templates
5468 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5469 break;
5470 case SHORT_MNEM_SUFFIX:
5471 case LONG_MNEM_SUFFIX:
5472 if (!intel_syntax)
5473 {
5474 i.suffix = mnem_p[-1];
5475 mnem_p[-1] = '\0';
fe0e921f
AM
5476 current_templates
5477 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5478 }
5479 break;
5480
5481 /* Intel Syntax. */
5482 case 'd':
5483 if (intel_syntax)
5484 {
5485 if (intel_float_operand (mnemonic) == 1)
5486 i.suffix = SHORT_MNEM_SUFFIX;
5487 else
5488 i.suffix = LONG_MNEM_SUFFIX;
5489 mnem_p[-1] = '\0';
fe0e921f
AM
5490 current_templates
5491 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5492 }
5493 break;
29b0f896 5494 }
29b0f896 5495 }
1c529385 5496
29b0f896
AM
5497 if (!current_templates)
5498 {
5499 as_bad (_("no such instruction: `%s'"), token_start);
5500 return NULL;
5501 }
5502 }
252b5132 5503
0cfa3eb3
JB
5504 if (current_templates->start->opcode_modifier.jump == JUMP
5505 || current_templates->start->opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
5506 {
5507 /* Check for a branch hint. We allow ",pt" and ",pn" for
5508 predict taken and predict not taken respectively.
5509 I'm not sure that branch hints actually do anything on loop
5510 and jcxz insns (JumpByte) for current Pentium4 chips. They
5511 may work in the future and it doesn't hurt to accept them
5512 now. */
5513 if (l[0] == ',' && l[1] == 'p')
5514 {
5515 if (l[2] == 't')
5516 {
5517 if (!add_prefix (DS_PREFIX_OPCODE))
5518 return NULL;
5519 l += 3;
5520 }
5521 else if (l[2] == 'n')
5522 {
5523 if (!add_prefix (CS_PREFIX_OPCODE))
5524 return NULL;
5525 l += 3;
5526 }
5527 }
5528 }
5529 /* Any other comma loses. */
5530 if (*l == ',')
5531 {
5532 as_bad (_("invalid character %s in mnemonic"),
5533 output_invalid (*l));
5534 return NULL;
5535 }
252b5132 5536
29b0f896 5537 /* Check if instruction is supported on specified architecture. */
5c6af06e
JB
5538 supported = 0;
5539 for (t = current_templates->start; t < current_templates->end; ++t)
5540 {
c0f3af97
L
5541 supported |= cpu_flags_match (t);
5542 if (supported == CPU_FLAGS_PERFECT_MATCH)
d59a54c2 5543 return l;
29b0f896 5544 }
3629bb00 5545
548d0ee6
JB
5546 if (!(supported & CPU_FLAGS_64BIT_MATCH))
5547 as_bad (flag_code == CODE_64BIT
5548 ? _("`%s' is not supported in 64-bit mode")
5549 : _("`%s' is only supported in 64-bit mode"),
5550 current_templates->start->name);
5551 else
5552 as_bad (_("`%s' is not supported on `%s%s'"),
5553 current_templates->start->name,
5554 cpu_arch_name ? cpu_arch_name : default_arch,
5555 cpu_sub_arch_name ? cpu_sub_arch_name : "");
252b5132 5556
548d0ee6 5557 return NULL;
29b0f896 5558}
252b5132 5559
29b0f896 5560static char *
e3bb37b5 5561parse_operands (char *l, const char *mnemonic)
29b0f896
AM
5562{
5563 char *token_start;
3138f287 5564
29b0f896
AM
5565 /* 1 if operand is pending after ','. */
5566 unsigned int expecting_operand = 0;
252b5132 5567
29b0f896
AM
5568 while (*l != END_OF_INSN)
5569 {
e68c3d59
JB
5570 /* Non-zero if operand parens not balanced. */
5571 unsigned int paren_not_balanced = 0;
5572 /* True if inside double quotes. */
5573 bool in_quotes = false;
5574
29b0f896
AM
5575 /* Skip optional white space before operand. */
5576 if (is_space_char (*l))
5577 ++l;
d02603dc 5578 if (!is_operand_char (*l) && *l != END_OF_INSN && *l != '"')
29b0f896
AM
5579 {
5580 as_bad (_("invalid character %s before operand %d"),
5581 output_invalid (*l),
5582 i.operands + 1);
5583 return NULL;
5584 }
d02603dc 5585 token_start = l; /* After white space. */
e68c3d59 5586 while (in_quotes || paren_not_balanced || *l != ',')
29b0f896
AM
5587 {
5588 if (*l == END_OF_INSN)
5589 {
e68c3d59
JB
5590 if (in_quotes)
5591 {
5592 as_bad (_("unbalanced double quotes in operand %d."),
5593 i.operands + 1);
5594 return NULL;
5595 }
29b0f896
AM
5596 if (paren_not_balanced)
5597 {
98ff9f1c
JB
5598 know (!intel_syntax);
5599 as_bad (_("unbalanced parenthesis in operand %d."),
5600 i.operands + 1);
29b0f896
AM
5601 return NULL;
5602 }
5603 else
5604 break; /* we are done */
5605 }
e68c3d59
JB
5606 else if (*l == '\\' && l[1] == '"')
5607 ++l;
5608 else if (*l == '"')
5609 in_quotes = !in_quotes;
5610 else if (!in_quotes && !is_operand_char (*l) && !is_space_char (*l))
29b0f896
AM
5611 {
5612 as_bad (_("invalid character %s in operand %d"),
5613 output_invalid (*l),
5614 i.operands + 1);
5615 return NULL;
5616 }
e68c3d59 5617 if (!intel_syntax && !in_quotes)
29b0f896
AM
5618 {
5619 if (*l == '(')
5620 ++paren_not_balanced;
5621 if (*l == ')')
5622 --paren_not_balanced;
5623 }
29b0f896
AM
5624 l++;
5625 }
5626 if (l != token_start)
5627 { /* Yes, we've read in another operand. */
5628 unsigned int operand_ok;
5629 this_operand = i.operands++;
5630 if (i.operands > MAX_OPERANDS)
5631 {
5632 as_bad (_("spurious operands; (%d operands/instruction max)"),
5633 MAX_OPERANDS);
5634 return NULL;
5635 }
9d46ce34 5636 i.types[this_operand].bitfield.unspecified = 1;
29b0f896
AM
5637 /* Now parse operand adding info to 'i' as we go along. */
5638 END_STRING_AND_SAVE (l);
5639
1286ab78
L
5640 if (i.mem_operands > 1)
5641 {
5642 as_bad (_("too many memory references for `%s'"),
5643 mnemonic);
5644 return 0;
5645 }
5646
29b0f896
AM
5647 if (intel_syntax)
5648 operand_ok =
5649 i386_intel_operand (token_start,
5650 intel_float_operand (mnemonic));
5651 else
a7619375 5652 operand_ok = i386_att_operand (token_start);
29b0f896
AM
5653
5654 RESTORE_END_STRING (l);
5655 if (!operand_ok)
5656 return NULL;
5657 }
5658 else
5659 {
5660 if (expecting_operand)
5661 {
5662 expecting_operand_after_comma:
5663 as_bad (_("expecting operand after ','; got nothing"));
5664 return NULL;
5665 }
5666 if (*l == ',')
5667 {
5668 as_bad (_("expecting operand before ','; got nothing"));
5669 return NULL;
5670 }
5671 }
7f3f1ea2 5672
29b0f896
AM
5673 /* Now *l must be either ',' or END_OF_INSN. */
5674 if (*l == ',')
5675 {
5676 if (*++l == END_OF_INSN)
5677 {
5678 /* Just skip it, if it's \n complain. */
5679 goto expecting_operand_after_comma;
5680 }
5681 expecting_operand = 1;
5682 }
5683 }
5684 return l;
5685}
7f3f1ea2 5686
050dfa73 5687static void
783c187b 5688swap_2_operands (unsigned int xchg1, unsigned int xchg2)
050dfa73
MM
5689{
5690 union i386_op temp_op;
40fb9820 5691 i386_operand_type temp_type;
c48dadc9 5692 unsigned int temp_flags;
050dfa73 5693 enum bfd_reloc_code_real temp_reloc;
4eed87de 5694
050dfa73
MM
5695 temp_type = i.types[xchg2];
5696 i.types[xchg2] = i.types[xchg1];
5697 i.types[xchg1] = temp_type;
c48dadc9
JB
5698
5699 temp_flags = i.flags[xchg2];
5700 i.flags[xchg2] = i.flags[xchg1];
5701 i.flags[xchg1] = temp_flags;
5702
050dfa73
MM
5703 temp_op = i.op[xchg2];
5704 i.op[xchg2] = i.op[xchg1];
5705 i.op[xchg1] = temp_op;
c48dadc9 5706
050dfa73
MM
5707 temp_reloc = i.reloc[xchg2];
5708 i.reloc[xchg2] = i.reloc[xchg1];
5709 i.reloc[xchg1] = temp_reloc;
43234a1e 5710
6225c532 5711 if (i.mask.reg)
43234a1e 5712 {
6225c532
JB
5713 if (i.mask.operand == xchg1)
5714 i.mask.operand = xchg2;
5715 else if (i.mask.operand == xchg2)
5716 i.mask.operand = xchg1;
43234a1e 5717 }
a5748e0d 5718 if (i.broadcast.type || i.broadcast.bytes)
43234a1e 5719 {
5273a3cd
JB
5720 if (i.broadcast.operand == xchg1)
5721 i.broadcast.operand = xchg2;
5722 else if (i.broadcast.operand == xchg2)
5723 i.broadcast.operand = xchg1;
43234a1e 5724 }
050dfa73
MM
5725}
5726
29b0f896 5727static void
e3bb37b5 5728swap_operands (void)
29b0f896 5729{
b7c61d9a 5730 switch (i.operands)
050dfa73 5731 {
c0f3af97 5732 case 5:
b7c61d9a 5733 case 4:
4d456e3d 5734 swap_2_operands (1, i.operands - 2);
1a0670f3 5735 /* Fall through. */
b7c61d9a
L
5736 case 3:
5737 case 2:
4d456e3d 5738 swap_2_operands (0, i.operands - 1);
b7c61d9a
L
5739 break;
5740 default:
5741 abort ();
29b0f896 5742 }
29b0f896
AM
5743
5744 if (i.mem_operands == 2)
5745 {
5e042380 5746 const reg_entry *temp_seg;
29b0f896
AM
5747 temp_seg = i.seg[0];
5748 i.seg[0] = i.seg[1];
5749 i.seg[1] = temp_seg;
5750 }
5751}
252b5132 5752
29b0f896
AM
5753/* Try to ensure constant immediates are represented in the smallest
5754 opcode possible. */
5755static void
e3bb37b5 5756optimize_imm (void)
29b0f896
AM
5757{
5758 char guess_suffix = 0;
5759 int op;
252b5132 5760
29b0f896
AM
5761 if (i.suffix)
5762 guess_suffix = i.suffix;
5763 else if (i.reg_operands)
5764 {
5765 /* Figure out a suffix from the last register operand specified.
75e5731b
JB
5766 We can't do this properly yet, i.e. excluding special register
5767 instances, but the following works for instructions with
5768 immediates. In any case, we can't set i.suffix yet. */
29b0f896 5769 for (op = i.operands; --op >= 0;)
bab6aec1
JB
5770 if (i.types[op].bitfield.class != Reg)
5771 continue;
5772 else if (i.types[op].bitfield.byte)
7ab9ffdd 5773 {
40fb9820
L
5774 guess_suffix = BYTE_MNEM_SUFFIX;
5775 break;
5776 }
bab6aec1 5777 else if (i.types[op].bitfield.word)
252b5132 5778 {
40fb9820
L
5779 guess_suffix = WORD_MNEM_SUFFIX;
5780 break;
5781 }
bab6aec1 5782 else if (i.types[op].bitfield.dword)
40fb9820
L
5783 {
5784 guess_suffix = LONG_MNEM_SUFFIX;
5785 break;
5786 }
bab6aec1 5787 else if (i.types[op].bitfield.qword)
40fb9820
L
5788 {
5789 guess_suffix = QWORD_MNEM_SUFFIX;
29b0f896 5790 break;
252b5132 5791 }
29b0f896
AM
5792 }
5793 else if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
5794 guess_suffix = WORD_MNEM_SUFFIX;
5795
5796 for (op = i.operands; --op >= 0;)
40fb9820 5797 if (operand_type_check (i.types[op], imm))
29b0f896
AM
5798 {
5799 switch (i.op[op].imms->X_op)
252b5132 5800 {
29b0f896
AM
5801 case O_constant:
5802 /* If a suffix is given, this operand may be shortened. */
5803 switch (guess_suffix)
252b5132 5804 {
29b0f896 5805 case LONG_MNEM_SUFFIX:
40fb9820
L
5806 i.types[op].bitfield.imm32 = 1;
5807 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
5808 break;
5809 case WORD_MNEM_SUFFIX:
40fb9820
L
5810 i.types[op].bitfield.imm16 = 1;
5811 i.types[op].bitfield.imm32 = 1;
5812 i.types[op].bitfield.imm32s = 1;
5813 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
5814 break;
5815 case BYTE_MNEM_SUFFIX:
40fb9820
L
5816 i.types[op].bitfield.imm8 = 1;
5817 i.types[op].bitfield.imm8s = 1;
5818 i.types[op].bitfield.imm16 = 1;
5819 i.types[op].bitfield.imm32 = 1;
5820 i.types[op].bitfield.imm32s = 1;
5821 i.types[op].bitfield.imm64 = 1;
29b0f896 5822 break;
252b5132 5823 }
252b5132 5824
29b0f896
AM
5825 /* If this operand is at most 16 bits, convert it
5826 to a signed 16 bit number before trying to see
5827 whether it will fit in an even smaller size.
5828 This allows a 16-bit operand such as $0xffe0 to
5829 be recognised as within Imm8S range. */
40fb9820 5830 if ((i.types[op].bitfield.imm16)
7e96fb68 5831 && fits_in_unsigned_word (i.op[op].imms->X_add_number))
252b5132 5832 {
87ed972d
JB
5833 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
5834 ^ 0x8000) - 0x8000);
29b0f896 5835 }
a28def75
L
5836#ifdef BFD64
5837 /* Store 32-bit immediate in 64-bit for 64-bit BFD. */
40fb9820 5838 if ((i.types[op].bitfield.imm32)
7e96fb68 5839 && fits_in_unsigned_long (i.op[op].imms->X_add_number))
29b0f896
AM
5840 {
5841 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
5842 ^ ((offsetT) 1 << 31))
5843 - ((offsetT) 1 << 31));
5844 }
a28def75 5845#endif
40fb9820 5846 i.types[op]
c6fb90c8
L
5847 = operand_type_or (i.types[op],
5848 smallest_imm_type (i.op[op].imms->X_add_number));
252b5132 5849
29b0f896
AM
5850 /* We must avoid matching of Imm32 templates when 64bit
5851 only immediate is available. */
5852 if (guess_suffix == QWORD_MNEM_SUFFIX)
40fb9820 5853 i.types[op].bitfield.imm32 = 0;
29b0f896 5854 break;
252b5132 5855
29b0f896
AM
5856 case O_absent:
5857 case O_register:
5858 abort ();
5859
5860 /* Symbols and expressions. */
5861 default:
9cd96992
JB
5862 /* Convert symbolic operand to proper sizes for matching, but don't
5863 prevent matching a set of insns that only supports sizes other
5864 than those matching the insn suffix. */
5865 {
40fb9820 5866 i386_operand_type mask, allowed;
87ed972d 5867 const insn_template *t = current_templates->start;
9cd96992 5868
0dfbf9d7 5869 operand_type_set (&mask, 0);
9cd96992
JB
5870 switch (guess_suffix)
5871 {
5872 case QWORD_MNEM_SUFFIX:
40fb9820
L
5873 mask.bitfield.imm64 = 1;
5874 mask.bitfield.imm32s = 1;
9cd96992
JB
5875 break;
5876 case LONG_MNEM_SUFFIX:
40fb9820 5877 mask.bitfield.imm32 = 1;
9cd96992
JB
5878 break;
5879 case WORD_MNEM_SUFFIX:
40fb9820 5880 mask.bitfield.imm16 = 1;
9cd96992
JB
5881 break;
5882 case BYTE_MNEM_SUFFIX:
40fb9820 5883 mask.bitfield.imm8 = 1;
9cd96992
JB
5884 break;
5885 default:
9cd96992
JB
5886 break;
5887 }
8f0212ac
JB
5888
5889 allowed = operand_type_and (t->operand_types[op], mask);
5890 while (++t < current_templates->end)
5891 {
5892 allowed = operand_type_or (allowed, t->operand_types[op]);
5893 allowed = operand_type_and (allowed, mask);
5894 }
5895
0dfbf9d7 5896 if (!operand_type_all_zero (&allowed))
c6fb90c8 5897 i.types[op] = operand_type_and (i.types[op], mask);
9cd96992 5898 }
29b0f896 5899 break;
252b5132 5900 }
29b0f896
AM
5901 }
5902}
47926f60 5903
29b0f896
AM
5904/* Try to use the smallest displacement type too. */
5905static void
e3bb37b5 5906optimize_disp (void)
29b0f896
AM
5907{
5908 int op;
3e73aa7c 5909
29b0f896 5910 for (op = i.operands; --op >= 0;)
40fb9820 5911 if (operand_type_check (i.types[op], disp))
252b5132 5912 {
b300c311 5913 if (i.op[op].disps->X_op == O_constant)
252b5132 5914 {
91d6fa6a 5915 offsetT op_disp = i.op[op].disps->X_add_number;
29b0f896 5916
91d6fa6a 5917 if (!op_disp && i.types[op].bitfield.baseindex)
b300c311 5918 {
2f2be86b
JB
5919 i.types[op] = operand_type_and_not (i.types[op], anydisp);
5920 i.op[op].disps = NULL;
b300c311 5921 i.disp_operands--;
f185acdd
JB
5922 continue;
5923 }
5924
5925 if (i.types[op].bitfield.disp16
cd613c1f 5926 && fits_in_unsigned_word (op_disp))
f185acdd
JB
5927 {
5928 /* If this operand is at most 16 bits, convert
5929 to a signed 16 bit number and don't use 64bit
5930 displacement. */
5931 op_disp = ((op_disp ^ 0x8000) - 0x8000);
5932 i.types[op].bitfield.disp64 = 0;
b300c311 5933 }
f185acdd 5934
28a167a4 5935#ifdef BFD64
a50187b2 5936 /* Optimize 64-bit displacement to 32-bit for 64-bit BFD. */
a775efc8
JB
5937 if ((flag_code != CODE_64BIT
5938 ? i.types[op].bitfield.disp32
5939 : want_disp32 (current_templates->start)
5940 && (!current_templates->start->opcode_modifier.jump
5941 || i.jumpabsolute || i.types[op].bitfield.baseindex))
a50187b2 5942 && fits_in_unsigned_long (op_disp))
b300c311 5943 {
a50187b2
JB
5944 /* If this operand is at most 32 bits, convert
5945 to a signed 32 bit number and don't use 64bit
5946 displacement. */
5947 op_disp = (op_disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
5948 i.types[op].bitfield.disp64 = 0;
5949 i.types[op].bitfield.disp32 = 1;
5950 }
28a167a4 5951
a50187b2
JB
5952 if (flag_code == CODE_64BIT && fits_in_signed_long (op_disp))
5953 {
5954 i.types[op].bitfield.disp64 = 0;
a775efc8 5955 i.types[op].bitfield.disp32 = 1;
b300c311 5956 }
28a167a4 5957#endif
40fb9820 5958 if ((i.types[op].bitfield.disp32
40fb9820 5959 || i.types[op].bitfield.disp16)
b5014f7a 5960 && fits_in_disp8 (op_disp))
40fb9820 5961 i.types[op].bitfield.disp8 = 1;
77c59789
JB
5962
5963 i.op[op].disps->X_add_number = op_disp;
252b5132 5964 }
67a4f2b7
AO
5965 else if (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
5966 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL)
5967 {
5968 fix_new_exp (frag_now, frag_more (0) - frag_now->fr_literal, 0,
5969 i.op[op].disps, 0, i.reloc[op]);
2f2be86b 5970 i.types[op] = operand_type_and_not (i.types[op], anydisp);
67a4f2b7
AO
5971 }
5972 else
b300c311 5973 /* We only support 64bit displacement on constants. */
40fb9820 5974 i.types[op].bitfield.disp64 = 0;
252b5132 5975 }
29b0f896
AM
5976}
5977
4a1b91ea
L
5978/* Return 1 if there is a match in broadcast bytes between operand
5979 GIVEN and instruction template T. */
5980
5981static INLINE int
5982match_broadcast_size (const insn_template *t, unsigned int given)
5983{
5984 return ((t->opcode_modifier.broadcast == BYTE_BROADCAST
5985 && i.types[given].bitfield.byte)
5986 || (t->opcode_modifier.broadcast == WORD_BROADCAST
5987 && i.types[given].bitfield.word)
5988 || (t->opcode_modifier.broadcast == DWORD_BROADCAST
5989 && i.types[given].bitfield.dword)
5990 || (t->opcode_modifier.broadcast == QWORD_BROADCAST
5991 && i.types[given].bitfield.qword));
5992}
5993
6c30d220
L
5994/* Check if operands are valid for the instruction. */
5995
5996static int
5997check_VecOperands (const insn_template *t)
5998{
43234a1e 5999 unsigned int op;
e2195274 6000 i386_cpu_flags cpu;
e2195274
JB
6001
6002 /* Templates allowing for ZMMword as well as YMMword and/or XMMword for
6003 any one operand are implicity requiring AVX512VL support if the actual
6004 operand size is YMMword or XMMword. Since this function runs after
6005 template matching, there's no need to check for YMMword/XMMword in
6006 the template. */
6007 cpu = cpu_flags_and (t->cpu_flags, avx512);
6008 if (!cpu_flags_all_zero (&cpu)
6009 && !t->cpu_flags.bitfield.cpuavx512vl
6010 && !cpu_arch_flags.bitfield.cpuavx512vl)
6011 {
6012 for (op = 0; op < t->operands; ++op)
6013 {
6014 if (t->operand_types[op].bitfield.zmmword
6015 && (i.types[op].bitfield.ymmword
6016 || i.types[op].bitfield.xmmword))
6017 {
6018 i.error = unsupported;
6019 return 1;
6020 }
6021 }
6022 }
43234a1e 6023
22c36940
JB
6024 /* Somewhat similarly, templates specifying both AVX and AVX2 are
6025 requiring AVX2 support if the actual operand size is YMMword. */
6026 if (t->cpu_flags.bitfield.cpuavx
6027 && t->cpu_flags.bitfield.cpuavx2
6028 && !cpu_arch_flags.bitfield.cpuavx2)
6029 {
6030 for (op = 0; op < t->operands; ++op)
6031 {
6032 if (t->operand_types[op].bitfield.xmmword
6033 && i.types[op].bitfield.ymmword)
6034 {
6035 i.error = unsupported;
6036 return 1;
6037 }
6038 }
6039 }
6040
6c30d220 6041 /* Without VSIB byte, we can't have a vector register for index. */
63112cd6 6042 if (!t->opcode_modifier.sib
6c30d220 6043 && i.index_reg
1b54b8d7
JB
6044 && (i.index_reg->reg_type.bitfield.xmmword
6045 || i.index_reg->reg_type.bitfield.ymmword
6046 || i.index_reg->reg_type.bitfield.zmmword))
6c30d220
L
6047 {
6048 i.error = unsupported_vector_index_register;
6049 return 1;
6050 }
6051
ad8ecc81 6052 /* Check if default mask is allowed. */
255571cd 6053 if (t->opcode_modifier.operandconstraint == NO_DEFAULT_MASK
6225c532 6054 && (!i.mask.reg || i.mask.reg->reg_num == 0))
ad8ecc81
MZ
6055 {
6056 i.error = no_default_mask;
6057 return 1;
6058 }
6059
7bab8ab5
JB
6060 /* For VSIB byte, we need a vector register for index, and all vector
6061 registers must be distinct. */
260cd341 6062 if (t->opcode_modifier.sib && t->opcode_modifier.sib != SIBMEM)
7bab8ab5
JB
6063 {
6064 if (!i.index_reg
63112cd6 6065 || !((t->opcode_modifier.sib == VECSIB128
1b54b8d7 6066 && i.index_reg->reg_type.bitfield.xmmword)
63112cd6 6067 || (t->opcode_modifier.sib == VECSIB256
1b54b8d7 6068 && i.index_reg->reg_type.bitfield.ymmword)
63112cd6 6069 || (t->opcode_modifier.sib == VECSIB512
1b54b8d7 6070 && i.index_reg->reg_type.bitfield.zmmword)))
7bab8ab5
JB
6071 {
6072 i.error = invalid_vsib_address;
6073 return 1;
6074 }
6075
6225c532
JB
6076 gas_assert (i.reg_operands == 2 || i.mask.reg);
6077 if (i.reg_operands == 2 && !i.mask.reg)
43234a1e 6078 {
3528c362 6079 gas_assert (i.types[0].bitfield.class == RegSIMD);
1b54b8d7
JB
6080 gas_assert (i.types[0].bitfield.xmmword
6081 || i.types[0].bitfield.ymmword);
3528c362 6082 gas_assert (i.types[2].bitfield.class == RegSIMD);
1b54b8d7
JB
6083 gas_assert (i.types[2].bitfield.xmmword
6084 || i.types[2].bitfield.ymmword);
43234a1e
L
6085 if (operand_check == check_none)
6086 return 0;
6087 if (register_number (i.op[0].regs)
6088 != register_number (i.index_reg)
6089 && register_number (i.op[2].regs)
6090 != register_number (i.index_reg)
6091 && register_number (i.op[0].regs)
6092 != register_number (i.op[2].regs))
6093 return 0;
6094 if (operand_check == check_error)
6095 {
6096 i.error = invalid_vector_register_set;
6097 return 1;
6098 }
6099 as_warn (_("mask, index, and destination registers should be distinct"));
6100 }
6225c532 6101 else if (i.reg_operands == 1 && i.mask.reg)
8444f82a 6102 {
3528c362 6103 if (i.types[1].bitfield.class == RegSIMD
1b54b8d7
JB
6104 && (i.types[1].bitfield.xmmword
6105 || i.types[1].bitfield.ymmword
6106 || i.types[1].bitfield.zmmword)
8444f82a
MZ
6107 && (register_number (i.op[1].regs)
6108 == register_number (i.index_reg)))
6109 {
6110 if (operand_check == check_error)
6111 {
6112 i.error = invalid_vector_register_set;
6113 return 1;
6114 }
6115 if (operand_check != check_none)
6116 as_warn (_("index and destination registers should be distinct"));
6117 }
6118 }
43234a1e 6119 }
7bab8ab5 6120
fc141319
L
6121 /* For AMX instructions with 3 TMM register operands, all operands
6122 must be distinct. */
6123 if (i.reg_operands == 3
6124 && t->operand_types[0].bitfield.tmmword
6125 && (i.op[0].regs == i.op[1].regs
6126 || i.op[0].regs == i.op[2].regs
6127 || i.op[1].regs == i.op[2].regs))
6128 {
6129 i.error = invalid_tmm_register_set;
6130 return 1;
260cd341
LC
6131 }
6132
0cc78721
CL
6133 /* For some special instructions require that destination must be distinct
6134 from source registers. */
255571cd 6135 if (t->opcode_modifier.operandconstraint == DISTINCT_DEST)
0cc78721
CL
6136 {
6137 unsigned int dest_reg = i.operands - 1;
6138
6139 know (i.operands >= 3);
6140
6141 /* #UD if dest_reg == src1_reg or dest_reg == src2_reg. */
6142 if (i.op[dest_reg - 1].regs == i.op[dest_reg].regs
6143 || (i.reg_operands > 2
6144 && i.op[dest_reg - 2].regs == i.op[dest_reg].regs))
6145 {
6146 i.error = invalid_dest_and_src_register_set;
6147 return 1;
6148 }
6149 }
6150
43234a1e
L
6151 /* Check if broadcast is supported by the instruction and is applied
6152 to the memory operand. */
a5748e0d 6153 if (i.broadcast.type || i.broadcast.bytes)
43234a1e 6154 {
8e6e0792 6155 i386_operand_type type, overlap;
43234a1e
L
6156
6157 /* Check if specified broadcast is supported in this instruction,
4a1b91ea 6158 and its broadcast bytes match the memory operand. */
5273a3cd 6159 op = i.broadcast.operand;
8e6e0792 6160 if (!t->opcode_modifier.broadcast
c48dadc9 6161 || !(i.flags[op] & Operand_Mem)
c39e5b26 6162 || (!i.types[op].bitfield.unspecified
4a1b91ea 6163 && !match_broadcast_size (t, op)))
43234a1e
L
6164 {
6165 bad_broadcast:
6166 i.error = unsupported_broadcast;
6167 return 1;
6168 }
8e6e0792 6169
a5748e0d
JB
6170 if (i.broadcast.type)
6171 i.broadcast.bytes = ((1 << (t->opcode_modifier.broadcast - 1))
6172 * i.broadcast.type);
8e6e0792 6173 operand_type_set (&type, 0);
a5748e0d 6174 switch (get_broadcast_bytes (t, false))
8e6e0792 6175 {
4a1b91ea
L
6176 case 2:
6177 type.bitfield.word = 1;
6178 break;
6179 case 4:
6180 type.bitfield.dword = 1;
6181 break;
8e6e0792
JB
6182 case 8:
6183 type.bitfield.qword = 1;
6184 break;
6185 case 16:
6186 type.bitfield.xmmword = 1;
6187 break;
6188 case 32:
6189 type.bitfield.ymmword = 1;
6190 break;
6191 case 64:
6192 type.bitfield.zmmword = 1;
6193 break;
6194 default:
6195 goto bad_broadcast;
6196 }
6197
6198 overlap = operand_type_and (type, t->operand_types[op]);
bc49bfd8
JB
6199 if (t->operand_types[op].bitfield.class == RegSIMD
6200 && t->operand_types[op].bitfield.byte
6201 + t->operand_types[op].bitfield.word
6202 + t->operand_types[op].bitfield.dword
6203 + t->operand_types[op].bitfield.qword > 1)
6204 {
6205 overlap.bitfield.xmmword = 0;
6206 overlap.bitfield.ymmword = 0;
6207 overlap.bitfield.zmmword = 0;
6208 }
8e6e0792
JB
6209 if (operand_type_all_zero (&overlap))
6210 goto bad_broadcast;
6211
6212 if (t->opcode_modifier.checkregsize)
6213 {
6214 unsigned int j;
6215
e2195274 6216 type.bitfield.baseindex = 1;
8e6e0792
JB
6217 for (j = 0; j < i.operands; ++j)
6218 {
6219 if (j != op
6220 && !operand_type_register_match(i.types[j],
6221 t->operand_types[j],
6222 type,
6223 t->operand_types[op]))
6224 goto bad_broadcast;
6225 }
6226 }
43234a1e
L
6227 }
6228 /* If broadcast is supported in this instruction, we need to check if
6229 operand of one-element size isn't specified without broadcast. */
6230 else if (t->opcode_modifier.broadcast && i.mem_operands)
6231 {
6232 /* Find memory operand. */
6233 for (op = 0; op < i.operands; op++)
8dc0818e 6234 if (i.flags[op] & Operand_Mem)
43234a1e
L
6235 break;
6236 gas_assert (op < i.operands);
6237 /* Check size of the memory operand. */
4a1b91ea 6238 if (match_broadcast_size (t, op))
43234a1e
L
6239 {
6240 i.error = broadcast_needed;
6241 return 1;
6242 }
6243 }
c39e5b26
JB
6244 else
6245 op = MAX_OPERANDS - 1; /* Avoid uninitialized variable warning. */
43234a1e
L
6246
6247 /* Check if requested masking is supported. */
6225c532 6248 if (i.mask.reg)
43234a1e 6249 {
ae2387fe
JB
6250 switch (t->opcode_modifier.masking)
6251 {
6252 case BOTH_MASKING:
6253 break;
6254 case MERGING_MASKING:
6225c532 6255 if (i.mask.zeroing)
ae2387fe
JB
6256 {
6257 case 0:
6258 i.error = unsupported_masking;
6259 return 1;
6260 }
6261 break;
6262 case DYNAMIC_MASKING:
6263 /* Memory destinations allow only merging masking. */
6225c532 6264 if (i.mask.zeroing && i.mem_operands)
ae2387fe
JB
6265 {
6266 /* Find memory operand. */
6267 for (op = 0; op < i.operands; op++)
c48dadc9 6268 if (i.flags[op] & Operand_Mem)
ae2387fe
JB
6269 break;
6270 gas_assert (op < i.operands);
6271 if (op == i.operands - 1)
6272 {
6273 i.error = unsupported_masking;
6274 return 1;
6275 }
6276 }
6277 break;
6278 default:
6279 abort ();
6280 }
43234a1e
L
6281 }
6282
6283 /* Check if masking is applied to dest operand. */
6225c532 6284 if (i.mask.reg && (i.mask.operand != i.operands - 1))
43234a1e
L
6285 {
6286 i.error = mask_not_on_destination;
6287 return 1;
6288 }
6289
43234a1e 6290 /* Check RC/SAE. */
ca5312a2 6291 if (i.rounding.type != rc_none)
43234a1e 6292 {
a80195f1 6293 if (!t->opcode_modifier.sae
cf665fee
JB
6294 || ((i.rounding.type != saeonly) != t->opcode_modifier.staticrounding)
6295 || i.mem_operands)
43234a1e
L
6296 {
6297 i.error = unsupported_rc_sae;
6298 return 1;
6299 }
cf665fee
JB
6300
6301 /* Non-EVEX.LIG forms need to have a ZMM register as at least one
6302 operand. */
6303 if (t->opcode_modifier.evex != EVEXLIG)
7bab8ab5 6304 {
cf665fee
JB
6305 for (op = 0; op < t->operands; ++op)
6306 if (i.types[op].bitfield.zmmword)
6307 break;
6308 if (op >= t->operands)
6309 {
6310 i.error = operand_size_mismatch;
6311 return 1;
6312 }
7bab8ab5 6313 }
6c30d220
L
6314 }
6315
da4977e0
JB
6316 /* Check the special Imm4 cases; must be the first operand. */
6317 if (t->cpu_flags.bitfield.cpuxop && t->operands == 5)
6318 {
6319 if (i.op[0].imms->X_op != O_constant
6320 || !fits_in_imm4 (i.op[0].imms->X_add_number))
6321 {
6322 i.error = bad_imm4;
6323 return 1;
6324 }
6325
6326 /* Turn off Imm<N> so that update_imm won't complain. */
6327 operand_type_set (&i.types[0], 0);
6328 }
6329
43234a1e 6330 /* Check vector Disp8 operand. */
b5014f7a 6331 if (t->opcode_modifier.disp8memshift
1a42a9fe 6332 && i.disp_encoding <= disp_encoding_8bit)
43234a1e 6333 {
a5748e0d 6334 if (i.broadcast.bytes)
4a1b91ea 6335 i.memshift = t->opcode_modifier.broadcast - 1;
7091c612 6336 else if (t->opcode_modifier.disp8memshift != DISP8_SHIFT_VL)
43234a1e 6337 i.memshift = t->opcode_modifier.disp8memshift;
7091c612
JB
6338 else
6339 {
125ff819 6340 const i386_operand_type *type = NULL, *fallback = NULL;
7091c612
JB
6341
6342 i.memshift = 0;
6343 for (op = 0; op < i.operands; op++)
8dc0818e 6344 if (i.flags[op] & Operand_Mem)
7091c612 6345 {
4174bfff
JB
6346 if (t->opcode_modifier.evex == EVEXLIG)
6347 i.memshift = 2 + (i.suffix == QWORD_MNEM_SUFFIX);
6348 else if (t->operand_types[op].bitfield.xmmword
6349 + t->operand_types[op].bitfield.ymmword
6350 + t->operand_types[op].bitfield.zmmword <= 1)
7091c612
JB
6351 type = &t->operand_types[op];
6352 else if (!i.types[op].bitfield.unspecified)
6353 type = &i.types[op];
125ff819
JB
6354 else /* Ambiguities get resolved elsewhere. */
6355 fallback = &t->operand_types[op];
7091c612 6356 }
3528c362 6357 else if (i.types[op].bitfield.class == RegSIMD
4174bfff 6358 && t->opcode_modifier.evex != EVEXLIG)
7091c612
JB
6359 {
6360 if (i.types[op].bitfield.zmmword)
6361 i.memshift = 6;
6362 else if (i.types[op].bitfield.ymmword && i.memshift < 5)
6363 i.memshift = 5;
6364 else if (i.types[op].bitfield.xmmword && i.memshift < 4)
6365 i.memshift = 4;
6366 }
6367
125ff819
JB
6368 if (!type && !i.memshift)
6369 type = fallback;
7091c612
JB
6370 if (type)
6371 {
6372 if (type->bitfield.zmmword)
6373 i.memshift = 6;
6374 else if (type->bitfield.ymmword)
6375 i.memshift = 5;
6376 else if (type->bitfield.xmmword)
6377 i.memshift = 4;
6378 }
6379
6380 /* For the check in fits_in_disp8(). */
6381 if (i.memshift == 0)
6382 i.memshift = -1;
6383 }
43234a1e
L
6384
6385 for (op = 0; op < i.operands; op++)
6386 if (operand_type_check (i.types[op], disp)
6387 && i.op[op].disps->X_op == O_constant)
6388 {
b5014f7a 6389 if (fits_in_disp8 (i.op[op].disps->X_add_number))
43234a1e 6390 {
b5014f7a
JB
6391 i.types[op].bitfield.disp8 = 1;
6392 return 0;
43234a1e 6393 }
b5014f7a 6394 i.types[op].bitfield.disp8 = 0;
43234a1e
L
6395 }
6396 }
b5014f7a
JB
6397
6398 i.memshift = 0;
43234a1e 6399
6c30d220
L
6400 return 0;
6401}
6402
da4977e0 6403/* Check if encoding requirements are met by the instruction. */
a683cc34
SP
6404
6405static int
da4977e0 6406VEX_check_encoding (const insn_template *t)
a683cc34 6407{
da4977e0
JB
6408 if (i.vec_encoding == vex_encoding_error)
6409 {
6410 i.error = unsupported;
6411 return 1;
6412 }
6413
86fa6981 6414 if (i.vec_encoding == vex_encoding_evex)
43234a1e 6415 {
86fa6981 6416 /* This instruction must be encoded with EVEX prefix. */
e771e7c9 6417 if (!is_evex_encoding (t))
86fa6981
L
6418 {
6419 i.error = unsupported;
6420 return 1;
6421 }
6422 return 0;
43234a1e
L
6423 }
6424
a683cc34 6425 if (!t->opcode_modifier.vex)
86fa6981
L
6426 {
6427 /* This instruction template doesn't have VEX prefix. */
6428 if (i.vec_encoding != vex_encoding_default)
6429 {
6430 i.error = unsupported;
6431 return 1;
6432 }
6433 return 0;
6434 }
a683cc34 6435
a683cc34
SP
6436 return 0;
6437}
6438
7b94647a
JB
6439/* Helper function for the progress() macro in match_template(). */
6440static INLINE enum i386_error progress (enum i386_error new,
6441 enum i386_error last,
6442 unsigned int line, unsigned int *line_p)
6443{
6444 if (line <= *line_p)
6445 return last;
6446 *line_p = line;
6447 return new;
6448}
6449
d3ce72d0 6450static const insn_template *
83b16ac6 6451match_template (char mnem_suffix)
29b0f896
AM
6452{
6453 /* Points to template once we've found it. */
d3ce72d0 6454 const insn_template *t;
40fb9820 6455 i386_operand_type overlap0, overlap1, overlap2, overlap3;
c0f3af97 6456 i386_operand_type overlap4;
29b0f896 6457 unsigned int found_reverse_match;
dc2be329 6458 i386_opcode_modifier suffix_check;
40fb9820 6459 i386_operand_type operand_types [MAX_OPERANDS];
539e75ad 6460 int addr_prefix_disp;
7b94647a
JB
6461 unsigned int j, size_match, check_register, errline = __LINE__;
6462 enum i386_error specific_error = number_of_operands_mismatch;
6463#define progress(err) progress (err, specific_error, __LINE__, &errline)
29b0f896 6464
c0f3af97
L
6465#if MAX_OPERANDS != 5
6466# error "MAX_OPERANDS must be 5."
f48ff2ae
L
6467#endif
6468
29b0f896 6469 found_reverse_match = 0;
539e75ad 6470 addr_prefix_disp = -1;
40fb9820 6471
dc2be329 6472 /* Prepare for mnemonic suffix check. */
40fb9820 6473 memset (&suffix_check, 0, sizeof (suffix_check));
dc2be329
L
6474 switch (mnem_suffix)
6475 {
6476 case BYTE_MNEM_SUFFIX:
6477 suffix_check.no_bsuf = 1;
6478 break;
6479 case WORD_MNEM_SUFFIX:
6480 suffix_check.no_wsuf = 1;
6481 break;
6482 case SHORT_MNEM_SUFFIX:
6483 suffix_check.no_ssuf = 1;
6484 break;
6485 case LONG_MNEM_SUFFIX:
6486 suffix_check.no_lsuf = 1;
6487 break;
6488 case QWORD_MNEM_SUFFIX:
6489 suffix_check.no_qsuf = 1;
6490 break;
6491 default:
6492 /* NB: In Intel syntax, normally we can check for memory operand
6493 size when there is no mnemonic suffix. But jmp and call have
6494 2 different encodings with Dword memory operand size, one with
6495 No_ldSuf and the other without. i.suffix is set to
6496 LONG_DOUBLE_MNEM_SUFFIX to skip the one with No_ldSuf. */
6497 if (i.suffix == LONG_DOUBLE_MNEM_SUFFIX)
6498 suffix_check.no_ldsuf = 1;
83b16ac6
JB
6499 }
6500
45aa61fe 6501 for (t = current_templates->start; t < current_templates->end; t++)
29b0f896 6502 {
539e75ad 6503 addr_prefix_disp = -1;
dbbc8b7e 6504 found_reverse_match = 0;
539e75ad 6505
7b94647a 6506 /* Must have right number of operands. */
29b0f896
AM
6507 if (i.operands != t->operands)
6508 continue;
6509
50aecf8c 6510 /* Check processor support. */
7b94647a 6511 specific_error = progress (unsupported);
45a4bb20 6512 if (cpu_flags_match (t) != CPU_FLAGS_PERFECT_MATCH)
50aecf8c
L
6513 continue;
6514
e1d4d893 6515 /* Check AT&T mnemonic. */
7b94647a 6516 specific_error = progress (unsupported_with_intel_mnemonic);
e1d4d893 6517 if (intel_mnemonic && t->opcode_modifier.attmnemonic)
1efbbeb4
L
6518 continue;
6519
4b5aaf5f 6520 /* Check AT&T/Intel syntax. */
7b94647a 6521 specific_error = progress (unsupported_syntax);
5c07affc 6522 if ((intel_syntax && t->opcode_modifier.attsyntax)
4b5aaf5f 6523 || (!intel_syntax && t->opcode_modifier.intelsyntax))
1efbbeb4
L
6524 continue;
6525
4b5aaf5f
L
6526 /* Check Intel64/AMD64 ISA. */
6527 switch (isa64)
6528 {
6529 default:
6530 /* Default: Don't accept Intel64. */
6531 if (t->opcode_modifier.isa64 == INTEL64)
6532 continue;
6533 break;
6534 case amd64:
6535 /* -mamd64: Don't accept Intel64 and Intel64 only. */
6536 if (t->opcode_modifier.isa64 >= INTEL64)
6537 continue;
6538 break;
6539 case intel64:
6540 /* -mintel64: Don't accept AMD64. */
5990e377 6541 if (t->opcode_modifier.isa64 == AMD64 && flag_code == CODE_64BIT)
4b5aaf5f
L
6542 continue;
6543 break;
6544 }
6545
dc2be329 6546 /* Check the suffix. */
7b94647a 6547 specific_error = progress (invalid_instruction_suffix);
dc2be329
L
6548 if ((t->opcode_modifier.no_bsuf && suffix_check.no_bsuf)
6549 || (t->opcode_modifier.no_wsuf && suffix_check.no_wsuf)
6550 || (t->opcode_modifier.no_lsuf && suffix_check.no_lsuf)
6551 || (t->opcode_modifier.no_ssuf && suffix_check.no_ssuf)
6552 || (t->opcode_modifier.no_qsuf && suffix_check.no_qsuf)
6553 || (t->opcode_modifier.no_ldsuf && suffix_check.no_ldsuf))
83b16ac6 6554 continue;
29b0f896 6555
7b94647a 6556 specific_error = progress (operand_size_mismatch);
3ac21baa
JB
6557 size_match = operand_size_match (t);
6558 if (!size_match)
7d5e4556 6559 continue;
539e75ad 6560
6f2f06be
JB
6561 /* This is intentionally not
6562
0cfa3eb3 6563 if (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE))
6f2f06be
JB
6564
6565 as the case of a missing * on the operand is accepted (perhaps with
6566 a warning, issued further down). */
7b94647a 6567 specific_error = progress (operand_type_mismatch);
0cfa3eb3 6568 if (i.jumpabsolute && t->opcode_modifier.jump != JUMP_ABSOLUTE)
7b94647a 6569 continue;
6f2f06be 6570
5c07affc
L
6571 for (j = 0; j < MAX_OPERANDS; j++)
6572 operand_types[j] = t->operand_types[j];
6573
e365e234
JB
6574 /* In general, don't allow
6575 - 64-bit operands outside of 64-bit mode,
6576 - 32-bit operands on pre-386. */
7b94647a
JB
6577 specific_error = progress (mnem_suffix ? invalid_instruction_suffix
6578 : operand_size_mismatch);
4873e243 6579 j = i.imm_operands + (t->operands > i.imm_operands + 1);
e365e234
JB
6580 if (((i.suffix == QWORD_MNEM_SUFFIX
6581 && flag_code != CODE_64BIT
389d00a5
JB
6582 && !(t->opcode_modifier.opcodespace == SPACE_0F
6583 && t->base_opcode == 0xc7
5e74b495 6584 && t->opcode_modifier.opcodeprefix == PREFIX_NONE
8b65b895 6585 && t->extension_opcode == 1) /* cmpxchg8b */)
e365e234
JB
6586 || (i.suffix == LONG_MNEM_SUFFIX
6587 && !cpu_arch_flags.bitfield.cpui386))
45aa61fe 6588 && (intel_syntax
3cd7f3e3 6589 ? (t->opcode_modifier.mnemonicsize != IGNORESIZE
45aa61fe
AM
6590 && !intel_float_operand (t->name))
6591 : intel_float_operand (t->name) != 2)
4873e243
JB
6592 && (t->operands == i.imm_operands
6593 || (operand_types[i.imm_operands].bitfield.class != RegMMX
6594 && operand_types[i.imm_operands].bitfield.class != RegSIMD
6595 && operand_types[i.imm_operands].bitfield.class != RegMask)
6596 || (operand_types[j].bitfield.class != RegMMX
6597 && operand_types[j].bitfield.class != RegSIMD
6598 && operand_types[j].bitfield.class != RegMask))
63112cd6 6599 && !t->opcode_modifier.sib)
192dc9c6
JB
6600 continue;
6601
29b0f896 6602 /* Do not verify operands when there are none. */
e365e234 6603 if (!t->operands)
da4977e0
JB
6604 {
6605 if (VEX_check_encoding (t))
6606 {
7b94647a 6607 specific_error = progress (i.error);
da4977e0
JB
6608 continue;
6609 }
6610
6611 /* We've found a match; break out of loop. */
6612 break;
6613 }
252b5132 6614
48bcea9f
JB
6615 if (!t->opcode_modifier.jump
6616 || t->opcode_modifier.jump == JUMP_ABSOLUTE)
6617 {
6618 /* There should be only one Disp operand. */
6619 for (j = 0; j < MAX_OPERANDS; j++)
6620 if (operand_type_check (operand_types[j], disp))
539e75ad 6621 break;
48bcea9f
JB
6622 if (j < MAX_OPERANDS)
6623 {
5b7c81bd 6624 bool override = (i.prefix[ADDR_PREFIX] != 0);
48bcea9f
JB
6625
6626 addr_prefix_disp = j;
6627
a775efc8
JB
6628 /* Address size prefix will turn Disp64 operand into Disp32 and
6629 Disp32/Disp16 one into Disp16/Disp32 respectively. */
48bcea9f 6630 switch (flag_code)
40fb9820 6631 {
48bcea9f
JB
6632 case CODE_16BIT:
6633 override = !override;
6634 /* Fall through. */
6635 case CODE_32BIT:
6636 if (operand_types[j].bitfield.disp32
6637 && operand_types[j].bitfield.disp16)
40fb9820 6638 {
48bcea9f
JB
6639 operand_types[j].bitfield.disp16 = override;
6640 operand_types[j].bitfield.disp32 = !override;
40fb9820 6641 }
a775efc8 6642 gas_assert (!operand_types[j].bitfield.disp64);
48bcea9f
JB
6643 break;
6644
6645 case CODE_64BIT:
a775efc8 6646 if (operand_types[j].bitfield.disp64)
40fb9820 6647 {
a775efc8 6648 gas_assert (!operand_types[j].bitfield.disp32);
48bcea9f 6649 operand_types[j].bitfield.disp32 = override;
a775efc8 6650 operand_types[j].bitfield.disp64 = !override;
40fb9820 6651 }
48bcea9f
JB
6652 operand_types[j].bitfield.disp16 = 0;
6653 break;
40fb9820 6654 }
539e75ad 6655 }
48bcea9f 6656 }
539e75ad 6657
d7e3e627
L
6658 switch (i.reloc[0])
6659 {
6660 case BFD_RELOC_386_GOT32:
6661 /* Force 0x8b encoding for "mov foo@GOT, %eax". */
6662 if (t->base_opcode == 0xa0
6663 && t->opcode_modifier.opcodespace == SPACE_BASE)
6664 continue;
6665 break;
6666 case BFD_RELOC_386_TLS_GOTIE:
6667 case BFD_RELOC_386_TLS_LE_32:
6668 case BFD_RELOC_X86_64_GOTTPOFF:
6669 case BFD_RELOC_X86_64_TLSLD:
6670 /* Don't allow KMOV in TLS code sequences. */
6671 if (t->opcode_modifier.vex)
6672 continue;
6673 break;
6674 default:
6675 break;
6676 }
02a86693 6677
56ffb741 6678 /* We check register size if needed. */
e2195274
JB
6679 if (t->opcode_modifier.checkregsize)
6680 {
6681 check_register = (1 << t->operands) - 1;
a5748e0d 6682 if (i.broadcast.type || i.broadcast.bytes)
5273a3cd 6683 check_register &= ~(1 << i.broadcast.operand);
e2195274
JB
6684 }
6685 else
6686 check_register = 0;
6687
c6fb90c8 6688 overlap0 = operand_type_and (i.types[0], operand_types[0]);
29b0f896
AM
6689 switch (t->operands)
6690 {
6691 case 1:
40fb9820 6692 if (!operand_type_match (overlap0, i.types[0]))
29b0f896
AM
6693 continue;
6694 break;
6695 case 2:
33eaf5de 6696 /* xchg %eax, %eax is a special case. It is an alias for nop
8b38ad71
L
6697 only in 32bit mode and we can use opcode 0x90. In 64bit
6698 mode, we can't use 0x90 for xchg %eax, %eax since it should
6699 zero-extend %eax to %rax. */
6700 if (flag_code == CODE_64BIT
6701 && t->base_opcode == 0x90
35648716 6702 && t->opcode_modifier.opcodespace == SPACE_BASE
75e5731b
JB
6703 && i.types[0].bitfield.instance == Accum
6704 && i.types[0].bitfield.dword
6705 && i.types[1].bitfield.instance == Accum
6706 && i.types[1].bitfield.dword)
8b38ad71 6707 continue;
1212781b
JB
6708 /* xrelease mov %eax, <disp> is another special case. It must not
6709 match the accumulator-only encoding of mov. */
6710 if (flag_code != CODE_64BIT
6711 && i.hle_prefix
6712 && t->base_opcode == 0xa0
35648716 6713 && t->opcode_modifier.opcodespace == SPACE_BASE
75e5731b 6714 && i.types[0].bitfield.instance == Accum
8dc0818e 6715 && (i.flags[1] & Operand_Mem))
1212781b 6716 continue;
f5eb1d70
JB
6717 /* Fall through. */
6718
6719 case 3:
3ac21baa
JB
6720 if (!(size_match & MATCH_STRAIGHT))
6721 goto check_reverse;
64c49ab3
JB
6722 /* Reverse direction of operands if swapping is possible in the first
6723 place (operands need to be symmetric) and
6724 - the load form is requested, and the template is a store form,
6725 - the store form is requested, and the template is a load form,
6726 - the non-default (swapped) form is requested. */
6727 overlap1 = operand_type_and (operand_types[0], operand_types[1]);
f5eb1d70 6728 if (t->opcode_modifier.d && i.reg_operands == i.operands
64c49ab3
JB
6729 && !operand_type_all_zero (&overlap1))
6730 switch (i.dir_encoding)
6731 {
6732 case dir_encoding_load:
6733 if (operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 6734 || t->opcode_modifier.regmem)
64c49ab3
JB
6735 goto check_reverse;
6736 break;
6737
6738 case dir_encoding_store:
6739 if (!operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 6740 && !t->opcode_modifier.regmem)
64c49ab3
JB
6741 goto check_reverse;
6742 break;
6743
6744 case dir_encoding_swap:
6745 goto check_reverse;
6746
6747 case dir_encoding_default:
6748 break;
6749 }
86fa6981 6750 /* If we want store form, we skip the current load. */
64c49ab3
JB
6751 if ((i.dir_encoding == dir_encoding_store
6752 || i.dir_encoding == dir_encoding_swap)
86fa6981
L
6753 && i.mem_operands == 0
6754 && t->opcode_modifier.load)
fa99fab2 6755 continue;
1a0670f3 6756 /* Fall through. */
f48ff2ae 6757 case 4:
c0f3af97 6758 case 5:
c6fb90c8 6759 overlap1 = operand_type_and (i.types[1], operand_types[1]);
40fb9820
L
6760 if (!operand_type_match (overlap0, i.types[0])
6761 || !operand_type_match (overlap1, i.types[1])
e2195274 6762 || ((check_register & 3) == 3
dc821c5f 6763 && !operand_type_register_match (i.types[0],
40fb9820 6764 operand_types[0],
dc821c5f 6765 i.types[1],
40fb9820 6766 operand_types[1])))
29b0f896 6767 {
7b94647a
JB
6768 specific_error = progress (i.error);
6769
29b0f896 6770 /* Check if other direction is valid ... */
38e314eb 6771 if (!t->opcode_modifier.d)
29b0f896
AM
6772 continue;
6773
dc1e8a47 6774 check_reverse:
3ac21baa
JB
6775 if (!(size_match & MATCH_REVERSE))
6776 continue;
29b0f896 6777 /* Try reversing direction of operands. */
8bd915b7
JB
6778 j = t->opcode_modifier.vexsources ? 1 : i.operands - 1;
6779 overlap0 = operand_type_and (i.types[0], operand_types[j]);
6780 overlap1 = operand_type_and (i.types[j], operand_types[0]);
c975cec5
JB
6781 overlap2 = operand_type_and (i.types[1], operand_types[1]);
6782 gas_assert (t->operands != 3 || !check_register);
40fb9820 6783 if (!operand_type_match (overlap0, i.types[0])
8bd915b7 6784 || !operand_type_match (overlap1, i.types[j])
c975cec5
JB
6785 || (t->operands == 3
6786 && !operand_type_match (overlap2, i.types[1]))
45664ddb 6787 || (check_register
dc821c5f 6788 && !operand_type_register_match (i.types[0],
8bd915b7
JB
6789 operand_types[j],
6790 i.types[j],
45664ddb 6791 operand_types[0])))
29b0f896
AM
6792 {
6793 /* Does not match either direction. */
7b94647a 6794 specific_error = progress (i.error);
29b0f896
AM
6795 continue;
6796 }
38e314eb 6797 /* found_reverse_match holds which of D or FloatR
29b0f896 6798 we've found. */
38e314eb
JB
6799 if (!t->opcode_modifier.d)
6800 found_reverse_match = 0;
6801 else if (operand_types[0].bitfield.tbyte)
8a2ed489 6802 found_reverse_match = Opcode_FloatD;
8bd915b7
JB
6803 else if (t->opcode_modifier.vexsources)
6804 {
6805 found_reverse_match = Opcode_VexW;
6806 goto check_operands_345;
6807 }
2c735193
JB
6808 else if (t->opcode_modifier.opcodespace != SPACE_BASE
6809 && (t->opcode_modifier.opcodespace != SPACE_0F
6810 /* MOV to/from CR/DR/TR, as an exception, follow
6811 the base opcode space encoding model. */
6812 || (t->base_opcode | 7) != 0x27))
dbbc8b7e 6813 found_reverse_match = (t->base_opcode & 0xee) != 0x6e
2c735193 6814 ? Opcode_ExtD : Opcode_SIMD_IntD;
8a2ed489 6815 else
38e314eb 6816 found_reverse_match = Opcode_D;
40fb9820 6817 if (t->opcode_modifier.floatr)
8a2ed489 6818 found_reverse_match |= Opcode_FloatR;
29b0f896 6819 }
f48ff2ae 6820 else
29b0f896 6821 {
f48ff2ae 6822 /* Found a forward 2 operand match here. */
8bd915b7 6823 check_operands_345:
d1cbb4db
L
6824 switch (t->operands)
6825 {
c0f3af97 6826 case 5:
3d0738af 6827 overlap4 = operand_type_and (i.types[4], operand_types[4]);
c0f3af97 6828 if (!operand_type_match (overlap4, i.types[4])
dc821c5f 6829 || !operand_type_register_match (i.types[3],
c0f3af97 6830 operand_types[3],
c0f3af97
L
6831 i.types[4],
6832 operand_types[4]))
7b94647a
JB
6833 {
6834 specific_error = progress (i.error);
6835 continue;
6836 }
1a0670f3 6837 /* Fall through. */
f48ff2ae 6838 case 4:
3d0738af 6839 overlap3 = operand_type_and (i.types[3], operand_types[3]);
40fb9820 6840 if (!operand_type_match (overlap3, i.types[3])
e2195274
JB
6841 || ((check_register & 0xa) == 0xa
6842 && !operand_type_register_match (i.types[1],
f7768225
JB
6843 operand_types[1],
6844 i.types[3],
e2195274
JB
6845 operand_types[3]))
6846 || ((check_register & 0xc) == 0xc
6847 && !operand_type_register_match (i.types[2],
6848 operand_types[2],
6849 i.types[3],
6850 operand_types[3])))
7b94647a
JB
6851 {
6852 specific_error = progress (i.error);
6853 continue;
6854 }
1a0670f3 6855 /* Fall through. */
f48ff2ae 6856 case 3:
3d0738af 6857 overlap2 = operand_type_and (i.types[2], operand_types[2]);
40fb9820 6858 if (!operand_type_match (overlap2, i.types[2])
e2195274
JB
6859 || ((check_register & 5) == 5
6860 && !operand_type_register_match (i.types[0],
23e42951
JB
6861 operand_types[0],
6862 i.types[2],
e2195274
JB
6863 operand_types[2]))
6864 || ((check_register & 6) == 6
6865 && !operand_type_register_match (i.types[1],
6866 operand_types[1],
6867 i.types[2],
6868 operand_types[2])))
7b94647a
JB
6869 {
6870 specific_error = progress (i.error);
6871 continue;
6872 }
f48ff2ae
L
6873 break;
6874 }
29b0f896 6875 }
f48ff2ae 6876 /* Found either forward/reverse 2, 3 or 4 operand match here:
29b0f896
AM
6877 slip through to break. */
6878 }
c0f3af97 6879
9bb4d860
L
6880 /* Check if VEX/EVEX encoding requirements can be satisfied. */
6881 if (VEX_check_encoding (t))
da4977e0 6882 {
7b94647a 6883 specific_error = progress (i.error);
da4977e0
JB
6884 continue;
6885 }
6886
9bb4d860
L
6887 /* Check if vector operands are valid. */
6888 if (check_VecOperands (t))
5614d22c 6889 {
7b94647a 6890 specific_error = progress (i.error);
5614d22c
JB
6891 continue;
6892 }
a683cc34 6893
29b0f896
AM
6894 /* We've found a match; break out of loop. */
6895 break;
6896 }
6897
7b94647a
JB
6898#undef progress
6899
29b0f896
AM
6900 if (t == current_templates->end)
6901 {
6902 /* We found no match. */
a65babc9 6903 const char *err_msg;
7b94647a 6904 switch (specific_error)
a65babc9
L
6905 {
6906 default:
6907 abort ();
86e026a4 6908 case operand_size_mismatch:
a65babc9
L
6909 err_msg = _("operand size mismatch");
6910 break;
6911 case operand_type_mismatch:
6912 err_msg = _("operand type mismatch");
6913 break;
6914 case register_type_mismatch:
6915 err_msg = _("register type mismatch");
6916 break;
6917 case number_of_operands_mismatch:
6918 err_msg = _("number of operands mismatch");
6919 break;
6920 case invalid_instruction_suffix:
6921 err_msg = _("invalid instruction suffix");
6922 break;
6923 case bad_imm4:
4a2608e3 6924 err_msg = _("constant doesn't fit in 4 bits");
a65babc9 6925 break;
a65babc9
L
6926 case unsupported_with_intel_mnemonic:
6927 err_msg = _("unsupported with Intel mnemonic");
6928 break;
6929 case unsupported_syntax:
6930 err_msg = _("unsupported syntax");
6931 break;
6932 case unsupported:
35262a23 6933 as_bad (_("unsupported instruction `%s'"),
10efe3f6
L
6934 current_templates->start->name);
6935 return NULL;
260cd341
LC
6936 case invalid_sib_address:
6937 err_msg = _("invalid SIB address");
6938 break;
6c30d220
L
6939 case invalid_vsib_address:
6940 err_msg = _("invalid VSIB address");
6941 break;
7bab8ab5
JB
6942 case invalid_vector_register_set:
6943 err_msg = _("mask, index, and destination registers must be distinct");
6944 break;
260cd341
LC
6945 case invalid_tmm_register_set:
6946 err_msg = _("all tmm registers must be distinct");
6947 break;
0cc78721
CL
6948 case invalid_dest_and_src_register_set:
6949 err_msg = _("destination and source registers must be distinct");
6950 break;
6c30d220
L
6951 case unsupported_vector_index_register:
6952 err_msg = _("unsupported vector index register");
6953 break;
43234a1e
L
6954 case unsupported_broadcast:
6955 err_msg = _("unsupported broadcast");
6956 break;
43234a1e
L
6957 case broadcast_needed:
6958 err_msg = _("broadcast is needed for operand of such type");
6959 break;
6960 case unsupported_masking:
6961 err_msg = _("unsupported masking");
6962 break;
6963 case mask_not_on_destination:
6964 err_msg = _("mask not on destination operand");
6965 break;
6966 case no_default_mask:
6967 err_msg = _("default mask isn't allowed");
6968 break;
6969 case unsupported_rc_sae:
6970 err_msg = _("unsupported static rounding/sae");
6971 break;
43234a1e
L
6972 case invalid_register_operand:
6973 err_msg = _("invalid register operand");
6974 break;
a65babc9
L
6975 }
6976 as_bad (_("%s for `%s'"), err_msg,
891edac4 6977 current_templates->start->name);
fa99fab2 6978 return NULL;
29b0f896 6979 }
252b5132 6980
29b0f896
AM
6981 if (!quiet_warnings)
6982 {
6983 if (!intel_syntax
0cfa3eb3 6984 && (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE)))
6f2f06be 6985 as_warn (_("indirect %s without `*'"), t->name);
29b0f896 6986
40fb9820 6987 if (t->opcode_modifier.isprefix
3cd7f3e3 6988 && t->opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
6989 {
6990 /* Warn them that a data or address size prefix doesn't
6991 affect assembly of the next line of code. */
6992 as_warn (_("stand-alone `%s' prefix"), t->name);
6993 }
6994 }
6995
6996 /* Copy the template we found. */
9a182d04 6997 install_template (t);
539e75ad
L
6998
6999 if (addr_prefix_disp != -1)
7000 i.tm.operand_types[addr_prefix_disp]
7001 = operand_types[addr_prefix_disp];
7002
8bd915b7 7003 switch (found_reverse_match)
29b0f896 7004 {
8bd915b7
JB
7005 case 0:
7006 break;
7007
7008 default:
dfd69174
JB
7009 /* If we found a reverse match we must alter the opcode direction
7010 bit and clear/flip the regmem modifier one. found_reverse_match
7011 holds bits to change (different for int & float insns). */
29b0f896
AM
7012
7013 i.tm.base_opcode ^= found_reverse_match;
7014
f5eb1d70
JB
7015 i.tm.operand_types[0] = operand_types[i.operands - 1];
7016 i.tm.operand_types[i.operands - 1] = operand_types[0];
dfd69174
JB
7017
7018 /* Certain SIMD insns have their load forms specified in the opcode
7019 table, and hence we need to _set_ RegMem instead of clearing it.
7020 We need to avoid setting the bit though on insns like KMOVW. */
7021 i.tm.opcode_modifier.regmem
7022 = i.tm.opcode_modifier.modrm && i.tm.opcode_modifier.d
7023 && i.tm.operands > 2U - i.tm.opcode_modifier.sse2avx
7024 && !i.tm.opcode_modifier.regmem;
8bd915b7
JB
7025 break;
7026
7027 case Opcode_VexW:
7028 /* Only the first two register operands need reversing, alongside
7029 flipping VEX.W. */
7030 i.tm.opcode_modifier.vexw ^= VEXW0 ^ VEXW1;
7031
7032 j = i.tm.operand_types[0].bitfield.imm8;
7033 i.tm.operand_types[j] = operand_types[j + 1];
7034 i.tm.operand_types[j + 1] = operand_types[j];
7035 break;
29b0f896
AM
7036 }
7037
fa99fab2 7038 return t;
29b0f896
AM
7039}
7040
7041static int
e3bb37b5 7042check_string (void)
29b0f896 7043{
51c8edf6
JB
7044 unsigned int es_op = i.tm.opcode_modifier.isstring - IS_STRING_ES_OP0;
7045 unsigned int op = i.tm.operand_types[0].bitfield.baseindex ? es_op : 0;
8dc0818e 7046
5e042380 7047 if (i.seg[op] != NULL && i.seg[op] != reg_es)
29b0f896 7048 {
51c8edf6
JB
7049 as_bad (_("`%s' operand %u must use `%ses' segment"),
7050 i.tm.name,
7051 intel_syntax ? i.tm.operands - es_op : es_op + 1,
7052 register_prefix);
7053 return 0;
29b0f896 7054 }
51c8edf6
JB
7055
7056 /* There's only ever one segment override allowed per instruction.
7057 This instruction possibly has a legal segment override on the
7058 second operand, so copy the segment to where non-string
7059 instructions store it, allowing common code. */
7060 i.seg[op] = i.seg[1];
7061
29b0f896
AM
7062 return 1;
7063}
7064
7065static int
543613e9 7066process_suffix (void)
29b0f896 7067{
5b7c81bd 7068 bool is_crc32 = false, is_movx = false;
8b65b895 7069
29b0f896
AM
7070 /* If matched instruction specifies an explicit instruction mnemonic
7071 suffix, use it. */
673fe0f0 7072 if (i.tm.opcode_modifier.size == SIZE16)
40fb9820 7073 i.suffix = WORD_MNEM_SUFFIX;
673fe0f0 7074 else if (i.tm.opcode_modifier.size == SIZE32)
40fb9820 7075 i.suffix = LONG_MNEM_SUFFIX;
673fe0f0 7076 else if (i.tm.opcode_modifier.size == SIZE64)
40fb9820 7077 i.suffix = QWORD_MNEM_SUFFIX;
13e600d0 7078 else if (i.reg_operands
c8f8eebc 7079 && (i.operands > 1 || i.types[0].bitfield.class == Reg)
255571cd 7080 && i.tm.opcode_modifier.operandconstraint != ADDR_PREFIX_OP_REG)
29b0f896 7081 {
65fca059 7082 unsigned int numop = i.operands;
389d00a5
JB
7083
7084 /* MOVSX/MOVZX */
7085 is_movx = (i.tm.opcode_modifier.opcodespace == SPACE_0F
7086 && (i.tm.base_opcode | 8) == 0xbe)
7087 || (i.tm.opcode_modifier.opcodespace == SPACE_BASE
7088 && i.tm.base_opcode == 0x63
7089 && i.tm.cpu_flags.bitfield.cpu64);
7090
8b65b895 7091 /* CRC32 */
389d00a5
JB
7092 is_crc32 = (i.tm.base_opcode == 0xf0
7093 && i.tm.opcode_modifier.opcodespace == SPACE_0F38
8b65b895 7094 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2);
65fca059
JB
7095
7096 /* movsx/movzx want only their source operand considered here, for the
7097 ambiguity checking below. The suffix will be replaced afterwards
7098 to represent the destination (register). */
389d00a5 7099 if (is_movx && (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63))
65fca059
JB
7100 --i.operands;
7101
643bb870 7102 /* crc32 needs REX.W set regardless of suffix / source operand size. */
8b65b895 7103 if (is_crc32 && i.tm.operand_types[1].bitfield.qword)
643bb870
JB
7104 i.rex |= REX_W;
7105
29b0f896 7106 /* If there's no instruction mnemonic suffix we try to invent one
13e600d0 7107 based on GPR operands. */
29b0f896
AM
7108 if (!i.suffix)
7109 {
7110 /* We take i.suffix from the last register operand specified,
7111 Destination register type is more significant than source
381d071f
L
7112 register type. crc32 in SSE4.2 prefers source register
7113 type. */
8b65b895 7114 unsigned int op = is_crc32 ? 1 : i.operands;
20592a94 7115
1a035124
JB
7116 while (op--)
7117 if (i.tm.operand_types[op].bitfield.instance == InstanceNone
7118 || i.tm.operand_types[op].bitfield.instance == Accum)
7119 {
7120 if (i.types[op].bitfield.class != Reg)
7121 continue;
7122 if (i.types[op].bitfield.byte)
7123 i.suffix = BYTE_MNEM_SUFFIX;
7124 else if (i.types[op].bitfield.word)
7125 i.suffix = WORD_MNEM_SUFFIX;
7126 else if (i.types[op].bitfield.dword)
7127 i.suffix = LONG_MNEM_SUFFIX;
7128 else if (i.types[op].bitfield.qword)
7129 i.suffix = QWORD_MNEM_SUFFIX;
7130 else
7131 continue;
7132 break;
7133 }
65fca059
JB
7134
7135 /* As an exception, movsx/movzx silently default to a byte source
7136 in AT&T mode. */
389d00a5 7137 if (is_movx && i.tm.opcode_modifier.w && !i.suffix && !intel_syntax)
65fca059 7138 i.suffix = BYTE_MNEM_SUFFIX;
29b0f896
AM
7139 }
7140 else if (i.suffix == BYTE_MNEM_SUFFIX)
7141 {
1cb0ab18 7142 if (!check_byte_reg ())
29b0f896
AM
7143 return 0;
7144 }
7145 else if (i.suffix == LONG_MNEM_SUFFIX)
7146 {
1cb0ab18 7147 if (!check_long_reg ())
29b0f896
AM
7148 return 0;
7149 }
7150 else if (i.suffix == QWORD_MNEM_SUFFIX)
7151 {
1cb0ab18 7152 if (!check_qword_reg ())
29b0f896
AM
7153 return 0;
7154 }
7155 else if (i.suffix == WORD_MNEM_SUFFIX)
7156 {
1cb0ab18 7157 if (!check_word_reg ())
29b0f896
AM
7158 return 0;
7159 }
3cd7f3e3
L
7160 else if (intel_syntax
7161 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
7162 /* Do nothing if the instruction is going to ignore the prefix. */
7163 ;
7164 else
7165 abort ();
65fca059
JB
7166
7167 /* Undo the movsx/movzx change done above. */
7168 i.operands = numop;
29b0f896 7169 }
3cd7f3e3
L
7170 else if (i.tm.opcode_modifier.mnemonicsize == DEFAULTSIZE
7171 && !i.suffix)
29b0f896 7172 {
13e600d0
JB
7173 i.suffix = stackop_size;
7174 if (stackop_size == LONG_MNEM_SUFFIX)
06f74c5c
L
7175 {
7176 /* stackop_size is set to LONG_MNEM_SUFFIX for the
7177 .code16gcc directive to support 16-bit mode with
7178 32-bit address. For IRET without a suffix, generate
7179 16-bit IRET (opcode 0xcf) to return from an interrupt
7180 handler. */
13e600d0
JB
7181 if (i.tm.base_opcode == 0xcf)
7182 {
7183 i.suffix = WORD_MNEM_SUFFIX;
7184 as_warn (_("generating 16-bit `iret' for .code16gcc directive"));
7185 }
7186 /* Warn about changed behavior for segment register push/pop. */
7187 else if ((i.tm.base_opcode | 1) == 0x07)
7188 as_warn (_("generating 32-bit `%s', unlike earlier gas versions"),
7189 i.tm.name);
06f74c5c 7190 }
29b0f896 7191 }
c006a730 7192 else if (!i.suffix
0cfa3eb3
JB
7193 && (i.tm.opcode_modifier.jump == JUMP_ABSOLUTE
7194 || i.tm.opcode_modifier.jump == JUMP_BYTE
7195 || i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT
389d00a5
JB
7196 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
7197 && i.tm.base_opcode == 0x01 /* [ls][gi]dt */
64e74474 7198 && i.tm.extension_opcode <= 3)))
9306ca4a
JB
7199 {
7200 switch (flag_code)
7201 {
7202 case CODE_64BIT:
40fb9820 7203 if (!i.tm.opcode_modifier.no_qsuf)
9306ca4a 7204 {
828c2a25
JB
7205 if (i.tm.opcode_modifier.jump == JUMP_BYTE
7206 || i.tm.opcode_modifier.no_lsuf)
7207 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a
JB
7208 break;
7209 }
1a0670f3 7210 /* Fall through. */
9306ca4a 7211 case CODE_32BIT:
40fb9820 7212 if (!i.tm.opcode_modifier.no_lsuf)
9306ca4a
JB
7213 i.suffix = LONG_MNEM_SUFFIX;
7214 break;
7215 case CODE_16BIT:
40fb9820 7216 if (!i.tm.opcode_modifier.no_wsuf)
9306ca4a
JB
7217 i.suffix = WORD_MNEM_SUFFIX;
7218 break;
7219 }
7220 }
252b5132 7221
c006a730 7222 if (!i.suffix
3cd7f3e3 7223 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8
JB
7224 /* Also cover lret/retf/iret in 64-bit mode. */
7225 || (flag_code == CODE_64BIT
7226 && !i.tm.opcode_modifier.no_lsuf
7227 && !i.tm.opcode_modifier.no_qsuf))
3cd7f3e3 7228 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
8bbb3ad8
JB
7229 /* Explicit sizing prefixes are assumed to disambiguate insns. */
7230 && !i.prefix[DATA_PREFIX] && !(i.prefix[REX_PREFIX] & REX_W)
62b3f548
JB
7231 /* Accept FLDENV et al without suffix. */
7232 && (i.tm.opcode_modifier.no_ssuf || i.tm.opcode_modifier.floatmf))
29b0f896 7233 {
6c0946d0 7234 unsigned int suffixes, evex = 0;
c006a730
JB
7235
7236 suffixes = !i.tm.opcode_modifier.no_bsuf;
7237 if (!i.tm.opcode_modifier.no_wsuf)
7238 suffixes |= 1 << 1;
7239 if (!i.tm.opcode_modifier.no_lsuf)
7240 suffixes |= 1 << 2;
7241 if (!i.tm.opcode_modifier.no_ldsuf)
7242 suffixes |= 1 << 3;
7243 if (!i.tm.opcode_modifier.no_ssuf)
7244 suffixes |= 1 << 4;
7245 if (flag_code == CODE_64BIT && !i.tm.opcode_modifier.no_qsuf)
7246 suffixes |= 1 << 5;
7247
6c0946d0
JB
7248 /* For [XYZ]MMWORD operands inspect operand sizes. While generally
7249 also suitable for AT&T syntax mode, it was requested that this be
7250 restricted to just Intel syntax. */
a5748e0d
JB
7251 if (intel_syntax && is_any_vex_encoding (&i.tm)
7252 && !i.broadcast.type && !i.broadcast.bytes)
6c0946d0 7253 {
b9915cbc 7254 unsigned int op;
6c0946d0 7255
b9915cbc 7256 for (op = 0; op < i.tm.operands; ++op)
6c0946d0 7257 {
b9915cbc
JB
7258 if (is_evex_encoding (&i.tm)
7259 && !cpu_arch_flags.bitfield.cpuavx512vl)
6c0946d0 7260 {
b9915cbc
JB
7261 if (i.tm.operand_types[op].bitfield.ymmword)
7262 i.tm.operand_types[op].bitfield.xmmword = 0;
7263 if (i.tm.operand_types[op].bitfield.zmmword)
7264 i.tm.operand_types[op].bitfield.ymmword = 0;
7265 if (!i.tm.opcode_modifier.evex
7266 || i.tm.opcode_modifier.evex == EVEXDYN)
7267 i.tm.opcode_modifier.evex = EVEX512;
7268 }
6c0946d0 7269
b9915cbc
JB
7270 if (i.tm.operand_types[op].bitfield.xmmword
7271 + i.tm.operand_types[op].bitfield.ymmword
7272 + i.tm.operand_types[op].bitfield.zmmword < 2)
7273 continue;
6c0946d0 7274
b9915cbc
JB
7275 /* Any properly sized operand disambiguates the insn. */
7276 if (i.types[op].bitfield.xmmword
7277 || i.types[op].bitfield.ymmword
7278 || i.types[op].bitfield.zmmword)
7279 {
7280 suffixes &= ~(7 << 6);
7281 evex = 0;
7282 break;
7283 }
6c0946d0 7284
b9915cbc
JB
7285 if ((i.flags[op] & Operand_Mem)
7286 && i.tm.operand_types[op].bitfield.unspecified)
7287 {
7288 if (i.tm.operand_types[op].bitfield.xmmword)
7289 suffixes |= 1 << 6;
7290 if (i.tm.operand_types[op].bitfield.ymmword)
7291 suffixes |= 1 << 7;
7292 if (i.tm.operand_types[op].bitfield.zmmword)
7293 suffixes |= 1 << 8;
7294 if (is_evex_encoding (&i.tm))
7295 evex = EVEX512;
6c0946d0
JB
7296 }
7297 }
7298 }
7299
7300 /* Are multiple suffixes / operand sizes allowed? */
c006a730 7301 if (suffixes & (suffixes - 1))
9306ca4a 7302 {
873494c8 7303 if (intel_syntax
3cd7f3e3 7304 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8 7305 || operand_check == check_error))
9306ca4a 7306 {
c006a730 7307 as_bad (_("ambiguous operand size for `%s'"), i.tm.name);
9306ca4a
JB
7308 return 0;
7309 }
c006a730 7310 if (operand_check == check_error)
9306ca4a 7311 {
c006a730
JB
7312 as_bad (_("no instruction mnemonic suffix given and "
7313 "no register operands; can't size `%s'"), i.tm.name);
9306ca4a
JB
7314 return 0;
7315 }
c006a730 7316 if (operand_check == check_warning)
873494c8
JB
7317 as_warn (_("%s; using default for `%s'"),
7318 intel_syntax
7319 ? _("ambiguous operand size")
7320 : _("no instruction mnemonic suffix given and "
7321 "no register operands"),
7322 i.tm.name);
c006a730
JB
7323
7324 if (i.tm.opcode_modifier.floatmf)
7325 i.suffix = SHORT_MNEM_SUFFIX;
389d00a5 7326 else if (is_movx)
65fca059 7327 /* handled below */;
6c0946d0
JB
7328 else if (evex)
7329 i.tm.opcode_modifier.evex = evex;
c006a730
JB
7330 else if (flag_code == CODE_16BIT)
7331 i.suffix = WORD_MNEM_SUFFIX;
1a035124 7332 else if (!i.tm.opcode_modifier.no_lsuf)
c006a730 7333 i.suffix = LONG_MNEM_SUFFIX;
1a035124
JB
7334 else
7335 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a 7336 }
29b0f896 7337 }
252b5132 7338
389d00a5 7339 if (is_movx)
65fca059
JB
7340 {
7341 /* In Intel syntax, movsx/movzx must have a "suffix" (checked above).
7342 In AT&T syntax, if there is no suffix (warned about above), the default
7343 will be byte extension. */
7344 if (i.tm.opcode_modifier.w && i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
7345 i.tm.base_opcode |= 1;
7346
7347 /* For further processing, the suffix should represent the destination
7348 (register). This is already the case when one was used with
7349 mov[sz][bw]*, but we need to replace it for mov[sz]x, or if there was
7350 no suffix to begin with. */
7351 if (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63 || !i.suffix)
7352 {
7353 if (i.types[1].bitfield.word)
7354 i.suffix = WORD_MNEM_SUFFIX;
7355 else if (i.types[1].bitfield.qword)
7356 i.suffix = QWORD_MNEM_SUFFIX;
7357 else
7358 i.suffix = LONG_MNEM_SUFFIX;
7359
7360 i.tm.opcode_modifier.w = 0;
7361 }
7362 }
7363
50128d0c
JB
7364 if (!i.tm.opcode_modifier.modrm && i.reg_operands && i.tm.operands < 3)
7365 i.short_form = (i.tm.operand_types[0].bitfield.class == Reg)
7366 != (i.tm.operand_types[1].bitfield.class == Reg);
7367
d2224064
JB
7368 /* Change the opcode based on the operand size given by i.suffix. */
7369 switch (i.suffix)
29b0f896 7370 {
d2224064
JB
7371 /* Size floating point instruction. */
7372 case LONG_MNEM_SUFFIX:
7373 if (i.tm.opcode_modifier.floatmf)
7374 {
7375 i.tm.base_opcode ^= 4;
7376 break;
7377 }
7378 /* fall through */
7379 case WORD_MNEM_SUFFIX:
7380 case QWORD_MNEM_SUFFIX:
29b0f896 7381 /* It's not a byte, select word/dword operation. */
40fb9820 7382 if (i.tm.opcode_modifier.w)
29b0f896 7383 {
50128d0c 7384 if (i.short_form)
29b0f896
AM
7385 i.tm.base_opcode |= 8;
7386 else
7387 i.tm.base_opcode |= 1;
7388 }
d2224064
JB
7389 /* fall through */
7390 case SHORT_MNEM_SUFFIX:
29b0f896
AM
7391 /* Now select between word & dword operations via the operand
7392 size prefix, except for instructions that will ignore this
7393 prefix anyway. */
c8f8eebc 7394 if (i.suffix != QWORD_MNEM_SUFFIX
3cd7f3e3 7395 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
c8f8eebc
JB
7396 && !i.tm.opcode_modifier.floatmf
7397 && !is_any_vex_encoding (&i.tm)
7398 && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
7399 || (flag_code == CODE_64BIT
7400 && i.tm.opcode_modifier.jump == JUMP_BYTE)))
24eab124
AM
7401 {
7402 unsigned int prefix = DATA_PREFIX_OPCODE;
543613e9 7403
0cfa3eb3 7404 if (i.tm.opcode_modifier.jump == JUMP_BYTE) /* jcxz, loop */
29b0f896 7405 prefix = ADDR_PREFIX_OPCODE;
252b5132 7406
29b0f896
AM
7407 if (!add_prefix (prefix))
7408 return 0;
24eab124 7409 }
252b5132 7410
29b0f896
AM
7411 /* Set mode64 for an operand. */
7412 if (i.suffix == QWORD_MNEM_SUFFIX
9146926a 7413 && flag_code == CODE_64BIT
d2224064 7414 && !i.tm.opcode_modifier.norex64
4ed21b58 7415 && !i.tm.opcode_modifier.vexw
46e883c5 7416 /* Special case for xchg %rax,%rax. It is NOP and doesn't
d2224064
JB
7417 need rex64. */
7418 && ! (i.operands == 2
7419 && i.tm.base_opcode == 0x90
7420 && i.tm.extension_opcode == None
75e5731b
JB
7421 && i.types[0].bitfield.instance == Accum
7422 && i.types[0].bitfield.qword
7423 && i.types[1].bitfield.instance == Accum
7424 && i.types[1].bitfield.qword))
d2224064 7425 i.rex |= REX_W;
3e73aa7c 7426
d2224064 7427 break;
8bbb3ad8
JB
7428
7429 case 0:
f9a6a8f0 7430 /* Select word/dword/qword operation with explicit data sizing prefix
8bbb3ad8
JB
7431 when there are no suitable register operands. */
7432 if (i.tm.opcode_modifier.w
7433 && (i.prefix[DATA_PREFIX] || (i.prefix[REX_PREFIX] & REX_W))
7434 && (!i.reg_operands
7435 || (i.reg_operands == 1
7436 /* ShiftCount */
7437 && (i.tm.operand_types[0].bitfield.instance == RegC
7438 /* InOutPortReg */
7439 || i.tm.operand_types[0].bitfield.instance == RegD
7440 || i.tm.operand_types[1].bitfield.instance == RegD
7441 /* CRC32 */
8b65b895 7442 || is_crc32))))
8bbb3ad8
JB
7443 i.tm.base_opcode |= 1;
7444 break;
29b0f896 7445 }
7ecd2f8b 7446
255571cd 7447 if (i.tm.opcode_modifier.operandconstraint == ADDR_PREFIX_OP_REG)
c0a30a9f 7448 {
c8f8eebc
JB
7449 gas_assert (!i.suffix);
7450 gas_assert (i.reg_operands);
c0a30a9f 7451
c8f8eebc
JB
7452 if (i.tm.operand_types[0].bitfield.instance == Accum
7453 || i.operands == 1)
7454 {
7455 /* The address size override prefix changes the size of the
7456 first operand. */
7457 if (flag_code == CODE_64BIT
7458 && i.op[0].regs->reg_type.bitfield.word)
7459 {
7460 as_bad (_("16-bit addressing unavailable for `%s'"),
7461 i.tm.name);
7462 return 0;
7463 }
7464
7465 if ((flag_code == CODE_32BIT
7466 ? i.op[0].regs->reg_type.bitfield.word
7467 : i.op[0].regs->reg_type.bitfield.dword)
7468 && !add_prefix (ADDR_PREFIX_OPCODE))
7469 return 0;
7470 }
c0a30a9f
L
7471 else
7472 {
c8f8eebc
JB
7473 /* Check invalid register operand when the address size override
7474 prefix changes the size of register operands. */
7475 unsigned int op;
7476 enum { need_word, need_dword, need_qword } need;
7477
27f13469 7478 /* Check the register operand for the address size prefix if
b3a3496f 7479 the memory operand has no real registers, like symbol, DISP
829f3fe1 7480 or bogus (x32-only) symbol(%rip) when symbol(%eip) is meant. */
27f13469
L
7481 if (i.mem_operands == 1
7482 && i.reg_operands == 1
7483 && i.operands == 2
27f13469 7484 && i.types[1].bitfield.class == Reg
b3a3496f
L
7485 && (flag_code == CODE_32BIT
7486 ? i.op[1].regs->reg_type.bitfield.word
7487 : i.op[1].regs->reg_type.bitfield.dword)
7488 && ((i.base_reg == NULL && i.index_reg == NULL)
829f3fe1
JB
7489#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
7490 || (x86_elf_abi == X86_64_X32_ABI
7491 && i.base_reg
b3a3496f
L
7492 && i.base_reg->reg_num == RegIP
7493 && i.base_reg->reg_type.bitfield.qword))
829f3fe1
JB
7494#else
7495 || 0)
7496#endif
27f13469
L
7497 && !add_prefix (ADDR_PREFIX_OPCODE))
7498 return 0;
7499
c8f8eebc
JB
7500 if (flag_code == CODE_32BIT)
7501 need = i.prefix[ADDR_PREFIX] ? need_word : need_dword;
7502 else if (i.prefix[ADDR_PREFIX])
c0a30a9f
L
7503 need = need_dword;
7504 else
7505 need = flag_code == CODE_64BIT ? need_qword : need_word;
c0a30a9f 7506
c8f8eebc
JB
7507 for (op = 0; op < i.operands; op++)
7508 {
7509 if (i.types[op].bitfield.class != Reg)
7510 continue;
7511
7512 switch (need)
7513 {
7514 case need_word:
7515 if (i.op[op].regs->reg_type.bitfield.word)
7516 continue;
7517 break;
7518 case need_dword:
7519 if (i.op[op].regs->reg_type.bitfield.dword)
7520 continue;
7521 break;
7522 case need_qword:
7523 if (i.op[op].regs->reg_type.bitfield.qword)
7524 continue;
7525 break;
7526 }
7527
7528 as_bad (_("invalid register operand size for `%s'"),
7529 i.tm.name);
7530 return 0;
7531 }
7532 }
c0a30a9f
L
7533 }
7534
29b0f896
AM
7535 return 1;
7536}
3e73aa7c 7537
29b0f896 7538static int
543613e9 7539check_byte_reg (void)
29b0f896
AM
7540{
7541 int op;
543613e9 7542
29b0f896
AM
7543 for (op = i.operands; --op >= 0;)
7544 {
dc821c5f 7545 /* Skip non-register operands. */
bab6aec1 7546 if (i.types[op].bitfield.class != Reg)
dc821c5f
JB
7547 continue;
7548
29b0f896
AM
7549 /* If this is an eight bit register, it's OK. If it's the 16 or
7550 32 bit version of an eight bit register, we will just use the
7551 low portion, and that's OK too. */
dc821c5f 7552 if (i.types[op].bitfield.byte)
29b0f896
AM
7553 continue;
7554
5a819eb9 7555 /* I/O port address operands are OK too. */
75e5731b
JB
7556 if (i.tm.operand_types[op].bitfield.instance == RegD
7557 && i.tm.operand_types[op].bitfield.word)
5a819eb9
JB
7558 continue;
7559
9706160a 7560 /* crc32 only wants its source operand checked here. */
389d00a5
JB
7561 if (i.tm.base_opcode == 0xf0
7562 && i.tm.opcode_modifier.opcodespace == SPACE_0F38
8b65b895
L
7563 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2
7564 && op != 0)
9344ff29
L
7565 continue;
7566
29b0f896 7567 /* Any other register is bad. */
73c76375
JB
7568 as_bad (_("`%s%s' not allowed with `%s%c'"),
7569 register_prefix, i.op[op].regs->reg_name,
7570 i.tm.name, i.suffix);
7571 return 0;
29b0f896
AM
7572 }
7573 return 1;
7574}
7575
7576static int
e3bb37b5 7577check_long_reg (void)
29b0f896
AM
7578{
7579 int op;
7580
7581 for (op = i.operands; --op >= 0;)
dc821c5f 7582 /* Skip non-register operands. */
bab6aec1 7583 if (i.types[op].bitfield.class != Reg)
dc821c5f 7584 continue;
29b0f896
AM
7585 /* Reject eight bit registers, except where the template requires
7586 them. (eg. movzb) */
dc821c5f 7587 else if (i.types[op].bitfield.byte
bab6aec1 7588 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7589 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7590 && (i.tm.operand_types[op].bitfield.word
7591 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7592 {
a540244d
L
7593 as_bad (_("`%s%s' not allowed with `%s%c'"),
7594 register_prefix,
29b0f896
AM
7595 i.op[op].regs->reg_name,
7596 i.tm.name,
7597 i.suffix);
7598 return 0;
7599 }
be4c5e58
L
7600 /* Error if the e prefix on a general reg is missing. */
7601 else if (i.types[op].bitfield.word
bab6aec1 7602 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7603 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7604 && i.tm.operand_types[op].bitfield.dword)
29b0f896 7605 {
be4c5e58
L
7606 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7607 register_prefix, i.op[op].regs->reg_name,
7608 i.suffix);
7609 return 0;
252b5132 7610 }
e4630f71 7611 /* Warn if the r prefix on a general reg is present. */
dc821c5f 7612 else if (i.types[op].bitfield.qword
bab6aec1 7613 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7614 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7615 && i.tm.operand_types[op].bitfield.dword)
252b5132 7616 {
1cb0ab18
JB
7617 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7618 register_prefix, i.op[op].regs->reg_name, i.suffix);
7619 return 0;
29b0f896
AM
7620 }
7621 return 1;
7622}
252b5132 7623
29b0f896 7624static int
e3bb37b5 7625check_qword_reg (void)
29b0f896
AM
7626{
7627 int op;
252b5132 7628
29b0f896 7629 for (op = i.operands; --op >= 0; )
dc821c5f 7630 /* Skip non-register operands. */
bab6aec1 7631 if (i.types[op].bitfield.class != Reg)
dc821c5f 7632 continue;
29b0f896
AM
7633 /* Reject eight bit registers, except where the template requires
7634 them. (eg. movzb) */
dc821c5f 7635 else if (i.types[op].bitfield.byte
bab6aec1 7636 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7637 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7638 && (i.tm.operand_types[op].bitfield.word
7639 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7640 {
a540244d
L
7641 as_bad (_("`%s%s' not allowed with `%s%c'"),
7642 register_prefix,
29b0f896
AM
7643 i.op[op].regs->reg_name,
7644 i.tm.name,
7645 i.suffix);
7646 return 0;
7647 }
e4630f71 7648 /* Warn if the r prefix on a general reg is missing. */
dc821c5f
JB
7649 else if ((i.types[op].bitfield.word
7650 || i.types[op].bitfield.dword)
bab6aec1 7651 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7652 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7653 && i.tm.operand_types[op].bitfield.qword)
29b0f896
AM
7654 {
7655 /* Prohibit these changes in the 64bit mode, since the
7656 lowering is more complicated. */
1cb0ab18
JB
7657 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7658 register_prefix, i.op[op].regs->reg_name, i.suffix);
7659 return 0;
252b5132 7660 }
29b0f896
AM
7661 return 1;
7662}
252b5132 7663
29b0f896 7664static int
e3bb37b5 7665check_word_reg (void)
29b0f896
AM
7666{
7667 int op;
7668 for (op = i.operands; --op >= 0;)
dc821c5f 7669 /* Skip non-register operands. */
bab6aec1 7670 if (i.types[op].bitfield.class != Reg)
dc821c5f 7671 continue;
29b0f896
AM
7672 /* Reject eight bit registers, except where the template requires
7673 them. (eg. movzb) */
dc821c5f 7674 else if (i.types[op].bitfield.byte
bab6aec1 7675 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7676 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
7677 && (i.tm.operand_types[op].bitfield.word
7678 || i.tm.operand_types[op].bitfield.dword))
29b0f896 7679 {
a540244d
L
7680 as_bad (_("`%s%s' not allowed with `%s%c'"),
7681 register_prefix,
29b0f896
AM
7682 i.op[op].regs->reg_name,
7683 i.tm.name,
7684 i.suffix);
7685 return 0;
7686 }
9706160a
JB
7687 /* Error if the e or r prefix on a general reg is present. */
7688 else if ((i.types[op].bitfield.dword
dc821c5f 7689 || i.types[op].bitfield.qword)
bab6aec1 7690 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 7691 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 7692 && i.tm.operand_types[op].bitfield.word)
252b5132 7693 {
9706160a
JB
7694 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7695 register_prefix, i.op[op].regs->reg_name,
7696 i.suffix);
7697 return 0;
29b0f896
AM
7698 }
7699 return 1;
7700}
252b5132 7701
29b0f896 7702static int
40fb9820 7703update_imm (unsigned int j)
29b0f896 7704{
bc0844ae 7705 i386_operand_type overlap = i.types[j];
be1643ff
JB
7706 if (overlap.bitfield.imm8
7707 + overlap.bitfield.imm8s
7708 + overlap.bitfield.imm16
7709 + overlap.bitfield.imm32
7710 + overlap.bitfield.imm32s
7711 + overlap.bitfield.imm64 > 1)
29b0f896
AM
7712 {
7713 if (i.suffix)
7714 {
40fb9820
L
7715 i386_operand_type temp;
7716
0dfbf9d7 7717 operand_type_set (&temp, 0);
7ab9ffdd 7718 if (i.suffix == BYTE_MNEM_SUFFIX)
40fb9820
L
7719 {
7720 temp.bitfield.imm8 = overlap.bitfield.imm8;
7721 temp.bitfield.imm8s = overlap.bitfield.imm8s;
7722 }
7723 else if (i.suffix == WORD_MNEM_SUFFIX)
7724 temp.bitfield.imm16 = overlap.bitfield.imm16;
7725 else if (i.suffix == QWORD_MNEM_SUFFIX)
7726 {
7727 temp.bitfield.imm64 = overlap.bitfield.imm64;
7728 temp.bitfield.imm32s = overlap.bitfield.imm32s;
7729 }
7730 else
7731 temp.bitfield.imm32 = overlap.bitfield.imm32;
7732 overlap = temp;
29b0f896 7733 }
0dfbf9d7
L
7734 else if (operand_type_equal (&overlap, &imm16_32_32s)
7735 || operand_type_equal (&overlap, &imm16_32)
7736 || operand_type_equal (&overlap, &imm16_32s))
29b0f896 7737 {
40fb9820 7738 if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
65da13b5 7739 overlap = imm16;
40fb9820 7740 else
65da13b5 7741 overlap = imm32s;
29b0f896 7742 }
8bbb3ad8
JB
7743 else if (i.prefix[REX_PREFIX] & REX_W)
7744 overlap = operand_type_and (overlap, imm32s);
7745 else if (i.prefix[DATA_PREFIX])
7746 overlap = operand_type_and (overlap,
7747 flag_code != CODE_16BIT ? imm16 : imm32);
be1643ff
JB
7748 if (overlap.bitfield.imm8
7749 + overlap.bitfield.imm8s
7750 + overlap.bitfield.imm16
7751 + overlap.bitfield.imm32
7752 + overlap.bitfield.imm32s
7753 + overlap.bitfield.imm64 != 1)
29b0f896 7754 {
4eed87de
AM
7755 as_bad (_("no instruction mnemonic suffix given; "
7756 "can't determine immediate size"));
29b0f896
AM
7757 return 0;
7758 }
7759 }
40fb9820 7760 i.types[j] = overlap;
29b0f896 7761
40fb9820
L
7762 return 1;
7763}
7764
7765static int
7766finalize_imm (void)
7767{
bc0844ae 7768 unsigned int j, n;
29b0f896 7769
bc0844ae
L
7770 /* Update the first 2 immediate operands. */
7771 n = i.operands > 2 ? 2 : i.operands;
7772 if (n)
7773 {
7774 for (j = 0; j < n; j++)
7775 if (update_imm (j) == 0)
7776 return 0;
40fb9820 7777
bc0844ae
L
7778 /* The 3rd operand can't be immediate operand. */
7779 gas_assert (operand_type_check (i.types[2], imm) == 0);
7780 }
29b0f896
AM
7781
7782 return 1;
7783}
7784
7785static int
e3bb37b5 7786process_operands (void)
29b0f896
AM
7787{
7788 /* Default segment register this instruction will use for memory
7789 accesses. 0 means unknown. This is only for optimizing out
7790 unnecessary segment overrides. */
5e042380 7791 const reg_entry *default_seg = NULL;
29b0f896 7792
a5aeccd9
JB
7793 if (i.tm.opcode_modifier.sse2avx)
7794 {
7795 /* Legacy encoded insns allow explicit REX prefixes, so these prefixes
7796 need converting. */
7797 i.rex |= i.prefix[REX_PREFIX] & (REX_W | REX_R | REX_X | REX_B);
7798 i.prefix[REX_PREFIX] = 0;
7799 i.rex_encoding = 0;
7800 }
c423d21a
JB
7801 /* ImmExt should be processed after SSE2AVX. */
7802 else if (i.tm.opcode_modifier.immext)
7803 process_immext ();
a5aeccd9 7804
2426c15f 7805 if (i.tm.opcode_modifier.sse2avx && i.tm.opcode_modifier.vexvvvv)
29b0f896 7806 {
91d6fa6a
NC
7807 unsigned int dupl = i.operands;
7808 unsigned int dest = dupl - 1;
9fcfb3d7
L
7809 unsigned int j;
7810
c0f3af97 7811 /* The destination must be an xmm register. */
9c2799c2 7812 gas_assert (i.reg_operands
91d6fa6a 7813 && MAX_OPERANDS > dupl
7ab9ffdd 7814 && operand_type_equal (&i.types[dest], &regxmm));
c0f3af97 7815
75e5731b 7816 if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 7817 && i.tm.operand_types[0].bitfield.xmmword)
e2ec9d29 7818 {
8cd7925b 7819 if (i.tm.opcode_modifier.vexsources == VEX3SOURCES)
c0f3af97
L
7820 {
7821 /* Keep xmm0 for instructions with VEX prefix and 3
7822 sources. */
75e5731b 7823 i.tm.operand_types[0].bitfield.instance = InstanceNone;
3528c362 7824 i.tm.operand_types[0].bitfield.class = RegSIMD;
c0f3af97
L
7825 goto duplicate;
7826 }
e2ec9d29 7827 else
c0f3af97
L
7828 {
7829 /* We remove the first xmm0 and keep the number of
7830 operands unchanged, which in fact duplicates the
7831 destination. */
7832 for (j = 1; j < i.operands; j++)
7833 {
7834 i.op[j - 1] = i.op[j];
7835 i.types[j - 1] = i.types[j];
7836 i.tm.operand_types[j - 1] = i.tm.operand_types[j];
8dc0818e 7837 i.flags[j - 1] = i.flags[j];
c0f3af97
L
7838 }
7839 }
7840 }
255571cd 7841 else if (i.tm.opcode_modifier.operandconstraint == IMPLICIT_1ST_XMM0)
7ab9ffdd 7842 {
91d6fa6a 7843 gas_assert ((MAX_OPERANDS - 1) > dupl
8cd7925b
L
7844 && (i.tm.opcode_modifier.vexsources
7845 == VEX3SOURCES));
c0f3af97
L
7846
7847 /* Add the implicit xmm0 for instructions with VEX prefix
7848 and 3 sources. */
7849 for (j = i.operands; j > 0; j--)
7850 {
7851 i.op[j] = i.op[j - 1];
7852 i.types[j] = i.types[j - 1];
7853 i.tm.operand_types[j] = i.tm.operand_types[j - 1];
8dc0818e 7854 i.flags[j] = i.flags[j - 1];
c0f3af97
L
7855 }
7856 i.op[0].regs
629310ab 7857 = (const reg_entry *) str_hash_find (reg_hash, "xmm0");
7ab9ffdd 7858 i.types[0] = regxmm;
c0f3af97
L
7859 i.tm.operand_types[0] = regxmm;
7860
7861 i.operands += 2;
7862 i.reg_operands += 2;
7863 i.tm.operands += 2;
7864
91d6fa6a 7865 dupl++;
c0f3af97 7866 dest++;
91d6fa6a
NC
7867 i.op[dupl] = i.op[dest];
7868 i.types[dupl] = i.types[dest];
7869 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 7870 i.flags[dupl] = i.flags[dest];
e2ec9d29 7871 }
c0f3af97
L
7872 else
7873 {
dc1e8a47 7874 duplicate:
c0f3af97
L
7875 i.operands++;
7876 i.reg_operands++;
7877 i.tm.operands++;
7878
91d6fa6a
NC
7879 i.op[dupl] = i.op[dest];
7880 i.types[dupl] = i.types[dest];
7881 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 7882 i.flags[dupl] = i.flags[dest];
c0f3af97
L
7883 }
7884
7885 if (i.tm.opcode_modifier.immext)
7886 process_immext ();
7887 }
75e5731b 7888 else if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 7889 && i.tm.operand_types[0].bitfield.xmmword)
c0f3af97
L
7890 {
7891 unsigned int j;
7892
9fcfb3d7
L
7893 for (j = 1; j < i.operands; j++)
7894 {
7895 i.op[j - 1] = i.op[j];
7896 i.types[j - 1] = i.types[j];
7897
7898 /* We need to adjust fields in i.tm since they are used by
7899 build_modrm_byte. */
7900 i.tm.operand_types [j - 1] = i.tm.operand_types [j];
8dc0818e
JB
7901
7902 i.flags[j - 1] = i.flags[j];
9fcfb3d7
L
7903 }
7904
e2ec9d29
L
7905 i.operands--;
7906 i.reg_operands--;
e2ec9d29
L
7907 i.tm.operands--;
7908 }
255571cd 7909 else if (i.tm.opcode_modifier.operandconstraint == IMPLICIT_QUAD_GROUP)
920d2ddc 7910 {
a477a8c4
JB
7911 unsigned int regnum, first_reg_in_group, last_reg_in_group;
7912
920d2ddc 7913 /* The second operand must be {x,y,z}mmN, where N is a multiple of 4. */
3528c362 7914 gas_assert (i.operands >= 2 && i.types[1].bitfield.class == RegSIMD);
a477a8c4
JB
7915 regnum = register_number (i.op[1].regs);
7916 first_reg_in_group = regnum & ~3;
7917 last_reg_in_group = first_reg_in_group + 3;
7918 if (regnum != first_reg_in_group)
7919 as_warn (_("source register `%s%s' implicitly denotes"
7920 " `%s%.3s%u' to `%s%.3s%u' source group in `%s'"),
7921 register_prefix, i.op[1].regs->reg_name,
7922 register_prefix, i.op[1].regs->reg_name, first_reg_in_group,
7923 register_prefix, i.op[1].regs->reg_name, last_reg_in_group,
7924 i.tm.name);
7925 }
255571cd 7926 else if (i.tm.opcode_modifier.operandconstraint == REG_KLUDGE)
e2ec9d29
L
7927 {
7928 /* The imul $imm, %reg instruction is converted into
7929 imul $imm, %reg, %reg, and the clr %reg instruction
7930 is converted into xor %reg, %reg. */
7931
7932 unsigned int first_reg_op;
7933
7934 if (operand_type_check (i.types[0], reg))
7935 first_reg_op = 0;
7936 else
7937 first_reg_op = 1;
7938 /* Pretend we saw the extra register operand. */
9c2799c2 7939 gas_assert (i.reg_operands == 1
7ab9ffdd 7940 && i.op[first_reg_op + 1].regs == 0);
e2ec9d29
L
7941 i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
7942 i.types[first_reg_op + 1] = i.types[first_reg_op];
7943 i.operands++;
7944 i.reg_operands++;
29b0f896
AM
7945 }
7946
85b80b0f 7947 if (i.tm.opcode_modifier.modrm)
29b0f896
AM
7948 {
7949 /* The opcode is completed (modulo i.tm.extension_opcode which
52271982
AM
7950 must be put into the modrm byte). Now, we make the modrm and
7951 index base bytes based on all the info we've collected. */
29b0f896
AM
7952
7953 default_seg = build_modrm_byte ();
7954 }
00cee14f 7955 else if (i.types[0].bitfield.class == SReg)
85b80b0f
JB
7956 {
7957 if (flag_code != CODE_64BIT
7958 ? i.tm.base_opcode == POP_SEG_SHORT
7959 && i.op[0].regs->reg_num == 1
389d00a5 7960 : (i.tm.base_opcode | 1) == (POP_SEG386_SHORT & 0xff)
85b80b0f
JB
7961 && i.op[0].regs->reg_num < 4)
7962 {
7963 as_bad (_("you can't `%s %s%s'"),
7964 i.tm.name, register_prefix, i.op[0].regs->reg_name);
7965 return 0;
7966 }
389d00a5
JB
7967 if (i.op[0].regs->reg_num > 3
7968 && i.tm.opcode_modifier.opcodespace == SPACE_BASE )
85b80b0f 7969 {
389d00a5
JB
7970 i.tm.base_opcode ^= (POP_SEG_SHORT ^ POP_SEG386_SHORT) & 0xff;
7971 i.tm.opcode_modifier.opcodespace = SPACE_0F;
85b80b0f
JB
7972 }
7973 i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
7974 }
389d00a5
JB
7975 else if (i.tm.opcode_modifier.opcodespace == SPACE_BASE
7976 && (i.tm.base_opcode & ~3) == MOV_AX_DISP32)
29b0f896 7977 {
5e042380 7978 default_seg = reg_ds;
29b0f896 7979 }
40fb9820 7980 else if (i.tm.opcode_modifier.isstring)
29b0f896
AM
7981 {
7982 /* For the string instructions that allow a segment override
7983 on one of their operands, the default segment is ds. */
5e042380 7984 default_seg = reg_ds;
29b0f896 7985 }
50128d0c 7986 else if (i.short_form)
85b80b0f
JB
7987 {
7988 /* The register or float register operand is in operand
7989 0 or 1. */
bab6aec1 7990 unsigned int op = i.tm.operand_types[0].bitfield.class != Reg;
85b80b0f
JB
7991
7992 /* Register goes in low 3 bits of opcode. */
7993 i.tm.base_opcode |= i.op[op].regs->reg_num;
7994 if ((i.op[op].regs->reg_flags & RegRex) != 0)
7995 i.rex |= REX_B;
255571cd 7996 if (!quiet_warnings && i.tm.opcode_modifier.operandconstraint == UGH)
85b80b0f
JB
7997 {
7998 /* Warn about some common errors, but press on regardless.
7999 The first case can be generated by gcc (<= 2.8.1). */
8000 if (i.operands == 2)
8001 {
8002 /* Reversed arguments on faddp, fsubp, etc. */
8003 as_warn (_("translating to `%s %s%s,%s%s'"), i.tm.name,
8004 register_prefix, i.op[!intel_syntax].regs->reg_name,
8005 register_prefix, i.op[intel_syntax].regs->reg_name);
8006 }
8007 else
8008 {
8009 /* Extraneous `l' suffix on fp insn. */
8010 as_warn (_("translating to `%s %s%s'"), i.tm.name,
8011 register_prefix, i.op[0].regs->reg_name);
8012 }
8013 }
8014 }
29b0f896 8015
514a8bb0 8016 if ((i.seg[0] || i.prefix[SEG_PREFIX])
514a8bb0 8017 && i.tm.base_opcode == 0x8d /* lea */
35648716 8018 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
514a8bb0 8019 && !is_any_vex_encoding(&i.tm))
92334ad2
JB
8020 {
8021 if (!quiet_warnings)
8022 as_warn (_("segment override on `%s' is ineffectual"), i.tm.name);
8023 if (optimize)
8024 {
8025 i.seg[0] = NULL;
8026 i.prefix[SEG_PREFIX] = 0;
8027 }
8028 }
52271982
AM
8029
8030 /* If a segment was explicitly specified, and the specified segment
b6773884
JB
8031 is neither the default nor the one already recorded from a prefix,
8032 use an opcode prefix to select it. If we never figured out what
8033 the default segment is, then default_seg will be zero at this
8034 point, and the specified segment prefix will always be used. */
8035 if (i.seg[0]
8036 && i.seg[0] != default_seg
5e042380 8037 && i386_seg_prefixes[i.seg[0]->reg_num] != i.prefix[SEG_PREFIX])
29b0f896 8038 {
5e042380 8039 if (!add_prefix (i386_seg_prefixes[i.seg[0]->reg_num]))
29b0f896
AM
8040 return 0;
8041 }
8042 return 1;
8043}
8044
a5aeccd9 8045static INLINE void set_rex_vrex (const reg_entry *r, unsigned int rex_bit,
5b7c81bd 8046 bool do_sse2avx)
a5aeccd9
JB
8047{
8048 if (r->reg_flags & RegRex)
8049 {
8050 if (i.rex & rex_bit)
8051 as_bad (_("same type of prefix used twice"));
8052 i.rex |= rex_bit;
8053 }
8054 else if (do_sse2avx && (i.rex & rex_bit) && i.vex.register_specifier)
8055 {
8056 gas_assert (i.vex.register_specifier == r);
8057 i.vex.register_specifier += 8;
8058 }
8059
8060 if (r->reg_flags & RegVRex)
8061 i.vrex |= rex_bit;
8062}
8063
5e042380 8064static const reg_entry *
e3bb37b5 8065build_modrm_byte (void)
29b0f896 8066{
5e042380 8067 const reg_entry *default_seg = NULL;
c0f3af97 8068 unsigned int source, dest;
8cd7925b 8069 int vex_3_sources;
c0f3af97 8070
8cd7925b 8071 vex_3_sources = i.tm.opcode_modifier.vexsources == VEX3SOURCES;
c0f3af97
L
8072 if (vex_3_sources)
8073 {
91d6fa6a 8074 unsigned int nds, reg_slot;
4c2c6516 8075 expressionS *exp;
c0f3af97 8076
6b8d3588 8077 dest = i.operands - 1;
c0f3af97 8078 nds = dest - 1;
922d8de8 8079
a683cc34 8080 /* There are 2 kinds of instructions:
bed3d976 8081 1. 5 operands: 4 register operands or 3 register operands
9d3bf266 8082 plus 1 memory operand plus one Imm4 operand, VexXDS, and
bed3d976 8083 VexW0 or VexW1. The destination must be either XMM, YMM or
43234a1e 8084 ZMM register.
bed3d976 8085 2. 4 operands: 4 register operands or 3 register operands
2f1bada2 8086 plus 1 memory operand, with VexXDS. */
922d8de8 8087 gas_assert ((i.reg_operands == 4
bed3d976
JB
8088 || (i.reg_operands == 3 && i.mem_operands == 1))
8089 && i.tm.opcode_modifier.vexvvvv == VEXXDS
dcd7e323 8090 && i.tm.opcode_modifier.vexw
3528c362 8091 && i.tm.operand_types[dest].bitfield.class == RegSIMD);
a683cc34 8092
48db9223
JB
8093 /* If VexW1 is set, the first non-immediate operand is the source and
8094 the second non-immediate one is encoded in the immediate operand. */
8095 if (i.tm.opcode_modifier.vexw == VEXW1)
8096 {
8097 source = i.imm_operands;
8098 reg_slot = i.imm_operands + 1;
8099 }
8100 else
8101 {
8102 source = i.imm_operands + 1;
8103 reg_slot = i.imm_operands;
8104 }
8105
a683cc34 8106 if (i.imm_operands == 0)
bed3d976
JB
8107 {
8108 /* When there is no immediate operand, generate an 8bit
8109 immediate operand to encode the first operand. */
8110 exp = &im_expressions[i.imm_operands++];
8111 i.op[i.operands].imms = exp;
be1643ff 8112 i.types[i.operands].bitfield.imm8 = 1;
bed3d976
JB
8113 i.operands++;
8114
3528c362 8115 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
bed3d976
JB
8116 exp->X_op = O_constant;
8117 exp->X_add_number = register_number (i.op[reg_slot].regs) << 4;
43234a1e
L
8118 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
8119 }
922d8de8 8120 else
bed3d976 8121 {
9d3bf266
JB
8122 gas_assert (i.imm_operands == 1);
8123 gas_assert (fits_in_imm4 (i.op[0].imms->X_add_number));
8124 gas_assert (!i.tm.opcode_modifier.immext);
a683cc34 8125
9d3bf266
JB
8126 /* Turn on Imm8 again so that output_imm will generate it. */
8127 i.types[0].bitfield.imm8 = 1;
bed3d976 8128
3528c362 8129 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
9d3bf266 8130 i.op[0].imms->X_add_number
bed3d976 8131 |= register_number (i.op[reg_slot].regs) << 4;
43234a1e 8132 gas_assert ((i.op[reg_slot].regs->reg_flags & RegVRex) == 0);
bed3d976 8133 }
a683cc34 8134
3528c362 8135 gas_assert (i.tm.operand_types[nds].bitfield.class == RegSIMD);
dae39acc 8136 i.vex.register_specifier = i.op[nds].regs;
c0f3af97
L
8137 }
8138 else
8139 source = dest = 0;
29b0f896
AM
8140
8141 /* i.reg_operands MUST be the number of real register operands;
c0f3af97
L
8142 implicit registers do not count. If there are 3 register
8143 operands, it must be a instruction with VexNDS. For a
8144 instruction with VexNDD, the destination register is encoded
8145 in VEX prefix. If there are 4 register operands, it must be
8146 a instruction with VEX prefix and 3 sources. */
7ab9ffdd
L
8147 if (i.mem_operands == 0
8148 && ((i.reg_operands == 2
2426c15f 8149 && i.tm.opcode_modifier.vexvvvv <= VEXXDS)
7ab9ffdd 8150 || (i.reg_operands == 3
2426c15f 8151 && i.tm.opcode_modifier.vexvvvv == VEXXDS)
7ab9ffdd 8152 || (i.reg_operands == 4 && vex_3_sources)))
29b0f896 8153 {
cab737b9
L
8154 switch (i.operands)
8155 {
8156 case 2:
8157 source = 0;
8158 break;
8159 case 3:
c81128dc
L
8160 /* When there are 3 operands, one of them may be immediate,
8161 which may be the first or the last operand. Otherwise,
c0f3af97
L
8162 the first operand must be shift count register (cl) or it
8163 is an instruction with VexNDS. */
9c2799c2 8164 gas_assert (i.imm_operands == 1
7ab9ffdd 8165 || (i.imm_operands == 0
2426c15f 8166 && (i.tm.opcode_modifier.vexvvvv == VEXXDS
75e5731b
JB
8167 || (i.types[0].bitfield.instance == RegC
8168 && i.types[0].bitfield.byte))));
40fb9820 8169 if (operand_type_check (i.types[0], imm)
75e5731b
JB
8170 || (i.types[0].bitfield.instance == RegC
8171 && i.types[0].bitfield.byte))
40fb9820
L
8172 source = 1;
8173 else
8174 source = 0;
cab737b9
L
8175 break;
8176 case 4:
368d64cc
L
8177 /* When there are 4 operands, the first two must be 8bit
8178 immediate operands. The source operand will be the 3rd
c0f3af97
L
8179 one.
8180
8181 For instructions with VexNDS, if the first operand
8182 an imm8, the source operand is the 2nd one. If the last
8183 operand is imm8, the source operand is the first one. */
9c2799c2 8184 gas_assert ((i.imm_operands == 2
7ab9ffdd
L
8185 && i.types[0].bitfield.imm8
8186 && i.types[1].bitfield.imm8)
2426c15f 8187 || (i.tm.opcode_modifier.vexvvvv == VEXXDS
7ab9ffdd
L
8188 && i.imm_operands == 1
8189 && (i.types[0].bitfield.imm8
cf665fee 8190 || i.types[i.operands - 1].bitfield.imm8)));
9f2670f2
L
8191 if (i.imm_operands == 2)
8192 source = 2;
8193 else
c0f3af97
L
8194 {
8195 if (i.types[0].bitfield.imm8)
8196 source = 1;
8197 else
8198 source = 0;
8199 }
c0f3af97
L
8200 break;
8201 case 5:
cf665fee
JB
8202 gas_assert (!is_evex_encoding (&i.tm));
8203 gas_assert (i.imm_operands == 1 && vex_3_sources);
cab737b9
L
8204 break;
8205 default:
8206 abort ();
8207 }
8208
c0f3af97
L
8209 if (!vex_3_sources)
8210 {
8211 dest = source + 1;
8212
2426c15f 8213 if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
c0f3af97 8214 {
43234a1e 8215 /* For instructions with VexNDS, the register-only source
c5d0745b 8216 operand must be a 32/64bit integer, XMM, YMM, ZMM, or mask
dfd69174 8217 register. It is encoded in VEX prefix. */
f12dc422
L
8218
8219 i386_operand_type op;
8220 unsigned int vvvv;
8221
c2ecccb3 8222 /* Swap two source operands if needed. */
255571cd 8223 if (i.tm.opcode_modifier.operandconstraint == SWAP_SOURCES)
f12dc422
L
8224 {
8225 vvvv = source;
8226 source = dest;
8227 }
8228 else
8229 vvvv = dest;
8230
8231 op = i.tm.operand_types[vvvv];
c0f3af97 8232 if ((dest + 1) >= i.operands
bab6aec1 8233 || ((op.bitfield.class != Reg
dc821c5f 8234 || (!op.bitfield.dword && !op.bitfield.qword))
3528c362 8235 && op.bitfield.class != RegSIMD
c0f327b8 8236 && op.bitfield.class != RegMask))
c0f3af97 8237 abort ();
f12dc422 8238 i.vex.register_specifier = i.op[vvvv].regs;
c0f3af97
L
8239 dest++;
8240 }
8241 }
29b0f896
AM
8242
8243 i.rm.mode = 3;
dfd69174
JB
8244 /* One of the register operands will be encoded in the i.rm.reg
8245 field, the other in the combined i.rm.mode and i.rm.regmem
29b0f896
AM
8246 fields. If no form of this instruction supports a memory
8247 destination operand, then we assume the source operand may
8248 sometimes be a memory operand and so we need to store the
8249 destination in the i.rm.reg field. */
dfd69174 8250 if (!i.tm.opcode_modifier.regmem
40fb9820 8251 && operand_type_check (i.tm.operand_types[dest], anymem) == 0)
29b0f896
AM
8252 {
8253 i.rm.reg = i.op[dest].regs->reg_num;
8254 i.rm.regmem = i.op[source].regs->reg_num;
a5aeccd9 8255 set_rex_vrex (i.op[dest].regs, REX_R, i.tm.opcode_modifier.sse2avx);
5b7c81bd 8256 set_rex_vrex (i.op[source].regs, REX_B, false);
29b0f896
AM
8257 }
8258 else
8259 {
8260 i.rm.reg = i.op[source].regs->reg_num;
8261 i.rm.regmem = i.op[dest].regs->reg_num;
a5aeccd9 8262 set_rex_vrex (i.op[dest].regs, REX_B, i.tm.opcode_modifier.sse2avx);
5b7c81bd 8263 set_rex_vrex (i.op[source].regs, REX_R, false);
29b0f896 8264 }
e0c7f900 8265 if (flag_code != CODE_64BIT && (i.rex & REX_R))
c4a530c5 8266 {
4a5c67ed 8267 if (i.types[!i.tm.opcode_modifier.regmem].bitfield.class != RegCR)
c4a530c5 8268 abort ();
e0c7f900 8269 i.rex &= ~REX_R;
c4a530c5
JB
8270 add_prefix (LOCK_PREFIX_OPCODE);
8271 }
29b0f896
AM
8272 }
8273 else
8274 { /* If it's not 2 reg operands... */
c0f3af97
L
8275 unsigned int mem;
8276
29b0f896
AM
8277 if (i.mem_operands)
8278 {
8279 unsigned int fake_zero_displacement = 0;
99018f42 8280 unsigned int op;
4eed87de 8281
7ab9ffdd 8282 for (op = 0; op < i.operands; op++)
8dc0818e 8283 if (i.flags[op] & Operand_Mem)
7ab9ffdd 8284 break;
7ab9ffdd 8285 gas_assert (op < i.operands);
29b0f896 8286
63112cd6 8287 if (i.tm.opcode_modifier.sib)
6c30d220 8288 {
260cd341
LC
8289 /* The index register of VSIB shouldn't be RegIZ. */
8290 if (i.tm.opcode_modifier.sib != SIBMEM
8291 && i.index_reg->reg_num == RegIZ)
6c30d220
L
8292 abort ();
8293
8294 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8295 if (!i.base_reg)
8296 {
8297 i.sib.base = NO_BASE_REGISTER;
8298 i.sib.scale = i.log2_scale_factor;
2f2be86b 8299 i.types[op] = operand_type_and_not (i.types[op], anydisp);
a775efc8 8300 i.types[op].bitfield.disp32 = 1;
6c30d220 8301 }
260cd341
LC
8302
8303 /* Since the mandatory SIB always has index register, so
8304 the code logic remains unchanged. The non-mandatory SIB
8305 without index register is allowed and will be handled
8306 later. */
8307 if (i.index_reg)
8308 {
8309 if (i.index_reg->reg_num == RegIZ)
8310 i.sib.index = NO_INDEX_REGISTER;
8311 else
8312 i.sib.index = i.index_reg->reg_num;
5b7c81bd 8313 set_rex_vrex (i.index_reg, REX_X, false);
260cd341 8314 }
6c30d220
L
8315 }
8316
5e042380 8317 default_seg = reg_ds;
29b0f896
AM
8318
8319 if (i.base_reg == 0)
8320 {
8321 i.rm.mode = 0;
8322 if (!i.disp_operands)
9bb129e8 8323 fake_zero_displacement = 1;
29b0f896
AM
8324 if (i.index_reg == 0)
8325 {
260cd341
LC
8326 /* Both check for VSIB and mandatory non-vector SIB. */
8327 gas_assert (!i.tm.opcode_modifier.sib
8328 || i.tm.opcode_modifier.sib == SIBMEM);
29b0f896 8329 /* Operand is just <disp> */
2f2be86b 8330 i.types[op] = operand_type_and_not (i.types[op], anydisp);
20f0a1fc 8331 if (flag_code == CODE_64BIT)
29b0f896
AM
8332 {
8333 /* 64bit mode overwrites the 32bit absolute
8334 addressing by RIP relative addressing and
8335 absolute addressing is encoded by one of the
8336 redundant SIB forms. */
8337 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8338 i.sib.base = NO_BASE_REGISTER;
8339 i.sib.index = NO_INDEX_REGISTER;
a775efc8 8340 i.types[op].bitfield.disp32 = 1;
20f0a1fc 8341 }
fc225355
L
8342 else if ((flag_code == CODE_16BIT)
8343 ^ (i.prefix[ADDR_PREFIX] != 0))
20f0a1fc
NC
8344 {
8345 i.rm.regmem = NO_BASE_REGISTER_16;
2f2be86b 8346 i.types[op].bitfield.disp16 = 1;
20f0a1fc
NC
8347 }
8348 else
8349 {
8350 i.rm.regmem = NO_BASE_REGISTER;
2f2be86b 8351 i.types[op].bitfield.disp32 = 1;
29b0f896
AM
8352 }
8353 }
63112cd6 8354 else if (!i.tm.opcode_modifier.sib)
29b0f896 8355 {
6c30d220 8356 /* !i.base_reg && i.index_reg */
e968fc9b 8357 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8358 i.sib.index = NO_INDEX_REGISTER;
8359 else
8360 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8361 i.sib.base = NO_BASE_REGISTER;
8362 i.sib.scale = i.log2_scale_factor;
8363 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
2f2be86b 8364 i.types[op] = operand_type_and_not (i.types[op], anydisp);
a775efc8 8365 i.types[op].bitfield.disp32 = 1;
29b0f896 8366 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8367 i.rex |= REX_X;
29b0f896
AM
8368 }
8369 }
8370 /* RIP addressing for 64bit mode. */
e968fc9b 8371 else if (i.base_reg->reg_num == RegIP)
29b0f896 8372 {
63112cd6 8373 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896 8374 i.rm.regmem = NO_BASE_REGISTER;
40fb9820
L
8375 i.types[op].bitfield.disp8 = 0;
8376 i.types[op].bitfield.disp16 = 0;
a775efc8 8377 i.types[op].bitfield.disp32 = 1;
40fb9820 8378 i.types[op].bitfield.disp64 = 0;
71903a11 8379 i.flags[op] |= Operand_PCrel;
20f0a1fc
NC
8380 if (! i.disp_operands)
8381 fake_zero_displacement = 1;
29b0f896 8382 }
dc821c5f 8383 else if (i.base_reg->reg_type.bitfield.word)
29b0f896 8384 {
63112cd6 8385 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896
AM
8386 switch (i.base_reg->reg_num)
8387 {
8388 case 3: /* (%bx) */
8389 if (i.index_reg == 0)
8390 i.rm.regmem = 7;
8391 else /* (%bx,%si) -> 0, or (%bx,%di) -> 1 */
8392 i.rm.regmem = i.index_reg->reg_num - 6;
8393 break;
8394 case 5: /* (%bp) */
5e042380 8395 default_seg = reg_ss;
29b0f896
AM
8396 if (i.index_reg == 0)
8397 {
8398 i.rm.regmem = 6;
40fb9820 8399 if (operand_type_check (i.types[op], disp) == 0)
29b0f896
AM
8400 {
8401 /* fake (%bp) into 0(%bp) */
41eb8e88 8402 if (i.disp_encoding == disp_encoding_16bit)
1a02d6b0
L
8403 i.types[op].bitfield.disp16 = 1;
8404 else
8405 i.types[op].bitfield.disp8 = 1;
252b5132 8406 fake_zero_displacement = 1;
29b0f896
AM
8407 }
8408 }
8409 else /* (%bp,%si) -> 2, or (%bp,%di) -> 3 */
8410 i.rm.regmem = i.index_reg->reg_num - 6 + 2;
8411 break;
8412 default: /* (%si) -> 4 or (%di) -> 5 */
8413 i.rm.regmem = i.base_reg->reg_num - 6 + 4;
8414 }
41eb8e88
L
8415 if (!fake_zero_displacement
8416 && !i.disp_operands
8417 && i.disp_encoding)
8418 {
8419 fake_zero_displacement = 1;
8420 if (i.disp_encoding == disp_encoding_8bit)
8421 i.types[op].bitfield.disp8 = 1;
8422 else
8423 i.types[op].bitfield.disp16 = 1;
8424 }
29b0f896
AM
8425 i.rm.mode = mode_from_disp_size (i.types[op]);
8426 }
8427 else /* i.base_reg and 32/64 bit mode */
8428 {
a9aabc23 8429 if (operand_type_check (i.types[op], disp))
40fb9820 8430 {
73053c1f
JB
8431 i.types[op].bitfield.disp16 = 0;
8432 i.types[op].bitfield.disp64 = 0;
a775efc8 8433 i.types[op].bitfield.disp32 = 1;
40fb9820 8434 }
20f0a1fc 8435
63112cd6 8436 if (!i.tm.opcode_modifier.sib)
6c30d220 8437 i.rm.regmem = i.base_reg->reg_num;
29b0f896 8438 if ((i.base_reg->reg_flags & RegRex) != 0)
161a04f6 8439 i.rex |= REX_B;
29b0f896
AM
8440 i.sib.base = i.base_reg->reg_num;
8441 /* x86-64 ignores REX prefix bit here to avoid decoder
8442 complications. */
848930b2
JB
8443 if (!(i.base_reg->reg_flags & RegRex)
8444 && (i.base_reg->reg_num == EBP_REG_NUM
8445 || i.base_reg->reg_num == ESP_REG_NUM))
5e042380 8446 default_seg = reg_ss;
848930b2 8447 if (i.base_reg->reg_num == 5 && i.disp_operands == 0)
29b0f896 8448 {
848930b2 8449 fake_zero_displacement = 1;
1a02d6b0
L
8450 if (i.disp_encoding == disp_encoding_32bit)
8451 i.types[op].bitfield.disp32 = 1;
8452 else
8453 i.types[op].bitfield.disp8 = 1;
29b0f896
AM
8454 }
8455 i.sib.scale = i.log2_scale_factor;
8456 if (i.index_reg == 0)
8457 {
260cd341
LC
8458 /* Only check for VSIB. */
8459 gas_assert (i.tm.opcode_modifier.sib != VECSIB128
8460 && i.tm.opcode_modifier.sib != VECSIB256
8461 && i.tm.opcode_modifier.sib != VECSIB512);
8462
29b0f896
AM
8463 /* <disp>(%esp) becomes two byte modrm with no index
8464 register. We've already stored the code for esp
8465 in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
8466 Any base register besides %esp will not use the
8467 extra modrm byte. */
8468 i.sib.index = NO_INDEX_REGISTER;
29b0f896 8469 }
63112cd6 8470 else if (!i.tm.opcode_modifier.sib)
29b0f896 8471 {
e968fc9b 8472 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8473 i.sib.index = NO_INDEX_REGISTER;
8474 else
8475 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8476 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8477 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8478 i.rex |= REX_X;
29b0f896 8479 }
67a4f2b7
AO
8480
8481 if (i.disp_operands
8482 && (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
8483 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL))
8484 i.rm.mode = 0;
8485 else
a501d77e
L
8486 {
8487 if (!fake_zero_displacement
8488 && !i.disp_operands
8489 && i.disp_encoding)
8490 {
8491 fake_zero_displacement = 1;
8492 if (i.disp_encoding == disp_encoding_8bit)
8493 i.types[op].bitfield.disp8 = 1;
8494 else
8495 i.types[op].bitfield.disp32 = 1;
8496 }
8497 i.rm.mode = mode_from_disp_size (i.types[op]);
8498 }
29b0f896 8499 }
252b5132 8500
29b0f896
AM
8501 if (fake_zero_displacement)
8502 {
8503 /* Fakes a zero displacement assuming that i.types[op]
8504 holds the correct displacement size. */
8505 expressionS *exp;
8506
9c2799c2 8507 gas_assert (i.op[op].disps == 0);
29b0f896
AM
8508 exp = &disp_expressions[i.disp_operands++];
8509 i.op[op].disps = exp;
8510 exp->X_op = O_constant;
8511 exp->X_add_number = 0;
8512 exp->X_add_symbol = (symbolS *) 0;
8513 exp->X_op_symbol = (symbolS *) 0;
8514 }
c0f3af97
L
8515
8516 mem = op;
29b0f896 8517 }
c0f3af97
L
8518 else
8519 mem = ~0;
252b5132 8520
8c43a48b 8521 if (i.tm.opcode_modifier.vexsources == XOP2SOURCES)
5dd85c99
SP
8522 {
8523 if (operand_type_check (i.types[0], imm))
8524 i.vex.register_specifier = NULL;
8525 else
8526 {
8527 /* VEX.vvvv encodes one of the sources when the first
8528 operand is not an immediate. */
1ef99a7b 8529 if (i.tm.opcode_modifier.vexw == VEXW0)
5dd85c99
SP
8530 i.vex.register_specifier = i.op[0].regs;
8531 else
8532 i.vex.register_specifier = i.op[1].regs;
8533 }
8534
8535 /* Destination is a XMM register encoded in the ModRM.reg
8536 and VEX.R bit. */
8537 i.rm.reg = i.op[2].regs->reg_num;
8538 if ((i.op[2].regs->reg_flags & RegRex) != 0)
8539 i.rex |= REX_R;
8540
8541 /* ModRM.rm and VEX.B encodes the other source. */
8542 if (!i.mem_operands)
8543 {
8544 i.rm.mode = 3;
8545
1ef99a7b 8546 if (i.tm.opcode_modifier.vexw == VEXW0)
5dd85c99
SP
8547 i.rm.regmem = i.op[1].regs->reg_num;
8548 else
8549 i.rm.regmem = i.op[0].regs->reg_num;
8550
8551 if ((i.op[1].regs->reg_flags & RegRex) != 0)
8552 i.rex |= REX_B;
8553 }
8554 }
2426c15f 8555 else if (i.tm.opcode_modifier.vexvvvv == VEXLWP)
f88c9eb0
SP
8556 {
8557 i.vex.register_specifier = i.op[2].regs;
8558 if (!i.mem_operands)
8559 {
8560 i.rm.mode = 3;
8561 i.rm.regmem = i.op[1].regs->reg_num;
8562 if ((i.op[1].regs->reg_flags & RegRex) != 0)
8563 i.rex |= REX_B;
8564 }
8565 }
29b0f896
AM
8566 /* Fill in i.rm.reg or i.rm.regmem field with register operand
8567 (if any) based on i.tm.extension_opcode. Again, we must be
8568 careful to make sure that segment/control/debug/test/MMX
8569 registers are coded into the i.rm.reg field. */
f88c9eb0 8570 else if (i.reg_operands)
29b0f896 8571 {
99018f42 8572 unsigned int op;
7ab9ffdd
L
8573 unsigned int vex_reg = ~0;
8574
8575 for (op = 0; op < i.operands; op++)
921eafea
L
8576 if (i.types[op].bitfield.class == Reg
8577 || i.types[op].bitfield.class == RegBND
8578 || i.types[op].bitfield.class == RegMask
8579 || i.types[op].bitfield.class == SReg
8580 || i.types[op].bitfield.class == RegCR
8581 || i.types[op].bitfield.class == RegDR
8582 || i.types[op].bitfield.class == RegTR
8583 || i.types[op].bitfield.class == RegSIMD
8584 || i.types[op].bitfield.class == RegMMX)
8585 break;
c0209578 8586
7ab9ffdd
L
8587 if (vex_3_sources)
8588 op = dest;
2426c15f 8589 else if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
7ab9ffdd
L
8590 {
8591 /* For instructions with VexNDS, the register-only
8592 source operand is encoded in VEX prefix. */
8593 gas_assert (mem != (unsigned int) ~0);
c0f3af97 8594
a93e3234 8595 if (op > mem || i.tm.cpu_flags.bitfield.cpucmpccxadd)
c0f3af97 8596 {
7ab9ffdd
L
8597 vex_reg = op++;
8598 gas_assert (op < i.operands);
c0f3af97
L
8599 }
8600 else
c0f3af97 8601 {
f12dc422
L
8602 /* Check register-only source operand when two source
8603 operands are swapped. */
8604 if (!i.tm.operand_types[op].bitfield.baseindex
8605 && i.tm.operand_types[op + 1].bitfield.baseindex)
8606 {
8607 vex_reg = op;
8608 op += 2;
8609 gas_assert (mem == (vex_reg + 1)
8610 && op < i.operands);
8611 }
8612 else
8613 {
8614 vex_reg = op + 1;
8615 gas_assert (vex_reg < i.operands);
8616 }
c0f3af97 8617 }
7ab9ffdd 8618 }
2426c15f 8619 else if (i.tm.opcode_modifier.vexvvvv == VEXNDD)
7ab9ffdd 8620 {
f12dc422 8621 /* For instructions with VexNDD, the register destination
7ab9ffdd 8622 is encoded in VEX prefix. */
f12dc422
L
8623 if (i.mem_operands == 0)
8624 {
8625 /* There is no memory operand. */
8626 gas_assert ((op + 2) == i.operands);
8627 vex_reg = op + 1;
8628 }
8629 else
8d63c93e 8630 {
ed438a93
JB
8631 /* There are only 2 non-immediate operands. */
8632 gas_assert (op < i.imm_operands + 2
8633 && i.operands == i.imm_operands + 2);
8634 vex_reg = i.imm_operands + 1;
f12dc422 8635 }
7ab9ffdd
L
8636 }
8637 else
8638 gas_assert (op < i.operands);
99018f42 8639
7ab9ffdd
L
8640 if (vex_reg != (unsigned int) ~0)
8641 {
f12dc422 8642 i386_operand_type *type = &i.tm.operand_types[vex_reg];
7ab9ffdd 8643
bab6aec1 8644 if ((type->bitfield.class != Reg
dc821c5f 8645 || (!type->bitfield.dword && !type->bitfield.qword))
3528c362 8646 && type->bitfield.class != RegSIMD
c0f327b8 8647 && type->bitfield.class != RegMask)
7ab9ffdd 8648 abort ();
f88c9eb0 8649
7ab9ffdd
L
8650 i.vex.register_specifier = i.op[vex_reg].regs;
8651 }
8652
1b9f0c97
L
8653 /* Don't set OP operand twice. */
8654 if (vex_reg != op)
7ab9ffdd 8655 {
1b9f0c97
L
8656 /* If there is an extension opcode to put here, the
8657 register number must be put into the regmem field. */
8658 if (i.tm.extension_opcode != None)
8659 {
8660 i.rm.regmem = i.op[op].regs->reg_num;
a5aeccd9
JB
8661 set_rex_vrex (i.op[op].regs, REX_B,
8662 i.tm.opcode_modifier.sse2avx);
1b9f0c97
L
8663 }
8664 else
8665 {
8666 i.rm.reg = i.op[op].regs->reg_num;
a5aeccd9
JB
8667 set_rex_vrex (i.op[op].regs, REX_R,
8668 i.tm.opcode_modifier.sse2avx);
1b9f0c97 8669 }
7ab9ffdd 8670 }
252b5132 8671
29b0f896
AM
8672 /* Now, if no memory operand has set i.rm.mode = 0, 1, 2 we
8673 must set it to 3 to indicate this is a register operand
8674 in the regmem field. */
8675 if (!i.mem_operands)
8676 i.rm.mode = 3;
8677 }
252b5132 8678
29b0f896 8679 /* Fill in i.rm.reg field with extension opcode (if any). */
c1e679ec 8680 if (i.tm.extension_opcode != None)
29b0f896
AM
8681 i.rm.reg = i.tm.extension_opcode;
8682 }
8683 return default_seg;
8684}
252b5132 8685
48ef937e
JB
8686static INLINE void
8687frag_opcode_byte (unsigned char byte)
8688{
8689 if (now_seg != absolute_section)
8690 FRAG_APPEND_1_CHAR (byte);
8691 else
8692 ++abs_section_offset;
8693}
8694
376cd056
JB
8695static unsigned int
8696flip_code16 (unsigned int code16)
8697{
8698 gas_assert (i.tm.operands == 1);
8699
8700 return !(i.prefix[REX_PREFIX] & REX_W)
8701 && (code16 ? i.tm.operand_types[0].bitfield.disp32
376cd056
JB
8702 : i.tm.operand_types[0].bitfield.disp16)
8703 ? CODE16 : 0;
8704}
8705
29b0f896 8706static void
e3bb37b5 8707output_branch (void)
29b0f896
AM
8708{
8709 char *p;
f8a5c266 8710 int size;
29b0f896
AM
8711 int code16;
8712 int prefix;
8713 relax_substateT subtype;
8714 symbolS *sym;
8715 offsetT off;
8716
48ef937e
JB
8717 if (now_seg == absolute_section)
8718 {
8719 as_bad (_("relaxable branches not supported in absolute section"));
8720 return;
8721 }
8722
f8a5c266 8723 code16 = flag_code == CODE_16BIT ? CODE16 : 0;
1a42a9fe 8724 size = i.disp_encoding > disp_encoding_8bit ? BIG : SMALL;
29b0f896
AM
8725
8726 prefix = 0;
8727 if (i.prefix[DATA_PREFIX] != 0)
252b5132 8728 {
29b0f896
AM
8729 prefix = 1;
8730 i.prefixes -= 1;
376cd056 8731 code16 ^= flip_code16(code16);
252b5132 8732 }
29b0f896
AM
8733 /* Pentium4 branch hints. */
8734 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8735 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
2f66722d 8736 {
29b0f896
AM
8737 prefix++;
8738 i.prefixes--;
8739 }
8740 if (i.prefix[REX_PREFIX] != 0)
8741 {
8742 prefix++;
8743 i.prefixes--;
2f66722d
AM
8744 }
8745
7e8b059b
L
8746 /* BND prefixed jump. */
8747 if (i.prefix[BND_PREFIX] != 0)
8748 {
6cb0a70e
JB
8749 prefix++;
8750 i.prefixes--;
7e8b059b
L
8751 }
8752
f2810fe0
JB
8753 if (i.prefixes != 0)
8754 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
29b0f896
AM
8755
8756 /* It's always a symbol; End frag & setup for relax.
8757 Make sure there is enough room in this frag for the largest
8758 instruction we may generate in md_convert_frag. This is 2
8759 bytes for the opcode and room for the prefix and largest
8760 displacement. */
8761 frag_grow (prefix + 2 + 4);
8762 /* Prefix and 1 opcode byte go in fr_fix. */
8763 p = frag_more (prefix + 1);
8764 if (i.prefix[DATA_PREFIX] != 0)
8765 *p++ = DATA_PREFIX_OPCODE;
8766 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
8767 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
8768 *p++ = i.prefix[SEG_PREFIX];
6cb0a70e
JB
8769 if (i.prefix[BND_PREFIX] != 0)
8770 *p++ = BND_PREFIX_OPCODE;
29b0f896
AM
8771 if (i.prefix[REX_PREFIX] != 0)
8772 *p++ = i.prefix[REX_PREFIX];
8773 *p = i.tm.base_opcode;
8774
8775 if ((unsigned char) *p == JUMP_PC_RELATIVE)
f8a5c266 8776 subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, size);
40fb9820 8777 else if (cpu_arch_flags.bitfield.cpui386)
f8a5c266 8778 subtype = ENCODE_RELAX_STATE (COND_JUMP, size);
29b0f896 8779 else
f8a5c266 8780 subtype = ENCODE_RELAX_STATE (COND_JUMP86, size);
29b0f896 8781 subtype |= code16;
3e73aa7c 8782
29b0f896
AM
8783 sym = i.op[0].disps->X_add_symbol;
8784 off = i.op[0].disps->X_add_number;
3e73aa7c 8785
29b0f896
AM
8786 if (i.op[0].disps->X_op != O_constant
8787 && i.op[0].disps->X_op != O_symbol)
3e73aa7c 8788 {
29b0f896
AM
8789 /* Handle complex expressions. */
8790 sym = make_expr_symbol (i.op[0].disps);
8791 off = 0;
8792 }
3e73aa7c 8793
1ef3994a
JB
8794 frag_now->tc_frag_data.code64 = flag_code == CODE_64BIT;
8795
29b0f896
AM
8796 /* 1 possible extra opcode + 4 byte displacement go in var part.
8797 Pass reloc in fr_var. */
d258b828 8798 frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
29b0f896 8799}
3e73aa7c 8800
bd7ab16b
L
8801#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8802/* Return TRUE iff PLT32 relocation should be used for branching to
8803 symbol S. */
8804
5b7c81bd 8805static bool
bd7ab16b
L
8806need_plt32_p (symbolS *s)
8807{
8808 /* PLT32 relocation is ELF only. */
8809 if (!IS_ELF)
5b7c81bd 8810 return false;
bd7ab16b 8811
a5def729
RO
8812#ifdef TE_SOLARIS
8813 /* Don't emit PLT32 relocation on Solaris: neither native linker nor
8814 krtld support it. */
5b7c81bd 8815 return false;
a5def729
RO
8816#endif
8817
bd7ab16b
L
8818 /* Since there is no need to prepare for PLT branch on x86-64, we
8819 can generate R_X86_64_PLT32, instead of R_X86_64_PC32, which can
8820 be used as a marker for 32-bit PC-relative branches. */
8821 if (!object_64bit)
5b7c81bd 8822 return false;
bd7ab16b 8823
44365e88 8824 if (s == NULL)
5b7c81bd 8825 return false;
44365e88 8826
bd7ab16b
L
8827 /* Weak or undefined symbol need PLT32 relocation. */
8828 if (S_IS_WEAK (s) || !S_IS_DEFINED (s))
5b7c81bd 8829 return true;
bd7ab16b
L
8830
8831 /* Non-global symbol doesn't need PLT32 relocation. */
8832 if (! S_IS_EXTERNAL (s))
5b7c81bd 8833 return false;
bd7ab16b
L
8834
8835 /* Other global symbols need PLT32 relocation. NB: Symbol with
8836 non-default visibilities are treated as normal global symbol
8837 so that PLT32 relocation can be used as a marker for 32-bit
8838 PC-relative branches. It is useful for linker relaxation. */
5b7c81bd 8839 return true;
bd7ab16b
L
8840}
8841#endif
8842
29b0f896 8843static void
e3bb37b5 8844output_jump (void)
29b0f896
AM
8845{
8846 char *p;
8847 int size;
3e02c1cc 8848 fixS *fixP;
bd7ab16b 8849 bfd_reloc_code_real_type jump_reloc = i.reloc[0];
29b0f896 8850
0cfa3eb3 8851 if (i.tm.opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
8852 {
8853 /* This is a loop or jecxz type instruction. */
8854 size = 1;
8855 if (i.prefix[ADDR_PREFIX] != 0)
8856 {
48ef937e 8857 frag_opcode_byte (ADDR_PREFIX_OPCODE);
29b0f896
AM
8858 i.prefixes -= 1;
8859 }
8860 /* Pentium4 branch hints. */
8861 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8862 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
8863 {
48ef937e 8864 frag_opcode_byte (i.prefix[SEG_PREFIX]);
29b0f896 8865 i.prefixes--;
3e73aa7c
JH
8866 }
8867 }
29b0f896
AM
8868 else
8869 {
8870 int code16;
3e73aa7c 8871
29b0f896
AM
8872 code16 = 0;
8873 if (flag_code == CODE_16BIT)
8874 code16 = CODE16;
3e73aa7c 8875
29b0f896
AM
8876 if (i.prefix[DATA_PREFIX] != 0)
8877 {
48ef937e 8878 frag_opcode_byte (DATA_PREFIX_OPCODE);
29b0f896 8879 i.prefixes -= 1;
376cd056 8880 code16 ^= flip_code16(code16);
29b0f896 8881 }
252b5132 8882
29b0f896
AM
8883 size = 4;
8884 if (code16)
8885 size = 2;
8886 }
9fcc94b6 8887
6cb0a70e
JB
8888 /* BND prefixed jump. */
8889 if (i.prefix[BND_PREFIX] != 0)
29b0f896 8890 {
48ef937e 8891 frag_opcode_byte (i.prefix[BND_PREFIX]);
29b0f896
AM
8892 i.prefixes -= 1;
8893 }
252b5132 8894
6cb0a70e 8895 if (i.prefix[REX_PREFIX] != 0)
7e8b059b 8896 {
48ef937e 8897 frag_opcode_byte (i.prefix[REX_PREFIX]);
7e8b059b
L
8898 i.prefixes -= 1;
8899 }
8900
f2810fe0
JB
8901 if (i.prefixes != 0)
8902 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
e0890092 8903
48ef937e
JB
8904 if (now_seg == absolute_section)
8905 {
9a182d04 8906 abs_section_offset += i.opcode_length + size;
48ef937e
JB
8907 return;
8908 }
8909
9a182d04
JB
8910 p = frag_more (i.opcode_length + size);
8911 switch (i.opcode_length)
42164a71
L
8912 {
8913 case 2:
8914 *p++ = i.tm.base_opcode >> 8;
1a0670f3 8915 /* Fall through. */
42164a71
L
8916 case 1:
8917 *p++ = i.tm.base_opcode;
8918 break;
8919 default:
8920 abort ();
8921 }
e0890092 8922
bd7ab16b 8923#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1ef3994a
JB
8924 if (flag_code == CODE_64BIT && size == 4
8925 && jump_reloc == NO_RELOC && i.op[0].disps->X_add_number == 0
bd7ab16b
L
8926 && need_plt32_p (i.op[0].disps->X_add_symbol))
8927 jump_reloc = BFD_RELOC_X86_64_PLT32;
8928#endif
8929
8930 jump_reloc = reloc (size, 1, 1, jump_reloc);
8931
3e02c1cc 8932 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
bd7ab16b 8933 i.op[0].disps, 1, jump_reloc);
3e02c1cc 8934
eb19308f
JB
8935 /* All jumps handled here are signed, but don't unconditionally use a
8936 signed limit check for 32 and 16 bit jumps as we want to allow wrap
8937 around at 4G (outside of 64-bit mode) and 64k (except for XBEGIN)
8938 respectively. */
8939 switch (size)
8940 {
8941 case 1:
8942 fixP->fx_signed = 1;
8943 break;
8944
8945 case 2:
8946 if (i.tm.base_opcode == 0xc7f8)
8947 fixP->fx_signed = 1;
8948 break;
8949
8950 case 4:
8951 if (flag_code == CODE_64BIT)
8952 fixP->fx_signed = 1;
8953 break;
8954 }
29b0f896 8955}
e0890092 8956
29b0f896 8957static void
e3bb37b5 8958output_interseg_jump (void)
29b0f896
AM
8959{
8960 char *p;
8961 int size;
8962 int prefix;
8963 int code16;
252b5132 8964
29b0f896
AM
8965 code16 = 0;
8966 if (flag_code == CODE_16BIT)
8967 code16 = CODE16;
a217f122 8968
29b0f896
AM
8969 prefix = 0;
8970 if (i.prefix[DATA_PREFIX] != 0)
8971 {
8972 prefix = 1;
8973 i.prefixes -= 1;
8974 code16 ^= CODE16;
8975 }
6cb0a70e
JB
8976
8977 gas_assert (!i.prefix[REX_PREFIX]);
252b5132 8978
29b0f896
AM
8979 size = 4;
8980 if (code16)
8981 size = 2;
252b5132 8982
f2810fe0
JB
8983 if (i.prefixes != 0)
8984 as_warn (_("skipping prefixes on `%s'"), i.tm.name);
252b5132 8985
48ef937e
JB
8986 if (now_seg == absolute_section)
8987 {
8988 abs_section_offset += prefix + 1 + 2 + size;
8989 return;
8990 }
8991
29b0f896
AM
8992 /* 1 opcode; 2 segment; offset */
8993 p = frag_more (prefix + 1 + 2 + size);
3e73aa7c 8994
29b0f896
AM
8995 if (i.prefix[DATA_PREFIX] != 0)
8996 *p++ = DATA_PREFIX_OPCODE;
252b5132 8997
29b0f896
AM
8998 if (i.prefix[REX_PREFIX] != 0)
8999 *p++ = i.prefix[REX_PREFIX];
252b5132 9000
29b0f896
AM
9001 *p++ = i.tm.base_opcode;
9002 if (i.op[1].imms->X_op == O_constant)
9003 {
9004 offsetT n = i.op[1].imms->X_add_number;
252b5132 9005
29b0f896
AM
9006 if (size == 2
9007 && !fits_in_unsigned_word (n)
9008 && !fits_in_signed_word (n))
9009 {
9010 as_bad (_("16-bit jump out of range"));
9011 return;
9012 }
9013 md_number_to_chars (p, n, size);
9014 }
9015 else
9016 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
d258b828 9017 i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
6d96a594
C
9018
9019 p += size;
9020 if (i.op[0].imms->X_op == O_constant)
9021 md_number_to_chars (p, (valueT) i.op[0].imms->X_add_number, 2);
9022 else
9023 fix_new_exp (frag_now, p - frag_now->fr_literal, 2,
9024 i.op[0].imms, 0, reloc (2, 0, 0, i.reloc[0]));
29b0f896 9025}
a217f122 9026
b4a3a7b4
L
9027#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9028void
9029x86_cleanup (void)
9030{
9031 char *p;
9032 asection *seg = now_seg;
9033 subsegT subseg = now_subseg;
9034 asection *sec;
9035 unsigned int alignment, align_size_1;
9036 unsigned int isa_1_descsz, feature_2_descsz, descsz;
9037 unsigned int isa_1_descsz_raw, feature_2_descsz_raw;
9038 unsigned int padding;
9039
1273b2f8 9040 if (!IS_ELF || !x86_used_note)
b4a3a7b4
L
9041 return;
9042
b4a3a7b4
L
9043 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X86;
9044
9045 /* The .note.gnu.property section layout:
9046
9047 Field Length Contents
9048 ---- ---- ----
9049 n_namsz 4 4
9050 n_descsz 4 The note descriptor size
9051 n_type 4 NT_GNU_PROPERTY_TYPE_0
9052 n_name 4 "GNU"
9053 n_desc n_descsz The program property array
9054 .... .... ....
9055 */
9056
9057 /* Create the .note.gnu.property section. */
9058 sec = subseg_new (NOTE_GNU_PROPERTY_SECTION_NAME, 0);
fd361982 9059 bfd_set_section_flags (sec,
b4a3a7b4
L
9060 (SEC_ALLOC
9061 | SEC_LOAD
9062 | SEC_DATA
9063 | SEC_HAS_CONTENTS
9064 | SEC_READONLY));
9065
9066 if (get_elf_backend_data (stdoutput)->s->elfclass == ELFCLASS64)
9067 {
9068 align_size_1 = 7;
9069 alignment = 3;
9070 }
9071 else
9072 {
9073 align_size_1 = 3;
9074 alignment = 2;
9075 }
9076
fd361982 9077 bfd_set_section_alignment (sec, alignment);
b4a3a7b4
L
9078 elf_section_type (sec) = SHT_NOTE;
9079
1273b2f8
L
9080 /* GNU_PROPERTY_X86_ISA_1_USED: 4-byte type + 4-byte data size
9081 + 4-byte data */
9082 isa_1_descsz_raw = 4 + 4 + 4;
9083 /* Align GNU_PROPERTY_X86_ISA_1_USED. */
9084 isa_1_descsz = (isa_1_descsz_raw + align_size_1) & ~align_size_1;
b4a3a7b4
L
9085
9086 feature_2_descsz_raw = isa_1_descsz;
9087 /* GNU_PROPERTY_X86_FEATURE_2_USED: 4-byte type + 4-byte data size
1273b2f8 9088 + 4-byte data */
b4a3a7b4
L
9089 feature_2_descsz_raw += 4 + 4 + 4;
9090 /* Align GNU_PROPERTY_X86_FEATURE_2_USED. */
9091 feature_2_descsz = ((feature_2_descsz_raw + align_size_1)
9092 & ~align_size_1);
9093
9094 descsz = feature_2_descsz;
9095 /* Section size: n_namsz + n_descsz + n_type + n_name + n_descsz. */
9096 p = frag_more (4 + 4 + 4 + 4 + descsz);
9097
9098 /* Write n_namsz. */
9099 md_number_to_chars (p, (valueT) 4, 4);
9100
9101 /* Write n_descsz. */
9102 md_number_to_chars (p + 4, (valueT) descsz, 4);
9103
9104 /* Write n_type. */
9105 md_number_to_chars (p + 4 * 2, (valueT) NT_GNU_PROPERTY_TYPE_0, 4);
9106
9107 /* Write n_name. */
9108 memcpy (p + 4 * 3, "GNU", 4);
9109
1273b2f8
L
9110 /* Write 4-byte type. */
9111 md_number_to_chars (p + 4 * 4,
9112 (valueT) GNU_PROPERTY_X86_ISA_1_USED, 4);
b4a3a7b4 9113
1273b2f8
L
9114 /* Write 4-byte data size. */
9115 md_number_to_chars (p + 4 * 5, (valueT) 4, 4);
b4a3a7b4 9116
1273b2f8
L
9117 /* Write 4-byte data. */
9118 md_number_to_chars (p + 4 * 6, (valueT) x86_isa_1_used, 4);
b4a3a7b4 9119
1273b2f8
L
9120 /* Zero out paddings. */
9121 padding = isa_1_descsz - isa_1_descsz_raw;
9122 if (padding)
9123 memset (p + 4 * 7, 0, padding);
b4a3a7b4
L
9124
9125 /* Write 4-byte type. */
9126 md_number_to_chars (p + isa_1_descsz + 4 * 4,
9127 (valueT) GNU_PROPERTY_X86_FEATURE_2_USED, 4);
9128
9129 /* Write 4-byte data size. */
9130 md_number_to_chars (p + isa_1_descsz + 4 * 5, (valueT) 4, 4);
9131
9132 /* Write 4-byte data. */
9133 md_number_to_chars (p + isa_1_descsz + 4 * 6,
9134 (valueT) x86_feature_2_used, 4);
9135
9136 /* Zero out paddings. */
9137 padding = feature_2_descsz - feature_2_descsz_raw;
9138 if (padding)
9139 memset (p + isa_1_descsz + 4 * 7, 0, padding);
9140
9141 /* We probably can't restore the current segment, for there likely
9142 isn't one yet... */
9143 if (seg && subseg)
9144 subseg_set (seg, subseg);
9145}
b52c4ee4
IB
9146
9147bool
9148x86_support_sframe_p (void)
9149{
9150 /* At this time, SFrame unwind is supported for AMD64 ABI only. */
9151 return (x86_elf_abi == X86_64_ABI);
9152}
9153
9154bool
9155x86_sframe_ra_tracking_p (void)
9156{
9157 /* In AMD64, return address is always stored on the stack at a fixed offset
9158 from the CFA (provided via x86_sframe_cfa_ra_offset ()).
9159 Do not track explicitly via an SFrame Frame Row Entry. */
9160 return false;
9161}
9162
9163offsetT
9164x86_sframe_cfa_ra_offset (void)
9165{
9166 gas_assert (x86_elf_abi == X86_64_ABI);
9167 return (offsetT) -8;
9168}
9169
9170unsigned char
9171x86_sframe_get_abi_arch (void)
9172{
9173 unsigned char sframe_abi_arch = 0;
9174
9175 if (x86_support_sframe_p ())
9176 {
9177 gas_assert (!target_big_endian);
9178 sframe_abi_arch = SFRAME_ABI_AMD64_ENDIAN_LITTLE;
9179 }
9180
9181 return sframe_abi_arch;
9182}
9183
b4a3a7b4
L
9184#endif
9185
9c33702b
JB
9186static unsigned int
9187encoding_length (const fragS *start_frag, offsetT start_off,
9188 const char *frag_now_ptr)
9189{
9190 unsigned int len = 0;
9191
9192 if (start_frag != frag_now)
9193 {
9194 const fragS *fr = start_frag;
9195
9196 do {
9197 len += fr->fr_fix;
9198 fr = fr->fr_next;
9199 } while (fr && fr != frag_now);
9200 }
9201
9202 return len - start_off + (frag_now_ptr - frag_now->fr_literal);
9203}
9204
e379e5f3 9205/* Return 1 for test, and, cmp, add, sub, inc and dec which may
79d72f45
HL
9206 be macro-fused with conditional jumps.
9207 NB: If TEST/AND/CMP/ADD/SUB/INC/DEC is of RIP relative address,
9208 or is one of the following format:
9209
9210 cmp m, imm
9211 add m, imm
9212 sub m, imm
9213 test m, imm
9214 and m, imm
9215 inc m
9216 dec m
9217
9218 it is unfusible. */
e379e5f3
L
9219
9220static int
79d72f45 9221maybe_fused_with_jcc_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
9222{
9223 /* No RIP address. */
9224 if (i.base_reg && i.base_reg->reg_num == RegIP)
9225 return 0;
9226
389d00a5
JB
9227 /* No opcodes outside of base encoding space. */
9228 if (i.tm.opcode_modifier.opcodespace != SPACE_BASE)
e379e5f3
L
9229 return 0;
9230
79d72f45
HL
9231 /* add, sub without add/sub m, imm. */
9232 if (i.tm.base_opcode <= 5
e379e5f3
L
9233 || (i.tm.base_opcode >= 0x28 && i.tm.base_opcode <= 0x2d)
9234 || ((i.tm.base_opcode | 3) == 0x83
79d72f45 9235 && (i.tm.extension_opcode == 0x5
e379e5f3 9236 || i.tm.extension_opcode == 0x0)))
79d72f45
HL
9237 {
9238 *mf_cmp_p = mf_cmp_alu_cmp;
9239 return !(i.mem_operands && i.imm_operands);
9240 }
e379e5f3 9241
79d72f45
HL
9242 /* and without and m, imm. */
9243 if ((i.tm.base_opcode >= 0x20 && i.tm.base_opcode <= 0x25)
9244 || ((i.tm.base_opcode | 3) == 0x83
9245 && i.tm.extension_opcode == 0x4))
9246 {
9247 *mf_cmp_p = mf_cmp_test_and;
9248 return !(i.mem_operands && i.imm_operands);
9249 }
9250
9251 /* test without test m imm. */
e379e5f3
L
9252 if ((i.tm.base_opcode | 1) == 0x85
9253 || (i.tm.base_opcode | 1) == 0xa9
9254 || ((i.tm.base_opcode | 1) == 0xf7
79d72f45
HL
9255 && i.tm.extension_opcode == 0))
9256 {
9257 *mf_cmp_p = mf_cmp_test_and;
9258 return !(i.mem_operands && i.imm_operands);
9259 }
9260
9261 /* cmp without cmp m, imm. */
9262 if ((i.tm.base_opcode >= 0x38 && i.tm.base_opcode <= 0x3d)
e379e5f3
L
9263 || ((i.tm.base_opcode | 3) == 0x83
9264 && (i.tm.extension_opcode == 0x7)))
79d72f45
HL
9265 {
9266 *mf_cmp_p = mf_cmp_alu_cmp;
9267 return !(i.mem_operands && i.imm_operands);
9268 }
e379e5f3 9269
79d72f45 9270 /* inc, dec without inc/dec m. */
e379e5f3
L
9271 if ((i.tm.cpu_flags.bitfield.cpuno64
9272 && (i.tm.base_opcode | 0xf) == 0x4f)
9273 || ((i.tm.base_opcode | 1) == 0xff
9274 && i.tm.extension_opcode <= 0x1))
79d72f45
HL
9275 {
9276 *mf_cmp_p = mf_cmp_incdec;
9277 return !i.mem_operands;
9278 }
e379e5f3
L
9279
9280 return 0;
9281}
9282
9283/* Return 1 if a FUSED_JCC_PADDING frag should be generated. */
9284
9285static int
79d72f45 9286add_fused_jcc_padding_frag_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
9287{
9288 /* NB: Don't work with COND_JUMP86 without i386. */
9289 if (!align_branch_power
9290 || now_seg == absolute_section
9291 || !cpu_arch_flags.bitfield.cpui386
9292 || !(align_branch & align_branch_fused_bit))
9293 return 0;
9294
79d72f45 9295 if (maybe_fused_with_jcc_p (mf_cmp_p))
e379e5f3
L
9296 {
9297 if (last_insn.kind == last_insn_other
9298 || last_insn.seg != now_seg)
9299 return 1;
9300 if (flag_debug)
9301 as_warn_where (last_insn.file, last_insn.line,
9302 _("`%s` skips -malign-branch-boundary on `%s`"),
9303 last_insn.name, i.tm.name);
9304 }
9305
9306 return 0;
9307}
9308
9309/* Return 1 if a BRANCH_PREFIX frag should be generated. */
9310
9311static int
9312add_branch_prefix_frag_p (void)
9313{
9314 /* NB: Don't work with COND_JUMP86 without i386. Don't add prefix
9315 to PadLock instructions since they include prefixes in opcode. */
9316 if (!align_branch_power
9317 || !align_branch_prefix_size
9318 || now_seg == absolute_section
9319 || i.tm.cpu_flags.bitfield.cpupadlock
9320 || !cpu_arch_flags.bitfield.cpui386)
9321 return 0;
9322
9323 /* Don't add prefix if it is a prefix or there is no operand in case
9324 that segment prefix is special. */
9325 if (!i.operands || i.tm.opcode_modifier.isprefix)
9326 return 0;
9327
9328 if (last_insn.kind == last_insn_other
9329 || last_insn.seg != now_seg)
9330 return 1;
9331
9332 if (flag_debug)
9333 as_warn_where (last_insn.file, last_insn.line,
9334 _("`%s` skips -malign-branch-boundary on `%s`"),
9335 last_insn.name, i.tm.name);
9336
9337 return 0;
9338}
9339
9340/* Return 1 if a BRANCH_PADDING frag should be generated. */
9341
9342static int
79d72f45
HL
9343add_branch_padding_frag_p (enum align_branch_kind *branch_p,
9344 enum mf_jcc_kind *mf_jcc_p)
e379e5f3
L
9345{
9346 int add_padding;
9347
9348 /* NB: Don't work with COND_JUMP86 without i386. */
9349 if (!align_branch_power
9350 || now_seg == absolute_section
389d00a5
JB
9351 || !cpu_arch_flags.bitfield.cpui386
9352 || i.tm.opcode_modifier.opcodespace != SPACE_BASE)
e379e5f3
L
9353 return 0;
9354
9355 add_padding = 0;
9356
9357 /* Check for jcc and direct jmp. */
9358 if (i.tm.opcode_modifier.jump == JUMP)
9359 {
9360 if (i.tm.base_opcode == JUMP_PC_RELATIVE)
9361 {
9362 *branch_p = align_branch_jmp;
9363 add_padding = align_branch & align_branch_jmp_bit;
9364 }
9365 else
9366 {
79d72f45
HL
9367 /* Because J<cc> and JN<cc> share same group in macro-fusible table,
9368 igore the lowest bit. */
9369 *mf_jcc_p = (i.tm.base_opcode & 0x0e) >> 1;
e379e5f3
L
9370 *branch_p = align_branch_jcc;
9371 if ((align_branch & align_branch_jcc_bit))
9372 add_padding = 1;
9373 }
9374 }
e379e5f3
L
9375 else if ((i.tm.base_opcode | 1) == 0xc3)
9376 {
9377 /* Near ret. */
9378 *branch_p = align_branch_ret;
9379 if ((align_branch & align_branch_ret_bit))
9380 add_padding = 1;
9381 }
9382 else
9383 {
9384 /* Check for indirect jmp, direct and indirect calls. */
9385 if (i.tm.base_opcode == 0xe8)
9386 {
9387 /* Direct call. */
9388 *branch_p = align_branch_call;
9389 if ((align_branch & align_branch_call_bit))
9390 add_padding = 1;
9391 }
9392 else if (i.tm.base_opcode == 0xff
9393 && (i.tm.extension_opcode == 2
9394 || i.tm.extension_opcode == 4))
9395 {
9396 /* Indirect call and jmp. */
9397 *branch_p = align_branch_indirect;
9398 if ((align_branch & align_branch_indirect_bit))
9399 add_padding = 1;
9400 }
9401
9402 if (add_padding
9403 && i.disp_operands
9404 && tls_get_addr
9405 && (i.op[0].disps->X_op == O_symbol
9406 || (i.op[0].disps->X_op == O_subtract
9407 && i.op[0].disps->X_op_symbol == GOT_symbol)))
9408 {
9409 symbolS *s = i.op[0].disps->X_add_symbol;
9410 /* No padding to call to global or undefined tls_get_addr. */
9411 if ((S_IS_EXTERNAL (s) || !S_IS_DEFINED (s))
9412 && strcmp (S_GET_NAME (s), tls_get_addr) == 0)
9413 return 0;
9414 }
9415 }
9416
9417 if (add_padding
9418 && last_insn.kind != last_insn_other
9419 && last_insn.seg == now_seg)
9420 {
9421 if (flag_debug)
9422 as_warn_where (last_insn.file, last_insn.line,
9423 _("`%s` skips -malign-branch-boundary on `%s`"),
9424 last_insn.name, i.tm.name);
9425 return 0;
9426 }
9427
9428 return add_padding;
9429}
9430
29b0f896 9431static void
e3bb37b5 9432output_insn (void)
29b0f896 9433{
2bbd9c25
JJ
9434 fragS *insn_start_frag;
9435 offsetT insn_start_off;
e379e5f3
L
9436 fragS *fragP = NULL;
9437 enum align_branch_kind branch = align_branch_none;
79d72f45
HL
9438 /* The initializer is arbitrary just to avoid uninitialized error.
9439 it's actually either assigned in add_branch_padding_frag_p
9440 or never be used. */
9441 enum mf_jcc_kind mf_jcc = mf_jcc_jo;
2bbd9c25 9442
b4a3a7b4 9443#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
48ef937e 9444 if (IS_ELF && x86_used_note && now_seg != absolute_section)
b4a3a7b4 9445 {
32930e4e
L
9446 if ((i.xstate & xstate_tmm) == xstate_tmm
9447 || i.tm.cpu_flags.bitfield.cpuamx_tile)
9448 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_TMM;
9449
b4a3a7b4
L
9450 if (i.tm.cpu_flags.bitfield.cpu8087
9451 || i.tm.cpu_flags.bitfield.cpu287
9452 || i.tm.cpu_flags.bitfield.cpu387
9453 || i.tm.cpu_flags.bitfield.cpu687
9454 || i.tm.cpu_flags.bitfield.cpufisttp)
9455 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X87;
014d61ea 9456
921eafea 9457 if ((i.xstate & xstate_mmx)
389d00a5
JB
9458 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
9459 && !is_any_vex_encoding (&i.tm)
9460 && (i.tm.base_opcode == 0x77 /* emms */
9461 || i.tm.base_opcode == 0x0e /* femms */)))
b4a3a7b4 9462 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MMX;
014d61ea 9463
32930e4e
L
9464 if (i.index_reg)
9465 {
9466 if (i.index_reg->reg_type.bitfield.zmmword)
9467 i.xstate |= xstate_zmm;
9468 else if (i.index_reg->reg_type.bitfield.ymmword)
9469 i.xstate |= xstate_ymm;
9470 else if (i.index_reg->reg_type.bitfield.xmmword)
9471 i.xstate |= xstate_xmm;
9472 }
014d61ea
JB
9473
9474 /* vzeroall / vzeroupper */
9475 if (i.tm.base_opcode == 0x77 && i.tm.cpu_flags.bitfield.cpuavx)
9476 i.xstate |= xstate_ymm;
9477
c4694f17 9478 if ((i.xstate & xstate_xmm)
389d00a5
JB
9479 /* ldmxcsr / stmxcsr / vldmxcsr / vstmxcsr */
9480 || (i.tm.base_opcode == 0xae
9481 && (i.tm.cpu_flags.bitfield.cpusse
9482 || i.tm.cpu_flags.bitfield.cpuavx))
c4694f17
TG
9483 || i.tm.cpu_flags.bitfield.cpuwidekl
9484 || i.tm.cpu_flags.bitfield.cpukl)
b4a3a7b4 9485 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XMM;
014d61ea 9486
921eafea 9487 if ((i.xstate & xstate_ymm) == xstate_ymm)
b4a3a7b4 9488 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_YMM;
921eafea 9489 if ((i.xstate & xstate_zmm) == xstate_zmm)
b4a3a7b4 9490 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_ZMM;
6225c532 9491 if (i.mask.reg || (i.xstate & xstate_mask) == xstate_mask)
32930e4e 9492 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MASK;
b4a3a7b4
L
9493 if (i.tm.cpu_flags.bitfield.cpufxsr)
9494 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_FXSR;
9495 if (i.tm.cpu_flags.bitfield.cpuxsave)
9496 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVE;
9497 if (i.tm.cpu_flags.bitfield.cpuxsaveopt)
9498 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT;
9499 if (i.tm.cpu_flags.bitfield.cpuxsavec)
9500 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEC;
b0ab0693
L
9501
9502 if (x86_feature_2_used
9503 || i.tm.cpu_flags.bitfield.cpucmov
9504 || i.tm.cpu_flags.bitfield.cpusyscall
389d00a5
JB
9505 || (i.tm.opcode_modifier.opcodespace == SPACE_0F
9506 && i.tm.base_opcode == 0xc7
70e95837 9507 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
b0ab0693
L
9508 && i.tm.extension_opcode == 1) /* cmpxchg8b */)
9509 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_BASELINE;
9510 if (i.tm.cpu_flags.bitfield.cpusse3
9511 || i.tm.cpu_flags.bitfield.cpussse3
9512 || i.tm.cpu_flags.bitfield.cpusse4_1
9513 || i.tm.cpu_flags.bitfield.cpusse4_2
9514 || i.tm.cpu_flags.bitfield.cpucx16
9515 || i.tm.cpu_flags.bitfield.cpupopcnt
9516 /* LAHF-SAHF insns in 64-bit mode. */
9517 || (flag_code == CODE_64BIT
35648716
JB
9518 && (i.tm.base_opcode | 1) == 0x9f
9519 && i.tm.opcode_modifier.opcodespace == SPACE_BASE))
b0ab0693
L
9520 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V2;
9521 if (i.tm.cpu_flags.bitfield.cpuavx
9522 || i.tm.cpu_flags.bitfield.cpuavx2
a9860005
JB
9523 /* Any VEX encoded insns execpt for AVX512F, AVX512BW, AVX512DQ,
9524 XOP, FMA4, LPW, TBM, and AMX. */
b0ab0693
L
9525 || (i.tm.opcode_modifier.vex
9526 && !i.tm.cpu_flags.bitfield.cpuavx512f
9527 && !i.tm.cpu_flags.bitfield.cpuavx512bw
9528 && !i.tm.cpu_flags.bitfield.cpuavx512dq
a9860005
JB
9529 && !i.tm.cpu_flags.bitfield.cpuxop
9530 && !i.tm.cpu_flags.bitfield.cpufma4
b0ab0693
L
9531 && !i.tm.cpu_flags.bitfield.cpulwp
9532 && !i.tm.cpu_flags.bitfield.cputbm
9533 && !(x86_feature_2_used & GNU_PROPERTY_X86_FEATURE_2_TMM))
9534 || i.tm.cpu_flags.bitfield.cpuf16c
9535 || i.tm.cpu_flags.bitfield.cpufma
9536 || i.tm.cpu_flags.bitfield.cpulzcnt
9537 || i.tm.cpu_flags.bitfield.cpumovbe
9538 || i.tm.cpu_flags.bitfield.cpuxsaves
9539 || (x86_feature_2_used
9540 & (GNU_PROPERTY_X86_FEATURE_2_XSAVE
9541 | GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT
9542 | GNU_PROPERTY_X86_FEATURE_2_XSAVEC)) != 0)
9543 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V3;
9544 if (i.tm.cpu_flags.bitfield.cpuavx512f
9545 || i.tm.cpu_flags.bitfield.cpuavx512bw
9546 || i.tm.cpu_flags.bitfield.cpuavx512dq
9547 || i.tm.cpu_flags.bitfield.cpuavx512vl
a9860005
JB
9548 /* Any EVEX encoded insns except for AVX512ER, AVX512PF,
9549 AVX512-4FMAPS, and AVX512-4VNNIW. */
b0ab0693
L
9550 || (i.tm.opcode_modifier.evex
9551 && !i.tm.cpu_flags.bitfield.cpuavx512er
9552 && !i.tm.cpu_flags.bitfield.cpuavx512pf
a9860005 9553 && !i.tm.cpu_flags.bitfield.cpuavx512_4fmaps
b0ab0693
L
9554 && !i.tm.cpu_flags.bitfield.cpuavx512_4vnniw))
9555 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V4;
b4a3a7b4
L
9556 }
9557#endif
9558
29b0f896
AM
9559 /* Tie dwarf2 debug info to the address at the start of the insn.
9560 We can't do this after the insn has been output as the current
9561 frag may have been closed off. eg. by frag_var. */
9562 dwarf2_emit_insn (0);
9563
2bbd9c25
JJ
9564 insn_start_frag = frag_now;
9565 insn_start_off = frag_now_fix ();
9566
79d72f45 9567 if (add_branch_padding_frag_p (&branch, &mf_jcc))
e379e5f3
L
9568 {
9569 char *p;
9570 /* Branch can be 8 bytes. Leave some room for prefixes. */
9571 unsigned int max_branch_padding_size = 14;
9572
9573 /* Align section to boundary. */
9574 record_alignment (now_seg, align_branch_power);
9575
9576 /* Make room for padding. */
9577 frag_grow (max_branch_padding_size);
9578
9579 /* Start of the padding. */
9580 p = frag_more (0);
9581
9582 fragP = frag_now;
9583
9584 frag_var (rs_machine_dependent, max_branch_padding_size, 0,
9585 ENCODE_RELAX_STATE (BRANCH_PADDING, 0),
9586 NULL, 0, p);
9587
79d72f45 9588 fragP->tc_frag_data.mf_type = mf_jcc;
e379e5f3
L
9589 fragP->tc_frag_data.branch_type = branch;
9590 fragP->tc_frag_data.max_bytes = max_branch_padding_size;
9591 }
9592
d59a54c2
JB
9593 if (!cpu_arch_flags.bitfield.cpui386 && (flag_code != CODE_16BIT)
9594 && !pre_386_16bit_warned)
9595 {
9596 as_warn (_("use .code16 to ensure correct addressing mode"));
9597 pre_386_16bit_warned = true;
9598 }
9599
29b0f896 9600 /* Output jumps. */
0cfa3eb3 9601 if (i.tm.opcode_modifier.jump == JUMP)
29b0f896 9602 output_branch ();
0cfa3eb3
JB
9603 else if (i.tm.opcode_modifier.jump == JUMP_BYTE
9604 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896 9605 output_jump ();
0cfa3eb3 9606 else if (i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT)
29b0f896
AM
9607 output_interseg_jump ();
9608 else
9609 {
9610 /* Output normal instructions here. */
9611 char *p;
9612 unsigned char *q;
47465058 9613 unsigned int j;
79d72f45 9614 enum mf_cmp_kind mf_cmp;
4dffcebc 9615
e4e00185 9616 if (avoid_fence
389d00a5
JB
9617 && (i.tm.base_opcode == 0xaee8
9618 || i.tm.base_opcode == 0xaef0
9619 || i.tm.base_opcode == 0xaef8))
48ef937e
JB
9620 {
9621 /* Encode lfence, mfence, and sfence as
9622 f0 83 04 24 00 lock addl $0x0, (%{re}sp). */
47f4115a
JB
9623 if (flag_code == CODE_16BIT)
9624 as_bad (_("Cannot convert `%s' in 16-bit mode"), i.tm.name);
9625 else if (omit_lock_prefix)
9626 as_bad (_("Cannot convert `%s' with `-momit-lock-prefix=yes' in effect"),
9627 i.tm.name);
9628 else if (now_seg != absolute_section)
48ef937e
JB
9629 {
9630 offsetT val = 0x240483f0ULL;
9631
9632 p = frag_more (5);
9633 md_number_to_chars (p, val, 5);
9634 }
9635 else
9636 abs_section_offset += 5;
9637 return;
9638 }
e4e00185 9639
d022bddd
IT
9640 /* Some processors fail on LOCK prefix. This options makes
9641 assembler ignore LOCK prefix and serves as a workaround. */
9642 if (omit_lock_prefix)
9643 {
35648716
JB
9644 if (i.tm.base_opcode == LOCK_PREFIX_OPCODE
9645 && i.tm.opcode_modifier.isprefix)
d022bddd
IT
9646 return;
9647 i.prefix[LOCK_PREFIX] = 0;
9648 }
9649
e379e5f3
L
9650 if (branch)
9651 /* Skip if this is a branch. */
9652 ;
79d72f45 9653 else if (add_fused_jcc_padding_frag_p (&mf_cmp))
e379e5f3
L
9654 {
9655 /* Make room for padding. */
9656 frag_grow (MAX_FUSED_JCC_PADDING_SIZE);
9657 p = frag_more (0);
9658
9659 fragP = frag_now;
9660
9661 frag_var (rs_machine_dependent, MAX_FUSED_JCC_PADDING_SIZE, 0,
9662 ENCODE_RELAX_STATE (FUSED_JCC_PADDING, 0),
9663 NULL, 0, p);
9664
79d72f45 9665 fragP->tc_frag_data.mf_type = mf_cmp;
e379e5f3
L
9666 fragP->tc_frag_data.branch_type = align_branch_fused;
9667 fragP->tc_frag_data.max_bytes = MAX_FUSED_JCC_PADDING_SIZE;
9668 }
9669 else if (add_branch_prefix_frag_p ())
9670 {
9671 unsigned int max_prefix_size = align_branch_prefix_size;
9672
9673 /* Make room for padding. */
9674 frag_grow (max_prefix_size);
9675 p = frag_more (0);
9676
9677 fragP = frag_now;
9678
9679 frag_var (rs_machine_dependent, max_prefix_size, 0,
9680 ENCODE_RELAX_STATE (BRANCH_PREFIX, 0),
9681 NULL, 0, p);
9682
9683 fragP->tc_frag_data.max_bytes = max_prefix_size;
9684 }
9685
43234a1e
L
9686 /* Since the VEX/EVEX prefix contains the implicit prefix, we
9687 don't need the explicit prefix. */
cf665fee 9688 if (!is_any_vex_encoding (&i.tm))
bc4bd9ab 9689 {
7b47a312 9690 switch (i.tm.opcode_modifier.opcodeprefix)
bc4bd9ab 9691 {
7b47a312
L
9692 case PREFIX_0X66:
9693 add_prefix (0x66);
9694 break;
9695 case PREFIX_0XF2:
9696 add_prefix (0xf2);
9697 break;
9698 case PREFIX_0XF3:
8b65b895
L
9699 if (!i.tm.cpu_flags.bitfield.cpupadlock
9700 || (i.prefix[REP_PREFIX] != 0xf3))
9701 add_prefix (0xf3);
c0f3af97 9702 break;
7b47a312 9703 case PREFIX_NONE:
9a182d04 9704 switch (i.opcode_length)
c0f3af97 9705 {
7b47a312 9706 case 2:
7b47a312 9707 break;
9a182d04 9708 case 1:
7b47a312 9709 /* Check for pseudo prefixes. */
9a182d04
JB
9710 if (!i.tm.opcode_modifier.isprefix || i.tm.base_opcode)
9711 break;
7b47a312
L
9712 as_bad_where (insn_start_frag->fr_file,
9713 insn_start_frag->fr_line,
9714 _("pseudo prefix without instruction"));
9715 return;
9716 default:
9717 abort ();
4dffcebc 9718 }
c0f3af97 9719 break;
c0f3af97
L
9720 default:
9721 abort ();
bc4bd9ab 9722 }
c0f3af97 9723
6d19a37a 9724#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
cf61b747
L
9725 /* For x32, add a dummy REX_OPCODE prefix for mov/add with
9726 R_X86_64_GOTTPOFF relocation so that linker can safely
14470f07
L
9727 perform IE->LE optimization. A dummy REX_OPCODE prefix
9728 is also needed for lea with R_X86_64_GOTPC32_TLSDESC
9729 relocation for GDesc -> IE/LE optimization. */
cf61b747
L
9730 if (x86_elf_abi == X86_64_X32_ABI
9731 && i.operands == 2
14470f07
L
9732 && (i.reloc[0] == BFD_RELOC_X86_64_GOTTPOFF
9733 || i.reloc[0] == BFD_RELOC_X86_64_GOTPC32_TLSDESC)
cf61b747
L
9734 && i.prefix[REX_PREFIX] == 0)
9735 add_prefix (REX_OPCODE);
6d19a37a 9736#endif
cf61b747 9737
c0f3af97
L
9738 /* The prefix bytes. */
9739 for (j = ARRAY_SIZE (i.prefix), q = i.prefix; j > 0; j--, q++)
9740 if (*q)
48ef937e 9741 frag_opcode_byte (*q);
0f10071e 9742 }
ae5c1c7b 9743 else
c0f3af97
L
9744 {
9745 for (j = 0, q = i.prefix; j < ARRAY_SIZE (i.prefix); j++, q++)
9746 if (*q)
9747 switch (j)
9748 {
c0f3af97
L
9749 case SEG_PREFIX:
9750 case ADDR_PREFIX:
48ef937e 9751 frag_opcode_byte (*q);
c0f3af97
L
9752 break;
9753 default:
9754 /* There should be no other prefixes for instructions
9755 with VEX prefix. */
9756 abort ();
9757 }
9758
43234a1e
L
9759 /* For EVEX instructions i.vrex should become 0 after
9760 build_evex_prefix. For VEX instructions upper 16 registers
9761 aren't available, so VREX should be 0. */
9762 if (i.vrex)
9763 abort ();
c0f3af97 9764 /* Now the VEX prefix. */
48ef937e
JB
9765 if (now_seg != absolute_section)
9766 {
9767 p = frag_more (i.vex.length);
9768 for (j = 0; j < i.vex.length; j++)
9769 p[j] = i.vex.bytes[j];
9770 }
9771 else
9772 abs_section_offset += i.vex.length;
c0f3af97 9773 }
252b5132 9774
29b0f896 9775 /* Now the opcode; be careful about word order here! */
389d00a5
JB
9776 j = i.opcode_length;
9777 if (!i.vex.length)
9778 switch (i.tm.opcode_modifier.opcodespace)
9779 {
9780 case SPACE_BASE:
9781 break;
9782 case SPACE_0F:
9783 ++j;
9784 break;
9785 case SPACE_0F38:
9786 case SPACE_0F3A:
9787 j += 2;
9788 break;
9789 default:
9790 abort ();
9791 }
9792
48ef937e 9793 if (now_seg == absolute_section)
389d00a5
JB
9794 abs_section_offset += j;
9795 else if (j == 1)
29b0f896
AM
9796 {
9797 FRAG_APPEND_1_CHAR (i.tm.base_opcode);
9798 }
9799 else
9800 {
389d00a5
JB
9801 p = frag_more (j);
9802 if (!i.vex.length
9803 && i.tm.opcode_modifier.opcodespace != SPACE_BASE)
9804 {
9805 *p++ = 0x0f;
9806 if (i.tm.opcode_modifier.opcodespace != SPACE_0F)
9807 *p++ = i.tm.opcode_modifier.opcodespace == SPACE_0F38
9808 ? 0x38 : 0x3a;
9809 }
9810
9a182d04 9811 switch (i.opcode_length)
331d2d0d 9812 {
4dffcebc 9813 case 2:
389d00a5
JB
9814 /* Put out high byte first: can't use md_number_to_chars! */
9815 *p++ = (i.tm.base_opcode >> 8) & 0xff;
9816 /* Fall through. */
9817 case 1:
9818 *p = i.tm.base_opcode & 0xff;
4dffcebc
L
9819 break;
9820 default:
9821 abort ();
9822 break;
331d2d0d 9823 }
0f10071e 9824
29b0f896 9825 }
3e73aa7c 9826
29b0f896 9827 /* Now the modrm byte and sib byte (if present). */
40fb9820 9828 if (i.tm.opcode_modifier.modrm)
29b0f896 9829 {
48ef937e
JB
9830 frag_opcode_byte ((i.rm.regmem << 0)
9831 | (i.rm.reg << 3)
9832 | (i.rm.mode << 6));
29b0f896
AM
9833 /* If i.rm.regmem == ESP (4)
9834 && i.rm.mode != (Register mode)
9835 && not 16 bit
9836 ==> need second modrm byte. */
9837 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
9838 && i.rm.mode != 3
dc821c5f 9839 && !(i.base_reg && i.base_reg->reg_type.bitfield.word))
48ef937e
JB
9840 frag_opcode_byte ((i.sib.base << 0)
9841 | (i.sib.index << 3)
9842 | (i.sib.scale << 6));
29b0f896 9843 }
3e73aa7c 9844
29b0f896 9845 if (i.disp_operands)
2bbd9c25 9846 output_disp (insn_start_frag, insn_start_off);
3e73aa7c 9847
29b0f896 9848 if (i.imm_operands)
2bbd9c25 9849 output_imm (insn_start_frag, insn_start_off);
9c33702b
JB
9850
9851 /*
9852 * frag_now_fix () returning plain abs_section_offset when we're in the
9853 * absolute section, and abs_section_offset not getting updated as data
9854 * gets added to the frag breaks the logic below.
9855 */
9856 if (now_seg != absolute_section)
9857 {
9858 j = encoding_length (insn_start_frag, insn_start_off, frag_more (0));
9859 if (j > 15)
9860 as_warn (_("instruction length of %u bytes exceeds the limit of 15"),
9861 j);
e379e5f3
L
9862 else if (fragP)
9863 {
9864 /* NB: Don't add prefix with GOTPC relocation since
9865 output_disp() above depends on the fixed encoding
9866 length. Can't add prefix with TLS relocation since
9867 it breaks TLS linker optimization. */
9868 unsigned int max = i.has_gotpc_tls_reloc ? 0 : 15 - j;
9869 /* Prefix count on the current instruction. */
9870 unsigned int count = i.vex.length;
9871 unsigned int k;
9872 for (k = 0; k < ARRAY_SIZE (i.prefix); k++)
9873 /* REX byte is encoded in VEX/EVEX prefix. */
9874 if (i.prefix[k] && (k != REX_PREFIX || !i.vex.length))
9875 count++;
9876
9877 /* Count prefixes for extended opcode maps. */
9878 if (!i.vex.length)
389d00a5 9879 switch (i.tm.opcode_modifier.opcodespace)
e379e5f3 9880 {
389d00a5 9881 case SPACE_BASE:
e379e5f3 9882 break;
389d00a5
JB
9883 case SPACE_0F:
9884 count++;
e379e5f3 9885 break;
389d00a5
JB
9886 case SPACE_0F38:
9887 case SPACE_0F3A:
9888 count += 2;
e379e5f3
L
9889 break;
9890 default:
9891 abort ();
9892 }
9893
9894 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
9895 == BRANCH_PREFIX)
9896 {
9897 /* Set the maximum prefix size in BRANCH_PREFIX
9898 frag. */
9899 if (fragP->tc_frag_data.max_bytes > max)
9900 fragP->tc_frag_data.max_bytes = max;
9901 if (fragP->tc_frag_data.max_bytes > count)
9902 fragP->tc_frag_data.max_bytes -= count;
9903 else
9904 fragP->tc_frag_data.max_bytes = 0;
9905 }
9906 else
9907 {
9908 /* Remember the maximum prefix size in FUSED_JCC_PADDING
9909 frag. */
9910 unsigned int max_prefix_size;
9911 if (align_branch_prefix_size > max)
9912 max_prefix_size = max;
9913 else
9914 max_prefix_size = align_branch_prefix_size;
9915 if (max_prefix_size > count)
9916 fragP->tc_frag_data.max_prefix_length
9917 = max_prefix_size - count;
9918 }
9919
9920 /* Use existing segment prefix if possible. Use CS
9921 segment prefix in 64-bit mode. In 32-bit mode, use SS
9922 segment prefix with ESP/EBP base register and use DS
9923 segment prefix without ESP/EBP base register. */
9924 if (i.prefix[SEG_PREFIX])
9925 fragP->tc_frag_data.default_prefix = i.prefix[SEG_PREFIX];
9926 else if (flag_code == CODE_64BIT)
9927 fragP->tc_frag_data.default_prefix = CS_PREFIX_OPCODE;
9928 else if (i.base_reg
9929 && (i.base_reg->reg_num == 4
9930 || i.base_reg->reg_num == 5))
9931 fragP->tc_frag_data.default_prefix = SS_PREFIX_OPCODE;
9932 else
9933 fragP->tc_frag_data.default_prefix = DS_PREFIX_OPCODE;
9934 }
9c33702b 9935 }
29b0f896 9936 }
252b5132 9937
e379e5f3
L
9938 /* NB: Don't work with COND_JUMP86 without i386. */
9939 if (align_branch_power
9940 && now_seg != absolute_section
9941 && cpu_arch_flags.bitfield.cpui386)
9942 {
9943 /* Terminate each frag so that we can add prefix and check for
9944 fused jcc. */
9945 frag_wane (frag_now);
9946 frag_new (0);
9947 }
9948
29b0f896
AM
9949#ifdef DEBUG386
9950 if (flag_debug)
9951 {
7b81dfbb 9952 pi ("" /*line*/, &i);
29b0f896
AM
9953 }
9954#endif /* DEBUG386 */
9955}
252b5132 9956
e205caa7
L
9957/* Return the size of the displacement operand N. */
9958
9959static int
9960disp_size (unsigned int n)
9961{
9962 int size = 4;
43234a1e 9963
b5014f7a 9964 if (i.types[n].bitfield.disp64)
40fb9820
L
9965 size = 8;
9966 else if (i.types[n].bitfield.disp8)
9967 size = 1;
9968 else if (i.types[n].bitfield.disp16)
9969 size = 2;
e205caa7
L
9970 return size;
9971}
9972
9973/* Return the size of the immediate operand N. */
9974
9975static int
9976imm_size (unsigned int n)
9977{
9978 int size = 4;
40fb9820
L
9979 if (i.types[n].bitfield.imm64)
9980 size = 8;
9981 else if (i.types[n].bitfield.imm8 || i.types[n].bitfield.imm8s)
9982 size = 1;
9983 else if (i.types[n].bitfield.imm16)
9984 size = 2;
e205caa7
L
9985 return size;
9986}
9987
29b0f896 9988static void
64e74474 9989output_disp (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
9990{
9991 char *p;
9992 unsigned int n;
252b5132 9993
29b0f896
AM
9994 for (n = 0; n < i.operands; n++)
9995 {
b5014f7a 9996 if (operand_type_check (i.types[n], disp))
29b0f896 9997 {
48ef937e
JB
9998 int size = disp_size (n);
9999
10000 if (now_seg == absolute_section)
10001 abs_section_offset += size;
10002 else if (i.op[n].disps->X_op == O_constant)
29b0f896 10003 {
43234a1e 10004 offsetT val = i.op[n].disps->X_add_number;
252b5132 10005
629cfaf1
JB
10006 val = offset_in_range (val >> (size == 1 ? i.memshift : 0),
10007 size);
29b0f896
AM
10008 p = frag_more (size);
10009 md_number_to_chars (p, val, size);
10010 }
10011 else
10012 {
f86103b7 10013 enum bfd_reloc_code_real reloc_type;
a775efc8
JB
10014 bool pcrel = (i.flags[n] & Operand_PCrel) != 0;
10015 bool sign = (flag_code == CODE_64BIT && size == 4
10016 && (!want_disp32 (&i.tm)
10017 || (i.tm.opcode_modifier.jump && !i.jumpabsolute
10018 && !i.types[n].bitfield.baseindex)))
10019 || pcrel;
02a86693 10020 fixS *fixP;
29b0f896 10021
e205caa7 10022 /* We can't have 8 bit displacement here. */
9c2799c2 10023 gas_assert (!i.types[n].bitfield.disp8);
e205caa7 10024
29b0f896
AM
10025 /* The PC relative address is computed relative
10026 to the instruction boundary, so in case immediate
10027 fields follows, we need to adjust the value. */
10028 if (pcrel && i.imm_operands)
10029 {
29b0f896 10030 unsigned int n1;
e205caa7 10031 int sz = 0;
252b5132 10032
29b0f896 10033 for (n1 = 0; n1 < i.operands; n1++)
40fb9820 10034 if (operand_type_check (i.types[n1], imm))
252b5132 10035 {
e205caa7
L
10036 /* Only one immediate is allowed for PC
10037 relative address. */
9c2799c2 10038 gas_assert (sz == 0);
e205caa7
L
10039 sz = imm_size (n1);
10040 i.op[n].disps->X_add_number -= sz;
252b5132 10041 }
29b0f896 10042 /* We should find the immediate. */
9c2799c2 10043 gas_assert (sz != 0);
29b0f896 10044 }
520dc8e8 10045
29b0f896 10046 p = frag_more (size);
d258b828 10047 reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
d6ab8113 10048 if (GOT_symbol
2bbd9c25 10049 && GOT_symbol == i.op[n].disps->X_add_symbol
d6ab8113 10050 && (((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
10051 || reloc_type == BFD_RELOC_X86_64_32S
10052 || (reloc_type == BFD_RELOC_64
10053 && object_64bit))
d6ab8113
JB
10054 && (i.op[n].disps->X_op == O_symbol
10055 || (i.op[n].disps->X_op == O_add
10056 && ((symbol_get_value_expression
10057 (i.op[n].disps->X_op_symbol)->X_op)
10058 == O_subtract))))
10059 || reloc_type == BFD_RELOC_32_PCREL))
2bbd9c25 10060 {
4fa24527 10061 if (!object_64bit)
7b81dfbb
AJ
10062 {
10063 reloc_type = BFD_RELOC_386_GOTPC;
5b7c81bd 10064 i.has_gotpc_tls_reloc = true;
98da05bf 10065 i.op[n].disps->X_add_number +=
d583596c 10066 encoding_length (insn_start_frag, insn_start_off, p);
7b81dfbb
AJ
10067 }
10068 else if (reloc_type == BFD_RELOC_64)
10069 reloc_type = BFD_RELOC_X86_64_GOTPC64;
d6ab8113 10070 else
7b81dfbb
AJ
10071 /* Don't do the adjustment for x86-64, as there
10072 the pcrel addressing is relative to the _next_
10073 insn, and that is taken care of in other code. */
d6ab8113 10074 reloc_type = BFD_RELOC_X86_64_GOTPC32;
2bbd9c25 10075 }
e379e5f3
L
10076 else if (align_branch_power)
10077 {
10078 switch (reloc_type)
10079 {
10080 case BFD_RELOC_386_TLS_GD:
10081 case BFD_RELOC_386_TLS_LDM:
10082 case BFD_RELOC_386_TLS_IE:
10083 case BFD_RELOC_386_TLS_IE_32:
10084 case BFD_RELOC_386_TLS_GOTIE:
10085 case BFD_RELOC_386_TLS_GOTDESC:
10086 case BFD_RELOC_386_TLS_DESC_CALL:
10087 case BFD_RELOC_X86_64_TLSGD:
10088 case BFD_RELOC_X86_64_TLSLD:
10089 case BFD_RELOC_X86_64_GOTTPOFF:
10090 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
10091 case BFD_RELOC_X86_64_TLSDESC_CALL:
5b7c81bd 10092 i.has_gotpc_tls_reloc = true;
e379e5f3
L
10093 default:
10094 break;
10095 }
10096 }
02a86693
L
10097 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal,
10098 size, i.op[n].disps, pcrel,
10099 reloc_type);
eb19308f
JB
10100
10101 if (flag_code == CODE_64BIT && size == 4 && pcrel
10102 && !i.prefix[ADDR_PREFIX])
10103 fixP->fx_signed = 1;
10104
02a86693
L
10105 /* Check for "call/jmp *mem", "mov mem, %reg",
10106 "test %reg, mem" and "binop mem, %reg" where binop
10107 is one of adc, add, and, cmp, or, sbb, sub, xor
e60f4d3b
L
10108 instructions without data prefix. Always generate
10109 R_386_GOT32X for "sym*GOT" operand in 32-bit mode. */
10110 if (i.prefix[DATA_PREFIX] == 0
10111 && (generate_relax_relocations
10112 || (!object_64bit
10113 && i.rm.mode == 0
10114 && i.rm.regmem == 5))
0cb4071e
L
10115 && (i.rm.mode == 2
10116 || (i.rm.mode == 0 && i.rm.regmem == 5))
389d00a5 10117 && i.tm.opcode_modifier.opcodespace == SPACE_BASE
02a86693
L
10118 && ((i.operands == 1
10119 && i.tm.base_opcode == 0xff
10120 && (i.rm.reg == 2 || i.rm.reg == 4))
10121 || (i.operands == 2
10122 && (i.tm.base_opcode == 0x8b
10123 || i.tm.base_opcode == 0x85
2ae4c703 10124 || (i.tm.base_opcode & ~0x38) == 0x03))))
02a86693
L
10125 {
10126 if (object_64bit)
10127 {
10128 fixP->fx_tcbit = i.rex != 0;
10129 if (i.base_reg
e968fc9b 10130 && (i.base_reg->reg_num == RegIP))
02a86693
L
10131 fixP->fx_tcbit2 = 1;
10132 }
10133 else
10134 fixP->fx_tcbit2 = 1;
10135 }
29b0f896
AM
10136 }
10137 }
10138 }
10139}
252b5132 10140
29b0f896 10141static void
64e74474 10142output_imm (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
10143{
10144 char *p;
10145 unsigned int n;
252b5132 10146
29b0f896
AM
10147 for (n = 0; n < i.operands; n++)
10148 {
40fb9820 10149 if (operand_type_check (i.types[n], imm))
29b0f896 10150 {
48ef937e
JB
10151 int size = imm_size (n);
10152
10153 if (now_seg == absolute_section)
10154 abs_section_offset += size;
10155 else if (i.op[n].imms->X_op == O_constant)
29b0f896 10156 {
29b0f896 10157 offsetT val;
b4cac588 10158
29b0f896
AM
10159 val = offset_in_range (i.op[n].imms->X_add_number,
10160 size);
10161 p = frag_more (size);
10162 md_number_to_chars (p, val, size);
10163 }
10164 else
10165 {
10166 /* Not absolute_section.
10167 Need a 32-bit fixup (don't support 8bit
10168 non-absolute imms). Try to support other
10169 sizes ... */
f86103b7 10170 enum bfd_reloc_code_real reloc_type;
e205caa7 10171 int sign;
29b0f896 10172
40fb9820 10173 if (i.types[n].bitfield.imm32s
a7d61044 10174 && (i.suffix == QWORD_MNEM_SUFFIX
40fb9820 10175 || (!i.suffix && i.tm.opcode_modifier.no_lsuf)))
29b0f896 10176 sign = 1;
e205caa7
L
10177 else
10178 sign = 0;
520dc8e8 10179
29b0f896 10180 p = frag_more (size);
d258b828 10181 reloc_type = reloc (size, 0, sign, i.reloc[n]);
f86103b7 10182
2bbd9c25
JJ
10183 /* This is tough to explain. We end up with this one if we
10184 * have operands that look like
10185 * "_GLOBAL_OFFSET_TABLE_+[.-.L284]". The goal here is to
10186 * obtain the absolute address of the GOT, and it is strongly
10187 * preferable from a performance point of view to avoid using
10188 * a runtime relocation for this. The actual sequence of
10189 * instructions often look something like:
10190 *
10191 * call .L66
10192 * .L66:
10193 * popl %ebx
10194 * addl $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
10195 *
10196 * The call and pop essentially return the absolute address
10197 * of the label .L66 and store it in %ebx. The linker itself
10198 * will ultimately change the first operand of the addl so
10199 * that %ebx points to the GOT, but to keep things simple, the
10200 * .o file must have this operand set so that it generates not
10201 * the absolute address of .L66, but the absolute address of
10202 * itself. This allows the linker itself simply treat a GOTPC
10203 * relocation as asking for a pcrel offset to the GOT to be
10204 * added in, and the addend of the relocation is stored in the
10205 * operand field for the instruction itself.
10206 *
10207 * Our job here is to fix the operand so that it would add
10208 * the correct offset so that %ebx would point to itself. The
10209 * thing that is tricky is that .-.L66 will point to the
10210 * beginning of the instruction, so we need to further modify
10211 * the operand so that it will point to itself. There are
10212 * other cases where you have something like:
10213 *
10214 * .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
10215 *
10216 * and here no correction would be required. Internally in
10217 * the assembler we treat operands of this form as not being
10218 * pcrel since the '.' is explicitly mentioned, and I wonder
10219 * whether it would simplify matters to do it this way. Who
10220 * knows. In earlier versions of the PIC patches, the
10221 * pcrel_adjust field was used to store the correction, but
10222 * since the expression is not pcrel, I felt it would be
10223 * confusing to do it this way. */
10224
d6ab8113 10225 if ((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
10226 || reloc_type == BFD_RELOC_X86_64_32S
10227 || reloc_type == BFD_RELOC_64)
29b0f896
AM
10228 && GOT_symbol
10229 && GOT_symbol == i.op[n].imms->X_add_symbol
10230 && (i.op[n].imms->X_op == O_symbol
10231 || (i.op[n].imms->X_op == O_add
10232 && ((symbol_get_value_expression
10233 (i.op[n].imms->X_op_symbol)->X_op)
10234 == O_subtract))))
10235 {
4fa24527 10236 if (!object_64bit)
d6ab8113 10237 reloc_type = BFD_RELOC_386_GOTPC;
7b81dfbb 10238 else if (size == 4)
d6ab8113 10239 reloc_type = BFD_RELOC_X86_64_GOTPC32;
7b81dfbb
AJ
10240 else if (size == 8)
10241 reloc_type = BFD_RELOC_X86_64_GOTPC64;
5b7c81bd 10242 i.has_gotpc_tls_reloc = true;
d583596c
JB
10243 i.op[n].imms->X_add_number +=
10244 encoding_length (insn_start_frag, insn_start_off, p);
29b0f896 10245 }
29b0f896
AM
10246 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
10247 i.op[n].imms, 0, reloc_type);
10248 }
10249 }
10250 }
252b5132
RH
10251}
10252\f
d182319b
JB
10253/* x86_cons_fix_new is called via the expression parsing code when a
10254 reloc is needed. We use this hook to get the correct .got reloc. */
d182319b
JB
10255static int cons_sign = -1;
10256
10257void
e3bb37b5 10258x86_cons_fix_new (fragS *frag, unsigned int off, unsigned int len,
62ebcb5c 10259 expressionS *exp, bfd_reloc_code_real_type r)
d182319b 10260{
d258b828 10261 r = reloc (len, 0, cons_sign, r);
d182319b
JB
10262
10263#ifdef TE_PE
10264 if (exp->X_op == O_secrel)
10265 {
10266 exp->X_op = O_symbol;
10267 r = BFD_RELOC_32_SECREL;
10268 }
145667f8
MH
10269 else if (exp->X_op == O_secidx)
10270 r = BFD_RELOC_16_SECIDX;
d182319b
JB
10271#endif
10272
10273 fix_new_exp (frag, off, len, exp, 0, r);
10274}
10275
357d1bd8
L
10276/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
10277 purpose of the `.dc.a' internal pseudo-op. */
10278
10279int
10280x86_address_bytes (void)
10281{
10282 if ((stdoutput->arch_info->mach & bfd_mach_x64_32))
10283 return 4;
10284 return stdoutput->arch_info->bits_per_address / 8;
10285}
10286
deea4973
JB
10287#if (!(defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined (OBJ_MACH_O)) \
10288 || defined (LEX_AT)) && !defined (TE_PE)
d258b828 10289# define lex_got(reloc, adjust, types) NULL
718ddfc0 10290#else
f3c180ae
AM
10291/* Parse operands of the form
10292 <symbol>@GOTOFF+<nnn>
10293 and similar .plt or .got references.
10294
10295 If we find one, set up the correct relocation in RELOC and copy the
10296 input string, minus the `@GOTOFF' into a malloc'd buffer for
10297 parsing by the calling routine. Return this buffer, and if ADJUST
10298 is non-null set it to the length of the string we removed from the
10299 input line. Otherwise return NULL. */
10300static char *
91d6fa6a 10301lex_got (enum bfd_reloc_code_real *rel,
64e74474 10302 int *adjust,
d258b828 10303 i386_operand_type *types)
f3c180ae 10304{
7b81dfbb
AJ
10305 /* Some of the relocations depend on the size of what field is to
10306 be relocated. But in our callers i386_immediate and i386_displacement
10307 we don't yet know the operand size (this will be set by insn
10308 matching). Hence we record the word32 relocation here,
10309 and adjust the reloc according to the real size in reloc(). */
145667f8
MH
10310 static const struct
10311 {
f3c180ae 10312 const char *str;
cff8d58a 10313 int len;
4fa24527 10314 const enum bfd_reloc_code_real rel[2];
40fb9820 10315 const i386_operand_type types64;
5b7c81bd 10316 bool need_GOT_symbol;
145667f8
MH
10317 }
10318 gotrel[] =
10319 {
deea4973 10320#ifndef TE_PE
8ce3d284 10321#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
10322 { STRING_COMMA_LEN ("SIZE"), { BFD_RELOC_SIZE32,
10323 BFD_RELOC_SIZE32 },
5b7c81bd 10324 OPERAND_TYPE_IMM32_64, false },
8ce3d284 10325#endif
cff8d58a
L
10326 { STRING_COMMA_LEN ("PLTOFF"), { _dummy_first_bfd_reloc_code_real,
10327 BFD_RELOC_X86_64_PLTOFF64 },
5b7c81bd 10328 OPERAND_TYPE_IMM64, true },
cff8d58a
L
10329 { STRING_COMMA_LEN ("PLT"), { BFD_RELOC_386_PLT32,
10330 BFD_RELOC_X86_64_PLT32 },
a775efc8 10331 OPERAND_TYPE_IMM32_32S_DISP32, false },
cff8d58a
L
10332 { STRING_COMMA_LEN ("GOTPLT"), { _dummy_first_bfd_reloc_code_real,
10333 BFD_RELOC_X86_64_GOTPLT64 },
5b7c81bd 10334 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
10335 { STRING_COMMA_LEN ("GOTOFF"), { BFD_RELOC_386_GOTOFF,
10336 BFD_RELOC_X86_64_GOTOFF64 },
5b7c81bd 10337 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
10338 { STRING_COMMA_LEN ("GOTPCREL"), { _dummy_first_bfd_reloc_code_real,
10339 BFD_RELOC_X86_64_GOTPCREL },
a775efc8 10340 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10341 { STRING_COMMA_LEN ("TLSGD"), { BFD_RELOC_386_TLS_GD,
10342 BFD_RELOC_X86_64_TLSGD },
a775efc8 10343 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10344 { STRING_COMMA_LEN ("TLSLDM"), { BFD_RELOC_386_TLS_LDM,
10345 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10346 OPERAND_TYPE_NONE, true },
cff8d58a
L
10347 { STRING_COMMA_LEN ("TLSLD"), { _dummy_first_bfd_reloc_code_real,
10348 BFD_RELOC_X86_64_TLSLD },
a775efc8 10349 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10350 { STRING_COMMA_LEN ("GOTTPOFF"), { BFD_RELOC_386_TLS_IE_32,
10351 BFD_RELOC_X86_64_GOTTPOFF },
a775efc8 10352 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10353 { STRING_COMMA_LEN ("TPOFF"), { BFD_RELOC_386_TLS_LE_32,
10354 BFD_RELOC_X86_64_TPOFF32 },
a775efc8 10355 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
10356 { STRING_COMMA_LEN ("NTPOFF"), { BFD_RELOC_386_TLS_LE,
10357 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10358 OPERAND_TYPE_NONE, true },
cff8d58a
L
10359 { STRING_COMMA_LEN ("DTPOFF"), { BFD_RELOC_386_TLS_LDO_32,
10360 BFD_RELOC_X86_64_DTPOFF32 },
a775efc8 10361 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
10362 { STRING_COMMA_LEN ("GOTNTPOFF"),{ BFD_RELOC_386_TLS_GOTIE,
10363 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10364 OPERAND_TYPE_NONE, true },
cff8d58a
L
10365 { STRING_COMMA_LEN ("INDNTPOFF"),{ BFD_RELOC_386_TLS_IE,
10366 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10367 OPERAND_TYPE_NONE, true },
cff8d58a
L
10368 { STRING_COMMA_LEN ("GOT"), { BFD_RELOC_386_GOT32,
10369 BFD_RELOC_X86_64_GOT32 },
a775efc8 10370 OPERAND_TYPE_IMM32_32S_64_DISP32, true },
cff8d58a
L
10371 { STRING_COMMA_LEN ("TLSDESC"), { BFD_RELOC_386_TLS_GOTDESC,
10372 BFD_RELOC_X86_64_GOTPC32_TLSDESC },
a775efc8 10373 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10374 { STRING_COMMA_LEN ("TLSCALL"), { BFD_RELOC_386_TLS_DESC_CALL,
10375 BFD_RELOC_X86_64_TLSDESC_CALL },
a775efc8 10376 OPERAND_TYPE_IMM32_32S_DISP32, true },
deea4973
JB
10377#else /* TE_PE */
10378 { STRING_COMMA_LEN ("SECREL32"), { BFD_RELOC_32_SECREL,
10379 BFD_RELOC_32_SECREL },
a775efc8 10380 OPERAND_TYPE_IMM32_32S_64_DISP32_64, false },
deea4973 10381#endif
f3c180ae
AM
10382 };
10383 char *cp;
10384 unsigned int j;
10385
deea4973 10386#if defined (OBJ_MAYBE_ELF) && !defined (TE_PE)
718ddfc0
JB
10387 if (!IS_ELF)
10388 return NULL;
d382c579 10389#endif
718ddfc0 10390
f3c180ae 10391 for (cp = input_line_pointer; *cp != '@'; cp++)
67c11a9b 10392 if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
f3c180ae
AM
10393 return NULL;
10394
47465058 10395 for (j = 0; j < ARRAY_SIZE (gotrel); j++)
f3c180ae 10396 {
cff8d58a 10397 int len = gotrel[j].len;
28f81592 10398 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
f3c180ae 10399 {
4fa24527 10400 if (gotrel[j].rel[object_64bit] != 0)
f3c180ae 10401 {
28f81592
AM
10402 int first, second;
10403 char *tmpbuf, *past_reloc;
f3c180ae 10404
91d6fa6a 10405 *rel = gotrel[j].rel[object_64bit];
f3c180ae 10406
3956db08
JB
10407 if (types)
10408 {
10409 if (flag_code != CODE_64BIT)
40fb9820
L
10410 {
10411 types->bitfield.imm32 = 1;
10412 types->bitfield.disp32 = 1;
10413 }
3956db08
JB
10414 else
10415 *types = gotrel[j].types64;
10416 }
10417
844bf810 10418 if (gotrel[j].need_GOT_symbol && GOT_symbol == NULL)
f3c180ae
AM
10419 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
10420
28f81592 10421 /* The length of the first part of our input line. */
f3c180ae 10422 first = cp - input_line_pointer;
28f81592
AM
10423
10424 /* The second part goes from after the reloc token until
67c11a9b 10425 (and including) an end_of_line char or comma. */
28f81592 10426 past_reloc = cp + 1 + len;
67c11a9b
AM
10427 cp = past_reloc;
10428 while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
10429 ++cp;
10430 second = cp + 1 - past_reloc;
28f81592
AM
10431
10432 /* Allocate and copy string. The trailing NUL shouldn't
10433 be necessary, but be safe. */
add39d23 10434 tmpbuf = XNEWVEC (char, first + second + 2);
f3c180ae 10435 memcpy (tmpbuf, input_line_pointer, first);
0787a12d
AM
10436 if (second != 0 && *past_reloc != ' ')
10437 /* Replace the relocation token with ' ', so that
10438 errors like foo@GOTOFF1 will be detected. */
10439 tmpbuf[first++] = ' ';
af89796a
L
10440 else
10441 /* Increment length by 1 if the relocation token is
10442 removed. */
10443 len++;
10444 if (adjust)
10445 *adjust = len;
0787a12d
AM
10446 memcpy (tmpbuf + first, past_reloc, second);
10447 tmpbuf[first + second] = '\0';
f3c180ae
AM
10448 return tmpbuf;
10449 }
10450
4fa24527
JB
10451 as_bad (_("@%s reloc is not supported with %d-bit output format"),
10452 gotrel[j].str, 1 << (5 + object_64bit));
f3c180ae
AM
10453 return NULL;
10454 }
10455 }
10456
10457 /* Might be a symbol version string. Don't as_bad here. */
10458 return NULL;
10459}
4e4f7c87 10460#endif
f3c180ae 10461
62ebcb5c 10462bfd_reloc_code_real_type
e3bb37b5 10463x86_cons (expressionS *exp, int size)
f3c180ae 10464{
62ebcb5c
AM
10465 bfd_reloc_code_real_type got_reloc = NO_RELOC;
10466
2748c1b1
L
10467#if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
10468 && !defined (LEX_AT)) \
10469 || defined (TE_PE)
ee86248c
JB
10470 intel_syntax = -intel_syntax;
10471
3c7b9c2c 10472 exp->X_md = 0;
4fa24527 10473 if (size == 4 || (object_64bit && size == 8))
f3c180ae
AM
10474 {
10475 /* Handle @GOTOFF and the like in an expression. */
10476 char *save;
10477 char *gotfree_input_line;
4a57f2cf 10478 int adjust = 0;
f3c180ae
AM
10479
10480 save = input_line_pointer;
d258b828 10481 gotfree_input_line = lex_got (&got_reloc, &adjust, NULL);
f3c180ae
AM
10482 if (gotfree_input_line)
10483 input_line_pointer = gotfree_input_line;
10484
10485 expression (exp);
10486
10487 if (gotfree_input_line)
10488 {
10489 /* expression () has merrily parsed up to the end of line,
10490 or a comma - in the wrong buffer. Transfer how far
10491 input_line_pointer has moved to the right buffer. */
10492 input_line_pointer = (save
10493 + (input_line_pointer - gotfree_input_line)
10494 + adjust);
10495 free (gotfree_input_line);
3992d3b7
AM
10496 if (exp->X_op == O_constant
10497 || exp->X_op == O_absent
10498 || exp->X_op == O_illegal
0398aac5 10499 || exp->X_op == O_register
3992d3b7
AM
10500 || exp->X_op == O_big)
10501 {
10502 char c = *input_line_pointer;
10503 *input_line_pointer = 0;
10504 as_bad (_("missing or invalid expression `%s'"), save);
10505 *input_line_pointer = c;
10506 }
b9519cfe
L
10507 else if ((got_reloc == BFD_RELOC_386_PLT32
10508 || got_reloc == BFD_RELOC_X86_64_PLT32)
10509 && exp->X_op != O_symbol)
10510 {
10511 char c = *input_line_pointer;
10512 *input_line_pointer = 0;
10513 as_bad (_("invalid PLT expression `%s'"), save);
10514 *input_line_pointer = c;
10515 }
f3c180ae
AM
10516 }
10517 }
10518 else
10519 expression (exp);
ee86248c
JB
10520
10521 intel_syntax = -intel_syntax;
10522
10523 if (intel_syntax)
10524 i386_intel_simplify (exp);
2748c1b1
L
10525#else
10526 expression (exp);
10527#endif
62ebcb5c 10528
a442cac5
JB
10529 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
10530 if (size == 4 && exp->X_op == O_constant && !object_64bit)
10531 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
10532
62ebcb5c 10533 return got_reloc;
f3c180ae 10534}
f3c180ae 10535
9f32dd5b
L
10536static void
10537signed_cons (int size)
6482c264 10538{
a442cac5 10539 if (object_64bit)
d182319b
JB
10540 cons_sign = 1;
10541 cons (size);
10542 cons_sign = -1;
6482c264
NC
10543}
10544
d182319b 10545#ifdef TE_PE
6482c264 10546static void
7016a5d5 10547pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
6482c264
NC
10548{
10549 expressionS exp;
10550
10551 do
10552 {
10553 expression (&exp);
10554 if (exp.X_op == O_symbol)
10555 exp.X_op = O_secrel;
10556
10557 emit_expr (&exp, 4);
10558 }
10559 while (*input_line_pointer++ == ',');
10560
10561 input_line_pointer--;
10562 demand_empty_rest_of_line ();
10563}
145667f8
MH
10564
10565static void
10566pe_directive_secidx (int dummy ATTRIBUTE_UNUSED)
10567{
10568 expressionS exp;
10569
10570 do
10571 {
10572 expression (&exp);
10573 if (exp.X_op == O_symbol)
10574 exp.X_op = O_secidx;
10575
10576 emit_expr (&exp, 2);
10577 }
10578 while (*input_line_pointer++ == ',');
10579
10580 input_line_pointer--;
10581 demand_empty_rest_of_line ();
10582}
6482c264
NC
10583#endif
10584
7063667e
JB
10585/* Handle Rounding Control / SAE specifiers. */
10586
10587static char *
10588RC_SAE_specifier (const char *pstr)
10589{
10590 unsigned int j;
10591
10592 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); j++)
10593 {
10594 if (!strncmp (pstr, RC_NamesTable[j].name, RC_NamesTable[j].len))
10595 {
10596 if (i.rounding.type != rc_none)
10597 {
10598 as_bad (_("duplicated `{%s}'"), RC_NamesTable[j].name);
10599 return NULL;
10600 }
10601
10602 i.rounding.type = RC_NamesTable[j].type;
10603
10604 return (char *)(pstr + RC_NamesTable[j].len);
10605 }
10606 }
10607
10608 return NULL;
10609}
10610
43234a1e
L
10611/* Handle Vector operations. */
10612
10613static char *
f70c6814 10614check_VecOperations (char *op_string)
43234a1e
L
10615{
10616 const reg_entry *mask;
10617 const char *saved;
10618 char *end_op;
10619
f70c6814 10620 while (*op_string)
43234a1e
L
10621 {
10622 saved = op_string;
10623 if (*op_string == '{')
10624 {
10625 op_string++;
10626
10627 /* Check broadcasts. */
d34049e8 10628 if (startswith (op_string, "1to"))
43234a1e 10629 {
5273a3cd 10630 unsigned int bcst_type;
43234a1e 10631
5273a3cd 10632 if (i.broadcast.type)
43234a1e
L
10633 goto duplicated_vec_op;
10634
10635 op_string += 3;
10636 if (*op_string == '8')
8e6e0792 10637 bcst_type = 8;
b28d1bda 10638 else if (*op_string == '4')
8e6e0792 10639 bcst_type = 4;
b28d1bda 10640 else if (*op_string == '2')
8e6e0792 10641 bcst_type = 2;
43234a1e
L
10642 else if (*op_string == '1'
10643 && *(op_string+1) == '6')
10644 {
8e6e0792 10645 bcst_type = 16;
43234a1e
L
10646 op_string++;
10647 }
0cc78721
CL
10648 else if (*op_string == '3'
10649 && *(op_string+1) == '2')
10650 {
10651 bcst_type = 32;
10652 op_string++;
10653 }
43234a1e
L
10654 else
10655 {
10656 as_bad (_("Unsupported broadcast: `%s'"), saved);
10657 return NULL;
10658 }
10659 op_string++;
10660
5273a3cd
JB
10661 i.broadcast.type = bcst_type;
10662 i.broadcast.operand = this_operand;
43234a1e
L
10663 }
10664 /* Check masking operation. */
10665 else if ((mask = parse_register (op_string, &end_op)) != NULL)
10666 {
8a6fb3f9
JB
10667 if (mask == &bad_reg)
10668 return NULL;
10669
43234a1e 10670 /* k0 can't be used for write mask. */
f74a6307 10671 if (mask->reg_type.bitfield.class != RegMask || !mask->reg_num)
43234a1e 10672 {
6d2cd6b2
JB
10673 as_bad (_("`%s%s' can't be used for write mask"),
10674 register_prefix, mask->reg_name);
43234a1e
L
10675 return NULL;
10676 }
10677
6225c532 10678 if (!i.mask.reg)
43234a1e 10679 {
6225c532
JB
10680 i.mask.reg = mask;
10681 i.mask.operand = this_operand;
43234a1e 10682 }
6225c532
JB
10683 else if (i.mask.reg->reg_num)
10684 goto duplicated_vec_op;
43234a1e
L
10685 else
10686 {
6225c532 10687 i.mask.reg = mask;
43234a1e
L
10688
10689 /* Only "{z}" is allowed here. No need to check
10690 zeroing mask explicitly. */
6225c532 10691 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
10692 {
10693 as_bad (_("invalid write mask `%s'"), saved);
10694 return NULL;
10695 }
10696 }
10697
10698 op_string = end_op;
10699 }
10700 /* Check zeroing-flag for masking operation. */
10701 else if (*op_string == 'z')
10702 {
6225c532 10703 if (!i.mask.reg)
43234a1e 10704 {
6225c532
JB
10705 i.mask.reg = reg_k0;
10706 i.mask.zeroing = 1;
10707 i.mask.operand = this_operand;
43234a1e
L
10708 }
10709 else
10710 {
6225c532 10711 if (i.mask.zeroing)
43234a1e
L
10712 {
10713 duplicated_vec_op:
10714 as_bad (_("duplicated `%s'"), saved);
10715 return NULL;
10716 }
10717
6225c532 10718 i.mask.zeroing = 1;
43234a1e
L
10719
10720 /* Only "{%k}" is allowed here. No need to check mask
10721 register explicitly. */
6225c532 10722 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
10723 {
10724 as_bad (_("invalid zeroing-masking `%s'"),
10725 saved);
10726 return NULL;
10727 }
10728 }
10729
10730 op_string++;
10731 }
7063667e
JB
10732 else if (intel_syntax
10733 && (op_string = RC_SAE_specifier (op_string)) != NULL)
10734 i.rounding.modifier = true;
43234a1e
L
10735 else
10736 goto unknown_vec_op;
10737
10738 if (*op_string != '}')
10739 {
10740 as_bad (_("missing `}' in `%s'"), saved);
10741 return NULL;
10742 }
10743 op_string++;
0ba3a731
L
10744
10745 /* Strip whitespace since the addition of pseudo prefixes
10746 changed how the scrubber treats '{'. */
10747 if (is_space_char (*op_string))
10748 ++op_string;
10749
43234a1e
L
10750 continue;
10751 }
10752 unknown_vec_op:
10753 /* We don't know this one. */
10754 as_bad (_("unknown vector operation: `%s'"), saved);
10755 return NULL;
10756 }
10757
6225c532 10758 if (i.mask.reg && i.mask.zeroing && !i.mask.reg->reg_num)
6d2cd6b2
JB
10759 {
10760 as_bad (_("zeroing-masking only allowed with write mask"));
10761 return NULL;
10762 }
10763
43234a1e
L
10764 return op_string;
10765}
10766
252b5132 10767static int
70e41ade 10768i386_immediate (char *imm_start)
252b5132
RH
10769{
10770 char *save_input_line_pointer;
f3c180ae 10771 char *gotfree_input_line;
252b5132 10772 segT exp_seg = 0;
47926f60 10773 expressionS *exp;
40fb9820
L
10774 i386_operand_type types;
10775
0dfbf9d7 10776 operand_type_set (&types, ~0);
252b5132
RH
10777
10778 if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
10779 {
31b2323c
L
10780 as_bad (_("at most %d immediate operands are allowed"),
10781 MAX_IMMEDIATE_OPERANDS);
252b5132
RH
10782 return 0;
10783 }
10784
10785 exp = &im_expressions[i.imm_operands++];
520dc8e8 10786 i.op[this_operand].imms = exp;
252b5132
RH
10787
10788 if (is_space_char (*imm_start))
10789 ++imm_start;
10790
10791 save_input_line_pointer = input_line_pointer;
10792 input_line_pointer = imm_start;
10793
d258b828 10794 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
10795 if (gotfree_input_line)
10796 input_line_pointer = gotfree_input_line;
252b5132
RH
10797
10798 exp_seg = expression (exp);
10799
83183c0c 10800 SKIP_WHITESPACE ();
252b5132 10801 if (*input_line_pointer)
f3c180ae 10802 as_bad (_("junk `%s' after expression"), input_line_pointer);
252b5132
RH
10803
10804 input_line_pointer = save_input_line_pointer;
f3c180ae 10805 if (gotfree_input_line)
ee86248c
JB
10806 {
10807 free (gotfree_input_line);
10808
9aac24b1 10809 if (exp->X_op == O_constant)
ee86248c
JB
10810 exp->X_op = O_illegal;
10811 }
10812
9aac24b1
JB
10813 if (exp_seg == reg_section)
10814 {
10815 as_bad (_("illegal immediate register operand %s"), imm_start);
10816 return 0;
10817 }
10818
ee86248c
JB
10819 return i386_finalize_immediate (exp_seg, exp, types, imm_start);
10820}
252b5132 10821
ee86248c
JB
10822static int
10823i386_finalize_immediate (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
10824 i386_operand_type types, const char *imm_start)
10825{
10826 if (exp->X_op == O_absent || exp->X_op == O_illegal || exp->X_op == O_big)
252b5132 10827 {
313c53d1
L
10828 if (imm_start)
10829 as_bad (_("missing or invalid immediate expression `%s'"),
10830 imm_start);
3992d3b7 10831 return 0;
252b5132 10832 }
3e73aa7c 10833 else if (exp->X_op == O_constant)
252b5132 10834 {
47926f60 10835 /* Size it properly later. */
40fb9820 10836 i.types[this_operand].bitfield.imm64 = 1;
a442cac5
JB
10837
10838 /* If not 64bit, sign/zero extend val, to account for wraparound
10839 when !BFD64. */
10840 if (flag_code != CODE_64BIT)
10841 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
252b5132 10842 }
4c63da97 10843#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
f86103b7 10844 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
31312f95 10845 && exp_seg != absolute_section
47926f60 10846 && exp_seg != text_section
24eab124
AM
10847 && exp_seg != data_section
10848 && exp_seg != bss_section
10849 && exp_seg != undefined_section
f86103b7 10850 && !bfd_is_com_section (exp_seg))
252b5132 10851 {
d0b47220 10852 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
252b5132
RH
10853 return 0;
10854 }
10855#endif
10856 else
10857 {
10858 /* This is an address. The size of the address will be
24eab124 10859 determined later, depending on destination register,
3e73aa7c 10860 suffix, or the default for the section. */
40fb9820
L
10861 i.types[this_operand].bitfield.imm8 = 1;
10862 i.types[this_operand].bitfield.imm16 = 1;
10863 i.types[this_operand].bitfield.imm32 = 1;
10864 i.types[this_operand].bitfield.imm32s = 1;
10865 i.types[this_operand].bitfield.imm64 = 1;
c6fb90c8
L
10866 i.types[this_operand] = operand_type_and (i.types[this_operand],
10867 types);
252b5132
RH
10868 }
10869
10870 return 1;
10871}
10872
551c1ca1 10873static char *
e3bb37b5 10874i386_scale (char *scale)
252b5132 10875{
551c1ca1
AM
10876 offsetT val;
10877 char *save = input_line_pointer;
252b5132 10878
551c1ca1
AM
10879 input_line_pointer = scale;
10880 val = get_absolute_expression ();
10881
10882 switch (val)
252b5132 10883 {
551c1ca1 10884 case 1:
252b5132
RH
10885 i.log2_scale_factor = 0;
10886 break;
551c1ca1 10887 case 2:
252b5132
RH
10888 i.log2_scale_factor = 1;
10889 break;
551c1ca1 10890 case 4:
252b5132
RH
10891 i.log2_scale_factor = 2;
10892 break;
551c1ca1 10893 case 8:
252b5132
RH
10894 i.log2_scale_factor = 3;
10895 break;
10896 default:
a724f0f4
JB
10897 {
10898 char sep = *input_line_pointer;
10899
10900 *input_line_pointer = '\0';
10901 as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
10902 scale);
10903 *input_line_pointer = sep;
10904 input_line_pointer = save;
10905 return NULL;
10906 }
252b5132 10907 }
29b0f896 10908 if (i.log2_scale_factor != 0 && i.index_reg == 0)
252b5132
RH
10909 {
10910 as_warn (_("scale factor of %d without an index register"),
24eab124 10911 1 << i.log2_scale_factor);
252b5132 10912 i.log2_scale_factor = 0;
252b5132 10913 }
551c1ca1
AM
10914 scale = input_line_pointer;
10915 input_line_pointer = save;
10916 return scale;
252b5132
RH
10917}
10918
252b5132 10919static int
e3bb37b5 10920i386_displacement (char *disp_start, char *disp_end)
252b5132 10921{
29b0f896 10922 expressionS *exp;
252b5132
RH
10923 segT exp_seg = 0;
10924 char *save_input_line_pointer;
f3c180ae 10925 char *gotfree_input_line;
40fb9820
L
10926 int override;
10927 i386_operand_type bigdisp, types = anydisp;
3992d3b7 10928 int ret;
252b5132 10929
31b2323c
L
10930 if (i.disp_operands == MAX_MEMORY_OPERANDS)
10931 {
10932 as_bad (_("at most %d displacement operands are allowed"),
10933 MAX_MEMORY_OPERANDS);
10934 return 0;
10935 }
10936
0dfbf9d7 10937 operand_type_set (&bigdisp, 0);
6f2f06be 10938 if (i.jumpabsolute
48bcea9f 10939 || i.types[this_operand].bitfield.baseindex
0cfa3eb3
JB
10940 || (current_templates->start->opcode_modifier.jump != JUMP
10941 && current_templates->start->opcode_modifier.jump != JUMP_DWORD))
e05278af 10942 {
48bcea9f 10943 i386_addressing_mode ();
e05278af 10944 override = (i.prefix[ADDR_PREFIX] != 0);
40fb9820
L
10945 if (flag_code == CODE_64BIT)
10946 {
a775efc8 10947 bigdisp.bitfield.disp32 = 1;
40fb9820 10948 if (!override)
a775efc8 10949 bigdisp.bitfield.disp64 = 1;
40fb9820
L
10950 }
10951 else if ((flag_code == CODE_16BIT) ^ override)
40fb9820 10952 bigdisp.bitfield.disp16 = 1;
48bcea9f
JB
10953 else
10954 bigdisp.bitfield.disp32 = 1;
e05278af
JB
10955 }
10956 else
10957 {
376cd056
JB
10958 /* For PC-relative branches, the width of the displacement may be
10959 dependent upon data size, but is never dependent upon address size.
10960 Also make sure to not unintentionally match against a non-PC-relative
10961 branch template. */
10962 static templates aux_templates;
10963 const insn_template *t = current_templates->start;
5b7c81bd 10964 bool has_intel64 = false;
376cd056
JB
10965
10966 aux_templates.start = t;
10967 while (++t < current_templates->end)
10968 {
10969 if (t->opcode_modifier.jump
10970 != current_templates->start->opcode_modifier.jump)
10971 break;
4b5aaf5f 10972 if ((t->opcode_modifier.isa64 >= INTEL64))
5b7c81bd 10973 has_intel64 = true;
376cd056
JB
10974 }
10975 if (t < current_templates->end)
10976 {
10977 aux_templates.end = t;
10978 current_templates = &aux_templates;
10979 }
10980
e05278af 10981 override = (i.prefix[DATA_PREFIX] != 0);
40fb9820
L
10982 if (flag_code == CODE_64BIT)
10983 {
376cd056
JB
10984 if ((override || i.suffix == WORD_MNEM_SUFFIX)
10985 && (!intel64 || !has_intel64))
40fb9820
L
10986 bigdisp.bitfield.disp16 = 1;
10987 else
a775efc8 10988 bigdisp.bitfield.disp32 = 1;
40fb9820
L
10989 }
10990 else
e05278af
JB
10991 {
10992 if (!override)
10993 override = (i.suffix == (flag_code != CODE_16BIT
10994 ? WORD_MNEM_SUFFIX
10995 : LONG_MNEM_SUFFIX));
40fb9820
L
10996 bigdisp.bitfield.disp32 = 1;
10997 if ((flag_code == CODE_16BIT) ^ override)
10998 {
10999 bigdisp.bitfield.disp32 = 0;
11000 bigdisp.bitfield.disp16 = 1;
11001 }
e05278af 11002 }
e05278af 11003 }
c6fb90c8
L
11004 i.types[this_operand] = operand_type_or (i.types[this_operand],
11005 bigdisp);
252b5132
RH
11006
11007 exp = &disp_expressions[i.disp_operands];
520dc8e8 11008 i.op[this_operand].disps = exp;
252b5132
RH
11009 i.disp_operands++;
11010 save_input_line_pointer = input_line_pointer;
11011 input_line_pointer = disp_start;
11012 END_STRING_AND_SAVE (disp_end);
11013
11014#ifndef GCC_ASM_O_HACK
11015#define GCC_ASM_O_HACK 0
11016#endif
11017#if GCC_ASM_O_HACK
11018 END_STRING_AND_SAVE (disp_end + 1);
40fb9820 11019 if (i.types[this_operand].bitfield.baseIndex
24eab124 11020 && displacement_string_end[-1] == '+')
252b5132
RH
11021 {
11022 /* This hack is to avoid a warning when using the "o"
24eab124
AM
11023 constraint within gcc asm statements.
11024 For instance:
11025
11026 #define _set_tssldt_desc(n,addr,limit,type) \
11027 __asm__ __volatile__ ( \
11028 "movw %w2,%0\n\t" \
11029 "movw %w1,2+%0\n\t" \
11030 "rorl $16,%1\n\t" \
11031 "movb %b1,4+%0\n\t" \
11032 "movb %4,5+%0\n\t" \
11033 "movb $0,6+%0\n\t" \
11034 "movb %h1,7+%0\n\t" \
11035 "rorl $16,%1" \
11036 : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
11037
11038 This works great except that the output assembler ends
11039 up looking a bit weird if it turns out that there is
11040 no offset. You end up producing code that looks like:
11041
11042 #APP
11043 movw $235,(%eax)
11044 movw %dx,2+(%eax)
11045 rorl $16,%edx
11046 movb %dl,4+(%eax)
11047 movb $137,5+(%eax)
11048 movb $0,6+(%eax)
11049 movb %dh,7+(%eax)
11050 rorl $16,%edx
11051 #NO_APP
11052
47926f60 11053 So here we provide the missing zero. */
24eab124
AM
11054
11055 *displacement_string_end = '0';
252b5132
RH
11056 }
11057#endif
d258b828 11058 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
11059 if (gotfree_input_line)
11060 input_line_pointer = gotfree_input_line;
252b5132 11061
24eab124 11062 exp_seg = expression (exp);
252b5132 11063
636c26b0
AM
11064 SKIP_WHITESPACE ();
11065 if (*input_line_pointer)
11066 as_bad (_("junk `%s' after expression"), input_line_pointer);
11067#if GCC_ASM_O_HACK
11068 RESTORE_END_STRING (disp_end + 1);
11069#endif
636c26b0 11070 input_line_pointer = save_input_line_pointer;
636c26b0 11071 if (gotfree_input_line)
ee86248c
JB
11072 {
11073 free (gotfree_input_line);
11074
11075 if (exp->X_op == O_constant || exp->X_op == O_register)
11076 exp->X_op = O_illegal;
11077 }
11078
11079 ret = i386_finalize_displacement (exp_seg, exp, types, disp_start);
11080
11081 RESTORE_END_STRING (disp_end);
11082
11083 return ret;
11084}
11085
11086static int
11087i386_finalize_displacement (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
11088 i386_operand_type types, const char *disp_start)
11089{
ee86248c 11090 int ret = 1;
636c26b0 11091
24eab124
AM
11092 /* We do this to make sure that the section symbol is in
11093 the symbol table. We will ultimately change the relocation
47926f60 11094 to be relative to the beginning of the section. */
1ae12ab7 11095 if (i.reloc[this_operand] == BFD_RELOC_386_GOTOFF
d6ab8113
JB
11096 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
11097 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
24eab124 11098 {
636c26b0 11099 if (exp->X_op != O_symbol)
3992d3b7 11100 goto inv_disp;
636c26b0 11101
e5cb08ac 11102 if (S_IS_LOCAL (exp->X_add_symbol)
c64efb4b
L
11103 && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section
11104 && S_GET_SEGMENT (exp->X_add_symbol) != expr_section)
24eab124 11105 section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
24eab124
AM
11106 exp->X_op = O_subtract;
11107 exp->X_op_symbol = GOT_symbol;
1ae12ab7 11108 if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
29b0f896 11109 i.reloc[this_operand] = BFD_RELOC_32_PCREL;
d6ab8113
JB
11110 else if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
11111 i.reloc[this_operand] = BFD_RELOC_64;
23df1078 11112 else
29b0f896 11113 i.reloc[this_operand] = BFD_RELOC_32;
24eab124 11114 }
252b5132 11115
3992d3b7
AM
11116 else if (exp->X_op == O_absent
11117 || exp->X_op == O_illegal
ee86248c 11118 || exp->X_op == O_big)
2daf4fd8 11119 {
3992d3b7
AM
11120 inv_disp:
11121 as_bad (_("missing or invalid displacement expression `%s'"),
2daf4fd8 11122 disp_start);
3992d3b7 11123 ret = 0;
2daf4fd8
AM
11124 }
11125
a50187b2
JB
11126 else if (exp->X_op == O_constant)
11127 {
11128 /* Sizing gets taken care of by optimize_disp().
11129
11130 If not 64bit, sign/zero extend val, to account for wraparound
11131 when !BFD64. */
11132 if (flag_code != CODE_64BIT)
11133 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
11134 }
11135
4c63da97 11136#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
a50187b2 11137 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
3992d3b7
AM
11138 && exp_seg != absolute_section
11139 && exp_seg != text_section
11140 && exp_seg != data_section
11141 && exp_seg != bss_section
11142 && exp_seg != undefined_section
11143 && !bfd_is_com_section (exp_seg))
24eab124 11144 {
d0b47220 11145 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
3992d3b7 11146 ret = 0;
24eab124 11147 }
252b5132 11148#endif
3956db08 11149
a50187b2 11150 else if (current_templates->start->opcode_modifier.jump == JUMP_BYTE)
48bcea9f
JB
11151 i.types[this_operand].bitfield.disp8 = 1;
11152
40fb9820 11153 /* Check if this is a displacement only operand. */
02b83698
JB
11154 if (!i.types[this_operand].bitfield.baseindex)
11155 i.types[this_operand] =
11156 operand_type_or (operand_type_and_not (i.types[this_operand], anydisp),
11157 operand_type_and (i.types[this_operand], types));
3956db08 11158
3992d3b7 11159 return ret;
252b5132
RH
11160}
11161
2abc2bec
JB
11162/* Return the active addressing mode, taking address override and
11163 registers forming the address into consideration. Update the
11164 address override prefix if necessary. */
47926f60 11165
2abc2bec
JB
11166static enum flag_code
11167i386_addressing_mode (void)
252b5132 11168{
be05d201
L
11169 enum flag_code addr_mode;
11170
11171 if (i.prefix[ADDR_PREFIX])
11172 addr_mode = flag_code == CODE_32BIT ? CODE_16BIT : CODE_32BIT;
a23b33b3
JB
11173 else if (flag_code == CODE_16BIT
11174 && current_templates->start->cpu_flags.bitfield.cpumpx
11175 /* Avoid replacing the "16-bit addressing not allowed" diagnostic
11176 from md_assemble() by "is not a valid base/index expression"
11177 when there is a base and/or index. */
11178 && !i.types[this_operand].bitfield.baseindex)
11179 {
11180 /* MPX insn memory operands with neither base nor index must be forced
11181 to use 32-bit addressing in 16-bit mode. */
11182 addr_mode = CODE_32BIT;
11183 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
11184 ++i.prefixes;
11185 gas_assert (!i.types[this_operand].bitfield.disp16);
11186 gas_assert (!i.types[this_operand].bitfield.disp32);
11187 }
be05d201
L
11188 else
11189 {
11190 addr_mode = flag_code;
11191
24eab124 11192#if INFER_ADDR_PREFIX
be05d201
L
11193 if (i.mem_operands == 0)
11194 {
11195 /* Infer address prefix from the first memory operand. */
11196 const reg_entry *addr_reg = i.base_reg;
11197
11198 if (addr_reg == NULL)
11199 addr_reg = i.index_reg;
eecb386c 11200
be05d201
L
11201 if (addr_reg)
11202 {
e968fc9b 11203 if (addr_reg->reg_type.bitfield.dword)
be05d201
L
11204 addr_mode = CODE_32BIT;
11205 else if (flag_code != CODE_64BIT
dc821c5f 11206 && addr_reg->reg_type.bitfield.word)
be05d201
L
11207 addr_mode = CODE_16BIT;
11208
11209 if (addr_mode != flag_code)
11210 {
11211 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
11212 i.prefixes += 1;
11213 /* Change the size of any displacement too. At most one
11214 of Disp16 or Disp32 is set.
11215 FIXME. There doesn't seem to be any real need for
11216 separate Disp16 and Disp32 flags. The same goes for
11217 Imm16 and Imm32. Removing them would probably clean
11218 up the code quite a lot. */
11219 if (flag_code != CODE_64BIT
11220 && (i.types[this_operand].bitfield.disp16
11221 || i.types[this_operand].bitfield.disp32))
11222 i.types[this_operand]
11223 = operand_type_xor (i.types[this_operand], disp16_32);
11224 }
11225 }
11226 }
24eab124 11227#endif
be05d201
L
11228 }
11229
2abc2bec
JB
11230 return addr_mode;
11231}
11232
11233/* Make sure the memory operand we've been dealt is valid.
11234 Return 1 on success, 0 on a failure. */
11235
11236static int
11237i386_index_check (const char *operand_string)
11238{
11239 const char *kind = "base/index";
11240 enum flag_code addr_mode = i386_addressing_mode ();
a152332d 11241 const insn_template *t = current_templates->start;
2abc2bec 11242
a152332d 11243 if (t->opcode_modifier.isstring
fc0763e6
JB
11244 && (current_templates->end[-1].opcode_modifier.isstring
11245 || i.mem_operands))
11246 {
11247 /* Memory operands of string insns are special in that they only allow
11248 a single register (rDI, rSI, or rBX) as their memory address. */
be05d201
L
11249 const reg_entry *expected_reg;
11250 static const char *di_si[][2] =
11251 {
11252 { "esi", "edi" },
11253 { "si", "di" },
11254 { "rsi", "rdi" }
11255 };
11256 static const char *bx[] = { "ebx", "bx", "rbx" };
fc0763e6
JB
11257
11258 kind = "string address";
11259
a152332d 11260 if (t->opcode_modifier.prefixok == PrefixRep)
fc0763e6 11261 {
51c8edf6
JB
11262 int es_op = current_templates->end[-1].opcode_modifier.isstring
11263 - IS_STRING_ES_OP0;
11264 int op = 0;
fc0763e6 11265
51c8edf6 11266 if (!current_templates->end[-1].operand_types[0].bitfield.baseindex
fc0763e6
JB
11267 || ((!i.mem_operands != !intel_syntax)
11268 && current_templates->end[-1].operand_types[1]
11269 .bitfield.baseindex))
51c8edf6 11270 op = 1;
fe0e921f
AM
11271 expected_reg
11272 = (const reg_entry *) str_hash_find (reg_hash,
11273 di_si[addr_mode][op == es_op]);
fc0763e6
JB
11274 }
11275 else
fe0e921f
AM
11276 expected_reg
11277 = (const reg_entry *)str_hash_find (reg_hash, bx[addr_mode]);
fc0763e6 11278
be05d201
L
11279 if (i.base_reg != expected_reg
11280 || i.index_reg
fc0763e6 11281 || operand_type_check (i.types[this_operand], disp))
fc0763e6 11282 {
be05d201
L
11283 /* The second memory operand must have the same size as
11284 the first one. */
11285 if (i.mem_operands
11286 && i.base_reg
11287 && !((addr_mode == CODE_64BIT
dc821c5f 11288 && i.base_reg->reg_type.bitfield.qword)
be05d201 11289 || (addr_mode == CODE_32BIT
dc821c5f
JB
11290 ? i.base_reg->reg_type.bitfield.dword
11291 : i.base_reg->reg_type.bitfield.word)))
be05d201
L
11292 goto bad_address;
11293
fc0763e6
JB
11294 as_warn (_("`%s' is not valid here (expected `%c%s%s%c')"),
11295 operand_string,
11296 intel_syntax ? '[' : '(',
11297 register_prefix,
be05d201 11298 expected_reg->reg_name,
fc0763e6 11299 intel_syntax ? ']' : ')');
be05d201 11300 return 1;
fc0763e6 11301 }
be05d201
L
11302 else
11303 return 1;
11304
dc1e8a47 11305 bad_address:
be05d201
L
11306 as_bad (_("`%s' is not a valid %s expression"),
11307 operand_string, kind);
11308 return 0;
3e73aa7c
JH
11309 }
11310 else
11311 {
be05d201
L
11312 if (addr_mode != CODE_16BIT)
11313 {
11314 /* 32-bit/64-bit checks. */
41eb8e88
L
11315 if (i.disp_encoding == disp_encoding_16bit)
11316 {
11317 bad_disp:
11318 as_bad (_("invalid `%s' prefix"),
11319 addr_mode == CODE_16BIT ? "{disp32}" : "{disp16}");
11320 return 0;
11321 }
11322
be05d201 11323 if ((i.base_reg
e968fc9b
JB
11324 && ((addr_mode == CODE_64BIT
11325 ? !i.base_reg->reg_type.bitfield.qword
11326 : !i.base_reg->reg_type.bitfield.dword)
11327 || (i.index_reg && i.base_reg->reg_num == RegIP)
11328 || i.base_reg->reg_num == RegIZ))
be05d201 11329 || (i.index_reg
1b54b8d7
JB
11330 && !i.index_reg->reg_type.bitfield.xmmword
11331 && !i.index_reg->reg_type.bitfield.ymmword
11332 && !i.index_reg->reg_type.bitfield.zmmword
be05d201 11333 && ((addr_mode == CODE_64BIT
e968fc9b
JB
11334 ? !i.index_reg->reg_type.bitfield.qword
11335 : !i.index_reg->reg_type.bitfield.dword)
be05d201
L
11336 || !i.index_reg->reg_type.bitfield.baseindex)))
11337 goto bad_address;
8178be5b 11338
260cd341 11339 /* bndmk, bndldx, bndstx and mandatory non-vector SIB have special restrictions. */
a152332d 11340 if ((t->opcode_modifier.opcodeprefix == PREFIX_0XF3
389d00a5
JB
11341 && t->opcode_modifier.opcodespace == SPACE_0F
11342 && t->base_opcode == 0x1b)
a152332d 11343 || (t->opcode_modifier.opcodeprefix == PREFIX_NONE
389d00a5
JB
11344 && t->opcode_modifier.opcodespace == SPACE_0F
11345 && (t->base_opcode & ~1) == 0x1a)
a152332d 11346 || t->opcode_modifier.sib == SIBMEM)
8178be5b
JB
11347 {
11348 /* They cannot use RIP-relative addressing. */
e968fc9b 11349 if (i.base_reg && i.base_reg->reg_num == RegIP)
8178be5b
JB
11350 {
11351 as_bad (_("`%s' cannot be used here"), operand_string);
11352 return 0;
11353 }
11354
11355 /* bndldx and bndstx ignore their scale factor. */
a152332d 11356 if (t->opcode_modifier.opcodeprefix == PREFIX_NONE
389d00a5
JB
11357 && t->opcode_modifier.opcodespace == SPACE_0F
11358 && (t->base_opcode & ~1) == 0x1a
8178be5b
JB
11359 && i.log2_scale_factor)
11360 as_warn (_("register scaling is being ignored here"));
11361 }
be05d201
L
11362 }
11363 else
3e73aa7c 11364 {
be05d201 11365 /* 16-bit checks. */
41eb8e88
L
11366 if (i.disp_encoding == disp_encoding_32bit)
11367 goto bad_disp;
11368
3e73aa7c 11369 if ((i.base_reg
dc821c5f 11370 && (!i.base_reg->reg_type.bitfield.word
40fb9820 11371 || !i.base_reg->reg_type.bitfield.baseindex))
3e73aa7c 11372 || (i.index_reg
dc821c5f 11373 && (!i.index_reg->reg_type.bitfield.word
40fb9820 11374 || !i.index_reg->reg_type.bitfield.baseindex
29b0f896
AM
11375 || !(i.base_reg
11376 && i.base_reg->reg_num < 6
11377 && i.index_reg->reg_num >= 6
11378 && i.log2_scale_factor == 0))))
be05d201 11379 goto bad_address;
3e73aa7c
JH
11380 }
11381 }
be05d201 11382 return 1;
24eab124 11383}
252b5132 11384
43234a1e
L
11385/* Handle vector immediates. */
11386
11387static int
11388RC_SAE_immediate (const char *imm_start)
11389{
43234a1e 11390 const char *pstr = imm_start;
43234a1e
L
11391
11392 if (*pstr != '{')
11393 return 0;
11394
7063667e
JB
11395 pstr = RC_SAE_specifier (pstr + 1);
11396 if (pstr == NULL)
43234a1e
L
11397 return 0;
11398
11399 if (*pstr++ != '}')
11400 {
11401 as_bad (_("Missing '}': '%s'"), imm_start);
11402 return 0;
11403 }
11404 /* RC/SAE immediate string should contain nothing more. */;
11405 if (*pstr != 0)
11406 {
11407 as_bad (_("Junk after '}': '%s'"), imm_start);
11408 return 0;
11409 }
11410
cf665fee
JB
11411 /* Internally this doesn't count as an operand. */
11412 --i.operands;
43234a1e 11413
43234a1e
L
11414 return 1;
11415}
11416
8325cc63
JB
11417/* Only string instructions can have a second memory operand, so
11418 reduce current_templates to just those if it contains any. */
11419static int
11420maybe_adjust_templates (void)
11421{
11422 const insn_template *t;
11423
11424 gas_assert (i.mem_operands == 1);
11425
11426 for (t = current_templates->start; t < current_templates->end; ++t)
11427 if (t->opcode_modifier.isstring)
11428 break;
11429
11430 if (t < current_templates->end)
11431 {
11432 static templates aux_templates;
5b7c81bd 11433 bool recheck;
8325cc63
JB
11434
11435 aux_templates.start = t;
11436 for (; t < current_templates->end; ++t)
11437 if (!t->opcode_modifier.isstring)
11438 break;
11439 aux_templates.end = t;
11440
11441 /* Determine whether to re-check the first memory operand. */
11442 recheck = (aux_templates.start != current_templates->start
11443 || t != current_templates->end);
11444
11445 current_templates = &aux_templates;
11446
11447 if (recheck)
11448 {
11449 i.mem_operands = 0;
11450 if (i.memop1_string != NULL
11451 && i386_index_check (i.memop1_string) == 0)
11452 return 0;
11453 i.mem_operands = 1;
11454 }
11455 }
11456
11457 return 1;
11458}
11459
9d299bea
JB
11460static INLINE bool starts_memory_operand (char c)
11461{
014fbcda 11462 return ISDIGIT (c)
9d299bea 11463 || is_identifier_char (c)
014fbcda 11464 || strchr ("([\"+-!~", c);
9d299bea
JB
11465}
11466
fc0763e6 11467/* Parse OPERAND_STRING into the i386_insn structure I. Returns zero
47926f60 11468 on error. */
252b5132 11469
252b5132 11470static int
a7619375 11471i386_att_operand (char *operand_string)
252b5132 11472{
af6bdddf
AM
11473 const reg_entry *r;
11474 char *end_op;
24eab124 11475 char *op_string = operand_string;
252b5132 11476
24eab124 11477 if (is_space_char (*op_string))
252b5132
RH
11478 ++op_string;
11479
24eab124 11480 /* We check for an absolute prefix (differentiating,
47926f60 11481 for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
24eab124
AM
11482 if (*op_string == ABSOLUTE_PREFIX)
11483 {
11484 ++op_string;
11485 if (is_space_char (*op_string))
11486 ++op_string;
5b7c81bd 11487 i.jumpabsolute = true;
24eab124 11488 }
252b5132 11489
47926f60 11490 /* Check if operand is a register. */
4d1bb795 11491 if ((r = parse_register (op_string, &end_op)) != NULL)
24eab124 11492 {
40fb9820
L
11493 i386_operand_type temp;
11494
8a6fb3f9
JB
11495 if (r == &bad_reg)
11496 return 0;
11497
24eab124
AM
11498 /* Check for a segment override by searching for ':' after a
11499 segment register. */
11500 op_string = end_op;
11501 if (is_space_char (*op_string))
11502 ++op_string;
00cee14f 11503 if (*op_string == ':' && r->reg_type.bitfield.class == SReg)
24eab124 11504 {
5e042380 11505 i.seg[i.mem_operands] = r;
252b5132 11506
24eab124 11507 /* Skip the ':' and whitespace. */
252b5132
RH
11508 ++op_string;
11509 if (is_space_char (*op_string))
24eab124 11510 ++op_string;
252b5132 11511
47926f60 11512 /* Handle case of %es:*foo. */
c8d541e2 11513 if (!i.jumpabsolute && *op_string == ABSOLUTE_PREFIX)
24eab124
AM
11514 {
11515 ++op_string;
11516 if (is_space_char (*op_string))
11517 ++op_string;
5b7c81bd 11518 i.jumpabsolute = true;
24eab124 11519 }
c8d541e2 11520
9d299bea 11521 if (!starts_memory_operand (*op_string))
c8d541e2
JB
11522 {
11523 as_bad (_("bad memory operand `%s'"), op_string);
11524 return 0;
11525 }
24eab124
AM
11526 goto do_memory_reference;
11527 }
43234a1e
L
11528
11529 /* Handle vector operations. */
11530 if (*op_string == '{')
11531 {
f70c6814 11532 op_string = check_VecOperations (op_string);
43234a1e
L
11533 if (op_string == NULL)
11534 return 0;
11535 }
11536
24eab124
AM
11537 if (*op_string)
11538 {
d0b47220 11539 as_bad (_("junk `%s' after register"), op_string);
24eab124
AM
11540 return 0;
11541 }
40fb9820
L
11542 temp = r->reg_type;
11543 temp.bitfield.baseindex = 0;
c6fb90c8
L
11544 i.types[this_operand] = operand_type_or (i.types[this_operand],
11545 temp);
7d5e4556 11546 i.types[this_operand].bitfield.unspecified = 0;
520dc8e8 11547 i.op[this_operand].regs = r;
24eab124 11548 i.reg_operands++;
cf665fee
JB
11549
11550 /* A GPR may follow an RC or SAE immediate only if a (vector) register
11551 operand was also present earlier on. */
11552 if (i.rounding.type != rc_none && temp.bitfield.class == Reg
11553 && i.reg_operands == 1)
11554 {
11555 unsigned int j;
11556
11557 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); ++j)
11558 if (i.rounding.type == RC_NamesTable[j].type)
11559 break;
11560 as_bad (_("`%s': misplaced `{%s}'"),
11561 current_templates->start->name, RC_NamesTable[j].name);
11562 return 0;
11563 }
24eab124 11564 }
af6bdddf
AM
11565 else if (*op_string == REGISTER_PREFIX)
11566 {
11567 as_bad (_("bad register name `%s'"), op_string);
11568 return 0;
11569 }
24eab124 11570 else if (*op_string == IMMEDIATE_PREFIX)
ce8a8b2f 11571 {
24eab124 11572 ++op_string;
6f2f06be 11573 if (i.jumpabsolute)
24eab124 11574 {
d0b47220 11575 as_bad (_("immediate operand illegal with absolute jump"));
24eab124
AM
11576 return 0;
11577 }
11578 if (!i386_immediate (op_string))
11579 return 0;
cf665fee
JB
11580 if (i.rounding.type != rc_none)
11581 {
11582 as_bad (_("`%s': RC/SAE operand must follow immediate operands"),
11583 current_templates->start->name);
11584 return 0;
11585 }
24eab124 11586 }
43234a1e
L
11587 else if (RC_SAE_immediate (operand_string))
11588 {
cf665fee
JB
11589 /* If it is a RC or SAE immediate, do the necessary placement check:
11590 Only another immediate or a GPR may precede it. */
11591 if (i.mem_operands || i.reg_operands + i.imm_operands > 1
11592 || (i.reg_operands == 1
11593 && i.op[0].regs->reg_type.bitfield.class != Reg))
11594 {
11595 as_bad (_("`%s': misplaced `%s'"),
11596 current_templates->start->name, operand_string);
11597 return 0;
11598 }
43234a1e 11599 }
9d299bea 11600 else if (starts_memory_operand (*op_string))
24eab124 11601 {
47926f60 11602 /* This is a memory reference of some sort. */
af6bdddf 11603 char *base_string;
252b5132 11604
47926f60 11605 /* Start and end of displacement string expression (if found). */
eecb386c
AM
11606 char *displacement_string_start;
11607 char *displacement_string_end;
252b5132 11608
24eab124 11609 do_memory_reference:
8325cc63
JB
11610 if (i.mem_operands == 1 && !maybe_adjust_templates ())
11611 return 0;
24eab124 11612 if ((i.mem_operands == 1
40fb9820 11613 && !current_templates->start->opcode_modifier.isstring)
24eab124
AM
11614 || i.mem_operands == 2)
11615 {
11616 as_bad (_("too many memory references for `%s'"),
11617 current_templates->start->name);
11618 return 0;
11619 }
252b5132 11620
24eab124
AM
11621 /* Check for base index form. We detect the base index form by
11622 looking for an ')' at the end of the operand, searching
11623 for the '(' matching it, and finding a REGISTER_PREFIX or ','
11624 after the '('. */
af6bdddf 11625 base_string = op_string + strlen (op_string);
c3332e24 11626
43234a1e 11627 /* Handle vector operations. */
6b5ba0d4
JB
11628 --base_string;
11629 if (is_space_char (*base_string))
11630 --base_string;
11631
11632 if (*base_string == '}')
43234a1e 11633 {
6b5ba0d4
JB
11634 char *vop_start = NULL;
11635
11636 while (base_string-- > op_string)
11637 {
11638 if (*base_string == '"')
11639 break;
11640 if (*base_string != '{')
11641 continue;
11642
11643 vop_start = base_string;
11644
11645 --base_string;
11646 if (is_space_char (*base_string))
11647 --base_string;
11648
11649 if (*base_string != '}')
11650 break;
11651
11652 vop_start = NULL;
11653 }
11654
11655 if (!vop_start)
11656 {
11657 as_bad (_("unbalanced figure braces"));
11658 return 0;
11659 }
11660
f70c6814 11661 if (check_VecOperations (vop_start) == NULL)
43234a1e 11662 return 0;
43234a1e
L
11663 }
11664
47926f60 11665 /* If we only have a displacement, set-up for it to be parsed later. */
af6bdddf
AM
11666 displacement_string_start = op_string;
11667 displacement_string_end = base_string + 1;
252b5132 11668
24eab124
AM
11669 if (*base_string == ')')
11670 {
af6bdddf 11671 char *temp_string;
e87fb6a6
JB
11672 unsigned int parens_not_balanced = 0;
11673 bool in_quotes = false;
e68c3d59 11674
24eab124 11675 /* We've already checked that the number of left & right ()'s are
e87fb6a6
JB
11676 equal, and that there's a matching set of double quotes. */
11677 end_op = base_string;
11678 for (temp_string = op_string; temp_string < end_op; temp_string++)
24eab124 11679 {
e87fb6a6
JB
11680 if (*temp_string == '\\' && temp_string[1] == '"')
11681 ++temp_string;
11682 else if (*temp_string == '"')
11683 in_quotes = !in_quotes;
11684 else if (!in_quotes)
11685 {
11686 if (*temp_string == '(' && !parens_not_balanced++)
11687 base_string = temp_string;
11688 if (*temp_string == ')')
11689 --parens_not_balanced;
11690 }
24eab124 11691 }
c3332e24 11692
af6bdddf 11693 temp_string = base_string;
c3332e24 11694
24eab124 11695 /* Skip past '(' and whitespace. */
e87fb6a6
JB
11696 gas_assert (*base_string == '(');
11697 ++base_string;
252b5132 11698 if (is_space_char (*base_string))
24eab124 11699 ++base_string;
252b5132 11700
af6bdddf 11701 if (*base_string == ','
4eed87de
AM
11702 || ((i.base_reg = parse_register (base_string, &end_op))
11703 != NULL))
252b5132 11704 {
af6bdddf 11705 displacement_string_end = temp_string;
252b5132 11706
40fb9820 11707 i.types[this_operand].bitfield.baseindex = 1;
252b5132 11708
af6bdddf 11709 if (i.base_reg)
24eab124 11710 {
8a6fb3f9
JB
11711 if (i.base_reg == &bad_reg)
11712 return 0;
24eab124
AM
11713 base_string = end_op;
11714 if (is_space_char (*base_string))
11715 ++base_string;
af6bdddf
AM
11716 }
11717
11718 /* There may be an index reg or scale factor here. */
11719 if (*base_string == ',')
11720 {
11721 ++base_string;
11722 if (is_space_char (*base_string))
11723 ++base_string;
11724
4eed87de
AM
11725 if ((i.index_reg = parse_register (base_string, &end_op))
11726 != NULL)
24eab124 11727 {
8a6fb3f9
JB
11728 if (i.index_reg == &bad_reg)
11729 return 0;
af6bdddf 11730 base_string = end_op;
24eab124
AM
11731 if (is_space_char (*base_string))
11732 ++base_string;
af6bdddf
AM
11733 if (*base_string == ',')
11734 {
11735 ++base_string;
11736 if (is_space_char (*base_string))
11737 ++base_string;
11738 }
e5cb08ac 11739 else if (*base_string != ')')
af6bdddf 11740 {
4eed87de
AM
11741 as_bad (_("expecting `,' or `)' "
11742 "after index register in `%s'"),
af6bdddf
AM
11743 operand_string);
11744 return 0;
11745 }
24eab124 11746 }
af6bdddf 11747 else if (*base_string == REGISTER_PREFIX)
24eab124 11748 {
f76bf5e0
L
11749 end_op = strchr (base_string, ',');
11750 if (end_op)
11751 *end_op = '\0';
af6bdddf 11752 as_bad (_("bad register name `%s'"), base_string);
24eab124
AM
11753 return 0;
11754 }
252b5132 11755
47926f60 11756 /* Check for scale factor. */
551c1ca1 11757 if (*base_string != ')')
af6bdddf 11758 {
551c1ca1
AM
11759 char *end_scale = i386_scale (base_string);
11760
11761 if (!end_scale)
af6bdddf 11762 return 0;
24eab124 11763
551c1ca1 11764 base_string = end_scale;
af6bdddf
AM
11765 if (is_space_char (*base_string))
11766 ++base_string;
11767 if (*base_string != ')')
11768 {
4eed87de
AM
11769 as_bad (_("expecting `)' "
11770 "after scale factor in `%s'"),
af6bdddf
AM
11771 operand_string);
11772 return 0;
11773 }
11774 }
11775 else if (!i.index_reg)
24eab124 11776 {
4eed87de
AM
11777 as_bad (_("expecting index register or scale factor "
11778 "after `,'; got '%c'"),
af6bdddf 11779 *base_string);
24eab124
AM
11780 return 0;
11781 }
11782 }
af6bdddf 11783 else if (*base_string != ')')
24eab124 11784 {
4eed87de
AM
11785 as_bad (_("expecting `,' or `)' "
11786 "after base register in `%s'"),
af6bdddf 11787 operand_string);
24eab124
AM
11788 return 0;
11789 }
c3332e24 11790 }
af6bdddf 11791 else if (*base_string == REGISTER_PREFIX)
c3332e24 11792 {
f76bf5e0
L
11793 end_op = strchr (base_string, ',');
11794 if (end_op)
11795 *end_op = '\0';
af6bdddf 11796 as_bad (_("bad register name `%s'"), base_string);
24eab124 11797 return 0;
c3332e24 11798 }
24eab124
AM
11799 }
11800
11801 /* If there's an expression beginning the operand, parse it,
11802 assuming displacement_string_start and
11803 displacement_string_end are meaningful. */
11804 if (displacement_string_start != displacement_string_end)
11805 {
11806 if (!i386_displacement (displacement_string_start,
11807 displacement_string_end))
11808 return 0;
11809 }
11810
11811 /* Special case for (%dx) while doing input/output op. */
11812 if (i.base_reg
75e5731b
JB
11813 && i.base_reg->reg_type.bitfield.instance == RegD
11814 && i.base_reg->reg_type.bitfield.word
24eab124
AM
11815 && i.index_reg == 0
11816 && i.log2_scale_factor == 0
11817 && i.seg[i.mem_operands] == 0
40fb9820 11818 && !operand_type_check (i.types[this_operand], disp))
24eab124 11819 {
2fb5be8d 11820 i.types[this_operand] = i.base_reg->reg_type;
9373f275 11821 i.input_output_operand = true;
24eab124
AM
11822 return 1;
11823 }
11824
eecb386c
AM
11825 if (i386_index_check (operand_string) == 0)
11826 return 0;
c48dadc9 11827 i.flags[this_operand] |= Operand_Mem;
8325cc63
JB
11828 if (i.mem_operands == 0)
11829 i.memop1_string = xstrdup (operand_string);
24eab124
AM
11830 i.mem_operands++;
11831 }
11832 else
ce8a8b2f
AM
11833 {
11834 /* It's not a memory operand; argh! */
24eab124
AM
11835 as_bad (_("invalid char %s beginning operand %d `%s'"),
11836 output_invalid (*op_string),
11837 this_operand + 1,
11838 op_string);
11839 return 0;
11840 }
47926f60 11841 return 1; /* Normal return. */
252b5132
RH
11842}
11843\f
fa94de6b
RM
11844/* Calculate the maximum variable size (i.e., excluding fr_fix)
11845 that an rs_machine_dependent frag may reach. */
11846
11847unsigned int
11848i386_frag_max_var (fragS *frag)
11849{
11850 /* The only relaxable frags are for jumps.
11851 Unconditional jumps can grow by 4 bytes and others by 5 bytes. */
11852 gas_assert (frag->fr_type == rs_machine_dependent);
11853 return TYPE_FROM_RELAX_STATE (frag->fr_subtype) == UNCOND_JUMP ? 4 : 5;
11854}
11855
b084df0b
L
11856#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
11857static int
8dcea932 11858elf_symbol_resolved_in_segment_p (symbolS *fr_symbol, offsetT fr_var)
b084df0b
L
11859{
11860 /* STT_GNU_IFUNC symbol must go through PLT. */
11861 if ((symbol_get_bfdsym (fr_symbol)->flags
11862 & BSF_GNU_INDIRECT_FUNCTION) != 0)
11863 return 0;
11864
11865 if (!S_IS_EXTERNAL (fr_symbol))
11866 /* Symbol may be weak or local. */
11867 return !S_IS_WEAK (fr_symbol);
11868
8dcea932
L
11869 /* Global symbols with non-default visibility can't be preempted. */
11870 if (ELF_ST_VISIBILITY (S_GET_OTHER (fr_symbol)) != STV_DEFAULT)
11871 return 1;
11872
11873 if (fr_var != NO_RELOC)
11874 switch ((enum bfd_reloc_code_real) fr_var)
11875 {
11876 case BFD_RELOC_386_PLT32:
11877 case BFD_RELOC_X86_64_PLT32:
33eaf5de 11878 /* Symbol with PLT relocation may be preempted. */
8dcea932
L
11879 return 0;
11880 default:
11881 abort ();
11882 }
11883
b084df0b
L
11884 /* Global symbols with default visibility in a shared library may be
11885 preempted by another definition. */
8dcea932 11886 return !shared;
b084df0b
L
11887}
11888#endif
11889
79d72f45
HL
11890/* Table 3-2. Macro-Fusible Instructions in Haswell Microarchitecture
11891 Note also work for Skylake and Cascadelake.
11892---------------------------------------------------------------------
11893| JCC | ADD/SUB/CMP | INC/DEC | TEST/AND |
11894| ------ | ----------- | ------- | -------- |
11895| Jo | N | N | Y |
11896| Jno | N | N | Y |
11897| Jc/Jb | Y | N | Y |
11898| Jae/Jnb | Y | N | Y |
11899| Je/Jz | Y | Y | Y |
11900| Jne/Jnz | Y | Y | Y |
11901| Jna/Jbe | Y | N | Y |
11902| Ja/Jnbe | Y | N | Y |
11903| Js | N | N | Y |
11904| Jns | N | N | Y |
11905| Jp/Jpe | N | N | Y |
11906| Jnp/Jpo | N | N | Y |
11907| Jl/Jnge | Y | Y | Y |
11908| Jge/Jnl | Y | Y | Y |
11909| Jle/Jng | Y | Y | Y |
11910| Jg/Jnle | Y | Y | Y |
11911--------------------------------------------------------------------- */
11912static int
11913i386_macro_fusible_p (enum mf_cmp_kind mf_cmp, enum mf_jcc_kind mf_jcc)
11914{
11915 if (mf_cmp == mf_cmp_alu_cmp)
11916 return ((mf_jcc >= mf_jcc_jc && mf_jcc <= mf_jcc_jna)
11917 || mf_jcc == mf_jcc_jl || mf_jcc == mf_jcc_jle);
11918 if (mf_cmp == mf_cmp_incdec)
11919 return (mf_jcc == mf_jcc_je || mf_jcc == mf_jcc_jl
11920 || mf_jcc == mf_jcc_jle);
11921 if (mf_cmp == mf_cmp_test_and)
11922 return 1;
11923 return 0;
11924}
11925
e379e5f3
L
11926/* Return the next non-empty frag. */
11927
11928static fragS *
11929i386_next_non_empty_frag (fragS *fragP)
11930{
11931 /* There may be a frag with a ".fill 0" when there is no room in
11932 the current frag for frag_grow in output_insn. */
11933 for (fragP = fragP->fr_next;
11934 (fragP != NULL
11935 && fragP->fr_type == rs_fill
11936 && fragP->fr_fix == 0);
11937 fragP = fragP->fr_next)
11938 ;
11939 return fragP;
11940}
11941
11942/* Return the next jcc frag after BRANCH_PADDING. */
11943
11944static fragS *
79d72f45 11945i386_next_fusible_jcc_frag (fragS *maybe_cmp_fragP, fragS *pad_fragP)
e379e5f3 11946{
79d72f45
HL
11947 fragS *branch_fragP;
11948 if (!pad_fragP)
e379e5f3
L
11949 return NULL;
11950
79d72f45
HL
11951 if (pad_fragP->fr_type == rs_machine_dependent
11952 && (TYPE_FROM_RELAX_STATE (pad_fragP->fr_subtype)
e379e5f3
L
11953 == BRANCH_PADDING))
11954 {
79d72f45
HL
11955 branch_fragP = i386_next_non_empty_frag (pad_fragP);
11956 if (branch_fragP->fr_type != rs_machine_dependent)
e379e5f3 11957 return NULL;
79d72f45
HL
11958 if (TYPE_FROM_RELAX_STATE (branch_fragP->fr_subtype) == COND_JUMP
11959 && i386_macro_fusible_p (maybe_cmp_fragP->tc_frag_data.mf_type,
11960 pad_fragP->tc_frag_data.mf_type))
11961 return branch_fragP;
e379e5f3
L
11962 }
11963
11964 return NULL;
11965}
11966
11967/* Classify BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags. */
11968
11969static void
11970i386_classify_machine_dependent_frag (fragS *fragP)
11971{
11972 fragS *cmp_fragP;
11973 fragS *pad_fragP;
11974 fragS *branch_fragP;
11975 fragS *next_fragP;
11976 unsigned int max_prefix_length;
11977
11978 if (fragP->tc_frag_data.classified)
11979 return;
11980
11981 /* First scan for BRANCH_PADDING and FUSED_JCC_PADDING. Convert
11982 FUSED_JCC_PADDING and merge BRANCH_PADDING. */
11983 for (next_fragP = fragP;
11984 next_fragP != NULL;
11985 next_fragP = next_fragP->fr_next)
11986 {
11987 next_fragP->tc_frag_data.classified = 1;
11988 if (next_fragP->fr_type == rs_machine_dependent)
11989 switch (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype))
11990 {
11991 case BRANCH_PADDING:
11992 /* The BRANCH_PADDING frag must be followed by a branch
11993 frag. */
11994 branch_fragP = i386_next_non_empty_frag (next_fragP);
11995 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
11996 break;
11997 case FUSED_JCC_PADDING:
11998 /* Check if this is a fused jcc:
11999 FUSED_JCC_PADDING
12000 CMP like instruction
12001 BRANCH_PADDING
12002 COND_JUMP
12003 */
12004 cmp_fragP = i386_next_non_empty_frag (next_fragP);
12005 pad_fragP = i386_next_non_empty_frag (cmp_fragP);
79d72f45 12006 branch_fragP = i386_next_fusible_jcc_frag (next_fragP, pad_fragP);
e379e5f3
L
12007 if (branch_fragP)
12008 {
12009 /* The BRANCH_PADDING frag is merged with the
12010 FUSED_JCC_PADDING frag. */
12011 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
12012 /* CMP like instruction size. */
12013 next_fragP->tc_frag_data.cmp_size = cmp_fragP->fr_fix;
12014 frag_wane (pad_fragP);
12015 /* Skip to branch_fragP. */
12016 next_fragP = branch_fragP;
12017 }
12018 else if (next_fragP->tc_frag_data.max_prefix_length)
12019 {
12020 /* Turn FUSED_JCC_PADDING into BRANCH_PREFIX if it isn't
12021 a fused jcc. */
12022 next_fragP->fr_subtype
12023 = ENCODE_RELAX_STATE (BRANCH_PREFIX, 0);
12024 next_fragP->tc_frag_data.max_bytes
12025 = next_fragP->tc_frag_data.max_prefix_length;
12026 /* This will be updated in the BRANCH_PREFIX scan. */
12027 next_fragP->tc_frag_data.max_prefix_length = 0;
12028 }
12029 else
12030 frag_wane (next_fragP);
12031 break;
12032 }
12033 }
12034
12035 /* Stop if there is no BRANCH_PREFIX. */
12036 if (!align_branch_prefix_size)
12037 return;
12038
12039 /* Scan for BRANCH_PREFIX. */
12040 for (; fragP != NULL; fragP = fragP->fr_next)
12041 {
12042 if (fragP->fr_type != rs_machine_dependent
12043 || (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
12044 != BRANCH_PREFIX))
12045 continue;
12046
12047 /* Count all BRANCH_PREFIX frags before BRANCH_PADDING and
12048 COND_JUMP_PREFIX. */
12049 max_prefix_length = 0;
12050 for (next_fragP = fragP;
12051 next_fragP != NULL;
12052 next_fragP = next_fragP->fr_next)
12053 {
12054 if (next_fragP->fr_type == rs_fill)
12055 /* Skip rs_fill frags. */
12056 continue;
12057 else if (next_fragP->fr_type != rs_machine_dependent)
12058 /* Stop for all other frags. */
12059 break;
12060
12061 /* rs_machine_dependent frags. */
12062 if (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12063 == BRANCH_PREFIX)
12064 {
12065 /* Count BRANCH_PREFIX frags. */
12066 if (max_prefix_length >= MAX_FUSED_JCC_PADDING_SIZE)
12067 {
12068 max_prefix_length = MAX_FUSED_JCC_PADDING_SIZE;
12069 frag_wane (next_fragP);
12070 }
12071 else
12072 max_prefix_length
12073 += next_fragP->tc_frag_data.max_bytes;
12074 }
12075 else if ((TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12076 == BRANCH_PADDING)
12077 || (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12078 == FUSED_JCC_PADDING))
12079 {
12080 /* Stop at BRANCH_PADDING and FUSED_JCC_PADDING. */
12081 fragP->tc_frag_data.u.padding_fragP = next_fragP;
12082 break;
12083 }
12084 else
12085 /* Stop for other rs_machine_dependent frags. */
12086 break;
12087 }
12088
12089 fragP->tc_frag_data.max_prefix_length = max_prefix_length;
12090
12091 /* Skip to the next frag. */
12092 fragP = next_fragP;
12093 }
12094}
12095
12096/* Compute padding size for
12097
12098 FUSED_JCC_PADDING
12099 CMP like instruction
12100 BRANCH_PADDING
12101 COND_JUMP/UNCOND_JUMP
12102
12103 or
12104
12105 BRANCH_PADDING
12106 COND_JUMP/UNCOND_JUMP
12107 */
12108
12109static int
12110i386_branch_padding_size (fragS *fragP, offsetT address)
12111{
12112 unsigned int offset, size, padding_size;
12113 fragS *branch_fragP = fragP->tc_frag_data.u.branch_fragP;
12114
12115 /* The start address of the BRANCH_PADDING or FUSED_JCC_PADDING frag. */
12116 if (!address)
12117 address = fragP->fr_address;
12118 address += fragP->fr_fix;
12119
12120 /* CMP like instrunction size. */
12121 size = fragP->tc_frag_data.cmp_size;
12122
12123 /* The base size of the branch frag. */
12124 size += branch_fragP->fr_fix;
12125
12126 /* Add opcode and displacement bytes for the rs_machine_dependent
12127 branch frag. */
12128 if (branch_fragP->fr_type == rs_machine_dependent)
12129 size += md_relax_table[branch_fragP->fr_subtype].rlx_length;
12130
12131 /* Check if branch is within boundary and doesn't end at the last
12132 byte. */
12133 offset = address & ((1U << align_branch_power) - 1);
12134 if ((offset + size) >= (1U << align_branch_power))
12135 /* Padding needed to avoid crossing boundary. */
12136 padding_size = (1U << align_branch_power) - offset;
12137 else
12138 /* No padding needed. */
12139 padding_size = 0;
12140
12141 /* The return value may be saved in tc_frag_data.length which is
12142 unsigned byte. */
12143 if (!fits_in_unsigned_byte (padding_size))
12144 abort ();
12145
12146 return padding_size;
12147}
12148
12149/* i386_generic_table_relax_frag()
12150
12151 Handle BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags to
12152 grow/shrink padding to align branch frags. Hand others to
12153 relax_frag(). */
12154
12155long
12156i386_generic_table_relax_frag (segT segment, fragS *fragP, long stretch)
12157{
12158 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12159 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
12160 {
12161 long padding_size = i386_branch_padding_size (fragP, 0);
12162 long grow = padding_size - fragP->tc_frag_data.length;
12163
12164 /* When the BRANCH_PREFIX frag is used, the computed address
12165 must match the actual address and there should be no padding. */
12166 if (fragP->tc_frag_data.padding_address
12167 && (fragP->tc_frag_data.padding_address != fragP->fr_address
12168 || padding_size))
12169 abort ();
12170
12171 /* Update the padding size. */
12172 if (grow)
12173 fragP->tc_frag_data.length = padding_size;
12174
12175 return grow;
12176 }
12177 else if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12178 {
12179 fragS *padding_fragP, *next_fragP;
12180 long padding_size, left_size, last_size;
12181
12182 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
12183 if (!padding_fragP)
12184 /* Use the padding set by the leading BRANCH_PREFIX frag. */
12185 return (fragP->tc_frag_data.length
12186 - fragP->tc_frag_data.last_length);
12187
12188 /* Compute the relative address of the padding frag in the very
12189 first time where the BRANCH_PREFIX frag sizes are zero. */
12190 if (!fragP->tc_frag_data.padding_address)
12191 fragP->tc_frag_data.padding_address
12192 = padding_fragP->fr_address - (fragP->fr_address - stretch);
12193
12194 /* First update the last length from the previous interation. */
12195 left_size = fragP->tc_frag_data.prefix_length;
12196 for (next_fragP = fragP;
12197 next_fragP != padding_fragP;
12198 next_fragP = next_fragP->fr_next)
12199 if (next_fragP->fr_type == rs_machine_dependent
12200 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12201 == BRANCH_PREFIX))
12202 {
12203 if (left_size)
12204 {
12205 int max = next_fragP->tc_frag_data.max_bytes;
12206 if (max)
12207 {
12208 int size;
12209 if (max > left_size)
12210 size = left_size;
12211 else
12212 size = max;
12213 left_size -= size;
12214 next_fragP->tc_frag_data.last_length = size;
12215 }
12216 }
12217 else
12218 next_fragP->tc_frag_data.last_length = 0;
12219 }
12220
12221 /* Check the padding size for the padding frag. */
12222 padding_size = i386_branch_padding_size
12223 (padding_fragP, (fragP->fr_address
12224 + fragP->tc_frag_data.padding_address));
12225
12226 last_size = fragP->tc_frag_data.prefix_length;
12227 /* Check if there is change from the last interation. */
12228 if (padding_size == last_size)
12229 {
12230 /* Update the expected address of the padding frag. */
12231 padding_fragP->tc_frag_data.padding_address
12232 = (fragP->fr_address + padding_size
12233 + fragP->tc_frag_data.padding_address);
12234 return 0;
12235 }
12236
12237 if (padding_size > fragP->tc_frag_data.max_prefix_length)
12238 {
12239 /* No padding if there is no sufficient room. Clear the
12240 expected address of the padding frag. */
12241 padding_fragP->tc_frag_data.padding_address = 0;
12242 padding_size = 0;
12243 }
12244 else
12245 /* Store the expected address of the padding frag. */
12246 padding_fragP->tc_frag_data.padding_address
12247 = (fragP->fr_address + padding_size
12248 + fragP->tc_frag_data.padding_address);
12249
12250 fragP->tc_frag_data.prefix_length = padding_size;
12251
12252 /* Update the length for the current interation. */
12253 left_size = padding_size;
12254 for (next_fragP = fragP;
12255 next_fragP != padding_fragP;
12256 next_fragP = next_fragP->fr_next)
12257 if (next_fragP->fr_type == rs_machine_dependent
12258 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12259 == BRANCH_PREFIX))
12260 {
12261 if (left_size)
12262 {
12263 int max = next_fragP->tc_frag_data.max_bytes;
12264 if (max)
12265 {
12266 int size;
12267 if (max > left_size)
12268 size = left_size;
12269 else
12270 size = max;
12271 left_size -= size;
12272 next_fragP->tc_frag_data.length = size;
12273 }
12274 }
12275 else
12276 next_fragP->tc_frag_data.length = 0;
12277 }
12278
12279 return (fragP->tc_frag_data.length
12280 - fragP->tc_frag_data.last_length);
12281 }
12282 return relax_frag (segment, fragP, stretch);
12283}
12284
ee7fcc42
AM
12285/* md_estimate_size_before_relax()
12286
12287 Called just before relax() for rs_machine_dependent frags. The x86
12288 assembler uses these frags to handle variable size jump
12289 instructions.
12290
12291 Any symbol that is now undefined will not become defined.
12292 Return the correct fr_subtype in the frag.
12293 Return the initial "guess for variable size of frag" to caller.
12294 The guess is actually the growth beyond the fixed part. Whatever
12295 we do to grow the fixed or variable part contributes to our
12296 returned value. */
12297
252b5132 12298int
7016a5d5 12299md_estimate_size_before_relax (fragS *fragP, segT segment)
252b5132 12300{
e379e5f3
L
12301 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12302 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX
12303 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
12304 {
12305 i386_classify_machine_dependent_frag (fragP);
12306 return fragP->tc_frag_data.length;
12307 }
12308
252b5132 12309 /* We've already got fragP->fr_subtype right; all we have to do is
b98ef147
AM
12310 check for un-relaxable symbols. On an ELF system, we can't relax
12311 an externally visible symbol, because it may be overridden by a
12312 shared library. */
12313 if (S_GET_SEGMENT (fragP->fr_symbol) != segment
6d249963 12314#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12315 || (IS_ELF
8dcea932
L
12316 && !elf_symbol_resolved_in_segment_p (fragP->fr_symbol,
12317 fragP->fr_var))
fbeb56a4
DK
12318#endif
12319#if defined (OBJ_COFF) && defined (TE_PE)
7ab9ffdd 12320 || (OUTPUT_FLAVOR == bfd_target_coff_flavour
fbeb56a4 12321 && S_IS_WEAK (fragP->fr_symbol))
b98ef147
AM
12322#endif
12323 )
252b5132 12324 {
b98ef147
AM
12325 /* Symbol is undefined in this segment, or we need to keep a
12326 reloc so that weak symbols can be overridden. */
12327 int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
f86103b7 12328 enum bfd_reloc_code_real reloc_type;
ee7fcc42
AM
12329 unsigned char *opcode;
12330 int old_fr_fix;
eb19308f 12331 fixS *fixP = NULL;
f6af82bd 12332
ee7fcc42 12333 if (fragP->fr_var != NO_RELOC)
1e9cc1c2 12334 reloc_type = (enum bfd_reloc_code_real) fragP->fr_var;
b98ef147 12335 else if (size == 2)
f6af82bd 12336 reloc_type = BFD_RELOC_16_PCREL;
bd7ab16b 12337#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1ef3994a
JB
12338 else if (fragP->tc_frag_data.code64 && fragP->fr_offset == 0
12339 && need_plt32_p (fragP->fr_symbol))
bd7ab16b
L
12340 reloc_type = BFD_RELOC_X86_64_PLT32;
12341#endif
f6af82bd
AM
12342 else
12343 reloc_type = BFD_RELOC_32_PCREL;
252b5132 12344
ee7fcc42
AM
12345 old_fr_fix = fragP->fr_fix;
12346 opcode = (unsigned char *) fragP->fr_opcode;
12347
fddf5b5b 12348 switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
252b5132 12349 {
fddf5b5b
AM
12350 case UNCOND_JUMP:
12351 /* Make jmp (0xeb) a (d)word displacement jump. */
47926f60 12352 opcode[0] = 0xe9;
252b5132 12353 fragP->fr_fix += size;
eb19308f
JB
12354 fixP = fix_new (fragP, old_fr_fix, size,
12355 fragP->fr_symbol,
12356 fragP->fr_offset, 1,
12357 reloc_type);
252b5132
RH
12358 break;
12359
fddf5b5b 12360 case COND_JUMP86:
412167cb
AM
12361 if (size == 2
12362 && (!no_cond_jump_promotion || fragP->fr_var != NO_RELOC))
fddf5b5b
AM
12363 {
12364 /* Negate the condition, and branch past an
12365 unconditional jump. */
12366 opcode[0] ^= 1;
12367 opcode[1] = 3;
12368 /* Insert an unconditional jump. */
12369 opcode[2] = 0xe9;
12370 /* We added two extra opcode bytes, and have a two byte
12371 offset. */
12372 fragP->fr_fix += 2 + 2;
062cd5e7
AS
12373 fix_new (fragP, old_fr_fix + 2, 2,
12374 fragP->fr_symbol,
12375 fragP->fr_offset, 1,
12376 reloc_type);
fddf5b5b
AM
12377 break;
12378 }
12379 /* Fall through. */
12380
12381 case COND_JUMP:
412167cb
AM
12382 if (no_cond_jump_promotion && fragP->fr_var == NO_RELOC)
12383 {
12384 fragP->fr_fix += 1;
3e02c1cc
AM
12385 fixP = fix_new (fragP, old_fr_fix, 1,
12386 fragP->fr_symbol,
12387 fragP->fr_offset, 1,
12388 BFD_RELOC_8_PCREL);
12389 fixP->fx_signed = 1;
412167cb
AM
12390 break;
12391 }
93c2a809 12392
24eab124 12393 /* This changes the byte-displacement jump 0x7N
fddf5b5b 12394 to the (d)word-displacement jump 0x0f,0x8N. */
252b5132 12395 opcode[1] = opcode[0] + 0x10;
f6af82bd 12396 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
47926f60
KH
12397 /* We've added an opcode byte. */
12398 fragP->fr_fix += 1 + size;
eb19308f
JB
12399 fixP = fix_new (fragP, old_fr_fix + 1, size,
12400 fragP->fr_symbol,
12401 fragP->fr_offset, 1,
12402 reloc_type);
252b5132 12403 break;
fddf5b5b
AM
12404
12405 default:
12406 BAD_CASE (fragP->fr_subtype);
12407 break;
252b5132 12408 }
eb19308f
JB
12409
12410 /* All jumps handled here are signed, but don't unconditionally use a
12411 signed limit check for 32 and 16 bit jumps as we want to allow wrap
12412 around at 4G (outside of 64-bit mode) and 64k. */
12413 if (size == 4 && flag_code == CODE_64BIT)
12414 fixP->fx_signed = 1;
12415
252b5132 12416 frag_wane (fragP);
ee7fcc42 12417 return fragP->fr_fix - old_fr_fix;
252b5132 12418 }
93c2a809 12419
93c2a809
AM
12420 /* Guess size depending on current relax state. Initially the relax
12421 state will correspond to a short jump and we return 1, because
12422 the variable part of the frag (the branch offset) is one byte
12423 long. However, we can relax a section more than once and in that
12424 case we must either set fr_subtype back to the unrelaxed state,
12425 or return the value for the appropriate branch. */
12426 return md_relax_table[fragP->fr_subtype].rlx_length;
ee7fcc42
AM
12427}
12428
47926f60
KH
12429/* Called after relax() is finished.
12430
12431 In: Address of frag.
12432 fr_type == rs_machine_dependent.
12433 fr_subtype is what the address relaxed to.
12434
12435 Out: Any fixSs and constants are set up.
12436 Caller will turn frag into a ".space 0". */
12437
252b5132 12438void
7016a5d5
TG
12439md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec ATTRIBUTE_UNUSED,
12440 fragS *fragP)
252b5132 12441{
29b0f896 12442 unsigned char *opcode;
252b5132 12443 unsigned char *where_to_put_displacement = NULL;
847f7ad4
AM
12444 offsetT target_address;
12445 offsetT opcode_address;
252b5132 12446 unsigned int extension = 0;
847f7ad4 12447 offsetT displacement_from_opcode_start;
252b5132 12448
e379e5f3
L
12449 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12450 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING
12451 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12452 {
12453 /* Generate nop padding. */
12454 unsigned int size = fragP->tc_frag_data.length;
12455 if (size)
12456 {
12457 if (size > fragP->tc_frag_data.max_bytes)
12458 abort ();
12459
12460 if (flag_debug)
12461 {
12462 const char *msg;
12463 const char *branch = "branch";
12464 const char *prefix = "";
12465 fragS *padding_fragP;
12466 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
12467 == BRANCH_PREFIX)
12468 {
12469 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
12470 switch (fragP->tc_frag_data.default_prefix)
12471 {
12472 default:
12473 abort ();
12474 break;
12475 case CS_PREFIX_OPCODE:
12476 prefix = " cs";
12477 break;
12478 case DS_PREFIX_OPCODE:
12479 prefix = " ds";
12480 break;
12481 case ES_PREFIX_OPCODE:
12482 prefix = " es";
12483 break;
12484 case FS_PREFIX_OPCODE:
12485 prefix = " fs";
12486 break;
12487 case GS_PREFIX_OPCODE:
12488 prefix = " gs";
12489 break;
12490 case SS_PREFIX_OPCODE:
12491 prefix = " ss";
12492 break;
12493 }
12494 if (padding_fragP)
12495 msg = _("%s:%u: add %d%s at 0x%llx to align "
12496 "%s within %d-byte boundary\n");
12497 else
12498 msg = _("%s:%u: add additional %d%s at 0x%llx to "
12499 "align %s within %d-byte boundary\n");
12500 }
12501 else
12502 {
12503 padding_fragP = fragP;
12504 msg = _("%s:%u: add %d%s-byte nop at 0x%llx to align "
12505 "%s within %d-byte boundary\n");
12506 }
12507
12508 if (padding_fragP)
12509 switch (padding_fragP->tc_frag_data.branch_type)
12510 {
12511 case align_branch_jcc:
12512 branch = "jcc";
12513 break;
12514 case align_branch_fused:
12515 branch = "fused jcc";
12516 break;
12517 case align_branch_jmp:
12518 branch = "jmp";
12519 break;
12520 case align_branch_call:
12521 branch = "call";
12522 break;
12523 case align_branch_indirect:
12524 branch = "indiret branch";
12525 break;
12526 case align_branch_ret:
12527 branch = "ret";
12528 break;
12529 default:
12530 break;
12531 }
12532
12533 fprintf (stdout, msg,
12534 fragP->fr_file, fragP->fr_line, size, prefix,
12535 (long long) fragP->fr_address, branch,
12536 1 << align_branch_power);
12537 }
12538 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12539 memset (fragP->fr_opcode,
12540 fragP->tc_frag_data.default_prefix, size);
12541 else
12542 i386_generate_nops (fragP, (char *) fragP->fr_opcode,
12543 size, 0);
12544 fragP->fr_fix += size;
12545 }
12546 return;
12547 }
12548
252b5132
RH
12549 opcode = (unsigned char *) fragP->fr_opcode;
12550
47926f60 12551 /* Address we want to reach in file space. */
252b5132 12552 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
252b5132 12553
47926f60 12554 /* Address opcode resides at in file space. */
252b5132
RH
12555 opcode_address = fragP->fr_address + fragP->fr_fix;
12556
47926f60 12557 /* Displacement from opcode start to fill into instruction. */
252b5132
RH
12558 displacement_from_opcode_start = target_address - opcode_address;
12559
fddf5b5b 12560 if ((fragP->fr_subtype & BIG) == 0)
252b5132 12561 {
47926f60
KH
12562 /* Don't have to change opcode. */
12563 extension = 1; /* 1 opcode + 1 displacement */
252b5132 12564 where_to_put_displacement = &opcode[1];
fddf5b5b
AM
12565 }
12566 else
12567 {
12568 if (no_cond_jump_promotion
12569 && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
4eed87de
AM
12570 as_warn_where (fragP->fr_file, fragP->fr_line,
12571 _("long jump required"));
252b5132 12572
fddf5b5b
AM
12573 switch (fragP->fr_subtype)
12574 {
12575 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
12576 extension = 4; /* 1 opcode + 4 displacement */
12577 opcode[0] = 0xe9;
12578 where_to_put_displacement = &opcode[1];
12579 break;
252b5132 12580
fddf5b5b
AM
12581 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
12582 extension = 2; /* 1 opcode + 2 displacement */
12583 opcode[0] = 0xe9;
12584 where_to_put_displacement = &opcode[1];
12585 break;
252b5132 12586
fddf5b5b
AM
12587 case ENCODE_RELAX_STATE (COND_JUMP, BIG):
12588 case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
12589 extension = 5; /* 2 opcode + 4 displacement */
12590 opcode[1] = opcode[0] + 0x10;
12591 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12592 where_to_put_displacement = &opcode[2];
12593 break;
252b5132 12594
fddf5b5b
AM
12595 case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
12596 extension = 3; /* 2 opcode + 2 displacement */
12597 opcode[1] = opcode[0] + 0x10;
12598 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
12599 where_to_put_displacement = &opcode[2];
12600 break;
252b5132 12601
fddf5b5b
AM
12602 case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
12603 extension = 4;
12604 opcode[0] ^= 1;
12605 opcode[1] = 3;
12606 opcode[2] = 0xe9;
12607 where_to_put_displacement = &opcode[3];
12608 break;
12609
12610 default:
12611 BAD_CASE (fragP->fr_subtype);
12612 break;
12613 }
252b5132 12614 }
fddf5b5b 12615
7b81dfbb
AJ
12616 /* If size if less then four we are sure that the operand fits,
12617 but if it's 4, then it could be that the displacement is larger
12618 then -/+ 2GB. */
12619 if (DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype) == 4
12620 && object_64bit
12621 && ((addressT) (displacement_from_opcode_start - extension
4eed87de
AM
12622 + ((addressT) 1 << 31))
12623 > (((addressT) 2 << 31) - 1)))
7b81dfbb
AJ
12624 {
12625 as_bad_where (fragP->fr_file, fragP->fr_line,
12626 _("jump target out of range"));
12627 /* Make us emit 0. */
12628 displacement_from_opcode_start = extension;
12629 }
47926f60 12630 /* Now put displacement after opcode. */
252b5132
RH
12631 md_number_to_chars ((char *) where_to_put_displacement,
12632 (valueT) (displacement_from_opcode_start - extension),
fddf5b5b 12633 DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
252b5132
RH
12634 fragP->fr_fix += extension;
12635}
12636\f
7016a5d5 12637/* Apply a fixup (fixP) to segment data, once it has been determined
252b5132
RH
12638 by our caller that we have all the info we need to fix it up.
12639
7016a5d5
TG
12640 Parameter valP is the pointer to the value of the bits.
12641
252b5132
RH
12642 On the 386, immediates, displacements, and data pointers are all in
12643 the same (little-endian) format, so we don't need to care about which
12644 we are handling. */
12645
94f592af 12646void
7016a5d5 12647md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 12648{
94f592af 12649 char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
c6682705 12650 valueT value = *valP;
252b5132 12651
f86103b7 12652#if !defined (TE_Mach)
93382f6d
AM
12653 if (fixP->fx_pcrel)
12654 {
12655 switch (fixP->fx_r_type)
12656 {
5865bb77
ILT
12657 default:
12658 break;
12659
d6ab8113
JB
12660 case BFD_RELOC_64:
12661 fixP->fx_r_type = BFD_RELOC_64_PCREL;
12662 break;
93382f6d 12663 case BFD_RELOC_32:
ae8887b5 12664 case BFD_RELOC_X86_64_32S:
93382f6d
AM
12665 fixP->fx_r_type = BFD_RELOC_32_PCREL;
12666 break;
12667 case BFD_RELOC_16:
12668 fixP->fx_r_type = BFD_RELOC_16_PCREL;
12669 break;
12670 case BFD_RELOC_8:
12671 fixP->fx_r_type = BFD_RELOC_8_PCREL;
12672 break;
12673 }
12674 }
252b5132 12675
a161fe53 12676 if (fixP->fx_addsy != NULL
31312f95 12677 && (fixP->fx_r_type == BFD_RELOC_32_PCREL
d6ab8113 12678 || fixP->fx_r_type == BFD_RELOC_64_PCREL
31312f95 12679 || fixP->fx_r_type == BFD_RELOC_16_PCREL
d258b828 12680 || fixP->fx_r_type == BFD_RELOC_8_PCREL)
31312f95 12681 && !use_rela_relocations)
252b5132 12682 {
31312f95
AM
12683 /* This is a hack. There should be a better way to handle this.
12684 This covers for the fact that bfd_install_relocation will
12685 subtract the current location (for partial_inplace, PC relative
12686 relocations); see more below. */
252b5132 12687#ifndef OBJ_AOUT
718ddfc0 12688 if (IS_ELF
252b5132
RH
12689#ifdef TE_PE
12690 || OUTPUT_FLAVOR == bfd_target_coff_flavour
12691#endif
12692 )
12693 value += fixP->fx_where + fixP->fx_frag->fr_address;
12694#endif
12695#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12696 if (IS_ELF)
252b5132 12697 {
6539b54b 12698 segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
2f66722d 12699
6539b54b 12700 if ((sym_seg == seg
2f66722d 12701 || (symbol_section_p (fixP->fx_addsy)
6539b54b 12702 && sym_seg != absolute_section))
af65af87 12703 && !generic_force_reloc (fixP))
2f66722d
AM
12704 {
12705 /* Yes, we add the values in twice. This is because
6539b54b
AM
12706 bfd_install_relocation subtracts them out again. I think
12707 bfd_install_relocation is broken, but I don't dare change
2f66722d
AM
12708 it. FIXME. */
12709 value += fixP->fx_where + fixP->fx_frag->fr_address;
12710 }
252b5132
RH
12711 }
12712#endif
12713#if defined (OBJ_COFF) && defined (TE_PE)
977cdf5a
NC
12714 /* For some reason, the PE format does not store a
12715 section address offset for a PC relative symbol. */
12716 if (S_GET_SEGMENT (fixP->fx_addsy) != seg
7be1c489 12717 || S_IS_WEAK (fixP->fx_addsy))
252b5132
RH
12718 value += md_pcrel_from (fixP);
12719#endif
12720 }
fbeb56a4 12721#if defined (OBJ_COFF) && defined (TE_PE)
f01c1a09
NC
12722 if (fixP->fx_addsy != NULL
12723 && S_IS_WEAK (fixP->fx_addsy)
12724 /* PR 16858: Do not modify weak function references. */
12725 && ! fixP->fx_pcrel)
fbeb56a4 12726 {
296a8689
NC
12727#if !defined (TE_PEP)
12728 /* For x86 PE weak function symbols are neither PC-relative
12729 nor do they set S_IS_FUNCTION. So the only reliable way
12730 to detect them is to check the flags of their containing
12731 section. */
12732 if (S_GET_SEGMENT (fixP->fx_addsy) != NULL
12733 && S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_CODE)
12734 ;
12735 else
12736#endif
fbeb56a4
DK
12737 value -= S_GET_VALUE (fixP->fx_addsy);
12738 }
12739#endif
252b5132
RH
12740
12741 /* Fix a few things - the dynamic linker expects certain values here,
0234cb7c 12742 and we must not disappoint it. */
252b5132 12743#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 12744 if (IS_ELF && fixP->fx_addsy)
47926f60
KH
12745 switch (fixP->fx_r_type)
12746 {
12747 case BFD_RELOC_386_PLT32:
3e73aa7c 12748 case BFD_RELOC_X86_64_PLT32:
b9519cfe
L
12749 /* Make the jump instruction point to the address of the operand.
12750 At runtime we merely add the offset to the actual PLT entry.
12751 NB: Subtract the offset size only for jump instructions. */
12752 if (fixP->fx_pcrel)
12753 value = -4;
47926f60 12754 break;
31312f95 12755
13ae64f3
JJ
12756 case BFD_RELOC_386_TLS_GD:
12757 case BFD_RELOC_386_TLS_LDM:
13ae64f3 12758 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
12759 case BFD_RELOC_386_TLS_IE:
12760 case BFD_RELOC_386_TLS_GOTIE:
67a4f2b7 12761 case BFD_RELOC_386_TLS_GOTDESC:
bffbf940
JJ
12762 case BFD_RELOC_X86_64_TLSGD:
12763 case BFD_RELOC_X86_64_TLSLD:
12764 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7 12765 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
00f7efb6
JJ
12766 value = 0; /* Fully resolved at runtime. No addend. */
12767 /* Fallthrough */
12768 case BFD_RELOC_386_TLS_LE:
12769 case BFD_RELOC_386_TLS_LDO_32:
12770 case BFD_RELOC_386_TLS_LE_32:
12771 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 12772 case BFD_RELOC_X86_64_DTPOFF64:
00f7efb6 12773 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113 12774 case BFD_RELOC_X86_64_TPOFF64:
00f7efb6
JJ
12775 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12776 break;
12777
67a4f2b7
AO
12778 case BFD_RELOC_386_TLS_DESC_CALL:
12779 case BFD_RELOC_X86_64_TLSDESC_CALL:
12780 value = 0; /* Fully resolved at runtime. No addend. */
12781 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12782 fixP->fx_done = 0;
12783 return;
12784
47926f60
KH
12785 case BFD_RELOC_VTABLE_INHERIT:
12786 case BFD_RELOC_VTABLE_ENTRY:
12787 fixP->fx_done = 0;
94f592af 12788 return;
47926f60
KH
12789
12790 default:
12791 break;
12792 }
12793#endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) */
a442cac5
JB
12794
12795 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
12796 if (!object_64bit)
12797 value = extend_to_32bit_address (value);
12798
c6682705 12799 *valP = value;
f86103b7 12800#endif /* !defined (TE_Mach) */
3e73aa7c 12801
3e73aa7c 12802 /* Are we finished with this relocation now? */
c6682705 12803 if (fixP->fx_addsy == NULL)
b8188555
JB
12804 {
12805 fixP->fx_done = 1;
12806 switch (fixP->fx_r_type)
12807 {
12808 case BFD_RELOC_X86_64_32S:
12809 fixP->fx_signed = 1;
12810 break;
12811
12812 default:
12813 break;
12814 }
12815 }
fbeb56a4
DK
12816#if defined (OBJ_COFF) && defined (TE_PE)
12817 else if (fixP->fx_addsy != NULL && S_IS_WEAK (fixP->fx_addsy))
12818 {
12819 fixP->fx_done = 0;
12820 /* Remember value for tc_gen_reloc. */
12821 fixP->fx_addnumber = value;
12822 /* Clear out the frag for now. */
12823 value = 0;
12824 }
12825#endif
3e73aa7c
JH
12826 else if (use_rela_relocations)
12827 {
46fb6d5a
JB
12828 if (!disallow_64bit_reloc || fixP->fx_r_type == NO_RELOC)
12829 fixP->fx_no_overflow = 1;
062cd5e7
AS
12830 /* Remember value for tc_gen_reloc. */
12831 fixP->fx_addnumber = value;
3e73aa7c
JH
12832 value = 0;
12833 }
f86103b7 12834
94f592af 12835 md_number_to_chars (p, value, fixP->fx_size);
252b5132 12836}
252b5132 12837\f
6d4af3c2 12838const char *
499ac353 12839md_atof (int type, char *litP, int *sizeP)
252b5132 12840{
499ac353
NC
12841 /* This outputs the LITTLENUMs in REVERSE order;
12842 in accord with the bigendian 386. */
5b7c81bd 12843 return ieee_md_atof (type, litP, sizeP, false);
252b5132
RH
12844}
12845\f
2d545b82 12846static char output_invalid_buf[sizeof (unsigned char) * 2 + 6];
252b5132 12847
252b5132 12848static char *
e3bb37b5 12849output_invalid (int c)
252b5132 12850{
3882b010 12851 if (ISPRINT (c))
f9f21a03
L
12852 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
12853 "'%c'", c);
252b5132 12854 else
f9f21a03 12855 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
2d545b82 12856 "(0x%x)", (unsigned char) c);
252b5132
RH
12857 return output_invalid_buf;
12858}
12859
8a6fb3f9
JB
12860/* Verify that @r can be used in the current context. */
12861
5b7c81bd 12862static bool check_register (const reg_entry *r)
8a6fb3f9
JB
12863{
12864 if (allow_pseudo_reg)
5b7c81bd 12865 return true;
8a6fb3f9
JB
12866
12867 if (operand_type_all_zero (&r->reg_type))
5b7c81bd 12868 return false;
8a6fb3f9
JB
12869
12870 if ((r->reg_type.bitfield.dword
12871 || (r->reg_type.bitfield.class == SReg && r->reg_num > 3)
12872 || r->reg_type.bitfield.class == RegCR
22e00a3f 12873 || r->reg_type.bitfield.class == RegDR)
8a6fb3f9 12874 && !cpu_arch_flags.bitfield.cpui386)
5b7c81bd 12875 return false;
8a6fb3f9 12876
22e00a3f
JB
12877 if (r->reg_type.bitfield.class == RegTR
12878 && (flag_code == CODE_64BIT
12879 || !cpu_arch_flags.bitfield.cpui386
12880 || cpu_arch_isa_flags.bitfield.cpui586
12881 || cpu_arch_isa_flags.bitfield.cpui686))
5b7c81bd 12882 return false;
22e00a3f 12883
8a6fb3f9 12884 if (r->reg_type.bitfield.class == RegMMX && !cpu_arch_flags.bitfield.cpummx)
5b7c81bd 12885 return false;
8a6fb3f9
JB
12886
12887 if (!cpu_arch_flags.bitfield.cpuavx512f)
12888 {
12889 if (r->reg_type.bitfield.zmmword
12890 || r->reg_type.bitfield.class == RegMask)
5b7c81bd 12891 return false;
8a6fb3f9
JB
12892
12893 if (!cpu_arch_flags.bitfield.cpuavx)
12894 {
12895 if (r->reg_type.bitfield.ymmword)
5b7c81bd 12896 return false;
8a6fb3f9
JB
12897
12898 if (!cpu_arch_flags.bitfield.cpusse && r->reg_type.bitfield.xmmword)
5b7c81bd 12899 return false;
8a6fb3f9
JB
12900 }
12901 }
12902
260cd341
LC
12903 if (r->reg_type.bitfield.tmmword
12904 && (!cpu_arch_flags.bitfield.cpuamx_tile
12905 || flag_code != CODE_64BIT))
5b7c81bd 12906 return false;
260cd341 12907
8a6fb3f9 12908 if (r->reg_type.bitfield.class == RegBND && !cpu_arch_flags.bitfield.cpumpx)
5b7c81bd 12909 return false;
8a6fb3f9
JB
12910
12911 /* Don't allow fake index register unless allow_index_reg isn't 0. */
12912 if (!allow_index_reg && r->reg_num == RegIZ)
5b7c81bd 12913 return false;
8a6fb3f9
JB
12914
12915 /* Upper 16 vector registers are only available with VREX in 64bit
12916 mode, and require EVEX encoding. */
12917 if (r->reg_flags & RegVRex)
12918 {
12919 if (!cpu_arch_flags.bitfield.cpuavx512f
12920 || flag_code != CODE_64BIT)
5b7c81bd 12921 return false;
8a6fb3f9 12922
da4977e0
JB
12923 if (i.vec_encoding == vex_encoding_default)
12924 i.vec_encoding = vex_encoding_evex;
12925 else if (i.vec_encoding != vex_encoding_evex)
12926 i.vec_encoding = vex_encoding_error;
8a6fb3f9
JB
12927 }
12928
12929 if (((r->reg_flags & (RegRex64 | RegRex)) || r->reg_type.bitfield.qword)
12930 && (!cpu_arch_flags.bitfield.cpulm || r->reg_type.bitfield.class != RegCR)
12931 && flag_code != CODE_64BIT)
5b7c81bd 12932 return false;
8a6fb3f9
JB
12933
12934 if (r->reg_type.bitfield.class == SReg && r->reg_num == RegFlat
12935 && !intel_syntax)
5b7c81bd 12936 return false;
8a6fb3f9 12937
5b7c81bd 12938 return true;
8a6fb3f9
JB
12939}
12940
af6bdddf 12941/* REG_STRING starts *before* REGISTER_PREFIX. */
252b5132
RH
12942
12943static const reg_entry *
4d1bb795 12944parse_real_register (char *reg_string, char **end_op)
252b5132 12945{
af6bdddf
AM
12946 char *s = reg_string;
12947 char *p;
252b5132
RH
12948 char reg_name_given[MAX_REG_NAME_SIZE + 1];
12949 const reg_entry *r;
12950
12951 /* Skip possible REGISTER_PREFIX and possible whitespace. */
12952 if (*s == REGISTER_PREFIX)
12953 ++s;
12954
12955 if (is_space_char (*s))
12956 ++s;
12957
12958 p = reg_name_given;
af6bdddf 12959 while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
252b5132
RH
12960 {
12961 if (p >= reg_name_given + MAX_REG_NAME_SIZE)
af6bdddf
AM
12962 return (const reg_entry *) NULL;
12963 s++;
252b5132
RH
12964 }
12965
6588847e
DN
12966 /* For naked regs, make sure that we are not dealing with an identifier.
12967 This prevents confusing an identifier like `eax_var' with register
12968 `eax'. */
12969 if (allow_naked_reg && identifier_chars[(unsigned char) *s])
12970 return (const reg_entry *) NULL;
12971
af6bdddf 12972 *end_op = s;
252b5132 12973
629310ab 12974 r = (const reg_entry *) str_hash_find (reg_hash, reg_name_given);
252b5132 12975
5f47d35b 12976 /* Handle floating point regs, allowing spaces in the (i) part. */
6288d05f 12977 if (r == reg_st0)
5f47d35b 12978 {
0e0eea78
JB
12979 if (!cpu_arch_flags.bitfield.cpu8087
12980 && !cpu_arch_flags.bitfield.cpu287
af32b722
JB
12981 && !cpu_arch_flags.bitfield.cpu387
12982 && !allow_pseudo_reg)
0e0eea78
JB
12983 return (const reg_entry *) NULL;
12984
5f47d35b
AM
12985 if (is_space_char (*s))
12986 ++s;
12987 if (*s == '(')
12988 {
af6bdddf 12989 ++s;
5f47d35b
AM
12990 if (is_space_char (*s))
12991 ++s;
12992 if (*s >= '0' && *s <= '7')
12993 {
db557034 12994 int fpr = *s - '0';
af6bdddf 12995 ++s;
5f47d35b
AM
12996 if (is_space_char (*s))
12997 ++s;
12998 if (*s == ')')
12999 {
13000 *end_op = s + 1;
6288d05f 13001 know (r[fpr].reg_num == fpr);
db557034 13002 return r + fpr;
5f47d35b 13003 }
5f47d35b 13004 }
47926f60 13005 /* We have "%st(" then garbage. */
5f47d35b
AM
13006 return (const reg_entry *) NULL;
13007 }
13008 }
13009
8a6fb3f9 13010 return r && check_register (r) ? r : NULL;
252b5132 13011}
4d1bb795
JB
13012
13013/* REG_STRING starts *before* REGISTER_PREFIX. */
13014
13015static const reg_entry *
13016parse_register (char *reg_string, char **end_op)
13017{
13018 const reg_entry *r;
13019
13020 if (*reg_string == REGISTER_PREFIX || allow_naked_reg)
13021 r = parse_real_register (reg_string, end_op);
13022 else
13023 r = NULL;
13024 if (!r)
13025 {
13026 char *save = input_line_pointer;
13027 char c;
13028 symbolS *symbolP;
13029
13030 input_line_pointer = reg_string;
d02603dc 13031 c = get_symbol_name (&reg_string);
4d1bb795 13032 symbolP = symbol_find (reg_string);
64d23078
JB
13033 while (symbolP && S_GET_SEGMENT (symbolP) != reg_section)
13034 {
13035 const expressionS *e = symbol_get_value_expression(symbolP);
13036
13037 if (e->X_op != O_symbol || e->X_add_number)
13038 break;
13039 symbolP = e->X_add_symbol;
13040 }
4d1bb795
JB
13041 if (symbolP && S_GET_SEGMENT (symbolP) == reg_section)
13042 {
13043 const expressionS *e = symbol_get_value_expression (symbolP);
13044
3b55a1d0
JB
13045 know (e->X_op == O_register);
13046 know (e->X_add_number >= 0
13047 && (valueT) e->X_add_number < i386_regtab_size);
13048 r = i386_regtab + e->X_add_number;
13049 if (!check_register (r))
8a6fb3f9 13050 {
3b55a1d0
JB
13051 as_bad (_("register '%s%s' cannot be used here"),
13052 register_prefix, r->reg_name);
13053 r = &bad_reg;
8a6fb3f9 13054 }
3b55a1d0 13055 *end_op = input_line_pointer;
4d1bb795
JB
13056 }
13057 *input_line_pointer = c;
13058 input_line_pointer = save;
13059 }
13060 return r;
13061}
13062
13063int
13064i386_parse_name (char *name, expressionS *e, char *nextcharP)
13065{
4faaa10f 13066 const reg_entry *r = NULL;
4d1bb795
JB
13067 char *end = input_line_pointer;
13068
13069 *end = *nextcharP;
4faaa10f
JB
13070 if (*name == REGISTER_PREFIX || allow_naked_reg)
13071 r = parse_real_register (name, &input_line_pointer);
4d1bb795
JB
13072 if (r && end <= input_line_pointer)
13073 {
13074 *nextcharP = *input_line_pointer;
13075 *input_line_pointer = 0;
8a6fb3f9
JB
13076 if (r != &bad_reg)
13077 {
13078 e->X_op = O_register;
13079 e->X_add_number = r - i386_regtab;
13080 }
13081 else
13082 e->X_op = O_illegal;
4d1bb795
JB
13083 return 1;
13084 }
13085 input_line_pointer = end;
13086 *end = 0;
ee86248c 13087 return intel_syntax ? i386_intel_parse_name (name, e) : 0;
4d1bb795
JB
13088}
13089
13090void
13091md_operand (expressionS *e)
13092{
ee86248c
JB
13093 char *end;
13094 const reg_entry *r;
4d1bb795 13095
ee86248c
JB
13096 switch (*input_line_pointer)
13097 {
13098 case REGISTER_PREFIX:
13099 r = parse_real_register (input_line_pointer, &end);
4d1bb795
JB
13100 if (r)
13101 {
13102 e->X_op = O_register;
13103 e->X_add_number = r - i386_regtab;
13104 input_line_pointer = end;
13105 }
ee86248c
JB
13106 break;
13107
13108 case '[':
9c2799c2 13109 gas_assert (intel_syntax);
ee86248c
JB
13110 end = input_line_pointer++;
13111 expression (e);
13112 if (*input_line_pointer == ']')
13113 {
13114 ++input_line_pointer;
13115 e->X_op_symbol = make_expr_symbol (e);
13116 e->X_add_symbol = NULL;
13117 e->X_add_number = 0;
13118 e->X_op = O_index;
13119 }
13120 else
13121 {
13122 e->X_op = O_absent;
13123 input_line_pointer = end;
13124 }
13125 break;
4d1bb795
JB
13126 }
13127}
13128
252b5132 13129\f
4cc782b5 13130#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
b6f8c7c4 13131const char *md_shortopts = "kVQ:sqnO::";
252b5132 13132#else
b6f8c7c4 13133const char *md_shortopts = "qnO::";
252b5132 13134#endif
6e0b89ee 13135
3e73aa7c 13136#define OPTION_32 (OPTION_MD_BASE + 0)
b3b91714
AM
13137#define OPTION_64 (OPTION_MD_BASE + 1)
13138#define OPTION_DIVIDE (OPTION_MD_BASE + 2)
9103f4f4
L
13139#define OPTION_MARCH (OPTION_MD_BASE + 3)
13140#define OPTION_MTUNE (OPTION_MD_BASE + 4)
1efbbeb4
L
13141#define OPTION_MMNEMONIC (OPTION_MD_BASE + 5)
13142#define OPTION_MSYNTAX (OPTION_MD_BASE + 6)
13143#define OPTION_MINDEX_REG (OPTION_MD_BASE + 7)
13144#define OPTION_MNAKED_REG (OPTION_MD_BASE + 8)
bd5dea88 13145#define OPTION_MRELAX_RELOCATIONS (OPTION_MD_BASE + 9)
c0f3af97 13146#define OPTION_MSSE2AVX (OPTION_MD_BASE + 10)
daf50ae7 13147#define OPTION_MSSE_CHECK (OPTION_MD_BASE + 11)
7bab8ab5
JB
13148#define OPTION_MOPERAND_CHECK (OPTION_MD_BASE + 12)
13149#define OPTION_MAVXSCALAR (OPTION_MD_BASE + 13)
13150#define OPTION_X32 (OPTION_MD_BASE + 14)
7e8b059b 13151#define OPTION_MADD_BND_PREFIX (OPTION_MD_BASE + 15)
43234a1e
L
13152#define OPTION_MEVEXLIG (OPTION_MD_BASE + 16)
13153#define OPTION_MEVEXWIG (OPTION_MD_BASE + 17)
167ad85b 13154#define OPTION_MBIG_OBJ (OPTION_MD_BASE + 18)
d1982f93 13155#define OPTION_MOMIT_LOCK_PREFIX (OPTION_MD_BASE + 19)
d3d3c6db 13156#define OPTION_MEVEXRCIG (OPTION_MD_BASE + 20)
8dcea932 13157#define OPTION_MSHARED (OPTION_MD_BASE + 21)
5db04b09
L
13158#define OPTION_MAMD64 (OPTION_MD_BASE + 22)
13159#define OPTION_MINTEL64 (OPTION_MD_BASE + 23)
e4e00185 13160#define OPTION_MFENCE_AS_LOCK_ADD (OPTION_MD_BASE + 24)
b4a3a7b4 13161#define OPTION_X86_USED_NOTE (OPTION_MD_BASE + 25)
03751133 13162#define OPTION_MVEXWIG (OPTION_MD_BASE + 26)
e379e5f3
L
13163#define OPTION_MALIGN_BRANCH_BOUNDARY (OPTION_MD_BASE + 27)
13164#define OPTION_MALIGN_BRANCH_PREFIX_SIZE (OPTION_MD_BASE + 28)
13165#define OPTION_MALIGN_BRANCH (OPTION_MD_BASE + 29)
76cf450b 13166#define OPTION_MBRANCHES_WITH_32B_BOUNDARIES (OPTION_MD_BASE + 30)
ae531041
L
13167#define OPTION_MLFENCE_AFTER_LOAD (OPTION_MD_BASE + 31)
13168#define OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH (OPTION_MD_BASE + 32)
13169#define OPTION_MLFENCE_BEFORE_RET (OPTION_MD_BASE + 33)
c8480b58 13170#define OPTION_MUSE_UNALIGNED_VECTOR_MOVE (OPTION_MD_BASE + 34)
b3b91714 13171
99ad8390
NC
13172struct option md_longopts[] =
13173{
3e73aa7c 13174 {"32", no_argument, NULL, OPTION_32},
321098a5 13175#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 13176 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c 13177 {"64", no_argument, NULL, OPTION_64},
351f65ca
L
13178#endif
13179#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 13180 {"x32", no_argument, NULL, OPTION_X32},
8dcea932 13181 {"mshared", no_argument, NULL, OPTION_MSHARED},
b4a3a7b4 13182 {"mx86-used-note", required_argument, NULL, OPTION_X86_USED_NOTE},
6e0b89ee 13183#endif
b3b91714 13184 {"divide", no_argument, NULL, OPTION_DIVIDE},
9103f4f4
L
13185 {"march", required_argument, NULL, OPTION_MARCH},
13186 {"mtune", required_argument, NULL, OPTION_MTUNE},
1efbbeb4
L
13187 {"mmnemonic", required_argument, NULL, OPTION_MMNEMONIC},
13188 {"msyntax", required_argument, NULL, OPTION_MSYNTAX},
13189 {"mindex-reg", no_argument, NULL, OPTION_MINDEX_REG},
13190 {"mnaked-reg", no_argument, NULL, OPTION_MNAKED_REG},
c0f3af97 13191 {"msse2avx", no_argument, NULL, OPTION_MSSE2AVX},
c8480b58 13192 {"muse-unaligned-vector-move", no_argument, NULL, OPTION_MUSE_UNALIGNED_VECTOR_MOVE},
daf50ae7 13193 {"msse-check", required_argument, NULL, OPTION_MSSE_CHECK},
7bab8ab5 13194 {"moperand-check", required_argument, NULL, OPTION_MOPERAND_CHECK},
539f890d 13195 {"mavxscalar", required_argument, NULL, OPTION_MAVXSCALAR},
03751133 13196 {"mvexwig", required_argument, NULL, OPTION_MVEXWIG},
7e8b059b 13197 {"madd-bnd-prefix", no_argument, NULL, OPTION_MADD_BND_PREFIX},
43234a1e
L
13198 {"mevexlig", required_argument, NULL, OPTION_MEVEXLIG},
13199 {"mevexwig", required_argument, NULL, OPTION_MEVEXWIG},
167ad85b
TG
13200# if defined (TE_PE) || defined (TE_PEP)
13201 {"mbig-obj", no_argument, NULL, OPTION_MBIG_OBJ},
13202#endif
d1982f93 13203 {"momit-lock-prefix", required_argument, NULL, OPTION_MOMIT_LOCK_PREFIX},
e4e00185 13204 {"mfence-as-lock-add", required_argument, NULL, OPTION_MFENCE_AS_LOCK_ADD},
0cb4071e 13205 {"mrelax-relocations", required_argument, NULL, OPTION_MRELAX_RELOCATIONS},
d3d3c6db 13206 {"mevexrcig", required_argument, NULL, OPTION_MEVEXRCIG},
e379e5f3
L
13207 {"malign-branch-boundary", required_argument, NULL, OPTION_MALIGN_BRANCH_BOUNDARY},
13208 {"malign-branch-prefix-size", required_argument, NULL, OPTION_MALIGN_BRANCH_PREFIX_SIZE},
13209 {"malign-branch", required_argument, NULL, OPTION_MALIGN_BRANCH},
76cf450b 13210 {"mbranches-within-32B-boundaries", no_argument, NULL, OPTION_MBRANCHES_WITH_32B_BOUNDARIES},
ae531041
L
13211 {"mlfence-after-load", required_argument, NULL, OPTION_MLFENCE_AFTER_LOAD},
13212 {"mlfence-before-indirect-branch", required_argument, NULL,
13213 OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH},
13214 {"mlfence-before-ret", required_argument, NULL, OPTION_MLFENCE_BEFORE_RET},
5db04b09
L
13215 {"mamd64", no_argument, NULL, OPTION_MAMD64},
13216 {"mintel64", no_argument, NULL, OPTION_MINTEL64},
252b5132
RH
13217 {NULL, no_argument, NULL, 0}
13218};
13219size_t md_longopts_size = sizeof (md_longopts);
13220
13221int
17b9d67d 13222md_parse_option (int c, const char *arg)
252b5132 13223{
91d6fa6a 13224 unsigned int j;
e379e5f3 13225 char *arch, *next, *saved, *type;
9103f4f4 13226
252b5132
RH
13227 switch (c)
13228 {
12b55ccc
L
13229 case 'n':
13230 optimize_align_code = 0;
13231 break;
13232
a38cf1db
AM
13233 case 'q':
13234 quiet_warnings = 1;
252b5132
RH
13235 break;
13236
13237#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
a38cf1db
AM
13238 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
13239 should be emitted or not. FIXME: Not implemented. */
13240 case 'Q':
d4693039
JB
13241 if ((arg[0] != 'y' && arg[0] != 'n') || arg[1])
13242 return 0;
252b5132
RH
13243 break;
13244
13245 /* -V: SVR4 argument to print version ID. */
13246 case 'V':
13247 print_version_id ();
13248 break;
13249
a38cf1db
AM
13250 /* -k: Ignore for FreeBSD compatibility. */
13251 case 'k':
252b5132 13252 break;
4cc782b5
ILT
13253
13254 case 's':
13255 /* -s: On i386 Solaris, this tells the native assembler to use
29b0f896 13256 .stab instead of .stab.excl. We always use .stab anyhow. */
4cc782b5 13257 break;
8dcea932
L
13258
13259 case OPTION_MSHARED:
13260 shared = 1;
13261 break;
b4a3a7b4
L
13262
13263 case OPTION_X86_USED_NOTE:
13264 if (strcasecmp (arg, "yes") == 0)
13265 x86_used_note = 1;
13266 else if (strcasecmp (arg, "no") == 0)
13267 x86_used_note = 0;
13268 else
13269 as_fatal (_("invalid -mx86-used-note= option: `%s'"), arg);
13270 break;
13271
13272
99ad8390 13273#endif
321098a5 13274#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 13275 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c
JH
13276 case OPTION_64:
13277 {
13278 const char **list, **l;
13279
3e73aa7c
JH
13280 list = bfd_target_list ();
13281 for (l = list; *l != NULL; l++)
08dedd66 13282 if (startswith (*l, "elf64-x86-64")
99ad8390
NC
13283 || strcmp (*l, "coff-x86-64") == 0
13284 || strcmp (*l, "pe-x86-64") == 0
d382c579
TG
13285 || strcmp (*l, "pei-x86-64") == 0
13286 || strcmp (*l, "mach-o-x86-64") == 0)
6e0b89ee
AM
13287 {
13288 default_arch = "x86_64";
13289 break;
13290 }
3e73aa7c 13291 if (*l == NULL)
2b5d6a91 13292 as_fatal (_("no compiled in support for x86_64"));
3e73aa7c
JH
13293 free (list);
13294 }
13295 break;
13296#endif
252b5132 13297
351f65ca 13298#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 13299 case OPTION_X32:
351f65ca
L
13300 if (IS_ELF)
13301 {
13302 const char **list, **l;
13303
13304 list = bfd_target_list ();
13305 for (l = list; *l != NULL; l++)
08dedd66 13306 if (startswith (*l, "elf32-x86-64"))
351f65ca
L
13307 {
13308 default_arch = "x86_64:32";
13309 break;
13310 }
13311 if (*l == NULL)
2b5d6a91 13312 as_fatal (_("no compiled in support for 32bit x86_64"));
351f65ca
L
13313 free (list);
13314 }
13315 else
13316 as_fatal (_("32bit x86_64 is only supported for ELF"));
13317 break;
13318#endif
13319
6e0b89ee
AM
13320 case OPTION_32:
13321 default_arch = "i386";
13322 break;
13323
b3b91714
AM
13324 case OPTION_DIVIDE:
13325#ifdef SVR4_COMMENT_CHARS
13326 {
13327 char *n, *t;
13328 const char *s;
13329
add39d23 13330 n = XNEWVEC (char, strlen (i386_comment_chars) + 1);
b3b91714
AM
13331 t = n;
13332 for (s = i386_comment_chars; *s != '\0'; s++)
13333 if (*s != '/')
13334 *t++ = *s;
13335 *t = '\0';
13336 i386_comment_chars = n;
13337 }
13338#endif
13339 break;
13340
9103f4f4 13341 case OPTION_MARCH:
293f5f65
L
13342 saved = xstrdup (arg);
13343 arch = saved;
13344 /* Allow -march=+nosse. */
13345 if (*arch == '+')
13346 arch++;
6305a203 13347 do
9103f4f4 13348 {
6305a203 13349 if (*arch == '.')
2b5d6a91 13350 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
13351 next = strchr (arch, '+');
13352 if (next)
13353 *next++ = '\0';
91d6fa6a 13354 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 13355 {
ae89daec
JB
13356 if (arch == saved && cpu_arch[j].type != PROCESSOR_NONE
13357 && strcmp (arch, cpu_arch[j].name) == 0)
ccc9c027 13358 {
6305a203 13359 /* Processor. */
ae89daec 13360 if (! cpu_arch[j].enable.bitfield.cpui386)
1ded5609
JB
13361 continue;
13362
91d6fa6a 13363 cpu_arch_name = cpu_arch[j].name;
d92c7521 13364 free (cpu_sub_arch_name);
6305a203 13365 cpu_sub_arch_name = NULL;
ae89daec 13366 cpu_arch_flags = cpu_arch[j].enable;
91d6fa6a 13367 cpu_arch_isa = cpu_arch[j].type;
ae89daec 13368 cpu_arch_isa_flags = cpu_arch[j].enable;
6305a203
L
13369 if (!cpu_arch_tune_set)
13370 {
13371 cpu_arch_tune = cpu_arch_isa;
13372 cpu_arch_tune_flags = cpu_arch_isa_flags;
13373 }
13374 break;
13375 }
ae89daec
JB
13376 else if (cpu_arch[j].type == PROCESSOR_NONE
13377 && strcmp (arch, cpu_arch[j].name) == 0
13378 && !cpu_flags_all_zero (&cpu_arch[j].enable))
6305a203 13379 {
33eaf5de 13380 /* ISA extension. */
6305a203 13381 i386_cpu_flags flags;
309d3373 13382
293f5f65 13383 flags = cpu_flags_or (cpu_arch_flags,
ae89daec 13384 cpu_arch[j].enable);
81486035 13385
5b64d091 13386 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
6305a203 13387 {
ae89daec 13388 extend_cpu_sub_arch_name (arch);
6305a203 13389 cpu_arch_flags = flags;
a586129e 13390 cpu_arch_isa_flags = flags;
6305a203 13391 }
0089dace
L
13392 else
13393 cpu_arch_isa_flags
13394 = cpu_flags_or (cpu_arch_isa_flags,
ae89daec 13395 cpu_arch[j].enable);
6305a203 13396 break;
ccc9c027 13397 }
9103f4f4 13398 }
6305a203 13399
ae89daec 13400 if (j >= ARRAY_SIZE (cpu_arch) && startswith (arch, "no"))
293f5f65 13401 {
33eaf5de 13402 /* Disable an ISA extension. */
ae89daec
JB
13403 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13404 if (cpu_arch[j].type == PROCESSOR_NONE
13405 && strcmp (arch + 2, cpu_arch[j].name) == 0)
293f5f65
L
13406 {
13407 i386_cpu_flags flags;
13408
13409 flags = cpu_flags_and_not (cpu_arch_flags,
ae89daec 13410 cpu_arch[j].disable);
293f5f65
L
13411 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
13412 {
8180707f 13413 extend_cpu_sub_arch_name (arch);
293f5f65
L
13414 cpu_arch_flags = flags;
13415 cpu_arch_isa_flags = flags;
13416 }
13417 break;
13418 }
293f5f65
L
13419 }
13420
91d6fa6a 13421 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 13422 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
13423
13424 arch = next;
9103f4f4 13425 }
293f5f65
L
13426 while (next != NULL);
13427 free (saved);
9103f4f4
L
13428 break;
13429
13430 case OPTION_MTUNE:
13431 if (*arg == '.')
2b5d6a91 13432 as_fatal (_("invalid -mtune= option: `%s'"), arg);
91d6fa6a 13433 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 13434 {
ae89daec
JB
13435 if (cpu_arch[j].type != PROCESSOR_NONE
13436 && strcmp (arg, cpu_arch[j].name) == 0)
9103f4f4 13437 {
ccc9c027 13438 cpu_arch_tune_set = 1;
91d6fa6a 13439 cpu_arch_tune = cpu_arch [j].type;
ae89daec 13440 cpu_arch_tune_flags = cpu_arch[j].enable;
9103f4f4
L
13441 break;
13442 }
13443 }
91d6fa6a 13444 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 13445 as_fatal (_("invalid -mtune= option: `%s'"), arg);
9103f4f4
L
13446 break;
13447
1efbbeb4
L
13448 case OPTION_MMNEMONIC:
13449 if (strcasecmp (arg, "att") == 0)
13450 intel_mnemonic = 0;
13451 else if (strcasecmp (arg, "intel") == 0)
13452 intel_mnemonic = 1;
13453 else
2b5d6a91 13454 as_fatal (_("invalid -mmnemonic= option: `%s'"), arg);
1efbbeb4
L
13455 break;
13456
13457 case OPTION_MSYNTAX:
13458 if (strcasecmp (arg, "att") == 0)
13459 intel_syntax = 0;
13460 else if (strcasecmp (arg, "intel") == 0)
13461 intel_syntax = 1;
13462 else
2b5d6a91 13463 as_fatal (_("invalid -msyntax= option: `%s'"), arg);
1efbbeb4
L
13464 break;
13465
13466 case OPTION_MINDEX_REG:
13467 allow_index_reg = 1;
13468 break;
13469
13470 case OPTION_MNAKED_REG:
13471 allow_naked_reg = 1;
13472 break;
13473
c0f3af97
L
13474 case OPTION_MSSE2AVX:
13475 sse2avx = 1;
13476 break;
13477
c8480b58
L
13478 case OPTION_MUSE_UNALIGNED_VECTOR_MOVE:
13479 use_unaligned_vector_move = 1;
13480 break;
13481
daf50ae7
L
13482 case OPTION_MSSE_CHECK:
13483 if (strcasecmp (arg, "error") == 0)
7bab8ab5 13484 sse_check = check_error;
daf50ae7 13485 else if (strcasecmp (arg, "warning") == 0)
7bab8ab5 13486 sse_check = check_warning;
daf50ae7 13487 else if (strcasecmp (arg, "none") == 0)
7bab8ab5 13488 sse_check = check_none;
daf50ae7 13489 else
2b5d6a91 13490 as_fatal (_("invalid -msse-check= option: `%s'"), arg);
daf50ae7
L
13491 break;
13492
7bab8ab5
JB
13493 case OPTION_MOPERAND_CHECK:
13494 if (strcasecmp (arg, "error") == 0)
13495 operand_check = check_error;
13496 else if (strcasecmp (arg, "warning") == 0)
13497 operand_check = check_warning;
13498 else if (strcasecmp (arg, "none") == 0)
13499 operand_check = check_none;
13500 else
13501 as_fatal (_("invalid -moperand-check= option: `%s'"), arg);
13502 break;
13503
539f890d
L
13504 case OPTION_MAVXSCALAR:
13505 if (strcasecmp (arg, "128") == 0)
13506 avxscalar = vex128;
13507 else if (strcasecmp (arg, "256") == 0)
13508 avxscalar = vex256;
13509 else
2b5d6a91 13510 as_fatal (_("invalid -mavxscalar= option: `%s'"), arg);
539f890d
L
13511 break;
13512
03751133
L
13513 case OPTION_MVEXWIG:
13514 if (strcmp (arg, "0") == 0)
40c9c8de 13515 vexwig = vexw0;
03751133 13516 else if (strcmp (arg, "1") == 0)
40c9c8de 13517 vexwig = vexw1;
03751133
L
13518 else
13519 as_fatal (_("invalid -mvexwig= option: `%s'"), arg);
13520 break;
13521
7e8b059b
L
13522 case OPTION_MADD_BND_PREFIX:
13523 add_bnd_prefix = 1;
13524 break;
13525
43234a1e
L
13526 case OPTION_MEVEXLIG:
13527 if (strcmp (arg, "128") == 0)
13528 evexlig = evexl128;
13529 else if (strcmp (arg, "256") == 0)
13530 evexlig = evexl256;
13531 else if (strcmp (arg, "512") == 0)
13532 evexlig = evexl512;
13533 else
13534 as_fatal (_("invalid -mevexlig= option: `%s'"), arg);
13535 break;
13536
d3d3c6db
IT
13537 case OPTION_MEVEXRCIG:
13538 if (strcmp (arg, "rne") == 0)
13539 evexrcig = rne;
13540 else if (strcmp (arg, "rd") == 0)
13541 evexrcig = rd;
13542 else if (strcmp (arg, "ru") == 0)
13543 evexrcig = ru;
13544 else if (strcmp (arg, "rz") == 0)
13545 evexrcig = rz;
13546 else
13547 as_fatal (_("invalid -mevexrcig= option: `%s'"), arg);
13548 break;
13549
43234a1e
L
13550 case OPTION_MEVEXWIG:
13551 if (strcmp (arg, "0") == 0)
13552 evexwig = evexw0;
13553 else if (strcmp (arg, "1") == 0)
13554 evexwig = evexw1;
13555 else
13556 as_fatal (_("invalid -mevexwig= option: `%s'"), arg);
13557 break;
13558
167ad85b
TG
13559# if defined (TE_PE) || defined (TE_PEP)
13560 case OPTION_MBIG_OBJ:
13561 use_big_obj = 1;
13562 break;
13563#endif
13564
d1982f93 13565 case OPTION_MOMIT_LOCK_PREFIX:
d022bddd
IT
13566 if (strcasecmp (arg, "yes") == 0)
13567 omit_lock_prefix = 1;
13568 else if (strcasecmp (arg, "no") == 0)
13569 omit_lock_prefix = 0;
13570 else
13571 as_fatal (_("invalid -momit-lock-prefix= option: `%s'"), arg);
13572 break;
13573
e4e00185
AS
13574 case OPTION_MFENCE_AS_LOCK_ADD:
13575 if (strcasecmp (arg, "yes") == 0)
13576 avoid_fence = 1;
13577 else if (strcasecmp (arg, "no") == 0)
13578 avoid_fence = 0;
13579 else
13580 as_fatal (_("invalid -mfence-as-lock-add= option: `%s'"), arg);
13581 break;
13582
ae531041
L
13583 case OPTION_MLFENCE_AFTER_LOAD:
13584 if (strcasecmp (arg, "yes") == 0)
13585 lfence_after_load = 1;
13586 else if (strcasecmp (arg, "no") == 0)
13587 lfence_after_load = 0;
13588 else
13589 as_fatal (_("invalid -mlfence-after-load= option: `%s'"), arg);
13590 break;
13591
13592 case OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH:
13593 if (strcasecmp (arg, "all") == 0)
a09f656b 13594 {
13595 lfence_before_indirect_branch = lfence_branch_all;
13596 if (lfence_before_ret == lfence_before_ret_none)
13597 lfence_before_ret = lfence_before_ret_shl;
13598 }
ae531041
L
13599 else if (strcasecmp (arg, "memory") == 0)
13600 lfence_before_indirect_branch = lfence_branch_memory;
13601 else if (strcasecmp (arg, "register") == 0)
13602 lfence_before_indirect_branch = lfence_branch_register;
13603 else if (strcasecmp (arg, "none") == 0)
13604 lfence_before_indirect_branch = lfence_branch_none;
13605 else
13606 as_fatal (_("invalid -mlfence-before-indirect-branch= option: `%s'"),
13607 arg);
13608 break;
13609
13610 case OPTION_MLFENCE_BEFORE_RET:
13611 if (strcasecmp (arg, "or") == 0)
13612 lfence_before_ret = lfence_before_ret_or;
13613 else if (strcasecmp (arg, "not") == 0)
13614 lfence_before_ret = lfence_before_ret_not;
a09f656b 13615 else if (strcasecmp (arg, "shl") == 0 || strcasecmp (arg, "yes") == 0)
13616 lfence_before_ret = lfence_before_ret_shl;
ae531041
L
13617 else if (strcasecmp (arg, "none") == 0)
13618 lfence_before_ret = lfence_before_ret_none;
13619 else
13620 as_fatal (_("invalid -mlfence-before-ret= option: `%s'"),
13621 arg);
13622 break;
13623
0cb4071e
L
13624 case OPTION_MRELAX_RELOCATIONS:
13625 if (strcasecmp (arg, "yes") == 0)
13626 generate_relax_relocations = 1;
13627 else if (strcasecmp (arg, "no") == 0)
13628 generate_relax_relocations = 0;
13629 else
13630 as_fatal (_("invalid -mrelax-relocations= option: `%s'"), arg);
13631 break;
13632
e379e5f3
L
13633 case OPTION_MALIGN_BRANCH_BOUNDARY:
13634 {
13635 char *end;
13636 long int align = strtoul (arg, &end, 0);
13637 if (*end == '\0')
13638 {
13639 if (align == 0)
13640 {
13641 align_branch_power = 0;
13642 break;
13643 }
13644 else if (align >= 16)
13645 {
13646 int align_power;
13647 for (align_power = 0;
13648 (align & 1) == 0;
13649 align >>= 1, align_power++)
13650 continue;
13651 /* Limit alignment power to 31. */
13652 if (align == 1 && align_power < 32)
13653 {
13654 align_branch_power = align_power;
13655 break;
13656 }
13657 }
13658 }
13659 as_fatal (_("invalid -malign-branch-boundary= value: %s"), arg);
13660 }
13661 break;
13662
13663 case OPTION_MALIGN_BRANCH_PREFIX_SIZE:
13664 {
13665 char *end;
13666 int align = strtoul (arg, &end, 0);
13667 /* Some processors only support 5 prefixes. */
13668 if (*end == '\0' && align >= 0 && align < 6)
13669 {
13670 align_branch_prefix_size = align;
13671 break;
13672 }
13673 as_fatal (_("invalid -malign-branch-prefix-size= value: %s"),
13674 arg);
13675 }
13676 break;
13677
13678 case OPTION_MALIGN_BRANCH:
13679 align_branch = 0;
13680 saved = xstrdup (arg);
13681 type = saved;
13682 do
13683 {
13684 next = strchr (type, '+');
13685 if (next)
13686 *next++ = '\0';
13687 if (strcasecmp (type, "jcc") == 0)
13688 align_branch |= align_branch_jcc_bit;
13689 else if (strcasecmp (type, "fused") == 0)
13690 align_branch |= align_branch_fused_bit;
13691 else if (strcasecmp (type, "jmp") == 0)
13692 align_branch |= align_branch_jmp_bit;
13693 else if (strcasecmp (type, "call") == 0)
13694 align_branch |= align_branch_call_bit;
13695 else if (strcasecmp (type, "ret") == 0)
13696 align_branch |= align_branch_ret_bit;
13697 else if (strcasecmp (type, "indirect") == 0)
13698 align_branch |= align_branch_indirect_bit;
13699 else
13700 as_fatal (_("invalid -malign-branch= option: `%s'"), arg);
13701 type = next;
13702 }
13703 while (next != NULL);
13704 free (saved);
13705 break;
13706
76cf450b
L
13707 case OPTION_MBRANCHES_WITH_32B_BOUNDARIES:
13708 align_branch_power = 5;
13709 align_branch_prefix_size = 5;
13710 align_branch = (align_branch_jcc_bit
13711 | align_branch_fused_bit
13712 | align_branch_jmp_bit);
13713 break;
13714
5db04b09 13715 case OPTION_MAMD64:
4b5aaf5f 13716 isa64 = amd64;
5db04b09
L
13717 break;
13718
13719 case OPTION_MINTEL64:
4b5aaf5f 13720 isa64 = intel64;
5db04b09
L
13721 break;
13722
b6f8c7c4
L
13723 case 'O':
13724 if (arg == NULL)
13725 {
13726 optimize = 1;
13727 /* Turn off -Os. */
13728 optimize_for_space = 0;
13729 }
13730 else if (*arg == 's')
13731 {
13732 optimize_for_space = 1;
13733 /* Turn on all encoding optimizations. */
41fd2579 13734 optimize = INT_MAX;
b6f8c7c4
L
13735 }
13736 else
13737 {
13738 optimize = atoi (arg);
13739 /* Turn off -Os. */
13740 optimize_for_space = 0;
13741 }
13742 break;
13743
252b5132
RH
13744 default:
13745 return 0;
13746 }
13747 return 1;
13748}
13749
8a2c8fef
L
13750#define MESSAGE_TEMPLATE \
13751" "
13752
293f5f65
L
13753static char *
13754output_message (FILE *stream, char *p, char *message, char *start,
13755 int *left_p, const char *name, int len)
13756{
13757 int size = sizeof (MESSAGE_TEMPLATE);
13758 int left = *left_p;
13759
13760 /* Reserve 2 spaces for ", " or ",\0" */
13761 left -= len + 2;
13762
13763 /* Check if there is any room. */
13764 if (left >= 0)
13765 {
13766 if (p != start)
13767 {
13768 *p++ = ',';
13769 *p++ = ' ';
13770 }
13771 p = mempcpy (p, name, len);
13772 }
13773 else
13774 {
13775 /* Output the current message now and start a new one. */
13776 *p++ = ',';
13777 *p = '\0';
13778 fprintf (stream, "%s\n", message);
13779 p = start;
13780 left = size - (start - message) - len - 2;
13781
13782 gas_assert (left >= 0);
13783
13784 p = mempcpy (p, name, len);
13785 }
13786
13787 *left_p = left;
13788 return p;
13789}
13790
8a2c8fef 13791static void
1ded5609 13792show_arch (FILE *stream, int ext, int check)
8a2c8fef
L
13793{
13794 static char message[] = MESSAGE_TEMPLATE;
13795 char *start = message + 27;
13796 char *p;
13797 int size = sizeof (MESSAGE_TEMPLATE);
13798 int left;
13799 const char *name;
13800 int len;
13801 unsigned int j;
13802
13803 p = start;
13804 left = size - (start - message);
3ce2ebcf
JB
13805
13806 if (!ext && check)
13807 {
13808 p = output_message (stream, p, message, start, &left,
13809 STRING_COMMA_LEN ("default"));
f68697e8
JB
13810 p = output_message (stream, p, message, start, &left,
13811 STRING_COMMA_LEN ("push"));
13812 p = output_message (stream, p, message, start, &left,
13813 STRING_COMMA_LEN ("pop"));
3ce2ebcf
JB
13814 }
13815
8a2c8fef
L
13816 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
13817 {
13818 /* Should it be skipped? */
13819 if (cpu_arch [j].skip)
13820 continue;
13821
13822 name = cpu_arch [j].name;
13823 len = cpu_arch [j].len;
ae89daec 13824 if (cpu_arch[j].type == PROCESSOR_NONE)
8a2c8fef
L
13825 {
13826 /* It is an extension. Skip if we aren't asked to show it. */
ae89daec 13827 if (!ext || cpu_flags_all_zero (&cpu_arch[j].enable))
8a2c8fef
L
13828 continue;
13829 }
13830 else if (ext)
13831 {
13832 /* It is an processor. Skip if we show only extension. */
13833 continue;
13834 }
ae89daec 13835 else if (check && ! cpu_arch[j].enable.bitfield.cpui386)
1ded5609
JB
13836 {
13837 /* It is an impossible processor - skip. */
13838 continue;
13839 }
8a2c8fef 13840
293f5f65 13841 p = output_message (stream, p, message, start, &left, name, len);
8a2c8fef
L
13842 }
13843
293f5f65
L
13844 /* Display disabled extensions. */
13845 if (ext)
ae89daec 13846 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
293f5f65 13847 {
ae89daec
JB
13848 char *str;
13849
13850 if (cpu_arch[j].type != PROCESSOR_NONE
13851 || !cpu_flags_all_zero (&cpu_arch[j].enable))
13852 continue;
13853 str = xasprintf ("no%s", cpu_arch[j].name);
13854 p = output_message (stream, p, message, start, &left, str,
13855 strlen (str));
13856 free (str);
293f5f65
L
13857 }
13858
8a2c8fef
L
13859 *p = '\0';
13860 fprintf (stream, "%s\n", message);
13861}
13862
252b5132 13863void
8a2c8fef 13864md_show_usage (FILE *stream)
252b5132 13865{
4cc782b5
ILT
13866#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13867 fprintf (stream, _("\
d4693039 13868 -Qy, -Qn ignored\n\
a38cf1db 13869 -V print assembler version number\n\
b3b91714
AM
13870 -k ignored\n"));
13871#endif
13872 fprintf (stream, _("\
7ebd68d1
NC
13873 -n do not optimize code alignment\n\
13874 -O{012s} attempt some code optimizations\n\
b3b91714
AM
13875 -q quieten some warnings\n"));
13876#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13877 fprintf (stream, _("\
a38cf1db 13878 -s ignored\n"));
b3b91714 13879#endif
b00af7c8
JB
13880#ifdef BFD64
13881# if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13882 fprintf (stream, _("\
13883 --32/--64/--x32 generate 32bit/64bit/x32 object\n"));
13884# elif defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O)
751d281c 13885 fprintf (stream, _("\
b00af7c8
JB
13886 --32/--64 generate 32bit/64bit object\n"));
13887# endif
751d281c 13888#endif
b3b91714
AM
13889#ifdef SVR4_COMMENT_CHARS
13890 fprintf (stream, _("\
13891 --divide do not treat `/' as a comment character\n"));
a38cf1db
AM
13892#else
13893 fprintf (stream, _("\
b3b91714 13894 --divide ignored\n"));
4cc782b5 13895#endif
9103f4f4 13896 fprintf (stream, _("\
6305a203 13897 -march=CPU[,+EXTENSION...]\n\
8a2c8fef 13898 generate code for CPU and EXTENSION, CPU is one of:\n"));
1ded5609 13899 show_arch (stream, 0, 1);
8a2c8fef 13900 fprintf (stream, _("\
ae89daec 13901 EXTENSION is combination of (possibly \"no\"-prefixed):\n"));
1ded5609 13902 show_arch (stream, 1, 0);
6305a203 13903 fprintf (stream, _("\
8a2c8fef 13904 -mtune=CPU optimize for CPU, CPU is one of:\n"));
1ded5609 13905 show_arch (stream, 0, 0);
ba104c83 13906 fprintf (stream, _("\
c0f3af97
L
13907 -msse2avx encode SSE instructions with VEX prefix\n"));
13908 fprintf (stream, _("\
c8480b58
L
13909 -muse-unaligned-vector-move\n\
13910 encode aligned vector move as unaligned vector move\n"));
13911 fprintf (stream, _("\
7c5c05ef 13912 -msse-check=[none|error|warning] (default: warning)\n\
daf50ae7
L
13913 check SSE instructions\n"));
13914 fprintf (stream, _("\
7c5c05ef 13915 -moperand-check=[none|error|warning] (default: warning)\n\
7bab8ab5
JB
13916 check operand combinations for validity\n"));
13917 fprintf (stream, _("\
7c5c05ef
L
13918 -mavxscalar=[128|256] (default: 128)\n\
13919 encode scalar AVX instructions with specific vector\n\
539f890d
L
13920 length\n"));
13921 fprintf (stream, _("\
03751133
L
13922 -mvexwig=[0|1] (default: 0)\n\
13923 encode VEX instructions with specific VEX.W value\n\
13924 for VEX.W bit ignored instructions\n"));
13925 fprintf (stream, _("\
7c5c05ef
L
13926 -mevexlig=[128|256|512] (default: 128)\n\
13927 encode scalar EVEX instructions with specific vector\n\
43234a1e
L
13928 length\n"));
13929 fprintf (stream, _("\
7c5c05ef
L
13930 -mevexwig=[0|1] (default: 0)\n\
13931 encode EVEX instructions with specific EVEX.W value\n\
43234a1e
L
13932 for EVEX.W bit ignored instructions\n"));
13933 fprintf (stream, _("\
7c5c05ef 13934 -mevexrcig=[rne|rd|ru|rz] (default: rne)\n\
d3d3c6db
IT
13935 encode EVEX instructions with specific EVEX.RC value\n\
13936 for SAE-only ignored instructions\n"));
13937 fprintf (stream, _("\
7c5c05ef
L
13938 -mmnemonic=[att|intel] "));
13939 if (SYSV386_COMPAT)
13940 fprintf (stream, _("(default: att)\n"));
13941 else
13942 fprintf (stream, _("(default: intel)\n"));
13943 fprintf (stream, _("\
13944 use AT&T/Intel mnemonic\n"));
ba104c83 13945 fprintf (stream, _("\
7c5c05ef
L
13946 -msyntax=[att|intel] (default: att)\n\
13947 use AT&T/Intel syntax\n"));
ba104c83
L
13948 fprintf (stream, _("\
13949 -mindex-reg support pseudo index registers\n"));
13950 fprintf (stream, _("\
13951 -mnaked-reg don't require `%%' prefix for registers\n"));
13952 fprintf (stream, _("\
7e8b059b 13953 -madd-bnd-prefix add BND prefix for all valid branches\n"));
b4a3a7b4 13954#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8dcea932
L
13955 fprintf (stream, _("\
13956 -mshared disable branch optimization for shared code\n"));
b4a3a7b4
L
13957 fprintf (stream, _("\
13958 -mx86-used-note=[no|yes] "));
13959 if (DEFAULT_X86_USED_NOTE)
13960 fprintf (stream, _("(default: yes)\n"));
13961 else
13962 fprintf (stream, _("(default: no)\n"));
13963 fprintf (stream, _("\
13964 generate x86 used ISA and feature properties\n"));
13965#endif
13966#if defined (TE_PE) || defined (TE_PEP)
167ad85b
TG
13967 fprintf (stream, _("\
13968 -mbig-obj generate big object files\n"));
13969#endif
d022bddd 13970 fprintf (stream, _("\
7c5c05ef 13971 -momit-lock-prefix=[no|yes] (default: no)\n\
d022bddd 13972 strip all lock prefixes\n"));
5db04b09 13973 fprintf (stream, _("\
7c5c05ef 13974 -mfence-as-lock-add=[no|yes] (default: no)\n\
e4e00185
AS
13975 encode lfence, mfence and sfence as\n\
13976 lock addl $0x0, (%%{re}sp)\n"));
13977 fprintf (stream, _("\
7c5c05ef
L
13978 -mrelax-relocations=[no|yes] "));
13979 if (DEFAULT_GENERATE_X86_RELAX_RELOCATIONS)
13980 fprintf (stream, _("(default: yes)\n"));
13981 else
13982 fprintf (stream, _("(default: no)\n"));
13983 fprintf (stream, _("\
0cb4071e
L
13984 generate relax relocations\n"));
13985 fprintf (stream, _("\
e379e5f3
L
13986 -malign-branch-boundary=NUM (default: 0)\n\
13987 align branches within NUM byte boundary\n"));
13988 fprintf (stream, _("\
13989 -malign-branch=TYPE[+TYPE...] (default: jcc+fused+jmp)\n\
13990 TYPE is combination of jcc, fused, jmp, call, ret,\n\
13991 indirect\n\
13992 specify types of branches to align\n"));
13993 fprintf (stream, _("\
13994 -malign-branch-prefix-size=NUM (default: 5)\n\
13995 align branches with NUM prefixes per instruction\n"));
13996 fprintf (stream, _("\
76cf450b
L
13997 -mbranches-within-32B-boundaries\n\
13998 align branches within 32 byte boundary\n"));
13999 fprintf (stream, _("\
ae531041
L
14000 -mlfence-after-load=[no|yes] (default: no)\n\
14001 generate lfence after load\n"));
14002 fprintf (stream, _("\
14003 -mlfence-before-indirect-branch=[none|all|register|memory] (default: none)\n\
14004 generate lfence before indirect near branch\n"));
14005 fprintf (stream, _("\
a09f656b 14006 -mlfence-before-ret=[none|or|not|shl|yes] (default: none)\n\
ae531041
L
14007 generate lfence before ret\n"));
14008 fprintf (stream, _("\
7c5c05ef 14009 -mamd64 accept only AMD64 ISA [default]\n"));
5db04b09
L
14010 fprintf (stream, _("\
14011 -mintel64 accept only Intel64 ISA\n"));
252b5132
RH
14012}
14013
3e73aa7c 14014#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
321098a5 14015 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
e57f8c65 14016 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
252b5132
RH
14017
14018/* Pick the target format to use. */
14019
47926f60 14020const char *
e3bb37b5 14021i386_target_format (void)
252b5132 14022{
d34049e8 14023 if (startswith (default_arch, "x86_64"))
351f65ca
L
14024 {
14025 update_code_flag (CODE_64BIT, 1);
14026 if (default_arch[6] == '\0')
7f56bc95 14027 x86_elf_abi = X86_64_ABI;
351f65ca 14028 else
7f56bc95 14029 x86_elf_abi = X86_64_X32_ABI;
351f65ca 14030 }
3e73aa7c 14031 else if (!strcmp (default_arch, "i386"))
78f12dd3 14032 update_code_flag (CODE_32BIT, 1);
5197d474
L
14033 else if (!strcmp (default_arch, "iamcu"))
14034 {
14035 update_code_flag (CODE_32BIT, 1);
14036 if (cpu_arch_isa == PROCESSOR_UNKNOWN)
14037 {
14038 static const i386_cpu_flags iamcu_flags = CPU_IAMCU_FLAGS;
14039 cpu_arch_name = "iamcu";
d92c7521 14040 free (cpu_sub_arch_name);
5197d474
L
14041 cpu_sub_arch_name = NULL;
14042 cpu_arch_flags = iamcu_flags;
14043 cpu_arch_isa = PROCESSOR_IAMCU;
14044 cpu_arch_isa_flags = iamcu_flags;
14045 if (!cpu_arch_tune_set)
14046 {
14047 cpu_arch_tune = cpu_arch_isa;
14048 cpu_arch_tune_flags = cpu_arch_isa_flags;
14049 }
14050 }
8d471ec1 14051 else if (cpu_arch_isa != PROCESSOR_IAMCU)
5197d474
L
14052 as_fatal (_("Intel MCU doesn't support `%s' architecture"),
14053 cpu_arch_name);
14054 }
3e73aa7c 14055 else
2b5d6a91 14056 as_fatal (_("unknown architecture"));
89507696
JB
14057
14058 if (cpu_flags_all_zero (&cpu_arch_isa_flags))
ae89daec 14059 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
89507696 14060 if (cpu_flags_all_zero (&cpu_arch_tune_flags))
ae89daec 14061 cpu_arch_tune_flags = cpu_arch[flag_code == CODE_64BIT].enable;
89507696 14062
252b5132
RH
14063 switch (OUTPUT_FLAVOR)
14064 {
9384f2ff 14065#if defined (OBJ_MAYBE_AOUT) || defined (OBJ_AOUT)
4c63da97 14066 case bfd_target_aout_flavour:
47926f60 14067 return AOUT_TARGET_FORMAT;
4c63da97 14068#endif
9384f2ff
AM
14069#if defined (OBJ_MAYBE_COFF) || defined (OBJ_COFF)
14070# if defined (TE_PE) || defined (TE_PEP)
14071 case bfd_target_coff_flavour:
167ad85b 14072 if (flag_code == CODE_64BIT)
eb19308f
JB
14073 {
14074 object_64bit = 1;
14075 return use_big_obj ? "pe-bigobj-x86-64" : "pe-x86-64";
14076 }
14077 return use_big_obj ? "pe-bigobj-i386" : "pe-i386";
9384f2ff 14078# elif defined (TE_GO32)
0561d57c
JK
14079 case bfd_target_coff_flavour:
14080 return "coff-go32";
9384f2ff 14081# else
252b5132
RH
14082 case bfd_target_coff_flavour:
14083 return "coff-i386";
9384f2ff 14084# endif
4c63da97 14085#endif
3e73aa7c 14086#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
252b5132 14087 case bfd_target_elf_flavour:
3e73aa7c 14088 {
351f65ca
L
14089 const char *format;
14090
14091 switch (x86_elf_abi)
4fa24527 14092 {
351f65ca
L
14093 default:
14094 format = ELF_TARGET_FORMAT;
e379e5f3
L
14095#ifndef TE_SOLARIS
14096 tls_get_addr = "___tls_get_addr";
14097#endif
351f65ca 14098 break;
7f56bc95 14099 case X86_64_ABI:
351f65ca 14100 use_rela_relocations = 1;
4fa24527 14101 object_64bit = 1;
e379e5f3
L
14102#ifndef TE_SOLARIS
14103 tls_get_addr = "__tls_get_addr";
14104#endif
351f65ca
L
14105 format = ELF_TARGET_FORMAT64;
14106 break;
7f56bc95 14107 case X86_64_X32_ABI:
4fa24527 14108 use_rela_relocations = 1;
351f65ca 14109 object_64bit = 1;
e379e5f3
L
14110#ifndef TE_SOLARIS
14111 tls_get_addr = "__tls_get_addr";
14112#endif
862be3fb 14113 disallow_64bit_reloc = 1;
351f65ca
L
14114 format = ELF_TARGET_FORMAT32;
14115 break;
4fa24527 14116 }
c085ab00 14117 if (cpu_arch_isa == PROCESSOR_IAMCU)
81486035
L
14118 {
14119 if (x86_elf_abi != I386_ABI)
14120 as_fatal (_("Intel MCU is 32bit only"));
14121 return ELF_TARGET_IAMCU_FORMAT;
14122 }
8a9036a4 14123 else
351f65ca 14124 return format;
3e73aa7c 14125 }
e57f8c65
TG
14126#endif
14127#if defined (OBJ_MACH_O)
14128 case bfd_target_mach_o_flavour:
d382c579
TG
14129 if (flag_code == CODE_64BIT)
14130 {
14131 use_rela_relocations = 1;
14132 object_64bit = 1;
14133 return "mach-o-x86-64";
14134 }
14135 else
14136 return "mach-o-i386";
4c63da97 14137#endif
252b5132
RH
14138 default:
14139 abort ();
14140 return NULL;
14141 }
14142}
14143
47926f60 14144#endif /* OBJ_MAYBE_ more than one */
252b5132 14145\f
252b5132 14146symbolS *
7016a5d5 14147md_undefined_symbol (char *name)
252b5132 14148{
18dc2407
ILT
14149 if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
14150 && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
14151 && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
14152 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
24eab124
AM
14153 {
14154 if (!GOT_symbol)
14155 {
14156 if (symbol_find (name))
14157 as_bad (_("GOT already in symbol table"));
14158 GOT_symbol = symbol_new (name, undefined_section,
e01e1cee 14159 &zero_address_frag, 0);
24eab124
AM
14160 };
14161 return GOT_symbol;
14162 }
252b5132
RH
14163 return 0;
14164}
14165
14166/* Round up a section size to the appropriate boundary. */
47926f60 14167
252b5132 14168valueT
7016a5d5 14169md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
252b5132 14170{
4c63da97
AM
14171#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
14172 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
14173 {
14174 /* For a.out, force the section size to be aligned. If we don't do
14175 this, BFD will align it for us, but it will not write out the
14176 final bytes of the section. This may be a bug in BFD, but it is
14177 easier to fix it here since that is how the other a.out targets
14178 work. */
14179 int align;
14180
fd361982 14181 align = bfd_section_alignment (segment);
8d3842cd 14182 size = ((size + (1 << align) - 1) & (-((valueT) 1 << align)));
4c63da97 14183 }
252b5132
RH
14184#endif
14185
14186 return size;
14187}
14188
14189/* On the i386, PC-relative offsets are relative to the start of the
14190 next instruction. That is, the address of the offset, plus its
14191 size, since the offset is always the last part of the insn. */
14192
14193long
e3bb37b5 14194md_pcrel_from (fixS *fixP)
252b5132
RH
14195{
14196 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
14197}
14198
14199#ifndef I386COFF
14200
14201static void
e3bb37b5 14202s_bss (int ignore ATTRIBUTE_UNUSED)
252b5132 14203{
29b0f896 14204 int temp;
252b5132 14205
8a75718c
JB
14206#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14207 if (IS_ELF)
14208 obj_elf_section_change_hook ();
14209#endif
252b5132
RH
14210 temp = get_absolute_expression ();
14211 subseg_set (bss_section, (subsegT) temp);
14212 demand_empty_rest_of_line ();
14213}
14214
14215#endif
14216
e379e5f3
L
14217/* Remember constant directive. */
14218
14219void
14220i386_cons_align (int ignore ATTRIBUTE_UNUSED)
14221{
14222 if (last_insn.kind != last_insn_directive
14223 && (bfd_section_flags (now_seg) & SEC_CODE))
14224 {
14225 last_insn.seg = now_seg;
14226 last_insn.kind = last_insn_directive;
14227 last_insn.name = "constant directive";
14228 last_insn.file = as_where (&last_insn.line);
ae531041
L
14229 if (lfence_before_ret != lfence_before_ret_none)
14230 {
14231 if (lfence_before_indirect_branch != lfence_branch_none)
14232 as_warn (_("constant directive skips -mlfence-before-ret "
14233 "and -mlfence-before-indirect-branch"));
14234 else
14235 as_warn (_("constant directive skips -mlfence-before-ret"));
14236 }
14237 else if (lfence_before_indirect_branch != lfence_branch_none)
14238 as_warn (_("constant directive skips -mlfence-before-indirect-branch"));
e379e5f3
L
14239 }
14240}
14241
3abbafc2 14242int
e3bb37b5 14243i386_validate_fix (fixS *fixp)
252b5132 14244{
e52a16f2
JB
14245 if (fixp->fx_addsy && S_GET_SEGMENT(fixp->fx_addsy) == reg_section)
14246 {
14247 reloc_howto_type *howto;
14248
14249 howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
14250 as_bad_where (fixp->fx_file, fixp->fx_line,
14251 _("invalid %s relocation against register"),
14252 howto ? howto->name : "<unknown>");
14253 return 0;
14254 }
14255
3abbafc2
JB
14256#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14257 if (fixp->fx_r_type == BFD_RELOC_SIZE32
14258 || fixp->fx_r_type == BFD_RELOC_SIZE64)
14259 return IS_ELF && fixp->fx_addsy
14260 && (!S_IS_DEFINED (fixp->fx_addsy)
14261 || S_IS_EXTERNAL (fixp->fx_addsy));
14262#endif
14263
02a86693 14264 if (fixp->fx_subsy)
252b5132 14265 {
02a86693 14266 if (fixp->fx_subsy == GOT_symbol)
23df1078 14267 {
02a86693
L
14268 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
14269 {
14270 if (!object_64bit)
14271 abort ();
14272#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14273 if (fixp->fx_tcbit2)
56ceb5b5
L
14274 fixp->fx_r_type = (fixp->fx_tcbit
14275 ? BFD_RELOC_X86_64_REX_GOTPCRELX
14276 : BFD_RELOC_X86_64_GOTPCRELX);
02a86693
L
14277 else
14278#endif
14279 fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
14280 }
d6ab8113 14281 else
02a86693
L
14282 {
14283 if (!object_64bit)
14284 fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
14285 else
14286 fixp->fx_r_type = BFD_RELOC_X86_64_GOTOFF64;
14287 }
14288 fixp->fx_subsy = 0;
23df1078 14289 }
252b5132 14290 }
02a86693 14291#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2585b7a5 14292 else
02a86693 14293 {
2585b7a5
L
14294 /* NB: Commit 292676c1 resolved PLT32 reloc aganst local symbol
14295 to section. Since PLT32 relocation must be against symbols,
14296 turn such PLT32 relocation into PC32 relocation. */
14297 if (fixp->fx_addsy
14298 && (fixp->fx_r_type == BFD_RELOC_386_PLT32
14299 || fixp->fx_r_type == BFD_RELOC_X86_64_PLT32)
14300 && symbol_section_p (fixp->fx_addsy))
14301 fixp->fx_r_type = BFD_RELOC_32_PCREL;
14302 if (!object_64bit)
14303 {
14304 if (fixp->fx_r_type == BFD_RELOC_386_GOT32
14305 && fixp->fx_tcbit2)
14306 fixp->fx_r_type = BFD_RELOC_386_GOT32X;
14307 }
02a86693
L
14308 }
14309#endif
3abbafc2
JB
14310
14311 return 1;
252b5132
RH
14312}
14313
252b5132 14314arelent *
7016a5d5 14315tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
14316{
14317 arelent *rel;
14318 bfd_reloc_code_real_type code;
14319
14320 switch (fixp->fx_r_type)
14321 {
8ce3d284 14322#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3abbafc2
JB
14323 symbolS *sym;
14324
8fd4256d
L
14325 case BFD_RELOC_SIZE32:
14326 case BFD_RELOC_SIZE64:
3abbafc2
JB
14327 if (fixp->fx_addsy
14328 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))
14329 && (!fixp->fx_subsy
14330 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))))
14331 sym = fixp->fx_addsy;
14332 else if (fixp->fx_subsy
14333 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))
14334 && (!fixp->fx_addsy
14335 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))))
14336 sym = fixp->fx_subsy;
14337 else
14338 sym = NULL;
14339 if (IS_ELF && sym && S_IS_DEFINED (sym) && !S_IS_EXTERNAL (sym))
8fd4256d
L
14340 {
14341 /* Resolve size relocation against local symbol to size of
14342 the symbol plus addend. */
3abbafc2 14343 valueT value = S_GET_SIZE (sym);
44f87162 14344
3abbafc2
JB
14345 if (symbol_get_bfdsym (sym)->flags & BSF_SECTION_SYM)
14346 value = bfd_section_size (S_GET_SEGMENT (sym));
14347 if (sym == fixp->fx_subsy)
14348 {
14349 value = -value;
14350 if (fixp->fx_addsy)
14351 value += S_GET_VALUE (fixp->fx_addsy);
14352 }
14353 else if (fixp->fx_subsy)
14354 value -= S_GET_VALUE (fixp->fx_subsy);
44f87162 14355 value += fixp->fx_offset;
8fd4256d 14356 if (fixp->fx_r_type == BFD_RELOC_SIZE32
d965814f 14357 && object_64bit
8fd4256d
L
14358 && !fits_in_unsigned_long (value))
14359 as_bad_where (fixp->fx_file, fixp->fx_line,
14360 _("symbol size computation overflow"));
14361 fixp->fx_addsy = NULL;
14362 fixp->fx_subsy = NULL;
14363 md_apply_fix (fixp, (valueT *) &value, NULL);
14364 return NULL;
14365 }
3abbafc2
JB
14366 if (!fixp->fx_addsy || fixp->fx_subsy)
14367 {
14368 as_bad_where (fixp->fx_file, fixp->fx_line,
14369 "unsupported expression involving @size");
14370 return NULL;
14371 }
8ce3d284 14372#endif
1a0670f3 14373 /* Fall through. */
8fd4256d 14374
3e73aa7c
JH
14375 case BFD_RELOC_X86_64_PLT32:
14376 case BFD_RELOC_X86_64_GOT32:
14377 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
14378 case BFD_RELOC_X86_64_GOTPCRELX:
14379 case BFD_RELOC_X86_64_REX_GOTPCRELX:
252b5132
RH
14380 case BFD_RELOC_386_PLT32:
14381 case BFD_RELOC_386_GOT32:
02a86693 14382 case BFD_RELOC_386_GOT32X:
252b5132
RH
14383 case BFD_RELOC_386_GOTOFF:
14384 case BFD_RELOC_386_GOTPC:
13ae64f3
JJ
14385 case BFD_RELOC_386_TLS_GD:
14386 case BFD_RELOC_386_TLS_LDM:
14387 case BFD_RELOC_386_TLS_LDO_32:
14388 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
14389 case BFD_RELOC_386_TLS_IE:
14390 case BFD_RELOC_386_TLS_GOTIE:
13ae64f3
JJ
14391 case BFD_RELOC_386_TLS_LE_32:
14392 case BFD_RELOC_386_TLS_LE:
67a4f2b7
AO
14393 case BFD_RELOC_386_TLS_GOTDESC:
14394 case BFD_RELOC_386_TLS_DESC_CALL:
bffbf940
JJ
14395 case BFD_RELOC_X86_64_TLSGD:
14396 case BFD_RELOC_X86_64_TLSLD:
14397 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 14398 case BFD_RELOC_X86_64_DTPOFF64:
bffbf940
JJ
14399 case BFD_RELOC_X86_64_GOTTPOFF:
14400 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113
JB
14401 case BFD_RELOC_X86_64_TPOFF64:
14402 case BFD_RELOC_X86_64_GOTOFF64:
14403 case BFD_RELOC_X86_64_GOTPC32:
7b81dfbb
AJ
14404 case BFD_RELOC_X86_64_GOT64:
14405 case BFD_RELOC_X86_64_GOTPCREL64:
14406 case BFD_RELOC_X86_64_GOTPC64:
14407 case BFD_RELOC_X86_64_GOTPLT64:
14408 case BFD_RELOC_X86_64_PLTOFF64:
67a4f2b7
AO
14409 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14410 case BFD_RELOC_X86_64_TLSDESC_CALL:
252b5132
RH
14411 case BFD_RELOC_RVA:
14412 case BFD_RELOC_VTABLE_ENTRY:
14413 case BFD_RELOC_VTABLE_INHERIT:
6482c264
NC
14414#ifdef TE_PE
14415 case BFD_RELOC_32_SECREL:
145667f8 14416 case BFD_RELOC_16_SECIDX:
6482c264 14417#endif
252b5132
RH
14418 code = fixp->fx_r_type;
14419 break;
dbbaec26
L
14420 case BFD_RELOC_X86_64_32S:
14421 if (!fixp->fx_pcrel)
14422 {
14423 /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32. */
14424 code = fixp->fx_r_type;
14425 break;
14426 }
1a0670f3 14427 /* Fall through. */
252b5132 14428 default:
93382f6d 14429 if (fixp->fx_pcrel)
252b5132 14430 {
93382f6d
AM
14431 switch (fixp->fx_size)
14432 {
14433 default:
b091f402
AM
14434 as_bad_where (fixp->fx_file, fixp->fx_line,
14435 _("can not do %d byte pc-relative relocation"),
14436 fixp->fx_size);
93382f6d
AM
14437 code = BFD_RELOC_32_PCREL;
14438 break;
14439 case 1: code = BFD_RELOC_8_PCREL; break;
14440 case 2: code = BFD_RELOC_16_PCREL; break;
d258b828 14441 case 4: code = BFD_RELOC_32_PCREL; break;
d6ab8113
JB
14442#ifdef BFD64
14443 case 8: code = BFD_RELOC_64_PCREL; break;
14444#endif
93382f6d
AM
14445 }
14446 }
14447 else
14448 {
14449 switch (fixp->fx_size)
14450 {
14451 default:
b091f402
AM
14452 as_bad_where (fixp->fx_file, fixp->fx_line,
14453 _("can not do %d byte relocation"),
14454 fixp->fx_size);
93382f6d
AM
14455 code = BFD_RELOC_32;
14456 break;
14457 case 1: code = BFD_RELOC_8; break;
14458 case 2: code = BFD_RELOC_16; break;
14459 case 4: code = BFD_RELOC_32; break;
937149dd 14460#ifdef BFD64
3e73aa7c 14461 case 8: code = BFD_RELOC_64; break;
937149dd 14462#endif
93382f6d 14463 }
252b5132
RH
14464 }
14465 break;
14466 }
252b5132 14467
d182319b
JB
14468 if ((code == BFD_RELOC_32
14469 || code == BFD_RELOC_32_PCREL
14470 || code == BFD_RELOC_X86_64_32S)
252b5132
RH
14471 && GOT_symbol
14472 && fixp->fx_addsy == GOT_symbol)
3e73aa7c 14473 {
4fa24527 14474 if (!object_64bit)
d6ab8113
JB
14475 code = BFD_RELOC_386_GOTPC;
14476 else
14477 code = BFD_RELOC_X86_64_GOTPC32;
3e73aa7c 14478 }
7b81dfbb
AJ
14479 if ((code == BFD_RELOC_64 || code == BFD_RELOC_64_PCREL)
14480 && GOT_symbol
14481 && fixp->fx_addsy == GOT_symbol)
14482 {
14483 code = BFD_RELOC_X86_64_GOTPC64;
14484 }
252b5132 14485
add39d23
TS
14486 rel = XNEW (arelent);
14487 rel->sym_ptr_ptr = XNEW (asymbol *);
49309057 14488 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
14489
14490 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
c87db184 14491
3e73aa7c
JH
14492 if (!use_rela_relocations)
14493 {
14494 /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
14495 vtable entry to be used in the relocation's section offset. */
14496 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
14497 rel->address = fixp->fx_offset;
fbeb56a4
DK
14498#if defined (OBJ_COFF) && defined (TE_PE)
14499 else if (fixp->fx_addsy && S_IS_WEAK (fixp->fx_addsy))
14500 rel->addend = fixp->fx_addnumber - (S_GET_VALUE (fixp->fx_addsy) * 2);
14501 else
14502#endif
c6682705 14503 rel->addend = 0;
3e73aa7c
JH
14504 }
14505 /* Use the rela in 64bit mode. */
252b5132 14506 else
3e73aa7c 14507 {
862be3fb
L
14508 if (disallow_64bit_reloc)
14509 switch (code)
14510 {
862be3fb
L
14511 case BFD_RELOC_X86_64_DTPOFF64:
14512 case BFD_RELOC_X86_64_TPOFF64:
14513 case BFD_RELOC_64_PCREL:
14514 case BFD_RELOC_X86_64_GOTOFF64:
14515 case BFD_RELOC_X86_64_GOT64:
14516 case BFD_RELOC_X86_64_GOTPCREL64:
14517 case BFD_RELOC_X86_64_GOTPC64:
14518 case BFD_RELOC_X86_64_GOTPLT64:
14519 case BFD_RELOC_X86_64_PLTOFF64:
14520 as_bad_where (fixp->fx_file, fixp->fx_line,
14521 _("cannot represent relocation type %s in x32 mode"),
14522 bfd_get_reloc_code_name (code));
14523 break;
14524 default:
14525 break;
14526 }
14527
062cd5e7
AS
14528 if (!fixp->fx_pcrel)
14529 rel->addend = fixp->fx_offset;
14530 else
14531 switch (code)
14532 {
14533 case BFD_RELOC_X86_64_PLT32:
14534 case BFD_RELOC_X86_64_GOT32:
14535 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
14536 case BFD_RELOC_X86_64_GOTPCRELX:
14537 case BFD_RELOC_X86_64_REX_GOTPCRELX:
bffbf940
JJ
14538 case BFD_RELOC_X86_64_TLSGD:
14539 case BFD_RELOC_X86_64_TLSLD:
14540 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7
AO
14541 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
14542 case BFD_RELOC_X86_64_TLSDESC_CALL:
062cd5e7
AS
14543 rel->addend = fixp->fx_offset - fixp->fx_size;
14544 break;
14545 default:
14546 rel->addend = (section->vma
14547 - fixp->fx_size
14548 + fixp->fx_addnumber
14549 + md_pcrel_from (fixp));
14550 break;
14551 }
3e73aa7c
JH
14552 }
14553
252b5132
RH
14554 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
14555 if (rel->howto == NULL)
14556 {
14557 as_bad_where (fixp->fx_file, fixp->fx_line,
d0b47220 14558 _("cannot represent relocation type %s"),
252b5132
RH
14559 bfd_get_reloc_code_name (code));
14560 /* Set howto to a garbage value so that we can keep going. */
14561 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
9c2799c2 14562 gas_assert (rel->howto != NULL);
252b5132
RH
14563 }
14564
14565 return rel;
14566}
14567
ee86248c 14568#include "tc-i386-intel.c"
54cfded0 14569
a60de03c
JB
14570void
14571tc_x86_parse_to_dw2regnum (expressionS *exp)
54cfded0 14572{
a60de03c
JB
14573 int saved_naked_reg;
14574 char saved_register_dot;
54cfded0 14575
a60de03c
JB
14576 saved_naked_reg = allow_naked_reg;
14577 allow_naked_reg = 1;
14578 saved_register_dot = register_chars['.'];
14579 register_chars['.'] = '.';
14580 allow_pseudo_reg = 1;
14581 expression_and_evaluate (exp);
14582 allow_pseudo_reg = 0;
14583 register_chars['.'] = saved_register_dot;
14584 allow_naked_reg = saved_naked_reg;
14585
e96d56a1 14586 if (exp->X_op == O_register && exp->X_add_number >= 0)
54cfded0 14587 {
a60de03c
JB
14588 if ((addressT) exp->X_add_number < i386_regtab_size)
14589 {
14590 exp->X_op = O_constant;
14591 exp->X_add_number = i386_regtab[exp->X_add_number]
14592 .dw2_regnum[flag_code >> 1];
14593 }
14594 else
14595 exp->X_op = O_illegal;
54cfded0 14596 }
54cfded0
AM
14597}
14598
14599void
14600tc_x86_frame_initial_instructions (void)
14601{
a60de03c
JB
14602 static unsigned int sp_regno[2];
14603
14604 if (!sp_regno[flag_code >> 1])
14605 {
14606 char *saved_input = input_line_pointer;
14607 char sp[][4] = {"esp", "rsp"};
14608 expressionS exp;
a4447b93 14609
a60de03c
JB
14610 input_line_pointer = sp[flag_code >> 1];
14611 tc_x86_parse_to_dw2regnum (&exp);
9c2799c2 14612 gas_assert (exp.X_op == O_constant);
a60de03c
JB
14613 sp_regno[flag_code >> 1] = exp.X_add_number;
14614 input_line_pointer = saved_input;
14615 }
a4447b93 14616
61ff971f
L
14617 cfi_add_CFA_def_cfa (sp_regno[flag_code >> 1], -x86_cie_data_alignment);
14618 cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
54cfded0 14619}
d2b2c203 14620
d7921315
L
14621int
14622x86_dwarf2_addr_size (void)
14623{
14624#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
14625 if (x86_elf_abi == X86_64_X32_ABI)
14626 return 4;
14627#endif
14628 return bfd_arch_bits_per_address (stdoutput) / 8;
14629}
14630
d2b2c203
DJ
14631int
14632i386_elf_section_type (const char *str, size_t len)
14633{
14634 if (flag_code == CODE_64BIT
14635 && len == sizeof ("unwind") - 1
d34049e8 14636 && startswith (str, "unwind"))
d2b2c203
DJ
14637 return SHT_X86_64_UNWIND;
14638
14639 return -1;
14640}
bb41ade5 14641
ad5fec3b
EB
14642#ifdef TE_SOLARIS
14643void
14644i386_solaris_fix_up_eh_frame (segT sec)
14645{
14646 if (flag_code == CODE_64BIT)
14647 elf_section_type (sec) = SHT_X86_64_UNWIND;
14648}
14649#endif
14650
bb41ade5
AM
14651#ifdef TE_PE
14652void
14653tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
14654{
91d6fa6a 14655 expressionS exp;
bb41ade5 14656
91d6fa6a
NC
14657 exp.X_op = O_secrel;
14658 exp.X_add_symbol = symbol;
14659 exp.X_add_number = 0;
14660 emit_expr (&exp, size);
bb41ade5
AM
14661}
14662#endif
3b22753a
L
14663
14664#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14665/* For ELF on x86-64, add support for SHF_X86_64_LARGE. */
14666
01e1a5bc 14667bfd_vma
6d4af3c2 14668x86_64_section_letter (int letter, const char **ptr_msg)
3b22753a
L
14669{
14670 if (flag_code == CODE_64BIT)
14671 {
14672 if (letter == 'l')
14673 return SHF_X86_64_LARGE;
14674
8f3bae45 14675 *ptr_msg = _("bad .section directive: want a,l,w,x,M,S,G,T in string");
64e74474 14676 }
3b22753a 14677 else
8f3bae45 14678 *ptr_msg = _("bad .section directive: want a,w,x,M,S,G,T in string");
3b22753a
L
14679 return -1;
14680}
14681
01e1a5bc 14682bfd_vma
3b22753a
L
14683x86_64_section_word (char *str, size_t len)
14684{
08dedd66 14685 if (len == 5 && flag_code == CODE_64BIT && startswith (str, "large"))
3b22753a
L
14686 return SHF_X86_64_LARGE;
14687
14688 return -1;
14689}
14690
14691static void
14692handle_large_common (int small ATTRIBUTE_UNUSED)
14693{
14694 if (flag_code != CODE_64BIT)
14695 {
14696 s_comm_internal (0, elf_common_parse);
14697 as_warn (_(".largecomm supported only in 64bit mode, producing .comm"));
14698 }
14699 else
14700 {
14701 static segT lbss_section;
14702 asection *saved_com_section_ptr = elf_com_section_ptr;
14703 asection *saved_bss_section = bss_section;
14704
14705 if (lbss_section == NULL)
14706 {
14707 flagword applicable;
14708 segT seg = now_seg;
14709 subsegT subseg = now_subseg;
14710
14711 /* The .lbss section is for local .largecomm symbols. */
14712 lbss_section = subseg_new (".lbss", 0);
14713 applicable = bfd_applicable_section_flags (stdoutput);
fd361982 14714 bfd_set_section_flags (lbss_section, applicable & SEC_ALLOC);
3b22753a
L
14715 seg_info (lbss_section)->bss = 1;
14716
14717 subseg_set (seg, subseg);
14718 }
14719
14720 elf_com_section_ptr = &_bfd_elf_large_com_section;
14721 bss_section = lbss_section;
14722
14723 s_comm_internal (0, elf_common_parse);
14724
14725 elf_com_section_ptr = saved_com_section_ptr;
14726 bss_section = saved_bss_section;
14727 }
14728}
14729#endif /* OBJ_ELF || OBJ_MAYBE_ELF */